at91_adc.c 40 KB

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