i2c-rcar.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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/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. /* ICSCR */
  48. #define SDBS (1 << 3) /* slave data buffer select */
  49. #define SIE (1 << 2) /* slave interface enable */
  50. #define GCAE (1 << 1) /* general call address enable */
  51. #define FNA (1 << 0) /* forced non acknowledgment */
  52. /* ICMCR */
  53. #define MDBS (1 << 7) /* non-fifo mode switch */
  54. #define FSCL (1 << 6) /* override SCL pin */
  55. #define FSDA (1 << 5) /* override SDA pin */
  56. #define OBPC (1 << 4) /* override pins */
  57. #define MIE (1 << 3) /* master if enable */
  58. #define TSBE (1 << 2)
  59. #define FSB (1 << 1) /* force stop bit */
  60. #define ESG (1 << 0) /* en startbit gen */
  61. /* ICSSR (also for ICSIER) */
  62. #define GCAR (1 << 6) /* general call received */
  63. #define STM (1 << 5) /* slave transmit mode */
  64. #define SSR (1 << 4) /* stop received */
  65. #define SDE (1 << 3) /* slave data empty */
  66. #define SDT (1 << 2) /* slave data transmitted */
  67. #define SDR (1 << 1) /* slave data received */
  68. #define SAR (1 << 0) /* slave addr received */
  69. /* ICMSR (also for ICMIE) */
  70. #define MNR (1 << 6) /* nack received */
  71. #define MAL (1 << 5) /* arbitration lost */
  72. #define MST (1 << 4) /* sent a stop */
  73. #define MDE (1 << 3)
  74. #define MDT (1 << 2)
  75. #define MDR (1 << 1)
  76. #define MAT (1 << 0) /* slave addr xfer done */
  77. #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
  78. #define RCAR_BUS_PHASE_DATA (MDBS | MIE)
  79. #define RCAR_BUS_MASK_DATA (~(ESG | FSB) & 0xFF)
  80. #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
  81. #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
  82. #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
  83. #define RCAR_IRQ_STOP (MST)
  84. #define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF)
  85. #define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF)
  86. #define ID_LAST_MSG (1 << 0)
  87. #define ID_DONE (1 << 2)
  88. #define ID_ARBLOST (1 << 3)
  89. #define ID_NACK (1 << 4)
  90. enum rcar_i2c_type {
  91. I2C_RCAR_GEN1,
  92. I2C_RCAR_GEN2,
  93. I2C_RCAR_GEN3,
  94. };
  95. struct rcar_i2c_priv {
  96. void __iomem *io;
  97. struct i2c_adapter adap;
  98. struct i2c_msg *msg;
  99. int msgs_left;
  100. struct clk *clk;
  101. wait_queue_head_t wait;
  102. int pos;
  103. u32 icccr;
  104. u32 flags;
  105. enum rcar_i2c_type devtype;
  106. struct i2c_client *slave;
  107. };
  108. #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
  109. #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
  110. #define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
  111. #define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
  112. #define LOOP_TIMEOUT 1024
  113. static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
  114. {
  115. writel(val, priv->io + reg);
  116. }
  117. static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
  118. {
  119. return readl(priv->io + reg);
  120. }
  121. static void rcar_i2c_init(struct rcar_i2c_priv *priv)
  122. {
  123. /* reset master mode */
  124. rcar_i2c_write(priv, ICMIER, 0);
  125. rcar_i2c_write(priv, ICMCR, MDBS);
  126. rcar_i2c_write(priv, ICMSR, 0);
  127. /* start clock */
  128. rcar_i2c_write(priv, ICCCR, priv->icccr);
  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. case I2C_RCAR_GEN3:
  156. cdf_width = 3;
  157. break;
  158. default:
  159. dev_err(dev, "device type error\n");
  160. return -EIO;
  161. }
  162. /*
  163. * calculate SCL clock
  164. * see
  165. * ICCCR
  166. *
  167. * ick = clkp / (1 + CDF)
  168. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  169. *
  170. * ick : I2C internal clock < 20 MHz
  171. * ticf : I2C SCL falling time = 35 ns here
  172. * tr : I2C SCL rising time = 200 ns here
  173. * intd : LSI internal delay = 50 ns here
  174. * clkp : peripheral_clk
  175. * F[] : integer up-valuation
  176. */
  177. rate = clk_get_rate(priv->clk);
  178. cdf = rate / 20000000;
  179. if (cdf >= 1U << cdf_width) {
  180. dev_err(dev, "Input clock %lu too high\n", rate);
  181. return -EIO;
  182. }
  183. ick = rate / (cdf + 1);
  184. /*
  185. * it is impossible to calculate large scale
  186. * number on u32. separate it
  187. *
  188. * F[(ticf + tr + intd) * ick]
  189. * = F[(35 + 200 + 50)ns * ick]
  190. * = F[285 * ick / 1000000000]
  191. * = F[(ick / 1000000) * 285 / 1000]
  192. */
  193. round = (ick + 500000) / 1000000 * 285;
  194. round = (round + 500) / 1000;
  195. /*
  196. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  197. *
  198. * Calculation result (= SCL) should be less than
  199. * bus_speed for hardware safety
  200. *
  201. * We could use something along the lines of
  202. * div = ick / (bus_speed + 1) + 1;
  203. * scgd = (div - 20 - round + 7) / 8;
  204. * scl = ick / (20 + (scgd * 8) + round);
  205. * (not fully verified) but that would get pretty involved
  206. */
  207. for (scgd = 0; scgd < 0x40; scgd++) {
  208. scl = ick / (20 + (scgd * 8) + round);
  209. if (scl <= bus_speed)
  210. goto scgd_find;
  211. }
  212. dev_err(dev, "it is impossible to calculate best SCL\n");
  213. return -EIO;
  214. scgd_find:
  215. dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
  216. scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
  217. /*
  218. * keep icccr value
  219. */
  220. priv->icccr = scgd << cdf_width | cdf;
  221. return 0;
  222. }
  223. static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
  224. {
  225. int read = !!rcar_i2c_is_recv(priv);
  226. priv->pos = 0;
  227. priv->flags = 0;
  228. if (priv->msgs_left == 1)
  229. rcar_i2c_flags_set(priv, ID_LAST_MSG);
  230. rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
  231. rcar_i2c_write(priv, ICMSR, 0);
  232. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
  233. rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
  234. }
  235. static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
  236. {
  237. priv->msg++;
  238. priv->msgs_left--;
  239. rcar_i2c_prepare_msg(priv);
  240. }
  241. /*
  242. * interrupt functions
  243. */
  244. static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
  245. {
  246. struct i2c_msg *msg = priv->msg;
  247. /*
  248. * FIXME
  249. * sometimes, unknown interrupt happened.
  250. * Do nothing
  251. */
  252. if (!(msr & MDE))
  253. return;
  254. if (priv->pos < msg->len) {
  255. /*
  256. * Prepare next data to ICRXTX register.
  257. * This data will go to _SHIFT_ register.
  258. *
  259. * *
  260. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  261. */
  262. rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
  263. priv->pos++;
  264. } else {
  265. /*
  266. * The last data was pushed to ICRXTX on _PREV_ empty irq.
  267. * It is on _SHIFT_ register, and will sent to I2C bus.
  268. *
  269. * *
  270. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  271. */
  272. if (priv->flags & ID_LAST_MSG) {
  273. /*
  274. * If current msg is the _LAST_ msg,
  275. * prepare stop condition here.
  276. * ID_DONE will be set on STOP irq.
  277. */
  278. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  279. } else {
  280. rcar_i2c_next_msg(priv);
  281. return;
  282. }
  283. }
  284. rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
  285. }
  286. static void 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;
  296. if (msr & MAT) {
  297. /* Address transfer phase finished, but no data at this point. */
  298. } else if (priv->pos < msg->len) {
  299. /*
  300. * get received data
  301. */
  302. msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
  303. priv->pos++;
  304. }
  305. /*
  306. * If next received data is the _LAST_,
  307. * go to STOP phase,
  308. * otherwise, go to DATA phase.
  309. */
  310. if (priv->pos + 1 >= msg->len)
  311. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  312. if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
  313. rcar_i2c_next_msg(priv);
  314. else
  315. rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
  316. }
  317. static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
  318. {
  319. u32 ssr_raw, ssr_filtered;
  320. u8 value;
  321. ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
  322. ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);
  323. if (!ssr_filtered)
  324. return false;
  325. /* address detected */
  326. if (ssr_filtered & SAR) {
  327. /* read or write request */
  328. if (ssr_raw & STM) {
  329. i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
  330. rcar_i2c_write(priv, ICRXTX, value);
  331. rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
  332. } else {
  333. i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
  334. rcar_i2c_read(priv, ICRXTX); /* dummy read */
  335. rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
  336. }
  337. rcar_i2c_write(priv, ICSSR, ~SAR & 0xff);
  338. }
  339. /* master sent stop */
  340. if (ssr_filtered & SSR) {
  341. i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
  342. rcar_i2c_write(priv, ICSIER, SAR | SSR);
  343. rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
  344. }
  345. /* master wants to write to us */
  346. if (ssr_filtered & SDR) {
  347. int ret;
  348. value = rcar_i2c_read(priv, ICRXTX);
  349. ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
  350. /* Send NACK in case of error */
  351. rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
  352. rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
  353. }
  354. /* master wants to read from us */
  355. if (ssr_filtered & SDE) {
  356. i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
  357. rcar_i2c_write(priv, ICRXTX, value);
  358. rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
  359. }
  360. return true;
  361. }
  362. static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
  363. {
  364. struct rcar_i2c_priv *priv = ptr;
  365. u32 msr, val;
  366. /* Clear START or STOP as soon as we can */
  367. val = rcar_i2c_read(priv, ICMCR);
  368. rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA);
  369. msr = rcar_i2c_read(priv, ICMSR);
  370. /* Only handle interrupts that are currently enabled */
  371. msr &= rcar_i2c_read(priv, ICMIER);
  372. if (!msr) {
  373. if (rcar_i2c_slave_irq(priv))
  374. return IRQ_HANDLED;
  375. return IRQ_NONE;
  376. }
  377. /* Arbitration lost */
  378. if (msr & MAL) {
  379. rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
  380. goto out;
  381. }
  382. /* Nack */
  383. if (msr & MNR) {
  384. /* HW automatically sends STOP after received NACK */
  385. rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
  386. rcar_i2c_flags_set(priv, ID_NACK);
  387. goto out;
  388. }
  389. /* Stop */
  390. if (msr & MST) {
  391. priv->msgs_left--; /* The last message also made it */
  392. rcar_i2c_flags_set(priv, ID_DONE);
  393. goto out;
  394. }
  395. if (rcar_i2c_is_recv(priv))
  396. rcar_i2c_irq_recv(priv, msr);
  397. else
  398. rcar_i2c_irq_send(priv, msr);
  399. out:
  400. if (rcar_i2c_flags_has(priv, ID_DONE)) {
  401. rcar_i2c_write(priv, ICMIER, 0);
  402. rcar_i2c_write(priv, ICMSR, 0);
  403. wake_up(&priv->wait);
  404. }
  405. return IRQ_HANDLED;
  406. }
  407. static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
  408. struct i2c_msg *msgs,
  409. int num)
  410. {
  411. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  412. struct device *dev = rcar_i2c_priv_to_dev(priv);
  413. int i, ret;
  414. long time_left;
  415. pm_runtime_get_sync(dev);
  416. rcar_i2c_init(priv);
  417. ret = rcar_i2c_bus_barrier(priv);
  418. if (ret < 0)
  419. goto out;
  420. for (i = 0; i < num; i++) {
  421. /* This HW can't send STOP after address phase */
  422. if (msgs[i].len == 0) {
  423. ret = -EOPNOTSUPP;
  424. goto out;
  425. }
  426. }
  427. /* init data */
  428. priv->msg = msgs;
  429. priv->msgs_left = num;
  430. rcar_i2c_prepare_msg(priv);
  431. time_left = wait_event_timeout(priv->wait,
  432. rcar_i2c_flags_has(priv, ID_DONE),
  433. num * adap->timeout);
  434. if (!time_left) {
  435. rcar_i2c_init(priv);
  436. ret = -ETIMEDOUT;
  437. } else if (rcar_i2c_flags_has(priv, ID_NACK)) {
  438. ret = -ENXIO;
  439. } else if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
  440. ret = -EAGAIN;
  441. } else {
  442. ret = num - priv->msgs_left; /* The number of transfer */
  443. }
  444. out:
  445. pm_runtime_put(dev);
  446. if (ret < 0 && ret != -ENXIO)
  447. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  448. return ret;
  449. }
  450. static int rcar_reg_slave(struct i2c_client *slave)
  451. {
  452. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  453. if (priv->slave)
  454. return -EBUSY;
  455. if (slave->flags & I2C_CLIENT_TEN)
  456. return -EAFNOSUPPORT;
  457. pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv));
  458. priv->slave = slave;
  459. rcar_i2c_write(priv, ICSAR, slave->addr);
  460. rcar_i2c_write(priv, ICSSR, 0);
  461. rcar_i2c_write(priv, ICSIER, SAR | SSR);
  462. rcar_i2c_write(priv, ICSCR, SIE | SDBS);
  463. return 0;
  464. }
  465. static int rcar_unreg_slave(struct i2c_client *slave)
  466. {
  467. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  468. WARN_ON(!priv->slave);
  469. rcar_i2c_write(priv, ICSIER, 0);
  470. rcar_i2c_write(priv, ICSCR, 0);
  471. priv->slave = NULL;
  472. pm_runtime_put(rcar_i2c_priv_to_dev(priv));
  473. return 0;
  474. }
  475. static u32 rcar_i2c_func(struct i2c_adapter *adap)
  476. {
  477. /* This HW can't do SMBUS_QUICK and NOSTART */
  478. return I2C_FUNC_I2C | I2C_FUNC_SLAVE |
  479. (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  480. }
  481. static const struct i2c_algorithm rcar_i2c_algo = {
  482. .master_xfer = rcar_i2c_master_xfer,
  483. .functionality = rcar_i2c_func,
  484. .reg_slave = rcar_reg_slave,
  485. .unreg_slave = rcar_unreg_slave,
  486. };
  487. static const struct of_device_id rcar_i2c_dt_ids[] = {
  488. { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
  489. { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
  490. { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
  491. { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
  492. { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
  493. { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
  494. { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
  495. { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
  496. { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
  497. {},
  498. };
  499. MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
  500. static int rcar_i2c_probe(struct platform_device *pdev)
  501. {
  502. struct rcar_i2c_priv *priv;
  503. struct i2c_adapter *adap;
  504. struct resource *res;
  505. struct device *dev = &pdev->dev;
  506. u32 bus_speed;
  507. int irq, ret;
  508. priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
  509. if (!priv)
  510. return -ENOMEM;
  511. priv->clk = devm_clk_get(dev, NULL);
  512. if (IS_ERR(priv->clk)) {
  513. dev_err(dev, "cannot get clock\n");
  514. return PTR_ERR(priv->clk);
  515. }
  516. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  517. priv->io = devm_ioremap_resource(dev, res);
  518. if (IS_ERR(priv->io))
  519. return PTR_ERR(priv->io);
  520. bus_speed = 100000; /* default 100 kHz */
  521. of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
  522. priv->devtype = (enum rcar_i2c_type)of_match_device(rcar_i2c_dt_ids, dev)->data;
  523. pm_runtime_enable(dev);
  524. pm_runtime_get_sync(dev);
  525. ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
  526. if (ret < 0)
  527. goto out_pm_put;
  528. pm_runtime_put(dev);
  529. irq = platform_get_irq(pdev, 0);
  530. init_waitqueue_head(&priv->wait);
  531. adap = &priv->adap;
  532. adap->nr = pdev->id;
  533. adap->algo = &rcar_i2c_algo;
  534. adap->class = I2C_CLASS_DEPRECATED;
  535. adap->retries = 3;
  536. adap->dev.parent = dev;
  537. adap->dev.of_node = dev->of_node;
  538. i2c_set_adapdata(adap, priv);
  539. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  540. ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
  541. dev_name(dev), priv);
  542. if (ret < 0) {
  543. dev_err(dev, "cannot get irq %d\n", irq);
  544. goto out_pm_disable;
  545. }
  546. platform_set_drvdata(pdev, priv);
  547. ret = i2c_add_numbered_adapter(adap);
  548. if (ret < 0) {
  549. dev_err(dev, "reg adap failed: %d\n", ret);
  550. goto out_pm_disable;
  551. }
  552. dev_info(dev, "probed\n");
  553. return 0;
  554. out_pm_put:
  555. pm_runtime_put(dev);
  556. out_pm_disable:
  557. pm_runtime_disable(dev);
  558. return ret;
  559. }
  560. static int rcar_i2c_remove(struct platform_device *pdev)
  561. {
  562. struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
  563. struct device *dev = &pdev->dev;
  564. i2c_del_adapter(&priv->adap);
  565. pm_runtime_disable(dev);
  566. return 0;
  567. }
  568. static struct platform_driver rcar_i2c_driver = {
  569. .driver = {
  570. .name = "i2c-rcar",
  571. .of_match_table = rcar_i2c_dt_ids,
  572. },
  573. .probe = rcar_i2c_probe,
  574. .remove = rcar_i2c_remove,
  575. };
  576. module_platform_driver(rcar_i2c_driver);
  577. MODULE_LICENSE("GPL v2");
  578. MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
  579. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");