Lines Matching +full:chip +full:- +full:temperature +full:- +full:threshold +full:- +full:celsius

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2015, 2017, 2020, The Linux Foundation. All rights reserved.
44 /* Over-temperature trip point values in mC */
59 #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
66 /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
78 /* protects .thresh, .stage and chip registers */
89 static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data) in qpnp_tm_read() argument
94 ret = regmap_read(chip->map, chip->base + addr, &val); in qpnp_tm_read()
102 static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data) in qpnp_tm_write() argument
104 return regmap_write(chip->map, chip->base + addr, data); in qpnp_tm_write()
108 * qpnp_tm_decode_temp() - return temperature in mC corresponding to the
109 * specified over-temperature stage
110 * @chip: Pointer to the qpnp_tm chip
111 * @stage: Over-temperature stage
113 * Return: temperature in mC
115 static long qpnp_tm_decode_temp(struct qpnp_tm_chip *chip, unsigned int stage) in qpnp_tm_decode_temp() argument
117 if (!chip->temp_map || chip->thresh >= THRESH_COUNT || stage == 0 || in qpnp_tm_decode_temp()
121 return (*chip->temp_map)[chip->thresh][stage - 1]; in qpnp_tm_decode_temp()
125 * qpnp_tm_get_temp_stage() - return over-temperature stage
126 * @chip: Pointer to the qpnp_tm chip
130 static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip) in qpnp_tm_get_temp_stage() argument
135 ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg); in qpnp_tm_get_temp_stage()
139 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) in qpnp_tm_get_temp_stage()
149 * current thermal stage and threshold as well as the previous stage
151 static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip) in qpnp_tm_update_temp_no_adc() argument
156 WARN_ON(!mutex_is_locked(&chip->lock)); in qpnp_tm_update_temp_no_adc()
158 ret = qpnp_tm_get_temp_stage(chip); in qpnp_tm_update_temp_no_adc()
163 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) { in qpnp_tm_update_temp_no_adc()
165 stage_old = chip->stage; in qpnp_tm_update_temp_no_adc()
168 stage_old = alarm_state_map[chip->stage]; in qpnp_tm_update_temp_no_adc()
173 chip->temp = qpnp_tm_decode_temp(chip, stage_new) in qpnp_tm_update_temp_no_adc()
177 chip->temp = qpnp_tm_decode_temp(chip, stage_new + 1) in qpnp_tm_update_temp_no_adc()
178 - TEMP_STAGE_HYSTERESIS; in qpnp_tm_update_temp_no_adc()
181 chip->stage = stage; in qpnp_tm_update_temp_no_adc()
188 struct qpnp_tm_chip *chip = thermal_zone_device_priv(tz); in qpnp_tm_get_temp() local
192 return -EINVAL; in qpnp_tm_get_temp()
194 if (!chip->initialized) { in qpnp_tm_get_temp()
199 if (!chip->adc) { in qpnp_tm_get_temp()
200 mutex_lock(&chip->lock); in qpnp_tm_get_temp()
201 ret = qpnp_tm_update_temp_no_adc(chip); in qpnp_tm_get_temp()
202 mutex_unlock(&chip->lock); in qpnp_tm_get_temp()
206 ret = iio_read_channel_processed(chip->adc, &mili_celsius); in qpnp_tm_get_temp()
210 chip->temp = mili_celsius; in qpnp_tm_get_temp()
213 *temp = chip->temp; in qpnp_tm_get_temp()
218 static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip, in qpnp_tm_update_critical_trip_temp() argument
221 long stage2_threshold_min = (*chip->temp_map)[THRESH_MIN][1]; in qpnp_tm_update_critical_trip_temp()
222 long stage2_threshold_max = (*chip->temp_map)[THRESH_MAX][1]; in qpnp_tm_update_critical_trip_temp()
226 WARN_ON(!mutex_is_locked(&chip->lock)); in qpnp_tm_update_critical_trip_temp()
230 * lowest threshold set, monitoring at 25Hz in qpnp_tm_update_critical_trip_temp()
236 chip->thresh = THRESH_MIN; in qpnp_tm_update_critical_trip_temp()
241 chip->thresh = THRESH_MAX - in qpnp_tm_update_critical_trip_temp()
242 ((stage2_threshold_max - temp) / in qpnp_tm_update_critical_trip_temp()
246 chip->thresh = THRESH_MAX; in qpnp_tm_update_critical_trip_temp()
248 if (chip->adc) in qpnp_tm_update_critical_trip_temp()
251 dev_warn(chip->dev, in qpnp_tm_update_critical_trip_temp()
252 …"No ADC is configured and critical temperature %d mC is above the maximum stage 2 threshold of %ld… in qpnp_tm_update_critical_trip_temp()
257 reg |= chip->thresh; in qpnp_tm_update_critical_trip_temp()
261 return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg); in qpnp_tm_update_critical_trip_temp()
267 struct qpnp_tm_chip *chip = thermal_zone_device_priv(tz); in qpnp_tm_set_trip_temp() local
270 if (trip->type != THERMAL_TRIP_CRITICAL) in qpnp_tm_set_trip_temp()
273 mutex_lock(&chip->lock); in qpnp_tm_set_trip_temp()
274 ret = qpnp_tm_update_critical_trip_temp(chip, temp); in qpnp_tm_set_trip_temp()
275 mutex_unlock(&chip->lock); in qpnp_tm_set_trip_temp()
287 struct qpnp_tm_chip *chip = data; in qpnp_tm_isr() local
289 thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED); in qpnp_tm_isr()
296 * current thermal stage and threshold. Setup threshold control and
299 static int qpnp_tm_init(struct qpnp_tm_chip *chip) in qpnp_tm_init() argument
306 mutex_lock(&chip->lock); in qpnp_tm_init()
308 ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg); in qpnp_tm_init()
312 chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK; in qpnp_tm_init()
313 chip->temp = DEFAULT_TEMP; in qpnp_tm_init()
315 ret = qpnp_tm_get_temp_stage(chip); in qpnp_tm_init()
318 chip->stage = ret; in qpnp_tm_init()
320 stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1 in qpnp_tm_init()
321 ? chip->stage : alarm_state_map[chip->stage]; in qpnp_tm_init()
324 chip->temp = qpnp_tm_decode_temp(chip, stage); in qpnp_tm_init()
326 mutex_unlock(&chip->lock); in qpnp_tm_init()
328 ret = thermal_zone_get_crit_temp(chip->tz_dev, &crit_temp); in qpnp_tm_init()
332 mutex_lock(&chip->lock); in qpnp_tm_init()
334 ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp); in qpnp_tm_init()
338 /* Enable the thermal alarm PMIC module in always-on mode. */ in qpnp_tm_init()
340 ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg); in qpnp_tm_init()
342 chip->initialized = true; in qpnp_tm_init()
345 mutex_unlock(&chip->lock); in qpnp_tm_init()
351 struct qpnp_tm_chip *chip; in qpnp_tm_probe() local
357 node = pdev->dev.of_node; in qpnp_tm_probe()
359 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); in qpnp_tm_probe()
360 if (!chip) in qpnp_tm_probe()
361 return -ENOMEM; in qpnp_tm_probe()
363 dev_set_drvdata(&pdev->dev, chip); in qpnp_tm_probe()
364 chip->dev = &pdev->dev; in qpnp_tm_probe()
366 mutex_init(&chip->lock); in qpnp_tm_probe()
368 chip->map = dev_get_regmap(pdev->dev.parent, NULL); in qpnp_tm_probe()
369 if (!chip->map) in qpnp_tm_probe()
370 return -ENXIO; in qpnp_tm_probe()
381 chip->adc = devm_iio_channel_get(&pdev->dev, "thermal"); in qpnp_tm_probe()
382 if (IS_ERR(chip->adc)) { in qpnp_tm_probe()
383 ret = PTR_ERR(chip->adc); in qpnp_tm_probe()
384 chip->adc = NULL; in qpnp_tm_probe()
385 if (ret == -EPROBE_DEFER) in qpnp_tm_probe()
389 chip->base = res; in qpnp_tm_probe()
391 ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type); in qpnp_tm_probe()
393 return dev_err_probe(&pdev->dev, ret, in qpnp_tm_probe()
396 ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype); in qpnp_tm_probe()
398 return dev_err_probe(&pdev->dev, ret, in qpnp_tm_probe()
401 ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MAJOR, &dig_major); in qpnp_tm_probe()
403 return dev_err_probe(&pdev->dev, ret, in qpnp_tm_probe()
408 dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n", in qpnp_tm_probe()
410 return -ENODEV; in qpnp_tm_probe()
413 chip->subtype = subtype; in qpnp_tm_probe()
415 chip->temp_map = &temp_map_gen2_v1; in qpnp_tm_probe()
417 chip->temp_map = &temp_map_gen1; in qpnp_tm_probe()
421 * read the trip points. get_temp() returns the default temperature in qpnp_tm_probe()
424 chip->tz_dev = devm_thermal_of_zone_register( in qpnp_tm_probe()
425 &pdev->dev, 0, chip, &qpnp_tm_sensor_ops); in qpnp_tm_probe()
426 if (IS_ERR(chip->tz_dev)) in qpnp_tm_probe()
427 return dev_err_probe(&pdev->dev, PTR_ERR(chip->tz_dev), in qpnp_tm_probe()
430 ret = qpnp_tm_init(chip); in qpnp_tm_probe()
432 return dev_err_probe(&pdev->dev, ret, "init failed\n"); in qpnp_tm_probe()
434 devm_thermal_add_hwmon_sysfs(&pdev->dev, chip->tz_dev); in qpnp_tm_probe()
436 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr, in qpnp_tm_probe()
437 IRQF_ONESHOT, node->name, chip); in qpnp_tm_probe()
441 thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED); in qpnp_tm_probe()
447 { .compatible = "qcom,spmi-temp-alarm" },
454 .name = "spmi-temp-alarm",
461 MODULE_ALIAS("platform:spmi-temp-alarm");
462 MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");