mxs-lradc.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. /*
  2. * Freescale MXS LRADC driver
  3. *
  4. * Copyright (c) 2012 DENX Software Engineering, GmbH.
  5. * Marek Vasut <marex@denx.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/device.h>
  21. #include <linux/err.h>
  22. #include <linux/input.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/mutex.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/stmp_device.h>
  33. #include <linux/sysfs.h>
  34. #include <linux/iio/buffer.h>
  35. #include <linux/iio/iio.h>
  36. #include <linux/iio/trigger.h>
  37. #include <linux/iio/trigger_consumer.h>
  38. #include <linux/iio/triggered_buffer.h>
  39. #include <linux/iio/sysfs.h>
  40. #define DRIVER_NAME "mxs-lradc"
  41. #define LRADC_MAX_DELAY_CHANS 4
  42. #define LRADC_MAX_MAPPED_CHANS 8
  43. #define LRADC_MAX_TOTAL_CHANS 16
  44. #define LRADC_DELAY_TIMER_HZ 2000
  45. /*
  46. * Make this runtime configurable if necessary. Currently, if the buffered mode
  47. * is enabled, the LRADC takes LRADC_DELAY_TIMER_LOOP samples of data before
  48. * triggering IRQ. The sampling happens every (LRADC_DELAY_TIMER_PER / 2000)
  49. * seconds. The result is that the samples arrive every 500mS.
  50. */
  51. #define LRADC_DELAY_TIMER_PER 200
  52. #define LRADC_DELAY_TIMER_LOOP 5
  53. /*
  54. * Once the pen touches the touchscreen, the touchscreen switches from
  55. * IRQ-driven mode to polling mode to prevent interrupt storm. The polling
  56. * is realized by worker thread, which is called every 20 or so milliseconds.
  57. * This gives the touchscreen enough fluency and does not strain the system
  58. * too much.
  59. */
  60. #define LRADC_TS_SAMPLE_DELAY_MS 5
  61. /*
  62. * The LRADC reads the following amount of samples from each touchscreen
  63. * channel and the driver then computes average of these.
  64. */
  65. #define LRADC_TS_SAMPLE_AMOUNT 4
  66. enum mxs_lradc_id {
  67. IMX23_LRADC,
  68. IMX28_LRADC,
  69. };
  70. static const char * const mx23_lradc_irq_names[] = {
  71. "mxs-lradc-touchscreen",
  72. "mxs-lradc-channel0",
  73. "mxs-lradc-channel1",
  74. "mxs-lradc-channel2",
  75. "mxs-lradc-channel3",
  76. "mxs-lradc-channel4",
  77. "mxs-lradc-channel5",
  78. "mxs-lradc-channel6",
  79. "mxs-lradc-channel7",
  80. };
  81. static const char * const mx28_lradc_irq_names[] = {
  82. "mxs-lradc-touchscreen",
  83. "mxs-lradc-thresh0",
  84. "mxs-lradc-thresh1",
  85. "mxs-lradc-channel0",
  86. "mxs-lradc-channel1",
  87. "mxs-lradc-channel2",
  88. "mxs-lradc-channel3",
  89. "mxs-lradc-channel4",
  90. "mxs-lradc-channel5",
  91. "mxs-lradc-channel6",
  92. "mxs-lradc-channel7",
  93. "mxs-lradc-button0",
  94. "mxs-lradc-button1",
  95. };
  96. struct mxs_lradc_of_config {
  97. const int irq_count;
  98. const char * const *irq_name;
  99. const u32 *vref_mv;
  100. };
  101. #define VREF_MV_BASE 1850
  102. static const u32 mx23_vref_mv[LRADC_MAX_TOTAL_CHANS] = {
  103. VREF_MV_BASE, /* CH0 */
  104. VREF_MV_BASE, /* CH1 */
  105. VREF_MV_BASE, /* CH2 */
  106. VREF_MV_BASE, /* CH3 */
  107. VREF_MV_BASE, /* CH4 */
  108. VREF_MV_BASE, /* CH5 */
  109. VREF_MV_BASE * 2, /* CH6 VDDIO */
  110. VREF_MV_BASE * 4, /* CH7 VBATT */
  111. VREF_MV_BASE, /* CH8 Temp sense 0 */
  112. VREF_MV_BASE, /* CH9 Temp sense 1 */
  113. VREF_MV_BASE, /* CH10 */
  114. VREF_MV_BASE, /* CH11 */
  115. VREF_MV_BASE, /* CH12 USB_DP */
  116. VREF_MV_BASE, /* CH13 USB_DN */
  117. VREF_MV_BASE, /* CH14 VBG */
  118. VREF_MV_BASE * 4, /* CH15 VDD5V */
  119. };
  120. static const u32 mx28_vref_mv[LRADC_MAX_TOTAL_CHANS] = {
  121. VREF_MV_BASE, /* CH0 */
  122. VREF_MV_BASE, /* CH1 */
  123. VREF_MV_BASE, /* CH2 */
  124. VREF_MV_BASE, /* CH3 */
  125. VREF_MV_BASE, /* CH4 */
  126. VREF_MV_BASE, /* CH5 */
  127. VREF_MV_BASE, /* CH6 */
  128. VREF_MV_BASE * 4, /* CH7 VBATT */
  129. VREF_MV_BASE, /* CH8 Temp sense 0 */
  130. VREF_MV_BASE, /* CH9 Temp sense 1 */
  131. VREF_MV_BASE * 2, /* CH10 VDDIO */
  132. VREF_MV_BASE, /* CH11 VTH */
  133. VREF_MV_BASE * 2, /* CH12 VDDA */
  134. VREF_MV_BASE, /* CH13 VDDD */
  135. VREF_MV_BASE, /* CH14 VBG */
  136. VREF_MV_BASE * 4, /* CH15 VDD5V */
  137. };
  138. static const struct mxs_lradc_of_config mxs_lradc_of_config[] = {
  139. [IMX23_LRADC] = {
  140. .irq_count = ARRAY_SIZE(mx23_lradc_irq_names),
  141. .irq_name = mx23_lradc_irq_names,
  142. .vref_mv = mx23_vref_mv,
  143. },
  144. [IMX28_LRADC] = {
  145. .irq_count = ARRAY_SIZE(mx28_lradc_irq_names),
  146. .irq_name = mx28_lradc_irq_names,
  147. .vref_mv = mx28_vref_mv,
  148. },
  149. };
  150. enum mxs_lradc_ts {
  151. MXS_LRADC_TOUCHSCREEN_NONE = 0,
  152. MXS_LRADC_TOUCHSCREEN_4WIRE,
  153. MXS_LRADC_TOUCHSCREEN_5WIRE,
  154. };
  155. /*
  156. * Touchscreen handling
  157. */
  158. enum lradc_ts_plate {
  159. LRADC_TOUCH = 0,
  160. LRADC_SAMPLE_X,
  161. LRADC_SAMPLE_Y,
  162. LRADC_SAMPLE_PRESSURE,
  163. LRADC_SAMPLE_VALID,
  164. };
  165. enum mxs_lradc_divbytwo {
  166. MXS_LRADC_DIV_DISABLED = 0,
  167. MXS_LRADC_DIV_ENABLED,
  168. };
  169. struct mxs_lradc_scale {
  170. unsigned int integer;
  171. unsigned int nano;
  172. };
  173. struct mxs_lradc {
  174. struct device *dev;
  175. void __iomem *base;
  176. int irq[13];
  177. struct clk *clk;
  178. u32 *buffer;
  179. struct iio_trigger *trig;
  180. struct mutex lock;
  181. struct completion completion;
  182. const u32 *vref_mv;
  183. struct mxs_lradc_scale scale_avail[LRADC_MAX_TOTAL_CHANS][2];
  184. unsigned long is_divided;
  185. /*
  186. * When the touchscreen is enabled, we give it two private virtual
  187. * channels: #6 and #7. This means that only 6 virtual channels (instead
  188. * of 8) will be available for buffered capture.
  189. */
  190. #define TOUCHSCREEN_VCHANNEL1 7
  191. #define TOUCHSCREEN_VCHANNEL2 6
  192. #define BUFFER_VCHANS_LIMITED 0x3f
  193. #define BUFFER_VCHANS_ALL 0xff
  194. u8 buffer_vchans;
  195. /*
  196. * Furthermore, certain LRADC channels are shared between touchscreen
  197. * and/or touch-buttons and generic LRADC block. Therefore when using
  198. * either of these, these channels are not available for the regular
  199. * sampling. The shared channels are as follows:
  200. *
  201. * CH0 -- Touch button #0
  202. * CH1 -- Touch button #1
  203. * CH2 -- Touch screen XPUL
  204. * CH3 -- Touch screen YPLL
  205. * CH4 -- Touch screen XNUL
  206. * CH5 -- Touch screen YNLR
  207. * CH6 -- Touch screen WIPER (5-wire only)
  208. *
  209. * The bit fields below represents which parts of the LRADC block are
  210. * switched into special mode of operation. These channels can not
  211. * be sampled as regular LRADC channels. The driver will refuse any
  212. * attempt to sample these channels.
  213. */
  214. #define CHAN_MASK_TOUCHBUTTON (BIT(1) | BIT(0))
  215. #define CHAN_MASK_TOUCHSCREEN_4WIRE (0xf << 2)
  216. #define CHAN_MASK_TOUCHSCREEN_5WIRE (0x1f << 2)
  217. enum mxs_lradc_ts use_touchscreen;
  218. bool use_touchbutton;
  219. struct input_dev *ts_input;
  220. enum mxs_lradc_id soc;
  221. enum lradc_ts_plate cur_plate; /* state machine */
  222. bool ts_valid;
  223. unsigned ts_x_pos;
  224. unsigned ts_y_pos;
  225. unsigned ts_pressure;
  226. /* handle touchscreen's physical behaviour */
  227. /* samples per coordinate */
  228. unsigned over_sample_cnt;
  229. /* time clocks between samples */
  230. unsigned over_sample_delay;
  231. /* time in clocks to wait after the plates where switched */
  232. unsigned settling_delay;
  233. };
  234. #define LRADC_CTRL0 0x00
  235. # define LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE BIT(23)
  236. # define LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE BIT(22)
  237. # define LRADC_CTRL0_MX28_YNNSW /* YM */ BIT(21)
  238. # define LRADC_CTRL0_MX28_YPNSW /* YP */ BIT(20)
  239. # define LRADC_CTRL0_MX28_YPPSW /* YP */ BIT(19)
  240. # define LRADC_CTRL0_MX28_XNNSW /* XM */ BIT(18)
  241. # define LRADC_CTRL0_MX28_XNPSW /* XM */ BIT(17)
  242. # define LRADC_CTRL0_MX28_XPPSW /* XP */ BIT(16)
  243. # define LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE BIT(20)
  244. # define LRADC_CTRL0_MX23_YM BIT(19)
  245. # define LRADC_CTRL0_MX23_XM BIT(18)
  246. # define LRADC_CTRL0_MX23_YP BIT(17)
  247. # define LRADC_CTRL0_MX23_XP BIT(16)
  248. # define LRADC_CTRL0_MX28_PLATE_MASK \
  249. (LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE | \
  250. LRADC_CTRL0_MX28_YNNSW | LRADC_CTRL0_MX28_YPNSW | \
  251. LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_XNNSW | \
  252. LRADC_CTRL0_MX28_XNPSW | LRADC_CTRL0_MX28_XPPSW)
  253. # define LRADC_CTRL0_MX23_PLATE_MASK \
  254. (LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE | \
  255. LRADC_CTRL0_MX23_YM | LRADC_CTRL0_MX23_XM | \
  256. LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_XP)
  257. #define LRADC_CTRL1 0x10
  258. #define LRADC_CTRL1_TOUCH_DETECT_IRQ_EN BIT(24)
  259. #define LRADC_CTRL1_LRADC_IRQ_EN(n) (1 << ((n) + 16))
  260. #define LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK (0x1fff << 16)
  261. #define LRADC_CTRL1_MX23_LRADC_IRQ_EN_MASK (0x01ff << 16)
  262. #define LRADC_CTRL1_LRADC_IRQ_EN_OFFSET 16
  263. #define LRADC_CTRL1_TOUCH_DETECT_IRQ BIT(8)
  264. #define LRADC_CTRL1_LRADC_IRQ(n) (1 << (n))
  265. #define LRADC_CTRL1_MX28_LRADC_IRQ_MASK 0x1fff
  266. #define LRADC_CTRL1_MX23_LRADC_IRQ_MASK 0x01ff
  267. #define LRADC_CTRL1_LRADC_IRQ_OFFSET 0
  268. #define LRADC_CTRL2 0x20
  269. #define LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET 24
  270. #define LRADC_CTRL2_TEMPSENSE_PWD BIT(15)
  271. #define LRADC_STATUS 0x40
  272. #define LRADC_STATUS_TOUCH_DETECT_RAW BIT(0)
  273. #define LRADC_CH(n) (0x50 + (0x10 * (n)))
  274. #define LRADC_CH_ACCUMULATE BIT(29)
  275. #define LRADC_CH_NUM_SAMPLES_MASK (0x1f << 24)
  276. #define LRADC_CH_NUM_SAMPLES_OFFSET 24
  277. #define LRADC_CH_NUM_SAMPLES(x) \
  278. ((x) << LRADC_CH_NUM_SAMPLES_OFFSET)
  279. #define LRADC_CH_VALUE_MASK 0x3ffff
  280. #define LRADC_CH_VALUE_OFFSET 0
  281. #define LRADC_DELAY(n) (0xd0 + (0x10 * (n)))
  282. #define LRADC_DELAY_TRIGGER_LRADCS_MASK (0xffUL << 24)
  283. #define LRADC_DELAY_TRIGGER_LRADCS_OFFSET 24
  284. #define LRADC_DELAY_TRIGGER(x) \
  285. (((x) << LRADC_DELAY_TRIGGER_LRADCS_OFFSET) & \
  286. LRADC_DELAY_TRIGGER_LRADCS_MASK)
  287. #define LRADC_DELAY_KICK BIT(20)
  288. #define LRADC_DELAY_TRIGGER_DELAYS_MASK (0xf << 16)
  289. #define LRADC_DELAY_TRIGGER_DELAYS_OFFSET 16
  290. #define LRADC_DELAY_TRIGGER_DELAYS(x) \
  291. (((x) << LRADC_DELAY_TRIGGER_DELAYS_OFFSET) & \
  292. LRADC_DELAY_TRIGGER_DELAYS_MASK)
  293. #define LRADC_DELAY_LOOP_COUNT_MASK (0x1f << 11)
  294. #define LRADC_DELAY_LOOP_COUNT_OFFSET 11
  295. #define LRADC_DELAY_LOOP(x) \
  296. (((x) << LRADC_DELAY_LOOP_COUNT_OFFSET) & \
  297. LRADC_DELAY_LOOP_COUNT_MASK)
  298. #define LRADC_DELAY_DELAY_MASK 0x7ff
  299. #define LRADC_DELAY_DELAY_OFFSET 0
  300. #define LRADC_DELAY_DELAY(x) \
  301. (((x) << LRADC_DELAY_DELAY_OFFSET) & \
  302. LRADC_DELAY_DELAY_MASK)
  303. #define LRADC_CTRL4 0x140
  304. #define LRADC_CTRL4_LRADCSELECT_MASK(n) (0xf << ((n) * 4))
  305. #define LRADC_CTRL4_LRADCSELECT_OFFSET(n) ((n) * 4)
  306. #define LRADC_CTRL4_LRADCSELECT(n, x) \
  307. (((x) << LRADC_CTRL4_LRADCSELECT_OFFSET(n)) & \
  308. LRADC_CTRL4_LRADCSELECT_MASK(n))
  309. #define LRADC_RESOLUTION 12
  310. #define LRADC_SINGLE_SAMPLE_MASK ((1 << LRADC_RESOLUTION) - 1)
  311. static void mxs_lradc_reg_set(struct mxs_lradc *lradc, u32 val, u32 reg)
  312. {
  313. writel(val, lradc->base + reg + STMP_OFFSET_REG_SET);
  314. }
  315. static void mxs_lradc_reg_clear(struct mxs_lradc *lradc, u32 val, u32 reg)
  316. {
  317. writel(val, lradc->base + reg + STMP_OFFSET_REG_CLR);
  318. }
  319. static void mxs_lradc_reg_wrt(struct mxs_lradc *lradc, u32 val, u32 reg)
  320. {
  321. writel(val, lradc->base + reg);
  322. }
  323. static u32 mxs_lradc_plate_mask(struct mxs_lradc *lradc)
  324. {
  325. if (lradc->soc == IMX23_LRADC)
  326. return LRADC_CTRL0_MX23_PLATE_MASK;
  327. return LRADC_CTRL0_MX28_PLATE_MASK;
  328. }
  329. static u32 mxs_lradc_irq_mask(struct mxs_lradc *lradc)
  330. {
  331. if (lradc->soc == IMX23_LRADC)
  332. return LRADC_CTRL1_MX23_LRADC_IRQ_MASK;
  333. return LRADC_CTRL1_MX28_LRADC_IRQ_MASK;
  334. }
  335. static u32 mxs_lradc_touch_detect_bit(struct mxs_lradc *lradc)
  336. {
  337. if (lradc->soc == IMX23_LRADC)
  338. return LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE;
  339. return LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE;
  340. }
  341. static u32 mxs_lradc_drive_x_plate(struct mxs_lradc *lradc)
  342. {
  343. if (lradc->soc == IMX23_LRADC)
  344. return LRADC_CTRL0_MX23_XP | LRADC_CTRL0_MX23_XM;
  345. return LRADC_CTRL0_MX28_XPPSW | LRADC_CTRL0_MX28_XNNSW;
  346. }
  347. static u32 mxs_lradc_drive_y_plate(struct mxs_lradc *lradc)
  348. {
  349. if (lradc->soc == IMX23_LRADC)
  350. return LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_YM;
  351. return LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_YNNSW;
  352. }
  353. static u32 mxs_lradc_drive_pressure(struct mxs_lradc *lradc)
  354. {
  355. if (lradc->soc == IMX23_LRADC)
  356. return LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_XM;
  357. return LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_XNNSW;
  358. }
  359. static bool mxs_lradc_check_touch_event(struct mxs_lradc *lradc)
  360. {
  361. return !!(readl(lradc->base + LRADC_STATUS) &
  362. LRADC_STATUS_TOUCH_DETECT_RAW);
  363. }
  364. static void mxs_lradc_map_channel(struct mxs_lradc *lradc, unsigned vch,
  365. unsigned ch)
  366. {
  367. mxs_lradc_reg_clear(lradc, LRADC_CTRL4_LRADCSELECT_MASK(vch),
  368. LRADC_CTRL4);
  369. mxs_lradc_reg_set(lradc, LRADC_CTRL4_LRADCSELECT(vch, ch), LRADC_CTRL4);
  370. }
  371. static void mxs_lradc_setup_ts_channel(struct mxs_lradc *lradc, unsigned ch)
  372. {
  373. /*
  374. * prepare for oversampling conversion
  375. *
  376. * from the datasheet:
  377. * "The ACCUMULATE bit in the appropriate channel register
  378. * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0;
  379. * otherwise, the IRQs will not fire."
  380. */
  381. mxs_lradc_reg_wrt(lradc, LRADC_CH_ACCUMULATE |
  382. LRADC_CH_NUM_SAMPLES(lradc->over_sample_cnt - 1),
  383. LRADC_CH(ch));
  384. /*
  385. * from the datasheet:
  386. * "Software must clear this register in preparation for a
  387. * multi-cycle accumulation.
  388. */
  389. mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch));
  390. /*
  391. * prepare the delay/loop unit according to the oversampling count
  392. *
  393. * from the datasheet:
  394. * "The DELAY fields in HW_LRADC_DELAY0, HW_LRADC_DELAY1,
  395. * HW_LRADC_DELAY2, and HW_LRADC_DELAY3 must be non-zero; otherwise,
  396. * the LRADC will not trigger the delay group."
  397. */
  398. mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << ch) |
  399. LRADC_DELAY_TRIGGER_DELAYS(0) |
  400. LRADC_DELAY_LOOP(lradc->over_sample_cnt - 1) |
  401. LRADC_DELAY_DELAY(lradc->over_sample_delay - 1),
  402. LRADC_DELAY(3));
  403. mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(ch), LRADC_CTRL1);
  404. /*
  405. * after changing the touchscreen plates setting
  406. * the signals need some initial time to settle. Start the
  407. * SoC's delay unit and start the conversion later
  408. * and automatically.
  409. */
  410. mxs_lradc_reg_wrt(
  411. lradc,
  412. LRADC_DELAY_TRIGGER(0) | /* don't trigger ADC */
  413. LRADC_DELAY_TRIGGER_DELAYS(BIT(3)) | /* trigger DELAY unit#3 */
  414. LRADC_DELAY_KICK |
  415. LRADC_DELAY_DELAY(lradc->settling_delay),
  416. LRADC_DELAY(2));
  417. }
  418. /*
  419. * Pressure detection is special:
  420. * We want to do both required measurements for the pressure detection in
  421. * one turn. Use the hardware features to chain both conversions and let the
  422. * hardware report one interrupt if both conversions are done
  423. */
  424. static void mxs_lradc_setup_ts_pressure(struct mxs_lradc *lradc, unsigned ch1,
  425. unsigned ch2)
  426. {
  427. u32 reg;
  428. /*
  429. * prepare for oversampling conversion
  430. *
  431. * from the datasheet:
  432. * "The ACCUMULATE bit in the appropriate channel register
  433. * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0;
  434. * otherwise, the IRQs will not fire."
  435. */
  436. reg = LRADC_CH_ACCUMULATE |
  437. LRADC_CH_NUM_SAMPLES(lradc->over_sample_cnt - 1);
  438. mxs_lradc_reg_wrt(lradc, reg, LRADC_CH(ch1));
  439. mxs_lradc_reg_wrt(lradc, reg, LRADC_CH(ch2));
  440. /*
  441. * from the datasheet:
  442. * "Software must clear this register in preparation for a
  443. * multi-cycle accumulation.
  444. */
  445. mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch1));
  446. mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch2));
  447. /* prepare the delay/loop unit according to the oversampling count */
  448. mxs_lradc_reg_wrt(
  449. lradc,
  450. LRADC_DELAY_TRIGGER(1 << ch1) |
  451. LRADC_DELAY_TRIGGER(1 << ch2) | /* start both channels */
  452. LRADC_DELAY_TRIGGER_DELAYS(0) |
  453. LRADC_DELAY_LOOP(lradc->over_sample_cnt - 1) |
  454. LRADC_DELAY_DELAY(lradc->over_sample_delay - 1),
  455. LRADC_DELAY(3));
  456. mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(ch2), LRADC_CTRL1);
  457. /*
  458. * after changing the touchscreen plates setting
  459. * the signals need some initial time to settle. Start the
  460. * SoC's delay unit and start the conversion later
  461. * and automatically.
  462. */
  463. mxs_lradc_reg_wrt(
  464. lradc,
  465. LRADC_DELAY_TRIGGER(0) | /* don't trigger ADC */
  466. LRADC_DELAY_TRIGGER_DELAYS(BIT(3)) | /* trigger DELAY unit#3 */
  467. LRADC_DELAY_KICK |
  468. LRADC_DELAY_DELAY(lradc->settling_delay), LRADC_DELAY(2));
  469. }
  470. static unsigned mxs_lradc_read_raw_channel(struct mxs_lradc *lradc,
  471. unsigned channel)
  472. {
  473. u32 reg;
  474. unsigned num_samples, val;
  475. reg = readl(lradc->base + LRADC_CH(channel));
  476. if (reg & LRADC_CH_ACCUMULATE)
  477. num_samples = lradc->over_sample_cnt;
  478. else
  479. num_samples = 1;
  480. val = (reg & LRADC_CH_VALUE_MASK) >> LRADC_CH_VALUE_OFFSET;
  481. return val / num_samples;
  482. }
  483. static unsigned mxs_lradc_read_ts_pressure(struct mxs_lradc *lradc,
  484. unsigned ch1, unsigned ch2)
  485. {
  486. u32 reg, mask;
  487. unsigned pressure, m1, m2;
  488. mask = LRADC_CTRL1_LRADC_IRQ(ch1) | LRADC_CTRL1_LRADC_IRQ(ch2);
  489. reg = readl(lradc->base + LRADC_CTRL1) & mask;
  490. while (reg != mask) {
  491. reg = readl(lradc->base + LRADC_CTRL1) & mask;
  492. dev_dbg(lradc->dev, "One channel is still busy: %X\n", reg);
  493. }
  494. m1 = mxs_lradc_read_raw_channel(lradc, ch1);
  495. m2 = mxs_lradc_read_raw_channel(lradc, ch2);
  496. if (m2 == 0) {
  497. dev_warn(lradc->dev, "Cannot calculate pressure\n");
  498. return 1 << (LRADC_RESOLUTION - 1);
  499. }
  500. /* simply scale the value from 0 ... max ADC resolution */
  501. pressure = m1;
  502. pressure *= (1 << LRADC_RESOLUTION);
  503. pressure /= m2;
  504. dev_dbg(lradc->dev, "Pressure = %u\n", pressure);
  505. return pressure;
  506. }
  507. #define TS_CH_XP 2
  508. #define TS_CH_YP 3
  509. #define TS_CH_XM 4
  510. #define TS_CH_YM 5
  511. /*
  512. * YP(open)--+-------------+
  513. * | |--+
  514. * | | |
  515. * YM(-)--+-------------+ |
  516. * +--------------+
  517. * | |
  518. * XP(weak+) XM(open)
  519. *
  520. * "weak+" means 200k Ohm VDDIO
  521. * (-) means GND
  522. */
  523. static void mxs_lradc_setup_touch_detection(struct mxs_lradc *lradc)
  524. {
  525. /*
  526. * In order to detect a touch event the 'touch detect enable' bit
  527. * enables:
  528. * - a weak pullup to the X+ connector
  529. * - a strong ground at the Y- connector
  530. */
  531. mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
  532. mxs_lradc_reg_set(lradc, mxs_lradc_touch_detect_bit(lradc),
  533. LRADC_CTRL0);
  534. }
  535. /*
  536. * YP(meas)--+-------------+
  537. * | |--+
  538. * | | |
  539. * YM(open)--+-------------+ |
  540. * +--------------+
  541. * | |
  542. * XP(+) XM(-)
  543. *
  544. * (+) means here 1.85 V
  545. * (-) means here GND
  546. */
  547. static void mxs_lradc_prepare_x_pos(struct mxs_lradc *lradc)
  548. {
  549. mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
  550. mxs_lradc_reg_set(lradc, mxs_lradc_drive_x_plate(lradc), LRADC_CTRL0);
  551. lradc->cur_plate = LRADC_SAMPLE_X;
  552. mxs_lradc_map_channel(lradc, TOUCHSCREEN_VCHANNEL1, TS_CH_YP);
  553. mxs_lradc_setup_ts_channel(lradc, TOUCHSCREEN_VCHANNEL1);
  554. }
  555. /*
  556. * YP(+)--+-------------+
  557. * | |--+
  558. * | | |
  559. * YM(-)--+-------------+ |
  560. * +--------------+
  561. * | |
  562. * XP(open) XM(meas)
  563. *
  564. * (+) means here 1.85 V
  565. * (-) means here GND
  566. */
  567. static void mxs_lradc_prepare_y_pos(struct mxs_lradc *lradc)
  568. {
  569. mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
  570. mxs_lradc_reg_set(lradc, mxs_lradc_drive_y_plate(lradc), LRADC_CTRL0);
  571. lradc->cur_plate = LRADC_SAMPLE_Y;
  572. mxs_lradc_map_channel(lradc, TOUCHSCREEN_VCHANNEL1, TS_CH_XM);
  573. mxs_lradc_setup_ts_channel(lradc, TOUCHSCREEN_VCHANNEL1);
  574. }
  575. /*
  576. * YP(+)--+-------------+
  577. * | |--+
  578. * | | |
  579. * YM(meas)--+-------------+ |
  580. * +--------------+
  581. * | |
  582. * XP(meas) XM(-)
  583. *
  584. * (+) means here 1.85 V
  585. * (-) means here GND
  586. */
  587. static void mxs_lradc_prepare_pressure(struct mxs_lradc *lradc)
  588. {
  589. mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
  590. mxs_lradc_reg_set(lradc, mxs_lradc_drive_pressure(lradc), LRADC_CTRL0);
  591. lradc->cur_plate = LRADC_SAMPLE_PRESSURE;
  592. mxs_lradc_map_channel(lradc, TOUCHSCREEN_VCHANNEL1, TS_CH_YM);
  593. mxs_lradc_map_channel(lradc, TOUCHSCREEN_VCHANNEL2, TS_CH_XP);
  594. mxs_lradc_setup_ts_pressure(lradc, TOUCHSCREEN_VCHANNEL2,
  595. TOUCHSCREEN_VCHANNEL1);
  596. }
  597. static void mxs_lradc_enable_touch_detection(struct mxs_lradc *lradc)
  598. {
  599. /* Configure the touchscreen type */
  600. if (lradc->soc == IMX28_LRADC) {
  601. mxs_lradc_reg_clear(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
  602. LRADC_CTRL0);
  603. if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE)
  604. mxs_lradc_reg_set(lradc,
  605. LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
  606. LRADC_CTRL0);
  607. }
  608. mxs_lradc_setup_touch_detection(lradc);
  609. lradc->cur_plate = LRADC_TOUCH;
  610. mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ |
  611. LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1);
  612. mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1);
  613. }
  614. static void mxs_lradc_start_touch_event(struct mxs_lradc *lradc)
  615. {
  616. mxs_lradc_reg_clear(lradc,
  617. LRADC_CTRL1_TOUCH_DETECT_IRQ_EN,
  618. LRADC_CTRL1);
  619. mxs_lradc_reg_set(lradc,
  620. LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL1),
  621. LRADC_CTRL1);
  622. /*
  623. * start with the Y-pos, because it uses nearly the same plate
  624. * settings like the touch detection
  625. */
  626. mxs_lradc_prepare_y_pos(lradc);
  627. }
  628. static void mxs_lradc_report_ts_event(struct mxs_lradc *lradc)
  629. {
  630. input_report_abs(lradc->ts_input, ABS_X, lradc->ts_x_pos);
  631. input_report_abs(lradc->ts_input, ABS_Y, lradc->ts_y_pos);
  632. input_report_abs(lradc->ts_input, ABS_PRESSURE, lradc->ts_pressure);
  633. input_report_key(lradc->ts_input, BTN_TOUCH, 1);
  634. input_sync(lradc->ts_input);
  635. }
  636. static void mxs_lradc_complete_touch_event(struct mxs_lradc *lradc)
  637. {
  638. mxs_lradc_setup_touch_detection(lradc);
  639. lradc->cur_plate = LRADC_SAMPLE_VALID;
  640. /*
  641. * start a dummy conversion to burn time to settle the signals
  642. * note: we are not interested in the conversion's value
  643. */
  644. mxs_lradc_reg_wrt(lradc, 0, LRADC_CH(TOUCHSCREEN_VCHANNEL1));
  645. mxs_lradc_reg_clear(lradc,
  646. LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1) |
  647. LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL2),
  648. LRADC_CTRL1);
  649. mxs_lradc_reg_wrt(
  650. lradc,
  651. LRADC_DELAY_TRIGGER(1 << TOUCHSCREEN_VCHANNEL1) |
  652. LRADC_DELAY_KICK | LRADC_DELAY_DELAY(10), /* waste 5 ms */
  653. LRADC_DELAY(2));
  654. }
  655. /*
  656. * in order to avoid false measurements, report only samples where
  657. * the surface is still touched after the position measurement
  658. */
  659. static void mxs_lradc_finish_touch_event(struct mxs_lradc *lradc, bool valid)
  660. {
  661. /* if it is still touched, report the sample */
  662. if (valid && mxs_lradc_check_touch_event(lradc)) {
  663. lradc->ts_valid = true;
  664. mxs_lradc_report_ts_event(lradc);
  665. }
  666. /* if it is even still touched, continue with the next measurement */
  667. if (mxs_lradc_check_touch_event(lradc)) {
  668. mxs_lradc_prepare_y_pos(lradc);
  669. return;
  670. }
  671. if (lradc->ts_valid) {
  672. /* signal the release */
  673. lradc->ts_valid = false;
  674. input_report_key(lradc->ts_input, BTN_TOUCH, 0);
  675. input_sync(lradc->ts_input);
  676. }
  677. /* if it is released, wait for the next touch via IRQ */
  678. lradc->cur_plate = LRADC_TOUCH;
  679. mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2));
  680. mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3));
  681. mxs_lradc_reg_clear(lradc,
  682. LRADC_CTRL1_TOUCH_DETECT_IRQ |
  683. LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL1) |
  684. LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1),
  685. LRADC_CTRL1);
  686. mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1);
  687. }
  688. /* touchscreen's state machine */
  689. static void mxs_lradc_handle_touch(struct mxs_lradc *lradc)
  690. {
  691. switch (lradc->cur_plate) {
  692. case LRADC_TOUCH:
  693. if (mxs_lradc_check_touch_event(lradc))
  694. mxs_lradc_start_touch_event(lradc);
  695. mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ,
  696. LRADC_CTRL1);
  697. return;
  698. case LRADC_SAMPLE_Y:
  699. lradc->ts_y_pos =
  700. mxs_lradc_read_raw_channel(lradc,
  701. TOUCHSCREEN_VCHANNEL1);
  702. mxs_lradc_prepare_x_pos(lradc);
  703. return;
  704. case LRADC_SAMPLE_X:
  705. lradc->ts_x_pos =
  706. mxs_lradc_read_raw_channel(lradc,
  707. TOUCHSCREEN_VCHANNEL1);
  708. mxs_lradc_prepare_pressure(lradc);
  709. return;
  710. case LRADC_SAMPLE_PRESSURE:
  711. lradc->ts_pressure =
  712. mxs_lradc_read_ts_pressure(lradc,
  713. TOUCHSCREEN_VCHANNEL2,
  714. TOUCHSCREEN_VCHANNEL1);
  715. mxs_lradc_complete_touch_event(lradc);
  716. return;
  717. case LRADC_SAMPLE_VALID:
  718. mxs_lradc_finish_touch_event(lradc, 1);
  719. break;
  720. }
  721. }
  722. /*
  723. * Raw I/O operations
  724. */
  725. static int mxs_lradc_read_single(struct iio_dev *iio_dev, int chan, int *val)
  726. {
  727. struct mxs_lradc *lradc = iio_priv(iio_dev);
  728. int ret;
  729. /*
  730. * See if there is no buffered operation in progress. If there is, simply
  731. * bail out. This can be improved to support both buffered and raw IO at
  732. * the same time, yet the code becomes horribly complicated. Therefore I
  733. * applied KISS principle here.
  734. */
  735. ret = mutex_trylock(&lradc->lock);
  736. if (!ret)
  737. return -EBUSY;
  738. reinit_completion(&lradc->completion);
  739. /*
  740. * No buffered operation in progress, map the channel and trigger it.
  741. * Virtual channel 0 is always used here as the others are always not
  742. * used if doing raw sampling.
  743. */
  744. if (lradc->soc == IMX28_LRADC)
  745. mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(0),
  746. LRADC_CTRL1);
  747. mxs_lradc_reg_clear(lradc, 0x1, LRADC_CTRL0);
  748. /* Enable / disable the divider per requirement */
  749. if (test_bit(chan, &lradc->is_divided))
  750. mxs_lradc_reg_set(lradc,
  751. 1 << LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET,
  752. LRADC_CTRL2);
  753. else
  754. mxs_lradc_reg_clear(lradc,
  755. 1 << LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET,
  756. LRADC_CTRL2);
  757. /* Clean the slot's previous content, then set new one. */
  758. mxs_lradc_reg_clear(lradc, LRADC_CTRL4_LRADCSELECT_MASK(0),
  759. LRADC_CTRL4);
  760. mxs_lradc_reg_set(lradc, chan, LRADC_CTRL4);
  761. mxs_lradc_reg_wrt(lradc, 0, LRADC_CH(0));
  762. /* Enable the IRQ and start sampling the channel. */
  763. mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(0), LRADC_CTRL1);
  764. mxs_lradc_reg_set(lradc, BIT(0), LRADC_CTRL0);
  765. /* Wait for completion on the channel, 1 second max. */
  766. ret = wait_for_completion_killable_timeout(&lradc->completion, HZ);
  767. if (!ret)
  768. ret = -ETIMEDOUT;
  769. if (ret < 0)
  770. goto err;
  771. /* Read the data. */
  772. *val = readl(lradc->base + LRADC_CH(0)) & LRADC_CH_VALUE_MASK;
  773. ret = IIO_VAL_INT;
  774. err:
  775. mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(0), LRADC_CTRL1);
  776. mutex_unlock(&lradc->lock);
  777. return ret;
  778. }
  779. static int mxs_lradc_read_temp(struct iio_dev *iio_dev, int *val)
  780. {
  781. int ret, min, max;
  782. ret = mxs_lradc_read_single(iio_dev, 8, &min);
  783. if (ret != IIO_VAL_INT)
  784. return ret;
  785. ret = mxs_lradc_read_single(iio_dev, 9, &max);
  786. if (ret != IIO_VAL_INT)
  787. return ret;
  788. *val = max - min;
  789. return IIO_VAL_INT;
  790. }
  791. static int mxs_lradc_read_raw(struct iio_dev *iio_dev,
  792. const struct iio_chan_spec *chan,
  793. int *val, int *val2, long m)
  794. {
  795. struct mxs_lradc *lradc = iio_priv(iio_dev);
  796. switch (m) {
  797. case IIO_CHAN_INFO_RAW:
  798. if (chan->type == IIO_TEMP)
  799. return mxs_lradc_read_temp(iio_dev, val);
  800. return mxs_lradc_read_single(iio_dev, chan->channel, val);
  801. case IIO_CHAN_INFO_SCALE:
  802. if (chan->type == IIO_TEMP) {
  803. /*
  804. * From the datasheet, we have to multiply by 1.012 and
  805. * divide by 4
  806. */
  807. *val = 0;
  808. *val2 = 253000;
  809. return IIO_VAL_INT_PLUS_MICRO;
  810. }
  811. *val = lradc->vref_mv[chan->channel];
  812. *val2 = chan->scan_type.realbits -
  813. test_bit(chan->channel, &lradc->is_divided);
  814. return IIO_VAL_FRACTIONAL_LOG2;
  815. case IIO_CHAN_INFO_OFFSET:
  816. if (chan->type == IIO_TEMP) {
  817. /*
  818. * The calculated value from the ADC is in Kelvin, we
  819. * want Celsius for hwmon so the offset is -273.15
  820. * The offset is applied before scaling so it is
  821. * actually -213.15 * 4 / 1.012 = -1079.644268
  822. */
  823. *val = -1079;
  824. *val2 = 644268;
  825. return IIO_VAL_INT_PLUS_MICRO;
  826. }
  827. return -EINVAL;
  828. default:
  829. break;
  830. }
  831. return -EINVAL;
  832. }
  833. static int mxs_lradc_write_raw(struct iio_dev *iio_dev,
  834. const struct iio_chan_spec *chan,
  835. int val, int val2, long m)
  836. {
  837. struct mxs_lradc *lradc = iio_priv(iio_dev);
  838. struct mxs_lradc_scale *scale_avail =
  839. lradc->scale_avail[chan->channel];
  840. int ret;
  841. ret = mutex_trylock(&lradc->lock);
  842. if (!ret)
  843. return -EBUSY;
  844. switch (m) {
  845. case IIO_CHAN_INFO_SCALE:
  846. ret = -EINVAL;
  847. if (val == scale_avail[MXS_LRADC_DIV_DISABLED].integer &&
  848. val2 == scale_avail[MXS_LRADC_DIV_DISABLED].nano) {
  849. /* divider by two disabled */
  850. clear_bit(chan->channel, &lradc->is_divided);
  851. ret = 0;
  852. } else if (val == scale_avail[MXS_LRADC_DIV_ENABLED].integer &&
  853. val2 == scale_avail[MXS_LRADC_DIV_ENABLED].nano) {
  854. /* divider by two enabled */
  855. set_bit(chan->channel, &lradc->is_divided);
  856. ret = 0;
  857. }
  858. break;
  859. default:
  860. ret = -EINVAL;
  861. break;
  862. }
  863. mutex_unlock(&lradc->lock);
  864. return ret;
  865. }
  866. static int mxs_lradc_write_raw_get_fmt(struct iio_dev *iio_dev,
  867. const struct iio_chan_spec *chan,
  868. long m)
  869. {
  870. return IIO_VAL_INT_PLUS_NANO;
  871. }
  872. static ssize_t mxs_lradc_show_scale_available_ch(struct device *dev,
  873. struct device_attribute *attr,
  874. char *buf,
  875. int ch)
  876. {
  877. struct iio_dev *iio = dev_to_iio_dev(dev);
  878. struct mxs_lradc *lradc = iio_priv(iio);
  879. int i, len = 0;
  880. for (i = 0; i < ARRAY_SIZE(lradc->scale_avail[ch]); i++)
  881. len += sprintf(buf + len, "%u.%09u ",
  882. lradc->scale_avail[ch][i].integer,
  883. lradc->scale_avail[ch][i].nano);
  884. len += sprintf(buf + len, "\n");
  885. return len;
  886. }
  887. static ssize_t mxs_lradc_show_scale_available(struct device *dev,
  888. struct device_attribute *attr,
  889. char *buf)
  890. {
  891. struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
  892. return mxs_lradc_show_scale_available_ch(dev, attr, buf,
  893. iio_attr->address);
  894. }
  895. #define SHOW_SCALE_AVAILABLE_ATTR(ch) \
  896. static IIO_DEVICE_ATTR(in_voltage##ch##_scale_available, S_IRUGO, \
  897. mxs_lradc_show_scale_available, NULL, ch)
  898. SHOW_SCALE_AVAILABLE_ATTR(0);
  899. SHOW_SCALE_AVAILABLE_ATTR(1);
  900. SHOW_SCALE_AVAILABLE_ATTR(2);
  901. SHOW_SCALE_AVAILABLE_ATTR(3);
  902. SHOW_SCALE_AVAILABLE_ATTR(4);
  903. SHOW_SCALE_AVAILABLE_ATTR(5);
  904. SHOW_SCALE_AVAILABLE_ATTR(6);
  905. SHOW_SCALE_AVAILABLE_ATTR(7);
  906. SHOW_SCALE_AVAILABLE_ATTR(10);
  907. SHOW_SCALE_AVAILABLE_ATTR(11);
  908. SHOW_SCALE_AVAILABLE_ATTR(12);
  909. SHOW_SCALE_AVAILABLE_ATTR(13);
  910. SHOW_SCALE_AVAILABLE_ATTR(14);
  911. SHOW_SCALE_AVAILABLE_ATTR(15);
  912. static struct attribute *mxs_lradc_attributes[] = {
  913. &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
  914. &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
  915. &iio_dev_attr_in_voltage2_scale_available.dev_attr.attr,
  916. &iio_dev_attr_in_voltage3_scale_available.dev_attr.attr,
  917. &iio_dev_attr_in_voltage4_scale_available.dev_attr.attr,
  918. &iio_dev_attr_in_voltage5_scale_available.dev_attr.attr,
  919. &iio_dev_attr_in_voltage6_scale_available.dev_attr.attr,
  920. &iio_dev_attr_in_voltage7_scale_available.dev_attr.attr,
  921. &iio_dev_attr_in_voltage10_scale_available.dev_attr.attr,
  922. &iio_dev_attr_in_voltage11_scale_available.dev_attr.attr,
  923. &iio_dev_attr_in_voltage12_scale_available.dev_attr.attr,
  924. &iio_dev_attr_in_voltage13_scale_available.dev_attr.attr,
  925. &iio_dev_attr_in_voltage14_scale_available.dev_attr.attr,
  926. &iio_dev_attr_in_voltage15_scale_available.dev_attr.attr,
  927. NULL
  928. };
  929. static const struct attribute_group mxs_lradc_attribute_group = {
  930. .attrs = mxs_lradc_attributes,
  931. };
  932. static const struct iio_info mxs_lradc_iio_info = {
  933. .driver_module = THIS_MODULE,
  934. .read_raw = mxs_lradc_read_raw,
  935. .write_raw = mxs_lradc_write_raw,
  936. .write_raw_get_fmt = mxs_lradc_write_raw_get_fmt,
  937. .attrs = &mxs_lradc_attribute_group,
  938. };
  939. static int mxs_lradc_ts_open(struct input_dev *dev)
  940. {
  941. struct mxs_lradc *lradc = input_get_drvdata(dev);
  942. /* Enable the touch-detect circuitry. */
  943. mxs_lradc_enable_touch_detection(lradc);
  944. return 0;
  945. }
  946. static void mxs_lradc_disable_ts(struct mxs_lradc *lradc)
  947. {
  948. /* stop all interrupts from firing */
  949. mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN |
  950. LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL1) |
  951. LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL2), LRADC_CTRL1);
  952. /* Power-down touchscreen touch-detect circuitry. */
  953. mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
  954. }
  955. static void mxs_lradc_ts_close(struct input_dev *dev)
  956. {
  957. struct mxs_lradc *lradc = input_get_drvdata(dev);
  958. mxs_lradc_disable_ts(lradc);
  959. }
  960. static int mxs_lradc_ts_register(struct mxs_lradc *lradc)
  961. {
  962. struct input_dev *input;
  963. struct device *dev = lradc->dev;
  964. if (!lradc->use_touchscreen)
  965. return 0;
  966. input = devm_input_allocate_device(dev);
  967. if (!input)
  968. return -ENOMEM;
  969. input->name = DRIVER_NAME;
  970. input->id.bustype = BUS_HOST;
  971. input->open = mxs_lradc_ts_open;
  972. input->close = mxs_lradc_ts_close;
  973. __set_bit(EV_ABS, input->evbit);
  974. __set_bit(EV_KEY, input->evbit);
  975. __set_bit(BTN_TOUCH, input->keybit);
  976. __set_bit(INPUT_PROP_DIRECT, input->propbit);
  977. input_set_abs_params(input, ABS_X, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0);
  978. input_set_abs_params(input, ABS_Y, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0);
  979. input_set_abs_params(input, ABS_PRESSURE, 0, LRADC_SINGLE_SAMPLE_MASK,
  980. 0, 0);
  981. lradc->ts_input = input;
  982. input_set_drvdata(input, lradc);
  983. return input_register_device(input);
  984. }
  985. /*
  986. * IRQ Handling
  987. */
  988. static irqreturn_t mxs_lradc_handle_irq(int irq, void *data)
  989. {
  990. struct iio_dev *iio = data;
  991. struct mxs_lradc *lradc = iio_priv(iio);
  992. unsigned long reg = readl(lradc->base + LRADC_CTRL1);
  993. u32 clr_irq = mxs_lradc_irq_mask(lradc);
  994. const u32 ts_irq_mask =
  995. LRADC_CTRL1_TOUCH_DETECT_IRQ |
  996. LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1) |
  997. LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL2);
  998. if (!(reg & mxs_lradc_irq_mask(lradc)))
  999. return IRQ_NONE;
  1000. if (lradc->use_touchscreen && (reg & ts_irq_mask)) {
  1001. mxs_lradc_handle_touch(lradc);
  1002. /* Make sure we don't clear the next conversion's interrupt. */
  1003. clr_irq &= ~(LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1) |
  1004. LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL2));
  1005. }
  1006. if (iio_buffer_enabled(iio)) {
  1007. if (reg & lradc->buffer_vchans)
  1008. iio_trigger_poll(iio->trig);
  1009. } else if (reg & LRADC_CTRL1_LRADC_IRQ(0)) {
  1010. complete(&lradc->completion);
  1011. }
  1012. mxs_lradc_reg_clear(lradc, reg & clr_irq, LRADC_CTRL1);
  1013. return IRQ_HANDLED;
  1014. }
  1015. /*
  1016. * Trigger handling
  1017. */
  1018. static irqreturn_t mxs_lradc_trigger_handler(int irq, void *p)
  1019. {
  1020. struct iio_poll_func *pf = p;
  1021. struct iio_dev *iio = pf->indio_dev;
  1022. struct mxs_lradc *lradc = iio_priv(iio);
  1023. const u32 chan_value = LRADC_CH_ACCUMULATE |
  1024. ((LRADC_DELAY_TIMER_LOOP - 1) << LRADC_CH_NUM_SAMPLES_OFFSET);
  1025. unsigned int i, j = 0;
  1026. for_each_set_bit(i, iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS) {
  1027. lradc->buffer[j] = readl(lradc->base + LRADC_CH(j));
  1028. mxs_lradc_reg_wrt(lradc, chan_value, LRADC_CH(j));
  1029. lradc->buffer[j] &= LRADC_CH_VALUE_MASK;
  1030. lradc->buffer[j] /= LRADC_DELAY_TIMER_LOOP;
  1031. j++;
  1032. }
  1033. iio_push_to_buffers_with_timestamp(iio, lradc->buffer, pf->timestamp);
  1034. iio_trigger_notify_done(iio->trig);
  1035. return IRQ_HANDLED;
  1036. }
  1037. static int mxs_lradc_configure_trigger(struct iio_trigger *trig, bool state)
  1038. {
  1039. struct iio_dev *iio = iio_trigger_get_drvdata(trig);
  1040. struct mxs_lradc *lradc = iio_priv(iio);
  1041. const u32 st = state ? STMP_OFFSET_REG_SET : STMP_OFFSET_REG_CLR;
  1042. mxs_lradc_reg_wrt(lradc, LRADC_DELAY_KICK, LRADC_DELAY(0) + st);
  1043. return 0;
  1044. }
  1045. static const struct iio_trigger_ops mxs_lradc_trigger_ops = {
  1046. .owner = THIS_MODULE,
  1047. .set_trigger_state = &mxs_lradc_configure_trigger,
  1048. };
  1049. static int mxs_lradc_trigger_init(struct iio_dev *iio)
  1050. {
  1051. int ret;
  1052. struct iio_trigger *trig;
  1053. struct mxs_lradc *lradc = iio_priv(iio);
  1054. trig = iio_trigger_alloc("%s-dev%i", iio->name, iio->id);
  1055. if (!trig)
  1056. return -ENOMEM;
  1057. trig->dev.parent = lradc->dev;
  1058. iio_trigger_set_drvdata(trig, iio);
  1059. trig->ops = &mxs_lradc_trigger_ops;
  1060. ret = iio_trigger_register(trig);
  1061. if (ret) {
  1062. iio_trigger_free(trig);
  1063. return ret;
  1064. }
  1065. lradc->trig = trig;
  1066. return 0;
  1067. }
  1068. static void mxs_lradc_trigger_remove(struct iio_dev *iio)
  1069. {
  1070. struct mxs_lradc *lradc = iio_priv(iio);
  1071. iio_trigger_unregister(lradc->trig);
  1072. iio_trigger_free(lradc->trig);
  1073. }
  1074. static int mxs_lradc_buffer_preenable(struct iio_dev *iio)
  1075. {
  1076. struct mxs_lradc *lradc = iio_priv(iio);
  1077. int ret = 0, chan, ofs = 0;
  1078. unsigned long enable = 0;
  1079. u32 ctrl4_set = 0;
  1080. u32 ctrl4_clr = 0;
  1081. u32 ctrl1_irq = 0;
  1082. const u32 chan_value = LRADC_CH_ACCUMULATE |
  1083. ((LRADC_DELAY_TIMER_LOOP - 1) << LRADC_CH_NUM_SAMPLES_OFFSET);
  1084. const int len = bitmap_weight(iio->active_scan_mask,
  1085. LRADC_MAX_TOTAL_CHANS);
  1086. if (!len)
  1087. return -EINVAL;
  1088. /*
  1089. * Lock the driver so raw access can not be done during buffered
  1090. * operation. This simplifies the code a lot.
  1091. */
  1092. ret = mutex_trylock(&lradc->lock);
  1093. if (!ret)
  1094. return -EBUSY;
  1095. lradc->buffer = kmalloc_array(len, sizeof(*lradc->buffer), GFP_KERNEL);
  1096. if (!lradc->buffer) {
  1097. ret = -ENOMEM;
  1098. goto err_mem;
  1099. }
  1100. if (lradc->soc == IMX28_LRADC)
  1101. mxs_lradc_reg_clear(
  1102. lradc,
  1103. lradc->buffer_vchans << LRADC_CTRL1_LRADC_IRQ_EN_OFFSET,
  1104. LRADC_CTRL1);
  1105. mxs_lradc_reg_clear(lradc, lradc->buffer_vchans, LRADC_CTRL0);
  1106. for_each_set_bit(chan, iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS) {
  1107. ctrl4_set |= chan << LRADC_CTRL4_LRADCSELECT_OFFSET(ofs);
  1108. ctrl4_clr |= LRADC_CTRL4_LRADCSELECT_MASK(ofs);
  1109. ctrl1_irq |= LRADC_CTRL1_LRADC_IRQ_EN(ofs);
  1110. mxs_lradc_reg_wrt(lradc, chan_value, LRADC_CH(ofs));
  1111. bitmap_set(&enable, ofs, 1);
  1112. ofs++;
  1113. }
  1114. mxs_lradc_reg_clear(lradc, LRADC_DELAY_TRIGGER_LRADCS_MASK |
  1115. LRADC_DELAY_KICK, LRADC_DELAY(0));
  1116. mxs_lradc_reg_clear(lradc, ctrl4_clr, LRADC_CTRL4);
  1117. mxs_lradc_reg_set(lradc, ctrl4_set, LRADC_CTRL4);
  1118. mxs_lradc_reg_set(lradc, ctrl1_irq, LRADC_CTRL1);
  1119. mxs_lradc_reg_set(lradc, enable << LRADC_DELAY_TRIGGER_LRADCS_OFFSET,
  1120. LRADC_DELAY(0));
  1121. return 0;
  1122. err_mem:
  1123. mutex_unlock(&lradc->lock);
  1124. return ret;
  1125. }
  1126. static int mxs_lradc_buffer_postdisable(struct iio_dev *iio)
  1127. {
  1128. struct mxs_lradc *lradc = iio_priv(iio);
  1129. mxs_lradc_reg_clear(lradc, LRADC_DELAY_TRIGGER_LRADCS_MASK |
  1130. LRADC_DELAY_KICK, LRADC_DELAY(0));
  1131. mxs_lradc_reg_clear(lradc, lradc->buffer_vchans, LRADC_CTRL0);
  1132. if (lradc->soc == IMX28_LRADC)
  1133. mxs_lradc_reg_clear(
  1134. lradc,
  1135. lradc->buffer_vchans << LRADC_CTRL1_LRADC_IRQ_EN_OFFSET,
  1136. LRADC_CTRL1);
  1137. kfree(lradc->buffer);
  1138. mutex_unlock(&lradc->lock);
  1139. return 0;
  1140. }
  1141. static bool mxs_lradc_validate_scan_mask(struct iio_dev *iio,
  1142. const unsigned long *mask)
  1143. {
  1144. struct mxs_lradc *lradc = iio_priv(iio);
  1145. const int map_chans = bitmap_weight(mask, LRADC_MAX_TOTAL_CHANS);
  1146. int rsvd_chans = 0;
  1147. unsigned long rsvd_mask = 0;
  1148. if (lradc->use_touchbutton)
  1149. rsvd_mask |= CHAN_MASK_TOUCHBUTTON;
  1150. if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_4WIRE)
  1151. rsvd_mask |= CHAN_MASK_TOUCHSCREEN_4WIRE;
  1152. if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE)
  1153. rsvd_mask |= CHAN_MASK_TOUCHSCREEN_5WIRE;
  1154. if (lradc->use_touchbutton)
  1155. rsvd_chans++;
  1156. if (lradc->use_touchscreen)
  1157. rsvd_chans += 2;
  1158. /* Test for attempts to map channels with special mode of operation. */
  1159. if (bitmap_intersects(mask, &rsvd_mask, LRADC_MAX_TOTAL_CHANS))
  1160. return false;
  1161. /* Test for attempts to map more channels then available slots. */
  1162. if (map_chans + rsvd_chans > LRADC_MAX_MAPPED_CHANS)
  1163. return false;
  1164. return true;
  1165. }
  1166. static const struct iio_buffer_setup_ops mxs_lradc_buffer_ops = {
  1167. .preenable = &mxs_lradc_buffer_preenable,
  1168. .postenable = &iio_triggered_buffer_postenable,
  1169. .predisable = &iio_triggered_buffer_predisable,
  1170. .postdisable = &mxs_lradc_buffer_postdisable,
  1171. .validate_scan_mask = &mxs_lradc_validate_scan_mask,
  1172. };
  1173. /*
  1174. * Driver initialization
  1175. */
  1176. #define MXS_ADC_CHAN(idx, chan_type, name) { \
  1177. .type = (chan_type), \
  1178. .indexed = 1, \
  1179. .scan_index = (idx), \
  1180. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  1181. BIT(IIO_CHAN_INFO_SCALE), \
  1182. .channel = (idx), \
  1183. .address = (idx), \
  1184. .scan_type = { \
  1185. .sign = 'u', \
  1186. .realbits = LRADC_RESOLUTION, \
  1187. .storagebits = 32, \
  1188. }, \
  1189. .datasheet_name = (name), \
  1190. }
  1191. static const struct iio_chan_spec mx23_lradc_chan_spec[] = {
  1192. MXS_ADC_CHAN(0, IIO_VOLTAGE, "LRADC0"),
  1193. MXS_ADC_CHAN(1, IIO_VOLTAGE, "LRADC1"),
  1194. MXS_ADC_CHAN(2, IIO_VOLTAGE, "LRADC2"),
  1195. MXS_ADC_CHAN(3, IIO_VOLTAGE, "LRADC3"),
  1196. MXS_ADC_CHAN(4, IIO_VOLTAGE, "LRADC4"),
  1197. MXS_ADC_CHAN(5, IIO_VOLTAGE, "LRADC5"),
  1198. MXS_ADC_CHAN(6, IIO_VOLTAGE, "VDDIO"),
  1199. MXS_ADC_CHAN(7, IIO_VOLTAGE, "VBATT"),
  1200. /* Combined Temperature sensors */
  1201. {
  1202. .type = IIO_TEMP,
  1203. .indexed = 1,
  1204. .scan_index = 8,
  1205. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1206. BIT(IIO_CHAN_INFO_OFFSET) |
  1207. BIT(IIO_CHAN_INFO_SCALE),
  1208. .channel = 8,
  1209. .scan_type = {.sign = 'u', .realbits = 18, .storagebits = 32,},
  1210. .datasheet_name = "TEMP_DIE",
  1211. },
  1212. /* Hidden channel to keep indexes */
  1213. {
  1214. .type = IIO_TEMP,
  1215. .indexed = 1,
  1216. .scan_index = -1,
  1217. .channel = 9,
  1218. },
  1219. MXS_ADC_CHAN(10, IIO_VOLTAGE, NULL),
  1220. MXS_ADC_CHAN(11, IIO_VOLTAGE, NULL),
  1221. MXS_ADC_CHAN(12, IIO_VOLTAGE, "USB_DP"),
  1222. MXS_ADC_CHAN(13, IIO_VOLTAGE, "USB_DN"),
  1223. MXS_ADC_CHAN(14, IIO_VOLTAGE, "VBG"),
  1224. MXS_ADC_CHAN(15, IIO_VOLTAGE, "VDD5V"),
  1225. };
  1226. static const struct iio_chan_spec mx28_lradc_chan_spec[] = {
  1227. MXS_ADC_CHAN(0, IIO_VOLTAGE, "LRADC0"),
  1228. MXS_ADC_CHAN(1, IIO_VOLTAGE, "LRADC1"),
  1229. MXS_ADC_CHAN(2, IIO_VOLTAGE, "LRADC2"),
  1230. MXS_ADC_CHAN(3, IIO_VOLTAGE, "LRADC3"),
  1231. MXS_ADC_CHAN(4, IIO_VOLTAGE, "LRADC4"),
  1232. MXS_ADC_CHAN(5, IIO_VOLTAGE, "LRADC5"),
  1233. MXS_ADC_CHAN(6, IIO_VOLTAGE, "LRADC6"),
  1234. MXS_ADC_CHAN(7, IIO_VOLTAGE, "VBATT"),
  1235. /* Combined Temperature sensors */
  1236. {
  1237. .type = IIO_TEMP,
  1238. .indexed = 1,
  1239. .scan_index = 8,
  1240. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1241. BIT(IIO_CHAN_INFO_OFFSET) |
  1242. BIT(IIO_CHAN_INFO_SCALE),
  1243. .channel = 8,
  1244. .scan_type = {.sign = 'u', .realbits = 18, .storagebits = 32,},
  1245. .datasheet_name = "TEMP_DIE",
  1246. },
  1247. /* Hidden channel to keep indexes */
  1248. {
  1249. .type = IIO_TEMP,
  1250. .indexed = 1,
  1251. .scan_index = -1,
  1252. .channel = 9,
  1253. },
  1254. MXS_ADC_CHAN(10, IIO_VOLTAGE, "VDDIO"),
  1255. MXS_ADC_CHAN(11, IIO_VOLTAGE, "VTH"),
  1256. MXS_ADC_CHAN(12, IIO_VOLTAGE, "VDDA"),
  1257. MXS_ADC_CHAN(13, IIO_VOLTAGE, "VDDD"),
  1258. MXS_ADC_CHAN(14, IIO_VOLTAGE, "VBG"),
  1259. MXS_ADC_CHAN(15, IIO_VOLTAGE, "VDD5V"),
  1260. };
  1261. static void mxs_lradc_hw_init(struct mxs_lradc *lradc)
  1262. {
  1263. /* The ADC always uses DELAY CHANNEL 0. */
  1264. const u32 adc_cfg =
  1265. (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET + 0)) |
  1266. (LRADC_DELAY_TIMER_PER << LRADC_DELAY_DELAY_OFFSET);
  1267. /* Configure DELAY CHANNEL 0 for generic ADC sampling. */
  1268. mxs_lradc_reg_wrt(lradc, adc_cfg, LRADC_DELAY(0));
  1269. /* Disable remaining DELAY CHANNELs */
  1270. mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(1));
  1271. mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2));
  1272. mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3));
  1273. /* Start internal temperature sensing. */
  1274. mxs_lradc_reg_wrt(lradc, 0, LRADC_CTRL2);
  1275. }
  1276. static void mxs_lradc_hw_stop(struct mxs_lradc *lradc)
  1277. {
  1278. int i;
  1279. mxs_lradc_reg_clear(lradc,
  1280. lradc->buffer_vchans << LRADC_CTRL1_LRADC_IRQ_EN_OFFSET,
  1281. LRADC_CTRL1);
  1282. for (i = 0; i < LRADC_MAX_DELAY_CHANS; i++)
  1283. mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(i));
  1284. }
  1285. static const struct of_device_id mxs_lradc_dt_ids[] = {
  1286. { .compatible = "fsl,imx23-lradc", .data = (void *)IMX23_LRADC, },
  1287. { .compatible = "fsl,imx28-lradc", .data = (void *)IMX28_LRADC, },
  1288. { /* sentinel */ }
  1289. };
  1290. MODULE_DEVICE_TABLE(of, mxs_lradc_dt_ids);
  1291. static int mxs_lradc_probe_touchscreen(struct mxs_lradc *lradc,
  1292. struct device_node *lradc_node)
  1293. {
  1294. int ret;
  1295. u32 ts_wires = 0, adapt;
  1296. ret = of_property_read_u32(lradc_node, "fsl,lradc-touchscreen-wires",
  1297. &ts_wires);
  1298. if (ret)
  1299. return -ENODEV; /* touchscreen feature disabled */
  1300. switch (ts_wires) {
  1301. case 4:
  1302. lradc->use_touchscreen = MXS_LRADC_TOUCHSCREEN_4WIRE;
  1303. break;
  1304. case 5:
  1305. if (lradc->soc == IMX28_LRADC) {
  1306. lradc->use_touchscreen = MXS_LRADC_TOUCHSCREEN_5WIRE;
  1307. break;
  1308. }
  1309. /* fall through an error message for i.MX23 */
  1310. default:
  1311. dev_err(lradc->dev,
  1312. "Unsupported number of touchscreen wires (%d)\n",
  1313. ts_wires);
  1314. return -EINVAL;
  1315. }
  1316. if (of_property_read_u32(lradc_node, "fsl,ave-ctrl", &adapt)) {
  1317. lradc->over_sample_cnt = 4;
  1318. } else {
  1319. if (adapt < 1 || adapt > 32) {
  1320. dev_err(lradc->dev, "Invalid sample count (%u)\n",
  1321. adapt);
  1322. return -EINVAL;
  1323. }
  1324. lradc->over_sample_cnt = adapt;
  1325. }
  1326. if (of_property_read_u32(lradc_node, "fsl,ave-delay", &adapt)) {
  1327. lradc->over_sample_delay = 2;
  1328. } else {
  1329. if (adapt < 2 || adapt > LRADC_DELAY_DELAY_MASK + 1) {
  1330. dev_err(lradc->dev, "Invalid sample delay (%u)\n",
  1331. adapt);
  1332. return -EINVAL;
  1333. }
  1334. lradc->over_sample_delay = adapt;
  1335. }
  1336. if (of_property_read_u32(lradc_node, "fsl,settling", &adapt)) {
  1337. lradc->settling_delay = 10;
  1338. } else {
  1339. if (adapt < 1 || adapt > LRADC_DELAY_DELAY_MASK) {
  1340. dev_err(lradc->dev, "Invalid settling delay (%u)\n",
  1341. adapt);
  1342. return -EINVAL;
  1343. }
  1344. lradc->settling_delay = adapt;
  1345. }
  1346. return 0;
  1347. }
  1348. static int mxs_lradc_probe(struct platform_device *pdev)
  1349. {
  1350. const struct of_device_id *of_id =
  1351. of_match_device(mxs_lradc_dt_ids, &pdev->dev);
  1352. const struct mxs_lradc_of_config *of_cfg =
  1353. &mxs_lradc_of_config[(enum mxs_lradc_id)of_id->data];
  1354. struct device *dev = &pdev->dev;
  1355. struct device_node *node = dev->of_node;
  1356. struct mxs_lradc *lradc;
  1357. struct iio_dev *iio;
  1358. struct resource *iores;
  1359. int ret = 0, touch_ret;
  1360. int i, s;
  1361. u64 scale_uv;
  1362. /* Allocate the IIO device. */
  1363. iio = devm_iio_device_alloc(dev, sizeof(*lradc));
  1364. if (!iio) {
  1365. dev_err(dev, "Failed to allocate IIO device\n");
  1366. return -ENOMEM;
  1367. }
  1368. lradc = iio_priv(iio);
  1369. lradc->soc = (enum mxs_lradc_id)of_id->data;
  1370. /* Grab the memory area */
  1371. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1372. lradc->dev = &pdev->dev;
  1373. lradc->base = devm_ioremap_resource(dev, iores);
  1374. if (IS_ERR(lradc->base))
  1375. return PTR_ERR(lradc->base);
  1376. lradc->clk = devm_clk_get(&pdev->dev, NULL);
  1377. if (IS_ERR(lradc->clk)) {
  1378. dev_err(dev, "Failed to get the delay unit clock\n");
  1379. return PTR_ERR(lradc->clk);
  1380. }
  1381. ret = clk_prepare_enable(lradc->clk);
  1382. if (ret != 0) {
  1383. dev_err(dev, "Failed to enable the delay unit clock\n");
  1384. return ret;
  1385. }
  1386. touch_ret = mxs_lradc_probe_touchscreen(lradc, node);
  1387. if (touch_ret == 0)
  1388. lradc->buffer_vchans = BUFFER_VCHANS_LIMITED;
  1389. else
  1390. lradc->buffer_vchans = BUFFER_VCHANS_ALL;
  1391. /* Grab all IRQ sources */
  1392. for (i = 0; i < of_cfg->irq_count; i++) {
  1393. lradc->irq[i] = platform_get_irq(pdev, i);
  1394. if (lradc->irq[i] < 0) {
  1395. ret = lradc->irq[i];
  1396. goto err_clk;
  1397. }
  1398. ret = devm_request_irq(dev, lradc->irq[i],
  1399. mxs_lradc_handle_irq, 0,
  1400. of_cfg->irq_name[i], iio);
  1401. if (ret)
  1402. goto err_clk;
  1403. }
  1404. lradc->vref_mv = of_cfg->vref_mv;
  1405. platform_set_drvdata(pdev, iio);
  1406. init_completion(&lradc->completion);
  1407. mutex_init(&lradc->lock);
  1408. iio->name = pdev->name;
  1409. iio->dev.parent = &pdev->dev;
  1410. iio->info = &mxs_lradc_iio_info;
  1411. iio->modes = INDIO_DIRECT_MODE;
  1412. iio->masklength = LRADC_MAX_TOTAL_CHANS;
  1413. if (lradc->soc == IMX23_LRADC) {
  1414. iio->channels = mx23_lradc_chan_spec;
  1415. iio->num_channels = ARRAY_SIZE(mx23_lradc_chan_spec);
  1416. } else {
  1417. iio->channels = mx28_lradc_chan_spec;
  1418. iio->num_channels = ARRAY_SIZE(mx28_lradc_chan_spec);
  1419. }
  1420. ret = iio_triggered_buffer_setup(iio, &iio_pollfunc_store_time,
  1421. &mxs_lradc_trigger_handler,
  1422. &mxs_lradc_buffer_ops);
  1423. if (ret)
  1424. goto err_clk;
  1425. ret = mxs_lradc_trigger_init(iio);
  1426. if (ret)
  1427. goto err_trig;
  1428. /* Populate available ADC input ranges */
  1429. for (i = 0; i < LRADC_MAX_TOTAL_CHANS; i++) {
  1430. for (s = 0; s < ARRAY_SIZE(lradc->scale_avail[i]); s++) {
  1431. /*
  1432. * [s=0] = optional divider by two disabled (default)
  1433. * [s=1] = optional divider by two enabled
  1434. *
  1435. * The scale is calculated by doing:
  1436. * Vref >> (realbits - s)
  1437. * which multiplies by two on the second component
  1438. * of the array.
  1439. */
  1440. scale_uv = ((u64)lradc->vref_mv[i] * 100000000) >>
  1441. (LRADC_RESOLUTION - s);
  1442. lradc->scale_avail[i][s].nano =
  1443. do_div(scale_uv, 100000000) * 10;
  1444. lradc->scale_avail[i][s].integer = scale_uv;
  1445. }
  1446. }
  1447. ret = stmp_reset_block(lradc->base);
  1448. if (ret)
  1449. goto err_dev;
  1450. /* Configure the hardware. */
  1451. mxs_lradc_hw_init(lradc);
  1452. /* Register the touchscreen input device. */
  1453. if (touch_ret == 0) {
  1454. ret = mxs_lradc_ts_register(lradc);
  1455. if (ret)
  1456. goto err_ts_register;
  1457. }
  1458. /* Register IIO device. */
  1459. ret = iio_device_register(iio);
  1460. if (ret) {
  1461. dev_err(dev, "Failed to register IIO device\n");
  1462. return ret;
  1463. }
  1464. return 0;
  1465. err_ts_register:
  1466. mxs_lradc_hw_stop(lradc);
  1467. err_dev:
  1468. mxs_lradc_trigger_remove(iio);
  1469. err_trig:
  1470. iio_triggered_buffer_cleanup(iio);
  1471. err_clk:
  1472. clk_disable_unprepare(lradc->clk);
  1473. return ret;
  1474. }
  1475. static int mxs_lradc_remove(struct platform_device *pdev)
  1476. {
  1477. struct iio_dev *iio = platform_get_drvdata(pdev);
  1478. struct mxs_lradc *lradc = iio_priv(iio);
  1479. iio_device_unregister(iio);
  1480. mxs_lradc_hw_stop(lradc);
  1481. mxs_lradc_trigger_remove(iio);
  1482. iio_triggered_buffer_cleanup(iio);
  1483. clk_disable_unprepare(lradc->clk);
  1484. return 0;
  1485. }
  1486. static struct platform_driver mxs_lradc_driver = {
  1487. .driver = {
  1488. .name = DRIVER_NAME,
  1489. .of_match_table = mxs_lradc_dt_ids,
  1490. },
  1491. .probe = mxs_lradc_probe,
  1492. .remove = mxs_lradc_remove,
  1493. };
  1494. module_platform_driver(mxs_lradc_driver);
  1495. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  1496. MODULE_DESCRIPTION("Freescale MXS LRADC driver");
  1497. MODULE_LICENSE("GPL v2");
  1498. MODULE_ALIAS("platform:" DRIVER_NAME);