i2c-at91.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. dev_dbg(dev->dev, "wrote 0x%x, to go %d\n", *dev->buf, dev->buf_len);
  240. ++dev->buf;
  241. }
  242. static void at91_twi_write_data_dma_callback(void *data)
  243. {
  244. struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
  245. dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
  246. dev->buf_len, DMA_TO_DEVICE);
  247. /*
  248. * When this callback is called, THR/TX FIFO is likely not to be empty
  249. * yet. So we have to wait for TXCOMP or NACK bits to be set into the
  250. * Status Register to be sure that the STOP bit has been sent and the
  251. * transfer is completed. The NACK interrupt has already been enabled,
  252. * we just have to enable TXCOMP one.
  253. */
  254. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
  255. if (!dev->use_alt_cmd)
  256. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
  257. }
  258. static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
  259. {
  260. dma_addr_t dma_addr;
  261. struct dma_async_tx_descriptor *txdesc;
  262. struct at91_twi_dma *dma = &dev->dma;
  263. struct dma_chan *chan_tx = dma->chan_tx;
  264. unsigned int sg_len = 1;
  265. if (!dev->buf_len)
  266. return;
  267. dma->direction = DMA_TO_DEVICE;
  268. at91_twi_irq_save(dev);
  269. dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len,
  270. DMA_TO_DEVICE);
  271. if (dma_mapping_error(dev->dev, dma_addr)) {
  272. dev_err(dev->dev, "dma map failed\n");
  273. return;
  274. }
  275. dma->buf_mapped = true;
  276. at91_twi_irq_restore(dev);
  277. if (dev->fifo_size) {
  278. size_t part1_len, part2_len;
  279. struct scatterlist *sg;
  280. unsigned fifo_mr;
  281. sg_len = 0;
  282. part1_len = dev->buf_len & ~0x3;
  283. if (part1_len) {
  284. sg = &dma->sg[sg_len++];
  285. sg_dma_len(sg) = part1_len;
  286. sg_dma_address(sg) = dma_addr;
  287. }
  288. part2_len = dev->buf_len & 0x3;
  289. if (part2_len) {
  290. sg = &dma->sg[sg_len++];
  291. sg_dma_len(sg) = part2_len;
  292. sg_dma_address(sg) = dma_addr + part1_len;
  293. }
  294. /*
  295. * DMA controller is triggered when at least 4 data can be
  296. * written into the TX FIFO
  297. */
  298. fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
  299. fifo_mr &= ~AT91_TWI_FMR_TXRDYM_MASK;
  300. fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_FOUR_DATA);
  301. at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
  302. } else {
  303. sg_dma_len(&dma->sg[0]) = dev->buf_len;
  304. sg_dma_address(&dma->sg[0]) = dma_addr;
  305. }
  306. txdesc = dmaengine_prep_slave_sg(chan_tx, dma->sg, sg_len,
  307. DMA_MEM_TO_DEV,
  308. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  309. if (!txdesc) {
  310. dev_err(dev->dev, "dma prep slave sg failed\n");
  311. goto error;
  312. }
  313. txdesc->callback = at91_twi_write_data_dma_callback;
  314. txdesc->callback_param = dev;
  315. dma->xfer_in_progress = true;
  316. dmaengine_submit(txdesc);
  317. dma_async_issue_pending(chan_tx);
  318. return;
  319. error:
  320. at91_twi_dma_cleanup(dev);
  321. }
  322. static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
  323. {
  324. /*
  325. * If we are in this case, it means there is garbage data in RHR, so
  326. * delete them.
  327. */
  328. if (!dev->buf_len) {
  329. at91_twi_read(dev, AT91_TWI_RHR);
  330. return;
  331. }
  332. /* 8bit read works with and without FIFO */
  333. *dev->buf = readb_relaxed(dev->base + AT91_TWI_RHR);
  334. --dev->buf_len;
  335. /* return if aborting, we only needed to read RHR to clear RXRDY*/
  336. if (dev->recv_len_abort)
  337. return;
  338. /* handle I2C_SMBUS_BLOCK_DATA */
  339. if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
  340. /* ensure length byte is a valid value */
  341. if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) {
  342. dev->msg->flags &= ~I2C_M_RECV_LEN;
  343. dev->buf_len += *dev->buf;
  344. dev->msg->len = dev->buf_len + 1;
  345. dev_dbg(dev->dev, "received block length %d\n",
  346. dev->buf_len);
  347. } else {
  348. /* abort and send the stop by reading one more byte */
  349. dev->recv_len_abort = true;
  350. dev->buf_len = 1;
  351. }
  352. }
  353. /* send stop if second but last byte has been read */
  354. if (!dev->use_alt_cmd && dev->buf_len == 1)
  355. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
  356. dev_dbg(dev->dev, "read 0x%x, to go %d\n", *dev->buf, dev->buf_len);
  357. ++dev->buf;
  358. }
  359. static void at91_twi_read_data_dma_callback(void *data)
  360. {
  361. struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
  362. unsigned ier = AT91_TWI_TXCOMP;
  363. dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
  364. dev->buf_len, DMA_FROM_DEVICE);
  365. if (!dev->use_alt_cmd) {
  366. /* The last two bytes have to be read without using dma */
  367. dev->buf += dev->buf_len - 2;
  368. dev->buf_len = 2;
  369. ier |= AT91_TWI_RXRDY;
  370. }
  371. at91_twi_write(dev, AT91_TWI_IER, ier);
  372. }
  373. static void at91_twi_read_data_dma(struct at91_twi_dev *dev)
  374. {
  375. dma_addr_t dma_addr;
  376. struct dma_async_tx_descriptor *rxdesc;
  377. struct at91_twi_dma *dma = &dev->dma;
  378. struct dma_chan *chan_rx = dma->chan_rx;
  379. size_t buf_len;
  380. buf_len = (dev->use_alt_cmd) ? dev->buf_len : dev->buf_len - 2;
  381. dma->direction = DMA_FROM_DEVICE;
  382. /* Keep in mind that we won't use dma to read the last two bytes */
  383. at91_twi_irq_save(dev);
  384. dma_addr = dma_map_single(dev->dev, dev->buf, buf_len, DMA_FROM_DEVICE);
  385. if (dma_mapping_error(dev->dev, dma_addr)) {
  386. dev_err(dev->dev, "dma map failed\n");
  387. return;
  388. }
  389. dma->buf_mapped = true;
  390. at91_twi_irq_restore(dev);
  391. if (dev->fifo_size && IS_ALIGNED(buf_len, 4)) {
  392. unsigned fifo_mr;
  393. /*
  394. * DMA controller is triggered when at least 4 data can be
  395. * read from the RX FIFO
  396. */
  397. fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
  398. fifo_mr &= ~AT91_TWI_FMR_RXRDYM_MASK;
  399. fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_FOUR_DATA);
  400. at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
  401. }
  402. sg_dma_len(&dma->sg[0]) = buf_len;
  403. sg_dma_address(&dma->sg[0]) = dma_addr;
  404. rxdesc = dmaengine_prep_slave_sg(chan_rx, dma->sg, 1, DMA_DEV_TO_MEM,
  405. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  406. if (!rxdesc) {
  407. dev_err(dev->dev, "dma prep slave sg failed\n");
  408. goto error;
  409. }
  410. rxdesc->callback = at91_twi_read_data_dma_callback;
  411. rxdesc->callback_param = dev;
  412. dma->xfer_in_progress = true;
  413. dmaengine_submit(rxdesc);
  414. dma_async_issue_pending(dma->chan_rx);
  415. return;
  416. error:
  417. at91_twi_dma_cleanup(dev);
  418. }
  419. static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
  420. {
  421. struct at91_twi_dev *dev = dev_id;
  422. const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
  423. const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
  424. if (!irqstatus)
  425. return IRQ_NONE;
  426. /*
  427. * In reception, the behavior of the twi device (before sama5d2) is
  428. * weird. There is some magic about RXRDY flag! When a data has been
  429. * almost received, the reception of a new one is anticipated if there
  430. * is no stop command to send. That is the reason why ask for sending
  431. * the stop command not on the last data but on the second last one.
  432. *
  433. * Unfortunately, we could still have the RXRDY flag set even if the
  434. * transfer is done and we have read the last data. It might happen
  435. * when the i2c slave device sends too quickly data after receiving the
  436. * ack from the master. The data has been almost received before having
  437. * the order to send stop. In this case, sending the stop command could
  438. * cause a RXRDY interrupt with a TXCOMP one. It is better to manage
  439. * the RXRDY interrupt first in order to not keep garbage data in the
  440. * Receive Holding Register for the next transfer.
  441. */
  442. if (irqstatus & AT91_TWI_RXRDY)
  443. at91_twi_read_next_byte(dev);
  444. /*
  445. * When a NACK condition is detected, the I2C controller sets the NACK,
  446. * TXCOMP and TXRDY bits all together in the Status Register (SR).
  447. *
  448. * 1 - Handling NACK errors with CPU write transfer.
  449. *
  450. * In such case, we should not write the next byte into the Transmit
  451. * Holding Register (THR) otherwise the I2C controller would start a new
  452. * transfer and the I2C slave is likely to reply by another NACK.
  453. *
  454. * 2 - Handling NACK errors with DMA write transfer.
  455. *
  456. * By setting the TXRDY bit in the SR, the I2C controller also triggers
  457. * the DMA controller to write the next data into the THR. Then the
  458. * result depends on the hardware version of the I2C controller.
  459. *
  460. * 2a - Without support of the Alternative Command mode.
  461. *
  462. * This is the worst case: the DMA controller is triggered to write the
  463. * next data into the THR, hence starting a new transfer: the I2C slave
  464. * is likely to reply by another NACK.
  465. * Concurrently, this interrupt handler is likely to be called to manage
  466. * the first NACK before the I2C controller detects the second NACK and
  467. * sets once again the NACK bit into the SR.
  468. * When handling the first NACK, this interrupt handler disables the I2C
  469. * controller interruptions, especially the NACK interrupt.
  470. * Hence, the NACK bit is pending into the SR. This is why we should
  471. * read the SR to clear all pending interrupts at the beginning of
  472. * at91_do_twi_transfer() before actually starting a new transfer.
  473. *
  474. * 2b - With support of the Alternative Command mode.
  475. *
  476. * When a NACK condition is detected, the I2C controller also locks the
  477. * THR (and sets the LOCK bit in the SR): even though the DMA controller
  478. * is triggered by the TXRDY bit to write the next data into the THR,
  479. * this data actually won't go on the I2C bus hence a second NACK is not
  480. * generated.
  481. */
  482. if (irqstatus & (AT91_TWI_TXCOMP | AT91_TWI_NACK)) {
  483. at91_disable_twi_interrupts(dev);
  484. complete(&dev->cmd_complete);
  485. } else if (irqstatus & AT91_TWI_TXRDY) {
  486. at91_twi_write_next_byte(dev);
  487. }
  488. /* catch error flags */
  489. dev->transfer_status |= status;
  490. return IRQ_HANDLED;
  491. }
  492. static int at91_do_twi_transfer(struct at91_twi_dev *dev)
  493. {
  494. int ret;
  495. unsigned long time_left;
  496. bool has_unre_flag = dev->pdata->has_unre_flag;
  497. bool has_alt_cmd = dev->pdata->has_alt_cmd;
  498. /*
  499. * WARNING: the TXCOMP bit in the Status Register is NOT a clear on
  500. * read flag but shows the state of the transmission at the time the
  501. * Status Register is read. According to the programmer datasheet,
  502. * TXCOMP is set when both holding register and internal shifter are
  503. * empty and STOP condition has been sent.
  504. * Consequently, we should enable NACK interrupt rather than TXCOMP to
  505. * detect transmission failure.
  506. * Indeed let's take the case of an i2c write command using DMA.
  507. * Whenever the slave doesn't acknowledge a byte, the LOCK, NACK and
  508. * TXCOMP bits are set together into the Status Register.
  509. * LOCK is a clear on write bit, which is set to prevent the DMA
  510. * controller from sending new data on the i2c bus after a NACK
  511. * condition has happened. Once locked, this i2c peripheral stops
  512. * triggering the DMA controller for new data but it is more than
  513. * likely that a new DMA transaction is already in progress, writing
  514. * into the Transmit Holding Register. Since the peripheral is locked,
  515. * these new data won't be sent to the i2c bus but they will remain
  516. * into the Transmit Holding Register, so TXCOMP bit is cleared.
  517. * Then when the interrupt handler is called, the Status Register is
  518. * read: the TXCOMP bit is clear but NACK bit is still set. The driver
  519. * manage the error properly, without waiting for timeout.
  520. * This case can be reproduced easyly when writing into an at24 eeprom.
  521. *
  522. * Besides, the TXCOMP bit is already set before the i2c transaction
  523. * has been started. For read transactions, this bit is cleared when
  524. * writing the START bit into the Control Register. So the
  525. * corresponding interrupt can safely be enabled just after.
  526. * However for write transactions managed by the CPU, we first write
  527. * into THR, so TXCOMP is cleared. Then we can safely enable TXCOMP
  528. * interrupt. If TXCOMP interrupt were enabled before writing into THR,
  529. * the interrupt handler would be called immediately and the i2c command
  530. * would be reported as completed.
  531. * Also when a write transaction is managed by the DMA controller,
  532. * enabling the TXCOMP interrupt in this function may lead to a race
  533. * condition since we don't know whether the TXCOMP interrupt is enabled
  534. * before or after the DMA has started to write into THR. So the TXCOMP
  535. * interrupt is enabled later by at91_twi_write_data_dma_callback().
  536. * Immediately after in that DMA callback, if the alternative command
  537. * mode is not used, we still need to send the STOP condition manually
  538. * writing the corresponding bit into the Control Register.
  539. */
  540. dev_dbg(dev->dev, "transfer: %s %d bytes.\n",
  541. (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
  542. reinit_completion(&dev->cmd_complete);
  543. dev->transfer_status = 0;
  544. /* Clear pending interrupts, such as NACK. */
  545. at91_twi_read(dev, AT91_TWI_SR);
  546. if (dev->fifo_size) {
  547. unsigned fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
  548. /* Reset FIFO mode register */
  549. fifo_mr &= ~(AT91_TWI_FMR_TXRDYM_MASK |
  550. AT91_TWI_FMR_RXRDYM_MASK);
  551. fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_ONE_DATA);
  552. fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_ONE_DATA);
  553. at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
  554. /* Flush FIFOs */
  555. at91_twi_write(dev, AT91_TWI_CR,
  556. AT91_TWI_THRCLR | AT91_TWI_RHRCLR);
  557. }
  558. if (!dev->buf_len) {
  559. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK);
  560. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
  561. } else if (dev->msg->flags & I2C_M_RD) {
  562. unsigned start_flags = AT91_TWI_START;
  563. /* if only one byte is to be read, immediately stop transfer */
  564. if (!dev->use_alt_cmd && dev->buf_len <= 1 &&
  565. !(dev->msg->flags & I2C_M_RECV_LEN))
  566. start_flags |= AT91_TWI_STOP;
  567. at91_twi_write(dev, AT91_TWI_CR, start_flags);
  568. /*
  569. * When using dma without alternative command mode, the last
  570. * byte has to be read manually in order to not send the stop
  571. * command too late and then to receive extra data.
  572. * In practice, there are some issues if you use the dma to
  573. * read n-1 bytes because of latency.
  574. * Reading n-2 bytes with dma and the two last ones manually
  575. * seems to be the best solution.
  576. */
  577. if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
  578. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
  579. at91_twi_read_data_dma(dev);
  580. } else {
  581. at91_twi_write(dev, AT91_TWI_IER,
  582. AT91_TWI_TXCOMP |
  583. AT91_TWI_NACK |
  584. AT91_TWI_RXRDY);
  585. }
  586. } else {
  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_write_data_dma(dev);
  590. } else {
  591. at91_twi_write_next_byte(dev);
  592. at91_twi_write(dev, AT91_TWI_IER,
  593. AT91_TWI_TXCOMP |
  594. AT91_TWI_NACK |
  595. AT91_TWI_TXRDY);
  596. }
  597. }
  598. time_left = wait_for_completion_timeout(&dev->cmd_complete,
  599. dev->adapter.timeout);
  600. if (time_left == 0) {
  601. dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
  602. dev_err(dev->dev, "controller timed out\n");
  603. at91_init_twi_bus(dev);
  604. ret = -ETIMEDOUT;
  605. goto error;
  606. }
  607. if (dev->transfer_status & AT91_TWI_NACK) {
  608. dev_dbg(dev->dev, "received nack\n");
  609. ret = -EREMOTEIO;
  610. goto error;
  611. }
  612. if (dev->transfer_status & AT91_TWI_OVRE) {
  613. dev_err(dev->dev, "overrun while reading\n");
  614. ret = -EIO;
  615. goto error;
  616. }
  617. if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
  618. dev_err(dev->dev, "underrun while writing\n");
  619. ret = -EIO;
  620. goto error;
  621. }
  622. if ((has_alt_cmd || dev->fifo_size) &&
  623. (dev->transfer_status & AT91_TWI_LOCK)) {
  624. dev_err(dev->dev, "tx locked\n");
  625. ret = -EIO;
  626. goto error;
  627. }
  628. if (dev->recv_len_abort) {
  629. dev_err(dev->dev, "invalid smbus block length recvd\n");
  630. ret = -EPROTO;
  631. goto error;
  632. }
  633. dev_dbg(dev->dev, "transfer complete\n");
  634. return 0;
  635. error:
  636. /* first stop DMA transfer if still in progress */
  637. at91_twi_dma_cleanup(dev);
  638. /* then flush THR/FIFO and unlock TX if locked */
  639. if ((has_alt_cmd || dev->fifo_size) &&
  640. (dev->transfer_status & AT91_TWI_LOCK)) {
  641. dev_dbg(dev->dev, "unlock tx\n");
  642. at91_twi_write(dev, AT91_TWI_CR,
  643. AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
  644. }
  645. return ret;
  646. }
  647. static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
  648. {
  649. struct at91_twi_dev *dev = i2c_get_adapdata(adap);
  650. int ret;
  651. unsigned int_addr_flag = 0;
  652. struct i2c_msg *m_start = msg;
  653. bool is_read;
  654. dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
  655. ret = pm_runtime_get_sync(dev->dev);
  656. if (ret < 0)
  657. goto out;
  658. if (num == 2) {
  659. int internal_address = 0;
  660. int i;
  661. /* 1st msg is put into the internal address, start with 2nd */
  662. m_start = &msg[1];
  663. for (i = 0; i < msg->len; ++i) {
  664. const unsigned addr = msg->buf[msg->len - 1 - i];
  665. internal_address |= addr << (8 * i);
  666. int_addr_flag += AT91_TWI_IADRSZ_1;
  667. }
  668. at91_twi_write(dev, AT91_TWI_IADR, internal_address);
  669. }
  670. dev->use_alt_cmd = false;
  671. is_read = (m_start->flags & I2C_M_RD);
  672. if (dev->pdata->has_alt_cmd) {
  673. if (m_start->len > 0 &&
  674. m_start->len < AT91_I2C_MAX_ALT_CMD_DATA_SIZE) {
  675. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMEN);
  676. at91_twi_write(dev, AT91_TWI_ACR,
  677. AT91_TWI_ACR_DATAL(m_start->len) |
  678. ((is_read) ? AT91_TWI_ACR_DIR : 0));
  679. dev->use_alt_cmd = true;
  680. } else {
  681. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMDIS);
  682. }
  683. }
  684. at91_twi_write(dev, AT91_TWI_MMR,
  685. (m_start->addr << 16) |
  686. int_addr_flag |
  687. ((!dev->use_alt_cmd && is_read) ? AT91_TWI_MREAD : 0));
  688. dev->buf_len = m_start->len;
  689. dev->buf = m_start->buf;
  690. dev->msg = m_start;
  691. dev->recv_len_abort = false;
  692. ret = at91_do_twi_transfer(dev);
  693. ret = (ret < 0) ? ret : num;
  694. out:
  695. pm_runtime_mark_last_busy(dev->dev);
  696. pm_runtime_put_autosuspend(dev->dev);
  697. return ret;
  698. }
  699. /*
  700. * The hardware can handle at most two messages concatenated by a
  701. * repeated start via it's internal address feature.
  702. */
  703. static struct i2c_adapter_quirks at91_twi_quirks = {
  704. .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
  705. .max_comb_1st_msg_len = 3,
  706. };
  707. static u32 at91_twi_func(struct i2c_adapter *adapter)
  708. {
  709. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  710. | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  711. }
  712. static struct i2c_algorithm at91_twi_algorithm = {
  713. .master_xfer = at91_twi_xfer,
  714. .functionality = at91_twi_func,
  715. };
  716. static struct at91_twi_pdata at91rm9200_config = {
  717. .clk_max_div = 5,
  718. .clk_offset = 3,
  719. .has_unre_flag = true,
  720. .has_alt_cmd = false,
  721. .has_hold_field = false,
  722. };
  723. static struct at91_twi_pdata at91sam9261_config = {
  724. .clk_max_div = 5,
  725. .clk_offset = 4,
  726. .has_unre_flag = false,
  727. .has_alt_cmd = false,
  728. .has_hold_field = false,
  729. };
  730. static struct at91_twi_pdata at91sam9260_config = {
  731. .clk_max_div = 7,
  732. .clk_offset = 4,
  733. .has_unre_flag = false,
  734. .has_alt_cmd = false,
  735. .has_hold_field = false,
  736. };
  737. static struct at91_twi_pdata at91sam9g20_config = {
  738. .clk_max_div = 7,
  739. .clk_offset = 4,
  740. .has_unre_flag = false,
  741. .has_alt_cmd = false,
  742. .has_hold_field = false,
  743. };
  744. static struct at91_twi_pdata at91sam9g10_config = {
  745. .clk_max_div = 7,
  746. .clk_offset = 4,
  747. .has_unre_flag = false,
  748. .has_alt_cmd = false,
  749. .has_hold_field = false,
  750. };
  751. static const struct platform_device_id at91_twi_devtypes[] = {
  752. {
  753. .name = "i2c-at91rm9200",
  754. .driver_data = (unsigned long) &at91rm9200_config,
  755. }, {
  756. .name = "i2c-at91sam9261",
  757. .driver_data = (unsigned long) &at91sam9261_config,
  758. }, {
  759. .name = "i2c-at91sam9260",
  760. .driver_data = (unsigned long) &at91sam9260_config,
  761. }, {
  762. .name = "i2c-at91sam9g20",
  763. .driver_data = (unsigned long) &at91sam9g20_config,
  764. }, {
  765. .name = "i2c-at91sam9g10",
  766. .driver_data = (unsigned long) &at91sam9g10_config,
  767. }, {
  768. /* sentinel */
  769. }
  770. };
  771. #if defined(CONFIG_OF)
  772. static struct at91_twi_pdata at91sam9x5_config = {
  773. .clk_max_div = 7,
  774. .clk_offset = 4,
  775. .has_unre_flag = false,
  776. .has_alt_cmd = false,
  777. .has_hold_field = false,
  778. };
  779. static struct at91_twi_pdata sama5d4_config = {
  780. .clk_max_div = 7,
  781. .clk_offset = 4,
  782. .has_unre_flag = false,
  783. .has_alt_cmd = false,
  784. .has_hold_field = true,
  785. };
  786. static struct at91_twi_pdata sama5d2_config = {
  787. .clk_max_div = 7,
  788. .clk_offset = 4,
  789. .has_unre_flag = true,
  790. .has_alt_cmd = true,
  791. .has_hold_field = true,
  792. };
  793. static const struct of_device_id atmel_twi_dt_ids[] = {
  794. {
  795. .compatible = "atmel,at91rm9200-i2c",
  796. .data = &at91rm9200_config,
  797. } , {
  798. .compatible = "atmel,at91sam9260-i2c",
  799. .data = &at91sam9260_config,
  800. } , {
  801. .compatible = "atmel,at91sam9261-i2c",
  802. .data = &at91sam9261_config,
  803. } , {
  804. .compatible = "atmel,at91sam9g20-i2c",
  805. .data = &at91sam9g20_config,
  806. } , {
  807. .compatible = "atmel,at91sam9g10-i2c",
  808. .data = &at91sam9g10_config,
  809. }, {
  810. .compatible = "atmel,at91sam9x5-i2c",
  811. .data = &at91sam9x5_config,
  812. }, {
  813. .compatible = "atmel,sama5d4-i2c",
  814. .data = &sama5d4_config,
  815. }, {
  816. .compatible = "atmel,sama5d2-i2c",
  817. .data = &sama5d2_config,
  818. }, {
  819. /* sentinel */
  820. }
  821. };
  822. MODULE_DEVICE_TABLE(of, atmel_twi_dt_ids);
  823. #endif
  824. static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
  825. {
  826. int ret = 0;
  827. struct dma_slave_config slave_config;
  828. struct at91_twi_dma *dma = &dev->dma;
  829. enum dma_slave_buswidth addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  830. /*
  831. * The actual width of the access will be chosen in
  832. * dmaengine_prep_slave_sg():
  833. * for each buffer in the scatter-gather list, if its size is aligned
  834. * to addr_width then addr_width accesses will be performed to transfer
  835. * the buffer. On the other hand, if the buffer size is not aligned to
  836. * addr_width then the buffer is transferred using single byte accesses.
  837. * Please refer to the Atmel eXtended DMA controller driver.
  838. * When FIFOs are used, the TXRDYM threshold can always be set to
  839. * trigger the XDMAC when at least 4 data can be written into the TX
  840. * FIFO, even if single byte accesses are performed.
  841. * However the RXRDYM threshold must be set to fit the access width,
  842. * deduced from buffer length, so the XDMAC is triggered properly to
  843. * read data from the RX FIFO.
  844. */
  845. if (dev->fifo_size)
  846. addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  847. memset(&slave_config, 0, sizeof(slave_config));
  848. slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
  849. slave_config.src_addr_width = addr_width;
  850. slave_config.src_maxburst = 1;
  851. slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
  852. slave_config.dst_addr_width = addr_width;
  853. slave_config.dst_maxburst = 1;
  854. slave_config.device_fc = false;
  855. dma->chan_tx = dma_request_slave_channel_reason(dev->dev, "tx");
  856. if (IS_ERR(dma->chan_tx)) {
  857. ret = PTR_ERR(dma->chan_tx);
  858. dma->chan_tx = NULL;
  859. goto error;
  860. }
  861. dma->chan_rx = dma_request_slave_channel_reason(dev->dev, "rx");
  862. if (IS_ERR(dma->chan_rx)) {
  863. ret = PTR_ERR(dma->chan_rx);
  864. dma->chan_rx = NULL;
  865. goto error;
  866. }
  867. slave_config.direction = DMA_MEM_TO_DEV;
  868. if (dmaengine_slave_config(dma->chan_tx, &slave_config)) {
  869. dev_err(dev->dev, "failed to configure tx channel\n");
  870. ret = -EINVAL;
  871. goto error;
  872. }
  873. slave_config.direction = DMA_DEV_TO_MEM;
  874. if (dmaengine_slave_config(dma->chan_rx, &slave_config)) {
  875. dev_err(dev->dev, "failed to configure rx channel\n");
  876. ret = -EINVAL;
  877. goto error;
  878. }
  879. sg_init_table(dma->sg, 2);
  880. dma->buf_mapped = false;
  881. dma->xfer_in_progress = false;
  882. dev->use_dma = true;
  883. dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
  884. dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
  885. return ret;
  886. error:
  887. if (ret != -EPROBE_DEFER)
  888. dev_info(dev->dev, "can't get DMA channel, continue without DMA support\n");
  889. if (dma->chan_rx)
  890. dma_release_channel(dma->chan_rx);
  891. if (dma->chan_tx)
  892. dma_release_channel(dma->chan_tx);
  893. return ret;
  894. }
  895. static struct at91_twi_pdata *at91_twi_get_driver_data(
  896. struct platform_device *pdev)
  897. {
  898. if (pdev->dev.of_node) {
  899. const struct of_device_id *match;
  900. match = of_match_node(atmel_twi_dt_ids, pdev->dev.of_node);
  901. if (!match)
  902. return NULL;
  903. return (struct at91_twi_pdata *)match->data;
  904. }
  905. return (struct at91_twi_pdata *) platform_get_device_id(pdev)->driver_data;
  906. }
  907. static int at91_twi_probe(struct platform_device *pdev)
  908. {
  909. struct at91_twi_dev *dev;
  910. struct resource *mem;
  911. int rc;
  912. u32 phy_addr;
  913. u32 bus_clk_rate;
  914. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  915. if (!dev)
  916. return -ENOMEM;
  917. init_completion(&dev->cmd_complete);
  918. dev->dev = &pdev->dev;
  919. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  920. if (!mem)
  921. return -ENODEV;
  922. phy_addr = mem->start;
  923. dev->pdata = at91_twi_get_driver_data(pdev);
  924. if (!dev->pdata)
  925. return -ENODEV;
  926. dev->base = devm_ioremap_resource(&pdev->dev, mem);
  927. if (IS_ERR(dev->base))
  928. return PTR_ERR(dev->base);
  929. dev->irq = platform_get_irq(pdev, 0);
  930. if (dev->irq < 0)
  931. return dev->irq;
  932. rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
  933. dev_name(dev->dev), dev);
  934. if (rc) {
  935. dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
  936. return rc;
  937. }
  938. platform_set_drvdata(pdev, dev);
  939. dev->clk = devm_clk_get(dev->dev, NULL);
  940. if (IS_ERR(dev->clk)) {
  941. dev_err(dev->dev, "no clock defined\n");
  942. return -ENODEV;
  943. }
  944. clk_prepare_enable(dev->clk);
  945. if (dev->dev->of_node) {
  946. rc = at91_twi_configure_dma(dev, phy_addr);
  947. if (rc == -EPROBE_DEFER)
  948. return rc;
  949. }
  950. if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
  951. &dev->fifo_size)) {
  952. dev_info(dev->dev, "Using FIFO (%u data)\n", dev->fifo_size);
  953. }
  954. rc = of_property_read_u32(dev->dev->of_node, "clock-frequency",
  955. &bus_clk_rate);
  956. if (rc)
  957. bus_clk_rate = DEFAULT_TWI_CLK_HZ;
  958. at91_calc_twi_clock(dev, bus_clk_rate);
  959. at91_init_twi_bus(dev);
  960. snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91");
  961. i2c_set_adapdata(&dev->adapter, dev);
  962. dev->adapter.owner = THIS_MODULE;
  963. dev->adapter.class = I2C_CLASS_DEPRECATED;
  964. dev->adapter.algo = &at91_twi_algorithm;
  965. dev->adapter.quirks = &at91_twi_quirks;
  966. dev->adapter.dev.parent = dev->dev;
  967. dev->adapter.nr = pdev->id;
  968. dev->adapter.timeout = AT91_I2C_TIMEOUT;
  969. dev->adapter.dev.of_node = pdev->dev.of_node;
  970. pm_runtime_set_autosuspend_delay(dev->dev, AUTOSUSPEND_TIMEOUT);
  971. pm_runtime_use_autosuspend(dev->dev);
  972. pm_runtime_set_active(dev->dev);
  973. pm_runtime_enable(dev->dev);
  974. rc = i2c_add_numbered_adapter(&dev->adapter);
  975. if (rc) {
  976. clk_disable_unprepare(dev->clk);
  977. pm_runtime_disable(dev->dev);
  978. pm_runtime_set_suspended(dev->dev);
  979. return rc;
  980. }
  981. dev_info(dev->dev, "AT91 i2c bus driver (hw version: %#x).\n",
  982. at91_twi_read(dev, AT91_TWI_VER));
  983. return 0;
  984. }
  985. static int at91_twi_remove(struct platform_device *pdev)
  986. {
  987. struct at91_twi_dev *dev = platform_get_drvdata(pdev);
  988. i2c_del_adapter(&dev->adapter);
  989. clk_disable_unprepare(dev->clk);
  990. pm_runtime_disable(dev->dev);
  991. pm_runtime_set_suspended(dev->dev);
  992. return 0;
  993. }
  994. #ifdef CONFIG_PM
  995. static int at91_twi_runtime_suspend(struct device *dev)
  996. {
  997. struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
  998. clk_disable_unprepare(twi_dev->clk);
  999. pinctrl_pm_select_sleep_state(dev);
  1000. return 0;
  1001. }
  1002. static int at91_twi_runtime_resume(struct device *dev)
  1003. {
  1004. struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
  1005. pinctrl_pm_select_default_state(dev);
  1006. return clk_prepare_enable(twi_dev->clk);
  1007. }
  1008. static int at91_twi_suspend_noirq(struct device *dev)
  1009. {
  1010. if (!pm_runtime_status_suspended(dev))
  1011. at91_twi_runtime_suspend(dev);
  1012. return 0;
  1013. }
  1014. static int at91_twi_resume_noirq(struct device *dev)
  1015. {
  1016. struct at91_twi_dev *twi_dev = dev_get_drvdata(dev);
  1017. int ret;
  1018. if (!pm_runtime_status_suspended(dev)) {
  1019. ret = at91_twi_runtime_resume(dev);
  1020. if (ret)
  1021. return ret;
  1022. }
  1023. pm_runtime_mark_last_busy(dev);
  1024. pm_request_autosuspend(dev);
  1025. at91_init_twi_bus(twi_dev);
  1026. return 0;
  1027. }
  1028. static const struct dev_pm_ops at91_twi_pm = {
  1029. .suspend_noirq = at91_twi_suspend_noirq,
  1030. .resume_noirq = at91_twi_resume_noirq,
  1031. .runtime_suspend = at91_twi_runtime_suspend,
  1032. .runtime_resume = at91_twi_runtime_resume,
  1033. };
  1034. #define at91_twi_pm_ops (&at91_twi_pm)
  1035. #else
  1036. #define at91_twi_pm_ops NULL
  1037. #endif
  1038. static struct platform_driver at91_twi_driver = {
  1039. .probe = at91_twi_probe,
  1040. .remove = at91_twi_remove,
  1041. .id_table = at91_twi_devtypes,
  1042. .driver = {
  1043. .name = "at91_i2c",
  1044. .of_match_table = of_match_ptr(atmel_twi_dt_ids),
  1045. .pm = at91_twi_pm_ops,
  1046. },
  1047. };
  1048. static int __init at91_twi_init(void)
  1049. {
  1050. return platform_driver_register(&at91_twi_driver);
  1051. }
  1052. static void __exit at91_twi_exit(void)
  1053. {
  1054. platform_driver_unregister(&at91_twi_driver);
  1055. }
  1056. subsys_initcall(at91_twi_init);
  1057. module_exit(at91_twi_exit);
  1058. MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>");
  1059. MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
  1060. MODULE_LICENSE("GPL");
  1061. MODULE_ALIAS("platform:at91_i2c");