at91-sama5d2_adc.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Atmel ADC driver for SAMA5D2 devices and compatible.
  4. *
  5. * Copyright (C) 2015 Atmel,
  6. * 2015 Ludovic Desroches <[email protected]>
  7. * 2021 Microchip Technology, Inc. and its subsidiaries
  8. * 2021 Eugen Hristev <[email protected]>
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/property.h>
  21. #include <linux/sched.h>
  22. #include <linux/units.h>
  23. #include <linux/wait.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/sysfs.h>
  26. #include <linux/iio/buffer.h>
  27. #include <linux/iio/trigger.h>
  28. #include <linux/iio/trigger_consumer.h>
  29. #include <linux/iio/triggered_buffer.h>
  30. #include <linux/nvmem-consumer.h>
  31. #include <linux/pinctrl/consumer.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <dt-bindings/iio/adc/at91-sama5d2_adc.h>
  35. struct at91_adc_reg_layout {
  36. /* Control Register */
  37. u16 CR;
  38. /* Software Reset */
  39. #define AT91_SAMA5D2_CR_SWRST BIT(0)
  40. /* Start Conversion */
  41. #define AT91_SAMA5D2_CR_START BIT(1)
  42. /* Touchscreen Calibration */
  43. #define AT91_SAMA5D2_CR_TSCALIB BIT(2)
  44. /* Comparison Restart */
  45. #define AT91_SAMA5D2_CR_CMPRST BIT(4)
  46. /* Mode Register */
  47. u16 MR;
  48. /* Trigger Selection */
  49. #define AT91_SAMA5D2_MR_TRGSEL(v) ((v) << 1)
  50. /* ADTRG */
  51. #define AT91_SAMA5D2_MR_TRGSEL_TRIG0 0
  52. /* TIOA0 */
  53. #define AT91_SAMA5D2_MR_TRGSEL_TRIG1 1
  54. /* TIOA1 */
  55. #define AT91_SAMA5D2_MR_TRGSEL_TRIG2 2
  56. /* TIOA2 */
  57. #define AT91_SAMA5D2_MR_TRGSEL_TRIG3 3
  58. /* PWM event line 0 */
  59. #define AT91_SAMA5D2_MR_TRGSEL_TRIG4 4
  60. /* PWM event line 1 */
  61. #define AT91_SAMA5D2_MR_TRGSEL_TRIG5 5
  62. /* TIOA3 */
  63. #define AT91_SAMA5D2_MR_TRGSEL_TRIG6 6
  64. /* RTCOUT0 */
  65. #define AT91_SAMA5D2_MR_TRGSEL_TRIG7 7
  66. /* Sleep Mode */
  67. #define AT91_SAMA5D2_MR_SLEEP BIT(5)
  68. /* Fast Wake Up */
  69. #define AT91_SAMA5D2_MR_FWUP BIT(6)
  70. /* Prescaler Rate Selection */
  71. #define AT91_SAMA5D2_MR_PRESCAL(v) ((v) << AT91_SAMA5D2_MR_PRESCAL_OFFSET)
  72. #define AT91_SAMA5D2_MR_PRESCAL_OFFSET 8
  73. #define AT91_SAMA5D2_MR_PRESCAL_MAX 0xff
  74. #define AT91_SAMA5D2_MR_PRESCAL_MASK GENMASK(15, 8)
  75. /* Startup Time */
  76. #define AT91_SAMA5D2_MR_STARTUP(v) ((v) << 16)
  77. #define AT91_SAMA5D2_MR_STARTUP_MASK GENMASK(19, 16)
  78. /* Minimum startup time for temperature sensor */
  79. #define AT91_SAMA5D2_MR_STARTUP_TS_MIN (50)
  80. /* Analog Change */
  81. #define AT91_SAMA5D2_MR_ANACH BIT(23)
  82. /* Tracking Time */
  83. #define AT91_SAMA5D2_MR_TRACKTIM(v) ((v) << 24)
  84. #define AT91_SAMA5D2_MR_TRACKTIM_TS 6
  85. #define AT91_SAMA5D2_MR_TRACKTIM_MAX 0xf
  86. /* Transfer Time */
  87. #define AT91_SAMA5D2_MR_TRANSFER(v) ((v) << 28)
  88. #define AT91_SAMA5D2_MR_TRANSFER_MAX 0x3
  89. /* Use Sequence Enable */
  90. #define AT91_SAMA5D2_MR_USEQ BIT(31)
  91. /* Channel Sequence Register 1 */
  92. u16 SEQR1;
  93. /* Channel Sequence Register 2 */
  94. u16 SEQR2;
  95. /* Channel Enable Register */
  96. u16 CHER;
  97. /* Channel Disable Register */
  98. u16 CHDR;
  99. /* Channel Status Register */
  100. u16 CHSR;
  101. /* Last Converted Data Register */
  102. u16 LCDR;
  103. /* Interrupt Enable Register */
  104. u16 IER;
  105. /* Interrupt Enable Register - TS X measurement ready */
  106. #define AT91_SAMA5D2_IER_XRDY BIT(20)
  107. /* Interrupt Enable Register - TS Y measurement ready */
  108. #define AT91_SAMA5D2_IER_YRDY BIT(21)
  109. /* Interrupt Enable Register - TS pressure measurement ready */
  110. #define AT91_SAMA5D2_IER_PRDY BIT(22)
  111. /* Interrupt Enable Register - Data ready */
  112. #define AT91_SAMA5D2_IER_DRDY BIT(24)
  113. /* Interrupt Enable Register - general overrun error */
  114. #define AT91_SAMA5D2_IER_GOVRE BIT(25)
  115. /* Interrupt Enable Register - Pen detect */
  116. #define AT91_SAMA5D2_IER_PEN BIT(29)
  117. /* Interrupt Enable Register - No pen detect */
  118. #define AT91_SAMA5D2_IER_NOPEN BIT(30)
  119. /* Interrupt Disable Register */
  120. u16 IDR;
  121. /* Interrupt Mask Register */
  122. u16 IMR;
  123. /* Interrupt Status Register */
  124. u16 ISR;
  125. /* End of Conversion Interrupt Enable Register */
  126. u16 EOC_IER;
  127. /* End of Conversion Interrupt Disable Register */
  128. u16 EOC_IDR;
  129. /* End of Conversion Interrupt Mask Register */
  130. u16 EOC_IMR;
  131. /* End of Conversion Interrupt Status Register */
  132. u16 EOC_ISR;
  133. /* Interrupt Status Register - Pen touching sense status */
  134. #define AT91_SAMA5D2_ISR_PENS BIT(31)
  135. /* Last Channel Trigger Mode Register */
  136. u16 LCTMR;
  137. /* Last Channel Compare Window Register */
  138. u16 LCCWR;
  139. /* Overrun Status Register */
  140. u16 OVER;
  141. /* Extended Mode Register */
  142. u16 EMR;
  143. /* Extended Mode Register - Oversampling rate */
  144. #define AT91_SAMA5D2_EMR_OSR(V, M) (((V) << 16) & (M))
  145. #define AT91_SAMA5D2_EMR_OSR_1SAMPLES 0
  146. #define AT91_SAMA5D2_EMR_OSR_4SAMPLES 1
  147. #define AT91_SAMA5D2_EMR_OSR_16SAMPLES 2
  148. #define AT91_SAMA5D2_EMR_OSR_64SAMPLES 3
  149. #define AT91_SAMA5D2_EMR_OSR_256SAMPLES 4
  150. /* Extended Mode Register - TRACKX */
  151. #define AT91_SAMA5D2_TRACKX_MASK GENMASK(23, 22)
  152. #define AT91_SAMA5D2_TRACKX(x) (((x) << 22) & \
  153. AT91_SAMA5D2_TRACKX_MASK)
  154. /* TRACKX for temperature sensor. */
  155. #define AT91_SAMA5D2_TRACKX_TS (1)
  156. /* Extended Mode Register - Averaging on single trigger event */
  157. #define AT91_SAMA5D2_EMR_ASTE(V) ((V) << 20)
  158. /* Compare Window Register */
  159. u16 CWR;
  160. /* Channel Gain Register */
  161. u16 CGR;
  162. /* Channel Offset Register */
  163. u16 COR;
  164. /* Channel Offset Register differential offset - constant, not a register */
  165. u16 COR_diff_offset;
  166. /* Analog Control Register */
  167. u16 ACR;
  168. /* Analog Control Register - Pen detect sensitivity mask */
  169. #define AT91_SAMA5D2_ACR_PENDETSENS_MASK GENMASK(1, 0)
  170. /* Analog Control Register - Source last channel */
  171. #define AT91_SAMA5D2_ACR_SRCLCH BIT(16)
  172. /* Touchscreen Mode Register */
  173. u16 TSMR;
  174. /* Touchscreen Mode Register - No touch mode */
  175. #define AT91_SAMA5D2_TSMR_TSMODE_NONE 0
  176. /* Touchscreen Mode Register - 4 wire screen, no pressure measurement */
  177. #define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_NO_PRESS 1
  178. /* Touchscreen Mode Register - 4 wire screen, pressure measurement */
  179. #define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS 2
  180. /* Touchscreen Mode Register - 5 wire screen */
  181. #define AT91_SAMA5D2_TSMR_TSMODE_5WIRE 3
  182. /* Touchscreen Mode Register - Average samples mask */
  183. #define AT91_SAMA5D2_TSMR_TSAV_MASK GENMASK(5, 4)
  184. /* Touchscreen Mode Register - Average samples */
  185. #define AT91_SAMA5D2_TSMR_TSAV(x) ((x) << 4)
  186. /* Touchscreen Mode Register - Touch/trigger frequency ratio mask */
  187. #define AT91_SAMA5D2_TSMR_TSFREQ_MASK GENMASK(11, 8)
  188. /* Touchscreen Mode Register - Touch/trigger frequency ratio */
  189. #define AT91_SAMA5D2_TSMR_TSFREQ(x) ((x) << 8)
  190. /* Touchscreen Mode Register - Pen Debounce Time mask */
  191. #define AT91_SAMA5D2_TSMR_PENDBC_MASK GENMASK(31, 28)
  192. /* Touchscreen Mode Register - Pen Debounce Time */
  193. #define AT91_SAMA5D2_TSMR_PENDBC(x) ((x) << 28)
  194. /* Touchscreen Mode Register - No DMA for touch measurements */
  195. #define AT91_SAMA5D2_TSMR_NOTSDMA BIT(22)
  196. /* Touchscreen Mode Register - Disable pen detection */
  197. #define AT91_SAMA5D2_TSMR_PENDET_DIS (0 << 24)
  198. /* Touchscreen Mode Register - Enable pen detection */
  199. #define AT91_SAMA5D2_TSMR_PENDET_ENA BIT(24)
  200. /* Touchscreen X Position Register */
  201. u16 XPOSR;
  202. /* Touchscreen Y Position Register */
  203. u16 YPOSR;
  204. /* Touchscreen Pressure Register */
  205. u16 PRESSR;
  206. /* Trigger Register */
  207. u16 TRGR;
  208. /* Mask for TRGMOD field of TRGR register */
  209. #define AT91_SAMA5D2_TRGR_TRGMOD_MASK GENMASK(2, 0)
  210. /* No trigger, only software trigger can start conversions */
  211. #define AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER 0
  212. /* Trigger Mode external trigger rising edge */
  213. #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE 1
  214. /* Trigger Mode external trigger falling edge */
  215. #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL 2
  216. /* Trigger Mode external trigger any edge */
  217. #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY 3
  218. /* Trigger Mode internal periodic */
  219. #define AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC 5
  220. /* Trigger Mode - trigger period mask */
  221. #define AT91_SAMA5D2_TRGR_TRGPER_MASK GENMASK(31, 16)
  222. /* Trigger Mode - trigger period */
  223. #define AT91_SAMA5D2_TRGR_TRGPER(x) ((x) << 16)
  224. /* Correction Select Register */
  225. u16 COSR;
  226. /* Correction Value Register */
  227. u16 CVR;
  228. /* Channel Error Correction Register */
  229. u16 CECR;
  230. /* Write Protection Mode Register */
  231. u16 WPMR;
  232. /* Write Protection Status Register */
  233. u16 WPSR;
  234. /* Version Register */
  235. u16 VERSION;
  236. /* Temperature Sensor Mode Register */
  237. u16 TEMPMR;
  238. /* Temperature Sensor Mode - Temperature sensor on */
  239. #define AT91_SAMA5D2_TEMPMR_TEMPON BIT(0)
  240. };
  241. static const struct at91_adc_reg_layout sama5d2_layout = {
  242. .CR = 0x00,
  243. .MR = 0x04,
  244. .SEQR1 = 0x08,
  245. .SEQR2 = 0x0c,
  246. .CHER = 0x10,
  247. .CHDR = 0x14,
  248. .CHSR = 0x18,
  249. .LCDR = 0x20,
  250. .IER = 0x24,
  251. .IDR = 0x28,
  252. .IMR = 0x2c,
  253. .ISR = 0x30,
  254. .LCTMR = 0x34,
  255. .LCCWR = 0x38,
  256. .OVER = 0x3c,
  257. .EMR = 0x40,
  258. .CWR = 0x44,
  259. .CGR = 0x48,
  260. .COR = 0x4c,
  261. .COR_diff_offset = 16,
  262. .ACR = 0x94,
  263. .TSMR = 0xb0,
  264. .XPOSR = 0xb4,
  265. .YPOSR = 0xb8,
  266. .PRESSR = 0xbc,
  267. .TRGR = 0xc0,
  268. .COSR = 0xd0,
  269. .CVR = 0xd4,
  270. .CECR = 0xd8,
  271. .WPMR = 0xe4,
  272. .WPSR = 0xe8,
  273. .VERSION = 0xfc,
  274. };
  275. static const struct at91_adc_reg_layout sama7g5_layout = {
  276. .CR = 0x00,
  277. .MR = 0x04,
  278. .SEQR1 = 0x08,
  279. .SEQR2 = 0x0c,
  280. .CHER = 0x10,
  281. .CHDR = 0x14,
  282. .CHSR = 0x18,
  283. .LCDR = 0x20,
  284. .IER = 0x24,
  285. .IDR = 0x28,
  286. .IMR = 0x2c,
  287. .ISR = 0x30,
  288. .EOC_IER = 0x34,
  289. .EOC_IDR = 0x38,
  290. .EOC_IMR = 0x3c,
  291. .EOC_ISR = 0x40,
  292. .TEMPMR = 0x44,
  293. .OVER = 0x4c,
  294. .EMR = 0x50,
  295. .CWR = 0x54,
  296. .COR = 0x5c,
  297. .COR_diff_offset = 0,
  298. .ACR = 0xe0,
  299. .TRGR = 0x100,
  300. .COSR = 0x104,
  301. .CVR = 0x108,
  302. .CECR = 0x10c,
  303. .WPMR = 0x118,
  304. .WPSR = 0x11c,
  305. .VERSION = 0x130,
  306. };
  307. #define AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */
  308. #define AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US 200
  309. #define AT91_SAMA5D2_XYZ_MASK GENMASK(11, 0)
  310. #define AT91_SAMA5D2_MAX_POS_BITS 12
  311. #define AT91_HWFIFO_MAX_SIZE_STR "128"
  312. #define AT91_HWFIFO_MAX_SIZE 128
  313. #define AT91_SAMA5D2_CHAN_SINGLE(index, num, addr) \
  314. { \
  315. .type = IIO_VOLTAGE, \
  316. .channel = num, \
  317. .address = addr, \
  318. .scan_index = index, \
  319. .scan_type = { \
  320. .sign = 'u', \
  321. .realbits = 14, \
  322. .storagebits = 16, \
  323. }, \
  324. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  325. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  326. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
  327. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  328. .info_mask_shared_by_all_available = \
  329. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  330. .datasheet_name = "CH"#num, \
  331. .indexed = 1, \
  332. }
  333. #define AT91_SAMA5D2_CHAN_DIFF(index, num, num2, addr) \
  334. { \
  335. .type = IIO_VOLTAGE, \
  336. .differential = 1, \
  337. .channel = num, \
  338. .channel2 = num2, \
  339. .address = addr, \
  340. .scan_index = index, \
  341. .scan_type = { \
  342. .sign = 's', \
  343. .realbits = 14, \
  344. .storagebits = 16, \
  345. }, \
  346. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  347. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  348. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
  349. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  350. .info_mask_shared_by_all_available = \
  351. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  352. .datasheet_name = "CH"#num"-CH"#num2, \
  353. .indexed = 1, \
  354. }
  355. #define AT91_SAMA5D2_CHAN_TOUCH(num, name, mod) \
  356. { \
  357. .type = IIO_POSITIONRELATIVE, \
  358. .modified = 1, \
  359. .channel = num, \
  360. .channel2 = mod, \
  361. .scan_index = num, \
  362. .scan_type = { \
  363. .sign = 'u', \
  364. .realbits = 12, \
  365. .storagebits = 16, \
  366. }, \
  367. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  368. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
  369. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  370. .info_mask_shared_by_all_available = \
  371. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  372. .datasheet_name = name, \
  373. }
  374. #define AT91_SAMA5D2_CHAN_PRESSURE(num, name) \
  375. { \
  376. .type = IIO_PRESSURE, \
  377. .channel = num, \
  378. .scan_index = num, \
  379. .scan_type = { \
  380. .sign = 'u', \
  381. .realbits = 12, \
  382. .storagebits = 16, \
  383. }, \
  384. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  385. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
  386. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  387. .info_mask_shared_by_all_available = \
  388. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  389. .datasheet_name = name, \
  390. }
  391. #define AT91_SAMA5D2_CHAN_TEMP(num, name, addr) \
  392. { \
  393. .type = IIO_TEMP, \
  394. .channel = num, \
  395. .address = addr, \
  396. .scan_index = num, \
  397. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
  398. .info_mask_shared_by_all = \
  399. BIT(IIO_CHAN_INFO_PROCESSED) | \
  400. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  401. .info_mask_shared_by_all_available = \
  402. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  403. .datasheet_name = name, \
  404. }
  405. #define at91_adc_readl(st, reg) \
  406. readl_relaxed((st)->base + (st)->soc_info.platform->layout->reg)
  407. #define at91_adc_read_chan(st, reg) \
  408. readl_relaxed((st)->base + reg)
  409. #define at91_adc_writel(st, reg, val) \
  410. writel_relaxed(val, (st)->base + (st)->soc_info.platform->layout->reg)
  411. /**
  412. * struct at91_adc_platform - at91-sama5d2 platform information struct
  413. * @layout: pointer to the reg layout struct
  414. * @adc_channels: pointer to an array of channels for registering in
  415. * the iio subsystem
  416. * @nr_channels: number of physical channels available
  417. * @touch_chan_x: index of the touchscreen X channel
  418. * @touch_chan_y: index of the touchscreen Y channel
  419. * @touch_chan_p: index of the touchscreen P channel
  420. * @max_channels: number of total channels
  421. * @max_index: highest channel index (highest index may be higher
  422. * than the total channel number)
  423. * @hw_trig_cnt: number of possible hardware triggers
  424. * @osr_mask: oversampling ratio bitmask on EMR register
  425. * @oversampling_avail: available oversampling values
  426. * @oversampling_avail_no: number of available oversampling values
  427. * @chan_realbits: realbits for registered channels
  428. * @temp_chan: temperature channel index
  429. * @temp_sensor: temperature sensor supported
  430. */
  431. struct at91_adc_platform {
  432. const struct at91_adc_reg_layout *layout;
  433. const struct iio_chan_spec (*adc_channels)[];
  434. unsigned int nr_channels;
  435. unsigned int touch_chan_x;
  436. unsigned int touch_chan_y;
  437. unsigned int touch_chan_p;
  438. unsigned int max_channels;
  439. unsigned int max_index;
  440. unsigned int hw_trig_cnt;
  441. unsigned int osr_mask;
  442. unsigned int oversampling_avail[5];
  443. unsigned int oversampling_avail_no;
  444. unsigned int chan_realbits;
  445. unsigned int temp_chan;
  446. bool temp_sensor;
  447. };
  448. /**
  449. * struct at91_adc_temp_sensor_clb - at91-sama5d2 temperature sensor
  450. * calibration data structure
  451. * @p1: P1 calibration temperature
  452. * @p4: P4 calibration voltage
  453. * @p6: P6 calibration voltage
  454. */
  455. struct at91_adc_temp_sensor_clb {
  456. u32 p1;
  457. u32 p4;
  458. u32 p6;
  459. };
  460. /**
  461. * enum at91_adc_ts_clb_idx - calibration indexes in NVMEM buffer
  462. * @AT91_ADC_TS_CLB_IDX_P1: index for P1
  463. * @AT91_ADC_TS_CLB_IDX_P4: index for P4
  464. * @AT91_ADC_TS_CLB_IDX_P6: index for P6
  465. * @AT91_ADC_TS_CLB_IDX_MAX: max index for temperature calibration packet in OTP
  466. */
  467. enum at91_adc_ts_clb_idx {
  468. AT91_ADC_TS_CLB_IDX_P1 = 2,
  469. AT91_ADC_TS_CLB_IDX_P4 = 5,
  470. AT91_ADC_TS_CLB_IDX_P6 = 7,
  471. AT91_ADC_TS_CLB_IDX_MAX = 19,
  472. };
  473. /* Temperature sensor calibration - Vtemp voltage sensitivity to temperature. */
  474. #define AT91_ADC_TS_VTEMP_DT (2080U)
  475. /**
  476. * struct at91_adc_soc_info - at91-sama5d2 soc information struct
  477. * @startup_time: device startup time
  478. * @min_sample_rate: minimum sample rate in Hz
  479. * @max_sample_rate: maximum sample rate in Hz
  480. * @platform: pointer to the platform structure
  481. * @temp_sensor_clb: temperature sensor calibration data structure
  482. */
  483. struct at91_adc_soc_info {
  484. unsigned startup_time;
  485. unsigned min_sample_rate;
  486. unsigned max_sample_rate;
  487. const struct at91_adc_platform *platform;
  488. struct at91_adc_temp_sensor_clb temp_sensor_clb;
  489. };
  490. struct at91_adc_trigger {
  491. char *name;
  492. unsigned int trgmod_value;
  493. unsigned int edge_type;
  494. bool hw_trig;
  495. };
  496. /**
  497. * struct at91_adc_dma - at91-sama5d2 dma information struct
  498. * @dma_chan: the dma channel acquired
  499. * @rx_buf: dma coherent allocated area
  500. * @rx_dma_buf: dma handler for the buffer
  501. * @phys_addr: physical address of the ADC base register
  502. * @buf_idx: index inside the dma buffer where reading was last done
  503. * @rx_buf_sz: size of buffer used by DMA operation
  504. * @watermark: number of conversions to copy before DMA triggers irq
  505. * @dma_ts: hold the start timestamp of dma operation
  506. */
  507. struct at91_adc_dma {
  508. struct dma_chan *dma_chan;
  509. u8 *rx_buf;
  510. dma_addr_t rx_dma_buf;
  511. phys_addr_t phys_addr;
  512. int buf_idx;
  513. int rx_buf_sz;
  514. int watermark;
  515. s64 dma_ts;
  516. };
  517. /**
  518. * struct at91_adc_touch - at91-sama5d2 touchscreen information struct
  519. * @sample_period_val: the value for periodic trigger interval
  520. * @touching: is the pen touching the screen or not
  521. * @x_pos: temporary placeholder for pressure computation
  522. * @channels_bitmask: bitmask with the touchscreen channels enabled
  523. * @workq: workqueue for buffer data pushing
  524. */
  525. struct at91_adc_touch {
  526. u16 sample_period_val;
  527. bool touching;
  528. u16 x_pos;
  529. unsigned long channels_bitmask;
  530. struct work_struct workq;
  531. };
  532. /**
  533. * struct at91_adc_temp - at91-sama5d2 temperature information structure
  534. * @sample_period_val: sample period value
  535. * @saved_sample_rate: saved sample rate
  536. * @saved_oversampling: saved oversampling
  537. */
  538. struct at91_adc_temp {
  539. u16 sample_period_val;
  540. u16 saved_sample_rate;
  541. u16 saved_oversampling;
  542. };
  543. /*
  544. * Buffer size requirements:
  545. * No channels * bytes_per_channel(2) + timestamp bytes (8)
  546. * Divided by 2 because we need half words.
  547. * We assume 32 channels for now, has to be increased if needed.
  548. * Nobody minds a buffer being too big.
  549. */
  550. #define AT91_BUFFER_MAX_HWORDS ((32 * 2 + 8) / 2)
  551. struct at91_adc_state {
  552. void __iomem *base;
  553. int irq;
  554. struct clk *per_clk;
  555. struct regulator *reg;
  556. struct regulator *vref;
  557. int vref_uv;
  558. unsigned int current_sample_rate;
  559. struct iio_trigger *trig;
  560. const struct at91_adc_trigger *selected_trig;
  561. const struct iio_chan_spec *chan;
  562. bool conversion_done;
  563. u32 conversion_value;
  564. unsigned int oversampling_ratio;
  565. struct at91_adc_soc_info soc_info;
  566. wait_queue_head_t wq_data_available;
  567. struct at91_adc_dma dma_st;
  568. struct at91_adc_touch touch_st;
  569. struct at91_adc_temp temp_st;
  570. struct iio_dev *indio_dev;
  571. struct device *dev;
  572. /* Ensure naturally aligned timestamp */
  573. u16 buffer[AT91_BUFFER_MAX_HWORDS] __aligned(8);
  574. /*
  575. * lock to prevent concurrent 'single conversion' requests through
  576. * sysfs.
  577. */
  578. struct mutex lock;
  579. };
  580. static const struct at91_adc_trigger at91_adc_trigger_list[] = {
  581. {
  582. .name = "external_rising",
  583. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE,
  584. .edge_type = IRQ_TYPE_EDGE_RISING,
  585. .hw_trig = true,
  586. },
  587. {
  588. .name = "external_falling",
  589. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL,
  590. .edge_type = IRQ_TYPE_EDGE_FALLING,
  591. .hw_trig = true,
  592. },
  593. {
  594. .name = "external_any",
  595. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY,
  596. .edge_type = IRQ_TYPE_EDGE_BOTH,
  597. .hw_trig = true,
  598. },
  599. {
  600. .name = "software",
  601. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER,
  602. .edge_type = IRQ_TYPE_NONE,
  603. .hw_trig = false,
  604. },
  605. };
  606. static const struct iio_chan_spec at91_sama5d2_adc_channels[] = {
  607. AT91_SAMA5D2_CHAN_SINGLE(0, 0, 0x50),
  608. AT91_SAMA5D2_CHAN_SINGLE(1, 1, 0x54),
  609. AT91_SAMA5D2_CHAN_SINGLE(2, 2, 0x58),
  610. AT91_SAMA5D2_CHAN_SINGLE(3, 3, 0x5c),
  611. AT91_SAMA5D2_CHAN_SINGLE(4, 4, 0x60),
  612. AT91_SAMA5D2_CHAN_SINGLE(5, 5, 0x64),
  613. AT91_SAMA5D2_CHAN_SINGLE(6, 6, 0x68),
  614. AT91_SAMA5D2_CHAN_SINGLE(7, 7, 0x6c),
  615. AT91_SAMA5D2_CHAN_SINGLE(8, 8, 0x70),
  616. AT91_SAMA5D2_CHAN_SINGLE(9, 9, 0x74),
  617. AT91_SAMA5D2_CHAN_SINGLE(10, 10, 0x78),
  618. AT91_SAMA5D2_CHAN_SINGLE(11, 11, 0x7c),
  619. /* original ABI has the differential channels with a gap in between */
  620. AT91_SAMA5D2_CHAN_DIFF(12, 0, 1, 0x50),
  621. AT91_SAMA5D2_CHAN_DIFF(14, 2, 3, 0x58),
  622. AT91_SAMA5D2_CHAN_DIFF(16, 4, 5, 0x60),
  623. AT91_SAMA5D2_CHAN_DIFF(18, 6, 7, 0x68),
  624. AT91_SAMA5D2_CHAN_DIFF(20, 8, 9, 0x70),
  625. AT91_SAMA5D2_CHAN_DIFF(22, 10, 11, 0x78),
  626. IIO_CHAN_SOFT_TIMESTAMP(23),
  627. AT91_SAMA5D2_CHAN_TOUCH(24, "x", IIO_MOD_X),
  628. AT91_SAMA5D2_CHAN_TOUCH(25, "y", IIO_MOD_Y),
  629. AT91_SAMA5D2_CHAN_PRESSURE(26, "pressure"),
  630. };
  631. static const struct iio_chan_spec at91_sama7g5_adc_channels[] = {
  632. AT91_SAMA5D2_CHAN_SINGLE(0, 0, 0x60),
  633. AT91_SAMA5D2_CHAN_SINGLE(1, 1, 0x64),
  634. AT91_SAMA5D2_CHAN_SINGLE(2, 2, 0x68),
  635. AT91_SAMA5D2_CHAN_SINGLE(3, 3, 0x6c),
  636. AT91_SAMA5D2_CHAN_SINGLE(4, 4, 0x70),
  637. AT91_SAMA5D2_CHAN_SINGLE(5, 5, 0x74),
  638. AT91_SAMA5D2_CHAN_SINGLE(6, 6, 0x78),
  639. AT91_SAMA5D2_CHAN_SINGLE(7, 7, 0x7c),
  640. AT91_SAMA5D2_CHAN_SINGLE(8, 8, 0x80),
  641. AT91_SAMA5D2_CHAN_SINGLE(9, 9, 0x84),
  642. AT91_SAMA5D2_CHAN_SINGLE(10, 10, 0x88),
  643. AT91_SAMA5D2_CHAN_SINGLE(11, 11, 0x8c),
  644. AT91_SAMA5D2_CHAN_SINGLE(12, 12, 0x90),
  645. AT91_SAMA5D2_CHAN_SINGLE(13, 13, 0x94),
  646. AT91_SAMA5D2_CHAN_SINGLE(14, 14, 0x98),
  647. AT91_SAMA5D2_CHAN_SINGLE(15, 15, 0x9c),
  648. AT91_SAMA5D2_CHAN_DIFF(16, 0, 1, 0x60),
  649. AT91_SAMA5D2_CHAN_DIFF(17, 2, 3, 0x68),
  650. AT91_SAMA5D2_CHAN_DIFF(18, 4, 5, 0x70),
  651. AT91_SAMA5D2_CHAN_DIFF(19, 6, 7, 0x78),
  652. AT91_SAMA5D2_CHAN_DIFF(20, 8, 9, 0x80),
  653. AT91_SAMA5D2_CHAN_DIFF(21, 10, 11, 0x88),
  654. AT91_SAMA5D2_CHAN_DIFF(22, 12, 13, 0x90),
  655. AT91_SAMA5D2_CHAN_DIFF(23, 14, 15, 0x98),
  656. IIO_CHAN_SOFT_TIMESTAMP(24),
  657. AT91_SAMA5D2_CHAN_TEMP(AT91_SAMA7G5_ADC_TEMP_CHANNEL, "temp", 0xdc),
  658. };
  659. static const struct at91_adc_platform sama5d2_platform = {
  660. .layout = &sama5d2_layout,
  661. .adc_channels = &at91_sama5d2_adc_channels,
  662. #define AT91_SAMA5D2_SINGLE_CHAN_CNT 12
  663. #define AT91_SAMA5D2_DIFF_CHAN_CNT 6
  664. .nr_channels = AT91_SAMA5D2_SINGLE_CHAN_CNT +
  665. AT91_SAMA5D2_DIFF_CHAN_CNT,
  666. #define AT91_SAMA5D2_TOUCH_X_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \
  667. AT91_SAMA5D2_DIFF_CHAN_CNT * 2)
  668. .touch_chan_x = AT91_SAMA5D2_TOUCH_X_CHAN_IDX,
  669. #define AT91_SAMA5D2_TOUCH_Y_CHAN_IDX (AT91_SAMA5D2_TOUCH_X_CHAN_IDX + 1)
  670. .touch_chan_y = AT91_SAMA5D2_TOUCH_Y_CHAN_IDX,
  671. #define AT91_SAMA5D2_TOUCH_P_CHAN_IDX (AT91_SAMA5D2_TOUCH_Y_CHAN_IDX + 1)
  672. .touch_chan_p = AT91_SAMA5D2_TOUCH_P_CHAN_IDX,
  673. #define AT91_SAMA5D2_MAX_CHAN_IDX AT91_SAMA5D2_TOUCH_P_CHAN_IDX
  674. .max_channels = ARRAY_SIZE(at91_sama5d2_adc_channels),
  675. .max_index = AT91_SAMA5D2_MAX_CHAN_IDX,
  676. #define AT91_SAMA5D2_HW_TRIG_CNT 3
  677. .hw_trig_cnt = AT91_SAMA5D2_HW_TRIG_CNT,
  678. .osr_mask = GENMASK(17, 16),
  679. .oversampling_avail = { 1, 4, 16, },
  680. .oversampling_avail_no = 3,
  681. .chan_realbits = 14,
  682. };
  683. static const struct at91_adc_platform sama7g5_platform = {
  684. .layout = &sama7g5_layout,
  685. .adc_channels = &at91_sama7g5_adc_channels,
  686. #define AT91_SAMA7G5_SINGLE_CHAN_CNT 16
  687. #define AT91_SAMA7G5_DIFF_CHAN_CNT 8
  688. #define AT91_SAMA7G5_TEMP_CHAN_CNT 1
  689. .nr_channels = AT91_SAMA7G5_SINGLE_CHAN_CNT +
  690. AT91_SAMA7G5_DIFF_CHAN_CNT +
  691. AT91_SAMA7G5_TEMP_CHAN_CNT,
  692. #define AT91_SAMA7G5_MAX_CHAN_IDX (AT91_SAMA7G5_SINGLE_CHAN_CNT + \
  693. AT91_SAMA7G5_DIFF_CHAN_CNT + \
  694. AT91_SAMA7G5_TEMP_CHAN_CNT)
  695. .max_channels = ARRAY_SIZE(at91_sama7g5_adc_channels),
  696. .max_index = AT91_SAMA7G5_MAX_CHAN_IDX,
  697. #define AT91_SAMA7G5_HW_TRIG_CNT 3
  698. .hw_trig_cnt = AT91_SAMA7G5_HW_TRIG_CNT,
  699. .osr_mask = GENMASK(18, 16),
  700. .oversampling_avail = { 1, 4, 16, 64, 256, },
  701. .oversampling_avail_no = 5,
  702. .chan_realbits = 16,
  703. .temp_sensor = true,
  704. .temp_chan = AT91_SAMA7G5_ADC_TEMP_CHANNEL,
  705. };
  706. static int at91_adc_chan_xlate(struct iio_dev *indio_dev, int chan)
  707. {
  708. int i;
  709. for (i = 0; i < indio_dev->num_channels; i++) {
  710. if (indio_dev->channels[i].scan_index == chan)
  711. return i;
  712. }
  713. return -EINVAL;
  714. }
  715. static inline struct iio_chan_spec const *
  716. at91_adc_chan_get(struct iio_dev *indio_dev, int chan)
  717. {
  718. int index = at91_adc_chan_xlate(indio_dev, chan);
  719. if (index < 0)
  720. return NULL;
  721. return indio_dev->channels + index;
  722. }
  723. static inline int at91_adc_fwnode_xlate(struct iio_dev *indio_dev,
  724. const struct fwnode_reference_args *iiospec)
  725. {
  726. return at91_adc_chan_xlate(indio_dev, iiospec->args[0]);
  727. }
  728. static unsigned int at91_adc_active_scan_mask_to_reg(struct iio_dev *indio_dev)
  729. {
  730. u32 mask = 0;
  731. u8 bit;
  732. struct at91_adc_state *st = iio_priv(indio_dev);
  733. for_each_set_bit(bit, indio_dev->active_scan_mask,
  734. indio_dev->num_channels) {
  735. struct iio_chan_spec const *chan =
  736. at91_adc_chan_get(indio_dev, bit);
  737. mask |= BIT(chan->channel);
  738. }
  739. return mask & GENMASK(st->soc_info.platform->nr_channels, 0);
  740. }
  741. static void at91_adc_cor(struct at91_adc_state *st,
  742. struct iio_chan_spec const *chan)
  743. {
  744. u32 cor, cur_cor;
  745. cor = BIT(chan->channel) | BIT(chan->channel2);
  746. cur_cor = at91_adc_readl(st, COR);
  747. cor <<= st->soc_info.platform->layout->COR_diff_offset;
  748. if (chan->differential)
  749. at91_adc_writel(st, COR, cur_cor | cor);
  750. else
  751. at91_adc_writel(st, COR, cur_cor & ~cor);
  752. }
  753. static void at91_adc_irq_status(struct at91_adc_state *st, u32 *status,
  754. u32 *eoc)
  755. {
  756. *status = at91_adc_readl(st, ISR);
  757. if (st->soc_info.platform->layout->EOC_ISR)
  758. *eoc = at91_adc_readl(st, EOC_ISR);
  759. else
  760. *eoc = *status;
  761. }
  762. static void at91_adc_irq_mask(struct at91_adc_state *st, u32 *status, u32 *eoc)
  763. {
  764. *status = at91_adc_readl(st, IMR);
  765. if (st->soc_info.platform->layout->EOC_IMR)
  766. *eoc = at91_adc_readl(st, EOC_IMR);
  767. else
  768. *eoc = *status;
  769. }
  770. static void at91_adc_eoc_dis(struct at91_adc_state *st, unsigned int channel)
  771. {
  772. /*
  773. * On some products having the EOC bits in a separate register,
  774. * errata recommends not writing this register (EOC_IDR).
  775. * On products having the EOC bits in the IDR register, it's fine to write it.
  776. */
  777. if (!st->soc_info.platform->layout->EOC_IDR)
  778. at91_adc_writel(st, IDR, BIT(channel));
  779. }
  780. static void at91_adc_eoc_ena(struct at91_adc_state *st, unsigned int channel)
  781. {
  782. if (!st->soc_info.platform->layout->EOC_IDR)
  783. at91_adc_writel(st, IER, BIT(channel));
  784. else
  785. at91_adc_writel(st, EOC_IER, BIT(channel));
  786. }
  787. static int at91_adc_config_emr(struct at91_adc_state *st,
  788. u32 oversampling_ratio, u32 trackx)
  789. {
  790. /* configure the extended mode register */
  791. unsigned int emr, osr;
  792. unsigned int osr_mask = st->soc_info.platform->osr_mask;
  793. int i, ret;
  794. /* Check against supported oversampling values. */
  795. for (i = 0; i < st->soc_info.platform->oversampling_avail_no; i++) {
  796. if (oversampling_ratio == st->soc_info.platform->oversampling_avail[i])
  797. break;
  798. }
  799. if (i == st->soc_info.platform->oversampling_avail_no)
  800. return -EINVAL;
  801. /* select oversampling ratio from configuration */
  802. switch (oversampling_ratio) {
  803. case 1:
  804. osr = AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_1SAMPLES,
  805. osr_mask);
  806. break;
  807. case 4:
  808. osr = AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_4SAMPLES,
  809. osr_mask);
  810. break;
  811. case 16:
  812. osr = AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_16SAMPLES,
  813. osr_mask);
  814. break;
  815. case 64:
  816. osr = AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_64SAMPLES,
  817. osr_mask);
  818. break;
  819. case 256:
  820. osr = AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_256SAMPLES,
  821. osr_mask);
  822. break;
  823. }
  824. ret = pm_runtime_resume_and_get(st->dev);
  825. if (ret < 0)
  826. return ret;
  827. emr = at91_adc_readl(st, EMR);
  828. /* select oversampling per single trigger event */
  829. emr |= AT91_SAMA5D2_EMR_ASTE(1);
  830. /* delete leftover content if it's the case */
  831. emr &= ~(osr_mask | AT91_SAMA5D2_TRACKX_MASK);
  832. /* Update osr and trackx. */
  833. emr |= osr | AT91_SAMA5D2_TRACKX(trackx);
  834. at91_adc_writel(st, EMR, emr);
  835. pm_runtime_mark_last_busy(st->dev);
  836. pm_runtime_put_autosuspend(st->dev);
  837. st->oversampling_ratio = oversampling_ratio;
  838. return 0;
  839. }
  840. static int at91_adc_adjust_val_osr(struct at91_adc_state *st, int *val)
  841. {
  842. int nbits, diff;
  843. if (st->oversampling_ratio == 1)
  844. nbits = 12;
  845. else if (st->oversampling_ratio == 4)
  846. nbits = 13;
  847. else if (st->oversampling_ratio == 16)
  848. nbits = 14;
  849. else if (st->oversampling_ratio == 64)
  850. nbits = 15;
  851. else if (st->oversampling_ratio == 256)
  852. nbits = 16;
  853. else
  854. /* Should not happen. */
  855. return -EINVAL;
  856. /*
  857. * We have nbits of real data and channel is registered as
  858. * st->soc_info.platform->chan_realbits, so shift left diff bits.
  859. */
  860. diff = st->soc_info.platform->chan_realbits - nbits;
  861. *val <<= diff;
  862. return IIO_VAL_INT;
  863. }
  864. static void at91_adc_adjust_val_osr_array(struct at91_adc_state *st, void *buf,
  865. int len)
  866. {
  867. int i = 0, val;
  868. u16 *buf_u16 = (u16 *) buf;
  869. /*
  870. * We are converting each two bytes (each sample).
  871. * First convert the byte based array to u16, and convert each sample
  872. * separately.
  873. * Each value is two bytes in an array of chars, so to not shift
  874. * more than we need, save the value separately.
  875. * len is in bytes, so divide by two to get number of samples.
  876. */
  877. while (i < len / 2) {
  878. val = buf_u16[i];
  879. at91_adc_adjust_val_osr(st, &val);
  880. buf_u16[i] = val;
  881. i++;
  882. }
  883. }
  884. static int at91_adc_configure_touch(struct at91_adc_state *st, bool state)
  885. {
  886. u32 clk_khz = st->current_sample_rate / 1000;
  887. int i = 0, ret;
  888. u16 pendbc;
  889. u32 tsmr, acr;
  890. if (state) {
  891. ret = pm_runtime_resume_and_get(st->dev);
  892. if (ret < 0)
  893. return ret;
  894. } else {
  895. /* disabling touch IRQs and setting mode to no touch enabled */
  896. at91_adc_writel(st, IDR,
  897. AT91_SAMA5D2_IER_PEN | AT91_SAMA5D2_IER_NOPEN);
  898. at91_adc_writel(st, TSMR, 0);
  899. pm_runtime_mark_last_busy(st->dev);
  900. pm_runtime_put_autosuspend(st->dev);
  901. return 0;
  902. }
  903. /*
  904. * debounce time is in microseconds, we need it in milliseconds to
  905. * multiply with kilohertz, so, divide by 1000, but after the multiply.
  906. * round up to make sure pendbc is at least 1
  907. */
  908. pendbc = round_up(AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US *
  909. clk_khz / 1000, 1);
  910. /* get the required exponent */
  911. while (pendbc >> i++)
  912. ;
  913. pendbc = i;
  914. tsmr = AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS;
  915. tsmr |= AT91_SAMA5D2_TSMR_TSAV(2) & AT91_SAMA5D2_TSMR_TSAV_MASK;
  916. tsmr |= AT91_SAMA5D2_TSMR_PENDBC(pendbc) &
  917. AT91_SAMA5D2_TSMR_PENDBC_MASK;
  918. tsmr |= AT91_SAMA5D2_TSMR_NOTSDMA;
  919. tsmr |= AT91_SAMA5D2_TSMR_PENDET_ENA;
  920. tsmr |= AT91_SAMA5D2_TSMR_TSFREQ(2) & AT91_SAMA5D2_TSMR_TSFREQ_MASK;
  921. at91_adc_writel(st, TSMR, tsmr);
  922. acr = at91_adc_readl(st, ACR);
  923. acr &= ~AT91_SAMA5D2_ACR_PENDETSENS_MASK;
  924. acr |= 0x02 & AT91_SAMA5D2_ACR_PENDETSENS_MASK;
  925. at91_adc_writel(st, ACR, acr);
  926. /* Sample Period Time = (TRGPER + 1) / ADCClock */
  927. st->touch_st.sample_period_val =
  928. round_up((AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US *
  929. clk_khz / 1000) - 1, 1);
  930. /* enable pen detect IRQ */
  931. at91_adc_writel(st, IER, AT91_SAMA5D2_IER_PEN);
  932. return 0;
  933. }
  934. static u16 at91_adc_touch_pos(struct at91_adc_state *st, int reg)
  935. {
  936. u32 val = 0;
  937. u32 scale, result, pos;
  938. /*
  939. * to obtain the actual position we must divide by scale
  940. * and multiply with max, where
  941. * max = 2^AT91_SAMA5D2_MAX_POS_BITS - 1
  942. */
  943. /* first half of register is the x or y, second half is the scale */
  944. if (reg == st->soc_info.platform->layout->XPOSR)
  945. val = at91_adc_readl(st, XPOSR);
  946. else if (reg == st->soc_info.platform->layout->YPOSR)
  947. val = at91_adc_readl(st, YPOSR);
  948. if (!val)
  949. dev_dbg(&st->indio_dev->dev, "pos is 0\n");
  950. pos = val & AT91_SAMA5D2_XYZ_MASK;
  951. result = (pos << AT91_SAMA5D2_MAX_POS_BITS) - pos;
  952. scale = (val >> 16) & AT91_SAMA5D2_XYZ_MASK;
  953. if (scale == 0) {
  954. dev_err(&st->indio_dev->dev, "scale is 0\n");
  955. return 0;
  956. }
  957. result /= scale;
  958. return result;
  959. }
  960. static u16 at91_adc_touch_x_pos(struct at91_adc_state *st)
  961. {
  962. st->touch_st.x_pos = at91_adc_touch_pos(st, st->soc_info.platform->layout->XPOSR);
  963. return st->touch_st.x_pos;
  964. }
  965. static u16 at91_adc_touch_y_pos(struct at91_adc_state *st)
  966. {
  967. return at91_adc_touch_pos(st, st->soc_info.platform->layout->YPOSR);
  968. }
  969. static u16 at91_adc_touch_pressure(struct at91_adc_state *st)
  970. {
  971. u32 val;
  972. u32 z1, z2;
  973. u32 pres;
  974. u32 rxp = 1;
  975. u32 factor = 1000;
  976. /* calculate the pressure */
  977. val = at91_adc_readl(st, PRESSR);
  978. z1 = val & AT91_SAMA5D2_XYZ_MASK;
  979. z2 = (val >> 16) & AT91_SAMA5D2_XYZ_MASK;
  980. if (z1 != 0)
  981. pres = rxp * (st->touch_st.x_pos * factor / 1024) *
  982. (z2 * factor / z1 - factor) /
  983. factor;
  984. else
  985. pres = 0xFFFF; /* no pen contact */
  986. /*
  987. * The pressure from device grows down, minimum is 0xFFFF, maximum 0x0.
  988. * We compute it this way, but let's return it in the expected way,
  989. * growing from 0 to 0xFFFF.
  990. */
  991. return 0xFFFF - pres;
  992. }
  993. static int at91_adc_read_position(struct at91_adc_state *st, int chan, u16 *val)
  994. {
  995. *val = 0;
  996. if (!st->touch_st.touching)
  997. return -ENODATA;
  998. if (chan == st->soc_info.platform->touch_chan_x)
  999. *val = at91_adc_touch_x_pos(st);
  1000. else if (chan == st->soc_info.platform->touch_chan_y)
  1001. *val = at91_adc_touch_y_pos(st);
  1002. else
  1003. return -ENODATA;
  1004. return IIO_VAL_INT;
  1005. }
  1006. static int at91_adc_read_pressure(struct at91_adc_state *st, int chan, u16 *val)
  1007. {
  1008. *val = 0;
  1009. if (!st->touch_st.touching)
  1010. return -ENODATA;
  1011. if (chan == st->soc_info.platform->touch_chan_p)
  1012. *val = at91_adc_touch_pressure(st);
  1013. else
  1014. return -ENODATA;
  1015. return IIO_VAL_INT;
  1016. }
  1017. static void at91_adc_configure_trigger_registers(struct at91_adc_state *st,
  1018. bool state)
  1019. {
  1020. u32 status = at91_adc_readl(st, TRGR);
  1021. /* clear TRGMOD */
  1022. status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK;
  1023. if (state)
  1024. status |= st->selected_trig->trgmod_value;
  1025. /* set/unset hw trigger */
  1026. at91_adc_writel(st, TRGR, status);
  1027. }
  1028. static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
  1029. {
  1030. struct iio_dev *indio = iio_trigger_get_drvdata(trig);
  1031. struct at91_adc_state *st = iio_priv(indio);
  1032. int ret;
  1033. if (state) {
  1034. ret = pm_runtime_resume_and_get(st->dev);
  1035. if (ret < 0)
  1036. return ret;
  1037. }
  1038. at91_adc_configure_trigger_registers(st, state);
  1039. if (!state) {
  1040. pm_runtime_mark_last_busy(st->dev);
  1041. pm_runtime_put_autosuspend(st->dev);
  1042. }
  1043. return 0;
  1044. }
  1045. static void at91_adc_reenable_trigger(struct iio_trigger *trig)
  1046. {
  1047. struct iio_dev *indio = iio_trigger_get_drvdata(trig);
  1048. struct at91_adc_state *st = iio_priv(indio);
  1049. /* if we are using DMA, we must not reenable irq after each trigger */
  1050. if (st->dma_st.dma_chan)
  1051. return;
  1052. enable_irq(st->irq);
  1053. /* Needed to ACK the DRDY interruption */
  1054. at91_adc_readl(st, LCDR);
  1055. }
  1056. static const struct iio_trigger_ops at91_adc_trigger_ops = {
  1057. .set_trigger_state = &at91_adc_configure_trigger,
  1058. .reenable = &at91_adc_reenable_trigger,
  1059. .validate_device = iio_trigger_validate_own_device,
  1060. };
  1061. static int at91_adc_dma_size_done(struct at91_adc_state *st)
  1062. {
  1063. struct dma_tx_state state;
  1064. enum dma_status status;
  1065. int i, size;
  1066. status = dmaengine_tx_status(st->dma_st.dma_chan,
  1067. st->dma_st.dma_chan->cookie,
  1068. &state);
  1069. if (status != DMA_IN_PROGRESS)
  1070. return 0;
  1071. /* Transferred length is size in bytes from end of buffer */
  1072. i = st->dma_st.rx_buf_sz - state.residue;
  1073. /* Return available bytes */
  1074. if (i >= st->dma_st.buf_idx)
  1075. size = i - st->dma_st.buf_idx;
  1076. else
  1077. size = st->dma_st.rx_buf_sz + i - st->dma_st.buf_idx;
  1078. return size;
  1079. }
  1080. static void at91_dma_buffer_done(void *data)
  1081. {
  1082. struct iio_dev *indio_dev = data;
  1083. iio_trigger_poll_chained(indio_dev->trig);
  1084. }
  1085. static int at91_adc_dma_start(struct iio_dev *indio_dev)
  1086. {
  1087. struct at91_adc_state *st = iio_priv(indio_dev);
  1088. struct dma_async_tx_descriptor *desc;
  1089. dma_cookie_t cookie;
  1090. int ret;
  1091. u8 bit;
  1092. if (!st->dma_st.dma_chan)
  1093. return 0;
  1094. /* we start a new DMA, so set buffer index to start */
  1095. st->dma_st.buf_idx = 0;
  1096. /*
  1097. * compute buffer size w.r.t. watermark and enabled channels.
  1098. * scan_bytes is aligned so we need an exact size for DMA
  1099. */
  1100. st->dma_st.rx_buf_sz = 0;
  1101. for_each_set_bit(bit, indio_dev->active_scan_mask,
  1102. indio_dev->num_channels) {
  1103. struct iio_chan_spec const *chan =
  1104. at91_adc_chan_get(indio_dev, bit);
  1105. if (!chan)
  1106. continue;
  1107. st->dma_st.rx_buf_sz += chan->scan_type.storagebits / 8;
  1108. }
  1109. st->dma_st.rx_buf_sz *= st->dma_st.watermark;
  1110. /* Prepare a DMA cyclic transaction */
  1111. desc = dmaengine_prep_dma_cyclic(st->dma_st.dma_chan,
  1112. st->dma_st.rx_dma_buf,
  1113. st->dma_st.rx_buf_sz,
  1114. st->dma_st.rx_buf_sz / 2,
  1115. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
  1116. if (!desc) {
  1117. dev_err(&indio_dev->dev, "cannot prepare DMA cyclic\n");
  1118. return -EBUSY;
  1119. }
  1120. desc->callback = at91_dma_buffer_done;
  1121. desc->callback_param = indio_dev;
  1122. cookie = dmaengine_submit(desc);
  1123. ret = dma_submit_error(cookie);
  1124. if (ret) {
  1125. dev_err(&indio_dev->dev, "cannot submit DMA cyclic\n");
  1126. dmaengine_terminate_async(st->dma_st.dma_chan);
  1127. return ret;
  1128. }
  1129. /* enable general overrun error signaling */
  1130. at91_adc_writel(st, IER, AT91_SAMA5D2_IER_GOVRE);
  1131. /* Issue pending DMA requests */
  1132. dma_async_issue_pending(st->dma_st.dma_chan);
  1133. /* consider current time as DMA start time for timestamps */
  1134. st->dma_st.dma_ts = iio_get_time_ns(indio_dev);
  1135. dev_dbg(&indio_dev->dev, "DMA cyclic started\n");
  1136. return 0;
  1137. }
  1138. static bool at91_adc_buffer_check_use_irq(struct iio_dev *indio,
  1139. struct at91_adc_state *st)
  1140. {
  1141. /* if using DMA, we do not use our own IRQ (we use DMA-controller) */
  1142. if (st->dma_st.dma_chan)
  1143. return false;
  1144. /* if the trigger is not ours, then it has its own IRQ */
  1145. if (iio_trigger_validate_own_device(indio->trig, indio))
  1146. return false;
  1147. return true;
  1148. }
  1149. static bool at91_adc_current_chan_is_touch(struct iio_dev *indio_dev)
  1150. {
  1151. struct at91_adc_state *st = iio_priv(indio_dev);
  1152. return !!bitmap_subset(indio_dev->active_scan_mask,
  1153. &st->touch_st.channels_bitmask,
  1154. st->soc_info.platform->max_index + 1);
  1155. }
  1156. static int at91_adc_buffer_prepare(struct iio_dev *indio_dev)
  1157. {
  1158. int ret;
  1159. u8 bit;
  1160. struct at91_adc_state *st = iio_priv(indio_dev);
  1161. /* check if we are enabling triggered buffer or the touchscreen */
  1162. if (at91_adc_current_chan_is_touch(indio_dev))
  1163. return at91_adc_configure_touch(st, true);
  1164. /* if we are not in triggered mode, we cannot enable the buffer. */
  1165. if (!(iio_device_get_current_mode(indio_dev) & INDIO_ALL_TRIGGERED_MODES))
  1166. return -EINVAL;
  1167. ret = pm_runtime_resume_and_get(st->dev);
  1168. if (ret < 0)
  1169. return ret;
  1170. /* we continue with the triggered buffer */
  1171. ret = at91_adc_dma_start(indio_dev);
  1172. if (ret) {
  1173. dev_err(&indio_dev->dev, "buffer prepare failed\n");
  1174. goto pm_runtime_put;
  1175. }
  1176. for_each_set_bit(bit, indio_dev->active_scan_mask,
  1177. indio_dev->num_channels) {
  1178. struct iio_chan_spec const *chan =
  1179. at91_adc_chan_get(indio_dev, bit);
  1180. if (!chan)
  1181. continue;
  1182. /* these channel types cannot be handled by this trigger */
  1183. if (chan->type == IIO_POSITIONRELATIVE ||
  1184. chan->type == IIO_PRESSURE ||
  1185. chan->type == IIO_TEMP)
  1186. continue;
  1187. at91_adc_cor(st, chan);
  1188. at91_adc_writel(st, CHER, BIT(chan->channel));
  1189. }
  1190. if (at91_adc_buffer_check_use_irq(indio_dev, st))
  1191. at91_adc_writel(st, IER, AT91_SAMA5D2_IER_DRDY);
  1192. pm_runtime_put:
  1193. pm_runtime_mark_last_busy(st->dev);
  1194. pm_runtime_put_autosuspend(st->dev);
  1195. return ret;
  1196. }
  1197. static int at91_adc_buffer_postdisable(struct iio_dev *indio_dev)
  1198. {
  1199. struct at91_adc_state *st = iio_priv(indio_dev);
  1200. int ret;
  1201. u8 bit;
  1202. /* check if we are disabling triggered buffer or the touchscreen */
  1203. if (at91_adc_current_chan_is_touch(indio_dev))
  1204. return at91_adc_configure_touch(st, false);
  1205. /* if we are not in triggered mode, nothing to do here */
  1206. if (!(iio_device_get_current_mode(indio_dev) & INDIO_ALL_TRIGGERED_MODES))
  1207. return -EINVAL;
  1208. ret = pm_runtime_resume_and_get(st->dev);
  1209. if (ret < 0)
  1210. return ret;
  1211. /*
  1212. * For each enable channel we must disable it in hardware.
  1213. * In the case of DMA, we must read the last converted value
  1214. * to clear EOC status and not get a possible interrupt later.
  1215. * This value is being read by DMA from LCDR anyway, so it's not lost.
  1216. */
  1217. for_each_set_bit(bit, indio_dev->active_scan_mask,
  1218. indio_dev->num_channels) {
  1219. struct iio_chan_spec const *chan =
  1220. at91_adc_chan_get(indio_dev, bit);
  1221. if (!chan)
  1222. continue;
  1223. /* these channel types are virtual, no need to do anything */
  1224. if (chan->type == IIO_POSITIONRELATIVE ||
  1225. chan->type == IIO_PRESSURE ||
  1226. chan->type == IIO_TEMP)
  1227. continue;
  1228. at91_adc_writel(st, CHDR, BIT(chan->channel));
  1229. if (st->dma_st.dma_chan)
  1230. at91_adc_read_chan(st, chan->address);
  1231. }
  1232. if (at91_adc_buffer_check_use_irq(indio_dev, st))
  1233. at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_DRDY);
  1234. /* read overflow register to clear possible overflow status */
  1235. at91_adc_readl(st, OVER);
  1236. /* if we are using DMA we must clear registers and end DMA */
  1237. if (st->dma_st.dma_chan)
  1238. dmaengine_terminate_sync(st->dma_st.dma_chan);
  1239. pm_runtime_mark_last_busy(st->dev);
  1240. pm_runtime_put_autosuspend(st->dev);
  1241. return 0;
  1242. }
  1243. static const struct iio_buffer_setup_ops at91_buffer_setup_ops = {
  1244. .postdisable = &at91_adc_buffer_postdisable,
  1245. };
  1246. static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *indio,
  1247. char *trigger_name)
  1248. {
  1249. struct iio_trigger *trig;
  1250. int ret;
  1251. trig = devm_iio_trigger_alloc(&indio->dev, "%s-dev%d-%s", indio->name,
  1252. iio_device_id(indio), trigger_name);
  1253. if (!trig)
  1254. return ERR_PTR(-ENOMEM);
  1255. trig->dev.parent = indio->dev.parent;
  1256. iio_trigger_set_drvdata(trig, indio);
  1257. trig->ops = &at91_adc_trigger_ops;
  1258. ret = devm_iio_trigger_register(&indio->dev, trig);
  1259. if (ret)
  1260. return ERR_PTR(ret);
  1261. return trig;
  1262. }
  1263. static void at91_adc_trigger_handler_nodma(struct iio_dev *indio_dev,
  1264. struct iio_poll_func *pf)
  1265. {
  1266. struct at91_adc_state *st = iio_priv(indio_dev);
  1267. int i = 0;
  1268. int val;
  1269. u8 bit;
  1270. u32 mask = at91_adc_active_scan_mask_to_reg(indio_dev);
  1271. unsigned int timeout = 50;
  1272. u32 status, imr, eoc = 0, eoc_imr;
  1273. /*
  1274. * Check if the conversion is ready. If not, wait a little bit, and
  1275. * in case of timeout exit with an error.
  1276. */
  1277. while (((eoc & mask) != mask) && timeout) {
  1278. at91_adc_irq_status(st, &status, &eoc);
  1279. at91_adc_irq_mask(st, &imr, &eoc_imr);
  1280. usleep_range(50, 100);
  1281. timeout--;
  1282. }
  1283. /* Cannot read data, not ready. Continue without reporting data */
  1284. if (!timeout)
  1285. return;
  1286. for_each_set_bit(bit, indio_dev->active_scan_mask,
  1287. indio_dev->num_channels) {
  1288. struct iio_chan_spec const *chan =
  1289. at91_adc_chan_get(indio_dev, bit);
  1290. if (!chan)
  1291. continue;
  1292. /*
  1293. * Our external trigger only supports the voltage channels.
  1294. * In case someone requested a different type of channel
  1295. * just put zeroes to buffer.
  1296. * This should not happen because we check the scan mode
  1297. * and scan mask when we enable the buffer, and we don't allow
  1298. * the buffer to start with a mixed mask (voltage and something
  1299. * else).
  1300. * Thus, emit a warning.
  1301. */
  1302. if (chan->type == IIO_VOLTAGE) {
  1303. val = at91_adc_read_chan(st, chan->address);
  1304. at91_adc_adjust_val_osr(st, &val);
  1305. st->buffer[i] = val;
  1306. } else {
  1307. st->buffer[i] = 0;
  1308. WARN(true, "This trigger cannot handle this type of channel");
  1309. }
  1310. i++;
  1311. }
  1312. iio_push_to_buffers_with_timestamp(indio_dev, st->buffer,
  1313. pf->timestamp);
  1314. }
  1315. static void at91_adc_trigger_handler_dma(struct iio_dev *indio_dev)
  1316. {
  1317. struct at91_adc_state *st = iio_priv(indio_dev);
  1318. int transferred_len = at91_adc_dma_size_done(st);
  1319. s64 ns = iio_get_time_ns(indio_dev);
  1320. s64 interval;
  1321. int sample_index = 0, sample_count, sample_size;
  1322. u32 status = at91_adc_readl(st, ISR);
  1323. /* if we reached this point, we cannot sample faster */
  1324. if (status & AT91_SAMA5D2_IER_GOVRE)
  1325. pr_info_ratelimited("%s: conversion overrun detected\n",
  1326. indio_dev->name);
  1327. sample_size = div_s64(st->dma_st.rx_buf_sz, st->dma_st.watermark);
  1328. sample_count = div_s64(transferred_len, sample_size);
  1329. /*
  1330. * interval between samples is total time since last transfer handling
  1331. * divided by the number of samples (total size divided by sample size)
  1332. */
  1333. interval = div_s64((ns - st->dma_st.dma_ts), sample_count);
  1334. while (transferred_len >= sample_size) {
  1335. /*
  1336. * for all the values in the current sample,
  1337. * adjust the values inside the buffer for oversampling
  1338. */
  1339. at91_adc_adjust_val_osr_array(st,
  1340. &st->dma_st.rx_buf[st->dma_st.buf_idx],
  1341. sample_size);
  1342. iio_push_to_buffers_with_timestamp(indio_dev,
  1343. (st->dma_st.rx_buf + st->dma_st.buf_idx),
  1344. (st->dma_st.dma_ts + interval * sample_index));
  1345. /* adjust remaining length */
  1346. transferred_len -= sample_size;
  1347. /* adjust buffer index */
  1348. st->dma_st.buf_idx += sample_size;
  1349. /* in case of reaching end of buffer, reset index */
  1350. if (st->dma_st.buf_idx >= st->dma_st.rx_buf_sz)
  1351. st->dma_st.buf_idx = 0;
  1352. sample_index++;
  1353. }
  1354. /* adjust saved time for next transfer handling */
  1355. st->dma_st.dma_ts = iio_get_time_ns(indio_dev);
  1356. }
  1357. static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
  1358. {
  1359. struct iio_poll_func *pf = p;
  1360. struct iio_dev *indio_dev = pf->indio_dev;
  1361. struct at91_adc_state *st = iio_priv(indio_dev);
  1362. /*
  1363. * If it's not our trigger, start a conversion now, as we are
  1364. * actually polling the trigger now.
  1365. */
  1366. if (iio_trigger_validate_own_device(indio_dev->trig, indio_dev))
  1367. at91_adc_writel(st, CR, AT91_SAMA5D2_CR_START);
  1368. if (st->dma_st.dma_chan)
  1369. at91_adc_trigger_handler_dma(indio_dev);
  1370. else
  1371. at91_adc_trigger_handler_nodma(indio_dev, pf);
  1372. iio_trigger_notify_done(indio_dev->trig);
  1373. return IRQ_HANDLED;
  1374. }
  1375. static unsigned at91_adc_startup_time(unsigned startup_time_min,
  1376. unsigned adc_clk_khz)
  1377. {
  1378. static const unsigned int startup_lookup[] = {
  1379. 0, 8, 16, 24,
  1380. 64, 80, 96, 112,
  1381. 512, 576, 640, 704,
  1382. 768, 832, 896, 960
  1383. };
  1384. unsigned ticks_min, i;
  1385. /*
  1386. * Since the adc frequency is checked before, there is no reason
  1387. * to not meet the startup time constraint.
  1388. */
  1389. ticks_min = startup_time_min * adc_clk_khz / 1000;
  1390. for (i = 0; i < ARRAY_SIZE(startup_lookup); i++)
  1391. if (startup_lookup[i] > ticks_min)
  1392. break;
  1393. return i;
  1394. }
  1395. static void at91_adc_setup_samp_freq(struct iio_dev *indio_dev, unsigned freq,
  1396. unsigned int startup_time,
  1397. unsigned int tracktim)
  1398. {
  1399. struct at91_adc_state *st = iio_priv(indio_dev);
  1400. unsigned f_per, prescal, startup, mr;
  1401. int ret;
  1402. f_per = clk_get_rate(st->per_clk);
  1403. prescal = (f_per / (2 * freq)) - 1;
  1404. startup = at91_adc_startup_time(startup_time, freq / 1000);
  1405. ret = pm_runtime_resume_and_get(st->dev);
  1406. if (ret < 0)
  1407. return;
  1408. mr = at91_adc_readl(st, MR);
  1409. mr &= ~(AT91_SAMA5D2_MR_STARTUP_MASK | AT91_SAMA5D2_MR_PRESCAL_MASK);
  1410. mr |= AT91_SAMA5D2_MR_STARTUP(startup);
  1411. mr |= AT91_SAMA5D2_MR_PRESCAL(prescal);
  1412. mr |= AT91_SAMA5D2_MR_TRACKTIM(tracktim);
  1413. at91_adc_writel(st, MR, mr);
  1414. pm_runtime_mark_last_busy(st->dev);
  1415. pm_runtime_put_autosuspend(st->dev);
  1416. dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u, tracktim=%u\n",
  1417. freq, startup, prescal, tracktim);
  1418. st->current_sample_rate = freq;
  1419. }
  1420. static inline unsigned at91_adc_get_sample_freq(struct at91_adc_state *st)
  1421. {
  1422. return st->current_sample_rate;
  1423. }
  1424. static void at91_adc_touch_data_handler(struct iio_dev *indio_dev)
  1425. {
  1426. struct at91_adc_state *st = iio_priv(indio_dev);
  1427. u8 bit;
  1428. u16 val;
  1429. int i = 0;
  1430. for_each_set_bit(bit, indio_dev->active_scan_mask,
  1431. st->soc_info.platform->max_index + 1) {
  1432. struct iio_chan_spec const *chan =
  1433. at91_adc_chan_get(indio_dev, bit);
  1434. if (chan->type == IIO_POSITIONRELATIVE)
  1435. at91_adc_read_position(st, chan->channel, &val);
  1436. else if (chan->type == IIO_PRESSURE)
  1437. at91_adc_read_pressure(st, chan->channel, &val);
  1438. else
  1439. continue;
  1440. st->buffer[i] = val;
  1441. i++;
  1442. }
  1443. /*
  1444. * Schedule work to push to buffers.
  1445. * This is intended to push to the callback buffer that another driver
  1446. * registered. We are still in a handler from our IRQ. If we push
  1447. * directly, it means the other driver has it's callback called
  1448. * from our IRQ context. Which is something we better avoid.
  1449. * Let's schedule it after our IRQ is completed.
  1450. */
  1451. schedule_work(&st->touch_st.workq);
  1452. }
  1453. static void at91_adc_pen_detect_interrupt(struct at91_adc_state *st)
  1454. {
  1455. at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_PEN);
  1456. at91_adc_writel(st, IER, AT91_SAMA5D2_IER_NOPEN |
  1457. AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
  1458. AT91_SAMA5D2_IER_PRDY);
  1459. at91_adc_writel(st, TRGR, AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC |
  1460. AT91_SAMA5D2_TRGR_TRGPER(st->touch_st.sample_period_val));
  1461. st->touch_st.touching = true;
  1462. }
  1463. static void at91_adc_no_pen_detect_interrupt(struct iio_dev *indio_dev)
  1464. {
  1465. struct at91_adc_state *st = iio_priv(indio_dev);
  1466. at91_adc_writel(st, TRGR, AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER);
  1467. at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_NOPEN |
  1468. AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
  1469. AT91_SAMA5D2_IER_PRDY);
  1470. st->touch_st.touching = false;
  1471. at91_adc_touch_data_handler(indio_dev);
  1472. at91_adc_writel(st, IER, AT91_SAMA5D2_IER_PEN);
  1473. }
  1474. static void at91_adc_workq_handler(struct work_struct *workq)
  1475. {
  1476. struct at91_adc_touch *touch_st = container_of(workq,
  1477. struct at91_adc_touch, workq);
  1478. struct at91_adc_state *st = container_of(touch_st,
  1479. struct at91_adc_state, touch_st);
  1480. struct iio_dev *indio_dev = st->indio_dev;
  1481. iio_push_to_buffers(indio_dev, st->buffer);
  1482. }
  1483. static irqreturn_t at91_adc_interrupt(int irq, void *private)
  1484. {
  1485. struct iio_dev *indio = private;
  1486. struct at91_adc_state *st = iio_priv(indio);
  1487. u32 status, eoc, imr, eoc_imr;
  1488. u32 rdy_mask = AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
  1489. AT91_SAMA5D2_IER_PRDY;
  1490. at91_adc_irq_status(st, &status, &eoc);
  1491. at91_adc_irq_mask(st, &imr, &eoc_imr);
  1492. if (!(status & imr) && !(eoc & eoc_imr))
  1493. return IRQ_NONE;
  1494. if (status & AT91_SAMA5D2_IER_PEN) {
  1495. /* pen detected IRQ */
  1496. at91_adc_pen_detect_interrupt(st);
  1497. } else if ((status & AT91_SAMA5D2_IER_NOPEN)) {
  1498. /* nopen detected IRQ */
  1499. at91_adc_no_pen_detect_interrupt(indio);
  1500. } else if ((status & AT91_SAMA5D2_ISR_PENS) &&
  1501. ((status & rdy_mask) == rdy_mask)) {
  1502. /* periodic trigger IRQ - during pen sense */
  1503. at91_adc_touch_data_handler(indio);
  1504. } else if (status & AT91_SAMA5D2_ISR_PENS) {
  1505. /*
  1506. * touching, but the measurements are not ready yet.
  1507. * read and ignore.
  1508. */
  1509. status = at91_adc_readl(st, XPOSR);
  1510. status = at91_adc_readl(st, YPOSR);
  1511. status = at91_adc_readl(st, PRESSR);
  1512. } else if (iio_buffer_enabled(indio) &&
  1513. (status & AT91_SAMA5D2_IER_DRDY)) {
  1514. /* triggered buffer without DMA */
  1515. disable_irq_nosync(irq);
  1516. iio_trigger_poll(indio->trig);
  1517. } else if (iio_buffer_enabled(indio) && st->dma_st.dma_chan) {
  1518. /* triggered buffer with DMA - should not happen */
  1519. disable_irq_nosync(irq);
  1520. WARN(true, "Unexpected irq occurred\n");
  1521. } else if (!iio_buffer_enabled(indio)) {
  1522. /* software requested conversion */
  1523. st->conversion_value = at91_adc_read_chan(st, st->chan->address);
  1524. st->conversion_done = true;
  1525. wake_up_interruptible(&st->wq_data_available);
  1526. }
  1527. return IRQ_HANDLED;
  1528. }
  1529. /* This needs to be called with direct mode claimed and st->lock locked. */
  1530. static int at91_adc_read_info_raw(struct iio_dev *indio_dev,
  1531. struct iio_chan_spec const *chan, int *val)
  1532. {
  1533. struct at91_adc_state *st = iio_priv(indio_dev);
  1534. u16 tmp_val;
  1535. int ret;
  1536. ret = pm_runtime_resume_and_get(st->dev);
  1537. if (ret < 0)
  1538. return ret;
  1539. /*
  1540. * Keep in mind that we cannot use software trigger or touchscreen
  1541. * if external trigger is enabled
  1542. */
  1543. if (chan->type == IIO_POSITIONRELATIVE) {
  1544. ret = at91_adc_read_position(st, chan->channel,
  1545. &tmp_val);
  1546. *val = tmp_val;
  1547. if (ret > 0)
  1548. ret = at91_adc_adjust_val_osr(st, val);
  1549. goto pm_runtime_put;
  1550. }
  1551. if (chan->type == IIO_PRESSURE) {
  1552. ret = at91_adc_read_pressure(st, chan->channel,
  1553. &tmp_val);
  1554. *val = tmp_val;
  1555. if (ret > 0)
  1556. ret = at91_adc_adjust_val_osr(st, val);
  1557. goto pm_runtime_put;
  1558. }
  1559. /* in this case we have a voltage or temperature channel */
  1560. st->chan = chan;
  1561. at91_adc_cor(st, chan);
  1562. at91_adc_writel(st, CHER, BIT(chan->channel));
  1563. /*
  1564. * TEMPMR.TEMPON needs to update after CHER otherwise if none
  1565. * of the channels are enabled and TEMPMR.TEMPON = 1 will
  1566. * trigger DRDY interruption while preparing for temperature read.
  1567. */
  1568. if (chan->type == IIO_TEMP)
  1569. at91_adc_writel(st, TEMPMR, AT91_SAMA5D2_TEMPMR_TEMPON);
  1570. at91_adc_eoc_ena(st, chan->channel);
  1571. at91_adc_writel(st, CR, AT91_SAMA5D2_CR_START);
  1572. ret = wait_event_interruptible_timeout(st->wq_data_available,
  1573. st->conversion_done,
  1574. msecs_to_jiffies(1000));
  1575. if (ret == 0)
  1576. ret = -ETIMEDOUT;
  1577. if (ret > 0) {
  1578. *val = st->conversion_value;
  1579. ret = at91_adc_adjust_val_osr(st, val);
  1580. if (chan->scan_type.sign == 's')
  1581. *val = sign_extend32(*val,
  1582. chan->scan_type.realbits - 1);
  1583. st->conversion_done = false;
  1584. }
  1585. at91_adc_eoc_dis(st, st->chan->channel);
  1586. if (chan->type == IIO_TEMP)
  1587. at91_adc_writel(st, TEMPMR, 0U);
  1588. at91_adc_writel(st, CHDR, BIT(chan->channel));
  1589. /* Needed to ACK the DRDY interruption */
  1590. at91_adc_readl(st, LCDR);
  1591. pm_runtime_put:
  1592. pm_runtime_mark_last_busy(st->dev);
  1593. pm_runtime_put_autosuspend(st->dev);
  1594. return ret;
  1595. }
  1596. static int at91_adc_read_info_locked(struct iio_dev *indio_dev,
  1597. struct iio_chan_spec const *chan, int *val)
  1598. {
  1599. struct at91_adc_state *st = iio_priv(indio_dev);
  1600. int ret;
  1601. ret = iio_device_claim_direct_mode(indio_dev);
  1602. if (ret)
  1603. return ret;
  1604. mutex_lock(&st->lock);
  1605. ret = at91_adc_read_info_raw(indio_dev, chan, val);
  1606. mutex_unlock(&st->lock);
  1607. iio_device_release_direct_mode(indio_dev);
  1608. return ret;
  1609. }
  1610. static void at91_adc_temp_sensor_configure(struct at91_adc_state *st,
  1611. bool start)
  1612. {
  1613. u32 sample_rate, oversampling_ratio;
  1614. u32 startup_time, tracktim, trackx;
  1615. if (start) {
  1616. /*
  1617. * Configure the sensor for best accuracy: 10MHz frequency,
  1618. * oversampling rate of 256, tracktim=0xf and trackx=1.
  1619. */
  1620. sample_rate = 10 * MEGA;
  1621. oversampling_ratio = 256;
  1622. startup_time = AT91_SAMA5D2_MR_STARTUP_TS_MIN;
  1623. tracktim = AT91_SAMA5D2_MR_TRACKTIM_TS;
  1624. trackx = AT91_SAMA5D2_TRACKX_TS;
  1625. st->temp_st.saved_sample_rate = st->current_sample_rate;
  1626. st->temp_st.saved_oversampling = st->oversampling_ratio;
  1627. } else {
  1628. /* Go back to previous settings. */
  1629. sample_rate = st->temp_st.saved_sample_rate;
  1630. oversampling_ratio = st->temp_st.saved_oversampling;
  1631. startup_time = st->soc_info.startup_time;
  1632. tracktim = 0;
  1633. trackx = 0;
  1634. }
  1635. at91_adc_setup_samp_freq(st->indio_dev, sample_rate, startup_time,
  1636. tracktim);
  1637. at91_adc_config_emr(st, oversampling_ratio, trackx);
  1638. }
  1639. static int at91_adc_read_temp(struct iio_dev *indio_dev,
  1640. struct iio_chan_spec const *chan, int *val)
  1641. {
  1642. struct at91_adc_state *st = iio_priv(indio_dev);
  1643. struct at91_adc_temp_sensor_clb *clb = &st->soc_info.temp_sensor_clb;
  1644. u64 div1, div2;
  1645. u32 tmp;
  1646. int ret, vbg, vtemp;
  1647. ret = iio_device_claim_direct_mode(indio_dev);
  1648. if (ret)
  1649. return ret;
  1650. mutex_lock(&st->lock);
  1651. ret = pm_runtime_resume_and_get(st->dev);
  1652. if (ret < 0)
  1653. goto unlock;
  1654. at91_adc_temp_sensor_configure(st, true);
  1655. /* Read VBG. */
  1656. tmp = at91_adc_readl(st, ACR);
  1657. tmp |= AT91_SAMA5D2_ACR_SRCLCH;
  1658. at91_adc_writel(st, ACR, tmp);
  1659. ret = at91_adc_read_info_raw(indio_dev, chan, &vbg);
  1660. if (ret < 0)
  1661. goto restore_config;
  1662. /* Read VTEMP. */
  1663. tmp &= ~AT91_SAMA5D2_ACR_SRCLCH;
  1664. at91_adc_writel(st, ACR, tmp);
  1665. ret = at91_adc_read_info_raw(indio_dev, chan, &vtemp);
  1666. restore_config:
  1667. /* Revert previous settings. */
  1668. at91_adc_temp_sensor_configure(st, false);
  1669. pm_runtime_mark_last_busy(st->dev);
  1670. pm_runtime_put_autosuspend(st->dev);
  1671. unlock:
  1672. mutex_unlock(&st->lock);
  1673. iio_device_release_direct_mode(indio_dev);
  1674. if (ret < 0)
  1675. return ret;
  1676. /*
  1677. * Temp[milli] = p1[milli] + (vtemp * clb->p6 - clb->p4 * vbg)/
  1678. * (vbg * AT91_ADC_TS_VTEMP_DT)
  1679. */
  1680. div1 = DIV_ROUND_CLOSEST_ULL(((u64)vtemp * clb->p6), vbg);
  1681. div1 = DIV_ROUND_CLOSEST_ULL((div1 * 1000), AT91_ADC_TS_VTEMP_DT);
  1682. div2 = DIV_ROUND_CLOSEST_ULL((u64)clb->p4, AT91_ADC_TS_VTEMP_DT);
  1683. div2 *= 1000;
  1684. *val = clb->p1 + (int)div1 - (int)div2;
  1685. return ret;
  1686. }
  1687. static int at91_adc_read_raw(struct iio_dev *indio_dev,
  1688. struct iio_chan_spec const *chan,
  1689. int *val, int *val2, long mask)
  1690. {
  1691. struct at91_adc_state *st = iio_priv(indio_dev);
  1692. switch (mask) {
  1693. case IIO_CHAN_INFO_RAW:
  1694. return at91_adc_read_info_locked(indio_dev, chan, val);
  1695. case IIO_CHAN_INFO_SCALE:
  1696. *val = st->vref_uv / 1000;
  1697. if (chan->differential)
  1698. *val *= 2;
  1699. *val2 = chan->scan_type.realbits;
  1700. return IIO_VAL_FRACTIONAL_LOG2;
  1701. case IIO_CHAN_INFO_PROCESSED:
  1702. if (chan->type != IIO_TEMP)
  1703. return -EINVAL;
  1704. return at91_adc_read_temp(indio_dev, chan, val);
  1705. case IIO_CHAN_INFO_SAMP_FREQ:
  1706. *val = at91_adc_get_sample_freq(st);
  1707. return IIO_VAL_INT;
  1708. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  1709. *val = st->oversampling_ratio;
  1710. return IIO_VAL_INT;
  1711. default:
  1712. return -EINVAL;
  1713. }
  1714. }
  1715. static int at91_adc_write_raw(struct iio_dev *indio_dev,
  1716. struct iio_chan_spec const *chan,
  1717. int val, int val2, long mask)
  1718. {
  1719. struct at91_adc_state *st = iio_priv(indio_dev);
  1720. int ret;
  1721. switch (mask) {
  1722. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  1723. /* if no change, optimize out */
  1724. if (val == st->oversampling_ratio)
  1725. return 0;
  1726. ret = iio_device_claim_direct_mode(indio_dev);
  1727. if (ret)
  1728. return ret;
  1729. mutex_lock(&st->lock);
  1730. /* update ratio */
  1731. ret = at91_adc_config_emr(st, val, 0);
  1732. mutex_unlock(&st->lock);
  1733. iio_device_release_direct_mode(indio_dev);
  1734. return ret;
  1735. case IIO_CHAN_INFO_SAMP_FREQ:
  1736. if (val < st->soc_info.min_sample_rate ||
  1737. val > st->soc_info.max_sample_rate)
  1738. return -EINVAL;
  1739. ret = iio_device_claim_direct_mode(indio_dev);
  1740. if (ret)
  1741. return ret;
  1742. mutex_lock(&st->lock);
  1743. at91_adc_setup_samp_freq(indio_dev, val,
  1744. st->soc_info.startup_time, 0);
  1745. mutex_unlock(&st->lock);
  1746. iio_device_release_direct_mode(indio_dev);
  1747. return 0;
  1748. default:
  1749. return -EINVAL;
  1750. }
  1751. }
  1752. static int at91_adc_read_avail(struct iio_dev *indio_dev,
  1753. struct iio_chan_spec const *chan,
  1754. const int **vals, int *type, int *length,
  1755. long mask)
  1756. {
  1757. struct at91_adc_state *st = iio_priv(indio_dev);
  1758. switch (mask) {
  1759. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  1760. *vals = (int *)st->soc_info.platform->oversampling_avail;
  1761. *type = IIO_VAL_INT;
  1762. *length = st->soc_info.platform->oversampling_avail_no;
  1763. return IIO_AVAIL_LIST;
  1764. default:
  1765. return -EINVAL;
  1766. }
  1767. }
  1768. static void at91_adc_dma_init(struct at91_adc_state *st)
  1769. {
  1770. struct device *dev = &st->indio_dev->dev;
  1771. struct dma_slave_config config = {0};
  1772. /* we have 2 bytes for each channel */
  1773. unsigned int sample_size = st->soc_info.platform->nr_channels * 2;
  1774. /*
  1775. * We make the buffer double the size of the fifo,
  1776. * such that DMA uses one half of the buffer (full fifo size)
  1777. * and the software uses the other half to read/write.
  1778. */
  1779. unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE *
  1780. sample_size * 2, PAGE_SIZE);
  1781. if (st->dma_st.dma_chan)
  1782. return;
  1783. st->dma_st.dma_chan = dma_request_chan(dev, "rx");
  1784. if (IS_ERR(st->dma_st.dma_chan)) {
  1785. dev_info(dev, "can't get DMA channel\n");
  1786. st->dma_st.dma_chan = NULL;
  1787. goto dma_exit;
  1788. }
  1789. st->dma_st.rx_buf = dma_alloc_coherent(st->dma_st.dma_chan->device->dev,
  1790. pages * PAGE_SIZE,
  1791. &st->dma_st.rx_dma_buf,
  1792. GFP_KERNEL);
  1793. if (!st->dma_st.rx_buf) {
  1794. dev_info(dev, "can't allocate coherent DMA area\n");
  1795. goto dma_chan_disable;
  1796. }
  1797. /* Configure DMA channel to read data register */
  1798. config.direction = DMA_DEV_TO_MEM;
  1799. config.src_addr = (phys_addr_t)(st->dma_st.phys_addr
  1800. + st->soc_info.platform->layout->LCDR);
  1801. config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  1802. config.src_maxburst = 1;
  1803. config.dst_maxburst = 1;
  1804. if (dmaengine_slave_config(st->dma_st.dma_chan, &config)) {
  1805. dev_info(dev, "can't configure DMA slave\n");
  1806. goto dma_free_area;
  1807. }
  1808. dev_info(dev, "using %s for rx DMA transfers\n",
  1809. dma_chan_name(st->dma_st.dma_chan));
  1810. return;
  1811. dma_free_area:
  1812. dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE,
  1813. st->dma_st.rx_buf, st->dma_st.rx_dma_buf);
  1814. dma_chan_disable:
  1815. dma_release_channel(st->dma_st.dma_chan);
  1816. st->dma_st.dma_chan = NULL;
  1817. dma_exit:
  1818. dev_info(dev, "continuing without DMA support\n");
  1819. }
  1820. static void at91_adc_dma_disable(struct at91_adc_state *st)
  1821. {
  1822. struct device *dev = &st->indio_dev->dev;
  1823. /* we have 2 bytes for each channel */
  1824. unsigned int sample_size = st->soc_info.platform->nr_channels * 2;
  1825. unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE *
  1826. sample_size * 2, PAGE_SIZE);
  1827. /* if we are not using DMA, just return */
  1828. if (!st->dma_st.dma_chan)
  1829. return;
  1830. /* wait for all transactions to be terminated first*/
  1831. dmaengine_terminate_sync(st->dma_st.dma_chan);
  1832. dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE,
  1833. st->dma_st.rx_buf, st->dma_st.rx_dma_buf);
  1834. dma_release_channel(st->dma_st.dma_chan);
  1835. st->dma_st.dma_chan = NULL;
  1836. dev_info(dev, "continuing without DMA support\n");
  1837. }
  1838. static int at91_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val)
  1839. {
  1840. struct at91_adc_state *st = iio_priv(indio_dev);
  1841. int ret;
  1842. if (val > AT91_HWFIFO_MAX_SIZE)
  1843. val = AT91_HWFIFO_MAX_SIZE;
  1844. if (!st->selected_trig->hw_trig) {
  1845. dev_dbg(&indio_dev->dev, "we need hw trigger for DMA\n");
  1846. return 0;
  1847. }
  1848. dev_dbg(&indio_dev->dev, "new watermark is %u\n", val);
  1849. st->dma_st.watermark = val;
  1850. /*
  1851. * The logic here is: if we have watermark 1, it means we do
  1852. * each conversion with it's own IRQ, thus we don't need DMA.
  1853. * If the watermark is higher, we do DMA to do all the transfers in bulk
  1854. */
  1855. if (val == 1)
  1856. at91_adc_dma_disable(st);
  1857. else if (val > 1)
  1858. at91_adc_dma_init(st);
  1859. /*
  1860. * We can start the DMA only after setting the watermark and
  1861. * having the DMA initialization completed
  1862. */
  1863. ret = at91_adc_buffer_prepare(indio_dev);
  1864. if (ret)
  1865. at91_adc_dma_disable(st);
  1866. return ret;
  1867. }
  1868. static int at91_adc_update_scan_mode(struct iio_dev *indio_dev,
  1869. const unsigned long *scan_mask)
  1870. {
  1871. struct at91_adc_state *st = iio_priv(indio_dev);
  1872. if (bitmap_subset(scan_mask, &st->touch_st.channels_bitmask,
  1873. st->soc_info.platform->max_index + 1))
  1874. return 0;
  1875. /*
  1876. * if the new bitmap is a combination of touchscreen and regular
  1877. * channels, then we are not fine
  1878. */
  1879. if (bitmap_intersects(&st->touch_st.channels_bitmask, scan_mask,
  1880. st->soc_info.platform->max_index + 1))
  1881. return -EINVAL;
  1882. return 0;
  1883. }
  1884. static void at91_adc_hw_init(struct iio_dev *indio_dev)
  1885. {
  1886. struct at91_adc_state *st = iio_priv(indio_dev);
  1887. at91_adc_writel(st, CR, AT91_SAMA5D2_CR_SWRST);
  1888. if (st->soc_info.platform->layout->EOC_IDR)
  1889. at91_adc_writel(st, EOC_IDR, 0xffffffff);
  1890. at91_adc_writel(st, IDR, 0xffffffff);
  1891. /*
  1892. * Transfer field must be set to 2 according to the datasheet and
  1893. * allows different analog settings for each channel.
  1894. */
  1895. at91_adc_writel(st, MR,
  1896. AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH);
  1897. at91_adc_setup_samp_freq(indio_dev, st->soc_info.min_sample_rate,
  1898. st->soc_info.startup_time, 0);
  1899. /* configure extended mode register */
  1900. at91_adc_config_emr(st, st->oversampling_ratio, 0);
  1901. }
  1902. static ssize_t at91_adc_get_fifo_state(struct device *dev,
  1903. struct device_attribute *attr, char *buf)
  1904. {
  1905. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1906. struct at91_adc_state *st = iio_priv(indio_dev);
  1907. return scnprintf(buf, PAGE_SIZE, "%d\n", !!st->dma_st.dma_chan);
  1908. }
  1909. static ssize_t at91_adc_get_watermark(struct device *dev,
  1910. struct device_attribute *attr, char *buf)
  1911. {
  1912. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1913. struct at91_adc_state *st = iio_priv(indio_dev);
  1914. return scnprintf(buf, PAGE_SIZE, "%d\n", st->dma_st.watermark);
  1915. }
  1916. static ssize_t hwfifo_watermark_min_show(struct device *dev,
  1917. struct device_attribute *attr,
  1918. char *buf)
  1919. {
  1920. return sysfs_emit(buf, "%s\n", "2");
  1921. }
  1922. static ssize_t hwfifo_watermark_max_show(struct device *dev,
  1923. struct device_attribute *attr,
  1924. char *buf)
  1925. {
  1926. return sysfs_emit(buf, "%s\n", AT91_HWFIFO_MAX_SIZE_STR);
  1927. }
  1928. static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
  1929. at91_adc_get_fifo_state, NULL, 0);
  1930. static IIO_DEVICE_ATTR(hwfifo_watermark, 0444,
  1931. at91_adc_get_watermark, NULL, 0);
  1932. static IIO_DEVICE_ATTR_RO(hwfifo_watermark_min, 0);
  1933. static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0);
  1934. static const struct attribute *at91_adc_fifo_attributes[] = {
  1935. &iio_dev_attr_hwfifo_watermark_min.dev_attr.attr,
  1936. &iio_dev_attr_hwfifo_watermark_max.dev_attr.attr,
  1937. &iio_dev_attr_hwfifo_watermark.dev_attr.attr,
  1938. &iio_dev_attr_hwfifo_enabled.dev_attr.attr,
  1939. NULL,
  1940. };
  1941. static const struct iio_info at91_adc_info = {
  1942. .read_avail = &at91_adc_read_avail,
  1943. .read_raw = &at91_adc_read_raw,
  1944. .write_raw = &at91_adc_write_raw,
  1945. .update_scan_mode = &at91_adc_update_scan_mode,
  1946. .fwnode_xlate = &at91_adc_fwnode_xlate,
  1947. .hwfifo_set_watermark = &at91_adc_set_watermark,
  1948. };
  1949. static int at91_adc_buffer_and_trigger_init(struct device *dev,
  1950. struct iio_dev *indio)
  1951. {
  1952. struct at91_adc_state *st = iio_priv(indio);
  1953. const struct attribute **fifo_attrs;
  1954. int ret;
  1955. if (st->selected_trig->hw_trig)
  1956. fifo_attrs = at91_adc_fifo_attributes;
  1957. else
  1958. fifo_attrs = NULL;
  1959. ret = devm_iio_triggered_buffer_setup_ext(&indio->dev, indio,
  1960. &iio_pollfunc_store_time, &at91_adc_trigger_handler,
  1961. IIO_BUFFER_DIRECTION_IN, &at91_buffer_setup_ops, fifo_attrs);
  1962. if (ret < 0) {
  1963. dev_err(dev, "couldn't initialize the buffer.\n");
  1964. return ret;
  1965. }
  1966. if (!st->selected_trig->hw_trig)
  1967. return 0;
  1968. st->trig = at91_adc_allocate_trigger(indio, st->selected_trig->name);
  1969. if (IS_ERR(st->trig)) {
  1970. dev_err(dev, "could not allocate trigger\n");
  1971. return PTR_ERR(st->trig);
  1972. }
  1973. /*
  1974. * Initially the iio buffer has a length of 2 and
  1975. * a watermark of 1
  1976. */
  1977. st->dma_st.watermark = 1;
  1978. return 0;
  1979. }
  1980. static int at91_adc_temp_sensor_init(struct at91_adc_state *st,
  1981. struct device *dev)
  1982. {
  1983. struct at91_adc_temp_sensor_clb *clb = &st->soc_info.temp_sensor_clb;
  1984. struct nvmem_cell *temp_calib;
  1985. u32 *buf;
  1986. size_t len;
  1987. int ret = 0;
  1988. if (!st->soc_info.platform->temp_sensor)
  1989. return 0;
  1990. /* Get the calibration data from NVMEM. */
  1991. temp_calib = devm_nvmem_cell_get(dev, "temperature_calib");
  1992. if (IS_ERR(temp_calib)) {
  1993. ret = PTR_ERR(temp_calib);
  1994. if (ret != -ENOENT)
  1995. dev_err(dev, "Failed to get temperature_calib cell!\n");
  1996. return ret;
  1997. }
  1998. buf = nvmem_cell_read(temp_calib, &len);
  1999. if (IS_ERR(buf)) {
  2000. dev_err(dev, "Failed to read calibration data!\n");
  2001. return PTR_ERR(buf);
  2002. }
  2003. if (len < AT91_ADC_TS_CLB_IDX_MAX * 4) {
  2004. dev_err(dev, "Invalid calibration data!\n");
  2005. ret = -EINVAL;
  2006. goto free_buf;
  2007. }
  2008. /* Store calibration data for later use. */
  2009. clb->p1 = buf[AT91_ADC_TS_CLB_IDX_P1];
  2010. clb->p4 = buf[AT91_ADC_TS_CLB_IDX_P4];
  2011. clb->p6 = buf[AT91_ADC_TS_CLB_IDX_P6];
  2012. /*
  2013. * We prepare here the conversion to milli to avoid doing it on hotpath.
  2014. */
  2015. clb->p1 = clb->p1 * 1000;
  2016. free_buf:
  2017. kfree(buf);
  2018. return ret;
  2019. }
  2020. static int at91_adc_probe(struct platform_device *pdev)
  2021. {
  2022. struct device *dev = &pdev->dev;
  2023. struct iio_dev *indio_dev;
  2024. struct at91_adc_state *st;
  2025. struct resource *res;
  2026. int ret, i, num_channels;
  2027. u32 edge_type = IRQ_TYPE_NONE;
  2028. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
  2029. if (!indio_dev)
  2030. return -ENOMEM;
  2031. st = iio_priv(indio_dev);
  2032. st->indio_dev = indio_dev;
  2033. st->soc_info.platform = device_get_match_data(dev);
  2034. ret = at91_adc_temp_sensor_init(st, &pdev->dev);
  2035. /* Don't register temperature channel if initialization failed. */
  2036. if (ret)
  2037. num_channels = st->soc_info.platform->max_channels - 1;
  2038. else
  2039. num_channels = st->soc_info.platform->max_channels;
  2040. indio_dev->name = dev_name(&pdev->dev);
  2041. indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
  2042. indio_dev->info = &at91_adc_info;
  2043. indio_dev->channels = *st->soc_info.platform->adc_channels;
  2044. indio_dev->num_channels = num_channels;
  2045. bitmap_set(&st->touch_st.channels_bitmask,
  2046. st->soc_info.platform->touch_chan_x, 1);
  2047. bitmap_set(&st->touch_st.channels_bitmask,
  2048. st->soc_info.platform->touch_chan_y, 1);
  2049. bitmap_set(&st->touch_st.channels_bitmask,
  2050. st->soc_info.platform->touch_chan_p, 1);
  2051. st->oversampling_ratio = 1;
  2052. ret = device_property_read_u32(dev, "atmel,min-sample-rate-hz",
  2053. &st->soc_info.min_sample_rate);
  2054. if (ret) {
  2055. dev_err(&pdev->dev,
  2056. "invalid or missing value for atmel,min-sample-rate-hz\n");
  2057. return ret;
  2058. }
  2059. ret = device_property_read_u32(dev, "atmel,max-sample-rate-hz",
  2060. &st->soc_info.max_sample_rate);
  2061. if (ret) {
  2062. dev_err(&pdev->dev,
  2063. "invalid or missing value for atmel,max-sample-rate-hz\n");
  2064. return ret;
  2065. }
  2066. ret = device_property_read_u32(dev, "atmel,startup-time-ms",
  2067. &st->soc_info.startup_time);
  2068. if (ret) {
  2069. dev_err(&pdev->dev,
  2070. "invalid or missing value for atmel,startup-time-ms\n");
  2071. return ret;
  2072. }
  2073. ret = device_property_read_u32(dev, "atmel,trigger-edge-type",
  2074. &edge_type);
  2075. if (ret) {
  2076. dev_dbg(&pdev->dev,
  2077. "atmel,trigger-edge-type not specified, only software trigger available\n");
  2078. }
  2079. st->selected_trig = NULL;
  2080. /* find the right trigger, or no trigger at all */
  2081. for (i = 0; i < st->soc_info.platform->hw_trig_cnt + 1; i++)
  2082. if (at91_adc_trigger_list[i].edge_type == edge_type) {
  2083. st->selected_trig = &at91_adc_trigger_list[i];
  2084. break;
  2085. }
  2086. if (!st->selected_trig) {
  2087. dev_err(&pdev->dev, "invalid external trigger edge value\n");
  2088. return -EINVAL;
  2089. }
  2090. init_waitqueue_head(&st->wq_data_available);
  2091. mutex_init(&st->lock);
  2092. INIT_WORK(&st->touch_st.workq, at91_adc_workq_handler);
  2093. st->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  2094. if (IS_ERR(st->base))
  2095. return PTR_ERR(st->base);
  2096. /* if we plan to use DMA, we need the physical address of the regs */
  2097. st->dma_st.phys_addr = res->start;
  2098. st->irq = platform_get_irq(pdev, 0);
  2099. if (st->irq <= 0) {
  2100. if (!st->irq)
  2101. st->irq = -ENXIO;
  2102. return st->irq;
  2103. }
  2104. st->per_clk = devm_clk_get(&pdev->dev, "adc_clk");
  2105. if (IS_ERR(st->per_clk))
  2106. return PTR_ERR(st->per_clk);
  2107. st->reg = devm_regulator_get(&pdev->dev, "vddana");
  2108. if (IS_ERR(st->reg))
  2109. return PTR_ERR(st->reg);
  2110. st->vref = devm_regulator_get(&pdev->dev, "vref");
  2111. if (IS_ERR(st->vref))
  2112. return PTR_ERR(st->vref);
  2113. ret = devm_request_irq(&pdev->dev, st->irq, at91_adc_interrupt, 0,
  2114. pdev->dev.driver->name, indio_dev);
  2115. if (ret)
  2116. return ret;
  2117. ret = regulator_enable(st->reg);
  2118. if (ret)
  2119. return ret;
  2120. ret = regulator_enable(st->vref);
  2121. if (ret)
  2122. goto reg_disable;
  2123. st->vref_uv = regulator_get_voltage(st->vref);
  2124. if (st->vref_uv <= 0) {
  2125. ret = -EINVAL;
  2126. goto vref_disable;
  2127. }
  2128. ret = clk_prepare_enable(st->per_clk);
  2129. if (ret)
  2130. goto vref_disable;
  2131. platform_set_drvdata(pdev, indio_dev);
  2132. st->dev = &pdev->dev;
  2133. pm_runtime_set_autosuspend_delay(st->dev, 500);
  2134. pm_runtime_use_autosuspend(st->dev);
  2135. pm_runtime_set_active(st->dev);
  2136. pm_runtime_enable(st->dev);
  2137. pm_runtime_get_noresume(st->dev);
  2138. at91_adc_hw_init(indio_dev);
  2139. ret = at91_adc_buffer_and_trigger_init(&pdev->dev, indio_dev);
  2140. if (ret < 0)
  2141. goto err_pm_disable;
  2142. if (dma_coerce_mask_and_coherent(&indio_dev->dev, DMA_BIT_MASK(32)))
  2143. dev_info(&pdev->dev, "cannot set DMA mask to 32-bit\n");
  2144. ret = iio_device_register(indio_dev);
  2145. if (ret < 0)
  2146. goto dma_disable;
  2147. if (st->selected_trig->hw_trig)
  2148. dev_info(&pdev->dev, "setting up trigger as %s\n",
  2149. st->selected_trig->name);
  2150. dev_info(&pdev->dev, "version: %x\n",
  2151. readl_relaxed(st->base + st->soc_info.platform->layout->VERSION));
  2152. pm_runtime_mark_last_busy(st->dev);
  2153. pm_runtime_put_autosuspend(st->dev);
  2154. return 0;
  2155. dma_disable:
  2156. at91_adc_dma_disable(st);
  2157. err_pm_disable:
  2158. pm_runtime_put_noidle(st->dev);
  2159. pm_runtime_disable(st->dev);
  2160. pm_runtime_set_suspended(st->dev);
  2161. pm_runtime_dont_use_autosuspend(st->dev);
  2162. clk_disable_unprepare(st->per_clk);
  2163. vref_disable:
  2164. regulator_disable(st->vref);
  2165. reg_disable:
  2166. regulator_disable(st->reg);
  2167. return ret;
  2168. }
  2169. static int at91_adc_remove(struct platform_device *pdev)
  2170. {
  2171. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  2172. struct at91_adc_state *st = iio_priv(indio_dev);
  2173. iio_device_unregister(indio_dev);
  2174. at91_adc_dma_disable(st);
  2175. pm_runtime_disable(st->dev);
  2176. pm_runtime_set_suspended(st->dev);
  2177. clk_disable_unprepare(st->per_clk);
  2178. regulator_disable(st->vref);
  2179. regulator_disable(st->reg);
  2180. return 0;
  2181. }
  2182. static int at91_adc_suspend(struct device *dev)
  2183. {
  2184. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  2185. struct at91_adc_state *st = iio_priv(indio_dev);
  2186. int ret;
  2187. ret = pm_runtime_resume_and_get(st->dev);
  2188. if (ret < 0)
  2189. return ret;
  2190. if (iio_buffer_enabled(indio_dev))
  2191. at91_adc_buffer_postdisable(indio_dev);
  2192. /*
  2193. * Do a sofware reset of the ADC before we go to suspend.
  2194. * this will ensure that all pins are free from being muxed by the ADC
  2195. * and can be used by for other devices.
  2196. * Otherwise, ADC will hog them and we can't go to suspend mode.
  2197. */
  2198. at91_adc_writel(st, CR, AT91_SAMA5D2_CR_SWRST);
  2199. pm_runtime_mark_last_busy(st->dev);
  2200. pm_runtime_put_noidle(st->dev);
  2201. clk_disable_unprepare(st->per_clk);
  2202. regulator_disable(st->vref);
  2203. regulator_disable(st->reg);
  2204. return pinctrl_pm_select_sleep_state(dev);
  2205. }
  2206. static int at91_adc_resume(struct device *dev)
  2207. {
  2208. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  2209. struct at91_adc_state *st = iio_priv(indio_dev);
  2210. int ret;
  2211. ret = pinctrl_pm_select_default_state(dev);
  2212. if (ret)
  2213. goto resume_failed;
  2214. ret = regulator_enable(st->reg);
  2215. if (ret)
  2216. goto resume_failed;
  2217. ret = regulator_enable(st->vref);
  2218. if (ret)
  2219. goto reg_disable_resume;
  2220. ret = clk_prepare_enable(st->per_clk);
  2221. if (ret)
  2222. goto vref_disable_resume;
  2223. pm_runtime_get_noresume(st->dev);
  2224. at91_adc_hw_init(indio_dev);
  2225. /* reconfiguring trigger hardware state */
  2226. if (iio_buffer_enabled(indio_dev)) {
  2227. ret = at91_adc_buffer_prepare(indio_dev);
  2228. if (ret)
  2229. goto pm_runtime_put;
  2230. at91_adc_configure_trigger_registers(st, true);
  2231. }
  2232. pm_runtime_mark_last_busy(st->dev);
  2233. pm_runtime_put_autosuspend(st->dev);
  2234. return 0;
  2235. pm_runtime_put:
  2236. pm_runtime_mark_last_busy(st->dev);
  2237. pm_runtime_put_noidle(st->dev);
  2238. clk_disable_unprepare(st->per_clk);
  2239. vref_disable_resume:
  2240. regulator_disable(st->vref);
  2241. reg_disable_resume:
  2242. regulator_disable(st->reg);
  2243. resume_failed:
  2244. dev_err(&indio_dev->dev, "failed to resume\n");
  2245. return ret;
  2246. }
  2247. static int at91_adc_runtime_suspend(struct device *dev)
  2248. {
  2249. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  2250. struct at91_adc_state *st = iio_priv(indio_dev);
  2251. clk_disable(st->per_clk);
  2252. return 0;
  2253. }
  2254. static int at91_adc_runtime_resume(struct device *dev)
  2255. {
  2256. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  2257. struct at91_adc_state *st = iio_priv(indio_dev);
  2258. return clk_enable(st->per_clk);
  2259. }
  2260. static const struct dev_pm_ops at91_adc_pm_ops = {
  2261. SYSTEM_SLEEP_PM_OPS(at91_adc_suspend, at91_adc_resume)
  2262. RUNTIME_PM_OPS(at91_adc_runtime_suspend, at91_adc_runtime_resume,
  2263. NULL)
  2264. };
  2265. static const struct of_device_id at91_adc_dt_match[] = {
  2266. {
  2267. .compatible = "atmel,sama5d2-adc",
  2268. .data = (const void *)&sama5d2_platform,
  2269. }, {
  2270. .compatible = "microchip,sama7g5-adc",
  2271. .data = (const void *)&sama7g5_platform,
  2272. }, {
  2273. /* sentinel */
  2274. }
  2275. };
  2276. MODULE_DEVICE_TABLE(of, at91_adc_dt_match);
  2277. static struct platform_driver at91_adc_driver = {
  2278. .probe = at91_adc_probe,
  2279. .remove = at91_adc_remove,
  2280. .driver = {
  2281. .name = "at91-sama5d2_adc",
  2282. .of_match_table = at91_adc_dt_match,
  2283. .pm = pm_ptr(&at91_adc_pm_ops),
  2284. },
  2285. };
  2286. module_platform_driver(at91_adc_driver)
  2287. MODULE_AUTHOR("Ludovic Desroches <[email protected]>");
  2288. MODULE_AUTHOR("Eugen Hristev <[email protected]");
  2289. MODULE_DESCRIPTION("Atmel AT91 SAMA5D2 ADC");
  2290. MODULE_LICENSE("GPL v2");