at91_adc.c 40 KB

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