i2c-uniphier-f.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #define UNIPHIER_FI2C_CR 0x00 /* control register */
  21. #define UNIPHIER_FI2C_CR_MST BIT(3) /* master mode */
  22. #define UNIPHIER_FI2C_CR_STA BIT(2) /* start condition */
  23. #define UNIPHIER_FI2C_CR_STO BIT(1) /* stop condition */
  24. #define UNIPHIER_FI2C_CR_NACK BIT(0) /* do not return ACK */
  25. #define UNIPHIER_FI2C_DTTX 0x04 /* TX FIFO */
  26. #define UNIPHIER_FI2C_DTTX_CMD BIT(8) /* send command (slave addr) */
  27. #define UNIPHIER_FI2C_DTTX_RD BIT(0) /* read transaction */
  28. #define UNIPHIER_FI2C_DTRX 0x04 /* RX FIFO */
  29. #define UNIPHIER_FI2C_SLAD 0x0c /* slave address */
  30. #define UNIPHIER_FI2C_CYC 0x10 /* clock cycle control */
  31. #define UNIPHIER_FI2C_LCTL 0x14 /* clock low period control */
  32. #define UNIPHIER_FI2C_SSUT 0x18 /* restart/stop setup time control */
  33. #define UNIPHIER_FI2C_DSUT 0x1c /* data setup time control */
  34. #define UNIPHIER_FI2C_INT 0x20 /* interrupt status */
  35. #define UNIPHIER_FI2C_IE 0x24 /* interrupt enable */
  36. #define UNIPHIER_FI2C_IC 0x28 /* interrupt clear */
  37. #define UNIPHIER_FI2C_INT_TE BIT(9) /* TX FIFO empty */
  38. #define UNIPHIER_FI2C_INT_RF BIT(8) /* RX FIFO full */
  39. #define UNIPHIER_FI2C_INT_TC BIT(7) /* send complete (STOP) */
  40. #define UNIPHIER_FI2C_INT_RC BIT(6) /* receive complete (STOP) */
  41. #define UNIPHIER_FI2C_INT_TB BIT(5) /* sent specified bytes */
  42. #define UNIPHIER_FI2C_INT_RB BIT(4) /* received specified bytes */
  43. #define UNIPHIER_FI2C_INT_NA BIT(2) /* no ACK */
  44. #define UNIPHIER_FI2C_INT_AL BIT(1) /* arbitration lost */
  45. #define UNIPHIER_FI2C_SR 0x2c /* status register */
  46. #define UNIPHIER_FI2C_SR_DB BIT(12) /* device busy */
  47. #define UNIPHIER_FI2C_SR_STS BIT(11) /* stop condition detected */
  48. #define UNIPHIER_FI2C_SR_BB BIT(8) /* bus busy */
  49. #define UNIPHIER_FI2C_SR_RFF BIT(3) /* RX FIFO full */
  50. #define UNIPHIER_FI2C_SR_RNE BIT(2) /* RX FIFO not empty */
  51. #define UNIPHIER_FI2C_SR_TNF BIT(1) /* TX FIFO not full */
  52. #define UNIPHIER_FI2C_SR_TFE BIT(0) /* TX FIFO empty */
  53. #define UNIPHIER_FI2C_RST 0x34 /* reset control */
  54. #define UNIPHIER_FI2C_RST_TBRST BIT(2) /* clear TX FIFO */
  55. #define UNIPHIER_FI2C_RST_RBRST BIT(1) /* clear RX FIFO */
  56. #define UNIPHIER_FI2C_RST_RST BIT(0) /* forcible bus reset */
  57. #define UNIPHIER_FI2C_BM 0x38 /* bus monitor */
  58. #define UNIPHIER_FI2C_BM_SDAO BIT(3) /* output for SDA line */
  59. #define UNIPHIER_FI2C_BM_SDAS BIT(2) /* readback of SDA line */
  60. #define UNIPHIER_FI2C_BM_SCLO BIT(1) /* output for SCL line */
  61. #define UNIPHIER_FI2C_BM_SCLS BIT(0) /* readback of SCL line */
  62. #define UNIPHIER_FI2C_NOISE 0x3c /* noise filter control */
  63. #define UNIPHIER_FI2C_TBC 0x40 /* TX byte count setting */
  64. #define UNIPHIER_FI2C_RBC 0x44 /* RX byte count setting */
  65. #define UNIPHIER_FI2C_TBCM 0x48 /* TX byte count monitor */
  66. #define UNIPHIER_FI2C_RBCM 0x4c /* RX byte count monitor */
  67. #define UNIPHIER_FI2C_BRST 0x50 /* bus reset */
  68. #define UNIPHIER_FI2C_BRST_FOEN BIT(1) /* normal operation */
  69. #define UNIPHIER_FI2C_BRST_RSCL BIT(0) /* release SCL */
  70. #define UNIPHIER_FI2C_INT_FAULTS \
  71. (UNIPHIER_FI2C_INT_NA | UNIPHIER_FI2C_INT_AL)
  72. #define UNIPHIER_FI2C_INT_STOP \
  73. (UNIPHIER_FI2C_INT_TC | UNIPHIER_FI2C_INT_RC)
  74. #define UNIPHIER_FI2C_RD BIT(0)
  75. #define UNIPHIER_FI2C_STOP BIT(1)
  76. #define UNIPHIER_FI2C_MANUAL_NACK BIT(2)
  77. #define UNIPHIER_FI2C_BYTE_WISE BIT(3)
  78. #define UNIPHIER_FI2C_DEFER_STOP_COMP BIT(4)
  79. #define UNIPHIER_FI2C_DEFAULT_SPEED 100000
  80. #define UNIPHIER_FI2C_MAX_SPEED 400000
  81. #define UNIPHIER_FI2C_FIFO_SIZE 8
  82. struct uniphier_fi2c_priv {
  83. struct completion comp;
  84. struct i2c_adapter adap;
  85. void __iomem *membase;
  86. struct clk *clk;
  87. unsigned int len;
  88. u8 *buf;
  89. u32 enabled_irqs;
  90. int error;
  91. unsigned int flags;
  92. unsigned int busy_cnt;
  93. };
  94. static void uniphier_fi2c_fill_txfifo(struct uniphier_fi2c_priv *priv,
  95. bool first)
  96. {
  97. int fifo_space = UNIPHIER_FI2C_FIFO_SIZE;
  98. /*
  99. * TX-FIFO stores slave address in it for the first access.
  100. * Decrement the counter.
  101. */
  102. if (first)
  103. fifo_space--;
  104. while (priv->len) {
  105. if (fifo_space-- <= 0)
  106. break;
  107. dev_dbg(&priv->adap.dev, "write data: %02x\n", *priv->buf);
  108. writel(*priv->buf++, priv->membase + UNIPHIER_FI2C_DTTX);
  109. priv->len--;
  110. }
  111. }
  112. static void uniphier_fi2c_drain_rxfifo(struct uniphier_fi2c_priv *priv)
  113. {
  114. int fifo_left = priv->flags & UNIPHIER_FI2C_BYTE_WISE ?
  115. 1 : UNIPHIER_FI2C_FIFO_SIZE;
  116. while (priv->len) {
  117. if (fifo_left-- <= 0)
  118. break;
  119. *priv->buf++ = readl(priv->membase + UNIPHIER_FI2C_DTRX);
  120. dev_dbg(&priv->adap.dev, "read data: %02x\n", priv->buf[-1]);
  121. priv->len--;
  122. }
  123. }
  124. static void uniphier_fi2c_set_irqs(struct uniphier_fi2c_priv *priv)
  125. {
  126. writel(priv->enabled_irqs, priv->membase + UNIPHIER_FI2C_IE);
  127. }
  128. static void uniphier_fi2c_clear_irqs(struct uniphier_fi2c_priv *priv)
  129. {
  130. writel(-1, priv->membase + UNIPHIER_FI2C_IC);
  131. }
  132. static void uniphier_fi2c_stop(struct uniphier_fi2c_priv *priv)
  133. {
  134. dev_dbg(&priv->adap.dev, "stop condition\n");
  135. priv->enabled_irqs |= UNIPHIER_FI2C_INT_STOP;
  136. uniphier_fi2c_set_irqs(priv);
  137. writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STO,
  138. priv->membase + UNIPHIER_FI2C_CR);
  139. }
  140. static irqreturn_t uniphier_fi2c_interrupt(int irq, void *dev_id)
  141. {
  142. struct uniphier_fi2c_priv *priv = dev_id;
  143. u32 irq_status;
  144. irq_status = readl(priv->membase + UNIPHIER_FI2C_INT);
  145. dev_dbg(&priv->adap.dev,
  146. "interrupt: enabled_irqs=%04x, irq_status=%04x\n",
  147. priv->enabled_irqs, irq_status);
  148. if (irq_status & UNIPHIER_FI2C_INT_STOP)
  149. goto complete;
  150. if (unlikely(irq_status & UNIPHIER_FI2C_INT_AL)) {
  151. dev_dbg(&priv->adap.dev, "arbitration lost\n");
  152. priv->error = -EAGAIN;
  153. goto complete;
  154. }
  155. if (unlikely(irq_status & UNIPHIER_FI2C_INT_NA)) {
  156. dev_dbg(&priv->adap.dev, "could not get ACK\n");
  157. priv->error = -ENXIO;
  158. if (priv->flags & UNIPHIER_FI2C_RD) {
  159. /*
  160. * work around a hardware bug:
  161. * The receive-completed interrupt is never set even if
  162. * STOP condition is detected after the address phase
  163. * of read transaction fails to get ACK.
  164. * To avoid time-out error, we issue STOP here,
  165. * but do not wait for its completion.
  166. * It should be checked after exiting this handler.
  167. */
  168. uniphier_fi2c_stop(priv);
  169. priv->flags |= UNIPHIER_FI2C_DEFER_STOP_COMP;
  170. goto complete;
  171. }
  172. goto stop;
  173. }
  174. if (irq_status & UNIPHIER_FI2C_INT_TE) {
  175. if (!priv->len)
  176. goto data_done;
  177. uniphier_fi2c_fill_txfifo(priv, false);
  178. goto handled;
  179. }
  180. if (irq_status & (UNIPHIER_FI2C_INT_RF | UNIPHIER_FI2C_INT_RB)) {
  181. uniphier_fi2c_drain_rxfifo(priv);
  182. if (!priv->len)
  183. goto data_done;
  184. if (unlikely(priv->flags & UNIPHIER_FI2C_MANUAL_NACK)) {
  185. if (priv->len <= UNIPHIER_FI2C_FIFO_SIZE &&
  186. !(priv->flags & UNIPHIER_FI2C_BYTE_WISE)) {
  187. dev_dbg(&priv->adap.dev,
  188. "enable read byte count IRQ\n");
  189. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RB;
  190. uniphier_fi2c_set_irqs(priv);
  191. priv->flags |= UNIPHIER_FI2C_BYTE_WISE;
  192. }
  193. if (priv->len <= 1) {
  194. dev_dbg(&priv->adap.dev, "set NACK\n");
  195. writel(UNIPHIER_FI2C_CR_MST |
  196. UNIPHIER_FI2C_CR_NACK,
  197. priv->membase + UNIPHIER_FI2C_CR);
  198. }
  199. }
  200. goto handled;
  201. }
  202. return IRQ_NONE;
  203. data_done:
  204. if (priv->flags & UNIPHIER_FI2C_STOP) {
  205. stop:
  206. uniphier_fi2c_stop(priv);
  207. } else {
  208. complete:
  209. priv->enabled_irqs = 0;
  210. uniphier_fi2c_set_irqs(priv);
  211. complete(&priv->comp);
  212. }
  213. handled:
  214. uniphier_fi2c_clear_irqs(priv);
  215. return IRQ_HANDLED;
  216. }
  217. static void uniphier_fi2c_tx_init(struct uniphier_fi2c_priv *priv, u16 addr)
  218. {
  219. priv->enabled_irqs |= UNIPHIER_FI2C_INT_TE;
  220. /* do not use TX byte counter */
  221. writel(0, priv->membase + UNIPHIER_FI2C_TBC);
  222. /* set slave address */
  223. writel(UNIPHIER_FI2C_DTTX_CMD | addr << 1,
  224. priv->membase + UNIPHIER_FI2C_DTTX);
  225. /* first chunk of data */
  226. uniphier_fi2c_fill_txfifo(priv, true);
  227. }
  228. static void uniphier_fi2c_rx_init(struct uniphier_fi2c_priv *priv, u16 addr)
  229. {
  230. priv->flags |= UNIPHIER_FI2C_RD;
  231. if (likely(priv->len < 256)) {
  232. /*
  233. * If possible, use RX byte counter.
  234. * It can automatically handle NACK for the last byte.
  235. */
  236. writel(priv->len, priv->membase + UNIPHIER_FI2C_RBC);
  237. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF |
  238. UNIPHIER_FI2C_INT_RB;
  239. } else {
  240. /*
  241. * The byte counter can not count over 256. In this case,
  242. * do not use it at all. Drain data when FIFO gets full,
  243. * but treat the last portion as a special case.
  244. */
  245. writel(0, priv->membase + UNIPHIER_FI2C_RBC);
  246. priv->flags |= UNIPHIER_FI2C_MANUAL_NACK;
  247. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF;
  248. }
  249. /* set slave address with RD bit */
  250. writel(UNIPHIER_FI2C_DTTX_CMD | UNIPHIER_FI2C_DTTX_RD | addr << 1,
  251. priv->membase + UNIPHIER_FI2C_DTTX);
  252. }
  253. static void uniphier_fi2c_reset(struct uniphier_fi2c_priv *priv)
  254. {
  255. writel(UNIPHIER_FI2C_RST_RST, priv->membase + UNIPHIER_FI2C_RST);
  256. }
  257. static void uniphier_fi2c_prepare_operation(struct uniphier_fi2c_priv *priv)
  258. {
  259. writel(UNIPHIER_FI2C_BRST_FOEN | UNIPHIER_FI2C_BRST_RSCL,
  260. priv->membase + UNIPHIER_FI2C_BRST);
  261. }
  262. static void uniphier_fi2c_recover(struct uniphier_fi2c_priv *priv)
  263. {
  264. uniphier_fi2c_reset(priv);
  265. i2c_recover_bus(&priv->adap);
  266. }
  267. static int uniphier_fi2c_master_xfer_one(struct i2c_adapter *adap,
  268. struct i2c_msg *msg, bool stop)
  269. {
  270. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  271. bool is_read = msg->flags & I2C_M_RD;
  272. unsigned long time_left;
  273. dev_dbg(&adap->dev, "%s: addr=0x%02x, len=%d, stop=%d\n",
  274. is_read ? "receive" : "transmit", msg->addr, msg->len, stop);
  275. priv->len = msg->len;
  276. priv->buf = msg->buf;
  277. priv->enabled_irqs = UNIPHIER_FI2C_INT_FAULTS;
  278. priv->error = 0;
  279. priv->flags = 0;
  280. if (stop)
  281. priv->flags |= UNIPHIER_FI2C_STOP;
  282. reinit_completion(&priv->comp);
  283. uniphier_fi2c_clear_irqs(priv);
  284. writel(UNIPHIER_FI2C_RST_TBRST | UNIPHIER_FI2C_RST_RBRST,
  285. priv->membase + UNIPHIER_FI2C_RST); /* reset TX/RX FIFO */
  286. if (is_read)
  287. uniphier_fi2c_rx_init(priv, msg->addr);
  288. else
  289. uniphier_fi2c_tx_init(priv, msg->addr);
  290. uniphier_fi2c_set_irqs(priv);
  291. dev_dbg(&adap->dev, "start condition\n");
  292. writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STA,
  293. priv->membase + UNIPHIER_FI2C_CR);
  294. time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
  295. if (!time_left) {
  296. dev_err(&adap->dev, "transaction timeout.\n");
  297. uniphier_fi2c_recover(priv);
  298. return -ETIMEDOUT;
  299. }
  300. dev_dbg(&adap->dev, "complete\n");
  301. if (unlikely(priv->flags & UNIPHIER_FI2C_DEFER_STOP_COMP)) {
  302. u32 status = readl(priv->membase + UNIPHIER_FI2C_SR);
  303. if (!(status & UNIPHIER_FI2C_SR_STS) ||
  304. status & UNIPHIER_FI2C_SR_BB) {
  305. dev_err(&adap->dev,
  306. "stop condition was not completed.\n");
  307. uniphier_fi2c_recover(priv);
  308. return -EBUSY;
  309. }
  310. }
  311. return priv->error;
  312. }
  313. static int uniphier_fi2c_check_bus_busy(struct i2c_adapter *adap)
  314. {
  315. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  316. if (readl(priv->membase + UNIPHIER_FI2C_SR) & UNIPHIER_FI2C_SR_DB) {
  317. if (priv->busy_cnt++ > 3) {
  318. /*
  319. * If bus busy continues too long, it is probably
  320. * in a wrong state. Try bus recovery.
  321. */
  322. uniphier_fi2c_recover(priv);
  323. priv->busy_cnt = 0;
  324. }
  325. return -EAGAIN;
  326. }
  327. priv->busy_cnt = 0;
  328. return 0;
  329. }
  330. static int uniphier_fi2c_master_xfer(struct i2c_adapter *adap,
  331. struct i2c_msg *msgs, int num)
  332. {
  333. struct i2c_msg *msg, *emsg = msgs + num;
  334. int ret;
  335. ret = uniphier_fi2c_check_bus_busy(adap);
  336. if (ret)
  337. return ret;
  338. for (msg = msgs; msg < emsg; msg++) {
  339. /* Emit STOP if it is the last message or I2C_M_STOP is set. */
  340. bool stop = (msg + 1 == emsg) || (msg->flags & I2C_M_STOP);
  341. ret = uniphier_fi2c_master_xfer_one(adap, msg, stop);
  342. if (ret)
  343. return ret;
  344. }
  345. return num;
  346. }
  347. static u32 uniphier_fi2c_functionality(struct i2c_adapter *adap)
  348. {
  349. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  350. }
  351. static const struct i2c_algorithm uniphier_fi2c_algo = {
  352. .master_xfer = uniphier_fi2c_master_xfer,
  353. .functionality = uniphier_fi2c_functionality,
  354. };
  355. static int uniphier_fi2c_get_scl(struct i2c_adapter *adap)
  356. {
  357. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  358. return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
  359. UNIPHIER_FI2C_BM_SCLS);
  360. }
  361. static void uniphier_fi2c_set_scl(struct i2c_adapter *adap, int val)
  362. {
  363. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  364. writel(val ? UNIPHIER_FI2C_BRST_RSCL : 0,
  365. priv->membase + UNIPHIER_FI2C_BRST);
  366. }
  367. static int uniphier_fi2c_get_sda(struct i2c_adapter *adap)
  368. {
  369. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  370. return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
  371. UNIPHIER_FI2C_BM_SDAS);
  372. }
  373. static void uniphier_fi2c_unprepare_recovery(struct i2c_adapter *adap)
  374. {
  375. uniphier_fi2c_prepare_operation(i2c_get_adapdata(adap));
  376. }
  377. static struct i2c_bus_recovery_info uniphier_fi2c_bus_recovery_info = {
  378. .recover_bus = i2c_generic_scl_recovery,
  379. .get_scl = uniphier_fi2c_get_scl,
  380. .set_scl = uniphier_fi2c_set_scl,
  381. .get_sda = uniphier_fi2c_get_sda,
  382. .unprepare_recovery = uniphier_fi2c_unprepare_recovery,
  383. };
  384. static int uniphier_fi2c_clk_init(struct device *dev,
  385. struct uniphier_fi2c_priv *priv)
  386. {
  387. struct device_node *np = dev->of_node;
  388. unsigned long clk_rate;
  389. u32 bus_speed, clk_count;
  390. int ret;
  391. if (of_property_read_u32(np, "clock-frequency", &bus_speed))
  392. bus_speed = UNIPHIER_FI2C_DEFAULT_SPEED;
  393. if (bus_speed > UNIPHIER_FI2C_MAX_SPEED)
  394. bus_speed = UNIPHIER_FI2C_MAX_SPEED;
  395. /* Get input clk rate through clk driver */
  396. priv->clk = devm_clk_get(dev, NULL);
  397. if (IS_ERR(priv->clk)) {
  398. dev_err(dev, "failed to get clock\n");
  399. return PTR_ERR(priv->clk);
  400. }
  401. ret = clk_prepare_enable(priv->clk);
  402. if (ret)
  403. return ret;
  404. clk_rate = clk_get_rate(priv->clk);
  405. uniphier_fi2c_reset(priv);
  406. clk_count = clk_rate / bus_speed;
  407. writel(clk_count, priv->membase + UNIPHIER_FI2C_CYC);
  408. writel(clk_count / 2, priv->membase + UNIPHIER_FI2C_LCTL);
  409. writel(clk_count / 2, priv->membase + UNIPHIER_FI2C_SSUT);
  410. writel(clk_count / 16, priv->membase + UNIPHIER_FI2C_DSUT);
  411. uniphier_fi2c_prepare_operation(priv);
  412. return 0;
  413. }
  414. static int uniphier_fi2c_probe(struct platform_device *pdev)
  415. {
  416. struct device *dev = &pdev->dev;
  417. struct uniphier_fi2c_priv *priv;
  418. struct resource *regs;
  419. int irq;
  420. int ret;
  421. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  422. if (!priv)
  423. return -ENOMEM;
  424. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  425. priv->membase = devm_ioremap_resource(dev, regs);
  426. if (IS_ERR(priv->membase))
  427. return PTR_ERR(priv->membase);
  428. irq = platform_get_irq(pdev, 0);
  429. if (irq < 0) {
  430. dev_err(dev, "failed to get IRQ number");
  431. return irq;
  432. }
  433. init_completion(&priv->comp);
  434. priv->adap.owner = THIS_MODULE;
  435. priv->adap.algo = &uniphier_fi2c_algo;
  436. priv->adap.dev.parent = dev;
  437. priv->adap.dev.of_node = dev->of_node;
  438. strlcpy(priv->adap.name, "UniPhier FI2C", sizeof(priv->adap.name));
  439. priv->adap.bus_recovery_info = &uniphier_fi2c_bus_recovery_info;
  440. i2c_set_adapdata(&priv->adap, priv);
  441. platform_set_drvdata(pdev, priv);
  442. ret = uniphier_fi2c_clk_init(dev, priv);
  443. if (ret)
  444. return ret;
  445. ret = devm_request_irq(dev, irq, uniphier_fi2c_interrupt, 0,
  446. pdev->name, priv);
  447. if (ret) {
  448. dev_err(dev, "failed to request irq %d\n", irq);
  449. goto err;
  450. }
  451. ret = i2c_add_adapter(&priv->adap);
  452. if (ret) {
  453. dev_err(dev, "failed to add I2C adapter\n");
  454. goto err;
  455. }
  456. err:
  457. if (ret)
  458. clk_disable_unprepare(priv->clk);
  459. return ret;
  460. }
  461. static int uniphier_fi2c_remove(struct platform_device *pdev)
  462. {
  463. struct uniphier_fi2c_priv *priv = platform_get_drvdata(pdev);
  464. i2c_del_adapter(&priv->adap);
  465. clk_disable_unprepare(priv->clk);
  466. return 0;
  467. }
  468. static const struct of_device_id uniphier_fi2c_match[] = {
  469. { .compatible = "socionext,uniphier-fi2c" },
  470. { /* sentinel */ }
  471. };
  472. MODULE_DEVICE_TABLE(of, uniphier_fi2c_match);
  473. static struct platform_driver uniphier_fi2c_drv = {
  474. .probe = uniphier_fi2c_probe,
  475. .remove = uniphier_fi2c_remove,
  476. .driver = {
  477. .name = "uniphier-fi2c",
  478. .of_match_table = uniphier_fi2c_match,
  479. },
  480. };
  481. module_platform_driver(uniphier_fi2c_drv);
  482. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  483. MODULE_DESCRIPTION("UniPhier FIFO-builtin I2C bus driver");
  484. MODULE_LICENSE("GPL");