i2c-at91.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * i2c Support for Atmel's AT91 Two-Wire Interface (TWI)
  3. *
  4. * Copyright (C) 2011 Weinmann Medical GmbH
  5. * Author: Nikolaus Voss <n.voss@weinmann.de>
  6. *
  7. * Evolved from original work by:
  8. * Copyright (C) 2004 Rick Bronson
  9. * Converted to 2.6 by Andrew Victor <andrew@sanpeople.com>
  10. *
  11. * Borrowed heavily from original work by:
  12. * Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/completion.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/err.h>
  24. #include <linux/i2c.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/module.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/platform_data/dma-atmel.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/pinctrl/consumer.h>
  35. #define DEFAULT_TWI_CLK_HZ 100000 /* max 400 Kbits/s */
  36. #define AT91_I2C_TIMEOUT msecs_to_jiffies(100) /* transfer timeout */
  37. #define AT91_I2C_DMA_THRESHOLD 8 /* enable DMA if transfer size is bigger than this threshold */
  38. #define AUTOSUSPEND_TIMEOUT 2000
  39. #define AT91_I2C_MAX_ALT_CMD_DATA_SIZE 256
  40. /* AT91 TWI register definitions */
  41. #define AT91_TWI_CR 0x0000 /* Control Register */
  42. #define AT91_TWI_START BIT(0) /* Send a Start Condition */
  43. #define AT91_TWI_STOP BIT(1) /* Send a Stop Condition */
  44. #define AT91_TWI_MSEN BIT(2) /* Master Transfer Enable */
  45. #define AT91_TWI_MSDIS BIT(3) /* Master Transfer Disable */
  46. #define AT91_TWI_SVEN BIT(4) /* Slave Transfer Enable */
  47. #define AT91_TWI_SVDIS BIT(5) /* Slave Transfer Disable */
  48. #define AT91_TWI_QUICK BIT(6) /* SMBus quick command */
  49. #define AT91_TWI_SWRST BIT(7) /* Software Reset */
  50. #define AT91_TWI_ACMEN BIT(16) /* Alternative Command Mode Enable */
  51. #define AT91_TWI_ACMDIS BIT(17) /* Alternative Command Mode Disable */
  52. #define AT91_TWI_THRCLR BIT(24) /* Transmit Holding Register Clear */
  53. #define AT91_TWI_RHRCLR BIT(25) /* Receive Holding Register Clear */
  54. #define AT91_TWI_LOCKCLR BIT(26) /* Lock Clear */
  55. #define AT91_TWI_FIFOEN BIT(28) /* FIFO Enable */
  56. #define AT91_TWI_FIFODIS BIT(29) /* FIFO Disable */
  57. #define AT91_TWI_MMR 0x0004 /* Master Mode Register */
  58. #define AT91_TWI_IADRSZ_1 0x0100 /* Internal Device Address Size */
  59. #define AT91_TWI_MREAD BIT(12) /* Master Read Direction */
  60. #define AT91_TWI_IADR 0x000c /* Internal Address Register */
  61. #define AT91_TWI_CWGR 0x0010 /* Clock Waveform Generator Reg */
  62. #define AT91_TWI_CWGR_HOLD_MAX 0x1f
  63. #define AT91_TWI_CWGR_HOLD(x) (((x) & AT91_TWI_CWGR_HOLD_MAX) << 24)
  64. #define AT91_TWI_SR 0x0020 /* Status Register */
  65. #define AT91_TWI_TXCOMP BIT(0) /* Transmission Complete */
  66. #define AT91_TWI_RXRDY BIT(1) /* Receive Holding Register Ready */
  67. #define AT91_TWI_TXRDY BIT(2) /* Transmit Holding Register Ready */
  68. #define AT91_TWI_OVRE BIT(6) /* Overrun Error */
  69. #define AT91_TWI_UNRE BIT(7) /* Underrun Error */
  70. #define AT91_TWI_NACK BIT(8) /* Not Acknowledged */
  71. #define AT91_TWI_LOCK BIT(23) /* TWI Lock due to Frame Errors */
  72. #define AT91_TWI_INT_MASK \
  73. (AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY | AT91_TWI_NACK)
  74. #define AT91_TWI_IER 0x0024 /* Interrupt Enable Register */
  75. #define AT91_TWI_IDR 0x0028 /* Interrupt Disable Register */
  76. #define AT91_TWI_IMR 0x002c /* Interrupt Mask Register */
  77. #define AT91_TWI_RHR 0x0030 /* Receive Holding Register */
  78. #define AT91_TWI_THR 0x0034 /* Transmit Holding Register */
  79. #define AT91_TWI_ACR 0x0040 /* Alternative Command Register */
  80. #define AT91_TWI_ACR_DATAL(len) ((len) & 0xff)
  81. #define AT91_TWI_ACR_DIR BIT(8)
  82. #define AT91_TWI_FMR 0x0050 /* FIFO Mode Register */
  83. #define AT91_TWI_FMR_TXRDYM(mode) (((mode) & 0x3) << 0)
  84. #define AT91_TWI_FMR_TXRDYM_MASK (0x3 << 0)
  85. #define AT91_TWI_FMR_RXRDYM(mode) (((mode) & 0x3) << 4)
  86. #define AT91_TWI_FMR_RXRDYM_MASK (0x3 << 4)
  87. #define AT91_TWI_ONE_DATA 0x0
  88. #define AT91_TWI_TWO_DATA 0x1
  89. #define AT91_TWI_FOUR_DATA 0x2
  90. #define AT91_TWI_FLR 0x0054 /* FIFO Level Register */
  91. #define AT91_TWI_FSR 0x0060 /* FIFO Status Register */
  92. #define AT91_TWI_FIER 0x0064 /* FIFO Interrupt Enable Register */
  93. #define AT91_TWI_FIDR 0x0068 /* FIFO Interrupt Disable Register */
  94. #define AT91_TWI_FIMR 0x006c /* FIFO Interrupt Mask Register */
  95. #define AT91_TWI_VER 0x00fc /* Version Register */
  96. struct at91_twi_pdata {
  97. unsigned clk_max_div;
  98. unsigned clk_offset;
  99. bool has_unre_flag;
  100. bool has_alt_cmd;
  101. bool has_hold_field;
  102. struct at_dma_slave dma_slave;
  103. };
  104. struct at91_twi_dma {
  105. struct dma_chan *chan_rx;
  106. struct dma_chan *chan_tx;
  107. struct scatterlist sg[2];
  108. struct dma_async_tx_descriptor *data_desc;
  109. enum dma_data_direction direction;
  110. bool buf_mapped;
  111. bool xfer_in_progress;
  112. };
  113. struct at91_twi_dev {
  114. struct device *dev;
  115. void __iomem *base;
  116. struct completion cmd_complete;
  117. struct clk *clk;
  118. u8 *buf;
  119. size_t buf_len;
  120. struct i2c_msg *msg;
  121. int irq;
  122. unsigned imr;
  123. unsigned transfer_status;
  124. struct i2c_adapter adapter;
  125. unsigned twi_cwgr_reg;
  126. struct at91_twi_pdata *pdata;
  127. bool use_dma;
  128. bool use_alt_cmd;
  129. bool recv_len_abort;
  130. u32 fifo_size;
  131. struct at91_twi_dma dma;
  132. };
  133. static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg)
  134. {
  135. return readl_relaxed(dev->base + reg);
  136. }
  137. static void at91_twi_write(struct at91_twi_dev *dev, unsigned reg, unsigned val)
  138. {
  139. writel_relaxed(val, dev->base + reg);
  140. }
  141. static void at91_disable_twi_interrupts(struct at91_twi_dev *dev)
  142. {
  143. at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_INT_MASK);
  144. }
  145. static void at91_twi_irq_save(struct at91_twi_dev *dev)
  146. {
  147. dev->imr = at91_twi_read(dev, AT91_TWI_IMR) & AT91_TWI_INT_MASK;
  148. at91_disable_twi_interrupts(dev);
  149. }
  150. static void at91_twi_irq_restore(struct at91_twi_dev *dev)
  151. {
  152. at91_twi_write(dev, AT91_TWI_IER, dev->imr);
  153. }
  154. static void at91_init_twi_bus(struct at91_twi_dev *dev)
  155. {
  156. at91_disable_twi_interrupts(dev);
  157. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
  158. /* FIFO should be enabled immediately after the software reset */
  159. if (dev->fifo_size)
  160. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN);
  161. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
  162. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
  163. at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
  164. }
  165. /*
  166. * Calculate symmetric clock as stated in datasheet:
  167. * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
  168. */
  169. static void at91_calc_twi_clock(struct at91_twi_dev *dev, int twi_clk)
  170. {
  171. int ckdiv, cdiv, div, hold = 0;
  172. struct at91_twi_pdata *pdata = dev->pdata;
  173. int offset = pdata->clk_offset;
  174. int max_ckdiv = pdata->clk_max_div;
  175. u32 twd_hold_time_ns = 0;
  176. div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk),
  177. 2 * twi_clk) - offset);
  178. ckdiv = fls(div >> 8);
  179. cdiv = div >> ckdiv;
  180. if (ckdiv > max_ckdiv) {
  181. dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
  182. ckdiv, max_ckdiv);
  183. ckdiv = max_ckdiv;
  184. cdiv = 255;
  185. }
  186. if (pdata->has_hold_field) {
  187. of_property_read_u32(dev->dev->of_node, "i2c-sda-hold-time-ns",
  188. &twd_hold_time_ns);
  189. /*
  190. * hold time = HOLD + 3 x T_peripheral_clock
  191. * Use clk rate in kHz to prevent overflows when computing
  192. * hold.
  193. */
  194. hold = DIV_ROUND_UP(twd_hold_time_ns
  195. * (clk_get_rate(dev->clk) / 1000), 1000000);
  196. hold -= 3;
  197. if (hold < 0)
  198. hold = 0;
  199. if (hold > AT91_TWI_CWGR_HOLD_MAX) {
  200. dev_warn(dev->dev,
  201. "HOLD field set to its maximum value (%d instead of %d)\n",
  202. AT91_TWI_CWGR_HOLD_MAX, hold);
  203. hold = AT91_TWI_CWGR_HOLD_MAX;
  204. }
  205. }
  206. dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv
  207. | AT91_TWI_CWGR_HOLD(hold);
  208. dev_dbg(dev->dev, "cdiv %d ckdiv %d hold %d (%d ns)\n",
  209. cdiv, ckdiv, hold, twd_hold_time_ns);
  210. }
  211. static void at91_twi_dma_cleanup(struct at91_twi_dev *dev)
  212. {
  213. struct at91_twi_dma *dma = &dev->dma;
  214. at91_twi_irq_save(dev);
  215. if (dma->xfer_in_progress) {
  216. if (dma->direction == DMA_FROM_DEVICE)
  217. dmaengine_terminate_all(dma->chan_rx);
  218. else
  219. dmaengine_terminate_all(dma->chan_tx);
  220. dma->xfer_in_progress = false;
  221. }
  222. if (dma->buf_mapped) {
  223. dma_unmap_single(dev->dev, sg_dma_address(&dma->sg[0]),
  224. dev->buf_len, dma->direction);
  225. dma->buf_mapped = false;
  226. }
  227. at91_twi_irq_restore(dev);
  228. }
  229. static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
  230. {
  231. if (!dev->buf_len)
  232. return;
  233. /* 8bit write works with and without FIFO */
  234. writeb_relaxed(*dev->buf, dev->base + AT91_TWI_THR);
  235. /* send stop when last byte has been written */
  236. if (--dev->buf_len == 0) {
  237. if (!dev->use_alt_cmd)
  238. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
  239. at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_TXRDY);
  240. }
  241. dev_dbg(dev->dev, "wrote 0x%x, to go %zu\n", *dev->buf, dev->buf_len);
  242. ++dev->buf;
  243. }
  244. static void at91_twi_write_data_dma_callback(void *data)
  245. {
  246. struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
  247. dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
  248. dev->buf_len, DMA_TO_DEVICE);
  249. /*
  250. * When this callback is called, THR/TX FIFO is likely not to be empty
  251. * yet. So we have to wait for TXCOMP or NACK bits to be set into the
  252. * Status Register to be sure that the STOP bit has been sent and the
  253. * transfer is completed. The NACK interrupt has already been enabled,
  254. * we just have to enable TXCOMP one.
  255. */
  256. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
  257. if (!dev->use_alt_cmd)
  258. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
  259. }
  260. static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
  261. {
  262. dma_addr_t dma_addr;
  263. struct dma_async_tx_descriptor *txdesc;
  264. struct at91_twi_dma *dma = &dev->dma;
  265. struct dma_chan *chan_tx = dma->chan_tx;
  266. unsigned int sg_len = 1;
  267. if (!dev->buf_len)
  268. return;
  269. dma->direction = DMA_TO_DEVICE;
  270. at91_twi_irq_save(dev);
  271. dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len,
  272. DMA_TO_DEVICE);
  273. if (dma_mapping_error(dev->dev, dma_addr)) {
  274. dev_err(dev->dev, "dma map failed\n");
  275. return;
  276. }
  277. dma->buf_mapped = true;
  278. at91_twi_irq_restore(dev);
  279. if (dev->fifo_size) {
  280. size_t part1_len, part2_len;
  281. struct scatterlist *sg;
  282. unsigned fifo_mr;
  283. sg_len = 0;
  284. part1_len = dev->buf_len & ~0x3;
  285. if (part1_len) {
  286. sg = &dma->sg[sg_len++];
  287. sg_dma_len(sg) = part1_len;
  288. sg_dma_address(sg) = dma_addr;
  289. }
  290. part2_len = dev->buf_len & 0x3;
  291. if (part2_len) {
  292. sg = &dma->sg[sg_len++];
  293. sg_dma_len(sg) = part2_len;
  294. sg_dma_address(sg) = dma_addr + part1_len;
  295. }
  296. /*
  297. * DMA controller is triggered when at least 4 data can be
  298. * written into the TX FIFO
  299. */
  300. fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
  301. fifo_mr &= ~AT91_TWI_FMR_TXRDYM_MASK;
  302. fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_FOUR_DATA);
  303. at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
  304. } else {
  305. sg_dma_len(&dma->sg[0]) = dev->buf_len;
  306. sg_dma_address(&dma->sg[0]) = dma_addr;
  307. }
  308. txdesc = dmaengine_prep_slave_sg(chan_tx, dma->sg, sg_len,
  309. DMA_MEM_TO_DEV,
  310. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  311. if (!txdesc) {
  312. dev_err(dev->dev, "dma prep slave sg failed\n");
  313. goto error;
  314. }
  315. txdesc->callback = at91_twi_write_data_dma_callback;
  316. txdesc->callback_param = dev;
  317. dma->xfer_in_progress = true;
  318. dmaengine_submit(txdesc);
  319. dma_async_issue_pending(chan_tx);
  320. return;
  321. error:
  322. at91_twi_dma_cleanup(dev);
  323. }
  324. static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
  325. {
  326. /*
  327. * If we are in this case, it means there is garbage data in RHR, so
  328. * delete them.
  329. */
  330. if (!dev->buf_len) {
  331. at91_twi_read(dev, AT91_TWI_RHR);
  332. return;
  333. }
  334. /* 8bit read works with and without FIFO */
  335. *dev->buf = readb_relaxed(dev->base + AT91_TWI_RHR);
  336. --dev->buf_len;
  337. /* return if aborting, we only needed to read RHR to clear RXRDY*/
  338. if (dev->recv_len_abort)
  339. return;
  340. /* handle I2C_SMBUS_BLOCK_DATA */
  341. if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
  342. /* ensure length byte is a valid value */
  343. if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) {
  344. dev->msg->flags &= ~I2C_M_RECV_LEN;
  345. dev->buf_len += *dev->buf;
  346. dev->msg->len = dev->buf_len + 1;
  347. dev_dbg(dev->dev, "received block length %zu\n",
  348. dev->buf_len);
  349. } else {
  350. /* abort and send the stop by reading one more byte */
  351. dev->recv_len_abort = true;
  352. dev->buf_len = 1;
  353. }
  354. }
  355. /* send stop if second but last byte has been read */
  356. if (!dev->use_alt_cmd && dev->buf_len == 1)
  357. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
  358. dev_dbg(dev->dev, "read 0x%x, to go %zu\n", *dev->buf, dev->buf_len);
  359. ++dev->buf;
  360. }
  361. static void at91_twi_read_data_dma_callback(void *data)
  362. {
  363. struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
  364. unsigned ier = AT91_TWI_TXCOMP;
  365. dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
  366. dev->buf_len, DMA_FROM_DEVICE);
  367. if (!dev->use_alt_cmd) {
  368. /* The last two bytes have to be read without using dma */
  369. dev->buf += dev->buf_len - 2;
  370. dev->buf_len = 2;
  371. ier |= AT91_TWI_RXRDY;
  372. }
  373. at91_twi_write(dev, AT91_TWI_IER, ier);
  374. }
  375. static void at91_twi_read_data_dma(struct at91_twi_dev *dev)
  376. {
  377. dma_addr_t dma_addr;
  378. struct dma_async_tx_descriptor *rxdesc;
  379. struct at91_twi_dma *dma = &dev->dma;
  380. struct dma_chan *chan_rx = dma->chan_rx;
  381. size_t buf_len;
  382. buf_len = (dev->use_alt_cmd) ? dev->buf_len : dev->buf_len - 2;
  383. dma->direction = DMA_FROM_DEVICE;
  384. /* Keep in mind that we won't use dma to read the last two bytes */
  385. at91_twi_irq_save(dev);
  386. dma_addr = dma_map_single(dev->dev, dev->buf, buf_len, DMA_FROM_DEVICE);
  387. if (dma_mapping_error(dev->dev, dma_addr)) {
  388. dev_err(dev->dev, "dma map failed\n");
  389. return;
  390. }
  391. dma->buf_mapped = true;
  392. at91_twi_irq_restore(dev);
  393. if (dev->fifo_size && IS_ALIGNED(buf_len, 4)) {
  394. unsigned fifo_mr;
  395. /*
  396. * DMA controller is triggered when at least 4 data can be
  397. * read from the RX FIFO
  398. */
  399. fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
  400. fifo_mr &= ~AT91_TWI_FMR_RXRDYM_MASK;
  401. fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_FOUR_DATA);
  402. at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
  403. }
  404. sg_dma_len(&dma->sg[0]) = buf_len;
  405. sg_dma_address(&dma->sg[0]) = dma_addr;
  406. rxdesc = dmaengine_prep_slave_sg(chan_rx, dma->sg, 1, DMA_DEV_TO_MEM,
  407. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  408. if (!rxdesc) {
  409. dev_err(dev->dev, "dma prep slave sg failed\n");
  410. goto error;
  411. }
  412. rxdesc->callback = at91_twi_read_data_dma_callback;
  413. rxdesc->callback_param = dev;
  414. dma->xfer_in_progress = true;
  415. dmaengine_submit(rxdesc);
  416. dma_async_issue_pending(dma->chan_rx);
  417. return;
  418. error:
  419. at91_twi_dma_cleanup(dev);
  420. }
  421. static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
  422. {
  423. struct at91_twi_dev *dev = dev_id;
  424. const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
  425. const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
  426. if (!irqstatus)
  427. return IRQ_NONE;
  428. /*
  429. * In reception, the behavior of the twi device (before sama5d2) is
  430. * weird. There is some magic about RXRDY flag! When a data has been
  431. * almost received, the reception of a new one is anticipated if there
  432. * is no stop command to send. That is the reason why ask for sending
  433. * the stop command not on the last data but on the second last one.
  434. *
  435. * Unfortunately, we could still have the RXRDY flag set even if the
  436. * transfer is done and we have read the last data. It might happen
  437. * when the i2c slave device sends too quickly data after receiving the
  438. * ack from the master. The data has been almost received before having
  439. * the order to send stop. In this case, sending the stop command could
  440. * cause a RXRDY interrupt with a TXCOMP one. It is better to manage
  441. * the RXRDY interrupt first in order to not keep garbage data in the
  442. * Receive Holding Register for the next transfer.
  443. */
  444. if (irqstatus & AT91_TWI_RXRDY) {
  445. /*
  446. * Read all available bytes at once by polling RXRDY usable w/
  447. * and w/o FIFO. With FIFO enabled we could also read RXFL and
  448. * avoid polling RXRDY.
  449. */
  450. do {
  451. at91_twi_read_next_byte(dev);
  452. } while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);
  453. }
  454. /*
  455. * When a NACK condition is detected, the I2C controller sets the NACK,
  456. * TXCOMP and TXRDY bits all together in the Status Register (SR).
  457. *
  458. * 1 - Handling NACK errors with CPU write transfer.
  459. *
  460. * In such case, we should not write the next byte into the Transmit
  461. * Holding Register (THR) otherwise the I2C controller would start a new
  462. * transfer and the I2C slave is likely to reply by another NACK.
  463. *
  464. * 2 - Handling NACK errors with DMA write transfer.
  465. *
  466. * By setting the TXRDY bit in the SR, the I2C controller also triggers
  467. * the DMA controller to write the next data into the THR. Then the
  468. * result depends on the hardware version of the I2C controller.
  469. *
  470. * 2a - Without support of the Alternative Command mode.
  471. *
  472. * This is the worst case: the DMA controller is triggered to write the
  473. * next data into the THR, hence starting a new transfer: the I2C slave
  474. * is likely to reply by another NACK.
  475. * Concurrently, this interrupt handler is likely to be called to manage
  476. * the first NACK before the I2C controller detects the second NACK and
  477. * sets once again the NACK bit into the SR.
  478. * When handling the first NACK, this interrupt handler disables the I2C
  479. * controller interruptions, especially the NACK interrupt.
  480. * Hence, the NACK bit is pending into the SR. This is why we should
  481. * read the SR to clear all pending interrupts at the beginning of
  482. * at91_do_twi_transfer() before actually starting a new transfer.
  483. *
  484. * 2b - With support of the Alternative Command mode.
  485. *
  486. * When a NACK condition is detected, the I2C controller also locks the
  487. * THR (and sets the LOCK bit in the SR): even though the DMA controller
  488. * is triggered by the TXRDY bit to write the next data into the THR,
  489. * this data actually won't go on the I2C bus hence a second NACK is not
  490. * generated.
  491. */
  492. if (irqstatus & (AT91_TWI_TXCOMP | AT91_TWI_NACK)) {
  493. at91_disable_twi_interrupts(dev);
  494. complete(&dev->cmd_complete);
  495. } else if (irqstatus & AT91_TWI_TXRDY) {
  496. at91_twi_write_next_byte(dev);
  497. }
  498. /* catch error flags */
  499. dev->transfer_status |= status;
  500. return IRQ_HANDLED;
  501. }
  502. static int at91_do_twi_transfer(struct at91_twi_dev *dev)
  503. {
  504. int ret;
  505. unsigned long time_left;
  506. bool has_unre_flag = dev->pdata->has_unre_flag;
  507. bool has_alt_cmd = dev->pdata->has_alt_cmd;
  508. /*
  509. * WARNING: the TXCOMP bit in the Status Register is NOT a clear on
  510. * read flag but shows the state of the transmission at the time the
  511. * Status Register is read. According to the programmer datasheet,
  512. * TXCOMP is set when both holding register and internal shifter are
  513. * empty and STOP condition has been sent.
  514. * Consequently, we should enable NACK interrupt rather than TXCOMP to
  515. * detect transmission failure.
  516. * Indeed let's take the case of an i2c write command using DMA.
  517. * Whenever the slave doesn't acknowledge a byte, the LOCK, NACK and
  518. * TXCOMP bits are set together into the Status Register.
  519. * LOCK is a clear on write bit, which is set to prevent the DMA
  520. * controller from sending new data on the i2c bus after a NACK
  521. * condition has happened. Once locked, this i2c peripheral stops
  522. * triggering the DMA controller for new data but it is more than
  523. * likely that a new DMA transaction is already in progress, writing
  524. * into the Transmit Holding Register. Since the peripheral is locked,
  525. * these new data won't be sent to the i2c bus but they will remain
  526. * into the Transmit Holding Register, so TXCOMP bit is cleared.
  527. * Then when the interrupt handler is called, the Status Register is
  528. * read: the TXCOMP bit is clear but NACK bit is still set. The driver
  529. * manage the error properly, without waiting for timeout.
  530. * This case can be reproduced easyly when writing into an at24 eeprom.
  531. *
  532. * Besides, the TXCOMP bit is already set before the i2c transaction
  533. * has been started. For read transactions, this bit is cleared when
  534. * writing the START bit into the Control Register. So the
  535. * corresponding interrupt can safely be enabled just after.
  536. * However for write transactions managed by the CPU, we first write
  537. * into THR, so TXCOMP is cleared. Then we can safely enable TXCOMP
  538. * interrupt. If TXCOMP interrupt were enabled before writing into THR,
  539. * the interrupt handler would be called immediately and the i2c command
  540. * would be reported as completed.
  541. * Also when a write transaction is managed by the DMA controller,
  542. * enabling the TXCOMP interrupt in this function may lead to a race
  543. * condition since we don't know whether the TXCOMP interrupt is enabled
  544. * before or after the DMA has started to write into THR. So the TXCOMP
  545. * interrupt is enabled later by at91_twi_write_data_dma_callback().
  546. * Immediately after in that DMA callback, if the alternative command
  547. * mode is not used, we still need to send the STOP condition manually
  548. * writing the corresponding bit into the Control Register.
  549. */
  550. dev_dbg(dev->dev, "transfer: %s %zu bytes.\n",
  551. (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
  552. reinit_completion(&dev->cmd_complete);
  553. dev->transfer_status = 0;
  554. /* Clear pending interrupts, such as NACK. */
  555. at91_twi_read(dev, AT91_TWI_SR);
  556. if (dev->fifo_size) {
  557. unsigned fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
  558. /* Reset FIFO mode register */
  559. fifo_mr &= ~(AT91_TWI_FMR_TXRDYM_MASK |
  560. AT91_TWI_FMR_RXRDYM_MASK);
  561. fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_ONE_DATA);
  562. fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_ONE_DATA);
  563. at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
  564. /* Flush FIFOs */
  565. at91_twi_write(dev, AT91_TWI_CR,
  566. AT91_TWI_THRCLR | AT91_TWI_RHRCLR);
  567. }
  568. if (!dev->buf_len) {
  569. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK);
  570. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
  571. } else if (dev->msg->flags & I2C_M_RD) {
  572. unsigned start_flags = AT91_TWI_START;
  573. /* if only one byte is to be read, immediately stop transfer */
  574. if (!dev->use_alt_cmd && dev->buf_len <= 1 &&
  575. !(dev->msg->flags & I2C_M_RECV_LEN))
  576. start_flags |= AT91_TWI_STOP;
  577. at91_twi_write(dev, AT91_TWI_CR, start_flags);
  578. /*
  579. * When using dma without alternative command mode, the last
  580. * byte has to be read manually in order to not send the stop
  581. * command too late and then to receive extra data.
  582. * In practice, there are some issues if you use the dma to
  583. * read n-1 bytes because of latency.
  584. * Reading n-2 bytes with dma and the two last ones manually
  585. * seems to be the best solution.
  586. */
  587. if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
  588. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
  589. at91_twi_read_data_dma(dev);
  590. } else {
  591. at91_twi_write(dev, AT91_TWI_IER,
  592. AT91_TWI_TXCOMP |
  593. AT91_TWI_NACK |
  594. AT91_TWI_RXRDY);
  595. }
  596. } else {
  597. if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
  598. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
  599. at91_twi_write_data_dma(dev);
  600. } else {
  601. at91_twi_write_next_byte(dev);
  602. at91_twi_write(dev, AT91_TWI_IER,
  603. AT91_TWI_TXCOMP | AT91_TWI_NACK |
  604. (dev->buf_len ? AT91_TWI_TXRDY : 0));
  605. }
  606. }
  607. time_left = wait_for_completion_timeout(&dev->cmd_complete,
  608. dev->adapter.timeout);
  609. if (time_left == 0) {
  610. dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
  611. dev_err(dev->dev, "controller timed out\n");
  612. at91_init_twi_bus(dev);
  613. ret = -ETIMEDOUT;
  614. goto error;
  615. }
  616. if (dev->transfer_status & AT91_TWI_NACK) {
  617. dev_dbg(dev->dev, "received nack\n");
  618. ret = -EREMOTEIO;
  619. goto error;
  620. }
  621. if (dev->transfer_status & AT91_TWI_OVRE) {
  622. dev_err(dev->dev, "overrun while reading\n");
  623. ret = -EIO;
  624. goto error;
  625. }
  626. if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
  627. dev_err(dev->dev, "underrun while writing\n");
  628. ret = -EIO;
  629. goto error;
  630. }
  631. if ((has_alt_cmd || dev->fifo_size) &&
  632. (dev->transfer_status & AT91_TWI_LOCK)) {
  633. dev_err(dev->dev, "tx locked\n");
  634. ret = -EIO;
  635. goto error;
  636. }
  637. if (dev->recv_len_abort) {
  638. dev_err(dev->dev, "invalid smbus block length recvd\n");
  639. ret = -EPROTO;
  640. goto error;
  641. }
  642. dev_dbg(dev->dev, "transfer complete\n");
  643. return 0;
  644. error:
  645. /* first stop DMA transfer if still in progress */
  646. at91_twi_dma_cleanup(dev);
  647. /* then flush THR/FIFO and unlock TX if locked */
  648. if ((has_alt_cmd || dev->fifo_size) &&
  649. (dev->transfer_status & AT91_TWI_LOCK)) {
  650. dev_dbg(dev->dev, "unlock tx\n");
  651. at91_twi_write(dev, AT91_TWI_CR,
  652. AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
  653. }
  654. return ret;
  655. }
  656. static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
  657. {
  658. struct at91_twi_dev *dev = i2c_get_adapdata(adap);
  659. int ret;
  660. unsigned int_addr_flag = 0;
  661. struct i2c_msg *m_start = msg;
  662. bool is_read;
  663. dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
  664. ret = pm_runtime_get_sync(dev->dev);
  665. if (ret < 0)
  666. goto out;
  667. if (num == 2) {
  668. int internal_address = 0;
  669. int i;
  670. /* 1st msg is put into the internal address, start with 2nd */
  671. m_start = &msg[1];
  672. for (i = 0; i < msg->len; ++i) {
  673. const unsigned addr = msg->buf[msg->len - 1 - i];
  674. internal_address |= addr << (8 * i);
  675. int_addr_flag += AT91_TWI_IADRSZ_1;
  676. }
  677. at91_twi_write(dev, AT91_TWI_IADR, internal_address);
  678. }
  679. dev->use_alt_cmd = false;
  680. is_read = (m_start->flags & I2C_M_RD);
  681. if (dev->pdata->has_alt_cmd) {
  682. if (m_start->len > 0 &&
  683. m_start->len < AT91_I2C_MAX_ALT_CMD_DATA_SIZE) {
  684. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMEN);
  685. at91_twi_write(dev, AT91_TWI_ACR,
  686. AT91_TWI_ACR_DATAL(m_start->len) |
  687. ((is_read) ? AT91_TWI_ACR_DIR : 0));
  688. dev->use_alt_cmd = true;
  689. } else {
  690. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMDIS);
  691. }
  692. }
  693. at91_twi_write(dev, AT91_TWI_MMR,
  694. (m_start->addr << 16) |
  695. int_addr_flag |
  696. ((!dev->use_alt_cmd && is_read) ? AT91_TWI_MREAD : 0));
  697. dev->buf_len = m_start->len;
  698. dev->buf = m_start->buf;
  699. dev->msg = m_start;
  700. dev->recv_len_abort = false;
  701. ret = at91_do_twi_transfer(dev);
  702. ret = (ret < 0) ? ret : num;
  703. out:
  704. pm_runtime_mark_last_busy(dev->dev);
  705. pm_runtime_put_autosuspend(dev->dev);
  706. return ret;
  707. }
  708. /*
  709. * The hardware can handle at most two messages concatenated by a
  710. * repeated start via it's internal address feature.
  711. */
  712. static const struct i2c_adapter_quirks at91_twi_quirks = {
  713. .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
  714. .max_comb_1st_msg_len = 3,
  715. };
  716. static u32 at91_twi_func(struct i2c_adapter *adapter)
  717. {
  718. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  719. | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  720. }
  721. static const struct i2c_algorithm at91_twi_algorithm = {
  722. .master_xfer = at91_twi_xfer,
  723. .functionality = at91_twi_func,
  724. };
  725. static struct at91_twi_pdata at91rm9200_config = {
  726. .clk_max_div = 5,
  727. .clk_offset = 3,
  728. .has_unre_flag = true,
  729. .has_alt_cmd = false,
  730. .has_hold_field = false,
  731. };
  732. static struct at91_twi_pdata at91sam9261_config = {
  733. .clk_max_div = 5,
  734. .clk_offset = 4,
  735. .has_unre_flag = false,
  736. .has_alt_cmd = false,
  737. .has_hold_field = false,
  738. };
  739. static struct at91_twi_pdata at91sam9260_config = {
  740. .clk_max_div = 7,
  741. .clk_offset = 4,
  742. .has_unre_flag = false,
  743. .has_alt_cmd = false,
  744. .has_hold_field = false,
  745. };
  746. static struct at91_twi_pdata at91sam9g20_config = {
  747. .clk_max_div = 7,
  748. .clk_offset = 4,
  749. .has_unre_flag = false,
  750. .has_alt_cmd = false,
  751. .has_hold_field = false,
  752. };
  753. static struct at91_twi_pdata at91sam9g10_config = {
  754. .clk_max_div = 7,
  755. .clk_offset = 4,
  756. .has_unre_flag = false,
  757. .has_alt_cmd = false,
  758. .has_hold_field = false,
  759. };
  760. static const struct platform_device_id at91_twi_devtypes[] = {
  761. {
  762. .name = "i2c-at91rm9200",
  763. .driver_data = (unsigned long) &at91rm9200_config,
  764. }, {
  765. .name = "i2c-at91sam9261",
  766. .driver_data = (unsigned long) &at91sam9261_config,
  767. }, {
  768. .name = "i2c-at91sam9260",
  769. .driver_data = (unsigned long) &at91sam9260_config,
  770. }, {
  771. .name = "i2c-at91sam9g20",
  772. .driver_data = (unsigned long) &at91sam9g20_config,
  773. }, {
  774. .name = "i2c-at91sam9g10",
  775. .driver_data = (unsigned long) &at91sam9g10_config,
  776. }, {
  777. /* sentinel */
  778. }
  779. };
  780. #if defined(CONFIG_OF)
  781. static struct at91_twi_pdata at91sam9x5_config = {
  782. .clk_max_div = 7,
  783. .clk_offset = 4,
  784. .has_unre_flag = false,
  785. .has_alt_cmd = false,
  786. .has_hold_field = false,
  787. };
  788. static struct at91_twi_pdata sama5d4_config = {
  789. .clk_max_div = 7,
  790. .clk_offset = 4,
  791. .has_unre_flag = false,
  792. .has_alt_cmd = false,
  793. .has_hold_field = true,
  794. };
  795. static struct at91_twi_pdata sama5d2_config = {
  796. .clk_max_div = 7,
  797. .clk_offset = 3,
  798. .has_unre_flag = true,
  799. .has_alt_cmd = true,
  800. .has_hold_field = true,
  801. };
  802. static const struct of_device_id atmel_twi_dt_ids[] = {
  803. {
  804. .compatible = "atmel,at91rm9200-i2c",
  805. .data = &at91rm9200_config,
  806. } , {
  807. .compatible = "atmel,at91sam9260-i2c",
  808. .data = &at91sam9260_config,
  809. } , {
  810. .compatible = "atmel,at91sam9261-i2c",
  811. .data = &at91sam9261_config,
  812. } , {
  813. .compatible = "atmel,at91sam9g20-i2c",
  814. .data = &at91sam9g20_config,
  815. } , {
  816. .compatible = "atmel,at91sam9g10-i2c",
  817. .data = &at91sam9g10_config,
  818. }, {
  819. .compatible = "atmel,at91sam9x5-i2c",
  820. .data = &at91sam9x5_config,
  821. }, {
  822. .compatible = "atmel,sama5d4-i2c",
  823. .data = &sama5d4_config,
  824. }, {
  825. .compatible = "atmel,sama5d2-i2c",
  826. .data = &sama5d2_config,
  827. }, {
  828. /* sentinel */
  829. }
  830. };
  831. MODULE_DEVICE_TABLE(of, atmel_twi_dt_ids);
  832. #endif
  833. static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
  834. {
  835. int ret = 0;
  836. struct dma_slave_config slave_config;
  837. struct at91_twi_dma *dma = &dev->dma;
  838. enum dma_slave_buswidth addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  839. /*
  840. * The actual width of the access will be chosen in
  841. * dmaengine_prep_slave_sg():
  842. * for each buffer in the scatter-gather list, if its size is aligned
  843. * to addr_width then addr_width accesses will be performed to transfer
  844. * the buffer. On the other hand, if the buffer size is not aligned to
  845. * addr_width then the buffer is transferred using single byte accesses.
  846. * Please refer to the Atmel eXtended DMA controller driver.
  847. * When FIFOs are used, the TXRDYM threshold can always be set to
  848. * trigger the XDMAC when at least 4 data can be written into the TX
  849. * FIFO, even if single byte accesses are performed.
  850. * However the RXRDYM threshold must be set to fit the access width,
  851. * deduced from buffer length, so the XDMAC is triggered properly to
  852. * read data from the RX FIFO.
  853. */
  854. if (dev->fifo_size)
  855. addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  856. memset(&slave_config, 0, sizeof(slave_config));
  857. slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
  858. slave_config.src_addr_width = addr_width;
  859. slave_config.src_maxburst = 1;
  860. slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
  861. slave_config.dst_addr_width = addr_width;
  862. slave_config.dst_maxburst = 1;
  863. slave_config.device_fc = false;
  864. dma->chan_tx = dma_request_slave_channel_reason(dev->dev, "tx");
  865. if (IS_ERR(dma->chan_tx)) {
  866. ret = PTR_ERR(dma->chan_tx);
  867. dma->chan_tx = NULL;
  868. goto error;
  869. }
  870. dma->chan_rx = dma_request_slave_channel_reason(dev->dev, "rx");
  871. if (IS_ERR(dma->chan_rx)) {
  872. ret = PTR_ERR(dma->chan_rx);
  873. dma->chan_rx = NULL;
  874. goto error;
  875. }
  876. slave_config.direction = DMA_MEM_TO_DEV;
  877. if (dmaengine_slave_config(dma->chan_tx, &slave_config)) {
  878. dev_err(dev->dev, "failed to configure tx channel\n");
  879. ret = -EINVAL;
  880. goto error;
  881. }
  882. slave_config.direction = DMA_DEV_TO_MEM;
  883. if (dmaengine_slave_config(dma->chan_rx, &slave_config)) {
  884. dev_err(dev->dev, "failed to configure rx channel\n");
  885. ret = -EINVAL;
  886. goto error;
  887. }
  888. sg_init_table(dma->sg, 2);
  889. dma->buf_mapped = false;
  890. dma->xfer_in_progress = false;
  891. dev->use_dma = true;
  892. dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
  893. dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
  894. return ret;
  895. error:
  896. if (ret != -EPROBE_DEFER)
  897. dev_info(dev->dev, "can't get DMA channel, continue without DMA support\n");
  898. if (dma->chan_rx)
  899. dma_release_channel(dma->chan_rx);
  900. if (dma->chan_tx)
  901. dma_release_channel(dma->chan_tx);
  902. return ret;
  903. }
  904. static struct at91_twi_pdata *at91_twi_get_driver_data(
  905. struct platform_device *pdev)
  906. {
  907. if (pdev->dev.of_node) {
  908. const struct of_device_id *match;
  909. match = of_match_node(atmel_twi_dt_ids, pdev->dev.of_node);
  910. if (!match)
  911. return NULL;
  912. return (struct at91_twi_pdata *)match->data;
  913. }
  914. return (struct at91_twi_pdata *) platform_get_device_id(pdev)->driver_data;
  915. }
  916. static int at91_twi_probe(struct platform_device *pdev)
  917. {
  918. struct at91_twi_dev *dev;
  919. struct resource *mem;
  920. int rc;
  921. u32 phy_addr;
  922. u32 bus_clk_rate;
  923. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  924. if (!dev)
  925. return -ENOMEM;
  926. init_completion(&dev->cmd_complete);
  927. dev->dev = &pdev->dev;
  928. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  929. if (!mem)
  930. return -ENODEV;
  931. phy_addr = mem->start;
  932. dev->pdata = at91_twi_get_driver_data(pdev);
  933. if (!dev->pdata)
  934. return -ENODEV;
  935. dev->base = devm_ioremap_resource(&pdev->dev, mem);
  936. if (IS_ERR(dev->base))
  937. return PTR_ERR(dev->base);
  938. dev->irq = platform_get_irq(pdev, 0);
  939. if (dev->irq < 0)
  940. return dev->irq;
  941. rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
  942. dev_name(dev->dev), dev);
  943. if (rc) {
  944. dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
  945. return rc;
  946. }
  947. platform_set_drvdata(pdev, dev);
  948. dev->clk = devm_clk_get(dev->dev, NULL);
  949. if (IS_ERR(dev->clk)) {
  950. dev_err(dev->dev, "no clock defined\n");
  951. return -ENODEV;
  952. }
  953. rc = clk_prepare_enable(dev->clk);
  954. if (rc)
  955. return rc;
  956. if (dev->dev->of_node) {
  957. rc = at91_twi_configure_dma(dev, phy_addr);
  958. if (rc == -EPROBE_DEFER) {
  959. clk_disable_unprepare(dev->clk);
  960. return rc;
  961. }
  962. }
  963. if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
  964. &dev->fifo_size)) {
  965. dev_info(dev->dev, "Using FIFO (%u data)\n", dev->fifo_size);
  966. }
  967. rc = of_property_read_u32(dev->dev->of_node, "clock-frequency",
  968. &bus_clk_rate);
  969. if (rc)
  970. bus_clk_rate = DEFAULT_TWI_CLK_HZ;
  971. at91_calc_twi_clock(dev, bus_clk_rate);
  972. at91_init_twi_bus(dev);
  973. snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91");
  974. i2c_set_adapdata(&dev->adapter, dev);
  975. dev->adapter.owner = THIS_MODULE;
  976. dev->adapter.class = I2C_CLASS_DEPRECATED;
  977. dev->adapter.algo = &at91_twi_algorithm;
  978. dev->adapter.quirks = &at91_twi_quirks;
  979. dev->adapter.dev.parent = dev->dev;
  980. dev->adapter.nr = pdev->id;
  981. dev->adapter.timeout = AT91_I2C_TIMEOUT;
  982. dev->adapter.dev.of_node = pdev->dev.of_node;
  983. pm_runtime_set_autosuspend_delay(dev->dev, AUTOSUSPEND_TIMEOUT);
  984. pm_runtime_use_autosuspend(dev->dev);
  985. pm_runtime_set_active(dev->dev);
  986. pm_runtime_enable(dev->dev);
  987. rc = i2c_add_numbered_adapter(&dev->adapter);
  988. if (rc) {
  989. clk_disable_unprepare(dev->clk);
  990. pm_runtime_disable(dev->dev);
  991. pm_runtime_set_suspended(dev->dev);
  992. return rc;
  993. }
  994. dev_info(dev->dev, "AT91 i2c bus driver (hw version: %#x).\n",
  995. at91_twi_read(dev, AT91_TWI_VER));
  996. return 0;
  997. }
  998. static int at91_twi_remove(struct platform_device *pdev)
  999. {
  1000. struct at91_twi_dev *dev = platform_get_drvdata(pdev);
  1001. i2c_del_adapter(&dev->adapter);
  1002. clk_disable_unprepare(dev->clk);
  1003. pm_runtime_disable(dev->dev);
  1004. pm_runtime_set_suspended(dev->dev);
  1005. return 0;
  1006. }
  1007. #ifdef CONFIG_PM
  1008. static int at91_twi_runtime_suspend(struct device *dev)
  1009. {
  1010. struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
  1011. clk_disable_unprepare(twi_dev->clk);
  1012. pinctrl_pm_select_sleep_state(dev);
  1013. return 0;
  1014. }
  1015. static int at91_twi_runtime_resume(struct device *dev)
  1016. {
  1017. struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
  1018. pinctrl_pm_select_default_state(dev);
  1019. return clk_prepare_enable(twi_dev->clk);
  1020. }
  1021. static int at91_twi_suspend_noirq(struct device *dev)
  1022. {
  1023. if (!pm_runtime_status_suspended(dev))
  1024. at91_twi_runtime_suspend(dev);
  1025. return 0;
  1026. }
  1027. static int at91_twi_resume_noirq(struct device *dev)
  1028. {
  1029. struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
  1030. int ret;
  1031. if (!pm_runtime_status_suspended(dev)) {
  1032. ret = at91_twi_runtime_resume(dev);
  1033. if (ret)
  1034. return ret;
  1035. }
  1036. pm_runtime_mark_last_busy(dev);
  1037. pm_request_autosuspend(dev);
  1038. at91_init_twi_bus(twi_dev);
  1039. return 0;
  1040. }
  1041. static const struct dev_pm_ops at91_twi_pm = {
  1042. .suspend_noirq = at91_twi_suspend_noirq,
  1043. .resume_noirq = at91_twi_resume_noirq,
  1044. .runtime_suspend = at91_twi_runtime_suspend,
  1045. .runtime_resume = at91_twi_runtime_resume,
  1046. };
  1047. #define at91_twi_pm_ops (&at91_twi_pm)
  1048. #else
  1049. #define at91_twi_pm_ops NULL
  1050. #endif
  1051. static struct platform_driver at91_twi_driver = {
  1052. .probe = at91_twi_probe,
  1053. .remove = at91_twi_remove,
  1054. .id_table = at91_twi_devtypes,
  1055. .driver = {
  1056. .name = "at91_i2c",
  1057. .of_match_table = of_match_ptr(atmel_twi_dt_ids),
  1058. .pm = at91_twi_pm_ops,
  1059. },
  1060. };
  1061. static int __init at91_twi_init(void)
  1062. {
  1063. return platform_driver_register(&at91_twi_driver);
  1064. }
  1065. static void __exit at91_twi_exit(void)
  1066. {
  1067. platform_driver_unregister(&at91_twi_driver);
  1068. }
  1069. subsys_initcall(at91_twi_init);
  1070. module_exit(at91_twi_exit);
  1071. MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>");
  1072. MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
  1073. MODULE_LICENSE("GPL");
  1074. MODULE_ALIAS("platform:at91_i2c");