at91-sama5d2_adc.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. /*
  2. * Atmel ADC driver for SAMA5D2 devices and compatible.
  3. *
  4. * Copyright (C) 2015 Atmel,
  5. * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/bitops.h>
  17. #include <linux/clk.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/dmaengine.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/sched.h>
  26. #include <linux/wait.h>
  27. #include <linux/iio/iio.h>
  28. #include <linux/iio/sysfs.h>
  29. #include <linux/iio/buffer.h>
  30. #include <linux/iio/trigger.h>
  31. #include <linux/iio/trigger_consumer.h>
  32. #include <linux/iio/triggered_buffer.h>
  33. #include <linux/pinctrl/consumer.h>
  34. #include <linux/regulator/consumer.h>
  35. /* Control Register */
  36. #define AT91_SAMA5D2_CR 0x00
  37. /* Software Reset */
  38. #define AT91_SAMA5D2_CR_SWRST BIT(0)
  39. /* Start Conversion */
  40. #define AT91_SAMA5D2_CR_START BIT(1)
  41. /* Touchscreen Calibration */
  42. #define AT91_SAMA5D2_CR_TSCALIB BIT(2)
  43. /* Comparison Restart */
  44. #define AT91_SAMA5D2_CR_CMPRST BIT(4)
  45. /* Mode Register */
  46. #define AT91_SAMA5D2_MR 0x04
  47. /* Trigger Selection */
  48. #define AT91_SAMA5D2_MR_TRGSEL(v) ((v) << 1)
  49. /* ADTRG */
  50. #define AT91_SAMA5D2_MR_TRGSEL_TRIG0 0
  51. /* TIOA0 */
  52. #define AT91_SAMA5D2_MR_TRGSEL_TRIG1 1
  53. /* TIOA1 */
  54. #define AT91_SAMA5D2_MR_TRGSEL_TRIG2 2
  55. /* TIOA2 */
  56. #define AT91_SAMA5D2_MR_TRGSEL_TRIG3 3
  57. /* PWM event line 0 */
  58. #define AT91_SAMA5D2_MR_TRGSEL_TRIG4 4
  59. /* PWM event line 1 */
  60. #define AT91_SAMA5D2_MR_TRGSEL_TRIG5 5
  61. /* TIOA3 */
  62. #define AT91_SAMA5D2_MR_TRGSEL_TRIG6 6
  63. /* RTCOUT0 */
  64. #define AT91_SAMA5D2_MR_TRGSEL_TRIG7 7
  65. /* Sleep Mode */
  66. #define AT91_SAMA5D2_MR_SLEEP BIT(5)
  67. /* Fast Wake Up */
  68. #define AT91_SAMA5D2_MR_FWUP BIT(6)
  69. /* Prescaler Rate Selection */
  70. #define AT91_SAMA5D2_MR_PRESCAL(v) ((v) << AT91_SAMA5D2_MR_PRESCAL_OFFSET)
  71. #define AT91_SAMA5D2_MR_PRESCAL_OFFSET 8
  72. #define AT91_SAMA5D2_MR_PRESCAL_MAX 0xff
  73. #define AT91_SAMA5D2_MR_PRESCAL_MASK GENMASK(15, 8)
  74. /* Startup Time */
  75. #define AT91_SAMA5D2_MR_STARTUP(v) ((v) << 16)
  76. #define AT91_SAMA5D2_MR_STARTUP_MASK GENMASK(19, 16)
  77. /* Analog Change */
  78. #define AT91_SAMA5D2_MR_ANACH BIT(23)
  79. /* Tracking Time */
  80. #define AT91_SAMA5D2_MR_TRACKTIM(v) ((v) << 24)
  81. #define AT91_SAMA5D2_MR_TRACKTIM_MAX 0xff
  82. /* Transfer Time */
  83. #define AT91_SAMA5D2_MR_TRANSFER(v) ((v) << 28)
  84. #define AT91_SAMA5D2_MR_TRANSFER_MAX 0x3
  85. /* Use Sequence Enable */
  86. #define AT91_SAMA5D2_MR_USEQ BIT(31)
  87. /* Channel Sequence Register 1 */
  88. #define AT91_SAMA5D2_SEQR1 0x08
  89. /* Channel Sequence Register 2 */
  90. #define AT91_SAMA5D2_SEQR2 0x0c
  91. /* Channel Enable Register */
  92. #define AT91_SAMA5D2_CHER 0x10
  93. /* Channel Disable Register */
  94. #define AT91_SAMA5D2_CHDR 0x14
  95. /* Channel Status Register */
  96. #define AT91_SAMA5D2_CHSR 0x18
  97. /* Last Converted Data Register */
  98. #define AT91_SAMA5D2_LCDR 0x20
  99. /* Interrupt Enable Register */
  100. #define AT91_SAMA5D2_IER 0x24
  101. /* Interrupt Enable Register - TS X measurement ready */
  102. #define AT91_SAMA5D2_IER_XRDY BIT(20)
  103. /* Interrupt Enable Register - TS Y measurement ready */
  104. #define AT91_SAMA5D2_IER_YRDY BIT(21)
  105. /* Interrupt Enable Register - TS pressure measurement ready */
  106. #define AT91_SAMA5D2_IER_PRDY BIT(22)
  107. /* Interrupt Enable Register - general overrun error */
  108. #define AT91_SAMA5D2_IER_GOVRE BIT(25)
  109. /* Interrupt Enable Register - Pen detect */
  110. #define AT91_SAMA5D2_IER_PEN BIT(29)
  111. /* Interrupt Enable Register - No pen detect */
  112. #define AT91_SAMA5D2_IER_NOPEN BIT(30)
  113. /* Interrupt Disable Register */
  114. #define AT91_SAMA5D2_IDR 0x28
  115. /* Interrupt Mask Register */
  116. #define AT91_SAMA5D2_IMR 0x2c
  117. /* Interrupt Status Register */
  118. #define AT91_SAMA5D2_ISR 0x30
  119. /* Interrupt Status Register - Pen touching sense status */
  120. #define AT91_SAMA5D2_ISR_PENS BIT(31)
  121. /* Last Channel Trigger Mode Register */
  122. #define AT91_SAMA5D2_LCTMR 0x34
  123. /* Last Channel Compare Window Register */
  124. #define AT91_SAMA5D2_LCCWR 0x38
  125. /* Overrun Status Register */
  126. #define AT91_SAMA5D2_OVER 0x3c
  127. /* Extended Mode Register */
  128. #define AT91_SAMA5D2_EMR 0x40
  129. /* Extended Mode Register - Oversampling rate */
  130. #define AT91_SAMA5D2_EMR_OSR(V) ((V) << 16)
  131. #define AT91_SAMA5D2_EMR_OSR_MASK GENMASK(17, 16)
  132. #define AT91_SAMA5D2_EMR_OSR_1SAMPLES 0
  133. #define AT91_SAMA5D2_EMR_OSR_4SAMPLES 1
  134. #define AT91_SAMA5D2_EMR_OSR_16SAMPLES 2
  135. /* Extended Mode Register - Averaging on single trigger event */
  136. #define AT91_SAMA5D2_EMR_ASTE(V) ((V) << 20)
  137. /* Compare Window Register */
  138. #define AT91_SAMA5D2_CWR 0x44
  139. /* Channel Gain Register */
  140. #define AT91_SAMA5D2_CGR 0x48
  141. /* Channel Offset Register */
  142. #define AT91_SAMA5D2_COR 0x4c
  143. #define AT91_SAMA5D2_COR_DIFF_OFFSET 16
  144. /* Channel Data Register 0 */
  145. #define AT91_SAMA5D2_CDR0 0x50
  146. /* Analog Control Register */
  147. #define AT91_SAMA5D2_ACR 0x94
  148. /* Analog Control Register - Pen detect sensitivity mask */
  149. #define AT91_SAMA5D2_ACR_PENDETSENS_MASK GENMASK(1, 0)
  150. /* Touchscreen Mode Register */
  151. #define AT91_SAMA5D2_TSMR 0xb0
  152. /* Touchscreen Mode Register - No touch mode */
  153. #define AT91_SAMA5D2_TSMR_TSMODE_NONE 0
  154. /* Touchscreen Mode Register - 4 wire screen, no pressure measurement */
  155. #define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_NO_PRESS 1
  156. /* Touchscreen Mode Register - 4 wire screen, pressure measurement */
  157. #define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS 2
  158. /* Touchscreen Mode Register - 5 wire screen */
  159. #define AT91_SAMA5D2_TSMR_TSMODE_5WIRE 3
  160. /* Touchscreen Mode Register - Average samples mask */
  161. #define AT91_SAMA5D2_TSMR_TSAV_MASK GENMASK(5, 4)
  162. /* Touchscreen Mode Register - Average samples */
  163. #define AT91_SAMA5D2_TSMR_TSAV(x) ((x) << 4)
  164. /* Touchscreen Mode Register - Touch/trigger frequency ratio mask */
  165. #define AT91_SAMA5D2_TSMR_TSFREQ_MASK GENMASK(11, 8)
  166. /* Touchscreen Mode Register - Touch/trigger frequency ratio */
  167. #define AT91_SAMA5D2_TSMR_TSFREQ(x) ((x) << 8)
  168. /* Touchscreen Mode Register - Pen Debounce Time mask */
  169. #define AT91_SAMA5D2_TSMR_PENDBC_MASK GENMASK(31, 28)
  170. /* Touchscreen Mode Register - Pen Debounce Time */
  171. #define AT91_SAMA5D2_TSMR_PENDBC(x) ((x) << 28)
  172. /* Touchscreen Mode Register - No DMA for touch measurements */
  173. #define AT91_SAMA5D2_TSMR_NOTSDMA BIT(22)
  174. /* Touchscreen Mode Register - Disable pen detection */
  175. #define AT91_SAMA5D2_TSMR_PENDET_DIS (0 << 24)
  176. /* Touchscreen Mode Register - Enable pen detection */
  177. #define AT91_SAMA5D2_TSMR_PENDET_ENA BIT(24)
  178. /* Touchscreen X Position Register */
  179. #define AT91_SAMA5D2_XPOSR 0xb4
  180. /* Touchscreen Y Position Register */
  181. #define AT91_SAMA5D2_YPOSR 0xb8
  182. /* Touchscreen Pressure Register */
  183. #define AT91_SAMA5D2_PRESSR 0xbc
  184. /* Trigger Register */
  185. #define AT91_SAMA5D2_TRGR 0xc0
  186. /* Mask for TRGMOD field of TRGR register */
  187. #define AT91_SAMA5D2_TRGR_TRGMOD_MASK GENMASK(2, 0)
  188. /* No trigger, only software trigger can start conversions */
  189. #define AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER 0
  190. /* Trigger Mode external trigger rising edge */
  191. #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE 1
  192. /* Trigger Mode external trigger falling edge */
  193. #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL 2
  194. /* Trigger Mode external trigger any edge */
  195. #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY 3
  196. /* Trigger Mode internal periodic */
  197. #define AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC 5
  198. /* Trigger Mode - trigger period mask */
  199. #define AT91_SAMA5D2_TRGR_TRGPER_MASK GENMASK(31, 16)
  200. /* Trigger Mode - trigger period */
  201. #define AT91_SAMA5D2_TRGR_TRGPER(x) ((x) << 16)
  202. /* Correction Select Register */
  203. #define AT91_SAMA5D2_COSR 0xd0
  204. /* Correction Value Register */
  205. #define AT91_SAMA5D2_CVR 0xd4
  206. /* Channel Error Correction Register */
  207. #define AT91_SAMA5D2_CECR 0xd8
  208. /* Write Protection Mode Register */
  209. #define AT91_SAMA5D2_WPMR 0xe4
  210. /* Write Protection Status Register */
  211. #define AT91_SAMA5D2_WPSR 0xe8
  212. /* Version Register */
  213. #define AT91_SAMA5D2_VERSION 0xfc
  214. #define AT91_SAMA5D2_HW_TRIG_CNT 3
  215. #define AT91_SAMA5D2_SINGLE_CHAN_CNT 12
  216. #define AT91_SAMA5D2_DIFF_CHAN_CNT 6
  217. #define AT91_SAMA5D2_TIMESTAMP_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \
  218. AT91_SAMA5D2_DIFF_CHAN_CNT + 1)
  219. #define AT91_SAMA5D2_TOUCH_X_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \
  220. AT91_SAMA5D2_DIFF_CHAN_CNT * 2)
  221. #define AT91_SAMA5D2_TOUCH_Y_CHAN_IDX (AT91_SAMA5D2_TOUCH_X_CHAN_IDX + 1)
  222. #define AT91_SAMA5D2_TOUCH_P_CHAN_IDX (AT91_SAMA5D2_TOUCH_Y_CHAN_IDX + 1)
  223. #define AT91_SAMA5D2_MAX_CHAN_IDX AT91_SAMA5D2_TOUCH_P_CHAN_IDX
  224. #define AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */
  225. #define AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US 200
  226. #define AT91_SAMA5D2_XYZ_MASK GENMASK(11, 0)
  227. #define AT91_SAMA5D2_MAX_POS_BITS 12
  228. /*
  229. * Maximum number of bytes to hold conversion from all channels
  230. * without the timestamp.
  231. */
  232. #define AT91_BUFFER_MAX_CONVERSION_BYTES ((AT91_SAMA5D2_SINGLE_CHAN_CNT + \
  233. AT91_SAMA5D2_DIFF_CHAN_CNT) * 2)
  234. /* This total must also include the timestamp */
  235. #define AT91_BUFFER_MAX_BYTES (AT91_BUFFER_MAX_CONVERSION_BYTES + 8)
  236. #define AT91_BUFFER_MAX_HWORDS (AT91_BUFFER_MAX_BYTES / 2)
  237. #define AT91_HWFIFO_MAX_SIZE_STR "128"
  238. #define AT91_HWFIFO_MAX_SIZE 128
  239. /* Possible values for oversampling ratio */
  240. #define AT91_OSR_1SAMPLES 1
  241. #define AT91_OSR_4SAMPLES 4
  242. #define AT91_OSR_16SAMPLES 16
  243. #define AT91_SAMA5D2_CHAN_SINGLE(num, addr) \
  244. { \
  245. .type = IIO_VOLTAGE, \
  246. .channel = num, \
  247. .address = addr, \
  248. .scan_index = num, \
  249. .scan_type = { \
  250. .sign = 'u', \
  251. .realbits = 14, \
  252. .storagebits = 16, \
  253. }, \
  254. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  255. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  256. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
  257. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  258. .datasheet_name = "CH"#num, \
  259. .indexed = 1, \
  260. }
  261. #define AT91_SAMA5D2_CHAN_DIFF(num, num2, addr) \
  262. { \
  263. .type = IIO_VOLTAGE, \
  264. .differential = 1, \
  265. .channel = num, \
  266. .channel2 = num2, \
  267. .address = addr, \
  268. .scan_index = num + AT91_SAMA5D2_SINGLE_CHAN_CNT, \
  269. .scan_type = { \
  270. .sign = 's', \
  271. .realbits = 14, \
  272. .storagebits = 16, \
  273. }, \
  274. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  275. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  276. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
  277. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  278. .datasheet_name = "CH"#num"-CH"#num2, \
  279. .indexed = 1, \
  280. }
  281. #define AT91_SAMA5D2_CHAN_TOUCH(num, name, mod) \
  282. { \
  283. .type = IIO_POSITIONRELATIVE, \
  284. .modified = 1, \
  285. .channel = num, \
  286. .channel2 = mod, \
  287. .scan_index = num, \
  288. .scan_type = { \
  289. .sign = 'u', \
  290. .realbits = 12, \
  291. .storagebits = 16, \
  292. }, \
  293. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  294. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
  295. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  296. .datasheet_name = name, \
  297. }
  298. #define AT91_SAMA5D2_CHAN_PRESSURE(num, name) \
  299. { \
  300. .type = IIO_PRESSURE, \
  301. .channel = num, \
  302. .scan_index = num, \
  303. .scan_type = { \
  304. .sign = 'u', \
  305. .realbits = 12, \
  306. .storagebits = 16, \
  307. }, \
  308. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  309. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
  310. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  311. .datasheet_name = name, \
  312. }
  313. #define at91_adc_readl(st, reg) readl_relaxed(st->base + reg)
  314. #define at91_adc_writel(st, reg, val) writel_relaxed(val, st->base + reg)
  315. struct at91_adc_soc_info {
  316. unsigned startup_time;
  317. unsigned min_sample_rate;
  318. unsigned max_sample_rate;
  319. };
  320. struct at91_adc_trigger {
  321. char *name;
  322. unsigned int trgmod_value;
  323. unsigned int edge_type;
  324. bool hw_trig;
  325. };
  326. /**
  327. * at91_adc_dma - at91-sama5d2 dma information struct
  328. * @dma_chan: the dma channel acquired
  329. * @rx_buf: dma coherent allocated area
  330. * @rx_dma_buf: dma handler for the buffer
  331. * @phys_addr: physical address of the ADC base register
  332. * @buf_idx: index inside the dma buffer where reading was last done
  333. * @rx_buf_sz: size of buffer used by DMA operation
  334. * @watermark: number of conversions to copy before DMA triggers irq
  335. * @dma_ts: hold the start timestamp of dma operation
  336. */
  337. struct at91_adc_dma {
  338. struct dma_chan *dma_chan;
  339. u8 *rx_buf;
  340. dma_addr_t rx_dma_buf;
  341. phys_addr_t phys_addr;
  342. int buf_idx;
  343. int rx_buf_sz;
  344. int watermark;
  345. s64 dma_ts;
  346. };
  347. /**
  348. * at91_adc_touch - at91-sama5d2 touchscreen information struct
  349. * @sample_period_val: the value for periodic trigger interval
  350. * @touching: is the pen touching the screen or not
  351. * @x_pos: temporary placeholder for pressure computation
  352. * @channels_bitmask: bitmask with the touchscreen channels enabled
  353. * @workq: workqueue for buffer data pushing
  354. */
  355. struct at91_adc_touch {
  356. u16 sample_period_val;
  357. bool touching;
  358. u16 x_pos;
  359. unsigned long channels_bitmask;
  360. struct work_struct workq;
  361. };
  362. struct at91_adc_state {
  363. void __iomem *base;
  364. int irq;
  365. struct clk *per_clk;
  366. struct regulator *reg;
  367. struct regulator *vref;
  368. int vref_uv;
  369. unsigned int current_sample_rate;
  370. struct iio_trigger *trig;
  371. const struct at91_adc_trigger *selected_trig;
  372. const struct iio_chan_spec *chan;
  373. bool conversion_done;
  374. u32 conversion_value;
  375. unsigned int oversampling_ratio;
  376. struct at91_adc_soc_info soc_info;
  377. wait_queue_head_t wq_data_available;
  378. struct at91_adc_dma dma_st;
  379. struct at91_adc_touch touch_st;
  380. u16 buffer[AT91_BUFFER_MAX_HWORDS];
  381. /*
  382. * lock to prevent concurrent 'single conversion' requests through
  383. * sysfs.
  384. */
  385. struct mutex lock;
  386. };
  387. static const struct at91_adc_trigger at91_adc_trigger_list[] = {
  388. {
  389. .name = "external_rising",
  390. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE,
  391. .edge_type = IRQ_TYPE_EDGE_RISING,
  392. .hw_trig = true,
  393. },
  394. {
  395. .name = "external_falling",
  396. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL,
  397. .edge_type = IRQ_TYPE_EDGE_FALLING,
  398. .hw_trig = true,
  399. },
  400. {
  401. .name = "external_any",
  402. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY,
  403. .edge_type = IRQ_TYPE_EDGE_BOTH,
  404. .hw_trig = true,
  405. },
  406. {
  407. .name = "software",
  408. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER,
  409. .edge_type = IRQ_TYPE_NONE,
  410. .hw_trig = false,
  411. },
  412. };
  413. static const struct iio_chan_spec at91_adc_channels[] = {
  414. AT91_SAMA5D2_CHAN_SINGLE(0, 0x50),
  415. AT91_SAMA5D2_CHAN_SINGLE(1, 0x54),
  416. AT91_SAMA5D2_CHAN_SINGLE(2, 0x58),
  417. AT91_SAMA5D2_CHAN_SINGLE(3, 0x5c),
  418. AT91_SAMA5D2_CHAN_SINGLE(4, 0x60),
  419. AT91_SAMA5D2_CHAN_SINGLE(5, 0x64),
  420. AT91_SAMA5D2_CHAN_SINGLE(6, 0x68),
  421. AT91_SAMA5D2_CHAN_SINGLE(7, 0x6c),
  422. AT91_SAMA5D2_CHAN_SINGLE(8, 0x70),
  423. AT91_SAMA5D2_CHAN_SINGLE(9, 0x74),
  424. AT91_SAMA5D2_CHAN_SINGLE(10, 0x78),
  425. AT91_SAMA5D2_CHAN_SINGLE(11, 0x7c),
  426. AT91_SAMA5D2_CHAN_DIFF(0, 1, 0x50),
  427. AT91_SAMA5D2_CHAN_DIFF(2, 3, 0x58),
  428. AT91_SAMA5D2_CHAN_DIFF(4, 5, 0x60),
  429. AT91_SAMA5D2_CHAN_DIFF(6, 7, 0x68),
  430. AT91_SAMA5D2_CHAN_DIFF(8, 9, 0x70),
  431. AT91_SAMA5D2_CHAN_DIFF(10, 11, 0x78),
  432. IIO_CHAN_SOFT_TIMESTAMP(AT91_SAMA5D2_TIMESTAMP_CHAN_IDX),
  433. AT91_SAMA5D2_CHAN_TOUCH(AT91_SAMA5D2_TOUCH_X_CHAN_IDX, "x", IIO_MOD_X),
  434. AT91_SAMA5D2_CHAN_TOUCH(AT91_SAMA5D2_TOUCH_Y_CHAN_IDX, "y", IIO_MOD_Y),
  435. AT91_SAMA5D2_CHAN_PRESSURE(AT91_SAMA5D2_TOUCH_P_CHAN_IDX, "pressure"),
  436. };
  437. static int at91_adc_chan_xlate(struct iio_dev *indio_dev, int chan)
  438. {
  439. int i;
  440. for (i = 0; i < indio_dev->num_channels; i++) {
  441. if (indio_dev->channels[i].scan_index == chan)
  442. return i;
  443. }
  444. return -EINVAL;
  445. }
  446. static inline struct iio_chan_spec const *
  447. at91_adc_chan_get(struct iio_dev *indio_dev, int chan)
  448. {
  449. int index = at91_adc_chan_xlate(indio_dev, chan);
  450. if (index < 0)
  451. return NULL;
  452. return indio_dev->channels + index;
  453. }
  454. static inline int at91_adc_of_xlate(struct iio_dev *indio_dev,
  455. const struct of_phandle_args *iiospec)
  456. {
  457. return at91_adc_chan_xlate(indio_dev, iiospec->args[0]);
  458. }
  459. static void at91_adc_config_emr(struct at91_adc_state *st)
  460. {
  461. /* configure the extended mode register */
  462. unsigned int emr = at91_adc_readl(st, AT91_SAMA5D2_EMR);
  463. /* select oversampling per single trigger event */
  464. emr |= AT91_SAMA5D2_EMR_ASTE(1);
  465. /* delete leftover content if it's the case */
  466. emr &= ~AT91_SAMA5D2_EMR_OSR_MASK;
  467. /* select oversampling ratio from configuration */
  468. switch (st->oversampling_ratio) {
  469. case AT91_OSR_1SAMPLES:
  470. emr |= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_1SAMPLES) &
  471. AT91_SAMA5D2_EMR_OSR_MASK;
  472. break;
  473. case AT91_OSR_4SAMPLES:
  474. emr |= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_4SAMPLES) &
  475. AT91_SAMA5D2_EMR_OSR_MASK;
  476. break;
  477. case AT91_OSR_16SAMPLES:
  478. emr |= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_16SAMPLES) &
  479. AT91_SAMA5D2_EMR_OSR_MASK;
  480. break;
  481. }
  482. at91_adc_writel(st, AT91_SAMA5D2_EMR, emr);
  483. }
  484. static int at91_adc_adjust_val_osr(struct at91_adc_state *st, int *val)
  485. {
  486. if (st->oversampling_ratio == AT91_OSR_1SAMPLES) {
  487. /*
  488. * in this case we only have 12 bits of real data, but channel
  489. * is registered as 14 bits, so shift left two bits
  490. */
  491. *val <<= 2;
  492. } else if (st->oversampling_ratio == AT91_OSR_4SAMPLES) {
  493. /*
  494. * in this case we have 13 bits of real data, but channel
  495. * is registered as 14 bits, so left shift one bit
  496. */
  497. *val <<= 1;
  498. }
  499. return IIO_VAL_INT;
  500. }
  501. static void at91_adc_adjust_val_osr_array(struct at91_adc_state *st, void *buf,
  502. int len)
  503. {
  504. int i = 0, val;
  505. u16 *buf_u16 = (u16 *) buf;
  506. /*
  507. * We are converting each two bytes (each sample).
  508. * First convert the byte based array to u16, and convert each sample
  509. * separately.
  510. * Each value is two bytes in an array of chars, so to not shift
  511. * more than we need, save the value separately.
  512. * len is in bytes, so divide by two to get number of samples.
  513. */
  514. while (i < len / 2) {
  515. val = buf_u16[i];
  516. at91_adc_adjust_val_osr(st, &val);
  517. buf_u16[i] = val;
  518. i++;
  519. }
  520. }
  521. static int at91_adc_configure_touch(struct at91_adc_state *st, bool state)
  522. {
  523. u32 clk_khz = st->current_sample_rate / 1000;
  524. int i = 0;
  525. u16 pendbc;
  526. u32 tsmr, acr;
  527. if (!state) {
  528. /* disabling touch IRQs and setting mode to no touch enabled */
  529. at91_adc_writel(st, AT91_SAMA5D2_IDR,
  530. AT91_SAMA5D2_IER_PEN | AT91_SAMA5D2_IER_NOPEN);
  531. at91_adc_writel(st, AT91_SAMA5D2_TSMR, 0);
  532. return 0;
  533. }
  534. /*
  535. * debounce time is in microseconds, we need it in milliseconds to
  536. * multiply with kilohertz, so, divide by 1000, but after the multiply.
  537. * round up to make sure pendbc is at least 1
  538. */
  539. pendbc = round_up(AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US *
  540. clk_khz / 1000, 1);
  541. /* get the required exponent */
  542. while (pendbc >> i++)
  543. ;
  544. pendbc = i;
  545. tsmr = AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS;
  546. tsmr |= AT91_SAMA5D2_TSMR_TSAV(2) & AT91_SAMA5D2_TSMR_TSAV_MASK;
  547. tsmr |= AT91_SAMA5D2_TSMR_PENDBC(pendbc) &
  548. AT91_SAMA5D2_TSMR_PENDBC_MASK;
  549. tsmr |= AT91_SAMA5D2_TSMR_NOTSDMA;
  550. tsmr |= AT91_SAMA5D2_TSMR_PENDET_ENA;
  551. tsmr |= AT91_SAMA5D2_TSMR_TSFREQ(2) & AT91_SAMA5D2_TSMR_TSFREQ_MASK;
  552. at91_adc_writel(st, AT91_SAMA5D2_TSMR, tsmr);
  553. acr = at91_adc_readl(st, AT91_SAMA5D2_ACR);
  554. acr &= ~AT91_SAMA5D2_ACR_PENDETSENS_MASK;
  555. acr |= 0x02 & AT91_SAMA5D2_ACR_PENDETSENS_MASK;
  556. at91_adc_writel(st, AT91_SAMA5D2_ACR, acr);
  557. /* Sample Period Time = (TRGPER + 1) / ADCClock */
  558. st->touch_st.sample_period_val =
  559. round_up((AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US *
  560. clk_khz / 1000) - 1, 1);
  561. /* enable pen detect IRQ */
  562. at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_PEN);
  563. return 0;
  564. }
  565. static u16 at91_adc_touch_pos(struct at91_adc_state *st, int reg)
  566. {
  567. u32 val;
  568. u32 scale, result, pos;
  569. /*
  570. * to obtain the actual position we must divide by scale
  571. * and multiply with max, where
  572. * max = 2^AT91_SAMA5D2_MAX_POS_BITS - 1
  573. */
  574. /* first half of register is the x or y, second half is the scale */
  575. val = at91_adc_readl(st, reg);
  576. if (!val)
  577. dev_dbg(&iio_priv_to_dev(st)->dev, "pos is 0\n");
  578. pos = val & AT91_SAMA5D2_XYZ_MASK;
  579. result = (pos << AT91_SAMA5D2_MAX_POS_BITS) - pos;
  580. scale = (val >> 16) & AT91_SAMA5D2_XYZ_MASK;
  581. if (scale == 0) {
  582. dev_err(&iio_priv_to_dev(st)->dev, "scale is 0\n");
  583. return 0;
  584. }
  585. result /= scale;
  586. return result;
  587. }
  588. static u16 at91_adc_touch_x_pos(struct at91_adc_state *st)
  589. {
  590. st->touch_st.x_pos = at91_adc_touch_pos(st, AT91_SAMA5D2_XPOSR);
  591. return st->touch_st.x_pos;
  592. }
  593. static u16 at91_adc_touch_y_pos(struct at91_adc_state *st)
  594. {
  595. return at91_adc_touch_pos(st, AT91_SAMA5D2_YPOSR);
  596. }
  597. static u16 at91_adc_touch_pressure(struct at91_adc_state *st)
  598. {
  599. u32 val;
  600. u32 z1, z2;
  601. u32 pres;
  602. u32 rxp = 1;
  603. u32 factor = 1000;
  604. /* calculate the pressure */
  605. val = at91_adc_readl(st, AT91_SAMA5D2_PRESSR);
  606. z1 = val & AT91_SAMA5D2_XYZ_MASK;
  607. z2 = (val >> 16) & AT91_SAMA5D2_XYZ_MASK;
  608. if (z1 != 0)
  609. pres = rxp * (st->touch_st.x_pos * factor / 1024) *
  610. (z2 * factor / z1 - factor) /
  611. factor;
  612. else
  613. pres = 0xFFFF; /* no pen contact */
  614. /*
  615. * The pressure from device grows down, minimum is 0xFFFF, maximum 0x0.
  616. * We compute it this way, but let's return it in the expected way,
  617. * growing from 0 to 0xFFFF.
  618. */
  619. return 0xFFFF - pres;
  620. }
  621. static int at91_adc_read_position(struct at91_adc_state *st, int chan, u16 *val)
  622. {
  623. *val = 0;
  624. if (!st->touch_st.touching)
  625. return -ENODATA;
  626. if (chan == AT91_SAMA5D2_TOUCH_X_CHAN_IDX)
  627. *val = at91_adc_touch_x_pos(st);
  628. else if (chan == AT91_SAMA5D2_TOUCH_Y_CHAN_IDX)
  629. *val = at91_adc_touch_y_pos(st);
  630. else
  631. return -ENODATA;
  632. return IIO_VAL_INT;
  633. }
  634. static int at91_adc_read_pressure(struct at91_adc_state *st, int chan, u16 *val)
  635. {
  636. *val = 0;
  637. if (!st->touch_st.touching)
  638. return -ENODATA;
  639. if (chan == AT91_SAMA5D2_TOUCH_P_CHAN_IDX)
  640. *val = at91_adc_touch_pressure(st);
  641. else
  642. return -ENODATA;
  643. return IIO_VAL_INT;
  644. }
  645. static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
  646. {
  647. struct iio_dev *indio = iio_trigger_get_drvdata(trig);
  648. struct at91_adc_state *st = iio_priv(indio);
  649. u32 status = at91_adc_readl(st, AT91_SAMA5D2_TRGR);
  650. u8 bit;
  651. /* clear TRGMOD */
  652. status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK;
  653. if (state)
  654. status |= st->selected_trig->trgmod_value;
  655. /* set/unset hw trigger */
  656. at91_adc_writel(st, AT91_SAMA5D2_TRGR, status);
  657. for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) {
  658. struct iio_chan_spec const *chan = at91_adc_chan_get(indio, bit);
  659. u32 cor;
  660. if (!chan)
  661. continue;
  662. /* these channel types cannot be handled by this trigger */
  663. if (chan->type == IIO_POSITIONRELATIVE ||
  664. chan->type == IIO_PRESSURE)
  665. continue;
  666. if (state) {
  667. cor = at91_adc_readl(st, AT91_SAMA5D2_COR);
  668. if (chan->differential)
  669. cor |= (BIT(chan->channel) |
  670. BIT(chan->channel2)) <<
  671. AT91_SAMA5D2_COR_DIFF_OFFSET;
  672. else
  673. cor &= ~(BIT(chan->channel) <<
  674. AT91_SAMA5D2_COR_DIFF_OFFSET);
  675. at91_adc_writel(st, AT91_SAMA5D2_COR, cor);
  676. }
  677. if (state) {
  678. at91_adc_writel(st, AT91_SAMA5D2_CHER,
  679. BIT(chan->channel));
  680. /* enable irq only if not using DMA */
  681. if (!st->dma_st.dma_chan) {
  682. at91_adc_writel(st, AT91_SAMA5D2_IER,
  683. BIT(chan->channel));
  684. }
  685. } else {
  686. /* disable irq only if not using DMA */
  687. if (!st->dma_st.dma_chan) {
  688. at91_adc_writel(st, AT91_SAMA5D2_IDR,
  689. BIT(chan->channel));
  690. }
  691. at91_adc_writel(st, AT91_SAMA5D2_CHDR,
  692. BIT(chan->channel));
  693. }
  694. }
  695. return 0;
  696. }
  697. static int at91_adc_reenable_trigger(struct iio_trigger *trig)
  698. {
  699. struct iio_dev *indio = iio_trigger_get_drvdata(trig);
  700. struct at91_adc_state *st = iio_priv(indio);
  701. /* if we are using DMA, we must not reenable irq after each trigger */
  702. if (st->dma_st.dma_chan)
  703. return 0;
  704. enable_irq(st->irq);
  705. /* Needed to ACK the DRDY interruption */
  706. at91_adc_readl(st, AT91_SAMA5D2_LCDR);
  707. return 0;
  708. }
  709. static const struct iio_trigger_ops at91_adc_trigger_ops = {
  710. .set_trigger_state = &at91_adc_configure_trigger,
  711. .try_reenable = &at91_adc_reenable_trigger,
  712. .validate_device = iio_trigger_validate_own_device,
  713. };
  714. static int at91_adc_dma_size_done(struct at91_adc_state *st)
  715. {
  716. struct dma_tx_state state;
  717. enum dma_status status;
  718. int i, size;
  719. status = dmaengine_tx_status(st->dma_st.dma_chan,
  720. st->dma_st.dma_chan->cookie,
  721. &state);
  722. if (status != DMA_IN_PROGRESS)
  723. return 0;
  724. /* Transferred length is size in bytes from end of buffer */
  725. i = st->dma_st.rx_buf_sz - state.residue;
  726. /* Return available bytes */
  727. if (i >= st->dma_st.buf_idx)
  728. size = i - st->dma_st.buf_idx;
  729. else
  730. size = st->dma_st.rx_buf_sz + i - st->dma_st.buf_idx;
  731. return size;
  732. }
  733. static void at91_dma_buffer_done(void *data)
  734. {
  735. struct iio_dev *indio_dev = data;
  736. iio_trigger_poll_chained(indio_dev->trig);
  737. }
  738. static int at91_adc_dma_start(struct iio_dev *indio_dev)
  739. {
  740. struct at91_adc_state *st = iio_priv(indio_dev);
  741. struct dma_async_tx_descriptor *desc;
  742. dma_cookie_t cookie;
  743. int ret;
  744. u8 bit;
  745. if (!st->dma_st.dma_chan)
  746. return 0;
  747. /* we start a new DMA, so set buffer index to start */
  748. st->dma_st.buf_idx = 0;
  749. /*
  750. * compute buffer size w.r.t. watermark and enabled channels.
  751. * scan_bytes is aligned so we need an exact size for DMA
  752. */
  753. st->dma_st.rx_buf_sz = 0;
  754. for_each_set_bit(bit, indio_dev->active_scan_mask,
  755. indio_dev->num_channels) {
  756. struct iio_chan_spec const *chan =
  757. at91_adc_chan_get(indio_dev, bit);
  758. if (!chan)
  759. continue;
  760. st->dma_st.rx_buf_sz += chan->scan_type.storagebits / 8;
  761. }
  762. st->dma_st.rx_buf_sz *= st->dma_st.watermark;
  763. /* Prepare a DMA cyclic transaction */
  764. desc = dmaengine_prep_dma_cyclic(st->dma_st.dma_chan,
  765. st->dma_st.rx_dma_buf,
  766. st->dma_st.rx_buf_sz,
  767. st->dma_st.rx_buf_sz / 2,
  768. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
  769. if (!desc) {
  770. dev_err(&indio_dev->dev, "cannot prepare DMA cyclic\n");
  771. return -EBUSY;
  772. }
  773. desc->callback = at91_dma_buffer_done;
  774. desc->callback_param = indio_dev;
  775. cookie = dmaengine_submit(desc);
  776. ret = dma_submit_error(cookie);
  777. if (ret) {
  778. dev_err(&indio_dev->dev, "cannot submit DMA cyclic\n");
  779. dmaengine_terminate_async(st->dma_st.dma_chan);
  780. return ret;
  781. }
  782. /* enable general overrun error signaling */
  783. at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_GOVRE);
  784. /* Issue pending DMA requests */
  785. dma_async_issue_pending(st->dma_st.dma_chan);
  786. /* consider current time as DMA start time for timestamps */
  787. st->dma_st.dma_ts = iio_get_time_ns(indio_dev);
  788. dev_dbg(&indio_dev->dev, "DMA cyclic started\n");
  789. return 0;
  790. }
  791. static int at91_adc_buffer_postenable(struct iio_dev *indio_dev)
  792. {
  793. int ret;
  794. struct at91_adc_state *st = iio_priv(indio_dev);
  795. /* check if we are enabling triggered buffer or the touchscreen */
  796. if (bitmap_subset(indio_dev->active_scan_mask,
  797. &st->touch_st.channels_bitmask,
  798. AT91_SAMA5D2_MAX_CHAN_IDX + 1)) {
  799. /* touchscreen enabling */
  800. return at91_adc_configure_touch(st, true);
  801. }
  802. /* if we are not in triggered mode, we cannot enable the buffer. */
  803. if (!(indio_dev->currentmode & INDIO_ALL_TRIGGERED_MODES))
  804. return -EINVAL;
  805. /* we continue with the triggered buffer */
  806. ret = at91_adc_dma_start(indio_dev);
  807. if (ret) {
  808. dev_err(&indio_dev->dev, "buffer postenable failed\n");
  809. return ret;
  810. }
  811. return iio_triggered_buffer_postenable(indio_dev);
  812. }
  813. static int at91_adc_buffer_predisable(struct iio_dev *indio_dev)
  814. {
  815. struct at91_adc_state *st = iio_priv(indio_dev);
  816. int ret;
  817. u8 bit;
  818. /* check if we are disabling triggered buffer or the touchscreen */
  819. if (bitmap_subset(indio_dev->active_scan_mask,
  820. &st->touch_st.channels_bitmask,
  821. AT91_SAMA5D2_MAX_CHAN_IDX + 1)) {
  822. /* touchscreen disable */
  823. return at91_adc_configure_touch(st, false);
  824. }
  825. /* if we are not in triggered mode, nothing to do here */
  826. if (!(indio_dev->currentmode & INDIO_ALL_TRIGGERED_MODES))
  827. return -EINVAL;
  828. /* continue with the triggered buffer */
  829. ret = iio_triggered_buffer_predisable(indio_dev);
  830. if (ret < 0)
  831. dev_err(&indio_dev->dev, "buffer predisable failed\n");
  832. if (!st->dma_st.dma_chan)
  833. return ret;
  834. /* if we are using DMA we must clear registers and end DMA */
  835. dmaengine_terminate_sync(st->dma_st.dma_chan);
  836. /*
  837. * For each enabled channel we must read the last converted value
  838. * to clear EOC status and not get a possible interrupt later.
  839. * This value is being read by DMA from LCDR anyway
  840. */
  841. for_each_set_bit(bit, indio_dev->active_scan_mask,
  842. indio_dev->num_channels) {
  843. struct iio_chan_spec const *chan =
  844. at91_adc_chan_get(indio_dev, bit);
  845. if (!chan)
  846. continue;
  847. /* these channel types are virtual, no need to do anything */
  848. if (chan->type == IIO_POSITIONRELATIVE ||
  849. chan->type == IIO_PRESSURE)
  850. continue;
  851. if (st->dma_st.dma_chan)
  852. at91_adc_readl(st, chan->address);
  853. }
  854. /* read overflow register to clear possible overflow status */
  855. at91_adc_readl(st, AT91_SAMA5D2_OVER);
  856. return ret;
  857. }
  858. static const struct iio_buffer_setup_ops at91_buffer_setup_ops = {
  859. .postenable = &at91_adc_buffer_postenable,
  860. .predisable = &at91_adc_buffer_predisable,
  861. };
  862. static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *indio,
  863. char *trigger_name)
  864. {
  865. struct iio_trigger *trig;
  866. int ret;
  867. trig = devm_iio_trigger_alloc(&indio->dev, "%s-dev%d-%s", indio->name,
  868. indio->id, trigger_name);
  869. if (!trig)
  870. return NULL;
  871. trig->dev.parent = indio->dev.parent;
  872. iio_trigger_set_drvdata(trig, indio);
  873. trig->ops = &at91_adc_trigger_ops;
  874. ret = devm_iio_trigger_register(&indio->dev, trig);
  875. if (ret)
  876. return ERR_PTR(ret);
  877. return trig;
  878. }
  879. static int at91_adc_trigger_init(struct iio_dev *indio)
  880. {
  881. struct at91_adc_state *st = iio_priv(indio);
  882. st->trig = at91_adc_allocate_trigger(indio, st->selected_trig->name);
  883. if (IS_ERR(st->trig)) {
  884. dev_err(&indio->dev,
  885. "could not allocate trigger\n");
  886. return PTR_ERR(st->trig);
  887. }
  888. return 0;
  889. }
  890. static void at91_adc_trigger_handler_nodma(struct iio_dev *indio_dev,
  891. struct iio_poll_func *pf)
  892. {
  893. struct at91_adc_state *st = iio_priv(indio_dev);
  894. int i = 0;
  895. int val;
  896. u8 bit;
  897. for_each_set_bit(bit, indio_dev->active_scan_mask,
  898. indio_dev->num_channels) {
  899. struct iio_chan_spec const *chan =
  900. at91_adc_chan_get(indio_dev, bit);
  901. if (!chan)
  902. continue;
  903. /*
  904. * Our external trigger only supports the voltage channels.
  905. * In case someone requested a different type of channel
  906. * just put zeroes to buffer.
  907. * This should not happen because we check the scan mode
  908. * and scan mask when we enable the buffer, and we don't allow
  909. * the buffer to start with a mixed mask (voltage and something
  910. * else).
  911. * Thus, emit a warning.
  912. */
  913. if (chan->type == IIO_VOLTAGE) {
  914. val = at91_adc_readl(st, chan->address);
  915. at91_adc_adjust_val_osr(st, &val);
  916. st->buffer[i] = val;
  917. } else {
  918. st->buffer[i] = 0;
  919. WARN(true, "This trigger cannot handle this type of channel");
  920. }
  921. i++;
  922. }
  923. iio_push_to_buffers_with_timestamp(indio_dev, st->buffer,
  924. pf->timestamp);
  925. }
  926. static void at91_adc_trigger_handler_dma(struct iio_dev *indio_dev)
  927. {
  928. struct at91_adc_state *st = iio_priv(indio_dev);
  929. int transferred_len = at91_adc_dma_size_done(st);
  930. s64 ns = iio_get_time_ns(indio_dev);
  931. s64 interval;
  932. int sample_index = 0, sample_count, sample_size;
  933. u32 status = at91_adc_readl(st, AT91_SAMA5D2_ISR);
  934. /* if we reached this point, we cannot sample faster */
  935. if (status & AT91_SAMA5D2_IER_GOVRE)
  936. pr_info_ratelimited("%s: conversion overrun detected\n",
  937. indio_dev->name);
  938. sample_size = div_s64(st->dma_st.rx_buf_sz, st->dma_st.watermark);
  939. sample_count = div_s64(transferred_len, sample_size);
  940. /*
  941. * interval between samples is total time since last transfer handling
  942. * divided by the number of samples (total size divided by sample size)
  943. */
  944. interval = div_s64((ns - st->dma_st.dma_ts), sample_count);
  945. while (transferred_len >= sample_size) {
  946. /*
  947. * for all the values in the current sample,
  948. * adjust the values inside the buffer for oversampling
  949. */
  950. at91_adc_adjust_val_osr_array(st,
  951. &st->dma_st.rx_buf[st->dma_st.buf_idx],
  952. sample_size);
  953. iio_push_to_buffers_with_timestamp(indio_dev,
  954. (st->dma_st.rx_buf + st->dma_st.buf_idx),
  955. (st->dma_st.dma_ts + interval * sample_index));
  956. /* adjust remaining length */
  957. transferred_len -= sample_size;
  958. /* adjust buffer index */
  959. st->dma_st.buf_idx += sample_size;
  960. /* in case of reaching end of buffer, reset index */
  961. if (st->dma_st.buf_idx >= st->dma_st.rx_buf_sz)
  962. st->dma_st.buf_idx = 0;
  963. sample_index++;
  964. }
  965. /* adjust saved time for next transfer handling */
  966. st->dma_st.dma_ts = iio_get_time_ns(indio_dev);
  967. }
  968. static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
  969. {
  970. struct iio_poll_func *pf = p;
  971. struct iio_dev *indio_dev = pf->indio_dev;
  972. struct at91_adc_state *st = iio_priv(indio_dev);
  973. if (st->dma_st.dma_chan)
  974. at91_adc_trigger_handler_dma(indio_dev);
  975. else
  976. at91_adc_trigger_handler_nodma(indio_dev, pf);
  977. iio_trigger_notify_done(indio_dev->trig);
  978. return IRQ_HANDLED;
  979. }
  980. static int at91_adc_buffer_init(struct iio_dev *indio)
  981. {
  982. struct at91_adc_state *st = iio_priv(indio);
  983. if (st->selected_trig->hw_trig) {
  984. return devm_iio_triggered_buffer_setup(&indio->dev, indio,
  985. &iio_pollfunc_store_time,
  986. &at91_adc_trigger_handler, &at91_buffer_setup_ops);
  987. }
  988. /*
  989. * we need to prepare the buffer ops in case we will get
  990. * another buffer attached (like a callback buffer for the touchscreen)
  991. */
  992. indio->setup_ops = &at91_buffer_setup_ops;
  993. return 0;
  994. }
  995. static unsigned at91_adc_startup_time(unsigned startup_time_min,
  996. unsigned adc_clk_khz)
  997. {
  998. static const unsigned int startup_lookup[] = {
  999. 0, 8, 16, 24,
  1000. 64, 80, 96, 112,
  1001. 512, 576, 640, 704,
  1002. 768, 832, 896, 960
  1003. };
  1004. unsigned ticks_min, i;
  1005. /*
  1006. * Since the adc frequency is checked before, there is no reason
  1007. * to not meet the startup time constraint.
  1008. */
  1009. ticks_min = startup_time_min * adc_clk_khz / 1000;
  1010. for (i = 0; i < ARRAY_SIZE(startup_lookup); i++)
  1011. if (startup_lookup[i] > ticks_min)
  1012. break;
  1013. return i;
  1014. }
  1015. static void at91_adc_setup_samp_freq(struct at91_adc_state *st, unsigned freq)
  1016. {
  1017. struct iio_dev *indio_dev = iio_priv_to_dev(st);
  1018. unsigned f_per, prescal, startup, mr;
  1019. f_per = clk_get_rate(st->per_clk);
  1020. prescal = (f_per / (2 * freq)) - 1;
  1021. startup = at91_adc_startup_time(st->soc_info.startup_time,
  1022. freq / 1000);
  1023. mr = at91_adc_readl(st, AT91_SAMA5D2_MR);
  1024. mr &= ~(AT91_SAMA5D2_MR_STARTUP_MASK | AT91_SAMA5D2_MR_PRESCAL_MASK);
  1025. mr |= AT91_SAMA5D2_MR_STARTUP(startup);
  1026. mr |= AT91_SAMA5D2_MR_PRESCAL(prescal);
  1027. at91_adc_writel(st, AT91_SAMA5D2_MR, mr);
  1028. dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n",
  1029. freq, startup, prescal);
  1030. st->current_sample_rate = freq;
  1031. }
  1032. static inline unsigned at91_adc_get_sample_freq(struct at91_adc_state *st)
  1033. {
  1034. return st->current_sample_rate;
  1035. }
  1036. static void at91_adc_touch_data_handler(struct iio_dev *indio_dev)
  1037. {
  1038. struct at91_adc_state *st = iio_priv(indio_dev);
  1039. u8 bit;
  1040. u16 val;
  1041. int i = 0;
  1042. for_each_set_bit(bit, indio_dev->active_scan_mask,
  1043. AT91_SAMA5D2_MAX_CHAN_IDX + 1) {
  1044. struct iio_chan_spec const *chan =
  1045. at91_adc_chan_get(indio_dev, bit);
  1046. if (chan->type == IIO_POSITIONRELATIVE)
  1047. at91_adc_read_position(st, chan->channel, &val);
  1048. else if (chan->type == IIO_PRESSURE)
  1049. at91_adc_read_pressure(st, chan->channel, &val);
  1050. else
  1051. continue;
  1052. st->buffer[i] = val;
  1053. i++;
  1054. }
  1055. /*
  1056. * Schedule work to push to buffers.
  1057. * This is intended to push to the callback buffer that another driver
  1058. * registered. We are still in a handler from our IRQ. If we push
  1059. * directly, it means the other driver has it's callback called
  1060. * from our IRQ context. Which is something we better avoid.
  1061. * Let's schedule it after our IRQ is completed.
  1062. */
  1063. schedule_work(&st->touch_st.workq);
  1064. }
  1065. static void at91_adc_pen_detect_interrupt(struct at91_adc_state *st)
  1066. {
  1067. at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_PEN);
  1068. at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_NOPEN |
  1069. AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
  1070. AT91_SAMA5D2_IER_PRDY);
  1071. at91_adc_writel(st, AT91_SAMA5D2_TRGR,
  1072. AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC |
  1073. AT91_SAMA5D2_TRGR_TRGPER(st->touch_st.sample_period_val));
  1074. st->touch_st.touching = true;
  1075. }
  1076. static void at91_adc_no_pen_detect_interrupt(struct at91_adc_state *st)
  1077. {
  1078. struct iio_dev *indio_dev = iio_priv_to_dev(st);
  1079. at91_adc_writel(st, AT91_SAMA5D2_TRGR,
  1080. AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER);
  1081. at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_NOPEN |
  1082. AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
  1083. AT91_SAMA5D2_IER_PRDY);
  1084. st->touch_st.touching = false;
  1085. at91_adc_touch_data_handler(indio_dev);
  1086. at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_PEN);
  1087. }
  1088. static void at91_adc_workq_handler(struct work_struct *workq)
  1089. {
  1090. struct at91_adc_touch *touch_st = container_of(workq,
  1091. struct at91_adc_touch, workq);
  1092. struct at91_adc_state *st = container_of(touch_st,
  1093. struct at91_adc_state, touch_st);
  1094. struct iio_dev *indio_dev = iio_priv_to_dev(st);
  1095. iio_push_to_buffers(indio_dev, st->buffer);
  1096. }
  1097. static irqreturn_t at91_adc_interrupt(int irq, void *private)
  1098. {
  1099. struct iio_dev *indio = private;
  1100. struct at91_adc_state *st = iio_priv(indio);
  1101. u32 status = at91_adc_readl(st, AT91_SAMA5D2_ISR);
  1102. u32 imr = at91_adc_readl(st, AT91_SAMA5D2_IMR);
  1103. u32 rdy_mask = AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
  1104. AT91_SAMA5D2_IER_PRDY;
  1105. if (!(status & imr))
  1106. return IRQ_NONE;
  1107. if (status & AT91_SAMA5D2_IER_PEN) {
  1108. /* pen detected IRQ */
  1109. at91_adc_pen_detect_interrupt(st);
  1110. } else if ((status & AT91_SAMA5D2_IER_NOPEN)) {
  1111. /* nopen detected IRQ */
  1112. at91_adc_no_pen_detect_interrupt(st);
  1113. } else if ((status & AT91_SAMA5D2_ISR_PENS) &&
  1114. ((status & rdy_mask) == rdy_mask)) {
  1115. /* periodic trigger IRQ - during pen sense */
  1116. at91_adc_touch_data_handler(indio);
  1117. } else if (status & AT91_SAMA5D2_ISR_PENS) {
  1118. /*
  1119. * touching, but the measurements are not ready yet.
  1120. * read and ignore.
  1121. */
  1122. status = at91_adc_readl(st, AT91_SAMA5D2_XPOSR);
  1123. status = at91_adc_readl(st, AT91_SAMA5D2_YPOSR);
  1124. status = at91_adc_readl(st, AT91_SAMA5D2_PRESSR);
  1125. } else if (iio_buffer_enabled(indio) && !st->dma_st.dma_chan) {
  1126. /* triggered buffer without DMA */
  1127. disable_irq_nosync(irq);
  1128. iio_trigger_poll(indio->trig);
  1129. } else if (iio_buffer_enabled(indio) && st->dma_st.dma_chan) {
  1130. /* triggered buffer with DMA - should not happen */
  1131. disable_irq_nosync(irq);
  1132. WARN(true, "Unexpected irq occurred\n");
  1133. } else if (!iio_buffer_enabled(indio)) {
  1134. /* software requested conversion */
  1135. st->conversion_value = at91_adc_readl(st, st->chan->address);
  1136. st->conversion_done = true;
  1137. wake_up_interruptible(&st->wq_data_available);
  1138. }
  1139. return IRQ_HANDLED;
  1140. }
  1141. static int at91_adc_read_info_raw(struct iio_dev *indio_dev,
  1142. struct iio_chan_spec const *chan, int *val)
  1143. {
  1144. struct at91_adc_state *st = iio_priv(indio_dev);
  1145. u32 cor = 0;
  1146. u16 tmp_val;
  1147. int ret;
  1148. /*
  1149. * Keep in mind that we cannot use software trigger or touchscreen
  1150. * if external trigger is enabled
  1151. */
  1152. if (chan->type == IIO_POSITIONRELATIVE) {
  1153. ret = iio_device_claim_direct_mode(indio_dev);
  1154. if (ret)
  1155. return ret;
  1156. mutex_lock(&st->lock);
  1157. ret = at91_adc_read_position(st, chan->channel,
  1158. &tmp_val);
  1159. *val = tmp_val;
  1160. mutex_unlock(&st->lock);
  1161. iio_device_release_direct_mode(indio_dev);
  1162. return at91_adc_adjust_val_osr(st, val);
  1163. }
  1164. if (chan->type == IIO_PRESSURE) {
  1165. ret = iio_device_claim_direct_mode(indio_dev);
  1166. if (ret)
  1167. return ret;
  1168. mutex_lock(&st->lock);
  1169. ret = at91_adc_read_pressure(st, chan->channel,
  1170. &tmp_val);
  1171. *val = tmp_val;
  1172. mutex_unlock(&st->lock);
  1173. iio_device_release_direct_mode(indio_dev);
  1174. return at91_adc_adjust_val_osr(st, val);
  1175. }
  1176. /* in this case we have a voltage channel */
  1177. ret = iio_device_claim_direct_mode(indio_dev);
  1178. if (ret)
  1179. return ret;
  1180. mutex_lock(&st->lock);
  1181. st->chan = chan;
  1182. if (chan->differential)
  1183. cor = (BIT(chan->channel) | BIT(chan->channel2)) <<
  1184. AT91_SAMA5D2_COR_DIFF_OFFSET;
  1185. at91_adc_writel(st, AT91_SAMA5D2_COR, cor);
  1186. at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel));
  1187. at91_adc_writel(st, AT91_SAMA5D2_IER, BIT(chan->channel));
  1188. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START);
  1189. ret = wait_event_interruptible_timeout(st->wq_data_available,
  1190. st->conversion_done,
  1191. msecs_to_jiffies(1000));
  1192. if (ret == 0)
  1193. ret = -ETIMEDOUT;
  1194. if (ret > 0) {
  1195. *val = st->conversion_value;
  1196. ret = at91_adc_adjust_val_osr(st, val);
  1197. if (chan->scan_type.sign == 's')
  1198. *val = sign_extend32(*val, 11);
  1199. st->conversion_done = false;
  1200. }
  1201. at91_adc_writel(st, AT91_SAMA5D2_IDR, BIT(chan->channel));
  1202. at91_adc_writel(st, AT91_SAMA5D2_CHDR, BIT(chan->channel));
  1203. /* Needed to ACK the DRDY interruption */
  1204. at91_adc_readl(st, AT91_SAMA5D2_LCDR);
  1205. mutex_unlock(&st->lock);
  1206. iio_device_release_direct_mode(indio_dev);
  1207. return ret;
  1208. }
  1209. static int at91_adc_read_raw(struct iio_dev *indio_dev,
  1210. struct iio_chan_spec const *chan,
  1211. int *val, int *val2, long mask)
  1212. {
  1213. struct at91_adc_state *st = iio_priv(indio_dev);
  1214. switch (mask) {
  1215. case IIO_CHAN_INFO_RAW:
  1216. return at91_adc_read_info_raw(indio_dev, chan, val);
  1217. case IIO_CHAN_INFO_SCALE:
  1218. *val = st->vref_uv / 1000;
  1219. if (chan->differential)
  1220. *val *= 2;
  1221. *val2 = chan->scan_type.realbits;
  1222. return IIO_VAL_FRACTIONAL_LOG2;
  1223. case IIO_CHAN_INFO_SAMP_FREQ:
  1224. *val = at91_adc_get_sample_freq(st);
  1225. return IIO_VAL_INT;
  1226. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  1227. *val = st->oversampling_ratio;
  1228. return IIO_VAL_INT;
  1229. default:
  1230. return -EINVAL;
  1231. }
  1232. }
  1233. static int at91_adc_write_raw(struct iio_dev *indio_dev,
  1234. struct iio_chan_spec const *chan,
  1235. int val, int val2, long mask)
  1236. {
  1237. struct at91_adc_state *st = iio_priv(indio_dev);
  1238. switch (mask) {
  1239. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  1240. if ((val != AT91_OSR_1SAMPLES) && (val != AT91_OSR_4SAMPLES) &&
  1241. (val != AT91_OSR_16SAMPLES))
  1242. return -EINVAL;
  1243. /* if no change, optimize out */
  1244. if (val == st->oversampling_ratio)
  1245. return 0;
  1246. st->oversampling_ratio = val;
  1247. /* update ratio */
  1248. at91_adc_config_emr(st);
  1249. return 0;
  1250. case IIO_CHAN_INFO_SAMP_FREQ:
  1251. if (val < st->soc_info.min_sample_rate ||
  1252. val > st->soc_info.max_sample_rate)
  1253. return -EINVAL;
  1254. at91_adc_setup_samp_freq(st, val);
  1255. return 0;
  1256. default:
  1257. return -EINVAL;
  1258. };
  1259. }
  1260. static void at91_adc_dma_init(struct platform_device *pdev)
  1261. {
  1262. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  1263. struct at91_adc_state *st = iio_priv(indio_dev);
  1264. struct dma_slave_config config = {0};
  1265. /*
  1266. * We make the buffer double the size of the fifo,
  1267. * such that DMA uses one half of the buffer (full fifo size)
  1268. * and the software uses the other half to read/write.
  1269. */
  1270. unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE *
  1271. AT91_BUFFER_MAX_CONVERSION_BYTES * 2,
  1272. PAGE_SIZE);
  1273. if (st->dma_st.dma_chan)
  1274. return;
  1275. st->dma_st.dma_chan = dma_request_slave_channel(&pdev->dev, "rx");
  1276. if (!st->dma_st.dma_chan) {
  1277. dev_info(&pdev->dev, "can't get DMA channel\n");
  1278. goto dma_exit;
  1279. }
  1280. st->dma_st.rx_buf = dma_alloc_coherent(st->dma_st.dma_chan->device->dev,
  1281. pages * PAGE_SIZE,
  1282. &st->dma_st.rx_dma_buf,
  1283. GFP_KERNEL);
  1284. if (!st->dma_st.rx_buf) {
  1285. dev_info(&pdev->dev, "can't allocate coherent DMA area\n");
  1286. goto dma_chan_disable;
  1287. }
  1288. /* Configure DMA channel to read data register */
  1289. config.direction = DMA_DEV_TO_MEM;
  1290. config.src_addr = (phys_addr_t)(st->dma_st.phys_addr
  1291. + AT91_SAMA5D2_LCDR);
  1292. config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  1293. config.src_maxburst = 1;
  1294. config.dst_maxburst = 1;
  1295. if (dmaengine_slave_config(st->dma_st.dma_chan, &config)) {
  1296. dev_info(&pdev->dev, "can't configure DMA slave\n");
  1297. goto dma_free_area;
  1298. }
  1299. dev_info(&pdev->dev, "using %s for rx DMA transfers\n",
  1300. dma_chan_name(st->dma_st.dma_chan));
  1301. return;
  1302. dma_free_area:
  1303. dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE,
  1304. st->dma_st.rx_buf, st->dma_st.rx_dma_buf);
  1305. dma_chan_disable:
  1306. dma_release_channel(st->dma_st.dma_chan);
  1307. st->dma_st.dma_chan = 0;
  1308. dma_exit:
  1309. dev_info(&pdev->dev, "continuing without DMA support\n");
  1310. }
  1311. static void at91_adc_dma_disable(struct platform_device *pdev)
  1312. {
  1313. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  1314. struct at91_adc_state *st = iio_priv(indio_dev);
  1315. unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE *
  1316. AT91_BUFFER_MAX_CONVERSION_BYTES * 2,
  1317. PAGE_SIZE);
  1318. /* if we are not using DMA, just return */
  1319. if (!st->dma_st.dma_chan)
  1320. return;
  1321. /* wait for all transactions to be terminated first*/
  1322. dmaengine_terminate_sync(st->dma_st.dma_chan);
  1323. dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE,
  1324. st->dma_st.rx_buf, st->dma_st.rx_dma_buf);
  1325. dma_release_channel(st->dma_st.dma_chan);
  1326. st->dma_st.dma_chan = 0;
  1327. dev_info(&pdev->dev, "continuing without DMA support\n");
  1328. }
  1329. static int at91_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val)
  1330. {
  1331. struct at91_adc_state *st = iio_priv(indio_dev);
  1332. if (val > AT91_HWFIFO_MAX_SIZE)
  1333. return -EINVAL;
  1334. if (!st->selected_trig->hw_trig) {
  1335. dev_dbg(&indio_dev->dev, "we need hw trigger for DMA\n");
  1336. return 0;
  1337. }
  1338. dev_dbg(&indio_dev->dev, "new watermark is %u\n", val);
  1339. st->dma_st.watermark = val;
  1340. /*
  1341. * The logic here is: if we have watermark 1, it means we do
  1342. * each conversion with it's own IRQ, thus we don't need DMA.
  1343. * If the watermark is higher, we do DMA to do all the transfers in bulk
  1344. */
  1345. if (val == 1)
  1346. at91_adc_dma_disable(to_platform_device(&indio_dev->dev));
  1347. else if (val > 1)
  1348. at91_adc_dma_init(to_platform_device(&indio_dev->dev));
  1349. return 0;
  1350. }
  1351. static int at91_adc_update_scan_mode(struct iio_dev *indio_dev,
  1352. const unsigned long *scan_mask)
  1353. {
  1354. struct at91_adc_state *st = iio_priv(indio_dev);
  1355. if (bitmap_subset(scan_mask, &st->touch_st.channels_bitmask,
  1356. AT91_SAMA5D2_MAX_CHAN_IDX + 1))
  1357. return 0;
  1358. /*
  1359. * if the new bitmap is a combination of touchscreen and regular
  1360. * channels, then we are not fine
  1361. */
  1362. if (bitmap_intersects(&st->touch_st.channels_bitmask, scan_mask,
  1363. AT91_SAMA5D2_MAX_CHAN_IDX + 1))
  1364. return -EINVAL;
  1365. return 0;
  1366. }
  1367. static void at91_adc_hw_init(struct at91_adc_state *st)
  1368. {
  1369. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST);
  1370. at91_adc_writel(st, AT91_SAMA5D2_IDR, 0xffffffff);
  1371. /*
  1372. * Transfer field must be set to 2 according to the datasheet and
  1373. * allows different analog settings for each channel.
  1374. */
  1375. at91_adc_writel(st, AT91_SAMA5D2_MR,
  1376. AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH);
  1377. at91_adc_setup_samp_freq(st, st->soc_info.min_sample_rate);
  1378. /* configure extended mode register */
  1379. at91_adc_config_emr(st);
  1380. }
  1381. static ssize_t at91_adc_get_fifo_state(struct device *dev,
  1382. struct device_attribute *attr, char *buf)
  1383. {
  1384. struct iio_dev *indio_dev =
  1385. platform_get_drvdata(to_platform_device(dev));
  1386. struct at91_adc_state *st = iio_priv(indio_dev);
  1387. return scnprintf(buf, PAGE_SIZE, "%d\n", !!st->dma_st.dma_chan);
  1388. }
  1389. static ssize_t at91_adc_get_watermark(struct device *dev,
  1390. struct device_attribute *attr, char *buf)
  1391. {
  1392. struct iio_dev *indio_dev =
  1393. platform_get_drvdata(to_platform_device(dev));
  1394. struct at91_adc_state *st = iio_priv(indio_dev);
  1395. return scnprintf(buf, PAGE_SIZE, "%d\n", st->dma_st.watermark);
  1396. }
  1397. static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
  1398. at91_adc_get_fifo_state, NULL, 0);
  1399. static IIO_DEVICE_ATTR(hwfifo_watermark, 0444,
  1400. at91_adc_get_watermark, NULL, 0);
  1401. static IIO_CONST_ATTR(hwfifo_watermark_min, "2");
  1402. static IIO_CONST_ATTR(hwfifo_watermark_max, AT91_HWFIFO_MAX_SIZE_STR);
  1403. static IIO_CONST_ATTR(oversampling_ratio_available,
  1404. __stringify(AT91_OSR_1SAMPLES) " "
  1405. __stringify(AT91_OSR_4SAMPLES) " "
  1406. __stringify(AT91_OSR_16SAMPLES));
  1407. static struct attribute *at91_adc_attributes[] = {
  1408. &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
  1409. NULL,
  1410. };
  1411. static const struct attribute_group at91_adc_attribute_group = {
  1412. .attrs = at91_adc_attributes,
  1413. };
  1414. static const struct attribute *at91_adc_fifo_attributes[] = {
  1415. &iio_const_attr_hwfifo_watermark_min.dev_attr.attr,
  1416. &iio_const_attr_hwfifo_watermark_max.dev_attr.attr,
  1417. &iio_dev_attr_hwfifo_watermark.dev_attr.attr,
  1418. &iio_dev_attr_hwfifo_enabled.dev_attr.attr,
  1419. NULL,
  1420. };
  1421. static const struct iio_info at91_adc_info = {
  1422. .attrs = &at91_adc_attribute_group,
  1423. .read_raw = &at91_adc_read_raw,
  1424. .write_raw = &at91_adc_write_raw,
  1425. .update_scan_mode = &at91_adc_update_scan_mode,
  1426. .of_xlate = &at91_adc_of_xlate,
  1427. .hwfifo_set_watermark = &at91_adc_set_watermark,
  1428. };
  1429. static int at91_adc_probe(struct platform_device *pdev)
  1430. {
  1431. struct iio_dev *indio_dev;
  1432. struct at91_adc_state *st;
  1433. struct resource *res;
  1434. int ret, i;
  1435. u32 edge_type = IRQ_TYPE_NONE;
  1436. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
  1437. if (!indio_dev)
  1438. return -ENOMEM;
  1439. indio_dev->dev.parent = &pdev->dev;
  1440. indio_dev->name = dev_name(&pdev->dev);
  1441. indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
  1442. indio_dev->info = &at91_adc_info;
  1443. indio_dev->channels = at91_adc_channels;
  1444. indio_dev->num_channels = ARRAY_SIZE(at91_adc_channels);
  1445. st = iio_priv(indio_dev);
  1446. bitmap_set(&st->touch_st.channels_bitmask,
  1447. AT91_SAMA5D2_TOUCH_X_CHAN_IDX, 1);
  1448. bitmap_set(&st->touch_st.channels_bitmask,
  1449. AT91_SAMA5D2_TOUCH_Y_CHAN_IDX, 1);
  1450. bitmap_set(&st->touch_st.channels_bitmask,
  1451. AT91_SAMA5D2_TOUCH_P_CHAN_IDX, 1);
  1452. st->oversampling_ratio = AT91_OSR_1SAMPLES;
  1453. ret = of_property_read_u32(pdev->dev.of_node,
  1454. "atmel,min-sample-rate-hz",
  1455. &st->soc_info.min_sample_rate);
  1456. if (ret) {
  1457. dev_err(&pdev->dev,
  1458. "invalid or missing value for atmel,min-sample-rate-hz\n");
  1459. return ret;
  1460. }
  1461. ret = of_property_read_u32(pdev->dev.of_node,
  1462. "atmel,max-sample-rate-hz",
  1463. &st->soc_info.max_sample_rate);
  1464. if (ret) {
  1465. dev_err(&pdev->dev,
  1466. "invalid or missing value for atmel,max-sample-rate-hz\n");
  1467. return ret;
  1468. }
  1469. ret = of_property_read_u32(pdev->dev.of_node, "atmel,startup-time-ms",
  1470. &st->soc_info.startup_time);
  1471. if (ret) {
  1472. dev_err(&pdev->dev,
  1473. "invalid or missing value for atmel,startup-time-ms\n");
  1474. return ret;
  1475. }
  1476. ret = of_property_read_u32(pdev->dev.of_node,
  1477. "atmel,trigger-edge-type", &edge_type);
  1478. if (ret) {
  1479. dev_dbg(&pdev->dev,
  1480. "atmel,trigger-edge-type not specified, only software trigger available\n");
  1481. }
  1482. st->selected_trig = NULL;
  1483. /* find the right trigger, or no trigger at all */
  1484. for (i = 0; i < AT91_SAMA5D2_HW_TRIG_CNT + 1; i++)
  1485. if (at91_adc_trigger_list[i].edge_type == edge_type) {
  1486. st->selected_trig = &at91_adc_trigger_list[i];
  1487. break;
  1488. }
  1489. if (!st->selected_trig) {
  1490. dev_err(&pdev->dev, "invalid external trigger edge value\n");
  1491. return -EINVAL;
  1492. }
  1493. init_waitqueue_head(&st->wq_data_available);
  1494. mutex_init(&st->lock);
  1495. INIT_WORK(&st->touch_st.workq, at91_adc_workq_handler);
  1496. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1497. if (!res)
  1498. return -EINVAL;
  1499. /* if we plan to use DMA, we need the physical address of the regs */
  1500. st->dma_st.phys_addr = res->start;
  1501. st->base = devm_ioremap_resource(&pdev->dev, res);
  1502. if (IS_ERR(st->base))
  1503. return PTR_ERR(st->base);
  1504. st->irq = platform_get_irq(pdev, 0);
  1505. if (st->irq <= 0) {
  1506. if (!st->irq)
  1507. st->irq = -ENXIO;
  1508. return st->irq;
  1509. }
  1510. st->per_clk = devm_clk_get(&pdev->dev, "adc_clk");
  1511. if (IS_ERR(st->per_clk))
  1512. return PTR_ERR(st->per_clk);
  1513. st->reg = devm_regulator_get(&pdev->dev, "vddana");
  1514. if (IS_ERR(st->reg))
  1515. return PTR_ERR(st->reg);
  1516. st->vref = devm_regulator_get(&pdev->dev, "vref");
  1517. if (IS_ERR(st->vref))
  1518. return PTR_ERR(st->vref);
  1519. ret = devm_request_irq(&pdev->dev, st->irq, at91_adc_interrupt, 0,
  1520. pdev->dev.driver->name, indio_dev);
  1521. if (ret)
  1522. return ret;
  1523. ret = regulator_enable(st->reg);
  1524. if (ret)
  1525. return ret;
  1526. ret = regulator_enable(st->vref);
  1527. if (ret)
  1528. goto reg_disable;
  1529. st->vref_uv = regulator_get_voltage(st->vref);
  1530. if (st->vref_uv <= 0) {
  1531. ret = -EINVAL;
  1532. goto vref_disable;
  1533. }
  1534. at91_adc_hw_init(st);
  1535. ret = clk_prepare_enable(st->per_clk);
  1536. if (ret)
  1537. goto vref_disable;
  1538. platform_set_drvdata(pdev, indio_dev);
  1539. ret = at91_adc_buffer_init(indio_dev);
  1540. if (ret < 0) {
  1541. dev_err(&pdev->dev, "couldn't initialize the buffer.\n");
  1542. goto per_clk_disable_unprepare;
  1543. }
  1544. if (st->selected_trig->hw_trig) {
  1545. ret = at91_adc_trigger_init(indio_dev);
  1546. if (ret < 0) {
  1547. dev_err(&pdev->dev, "couldn't setup the triggers.\n");
  1548. goto per_clk_disable_unprepare;
  1549. }
  1550. /*
  1551. * Initially the iio buffer has a length of 2 and
  1552. * a watermark of 1
  1553. */
  1554. st->dma_st.watermark = 1;
  1555. iio_buffer_set_attrs(indio_dev->buffer,
  1556. at91_adc_fifo_attributes);
  1557. }
  1558. if (dma_coerce_mask_and_coherent(&indio_dev->dev, DMA_BIT_MASK(32)))
  1559. dev_info(&pdev->dev, "cannot set DMA mask to 32-bit\n");
  1560. ret = iio_device_register(indio_dev);
  1561. if (ret < 0)
  1562. goto dma_disable;
  1563. if (st->selected_trig->hw_trig)
  1564. dev_info(&pdev->dev, "setting up trigger as %s\n",
  1565. st->selected_trig->name);
  1566. dev_info(&pdev->dev, "version: %x\n",
  1567. readl_relaxed(st->base + AT91_SAMA5D2_VERSION));
  1568. return 0;
  1569. dma_disable:
  1570. at91_adc_dma_disable(pdev);
  1571. per_clk_disable_unprepare:
  1572. clk_disable_unprepare(st->per_clk);
  1573. vref_disable:
  1574. regulator_disable(st->vref);
  1575. reg_disable:
  1576. regulator_disable(st->reg);
  1577. return ret;
  1578. }
  1579. static int at91_adc_remove(struct platform_device *pdev)
  1580. {
  1581. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  1582. struct at91_adc_state *st = iio_priv(indio_dev);
  1583. iio_device_unregister(indio_dev);
  1584. at91_adc_dma_disable(pdev);
  1585. clk_disable_unprepare(st->per_clk);
  1586. regulator_disable(st->vref);
  1587. regulator_disable(st->reg);
  1588. return 0;
  1589. }
  1590. static __maybe_unused int at91_adc_suspend(struct device *dev)
  1591. {
  1592. struct iio_dev *indio_dev =
  1593. platform_get_drvdata(to_platform_device(dev));
  1594. struct at91_adc_state *st = iio_priv(indio_dev);
  1595. /*
  1596. * Do a sofware reset of the ADC before we go to suspend.
  1597. * this will ensure that all pins are free from being muxed by the ADC
  1598. * and can be used by for other devices.
  1599. * Otherwise, ADC will hog them and we can't go to suspend mode.
  1600. */
  1601. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST);
  1602. clk_disable_unprepare(st->per_clk);
  1603. regulator_disable(st->vref);
  1604. regulator_disable(st->reg);
  1605. return pinctrl_pm_select_sleep_state(dev);
  1606. }
  1607. static __maybe_unused int at91_adc_resume(struct device *dev)
  1608. {
  1609. struct iio_dev *indio_dev =
  1610. platform_get_drvdata(to_platform_device(dev));
  1611. struct at91_adc_state *st = iio_priv(indio_dev);
  1612. int ret;
  1613. ret = pinctrl_pm_select_default_state(dev);
  1614. if (ret)
  1615. goto resume_failed;
  1616. ret = regulator_enable(st->reg);
  1617. if (ret)
  1618. goto resume_failed;
  1619. ret = regulator_enable(st->vref);
  1620. if (ret)
  1621. goto reg_disable_resume;
  1622. ret = clk_prepare_enable(st->per_clk);
  1623. if (ret)
  1624. goto vref_disable_resume;
  1625. at91_adc_hw_init(st);
  1626. /* reconfiguring trigger hardware state */
  1627. if (!iio_buffer_enabled(indio_dev))
  1628. return 0;
  1629. /* check if we are enabling triggered buffer or the touchscreen */
  1630. if (bitmap_subset(indio_dev->active_scan_mask,
  1631. &st->touch_st.channels_bitmask,
  1632. AT91_SAMA5D2_MAX_CHAN_IDX + 1)) {
  1633. /* touchscreen enabling */
  1634. return at91_adc_configure_touch(st, true);
  1635. } else {
  1636. return at91_adc_configure_trigger(st->trig, true);
  1637. }
  1638. /* not needed but more explicit */
  1639. return 0;
  1640. vref_disable_resume:
  1641. regulator_disable(st->vref);
  1642. reg_disable_resume:
  1643. regulator_disable(st->reg);
  1644. resume_failed:
  1645. dev_err(&indio_dev->dev, "failed to resume\n");
  1646. return ret;
  1647. }
  1648. static SIMPLE_DEV_PM_OPS(at91_adc_pm_ops, at91_adc_suspend, at91_adc_resume);
  1649. static const struct of_device_id at91_adc_dt_match[] = {
  1650. {
  1651. .compatible = "atmel,sama5d2-adc",
  1652. }, {
  1653. /* sentinel */
  1654. }
  1655. };
  1656. MODULE_DEVICE_TABLE(of, at91_adc_dt_match);
  1657. static struct platform_driver at91_adc_driver = {
  1658. .probe = at91_adc_probe,
  1659. .remove = at91_adc_remove,
  1660. .driver = {
  1661. .name = "at91-sama5d2_adc",
  1662. .of_match_table = at91_adc_dt_match,
  1663. .pm = &at91_adc_pm_ops,
  1664. },
  1665. };
  1666. module_platform_driver(at91_adc_driver)
  1667. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  1668. MODULE_DESCRIPTION("Atmel AT91 SAMA5D2 ADC");
  1669. MODULE_LICENSE("GPL v2");