blob: 1beae65aef2c3542b4345b66af273dabd6b41a30 [file] [log] [blame]
Maxime Ripard0e589d52012-05-11 15:35:33 +02001/*
2 * Driver for the ADC present in the Atmel AT91 evaluation boards.
3 *
4 * Copyright 2011 Free Electrons
5 *
6 * Licensed under the GPLv2 or later.
7 */
8
9#include <linux/bitmap.h>
10#include <linux/bitops.h>
11#include <linux/clk.h>
12#include <linux/err.h>
13#include <linux/io.h>
Josh Wuc8b11de2013-10-08 04:48:00 +010014#include <linux/input.h>
Maxime Ripard0e589d52012-05-11 15:35:33 +020015#include <linux/interrupt.h>
16#include <linux/jiffies.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
Maxime Riparde3641852012-05-11 15:35:37 +020019#include <linux/of.h>
20#include <linux/of_device.h>
Maxime Ripard0e589d52012-05-11 15:35:33 +020021#include <linux/platform_device.h>
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/wait.h>
25
26#include <linux/platform_data/at91_adc.h>
27
28#include <linux/iio/iio.h>
29#include <linux/iio/buffer.h>
Maxime Ripard0e589d52012-05-11 15:35:33 +020030#include <linux/iio/trigger.h>
31#include <linux/iio/trigger_consumer.h>
Lars-Peter Clausen90032e42012-06-18 18:33:49 +020032#include <linux/iio/triggered_buffer.h>
Maxime Ripard0e589d52012-05-11 15:35:33 +020033
34#include <mach/at91_adc.h>
35
36#define AT91_ADC_CHAN(st, ch) \
37 (st->registers->channel_base + (ch * 4))
38#define at91_adc_readl(st, reg) \
39 (readl_relaxed(st->reg_base + reg))
40#define at91_adc_writel(st, reg, val) \
41 (writel_relaxed(val, st->reg_base + reg))
42
Josh Wuc8b11de2013-10-08 04:48:00 +010043#define DRIVER_NAME "at91_adc"
44#define MAX_POS_BITS 12
45
46#define TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */
47#define TOUCH_PEN_DETECT_DEBOUNCE_US 200
48
Alexandre Belloni2de0c012014-04-15 12:27:58 +020049/**
50 * struct at91_adc_reg_desc - Various informations relative to registers
51 * @channel_base: Base offset for the channel data registers
52 * @drdy_mask: Mask of the DRDY field in the relevant registers
53 (Interruptions registers mostly)
54 * @status_register: Offset of the Interrupt Status Register
55 * @trigger_register: Offset of the Trigger setup register
56 * @mr_prescal_mask: Mask of the PRESCAL field in the adc MR register
57 * @mr_startup_mask: Mask of the STARTUP field in the adc MR register
58 */
59struct at91_adc_reg_desc {
60 u8 channel_base;
61 u32 drdy_mask;
62 u8 status_register;
63 u8 trigger_register;
64 u32 mr_prescal_mask;
65 u32 mr_startup_mask;
66};
67
Josh Wue1811f92013-08-27 12:28:00 +010068struct at91_adc_caps {
Josh Wuc8b11de2013-10-08 04:48:00 +010069 bool has_ts; /* Support touch screen */
70 bool has_tsmr; /* only at91sam9x5, sama5d3 have TSMR reg */
71 /*
72 * Numbers of sampling data will be averaged. Can be 0~3.
73 * Hardware can average (2 ^ ts_filter_average) sample data.
74 */
75 u8 ts_filter_average;
76 /* Pen Detection input pull-up resistor, can be 0~3 */
77 u8 ts_pen_detect_sensitivity;
78
Josh Wuc4601662013-10-08 04:48:00 +010079 /* startup time calculate function */
80 u32 (*calc_startup_ticks)(u8 startup_time, u32 adc_clk_khz);
81
Josh Wu2b6d5982013-10-08 04:48:00 +010082 u8 num_channels;
Josh Wue1811f92013-08-27 12:28:00 +010083 struct at91_adc_reg_desc registers;
84};
85
Josh Wuc8b11de2013-10-08 04:48:00 +010086enum atmel_adc_ts_type {
87 ATMEL_ADC_TOUCHSCREEN_NONE = 0,
88 ATMEL_ADC_TOUCHSCREEN_4WIRE = 4,
89 ATMEL_ADC_TOUCHSCREEN_5WIRE = 5,
90};
91
Maxime Ripard0e589d52012-05-11 15:35:33 +020092struct at91_adc_state {
93 struct clk *adc_clk;
94 u16 *buffer;
95 unsigned long channels_mask;
96 struct clk *clk;
97 bool done;
98 int irq;
Maxime Ripard0e589d52012-05-11 15:35:33 +020099 u16 last_value;
100 struct mutex lock;
101 u8 num_channels;
102 void __iomem *reg_base;
103 struct at91_adc_reg_desc *registers;
104 u8 startup_time;
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +0000105 u8 sample_hold_time;
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000106 bool sleep_mode;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200107 struct iio_trigger **trig;
108 struct at91_adc_trigger *trigger_list;
109 u32 trigger_number;
110 bool use_external;
111 u32 vref_mv;
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000112 u32 res; /* resolution used for convertions */
113 bool low_res; /* the resolution corresponds to the lowest one */
Maxime Ripard0e589d52012-05-11 15:35:33 +0200114 wait_queue_head_t wq_data_avail;
Josh Wue1811f92013-08-27 12:28:00 +0100115 struct at91_adc_caps *caps;
Josh Wuc8b11de2013-10-08 04:48:00 +0100116
117 /*
118 * Following ADC channels are shared by touchscreen:
119 *
120 * CH0 -- Touch screen XP/UL
121 * CH1 -- Touch screen XM/UR
122 * CH2 -- Touch screen YP/LL
123 * CH3 -- Touch screen YM/Sense
124 * CH4 -- Touch screen LR(5-wire only)
125 *
126 * The bitfields below represents the reserved channel in the
127 * touchscreen mode.
128 */
129#define CHAN_MASK_TOUCHSCREEN_4WIRE (0xf << 0)
130#define CHAN_MASK_TOUCHSCREEN_5WIRE (0x1f << 0)
131 enum atmel_adc_ts_type touchscreen_type;
132 struct input_dev *ts_input;
133
134 u16 ts_sample_period_val;
135 u32 ts_pressure_threshold;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200136};
137
138static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
139{
140 struct iio_poll_func *pf = p;
141 struct iio_dev *idev = pf->indio_dev;
142 struct at91_adc_state *st = iio_priv(idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200143 int i, j = 0;
144
145 for (i = 0; i < idev->masklength; i++) {
146 if (!test_bit(i, idev->active_scan_mask))
147 continue;
148 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
149 j++;
150 }
151
Lars-Peter Clausene79cece2013-09-19 13:59:00 +0100152 iio_push_to_buffers_with_timestamp(idev, st->buffer, pf->timestamp);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200153
154 iio_trigger_notify_done(idev->trig);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200155
156 /* Needed to ACK the DRDY interruption */
157 at91_adc_readl(st, AT91_ADC_LCDR);
158
159 enable_irq(st->irq);
160
161 return IRQ_HANDLED;
162}
163
Josh Wuc8b11de2013-10-08 04:48:00 +0100164/* Handler for classic adc channel eoc trigger */
165void handle_adc_eoc_trigger(int irq, struct iio_dev *idev)
Maxime Ripard0e589d52012-05-11 15:35:33 +0200166{
Maxime Ripard0e589d52012-05-11 15:35:33 +0200167 struct at91_adc_state *st = iio_priv(idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200168
169 if (iio_buffer_enabled(idev)) {
170 disable_irq_nosync(irq);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200171 iio_trigger_poll(idev->trig, iio_get_time_ns());
172 } else {
173 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
174 st->done = true;
175 wake_up_interruptible(&st->wq_data_avail);
176 }
Josh Wuc8b11de2013-10-08 04:48:00 +0100177}
178
179static int at91_ts_sample(struct at91_adc_state *st)
180{
181 unsigned int xscale, yscale, reg, z1, z2;
182 unsigned int x, y, pres, xpos, ypos;
183 unsigned int rxp = 1;
184 unsigned int factor = 1000;
185 struct iio_dev *idev = iio_priv_to_dev(st);
186
187 unsigned int xyz_mask_bits = st->res;
188 unsigned int xyz_mask = (1 << xyz_mask_bits) - 1;
189
190 /* calculate position */
191 /* x position = (x / xscale) * max, max = 2^MAX_POS_BITS - 1 */
192 reg = at91_adc_readl(st, AT91_ADC_TSXPOSR);
193 xpos = reg & xyz_mask;
194 x = (xpos << MAX_POS_BITS) - xpos;
195 xscale = (reg >> 16) & xyz_mask;
196 if (xscale == 0) {
197 dev_err(&idev->dev, "Error: xscale == 0!\n");
198 return -1;
199 }
200 x /= xscale;
201
202 /* y position = (y / yscale) * max, max = 2^MAX_POS_BITS - 1 */
203 reg = at91_adc_readl(st, AT91_ADC_TSYPOSR);
204 ypos = reg & xyz_mask;
205 y = (ypos << MAX_POS_BITS) - ypos;
206 yscale = (reg >> 16) & xyz_mask;
207 if (yscale == 0) {
208 dev_err(&idev->dev, "Error: yscale == 0!\n");
209 return -1;
210 }
211 y /= yscale;
212
213 /* calculate the pressure */
214 reg = at91_adc_readl(st, AT91_ADC_TSPRESSR);
215 z1 = reg & xyz_mask;
216 z2 = (reg >> 16) & xyz_mask;
217
218 if (z1 != 0)
219 pres = rxp * (x * factor / 1024) * (z2 * factor / z1 - factor)
220 / factor;
221 else
222 pres = st->ts_pressure_threshold; /* no pen contacted */
223
224 dev_dbg(&idev->dev, "xpos = %d, xscale = %d, ypos = %d, yscale = %d, z1 = %d, z2 = %d, press = %d\n",
225 xpos, xscale, ypos, yscale, z1, z2, pres);
226
227 if (pres < st->ts_pressure_threshold) {
228 dev_dbg(&idev->dev, "x = %d, y = %d, pressure = %d\n",
229 x, y, pres / factor);
230 input_report_abs(st->ts_input, ABS_X, x);
231 input_report_abs(st->ts_input, ABS_Y, y);
232 input_report_abs(st->ts_input, ABS_PRESSURE, pres);
233 input_report_key(st->ts_input, BTN_TOUCH, 1);
234 input_sync(st->ts_input);
235 } else {
236 dev_dbg(&idev->dev, "pressure too low: not reporting\n");
237 }
238
239 return 0;
240}
241
242static irqreturn_t at91_adc_interrupt(int irq, void *private)
243{
244 struct iio_dev *idev = private;
245 struct at91_adc_state *st = iio_priv(idev);
246 u32 status = at91_adc_readl(st, st->registers->status_register);
247 const uint32_t ts_data_irq_mask =
248 AT91_ADC_IER_XRDY |
249 AT91_ADC_IER_YRDY |
250 AT91_ADC_IER_PRDY;
251
252 if (status & st->registers->drdy_mask)
253 handle_adc_eoc_trigger(irq, idev);
254
255 if (status & AT91_ADC_IER_PEN) {
256 at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
257 at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_NOPEN |
258 ts_data_irq_mask);
259 /* Set up period trigger for sampling */
260 at91_adc_writel(st, st->registers->trigger_register,
261 AT91_ADC_TRGR_MOD_PERIOD_TRIG |
262 AT91_ADC_TRGR_TRGPER_(st->ts_sample_period_val));
263 } else if (status & AT91_ADC_IER_NOPEN) {
264 at91_adc_writel(st, st->registers->trigger_register, 0);
265 at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_NOPEN |
266 ts_data_irq_mask);
267 at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
268
269 input_report_key(st->ts_input, BTN_TOUCH, 0);
270 input_sync(st->ts_input);
271 } else if ((status & ts_data_irq_mask) == ts_data_irq_mask) {
272 /* Now all touchscreen data is ready */
273
274 if (status & AT91_ADC_ISR_PENS) {
275 /* validate data by pen contact */
276 at91_ts_sample(st);
277 } else {
278 /* triggered by event that is no pen contact, just read
279 * them to clean the interrupt and discard all.
280 */
281 at91_adc_readl(st, AT91_ADC_TSXPOSR);
282 at91_adc_readl(st, AT91_ADC_TSYPOSR);
283 at91_adc_readl(st, AT91_ADC_TSPRESSR);
284 }
285 }
Maxime Ripard0e589d52012-05-11 15:35:33 +0200286
287 return IRQ_HANDLED;
288}
289
290static int at91_adc_channel_init(struct iio_dev *idev)
291{
292 struct at91_adc_state *st = iio_priv(idev);
293 struct iio_chan_spec *chan_array, *timestamp;
294 int bit, idx = 0;
Josh Wuc8b11de2013-10-08 04:48:00 +0100295 unsigned long rsvd_mask = 0;
296
297 /* If touchscreen is enable, then reserve the adc channels */
298 if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
299 rsvd_mask = CHAN_MASK_TOUCHSCREEN_4WIRE;
300 else if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_5WIRE)
301 rsvd_mask = CHAN_MASK_TOUCHSCREEN_5WIRE;
302
303 /* set up the channel mask to reserve touchscreen channels */
304 st->channels_mask &= ~rsvd_mask;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200305
306 idev->num_channels = bitmap_weight(&st->channels_mask,
307 st->num_channels) + 1;
308
Axel Lin6b3aa312012-10-29 08:25:00 +0000309 chan_array = devm_kzalloc(&idev->dev,
310 ((idev->num_channels + 1) *
311 sizeof(struct iio_chan_spec)),
312 GFP_KERNEL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200313
314 if (!chan_array)
315 return -ENOMEM;
316
317 for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
318 struct iio_chan_spec *chan = chan_array + idx;
319
320 chan->type = IIO_VOLTAGE;
321 chan->indexed = 1;
322 chan->channel = bit;
323 chan->scan_index = idx;
324 chan->scan_type.sign = 'u';
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000325 chan->scan_type.realbits = st->res;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200326 chan->scan_type.storagebits = 16;
Jonathan Cameron01bdab62013-02-27 19:06:10 +0000327 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
328 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200329 idx++;
330 }
331 timestamp = chan_array + idx;
332
333 timestamp->type = IIO_TIMESTAMP;
334 timestamp->channel = -1;
335 timestamp->scan_index = idx;
336 timestamp->scan_type.sign = 's';
337 timestamp->scan_type.realbits = 64;
338 timestamp->scan_type.storagebits = 64;
339
340 idev->channels = chan_array;
341 return idev->num_channels;
342}
343
344static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
345 struct at91_adc_trigger *triggers,
346 const char *trigger_name)
347{
348 struct at91_adc_state *st = iio_priv(idev);
349 u8 value = 0;
350 int i;
351
352 for (i = 0; i < st->trigger_number; i++) {
353 char *name = kasprintf(GFP_KERNEL,
354 "%s-dev%d-%s",
355 idev->name,
356 idev->id,
357 triggers[i].name);
358 if (!name)
359 return -ENOMEM;
360
361 if (strcmp(trigger_name, name) == 0) {
362 value = triggers[i].value;
363 kfree(name);
364 break;
365 }
366
367 kfree(name);
368 }
369
370 return value;
371}
372
373static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
374{
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000375 struct iio_dev *idev = iio_trigger_get_drvdata(trig);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200376 struct at91_adc_state *st = iio_priv(idev);
377 struct iio_buffer *buffer = idev->buffer;
378 struct at91_adc_reg_desc *reg = st->registers;
379 u32 status = at91_adc_readl(st, reg->trigger_register);
380 u8 value;
381 u8 bit;
382
383 value = at91_adc_get_trigger_value_by_name(idev,
384 st->trigger_list,
385 idev->trig->name);
386 if (value == 0)
387 return -EINVAL;
388
389 if (state) {
390 st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
391 if (st->buffer == NULL)
392 return -ENOMEM;
393
394 at91_adc_writel(st, reg->trigger_register,
395 status | value);
396
397 for_each_set_bit(bit, buffer->scan_mask,
398 st->num_channels) {
399 struct iio_chan_spec const *chan = idev->channels + bit;
400 at91_adc_writel(st, AT91_ADC_CHER,
401 AT91_ADC_CH(chan->channel));
402 }
403
404 at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
405
406 } else {
407 at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
408
409 at91_adc_writel(st, reg->trigger_register,
410 status & ~value);
411
412 for_each_set_bit(bit, buffer->scan_mask,
413 st->num_channels) {
414 struct iio_chan_spec const *chan = idev->channels + bit;
415 at91_adc_writel(st, AT91_ADC_CHDR,
416 AT91_ADC_CH(chan->channel));
417 }
418 kfree(st->buffer);
419 }
420
421 return 0;
422}
423
424static const struct iio_trigger_ops at91_adc_trigger_ops = {
425 .owner = THIS_MODULE,
426 .set_trigger_state = &at91_adc_configure_trigger,
427};
428
429static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
430 struct at91_adc_trigger *trigger)
431{
432 struct iio_trigger *trig;
433 int ret;
434
435 trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
436 idev->id, trigger->name);
437 if (trig == NULL)
438 return NULL;
439
440 trig->dev.parent = idev->dev.parent;
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000441 iio_trigger_set_drvdata(trig, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200442 trig->ops = &at91_adc_trigger_ops;
443
444 ret = iio_trigger_register(trig);
445 if (ret)
446 return NULL;
447
448 return trig;
449}
450
451static int at91_adc_trigger_init(struct iio_dev *idev)
452{
453 struct at91_adc_state *st = iio_priv(idev);
454 int i, ret;
455
Axel Lin6b3aa312012-10-29 08:25:00 +0000456 st->trig = devm_kzalloc(&idev->dev,
Thomas Meyer42877232013-09-19 22:42:00 +0100457 st->trigger_number * sizeof(*st->trig),
Axel Lin6b3aa312012-10-29 08:25:00 +0000458 GFP_KERNEL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200459
460 if (st->trig == NULL) {
461 ret = -ENOMEM;
462 goto error_ret;
463 }
464
465 for (i = 0; i < st->trigger_number; i++) {
466 if (st->trigger_list[i].is_external && !(st->use_external))
467 continue;
468
469 st->trig[i] = at91_adc_allocate_trigger(idev,
470 st->trigger_list + i);
471 if (st->trig[i] == NULL) {
472 dev_err(&idev->dev,
473 "Could not allocate trigger %d\n", i);
474 ret = -ENOMEM;
475 goto error_trigger;
476 }
477 }
478
479 return 0;
480
481error_trigger:
482 for (i--; i >= 0; i--) {
483 iio_trigger_unregister(st->trig[i]);
484 iio_trigger_free(st->trig[i]);
485 }
486error_ret:
487 return ret;
488}
489
490static void at91_adc_trigger_remove(struct iio_dev *idev)
491{
492 struct at91_adc_state *st = iio_priv(idev);
493 int i;
494
495 for (i = 0; i < st->trigger_number; i++) {
496 iio_trigger_unregister(st->trig[i]);
497 iio_trigger_free(st->trig[i]);
498 }
499}
500
Maxime Ripard0e589d52012-05-11 15:35:33 +0200501static int at91_adc_buffer_init(struct iio_dev *idev)
502{
Lars-Peter Clausen90032e42012-06-18 18:33:49 +0200503 return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
504 &at91_adc_trigger_handler, NULL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200505}
506
507static void at91_adc_buffer_remove(struct iio_dev *idev)
508{
Lars-Peter Clausen90032e42012-06-18 18:33:49 +0200509 iio_triggered_buffer_cleanup(idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200510}
511
512static int at91_adc_read_raw(struct iio_dev *idev,
513 struct iio_chan_spec const *chan,
514 int *val, int *val2, long mask)
515{
516 struct at91_adc_state *st = iio_priv(idev);
517 int ret;
518
519 switch (mask) {
520 case IIO_CHAN_INFO_RAW:
521 mutex_lock(&st->lock);
522
523 at91_adc_writel(st, AT91_ADC_CHER,
524 AT91_ADC_CH(chan->channel));
525 at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask);
526 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
527
528 ret = wait_event_interruptible_timeout(st->wq_data_avail,
529 st->done,
530 msecs_to_jiffies(1000));
531 if (ret == 0)
Lars-Peter Clausen90e6dc72012-06-26 10:43:05 +0200532 ret = -ETIMEDOUT;
533 if (ret < 0) {
534 mutex_unlock(&st->lock);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200535 return ret;
Lars-Peter Clausen90e6dc72012-06-26 10:43:05 +0200536 }
Maxime Ripard0e589d52012-05-11 15:35:33 +0200537
538 *val = st->last_value;
539
540 at91_adc_writel(st, AT91_ADC_CHDR,
541 AT91_ADC_CH(chan->channel));
542 at91_adc_writel(st, AT91_ADC_IDR, st->registers->drdy_mask);
543
544 st->last_value = 0;
545 st->done = false;
546 mutex_unlock(&st->lock);
547 return IIO_VAL_INT;
548
549 case IIO_CHAN_INFO_SCALE:
Lars-Peter Clausenc45e5612013-09-28 10:31:00 +0100550 *val = st->vref_mv;
551 *val2 = chan->scan_type.realbits;
552 return IIO_VAL_FRACTIONAL_LOG2;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200553 default:
554 break;
555 }
556 return -EINVAL;
557}
558
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000559static int at91_adc_of_get_resolution(struct at91_adc_state *st,
560 struct platform_device *pdev)
561{
562 struct iio_dev *idev = iio_priv_to_dev(st);
563 struct device_node *np = pdev->dev.of_node;
564 int count, i, ret = 0;
565 char *res_name, *s;
566 u32 *resolutions;
567
568 count = of_property_count_strings(np, "atmel,adc-res-names");
569 if (count < 2) {
570 dev_err(&idev->dev, "You must specified at least two resolution names for "
571 "adc-res-names property in the DT\n");
572 return count;
573 }
574
575 resolutions = kmalloc(count * sizeof(*resolutions), GFP_KERNEL);
576 if (!resolutions)
577 return -ENOMEM;
578
579 if (of_property_read_u32_array(np, "atmel,adc-res", resolutions, count)) {
580 dev_err(&idev->dev, "Missing adc-res property in the DT.\n");
581 ret = -ENODEV;
582 goto ret;
583 }
584
585 if (of_property_read_string(np, "atmel,adc-use-res", (const char **)&res_name))
586 res_name = "highres";
587
588 for (i = 0; i < count; i++) {
589 if (of_property_read_string_index(np, "atmel,adc-res-names", i, (const char **)&s))
590 continue;
591
592 if (strcmp(res_name, s))
593 continue;
594
595 st->res = resolutions[i];
596 if (!strcmp(res_name, "lowres"))
597 st->low_res = true;
598 else
599 st->low_res = false;
600
601 dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
602 goto ret;
603 }
604
605 dev_err(&idev->dev, "There is no resolution for %s\n", res_name);
606
607ret:
608 kfree(resolutions);
609 return ret;
610}
611
Josh Wuc4601662013-10-08 04:48:00 +0100612static u32 calc_startup_ticks_9260(u8 startup_time, u32 adc_clk_khz)
613{
614 /*
615 * Number of ticks needed to cover the startup time of the ADC
616 * as defined in the electrical characteristics of the board,
617 * divided by 8. The formula thus is :
618 * Startup Time = (ticks + 1) * 8 / ADC Clock
619 */
620 return round_up((startup_time * adc_clk_khz / 1000) - 1, 8) / 8;
621}
622
623static u32 calc_startup_ticks_9x5(u8 startup_time, u32 adc_clk_khz)
624{
625 /*
626 * For sama5d3x and at91sam9x5, the formula changes to:
627 * Startup Time = <lookup_table_value> / ADC Clock
628 */
629 const int startup_lookup[] = {
630 0 , 8 , 16 , 24 ,
631 64 , 80 , 96 , 112,
632 512, 576, 640, 704,
633 768, 832, 896, 960
634 };
635 int i, size = ARRAY_SIZE(startup_lookup);
636 unsigned int ticks;
637
638 ticks = startup_time * adc_clk_khz / 1000;
639 for (i = 0; i < size; i++)
640 if (ticks < startup_lookup[i])
641 break;
642
643 ticks = i;
644 if (ticks == size)
645 /* Reach the end of lookup table */
646 ticks = size - 1;
647
648 return ticks;
649}
650
Josh Wue1811f92013-08-27 12:28:00 +0100651static const struct of_device_id at91_adc_dt_ids[];
652
Josh Wuc8b11de2013-10-08 04:48:00 +0100653static int at91_adc_probe_dt_ts(struct device_node *node,
654 struct at91_adc_state *st, struct device *dev)
655{
656 int ret;
657 u32 prop;
658
659 ret = of_property_read_u32(node, "atmel,adc-ts-wires", &prop);
660 if (ret) {
661 dev_info(dev, "ADC Touch screen is disabled.\n");
662 return 0;
663 }
664
665 switch (prop) {
666 case 4:
667 case 5:
668 st->touchscreen_type = prop;
669 break;
670 default:
671 dev_err(dev, "Unsupported number of touchscreen wires (%d). Should be 4 or 5.\n", prop);
672 return -EINVAL;
673 }
674
675 prop = 0;
676 of_property_read_u32(node, "atmel,adc-ts-pressure-threshold", &prop);
677 st->ts_pressure_threshold = prop;
678 if (st->ts_pressure_threshold) {
679 return 0;
680 } else {
681 dev_err(dev, "Invalid pressure threshold for the touchscreen\n");
682 return -EINVAL;
683 }
684}
685
Maxime Riparde3641852012-05-11 15:35:37 +0200686static int at91_adc_probe_dt(struct at91_adc_state *st,
687 struct platform_device *pdev)
688{
689 struct iio_dev *idev = iio_priv_to_dev(st);
690 struct device_node *node = pdev->dev.of_node;
691 struct device_node *trig_node;
692 int i = 0, ret;
693 u32 prop;
694
695 if (!node)
696 return -EINVAL;
697
Josh Wue1811f92013-08-27 12:28:00 +0100698 st->caps = (struct at91_adc_caps *)
699 of_match_device(at91_adc_dt_ids, &pdev->dev)->data;
700
Maxime Riparde3641852012-05-11 15:35:37 +0200701 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
702
703 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
704 dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
705 ret = -EINVAL;
706 goto error_ret;
707 }
708 st->channels_mask = prop;
709
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000710 st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
711
Maxime Riparde3641852012-05-11 15:35:37 +0200712 if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
713 dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
714 ret = -EINVAL;
715 goto error_ret;
716 }
717 st->startup_time = prop;
718
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +0000719 prop = 0;
720 of_property_read_u32(node, "atmel,adc-sample-hold-time", &prop);
721 st->sample_hold_time = prop;
Maxime Riparde3641852012-05-11 15:35:37 +0200722
723 if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
724 dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
725 ret = -EINVAL;
726 goto error_ret;
727 }
728 st->vref_mv = prop;
729
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000730 ret = at91_adc_of_get_resolution(st, pdev);
731 if (ret)
732 goto error_ret;
733
Josh Wue1811f92013-08-27 12:28:00 +0100734 st->registers = &st->caps->registers;
Josh Wu2b6d5982013-10-08 04:48:00 +0100735 st->num_channels = st->caps->num_channels;
Maxime Riparde3641852012-05-11 15:35:37 +0200736 st->trigger_number = of_get_child_count(node);
Axel Lin6b3aa312012-10-29 08:25:00 +0000737 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
738 sizeof(struct at91_adc_trigger),
739 GFP_KERNEL);
Maxime Riparde3641852012-05-11 15:35:37 +0200740 if (!st->trigger_list) {
741 dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
742 ret = -ENOMEM;
743 goto error_ret;
744 }
745
746 for_each_child_of_node(node, trig_node) {
747 struct at91_adc_trigger *trig = st->trigger_list + i;
748 const char *name;
749
750 if (of_property_read_string(trig_node, "trigger-name", &name)) {
751 dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
752 ret = -EINVAL;
753 goto error_ret;
754 }
755 trig->name = name;
756
757 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
758 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
759 ret = -EINVAL;
760 goto error_ret;
761 }
762 trig->value = prop;
763 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
764 i++;
765 }
766
Josh Wuc8b11de2013-10-08 04:48:00 +0100767 /* Check if touchscreen is supported. */
768 if (st->caps->has_ts)
769 return at91_adc_probe_dt_ts(node, st, &idev->dev);
770 else
771 dev_info(&idev->dev, "not support touchscreen in the adc compatible string.\n");
772
Maxime Riparde3641852012-05-11 15:35:37 +0200773 return 0;
774
775error_ret:
776 return ret;
777}
778
Maxime Ripard0e589d52012-05-11 15:35:33 +0200779static int at91_adc_probe_pdata(struct at91_adc_state *st,
780 struct platform_device *pdev)
781{
782 struct at91_adc_data *pdata = pdev->dev.platform_data;
783
784 if (!pdata)
785 return -EINVAL;
786
Alexandre Belloni467a44b2014-05-03 16:57:00 +0100787 st->caps = (struct at91_adc_caps *)
788 platform_get_device_id(pdev)->driver_data;
789
Maxime Ripard0e589d52012-05-11 15:35:33 +0200790 st->use_external = pdata->use_external_triggers;
791 st->vref_mv = pdata->vref;
792 st->channels_mask = pdata->channels_used;
Alexandre Belloni467a44b2014-05-03 16:57:00 +0100793 st->num_channels = st->caps->num_channels;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200794 st->startup_time = pdata->startup_time;
795 st->trigger_number = pdata->trigger_number;
796 st->trigger_list = pdata->trigger_list;
Alexandre Belloni467a44b2014-05-03 16:57:00 +0100797 st->registers = &st->caps->registers;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200798
799 return 0;
800}
801
802static const struct iio_info at91_adc_info = {
803 .driver_module = THIS_MODULE,
804 .read_raw = &at91_adc_read_raw,
805};
806
Josh Wuc8b11de2013-10-08 04:48:00 +0100807/* Touchscreen related functions */
808static int atmel_ts_open(struct input_dev *dev)
809{
810 struct at91_adc_state *st = input_get_drvdata(dev);
811
812 at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
813 return 0;
814}
815
816static void atmel_ts_close(struct input_dev *dev)
817{
818 struct at91_adc_state *st = input_get_drvdata(dev);
819
820 at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
821}
822
823static int at91_ts_hw_init(struct at91_adc_state *st, u32 adc_clk_khz)
824{
825 u32 reg = 0, pendbc;
826 int i = 0;
827
828 if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
829 reg = AT91_ADC_TSMR_TSMODE_4WIRE_PRESS;
830 else
831 reg = AT91_ADC_TSMR_TSMODE_5WIRE;
832
833 /* a Pen Detect Debounce Time is necessary for the ADC Touch to avoid
834 * pen detect noise.
835 * The formula is : Pen Detect Debounce Time = (2 ^ pendbc) / ADCClock
836 */
837 pendbc = round_up(TOUCH_PEN_DETECT_DEBOUNCE_US * adc_clk_khz / 1000, 1);
838
839 while (pendbc >> ++i)
840 ; /* Empty! Find the shift offset */
841 if (abs(pendbc - (1 << i)) < abs(pendbc - (1 << (i - 1))))
842 pendbc = i;
843 else
844 pendbc = i - 1;
845
846 if (st->caps->has_tsmr) {
847 reg |= AT91_ADC_TSMR_TSAV_(st->caps->ts_filter_average)
848 & AT91_ADC_TSMR_TSAV;
849 reg |= AT91_ADC_TSMR_PENDBC_(pendbc) & AT91_ADC_TSMR_PENDBC;
850 reg |= AT91_ADC_TSMR_NOTSDMA;
851 reg |= AT91_ADC_TSMR_PENDET_ENA;
852 reg |= 0x03 << 8; /* TSFREQ, need bigger than TSAV */
853
854 at91_adc_writel(st, AT91_ADC_TSMR, reg);
855 } else {
856 /* TODO: for 9g45 which has no TSMR */
857 }
858
859 /* Change adc internal resistor value for better pen detection,
860 * default value is 100 kOhm.
861 * 0 = 200 kOhm, 1 = 150 kOhm, 2 = 100 kOhm, 3 = 50 kOhm
862 * option only available on ES2 and higher
863 */
864 at91_adc_writel(st, AT91_ADC_ACR, st->caps->ts_pen_detect_sensitivity
865 & AT91_ADC_ACR_PENDETSENS);
866
867 /* Sample Peroid Time = (TRGPER + 1) / ADCClock */
868 st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US *
869 adc_clk_khz / 1000) - 1, 1);
870
871 return 0;
872}
873
874static int at91_ts_register(struct at91_adc_state *st,
875 struct platform_device *pdev)
876{
877 struct input_dev *input;
878 struct iio_dev *idev = iio_priv_to_dev(st);
879 int ret;
880
881 input = input_allocate_device();
882 if (!input) {
883 dev_err(&idev->dev, "Failed to allocate TS device!\n");
884 return -ENOMEM;
885 }
886
887 input->name = DRIVER_NAME;
888 input->id.bustype = BUS_HOST;
889 input->dev.parent = &pdev->dev;
890 input->open = atmel_ts_open;
891 input->close = atmel_ts_close;
892
893 __set_bit(EV_ABS, input->evbit);
894 __set_bit(EV_KEY, input->evbit);
895 __set_bit(BTN_TOUCH, input->keybit);
896 input_set_abs_params(input, ABS_X, 0, (1 << MAX_POS_BITS) - 1, 0, 0);
897 input_set_abs_params(input, ABS_Y, 0, (1 << MAX_POS_BITS) - 1, 0, 0);
898 input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);
899
900 st->ts_input = input;
901 input_set_drvdata(input, st);
902
903 ret = input_register_device(input);
904 if (ret)
905 input_free_device(st->ts_input);
906
907 return ret;
908}
909
910static void at91_ts_unregister(struct at91_adc_state *st)
911{
912 input_unregister_device(st->ts_input);
913}
914
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800915static int at91_adc_probe(struct platform_device *pdev)
Maxime Ripard0e589d52012-05-11 15:35:33 +0200916{
Josh Wudb10e202013-08-27 12:28:00 +0100917 unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200918 int ret;
919 struct iio_dev *idev;
920 struct at91_adc_state *st;
921 struct resource *res;
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000922 u32 reg;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200923
Sachin Kamatf8837532013-07-22 12:02:00 +0100924 idev = devm_iio_device_alloc(&pdev->dev, sizeof(struct at91_adc_state));
925 if (!idev)
926 return -ENOMEM;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200927
928 st = iio_priv(idev);
929
Maxime Riparde3641852012-05-11 15:35:37 +0200930 if (pdev->dev.of_node)
931 ret = at91_adc_probe_dt(st, pdev);
932 else
933 ret = at91_adc_probe_pdata(st, pdev);
934
Maxime Ripard0e589d52012-05-11 15:35:33 +0200935 if (ret) {
936 dev_err(&pdev->dev, "No platform data available.\n");
Sachin Kamatf8837532013-07-22 12:02:00 +0100937 return -EINVAL;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200938 }
939
Maxime Ripard0e589d52012-05-11 15:35:33 +0200940 platform_set_drvdata(pdev, idev);
941
942 idev->dev.parent = &pdev->dev;
943 idev->name = dev_name(&pdev->dev);
944 idev->modes = INDIO_DIRECT_MODE;
945 idev->info = &at91_adc_info;
946
947 st->irq = platform_get_irq(pdev, 0);
948 if (st->irq < 0) {
949 dev_err(&pdev->dev, "No IRQ ID is designated\n");
Sachin Kamatf8837532013-07-22 12:02:00 +0100950 return -ENODEV;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200951 }
952
Julia Lawall390d75c2012-07-31 14:09:00 +0100953 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200954
Thierry Reding5fd98462013-01-21 11:09:04 +0100955 st->reg_base = devm_ioremap_resource(&pdev->dev, res);
956 if (IS_ERR(st->reg_base)) {
Sachin Kamatf8837532013-07-22 12:02:00 +0100957 return PTR_ERR(st->reg_base);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200958 }
959
960 /*
961 * Disable all IRQs before setting up the handler
962 */
963 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
964 at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
965 ret = request_irq(st->irq,
Josh Wuc8b11de2013-10-08 04:48:00 +0100966 at91_adc_interrupt,
Maxime Ripard0e589d52012-05-11 15:35:33 +0200967 0,
968 pdev->dev.driver->name,
969 idev);
970 if (ret) {
971 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
Sachin Kamatf8837532013-07-22 12:02:00 +0100972 return ret;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200973 }
974
Julia Lawall390d75c2012-07-31 14:09:00 +0100975 st->clk = devm_clk_get(&pdev->dev, "adc_clk");
Maxime Ripard0e589d52012-05-11 15:35:33 +0200976 if (IS_ERR(st->clk)) {
977 dev_err(&pdev->dev, "Failed to get the clock.\n");
978 ret = PTR_ERR(st->clk);
979 goto error_free_irq;
980 }
981
Julia Lawall00062a9c2012-08-26 17:00:00 +0100982 ret = clk_prepare_enable(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200983 if (ret) {
Julia Lawall00062a9c2012-08-26 17:00:00 +0100984 dev_err(&pdev->dev,
985 "Could not prepare or enable the clock.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100986 goto error_free_irq;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200987 }
988
Julia Lawall390d75c2012-07-31 14:09:00 +0100989 st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
Maxime Ripard0e589d52012-05-11 15:35:33 +0200990 if (IS_ERR(st->adc_clk)) {
991 dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
Julia Lawallf755bbb2012-08-25 21:57:09 +0200992 ret = PTR_ERR(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200993 goto error_disable_clk;
994 }
995
Julia Lawall00062a9c2012-08-26 17:00:00 +0100996 ret = clk_prepare_enable(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200997 if (ret) {
Julia Lawall00062a9c2012-08-26 17:00:00 +0100998 dev_err(&pdev->dev,
999 "Could not prepare or enable the ADC clock.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +01001000 goto error_disable_clk;
Maxime Ripard0e589d52012-05-11 15:35:33 +02001001 }
1002
Maxime Ripard0e589d52012-05-11 15:35:33 +02001003 /*
1004 * Prescaler rate computation using the formula from the Atmel's
1005 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
1006 * specified by the electrical characteristics of the board.
1007 */
1008 mstrclk = clk_get_rate(st->clk);
1009 adc_clk = clk_get_rate(st->adc_clk);
Josh Wudb10e202013-08-27 12:28:00 +01001010 adc_clk_khz = adc_clk / 1000;
Josh Wuc8b11de2013-10-08 04:48:00 +01001011
1012 dev_dbg(&pdev->dev, "Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n",
1013 mstrclk, adc_clk);
1014
Maxime Ripard0e589d52012-05-11 15:35:33 +02001015 prsc = (mstrclk / (2 * adc_clk)) - 1;
1016
1017 if (!st->startup_time) {
1018 dev_err(&pdev->dev, "No startup time available.\n");
1019 ret = -EINVAL;
1020 goto error_disable_adc_clk;
1021 }
Josh Wuc4601662013-10-08 04:48:00 +01001022 ticks = (*st->caps->calc_startup_ticks)(st->startup_time, adc_clk_khz);
Maxime Ripard0e589d52012-05-11 15:35:33 +02001023
1024 /*
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +00001025 * a minimal Sample and Hold Time is necessary for the ADC to guarantee
1026 * the best converted final value between two channels selection
1027 * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
1028 */
Alexandre Belloni8f32b6b2014-03-03 18:07:00 +00001029 if (st->sample_hold_time > 0)
1030 shtim = round_up((st->sample_hold_time * adc_clk_khz / 1000)
1031 - 1, 1);
1032 else
1033 shtim = 0;
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +00001034
Josh Wu9120c0b2013-08-27 12:28:00 +01001035 reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask;
1036 reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask;
Ludovic Desroches47be16b2013-03-29 14:54:00 +00001037 if (st->low_res)
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +00001038 reg |= AT91_ADC_LOWRES;
1039 if (st->sleep_mode)
1040 reg |= AT91_ADC_SLEEP;
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +00001041 reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +00001042 at91_adc_writel(st, AT91_ADC_MR, reg);
Maxime Ripard0e589d52012-05-11 15:35:33 +02001043
1044 /* Setup the ADC channels available on the board */
1045 ret = at91_adc_channel_init(idev);
1046 if (ret < 0) {
1047 dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
1048 goto error_disable_adc_clk;
1049 }
1050
1051 init_waitqueue_head(&st->wq_data_avail);
1052 mutex_init(&st->lock);
1053
Josh Wuc8b11de2013-10-08 04:48:00 +01001054 /*
1055 * Since touch screen will set trigger register as period trigger. So
1056 * when touch screen is enabled, then we have to disable hardware
1057 * trigger for classic adc.
1058 */
1059 if (!st->touchscreen_type) {
1060 ret = at91_adc_buffer_init(idev);
1061 if (ret < 0) {
1062 dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
1063 goto error_disable_adc_clk;
1064 }
Maxime Ripard0e589d52012-05-11 15:35:33 +02001065
Josh Wuc8b11de2013-10-08 04:48:00 +01001066 ret = at91_adc_trigger_init(idev);
1067 if (ret < 0) {
1068 dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
1069 at91_adc_buffer_remove(idev);
1070 goto error_disable_adc_clk;
1071 }
1072 } else {
1073 if (!st->caps->has_tsmr) {
1074 dev_err(&pdev->dev, "We don't support non-TSMR adc\n");
Wei Yongjun00582bf2013-10-30 05:20:00 +00001075 ret = -ENODEV;
Josh Wuc8b11de2013-10-08 04:48:00 +01001076 goto error_disable_adc_clk;
1077 }
1078
1079 ret = at91_ts_register(st, pdev);
1080 if (ret)
1081 goto error_disable_adc_clk;
1082
1083 at91_ts_hw_init(st, adc_clk_khz);
Maxime Ripard0e589d52012-05-11 15:35:33 +02001084 }
1085
1086 ret = iio_device_register(idev);
1087 if (ret < 0) {
1088 dev_err(&pdev->dev, "Couldn't register the device.\n");
Josh Wuc8b11de2013-10-08 04:48:00 +01001089 goto error_iio_device_register;
Maxime Ripard0e589d52012-05-11 15:35:33 +02001090 }
1091
1092 return 0;
1093
Josh Wuc8b11de2013-10-08 04:48:00 +01001094error_iio_device_register:
1095 if (!st->touchscreen_type) {
1096 at91_adc_trigger_remove(idev);
1097 at91_adc_buffer_remove(idev);
1098 } else {
1099 at91_ts_unregister(st);
1100 }
Maxime Ripard0e589d52012-05-11 15:35:33 +02001101error_disable_adc_clk:
Julia Lawall00062a9c2012-08-26 17:00:00 +01001102 clk_disable_unprepare(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +02001103error_disable_clk:
Julia Lawall00062a9c2012-08-26 17:00:00 +01001104 clk_disable_unprepare(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +02001105error_free_irq:
1106 free_irq(st->irq, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +02001107 return ret;
1108}
1109
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001110static int at91_adc_remove(struct platform_device *pdev)
Maxime Ripard0e589d52012-05-11 15:35:33 +02001111{
1112 struct iio_dev *idev = platform_get_drvdata(pdev);
Maxime Ripard0e589d52012-05-11 15:35:33 +02001113 struct at91_adc_state *st = iio_priv(idev);
1114
1115 iio_device_unregister(idev);
Josh Wuc8b11de2013-10-08 04:48:00 +01001116 if (!st->touchscreen_type) {
1117 at91_adc_trigger_remove(idev);
1118 at91_adc_buffer_remove(idev);
1119 } else {
1120 at91_ts_unregister(st);
1121 }
Maxime Ripard0e589d52012-05-11 15:35:33 +02001122 clk_disable_unprepare(st->adc_clk);
Julia Lawall00062a9c2012-08-26 17:00:00 +01001123 clk_disable_unprepare(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +02001124 free_irq(st->irq, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +02001125
1126 return 0;
1127}
1128
Josh Wue1811f92013-08-27 12:28:00 +01001129static struct at91_adc_caps at91sam9260_caps = {
Josh Wuc4601662013-10-08 04:48:00 +01001130 .calc_startup_ticks = calc_startup_ticks_9260,
Josh Wu2b6d5982013-10-08 04:48:00 +01001131 .num_channels = 4,
Josh Wue1811f92013-08-27 12:28:00 +01001132 .registers = {
1133 .channel_base = AT91_ADC_CHR(0),
1134 .drdy_mask = AT91_ADC_DRDY,
1135 .status_register = AT91_ADC_SR,
1136 .trigger_register = AT91_ADC_TRGR_9260,
Josh Wu9120c0b2013-08-27 12:28:00 +01001137 .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
1138 .mr_startup_mask = AT91_ADC_STARTUP_9260,
Josh Wue1811f92013-08-27 12:28:00 +01001139 },
1140};
1141
1142static struct at91_adc_caps at91sam9g45_caps = {
Josh Wuc8b11de2013-10-08 04:48:00 +01001143 .has_ts = true,
Josh Wuc4601662013-10-08 04:48:00 +01001144 .calc_startup_ticks = calc_startup_ticks_9260, /* same as 9260 */
Josh Wu2b6d5982013-10-08 04:48:00 +01001145 .num_channels = 8,
Josh Wue1811f92013-08-27 12:28:00 +01001146 .registers = {
1147 .channel_base = AT91_ADC_CHR(0),
1148 .drdy_mask = AT91_ADC_DRDY,
1149 .status_register = AT91_ADC_SR,
1150 .trigger_register = AT91_ADC_TRGR_9G45,
Josh Wu9120c0b2013-08-27 12:28:00 +01001151 .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
1152 .mr_startup_mask = AT91_ADC_STARTUP_9G45,
Josh Wue1811f92013-08-27 12:28:00 +01001153 },
1154};
1155
1156static struct at91_adc_caps at91sam9x5_caps = {
Josh Wuc8b11de2013-10-08 04:48:00 +01001157 .has_ts = true,
1158 .has_tsmr = true,
1159 .ts_filter_average = 3,
1160 .ts_pen_detect_sensitivity = 2,
Josh Wuc4601662013-10-08 04:48:00 +01001161 .calc_startup_ticks = calc_startup_ticks_9x5,
Josh Wu2b6d5982013-10-08 04:48:00 +01001162 .num_channels = 12,
Josh Wue1811f92013-08-27 12:28:00 +01001163 .registers = {
1164 .channel_base = AT91_ADC_CDR0_9X5,
1165 .drdy_mask = AT91_ADC_SR_DRDY_9X5,
1166 .status_register = AT91_ADC_SR_9X5,
1167 .trigger_register = AT91_ADC_TRGR_9X5,
Josh Wu9120c0b2013-08-27 12:28:00 +01001168 /* prescal mask is same as 9G45 */
1169 .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
1170 .mr_startup_mask = AT91_ADC_STARTUP_9X5,
Josh Wue1811f92013-08-27 12:28:00 +01001171 },
1172};
1173
Maxime Riparde3641852012-05-11 15:35:37 +02001174static const struct of_device_id at91_adc_dt_ids[] = {
Josh Wue1811f92013-08-27 12:28:00 +01001175 { .compatible = "atmel,at91sam9260-adc", .data = &at91sam9260_caps },
1176 { .compatible = "atmel,at91sam9g45-adc", .data = &at91sam9g45_caps },
1177 { .compatible = "atmel,at91sam9x5-adc", .data = &at91sam9x5_caps },
Maxime Riparde3641852012-05-11 15:35:37 +02001178 {},
1179};
1180MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
Alexandre Belloni467a44b2014-05-03 16:57:00 +01001181
1182static const struct platform_device_id at91_adc_ids[] = {
1183 {
1184 .name = "at91sam9260-adc",
1185 .driver_data = (unsigned long)&at91sam9260_caps,
1186 }, {
1187 .name = "at91sam9g45-adc",
1188 .driver_data = (unsigned long)&at91sam9g45_caps,
1189 }, {
1190 .name = "at91sam9x5-adc",
1191 .driver_data = (unsigned long)&at91sam9x5_caps,
1192 }, {
1193 /* terminator */
1194 }
1195};
1196MODULE_DEVICE_TABLE(platform, at91_adc_ids);
Maxime Riparde3641852012-05-11 15:35:37 +02001197
Maxime Ripard0e589d52012-05-11 15:35:33 +02001198static struct platform_driver at91_adc_driver = {
1199 .probe = at91_adc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001200 .remove = at91_adc_remove,
Alexandre Belloni467a44b2014-05-03 16:57:00 +01001201 .id_table = at91_adc_ids,
Maxime Ripard0e589d52012-05-11 15:35:33 +02001202 .driver = {
Josh Wuc8b11de2013-10-08 04:48:00 +01001203 .name = DRIVER_NAME,
Maxime Riparde3641852012-05-11 15:35:37 +02001204 .of_match_table = of_match_ptr(at91_adc_dt_ids),
Maxime Ripard0e589d52012-05-11 15:35:33 +02001205 },
1206};
1207
1208module_platform_driver(at91_adc_driver);
1209
1210MODULE_LICENSE("GPL");
1211MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
1212MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");