at91_adc.c 41 KB

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