xref: /linux/drivers/thermal/qcom/qcom-spmi-temp-alarm.c (revision d22300518d875f78203e9afacb5aa0b0316da523)
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			base;
7872e9baf9SAmit Kucheria 	/* protects .thresh, .stage and chip registers */
7972e9baf9SAmit Kucheria 	struct mutex			lock;
8072e9baf9SAmit Kucheria 	bool				initialized;
8172e9baf9SAmit Kucheria 
8272e9baf9SAmit Kucheria 	struct iio_channel		*adc;
83aa92b331SDavid Collins 	const long			(*temp_map)[THRESH_COUNT][STAGE_COUNT];
8472e9baf9SAmit Kucheria };
8572e9baf9SAmit Kucheria 
8672e9baf9SAmit Kucheria /* This array maps from GEN2 alarm state to GEN1 alarm stage */
8772e9baf9SAmit Kucheria static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3};
8872e9baf9SAmit Kucheria 
qpnp_tm_read(struct qpnp_tm_chip * chip,u16 addr,u8 * data)8972e9baf9SAmit Kucheria static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
9072e9baf9SAmit Kucheria {
9172e9baf9SAmit Kucheria 	unsigned int val;
9272e9baf9SAmit Kucheria 	int ret;
9372e9baf9SAmit Kucheria 
9472e9baf9SAmit Kucheria 	ret = regmap_read(chip->map, chip->base + addr, &val);
9572e9baf9SAmit Kucheria 	if (ret < 0)
9672e9baf9SAmit Kucheria 		return ret;
9772e9baf9SAmit Kucheria 
9872e9baf9SAmit Kucheria 	*data = val;
9972e9baf9SAmit Kucheria 	return 0;
10072e9baf9SAmit Kucheria }
10172e9baf9SAmit Kucheria 
qpnp_tm_write(struct qpnp_tm_chip * chip,u16 addr,u8 data)10272e9baf9SAmit Kucheria static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
10372e9baf9SAmit Kucheria {
10472e9baf9SAmit Kucheria 	return regmap_write(chip->map, chip->base + addr, data);
10572e9baf9SAmit Kucheria }
10672e9baf9SAmit Kucheria 
10772e9baf9SAmit Kucheria /**
108aa92b331SDavid Collins  * qpnp_tm_decode_temp() - return temperature in mC corresponding to the
109aa92b331SDavid Collins  *		specified over-temperature stage
110aa92b331SDavid Collins  * @chip:		Pointer to the qpnp_tm chip
111aa92b331SDavid Collins  * @stage:		Over-temperature stage
112aa92b331SDavid Collins  *
113aa92b331SDavid Collins  * Return: temperature in mC
114aa92b331SDavid Collins  */
qpnp_tm_decode_temp(struct qpnp_tm_chip * chip,unsigned int stage)115aa92b331SDavid Collins static long qpnp_tm_decode_temp(struct qpnp_tm_chip *chip, unsigned int stage)
116aa92b331SDavid Collins {
117aa92b331SDavid Collins 	if (!chip->temp_map || chip->thresh >= THRESH_COUNT || stage == 0 ||
118aa92b331SDavid Collins 	    stage > STAGE_COUNT)
119aa92b331SDavid Collins 		return 0;
120aa92b331SDavid Collins 
121aa92b331SDavid Collins 	return (*chip->temp_map)[chip->thresh][stage - 1];
122aa92b331SDavid Collins }
123aa92b331SDavid Collins 
124aa92b331SDavid Collins /**
12572e9baf9SAmit Kucheria  * qpnp_tm_get_temp_stage() - return over-temperature stage
12672e9baf9SAmit Kucheria  * @chip:		Pointer to the qpnp_tm chip
12772e9baf9SAmit Kucheria  *
12872e9baf9SAmit Kucheria  * Return: stage (GEN1) or state (GEN2) on success, or errno on failure.
12972e9baf9SAmit Kucheria  */
qpnp_tm_get_temp_stage(struct qpnp_tm_chip * chip)13072e9baf9SAmit Kucheria static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip)
13172e9baf9SAmit Kucheria {
13272e9baf9SAmit Kucheria 	int ret;
13372e9baf9SAmit Kucheria 	u8 reg = 0;
13472e9baf9SAmit Kucheria 
13572e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg);
13672e9baf9SAmit Kucheria 	if (ret < 0)
13772e9baf9SAmit Kucheria 		return ret;
13872e9baf9SAmit Kucheria 
13972e9baf9SAmit Kucheria 	if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
14072e9baf9SAmit Kucheria 		ret = reg & STATUS_GEN1_STAGE_MASK;
14172e9baf9SAmit Kucheria 	else
14272e9baf9SAmit Kucheria 		ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT;
14372e9baf9SAmit Kucheria 
14472e9baf9SAmit Kucheria 	return ret;
14572e9baf9SAmit Kucheria }
14672e9baf9SAmit Kucheria 
14772e9baf9SAmit Kucheria /*
14872e9baf9SAmit Kucheria  * This function updates the internal temp value based on the
14972e9baf9SAmit Kucheria  * current thermal stage and threshold as well as the previous stage
15072e9baf9SAmit Kucheria  */
qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip * chip)15172e9baf9SAmit Kucheria static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
15272e9baf9SAmit Kucheria {
15372e9baf9SAmit Kucheria 	unsigned int stage, stage_new, stage_old;
15472e9baf9SAmit Kucheria 	int ret;
15572e9baf9SAmit Kucheria 
15672e9baf9SAmit Kucheria 	WARN_ON(!mutex_is_locked(&chip->lock));
15772e9baf9SAmit Kucheria 
15872e9baf9SAmit Kucheria 	ret = qpnp_tm_get_temp_stage(chip);
15972e9baf9SAmit Kucheria 	if (ret < 0)
16072e9baf9SAmit Kucheria 		return ret;
16172e9baf9SAmit Kucheria 	stage = ret;
16272e9baf9SAmit Kucheria 
16372e9baf9SAmit Kucheria 	if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
16472e9baf9SAmit Kucheria 		stage_new = stage;
16572e9baf9SAmit Kucheria 		stage_old = chip->stage;
16672e9baf9SAmit Kucheria 	} else {
16772e9baf9SAmit Kucheria 		stage_new = alarm_state_map[stage];
16872e9baf9SAmit Kucheria 		stage_old = alarm_state_map[chip->stage];
16972e9baf9SAmit Kucheria 	}
17072e9baf9SAmit Kucheria 
17172e9baf9SAmit Kucheria 	if (stage_new > stage_old) {
17272e9baf9SAmit Kucheria 		/* increasing stage, use lower bound */
173aa92b331SDavid Collins 		chip->temp = qpnp_tm_decode_temp(chip, stage_new)
174aa92b331SDavid Collins 				+ TEMP_STAGE_HYSTERESIS;
17572e9baf9SAmit Kucheria 	} else if (stage_new < stage_old) {
17672e9baf9SAmit Kucheria 		/* decreasing stage, use upper bound */
177aa92b331SDavid Collins 		chip->temp = qpnp_tm_decode_temp(chip, stage_new + 1)
178aa92b331SDavid Collins 				- TEMP_STAGE_HYSTERESIS;
17972e9baf9SAmit Kucheria 	}
18072e9baf9SAmit Kucheria 
18172e9baf9SAmit Kucheria 	chip->stage = stage;
18272e9baf9SAmit Kucheria 
18372e9baf9SAmit Kucheria 	return 0;
18472e9baf9SAmit Kucheria }
18572e9baf9SAmit Kucheria 
qpnp_tm_get_temp(struct thermal_zone_device * tz,int * temp)186ca1b9a9eSDaniel Lezcano static int qpnp_tm_get_temp(struct thermal_zone_device *tz, int *temp)
18772e9baf9SAmit Kucheria {
1885f68d078SDaniel Lezcano 	struct qpnp_tm_chip *chip = thermal_zone_device_priv(tz);
18972e9baf9SAmit Kucheria 	int ret, mili_celsius;
19072e9baf9SAmit Kucheria 
19172e9baf9SAmit Kucheria 	if (!temp)
19272e9baf9SAmit Kucheria 		return -EINVAL;
19372e9baf9SAmit Kucheria 
19472e9baf9SAmit Kucheria 	if (!chip->initialized) {
19572e9baf9SAmit Kucheria 		*temp = DEFAULT_TEMP;
19672e9baf9SAmit Kucheria 		return 0;
19772e9baf9SAmit Kucheria 	}
19872e9baf9SAmit Kucheria 
19972e9baf9SAmit Kucheria 	if (!chip->adc) {
20072e9baf9SAmit Kucheria 		mutex_lock(&chip->lock);
20172e9baf9SAmit Kucheria 		ret = qpnp_tm_update_temp_no_adc(chip);
20272e9baf9SAmit Kucheria 		mutex_unlock(&chip->lock);
20372e9baf9SAmit Kucheria 		if (ret < 0)
20472e9baf9SAmit Kucheria 			return ret;
20572e9baf9SAmit Kucheria 	} else {
20672e9baf9SAmit Kucheria 		ret = iio_read_channel_processed(chip->adc, &mili_celsius);
20772e9baf9SAmit Kucheria 		if (ret < 0)
20872e9baf9SAmit Kucheria 			return ret;
20972e9baf9SAmit Kucheria 
21072e9baf9SAmit Kucheria 		chip->temp = mili_celsius;
21172e9baf9SAmit Kucheria 	}
21272e9baf9SAmit Kucheria 
2130ffdab6fSVeera Vegivada 	*temp = chip->temp;
21472e9baf9SAmit Kucheria 
21572e9baf9SAmit Kucheria 	return 0;
21672e9baf9SAmit Kucheria }
21772e9baf9SAmit Kucheria 
qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip * chip,int temp)21872e9baf9SAmit Kucheria static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip,
21972e9baf9SAmit Kucheria 					     int temp)
22072e9baf9SAmit Kucheria {
221aa92b331SDavid Collins 	long stage2_threshold_min = (*chip->temp_map)[THRESH_MIN][1];
222aa92b331SDavid Collins 	long stage2_threshold_max = (*chip->temp_map)[THRESH_MAX][1];
22372e9baf9SAmit Kucheria 	bool disable_s2_shutdown = false;
224aa92b331SDavid Collins 	u8 reg;
22572e9baf9SAmit Kucheria 
22672e9baf9SAmit Kucheria 	WARN_ON(!mutex_is_locked(&chip->lock));
22772e9baf9SAmit Kucheria 
22872e9baf9SAmit Kucheria 	/*
22972e9baf9SAmit Kucheria 	 * Default: S2 and S3 shutdown enabled, thresholds at
230aa92b331SDavid Collins 	 * lowest threshold set, monitoring at 25Hz
23172e9baf9SAmit Kucheria 	 */
23272e9baf9SAmit Kucheria 	reg = SHUTDOWN_CTRL1_RATE_25HZ;
23372e9baf9SAmit Kucheria 
23472e9baf9SAmit Kucheria 	if (temp == THERMAL_TEMP_INVALID ||
235aa92b331SDavid Collins 	    temp < stage2_threshold_min) {
23672e9baf9SAmit Kucheria 		chip->thresh = THRESH_MIN;
23772e9baf9SAmit Kucheria 		goto skip;
23872e9baf9SAmit Kucheria 	}
23972e9baf9SAmit Kucheria 
240aa92b331SDavid Collins 	if (temp <= stage2_threshold_max) {
24172e9baf9SAmit Kucheria 		chip->thresh = THRESH_MAX -
242aa92b331SDavid Collins 			((stage2_threshold_max - temp) /
24372e9baf9SAmit Kucheria 			 TEMP_THRESH_STEP);
24472e9baf9SAmit Kucheria 		disable_s2_shutdown = true;
24572e9baf9SAmit Kucheria 	} else {
24672e9baf9SAmit Kucheria 		chip->thresh = THRESH_MAX;
24772e9baf9SAmit Kucheria 
24872e9baf9SAmit Kucheria 		if (chip->adc)
24972e9baf9SAmit Kucheria 			disable_s2_shutdown = true;
25072e9baf9SAmit Kucheria 		else
25172e9baf9SAmit Kucheria 			dev_warn(chip->dev,
2528763f8acSLuca 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",
2538763f8acSLuca Weiss 				 temp, stage2_threshold_max, stage2_threshold_max);
25472e9baf9SAmit Kucheria 	}
25572e9baf9SAmit Kucheria 
25672e9baf9SAmit Kucheria skip:
25772e9baf9SAmit Kucheria 	reg |= chip->thresh;
25872e9baf9SAmit Kucheria 	if (disable_s2_shutdown)
25972e9baf9SAmit Kucheria 		reg |= SHUTDOWN_CTRL1_OVERRIDE_S2;
26072e9baf9SAmit Kucheria 
26172e9baf9SAmit Kucheria 	return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
26272e9baf9SAmit Kucheria }
26372e9baf9SAmit Kucheria 
qpnp_tm_set_trip_temp(struct thermal_zone_device * tz,const struct thermal_trip * trip,int temp)2640728c810SRafael J. Wysocki static int qpnp_tm_set_trip_temp(struct thermal_zone_device *tz,
2650728c810SRafael J. Wysocki 				 const struct thermal_trip *trip, int temp)
26672e9baf9SAmit Kucheria {
2675f68d078SDaniel Lezcano 	struct qpnp_tm_chip *chip = thermal_zone_device_priv(tz);
26872e9baf9SAmit Kucheria 	int ret;
26972e9baf9SAmit Kucheria 
2700728c810SRafael J. Wysocki 	if (trip->type != THERMAL_TRIP_CRITICAL)
27172e9baf9SAmit Kucheria 		return 0;
27272e9baf9SAmit Kucheria 
27372e9baf9SAmit Kucheria 	mutex_lock(&chip->lock);
27472e9baf9SAmit Kucheria 	ret = qpnp_tm_update_critical_trip_temp(chip, temp);
27572e9baf9SAmit Kucheria 	mutex_unlock(&chip->lock);
27672e9baf9SAmit Kucheria 
27772e9baf9SAmit Kucheria 	return ret;
27872e9baf9SAmit Kucheria }
27972e9baf9SAmit Kucheria 
280ca1b9a9eSDaniel Lezcano static const struct thermal_zone_device_ops qpnp_tm_sensor_ops = {
28172e9baf9SAmit Kucheria 	.get_temp = qpnp_tm_get_temp,
28272e9baf9SAmit Kucheria 	.set_trip_temp = qpnp_tm_set_trip_temp,
28372e9baf9SAmit Kucheria };
28472e9baf9SAmit Kucheria 
qpnp_tm_isr(int irq,void * data)28572e9baf9SAmit Kucheria static irqreturn_t qpnp_tm_isr(int irq, void *data)
28672e9baf9SAmit Kucheria {
28772e9baf9SAmit Kucheria 	struct qpnp_tm_chip *chip = data;
28872e9baf9SAmit Kucheria 
28972e9baf9SAmit Kucheria 	thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
29072e9baf9SAmit Kucheria 
29172e9baf9SAmit Kucheria 	return IRQ_HANDLED;
29272e9baf9SAmit Kucheria }
29372e9baf9SAmit Kucheria 
29472e9baf9SAmit Kucheria /*
29572e9baf9SAmit Kucheria  * This function initializes the internal temp value based on only the
29672e9baf9SAmit Kucheria  * current thermal stage and threshold. Setup threshold control and
29772e9baf9SAmit Kucheria  * disable shutdown override.
29872e9baf9SAmit Kucheria  */
qpnp_tm_init(struct qpnp_tm_chip * chip)29972e9baf9SAmit Kucheria static int qpnp_tm_init(struct qpnp_tm_chip *chip)
30072e9baf9SAmit Kucheria {
30172e9baf9SAmit Kucheria 	unsigned int stage;
30272e9baf9SAmit Kucheria 	int ret;
30372e9baf9SAmit Kucheria 	u8 reg = 0;
30472e9baf9SAmit Kucheria 	int crit_temp;
30572e9baf9SAmit Kucheria 
30672e9baf9SAmit Kucheria 	mutex_lock(&chip->lock);
30772e9baf9SAmit Kucheria 
30872e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg);
30972e9baf9SAmit Kucheria 	if (ret < 0)
31072e9baf9SAmit Kucheria 		goto out;
31172e9baf9SAmit Kucheria 
31272e9baf9SAmit Kucheria 	chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
31372e9baf9SAmit Kucheria 	chip->temp = DEFAULT_TEMP;
31472e9baf9SAmit Kucheria 
31572e9baf9SAmit Kucheria 	ret = qpnp_tm_get_temp_stage(chip);
31672e9baf9SAmit Kucheria 	if (ret < 0)
31772e9baf9SAmit Kucheria 		goto out;
31872e9baf9SAmit Kucheria 	chip->stage = ret;
31972e9baf9SAmit Kucheria 
32072e9baf9SAmit Kucheria 	stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1
32172e9baf9SAmit Kucheria 		? chip->stage : alarm_state_map[chip->stage];
32272e9baf9SAmit Kucheria 
32372e9baf9SAmit Kucheria 	if (stage)
324aa92b331SDavid Collins 		chip->temp = qpnp_tm_decode_temp(chip, stage);
32572e9baf9SAmit Kucheria 
32659edcd91SJohan Hovold 	mutex_unlock(&chip->lock);
32759edcd91SJohan Hovold 
328*ab446887SRafael J. Wysocki 	ret = thermal_zone_get_crit_temp(chip->tz_dev, &crit_temp);
329*ab446887SRafael J. Wysocki 	if (ret)
330*ab446887SRafael J. Wysocki 		crit_temp = THERMAL_TEMP_INVALID;
33159edcd91SJohan Hovold 
33259edcd91SJohan Hovold 	mutex_lock(&chip->lock);
33359edcd91SJohan Hovold 
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 
qpnp_tm_probe(struct platform_device * pdev)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;
353aa92b331SDavid Collins 	u8 type, subtype, dig_major;
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);
39257c9eaa4SLuca Weiss 	if (ret < 0)
39357c9eaa4SLuca Weiss 		return dev_err_probe(&pdev->dev, ret,
39457c9eaa4SLuca Weiss 				     "could not read type\n");
39572e9baf9SAmit Kucheria 
39672e9baf9SAmit Kucheria 	ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
39757c9eaa4SLuca Weiss 	if (ret < 0)
39857c9eaa4SLuca Weiss 		return dev_err_probe(&pdev->dev, ret,
39957c9eaa4SLuca Weiss 				     "could not read subtype\n");
40072e9baf9SAmit Kucheria 
401aa92b331SDavid Collins 	ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MAJOR, &dig_major);
40257c9eaa4SLuca Weiss 	if (ret < 0)
40357c9eaa4SLuca Weiss 		return dev_err_probe(&pdev->dev, ret,
40457c9eaa4SLuca Weiss 				     "could not read dig_major\n");
405aa92b331SDavid Collins 
40672e9baf9SAmit Kucheria 	if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1
40772e9baf9SAmit Kucheria 				     && subtype != QPNP_TM_SUBTYPE_GEN2)) {
40872e9baf9SAmit Kucheria 		dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n",
40972e9baf9SAmit Kucheria 			type, subtype);
41072e9baf9SAmit Kucheria 		return -ENODEV;
41172e9baf9SAmit Kucheria 	}
41272e9baf9SAmit Kucheria 
41372e9baf9SAmit Kucheria 	chip->subtype = subtype;
414aa92b331SDavid Collins 	if (subtype == QPNP_TM_SUBTYPE_GEN2 && dig_major >= 1)
415aa92b331SDavid Collins 		chip->temp_map = &temp_map_gen2_v1;
416aa92b331SDavid Collins 	else
417aa92b331SDavid Collins 		chip->temp_map = &temp_map_gen1;
41872e9baf9SAmit Kucheria 
41972e9baf9SAmit Kucheria 	/*
42072e9baf9SAmit Kucheria 	 * Register the sensor before initializing the hardware to be able to
42172e9baf9SAmit Kucheria 	 * read the trip points. get_temp() returns the default temperature
42272e9baf9SAmit Kucheria 	 * before the hardware initialization is completed.
42372e9baf9SAmit Kucheria 	 */
424ca1b9a9eSDaniel Lezcano 	chip->tz_dev = devm_thermal_of_zone_register(
42572e9baf9SAmit Kucheria 		&pdev->dev, 0, chip, &qpnp_tm_sensor_ops);
42657c9eaa4SLuca Weiss 	if (IS_ERR(chip->tz_dev))
42757c9eaa4SLuca Weiss 		return dev_err_probe(&pdev->dev, PTR_ERR(chip->tz_dev),
42857c9eaa4SLuca Weiss 				     "failed to register sensor\n");
42972e9baf9SAmit Kucheria 
43072e9baf9SAmit Kucheria 	ret = qpnp_tm_init(chip);
43157c9eaa4SLuca Weiss 	if (ret < 0)
43257c9eaa4SLuca Weiss 		return dev_err_probe(&pdev->dev, ret, "init failed\n");
43372e9baf9SAmit Kucheria 
4347adbbb3bSYangtao Li 	devm_thermal_add_hwmon_sysfs(&pdev->dev, chip->tz_dev);
435fb55b012SDmitry Baryshkov 
43672e9baf9SAmit Kucheria 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr,
43772e9baf9SAmit Kucheria 					IRQF_ONESHOT, node->name, chip);
43872e9baf9SAmit Kucheria 	if (ret < 0)
43972e9baf9SAmit Kucheria 		return ret;
44072e9baf9SAmit Kucheria 
44172e9baf9SAmit Kucheria 	thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
44272e9baf9SAmit Kucheria 
44372e9baf9SAmit Kucheria 	return 0;
44472e9baf9SAmit Kucheria }
44572e9baf9SAmit Kucheria 
44672e9baf9SAmit Kucheria static const struct of_device_id qpnp_tm_match_table[] = {
44772e9baf9SAmit Kucheria 	{ .compatible = "qcom,spmi-temp-alarm" },
44872e9baf9SAmit Kucheria 	{ }
44972e9baf9SAmit Kucheria };
45072e9baf9SAmit Kucheria MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
45172e9baf9SAmit Kucheria 
45272e9baf9SAmit Kucheria static struct platform_driver qpnp_tm_driver = {
45372e9baf9SAmit Kucheria 	.driver = {
45472e9baf9SAmit Kucheria 		.name = "spmi-temp-alarm",
45572e9baf9SAmit Kucheria 		.of_match_table = qpnp_tm_match_table,
45672e9baf9SAmit Kucheria 	},
45772e9baf9SAmit Kucheria 	.probe  = qpnp_tm_probe,
45872e9baf9SAmit Kucheria };
45972e9baf9SAmit Kucheria module_platform_driver(qpnp_tm_driver);
46072e9baf9SAmit Kucheria 
46172e9baf9SAmit Kucheria MODULE_ALIAS("platform:spmi-temp-alarm");
46272e9baf9SAmit Kucheria MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
46372e9baf9SAmit Kucheria MODULE_LICENSE("GPL v2");
464