i2c-rcar.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * Driver for the Renesas RCar I2C unit
  3. *
  4. * Copyright (C) 2014 Wolfram Sang <wsa@sang-engineering.com>
  5. *
  6. * Copyright (C) 2012-14 Renesas Solutions Corp.
  7. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  8. *
  9. * This file is based on the drivers/i2c/busses/i2c-sh7760.c
  10. * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
  11. *
  12. * This file used out-of-tree driver i2c-rcar.c
  13. * Copyright (C) 2011-2012 Renesas Electronics Corporation
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; version 2 of the License.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #include <linux/clk.h>
  25. #include <linux/delay.h>
  26. #include <linux/err.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/io.h>
  29. #include <linux/i2c.h>
  30. #include <linux/i2c/i2c-rcar.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/of_device.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/slab.h>
  37. #include <linux/spinlock.h>
  38. /* register offsets */
  39. #define ICSCR 0x00 /* slave ctrl */
  40. #define ICMCR 0x04 /* master ctrl */
  41. #define ICSSR 0x08 /* slave status */
  42. #define ICMSR 0x0C /* master status */
  43. #define ICSIER 0x10 /* slave irq enable */
  44. #define ICMIER 0x14 /* master irq enable */
  45. #define ICCCR 0x18 /* clock dividers */
  46. #define ICSAR 0x1C /* slave address */
  47. #define ICMAR 0x20 /* master address */
  48. #define ICRXTX 0x24 /* data port */
  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. #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
  80. #define RCAR_BUS_PHASE_DATA (MDBS | MIE)
  81. #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
  82. #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
  83. #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
  84. #define RCAR_IRQ_STOP (MST)
  85. #define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF)
  86. #define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF)
  87. #define ID_LAST_MSG (1 << 0)
  88. #define ID_IOERROR (1 << 1)
  89. #define ID_DONE (1 << 2)
  90. #define ID_ARBLOST (1 << 3)
  91. #define ID_NACK (1 << 4)
  92. enum rcar_i2c_type {
  93. I2C_RCAR_GEN1,
  94. I2C_RCAR_GEN2,
  95. };
  96. struct rcar_i2c_priv {
  97. void __iomem *io;
  98. struct i2c_adapter adap;
  99. struct i2c_msg *msg;
  100. struct clk *clk;
  101. spinlock_t lock;
  102. wait_queue_head_t wait;
  103. int pos;
  104. u32 icccr;
  105. u32 flags;
  106. enum rcar_i2c_type devtype;
  107. struct i2c_client *slave;
  108. };
  109. #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
  110. #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
  111. #define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
  112. #define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
  113. #define LOOP_TIMEOUT 1024
  114. static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
  115. {
  116. writel(val, priv->io + reg);
  117. }
  118. static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
  119. {
  120. return readl(priv->io + reg);
  121. }
  122. static void rcar_i2c_init(struct rcar_i2c_priv *priv)
  123. {
  124. /* reset master mode */
  125. rcar_i2c_write(priv, ICMIER, 0);
  126. rcar_i2c_write(priv, ICMCR, 0);
  127. rcar_i2c_write(priv, ICMSR, 0);
  128. rcar_i2c_write(priv, ICMAR, 0);
  129. }
  130. static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
  131. {
  132. int i;
  133. for (i = 0; i < LOOP_TIMEOUT; i++) {
  134. /* make sure that bus is not busy */
  135. if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
  136. return 0;
  137. udelay(1);
  138. }
  139. return -EBUSY;
  140. }
  141. static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
  142. u32 bus_speed,
  143. struct device *dev)
  144. {
  145. u32 scgd, cdf;
  146. u32 round, ick;
  147. u32 scl;
  148. u32 cdf_width;
  149. unsigned long rate;
  150. switch (priv->devtype) {
  151. case I2C_RCAR_GEN1:
  152. cdf_width = 2;
  153. break;
  154. case I2C_RCAR_GEN2:
  155. cdf_width = 3;
  156. break;
  157. default:
  158. dev_err(dev, "device type error\n");
  159. return -EIO;
  160. }
  161. /*
  162. * calculate SCL clock
  163. * see
  164. * ICCCR
  165. *
  166. * ick = clkp / (1 + CDF)
  167. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  168. *
  169. * ick : I2C internal clock < 20 MHz
  170. * ticf : I2C SCL falling time = 35 ns here
  171. * tr : I2C SCL rising time = 200 ns here
  172. * intd : LSI internal delay = 50 ns here
  173. * clkp : peripheral_clk
  174. * F[] : integer up-valuation
  175. */
  176. rate = clk_get_rate(priv->clk);
  177. cdf = rate / 20000000;
  178. if (cdf >= 1U << cdf_width) {
  179. dev_err(dev, "Input clock %lu too high\n", rate);
  180. return -EIO;
  181. }
  182. ick = rate / (cdf + 1);
  183. /*
  184. * it is impossible to calculate large scale
  185. * number on u32. separate it
  186. *
  187. * F[(ticf + tr + intd) * ick]
  188. * = F[(35 + 200 + 50)ns * ick]
  189. * = F[285 * ick / 1000000000]
  190. * = F[(ick / 1000000) * 285 / 1000]
  191. */
  192. round = (ick + 500000) / 1000000 * 285;
  193. round = (round + 500) / 1000;
  194. /*
  195. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  196. *
  197. * Calculation result (= SCL) should be less than
  198. * bus_speed for hardware safety
  199. *
  200. * We could use something along the lines of
  201. * div = ick / (bus_speed + 1) + 1;
  202. * scgd = (div - 20 - round + 7) / 8;
  203. * scl = ick / (20 + (scgd * 8) + round);
  204. * (not fully verified) but that would get pretty involved
  205. */
  206. for (scgd = 0; scgd < 0x40; scgd++) {
  207. scl = ick / (20 + (scgd * 8) + round);
  208. if (scl <= bus_speed)
  209. goto scgd_find;
  210. }
  211. dev_err(dev, "it is impossible to calculate best SCL\n");
  212. return -EIO;
  213. scgd_find:
  214. dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
  215. scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
  216. /*
  217. * keep icccr value
  218. */
  219. priv->icccr = scgd << cdf_width | cdf;
  220. return 0;
  221. }
  222. static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
  223. {
  224. int read = !!rcar_i2c_is_recv(priv);
  225. rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
  226. rcar_i2c_write(priv, ICMSR, 0);
  227. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
  228. rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
  229. }
  230. /*
  231. * interrupt functions
  232. */
  233. static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
  234. {
  235. struct i2c_msg *msg = priv->msg;
  236. /*
  237. * FIXME
  238. * sometimes, unknown interrupt happened.
  239. * Do nothing
  240. */
  241. if (!(msr & MDE))
  242. return 0;
  243. /*
  244. * If address transfer phase finished,
  245. * goto data phase.
  246. */
  247. if (msr & MAT)
  248. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
  249. if (priv->pos < msg->len) {
  250. /*
  251. * Prepare next data to ICRXTX register.
  252. * This data will go to _SHIFT_ register.
  253. *
  254. * *
  255. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  256. */
  257. rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
  258. priv->pos++;
  259. } else {
  260. /*
  261. * The last data was pushed to ICRXTX on _PREV_ empty irq.
  262. * It is on _SHIFT_ register, and will sent to I2C bus.
  263. *
  264. * *
  265. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  266. */
  267. if (priv->flags & ID_LAST_MSG)
  268. /*
  269. * If current msg is the _LAST_ msg,
  270. * prepare stop condition here.
  271. * ID_DONE will be set on STOP irq.
  272. */
  273. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  274. else
  275. /*
  276. * If current msg is _NOT_ last msg,
  277. * it doesn't call stop phase.
  278. * thus, there is no STOP irq.
  279. * return ID_DONE here.
  280. */
  281. return ID_DONE;
  282. }
  283. rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
  284. return 0;
  285. }
  286. static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
  287. {
  288. struct i2c_msg *msg = priv->msg;
  289. /*
  290. * FIXME
  291. * sometimes, unknown interrupt happened.
  292. * Do nothing
  293. */
  294. if (!(msr & MDR))
  295. return 0;
  296. if (msr & MAT) {
  297. /*
  298. * Address transfer phase finished,
  299. * but, there is no data at this point.
  300. * Do nothing.
  301. */
  302. } else if (priv->pos < msg->len) {
  303. /*
  304. * get received data
  305. */
  306. msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
  307. priv->pos++;
  308. }
  309. /*
  310. * If next received data is the _LAST_,
  311. * go to STOP phase,
  312. * otherwise, go to DATA phase.
  313. */
  314. if (priv->pos + 1 >= msg->len)
  315. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  316. else
  317. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
  318. rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
  319. return 0;
  320. }
  321. static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
  322. {
  323. u32 ssr_raw, ssr_filtered;
  324. u8 value;
  325. ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
  326. ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);
  327. if (!ssr_filtered)
  328. return false;
  329. /* address detected */
  330. if (ssr_filtered & SAR) {
  331. /* read or write request */
  332. if (ssr_raw & STM) {
  333. i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
  334. rcar_i2c_write(priv, ICRXTX, value);
  335. rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
  336. } else {
  337. i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
  338. rcar_i2c_read(priv, ICRXTX); /* dummy read */
  339. rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
  340. }
  341. rcar_i2c_write(priv, ICSSR, ~SAR & 0xff);
  342. }
  343. /* master sent stop */
  344. if (ssr_filtered & SSR) {
  345. i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
  346. rcar_i2c_write(priv, ICSIER, SAR | SSR);
  347. rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
  348. }
  349. /* master wants to write to us */
  350. if (ssr_filtered & SDR) {
  351. int ret;
  352. value = rcar_i2c_read(priv, ICRXTX);
  353. ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
  354. /* Send NACK in case of error */
  355. rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
  356. rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
  357. }
  358. /* master wants to read from us */
  359. if (ssr_filtered & SDE) {
  360. i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
  361. rcar_i2c_write(priv, ICRXTX, value);
  362. rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
  363. }
  364. return true;
  365. }
  366. static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
  367. {
  368. struct rcar_i2c_priv *priv = ptr;
  369. irqreturn_t result = IRQ_HANDLED;
  370. u32 msr;
  371. /*-------------- spin lock -----------------*/
  372. spin_lock(&priv->lock);
  373. if (rcar_i2c_slave_irq(priv))
  374. goto exit;
  375. msr = rcar_i2c_read(priv, ICMSR);
  376. /* Only handle interrupts that are currently enabled */
  377. msr &= rcar_i2c_read(priv, ICMIER);
  378. if (!msr) {
  379. result = IRQ_NONE;
  380. goto exit;
  381. }
  382. /* Arbitration lost */
  383. if (msr & MAL) {
  384. rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
  385. goto out;
  386. }
  387. /* Nack */
  388. if (msr & MNR) {
  389. /* go to stop phase */
  390. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  391. rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
  392. rcar_i2c_flags_set(priv, ID_NACK);
  393. goto out;
  394. }
  395. /* Stop */
  396. if (msr & MST) {
  397. rcar_i2c_flags_set(priv, ID_DONE);
  398. goto out;
  399. }
  400. if (rcar_i2c_is_recv(priv))
  401. rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
  402. else
  403. rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
  404. out:
  405. if (rcar_i2c_flags_has(priv, ID_DONE)) {
  406. rcar_i2c_write(priv, ICMIER, 0);
  407. rcar_i2c_write(priv, ICMSR, 0);
  408. wake_up(&priv->wait);
  409. }
  410. exit:
  411. spin_unlock(&priv->lock);
  412. /*-------------- spin unlock -----------------*/
  413. return result;
  414. }
  415. static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
  416. struct i2c_msg *msgs,
  417. int num)
  418. {
  419. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  420. struct device *dev = rcar_i2c_priv_to_dev(priv);
  421. unsigned long flags;
  422. int i, ret, timeout;
  423. pm_runtime_get_sync(dev);
  424. /*-------------- spin lock -----------------*/
  425. spin_lock_irqsave(&priv->lock, flags);
  426. rcar_i2c_init(priv);
  427. /* start clock */
  428. rcar_i2c_write(priv, ICCCR, priv->icccr);
  429. spin_unlock_irqrestore(&priv->lock, flags);
  430. /*-------------- spin unlock -----------------*/
  431. ret = rcar_i2c_bus_barrier(priv);
  432. if (ret < 0)
  433. goto out;
  434. for (i = 0; i < num; i++) {
  435. /* This HW can't send STOP after address phase */
  436. if (msgs[i].len == 0) {
  437. ret = -EOPNOTSUPP;
  438. break;
  439. }
  440. /*-------------- spin lock -----------------*/
  441. spin_lock_irqsave(&priv->lock, flags);
  442. /* init each data */
  443. priv->msg = &msgs[i];
  444. priv->pos = 0;
  445. priv->flags = 0;
  446. if (i == num - 1)
  447. rcar_i2c_flags_set(priv, ID_LAST_MSG);
  448. rcar_i2c_prepare_msg(priv);
  449. spin_unlock_irqrestore(&priv->lock, flags);
  450. /*-------------- spin unlock -----------------*/
  451. timeout = wait_event_timeout(priv->wait,
  452. rcar_i2c_flags_has(priv, ID_DONE),
  453. 5 * HZ);
  454. if (!timeout) {
  455. ret = -ETIMEDOUT;
  456. break;
  457. }
  458. if (rcar_i2c_flags_has(priv, ID_NACK)) {
  459. ret = -ENXIO;
  460. break;
  461. }
  462. if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
  463. ret = -EAGAIN;
  464. break;
  465. }
  466. if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
  467. ret = -EIO;
  468. break;
  469. }
  470. ret = i + 1; /* The number of transfer */
  471. }
  472. out:
  473. pm_runtime_put(dev);
  474. if (ret < 0 && ret != -ENXIO)
  475. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  476. return ret;
  477. }
  478. static int rcar_reg_slave(struct i2c_client *slave)
  479. {
  480. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  481. if (priv->slave)
  482. return -EBUSY;
  483. if (slave->flags & I2C_CLIENT_TEN)
  484. return -EAFNOSUPPORT;
  485. pm_runtime_forbid(rcar_i2c_priv_to_dev(priv));
  486. priv->slave = slave;
  487. rcar_i2c_write(priv, ICSAR, slave->addr);
  488. rcar_i2c_write(priv, ICSSR, 0);
  489. rcar_i2c_write(priv, ICSIER, SAR | SSR);
  490. rcar_i2c_write(priv, ICSCR, SIE | SDBS);
  491. return 0;
  492. }
  493. static int rcar_unreg_slave(struct i2c_client *slave)
  494. {
  495. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  496. WARN_ON(!priv->slave);
  497. rcar_i2c_write(priv, ICSIER, 0);
  498. rcar_i2c_write(priv, ICSCR, 0);
  499. priv->slave = NULL;
  500. pm_runtime_allow(rcar_i2c_priv_to_dev(priv));
  501. return 0;
  502. }
  503. static u32 rcar_i2c_func(struct i2c_adapter *adap)
  504. {
  505. /* This HW can't do SMBUS_QUICK and NOSTART */
  506. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  507. }
  508. static const struct i2c_algorithm rcar_i2c_algo = {
  509. .master_xfer = rcar_i2c_master_xfer,
  510. .functionality = rcar_i2c_func,
  511. .reg_slave = rcar_reg_slave,
  512. .unreg_slave = rcar_unreg_slave,
  513. };
  514. static const struct of_device_id rcar_i2c_dt_ids[] = {
  515. { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
  516. { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
  517. { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
  518. { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
  519. { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
  520. { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
  521. { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
  522. { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
  523. {},
  524. };
  525. MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
  526. static int rcar_i2c_probe(struct platform_device *pdev)
  527. {
  528. struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
  529. struct rcar_i2c_priv *priv;
  530. struct i2c_adapter *adap;
  531. struct resource *res;
  532. struct device *dev = &pdev->dev;
  533. u32 bus_speed;
  534. int irq, ret;
  535. priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
  536. if (!priv)
  537. return -ENOMEM;
  538. priv->clk = devm_clk_get(dev, NULL);
  539. if (IS_ERR(priv->clk)) {
  540. dev_err(dev, "cannot get clock\n");
  541. return PTR_ERR(priv->clk);
  542. }
  543. bus_speed = 100000; /* default 100 kHz */
  544. ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
  545. if (ret < 0 && pdata && pdata->bus_speed)
  546. bus_speed = pdata->bus_speed;
  547. if (pdev->dev.of_node)
  548. priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
  549. dev)->data;
  550. else
  551. priv->devtype = platform_get_device_id(pdev)->driver_data;
  552. ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
  553. if (ret < 0)
  554. return ret;
  555. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  556. priv->io = devm_ioremap_resource(dev, res);
  557. if (IS_ERR(priv->io))
  558. return PTR_ERR(priv->io);
  559. irq = platform_get_irq(pdev, 0);
  560. init_waitqueue_head(&priv->wait);
  561. spin_lock_init(&priv->lock);
  562. adap = &priv->adap;
  563. adap->nr = pdev->id;
  564. adap->algo = &rcar_i2c_algo;
  565. adap->class = I2C_CLASS_DEPRECATED;
  566. adap->retries = 3;
  567. adap->dev.parent = dev;
  568. adap->dev.of_node = dev->of_node;
  569. i2c_set_adapdata(adap, priv);
  570. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  571. ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
  572. dev_name(dev), priv);
  573. if (ret < 0) {
  574. dev_err(dev, "cannot get irq %d\n", irq);
  575. return ret;
  576. }
  577. ret = i2c_add_numbered_adapter(adap);
  578. if (ret < 0) {
  579. dev_err(dev, "reg adap failed: %d\n", ret);
  580. return ret;
  581. }
  582. pm_runtime_enable(dev);
  583. platform_set_drvdata(pdev, priv);
  584. dev_info(dev, "probed\n");
  585. return 0;
  586. }
  587. static int rcar_i2c_remove(struct platform_device *pdev)
  588. {
  589. struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
  590. struct device *dev = &pdev->dev;
  591. i2c_del_adapter(&priv->adap);
  592. pm_runtime_disable(dev);
  593. return 0;
  594. }
  595. static struct platform_device_id rcar_i2c_id_table[] = {
  596. { "i2c-rcar", I2C_RCAR_GEN1 },
  597. { "i2c-rcar_gen1", I2C_RCAR_GEN1 },
  598. { "i2c-rcar_gen2", I2C_RCAR_GEN2 },
  599. {},
  600. };
  601. MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
  602. static struct platform_driver rcar_i2c_driver = {
  603. .driver = {
  604. .name = "i2c-rcar",
  605. .of_match_table = rcar_i2c_dt_ids,
  606. },
  607. .probe = rcar_i2c_probe,
  608. .remove = rcar_i2c_remove,
  609. .id_table = rcar_i2c_id_table,
  610. };
  611. module_platform_driver(rcar_i2c_driver);
  612. MODULE_LICENSE("GPL v2");
  613. MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
  614. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");