i2c-uniphier-f.c 17 KB

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