i2c-rcar.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Renesas R-Car I2C unit
  4. *
  5. * Copyright (C) 2014-19 Wolfram Sang <wsa@sang-engineering.com>
  6. * Copyright (C) 2011-2019 Renesas Electronics Corporation
  7. *
  8. * Copyright (C) 2012-14 Renesas Solutions Corp.
  9. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  10. *
  11. * This file is based on the drivers/i2c/busses/i2c-sh7760.c
  12. * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/dmaengine.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/err.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/iopoll.h>
  23. #include <linux/i2c.h>
  24. #include <linux/i2c-smbus.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/reset.h>
  31. #include <linux/slab.h>
  32. /* register offsets */
  33. #define ICSCR 0x00 /* slave ctrl */
  34. #define ICMCR 0x04 /* master ctrl */
  35. #define ICSSR 0x08 /* slave status */
  36. #define ICMSR 0x0C /* master status */
  37. #define ICSIER 0x10 /* slave irq enable */
  38. #define ICMIER 0x14 /* master irq enable */
  39. #define ICCCR 0x18 /* clock dividers */
  40. #define ICSAR 0x1C /* slave address */
  41. #define ICMAR 0x20 /* master address */
  42. #define ICRXTX 0x24 /* data port */
  43. #define ICCCR2 0x28 /* Clock control 2 */
  44. #define ICMPR 0x2C /* SCL mask control */
  45. #define ICHPR 0x30 /* SCL HIGH control */
  46. #define ICLPR 0x34 /* SCL LOW control */
  47. #define ICFBSCR 0x38 /* first bit setup cycle (Gen3) */
  48. #define ICDMAER 0x3c /* DMA enable (Gen3) */
  49. /* ICSCR */
  50. #define SDBS BIT(3) /* slave data buffer select */
  51. #define SIE BIT(2) /* slave interface enable */
  52. #define GCAE BIT(1) /* general call address enable */
  53. #define FNA BIT(0) /* forced non acknowledgment */
  54. /* ICMCR */
  55. #define MDBS BIT(7) /* non-fifo mode switch */
  56. #define FSCL BIT(6) /* override SCL pin */
  57. #define FSDA BIT(5) /* override SDA pin */
  58. #define OBPC BIT(4) /* override pins */
  59. #define MIE BIT(3) /* master if enable */
  60. #define TSBE BIT(2)
  61. #define FSB BIT(1) /* force stop bit */
  62. #define ESG BIT(0) /* enable start bit gen */
  63. /* ICSSR (also for ICSIER) */
  64. #define GCAR BIT(6) /* general call received */
  65. #define STM BIT(5) /* slave transmit mode */
  66. #define SSR BIT(4) /* stop received */
  67. #define SDE BIT(3) /* slave data empty */
  68. #define SDT BIT(2) /* slave data transmitted */
  69. #define SDR BIT(1) /* slave data received */
  70. #define SAR BIT(0) /* slave addr received */
  71. /* ICMSR (also for ICMIE) */
  72. #define MNR BIT(6) /* nack received */
  73. #define MAL BIT(5) /* arbitration lost */
  74. #define MST BIT(4) /* sent a stop */
  75. #define MDE BIT(3)
  76. #define MDT BIT(2)
  77. #define MDR BIT(1)
  78. #define MAT BIT(0) /* slave addr xfer done */
  79. /* ICDMAER */
  80. #define RSDMAE BIT(3) /* DMA Slave Received Enable */
  81. #define TSDMAE BIT(2) /* DMA Slave Transmitted Enable */
  82. #define RMDMAE BIT(1) /* DMA Master Received Enable */
  83. #define TMDMAE BIT(0) /* DMA Master Transmitted Enable */
  84. /* ICCCR2 */
  85. #define FMPE BIT(7) /* Fast Mode Plus Enable */
  86. #define CDFD BIT(2) /* CDF Disable */
  87. #define HLSE BIT(1) /* HIGH/LOW Separate Control Enable */
  88. #define SME BIT(0) /* SCL Mask Enable */
  89. /* ICFBSCR */
  90. #define TCYC17 0x0f /* 17*Tcyc delay 1st bit between SDA and SCL */
  91. #define RCAR_MIN_DMA_LEN 8
  92. /* SCL low/high ratio 5:4 to meet all I2C timing specs (incl safety margin) */
  93. #define RCAR_SCLD_RATIO 5
  94. #define RCAR_SCHD_RATIO 4
  95. /*
  96. * SMD should be smaller than SCLD/SCHD and is always around 20 in the docs.
  97. * Thus, we simply use 20 which works for low and high speeds.
  98. */
  99. #define RCAR_DEFAULT_SMD 20
  100. #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
  101. #define RCAR_BUS_PHASE_DATA (MDBS | MIE)
  102. #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
  103. #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
  104. #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
  105. #define RCAR_IRQ_STOP (MST)
  106. #define ID_LAST_MSG BIT(0)
  107. #define ID_REP_AFTER_RD BIT(1)
  108. #define ID_DONE BIT(2)
  109. #define ID_ARBLOST BIT(3)
  110. #define ID_NACK BIT(4)
  111. #define ID_EPROTO BIT(5)
  112. /* persistent flags */
  113. #define ID_P_FMPLUS BIT(27)
  114. #define ID_P_NOT_ATOMIC BIT(28)
  115. #define ID_P_HOST_NOTIFY BIT(29)
  116. #define ID_P_NO_RXDMA BIT(30) /* HW forbids RXDMA sometimes */
  117. #define ID_P_PM_BLOCKED BIT(31)
  118. #define ID_P_MASK GENMASK(31, 27)
  119. #define ID_SLAVE_NACK BIT(0)
  120. enum rcar_i2c_type {
  121. I2C_RCAR_GEN1,
  122. I2C_RCAR_GEN2,
  123. I2C_RCAR_GEN3,
  124. I2C_RCAR_GEN4,
  125. };
  126. struct rcar_i2c_priv {
  127. u32 flags;
  128. void __iomem *io;
  129. struct i2c_adapter adap;
  130. struct i2c_msg *msg;
  131. int msgs_left;
  132. struct clk *clk;
  133. wait_queue_head_t wait;
  134. int pos;
  135. u32 icccr;
  136. u16 schd;
  137. u16 scld;
  138. u8 smd;
  139. u8 recovery_icmcr; /* protected by adapter lock */
  140. enum rcar_i2c_type devtype;
  141. struct i2c_client *slave;
  142. struct resource *res;
  143. struct dma_chan *dma_tx;
  144. struct dma_chan *dma_rx;
  145. struct scatterlist sg;
  146. enum dma_data_direction dma_direction;
  147. struct reset_control *rstc;
  148. int irq;
  149. struct i2c_client *host_notify_client;
  150. u8 slave_flags;
  151. };
  152. #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
  153. #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
  154. static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
  155. {
  156. writel(val, priv->io + reg);
  157. }
  158. static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
  159. {
  160. return readl(priv->io + reg);
  161. }
  162. static void rcar_i2c_clear_irq(struct rcar_i2c_priv *priv, u32 val)
  163. {
  164. writel(~val & 0x7f, priv->io + ICMSR);
  165. }
  166. static int rcar_i2c_get_scl(struct i2c_adapter *adap)
  167. {
  168. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  169. return !!(rcar_i2c_read(priv, ICMCR) & FSCL);
  170. }
  171. static void rcar_i2c_set_scl(struct i2c_adapter *adap, int val)
  172. {
  173. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  174. if (val)
  175. priv->recovery_icmcr |= FSCL;
  176. else
  177. priv->recovery_icmcr &= ~FSCL;
  178. rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr);
  179. }
  180. static void rcar_i2c_set_sda(struct i2c_adapter *adap, int val)
  181. {
  182. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  183. if (val)
  184. priv->recovery_icmcr |= FSDA;
  185. else
  186. priv->recovery_icmcr &= ~FSDA;
  187. rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr);
  188. }
  189. static int rcar_i2c_get_bus_free(struct i2c_adapter *adap)
  190. {
  191. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  192. return !(rcar_i2c_read(priv, ICMCR) & FSDA);
  193. }
  194. static struct i2c_bus_recovery_info rcar_i2c_bri = {
  195. .get_scl = rcar_i2c_get_scl,
  196. .set_scl = rcar_i2c_set_scl,
  197. .set_sda = rcar_i2c_set_sda,
  198. .get_bus_free = rcar_i2c_get_bus_free,
  199. .recover_bus = i2c_generic_scl_recovery,
  200. };
  201. static void rcar_i2c_init(struct rcar_i2c_priv *priv)
  202. {
  203. /* reset master mode */
  204. rcar_i2c_write(priv, ICMIER, 0);
  205. rcar_i2c_write(priv, ICMCR, MDBS);
  206. rcar_i2c_write(priv, ICMSR, 0);
  207. /* start clock */
  208. if (priv->devtype < I2C_RCAR_GEN3) {
  209. rcar_i2c_write(priv, ICCCR, priv->icccr);
  210. } else {
  211. u32 icccr2 = CDFD | HLSE | SME;
  212. if (priv->flags & ID_P_FMPLUS)
  213. icccr2 |= FMPE;
  214. rcar_i2c_write(priv, ICCCR2, icccr2);
  215. rcar_i2c_write(priv, ICCCR, priv->icccr);
  216. rcar_i2c_write(priv, ICMPR, priv->smd);
  217. rcar_i2c_write(priv, ICHPR, priv->schd);
  218. rcar_i2c_write(priv, ICLPR, priv->scld);
  219. rcar_i2c_write(priv, ICFBSCR, TCYC17);
  220. }
  221. }
  222. static void rcar_i2c_reset_slave(struct rcar_i2c_priv *priv)
  223. {
  224. rcar_i2c_write(priv, ICSIER, 0);
  225. rcar_i2c_write(priv, ICSSR, 0);
  226. rcar_i2c_write(priv, ICSCR, SDBS);
  227. rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */
  228. }
  229. static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
  230. {
  231. int ret;
  232. u32 val;
  233. ret = readl_poll_timeout(priv->io + ICMCR, val, !(val & FSDA), 10,
  234. priv->adap.timeout);
  235. if (ret) {
  236. /* Waiting did not help, try to recover */
  237. priv->recovery_icmcr = MDBS | OBPC | FSDA | FSCL;
  238. ret = i2c_recover_bus(&priv->adap);
  239. }
  240. return ret;
  241. }
  242. static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv)
  243. {
  244. u32 cdf, round, ick, sum, scl, cdf_width;
  245. unsigned long rate;
  246. struct device *dev = rcar_i2c_priv_to_dev(priv);
  247. struct i2c_timings t = {
  248. .bus_freq_hz = I2C_MAX_STANDARD_MODE_FREQ,
  249. .scl_fall_ns = 35,
  250. .scl_rise_ns = 200,
  251. .scl_int_delay_ns = 50,
  252. };
  253. /* Fall back to previously used values if not supplied */
  254. i2c_parse_fw_timings(dev, &t, false);
  255. priv->smd = RCAR_DEFAULT_SMD;
  256. /*
  257. * calculate SCL clock
  258. * see
  259. * ICCCR (and ICCCR2 for Gen3+)
  260. *
  261. * ick = clkp / (1 + CDF)
  262. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  263. *
  264. * for Gen3+:
  265. * SCL = clkp / (8 + SMD * 2 + SCLD + SCHD +F[(ticf + tr + intd) * clkp])
  266. *
  267. * ick : I2C internal clock < 20 MHz
  268. * ticf : I2C SCL falling time
  269. * tr : I2C SCL rising time
  270. * intd : LSI internal delay
  271. * clkp : peripheral_clk
  272. * F[] : integer up-valuation
  273. */
  274. rate = clk_get_rate(priv->clk);
  275. cdf = rate / 20000000;
  276. cdf_width = (priv->devtype == I2C_RCAR_GEN1) ? 2 : 3;
  277. if (cdf >= 1U << cdf_width)
  278. goto err_no_val;
  279. if (t.bus_freq_hz > I2C_MAX_FAST_MODE_FREQ && priv->devtype >= I2C_RCAR_GEN4)
  280. priv->flags |= ID_P_FMPLUS;
  281. else
  282. priv->flags &= ~ID_P_FMPLUS;
  283. /* On Gen3+, we use cdf only for the filters, not as a SCL divider */
  284. ick = rate / (priv->devtype < I2C_RCAR_GEN3 ? (cdf + 1) : 1);
  285. /*
  286. * It is impossible to calculate a large scale number on u32. Separate it.
  287. *
  288. * F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd)
  289. * = F[sum * ick / 1000000000]
  290. * = F[(ick / 1000000) * sum / 1000]
  291. */
  292. sum = t.scl_fall_ns + t.scl_rise_ns + t.scl_int_delay_ns;
  293. round = DIV_ROUND_CLOSEST(ick, 1000000);
  294. round = DIV_ROUND_CLOSEST(round * sum, 1000);
  295. if (priv->devtype < I2C_RCAR_GEN3) {
  296. u32 scgd;
  297. /*
  298. * SCL = ick / (20 + 8 * SCGD + F[(ticf + tr + intd) * ick])
  299. * 20 + 8 * SCGD + F[...] = ick / SCL
  300. * SCGD = ((ick / SCL) - 20 - F[...]) / 8
  301. * Result (= SCL) should be less than bus_speed for hardware safety
  302. */
  303. scgd = DIV_ROUND_UP(ick, t.bus_freq_hz ?: 1);
  304. scgd = DIV_ROUND_UP(scgd - 20 - round, 8);
  305. scl = ick / (20 + 8 * scgd + round);
  306. if (scgd > 0x3f)
  307. goto err_no_val;
  308. dev_dbg(dev, "clk %u/%u(%lu), round %u, CDF: %u, SCGD: %u\n",
  309. scl, t.bus_freq_hz, rate, round, cdf, scgd);
  310. priv->icccr = scgd << cdf_width | cdf;
  311. } else {
  312. u32 x, sum_ratio = RCAR_SCHD_RATIO + RCAR_SCLD_RATIO;
  313. /*
  314. * SCLD/SCHD ratio and SMD default value are explained above
  315. * where they are defined. With these definitions, we can compute
  316. * x as a base value for the SCLD/SCHD ratio:
  317. *
  318. * SCL = clkp / (8 + 2 * SMD + SCLD + SCHD + F[(ticf + tr + intd) * clkp])
  319. * SCL = clkp / (8 + 2 * SMD + RCAR_SCLD_RATIO * x
  320. * + RCAR_SCHD_RATIO * x + F[...])
  321. *
  322. * with: sum_ratio = RCAR_SCLD_RATIO + RCAR_SCHD_RATIO
  323. *
  324. * SCL = clkp / (8 + 2 * smd + sum_ratio * x + F[...])
  325. * 8 + 2 * smd + sum_ratio * x + F[...] = clkp / SCL
  326. * x = ((clkp / SCL) - 8 - 2 * smd - F[...]) / sum_ratio
  327. */
  328. x = DIV_ROUND_UP(rate, t.bus_freq_hz ?: 1);
  329. x = DIV_ROUND_UP(x - 8 - 2 * priv->smd - round, sum_ratio);
  330. scl = rate / (8 + 2 * priv->smd + sum_ratio * x + round);
  331. if (x == 0 || x * RCAR_SCLD_RATIO > 0xffff)
  332. goto err_no_val;
  333. priv->icccr = cdf;
  334. priv->schd = RCAR_SCHD_RATIO * x;
  335. priv->scld = RCAR_SCLD_RATIO * x;
  336. if (priv->smd >= priv->schd)
  337. priv->smd = priv->schd - 1;
  338. dev_dbg(dev, "clk %u/%u(%lu), round %u, CDF: %u SCHD %u SCLD %u SMD %u\n",
  339. scl, t.bus_freq_hz, rate, round, cdf, priv->schd, priv->scld, priv->smd);
  340. }
  341. return 0;
  342. err_no_val:
  343. dev_err(dev, "it is impossible to calculate best SCL\n");
  344. return -EINVAL;
  345. }
  346. /*
  347. * We don't have a test case but the HW engineers say that the write order of
  348. * ICMSR and ICMCR depends on whether we issue START or REP_START. So, ICMSR
  349. * handling is outside of this function. First messages clear ICMSR before this
  350. * function, interrupt handlers clear the relevant bits after this function.
  351. */
  352. static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
  353. {
  354. int read = !!rcar_i2c_is_recv(priv);
  355. bool rep_start = !(priv->flags & ID_REP_AFTER_RD);
  356. priv->pos = 0;
  357. priv->flags &= ID_P_MASK;
  358. if (priv->msgs_left == 1)
  359. priv->flags |= ID_LAST_MSG;
  360. rcar_i2c_write(priv, ICMAR, i2c_8bit_addr_from_msg(priv->msg));
  361. if (priv->flags & ID_P_NOT_ATOMIC)
  362. rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
  363. if (rep_start)
  364. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
  365. }
  366. static void rcar_i2c_first_msg(struct rcar_i2c_priv *priv,
  367. struct i2c_msg *msgs, int num)
  368. {
  369. priv->msg = msgs;
  370. priv->msgs_left = num;
  371. rcar_i2c_write(priv, ICMSR, 0); /* must be before preparing msg */
  372. rcar_i2c_prepare_msg(priv);
  373. }
  374. static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
  375. {
  376. priv->msg++;
  377. priv->msgs_left--;
  378. rcar_i2c_prepare_msg(priv);
  379. /* ICMSR handling must come afterwards in the irq handler */
  380. }
  381. static void rcar_i2c_cleanup_dma(struct rcar_i2c_priv *priv, bool terminate)
  382. {
  383. struct dma_chan *chan = priv->dma_direction == DMA_FROM_DEVICE
  384. ? priv->dma_rx : priv->dma_tx;
  385. /* only allowed from thread context! */
  386. if (terminate)
  387. dmaengine_terminate_sync(chan);
  388. dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg),
  389. sg_dma_len(&priv->sg), priv->dma_direction);
  390. /* Gen3+ can only do one RXDMA per transfer and we just completed it */
  391. if (priv->devtype >= I2C_RCAR_GEN3 &&
  392. priv->dma_direction == DMA_FROM_DEVICE)
  393. priv->flags |= ID_P_NO_RXDMA;
  394. priv->dma_direction = DMA_NONE;
  395. /* Disable DMA Master Received/Transmitted, must be last! */
  396. rcar_i2c_write(priv, ICDMAER, 0);
  397. }
  398. static void rcar_i2c_dma_callback(void *data)
  399. {
  400. struct rcar_i2c_priv *priv = data;
  401. priv->pos += sg_dma_len(&priv->sg);
  402. rcar_i2c_cleanup_dma(priv, false);
  403. }
  404. static bool rcar_i2c_dma(struct rcar_i2c_priv *priv)
  405. {
  406. struct device *dev = rcar_i2c_priv_to_dev(priv);
  407. struct i2c_msg *msg = priv->msg;
  408. bool read = msg->flags & I2C_M_RD;
  409. enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  410. struct dma_chan *chan = read ? priv->dma_rx : priv->dma_tx;
  411. struct dma_async_tx_descriptor *txdesc;
  412. dma_addr_t dma_addr;
  413. dma_cookie_t cookie;
  414. unsigned char *buf;
  415. int len;
  416. /* Do various checks to see if DMA is feasible at all */
  417. if (!(priv->flags & ID_P_NOT_ATOMIC) || IS_ERR(chan) || msg->len < RCAR_MIN_DMA_LEN ||
  418. !(msg->flags & I2C_M_DMA_SAFE) || (read && priv->flags & ID_P_NO_RXDMA))
  419. return false;
  420. if (read) {
  421. /*
  422. * The last two bytes needs to be fetched using PIO in
  423. * order for the STOP phase to work.
  424. */
  425. buf = priv->msg->buf;
  426. len = priv->msg->len - 2;
  427. } else {
  428. /*
  429. * First byte in message was sent using PIO.
  430. */
  431. buf = priv->msg->buf + 1;
  432. len = priv->msg->len - 1;
  433. }
  434. dma_addr = dma_map_single(chan->device->dev, buf, len, dir);
  435. if (dma_mapping_error(chan->device->dev, dma_addr)) {
  436. dev_dbg(dev, "dma map failed, using PIO\n");
  437. return false;
  438. }
  439. sg_dma_len(&priv->sg) = len;
  440. sg_dma_address(&priv->sg) = dma_addr;
  441. priv->dma_direction = dir;
  442. txdesc = dmaengine_prep_slave_sg(chan, &priv->sg, 1,
  443. read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
  444. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  445. if (!txdesc) {
  446. dev_dbg(dev, "dma prep slave sg failed, using PIO\n");
  447. rcar_i2c_cleanup_dma(priv, false);
  448. return false;
  449. }
  450. txdesc->callback = rcar_i2c_dma_callback;
  451. txdesc->callback_param = priv;
  452. cookie = dmaengine_submit(txdesc);
  453. if (dma_submit_error(cookie)) {
  454. dev_dbg(dev, "submitting dma failed, using PIO\n");
  455. rcar_i2c_cleanup_dma(priv, false);
  456. return false;
  457. }
  458. /* Enable DMA Master Received/Transmitted */
  459. if (read)
  460. rcar_i2c_write(priv, ICDMAER, RMDMAE);
  461. else
  462. rcar_i2c_write(priv, ICDMAER, TMDMAE);
  463. dma_async_issue_pending(chan);
  464. return true;
  465. }
  466. static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
  467. {
  468. struct i2c_msg *msg = priv->msg;
  469. u32 irqs_to_clear = MDE;
  470. /* FIXME: sometimes, unknown interrupt happened. Do nothing */
  471. if (WARN(!(msr & MDE), "spurious irq"))
  472. return;
  473. if (msr & MAT)
  474. irqs_to_clear |= MAT;
  475. /* Check if DMA can be enabled and take over */
  476. if (priv->pos == 1 && rcar_i2c_dma(priv))
  477. return;
  478. if (priv->pos < msg->len) {
  479. /*
  480. * Prepare next data to ICRXTX register.
  481. * This data will go to _SHIFT_ register.
  482. *
  483. * *
  484. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  485. */
  486. rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
  487. priv->pos++;
  488. } else {
  489. /*
  490. * The last data was pushed to ICRXTX on _PREV_ empty irq.
  491. * It is on _SHIFT_ register, and will sent to I2C bus.
  492. *
  493. * *
  494. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  495. */
  496. if (priv->flags & ID_LAST_MSG)
  497. /*
  498. * If current msg is the _LAST_ msg,
  499. * prepare stop condition here.
  500. * ID_DONE will be set on STOP irq.
  501. */
  502. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  503. else
  504. rcar_i2c_next_msg(priv);
  505. }
  506. rcar_i2c_clear_irq(priv, irqs_to_clear);
  507. }
  508. static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
  509. {
  510. struct i2c_msg *msg = priv->msg;
  511. bool recv_len_init = priv->pos == 0 && msg->flags & I2C_M_RECV_LEN;
  512. u32 irqs_to_clear = MDR;
  513. /* FIXME: sometimes, unknown interrupt happened. Do nothing */
  514. if (!(msr & MDR))
  515. return;
  516. if (msr & MAT) {
  517. irqs_to_clear |= MAT;
  518. /*
  519. * Address transfer phase finished, but no data at this point.
  520. * Try to use DMA to receive data.
  521. */
  522. rcar_i2c_dma(priv);
  523. } else if (priv->pos < msg->len) {
  524. /* get received data */
  525. u8 data = rcar_i2c_read(priv, ICRXTX);
  526. msg->buf[priv->pos] = data;
  527. if (recv_len_init) {
  528. if (data == 0 || data > I2C_SMBUS_BLOCK_MAX) {
  529. priv->flags |= ID_DONE | ID_EPROTO;
  530. return;
  531. }
  532. msg->len += msg->buf[0];
  533. /* Enough data for DMA? */
  534. if (rcar_i2c_dma(priv))
  535. return;
  536. /* new length after RECV_LEN now properly initialized */
  537. recv_len_init = false;
  538. }
  539. priv->pos++;
  540. }
  541. /*
  542. * If next received data is the _LAST_ and we are not waiting for a new
  543. * length because of RECV_LEN, then go to a new phase.
  544. */
  545. if (priv->pos + 1 == msg->len && !recv_len_init) {
  546. if (priv->flags & ID_LAST_MSG) {
  547. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  548. } else {
  549. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
  550. priv->flags |= ID_REP_AFTER_RD;
  551. }
  552. }
  553. if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
  554. rcar_i2c_next_msg(priv);
  555. rcar_i2c_clear_irq(priv, irqs_to_clear);
  556. }
  557. static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
  558. {
  559. u32 ssr_raw, ssr_filtered;
  560. u8 value;
  561. int ret;
  562. ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
  563. ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);
  564. if (!ssr_filtered)
  565. return false;
  566. /* address detected */
  567. if (ssr_filtered & SAR) {
  568. /* read or write request */
  569. if (ssr_raw & STM) {
  570. i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
  571. rcar_i2c_write(priv, ICRXTX, value);
  572. rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
  573. } else {
  574. ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
  575. if (ret)
  576. priv->slave_flags |= ID_SLAVE_NACK;
  577. rcar_i2c_read(priv, ICRXTX); /* dummy read */
  578. rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
  579. }
  580. /* Clear SSR, too, because of old STOPs to other clients than us */
  581. rcar_i2c_write(priv, ICSSR, ~(SAR | SSR) & 0xff);
  582. }
  583. /* master sent stop */
  584. if (ssr_filtered & SSR) {
  585. i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
  586. rcar_i2c_write(priv, ICSCR, SIE | SDBS); /* clear our NACK */
  587. priv->slave_flags &= ~ID_SLAVE_NACK;
  588. rcar_i2c_write(priv, ICSIER, SAR);
  589. rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
  590. }
  591. /* master wants to write to us */
  592. if (ssr_filtered & SDR) {
  593. value = rcar_i2c_read(priv, ICRXTX);
  594. ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
  595. if (ret)
  596. priv->slave_flags |= ID_SLAVE_NACK;
  597. /* Send NACK in case of error, but it will come 1 byte late :( */
  598. rcar_i2c_write(priv, ICSCR, SIE | SDBS |
  599. (priv->slave_flags & ID_SLAVE_NACK ? FNA : 0));
  600. rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
  601. }
  602. /* master wants to read from us */
  603. if (ssr_filtered & SDE) {
  604. i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
  605. rcar_i2c_write(priv, ICRXTX, value);
  606. rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
  607. }
  608. return true;
  609. }
  610. /*
  611. * This driver has a lock-free design because there are IP cores (at least
  612. * R-Car Gen2) which have an inherent race condition in their hardware design.
  613. * There, we need to switch to RCAR_BUS_PHASE_DATA as soon as possible after
  614. * the interrupt was generated, otherwise an unwanted repeated message gets
  615. * generated. It turned out that taking a spinlock at the beginning of the ISR
  616. * was already causing repeated messages. Thus, this driver was converted to
  617. * the now lockless behaviour. Please keep this in mind when hacking the driver.
  618. * R-Car Gen3 seems to have this fixed but earlier versions than R-Car Gen2 are
  619. * likely affected. Therefore, we have different interrupt handler entries.
  620. */
  621. static irqreturn_t rcar_i2c_irq(int irq, struct rcar_i2c_priv *priv, u32 msr)
  622. {
  623. if (!msr) {
  624. if (rcar_i2c_slave_irq(priv))
  625. return IRQ_HANDLED;
  626. return IRQ_NONE;
  627. }
  628. /* Arbitration lost */
  629. if (msr & MAL) {
  630. priv->flags |= ID_DONE | ID_ARBLOST;
  631. goto out;
  632. }
  633. /* Nack */
  634. if (msr & MNR) {
  635. /* HW automatically sends STOP after received NACK */
  636. if (priv->flags & ID_P_NOT_ATOMIC)
  637. rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
  638. priv->flags |= ID_NACK;
  639. goto out;
  640. }
  641. /* Stop */
  642. if (msr & MST) {
  643. priv->msgs_left--; /* The last message also made it */
  644. priv->flags |= ID_DONE;
  645. goto out;
  646. }
  647. if (rcar_i2c_is_recv(priv))
  648. rcar_i2c_irq_recv(priv, msr);
  649. else
  650. rcar_i2c_irq_send(priv, msr);
  651. out:
  652. if (priv->flags & ID_DONE) {
  653. rcar_i2c_write(priv, ICMIER, 0);
  654. rcar_i2c_write(priv, ICMSR, 0);
  655. if (priv->flags & ID_P_NOT_ATOMIC)
  656. wake_up(&priv->wait);
  657. }
  658. return IRQ_HANDLED;
  659. }
  660. static irqreturn_t rcar_i2c_gen2_irq(int irq, void *ptr)
  661. {
  662. struct rcar_i2c_priv *priv = ptr;
  663. u32 msr;
  664. /* Clear START or STOP immediately, except for REPSTART after read */
  665. if (likely(!(priv->flags & ID_REP_AFTER_RD)))
  666. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
  667. /* Only handle interrupts that are currently enabled */
  668. msr = rcar_i2c_read(priv, ICMSR);
  669. if (priv->flags & ID_P_NOT_ATOMIC)
  670. msr &= rcar_i2c_read(priv, ICMIER);
  671. return rcar_i2c_irq(irq, priv, msr);
  672. }
  673. static irqreturn_t rcar_i2c_gen3_irq(int irq, void *ptr)
  674. {
  675. struct rcar_i2c_priv *priv = ptr;
  676. u32 msr;
  677. /* Only handle interrupts that are currently enabled */
  678. msr = rcar_i2c_read(priv, ICMSR);
  679. if (priv->flags & ID_P_NOT_ATOMIC)
  680. msr &= rcar_i2c_read(priv, ICMIER);
  681. /*
  682. * Clear START or STOP immediately, except for REPSTART after read or
  683. * if a spurious interrupt was detected.
  684. */
  685. if (likely(!(priv->flags & ID_REP_AFTER_RD) && msr))
  686. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
  687. return rcar_i2c_irq(irq, priv, msr);
  688. }
  689. static struct dma_chan *rcar_i2c_request_dma_chan(struct device *dev,
  690. enum dma_transfer_direction dir,
  691. dma_addr_t port_addr)
  692. {
  693. struct dma_chan *chan;
  694. struct dma_slave_config cfg;
  695. char *chan_name = dir == DMA_MEM_TO_DEV ? "tx" : "rx";
  696. int ret;
  697. chan = dma_request_chan(dev, chan_name);
  698. if (IS_ERR(chan)) {
  699. dev_dbg(dev, "request_channel failed for %s (%ld)\n",
  700. chan_name, PTR_ERR(chan));
  701. return chan;
  702. }
  703. memset(&cfg, 0, sizeof(cfg));
  704. cfg.direction = dir;
  705. if (dir == DMA_MEM_TO_DEV) {
  706. cfg.dst_addr = port_addr;
  707. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  708. } else {
  709. cfg.src_addr = port_addr;
  710. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  711. }
  712. ret = dmaengine_slave_config(chan, &cfg);
  713. if (ret) {
  714. dev_dbg(dev, "slave_config failed for %s (%d)\n",
  715. chan_name, ret);
  716. dma_release_channel(chan);
  717. return ERR_PTR(ret);
  718. }
  719. dev_dbg(dev, "got DMA channel for %s\n", chan_name);
  720. return chan;
  721. }
  722. static void rcar_i2c_request_dma(struct rcar_i2c_priv *priv,
  723. struct i2c_msg *msg)
  724. {
  725. struct device *dev = rcar_i2c_priv_to_dev(priv);
  726. bool read;
  727. struct dma_chan *chan;
  728. enum dma_transfer_direction dir;
  729. read = msg->flags & I2C_M_RD;
  730. chan = read ? priv->dma_rx : priv->dma_tx;
  731. if (PTR_ERR(chan) != -EPROBE_DEFER)
  732. return;
  733. dir = read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  734. chan = rcar_i2c_request_dma_chan(dev, dir, priv->res->start + ICRXTX);
  735. if (read)
  736. priv->dma_rx = chan;
  737. else
  738. priv->dma_tx = chan;
  739. }
  740. static void rcar_i2c_release_dma(struct rcar_i2c_priv *priv)
  741. {
  742. if (!IS_ERR(priv->dma_tx)) {
  743. dma_release_channel(priv->dma_tx);
  744. priv->dma_tx = ERR_PTR(-EPROBE_DEFER);
  745. }
  746. if (!IS_ERR(priv->dma_rx)) {
  747. dma_release_channel(priv->dma_rx);
  748. priv->dma_rx = ERR_PTR(-EPROBE_DEFER);
  749. }
  750. }
  751. /* I2C is a special case, we need to poll the status of a reset */
  752. static int rcar_i2c_do_reset(struct rcar_i2c_priv *priv)
  753. {
  754. int ret;
  755. /* Don't reset if a slave instance is currently running */
  756. if (priv->slave)
  757. return -EISCONN;
  758. ret = reset_control_reset(priv->rstc);
  759. if (ret)
  760. return ret;
  761. return read_poll_timeout_atomic(reset_control_status, ret, ret == 0, 1,
  762. 100, false, priv->rstc);
  763. }
  764. static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
  765. struct i2c_msg *msgs,
  766. int num)
  767. {
  768. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  769. struct device *dev = rcar_i2c_priv_to_dev(priv);
  770. int i, ret;
  771. long time_left;
  772. priv->flags |= ID_P_NOT_ATOMIC;
  773. pm_runtime_get_sync(dev);
  774. /* Check bus state before init otherwise bus busy info will be lost */
  775. ret = rcar_i2c_bus_barrier(priv);
  776. if (ret < 0)
  777. goto out;
  778. /* Gen3+ needs a reset. That also allows RXDMA once */
  779. if (priv->devtype >= I2C_RCAR_GEN3) {
  780. ret = rcar_i2c_do_reset(priv);
  781. if (ret)
  782. goto out;
  783. priv->flags &= ~ID_P_NO_RXDMA;
  784. }
  785. rcar_i2c_init(priv);
  786. for (i = 0; i < num; i++)
  787. rcar_i2c_request_dma(priv, msgs + i);
  788. rcar_i2c_first_msg(priv, msgs, num);
  789. time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE,
  790. num * adap->timeout);
  791. /* cleanup DMA if it couldn't complete properly due to an error */
  792. if (priv->dma_direction != DMA_NONE)
  793. rcar_i2c_cleanup_dma(priv, true);
  794. if (!time_left) {
  795. rcar_i2c_init(priv);
  796. ret = -ETIMEDOUT;
  797. } else if (priv->flags & ID_NACK) {
  798. ret = -ENXIO;
  799. } else if (priv->flags & ID_ARBLOST) {
  800. ret = -EAGAIN;
  801. } else if (priv->flags & ID_EPROTO) {
  802. ret = -EPROTO;
  803. } else {
  804. ret = num - priv->msgs_left; /* The number of transfer */
  805. }
  806. out:
  807. pm_runtime_put(dev);
  808. if (ret < 0 && ret != -ENXIO)
  809. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  810. return ret;
  811. }
  812. static int rcar_i2c_master_xfer_atomic(struct i2c_adapter *adap,
  813. struct i2c_msg *msgs,
  814. int num)
  815. {
  816. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  817. struct device *dev = rcar_i2c_priv_to_dev(priv);
  818. unsigned long j;
  819. bool time_left;
  820. int ret;
  821. priv->flags &= ~ID_P_NOT_ATOMIC;
  822. pm_runtime_get_sync(dev);
  823. /* Check bus state before init otherwise bus busy info will be lost */
  824. ret = rcar_i2c_bus_barrier(priv);
  825. if (ret < 0)
  826. goto out;
  827. rcar_i2c_init(priv);
  828. rcar_i2c_first_msg(priv, msgs, num);
  829. j = jiffies + num * adap->timeout;
  830. do {
  831. u32 msr = rcar_i2c_read(priv, ICMSR);
  832. msr &= (rcar_i2c_is_recv(priv) ? RCAR_IRQ_RECV : RCAR_IRQ_SEND) | RCAR_IRQ_STOP;
  833. if (msr) {
  834. if (priv->devtype < I2C_RCAR_GEN3)
  835. rcar_i2c_gen2_irq(0, priv);
  836. else
  837. rcar_i2c_gen3_irq(0, priv);
  838. }
  839. time_left = time_before_eq(jiffies, j);
  840. } while (!(priv->flags & ID_DONE) && time_left);
  841. if (!time_left) {
  842. rcar_i2c_init(priv);
  843. ret = -ETIMEDOUT;
  844. } else if (priv->flags & ID_NACK) {
  845. ret = -ENXIO;
  846. } else if (priv->flags & ID_ARBLOST) {
  847. ret = -EAGAIN;
  848. } else if (priv->flags & ID_EPROTO) {
  849. ret = -EPROTO;
  850. } else {
  851. ret = num - priv->msgs_left; /* The number of transfer */
  852. }
  853. out:
  854. pm_runtime_put(dev);
  855. if (ret < 0 && ret != -ENXIO)
  856. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  857. return ret;
  858. }
  859. static int rcar_reg_slave(struct i2c_client *slave)
  860. {
  861. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  862. if (priv->slave)
  863. return -EBUSY;
  864. if (slave->flags & I2C_CLIENT_TEN)
  865. return -EAFNOSUPPORT;
  866. /* Keep device active for slave address detection logic */
  867. pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv));
  868. priv->slave = slave;
  869. rcar_i2c_write(priv, ICSAR, slave->addr);
  870. rcar_i2c_write(priv, ICSSR, 0);
  871. rcar_i2c_write(priv, ICSIER, SAR);
  872. rcar_i2c_write(priv, ICSCR, SIE | SDBS);
  873. return 0;
  874. }
  875. static int rcar_unreg_slave(struct i2c_client *slave)
  876. {
  877. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  878. WARN_ON(!priv->slave);
  879. /* ensure no irq is running before clearing ptr */
  880. disable_irq(priv->irq);
  881. rcar_i2c_reset_slave(priv);
  882. enable_irq(priv->irq);
  883. priv->slave = NULL;
  884. pm_runtime_put(rcar_i2c_priv_to_dev(priv));
  885. return 0;
  886. }
  887. static u32 rcar_i2c_func(struct i2c_adapter *adap)
  888. {
  889. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  890. /*
  891. * This HW can't do:
  892. * I2C_SMBUS_QUICK (setting FSB during START didn't work)
  893. * I2C_M_NOSTART (automatically sends address after START)
  894. * I2C_M_IGNORE_NAK (automatically sends STOP after NAK)
  895. */
  896. u32 func = I2C_FUNC_I2C | I2C_FUNC_SLAVE |
  897. (I2C_FUNC_SMBUS_EMUL_ALL & ~I2C_FUNC_SMBUS_QUICK);
  898. if (priv->flags & ID_P_HOST_NOTIFY)
  899. func |= I2C_FUNC_SMBUS_HOST_NOTIFY;
  900. return func;
  901. }
  902. static const struct i2c_algorithm rcar_i2c_algo = {
  903. .master_xfer = rcar_i2c_master_xfer,
  904. .master_xfer_atomic = rcar_i2c_master_xfer_atomic,
  905. .functionality = rcar_i2c_func,
  906. .reg_slave = rcar_reg_slave,
  907. .unreg_slave = rcar_unreg_slave,
  908. };
  909. static const struct i2c_adapter_quirks rcar_i2c_quirks = {
  910. .flags = I2C_AQ_NO_ZERO_LEN,
  911. };
  912. static const struct of_device_id rcar_i2c_dt_ids[] = {
  913. { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
  914. { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
  915. { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
  916. { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
  917. { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
  918. { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
  919. { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
  920. { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
  921. { .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 },
  922. /* S4 has no FM+ bit */
  923. { .compatible = "renesas,i2c-r8a779f0", .data = (void *)I2C_RCAR_GEN3 },
  924. { .compatible = "renesas,rcar-gen1-i2c", .data = (void *)I2C_RCAR_GEN1 },
  925. { .compatible = "renesas,rcar-gen2-i2c", .data = (void *)I2C_RCAR_GEN2 },
  926. { .compatible = "renesas,rcar-gen3-i2c", .data = (void *)I2C_RCAR_GEN3 },
  927. { .compatible = "renesas,rcar-gen4-i2c", .data = (void *)I2C_RCAR_GEN4 },
  928. {},
  929. };
  930. MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
  931. static int rcar_i2c_probe(struct platform_device *pdev)
  932. {
  933. struct rcar_i2c_priv *priv;
  934. struct i2c_adapter *adap;
  935. struct device *dev = &pdev->dev;
  936. unsigned long irqflags = 0;
  937. irqreturn_t (*irqhandler)(int irq, void *ptr) = rcar_i2c_gen3_irq;
  938. int ret;
  939. /* Otherwise logic will break because some bytes must always use PIO */
  940. BUILD_BUG_ON_MSG(RCAR_MIN_DMA_LEN < 3, "Invalid min DMA length");
  941. priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
  942. if (!priv)
  943. return -ENOMEM;
  944. priv->clk = devm_clk_get(dev, NULL);
  945. if (IS_ERR(priv->clk)) {
  946. dev_err(dev, "cannot get clock\n");
  947. return PTR_ERR(priv->clk);
  948. }
  949. priv->io = devm_platform_get_and_ioremap_resource(pdev, 0, &priv->res);
  950. if (IS_ERR(priv->io))
  951. return PTR_ERR(priv->io);
  952. priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev);
  953. init_waitqueue_head(&priv->wait);
  954. adap = &priv->adap;
  955. adap->nr = pdev->id;
  956. adap->algo = &rcar_i2c_algo;
  957. adap->class = I2C_CLASS_DEPRECATED;
  958. adap->retries = 3;
  959. adap->dev.parent = dev;
  960. adap->dev.of_node = dev->of_node;
  961. adap->bus_recovery_info = &rcar_i2c_bri;
  962. adap->quirks = &rcar_i2c_quirks;
  963. i2c_set_adapdata(adap, priv);
  964. strscpy(adap->name, pdev->name, sizeof(adap->name));
  965. /* Init DMA */
  966. sg_init_table(&priv->sg, 1);
  967. priv->dma_direction = DMA_NONE;
  968. priv->dma_rx = priv->dma_tx = ERR_PTR(-EPROBE_DEFER);
  969. /* Activate device for clock calculation */
  970. pm_runtime_enable(dev);
  971. pm_runtime_get_sync(dev);
  972. ret = rcar_i2c_clock_calculate(priv);
  973. if (ret < 0) {
  974. pm_runtime_put(dev);
  975. goto out_pm_disable;
  976. }
  977. /* Bring hardware to known state */
  978. rcar_i2c_init(priv);
  979. rcar_i2c_reset_slave(priv);
  980. /* Stay always active when multi-master to keep arbitration working */
  981. if (of_property_read_bool(dev->of_node, "multi-master"))
  982. priv->flags |= ID_P_PM_BLOCKED;
  983. else
  984. pm_runtime_put(dev);
  985. if (of_property_read_bool(dev->of_node, "smbus"))
  986. priv->flags |= ID_P_HOST_NOTIFY;
  987. if (priv->devtype < I2C_RCAR_GEN3) {
  988. irqflags |= IRQF_NO_THREAD;
  989. irqhandler = rcar_i2c_gen2_irq;
  990. } else {
  991. /* R-Car Gen3+ needs a reset before every transfer */
  992. priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  993. if (IS_ERR(priv->rstc)) {
  994. ret = PTR_ERR(priv->rstc);
  995. goto out_pm_put;
  996. }
  997. ret = reset_control_status(priv->rstc);
  998. if (ret < 0)
  999. goto out_pm_put;
  1000. /* hard reset disturbs HostNotify local target, so disable it */
  1001. priv->flags &= ~ID_P_HOST_NOTIFY;
  1002. }
  1003. ret = platform_get_irq(pdev, 0);
  1004. if (ret < 0)
  1005. goto out_pm_put;
  1006. priv->irq = ret;
  1007. ret = devm_request_irq(dev, priv->irq, irqhandler, irqflags, dev_name(dev), priv);
  1008. if (ret < 0) {
  1009. dev_err(dev, "cannot get irq %d\n", priv->irq);
  1010. goto out_pm_put;
  1011. }
  1012. platform_set_drvdata(pdev, priv);
  1013. ret = i2c_add_numbered_adapter(adap);
  1014. if (ret < 0)
  1015. goto out_pm_put;
  1016. if (priv->flags & ID_P_HOST_NOTIFY) {
  1017. priv->host_notify_client = i2c_new_slave_host_notify_device(adap);
  1018. if (IS_ERR(priv->host_notify_client)) {
  1019. ret = PTR_ERR(priv->host_notify_client);
  1020. goto out_del_device;
  1021. }
  1022. }
  1023. dev_info(dev, "probed\n");
  1024. return 0;
  1025. out_del_device:
  1026. i2c_del_adapter(&priv->adap);
  1027. out_pm_put:
  1028. if (priv->flags & ID_P_PM_BLOCKED)
  1029. pm_runtime_put(dev);
  1030. out_pm_disable:
  1031. pm_runtime_disable(dev);
  1032. return ret;
  1033. }
  1034. static void rcar_i2c_remove(struct platform_device *pdev)
  1035. {
  1036. struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
  1037. struct device *dev = &pdev->dev;
  1038. if (priv->host_notify_client)
  1039. i2c_free_slave_host_notify_device(priv->host_notify_client);
  1040. i2c_del_adapter(&priv->adap);
  1041. rcar_i2c_release_dma(priv);
  1042. if (priv->flags & ID_P_PM_BLOCKED)
  1043. pm_runtime_put(dev);
  1044. pm_runtime_disable(dev);
  1045. }
  1046. static int rcar_i2c_suspend(struct device *dev)
  1047. {
  1048. struct rcar_i2c_priv *priv = dev_get_drvdata(dev);
  1049. i2c_mark_adapter_suspended(&priv->adap);
  1050. return 0;
  1051. }
  1052. static int rcar_i2c_resume(struct device *dev)
  1053. {
  1054. struct rcar_i2c_priv *priv = dev_get_drvdata(dev);
  1055. i2c_mark_adapter_resumed(&priv->adap);
  1056. return 0;
  1057. }
  1058. static const struct dev_pm_ops rcar_i2c_pm_ops = {
  1059. NOIRQ_SYSTEM_SLEEP_PM_OPS(rcar_i2c_suspend, rcar_i2c_resume)
  1060. };
  1061. static struct platform_driver rcar_i2c_driver = {
  1062. .driver = {
  1063. .name = "i2c-rcar",
  1064. .of_match_table = rcar_i2c_dt_ids,
  1065. .pm = pm_sleep_ptr(&rcar_i2c_pm_ops),
  1066. },
  1067. .probe = rcar_i2c_probe,
  1068. .remove_new = rcar_i2c_remove,
  1069. };
  1070. module_platform_driver(rcar_i2c_driver);
  1071. MODULE_LICENSE("GPL v2");
  1072. MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
  1073. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");