qcom-spmi-temp-alarm.c 12 KB

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