i2c-rcar.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * drivers/i2c/busses/i2c-rcar.c
  3. *
  4. * Copyright (C) 2012 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * This file is based on the drivers/i2c/busses/i2c-sh7760.c
  8. * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
  9. *
  10. * This file used out-of-tree driver i2c-rcar.c
  11. * Copyright (C) 2011-2012 Renesas Electronics Corporation
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/clk.h>
  27. #include <linux/delay.h>
  28. #include <linux/err.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/io.h>
  32. #include <linux/i2c.h>
  33. #include <linux/i2c/i2c-rcar.h>
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/slab.h>
  39. #include <linux/spinlock.h>
  40. /* register offsets */
  41. #define ICSCR 0x00 /* slave ctrl */
  42. #define ICMCR 0x04 /* master ctrl */
  43. #define ICSSR 0x08 /* slave status */
  44. #define ICMSR 0x0C /* master status */
  45. #define ICSIER 0x10 /* slave irq enable */
  46. #define ICMIER 0x14 /* master irq enable */
  47. #define ICCCR 0x18 /* clock dividers */
  48. #define ICSAR 0x1C /* slave address */
  49. #define ICMAR 0x20 /* master address */
  50. #define ICRXTX 0x24 /* data port */
  51. /* ICMCR */
  52. #define MDBS (1 << 7) /* non-fifo mode switch */
  53. #define FSCL (1 << 6) /* override SCL pin */
  54. #define FSDA (1 << 5) /* override SDA pin */
  55. #define OBPC (1 << 4) /* override pins */
  56. #define MIE (1 << 3) /* master if enable */
  57. #define TSBE (1 << 2)
  58. #define FSB (1 << 1) /* force stop bit */
  59. #define ESG (1 << 0) /* en startbit gen */
  60. /* ICMSR */
  61. #define MNR (1 << 6) /* nack received */
  62. #define MAL (1 << 5) /* arbitration lost */
  63. #define MST (1 << 4) /* sent a stop */
  64. #define MDE (1 << 3)
  65. #define MDT (1 << 2)
  66. #define MDR (1 << 1)
  67. #define MAT (1 << 0) /* slave addr xfer done */
  68. /* ICMIE */
  69. #define MNRE (1 << 6) /* nack irq en */
  70. #define MALE (1 << 5) /* arblos irq en */
  71. #define MSTE (1 << 4) /* stop irq en */
  72. #define MDEE (1 << 3)
  73. #define MDTE (1 << 2)
  74. #define MDRE (1 << 1)
  75. #define MATE (1 << 0) /* address sent irq en */
  76. enum {
  77. RCAR_BUS_PHASE_ADDR,
  78. RCAR_BUS_PHASE_DATA,
  79. RCAR_BUS_PHASE_STOP,
  80. };
  81. enum {
  82. RCAR_IRQ_CLOSE,
  83. RCAR_IRQ_OPEN_FOR_SEND,
  84. RCAR_IRQ_OPEN_FOR_RECV,
  85. RCAR_IRQ_OPEN_FOR_STOP,
  86. };
  87. /*
  88. * flags
  89. */
  90. #define ID_LAST_MSG (1 << 0)
  91. #define ID_IOERROR (1 << 1)
  92. #define ID_DONE (1 << 2)
  93. #define ID_ARBLOST (1 << 3)
  94. #define ID_NACK (1 << 4)
  95. struct rcar_i2c_priv {
  96. void __iomem *io;
  97. struct i2c_adapter adap;
  98. struct i2c_msg *msg;
  99. spinlock_t lock;
  100. wait_queue_head_t wait;
  101. int pos;
  102. int irq;
  103. u32 icccr;
  104. u32 flags;
  105. };
  106. #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
  107. #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
  108. #define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
  109. #define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
  110. #define LOOP_TIMEOUT 1024
  111. /*
  112. * basic functions
  113. */
  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. /*
  125. * reset slave mode.
  126. * slave mode is not used on this driver
  127. */
  128. rcar_i2c_write(priv, ICSIER, 0);
  129. rcar_i2c_write(priv, ICSAR, 0);
  130. rcar_i2c_write(priv, ICSCR, 0);
  131. rcar_i2c_write(priv, ICSSR, 0);
  132. /* reset master mode */
  133. rcar_i2c_write(priv, ICMIER, 0);
  134. rcar_i2c_write(priv, ICMCR, 0);
  135. rcar_i2c_write(priv, ICMSR, 0);
  136. rcar_i2c_write(priv, ICMAR, 0);
  137. }
  138. static void rcar_i2c_irq_mask(struct rcar_i2c_priv *priv, int open)
  139. {
  140. u32 val = MNRE | MALE | MSTE | MATE; /* default */
  141. switch (open) {
  142. case RCAR_IRQ_OPEN_FOR_SEND:
  143. val |= MDEE; /* default + send */
  144. break;
  145. case RCAR_IRQ_OPEN_FOR_RECV:
  146. val |= MDRE; /* default + read */
  147. break;
  148. case RCAR_IRQ_OPEN_FOR_STOP:
  149. val = MSTE; /* stop irq only */
  150. break;
  151. case RCAR_IRQ_CLOSE:
  152. default:
  153. val = 0; /* all close */
  154. break;
  155. }
  156. rcar_i2c_write(priv, ICMIER, val);
  157. }
  158. static void rcar_i2c_set_addr(struct rcar_i2c_priv *priv, u32 recv)
  159. {
  160. rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | recv);
  161. }
  162. /*
  163. * bus control functions
  164. */
  165. static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
  166. {
  167. int i;
  168. for (i = 0; i < LOOP_TIMEOUT; i++) {
  169. /* make sure that bus is not busy */
  170. if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
  171. return 0;
  172. udelay(1);
  173. }
  174. return -EBUSY;
  175. }
  176. static void rcar_i2c_bus_phase(struct rcar_i2c_priv *priv, int phase)
  177. {
  178. switch (phase) {
  179. case RCAR_BUS_PHASE_ADDR:
  180. rcar_i2c_write(priv, ICMCR, MDBS | MIE | ESG);
  181. break;
  182. case RCAR_BUS_PHASE_DATA:
  183. rcar_i2c_write(priv, ICMCR, MDBS | MIE);
  184. break;
  185. case RCAR_BUS_PHASE_STOP:
  186. rcar_i2c_write(priv, ICMCR, MDBS | MIE | FSB);
  187. break;
  188. }
  189. }
  190. /*
  191. * clock function
  192. */
  193. static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
  194. u32 bus_speed,
  195. struct device *dev)
  196. {
  197. struct clk *clkp = clk_get(NULL, "peripheral_clk");
  198. u32 scgd, cdf;
  199. u32 round, ick;
  200. u32 scl;
  201. if (!clkp) {
  202. dev_err(dev, "there is no peripheral_clk\n");
  203. return -EIO;
  204. }
  205. /*
  206. * calculate SCL clock
  207. * see
  208. * ICCCR
  209. *
  210. * ick = clkp / (1 + CDF)
  211. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  212. *
  213. * ick : I2C internal clock < 20 MHz
  214. * ticf : I2C SCL falling time = 35 ns here
  215. * tr : I2C SCL rising time = 200 ns here
  216. * intd : LSI internal delay = 50 ns here
  217. * clkp : peripheral_clk
  218. * F[] : integer up-valuation
  219. */
  220. for (cdf = 0; cdf < 4; cdf++) {
  221. ick = clk_get_rate(clkp) / (1 + cdf);
  222. if (ick < 20000000)
  223. goto ick_find;
  224. }
  225. dev_err(dev, "there is no best CDF\n");
  226. return -EIO;
  227. ick_find:
  228. /*
  229. * it is impossible to calculate large scale
  230. * number on u32. separate it
  231. *
  232. * F[(ticf + tr + intd) * ick]
  233. * = F[(35 + 200 + 50)ns * ick]
  234. * = F[285 * ick / 1000000000]
  235. * = F[(ick / 1000000) * 285 / 1000]
  236. */
  237. round = (ick + 500000) / 1000000 * 285;
  238. round = (round + 500) / 1000;
  239. /*
  240. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  241. *
  242. * Calculation result (= SCL) should be less than
  243. * bus_speed for hardware safety
  244. */
  245. for (scgd = 0; scgd < 0x40; scgd++) {
  246. scl = ick / (20 + (scgd * 8) + round);
  247. if (scl <= bus_speed)
  248. goto scgd_find;
  249. }
  250. dev_err(dev, "it is impossible to calculate best SCL\n");
  251. return -EIO;
  252. scgd_find:
  253. dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
  254. scl, bus_speed, clk_get_rate(clkp), round, cdf, scgd);
  255. /*
  256. * keep icccr value
  257. */
  258. priv->icccr = (scgd << 2 | cdf);
  259. return 0;
  260. }
  261. static void rcar_i2c_clock_start(struct rcar_i2c_priv *priv)
  262. {
  263. rcar_i2c_write(priv, ICCCR, priv->icccr);
  264. }
  265. /*
  266. * status functions
  267. */
  268. static u32 rcar_i2c_status_get(struct rcar_i2c_priv *priv)
  269. {
  270. return rcar_i2c_read(priv, ICMSR);
  271. }
  272. #define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
  273. static void rcar_i2c_status_bit_clear(struct rcar_i2c_priv *priv, u32 bit)
  274. {
  275. rcar_i2c_write(priv, ICMSR, ~bit);
  276. }
  277. /*
  278. * recv/send functions
  279. */
  280. static int rcar_i2c_recv(struct rcar_i2c_priv *priv)
  281. {
  282. rcar_i2c_set_addr(priv, 1);
  283. rcar_i2c_status_clear(priv);
  284. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
  285. rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_RECV);
  286. return 0;
  287. }
  288. static int rcar_i2c_send(struct rcar_i2c_priv *priv)
  289. {
  290. int ret;
  291. /*
  292. * It should check bus status when send case
  293. */
  294. ret = rcar_i2c_bus_barrier(priv);
  295. if (ret < 0)
  296. return ret;
  297. rcar_i2c_set_addr(priv, 0);
  298. rcar_i2c_status_clear(priv);
  299. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
  300. rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_SEND);
  301. return 0;
  302. }
  303. #define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
  304. #define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
  305. /*
  306. * interrupt functions
  307. */
  308. static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
  309. {
  310. struct i2c_msg *msg = priv->msg;
  311. /*
  312. * FIXME
  313. * sometimes, unknown interrupt happened.
  314. * Do nothing
  315. */
  316. if (!(msr & MDE))
  317. return 0;
  318. /*
  319. * If address transfer phase finished,
  320. * goto data phase.
  321. */
  322. if (msr & MAT)
  323. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
  324. if (priv->pos < msg->len) {
  325. /*
  326. * Prepare next data to ICRXTX register.
  327. * This data will go to _SHIFT_ register.
  328. *
  329. * *
  330. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  331. */
  332. rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
  333. priv->pos++;
  334. } else {
  335. /*
  336. * The last data was pushed to ICRXTX on _PREV_ empty irq.
  337. * It is on _SHIFT_ register, and will sent to I2C bus.
  338. *
  339. * *
  340. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  341. */
  342. if (priv->flags & ID_LAST_MSG)
  343. /*
  344. * If current msg is the _LAST_ msg,
  345. * prepare stop condition here.
  346. * ID_DONE will be set on STOP irq.
  347. */
  348. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
  349. else
  350. /*
  351. * If current msg is _NOT_ last msg,
  352. * it doesn't call stop phase.
  353. * thus, there is no STOP irq.
  354. * return ID_DONE here.
  355. */
  356. return ID_DONE;
  357. }
  358. rcar_i2c_send_restart(priv);
  359. return 0;
  360. }
  361. static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
  362. {
  363. struct i2c_msg *msg = priv->msg;
  364. /*
  365. * FIXME
  366. * sometimes, unknown interrupt happened.
  367. * Do nothing
  368. */
  369. if (!(msr & MDR))
  370. return 0;
  371. if (msr & MAT) {
  372. /*
  373. * Address transfer phase finished,
  374. * but, there is no data at this point.
  375. * Do nothing.
  376. */
  377. } else if (priv->pos < msg->len) {
  378. /*
  379. * get received data
  380. */
  381. msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
  382. priv->pos++;
  383. }
  384. /*
  385. * If next received data is the _LAST_,
  386. * go to STOP phase,
  387. * otherwise, go to DATA phase.
  388. */
  389. if (priv->pos + 1 >= msg->len)
  390. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
  391. else
  392. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
  393. rcar_i2c_recv_restart(priv);
  394. return 0;
  395. }
  396. static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
  397. {
  398. struct rcar_i2c_priv *priv = ptr;
  399. struct device *dev = rcar_i2c_priv_to_dev(priv);
  400. u32 msr;
  401. /*-------------- spin lock -----------------*/
  402. spin_lock(&priv->lock);
  403. msr = rcar_i2c_status_get(priv);
  404. /*
  405. * Arbitration lost
  406. */
  407. if (msr & MAL) {
  408. /*
  409. * CAUTION
  410. *
  411. * When arbitration lost, device become _slave_ mode.
  412. */
  413. dev_dbg(dev, "Arbitration Lost\n");
  414. rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
  415. goto out;
  416. }
  417. /*
  418. * Stop
  419. */
  420. if (msr & MST) {
  421. dev_dbg(dev, "Stop\n");
  422. rcar_i2c_flags_set(priv, ID_DONE);
  423. goto out;
  424. }
  425. /*
  426. * Nack
  427. */
  428. if (msr & MNR) {
  429. dev_dbg(dev, "Nack\n");
  430. /* go to stop phase */
  431. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
  432. rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_STOP);
  433. rcar_i2c_flags_set(priv, ID_NACK);
  434. goto out;
  435. }
  436. /*
  437. * recv/send
  438. */
  439. if (rcar_i2c_is_recv(priv))
  440. rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
  441. else
  442. rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
  443. out:
  444. if (rcar_i2c_flags_has(priv, ID_DONE)) {
  445. rcar_i2c_irq_mask(priv, RCAR_IRQ_CLOSE);
  446. rcar_i2c_status_clear(priv);
  447. wake_up(&priv->wait);
  448. }
  449. spin_unlock(&priv->lock);
  450. /*-------------- spin unlock -----------------*/
  451. return IRQ_HANDLED;
  452. }
  453. static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
  454. struct i2c_msg *msgs,
  455. int num)
  456. {
  457. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  458. struct device *dev = rcar_i2c_priv_to_dev(priv);
  459. unsigned long flags;
  460. int i, ret, timeout;
  461. pm_runtime_get_sync(dev);
  462. /*-------------- spin lock -----------------*/
  463. spin_lock_irqsave(&priv->lock, flags);
  464. rcar_i2c_init(priv);
  465. rcar_i2c_clock_start(priv);
  466. spin_unlock_irqrestore(&priv->lock, flags);
  467. /*-------------- spin unlock -----------------*/
  468. ret = -EINVAL;
  469. for (i = 0; i < num; i++) {
  470. /*-------------- spin lock -----------------*/
  471. spin_lock_irqsave(&priv->lock, flags);
  472. /* init each data */
  473. priv->msg = &msgs[i];
  474. priv->pos = 0;
  475. priv->flags = 0;
  476. if (priv->msg == &msgs[num - 1])
  477. rcar_i2c_flags_set(priv, ID_LAST_MSG);
  478. /* start send/recv */
  479. if (rcar_i2c_is_recv(priv))
  480. ret = rcar_i2c_recv(priv);
  481. else
  482. ret = rcar_i2c_send(priv);
  483. spin_unlock_irqrestore(&priv->lock, flags);
  484. /*-------------- spin unlock -----------------*/
  485. if (ret < 0)
  486. break;
  487. /*
  488. * wait result
  489. */
  490. timeout = wait_event_timeout(priv->wait,
  491. rcar_i2c_flags_has(priv, ID_DONE),
  492. 5 * HZ);
  493. if (!timeout) {
  494. ret = -ETIMEDOUT;
  495. break;
  496. }
  497. /*
  498. * error handling
  499. */
  500. if (rcar_i2c_flags_has(priv, ID_NACK)) {
  501. ret = -EREMOTEIO;
  502. break;
  503. }
  504. if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
  505. ret = -EAGAIN;
  506. break;
  507. }
  508. if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
  509. ret = -EIO;
  510. break;
  511. }
  512. ret = i + 1; /* The number of transfer */
  513. }
  514. pm_runtime_put(dev);
  515. if (ret < 0)
  516. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  517. return ret;
  518. }
  519. static u32 rcar_i2c_func(struct i2c_adapter *adap)
  520. {
  521. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  522. }
  523. static const struct i2c_algorithm rcar_i2c_algo = {
  524. .master_xfer = rcar_i2c_master_xfer,
  525. .functionality = rcar_i2c_func,
  526. };
  527. static int rcar_i2c_probe(struct platform_device *pdev)
  528. {
  529. struct i2c_rcar_platform_data *pdata = pdev->dev.platform_data;
  530. struct rcar_i2c_priv *priv;
  531. struct i2c_adapter *adap;
  532. struct resource *res;
  533. struct device *dev = &pdev->dev;
  534. u32 bus_speed;
  535. int ret;
  536. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  537. if (!res) {
  538. dev_err(dev, "no mmio resources\n");
  539. return -ENODEV;
  540. }
  541. priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
  542. if (!priv) {
  543. dev_err(dev, "no mem for private data\n");
  544. return -ENOMEM;
  545. }
  546. bus_speed = 100000; /* default 100 kHz */
  547. if (pdata && pdata->bus_speed)
  548. bus_speed = pdata->bus_speed;
  549. ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
  550. if (ret < 0)
  551. return ret;
  552. priv->io = devm_ioremap_resource(dev, res);
  553. if (IS_ERR(priv->io))
  554. return PTR_ERR(priv->io);
  555. priv->irq = platform_get_irq(pdev, 0);
  556. init_waitqueue_head(&priv->wait);
  557. spin_lock_init(&priv->lock);
  558. adap = &priv->adap;
  559. adap->nr = pdev->id;
  560. adap->algo = &rcar_i2c_algo;
  561. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  562. adap->retries = 3;
  563. adap->dev.parent = dev;
  564. i2c_set_adapdata(adap, priv);
  565. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  566. ret = devm_request_irq(dev, priv->irq, rcar_i2c_irq, 0,
  567. dev_name(dev), priv);
  568. if (ret < 0) {
  569. dev_err(dev, "cannot get irq %d\n", priv->irq);
  570. return ret;
  571. }
  572. ret = i2c_add_numbered_adapter(adap);
  573. if (ret < 0) {
  574. dev_err(dev, "reg adap failed: %d\n", ret);
  575. return ret;
  576. }
  577. pm_runtime_enable(dev);
  578. platform_set_drvdata(pdev, priv);
  579. dev_info(dev, "probed\n");
  580. return 0;
  581. }
  582. static int rcar_i2c_remove(struct platform_device *pdev)
  583. {
  584. struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
  585. struct device *dev = &pdev->dev;
  586. i2c_del_adapter(&priv->adap);
  587. pm_runtime_disable(dev);
  588. return 0;
  589. }
  590. static struct platform_driver rcar_i2c_driver = {
  591. .driver = {
  592. .name = "i2c-rcar",
  593. .owner = THIS_MODULE,
  594. },
  595. .probe = rcar_i2c_probe,
  596. .remove = rcar_i2c_remove,
  597. };
  598. module_platform_driver(rcar_i2c_driver);
  599. MODULE_LICENSE("GPL");
  600. MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
  601. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");