i2c-rcar.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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_REQ_READ_START, &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_REQ_WRITE_START, &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_REQ_WRITE_END, &value);
  354. /* Send NACK in case of error */
  355. rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
  356. i2c_slave_event(priv->slave, I2C_SLAVE_REQ_WRITE_START, &value);
  357. rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
  358. }
  359. /* master wants to read from us */
  360. if (ssr_filtered & SDE) {
  361. i2c_slave_event(priv->slave, I2C_SLAVE_REQ_READ_END, &value);
  362. i2c_slave_event(priv->slave, I2C_SLAVE_REQ_READ_START, &value);
  363. rcar_i2c_write(priv, ICRXTX, value);
  364. rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
  365. }
  366. return true;
  367. }
  368. static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
  369. {
  370. struct rcar_i2c_priv *priv = ptr;
  371. irqreturn_t result = IRQ_HANDLED;
  372. u32 msr;
  373. /*-------------- spin lock -----------------*/
  374. spin_lock(&priv->lock);
  375. if (rcar_i2c_slave_irq(priv))
  376. goto exit;
  377. msr = rcar_i2c_read(priv, ICMSR);
  378. /* Only handle interrupts that are currently enabled */
  379. msr &= rcar_i2c_read(priv, ICMIER);
  380. if (!msr) {
  381. result = IRQ_NONE;
  382. goto exit;
  383. }
  384. /* Arbitration lost */
  385. if (msr & MAL) {
  386. rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
  387. goto out;
  388. }
  389. /* Nack */
  390. if (msr & MNR) {
  391. /* go to stop phase */
  392. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  393. rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
  394. rcar_i2c_flags_set(priv, ID_NACK);
  395. goto out;
  396. }
  397. /* Stop */
  398. if (msr & MST) {
  399. rcar_i2c_flags_set(priv, ID_DONE);
  400. goto out;
  401. }
  402. if (rcar_i2c_is_recv(priv))
  403. rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
  404. else
  405. rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
  406. out:
  407. if (rcar_i2c_flags_has(priv, ID_DONE)) {
  408. rcar_i2c_write(priv, ICMIER, 0);
  409. rcar_i2c_write(priv, ICMSR, 0);
  410. wake_up(&priv->wait);
  411. }
  412. exit:
  413. spin_unlock(&priv->lock);
  414. /*-------------- spin unlock -----------------*/
  415. return result;
  416. }
  417. static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
  418. struct i2c_msg *msgs,
  419. int num)
  420. {
  421. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  422. struct device *dev = rcar_i2c_priv_to_dev(priv);
  423. unsigned long flags;
  424. int i, ret, timeout;
  425. pm_runtime_get_sync(dev);
  426. /*-------------- spin lock -----------------*/
  427. spin_lock_irqsave(&priv->lock, flags);
  428. rcar_i2c_init(priv);
  429. /* start clock */
  430. rcar_i2c_write(priv, ICCCR, priv->icccr);
  431. spin_unlock_irqrestore(&priv->lock, flags);
  432. /*-------------- spin unlock -----------------*/
  433. ret = rcar_i2c_bus_barrier(priv);
  434. if (ret < 0)
  435. goto out;
  436. for (i = 0; i < num; i++) {
  437. /* This HW can't send STOP after address phase */
  438. if (msgs[i].len == 0) {
  439. ret = -EOPNOTSUPP;
  440. break;
  441. }
  442. /*-------------- spin lock -----------------*/
  443. spin_lock_irqsave(&priv->lock, flags);
  444. /* init each data */
  445. priv->msg = &msgs[i];
  446. priv->pos = 0;
  447. priv->flags = 0;
  448. if (i == num - 1)
  449. rcar_i2c_flags_set(priv, ID_LAST_MSG);
  450. rcar_i2c_prepare_msg(priv);
  451. spin_unlock_irqrestore(&priv->lock, flags);
  452. /*-------------- spin unlock -----------------*/
  453. timeout = wait_event_timeout(priv->wait,
  454. rcar_i2c_flags_has(priv, ID_DONE),
  455. 5 * HZ);
  456. if (!timeout) {
  457. ret = -ETIMEDOUT;
  458. break;
  459. }
  460. if (rcar_i2c_flags_has(priv, ID_NACK)) {
  461. ret = -ENXIO;
  462. break;
  463. }
  464. if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
  465. ret = -EAGAIN;
  466. break;
  467. }
  468. if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
  469. ret = -EIO;
  470. break;
  471. }
  472. ret = i + 1; /* The number of transfer */
  473. }
  474. out:
  475. pm_runtime_put(dev);
  476. if (ret < 0 && ret != -ENXIO)
  477. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  478. return ret;
  479. }
  480. static int rcar_reg_slave(struct i2c_client *slave)
  481. {
  482. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  483. if (priv->slave)
  484. return -EBUSY;
  485. if (slave->flags & I2C_CLIENT_TEN)
  486. return -EAFNOSUPPORT;
  487. pm_runtime_forbid(rcar_i2c_priv_to_dev(priv));
  488. priv->slave = slave;
  489. rcar_i2c_write(priv, ICSAR, slave->addr);
  490. rcar_i2c_write(priv, ICSSR, 0);
  491. rcar_i2c_write(priv, ICSIER, SAR | SSR);
  492. rcar_i2c_write(priv, ICSCR, SIE | SDBS);
  493. return 0;
  494. }
  495. static int rcar_unreg_slave(struct i2c_client *slave)
  496. {
  497. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  498. WARN_ON(!priv->slave);
  499. rcar_i2c_write(priv, ICSIER, 0);
  500. rcar_i2c_write(priv, ICSCR, 0);
  501. priv->slave = NULL;
  502. pm_runtime_allow(rcar_i2c_priv_to_dev(priv));
  503. return 0;
  504. }
  505. static u32 rcar_i2c_func(struct i2c_adapter *adap)
  506. {
  507. /* This HW can't do SMBUS_QUICK and NOSTART */
  508. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  509. }
  510. static const struct i2c_algorithm rcar_i2c_algo = {
  511. .master_xfer = rcar_i2c_master_xfer,
  512. .functionality = rcar_i2c_func,
  513. .reg_slave = rcar_reg_slave,
  514. .unreg_slave = rcar_unreg_slave,
  515. };
  516. static const struct of_device_id rcar_i2c_dt_ids[] = {
  517. { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
  518. { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
  519. { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
  520. { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
  521. { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
  522. { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
  523. { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
  524. { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
  525. {},
  526. };
  527. MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
  528. static int rcar_i2c_probe(struct platform_device *pdev)
  529. {
  530. struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
  531. struct rcar_i2c_priv *priv;
  532. struct i2c_adapter *adap;
  533. struct resource *res;
  534. struct device *dev = &pdev->dev;
  535. u32 bus_speed;
  536. int irq, ret;
  537. priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
  538. if (!priv)
  539. return -ENOMEM;
  540. priv->clk = devm_clk_get(dev, NULL);
  541. if (IS_ERR(priv->clk)) {
  542. dev_err(dev, "cannot get clock\n");
  543. return PTR_ERR(priv->clk);
  544. }
  545. bus_speed = 100000; /* default 100 kHz */
  546. ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
  547. if (ret < 0 && pdata && pdata->bus_speed)
  548. bus_speed = pdata->bus_speed;
  549. if (pdev->dev.of_node)
  550. priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
  551. dev)->data;
  552. else
  553. priv->devtype = platform_get_device_id(pdev)->driver_data;
  554. ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
  555. if (ret < 0)
  556. return ret;
  557. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  558. priv->io = devm_ioremap_resource(dev, res);
  559. if (IS_ERR(priv->io))
  560. return PTR_ERR(priv->io);
  561. irq = platform_get_irq(pdev, 0);
  562. init_waitqueue_head(&priv->wait);
  563. spin_lock_init(&priv->lock);
  564. adap = &priv->adap;
  565. adap->nr = pdev->id;
  566. adap->algo = &rcar_i2c_algo;
  567. adap->class = I2C_CLASS_DEPRECATED;
  568. adap->retries = 3;
  569. adap->dev.parent = dev;
  570. adap->dev.of_node = dev->of_node;
  571. i2c_set_adapdata(adap, priv);
  572. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  573. ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
  574. dev_name(dev), priv);
  575. if (ret < 0) {
  576. dev_err(dev, "cannot get irq %d\n", irq);
  577. return ret;
  578. }
  579. ret = i2c_add_numbered_adapter(adap);
  580. if (ret < 0) {
  581. dev_err(dev, "reg adap failed: %d\n", ret);
  582. return ret;
  583. }
  584. pm_runtime_enable(dev);
  585. platform_set_drvdata(pdev, priv);
  586. dev_info(dev, "probed\n");
  587. return 0;
  588. }
  589. static int rcar_i2c_remove(struct platform_device *pdev)
  590. {
  591. struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
  592. struct device *dev = &pdev->dev;
  593. i2c_del_adapter(&priv->adap);
  594. pm_runtime_disable(dev);
  595. return 0;
  596. }
  597. static struct platform_device_id rcar_i2c_id_table[] = {
  598. { "i2c-rcar", I2C_RCAR_GEN1 },
  599. { "i2c-rcar_gen1", I2C_RCAR_GEN1 },
  600. { "i2c-rcar_gen2", I2C_RCAR_GEN2 },
  601. {},
  602. };
  603. MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
  604. static struct platform_driver rcar_i2c_driver = {
  605. .driver = {
  606. .name = "i2c-rcar",
  607. .of_match_table = rcar_i2c_dt_ids,
  608. },
  609. .probe = rcar_i2c_probe,
  610. .remove = rcar_i2c_remove,
  611. .id_table = rcar_i2c_id_table,
  612. };
  613. module_platform_driver(rcar_i2c_driver);
  614. MODULE_LICENSE("GPL v2");
  615. MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
  616. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");