i2c-rcar.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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/i2c/i2c-rcar.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/of_device.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/slab.h>
  37. /* register offsets */
  38. #define ICSCR 0x00 /* slave ctrl */
  39. #define ICMCR 0x04 /* master ctrl */
  40. #define ICSSR 0x08 /* slave status */
  41. #define ICMSR 0x0C /* master status */
  42. #define ICSIER 0x10 /* slave irq enable */
  43. #define ICMIER 0x14 /* master irq enable */
  44. #define ICCCR 0x18 /* clock dividers */
  45. #define ICSAR 0x1C /* slave address */
  46. #define ICMAR 0x20 /* master address */
  47. #define ICRXTX 0x24 /* data port */
  48. /* ICMCR */
  49. #define MDBS (1 << 7) /* non-fifo mode switch */
  50. #define FSCL (1 << 6) /* override SCL pin */
  51. #define FSDA (1 << 5) /* override SDA pin */
  52. #define OBPC (1 << 4) /* override pins */
  53. #define MIE (1 << 3) /* master if enable */
  54. #define TSBE (1 << 2)
  55. #define FSB (1 << 1) /* force stop bit */
  56. #define ESG (1 << 0) /* en startbit gen */
  57. /* ICMSR (also for ICMIE) */
  58. #define MNR (1 << 6) /* nack received */
  59. #define MAL (1 << 5) /* arbitration lost */
  60. #define MST (1 << 4) /* sent a stop */
  61. #define MDE (1 << 3)
  62. #define MDT (1 << 2)
  63. #define MDR (1 << 1)
  64. #define MAT (1 << 0) /* slave addr xfer done */
  65. #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
  66. #define RCAR_BUS_PHASE_DATA (MDBS | MIE)
  67. #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
  68. #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
  69. #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
  70. #define RCAR_IRQ_STOP (MST)
  71. #define RCAR_IRQ_ACK_SEND (~(MAT | MDE))
  72. #define RCAR_IRQ_ACK_RECV (~(MAT | MDR))
  73. #define ID_LAST_MSG (1 << 0)
  74. #define ID_IOERROR (1 << 1)
  75. #define ID_DONE (1 << 2)
  76. #define ID_ARBLOST (1 << 3)
  77. #define ID_NACK (1 << 4)
  78. enum rcar_i2c_type {
  79. I2C_RCAR_GEN1,
  80. I2C_RCAR_GEN2,
  81. };
  82. struct rcar_i2c_priv {
  83. void __iomem *io;
  84. struct i2c_adapter adap;
  85. struct i2c_msg *msg;
  86. struct clk *clk;
  87. wait_queue_head_t wait;
  88. int pos;
  89. u32 icccr;
  90. u32 flags;
  91. enum rcar_i2c_type devtype;
  92. };
  93. #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
  94. #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
  95. #define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
  96. #define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
  97. #define LOOP_TIMEOUT 1024
  98. static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
  99. {
  100. writel(val, priv->io + reg);
  101. }
  102. static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
  103. {
  104. return readl(priv->io + reg);
  105. }
  106. static void rcar_i2c_init(struct rcar_i2c_priv *priv)
  107. {
  108. /*
  109. * reset slave mode.
  110. * slave mode is not used on this driver
  111. */
  112. rcar_i2c_write(priv, ICSIER, 0);
  113. rcar_i2c_write(priv, ICSAR, 0);
  114. rcar_i2c_write(priv, ICSCR, 0);
  115. rcar_i2c_write(priv, ICSSR, 0);
  116. /* reset master mode */
  117. rcar_i2c_write(priv, ICMIER, 0);
  118. rcar_i2c_write(priv, ICMCR, 0);
  119. rcar_i2c_write(priv, ICMSR, 0);
  120. rcar_i2c_write(priv, ICMAR, 0);
  121. }
  122. static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
  123. {
  124. int i;
  125. for (i = 0; i < LOOP_TIMEOUT; i++) {
  126. /* make sure that bus is not busy */
  127. if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
  128. return 0;
  129. udelay(1);
  130. }
  131. return -EBUSY;
  132. }
  133. static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
  134. u32 bus_speed,
  135. struct device *dev)
  136. {
  137. u32 scgd, cdf;
  138. u32 round, ick;
  139. u32 scl;
  140. u32 cdf_width;
  141. unsigned long rate;
  142. switch (priv->devtype) {
  143. case I2C_RCAR_GEN1:
  144. cdf_width = 2;
  145. break;
  146. case I2C_RCAR_GEN2:
  147. cdf_width = 3;
  148. break;
  149. default:
  150. dev_err(dev, "device type error\n");
  151. return -EIO;
  152. }
  153. /*
  154. * calculate SCL clock
  155. * see
  156. * ICCCR
  157. *
  158. * ick = clkp / (1 + CDF)
  159. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  160. *
  161. * ick : I2C internal clock < 20 MHz
  162. * ticf : I2C SCL falling time = 35 ns here
  163. * tr : I2C SCL rising time = 200 ns here
  164. * intd : LSI internal delay = 50 ns here
  165. * clkp : peripheral_clk
  166. * F[] : integer up-valuation
  167. */
  168. rate = clk_get_rate(priv->clk);
  169. cdf = rate / 20000000;
  170. if (cdf >= 1 << cdf_width) {
  171. dev_err(dev, "Input clock %lu too high\n", rate);
  172. return -EIO;
  173. }
  174. ick = rate / (cdf + 1);
  175. /*
  176. * it is impossible to calculate large scale
  177. * number on u32. separate it
  178. *
  179. * F[(ticf + tr + intd) * ick]
  180. * = F[(35 + 200 + 50)ns * ick]
  181. * = F[285 * ick / 1000000000]
  182. * = F[(ick / 1000000) * 285 / 1000]
  183. */
  184. round = (ick + 500000) / 1000000 * 285;
  185. round = (round + 500) / 1000;
  186. /*
  187. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  188. *
  189. * Calculation result (= SCL) should be less than
  190. * bus_speed for hardware safety
  191. *
  192. * We could use something along the lines of
  193. * div = ick / (bus_speed + 1) + 1;
  194. * scgd = (div - 20 - round + 7) / 8;
  195. * scl = ick / (20 + (scgd * 8) + round);
  196. * (not fully verified) but that would get pretty involved
  197. */
  198. for (scgd = 0; scgd < 0x40; scgd++) {
  199. scl = ick / (20 + (scgd * 8) + round);
  200. if (scl <= bus_speed)
  201. goto scgd_find;
  202. }
  203. dev_err(dev, "it is impossible to calculate best SCL\n");
  204. return -EIO;
  205. scgd_find:
  206. dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
  207. scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
  208. /*
  209. * keep icccr value
  210. */
  211. priv->icccr = scgd << cdf_width | cdf;
  212. return 0;
  213. }
  214. static int rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
  215. {
  216. int read = !!rcar_i2c_is_recv(priv);
  217. rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
  218. rcar_i2c_write(priv, ICMSR, 0);
  219. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
  220. rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
  221. return 0;
  222. }
  223. /*
  224. * interrupt functions
  225. */
  226. static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
  227. {
  228. struct i2c_msg *msg = priv->msg;
  229. /*
  230. * FIXME
  231. * sometimes, unknown interrupt happened.
  232. * Do nothing
  233. */
  234. if (!(msr & MDE))
  235. return 0;
  236. /*
  237. * If address transfer phase finished,
  238. * goto data phase.
  239. */
  240. if (msr & MAT)
  241. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
  242. if (priv->pos < msg->len) {
  243. /*
  244. * Prepare next data to ICRXTX register.
  245. * This data will go to _SHIFT_ register.
  246. *
  247. * *
  248. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  249. */
  250. rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
  251. priv->pos++;
  252. } else {
  253. /*
  254. * The last data was pushed to ICRXTX on _PREV_ empty irq.
  255. * It is on _SHIFT_ register, and will sent to I2C bus.
  256. *
  257. * *
  258. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  259. */
  260. if (priv->flags & ID_LAST_MSG)
  261. /*
  262. * If current msg is the _LAST_ msg,
  263. * prepare stop condition here.
  264. * ID_DONE will be set on STOP irq.
  265. */
  266. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  267. else
  268. /*
  269. * If current msg is _NOT_ last msg,
  270. * it doesn't call stop phase.
  271. * thus, there is no STOP irq.
  272. * return ID_DONE here.
  273. */
  274. return ID_DONE;
  275. }
  276. rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
  277. return 0;
  278. }
  279. static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
  280. {
  281. struct i2c_msg *msg = priv->msg;
  282. /*
  283. * FIXME
  284. * sometimes, unknown interrupt happened.
  285. * Do nothing
  286. */
  287. if (!(msr & MDR))
  288. return 0;
  289. if (msr & MAT) {
  290. /*
  291. * Address transfer phase finished,
  292. * but, there is no data at this point.
  293. * Do nothing.
  294. */
  295. } else if (priv->pos < msg->len) {
  296. /*
  297. * get received data
  298. */
  299. msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
  300. priv->pos++;
  301. }
  302. /*
  303. * If next received data is the _LAST_,
  304. * go to STOP phase,
  305. * otherwise, go to DATA phase.
  306. */
  307. if (priv->pos + 1 >= msg->len)
  308. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  309. else
  310. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
  311. rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
  312. return 0;
  313. }
  314. static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
  315. {
  316. struct rcar_i2c_priv *priv = ptr;
  317. u32 msr;
  318. msr = rcar_i2c_read(priv, ICMSR);
  319. /* Arbitration lost */
  320. if (msr & MAL) {
  321. rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
  322. goto out;
  323. }
  324. /* Stop */
  325. if (msr & MST) {
  326. rcar_i2c_flags_set(priv, ID_DONE);
  327. goto out;
  328. }
  329. /* Nack */
  330. if (msr & MNR) {
  331. /* go to stop phase */
  332. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  333. rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
  334. rcar_i2c_flags_set(priv, ID_NACK);
  335. goto out;
  336. }
  337. if (rcar_i2c_is_recv(priv))
  338. rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
  339. else
  340. rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));
  341. out:
  342. if (rcar_i2c_flags_has(priv, ID_DONE)) {
  343. rcar_i2c_write(priv, ICMIER, 0);
  344. rcar_i2c_write(priv, ICMSR, 0);
  345. wake_up(&priv->wait);
  346. }
  347. return IRQ_HANDLED;
  348. }
  349. static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
  350. struct i2c_msg *msgs,
  351. int num)
  352. {
  353. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  354. struct device *dev = rcar_i2c_priv_to_dev(priv);
  355. int i, ret, timeout;
  356. pm_runtime_get_sync(dev);
  357. rcar_i2c_init(priv);
  358. /* start clock */
  359. rcar_i2c_write(priv, ICCCR, priv->icccr);
  360. ret = rcar_i2c_bus_barrier(priv);
  361. if (ret < 0)
  362. goto out;
  363. for (i = 0; i < num; i++) {
  364. /* This HW can't send STOP after address phase */
  365. if (msgs[i].len == 0) {
  366. ret = -EOPNOTSUPP;
  367. break;
  368. }
  369. /* init each data */
  370. priv->msg = &msgs[i];
  371. priv->pos = 0;
  372. priv->flags = 0;
  373. if (priv->msg == &msgs[num - 1])
  374. rcar_i2c_flags_set(priv, ID_LAST_MSG);
  375. ret = rcar_i2c_prepare_msg(priv);
  376. if (ret < 0)
  377. break;
  378. timeout = wait_event_timeout(priv->wait,
  379. rcar_i2c_flags_has(priv, ID_DONE),
  380. 5 * HZ);
  381. if (!timeout) {
  382. ret = -ETIMEDOUT;
  383. break;
  384. }
  385. if (rcar_i2c_flags_has(priv, ID_NACK)) {
  386. ret = -ENXIO;
  387. break;
  388. }
  389. if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
  390. ret = -EAGAIN;
  391. break;
  392. }
  393. if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
  394. ret = -EIO;
  395. break;
  396. }
  397. ret = i + 1; /* The number of transfer */
  398. }
  399. out:
  400. pm_runtime_put(dev);
  401. if (ret < 0 && ret != -ENXIO)
  402. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  403. return ret;
  404. }
  405. static u32 rcar_i2c_func(struct i2c_adapter *adap)
  406. {
  407. /* This HW can't do SMBUS_QUICK and NOSTART */
  408. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  409. }
  410. static const struct i2c_algorithm rcar_i2c_algo = {
  411. .master_xfer = rcar_i2c_master_xfer,
  412. .functionality = rcar_i2c_func,
  413. };
  414. static const struct of_device_id rcar_i2c_dt_ids[] = {
  415. { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
  416. { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
  417. { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
  418. { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
  419. { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
  420. { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
  421. { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
  422. { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
  423. {},
  424. };
  425. MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
  426. static int rcar_i2c_probe(struct platform_device *pdev)
  427. {
  428. struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
  429. struct rcar_i2c_priv *priv;
  430. struct i2c_adapter *adap;
  431. struct resource *res;
  432. struct device *dev = &pdev->dev;
  433. u32 bus_speed;
  434. int irq, ret;
  435. priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
  436. if (!priv)
  437. return -ENOMEM;
  438. priv->clk = devm_clk_get(dev, NULL);
  439. if (IS_ERR(priv->clk)) {
  440. dev_err(dev, "cannot get clock\n");
  441. return PTR_ERR(priv->clk);
  442. }
  443. bus_speed = 100000; /* default 100 kHz */
  444. ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
  445. if (ret < 0 && pdata && pdata->bus_speed)
  446. bus_speed = pdata->bus_speed;
  447. if (pdev->dev.of_node)
  448. priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
  449. dev)->data;
  450. else
  451. priv->devtype = platform_get_device_id(pdev)->driver_data;
  452. ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
  453. if (ret < 0)
  454. return ret;
  455. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  456. priv->io = devm_ioremap_resource(dev, res);
  457. if (IS_ERR(priv->io))
  458. return PTR_ERR(priv->io);
  459. irq = platform_get_irq(pdev, 0);
  460. init_waitqueue_head(&priv->wait);
  461. adap = &priv->adap;
  462. adap->nr = pdev->id;
  463. adap->algo = &rcar_i2c_algo;
  464. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD | I2C_CLASS_DEPRECATED;
  465. adap->retries = 3;
  466. adap->dev.parent = dev;
  467. adap->dev.of_node = dev->of_node;
  468. i2c_set_adapdata(adap, priv);
  469. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  470. ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
  471. dev_name(dev), priv);
  472. if (ret < 0) {
  473. dev_err(dev, "cannot get irq %d\n", irq);
  474. return ret;
  475. }
  476. ret = i2c_add_numbered_adapter(adap);
  477. if (ret < 0) {
  478. dev_err(dev, "reg adap failed: %d\n", ret);
  479. return ret;
  480. }
  481. pm_runtime_enable(dev);
  482. platform_set_drvdata(pdev, priv);
  483. dev_info(dev, "probed\n");
  484. return 0;
  485. }
  486. static int rcar_i2c_remove(struct platform_device *pdev)
  487. {
  488. struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
  489. struct device *dev = &pdev->dev;
  490. i2c_del_adapter(&priv->adap);
  491. pm_runtime_disable(dev);
  492. return 0;
  493. }
  494. static struct platform_device_id rcar_i2c_id_table[] = {
  495. { "i2c-rcar", I2C_RCAR_GEN1 },
  496. { "i2c-rcar_gen1", I2C_RCAR_GEN1 },
  497. { "i2c-rcar_gen2", I2C_RCAR_GEN2 },
  498. {},
  499. };
  500. MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
  501. static struct platform_driver rcar_i2c_driver = {
  502. .driver = {
  503. .name = "i2c-rcar",
  504. .owner = THIS_MODULE,
  505. .of_match_table = rcar_i2c_dt_ids,
  506. },
  507. .probe = rcar_i2c_probe,
  508. .remove = rcar_i2c_remove,
  509. .id_table = rcar_i2c_id_table,
  510. };
  511. module_platform_driver(rcar_i2c_driver);
  512. MODULE_LICENSE("GPL v2");
  513. MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
  514. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");