Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mxs-lradc.c
Go to the documentation of this file.
1 /*
2  * Freescale i.MX28 LRADC driver
3  *
4  * Copyright (c) 2012 DENX Software Engineering, GmbH.
5  * Marek Vasut <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  */
17 
18 #include <linux/interrupt.h>
19 #include <linux/device.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/sysfs.h>
25 #include <linux/list.h>
26 #include <linux/io.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/spinlock.h>
30 #include <linux/wait.h>
31 #include <linux/sched.h>
32 #include <linux/stmp_device.h>
33 #include <linux/bitops.h>
34 #include <linux/completion.h>
35 
36 #include <mach/mxs.h>
37 #include <mach/common.h>
38 
39 #include <linux/iio/iio.h>
40 #include <linux/iio/buffer.h>
41 #include <linux/iio/trigger.h>
44 
45 #define DRIVER_NAME "mxs-lradc"
46 
47 #define LRADC_MAX_DELAY_CHANS 4
48 #define LRADC_MAX_MAPPED_CHANS 8
49 #define LRADC_MAX_TOTAL_CHANS 16
50 
51 #define LRADC_DELAY_TIMER_HZ 2000
52 
53 /*
54  * Make this runtime configurable if necessary. Currently, if the buffered mode
55  * is enabled, the LRADC takes LRADC_DELAY_TIMER_LOOP samples of data before
56  * triggering IRQ. The sampling happens every (LRADC_DELAY_TIMER_PER / 2000)
57  * seconds. The result is that the samples arrive every 500mS.
58  */
59 #define LRADC_DELAY_TIMER_PER 200
60 #define LRADC_DELAY_TIMER_LOOP 5
61 
62 static const char * const mxs_lradc_irq_name[] = {
63  "mxs-lradc-touchscreen",
64  "mxs-lradc-thresh0",
65  "mxs-lradc-thresh1",
66  "mxs-lradc-channel0",
67  "mxs-lradc-channel1",
68  "mxs-lradc-channel2",
69  "mxs-lradc-channel3",
70  "mxs-lradc-channel4",
71  "mxs-lradc-channel5",
72  "mxs-lradc-channel6",
73  "mxs-lradc-channel7",
74  "mxs-lradc-button0",
75  "mxs-lradc-button1",
76 };
77 
81 };
82 
83 struct mxs_lradc {
84  struct device *dev;
85  void __iomem *base;
86  int irq[13];
87 
89  struct iio_trigger *trig;
90 
91  struct mutex lock;
92 
94 
96 };
97 
98 #define LRADC_CTRL0 0x00
99 #define LRADC_CTRL0_TOUCH_DETECT_ENABLE (1 << 23)
100 #define LRADC_CTRL0_TOUCH_SCREEN_TYPE (1 << 22)
101 
102 #define LRADC_CTRL1 0x10
103 #define LRADC_CTRL1_LRADC_IRQ(n) (1 << (n))
104 #define LRADC_CTRL1_LRADC_IRQ_MASK 0x1fff
105 #define LRADC_CTRL1_LRADC_IRQ_EN(n) (1 << ((n) + 16))
106 #define LRADC_CTRL1_LRADC_IRQ_EN_MASK (0x1fff << 16)
107 
108 #define LRADC_CTRL2 0x20
109 #define LRADC_CTRL2_TEMPSENSE_PWD (1 << 15)
110 
111 #define LRADC_CH(n) (0x50 + (0x10 * (n)))
112 #define LRADC_CH_ACCUMULATE (1 << 29)
113 #define LRADC_CH_NUM_SAMPLES_MASK (0x1f << 24)
114 #define LRADC_CH_NUM_SAMPLES_OFFSET 24
115 #define LRADC_CH_VALUE_MASK 0x3ffff
116 #define LRADC_CH_VALUE_OFFSET 0
117 
118 #define LRADC_DELAY(n) (0xd0 + (0x10 * (n)))
119 #define LRADC_DELAY_TRIGGER_LRADCS_MASK (0xff << 24)
120 #define LRADC_DELAY_TRIGGER_LRADCS_OFFSET 24
121 #define LRADC_DELAY_KICK (1 << 20)
122 #define LRADC_DELAY_TRIGGER_DELAYS_MASK (0xf << 16)
123 #define LRADC_DELAY_TRIGGER_DELAYS_OFFSET 16
124 #define LRADC_DELAY_LOOP_COUNT_MASK (0x1f << 11)
125 #define LRADC_DELAY_LOOP_COUNT_OFFSET 11
126 #define LRADC_DELAY_DELAY_MASK 0x7ff
127 #define LRADC_DELAY_DELAY_OFFSET 0
128 
129 #define LRADC_CTRL4 0x140
130 #define LRADC_CTRL4_LRADCSELECT_MASK(n) (0xf << ((n) * 4))
131 #define LRADC_CTRL4_LRADCSELECT_OFFSET(n) ((n) * 4)
132 
133 /*
134  * Raw I/O operations
135  */
136 static int mxs_lradc_read_raw(struct iio_dev *iio_dev,
137  const struct iio_chan_spec *chan,
138  int *val, int *val2, long m)
139 {
140  struct mxs_lradc *lradc = iio_priv(iio_dev);
141  int ret;
142 
143  if (m != IIO_CHAN_INFO_RAW)
144  return -EINVAL;
145 
146  /* Check for invalid channel */
147  if (chan->channel > LRADC_MAX_TOTAL_CHANS)
148  return -EINVAL;
149 
150  /*
151  * See if there is no buffered operation in progess. If there is, simply
152  * bail out. This can be improved to support both buffered and raw IO at
153  * the same time, yet the code becomes horribly complicated. Therefore I
154  * applied KISS principle here.
155  */
156  ret = mutex_trylock(&lradc->lock);
157  if (!ret)
158  return -EBUSY;
159 
160  INIT_COMPLETION(lradc->completion);
161 
162  /*
163  * No buffered operation in progress, map the channel and trigger it.
164  * Virtual channel 0 is always used here as the others are always not
165  * used if doing raw sampling.
166  */
169  writel(0xff, lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
170 
171  writel(chan->channel, lradc->base + LRADC_CTRL4);
172  writel(0, lradc->base + LRADC_CH(0));
173 
174  /* Enable the IRQ and start sampling the channel. */
177  writel(1 << 0, lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET);
178 
179  /* Wait for completion on the channel, 1 second max. */
181  if (!ret)
182  ret = -ETIMEDOUT;
183  if (ret < 0)
184  goto err;
185 
186  /* Read the data. */
187  *val = readl(lradc->base + LRADC_CH(0)) & LRADC_CH_VALUE_MASK;
188  ret = IIO_VAL_INT;
189 
190 err:
193 
194  mutex_unlock(&lradc->lock);
195 
196  return ret;
197 }
198 
199 static const struct iio_info mxs_lradc_iio_info = {
200  .driver_module = THIS_MODULE,
201  .read_raw = mxs_lradc_read_raw,
202 };
203 
204 /*
205  * IRQ Handling
206  */
207 static irqreturn_t mxs_lradc_handle_irq(int irq, void *data)
208 {
209  struct iio_dev *iio = data;
210  struct mxs_lradc *lradc = iio_priv(iio);
211  unsigned long reg = readl(lradc->base + LRADC_CTRL1);
212 
213  if (!(reg & LRADC_CTRL1_LRADC_IRQ_MASK))
214  return IRQ_NONE;
215 
216  /*
217  * Touchscreen IRQ handling code shall probably have priority
218  * and therefore shall be placed here.
219  */
220 
221  if (iio_buffer_enabled(iio))
222  iio_trigger_poll(iio->trig, iio_get_time_ns());
223  else if (reg & LRADC_CTRL1_LRADC_IRQ(0))
224  complete(&lradc->completion);
225 
226  writel(reg & LRADC_CTRL1_LRADC_IRQ_MASK,
228 
229  return IRQ_HANDLED;
230 }
231 
232 /*
233  * Trigger handling
234  */
235 static irqreturn_t mxs_lradc_trigger_handler(int irq, void *p)
236 {
237  struct iio_poll_func *pf = p;
238  struct iio_dev *iio = pf->indio_dev;
239  struct mxs_lradc *lradc = iio_priv(iio);
240  struct iio_buffer *buffer = iio->buffer;
241  const uint32_t chan_value = LRADC_CH_ACCUMULATE |
243  int i, j = 0;
244 
246  lradc->buffer[j] = readl(lradc->base + LRADC_CH(j));
247  writel(chan_value, lradc->base + LRADC_CH(j));
248  lradc->buffer[j] &= LRADC_CH_VALUE_MASK;
249  lradc->buffer[j] /= LRADC_DELAY_TIMER_LOOP;
250  j++;
251  }
252 
253  if (iio->scan_timestamp) {
254  s64 *timestamp = (s64 *)((u8 *)lradc->buffer +
255  ALIGN(j, sizeof(s64)));
256  *timestamp = pf->timestamp;
257  }
258 
259  iio_push_to_buffer(buffer, (u8 *)lradc->buffer);
260 
262 
263  return IRQ_HANDLED;
264 }
265 
266 static int mxs_lradc_configure_trigger(struct iio_trigger *trig, bool state)
267 {
268  struct iio_dev *iio = trig->private_data;
269  struct mxs_lradc *lradc = iio_priv(iio);
271 
272  writel(LRADC_DELAY_KICK, lradc->base + LRADC_DELAY(0) + st);
273 
274  return 0;
275 }
276 
277 static const struct iio_trigger_ops mxs_lradc_trigger_ops = {
278  .owner = THIS_MODULE,
279  .set_trigger_state = &mxs_lradc_configure_trigger,
280 };
281 
282 static int mxs_lradc_trigger_init(struct iio_dev *iio)
283 {
284  int ret;
285  struct iio_trigger *trig;
286 
287  trig = iio_trigger_alloc("%s-dev%i", iio->name, iio->id);
288  if (trig == NULL)
289  return -ENOMEM;
290 
291  trig->dev.parent = iio->dev.parent;
292  trig->private_data = iio;
293  trig->ops = &mxs_lradc_trigger_ops;
294 
295  ret = iio_trigger_register(trig);
296  if (ret) {
297  iio_trigger_free(trig);
298  return ret;
299  }
300 
301  iio->trig = trig;
302 
303  return 0;
304 }
305 
306 static void mxs_lradc_trigger_remove(struct iio_dev *iio)
307 {
309  iio_trigger_free(iio->trig);
310 }
311 
312 static int mxs_lradc_buffer_preenable(struct iio_dev *iio)
313 {
314  struct mxs_lradc *lradc = iio_priv(iio);
315  struct iio_buffer *buffer = iio->buffer;
316  int ret = 0, chan, ofs = 0, enable = 0;
317  uint32_t ctrl4 = 0;
318  uint32_t ctrl1_irq = 0;
319  const uint32_t chan_value = LRADC_CH_ACCUMULATE |
321  const int len = bitmap_weight(buffer->scan_mask, LRADC_MAX_TOTAL_CHANS);
322 
323  if (!len)
324  return -EINVAL;
325 
326  /*
327  * Lock the driver so raw access can not be done during buffered
328  * operation. This simplifies the code a lot.
329  */
330  ret = mutex_trylock(&lradc->lock);
331  if (!ret)
332  return -EBUSY;
333 
334  lradc->buffer = kmalloc(len * sizeof(*lradc->buffer), GFP_KERNEL);
335  if (!lradc->buffer) {
336  ret = -ENOMEM;
337  goto err_mem;
338  }
339 
340  ret = iio_sw_buffer_preenable(iio);
341  if (ret < 0)
342  goto err_buf;
343 
346  writel(0xff, lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
347 
348  for_each_set_bit(chan, buffer->scan_mask, LRADC_MAX_TOTAL_CHANS) {
349  ctrl4 |= chan << LRADC_CTRL4_LRADCSELECT_OFFSET(ofs);
350  ctrl1_irq |= LRADC_CTRL1_LRADC_IRQ_EN(ofs);
351  writel(chan_value, lradc->base + LRADC_CH(ofs));
352  enable |= 1 << ofs;
353  ofs++;
354  };
355 
357  lradc->base + LRADC_DELAY(0) + STMP_OFFSET_REG_CLR);
358 
359  writel(ctrl4, lradc->base + LRADC_CTRL4);
360  writel(ctrl1_irq, lradc->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET);
361 
363  lradc->base + LRADC_DELAY(0) + STMP_OFFSET_REG_SET);
364 
365  return 0;
366 
367 err_buf:
368  kfree(lradc->buffer);
369 err_mem:
370  mutex_unlock(&lradc->lock);
371  return ret;
372 }
373 
374 static int mxs_lradc_buffer_postdisable(struct iio_dev *iio)
375 {
376  struct mxs_lradc *lradc = iio_priv(iio);
377 
379  lradc->base + LRADC_DELAY(0) + STMP_OFFSET_REG_CLR);
380 
381  writel(0xff, lradc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
384 
385  kfree(lradc->buffer);
386  mutex_unlock(&lradc->lock);
387 
388  return 0;
389 }
390 
391 static bool mxs_lradc_validate_scan_mask(struct iio_dev *iio,
392  const unsigned long *mask)
393 {
394  const int mw = bitmap_weight(mask, iio->masklength);
395 
396  return mw <= LRADC_MAX_MAPPED_CHANS;
397 }
398 
399 static const struct iio_buffer_setup_ops mxs_lradc_buffer_ops = {
400  .preenable = &mxs_lradc_buffer_preenable,
401  .postenable = &iio_triggered_buffer_postenable,
402  .predisable = &iio_triggered_buffer_predisable,
403  .postdisable = &mxs_lradc_buffer_postdisable,
404  .validate_scan_mask = &mxs_lradc_validate_scan_mask,
405 };
406 
407 /*
408  * Driver initialization
409  */
410 
411 #define MXS_ADC_CHAN(idx, chan_type) { \
412  .type = (chan_type), \
413  .indexed = 1, \
414  .scan_index = (idx), \
415  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT, \
416  .channel = (idx), \
417  .scan_type = { \
418  .sign = 'u', \
419  .realbits = 18, \
420  .storagebits = 32, \
421  }, \
422 }
423 
424 static const struct iio_chan_spec mxs_lradc_chan_spec[] = {
432  MXS_ADC_CHAN(7, IIO_VOLTAGE), /* VBATT */
433  MXS_ADC_CHAN(8, IIO_TEMP), /* Temp sense 0 */
434  MXS_ADC_CHAN(9, IIO_TEMP), /* Temp sense 1 */
435  MXS_ADC_CHAN(10, IIO_VOLTAGE), /* VDDIO */
436  MXS_ADC_CHAN(11, IIO_VOLTAGE), /* VTH */
437  MXS_ADC_CHAN(12, IIO_VOLTAGE), /* VDDA */
438  MXS_ADC_CHAN(13, IIO_VOLTAGE), /* VDDD */
439  MXS_ADC_CHAN(14, IIO_VOLTAGE), /* VBG */
440  MXS_ADC_CHAN(15, IIO_VOLTAGE), /* VDD5V */
441 };
442 
443 static void mxs_lradc_hw_init(struct mxs_lradc *lradc)
444 {
445  int i;
446  const uint32_t cfg =
448 
449  stmp_reset_block(lradc->base);
450 
451  for (i = 0; i < LRADC_MAX_DELAY_CHANS; i++)
452  writel(cfg | (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET + i)),
453  lradc->base + LRADC_DELAY(i));
454 
455  /* Start internal temperature sensing. */
456  writel(0, lradc->base + LRADC_CTRL2);
457 }
458 
459 static void mxs_lradc_hw_stop(struct mxs_lradc *lradc)
460 {
461  int i;
462 
465 
466  for (i = 0; i < LRADC_MAX_DELAY_CHANS; i++)
467  writel(0, lradc->base + LRADC_DELAY(i));
468 }
469 
470 static int __devinit mxs_lradc_probe(struct platform_device *pdev)
471 {
472  struct device *dev = &pdev->dev;
473  struct mxs_lradc *lradc;
474  struct iio_dev *iio;
475  struct resource *iores;
476  int ret = 0;
477  int i;
478 
479  /* Allocate the IIO device. */
480  iio = iio_device_alloc(sizeof(*lradc));
481  if (!iio) {
482  dev_err(dev, "Failed to allocate IIO device\n");
483  return -ENOMEM;
484  }
485 
486  lradc = iio_priv(iio);
487 
488  /* Grab the memory area */
489  iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
490  lradc->dev = &pdev->dev;
491  lradc->base = devm_request_and_ioremap(dev, iores);
492  if (!lradc->base) {
493  ret = -EADDRNOTAVAIL;
494  goto err_addr;
495  }
496 
497  /* Grab all IRQ sources */
498  for (i = 0; i < 13; i++) {
499  lradc->irq[i] = platform_get_irq(pdev, i);
500  if (lradc->irq[i] < 0) {
501  ret = -EINVAL;
502  goto err_addr;
503  }
504 
505  ret = devm_request_irq(dev, lradc->irq[i],
506  mxs_lradc_handle_irq, 0,
507  mxs_lradc_irq_name[i], iio);
508  if (ret)
509  goto err_addr;
510  }
511 
512  platform_set_drvdata(pdev, iio);
513 
514  init_completion(&lradc->completion);
515  mutex_init(&lradc->lock);
516 
517  iio->name = pdev->name;
518  iio->dev.parent = &pdev->dev;
519  iio->info = &mxs_lradc_iio_info;
520  iio->modes = INDIO_DIRECT_MODE;
521  iio->channels = mxs_lradc_chan_spec;
522  iio->num_channels = ARRAY_SIZE(mxs_lradc_chan_spec);
523 
525  &mxs_lradc_trigger_handler,
526  &mxs_lradc_buffer_ops);
527  if (ret)
528  goto err_addr;
529 
530  ret = mxs_lradc_trigger_init(iio);
531  if (ret)
532  goto err_trig;
533 
534  /* Register IIO device. */
535  ret = iio_device_register(iio);
536  if (ret) {
537  dev_err(dev, "Failed to register IIO device\n");
538  goto err_dev;
539  }
540 
541  /* Configure the hardware. */
542  mxs_lradc_hw_init(lradc);
543 
544  return 0;
545 
546 err_dev:
547  mxs_lradc_trigger_remove(iio);
548 err_trig:
550 err_addr:
551  iio_device_free(iio);
552  return ret;
553 }
554 
555 static int __devexit mxs_lradc_remove(struct platform_device *pdev)
556 {
557  struct iio_dev *iio = platform_get_drvdata(pdev);
558  struct mxs_lradc *lradc = iio_priv(iio);
559 
560  mxs_lradc_hw_stop(lradc);
561 
564  mxs_lradc_trigger_remove(iio);
565  iio_device_free(iio);
566 
567  return 0;
568 }
569 
570 static const struct of_device_id mxs_lradc_dt_ids[] = {
571  { .compatible = "fsl,imx28-lradc", },
572  { /* sentinel */ }
573 };
574 MODULE_DEVICE_TABLE(of, mxs_lradc_dt_ids);
575 
576 static struct platform_driver mxs_lradc_driver = {
577  .driver = {
578  .name = DRIVER_NAME,
579  .owner = THIS_MODULE,
580  .of_match_table = mxs_lradc_dt_ids,
581  },
582  .probe = mxs_lradc_probe,
583  .remove = __devexit_p(mxs_lradc_remove),
584 };
585 
586 module_platform_driver(mxs_lradc_driver);
587 
588 MODULE_AUTHOR("Marek Vasut <[email protected]>");
589 MODULE_DESCRIPTION("Freescale i.MX28 LRADC driver");
590 MODULE_LICENSE("GPL v2");