qcom-spmi-temp-alarm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/iio/consumer.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/thermal.h>
  23. #define QPNP_TM_REG_TYPE 0x04
  24. #define QPNP_TM_REG_SUBTYPE 0x05
  25. #define QPNP_TM_REG_STATUS 0x08
  26. #define QPNP_TM_REG_SHUTDOWN_CTRL1 0x40
  27. #define QPNP_TM_REG_ALARM_CTRL 0x46
  28. #define QPNP_TM_TYPE 0x09
  29. #define QPNP_TM_SUBTYPE 0x08
  30. #define STATUS_STAGE_MASK 0x03
  31. #define SHUTDOWN_CTRL1_THRESHOLD_MASK 0x03
  32. #define ALARM_CTRL_FORCE_ENABLE 0x80
  33. /*
  34. * Trip point values based on threshold control
  35. * 0 = {105 C, 125 C, 145 C}
  36. * 1 = {110 C, 130 C, 150 C}
  37. * 2 = {115 C, 135 C, 155 C}
  38. * 3 = {120 C, 140 C, 160 C}
  39. */
  40. #define TEMP_STAGE_STEP 20000 /* Stage step: 20.000 C */
  41. #define TEMP_STAGE_HYSTERESIS 2000
  42. #define TEMP_THRESH_MIN 105000 /* Threshold Min: 105 C */
  43. #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
  44. #define THRESH_MIN 0
  45. /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
  46. #define DEFAULT_TEMP 37000
  47. struct qpnp_tm_chip {
  48. struct regmap *map;
  49. struct thermal_zone_device *tz_dev;
  50. long temp;
  51. unsigned int thresh;
  52. unsigned int stage;
  53. unsigned int prev_stage;
  54. unsigned int base;
  55. struct iio_channel *adc;
  56. };
  57. static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
  58. {
  59. unsigned int val;
  60. int ret;
  61. ret = regmap_read(chip->map, chip->base + addr, &val);
  62. if (ret < 0)
  63. return ret;
  64. *data = val;
  65. return 0;
  66. }
  67. static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
  68. {
  69. return regmap_write(chip->map, chip->base + addr, data);
  70. }
  71. /*
  72. * This function updates the internal temp value based on the
  73. * current thermal stage and threshold as well as the previous stage
  74. */
  75. static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
  76. {
  77. unsigned int stage;
  78. int ret;
  79. u8 reg = 0;
  80. ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg);
  81. if (ret < 0)
  82. return ret;
  83. stage = reg & STATUS_STAGE_MASK;
  84. if (stage > chip->stage) {
  85. /* increasing stage, use lower bound */
  86. chip->temp = (stage - 1) * TEMP_STAGE_STEP +
  87. chip->thresh * TEMP_THRESH_STEP +
  88. TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
  89. } else if (stage < chip->stage) {
  90. /* decreasing stage, use upper bound */
  91. chip->temp = stage * TEMP_STAGE_STEP +
  92. chip->thresh * TEMP_THRESH_STEP -
  93. TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
  94. }
  95. chip->stage = stage;
  96. return 0;
  97. }
  98. static int qpnp_tm_get_temp(void *data, int *temp)
  99. {
  100. struct qpnp_tm_chip *chip = data;
  101. int ret, mili_celsius;
  102. if (!temp)
  103. return -EINVAL;
  104. if (IS_ERR(chip->adc)) {
  105. ret = qpnp_tm_update_temp_no_adc(chip);
  106. if (ret < 0)
  107. return ret;
  108. } else {
  109. ret = iio_read_channel_processed(chip->adc, &mili_celsius);
  110. if (ret < 0)
  111. return ret;
  112. chip->temp = mili_celsius;
  113. }
  114. *temp = chip->temp < 0 ? 0 : chip->temp;
  115. return 0;
  116. }
  117. static const struct thermal_zone_of_device_ops qpnp_tm_sensor_ops = {
  118. .get_temp = qpnp_tm_get_temp,
  119. };
  120. static irqreturn_t qpnp_tm_isr(int irq, void *data)
  121. {
  122. struct qpnp_tm_chip *chip = data;
  123. thermal_zone_device_update(chip->tz_dev);
  124. return IRQ_HANDLED;
  125. }
  126. /*
  127. * This function initializes the internal temp value based on only the
  128. * current thermal stage and threshold. Setup threshold control and
  129. * disable shutdown override.
  130. */
  131. static int qpnp_tm_init(struct qpnp_tm_chip *chip)
  132. {
  133. int ret;
  134. u8 reg;
  135. chip->thresh = THRESH_MIN;
  136. chip->temp = DEFAULT_TEMP;
  137. ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg);
  138. if (ret < 0)
  139. return ret;
  140. chip->stage = reg & STATUS_STAGE_MASK;
  141. if (chip->stage)
  142. chip->temp = chip->thresh * TEMP_THRESH_STEP +
  143. (chip->stage - 1) * TEMP_STAGE_STEP +
  144. TEMP_THRESH_MIN;
  145. /*
  146. * Set threshold and disable software override of stage 2 and 3
  147. * shutdowns.
  148. */
  149. reg = chip->thresh & SHUTDOWN_CTRL1_THRESHOLD_MASK;
  150. ret = qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
  151. if (ret < 0)
  152. return ret;
  153. /* Enable the thermal alarm PMIC module in always-on mode. */
  154. reg = ALARM_CTRL_FORCE_ENABLE;
  155. ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg);
  156. return ret;
  157. }
  158. static int qpnp_tm_probe(struct platform_device *pdev)
  159. {
  160. struct qpnp_tm_chip *chip;
  161. struct device_node *node;
  162. u8 type, subtype;
  163. u32 res[2];
  164. int ret, irq;
  165. node = pdev->dev.of_node;
  166. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  167. if (!chip)
  168. return -ENOMEM;
  169. dev_set_drvdata(&pdev->dev, chip);
  170. chip->map = dev_get_regmap(pdev->dev.parent, NULL);
  171. if (!chip->map)
  172. return -ENXIO;
  173. ret = of_property_read_u32_array(node, "reg", res, 2);
  174. if (ret < 0)
  175. return ret;
  176. irq = platform_get_irq(pdev, 0);
  177. if (irq < 0)
  178. return irq;
  179. /* ADC based measurements are optional */
  180. chip->adc = iio_channel_get(&pdev->dev, "thermal");
  181. if (PTR_ERR(chip->adc) == -EPROBE_DEFER)
  182. return PTR_ERR(chip->adc);
  183. chip->base = res[0];
  184. ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type);
  185. if (ret < 0) {
  186. dev_err(&pdev->dev, "could not read type\n");
  187. goto fail;
  188. }
  189. ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
  190. if (ret < 0) {
  191. dev_err(&pdev->dev, "could not read subtype\n");
  192. goto fail;
  193. }
  194. if (type != QPNP_TM_TYPE || subtype != QPNP_TM_SUBTYPE) {
  195. dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n",
  196. type, subtype);
  197. ret = -ENODEV;
  198. goto fail;
  199. }
  200. ret = qpnp_tm_init(chip);
  201. if (ret < 0) {
  202. dev_err(&pdev->dev, "init failed\n");
  203. goto fail;
  204. }
  205. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr,
  206. IRQF_ONESHOT, node->name, chip);
  207. if (ret < 0)
  208. goto fail;
  209. chip->tz_dev = thermal_zone_of_sensor_register(&pdev->dev, 0, chip,
  210. &qpnp_tm_sensor_ops);
  211. if (IS_ERR(chip->tz_dev)) {
  212. dev_err(&pdev->dev, "failed to register sensor\n");
  213. ret = PTR_ERR(chip->tz_dev);
  214. goto fail;
  215. }
  216. return 0;
  217. fail:
  218. if (!IS_ERR(chip->adc))
  219. iio_channel_release(chip->adc);
  220. return ret;
  221. }
  222. static int qpnp_tm_remove(struct platform_device *pdev)
  223. {
  224. struct qpnp_tm_chip *chip = dev_get_drvdata(&pdev->dev);
  225. thermal_zone_of_sensor_unregister(&pdev->dev, chip->tz_dev);
  226. if (!IS_ERR(chip->adc))
  227. iio_channel_release(chip->adc);
  228. return 0;
  229. }
  230. static const struct of_device_id qpnp_tm_match_table[] = {
  231. { .compatible = "qcom,spmi-temp-alarm" },
  232. { }
  233. };
  234. MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
  235. static struct platform_driver qpnp_tm_driver = {
  236. .driver = {
  237. .name = "spmi-temp-alarm",
  238. .of_match_table = qpnp_tm_match_table,
  239. },
  240. .probe = qpnp_tm_probe,
  241. .remove = qpnp_tm_remove,
  242. };
  243. module_platform_driver(qpnp_tm_driver);
  244. MODULE_ALIAS("platform:spmi-temp-alarm");
  245. MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
  246. MODULE_LICENSE("GPL v2");