xilinx-xadc-core.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*
  2. * Xilinx XADC driver
  3. *
  4. * Copyright 2013-2014 Analog Devices Inc.
  5. * Author: Lars-Peter Clauen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2.
  8. *
  9. * Documentation for the parts can be found at:
  10. * - XADC hardmacro: Xilinx UG480
  11. * - ZYNQ XADC interface: Xilinx UG585
  12. * - AXI XADC interface: Xilinx PG019
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/sysfs.h>
  25. #include <linux/iio/buffer.h>
  26. #include <linux/iio/events.h>
  27. #include <linux/iio/iio.h>
  28. #include <linux/iio/sysfs.h>
  29. #include <linux/iio/trigger.h>
  30. #include <linux/iio/trigger_consumer.h>
  31. #include <linux/iio/triggered_buffer.h>
  32. #include "xilinx-xadc.h"
  33. static const unsigned int XADC_ZYNQ_UNMASK_TIMEOUT = 500;
  34. /* ZYNQ register definitions */
  35. #define XADC_ZYNQ_REG_CFG 0x00
  36. #define XADC_ZYNQ_REG_INTSTS 0x04
  37. #define XADC_ZYNQ_REG_INTMSK 0x08
  38. #define XADC_ZYNQ_REG_STATUS 0x0c
  39. #define XADC_ZYNQ_REG_CFIFO 0x10
  40. #define XADC_ZYNQ_REG_DFIFO 0x14
  41. #define XADC_ZYNQ_REG_CTL 0x18
  42. #define XADC_ZYNQ_CFG_ENABLE BIT(31)
  43. #define XADC_ZYNQ_CFG_CFIFOTH_MASK (0xf << 20)
  44. #define XADC_ZYNQ_CFG_CFIFOTH_OFFSET 20
  45. #define XADC_ZYNQ_CFG_DFIFOTH_MASK (0xf << 16)
  46. #define XADC_ZYNQ_CFG_DFIFOTH_OFFSET 16
  47. #define XADC_ZYNQ_CFG_WEDGE BIT(13)
  48. #define XADC_ZYNQ_CFG_REDGE BIT(12)
  49. #define XADC_ZYNQ_CFG_TCKRATE_MASK (0x3 << 8)
  50. #define XADC_ZYNQ_CFG_TCKRATE_DIV2 (0x0 << 8)
  51. #define XADC_ZYNQ_CFG_TCKRATE_DIV4 (0x1 << 8)
  52. #define XADC_ZYNQ_CFG_TCKRATE_DIV8 (0x2 << 8)
  53. #define XADC_ZYNQ_CFG_TCKRATE_DIV16 (0x3 << 8)
  54. #define XADC_ZYNQ_CFG_IGAP_MASK 0x1f
  55. #define XADC_ZYNQ_CFG_IGAP(x) (x)
  56. #define XADC_ZYNQ_INT_CFIFO_LTH BIT(9)
  57. #define XADC_ZYNQ_INT_DFIFO_GTH BIT(8)
  58. #define XADC_ZYNQ_INT_ALARM_MASK 0xff
  59. #define XADC_ZYNQ_INT_ALARM_OFFSET 0
  60. #define XADC_ZYNQ_STATUS_CFIFO_LVL_MASK (0xf << 16)
  61. #define XADC_ZYNQ_STATUS_CFIFO_LVL_OFFSET 16
  62. #define XADC_ZYNQ_STATUS_DFIFO_LVL_MASK (0xf << 12)
  63. #define XADC_ZYNQ_STATUS_DFIFO_LVL_OFFSET 12
  64. #define XADC_ZYNQ_STATUS_CFIFOF BIT(11)
  65. #define XADC_ZYNQ_STATUS_CFIFOE BIT(10)
  66. #define XADC_ZYNQ_STATUS_DFIFOF BIT(9)
  67. #define XADC_ZYNQ_STATUS_DFIFOE BIT(8)
  68. #define XADC_ZYNQ_STATUS_OT BIT(7)
  69. #define XADC_ZYNQ_STATUS_ALM(x) BIT(x)
  70. #define XADC_ZYNQ_CTL_RESET BIT(4)
  71. #define XADC_ZYNQ_CMD_NOP 0x00
  72. #define XADC_ZYNQ_CMD_READ 0x01
  73. #define XADC_ZYNQ_CMD_WRITE 0x02
  74. #define XADC_ZYNQ_CMD(cmd, addr, data) (((cmd) << 26) | ((addr) << 16) | (data))
  75. /* AXI register definitions */
  76. #define XADC_AXI_REG_RESET 0x00
  77. #define XADC_AXI_REG_STATUS 0x04
  78. #define XADC_AXI_REG_ALARM_STATUS 0x08
  79. #define XADC_AXI_REG_CONVST 0x0c
  80. #define XADC_AXI_REG_XADC_RESET 0x10
  81. #define XADC_AXI_REG_GIER 0x5c
  82. #define XADC_AXI_REG_IPISR 0x60
  83. #define XADC_AXI_REG_IPIER 0x68
  84. #define XADC_AXI_ADC_REG_OFFSET 0x200
  85. #define XADC_AXI_RESET_MAGIC 0xa
  86. #define XADC_AXI_GIER_ENABLE BIT(31)
  87. #define XADC_AXI_INT_EOS BIT(4)
  88. #define XADC_AXI_INT_ALARM_MASK 0x3c0f
  89. #define XADC_FLAGS_BUFFERED BIT(0)
  90. static void xadc_write_reg(struct xadc *xadc, unsigned int reg,
  91. uint32_t val)
  92. {
  93. writel(val, xadc->base + reg);
  94. }
  95. static void xadc_read_reg(struct xadc *xadc, unsigned int reg,
  96. uint32_t *val)
  97. {
  98. *val = readl(xadc->base + reg);
  99. }
  100. /*
  101. * The ZYNQ interface uses two asynchronous FIFOs for communication with the
  102. * XADC. Reads and writes to the XADC register are performed by submitting a
  103. * request to the command FIFO (CFIFO), once the request has been completed the
  104. * result can be read from the data FIFO (DFIFO). The method currently used in
  105. * this driver is to submit the request for a read/write operation, then go to
  106. * sleep and wait for an interrupt that signals that a response is available in
  107. * the data FIFO.
  108. */
  109. static void xadc_zynq_write_fifo(struct xadc *xadc, uint32_t *cmd,
  110. unsigned int n)
  111. {
  112. unsigned int i;
  113. for (i = 0; i < n; i++)
  114. xadc_write_reg(xadc, XADC_ZYNQ_REG_CFIFO, cmd[i]);
  115. }
  116. static void xadc_zynq_drain_fifo(struct xadc *xadc)
  117. {
  118. uint32_t status, tmp;
  119. xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &status);
  120. while (!(status & XADC_ZYNQ_STATUS_DFIFOE)) {
  121. xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &tmp);
  122. xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &status);
  123. }
  124. }
  125. static void xadc_zynq_update_intmsk(struct xadc *xadc, unsigned int mask,
  126. unsigned int val)
  127. {
  128. xadc->zynq_intmask &= ~mask;
  129. xadc->zynq_intmask |= val;
  130. xadc_write_reg(xadc, XADC_ZYNQ_REG_INTMSK,
  131. xadc->zynq_intmask | xadc->zynq_masked_alarm);
  132. }
  133. static int xadc_zynq_write_adc_reg(struct xadc *xadc, unsigned int reg,
  134. uint16_t val)
  135. {
  136. uint32_t cmd[1];
  137. uint32_t tmp;
  138. int ret;
  139. spin_lock_irq(&xadc->lock);
  140. xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
  141. XADC_ZYNQ_INT_DFIFO_GTH);
  142. reinit_completion(&xadc->completion);
  143. cmd[0] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_WRITE, reg, val);
  144. xadc_zynq_write_fifo(xadc, cmd, ARRAY_SIZE(cmd));
  145. xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &tmp);
  146. tmp &= ~XADC_ZYNQ_CFG_DFIFOTH_MASK;
  147. tmp |= 0 << XADC_ZYNQ_CFG_DFIFOTH_OFFSET;
  148. xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, tmp);
  149. xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH, 0);
  150. spin_unlock_irq(&xadc->lock);
  151. ret = wait_for_completion_interruptible_timeout(&xadc->completion, HZ);
  152. if (ret == 0)
  153. ret = -EIO;
  154. else
  155. ret = 0;
  156. xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &tmp);
  157. return ret;
  158. }
  159. static int xadc_zynq_read_adc_reg(struct xadc *xadc, unsigned int reg,
  160. uint16_t *val)
  161. {
  162. uint32_t cmd[2];
  163. uint32_t resp, tmp;
  164. int ret;
  165. cmd[0] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_READ, reg, 0);
  166. cmd[1] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_NOP, 0, 0);
  167. spin_lock_irq(&xadc->lock);
  168. xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
  169. XADC_ZYNQ_INT_DFIFO_GTH);
  170. xadc_zynq_drain_fifo(xadc);
  171. reinit_completion(&xadc->completion);
  172. xadc_zynq_write_fifo(xadc, cmd, ARRAY_SIZE(cmd));
  173. xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &tmp);
  174. tmp &= ~XADC_ZYNQ_CFG_DFIFOTH_MASK;
  175. tmp |= 1 << XADC_ZYNQ_CFG_DFIFOTH_OFFSET;
  176. xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, tmp);
  177. xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH, 0);
  178. spin_unlock_irq(&xadc->lock);
  179. ret = wait_for_completion_interruptible_timeout(&xadc->completion, HZ);
  180. if (ret == 0)
  181. ret = -EIO;
  182. if (ret < 0)
  183. return ret;
  184. xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &resp);
  185. xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &resp);
  186. *val = resp & 0xffff;
  187. return 0;
  188. }
  189. static unsigned int xadc_zynq_transform_alarm(unsigned int alarm)
  190. {
  191. return ((alarm & 0x80) >> 4) |
  192. ((alarm & 0x78) << 1) |
  193. (alarm & 0x07);
  194. }
  195. /*
  196. * The ZYNQ threshold interrupts are level sensitive. Since we can't make the
  197. * threshold condition go way from within the interrupt handler, this means as
  198. * soon as a threshold condition is present we would enter the interrupt handler
  199. * again and again. To work around this we mask all active thresholds interrupts
  200. * in the interrupt handler and start a timer. In this timer we poll the
  201. * interrupt status and only if the interrupt is inactive we unmask it again.
  202. */
  203. static void xadc_zynq_unmask_worker(struct work_struct *work)
  204. {
  205. struct xadc *xadc = container_of(work, struct xadc, zynq_unmask_work.work);
  206. unsigned int misc_sts, unmask;
  207. xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &misc_sts);
  208. misc_sts &= XADC_ZYNQ_INT_ALARM_MASK;
  209. spin_lock_irq(&xadc->lock);
  210. /* Clear those bits which are not active anymore */
  211. unmask = (xadc->zynq_masked_alarm ^ misc_sts) & xadc->zynq_masked_alarm;
  212. xadc->zynq_masked_alarm &= misc_sts;
  213. /* Also clear those which are masked out anyway */
  214. xadc->zynq_masked_alarm &= ~xadc->zynq_intmask;
  215. /* Clear the interrupts before we unmask them */
  216. xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, unmask);
  217. xadc_zynq_update_intmsk(xadc, 0, 0);
  218. spin_unlock_irq(&xadc->lock);
  219. /* if still pending some alarm re-trigger the timer */
  220. if (xadc->zynq_masked_alarm) {
  221. schedule_delayed_work(&xadc->zynq_unmask_work,
  222. msecs_to_jiffies(XADC_ZYNQ_UNMASK_TIMEOUT));
  223. }
  224. }
  225. static irqreturn_t xadc_zynq_interrupt_handler(int irq, void *devid)
  226. {
  227. struct iio_dev *indio_dev = devid;
  228. struct xadc *xadc = iio_priv(indio_dev);
  229. uint32_t status;
  230. xadc_read_reg(xadc, XADC_ZYNQ_REG_INTSTS, &status);
  231. status &= ~(xadc->zynq_intmask | xadc->zynq_masked_alarm);
  232. if (!status)
  233. return IRQ_NONE;
  234. spin_lock(&xadc->lock);
  235. xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, status);
  236. if (status & XADC_ZYNQ_INT_DFIFO_GTH) {
  237. xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
  238. XADC_ZYNQ_INT_DFIFO_GTH);
  239. complete(&xadc->completion);
  240. }
  241. status &= XADC_ZYNQ_INT_ALARM_MASK;
  242. if (status) {
  243. xadc->zynq_masked_alarm |= status;
  244. /*
  245. * mask the current event interrupt,
  246. * unmask it when the interrupt is no more active.
  247. */
  248. xadc_zynq_update_intmsk(xadc, 0, 0);
  249. xadc_handle_events(indio_dev,
  250. xadc_zynq_transform_alarm(status));
  251. /* unmask the required interrupts in timer. */
  252. schedule_delayed_work(&xadc->zynq_unmask_work,
  253. msecs_to_jiffies(XADC_ZYNQ_UNMASK_TIMEOUT));
  254. }
  255. spin_unlock(&xadc->lock);
  256. return IRQ_HANDLED;
  257. }
  258. #define XADC_ZYNQ_TCK_RATE_MAX 50000000
  259. #define XADC_ZYNQ_IGAP_DEFAULT 20
  260. static int xadc_zynq_setup(struct platform_device *pdev,
  261. struct iio_dev *indio_dev, int irq)
  262. {
  263. struct xadc *xadc = iio_priv(indio_dev);
  264. unsigned long pcap_rate;
  265. unsigned int tck_div;
  266. unsigned int div;
  267. unsigned int igap;
  268. unsigned int tck_rate;
  269. /* TODO: Figure out how to make igap and tck_rate configurable */
  270. igap = XADC_ZYNQ_IGAP_DEFAULT;
  271. tck_rate = XADC_ZYNQ_TCK_RATE_MAX;
  272. xadc->zynq_intmask = ~0;
  273. pcap_rate = clk_get_rate(xadc->clk);
  274. if (tck_rate > XADC_ZYNQ_TCK_RATE_MAX)
  275. tck_rate = XADC_ZYNQ_TCK_RATE_MAX;
  276. if (tck_rate > pcap_rate / 2) {
  277. div = 2;
  278. } else {
  279. div = pcap_rate / tck_rate;
  280. if (pcap_rate / div > XADC_ZYNQ_TCK_RATE_MAX)
  281. div++;
  282. }
  283. if (div <= 3)
  284. tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV2;
  285. else if (div <= 7)
  286. tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV4;
  287. else if (div <= 15)
  288. tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV8;
  289. else
  290. tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV16;
  291. xadc_write_reg(xadc, XADC_ZYNQ_REG_CTL, XADC_ZYNQ_CTL_RESET);
  292. xadc_write_reg(xadc, XADC_ZYNQ_REG_CTL, 0);
  293. xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, ~0);
  294. xadc_write_reg(xadc, XADC_ZYNQ_REG_INTMSK, xadc->zynq_intmask);
  295. xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, XADC_ZYNQ_CFG_ENABLE |
  296. XADC_ZYNQ_CFG_REDGE | XADC_ZYNQ_CFG_WEDGE |
  297. tck_div | XADC_ZYNQ_CFG_IGAP(igap));
  298. return 0;
  299. }
  300. static unsigned long xadc_zynq_get_dclk_rate(struct xadc *xadc)
  301. {
  302. unsigned int div;
  303. uint32_t val;
  304. xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &val);
  305. switch (val & XADC_ZYNQ_CFG_TCKRATE_MASK) {
  306. case XADC_ZYNQ_CFG_TCKRATE_DIV4:
  307. div = 4;
  308. break;
  309. case XADC_ZYNQ_CFG_TCKRATE_DIV8:
  310. div = 8;
  311. break;
  312. case XADC_ZYNQ_CFG_TCKRATE_DIV16:
  313. div = 16;
  314. break;
  315. default:
  316. div = 2;
  317. break;
  318. }
  319. return clk_get_rate(xadc->clk) / div;
  320. }
  321. static void xadc_zynq_update_alarm(struct xadc *xadc, unsigned int alarm)
  322. {
  323. unsigned long flags;
  324. uint32_t status;
  325. /* Move OT to bit 7 */
  326. alarm = ((alarm & 0x08) << 4) | ((alarm & 0xf0) >> 1) | (alarm & 0x07);
  327. spin_lock_irqsave(&xadc->lock, flags);
  328. /* Clear previous interrupts if any. */
  329. xadc_read_reg(xadc, XADC_ZYNQ_REG_INTSTS, &status);
  330. xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, status & alarm);
  331. xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_ALARM_MASK,
  332. ~alarm & XADC_ZYNQ_INT_ALARM_MASK);
  333. spin_unlock_irqrestore(&xadc->lock, flags);
  334. }
  335. static const struct xadc_ops xadc_zynq_ops = {
  336. .read = xadc_zynq_read_adc_reg,
  337. .write = xadc_zynq_write_adc_reg,
  338. .setup = xadc_zynq_setup,
  339. .get_dclk_rate = xadc_zynq_get_dclk_rate,
  340. .interrupt_handler = xadc_zynq_interrupt_handler,
  341. .update_alarm = xadc_zynq_update_alarm,
  342. };
  343. static int xadc_axi_read_adc_reg(struct xadc *xadc, unsigned int reg,
  344. uint16_t *val)
  345. {
  346. uint32_t val32;
  347. xadc_read_reg(xadc, XADC_AXI_ADC_REG_OFFSET + reg * 4, &val32);
  348. *val = val32 & 0xffff;
  349. return 0;
  350. }
  351. static int xadc_axi_write_adc_reg(struct xadc *xadc, unsigned int reg,
  352. uint16_t val)
  353. {
  354. xadc_write_reg(xadc, XADC_AXI_ADC_REG_OFFSET + reg * 4, val);
  355. return 0;
  356. }
  357. static int xadc_axi_setup(struct platform_device *pdev,
  358. struct iio_dev *indio_dev, int irq)
  359. {
  360. struct xadc *xadc = iio_priv(indio_dev);
  361. xadc_write_reg(xadc, XADC_AXI_REG_RESET, XADC_AXI_RESET_MAGIC);
  362. xadc_write_reg(xadc, XADC_AXI_REG_GIER, XADC_AXI_GIER_ENABLE);
  363. return 0;
  364. }
  365. static irqreturn_t xadc_axi_interrupt_handler(int irq, void *devid)
  366. {
  367. struct iio_dev *indio_dev = devid;
  368. struct xadc *xadc = iio_priv(indio_dev);
  369. uint32_t status, mask;
  370. unsigned int events;
  371. xadc_read_reg(xadc, XADC_AXI_REG_IPISR, &status);
  372. xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &mask);
  373. status &= mask;
  374. if (!status)
  375. return IRQ_NONE;
  376. if ((status & XADC_AXI_INT_EOS) && xadc->trigger)
  377. iio_trigger_poll(xadc->trigger);
  378. if (status & XADC_AXI_INT_ALARM_MASK) {
  379. /*
  380. * The order of the bits in the AXI-XADC status register does
  381. * not match the order of the bits in the XADC alarm enable
  382. * register. xadc_handle_events() expects the events to be in
  383. * the same order as the XADC alarm enable register.
  384. */
  385. events = (status & 0x000e) >> 1;
  386. events |= (status & 0x0001) << 3;
  387. events |= (status & 0x3c00) >> 6;
  388. xadc_handle_events(indio_dev, events);
  389. }
  390. xadc_write_reg(xadc, XADC_AXI_REG_IPISR, status);
  391. return IRQ_HANDLED;
  392. }
  393. static void xadc_axi_update_alarm(struct xadc *xadc, unsigned int alarm)
  394. {
  395. uint32_t val;
  396. unsigned long flags;
  397. /*
  398. * The order of the bits in the AXI-XADC status register does not match
  399. * the order of the bits in the XADC alarm enable register. We get
  400. * passed the alarm mask in the same order as in the XADC alarm enable
  401. * register.
  402. */
  403. alarm = ((alarm & 0x07) << 1) | ((alarm & 0x08) >> 3) |
  404. ((alarm & 0xf0) << 6);
  405. spin_lock_irqsave(&xadc->lock, flags);
  406. xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val);
  407. val &= ~XADC_AXI_INT_ALARM_MASK;
  408. val |= alarm;
  409. xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val);
  410. spin_unlock_irqrestore(&xadc->lock, flags);
  411. }
  412. static unsigned long xadc_axi_get_dclk(struct xadc *xadc)
  413. {
  414. return clk_get_rate(xadc->clk);
  415. }
  416. static const struct xadc_ops xadc_axi_ops = {
  417. .read = xadc_axi_read_adc_reg,
  418. .write = xadc_axi_write_adc_reg,
  419. .setup = xadc_axi_setup,
  420. .get_dclk_rate = xadc_axi_get_dclk,
  421. .update_alarm = xadc_axi_update_alarm,
  422. .interrupt_handler = xadc_axi_interrupt_handler,
  423. .flags = XADC_FLAGS_BUFFERED,
  424. };
  425. static int _xadc_update_adc_reg(struct xadc *xadc, unsigned int reg,
  426. uint16_t mask, uint16_t val)
  427. {
  428. uint16_t tmp;
  429. int ret;
  430. ret = _xadc_read_adc_reg(xadc, reg, &tmp);
  431. if (ret)
  432. return ret;
  433. return _xadc_write_adc_reg(xadc, reg, (tmp & ~mask) | val);
  434. }
  435. static int xadc_update_adc_reg(struct xadc *xadc, unsigned int reg,
  436. uint16_t mask, uint16_t val)
  437. {
  438. int ret;
  439. mutex_lock(&xadc->mutex);
  440. ret = _xadc_update_adc_reg(xadc, reg, mask, val);
  441. mutex_unlock(&xadc->mutex);
  442. return ret;
  443. }
  444. static unsigned long xadc_get_dclk_rate(struct xadc *xadc)
  445. {
  446. return xadc->ops->get_dclk_rate(xadc);
  447. }
  448. static int xadc_update_scan_mode(struct iio_dev *indio_dev,
  449. const unsigned long *mask)
  450. {
  451. struct xadc *xadc = iio_priv(indio_dev);
  452. unsigned int n;
  453. n = bitmap_weight(mask, indio_dev->masklength);
  454. kfree(xadc->data);
  455. xadc->data = kcalloc(n, sizeof(*xadc->data), GFP_KERNEL);
  456. if (!xadc->data)
  457. return -ENOMEM;
  458. return 0;
  459. }
  460. static unsigned int xadc_scan_index_to_channel(unsigned int scan_index)
  461. {
  462. switch (scan_index) {
  463. case 5:
  464. return XADC_REG_VCCPINT;
  465. case 6:
  466. return XADC_REG_VCCPAUX;
  467. case 7:
  468. return XADC_REG_VCCO_DDR;
  469. case 8:
  470. return XADC_REG_TEMP;
  471. case 9:
  472. return XADC_REG_VCCINT;
  473. case 10:
  474. return XADC_REG_VCCAUX;
  475. case 11:
  476. return XADC_REG_VPVN;
  477. case 12:
  478. return XADC_REG_VREFP;
  479. case 13:
  480. return XADC_REG_VREFN;
  481. case 14:
  482. return XADC_REG_VCCBRAM;
  483. default:
  484. return XADC_REG_VAUX(scan_index - 16);
  485. }
  486. }
  487. static irqreturn_t xadc_trigger_handler(int irq, void *p)
  488. {
  489. struct iio_poll_func *pf = p;
  490. struct iio_dev *indio_dev = pf->indio_dev;
  491. struct xadc *xadc = iio_priv(indio_dev);
  492. unsigned int chan;
  493. int i, j;
  494. if (!xadc->data)
  495. goto out;
  496. j = 0;
  497. for_each_set_bit(i, indio_dev->active_scan_mask,
  498. indio_dev->masklength) {
  499. chan = xadc_scan_index_to_channel(i);
  500. xadc_read_adc_reg(xadc, chan, &xadc->data[j]);
  501. j++;
  502. }
  503. iio_push_to_buffers(indio_dev, xadc->data);
  504. out:
  505. iio_trigger_notify_done(indio_dev->trig);
  506. return IRQ_HANDLED;
  507. }
  508. static int xadc_trigger_set_state(struct iio_trigger *trigger, bool state)
  509. {
  510. struct xadc *xadc = iio_trigger_get_drvdata(trigger);
  511. unsigned long flags;
  512. unsigned int convst;
  513. unsigned int val;
  514. int ret = 0;
  515. mutex_lock(&xadc->mutex);
  516. if (state) {
  517. /* Only one of the two triggers can be active at the a time. */
  518. if (xadc->trigger != NULL) {
  519. ret = -EBUSY;
  520. goto err_out;
  521. } else {
  522. xadc->trigger = trigger;
  523. if (trigger == xadc->convst_trigger)
  524. convst = XADC_CONF0_EC;
  525. else
  526. convst = 0;
  527. }
  528. ret = _xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF0_EC,
  529. convst);
  530. if (ret)
  531. goto err_out;
  532. } else {
  533. xadc->trigger = NULL;
  534. }
  535. spin_lock_irqsave(&xadc->lock, flags);
  536. xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val);
  537. xadc_write_reg(xadc, XADC_AXI_REG_IPISR, val & XADC_AXI_INT_EOS);
  538. if (state)
  539. val |= XADC_AXI_INT_EOS;
  540. else
  541. val &= ~XADC_AXI_INT_EOS;
  542. xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val);
  543. spin_unlock_irqrestore(&xadc->lock, flags);
  544. err_out:
  545. mutex_unlock(&xadc->mutex);
  546. return ret;
  547. }
  548. static const struct iio_trigger_ops xadc_trigger_ops = {
  549. .owner = THIS_MODULE,
  550. .set_trigger_state = &xadc_trigger_set_state,
  551. };
  552. static struct iio_trigger *xadc_alloc_trigger(struct iio_dev *indio_dev,
  553. const char *name)
  554. {
  555. struct iio_trigger *trig;
  556. int ret;
  557. trig = iio_trigger_alloc("%s%d-%s", indio_dev->name,
  558. indio_dev->id, name);
  559. if (trig == NULL)
  560. return ERR_PTR(-ENOMEM);
  561. trig->dev.parent = indio_dev->dev.parent;
  562. trig->ops = &xadc_trigger_ops;
  563. iio_trigger_set_drvdata(trig, iio_priv(indio_dev));
  564. ret = iio_trigger_register(trig);
  565. if (ret)
  566. goto error_free_trig;
  567. return trig;
  568. error_free_trig:
  569. iio_trigger_free(trig);
  570. return ERR_PTR(ret);
  571. }
  572. static int xadc_power_adc_b(struct xadc *xadc, unsigned int seq_mode)
  573. {
  574. uint16_t val;
  575. switch (seq_mode) {
  576. case XADC_CONF1_SEQ_SIMULTANEOUS:
  577. case XADC_CONF1_SEQ_INDEPENDENT:
  578. val = XADC_CONF2_PD_ADC_B;
  579. break;
  580. default:
  581. val = 0;
  582. break;
  583. }
  584. return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_PD_MASK,
  585. val);
  586. }
  587. static int xadc_get_seq_mode(struct xadc *xadc, unsigned long scan_mode)
  588. {
  589. unsigned int aux_scan_mode = scan_mode >> 16;
  590. if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_DUAL)
  591. return XADC_CONF1_SEQ_SIMULTANEOUS;
  592. if ((aux_scan_mode & 0xff00) == 0 ||
  593. (aux_scan_mode & 0x00ff) == 0)
  594. return XADC_CONF1_SEQ_CONTINUOUS;
  595. return XADC_CONF1_SEQ_SIMULTANEOUS;
  596. }
  597. static int xadc_postdisable(struct iio_dev *indio_dev)
  598. {
  599. struct xadc *xadc = iio_priv(indio_dev);
  600. unsigned long scan_mask;
  601. int ret;
  602. int i;
  603. scan_mask = 1; /* Run calibration as part of the sequence */
  604. for (i = 0; i < indio_dev->num_channels; i++)
  605. scan_mask |= BIT(indio_dev->channels[i].scan_index);
  606. /* Enable all channels and calibration */
  607. ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff);
  608. if (ret)
  609. return ret;
  610. ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16);
  611. if (ret)
  612. return ret;
  613. ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
  614. XADC_CONF1_SEQ_CONTINUOUS);
  615. if (ret)
  616. return ret;
  617. return xadc_power_adc_b(xadc, XADC_CONF1_SEQ_CONTINUOUS);
  618. }
  619. static int xadc_preenable(struct iio_dev *indio_dev)
  620. {
  621. struct xadc *xadc = iio_priv(indio_dev);
  622. unsigned long scan_mask;
  623. int seq_mode;
  624. int ret;
  625. ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
  626. XADC_CONF1_SEQ_DEFAULT);
  627. if (ret)
  628. goto err;
  629. scan_mask = *indio_dev->active_scan_mask;
  630. seq_mode = xadc_get_seq_mode(xadc, scan_mask);
  631. ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff);
  632. if (ret)
  633. goto err;
  634. ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16);
  635. if (ret)
  636. goto err;
  637. ret = xadc_power_adc_b(xadc, seq_mode);
  638. if (ret)
  639. goto err;
  640. ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
  641. seq_mode);
  642. if (ret)
  643. goto err;
  644. return 0;
  645. err:
  646. xadc_postdisable(indio_dev);
  647. return ret;
  648. }
  649. static const struct iio_buffer_setup_ops xadc_buffer_ops = {
  650. .preenable = &xadc_preenable,
  651. .postenable = &iio_triggered_buffer_postenable,
  652. .predisable = &iio_triggered_buffer_predisable,
  653. .postdisable = &xadc_postdisable,
  654. };
  655. static int xadc_read_raw(struct iio_dev *indio_dev,
  656. struct iio_chan_spec const *chan, int *val, int *val2, long info)
  657. {
  658. struct xadc *xadc = iio_priv(indio_dev);
  659. unsigned int div;
  660. uint16_t val16;
  661. int ret;
  662. switch (info) {
  663. case IIO_CHAN_INFO_RAW:
  664. if (iio_buffer_enabled(indio_dev))
  665. return -EBUSY;
  666. ret = xadc_read_adc_reg(xadc, chan->address, &val16);
  667. if (ret < 0)
  668. return ret;
  669. val16 >>= 4;
  670. if (chan->scan_type.sign == 'u')
  671. *val = val16;
  672. else
  673. *val = sign_extend32(val16, 11);
  674. return IIO_VAL_INT;
  675. case IIO_CHAN_INFO_SCALE:
  676. switch (chan->type) {
  677. case IIO_VOLTAGE:
  678. /* V = (val * 3.0) / 4096 */
  679. switch (chan->address) {
  680. case XADC_REG_VCCINT:
  681. case XADC_REG_VCCAUX:
  682. case XADC_REG_VREFP:
  683. case XADC_REG_VREFN:
  684. case XADC_REG_VCCBRAM:
  685. case XADC_REG_VCCPINT:
  686. case XADC_REG_VCCPAUX:
  687. case XADC_REG_VCCO_DDR:
  688. *val = 3000;
  689. break;
  690. default:
  691. *val = 1000;
  692. break;
  693. }
  694. *val2 = 12;
  695. return IIO_VAL_FRACTIONAL_LOG2;
  696. case IIO_TEMP:
  697. /* Temp in C = (val * 503.975) / 4096 - 273.15 */
  698. *val = 503975;
  699. *val2 = 12;
  700. return IIO_VAL_FRACTIONAL_LOG2;
  701. default:
  702. return -EINVAL;
  703. }
  704. case IIO_CHAN_INFO_OFFSET:
  705. /* Only the temperature channel has an offset */
  706. *val = -((273150 << 12) / 503975);
  707. return IIO_VAL_INT;
  708. case IIO_CHAN_INFO_SAMP_FREQ:
  709. ret = xadc_read_adc_reg(xadc, XADC_REG_CONF2, &val16);
  710. if (ret)
  711. return ret;
  712. div = (val16 & XADC_CONF2_DIV_MASK) >> XADC_CONF2_DIV_OFFSET;
  713. if (div < 2)
  714. div = 2;
  715. *val = xadc_get_dclk_rate(xadc) / div / 26;
  716. return IIO_VAL_INT;
  717. default:
  718. return -EINVAL;
  719. }
  720. }
  721. static int xadc_write_raw(struct iio_dev *indio_dev,
  722. struct iio_chan_spec const *chan, int val, int val2, long info)
  723. {
  724. struct xadc *xadc = iio_priv(indio_dev);
  725. unsigned long clk_rate = xadc_get_dclk_rate(xadc);
  726. unsigned int div;
  727. if (info != IIO_CHAN_INFO_SAMP_FREQ)
  728. return -EINVAL;
  729. if (val <= 0)
  730. return -EINVAL;
  731. /* Max. 150 kSPS */
  732. if (val > 150000)
  733. val = 150000;
  734. val *= 26;
  735. /* Min 1MHz */
  736. if (val < 1000000)
  737. val = 1000000;
  738. /*
  739. * We want to round down, but only if we do not exceed the 150 kSPS
  740. * limit.
  741. */
  742. div = clk_rate / val;
  743. if (clk_rate / div / 26 > 150000)
  744. div++;
  745. if (div < 2)
  746. div = 2;
  747. else if (div > 0xff)
  748. div = 0xff;
  749. return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_DIV_MASK,
  750. div << XADC_CONF2_DIV_OFFSET);
  751. }
  752. static const struct iio_event_spec xadc_temp_events[] = {
  753. {
  754. .type = IIO_EV_TYPE_THRESH,
  755. .dir = IIO_EV_DIR_RISING,
  756. .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
  757. BIT(IIO_EV_INFO_VALUE) |
  758. BIT(IIO_EV_INFO_HYSTERESIS),
  759. },
  760. };
  761. /* Separate values for upper and lower thresholds, but only a shared enabled */
  762. static const struct iio_event_spec xadc_voltage_events[] = {
  763. {
  764. .type = IIO_EV_TYPE_THRESH,
  765. .dir = IIO_EV_DIR_RISING,
  766. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  767. }, {
  768. .type = IIO_EV_TYPE_THRESH,
  769. .dir = IIO_EV_DIR_FALLING,
  770. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  771. }, {
  772. .type = IIO_EV_TYPE_THRESH,
  773. .dir = IIO_EV_DIR_EITHER,
  774. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  775. },
  776. };
  777. #define XADC_CHAN_TEMP(_chan, _scan_index, _addr) { \
  778. .type = IIO_TEMP, \
  779. .indexed = 1, \
  780. .channel = (_chan), \
  781. .address = (_addr), \
  782. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  783. BIT(IIO_CHAN_INFO_SCALE) | \
  784. BIT(IIO_CHAN_INFO_OFFSET), \
  785. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  786. .event_spec = xadc_temp_events, \
  787. .num_event_specs = ARRAY_SIZE(xadc_temp_events), \
  788. .scan_index = (_scan_index), \
  789. .scan_type = { \
  790. .sign = 'u', \
  791. .realbits = 12, \
  792. .storagebits = 16, \
  793. .shift = 4, \
  794. .endianness = IIO_CPU, \
  795. }, \
  796. }
  797. #define XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, _ext, _alarm) { \
  798. .type = IIO_VOLTAGE, \
  799. .indexed = 1, \
  800. .channel = (_chan), \
  801. .address = (_addr), \
  802. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  803. BIT(IIO_CHAN_INFO_SCALE), \
  804. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  805. .event_spec = (_alarm) ? xadc_voltage_events : NULL, \
  806. .num_event_specs = (_alarm) ? ARRAY_SIZE(xadc_voltage_events) : 0, \
  807. .scan_index = (_scan_index), \
  808. .scan_type = { \
  809. .sign = ((_addr) == XADC_REG_VREFN) ? 's' : 'u', \
  810. .realbits = 12, \
  811. .storagebits = 16, \
  812. .shift = 4, \
  813. .endianness = IIO_CPU, \
  814. }, \
  815. .extend_name = _ext, \
  816. }
  817. static const struct iio_chan_spec xadc_channels[] = {
  818. XADC_CHAN_TEMP(0, 8, XADC_REG_TEMP),
  819. XADC_CHAN_VOLTAGE(0, 9, XADC_REG_VCCINT, "vccint", true),
  820. XADC_CHAN_VOLTAGE(1, 10, XADC_REG_VCCAUX, "vccaux", true),
  821. XADC_CHAN_VOLTAGE(2, 14, XADC_REG_VCCBRAM, "vccbram", true),
  822. XADC_CHAN_VOLTAGE(3, 5, XADC_REG_VCCPINT, "vccpint", true),
  823. XADC_CHAN_VOLTAGE(4, 6, XADC_REG_VCCPAUX, "vccpaux", true),
  824. XADC_CHAN_VOLTAGE(5, 7, XADC_REG_VCCO_DDR, "vccoddr", true),
  825. XADC_CHAN_VOLTAGE(6, 12, XADC_REG_VREFP, "vrefp", false),
  826. XADC_CHAN_VOLTAGE(7, 13, XADC_REG_VREFN, "vrefn", false),
  827. XADC_CHAN_VOLTAGE(8, 11, XADC_REG_VPVN, NULL, false),
  828. XADC_CHAN_VOLTAGE(9, 16, XADC_REG_VAUX(0), NULL, false),
  829. XADC_CHAN_VOLTAGE(10, 17, XADC_REG_VAUX(1), NULL, false),
  830. XADC_CHAN_VOLTAGE(11, 18, XADC_REG_VAUX(2), NULL, false),
  831. XADC_CHAN_VOLTAGE(12, 19, XADC_REG_VAUX(3), NULL, false),
  832. XADC_CHAN_VOLTAGE(13, 20, XADC_REG_VAUX(4), NULL, false),
  833. XADC_CHAN_VOLTAGE(14, 21, XADC_REG_VAUX(5), NULL, false),
  834. XADC_CHAN_VOLTAGE(15, 22, XADC_REG_VAUX(6), NULL, false),
  835. XADC_CHAN_VOLTAGE(16, 23, XADC_REG_VAUX(7), NULL, false),
  836. XADC_CHAN_VOLTAGE(17, 24, XADC_REG_VAUX(8), NULL, false),
  837. XADC_CHAN_VOLTAGE(18, 25, XADC_REG_VAUX(9), NULL, false),
  838. XADC_CHAN_VOLTAGE(19, 26, XADC_REG_VAUX(10), NULL, false),
  839. XADC_CHAN_VOLTAGE(20, 27, XADC_REG_VAUX(11), NULL, false),
  840. XADC_CHAN_VOLTAGE(21, 28, XADC_REG_VAUX(12), NULL, false),
  841. XADC_CHAN_VOLTAGE(22, 29, XADC_REG_VAUX(13), NULL, false),
  842. XADC_CHAN_VOLTAGE(23, 30, XADC_REG_VAUX(14), NULL, false),
  843. XADC_CHAN_VOLTAGE(24, 31, XADC_REG_VAUX(15), NULL, false),
  844. };
  845. static const struct iio_info xadc_info = {
  846. .read_raw = &xadc_read_raw,
  847. .write_raw = &xadc_write_raw,
  848. .read_event_config = &xadc_read_event_config,
  849. .write_event_config = &xadc_write_event_config,
  850. .read_event_value = &xadc_read_event_value,
  851. .write_event_value = &xadc_write_event_value,
  852. .update_scan_mode = &xadc_update_scan_mode,
  853. .driver_module = THIS_MODULE,
  854. };
  855. static const struct of_device_id xadc_of_match_table[] = {
  856. { .compatible = "xlnx,zynq-xadc-1.00.a", (void *)&xadc_zynq_ops },
  857. { .compatible = "xlnx,axi-xadc-1.00.a", (void *)&xadc_axi_ops },
  858. { },
  859. };
  860. MODULE_DEVICE_TABLE(of, xadc_of_match_table);
  861. static int xadc_parse_dt(struct iio_dev *indio_dev, struct device_node *np,
  862. unsigned int *conf)
  863. {
  864. struct xadc *xadc = iio_priv(indio_dev);
  865. struct iio_chan_spec *channels, *chan;
  866. struct device_node *chan_node, *child;
  867. unsigned int num_channels;
  868. const char *external_mux;
  869. u32 ext_mux_chan;
  870. int reg;
  871. int ret;
  872. *conf = 0;
  873. ret = of_property_read_string(np, "xlnx,external-mux", &external_mux);
  874. if (ret < 0 || strcasecmp(external_mux, "none") == 0)
  875. xadc->external_mux_mode = XADC_EXTERNAL_MUX_NONE;
  876. else if (strcasecmp(external_mux, "single") == 0)
  877. xadc->external_mux_mode = XADC_EXTERNAL_MUX_SINGLE;
  878. else if (strcasecmp(external_mux, "dual") == 0)
  879. xadc->external_mux_mode = XADC_EXTERNAL_MUX_DUAL;
  880. else
  881. return -EINVAL;
  882. if (xadc->external_mux_mode != XADC_EXTERNAL_MUX_NONE) {
  883. ret = of_property_read_u32(np, "xlnx,external-mux-channel",
  884. &ext_mux_chan);
  885. if (ret < 0)
  886. return ret;
  887. if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_SINGLE) {
  888. if (ext_mux_chan == 0)
  889. ext_mux_chan = XADC_REG_VPVN;
  890. else if (ext_mux_chan <= 16)
  891. ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1);
  892. else
  893. return -EINVAL;
  894. } else {
  895. if (ext_mux_chan > 0 && ext_mux_chan <= 8)
  896. ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1);
  897. else
  898. return -EINVAL;
  899. }
  900. *conf |= XADC_CONF0_MUX | XADC_CONF0_CHAN(ext_mux_chan);
  901. }
  902. channels = kmemdup(xadc_channels, sizeof(xadc_channels), GFP_KERNEL);
  903. if (!channels)
  904. return -ENOMEM;
  905. num_channels = 9;
  906. chan = &channels[9];
  907. chan_node = of_get_child_by_name(np, "xlnx,channels");
  908. if (chan_node) {
  909. for_each_child_of_node(chan_node, child) {
  910. if (num_channels >= ARRAY_SIZE(xadc_channels)) {
  911. of_node_put(child);
  912. break;
  913. }
  914. ret = of_property_read_u32(child, "reg", &reg);
  915. if (ret || reg > 16)
  916. continue;
  917. if (of_property_read_bool(child, "xlnx,bipolar"))
  918. chan->scan_type.sign = 's';
  919. if (reg == 0) {
  920. chan->scan_index = 11;
  921. chan->address = XADC_REG_VPVN;
  922. } else {
  923. chan->scan_index = 15 + reg;
  924. chan->address = XADC_REG_VAUX(reg - 1);
  925. }
  926. num_channels++;
  927. chan++;
  928. }
  929. }
  930. of_node_put(chan_node);
  931. indio_dev->num_channels = num_channels;
  932. indio_dev->channels = krealloc(channels, sizeof(*channels) *
  933. num_channels, GFP_KERNEL);
  934. /* If we can't resize the channels array, just use the original */
  935. if (!indio_dev->channels)
  936. indio_dev->channels = channels;
  937. return 0;
  938. }
  939. static int xadc_probe(struct platform_device *pdev)
  940. {
  941. const struct of_device_id *id;
  942. struct iio_dev *indio_dev;
  943. unsigned int bipolar_mask;
  944. struct resource *mem;
  945. unsigned int conf0;
  946. struct xadc *xadc;
  947. int ret;
  948. int irq;
  949. int i;
  950. if (!pdev->dev.of_node)
  951. return -ENODEV;
  952. id = of_match_node(xadc_of_match_table, pdev->dev.of_node);
  953. if (!id)
  954. return -EINVAL;
  955. irq = platform_get_irq(pdev, 0);
  956. if (irq <= 0)
  957. return -ENXIO;
  958. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*xadc));
  959. if (!indio_dev)
  960. return -ENOMEM;
  961. xadc = iio_priv(indio_dev);
  962. xadc->ops = id->data;
  963. init_completion(&xadc->completion);
  964. mutex_init(&xadc->mutex);
  965. spin_lock_init(&xadc->lock);
  966. INIT_DELAYED_WORK(&xadc->zynq_unmask_work, xadc_zynq_unmask_worker);
  967. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  968. xadc->base = devm_ioremap_resource(&pdev->dev, mem);
  969. if (IS_ERR(xadc->base))
  970. return PTR_ERR(xadc->base);
  971. indio_dev->dev.parent = &pdev->dev;
  972. indio_dev->dev.of_node = pdev->dev.of_node;
  973. indio_dev->name = "xadc";
  974. indio_dev->modes = INDIO_DIRECT_MODE;
  975. indio_dev->info = &xadc_info;
  976. ret = xadc_parse_dt(indio_dev, pdev->dev.of_node, &conf0);
  977. if (ret)
  978. goto err_device_free;
  979. if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
  980. ret = iio_triggered_buffer_setup(indio_dev,
  981. &iio_pollfunc_store_time, &xadc_trigger_handler,
  982. &xadc_buffer_ops);
  983. if (ret)
  984. goto err_device_free;
  985. xadc->convst_trigger = xadc_alloc_trigger(indio_dev, "convst");
  986. if (IS_ERR(xadc->convst_trigger)) {
  987. ret = PTR_ERR(xadc->convst_trigger);
  988. goto err_triggered_buffer_cleanup;
  989. }
  990. xadc->samplerate_trigger = xadc_alloc_trigger(indio_dev,
  991. "samplerate");
  992. if (IS_ERR(xadc->samplerate_trigger)) {
  993. ret = PTR_ERR(xadc->samplerate_trigger);
  994. goto err_free_convst_trigger;
  995. }
  996. }
  997. xadc->clk = devm_clk_get(&pdev->dev, NULL);
  998. if (IS_ERR(xadc->clk)) {
  999. ret = PTR_ERR(xadc->clk);
  1000. goto err_free_samplerate_trigger;
  1001. }
  1002. clk_prepare_enable(xadc->clk);
  1003. ret = xadc->ops->setup(pdev, indio_dev, irq);
  1004. if (ret)
  1005. goto err_clk_disable_unprepare;
  1006. ret = request_irq(irq, xadc->ops->interrupt_handler, 0,
  1007. dev_name(&pdev->dev), indio_dev);
  1008. if (ret)
  1009. goto err_clk_disable_unprepare;
  1010. for (i = 0; i < 16; i++)
  1011. xadc_read_adc_reg(xadc, XADC_REG_THRESHOLD(i),
  1012. &xadc->threshold[i]);
  1013. ret = xadc_write_adc_reg(xadc, XADC_REG_CONF0, conf0);
  1014. if (ret)
  1015. goto err_free_irq;
  1016. bipolar_mask = 0;
  1017. for (i = 0; i < indio_dev->num_channels; i++) {
  1018. if (indio_dev->channels[i].scan_type.sign == 's')
  1019. bipolar_mask |= BIT(indio_dev->channels[i].scan_index);
  1020. }
  1021. ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(0), bipolar_mask);
  1022. if (ret)
  1023. goto err_free_irq;
  1024. ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(1),
  1025. bipolar_mask >> 16);
  1026. if (ret)
  1027. goto err_free_irq;
  1028. /* Disable all alarms */
  1029. xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_ALARM_MASK,
  1030. XADC_CONF1_ALARM_MASK);
  1031. /* Set thresholds to min/max */
  1032. for (i = 0; i < 16; i++) {
  1033. /*
  1034. * Set max voltage threshold and both temperature thresholds to
  1035. * 0xffff, min voltage threshold to 0.
  1036. */
  1037. if (i % 8 < 4 || i == 7)
  1038. xadc->threshold[i] = 0xffff;
  1039. else
  1040. xadc->threshold[i] = 0;
  1041. xadc_write_adc_reg(xadc, XADC_REG_THRESHOLD(i),
  1042. xadc->threshold[i]);
  1043. }
  1044. /* Go to non-buffered mode */
  1045. xadc_postdisable(indio_dev);
  1046. ret = iio_device_register(indio_dev);
  1047. if (ret)
  1048. goto err_free_irq;
  1049. platform_set_drvdata(pdev, indio_dev);
  1050. return 0;
  1051. err_free_irq:
  1052. free_irq(irq, indio_dev);
  1053. err_clk_disable_unprepare:
  1054. clk_disable_unprepare(xadc->clk);
  1055. err_free_samplerate_trigger:
  1056. if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
  1057. iio_trigger_free(xadc->samplerate_trigger);
  1058. err_free_convst_trigger:
  1059. if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
  1060. iio_trigger_free(xadc->convst_trigger);
  1061. err_triggered_buffer_cleanup:
  1062. if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
  1063. iio_triggered_buffer_cleanup(indio_dev);
  1064. err_device_free:
  1065. kfree(indio_dev->channels);
  1066. return ret;
  1067. }
  1068. static int xadc_remove(struct platform_device *pdev)
  1069. {
  1070. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  1071. struct xadc *xadc = iio_priv(indio_dev);
  1072. int irq = platform_get_irq(pdev, 0);
  1073. iio_device_unregister(indio_dev);
  1074. if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
  1075. iio_trigger_free(xadc->samplerate_trigger);
  1076. iio_trigger_free(xadc->convst_trigger);
  1077. iio_triggered_buffer_cleanup(indio_dev);
  1078. }
  1079. free_irq(irq, indio_dev);
  1080. clk_disable_unprepare(xadc->clk);
  1081. cancel_delayed_work(&xadc->zynq_unmask_work);
  1082. kfree(xadc->data);
  1083. kfree(indio_dev->channels);
  1084. return 0;
  1085. }
  1086. static struct platform_driver xadc_driver = {
  1087. .probe = xadc_probe,
  1088. .remove = xadc_remove,
  1089. .driver = {
  1090. .name = "xadc",
  1091. .of_match_table = xadc_of_match_table,
  1092. },
  1093. };
  1094. module_platform_driver(xadc_driver);
  1095. MODULE_LICENSE("GPL v2");
  1096. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  1097. MODULE_DESCRIPTION("Xilinx XADC IIO driver");