at91_adc.c 40 KB

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