1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2019, 2020, Linaro Ltd.
7 #include <linux/debugfs.h>
10 #include <linux/module.h>
11 #include <linux/nvmem-consumer.h>
13 #include <linux/of_address.h>
14 #include <linux/of_platform.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 #include <linux/thermal.h>
21 #include "../thermal_hwmon.h"
25 * struct tsens_irq_data - IRQ status and temperature violations
26 * @up_viol: upper threshold violated
27 * @up_thresh: upper threshold temperature value
28 * @up_irq_mask: mask register for upper threshold irqs
29 * @up_irq_clear: clear register for uppper threshold irqs
30 * @low_viol: lower threshold violated
31 * @low_thresh: lower threshold temperature value
32 * @low_irq_mask: mask register for lower threshold irqs
33 * @low_irq_clear: clear register for lower threshold irqs
34 * @crit_viol: critical threshold violated
35 * @crit_thresh: critical threshold temperature value
36 * @crit_irq_mask: mask register for critical threshold irqs
37 * @crit_irq_clear: clear register for critical threshold irqs
39 * Structure containing data about temperature threshold settings and
40 * irq status if they were violated.
42 struct tsens_irq_data {
57 char *qfprom_read(struct device *dev, const char *cname)
59 struct nvmem_cell *cell;
63 cell = nvmem_cell_get(dev, cname);
65 return ERR_CAST(cell);
67 ret = nvmem_cell_read(cell, &data);
73 int tsens_read_calibration(struct tsens_priv *priv, int shift, u32 *p1, u32 *p2, bool backup)
77 char name[] = "sXX_pY_backup"; /* s10_p1_backup */
80 if (priv->num_sensors > MAX_SENSORS)
83 ret = snprintf(name, sizeof(name), "mode%s", backup ? "_backup" : "");
87 ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &mode);
89 dev_warn(priv->dev, "Please migrate to separate nvmem cells for calibration data\n");
93 dev_dbg(priv->dev, "calibration mode is %d\n", mode);
95 ret = snprintf(name, sizeof(name), "base1%s", backup ? "_backup" : "");
99 ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &base1);
103 ret = snprintf(name, sizeof(name), "base2%s", backup ? "_backup" : "");
107 ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &base2);
111 for (i = 0; i < priv->num_sensors; i++) {
112 ret = snprintf(name, sizeof(name), "s%d_p1%s", priv->sensor[i].hw_id,
113 backup ? "_backup" : "");
117 ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &p1[i]);
121 ret = snprintf(name, sizeof(name), "s%d_p2%s", priv->sensor[i].hw_id,
122 backup ? "_backup" : "");
126 ret = nvmem_cell_read_variable_le_u32(priv->dev, name, &p2[i]);
133 for (i = 0; i < priv->num_sensors; i++)
134 p1[i] = p1[i] + (base1 << shift);
137 for (i = 0; i < priv->num_sensors; i++)
138 p2[i] = (p2[i] + base2) << shift;
141 for (i = 0; i < priv->num_sensors; i++)
142 p1[i] = (p1[i] + base1) << shift;
145 dev_dbg(priv->dev, "calibrationless mode\n");
146 for (i = 0; i < priv->num_sensors; i++) {
155 int tsens_calibrate_nvmem(struct tsens_priv *priv, int shift)
157 u32 p1[MAX_SENSORS], p2[MAX_SENSORS];
160 mode = tsens_read_calibration(priv, shift, p1, p2, false);
164 compute_intercept_slope(priv, p1, p2, mode);
169 int tsens_calibrate_common(struct tsens_priv *priv)
171 return tsens_calibrate_nvmem(priv, 2);
174 static u32 tsens_read_cell(const struct tsens_single_value *cell, u8 len, u32 *data0, u32 *data1)
177 u32 *data = cell->blob ? data1 : data0;
179 if (cell->shift + len <= 32) {
180 val = data[cell->idx] >> cell->shift;
182 u8 part = 32 - cell->shift;
184 val = data[cell->idx] >> cell->shift;
185 val |= data[cell->idx + 1] << part;
188 return val & ((1 << len) - 1);
191 int tsens_read_calibration_legacy(struct tsens_priv *priv,
192 const struct tsens_legacy_calibration_format *format,
194 u32 *cdata0, u32 *cdata1)
200 mode = tsens_read_cell(&format->mode, 2, cdata0, cdata1);
201 invalid = tsens_read_cell(&format->invalid, 1, cdata0, cdata1);
204 dev_dbg(priv->dev, "calibration mode is %d\n", mode);
206 base1 = tsens_read_cell(&format->base[0], format->base_len, cdata0, cdata1);
207 base2 = tsens_read_cell(&format->base[1], format->base_len, cdata0, cdata1);
209 for (i = 0; i < priv->num_sensors; i++) {
210 p1[i] = tsens_read_cell(&format->sp[i][0], format->sp_len, cdata0, cdata1);
211 p2[i] = tsens_read_cell(&format->sp[i][1], format->sp_len, cdata0, cdata1);
216 for (i = 0; i < priv->num_sensors; i++)
217 p1[i] = p1[i] + (base1 << format->base_shift);
220 for (i = 0; i < priv->num_sensors; i++)
221 p2[i] = (p2[i] + base2) << format->base_shift;
224 for (i = 0; i < priv->num_sensors; i++)
225 p1[i] = (p1[i] + base1) << format->base_shift;
228 dev_dbg(priv->dev, "calibrationless mode\n");
229 for (i = 0; i < priv->num_sensors; i++) {
239 * Use this function on devices where slope and offset calculations
240 * depend on calibration data read from qfprom. On others the slope
241 * and offset values are derived from tz->tzp->slope and tz->tzp->offset
244 void compute_intercept_slope(struct tsens_priv *priv, u32 *p1,
250 for (i = 0; i < priv->num_sensors; i++) {
252 "%s: sensor%d - data_point1:%#x data_point2:%#x\n",
253 __func__, i, p1[i], p2[i]);
255 if (!priv->sensor[i].slope)
256 priv->sensor[i].slope = SLOPE_DEFAULT;
257 if (mode == TWO_PT_CALIB) {
259 * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
260 * temp_120_degc - temp_30_degc (x2 - x1)
264 den = CAL_DEGC_PT2 - CAL_DEGC_PT1;
265 priv->sensor[i].slope = num / den;
268 priv->sensor[i].offset = (p1[i] * SLOPE_FACTOR) -
270 priv->sensor[i].slope);
271 dev_dbg(priv->dev, "%s: offset:%d\n", __func__,
272 priv->sensor[i].offset);
276 static inline u32 degc_to_code(int degc, const struct tsens_sensor *s)
278 u64 code = div_u64(((u64)degc * s->slope + s->offset), SLOPE_FACTOR);
280 pr_debug("%s: raw_code: 0x%llx, degc:%d\n", __func__, code, degc);
281 return clamp_val(code, THRESHOLD_MIN_ADC_CODE, THRESHOLD_MAX_ADC_CODE);
284 static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
288 num = (adc_code * SLOPE_FACTOR) - s->offset;
292 degc = num + (den / 2);
294 degc = num - (den / 2);
304 * tsens_hw_to_mC - Return sign-extended temperature in mCelsius.
305 * @s: Pointer to sensor struct
306 * @field: Index into regmap_field array pointing to temperature data
308 * This function handles temperature returned in ADC code or deciCelsius
309 * depending on IP version.
311 * Return: Temperature in milliCelsius on success, a negative errno will
312 * be returned in error cases
314 static int tsens_hw_to_mC(const struct tsens_sensor *s, int field)
316 struct tsens_priv *priv = s->priv;
321 resolution = priv->fields[LAST_TEMP_0].msb -
322 priv->fields[LAST_TEMP_0].lsb;
324 ret = regmap_field_read(priv->rf[field], &temp);
328 /* Convert temperature from ADC code to milliCelsius */
330 return code_to_degc(temp, s) * 1000;
332 /* deciCelsius -> milliCelsius along with sign extension */
333 return sign_extend32(temp, resolution) * 100;
337 * tsens_mC_to_hw - Convert temperature to hardware register value
338 * @s: Pointer to sensor struct
339 * @temp: temperature in milliCelsius to be programmed to hardware
341 * This function outputs the value to be written to hardware in ADC code
342 * or deciCelsius depending on IP version.
344 * Return: ADC code or temperature in deciCelsius.
346 static int tsens_mC_to_hw(const struct tsens_sensor *s, int temp)
348 struct tsens_priv *priv = s->priv;
350 /* milliC to adc code */
352 return degc_to_code(temp / 1000, s);
354 /* milliC to deciC */
358 static inline enum tsens_ver tsens_version(struct tsens_priv *priv)
360 return priv->feat->ver_major;
363 static void tsens_set_interrupt_v1(struct tsens_priv *priv, u32 hw_id,
364 enum tsens_irq_type irq_type, bool enable)
370 index = UP_INT_CLEAR_0 + hw_id;
373 index = LOW_INT_CLEAR_0 + hw_id;
376 /* No critical interrupts before v2 */
379 regmap_field_write(priv->rf[index], enable ? 0 : 1);
382 static void tsens_set_interrupt_v2(struct tsens_priv *priv, u32 hw_id,
383 enum tsens_irq_type irq_type, bool enable)
385 u32 index_mask = 0, index_clear = 0;
388 * To enable the interrupt flag for a sensor:
389 * - clear the mask bit
390 * To disable the interrupt flag for a sensor:
391 * - Mask further interrupts for this sensor
392 * - Write 1 followed by 0 to clear the interrupt
396 index_mask = UP_INT_MASK_0 + hw_id;
397 index_clear = UP_INT_CLEAR_0 + hw_id;
400 index_mask = LOW_INT_MASK_0 + hw_id;
401 index_clear = LOW_INT_CLEAR_0 + hw_id;
404 index_mask = CRIT_INT_MASK_0 + hw_id;
405 index_clear = CRIT_INT_CLEAR_0 + hw_id;
410 regmap_field_write(priv->rf[index_mask], 0);
412 regmap_field_write(priv->rf[index_mask], 1);
413 regmap_field_write(priv->rf[index_clear], 1);
414 regmap_field_write(priv->rf[index_clear], 0);
419 * tsens_set_interrupt - Set state of an interrupt
420 * @priv: Pointer to tsens controller private data
421 * @hw_id: Hardware ID aka. sensor number
422 * @irq_type: irq_type from enum tsens_irq_type
423 * @enable: false = disable, true = enable
425 * Call IP-specific function to set state of an interrupt
429 static void tsens_set_interrupt(struct tsens_priv *priv, u32 hw_id,
430 enum tsens_irq_type irq_type, bool enable)
432 dev_dbg(priv->dev, "[%u] %s: %s -> %s\n", hw_id, __func__,
433 irq_type ? ((irq_type == 1) ? "UP" : "CRITICAL") : "LOW",
434 enable ? "en" : "dis");
435 if (tsens_version(priv) > VER_1_X)
436 tsens_set_interrupt_v2(priv, hw_id, irq_type, enable);
438 tsens_set_interrupt_v1(priv, hw_id, irq_type, enable);
442 * tsens_threshold_violated - Check if a sensor temperature violated a preset threshold
443 * @priv: Pointer to tsens controller private data
444 * @hw_id: Hardware ID aka. sensor number
445 * @d: Pointer to irq state data
447 * Return: 0 if threshold was not violated, 1 if it was violated and negative
448 * errno in case of errors
450 static int tsens_threshold_violated(struct tsens_priv *priv, u32 hw_id,
451 struct tsens_irq_data *d)
455 ret = regmap_field_read(priv->rf[UPPER_STATUS_0 + hw_id], &d->up_viol);
458 ret = regmap_field_read(priv->rf[LOWER_STATUS_0 + hw_id], &d->low_viol);
462 if (priv->feat->crit_int) {
463 ret = regmap_field_read(priv->rf[CRITICAL_STATUS_0 + hw_id],
469 if (d->up_viol || d->low_viol || d->crit_viol)
475 static int tsens_read_irq_state(struct tsens_priv *priv, u32 hw_id,
476 const struct tsens_sensor *s,
477 struct tsens_irq_data *d)
481 ret = regmap_field_read(priv->rf[UP_INT_CLEAR_0 + hw_id], &d->up_irq_clear);
484 ret = regmap_field_read(priv->rf[LOW_INT_CLEAR_0 + hw_id], &d->low_irq_clear);
487 if (tsens_version(priv) > VER_1_X) {
488 ret = regmap_field_read(priv->rf[UP_INT_MASK_0 + hw_id], &d->up_irq_mask);
491 ret = regmap_field_read(priv->rf[LOW_INT_MASK_0 + hw_id], &d->low_irq_mask);
494 ret = regmap_field_read(priv->rf[CRIT_INT_CLEAR_0 + hw_id],
498 ret = regmap_field_read(priv->rf[CRIT_INT_MASK_0 + hw_id],
503 d->crit_thresh = tsens_hw_to_mC(s, CRIT_THRESH_0 + hw_id);
505 /* No mask register on older TSENS */
508 d->crit_irq_clear = 0;
509 d->crit_irq_mask = 0;
513 d->up_thresh = tsens_hw_to_mC(s, UP_THRESH_0 + hw_id);
514 d->low_thresh = tsens_hw_to_mC(s, LOW_THRESH_0 + hw_id);
516 dev_dbg(priv->dev, "[%u] %s%s: status(%u|%u|%u) | clr(%u|%u|%u) | mask(%u|%u|%u)\n",
518 (d->up_viol || d->low_viol || d->crit_viol) ? "(V)" : "",
519 d->low_viol, d->up_viol, d->crit_viol,
520 d->low_irq_clear, d->up_irq_clear, d->crit_irq_clear,
521 d->low_irq_mask, d->up_irq_mask, d->crit_irq_mask);
522 dev_dbg(priv->dev, "[%u] %s%s: thresh: (%d:%d:%d)\n", hw_id, __func__,
523 (d->up_viol || d->low_viol || d->crit_viol) ? "(V)" : "",
524 d->low_thresh, d->up_thresh, d->crit_thresh);
529 static inline u32 masked_irq(u32 hw_id, u32 mask, enum tsens_ver ver)
532 return mask & (1 << hw_id);
534 /* v1, v0.1 don't have a irq mask register */
539 * tsens_critical_irq_thread() - Threaded handler for critical interrupts
541 * @data: tsens controller private data
543 * Check FSM watchdog bark status and clear if needed.
544 * Check all sensors to find ones that violated their critical threshold limits.
545 * Clear and then re-enable the interrupt.
547 * The level-triggered interrupt might deassert if the temperature returned to
548 * within the threshold limits by the time the handler got scheduled. We
549 * consider the irq to have been handled in that case.
551 * Return: IRQ_HANDLED
553 static irqreturn_t tsens_critical_irq_thread(int irq, void *data)
555 struct tsens_priv *priv = data;
556 struct tsens_irq_data d;
558 u32 wdog_status, wdog_count;
560 if (priv->feat->has_watchdog) {
561 ret = regmap_field_read(priv->rf[WDOG_BARK_STATUS],
567 /* Clear WDOG interrupt */
568 regmap_field_write(priv->rf[WDOG_BARK_CLEAR], 1);
569 regmap_field_write(priv->rf[WDOG_BARK_CLEAR], 0);
570 ret = regmap_field_read(priv->rf[WDOG_BARK_COUNT],
575 dev_dbg(priv->dev, "%s: watchdog count: %d\n",
576 __func__, wdog_count);
578 /* Fall through to handle critical interrupts if any */
582 for (i = 0; i < priv->num_sensors; i++) {
583 const struct tsens_sensor *s = &priv->sensor[i];
584 u32 hw_id = s->hw_id;
588 if (!tsens_threshold_violated(priv, hw_id, &d))
590 ret = get_temp_tsens_valid(s, &temp);
592 dev_err(priv->dev, "[%u] %s: error reading sensor\n",
597 tsens_read_irq_state(priv, hw_id, s, &d);
599 !masked_irq(hw_id, d.crit_irq_mask, tsens_version(priv))) {
600 /* Mask critical interrupts, unused on Linux */
601 tsens_set_interrupt(priv, hw_id, CRITICAL, false);
609 * tsens_irq_thread - Threaded interrupt handler for uplow interrupts
611 * @data: tsens controller private data
613 * Check all sensors to find ones that violated their threshold limits. If the
614 * temperature is still outside the limits, call thermal_zone_device_update() to
615 * update the thresholds, else re-enable the interrupts.
617 * The level-triggered interrupt might deassert if the temperature returned to
618 * within the threshold limits by the time the handler got scheduled. We
619 * consider the irq to have been handled in that case.
621 * Return: IRQ_HANDLED
623 static irqreturn_t tsens_irq_thread(int irq, void *data)
625 struct tsens_priv *priv = data;
626 struct tsens_irq_data d;
629 for (i = 0; i < priv->num_sensors; i++) {
630 const struct tsens_sensor *s = &priv->sensor[i];
631 u32 hw_id = s->hw_id;
635 if (!tsens_threshold_violated(priv, hw_id, &d))
638 thermal_zone_device_update(s->tzd, THERMAL_EVENT_UNSPECIFIED);
640 if (tsens_version(priv) < VER_0_1) {
641 /* Constraint: There is only 1 interrupt control register for all
642 * 11 temperature sensor. So monitoring more than 1 sensor based
643 * on interrupts will yield inconsistent result. To overcome this
644 * issue we will monitor only sensor 0 which is the master sensor.
654 * tsens_combined_irq_thread() - Threaded interrupt handler for combined interrupts
656 * @data: tsens controller private data
658 * Handle the combined interrupt as if it were 2 separate interrupts, so call the
659 * critical handler first and then the up/low one.
661 * Return: IRQ_HANDLED
663 static irqreturn_t tsens_combined_irq_thread(int irq, void *data)
667 ret = tsens_critical_irq_thread(irq, data);
668 if (ret != IRQ_HANDLED)
671 return tsens_irq_thread(irq, data);
674 static int tsens_set_trips(struct thermal_zone_device *tz, int low, int high)
676 struct tsens_sensor *s = thermal_zone_device_priv(tz);
677 struct tsens_priv *priv = s->priv;
678 struct device *dev = priv->dev;
679 struct tsens_irq_data d;
681 int high_val, low_val, cl_high, cl_low;
682 u32 hw_id = s->hw_id;
684 if (tsens_version(priv) < VER_0_1) {
685 /* Pre v0.1 IP had a single register for each type of interrupt
691 dev_dbg(dev, "[%u] %s: proposed thresholds: (%d:%d)\n",
692 hw_id, __func__, low, high);
694 cl_high = clamp_val(high, priv->feat->trip_min_temp, priv->feat->trip_max_temp);
695 cl_low = clamp_val(low, priv->feat->trip_min_temp, priv->feat->trip_max_temp);
697 high_val = tsens_mC_to_hw(s, cl_high);
698 low_val = tsens_mC_to_hw(s, cl_low);
700 spin_lock_irqsave(&priv->ul_lock, flags);
702 tsens_read_irq_state(priv, hw_id, s, &d);
704 /* Write the new thresholds and clear the status */
705 regmap_field_write(priv->rf[LOW_THRESH_0 + hw_id], low_val);
706 regmap_field_write(priv->rf[UP_THRESH_0 + hw_id], high_val);
707 tsens_set_interrupt(priv, hw_id, LOWER, true);
708 tsens_set_interrupt(priv, hw_id, UPPER, true);
710 spin_unlock_irqrestore(&priv->ul_lock, flags);
712 dev_dbg(dev, "[%u] %s: (%d:%d)->(%d:%d)\n",
713 hw_id, __func__, d.low_thresh, d.up_thresh, cl_low, cl_high);
718 static int tsens_enable_irq(struct tsens_priv *priv)
721 int val = tsens_version(priv) > VER_1_X ? 7 : 1;
723 ret = regmap_field_write(priv->rf[INT_EN], val);
725 dev_err(priv->dev, "%s: failed to enable interrupts\n",
731 static void tsens_disable_irq(struct tsens_priv *priv)
733 regmap_field_write(priv->rf[INT_EN], 0);
736 int get_temp_tsens_valid(const struct tsens_sensor *s, int *temp)
738 struct tsens_priv *priv = s->priv;
739 int hw_id = s->hw_id;
740 u32 temp_idx = LAST_TEMP_0 + hw_id;
741 u32 valid_idx = VALID_0 + hw_id;
745 /* VER_0 doesn't have VALID bit */
746 if (tsens_version(priv) == VER_0)
749 /* Valid bit is 0 for 6 AHB clock cycles.
750 * At 19.2MHz, 1 AHB clock is ~60ns.
751 * We should enter this loop very, very rarely.
752 * Wait 1 us since it's the min of poll_timeout macro.
753 * Old value was 400 ns.
755 ret = regmap_field_read_poll_timeout(priv->rf[valid_idx], valid,
756 valid, 1, 20 * USEC_PER_MSEC);
761 /* Valid bit is set, OK to read the temperature */
762 *temp = tsens_hw_to_mC(s, temp_idx);
767 int get_temp_common(const struct tsens_sensor *s, int *temp)
769 struct tsens_priv *priv = s->priv;
770 int hw_id = s->hw_id;
771 int last_temp = 0, ret, trdy;
772 unsigned long timeout;
774 timeout = jiffies + usecs_to_jiffies(TIMEOUT_US);
776 if (tsens_version(priv) == VER_0) {
777 ret = regmap_field_read(priv->rf[TRDY], &trdy);
784 ret = regmap_field_read(priv->rf[LAST_TEMP_0 + hw_id], &last_temp);
788 *temp = code_to_degc(last_temp, s) * 1000;
791 } while (time_before(jiffies, timeout));
796 #ifdef CONFIG_DEBUG_FS
797 static int dbg_sensors_show(struct seq_file *s, void *data)
799 struct platform_device *pdev = s->private;
800 struct tsens_priv *priv = platform_get_drvdata(pdev);
803 seq_printf(s, "max: %2d\nnum: %2d\n\n",
804 priv->feat->max_sensors, priv->num_sensors);
806 seq_puts(s, " id slope offset\n--------------------------\n");
807 for (i = 0; i < priv->num_sensors; i++) {
808 seq_printf(s, "%8d %8d %8d\n", priv->sensor[i].hw_id,
809 priv->sensor[i].slope, priv->sensor[i].offset);
815 static int dbg_version_show(struct seq_file *s, void *data)
817 struct platform_device *pdev = s->private;
818 struct tsens_priv *priv = platform_get_drvdata(pdev);
819 u32 maj_ver, min_ver, step_ver;
822 if (tsens_version(priv) > VER_0_1) {
823 ret = regmap_field_read(priv->rf[VER_MAJOR], &maj_ver);
826 ret = regmap_field_read(priv->rf[VER_MINOR], &min_ver);
829 ret = regmap_field_read(priv->rf[VER_STEP], &step_ver);
832 seq_printf(s, "%d.%d.%d\n", maj_ver, min_ver, step_ver);
834 seq_printf(s, "0.%d.0\n", priv->feat->ver_major);
840 DEFINE_SHOW_ATTRIBUTE(dbg_version);
841 DEFINE_SHOW_ATTRIBUTE(dbg_sensors);
843 static void tsens_debug_init(struct platform_device *pdev)
845 struct tsens_priv *priv = platform_get_drvdata(pdev);
847 priv->debug_root = debugfs_lookup("tsens", NULL);
848 if (!priv->debug_root)
849 priv->debug_root = debugfs_create_dir("tsens", NULL);
851 /* A directory for each instance of the TSENS IP */
852 priv->debug = debugfs_create_dir(dev_name(&pdev->dev), priv->debug_root);
853 debugfs_create_file("version", 0444, priv->debug, pdev, &dbg_version_fops);
854 debugfs_create_file("sensors", 0444, priv->debug, pdev, &dbg_sensors_fops);
857 static inline void tsens_debug_init(struct platform_device *pdev) {}
860 static const struct regmap_config tsens_config = {
867 static const struct regmap_config tsens_srot_config = {
874 int __init init_common(struct tsens_priv *priv)
876 void __iomem *tm_base, *srot_base;
877 struct device *dev = priv->dev;
879 struct resource *res;
882 struct platform_device *op = of_find_device_by_node(priv->dev->of_node);
887 if (op->num_resources > 1) {
888 /* DT with separate SROT and TM address space */
890 res = platform_get_resource(op, IORESOURCE_MEM, 1);
891 srot_base = devm_ioremap_resource(dev, res);
892 if (IS_ERR(srot_base)) {
893 ret = PTR_ERR(srot_base);
897 priv->srot_map = devm_regmap_init_mmio(dev, srot_base,
899 if (IS_ERR(priv->srot_map)) {
900 ret = PTR_ERR(priv->srot_map);
904 /* old DTs where SROT and TM were in a contiguous 2K block */
905 priv->tm_offset = 0x1000;
908 if (tsens_version(priv) >= VER_0_1) {
909 res = platform_get_resource(op, IORESOURCE_MEM, 0);
910 tm_base = devm_ioremap_resource(dev, res);
911 if (IS_ERR(tm_base)) {
912 ret = PTR_ERR(tm_base);
916 priv->tm_map = devm_regmap_init_mmio(dev, tm_base, &tsens_config);
917 } else { /* VER_0 share the same gcc regs using a syscon */
918 struct device *parent = priv->dev->parent;
921 priv->tm_map = syscon_node_to_regmap(parent->of_node);
924 if (IS_ERR_OR_NULL(priv->tm_map)) {
928 ret = PTR_ERR(priv->tm_map);
932 /* VER_0 have only tm_map */
934 priv->srot_map = priv->tm_map;
936 if (tsens_version(priv) > VER_0_1) {
937 for (i = VER_MAJOR; i <= VER_STEP; i++) {
938 priv->rf[i] = devm_regmap_field_alloc(dev, priv->srot_map,
940 if (IS_ERR(priv->rf[i])) {
941 ret = PTR_ERR(priv->rf[i]);
945 ret = regmap_field_read(priv->rf[VER_MINOR], &ver_minor);
950 priv->rf[TSENS_EN] = devm_regmap_field_alloc(dev, priv->srot_map,
951 priv->fields[TSENS_EN]);
952 if (IS_ERR(priv->rf[TSENS_EN])) {
953 ret = PTR_ERR(priv->rf[TSENS_EN]);
956 /* in VER_0 TSENS need to be explicitly enabled */
957 if (tsens_version(priv) == VER_0)
958 regmap_field_write(priv->rf[TSENS_EN], 1);
960 ret = regmap_field_read(priv->rf[TSENS_EN], &enabled);
964 dev_err(dev, "%s: device not enabled\n", __func__);
969 priv->rf[SENSOR_EN] = devm_regmap_field_alloc(dev, priv->srot_map,
970 priv->fields[SENSOR_EN]);
971 if (IS_ERR(priv->rf[SENSOR_EN])) {
972 ret = PTR_ERR(priv->rf[SENSOR_EN]);
975 priv->rf[INT_EN] = devm_regmap_field_alloc(dev, priv->tm_map,
976 priv->fields[INT_EN]);
977 if (IS_ERR(priv->rf[INT_EN])) {
978 ret = PTR_ERR(priv->rf[INT_EN]);
982 priv->rf[TSENS_SW_RST] =
983 devm_regmap_field_alloc(dev, priv->srot_map, priv->fields[TSENS_SW_RST]);
984 if (IS_ERR(priv->rf[TSENS_SW_RST])) {
985 ret = PTR_ERR(priv->rf[TSENS_SW_RST]);
989 priv->rf[TRDY] = devm_regmap_field_alloc(dev, priv->tm_map, priv->fields[TRDY]);
990 if (IS_ERR(priv->rf[TRDY])) {
991 ret = PTR_ERR(priv->rf[TRDY]);
995 /* This loop might need changes if enum regfield_ids is reordered */
996 for (j = LAST_TEMP_0; j <= UP_THRESH_15; j += 16) {
997 for (i = 0; i < priv->feat->max_sensors; i++) {
1000 priv->rf[idx] = devm_regmap_field_alloc(dev,
1003 if (IS_ERR(priv->rf[idx])) {
1004 ret = PTR_ERR(priv->rf[idx]);
1005 goto err_put_device;
1010 if (priv->feat->crit_int || tsens_version(priv) < VER_0_1) {
1011 /* Loop might need changes if enum regfield_ids is reordered */
1012 for (j = CRITICAL_STATUS_0; j <= CRIT_THRESH_15; j += 16) {
1013 for (i = 0; i < priv->feat->max_sensors; i++) {
1017 devm_regmap_field_alloc(dev,
1020 if (IS_ERR(priv->rf[idx])) {
1021 ret = PTR_ERR(priv->rf[idx]);
1022 goto err_put_device;
1028 if (tsens_version(priv) > VER_1_X && ver_minor > 2) {
1029 /* Watchdog is present only on v2.3+ */
1030 priv->feat->has_watchdog = 1;
1031 for (i = WDOG_BARK_STATUS; i <= CC_MON_MASK; i++) {
1032 priv->rf[i] = devm_regmap_field_alloc(dev, priv->tm_map,
1034 if (IS_ERR(priv->rf[i])) {
1035 ret = PTR_ERR(priv->rf[i]);
1036 goto err_put_device;
1040 * Watchdog is already enabled, unmask the bark.
1041 * Disable cycle completion monitoring
1043 regmap_field_write(priv->rf[WDOG_BARK_MASK], 0);
1044 regmap_field_write(priv->rf[CC_MON_MASK], 1);
1047 spin_lock_init(&priv->ul_lock);
1049 /* VER_0 interrupt doesn't need to be enabled */
1050 if (tsens_version(priv) >= VER_0_1)
1051 tsens_enable_irq(priv);
1054 put_device(&op->dev);
1058 static int tsens_get_temp(struct thermal_zone_device *tz, int *temp)
1060 struct tsens_sensor *s = thermal_zone_device_priv(tz);
1061 struct tsens_priv *priv = s->priv;
1063 return priv->ops->get_temp(s, temp);
1066 static int __maybe_unused tsens_suspend(struct device *dev)
1068 struct tsens_priv *priv = dev_get_drvdata(dev);
1070 if (priv->ops && priv->ops->suspend)
1071 return priv->ops->suspend(priv);
1076 static int __maybe_unused tsens_resume(struct device *dev)
1078 struct tsens_priv *priv = dev_get_drvdata(dev);
1080 if (priv->ops && priv->ops->resume)
1081 return priv->ops->resume(priv);
1086 static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume);
1088 static const struct of_device_id tsens_table[] = {
1090 .compatible = "qcom,ipq8064-tsens",
1093 .compatible = "qcom,ipq8074-tsens",
1094 .data = &data_ipq8074,
1096 .compatible = "qcom,mdm9607-tsens",
1099 .compatible = "qcom,msm8916-tsens",
1102 .compatible = "qcom,msm8939-tsens",
1105 .compatible = "qcom,msm8956-tsens",
1108 .compatible = "qcom,msm8960-tsens",
1111 .compatible = "qcom,msm8974-tsens",
1114 .compatible = "qcom,msm8976-tsens",
1117 .compatible = "qcom,msm8996-tsens",
1120 .compatible = "qcom,tsens-v1",
1121 .data = &data_tsens_v1,
1123 .compatible = "qcom,tsens-v2",
1124 .data = &data_tsens_v2,
1128 MODULE_DEVICE_TABLE(of, tsens_table);
1130 static const struct thermal_zone_device_ops tsens_of_ops = {
1131 .get_temp = tsens_get_temp,
1132 .set_trips = tsens_set_trips,
1135 static int tsens_register_irq(struct tsens_priv *priv, char *irqname,
1136 irq_handler_t thread_fn)
1138 struct platform_device *pdev;
1141 pdev = of_find_device_by_node(priv->dev->of_node);
1145 irq = platform_get_irq_byname(pdev, irqname);
1148 /* For old DTs with no IRQ defined */
1152 /* VER_0 interrupt is TRIGGER_RISING, VER_0_1 and up is ONESHOT */
1153 if (tsens_version(priv) == VER_0)
1154 ret = devm_request_threaded_irq(&pdev->dev, irq,
1156 IRQF_TRIGGER_RISING,
1157 dev_name(&pdev->dev),
1160 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1161 thread_fn, IRQF_ONESHOT,
1162 dev_name(&pdev->dev),
1166 dev_err(&pdev->dev, "%s: failed to get irq\n",
1169 enable_irq_wake(irq);
1172 put_device(&pdev->dev);
1176 static int tsens_register(struct tsens_priv *priv)
1179 struct thermal_zone_device *tzd;
1181 for (i = 0; i < priv->num_sensors; i++) {
1182 priv->sensor[i].priv = priv;
1183 tzd = devm_thermal_of_zone_register(priv->dev, priv->sensor[i].hw_id,
1188 priv->sensor[i].tzd = tzd;
1189 if (priv->ops->enable)
1190 priv->ops->enable(priv, i);
1192 if (devm_thermal_add_hwmon_sysfs(priv->dev, tzd))
1194 "Failed to add hwmon sysfs attributes\n");
1197 /* VER_0 require to set MIN and MAX THRESH
1198 * These 2 regs are set using the:
1199 * - CRIT_THRESH_0 for MAX THRESH hardcoded to 120°C
1200 * - CRIT_THRESH_1 for MIN THRESH hardcoded to 0°C
1202 if (tsens_version(priv) < VER_0_1) {
1203 regmap_field_write(priv->rf[CRIT_THRESH_0],
1204 tsens_mC_to_hw(priv->sensor, 120000));
1206 regmap_field_write(priv->rf[CRIT_THRESH_1],
1207 tsens_mC_to_hw(priv->sensor, 0));
1210 if (priv->feat->combo_int) {
1211 ret = tsens_register_irq(priv, "combined",
1212 tsens_combined_irq_thread);
1214 ret = tsens_register_irq(priv, "uplow", tsens_irq_thread);
1218 if (priv->feat->crit_int)
1219 ret = tsens_register_irq(priv, "critical",
1220 tsens_critical_irq_thread);
1226 static int tsens_probe(struct platform_device *pdev)
1230 struct device_node *np;
1231 struct tsens_priv *priv;
1232 const struct tsens_plat_data *data;
1233 const struct of_device_id *id;
1236 if (pdev->dev.of_node)
1239 dev = pdev->dev.parent;
1243 id = of_match_node(tsens_table, np);
1249 num_sensors = data->num_sensors;
1252 of_property_read_u32(np, "#qcom,sensors", &num_sensors);
1254 if (num_sensors <= 0) {
1255 dev_err(dev, "%s: invalid number of sensors\n", __func__);
1259 priv = devm_kzalloc(dev,
1260 struct_size(priv, sensor, num_sensors),
1266 priv->num_sensors = num_sensors;
1267 priv->ops = data->ops;
1268 for (i = 0; i < priv->num_sensors; i++) {
1270 priv->sensor[i].hw_id = data->hw_ids[i];
1272 priv->sensor[i].hw_id = i;
1274 priv->feat = data->feat;
1275 priv->fields = data->fields;
1277 platform_set_drvdata(pdev, priv);
1279 if (!priv->ops || !priv->ops->init || !priv->ops->get_temp)
1282 ret = priv->ops->init(priv);
1284 dev_err(dev, "%s: init failed\n", __func__);
1288 if (priv->ops->calibrate) {
1289 ret = priv->ops->calibrate(priv);
1291 if (ret != -EPROBE_DEFER)
1292 dev_err(dev, "%s: calibration failed\n", __func__);
1297 ret = tsens_register(priv);
1299 tsens_debug_init(pdev);
1304 static int tsens_remove(struct platform_device *pdev)
1306 struct tsens_priv *priv = platform_get_drvdata(pdev);
1308 debugfs_remove_recursive(priv->debug_root);
1309 tsens_disable_irq(priv);
1310 if (priv->ops->disable)
1311 priv->ops->disable(priv);
1316 static struct platform_driver tsens_driver = {
1317 .probe = tsens_probe,
1318 .remove = tsens_remove,
1320 .name = "qcom-tsens",
1321 .pm = &tsens_pm_ops,
1322 .of_match_table = tsens_table,
1325 module_platform_driver(tsens_driver);
1327 MODULE_LICENSE("GPL v2");
1328 MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
1329 MODULE_ALIAS("platform:qcom-tsens");