1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2011-2015, 2017, 2020, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/bitops.h>
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/iio/consumer.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/thermal.h>
17 
18 #include "../thermal_core.h"
19 #include "../thermal_hwmon.h"
20 
21 #define QPNP_TM_REG_DIG_MAJOR		0x01
22 #define QPNP_TM_REG_TYPE		0x04
23 #define QPNP_TM_REG_SUBTYPE		0x05
24 #define QPNP_TM_REG_STATUS		0x08
25 #define QPNP_TM_REG_SHUTDOWN_CTRL1	0x40
26 #define QPNP_TM_REG_ALARM_CTRL		0x46
27 
28 #define QPNP_TM_TYPE			0x09
29 #define QPNP_TM_SUBTYPE_GEN1		0x08
30 #define QPNP_TM_SUBTYPE_GEN2		0x09
31 
32 #define STATUS_GEN1_STAGE_MASK		GENMASK(1, 0)
33 #define STATUS_GEN2_STATE_MASK		GENMASK(6, 4)
34 #define STATUS_GEN2_STATE_SHIFT		4
35 
36 #define SHUTDOWN_CTRL1_OVERRIDE_S2	BIT(6)
37 #define SHUTDOWN_CTRL1_THRESHOLD_MASK	GENMASK(1, 0)
38 
39 #define SHUTDOWN_CTRL1_RATE_25HZ	BIT(3)
40 
41 #define ALARM_CTRL_FORCE_ENABLE		BIT(7)
42 
43 #define THRESH_COUNT			4
44 #define STAGE_COUNT			3
45 
46 /* Over-temperature trip point values in mC */
47 static const long temp_map_gen1[THRESH_COUNT][STAGE_COUNT] = {
48 	{ 105000, 125000, 145000 },
49 	{ 110000, 130000, 150000 },
50 	{ 115000, 135000, 155000 },
51 	{ 120000, 140000, 160000 },
52 };
53 
54 static const long temp_map_gen2_v1[THRESH_COUNT][STAGE_COUNT] = {
55 	{  90000, 110000, 140000 },
56 	{  95000, 115000, 145000 },
57 	{ 100000, 120000, 150000 },
58 	{ 105000, 125000, 155000 },
59 };
60 
61 #define TEMP_THRESH_STEP		5000 /* Threshold step: 5 C */
62 
63 #define THRESH_MIN			0
64 #define THRESH_MAX			3
65 
66 #define TEMP_STAGE_HYSTERESIS		2000
67 
68 /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
69 #define DEFAULT_TEMP			37000
70 
71 struct qpnp_tm_chip {
72 	struct regmap			*map;
73 	struct device			*dev;
74 	struct thermal_zone_device	*tz_dev;
75 	unsigned int			subtype;
76 	long				temp;
77 	unsigned int			thresh;
78 	unsigned int			stage;
79 	unsigned int			prev_stage;
80 	unsigned int			base;
81 	/* protects .thresh, .stage and chip registers */
82 	struct mutex			lock;
83 	bool				initialized;
84 
85 	struct iio_channel		*adc;
86 	const long			(*temp_map)[THRESH_COUNT][STAGE_COUNT];
87 };
88 
89 /* This array maps from GEN2 alarm state to GEN1 alarm stage */
90 static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3};
91 
92 static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
93 {
94 	unsigned int val;
95 	int ret;
96 
97 	ret = regmap_read(chip->map, chip->base + addr, &val);
98 	if (ret < 0)
99 		return ret;
100 
101 	*data = val;
102 	return 0;
103 }
104 
105 static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
106 {
107 	return regmap_write(chip->map, chip->base + addr, data);
108 }
109 
110 /**
111  * qpnp_tm_decode_temp() - return temperature in mC corresponding to the
112  *		specified over-temperature stage
113  * @chip:		Pointer to the qpnp_tm chip
114  * @stage:		Over-temperature stage
115  *
116  * Return: temperature in mC
117  */
118 static long qpnp_tm_decode_temp(struct qpnp_tm_chip *chip, unsigned int stage)
119 {
120 	if (!chip->temp_map || chip->thresh >= THRESH_COUNT || stage == 0 ||
121 	    stage > STAGE_COUNT)
122 		return 0;
123 
124 	return (*chip->temp_map)[chip->thresh][stage - 1];
125 }
126 
127 /**
128  * qpnp_tm_get_temp_stage() - return over-temperature stage
129  * @chip:		Pointer to the qpnp_tm chip
130  *
131  * Return: stage (GEN1) or state (GEN2) on success, or errno on failure.
132  */
133 static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip)
134 {
135 	int ret;
136 	u8 reg = 0;
137 
138 	ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg);
139 	if (ret < 0)
140 		return ret;
141 
142 	if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
143 		ret = reg & STATUS_GEN1_STAGE_MASK;
144 	else
145 		ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT;
146 
147 	return ret;
148 }
149 
150 /*
151  * This function updates the internal temp value based on the
152  * current thermal stage and threshold as well as the previous stage
153  */
154 static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
155 {
156 	unsigned int stage, stage_new, stage_old;
157 	int ret;
158 
159 	WARN_ON(!mutex_is_locked(&chip->lock));
160 
161 	ret = qpnp_tm_get_temp_stage(chip);
162 	if (ret < 0)
163 		return ret;
164 	stage = ret;
165 
166 	if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
167 		stage_new = stage;
168 		stage_old = chip->stage;
169 	} else {
170 		stage_new = alarm_state_map[stage];
171 		stage_old = alarm_state_map[chip->stage];
172 	}
173 
174 	if (stage_new > stage_old) {
175 		/* increasing stage, use lower bound */
176 		chip->temp = qpnp_tm_decode_temp(chip, stage_new)
177 				+ TEMP_STAGE_HYSTERESIS;
178 	} else if (stage_new < stage_old) {
179 		/* decreasing stage, use upper bound */
180 		chip->temp = qpnp_tm_decode_temp(chip, stage_new + 1)
181 				- TEMP_STAGE_HYSTERESIS;
182 	}
183 
184 	chip->stage = stage;
185 
186 	return 0;
187 }
188 
189 static int qpnp_tm_get_temp(struct thermal_zone_device *tz, int *temp)
190 {
191 	struct qpnp_tm_chip *chip = tz->devdata;
192 	int ret, mili_celsius;
193 
194 	if (!temp)
195 		return -EINVAL;
196 
197 	if (!chip->initialized) {
198 		*temp = DEFAULT_TEMP;
199 		return 0;
200 	}
201 
202 	if (!chip->adc) {
203 		mutex_lock(&chip->lock);
204 		ret = qpnp_tm_update_temp_no_adc(chip);
205 		mutex_unlock(&chip->lock);
206 		if (ret < 0)
207 			return ret;
208 	} else {
209 		ret = iio_read_channel_processed(chip->adc, &mili_celsius);
210 		if (ret < 0)
211 			return ret;
212 
213 		chip->temp = mili_celsius;
214 	}
215 
216 	*temp = chip->temp;
217 
218 	return 0;
219 }
220 
221 static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip,
222 					     int temp)
223 {
224 	long stage2_threshold_min = (*chip->temp_map)[THRESH_MIN][1];
225 	long stage2_threshold_max = (*chip->temp_map)[THRESH_MAX][1];
226 	bool disable_s2_shutdown = false;
227 	u8 reg;
228 
229 	WARN_ON(!mutex_is_locked(&chip->lock));
230 
231 	/*
232 	 * Default: S2 and S3 shutdown enabled, thresholds at
233 	 * lowest threshold set, monitoring at 25Hz
234 	 */
235 	reg = SHUTDOWN_CTRL1_RATE_25HZ;
236 
237 	if (temp == THERMAL_TEMP_INVALID ||
238 	    temp < stage2_threshold_min) {
239 		chip->thresh = THRESH_MIN;
240 		goto skip;
241 	}
242 
243 	if (temp <= stage2_threshold_max) {
244 		chip->thresh = THRESH_MAX -
245 			((stage2_threshold_max - temp) /
246 			 TEMP_THRESH_STEP);
247 		disable_s2_shutdown = true;
248 	} else {
249 		chip->thresh = THRESH_MAX;
250 
251 		if (chip->adc)
252 			disable_s2_shutdown = true;
253 		else
254 			dev_warn(chip->dev,
255 				 "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",
256 				 temp, stage2_threshold_max, stage2_threshold_max);
257 	}
258 
259 skip:
260 	reg |= chip->thresh;
261 	if (disable_s2_shutdown)
262 		reg |= SHUTDOWN_CTRL1_OVERRIDE_S2;
263 
264 	return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
265 }
266 
267 static int qpnp_tm_set_trip_temp(struct thermal_zone_device *tz, int trip, int temp)
268 {
269 	struct qpnp_tm_chip *chip = tz->devdata;
270 	const struct thermal_trip *trip_points;
271 	int ret;
272 
273 	trip_points = of_thermal_get_trip_points(chip->tz_dev);
274 	if (!trip_points)
275 		return -EINVAL;
276 
277 	if (trip_points[trip].type != THERMAL_TRIP_CRITICAL)
278 		return 0;
279 
280 	mutex_lock(&chip->lock);
281 	ret = qpnp_tm_update_critical_trip_temp(chip, temp);
282 	mutex_unlock(&chip->lock);
283 
284 	return ret;
285 }
286 
287 static const struct thermal_zone_device_ops qpnp_tm_sensor_ops = {
288 	.get_temp = qpnp_tm_get_temp,
289 	.set_trip_temp = qpnp_tm_set_trip_temp,
290 };
291 
292 static irqreturn_t qpnp_tm_isr(int irq, void *data)
293 {
294 	struct qpnp_tm_chip *chip = data;
295 
296 	thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
297 
298 	return IRQ_HANDLED;
299 }
300 
301 static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip)
302 {
303 	int ntrips;
304 	const struct thermal_trip *trips;
305 	int i;
306 
307 	ntrips = of_thermal_get_ntrips(chip->tz_dev);
308 	if (ntrips <= 0)
309 		return THERMAL_TEMP_INVALID;
310 
311 	trips = of_thermal_get_trip_points(chip->tz_dev);
312 	if (!trips)
313 		return THERMAL_TEMP_INVALID;
314 
315 	for (i = 0; i < ntrips; i++) {
316 		if (of_thermal_is_trip_valid(chip->tz_dev, i) &&
317 		    trips[i].type == THERMAL_TRIP_CRITICAL)
318 			return trips[i].temperature;
319 	}
320 
321 	return THERMAL_TEMP_INVALID;
322 }
323 
324 /*
325  * This function initializes the internal temp value based on only the
326  * current thermal stage and threshold. Setup threshold control and
327  * disable shutdown override.
328  */
329 static int qpnp_tm_init(struct qpnp_tm_chip *chip)
330 {
331 	unsigned int stage;
332 	int ret;
333 	u8 reg = 0;
334 	int crit_temp;
335 
336 	mutex_lock(&chip->lock);
337 
338 	ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg);
339 	if (ret < 0)
340 		goto out;
341 
342 	chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
343 	chip->temp = DEFAULT_TEMP;
344 
345 	ret = qpnp_tm_get_temp_stage(chip);
346 	if (ret < 0)
347 		goto out;
348 	chip->stage = ret;
349 
350 	stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1
351 		? chip->stage : alarm_state_map[chip->stage];
352 
353 	if (stage)
354 		chip->temp = qpnp_tm_decode_temp(chip, stage);
355 
356 	crit_temp = qpnp_tm_get_critical_trip_temp(chip);
357 	ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp);
358 	if (ret < 0)
359 		goto out;
360 
361 	/* Enable the thermal alarm PMIC module in always-on mode. */
362 	reg = ALARM_CTRL_FORCE_ENABLE;
363 	ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg);
364 
365 	chip->initialized = true;
366 
367 out:
368 	mutex_unlock(&chip->lock);
369 	return ret;
370 }
371 
372 static int qpnp_tm_probe(struct platform_device *pdev)
373 {
374 	struct qpnp_tm_chip *chip;
375 	struct device_node *node;
376 	u8 type, subtype, dig_major;
377 	u32 res;
378 	int ret, irq;
379 
380 	node = pdev->dev.of_node;
381 
382 	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
383 	if (!chip)
384 		return -ENOMEM;
385 
386 	dev_set_drvdata(&pdev->dev, chip);
387 	chip->dev = &pdev->dev;
388 
389 	mutex_init(&chip->lock);
390 
391 	chip->map = dev_get_regmap(pdev->dev.parent, NULL);
392 	if (!chip->map)
393 		return -ENXIO;
394 
395 	ret = of_property_read_u32(node, "reg", &res);
396 	if (ret < 0)
397 		return ret;
398 
399 	irq = platform_get_irq(pdev, 0);
400 	if (irq < 0)
401 		return irq;
402 
403 	/* ADC based measurements are optional */
404 	chip->adc = devm_iio_channel_get(&pdev->dev, "thermal");
405 	if (IS_ERR(chip->adc)) {
406 		ret = PTR_ERR(chip->adc);
407 		chip->adc = NULL;
408 		if (ret == -EPROBE_DEFER)
409 			return ret;
410 	}
411 
412 	chip->base = res;
413 
414 	ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type);
415 	if (ret < 0) {
416 		dev_err(&pdev->dev, "could not read type\n");
417 		return ret;
418 	}
419 
420 	ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
421 	if (ret < 0) {
422 		dev_err(&pdev->dev, "could not read subtype\n");
423 		return ret;
424 	}
425 
426 	ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MAJOR, &dig_major);
427 	if (ret < 0) {
428 		dev_err(&pdev->dev, "could not read dig_major\n");
429 		return ret;
430 	}
431 
432 	if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1
433 				     && subtype != QPNP_TM_SUBTYPE_GEN2)) {
434 		dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n",
435 			type, subtype);
436 		return -ENODEV;
437 	}
438 
439 	chip->subtype = subtype;
440 	if (subtype == QPNP_TM_SUBTYPE_GEN2 && dig_major >= 1)
441 		chip->temp_map = &temp_map_gen2_v1;
442 	else
443 		chip->temp_map = &temp_map_gen1;
444 
445 	/*
446 	 * Register the sensor before initializing the hardware to be able to
447 	 * read the trip points. get_temp() returns the default temperature
448 	 * before the hardware initialization is completed.
449 	 */
450 	chip->tz_dev = devm_thermal_of_zone_register(
451 		&pdev->dev, 0, chip, &qpnp_tm_sensor_ops);
452 	if (IS_ERR(chip->tz_dev)) {
453 		dev_err(&pdev->dev, "failed to register sensor\n");
454 		return PTR_ERR(chip->tz_dev);
455 	}
456 
457 	ret = qpnp_tm_init(chip);
458 	if (ret < 0) {
459 		dev_err(&pdev->dev, "init failed\n");
460 		return ret;
461 	}
462 
463 	if (devm_thermal_add_hwmon_sysfs(chip->tz_dev))
464 		dev_warn(&pdev->dev,
465 			 "Failed to add hwmon sysfs attributes\n");
466 
467 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr,
468 					IRQF_ONESHOT, node->name, chip);
469 	if (ret < 0)
470 		return ret;
471 
472 	thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
473 
474 	return 0;
475 }
476 
477 static const struct of_device_id qpnp_tm_match_table[] = {
478 	{ .compatible = "qcom,spmi-temp-alarm" },
479 	{ }
480 };
481 MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
482 
483 static struct platform_driver qpnp_tm_driver = {
484 	.driver = {
485 		.name = "spmi-temp-alarm",
486 		.of_match_table = qpnp_tm_match_table,
487 	},
488 	.probe  = qpnp_tm_probe,
489 };
490 module_platform_driver(qpnp_tm_driver);
491 
492 MODULE_ALIAS("platform:spmi-temp-alarm");
493 MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
494 MODULE_LICENSE("GPL v2");
495