at91_adc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. #include <linux/bitmap.h>
  9. #include <linux/bitops.h>
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/input.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/wait.h>
  24. #include <linux/platform_data/at91_adc.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/buffer.h>
  27. #include <linux/iio/trigger.h>
  28. #include <linux/iio/trigger_consumer.h>
  29. #include <linux/iio/triggered_buffer.h>
  30. #include <mach/at91_adc.h>
  31. #define AT91_ADC_CHAN(st, ch) \
  32. (st->registers->channel_base + (ch * 4))
  33. #define at91_adc_readl(st, reg) \
  34. (readl_relaxed(st->reg_base + reg))
  35. #define at91_adc_writel(st, reg, val) \
  36. (writel_relaxed(val, st->reg_base + reg))
  37. #define DRIVER_NAME "at91_adc"
  38. #define MAX_POS_BITS 12
  39. #define TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */
  40. #define TOUCH_PEN_DETECT_DEBOUNCE_US 200
  41. /**
  42. * struct at91_adc_reg_desc - Various informations relative to registers
  43. * @channel_base: Base offset for the channel data registers
  44. * @drdy_mask: Mask of the DRDY field in the relevant registers
  45. (Interruptions registers mostly)
  46. * @status_register: Offset of the Interrupt Status Register
  47. * @trigger_register: Offset of the Trigger setup register
  48. * @mr_prescal_mask: Mask of the PRESCAL field in the adc MR register
  49. * @mr_startup_mask: Mask of the STARTUP field in the adc MR register
  50. */
  51. struct at91_adc_reg_desc {
  52. u8 channel_base;
  53. u32 drdy_mask;
  54. u8 status_register;
  55. u8 trigger_register;
  56. u32 mr_prescal_mask;
  57. u32 mr_startup_mask;
  58. };
  59. struct at91_adc_caps {
  60. bool has_ts; /* Support touch screen */
  61. bool has_tsmr; /* only at91sam9x5, sama5d3 have TSMR reg */
  62. /*
  63. * Numbers of sampling data will be averaged. Can be 0~3.
  64. * Hardware can average (2 ^ ts_filter_average) sample data.
  65. */
  66. u8 ts_filter_average;
  67. /* Pen Detection input pull-up resistor, can be 0~3 */
  68. u8 ts_pen_detect_sensitivity;
  69. /* startup time calculate function */
  70. u32 (*calc_startup_ticks)(u8 startup_time, u32 adc_clk_khz);
  71. u8 num_channels;
  72. struct at91_adc_reg_desc registers;
  73. };
  74. enum atmel_adc_ts_type {
  75. ATMEL_ADC_TOUCHSCREEN_NONE = 0,
  76. ATMEL_ADC_TOUCHSCREEN_4WIRE = 4,
  77. ATMEL_ADC_TOUCHSCREEN_5WIRE = 5,
  78. };
  79. struct at91_adc_state {
  80. struct clk *adc_clk;
  81. u16 *buffer;
  82. unsigned long channels_mask;
  83. struct clk *clk;
  84. bool done;
  85. int irq;
  86. u16 last_value;
  87. struct mutex lock;
  88. u8 num_channels;
  89. void __iomem *reg_base;
  90. struct at91_adc_reg_desc *registers;
  91. u8 startup_time;
  92. u8 sample_hold_time;
  93. bool sleep_mode;
  94. struct iio_trigger **trig;
  95. struct at91_adc_trigger *trigger_list;
  96. u32 trigger_number;
  97. bool use_external;
  98. u32 vref_mv;
  99. u32 res; /* resolution used for convertions */
  100. bool low_res; /* the resolution corresponds to the lowest one */
  101. wait_queue_head_t wq_data_avail;
  102. struct at91_adc_caps *caps;
  103. /*
  104. * Following ADC channels are shared by touchscreen:
  105. *
  106. * CH0 -- Touch screen XP/UL
  107. * CH1 -- Touch screen XM/UR
  108. * CH2 -- Touch screen YP/LL
  109. * CH3 -- Touch screen YM/Sense
  110. * CH4 -- Touch screen LR(5-wire only)
  111. *
  112. * The bitfields below represents the reserved channel in the
  113. * touchscreen mode.
  114. */
  115. #define CHAN_MASK_TOUCHSCREEN_4WIRE (0xf << 0)
  116. #define CHAN_MASK_TOUCHSCREEN_5WIRE (0x1f << 0)
  117. enum atmel_adc_ts_type touchscreen_type;
  118. struct input_dev *ts_input;
  119. u16 ts_sample_period_val;
  120. u32 ts_pressure_threshold;
  121. };
  122. static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
  123. {
  124. struct iio_poll_func *pf = p;
  125. struct iio_dev *idev = pf->indio_dev;
  126. struct at91_adc_state *st = iio_priv(idev);
  127. int i, j = 0;
  128. for (i = 0; i < idev->masklength; i++) {
  129. if (!test_bit(i, idev->active_scan_mask))
  130. continue;
  131. st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
  132. j++;
  133. }
  134. iio_push_to_buffers_with_timestamp(idev, st->buffer, pf->timestamp);
  135. iio_trigger_notify_done(idev->trig);
  136. /* Needed to ACK the DRDY interruption */
  137. at91_adc_readl(st, AT91_ADC_LCDR);
  138. enable_irq(st->irq);
  139. return IRQ_HANDLED;
  140. }
  141. /* Handler for classic adc channel eoc trigger */
  142. void handle_adc_eoc_trigger(int irq, struct iio_dev *idev)
  143. {
  144. struct at91_adc_state *st = iio_priv(idev);
  145. if (iio_buffer_enabled(idev)) {
  146. disable_irq_nosync(irq);
  147. iio_trigger_poll(idev->trig, iio_get_time_ns());
  148. } else {
  149. st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
  150. st->done = true;
  151. wake_up_interruptible(&st->wq_data_avail);
  152. }
  153. }
  154. static int at91_ts_sample(struct at91_adc_state *st)
  155. {
  156. unsigned int xscale, yscale, reg, z1, z2;
  157. unsigned int x, y, pres, xpos, ypos;
  158. unsigned int rxp = 1;
  159. unsigned int factor = 1000;
  160. struct iio_dev *idev = iio_priv_to_dev(st);
  161. unsigned int xyz_mask_bits = st->res;
  162. unsigned int xyz_mask = (1 << xyz_mask_bits) - 1;
  163. /* calculate position */
  164. /* x position = (x / xscale) * max, max = 2^MAX_POS_BITS - 1 */
  165. reg = at91_adc_readl(st, AT91_ADC_TSXPOSR);
  166. xpos = reg & xyz_mask;
  167. x = (xpos << MAX_POS_BITS) - xpos;
  168. xscale = (reg >> 16) & xyz_mask;
  169. if (xscale == 0) {
  170. dev_err(&idev->dev, "Error: xscale == 0!\n");
  171. return -1;
  172. }
  173. x /= xscale;
  174. /* y position = (y / yscale) * max, max = 2^MAX_POS_BITS - 1 */
  175. reg = at91_adc_readl(st, AT91_ADC_TSYPOSR);
  176. ypos = reg & xyz_mask;
  177. y = (ypos << MAX_POS_BITS) - ypos;
  178. yscale = (reg >> 16) & xyz_mask;
  179. if (yscale == 0) {
  180. dev_err(&idev->dev, "Error: yscale == 0!\n");
  181. return -1;
  182. }
  183. y /= yscale;
  184. /* calculate the pressure */
  185. reg = at91_adc_readl(st, AT91_ADC_TSPRESSR);
  186. z1 = reg & xyz_mask;
  187. z2 = (reg >> 16) & xyz_mask;
  188. if (z1 != 0)
  189. pres = rxp * (x * factor / 1024) * (z2 * factor / z1 - factor)
  190. / factor;
  191. else
  192. pres = st->ts_pressure_threshold; /* no pen contacted */
  193. dev_dbg(&idev->dev, "xpos = %d, xscale = %d, ypos = %d, yscale = %d, z1 = %d, z2 = %d, press = %d\n",
  194. xpos, xscale, ypos, yscale, z1, z2, pres);
  195. if (pres < st->ts_pressure_threshold) {
  196. dev_dbg(&idev->dev, "x = %d, y = %d, pressure = %d\n",
  197. x, y, pres / factor);
  198. input_report_abs(st->ts_input, ABS_X, x);
  199. input_report_abs(st->ts_input, ABS_Y, y);
  200. input_report_abs(st->ts_input, ABS_PRESSURE, pres);
  201. input_report_key(st->ts_input, BTN_TOUCH, 1);
  202. input_sync(st->ts_input);
  203. } else {
  204. dev_dbg(&idev->dev, "pressure too low: not reporting\n");
  205. }
  206. return 0;
  207. }
  208. static irqreturn_t at91_adc_interrupt(int irq, void *private)
  209. {
  210. struct iio_dev *idev = private;
  211. struct at91_adc_state *st = iio_priv(idev);
  212. u32 status = at91_adc_readl(st, st->registers->status_register);
  213. const uint32_t ts_data_irq_mask =
  214. AT91_ADC_IER_XRDY |
  215. AT91_ADC_IER_YRDY |
  216. AT91_ADC_IER_PRDY;
  217. if (status & st->registers->drdy_mask)
  218. handle_adc_eoc_trigger(irq, idev);
  219. if (status & AT91_ADC_IER_PEN) {
  220. at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
  221. at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_NOPEN |
  222. ts_data_irq_mask);
  223. /* Set up period trigger for sampling */
  224. at91_adc_writel(st, st->registers->trigger_register,
  225. AT91_ADC_TRGR_MOD_PERIOD_TRIG |
  226. AT91_ADC_TRGR_TRGPER_(st->ts_sample_period_val));
  227. } else if (status & AT91_ADC_IER_NOPEN) {
  228. at91_adc_writel(st, st->registers->trigger_register, 0);
  229. at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_NOPEN |
  230. ts_data_irq_mask);
  231. at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
  232. input_report_key(st->ts_input, BTN_TOUCH, 0);
  233. input_sync(st->ts_input);
  234. } else if ((status & ts_data_irq_mask) == ts_data_irq_mask) {
  235. /* Now all touchscreen data is ready */
  236. if (status & AT91_ADC_ISR_PENS) {
  237. /* validate data by pen contact */
  238. at91_ts_sample(st);
  239. } else {
  240. /* triggered by event that is no pen contact, just read
  241. * them to clean the interrupt and discard all.
  242. */
  243. at91_adc_readl(st, AT91_ADC_TSXPOSR);
  244. at91_adc_readl(st, AT91_ADC_TSYPOSR);
  245. at91_adc_readl(st, AT91_ADC_TSPRESSR);
  246. }
  247. }
  248. return IRQ_HANDLED;
  249. }
  250. static int at91_adc_channel_init(struct iio_dev *idev)
  251. {
  252. struct at91_adc_state *st = iio_priv(idev);
  253. struct iio_chan_spec *chan_array, *timestamp;
  254. int bit, idx = 0;
  255. unsigned long rsvd_mask = 0;
  256. /* If touchscreen is enable, then reserve the adc channels */
  257. if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
  258. rsvd_mask = CHAN_MASK_TOUCHSCREEN_4WIRE;
  259. else if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_5WIRE)
  260. rsvd_mask = CHAN_MASK_TOUCHSCREEN_5WIRE;
  261. /* set up the channel mask to reserve touchscreen channels */
  262. st->channels_mask &= ~rsvd_mask;
  263. idev->num_channels = bitmap_weight(&st->channels_mask,
  264. st->num_channels) + 1;
  265. chan_array = devm_kzalloc(&idev->dev,
  266. ((idev->num_channels + 1) *
  267. sizeof(struct iio_chan_spec)),
  268. GFP_KERNEL);
  269. if (!chan_array)
  270. return -ENOMEM;
  271. for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
  272. struct iio_chan_spec *chan = chan_array + idx;
  273. chan->type = IIO_VOLTAGE;
  274. chan->indexed = 1;
  275. chan->channel = bit;
  276. chan->scan_index = idx;
  277. chan->scan_type.sign = 'u';
  278. chan->scan_type.realbits = st->res;
  279. chan->scan_type.storagebits = 16;
  280. chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
  281. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  282. idx++;
  283. }
  284. timestamp = chan_array + idx;
  285. timestamp->type = IIO_TIMESTAMP;
  286. timestamp->channel = -1;
  287. timestamp->scan_index = idx;
  288. timestamp->scan_type.sign = 's';
  289. timestamp->scan_type.realbits = 64;
  290. timestamp->scan_type.storagebits = 64;
  291. idev->channels = chan_array;
  292. return idev->num_channels;
  293. }
  294. static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
  295. struct at91_adc_trigger *triggers,
  296. const char *trigger_name)
  297. {
  298. struct at91_adc_state *st = iio_priv(idev);
  299. u8 value = 0;
  300. int i;
  301. for (i = 0; i < st->trigger_number; i++) {
  302. char *name = kasprintf(GFP_KERNEL,
  303. "%s-dev%d-%s",
  304. idev->name,
  305. idev->id,
  306. triggers[i].name);
  307. if (!name)
  308. return -ENOMEM;
  309. if (strcmp(trigger_name, name) == 0) {
  310. value = triggers[i].value;
  311. kfree(name);
  312. break;
  313. }
  314. kfree(name);
  315. }
  316. return value;
  317. }
  318. static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
  319. {
  320. struct iio_dev *idev = iio_trigger_get_drvdata(trig);
  321. struct at91_adc_state *st = iio_priv(idev);
  322. struct iio_buffer *buffer = idev->buffer;
  323. struct at91_adc_reg_desc *reg = st->registers;
  324. u32 status = at91_adc_readl(st, reg->trigger_register);
  325. u8 value;
  326. u8 bit;
  327. value = at91_adc_get_trigger_value_by_name(idev,
  328. st->trigger_list,
  329. idev->trig->name);
  330. if (value == 0)
  331. return -EINVAL;
  332. if (state) {
  333. st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
  334. if (st->buffer == NULL)
  335. return -ENOMEM;
  336. at91_adc_writel(st, reg->trigger_register,
  337. status | value);
  338. for_each_set_bit(bit, buffer->scan_mask,
  339. st->num_channels) {
  340. struct iio_chan_spec const *chan = idev->channels + bit;
  341. at91_adc_writel(st, AT91_ADC_CHER,
  342. AT91_ADC_CH(chan->channel));
  343. }
  344. at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
  345. } else {
  346. at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
  347. at91_adc_writel(st, reg->trigger_register,
  348. status & ~value);
  349. for_each_set_bit(bit, buffer->scan_mask,
  350. st->num_channels) {
  351. struct iio_chan_spec const *chan = idev->channels + bit;
  352. at91_adc_writel(st, AT91_ADC_CHDR,
  353. AT91_ADC_CH(chan->channel));
  354. }
  355. kfree(st->buffer);
  356. }
  357. return 0;
  358. }
  359. static const struct iio_trigger_ops at91_adc_trigger_ops = {
  360. .owner = THIS_MODULE,
  361. .set_trigger_state = &at91_adc_configure_trigger,
  362. };
  363. static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
  364. struct at91_adc_trigger *trigger)
  365. {
  366. struct iio_trigger *trig;
  367. int ret;
  368. trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
  369. idev->id, trigger->name);
  370. if (trig == NULL)
  371. return NULL;
  372. trig->dev.parent = idev->dev.parent;
  373. iio_trigger_set_drvdata(trig, idev);
  374. trig->ops = &at91_adc_trigger_ops;
  375. ret = iio_trigger_register(trig);
  376. if (ret)
  377. return NULL;
  378. return trig;
  379. }
  380. static int at91_adc_trigger_init(struct iio_dev *idev)
  381. {
  382. struct at91_adc_state *st = iio_priv(idev);
  383. int i, ret;
  384. st->trig = devm_kzalloc(&idev->dev,
  385. st->trigger_number * sizeof(*st->trig),
  386. GFP_KERNEL);
  387. if (st->trig == NULL) {
  388. ret = -ENOMEM;
  389. goto error_ret;
  390. }
  391. for (i = 0; i < st->trigger_number; i++) {
  392. if (st->trigger_list[i].is_external && !(st->use_external))
  393. continue;
  394. st->trig[i] = at91_adc_allocate_trigger(idev,
  395. st->trigger_list + i);
  396. if (st->trig[i] == NULL) {
  397. dev_err(&idev->dev,
  398. "Could not allocate trigger %d\n", i);
  399. ret = -ENOMEM;
  400. goto error_trigger;
  401. }
  402. }
  403. return 0;
  404. error_trigger:
  405. for (i--; i >= 0; i--) {
  406. iio_trigger_unregister(st->trig[i]);
  407. iio_trigger_free(st->trig[i]);
  408. }
  409. error_ret:
  410. return ret;
  411. }
  412. static void at91_adc_trigger_remove(struct iio_dev *idev)
  413. {
  414. struct at91_adc_state *st = iio_priv(idev);
  415. int i;
  416. for (i = 0; i < st->trigger_number; i++) {
  417. iio_trigger_unregister(st->trig[i]);
  418. iio_trigger_free(st->trig[i]);
  419. }
  420. }
  421. static int at91_adc_buffer_init(struct iio_dev *idev)
  422. {
  423. return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
  424. &at91_adc_trigger_handler, NULL);
  425. }
  426. static void at91_adc_buffer_remove(struct iio_dev *idev)
  427. {
  428. iio_triggered_buffer_cleanup(idev);
  429. }
  430. static int at91_adc_read_raw(struct iio_dev *idev,
  431. struct iio_chan_spec const *chan,
  432. int *val, int *val2, long mask)
  433. {
  434. struct at91_adc_state *st = iio_priv(idev);
  435. int ret;
  436. switch (mask) {
  437. case IIO_CHAN_INFO_RAW:
  438. mutex_lock(&st->lock);
  439. at91_adc_writel(st, AT91_ADC_CHER,
  440. AT91_ADC_CH(chan->channel));
  441. at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask);
  442. at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
  443. ret = wait_event_interruptible_timeout(st->wq_data_avail,
  444. st->done,
  445. msecs_to_jiffies(1000));
  446. if (ret == 0)
  447. ret = -ETIMEDOUT;
  448. if (ret < 0) {
  449. mutex_unlock(&st->lock);
  450. return ret;
  451. }
  452. *val = st->last_value;
  453. at91_adc_writel(st, AT91_ADC_CHDR,
  454. AT91_ADC_CH(chan->channel));
  455. at91_adc_writel(st, AT91_ADC_IDR, st->registers->drdy_mask);
  456. st->last_value = 0;
  457. st->done = false;
  458. mutex_unlock(&st->lock);
  459. return IIO_VAL_INT;
  460. case IIO_CHAN_INFO_SCALE:
  461. *val = st->vref_mv;
  462. *val2 = chan->scan_type.realbits;
  463. return IIO_VAL_FRACTIONAL_LOG2;
  464. default:
  465. break;
  466. }
  467. return -EINVAL;
  468. }
  469. static int at91_adc_of_get_resolution(struct at91_adc_state *st,
  470. struct platform_device *pdev)
  471. {
  472. struct iio_dev *idev = iio_priv_to_dev(st);
  473. struct device_node *np = pdev->dev.of_node;
  474. int count, i, ret = 0;
  475. char *res_name, *s;
  476. u32 *resolutions;
  477. count = of_property_count_strings(np, "atmel,adc-res-names");
  478. if (count < 2) {
  479. dev_err(&idev->dev, "You must specified at least two resolution names for "
  480. "adc-res-names property in the DT\n");
  481. return count;
  482. }
  483. resolutions = kmalloc(count * sizeof(*resolutions), GFP_KERNEL);
  484. if (!resolutions)
  485. return -ENOMEM;
  486. if (of_property_read_u32_array(np, "atmel,adc-res", resolutions, count)) {
  487. dev_err(&idev->dev, "Missing adc-res property in the DT.\n");
  488. ret = -ENODEV;
  489. goto ret;
  490. }
  491. if (of_property_read_string(np, "atmel,adc-use-res", (const char **)&res_name))
  492. res_name = "highres";
  493. for (i = 0; i < count; i++) {
  494. if (of_property_read_string_index(np, "atmel,adc-res-names", i, (const char **)&s))
  495. continue;
  496. if (strcmp(res_name, s))
  497. continue;
  498. st->res = resolutions[i];
  499. if (!strcmp(res_name, "lowres"))
  500. st->low_res = true;
  501. else
  502. st->low_res = false;
  503. dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
  504. goto ret;
  505. }
  506. dev_err(&idev->dev, "There is no resolution for %s\n", res_name);
  507. ret:
  508. kfree(resolutions);
  509. return ret;
  510. }
  511. static u32 calc_startup_ticks_9260(u8 startup_time, u32 adc_clk_khz)
  512. {
  513. /*
  514. * Number of ticks needed to cover the startup time of the ADC
  515. * as defined in the electrical characteristics of the board,
  516. * divided by 8. The formula thus is :
  517. * Startup Time = (ticks + 1) * 8 / ADC Clock
  518. */
  519. return round_up((startup_time * adc_clk_khz / 1000) - 1, 8) / 8;
  520. }
  521. static u32 calc_startup_ticks_9x5(u8 startup_time, u32 adc_clk_khz)
  522. {
  523. /*
  524. * For sama5d3x and at91sam9x5, the formula changes to:
  525. * Startup Time = <lookup_table_value> / ADC Clock
  526. */
  527. const int startup_lookup[] = {
  528. 0 , 8 , 16 , 24 ,
  529. 64 , 80 , 96 , 112,
  530. 512, 576, 640, 704,
  531. 768, 832, 896, 960
  532. };
  533. int i, size = ARRAY_SIZE(startup_lookup);
  534. unsigned int ticks;
  535. ticks = startup_time * adc_clk_khz / 1000;
  536. for (i = 0; i < size; i++)
  537. if (ticks < startup_lookup[i])
  538. break;
  539. ticks = i;
  540. if (ticks == size)
  541. /* Reach the end of lookup table */
  542. ticks = size - 1;
  543. return ticks;
  544. }
  545. static const struct of_device_id at91_adc_dt_ids[];
  546. static int at91_adc_probe_dt_ts(struct device_node *node,
  547. struct at91_adc_state *st, struct device *dev)
  548. {
  549. int ret;
  550. u32 prop;
  551. ret = of_property_read_u32(node, "atmel,adc-ts-wires", &prop);
  552. if (ret) {
  553. dev_info(dev, "ADC Touch screen is disabled.\n");
  554. return 0;
  555. }
  556. switch (prop) {
  557. case 4:
  558. case 5:
  559. st->touchscreen_type = prop;
  560. break;
  561. default:
  562. dev_err(dev, "Unsupported number of touchscreen wires (%d). Should be 4 or 5.\n", prop);
  563. return -EINVAL;
  564. }
  565. prop = 0;
  566. of_property_read_u32(node, "atmel,adc-ts-pressure-threshold", &prop);
  567. st->ts_pressure_threshold = prop;
  568. if (st->ts_pressure_threshold) {
  569. return 0;
  570. } else {
  571. dev_err(dev, "Invalid pressure threshold for the touchscreen\n");
  572. return -EINVAL;
  573. }
  574. }
  575. static int at91_adc_probe_dt(struct at91_adc_state *st,
  576. struct platform_device *pdev)
  577. {
  578. struct iio_dev *idev = iio_priv_to_dev(st);
  579. struct device_node *node = pdev->dev.of_node;
  580. struct device_node *trig_node;
  581. int i = 0, ret;
  582. u32 prop;
  583. if (!node)
  584. return -EINVAL;
  585. st->caps = (struct at91_adc_caps *)
  586. of_match_device(at91_adc_dt_ids, &pdev->dev)->data;
  587. st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
  588. if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
  589. dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
  590. ret = -EINVAL;
  591. goto error_ret;
  592. }
  593. st->channels_mask = prop;
  594. st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
  595. if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
  596. dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
  597. ret = -EINVAL;
  598. goto error_ret;
  599. }
  600. st->startup_time = prop;
  601. prop = 0;
  602. of_property_read_u32(node, "atmel,adc-sample-hold-time", &prop);
  603. st->sample_hold_time = prop;
  604. if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
  605. dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
  606. ret = -EINVAL;
  607. goto error_ret;
  608. }
  609. st->vref_mv = prop;
  610. ret = at91_adc_of_get_resolution(st, pdev);
  611. if (ret)
  612. goto error_ret;
  613. st->registers = &st->caps->registers;
  614. st->num_channels = st->caps->num_channels;
  615. st->trigger_number = of_get_child_count(node);
  616. st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
  617. sizeof(struct at91_adc_trigger),
  618. GFP_KERNEL);
  619. if (!st->trigger_list) {
  620. dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
  621. ret = -ENOMEM;
  622. goto error_ret;
  623. }
  624. for_each_child_of_node(node, trig_node) {
  625. struct at91_adc_trigger *trig = st->trigger_list + i;
  626. const char *name;
  627. if (of_property_read_string(trig_node, "trigger-name", &name)) {
  628. dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
  629. ret = -EINVAL;
  630. goto error_ret;
  631. }
  632. trig->name = name;
  633. if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
  634. dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
  635. ret = -EINVAL;
  636. goto error_ret;
  637. }
  638. trig->value = prop;
  639. trig->is_external = of_property_read_bool(trig_node, "trigger-external");
  640. i++;
  641. }
  642. /* Check if touchscreen is supported. */
  643. if (st->caps->has_ts)
  644. return at91_adc_probe_dt_ts(node, st, &idev->dev);
  645. else
  646. dev_info(&idev->dev, "not support touchscreen in the adc compatible string.\n");
  647. return 0;
  648. error_ret:
  649. return ret;
  650. }
  651. static int at91_adc_probe_pdata(struct at91_adc_state *st,
  652. struct platform_device *pdev)
  653. {
  654. struct at91_adc_data *pdata = pdev->dev.platform_data;
  655. if (!pdata)
  656. return -EINVAL;
  657. st->caps = (struct at91_adc_caps *)
  658. platform_get_device_id(pdev)->driver_data;
  659. st->use_external = pdata->use_external_triggers;
  660. st->vref_mv = pdata->vref;
  661. st->channels_mask = pdata->channels_used;
  662. st->num_channels = st->caps->num_channels;
  663. st->startup_time = pdata->startup_time;
  664. st->trigger_number = pdata->trigger_number;
  665. st->trigger_list = pdata->trigger_list;
  666. st->registers = &st->caps->registers;
  667. return 0;
  668. }
  669. static const struct iio_info at91_adc_info = {
  670. .driver_module = THIS_MODULE,
  671. .read_raw = &at91_adc_read_raw,
  672. };
  673. /* Touchscreen related functions */
  674. static int atmel_ts_open(struct input_dev *dev)
  675. {
  676. struct at91_adc_state *st = input_get_drvdata(dev);
  677. at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
  678. return 0;
  679. }
  680. static void atmel_ts_close(struct input_dev *dev)
  681. {
  682. struct at91_adc_state *st = input_get_drvdata(dev);
  683. at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
  684. }
  685. static int at91_ts_hw_init(struct at91_adc_state *st, u32 adc_clk_khz)
  686. {
  687. u32 reg = 0, pendbc;
  688. int i = 0;
  689. if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
  690. reg = AT91_ADC_TSMR_TSMODE_4WIRE_PRESS;
  691. else
  692. reg = AT91_ADC_TSMR_TSMODE_5WIRE;
  693. /* a Pen Detect Debounce Time is necessary for the ADC Touch to avoid
  694. * pen detect noise.
  695. * The formula is : Pen Detect Debounce Time = (2 ^ pendbc) / ADCClock
  696. */
  697. pendbc = round_up(TOUCH_PEN_DETECT_DEBOUNCE_US * adc_clk_khz / 1000, 1);
  698. while (pendbc >> ++i)
  699. ; /* Empty! Find the shift offset */
  700. if (abs(pendbc - (1 << i)) < abs(pendbc - (1 << (i - 1))))
  701. pendbc = i;
  702. else
  703. pendbc = i - 1;
  704. if (st->caps->has_tsmr) {
  705. reg |= AT91_ADC_TSMR_TSAV_(st->caps->ts_filter_average)
  706. & AT91_ADC_TSMR_TSAV;
  707. reg |= AT91_ADC_TSMR_PENDBC_(pendbc) & AT91_ADC_TSMR_PENDBC;
  708. reg |= AT91_ADC_TSMR_NOTSDMA;
  709. reg |= AT91_ADC_TSMR_PENDET_ENA;
  710. reg |= 0x03 << 8; /* TSFREQ, need bigger than TSAV */
  711. at91_adc_writel(st, AT91_ADC_TSMR, reg);
  712. } else {
  713. /* TODO: for 9g45 which has no TSMR */
  714. }
  715. /* Change adc internal resistor value for better pen detection,
  716. * default value is 100 kOhm.
  717. * 0 = 200 kOhm, 1 = 150 kOhm, 2 = 100 kOhm, 3 = 50 kOhm
  718. * option only available on ES2 and higher
  719. */
  720. at91_adc_writel(st, AT91_ADC_ACR, st->caps->ts_pen_detect_sensitivity
  721. & AT91_ADC_ACR_PENDETSENS);
  722. /* Sample Peroid Time = (TRGPER + 1) / ADCClock */
  723. st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US *
  724. adc_clk_khz / 1000) - 1, 1);
  725. return 0;
  726. }
  727. static int at91_ts_register(struct at91_adc_state *st,
  728. struct platform_device *pdev)
  729. {
  730. struct input_dev *input;
  731. struct iio_dev *idev = iio_priv_to_dev(st);
  732. int ret;
  733. input = input_allocate_device();
  734. if (!input) {
  735. dev_err(&idev->dev, "Failed to allocate TS device!\n");
  736. return -ENOMEM;
  737. }
  738. input->name = DRIVER_NAME;
  739. input->id.bustype = BUS_HOST;
  740. input->dev.parent = &pdev->dev;
  741. input->open = atmel_ts_open;
  742. input->close = atmel_ts_close;
  743. __set_bit(EV_ABS, input->evbit);
  744. __set_bit(EV_KEY, input->evbit);
  745. __set_bit(BTN_TOUCH, input->keybit);
  746. input_set_abs_params(input, ABS_X, 0, (1 << MAX_POS_BITS) - 1, 0, 0);
  747. input_set_abs_params(input, ABS_Y, 0, (1 << MAX_POS_BITS) - 1, 0, 0);
  748. input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);
  749. st->ts_input = input;
  750. input_set_drvdata(input, st);
  751. ret = input_register_device(input);
  752. if (ret)
  753. input_free_device(st->ts_input);
  754. return ret;
  755. }
  756. static void at91_ts_unregister(struct at91_adc_state *st)
  757. {
  758. input_unregister_device(st->ts_input);
  759. }
  760. static int at91_adc_probe(struct platform_device *pdev)
  761. {
  762. unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim;
  763. int ret;
  764. struct iio_dev *idev;
  765. struct at91_adc_state *st;
  766. struct resource *res;
  767. u32 reg;
  768. idev = devm_iio_device_alloc(&pdev->dev, sizeof(struct at91_adc_state));
  769. if (!idev)
  770. return -ENOMEM;
  771. st = iio_priv(idev);
  772. if (pdev->dev.of_node)
  773. ret = at91_adc_probe_dt(st, pdev);
  774. else
  775. ret = at91_adc_probe_pdata(st, pdev);
  776. if (ret) {
  777. dev_err(&pdev->dev, "No platform data available.\n");
  778. return -EINVAL;
  779. }
  780. platform_set_drvdata(pdev, idev);
  781. idev->dev.parent = &pdev->dev;
  782. idev->name = dev_name(&pdev->dev);
  783. idev->modes = INDIO_DIRECT_MODE;
  784. idev->info = &at91_adc_info;
  785. st->irq = platform_get_irq(pdev, 0);
  786. if (st->irq < 0) {
  787. dev_err(&pdev->dev, "No IRQ ID is designated\n");
  788. return -ENODEV;
  789. }
  790. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  791. st->reg_base = devm_ioremap_resource(&pdev->dev, res);
  792. if (IS_ERR(st->reg_base)) {
  793. return PTR_ERR(st->reg_base);
  794. }
  795. /*
  796. * Disable all IRQs before setting up the handler
  797. */
  798. at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
  799. at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
  800. ret = request_irq(st->irq,
  801. at91_adc_interrupt,
  802. 0,
  803. pdev->dev.driver->name,
  804. idev);
  805. if (ret) {
  806. dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
  807. return ret;
  808. }
  809. st->clk = devm_clk_get(&pdev->dev, "adc_clk");
  810. if (IS_ERR(st->clk)) {
  811. dev_err(&pdev->dev, "Failed to get the clock.\n");
  812. ret = PTR_ERR(st->clk);
  813. goto error_free_irq;
  814. }
  815. ret = clk_prepare_enable(st->clk);
  816. if (ret) {
  817. dev_err(&pdev->dev,
  818. "Could not prepare or enable the clock.\n");
  819. goto error_free_irq;
  820. }
  821. st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
  822. if (IS_ERR(st->adc_clk)) {
  823. dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
  824. ret = PTR_ERR(st->adc_clk);
  825. goto error_disable_clk;
  826. }
  827. ret = clk_prepare_enable(st->adc_clk);
  828. if (ret) {
  829. dev_err(&pdev->dev,
  830. "Could not prepare or enable the ADC clock.\n");
  831. goto error_disable_clk;
  832. }
  833. /*
  834. * Prescaler rate computation using the formula from the Atmel's
  835. * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
  836. * specified by the electrical characteristics of the board.
  837. */
  838. mstrclk = clk_get_rate(st->clk);
  839. adc_clk = clk_get_rate(st->adc_clk);
  840. adc_clk_khz = adc_clk / 1000;
  841. dev_dbg(&pdev->dev, "Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n",
  842. mstrclk, adc_clk);
  843. prsc = (mstrclk / (2 * adc_clk)) - 1;
  844. if (!st->startup_time) {
  845. dev_err(&pdev->dev, "No startup time available.\n");
  846. ret = -EINVAL;
  847. goto error_disable_adc_clk;
  848. }
  849. ticks = (*st->caps->calc_startup_ticks)(st->startup_time, adc_clk_khz);
  850. /*
  851. * a minimal Sample and Hold Time is necessary for the ADC to guarantee
  852. * the best converted final value between two channels selection
  853. * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
  854. */
  855. if (st->sample_hold_time > 0)
  856. shtim = round_up((st->sample_hold_time * adc_clk_khz / 1000)
  857. - 1, 1);
  858. else
  859. shtim = 0;
  860. reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask;
  861. reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask;
  862. if (st->low_res)
  863. reg |= AT91_ADC_LOWRES;
  864. if (st->sleep_mode)
  865. reg |= AT91_ADC_SLEEP;
  866. reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
  867. at91_adc_writel(st, AT91_ADC_MR, reg);
  868. /* Setup the ADC channels available on the board */
  869. ret = at91_adc_channel_init(idev);
  870. if (ret < 0) {
  871. dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
  872. goto error_disable_adc_clk;
  873. }
  874. init_waitqueue_head(&st->wq_data_avail);
  875. mutex_init(&st->lock);
  876. /*
  877. * Since touch screen will set trigger register as period trigger. So
  878. * when touch screen is enabled, then we have to disable hardware
  879. * trigger for classic adc.
  880. */
  881. if (!st->touchscreen_type) {
  882. ret = at91_adc_buffer_init(idev);
  883. if (ret < 0) {
  884. dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
  885. goto error_disable_adc_clk;
  886. }
  887. ret = at91_adc_trigger_init(idev);
  888. if (ret < 0) {
  889. dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
  890. at91_adc_buffer_remove(idev);
  891. goto error_disable_adc_clk;
  892. }
  893. } else {
  894. if (!st->caps->has_tsmr) {
  895. dev_err(&pdev->dev, "We don't support non-TSMR adc\n");
  896. ret = -ENODEV;
  897. goto error_disable_adc_clk;
  898. }
  899. ret = at91_ts_register(st, pdev);
  900. if (ret)
  901. goto error_disable_adc_clk;
  902. at91_ts_hw_init(st, adc_clk_khz);
  903. }
  904. ret = iio_device_register(idev);
  905. if (ret < 0) {
  906. dev_err(&pdev->dev, "Couldn't register the device.\n");
  907. goto error_iio_device_register;
  908. }
  909. return 0;
  910. error_iio_device_register:
  911. if (!st->touchscreen_type) {
  912. at91_adc_trigger_remove(idev);
  913. at91_adc_buffer_remove(idev);
  914. } else {
  915. at91_ts_unregister(st);
  916. }
  917. error_disable_adc_clk:
  918. clk_disable_unprepare(st->adc_clk);
  919. error_disable_clk:
  920. clk_disable_unprepare(st->clk);
  921. error_free_irq:
  922. free_irq(st->irq, idev);
  923. return ret;
  924. }
  925. static int at91_adc_remove(struct platform_device *pdev)
  926. {
  927. struct iio_dev *idev = platform_get_drvdata(pdev);
  928. struct at91_adc_state *st = iio_priv(idev);
  929. iio_device_unregister(idev);
  930. if (!st->touchscreen_type) {
  931. at91_adc_trigger_remove(idev);
  932. at91_adc_buffer_remove(idev);
  933. } else {
  934. at91_ts_unregister(st);
  935. }
  936. clk_disable_unprepare(st->adc_clk);
  937. clk_disable_unprepare(st->clk);
  938. free_irq(st->irq, idev);
  939. return 0;
  940. }
  941. static struct at91_adc_caps at91sam9260_caps = {
  942. .calc_startup_ticks = calc_startup_ticks_9260,
  943. .num_channels = 4,
  944. .registers = {
  945. .channel_base = AT91_ADC_CHR(0),
  946. .drdy_mask = AT91_ADC_DRDY,
  947. .status_register = AT91_ADC_SR,
  948. .trigger_register = AT91_ADC_TRGR_9260,
  949. .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
  950. .mr_startup_mask = AT91_ADC_STARTUP_9260,
  951. },
  952. };
  953. static struct at91_adc_caps at91sam9g45_caps = {
  954. .has_ts = true,
  955. .calc_startup_ticks = calc_startup_ticks_9260, /* same as 9260 */
  956. .num_channels = 8,
  957. .registers = {
  958. .channel_base = AT91_ADC_CHR(0),
  959. .drdy_mask = AT91_ADC_DRDY,
  960. .status_register = AT91_ADC_SR,
  961. .trigger_register = AT91_ADC_TRGR_9G45,
  962. .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
  963. .mr_startup_mask = AT91_ADC_STARTUP_9G45,
  964. },
  965. };
  966. static struct at91_adc_caps at91sam9x5_caps = {
  967. .has_ts = true,
  968. .has_tsmr = true,
  969. .ts_filter_average = 3,
  970. .ts_pen_detect_sensitivity = 2,
  971. .calc_startup_ticks = calc_startup_ticks_9x5,
  972. .num_channels = 12,
  973. .registers = {
  974. .channel_base = AT91_ADC_CDR0_9X5,
  975. .drdy_mask = AT91_ADC_SR_DRDY_9X5,
  976. .status_register = AT91_ADC_SR_9X5,
  977. .trigger_register = AT91_ADC_TRGR_9X5,
  978. /* prescal mask is same as 9G45 */
  979. .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
  980. .mr_startup_mask = AT91_ADC_STARTUP_9X5,
  981. },
  982. };
  983. static const struct of_device_id at91_adc_dt_ids[] = {
  984. { .compatible = "atmel,at91sam9260-adc", .data = &at91sam9260_caps },
  985. { .compatible = "atmel,at91sam9g45-adc", .data = &at91sam9g45_caps },
  986. { .compatible = "atmel,at91sam9x5-adc", .data = &at91sam9x5_caps },
  987. {},
  988. };
  989. MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
  990. static const struct platform_device_id at91_adc_ids[] = {
  991. {
  992. .name = "at91sam9260-adc",
  993. .driver_data = (unsigned long)&at91sam9260_caps,
  994. }, {
  995. .name = "at91sam9g45-adc",
  996. .driver_data = (unsigned long)&at91sam9g45_caps,
  997. }, {
  998. .name = "at91sam9x5-adc",
  999. .driver_data = (unsigned long)&at91sam9x5_caps,
  1000. }, {
  1001. /* terminator */
  1002. }
  1003. };
  1004. MODULE_DEVICE_TABLE(platform, at91_adc_ids);
  1005. static struct platform_driver at91_adc_driver = {
  1006. .probe = at91_adc_probe,
  1007. .remove = at91_adc_remove,
  1008. .id_table = at91_adc_ids,
  1009. .driver = {
  1010. .name = DRIVER_NAME,
  1011. .of_match_table = of_match_ptr(at91_adc_dt_ids),
  1012. },
  1013. };
  1014. module_platform_driver(at91_adc_driver);
  1015. MODULE_LICENSE("GPL");
  1016. MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
  1017. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");