i2c-rcar.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /*
  2. * Driver for the Renesas RCar I2C unit
  3. *
  4. * Copyright (C) 2014-15 Wolfram Sang <wsa@sang-engineering.com>
  5. * Copyright (C) 2011-2015 Renesas Electronics Corporation
  6. *
  7. * Copyright (C) 2012-14 Renesas Solutions Corp.
  8. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  9. *
  10. * This file is based on the drivers/i2c/busses/i2c-sh7760.c
  11. * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; version 2 of the License.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/delay.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/err.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/io.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/of_device.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/slab.h>
  36. /* register offsets */
  37. #define ICSCR 0x00 /* slave ctrl */
  38. #define ICMCR 0x04 /* master ctrl */
  39. #define ICSSR 0x08 /* slave status */
  40. #define ICMSR 0x0C /* master status */
  41. #define ICSIER 0x10 /* slave irq enable */
  42. #define ICMIER 0x14 /* master irq enable */
  43. #define ICCCR 0x18 /* clock dividers */
  44. #define ICSAR 0x1C /* slave address */
  45. #define ICMAR 0x20 /* master address */
  46. #define ICRXTX 0x24 /* data port */
  47. #define ICDMAER 0x3c /* DMA enable */
  48. #define ICFBSCR 0x38 /* first bit setup cycle */
  49. /* ICSCR */
  50. #define SDBS (1 << 3) /* slave data buffer select */
  51. #define SIE (1 << 2) /* slave interface enable */
  52. #define GCAE (1 << 1) /* general call address enable */
  53. #define FNA (1 << 0) /* forced non acknowledgment */
  54. /* ICMCR */
  55. #define MDBS (1 << 7) /* non-fifo mode switch */
  56. #define FSCL (1 << 6) /* override SCL pin */
  57. #define FSDA (1 << 5) /* override SDA pin */
  58. #define OBPC (1 << 4) /* override pins */
  59. #define MIE (1 << 3) /* master if enable */
  60. #define TSBE (1 << 2)
  61. #define FSB (1 << 1) /* force stop bit */
  62. #define ESG (1 << 0) /* en startbit gen */
  63. /* ICSSR (also for ICSIER) */
  64. #define GCAR (1 << 6) /* general call received */
  65. #define STM (1 << 5) /* slave transmit mode */
  66. #define SSR (1 << 4) /* stop received */
  67. #define SDE (1 << 3) /* slave data empty */
  68. #define SDT (1 << 2) /* slave data transmitted */
  69. #define SDR (1 << 1) /* slave data received */
  70. #define SAR (1 << 0) /* slave addr received */
  71. /* ICMSR (also for ICMIE) */
  72. #define MNR (1 << 6) /* nack received */
  73. #define MAL (1 << 5) /* arbitration lost */
  74. #define MST (1 << 4) /* sent a stop */
  75. #define MDE (1 << 3)
  76. #define MDT (1 << 2)
  77. #define MDR (1 << 1)
  78. #define MAT (1 << 0) /* slave addr xfer done */
  79. /* ICDMAER */
  80. #define RSDMAE (1 << 3) /* DMA Slave Received Enable */
  81. #define TSDMAE (1 << 2) /* DMA Slave Transmitted Enable */
  82. #define RMDMAE (1 << 1) /* DMA Master Received Enable */
  83. #define TMDMAE (1 << 0) /* DMA Master Transmitted Enable */
  84. /* ICFBSCR */
  85. #define TCYC06 0x04 /* 6*Tcyc delay 1st bit between SDA and SCL */
  86. #define TCYC17 0x0f /* 17*Tcyc delay 1st bit between SDA and SCL */
  87. #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
  88. #define RCAR_BUS_PHASE_DATA (MDBS | MIE)
  89. #define RCAR_BUS_MASK_DATA (~(ESG | FSB) & 0xFF)
  90. #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
  91. #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
  92. #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
  93. #define RCAR_IRQ_STOP (MST)
  94. #define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF)
  95. #define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF)
  96. #define ID_LAST_MSG (1 << 0)
  97. #define ID_FIRST_MSG (1 << 1)
  98. #define ID_DONE (1 << 2)
  99. #define ID_ARBLOST (1 << 3)
  100. #define ID_NACK (1 << 4)
  101. /* persistent flags */
  102. #define ID_P_PM_BLOCKED (1 << 31)
  103. #define ID_P_MASK ID_P_PM_BLOCKED
  104. enum rcar_i2c_type {
  105. I2C_RCAR_GEN1,
  106. I2C_RCAR_GEN2,
  107. I2C_RCAR_GEN3,
  108. };
  109. struct rcar_i2c_priv {
  110. void __iomem *io;
  111. struct i2c_adapter adap;
  112. struct i2c_msg *msg;
  113. int msgs_left;
  114. struct clk *clk;
  115. wait_queue_head_t wait;
  116. int pos;
  117. u32 icccr;
  118. u32 flags;
  119. enum rcar_i2c_type devtype;
  120. struct i2c_client *slave;
  121. struct resource *res;
  122. struct dma_chan *dma_tx;
  123. struct dma_chan *dma_rx;
  124. struct scatterlist sg;
  125. enum dma_data_direction dma_direction;
  126. };
  127. #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
  128. #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
  129. #define LOOP_TIMEOUT 1024
  130. static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
  131. {
  132. writel(val, priv->io + reg);
  133. }
  134. static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
  135. {
  136. return readl(priv->io + reg);
  137. }
  138. static void rcar_i2c_init(struct rcar_i2c_priv *priv)
  139. {
  140. /* reset master mode */
  141. rcar_i2c_write(priv, ICMIER, 0);
  142. rcar_i2c_write(priv, ICMCR, MDBS);
  143. rcar_i2c_write(priv, ICMSR, 0);
  144. /* start clock */
  145. rcar_i2c_write(priv, ICCCR, priv->icccr);
  146. }
  147. static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
  148. {
  149. int i;
  150. for (i = 0; i < LOOP_TIMEOUT; i++) {
  151. /* make sure that bus is not busy */
  152. if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
  153. return 0;
  154. udelay(1);
  155. }
  156. return -EBUSY;
  157. }
  158. static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, struct i2c_timings *t)
  159. {
  160. u32 scgd, cdf, round, ick, sum, scl, cdf_width;
  161. unsigned long rate;
  162. struct device *dev = rcar_i2c_priv_to_dev(priv);
  163. /* Fall back to previously used values if not supplied */
  164. t->bus_freq_hz = t->bus_freq_hz ?: 100000;
  165. t->scl_fall_ns = t->scl_fall_ns ?: 35;
  166. t->scl_rise_ns = t->scl_rise_ns ?: 200;
  167. t->scl_int_delay_ns = t->scl_int_delay_ns ?: 50;
  168. switch (priv->devtype) {
  169. case I2C_RCAR_GEN1:
  170. cdf_width = 2;
  171. break;
  172. case I2C_RCAR_GEN2:
  173. case I2C_RCAR_GEN3:
  174. cdf_width = 3;
  175. break;
  176. default:
  177. dev_err(dev, "device type error\n");
  178. return -EIO;
  179. }
  180. /*
  181. * calculate SCL clock
  182. * see
  183. * ICCCR
  184. *
  185. * ick = clkp / (1 + CDF)
  186. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  187. *
  188. * ick : I2C internal clock < 20 MHz
  189. * ticf : I2C SCL falling time
  190. * tr : I2C SCL rising time
  191. * intd : LSI internal delay
  192. * clkp : peripheral_clk
  193. * F[] : integer up-valuation
  194. */
  195. rate = clk_get_rate(priv->clk);
  196. cdf = rate / 20000000;
  197. if (cdf >= 1U << cdf_width) {
  198. dev_err(dev, "Input clock %lu too high\n", rate);
  199. return -EIO;
  200. }
  201. ick = rate / (cdf + 1);
  202. /*
  203. * it is impossible to calculate large scale
  204. * number on u32. separate it
  205. *
  206. * F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd)
  207. * = F[sum * ick / 1000000000]
  208. * = F[(ick / 1000000) * sum / 1000]
  209. */
  210. sum = t->scl_fall_ns + t->scl_rise_ns + t->scl_int_delay_ns;
  211. round = (ick + 500000) / 1000000 * sum;
  212. round = (round + 500) / 1000;
  213. /*
  214. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  215. *
  216. * Calculation result (= SCL) should be less than
  217. * bus_speed for hardware safety
  218. *
  219. * We could use something along the lines of
  220. * div = ick / (bus_speed + 1) + 1;
  221. * scgd = (div - 20 - round + 7) / 8;
  222. * scl = ick / (20 + (scgd * 8) + round);
  223. * (not fully verified) but that would get pretty involved
  224. */
  225. for (scgd = 0; scgd < 0x40; scgd++) {
  226. scl = ick / (20 + (scgd * 8) + round);
  227. if (scl <= t->bus_freq_hz)
  228. goto scgd_find;
  229. }
  230. dev_err(dev, "it is impossible to calculate best SCL\n");
  231. return -EIO;
  232. scgd_find:
  233. dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
  234. scl, t->bus_freq_hz, clk_get_rate(priv->clk), round, cdf, scgd);
  235. /* keep icccr value */
  236. priv->icccr = scgd << cdf_width | cdf;
  237. return 0;
  238. }
  239. static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
  240. {
  241. int read = !!rcar_i2c_is_recv(priv);
  242. priv->pos = 0;
  243. if (priv->msgs_left == 1)
  244. priv->flags |= ID_LAST_MSG;
  245. rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
  246. /*
  247. * We don't have a testcase but the HW engineers say that the write order
  248. * of ICMSR and ICMCR depends on whether we issue START or REP_START. Since
  249. * it didn't cause a drawback for me, let's rather be safe than sorry.
  250. */
  251. if (priv->flags & ID_FIRST_MSG) {
  252. rcar_i2c_write(priv, ICMSR, 0);
  253. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
  254. } else {
  255. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
  256. rcar_i2c_write(priv, ICMSR, 0);
  257. }
  258. rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
  259. }
  260. static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
  261. {
  262. priv->msg++;
  263. priv->msgs_left--;
  264. priv->flags &= ID_P_MASK;
  265. rcar_i2c_prepare_msg(priv);
  266. }
  267. /*
  268. * interrupt functions
  269. */
  270. static void rcar_i2c_dma_unmap(struct rcar_i2c_priv *priv)
  271. {
  272. struct dma_chan *chan = priv->dma_direction == DMA_FROM_DEVICE
  273. ? priv->dma_rx : priv->dma_tx;
  274. /* Disable DMA Master Received/Transmitted */
  275. rcar_i2c_write(priv, ICDMAER, 0);
  276. /* Reset default delay */
  277. rcar_i2c_write(priv, ICFBSCR, TCYC06);
  278. dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg),
  279. priv->msg->len, priv->dma_direction);
  280. priv->dma_direction = DMA_NONE;
  281. }
  282. static void rcar_i2c_cleanup_dma(struct rcar_i2c_priv *priv)
  283. {
  284. if (priv->dma_direction == DMA_NONE)
  285. return;
  286. else if (priv->dma_direction == DMA_FROM_DEVICE)
  287. dmaengine_terminate_all(priv->dma_rx);
  288. else if (priv->dma_direction == DMA_TO_DEVICE)
  289. dmaengine_terminate_all(priv->dma_tx);
  290. rcar_i2c_dma_unmap(priv);
  291. }
  292. static void rcar_i2c_dma_callback(void *data)
  293. {
  294. struct rcar_i2c_priv *priv = data;
  295. priv->pos += sg_dma_len(&priv->sg);
  296. rcar_i2c_dma_unmap(priv);
  297. }
  298. static void rcar_i2c_dma(struct rcar_i2c_priv *priv)
  299. {
  300. struct device *dev = rcar_i2c_priv_to_dev(priv);
  301. struct i2c_msg *msg = priv->msg;
  302. bool read = msg->flags & I2C_M_RD;
  303. enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  304. struct dma_chan *chan = read ? priv->dma_rx : priv->dma_tx;
  305. struct dma_async_tx_descriptor *txdesc;
  306. dma_addr_t dma_addr;
  307. dma_cookie_t cookie;
  308. unsigned char *buf;
  309. int len;
  310. /* Do not use DMA if it's not available or for messages < 8 bytes */
  311. if (IS_ERR(chan) || msg->len < 8)
  312. return;
  313. if (read) {
  314. /*
  315. * The last two bytes needs to be fetched using PIO in
  316. * order for the STOP phase to work.
  317. */
  318. buf = priv->msg->buf;
  319. len = priv->msg->len - 2;
  320. } else {
  321. /*
  322. * First byte in message was sent using PIO.
  323. */
  324. buf = priv->msg->buf + 1;
  325. len = priv->msg->len - 1;
  326. }
  327. dma_addr = dma_map_single(chan->device->dev, buf, len, dir);
  328. if (dma_mapping_error(chan->device->dev, dma_addr)) {
  329. dev_dbg(dev, "dma map failed, using PIO\n");
  330. return;
  331. }
  332. sg_dma_len(&priv->sg) = len;
  333. sg_dma_address(&priv->sg) = dma_addr;
  334. priv->dma_direction = dir;
  335. txdesc = dmaengine_prep_slave_sg(chan, &priv->sg, 1,
  336. read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
  337. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  338. if (!txdesc) {
  339. dev_dbg(dev, "dma prep slave sg failed, using PIO\n");
  340. rcar_i2c_cleanup_dma(priv);
  341. return;
  342. }
  343. txdesc->callback = rcar_i2c_dma_callback;
  344. txdesc->callback_param = priv;
  345. cookie = dmaengine_submit(txdesc);
  346. if (dma_submit_error(cookie)) {
  347. dev_dbg(dev, "submitting dma failed, using PIO\n");
  348. rcar_i2c_cleanup_dma(priv);
  349. return;
  350. }
  351. /* Set delay for DMA operations */
  352. rcar_i2c_write(priv, ICFBSCR, TCYC17);
  353. /* Enable DMA Master Received/Transmitted */
  354. if (read)
  355. rcar_i2c_write(priv, ICDMAER, RMDMAE);
  356. else
  357. rcar_i2c_write(priv, ICDMAER, TMDMAE);
  358. dma_async_issue_pending(chan);
  359. }
  360. static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
  361. {
  362. struct i2c_msg *msg = priv->msg;
  363. /* FIXME: sometimes, unknown interrupt happened. Do nothing */
  364. if (!(msr & MDE))
  365. return;
  366. if (priv->pos < msg->len) {
  367. /*
  368. * Prepare next data to ICRXTX register.
  369. * This data will go to _SHIFT_ register.
  370. *
  371. * *
  372. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  373. */
  374. rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
  375. priv->pos++;
  376. /*
  377. * Try to use DMA to transmit the rest of the data if
  378. * address transfer pashe just finished.
  379. */
  380. if (msr & MAT)
  381. rcar_i2c_dma(priv);
  382. } else {
  383. /*
  384. * The last data was pushed to ICRXTX on _PREV_ empty irq.
  385. * It is on _SHIFT_ register, and will sent to I2C bus.
  386. *
  387. * *
  388. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  389. */
  390. if (priv->flags & ID_LAST_MSG) {
  391. /*
  392. * If current msg is the _LAST_ msg,
  393. * prepare stop condition here.
  394. * ID_DONE will be set on STOP irq.
  395. */
  396. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  397. } else {
  398. rcar_i2c_next_msg(priv);
  399. return;
  400. }
  401. }
  402. rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
  403. }
  404. static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
  405. {
  406. struct i2c_msg *msg = priv->msg;
  407. /* FIXME: sometimes, unknown interrupt happened. Do nothing */
  408. if (!(msr & MDR))
  409. return;
  410. if (msr & MAT) {
  411. /*
  412. * Address transfer phase finished, but no data at this point.
  413. * Try to use DMA to receive data.
  414. */
  415. rcar_i2c_dma(priv);
  416. } else if (priv->pos < msg->len) {
  417. /* get received data */
  418. msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
  419. priv->pos++;
  420. }
  421. /*
  422. * If next received data is the _LAST_, go to STOP phase. Might be
  423. * overwritten by REP START when setting up a new msg. Not elegant
  424. * but the only stable sequence for REP START I have found so far.
  425. */
  426. if (priv->pos + 1 >= msg->len)
  427. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  428. if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
  429. rcar_i2c_next_msg(priv);
  430. else
  431. rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
  432. }
  433. static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
  434. {
  435. u32 ssr_raw, ssr_filtered;
  436. u8 value;
  437. ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
  438. ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);
  439. if (!ssr_filtered)
  440. return false;
  441. /* address detected */
  442. if (ssr_filtered & SAR) {
  443. /* read or write request */
  444. if (ssr_raw & STM) {
  445. i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
  446. rcar_i2c_write(priv, ICRXTX, value);
  447. rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
  448. } else {
  449. i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
  450. rcar_i2c_read(priv, ICRXTX); /* dummy read */
  451. rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
  452. }
  453. rcar_i2c_write(priv, ICSSR, ~SAR & 0xff);
  454. }
  455. /* master sent stop */
  456. if (ssr_filtered & SSR) {
  457. i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
  458. rcar_i2c_write(priv, ICSIER, SAR | SSR);
  459. rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
  460. }
  461. /* master wants to write to us */
  462. if (ssr_filtered & SDR) {
  463. int ret;
  464. value = rcar_i2c_read(priv, ICRXTX);
  465. ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
  466. /* Send NACK in case of error */
  467. rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
  468. rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
  469. }
  470. /* master wants to read from us */
  471. if (ssr_filtered & SDE) {
  472. i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
  473. rcar_i2c_write(priv, ICRXTX, value);
  474. rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
  475. }
  476. return true;
  477. }
  478. static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
  479. {
  480. struct rcar_i2c_priv *priv = ptr;
  481. u32 msr, val;
  482. /* Clear START or STOP as soon as we can */
  483. val = rcar_i2c_read(priv, ICMCR);
  484. rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA);
  485. msr = rcar_i2c_read(priv, ICMSR);
  486. /* Only handle interrupts that are currently enabled */
  487. msr &= rcar_i2c_read(priv, ICMIER);
  488. if (!msr) {
  489. if (rcar_i2c_slave_irq(priv))
  490. return IRQ_HANDLED;
  491. return IRQ_NONE;
  492. }
  493. /* Arbitration lost */
  494. if (msr & MAL) {
  495. priv->flags |= ID_DONE | ID_ARBLOST;
  496. goto out;
  497. }
  498. /* Nack */
  499. if (msr & MNR) {
  500. /* HW automatically sends STOP after received NACK */
  501. rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
  502. priv->flags |= ID_NACK;
  503. goto out;
  504. }
  505. /* Stop */
  506. if (msr & MST) {
  507. priv->msgs_left--; /* The last message also made it */
  508. priv->flags |= ID_DONE;
  509. goto out;
  510. }
  511. if (rcar_i2c_is_recv(priv))
  512. rcar_i2c_irq_recv(priv, msr);
  513. else
  514. rcar_i2c_irq_send(priv, msr);
  515. out:
  516. if (priv->flags & ID_DONE) {
  517. rcar_i2c_write(priv, ICMIER, 0);
  518. rcar_i2c_write(priv, ICMSR, 0);
  519. wake_up(&priv->wait);
  520. }
  521. return IRQ_HANDLED;
  522. }
  523. static struct dma_chan *rcar_i2c_request_dma_chan(struct device *dev,
  524. enum dma_transfer_direction dir,
  525. dma_addr_t port_addr)
  526. {
  527. struct dma_chan *chan;
  528. struct dma_slave_config cfg;
  529. char *chan_name = dir == DMA_MEM_TO_DEV ? "tx" : "rx";
  530. int ret;
  531. chan = dma_request_chan(dev, chan_name);
  532. if (IS_ERR(chan)) {
  533. ret = PTR_ERR(chan);
  534. dev_dbg(dev, "request_channel failed for %s (%d)\n",
  535. chan_name, ret);
  536. return chan;
  537. }
  538. memset(&cfg, 0, sizeof(cfg));
  539. cfg.direction = dir;
  540. if (dir == DMA_MEM_TO_DEV) {
  541. cfg.dst_addr = port_addr;
  542. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  543. } else {
  544. cfg.src_addr = port_addr;
  545. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  546. }
  547. ret = dmaengine_slave_config(chan, &cfg);
  548. if (ret) {
  549. dev_dbg(dev, "slave_config failed for %s (%d)\n",
  550. chan_name, ret);
  551. dma_release_channel(chan);
  552. return ERR_PTR(ret);
  553. }
  554. dev_dbg(dev, "got DMA channel for %s\n", chan_name);
  555. return chan;
  556. }
  557. static void rcar_i2c_request_dma(struct rcar_i2c_priv *priv,
  558. struct i2c_msg *msg)
  559. {
  560. struct device *dev = rcar_i2c_priv_to_dev(priv);
  561. bool read;
  562. struct dma_chan *chan;
  563. enum dma_transfer_direction dir;
  564. read = msg->flags & I2C_M_RD;
  565. chan = read ? priv->dma_rx : priv->dma_tx;
  566. if (PTR_ERR(chan) != -EPROBE_DEFER)
  567. return;
  568. dir = read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  569. chan = rcar_i2c_request_dma_chan(dev, dir, priv->res->start + ICRXTX);
  570. if (read)
  571. priv->dma_rx = chan;
  572. else
  573. priv->dma_tx = chan;
  574. }
  575. static void rcar_i2c_release_dma(struct rcar_i2c_priv *priv)
  576. {
  577. if (!IS_ERR(priv->dma_tx)) {
  578. dma_release_channel(priv->dma_tx);
  579. priv->dma_tx = ERR_PTR(-EPROBE_DEFER);
  580. }
  581. if (!IS_ERR(priv->dma_rx)) {
  582. dma_release_channel(priv->dma_rx);
  583. priv->dma_rx = ERR_PTR(-EPROBE_DEFER);
  584. }
  585. }
  586. static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
  587. struct i2c_msg *msgs,
  588. int num)
  589. {
  590. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  591. struct device *dev = rcar_i2c_priv_to_dev(priv);
  592. int i, ret;
  593. long time_left;
  594. pm_runtime_get_sync(dev);
  595. ret = rcar_i2c_bus_barrier(priv);
  596. if (ret < 0)
  597. goto out;
  598. for (i = 0; i < num; i++) {
  599. /* This HW can't send STOP after address phase */
  600. if (msgs[i].len == 0) {
  601. ret = -EOPNOTSUPP;
  602. goto out;
  603. }
  604. rcar_i2c_request_dma(priv, msgs + i);
  605. }
  606. /* init first message */
  607. priv->msg = msgs;
  608. priv->msgs_left = num;
  609. priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG;
  610. rcar_i2c_prepare_msg(priv);
  611. time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE,
  612. num * adap->timeout);
  613. if (!time_left) {
  614. rcar_i2c_cleanup_dma(priv);
  615. rcar_i2c_init(priv);
  616. ret = -ETIMEDOUT;
  617. } else if (priv->flags & ID_NACK) {
  618. ret = -ENXIO;
  619. } else if (priv->flags & ID_ARBLOST) {
  620. ret = -EAGAIN;
  621. } else {
  622. ret = num - priv->msgs_left; /* The number of transfer */
  623. }
  624. out:
  625. pm_runtime_put(dev);
  626. if (ret < 0 && ret != -ENXIO)
  627. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  628. return ret;
  629. }
  630. static int rcar_reg_slave(struct i2c_client *slave)
  631. {
  632. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  633. if (priv->slave)
  634. return -EBUSY;
  635. if (slave->flags & I2C_CLIENT_TEN)
  636. return -EAFNOSUPPORT;
  637. pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv));
  638. priv->slave = slave;
  639. rcar_i2c_write(priv, ICSAR, slave->addr);
  640. rcar_i2c_write(priv, ICSSR, 0);
  641. rcar_i2c_write(priv, ICSIER, SAR | SSR);
  642. rcar_i2c_write(priv, ICSCR, SIE | SDBS);
  643. return 0;
  644. }
  645. static int rcar_unreg_slave(struct i2c_client *slave)
  646. {
  647. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  648. WARN_ON(!priv->slave);
  649. rcar_i2c_write(priv, ICSIER, 0);
  650. rcar_i2c_write(priv, ICSCR, 0);
  651. priv->slave = NULL;
  652. pm_runtime_put(rcar_i2c_priv_to_dev(priv));
  653. return 0;
  654. }
  655. static u32 rcar_i2c_func(struct i2c_adapter *adap)
  656. {
  657. /* This HW can't do SMBUS_QUICK and NOSTART */
  658. return I2C_FUNC_I2C | I2C_FUNC_SLAVE |
  659. (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  660. }
  661. static const struct i2c_algorithm rcar_i2c_algo = {
  662. .master_xfer = rcar_i2c_master_xfer,
  663. .functionality = rcar_i2c_func,
  664. .reg_slave = rcar_reg_slave,
  665. .unreg_slave = rcar_unreg_slave,
  666. };
  667. static const struct of_device_id rcar_i2c_dt_ids[] = {
  668. { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
  669. { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
  670. { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
  671. { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
  672. { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
  673. { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
  674. { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
  675. { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
  676. { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
  677. { .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 },
  678. {},
  679. };
  680. MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
  681. static int rcar_i2c_probe(struct platform_device *pdev)
  682. {
  683. struct rcar_i2c_priv *priv;
  684. struct i2c_adapter *adap;
  685. struct device *dev = &pdev->dev;
  686. struct i2c_timings i2c_t;
  687. int irq, ret;
  688. priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
  689. if (!priv)
  690. return -ENOMEM;
  691. priv->clk = devm_clk_get(dev, NULL);
  692. if (IS_ERR(priv->clk)) {
  693. dev_err(dev, "cannot get clock\n");
  694. return PTR_ERR(priv->clk);
  695. }
  696. priv->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  697. priv->io = devm_ioremap_resource(dev, priv->res);
  698. if (IS_ERR(priv->io))
  699. return PTR_ERR(priv->io);
  700. priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev);
  701. init_waitqueue_head(&priv->wait);
  702. adap = &priv->adap;
  703. adap->nr = pdev->id;
  704. adap->algo = &rcar_i2c_algo;
  705. adap->class = I2C_CLASS_DEPRECATED;
  706. adap->retries = 3;
  707. adap->dev.parent = dev;
  708. adap->dev.of_node = dev->of_node;
  709. i2c_set_adapdata(adap, priv);
  710. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  711. i2c_parse_fw_timings(dev, &i2c_t, false);
  712. /* Init DMA */
  713. sg_init_table(&priv->sg, 1);
  714. priv->dma_direction = DMA_NONE;
  715. priv->dma_rx = priv->dma_tx = ERR_PTR(-EPROBE_DEFER);
  716. pm_runtime_enable(dev);
  717. pm_runtime_get_sync(dev);
  718. ret = rcar_i2c_clock_calculate(priv, &i2c_t);
  719. if (ret < 0)
  720. goto out_pm_put;
  721. rcar_i2c_init(priv);
  722. /* Don't suspend when multi-master to keep arbitration working */
  723. if (of_property_read_bool(dev->of_node, "multi-master"))
  724. priv->flags |= ID_P_PM_BLOCKED;
  725. else
  726. pm_runtime_put(dev);
  727. irq = platform_get_irq(pdev, 0);
  728. ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0, dev_name(dev), priv);
  729. if (ret < 0) {
  730. dev_err(dev, "cannot get irq %d\n", irq);
  731. goto out_pm_disable;
  732. }
  733. platform_set_drvdata(pdev, priv);
  734. ret = i2c_add_numbered_adapter(adap);
  735. if (ret < 0)
  736. goto out_pm_disable;
  737. dev_info(dev, "probed\n");
  738. return 0;
  739. out_pm_put:
  740. pm_runtime_put(dev);
  741. out_pm_disable:
  742. pm_runtime_disable(dev);
  743. return ret;
  744. }
  745. static int rcar_i2c_remove(struct platform_device *pdev)
  746. {
  747. struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
  748. struct device *dev = &pdev->dev;
  749. i2c_del_adapter(&priv->adap);
  750. rcar_i2c_release_dma(priv);
  751. if (priv->flags & ID_P_PM_BLOCKED)
  752. pm_runtime_put(dev);
  753. pm_runtime_disable(dev);
  754. return 0;
  755. }
  756. static struct platform_driver rcar_i2c_driver = {
  757. .driver = {
  758. .name = "i2c-rcar",
  759. .of_match_table = rcar_i2c_dt_ids,
  760. },
  761. .probe = rcar_i2c_probe,
  762. .remove = rcar_i2c_remove,
  763. };
  764. module_platform_driver(rcar_i2c_driver);
  765. MODULE_LICENSE("GPL v2");
  766. MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
  767. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");