qcom-spmi-temp-alarm.c 23 KB

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