2 * Driver for the ADC present in the Atmel AT91 evaluation boards.
4 * Copyright 2011 Free Electrons
6 * Licensed under the GPLv2 or later.
9 #include <linux/bitmap.h>
10 #include <linux/bitops.h>
11 #include <linux/clk.h>
12 #include <linux/err.h>
14 #include <linux/input.h>
15 #include <linux/interrupt.h>
16 #include <linux/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/wait.h>
26 #include <linux/platform_data/at91_adc.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger.h>
31 #include <linux/iio/trigger_consumer.h>
32 #include <linux/iio/triggered_buffer.h>
33 #include <linux/pinctrl/consumer.h>
36 #define AT91_ADC_CR 0x00 /* Control Register */
37 #define AT91_ADC_SWRST (1 << 0) /* Software Reset */
38 #define AT91_ADC_START (1 << 1) /* Start Conversion */
40 #define AT91_ADC_MR 0x04 /* Mode Register */
41 #define AT91_ADC_TSAMOD (3 << 0) /* ADC mode */
42 #define AT91_ADC_TSAMOD_ADC_ONLY_MODE (0 << 0) /* ADC Mode */
43 #define AT91_ADC_TSAMOD_TS_ONLY_MODE (1 << 0) /* Touch Screen Only Mode */
44 #define AT91_ADC_TRGEN (1 << 0) /* Trigger Enable */
45 #define AT91_ADC_TRGSEL (7 << 1) /* Trigger Selection */
46 #define AT91_ADC_TRGSEL_TC0 (0 << 1)
47 #define AT91_ADC_TRGSEL_TC1 (1 << 1)
48 #define AT91_ADC_TRGSEL_TC2 (2 << 1)
49 #define AT91_ADC_TRGSEL_EXTERNAL (6 << 1)
50 #define AT91_ADC_LOWRES (1 << 4) /* Low Resolution */
51 #define AT91_ADC_SLEEP (1 << 5) /* Sleep Mode */
52 #define AT91_ADC_PENDET (1 << 6) /* Pen contact detection enable */
53 #define AT91_ADC_PRESCAL_9260 (0x3f << 8) /* Prescalar Rate Selection */
54 #define AT91_ADC_PRESCAL_9G45 (0xff << 8)
55 #define AT91_ADC_PRESCAL_(x) ((x) << 8)
56 #define AT91_ADC_STARTUP_9260 (0x1f << 16) /* Startup Up Time */
57 #define AT91_ADC_STARTUP_9G45 (0x7f << 16)
58 #define AT91_ADC_STARTUP_9X5 (0xf << 16)
59 #define AT91_ADC_STARTUP_(x) ((x) << 16)
60 #define AT91_ADC_SHTIM (0xf << 24) /* Sample & Hold Time */
61 #define AT91_ADC_SHTIM_(x) ((x) << 24)
62 #define AT91_ADC_PENDBC (0x0f << 28) /* Pen Debounce time */
63 #define AT91_ADC_PENDBC_(x) ((x) << 28)
65 #define AT91_ADC_TSR 0x0C
66 #define AT91_ADC_TSR_SHTIM (0xf << 24) /* Sample & Hold Time */
67 #define AT91_ADC_TSR_SHTIM_(x) ((x) << 24)
69 #define AT91_ADC_CHER 0x10 /* Channel Enable Register */
70 #define AT91_ADC_CHDR 0x14 /* Channel Disable Register */
71 #define AT91_ADC_CHSR 0x18 /* Channel Status Register */
72 #define AT91_ADC_CH(n) (1 << (n)) /* Channel Number */
74 #define AT91_ADC_SR 0x1C /* Status Register */
75 #define AT91_ADC_EOC(n) (1 << (n)) /* End of Conversion on Channel N */
76 #define AT91_ADC_OVRE(n) (1 << ((n) + 8))/* Overrun Error on Channel N */
77 #define AT91_ADC_DRDY (1 << 16) /* Data Ready */
78 #define AT91_ADC_GOVRE (1 << 17) /* General Overrun Error */
79 #define AT91_ADC_ENDRX (1 << 18) /* End of RX Buffer */
80 #define AT91_ADC_RXFUFF (1 << 19) /* RX Buffer Full */
82 #define AT91_ADC_SR_9X5 0x30 /* Status Register for 9x5 */
83 #define AT91_ADC_SR_DRDY_9X5 (1 << 24) /* Data Ready */
85 #define AT91_ADC_LCDR 0x20 /* Last Converted Data Register */
86 #define AT91_ADC_LDATA (0x3ff)
88 #define AT91_ADC_IER 0x24 /* Interrupt Enable Register */
89 #define AT91_ADC_IDR 0x28 /* Interrupt Disable Register */
90 #define AT91_ADC_IMR 0x2C /* Interrupt Mask Register */
91 #define AT91RL_ADC_IER_PEN (1 << 20)
92 #define AT91RL_ADC_IER_NOPEN (1 << 21)
93 #define AT91_ADC_IER_PEN (1 << 29)
94 #define AT91_ADC_IER_NOPEN (1 << 30)
95 #define AT91_ADC_IER_XRDY (1 << 20)
96 #define AT91_ADC_IER_YRDY (1 << 21)
97 #define AT91_ADC_IER_PRDY (1 << 22)
98 #define AT91_ADC_ISR_PENS (1 << 31)
100 #define AT91_ADC_CHR(n) (0x30 + ((n) * 4)) /* Channel Data Register N */
101 #define AT91_ADC_DATA (0x3ff)
103 #define AT91_ADC_CDR0_9X5 (0x50) /* Channel Data Register 0 for 9X5 */
105 #define AT91_ADC_ACR 0x94 /* Analog Control Register */
106 #define AT91_ADC_ACR_PENDETSENS (0x3 << 0) /* pull-up resistor */
108 #define AT91_ADC_TSMR 0xB0
109 #define AT91_ADC_TSMR_TSMODE (3 << 0) /* Touch Screen Mode */
110 #define AT91_ADC_TSMR_TSMODE_NONE (0 << 0)
111 #define AT91_ADC_TSMR_TSMODE_4WIRE_NO_PRESS (1 << 0)
112 #define AT91_ADC_TSMR_TSMODE_4WIRE_PRESS (2 << 0)
113 #define AT91_ADC_TSMR_TSMODE_5WIRE (3 << 0)
114 #define AT91_ADC_TSMR_TSAV (3 << 4) /* Averages samples */
115 #define AT91_ADC_TSMR_TSAV_(x) ((x) << 4)
116 #define AT91_ADC_TSMR_SCTIM (0x0f << 16) /* Switch closure time */
117 #define AT91_ADC_TSMR_SCTIM_(x) ((x) << 16)
118 #define AT91_ADC_TSMR_PENDBC (0x0f << 28) /* Pen Debounce time */
119 #define AT91_ADC_TSMR_PENDBC_(x) ((x) << 28)
120 #define AT91_ADC_TSMR_NOTSDMA (1 << 22) /* No Touchscreen DMA */
121 #define AT91_ADC_TSMR_PENDET_DIS (0 << 24) /* Pen contact detection disable */
122 #define AT91_ADC_TSMR_PENDET_ENA (1 << 24) /* Pen contact detection enable */
124 #define AT91_ADC_TSXPOSR 0xB4
125 #define AT91_ADC_TSYPOSR 0xB8
126 #define AT91_ADC_TSPRESSR 0xBC
128 #define AT91_ADC_TRGR_9260 AT91_ADC_MR
129 #define AT91_ADC_TRGR_9G45 0x08
130 #define AT91_ADC_TRGR_9X5 0xC0
132 /* Trigger Register bit field */
133 #define AT91_ADC_TRGR_TRGPER (0xffff << 16)
134 #define AT91_ADC_TRGR_TRGPER_(x) ((x) << 16)
135 #define AT91_ADC_TRGR_TRGMOD (0x7 << 0)
136 #define AT91_ADC_TRGR_NONE (0 << 0)
137 #define AT91_ADC_TRGR_MOD_PERIOD_TRIG (5 << 0)
139 #define AT91_ADC_CHAN(st, ch) \
140 (st->registers->channel_base + (ch * 4))
141 #define at91_adc_readl(st, reg) \
142 (readl_relaxed(st->reg_base + reg))
143 #define at91_adc_writel(st, reg, val) \
144 (writel_relaxed(val, st->reg_base + reg))
146 #define DRIVER_NAME "at91_adc"
147 #define MAX_POS_BITS 12
149 #define TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */
150 #define TOUCH_PEN_DETECT_DEBOUNCE_US 200
152 #define MAX_RLPOS_BITS 10
153 #define TOUCH_SAMPLE_PERIOD_US_RL 10000 /* 10ms, the SoC can't keep up with 2ms */
154 #define TOUCH_SHTIM 0xa
155 #define TOUCH_SCTIM_US 10 /* 10us for the Touchscreen Switches Closure Time */
158 * struct at91_adc_reg_desc - Various informations relative to registers
159 * @channel_base: Base offset for the channel data registers
160 * @drdy_mask: Mask of the DRDY field in the relevant registers
161 (Interruptions registers mostly)
162 * @status_register: Offset of the Interrupt Status Register
163 * @trigger_register: Offset of the Trigger setup register
164 * @mr_prescal_mask: Mask of the PRESCAL field in the adc MR register
165 * @mr_startup_mask: Mask of the STARTUP field in the adc MR register
167 struct at91_adc_reg_desc
{
176 struct at91_adc_caps
{
177 bool has_ts
; /* Support touch screen */
178 bool has_tsmr
; /* only at91sam9x5, sama5d3 have TSMR reg */
180 * Numbers of sampling data will be averaged. Can be 0~3.
181 * Hardware can average (2 ^ ts_filter_average) sample data.
183 u8 ts_filter_average
;
184 /* Pen Detection input pull-up resistor, can be 0~3 */
185 u8 ts_pen_detect_sensitivity
;
187 /* startup time calculate function */
188 u32 (*calc_startup_ticks
)(u32 startup_time
, u32 adc_clk_khz
);
191 struct at91_adc_reg_desc registers
;
194 struct at91_adc_state
{
197 unsigned long channels_mask
;
205 void __iomem
*reg_base
;
206 struct at91_adc_reg_desc
*registers
;
210 struct iio_trigger
**trig
;
211 struct at91_adc_trigger
*trigger_list
;
215 u32 res
; /* resolution used for convertions */
216 bool low_res
; /* the resolution corresponds to the lowest one */
217 wait_queue_head_t wq_data_avail
;
218 struct at91_adc_caps
*caps
;
221 * Following ADC channels are shared by touchscreen:
223 * CH0 -- Touch screen XP/UL
224 * CH1 -- Touch screen XM/UR
225 * CH2 -- Touch screen YP/LL
226 * CH3 -- Touch screen YM/Sense
227 * CH4 -- Touch screen LR(5-wire only)
229 * The bitfields below represents the reserved channel in the
232 #define CHAN_MASK_TOUCHSCREEN_4WIRE (0xf << 0)
233 #define CHAN_MASK_TOUCHSCREEN_5WIRE (0x1f << 0)
234 enum atmel_adc_ts_type touchscreen_type
;
235 struct input_dev
*ts_input
;
237 u16 ts_sample_period_val
;
238 u32 ts_pressure_threshold
;
241 bool ts_bufferedmeasure
;
246 static irqreturn_t
at91_adc_trigger_handler(int irq
, void *p
)
248 struct iio_poll_func
*pf
= p
;
249 struct iio_dev
*idev
= pf
->indio_dev
;
250 struct at91_adc_state
*st
= iio_priv(idev
);
253 for (i
= 0; i
< idev
->masklength
; i
++) {
254 if (!test_bit(i
, idev
->active_scan_mask
))
256 st
->buffer
[j
] = at91_adc_readl(st
, AT91_ADC_CHAN(st
, i
));
260 iio_push_to_buffers_with_timestamp(idev
, st
->buffer
, pf
->timestamp
);
262 iio_trigger_notify_done(idev
->trig
);
264 /* Needed to ACK the DRDY interruption */
265 at91_adc_readl(st
, AT91_ADC_LCDR
);
272 /* Handler for classic adc channel eoc trigger */
273 static void handle_adc_eoc_trigger(int irq
, struct iio_dev
*idev
)
275 struct at91_adc_state
*st
= iio_priv(idev
);
277 if (iio_buffer_enabled(idev
)) {
278 disable_irq_nosync(irq
);
279 iio_trigger_poll(idev
->trig
);
281 st
->last_value
= at91_adc_readl(st
, AT91_ADC_CHAN(st
, st
->chnb
));
283 wake_up_interruptible(&st
->wq_data_avail
);
287 static int at91_ts_sample(struct at91_adc_state
*st
)
289 unsigned int xscale
, yscale
, reg
, z1
, z2
;
290 unsigned int x
, y
, pres
, xpos
, ypos
;
291 unsigned int rxp
= 1;
292 unsigned int factor
= 1000;
293 struct iio_dev
*idev
= iio_priv_to_dev(st
);
295 unsigned int xyz_mask_bits
= st
->res
;
296 unsigned int xyz_mask
= (1 << xyz_mask_bits
) - 1;
298 /* calculate position */
299 /* x position = (x / xscale) * max, max = 2^MAX_POS_BITS - 1 */
300 reg
= at91_adc_readl(st
, AT91_ADC_TSXPOSR
);
301 xpos
= reg
& xyz_mask
;
302 x
= (xpos
<< MAX_POS_BITS
) - xpos
;
303 xscale
= (reg
>> 16) & xyz_mask
;
305 dev_err(&idev
->dev
, "Error: xscale == 0!\n");
310 /* y position = (y / yscale) * max, max = 2^MAX_POS_BITS - 1 */
311 reg
= at91_adc_readl(st
, AT91_ADC_TSYPOSR
);
312 ypos
= reg
& xyz_mask
;
313 y
= (ypos
<< MAX_POS_BITS
) - ypos
;
314 yscale
= (reg
>> 16) & xyz_mask
;
316 dev_err(&idev
->dev
, "Error: yscale == 0!\n");
321 /* calculate the pressure */
322 reg
= at91_adc_readl(st
, AT91_ADC_TSPRESSR
);
324 z2
= (reg
>> 16) & xyz_mask
;
327 pres
= rxp
* (x
* factor
/ 1024) * (z2
* factor
/ z1
- factor
)
330 pres
= st
->ts_pressure_threshold
; /* no pen contacted */
332 dev_dbg(&idev
->dev
, "xpos = %d, xscale = %d, ypos = %d, yscale = %d, z1 = %d, z2 = %d, press = %d\n",
333 xpos
, xscale
, ypos
, yscale
, z1
, z2
, pres
);
335 if (pres
< st
->ts_pressure_threshold
) {
336 dev_dbg(&idev
->dev
, "x = %d, y = %d, pressure = %d\n",
337 x
, y
, pres
/ factor
);
338 input_report_abs(st
->ts_input
, ABS_X
, x
);
339 input_report_abs(st
->ts_input
, ABS_Y
, y
);
340 input_report_abs(st
->ts_input
, ABS_PRESSURE
, pres
);
341 input_report_key(st
->ts_input
, BTN_TOUCH
, 1);
342 input_sync(st
->ts_input
);
344 dev_dbg(&idev
->dev
, "pressure too low: not reporting\n");
350 static irqreturn_t
at91_adc_rl_interrupt(int irq
, void *private)
352 struct iio_dev
*idev
= private;
353 struct at91_adc_state
*st
= iio_priv(idev
);
354 u32 status
= at91_adc_readl(st
, st
->registers
->status_register
);
357 status
&= at91_adc_readl(st
, AT91_ADC_IMR
);
358 if (status
& GENMASK(st
->num_channels
- 1, 0))
359 handle_adc_eoc_trigger(irq
, idev
);
361 if (status
& AT91RL_ADC_IER_PEN
) {
362 /* Disabling pen debounce is required to get a NOPEN irq */
363 reg
= at91_adc_readl(st
, AT91_ADC_MR
);
364 reg
&= ~AT91_ADC_PENDBC
;
365 at91_adc_writel(st
, AT91_ADC_MR
, reg
);
367 at91_adc_writel(st
, AT91_ADC_IDR
, AT91RL_ADC_IER_PEN
);
368 at91_adc_writel(st
, AT91_ADC_IER
, AT91RL_ADC_IER_NOPEN
370 /* Set up period trigger for sampling */
371 at91_adc_writel(st
, st
->registers
->trigger_register
,
372 AT91_ADC_TRGR_MOD_PERIOD_TRIG
|
373 AT91_ADC_TRGR_TRGPER_(st
->ts_sample_period_val
));
374 } else if (status
& AT91RL_ADC_IER_NOPEN
) {
375 reg
= at91_adc_readl(st
, AT91_ADC_MR
);
376 reg
|= AT91_ADC_PENDBC_(st
->ts_pendbc
) & AT91_ADC_PENDBC
;
377 at91_adc_writel(st
, AT91_ADC_MR
, reg
);
378 at91_adc_writel(st
, st
->registers
->trigger_register
,
381 at91_adc_writel(st
, AT91_ADC_IDR
, AT91RL_ADC_IER_NOPEN
383 at91_adc_writel(st
, AT91_ADC_IER
, AT91RL_ADC_IER_PEN
);
384 st
->ts_bufferedmeasure
= false;
385 input_report_key(st
->ts_input
, BTN_TOUCH
, 0);
386 input_sync(st
->ts_input
);
387 } else if (status
& AT91_ADC_EOC(3) && st
->ts_input
) {
388 /* Conversion finished and we've a touchscreen */
389 if (st
->ts_bufferedmeasure
) {
391 * Last measurement is always discarded, since it can
393 * Always report previous measurement
395 input_report_abs(st
->ts_input
, ABS_X
, st
->ts_prev_absx
);
396 input_report_abs(st
->ts_input
, ABS_Y
, st
->ts_prev_absy
);
397 input_report_key(st
->ts_input
, BTN_TOUCH
, 1);
398 input_sync(st
->ts_input
);
400 st
->ts_bufferedmeasure
= true;
402 /* Now make new measurement */
403 st
->ts_prev_absx
= at91_adc_readl(st
, AT91_ADC_CHAN(st
, 3))
405 st
->ts_prev_absx
/= at91_adc_readl(st
, AT91_ADC_CHAN(st
, 2));
407 st
->ts_prev_absy
= at91_adc_readl(st
, AT91_ADC_CHAN(st
, 1))
409 st
->ts_prev_absy
/= at91_adc_readl(st
, AT91_ADC_CHAN(st
, 0));
415 static irqreturn_t
at91_adc_9x5_interrupt(int irq
, void *private)
417 struct iio_dev
*idev
= private;
418 struct at91_adc_state
*st
= iio_priv(idev
);
419 u32 status
= at91_adc_readl(st
, st
->registers
->status_register
);
420 const uint32_t ts_data_irq_mask
=
425 if (status
& GENMASK(st
->num_channels
- 1, 0))
426 handle_adc_eoc_trigger(irq
, idev
);
428 if (status
& AT91_ADC_IER_PEN
) {
429 at91_adc_writel(st
, AT91_ADC_IDR
, AT91_ADC_IER_PEN
);
430 at91_adc_writel(st
, AT91_ADC_IER
, AT91_ADC_IER_NOPEN
|
432 /* Set up period trigger for sampling */
433 at91_adc_writel(st
, st
->registers
->trigger_register
,
434 AT91_ADC_TRGR_MOD_PERIOD_TRIG
|
435 AT91_ADC_TRGR_TRGPER_(st
->ts_sample_period_val
));
436 } else if (status
& AT91_ADC_IER_NOPEN
) {
437 at91_adc_writel(st
, st
->registers
->trigger_register
, 0);
438 at91_adc_writel(st
, AT91_ADC_IDR
, AT91_ADC_IER_NOPEN
|
440 at91_adc_writel(st
, AT91_ADC_IER
, AT91_ADC_IER_PEN
);
442 input_report_key(st
->ts_input
, BTN_TOUCH
, 0);
443 input_sync(st
->ts_input
);
444 } else if ((status
& ts_data_irq_mask
) == ts_data_irq_mask
) {
445 /* Now all touchscreen data is ready */
447 if (status
& AT91_ADC_ISR_PENS
) {
448 /* validate data by pen contact */
451 /* triggered by event that is no pen contact, just read
452 * them to clean the interrupt and discard all.
454 at91_adc_readl(st
, AT91_ADC_TSXPOSR
);
455 at91_adc_readl(st
, AT91_ADC_TSYPOSR
);
456 at91_adc_readl(st
, AT91_ADC_TSPRESSR
);
463 static int at91_adc_channel_init(struct iio_dev
*idev
)
465 struct at91_adc_state
*st
= iio_priv(idev
);
466 struct iio_chan_spec
*chan_array
, *timestamp
;
468 unsigned long rsvd_mask
= 0;
470 /* If touchscreen is enable, then reserve the adc channels */
471 if (st
->touchscreen_type
== ATMEL_ADC_TOUCHSCREEN_4WIRE
)
472 rsvd_mask
= CHAN_MASK_TOUCHSCREEN_4WIRE
;
473 else if (st
->touchscreen_type
== ATMEL_ADC_TOUCHSCREEN_5WIRE
)
474 rsvd_mask
= CHAN_MASK_TOUCHSCREEN_5WIRE
;
476 /* set up the channel mask to reserve touchscreen channels */
477 st
->channels_mask
&= ~rsvd_mask
;
479 idev
->num_channels
= bitmap_weight(&st
->channels_mask
,
480 st
->num_channels
) + 1;
482 chan_array
= devm_kzalloc(&idev
->dev
,
483 ((idev
->num_channels
+ 1) *
484 sizeof(struct iio_chan_spec
)),
490 for_each_set_bit(bit
, &st
->channels_mask
, st
->num_channels
) {
491 struct iio_chan_spec
*chan
= chan_array
+ idx
;
493 chan
->type
= IIO_VOLTAGE
;
496 chan
->scan_index
= idx
;
497 chan
->scan_type
.sign
= 'u';
498 chan
->scan_type
.realbits
= st
->res
;
499 chan
->scan_type
.storagebits
= 16;
500 chan
->info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
);
501 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
504 timestamp
= chan_array
+ idx
;
506 timestamp
->type
= IIO_TIMESTAMP
;
507 timestamp
->channel
= -1;
508 timestamp
->scan_index
= idx
;
509 timestamp
->scan_type
.sign
= 's';
510 timestamp
->scan_type
.realbits
= 64;
511 timestamp
->scan_type
.storagebits
= 64;
513 idev
->channels
= chan_array
;
514 return idev
->num_channels
;
517 static int at91_adc_get_trigger_value_by_name(struct iio_dev
*idev
,
518 struct at91_adc_trigger
*triggers
,
519 const char *trigger_name
)
521 struct at91_adc_state
*st
= iio_priv(idev
);
524 for (i
= 0; i
< st
->trigger_number
; i
++) {
525 char *name
= kasprintf(GFP_KERNEL
,
533 if (strcmp(trigger_name
, name
) == 0) {
535 if (triggers
[i
].value
== 0)
537 return triggers
[i
].value
;
546 static int at91_adc_configure_trigger(struct iio_trigger
*trig
, bool state
)
548 struct iio_dev
*idev
= iio_trigger_get_drvdata(trig
);
549 struct at91_adc_state
*st
= iio_priv(idev
);
550 struct at91_adc_reg_desc
*reg
= st
->registers
;
551 u32 status
= at91_adc_readl(st
, reg
->trigger_register
);
555 value
= at91_adc_get_trigger_value_by_name(idev
,
562 st
->buffer
= kmalloc(idev
->scan_bytes
, GFP_KERNEL
);
563 if (st
->buffer
== NULL
)
566 at91_adc_writel(st
, reg
->trigger_register
,
569 for_each_set_bit(bit
, idev
->active_scan_mask
,
571 struct iio_chan_spec
const *chan
= idev
->channels
+ bit
;
572 at91_adc_writel(st
, AT91_ADC_CHER
,
573 AT91_ADC_CH(chan
->channel
));
576 at91_adc_writel(st
, AT91_ADC_IER
, reg
->drdy_mask
);
579 at91_adc_writel(st
, AT91_ADC_IDR
, reg
->drdy_mask
);
581 at91_adc_writel(st
, reg
->trigger_register
,
584 for_each_set_bit(bit
, idev
->active_scan_mask
,
586 struct iio_chan_spec
const *chan
= idev
->channels
+ bit
;
587 at91_adc_writel(st
, AT91_ADC_CHDR
,
588 AT91_ADC_CH(chan
->channel
));
596 static const struct iio_trigger_ops at91_adc_trigger_ops
= {
597 .set_trigger_state
= &at91_adc_configure_trigger
,
600 static struct iio_trigger
*at91_adc_allocate_trigger(struct iio_dev
*idev
,
601 struct at91_adc_trigger
*trigger
)
603 struct iio_trigger
*trig
;
606 trig
= iio_trigger_alloc("%s-dev%d-%s", idev
->name
,
607 idev
->id
, trigger
->name
);
611 trig
->dev
.parent
= idev
->dev
.parent
;
612 iio_trigger_set_drvdata(trig
, idev
);
613 trig
->ops
= &at91_adc_trigger_ops
;
615 ret
= iio_trigger_register(trig
);
622 static int at91_adc_trigger_init(struct iio_dev
*idev
)
624 struct at91_adc_state
*st
= iio_priv(idev
);
627 st
->trig
= devm_kzalloc(&idev
->dev
,
628 st
->trigger_number
* sizeof(*st
->trig
),
631 if (st
->trig
== NULL
) {
636 for (i
= 0; i
< st
->trigger_number
; i
++) {
637 if (st
->trigger_list
[i
].is_external
&& !(st
->use_external
))
640 st
->trig
[i
] = at91_adc_allocate_trigger(idev
,
641 st
->trigger_list
+ i
);
642 if (st
->trig
[i
] == NULL
) {
644 "Could not allocate trigger %d\n", i
);
653 for (i
--; i
>= 0; i
--) {
654 iio_trigger_unregister(st
->trig
[i
]);
655 iio_trigger_free(st
->trig
[i
]);
661 static void at91_adc_trigger_remove(struct iio_dev
*idev
)
663 struct at91_adc_state
*st
= iio_priv(idev
);
666 for (i
= 0; i
< st
->trigger_number
; i
++) {
667 iio_trigger_unregister(st
->trig
[i
]);
668 iio_trigger_free(st
->trig
[i
]);
672 static int at91_adc_buffer_init(struct iio_dev
*idev
)
674 return iio_triggered_buffer_setup(idev
, &iio_pollfunc_store_time
,
675 &at91_adc_trigger_handler
, NULL
);
678 static void at91_adc_buffer_remove(struct iio_dev
*idev
)
680 iio_triggered_buffer_cleanup(idev
);
683 static int at91_adc_read_raw(struct iio_dev
*idev
,
684 struct iio_chan_spec
const *chan
,
685 int *val
, int *val2
, long mask
)
687 struct at91_adc_state
*st
= iio_priv(idev
);
691 case IIO_CHAN_INFO_RAW
:
692 mutex_lock(&st
->lock
);
694 st
->chnb
= chan
->channel
;
695 at91_adc_writel(st
, AT91_ADC_CHER
,
696 AT91_ADC_CH(chan
->channel
));
697 at91_adc_writel(st
, AT91_ADC_IER
, BIT(chan
->channel
));
698 at91_adc_writel(st
, AT91_ADC_CR
, AT91_ADC_START
);
700 ret
= wait_event_interruptible_timeout(st
->wq_data_avail
,
702 msecs_to_jiffies(1000));
706 mutex_unlock(&st
->lock
);
710 *val
= st
->last_value
;
712 at91_adc_writel(st
, AT91_ADC_CHDR
,
713 AT91_ADC_CH(chan
->channel
));
714 at91_adc_writel(st
, AT91_ADC_IDR
, BIT(chan
->channel
));
718 mutex_unlock(&st
->lock
);
721 case IIO_CHAN_INFO_SCALE
:
723 *val2
= chan
->scan_type
.realbits
;
724 return IIO_VAL_FRACTIONAL_LOG2
;
731 static int at91_adc_of_get_resolution(struct at91_adc_state
*st
,
732 struct platform_device
*pdev
)
734 struct iio_dev
*idev
= iio_priv_to_dev(st
);
735 struct device_node
*np
= pdev
->dev
.of_node
;
736 int count
, i
, ret
= 0;
740 count
= of_property_count_strings(np
, "atmel,adc-res-names");
742 dev_err(&idev
->dev
, "You must specified at least two resolution names for "
743 "adc-res-names property in the DT\n");
747 resolutions
= kmalloc_array(count
, sizeof(*resolutions
), GFP_KERNEL
);
751 if (of_property_read_u32_array(np
, "atmel,adc-res", resolutions
, count
)) {
752 dev_err(&idev
->dev
, "Missing adc-res property in the DT.\n");
757 if (of_property_read_string(np
, "atmel,adc-use-res", (const char **)&res_name
))
758 res_name
= "highres";
760 for (i
= 0; i
< count
; i
++) {
761 if (of_property_read_string_index(np
, "atmel,adc-res-names", i
, (const char **)&s
))
764 if (strcmp(res_name
, s
))
767 st
->res
= resolutions
[i
];
768 if (!strcmp(res_name
, "lowres"))
773 dev_info(&idev
->dev
, "Resolution used: %u bits\n", st
->res
);
777 dev_err(&idev
->dev
, "There is no resolution for %s\n", res_name
);
784 static u32
calc_startup_ticks_9260(u32 startup_time
, u32 adc_clk_khz
)
787 * Number of ticks needed to cover the startup time of the ADC
788 * as defined in the electrical characteristics of the board,
789 * divided by 8. The formula thus is :
790 * Startup Time = (ticks + 1) * 8 / ADC Clock
792 return round_up((startup_time
* adc_clk_khz
/ 1000) - 1, 8) / 8;
795 static u32
calc_startup_ticks_9x5(u32 startup_time
, u32 adc_clk_khz
)
798 * For sama5d3x and at91sam9x5, the formula changes to:
799 * Startup Time = <lookup_table_value> / ADC Clock
801 static const int startup_lookup
[] = {
807 int i
, size
= ARRAY_SIZE(startup_lookup
);
810 ticks
= startup_time
* adc_clk_khz
/ 1000;
811 for (i
= 0; i
< size
; i
++)
812 if (ticks
< startup_lookup
[i
])
817 /* Reach the end of lookup table */
823 static const struct of_device_id at91_adc_dt_ids
[];
825 static int at91_adc_probe_dt_ts(struct device_node
*node
,
826 struct at91_adc_state
*st
, struct device
*dev
)
831 ret
= of_property_read_u32(node
, "atmel,adc-ts-wires", &prop
);
833 dev_info(dev
, "ADC Touch screen is disabled.\n");
840 st
->touchscreen_type
= prop
;
843 dev_err(dev
, "Unsupported number of touchscreen wires (%d). Should be 4 or 5.\n", prop
);
847 if (!st
->caps
->has_tsmr
)
850 of_property_read_u32(node
, "atmel,adc-ts-pressure-threshold", &prop
);
851 st
->ts_pressure_threshold
= prop
;
852 if (st
->ts_pressure_threshold
) {
855 dev_err(dev
, "Invalid pressure threshold for the touchscreen\n");
860 static int at91_adc_probe_dt(struct at91_adc_state
*st
,
861 struct platform_device
*pdev
)
863 struct iio_dev
*idev
= iio_priv_to_dev(st
);
864 struct device_node
*node
= pdev
->dev
.of_node
;
865 struct device_node
*trig_node
;
872 st
->caps
= (struct at91_adc_caps
*)
873 of_match_device(at91_adc_dt_ids
, &pdev
->dev
)->data
;
875 st
->use_external
= of_property_read_bool(node
, "atmel,adc-use-external-triggers");
877 if (of_property_read_u32(node
, "atmel,adc-channels-used", &prop
)) {
878 dev_err(&idev
->dev
, "Missing adc-channels-used property in the DT.\n");
882 st
->channels_mask
= prop
;
884 st
->sleep_mode
= of_property_read_bool(node
, "atmel,adc-sleep-mode");
886 if (of_property_read_u32(node
, "atmel,adc-startup-time", &prop
)) {
887 dev_err(&idev
->dev
, "Missing adc-startup-time property in the DT.\n");
891 st
->startup_time
= prop
;
894 of_property_read_u32(node
, "atmel,adc-sample-hold-time", &prop
);
895 st
->sample_hold_time
= prop
;
897 if (of_property_read_u32(node
, "atmel,adc-vref", &prop
)) {
898 dev_err(&idev
->dev
, "Missing adc-vref property in the DT.\n");
904 ret
= at91_adc_of_get_resolution(st
, pdev
);
908 st
->registers
= &st
->caps
->registers
;
909 st
->num_channels
= st
->caps
->num_channels
;
910 st
->trigger_number
= of_get_child_count(node
);
911 st
->trigger_list
= devm_kzalloc(&idev
->dev
, st
->trigger_number
*
912 sizeof(struct at91_adc_trigger
),
914 if (!st
->trigger_list
) {
915 dev_err(&idev
->dev
, "Could not allocate trigger list memory.\n");
920 for_each_child_of_node(node
, trig_node
) {
921 struct at91_adc_trigger
*trig
= st
->trigger_list
+ i
;
924 if (of_property_read_string(trig_node
, "trigger-name", &name
)) {
925 dev_err(&idev
->dev
, "Missing trigger-name property in the DT.\n");
931 if (of_property_read_u32(trig_node
, "trigger-value", &prop
)) {
932 dev_err(&idev
->dev
, "Missing trigger-value property in the DT.\n");
937 trig
->is_external
= of_property_read_bool(trig_node
, "trigger-external");
941 /* Check if touchscreen is supported. */
942 if (st
->caps
->has_ts
)
943 return at91_adc_probe_dt_ts(node
, st
, &idev
->dev
);
945 dev_info(&idev
->dev
, "not support touchscreen in the adc compatible string.\n");
953 static int at91_adc_probe_pdata(struct at91_adc_state
*st
,
954 struct platform_device
*pdev
)
956 struct at91_adc_data
*pdata
= pdev
->dev
.platform_data
;
961 st
->caps
= (struct at91_adc_caps
*)
962 platform_get_device_id(pdev
)->driver_data
;
964 st
->use_external
= pdata
->use_external_triggers
;
965 st
->vref_mv
= pdata
->vref
;
966 st
->channels_mask
= pdata
->channels_used
;
967 st
->num_channels
= st
->caps
->num_channels
;
968 st
->startup_time
= pdata
->startup_time
;
969 st
->trigger_number
= pdata
->trigger_number
;
970 st
->trigger_list
= pdata
->trigger_list
;
971 st
->registers
= &st
->caps
->registers
;
972 st
->touchscreen_type
= pdata
->touchscreen_type
;
977 static const struct iio_info at91_adc_info
= {
978 .read_raw
= &at91_adc_read_raw
,
981 /* Touchscreen related functions */
982 static int atmel_ts_open(struct input_dev
*dev
)
984 struct at91_adc_state
*st
= input_get_drvdata(dev
);
986 if (st
->caps
->has_tsmr
)
987 at91_adc_writel(st
, AT91_ADC_IER
, AT91_ADC_IER_PEN
);
989 at91_adc_writel(st
, AT91_ADC_IER
, AT91RL_ADC_IER_PEN
);
993 static void atmel_ts_close(struct input_dev
*dev
)
995 struct at91_adc_state
*st
= input_get_drvdata(dev
);
997 if (st
->caps
->has_tsmr
)
998 at91_adc_writel(st
, AT91_ADC_IDR
, AT91_ADC_IER_PEN
);
1000 at91_adc_writel(st
, AT91_ADC_IDR
, AT91RL_ADC_IER_PEN
);
1003 static int at91_ts_hw_init(struct at91_adc_state
*st
, u32 adc_clk_khz
)
1005 struct iio_dev
*idev
= iio_priv_to_dev(st
);
1010 /* a Pen Detect Debounce Time is necessary for the ADC Touch to avoid
1012 * The formula is : Pen Detect Debounce Time = (2 ^ pendbc) / ADCClock
1014 st
->ts_pendbc
= round_up(TOUCH_PEN_DETECT_DEBOUNCE_US
* adc_clk_khz
/
1017 while (st
->ts_pendbc
>> ++i
)
1018 ; /* Empty! Find the shift offset */
1019 if (abs(st
->ts_pendbc
- (1 << i
)) < abs(st
->ts_pendbc
- (1 << (i
- 1))))
1022 st
->ts_pendbc
= i
- 1;
1024 if (!st
->caps
->has_tsmr
) {
1025 reg
= at91_adc_readl(st
, AT91_ADC_MR
);
1026 reg
|= AT91_ADC_TSAMOD_TS_ONLY_MODE
| AT91_ADC_PENDET
;
1028 reg
|= AT91_ADC_PENDBC_(st
->ts_pendbc
) & AT91_ADC_PENDBC
;
1029 at91_adc_writel(st
, AT91_ADC_MR
, reg
);
1031 reg
= AT91_ADC_TSR_SHTIM_(TOUCH_SHTIM
) & AT91_ADC_TSR_SHTIM
;
1032 at91_adc_writel(st
, AT91_ADC_TSR
, reg
);
1034 st
->ts_sample_period_val
= round_up((TOUCH_SAMPLE_PERIOD_US_RL
*
1035 adc_clk_khz
/ 1000) - 1, 1);
1040 /* Touchscreen Switches Closure time needed for allowing the value to
1042 * Switch Closure Time = (TSSCTIM * 4) ADCClock periods
1044 tssctim
= DIV_ROUND_UP(TOUCH_SCTIM_US
* adc_clk_khz
/ 1000, 4);
1045 dev_dbg(&idev
->dev
, "adc_clk at: %d KHz, tssctim at: %d\n",
1046 adc_clk_khz
, tssctim
);
1048 if (st
->touchscreen_type
== ATMEL_ADC_TOUCHSCREEN_4WIRE
)
1049 reg
= AT91_ADC_TSMR_TSMODE_4WIRE_PRESS
;
1051 reg
= AT91_ADC_TSMR_TSMODE_5WIRE
;
1053 reg
|= AT91_ADC_TSMR_SCTIM_(tssctim
) & AT91_ADC_TSMR_SCTIM
;
1054 reg
|= AT91_ADC_TSMR_TSAV_(st
->caps
->ts_filter_average
)
1055 & AT91_ADC_TSMR_TSAV
;
1056 reg
|= AT91_ADC_TSMR_PENDBC_(st
->ts_pendbc
) & AT91_ADC_TSMR_PENDBC
;
1057 reg
|= AT91_ADC_TSMR_NOTSDMA
;
1058 reg
|= AT91_ADC_TSMR_PENDET_ENA
;
1059 reg
|= 0x03 << 8; /* TSFREQ, needs to be bigger than TSAV */
1061 at91_adc_writel(st
, AT91_ADC_TSMR
, reg
);
1063 /* Change adc internal resistor value for better pen detection,
1064 * default value is 100 kOhm.
1065 * 0 = 200 kOhm, 1 = 150 kOhm, 2 = 100 kOhm, 3 = 50 kOhm
1066 * option only available on ES2 and higher
1068 at91_adc_writel(st
, AT91_ADC_ACR
, st
->caps
->ts_pen_detect_sensitivity
1069 & AT91_ADC_ACR_PENDETSENS
);
1071 /* Sample Period Time = (TRGPER + 1) / ADCClock */
1072 st
->ts_sample_period_val
= round_up((TOUCH_SAMPLE_PERIOD_US
*
1073 adc_clk_khz
/ 1000) - 1, 1);
1078 static int at91_ts_register(struct at91_adc_state
*st
,
1079 struct platform_device
*pdev
)
1081 struct input_dev
*input
;
1082 struct iio_dev
*idev
= iio_priv_to_dev(st
);
1085 input
= input_allocate_device();
1087 dev_err(&idev
->dev
, "Failed to allocate TS device!\n");
1091 input
->name
= DRIVER_NAME
;
1092 input
->id
.bustype
= BUS_HOST
;
1093 input
->dev
.parent
= &pdev
->dev
;
1094 input
->open
= atmel_ts_open
;
1095 input
->close
= atmel_ts_close
;
1097 __set_bit(EV_ABS
, input
->evbit
);
1098 __set_bit(EV_KEY
, input
->evbit
);
1099 __set_bit(BTN_TOUCH
, input
->keybit
);
1100 if (st
->caps
->has_tsmr
) {
1101 input_set_abs_params(input
, ABS_X
, 0, (1 << MAX_POS_BITS
) - 1,
1103 input_set_abs_params(input
, ABS_Y
, 0, (1 << MAX_POS_BITS
) - 1,
1105 input_set_abs_params(input
, ABS_PRESSURE
, 0, 0xffffff, 0, 0);
1107 if (st
->touchscreen_type
!= ATMEL_ADC_TOUCHSCREEN_4WIRE
) {
1109 "This touchscreen controller only support 4 wires\n");
1114 input_set_abs_params(input
, ABS_X
, 0, (1 << MAX_RLPOS_BITS
) - 1,
1116 input_set_abs_params(input
, ABS_Y
, 0, (1 << MAX_RLPOS_BITS
) - 1,
1120 st
->ts_input
= input
;
1121 input_set_drvdata(input
, st
);
1123 ret
= input_register_device(input
);
1130 input_free_device(st
->ts_input
);
1134 static void at91_ts_unregister(struct at91_adc_state
*st
)
1136 input_unregister_device(st
->ts_input
);
1139 static int at91_adc_probe(struct platform_device
*pdev
)
1141 unsigned int prsc
, mstrclk
, ticks
, adc_clk
, adc_clk_khz
, shtim
;
1143 struct iio_dev
*idev
;
1144 struct at91_adc_state
*st
;
1145 struct resource
*res
;
1148 idev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(struct at91_adc_state
));
1152 st
= iio_priv(idev
);
1154 if (pdev
->dev
.of_node
)
1155 ret
= at91_adc_probe_dt(st
, pdev
);
1157 ret
= at91_adc_probe_pdata(st
, pdev
);
1160 dev_err(&pdev
->dev
, "No platform data available.\n");
1164 platform_set_drvdata(pdev
, idev
);
1166 idev
->dev
.parent
= &pdev
->dev
;
1167 idev
->name
= dev_name(&pdev
->dev
);
1168 idev
->modes
= INDIO_DIRECT_MODE
;
1169 idev
->info
= &at91_adc_info
;
1171 st
->irq
= platform_get_irq(pdev
, 0);
1173 dev_err(&pdev
->dev
, "No IRQ ID is designated\n");
1177 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1179 st
->reg_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1180 if (IS_ERR(st
->reg_base
))
1181 return PTR_ERR(st
->reg_base
);
1185 * Disable all IRQs before setting up the handler
1187 at91_adc_writel(st
, AT91_ADC_CR
, AT91_ADC_SWRST
);
1188 at91_adc_writel(st
, AT91_ADC_IDR
, 0xFFFFFFFF);
1190 if (st
->caps
->has_tsmr
)
1191 ret
= request_irq(st
->irq
, at91_adc_9x5_interrupt
, 0,
1192 pdev
->dev
.driver
->name
, idev
);
1194 ret
= request_irq(st
->irq
, at91_adc_rl_interrupt
, 0,
1195 pdev
->dev
.driver
->name
, idev
);
1197 dev_err(&pdev
->dev
, "Failed to allocate IRQ.\n");
1201 st
->clk
= devm_clk_get(&pdev
->dev
, "adc_clk");
1202 if (IS_ERR(st
->clk
)) {
1203 dev_err(&pdev
->dev
, "Failed to get the clock.\n");
1204 ret
= PTR_ERR(st
->clk
);
1205 goto error_free_irq
;
1208 ret
= clk_prepare_enable(st
->clk
);
1211 "Could not prepare or enable the clock.\n");
1212 goto error_free_irq
;
1215 st
->adc_clk
= devm_clk_get(&pdev
->dev
, "adc_op_clk");
1216 if (IS_ERR(st
->adc_clk
)) {
1217 dev_err(&pdev
->dev
, "Failed to get the ADC clock.\n");
1218 ret
= PTR_ERR(st
->adc_clk
);
1219 goto error_disable_clk
;
1222 ret
= clk_prepare_enable(st
->adc_clk
);
1225 "Could not prepare or enable the ADC clock.\n");
1226 goto error_disable_clk
;
1230 * Prescaler rate computation using the formula from the Atmel's
1231 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
1232 * specified by the electrical characteristics of the board.
1234 mstrclk
= clk_get_rate(st
->clk
);
1235 adc_clk
= clk_get_rate(st
->adc_clk
);
1236 adc_clk_khz
= adc_clk
/ 1000;
1238 dev_dbg(&pdev
->dev
, "Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n",
1241 prsc
= (mstrclk
/ (2 * adc_clk
)) - 1;
1243 if (!st
->startup_time
) {
1244 dev_err(&pdev
->dev
, "No startup time available.\n");
1246 goto error_disable_adc_clk
;
1248 ticks
= (*st
->caps
->calc_startup_ticks
)(st
->startup_time
, adc_clk_khz
);
1251 * a minimal Sample and Hold Time is necessary for the ADC to guarantee
1252 * the best converted final value between two channels selection
1253 * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
1255 if (st
->sample_hold_time
> 0)
1256 shtim
= round_up((st
->sample_hold_time
* adc_clk_khz
/ 1000)
1261 reg
= AT91_ADC_PRESCAL_(prsc
) & st
->registers
->mr_prescal_mask
;
1262 reg
|= AT91_ADC_STARTUP_(ticks
) & st
->registers
->mr_startup_mask
;
1264 reg
|= AT91_ADC_LOWRES
;
1266 reg
|= AT91_ADC_SLEEP
;
1267 reg
|= AT91_ADC_SHTIM_(shtim
) & AT91_ADC_SHTIM
;
1268 at91_adc_writel(st
, AT91_ADC_MR
, reg
);
1270 /* Setup the ADC channels available on the board */
1271 ret
= at91_adc_channel_init(idev
);
1273 dev_err(&pdev
->dev
, "Couldn't initialize the channels.\n");
1274 goto error_disable_adc_clk
;
1277 init_waitqueue_head(&st
->wq_data_avail
);
1278 mutex_init(&st
->lock
);
1281 * Since touch screen will set trigger register as period trigger. So
1282 * when touch screen is enabled, then we have to disable hardware
1283 * trigger for classic adc.
1285 if (!st
->touchscreen_type
) {
1286 ret
= at91_adc_buffer_init(idev
);
1288 dev_err(&pdev
->dev
, "Couldn't initialize the buffer.\n");
1289 goto error_disable_adc_clk
;
1292 ret
= at91_adc_trigger_init(idev
);
1294 dev_err(&pdev
->dev
, "Couldn't setup the triggers.\n");
1295 at91_adc_buffer_remove(idev
);
1296 goto error_disable_adc_clk
;
1299 ret
= at91_ts_register(st
, pdev
);
1301 goto error_disable_adc_clk
;
1303 at91_ts_hw_init(st
, adc_clk_khz
);
1306 ret
= iio_device_register(idev
);
1308 dev_err(&pdev
->dev
, "Couldn't register the device.\n");
1309 goto error_iio_device_register
;
1314 error_iio_device_register
:
1315 if (!st
->touchscreen_type
) {
1316 at91_adc_trigger_remove(idev
);
1317 at91_adc_buffer_remove(idev
);
1319 at91_ts_unregister(st
);
1321 error_disable_adc_clk
:
1322 clk_disable_unprepare(st
->adc_clk
);
1324 clk_disable_unprepare(st
->clk
);
1326 free_irq(st
->irq
, idev
);
1330 static int at91_adc_remove(struct platform_device
*pdev
)
1332 struct iio_dev
*idev
= platform_get_drvdata(pdev
);
1333 struct at91_adc_state
*st
= iio_priv(idev
);
1335 iio_device_unregister(idev
);
1336 if (!st
->touchscreen_type
) {
1337 at91_adc_trigger_remove(idev
);
1338 at91_adc_buffer_remove(idev
);
1340 at91_ts_unregister(st
);
1342 clk_disable_unprepare(st
->adc_clk
);
1343 clk_disable_unprepare(st
->clk
);
1344 free_irq(st
->irq
, idev
);
1349 #ifdef CONFIG_PM_SLEEP
1350 static int at91_adc_suspend(struct device
*dev
)
1352 struct iio_dev
*idev
= platform_get_drvdata(to_platform_device(dev
));
1353 struct at91_adc_state
*st
= iio_priv(idev
);
1355 pinctrl_pm_select_sleep_state(dev
);
1356 clk_disable_unprepare(st
->clk
);
1361 static int at91_adc_resume(struct device
*dev
)
1363 struct iio_dev
*idev
= platform_get_drvdata(to_platform_device(dev
));
1364 struct at91_adc_state
*st
= iio_priv(idev
);
1366 clk_prepare_enable(st
->clk
);
1367 pinctrl_pm_select_default_state(dev
);
1373 static SIMPLE_DEV_PM_OPS(at91_adc_pm_ops
, at91_adc_suspend
, at91_adc_resume
);
1375 static struct at91_adc_caps at91sam9260_caps
= {
1376 .calc_startup_ticks
= calc_startup_ticks_9260
,
1379 .channel_base
= AT91_ADC_CHR(0),
1380 .drdy_mask
= AT91_ADC_DRDY
,
1381 .status_register
= AT91_ADC_SR
,
1382 .trigger_register
= AT91_ADC_TRGR_9260
,
1383 .mr_prescal_mask
= AT91_ADC_PRESCAL_9260
,
1384 .mr_startup_mask
= AT91_ADC_STARTUP_9260
,
1388 static struct at91_adc_caps at91sam9rl_caps
= {
1390 .calc_startup_ticks
= calc_startup_ticks_9260
, /* same as 9260 */
1393 .channel_base
= AT91_ADC_CHR(0),
1394 .drdy_mask
= AT91_ADC_DRDY
,
1395 .status_register
= AT91_ADC_SR
,
1396 .trigger_register
= AT91_ADC_TRGR_9G45
,
1397 .mr_prescal_mask
= AT91_ADC_PRESCAL_9260
,
1398 .mr_startup_mask
= AT91_ADC_STARTUP_9G45
,
1402 static struct at91_adc_caps at91sam9g45_caps
= {
1404 .calc_startup_ticks
= calc_startup_ticks_9260
, /* same as 9260 */
1407 .channel_base
= AT91_ADC_CHR(0),
1408 .drdy_mask
= AT91_ADC_DRDY
,
1409 .status_register
= AT91_ADC_SR
,
1410 .trigger_register
= AT91_ADC_TRGR_9G45
,
1411 .mr_prescal_mask
= AT91_ADC_PRESCAL_9G45
,
1412 .mr_startup_mask
= AT91_ADC_STARTUP_9G45
,
1416 static struct at91_adc_caps at91sam9x5_caps
= {
1419 .ts_filter_average
= 3,
1420 .ts_pen_detect_sensitivity
= 2,
1421 .calc_startup_ticks
= calc_startup_ticks_9x5
,
1424 .channel_base
= AT91_ADC_CDR0_9X5
,
1425 .drdy_mask
= AT91_ADC_SR_DRDY_9X5
,
1426 .status_register
= AT91_ADC_SR_9X5
,
1427 .trigger_register
= AT91_ADC_TRGR_9X5
,
1428 /* prescal mask is same as 9G45 */
1429 .mr_prescal_mask
= AT91_ADC_PRESCAL_9G45
,
1430 .mr_startup_mask
= AT91_ADC_STARTUP_9X5
,
1434 static const struct of_device_id at91_adc_dt_ids
[] = {
1435 { .compatible
= "atmel,at91sam9260-adc", .data
= &at91sam9260_caps
},
1436 { .compatible
= "atmel,at91sam9rl-adc", .data
= &at91sam9rl_caps
},
1437 { .compatible
= "atmel,at91sam9g45-adc", .data
= &at91sam9g45_caps
},
1438 { .compatible
= "atmel,at91sam9x5-adc", .data
= &at91sam9x5_caps
},
1441 MODULE_DEVICE_TABLE(of
, at91_adc_dt_ids
);
1443 static const struct platform_device_id at91_adc_ids
[] = {
1445 .name
= "at91sam9260-adc",
1446 .driver_data
= (unsigned long)&at91sam9260_caps
,
1448 .name
= "at91sam9rl-adc",
1449 .driver_data
= (unsigned long)&at91sam9rl_caps
,
1451 .name
= "at91sam9g45-adc",
1452 .driver_data
= (unsigned long)&at91sam9g45_caps
,
1454 .name
= "at91sam9x5-adc",
1455 .driver_data
= (unsigned long)&at91sam9x5_caps
,
1460 MODULE_DEVICE_TABLE(platform
, at91_adc_ids
);
1462 static struct platform_driver at91_adc_driver
= {
1463 .probe
= at91_adc_probe
,
1464 .remove
= at91_adc_remove
,
1465 .id_table
= at91_adc_ids
,
1467 .name
= DRIVER_NAME
,
1468 .of_match_table
= of_match_ptr(at91_adc_dt_ids
),
1469 .pm
= &at91_adc_pm_ops
,
1473 module_platform_driver(at91_adc_driver
);
1475 MODULE_LICENSE("GPL");
1476 MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
1477 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");