at91_adc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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. if (ret == 0)
  600. ret = -ETIMEDOUT;
  601. if (ret < 0) {
  602. mutex_unlock(&st->lock);
  603. return ret;
  604. }
  605. *val = st->last_value;
  606. at91_adc_writel(st, AT91_ADC_CHDR,
  607. AT91_ADC_CH(chan->channel));
  608. at91_adc_writel(st, AT91_ADC_IDR, BIT(chan->channel));
  609. st->last_value = 0;
  610. st->done = false;
  611. mutex_unlock(&st->lock);
  612. return IIO_VAL_INT;
  613. case IIO_CHAN_INFO_SCALE:
  614. *val = st->vref_mv;
  615. *val2 = chan->scan_type.realbits;
  616. return IIO_VAL_FRACTIONAL_LOG2;
  617. default:
  618. break;
  619. }
  620. return -EINVAL;
  621. }
  622. static int at91_adc_of_get_resolution(struct at91_adc_state *st,
  623. struct platform_device *pdev)
  624. {
  625. struct iio_dev *idev = iio_priv_to_dev(st);
  626. struct device_node *np = pdev->dev.of_node;
  627. int count, i, ret = 0;
  628. char *res_name, *s;
  629. u32 *resolutions;
  630. count = of_property_count_strings(np, "atmel,adc-res-names");
  631. if (count < 2) {
  632. dev_err(&idev->dev, "You must specified at least two resolution names for "
  633. "adc-res-names property in the DT\n");
  634. return count;
  635. }
  636. resolutions = kmalloc_array(count, sizeof(*resolutions), GFP_KERNEL);
  637. if (!resolutions)
  638. return -ENOMEM;
  639. if (of_property_read_u32_array(np, "atmel,adc-res", resolutions, count)) {
  640. dev_err(&idev->dev, "Missing adc-res property in the DT.\n");
  641. ret = -ENODEV;
  642. goto ret;
  643. }
  644. if (of_property_read_string(np, "atmel,adc-use-res", (const char **)&res_name))
  645. res_name = "highres";
  646. for (i = 0; i < count; i++) {
  647. if (of_property_read_string_index(np, "atmel,adc-res-names", i, (const char **)&s))
  648. continue;
  649. if (strcmp(res_name, s))
  650. continue;
  651. st->res = resolutions[i];
  652. if (!strcmp(res_name, "lowres"))
  653. st->low_res = true;
  654. else
  655. st->low_res = false;
  656. dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
  657. goto ret;
  658. }
  659. dev_err(&idev->dev, "There is no resolution for %s\n", res_name);
  660. ret:
  661. kfree(resolutions);
  662. return ret;
  663. }
  664. static u32 calc_startup_ticks_9260(u32 startup_time, u32 adc_clk_khz)
  665. {
  666. /*
  667. * Number of ticks needed to cover the startup time of the ADC
  668. * as defined in the electrical characteristics of the board,
  669. * divided by 8. The formula thus is :
  670. * Startup Time = (ticks + 1) * 8 / ADC Clock
  671. */
  672. return round_up((startup_time * adc_clk_khz / 1000) - 1, 8) / 8;
  673. }
  674. static u32 calc_startup_ticks_9x5(u32 startup_time, u32 adc_clk_khz)
  675. {
  676. /*
  677. * For sama5d3x and at91sam9x5, the formula changes to:
  678. * Startup Time = <lookup_table_value> / ADC Clock
  679. */
  680. static const int startup_lookup[] = {
  681. 0, 8, 16, 24,
  682. 64, 80, 96, 112,
  683. 512, 576, 640, 704,
  684. 768, 832, 896, 960
  685. };
  686. int i, size = ARRAY_SIZE(startup_lookup);
  687. unsigned int ticks;
  688. ticks = startup_time * adc_clk_khz / 1000;
  689. for (i = 0; i < size; i++)
  690. if (ticks < startup_lookup[i])
  691. break;
  692. ticks = i;
  693. if (ticks == size)
  694. /* Reach the end of lookup table */
  695. ticks = size - 1;
  696. return ticks;
  697. }
  698. static const struct of_device_id at91_adc_dt_ids[];
  699. static int at91_adc_probe_dt_ts(struct device_node *node,
  700. struct at91_adc_state *st, struct device *dev)
  701. {
  702. int ret;
  703. u32 prop;
  704. ret = of_property_read_u32(node, "atmel,adc-ts-wires", &prop);
  705. if (ret) {
  706. dev_info(dev, "ADC Touch screen is disabled.\n");
  707. return 0;
  708. }
  709. switch (prop) {
  710. case 4:
  711. case 5:
  712. st->touchscreen_type = prop;
  713. break;
  714. default:
  715. dev_err(dev, "Unsupported number of touchscreen wires (%d). Should be 4 or 5.\n", prop);
  716. return -EINVAL;
  717. }
  718. if (!st->caps->has_tsmr)
  719. return 0;
  720. prop = 0;
  721. of_property_read_u32(node, "atmel,adc-ts-pressure-threshold", &prop);
  722. st->ts_pressure_threshold = prop;
  723. if (st->ts_pressure_threshold) {
  724. return 0;
  725. } else {
  726. dev_err(dev, "Invalid pressure threshold for the touchscreen\n");
  727. return -EINVAL;
  728. }
  729. }
  730. static int at91_adc_probe_dt(struct at91_adc_state *st,
  731. struct platform_device *pdev)
  732. {
  733. struct iio_dev *idev = iio_priv_to_dev(st);
  734. struct device_node *node = pdev->dev.of_node;
  735. struct device_node *trig_node;
  736. int i = 0, ret;
  737. u32 prop;
  738. if (!node)
  739. return -EINVAL;
  740. st->caps = (struct at91_adc_caps *)
  741. of_match_device(at91_adc_dt_ids, &pdev->dev)->data;
  742. st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
  743. if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
  744. dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
  745. ret = -EINVAL;
  746. goto error_ret;
  747. }
  748. st->channels_mask = prop;
  749. st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
  750. if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
  751. dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
  752. ret = -EINVAL;
  753. goto error_ret;
  754. }
  755. st->startup_time = prop;
  756. prop = 0;
  757. of_property_read_u32(node, "atmel,adc-sample-hold-time", &prop);
  758. st->sample_hold_time = prop;
  759. if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
  760. dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
  761. ret = -EINVAL;
  762. goto error_ret;
  763. }
  764. st->vref_mv = prop;
  765. ret = at91_adc_of_get_resolution(st, pdev);
  766. if (ret)
  767. goto error_ret;
  768. st->registers = &st->caps->registers;
  769. st->num_channels = st->caps->num_channels;
  770. st->trigger_number = of_get_child_count(node);
  771. st->trigger_list = devm_kcalloc(&idev->dev,
  772. st->trigger_number,
  773. sizeof(struct at91_adc_trigger),
  774. GFP_KERNEL);
  775. if (!st->trigger_list) {
  776. dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
  777. ret = -ENOMEM;
  778. goto error_ret;
  779. }
  780. for_each_child_of_node(node, trig_node) {
  781. struct at91_adc_trigger *trig = st->trigger_list + i;
  782. const char *name;
  783. if (of_property_read_string(trig_node, "trigger-name", &name)) {
  784. dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
  785. ret = -EINVAL;
  786. goto error_ret;
  787. }
  788. trig->name = name;
  789. if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
  790. dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
  791. ret = -EINVAL;
  792. goto error_ret;
  793. }
  794. trig->value = prop;
  795. trig->is_external = of_property_read_bool(trig_node, "trigger-external");
  796. i++;
  797. }
  798. /* Check if touchscreen is supported. */
  799. if (st->caps->has_ts)
  800. return at91_adc_probe_dt_ts(node, st, &idev->dev);
  801. else
  802. dev_info(&idev->dev, "not support touchscreen in the adc compatible string.\n");
  803. return 0;
  804. error_ret:
  805. return ret;
  806. }
  807. static int at91_adc_probe_pdata(struct at91_adc_state *st,
  808. struct platform_device *pdev)
  809. {
  810. struct at91_adc_data *pdata = pdev->dev.platform_data;
  811. if (!pdata)
  812. return -EINVAL;
  813. st->caps = (struct at91_adc_caps *)
  814. platform_get_device_id(pdev)->driver_data;
  815. st->use_external = pdata->use_external_triggers;
  816. st->vref_mv = pdata->vref;
  817. st->channels_mask = pdata->channels_used;
  818. st->num_channels = st->caps->num_channels;
  819. st->startup_time = pdata->startup_time;
  820. st->trigger_number = pdata->trigger_number;
  821. st->trigger_list = pdata->trigger_list;
  822. st->registers = &st->caps->registers;
  823. st->touchscreen_type = pdata->touchscreen_type;
  824. return 0;
  825. }
  826. static const struct iio_info at91_adc_info = {
  827. .read_raw = &at91_adc_read_raw,
  828. };
  829. /* Touchscreen related functions */
  830. static int atmel_ts_open(struct input_dev *dev)
  831. {
  832. struct at91_adc_state *st = input_get_drvdata(dev);
  833. if (st->caps->has_tsmr)
  834. at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
  835. else
  836. at91_adc_writel(st, AT91_ADC_IER, AT91RL_ADC_IER_PEN);
  837. return 0;
  838. }
  839. static void atmel_ts_close(struct input_dev *dev)
  840. {
  841. struct at91_adc_state *st = input_get_drvdata(dev);
  842. if (st->caps->has_tsmr)
  843. at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
  844. else
  845. at91_adc_writel(st, AT91_ADC_IDR, AT91RL_ADC_IER_PEN);
  846. }
  847. static int at91_ts_hw_init(struct at91_adc_state *st, u32 adc_clk_khz)
  848. {
  849. struct iio_dev *idev = iio_priv_to_dev(st);
  850. u32 reg = 0;
  851. u32 tssctim = 0;
  852. int i = 0;
  853. /* a Pen Detect Debounce Time is necessary for the ADC Touch to avoid
  854. * pen detect noise.
  855. * The formula is : Pen Detect Debounce Time = (2 ^ pendbc) / ADCClock
  856. */
  857. st->ts_pendbc = round_up(TOUCH_PEN_DETECT_DEBOUNCE_US * adc_clk_khz /
  858. 1000, 1);
  859. while (st->ts_pendbc >> ++i)
  860. ; /* Empty! Find the shift offset */
  861. if (abs(st->ts_pendbc - (1 << i)) < abs(st->ts_pendbc - (1 << (i - 1))))
  862. st->ts_pendbc = i;
  863. else
  864. st->ts_pendbc = i - 1;
  865. if (!st->caps->has_tsmr) {
  866. reg = at91_adc_readl(st, AT91_ADC_MR);
  867. reg |= AT91_ADC_TSAMOD_TS_ONLY_MODE | AT91_ADC_PENDET;
  868. reg |= AT91_ADC_PENDBC_(st->ts_pendbc) & AT91_ADC_PENDBC;
  869. at91_adc_writel(st, AT91_ADC_MR, reg);
  870. reg = AT91_ADC_TSR_SHTIM_(TOUCH_SHTIM) & AT91_ADC_TSR_SHTIM;
  871. at91_adc_writel(st, AT91_ADC_TSR, reg);
  872. st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US_RL *
  873. adc_clk_khz / 1000) - 1, 1);
  874. return 0;
  875. }
  876. /* Touchscreen Switches Closure time needed for allowing the value to
  877. * stabilize.
  878. * Switch Closure Time = (TSSCTIM * 4) ADCClock periods
  879. */
  880. tssctim = DIV_ROUND_UP(TOUCH_SCTIM_US * adc_clk_khz / 1000, 4);
  881. dev_dbg(&idev->dev, "adc_clk at: %d KHz, tssctim at: %d\n",
  882. adc_clk_khz, tssctim);
  883. if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
  884. reg = AT91_ADC_TSMR_TSMODE_4WIRE_PRESS;
  885. else
  886. reg = AT91_ADC_TSMR_TSMODE_5WIRE;
  887. reg |= AT91_ADC_TSMR_SCTIM_(tssctim) & AT91_ADC_TSMR_SCTIM;
  888. reg |= AT91_ADC_TSMR_TSAV_(st->caps->ts_filter_average)
  889. & AT91_ADC_TSMR_TSAV;
  890. reg |= AT91_ADC_TSMR_PENDBC_(st->ts_pendbc) & AT91_ADC_TSMR_PENDBC;
  891. reg |= AT91_ADC_TSMR_NOTSDMA;
  892. reg |= AT91_ADC_TSMR_PENDET_ENA;
  893. reg |= 0x03 << 8; /* TSFREQ, needs to be bigger than TSAV */
  894. at91_adc_writel(st, AT91_ADC_TSMR, reg);
  895. /* Change adc internal resistor value for better pen detection,
  896. * default value is 100 kOhm.
  897. * 0 = 200 kOhm, 1 = 150 kOhm, 2 = 100 kOhm, 3 = 50 kOhm
  898. * option only available on ES2 and higher
  899. */
  900. at91_adc_writel(st, AT91_ADC_ACR, st->caps->ts_pen_detect_sensitivity
  901. & AT91_ADC_ACR_PENDETSENS);
  902. /* Sample Period Time = (TRGPER + 1) / ADCClock */
  903. st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US *
  904. adc_clk_khz / 1000) - 1, 1);
  905. return 0;
  906. }
  907. static int at91_ts_register(struct at91_adc_state *st,
  908. struct platform_device *pdev)
  909. {
  910. struct input_dev *input;
  911. struct iio_dev *idev = iio_priv_to_dev(st);
  912. int ret;
  913. input = input_allocate_device();
  914. if (!input) {
  915. dev_err(&idev->dev, "Failed to allocate TS device!\n");
  916. return -ENOMEM;
  917. }
  918. input->name = DRIVER_NAME;
  919. input->id.bustype = BUS_HOST;
  920. input->dev.parent = &pdev->dev;
  921. input->open = atmel_ts_open;
  922. input->close = atmel_ts_close;
  923. __set_bit(EV_ABS, input->evbit);
  924. __set_bit(EV_KEY, input->evbit);
  925. __set_bit(BTN_TOUCH, input->keybit);
  926. if (st->caps->has_tsmr) {
  927. input_set_abs_params(input, ABS_X, 0, (1 << MAX_POS_BITS) - 1,
  928. 0, 0);
  929. input_set_abs_params(input, ABS_Y, 0, (1 << MAX_POS_BITS) - 1,
  930. 0, 0);
  931. input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);
  932. } else {
  933. if (st->touchscreen_type != ATMEL_ADC_TOUCHSCREEN_4WIRE) {
  934. dev_err(&pdev->dev,
  935. "This touchscreen controller only support 4 wires\n");
  936. ret = -EINVAL;
  937. goto err;
  938. }
  939. input_set_abs_params(input, ABS_X, 0, (1 << MAX_RLPOS_BITS) - 1,
  940. 0, 0);
  941. input_set_abs_params(input, ABS_Y, 0, (1 << MAX_RLPOS_BITS) - 1,
  942. 0, 0);
  943. }
  944. st->ts_input = input;
  945. input_set_drvdata(input, st);
  946. ret = input_register_device(input);
  947. if (ret)
  948. goto err;
  949. return ret;
  950. err:
  951. input_free_device(st->ts_input);
  952. return ret;
  953. }
  954. static void at91_ts_unregister(struct at91_adc_state *st)
  955. {
  956. input_unregister_device(st->ts_input);
  957. }
  958. static int at91_adc_probe(struct platform_device *pdev)
  959. {
  960. unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim;
  961. int ret;
  962. struct iio_dev *idev;
  963. struct at91_adc_state *st;
  964. struct resource *res;
  965. u32 reg;
  966. idev = devm_iio_device_alloc(&pdev->dev, sizeof(struct at91_adc_state));
  967. if (!idev)
  968. return -ENOMEM;
  969. st = iio_priv(idev);
  970. if (pdev->dev.of_node)
  971. ret = at91_adc_probe_dt(st, pdev);
  972. else
  973. ret = at91_adc_probe_pdata(st, pdev);
  974. if (ret) {
  975. dev_err(&pdev->dev, "No platform data available.\n");
  976. return -EINVAL;
  977. }
  978. platform_set_drvdata(pdev, idev);
  979. idev->dev.parent = &pdev->dev;
  980. idev->name = dev_name(&pdev->dev);
  981. idev->modes = INDIO_DIRECT_MODE;
  982. idev->info = &at91_adc_info;
  983. st->irq = platform_get_irq(pdev, 0);
  984. if (st->irq < 0) {
  985. dev_err(&pdev->dev, "No IRQ ID is designated\n");
  986. return -ENODEV;
  987. }
  988. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  989. st->reg_base = devm_ioremap_resource(&pdev->dev, res);
  990. if (IS_ERR(st->reg_base))
  991. return PTR_ERR(st->reg_base);
  992. /*
  993. * Disable all IRQs before setting up the handler
  994. */
  995. at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
  996. at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
  997. if (st->caps->has_tsmr)
  998. ret = request_irq(st->irq, at91_adc_9x5_interrupt, 0,
  999. pdev->dev.driver->name, idev);
  1000. else
  1001. ret = request_irq(st->irq, at91_adc_rl_interrupt, 0,
  1002. pdev->dev.driver->name, idev);
  1003. if (ret) {
  1004. dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
  1005. return ret;
  1006. }
  1007. st->clk = devm_clk_get(&pdev->dev, "adc_clk");
  1008. if (IS_ERR(st->clk)) {
  1009. dev_err(&pdev->dev, "Failed to get the clock.\n");
  1010. ret = PTR_ERR(st->clk);
  1011. goto error_free_irq;
  1012. }
  1013. ret = clk_prepare_enable(st->clk);
  1014. if (ret) {
  1015. dev_err(&pdev->dev,
  1016. "Could not prepare or enable the clock.\n");
  1017. goto error_free_irq;
  1018. }
  1019. st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
  1020. if (IS_ERR(st->adc_clk)) {
  1021. dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
  1022. ret = PTR_ERR(st->adc_clk);
  1023. goto error_disable_clk;
  1024. }
  1025. ret = clk_prepare_enable(st->adc_clk);
  1026. if (ret) {
  1027. dev_err(&pdev->dev,
  1028. "Could not prepare or enable the ADC clock.\n");
  1029. goto error_disable_clk;
  1030. }
  1031. /*
  1032. * Prescaler rate computation using the formula from the Atmel's
  1033. * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
  1034. * specified by the electrical characteristics of the board.
  1035. */
  1036. mstrclk = clk_get_rate(st->clk);
  1037. adc_clk = clk_get_rate(st->adc_clk);
  1038. adc_clk_khz = adc_clk / 1000;
  1039. dev_dbg(&pdev->dev, "Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n",
  1040. mstrclk, adc_clk);
  1041. prsc = (mstrclk / (2 * adc_clk)) - 1;
  1042. if (!st->startup_time) {
  1043. dev_err(&pdev->dev, "No startup time available.\n");
  1044. ret = -EINVAL;
  1045. goto error_disable_adc_clk;
  1046. }
  1047. ticks = (*st->caps->calc_startup_ticks)(st->startup_time, adc_clk_khz);
  1048. /*
  1049. * a minimal Sample and Hold Time is necessary for the ADC to guarantee
  1050. * the best converted final value between two channels selection
  1051. * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
  1052. */
  1053. if (st->sample_hold_time > 0)
  1054. shtim = round_up((st->sample_hold_time * adc_clk_khz / 1000)
  1055. - 1, 1);
  1056. else
  1057. shtim = 0;
  1058. reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask;
  1059. reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask;
  1060. if (st->low_res)
  1061. reg |= AT91_ADC_LOWRES;
  1062. if (st->sleep_mode)
  1063. reg |= AT91_ADC_SLEEP;
  1064. reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
  1065. at91_adc_writel(st, AT91_ADC_MR, reg);
  1066. /* Setup the ADC channels available on the board */
  1067. ret = at91_adc_channel_init(idev);
  1068. if (ret < 0) {
  1069. dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
  1070. goto error_disable_adc_clk;
  1071. }
  1072. init_waitqueue_head(&st->wq_data_avail);
  1073. mutex_init(&st->lock);
  1074. /*
  1075. * Since touch screen will set trigger register as period trigger. So
  1076. * when touch screen is enabled, then we have to disable hardware
  1077. * trigger for classic adc.
  1078. */
  1079. if (!st->touchscreen_type) {
  1080. ret = at91_adc_buffer_init(idev);
  1081. if (ret < 0) {
  1082. dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
  1083. goto error_disable_adc_clk;
  1084. }
  1085. ret = at91_adc_trigger_init(idev);
  1086. if (ret < 0) {
  1087. dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
  1088. at91_adc_buffer_remove(idev);
  1089. goto error_disable_adc_clk;
  1090. }
  1091. } else {
  1092. ret = at91_ts_register(st, pdev);
  1093. if (ret)
  1094. goto error_disable_adc_clk;
  1095. at91_ts_hw_init(st, adc_clk_khz);
  1096. }
  1097. ret = iio_device_register(idev);
  1098. if (ret < 0) {
  1099. dev_err(&pdev->dev, "Couldn't register the device.\n");
  1100. goto error_iio_device_register;
  1101. }
  1102. return 0;
  1103. error_iio_device_register:
  1104. if (!st->touchscreen_type) {
  1105. at91_adc_trigger_remove(idev);
  1106. at91_adc_buffer_remove(idev);
  1107. } else {
  1108. at91_ts_unregister(st);
  1109. }
  1110. error_disable_adc_clk:
  1111. clk_disable_unprepare(st->adc_clk);
  1112. error_disable_clk:
  1113. clk_disable_unprepare(st->clk);
  1114. error_free_irq:
  1115. free_irq(st->irq, idev);
  1116. return ret;
  1117. }
  1118. static int at91_adc_remove(struct platform_device *pdev)
  1119. {
  1120. struct iio_dev *idev = platform_get_drvdata(pdev);
  1121. struct at91_adc_state *st = iio_priv(idev);
  1122. iio_device_unregister(idev);
  1123. if (!st->touchscreen_type) {
  1124. at91_adc_trigger_remove(idev);
  1125. at91_adc_buffer_remove(idev);
  1126. } else {
  1127. at91_ts_unregister(st);
  1128. }
  1129. clk_disable_unprepare(st->adc_clk);
  1130. clk_disable_unprepare(st->clk);
  1131. free_irq(st->irq, idev);
  1132. return 0;
  1133. }
  1134. #ifdef CONFIG_PM_SLEEP
  1135. static int at91_adc_suspend(struct device *dev)
  1136. {
  1137. struct iio_dev *idev = platform_get_drvdata(to_platform_device(dev));
  1138. struct at91_adc_state *st = iio_priv(idev);
  1139. pinctrl_pm_select_sleep_state(dev);
  1140. clk_disable_unprepare(st->clk);
  1141. return 0;
  1142. }
  1143. static int at91_adc_resume(struct device *dev)
  1144. {
  1145. struct iio_dev *idev = platform_get_drvdata(to_platform_device(dev));
  1146. struct at91_adc_state *st = iio_priv(idev);
  1147. clk_prepare_enable(st->clk);
  1148. pinctrl_pm_select_default_state(dev);
  1149. return 0;
  1150. }
  1151. #endif
  1152. static SIMPLE_DEV_PM_OPS(at91_adc_pm_ops, at91_adc_suspend, at91_adc_resume);
  1153. static struct at91_adc_caps at91sam9260_caps = {
  1154. .calc_startup_ticks = calc_startup_ticks_9260,
  1155. .num_channels = 4,
  1156. .registers = {
  1157. .channel_base = AT91_ADC_CHR(0),
  1158. .drdy_mask = AT91_ADC_DRDY,
  1159. .status_register = AT91_ADC_SR,
  1160. .trigger_register = AT91_ADC_TRGR_9260,
  1161. .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
  1162. .mr_startup_mask = AT91_ADC_STARTUP_9260,
  1163. },
  1164. };
  1165. static struct at91_adc_caps at91sam9rl_caps = {
  1166. .has_ts = true,
  1167. .calc_startup_ticks = calc_startup_ticks_9260, /* same as 9260 */
  1168. .num_channels = 6,
  1169. .registers = {
  1170. .channel_base = AT91_ADC_CHR(0),
  1171. .drdy_mask = AT91_ADC_DRDY,
  1172. .status_register = AT91_ADC_SR,
  1173. .trigger_register = AT91_ADC_TRGR_9G45,
  1174. .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
  1175. .mr_startup_mask = AT91_ADC_STARTUP_9G45,
  1176. },
  1177. };
  1178. static struct at91_adc_caps at91sam9g45_caps = {
  1179. .has_ts = true,
  1180. .calc_startup_ticks = calc_startup_ticks_9260, /* same as 9260 */
  1181. .num_channels = 8,
  1182. .registers = {
  1183. .channel_base = AT91_ADC_CHR(0),
  1184. .drdy_mask = AT91_ADC_DRDY,
  1185. .status_register = AT91_ADC_SR,
  1186. .trigger_register = AT91_ADC_TRGR_9G45,
  1187. .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
  1188. .mr_startup_mask = AT91_ADC_STARTUP_9G45,
  1189. },
  1190. };
  1191. static struct at91_adc_caps at91sam9x5_caps = {
  1192. .has_ts = true,
  1193. .has_tsmr = true,
  1194. .ts_filter_average = 3,
  1195. .ts_pen_detect_sensitivity = 2,
  1196. .calc_startup_ticks = calc_startup_ticks_9x5,
  1197. .num_channels = 12,
  1198. .registers = {
  1199. .channel_base = AT91_ADC_CDR0_9X5,
  1200. .drdy_mask = AT91_ADC_SR_DRDY_9X5,
  1201. .status_register = AT91_ADC_SR_9X5,
  1202. .trigger_register = AT91_ADC_TRGR_9X5,
  1203. /* prescal mask is same as 9G45 */
  1204. .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
  1205. .mr_startup_mask = AT91_ADC_STARTUP_9X5,
  1206. },
  1207. };
  1208. static const struct of_device_id at91_adc_dt_ids[] = {
  1209. { .compatible = "atmel,at91sam9260-adc", .data = &at91sam9260_caps },
  1210. { .compatible = "atmel,at91sam9rl-adc", .data = &at91sam9rl_caps },
  1211. { .compatible = "atmel,at91sam9g45-adc", .data = &at91sam9g45_caps },
  1212. { .compatible = "atmel,at91sam9x5-adc", .data = &at91sam9x5_caps },
  1213. {},
  1214. };
  1215. MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
  1216. static const struct platform_device_id at91_adc_ids[] = {
  1217. {
  1218. .name = "at91sam9260-adc",
  1219. .driver_data = (unsigned long)&at91sam9260_caps,
  1220. }, {
  1221. .name = "at91sam9rl-adc",
  1222. .driver_data = (unsigned long)&at91sam9rl_caps,
  1223. }, {
  1224. .name = "at91sam9g45-adc",
  1225. .driver_data = (unsigned long)&at91sam9g45_caps,
  1226. }, {
  1227. .name = "at91sam9x5-adc",
  1228. .driver_data = (unsigned long)&at91sam9x5_caps,
  1229. }, {
  1230. /* terminator */
  1231. }
  1232. };
  1233. MODULE_DEVICE_TABLE(platform, at91_adc_ids);
  1234. static struct platform_driver at91_adc_driver = {
  1235. .probe = at91_adc_probe,
  1236. .remove = at91_adc_remove,
  1237. .id_table = at91_adc_ids,
  1238. .driver = {
  1239. .name = DRIVER_NAME,
  1240. .of_match_table = of_match_ptr(at91_adc_dt_ids),
  1241. .pm = &at91_adc_pm_ops,
  1242. },
  1243. };
  1244. module_platform_driver(at91_adc_driver);
  1245. MODULE_LICENSE("GPL");
  1246. MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
  1247. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");