at91_adc.c 39 KB

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