i2c-rcar.c 17 KB

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