qpnp-temp-alarm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * Copyright (c) 2011-2013, 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. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/string.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/spmi.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/thermal.h>
  25. #include <linux/qpnp/qpnp-adc.h>
  26. #define QPNP_TM_DRIVER_NAME "qcom,qpnp-temp-alarm"
  27. enum qpnp_tm_registers {
  28. QPNP_TM_REG_TYPE = 0x04,
  29. QPNP_TM_REG_SUBTYPE = 0x05,
  30. QPNP_TM_REG_STATUS = 0x08,
  31. QPNP_TM_REG_SHUTDOWN_CTRL1 = 0x40,
  32. QPNP_TM_REG_SHUTDOWN_CTRL2 = 0x42,
  33. QPNP_TM_REG_ALARM_CTRL = 0x46,
  34. };
  35. #define QPNP_TM_TYPE 0x09
  36. #define QPNP_TM_SUBTYPE 0x08
  37. #define STATUS_STAGE_MASK 0x03
  38. #define SHUTDOWN_CTRL1_OVERRIDE_STAGE3 0x80
  39. #define SHUTDOWN_CTRL1_OVERRIDE_STAGE2 0x40
  40. #define SHUTDOWN_CTRL1_THRESHOLD_MASK 0x03
  41. #define SHUTDOWN_CTRL2_CLEAR_STAGE3 0x80
  42. #define SHUTDOWN_CTRL2_CLEAR_STAGE2 0x40
  43. #define ALARM_CTRL_FORCE_ENABLE 0x80
  44. #define ALARM_CTRL_FOLLOW_HW_ENABLE 0x01
  45. #define TEMP_STAGE_STEP 20000 /* Stage step: 20.000 C */
  46. #define TEMP_STAGE_HYSTERESIS 2000
  47. #define TEMP_THRESH_MIN 105000 /* Threshold Min: 105 C */
  48. #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
  49. #define THRESH_MIN 0
  50. #define THRESH_MAX 3
  51. /* Trip points from most critical to least critical */
  52. #define TRIP_STAGE3 0
  53. #define TRIP_STAGE2 1
  54. #define TRIP_STAGE1 2
  55. #define TRIP_NUM 3
  56. enum qpnp_tm_adc_type {
  57. QPNP_TM_ADC_NONE, /* Estimates temp based on overload level. */
  58. QPNP_TM_ADC_QPNP_ADC,
  59. };
  60. /*
  61. * Temperature in millicelcius reported during stage 0 if no ADC is present and
  62. * no value has been specified via device tree.
  63. */
  64. #define DEFAULT_NO_ADC_TEMP 37000
  65. struct qpnp_tm_chip {
  66. struct delayed_work irq_work;
  67. struct spmi_device *spmi_dev;
  68. struct thermal_zone_device *tz_dev;
  69. const char *tm_name;
  70. enum qpnp_tm_adc_type adc_type;
  71. unsigned long temperature;
  72. enum thermal_device_mode mode;
  73. unsigned int thresh;
  74. unsigned int stage;
  75. unsigned int prev_stage;
  76. int irq;
  77. enum qpnp_vadc_channels adc_channel;
  78. u16 base_addr;
  79. bool allow_software_override;
  80. struct qpnp_vadc_chip *vadc_dev;
  81. };
  82. /* Delay between TEMP_STAT IRQ going high and status value changing in ms. */
  83. #define STATUS_REGISTER_DELAY_MS 40
  84. enum pmic_thermal_override_mode {
  85. SOFTWARE_OVERRIDE_DISABLED = 0,
  86. SOFTWARE_OVERRIDE_ENABLED,
  87. };
  88. static inline int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *buf,
  89. int len)
  90. {
  91. int rc;
  92. rc = spmi_ext_register_readl(chip->spmi_dev->ctrl,
  93. chip->spmi_dev->sid, chip->base_addr + addr, buf, len);
  94. if (rc)
  95. dev_err(&chip->spmi_dev->dev, "%s: spmi_ext_register_readl() failed. sid=%d, addr=%04X, len=%d, rc=%d\n",
  96. __func__, chip->spmi_dev->sid, chip->base_addr + addr,
  97. len, rc);
  98. return rc;
  99. }
  100. static inline int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 *buf,
  101. int len)
  102. {
  103. int rc;
  104. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl,
  105. chip->spmi_dev->sid, chip->base_addr + addr, buf, len);
  106. if (rc)
  107. dev_err(&chip->spmi_dev->dev, "%s: spmi_ext_register_writel() failed. sid=%d, addr=%04X, len=%d, rc=%d\n",
  108. __func__, chip->spmi_dev->sid, chip->base_addr + addr,
  109. len, rc);
  110. return rc;
  111. }
  112. static inline int qpnp_tm_shutdown_override(struct qpnp_tm_chip *chip,
  113. enum pmic_thermal_override_mode mode)
  114. {
  115. int rc = 0;
  116. u8 reg;
  117. if (chip->allow_software_override) {
  118. reg = chip->thresh & SHUTDOWN_CTRL1_THRESHOLD_MASK;
  119. if (mode == SOFTWARE_OVERRIDE_ENABLED)
  120. reg |= SHUTDOWN_CTRL1_OVERRIDE_STAGE2
  121. | SHUTDOWN_CTRL1_OVERRIDE_STAGE3;
  122. rc = qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg, 1);
  123. }
  124. return rc;
  125. }
  126. static int qpnp_tm_update_temp(struct qpnp_tm_chip *chip)
  127. {
  128. struct qpnp_vadc_result adc_result;
  129. int rc;
  130. rc = qpnp_vadc_read(chip->vadc_dev, chip->adc_channel, &adc_result);
  131. if (!rc)
  132. chip->temperature = adc_result.physical;
  133. else
  134. dev_err(&chip->spmi_dev->dev, "%s: qpnp_vadc_read(%d) failed, rc=%d\n",
  135. __func__, chip->adc_channel, rc);
  136. return rc;
  137. }
  138. /*
  139. * This function initializes the internal temperature value based on only the
  140. * current thermal stage and threshold.
  141. */
  142. static int qpnp_tm_init_temp_no_adc(struct qpnp_tm_chip *chip)
  143. {
  144. int rc;
  145. u8 reg;
  146. rc = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg, 1);
  147. if (rc < 0)
  148. return rc;
  149. chip->stage = reg & STATUS_STAGE_MASK;
  150. if (chip->stage)
  151. chip->temperature = chip->thresh * TEMP_THRESH_STEP +
  152. (chip->stage - 1) * TEMP_STAGE_STEP +
  153. TEMP_THRESH_MIN;
  154. return 0;
  155. }
  156. /*
  157. * This function updates the internal temperature value based on the
  158. * current thermal stage and threshold as well as the previous stage
  159. */
  160. static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
  161. {
  162. unsigned int stage;
  163. int rc;
  164. u8 reg;
  165. rc = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg, 1);
  166. if (rc < 0)
  167. return rc;
  168. stage = reg & STATUS_STAGE_MASK;
  169. if (stage > chip->stage) {
  170. /* increasing stage, use lower bound */
  171. chip->temperature = (stage - 1) * TEMP_STAGE_STEP
  172. + chip->thresh * TEMP_THRESH_STEP
  173. + TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
  174. } else if (stage < chip->stage) {
  175. /* decreasing stage, use upper bound */
  176. chip->temperature = stage * TEMP_STAGE_STEP
  177. + chip->thresh * TEMP_THRESH_STEP
  178. - TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
  179. }
  180. chip->stage = stage;
  181. return 0;
  182. }
  183. static int qpnp_tz_get_temp_no_adc(struct thermal_zone_device *thermal,
  184. unsigned long *temperature)
  185. {
  186. struct qpnp_tm_chip *chip = thermal->devdata;
  187. int rc;
  188. if (!temperature)
  189. return -EINVAL;
  190. rc = qpnp_tm_update_temp_no_adc(chip);
  191. if (rc < 0)
  192. return rc;
  193. *temperature = chip->temperature;
  194. return 0;
  195. }
  196. static int qpnp_tz_get_temp_qpnp_adc(struct thermal_zone_device *thermal,
  197. unsigned long *temperature)
  198. {
  199. struct qpnp_tm_chip *chip = thermal->devdata;
  200. int rc;
  201. if (!temperature)
  202. return -EINVAL;
  203. rc = qpnp_tm_update_temp(chip);
  204. if (rc < 0) {
  205. dev_err(&chip->spmi_dev->dev, "%s: %s: adc read failed, rc = %d\n",
  206. __func__, chip->tm_name, rc);
  207. return rc;
  208. }
  209. *temperature = chip->temperature;
  210. return 0;
  211. }
  212. static int qpnp_tz_get_mode(struct thermal_zone_device *thermal,
  213. enum thermal_device_mode *mode)
  214. {
  215. struct qpnp_tm_chip *chip = thermal->devdata;
  216. if (!mode)
  217. return -EINVAL;
  218. *mode = chip->mode;
  219. return 0;
  220. }
  221. static int qpnp_tz_set_mode(struct thermal_zone_device *thermal,
  222. enum thermal_device_mode mode)
  223. {
  224. struct qpnp_tm_chip *chip = thermal->devdata;
  225. int rc = 0;
  226. if (mode != chip->mode) {
  227. if (mode == THERMAL_DEVICE_ENABLED)
  228. rc = qpnp_tm_shutdown_override(chip,
  229. SOFTWARE_OVERRIDE_ENABLED);
  230. else
  231. rc = qpnp_tm_shutdown_override(chip,
  232. SOFTWARE_OVERRIDE_DISABLED);
  233. chip->mode = mode;
  234. }
  235. return rc;
  236. }
  237. static int qpnp_tz_get_trip_type(struct thermal_zone_device *thermal,
  238. int trip, enum thermal_trip_type *type)
  239. {
  240. if (trip < 0 || !type)
  241. return -EINVAL;
  242. switch (trip) {
  243. case TRIP_STAGE3:
  244. *type = THERMAL_TRIP_CRITICAL;
  245. break;
  246. case TRIP_STAGE2:
  247. *type = THERMAL_TRIP_HOT;
  248. break;
  249. case TRIP_STAGE1:
  250. *type = THERMAL_TRIP_HOT;
  251. break;
  252. default:
  253. return -EINVAL;
  254. }
  255. return 0;
  256. }
  257. static int qpnp_tz_get_trip_temp(struct thermal_zone_device *thermal,
  258. int trip, unsigned long *temperature)
  259. {
  260. struct qpnp_tm_chip *chip = thermal->devdata;
  261. int thresh_temperature;
  262. if (trip < 0 || !temperature)
  263. return -EINVAL;
  264. thresh_temperature = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN;
  265. switch (trip) {
  266. case TRIP_STAGE3:
  267. thresh_temperature += 2 * TEMP_STAGE_STEP;
  268. break;
  269. case TRIP_STAGE2:
  270. thresh_temperature += TEMP_STAGE_STEP;
  271. break;
  272. case TRIP_STAGE1:
  273. break;
  274. default:
  275. return -EINVAL;
  276. }
  277. *temperature = thresh_temperature;
  278. return 0;
  279. }
  280. static int qpnp_tz_get_crit_temp(struct thermal_zone_device *thermal,
  281. unsigned long *temperature)
  282. {
  283. struct qpnp_tm_chip *chip = thermal->devdata;
  284. if (!temperature)
  285. return -EINVAL;
  286. *temperature = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN +
  287. 2 * TEMP_STAGE_STEP;
  288. return 0;
  289. }
  290. static struct thermal_zone_device_ops qpnp_thermal_zone_ops_no_adc = {
  291. .get_temp = qpnp_tz_get_temp_no_adc,
  292. .get_mode = qpnp_tz_get_mode,
  293. .set_mode = qpnp_tz_set_mode,
  294. .get_trip_type = qpnp_tz_get_trip_type,
  295. .get_trip_temp = qpnp_tz_get_trip_temp,
  296. .get_crit_temp = qpnp_tz_get_crit_temp,
  297. };
  298. static struct thermal_zone_device_ops qpnp_thermal_zone_ops_qpnp_adc = {
  299. .get_temp = qpnp_tz_get_temp_qpnp_adc,
  300. .get_mode = qpnp_tz_get_mode,
  301. .set_mode = qpnp_tz_set_mode,
  302. .get_trip_type = qpnp_tz_get_trip_type,
  303. .get_trip_temp = qpnp_tz_get_trip_temp,
  304. .get_crit_temp = qpnp_tz_get_crit_temp,
  305. };
  306. static void qpnp_tm_work(struct work_struct *work)
  307. {
  308. struct delayed_work *dwork
  309. = container_of(work, struct delayed_work, work);
  310. struct qpnp_tm_chip *chip
  311. = container_of(dwork, struct qpnp_tm_chip, irq_work);
  312. int rc;
  313. u8 reg;
  314. if (chip->adc_type == QPNP_TM_ADC_NONE) {
  315. rc = qpnp_tm_update_temp_no_adc(chip);
  316. if (rc < 0)
  317. goto bail;
  318. } else {
  319. rc = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg, 1);
  320. if (rc < 0)
  321. goto bail;
  322. chip->stage = reg & STATUS_STAGE_MASK;
  323. rc = qpnp_tm_update_temp(chip);
  324. if (rc < 0)
  325. goto bail;
  326. }
  327. if (chip->stage != chip->prev_stage) {
  328. chip->prev_stage = chip->stage;
  329. pr_crit("%s: PMIC Temp Alarm - stage=%u, threshold=%u, temperature=%lu mC\n",
  330. chip->tm_name, chip->stage, chip->thresh,
  331. chip->temperature);
  332. thermal_zone_device_update(chip->tz_dev);
  333. /* Notify user space */
  334. sysfs_notify(&chip->tz_dev->device.kobj, NULL, "type");
  335. }
  336. bail:
  337. return;
  338. }
  339. static irqreturn_t qpnp_tm_isr(int irq, void *data)
  340. {
  341. struct qpnp_tm_chip *chip = data;
  342. schedule_delayed_work(&chip->irq_work,
  343. msecs_to_jiffies(STATUS_REGISTER_DELAY_MS) + 1);
  344. return IRQ_HANDLED;
  345. }
  346. static int qpnp_tm_init_reg(struct qpnp_tm_chip *chip)
  347. {
  348. int rc = 0;
  349. u8 reg;
  350. if (chip->thresh < THRESH_MIN || chip->thresh > THRESH_MAX) {
  351. /* Read hardware threshold value if configuration is invalid. */
  352. rc = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg, 1);
  353. if (rc < 0)
  354. return rc;
  355. chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
  356. }
  357. /*
  358. * Set threshold and disable software override of stage 2 and 3
  359. * shutdowns.
  360. */
  361. reg = chip->thresh & SHUTDOWN_CTRL1_THRESHOLD_MASK;
  362. rc = qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg, 1);
  363. if (rc < 0)
  364. return rc;
  365. /* Enable the thermal alarm PMIC module in always-on mode. */
  366. reg = ALARM_CTRL_FORCE_ENABLE;
  367. rc = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, &reg, 1);
  368. return rc;
  369. }
  370. static int __devinit qpnp_tm_probe(struct spmi_device *spmi)
  371. {
  372. struct device_node *node;
  373. struct resource *res;
  374. struct qpnp_tm_chip *chip;
  375. struct thermal_zone_device_ops *tz_ops;
  376. char *tm_name;
  377. u32 default_temperature;
  378. int rc = 0;
  379. u8 raw_type[2], type, subtype;
  380. if (!spmi || !(&spmi->dev) || !spmi->dev.of_node) {
  381. dev_err(&spmi->dev, "%s: device tree node not found\n",
  382. __func__);
  383. return -EINVAL;
  384. }
  385. node = spmi->dev.of_node;
  386. chip = kzalloc(sizeof(struct qpnp_tm_chip), GFP_KERNEL);
  387. if (!chip) {
  388. dev_err(&spmi->dev, "%s: Can't allocate qpnp_tm_chip\n",
  389. __func__);
  390. return -ENOMEM;
  391. }
  392. dev_set_drvdata(&spmi->dev, chip);
  393. res = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0);
  394. if (!res) {
  395. dev_err(&spmi->dev, "%s: node is missing base address\n",
  396. __func__);
  397. rc = -EINVAL;
  398. goto free_chip;
  399. }
  400. chip->base_addr = res->start;
  401. chip->spmi_dev = spmi;
  402. chip->irq = spmi_get_irq(spmi, NULL, 0);
  403. if (chip->irq < 0) {
  404. rc = chip->irq;
  405. dev_err(&spmi->dev, "%s: node is missing irq, rc=%d\n",
  406. __func__, rc);
  407. goto free_chip;
  408. }
  409. chip->tm_name = of_get_property(node, "label", NULL);
  410. if (chip->tm_name == NULL) {
  411. dev_err(&spmi->dev, "%s: node is missing label\n",
  412. __func__);
  413. rc = -EINVAL;
  414. goto free_chip;
  415. }
  416. tm_name = kstrdup(chip->tm_name, GFP_KERNEL);
  417. if (tm_name == NULL) {
  418. dev_err(&spmi->dev, "%s: could not allocate memory for label\n",
  419. __func__);
  420. rc = -ENOMEM;
  421. goto free_chip;
  422. }
  423. chip->tm_name = tm_name;
  424. INIT_DELAYED_WORK(&chip->irq_work, qpnp_tm_work);
  425. /* These bindings are optional, so it is okay if they are not found. */
  426. chip->thresh = THRESH_MAX + 1;
  427. rc = of_property_read_u32(node, "qcom,threshold-set", &chip->thresh);
  428. if (!rc && (chip->thresh < THRESH_MIN || chip->thresh > THRESH_MAX))
  429. dev_err(&spmi->dev, "%s: invalid qcom,threshold-set=%u specified\n",
  430. __func__, chip->thresh);
  431. chip->adc_type = QPNP_TM_ADC_NONE;
  432. rc = of_property_read_u32(node, "qcom,channel-num", &chip->adc_channel);
  433. if (!rc) {
  434. if (chip->adc_channel < 0 || chip->adc_channel >= ADC_MAX_NUM) {
  435. dev_err(&spmi->dev, "%s: invalid qcom,channel-num=%d specified\n",
  436. __func__, chip->adc_channel);
  437. } else {
  438. chip->adc_type = QPNP_TM_ADC_QPNP_ADC;
  439. chip->vadc_dev = qpnp_get_vadc(&spmi->dev,
  440. "temp_alarm");
  441. if (IS_ERR(chip->vadc_dev)) {
  442. rc = PTR_ERR(chip->vadc_dev);
  443. if (rc != -EPROBE_DEFER)
  444. pr_err("vadc property missing\n");
  445. goto err_cancel_work;
  446. }
  447. }
  448. }
  449. if (chip->adc_type == QPNP_TM_ADC_QPNP_ADC)
  450. tz_ops = &qpnp_thermal_zone_ops_qpnp_adc;
  451. else
  452. tz_ops = &qpnp_thermal_zone_ops_no_adc;
  453. chip->allow_software_override
  454. = of_property_read_bool(node, "qcom,allow-override");
  455. default_temperature = DEFAULT_NO_ADC_TEMP;
  456. rc = of_property_read_u32(node, "qcom,default-temp",
  457. &default_temperature);
  458. chip->temperature = default_temperature;
  459. rc = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, raw_type, 2);
  460. if (rc) {
  461. dev_err(&spmi->dev, "%s: could not read type register, rc=%d\n",
  462. __func__, rc);
  463. goto err_cancel_work;
  464. }
  465. type = raw_type[0];
  466. subtype = raw_type[1];
  467. if (type != QPNP_TM_TYPE || subtype != QPNP_TM_SUBTYPE) {
  468. dev_err(&spmi->dev, "%s: invalid type=%02X or subtype=%02X register value\n",
  469. __func__, type, subtype);
  470. rc = -ENODEV;
  471. goto err_cancel_work;
  472. }
  473. rc = qpnp_tm_init_reg(chip);
  474. if (rc) {
  475. dev_err(&spmi->dev, "%s: qpnp_tm_init_reg() failed, rc=%d\n",
  476. __func__, rc);
  477. goto err_cancel_work;
  478. }
  479. if (chip->adc_type == QPNP_TM_ADC_NONE) {
  480. rc = qpnp_tm_init_temp_no_adc(chip);
  481. if (rc) {
  482. dev_err(&spmi->dev, "%s: qpnp_tm_init_temp_no_adc() failed, rc=%d\n",
  483. __func__, rc);
  484. goto err_cancel_work;
  485. }
  486. }
  487. /* Start in HW control; switch to SW control when user changes mode. */
  488. chip->mode = THERMAL_DEVICE_DISABLED;
  489. rc = qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED);
  490. if (rc) {
  491. dev_err(&spmi->dev, "%s: qpnp_tm_shutdown_override() failed, rc=%d\n",
  492. __func__, rc);
  493. goto err_cancel_work;
  494. }
  495. chip->tz_dev = thermal_zone_device_register(tm_name, TRIP_NUM, chip,
  496. tz_ops, 0, 0, 0, 0);
  497. if (chip->tz_dev == NULL) {
  498. dev_err(&spmi->dev, "%s: thermal_zone_device_register() failed.\n",
  499. __func__);
  500. rc = -ENODEV;
  501. goto err_cancel_work;
  502. }
  503. rc = request_irq(chip->irq, qpnp_tm_isr, IRQF_TRIGGER_RISING, tm_name,
  504. chip);
  505. if (rc < 0) {
  506. dev_err(&spmi->dev, "%s: request_irq(%d) failed: %d\n",
  507. __func__, chip->irq, rc);
  508. goto err_free_tz;
  509. }
  510. return 0;
  511. err_free_tz:
  512. thermal_zone_device_unregister(chip->tz_dev);
  513. err_cancel_work:
  514. cancel_delayed_work_sync(&chip->irq_work);
  515. kfree(chip->tm_name);
  516. free_chip:
  517. dev_set_drvdata(&spmi->dev, NULL);
  518. kfree(chip);
  519. return rc;
  520. }
  521. static int __devexit qpnp_tm_remove(struct spmi_device *spmi)
  522. {
  523. struct qpnp_tm_chip *chip = dev_get_drvdata(&spmi->dev);
  524. dev_set_drvdata(&spmi->dev, NULL);
  525. thermal_zone_device_unregister(chip->tz_dev);
  526. kfree(chip->tm_name);
  527. qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED);
  528. free_irq(chip->irq, chip);
  529. cancel_delayed_work_sync(&chip->irq_work);
  530. kfree(chip);
  531. return 0;
  532. }
  533. #ifdef CONFIG_PM
  534. static int qpnp_tm_suspend(struct device *dev)
  535. {
  536. struct qpnp_tm_chip *chip = dev_get_drvdata(dev);
  537. /* Clear override bits in suspend to allow hardware control */
  538. qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED);
  539. return 0;
  540. }
  541. static int qpnp_tm_resume(struct device *dev)
  542. {
  543. struct qpnp_tm_chip *chip = dev_get_drvdata(dev);
  544. /* Override hardware actions so software can control */
  545. if (chip->mode == THERMAL_DEVICE_ENABLED)
  546. qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_ENABLED);
  547. return 0;
  548. }
  549. static const struct dev_pm_ops qpnp_tm_pm_ops = {
  550. .suspend = qpnp_tm_suspend,
  551. .resume = qpnp_tm_resume,
  552. };
  553. #define QPNP_TM_PM_OPS (&qpnp_tm_pm_ops)
  554. #else
  555. #define QPNP_TM_PM_OPS NULL
  556. #endif
  557. static struct of_device_id qpnp_tm_match_table[] = {
  558. { .compatible = QPNP_TM_DRIVER_NAME, },
  559. {}
  560. };
  561. static const struct spmi_device_id qpnp_tm_id[] = {
  562. { QPNP_TM_DRIVER_NAME, 0 },
  563. {}
  564. };
  565. static struct spmi_driver qpnp_tm_driver = {
  566. .driver = {
  567. .name = QPNP_TM_DRIVER_NAME,
  568. .of_match_table = qpnp_tm_match_table,
  569. .owner = THIS_MODULE,
  570. .pm = QPNP_TM_PM_OPS,
  571. },
  572. .probe = qpnp_tm_probe,
  573. .remove = __devexit_p(qpnp_tm_remove),
  574. .id_table = qpnp_tm_id,
  575. };
  576. int __init qpnp_tm_init(void)
  577. {
  578. return spmi_driver_register(&qpnp_tm_driver);
  579. }
  580. static void __exit qpnp_tm_exit(void)
  581. {
  582. spmi_driver_unregister(&qpnp_tm_driver);
  583. }
  584. module_init(qpnp_tm_init);
  585. module_exit(qpnp_tm_exit);
  586. MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
  587. MODULE_LICENSE("GPL v2");