2 * Driver for the ADC present in the Atmel AT91 evaluation boards.
4 * Copyright 2011 Free Electrons
6 * Licensed under the GPLv2 or later.
9 #include <linux/bitmap.h>
10 #include <linux/bitops.h>
11 #include <linux/clk.h>
12 #include <linux/err.h>
14 #include <linux/input.h>
15 #include <linux/interrupt.h>
16 #include <linux/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/wait.h>
26 #include <linux/platform_data/at91_adc.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger.h>
31 #include <linux/iio/trigger_consumer.h>
32 #include <linux/iio/triggered_buffer.h>
34 #include <mach/at91_adc.h>
36 #define AT91_ADC_CHAN(st, ch) \
37 (st->registers->channel_base + (ch * 4))
38 #define at91_adc_readl(st, reg) \
39 (readl_relaxed(st->reg_base + reg))
40 #define at91_adc_writel(st, reg, val) \
41 (writel_relaxed(val, st->reg_base + reg))
43 #define DRIVER_NAME "at91_adc"
44 #define MAX_POS_BITS 12
46 #define TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */
47 #define TOUCH_PEN_DETECT_DEBOUNCE_US 200
49 #define MAX_RLPOS_BITS 10
50 #define TOUCH_SAMPLE_PERIOD_US_RL 10000 /* 10ms, the SoC can't keep up with 2ms */
51 #define TOUCH_SHTIM 0xa
54 * struct at91_adc_reg_desc - Various informations relative to registers
55 * @channel_base: Base offset for the channel data registers
56 * @drdy_mask: Mask of the DRDY field in the relevant registers
57 (Interruptions registers mostly)
58 * @status_register: Offset of the Interrupt Status Register
59 * @trigger_register: Offset of the Trigger setup register
60 * @mr_prescal_mask: Mask of the PRESCAL field in the adc MR register
61 * @mr_startup_mask: Mask of the STARTUP field in the adc MR register
63 struct at91_adc_reg_desc {
72 struct at91_adc_caps {
73 bool has_ts; /* Support touch screen */
74 bool has_tsmr; /* only at91sam9x5, sama5d3 have TSMR reg */
76 * Numbers of sampling data will be averaged. Can be 0~3.
77 * Hardware can average (2 ^ ts_filter_average) sample data.
80 /* Pen Detection input pull-up resistor, can be 0~3 */
81 u8 ts_pen_detect_sensitivity;
83 /* startup time calculate function */
84 u32 (*calc_startup_ticks)(u8 startup_time, u32 adc_clk_khz);
87 struct at91_adc_reg_desc registers;
90 struct at91_adc_state {
93 unsigned long channels_mask;
100 void __iomem *reg_base;
101 struct at91_adc_reg_desc *registers;
105 struct iio_trigger **trig;
106 struct at91_adc_trigger *trigger_list;
110 u32 res; /* resolution used for convertions */
111 bool low_res; /* the resolution corresponds to the lowest one */
112 wait_queue_head_t wq_data_avail;
113 struct at91_adc_caps *caps;
116 * Following ADC channels are shared by touchscreen:
118 * CH0 -- Touch screen XP/UL
119 * CH1 -- Touch screen XM/UR
120 * CH2 -- Touch screen YP/LL
121 * CH3 -- Touch screen YM/Sense
122 * CH4 -- Touch screen LR(5-wire only)
124 * The bitfields below represents the reserved channel in the
127 #define CHAN_MASK_TOUCHSCREEN_4WIRE (0xf << 0)
128 #define CHAN_MASK_TOUCHSCREEN_5WIRE (0x1f << 0)
129 enum atmel_adc_ts_type touchscreen_type;
130 struct input_dev *ts_input;
132 u16 ts_sample_period_val;
133 u32 ts_pressure_threshold;
136 bool ts_bufferedmeasure;
141 static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
143 struct iio_poll_func *pf = p;
144 struct iio_dev *idev = pf->indio_dev;
145 struct at91_adc_state *st = iio_priv(idev);
148 for (i = 0; i < idev->masklength; i++) {
149 if (!test_bit(i, idev->active_scan_mask))
151 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
155 iio_push_to_buffers_with_timestamp(idev, st->buffer, pf->timestamp);
157 iio_trigger_notify_done(idev->trig);
159 /* Needed to ACK the DRDY interruption */
160 at91_adc_readl(st, AT91_ADC_LCDR);
167 /* Handler for classic adc channel eoc trigger */
168 void handle_adc_eoc_trigger(int irq, struct iio_dev *idev)
170 struct at91_adc_state *st = iio_priv(idev);
172 if (iio_buffer_enabled(idev)) {
173 disable_irq_nosync(irq);
174 iio_trigger_poll(idev->trig, iio_get_time_ns());
176 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
178 wake_up_interruptible(&st->wq_data_avail);
182 static int at91_ts_sample(struct at91_adc_state *st)
184 unsigned int xscale, yscale, reg, z1, z2;
185 unsigned int x, y, pres, xpos, ypos;
186 unsigned int rxp = 1;
187 unsigned int factor = 1000;
188 struct iio_dev *idev = iio_priv_to_dev(st);
190 unsigned int xyz_mask_bits = st->res;
191 unsigned int xyz_mask = (1 << xyz_mask_bits) - 1;
193 /* calculate position */
194 /* x position = (x / xscale) * max, max = 2^MAX_POS_BITS - 1 */
195 reg = at91_adc_readl(st, AT91_ADC_TSXPOSR);
196 xpos = reg & xyz_mask;
197 x = (xpos << MAX_POS_BITS) - xpos;
198 xscale = (reg >> 16) & xyz_mask;
200 dev_err(&idev->dev, "Error: xscale == 0!\n");
205 /* y position = (y / yscale) * max, max = 2^MAX_POS_BITS - 1 */
206 reg = at91_adc_readl(st, AT91_ADC_TSYPOSR);
207 ypos = reg & xyz_mask;
208 y = (ypos << MAX_POS_BITS) - ypos;
209 yscale = (reg >> 16) & xyz_mask;
211 dev_err(&idev->dev, "Error: yscale == 0!\n");
216 /* calculate the pressure */
217 reg = at91_adc_readl(st, AT91_ADC_TSPRESSR);
219 z2 = (reg >> 16) & xyz_mask;
222 pres = rxp * (x * factor / 1024) * (z2 * factor / z1 - factor)
225 pres = st->ts_pressure_threshold; /* no pen contacted */
227 dev_dbg(&idev->dev, "xpos = %d, xscale = %d, ypos = %d, yscale = %d, z1 = %d, z2 = %d, press = %d\n",
228 xpos, xscale, ypos, yscale, z1, z2, pres);
230 if (pres < st->ts_pressure_threshold) {
231 dev_dbg(&idev->dev, "x = %d, y = %d, pressure = %d\n",
232 x, y, pres / factor);
233 input_report_abs(st->ts_input, ABS_X, x);
234 input_report_abs(st->ts_input, ABS_Y, y);
235 input_report_abs(st->ts_input, ABS_PRESSURE, pres);
236 input_report_key(st->ts_input, BTN_TOUCH, 1);
237 input_sync(st->ts_input);
239 dev_dbg(&idev->dev, "pressure too low: not reporting\n");
245 static irqreturn_t at91_adc_rl_interrupt(int irq, void *private)
247 struct iio_dev *idev = private;
248 struct at91_adc_state *st = iio_priv(idev);
249 u32 status = at91_adc_readl(st, st->registers->status_register);
252 status &= at91_adc_readl(st, AT91_ADC_IMR);
253 if (status & st->registers->drdy_mask)
254 handle_adc_eoc_trigger(irq, idev);
256 if (status & AT91RL_ADC_IER_PEN) {
257 /* Disabling pen debounce is required to get a NOPEN irq */
258 reg = at91_adc_readl(st, AT91_ADC_MR);
259 reg &= ~AT91_ADC_PENDBC;
260 at91_adc_writel(st, AT91_ADC_MR, reg);
262 at91_adc_writel(st, AT91_ADC_IDR, AT91RL_ADC_IER_PEN);
263 at91_adc_writel(st, AT91_ADC_IER, AT91RL_ADC_IER_NOPEN
265 /* Set up period trigger for sampling */
266 at91_adc_writel(st, st->registers->trigger_register,
267 AT91_ADC_TRGR_MOD_PERIOD_TRIG |
268 AT91_ADC_TRGR_TRGPER_(st->ts_sample_period_val));
269 } else if (status & AT91RL_ADC_IER_NOPEN) {
270 reg = at91_adc_readl(st, AT91_ADC_MR);
271 reg |= AT91_ADC_PENDBC_(st->ts_pendbc) & AT91_ADC_PENDBC;
272 at91_adc_writel(st, AT91_ADC_MR, reg);
273 at91_adc_writel(st, st->registers->trigger_register,
276 at91_adc_writel(st, AT91_ADC_IDR, AT91RL_ADC_IER_NOPEN
278 at91_adc_writel(st, AT91_ADC_IER, AT91RL_ADC_IER_PEN);
279 st->ts_bufferedmeasure = false;
280 input_report_key(st->ts_input, BTN_TOUCH, 0);
281 input_sync(st->ts_input);
282 } else if (status & AT91_ADC_EOC(3)) {
283 /* Conversion finished */
284 if (st->ts_bufferedmeasure) {
286 * Last measurement is always discarded, since it can
288 * Always report previous measurement
290 input_report_abs(st->ts_input, ABS_X, st->ts_prev_absx);
291 input_report_abs(st->ts_input, ABS_Y, st->ts_prev_absy);
292 input_report_key(st->ts_input, BTN_TOUCH, 1);
293 input_sync(st->ts_input);
295 st->ts_bufferedmeasure = true;
297 /* Now make new measurement */
298 st->ts_prev_absx = at91_adc_readl(st, AT91_ADC_CHAN(st, 3))
300 st->ts_prev_absx /= at91_adc_readl(st, AT91_ADC_CHAN(st, 2));
302 st->ts_prev_absy = at91_adc_readl(st, AT91_ADC_CHAN(st, 1))
304 st->ts_prev_absy /= at91_adc_readl(st, AT91_ADC_CHAN(st, 0));
310 static irqreturn_t at91_adc_9x5_interrupt(int irq, void *private)
312 struct iio_dev *idev = private;
313 struct at91_adc_state *st = iio_priv(idev);
314 u32 status = at91_adc_readl(st, st->registers->status_register);
315 const uint32_t ts_data_irq_mask =
320 if (status & st->registers->drdy_mask)
321 handle_adc_eoc_trigger(irq, idev);
323 if (status & AT91_ADC_IER_PEN) {
324 at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
325 at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_NOPEN |
327 /* Set up period trigger for sampling */
328 at91_adc_writel(st, st->registers->trigger_register,
329 AT91_ADC_TRGR_MOD_PERIOD_TRIG |
330 AT91_ADC_TRGR_TRGPER_(st->ts_sample_period_val));
331 } else if (status & AT91_ADC_IER_NOPEN) {
332 at91_adc_writel(st, st->registers->trigger_register, 0);
333 at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_NOPEN |
335 at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
337 input_report_key(st->ts_input, BTN_TOUCH, 0);
338 input_sync(st->ts_input);
339 } else if ((status & ts_data_irq_mask) == ts_data_irq_mask) {
340 /* Now all touchscreen data is ready */
342 if (status & AT91_ADC_ISR_PENS) {
343 /* validate data by pen contact */
346 /* triggered by event that is no pen contact, just read
347 * them to clean the interrupt and discard all.
349 at91_adc_readl(st, AT91_ADC_TSXPOSR);
350 at91_adc_readl(st, AT91_ADC_TSYPOSR);
351 at91_adc_readl(st, AT91_ADC_TSPRESSR);
358 static int at91_adc_channel_init(struct iio_dev *idev)
360 struct at91_adc_state *st = iio_priv(idev);
361 struct iio_chan_spec *chan_array, *timestamp;
363 unsigned long rsvd_mask = 0;
365 /* If touchscreen is enable, then reserve the adc channels */
366 if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
367 rsvd_mask = CHAN_MASK_TOUCHSCREEN_4WIRE;
368 else if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_5WIRE)
369 rsvd_mask = CHAN_MASK_TOUCHSCREEN_5WIRE;
371 /* set up the channel mask to reserve touchscreen channels */
372 st->channels_mask &= ~rsvd_mask;
374 idev->num_channels = bitmap_weight(&st->channels_mask,
375 st->num_channels) + 1;
377 chan_array = devm_kzalloc(&idev->dev,
378 ((idev->num_channels + 1) *
379 sizeof(struct iio_chan_spec)),
385 for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
386 struct iio_chan_spec *chan = chan_array + idx;
388 chan->type = IIO_VOLTAGE;
391 chan->scan_index = idx;
392 chan->scan_type.sign = 'u';
393 chan->scan_type.realbits = st->res;
394 chan->scan_type.storagebits = 16;
395 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
396 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
399 timestamp = chan_array + idx;
401 timestamp->type = IIO_TIMESTAMP;
402 timestamp->channel = -1;
403 timestamp->scan_index = idx;
404 timestamp->scan_type.sign = 's';
405 timestamp->scan_type.realbits = 64;
406 timestamp->scan_type.storagebits = 64;
408 idev->channels = chan_array;
409 return idev->num_channels;
412 static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
413 struct at91_adc_trigger *triggers,
414 const char *trigger_name)
416 struct at91_adc_state *st = iio_priv(idev);
420 for (i = 0; i < st->trigger_number; i++) {
421 char *name = kasprintf(GFP_KERNEL,
429 if (strcmp(trigger_name, name) == 0) {
430 value = triggers[i].value;
441 static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
443 struct iio_dev *idev = iio_trigger_get_drvdata(trig);
444 struct at91_adc_state *st = iio_priv(idev);
445 struct iio_buffer *buffer = idev->buffer;
446 struct at91_adc_reg_desc *reg = st->registers;
447 u32 status = at91_adc_readl(st, reg->trigger_register);
451 value = at91_adc_get_trigger_value_by_name(idev,
458 st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
459 if (st->buffer == NULL)
462 at91_adc_writel(st, reg->trigger_register,
465 for_each_set_bit(bit, buffer->scan_mask,
467 struct iio_chan_spec const *chan = idev->channels + bit;
468 at91_adc_writel(st, AT91_ADC_CHER,
469 AT91_ADC_CH(chan->channel));
472 at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
475 at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
477 at91_adc_writel(st, reg->trigger_register,
480 for_each_set_bit(bit, buffer->scan_mask,
482 struct iio_chan_spec const *chan = idev->channels + bit;
483 at91_adc_writel(st, AT91_ADC_CHDR,
484 AT91_ADC_CH(chan->channel));
492 static const struct iio_trigger_ops at91_adc_trigger_ops = {
493 .owner = THIS_MODULE,
494 .set_trigger_state = &at91_adc_configure_trigger,
497 static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
498 struct at91_adc_trigger *trigger)
500 struct iio_trigger *trig;
503 trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
504 idev->id, trigger->name);
508 trig->dev.parent = idev->dev.parent;
509 iio_trigger_set_drvdata(trig, idev);
510 trig->ops = &at91_adc_trigger_ops;
512 ret = iio_trigger_register(trig);
519 static int at91_adc_trigger_init(struct iio_dev *idev)
521 struct at91_adc_state *st = iio_priv(idev);
524 st->trig = devm_kzalloc(&idev->dev,
525 st->trigger_number * sizeof(*st->trig),
528 if (st->trig == NULL) {
533 for (i = 0; i < st->trigger_number; i++) {
534 if (st->trigger_list[i].is_external && !(st->use_external))
537 st->trig[i] = at91_adc_allocate_trigger(idev,
538 st->trigger_list + i);
539 if (st->trig[i] == NULL) {
541 "Could not allocate trigger %d\n", i);
550 for (i--; i >= 0; i--) {
551 iio_trigger_unregister(st->trig[i]);
552 iio_trigger_free(st->trig[i]);
558 static void at91_adc_trigger_remove(struct iio_dev *idev)
560 struct at91_adc_state *st = iio_priv(idev);
563 for (i = 0; i < st->trigger_number; i++) {
564 iio_trigger_unregister(st->trig[i]);
565 iio_trigger_free(st->trig[i]);
569 static int at91_adc_buffer_init(struct iio_dev *idev)
571 return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
572 &at91_adc_trigger_handler, NULL);
575 static void at91_adc_buffer_remove(struct iio_dev *idev)
577 iio_triggered_buffer_cleanup(idev);
580 static int at91_adc_read_raw(struct iio_dev *idev,
581 struct iio_chan_spec const *chan,
582 int *val, int *val2, long mask)
584 struct at91_adc_state *st = iio_priv(idev);
588 case IIO_CHAN_INFO_RAW:
589 mutex_lock(&st->lock);
591 at91_adc_writel(st, AT91_ADC_CHER,
592 AT91_ADC_CH(chan->channel));
593 at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask);
594 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
596 ret = wait_event_interruptible_timeout(st->wq_data_avail,
598 msecs_to_jiffies(1000));
602 mutex_unlock(&st->lock);
606 *val = st->last_value;
608 at91_adc_writel(st, AT91_ADC_CHDR,
609 AT91_ADC_CH(chan->channel));
610 at91_adc_writel(st, AT91_ADC_IDR, st->registers->drdy_mask);
614 mutex_unlock(&st->lock);
617 case IIO_CHAN_INFO_SCALE:
619 *val2 = chan->scan_type.realbits;
620 return IIO_VAL_FRACTIONAL_LOG2;
627 static int at91_adc_of_get_resolution(struct at91_adc_state *st,
628 struct platform_device *pdev)
630 struct iio_dev *idev = iio_priv_to_dev(st);
631 struct device_node *np = pdev->dev.of_node;
632 int count, i, ret = 0;
636 count = of_property_count_strings(np, "atmel,adc-res-names");
638 dev_err(&idev->dev, "You must specified at least two resolution names for "
639 "adc-res-names property in the DT\n");
643 resolutions = kmalloc(count * sizeof(*resolutions), GFP_KERNEL);
647 if (of_property_read_u32_array(np, "atmel,adc-res", resolutions, count)) {
648 dev_err(&idev->dev, "Missing adc-res property in the DT.\n");
653 if (of_property_read_string(np, "atmel,adc-use-res", (const char **)&res_name))
654 res_name = "highres";
656 for (i = 0; i < count; i++) {
657 if (of_property_read_string_index(np, "atmel,adc-res-names", i, (const char **)&s))
660 if (strcmp(res_name, s))
663 st->res = resolutions[i];
664 if (!strcmp(res_name, "lowres"))
669 dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
673 dev_err(&idev->dev, "There is no resolution for %s\n", res_name);
680 static u32 calc_startup_ticks_9260(u8 startup_time, u32 adc_clk_khz)
683 * Number of ticks needed to cover the startup time of the ADC
684 * as defined in the electrical characteristics of the board,
685 * divided by 8. The formula thus is :
686 * Startup Time = (ticks + 1) * 8 / ADC Clock
688 return round_up((startup_time * adc_clk_khz / 1000) - 1, 8) / 8;
691 static u32 calc_startup_ticks_9x5(u8 startup_time, u32 adc_clk_khz)
694 * For sama5d3x and at91sam9x5, the formula changes to:
695 * Startup Time = <lookup_table_value> / ADC Clock
697 const int startup_lookup[] = {
703 int i, size = ARRAY_SIZE(startup_lookup);
706 ticks = startup_time * adc_clk_khz / 1000;
707 for (i = 0; i < size; i++)
708 if (ticks < startup_lookup[i])
713 /* Reach the end of lookup table */
719 static const struct of_device_id at91_adc_dt_ids[];
721 static int at91_adc_probe_dt_ts(struct device_node *node,
722 struct at91_adc_state *st, struct device *dev)
727 ret = of_property_read_u32(node, "atmel,adc-ts-wires", &prop);
729 dev_info(dev, "ADC Touch screen is disabled.\n");
736 st->touchscreen_type = prop;
739 dev_err(dev, "Unsupported number of touchscreen wires (%d). Should be 4 or 5.\n", prop);
743 if (!st->caps->has_tsmr)
746 of_property_read_u32(node, "atmel,adc-ts-pressure-threshold", &prop);
747 st->ts_pressure_threshold = prop;
748 if (st->ts_pressure_threshold) {
751 dev_err(dev, "Invalid pressure threshold for the touchscreen\n");
756 static int at91_adc_probe_dt(struct at91_adc_state *st,
757 struct platform_device *pdev)
759 struct iio_dev *idev = iio_priv_to_dev(st);
760 struct device_node *node = pdev->dev.of_node;
761 struct device_node *trig_node;
768 st->caps = (struct at91_adc_caps *)
769 of_match_device(at91_adc_dt_ids, &pdev->dev)->data;
771 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
773 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
774 dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
778 st->channels_mask = prop;
780 st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
782 if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
783 dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
787 st->startup_time = prop;
790 of_property_read_u32(node, "atmel,adc-sample-hold-time", &prop);
791 st->sample_hold_time = prop;
793 if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
794 dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
800 ret = at91_adc_of_get_resolution(st, pdev);
804 st->registers = &st->caps->registers;
805 st->num_channels = st->caps->num_channels;
806 st->trigger_number = of_get_child_count(node);
807 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
808 sizeof(struct at91_adc_trigger),
810 if (!st->trigger_list) {
811 dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
816 for_each_child_of_node(node, trig_node) {
817 struct at91_adc_trigger *trig = st->trigger_list + i;
820 if (of_property_read_string(trig_node, "trigger-name", &name)) {
821 dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
827 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
828 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
833 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
837 /* Check if touchscreen is supported. */
838 if (st->caps->has_ts)
839 return at91_adc_probe_dt_ts(node, st, &idev->dev);
841 dev_info(&idev->dev, "not support touchscreen in the adc compatible string.\n");
849 static int at91_adc_probe_pdata(struct at91_adc_state *st,
850 struct platform_device *pdev)
852 struct at91_adc_data *pdata = pdev->dev.platform_data;
857 st->caps = (struct at91_adc_caps *)
858 platform_get_device_id(pdev)->driver_data;
860 st->use_external = pdata->use_external_triggers;
861 st->vref_mv = pdata->vref;
862 st->channels_mask = pdata->channels_used;
863 st->num_channels = st->caps->num_channels;
864 st->startup_time = pdata->startup_time;
865 st->trigger_number = pdata->trigger_number;
866 st->trigger_list = pdata->trigger_list;
867 st->registers = &st->caps->registers;
868 st->touchscreen_type = pdata->touchscreen_type;
873 static const struct iio_info at91_adc_info = {
874 .driver_module = THIS_MODULE,
875 .read_raw = &at91_adc_read_raw,
878 /* Touchscreen related functions */
879 static int atmel_ts_open(struct input_dev *dev)
881 struct at91_adc_state *st = input_get_drvdata(dev);
883 if (st->caps->has_tsmr)
884 at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
886 at91_adc_writel(st, AT91_ADC_IER, AT91RL_ADC_IER_PEN);
890 static void atmel_ts_close(struct input_dev *dev)
892 struct at91_adc_state *st = input_get_drvdata(dev);
894 if (st->caps->has_tsmr)
895 at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
897 at91_adc_writel(st, AT91_ADC_IDR, AT91RL_ADC_IER_PEN);
900 static int at91_ts_hw_init(struct at91_adc_state *st, u32 adc_clk_khz)
905 /* a Pen Detect Debounce Time is necessary for the ADC Touch to avoid
907 * The formula is : Pen Detect Debounce Time = (2 ^ pendbc) / ADCClock
909 st->ts_pendbc = round_up(TOUCH_PEN_DETECT_DEBOUNCE_US * adc_clk_khz /
912 while (st->ts_pendbc >> ++i)
913 ; /* Empty! Find the shift offset */
914 if (abs(st->ts_pendbc - (1 << i)) < abs(st->ts_pendbc - (1 << (i - 1))))
917 st->ts_pendbc = i - 1;
919 if (!st->caps->has_tsmr) {
920 reg = at91_adc_readl(st, AT91_ADC_MR);
921 reg |= AT91_ADC_TSAMOD_TS_ONLY_MODE | AT91_ADC_PENDET;
923 reg |= AT91_ADC_PENDBC_(st->ts_pendbc) & AT91_ADC_PENDBC;
924 at91_adc_writel(st, AT91_ADC_MR, reg);
926 reg = AT91_ADC_TSR_SHTIM_(TOUCH_SHTIM) & AT91_ADC_TSR_SHTIM;
927 at91_adc_writel(st, AT91_ADC_TSR, reg);
929 st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US_RL *
930 adc_clk_khz / 1000) - 1, 1);
935 if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
936 reg = AT91_ADC_TSMR_TSMODE_4WIRE_PRESS;
938 reg = AT91_ADC_TSMR_TSMODE_5WIRE;
940 reg |= AT91_ADC_TSMR_TSAV_(st->caps->ts_filter_average)
941 & AT91_ADC_TSMR_TSAV;
942 reg |= AT91_ADC_TSMR_PENDBC_(st->ts_pendbc) & AT91_ADC_TSMR_PENDBC;
943 reg |= AT91_ADC_TSMR_NOTSDMA;
944 reg |= AT91_ADC_TSMR_PENDET_ENA;
945 reg |= 0x03 << 8; /* TSFREQ, needs to be bigger than TSAV */
947 at91_adc_writel(st, AT91_ADC_TSMR, reg);
949 /* Change adc internal resistor value for better pen detection,
950 * default value is 100 kOhm.
951 * 0 = 200 kOhm, 1 = 150 kOhm, 2 = 100 kOhm, 3 = 50 kOhm
952 * option only available on ES2 and higher
954 at91_adc_writel(st, AT91_ADC_ACR, st->caps->ts_pen_detect_sensitivity
955 & AT91_ADC_ACR_PENDETSENS);
957 /* Sample Period Time = (TRGPER + 1) / ADCClock */
958 st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US *
959 adc_clk_khz / 1000) - 1, 1);
964 static int at91_ts_register(struct at91_adc_state *st,
965 struct platform_device *pdev)
967 struct input_dev *input;
968 struct iio_dev *idev = iio_priv_to_dev(st);
971 input = input_allocate_device();
973 dev_err(&idev->dev, "Failed to allocate TS device!\n");
977 input->name = DRIVER_NAME;
978 input->id.bustype = BUS_HOST;
979 input->dev.parent = &pdev->dev;
980 input->open = atmel_ts_open;
981 input->close = atmel_ts_close;
983 __set_bit(EV_ABS, input->evbit);
984 __set_bit(EV_KEY, input->evbit);
985 __set_bit(BTN_TOUCH, input->keybit);
986 if (st->caps->has_tsmr) {
987 input_set_abs_params(input, ABS_X, 0, (1 << MAX_POS_BITS) - 1,
989 input_set_abs_params(input, ABS_Y, 0, (1 << MAX_POS_BITS) - 1,
991 input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);
993 if (st->touchscreen_type != ATMEL_ADC_TOUCHSCREEN_4WIRE) {
995 "This touchscreen controller only support 4 wires\n");
1000 input_set_abs_params(input, ABS_X, 0, (1 << MAX_RLPOS_BITS) - 1,
1002 input_set_abs_params(input, ABS_Y, 0, (1 << MAX_RLPOS_BITS) - 1,
1006 st->ts_input = input;
1007 input_set_drvdata(input, st);
1009 ret = input_register_device(input);
1016 input_free_device(st->ts_input);
1020 static void at91_ts_unregister(struct at91_adc_state *st)
1022 input_unregister_device(st->ts_input);
1025 static int at91_adc_probe(struct platform_device *pdev)
1027 unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim;
1029 struct iio_dev *idev;
1030 struct at91_adc_state *st;
1031 struct resource *res;
1034 idev = devm_iio_device_alloc(&pdev->dev, sizeof(struct at91_adc_state));
1038 st = iio_priv(idev);
1040 if (pdev->dev.of_node)
1041 ret = at91_adc_probe_dt(st, pdev);
1043 ret = at91_adc_probe_pdata(st, pdev);
1046 dev_err(&pdev->dev, "No platform data available.\n");
1050 platform_set_drvdata(pdev, idev);
1052 idev->dev.parent = &pdev->dev;
1053 idev->name = dev_name(&pdev->dev);
1054 idev->modes = INDIO_DIRECT_MODE;
1055 idev->info = &at91_adc_info;
1057 st->irq = platform_get_irq(pdev, 0);
1059 dev_err(&pdev->dev, "No IRQ ID is designated\n");
1063 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1065 st->reg_base = devm_ioremap_resource(&pdev->dev, res);
1066 if (IS_ERR(st->reg_base)) {
1067 return PTR_ERR(st->reg_base);
1071 * Disable all IRQs before setting up the handler
1073 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
1074 at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
1076 if (st->caps->has_tsmr)
1077 ret = request_irq(st->irq, at91_adc_9x5_interrupt, 0,
1078 pdev->dev.driver->name, idev);
1080 ret = request_irq(st->irq, at91_adc_rl_interrupt, 0,
1081 pdev->dev.driver->name, idev);
1083 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
1087 st->clk = devm_clk_get(&pdev->dev, "adc_clk");
1088 if (IS_ERR(st->clk)) {
1089 dev_err(&pdev->dev, "Failed to get the clock.\n");
1090 ret = PTR_ERR(st->clk);
1091 goto error_free_irq;
1094 ret = clk_prepare_enable(st->clk);
1097 "Could not prepare or enable the clock.\n");
1098 goto error_free_irq;
1101 st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
1102 if (IS_ERR(st->adc_clk)) {
1103 dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
1104 ret = PTR_ERR(st->adc_clk);
1105 goto error_disable_clk;
1108 ret = clk_prepare_enable(st->adc_clk);
1111 "Could not prepare or enable the ADC clock.\n");
1112 goto error_disable_clk;
1116 * Prescaler rate computation using the formula from the Atmel's
1117 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
1118 * specified by the electrical characteristics of the board.
1120 mstrclk = clk_get_rate(st->clk);
1121 adc_clk = clk_get_rate(st->adc_clk);
1122 adc_clk_khz = adc_clk / 1000;
1124 dev_dbg(&pdev->dev, "Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n",
1127 prsc = (mstrclk / (2 * adc_clk)) - 1;
1129 if (!st->startup_time) {
1130 dev_err(&pdev->dev, "No startup time available.\n");
1132 goto error_disable_adc_clk;
1134 ticks = (*st->caps->calc_startup_ticks)(st->startup_time, adc_clk_khz);
1137 * a minimal Sample and Hold Time is necessary for the ADC to guarantee
1138 * the best converted final value between two channels selection
1139 * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
1141 if (st->sample_hold_time > 0)
1142 shtim = round_up((st->sample_hold_time * adc_clk_khz / 1000)
1147 reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask;
1148 reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask;
1150 reg |= AT91_ADC_LOWRES;
1152 reg |= AT91_ADC_SLEEP;
1153 reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
1154 at91_adc_writel(st, AT91_ADC_MR, reg);
1156 /* Setup the ADC channels available on the board */
1157 ret = at91_adc_channel_init(idev);
1159 dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
1160 goto error_disable_adc_clk;
1163 init_waitqueue_head(&st->wq_data_avail);
1164 mutex_init(&st->lock);
1167 * Since touch screen will set trigger register as period trigger. So
1168 * when touch screen is enabled, then we have to disable hardware
1169 * trigger for classic adc.
1171 if (!st->touchscreen_type) {
1172 ret = at91_adc_buffer_init(idev);
1174 dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
1175 goto error_disable_adc_clk;
1178 ret = at91_adc_trigger_init(idev);
1180 dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
1181 at91_adc_buffer_remove(idev);
1182 goto error_disable_adc_clk;
1185 ret = at91_ts_register(st, pdev);
1187 goto error_disable_adc_clk;
1189 at91_ts_hw_init(st, adc_clk_khz);
1192 ret = iio_device_register(idev);
1194 dev_err(&pdev->dev, "Couldn't register the device.\n");
1195 goto error_iio_device_register;
1200 error_iio_device_register:
1201 if (!st->touchscreen_type) {
1202 at91_adc_trigger_remove(idev);
1203 at91_adc_buffer_remove(idev);
1205 at91_ts_unregister(st);
1207 error_disable_adc_clk:
1208 clk_disable_unprepare(st->adc_clk);
1210 clk_disable_unprepare(st->clk);
1212 free_irq(st->irq, idev);
1216 static int at91_adc_remove(struct platform_device *pdev)
1218 struct iio_dev *idev = platform_get_drvdata(pdev);
1219 struct at91_adc_state *st = iio_priv(idev);
1221 iio_device_unregister(idev);
1222 if (!st->touchscreen_type) {
1223 at91_adc_trigger_remove(idev);
1224 at91_adc_buffer_remove(idev);
1226 at91_ts_unregister(st);
1228 clk_disable_unprepare(st->adc_clk);
1229 clk_disable_unprepare(st->clk);
1230 free_irq(st->irq, idev);
1235 static struct at91_adc_caps at91sam9260_caps = {
1236 .calc_startup_ticks = calc_startup_ticks_9260,
1239 .channel_base = AT91_ADC_CHR(0),
1240 .drdy_mask = AT91_ADC_DRDY,
1241 .status_register = AT91_ADC_SR,
1242 .trigger_register = AT91_ADC_TRGR_9260,
1243 .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
1244 .mr_startup_mask = AT91_ADC_STARTUP_9260,
1248 static struct at91_adc_caps at91sam9g45_caps = {
1250 .calc_startup_ticks = calc_startup_ticks_9260, /* same as 9260 */
1253 .channel_base = AT91_ADC_CHR(0),
1254 .drdy_mask = AT91_ADC_DRDY,
1255 .status_register = AT91_ADC_SR,
1256 .trigger_register = AT91_ADC_TRGR_9G45,
1257 .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
1258 .mr_startup_mask = AT91_ADC_STARTUP_9G45,
1262 static struct at91_adc_caps at91sam9x5_caps = {
1265 .ts_filter_average = 3,
1266 .ts_pen_detect_sensitivity = 2,
1267 .calc_startup_ticks = calc_startup_ticks_9x5,
1270 .channel_base = AT91_ADC_CDR0_9X5,
1271 .drdy_mask = AT91_ADC_SR_DRDY_9X5,
1272 .status_register = AT91_ADC_SR_9X5,
1273 .trigger_register = AT91_ADC_TRGR_9X5,
1274 /* prescal mask is same as 9G45 */
1275 .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
1276 .mr_startup_mask = AT91_ADC_STARTUP_9X5,
1280 static const struct of_device_id at91_adc_dt_ids[] = {
1281 { .compatible = "atmel,at91sam9260-adc", .data = &at91sam9260_caps },
1282 { .compatible = "atmel,at91sam9g45-adc", .data = &at91sam9g45_caps },
1283 { .compatible = "atmel,at91sam9x5-adc", .data = &at91sam9x5_caps },
1286 MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
1288 static const struct platform_device_id at91_adc_ids[] = {
1290 .name = "at91sam9260-adc",
1291 .driver_data = (unsigned long)&at91sam9260_caps,
1293 .name = "at91sam9g45-adc",
1294 .driver_data = (unsigned long)&at91sam9g45_caps,
1296 .name = "at91sam9x5-adc",
1297 .driver_data = (unsigned long)&at91sam9x5_caps,
1302 MODULE_DEVICE_TABLE(platform, at91_adc_ids);
1304 static struct platform_driver at91_adc_driver = {
1305 .probe = at91_adc_probe,
1306 .remove = at91_adc_remove,
1307 .id_table = at91_adc_ids,
1309 .name = DRIVER_NAME,
1310 .of_match_table = of_match_ptr(at91_adc_dt_ids),
1314 module_platform_driver(at91_adc_driver);
1316 MODULE_LICENSE("GPL");
1317 MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
1318 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");