1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale MXS LRADC ADC driver
5 * Copyright (c) 2012 DENX Software Engineering, GmbH.
6 * Copyright (c) 2017 Ksenija Stanojevic <ksenija.stanojevic@gmail.com>
9 * Marek Vasut <marex@denx.de>
10 * Ksenija Stanojevic <ksenija.stanojevic@gmail.com>
13 #include <linux/completion.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/interrupt.h>
17 #include <linux/mfd/core.h>
18 #include <linux/mfd/mxs-lradc.h>
19 #include <linux/module.h>
20 #include <linux/of_irq.h>
21 #include <linux/platform_device.h>
22 #include <linux/sysfs.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/trigger.h>
27 #include <linux/iio/trigger_consumer.h>
28 #include <linux/iio/triggered_buffer.h>
29 #include <linux/iio/sysfs.h>
32 * Make this runtime configurable if necessary. Currently, if the buffered mode
33 * is enabled, the LRADC takes LRADC_DELAY_TIMER_LOOP samples of data before
34 * triggering IRQ. The sampling happens every (LRADC_DELAY_TIMER_PER / 2000)
35 * seconds. The result is that the samples arrive every 500mS.
37 #define LRADC_DELAY_TIMER_PER 200
38 #define LRADC_DELAY_TIMER_LOOP 5
40 #define VREF_MV_BASE 1850
42 static const char *mx23_lradc_adc_irq_names
[] = {
51 static const char *mx28_lradc_adc_irq_names
[] = {
64 static const u32 mxs_lradc_adc_vref_mv
[][LRADC_MAX_TOTAL_CHANS
] = {
66 VREF_MV_BASE
, /* CH0 */
67 VREF_MV_BASE
, /* CH1 */
68 VREF_MV_BASE
, /* CH2 */
69 VREF_MV_BASE
, /* CH3 */
70 VREF_MV_BASE
, /* CH4 */
71 VREF_MV_BASE
, /* CH5 */
72 VREF_MV_BASE
* 2, /* CH6 VDDIO */
73 VREF_MV_BASE
* 4, /* CH7 VBATT */
74 VREF_MV_BASE
, /* CH8 Temp sense 0 */
75 VREF_MV_BASE
, /* CH9 Temp sense 1 */
76 VREF_MV_BASE
, /* CH10 */
77 VREF_MV_BASE
, /* CH11 */
78 VREF_MV_BASE
, /* CH12 USB_DP */
79 VREF_MV_BASE
, /* CH13 USB_DN */
80 VREF_MV_BASE
, /* CH14 VBG */
81 VREF_MV_BASE
* 4, /* CH15 VDD5V */
84 VREF_MV_BASE
, /* CH0 */
85 VREF_MV_BASE
, /* CH1 */
86 VREF_MV_BASE
, /* CH2 */
87 VREF_MV_BASE
, /* CH3 */
88 VREF_MV_BASE
, /* CH4 */
89 VREF_MV_BASE
, /* CH5 */
90 VREF_MV_BASE
, /* CH6 */
91 VREF_MV_BASE
* 4, /* CH7 VBATT */
92 VREF_MV_BASE
, /* CH8 Temp sense 0 */
93 VREF_MV_BASE
, /* CH9 Temp sense 1 */
94 VREF_MV_BASE
* 2, /* CH10 VDDIO */
95 VREF_MV_BASE
, /* CH11 VTH */
96 VREF_MV_BASE
* 2, /* CH12 VDDA */
97 VREF_MV_BASE
, /* CH13 VDDD */
98 VREF_MV_BASE
, /* CH14 VBG */
99 VREF_MV_BASE
* 4, /* CH15 VDD5V */
103 enum mxs_lradc_divbytwo
{
104 MXS_LRADC_DIV_DISABLED
= 0,
105 MXS_LRADC_DIV_ENABLED
,
108 struct mxs_lradc_scale
{
109 unsigned int integer
;
113 struct mxs_lradc_adc
{
114 struct mxs_lradc
*lradc
;
119 struct iio_trigger
*trig
;
120 struct completion completion
;
124 struct mxs_lradc_scale scale_avail
[LRADC_MAX_TOTAL_CHANS
][2];
125 unsigned long is_divided
;
129 /* Raw I/O operations */
130 static int mxs_lradc_adc_read_single(struct iio_dev
*iio_dev
, int chan
,
133 struct mxs_lradc_adc
*adc
= iio_priv(iio_dev
);
134 struct mxs_lradc
*lradc
= adc
->lradc
;
138 * See if there is no buffered operation in progress. If there is simply
139 * bail out. This can be improved to support both buffered and raw IO at
140 * the same time, yet the code becomes horribly complicated. Therefore I
141 * applied KISS principle here.
143 ret
= iio_device_claim_direct_mode(iio_dev
);
147 reinit_completion(&adc
->completion
);
150 * No buffered operation in progress, map the channel and trigger it.
151 * Virtual channel 0 is always used here as the others are always not
152 * used if doing raw sampling.
154 if (lradc
->soc
== IMX28_LRADC
)
155 writel(LRADC_CTRL1_LRADC_IRQ_EN(0),
156 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_CLR
);
157 writel(0x1, adc
->base
+ LRADC_CTRL0
+ STMP_OFFSET_REG_CLR
);
159 /* Enable / disable the divider per requirement */
160 if (test_bit(chan
, &adc
->is_divided
))
161 writel(1 << LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET
,
162 adc
->base
+ LRADC_CTRL2
+ STMP_OFFSET_REG_SET
);
164 writel(1 << LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET
,
165 adc
->base
+ LRADC_CTRL2
+ STMP_OFFSET_REG_CLR
);
167 /* Clean the slot's previous content, then set new one. */
168 writel(LRADC_CTRL4_LRADCSELECT_MASK(0),
169 adc
->base
+ LRADC_CTRL4
+ STMP_OFFSET_REG_CLR
);
170 writel(chan
, adc
->base
+ LRADC_CTRL4
+ STMP_OFFSET_REG_SET
);
172 writel(0, adc
->base
+ LRADC_CH(0));
174 /* Enable the IRQ and start sampling the channel. */
175 writel(LRADC_CTRL1_LRADC_IRQ_EN(0),
176 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_SET
);
177 writel(BIT(0), adc
->base
+ LRADC_CTRL0
+ STMP_OFFSET_REG_SET
);
179 /* Wait for completion on the channel, 1 second max. */
180 ret
= wait_for_completion_killable_timeout(&adc
->completion
, HZ
);
187 *val
= readl(adc
->base
+ LRADC_CH(0)) & LRADC_CH_VALUE_MASK
;
191 writel(LRADC_CTRL1_LRADC_IRQ_EN(0),
192 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_CLR
);
194 iio_device_release_direct_mode(iio_dev
);
199 static int mxs_lradc_adc_read_temp(struct iio_dev
*iio_dev
, int *val
)
203 ret
= mxs_lradc_adc_read_single(iio_dev
, 8, &min
);
204 if (ret
!= IIO_VAL_INT
)
207 ret
= mxs_lradc_adc_read_single(iio_dev
, 9, &max
);
208 if (ret
!= IIO_VAL_INT
)
216 static int mxs_lradc_adc_read_raw(struct iio_dev
*iio_dev
,
217 const struct iio_chan_spec
*chan
,
218 int *val
, int *val2
, long m
)
220 struct mxs_lradc_adc
*adc
= iio_priv(iio_dev
);
223 case IIO_CHAN_INFO_RAW
:
224 if (chan
->type
== IIO_TEMP
)
225 return mxs_lradc_adc_read_temp(iio_dev
, val
);
227 return mxs_lradc_adc_read_single(iio_dev
, chan
->channel
, val
);
229 case IIO_CHAN_INFO_SCALE
:
230 if (chan
->type
== IIO_TEMP
) {
232 * From the datasheet, we have to multiply by 1.012 and
237 return IIO_VAL_INT_PLUS_MICRO
;
240 *val
= adc
->vref_mv
[chan
->channel
];
241 *val2
= chan
->scan_type
.realbits
-
242 test_bit(chan
->channel
, &adc
->is_divided
);
243 return IIO_VAL_FRACTIONAL_LOG2
;
245 case IIO_CHAN_INFO_OFFSET
:
246 if (chan
->type
== IIO_TEMP
) {
248 * The calculated value from the ADC is in Kelvin, we
249 * want Celsius for hwmon so the offset is -273.15
250 * The offset is applied before scaling so it is
251 * actually -213.15 * 4 / 1.012 = -1079.644268
256 return IIO_VAL_INT_PLUS_MICRO
;
268 static int mxs_lradc_adc_write_raw(struct iio_dev
*iio_dev
,
269 const struct iio_chan_spec
*chan
,
270 int val
, int val2
, long m
)
272 struct mxs_lradc_adc
*adc
= iio_priv(iio_dev
);
273 struct mxs_lradc_scale
*scale_avail
=
274 adc
->scale_avail
[chan
->channel
];
277 ret
= iio_device_claim_direct_mode(iio_dev
);
282 case IIO_CHAN_INFO_SCALE
:
284 if (val
== scale_avail
[MXS_LRADC_DIV_DISABLED
].integer
&&
285 val2
== scale_avail
[MXS_LRADC_DIV_DISABLED
].nano
) {
286 /* divider by two disabled */
287 clear_bit(chan
->channel
, &adc
->is_divided
);
289 } else if (val
== scale_avail
[MXS_LRADC_DIV_ENABLED
].integer
&&
290 val2
== scale_avail
[MXS_LRADC_DIV_ENABLED
].nano
) {
291 /* divider by two enabled */
292 set_bit(chan
->channel
, &adc
->is_divided
);
302 iio_device_release_direct_mode(iio_dev
);
307 static int mxs_lradc_adc_write_raw_get_fmt(struct iio_dev
*iio_dev
,
308 const struct iio_chan_spec
*chan
,
311 return IIO_VAL_INT_PLUS_NANO
;
314 static ssize_t
mxs_lradc_adc_show_scale_avail(struct device
*dev
,
315 struct device_attribute
*attr
,
318 struct iio_dev
*iio
= dev_to_iio_dev(dev
);
319 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
320 struct iio_dev_attr
*iio_attr
= to_iio_dev_attr(attr
);
323 ch
= iio_attr
->address
;
324 for (i
= 0; i
< ARRAY_SIZE(adc
->scale_avail
[ch
]); i
++)
325 len
+= sprintf(buf
+ len
, "%u.%09u ",
326 adc
->scale_avail
[ch
][i
].integer
,
327 adc
->scale_avail
[ch
][i
].nano
);
329 len
+= sprintf(buf
+ len
, "\n");
334 #define SHOW_SCALE_AVAILABLE_ATTR(ch)\
335 IIO_DEVICE_ATTR(in_voltage##ch##_scale_available, 0444,\
336 mxs_lradc_adc_show_scale_avail, NULL, ch)
338 static SHOW_SCALE_AVAILABLE_ATTR(0);
339 static SHOW_SCALE_AVAILABLE_ATTR(1);
340 static SHOW_SCALE_AVAILABLE_ATTR(2);
341 static SHOW_SCALE_AVAILABLE_ATTR(3);
342 static SHOW_SCALE_AVAILABLE_ATTR(4);
343 static SHOW_SCALE_AVAILABLE_ATTR(5);
344 static SHOW_SCALE_AVAILABLE_ATTR(6);
345 static SHOW_SCALE_AVAILABLE_ATTR(7);
346 static SHOW_SCALE_AVAILABLE_ATTR(10);
347 static SHOW_SCALE_AVAILABLE_ATTR(11);
348 static SHOW_SCALE_AVAILABLE_ATTR(12);
349 static SHOW_SCALE_AVAILABLE_ATTR(13);
350 static SHOW_SCALE_AVAILABLE_ATTR(14);
351 static SHOW_SCALE_AVAILABLE_ATTR(15);
353 static struct attribute
*mxs_lradc_adc_attributes
[] = {
354 &iio_dev_attr_in_voltage0_scale_available
.dev_attr
.attr
,
355 &iio_dev_attr_in_voltage1_scale_available
.dev_attr
.attr
,
356 &iio_dev_attr_in_voltage2_scale_available
.dev_attr
.attr
,
357 &iio_dev_attr_in_voltage3_scale_available
.dev_attr
.attr
,
358 &iio_dev_attr_in_voltage4_scale_available
.dev_attr
.attr
,
359 &iio_dev_attr_in_voltage5_scale_available
.dev_attr
.attr
,
360 &iio_dev_attr_in_voltage6_scale_available
.dev_attr
.attr
,
361 &iio_dev_attr_in_voltage7_scale_available
.dev_attr
.attr
,
362 &iio_dev_attr_in_voltage10_scale_available
.dev_attr
.attr
,
363 &iio_dev_attr_in_voltage11_scale_available
.dev_attr
.attr
,
364 &iio_dev_attr_in_voltage12_scale_available
.dev_attr
.attr
,
365 &iio_dev_attr_in_voltage13_scale_available
.dev_attr
.attr
,
366 &iio_dev_attr_in_voltage14_scale_available
.dev_attr
.attr
,
367 &iio_dev_attr_in_voltage15_scale_available
.dev_attr
.attr
,
371 static const struct attribute_group mxs_lradc_adc_attribute_group
= {
372 .attrs
= mxs_lradc_adc_attributes
,
375 static const struct iio_info mxs_lradc_adc_iio_info
= {
376 .read_raw
= mxs_lradc_adc_read_raw
,
377 .write_raw
= mxs_lradc_adc_write_raw
,
378 .write_raw_get_fmt
= mxs_lradc_adc_write_raw_get_fmt
,
379 .attrs
= &mxs_lradc_adc_attribute_group
,
383 static irqreturn_t
mxs_lradc_adc_handle_irq(int irq
, void *data
)
385 struct iio_dev
*iio
= data
;
386 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
387 struct mxs_lradc
*lradc
= adc
->lradc
;
388 unsigned long reg
= readl(adc
->base
+ LRADC_CTRL1
);
391 if (!(reg
& mxs_lradc_irq_mask(lradc
)))
394 if (iio_buffer_enabled(iio
)) {
395 if (reg
& lradc
->buffer_vchans
) {
396 spin_lock_irqsave(&adc
->lock
, flags
);
397 iio_trigger_poll(iio
->trig
);
398 spin_unlock_irqrestore(&adc
->lock
, flags
);
400 } else if (reg
& LRADC_CTRL1_LRADC_IRQ(0)) {
401 complete(&adc
->completion
);
404 writel(reg
& mxs_lradc_irq_mask(lradc
),
405 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_CLR
);
411 /* Trigger handling */
412 static irqreturn_t
mxs_lradc_adc_trigger_handler(int irq
, void *p
)
414 struct iio_poll_func
*pf
= p
;
415 struct iio_dev
*iio
= pf
->indio_dev
;
416 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
417 const u32 chan_value
= LRADC_CH_ACCUMULATE
|
418 ((LRADC_DELAY_TIMER_LOOP
- 1) << LRADC_CH_NUM_SAMPLES_OFFSET
);
419 unsigned int i
, j
= 0;
421 for_each_set_bit(i
, iio
->active_scan_mask
, LRADC_MAX_TOTAL_CHANS
) {
422 adc
->buffer
[j
] = readl(adc
->base
+ LRADC_CH(j
));
423 writel(chan_value
, adc
->base
+ LRADC_CH(j
));
424 adc
->buffer
[j
] &= LRADC_CH_VALUE_MASK
;
425 adc
->buffer
[j
] /= LRADC_DELAY_TIMER_LOOP
;
429 iio_push_to_buffers_with_timestamp(iio
, adc
->buffer
, pf
->timestamp
);
431 iio_trigger_notify_done(iio
->trig
);
436 static int mxs_lradc_adc_configure_trigger(struct iio_trigger
*trig
, bool state
)
438 struct iio_dev
*iio
= iio_trigger_get_drvdata(trig
);
439 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
440 const u32 st
= state
? STMP_OFFSET_REG_SET
: STMP_OFFSET_REG_CLR
;
442 writel(LRADC_DELAY_KICK
, adc
->base
+ (LRADC_DELAY(0) + st
));
447 static const struct iio_trigger_ops mxs_lradc_adc_trigger_ops
= {
448 .set_trigger_state
= &mxs_lradc_adc_configure_trigger
,
451 static int mxs_lradc_adc_trigger_init(struct iio_dev
*iio
)
454 struct iio_trigger
*trig
;
455 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
457 trig
= devm_iio_trigger_alloc(&iio
->dev
, "%s-dev%i", iio
->name
,
462 trig
->dev
.parent
= adc
->dev
;
463 iio_trigger_set_drvdata(trig
, iio
);
464 trig
->ops
= &mxs_lradc_adc_trigger_ops
;
466 ret
= iio_trigger_register(trig
);
475 static void mxs_lradc_adc_trigger_remove(struct iio_dev
*iio
)
477 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
479 iio_trigger_unregister(adc
->trig
);
482 static int mxs_lradc_adc_buffer_preenable(struct iio_dev
*iio
)
484 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
485 struct mxs_lradc
*lradc
= adc
->lradc
;
487 unsigned long enable
= 0;
491 const u32 chan_value
= LRADC_CH_ACCUMULATE
|
492 ((LRADC_DELAY_TIMER_LOOP
- 1) << LRADC_CH_NUM_SAMPLES_OFFSET
);
494 if (lradc
->soc
== IMX28_LRADC
)
495 writel(lradc
->buffer_vchans
<< LRADC_CTRL1_LRADC_IRQ_EN_OFFSET
,
496 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_CLR
);
497 writel(lradc
->buffer_vchans
,
498 adc
->base
+ LRADC_CTRL0
+ STMP_OFFSET_REG_CLR
);
500 for_each_set_bit(chan
, iio
->active_scan_mask
, LRADC_MAX_TOTAL_CHANS
) {
501 ctrl4_set
|= chan
<< LRADC_CTRL4_LRADCSELECT_OFFSET(ofs
);
502 ctrl4_clr
|= LRADC_CTRL4_LRADCSELECT_MASK(ofs
);
503 ctrl1_irq
|= LRADC_CTRL1_LRADC_IRQ_EN(ofs
);
504 writel(chan_value
, adc
->base
+ LRADC_CH(ofs
));
505 bitmap_set(&enable
, ofs
, 1);
509 writel(LRADC_DELAY_TRIGGER_LRADCS_MASK
| LRADC_DELAY_KICK
,
510 adc
->base
+ LRADC_DELAY(0) + STMP_OFFSET_REG_CLR
);
511 writel(ctrl4_clr
, adc
->base
+ LRADC_CTRL4
+ STMP_OFFSET_REG_CLR
);
512 writel(ctrl4_set
, adc
->base
+ LRADC_CTRL4
+ STMP_OFFSET_REG_SET
);
513 writel(ctrl1_irq
, adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_SET
);
514 writel(enable
<< LRADC_DELAY_TRIGGER_LRADCS_OFFSET
,
515 adc
->base
+ LRADC_DELAY(0) + STMP_OFFSET_REG_SET
);
520 static int mxs_lradc_adc_buffer_postdisable(struct iio_dev
*iio
)
522 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
523 struct mxs_lradc
*lradc
= adc
->lradc
;
525 writel(LRADC_DELAY_TRIGGER_LRADCS_MASK
| LRADC_DELAY_KICK
,
526 adc
->base
+ LRADC_DELAY(0) + STMP_OFFSET_REG_CLR
);
528 writel(lradc
->buffer_vchans
,
529 adc
->base
+ LRADC_CTRL0
+ STMP_OFFSET_REG_CLR
);
530 if (lradc
->soc
== IMX28_LRADC
)
531 writel(lradc
->buffer_vchans
<< LRADC_CTRL1_LRADC_IRQ_EN_OFFSET
,
532 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_CLR
);
537 static bool mxs_lradc_adc_validate_scan_mask(struct iio_dev
*iio
,
538 const unsigned long *mask
)
540 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
541 struct mxs_lradc
*lradc
= adc
->lradc
;
542 const int map_chans
= bitmap_weight(mask
, LRADC_MAX_TOTAL_CHANS
);
544 unsigned long rsvd_mask
= 0;
546 if (lradc
->use_touchbutton
)
547 rsvd_mask
|= CHAN_MASK_TOUCHBUTTON
;
548 if (lradc
->touchscreen_wire
== MXS_LRADC_TOUCHSCREEN_4WIRE
)
549 rsvd_mask
|= CHAN_MASK_TOUCHSCREEN_4WIRE
;
550 if (lradc
->touchscreen_wire
== MXS_LRADC_TOUCHSCREEN_5WIRE
)
551 rsvd_mask
|= CHAN_MASK_TOUCHSCREEN_5WIRE
;
553 if (lradc
->use_touchbutton
)
555 if (lradc
->touchscreen_wire
)
558 /* Test for attempts to map channels with special mode of operation. */
559 if (bitmap_intersects(mask
, &rsvd_mask
, LRADC_MAX_TOTAL_CHANS
))
562 /* Test for attempts to map more channels then available slots. */
563 if (map_chans
+ rsvd_chans
> LRADC_MAX_MAPPED_CHANS
)
569 static const struct iio_buffer_setup_ops mxs_lradc_adc_buffer_ops
= {
570 .preenable
= &mxs_lradc_adc_buffer_preenable
,
571 .postenable
= &iio_triggered_buffer_postenable
,
572 .predisable
= &iio_triggered_buffer_predisable
,
573 .postdisable
= &mxs_lradc_adc_buffer_postdisable
,
574 .validate_scan_mask
= &mxs_lradc_adc_validate_scan_mask
,
577 /* Driver initialization */
578 #define MXS_ADC_CHAN(idx, chan_type, name) { \
579 .type = (chan_type), \
581 .scan_index = (idx), \
582 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
583 BIT(IIO_CHAN_INFO_SCALE), \
588 .realbits = LRADC_RESOLUTION, \
591 .datasheet_name = (name), \
594 static const struct iio_chan_spec mx23_lradc_chan_spec
[] = {
595 MXS_ADC_CHAN(0, IIO_VOLTAGE
, "LRADC0"),
596 MXS_ADC_CHAN(1, IIO_VOLTAGE
, "LRADC1"),
597 MXS_ADC_CHAN(2, IIO_VOLTAGE
, "LRADC2"),
598 MXS_ADC_CHAN(3, IIO_VOLTAGE
, "LRADC3"),
599 MXS_ADC_CHAN(4, IIO_VOLTAGE
, "LRADC4"),
600 MXS_ADC_CHAN(5, IIO_VOLTAGE
, "LRADC5"),
601 MXS_ADC_CHAN(6, IIO_VOLTAGE
, "VDDIO"),
602 MXS_ADC_CHAN(7, IIO_VOLTAGE
, "VBATT"),
603 /* Combined Temperature sensors */
608 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
609 BIT(IIO_CHAN_INFO_OFFSET
) |
610 BIT(IIO_CHAN_INFO_SCALE
),
612 .scan_type
= {.sign
= 'u', .realbits
= 18, .storagebits
= 32,},
613 .datasheet_name
= "TEMP_DIE",
615 /* Hidden channel to keep indexes */
622 MXS_ADC_CHAN(10, IIO_VOLTAGE
, NULL
),
623 MXS_ADC_CHAN(11, IIO_VOLTAGE
, NULL
),
624 MXS_ADC_CHAN(12, IIO_VOLTAGE
, "USB_DP"),
625 MXS_ADC_CHAN(13, IIO_VOLTAGE
, "USB_DN"),
626 MXS_ADC_CHAN(14, IIO_VOLTAGE
, "VBG"),
627 MXS_ADC_CHAN(15, IIO_VOLTAGE
, "VDD5V"),
630 static const struct iio_chan_spec mx28_lradc_chan_spec
[] = {
631 MXS_ADC_CHAN(0, IIO_VOLTAGE
, "LRADC0"),
632 MXS_ADC_CHAN(1, IIO_VOLTAGE
, "LRADC1"),
633 MXS_ADC_CHAN(2, IIO_VOLTAGE
, "LRADC2"),
634 MXS_ADC_CHAN(3, IIO_VOLTAGE
, "LRADC3"),
635 MXS_ADC_CHAN(4, IIO_VOLTAGE
, "LRADC4"),
636 MXS_ADC_CHAN(5, IIO_VOLTAGE
, "LRADC5"),
637 MXS_ADC_CHAN(6, IIO_VOLTAGE
, "LRADC6"),
638 MXS_ADC_CHAN(7, IIO_VOLTAGE
, "VBATT"),
639 /* Combined Temperature sensors */
644 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
645 BIT(IIO_CHAN_INFO_OFFSET
) |
646 BIT(IIO_CHAN_INFO_SCALE
),
648 .scan_type
= {.sign
= 'u', .realbits
= 18, .storagebits
= 32,},
649 .datasheet_name
= "TEMP_DIE",
651 /* Hidden channel to keep indexes */
658 MXS_ADC_CHAN(10, IIO_VOLTAGE
, "VDDIO"),
659 MXS_ADC_CHAN(11, IIO_VOLTAGE
, "VTH"),
660 MXS_ADC_CHAN(12, IIO_VOLTAGE
, "VDDA"),
661 MXS_ADC_CHAN(13, IIO_VOLTAGE
, "VDDD"),
662 MXS_ADC_CHAN(14, IIO_VOLTAGE
, "VBG"),
663 MXS_ADC_CHAN(15, IIO_VOLTAGE
, "VDD5V"),
666 static void mxs_lradc_adc_hw_init(struct mxs_lradc_adc
*adc
)
668 /* The ADC always uses DELAY CHANNEL 0. */
670 (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET
+ 0)) |
671 (LRADC_DELAY_TIMER_PER
<< LRADC_DELAY_DELAY_OFFSET
);
673 /* Configure DELAY CHANNEL 0 for generic ADC sampling. */
674 writel(adc_cfg
, adc
->base
+ LRADC_DELAY(0));
677 * Start internal temperature sensing by clearing bit
678 * HW_LRADC_CTRL2_TEMPSENSE_PWD. This bit can be left cleared
681 writel(0, adc
->base
+ LRADC_CTRL2
);
684 static void mxs_lradc_adc_hw_stop(struct mxs_lradc_adc
*adc
)
686 writel(0, adc
->base
+ LRADC_DELAY(0));
689 static int mxs_lradc_adc_probe(struct platform_device
*pdev
)
691 struct device
*dev
= &pdev
->dev
;
692 struct mxs_lradc
*lradc
= dev_get_drvdata(dev
->parent
);
693 struct mxs_lradc_adc
*adc
;
695 struct resource
*iores
;
696 int ret
, irq
, virq
, i
, s
, n
;
698 const char **irq_name
;
700 /* Allocate the IIO device. */
701 iio
= devm_iio_device_alloc(dev
, sizeof(*adc
));
703 dev_err(dev
, "Failed to allocate IIO device\n");
711 iores
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
715 adc
->base
= devm_ioremap(dev
, iores
->start
, resource_size(iores
));
719 init_completion(&adc
->completion
);
720 spin_lock_init(&adc
->lock
);
722 platform_set_drvdata(pdev
, iio
);
724 iio
->name
= pdev
->name
;
725 iio
->dev
.parent
= dev
;
726 iio
->dev
.of_node
= dev
->parent
->of_node
;
727 iio
->info
= &mxs_lradc_adc_iio_info
;
728 iio
->modes
= INDIO_DIRECT_MODE
;
729 iio
->masklength
= LRADC_MAX_TOTAL_CHANS
;
731 if (lradc
->soc
== IMX23_LRADC
) {
732 iio
->channels
= mx23_lradc_chan_spec
;
733 iio
->num_channels
= ARRAY_SIZE(mx23_lradc_chan_spec
);
734 irq_name
= mx23_lradc_adc_irq_names
;
735 n
= ARRAY_SIZE(mx23_lradc_adc_irq_names
);
737 iio
->channels
= mx28_lradc_chan_spec
;
738 iio
->num_channels
= ARRAY_SIZE(mx28_lradc_chan_spec
);
739 irq_name
= mx28_lradc_adc_irq_names
;
740 n
= ARRAY_SIZE(mx28_lradc_adc_irq_names
);
743 ret
= stmp_reset_block(adc
->base
);
747 for (i
= 0; i
< n
; i
++) {
748 irq
= platform_get_irq_byname(pdev
, irq_name
[i
]);
752 virq
= irq_of_parse_and_map(dev
->parent
->of_node
, irq
);
754 ret
= devm_request_irq(dev
, virq
, mxs_lradc_adc_handle_irq
,
755 0, irq_name
[i
], iio
);
760 ret
= mxs_lradc_adc_trigger_init(iio
);
764 ret
= iio_triggered_buffer_setup(iio
, &iio_pollfunc_store_time
,
765 &mxs_lradc_adc_trigger_handler
,
766 &mxs_lradc_adc_buffer_ops
);
770 adc
->vref_mv
= mxs_lradc_adc_vref_mv
[lradc
->soc
];
772 /* Populate available ADC input ranges */
773 for (i
= 0; i
< LRADC_MAX_TOTAL_CHANS
; i
++) {
774 for (s
= 0; s
< ARRAY_SIZE(adc
->scale_avail
[i
]); s
++) {
776 * [s=0] = optional divider by two disabled (default)
777 * [s=1] = optional divider by two enabled
779 * The scale is calculated by doing:
780 * Vref >> (realbits - s)
781 * which multiplies by two on the second component
784 scale_uv
= ((u64
)adc
->vref_mv
[i
] * 100000000) >>
785 (LRADC_RESOLUTION
- s
);
786 adc
->scale_avail
[i
][s
].nano
=
787 do_div(scale_uv
, 100000000) * 10;
788 adc
->scale_avail
[i
][s
].integer
= scale_uv
;
792 /* Configure the hardware. */
793 mxs_lradc_adc_hw_init(adc
);
795 /* Register IIO device. */
796 ret
= iio_device_register(iio
);
798 dev_err(dev
, "Failed to register IIO device\n");
805 mxs_lradc_adc_hw_stop(adc
);
806 mxs_lradc_adc_trigger_remove(iio
);
808 iio_triggered_buffer_cleanup(iio
);
812 static int mxs_lradc_adc_remove(struct platform_device
*pdev
)
814 struct iio_dev
*iio
= platform_get_drvdata(pdev
);
815 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
817 iio_device_unregister(iio
);
818 mxs_lradc_adc_hw_stop(adc
);
819 mxs_lradc_adc_trigger_remove(iio
);
820 iio_triggered_buffer_cleanup(iio
);
825 static struct platform_driver mxs_lradc_adc_driver
= {
827 .name
= "mxs-lradc-adc",
829 .probe
= mxs_lradc_adc_probe
,
830 .remove
= mxs_lradc_adc_remove
,
832 module_platform_driver(mxs_lradc_adc_driver
);
834 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
835 MODULE_DESCRIPTION("Freescale MXS LRADC driver general purpose ADC driver");
836 MODULE_LICENSE("GPL");
837 MODULE_ALIAS("platform:mxs-lradc-adc");