qcom-spmi-temp-alarm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright (c) 2011-2015, 2017, 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/bitops.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/iio/consumer.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/thermal.h>
  24. #include "thermal_core.h"
  25. #define QPNP_TM_REG_TYPE 0x04
  26. #define QPNP_TM_REG_SUBTYPE 0x05
  27. #define QPNP_TM_REG_STATUS 0x08
  28. #define QPNP_TM_REG_SHUTDOWN_CTRL1 0x40
  29. #define QPNP_TM_REG_ALARM_CTRL 0x46
  30. #define QPNP_TM_TYPE 0x09
  31. #define QPNP_TM_SUBTYPE_GEN1 0x08
  32. #define QPNP_TM_SUBTYPE_GEN2 0x09
  33. #define STATUS_GEN1_STAGE_MASK GENMASK(1, 0)
  34. #define STATUS_GEN2_STATE_MASK GENMASK(6, 4)
  35. #define STATUS_GEN2_STATE_SHIFT 4
  36. #define SHUTDOWN_CTRL1_OVERRIDE_S2 BIT(6)
  37. #define SHUTDOWN_CTRL1_THRESHOLD_MASK GENMASK(1, 0)
  38. #define SHUTDOWN_CTRL1_RATE_25HZ BIT(3)
  39. #define ALARM_CTRL_FORCE_ENABLE BIT(7)
  40. /*
  41. * Trip point values based on threshold control
  42. * 0 = {105 C, 125 C, 145 C}
  43. * 1 = {110 C, 130 C, 150 C}
  44. * 2 = {115 C, 135 C, 155 C}
  45. * 3 = {120 C, 140 C, 160 C}
  46. */
  47. #define TEMP_STAGE_STEP 20000 /* Stage step: 20.000 C */
  48. #define TEMP_STAGE_HYSTERESIS 2000
  49. #define TEMP_THRESH_MIN 105000 /* Threshold Min: 105 C */
  50. #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
  51. #define THRESH_MIN 0
  52. #define THRESH_MAX 3
  53. /* Stage 2 Threshold Min: 125 C */
  54. #define STAGE2_THRESHOLD_MIN 125000
  55. /* Stage 2 Threshold Max: 140 C */
  56. #define STAGE2_THRESHOLD_MAX 140000
  57. /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
  58. #define DEFAULT_TEMP 37000
  59. struct qpnp_tm_chip {
  60. struct regmap *map;
  61. struct device *dev;
  62. struct thermal_zone_device *tz_dev;
  63. unsigned int subtype;
  64. long temp;
  65. unsigned int thresh;
  66. unsigned int stage;
  67. unsigned int prev_stage;
  68. unsigned int base;
  69. /* protects .thresh, .stage and chip registers */
  70. struct mutex lock;
  71. bool initialized;
  72. struct iio_channel *adc;
  73. };
  74. /* This array maps from GEN2 alarm state to GEN1 alarm stage */
  75. static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3};
  76. static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
  77. {
  78. unsigned int val;
  79. int ret;
  80. ret = regmap_read(chip->map, chip->base + addr, &val);
  81. if (ret < 0)
  82. return ret;
  83. *data = val;
  84. return 0;
  85. }
  86. static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
  87. {
  88. return regmap_write(chip->map, chip->base + addr, data);
  89. }
  90. /**
  91. * qpnp_tm_get_temp_stage() - return over-temperature stage
  92. * @chip: Pointer to the qpnp_tm chip
  93. *
  94. * Return: stage (GEN1) or state (GEN2) on success, or errno on failure.
  95. */
  96. static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip)
  97. {
  98. int ret;
  99. u8 reg = 0;
  100. ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg);
  101. if (ret < 0)
  102. return ret;
  103. if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
  104. ret = reg & STATUS_GEN1_STAGE_MASK;
  105. else
  106. ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT;
  107. return ret;
  108. }
  109. /*
  110. * This function updates the internal temp value based on the
  111. * current thermal stage and threshold as well as the previous stage
  112. */
  113. static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
  114. {
  115. unsigned int stage, stage_new, stage_old;
  116. int ret;
  117. WARN_ON(!mutex_is_locked(&chip->lock));
  118. ret = qpnp_tm_get_temp_stage(chip);
  119. if (ret < 0)
  120. return ret;
  121. stage = ret;
  122. if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
  123. stage_new = stage;
  124. stage_old = chip->stage;
  125. } else {
  126. stage_new = alarm_state_map[stage];
  127. stage_old = alarm_state_map[chip->stage];
  128. }
  129. if (stage_new > stage_old) {
  130. /* increasing stage, use lower bound */
  131. chip->temp = (stage_new - 1) * TEMP_STAGE_STEP +
  132. chip->thresh * TEMP_THRESH_STEP +
  133. TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
  134. } else if (stage_new < stage_old) {
  135. /* decreasing stage, use upper bound */
  136. chip->temp = stage_new * TEMP_STAGE_STEP +
  137. chip->thresh * TEMP_THRESH_STEP -
  138. TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
  139. }
  140. chip->stage = stage;
  141. return 0;
  142. }
  143. static int qpnp_tm_get_temp(void *data, int *temp)
  144. {
  145. struct qpnp_tm_chip *chip = data;
  146. int ret, mili_celsius;
  147. if (!temp)
  148. return -EINVAL;
  149. if (!chip->initialized) {
  150. *temp = DEFAULT_TEMP;
  151. return 0;
  152. }
  153. if (!chip->adc) {
  154. mutex_lock(&chip->lock);
  155. ret = qpnp_tm_update_temp_no_adc(chip);
  156. mutex_unlock(&chip->lock);
  157. if (ret < 0)
  158. return ret;
  159. } else {
  160. ret = iio_read_channel_processed(chip->adc, &mili_celsius);
  161. if (ret < 0)
  162. return ret;
  163. chip->temp = mili_celsius;
  164. }
  165. *temp = chip->temp < 0 ? 0 : chip->temp;
  166. return 0;
  167. }
  168. static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip,
  169. int temp)
  170. {
  171. u8 reg;
  172. bool disable_s2_shutdown = false;
  173. WARN_ON(!mutex_is_locked(&chip->lock));
  174. /*
  175. * Default: S2 and S3 shutdown enabled, thresholds at
  176. * 105C/125C/145C, monitoring at 25Hz
  177. */
  178. reg = SHUTDOWN_CTRL1_RATE_25HZ;
  179. if (temp == THERMAL_TEMP_INVALID ||
  180. temp < STAGE2_THRESHOLD_MIN) {
  181. chip->thresh = THRESH_MIN;
  182. goto skip;
  183. }
  184. if (temp <= STAGE2_THRESHOLD_MAX) {
  185. chip->thresh = THRESH_MAX -
  186. ((STAGE2_THRESHOLD_MAX - temp) /
  187. TEMP_THRESH_STEP);
  188. disable_s2_shutdown = true;
  189. } else {
  190. chip->thresh = THRESH_MAX;
  191. if (chip->adc)
  192. disable_s2_shutdown = true;
  193. else
  194. dev_warn(chip->dev,
  195. "No ADC is configured and critical temperature is above the maximum stage 2 threshold of 140 C! Configuring stage 2 shutdown at 140 C.\n");
  196. }
  197. skip:
  198. reg |= chip->thresh;
  199. if (disable_s2_shutdown)
  200. reg |= SHUTDOWN_CTRL1_OVERRIDE_S2;
  201. return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
  202. }
  203. static int qpnp_tm_set_trip_temp(void *data, int trip, int temp)
  204. {
  205. struct qpnp_tm_chip *chip = data;
  206. const struct thermal_trip *trip_points;
  207. int ret;
  208. trip_points = of_thermal_get_trip_points(chip->tz_dev);
  209. if (!trip_points)
  210. return -EINVAL;
  211. if (trip_points[trip].type != THERMAL_TRIP_CRITICAL)
  212. return 0;
  213. mutex_lock(&chip->lock);
  214. ret = qpnp_tm_update_critical_trip_temp(chip, temp);
  215. mutex_unlock(&chip->lock);
  216. return ret;
  217. }
  218. static const struct thermal_zone_of_device_ops qpnp_tm_sensor_ops = {
  219. .get_temp = qpnp_tm_get_temp,
  220. .set_trip_temp = qpnp_tm_set_trip_temp,
  221. };
  222. static irqreturn_t qpnp_tm_isr(int irq, void *data)
  223. {
  224. struct qpnp_tm_chip *chip = data;
  225. thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
  226. return IRQ_HANDLED;
  227. }
  228. static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip)
  229. {
  230. int ntrips;
  231. const struct thermal_trip *trips;
  232. int i;
  233. ntrips = of_thermal_get_ntrips(chip->tz_dev);
  234. if (ntrips <= 0)
  235. return THERMAL_TEMP_INVALID;
  236. trips = of_thermal_get_trip_points(chip->tz_dev);
  237. if (!trips)
  238. return THERMAL_TEMP_INVALID;
  239. for (i = 0; i < ntrips; i++) {
  240. if (of_thermal_is_trip_valid(chip->tz_dev, i) &&
  241. trips[i].type == THERMAL_TRIP_CRITICAL)
  242. return trips[i].temperature;
  243. }
  244. return THERMAL_TEMP_INVALID;
  245. }
  246. /*
  247. * This function initializes the internal temp value based on only the
  248. * current thermal stage and threshold. Setup threshold control and
  249. * disable shutdown override.
  250. */
  251. static int qpnp_tm_init(struct qpnp_tm_chip *chip)
  252. {
  253. unsigned int stage;
  254. int ret;
  255. u8 reg = 0;
  256. int crit_temp;
  257. mutex_lock(&chip->lock);
  258. ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg);
  259. if (ret < 0)
  260. goto out;
  261. chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
  262. chip->temp = DEFAULT_TEMP;
  263. ret = qpnp_tm_get_temp_stage(chip);
  264. if (ret < 0)
  265. goto out;
  266. chip->stage = ret;
  267. stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1
  268. ? chip->stage : alarm_state_map[chip->stage];
  269. if (stage)
  270. chip->temp = chip->thresh * TEMP_THRESH_STEP +
  271. (stage - 1) * TEMP_STAGE_STEP +
  272. TEMP_THRESH_MIN;
  273. crit_temp = qpnp_tm_get_critical_trip_temp(chip);
  274. ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp);
  275. if (ret < 0)
  276. goto out;
  277. /* Enable the thermal alarm PMIC module in always-on mode. */
  278. reg = ALARM_CTRL_FORCE_ENABLE;
  279. ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg);
  280. chip->initialized = true;
  281. out:
  282. mutex_unlock(&chip->lock);
  283. return ret;
  284. }
  285. static int qpnp_tm_probe(struct platform_device *pdev)
  286. {
  287. struct qpnp_tm_chip *chip;
  288. struct device_node *node;
  289. u8 type, subtype;
  290. u32 res;
  291. int ret, irq;
  292. node = pdev->dev.of_node;
  293. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  294. if (!chip)
  295. return -ENOMEM;
  296. dev_set_drvdata(&pdev->dev, chip);
  297. chip->dev = &pdev->dev;
  298. mutex_init(&chip->lock);
  299. chip->map = dev_get_regmap(pdev->dev.parent, NULL);
  300. if (!chip->map)
  301. return -ENXIO;
  302. ret = of_property_read_u32(node, "reg", &res);
  303. if (ret < 0)
  304. return ret;
  305. irq = platform_get_irq(pdev, 0);
  306. if (irq < 0)
  307. return irq;
  308. /* ADC based measurements are optional */
  309. chip->adc = devm_iio_channel_get(&pdev->dev, "thermal");
  310. if (IS_ERR(chip->adc)) {
  311. ret = PTR_ERR(chip->adc);
  312. chip->adc = NULL;
  313. if (ret == -EPROBE_DEFER)
  314. return ret;
  315. }
  316. chip->base = res;
  317. ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type);
  318. if (ret < 0) {
  319. dev_err(&pdev->dev, "could not read type\n");
  320. return ret;
  321. }
  322. ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
  323. if (ret < 0) {
  324. dev_err(&pdev->dev, "could not read subtype\n");
  325. return ret;
  326. }
  327. if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1
  328. && subtype != QPNP_TM_SUBTYPE_GEN2)) {
  329. dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n",
  330. type, subtype);
  331. return -ENODEV;
  332. }
  333. chip->subtype = subtype;
  334. /*
  335. * Register the sensor before initializing the hardware to be able to
  336. * read the trip points. get_temp() returns the default temperature
  337. * before the hardware initialization is completed.
  338. */
  339. chip->tz_dev = devm_thermal_zone_of_sensor_register(
  340. &pdev->dev, 0, chip, &qpnp_tm_sensor_ops);
  341. if (IS_ERR(chip->tz_dev)) {
  342. dev_err(&pdev->dev, "failed to register sensor\n");
  343. return PTR_ERR(chip->tz_dev);
  344. }
  345. ret = qpnp_tm_init(chip);
  346. if (ret < 0) {
  347. dev_err(&pdev->dev, "init failed\n");
  348. return ret;
  349. }
  350. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr,
  351. IRQF_ONESHOT, node->name, chip);
  352. if (ret < 0)
  353. return ret;
  354. thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
  355. return 0;
  356. }
  357. static const struct of_device_id qpnp_tm_match_table[] = {
  358. { .compatible = "qcom,spmi-temp-alarm" },
  359. { }
  360. };
  361. MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
  362. static struct platform_driver qpnp_tm_driver = {
  363. .driver = {
  364. .name = "spmi-temp-alarm",
  365. .of_match_table = qpnp_tm_match_table,
  366. },
  367. .probe = qpnp_tm_probe,
  368. };
  369. module_platform_driver(qpnp_tm_driver);
  370. MODULE_ALIAS("platform:spmi-temp-alarm");
  371. MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
  372. MODULE_LICENSE("GPL v2");