blob: 2e2c9a80aa37c1eff8e124a1b09bb3e778107054 [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>
14#include <linux/interrupt.h>
15#include <linux/jiffies.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
Maxime Riparde3641852012-05-11 15:35:37 +020018#include <linux/of.h>
19#include <linux/of_device.h>
Maxime Ripard0e589d52012-05-11 15:35:33 +020020#include <linux/platform_device.h>
21#include <linux/sched.h>
22#include <linux/slab.h>
23#include <linux/wait.h>
24
25#include <linux/platform_data/at91_adc.h>
26
27#include <linux/iio/iio.h>
28#include <linux/iio/buffer.h>
Maxime Ripard0e589d52012-05-11 15:35:33 +020029#include <linux/iio/trigger.h>
30#include <linux/iio/trigger_consumer.h>
Lars-Peter Clausen90032e42012-06-18 18:33:49 +020031#include <linux/iio/triggered_buffer.h>
Maxime Ripard0e589d52012-05-11 15:35:33 +020032
33#include <mach/at91_adc.h>
34
35#define AT91_ADC_CHAN(st, ch) \
36 (st->registers->channel_base + (ch * 4))
37#define at91_adc_readl(st, reg) \
38 (readl_relaxed(st->reg_base + reg))
39#define at91_adc_writel(st, reg, val) \
40 (writel_relaxed(val, st->reg_base + reg))
41
42struct at91_adc_state {
43 struct clk *adc_clk;
44 u16 *buffer;
45 unsigned long channels_mask;
46 struct clk *clk;
47 bool done;
48 int irq;
Maxime Ripard0e589d52012-05-11 15:35:33 +020049 u16 last_value;
50 struct mutex lock;
51 u8 num_channels;
52 void __iomem *reg_base;
53 struct at91_adc_reg_desc *registers;
54 u8 startup_time;
55 struct iio_trigger **trig;
56 struct at91_adc_trigger *trigger_list;
57 u32 trigger_number;
58 bool use_external;
59 u32 vref_mv;
60 wait_queue_head_t wq_data_avail;
61};
62
63static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
64{
65 struct iio_poll_func *pf = p;
66 struct iio_dev *idev = pf->indio_dev;
67 struct at91_adc_state *st = iio_priv(idev);
68 struct iio_buffer *buffer = idev->buffer;
69 int i, j = 0;
70
71 for (i = 0; i < idev->masklength; i++) {
72 if (!test_bit(i, idev->active_scan_mask))
73 continue;
74 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
75 j++;
76 }
77
78 if (idev->scan_timestamp) {
79 s64 *timestamp = (s64 *)((u8 *)st->buffer +
80 ALIGN(j, sizeof(s64)));
81 *timestamp = pf->timestamp;
82 }
83
Peter Meerwald1217c482012-10-13 09:06:00 +010084 iio_push_to_buffer(buffer, st->buffer);
Maxime Ripard0e589d52012-05-11 15:35:33 +020085
86 iio_trigger_notify_done(idev->trig);
Maxime Ripard0e589d52012-05-11 15:35:33 +020087
88 /* Needed to ACK the DRDY interruption */
89 at91_adc_readl(st, AT91_ADC_LCDR);
90
91 enable_irq(st->irq);
92
93 return IRQ_HANDLED;
94}
95
96static irqreturn_t at91_adc_eoc_trigger(int irq, void *private)
97{
98 struct iio_dev *idev = private;
99 struct at91_adc_state *st = iio_priv(idev);
100 u32 status = at91_adc_readl(st, st->registers->status_register);
101
102 if (!(status & st->registers->drdy_mask))
103 return IRQ_HANDLED;
104
105 if (iio_buffer_enabled(idev)) {
106 disable_irq_nosync(irq);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200107 iio_trigger_poll(idev->trig, iio_get_time_ns());
108 } else {
109 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
110 st->done = true;
111 wake_up_interruptible(&st->wq_data_avail);
112 }
113
114 return IRQ_HANDLED;
115}
116
117static int at91_adc_channel_init(struct iio_dev *idev)
118{
119 struct at91_adc_state *st = iio_priv(idev);
120 struct iio_chan_spec *chan_array, *timestamp;
121 int bit, idx = 0;
122
123 idev->num_channels = bitmap_weight(&st->channels_mask,
124 st->num_channels) + 1;
125
Axel Lin6b3aa312012-10-29 08:25:00 +0000126 chan_array = devm_kzalloc(&idev->dev,
127 ((idev->num_channels + 1) *
128 sizeof(struct iio_chan_spec)),
129 GFP_KERNEL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200130
131 if (!chan_array)
132 return -ENOMEM;
133
134 for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
135 struct iio_chan_spec *chan = chan_array + idx;
136
137 chan->type = IIO_VOLTAGE;
138 chan->indexed = 1;
139 chan->channel = bit;
140 chan->scan_index = idx;
141 chan->scan_type.sign = 'u';
142 chan->scan_type.realbits = 10;
143 chan->scan_type.storagebits = 16;
144 chan->info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT |
145 IIO_CHAN_INFO_RAW_SEPARATE_BIT;
146 idx++;
147 }
148 timestamp = chan_array + idx;
149
150 timestamp->type = IIO_TIMESTAMP;
151 timestamp->channel = -1;
152 timestamp->scan_index = idx;
153 timestamp->scan_type.sign = 's';
154 timestamp->scan_type.realbits = 64;
155 timestamp->scan_type.storagebits = 64;
156
157 idev->channels = chan_array;
158 return idev->num_channels;
159}
160
161static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
162 struct at91_adc_trigger *triggers,
163 const char *trigger_name)
164{
165 struct at91_adc_state *st = iio_priv(idev);
166 u8 value = 0;
167 int i;
168
169 for (i = 0; i < st->trigger_number; i++) {
170 char *name = kasprintf(GFP_KERNEL,
171 "%s-dev%d-%s",
172 idev->name,
173 idev->id,
174 triggers[i].name);
175 if (!name)
176 return -ENOMEM;
177
178 if (strcmp(trigger_name, name) == 0) {
179 value = triggers[i].value;
180 kfree(name);
181 break;
182 }
183
184 kfree(name);
185 }
186
187 return value;
188}
189
190static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
191{
192 struct iio_dev *idev = trig->private_data;
193 struct at91_adc_state *st = iio_priv(idev);
194 struct iio_buffer *buffer = idev->buffer;
195 struct at91_adc_reg_desc *reg = st->registers;
196 u32 status = at91_adc_readl(st, reg->trigger_register);
197 u8 value;
198 u8 bit;
199
200 value = at91_adc_get_trigger_value_by_name(idev,
201 st->trigger_list,
202 idev->trig->name);
203 if (value == 0)
204 return -EINVAL;
205
206 if (state) {
207 st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
208 if (st->buffer == NULL)
209 return -ENOMEM;
210
211 at91_adc_writel(st, reg->trigger_register,
212 status | value);
213
214 for_each_set_bit(bit, buffer->scan_mask,
215 st->num_channels) {
216 struct iio_chan_spec const *chan = idev->channels + bit;
217 at91_adc_writel(st, AT91_ADC_CHER,
218 AT91_ADC_CH(chan->channel));
219 }
220
221 at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
222
223 } else {
224 at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
225
226 at91_adc_writel(st, reg->trigger_register,
227 status & ~value);
228
229 for_each_set_bit(bit, buffer->scan_mask,
230 st->num_channels) {
231 struct iio_chan_spec const *chan = idev->channels + bit;
232 at91_adc_writel(st, AT91_ADC_CHDR,
233 AT91_ADC_CH(chan->channel));
234 }
235 kfree(st->buffer);
236 }
237
238 return 0;
239}
240
241static const struct iio_trigger_ops at91_adc_trigger_ops = {
242 .owner = THIS_MODULE,
243 .set_trigger_state = &at91_adc_configure_trigger,
244};
245
246static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
247 struct at91_adc_trigger *trigger)
248{
249 struct iio_trigger *trig;
250 int ret;
251
252 trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
253 idev->id, trigger->name);
254 if (trig == NULL)
255 return NULL;
256
257 trig->dev.parent = idev->dev.parent;
258 trig->private_data = idev;
259 trig->ops = &at91_adc_trigger_ops;
260
261 ret = iio_trigger_register(trig);
262 if (ret)
263 return NULL;
264
265 return trig;
266}
267
268static int at91_adc_trigger_init(struct iio_dev *idev)
269{
270 struct at91_adc_state *st = iio_priv(idev);
271 int i, ret;
272
Axel Lin6b3aa312012-10-29 08:25:00 +0000273 st->trig = devm_kzalloc(&idev->dev,
274 st->trigger_number * sizeof(st->trig),
275 GFP_KERNEL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200276
277 if (st->trig == NULL) {
278 ret = -ENOMEM;
279 goto error_ret;
280 }
281
282 for (i = 0; i < st->trigger_number; i++) {
283 if (st->trigger_list[i].is_external && !(st->use_external))
284 continue;
285
286 st->trig[i] = at91_adc_allocate_trigger(idev,
287 st->trigger_list + i);
288 if (st->trig[i] == NULL) {
289 dev_err(&idev->dev,
290 "Could not allocate trigger %d\n", i);
291 ret = -ENOMEM;
292 goto error_trigger;
293 }
294 }
295
296 return 0;
297
298error_trigger:
299 for (i--; i >= 0; i--) {
300 iio_trigger_unregister(st->trig[i]);
301 iio_trigger_free(st->trig[i]);
302 }
303error_ret:
304 return ret;
305}
306
307static void at91_adc_trigger_remove(struct iio_dev *idev)
308{
309 struct at91_adc_state *st = iio_priv(idev);
310 int i;
311
312 for (i = 0; i < st->trigger_number; i++) {
313 iio_trigger_unregister(st->trig[i]);
314 iio_trigger_free(st->trig[i]);
315 }
316}
317
Maxime Ripard0e589d52012-05-11 15:35:33 +0200318static int at91_adc_buffer_init(struct iio_dev *idev)
319{
Lars-Peter Clausen90032e42012-06-18 18:33:49 +0200320 return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
321 &at91_adc_trigger_handler, NULL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200322}
323
324static void at91_adc_buffer_remove(struct iio_dev *idev)
325{
Lars-Peter Clausen90032e42012-06-18 18:33:49 +0200326 iio_triggered_buffer_cleanup(idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200327}
328
329static int at91_adc_read_raw(struct iio_dev *idev,
330 struct iio_chan_spec const *chan,
331 int *val, int *val2, long mask)
332{
333 struct at91_adc_state *st = iio_priv(idev);
334 int ret;
335
336 switch (mask) {
337 case IIO_CHAN_INFO_RAW:
338 mutex_lock(&st->lock);
339
340 at91_adc_writel(st, AT91_ADC_CHER,
341 AT91_ADC_CH(chan->channel));
342 at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask);
343 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
344
345 ret = wait_event_interruptible_timeout(st->wq_data_avail,
346 st->done,
347 msecs_to_jiffies(1000));
348 if (ret == 0)
Lars-Peter Clausen90e6dc72012-06-26 10:43:05 +0200349 ret = -ETIMEDOUT;
350 if (ret < 0) {
351 mutex_unlock(&st->lock);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200352 return ret;
Lars-Peter Clausen90e6dc72012-06-26 10:43:05 +0200353 }
Maxime Ripard0e589d52012-05-11 15:35:33 +0200354
355 *val = st->last_value;
356
357 at91_adc_writel(st, AT91_ADC_CHDR,
358 AT91_ADC_CH(chan->channel));
359 at91_adc_writel(st, AT91_ADC_IDR, st->registers->drdy_mask);
360
361 st->last_value = 0;
362 st->done = false;
363 mutex_unlock(&st->lock);
364 return IIO_VAL_INT;
365
366 case IIO_CHAN_INFO_SCALE:
367 *val = (st->vref_mv * 1000) >> chan->scan_type.realbits;
368 *val2 = 0;
369 return IIO_VAL_INT_PLUS_MICRO;
370 default:
371 break;
372 }
373 return -EINVAL;
374}
375
Maxime Riparde3641852012-05-11 15:35:37 +0200376static int at91_adc_probe_dt(struct at91_adc_state *st,
377 struct platform_device *pdev)
378{
379 struct iio_dev *idev = iio_priv_to_dev(st);
380 struct device_node *node = pdev->dev.of_node;
381 struct device_node *trig_node;
382 int i = 0, ret;
383 u32 prop;
384
385 if (!node)
386 return -EINVAL;
387
388 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
389
390 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
391 dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
392 ret = -EINVAL;
393 goto error_ret;
394 }
395 st->channels_mask = prop;
396
397 if (of_property_read_u32(node, "atmel,adc-num-channels", &prop)) {
398 dev_err(&idev->dev, "Missing adc-num-channels property in the DT.\n");
399 ret = -EINVAL;
400 goto error_ret;
401 }
402 st->num_channels = prop;
403
404 if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
405 dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
406 ret = -EINVAL;
407 goto error_ret;
408 }
409 st->startup_time = prop;
410
411
412 if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
413 dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
414 ret = -EINVAL;
415 goto error_ret;
416 }
417 st->vref_mv = prop;
418
419 st->registers = devm_kzalloc(&idev->dev,
420 sizeof(struct at91_adc_reg_desc),
421 GFP_KERNEL);
422 if (!st->registers) {
423 dev_err(&idev->dev, "Could not allocate register memory.\n");
424 ret = -ENOMEM;
425 goto error_ret;
426 }
427
428 if (of_property_read_u32(node, "atmel,adc-channel-base", &prop)) {
429 dev_err(&idev->dev, "Missing adc-channel-base property in the DT.\n");
430 ret = -EINVAL;
431 goto error_ret;
432 }
433 st->registers->channel_base = prop;
434
435 if (of_property_read_u32(node, "atmel,adc-drdy-mask", &prop)) {
436 dev_err(&idev->dev, "Missing adc-drdy-mask property in the DT.\n");
437 ret = -EINVAL;
438 goto error_ret;
439 }
440 st->registers->drdy_mask = prop;
441
442 if (of_property_read_u32(node, "atmel,adc-status-register", &prop)) {
443 dev_err(&idev->dev, "Missing adc-status-register property in the DT.\n");
444 ret = -EINVAL;
445 goto error_ret;
446 }
447 st->registers->status_register = prop;
448
449 if (of_property_read_u32(node, "atmel,adc-trigger-register", &prop)) {
450 dev_err(&idev->dev, "Missing adc-trigger-register property in the DT.\n");
451 ret = -EINVAL;
452 goto error_ret;
453 }
454 st->registers->trigger_register = prop;
455
456 st->trigger_number = of_get_child_count(node);
Axel Lin6b3aa312012-10-29 08:25:00 +0000457 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
458 sizeof(struct at91_adc_trigger),
459 GFP_KERNEL);
Maxime Riparde3641852012-05-11 15:35:37 +0200460 if (!st->trigger_list) {
461 dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
462 ret = -ENOMEM;
463 goto error_ret;
464 }
465
466 for_each_child_of_node(node, trig_node) {
467 struct at91_adc_trigger *trig = st->trigger_list + i;
468 const char *name;
469
470 if (of_property_read_string(trig_node, "trigger-name", &name)) {
471 dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
472 ret = -EINVAL;
473 goto error_ret;
474 }
475 trig->name = name;
476
477 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
478 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
479 ret = -EINVAL;
480 goto error_ret;
481 }
482 trig->value = prop;
483 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
484 i++;
485 }
486
487 return 0;
488
489error_ret:
490 return ret;
491}
492
Maxime Ripard0e589d52012-05-11 15:35:33 +0200493static int at91_adc_probe_pdata(struct at91_adc_state *st,
494 struct platform_device *pdev)
495{
496 struct at91_adc_data *pdata = pdev->dev.platform_data;
497
498 if (!pdata)
499 return -EINVAL;
500
501 st->use_external = pdata->use_external_triggers;
502 st->vref_mv = pdata->vref;
503 st->channels_mask = pdata->channels_used;
504 st->num_channels = pdata->num_channels;
505 st->startup_time = pdata->startup_time;
506 st->trigger_number = pdata->trigger_number;
507 st->trigger_list = pdata->trigger_list;
508 st->registers = pdata->registers;
509
510 return 0;
511}
512
513static const struct iio_info at91_adc_info = {
514 .driver_module = THIS_MODULE,
515 .read_raw = &at91_adc_read_raw,
516};
517
518static int __devinit at91_adc_probe(struct platform_device *pdev)
519{
520 unsigned int prsc, mstrclk, ticks, adc_clk;
521 int ret;
522 struct iio_dev *idev;
523 struct at91_adc_state *st;
524 struct resource *res;
525
526 idev = iio_device_alloc(sizeof(struct at91_adc_state));
527 if (idev == NULL) {
528 ret = -ENOMEM;
529 goto error_ret;
530 }
531
532 st = iio_priv(idev);
533
Maxime Riparde3641852012-05-11 15:35:37 +0200534 if (pdev->dev.of_node)
535 ret = at91_adc_probe_dt(st, pdev);
536 else
537 ret = at91_adc_probe_pdata(st, pdev);
538
Maxime Ripard0e589d52012-05-11 15:35:33 +0200539 if (ret) {
540 dev_err(&pdev->dev, "No platform data available.\n");
541 ret = -EINVAL;
542 goto error_free_device;
543 }
544
Maxime Ripard0e589d52012-05-11 15:35:33 +0200545 platform_set_drvdata(pdev, idev);
546
547 idev->dev.parent = &pdev->dev;
548 idev->name = dev_name(&pdev->dev);
549 idev->modes = INDIO_DIRECT_MODE;
550 idev->info = &at91_adc_info;
551
552 st->irq = platform_get_irq(pdev, 0);
553 if (st->irq < 0) {
554 dev_err(&pdev->dev, "No IRQ ID is designated\n");
555 ret = -ENODEV;
556 goto error_free_device;
557 }
558
Julia Lawall390d75c2012-07-31 14:09:00 +0100559 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200560
Julia Lawall390d75c2012-07-31 14:09:00 +0100561 st->reg_base = devm_request_and_ioremap(&pdev->dev, res);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200562 if (!st->reg_base) {
Maxime Ripard0e589d52012-05-11 15:35:33 +0200563 ret = -ENOMEM;
Julia Lawall390d75c2012-07-31 14:09:00 +0100564 goto error_free_device;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200565 }
566
567 /*
568 * Disable all IRQs before setting up the handler
569 */
570 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
571 at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
572 ret = request_irq(st->irq,
573 at91_adc_eoc_trigger,
574 0,
575 pdev->dev.driver->name,
576 idev);
577 if (ret) {
578 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100579 goto error_free_device;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200580 }
581
Julia Lawall390d75c2012-07-31 14:09:00 +0100582 st->clk = devm_clk_get(&pdev->dev, "adc_clk");
Maxime Ripard0e589d52012-05-11 15:35:33 +0200583 if (IS_ERR(st->clk)) {
584 dev_err(&pdev->dev, "Failed to get the clock.\n");
585 ret = PTR_ERR(st->clk);
586 goto error_free_irq;
587 }
588
Julia Lawall00062a9c2012-08-26 17:00:00 +0100589 ret = clk_prepare_enable(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200590 if (ret) {
Julia Lawall00062a9c2012-08-26 17:00:00 +0100591 dev_err(&pdev->dev,
592 "Could not prepare or enable the clock.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100593 goto error_free_irq;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200594 }
595
Julia Lawall390d75c2012-07-31 14:09:00 +0100596 st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
Maxime Ripard0e589d52012-05-11 15:35:33 +0200597 if (IS_ERR(st->adc_clk)) {
598 dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
Julia Lawallf755bbb2012-08-25 21:57:09 +0200599 ret = PTR_ERR(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200600 goto error_disable_clk;
601 }
602
Julia Lawall00062a9c2012-08-26 17:00:00 +0100603 ret = clk_prepare_enable(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200604 if (ret) {
Julia Lawall00062a9c2012-08-26 17:00:00 +0100605 dev_err(&pdev->dev,
606 "Could not prepare or enable the ADC clock.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100607 goto error_disable_clk;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200608 }
609
Maxime Ripard0e589d52012-05-11 15:35:33 +0200610 /*
611 * Prescaler rate computation using the formula from the Atmel's
612 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
613 * specified by the electrical characteristics of the board.
614 */
615 mstrclk = clk_get_rate(st->clk);
616 adc_clk = clk_get_rate(st->adc_clk);
617 prsc = (mstrclk / (2 * adc_clk)) - 1;
618
619 if (!st->startup_time) {
620 dev_err(&pdev->dev, "No startup time available.\n");
621 ret = -EINVAL;
622 goto error_disable_adc_clk;
623 }
624
625 /*
626 * Number of ticks needed to cover the startup time of the ADC as
627 * defined in the electrical characteristics of the board, divided by 8.
628 * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock
629 */
630 ticks = round_up((st->startup_time * adc_clk /
631 1000000) - 1, 8) / 8;
632 at91_adc_writel(st, AT91_ADC_MR,
633 (AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL) |
634 (AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP));
635
636 /* Setup the ADC channels available on the board */
637 ret = at91_adc_channel_init(idev);
638 if (ret < 0) {
639 dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
640 goto error_disable_adc_clk;
641 }
642
643 init_waitqueue_head(&st->wq_data_avail);
644 mutex_init(&st->lock);
645
646 ret = at91_adc_buffer_init(idev);
647 if (ret < 0) {
648 dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
649 goto error_disable_adc_clk;
650 }
651
652 ret = at91_adc_trigger_init(idev);
653 if (ret < 0) {
654 dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
655 goto error_unregister_buffer;
656 }
657
658 ret = iio_device_register(idev);
659 if (ret < 0) {
660 dev_err(&pdev->dev, "Couldn't register the device.\n");
661 goto error_remove_triggers;
662 }
663
664 return 0;
665
666error_remove_triggers:
667 at91_adc_trigger_remove(idev);
668error_unregister_buffer:
669 at91_adc_buffer_remove(idev);
670error_disable_adc_clk:
Julia Lawall00062a9c2012-08-26 17:00:00 +0100671 clk_disable_unprepare(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200672error_disable_clk:
Julia Lawall00062a9c2012-08-26 17:00:00 +0100673 clk_disable_unprepare(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200674error_free_irq:
675 free_irq(st->irq, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200676error_free_device:
677 iio_device_free(idev);
678error_ret:
679 return ret;
680}
681
682static int __devexit at91_adc_remove(struct platform_device *pdev)
683{
684 struct iio_dev *idev = platform_get_drvdata(pdev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200685 struct at91_adc_state *st = iio_priv(idev);
686
687 iio_device_unregister(idev);
688 at91_adc_trigger_remove(idev);
689 at91_adc_buffer_remove(idev);
690 clk_disable_unprepare(st->adc_clk);
Julia Lawall00062a9c2012-08-26 17:00:00 +0100691 clk_disable_unprepare(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200692 free_irq(st->irq, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200693 iio_device_free(idev);
694
695 return 0;
696}
697
Maxime Riparde3641852012-05-11 15:35:37 +0200698static const struct of_device_id at91_adc_dt_ids[] = {
699 { .compatible = "atmel,at91sam9260-adc" },
700 {},
701};
702MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
703
Maxime Ripard0e589d52012-05-11 15:35:33 +0200704static struct platform_driver at91_adc_driver = {
705 .probe = at91_adc_probe,
706 .remove = __devexit_p(at91_adc_remove),
707 .driver = {
708 .name = "at91_adc",
Maxime Riparde3641852012-05-11 15:35:37 +0200709 .of_match_table = of_match_ptr(at91_adc_dt_ids),
Maxime Ripard0e589d52012-05-11 15:35:33 +0200710 },
711};
712
713module_platform_driver(at91_adc_driver);
714
715MODULE_LICENSE("GPL");
716MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
717MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");