1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2015, 2017, 2020, The Linux Foundation. All rights reserved.
6 #include <linux/bitops.h>
7 #include <linux/delay.h>
9 #include <linux/iio/consumer.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/thermal.h>
18 #include "../thermal_core.h"
19 #include "../thermal_hwmon.h"
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
28 #define QPNP_TM_TYPE 0x09
29 #define QPNP_TM_SUBTYPE_GEN1 0x08
30 #define QPNP_TM_SUBTYPE_GEN2 0x09
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
36 #define SHUTDOWN_CTRL1_OVERRIDE_S2 BIT(6)
37 #define SHUTDOWN_CTRL1_THRESHOLD_MASK GENMASK(1, 0)
39 #define SHUTDOWN_CTRL1_RATE_25HZ BIT(3)
41 #define ALARM_CTRL_FORCE_ENABLE BIT(7)
43 #define THRESH_COUNT 4
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 },
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 },
61 #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
66 #define TEMP_STAGE_HYSTERESIS 2000
68 /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
69 #define DEFAULT_TEMP 37000
74 struct thermal_zone_device *tz_dev;
79 unsigned int prev_stage;
81 /* protects .thresh, .stage and chip registers */
85 struct iio_channel *adc;
86 const long (*temp_map)[THRESH_COUNT][STAGE_COUNT];
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};
92 static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
97 ret = regmap_read(chip->map, chip->base + addr, &val);
105 static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
107 return regmap_write(chip->map, chip->base + addr, data);
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
116 * Return: temperature in mC
118 static long qpnp_tm_decode_temp(struct qpnp_tm_chip *chip, unsigned int stage)
120 if (!chip->temp_map || chip->thresh >= THRESH_COUNT || stage == 0 ||
124 return (*chip->temp_map)[chip->thresh][stage - 1];
128 * qpnp_tm_get_temp_stage() - return over-temperature stage
129 * @chip: Pointer to the qpnp_tm chip
131 * Return: stage (GEN1) or state (GEN2) on success, or errno on failure.
133 static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip)
138 ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, ®);
142 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
143 ret = reg & STATUS_GEN1_STAGE_MASK;
145 ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT;
151 * This function updates the internal temp value based on the
152 * current thermal stage and threshold as well as the previous stage
154 static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
156 unsigned int stage, stage_new, stage_old;
159 WARN_ON(!mutex_is_locked(&chip->lock));
161 ret = qpnp_tm_get_temp_stage(chip);
166 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
168 stage_old = chip->stage;
170 stage_new = alarm_state_map[stage];
171 stage_old = alarm_state_map[chip->stage];
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;
189 static int qpnp_tm_get_temp(struct thermal_zone_device *tz, int *temp)
191 struct qpnp_tm_chip *chip = tz->devdata;
192 int ret, mili_celsius;
197 if (!chip->initialized) {
198 *temp = DEFAULT_TEMP;
203 mutex_lock(&chip->lock);
204 ret = qpnp_tm_update_temp_no_adc(chip);
205 mutex_unlock(&chip->lock);
209 ret = iio_read_channel_processed(chip->adc, &mili_celsius);
213 chip->temp = mili_celsius;
221 static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip,
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;
229 WARN_ON(!mutex_is_locked(&chip->lock));
232 * Default: S2 and S3 shutdown enabled, thresholds at
233 * lowest threshold set, monitoring at 25Hz
235 reg = SHUTDOWN_CTRL1_RATE_25HZ;
237 if (temp == THERMAL_TEMP_INVALID ||
238 temp < stage2_threshold_min) {
239 chip->thresh = THRESH_MIN;
243 if (temp <= stage2_threshold_max) {
244 chip->thresh = THRESH_MAX -
245 ((stage2_threshold_max - temp) /
247 disable_s2_shutdown = true;
249 chip->thresh = THRESH_MAX;
252 disable_s2_shutdown = true;
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);
261 if (disable_s2_shutdown)
262 reg |= SHUTDOWN_CTRL1_OVERRIDE_S2;
264 return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
267 static int qpnp_tm_set_trip_temp(struct thermal_zone_device *tz, int trip_id, int temp)
269 struct qpnp_tm_chip *chip = tz->devdata;
270 struct thermal_trip trip;
273 ret = __thermal_zone_get_trip(chip->tz_dev, trip_id, &trip);
277 if (trip.type != THERMAL_TRIP_CRITICAL)
280 mutex_lock(&chip->lock);
281 ret = qpnp_tm_update_critical_trip_temp(chip, temp);
282 mutex_unlock(&chip->lock);
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,
292 static irqreturn_t qpnp_tm_isr(int irq, void *data)
294 struct qpnp_tm_chip *chip = data;
296 thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
301 static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip)
303 struct thermal_trip trip;
306 for (i = 0; i < thermal_zone_get_num_trips(chip->tz_dev); i++) {
308 ret = thermal_zone_get_trip(chip->tz_dev, i, &trip);
312 if (trip.type == THERMAL_TRIP_CRITICAL)
313 return trip.temperature;
316 return THERMAL_TEMP_INVALID;
320 * This function initializes the internal temp value based on only the
321 * current thermal stage and threshold. Setup threshold control and
322 * disable shutdown override.
324 static int qpnp_tm_init(struct qpnp_tm_chip *chip)
331 mutex_lock(&chip->lock);
333 ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, ®);
337 chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
338 chip->temp = DEFAULT_TEMP;
340 ret = qpnp_tm_get_temp_stage(chip);
345 stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1
346 ? chip->stage : alarm_state_map[chip->stage];
349 chip->temp = qpnp_tm_decode_temp(chip, stage);
351 mutex_unlock(&chip->lock);
353 crit_temp = qpnp_tm_get_critical_trip_temp(chip);
355 mutex_lock(&chip->lock);
357 ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp);
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);
365 chip->initialized = true;
368 mutex_unlock(&chip->lock);
372 static int qpnp_tm_probe(struct platform_device *pdev)
374 struct qpnp_tm_chip *chip;
375 struct device_node *node;
376 u8 type, subtype, dig_major;
380 node = pdev->dev.of_node;
382 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
386 dev_set_drvdata(&pdev->dev, chip);
387 chip->dev = &pdev->dev;
389 mutex_init(&chip->lock);
391 chip->map = dev_get_regmap(pdev->dev.parent, NULL);
395 ret = of_property_read_u32(node, "reg", &res);
399 irq = platform_get_irq(pdev, 0);
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);
408 if (ret == -EPROBE_DEFER)
414 ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type);
416 dev_err(&pdev->dev, "could not read type\n");
420 ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
422 dev_err(&pdev->dev, "could not read subtype\n");
426 ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MAJOR, &dig_major);
428 dev_err(&pdev->dev, "could not read dig_major\n");
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",
439 chip->subtype = subtype;
440 if (subtype == QPNP_TM_SUBTYPE_GEN2 && dig_major >= 1)
441 chip->temp_map = &temp_map_gen2_v1;
443 chip->temp_map = &temp_map_gen1;
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.
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);
457 ret = qpnp_tm_init(chip);
459 dev_err(&pdev->dev, "init failed\n");
463 if (devm_thermal_add_hwmon_sysfs(chip->tz_dev))
465 "Failed to add hwmon sysfs attributes\n");
467 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr,
468 IRQF_ONESHOT, node->name, chip);
472 thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
477 static const struct of_device_id qpnp_tm_match_table[] = {
478 { .compatible = "qcom,spmi-temp-alarm" },
481 MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
483 static struct platform_driver qpnp_tm_driver = {
485 .name = "spmi-temp-alarm",
486 .of_match_table = qpnp_tm_match_table,
488 .probe = qpnp_tm_probe,
490 module_platform_driver(qpnp_tm_driver);
492 MODULE_ALIAS("platform:spmi-temp-alarm");
493 MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
494 MODULE_LICENSE("GPL v2");