xref: /openbmc/linux/drivers/thermal/qcom/qcom-spmi-temp-alarm.c (revision 97fb5e8d9b57f10f294303c9a5d1bd033eded6bf)
1*97fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
272e9baf9SAmit Kucheria /*
372e9baf9SAmit Kucheria  * Copyright (c) 2011-2015, 2017, 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/of_device.h>
1472e9baf9SAmit Kucheria #include <linux/platform_device.h>
1572e9baf9SAmit Kucheria #include <linux/regmap.h>
1672e9baf9SAmit Kucheria #include <linux/thermal.h>
1772e9baf9SAmit Kucheria 
1872e9baf9SAmit Kucheria #include "../thermal_core.h"
1972e9baf9SAmit Kucheria 
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 
4172e9baf9SAmit Kucheria /*
4272e9baf9SAmit Kucheria  * Trip point values based on threshold control
4372e9baf9SAmit Kucheria  * 0 = {105 C, 125 C, 145 C}
4472e9baf9SAmit Kucheria  * 1 = {110 C, 130 C, 150 C}
4572e9baf9SAmit Kucheria  * 2 = {115 C, 135 C, 155 C}
4672e9baf9SAmit Kucheria  * 3 = {120 C, 140 C, 160 C}
4772e9baf9SAmit Kucheria */
4872e9baf9SAmit Kucheria #define TEMP_STAGE_STEP			20000	/* Stage step: 20.000 C */
4972e9baf9SAmit Kucheria #define TEMP_STAGE_HYSTERESIS		2000
5072e9baf9SAmit Kucheria 
5172e9baf9SAmit Kucheria #define TEMP_THRESH_MIN			105000	/* Threshold Min: 105 C */
5272e9baf9SAmit Kucheria #define TEMP_THRESH_STEP		5000	/* Threshold step: 5 C */
5372e9baf9SAmit Kucheria 
5472e9baf9SAmit Kucheria #define THRESH_MIN			0
5572e9baf9SAmit Kucheria #define THRESH_MAX			3
5672e9baf9SAmit Kucheria 
5772e9baf9SAmit Kucheria /* Stage 2 Threshold Min: 125 C */
5872e9baf9SAmit Kucheria #define STAGE2_THRESHOLD_MIN		125000
5972e9baf9SAmit Kucheria /* Stage 2 Threshold Max: 140 C */
6072e9baf9SAmit Kucheria #define STAGE2_THRESHOLD_MAX		140000
6172e9baf9SAmit Kucheria 
6272e9baf9SAmit Kucheria /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
6372e9baf9SAmit Kucheria #define DEFAULT_TEMP			37000
6472e9baf9SAmit Kucheria 
6572e9baf9SAmit Kucheria struct qpnp_tm_chip {
6672e9baf9SAmit Kucheria 	struct regmap			*map;
6772e9baf9SAmit Kucheria 	struct device			*dev;
6872e9baf9SAmit Kucheria 	struct thermal_zone_device	*tz_dev;
6972e9baf9SAmit Kucheria 	unsigned int			subtype;
7072e9baf9SAmit Kucheria 	long				temp;
7172e9baf9SAmit Kucheria 	unsigned int			thresh;
7272e9baf9SAmit Kucheria 	unsigned int			stage;
7372e9baf9SAmit Kucheria 	unsigned int			prev_stage;
7472e9baf9SAmit Kucheria 	unsigned int			base;
7572e9baf9SAmit Kucheria 	/* protects .thresh, .stage and chip registers */
7672e9baf9SAmit Kucheria 	struct mutex			lock;
7772e9baf9SAmit Kucheria 	bool				initialized;
7872e9baf9SAmit Kucheria 
7972e9baf9SAmit Kucheria 	struct iio_channel		*adc;
8072e9baf9SAmit Kucheria };
8172e9baf9SAmit Kucheria 
8272e9baf9SAmit Kucheria /* This array maps from GEN2 alarm state to GEN1 alarm stage */
8372e9baf9SAmit Kucheria static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3};
8472e9baf9SAmit Kucheria 
8572e9baf9SAmit Kucheria static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
8672e9baf9SAmit Kucheria {
8772e9baf9SAmit Kucheria 	unsigned int val;
8872e9baf9SAmit Kucheria 	int ret;
8972e9baf9SAmit Kucheria 
9072e9baf9SAmit Kucheria 	ret = regmap_read(chip->map, chip->base + addr, &val);
9172e9baf9SAmit Kucheria 	if (ret < 0)
9272e9baf9SAmit Kucheria 		return ret;
9372e9baf9SAmit Kucheria 
9472e9baf9SAmit Kucheria 	*data = val;
9572e9baf9SAmit Kucheria 	return 0;
9672e9baf9SAmit Kucheria }
9772e9baf9SAmit Kucheria 
9872e9baf9SAmit Kucheria static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
9972e9baf9SAmit Kucheria {
10072e9baf9SAmit Kucheria 	return regmap_write(chip->map, chip->base + addr, data);
10172e9baf9SAmit Kucheria }
10272e9baf9SAmit Kucheria 
10372e9baf9SAmit Kucheria /**
10472e9baf9SAmit Kucheria  * qpnp_tm_get_temp_stage() - return over-temperature stage
10572e9baf9SAmit Kucheria  * @chip:		Pointer to the qpnp_tm chip
10672e9baf9SAmit Kucheria  *
10772e9baf9SAmit Kucheria  * Return: stage (GEN1) or state (GEN2) on success, or errno on failure.
10872e9baf9SAmit Kucheria  */
10972e9baf9SAmit Kucheria static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip)
11072e9baf9SAmit Kucheria {
11172e9baf9SAmit Kucheria 	int ret;
11272e9baf9SAmit Kucheria 	u8 reg = 0;
11372e9baf9SAmit Kucheria 
11472e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg);
11572e9baf9SAmit Kucheria 	if (ret < 0)
11672e9baf9SAmit Kucheria 		return ret;
11772e9baf9SAmit Kucheria 
11872e9baf9SAmit Kucheria 	if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
11972e9baf9SAmit Kucheria 		ret = reg & STATUS_GEN1_STAGE_MASK;
12072e9baf9SAmit Kucheria 	else
12172e9baf9SAmit Kucheria 		ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT;
12272e9baf9SAmit Kucheria 
12372e9baf9SAmit Kucheria 	return ret;
12472e9baf9SAmit Kucheria }
12572e9baf9SAmit Kucheria 
12672e9baf9SAmit Kucheria /*
12772e9baf9SAmit Kucheria  * This function updates the internal temp value based on the
12872e9baf9SAmit Kucheria  * current thermal stage and threshold as well as the previous stage
12972e9baf9SAmit Kucheria  */
13072e9baf9SAmit Kucheria static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
13172e9baf9SAmit Kucheria {
13272e9baf9SAmit Kucheria 	unsigned int stage, stage_new, stage_old;
13372e9baf9SAmit Kucheria 	int ret;
13472e9baf9SAmit Kucheria 
13572e9baf9SAmit Kucheria 	WARN_ON(!mutex_is_locked(&chip->lock));
13672e9baf9SAmit Kucheria 
13772e9baf9SAmit Kucheria 	ret = qpnp_tm_get_temp_stage(chip);
13872e9baf9SAmit Kucheria 	if (ret < 0)
13972e9baf9SAmit Kucheria 		return ret;
14072e9baf9SAmit Kucheria 	stage = ret;
14172e9baf9SAmit Kucheria 
14272e9baf9SAmit Kucheria 	if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
14372e9baf9SAmit Kucheria 		stage_new = stage;
14472e9baf9SAmit Kucheria 		stage_old = chip->stage;
14572e9baf9SAmit Kucheria 	} else {
14672e9baf9SAmit Kucheria 		stage_new = alarm_state_map[stage];
14772e9baf9SAmit Kucheria 		stage_old = alarm_state_map[chip->stage];
14872e9baf9SAmit Kucheria 	}
14972e9baf9SAmit Kucheria 
15072e9baf9SAmit Kucheria 	if (stage_new > stage_old) {
15172e9baf9SAmit Kucheria 		/* increasing stage, use lower bound */
15272e9baf9SAmit Kucheria 		chip->temp = (stage_new - 1) * TEMP_STAGE_STEP +
15372e9baf9SAmit Kucheria 			     chip->thresh * TEMP_THRESH_STEP +
15472e9baf9SAmit Kucheria 			     TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
15572e9baf9SAmit Kucheria 	} else if (stage_new < stage_old) {
15672e9baf9SAmit Kucheria 		/* decreasing stage, use upper bound */
15772e9baf9SAmit Kucheria 		chip->temp = stage_new * TEMP_STAGE_STEP +
15872e9baf9SAmit Kucheria 			     chip->thresh * TEMP_THRESH_STEP -
15972e9baf9SAmit Kucheria 			     TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN;
16072e9baf9SAmit Kucheria 	}
16172e9baf9SAmit Kucheria 
16272e9baf9SAmit Kucheria 	chip->stage = stage;
16372e9baf9SAmit Kucheria 
16472e9baf9SAmit Kucheria 	return 0;
16572e9baf9SAmit Kucheria }
16672e9baf9SAmit Kucheria 
16772e9baf9SAmit Kucheria static int qpnp_tm_get_temp(void *data, int *temp)
16872e9baf9SAmit Kucheria {
16972e9baf9SAmit Kucheria 	struct qpnp_tm_chip *chip = data;
17072e9baf9SAmit Kucheria 	int ret, mili_celsius;
17172e9baf9SAmit Kucheria 
17272e9baf9SAmit Kucheria 	if (!temp)
17372e9baf9SAmit Kucheria 		return -EINVAL;
17472e9baf9SAmit Kucheria 
17572e9baf9SAmit Kucheria 	if (!chip->initialized) {
17672e9baf9SAmit Kucheria 		*temp = DEFAULT_TEMP;
17772e9baf9SAmit Kucheria 		return 0;
17872e9baf9SAmit Kucheria 	}
17972e9baf9SAmit Kucheria 
18072e9baf9SAmit Kucheria 	if (!chip->adc) {
18172e9baf9SAmit Kucheria 		mutex_lock(&chip->lock);
18272e9baf9SAmit Kucheria 		ret = qpnp_tm_update_temp_no_adc(chip);
18372e9baf9SAmit Kucheria 		mutex_unlock(&chip->lock);
18472e9baf9SAmit Kucheria 		if (ret < 0)
18572e9baf9SAmit Kucheria 			return ret;
18672e9baf9SAmit Kucheria 	} else {
18772e9baf9SAmit Kucheria 		ret = iio_read_channel_processed(chip->adc, &mili_celsius);
18872e9baf9SAmit Kucheria 		if (ret < 0)
18972e9baf9SAmit Kucheria 			return ret;
19072e9baf9SAmit Kucheria 
19172e9baf9SAmit Kucheria 		chip->temp = mili_celsius;
19272e9baf9SAmit Kucheria 	}
19372e9baf9SAmit Kucheria 
19472e9baf9SAmit Kucheria 	*temp = chip->temp < 0 ? 0 : chip->temp;
19572e9baf9SAmit Kucheria 
19672e9baf9SAmit Kucheria 	return 0;
19772e9baf9SAmit Kucheria }
19872e9baf9SAmit Kucheria 
19972e9baf9SAmit Kucheria static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip,
20072e9baf9SAmit Kucheria 					     int temp)
20172e9baf9SAmit Kucheria {
20272e9baf9SAmit Kucheria 	u8 reg;
20372e9baf9SAmit Kucheria 	bool disable_s2_shutdown = false;
20472e9baf9SAmit Kucheria 
20572e9baf9SAmit Kucheria 	WARN_ON(!mutex_is_locked(&chip->lock));
20672e9baf9SAmit Kucheria 
20772e9baf9SAmit Kucheria 	/*
20872e9baf9SAmit Kucheria 	 * Default: S2 and S3 shutdown enabled, thresholds at
20972e9baf9SAmit Kucheria 	 * 105C/125C/145C, monitoring at 25Hz
21072e9baf9SAmit Kucheria 	 */
21172e9baf9SAmit Kucheria 	reg = SHUTDOWN_CTRL1_RATE_25HZ;
21272e9baf9SAmit Kucheria 
21372e9baf9SAmit Kucheria 	if (temp == THERMAL_TEMP_INVALID ||
21472e9baf9SAmit Kucheria 	    temp < STAGE2_THRESHOLD_MIN) {
21572e9baf9SAmit Kucheria 		chip->thresh = THRESH_MIN;
21672e9baf9SAmit Kucheria 		goto skip;
21772e9baf9SAmit Kucheria 	}
21872e9baf9SAmit Kucheria 
21972e9baf9SAmit Kucheria 	if (temp <= STAGE2_THRESHOLD_MAX) {
22072e9baf9SAmit Kucheria 		chip->thresh = THRESH_MAX -
22172e9baf9SAmit Kucheria 			((STAGE2_THRESHOLD_MAX - temp) /
22272e9baf9SAmit Kucheria 			 TEMP_THRESH_STEP);
22372e9baf9SAmit Kucheria 		disable_s2_shutdown = true;
22472e9baf9SAmit Kucheria 	} else {
22572e9baf9SAmit Kucheria 		chip->thresh = THRESH_MAX;
22672e9baf9SAmit Kucheria 
22772e9baf9SAmit Kucheria 		if (chip->adc)
22872e9baf9SAmit Kucheria 			disable_s2_shutdown = true;
22972e9baf9SAmit Kucheria 		else
23072e9baf9SAmit Kucheria 			dev_warn(chip->dev,
23172e9baf9SAmit Kucheria 				 "No ADC is configured and critical temperature is above the maximum stage 2 threshold of 140 C! Configuring stage 2 shutdown at 140 C.\n");
23272e9baf9SAmit Kucheria 	}
23372e9baf9SAmit Kucheria 
23472e9baf9SAmit Kucheria skip:
23572e9baf9SAmit Kucheria 	reg |= chip->thresh;
23672e9baf9SAmit Kucheria 	if (disable_s2_shutdown)
23772e9baf9SAmit Kucheria 		reg |= SHUTDOWN_CTRL1_OVERRIDE_S2;
23872e9baf9SAmit Kucheria 
23972e9baf9SAmit Kucheria 	return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
24072e9baf9SAmit Kucheria }
24172e9baf9SAmit Kucheria 
24272e9baf9SAmit Kucheria static int qpnp_tm_set_trip_temp(void *data, int trip, int temp)
24372e9baf9SAmit Kucheria {
24472e9baf9SAmit Kucheria 	struct qpnp_tm_chip *chip = data;
24572e9baf9SAmit Kucheria 	const struct thermal_trip *trip_points;
24672e9baf9SAmit Kucheria 	int ret;
24772e9baf9SAmit Kucheria 
24872e9baf9SAmit Kucheria 	trip_points = of_thermal_get_trip_points(chip->tz_dev);
24972e9baf9SAmit Kucheria 	if (!trip_points)
25072e9baf9SAmit Kucheria 		return -EINVAL;
25172e9baf9SAmit Kucheria 
25272e9baf9SAmit Kucheria 	if (trip_points[trip].type != THERMAL_TRIP_CRITICAL)
25372e9baf9SAmit Kucheria 		return 0;
25472e9baf9SAmit Kucheria 
25572e9baf9SAmit Kucheria 	mutex_lock(&chip->lock);
25672e9baf9SAmit Kucheria 	ret = qpnp_tm_update_critical_trip_temp(chip, temp);
25772e9baf9SAmit Kucheria 	mutex_unlock(&chip->lock);
25872e9baf9SAmit Kucheria 
25972e9baf9SAmit Kucheria 	return ret;
26072e9baf9SAmit Kucheria }
26172e9baf9SAmit Kucheria 
26272e9baf9SAmit Kucheria static const struct thermal_zone_of_device_ops qpnp_tm_sensor_ops = {
26372e9baf9SAmit Kucheria 	.get_temp = qpnp_tm_get_temp,
26472e9baf9SAmit Kucheria 	.set_trip_temp = qpnp_tm_set_trip_temp,
26572e9baf9SAmit Kucheria };
26672e9baf9SAmit Kucheria 
26772e9baf9SAmit Kucheria static irqreturn_t qpnp_tm_isr(int irq, void *data)
26872e9baf9SAmit Kucheria {
26972e9baf9SAmit Kucheria 	struct qpnp_tm_chip *chip = data;
27072e9baf9SAmit Kucheria 
27172e9baf9SAmit Kucheria 	thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
27272e9baf9SAmit Kucheria 
27372e9baf9SAmit Kucheria 	return IRQ_HANDLED;
27472e9baf9SAmit Kucheria }
27572e9baf9SAmit Kucheria 
27672e9baf9SAmit Kucheria static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip)
27772e9baf9SAmit Kucheria {
27872e9baf9SAmit Kucheria 	int ntrips;
27972e9baf9SAmit Kucheria 	const struct thermal_trip *trips;
28072e9baf9SAmit Kucheria 	int i;
28172e9baf9SAmit Kucheria 
28272e9baf9SAmit Kucheria 	ntrips = of_thermal_get_ntrips(chip->tz_dev);
28372e9baf9SAmit Kucheria 	if (ntrips <= 0)
28472e9baf9SAmit Kucheria 		return THERMAL_TEMP_INVALID;
28572e9baf9SAmit Kucheria 
28672e9baf9SAmit Kucheria 	trips = of_thermal_get_trip_points(chip->tz_dev);
28772e9baf9SAmit Kucheria 	if (!trips)
28872e9baf9SAmit Kucheria 		return THERMAL_TEMP_INVALID;
28972e9baf9SAmit Kucheria 
29072e9baf9SAmit Kucheria 	for (i = 0; i < ntrips; i++) {
29172e9baf9SAmit Kucheria 		if (of_thermal_is_trip_valid(chip->tz_dev, i) &&
29272e9baf9SAmit Kucheria 		    trips[i].type == THERMAL_TRIP_CRITICAL)
29372e9baf9SAmit Kucheria 			return trips[i].temperature;
29472e9baf9SAmit Kucheria 	}
29572e9baf9SAmit Kucheria 
29672e9baf9SAmit Kucheria 	return THERMAL_TEMP_INVALID;
29772e9baf9SAmit Kucheria }
29872e9baf9SAmit Kucheria 
29972e9baf9SAmit Kucheria /*
30072e9baf9SAmit Kucheria  * This function initializes the internal temp value based on only the
30172e9baf9SAmit Kucheria  * current thermal stage and threshold. Setup threshold control and
30272e9baf9SAmit Kucheria  * disable shutdown override.
30372e9baf9SAmit Kucheria  */
30472e9baf9SAmit Kucheria static int qpnp_tm_init(struct qpnp_tm_chip *chip)
30572e9baf9SAmit Kucheria {
30672e9baf9SAmit Kucheria 	unsigned int stage;
30772e9baf9SAmit Kucheria 	int ret;
30872e9baf9SAmit Kucheria 	u8 reg = 0;
30972e9baf9SAmit Kucheria 	int crit_temp;
31072e9baf9SAmit Kucheria 
31172e9baf9SAmit Kucheria 	mutex_lock(&chip->lock);
31272e9baf9SAmit Kucheria 
31372e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg);
31472e9baf9SAmit Kucheria 	if (ret < 0)
31572e9baf9SAmit Kucheria 		goto out;
31672e9baf9SAmit Kucheria 
31772e9baf9SAmit Kucheria 	chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
31872e9baf9SAmit Kucheria 	chip->temp = DEFAULT_TEMP;
31972e9baf9SAmit Kucheria 
32072e9baf9SAmit Kucheria 	ret = qpnp_tm_get_temp_stage(chip);
32172e9baf9SAmit Kucheria 	if (ret < 0)
32272e9baf9SAmit Kucheria 		goto out;
32372e9baf9SAmit Kucheria 	chip->stage = ret;
32472e9baf9SAmit Kucheria 
32572e9baf9SAmit Kucheria 	stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1
32672e9baf9SAmit Kucheria 		? chip->stage : alarm_state_map[chip->stage];
32772e9baf9SAmit Kucheria 
32872e9baf9SAmit Kucheria 	if (stage)
32972e9baf9SAmit Kucheria 		chip->temp = chip->thresh * TEMP_THRESH_STEP +
33072e9baf9SAmit Kucheria 			     (stage - 1) * TEMP_STAGE_STEP +
33172e9baf9SAmit Kucheria 			     TEMP_THRESH_MIN;
33272e9baf9SAmit Kucheria 
33372e9baf9SAmit Kucheria 	crit_temp = qpnp_tm_get_critical_trip_temp(chip);
33472e9baf9SAmit Kucheria 	ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp);
33572e9baf9SAmit Kucheria 	if (ret < 0)
33672e9baf9SAmit Kucheria 		goto out;
33772e9baf9SAmit Kucheria 
33872e9baf9SAmit Kucheria 	/* Enable the thermal alarm PMIC module in always-on mode. */
33972e9baf9SAmit Kucheria 	reg = ALARM_CTRL_FORCE_ENABLE;
34072e9baf9SAmit Kucheria 	ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg);
34172e9baf9SAmit Kucheria 
34272e9baf9SAmit Kucheria 	chip->initialized = true;
34372e9baf9SAmit Kucheria 
34472e9baf9SAmit Kucheria out:
34572e9baf9SAmit Kucheria 	mutex_unlock(&chip->lock);
34672e9baf9SAmit Kucheria 	return ret;
34772e9baf9SAmit Kucheria }
34872e9baf9SAmit Kucheria 
34972e9baf9SAmit Kucheria static int qpnp_tm_probe(struct platform_device *pdev)
35072e9baf9SAmit Kucheria {
35172e9baf9SAmit Kucheria 	struct qpnp_tm_chip *chip;
35272e9baf9SAmit Kucheria 	struct device_node *node;
35372e9baf9SAmit Kucheria 	u8 type, subtype;
35472e9baf9SAmit Kucheria 	u32 res;
35572e9baf9SAmit Kucheria 	int ret, irq;
35672e9baf9SAmit Kucheria 
35772e9baf9SAmit Kucheria 	node = pdev->dev.of_node;
35872e9baf9SAmit Kucheria 
35972e9baf9SAmit Kucheria 	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
36072e9baf9SAmit Kucheria 	if (!chip)
36172e9baf9SAmit Kucheria 		return -ENOMEM;
36272e9baf9SAmit Kucheria 
36372e9baf9SAmit Kucheria 	dev_set_drvdata(&pdev->dev, chip);
36472e9baf9SAmit Kucheria 	chip->dev = &pdev->dev;
36572e9baf9SAmit Kucheria 
36672e9baf9SAmit Kucheria 	mutex_init(&chip->lock);
36772e9baf9SAmit Kucheria 
36872e9baf9SAmit Kucheria 	chip->map = dev_get_regmap(pdev->dev.parent, NULL);
36972e9baf9SAmit Kucheria 	if (!chip->map)
37072e9baf9SAmit Kucheria 		return -ENXIO;
37172e9baf9SAmit Kucheria 
37272e9baf9SAmit Kucheria 	ret = of_property_read_u32(node, "reg", &res);
37372e9baf9SAmit Kucheria 	if (ret < 0)
37472e9baf9SAmit Kucheria 		return ret;
37572e9baf9SAmit Kucheria 
37672e9baf9SAmit Kucheria 	irq = platform_get_irq(pdev, 0);
37772e9baf9SAmit Kucheria 	if (irq < 0)
37872e9baf9SAmit Kucheria 		return irq;
37972e9baf9SAmit Kucheria 
38072e9baf9SAmit Kucheria 	/* ADC based measurements are optional */
38172e9baf9SAmit Kucheria 	chip->adc = devm_iio_channel_get(&pdev->dev, "thermal");
38272e9baf9SAmit Kucheria 	if (IS_ERR(chip->adc)) {
38372e9baf9SAmit Kucheria 		ret = PTR_ERR(chip->adc);
38472e9baf9SAmit Kucheria 		chip->adc = NULL;
38572e9baf9SAmit Kucheria 		if (ret == -EPROBE_DEFER)
38672e9baf9SAmit Kucheria 			return ret;
38772e9baf9SAmit Kucheria 	}
38872e9baf9SAmit Kucheria 
38972e9baf9SAmit Kucheria 	chip->base = res;
39072e9baf9SAmit Kucheria 
39172e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type);
39272e9baf9SAmit Kucheria 	if (ret < 0) {
39372e9baf9SAmit Kucheria 		dev_err(&pdev->dev, "could not read type\n");
39472e9baf9SAmit Kucheria 		return ret;
39572e9baf9SAmit Kucheria 	}
39672e9baf9SAmit Kucheria 
39772e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
39872e9baf9SAmit Kucheria 	if (ret < 0) {
39972e9baf9SAmit Kucheria 		dev_err(&pdev->dev, "could not read subtype\n");
40072e9baf9SAmit Kucheria 		return ret;
40172e9baf9SAmit Kucheria 	}
40272e9baf9SAmit Kucheria 
40372e9baf9SAmit Kucheria 	if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1
40472e9baf9SAmit Kucheria 				     && subtype != QPNP_TM_SUBTYPE_GEN2)) {
40572e9baf9SAmit Kucheria 		dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n",
40672e9baf9SAmit Kucheria 			type, subtype);
40772e9baf9SAmit Kucheria 		return -ENODEV;
40872e9baf9SAmit Kucheria 	}
40972e9baf9SAmit Kucheria 
41072e9baf9SAmit Kucheria 	chip->subtype = subtype;
41172e9baf9SAmit Kucheria 
41272e9baf9SAmit Kucheria 	/*
41372e9baf9SAmit Kucheria 	 * Register the sensor before initializing the hardware to be able to
41472e9baf9SAmit Kucheria 	 * read the trip points. get_temp() returns the default temperature
41572e9baf9SAmit Kucheria 	 * before the hardware initialization is completed.
41672e9baf9SAmit Kucheria 	 */
41772e9baf9SAmit Kucheria 	chip->tz_dev = devm_thermal_zone_of_sensor_register(
41872e9baf9SAmit Kucheria 		&pdev->dev, 0, chip, &qpnp_tm_sensor_ops);
41972e9baf9SAmit Kucheria 	if (IS_ERR(chip->tz_dev)) {
42072e9baf9SAmit Kucheria 		dev_err(&pdev->dev, "failed to register sensor\n");
42172e9baf9SAmit Kucheria 		return PTR_ERR(chip->tz_dev);
42272e9baf9SAmit Kucheria 	}
42372e9baf9SAmit Kucheria 
42472e9baf9SAmit Kucheria 	ret = qpnp_tm_init(chip);
42572e9baf9SAmit Kucheria 	if (ret < 0) {
42672e9baf9SAmit Kucheria 		dev_err(&pdev->dev, "init failed\n");
42772e9baf9SAmit Kucheria 		return ret;
42872e9baf9SAmit Kucheria 	}
42972e9baf9SAmit Kucheria 
43072e9baf9SAmit Kucheria 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr,
43172e9baf9SAmit Kucheria 					IRQF_ONESHOT, node->name, chip);
43272e9baf9SAmit Kucheria 	if (ret < 0)
43372e9baf9SAmit Kucheria 		return ret;
43472e9baf9SAmit Kucheria 
43572e9baf9SAmit Kucheria 	thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
43672e9baf9SAmit Kucheria 
43772e9baf9SAmit Kucheria 	return 0;
43872e9baf9SAmit Kucheria }
43972e9baf9SAmit Kucheria 
44072e9baf9SAmit Kucheria static const struct of_device_id qpnp_tm_match_table[] = {
44172e9baf9SAmit Kucheria 	{ .compatible = "qcom,spmi-temp-alarm" },
44272e9baf9SAmit Kucheria 	{ }
44372e9baf9SAmit Kucheria };
44472e9baf9SAmit Kucheria MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
44572e9baf9SAmit Kucheria 
44672e9baf9SAmit Kucheria static struct platform_driver qpnp_tm_driver = {
44772e9baf9SAmit Kucheria 	.driver = {
44872e9baf9SAmit Kucheria 		.name = "spmi-temp-alarm",
44972e9baf9SAmit Kucheria 		.of_match_table = qpnp_tm_match_table,
45072e9baf9SAmit Kucheria 	},
45172e9baf9SAmit Kucheria 	.probe  = qpnp_tm_probe,
45272e9baf9SAmit Kucheria };
45372e9baf9SAmit Kucheria module_platform_driver(qpnp_tm_driver);
45472e9baf9SAmit Kucheria 
45572e9baf9SAmit Kucheria MODULE_ALIAS("platform:spmi-temp-alarm");
45672e9baf9SAmit Kucheria MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
45772e9baf9SAmit Kucheria MODULE_LICENSE("GPL v2");
458