blob: e86a297d48c18c8da88cf74d211063f9d7f46f97 [file] [log] [blame]
David Collins8885f792017-01-26 14:36:34 -08001/*
2 * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/module.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/string.h>
20#include <linux/interrupt.h>
21#include <linux/kernel.h>
22#include <linux/regmap.h>
23#include <linux/slab.h>
24#include <linux/spmi.h>
25#include <linux/platform_device.h>
26#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/thermal.h>
29#include <linux/qpnp/qpnp-adc.h>
30
31#define QPNP_TM_DRIVER_NAME "qcom,qpnp-temp-alarm"
32
33enum qpnp_tm_registers {
34 QPNP_TM_REG_TYPE = 0x04,
35 QPNP_TM_REG_SUBTYPE = 0x05,
36 QPNP_TM_REG_STATUS = 0x08,
37 QPNP_TM_REG_SHUTDOWN_CTRL1 = 0x40,
38 QPNP_TM_REG_SHUTDOWN_CTRL2 = 0x42,
39 QPNP_TM_REG_ALARM_CTRL = 0x46,
40};
41
42#define QPNP_TM_TYPE 0x09
43#define QPNP_TM_SUBTYPE_GEN1 0x08
44#define QPNP_TM_SUBTYPE_GEN2 0x09
45
46#define STATUS_STATE_MASK 0x70
47#define STATUS_STATE_SHIFT 4
48#define STATUS_STAGE_MASK 0x03
49
50#define SHUTDOWN_CTRL1_OVERRIDE_STAGE3 0x80
51#define SHUTDOWN_CTRL1_OVERRIDE_STAGE2 0x40
52#define SHUTDOWN_CTRL1_CLK_RATE_MASK 0x0C
53#define SHUTDOWN_CTRL1_CLK_RATE_SHIFT 2
54#define SHUTDOWN_CTRL1_THRESHOLD_MASK 0x03
55
56#define SHUTDOWN_CTRL2_CLEAR_STAGE3 0x80
57#define SHUTDOWN_CTRL2_CLEAR_STAGE2 0x40
58
59#define ALARM_CTRL_FORCE_ENABLE 0x80
60#define ALARM_CTRL_FOLLOW_HW_ENABLE 0x01
61
62#define TEMP_STAGE_STEP 20000 /* Stage step: 20.000 C */
63#define TEMP_STAGE_HYSTERESIS 2000
64
65#define TEMP_THRESH_MIN 105000 /* Threshold Min: 105 C */
66#define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
67
68#define THRESH_MIN 0
69#define THRESH_MAX 3
70
71#define CLOCK_RATE_MIN 0
72#define CLOCK_RATE_MAX 3
73
74/* Trip points from most critical to least critical */
75#define TRIP_STAGE3 0
76#define TRIP_STAGE2 1
77#define TRIP_STAGE1 2
78#define TRIP_NUM 3
79
80enum qpnp_tm_adc_type {
81 QPNP_TM_ADC_NONE, /* Estimates temp based on overload level. */
82 QPNP_TM_ADC_QPNP_ADC,
83};
84
85/*
86 * Temperature in millicelcius reported during stage 0 if no ADC is present and
87 * no value has been specified via device tree.
88 */
89#define DEFAULT_NO_ADC_TEMP 37000
90
91struct qpnp_tm_chip {
92 struct delayed_work irq_work;
93 struct platform_device *pdev;
94 struct regmap *regmap;
95 struct thermal_zone_device *tz_dev;
96 const char *tm_name;
97 unsigned int subtype;
98 enum qpnp_tm_adc_type adc_type;
99 int temperature;
100 enum thermal_device_mode mode;
101 unsigned int thresh;
102 unsigned int clock_rate;
103 unsigned int stage;
104 unsigned int prev_stage;
105 int irq;
106 enum qpnp_vadc_channels adc_channel;
107 u16 base_addr;
108 bool allow_software_override;
109 struct qpnp_vadc_chip *vadc_dev;
110};
111
112/* Delay between TEMP_STAT IRQ going high and status value changing in ms. */
113#define STATUS_REGISTER_DELAY_MS 40
114
115enum pmic_thermal_override_mode {
116 SOFTWARE_OVERRIDE_DISABLED = 0,
117 SOFTWARE_OVERRIDE_ENABLED,
118};
119
120/* This array maps from GEN2 alarm state to GEN1 alarm stage */
121const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3};
122
123static inline int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *buf,
124 int len)
125{
126 int rc;
127
128 rc = regmap_bulk_read(chip->regmap, chip->base_addr + addr, buf, len);
129
130 if (rc)
131 dev_err(&chip->pdev->dev,
132 "%s: regmap_bulk_readl failed. sid=%d, addr=%04X, len=%d, rc=%d\n",
133 __func__,
134 to_spmi_device(chip->pdev->dev.parent)->usid,
135 chip->base_addr + addr,
136 len, rc);
137
138 return rc;
139}
140
141static inline int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 *buf,
142 int len)
143{
144 int rc;
145
146 rc = regmap_bulk_write(chip->regmap, chip->base_addr + addr, buf, len);
147
148 if (rc)
149 dev_err(&chip->pdev->dev,
150 "%s: regmap_bulk_write failed. sid=%d, addr=%04X, len=%d, rc=%d\n",
151 __func__,
152 to_spmi_device(chip->pdev->dev.parent)->usid,
153 chip->base_addr + addr,
154 len, rc);
155
156 return rc;
157}
158
159
160static inline int qpnp_tm_shutdown_override(struct qpnp_tm_chip *chip,
161 enum pmic_thermal_override_mode mode)
162{
163 int rc = 0;
164 u8 reg;
165
166 if (chip->allow_software_override) {
167 reg = chip->thresh & SHUTDOWN_CTRL1_THRESHOLD_MASK;
168 reg |= (chip->clock_rate << SHUTDOWN_CTRL1_CLK_RATE_SHIFT)
169 & SHUTDOWN_CTRL1_CLK_RATE_MASK;
170
171 if (mode == SOFTWARE_OVERRIDE_ENABLED)
172 reg |= SHUTDOWN_CTRL1_OVERRIDE_STAGE2
173 | SHUTDOWN_CTRL1_OVERRIDE_STAGE3;
174
175 rc = qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg, 1);
176 }
177
178 return rc;
179}
180
181static int qpnp_tm_update_temp(struct qpnp_tm_chip *chip)
182{
183 struct qpnp_vadc_result adc_result;
184 int rc;
185
186 rc = qpnp_vadc_read(chip->vadc_dev, chip->adc_channel, &adc_result);
187 if (!rc)
188 chip->temperature = adc_result.physical;
189 else
190 dev_err(&chip->pdev->dev,
191 "%s: qpnp_vadc_read(%d) failed, rc=%d\n",
192 __func__, chip->adc_channel, rc);
193
194 return rc;
195}
196
197static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip,
198 unsigned int *stage)
199{
200 int rc;
201 u8 reg;
202
203 rc = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg, 1);
204 if (rc < 0)
205 return rc;
206
207 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
208 *stage = reg & STATUS_STAGE_MASK;
209 else
210 *stage = (reg & STATUS_STATE_MASK) >> STATUS_STATE_SHIFT;
211
212 return 0;
213}
214
215/*
216 * This function initializes the internal temperature value based on only the
217 * current thermal stage and threshold.
218 */
219static int qpnp_tm_init_temp_no_adc(struct qpnp_tm_chip *chip)
220{
221 unsigned int stage;
222 int rc;
223
224 rc = qpnp_tm_get_temp_stage(chip, &chip->stage);
225 if (rc < 0)
226 return rc;
227
228 stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1
229 ? chip->stage : alarm_state_map[chip->stage];
230
231 if (stage)
232 chip->temperature = chip->thresh * TEMP_THRESH_STEP +
233 (stage - 1) * TEMP_STAGE_STEP +
234 TEMP_THRESH_MIN;
235
236 return 0;
237}
238
239/*
240 * This function updates the internal temperature value based on the
241 * current thermal stage and threshold as well as the previous stage
242 */
243static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
244{
245 unsigned int stage, stage_new, stage_old;
246 int rc;
247
248 rc = qpnp_tm_get_temp_stage(chip, &stage);
249 if (rc < 0)
250 return rc;
251
252 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
253 stage_new = stage;
254 stage_old = chip->stage;
255 } else {
256 stage_new = alarm_state_map[stage];
257 stage_old = alarm_state_map[chip->stage];
258 }
259
260 if (stage_new > stage_old) {
261 /* increasing stage, use lower bound */
262 chip->temperature = (stage_new - 1) * TEMP_STAGE_STEP
263 + chip->thresh * TEMP_THRESH_STEP
264 + TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
265 } else if (stage_new < stage_old) {
266 /* decreasing stage, use upper bound */
267 chip->temperature = stage_new * TEMP_STAGE_STEP
268 + chip->thresh * TEMP_THRESH_STEP
269 - TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
270 }
271
272 chip->stage = stage;
273
274 return 0;
275}
276
277static int qpnp_tz_get_temp_no_adc(struct thermal_zone_device *thermal,
278 int *temperature)
279{
280 struct qpnp_tm_chip *chip = thermal->devdata;
281 int rc;
282
283 if (!temperature)
284 return -EINVAL;
285
286 rc = qpnp_tm_update_temp_no_adc(chip);
287 if (rc < 0)
288 return rc;
289
290 *temperature = chip->temperature;
291
292 return 0;
293}
294
295static int qpnp_tz_get_temp_qpnp_adc(struct thermal_zone_device *thermal,
296 int *temperature)
297{
298 struct qpnp_tm_chip *chip = thermal->devdata;
299 int rc;
300
301 if (!temperature)
302 return -EINVAL;
303
304 rc = qpnp_tm_update_temp(chip);
305 if (rc < 0) {
306 dev_err(&chip->pdev->dev,
307 "%s: %s: adc read failed, rc = %d\n",
308 __func__, chip->tm_name, rc);
309 return rc;
310 }
311
312 *temperature = chip->temperature;
313
314 return 0;
315}
316
317static int qpnp_tz_get_mode(struct thermal_zone_device *thermal,
318 enum thermal_device_mode *mode)
319{
320 struct qpnp_tm_chip *chip = thermal->devdata;
321
322 if (!mode)
323 return -EINVAL;
324
325 *mode = chip->mode;
326
327 return 0;
328}
329
330static int qpnp_tz_set_mode(struct thermal_zone_device *thermal,
331 enum thermal_device_mode mode)
332{
333 struct qpnp_tm_chip *chip = thermal->devdata;
334 int rc = 0;
335
336 if (mode != chip->mode) {
337 if (mode == THERMAL_DEVICE_ENABLED)
338 rc = qpnp_tm_shutdown_override(chip,
339 SOFTWARE_OVERRIDE_ENABLED);
340 else
341 rc = qpnp_tm_shutdown_override(chip,
342 SOFTWARE_OVERRIDE_DISABLED);
343
344 chip->mode = mode;
345 }
346
347 return rc;
348}
349
350static int qpnp_tz_get_trip_type(struct thermal_zone_device *thermal,
351 int trip, enum thermal_trip_type *type)
352{
353 if (trip < 0 || !type)
354 return -EINVAL;
355
356 switch (trip) {
357 case TRIP_STAGE3:
358 *type = THERMAL_TRIP_CRITICAL;
359 break;
360 case TRIP_STAGE2:
361 *type = THERMAL_TRIP_HOT;
362 break;
363 case TRIP_STAGE1:
364 *type = THERMAL_TRIP_HOT;
365 break;
366 default:
367 return -EINVAL;
368 }
369
370 return 0;
371}
372
373static int qpnp_tz_get_trip_temp(struct thermal_zone_device *thermal,
374 int trip, int *temperature)
375{
376 struct qpnp_tm_chip *chip = thermal->devdata;
377 int thresh_temperature;
378
379 if (trip < 0 || !temperature)
380 return -EINVAL;
381
382 thresh_temperature = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN;
383
384 switch (trip) {
385 case TRIP_STAGE3:
386 thresh_temperature += 2 * TEMP_STAGE_STEP;
387 break;
388 case TRIP_STAGE2:
389 thresh_temperature += TEMP_STAGE_STEP;
390 break;
391 case TRIP_STAGE1:
392 break;
393 default:
394 return -EINVAL;
395 }
396
397 *temperature = thresh_temperature;
398
399 return 0;
400}
401
402static int qpnp_tz_get_crit_temp(struct thermal_zone_device *thermal,
403 int *temperature)
404{
405 struct qpnp_tm_chip *chip = thermal->devdata;
406
407 if (!temperature)
408 return -EINVAL;
409
410 *temperature = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN +
411 2 * TEMP_STAGE_STEP;
412
413 return 0;
414}
415
416static struct thermal_zone_device_ops qpnp_thermal_zone_ops_no_adc = {
417 .get_temp = qpnp_tz_get_temp_no_adc,
418 .get_mode = qpnp_tz_get_mode,
419 .set_mode = qpnp_tz_set_mode,
420 .get_trip_type = qpnp_tz_get_trip_type,
421 .get_trip_temp = qpnp_tz_get_trip_temp,
422 .get_crit_temp = qpnp_tz_get_crit_temp,
423};
424
425static struct thermal_zone_device_ops qpnp_thermal_zone_ops_qpnp_adc = {
426 .get_temp = qpnp_tz_get_temp_qpnp_adc,
427 .get_mode = qpnp_tz_get_mode,
428 .set_mode = qpnp_tz_set_mode,
429 .get_trip_type = qpnp_tz_get_trip_type,
430 .get_trip_temp = qpnp_tz_get_trip_temp,
431 .get_crit_temp = qpnp_tz_get_crit_temp,
432};
433
434static void qpnp_tm_work(struct work_struct *work)
435{
436 struct delayed_work *dwork
437 = container_of(work, struct delayed_work, work);
438 struct qpnp_tm_chip *chip
439 = container_of(dwork, struct qpnp_tm_chip, irq_work);
440 unsigned int stage_new, stage_old;
441 int rc;
442
443 if (chip->adc_type == QPNP_TM_ADC_NONE) {
444 rc = qpnp_tm_update_temp_no_adc(chip);
445 if (rc < 0)
446 goto bail;
447 } else {
448 rc = qpnp_tm_get_temp_stage(chip, &chip->stage);
449 if (rc < 0)
450 goto bail;
451
452 rc = qpnp_tm_update_temp(chip);
453 if (rc < 0)
454 goto bail;
455 }
456
457 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
458 stage_new = chip->stage;
459 stage_old = chip->prev_stage;
460 } else {
461 stage_new = alarm_state_map[chip->stage];
462 stage_old = alarm_state_map[chip->prev_stage];
463 }
464
465 chip->prev_stage = chip->stage;
466
467 if (stage_new != stage_old) {
468 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
469 pr_crit("%s: PMIC Temp Alarm - stage=%u, threshold=%u, temperature=%d mC\n",
470 chip->tm_name, chip->stage, chip->thresh,
471 chip->temperature);
472 else
473 pr_crit("%s: PMIC Temp Alarm - stage=%u, state=%u, threshold=%u, temperature=%d mC\n",
474 chip->tm_name, stage_new, chip->stage,
475 chip->thresh, chip->temperature);
476
477 thermal_zone_device_update(chip->tz_dev,
478 THERMAL_EVENT_UNSPECIFIED);
479
480 /* Notify user space */
481 sysfs_notify(&chip->tz_dev->device.kobj, NULL, "type");
482 }
483
484bail:
485 return;
486}
487
488static irqreturn_t qpnp_tm_isr(int irq, void *data)
489{
490 struct qpnp_tm_chip *chip = data;
491
492 schedule_delayed_work(&chip->irq_work,
493 msecs_to_jiffies(STATUS_REGISTER_DELAY_MS) + 1);
494
495 return IRQ_HANDLED;
496}
497
498static int qpnp_tm_init_reg(struct qpnp_tm_chip *chip)
499{
500 int rc = 0;
501 u8 reg;
502
503 rc = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg, 1);
504 if (rc < 0)
505 return rc;
506
507 if (chip->thresh < THRESH_MIN || chip->thresh > THRESH_MAX) {
508 /* Use hardware threshold value if configuration is invalid. */
509 chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
510 }
511
512 if (chip->clock_rate < CLOCK_RATE_MIN
513 || chip->clock_rate > CLOCK_RATE_MAX) {
514 /* Use hardware clock rate value if configuration is invalid. */
515 chip->clock_rate = (reg & SHUTDOWN_CTRL1_CLK_RATE_MASK)
516 >> SHUTDOWN_CTRL1_CLK_RATE_SHIFT;
517 }
518
519 /*
520 * Set threshold and clock rate and also disable software override of
521 * stage 2 and 3 shutdowns.
522 */
523 reg = chip->thresh & SHUTDOWN_CTRL1_THRESHOLD_MASK;
524 reg |= (chip->clock_rate << SHUTDOWN_CTRL1_CLK_RATE_SHIFT)
525 & SHUTDOWN_CTRL1_CLK_RATE_MASK;
526 rc = qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg, 1);
527 if (rc < 0)
528 return rc;
529
530 /* Enable the thermal alarm PMIC module in always-on mode. */
531 reg = ALARM_CTRL_FORCE_ENABLE;
532 rc = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, &reg, 1);
533
534 return rc;
535}
536
537static int qpnp_tm_probe(struct platform_device *pdev)
538{
539 struct device_node *node;
540 unsigned int base;
541 struct qpnp_tm_chip *chip;
542 struct thermal_zone_device_ops *tz_ops;
543 char *tm_name;
544 u32 default_temperature;
545 int rc = 0;
546 u8 raw_type[2], type, subtype;
547
548 if (!pdev || !(&pdev->dev) || !pdev->dev.of_node) {
549 dev_err(&pdev->dev, "%s: device tree node not found\n",
550 __func__);
551 return -EINVAL;
552 }
553
554 node = pdev->dev.of_node;
555
556 chip = kzalloc(sizeof(struct qpnp_tm_chip), GFP_KERNEL);
557 if (!chip)
558 return -ENOMEM;
559
560 chip->regmap = dev_get_regmap(pdev->dev.parent, NULL);
561 if (!chip->regmap) {
562 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
563 return -EINVAL;
564 }
565
566 dev_set_drvdata(&pdev->dev, chip);
567
568 rc = of_property_read_u32(pdev->dev.of_node, "reg", &base);
569 if (rc < 0) {
570 dev_err(&pdev->dev,
571 "Couldn't find reg in node = %s rc = %d\n",
572 pdev->dev.of_node->full_name, rc);
573 goto free_chip;
574 }
575 chip->base_addr = base;
576 chip->pdev = pdev;
577
578 chip->irq = platform_get_irq(pdev, 0);
579 if (chip->irq < 0) {
580 rc = chip->irq;
581 dev_err(&pdev->dev, "%s: node is missing irq, rc=%d\n",
582 __func__, rc);
583 goto free_chip;
584 }
585
586 chip->tm_name = of_get_property(node, "label", NULL);
587 if (chip->tm_name == NULL) {
588 dev_err(&pdev->dev, "%s: node is missing label\n", __func__);
589 rc = -EINVAL;
590 goto free_chip;
591 }
592
593 tm_name = kstrdup(chip->tm_name, GFP_KERNEL);
594 if (tm_name == NULL) {
595 rc = -ENOMEM;
596 goto free_chip;
597 }
598 chip->tm_name = tm_name;
599
600 INIT_DELAYED_WORK(&chip->irq_work, qpnp_tm_work);
601
602 /* These bindings are optional, so it is okay if they are not found. */
603 chip->thresh = THRESH_MAX + 1;
604 rc = of_property_read_u32(node, "qcom,threshold-set", &chip->thresh);
605 if (!rc && (chip->thresh < THRESH_MIN || chip->thresh > THRESH_MAX))
606 dev_err(&pdev->dev,
607 "%s: invalid qcom,threshold-set=%u specified\n",
608 __func__, chip->thresh);
609
610 chip->clock_rate = CLOCK_RATE_MAX + 1;
611 rc = of_property_read_u32(node, "qcom,clock-rate", &chip->clock_rate);
612 if (!rc && (chip->clock_rate < CLOCK_RATE_MIN
613 || chip->clock_rate > CLOCK_RATE_MAX))
614 dev_err(&pdev->dev,
615 "%s: invalid qcom,clock-rate=%u specified\n", __func__,
616 chip->clock_rate);
617
618 chip->adc_type = QPNP_TM_ADC_NONE;
619 rc = of_property_read_u32(node, "qcom,channel-num", &chip->adc_channel);
620 if (!rc) {
621 if (chip->adc_channel < 0 || chip->adc_channel >= ADC_MAX_NUM) {
622 dev_err(&pdev->dev,
623 "%s: invalid qcom,channel-num=%d specified\n",
624 __func__, chip->adc_channel);
625 } else {
626 chip->adc_type = QPNP_TM_ADC_QPNP_ADC;
627 chip->vadc_dev = qpnp_get_vadc(&pdev->dev,
628 "temp_alarm");
629 if (IS_ERR(chip->vadc_dev)) {
630 rc = PTR_ERR(chip->vadc_dev);
631 if (rc != -EPROBE_DEFER)
632 pr_err("vadc property missing\n");
633 goto err_cancel_work;
634 }
635 }
636 }
637
638 if (chip->adc_type == QPNP_TM_ADC_QPNP_ADC)
639 tz_ops = &qpnp_thermal_zone_ops_qpnp_adc;
640 else
641 tz_ops = &qpnp_thermal_zone_ops_no_adc;
642
643 chip->allow_software_override
644 = of_property_read_bool(node, "qcom,allow-override");
645
646 default_temperature = DEFAULT_NO_ADC_TEMP;
647 rc = of_property_read_u32(node, "qcom,default-temp",
648 &default_temperature);
649 chip->temperature = default_temperature;
650
651 rc = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, raw_type, 2);
652 if (rc) {
653 dev_err(&pdev->dev,
654 "%s: could not read type register, rc=%d\n",
655 __func__, rc);
656 goto err_cancel_work;
657 }
658 type = raw_type[0];
659 subtype = raw_type[1];
660
661 if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1
662 && subtype != QPNP_TM_SUBTYPE_GEN2)) {
663 dev_err(&pdev->dev,
664 "%s: invalid type=%02X or subtype=%02X register value\n",
665 __func__, type, subtype);
666 rc = -ENODEV;
667 goto err_cancel_work;
668 }
669
670 chip->subtype = subtype;
671
672 rc = qpnp_tm_init_reg(chip);
673 if (rc) {
674 dev_err(&pdev->dev, "%s: qpnp_tm_init_reg() failed, rc=%d\n",
675 __func__, rc);
676 goto err_cancel_work;
677 }
678
679 if (chip->adc_type == QPNP_TM_ADC_NONE) {
680 rc = qpnp_tm_init_temp_no_adc(chip);
681 if (rc) {
682 dev_err(&pdev->dev,
683 "%s: qpnp_tm_init_temp_no_adc() failed, rc=%d\n",
684 __func__, rc);
685 goto err_cancel_work;
686 }
687 }
688
689 /* Start in HW control; switch to SW control when user changes mode. */
690 chip->mode = THERMAL_DEVICE_DISABLED;
691 rc = qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED);
692 if (rc) {
693 dev_err(&pdev->dev,
694 "%s: qpnp_tm_shutdown_override() failed, rc=%d\n",
695 __func__, rc);
696 goto err_cancel_work;
697 }
698
699 chip->tz_dev = thermal_zone_device_register(tm_name, TRIP_NUM, 0, chip,
700 tz_ops, NULL, 0, 0);
701 if (chip->tz_dev == NULL) {
702 dev_err(&pdev->dev,
703 "%s: thermal_zone_device_register() failed.\n",
704 __func__);
705 rc = -ENODEV;
706 goto err_cancel_work;
707 }
708
709 rc = request_irq(chip->irq, qpnp_tm_isr, IRQF_TRIGGER_RISING, tm_name,
710 chip);
711 if (rc < 0) {
712 dev_err(&pdev->dev, "%s: request_irq(%d) failed: %d\n",
713 __func__, chip->irq, rc);
714 goto err_free_tz;
715 }
716
717 return 0;
718
719err_free_tz:
720 thermal_zone_device_unregister(chip->tz_dev);
721err_cancel_work:
722 cancel_delayed_work_sync(&chip->irq_work);
723 kfree(chip->tm_name);
724free_chip:
725 dev_set_drvdata(&pdev->dev, NULL);
726 kfree(chip);
727 return rc;
728}
729
730static int qpnp_tm_remove(struct platform_device *pdev)
731{
732 struct qpnp_tm_chip *chip = dev_get_drvdata(&pdev->dev);
733
734 dev_set_drvdata(&pdev->dev, NULL);
735 thermal_zone_device_unregister(chip->tz_dev);
736 kfree(chip->tm_name);
737 qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED);
738 free_irq(chip->irq, chip);
739 cancel_delayed_work_sync(&chip->irq_work);
740 kfree(chip);
741
742 return 0;
743}
744
745#ifdef CONFIG_PM
746static int qpnp_tm_suspend(struct device *dev)
747{
748 struct qpnp_tm_chip *chip = dev_get_drvdata(dev);
749
750 /* Clear override bits in suspend to allow hardware control */
751 qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED);
752
753 return 0;
754}
755
756static int qpnp_tm_resume(struct device *dev)
757{
758 struct qpnp_tm_chip *chip = dev_get_drvdata(dev);
759
760 /* Override hardware actions so software can control */
761 if (chip->mode == THERMAL_DEVICE_ENABLED)
762 qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_ENABLED);
763
764 return 0;
765}
766
767static const struct dev_pm_ops qpnp_tm_pm_ops = {
768 .suspend = qpnp_tm_suspend,
769 .resume = qpnp_tm_resume,
770};
771
772#define QPNP_TM_PM_OPS (&qpnp_tm_pm_ops)
773#else
774#define QPNP_TM_PM_OPS NULL
775#endif
776
777static const struct of_device_id qpnp_tm_match_table[] = {
778 { .compatible = QPNP_TM_DRIVER_NAME, },
779 {}
780};
781
782static const struct platform_device_id qpnp_tm_id[] = {
783 { QPNP_TM_DRIVER_NAME, 0 },
784 {}
785};
786
787static struct platform_driver qpnp_tm_driver = {
788 .driver = {
789 .name = QPNP_TM_DRIVER_NAME,
790 .of_match_table = qpnp_tm_match_table,
791 .owner = THIS_MODULE,
792 .pm = QPNP_TM_PM_OPS,
793 },
794 .probe = qpnp_tm_probe,
795 .remove = qpnp_tm_remove,
796 .id_table = qpnp_tm_id,
797};
798
799int __init qpnp_tm_init(void)
800{
801 return platform_driver_register(&qpnp_tm_driver);
802}
803
804static void __exit qpnp_tm_exit(void)
805{
806 platform_driver_unregister(&qpnp_tm_driver);
807}
808
809module_init(qpnp_tm_init);
810module_exit(qpnp_tm_exit);
811
812MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
813MODULE_LICENSE("GPL v2");