at91_adc.c 39 KB

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