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 #include <linux/bitfield.h>
9 #include <linux/iio/adc/qcom-vadc-common.h>
10 #include <linux/iio/consumer.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/thermal.h>
20 * Thermal monitoring block consists of 8 (ADC_TM5_NUM_CHANNELS) channels. Each
21 * channel is programmed to use one of ADC channels for voltage comparison.
22 * Voltages are programmed using ADC codes, so we have to convert temp to
23 * voltage and then to ADC code value.
25 * Configuration of TM channels must match configuration of corresponding ADC
29 #define ADC5_MAX_CHANNEL 0xc0
30 #define ADC_TM5_NUM_CHANNELS 8
32 #define ADC_TM5_STATUS_LOW 0x0a
34 #define ADC_TM5_STATUS_HIGH 0x0b
36 #define ADC_TM5_NUM_BTM 0x0f
38 #define ADC_TM5_ADC_DIG_PARAM 0x42
40 #define ADC_TM5_FAST_AVG_CTL (ADC_TM5_ADC_DIG_PARAM + 1)
41 #define ADC_TM5_FAST_AVG_EN BIT(7)
43 #define ADC_TM5_MEAS_INTERVAL_CTL (ADC_TM5_ADC_DIG_PARAM + 2)
44 #define ADC_TM5_TIMER1 3 /* 3.9ms */
46 #define ADC_TM5_MEAS_INTERVAL_CTL2 (ADC_TM5_ADC_DIG_PARAM + 3)
47 #define ADC_TM5_MEAS_INTERVAL_CTL2_MASK 0xf0
48 #define ADC_TM5_TIMER2 10 /* 1 second */
49 #define ADC_TM5_MEAS_INTERVAL_CTL3_MASK 0xf
50 #define ADC_TM5_TIMER3 4 /* 4 second */
52 #define ADC_TM_EN_CTL1 0x46
53 #define ADC_TM_EN BIT(7)
54 #define ADC_TM_CONV_REQ 0x47
55 #define ADC_TM_CONV_REQ_EN BIT(7)
57 #define ADC_TM5_M_CHAN_BASE 0x60
59 #define ADC_TM5_M_ADC_CH_SEL_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 0)
60 #define ADC_TM5_M_LOW_THR0(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 1)
61 #define ADC_TM5_M_LOW_THR1(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 2)
62 #define ADC_TM5_M_HIGH_THR0(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 3)
63 #define ADC_TM5_M_HIGH_THR1(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 4)
64 #define ADC_TM5_M_MEAS_INTERVAL_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 5)
65 #define ADC_TM5_M_CTL(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 6)
66 #define ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK 0xf
67 #define ADC_TM5_M_CTL_CAL_SEL_MASK 0x30
68 #define ADC_TM5_M_CTL_CAL_VAL 0x40
69 #define ADC_TM5_M_EN(n) (ADC_TM5_M_CHAN_BASE + ((n) * 8) + 7)
70 #define ADC_TM5_M_MEAS_EN BIT(7)
71 #define ADC_TM5_M_HIGH_THR_INT_EN BIT(1)
72 #define ADC_TM5_M_LOW_THR_INT_EN BIT(0)
74 enum adc5_timer_select {
82 const u32 full_scale_code_volt;
83 unsigned int *decimation;
84 unsigned int *hw_settle;
88 enum adc_tm5_cal_method {
90 ADC_TM5_RATIOMETRIC_CAL,
97 * struct adc_tm5_channel - ADC Thermal Monitoring channel data.
98 * @channel: channel number.
99 * @adc_channel: corresponding ADC channel number.
100 * @cal_method: calibration method.
101 * @prescale: channel scaling performed on the input signal.
102 * @hw_settle_time: the time between AMUX being configured and the
103 * start of conversion.
104 * @iio: IIO channel instance used by this channel.
105 * @chip: ADC TM chip instance.
106 * @tzd: thermal zone device used by this channel.
108 struct adc_tm5_channel {
109 unsigned int channel;
110 unsigned int adc_channel;
111 enum adc_tm5_cal_method cal_method;
112 unsigned int prescale;
113 unsigned int hw_settle_time;
114 struct iio_channel *iio;
115 struct adc_tm5_chip *chip;
116 struct thermal_zone_device *tzd;
120 * struct adc_tm5_chip - ADC Thermal Monitoring properties
121 * @regmap: SPMI ADC5 Thermal Monitoring peripheral register map field.
122 * @dev: SPMI ADC5 device.
123 * @data: software configuration data.
124 * @channels: array of ADC TM channel data.
125 * @nchannels: amount of channels defined/allocated
126 * @decimation: sampling rate supported for the channel.
127 * @avg_samples: ability to provide single result from the ADC
128 * that is an average of multiple measurements.
129 * @base: base address of TM registers.
131 struct adc_tm5_chip {
132 struct regmap *regmap;
134 const struct adc_tm5_data *data;
135 struct adc_tm5_channel *channels;
136 unsigned int nchannels;
137 unsigned int decimation;
138 unsigned int avg_samples;
142 static const struct adc_tm5_data adc_tm5_data_pmic = {
143 .full_scale_code_volt = 0x70e4,
144 .decimation = (unsigned int []) { 250, 420, 840 },
145 .hw_settle = (unsigned int []) { 15, 100, 200, 300, 400, 500, 600, 700,
146 1000, 2000, 4000, 8000, 16000, 32000,
150 static const struct adc_tm5_data adc_tm_hc_data_pmic = {
151 .full_scale_code_volt = 0x70e4,
152 .decimation = (unsigned int []) { 256, 512, 1024 },
153 .hw_settle = (unsigned int []) { 0, 100, 200, 300, 400, 500, 600, 700,
154 1000, 2000, 4000, 6000, 8000, 10000 },
158 static int adc_tm5_read(struct adc_tm5_chip *adc_tm, u16 offset, u8 *data, int len)
160 return regmap_bulk_read(adc_tm->regmap, adc_tm->base + offset, data, len);
163 static int adc_tm5_write(struct adc_tm5_chip *adc_tm, u16 offset, u8 *data, int len)
165 return regmap_bulk_write(adc_tm->regmap, adc_tm->base + offset, data, len);
168 static int adc_tm5_reg_update(struct adc_tm5_chip *adc_tm, u16 offset, u8 mask, u8 val)
170 return regmap_write_bits(adc_tm->regmap, adc_tm->base + offset, mask, val);
173 static irqreturn_t adc_tm5_isr(int irq, void *data)
175 struct adc_tm5_chip *chip = data;
176 u8 status_low, status_high, ctl;
179 ret = adc_tm5_read(chip, ADC_TM5_STATUS_LOW, &status_low, sizeof(status_low));
181 dev_err(chip->dev, "read status low failed: %d\n", ret);
185 ret = adc_tm5_read(chip, ADC_TM5_STATUS_HIGH, &status_high, sizeof(status_high));
187 dev_err(chip->dev, "read status high failed: %d\n", ret);
191 for (i = 0; i < chip->nchannels; i++) {
192 bool upper_set = false, lower_set = false;
193 unsigned int ch = chip->channels[i].channel;
195 /* No TZD, we warned at the boot time */
196 if (!chip->channels[i].tzd)
199 ret = adc_tm5_read(chip, ADC_TM5_M_EN(ch), &ctl, sizeof(ctl));
201 dev_err(chip->dev, "ctl read failed: %d, channel %d\n", ret, i);
205 if (!(ctl & ADC_TM5_M_MEAS_EN))
208 lower_set = (status_low & BIT(ch)) &&
209 (ctl & ADC_TM5_M_LOW_THR_INT_EN);
211 upper_set = (status_high & BIT(ch)) &&
212 (ctl & ADC_TM5_M_HIGH_THR_INT_EN);
214 if (upper_set || lower_set)
215 thermal_zone_device_update(chip->channels[i].tzd,
216 THERMAL_EVENT_UNSPECIFIED);
222 static int adc_tm5_get_temp(void *data, int *temp)
224 struct adc_tm5_channel *channel = data;
227 if (!channel || !channel->iio)
230 ret = iio_read_channel_processed(channel->iio, temp);
234 if (ret != IIO_VAL_INT)
240 static int adc_tm5_disable_channel(struct adc_tm5_channel *channel)
242 struct adc_tm5_chip *chip = channel->chip;
243 unsigned int reg = ADC_TM5_M_EN(channel->channel);
245 return adc_tm5_reg_update(chip, reg,
247 ADC_TM5_M_HIGH_THR_INT_EN |
248 ADC_TM5_M_LOW_THR_INT_EN,
252 static int adc_tm5_enable(struct adc_tm5_chip *chip)
258 ret = adc_tm5_write(chip, ADC_TM_EN_CTL1, &data, sizeof(data));
260 dev_err(chip->dev, "adc-tm enable failed\n");
264 data = ADC_TM_CONV_REQ_EN;
265 ret = adc_tm5_write(chip, ADC_TM_CONV_REQ, &data, sizeof(data));
267 dev_err(chip->dev, "adc-tm request conversion failed\n");
274 static int adc_tm5_configure(struct adc_tm5_channel *channel, int low, int high)
276 struct adc_tm5_chip *chip = channel->chip;
278 u16 reg = ADC_TM5_M_ADC_CH_SEL_CTL(channel->channel);
281 ret = adc_tm5_read(chip, reg, buf, sizeof(buf));
283 dev_err(chip->dev, "channel %d params read failed: %d\n", channel->channel, ret);
287 buf[0] = channel->adc_channel;
289 /* High temperature corresponds to low voltage threshold */
290 if (high != INT_MAX) {
291 u16 adc_code = qcom_adc_tm5_temp_volt_scale(channel->prescale,
292 chip->data->full_scale_code_volt, high);
294 buf[1] = adc_code & 0xff;
295 buf[2] = adc_code >> 8;
296 buf[7] |= ADC_TM5_M_LOW_THR_INT_EN;
298 buf[7] &= ~ADC_TM5_M_LOW_THR_INT_EN;
301 /* Low temperature corresponds to high voltage threshold */
302 if (low != -INT_MAX) {
303 u16 adc_code = qcom_adc_tm5_temp_volt_scale(channel->prescale,
304 chip->data->full_scale_code_volt, low);
306 buf[3] = adc_code & 0xff;
307 buf[4] = adc_code >> 8;
308 buf[7] |= ADC_TM5_M_HIGH_THR_INT_EN;
310 buf[7] &= ~ADC_TM5_M_HIGH_THR_INT_EN;
313 buf[5] = ADC5_TIMER_SEL_2;
315 /* Set calibration select, hw_settle delay */
316 buf[6] &= ~ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK;
317 buf[6] |= FIELD_PREP(ADC_TM5_M_CTL_HW_SETTLE_DELAY_MASK, channel->hw_settle_time);
318 buf[6] &= ~ADC_TM5_M_CTL_CAL_SEL_MASK;
319 buf[6] |= FIELD_PREP(ADC_TM5_M_CTL_CAL_SEL_MASK, channel->cal_method);
321 buf[7] |= ADC_TM5_M_MEAS_EN;
323 ret = adc_tm5_write(chip, reg, buf, sizeof(buf));
325 dev_err(chip->dev, "channel %d params write failed: %d\n", channel->channel, ret);
329 return adc_tm5_enable(chip);
332 static int adc_tm5_set_trips(void *data, int low, int high)
334 struct adc_tm5_channel *channel = data;
335 struct adc_tm5_chip *chip;
341 chip = channel->chip;
342 dev_dbg(chip->dev, "%d:low(mdegC):%d, high(mdegC):%d\n",
343 channel->channel, low, high);
345 if (high == INT_MAX && low <= -INT_MAX)
346 ret = adc_tm5_disable_channel(channel);
348 ret = adc_tm5_configure(channel, low, high);
353 static struct thermal_zone_of_device_ops adc_tm5_ops = {
354 .get_temp = adc_tm5_get_temp,
355 .set_trips = adc_tm5_set_trips,
358 static int adc_tm5_register_tzd(struct adc_tm5_chip *adc_tm)
361 struct thermal_zone_device *tzd;
363 for (i = 0; i < adc_tm->nchannels; i++) {
364 adc_tm->channels[i].chip = adc_tm;
366 tzd = devm_thermal_zone_of_sensor_register(adc_tm->dev,
367 adc_tm->channels[i].channel,
368 &adc_tm->channels[i],
371 if (PTR_ERR(tzd) == -ENODEV) {
372 dev_warn(adc_tm->dev, "thermal sensor on channel %d is not used\n",
373 adc_tm->channels[i].channel);
377 dev_err(adc_tm->dev, "Error registering TZ zone for channel %d: %ld\n",
378 adc_tm->channels[i].channel, PTR_ERR(tzd));
381 adc_tm->channels[i].tzd = tzd;
387 static int adc_tm_hc_init(struct adc_tm5_chip *chip)
393 for (i = 0; i < chip->nchannels; i++) {
394 if (chip->channels[i].channel >= ADC_TM5_NUM_CHANNELS) {
395 dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
400 buf[0] = chip->decimation;
401 buf[1] = chip->avg_samples | ADC_TM5_FAST_AVG_EN;
403 ret = adc_tm5_write(chip, ADC_TM5_ADC_DIG_PARAM, buf, sizeof(buf));
405 dev_err(chip->dev, "block write failed: %d\n", ret);
410 static int adc_tm5_init(struct adc_tm5_chip *chip)
412 u8 buf[4], channels_available;
416 ret = adc_tm5_read(chip, ADC_TM5_NUM_BTM,
417 &channels_available, sizeof(channels_available));
419 dev_err(chip->dev, "read failed for BTM channels\n");
423 for (i = 0; i < chip->nchannels; i++) {
424 if (chip->channels[i].channel >= channels_available) {
425 dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
430 buf[0] = chip->decimation;
431 buf[1] = chip->avg_samples | ADC_TM5_FAST_AVG_EN;
432 buf[2] = ADC_TM5_TIMER1;
433 buf[3] = FIELD_PREP(ADC_TM5_MEAS_INTERVAL_CTL2_MASK, ADC_TM5_TIMER2) |
434 FIELD_PREP(ADC_TM5_MEAS_INTERVAL_CTL3_MASK, ADC_TM5_TIMER3);
436 ret = adc_tm5_write(chip, ADC_TM5_ADC_DIG_PARAM, buf, sizeof(buf));
438 dev_err(chip->dev, "block write failed: %d\n", ret);
445 static int adc_tm5_get_dt_channel_data(struct adc_tm5_chip *adc_tm,
446 struct adc_tm5_channel *channel,
447 struct device_node *node)
449 const char *name = node->name;
450 u32 chan, value, varr[2];
452 struct device *dev = adc_tm->dev;
453 struct of_phandle_args args;
455 ret = of_property_read_u32(node, "reg", &chan);
457 dev_err(dev, "%s: invalid channel number %d\n", name, ret);
461 if (chan >= ADC_TM5_NUM_CHANNELS) {
462 dev_err(dev, "%s: channel number too big: %d\n", name, chan);
466 channel->channel = chan;
469 * We are tied to PMIC's ADC controller, which always use single
470 * argument for channel number. So don't bother parsing
471 * #io-channel-cells, just enforce cell_count = 1.
473 ret = of_parse_phandle_with_fixed_args(node, "io-channels", 1, 0, &args);
475 dev_err(dev, "%s: error parsing ADC channel number %d: %d\n", name, chan, ret);
478 of_node_put(args.np);
480 if (args.args_count != 1 || args.args[0] >= ADC5_MAX_CHANNEL) {
481 dev_err(dev, "%s: invalid ADC channel number %d\n", name, chan);
484 channel->adc_channel = args.args[0];
486 channel->iio = devm_of_iio_channel_get_by_name(adc_tm->dev, node, NULL);
487 if (IS_ERR(channel->iio)) {
488 ret = PTR_ERR(channel->iio);
489 if (ret != -EPROBE_DEFER)
490 dev_err(dev, "%s: error getting channel: %d\n", name, ret);
494 ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
496 ret = qcom_adc5_prescaling_from_dt(varr[0], varr[1]);
498 dev_err(dev, "%s: invalid pre-scaling <%d %d>\n",
499 name, varr[0], varr[1]);
502 channel->prescale = ret;
504 /* 1:1 prescale is index 0 */
505 channel->prescale = 0;
508 ret = of_property_read_u32(node, "qcom,hw-settle-time-us", &value);
510 ret = qcom_adc5_hw_settle_time_from_dt(value, adc_tm->data->hw_settle);
512 dev_err(dev, "%s invalid hw-settle-time-us %d us\n",
516 channel->hw_settle_time = ret;
518 channel->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
521 if (of_property_read_bool(node, "qcom,ratiometric"))
522 channel->cal_method = ADC_TM5_RATIOMETRIC_CAL;
524 channel->cal_method = ADC_TM5_ABSOLUTE_CAL;
529 static int adc_tm5_get_dt_data(struct adc_tm5_chip *adc_tm, struct device_node *node)
531 struct adc_tm5_channel *channels;
532 struct device_node *child;
535 struct device *dev = adc_tm->dev;
537 adc_tm->nchannels = of_get_available_child_count(node);
538 if (!adc_tm->nchannels)
541 adc_tm->channels = devm_kcalloc(dev, adc_tm->nchannels,
542 sizeof(*adc_tm->channels), GFP_KERNEL);
543 if (!adc_tm->channels)
546 channels = adc_tm->channels;
548 adc_tm->data = of_device_get_match_data(dev);
550 adc_tm->data = &adc_tm5_data_pmic;
552 ret = of_property_read_u32(node, "qcom,decimation", &value);
554 ret = qcom_adc5_decimation_from_dt(value, adc_tm->data->decimation);
556 dev_err(dev, "invalid decimation %d\n", value);
559 adc_tm->decimation = ret;
561 adc_tm->decimation = ADC5_DECIMATION_DEFAULT;
564 ret = of_property_read_u32(node, "qcom,avg-samples", &value);
566 ret = qcom_adc5_avg_samples_from_dt(value);
568 dev_err(dev, "invalid avg-samples %d\n", value);
571 adc_tm->avg_samples = ret;
573 adc_tm->avg_samples = VADC_DEF_AVG_SAMPLES;
576 for_each_available_child_of_node(node, child) {
577 ret = adc_tm5_get_dt_channel_data(adc_tm, channels, child);
589 static int adc_tm5_probe(struct platform_device *pdev)
591 struct device_node *node = pdev->dev.of_node;
592 struct device *dev = &pdev->dev;
593 struct adc_tm5_chip *adc_tm;
594 struct regmap *regmap;
598 regmap = dev_get_regmap(dev->parent, NULL);
602 ret = of_property_read_u32(node, "reg", ®);
606 adc_tm = devm_kzalloc(&pdev->dev, sizeof(*adc_tm), GFP_KERNEL);
610 adc_tm->regmap = regmap;
614 irq = platform_get_irq(pdev, 0);
616 dev_err(dev, "get_irq failed: %d\n", irq);
620 ret = adc_tm5_get_dt_data(adc_tm, node);
622 dev_err(dev, "get dt data failed: %d\n", ret);
626 if (adc_tm->data->is_hc)
627 ret = adc_tm_hc_init(adc_tm);
629 ret = adc_tm5_init(adc_tm);
631 dev_err(dev, "adc-tm init failed\n");
635 ret = adc_tm5_register_tzd(adc_tm);
637 dev_err(dev, "tzd register failed\n");
641 return devm_request_threaded_irq(dev, irq, NULL, adc_tm5_isr,
642 IRQF_ONESHOT, "pm-adc-tm5", adc_tm);
645 static const struct of_device_id adc_tm5_match_table[] = {
647 .compatible = "qcom,spmi-adc-tm5",
648 .data = &adc_tm5_data_pmic,
651 .compatible = "qcom,spmi-adc-tm-hc",
652 .data = &adc_tm_hc_data_pmic,
656 MODULE_DEVICE_TABLE(of, adc_tm5_match_table);
658 static struct platform_driver adc_tm5_driver = {
660 .name = "qcom-spmi-adc-tm5",
661 .of_match_table = adc_tm5_match_table,
663 .probe = adc_tm5_probe,
665 module_platform_driver(adc_tm5_driver);
667 MODULE_DESCRIPTION("SPMI PMIC Thermal Monitor ADC driver");
668 MODULE_LICENSE("GPL v2");