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>
21 #include <asm-generic/unaligned.h>
23 #include "../thermal_hwmon.h"
26 * Thermal monitoring block consists of 8 (ADC_TM5_NUM_CHANNELS) channels. Each
27 * channel is programmed to use one of ADC channels for voltage comparison.
28 * Voltages are programmed using ADC codes, so we have to convert temp to
29 * voltage and then to ADC code value.
31 * Configuration of TM channels must match configuration of corresponding ADC
35 #define ADC5_MAX_CHANNEL 0xc0
36 #define ADC_TM5_NUM_CHANNELS 8
38 #define ADC_TM5_STATUS_LOW 0x0a
40 #define ADC_TM5_STATUS_HIGH 0x0b
42 #define ADC_TM5_NUM_BTM 0x0f
44 #define ADC_TM5_ADC_DIG_PARAM 0x42
46 #define ADC_TM5_FAST_AVG_CTL (ADC_TM5_ADC_DIG_PARAM + 1)
47 #define ADC_TM5_FAST_AVG_EN BIT(7)
49 #define ADC_TM5_MEAS_INTERVAL_CTL (ADC_TM5_ADC_DIG_PARAM + 2)
50 #define ADC_TM5_TIMER1 3 /* 3.9ms */
52 #define ADC_TM5_MEAS_INTERVAL_CTL2 (ADC_TM5_ADC_DIG_PARAM + 3)
53 #define ADC_TM5_MEAS_INTERVAL_CTL2_MASK 0xf0
54 #define ADC_TM5_TIMER2 10 /* 1 second */
55 #define ADC_TM5_MEAS_INTERVAL_CTL3_MASK 0xf
56 #define ADC_TM5_TIMER3 4 /* 4 second */
58 #define ADC_TM_EN_CTL1 0x46
59 #define ADC_TM_EN BIT(7)
60 #define ADC_TM_CONV_REQ 0x47
61 #define ADC_TM_CONV_REQ_EN BIT(7)
63 #define ADC_TM5_M_CHAN_BASE 0x60
65 #define ADC_TM5_M_ADC_CH_SEL_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 0)
66 #define ADC_TM5_M_LOW_THR0(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 1)
67 #define ADC_TM5_M_LOW_THR1(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 2)
68 #define ADC_TM5_M_HIGH_THR0(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 3)
69 #define ADC_TM5_M_HIGH_THR1(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 4)
70 #define ADC_TM5_M_MEAS_INTERVAL_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 5)
71 #define ADC_TM5_M_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 6)
72 #define ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK 0xf
73 #define ADC_TM5_M_CTL_CAL_SEL_MASK 0x30
74 #define ADC_TM5_M_CTL_CAL_VAL 0x40
75 #define ADC_TM5_M_EN(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 7)
76 #define ADC_TM5_M_MEAS_EN BIT(7)
77 #define ADC_TM5_M_HIGH_THR_INT_EN BIT(1)
78 #define ADC_TM5_M_LOW_THR_INT_EN BIT(0)
80 #define ADC_TM_GEN2_STATUS1 0x08
81 #define ADC_TM_GEN2_STATUS_LOW_SET 0x09
82 #define ADC_TM_GEN2_STATUS_LOW_CLR 0x0a
83 #define ADC_TM_GEN2_STATUS_HIGH_SET 0x0b
84 #define ADC_TM_GEN2_STATUS_HIGH_CLR 0x0c
86 #define ADC_TM_GEN2_CFG_HS_SET 0x0d
87 #define ADC_TM_GEN2_CFG_HS_FLAG BIT(0)
88 #define ADC_TM_GEN2_CFG_HS_CLR 0x0e
90 #define ADC_TM_GEN2_SID 0x40
92 #define ADC_TM_GEN2_CH_CTL 0x41
93 #define ADC_TM_GEN2_TM_CH_SEL GENMASK(7, 5)
94 #define ADC_TM_GEN2_MEAS_INT_SEL GENMASK(3, 2)
96 #define ADC_TM_GEN2_ADC_DIG_PARAM 0x42
97 #define ADC_TM_GEN2_CTL_CAL_SEL GENMASK(5, 4)
98 #define ADC_TM_GEN2_CTL_DEC_RATIO_MASK GENMASK(3, 2)
100 #define ADC_TM_GEN2_FAST_AVG_CTL 0x43
101 #define ADC_TM_GEN2_FAST_AVG_EN BIT(7)
103 #define ADC_TM_GEN2_ADC_CH_SEL_CTL 0x44
105 #define ADC_TM_GEN2_DELAY_CTL 0x45
106 #define ADC_TM_GEN2_HW_SETTLE_DELAY GENMASK(3, 0)
108 #define ADC_TM_GEN2_EN_CTL1 0x46
109 #define ADC_TM_GEN2_EN BIT(7)
111 #define ADC_TM_GEN2_CONV_REQ 0x47
112 #define ADC_TM_GEN2_CONV_REQ_EN BIT(7)
114 #define ADC_TM_GEN2_LOW_THR0 0x49
115 #define ADC_TM_GEN2_LOW_THR1 0x4a
116 #define ADC_TM_GEN2_HIGH_THR0 0x4b
117 #define ADC_TM_GEN2_HIGH_THR1 0x4c
118 #define ADC_TM_GEN2_LOWER_MASK(n) ((n) & GENMASK(7, 0))
119 #define ADC_TM_GEN2_UPPER_MASK(n) (((n) & GENMASK(15, 8)) >> 8)
121 #define ADC_TM_GEN2_MEAS_IRQ_EN 0x4d
122 #define ADC_TM_GEN2_MEAS_EN BIT(7)
123 #define ADC_TM5_GEN2_HIGH_THR_INT_EN BIT(1)
124 #define ADC_TM5_GEN2_LOW_THR_INT_EN BIT(0)
126 #define ADC_TM_GEN2_MEAS_INT_LSB 0x50
127 #define ADC_TM_GEN2_MEAS_INT_MSB 0x51
128 #define ADC_TM_GEN2_MEAS_INT_MODE 0x52
130 #define ADC_TM_GEN2_Mn_DATA0(n) ((n * 2) + 0xa0)
131 #define ADC_TM_GEN2_Mn_DATA1(n) ((n * 2) + 0xa1)
132 #define ADC_TM_GEN2_DATA_SHIFT 8
134 enum adc5_timer_select {
135 ADC5_TIMER_SEL_1 = 0,
148 enum adc_tm5_cal_method {
150 ADC_TM5_RATIOMETRIC_CAL,
154 enum adc_tm_gen2_time_select {
163 struct adc_tm5_channel;
165 struct adc_tm5_data {
166 const u32 full_scale_code_volt;
167 unsigned int *decimation;
168 unsigned int *hw_settle;
169 int (*disable_channel)(struct adc_tm5_channel *channel);
170 int (*configure)(struct adc_tm5_channel *channel, int low, int high);
171 irqreturn_t (*isr)(int irq, void *data);
172 int (*init)(struct adc_tm5_chip *chip);
178 * struct adc_tm5_channel - ADC Thermal Monitoring channel data.
179 * @channel: channel number.
180 * @adc_channel: corresponding ADC channel number.
181 * @cal_method: calibration method.
182 * @prescale: channel scaling performed on the input signal.
183 * @hw_settle_time: the time between AMUX being configured and the
184 * start of conversion.
185 * @decimation: sampling rate supported for the channel.
186 * @avg_samples: ability to provide single result from the ADC
187 * that is an average of multiple measurements.
188 * @high_thr_en: channel upper voltage threshold enable state.
189 * @low_thr_en: channel lower voltage threshold enable state.
190 * @meas_en: recurring measurement enable state
191 * @iio: IIO channel instance used by this channel.
192 * @chip: ADC TM chip instance.
193 * @tzd: thermal zone device used by this channel.
195 struct adc_tm5_channel {
196 unsigned int channel;
197 unsigned int adc_channel;
198 enum adc_tm5_cal_method cal_method;
199 unsigned int prescale;
200 unsigned int hw_settle_time;
201 unsigned int decimation; /* For Gen2 ADC_TM */
202 unsigned int avg_samples; /* For Gen2 ADC_TM */
203 bool high_thr_en; /* For Gen2 ADC_TM */
204 bool low_thr_en; /* For Gen2 ADC_TM */
205 bool meas_en; /* For Gen2 ADC_TM */
206 struct iio_channel *iio;
207 struct adc_tm5_chip *chip;
208 struct thermal_zone_device *tzd;
212 * struct adc_tm5_chip - ADC Thermal Monitoring properties
213 * @regmap: SPMI ADC5 Thermal Monitoring peripheral register map field.
214 * @dev: SPMI ADC5 device.
215 * @data: software configuration data.
216 * @channels: array of ADC TM channel data.
217 * @nchannels: amount of channels defined/allocated
218 * @decimation: sampling rate supported for the channel.
219 * Applies to all channels, used only on Gen1 ADC_TM.
220 * @avg_samples: ability to provide single result from the ADC
221 * that is an average of multiple measurements. Applies to all
222 * channels, used only on Gen1 ADC_TM.
223 * @base: base address of TM registers.
224 * @adc_mutex_lock: ADC_TM mutex lock, used only on Gen2 ADC_TM.
225 * It is used to ensure only one ADC channel configuration
226 * is done at a time using the shared set of configuration
229 struct adc_tm5_chip {
230 struct regmap *regmap;
232 const struct adc_tm5_data *data;
233 struct adc_tm5_channel *channels;
234 unsigned int nchannels;
235 unsigned int decimation;
236 unsigned int avg_samples;
238 struct mutex adc_mutex_lock;
241 static int adc_tm5_read(struct adc_tm5_chip *adc_tm, u16 offset, u8 *data, int len)
243 return regmap_bulk_read(adc_tm->regmap, adc_tm->base + offset, data, len);
246 static int adc_tm5_write(struct adc_tm5_chip *adc_tm, u16 offset, u8 *data, int len)
248 return regmap_bulk_write(adc_tm->regmap, adc_tm->base + offset, data, len);
251 static int adc_tm5_reg_update(struct adc_tm5_chip *adc_tm, u16 offset, u8 mask, u8 val)
253 return regmap_write_bits(adc_tm->regmap, adc_tm->base + offset, mask, val);
256 static irqreturn_t adc_tm5_isr(int irq, void *data)
258 struct adc_tm5_chip *chip = data;
259 u8 status_low, status_high, ctl;
262 ret = adc_tm5_read(chip, ADC_TM5_STATUS_LOW, &status_low, sizeof(status_low));
264 dev_err(chip->dev, "read status low failed: %d\n", ret);
268 ret = adc_tm5_read(chip, ADC_TM5_STATUS_HIGH, &status_high, sizeof(status_high));
270 dev_err(chip->dev, "read status high failed: %d\n", ret);
274 for (i = 0; i < chip->nchannels; i++) {
275 bool upper_set = false, lower_set = false;
276 unsigned int ch = chip->channels[i].channel;
278 /* No TZD, we warned at the boot time */
279 if (!chip->channels[i].tzd)
282 ret = adc_tm5_read(chip, ADC_TM5_M_EN(ch), &ctl, sizeof(ctl));
284 dev_err(chip->dev, "ctl read failed: %d, channel %d\n", ret, i);
288 if (!(ctl & ADC_TM5_M_MEAS_EN))
291 lower_set = (status_low & BIT(ch)) &&
292 (ctl & ADC_TM5_M_LOW_THR_INT_EN);
294 upper_set = (status_high & BIT(ch)) &&
295 (ctl & ADC_TM5_M_HIGH_THR_INT_EN);
297 if (upper_set || lower_set)
298 thermal_zone_device_update(chip->channels[i].tzd,
299 THERMAL_EVENT_UNSPECIFIED);
305 static irqreturn_t adc_tm5_gen2_isr(int irq, void *data)
307 struct adc_tm5_chip *chip = data;
308 u8 status_low, status_high;
311 ret = adc_tm5_read(chip, ADC_TM_GEN2_STATUS_LOW_CLR, &status_low, sizeof(status_low));
313 dev_err(chip->dev, "read status_low failed: %d\n", ret);
317 ret = adc_tm5_read(chip, ADC_TM_GEN2_STATUS_HIGH_CLR, &status_high, sizeof(status_high));
319 dev_err(chip->dev, "read status_high failed: %d\n", ret);
323 ret = adc_tm5_write(chip, ADC_TM_GEN2_STATUS_LOW_CLR, &status_low, sizeof(status_low));
325 dev_err(chip->dev, "clear status low failed with %d\n", ret);
329 ret = adc_tm5_write(chip, ADC_TM_GEN2_STATUS_HIGH_CLR, &status_high, sizeof(status_high));
331 dev_err(chip->dev, "clear status high failed with %d\n", ret);
335 for (i = 0; i < chip->nchannels; i++) {
336 bool upper_set = false, lower_set = false;
337 unsigned int ch = chip->channels[i].channel;
339 /* No TZD, we warned at the boot time */
340 if (!chip->channels[i].tzd)
343 if (!chip->channels[i].meas_en)
346 lower_set = (status_low & BIT(ch)) &&
347 (chip->channels[i].low_thr_en);
349 upper_set = (status_high & BIT(ch)) &&
350 (chip->channels[i].high_thr_en);
352 if (upper_set || lower_set)
353 thermal_zone_device_update(chip->channels[i].tzd,
354 THERMAL_EVENT_UNSPECIFIED);
360 static int adc_tm5_get_temp(void *data, int *temp)
362 struct adc_tm5_channel *channel = data;
365 if (!channel || !channel->iio)
368 ret = iio_read_channel_processed(channel->iio, temp);
372 if (ret != IIO_VAL_INT)
378 static int adc_tm5_disable_channel(struct adc_tm5_channel *channel)
380 struct adc_tm5_chip *chip = channel->chip;
381 unsigned int reg = ADC_TM5_M_EN(channel->channel);
383 return adc_tm5_reg_update(chip, reg,
385 ADC_TM5_M_HIGH_THR_INT_EN |
386 ADC_TM5_M_LOW_THR_INT_EN,
390 #define ADC_TM_GEN2_POLL_DELAY_MIN_US 100
391 #define ADC_TM_GEN2_POLL_DELAY_MAX_US 110
392 #define ADC_TM_GEN2_POLL_RETRY_COUNT 3
394 static int32_t adc_tm5_gen2_conv_req(struct adc_tm5_chip *chip)
400 data = ADC_TM_GEN2_EN;
401 ret = adc_tm5_write(chip, ADC_TM_GEN2_EN_CTL1, &data, 1);
403 dev_err(chip->dev, "adc-tm enable failed with %d\n", ret);
407 data = ADC_TM_GEN2_CFG_HS_FLAG;
408 ret = adc_tm5_write(chip, ADC_TM_GEN2_CFG_HS_SET, &data, 1);
410 dev_err(chip->dev, "adc-tm handshake failed with %d\n", ret);
414 data = ADC_TM_GEN2_CONV_REQ_EN;
415 ret = adc_tm5_write(chip, ADC_TM_GEN2_CONV_REQ, &data, 1);
417 dev_err(chip->dev, "adc-tm request conversion failed with %d\n", ret);
422 * SW sets a handshake bit and waits for PBS to clear it
423 * before the next conversion request can be queued.
426 for (count = 0; count < ADC_TM_GEN2_POLL_RETRY_COUNT; count++) {
427 ret = adc_tm5_read(chip, ADC_TM_GEN2_CFG_HS_SET, &data, sizeof(data));
429 dev_err(chip->dev, "adc-tm read failed with %d\n", ret);
433 if (!(data & ADC_TM_GEN2_CFG_HS_FLAG))
435 usleep_range(ADC_TM_GEN2_POLL_DELAY_MIN_US,
436 ADC_TM_GEN2_POLL_DELAY_MAX_US);
439 dev_err(chip->dev, "adc-tm conversion request handshake timed out\n");
444 static int adc_tm5_gen2_disable_channel(struct adc_tm5_channel *channel)
446 struct adc_tm5_chip *chip = channel->chip;
450 mutex_lock(&chip->adc_mutex_lock);
452 channel->meas_en = false;
453 channel->high_thr_en = false;
454 channel->low_thr_en = false;
456 ret = adc_tm5_read(chip, ADC_TM_GEN2_CH_CTL, &val, sizeof(val));
458 dev_err(chip->dev, "adc-tm block read failed with %d\n", ret);
462 val &= ~ADC_TM_GEN2_TM_CH_SEL;
463 val |= FIELD_PREP(ADC_TM_GEN2_TM_CH_SEL, channel->channel);
465 ret = adc_tm5_write(chip, ADC_TM_GEN2_CH_CTL, &val, 1);
467 dev_err(chip->dev, "adc-tm channel disable failed with %d\n", ret);
472 ret = adc_tm5_write(chip, ADC_TM_GEN2_MEAS_IRQ_EN, &val, 1);
474 dev_err(chip->dev, "adc-tm interrupt disable failed with %d\n", ret);
479 ret = adc_tm5_gen2_conv_req(channel->chip);
481 dev_err(chip->dev, "adc-tm channel configure failed with %d\n", ret);
484 mutex_unlock(&chip->adc_mutex_lock);
488 static int adc_tm5_enable(struct adc_tm5_chip *chip)
494 ret = adc_tm5_write(chip, ADC_TM_EN_CTL1, &data, sizeof(data));
496 dev_err(chip->dev, "adc-tm enable failed\n");
500 data = ADC_TM_CONV_REQ_EN;
501 ret = adc_tm5_write(chip, ADC_TM_CONV_REQ, &data, sizeof(data));
503 dev_err(chip->dev, "adc-tm request conversion failed\n");
510 static int adc_tm5_configure(struct adc_tm5_channel *channel, int low, int high)
512 struct adc_tm5_chip *chip = channel->chip;
514 u16 reg = ADC_TM5_M_ADC_CH_SEL_CTL(channel->channel);
517 ret = adc_tm5_read(chip, reg, buf, sizeof(buf));
519 dev_err(chip->dev, "channel %d params read failed: %d\n", channel->channel, ret);
523 buf[0] = channel->adc_channel;
525 /* High temperature corresponds to low voltage threshold */
526 if (high != INT_MAX) {
527 u16 adc_code = qcom_adc_tm5_temp_volt_scale(channel->prescale,
528 chip->data->full_scale_code_volt, high);
530 put_unaligned_le16(adc_code, &buf[1]);
531 buf[7] |= ADC_TM5_M_LOW_THR_INT_EN;
533 buf[7] &= ~ADC_TM5_M_LOW_THR_INT_EN;
536 /* Low temperature corresponds to high voltage threshold */
537 if (low != -INT_MAX) {
538 u16 adc_code = qcom_adc_tm5_temp_volt_scale(channel->prescale,
539 chip->data->full_scale_code_volt, low);
541 put_unaligned_le16(adc_code, &buf[3]);
542 buf[7] |= ADC_TM5_M_HIGH_THR_INT_EN;
544 buf[7] &= ~ADC_TM5_M_HIGH_THR_INT_EN;
547 buf[5] = ADC5_TIMER_SEL_2;
549 /* Set calibration select, hw_settle delay */
550 buf[6] &= ~ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK;
551 buf[6] |= FIELD_PREP(ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK, channel->hw_settle_time);
552 buf[6] &= ~ADC_TM5_M_CTL_CAL_SEL_MASK;
553 buf[6] |= FIELD_PREP(ADC_TM5_M_CTL_CAL_SEL_MASK, channel->cal_method);
555 buf[7] |= ADC_TM5_M_MEAS_EN;
557 ret = adc_tm5_write(chip, reg, buf, sizeof(buf));
559 dev_err(chip->dev, "channel %d params write failed: %d\n", channel->channel, ret);
563 return adc_tm5_enable(chip);
566 static int adc_tm5_gen2_configure(struct adc_tm5_channel *channel, int low, int high)
568 struct adc_tm5_chip *chip = channel->chip;
573 mutex_lock(&chip->adc_mutex_lock);
575 channel->meas_en = true;
577 ret = adc_tm5_read(chip, ADC_TM_GEN2_SID, buf, sizeof(buf));
579 dev_err(chip->dev, "adc-tm block read failed with %d\n", ret);
583 /* Set SID from virtual channel number */
584 buf[0] = channel->adc_channel >> 8;
586 /* Set TM channel number used and measurement interval */
587 buf[1] &= ~ADC_TM_GEN2_TM_CH_SEL;
588 buf[1] |= FIELD_PREP(ADC_TM_GEN2_TM_CH_SEL, channel->channel);
589 buf[1] &= ~ADC_TM_GEN2_MEAS_INT_SEL;
590 buf[1] |= FIELD_PREP(ADC_TM_GEN2_MEAS_INT_SEL, MEAS_INT_1S);
592 buf[2] &= ~ADC_TM_GEN2_CTL_DEC_RATIO_MASK;
593 buf[2] |= FIELD_PREP(ADC_TM_GEN2_CTL_DEC_RATIO_MASK, channel->decimation);
594 buf[2] &= ~ADC_TM_GEN2_CTL_CAL_SEL;
595 buf[2] |= FIELD_PREP(ADC_TM_GEN2_CTL_CAL_SEL, channel->cal_method);
597 buf[3] = channel->avg_samples | ADC_TM_GEN2_FAST_AVG_EN;
599 buf[4] = channel->adc_channel & 0xff;
601 buf[5] = channel->hw_settle_time & ADC_TM_GEN2_HW_SETTLE_DELAY;
603 /* High temperature corresponds to low voltage threshold */
604 if (high != INT_MAX) {
605 channel->low_thr_en = true;
606 adc_code = qcom_adc_tm5_gen2_temp_res_scale(high);
607 put_unaligned_le16(adc_code, &buf[9]);
609 channel->low_thr_en = false;
612 /* Low temperature corresponds to high voltage threshold */
613 if (low != -INT_MAX) {
614 channel->high_thr_en = true;
615 adc_code = qcom_adc_tm5_gen2_temp_res_scale(low);
616 put_unaligned_le16(adc_code, &buf[11]);
618 channel->high_thr_en = false;
621 buf[13] = ADC_TM_GEN2_MEAS_EN;
622 if (channel->high_thr_en)
623 buf[13] |= ADC_TM5_GEN2_HIGH_THR_INT_EN;
624 if (channel->low_thr_en)
625 buf[13] |= ADC_TM5_GEN2_LOW_THR_INT_EN;
627 ret = adc_tm5_write(chip, ADC_TM_GEN2_SID, buf, sizeof(buf));
629 dev_err(chip->dev, "channel %d params write failed: %d\n", channel->channel, ret);
633 ret = adc_tm5_gen2_conv_req(channel->chip);
635 dev_err(chip->dev, "adc-tm channel configure failed with %d\n", ret);
638 mutex_unlock(&chip->adc_mutex_lock);
642 static int adc_tm5_set_trips(void *data, int low, int high)
644 struct adc_tm5_channel *channel = data;
645 struct adc_tm5_chip *chip;
651 chip = channel->chip;
652 dev_dbg(chip->dev, "%d:low(mdegC):%d, high(mdegC):%d\n",
653 channel->channel, low, high);
655 if (high == INT_MAX && low <= -INT_MAX)
656 ret = chip->data->disable_channel(channel);
658 ret = chip->data->configure(channel, low, high);
663 static struct thermal_zone_of_device_ops adc_tm5_thermal_ops = {
664 .get_temp = adc_tm5_get_temp,
665 .set_trips = adc_tm5_set_trips,
668 static int adc_tm5_register_tzd(struct adc_tm5_chip *adc_tm)
671 struct thermal_zone_device *tzd;
673 for (i = 0; i < adc_tm->nchannels; i++) {
674 adc_tm->channels[i].chip = adc_tm;
676 tzd = devm_thermal_zone_of_sensor_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_warn(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_of_iio_channel_get_by_name(adc_tm->dev, node, NULL);
834 if (IS_ERR(channel->iio)) {
835 ret = PTR_ERR(channel->iio);
836 if (ret != -EPROBE_DEFER)
837 dev_err(dev, "%s: error getting channel: %d\n", name, ret);
841 ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
843 ret = qcom_adc5_prescaling_from_dt(varr[0], varr[1]);
845 dev_err(dev, "%s: invalid pre-scaling <%d %d>\n",
846 name, varr[0], varr[1]);
849 channel->prescale = ret;
851 /* 1:1 prescale is index 0 */
852 channel->prescale = 0;
855 ret = of_property_read_u32(node, "qcom,hw-settle-time-us", &value);
857 ret = qcom_adc5_hw_settle_time_from_dt(value, adc_tm->data->hw_settle);
859 dev_err(dev, "%s invalid hw-settle-time-us %d us\n",
863 channel->hw_settle_time = ret;
865 channel->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
868 if (of_property_read_bool(node, "qcom,ratiometric"))
869 channel->cal_method = ADC_TM5_RATIOMETRIC_CAL;
871 channel->cal_method = ADC_TM5_ABSOLUTE_CAL;
873 if (adc_tm->data->gen == ADC_TM5_GEN2) {
874 ret = of_property_read_u32(node, "qcom,decimation", &value);
876 ret = qcom_adc5_decimation_from_dt(value, adc_tm->data->decimation);
878 dev_err(dev, "invalid decimation %d\n", value);
881 channel->decimation = ret;
883 channel->decimation = ADC5_DECIMATION_DEFAULT;
886 ret = of_property_read_u32(node, "qcom,avg-samples", &value);
888 ret = qcom_adc5_avg_samples_from_dt(value);
890 dev_err(dev, "invalid avg-samples %d\n", value);
893 channel->avg_samples = ret;
895 channel->avg_samples = VADC_DEF_AVG_SAMPLES;
902 static const struct adc_tm5_data adc_tm5_data_pmic = {
903 .full_scale_code_volt = 0x70e4,
904 .decimation = (unsigned int []) { 250, 420, 840 },
905 .hw_settle = (unsigned int []) { 15, 100, 200, 300, 400, 500, 600, 700,
906 1000, 2000, 4000, 8000, 16000, 32000,
908 .disable_channel = adc_tm5_disable_channel,
909 .configure = adc_tm5_configure,
911 .init = adc_tm5_init,
912 .irq_name = "pm-adc-tm5",
916 static const struct adc_tm5_data adc_tm_hc_data_pmic = {
917 .full_scale_code_volt = 0x70e4,
918 .decimation = (unsigned int []) { 256, 512, 1024 },
919 .hw_settle = (unsigned int []) { 0, 100, 200, 300, 400, 500, 600, 700,
920 1000, 2000, 4000, 6000, 8000, 10000 },
921 .disable_channel = adc_tm5_disable_channel,
922 .configure = adc_tm5_configure,
924 .init = adc_tm_hc_init,
925 .irq_name = "pm-adc-tm5",
929 static const struct adc_tm5_data adc_tm5_gen2_data_pmic = {
930 .full_scale_code_volt = 0x70e4,
931 .decimation = (unsigned int []) { 85, 340, 1360 },
932 .hw_settle = (unsigned int []) { 15, 100, 200, 300, 400, 500, 600, 700,
933 1000, 2000, 4000, 8000, 16000, 32000,
935 .disable_channel = adc_tm5_gen2_disable_channel,
936 .configure = adc_tm5_gen2_configure,
937 .isr = adc_tm5_gen2_isr,
938 .init = adc_tm5_gen2_init,
939 .irq_name = "pm-adc-tm5-gen2",
943 static int adc_tm5_get_dt_data(struct adc_tm5_chip *adc_tm, struct device_node *node)
945 struct adc_tm5_channel *channels;
946 struct device_node *child;
949 struct device *dev = adc_tm->dev;
951 adc_tm->nchannels = of_get_available_child_count(node);
952 if (!adc_tm->nchannels)
955 adc_tm->channels = devm_kcalloc(dev, adc_tm->nchannels,
956 sizeof(*adc_tm->channels), GFP_KERNEL);
957 if (!adc_tm->channels)
960 channels = adc_tm->channels;
962 adc_tm->data = of_device_get_match_data(dev);
964 adc_tm->data = &adc_tm5_data_pmic;
966 ret = of_property_read_u32(node, "qcom,decimation", &value);
968 ret = qcom_adc5_decimation_from_dt(value, adc_tm->data->decimation);
970 dev_err(dev, "invalid decimation %d\n", value);
973 adc_tm->decimation = ret;
975 adc_tm->decimation = ADC5_DECIMATION_DEFAULT;
978 ret = of_property_read_u32(node, "qcom,avg-samples", &value);
980 ret = qcom_adc5_avg_samples_from_dt(value);
982 dev_err(dev, "invalid avg-samples %d\n", value);
985 adc_tm->avg_samples = ret;
987 adc_tm->avg_samples = VADC_DEF_AVG_SAMPLES;
990 for_each_available_child_of_node(node, child) {
991 ret = adc_tm5_get_dt_channel_data(adc_tm, channels, child);
1003 static int adc_tm5_probe(struct platform_device *pdev)
1005 struct device_node *node = pdev->dev.of_node;
1006 struct device *dev = &pdev->dev;
1007 struct adc_tm5_chip *adc_tm;
1008 struct regmap *regmap;
1012 regmap = dev_get_regmap(dev->parent, NULL);
1016 ret = of_property_read_u32(node, "reg", ®);
1020 adc_tm = devm_kzalloc(&pdev->dev, sizeof(*adc_tm), GFP_KERNEL);
1024 adc_tm->regmap = regmap;
1028 irq = platform_get_irq(pdev, 0);
1030 dev_err(dev, "get_irq failed: %d\n", irq);
1034 ret = adc_tm5_get_dt_data(adc_tm, node);
1036 dev_err(dev, "get dt data failed: %d\n", ret);
1040 ret = adc_tm->data->init(adc_tm);
1042 dev_err(dev, "adc-tm init failed\n");
1046 ret = adc_tm5_register_tzd(adc_tm);
1048 dev_err(dev, "tzd register failed\n");
1052 return devm_request_threaded_irq(dev, irq, NULL, adc_tm->data->isr,
1053 IRQF_ONESHOT, adc_tm->data->irq_name, adc_tm);
1056 static const struct of_device_id adc_tm5_match_table[] = {
1058 .compatible = "qcom,spmi-adc-tm5",
1059 .data = &adc_tm5_data_pmic,
1062 .compatible = "qcom,spmi-adc-tm-hc",
1063 .data = &adc_tm_hc_data_pmic,
1066 .compatible = "qcom,spmi-adc-tm5-gen2",
1067 .data = &adc_tm5_gen2_data_pmic,
1071 MODULE_DEVICE_TABLE(of, adc_tm5_match_table);
1073 static struct platform_driver adc_tm5_driver = {
1075 .name = "qcom-spmi-adc-tm5",
1076 .of_match_table = adc_tm5_match_table,
1078 .probe = adc_tm5_probe,
1080 module_platform_driver(adc_tm5_driver);
1082 MODULE_DESCRIPTION("SPMI PMIC Thermal Monitor ADC driver");
1083 MODULE_LICENSE("GPL v2");