Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2020 Linaro Limited
4 *
5 * Based on original driver:
6 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
7 *
8 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
9 */
10
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>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/platform_device.h>
19#include <linux/regmap.h>
20#include <linux/thermal.h>
21
22#include <asm/unaligned.h>
23
24#include "../thermal_hwmon.h"
25
26/*
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.
31 *
32 * Configuration of TM channels must match configuration of corresponding ADC
33 * channels.
34 */
35
36#define ADC5_MAX_CHANNEL 0xc0
37#define ADC_TM5_NUM_CHANNELS 8
38
39#define ADC_TM5_STATUS_LOW 0x0a
40
41#define ADC_TM5_STATUS_HIGH 0x0b
42
43#define ADC_TM5_NUM_BTM 0x0f
44
45#define ADC_TM5_ADC_DIG_PARAM 0x42
46
47#define ADC_TM5_FAST_AVG_CTL (ADC_TM5_ADC_DIG_PARAM + 1)
48#define ADC_TM5_FAST_AVG_EN BIT(7)
49
50#define ADC_TM5_MEAS_INTERVAL_CTL (ADC_TM5_ADC_DIG_PARAM + 2)
51#define ADC_TM5_TIMER1 3 /* 3.9ms */
52
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 */
58
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)
63
64#define ADC_TM5_M_CHAN_BASE 0x60
65
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)
80
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
86
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
90
91#define ADC_TM_GEN2_SID 0x40
92
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)
96
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)
100
101#define ADC_TM_GEN2_FAST_AVG_CTL 0x43
102#define ADC_TM_GEN2_FAST_AVG_EN BIT(7)
103
104#define ADC_TM_GEN2_ADC_CH_SEL_CTL 0x44
105
106#define ADC_TM_GEN2_DELAY_CTL 0x45
107#define ADC_TM_GEN2_HW_SETTLE_DELAY GENMASK(3, 0)
108
109#define ADC_TM_GEN2_EN_CTL1 0x46
110#define ADC_TM_GEN2_EN BIT(7)
111
112#define ADC_TM_GEN2_CONV_REQ 0x47
113#define ADC_TM_GEN2_CONV_REQ_EN BIT(7)
114
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)
121
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)
126
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
130
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
134
135enum adc5_timer_select {
136 ADC5_TIMER_SEL_1 = 0,
137 ADC5_TIMER_SEL_2,
138 ADC5_TIMER_SEL_3,
139 ADC5_TIMER_SEL_NONE,
140};
141
142enum adc5_gen {
143 ADC_TM5,
144 ADC_TM_HC,
145 ADC_TM5_GEN2,
146 ADC_TM5_MAX
147};
148
149enum adc_tm5_cal_method {
150 ADC_TM5_NO_CAL = 0,
151 ADC_TM5_RATIOMETRIC_CAL,
152 ADC_TM5_ABSOLUTE_CAL
153};
154
155enum adc_tm_gen2_time_select {
156 MEAS_INT_50MS = 0,
157 MEAS_INT_100MS,
158 MEAS_INT_1S,
159 MEAS_INT_SET,
160 MEAS_INT_NONE,
161};
162
163struct adc_tm5_chip;
164struct adc_tm5_channel;
165
166struct 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);
174 char *irq_name;
175 int gen;
176};
177
178/**
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.
195 */
196struct 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;
210};
211
212/**
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
228 * registers.
229 */
230struct adc_tm5_chip {
231 struct regmap *regmap;
232 struct device *dev;
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;
238 u16 base;
239 struct mutex adc_mutex_lock;
240};
241
242static int adc_tm5_read(struct adc_tm5_chip *adc_tm, u16 offset, u8 *data, int len)
243{
244 return regmap_bulk_read(adc_tm->regmap, adc_tm->base + offset, data, len);
245}
246
247static int adc_tm5_write(struct adc_tm5_chip *adc_tm, u16 offset, u8 *data, int len)
248{
249 return regmap_bulk_write(adc_tm->regmap, adc_tm->base + offset, data, len);
250}
251
252static int adc_tm5_reg_update(struct adc_tm5_chip *adc_tm, u16 offset, u8 mask, u8 val)
253{
254 return regmap_write_bits(adc_tm->regmap, adc_tm->base + offset, mask, val);
255}
256
257static irqreturn_t adc_tm5_isr(int irq, void *data)
258{
259 struct adc_tm5_chip *chip = data;
260 u8 status_low, status_high, ctl;
261 int ret, i;
262
263 ret = adc_tm5_read(chip, ADC_TM5_STATUS_LOW, &status_low, sizeof(status_low));
264 if (unlikely(ret)) {
265 dev_err(chip->dev, "read status low failed: %d\n", ret);
266 return IRQ_HANDLED;
267 }
268
269 ret = adc_tm5_read(chip, ADC_TM5_STATUS_HIGH, &status_high, sizeof(status_high));
270 if (unlikely(ret)) {
271 dev_err(chip->dev, "read status high failed: %d\n", ret);
272 return IRQ_HANDLED;
273 }
274
275 for (i = 0; i < chip->nchannels; i++) {
276 bool upper_set = false, lower_set = false;
277 unsigned int ch = chip->channels[i].channel;
278
279 /* No TZD, we warned at the boot time */
280 if (!chip->channels[i].tzd)
281 continue;
282
283 ret = adc_tm5_read(chip, ADC_TM5_M_EN(ch), &ctl, sizeof(ctl));
284 if (unlikely(ret)) {
285 dev_err(chip->dev, "ctl read failed: %d, channel %d\n", ret, i);
286 continue;
287 }
288
289 if (!(ctl & ADC_TM5_M_MEAS_EN))
290 continue;
291
292 lower_set = (status_low & BIT(ch)) &&
293 (ctl & ADC_TM5_M_LOW_THR_INT_EN);
294
295 upper_set = (status_high & BIT(ch)) &&
296 (ctl & ADC_TM5_M_HIGH_THR_INT_EN);
297
298 if (upper_set || lower_set)
299 thermal_zone_device_update(chip->channels[i].tzd,
300 THERMAL_EVENT_UNSPECIFIED);
301 }
302
303 return IRQ_HANDLED;
304}
305
306static irqreturn_t adc_tm5_gen2_isr(int irq, void *data)
307{
308 struct adc_tm5_chip *chip = data;
309 u8 status_low, status_high;
310 int ret, i;
311
312 ret = adc_tm5_read(chip, ADC_TM_GEN2_STATUS_LOW_CLR, &status_low, sizeof(status_low));
313 if (ret) {
314 dev_err(chip->dev, "read status_low failed: %d\n", ret);
315 return IRQ_HANDLED;
316 }
317
318 ret = adc_tm5_read(chip, ADC_TM_GEN2_STATUS_HIGH_CLR, &status_high, sizeof(status_high));
319 if (ret) {
320 dev_err(chip->dev, "read status_high failed: %d\n", ret);
321 return IRQ_HANDLED;
322 }
323
324 ret = adc_tm5_write(chip, ADC_TM_GEN2_STATUS_LOW_CLR, &status_low, sizeof(status_low));
325 if (ret < 0) {
326 dev_err(chip->dev, "clear status low failed with %d\n", ret);
327 return IRQ_HANDLED;
328 }
329
330 ret = adc_tm5_write(chip, ADC_TM_GEN2_STATUS_HIGH_CLR, &status_high, sizeof(status_high));
331 if (ret < 0) {
332 dev_err(chip->dev, "clear status high failed with %d\n", ret);
333 return IRQ_HANDLED;
334 }
335
336 for (i = 0; i < chip->nchannels; i++) {
337 bool upper_set = false, lower_set = false;
338 unsigned int ch = chip->channels[i].channel;
339
340 /* No TZD, we warned at the boot time */
341 if (!chip->channels[i].tzd)
342 continue;
343
344 if (!chip->channels[i].meas_en)
345 continue;
346
347 lower_set = (status_low & BIT(ch)) &&
348 (chip->channels[i].low_thr_en);
349
350 upper_set = (status_high & BIT(ch)) &&
351 (chip->channels[i].high_thr_en);
352
353 if (upper_set || lower_set)
354 thermal_zone_device_update(chip->channels[i].tzd,
355 THERMAL_EVENT_UNSPECIFIED);
356 }
357
358 return IRQ_HANDLED;
359}
360
361static int adc_tm5_get_temp(struct thermal_zone_device *tz, int *temp)
362{
363 struct adc_tm5_channel *channel = thermal_zone_device_priv(tz);
364 int ret;
365
366 if (!channel || !channel->iio)
367 return -EINVAL;
368
369 ret = iio_read_channel_processed(channel->iio, temp);
370 if (ret < 0)
371 return ret;
372
373 if (ret != IIO_VAL_INT)
374 return -EINVAL;
375
376 return 0;
377}
378
379static int adc_tm5_disable_channel(struct adc_tm5_channel *channel)
380{
381 struct adc_tm5_chip *chip = channel->chip;
382 unsigned int reg = ADC_TM5_M_EN(channel->channel);
383
384 return adc_tm5_reg_update(chip, reg,
385 ADC_TM5_M_MEAS_EN |
386 ADC_TM5_M_HIGH_THR_INT_EN |
387 ADC_TM5_M_LOW_THR_INT_EN,
388 0);
389}
390
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
394
395static int32_t adc_tm5_gen2_conv_req(struct adc_tm5_chip *chip)
396{
397 int ret;
398 u8 data;
399 unsigned int count;
400
401 data = ADC_TM_GEN2_EN;
402 ret = adc_tm5_write(chip, ADC_TM_GEN2_EN_CTL1, &data, 1);
403 if (ret < 0) {
404 dev_err(chip->dev, "adc-tm enable failed with %d\n", ret);
405 return ret;
406 }
407
408 data = ADC_TM_GEN2_CFG_HS_FLAG;
409 ret = adc_tm5_write(chip, ADC_TM_GEN2_CFG_HS_SET, &data, 1);
410 if (ret < 0) {
411 dev_err(chip->dev, "adc-tm handshake failed with %d\n", ret);
412 return ret;
413 }
414
415 data = ADC_TM_GEN2_CONV_REQ_EN;
416 ret = adc_tm5_write(chip, ADC_TM_GEN2_CONV_REQ, &data, 1);
417 if (ret < 0) {
418 dev_err(chip->dev, "adc-tm request conversion failed with %d\n", ret);
419 return ret;
420 }
421
422 /*
423 * SW sets a handshake bit and waits for PBS to clear it
424 * before the next conversion request can be queued.
425 */
426
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));
429 if (ret < 0) {
430 dev_err(chip->dev, "adc-tm read failed with %d\n", ret);
431 return ret;
432 }
433
434 if (!(data & ADC_TM_GEN2_CFG_HS_FLAG))
435 return ret;
436 usleep_range(ADC_TM_GEN2_POLL_DELAY_MIN_US,
437 ADC_TM_GEN2_POLL_DELAY_MAX_US);
438 }
439
440 dev_err(chip->dev, "adc-tm conversion request handshake timed out\n");
441
442 return -ETIMEDOUT;
443}
444
445static int adc_tm5_gen2_disable_channel(struct adc_tm5_channel *channel)
446{
447 struct adc_tm5_chip *chip = channel->chip;
448 int ret;
449 u8 val;
450
451 mutex_lock(&chip->adc_mutex_lock);
452
453 channel->meas_en = false;
454 channel->high_thr_en = false;
455 channel->low_thr_en = false;
456
457 ret = adc_tm5_read(chip, ADC_TM_GEN2_CH_CTL, &val, sizeof(val));
458 if (ret < 0) {
459 dev_err(chip->dev, "adc-tm block read failed with %d\n", ret);
460 goto disable_fail;
461 }
462
463 val &= ~ADC_TM_GEN2_TM_CH_SEL;
464 val |= FIELD_PREP(ADC_TM_GEN2_TM_CH_SEL, channel->channel);
465
466 ret = adc_tm5_write(chip, ADC_TM_GEN2_CH_CTL, &val, 1);
467 if (ret < 0) {
468 dev_err(chip->dev, "adc-tm channel disable failed with %d\n", ret);
469 goto disable_fail;
470 }
471
472 val = 0;
473 ret = adc_tm5_write(chip, ADC_TM_GEN2_MEAS_IRQ_EN, &val, 1);
474 if (ret < 0) {
475 dev_err(chip->dev, "adc-tm interrupt disable failed with %d\n", ret);
476 goto disable_fail;
477 }
478
479
480 ret = adc_tm5_gen2_conv_req(channel->chip);
481 if (ret < 0)
482 dev_err(chip->dev, "adc-tm channel configure failed with %d\n", ret);
483
484disable_fail:
485 mutex_unlock(&chip->adc_mutex_lock);
486 return ret;
487}
488
489static int adc_tm5_enable(struct adc_tm5_chip *chip)
490{
491 int ret;
492 u8 data;
493
494 data = ADC_TM_EN;
495 ret = adc_tm5_write(chip, ADC_TM_EN_CTL1, &data, sizeof(data));
496 if (ret < 0) {
497 dev_err(chip->dev, "adc-tm enable failed\n");
498 return ret;
499 }
500
501 data = ADC_TM_CONV_REQ_EN;
502 ret = adc_tm5_write(chip, ADC_TM_CONV_REQ, &data, sizeof(data));
503 if (ret < 0) {
504 dev_err(chip->dev, "adc-tm request conversion failed\n");
505 return ret;
506 }
507
508 return 0;
509}
510
511static int adc_tm5_configure(struct adc_tm5_channel *channel, int low, int high)
512{
513 struct adc_tm5_chip *chip = channel->chip;
514 u8 buf[8];
515 u16 reg = ADC_TM5_M_ADC_CH_SEL_CTL(channel->channel);
516 int ret;
517
518 ret = adc_tm5_read(chip, reg, buf, sizeof(buf));
519 if (ret) {
520 dev_err(chip->dev, "channel %d params read failed: %d\n", channel->channel, ret);
521 return ret;
522 }
523
524 buf[0] = channel->adc_channel;
525
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);
530
531 put_unaligned_le16(adc_code, &buf[1]);
532 buf[7] |= ADC_TM5_M_LOW_THR_INT_EN;
533 } else {
534 buf[7] &= ~ADC_TM5_M_LOW_THR_INT_EN;
535 }
536
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);
541
542 put_unaligned_le16(adc_code, &buf[3]);
543 buf[7] |= ADC_TM5_M_HIGH_THR_INT_EN;
544 } else {
545 buf[7] &= ~ADC_TM5_M_HIGH_THR_INT_EN;
546 }
547
548 buf[5] = ADC5_TIMER_SEL_2;
549
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);
555
556 buf[7] |= ADC_TM5_M_MEAS_EN;
557
558 ret = adc_tm5_write(chip, reg, buf, sizeof(buf));
559 if (ret) {
560 dev_err(chip->dev, "channel %d params write failed: %d\n", channel->channel, ret);
561 return ret;
562 }
563
564 return adc_tm5_enable(chip);
565}
566
567static int adc_tm5_gen2_configure(struct adc_tm5_channel *channel, int low, int high)
568{
569 struct adc_tm5_chip *chip = channel->chip;
570 int ret;
571 u8 buf[14];
572 u16 adc_code;
573
574 mutex_lock(&chip->adc_mutex_lock);
575
576 channel->meas_en = true;
577
578 ret = adc_tm5_read(chip, ADC_TM_GEN2_SID, buf, sizeof(buf));
579 if (ret < 0) {
580 dev_err(chip->dev, "adc-tm block read failed with %d\n", ret);
581 goto config_fail;
582 }
583
584 /* Set SID from virtual channel number */
585 buf[0] = channel->adc_channel >> 8;
586
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);
592
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);
597
598 buf[3] = channel->avg_samples | ADC_TM_GEN2_FAST_AVG_EN;
599
600 buf[4] = channel->adc_channel & 0xff;
601
602 buf[5] = channel->hw_settle_time & ADC_TM_GEN2_HW_SETTLE_DELAY;
603
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]);
609 } else {
610 channel->low_thr_en = false;
611 }
612
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]);
618 } else {
619 channel->high_thr_en = false;
620 }
621
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;
627
628 ret = adc_tm5_write(chip, ADC_TM_GEN2_SID, buf, sizeof(buf));
629 if (ret) {
630 dev_err(chip->dev, "channel %d params write failed: %d\n", channel->channel, ret);
631 goto config_fail;
632 }
633
634 ret = adc_tm5_gen2_conv_req(channel->chip);
635 if (ret < 0)
636 dev_err(chip->dev, "adc-tm channel configure failed with %d\n", ret);
637
638config_fail:
639 mutex_unlock(&chip->adc_mutex_lock);
640 return ret;
641}
642
643static int adc_tm5_set_trips(struct thermal_zone_device *tz, int low, int high)
644{
645 struct adc_tm5_channel *channel = thermal_zone_device_priv(tz);
646 struct adc_tm5_chip *chip;
647 int ret;
648
649 if (!channel)
650 return -EINVAL;
651
652 chip = channel->chip;
653 dev_dbg(chip->dev, "%d:low(mdegC):%d, high(mdegC):%d\n",
654 channel->channel, low, high);
655
656 if (high == INT_MAX && low <= -INT_MAX)
657 ret = chip->data->disable_channel(channel);
658 else
659 ret = chip->data->configure(channel, low, high);
660
661 return ret;
662}
663
664static const struct thermal_zone_device_ops adc_tm5_thermal_ops = {
665 .get_temp = adc_tm5_get_temp,
666 .set_trips = adc_tm5_set_trips,
667};
668
669static int adc_tm5_register_tzd(struct adc_tm5_chip *adc_tm)
670{
671 unsigned int i;
672 struct thermal_zone_device *tzd;
673
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);
680 if (IS_ERR(tzd)) {
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);
684 continue;
685 }
686
687 dev_err(adc_tm->dev, "Error registering TZ zone for channel %d: %ld\n",
688 adc_tm->channels[i].channel, PTR_ERR(tzd));
689 return PTR_ERR(tzd);
690 }
691 adc_tm->channels[i].tzd = tzd;
692 devm_thermal_add_hwmon_sysfs(adc_tm->dev, tzd);
693 }
694
695 return 0;
696}
697
698static int adc_tm_hc_init(struct adc_tm5_chip *chip)
699{
700 unsigned int i;
701 u8 buf[2];
702 int ret;
703
704 for (i = 0; i < chip->nchannels; i++) {
705 if (chip->channels[i].channel >= ADC_TM5_NUM_CHANNELS) {
706 dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
707 return -EINVAL;
708 }
709 }
710
711 buf[0] = chip->decimation;
712 buf[1] = chip->avg_samples | ADC_TM5_FAST_AVG_EN;
713
714 ret = adc_tm5_write(chip, ADC_TM5_ADC_DIG_PARAM, buf, sizeof(buf));
715 if (ret)
716 dev_err(chip->dev, "block write failed: %d\n", ret);
717
718 return ret;
719}
720
721static int adc_tm5_init(struct adc_tm5_chip *chip)
722{
723 u8 buf[4], channels_available;
724 int ret;
725 unsigned int i;
726
727 ret = adc_tm5_read(chip, ADC_TM5_NUM_BTM,
728 &channels_available, sizeof(channels_available));
729 if (ret) {
730 dev_err(chip->dev, "read failed for BTM channels\n");
731 return ret;
732 }
733
734 for (i = 0; i < chip->nchannels; i++) {
735 if (chip->channels[i].channel >= channels_available) {
736 dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
737 return -EINVAL;
738 }
739 }
740
741 buf[0] = chip->decimation;
742 buf[1] = chip->avg_samples | ADC_TM5_FAST_AVG_EN;
743 buf[2] = ADC_TM5_TIMER1;
744 buf[3] = FIELD_PREP(ADC_TM5_MEAS_INTERVAL_CTL2_MASK, ADC_TM5_TIMER2) |
745 FIELD_PREP(ADC_TM5_MEAS_INTERVAL_CTL3_MASK, ADC_TM5_TIMER3);
746
747 ret = adc_tm5_write(chip, ADC_TM5_ADC_DIG_PARAM, buf, sizeof(buf));
748 if (ret) {
749 dev_err(chip->dev, "block write failed: %d\n", ret);
750 return ret;
751 }
752
753 return ret;
754}
755
756static int adc_tm5_gen2_init(struct adc_tm5_chip *chip)
757{
758 u8 channels_available;
759 int ret;
760 unsigned int i;
761
762 ret = adc_tm5_read(chip, ADC_TM5_NUM_BTM,
763 &channels_available, sizeof(channels_available));
764 if (ret) {
765 dev_err(chip->dev, "read failed for BTM channels\n");
766 return ret;
767 }
768
769 for (i = 0; i < chip->nchannels; i++) {
770 if (chip->channels[i].channel >= channels_available) {
771 dev_err(chip->dev, "Invalid channel %d\n", chip->channels[i].channel);
772 return -EINVAL;
773 }
774 }
775
776 mutex_init(&chip->adc_mutex_lock);
777
778 return ret;
779}
780
781static int adc_tm5_get_dt_channel_data(struct adc_tm5_chip *adc_tm,
782 struct adc_tm5_channel *channel,
783 struct device_node *node)
784{
785 const char *name = node->name;
786 u32 chan, value, adc_channel, varr[2];
787 int ret;
788 struct device *dev = adc_tm->dev;
789 struct of_phandle_args args;
790
791 ret = of_property_read_u32(node, "reg", &chan);
792 if (ret) {
793 dev_err(dev, "%s: invalid channel number %d\n", name, ret);
794 return ret;
795 }
796
797 if (chan >= ADC_TM5_NUM_CHANNELS) {
798 dev_err(dev, "%s: channel number too big: %d\n", name, chan);
799 return -EINVAL;
800 }
801
802 channel->channel = chan;
803
804 /*
805 * We are tied to PMIC's ADC controller, which always use single
806 * argument for channel number. So don't bother parsing
807 * #io-channel-cells, just enforce cell_count = 1.
808 */
809 ret = of_parse_phandle_with_fixed_args(node, "io-channels", 1, 0, &args);
810 if (ret < 0) {
811 dev_err(dev, "%s: error parsing ADC channel number %d: %d\n", name, chan, ret);
812 return ret;
813 }
814 of_node_put(args.np);
815
816 if (args.args_count != 1) {
817 dev_err(dev, "%s: invalid args count for ADC channel %d\n", name, chan);
818 return -EINVAL;
819 }
820
821 adc_channel = args.args[0];
822 if (adc_tm->data->gen == ADC_TM5_GEN2)
823 adc_channel &= 0xff;
824
825 if (adc_channel >= ADC5_MAX_CHANNEL) {
826 dev_err(dev, "%s: invalid ADC channel number %d\n", name, chan);
827 return -EINVAL;
828 }
829 channel->adc_channel = args.args[0];
830
831 channel->iio = devm_fwnode_iio_channel_get_by_name(adc_tm->dev,
832 of_fwnode_handle(node), NULL);
833 if (IS_ERR(channel->iio)) {
834 ret = PTR_ERR(channel->iio);
835 if (ret != -EPROBE_DEFER)
836 dev_err(dev, "%s: error getting channel: %d\n", name, ret);
837 return ret;
838 }
839
840 ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
841 if (!ret) {
842 ret = qcom_adc5_prescaling_from_dt(varr[0], varr[1]);
843 if (ret < 0) {
844 dev_err(dev, "%s: invalid pre-scaling <%d %d>\n",
845 name, varr[0], varr[1]);
846 return ret;
847 }
848 channel->prescale = ret;
849 } else {
850 /* 1:1 prescale is index 0 */
851 channel->prescale = 0;
852 }
853
854 ret = of_property_read_u32(node, "qcom,hw-settle-time-us", &value);
855 if (!ret) {
856 ret = qcom_adc5_hw_settle_time_from_dt(value, adc_tm->data->hw_settle);
857 if (ret < 0) {
858 dev_err(dev, "%s invalid hw-settle-time-us %d us\n",
859 name, value);
860 return ret;
861 }
862 channel->hw_settle_time = ret;
863 } else {
864 channel->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
865 }
866
867 if (of_property_read_bool(node, "qcom,ratiometric"))
868 channel->cal_method = ADC_TM5_RATIOMETRIC_CAL;
869 else
870 channel->cal_method = ADC_TM5_ABSOLUTE_CAL;
871
872 if (adc_tm->data->gen == ADC_TM5_GEN2) {
873 ret = of_property_read_u32(node, "qcom,decimation", &value);
874 if (!ret) {
875 ret = qcom_adc5_decimation_from_dt(value, adc_tm->data->decimation);
876 if (ret < 0) {
877 dev_err(dev, "invalid decimation %d\n", value);
878 return ret;
879 }
880 channel->decimation = ret;
881 } else {
882 channel->decimation = ADC5_DECIMATION_DEFAULT;
883 }
884
885 ret = of_property_read_u32(node, "qcom,avg-samples", &value);
886 if (!ret) {
887 ret = qcom_adc5_avg_samples_from_dt(value);
888 if (ret < 0) {
889 dev_err(dev, "invalid avg-samples %d\n", value);
890 return ret;
891 }
892 channel->avg_samples = ret;
893 } else {
894 channel->avg_samples = VADC_DEF_AVG_SAMPLES;
895 }
896 }
897
898 return 0;
899}
900
901static const struct adc_tm5_data adc_tm5_data_pmic = {
902 .full_scale_code_volt = 0x70e4,
903 .decimation = (unsigned int []) { 250, 420, 840 },
904 .hw_settle = (unsigned int []) { 15, 100, 200, 300, 400, 500, 600, 700,
905 1000, 2000, 4000, 8000, 16000, 32000,
906 64000, 128000 },
907 .disable_channel = adc_tm5_disable_channel,
908 .configure = adc_tm5_configure,
909 .isr = adc_tm5_isr,
910 .init = adc_tm5_init,
911 .irq_name = "pm-adc-tm5",
912 .gen = ADC_TM5,
913};
914
915static const struct adc_tm5_data adc_tm_hc_data_pmic = {
916 .full_scale_code_volt = 0x70e4,
917 .decimation = (unsigned int []) { 256, 512, 1024 },
918 .hw_settle = (unsigned int []) { 0, 100, 200, 300, 400, 500, 600, 700,
919 1000, 2000, 4000, 6000, 8000, 10000 },
920 .disable_channel = adc_tm5_disable_channel,
921 .configure = adc_tm5_configure,
922 .isr = adc_tm5_isr,
923 .init = adc_tm_hc_init,
924 .irq_name = "pm-adc-tm5",
925 .gen = ADC_TM_HC,
926};
927
928static const struct adc_tm5_data adc_tm5_gen2_data_pmic = {
929 .full_scale_code_volt = 0x70e4,
930 .decimation = (unsigned int []) { 85, 340, 1360 },
931 .hw_settle = (unsigned int []) { 15, 100, 200, 300, 400, 500, 600, 700,
932 1000, 2000, 4000, 8000, 16000, 32000,
933 64000, 128000 },
934 .disable_channel = adc_tm5_gen2_disable_channel,
935 .configure = adc_tm5_gen2_configure,
936 .isr = adc_tm5_gen2_isr,
937 .init = adc_tm5_gen2_init,
938 .irq_name = "pm-adc-tm5-gen2",
939 .gen = ADC_TM5_GEN2,
940};
941
942static int adc_tm5_get_dt_data(struct adc_tm5_chip *adc_tm, struct device_node *node)
943{
944 struct adc_tm5_channel *channels;
945 struct device_node *child;
946 u32 value;
947 int ret;
948 struct device *dev = adc_tm->dev;
949
950 adc_tm->nchannels = of_get_available_child_count(node);
951 if (!adc_tm->nchannels)
952 return -EINVAL;
953
954 adc_tm->channels = devm_kcalloc(dev, adc_tm->nchannels,
955 sizeof(*adc_tm->channels), GFP_KERNEL);
956 if (!adc_tm->channels)
957 return -ENOMEM;
958
959 channels = adc_tm->channels;
960
961 adc_tm->data = of_device_get_match_data(dev);
962 if (!adc_tm->data)
963 adc_tm->data = &adc_tm5_data_pmic;
964
965 ret = of_property_read_u32(node, "qcom,decimation", &value);
966 if (!ret) {
967 ret = qcom_adc5_decimation_from_dt(value, adc_tm->data->decimation);
968 if (ret < 0) {
969 dev_err(dev, "invalid decimation %d\n", value);
970 return ret;
971 }
972 adc_tm->decimation = ret;
973 } else {
974 adc_tm->decimation = ADC5_DECIMATION_DEFAULT;
975 }
976
977 ret = of_property_read_u32(node, "qcom,avg-samples", &value);
978 if (!ret) {
979 ret = qcom_adc5_avg_samples_from_dt(value);
980 if (ret < 0) {
981 dev_err(dev, "invalid avg-samples %d\n", value);
982 return ret;
983 }
984 adc_tm->avg_samples = ret;
985 } else {
986 adc_tm->avg_samples = VADC_DEF_AVG_SAMPLES;
987 }
988
989 for_each_available_child_of_node(node, child) {
990 ret = adc_tm5_get_dt_channel_data(adc_tm, channels, child);
991 if (ret) {
992 of_node_put(child);
993 return ret;
994 }
995
996 channels++;
997 }
998
999 return 0;
1000}
1001
1002static int adc_tm5_probe(struct platform_device *pdev)
1003{
1004 struct device_node *node = pdev->dev.of_node;
1005 struct device *dev = &pdev->dev;
1006 struct adc_tm5_chip *adc_tm;
1007 struct regmap *regmap;
1008 int ret, irq;
1009 u32 reg;
1010
1011 regmap = dev_get_regmap(dev->parent, NULL);
1012 if (!regmap)
1013 return -ENODEV;
1014
1015 ret = of_property_read_u32(node, "reg", ®);
1016 if (ret)
1017 return ret;
1018
1019 adc_tm = devm_kzalloc(&pdev->dev, sizeof(*adc_tm), GFP_KERNEL);
1020 if (!adc_tm)
1021 return -ENOMEM;
1022
1023 adc_tm->regmap = regmap;
1024 adc_tm->dev = dev;
1025 adc_tm->base = reg;
1026
1027 irq = platform_get_irq(pdev, 0);
1028 if (irq < 0)
1029 return irq;
1030
1031 ret = adc_tm5_get_dt_data(adc_tm, node);
1032 if (ret)
1033 return dev_err_probe(dev, ret, "get dt data failed\n");
1034
1035 ret = adc_tm->data->init(adc_tm);
1036 if (ret) {
1037 dev_err(dev, "adc-tm init failed\n");
1038 return ret;
1039 }
1040
1041 ret = adc_tm5_register_tzd(adc_tm);
1042 if (ret) {
1043 dev_err(dev, "tzd register failed\n");
1044 return ret;
1045 }
1046
1047 return devm_request_threaded_irq(dev, irq, NULL, adc_tm->data->isr,
1048 IRQF_ONESHOT, adc_tm->data->irq_name, adc_tm);
1049}
1050
1051static const struct of_device_id adc_tm5_match_table[] = {
1052 {
1053 .compatible = "qcom,spmi-adc-tm5",
1054 .data = &adc_tm5_data_pmic,
1055 },
1056 {
1057 .compatible = "qcom,spmi-adc-tm-hc",
1058 .data = &adc_tm_hc_data_pmic,
1059 },
1060 {
1061 .compatible = "qcom,spmi-adc-tm5-gen2",
1062 .data = &adc_tm5_gen2_data_pmic,
1063 },
1064 { }
1065};
1066MODULE_DEVICE_TABLE(of, adc_tm5_match_table);
1067
1068static struct platform_driver adc_tm5_driver = {
1069 .driver = {
1070 .name = "qcom-spmi-adc-tm5",
1071 .of_match_table = adc_tm5_match_table,
1072 },
1073 .probe = adc_tm5_probe,
1074};
1075module_platform_driver(adc_tm5_driver);
1076
1077MODULE_DESCRIPTION("SPMI PMIC Thermal Monitor ADC driver");
1078MODULE_LICENSE("GPL v2");