blob: 3fb3fe48a98c7dc45dfaa2a4f91cf07276a8648c [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;
Ludovic Desroches47be16b2013-03-29 14:54:00 +000060 u32 res; /* resolution used for convertions */
61 bool low_res; /* the resolution corresponds to the lowest one */
Maxime Ripard0e589d52012-05-11 15:35:33 +020062 wait_queue_head_t wq_data_avail;
63};
64
65static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
66{
67 struct iio_poll_func *pf = p;
68 struct iio_dev *idev = pf->indio_dev;
69 struct at91_adc_state *st = iio_priv(idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +020070 int i, j = 0;
71
72 for (i = 0; i < idev->masklength; i++) {
73 if (!test_bit(i, idev->active_scan_mask))
74 continue;
75 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
76 j++;
77 }
78
79 if (idev->scan_timestamp) {
80 s64 *timestamp = (s64 *)((u8 *)st->buffer +
81 ALIGN(j, sizeof(s64)));
82 *timestamp = pf->timestamp;
83 }
84
Jean-Christophe PLAGNIOL-VILLARD11679762012-12-12 15:17:00 +000085 iio_push_to_buffers(idev, (u8 *)st->buffer);
Maxime Ripard0e589d52012-05-11 15:35:33 +020086
87 iio_trigger_notify_done(idev->trig);
Maxime Ripard0e589d52012-05-11 15:35:33 +020088
89 /* Needed to ACK the DRDY interruption */
90 at91_adc_readl(st, AT91_ADC_LCDR);
91
92 enable_irq(st->irq);
93
94 return IRQ_HANDLED;
95}
96
97static irqreturn_t at91_adc_eoc_trigger(int irq, void *private)
98{
99 struct iio_dev *idev = private;
100 struct at91_adc_state *st = iio_priv(idev);
101 u32 status = at91_adc_readl(st, st->registers->status_register);
102
103 if (!(status & st->registers->drdy_mask))
104 return IRQ_HANDLED;
105
106 if (iio_buffer_enabled(idev)) {
107 disable_irq_nosync(irq);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200108 iio_trigger_poll(idev->trig, iio_get_time_ns());
109 } else {
110 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
111 st->done = true;
112 wake_up_interruptible(&st->wq_data_avail);
113 }
114
115 return IRQ_HANDLED;
116}
117
118static int at91_adc_channel_init(struct iio_dev *idev)
119{
120 struct at91_adc_state *st = iio_priv(idev);
121 struct iio_chan_spec *chan_array, *timestamp;
122 int bit, idx = 0;
123
124 idev->num_channels = bitmap_weight(&st->channels_mask,
125 st->num_channels) + 1;
126
Axel Lin6b3aa312012-10-29 08:25:00 +0000127 chan_array = devm_kzalloc(&idev->dev,
128 ((idev->num_channels + 1) *
129 sizeof(struct iio_chan_spec)),
130 GFP_KERNEL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200131
132 if (!chan_array)
133 return -ENOMEM;
134
135 for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
136 struct iio_chan_spec *chan = chan_array + idx;
137
138 chan->type = IIO_VOLTAGE;
139 chan->indexed = 1;
140 chan->channel = bit;
141 chan->scan_index = idx;
142 chan->scan_type.sign = 'u';
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000143 chan->scan_type.realbits = st->res;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200144 chan->scan_type.storagebits = 16;
Jonathan Cameron01bdab62013-02-27 19:06:10 +0000145 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
146 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200147 idx++;
148 }
149 timestamp = chan_array + idx;
150
151 timestamp->type = IIO_TIMESTAMP;
152 timestamp->channel = -1;
153 timestamp->scan_index = idx;
154 timestamp->scan_type.sign = 's';
155 timestamp->scan_type.realbits = 64;
156 timestamp->scan_type.storagebits = 64;
157
158 idev->channels = chan_array;
159 return idev->num_channels;
160}
161
162static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
163 struct at91_adc_trigger *triggers,
164 const char *trigger_name)
165{
166 struct at91_adc_state *st = iio_priv(idev);
167 u8 value = 0;
168 int i;
169
170 for (i = 0; i < st->trigger_number; i++) {
171 char *name = kasprintf(GFP_KERNEL,
172 "%s-dev%d-%s",
173 idev->name,
174 idev->id,
175 triggers[i].name);
176 if (!name)
177 return -ENOMEM;
178
179 if (strcmp(trigger_name, name) == 0) {
180 value = triggers[i].value;
181 kfree(name);
182 break;
183 }
184
185 kfree(name);
186 }
187
188 return value;
189}
190
191static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
192{
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000193 struct iio_dev *idev = iio_trigger_get_drvdata(trig);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200194 struct at91_adc_state *st = iio_priv(idev);
195 struct iio_buffer *buffer = idev->buffer;
196 struct at91_adc_reg_desc *reg = st->registers;
197 u32 status = at91_adc_readl(st, reg->trigger_register);
198 u8 value;
199 u8 bit;
200
201 value = at91_adc_get_trigger_value_by_name(idev,
202 st->trigger_list,
203 idev->trig->name);
204 if (value == 0)
205 return -EINVAL;
206
207 if (state) {
208 st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
209 if (st->buffer == NULL)
210 return -ENOMEM;
211
212 at91_adc_writel(st, reg->trigger_register,
213 status | value);
214
215 for_each_set_bit(bit, buffer->scan_mask,
216 st->num_channels) {
217 struct iio_chan_spec const *chan = idev->channels + bit;
218 at91_adc_writel(st, AT91_ADC_CHER,
219 AT91_ADC_CH(chan->channel));
220 }
221
222 at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
223
224 } else {
225 at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
226
227 at91_adc_writel(st, reg->trigger_register,
228 status & ~value);
229
230 for_each_set_bit(bit, buffer->scan_mask,
231 st->num_channels) {
232 struct iio_chan_spec const *chan = idev->channels + bit;
233 at91_adc_writel(st, AT91_ADC_CHDR,
234 AT91_ADC_CH(chan->channel));
235 }
236 kfree(st->buffer);
237 }
238
239 return 0;
240}
241
242static const struct iio_trigger_ops at91_adc_trigger_ops = {
243 .owner = THIS_MODULE,
244 .set_trigger_state = &at91_adc_configure_trigger,
245};
246
247static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
248 struct at91_adc_trigger *trigger)
249{
250 struct iio_trigger *trig;
251 int ret;
252
253 trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
254 idev->id, trigger->name);
255 if (trig == NULL)
256 return NULL;
257
258 trig->dev.parent = idev->dev.parent;
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000259 iio_trigger_set_drvdata(trig, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200260 trig->ops = &at91_adc_trigger_ops;
261
262 ret = iio_trigger_register(trig);
263 if (ret)
264 return NULL;
265
266 return trig;
267}
268
269static int at91_adc_trigger_init(struct iio_dev *idev)
270{
271 struct at91_adc_state *st = iio_priv(idev);
272 int i, ret;
273
Axel Lin6b3aa312012-10-29 08:25:00 +0000274 st->trig = devm_kzalloc(&idev->dev,
275 st->trigger_number * sizeof(st->trig),
276 GFP_KERNEL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200277
278 if (st->trig == NULL) {
279 ret = -ENOMEM;
280 goto error_ret;
281 }
282
283 for (i = 0; i < st->trigger_number; i++) {
284 if (st->trigger_list[i].is_external && !(st->use_external))
285 continue;
286
287 st->trig[i] = at91_adc_allocate_trigger(idev,
288 st->trigger_list + i);
289 if (st->trig[i] == NULL) {
290 dev_err(&idev->dev,
291 "Could not allocate trigger %d\n", i);
292 ret = -ENOMEM;
293 goto error_trigger;
294 }
295 }
296
297 return 0;
298
299error_trigger:
300 for (i--; i >= 0; i--) {
301 iio_trigger_unregister(st->trig[i]);
302 iio_trigger_free(st->trig[i]);
303 }
304error_ret:
305 return ret;
306}
307
308static void at91_adc_trigger_remove(struct iio_dev *idev)
309{
310 struct at91_adc_state *st = iio_priv(idev);
311 int i;
312
313 for (i = 0; i < st->trigger_number; i++) {
314 iio_trigger_unregister(st->trig[i]);
315 iio_trigger_free(st->trig[i]);
316 }
317}
318
Maxime Ripard0e589d52012-05-11 15:35:33 +0200319static int at91_adc_buffer_init(struct iio_dev *idev)
320{
Lars-Peter Clausen90032e42012-06-18 18:33:49 +0200321 return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
322 &at91_adc_trigger_handler, NULL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200323}
324
325static void at91_adc_buffer_remove(struct iio_dev *idev)
326{
Lars-Peter Clausen90032e42012-06-18 18:33:49 +0200327 iio_triggered_buffer_cleanup(idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200328}
329
330static int at91_adc_read_raw(struct iio_dev *idev,
331 struct iio_chan_spec const *chan,
332 int *val, int *val2, long mask)
333{
334 struct at91_adc_state *st = iio_priv(idev);
335 int ret;
336
337 switch (mask) {
338 case IIO_CHAN_INFO_RAW:
339 mutex_lock(&st->lock);
340
341 at91_adc_writel(st, AT91_ADC_CHER,
342 AT91_ADC_CH(chan->channel));
343 at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask);
344 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
345
346 ret = wait_event_interruptible_timeout(st->wq_data_avail,
347 st->done,
348 msecs_to_jiffies(1000));
349 if (ret == 0)
Lars-Peter Clausen90e6dc72012-06-26 10:43:05 +0200350 ret = -ETIMEDOUT;
351 if (ret < 0) {
352 mutex_unlock(&st->lock);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200353 return ret;
Lars-Peter Clausen90e6dc72012-06-26 10:43:05 +0200354 }
Maxime Ripard0e589d52012-05-11 15:35:33 +0200355
356 *val = st->last_value;
357
358 at91_adc_writel(st, AT91_ADC_CHDR,
359 AT91_ADC_CH(chan->channel));
360 at91_adc_writel(st, AT91_ADC_IDR, st->registers->drdy_mask);
361
362 st->last_value = 0;
363 st->done = false;
364 mutex_unlock(&st->lock);
365 return IIO_VAL_INT;
366
367 case IIO_CHAN_INFO_SCALE:
368 *val = (st->vref_mv * 1000) >> chan->scan_type.realbits;
369 *val2 = 0;
370 return IIO_VAL_INT_PLUS_MICRO;
371 default:
372 break;
373 }
374 return -EINVAL;
375}
376
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000377static int at91_adc_of_get_resolution(struct at91_adc_state *st,
378 struct platform_device *pdev)
379{
380 struct iio_dev *idev = iio_priv_to_dev(st);
381 struct device_node *np = pdev->dev.of_node;
382 int count, i, ret = 0;
383 char *res_name, *s;
384 u32 *resolutions;
385
386 count = of_property_count_strings(np, "atmel,adc-res-names");
387 if (count < 2) {
388 dev_err(&idev->dev, "You must specified at least two resolution names for "
389 "adc-res-names property in the DT\n");
390 return count;
391 }
392
393 resolutions = kmalloc(count * sizeof(*resolutions), GFP_KERNEL);
394 if (!resolutions)
395 return -ENOMEM;
396
397 if (of_property_read_u32_array(np, "atmel,adc-res", resolutions, count)) {
398 dev_err(&idev->dev, "Missing adc-res property in the DT.\n");
399 ret = -ENODEV;
400 goto ret;
401 }
402
403 if (of_property_read_string(np, "atmel,adc-use-res", (const char **)&res_name))
404 res_name = "highres";
405
406 for (i = 0; i < count; i++) {
407 if (of_property_read_string_index(np, "atmel,adc-res-names", i, (const char **)&s))
408 continue;
409
410 if (strcmp(res_name, s))
411 continue;
412
413 st->res = resolutions[i];
414 if (!strcmp(res_name, "lowres"))
415 st->low_res = true;
416 else
417 st->low_res = false;
418
419 dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
420 goto ret;
421 }
422
423 dev_err(&idev->dev, "There is no resolution for %s\n", res_name);
424
425ret:
426 kfree(resolutions);
427 return ret;
428}
429
Maxime Riparde3641852012-05-11 15:35:37 +0200430static int at91_adc_probe_dt(struct at91_adc_state *st,
431 struct platform_device *pdev)
432{
433 struct iio_dev *idev = iio_priv_to_dev(st);
434 struct device_node *node = pdev->dev.of_node;
435 struct device_node *trig_node;
436 int i = 0, ret;
437 u32 prop;
438
439 if (!node)
440 return -EINVAL;
441
442 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
443
444 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
445 dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
446 ret = -EINVAL;
447 goto error_ret;
448 }
449 st->channels_mask = prop;
450
451 if (of_property_read_u32(node, "atmel,adc-num-channels", &prop)) {
452 dev_err(&idev->dev, "Missing adc-num-channels property in the DT.\n");
453 ret = -EINVAL;
454 goto error_ret;
455 }
456 st->num_channels = prop;
457
458 if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
459 dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
460 ret = -EINVAL;
461 goto error_ret;
462 }
463 st->startup_time = prop;
464
465
466 if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
467 dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
468 ret = -EINVAL;
469 goto error_ret;
470 }
471 st->vref_mv = prop;
472
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000473 ret = at91_adc_of_get_resolution(st, pdev);
474 if (ret)
475 goto error_ret;
476
Maxime Riparde3641852012-05-11 15:35:37 +0200477 st->registers = devm_kzalloc(&idev->dev,
478 sizeof(struct at91_adc_reg_desc),
479 GFP_KERNEL);
480 if (!st->registers) {
481 dev_err(&idev->dev, "Could not allocate register memory.\n");
482 ret = -ENOMEM;
483 goto error_ret;
484 }
485
486 if (of_property_read_u32(node, "atmel,adc-channel-base", &prop)) {
487 dev_err(&idev->dev, "Missing adc-channel-base property in the DT.\n");
488 ret = -EINVAL;
489 goto error_ret;
490 }
491 st->registers->channel_base = prop;
492
493 if (of_property_read_u32(node, "atmel,adc-drdy-mask", &prop)) {
494 dev_err(&idev->dev, "Missing adc-drdy-mask property in the DT.\n");
495 ret = -EINVAL;
496 goto error_ret;
497 }
498 st->registers->drdy_mask = prop;
499
500 if (of_property_read_u32(node, "atmel,adc-status-register", &prop)) {
501 dev_err(&idev->dev, "Missing adc-status-register property in the DT.\n");
502 ret = -EINVAL;
503 goto error_ret;
504 }
505 st->registers->status_register = prop;
506
507 if (of_property_read_u32(node, "atmel,adc-trigger-register", &prop)) {
508 dev_err(&idev->dev, "Missing adc-trigger-register property in the DT.\n");
509 ret = -EINVAL;
510 goto error_ret;
511 }
512 st->registers->trigger_register = prop;
513
514 st->trigger_number = of_get_child_count(node);
Axel Lin6b3aa312012-10-29 08:25:00 +0000515 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
516 sizeof(struct at91_adc_trigger),
517 GFP_KERNEL);
Maxime Riparde3641852012-05-11 15:35:37 +0200518 if (!st->trigger_list) {
519 dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
520 ret = -ENOMEM;
521 goto error_ret;
522 }
523
524 for_each_child_of_node(node, trig_node) {
525 struct at91_adc_trigger *trig = st->trigger_list + i;
526 const char *name;
527
528 if (of_property_read_string(trig_node, "trigger-name", &name)) {
529 dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
530 ret = -EINVAL;
531 goto error_ret;
532 }
533 trig->name = name;
534
535 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
536 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
537 ret = -EINVAL;
538 goto error_ret;
539 }
540 trig->value = prop;
541 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
542 i++;
543 }
544
545 return 0;
546
547error_ret:
548 return ret;
549}
550
Maxime Ripard0e589d52012-05-11 15:35:33 +0200551static int at91_adc_probe_pdata(struct at91_adc_state *st,
552 struct platform_device *pdev)
553{
554 struct at91_adc_data *pdata = pdev->dev.platform_data;
555
556 if (!pdata)
557 return -EINVAL;
558
559 st->use_external = pdata->use_external_triggers;
560 st->vref_mv = pdata->vref;
561 st->channels_mask = pdata->channels_used;
562 st->num_channels = pdata->num_channels;
563 st->startup_time = pdata->startup_time;
564 st->trigger_number = pdata->trigger_number;
565 st->trigger_list = pdata->trigger_list;
566 st->registers = pdata->registers;
567
568 return 0;
569}
570
571static const struct iio_info at91_adc_info = {
572 .driver_module = THIS_MODULE,
573 .read_raw = &at91_adc_read_raw,
574};
575
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800576static int at91_adc_probe(struct platform_device *pdev)
Maxime Ripard0e589d52012-05-11 15:35:33 +0200577{
578 unsigned int prsc, mstrclk, ticks, adc_clk;
579 int ret;
580 struct iio_dev *idev;
581 struct at91_adc_state *st;
582 struct resource *res;
583
584 idev = iio_device_alloc(sizeof(struct at91_adc_state));
585 if (idev == NULL) {
586 ret = -ENOMEM;
587 goto error_ret;
588 }
589
590 st = iio_priv(idev);
591
Maxime Riparde3641852012-05-11 15:35:37 +0200592 if (pdev->dev.of_node)
593 ret = at91_adc_probe_dt(st, pdev);
594 else
595 ret = at91_adc_probe_pdata(st, pdev);
596
Maxime Ripard0e589d52012-05-11 15:35:33 +0200597 if (ret) {
598 dev_err(&pdev->dev, "No platform data available.\n");
599 ret = -EINVAL;
600 goto error_free_device;
601 }
602
Maxime Ripard0e589d52012-05-11 15:35:33 +0200603 platform_set_drvdata(pdev, idev);
604
605 idev->dev.parent = &pdev->dev;
606 idev->name = dev_name(&pdev->dev);
607 idev->modes = INDIO_DIRECT_MODE;
608 idev->info = &at91_adc_info;
609
610 st->irq = platform_get_irq(pdev, 0);
611 if (st->irq < 0) {
612 dev_err(&pdev->dev, "No IRQ ID is designated\n");
613 ret = -ENODEV;
614 goto error_free_device;
615 }
616
Julia Lawall390d75c2012-07-31 14:09:00 +0100617 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200618
Thierry Reding5fd98462013-01-21 11:09:04 +0100619 st->reg_base = devm_ioremap_resource(&pdev->dev, res);
620 if (IS_ERR(st->reg_base)) {
621 ret = PTR_ERR(st->reg_base);
Julia Lawall390d75c2012-07-31 14:09:00 +0100622 goto error_free_device;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200623 }
624
625 /*
626 * Disable all IRQs before setting up the handler
627 */
628 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
629 at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
630 ret = request_irq(st->irq,
631 at91_adc_eoc_trigger,
632 0,
633 pdev->dev.driver->name,
634 idev);
635 if (ret) {
636 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100637 goto error_free_device;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200638 }
639
Julia Lawall390d75c2012-07-31 14:09:00 +0100640 st->clk = devm_clk_get(&pdev->dev, "adc_clk");
Maxime Ripard0e589d52012-05-11 15:35:33 +0200641 if (IS_ERR(st->clk)) {
642 dev_err(&pdev->dev, "Failed to get the clock.\n");
643 ret = PTR_ERR(st->clk);
644 goto error_free_irq;
645 }
646
Julia Lawall00062a9c2012-08-26 17:00:00 +0100647 ret = clk_prepare_enable(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200648 if (ret) {
Julia Lawall00062a9c2012-08-26 17:00:00 +0100649 dev_err(&pdev->dev,
650 "Could not prepare or enable the clock.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100651 goto error_free_irq;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200652 }
653
Julia Lawall390d75c2012-07-31 14:09:00 +0100654 st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
Maxime Ripard0e589d52012-05-11 15:35:33 +0200655 if (IS_ERR(st->adc_clk)) {
656 dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
Julia Lawallf755bbb2012-08-25 21:57:09 +0200657 ret = PTR_ERR(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200658 goto error_disable_clk;
659 }
660
Julia Lawall00062a9c2012-08-26 17:00:00 +0100661 ret = clk_prepare_enable(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200662 if (ret) {
Julia Lawall00062a9c2012-08-26 17:00:00 +0100663 dev_err(&pdev->dev,
664 "Could not prepare or enable the ADC clock.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100665 goto error_disable_clk;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200666 }
667
Maxime Ripard0e589d52012-05-11 15:35:33 +0200668 /*
669 * Prescaler rate computation using the formula from the Atmel's
670 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
671 * specified by the electrical characteristics of the board.
672 */
673 mstrclk = clk_get_rate(st->clk);
674 adc_clk = clk_get_rate(st->adc_clk);
675 prsc = (mstrclk / (2 * adc_clk)) - 1;
676
677 if (!st->startup_time) {
678 dev_err(&pdev->dev, "No startup time available.\n");
679 ret = -EINVAL;
680 goto error_disable_adc_clk;
681 }
682
683 /*
684 * Number of ticks needed to cover the startup time of the ADC as
685 * defined in the electrical characteristics of the board, divided by 8.
686 * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock
687 */
688 ticks = round_up((st->startup_time * adc_clk /
689 1000000) - 1, 8) / 8;
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000690
691 if (st->low_res)
692 at91_adc_writel(st, AT91_ADC_MR,
693 AT91_ADC_LOWRES |
694 (AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL) |
695 (AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP));
696 else
697 at91_adc_writel(st, AT91_ADC_MR,
698 (AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL) |
699 (AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP));
Maxime Ripard0e589d52012-05-11 15:35:33 +0200700
701 /* Setup the ADC channels available on the board */
702 ret = at91_adc_channel_init(idev);
703 if (ret < 0) {
704 dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
705 goto error_disable_adc_clk;
706 }
707
708 init_waitqueue_head(&st->wq_data_avail);
709 mutex_init(&st->lock);
710
711 ret = at91_adc_buffer_init(idev);
712 if (ret < 0) {
713 dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
714 goto error_disable_adc_clk;
715 }
716
717 ret = at91_adc_trigger_init(idev);
718 if (ret < 0) {
719 dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
720 goto error_unregister_buffer;
721 }
722
723 ret = iio_device_register(idev);
724 if (ret < 0) {
725 dev_err(&pdev->dev, "Couldn't register the device.\n");
726 goto error_remove_triggers;
727 }
728
729 return 0;
730
731error_remove_triggers:
732 at91_adc_trigger_remove(idev);
733error_unregister_buffer:
734 at91_adc_buffer_remove(idev);
735error_disable_adc_clk:
Julia Lawall00062a9c2012-08-26 17:00:00 +0100736 clk_disable_unprepare(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200737error_disable_clk:
Julia Lawall00062a9c2012-08-26 17:00:00 +0100738 clk_disable_unprepare(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200739error_free_irq:
740 free_irq(st->irq, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200741error_free_device:
742 iio_device_free(idev);
743error_ret:
744 return ret;
745}
746
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800747static int at91_adc_remove(struct platform_device *pdev)
Maxime Ripard0e589d52012-05-11 15:35:33 +0200748{
749 struct iio_dev *idev = platform_get_drvdata(pdev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200750 struct at91_adc_state *st = iio_priv(idev);
751
752 iio_device_unregister(idev);
753 at91_adc_trigger_remove(idev);
754 at91_adc_buffer_remove(idev);
755 clk_disable_unprepare(st->adc_clk);
Julia Lawall00062a9c2012-08-26 17:00:00 +0100756 clk_disable_unprepare(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200757 free_irq(st->irq, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200758 iio_device_free(idev);
759
760 return 0;
761}
762
Maxime Riparde3641852012-05-11 15:35:37 +0200763static const struct of_device_id at91_adc_dt_ids[] = {
764 { .compatible = "atmel,at91sam9260-adc" },
765 {},
766};
767MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
768
Maxime Ripard0e589d52012-05-11 15:35:33 +0200769static struct platform_driver at91_adc_driver = {
770 .probe = at91_adc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800771 .remove = at91_adc_remove,
Maxime Ripard0e589d52012-05-11 15:35:33 +0200772 .driver = {
773 .name = "at91_adc",
Maxime Riparde3641852012-05-11 15:35:37 +0200774 .of_match_table = of_match_ptr(at91_adc_dt_ids),
Maxime Ripard0e589d52012-05-11 15:35:33 +0200775 },
776};
777
778module_platform_driver(at91_adc_driver);
779
780MODULE_LICENSE("GPL");
781MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
782MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");