qcom-spmi-temp-alarm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2015, 2017, 2020-2021, The Linux Foundation.
  4. * All rights reserved.
  5. * Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved.
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/iio/consumer.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/thermal.h>
  19. #include "../thermal_core.h"
  20. #include "../thermal_hwmon.h"
  21. #define QPNP_TM_REG_DIG_MINOR 0x00
  22. #define QPNP_TM_REG_DIG_MAJOR 0x01
  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_IRQ_STATUS 0x10
  27. #define QPNP_TM_REG_SHUTDOWN_CTRL1 0x40
  28. #define QPNP_TM_REG_ALARM_CTRL 0x46
  29. /* TEMP_DAC_* registers are only present for TEMP_GEN2 v2.0 */
  30. #define QPNP_TM_REG_TEMP_DAC_STG1 0x47
  31. #define QPNP_TM_REG_TEMP_DAC_STG2 0x48
  32. #define QPNP_TM_REG_TEMP_DAC_STG3 0x49
  33. #define QPNP_TM_REG_LITE_TEMP_CFG1 0x50
  34. #define QPNP_TM_REG_LITE_TEMP_CFG2 0x51
  35. #define QPNP_TM_TYPE 0x09
  36. #define QPNP_TM_SUBTYPE_GEN1 0x08
  37. #define QPNP_TM_SUBTYPE_GEN2 0x09
  38. #define QPNP_TM_SUBTYPE_LITE 0xC0
  39. #define STATUS_GEN1_STAGE_MASK GENMASK(1, 0)
  40. #define STATUS_GEN2_STATE_MASK GENMASK(6, 4)
  41. #define STATUS_GEN2_STATE_SHIFT 4
  42. /* IRQ status only needed for TEMP_ALARM_LITE */
  43. #define IRQ_STATUS_MASK BIT(0)
  44. #define SHUTDOWN_CTRL1_OVERRIDE_S2 BIT(6)
  45. #define SHUTDOWN_CTRL1_THRESHOLD_MASK GENMASK(1, 0)
  46. #define SHUTDOWN_CTRL1_RATE_25HZ BIT(3)
  47. #define ALARM_CTRL_FORCE_ENABLE BIT(7)
  48. #define LITE_TEMP_CFG_THRESHOLD_MASK GENMASK(3, 2)
  49. #define THRESH_COUNT 4
  50. #define STAGE_COUNT 3
  51. /* Over-temperature trip point values in mC */
  52. static const long temp_map_gen1[THRESH_COUNT][STAGE_COUNT] = {
  53. { 105000, 125000, 145000 },
  54. { 110000, 130000, 150000 },
  55. { 115000, 135000, 155000 },
  56. { 120000, 140000, 160000 },
  57. };
  58. static const long temp_map_gen2_v1[THRESH_COUNT][STAGE_COUNT] = {
  59. { 90000, 110000, 140000 },
  60. { 95000, 115000, 145000 },
  61. { 100000, 120000, 150000 },
  62. { 105000, 125000, 155000 },
  63. };
  64. #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
  65. #define THRESH_MIN 0
  66. #define THRESH_MAX 3
  67. #define TEMP_STAGE_HYSTERESIS 2000
  68. /*
  69. * For TEMP_GEN2 v2.0, TEMP_DAC_STG1/2/3 registers are used to set the threshold
  70. * for each stage independently.
  71. * TEMP_DAC_STG* = 0 --> 80 C
  72. * Each 8 step increase in TEMP_DAC_STG* value corresponds to 5 C (5000 mC).
  73. */
  74. #define TEMP_DAC_MIN 80000
  75. #define TEMP_DAC_SCALE_NUM 8
  76. #define TEMP_DAC_SCALE_DEN 5000
  77. #define TEMP_DAC_TEMP_TO_REG(temp) \
  78. (((temp) - TEMP_DAC_MIN) * TEMP_DAC_SCALE_NUM / TEMP_DAC_SCALE_DEN)
  79. #define TEMP_DAC_REG_TO_TEMP(reg) \
  80. (TEMP_DAC_MIN + (reg) * TEMP_DAC_SCALE_DEN / TEMP_DAC_SCALE_NUM)
  81. static const long temp_dac_max[STAGE_COUNT] = {
  82. 119375, 159375, 159375
  83. };
  84. /*
  85. * TEMP_ALARM_LITE has two stages: warning and shutdown with independently
  86. * configured threshold temperatures.
  87. */
  88. static const long temp_map_lite_warning[THRESH_COUNT] = {
  89. 115000, 125000, 135000, 145000
  90. };
  91. static const long temp_map_lite_shutdown[THRESH_COUNT] = {
  92. 135000, 145000, 160000, 175000
  93. };
  94. /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
  95. #define DEFAULT_TEMP 37000
  96. struct qpnp_tm_chip {
  97. struct regmap *map;
  98. struct device *dev;
  99. struct thermal_zone_device *tz_dev;
  100. unsigned int subtype;
  101. unsigned int dig_revision;
  102. long temp;
  103. unsigned int thresh;
  104. unsigned int stage;
  105. unsigned int prev_stage;
  106. unsigned int base;
  107. int irq;
  108. /* protects .thresh, .stage and chip registers */
  109. struct mutex lock;
  110. bool initialized;
  111. struct iio_channel *adc;
  112. const long (*temp_map)[THRESH_COUNT][STAGE_COUNT];
  113. bool has_temp_dac;
  114. long temp_dac_map[STAGE_COUNT];
  115. };
  116. /* This array maps from GEN2 alarm state to GEN1 alarm stage */
  117. static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3};
  118. static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
  119. {
  120. unsigned int val;
  121. int ret;
  122. ret = regmap_read(chip->map, chip->base + addr, &val);
  123. if (ret < 0)
  124. return ret;
  125. *data = val;
  126. return 0;
  127. }
  128. static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
  129. {
  130. return regmap_write(chip->map, chip->base + addr, data);
  131. }
  132. /**
  133. * qpnp_tm_decode_temp() - return temperature in mC corresponding to the
  134. * specified over-temperature stage
  135. * @chip: Pointer to the qpnp_tm chip
  136. * @stage: Over-temperature stage
  137. *
  138. * Return: temperature in mC
  139. */
  140. static long qpnp_tm_decode_temp(struct qpnp_tm_chip *chip, unsigned int stage)
  141. {
  142. if (chip->has_temp_dac || chip->subtype == QPNP_TM_SUBTYPE_LITE) {
  143. if (stage == 0 || stage > STAGE_COUNT)
  144. return 0;
  145. return chip->temp_dac_map[stage - 1];
  146. }
  147. if (!chip->temp_map || chip->thresh >= THRESH_COUNT || stage == 0 ||
  148. stage > STAGE_COUNT)
  149. return 0;
  150. return (*chip->temp_map)[chip->thresh][stage - 1];
  151. }
  152. /**
  153. * qpnp_tm_get_temp_stage() - return over-temperature stage
  154. * @chip: Pointer to the qpnp_tm chip
  155. *
  156. * Return: stage (GEN1), state (GEN2), or alarm interrupt state (LITE) on
  157. * success; or errno on failure.
  158. */
  159. static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip)
  160. {
  161. int ret;
  162. u16 addr = QPNP_TM_REG_STATUS;
  163. u8 reg = 0;
  164. if (chip->subtype == QPNP_TM_SUBTYPE_LITE)
  165. addr = QPNP_TM_REG_IRQ_STATUS;
  166. ret = qpnp_tm_read(chip, addr, &reg);
  167. if (ret < 0)
  168. return ret;
  169. if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
  170. ret = reg & STATUS_GEN1_STAGE_MASK;
  171. else if (chip->subtype == QPNP_TM_SUBTYPE_LITE)
  172. ret = reg & IRQ_STATUS_MASK;
  173. else
  174. ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT;
  175. return ret;
  176. }
  177. /*
  178. * This function updates the internal temp value based on the
  179. * current thermal stage and threshold as well as the previous stage
  180. */
  181. static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
  182. {
  183. unsigned int stage, stage_new, stage_old;
  184. int ret;
  185. WARN_ON(!mutex_is_locked(&chip->lock));
  186. ret = qpnp_tm_get_temp_stage(chip);
  187. if (ret < 0)
  188. return ret;
  189. stage = ret;
  190. if (chip->subtype == QPNP_TM_SUBTYPE_GEN1
  191. || chip->subtype == QPNP_TM_SUBTYPE_LITE) {
  192. stage_new = stage;
  193. stage_old = chip->stage;
  194. } else {
  195. stage_new = alarm_state_map[stage];
  196. stage_old = alarm_state_map[chip->stage];
  197. }
  198. if (stage_new > stage_old) {
  199. /* increasing stage, use lower bound */
  200. chip->temp = qpnp_tm_decode_temp(chip, stage_new)
  201. + TEMP_STAGE_HYSTERESIS;
  202. } else if (stage_new < stage_old) {
  203. /* decreasing stage, use upper bound */
  204. chip->temp = qpnp_tm_decode_temp(chip, stage_new + 1)
  205. - TEMP_STAGE_HYSTERESIS;
  206. }
  207. chip->stage = stage;
  208. return 0;
  209. }
  210. static int qpnp_tm_get_temp(struct thermal_zone_device *tz, int *temp)
  211. {
  212. struct qpnp_tm_chip *chip = tz->devdata;
  213. int ret, mili_celsius;
  214. if (!temp)
  215. return -EINVAL;
  216. if (!chip->initialized) {
  217. *temp = DEFAULT_TEMP;
  218. return 0;
  219. }
  220. if (!chip->adc) {
  221. mutex_lock(&chip->lock);
  222. ret = qpnp_tm_update_temp_no_adc(chip);
  223. mutex_unlock(&chip->lock);
  224. if (ret < 0)
  225. return ret;
  226. } else {
  227. ret = iio_read_channel_processed(chip->adc, &mili_celsius);
  228. if (ret < 0)
  229. return ret;
  230. chip->temp = mili_celsius;
  231. }
  232. *temp = chip->temp;
  233. return 0;
  234. }
  235. static int qpnp_tm_set_temp_dac_thresh(struct qpnp_tm_chip *chip, int trip,
  236. int temp)
  237. {
  238. int ret, temp_cfg;
  239. u8 reg;
  240. if (trip < 0 || trip >= STAGE_COUNT) {
  241. dev_err(chip->dev, "invalid TEMP_DAC trip = %d\n", trip);
  242. return -EINVAL;
  243. } else if (temp < TEMP_DAC_MIN || temp > temp_dac_max[trip]) {
  244. dev_err(chip->dev, "invalid TEMP_DAC temp = %d\n", temp);
  245. return -EINVAL;
  246. }
  247. reg = TEMP_DAC_TEMP_TO_REG(temp);
  248. temp_cfg = TEMP_DAC_REG_TO_TEMP(reg);
  249. ret = qpnp_tm_write(chip, QPNP_TM_REG_TEMP_DAC_STG1 + trip, reg);
  250. if (ret < 0) {
  251. dev_err(chip->dev, "TEMP_DAC_STG write failed, ret=%d\n", ret);
  252. return ret;
  253. }
  254. chip->temp_dac_map[trip] = temp_cfg;
  255. return 0;
  256. }
  257. static int qpnp_tm_set_temp_lite_thresh(struct qpnp_tm_chip *chip, int trip,
  258. int temp)
  259. {
  260. int ret, temp_cfg, i;
  261. const long *temp_map;
  262. u16 addr;
  263. u8 reg, thresh;
  264. if (trip < 0 || trip >= STAGE_COUNT) {
  265. dev_err(chip->dev, "invalid TEMP_LITE trip = %d\n", trip);
  266. return -EINVAL;
  267. }
  268. switch (trip) {
  269. case 0:
  270. temp_map = temp_map_lite_warning;
  271. addr = QPNP_TM_REG_LITE_TEMP_CFG1;
  272. break;
  273. case 1:
  274. /*
  275. * The second trip point is purely in software to facilitate
  276. * a controlled shutdown after the warning threshold is crossed
  277. * but before the automatic hardware shutdown threshold is
  278. * crossed.
  279. */
  280. return 0;
  281. case 2:
  282. temp_map = temp_map_lite_shutdown;
  283. addr = QPNP_TM_REG_LITE_TEMP_CFG2;
  284. break;
  285. default:
  286. return 0;
  287. }
  288. if (temp < temp_map[THRESH_MIN] || temp > temp_map[THRESH_MAX]) {
  289. dev_err(chip->dev, "invalid TEMP_LITE temp = %d\n", temp);
  290. return -EINVAL;
  291. }
  292. thresh = 0;
  293. temp_cfg = temp_map[thresh];
  294. for (i = THRESH_MAX; i >= THRESH_MIN; i--) {
  295. if (temp >= temp_map[i]) {
  296. thresh = i;
  297. temp_cfg = temp_map[i];
  298. break;
  299. }
  300. }
  301. if (temp_cfg == chip->temp_dac_map[trip])
  302. return 0;
  303. ret = qpnp_tm_read(chip, addr, &reg);
  304. if (ret < 0) {
  305. dev_err(chip->dev, "LITE_TEMP_CFG read failed, ret=%d\n", ret);
  306. return ret;
  307. }
  308. reg &= ~LITE_TEMP_CFG_THRESHOLD_MASK;
  309. reg |= FIELD_PREP(LITE_TEMP_CFG_THRESHOLD_MASK, thresh);
  310. ret = qpnp_tm_write(chip, addr, reg);
  311. if (ret < 0) {
  312. dev_err(chip->dev, "LITE_TEMP_CFG write failed, ret=%d\n", ret);
  313. return ret;
  314. }
  315. chip->temp_dac_map[trip] = temp_cfg;
  316. return 0;
  317. }
  318. static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip,
  319. int temp)
  320. {
  321. long stage2_threshold_min = (*chip->temp_map)[THRESH_MIN][1];
  322. long stage2_threshold_max = (*chip->temp_map)[THRESH_MAX][1];
  323. bool disable_s2_shutdown = false;
  324. bool require_s2_shutdown = false;
  325. u8 reg;
  326. WARN_ON(!mutex_is_locked(&chip->lock));
  327. /*
  328. * Default: S2 and S3 shutdown enabled, thresholds at
  329. * lowest threshold set, monitoring at 25Hz
  330. */
  331. reg = SHUTDOWN_CTRL1_RATE_25HZ;
  332. if (temp == THERMAL_TEMP_INVALID ||
  333. temp < stage2_threshold_min) {
  334. chip->thresh = THRESH_MIN;
  335. goto skip;
  336. }
  337. if (temp <= stage2_threshold_max) {
  338. chip->thresh = THRESH_MAX -
  339. ((stage2_threshold_max - temp) /
  340. TEMP_THRESH_STEP);
  341. disable_s2_shutdown = true;
  342. } else {
  343. chip->thresh = THRESH_MAX;
  344. if (chip->adc)
  345. disable_s2_shutdown = true;
  346. else
  347. dev_warn(chip->dev,
  348. "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",
  349. temp, stage2_threshold_max, stage2_threshold_max);
  350. }
  351. if (chip->subtype == QPNP_TM_SUBTYPE_GEN2) {
  352. /*
  353. * Check if stage 2 automatic partial shutdown must remain
  354. * enabled to avoid potential repeated faults upon reaching
  355. * over-temperature stage 3.
  356. */
  357. switch (chip->dig_revision) {
  358. case 0x0001:
  359. case 0x0002:
  360. case 0x0100:
  361. case 0x0101:
  362. require_s2_shutdown = true;
  363. break;
  364. }
  365. }
  366. skip:
  367. reg |= chip->thresh;
  368. if (disable_s2_shutdown && !require_s2_shutdown)
  369. reg |= SHUTDOWN_CTRL1_OVERRIDE_S2;
  370. return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
  371. }
  372. static int qpnp_tm_set_trip_temp(struct thermal_zone_device *tz, int trip, int temp)
  373. {
  374. struct qpnp_tm_chip *chip = tz->devdata;
  375. const struct thermal_trip *trip_points;
  376. int ret;
  377. trip_points = of_thermal_get_trip_points(chip->tz_dev);
  378. if (!trip_points)
  379. return -EINVAL;
  380. if (trip_points[trip].type != THERMAL_TRIP_CRITICAL)
  381. return 0;
  382. mutex_lock(&chip->lock);
  383. ret = qpnp_tm_update_critical_trip_temp(chip, temp);
  384. mutex_unlock(&chip->lock);
  385. return ret;
  386. }
  387. static const struct thermal_zone_device_ops qpnp_tm_sensor_ops = {
  388. .get_temp = qpnp_tm_get_temp,
  389. .set_trip_temp = qpnp_tm_set_trip_temp,
  390. };
  391. static int qpnp_tm_set_temp_dac_trip_temp(struct thermal_zone_device *tz, int trip, int temp)
  392. {
  393. struct qpnp_tm_chip *chip = tz->devdata;
  394. int ret;
  395. mutex_lock(&chip->lock);
  396. ret = qpnp_tm_set_temp_dac_thresh(chip, trip, temp);
  397. mutex_unlock(&chip->lock);
  398. return ret;
  399. }
  400. static const struct thermal_zone_device_ops qpnp_tm_sensor_temp_dac_ops = {
  401. .get_temp = qpnp_tm_get_temp,
  402. .set_trip_temp = qpnp_tm_set_temp_dac_trip_temp,
  403. };
  404. static int qpnp_tm_set_temp_lite_trip_temp(struct thermal_zone_device *tz, int trip, int temp)
  405. {
  406. struct qpnp_tm_chip *chip = tz->devdata;
  407. int ret;
  408. mutex_lock(&chip->lock);
  409. ret = qpnp_tm_set_temp_lite_thresh(chip, trip, temp);
  410. mutex_unlock(&chip->lock);
  411. return ret;
  412. }
  413. static const struct thermal_zone_device_ops qpnp_tm_sensor_temp_lite_ops = {
  414. .get_temp = qpnp_tm_get_temp,
  415. .set_trip_temp = qpnp_tm_set_temp_lite_trip_temp,
  416. };
  417. static irqreturn_t qpnp_tm_isr(int irq, void *data)
  418. {
  419. struct qpnp_tm_chip *chip = data;
  420. thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
  421. return IRQ_HANDLED;
  422. }
  423. static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip)
  424. {
  425. int ntrips;
  426. const struct thermal_trip *trips;
  427. int i;
  428. ntrips = of_thermal_get_ntrips(chip->tz_dev);
  429. if (ntrips <= 0)
  430. return THERMAL_TEMP_INVALID;
  431. trips = of_thermal_get_trip_points(chip->tz_dev);
  432. if (!trips)
  433. return THERMAL_TEMP_INVALID;
  434. for (i = 0; i < ntrips; i++) {
  435. if (of_thermal_is_trip_valid(chip->tz_dev, i) &&
  436. trips[i].type == THERMAL_TRIP_CRITICAL)
  437. return trips[i].temperature;
  438. }
  439. return THERMAL_TEMP_INVALID;
  440. }
  441. /* Configure TEMP_DAC registers based on DT thermal_zone trips */
  442. static int qpnp_tm_temp_dac_update_trip_temps(struct qpnp_tm_chip *chip)
  443. {
  444. const struct thermal_trip *trips;
  445. int ret, ntrips, i;
  446. ntrips = of_thermal_get_ntrips(chip->tz_dev);
  447. /* Keep hardware defaults if no DT trips are defined. */
  448. if (ntrips <= 0)
  449. return 0;
  450. trips = of_thermal_get_trip_points(chip->tz_dev);
  451. if (!trips)
  452. return -EINVAL;
  453. for (i = 0; i < ntrips; i++) {
  454. if (of_thermal_is_trip_valid(chip->tz_dev, i)) {
  455. ret = qpnp_tm_set_temp_dac_thresh(chip, i,
  456. trips[i].temperature);
  457. if (ret < 0)
  458. return ret;
  459. }
  460. }
  461. /* Verify that trips are strictly increasing. */
  462. for (i = 1; i < STAGE_COUNT; i++) {
  463. if (chip->temp_dac_map[i] <= chip->temp_dac_map[i - 1]) {
  464. dev_err(chip->dev, "Threshold %d=%ld <= threshold %d=%ld\n",
  465. i, chip->temp_dac_map[i], i - 1,
  466. chip->temp_dac_map[i - 1]);
  467. return -EINVAL;
  468. }
  469. }
  470. return 0;
  471. }
  472. /* Read the hardware default TEMP_DAC stage threshold temperatures */
  473. static int qpnp_tm_temp_dac_init(struct qpnp_tm_chip *chip)
  474. {
  475. int ret, i;
  476. u8 reg = 0;
  477. for (i = 0; i < STAGE_COUNT; i++) {
  478. ret = qpnp_tm_read(chip, QPNP_TM_REG_TEMP_DAC_STG1 + i, &reg);
  479. if (ret < 0)
  480. return ret;
  481. chip->temp_dac_map[i] = TEMP_DAC_REG_TO_TEMP(reg);
  482. }
  483. return 0;
  484. }
  485. /* Configure TEMP_LITE registers based on DT thermal_zone trips */
  486. static int qpnp_tm_temp_lite_update_trip_temps(struct qpnp_tm_chip *chip)
  487. {
  488. const struct thermal_trip *trips;
  489. int ret, ntrips, i;
  490. ntrips = of_thermal_get_ntrips(chip->tz_dev);
  491. /* Keep hardware defaults if no DT trips are defined. */
  492. if (ntrips <= 0)
  493. return 0;
  494. trips = of_thermal_get_trip_points(chip->tz_dev);
  495. if (!trips)
  496. return -EINVAL;
  497. for (i = 0; i < ntrips; i++) {
  498. if (of_thermal_is_trip_valid(chip->tz_dev, i)) {
  499. ret = qpnp_tm_set_temp_lite_thresh(chip, i,
  500. trips[i].temperature);
  501. if (ret < 0)
  502. return ret;
  503. }
  504. }
  505. /* Verify that trips are strictly increasing. */
  506. if (chip->temp_dac_map[2] <= chip->temp_dac_map[0]) {
  507. dev_err(chip->dev, "Threshold 2=%ld <= threshold 0=%ld\n",
  508. chip->temp_dac_map[2], chip->temp_dac_map[0]);
  509. return -EINVAL;
  510. }
  511. return 0;
  512. }
  513. /* Read the hardware default TEMP_LITE stage threshold temperatures */
  514. static int qpnp_tm_temp_lite_init(struct qpnp_tm_chip *chip)
  515. {
  516. int ret, thresh;
  517. u8 reg = 0;
  518. /*
  519. * Store the warning trip temp in temp_dac_map[0] and the shutdown trip
  520. * temp in temp_dac_map[2]. The second trip point is purely in software
  521. * to facilitate a controlled shutdown after the warning threshold is
  522. * crossed but before the automatic hardware shutdown threshold is
  523. * crossed. Thus, there is no register to read for the second trip
  524. * point.
  525. */
  526. ret = qpnp_tm_read(chip, QPNP_TM_REG_LITE_TEMP_CFG1, &reg);
  527. if (ret < 0)
  528. return ret;
  529. thresh = FIELD_GET(LITE_TEMP_CFG_THRESHOLD_MASK, reg);
  530. chip->temp_dac_map[0] = temp_map_lite_warning[thresh];
  531. ret = qpnp_tm_read(chip, QPNP_TM_REG_LITE_TEMP_CFG2, &reg);
  532. if (ret < 0)
  533. return ret;
  534. thresh = FIELD_GET(LITE_TEMP_CFG_THRESHOLD_MASK, reg);
  535. chip->temp_dac_map[2] = temp_map_lite_shutdown[thresh];
  536. return 0;
  537. }
  538. /*
  539. * This function initializes the internal temp value based on only the
  540. * current thermal stage and threshold. Setup threshold control and
  541. * disable shutdown override.
  542. */
  543. static int qpnp_tm_init(struct qpnp_tm_chip *chip)
  544. {
  545. unsigned int stage;
  546. int ret;
  547. u8 reg = 0;
  548. int crit_temp;
  549. mutex_lock(&chip->lock);
  550. ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg);
  551. if (ret < 0)
  552. goto out;
  553. chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
  554. chip->temp = DEFAULT_TEMP;
  555. ret = qpnp_tm_get_temp_stage(chip);
  556. if (ret < 0)
  557. goto out;
  558. chip->stage = ret;
  559. stage = (chip->subtype == QPNP_TM_SUBTYPE_GEN1
  560. || chip->subtype == QPNP_TM_SUBTYPE_LITE)
  561. ? chip->stage : alarm_state_map[chip->stage];
  562. if (stage)
  563. chip->temp = qpnp_tm_decode_temp(chip, stage);
  564. if (chip->subtype == QPNP_TM_SUBTYPE_LITE) {
  565. ret = qpnp_tm_temp_lite_update_trip_temps(chip);
  566. if (ret < 0)
  567. goto out;
  568. } else if (chip->has_temp_dac) {
  569. ret = qpnp_tm_temp_dac_update_trip_temps(chip);
  570. if (ret < 0)
  571. goto out;
  572. } else {
  573. crit_temp = qpnp_tm_get_critical_trip_temp(chip);
  574. ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp);
  575. if (ret < 0)
  576. goto out;
  577. }
  578. /* Enable the thermal alarm PMIC module in always-on mode. */
  579. reg = ALARM_CTRL_FORCE_ENABLE;
  580. ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg);
  581. chip->initialized = true;
  582. out:
  583. mutex_unlock(&chip->lock);
  584. return ret;
  585. }
  586. static int qpnp_tm_probe(struct platform_device *pdev)
  587. {
  588. struct qpnp_tm_chip *chip;
  589. struct device_node *node;
  590. const struct thermal_zone_device_ops *ops;
  591. u8 type, subtype, dig_major, dig_minor;
  592. u32 res;
  593. int ret;
  594. node = pdev->dev.of_node;
  595. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  596. if (!chip)
  597. return -ENOMEM;
  598. dev_set_drvdata(&pdev->dev, chip);
  599. chip->dev = &pdev->dev;
  600. mutex_init(&chip->lock);
  601. chip->map = dev_get_regmap(pdev->dev.parent, NULL);
  602. if (!chip->map)
  603. return -ENXIO;
  604. ret = of_property_read_u32(node, "reg", &res);
  605. if (ret < 0)
  606. return ret;
  607. chip->irq = platform_get_irq(pdev, 0);
  608. if (chip->irq < 0)
  609. return chip->irq;
  610. /* ADC based measurements are optional */
  611. chip->adc = devm_iio_channel_get(&pdev->dev, "thermal");
  612. if (IS_ERR(chip->adc)) {
  613. ret = PTR_ERR(chip->adc);
  614. chip->adc = NULL;
  615. if (ret == -EPROBE_DEFER)
  616. return ret;
  617. }
  618. chip->base = res;
  619. ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type);
  620. if (ret < 0) {
  621. dev_err(&pdev->dev, "could not read type\n");
  622. return ret;
  623. }
  624. ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
  625. if (ret < 0) {
  626. dev_err(&pdev->dev, "could not read subtype\n");
  627. return ret;
  628. }
  629. ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MAJOR, &dig_major);
  630. if (ret < 0) {
  631. dev_err(&pdev->dev, "could not read dig_major\n");
  632. return ret;
  633. }
  634. ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MINOR, &dig_minor);
  635. if (ret < 0) {
  636. dev_err(&pdev->dev, "could not read dig_minor\n");
  637. return ret;
  638. }
  639. chip->dig_revision = (dig_major << 8) | dig_minor;
  640. if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1
  641. && subtype != QPNP_TM_SUBTYPE_GEN2
  642. && subtype != QPNP_TM_SUBTYPE_LITE)) {
  643. dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n",
  644. type, subtype);
  645. return -ENODEV;
  646. }
  647. ops = &qpnp_tm_sensor_ops;
  648. chip->subtype = subtype;
  649. if (subtype == QPNP_TM_SUBTYPE_GEN2 && dig_major >= 2)
  650. chip->has_temp_dac = true;
  651. else if (subtype == QPNP_TM_SUBTYPE_GEN2 && dig_major >= 1)
  652. chip->temp_map = &temp_map_gen2_v1;
  653. else if (subtype == QPNP_TM_SUBTYPE_GEN1)
  654. chip->temp_map = &temp_map_gen1;
  655. if (chip->has_temp_dac) {
  656. ops = &qpnp_tm_sensor_temp_dac_ops;
  657. ret = qpnp_tm_temp_dac_init(chip);
  658. if (ret < 0)
  659. return ret;
  660. }
  661. if (chip->subtype == QPNP_TM_SUBTYPE_LITE) {
  662. ops = &qpnp_tm_sensor_temp_lite_ops;
  663. ret = qpnp_tm_temp_lite_init(chip);
  664. if (ret < 0)
  665. return ret;
  666. }
  667. /*
  668. * Register the sensor before initializing the hardware to be able to
  669. * read the trip points. get_temp() returns the default temperature
  670. * before the hardware initialization is completed.
  671. */
  672. chip->tz_dev = devm_thermal_of_zone_register(
  673. &pdev->dev, 0, chip, ops);
  674. if (IS_ERR(chip->tz_dev)) {
  675. dev_err(&pdev->dev, "failed to register sensor\n");
  676. return PTR_ERR(chip->tz_dev);
  677. }
  678. ret = qpnp_tm_init(chip);
  679. if (ret < 0) {
  680. dev_err(&pdev->dev, "init failed\n");
  681. return ret;
  682. }
  683. if (devm_thermal_add_hwmon_sysfs(chip->tz_dev))
  684. dev_warn(&pdev->dev,
  685. "Failed to add hwmon sysfs attributes\n");
  686. ret = devm_request_threaded_irq(&pdev->dev, chip->irq, NULL,
  687. qpnp_tm_isr, IRQF_ONESHOT,
  688. node->name, chip);
  689. if (ret < 0)
  690. return ret;
  691. thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
  692. return 0;
  693. }
  694. static int qpnp_tm_restore(struct device *dev)
  695. {
  696. int ret = 0;
  697. struct qpnp_tm_chip *chip = dev_get_drvdata(dev);
  698. struct device_node *node = dev->of_node;
  699. unsigned long flags;
  700. if (chip->subtype == QPNP_TM_SUBTYPE_GEN2)
  701. flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  702. else
  703. flags = IRQF_TRIGGER_RISING;
  704. if (chip->irq > 0) {
  705. ret = devm_request_threaded_irq(dev, chip->irq, NULL,
  706. qpnp_tm_isr, flags | IRQF_ONESHOT, node->name, chip);
  707. if (ret < 0)
  708. return ret;
  709. }
  710. ret = qpnp_tm_init(chip);
  711. if (ret < 0)
  712. dev_err(dev, "init failed\n");
  713. return ret;
  714. }
  715. static int qpnp_tm_freeze(struct device *dev)
  716. {
  717. struct qpnp_tm_chip *chip = dev_get_drvdata(dev);
  718. if (chip->irq > 0)
  719. devm_free_irq(dev, chip->irq, chip);
  720. return 0;
  721. }
  722. static const struct dev_pm_ops qpnp_tm_pm_ops = {
  723. .freeze = qpnp_tm_freeze,
  724. .restore = qpnp_tm_restore,
  725. };
  726. static void qpnp_tm_shutdown(struct platform_device *pdev)
  727. {
  728. struct qpnp_tm_chip *chip = platform_get_drvdata(pdev);
  729. if (chip->irq > 0)
  730. devm_free_irq(chip->dev, chip->irq, chip);
  731. }
  732. static const struct of_device_id qpnp_tm_match_table[] = {
  733. { .compatible = "qcom,spmi-temp-alarm" },
  734. { }
  735. };
  736. MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
  737. static struct platform_driver qpnp_tm_driver = {
  738. .driver = {
  739. .name = "spmi-temp-alarm",
  740. .of_match_table = qpnp_tm_match_table,
  741. .pm = &qpnp_tm_pm_ops,
  742. },
  743. .probe = qpnp_tm_probe,
  744. .shutdown = qpnp_tm_shutdown,
  745. };
  746. module_platform_driver(qpnp_tm_driver);
  747. MODULE_ALIAS("platform:spmi-temp-alarm");
  748. MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
  749. MODULE_LICENSE("GPL v2");