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/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/of_device.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/pm_runtime.h>
  39. #include <linux/slab.h>
  40. #include <linux/spinlock.h>
  41. /* register offsets */
  42. #define ICSCR 0x00 /* slave ctrl */
  43. #define ICMCR 0x04 /* master ctrl */
  44. #define ICSSR 0x08 /* slave status */
  45. #define ICMSR 0x0C /* master status */
  46. #define ICSIER 0x10 /* slave irq enable */
  47. #define ICMIER 0x14 /* master irq enable */
  48. #define ICCCR 0x18 /* clock dividers */
  49. #define ICSAR 0x1C /* slave address */
  50. #define ICMAR 0x20 /* master address */
  51. #define ICRXTX 0x24 /* data port */
  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. /* ICMSR */
  62. #define MNR (1 << 6) /* nack received */
  63. #define MAL (1 << 5) /* arbitration lost */
  64. #define MST (1 << 4) /* sent a stop */
  65. #define MDE (1 << 3)
  66. #define MDT (1 << 2)
  67. #define MDR (1 << 1)
  68. #define MAT (1 << 0) /* slave addr xfer done */
  69. /* ICMIE */
  70. #define MNRE (1 << 6) /* nack irq en */
  71. #define MALE (1 << 5) /* arblos irq en */
  72. #define MSTE (1 << 4) /* stop irq en */
  73. #define MDEE (1 << 3)
  74. #define MDTE (1 << 2)
  75. #define MDRE (1 << 1)
  76. #define MATE (1 << 0) /* address sent irq en */
  77. enum {
  78. RCAR_BUS_PHASE_ADDR,
  79. RCAR_BUS_PHASE_DATA,
  80. RCAR_BUS_PHASE_STOP,
  81. };
  82. enum {
  83. RCAR_IRQ_CLOSE,
  84. RCAR_IRQ_OPEN_FOR_SEND,
  85. RCAR_IRQ_OPEN_FOR_RECV,
  86. RCAR_IRQ_OPEN_FOR_STOP,
  87. };
  88. /*
  89. * flags
  90. */
  91. #define ID_LAST_MSG (1 << 0)
  92. #define ID_IOERROR (1 << 1)
  93. #define ID_DONE (1 << 2)
  94. #define ID_ARBLOST (1 << 3)
  95. #define ID_NACK (1 << 4)
  96. enum rcar_i2c_type {
  97. I2C_RCAR_H1,
  98. I2C_RCAR_H2,
  99. };
  100. struct rcar_i2c_priv {
  101. void __iomem *io;
  102. struct i2c_adapter adap;
  103. struct i2c_msg *msg;
  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. struct clk *clkp = clk_get(NULL, "peripheral_clk");
  204. u32 scgd, cdf;
  205. u32 round, ick;
  206. u32 scl;
  207. u32 cdf_width;
  208. unsigned long rate;
  209. if (!clkp) {
  210. dev_err(dev, "there is no peripheral_clk\n");
  211. return -EIO;
  212. }
  213. switch (priv->devtype) {
  214. case I2C_RCAR_H1:
  215. cdf_width = 2;
  216. break;
  217. case I2C_RCAR_H2:
  218. cdf_width = 3;
  219. break;
  220. default:
  221. dev_err(dev, "device type error\n");
  222. return -EIO;
  223. }
  224. /*
  225. * calculate SCL clock
  226. * see
  227. * ICCCR
  228. *
  229. * ick = clkp / (1 + CDF)
  230. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  231. *
  232. * ick : I2C internal clock < 20 MHz
  233. * ticf : I2C SCL falling time = 35 ns here
  234. * tr : I2C SCL rising time = 200 ns here
  235. * intd : LSI internal delay = 50 ns here
  236. * clkp : peripheral_clk
  237. * F[] : integer up-valuation
  238. */
  239. rate = clk_get_rate(clkp);
  240. cdf = rate / 20000000;
  241. if (cdf >= 1 << cdf_width) {
  242. dev_err(dev, "Input clock %lu too high\n", rate);
  243. return -EIO;
  244. }
  245. ick = rate / (cdf + 1);
  246. /*
  247. * it is impossible to calculate large scale
  248. * number on u32. separate it
  249. *
  250. * F[(ticf + tr + intd) * ick]
  251. * = F[(35 + 200 + 50)ns * ick]
  252. * = F[285 * ick / 1000000000]
  253. * = F[(ick / 1000000) * 285 / 1000]
  254. */
  255. round = (ick + 500000) / 1000000 * 285;
  256. round = (round + 500) / 1000;
  257. /*
  258. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  259. *
  260. * Calculation result (= SCL) should be less than
  261. * bus_speed for hardware safety
  262. *
  263. * We could use something along the lines of
  264. * div = ick / (bus_speed + 1) + 1;
  265. * scgd = (div - 20 - round + 7) / 8;
  266. * scl = ick / (20 + (scgd * 8) + round);
  267. * (not fully verified) but that would get pretty involved
  268. */
  269. for (scgd = 0; scgd < 0x40; scgd++) {
  270. scl = ick / (20 + (scgd * 8) + round);
  271. if (scl <= bus_speed)
  272. goto scgd_find;
  273. }
  274. dev_err(dev, "it is impossible to calculate best SCL\n");
  275. return -EIO;
  276. scgd_find:
  277. dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
  278. scl, bus_speed, clk_get_rate(clkp), round, cdf, scgd);
  279. /*
  280. * keep icccr value
  281. */
  282. priv->icccr = scgd << cdf_width | cdf;
  283. return 0;
  284. }
  285. static void rcar_i2c_clock_start(struct rcar_i2c_priv *priv)
  286. {
  287. rcar_i2c_write(priv, ICCCR, priv->icccr);
  288. }
  289. /*
  290. * status functions
  291. */
  292. static u32 rcar_i2c_status_get(struct rcar_i2c_priv *priv)
  293. {
  294. return rcar_i2c_read(priv, ICMSR);
  295. }
  296. #define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
  297. static void rcar_i2c_status_bit_clear(struct rcar_i2c_priv *priv, u32 bit)
  298. {
  299. rcar_i2c_write(priv, ICMSR, ~bit);
  300. }
  301. /*
  302. * recv/send functions
  303. */
  304. static int rcar_i2c_recv(struct rcar_i2c_priv *priv)
  305. {
  306. rcar_i2c_set_addr(priv, 1);
  307. rcar_i2c_status_clear(priv);
  308. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
  309. rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_RECV);
  310. return 0;
  311. }
  312. static int rcar_i2c_send(struct rcar_i2c_priv *priv)
  313. {
  314. int ret;
  315. /*
  316. * It should check bus status when send case
  317. */
  318. ret = rcar_i2c_bus_barrier(priv);
  319. if (ret < 0)
  320. return ret;
  321. rcar_i2c_set_addr(priv, 0);
  322. rcar_i2c_status_clear(priv);
  323. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
  324. rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_SEND);
  325. return 0;
  326. }
  327. #define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
  328. #define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
  329. /*
  330. * interrupt functions
  331. */
  332. static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
  333. {
  334. struct i2c_msg *msg = priv->msg;
  335. /*
  336. * FIXME
  337. * sometimes, unknown interrupt happened.
  338. * Do nothing
  339. */
  340. if (!(msr & MDE))
  341. return 0;
  342. /*
  343. * If address transfer phase finished,
  344. * goto data phase.
  345. */
  346. if (msr & MAT)
  347. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
  348. if (priv->pos < msg->len) {
  349. /*
  350. * Prepare next data to ICRXTX register.
  351. * This data will go to _SHIFT_ register.
  352. *
  353. * *
  354. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  355. */
  356. rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
  357. priv->pos++;
  358. } else {
  359. /*
  360. * The last data was pushed to ICRXTX on _PREV_ empty irq.
  361. * It is on _SHIFT_ register, and will sent to I2C bus.
  362. *
  363. * *
  364. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  365. */
  366. if (priv->flags & ID_LAST_MSG)
  367. /*
  368. * If current msg is the _LAST_ msg,
  369. * prepare stop condition here.
  370. * ID_DONE will be set on STOP irq.
  371. */
  372. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
  373. else
  374. /*
  375. * If current msg is _NOT_ last msg,
  376. * it doesn't call stop phase.
  377. * thus, there is no STOP irq.
  378. * return ID_DONE here.
  379. */
  380. return ID_DONE;
  381. }
  382. rcar_i2c_send_restart(priv);
  383. return 0;
  384. }
  385. static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
  386. {
  387. struct i2c_msg *msg = priv->msg;
  388. /*
  389. * FIXME
  390. * sometimes, unknown interrupt happened.
  391. * Do nothing
  392. */
  393. if (!(msr & MDR))
  394. return 0;
  395. if (msr & MAT) {
  396. /*
  397. * Address transfer phase finished,
  398. * but, there is no data at this point.
  399. * Do nothing.
  400. */
  401. } else if (priv->pos < msg->len) {
  402. /*
  403. * get received data
  404. */
  405. msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
  406. priv->pos++;
  407. }
  408. /*
  409. * If next received data is the _LAST_,
  410. * go to STOP phase,
  411. * otherwise, go to DATA phase.
  412. */
  413. if (priv->pos + 1 >= msg->len)
  414. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
  415. else
  416. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA);
  417. rcar_i2c_recv_restart(priv);
  418. return 0;
  419. }
  420. static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
  421. {
  422. struct rcar_i2c_priv *priv = ptr;
  423. struct device *dev = rcar_i2c_priv_to_dev(priv);
  424. u32 msr;
  425. /*-------------- spin lock -----------------*/
  426. spin_lock(&priv->lock);
  427. msr = rcar_i2c_status_get(priv);
  428. /*
  429. * Arbitration lost
  430. */
  431. if (msr & MAL) {
  432. /*
  433. * CAUTION
  434. *
  435. * When arbitration lost, device become _slave_ mode.
  436. */
  437. dev_dbg(dev, "Arbitration Lost\n");
  438. rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
  439. goto out;
  440. }
  441. /*
  442. * Stop
  443. */
  444. if (msr & MST) {
  445. dev_dbg(dev, "Stop\n");
  446. rcar_i2c_flags_set(priv, ID_DONE);
  447. goto out;
  448. }
  449. /*
  450. * Nack
  451. */
  452. if (msr & MNR) {
  453. dev_dbg(dev, "Nack\n");
  454. /* go to stop phase */
  455. rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP);
  456. rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_STOP);
  457. rcar_i2c_flags_set(priv, ID_NACK);
  458. goto out;
  459. }
  460. /*
  461. * recv/send
  462. */
  463. if (rcar_i2c_is_recv(priv))
  464. rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
  465. else
  466. rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
  467. out:
  468. if (rcar_i2c_flags_has(priv, ID_DONE)) {
  469. rcar_i2c_irq_mask(priv, RCAR_IRQ_CLOSE);
  470. rcar_i2c_status_clear(priv);
  471. wake_up(&priv->wait);
  472. }
  473. spin_unlock(&priv->lock);
  474. /*-------------- spin unlock -----------------*/
  475. return IRQ_HANDLED;
  476. }
  477. static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
  478. struct i2c_msg *msgs,
  479. int num)
  480. {
  481. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  482. struct device *dev = rcar_i2c_priv_to_dev(priv);
  483. unsigned long flags;
  484. int i, ret, timeout;
  485. pm_runtime_get_sync(dev);
  486. /*-------------- spin lock -----------------*/
  487. spin_lock_irqsave(&priv->lock, flags);
  488. rcar_i2c_init(priv);
  489. rcar_i2c_clock_start(priv);
  490. spin_unlock_irqrestore(&priv->lock, flags);
  491. /*-------------- spin unlock -----------------*/
  492. ret = -EINVAL;
  493. for (i = 0; i < num; i++) {
  494. /*-------------- spin lock -----------------*/
  495. spin_lock_irqsave(&priv->lock, flags);
  496. /* init each data */
  497. priv->msg = &msgs[i];
  498. priv->pos = 0;
  499. priv->flags = 0;
  500. if (priv->msg == &msgs[num - 1])
  501. rcar_i2c_flags_set(priv, ID_LAST_MSG);
  502. /* start send/recv */
  503. if (rcar_i2c_is_recv(priv))
  504. ret = rcar_i2c_recv(priv);
  505. else
  506. ret = rcar_i2c_send(priv);
  507. spin_unlock_irqrestore(&priv->lock, flags);
  508. /*-------------- spin unlock -----------------*/
  509. if (ret < 0)
  510. break;
  511. /*
  512. * wait result
  513. */
  514. timeout = wait_event_timeout(priv->wait,
  515. rcar_i2c_flags_has(priv, ID_DONE),
  516. 5 * HZ);
  517. if (!timeout) {
  518. ret = -ETIMEDOUT;
  519. break;
  520. }
  521. /*
  522. * error handling
  523. */
  524. if (rcar_i2c_flags_has(priv, ID_NACK)) {
  525. ret = -EREMOTEIO;
  526. break;
  527. }
  528. if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
  529. ret = -EAGAIN;
  530. break;
  531. }
  532. if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
  533. ret = -EIO;
  534. break;
  535. }
  536. ret = i + 1; /* The number of transfer */
  537. }
  538. pm_runtime_put(dev);
  539. if (ret < 0)
  540. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  541. return ret;
  542. }
  543. static u32 rcar_i2c_func(struct i2c_adapter *adap)
  544. {
  545. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  546. }
  547. static const struct i2c_algorithm rcar_i2c_algo = {
  548. .master_xfer = rcar_i2c_master_xfer,
  549. .functionality = rcar_i2c_func,
  550. };
  551. static const struct of_device_id rcar_i2c_dt_ids[] = {
  552. { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_H1 },
  553. { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_H1 },
  554. { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_H1 },
  555. { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_H2 },
  556. {},
  557. };
  558. MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
  559. static int rcar_i2c_probe(struct platform_device *pdev)
  560. {
  561. struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
  562. struct rcar_i2c_priv *priv;
  563. struct i2c_adapter *adap;
  564. struct resource *res;
  565. struct device *dev = &pdev->dev;
  566. u32 bus_speed;
  567. int ret;
  568. priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
  569. if (!priv) {
  570. dev_err(dev, "no mem for private data\n");
  571. return -ENOMEM;
  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_H1 },
  627. { "i2c-rcar_h1", I2C_RCAR_H1 },
  628. { "i2c-rcar_h2", I2C_RCAR_H2 },
  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>");