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/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/thermal.h>
17 #include "../thermal_hwmon.h"
19 #define QPNP_TM_REG_DIG_MAJOR 0x01
20 #define QPNP_TM_REG_TYPE 0x04
21 #define QPNP_TM_REG_SUBTYPE 0x05
22 #define QPNP_TM_REG_STATUS 0x08
23 #define QPNP_TM_REG_SHUTDOWN_CTRL1 0x40
24 #define QPNP_TM_REG_ALARM_CTRL 0x46
26 #define QPNP_TM_TYPE 0x09
27 #define QPNP_TM_SUBTYPE_GEN1 0x08
28 #define QPNP_TM_SUBTYPE_GEN2 0x09
30 #define STATUS_GEN1_STAGE_MASK GENMASK(1, 0)
31 #define STATUS_GEN2_STATE_MASK GENMASK(6, 4)
32 #define STATUS_GEN2_STATE_SHIFT 4
34 #define SHUTDOWN_CTRL1_OVERRIDE_S2 BIT(6)
35 #define SHUTDOWN_CTRL1_THRESHOLD_MASK GENMASK(1, 0)
37 #define SHUTDOWN_CTRL1_RATE_25HZ BIT(3)
39 #define ALARM_CTRL_FORCE_ENABLE BIT(7)
41 #define THRESH_COUNT 4
44 /* Over-temperature trip point values in mC */
45 static const long temp_map_gen1[THRESH_COUNT][STAGE_COUNT] = {
46 { 105000, 125000, 145000 },
47 { 110000, 130000, 150000 },
48 { 115000, 135000, 155000 },
49 { 120000, 140000, 160000 },
52 static const long temp_map_gen2_v1[THRESH_COUNT][STAGE_COUNT] = {
53 { 90000, 110000, 140000 },
54 { 95000, 115000, 145000 },
55 { 100000, 120000, 150000 },
56 { 105000, 125000, 155000 },
59 #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
64 #define TEMP_STAGE_HYSTERESIS 2000
66 /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
67 #define DEFAULT_TEMP 37000
72 struct thermal_zone_device *tz_dev;
77 unsigned int prev_stage;
79 /* protects .thresh, .stage and chip registers */
83 struct iio_channel *adc;
84 const long (*temp_map)[THRESH_COUNT][STAGE_COUNT];
87 /* This array maps from GEN2 alarm state to GEN1 alarm stage */
88 static const unsigned int alarm_state_map[8] = {0, 1, 1, 2, 2, 3, 3, 3};
90 static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data)
95 ret = regmap_read(chip->map, chip->base + addr, &val);
103 static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data)
105 return regmap_write(chip->map, chip->base + addr, data);
109 * qpnp_tm_decode_temp() - return temperature in mC corresponding to the
110 * specified over-temperature stage
111 * @chip: Pointer to the qpnp_tm chip
112 * @stage: Over-temperature stage
114 * Return: temperature in mC
116 static long qpnp_tm_decode_temp(struct qpnp_tm_chip *chip, unsigned int stage)
118 if (!chip->temp_map || chip->thresh >= THRESH_COUNT || stage == 0 ||
122 return (*chip->temp_map)[chip->thresh][stage - 1];
126 * qpnp_tm_get_temp_stage() - return over-temperature stage
127 * @chip: Pointer to the qpnp_tm chip
129 * Return: stage (GEN1) or state (GEN2) on success, or errno on failure.
131 static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip)
136 ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, ®);
140 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1)
141 ret = reg & STATUS_GEN1_STAGE_MASK;
143 ret = (reg & STATUS_GEN2_STATE_MASK) >> STATUS_GEN2_STATE_SHIFT;
149 * This function updates the internal temp value based on the
150 * current thermal stage and threshold as well as the previous stage
152 static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip)
154 unsigned int stage, stage_new, stage_old;
157 WARN_ON(!mutex_is_locked(&chip->lock));
159 ret = qpnp_tm_get_temp_stage(chip);
164 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) {
166 stage_old = chip->stage;
168 stage_new = alarm_state_map[stage];
169 stage_old = alarm_state_map[chip->stage];
172 if (stage_new > stage_old) {
173 /* increasing stage, use lower bound */
174 chip->temp = qpnp_tm_decode_temp(chip, stage_new)
175 + TEMP_STAGE_HYSTERESIS;
176 } else if (stage_new < stage_old) {
177 /* decreasing stage, use upper bound */
178 chip->temp = qpnp_tm_decode_temp(chip, stage_new + 1)
179 - TEMP_STAGE_HYSTERESIS;
187 static int qpnp_tm_get_temp(struct thermal_zone_device *tz, int *temp)
189 struct qpnp_tm_chip *chip = thermal_zone_device_priv(tz);
190 int ret, mili_celsius;
195 if (!chip->initialized) {
196 *temp = DEFAULT_TEMP;
201 mutex_lock(&chip->lock);
202 ret = qpnp_tm_update_temp_no_adc(chip);
203 mutex_unlock(&chip->lock);
207 ret = iio_read_channel_processed(chip->adc, &mili_celsius);
211 chip->temp = mili_celsius;
219 static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip,
222 long stage2_threshold_min = (*chip->temp_map)[THRESH_MIN][1];
223 long stage2_threshold_max = (*chip->temp_map)[THRESH_MAX][1];
224 bool disable_s2_shutdown = false;
227 WARN_ON(!mutex_is_locked(&chip->lock));
230 * Default: S2 and S3 shutdown enabled, thresholds at
231 * lowest threshold set, monitoring at 25Hz
233 reg = SHUTDOWN_CTRL1_RATE_25HZ;
235 if (temp == THERMAL_TEMP_INVALID ||
236 temp < stage2_threshold_min) {
237 chip->thresh = THRESH_MIN;
241 if (temp <= stage2_threshold_max) {
242 chip->thresh = THRESH_MAX -
243 ((stage2_threshold_max - temp) /
245 disable_s2_shutdown = true;
247 chip->thresh = THRESH_MAX;
250 disable_s2_shutdown = true;
253 "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",
254 temp, stage2_threshold_max, stage2_threshold_max);
259 if (disable_s2_shutdown)
260 reg |= SHUTDOWN_CTRL1_OVERRIDE_S2;
262 return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg);
265 static int qpnp_tm_set_trip_temp(struct thermal_zone_device *tz, int trip_id, int temp)
267 struct qpnp_tm_chip *chip = thermal_zone_device_priv(tz);
268 struct thermal_trip trip;
271 ret = __thermal_zone_get_trip(chip->tz_dev, trip_id, &trip);
275 if (trip.type != THERMAL_TRIP_CRITICAL)
278 mutex_lock(&chip->lock);
279 ret = qpnp_tm_update_critical_trip_temp(chip, temp);
280 mutex_unlock(&chip->lock);
285 static const struct thermal_zone_device_ops qpnp_tm_sensor_ops = {
286 .get_temp = qpnp_tm_get_temp,
287 .set_trip_temp = qpnp_tm_set_trip_temp,
290 static irqreturn_t qpnp_tm_isr(int irq, void *data)
292 struct qpnp_tm_chip *chip = data;
294 thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
299 static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip)
301 struct thermal_trip trip;
304 for (i = 0; i < thermal_zone_get_num_trips(chip->tz_dev); i++) {
306 ret = thermal_zone_get_trip(chip->tz_dev, i, &trip);
310 if (trip.type == THERMAL_TRIP_CRITICAL)
311 return trip.temperature;
314 return THERMAL_TEMP_INVALID;
318 * This function initializes the internal temp value based on only the
319 * current thermal stage and threshold. Setup threshold control and
320 * disable shutdown override.
322 static int qpnp_tm_init(struct qpnp_tm_chip *chip)
329 mutex_lock(&chip->lock);
331 ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, ®);
335 chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK;
336 chip->temp = DEFAULT_TEMP;
338 ret = qpnp_tm_get_temp_stage(chip);
343 stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1
344 ? chip->stage : alarm_state_map[chip->stage];
347 chip->temp = qpnp_tm_decode_temp(chip, stage);
349 mutex_unlock(&chip->lock);
351 crit_temp = qpnp_tm_get_critical_trip_temp(chip);
353 mutex_lock(&chip->lock);
355 ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp);
359 /* Enable the thermal alarm PMIC module in always-on mode. */
360 reg = ALARM_CTRL_FORCE_ENABLE;
361 ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg);
363 chip->initialized = true;
366 mutex_unlock(&chip->lock);
370 static int qpnp_tm_probe(struct platform_device *pdev)
372 struct qpnp_tm_chip *chip;
373 struct device_node *node;
374 u8 type, subtype, dig_major;
378 node = pdev->dev.of_node;
380 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
384 dev_set_drvdata(&pdev->dev, chip);
385 chip->dev = &pdev->dev;
387 mutex_init(&chip->lock);
389 chip->map = dev_get_regmap(pdev->dev.parent, NULL);
393 ret = of_property_read_u32(node, "reg", &res);
397 irq = platform_get_irq(pdev, 0);
401 /* ADC based measurements are optional */
402 chip->adc = devm_iio_channel_get(&pdev->dev, "thermal");
403 if (IS_ERR(chip->adc)) {
404 ret = PTR_ERR(chip->adc);
406 if (ret == -EPROBE_DEFER)
412 ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type);
414 return dev_err_probe(&pdev->dev, ret,
415 "could not read type\n");
417 ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype);
419 return dev_err_probe(&pdev->dev, ret,
420 "could not read subtype\n");
422 ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MAJOR, &dig_major);
424 return dev_err_probe(&pdev->dev, ret,
425 "could not read dig_major\n");
427 if (type != QPNP_TM_TYPE || (subtype != QPNP_TM_SUBTYPE_GEN1
428 && subtype != QPNP_TM_SUBTYPE_GEN2)) {
429 dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n",
434 chip->subtype = subtype;
435 if (subtype == QPNP_TM_SUBTYPE_GEN2 && dig_major >= 1)
436 chip->temp_map = &temp_map_gen2_v1;
438 chip->temp_map = &temp_map_gen1;
441 * Register the sensor before initializing the hardware to be able to
442 * read the trip points. get_temp() returns the default temperature
443 * before the hardware initialization is completed.
445 chip->tz_dev = devm_thermal_of_zone_register(
446 &pdev->dev, 0, chip, &qpnp_tm_sensor_ops);
447 if (IS_ERR(chip->tz_dev))
448 return dev_err_probe(&pdev->dev, PTR_ERR(chip->tz_dev),
449 "failed to register sensor\n");
451 ret = qpnp_tm_init(chip);
453 return dev_err_probe(&pdev->dev, ret, "init failed\n");
455 devm_thermal_add_hwmon_sysfs(&pdev->dev, chip->tz_dev);
457 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr,
458 IRQF_ONESHOT, node->name, chip);
462 thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED);
467 static const struct of_device_id qpnp_tm_match_table[] = {
468 { .compatible = "qcom,spmi-temp-alarm" },
471 MODULE_DEVICE_TABLE(of, qpnp_tm_match_table);
473 static struct platform_driver qpnp_tm_driver = {
475 .name = "spmi-temp-alarm",
476 .of_match_table = qpnp_tm_match_table,
478 .probe = qpnp_tm_probe,
480 module_platform_driver(qpnp_tm_driver);
482 MODULE_ALIAS("platform:spmi-temp-alarm");
483 MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");
484 MODULE_LICENSE("GPL v2");