]>
Commit | Line | Data |
---|---|---|
0e589d5f MR |
1 | /* |
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> | |
e364185f MR |
18 | #include <linux/of.h> |
19 | #include <linux/of_device.h> | |
0e589d5f MR |
20 | #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> | |
0e589d5f MR |
29 | #include <linux/iio/trigger.h> |
30 | #include <linux/iio/trigger_consumer.h> | |
90032e4e | 31 | #include <linux/iio/triggered_buffer.h> |
0e589d5f MR |
32 | |
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 | ||
42 | struct 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; | |
49 | bool irq_enabled; | |
50 | u16 last_value; | |
51 | struct mutex lock; | |
52 | u8 num_channels; | |
53 | void __iomem *reg_base; | |
54 | struct at91_adc_reg_desc *registers; | |
55 | u8 startup_time; | |
56 | struct iio_trigger **trig; | |
57 | struct at91_adc_trigger *trigger_list; | |
58 | u32 trigger_number; | |
59 | bool use_external; | |
60 | u32 vref_mv; | |
61 | wait_queue_head_t wq_data_avail; | |
62 | }; | |
63 | ||
64 | static irqreturn_t at91_adc_trigger_handler(int irq, void *p) | |
65 | { | |
66 | struct iio_poll_func *pf = p; | |
67 | struct iio_dev *idev = pf->indio_dev; | |
68 | struct at91_adc_state *st = iio_priv(idev); | |
69 | struct iio_buffer *buffer = idev->buffer; | |
70 | 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 | ||
85 | buffer->access->store_to(buffer, (u8 *)st->buffer, pf->timestamp); | |
86 | ||
87 | iio_trigger_notify_done(idev->trig); | |
88 | st->irq_enabled = true; | |
89 | ||
90 | /* Needed to ACK the DRDY interruption */ | |
91 | at91_adc_readl(st, AT91_ADC_LCDR); | |
92 | ||
93 | enable_irq(st->irq); | |
94 | ||
95 | return IRQ_HANDLED; | |
96 | } | |
97 | ||
98 | static irqreturn_t at91_adc_eoc_trigger(int irq, void *private) | |
99 | { | |
100 | struct iio_dev *idev = private; | |
101 | struct at91_adc_state *st = iio_priv(idev); | |
102 | u32 status = at91_adc_readl(st, st->registers->status_register); | |
103 | ||
104 | if (!(status & st->registers->drdy_mask)) | |
105 | return IRQ_HANDLED; | |
106 | ||
107 | if (iio_buffer_enabled(idev)) { | |
108 | disable_irq_nosync(irq); | |
109 | st->irq_enabled = false; | |
110 | iio_trigger_poll(idev->trig, iio_get_time_ns()); | |
111 | } else { | |
112 | st->last_value = at91_adc_readl(st, AT91_ADC_LCDR); | |
113 | st->done = true; | |
114 | wake_up_interruptible(&st->wq_data_avail); | |
115 | } | |
116 | ||
117 | return IRQ_HANDLED; | |
118 | } | |
119 | ||
120 | static int at91_adc_channel_init(struct iio_dev *idev) | |
121 | { | |
122 | struct at91_adc_state *st = iio_priv(idev); | |
123 | struct iio_chan_spec *chan_array, *timestamp; | |
124 | int bit, idx = 0; | |
125 | ||
126 | idev->num_channels = bitmap_weight(&st->channels_mask, | |
127 | st->num_channels) + 1; | |
128 | ||
129 | chan_array = devm_kzalloc(&idev->dev, | |
130 | ((idev->num_channels + 1) * | |
131 | sizeof(struct iio_chan_spec)), | |
132 | GFP_KERNEL); | |
133 | ||
134 | if (!chan_array) | |
135 | return -ENOMEM; | |
136 | ||
137 | for_each_set_bit(bit, &st->channels_mask, st->num_channels) { | |
138 | struct iio_chan_spec *chan = chan_array + idx; | |
139 | ||
140 | chan->type = IIO_VOLTAGE; | |
141 | chan->indexed = 1; | |
142 | chan->channel = bit; | |
143 | chan->scan_index = idx; | |
144 | chan->scan_type.sign = 'u'; | |
145 | chan->scan_type.realbits = 10; | |
146 | chan->scan_type.storagebits = 16; | |
147 | chan->info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT | | |
148 | IIO_CHAN_INFO_RAW_SEPARATE_BIT; | |
149 | idx++; | |
150 | } | |
151 | timestamp = chan_array + idx; | |
152 | ||
153 | timestamp->type = IIO_TIMESTAMP; | |
154 | timestamp->channel = -1; | |
155 | timestamp->scan_index = idx; | |
156 | timestamp->scan_type.sign = 's'; | |
157 | timestamp->scan_type.realbits = 64; | |
158 | timestamp->scan_type.storagebits = 64; | |
159 | ||
160 | idev->channels = chan_array; | |
161 | return idev->num_channels; | |
162 | } | |
163 | ||
164 | static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev, | |
165 | struct at91_adc_trigger *triggers, | |
166 | const char *trigger_name) | |
167 | { | |
168 | struct at91_adc_state *st = iio_priv(idev); | |
169 | u8 value = 0; | |
170 | int i; | |
171 | ||
172 | for (i = 0; i < st->trigger_number; i++) { | |
173 | char *name = kasprintf(GFP_KERNEL, | |
174 | "%s-dev%d-%s", | |
175 | idev->name, | |
176 | idev->id, | |
177 | triggers[i].name); | |
178 | if (!name) | |
179 | return -ENOMEM; | |
180 | ||
181 | if (strcmp(trigger_name, name) == 0) { | |
182 | value = triggers[i].value; | |
183 | kfree(name); | |
184 | break; | |
185 | } | |
186 | ||
187 | kfree(name); | |
188 | } | |
189 | ||
190 | return value; | |
191 | } | |
192 | ||
193 | static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state) | |
194 | { | |
195 | struct iio_dev *idev = trig->private_data; | |
196 | struct at91_adc_state *st = iio_priv(idev); | |
197 | struct iio_buffer *buffer = idev->buffer; | |
198 | struct at91_adc_reg_desc *reg = st->registers; | |
199 | u32 status = at91_adc_readl(st, reg->trigger_register); | |
200 | u8 value; | |
201 | u8 bit; | |
202 | ||
203 | value = at91_adc_get_trigger_value_by_name(idev, | |
204 | st->trigger_list, | |
205 | idev->trig->name); | |
206 | if (value == 0) | |
207 | return -EINVAL; | |
208 | ||
209 | if (state) { | |
210 | st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL); | |
211 | if (st->buffer == NULL) | |
212 | return -ENOMEM; | |
213 | ||
214 | at91_adc_writel(st, reg->trigger_register, | |
215 | status | value); | |
216 | ||
217 | for_each_set_bit(bit, buffer->scan_mask, | |
218 | st->num_channels) { | |
219 | struct iio_chan_spec const *chan = idev->channels + bit; | |
220 | at91_adc_writel(st, AT91_ADC_CHER, | |
221 | AT91_ADC_CH(chan->channel)); | |
222 | } | |
223 | ||
224 | at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask); | |
225 | ||
226 | } else { | |
227 | at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask); | |
228 | ||
229 | at91_adc_writel(st, reg->trigger_register, | |
230 | status & ~value); | |
231 | ||
232 | for_each_set_bit(bit, buffer->scan_mask, | |
233 | st->num_channels) { | |
234 | struct iio_chan_spec const *chan = idev->channels + bit; | |
235 | at91_adc_writel(st, AT91_ADC_CHDR, | |
236 | AT91_ADC_CH(chan->channel)); | |
237 | } | |
238 | kfree(st->buffer); | |
239 | } | |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
244 | static const struct iio_trigger_ops at91_adc_trigger_ops = { | |
245 | .owner = THIS_MODULE, | |
246 | .set_trigger_state = &at91_adc_configure_trigger, | |
247 | }; | |
248 | ||
249 | static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev, | |
250 | struct at91_adc_trigger *trigger) | |
251 | { | |
252 | struct iio_trigger *trig; | |
253 | int ret; | |
254 | ||
255 | trig = iio_trigger_alloc("%s-dev%d-%s", idev->name, | |
256 | idev->id, trigger->name); | |
257 | if (trig == NULL) | |
258 | return NULL; | |
259 | ||
260 | trig->dev.parent = idev->dev.parent; | |
261 | trig->private_data = idev; | |
262 | trig->ops = &at91_adc_trigger_ops; | |
263 | ||
264 | ret = iio_trigger_register(trig); | |
265 | if (ret) | |
266 | return NULL; | |
267 | ||
268 | return trig; | |
269 | } | |
270 | ||
271 | static int at91_adc_trigger_init(struct iio_dev *idev) | |
272 | { | |
273 | struct at91_adc_state *st = iio_priv(idev); | |
274 | int i, ret; | |
275 | ||
276 | st->trig = devm_kzalloc(&idev->dev, | |
277 | st->trigger_number * sizeof(st->trig), | |
278 | GFP_KERNEL); | |
279 | ||
280 | if (st->trig == NULL) { | |
281 | ret = -ENOMEM; | |
282 | goto error_ret; | |
283 | } | |
284 | ||
285 | for (i = 0; i < st->trigger_number; i++) { | |
286 | if (st->trigger_list[i].is_external && !(st->use_external)) | |
287 | continue; | |
288 | ||
289 | st->trig[i] = at91_adc_allocate_trigger(idev, | |
290 | st->trigger_list + i); | |
291 | if (st->trig[i] == NULL) { | |
292 | dev_err(&idev->dev, | |
293 | "Could not allocate trigger %d\n", i); | |
294 | ret = -ENOMEM; | |
295 | goto error_trigger; | |
296 | } | |
297 | } | |
298 | ||
299 | return 0; | |
300 | ||
301 | error_trigger: | |
302 | for (i--; i >= 0; i--) { | |
303 | iio_trigger_unregister(st->trig[i]); | |
304 | iio_trigger_free(st->trig[i]); | |
305 | } | |
306 | error_ret: | |
307 | return ret; | |
308 | } | |
309 | ||
310 | static void at91_adc_trigger_remove(struct iio_dev *idev) | |
311 | { | |
312 | struct at91_adc_state *st = iio_priv(idev); | |
313 | int i; | |
314 | ||
315 | for (i = 0; i < st->trigger_number; i++) { | |
316 | iio_trigger_unregister(st->trig[i]); | |
317 | iio_trigger_free(st->trig[i]); | |
318 | } | |
319 | } | |
320 | ||
0e589d5f MR |
321 | static int at91_adc_buffer_init(struct iio_dev *idev) |
322 | { | |
90032e4e LPC |
323 | return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time, |
324 | &at91_adc_trigger_handler, NULL); | |
0e589d5f MR |
325 | } |
326 | ||
327 | static void at91_adc_buffer_remove(struct iio_dev *idev) | |
328 | { | |
90032e4e | 329 | iio_triggered_buffer_cleanup(idev); |
0e589d5f MR |
330 | } |
331 | ||
332 | static int at91_adc_read_raw(struct iio_dev *idev, | |
333 | struct iio_chan_spec const *chan, | |
334 | int *val, int *val2, long mask) | |
335 | { | |
336 | struct at91_adc_state *st = iio_priv(idev); | |
337 | int ret; | |
338 | ||
339 | switch (mask) { | |
340 | case IIO_CHAN_INFO_RAW: | |
341 | mutex_lock(&st->lock); | |
342 | ||
343 | at91_adc_writel(st, AT91_ADC_CHER, | |
344 | AT91_ADC_CH(chan->channel)); | |
345 | at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask); | |
346 | at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START); | |
347 | ||
348 | ret = wait_event_interruptible_timeout(st->wq_data_avail, | |
349 | st->done, | |
350 | msecs_to_jiffies(1000)); | |
351 | if (ret == 0) | |
352 | return -ETIMEDOUT; | |
353 | else if (ret < 0) | |
354 | return ret; | |
355 | ||
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 | ||
e364185f MR |
377 | static int at91_adc_probe_dt(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 *node = pdev->dev.of_node; | |
382 | struct device_node *trig_node; | |
383 | int i = 0, ret; | |
384 | u32 prop; | |
385 | ||
386 | if (!node) | |
387 | return -EINVAL; | |
388 | ||
389 | st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers"); | |
390 | ||
391 | if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) { | |
392 | dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n"); | |
393 | ret = -EINVAL; | |
394 | goto error_ret; | |
395 | } | |
396 | st->channels_mask = prop; | |
397 | ||
398 | if (of_property_read_u32(node, "atmel,adc-num-channels", &prop)) { | |
399 | dev_err(&idev->dev, "Missing adc-num-channels property in the DT.\n"); | |
400 | ret = -EINVAL; | |
401 | goto error_ret; | |
402 | } | |
403 | st->num_channels = prop; | |
404 | ||
405 | if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) { | |
406 | dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n"); | |
407 | ret = -EINVAL; | |
408 | goto error_ret; | |
409 | } | |
410 | st->startup_time = prop; | |
411 | ||
412 | ||
413 | if (of_property_read_u32(node, "atmel,adc-vref", &prop)) { | |
414 | dev_err(&idev->dev, "Missing adc-vref property in the DT.\n"); | |
415 | ret = -EINVAL; | |
416 | goto error_ret; | |
417 | } | |
418 | st->vref_mv = prop; | |
419 | ||
420 | st->registers = devm_kzalloc(&idev->dev, | |
421 | sizeof(struct at91_adc_reg_desc), | |
422 | GFP_KERNEL); | |
423 | if (!st->registers) { | |
424 | dev_err(&idev->dev, "Could not allocate register memory.\n"); | |
425 | ret = -ENOMEM; | |
426 | goto error_ret; | |
427 | } | |
428 | ||
429 | if (of_property_read_u32(node, "atmel,adc-channel-base", &prop)) { | |
430 | dev_err(&idev->dev, "Missing adc-channel-base property in the DT.\n"); | |
431 | ret = -EINVAL; | |
432 | goto error_ret; | |
433 | } | |
434 | st->registers->channel_base = prop; | |
435 | ||
436 | if (of_property_read_u32(node, "atmel,adc-drdy-mask", &prop)) { | |
437 | dev_err(&idev->dev, "Missing adc-drdy-mask property in the DT.\n"); | |
438 | ret = -EINVAL; | |
439 | goto error_ret; | |
440 | } | |
441 | st->registers->drdy_mask = prop; | |
442 | ||
443 | if (of_property_read_u32(node, "atmel,adc-status-register", &prop)) { | |
444 | dev_err(&idev->dev, "Missing adc-status-register property in the DT.\n"); | |
445 | ret = -EINVAL; | |
446 | goto error_ret; | |
447 | } | |
448 | st->registers->status_register = prop; | |
449 | ||
450 | if (of_property_read_u32(node, "atmel,adc-trigger-register", &prop)) { | |
451 | dev_err(&idev->dev, "Missing adc-trigger-register property in the DT.\n"); | |
452 | ret = -EINVAL; | |
453 | goto error_ret; | |
454 | } | |
455 | st->registers->trigger_register = prop; | |
456 | ||
457 | st->trigger_number = of_get_child_count(node); | |
458 | st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number * | |
459 | sizeof(struct at91_adc_trigger), | |
460 | GFP_KERNEL); | |
461 | if (!st->trigger_list) { | |
462 | dev_err(&idev->dev, "Could not allocate trigger list memory.\n"); | |
463 | ret = -ENOMEM; | |
464 | goto error_ret; | |
465 | } | |
466 | ||
467 | for_each_child_of_node(node, trig_node) { | |
468 | struct at91_adc_trigger *trig = st->trigger_list + i; | |
469 | const char *name; | |
470 | ||
471 | if (of_property_read_string(trig_node, "trigger-name", &name)) { | |
472 | dev_err(&idev->dev, "Missing trigger-name property in the DT.\n"); | |
473 | ret = -EINVAL; | |
474 | goto error_ret; | |
475 | } | |
476 | trig->name = name; | |
477 | ||
478 | if (of_property_read_u32(trig_node, "trigger-value", &prop)) { | |
479 | dev_err(&idev->dev, "Missing trigger-value property in the DT.\n"); | |
480 | ret = -EINVAL; | |
481 | goto error_ret; | |
482 | } | |
483 | trig->value = prop; | |
484 | trig->is_external = of_property_read_bool(trig_node, "trigger-external"); | |
485 | i++; | |
486 | } | |
487 | ||
488 | return 0; | |
489 | ||
490 | error_ret: | |
491 | return ret; | |
492 | } | |
493 | ||
0e589d5f MR |
494 | static int at91_adc_probe_pdata(struct at91_adc_state *st, |
495 | struct platform_device *pdev) | |
496 | { | |
497 | struct at91_adc_data *pdata = pdev->dev.platform_data; | |
498 | ||
499 | if (!pdata) | |
500 | return -EINVAL; | |
501 | ||
502 | st->use_external = pdata->use_external_triggers; | |
503 | st->vref_mv = pdata->vref; | |
504 | st->channels_mask = pdata->channels_used; | |
505 | st->num_channels = pdata->num_channels; | |
506 | st->startup_time = pdata->startup_time; | |
507 | st->trigger_number = pdata->trigger_number; | |
508 | st->trigger_list = pdata->trigger_list; | |
509 | st->registers = pdata->registers; | |
510 | ||
511 | return 0; | |
512 | } | |
513 | ||
514 | static const struct iio_info at91_adc_info = { | |
515 | .driver_module = THIS_MODULE, | |
516 | .read_raw = &at91_adc_read_raw, | |
517 | }; | |
518 | ||
519 | static int __devinit at91_adc_probe(struct platform_device *pdev) | |
520 | { | |
521 | unsigned int prsc, mstrclk, ticks, adc_clk; | |
522 | int ret; | |
523 | struct iio_dev *idev; | |
524 | struct at91_adc_state *st; | |
525 | struct resource *res; | |
526 | ||
527 | idev = iio_device_alloc(sizeof(struct at91_adc_state)); | |
528 | if (idev == NULL) { | |
529 | ret = -ENOMEM; | |
530 | goto error_ret; | |
531 | } | |
532 | ||
533 | st = iio_priv(idev); | |
534 | ||
e364185f MR |
535 | if (pdev->dev.of_node) |
536 | ret = at91_adc_probe_dt(st, pdev); | |
537 | else | |
538 | ret = at91_adc_probe_pdata(st, pdev); | |
539 | ||
0e589d5f MR |
540 | if (ret) { |
541 | dev_err(&pdev->dev, "No platform data available.\n"); | |
542 | ret = -EINVAL; | |
543 | goto error_free_device; | |
544 | } | |
545 | ||
546 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
547 | if (!res) { | |
548 | dev_err(&pdev->dev, "No resource defined\n"); | |
549 | ret = -ENXIO; | |
550 | goto error_ret; | |
551 | } | |
552 | ||
553 | platform_set_drvdata(pdev, idev); | |
554 | ||
555 | idev->dev.parent = &pdev->dev; | |
556 | idev->name = dev_name(&pdev->dev); | |
557 | idev->modes = INDIO_DIRECT_MODE; | |
558 | idev->info = &at91_adc_info; | |
559 | ||
560 | st->irq = platform_get_irq(pdev, 0); | |
561 | if (st->irq < 0) { | |
562 | dev_err(&pdev->dev, "No IRQ ID is designated\n"); | |
563 | ret = -ENODEV; | |
564 | goto error_free_device; | |
565 | } | |
566 | ||
567 | if (!request_mem_region(res->start, resource_size(res), | |
568 | "AT91 adc registers")) { | |
569 | dev_err(&pdev->dev, "Resources are unavailable.\n"); | |
570 | ret = -EBUSY; | |
571 | goto error_free_device; | |
572 | } | |
573 | ||
574 | st->reg_base = ioremap(res->start, resource_size(res)); | |
575 | if (!st->reg_base) { | |
576 | dev_err(&pdev->dev, "Failed to map registers.\n"); | |
577 | ret = -ENOMEM; | |
578 | goto error_release_mem; | |
579 | } | |
580 | ||
581 | /* | |
582 | * Disable all IRQs before setting up the handler | |
583 | */ | |
584 | at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST); | |
585 | at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF); | |
586 | ret = request_irq(st->irq, | |
587 | at91_adc_eoc_trigger, | |
588 | 0, | |
589 | pdev->dev.driver->name, | |
590 | idev); | |
591 | if (ret) { | |
592 | dev_err(&pdev->dev, "Failed to allocate IRQ.\n"); | |
593 | goto error_unmap_reg; | |
594 | } | |
595 | ||
596 | st->clk = clk_get(&pdev->dev, "adc_clk"); | |
597 | if (IS_ERR(st->clk)) { | |
598 | dev_err(&pdev->dev, "Failed to get the clock.\n"); | |
599 | ret = PTR_ERR(st->clk); | |
600 | goto error_free_irq; | |
601 | } | |
602 | ||
603 | ret = clk_prepare(st->clk); | |
604 | if (ret) { | |
605 | dev_err(&pdev->dev, "Could not prepare the clock.\n"); | |
606 | goto error_free_clk; | |
607 | } | |
608 | ||
609 | ret = clk_enable(st->clk); | |
610 | if (ret) { | |
611 | dev_err(&pdev->dev, "Could not enable the clock.\n"); | |
612 | goto error_unprepare_clk; | |
613 | } | |
614 | ||
615 | st->adc_clk = clk_get(&pdev->dev, "adc_op_clk"); | |
616 | if (IS_ERR(st->adc_clk)) { | |
617 | dev_err(&pdev->dev, "Failed to get the ADC clock.\n"); | |
618 | ret = PTR_ERR(st->clk); | |
619 | goto error_disable_clk; | |
620 | } | |
621 | ||
622 | ret = clk_prepare(st->adc_clk); | |
623 | if (ret) { | |
624 | dev_err(&pdev->dev, "Could not prepare the ADC clock.\n"); | |
625 | goto error_free_adc_clk; | |
626 | } | |
627 | ||
628 | ret = clk_enable(st->adc_clk); | |
629 | if (ret) { | |
630 | dev_err(&pdev->dev, "Could not enable the ADC clock.\n"); | |
631 | goto error_unprepare_adc_clk; | |
632 | } | |
633 | ||
634 | /* | |
635 | * Prescaler rate computation using the formula from the Atmel's | |
636 | * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being | |
637 | * specified by the electrical characteristics of the board. | |
638 | */ | |
639 | mstrclk = clk_get_rate(st->clk); | |
640 | adc_clk = clk_get_rate(st->adc_clk); | |
641 | prsc = (mstrclk / (2 * adc_clk)) - 1; | |
642 | ||
643 | if (!st->startup_time) { | |
644 | dev_err(&pdev->dev, "No startup time available.\n"); | |
645 | ret = -EINVAL; | |
646 | goto error_disable_adc_clk; | |
647 | } | |
648 | ||
649 | /* | |
650 | * Number of ticks needed to cover the startup time of the ADC as | |
651 | * defined in the electrical characteristics of the board, divided by 8. | |
652 | * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock | |
653 | */ | |
654 | ticks = round_up((st->startup_time * adc_clk / | |
655 | 1000000) - 1, 8) / 8; | |
656 | at91_adc_writel(st, AT91_ADC_MR, | |
657 | (AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL) | | |
658 | (AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP)); | |
659 | ||
660 | /* Setup the ADC channels available on the board */ | |
661 | ret = at91_adc_channel_init(idev); | |
662 | if (ret < 0) { | |
663 | dev_err(&pdev->dev, "Couldn't initialize the channels.\n"); | |
664 | goto error_disable_adc_clk; | |
665 | } | |
666 | ||
667 | init_waitqueue_head(&st->wq_data_avail); | |
668 | mutex_init(&st->lock); | |
669 | ||
670 | ret = at91_adc_buffer_init(idev); | |
671 | if (ret < 0) { | |
672 | dev_err(&pdev->dev, "Couldn't initialize the buffer.\n"); | |
673 | goto error_disable_adc_clk; | |
674 | } | |
675 | ||
676 | ret = at91_adc_trigger_init(idev); | |
677 | if (ret < 0) { | |
678 | dev_err(&pdev->dev, "Couldn't setup the triggers.\n"); | |
679 | goto error_unregister_buffer; | |
680 | } | |
681 | ||
682 | ret = iio_device_register(idev); | |
683 | if (ret < 0) { | |
684 | dev_err(&pdev->dev, "Couldn't register the device.\n"); | |
685 | goto error_remove_triggers; | |
686 | } | |
687 | ||
688 | return 0; | |
689 | ||
690 | error_remove_triggers: | |
691 | at91_adc_trigger_remove(idev); | |
692 | error_unregister_buffer: | |
693 | at91_adc_buffer_remove(idev); | |
694 | error_disable_adc_clk: | |
695 | clk_disable(st->adc_clk); | |
696 | error_unprepare_adc_clk: | |
697 | clk_unprepare(st->adc_clk); | |
698 | error_free_adc_clk: | |
699 | clk_put(st->adc_clk); | |
700 | error_disable_clk: | |
701 | clk_disable(st->clk); | |
702 | error_unprepare_clk: | |
703 | clk_unprepare(st->clk); | |
704 | error_free_clk: | |
705 | clk_put(st->clk); | |
706 | error_free_irq: | |
707 | free_irq(st->irq, idev); | |
708 | error_unmap_reg: | |
709 | iounmap(st->reg_base); | |
710 | error_release_mem: | |
711 | release_mem_region(res->start, resource_size(res)); | |
712 | error_free_device: | |
713 | iio_device_free(idev); | |
714 | error_ret: | |
715 | return ret; | |
716 | } | |
717 | ||
718 | static int __devexit at91_adc_remove(struct platform_device *pdev) | |
719 | { | |
720 | struct iio_dev *idev = platform_get_drvdata(pdev); | |
721 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
722 | struct at91_adc_state *st = iio_priv(idev); | |
723 | ||
724 | iio_device_unregister(idev); | |
725 | at91_adc_trigger_remove(idev); | |
726 | at91_adc_buffer_remove(idev); | |
727 | clk_disable_unprepare(st->adc_clk); | |
728 | clk_put(st->adc_clk); | |
729 | clk_disable(st->clk); | |
730 | clk_unprepare(st->clk); | |
731 | clk_put(st->clk); | |
732 | free_irq(st->irq, idev); | |
733 | iounmap(st->reg_base); | |
734 | release_mem_region(res->start, resource_size(res)); | |
735 | iio_device_free(idev); | |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
e364185f MR |
740 | static const struct of_device_id at91_adc_dt_ids[] = { |
741 | { .compatible = "atmel,at91sam9260-adc" }, | |
742 | {}, | |
743 | }; | |
744 | MODULE_DEVICE_TABLE(of, at91_adc_dt_ids); | |
745 | ||
0e589d5f MR |
746 | static struct platform_driver at91_adc_driver = { |
747 | .probe = at91_adc_probe, | |
748 | .remove = __devexit_p(at91_adc_remove), | |
749 | .driver = { | |
750 | .name = "at91_adc", | |
e364185f | 751 | .of_match_table = of_match_ptr(at91_adc_dt_ids), |
0e589d5f MR |
752 | }, |
753 | }; | |
754 | ||
755 | module_platform_driver(at91_adc_driver); | |
756 | ||
757 | MODULE_LICENSE("GPL"); | |
758 | MODULE_DESCRIPTION("Atmel AT91 ADC Driver"); | |
759 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |