1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2020 Linaro Limited
5 * Based on original driver:
6 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
8 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
11 #include <linux/bitfield.h>
12 #include <linux/iio/adc/qcom-vadc-common.h>
13 #include <linux/iio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/thermal.h>
22 #include <asm/unaligned.h>
24 #include "../thermal_hwmon.h"
27 * Thermal monitoring block consists of 8 (ADC_TM5_NUM_CHANNELS) channels. Each
28 * channel is programmed to use one of ADC channels for voltage comparison.
29 * Voltages are programmed using ADC codes, so we have to convert temp to
30 * voltage and then to ADC code value.
32 * Configuration of TM channels must match configuration of corresponding ADC
36 #define ADC5_MAX_CHANNEL 0xc0
37 #define ADC_TM5_NUM_CHANNELS 8
39 #define ADC_TM5_STATUS_LOW 0x0a
41 #define ADC_TM5_STATUS_HIGH 0x0b
43 #define ADC_TM5_NUM_BTM 0x0f
45 #define ADC_TM5_ADC_DIG_PARAM 0x42
47 #define ADC_TM5_FAST_AVG_CTL (ADC_TM5_ADC_DIG_PARAM + 1)
48 #define ADC_TM5_FAST_AVG_EN BIT(7)
50 #define ADC_TM5_MEAS_INTERVAL_CTL (ADC_TM5_ADC_DIG_PARAM + 2)
51 #define ADC_TM5_TIMER1 3 /* 3.9ms */
53 #define ADC_TM5_MEAS_INTERVAL_CTL2 (ADC_TM5_ADC_DIG_PARAM + 3)
54 #define ADC_TM5_MEAS_INTERVAL_CTL2_MASK 0xf0
55 #define ADC_TM5_TIMER2 10 /* 1 second */
56 #define ADC_TM5_MEAS_INTERVAL_CTL3_MASK 0xf
57 #define ADC_TM5_TIMER3 4 /* 4 second */
59 #define ADC_TM_EN_CTL1 0x46
60 #define ADC_TM_EN BIT(7)
61 #define ADC_TM_CONV_REQ 0x47
62 #define ADC_TM_CONV_REQ_EN BIT(7)
64 #define ADC_TM5_M_CHAN_BASE 0x60
66 #define ADC_TM5_M_ADC_CH_SEL_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 0)
67 #define ADC_TM5_M_LOW_THR0(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 1)
68 #define ADC_TM5_M_LOW_THR1(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 2)
69 #define ADC_TM5_M_HIGH_THR0(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 3)
70 #define ADC_TM5_M_HIGH_THR1(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 4)
71 #define ADC_TM5_M_MEAS_INTERVAL_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 5)
72 #define ADC_TM5_M_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 6)
73 #define ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK 0xf
74 #define ADC_TM5_M_CTL_CAL_SEL_MASK 0x30
75 #define ADC_TM5_M_CTL_CAL_VAL 0x40
76 #define ADC_TM5_M_EN(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 7)
77 #define ADC_TM5_M_MEAS_EN BIT(7)
78 #define ADC_TM5_M_HIGH_THR_INT_EN BIT(1)
79 #define ADC_TM5_M_LOW_THR_INT_EN BIT(0)
81 #define ADC_TM_GEN2_STATUS1 0x08
82 #define ADC_TM_GEN2_STATUS_LOW_SET 0x09
83 #define ADC_TM_GEN2_STATUS_LOW_CLR 0x0a
84 #define ADC_TM_GEN2_STATUS_HIGH_SET 0x0b
85 #define ADC_TM_GEN2_STATUS_HIGH_CLR 0x0c
87 #define ADC_TM_GEN2_CFG_HS_SET 0x0d
88 #define ADC_TM_GEN2_CFG_HS_FLAG BIT(0)
89 #define ADC_TM_GEN2_CFG_HS_CLR 0x0e
91 #define ADC_TM_GEN2_SID 0x40
93 #define ADC_TM_GEN2_CH_CTL 0x41
94 #define ADC_TM_GEN2_TM_CH_SEL GENMASK(7, 5)
95 #define ADC_TM_GEN2_MEAS_INT_SEL GENMASK(3, 2)
97 #define ADC_TM_GEN2_ADC_DIG_PARAM 0x42
98 #define ADC_TM_GEN2_CTL_CAL_SEL GENMASK(5, 4)
99 #define ADC_TM_GEN2_CTL_DEC_RATIO_MASK GENMASK(3, 2)
101 #define ADC_TM_GEN2_FAST_AVG_CTL 0x43
102 #define ADC_TM_GEN2_FAST_AVG_EN BIT(7)
104 #define ADC_TM_GEN2_ADC_CH_SEL_CTL 0x44
106 #define ADC_TM_GEN2_DELAY_CTL 0x45
107 #define ADC_TM_GEN2_HW_SETTLE_DELAY GENMASK(3, 0)
109 #define ADC_TM_GEN2_EN_CTL1 0x46
110 #define ADC_TM_GEN2_EN BIT(7)
112 #define ADC_TM_GEN2_CONV_REQ 0x47
113 #define ADC_TM_GEN2_CONV_REQ_EN BIT(7)
115 #define ADC_TM_GEN2_LOW_THR0 0x49
116 #define ADC_TM_GEN2_LOW_THR1 0x4a
117 #define ADC_TM_GEN2_HIGH_THR0 0x4b
118 #define ADC_TM_GEN2_HIGH_THR1 0x4c
119 #define ADC_TM_GEN2_LOWER_MASK(n) ((n) & GENMASK(7, 0))
120 #define ADC_TM_GEN2_UPPER_MASK(n) (((n) & GENMASK(15, 8)) >> 8)
122 #define ADC_TM_GEN2_MEAS_IRQ_EN 0x4d
123 #define ADC_TM_GEN2_MEAS_EN BIT(7)
124 #define ADC_TM5_GEN2_HIGH_THR_INT_EN BIT(1)
125 #define ADC_TM5_GEN2_LOW_THR_INT_EN BIT(0)
127 #define ADC_TM_GEN2_MEAS_INT_LSB 0x50
128 #define ADC_TM_GEN2_MEAS_INT_MSB 0x51
129 #define ADC_TM_GEN2_MEAS_INT_MODE 0x52
131 #define ADC_TM_GEN2_Mn_DATA0(n) ((n * 2) + 0xa0)
132 #define ADC_TM_GEN2_Mn_DATA1(n) ((n * 2) + 0xa1)
133 #define ADC_TM_GEN2_DATA_SHIFT 8
135 enum adc5_timer_select {
136 ADC5_TIMER_SEL_1 = 0,
149 enum adc_tm5_cal_method {
151 ADC_TM5_RATIOMETRIC_CAL,
155 enum adc_tm_gen2_time_select {
164 struct adc_tm5_channel;
166 struct adc_tm5_data {
167 const u32 full_scale_code_volt;
168 unsigned int *decimation;
169 unsigned int *hw_settle;
170 int (*disable_channel)(struct adc_tm5_channel *channel);
171 int (*configure)(struct adc_tm5_channel *channel, int low, int high);
172 irqreturn_t (*isr)(int irq, void *data);
173 int (*init)(struct adc_tm5_chip *chip);
179 * struct adc_tm5_channel - ADC Thermal Monitoring channel data.
180 * @channel: channel number.
181 * @adc_channel: corresponding ADC channel number.
182 * @cal_method: calibration method.
183 * @prescale: channel scaling performed on the input signal.
184 * @hw_settle_time: the time between AMUX being configured and the
185 * start of conversion.
186 * @decimation: sampling rate supported for the channel.
187 * @avg_samples: ability to provide single result from the ADC
188 * that is an average of multiple measurements.
189 * @high_thr_en: channel upper voltage threshold enable state.
190 * @low_thr_en: channel lower voltage threshold enable state.
191 * @meas_en: recurring measurement enable state
192 * @iio: IIO channel instance used by this channel.
193 * @chip: ADC TM chip instance.
194 * @tzd: thermal zone device used by this channel.
196 struct adc_tm5_channel {
197 unsigned int channel;
198 unsigned int adc_channel;
199 enum adc_tm5_cal_method cal_method;
200 unsigned int prescale;
201 unsigned int hw_settle_time;
202 unsigned int decimation; /* For Gen2 ADC_TM */
203 unsigned int avg_samples; /* For Gen2 ADC_TM */
204 bool high_thr_en; /* For Gen2 ADC_TM */
205 bool low_thr_en; /* For Gen2 ADC_TM */
206 bool meas_en; /* For Gen2 ADC_TM */
207 struct iio_channel *iio;
208 struct adc_tm5_chip *chip;
209 struct thermal_zone_device *tzd;
213 * struct adc_tm5_chip - ADC Thermal Monitoring properties
214 * @regmap: SPMI ADC5 Thermal Monitoring peripheral register map field.
215 * @dev: SPMI ADC5 device.
216 * @data: software configuration data.
217 * @channels: array of ADC TM channel data.
218 * @nchannels: amount of channels defined/allocated
219 * @decimation: sampling rate supported for the channel.
220 * Applies to all channels, used only on Gen1 ADC_TM.
221 * @avg_samples: ability to provide single result from the ADC
222 * that is an average of multiple measurements. Applies to all
223 * channels, used only on Gen1 ADC_TM.
224 * @base: base address of TM registers.
225 * @adc_mutex_lock: ADC_TM mutex lock, used only on Gen2 ADC_TM.
226 * It is used to ensure only one ADC channel configuration
227 * is done at a time using the shared set of configuration
230 struct adc_tm5_chip {
231 struct regmap *regmap;
233 const struct adc_tm5_data *data;
234 struct adc_tm5_channel *channels;
235 unsigned int nchannels;
236 unsigned int decimation;
237 unsigned int avg_samples;
239 struct mutex adc_mutex_lock;
242 static int adc_tm5_read(struct adc_tm5_chip *adc_tm, u16 offset, u8 *data, int len)
244 return regmap_bulk_read(adc_tm->regmap, adc_tm->base + offset, data, len);
247 static int adc_tm5_write(struct adc_tm5_chip *adc_tm, u16 offset, u8 *data, int len)
249 return regmap_bulk_write(adc_tm->regmap, adc_tm->base + offset, data, len);
252 static int adc_tm5_reg_update(struct adc_tm5_chip *adc_tm, u16 offset, u8 mask, u8 val)
254 return regmap_write_bits(adc_tm->regmap, adc_tm->base + offset, mask, val);
257 static irqreturn_t adc_tm5_isr(int irq, void *data)
259 struct adc_tm5_chip *chip = data;
260 u8 status_low, status_high, ctl;
263 ret = adc_tm5_read(chip, ADC_TM5_STATUS_LOW, &status_low, sizeof(status_low));
265 dev_err(chip->dev, "read status low failed: %d\n", ret);
269 ret = adc_tm5_read(chip, ADC_TM5_STATUS_HIGH, &status_high, sizeof(status_high));
271 dev_err(chip->dev, "read status high failed: %d\n", ret);
275 for (i = 0; i < chip->nchannels; i++) {
276 bool upper_set = false, lower_set = false;
277 unsigned int ch = chip->channels[i].channel;
279 /* No TZD, we warned at the boot time */
280 if (!chip->channels[i].tzd)
283 ret = adc_tm5_read(chip, ADC_TM5_M_EN(ch), &ctl, sizeof(ctl));
285 dev_err(chip->dev, "ctl read failed: %d, channel %d\n", ret, i);
289 if (!(ctl & ADC_TM5_M_MEAS_EN))
292 lower_set = (status_low & BIT(ch)) &&
293 (ctl & ADC_TM5_M_LOW_THR_INT_EN);
295 upper_set = (status_high & BIT(ch)) &&
296 (ctl & ADC_TM5_M_HIGH_THR_INT_EN);
298 if (upper_set || lower_set)
299 thermal_zone_device_update(chip->channels[i].tzd,
300 THERMAL_EVENT_UNSPECIFIED);
306 static irqreturn_t adc_tm5_gen2_isr(int irq, void *data)
308 struct adc_tm5_chip *chip = data;
309 u8 status_low, status_high;
312 ret = adc_tm5_read(chip, ADC_TM_GEN2_STATUS_LOW_CLR, &status_low, sizeof(status_low));
314 dev_err(chip->dev, "read status_low failed: %d\n", ret);
318 ret = adc_tm5_read(chip, ADC_TM_GEN2_STATUS_HIGH_CLR, &status_high, sizeof(status_high));
320 dev_err(chip->dev, "read status_high failed: %d\n", ret);
324 ret = adc_tm5_write(chip, ADC_TM_GEN2_STATUS_LOW_CLR, &status_low, sizeof(status_low));
326 dev_err(chip->dev, "clear status low failed with %d\n", ret);
330 ret = adc_tm5_write(chip, ADC_TM_GEN2_STATUS_HIGH_CLR, &status_high, sizeof(status_high));
332 dev_err(chip->dev, "clear status high failed with %d\n", ret);
336 for (i = 0; i < chip->nchannels; i++) {
337 bool upper_set = false, lower_set = false;
338 unsigned int ch = chip->channels[i].channel;
340 /* No TZD, we warned at the boot time */
341 if (!chip->channels[i].tzd)
344 if (!chip->channels[i].meas_en)
347 lower_set = (status_low & BIT(ch)) &&
348 (chip->channels[i].low_thr_en);
350 upper_set = (status_high & BIT(ch)) &&
351 (chip->channels[i].high_thr_en);
353 if (upper_set || lower_set)
354 thermal_zone_device_update(chip->channels[i].tzd,
355 THERMAL_EVENT_UNSPECIFIED);
361 static int adc_tm5_get_temp(struct thermal_zone_device *tz, int *temp)
363 struct adc_tm5_channel *channel = tz->devdata;
366 if (!channel || !channel->iio)
369 ret = iio_read_channel_processed(channel->iio, temp);
373 if (ret != IIO_VAL_INT)
379 static int adc_tm5_disable_channel(struct adc_tm5_channel *channel)
381 struct adc_tm5_chip *chip = channel->chip;
382 unsigned int reg = ADC_TM5_M_EN(channel->channel);
384 return adc_tm5_reg_update(chip, reg,
386 ADC_TM5_M_HIGH_THR_INT_EN |
387 ADC_TM5_M_LOW_THR_INT_EN,
391 #define ADC_TM_GEN2_POLL_DELAY_MIN_US 100
392 #define ADC_TM_GEN2_POLL_DELAY_MAX_US 110
393 #define ADC_TM_GEN2_POLL_RETRY_COUNT 3
395 static int32_t adc_tm5_gen2_conv_req(struct adc_tm5_chip *chip)
401 data = ADC_TM_GEN2_EN;
402 ret = adc_tm5_write(chip, ADC_TM_GEN2_EN_CTL1, &data, 1);
404 dev_err(chip->dev, "adc-tm enable failed with %d\n", ret);
408 data = ADC_TM_GEN2_CFG_HS_FLAG;
409 ret = adc_tm5_write(chip, ADC_TM_GEN2_CFG_HS_SET, &data, 1);
411 dev_err(chip->dev, "adc-tm handshake failed with %d\n", ret);
415 data = ADC_TM_GEN2_CONV_REQ_EN;
416 ret = adc_tm5_write(chip, ADC_TM_GEN2_CONV_REQ, &data, 1);
418 dev_err(chip->dev, "adc-tm request conversion failed with %d\n", ret);
423 * SW sets a handshake bit and waits for PBS to clear it
424 * before the next conversion request can be queued.
427 for (count = 0; count < ADC_TM_GEN2_POLL_RETRY_COUNT; count++) {
428 ret = adc_tm5_read(chip, ADC_TM_GEN2_CFG_HS_SET, &data, sizeof(data));
430 dev_err(chip->dev, "adc-tm read failed with %d\n", ret);
434 if (!(data & ADC_TM_GEN2_CFG_HS_FLAG))
436 usleep_range(ADC_TM_GEN2_POLL_DELAY_MIN_US,
437 ADC_TM_GEN2_POLL_DELAY_MAX_US);
440 dev_err(chip->dev, "adc-tm conversion request handshake timed out\n");
445 static int adc_tm5_gen2_disable_channel(struct adc_tm5_channel *channel)
447 struct adc_tm5_chip *chip = channel->chip;
451 mutex_lock(&chip->adc_mutex_lock);
453 channel->meas_en = false;
454 channel->high_thr_en = false;
455 channel->low_thr_en = false;
457 ret = adc_tm5_read(chip, ADC_TM_GEN2_CH_CTL, &val, sizeof(val));
459 dev_err(chip->dev, "adc-tm block read failed with %d\n", ret);
463 val &= ~ADC_TM_GEN2_TM_CH_SEL;
464 val |= FIELD_PREP(ADC_TM_GEN2_TM_CH_SEL, channel->channel);
466 ret = adc_tm5_write(chip, ADC_TM_GEN2_CH_CTL, &val, 1);
468 dev_err(chip->dev, "adc-tm channel disable failed with %d\n", ret);
473 ret = adc_tm5_write(chip, ADC_TM_GEN2_MEAS_IRQ_EN, &val, 1);
475 dev_err(chip->dev, "adc-tm interrupt disable failed with %d\n", ret);
480 ret = adc_tm5_gen2_conv_req(channel->chip);
482 dev_err(chip->dev, "adc-tm channel configure failed with %d\n", ret);
485 mutex_unlock(&chip->adc_mutex_lock);
489 static int adc_tm5_enable(struct adc_tm5_chip *chip)
495 ret = adc_tm5_write(chip, ADC_TM_EN_CTL1, &data, sizeof(data));
497 dev_err(chip->dev, "adc-tm enable failed\n");
501 data = ADC_TM_CONV_REQ_EN;
502 ret = adc_tm5_write(chip, ADC_TM_CONV_REQ, &data, sizeof(data));
504 dev_err(chip->dev, "adc-tm request conversion failed\n");
511 static int adc_tm5_configure(struct adc_tm5_channel *channel, int low, int high)
513 struct adc_tm5_chip *chip = channel->chip;
515 u16 reg = ADC_TM5_M_ADC_CH_SEL_CTL(channel->channel);
518 ret = adc_tm5_read(chip, reg, buf, sizeof(buf));
520 dev_err(chip->dev, "channel %d params read failed: %d\n", channel->channel, ret);
524 buf[0] = channel->adc_channel;
526 /* High temperature corresponds to low voltage threshold */
527 if (high != INT_MAX) {
528 u16 adc_code = qcom_adc_tm5_temp_volt_scale(channel->prescale,
529 chip->data->full_scale_code_volt, high);
531 put_unaligned_le16(adc_code, &buf[1]);
532 buf[7] |= ADC_TM5_M_LOW_THR_INT_EN;
534 buf[7] &= ~ADC_TM5_M_LOW_THR_INT_EN;
537 /* Low temperature corresponds to high voltage threshold */
538 if (low != -INT_MAX) {
539 u16 adc_code = qcom_adc_tm5_temp_volt_scale(channel->prescale,
540 chip->data->full_scale_code_volt, low);
542 put_unaligned_le16(adc_code, &buf[3]);
543 buf[7] |= ADC_TM5_M_HIGH_THR_INT_EN;
545 buf[7] &= ~ADC_TM5_M_HIGH_THR_INT_EN;
548 buf[5] = ADC5_TIMER_SEL_2;
550 /* Set calibration select, hw_settle delay */
551 buf[6] &= ~ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK;
552 buf[6] |= FIELD_PREP(ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK, channel->hw_settle_time);
553 buf[6] &= ~ADC_TM5_M_CTL_CAL_SEL_MASK;
554 buf[6] |= FIELD_PREP(ADC_TM5_M_CTL_CAL_SEL_MASK, channel->cal_method);
556 buf[7] |= ADC_TM5_M_MEAS_EN;
558 ret = adc_tm5_write(chip, reg, buf, sizeof(buf));
560 dev_err(chip->dev, "channel %d params write failed: %d\n", channel->channel, ret);
564 return adc_tm5_enable(chip);
567 static int adc_tm5_gen2_configure(struct adc_tm5_channel *channel, int low, int high)
569 struct adc_tm5_chip *chip = channel->chip;
574 mutex_lock(&chip->adc_mutex_lock);
576 channel->meas_en = true;
578 ret = adc_tm5_read(chip, ADC_TM_GEN2_SID, buf, sizeof(buf));
580 dev_err(chip->dev, "adc-tm block read failed with %d\n", ret);
584 /* Set SID from virtual channel number */
585 buf[0] = channel->adc_channel >> 8;
587 /* Set TM channel number used and measurement interval */
588 buf[1] &= ~ADC_TM_GEN2_TM_CH_SEL;
589 buf[1] |= FIELD_PREP(ADC_TM_GEN2_TM_CH_SEL, channel->channel);
590 buf[1] &= ~ADC_TM_GEN2_MEAS_INT_SEL;
591 buf[1] |= FIELD_PREP(ADC_TM_GEN2_MEAS_INT_SEL, MEAS_INT_1S);
593 buf[2] &= ~ADC_TM_GEN2_CTL_DEC_RATIO_MASK;
594 buf[2] |= FIELD_PREP(ADC_TM_GEN2_CTL_DEC_RATIO_MASK, channel->decimation);
595 buf[2] &= ~ADC_TM_GEN2_CTL_CAL_SEL;
596 buf[2] |= FIELD_PREP(ADC_TM_GEN2_CTL_CAL_SEL, channel->cal_method);
598 buf[3] = channel->avg_samples | ADC_TM_GEN2_FAST_AVG_EN;
600 buf[4] = channel->adc_channel & 0xff;
602 buf[5] = channel->hw_settle_time & ADC_TM_GEN2_HW_SETTLE_DELAY;
604 /* High temperature corresponds to low voltage threshold */
605 if (high != INT_MAX) {
606 channel->low_thr_en = true;
607 adc_code = qcom_adc_tm5_gen2_temp_res_scale(high);
608 put_unaligned_le16(adc_code, &buf[9]);
610 channel->low_thr_en = false;
613 /* Low temperature corresponds to high voltage threshold */
614 if (low != -INT_MAX) {
615 channel->high_thr_en = true;
616 adc_code = qcom_adc_tm5_gen2_temp_res_scale(low);
617 put_unaligned_le16(adc_code, &buf[11]);
619 channel->high_thr_en = false;
622 buf[13] = ADC_TM_GEN2_MEAS_EN;
623 if (channel->high_thr_en)
624 buf[13] |= ADC_TM5_GEN2_HIGH_THR_INT_EN;
625 if (channel->low_thr_en)
626 buf[13] |= ADC_TM5_GEN2_LOW_THR_INT_EN;
628 ret = adc_tm5_write(chip, ADC_TM_GEN2_SID, buf, sizeof(buf));
630 dev_err(chip->dev, "channel %d params write failed: %d\n", channel->channel, ret);
634 ret = adc_tm5_gen2_conv_req(channel->chip);
636 dev_err(chip->dev, "adc-tm channel configure failed with %d\n", ret);
639 mutex_unlock(&chip->adc_mutex_lock);
643 static int adc_tm5_set_trips(struct thermal_zone_device *tz, int low, int high)
645 struct adc_tm5_channel *channel = tz->devdata;
646 struct adc_tm5_chip *chip;
652 chip = channel->chip;
653 dev_dbg(chip->dev, "%d:low(mdegC):%d, high(mdegC):%d\n",
654 channel->channel, low, high);
656 if (high == INT_MAX && low <= -INT_MAX)
657 ret = chip->data->disable_channel(channel);
659 ret = chip->data->configure(channel, low, high);
664 static const struct thermal_zone_device_ops adc_tm5_thermal_ops = {
665 .get_temp = adc_tm5_get_temp,
666 .set_trips = adc_tm5_set_trips,
669 static int adc_tm5_register_tzd(struct adc_tm5_chip *adc_tm)
672 struct thermal_zone_device *tzd;
674 for (i = 0; i < adc_tm->nchannels; i++) {
675 adc_tm->channels[i].chip = adc_tm;
676 tzd = devm_thermal_of_zone_register(adc_tm->dev,
677 adc_tm->channels[i].channel,
678 &adc_tm->channels[i],
679 &adc_tm5_thermal_ops);
681 if (PTR_ERR(tzd) == -ENODEV) {
682 dev_dbg(adc_tm->dev, "thermal sensor on channel %d is not used\n",
683 adc_tm->channels[i].channel);
687 dev_err(adc_tm->dev, "Error registering TZ zone for channel %d: %ld\n",
688 adc_tm->channels[i].channel, PTR_ERR(tzd));
691 adc_tm->channels[i].tzd = tzd;
692 if (devm_thermal_add_hwmon_sysfs(tzd))
693 dev_warn(adc_tm->dev,
694 "Failed to add hwmon sysfs attributes\n");
700 static int adc_tm_hc_init(struct adc_tm5_chip *chip)
706 for (i = 0; i < chip->nchannels; i++) {
707 if (chip->channels[i].channel >= ADC_TM5_NUM_CHANNELS) {
708 dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
713 buf[0] = chip->decimation;
714 buf[1] = chip->avg_samples | ADC_TM5_FAST_AVG_EN;
716 ret = adc_tm5_write(chip, ADC_TM5_ADC_DIG_PARAM, buf, sizeof(buf));
718 dev_err(chip->dev, "block write failed: %d\n", ret);
723 static int adc_tm5_init(struct adc_tm5_chip *chip)
725 u8 buf[4], channels_available;
729 ret = adc_tm5_read(chip, ADC_TM5_NUM_BTM,
730 &channels_available, sizeof(channels_available));
732 dev_err(chip->dev, "read failed for BTM channels\n");
736 for (i = 0; i < chip->nchannels; i++) {
737 if (chip->channels[i].channel >= channels_available) {
738 dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
743 buf[0] = chip->decimation;
744 buf[1] = chip->avg_samples | ADC_TM5_FAST_AVG_EN;
745 buf[2] = ADC_TM5_TIMER1;
746 buf[3] = FIELD_PREP(ADC_TM5_MEAS_INTERVAL_CTL2_MASK, ADC_TM5_TIMER2) |
747 FIELD_PREP(ADC_TM5_MEAS_INTERVAL_CTL3_MASK, ADC_TM5_TIMER3);
749 ret = adc_tm5_write(chip, ADC_TM5_ADC_DIG_PARAM, buf, sizeof(buf));
751 dev_err(chip->dev, "block write failed: %d\n", ret);
758 static int adc_tm5_gen2_init(struct adc_tm5_chip *chip)
760 u8 channels_available;
764 ret = adc_tm5_read(chip, ADC_TM5_NUM_BTM,
765 &channels_available, sizeof(channels_available));
767 dev_err(chip->dev, "read failed for BTM channels\n");
771 for (i = 0; i < chip->nchannels; i++) {
772 if (chip->channels[i].channel >= channels_available) {
773 dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
778 mutex_init(&chip->adc_mutex_lock);
783 static int adc_tm5_get_dt_channel_data(struct adc_tm5_chip *adc_tm,
784 struct adc_tm5_channel *channel,
785 struct device_node *node)
787 const char *name = node->name;
788 u32 chan, value, adc_channel, varr[2];
790 struct device *dev = adc_tm->dev;
791 struct of_phandle_args args;
793 ret = of_property_read_u32(node, "reg", &chan);
795 dev_err(dev, "%s: invalid channel number %d\n", name, ret);
799 if (chan >= ADC_TM5_NUM_CHANNELS) {
800 dev_err(dev, "%s: channel number too big: %d\n", name, chan);
804 channel->channel = chan;
807 * We are tied to PMIC's ADC controller, which always use single
808 * argument for channel number. So don't bother parsing
809 * #io-channel-cells, just enforce cell_count = 1.
811 ret = of_parse_phandle_with_fixed_args(node, "io-channels", 1, 0, &args);
813 dev_err(dev, "%s: error parsing ADC channel number %d: %d\n", name, chan, ret);
816 of_node_put(args.np);
818 if (args.args_count != 1) {
819 dev_err(dev, "%s: invalid args count for ADC channel %d\n", name, chan);
823 adc_channel = args.args[0];
824 if (adc_tm->data->gen == ADC_TM5_GEN2)
827 if (adc_channel >= ADC5_MAX_CHANNEL) {
828 dev_err(dev, "%s: invalid ADC channel number %d\n", name, chan);
831 channel->adc_channel = args.args[0];
833 channel->iio = devm_fwnode_iio_channel_get_by_name(adc_tm->dev,
834 of_fwnode_handle(node), NULL);
835 if (IS_ERR(channel->iio)) {
836 ret = PTR_ERR(channel->iio);
837 if (ret != -EPROBE_DEFER)
838 dev_err(dev, "%s: error getting channel: %d\n", name, ret);
842 ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
844 ret = qcom_adc5_prescaling_from_dt(varr[0], varr[1]);
846 dev_err(dev, "%s: invalid pre-scaling <%d %d>\n",
847 name, varr[0], varr[1]);
850 channel->prescale = ret;
852 /* 1:1 prescale is index 0 */
853 channel->prescale = 0;
856 ret = of_property_read_u32(node, "qcom,hw-settle-time-us", &value);
858 ret = qcom_adc5_hw_settle_time_from_dt(value, adc_tm->data->hw_settle);
860 dev_err(dev, "%s invalid hw-settle-time-us %d us\n",
864 channel->hw_settle_time = ret;
866 channel->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
869 if (of_property_read_bool(node, "qcom,ratiometric"))
870 channel->cal_method = ADC_TM5_RATIOMETRIC_CAL;
872 channel->cal_method = ADC_TM5_ABSOLUTE_CAL;
874 if (adc_tm->data->gen == ADC_TM5_GEN2) {
875 ret = of_property_read_u32(node, "qcom,decimation", &value);
877 ret = qcom_adc5_decimation_from_dt(value, adc_tm->data->decimation);
879 dev_err(dev, "invalid decimation %d\n", value);
882 channel->decimation = ret;
884 channel->decimation = ADC5_DECIMATION_DEFAULT;
887 ret = of_property_read_u32(node, "qcom,avg-samples", &value);
889 ret = qcom_adc5_avg_samples_from_dt(value);
891 dev_err(dev, "invalid avg-samples %d\n", value);
894 channel->avg_samples = ret;
896 channel->avg_samples = VADC_DEF_AVG_SAMPLES;
903 static const struct adc_tm5_data adc_tm5_data_pmic = {
904 .full_scale_code_volt = 0x70e4,
905 .decimation = (unsigned int []) { 250, 420, 840 },
906 .hw_settle = (unsigned int []) { 15, 100, 200, 300, 400, 500, 600, 700,
907 1000, 2000, 4000, 8000, 16000, 32000,
909 .disable_channel = adc_tm5_disable_channel,
910 .configure = adc_tm5_configure,
912 .init = adc_tm5_init,
913 .irq_name = "pm-adc-tm5",
917 static const struct adc_tm5_data adc_tm_hc_data_pmic = {
918 .full_scale_code_volt = 0x70e4,
919 .decimation = (unsigned int []) { 256, 512, 1024 },
920 .hw_settle = (unsigned int []) { 0, 100, 200, 300, 400, 500, 600, 700,
921 1000, 2000, 4000, 6000, 8000, 10000 },
922 .disable_channel = adc_tm5_disable_channel,
923 .configure = adc_tm5_configure,
925 .init = adc_tm_hc_init,
926 .irq_name = "pm-adc-tm5",
930 static const struct adc_tm5_data adc_tm5_gen2_data_pmic = {
931 .full_scale_code_volt = 0x70e4,
932 .decimation = (unsigned int []) { 85, 340, 1360 },
933 .hw_settle = (unsigned int []) { 15, 100, 200, 300, 400, 500, 600, 700,
934 1000, 2000, 4000, 8000, 16000, 32000,
936 .disable_channel = adc_tm5_gen2_disable_channel,
937 .configure = adc_tm5_gen2_configure,
938 .isr = adc_tm5_gen2_isr,
939 .init = adc_tm5_gen2_init,
940 .irq_name = "pm-adc-tm5-gen2",
944 static int adc_tm5_get_dt_data(struct adc_tm5_chip *adc_tm, struct device_node *node)
946 struct adc_tm5_channel *channels;
947 struct device_node *child;
950 struct device *dev = adc_tm->dev;
952 adc_tm->nchannels = of_get_available_child_count(node);
953 if (!adc_tm->nchannels)
956 adc_tm->channels = devm_kcalloc(dev, adc_tm->nchannels,
957 sizeof(*adc_tm->channels), GFP_KERNEL);
958 if (!adc_tm->channels)
961 channels = adc_tm->channels;
963 adc_tm->data = of_device_get_match_data(dev);
965 adc_tm->data = &adc_tm5_data_pmic;
967 ret = of_property_read_u32(node, "qcom,decimation", &value);
969 ret = qcom_adc5_decimation_from_dt(value, adc_tm->data->decimation);
971 dev_err(dev, "invalid decimation %d\n", value);
974 adc_tm->decimation = ret;
976 adc_tm->decimation = ADC5_DECIMATION_DEFAULT;
979 ret = of_property_read_u32(node, "qcom,avg-samples", &value);
981 ret = qcom_adc5_avg_samples_from_dt(value);
983 dev_err(dev, "invalid avg-samples %d\n", value);
986 adc_tm->avg_samples = ret;
988 adc_tm->avg_samples = VADC_DEF_AVG_SAMPLES;
991 for_each_available_child_of_node(node, child) {
992 ret = adc_tm5_get_dt_channel_data(adc_tm, channels, child);
1004 static int adc_tm5_probe(struct platform_device *pdev)
1006 struct device_node *node = pdev->dev.of_node;
1007 struct device *dev = &pdev->dev;
1008 struct adc_tm5_chip *adc_tm;
1009 struct regmap *regmap;
1013 regmap = dev_get_regmap(dev->parent, NULL);
1017 ret = of_property_read_u32(node, "reg", ®);
1021 adc_tm = devm_kzalloc(&pdev->dev, sizeof(*adc_tm), GFP_KERNEL);
1025 adc_tm->regmap = regmap;
1029 irq = platform_get_irq(pdev, 0);
1033 ret = adc_tm5_get_dt_data(adc_tm, node);
1035 return dev_err_probe(dev, ret, "get dt data failed\n");
1037 ret = adc_tm->data->init(adc_tm);
1039 dev_err(dev, "adc-tm init failed\n");
1043 ret = adc_tm5_register_tzd(adc_tm);
1045 dev_err(dev, "tzd register failed\n");
1049 return devm_request_threaded_irq(dev, irq, NULL, adc_tm->data->isr,
1050 IRQF_ONESHOT, adc_tm->data->irq_name, adc_tm);
1053 static const struct of_device_id adc_tm5_match_table[] = {
1055 .compatible = "qcom,spmi-adc-tm5",
1056 .data = &adc_tm5_data_pmic,
1059 .compatible = "qcom,spmi-adc-tm-hc",
1060 .data = &adc_tm_hc_data_pmic,
1063 .compatible = "qcom,spmi-adc-tm5-gen2",
1064 .data = &adc_tm5_gen2_data_pmic,
1068 MODULE_DEVICE_TABLE(of, adc_tm5_match_table);
1070 static struct platform_driver adc_tm5_driver = {
1072 .name = "qcom-spmi-adc-tm5",
1073 .of_match_table = adc_tm5_match_table,
1075 .probe = adc_tm5_probe,
1077 module_platform_driver(adc_tm5_driver);
1079 MODULE_DESCRIPTION("SPMI PMIC Thermal Monitor ADC driver");
1080 MODULE_LICENSE("GPL v2");