abx500.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (C) ST-Ericsson 2010 - 2013
  3. * Author: Martin Persson <martin.persson@stericsson.com>
  4. * Hongbo Zhang <hongbo.zhang@linaro.org>
  5. * License Terms: GNU General Public License v2
  6. *
  7. * ABX500 does not provide auto ADC, so to monitor the required temperatures,
  8. * a periodic work is used. It is more important to not wake up the CPU than
  9. * to perform this job, hence the use of a deferred delay.
  10. *
  11. * A deferred delay for thermal monitor is considered safe because:
  12. * If the chip gets too hot during a sleep state it's most likely due to
  13. * external factors, such as the surrounding temperature. I.e. no SW decisions
  14. * will make any difference.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/hwmon.h>
  18. #include <linux/hwmon-sysfs.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm.h>
  26. #include <linux/slab.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/workqueue.h>
  29. #include "abx500.h"
  30. #define DEFAULT_MONITOR_DELAY HZ
  31. #define DEFAULT_MAX_TEMP 130
  32. static inline void schedule_monitor(struct abx500_temp *data)
  33. {
  34. data->work_active = true;
  35. schedule_delayed_work(&data->work, DEFAULT_MONITOR_DELAY);
  36. }
  37. static void threshold_updated(struct abx500_temp *data)
  38. {
  39. int i;
  40. for (i = 0; i < data->monitored_sensors; i++)
  41. if (data->max[i] != 0 || data->min[i] != 0) {
  42. schedule_monitor(data);
  43. return;
  44. }
  45. dev_dbg(&data->pdev->dev, "No active thresholds.\n");
  46. cancel_delayed_work_sync(&data->work);
  47. data->work_active = false;
  48. }
  49. static void gpadc_monitor(struct work_struct *work)
  50. {
  51. int temp, i, ret;
  52. char alarm_node[30];
  53. bool updated_min_alarm, updated_max_alarm;
  54. struct abx500_temp *data;
  55. data = container_of(work, struct abx500_temp, work.work);
  56. mutex_lock(&data->lock);
  57. for (i = 0; i < data->monitored_sensors; i++) {
  58. /* Thresholds are considered inactive if set to 0 */
  59. if (data->max[i] == 0 && data->min[i] == 0)
  60. continue;
  61. if (data->max[i] < data->min[i])
  62. continue;
  63. ret = data->ops.read_sensor(data, data->gpadc_addr[i], &temp);
  64. if (ret < 0) {
  65. dev_err(&data->pdev->dev, "GPADC read failed\n");
  66. continue;
  67. }
  68. updated_min_alarm = false;
  69. updated_max_alarm = false;
  70. if (data->min[i] != 0) {
  71. if (temp < data->min[i]) {
  72. if (data->min_alarm[i] == false) {
  73. data->min_alarm[i] = true;
  74. updated_min_alarm = true;
  75. }
  76. } else {
  77. if (data->min_alarm[i] == true) {
  78. data->min_alarm[i] = false;
  79. updated_min_alarm = true;
  80. }
  81. }
  82. }
  83. if (data->max[i] != 0) {
  84. if (temp > data->max[i]) {
  85. if (data->max_alarm[i] == false) {
  86. data->max_alarm[i] = true;
  87. updated_max_alarm = true;
  88. }
  89. } else if (temp < data->max[i] - data->max_hyst[i]) {
  90. if (data->max_alarm[i] == true) {
  91. data->max_alarm[i] = false;
  92. updated_max_alarm = true;
  93. }
  94. }
  95. }
  96. if (updated_min_alarm) {
  97. ret = sprintf(alarm_node, "temp%d_min_alarm", i + 1);
  98. sysfs_notify(&data->pdev->dev.kobj, NULL, alarm_node);
  99. }
  100. if (updated_max_alarm) {
  101. ret = sprintf(alarm_node, "temp%d_max_alarm", i + 1);
  102. sysfs_notify(&data->pdev->dev.kobj, NULL, alarm_node);
  103. }
  104. }
  105. schedule_monitor(data);
  106. mutex_unlock(&data->lock);
  107. }
  108. /* HWMON sysfs interfaces */
  109. static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
  110. char *buf)
  111. {
  112. struct abx500_temp *data = dev_get_drvdata(dev);
  113. /* Show chip name */
  114. return data->ops.show_name(dev, devattr, buf);
  115. }
  116. static ssize_t show_label(struct device *dev,
  117. struct device_attribute *devattr, char *buf)
  118. {
  119. struct abx500_temp *data = dev_get_drvdata(dev);
  120. /* Show each sensor label */
  121. return data->ops.show_label(dev, devattr, buf);
  122. }
  123. static ssize_t show_input(struct device *dev,
  124. struct device_attribute *devattr, char *buf)
  125. {
  126. int ret, temp;
  127. struct abx500_temp *data = dev_get_drvdata(dev);
  128. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  129. u8 gpadc_addr = data->gpadc_addr[attr->index];
  130. ret = data->ops.read_sensor(data, gpadc_addr, &temp);
  131. if (ret < 0)
  132. return ret;
  133. return sprintf(buf, "%d\n", temp);
  134. }
  135. /* Set functions (RW nodes) */
  136. static ssize_t set_min(struct device *dev, struct device_attribute *devattr,
  137. const char *buf, size_t count)
  138. {
  139. unsigned long val;
  140. struct abx500_temp *data = dev_get_drvdata(dev);
  141. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  142. int res = kstrtol(buf, 10, &val);
  143. if (res < 0)
  144. return res;
  145. val = clamp_val(val, 0, DEFAULT_MAX_TEMP);
  146. mutex_lock(&data->lock);
  147. data->min[attr->index] = val;
  148. threshold_updated(data);
  149. mutex_unlock(&data->lock);
  150. return count;
  151. }
  152. static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
  153. const char *buf, size_t count)
  154. {
  155. unsigned long val;
  156. struct abx500_temp *data = dev_get_drvdata(dev);
  157. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  158. int res = kstrtol(buf, 10, &val);
  159. if (res < 0)
  160. return res;
  161. val = clamp_val(val, 0, DEFAULT_MAX_TEMP);
  162. mutex_lock(&data->lock);
  163. data->max[attr->index] = val;
  164. threshold_updated(data);
  165. mutex_unlock(&data->lock);
  166. return count;
  167. }
  168. static ssize_t set_max_hyst(struct device *dev,
  169. struct device_attribute *devattr,
  170. const char *buf, size_t count)
  171. {
  172. unsigned long val;
  173. struct abx500_temp *data = dev_get_drvdata(dev);
  174. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  175. int res = kstrtoul(buf, 10, &val);
  176. if (res < 0)
  177. return res;
  178. val = clamp_val(val, 0, DEFAULT_MAX_TEMP);
  179. mutex_lock(&data->lock);
  180. data->max_hyst[attr->index] = val;
  181. threshold_updated(data);
  182. mutex_unlock(&data->lock);
  183. return count;
  184. }
  185. /* Show functions (RO nodes) */
  186. static ssize_t show_min(struct device *dev,
  187. struct device_attribute *devattr, char *buf)
  188. {
  189. struct abx500_temp *data = dev_get_drvdata(dev);
  190. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  191. return sprintf(buf, "%lu\n", data->min[attr->index]);
  192. }
  193. static ssize_t show_max(struct device *dev,
  194. struct device_attribute *devattr, char *buf)
  195. {
  196. struct abx500_temp *data = dev_get_drvdata(dev);
  197. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  198. return sprintf(buf, "%lu\n", data->max[attr->index]);
  199. }
  200. static ssize_t show_max_hyst(struct device *dev,
  201. struct device_attribute *devattr, char *buf)
  202. {
  203. struct abx500_temp *data = dev_get_drvdata(dev);
  204. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  205. return sprintf(buf, "%lu\n", data->max_hyst[attr->index]);
  206. }
  207. static ssize_t show_min_alarm(struct device *dev,
  208. struct device_attribute *devattr, char *buf)
  209. {
  210. struct abx500_temp *data = dev_get_drvdata(dev);
  211. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  212. return sprintf(buf, "%d\n", data->min_alarm[attr->index]);
  213. }
  214. static ssize_t show_max_alarm(struct device *dev,
  215. struct device_attribute *devattr, char *buf)
  216. {
  217. struct abx500_temp *data = dev_get_drvdata(dev);
  218. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  219. return sprintf(buf, "%d\n", data->max_alarm[attr->index]);
  220. }
  221. static umode_t abx500_attrs_visible(struct kobject *kobj,
  222. struct attribute *attr, int n)
  223. {
  224. struct device *dev = container_of(kobj, struct device, kobj);
  225. struct abx500_temp *data = dev_get_drvdata(dev);
  226. if (data->ops.is_visible)
  227. return data->ops.is_visible(attr, n);
  228. return attr->mode;
  229. }
  230. /* Chip name, required by hwmon */
  231. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
  232. /* GPADC - SENSOR1 */
  233. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_label, NULL, 0);
  234. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
  235. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_min, set_min, 0);
  236. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_max, set_max, 0);
  237. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  238. show_max_hyst, set_max_hyst, 0);
  239. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_min_alarm, NULL, 0);
  240. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_max_alarm, NULL, 0);
  241. /* GPADC - SENSOR2 */
  242. static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL, 1);
  243. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 1);
  244. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min, set_min, 1);
  245. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max, 1);
  246. static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IWUSR | S_IRUGO,
  247. show_max_hyst, set_max_hyst, 1);
  248. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_min_alarm, NULL, 1);
  249. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_max_alarm, NULL, 1);
  250. /* GPADC - SENSOR3 */
  251. static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, show_label, NULL, 2);
  252. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_input, NULL, 2);
  253. static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min, set_min, 2);
  254. static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max, 2);
  255. static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IWUSR | S_IRUGO,
  256. show_max_hyst, set_max_hyst, 2);
  257. static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_min_alarm, NULL, 2);
  258. static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_max_alarm, NULL, 2);
  259. /* GPADC - SENSOR4 */
  260. static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, show_label, NULL, 3);
  261. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_input, NULL, 3);
  262. static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_min, set_min, 3);
  263. static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_max, set_max, 3);
  264. static SENSOR_DEVICE_ATTR(temp4_max_hyst, S_IWUSR | S_IRUGO,
  265. show_max_hyst, set_max_hyst, 3);
  266. static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_min_alarm, NULL, 3);
  267. static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_max_alarm, NULL, 3);
  268. static struct attribute *abx500_temp_attributes[] = {
  269. &sensor_dev_attr_name.dev_attr.attr,
  270. &sensor_dev_attr_temp1_label.dev_attr.attr,
  271. &sensor_dev_attr_temp1_input.dev_attr.attr,
  272. &sensor_dev_attr_temp1_min.dev_attr.attr,
  273. &sensor_dev_attr_temp1_max.dev_attr.attr,
  274. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  275. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  276. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  277. &sensor_dev_attr_temp2_label.dev_attr.attr,
  278. &sensor_dev_attr_temp2_input.dev_attr.attr,
  279. &sensor_dev_attr_temp2_min.dev_attr.attr,
  280. &sensor_dev_attr_temp2_max.dev_attr.attr,
  281. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  282. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  283. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  284. &sensor_dev_attr_temp3_label.dev_attr.attr,
  285. &sensor_dev_attr_temp3_input.dev_attr.attr,
  286. &sensor_dev_attr_temp3_min.dev_attr.attr,
  287. &sensor_dev_attr_temp3_max.dev_attr.attr,
  288. &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
  289. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  290. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  291. &sensor_dev_attr_temp4_label.dev_attr.attr,
  292. &sensor_dev_attr_temp4_input.dev_attr.attr,
  293. &sensor_dev_attr_temp4_min.dev_attr.attr,
  294. &sensor_dev_attr_temp4_max.dev_attr.attr,
  295. &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
  296. &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
  297. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  298. NULL
  299. };
  300. static const struct attribute_group abx500_temp_group = {
  301. .attrs = abx500_temp_attributes,
  302. .is_visible = abx500_attrs_visible,
  303. };
  304. static irqreturn_t abx500_temp_irq_handler(int irq, void *irq_data)
  305. {
  306. struct platform_device *pdev = irq_data;
  307. struct abx500_temp *data = platform_get_drvdata(pdev);
  308. data->ops.irq_handler(irq, data);
  309. return IRQ_HANDLED;
  310. }
  311. static int setup_irqs(struct platform_device *pdev)
  312. {
  313. int ret;
  314. int irq = platform_get_irq_byname(pdev, "ABX500_TEMP_WARM");
  315. if (irq < 0) {
  316. dev_err(&pdev->dev, "Get irq by name failed\n");
  317. return irq;
  318. }
  319. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  320. abx500_temp_irq_handler, IRQF_NO_SUSPEND, "abx500-temp", pdev);
  321. if (ret < 0)
  322. dev_err(&pdev->dev, "Request threaded irq failed (%d)\n", ret);
  323. return ret;
  324. }
  325. static int abx500_temp_probe(struct platform_device *pdev)
  326. {
  327. struct abx500_temp *data;
  328. int err;
  329. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  330. if (!data)
  331. return -ENOMEM;
  332. data->pdev = pdev;
  333. mutex_init(&data->lock);
  334. /* Chip specific initialization */
  335. err = abx500_hwmon_init(data);
  336. if (err < 0 || !data->ops.read_sensor || !data->ops.show_name ||
  337. !data->ops.show_label)
  338. return err;
  339. INIT_DEFERRABLE_WORK(&data->work, gpadc_monitor);
  340. platform_set_drvdata(pdev, data);
  341. err = sysfs_create_group(&pdev->dev.kobj, &abx500_temp_group);
  342. if (err < 0) {
  343. dev_err(&pdev->dev, "Create sysfs group failed (%d)\n", err);
  344. return err;
  345. }
  346. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  347. if (IS_ERR(data->hwmon_dev)) {
  348. err = PTR_ERR(data->hwmon_dev);
  349. dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
  350. goto exit_sysfs_group;
  351. }
  352. if (data->ops.irq_handler) {
  353. err = setup_irqs(pdev);
  354. if (err < 0)
  355. goto exit_hwmon_reg;
  356. }
  357. return 0;
  358. exit_hwmon_reg:
  359. hwmon_device_unregister(data->hwmon_dev);
  360. exit_sysfs_group:
  361. sysfs_remove_group(&pdev->dev.kobj, &abx500_temp_group);
  362. return err;
  363. }
  364. static int abx500_temp_remove(struct platform_device *pdev)
  365. {
  366. struct abx500_temp *data = platform_get_drvdata(pdev);
  367. cancel_delayed_work_sync(&data->work);
  368. hwmon_device_unregister(data->hwmon_dev);
  369. sysfs_remove_group(&pdev->dev.kobj, &abx500_temp_group);
  370. return 0;
  371. }
  372. static int abx500_temp_suspend(struct platform_device *pdev,
  373. pm_message_t state)
  374. {
  375. struct abx500_temp *data = platform_get_drvdata(pdev);
  376. if (data->work_active)
  377. cancel_delayed_work_sync(&data->work);
  378. return 0;
  379. }
  380. static int abx500_temp_resume(struct platform_device *pdev)
  381. {
  382. struct abx500_temp *data = platform_get_drvdata(pdev);
  383. if (data->work_active)
  384. schedule_monitor(data);
  385. return 0;
  386. }
  387. #ifdef CONFIG_OF
  388. static const struct of_device_id abx500_temp_match[] = {
  389. { .compatible = "stericsson,abx500-temp" },
  390. {},
  391. };
  392. #endif
  393. static struct platform_driver abx500_temp_driver = {
  394. .driver = {
  395. .name = "abx500-temp",
  396. .of_match_table = of_match_ptr(abx500_temp_match),
  397. },
  398. .suspend = abx500_temp_suspend,
  399. .resume = abx500_temp_resume,
  400. .probe = abx500_temp_probe,
  401. .remove = abx500_temp_remove,
  402. };
  403. module_platform_driver(abx500_temp_driver);
  404. MODULE_AUTHOR("Martin Persson <martin.persson@stericsson.com>");
  405. MODULE_DESCRIPTION("ABX500 temperature driver");
  406. MODULE_LICENSE("GPL");