da9062-thermal.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Thermal device driver for DA9062 and DA9061
  3. * Copyright (C) 2017 Dialog Semiconductor
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. /* When over-temperature is reached, an interrupt from the device will be
  16. * triggered. Following this event the interrupt will be disabled and
  17. * periodic transmission of uevents (HOT trip point) should define the
  18. * first level of temperature supervision. It is expected that any final
  19. * implementation of the thermal driver will include a .notify() function
  20. * to implement these uevents to userspace.
  21. *
  22. * These uevents are intended to indicate non-invasive temperature control
  23. * of the system, where the necessary measures for cooling are the
  24. * responsibility of the host software. Once the temperature falls again,
  25. * the IRQ is re-enabled so the start of a new over-temperature event can
  26. * be detected without constant software monitoring.
  27. */
  28. #include <linux/errno.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/module.h>
  31. #include <linux/of.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/regmap.h>
  34. #include <linux/thermal.h>
  35. #include <linux/workqueue.h>
  36. #include <linux/mfd/da9062/core.h>
  37. #include <linux/mfd/da9062/registers.h>
  38. /* Minimum, maximum and default polling millisecond periods are provided
  39. * here as an example. It is expected that any final implementation to also
  40. * include a modification of these settings to match the required
  41. * application.
  42. */
  43. #define DA9062_DEFAULT_POLLING_MS_PERIOD 3000
  44. #define DA9062_MAX_POLLING_MS_PERIOD 10000
  45. #define DA9062_MIN_POLLING_MS_PERIOD 1000
  46. #define DA9062_MILLI_CELSIUS(t) ((t) * 1000)
  47. struct da9062_thermal_config {
  48. const char *name;
  49. };
  50. struct da9062_thermal {
  51. struct da9062 *hw;
  52. struct delayed_work work;
  53. struct thermal_zone_device *zone;
  54. enum thermal_device_mode mode;
  55. struct mutex lock; /* protection for da9062_thermal temperature */
  56. int temperature;
  57. int irq;
  58. const struct da9062_thermal_config *config;
  59. struct device *dev;
  60. };
  61. static void da9062_thermal_poll_on(struct work_struct *work)
  62. {
  63. struct da9062_thermal *thermal = container_of(work,
  64. struct da9062_thermal,
  65. work.work);
  66. unsigned long delay;
  67. unsigned int val;
  68. int ret;
  69. /* clear E_TEMP */
  70. ret = regmap_write(thermal->hw->regmap,
  71. DA9062AA_EVENT_B,
  72. DA9062AA_E_TEMP_MASK);
  73. if (ret < 0) {
  74. dev_err(thermal->dev,
  75. "Cannot clear the TJUNC temperature status\n");
  76. goto err_enable_irq;
  77. }
  78. /* Now read E_TEMP again: it is acting like a status bit.
  79. * If over-temperature, then this status will be true.
  80. * If not over-temperature, this status will be false.
  81. */
  82. ret = regmap_read(thermal->hw->regmap,
  83. DA9062AA_EVENT_B,
  84. &val);
  85. if (ret < 0) {
  86. dev_err(thermal->dev,
  87. "Cannot check the TJUNC temperature status\n");
  88. goto err_enable_irq;
  89. }
  90. if (val & DA9062AA_E_TEMP_MASK) {
  91. mutex_lock(&thermal->lock);
  92. thermal->temperature = DA9062_MILLI_CELSIUS(125);
  93. mutex_unlock(&thermal->lock);
  94. thermal_zone_device_update(thermal->zone,
  95. THERMAL_EVENT_UNSPECIFIED);
  96. delay = msecs_to_jiffies(thermal->zone->passive_delay);
  97. queue_delayed_work(system_freezable_wq, &thermal->work, delay);
  98. return;
  99. }
  100. mutex_lock(&thermal->lock);
  101. thermal->temperature = DA9062_MILLI_CELSIUS(0);
  102. mutex_unlock(&thermal->lock);
  103. thermal_zone_device_update(thermal->zone,
  104. THERMAL_EVENT_UNSPECIFIED);
  105. err_enable_irq:
  106. enable_irq(thermal->irq);
  107. }
  108. static irqreturn_t da9062_thermal_irq_handler(int irq, void *data)
  109. {
  110. struct da9062_thermal *thermal = data;
  111. disable_irq_nosync(thermal->irq);
  112. queue_delayed_work(system_freezable_wq, &thermal->work, 0);
  113. return IRQ_HANDLED;
  114. }
  115. static int da9062_thermal_get_mode(struct thermal_zone_device *z,
  116. enum thermal_device_mode *mode)
  117. {
  118. struct da9062_thermal *thermal = z->devdata;
  119. *mode = thermal->mode;
  120. return 0;
  121. }
  122. static int da9062_thermal_get_trip_type(struct thermal_zone_device *z,
  123. int trip,
  124. enum thermal_trip_type *type)
  125. {
  126. struct da9062_thermal *thermal = z->devdata;
  127. switch (trip) {
  128. case 0:
  129. *type = THERMAL_TRIP_HOT;
  130. break;
  131. default:
  132. dev_err(thermal->dev,
  133. "Driver does not support more than 1 trip-wire\n");
  134. return -EINVAL;
  135. }
  136. return 0;
  137. }
  138. static int da9062_thermal_get_trip_temp(struct thermal_zone_device *z,
  139. int trip,
  140. int *temp)
  141. {
  142. struct da9062_thermal *thermal = z->devdata;
  143. switch (trip) {
  144. case 0:
  145. *temp = DA9062_MILLI_CELSIUS(125);
  146. break;
  147. default:
  148. dev_err(thermal->dev,
  149. "Driver does not support more than 1 trip-wire\n");
  150. return -EINVAL;
  151. }
  152. return 0;
  153. }
  154. static int da9062_thermal_get_temp(struct thermal_zone_device *z,
  155. int *temp)
  156. {
  157. struct da9062_thermal *thermal = z->devdata;
  158. mutex_lock(&thermal->lock);
  159. *temp = thermal->temperature;
  160. mutex_unlock(&thermal->lock);
  161. return 0;
  162. }
  163. static struct thermal_zone_device_ops da9062_thermal_ops = {
  164. .get_temp = da9062_thermal_get_temp,
  165. .get_mode = da9062_thermal_get_mode,
  166. .get_trip_type = da9062_thermal_get_trip_type,
  167. .get_trip_temp = da9062_thermal_get_trip_temp,
  168. };
  169. static const struct da9062_thermal_config da9062_config = {
  170. .name = "da9062-thermal",
  171. };
  172. static const struct of_device_id da9062_compatible_reg_id_table[] = {
  173. { .compatible = "dlg,da9062-thermal", .data = &da9062_config },
  174. { },
  175. };
  176. MODULE_DEVICE_TABLE(of, da9062_compatible_reg_id_table);
  177. static int da9062_thermal_probe(struct platform_device *pdev)
  178. {
  179. struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
  180. struct da9062_thermal *thermal;
  181. unsigned int pp_tmp = DA9062_DEFAULT_POLLING_MS_PERIOD;
  182. const struct of_device_id *match;
  183. int ret = 0;
  184. match = of_match_node(da9062_compatible_reg_id_table,
  185. pdev->dev.of_node);
  186. if (!match)
  187. return -ENXIO;
  188. if (pdev->dev.of_node) {
  189. if (!of_property_read_u32(pdev->dev.of_node,
  190. "polling-delay-passive",
  191. &pp_tmp)) {
  192. if (pp_tmp < DA9062_MIN_POLLING_MS_PERIOD ||
  193. pp_tmp > DA9062_MAX_POLLING_MS_PERIOD) {
  194. dev_warn(&pdev->dev,
  195. "Out-of-range polling period %d ms\n",
  196. pp_tmp);
  197. pp_tmp = DA9062_DEFAULT_POLLING_MS_PERIOD;
  198. }
  199. }
  200. }
  201. thermal = devm_kzalloc(&pdev->dev, sizeof(struct da9062_thermal),
  202. GFP_KERNEL);
  203. if (!thermal) {
  204. ret = -ENOMEM;
  205. goto err;
  206. }
  207. thermal->config = match->data;
  208. thermal->hw = chip;
  209. thermal->mode = THERMAL_DEVICE_ENABLED;
  210. thermal->dev = &pdev->dev;
  211. INIT_DELAYED_WORK(&thermal->work, da9062_thermal_poll_on);
  212. mutex_init(&thermal->lock);
  213. thermal->zone = thermal_zone_device_register(thermal->config->name,
  214. 1, 0, thermal,
  215. &da9062_thermal_ops, NULL, pp_tmp,
  216. 0);
  217. if (IS_ERR(thermal->zone)) {
  218. dev_err(&pdev->dev, "Cannot register thermal zone device\n");
  219. ret = PTR_ERR(thermal->zone);
  220. goto err;
  221. }
  222. dev_dbg(&pdev->dev,
  223. "TJUNC temperature polling period set at %d ms\n",
  224. thermal->zone->passive_delay);
  225. ret = platform_get_irq_byname(pdev, "THERMAL");
  226. if (ret < 0) {
  227. dev_err(&pdev->dev, "Failed to get platform IRQ.\n");
  228. goto err_zone;
  229. }
  230. thermal->irq = ret;
  231. ret = request_threaded_irq(thermal->irq, NULL,
  232. da9062_thermal_irq_handler,
  233. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  234. "THERMAL", thermal);
  235. if (ret) {
  236. dev_err(&pdev->dev,
  237. "Failed to request thermal device IRQ.\n");
  238. goto err_zone;
  239. }
  240. platform_set_drvdata(pdev, thermal);
  241. return 0;
  242. err_zone:
  243. thermal_zone_device_unregister(thermal->zone);
  244. err:
  245. return ret;
  246. }
  247. static int da9062_thermal_remove(struct platform_device *pdev)
  248. {
  249. struct da9062_thermal *thermal = platform_get_drvdata(pdev);
  250. free_irq(thermal->irq, thermal);
  251. cancel_delayed_work_sync(&thermal->work);
  252. thermal_zone_device_unregister(thermal->zone);
  253. return 0;
  254. }
  255. static struct platform_driver da9062_thermal_driver = {
  256. .probe = da9062_thermal_probe,
  257. .remove = da9062_thermal_remove,
  258. .driver = {
  259. .name = "da9062-thermal",
  260. .of_match_table = da9062_compatible_reg_id_table,
  261. },
  262. };
  263. module_platform_driver(da9062_thermal_driver);
  264. MODULE_AUTHOR("Steve Twiss");
  265. MODULE_DESCRIPTION("Thermal TJUNC device driver for Dialog DA9062 and DA9061");
  266. MODULE_LICENSE("GPL");
  267. MODULE_ALIAS("platform:da9062-thermal");