da9062-thermal.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. static unsigned int pp_tmp = DA9062_DEFAULT_POLLING_MS_PERIOD;
  39. struct da9062_thermal_config {
  40. const char *name;
  41. };
  42. struct da9062_thermal {
  43. struct da9062 *hw;
  44. struct delayed_work work;
  45. struct thermal_zone_device *zone;
  46. struct mutex lock; /* protection for da9062_thermal temperature */
  47. int temperature;
  48. int irq;
  49. const struct da9062_thermal_config *config;
  50. struct device *dev;
  51. };
  52. static void da9062_thermal_poll_on(struct work_struct *work)
  53. {
  54. struct da9062_thermal *thermal = container_of(work,
  55. struct da9062_thermal,
  56. work.work);
  57. unsigned long delay;
  58. unsigned int val;
  59. int ret;
  60. /* clear E_TEMP */
  61. ret = regmap_write(thermal->hw->regmap,
  62. DA9062AA_EVENT_B,
  63. DA9062AA_E_TEMP_MASK);
  64. if (ret < 0) {
  65. dev_err(thermal->dev,
  66. "Cannot clear the TJUNC temperature status\n");
  67. goto err_enable_irq;
  68. }
  69. /* Now read E_TEMP again: it is acting like a status bit.
  70. * If over-temperature, then this status will be true.
  71. * If not over-temperature, this status will be false.
  72. */
  73. ret = regmap_read(thermal->hw->regmap,
  74. DA9062AA_EVENT_B,
  75. &val);
  76. if (ret < 0) {
  77. dev_err(thermal->dev,
  78. "Cannot check the TJUNC temperature status\n");
  79. goto err_enable_irq;
  80. }
  81. if (val & DA9062AA_E_TEMP_MASK) {
  82. mutex_lock(&thermal->lock);
  83. thermal->temperature = DA9062_MILLI_CELSIUS(125);
  84. mutex_unlock(&thermal->lock);
  85. thermal_zone_device_update(thermal->zone,
  86. THERMAL_EVENT_UNSPECIFIED);
  87. /*
  88. * pp_tmp is between 1s and 10s, so we can round the jiffies
  89. */
  90. delay = round_jiffies(msecs_to_jiffies(pp_tmp));
  91. queue_delayed_work(system_freezable_wq, &thermal->work, delay);
  92. return;
  93. }
  94. mutex_lock(&thermal->lock);
  95. thermal->temperature = DA9062_MILLI_CELSIUS(0);
  96. mutex_unlock(&thermal->lock);
  97. thermal_zone_device_update(thermal->zone,
  98. THERMAL_EVENT_UNSPECIFIED);
  99. err_enable_irq:
  100. enable_irq(thermal->irq);
  101. }
  102. static irqreturn_t da9062_thermal_irq_handler(int irq, void *data)
  103. {
  104. struct da9062_thermal *thermal = data;
  105. disable_irq_nosync(thermal->irq);
  106. queue_delayed_work(system_freezable_wq, &thermal->work, 0);
  107. return IRQ_HANDLED;
  108. }
  109. static int da9062_thermal_get_temp(struct thermal_zone_device *z,
  110. int *temp)
  111. {
  112. struct da9062_thermal *thermal = thermal_zone_device_priv(z);
  113. mutex_lock(&thermal->lock);
  114. *temp = thermal->temperature;
  115. mutex_unlock(&thermal->lock);
  116. return 0;
  117. }
  118. static const struct thermal_zone_device_ops da9062_thermal_ops = {
  119. .get_temp = da9062_thermal_get_temp,
  120. };
  121. static struct thermal_trip trips[] = {
  122. { .temperature = DA9062_MILLI_CELSIUS(125), .type = THERMAL_TRIP_HOT },
  123. };
  124. static const struct da9062_thermal_config da9062_config = {
  125. .name = "da9062-thermal",
  126. };
  127. static const struct of_device_id da9062_compatible_reg_id_table[] = {
  128. { .compatible = "dlg,da9062-thermal", .data = &da9062_config },
  129. { },
  130. };
  131. MODULE_DEVICE_TABLE(of, da9062_compatible_reg_id_table);
  132. static int da9062_thermal_probe(struct platform_device *pdev)
  133. {
  134. struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
  135. struct da9062_thermal *thermal;
  136. const struct of_device_id *match;
  137. int ret = 0;
  138. match = of_match_node(da9062_compatible_reg_id_table,
  139. pdev->dev.of_node);
  140. if (!match)
  141. return -ENXIO;
  142. if (pdev->dev.of_node) {
  143. if (!of_property_read_u32(pdev->dev.of_node,
  144. "polling-delay-passive",
  145. &pp_tmp)) {
  146. if (pp_tmp < DA9062_MIN_POLLING_MS_PERIOD ||
  147. pp_tmp > DA9062_MAX_POLLING_MS_PERIOD) {
  148. dev_warn(&pdev->dev,
  149. "Out-of-range polling period %d ms\n",
  150. pp_tmp);
  151. pp_tmp = DA9062_DEFAULT_POLLING_MS_PERIOD;
  152. }
  153. }
  154. }
  155. thermal = devm_kzalloc(&pdev->dev, sizeof(struct da9062_thermal),
  156. GFP_KERNEL);
  157. if (!thermal) {
  158. ret = -ENOMEM;
  159. goto err;
  160. }
  161. thermal->config = match->data;
  162. thermal->hw = chip;
  163. thermal->dev = &pdev->dev;
  164. INIT_DELAYED_WORK(&thermal->work, da9062_thermal_poll_on);
  165. mutex_init(&thermal->lock);
  166. thermal->zone = thermal_zone_device_register_with_trips(thermal->config->name,
  167. trips, ARRAY_SIZE(trips), thermal,
  168. &da9062_thermal_ops, NULL, pp_tmp,
  169. 0);
  170. if (IS_ERR(thermal->zone)) {
  171. dev_err(&pdev->dev, "Cannot register thermal zone device\n");
  172. ret = PTR_ERR(thermal->zone);
  173. goto err;
  174. }
  175. ret = thermal_zone_device_enable(thermal->zone);
  176. if (ret) {
  177. dev_err(&pdev->dev, "Cannot enable thermal zone device\n");
  178. goto err_zone;
  179. }
  180. dev_dbg(&pdev->dev,
  181. "TJUNC temperature polling period set at %d ms\n", pp_tmp);
  182. ret = platform_get_irq_byname(pdev, "THERMAL");
  183. if (ret < 0)
  184. goto err_zone;
  185. thermal->irq = ret;
  186. ret = request_threaded_irq(thermal->irq, NULL,
  187. da9062_thermal_irq_handler,
  188. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  189. "THERMAL", thermal);
  190. if (ret) {
  191. dev_err(&pdev->dev,
  192. "Failed to request thermal device IRQ.\n");
  193. goto err_zone;
  194. }
  195. platform_set_drvdata(pdev, thermal);
  196. return 0;
  197. err_zone:
  198. thermal_zone_device_unregister(thermal->zone);
  199. err:
  200. return ret;
  201. }
  202. static void da9062_thermal_remove(struct platform_device *pdev)
  203. {
  204. struct da9062_thermal *thermal = platform_get_drvdata(pdev);
  205. free_irq(thermal->irq, thermal);
  206. cancel_delayed_work_sync(&thermal->work);
  207. thermal_zone_device_unregister(thermal->zone);
  208. }
  209. static struct platform_driver da9062_thermal_driver = {
  210. .probe = da9062_thermal_probe,
  211. .remove = da9062_thermal_remove,
  212. .driver = {
  213. .name = "da9062-thermal",
  214. .of_match_table = da9062_compatible_reg_id_table,
  215. },
  216. };
  217. module_platform_driver(da9062_thermal_driver);
  218. MODULE_AUTHOR("Steve Twiss");
  219. MODULE_DESCRIPTION("Thermal TJUNC device driver for Dialog DA9062 and DA9061");
  220. MODULE_LICENSE("GPL");
  221. MODULE_ALIAS("platform:da9062-thermal");