da9062-thermal.c 7.7 KB

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