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.
46 /* Over-temperature trip point values in mC */
61 #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */
68 /* Temperature in Milli Celsius reported during stage 0 if no ADC is present */
81 /* protects .thresh, .stage and chip registers */
92 static int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *data) in qpnp_tm_read() argument
97 ret = regmap_read(chip->map, chip->base + addr, &val); in qpnp_tm_read()
105 static int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 data) in qpnp_tm_write() argument
107 return regmap_write(chip->map, chip->base + addr, data); in qpnp_tm_write()
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) in qpnp_tm_decode_temp() argument
120 if (!chip->temp_map || chip->thresh >= THRESH_COUNT || stage == 0 || in qpnp_tm_decode_temp()
124 return (*chip->temp_map)[chip->thresh][stage - 1]; in qpnp_tm_decode_temp()
128 * qpnp_tm_get_temp_stage() - return over-temperature stage
129 * @chip: Pointer to the qpnp_tm chip
133 static int qpnp_tm_get_temp_stage(struct qpnp_tm_chip *chip) in qpnp_tm_get_temp_stage() argument
138 ret = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, &reg); in qpnp_tm_get_temp_stage()
142 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) in qpnp_tm_get_temp_stage()
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) in qpnp_tm_update_temp_no_adc() argument
159 WARN_ON(!mutex_is_locked(&chip->lock)); in qpnp_tm_update_temp_no_adc()
161 ret = qpnp_tm_get_temp_stage(chip); in qpnp_tm_update_temp_no_adc()
166 if (chip->subtype == QPNP_TM_SUBTYPE_GEN1) { in qpnp_tm_update_temp_no_adc()
168 stage_old = chip->stage; in qpnp_tm_update_temp_no_adc()
171 stage_old = alarm_state_map[chip->stage]; in qpnp_tm_update_temp_no_adc()
176 chip->temp = qpnp_tm_decode_temp(chip, stage_new) in qpnp_tm_update_temp_no_adc()
180 chip->temp = qpnp_tm_decode_temp(chip, stage_new + 1) in qpnp_tm_update_temp_no_adc()
181 - TEMP_STAGE_HYSTERESIS; in qpnp_tm_update_temp_no_adc()
184 chip->stage = stage; in qpnp_tm_update_temp_no_adc()
191 struct qpnp_tm_chip *chip = tz->devdata; in qpnp_tm_get_temp() local
195 return -EINVAL; in qpnp_tm_get_temp()
197 if (!chip->initialized) { in qpnp_tm_get_temp()
202 if (!chip->adc) { in qpnp_tm_get_temp()
203 mutex_lock(&chip->lock); in qpnp_tm_get_temp()
204 ret = qpnp_tm_update_temp_no_adc(chip); in qpnp_tm_get_temp()
205 mutex_unlock(&chip->lock); in qpnp_tm_get_temp()
209 ret = iio_read_channel_processed(chip->adc, &mili_celsius); in qpnp_tm_get_temp()
213 chip->temp = mili_celsius; in qpnp_tm_get_temp()
216 *temp = chip->temp; in qpnp_tm_get_temp()
221 static int qpnp_tm_update_critical_trip_temp(struct qpnp_tm_chip *chip, in qpnp_tm_update_critical_trip_temp() argument
224 long stage2_threshold_min = (*chip->temp_map)[THRESH_MIN][1]; in qpnp_tm_update_critical_trip_temp()
225 long stage2_threshold_max = (*chip->temp_map)[THRESH_MAX][1]; in qpnp_tm_update_critical_trip_temp()
229 WARN_ON(!mutex_is_locked(&chip->lock)); in qpnp_tm_update_critical_trip_temp()
233 * lowest threshold set, monitoring at 25Hz in qpnp_tm_update_critical_trip_temp()
239 chip->thresh = THRESH_MIN; in qpnp_tm_update_critical_trip_temp()
244 chip->thresh = THRESH_MAX - in qpnp_tm_update_critical_trip_temp()
245 ((stage2_threshold_max - temp) / in qpnp_tm_update_critical_trip_temp()
249 chip->thresh = THRESH_MAX; in qpnp_tm_update_critical_trip_temp()
251 if (chip->adc) in qpnp_tm_update_critical_trip_temp()
254 dev_warn(chip->dev, in qpnp_tm_update_critical_trip_temp()
255 …"No ADC is configured and critical temperature is above the maximum stage 2 threshold of 140 C! Co… in qpnp_tm_update_critical_trip_temp()
259 reg |= chip->thresh; in qpnp_tm_update_critical_trip_temp()
263 return qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, reg); in qpnp_tm_update_critical_trip_temp()
268 struct qpnp_tm_chip *chip = tz->devdata; in qpnp_tm_set_trip_temp() local
272 trip_points = of_thermal_get_trip_points(chip->tz_dev); in qpnp_tm_set_trip_temp()
274 return -EINVAL; in qpnp_tm_set_trip_temp()
279 mutex_lock(&chip->lock); in qpnp_tm_set_trip_temp()
280 ret = qpnp_tm_update_critical_trip_temp(chip, temp); in qpnp_tm_set_trip_temp()
281 mutex_unlock(&chip->lock); in qpnp_tm_set_trip_temp()
293 struct qpnp_tm_chip *chip = data; in qpnp_tm_isr() local
295 thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED); in qpnp_tm_isr()
300 static int qpnp_tm_get_critical_trip_temp(struct qpnp_tm_chip *chip) in qpnp_tm_get_critical_trip_temp() argument
306 ntrips = of_thermal_get_ntrips(chip->tz_dev); in qpnp_tm_get_critical_trip_temp()
310 trips = of_thermal_get_trip_points(chip->tz_dev); in qpnp_tm_get_critical_trip_temp()
315 if (of_thermal_is_trip_valid(chip->tz_dev, i) && in qpnp_tm_get_critical_trip_temp()
317 return trips[i].temperature; in qpnp_tm_get_critical_trip_temp()
325 * current thermal stage and threshold. Setup threshold control and
328 static int qpnp_tm_init(struct qpnp_tm_chip *chip) in qpnp_tm_init() argument
335 mutex_lock(&chip->lock); in qpnp_tm_init()
337 ret = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, &reg); in qpnp_tm_init()
341 chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK; in qpnp_tm_init()
342 chip->temp = DEFAULT_TEMP; in qpnp_tm_init()
344 ret = qpnp_tm_get_temp_stage(chip); in qpnp_tm_init()
347 chip->stage = ret; in qpnp_tm_init()
349 stage = chip->subtype == QPNP_TM_SUBTYPE_GEN1 in qpnp_tm_init()
350 ? chip->stage : alarm_state_map[chip->stage]; in qpnp_tm_init()
353 chip->temp = qpnp_tm_decode_temp(chip, stage); in qpnp_tm_init()
355 crit_temp = qpnp_tm_get_critical_trip_temp(chip); in qpnp_tm_init()
356 ret = qpnp_tm_update_critical_trip_temp(chip, crit_temp); in qpnp_tm_init()
360 /* Enable the thermal alarm PMIC module in always-on mode. */ in qpnp_tm_init()
362 ret = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, reg); in qpnp_tm_init()
364 chip->initialized = true; in qpnp_tm_init()
367 mutex_unlock(&chip->lock); in qpnp_tm_init()
373 struct qpnp_tm_chip *chip; in qpnp_tm_probe() local
379 node = pdev->dev.of_node; in qpnp_tm_probe()
381 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); in qpnp_tm_probe()
382 if (!chip) in qpnp_tm_probe()
383 return -ENOMEM; in qpnp_tm_probe()
385 dev_set_drvdata(&pdev->dev, chip); in qpnp_tm_probe()
386 chip->dev = &pdev->dev; in qpnp_tm_probe()
388 mutex_init(&chip->lock); in qpnp_tm_probe()
390 chip->map = dev_get_regmap(pdev->dev.parent, NULL); in qpnp_tm_probe()
391 if (!chip->map) in qpnp_tm_probe()
392 return -ENXIO; in qpnp_tm_probe()
403 chip->adc = devm_iio_channel_get(&pdev->dev, "thermal"); in qpnp_tm_probe()
404 if (IS_ERR(chip->adc)) { in qpnp_tm_probe()
405 ret = PTR_ERR(chip->adc); in qpnp_tm_probe()
406 chip->adc = NULL; in qpnp_tm_probe()
407 if (ret == -EPROBE_DEFER) in qpnp_tm_probe()
411 chip->base = res; in qpnp_tm_probe()
413 ret = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, &type); in qpnp_tm_probe()
415 dev_err(&pdev->dev, "could not read type\n"); in qpnp_tm_probe()
419 ret = qpnp_tm_read(chip, QPNP_TM_REG_SUBTYPE, &subtype); in qpnp_tm_probe()
421 dev_err(&pdev->dev, "could not read subtype\n"); in qpnp_tm_probe()
425 ret = qpnp_tm_read(chip, QPNP_TM_REG_DIG_MAJOR, &dig_major); in qpnp_tm_probe()
427 dev_err(&pdev->dev, "could not read dig_major\n"); in qpnp_tm_probe()
433 dev_err(&pdev->dev, "invalid type 0x%02x or subtype 0x%02x\n", in qpnp_tm_probe()
435 return -ENODEV; in qpnp_tm_probe()
438 chip->subtype = subtype; in qpnp_tm_probe()
440 chip->temp_map = &temp_map_gen2_v1; in qpnp_tm_probe()
442 chip->temp_map = &temp_map_gen1; in qpnp_tm_probe()
446 * read the trip points. get_temp() returns the default temperature in qpnp_tm_probe()
449 chip->tz_dev = devm_thermal_of_zone_register( in qpnp_tm_probe()
450 &pdev->dev, 0, chip, &qpnp_tm_sensor_ops); in qpnp_tm_probe()
451 if (IS_ERR(chip->tz_dev)) { in qpnp_tm_probe()
452 dev_err(&pdev->dev, "failed to register sensor\n"); in qpnp_tm_probe()
453 return PTR_ERR(chip->tz_dev); in qpnp_tm_probe()
456 ret = qpnp_tm_init(chip); in qpnp_tm_probe()
458 dev_err(&pdev->dev, "init failed\n"); in qpnp_tm_probe()
462 if (devm_thermal_add_hwmon_sysfs(chip->tz_dev)) in qpnp_tm_probe()
463 dev_warn(&pdev->dev, in qpnp_tm_probe()
466 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, qpnp_tm_isr, in qpnp_tm_probe()
467 IRQF_ONESHOT, node->name, chip); in qpnp_tm_probe()
471 thermal_zone_device_update(chip->tz_dev, THERMAL_EVENT_UNSPECIFIED); in qpnp_tm_probe()
477 { .compatible = "qcom,spmi-temp-alarm" },
484 .name = "spmi-temp-alarm",
491 MODULE_ALIAS("platform:spmi-temp-alarm");
492 MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver");