xref: /openbmc/linux/drivers/thermal/qcom/qcom-spmi-temp-alarm.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
197fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
272e9baf9SAmit Kucheria /*
30ffdab6fSVeera Vegivada  * Copyright (c) 2011-2015, 2017, 2020, The Linux Foundation. All rights reserved.
472e9baf9SAmit Kucheria  */
572e9baf9SAmit Kucheria 
672e9baf9SAmit Kucheria #include <linux/bitops.h>
772e9baf9SAmit Kucheria #include <linux/delay.h>
872e9baf9SAmit Kucheria #include <linux/err.h>
972e9baf9SAmit Kucheria #include <linux/iio/consumer.h>
1072e9baf9SAmit Kucheria #include <linux/interrupt.h>
1172e9baf9SAmit Kucheria #include <linux/module.h>
1272e9baf9SAmit Kucheria #include <linux/of.h>
1372e9baf9SAmit Kucheria #include <linux/platform_device.h>
1472e9baf9SAmit Kucheria #include <linux/regmap.h>
1572e9baf9SAmit Kucheria #include <linux/thermal.h>
1672e9baf9SAmit Kucheria 
17fb55b012SDmitry Baryshkov #include "../thermal_hwmon.h"
1872e9baf9SAmit Kucheria 
19aa92b331SDavid Collins #define QPNP_TM_REG_DIG_MAJOR		0x01
2072e9baf9SAmit Kucheria #define QPNP_TM_REG_TYPE		0x04
2172e9baf9SAmit Kucheria #define QPNP_TM_REG_SUBTYPE		0x05
2272e9baf9SAmit Kucheria #define QPNP_TM_REG_STATUS		0x08
2372e9baf9SAmit Kucheria #define QPNP_TM_REG_SHUTDOWN_CTRL1	0x40
2472e9baf9SAmit Kucheria #define QPNP_TM_REG_ALARM_CTRL		0x46
2572e9baf9SAmit Kucheria 
2672e9baf9SAmit Kucheria #define QPNP_TM_TYPE			0x09
2772e9baf9SAmit Kucheria #define QPNP_TM_SUBTYPE_GEN1		0x08
2872e9baf9SAmit Kucheria #define QPNP_TM_SUBTYPE_GEN2		0x09
2972e9baf9SAmit Kucheria 
3072e9baf9SAmit Kucheria #define STATUS_GEN1_STAGE_MASK		GENMASK(1, 0)
3172e9baf9SAmit Kucheria #define STATUS_GEN2_STATE_MASK		GENMASK(6, 4)
3272e9baf9SAmit Kucheria #define STATUS_GEN2_STATE_SHIFT		4
3372e9baf9SAmit Kucheria 
3472e9baf9SAmit Kucheria #define SHUTDOWN_CTRL1_OVERRIDE_S2	BIT(6)
3572e9baf9SAmit Kucheria #define SHUTDOWN_CTRL1_THRESHOLD_MASK	GENMASK(1, 0)
3672e9baf9SAmit Kucheria 
3772e9baf9SAmit Kucheria #define SHUTDOWN_CTRL1_RATE_25HZ	BIT(3)
3872e9baf9SAmit Kucheria 
3972e9baf9SAmit Kucheria #define ALARM_CTRL_FORCE_ENABLE		BIT(7)
4072e9baf9SAmit Kucheria 
41aa92b331SDavid Collins #define THRESH_COUNT			4
42aa92b331SDavid Collins #define STAGE_COUNT			3
4372e9baf9SAmit Kucheria 
44aa92b331SDavid Collins /* Over-temperature trip point values in mC */
45aa92b331SDavid Collins static const long temp_map_gen1[THRESH_COUNT][STAGE_COUNT] = {
46aa92b331SDavid Collins 	{ 105000, 125000, 145000 },
47aa92b331SDavid Collins 	{ 110000, 130000, 150000 },
48aa92b331SDavid Collins 	{ 115000, 135000, 155000 },
49aa92b331SDavid Collins 	{ 120000, 140000, 160000 },
50aa92b331SDavid Collins };
51aa92b331SDavid Collins 
52aa92b331SDavid Collins static const long temp_map_gen2_v1[THRESH_COUNT][STAGE_COUNT] = {
53aa92b331SDavid Collins 	{  90000, 110000, 140000 },
54aa92b331SDavid Collins 	{  95000, 115000, 145000 },
55aa92b331SDavid Collins 	{ 100000, 120000, 150000 },
56aa92b331SDavid Collins 	{ 105000, 125000, 155000 },
57aa92b331SDavid Collins };
58aa92b331SDavid Collins 
5972e9baf9SAmit Kucheria #define TEMP_THRESH_STEP		5000 /* Threshold step: 5 C */
6072e9baf9SAmit Kucheria 
6172e9baf9SAmit Kucheria #define THRESH_MIN			0
6272e9baf9SAmit Kucheria #define THRESH_MAX			3
6372e9baf9SAmit Kucheria 
64aa92b331SDavid Collins #define TEMP_STAGE_HYSTERESIS		2000
6572e9baf9SAmit Kucheria 
6672e9baf9SAmit Kucheria /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
6772e9baf9SAmit Kucheria #define DEFAULT_TEMP			37000
6872e9baf9SAmit Kucheria 
6972e9baf9SAmit Kucheria struct qpnp_tm_chip {
7072e9baf9SAmit Kucheria 	struct regmap			*map;
7172e9baf9SAmit Kucheria 	struct device			*dev;
7272e9baf9SAmit Kucheria 	struct thermal_zone_device	*tz_dev;
7372e9baf9SAmit Kucheria 	unsigned int			subtype;
7472e9baf9SAmit Kucheria 	long				temp;
7572e9baf9SAmit Kucheria 	unsigned int			thresh;
7672e9baf9SAmit Kucheria 	unsigned int			stage;
7772e9baf9SAmit Kucheria 	unsigned int			prev_stage;
7872e9baf9SAmit Kucheria 	unsigned int			base;
7972e9baf9SAmit Kucheria 	/* protects .thresh, .stage and chip registers */
8072e9baf9SAmit Kucheria 	struct mutex			lock;
8172e9baf9SAmit Kucheria 	bool				initialized;
8272e9baf9SAmit Kucheria 
8372e9baf9SAmit Kucheria 	struct iio_channel		*adc;
84aa92b331SDavid Collins 	const long			(*temp_map)[THRESH_COUNT][STAGE_COUNT];
8572e9baf9SAmit Kucheria };
8672e9baf9SAmit Kucheria 
8772e9baf9SAmit Kucheria /* This array maps from GEN2 alarm state to GEN1 alarm stage */
8872e9baf9SAmit Kucheria static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3};
8972e9baf9SAmit Kucheria 
qpnp_tm_read(struct qpnp_tm_chip * chip,u16 addr,u8 * data)9072e9baf9SAmit Kucheria static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
9172e9baf9SAmit Kucheria {
9272e9baf9SAmit Kucheria 	unsigned int val;
9372e9baf9SAmit Kucheria 	int ret;
9472e9baf9SAmit Kucheria 
9572e9baf9SAmit Kucheria 	ret = regmap_read(chip->map, chip->base + addr, &val);
9672e9baf9SAmit Kucheria 	if (ret < 0)
9772e9baf9SAmit Kucheria 		return ret;
9872e9baf9SAmit Kucheria 
9972e9baf9SAmit Kucheria 	*data = val;
10072e9baf9SAmit Kucheria 	return 0;
10172e9baf9SAmit Kucheria }
10272e9baf9SAmit Kucheria 
qpnp_tm_write(struct qpnp_tm_chip * chip,u16 addr,u8 data)10372e9baf9SAmit Kucheria static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
10472e9baf9SAmit Kucheria {
10572e9baf9SAmit Kucheria 	return regmap_write(chip->map, chip->base + addr, data);
10672e9baf9SAmit Kucheria }
10772e9baf9SAmit Kucheria 
10872e9baf9SAmit Kucheria /**
109aa92b331SDavid Collins  * qpnp_tm_decode_temp() - return temperature in mC corresponding to the
110aa92b331SDavid Collins  *		specified over-temperature stage
111aa92b331SDavid Collins  * @chip:		Pointer to the qpnp_tm chip
112aa92b331SDavid Collins  * @stage:		Over-temperature stage
113aa92b331SDavid Collins  *
114aa92b331SDavid Collins  * Return: temperature in mC
115aa92b331SDavid Collins  */
qpnp_tm_decode_temp(struct qpnp_tm_chip * chip,unsigned int stage)116aa92b331SDavid Collins static long qpnp_tm_decode_temp(struct qpnp_tm_chip *chip, unsigned int stage)
117aa92b331SDavid Collins {
118aa92b331SDavid Collins 	if (!chip->temp_map || chip->thresh >= THRESH_COUNT || stage == 0 ||
119aa92b331SDavid Collins 	    stage > STAGE_COUNT)
120aa92b331SDavid Collins 		return 0;
121aa92b331SDavid Collins 
122aa92b331SDavid Collins 	return (*chip->temp_map)[chip->thresh][stage - 1];
123aa92b331SDavid Collins }
124aa92b331SDavid Collins 
125aa92b331SDavid Collins /**
12672e9baf9SAmit Kucheria  * qpnp_tm_get_temp_stage() - return over-temperature stage
12772e9baf9SAmit Kucheria  * @chip:		Pointer to the qpnp_tm chip
12872e9baf9SAmit Kucheria  *
12972e9baf9SAmit Kucheria  * Return: stage (GEN1) or state (GEN2) on success, or errno on failure.
13072e9baf9SAmit Kucheria  */
qpnp_tm_get_temp_stage(struct qpnp_tm_chip * chip)13172e9baf9SAmit Kucheria static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip)
13272e9baf9SAmit Kucheria {
13372e9baf9SAmit Kucheria 	int ret;
13472e9baf9SAmit Kucheria 	u8 reg = 0;
13572e9baf9SAmit Kucheria 
13672e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg);
13772e9baf9SAmit Kucheria 	if (ret < 0)
13872e9baf9SAmit Kucheria 		return ret;
13972e9baf9SAmit Kucheria 
14072e9baf9SAmit Kucheria 	if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
14172e9baf9SAmit Kucheria 		ret = reg & STATUS_GEN1_STAGE_MASK;
14272e9baf9SAmit Kucheria 	else
14372e9baf9SAmit Kucheria 		ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT;
14472e9baf9SAmit Kucheria 
14572e9baf9SAmit Kucheria 	return ret;
14672e9baf9SAmit Kucheria }
14772e9baf9SAmit Kucheria 
14872e9baf9SAmit Kucheria /*
14972e9baf9SAmit Kucheria  * This function updates the internal temp value based on the
15072e9baf9SAmit Kucheria  * current thermal stage and threshold as well as the previous stage
15172e9baf9SAmit Kucheria  */
qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip * chip)15272e9baf9SAmit Kucheria static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
15372e9baf9SAmit Kucheria {
15472e9baf9SAmit Kucheria 	unsigned int stage, stage_new, stage_old;
15572e9baf9SAmit Kucheria 	int ret;
15672e9baf9SAmit Kucheria 
15772e9baf9SAmit Kucheria 	WARN_ON(!mutex_is_locked(&chip->lock));
15872e9baf9SAmit Kucheria 
15972e9baf9SAmit Kucheria 	ret = qpnp_tm_get_temp_stage(chip);
16072e9baf9SAmit Kucheria 	if (ret < 0)
16172e9baf9SAmit Kucheria 		return ret;
16272e9baf9SAmit Kucheria 	stage = ret;
16372e9baf9SAmit Kucheria 
16472e9baf9SAmit Kucheria 	if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
16572e9baf9SAmit Kucheria 		stage_new = stage;
16672e9baf9SAmit Kucheria 		stage_old = chip->stage;
16772e9baf9SAmit Kucheria 	} else {
16872e9baf9SAmit Kucheria 		stage_new = alarm_state_map[stage];
16972e9baf9SAmit Kucheria 		stage_old = alarm_state_map[chip->stage];
17072e9baf9SAmit Kucheria 	}
17172e9baf9SAmit Kucheria 
17272e9baf9SAmit Kucheria 	if (stage_new > stage_old) {
17372e9baf9SAmit Kucheria 		/* increasing stage, use lower bound */
174aa92b331SDavid Collins 		chip->temp = qpnp_tm_decode_temp(chip, stage_new)
175aa92b331SDavid Collins 				+ TEMP_STAGE_HYSTERESIS;
17672e9baf9SAmit Kucheria 	} else if (stage_new < stage_old) {
17772e9baf9SAmit Kucheria 		/* decreasing stage, use upper bound */
178aa92b331SDavid Collins 		chip->temp = qpnp_tm_decode_temp(chip, stage_new + 1)
179aa92b331SDavid Collins 				- TEMP_STAGE_HYSTERESIS;
18072e9baf9SAmit Kucheria 	}
18172e9baf9SAmit Kucheria 
18272e9baf9SAmit Kucheria 	chip->stage = stage;
18372e9baf9SAmit Kucheria 
18472e9baf9SAmit Kucheria 	return 0;
18572e9baf9SAmit Kucheria }
18672e9baf9SAmit Kucheria 
qpnp_tm_get_temp(struct thermal_zone_device * tz,int * temp)187ca1b9a9eSDaniel Lezcano static int qpnp_tm_get_temp(struct thermal_zone_device *tz, int *temp)
18872e9baf9SAmit Kucheria {
1895f68d078SDaniel Lezcano 	struct qpnp_tm_chip *chip = thermal_zone_device_priv(tz);
19072e9baf9SAmit Kucheria 	int ret, mili_celsius;
19172e9baf9SAmit Kucheria 
19272e9baf9SAmit Kucheria 	if (!temp)
19372e9baf9SAmit Kucheria 		return -EINVAL;
19472e9baf9SAmit Kucheria 
19572e9baf9SAmit Kucheria 	if (!chip->initialized) {
19672e9baf9SAmit Kucheria 		*temp = DEFAULT_TEMP;
19772e9baf9SAmit Kucheria 		return 0;
19872e9baf9SAmit Kucheria 	}
19972e9baf9SAmit Kucheria 
20072e9baf9SAmit Kucheria 	if (!chip->adc) {
20172e9baf9SAmit Kucheria 		mutex_lock(&chip->lock);
20272e9baf9SAmit Kucheria 		ret = qpnp_tm_update_temp_no_adc(chip);
20372e9baf9SAmit Kucheria 		mutex_unlock(&chip->lock);
20472e9baf9SAmit Kucheria 		if (ret < 0)
20572e9baf9SAmit Kucheria 			return ret;
20672e9baf9SAmit Kucheria 	} else {
20772e9baf9SAmit Kucheria 		ret = iio_read_channel_processed(chip->adc, &mili_celsius);
20872e9baf9SAmit Kucheria 		if (ret < 0)
20972e9baf9SAmit Kucheria 			return ret;
21072e9baf9SAmit Kucheria 
21172e9baf9SAmit Kucheria 		chip->temp = mili_celsius;
21272e9baf9SAmit Kucheria 	}
21372e9baf9SAmit Kucheria 
2140ffdab6fSVeera Vegivada 	*temp = chip->temp;
21572e9baf9SAmit Kucheria 
21672e9baf9SAmit Kucheria 	return 0;
21772e9baf9SAmit Kucheria }
21872e9baf9SAmit Kucheria 
qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip * chip,int temp)21972e9baf9SAmit Kucheria static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip,
22072e9baf9SAmit Kucheria 					     int temp)
22172e9baf9SAmit Kucheria {
222aa92b331SDavid Collins 	long stage2_threshold_min = (*chip->temp_map)[THRESH_MIN][1];
223aa92b331SDavid Collins 	long stage2_threshold_max = (*chip->temp_map)[THRESH_MAX][1];
22472e9baf9SAmit Kucheria 	bool disable_s2_shutdown = false;
225aa92b331SDavid Collins 	u8 reg;
22672e9baf9SAmit Kucheria 
22772e9baf9SAmit Kucheria 	WARN_ON(!mutex_is_locked(&chip->lock));
22872e9baf9SAmit Kucheria 
22972e9baf9SAmit Kucheria 	/*
23072e9baf9SAmit Kucheria 	 * Default: S2 and S3 shutdown enabled, thresholds at
231aa92b331SDavid Collins 	 * lowest threshold set, monitoring at 25Hz
23272e9baf9SAmit Kucheria 	 */
23372e9baf9SAmit Kucheria 	reg = SHUTDOWN_CTRL1_RATE_25HZ;
23472e9baf9SAmit Kucheria 
23572e9baf9SAmit Kucheria 	if (temp == THERMAL_TEMP_INVALID ||
236aa92b331SDavid Collins 	    temp < stage2_threshold_min) {
23772e9baf9SAmit Kucheria 		chip->thresh = THRESH_MIN;
23872e9baf9SAmit Kucheria 		goto skip;
23972e9baf9SAmit Kucheria 	}
24072e9baf9SAmit Kucheria 
241aa92b331SDavid Collins 	if (temp <= stage2_threshold_max) {
24272e9baf9SAmit Kucheria 		chip->thresh = THRESH_MAX -
243aa92b331SDavid Collins 			((stage2_threshold_max - temp) /
24472e9baf9SAmit Kucheria 			 TEMP_THRESH_STEP);
24572e9baf9SAmit Kucheria 		disable_s2_shutdown = true;
24672e9baf9SAmit Kucheria 	} else {
24772e9baf9SAmit Kucheria 		chip->thresh = THRESH_MAX;
24872e9baf9SAmit Kucheria 
24972e9baf9SAmit Kucheria 		if (chip->adc)
25072e9baf9SAmit Kucheria 			disable_s2_shutdown = true;
25172e9baf9SAmit Kucheria 		else
25272e9baf9SAmit Kucheria 			dev_warn(chip->dev,
2538763f8acSLuca Weiss 				 "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",
2548763f8acSLuca Weiss 				 temp, stage2_threshold_max, stage2_threshold_max);
25572e9baf9SAmit Kucheria 	}
25672e9baf9SAmit Kucheria 
25772e9baf9SAmit Kucheria skip:
25872e9baf9SAmit Kucheria 	reg |= chip->thresh;
25972e9baf9SAmit Kucheria 	if (disable_s2_shutdown)
26072e9baf9SAmit Kucheria 		reg |= SHUTDOWN_CTRL1_OVERRIDE_S2;
26172e9baf9SAmit Kucheria 
26272e9baf9SAmit Kucheria 	return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
26372e9baf9SAmit Kucheria }
26472e9baf9SAmit Kucheria 
qpnp_tm_set_trip_temp(struct thermal_zone_device * tz,int trip_id,int temp)2651fa86b0aSDaniel Lezcano static int qpnp_tm_set_trip_temp(struct thermal_zone_device *tz, int trip_id, int temp)
26672e9baf9SAmit Kucheria {
2675f68d078SDaniel Lezcano 	struct qpnp_tm_chip *chip = thermal_zone_device_priv(tz);
2681fa86b0aSDaniel Lezcano 	struct thermal_trip trip;
26972e9baf9SAmit Kucheria 	int ret;
27072e9baf9SAmit Kucheria 
271e6ec64f8SJohan Hovold 	ret = __thermal_zone_get_trip(chip->tz_dev, trip_id, &trip);
2721fa86b0aSDaniel Lezcano 	if (ret)
2731fa86b0aSDaniel Lezcano 		return ret;
27472e9baf9SAmit Kucheria 
2751fa86b0aSDaniel Lezcano 	if (trip.type != THERMAL_TRIP_CRITICAL)
27672e9baf9SAmit Kucheria 		return 0;
27772e9baf9SAmit Kucheria 
27872e9baf9SAmit Kucheria 	mutex_lock(&chip->lock);
27972e9baf9SAmit Kucheria 	ret = qpnp_tm_update_critical_trip_temp(chip, temp);
28072e9baf9SAmit Kucheria 	mutex_unlock(&chip->lock);
28172e9baf9SAmit Kucheria 
28272e9baf9SAmit Kucheria 	return ret;
28372e9baf9SAmit Kucheria }
28472e9baf9SAmit Kucheria 
285ca1b9a9eSDaniel Lezcano static const struct thermal_zone_device_ops qpnp_tm_sensor_ops = {
28672e9baf9SAmit Kucheria 	.get_temp = qpnp_tm_get_temp,
28772e9baf9SAmit Kucheria 	.set_trip_temp = qpnp_tm_set_trip_temp,
28872e9baf9SAmit Kucheria };
28972e9baf9SAmit Kucheria 
qpnp_tm_isr(int irq,void * data)29072e9baf9SAmit Kucheria static irqreturn_t qpnp_tm_isr(int irq, void *data)
29172e9baf9SAmit Kucheria {
29272e9baf9SAmit Kucheria 	struct qpnp_tm_chip *chip = data;
29372e9baf9SAmit Kucheria 
29472e9baf9SAmit Kucheria 	thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
29572e9baf9SAmit Kucheria 
29672e9baf9SAmit Kucheria 	return IRQ_HANDLED;
29772e9baf9SAmit Kucheria }
29872e9baf9SAmit Kucheria 
qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip * chip)29972e9baf9SAmit Kucheria static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip)
30072e9baf9SAmit Kucheria {
3011fa86b0aSDaniel Lezcano 	struct thermal_trip trip;
3021fa86b0aSDaniel Lezcano 	int i, ret;
30372e9baf9SAmit Kucheria 
3041fa86b0aSDaniel Lezcano 	for (i = 0; i < thermal_zone_get_num_trips(chip->tz_dev); i++) {
30572e9baf9SAmit Kucheria 
3061fa86b0aSDaniel Lezcano 		ret = thermal_zone_get_trip(chip->tz_dev, i, &trip);
3071fa86b0aSDaniel Lezcano 		if (ret)
3081fa86b0aSDaniel Lezcano 			continue;
30972e9baf9SAmit Kucheria 
3101fa86b0aSDaniel Lezcano 		if (trip.type == THERMAL_TRIP_CRITICAL)
3111fa86b0aSDaniel Lezcano 			return trip.temperature;
31272e9baf9SAmit Kucheria 	}
31372e9baf9SAmit Kucheria 
31472e9baf9SAmit Kucheria 	return THERMAL_TEMP_INVALID;
31572e9baf9SAmit Kucheria }
31672e9baf9SAmit Kucheria 
31772e9baf9SAmit Kucheria /*
31872e9baf9SAmit Kucheria  * This function initializes the internal temp value based on only the
31972e9baf9SAmit Kucheria  * current thermal stage and threshold. Setup threshold control and
32072e9baf9SAmit Kucheria  * disable shutdown override.
32172e9baf9SAmit Kucheria  */
qpnp_tm_init(struct qpnp_tm_chip * chip)32272e9baf9SAmit Kucheria static int qpnp_tm_init(struct qpnp_tm_chip *chip)
32372e9baf9SAmit Kucheria {
32472e9baf9SAmit Kucheria 	unsigned int stage;
32572e9baf9SAmit Kucheria 	int ret;
32672e9baf9SAmit Kucheria 	u8 reg = 0;
32772e9baf9SAmit Kucheria 	int crit_temp;
32872e9baf9SAmit Kucheria 
32972e9baf9SAmit Kucheria 	mutex_lock(&chip->lock);
33072e9baf9SAmit Kucheria 
33172e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg);
33272e9baf9SAmit Kucheria 	if (ret < 0)
33372e9baf9SAmit Kucheria 		goto out;
33472e9baf9SAmit Kucheria 
33572e9baf9SAmit Kucheria 	chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
33672e9baf9SAmit Kucheria 	chip->temp = DEFAULT_TEMP;
33772e9baf9SAmit Kucheria 
33872e9baf9SAmit Kucheria 	ret = qpnp_tm_get_temp_stage(chip);
33972e9baf9SAmit Kucheria 	if (ret < 0)
34072e9baf9SAmit Kucheria 		goto out;
34172e9baf9SAmit Kucheria 	chip->stage = ret;
34272e9baf9SAmit Kucheria 
34372e9baf9SAmit Kucheria 	stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1
34472e9baf9SAmit Kucheria 		? chip->stage : alarm_state_map[chip->stage];
34572e9baf9SAmit Kucheria 
34672e9baf9SAmit Kucheria 	if (stage)
347aa92b331SDavid Collins 		chip->temp = qpnp_tm_decode_temp(chip, stage);
34872e9baf9SAmit Kucheria 
34959edcd91SJohan Hovold 	mutex_unlock(&chip->lock);
35059edcd91SJohan Hovold 
35172e9baf9SAmit Kucheria 	crit_temp = qpnp_tm_get_critical_trip_temp(chip);
35259edcd91SJohan Hovold 
35359edcd91SJohan Hovold 	mutex_lock(&chip->lock);
35459edcd91SJohan Hovold 
35572e9baf9SAmit Kucheria 	ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp);
35672e9baf9SAmit Kucheria 	if (ret < 0)
35772e9baf9SAmit Kucheria 		goto out;
35872e9baf9SAmit Kucheria 
35972e9baf9SAmit Kucheria 	/* Enable the thermal alarm PMIC module in always-on mode. */
36072e9baf9SAmit Kucheria 	reg = ALARM_CTRL_FORCE_ENABLE;
36172e9baf9SAmit Kucheria 	ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg);
36272e9baf9SAmit Kucheria 
36372e9baf9SAmit Kucheria 	chip->initialized = true;
36472e9baf9SAmit Kucheria 
36572e9baf9SAmit Kucheria out:
36672e9baf9SAmit Kucheria 	mutex_unlock(&chip->lock);
36772e9baf9SAmit Kucheria 	return ret;
36872e9baf9SAmit Kucheria }
36972e9baf9SAmit Kucheria 
qpnp_tm_probe(struct platform_device * pdev)37072e9baf9SAmit Kucheria static int qpnp_tm_probe(struct platform_device *pdev)
37172e9baf9SAmit Kucheria {
37272e9baf9SAmit Kucheria 	struct qpnp_tm_chip *chip;
37372e9baf9SAmit Kucheria 	struct device_node *node;
374aa92b331SDavid Collins 	u8 type, subtype, dig_major;
37572e9baf9SAmit Kucheria 	u32 res;
37672e9baf9SAmit Kucheria 	int ret, irq;
37772e9baf9SAmit Kucheria 
37872e9baf9SAmit Kucheria 	node = pdev->dev.of_node;
37972e9baf9SAmit Kucheria 
38072e9baf9SAmit Kucheria 	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
38172e9baf9SAmit Kucheria 	if (!chip)
38272e9baf9SAmit Kucheria 		return -ENOMEM;
38372e9baf9SAmit Kucheria 
38472e9baf9SAmit Kucheria 	dev_set_drvdata(&pdev->dev, chip);
38572e9baf9SAmit Kucheria 	chip->dev = &pdev->dev;
38672e9baf9SAmit Kucheria 
38772e9baf9SAmit Kucheria 	mutex_init(&chip->lock);
38872e9baf9SAmit Kucheria 
38972e9baf9SAmit Kucheria 	chip->map = dev_get_regmap(pdev->dev.parent, NULL);
39072e9baf9SAmit Kucheria 	if (!chip->map)
39172e9baf9SAmit Kucheria 		return -ENXIO;
39272e9baf9SAmit Kucheria 
39372e9baf9SAmit Kucheria 	ret = of_property_read_u32(node, "reg", &res);
39472e9baf9SAmit Kucheria 	if (ret < 0)
39572e9baf9SAmit Kucheria 		return ret;
39672e9baf9SAmit Kucheria 
39772e9baf9SAmit Kucheria 	irq = platform_get_irq(pdev, 0);
39872e9baf9SAmit Kucheria 	if (irq < 0)
39972e9baf9SAmit Kucheria 		return irq;
40072e9baf9SAmit Kucheria 
40172e9baf9SAmit Kucheria 	/* ADC based measurements are optional */
40272e9baf9SAmit Kucheria 	chip->adc = devm_iio_channel_get(&pdev->dev, "thermal");
40372e9baf9SAmit Kucheria 	if (IS_ERR(chip->adc)) {
40472e9baf9SAmit Kucheria 		ret = PTR_ERR(chip->adc);
40572e9baf9SAmit Kucheria 		chip->adc = NULL;
40672e9baf9SAmit Kucheria 		if (ret == -EPROBE_DEFER)
40772e9baf9SAmit Kucheria 			return ret;
40872e9baf9SAmit Kucheria 	}
40972e9baf9SAmit Kucheria 
41072e9baf9SAmit Kucheria 	chip->base = res;
41172e9baf9SAmit Kucheria 
41272e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type);
413*57c9eaa4SLuca Weiss 	if (ret < 0)
414*57c9eaa4SLuca Weiss 		return dev_err_probe(&pdev->dev, ret,
415*57c9eaa4SLuca Weiss 				     "could not read type\n");
41672e9baf9SAmit Kucheria 
41772e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
418*57c9eaa4SLuca Weiss 	if (ret < 0)
419*57c9eaa4SLuca Weiss 		return dev_err_probe(&pdev->dev, ret,
420*57c9eaa4SLuca Weiss 				     "could not read subtype\n");
42172e9baf9SAmit Kucheria 
422aa92b331SDavid Collins 	ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MAJOR, &dig_major);
423*57c9eaa4SLuca Weiss 	if (ret < 0)
424*57c9eaa4SLuca Weiss 		return dev_err_probe(&pdev->dev, ret,
425*57c9eaa4SLuca Weiss 				     "could not read dig_major\n");
426aa92b331SDavid Collins 
42772e9baf9SAmit Kucheria 	if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1
42872e9baf9SAmit Kucheria 				     && subtype != QPNP_TM_SUBTYPE_GEN2)) {
42972e9baf9SAmit Kucheria 		dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n",
43072e9baf9SAmit Kucheria 			type, subtype);
43172e9baf9SAmit Kucheria 		return -ENODEV;
43272e9baf9SAmit Kucheria 	}
43372e9baf9SAmit Kucheria 
43472e9baf9SAmit Kucheria 	chip->subtype = subtype;
435aa92b331SDavid Collins 	if (subtype == QPNP_TM_SUBTYPE_GEN2 && dig_major >= 1)
436aa92b331SDavid Collins 		chip->temp_map = &temp_map_gen2_v1;
437aa92b331SDavid Collins 	else
438aa92b331SDavid Collins 		chip->temp_map = &temp_map_gen1;
43972e9baf9SAmit Kucheria 
44072e9baf9SAmit Kucheria 	/*
44172e9baf9SAmit Kucheria 	 * Register the sensor before initializing the hardware to be able to
44272e9baf9SAmit Kucheria 	 * read the trip points. get_temp() returns the default temperature
44372e9baf9SAmit Kucheria 	 * before the hardware initialization is completed.
44472e9baf9SAmit Kucheria 	 */
445ca1b9a9eSDaniel Lezcano 	chip->tz_dev = devm_thermal_of_zone_register(
44672e9baf9SAmit Kucheria 		&pdev->dev, 0, chip, &qpnp_tm_sensor_ops);
447*57c9eaa4SLuca Weiss 	if (IS_ERR(chip->tz_dev))
448*57c9eaa4SLuca Weiss 		return dev_err_probe(&pdev->dev, PTR_ERR(chip->tz_dev),
449*57c9eaa4SLuca Weiss 				     "failed to register sensor\n");
45072e9baf9SAmit Kucheria 
45172e9baf9SAmit Kucheria 	ret = qpnp_tm_init(chip);
452*57c9eaa4SLuca Weiss 	if (ret < 0)
453*57c9eaa4SLuca Weiss 		return dev_err_probe(&pdev->dev, ret, "init failed\n");
45472e9baf9SAmit Kucheria 
4557adbbb3bSYangtao Li 	devm_thermal_add_hwmon_sysfs(&pdev->dev, chip->tz_dev);
456fb55b012SDmitry Baryshkov 
45772e9baf9SAmit Kucheria 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr,
45872e9baf9SAmit Kucheria 					IRQF_ONESHOT, node->name, chip);
45972e9baf9SAmit Kucheria 	if (ret < 0)
46072e9baf9SAmit Kucheria 		return ret;
46172e9baf9SAmit Kucheria 
46272e9baf9SAmit Kucheria 	thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
46372e9baf9SAmit Kucheria 
46472e9baf9SAmit Kucheria 	return 0;
46572e9baf9SAmit Kucheria }
46672e9baf9SAmit Kucheria 
46772e9baf9SAmit Kucheria static const struct of_device_id qpnp_tm_match_table[] = {
46872e9baf9SAmit Kucheria 	{ .compatible = "qcom,spmi-temp-alarm" },
46972e9baf9SAmit Kucheria 	{ }
47072e9baf9SAmit Kucheria };
47172e9baf9SAmit Kucheria MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
47272e9baf9SAmit Kucheria 
47372e9baf9SAmit Kucheria static struct platform_driver qpnp_tm_driver = {
47472e9baf9SAmit Kucheria 	.driver = {
47572e9baf9SAmit Kucheria 		.name = "spmi-temp-alarm",
47672e9baf9SAmit Kucheria 		.of_match_table = qpnp_tm_match_table,
47772e9baf9SAmit Kucheria 	},
47872e9baf9SAmit Kucheria 	.probe  = qpnp_tm_probe,
47972e9baf9SAmit Kucheria };
48072e9baf9SAmit Kucheria module_platform_driver(qpnp_tm_driver);
48172e9baf9SAmit Kucheria 
48272e9baf9SAmit Kucheria MODULE_ALIAS("platform:spmi-temp-alarm");
48372e9baf9SAmit Kucheria MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
48472e9baf9SAmit Kucheria MODULE_LICENSE("GPL v2");
485