blob: 79c53b8e195c5de380b83afde3357d593ed4b1b9 [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
Josh Wue1811f92013-08-27 12:28:00 +010042struct at91_adc_caps {
43 struct at91_adc_reg_desc registers;
44};
45
Maxime Ripard0e589d52012-05-11 15:35:33 +020046struct at91_adc_state {
47 struct clk *adc_clk;
48 u16 *buffer;
49 unsigned long channels_mask;
50 struct clk *clk;
51 bool done;
52 int irq;
Maxime Ripard0e589d52012-05-11 15:35:33 +020053 u16 last_value;
54 struct mutex lock;
55 u8 num_channels;
56 void __iomem *reg_base;
57 struct at91_adc_reg_desc *registers;
58 u8 startup_time;
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +000059 u8 sample_hold_time;
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +000060 bool sleep_mode;
Maxime Ripard0e589d52012-05-11 15:35:33 +020061 struct iio_trigger **trig;
62 struct at91_adc_trigger *trigger_list;
63 u32 trigger_number;
64 bool use_external;
65 u32 vref_mv;
Ludovic Desroches47be16b2013-03-29 14:54:00 +000066 u32 res; /* resolution used for convertions */
67 bool low_res; /* the resolution corresponds to the lowest one */
Maxime Ripard0e589d52012-05-11 15:35:33 +020068 wait_queue_head_t wq_data_avail;
Josh Wue1811f92013-08-27 12:28:00 +010069 struct at91_adc_caps *caps;
Maxime Ripard0e589d52012-05-11 15:35:33 +020070};
71
72static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
73{
74 struct iio_poll_func *pf = p;
75 struct iio_dev *idev = pf->indio_dev;
76 struct at91_adc_state *st = iio_priv(idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +020077 int i, j = 0;
78
79 for (i = 0; i < idev->masklength; i++) {
80 if (!test_bit(i, idev->active_scan_mask))
81 continue;
82 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
83 j++;
84 }
85
86 if (idev->scan_timestamp) {
87 s64 *timestamp = (s64 *)((u8 *)st->buffer +
88 ALIGN(j, sizeof(s64)));
89 *timestamp = pf->timestamp;
90 }
91
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +010092 iio_push_to_buffers(idev, st->buffer);
Maxime Ripard0e589d52012-05-11 15:35:33 +020093
94 iio_trigger_notify_done(idev->trig);
Maxime Ripard0e589d52012-05-11 15:35:33 +020095
96 /* Needed to ACK the DRDY interruption */
97 at91_adc_readl(st, AT91_ADC_LCDR);
98
99 enable_irq(st->irq);
100
101 return IRQ_HANDLED;
102}
103
104static irqreturn_t at91_adc_eoc_trigger(int irq, void *private)
105{
106 struct iio_dev *idev = private;
107 struct at91_adc_state *st = iio_priv(idev);
108 u32 status = at91_adc_readl(st, st->registers->status_register);
109
110 if (!(status & st->registers->drdy_mask))
111 return IRQ_HANDLED;
112
113 if (iio_buffer_enabled(idev)) {
114 disable_irq_nosync(irq);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200115 iio_trigger_poll(idev->trig, iio_get_time_ns());
116 } else {
117 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
118 st->done = true;
119 wake_up_interruptible(&st->wq_data_avail);
120 }
121
122 return IRQ_HANDLED;
123}
124
125static int at91_adc_channel_init(struct iio_dev *idev)
126{
127 struct at91_adc_state *st = iio_priv(idev);
128 struct iio_chan_spec *chan_array, *timestamp;
129 int bit, idx = 0;
130
131 idev->num_channels = bitmap_weight(&st->channels_mask,
132 st->num_channels) + 1;
133
Axel Lin6b3aa312012-10-29 08:25:00 +0000134 chan_array = devm_kzalloc(&idev->dev,
135 ((idev->num_channels + 1) *
136 sizeof(struct iio_chan_spec)),
137 GFP_KERNEL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200138
139 if (!chan_array)
140 return -ENOMEM;
141
142 for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
143 struct iio_chan_spec *chan = chan_array + idx;
144
145 chan->type = IIO_VOLTAGE;
146 chan->indexed = 1;
147 chan->channel = bit;
148 chan->scan_index = idx;
149 chan->scan_type.sign = 'u';
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000150 chan->scan_type.realbits = st->res;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200151 chan->scan_type.storagebits = 16;
Jonathan Cameron01bdab62013-02-27 19:06:10 +0000152 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
153 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200154 idx++;
155 }
156 timestamp = chan_array + idx;
157
158 timestamp->type = IIO_TIMESTAMP;
159 timestamp->channel = -1;
160 timestamp->scan_index = idx;
161 timestamp->scan_type.sign = 's';
162 timestamp->scan_type.realbits = 64;
163 timestamp->scan_type.storagebits = 64;
164
165 idev->channels = chan_array;
166 return idev->num_channels;
167}
168
169static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
170 struct at91_adc_trigger *triggers,
171 const char *trigger_name)
172{
173 struct at91_adc_state *st = iio_priv(idev);
174 u8 value = 0;
175 int i;
176
177 for (i = 0; i < st->trigger_number; i++) {
178 char *name = kasprintf(GFP_KERNEL,
179 "%s-dev%d-%s",
180 idev->name,
181 idev->id,
182 triggers[i].name);
183 if (!name)
184 return -ENOMEM;
185
186 if (strcmp(trigger_name, name) == 0) {
187 value = triggers[i].value;
188 kfree(name);
189 break;
190 }
191
192 kfree(name);
193 }
194
195 return value;
196}
197
198static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
199{
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000200 struct iio_dev *idev = iio_trigger_get_drvdata(trig);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200201 struct at91_adc_state *st = iio_priv(idev);
202 struct iio_buffer *buffer = idev->buffer;
203 struct at91_adc_reg_desc *reg = st->registers;
204 u32 status = at91_adc_readl(st, reg->trigger_register);
205 u8 value;
206 u8 bit;
207
208 value = at91_adc_get_trigger_value_by_name(idev,
209 st->trigger_list,
210 idev->trig->name);
211 if (value == 0)
212 return -EINVAL;
213
214 if (state) {
215 st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
216 if (st->buffer == NULL)
217 return -ENOMEM;
218
219 at91_adc_writel(st, reg->trigger_register,
220 status | value);
221
222 for_each_set_bit(bit, buffer->scan_mask,
223 st->num_channels) {
224 struct iio_chan_spec const *chan = idev->channels + bit;
225 at91_adc_writel(st, AT91_ADC_CHER,
226 AT91_ADC_CH(chan->channel));
227 }
228
229 at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
230
231 } else {
232 at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
233
234 at91_adc_writel(st, reg->trigger_register,
235 status & ~value);
236
237 for_each_set_bit(bit, buffer->scan_mask,
238 st->num_channels) {
239 struct iio_chan_spec const *chan = idev->channels + bit;
240 at91_adc_writel(st, AT91_ADC_CHDR,
241 AT91_ADC_CH(chan->channel));
242 }
243 kfree(st->buffer);
244 }
245
246 return 0;
247}
248
249static const struct iio_trigger_ops at91_adc_trigger_ops = {
250 .owner = THIS_MODULE,
251 .set_trigger_state = &at91_adc_configure_trigger,
252};
253
254static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
255 struct at91_adc_trigger *trigger)
256{
257 struct iio_trigger *trig;
258 int ret;
259
260 trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
261 idev->id, trigger->name);
262 if (trig == NULL)
263 return NULL;
264
265 trig->dev.parent = idev->dev.parent;
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000266 iio_trigger_set_drvdata(trig, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200267 trig->ops = &at91_adc_trigger_ops;
268
269 ret = iio_trigger_register(trig);
270 if (ret)
271 return NULL;
272
273 return trig;
274}
275
276static int at91_adc_trigger_init(struct iio_dev *idev)
277{
278 struct at91_adc_state *st = iio_priv(idev);
279 int i, ret;
280
Axel Lin6b3aa312012-10-29 08:25:00 +0000281 st->trig = devm_kzalloc(&idev->dev,
Thomas Meyer42877232013-09-19 22:42:00 +0100282 st->trigger_number * sizeof(*st->trig),
Axel Lin6b3aa312012-10-29 08:25:00 +0000283 GFP_KERNEL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200284
285 if (st->trig == NULL) {
286 ret = -ENOMEM;
287 goto error_ret;
288 }
289
290 for (i = 0; i < st->trigger_number; i++) {
291 if (st->trigger_list[i].is_external && !(st->use_external))
292 continue;
293
294 st->trig[i] = at91_adc_allocate_trigger(idev,
295 st->trigger_list + i);
296 if (st->trig[i] == NULL) {
297 dev_err(&idev->dev,
298 "Could not allocate trigger %d\n", i);
299 ret = -ENOMEM;
300 goto error_trigger;
301 }
302 }
303
304 return 0;
305
306error_trigger:
307 for (i--; i >= 0; i--) {
308 iio_trigger_unregister(st->trig[i]);
309 iio_trigger_free(st->trig[i]);
310 }
311error_ret:
312 return ret;
313}
314
315static void at91_adc_trigger_remove(struct iio_dev *idev)
316{
317 struct at91_adc_state *st = iio_priv(idev);
318 int i;
319
320 for (i = 0; i < st->trigger_number; i++) {
321 iio_trigger_unregister(st->trig[i]);
322 iio_trigger_free(st->trig[i]);
323 }
324}
325
Maxime Ripard0e589d52012-05-11 15:35:33 +0200326static int at91_adc_buffer_init(struct iio_dev *idev)
327{
Lars-Peter Clausen90032e42012-06-18 18:33:49 +0200328 return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
329 &at91_adc_trigger_handler, NULL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200330}
331
332static void at91_adc_buffer_remove(struct iio_dev *idev)
333{
Lars-Peter Clausen90032e42012-06-18 18:33:49 +0200334 iio_triggered_buffer_cleanup(idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200335}
336
337static int at91_adc_read_raw(struct iio_dev *idev,
338 struct iio_chan_spec const *chan,
339 int *val, int *val2, long mask)
340{
341 struct at91_adc_state *st = iio_priv(idev);
342 int ret;
343
344 switch (mask) {
345 case IIO_CHAN_INFO_RAW:
346 mutex_lock(&st->lock);
347
348 at91_adc_writel(st, AT91_ADC_CHER,
349 AT91_ADC_CH(chan->channel));
350 at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask);
351 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
352
353 ret = wait_event_interruptible_timeout(st->wq_data_avail,
354 st->done,
355 msecs_to_jiffies(1000));
356 if (ret == 0)
Lars-Peter Clausen90e6dc72012-06-26 10:43:05 +0200357 ret = -ETIMEDOUT;
358 if (ret < 0) {
359 mutex_unlock(&st->lock);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200360 return ret;
Lars-Peter Clausen90e6dc72012-06-26 10:43:05 +0200361 }
Maxime Ripard0e589d52012-05-11 15:35:33 +0200362
363 *val = st->last_value;
364
365 at91_adc_writel(st, AT91_ADC_CHDR,
366 AT91_ADC_CH(chan->channel));
367 at91_adc_writel(st, AT91_ADC_IDR, st->registers->drdy_mask);
368
369 st->last_value = 0;
370 st->done = false;
371 mutex_unlock(&st->lock);
372 return IIO_VAL_INT;
373
374 case IIO_CHAN_INFO_SCALE:
375 *val = (st->vref_mv * 1000) >> chan->scan_type.realbits;
376 *val2 = 0;
377 return IIO_VAL_INT_PLUS_MICRO;
378 default:
379 break;
380 }
381 return -EINVAL;
382}
383
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000384static int at91_adc_of_get_resolution(struct at91_adc_state *st,
385 struct platform_device *pdev)
386{
387 struct iio_dev *idev = iio_priv_to_dev(st);
388 struct device_node *np = pdev->dev.of_node;
389 int count, i, ret = 0;
390 char *res_name, *s;
391 u32 *resolutions;
392
393 count = of_property_count_strings(np, "atmel,adc-res-names");
394 if (count < 2) {
395 dev_err(&idev->dev, "You must specified at least two resolution names for "
396 "adc-res-names property in the DT\n");
397 return count;
398 }
399
400 resolutions = kmalloc(count * sizeof(*resolutions), GFP_KERNEL);
401 if (!resolutions)
402 return -ENOMEM;
403
404 if (of_property_read_u32_array(np, "atmel,adc-res", resolutions, count)) {
405 dev_err(&idev->dev, "Missing adc-res property in the DT.\n");
406 ret = -ENODEV;
407 goto ret;
408 }
409
410 if (of_property_read_string(np, "atmel,adc-use-res", (const char **)&res_name))
411 res_name = "highres";
412
413 for (i = 0; i < count; i++) {
414 if (of_property_read_string_index(np, "atmel,adc-res-names", i, (const char **)&s))
415 continue;
416
417 if (strcmp(res_name, s))
418 continue;
419
420 st->res = resolutions[i];
421 if (!strcmp(res_name, "lowres"))
422 st->low_res = true;
423 else
424 st->low_res = false;
425
426 dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
427 goto ret;
428 }
429
430 dev_err(&idev->dev, "There is no resolution for %s\n", res_name);
431
432ret:
433 kfree(resolutions);
434 return ret;
435}
436
Josh Wue1811f92013-08-27 12:28:00 +0100437static const struct of_device_id at91_adc_dt_ids[];
438
Maxime Riparde3641852012-05-11 15:35:37 +0200439static int at91_adc_probe_dt(struct at91_adc_state *st,
440 struct platform_device *pdev)
441{
442 struct iio_dev *idev = iio_priv_to_dev(st);
443 struct device_node *node = pdev->dev.of_node;
444 struct device_node *trig_node;
445 int i = 0, ret;
446 u32 prop;
447
448 if (!node)
449 return -EINVAL;
450
Josh Wue1811f92013-08-27 12:28:00 +0100451 st->caps = (struct at91_adc_caps *)
452 of_match_device(at91_adc_dt_ids, &pdev->dev)->data;
453
Maxime Riparde3641852012-05-11 15:35:37 +0200454 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
455
456 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
457 dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
458 ret = -EINVAL;
459 goto error_ret;
460 }
461 st->channels_mask = prop;
462
463 if (of_property_read_u32(node, "atmel,adc-num-channels", &prop)) {
464 dev_err(&idev->dev, "Missing adc-num-channels property in the DT.\n");
465 ret = -EINVAL;
466 goto error_ret;
467 }
468 st->num_channels = prop;
469
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000470 st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
471
Maxime Riparde3641852012-05-11 15:35:37 +0200472 if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
473 dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
474 ret = -EINVAL;
475 goto error_ret;
476 }
477 st->startup_time = prop;
478
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +0000479 prop = 0;
480 of_property_read_u32(node, "atmel,adc-sample-hold-time", &prop);
481 st->sample_hold_time = prop;
Maxime Riparde3641852012-05-11 15:35:37 +0200482
483 if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
484 dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
485 ret = -EINVAL;
486 goto error_ret;
487 }
488 st->vref_mv = prop;
489
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000490 ret = at91_adc_of_get_resolution(st, pdev);
491 if (ret)
492 goto error_ret;
493
Josh Wue1811f92013-08-27 12:28:00 +0100494 st->registers = &st->caps->registers;
Maxime Riparde3641852012-05-11 15:35:37 +0200495 st->trigger_number = of_get_child_count(node);
Axel Lin6b3aa312012-10-29 08:25:00 +0000496 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
497 sizeof(struct at91_adc_trigger),
498 GFP_KERNEL);
Maxime Riparde3641852012-05-11 15:35:37 +0200499 if (!st->trigger_list) {
500 dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
501 ret = -ENOMEM;
502 goto error_ret;
503 }
504
505 for_each_child_of_node(node, trig_node) {
506 struct at91_adc_trigger *trig = st->trigger_list + i;
507 const char *name;
508
509 if (of_property_read_string(trig_node, "trigger-name", &name)) {
510 dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
511 ret = -EINVAL;
512 goto error_ret;
513 }
514 trig->name = name;
515
516 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
517 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
518 ret = -EINVAL;
519 goto error_ret;
520 }
521 trig->value = prop;
522 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
523 i++;
524 }
525
526 return 0;
527
528error_ret:
529 return ret;
530}
531
Maxime Ripard0e589d52012-05-11 15:35:33 +0200532static int at91_adc_probe_pdata(struct at91_adc_state *st,
533 struct platform_device *pdev)
534{
535 struct at91_adc_data *pdata = pdev->dev.platform_data;
536
537 if (!pdata)
538 return -EINVAL;
539
540 st->use_external = pdata->use_external_triggers;
541 st->vref_mv = pdata->vref;
542 st->channels_mask = pdata->channels_used;
543 st->num_channels = pdata->num_channels;
544 st->startup_time = pdata->startup_time;
545 st->trigger_number = pdata->trigger_number;
546 st->trigger_list = pdata->trigger_list;
547 st->registers = pdata->registers;
548
549 return 0;
550}
551
552static const struct iio_info at91_adc_info = {
553 .driver_module = THIS_MODULE,
554 .read_raw = &at91_adc_read_raw,
555};
556
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800557static int at91_adc_probe(struct platform_device *pdev)
Maxime Ripard0e589d52012-05-11 15:35:33 +0200558{
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +0000559 unsigned int prsc, mstrclk, ticks, adc_clk, shtim;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200560 int ret;
561 struct iio_dev *idev;
562 struct at91_adc_state *st;
563 struct resource *res;
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000564 u32 reg;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200565
Sachin Kamatf8837532013-07-22 12:02:00 +0100566 idev = devm_iio_device_alloc(&pdev->dev, sizeof(struct at91_adc_state));
567 if (!idev)
568 return -ENOMEM;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200569
570 st = iio_priv(idev);
571
Maxime Riparde3641852012-05-11 15:35:37 +0200572 if (pdev->dev.of_node)
573 ret = at91_adc_probe_dt(st, pdev);
574 else
575 ret = at91_adc_probe_pdata(st, pdev);
576
Maxime Ripard0e589d52012-05-11 15:35:33 +0200577 if (ret) {
578 dev_err(&pdev->dev, "No platform data available.\n");
Sachin Kamatf8837532013-07-22 12:02:00 +0100579 return -EINVAL;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200580 }
581
Maxime Ripard0e589d52012-05-11 15:35:33 +0200582 platform_set_drvdata(pdev, idev);
583
584 idev->dev.parent = &pdev->dev;
585 idev->name = dev_name(&pdev->dev);
586 idev->modes = INDIO_DIRECT_MODE;
587 idev->info = &at91_adc_info;
588
589 st->irq = platform_get_irq(pdev, 0);
590 if (st->irq < 0) {
591 dev_err(&pdev->dev, "No IRQ ID is designated\n");
Sachin Kamatf8837532013-07-22 12:02:00 +0100592 return -ENODEV;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200593 }
594
Julia Lawall390d75c2012-07-31 14:09:00 +0100595 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200596
Thierry Reding5fd98462013-01-21 11:09:04 +0100597 st->reg_base = devm_ioremap_resource(&pdev->dev, res);
598 if (IS_ERR(st->reg_base)) {
Sachin Kamatf8837532013-07-22 12:02:00 +0100599 return PTR_ERR(st->reg_base);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200600 }
601
602 /*
603 * Disable all IRQs before setting up the handler
604 */
605 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
606 at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
607 ret = request_irq(st->irq,
608 at91_adc_eoc_trigger,
609 0,
610 pdev->dev.driver->name,
611 idev);
612 if (ret) {
613 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
Sachin Kamatf8837532013-07-22 12:02:00 +0100614 return ret;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200615 }
616
Julia Lawall390d75c2012-07-31 14:09:00 +0100617 st->clk = devm_clk_get(&pdev->dev, "adc_clk");
Maxime Ripard0e589d52012-05-11 15:35:33 +0200618 if (IS_ERR(st->clk)) {
619 dev_err(&pdev->dev, "Failed to get the clock.\n");
620 ret = PTR_ERR(st->clk);
621 goto error_free_irq;
622 }
623
Julia Lawall00062a9c2012-08-26 17:00:00 +0100624 ret = clk_prepare_enable(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200625 if (ret) {
Julia Lawall00062a9c2012-08-26 17:00:00 +0100626 dev_err(&pdev->dev,
627 "Could not prepare or enable the clock.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100628 goto error_free_irq;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200629 }
630
Julia Lawall390d75c2012-07-31 14:09:00 +0100631 st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
Maxime Ripard0e589d52012-05-11 15:35:33 +0200632 if (IS_ERR(st->adc_clk)) {
633 dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
Julia Lawallf755bbb2012-08-25 21:57:09 +0200634 ret = PTR_ERR(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200635 goto error_disable_clk;
636 }
637
Julia Lawall00062a9c2012-08-26 17:00:00 +0100638 ret = clk_prepare_enable(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200639 if (ret) {
Julia Lawall00062a9c2012-08-26 17:00:00 +0100640 dev_err(&pdev->dev,
641 "Could not prepare or enable the ADC clock.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100642 goto error_disable_clk;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200643 }
644
Maxime Ripard0e589d52012-05-11 15:35:33 +0200645 /*
646 * Prescaler rate computation using the formula from the Atmel's
647 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
648 * specified by the electrical characteristics of the board.
649 */
650 mstrclk = clk_get_rate(st->clk);
651 adc_clk = clk_get_rate(st->adc_clk);
652 prsc = (mstrclk / (2 * adc_clk)) - 1;
653
654 if (!st->startup_time) {
655 dev_err(&pdev->dev, "No startup time available.\n");
656 ret = -EINVAL;
657 goto error_disable_adc_clk;
658 }
659
660 /*
661 * Number of ticks needed to cover the startup time of the ADC as
662 * defined in the electrical characteristics of the board, divided by 8.
663 * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock
664 */
665 ticks = round_up((st->startup_time * adc_clk /
666 1000000) - 1, 8) / 8;
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +0000667 /*
668 * a minimal Sample and Hold Time is necessary for the ADC to guarantee
669 * the best converted final value between two channels selection
670 * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
671 */
672 shtim = round_up((st->sample_hold_time * adc_clk /
673 1000000) - 1, 1);
674
Josh Wu9120c0b2013-08-27 12:28:00 +0100675 reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask;
676 reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask;
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000677 if (st->low_res)
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000678 reg |= AT91_ADC_LOWRES;
679 if (st->sleep_mode)
680 reg |= AT91_ADC_SLEEP;
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +0000681 reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000682 at91_adc_writel(st, AT91_ADC_MR, reg);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200683
684 /* Setup the ADC channels available on the board */
685 ret = at91_adc_channel_init(idev);
686 if (ret < 0) {
687 dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
688 goto error_disable_adc_clk;
689 }
690
691 init_waitqueue_head(&st->wq_data_avail);
692 mutex_init(&st->lock);
693
694 ret = at91_adc_buffer_init(idev);
695 if (ret < 0) {
696 dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
697 goto error_disable_adc_clk;
698 }
699
700 ret = at91_adc_trigger_init(idev);
701 if (ret < 0) {
702 dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
703 goto error_unregister_buffer;
704 }
705
706 ret = iio_device_register(idev);
707 if (ret < 0) {
708 dev_err(&pdev->dev, "Couldn't register the device.\n");
709 goto error_remove_triggers;
710 }
711
712 return 0;
713
714error_remove_triggers:
715 at91_adc_trigger_remove(idev);
716error_unregister_buffer:
717 at91_adc_buffer_remove(idev);
718error_disable_adc_clk:
Julia Lawall00062a9c2012-08-26 17:00:00 +0100719 clk_disable_unprepare(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200720error_disable_clk:
Julia Lawall00062a9c2012-08-26 17:00:00 +0100721 clk_disable_unprepare(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200722error_free_irq:
723 free_irq(st->irq, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200724 return ret;
725}
726
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800727static int at91_adc_remove(struct platform_device *pdev)
Maxime Ripard0e589d52012-05-11 15:35:33 +0200728{
729 struct iio_dev *idev = platform_get_drvdata(pdev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200730 struct at91_adc_state *st = iio_priv(idev);
731
732 iio_device_unregister(idev);
733 at91_adc_trigger_remove(idev);
734 at91_adc_buffer_remove(idev);
735 clk_disable_unprepare(st->adc_clk);
Julia Lawall00062a9c2012-08-26 17:00:00 +0100736 clk_disable_unprepare(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200737 free_irq(st->irq, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200738
739 return 0;
740}
741
Sachin Kamat00738ff2013-05-23 06:26:00 +0100742#ifdef CONFIG_OF
Josh Wue1811f92013-08-27 12:28:00 +0100743static struct at91_adc_caps at91sam9260_caps = {
744 .registers = {
745 .channel_base = AT91_ADC_CHR(0),
746 .drdy_mask = AT91_ADC_DRDY,
747 .status_register = AT91_ADC_SR,
748 .trigger_register = AT91_ADC_TRGR_9260,
Josh Wu9120c0b2013-08-27 12:28:00 +0100749 .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
750 .mr_startup_mask = AT91_ADC_STARTUP_9260,
Josh Wue1811f92013-08-27 12:28:00 +0100751 },
752};
753
754static struct at91_adc_caps at91sam9g45_caps = {
755 .registers = {
756 .channel_base = AT91_ADC_CHR(0),
757 .drdy_mask = AT91_ADC_DRDY,
758 .status_register = AT91_ADC_SR,
759 .trigger_register = AT91_ADC_TRGR_9G45,
Josh Wu9120c0b2013-08-27 12:28:00 +0100760 .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
761 .mr_startup_mask = AT91_ADC_STARTUP_9G45,
Josh Wue1811f92013-08-27 12:28:00 +0100762 },
763};
764
765static struct at91_adc_caps at91sam9x5_caps = {
766 .registers = {
767 .channel_base = AT91_ADC_CDR0_9X5,
768 .drdy_mask = AT91_ADC_SR_DRDY_9X5,
769 .status_register = AT91_ADC_SR_9X5,
770 .trigger_register = AT91_ADC_TRGR_9X5,
Josh Wu9120c0b2013-08-27 12:28:00 +0100771 /* prescal mask is same as 9G45 */
772 .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
773 .mr_startup_mask = AT91_ADC_STARTUP_9X5,
Josh Wue1811f92013-08-27 12:28:00 +0100774 },
775};
776
Maxime Riparde3641852012-05-11 15:35:37 +0200777static const struct of_device_id at91_adc_dt_ids[] = {
Josh Wue1811f92013-08-27 12:28:00 +0100778 { .compatible = "atmel,at91sam9260-adc", .data = &at91sam9260_caps },
779 { .compatible = "atmel,at91sam9g45-adc", .data = &at91sam9g45_caps },
780 { .compatible = "atmel,at91sam9x5-adc", .data = &at91sam9x5_caps },
Maxime Riparde3641852012-05-11 15:35:37 +0200781 {},
782};
783MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
Sachin Kamat00738ff2013-05-23 06:26:00 +0100784#endif
Maxime Riparde3641852012-05-11 15:35:37 +0200785
Maxime Ripard0e589d52012-05-11 15:35:33 +0200786static struct platform_driver at91_adc_driver = {
787 .probe = at91_adc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800788 .remove = at91_adc_remove,
Maxime Ripard0e589d52012-05-11 15:35:33 +0200789 .driver = {
790 .name = "at91_adc",
Maxime Riparde3641852012-05-11 15:35:37 +0200791 .of_match_table = of_match_ptr(at91_adc_dt_ids),
Maxime Ripard0e589d52012-05-11 15:35:33 +0200792 },
793};
794
795module_platform_driver(at91_adc_driver);
796
797MODULE_LICENSE("GPL");
798MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
799MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");