2 * Freescale MXS LRADC ADC driver
4 * Copyright (c) 2012 DENX Software Engineering, GmbH.
5 * Copyright (c) 2017 Ksenija Stanojevic <ksenija.stanojevic@gmail.com>
8 * Marek Vasut <marex@denx.de>
9 * Ksenija Stanojevic <ksenija.stanojevic@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
22 #include <linux/completion.h>
23 #include <linux/device.h>
24 #include <linux/err.h>
25 #include <linux/interrupt.h>
26 #include <linux/mfd/core.h>
27 #include <linux/mfd/mxs-lradc.h>
28 #include <linux/module.h>
29 #include <linux/of_irq.h>
30 #include <linux/platform_device.h>
31 #include <linux/sysfs.h>
33 #include <linux/iio/buffer.h>
34 #include <linux/iio/iio.h>
35 #include <linux/iio/trigger.h>
36 #include <linux/iio/trigger_consumer.h>
37 #include <linux/iio/triggered_buffer.h>
38 #include <linux/iio/sysfs.h>
41 * Make this runtime configurable if necessary. Currently, if the buffered mode
42 * is enabled, the LRADC takes LRADC_DELAY_TIMER_LOOP samples of data before
43 * triggering IRQ. The sampling happens every (LRADC_DELAY_TIMER_PER / 2000)
44 * seconds. The result is that the samples arrive every 500mS.
46 #define LRADC_DELAY_TIMER_PER 200
47 #define LRADC_DELAY_TIMER_LOOP 5
49 #define VREF_MV_BASE 1850
51 static const char *mx23_lradc_adc_irq_names
[] = {
60 static const char *mx28_lradc_adc_irq_names
[] = {
73 static const u32 mxs_lradc_adc_vref_mv
[][LRADC_MAX_TOTAL_CHANS
] = {
75 VREF_MV_BASE
, /* CH0 */
76 VREF_MV_BASE
, /* CH1 */
77 VREF_MV_BASE
, /* CH2 */
78 VREF_MV_BASE
, /* CH3 */
79 VREF_MV_BASE
, /* CH4 */
80 VREF_MV_BASE
, /* CH5 */
81 VREF_MV_BASE
* 2, /* CH6 VDDIO */
82 VREF_MV_BASE
* 4, /* CH7 VBATT */
83 VREF_MV_BASE
, /* CH8 Temp sense 0 */
84 VREF_MV_BASE
, /* CH9 Temp sense 1 */
85 VREF_MV_BASE
, /* CH10 */
86 VREF_MV_BASE
, /* CH11 */
87 VREF_MV_BASE
, /* CH12 USB_DP */
88 VREF_MV_BASE
, /* CH13 USB_DN */
89 VREF_MV_BASE
, /* CH14 VBG */
90 VREF_MV_BASE
* 4, /* CH15 VDD5V */
93 VREF_MV_BASE
, /* CH0 */
94 VREF_MV_BASE
, /* CH1 */
95 VREF_MV_BASE
, /* CH2 */
96 VREF_MV_BASE
, /* CH3 */
97 VREF_MV_BASE
, /* CH4 */
98 VREF_MV_BASE
, /* CH5 */
99 VREF_MV_BASE
, /* CH6 */
100 VREF_MV_BASE
* 4, /* CH7 VBATT */
101 VREF_MV_BASE
, /* CH8 Temp sense 0 */
102 VREF_MV_BASE
, /* CH9 Temp sense 1 */
103 VREF_MV_BASE
* 2, /* CH10 VDDIO */
104 VREF_MV_BASE
, /* CH11 VTH */
105 VREF_MV_BASE
* 2, /* CH12 VDDA */
106 VREF_MV_BASE
, /* CH13 VDDD */
107 VREF_MV_BASE
, /* CH14 VBG */
108 VREF_MV_BASE
* 4, /* CH15 VDD5V */
112 enum mxs_lradc_divbytwo
{
113 MXS_LRADC_DIV_DISABLED
= 0,
114 MXS_LRADC_DIV_ENABLED
,
117 struct mxs_lradc_scale
{
118 unsigned int integer
;
122 struct mxs_lradc_adc
{
123 struct mxs_lradc
*lradc
;
128 struct iio_trigger
*trig
;
129 struct completion completion
;
133 struct mxs_lradc_scale scale_avail
[LRADC_MAX_TOTAL_CHANS
][2];
134 unsigned long is_divided
;
138 /* Raw I/O operations */
139 static int mxs_lradc_adc_read_single(struct iio_dev
*iio_dev
, int chan
,
142 struct mxs_lradc_adc
*adc
= iio_priv(iio_dev
);
143 struct mxs_lradc
*lradc
= adc
->lradc
;
147 * See if there is no buffered operation in progress. If there is simply
148 * bail out. This can be improved to support both buffered and raw IO at
149 * the same time, yet the code becomes horribly complicated. Therefore I
150 * applied KISS principle here.
152 ret
= iio_device_claim_direct_mode(iio_dev
);
156 reinit_completion(&adc
->completion
);
159 * No buffered operation in progress, map the channel and trigger it.
160 * Virtual channel 0 is always used here as the others are always not
161 * used if doing raw sampling.
163 if (lradc
->soc
== IMX28_LRADC
)
164 writel(LRADC_CTRL1_LRADC_IRQ_EN(0),
165 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_CLR
);
166 writel(0x1, adc
->base
+ LRADC_CTRL0
+ STMP_OFFSET_REG_CLR
);
168 /* Enable / disable the divider per requirement */
169 if (test_bit(chan
, &adc
->is_divided
))
170 writel(1 << LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET
,
171 adc
->base
+ LRADC_CTRL2
+ STMP_OFFSET_REG_SET
);
173 writel(1 << LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET
,
174 adc
->base
+ LRADC_CTRL2
+ STMP_OFFSET_REG_CLR
);
176 /* Clean the slot's previous content, then set new one. */
177 writel(LRADC_CTRL4_LRADCSELECT_MASK(0),
178 adc
->base
+ LRADC_CTRL4
+ STMP_OFFSET_REG_CLR
);
179 writel(chan
, adc
->base
+ LRADC_CTRL4
+ STMP_OFFSET_REG_SET
);
181 writel(0, adc
->base
+ LRADC_CH(0));
183 /* Enable the IRQ and start sampling the channel. */
184 writel(LRADC_CTRL1_LRADC_IRQ_EN(0),
185 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_SET
);
186 writel(BIT(0), adc
->base
+ LRADC_CTRL0
+ STMP_OFFSET_REG_SET
);
188 /* Wait for completion on the channel, 1 second max. */
189 ret
= wait_for_completion_killable_timeout(&adc
->completion
, HZ
);
196 *val
= readl(adc
->base
+ LRADC_CH(0)) & LRADC_CH_VALUE_MASK
;
200 writel(LRADC_CTRL1_LRADC_IRQ_EN(0),
201 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_CLR
);
203 iio_device_release_direct_mode(iio_dev
);
208 static int mxs_lradc_adc_read_temp(struct iio_dev
*iio_dev
, int *val
)
212 ret
= mxs_lradc_adc_read_single(iio_dev
, 8, &min
);
213 if (ret
!= IIO_VAL_INT
)
216 ret
= mxs_lradc_adc_read_single(iio_dev
, 9, &max
);
217 if (ret
!= IIO_VAL_INT
)
225 static int mxs_lradc_adc_read_raw(struct iio_dev
*iio_dev
,
226 const struct iio_chan_spec
*chan
,
227 int *val
, int *val2
, long m
)
229 struct mxs_lradc_adc
*adc
= iio_priv(iio_dev
);
232 case IIO_CHAN_INFO_RAW
:
233 if (chan
->type
== IIO_TEMP
)
234 return mxs_lradc_adc_read_temp(iio_dev
, val
);
236 return mxs_lradc_adc_read_single(iio_dev
, chan
->channel
, val
);
238 case IIO_CHAN_INFO_SCALE
:
239 if (chan
->type
== IIO_TEMP
) {
241 * From the datasheet, we have to multiply by 1.012 and
246 return IIO_VAL_INT_PLUS_MICRO
;
249 *val
= adc
->vref_mv
[chan
->channel
];
250 *val2
= chan
->scan_type
.realbits
-
251 test_bit(chan
->channel
, &adc
->is_divided
);
252 return IIO_VAL_FRACTIONAL_LOG2
;
254 case IIO_CHAN_INFO_OFFSET
:
255 if (chan
->type
== IIO_TEMP
) {
257 * The calculated value from the ADC is in Kelvin, we
258 * want Celsius for hwmon so the offset is -273.15
259 * The offset is applied before scaling so it is
260 * actually -213.15 * 4 / 1.012 = -1079.644268
265 return IIO_VAL_INT_PLUS_MICRO
;
277 static int mxs_lradc_adc_write_raw(struct iio_dev
*iio_dev
,
278 const struct iio_chan_spec
*chan
,
279 int val
, int val2
, long m
)
281 struct mxs_lradc_adc
*adc
= iio_priv(iio_dev
);
282 struct mxs_lradc_scale
*scale_avail
=
283 adc
->scale_avail
[chan
->channel
];
286 ret
= iio_device_claim_direct_mode(iio_dev
);
291 case IIO_CHAN_INFO_SCALE
:
293 if (val
== scale_avail
[MXS_LRADC_DIV_DISABLED
].integer
&&
294 val2
== scale_avail
[MXS_LRADC_DIV_DISABLED
].nano
) {
295 /* divider by two disabled */
296 clear_bit(chan
->channel
, &adc
->is_divided
);
298 } else if (val
== scale_avail
[MXS_LRADC_DIV_ENABLED
].integer
&&
299 val2
== scale_avail
[MXS_LRADC_DIV_ENABLED
].nano
) {
300 /* divider by two enabled */
301 set_bit(chan
->channel
, &adc
->is_divided
);
311 iio_device_release_direct_mode(iio_dev
);
316 static int mxs_lradc_adc_write_raw_get_fmt(struct iio_dev
*iio_dev
,
317 const struct iio_chan_spec
*chan
,
320 return IIO_VAL_INT_PLUS_NANO
;
323 static ssize_t
mxs_lradc_adc_show_scale_avail(struct device
*dev
,
324 struct device_attribute
*attr
,
327 struct iio_dev
*iio
= dev_to_iio_dev(dev
);
328 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
329 struct iio_dev_attr
*iio_attr
= to_iio_dev_attr(attr
);
332 ch
= iio_attr
->address
;
333 for (i
= 0; i
< ARRAY_SIZE(adc
->scale_avail
[ch
]); i
++)
334 len
+= sprintf(buf
+ len
, "%u.%09u ",
335 adc
->scale_avail
[ch
][i
].integer
,
336 adc
->scale_avail
[ch
][i
].nano
);
338 len
+= sprintf(buf
+ len
, "\n");
343 #define SHOW_SCALE_AVAILABLE_ATTR(ch)\
344 IIO_DEVICE_ATTR(in_voltage##ch##_scale_available, 0444,\
345 mxs_lradc_adc_show_scale_avail, NULL, ch)
347 static SHOW_SCALE_AVAILABLE_ATTR(0);
348 static SHOW_SCALE_AVAILABLE_ATTR(1);
349 static SHOW_SCALE_AVAILABLE_ATTR(2);
350 static SHOW_SCALE_AVAILABLE_ATTR(3);
351 static SHOW_SCALE_AVAILABLE_ATTR(4);
352 static SHOW_SCALE_AVAILABLE_ATTR(5);
353 static SHOW_SCALE_AVAILABLE_ATTR(6);
354 static SHOW_SCALE_AVAILABLE_ATTR(7);
355 static SHOW_SCALE_AVAILABLE_ATTR(10);
356 static SHOW_SCALE_AVAILABLE_ATTR(11);
357 static SHOW_SCALE_AVAILABLE_ATTR(12);
358 static SHOW_SCALE_AVAILABLE_ATTR(13);
359 static SHOW_SCALE_AVAILABLE_ATTR(14);
360 static SHOW_SCALE_AVAILABLE_ATTR(15);
362 static struct attribute
*mxs_lradc_adc_attributes
[] = {
363 &iio_dev_attr_in_voltage0_scale_available
.dev_attr
.attr
,
364 &iio_dev_attr_in_voltage1_scale_available
.dev_attr
.attr
,
365 &iio_dev_attr_in_voltage2_scale_available
.dev_attr
.attr
,
366 &iio_dev_attr_in_voltage3_scale_available
.dev_attr
.attr
,
367 &iio_dev_attr_in_voltage4_scale_available
.dev_attr
.attr
,
368 &iio_dev_attr_in_voltage5_scale_available
.dev_attr
.attr
,
369 &iio_dev_attr_in_voltage6_scale_available
.dev_attr
.attr
,
370 &iio_dev_attr_in_voltage7_scale_available
.dev_attr
.attr
,
371 &iio_dev_attr_in_voltage10_scale_available
.dev_attr
.attr
,
372 &iio_dev_attr_in_voltage11_scale_available
.dev_attr
.attr
,
373 &iio_dev_attr_in_voltage12_scale_available
.dev_attr
.attr
,
374 &iio_dev_attr_in_voltage13_scale_available
.dev_attr
.attr
,
375 &iio_dev_attr_in_voltage14_scale_available
.dev_attr
.attr
,
376 &iio_dev_attr_in_voltage15_scale_available
.dev_attr
.attr
,
380 static const struct attribute_group mxs_lradc_adc_attribute_group
= {
381 .attrs
= mxs_lradc_adc_attributes
,
384 static const struct iio_info mxs_lradc_adc_iio_info
= {
385 .read_raw
= mxs_lradc_adc_read_raw
,
386 .write_raw
= mxs_lradc_adc_write_raw
,
387 .write_raw_get_fmt
= mxs_lradc_adc_write_raw_get_fmt
,
388 .attrs
= &mxs_lradc_adc_attribute_group
,
392 static irqreturn_t
mxs_lradc_adc_handle_irq(int irq
, void *data
)
394 struct iio_dev
*iio
= data
;
395 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
396 struct mxs_lradc
*lradc
= adc
->lradc
;
397 unsigned long reg
= readl(adc
->base
+ LRADC_CTRL1
);
400 if (!(reg
& mxs_lradc_irq_mask(lradc
)))
403 if (iio_buffer_enabled(iio
)) {
404 if (reg
& lradc
->buffer_vchans
) {
405 spin_lock_irqsave(&adc
->lock
, flags
);
406 iio_trigger_poll(iio
->trig
);
407 spin_unlock_irqrestore(&adc
->lock
, flags
);
409 } else if (reg
& LRADC_CTRL1_LRADC_IRQ(0)) {
410 complete(&adc
->completion
);
413 writel(reg
& mxs_lradc_irq_mask(lradc
),
414 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_CLR
);
420 /* Trigger handling */
421 static irqreturn_t
mxs_lradc_adc_trigger_handler(int irq
, void *p
)
423 struct iio_poll_func
*pf
= p
;
424 struct iio_dev
*iio
= pf
->indio_dev
;
425 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
426 const u32 chan_value
= LRADC_CH_ACCUMULATE
|
427 ((LRADC_DELAY_TIMER_LOOP
- 1) << LRADC_CH_NUM_SAMPLES_OFFSET
);
428 unsigned int i
, j
= 0;
430 for_each_set_bit(i
, iio
->active_scan_mask
, LRADC_MAX_TOTAL_CHANS
) {
431 adc
->buffer
[j
] = readl(adc
->base
+ LRADC_CH(j
));
432 writel(chan_value
, adc
->base
+ LRADC_CH(j
));
433 adc
->buffer
[j
] &= LRADC_CH_VALUE_MASK
;
434 adc
->buffer
[j
] /= LRADC_DELAY_TIMER_LOOP
;
438 iio_push_to_buffers_with_timestamp(iio
, adc
->buffer
, pf
->timestamp
);
440 iio_trigger_notify_done(iio
->trig
);
445 static int mxs_lradc_adc_configure_trigger(struct iio_trigger
*trig
, bool state
)
447 struct iio_dev
*iio
= iio_trigger_get_drvdata(trig
);
448 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
449 const u32 st
= state
? STMP_OFFSET_REG_SET
: STMP_OFFSET_REG_CLR
;
451 writel(LRADC_DELAY_KICK
, adc
->base
+ (LRADC_DELAY(0) + st
));
456 static const struct iio_trigger_ops mxs_lradc_adc_trigger_ops
= {
457 .set_trigger_state
= &mxs_lradc_adc_configure_trigger
,
460 static int mxs_lradc_adc_trigger_init(struct iio_dev
*iio
)
463 struct iio_trigger
*trig
;
464 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
466 trig
= devm_iio_trigger_alloc(&iio
->dev
, "%s-dev%i", iio
->name
,
469 trig
->dev
.parent
= adc
->dev
;
470 iio_trigger_set_drvdata(trig
, iio
);
471 trig
->ops
= &mxs_lradc_adc_trigger_ops
;
473 ret
= iio_trigger_register(trig
);
482 static void mxs_lradc_adc_trigger_remove(struct iio_dev
*iio
)
484 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
486 iio_trigger_unregister(adc
->trig
);
489 static int mxs_lradc_adc_buffer_preenable(struct iio_dev
*iio
)
491 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
492 struct mxs_lradc
*lradc
= adc
->lradc
;
494 unsigned long enable
= 0;
498 const u32 chan_value
= LRADC_CH_ACCUMULATE
|
499 ((LRADC_DELAY_TIMER_LOOP
- 1) << LRADC_CH_NUM_SAMPLES_OFFSET
);
501 if (lradc
->soc
== IMX28_LRADC
)
502 writel(lradc
->buffer_vchans
<< LRADC_CTRL1_LRADC_IRQ_EN_OFFSET
,
503 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_CLR
);
504 writel(lradc
->buffer_vchans
,
505 adc
->base
+ LRADC_CTRL0
+ STMP_OFFSET_REG_CLR
);
507 for_each_set_bit(chan
, iio
->active_scan_mask
, LRADC_MAX_TOTAL_CHANS
) {
508 ctrl4_set
|= chan
<< LRADC_CTRL4_LRADCSELECT_OFFSET(ofs
);
509 ctrl4_clr
|= LRADC_CTRL4_LRADCSELECT_MASK(ofs
);
510 ctrl1_irq
|= LRADC_CTRL1_LRADC_IRQ_EN(ofs
);
511 writel(chan_value
, adc
->base
+ LRADC_CH(ofs
));
512 bitmap_set(&enable
, ofs
, 1);
516 writel(LRADC_DELAY_TRIGGER_LRADCS_MASK
| LRADC_DELAY_KICK
,
517 adc
->base
+ LRADC_DELAY(0) + STMP_OFFSET_REG_CLR
);
518 writel(ctrl4_clr
, adc
->base
+ LRADC_CTRL4
+ STMP_OFFSET_REG_CLR
);
519 writel(ctrl4_set
, adc
->base
+ LRADC_CTRL4
+ STMP_OFFSET_REG_SET
);
520 writel(ctrl1_irq
, adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_SET
);
521 writel(enable
<< LRADC_DELAY_TRIGGER_LRADCS_OFFSET
,
522 adc
->base
+ LRADC_DELAY(0) + STMP_OFFSET_REG_SET
);
527 static int mxs_lradc_adc_buffer_postdisable(struct iio_dev
*iio
)
529 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
530 struct mxs_lradc
*lradc
= adc
->lradc
;
532 writel(LRADC_DELAY_TRIGGER_LRADCS_MASK
| LRADC_DELAY_KICK
,
533 adc
->base
+ LRADC_DELAY(0) + STMP_OFFSET_REG_CLR
);
535 writel(lradc
->buffer_vchans
,
536 adc
->base
+ LRADC_CTRL0
+ STMP_OFFSET_REG_CLR
);
537 if (lradc
->soc
== IMX28_LRADC
)
538 writel(lradc
->buffer_vchans
<< LRADC_CTRL1_LRADC_IRQ_EN_OFFSET
,
539 adc
->base
+ LRADC_CTRL1
+ STMP_OFFSET_REG_CLR
);
544 static bool mxs_lradc_adc_validate_scan_mask(struct iio_dev
*iio
,
545 const unsigned long *mask
)
547 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
548 struct mxs_lradc
*lradc
= adc
->lradc
;
549 const int map_chans
= bitmap_weight(mask
, LRADC_MAX_TOTAL_CHANS
);
551 unsigned long rsvd_mask
= 0;
553 if (lradc
->use_touchbutton
)
554 rsvd_mask
|= CHAN_MASK_TOUCHBUTTON
;
555 if (lradc
->touchscreen_wire
== MXS_LRADC_TOUCHSCREEN_4WIRE
)
556 rsvd_mask
|= CHAN_MASK_TOUCHSCREEN_4WIRE
;
557 if (lradc
->touchscreen_wire
== MXS_LRADC_TOUCHSCREEN_5WIRE
)
558 rsvd_mask
|= CHAN_MASK_TOUCHSCREEN_5WIRE
;
560 if (lradc
->use_touchbutton
)
562 if (lradc
->touchscreen_wire
)
565 /* Test for attempts to map channels with special mode of operation. */
566 if (bitmap_intersects(mask
, &rsvd_mask
, LRADC_MAX_TOTAL_CHANS
))
569 /* Test for attempts to map more channels then available slots. */
570 if (map_chans
+ rsvd_chans
> LRADC_MAX_MAPPED_CHANS
)
576 static const struct iio_buffer_setup_ops mxs_lradc_adc_buffer_ops
= {
577 .preenable
= &mxs_lradc_adc_buffer_preenable
,
578 .postenable
= &iio_triggered_buffer_postenable
,
579 .predisable
= &iio_triggered_buffer_predisable
,
580 .postdisable
= &mxs_lradc_adc_buffer_postdisable
,
581 .validate_scan_mask
= &mxs_lradc_adc_validate_scan_mask
,
584 /* Driver initialization */
585 #define MXS_ADC_CHAN(idx, chan_type, name) { \
586 .type = (chan_type), \
588 .scan_index = (idx), \
589 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
590 BIT(IIO_CHAN_INFO_SCALE), \
595 .realbits = LRADC_RESOLUTION, \
598 .datasheet_name = (name), \
601 static const struct iio_chan_spec mx23_lradc_chan_spec
[] = {
602 MXS_ADC_CHAN(0, IIO_VOLTAGE
, "LRADC0"),
603 MXS_ADC_CHAN(1, IIO_VOLTAGE
, "LRADC1"),
604 MXS_ADC_CHAN(2, IIO_VOLTAGE
, "LRADC2"),
605 MXS_ADC_CHAN(3, IIO_VOLTAGE
, "LRADC3"),
606 MXS_ADC_CHAN(4, IIO_VOLTAGE
, "LRADC4"),
607 MXS_ADC_CHAN(5, IIO_VOLTAGE
, "LRADC5"),
608 MXS_ADC_CHAN(6, IIO_VOLTAGE
, "VDDIO"),
609 MXS_ADC_CHAN(7, IIO_VOLTAGE
, "VBATT"),
610 /* Combined Temperature sensors */
615 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
616 BIT(IIO_CHAN_INFO_OFFSET
) |
617 BIT(IIO_CHAN_INFO_SCALE
),
619 .scan_type
= {.sign
= 'u', .realbits
= 18, .storagebits
= 32,},
620 .datasheet_name
= "TEMP_DIE",
622 /* Hidden channel to keep indexes */
629 MXS_ADC_CHAN(10, IIO_VOLTAGE
, NULL
),
630 MXS_ADC_CHAN(11, IIO_VOLTAGE
, NULL
),
631 MXS_ADC_CHAN(12, IIO_VOLTAGE
, "USB_DP"),
632 MXS_ADC_CHAN(13, IIO_VOLTAGE
, "USB_DN"),
633 MXS_ADC_CHAN(14, IIO_VOLTAGE
, "VBG"),
634 MXS_ADC_CHAN(15, IIO_VOLTAGE
, "VDD5V"),
637 static const struct iio_chan_spec mx28_lradc_chan_spec
[] = {
638 MXS_ADC_CHAN(0, IIO_VOLTAGE
, "LRADC0"),
639 MXS_ADC_CHAN(1, IIO_VOLTAGE
, "LRADC1"),
640 MXS_ADC_CHAN(2, IIO_VOLTAGE
, "LRADC2"),
641 MXS_ADC_CHAN(3, IIO_VOLTAGE
, "LRADC3"),
642 MXS_ADC_CHAN(4, IIO_VOLTAGE
, "LRADC4"),
643 MXS_ADC_CHAN(5, IIO_VOLTAGE
, "LRADC5"),
644 MXS_ADC_CHAN(6, IIO_VOLTAGE
, "LRADC6"),
645 MXS_ADC_CHAN(7, IIO_VOLTAGE
, "VBATT"),
646 /* Combined Temperature sensors */
651 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
652 BIT(IIO_CHAN_INFO_OFFSET
) |
653 BIT(IIO_CHAN_INFO_SCALE
),
655 .scan_type
= {.sign
= 'u', .realbits
= 18, .storagebits
= 32,},
656 .datasheet_name
= "TEMP_DIE",
658 /* Hidden channel to keep indexes */
665 MXS_ADC_CHAN(10, IIO_VOLTAGE
, "VDDIO"),
666 MXS_ADC_CHAN(11, IIO_VOLTAGE
, "VTH"),
667 MXS_ADC_CHAN(12, IIO_VOLTAGE
, "VDDA"),
668 MXS_ADC_CHAN(13, IIO_VOLTAGE
, "VDDD"),
669 MXS_ADC_CHAN(14, IIO_VOLTAGE
, "VBG"),
670 MXS_ADC_CHAN(15, IIO_VOLTAGE
, "VDD5V"),
673 static void mxs_lradc_adc_hw_init(struct mxs_lradc_adc
*adc
)
675 /* The ADC always uses DELAY CHANNEL 0. */
677 (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET
+ 0)) |
678 (LRADC_DELAY_TIMER_PER
<< LRADC_DELAY_DELAY_OFFSET
);
680 /* Configure DELAY CHANNEL 0 for generic ADC sampling. */
681 writel(adc_cfg
, adc
->base
+ LRADC_DELAY(0));
684 * Start internal temperature sensing by clearing bit
685 * HW_LRADC_CTRL2_TEMPSENSE_PWD. This bit can be left cleared
688 writel(0, adc
->base
+ LRADC_CTRL2
);
691 static void mxs_lradc_adc_hw_stop(struct mxs_lradc_adc
*adc
)
693 writel(0, adc
->base
+ LRADC_DELAY(0));
696 static int mxs_lradc_adc_probe(struct platform_device
*pdev
)
698 struct device
*dev
= &pdev
->dev
;
699 struct mxs_lradc
*lradc
= dev_get_drvdata(dev
->parent
);
700 struct mxs_lradc_adc
*adc
;
702 struct resource
*iores
;
703 int ret
, irq
, virq
, i
, s
, n
;
705 const char **irq_name
;
707 /* Allocate the IIO device. */
708 iio
= devm_iio_device_alloc(dev
, sizeof(*adc
));
710 dev_err(dev
, "Failed to allocate IIO device\n");
718 iores
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
722 adc
->base
= devm_ioremap(dev
, iores
->start
, resource_size(iores
));
726 init_completion(&adc
->completion
);
727 spin_lock_init(&adc
->lock
);
729 platform_set_drvdata(pdev
, iio
);
731 iio
->name
= pdev
->name
;
732 iio
->dev
.parent
= dev
;
733 iio
->dev
.of_node
= dev
->parent
->of_node
;
734 iio
->info
= &mxs_lradc_adc_iio_info
;
735 iio
->modes
= INDIO_DIRECT_MODE
;
736 iio
->masklength
= LRADC_MAX_TOTAL_CHANS
;
738 if (lradc
->soc
== IMX23_LRADC
) {
739 iio
->channels
= mx23_lradc_chan_spec
;
740 iio
->num_channels
= ARRAY_SIZE(mx23_lradc_chan_spec
);
741 irq_name
= mx23_lradc_adc_irq_names
;
742 n
= ARRAY_SIZE(mx23_lradc_adc_irq_names
);
744 iio
->channels
= mx28_lradc_chan_spec
;
745 iio
->num_channels
= ARRAY_SIZE(mx28_lradc_chan_spec
);
746 irq_name
= mx28_lradc_adc_irq_names
;
747 n
= ARRAY_SIZE(mx28_lradc_adc_irq_names
);
750 ret
= stmp_reset_block(adc
->base
);
754 for (i
= 0; i
< n
; i
++) {
755 irq
= platform_get_irq_byname(pdev
, irq_name
[i
]);
759 virq
= irq_of_parse_and_map(dev
->parent
->of_node
, irq
);
761 ret
= devm_request_irq(dev
, virq
, mxs_lradc_adc_handle_irq
,
762 0, irq_name
[i
], iio
);
767 ret
= mxs_lradc_adc_trigger_init(iio
);
771 ret
= iio_triggered_buffer_setup(iio
, &iio_pollfunc_store_time
,
772 &mxs_lradc_adc_trigger_handler
,
773 &mxs_lradc_adc_buffer_ops
);
777 adc
->vref_mv
= mxs_lradc_adc_vref_mv
[lradc
->soc
];
779 /* Populate available ADC input ranges */
780 for (i
= 0; i
< LRADC_MAX_TOTAL_CHANS
; i
++) {
781 for (s
= 0; s
< ARRAY_SIZE(adc
->scale_avail
[i
]); s
++) {
783 * [s=0] = optional divider by two disabled (default)
784 * [s=1] = optional divider by two enabled
786 * The scale is calculated by doing:
787 * Vref >> (realbits - s)
788 * which multiplies by two on the second component
791 scale_uv
= ((u64
)adc
->vref_mv
[i
] * 100000000) >>
792 (LRADC_RESOLUTION
- s
);
793 adc
->scale_avail
[i
][s
].nano
=
794 do_div(scale_uv
, 100000000) * 10;
795 adc
->scale_avail
[i
][s
].integer
= scale_uv
;
799 /* Configure the hardware. */
800 mxs_lradc_adc_hw_init(adc
);
802 /* Register IIO device. */
803 ret
= iio_device_register(iio
);
805 dev_err(dev
, "Failed to register IIO device\n");
812 mxs_lradc_adc_hw_stop(adc
);
813 mxs_lradc_adc_trigger_remove(iio
);
815 iio_triggered_buffer_cleanup(iio
);
819 static int mxs_lradc_adc_remove(struct platform_device
*pdev
)
821 struct iio_dev
*iio
= platform_get_drvdata(pdev
);
822 struct mxs_lradc_adc
*adc
= iio_priv(iio
);
824 iio_device_unregister(iio
);
825 mxs_lradc_adc_hw_stop(adc
);
826 mxs_lradc_adc_trigger_remove(iio
);
827 iio_triggered_buffer_cleanup(iio
);
832 static struct platform_driver mxs_lradc_adc_driver
= {
834 .name
= "mxs-lradc-adc",
836 .probe
= mxs_lradc_adc_probe
,
837 .remove
= mxs_lradc_adc_remove
,
839 module_platform_driver(mxs_lradc_adc_driver
);
841 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
842 MODULE_DESCRIPTION("Freescale MXS LRADC driver general purpose ADC driver");
843 MODULE_LICENSE("GPL");
844 MODULE_ALIAS("platform:mxs-lradc-adc");