i2c-rcar.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. enum rcar_i2c_type {
  96. I2C_RCAR_H1,
  97. I2C_RCAR_H2,
  98. };
  99. struct rcar_i2c_priv {
  100. void __iomem *io;
  101. struct i2c_adapter adap;
  102. struct i2c_msg *msg;
  103. spinlock_t lock;
  104. wait_queue_head_t wait;
  105. int pos;
  106. int irq;
  107. u32 icccr;
  108. u32 flags;
  109. enum rcar_i2c_type devtype;
  110. };
  111. #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
  112. #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
  113. #define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
  114. #define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
  115. #define LOOP_TIMEOUT 1024
  116. /*
  117. * basic functions
  118. */
  119. static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
  120. {
  121. writel(val, priv->io + reg);
  122. }
  123. static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
  124. {
  125. return readl(priv->io + reg);
  126. }
  127. static void rcar_i2c_init(struct rcar_i2c_priv *priv)
  128. {
  129. /*
  130. * reset slave mode.
  131. * slave mode is not used on this driver
  132. */
  133. rcar_i2c_write(priv, ICSIER, 0);
  134. rcar_i2c_write(priv, ICSAR, 0);
  135. rcar_i2c_write(priv, ICSCR, 0);
  136. rcar_i2c_write(priv, ICSSR, 0);
  137. /* reset master mode */
  138. rcar_i2c_write(priv, ICMIER, 0);
  139. rcar_i2c_write(priv, ICMCR, 0);
  140. rcar_i2c_write(priv, ICMSR, 0);
  141. rcar_i2c_write(priv, ICMAR, 0);
  142. }
  143. static void rcar_i2c_irq_mask(struct rcar_i2c_priv *priv, int open)
  144. {
  145. u32 val = MNRE | MALE | MSTE | MATE; /* default */
  146. switch (open) {
  147. case RCAR_IRQ_OPEN_FOR_SEND:
  148. val |= MDEE; /* default + send */
  149. break;
  150. case RCAR_IRQ_OPEN_FOR_RECV:
  151. val |= MDRE; /* default + read */
  152. break;
  153. case RCAR_IRQ_OPEN_FOR_STOP:
  154. val = MSTE; /* stop irq only */
  155. break;
  156. case RCAR_IRQ_CLOSE:
  157. default:
  158. val = 0; /* all close */
  159. break;
  160. }
  161. rcar_i2c_write(priv, ICMIER, val);
  162. }
  163. static void rcar_i2c_set_addr(struct rcar_i2c_priv *priv, u32 recv)
  164. {
  165. rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | recv);
  166. }
  167. /*
  168. * bus control functions
  169. */
  170. static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
  171. {
  172. int i;
  173. for (i = 0; i < LOOP_TIMEOUT; i++) {
  174. /* make sure that bus is not busy */
  175. if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
  176. return 0;
  177. udelay(1);
  178. }
  179. return -EBUSY;
  180. }
  181. static void rcar_i2c_bus_phase(struct rcar_i2c_priv *priv, int phase)
  182. {
  183. switch (phase) {
  184. case RCAR_BUS_PHASE_ADDR:
  185. rcar_i2c_write(priv, ICMCR, MDBS | MIE | ESG);
  186. break;
  187. case RCAR_BUS_PHASE_DATA:
  188. rcar_i2c_write(priv, ICMCR, MDBS | MIE);
  189. break;
  190. case RCAR_BUS_PHASE_STOP:
  191. rcar_i2c_write(priv, ICMCR, MDBS | MIE | FSB);
  192. break;
  193. }
  194. }
  195. /*
  196. * clock function
  197. */
  198. static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
  199. u32 bus_speed,
  200. struct device *dev)
  201. {
  202. struct clk *clkp = clk_get(NULL, "peripheral_clk");
  203. u32 scgd, cdf;
  204. u32 round, ick;
  205. u32 scl;
  206. u32 cdf_width;
  207. if (!clkp) {
  208. dev_err(dev, "there is no peripheral_clk\n");
  209. return -EIO;
  210. }
  211. switch (priv->devtype) {
  212. case I2C_RCAR_H1:
  213. cdf_width = 2;
  214. break;
  215. case I2C_RCAR_H2:
  216. cdf_width = 3;
  217. break;
  218. default:
  219. dev_err(dev, "device type error\n");
  220. return -EIO;
  221. }
  222. /*
  223. * calculate SCL clock
  224. * see
  225. * ICCCR
  226. *
  227. * ick = clkp / (1 + CDF)
  228. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  229. *
  230. * ick : I2C internal clock < 20 MHz
  231. * ticf : I2C SCL falling time = 35 ns here
  232. * tr : I2C SCL rising time = 200 ns here
  233. * intd : LSI internal delay = 50 ns here
  234. * clkp : peripheral_clk
  235. * F[] : integer up-valuation
  236. */
  237. for (cdf = 0; cdf < (1 << cdf_width); cdf++) {
  238. ick = clk_get_rate(clkp) / (1 + cdf);
  239. if (ick < 20000000)
  240. goto ick_find;
  241. }
  242. dev_err(dev, "there is no best CDF\n");
  243. return -EIO;
  244. ick_find:
  245. /*
  246. * it is impossible to calculate large scale
  247. * number on u32. separate it
  248. *
  249. * F[(ticf + tr + intd) * ick]
  250. * = F[(35 + 200 + 50)ns * ick]
  251. * = F[285 * ick / 1000000000]
  252. * = F[(ick / 1000000) * 285 / 1000]
  253. */
  254. round = (ick + 500000) / 1000000 * 285;
  255. round = (round + 500) / 1000;
  256. /*
  257. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  258. *
  259. * Calculation result (= SCL) should be less than
  260. * bus_speed for hardware safety
  261. */
  262. for (scgd = 0; scgd < 0x40; scgd++) {
  263. scl = ick / (20 + (scgd * 8) + round);
  264. if (scl <= bus_speed)
  265. goto scgd_find;
  266. }
  267. dev_err(dev, "it is impossible to calculate best SCL\n");
  268. return -EIO;
  269. scgd_find:
  270. dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
  271. scl, bus_speed, clk_get_rate(clkp), round, cdf, scgd);
  272. /*
  273. * keep icccr value
  274. */
  275. priv->icccr = (scgd << (cdf_width) | cdf);
  276. return 0;
  277. }
  278. static void rcar_i2c_clock_start(struct rcar_i2c_priv *priv)
  279. {
  280. rcar_i2c_write(priv, ICCCR, priv->icccr);
  281. }
  282. /*
  283. * status functions
  284. */
  285. static u32 rcar_i2c_status_get(struct rcar_i2c_priv *priv)
  286. {
  287. return rcar_i2c_read(priv, ICMSR);
  288. }
  289. #define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
  290. static void rcar_i2c_status_bit_clear(struct rcar_i2c_priv *priv, u32 bit)
  291. {
  292. rcar_i2c_write(priv, ICMSR, ~bit);
  293. }
  294. /*
  295. * recv/send functions
  296. */
  297. static int rcar_i2c_recv(struct rcar_i2c_priv *priv)
  298. {
  299. rcar_i2c_set_addr(priv, 1);
  300. rcar_i2c_status_clear(priv);
  301. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
  302. rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_RECV);
  303. return 0;
  304. }
  305. static int rcar_i2c_send(struct rcar_i2c_priv *priv)
  306. {
  307. int ret;
  308. /*
  309. * It should check bus status when send case
  310. */
  311. ret = rcar_i2c_bus_barrier(priv);
  312. if (ret < 0)
  313. return ret;
  314. rcar_i2c_set_addr(priv, 0);
  315. rcar_i2c_status_clear(priv);
  316. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
  317. rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_SEND);
  318. return 0;
  319. }
  320. #define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
  321. #define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
  322. /*
  323. * interrupt functions
  324. */
  325. static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
  326. {
  327. struct i2c_msg *msg = priv->msg;
  328. /*
  329. * FIXME
  330. * sometimes, unknown interrupt happened.
  331. * Do nothing
  332. */
  333. if (!(msr & MDE))
  334. return 0;
  335. /*
  336. * If address transfer phase finished,
  337. * goto data phase.
  338. */
  339. if (msr & MAT)
  340. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
  341. if (priv->pos < msg->len) {
  342. /*
  343. * Prepare next data to ICRXTX register.
  344. * This data will go to _SHIFT_ register.
  345. *
  346. * *
  347. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  348. */
  349. rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
  350. priv->pos++;
  351. } else {
  352. /*
  353. * The last data was pushed to ICRXTX on _PREV_ empty irq.
  354. * It is on _SHIFT_ register, and will sent to I2C bus.
  355. *
  356. * *
  357. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  358. */
  359. if (priv->flags & ID_LAST_MSG)
  360. /*
  361. * If current msg is the _LAST_ msg,
  362. * prepare stop condition here.
  363. * ID_DONE will be set on STOP irq.
  364. */
  365. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
  366. else
  367. /*
  368. * If current msg is _NOT_ last msg,
  369. * it doesn't call stop phase.
  370. * thus, there is no STOP irq.
  371. * return ID_DONE here.
  372. */
  373. return ID_DONE;
  374. }
  375. rcar_i2c_send_restart(priv);
  376. return 0;
  377. }
  378. static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
  379. {
  380. struct i2c_msg *msg = priv->msg;
  381. /*
  382. * FIXME
  383. * sometimes, unknown interrupt happened.
  384. * Do nothing
  385. */
  386. if (!(msr & MDR))
  387. return 0;
  388. if (msr & MAT) {
  389. /*
  390. * Address transfer phase finished,
  391. * but, there is no data at this point.
  392. * Do nothing.
  393. */
  394. } else if (priv->pos < msg->len) {
  395. /*
  396. * get received data
  397. */
  398. msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
  399. priv->pos++;
  400. }
  401. /*
  402. * If next received data is the _LAST_,
  403. * go to STOP phase,
  404. * otherwise, go to DATA phase.
  405. */
  406. if (priv->pos + 1 >= msg->len)
  407. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
  408. else
  409. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
  410. rcar_i2c_recv_restart(priv);
  411. return 0;
  412. }
  413. static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
  414. {
  415. struct rcar_i2c_priv *priv = ptr;
  416. struct device *dev = rcar_i2c_priv_to_dev(priv);
  417. u32 msr;
  418. /*-------------- spin lock -----------------*/
  419. spin_lock(&priv->lock);
  420. msr = rcar_i2c_status_get(priv);
  421. /*
  422. * Arbitration lost
  423. */
  424. if (msr & MAL) {
  425. /*
  426. * CAUTION
  427. *
  428. * When arbitration lost, device become _slave_ mode.
  429. */
  430. dev_dbg(dev, "Arbitration Lost\n");
  431. rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
  432. goto out;
  433. }
  434. /*
  435. * Stop
  436. */
  437. if (msr & MST) {
  438. dev_dbg(dev, "Stop\n");
  439. rcar_i2c_flags_set(priv, ID_DONE);
  440. goto out;
  441. }
  442. /*
  443. * Nack
  444. */
  445. if (msr & MNR) {
  446. dev_dbg(dev, "Nack\n");
  447. /* go to stop phase */
  448. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
  449. rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_STOP);
  450. rcar_i2c_flags_set(priv, ID_NACK);
  451. goto out;
  452. }
  453. /*
  454. * recv/send
  455. */
  456. if (rcar_i2c_is_recv(priv))
  457. rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
  458. else
  459. rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
  460. out:
  461. if (rcar_i2c_flags_has(priv, ID_DONE)) {
  462. rcar_i2c_irq_mask(priv, RCAR_IRQ_CLOSE);
  463. rcar_i2c_status_clear(priv);
  464. wake_up(&priv->wait);
  465. }
  466. spin_unlock(&priv->lock);
  467. /*-------------- spin unlock -----------------*/
  468. return IRQ_HANDLED;
  469. }
  470. static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
  471. struct i2c_msg *msgs,
  472. int num)
  473. {
  474. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  475. struct device *dev = rcar_i2c_priv_to_dev(priv);
  476. unsigned long flags;
  477. int i, ret, timeout;
  478. pm_runtime_get_sync(dev);
  479. /*-------------- spin lock -----------------*/
  480. spin_lock_irqsave(&priv->lock, flags);
  481. rcar_i2c_init(priv);
  482. rcar_i2c_clock_start(priv);
  483. spin_unlock_irqrestore(&priv->lock, flags);
  484. /*-------------- spin unlock -----------------*/
  485. ret = -EINVAL;
  486. for (i = 0; i < num; i++) {
  487. /*-------------- spin lock -----------------*/
  488. spin_lock_irqsave(&priv->lock, flags);
  489. /* init each data */
  490. priv->msg = &msgs[i];
  491. priv->pos = 0;
  492. priv->flags = 0;
  493. if (priv->msg == &msgs[num - 1])
  494. rcar_i2c_flags_set(priv, ID_LAST_MSG);
  495. /* start send/recv */
  496. if (rcar_i2c_is_recv(priv))
  497. ret = rcar_i2c_recv(priv);
  498. else
  499. ret = rcar_i2c_send(priv);
  500. spin_unlock_irqrestore(&priv->lock, flags);
  501. /*-------------- spin unlock -----------------*/
  502. if (ret < 0)
  503. break;
  504. /*
  505. * wait result
  506. */
  507. timeout = wait_event_timeout(priv->wait,
  508. rcar_i2c_flags_has(priv, ID_DONE),
  509. 5 * HZ);
  510. if (!timeout) {
  511. ret = -ETIMEDOUT;
  512. break;
  513. }
  514. /*
  515. * error handling
  516. */
  517. if (rcar_i2c_flags_has(priv, ID_NACK)) {
  518. ret = -EREMOTEIO;
  519. break;
  520. }
  521. if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
  522. ret = -EAGAIN;
  523. break;
  524. }
  525. if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
  526. ret = -EIO;
  527. break;
  528. }
  529. ret = i + 1; /* The number of transfer */
  530. }
  531. pm_runtime_put(dev);
  532. if (ret < 0)
  533. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  534. return ret;
  535. }
  536. static u32 rcar_i2c_func(struct i2c_adapter *adap)
  537. {
  538. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  539. }
  540. static const struct i2c_algorithm rcar_i2c_algo = {
  541. .master_xfer = rcar_i2c_master_xfer,
  542. .functionality = rcar_i2c_func,
  543. };
  544. static int rcar_i2c_probe(struct platform_device *pdev)
  545. {
  546. struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
  547. struct rcar_i2c_priv *priv;
  548. struct i2c_adapter *adap;
  549. struct resource *res;
  550. struct device *dev = &pdev->dev;
  551. u32 bus_speed;
  552. int ret;
  553. priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
  554. if (!priv) {
  555. dev_err(dev, "no mem for private data\n");
  556. return -ENOMEM;
  557. }
  558. bus_speed = 100000; /* default 100 kHz */
  559. if (pdata && pdata->bus_speed)
  560. bus_speed = pdata->bus_speed;
  561. priv->devtype = platform_get_device_id(pdev)->driver_data;
  562. ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
  563. if (ret < 0)
  564. return ret;
  565. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  566. priv->io = devm_ioremap_resource(dev, res);
  567. if (IS_ERR(priv->io))
  568. return PTR_ERR(priv->io);
  569. priv->irq = platform_get_irq(pdev, 0);
  570. init_waitqueue_head(&priv->wait);
  571. spin_lock_init(&priv->lock);
  572. adap = &priv->adap;
  573. adap->nr = pdev->id;
  574. adap->algo = &rcar_i2c_algo;
  575. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  576. adap->retries = 3;
  577. adap->dev.parent = dev;
  578. i2c_set_adapdata(adap, priv);
  579. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  580. ret = devm_request_irq(dev, priv->irq, rcar_i2c_irq, 0,
  581. dev_name(dev), priv);
  582. if (ret < 0) {
  583. dev_err(dev, "cannot get irq %d\n", priv->irq);
  584. return ret;
  585. }
  586. ret = i2c_add_numbered_adapter(adap);
  587. if (ret < 0) {
  588. dev_err(dev, "reg adap failed: %d\n", ret);
  589. return ret;
  590. }
  591. pm_runtime_enable(dev);
  592. platform_set_drvdata(pdev, priv);
  593. dev_info(dev, "probed\n");
  594. return 0;
  595. }
  596. static int rcar_i2c_remove(struct platform_device *pdev)
  597. {
  598. struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
  599. struct device *dev = &pdev->dev;
  600. i2c_del_adapter(&priv->adap);
  601. pm_runtime_disable(dev);
  602. return 0;
  603. }
  604. static struct platform_device_id rcar_i2c_id_table[] = {
  605. { "i2c-rcar", I2C_RCAR_H1 },
  606. { "i2c-rcar_h1", I2C_RCAR_H1 },
  607. { "i2c-rcar_h2", I2C_RCAR_H2 },
  608. {},
  609. };
  610. MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
  611. static struct platform_driver rcar_i2c_driver = {
  612. .driver = {
  613. .name = "i2c-rcar",
  614. .owner = THIS_MODULE,
  615. },
  616. .probe = rcar_i2c_probe,
  617. .remove = rcar_i2c_remove,
  618. .id_table = rcar_i2c_id_table,
  619. };
  620. module_platform_driver(rcar_i2c_driver);
  621. MODULE_LICENSE("GPL");
  622. MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
  623. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");