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
50 * struct at91_adc_reg_desc - Various informations relative to registers
51 * @channel_base: Base offset for the channel data registers
52 * @drdy_mask: Mask of the DRDY field in the relevant registers
53 (Interruptions registers mostly)
54 * @status_register: Offset of the Interrupt Status Register
55 * @trigger_register: Offset of the Trigger setup register
56 * @mr_prescal_mask: Mask of the PRESCAL field in the adc MR register
57 * @mr_startup_mask: Mask of the STARTUP field in the adc MR register
59 struct at91_adc_reg_desc {
68 struct at91_adc_caps {
69 bool has_ts; /* Support touch screen */
70 bool has_tsmr; /* only at91sam9x5, sama5d3 have TSMR reg */
72 * Numbers of sampling data will be averaged. Can be 0~3.
73 * Hardware can average (2 ^ ts_filter_average) sample data.
76 /* Pen Detection input pull-up resistor, can be 0~3 */
77 u8 ts_pen_detect_sensitivity;
79 /* startup time calculate function */
80 u32 (*calc_startup_ticks)(u8 startup_time, u32 adc_clk_khz);
83 struct at91_adc_reg_desc registers;
86 enum atmel_adc_ts_type {
87 ATMEL_ADC_TOUCHSCREEN_NONE = 0,
88 ATMEL_ADC_TOUCHSCREEN_4WIRE = 4,
89 ATMEL_ADC_TOUCHSCREEN_5WIRE = 5,
92 struct at91_adc_state {
95 unsigned long channels_mask;
102 void __iomem *reg_base;
103 struct at91_adc_reg_desc *registers;
107 struct iio_trigger **trig;
108 struct at91_adc_trigger *trigger_list;
112 u32 res; /* resolution used for convertions */
113 bool low_res; /* the resolution corresponds to the lowest one */
114 wait_queue_head_t wq_data_avail;
115 struct at91_adc_caps *caps;
118 * Following ADC channels are shared by touchscreen:
120 * CH0 -- Touch screen XP/UL
121 * CH1 -- Touch screen XM/UR
122 * CH2 -- Touch screen YP/LL
123 * CH3 -- Touch screen YM/Sense
124 * CH4 -- Touch screen LR(5-wire only)
126 * The bitfields below represents the reserved channel in the
129 #define CHAN_MASK_TOUCHSCREEN_4WIRE (0xf << 0)
130 #define CHAN_MASK_TOUCHSCREEN_5WIRE (0x1f << 0)
131 enum atmel_adc_ts_type touchscreen_type;
132 struct input_dev *ts_input;
134 u16 ts_sample_period_val;
135 u32 ts_pressure_threshold;
138 static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
140 struct iio_poll_func *pf = p;
141 struct iio_dev *idev = pf->indio_dev;
142 struct at91_adc_state *st = iio_priv(idev);
145 for (i = 0; i < idev->masklength; i++) {
146 if (!test_bit(i, idev->active_scan_mask))
148 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
152 iio_push_to_buffers_with_timestamp(idev, st->buffer, pf->timestamp);
154 iio_trigger_notify_done(idev->trig);
156 /* Needed to ACK the DRDY interruption */
157 at91_adc_readl(st, AT91_ADC_LCDR);
164 /* Handler for classic adc channel eoc trigger */
165 void handle_adc_eoc_trigger(int irq, struct iio_dev *idev)
167 struct at91_adc_state *st = iio_priv(idev);
169 if (iio_buffer_enabled(idev)) {
170 disable_irq_nosync(irq);
171 iio_trigger_poll(idev->trig, iio_get_time_ns());
173 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
175 wake_up_interruptible(&st->wq_data_avail);
179 static int at91_ts_sample(struct at91_adc_state *st)
181 unsigned int xscale, yscale, reg, z1, z2;
182 unsigned int x, y, pres, xpos, ypos;
183 unsigned int rxp = 1;
184 unsigned int factor = 1000;
185 struct iio_dev *idev = iio_priv_to_dev(st);
187 unsigned int xyz_mask_bits = st->res;
188 unsigned int xyz_mask = (1 << xyz_mask_bits) - 1;
190 /* calculate position */
191 /* x position = (x / xscale) * max, max = 2^MAX_POS_BITS - 1 */
192 reg = at91_adc_readl(st, AT91_ADC_TSXPOSR);
193 xpos = reg & xyz_mask;
194 x = (xpos << MAX_POS_BITS) - xpos;
195 xscale = (reg >> 16) & xyz_mask;
197 dev_err(&idev->dev, "Error: xscale == 0!\n");
202 /* y position = (y / yscale) * max, max = 2^MAX_POS_BITS - 1 */
203 reg = at91_adc_readl(st, AT91_ADC_TSYPOSR);
204 ypos = reg & xyz_mask;
205 y = (ypos << MAX_POS_BITS) - ypos;
206 yscale = (reg >> 16) & xyz_mask;
208 dev_err(&idev->dev, "Error: yscale == 0!\n");
213 /* calculate the pressure */
214 reg = at91_adc_readl(st, AT91_ADC_TSPRESSR);
216 z2 = (reg >> 16) & xyz_mask;
219 pres = rxp * (x * factor / 1024) * (z2 * factor / z1 - factor)
222 pres = st->ts_pressure_threshold; /* no pen contacted */
224 dev_dbg(&idev->dev, "xpos = %d, xscale = %d, ypos = %d, yscale = %d, z1 = %d, z2 = %d, press = %d\n",
225 xpos, xscale, ypos, yscale, z1, z2, pres);
227 if (pres < st->ts_pressure_threshold) {
228 dev_dbg(&idev->dev, "x = %d, y = %d, pressure = %d\n",
229 x, y, pres / factor);
230 input_report_abs(st->ts_input, ABS_X, x);
231 input_report_abs(st->ts_input, ABS_Y, y);
232 input_report_abs(st->ts_input, ABS_PRESSURE, pres);
233 input_report_key(st->ts_input, BTN_TOUCH, 1);
234 input_sync(st->ts_input);
236 dev_dbg(&idev->dev, "pressure too low: not reporting\n");
242 static irqreturn_t at91_adc_interrupt(int irq, void *private)
244 struct iio_dev *idev = private;
245 struct at91_adc_state *st = iio_priv(idev);
246 u32 status = at91_adc_readl(st, st->registers->status_register);
247 const uint32_t ts_data_irq_mask =
252 if (status & st->registers->drdy_mask)
253 handle_adc_eoc_trigger(irq, idev);
255 if (status & AT91_ADC_IER_PEN) {
256 at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
257 at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_NOPEN |
259 /* Set up period trigger for sampling */
260 at91_adc_writel(st, st->registers->trigger_register,
261 AT91_ADC_TRGR_MOD_PERIOD_TRIG |
262 AT91_ADC_TRGR_TRGPER_(st->ts_sample_period_val));
263 } else if (status & AT91_ADC_IER_NOPEN) {
264 at91_adc_writel(st, st->registers->trigger_register, 0);
265 at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_NOPEN |
267 at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
269 input_report_key(st->ts_input, BTN_TOUCH, 0);
270 input_sync(st->ts_input);
271 } else if ((status & ts_data_irq_mask) == ts_data_irq_mask) {
272 /* Now all touchscreen data is ready */
274 if (status & AT91_ADC_ISR_PENS) {
275 /* validate data by pen contact */
278 /* triggered by event that is no pen contact, just read
279 * them to clean the interrupt and discard all.
281 at91_adc_readl(st, AT91_ADC_TSXPOSR);
282 at91_adc_readl(st, AT91_ADC_TSYPOSR);
283 at91_adc_readl(st, AT91_ADC_TSPRESSR);
290 static int at91_adc_channel_init(struct iio_dev *idev)
292 struct at91_adc_state *st = iio_priv(idev);
293 struct iio_chan_spec *chan_array, *timestamp;
295 unsigned long rsvd_mask = 0;
297 /* If touchscreen is enable, then reserve the adc channels */
298 if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
299 rsvd_mask = CHAN_MASK_TOUCHSCREEN_4WIRE;
300 else if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_5WIRE)
301 rsvd_mask = CHAN_MASK_TOUCHSCREEN_5WIRE;
303 /* set up the channel mask to reserve touchscreen channels */
304 st->channels_mask &= ~rsvd_mask;
306 idev->num_channels = bitmap_weight(&st->channels_mask,
307 st->num_channels) + 1;
309 chan_array = devm_kzalloc(&idev->dev,
310 ((idev->num_channels + 1) *
311 sizeof(struct iio_chan_spec)),
317 for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
318 struct iio_chan_spec *chan = chan_array + idx;
320 chan->type = IIO_VOLTAGE;
323 chan->scan_index = idx;
324 chan->scan_type.sign = 'u';
325 chan->scan_type.realbits = st->res;
326 chan->scan_type.storagebits = 16;
327 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
328 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
331 timestamp = chan_array + idx;
333 timestamp->type = IIO_TIMESTAMP;
334 timestamp->channel = -1;
335 timestamp->scan_index = idx;
336 timestamp->scan_type.sign = 's';
337 timestamp->scan_type.realbits = 64;
338 timestamp->scan_type.storagebits = 64;
340 idev->channels = chan_array;
341 return idev->num_channels;
344 static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
345 struct at91_adc_trigger *triggers,
346 const char *trigger_name)
348 struct at91_adc_state *st = iio_priv(idev);
352 for (i = 0; i < st->trigger_number; i++) {
353 char *name = kasprintf(GFP_KERNEL,
361 if (strcmp(trigger_name, name) == 0) {
362 value = triggers[i].value;
373 static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
375 struct iio_dev *idev = iio_trigger_get_drvdata(trig);
376 struct at91_adc_state *st = iio_priv(idev);
377 struct iio_buffer *buffer = idev->buffer;
378 struct at91_adc_reg_desc *reg = st->registers;
379 u32 status = at91_adc_readl(st, reg->trigger_register);
383 value = at91_adc_get_trigger_value_by_name(idev,
390 st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
391 if (st->buffer == NULL)
394 at91_adc_writel(st, reg->trigger_register,
397 for_each_set_bit(bit, buffer->scan_mask,
399 struct iio_chan_spec const *chan = idev->channels + bit;
400 at91_adc_writel(st, AT91_ADC_CHER,
401 AT91_ADC_CH(chan->channel));
404 at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
407 at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
409 at91_adc_writel(st, reg->trigger_register,
412 for_each_set_bit(bit, buffer->scan_mask,
414 struct iio_chan_spec const *chan = idev->channels + bit;
415 at91_adc_writel(st, AT91_ADC_CHDR,
416 AT91_ADC_CH(chan->channel));
424 static const struct iio_trigger_ops at91_adc_trigger_ops = {
425 .owner = THIS_MODULE,
426 .set_trigger_state = &at91_adc_configure_trigger,
429 static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
430 struct at91_adc_trigger *trigger)
432 struct iio_trigger *trig;
435 trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
436 idev->id, trigger->name);
440 trig->dev.parent = idev->dev.parent;
441 iio_trigger_set_drvdata(trig, idev);
442 trig->ops = &at91_adc_trigger_ops;
444 ret = iio_trigger_register(trig);
451 static int at91_adc_trigger_init(struct iio_dev *idev)
453 struct at91_adc_state *st = iio_priv(idev);
456 st->trig = devm_kzalloc(&idev->dev,
457 st->trigger_number * sizeof(*st->trig),
460 if (st->trig == NULL) {
465 for (i = 0; i < st->trigger_number; i++) {
466 if (st->trigger_list[i].is_external && !(st->use_external))
469 st->trig[i] = at91_adc_allocate_trigger(idev,
470 st->trigger_list + i);
471 if (st->trig[i] == NULL) {
473 "Could not allocate trigger %d\n", i);
482 for (i--; i >= 0; i--) {
483 iio_trigger_unregister(st->trig[i]);
484 iio_trigger_free(st->trig[i]);
490 static void at91_adc_trigger_remove(struct iio_dev *idev)
492 struct at91_adc_state *st = iio_priv(idev);
495 for (i = 0; i < st->trigger_number; i++) {
496 iio_trigger_unregister(st->trig[i]);
497 iio_trigger_free(st->trig[i]);
501 static int at91_adc_buffer_init(struct iio_dev *idev)
503 return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
504 &at91_adc_trigger_handler, NULL);
507 static void at91_adc_buffer_remove(struct iio_dev *idev)
509 iio_triggered_buffer_cleanup(idev);
512 static int at91_adc_read_raw(struct iio_dev *idev,
513 struct iio_chan_spec const *chan,
514 int *val, int *val2, long mask)
516 struct at91_adc_state *st = iio_priv(idev);
520 case IIO_CHAN_INFO_RAW:
521 mutex_lock(&st->lock);
523 at91_adc_writel(st, AT91_ADC_CHER,
524 AT91_ADC_CH(chan->channel));
525 at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask);
526 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
528 ret = wait_event_interruptible_timeout(st->wq_data_avail,
530 msecs_to_jiffies(1000));
534 mutex_unlock(&st->lock);
538 *val = st->last_value;
540 at91_adc_writel(st, AT91_ADC_CHDR,
541 AT91_ADC_CH(chan->channel));
542 at91_adc_writel(st, AT91_ADC_IDR, st->registers->drdy_mask);
546 mutex_unlock(&st->lock);
549 case IIO_CHAN_INFO_SCALE:
551 *val2 = chan->scan_type.realbits;
552 return IIO_VAL_FRACTIONAL_LOG2;
559 static int at91_adc_of_get_resolution(struct at91_adc_state *st,
560 struct platform_device *pdev)
562 struct iio_dev *idev = iio_priv_to_dev(st);
563 struct device_node *np = pdev->dev.of_node;
564 int count, i, ret = 0;
568 count = of_property_count_strings(np, "atmel,adc-res-names");
570 dev_err(&idev->dev, "You must specified at least two resolution names for "
571 "adc-res-names property in the DT\n");
575 resolutions = kmalloc(count * sizeof(*resolutions), GFP_KERNEL);
579 if (of_property_read_u32_array(np, "atmel,adc-res", resolutions, count)) {
580 dev_err(&idev->dev, "Missing adc-res property in the DT.\n");
585 if (of_property_read_string(np, "atmel,adc-use-res", (const char **)&res_name))
586 res_name = "highres";
588 for (i = 0; i < count; i++) {
589 if (of_property_read_string_index(np, "atmel,adc-res-names", i, (const char **)&s))
592 if (strcmp(res_name, s))
595 st->res = resolutions[i];
596 if (!strcmp(res_name, "lowres"))
601 dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
605 dev_err(&idev->dev, "There is no resolution for %s\n", res_name);
612 static u32 calc_startup_ticks_9260(u8 startup_time, u32 adc_clk_khz)
615 * Number of ticks needed to cover the startup time of the ADC
616 * as defined in the electrical characteristics of the board,
617 * divided by 8. The formula thus is :
618 * Startup Time = (ticks + 1) * 8 / ADC Clock
620 return round_up((startup_time * adc_clk_khz / 1000) - 1, 8) / 8;
623 static u32 calc_startup_ticks_9x5(u8 startup_time, u32 adc_clk_khz)
626 * For sama5d3x and at91sam9x5, the formula changes to:
627 * Startup Time = <lookup_table_value> / ADC Clock
629 const int startup_lookup[] = {
635 int i, size = ARRAY_SIZE(startup_lookup);
638 ticks = startup_time * adc_clk_khz / 1000;
639 for (i = 0; i < size; i++)
640 if (ticks < startup_lookup[i])
645 /* Reach the end of lookup table */
651 static const struct of_device_id at91_adc_dt_ids[];
653 static int at91_adc_probe_dt_ts(struct device_node *node,
654 struct at91_adc_state *st, struct device *dev)
659 ret = of_property_read_u32(node, "atmel,adc-ts-wires", &prop);
661 dev_info(dev, "ADC Touch screen is disabled.\n");
668 st->touchscreen_type = prop;
671 dev_err(dev, "Unsupported number of touchscreen wires (%d). Should be 4 or 5.\n", prop);
676 of_property_read_u32(node, "atmel,adc-ts-pressure-threshold", &prop);
677 st->ts_pressure_threshold = prop;
678 if (st->ts_pressure_threshold) {
681 dev_err(dev, "Invalid pressure threshold for the touchscreen\n");
686 static int at91_adc_probe_dt(struct at91_adc_state *st,
687 struct platform_device *pdev)
689 struct iio_dev *idev = iio_priv_to_dev(st);
690 struct device_node *node = pdev->dev.of_node;
691 struct device_node *trig_node;
698 st->caps = (struct at91_adc_caps *)
699 of_match_device(at91_adc_dt_ids, &pdev->dev)->data;
701 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
703 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
704 dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
708 st->channels_mask = prop;
710 st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
712 if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
713 dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
717 st->startup_time = prop;
720 of_property_read_u32(node, "atmel,adc-sample-hold-time", &prop);
721 st->sample_hold_time = prop;
723 if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
724 dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
730 ret = at91_adc_of_get_resolution(st, pdev);
734 st->registers = &st->caps->registers;
735 st->num_channels = st->caps->num_channels;
736 st->trigger_number = of_get_child_count(node);
737 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
738 sizeof(struct at91_adc_trigger),
740 if (!st->trigger_list) {
741 dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
746 for_each_child_of_node(node, trig_node) {
747 struct at91_adc_trigger *trig = st->trigger_list + i;
750 if (of_property_read_string(trig_node, "trigger-name", &name)) {
751 dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
757 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
758 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
763 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
767 /* Check if touchscreen is supported. */
768 if (st->caps->has_ts)
769 return at91_adc_probe_dt_ts(node, st, &idev->dev);
771 dev_info(&idev->dev, "not support touchscreen in the adc compatible string.\n");
779 static int at91_adc_probe_pdata(struct at91_adc_state *st,
780 struct platform_device *pdev)
782 struct at91_adc_data *pdata = pdev->dev.platform_data;
787 st->caps = (struct at91_adc_caps *)
788 platform_get_device_id(pdev)->driver_data;
790 st->use_external = pdata->use_external_triggers;
791 st->vref_mv = pdata->vref;
792 st->channels_mask = pdata->channels_used;
793 st->num_channels = st->caps->num_channels;
794 st->startup_time = pdata->startup_time;
795 st->trigger_number = pdata->trigger_number;
796 st->trigger_list = pdata->trigger_list;
797 st->registers = &st->caps->registers;
802 static const struct iio_info at91_adc_info = {
803 .driver_module = THIS_MODULE,
804 .read_raw = &at91_adc_read_raw,
807 /* Touchscreen related functions */
808 static int atmel_ts_open(struct input_dev *dev)
810 struct at91_adc_state *st = input_get_drvdata(dev);
812 at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
816 static void atmel_ts_close(struct input_dev *dev)
818 struct at91_adc_state *st = input_get_drvdata(dev);
820 at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
823 static int at91_ts_hw_init(struct at91_adc_state *st, u32 adc_clk_khz)
828 if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
829 reg = AT91_ADC_TSMR_TSMODE_4WIRE_PRESS;
831 reg = AT91_ADC_TSMR_TSMODE_5WIRE;
833 /* a Pen Detect Debounce Time is necessary for the ADC Touch to avoid
835 * The formula is : Pen Detect Debounce Time = (2 ^ pendbc) / ADCClock
837 pendbc = round_up(TOUCH_PEN_DETECT_DEBOUNCE_US * adc_clk_khz / 1000, 1);
839 while (pendbc >> ++i)
840 ; /* Empty! Find the shift offset */
841 if (abs(pendbc - (1 << i)) < abs(pendbc - (1 << (i - 1))))
846 if (st->caps->has_tsmr) {
847 reg |= AT91_ADC_TSMR_TSAV_(st->caps->ts_filter_average)
848 & AT91_ADC_TSMR_TSAV;
849 reg |= AT91_ADC_TSMR_PENDBC_(pendbc) & AT91_ADC_TSMR_PENDBC;
850 reg |= AT91_ADC_TSMR_NOTSDMA;
851 reg |= AT91_ADC_TSMR_PENDET_ENA;
852 reg |= 0x03 << 8; /* TSFREQ, need bigger than TSAV */
854 at91_adc_writel(st, AT91_ADC_TSMR, reg);
856 /* TODO: for 9g45 which has no TSMR */
859 /* Change adc internal resistor value for better pen detection,
860 * default value is 100 kOhm.
861 * 0 = 200 kOhm, 1 = 150 kOhm, 2 = 100 kOhm, 3 = 50 kOhm
862 * option only available on ES2 and higher
864 at91_adc_writel(st, AT91_ADC_ACR, st->caps->ts_pen_detect_sensitivity
865 & AT91_ADC_ACR_PENDETSENS);
867 /* Sample Peroid Time = (TRGPER + 1) / ADCClock */
868 st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US *
869 adc_clk_khz / 1000) - 1, 1);
874 static int at91_ts_register(struct at91_adc_state *st,
875 struct platform_device *pdev)
877 struct input_dev *input;
878 struct iio_dev *idev = iio_priv_to_dev(st);
881 input = input_allocate_device();
883 dev_err(&idev->dev, "Failed to allocate TS device!\n");
887 input->name = DRIVER_NAME;
888 input->id.bustype = BUS_HOST;
889 input->dev.parent = &pdev->dev;
890 input->open = atmel_ts_open;
891 input->close = atmel_ts_close;
893 __set_bit(EV_ABS, input->evbit);
894 __set_bit(EV_KEY, input->evbit);
895 __set_bit(BTN_TOUCH, input->keybit);
896 input_set_abs_params(input, ABS_X, 0, (1 << MAX_POS_BITS) - 1, 0, 0);
897 input_set_abs_params(input, ABS_Y, 0, (1 << MAX_POS_BITS) - 1, 0, 0);
898 input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);
900 st->ts_input = input;
901 input_set_drvdata(input, st);
903 ret = input_register_device(input);
905 input_free_device(st->ts_input);
910 static void at91_ts_unregister(struct at91_adc_state *st)
912 input_unregister_device(st->ts_input);
915 static int at91_adc_probe(struct platform_device *pdev)
917 unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim;
919 struct iio_dev *idev;
920 struct at91_adc_state *st;
921 struct resource *res;
924 idev = devm_iio_device_alloc(&pdev->dev, sizeof(struct at91_adc_state));
930 if (pdev->dev.of_node)
931 ret = at91_adc_probe_dt(st, pdev);
933 ret = at91_adc_probe_pdata(st, pdev);
936 dev_err(&pdev->dev, "No platform data available.\n");
940 platform_set_drvdata(pdev, idev);
942 idev->dev.parent = &pdev->dev;
943 idev->name = dev_name(&pdev->dev);
944 idev->modes = INDIO_DIRECT_MODE;
945 idev->info = &at91_adc_info;
947 st->irq = platform_get_irq(pdev, 0);
949 dev_err(&pdev->dev, "No IRQ ID is designated\n");
953 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
955 st->reg_base = devm_ioremap_resource(&pdev->dev, res);
956 if (IS_ERR(st->reg_base)) {
957 return PTR_ERR(st->reg_base);
961 * Disable all IRQs before setting up the handler
963 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
964 at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
965 ret = request_irq(st->irq,
968 pdev->dev.driver->name,
971 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
975 st->clk = devm_clk_get(&pdev->dev, "adc_clk");
976 if (IS_ERR(st->clk)) {
977 dev_err(&pdev->dev, "Failed to get the clock.\n");
978 ret = PTR_ERR(st->clk);
982 ret = clk_prepare_enable(st->clk);
985 "Could not prepare or enable the clock.\n");
989 st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
990 if (IS_ERR(st->adc_clk)) {
991 dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
992 ret = PTR_ERR(st->adc_clk);
993 goto error_disable_clk;
996 ret = clk_prepare_enable(st->adc_clk);
999 "Could not prepare or enable the ADC clock.\n");
1000 goto error_disable_clk;
1004 * Prescaler rate computation using the formula from the Atmel's
1005 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
1006 * specified by the electrical characteristics of the board.
1008 mstrclk = clk_get_rate(st->clk);
1009 adc_clk = clk_get_rate(st->adc_clk);
1010 adc_clk_khz = adc_clk / 1000;
1012 dev_dbg(&pdev->dev, "Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n",
1015 prsc = (mstrclk / (2 * adc_clk)) - 1;
1017 if (!st->startup_time) {
1018 dev_err(&pdev->dev, "No startup time available.\n");
1020 goto error_disable_adc_clk;
1022 ticks = (*st->caps->calc_startup_ticks)(st->startup_time, adc_clk_khz);
1025 * a minimal Sample and Hold Time is necessary for the ADC to guarantee
1026 * the best converted final value between two channels selection
1027 * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
1029 if (st->sample_hold_time > 0)
1030 shtim = round_up((st->sample_hold_time * adc_clk_khz / 1000)
1035 reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask;
1036 reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask;
1038 reg |= AT91_ADC_LOWRES;
1040 reg |= AT91_ADC_SLEEP;
1041 reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
1042 at91_adc_writel(st, AT91_ADC_MR, reg);
1044 /* Setup the ADC channels available on the board */
1045 ret = at91_adc_channel_init(idev);
1047 dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
1048 goto error_disable_adc_clk;
1051 init_waitqueue_head(&st->wq_data_avail);
1052 mutex_init(&st->lock);
1055 * Since touch screen will set trigger register as period trigger. So
1056 * when touch screen is enabled, then we have to disable hardware
1057 * trigger for classic adc.
1059 if (!st->touchscreen_type) {
1060 ret = at91_adc_buffer_init(idev);
1062 dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
1063 goto error_disable_adc_clk;
1066 ret = at91_adc_trigger_init(idev);
1068 dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
1069 at91_adc_buffer_remove(idev);
1070 goto error_disable_adc_clk;
1073 if (!st->caps->has_tsmr) {
1074 dev_err(&pdev->dev, "We don't support non-TSMR adc\n");
1076 goto error_disable_adc_clk;
1079 ret = at91_ts_register(st, pdev);
1081 goto error_disable_adc_clk;
1083 at91_ts_hw_init(st, adc_clk_khz);
1086 ret = iio_device_register(idev);
1088 dev_err(&pdev->dev, "Couldn't register the device.\n");
1089 goto error_iio_device_register;
1094 error_iio_device_register:
1095 if (!st->touchscreen_type) {
1096 at91_adc_trigger_remove(idev);
1097 at91_adc_buffer_remove(idev);
1099 at91_ts_unregister(st);
1101 error_disable_adc_clk:
1102 clk_disable_unprepare(st->adc_clk);
1104 clk_disable_unprepare(st->clk);
1106 free_irq(st->irq, idev);
1110 static int at91_adc_remove(struct platform_device *pdev)
1112 struct iio_dev *idev = platform_get_drvdata(pdev);
1113 struct at91_adc_state *st = iio_priv(idev);
1115 iio_device_unregister(idev);
1116 if (!st->touchscreen_type) {
1117 at91_adc_trigger_remove(idev);
1118 at91_adc_buffer_remove(idev);
1120 at91_ts_unregister(st);
1122 clk_disable_unprepare(st->adc_clk);
1123 clk_disable_unprepare(st->clk);
1124 free_irq(st->irq, idev);
1129 static struct at91_adc_caps at91sam9260_caps = {
1130 .calc_startup_ticks = calc_startup_ticks_9260,
1133 .channel_base = AT91_ADC_CHR(0),
1134 .drdy_mask = AT91_ADC_DRDY,
1135 .status_register = AT91_ADC_SR,
1136 .trigger_register = AT91_ADC_TRGR_9260,
1137 .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
1138 .mr_startup_mask = AT91_ADC_STARTUP_9260,
1142 static struct at91_adc_caps at91sam9g45_caps = {
1144 .calc_startup_ticks = calc_startup_ticks_9260, /* same as 9260 */
1147 .channel_base = AT91_ADC_CHR(0),
1148 .drdy_mask = AT91_ADC_DRDY,
1149 .status_register = AT91_ADC_SR,
1150 .trigger_register = AT91_ADC_TRGR_9G45,
1151 .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
1152 .mr_startup_mask = AT91_ADC_STARTUP_9G45,
1156 static struct at91_adc_caps at91sam9x5_caps = {
1159 .ts_filter_average = 3,
1160 .ts_pen_detect_sensitivity = 2,
1161 .calc_startup_ticks = calc_startup_ticks_9x5,
1164 .channel_base = AT91_ADC_CDR0_9X5,
1165 .drdy_mask = AT91_ADC_SR_DRDY_9X5,
1166 .status_register = AT91_ADC_SR_9X5,
1167 .trigger_register = AT91_ADC_TRGR_9X5,
1168 /* prescal mask is same as 9G45 */
1169 .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
1170 .mr_startup_mask = AT91_ADC_STARTUP_9X5,
1174 static const struct of_device_id at91_adc_dt_ids[] = {
1175 { .compatible = "atmel,at91sam9260-adc", .data = &at91sam9260_caps },
1176 { .compatible = "atmel,at91sam9g45-adc", .data = &at91sam9g45_caps },
1177 { .compatible = "atmel,at91sam9x5-adc", .data = &at91sam9x5_caps },
1180 MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
1182 static const struct platform_device_id at91_adc_ids[] = {
1184 .name = "at91sam9260-adc",
1185 .driver_data = (unsigned long)&at91sam9260_caps,
1187 .name = "at91sam9g45-adc",
1188 .driver_data = (unsigned long)&at91sam9g45_caps,
1190 .name = "at91sam9x5-adc",
1191 .driver_data = (unsigned long)&at91sam9x5_caps,
1196 MODULE_DEVICE_TABLE(platform, at91_adc_ids);
1198 static struct platform_driver at91_adc_driver = {
1199 .probe = at91_adc_probe,
1200 .remove = at91_adc_remove,
1201 .id_table = at91_adc_ids,
1203 .name = DRIVER_NAME,
1204 .of_match_table = of_match_ptr(at91_adc_dt_ids),
1208 module_platform_driver(at91_adc_driver);
1210 MODULE_LICENSE("GPL");
1211 MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
1212 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");