i2c-uniphier-f.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. };
  95. static void uniphier_fi2c_fill_txfifo(struct uniphier_fi2c_priv *priv,
  96. bool first)
  97. {
  98. int fifo_space = UNIPHIER_FI2C_FIFO_SIZE;
  99. /*
  100. * TX-FIFO stores slave address in it for the first access.
  101. * Decrement the counter.
  102. */
  103. if (first)
  104. fifo_space--;
  105. while (priv->len) {
  106. if (fifo_space-- <= 0)
  107. break;
  108. dev_dbg(&priv->adap.dev, "write data: %02x\n", *priv->buf);
  109. writel(*priv->buf++, priv->membase + UNIPHIER_FI2C_DTTX);
  110. priv->len--;
  111. }
  112. }
  113. static void uniphier_fi2c_drain_rxfifo(struct uniphier_fi2c_priv *priv)
  114. {
  115. int fifo_left = priv->flags & UNIPHIER_FI2C_BYTE_WISE ?
  116. 1 : UNIPHIER_FI2C_FIFO_SIZE;
  117. while (priv->len) {
  118. if (fifo_left-- <= 0)
  119. break;
  120. *priv->buf++ = readl(priv->membase + UNIPHIER_FI2C_DTRX);
  121. dev_dbg(&priv->adap.dev, "read data: %02x\n", priv->buf[-1]);
  122. priv->len--;
  123. }
  124. }
  125. static void uniphier_fi2c_set_irqs(struct uniphier_fi2c_priv *priv)
  126. {
  127. writel(priv->enabled_irqs, priv->membase + UNIPHIER_FI2C_IE);
  128. }
  129. static void uniphier_fi2c_clear_irqs(struct uniphier_fi2c_priv *priv)
  130. {
  131. writel(-1, priv->membase + UNIPHIER_FI2C_IC);
  132. }
  133. static void uniphier_fi2c_stop(struct uniphier_fi2c_priv *priv)
  134. {
  135. dev_dbg(&priv->adap.dev, "stop condition\n");
  136. priv->enabled_irqs |= UNIPHIER_FI2C_INT_STOP;
  137. uniphier_fi2c_set_irqs(priv);
  138. writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STO,
  139. priv->membase + UNIPHIER_FI2C_CR);
  140. }
  141. static irqreturn_t uniphier_fi2c_interrupt(int irq, void *dev_id)
  142. {
  143. struct uniphier_fi2c_priv *priv = dev_id;
  144. u32 irq_status;
  145. irq_status = readl(priv->membase + UNIPHIER_FI2C_INT);
  146. dev_dbg(&priv->adap.dev,
  147. "interrupt: enabled_irqs=%04x, irq_status=%04x\n",
  148. priv->enabled_irqs, irq_status);
  149. if (irq_status & UNIPHIER_FI2C_INT_STOP)
  150. goto complete;
  151. if (unlikely(irq_status & UNIPHIER_FI2C_INT_AL)) {
  152. dev_dbg(&priv->adap.dev, "arbitration lost\n");
  153. priv->error = -EAGAIN;
  154. goto complete;
  155. }
  156. if (unlikely(irq_status & UNIPHIER_FI2C_INT_NA)) {
  157. dev_dbg(&priv->adap.dev, "could not get ACK\n");
  158. priv->error = -ENXIO;
  159. if (priv->flags & UNIPHIER_FI2C_RD) {
  160. /*
  161. * work around a hardware bug:
  162. * The receive-completed interrupt is never set even if
  163. * STOP condition is detected after the address phase
  164. * of read transaction fails to get ACK.
  165. * To avoid time-out error, we issue STOP here,
  166. * but do not wait for its completion.
  167. * It should be checked after exiting this handler.
  168. */
  169. uniphier_fi2c_stop(priv);
  170. priv->flags |= UNIPHIER_FI2C_DEFER_STOP_COMP;
  171. goto complete;
  172. }
  173. goto stop;
  174. }
  175. if (irq_status & UNIPHIER_FI2C_INT_TE) {
  176. if (!priv->len)
  177. goto data_done;
  178. uniphier_fi2c_fill_txfifo(priv, false);
  179. goto handled;
  180. }
  181. if (irq_status & (UNIPHIER_FI2C_INT_RF | UNIPHIER_FI2C_INT_RB)) {
  182. uniphier_fi2c_drain_rxfifo(priv);
  183. if (!priv->len)
  184. goto data_done;
  185. if (unlikely(priv->flags & UNIPHIER_FI2C_MANUAL_NACK)) {
  186. if (priv->len <= UNIPHIER_FI2C_FIFO_SIZE &&
  187. !(priv->flags & UNIPHIER_FI2C_BYTE_WISE)) {
  188. dev_dbg(&priv->adap.dev,
  189. "enable read byte count IRQ\n");
  190. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RB;
  191. uniphier_fi2c_set_irqs(priv);
  192. priv->flags |= UNIPHIER_FI2C_BYTE_WISE;
  193. }
  194. if (priv->len <= 1) {
  195. dev_dbg(&priv->adap.dev, "set NACK\n");
  196. writel(UNIPHIER_FI2C_CR_MST |
  197. UNIPHIER_FI2C_CR_NACK,
  198. priv->membase + UNIPHIER_FI2C_CR);
  199. }
  200. }
  201. goto handled;
  202. }
  203. return IRQ_NONE;
  204. data_done:
  205. if (priv->flags & UNIPHIER_FI2C_STOP) {
  206. stop:
  207. uniphier_fi2c_stop(priv);
  208. } else {
  209. complete:
  210. priv->enabled_irqs = 0;
  211. uniphier_fi2c_set_irqs(priv);
  212. complete(&priv->comp);
  213. }
  214. handled:
  215. uniphier_fi2c_clear_irqs(priv);
  216. return IRQ_HANDLED;
  217. }
  218. static void uniphier_fi2c_tx_init(struct uniphier_fi2c_priv *priv, u16 addr)
  219. {
  220. priv->enabled_irqs |= UNIPHIER_FI2C_INT_TE;
  221. /* do not use TX byte counter */
  222. writel(0, priv->membase + UNIPHIER_FI2C_TBC);
  223. /* set slave address */
  224. writel(UNIPHIER_FI2C_DTTX_CMD | addr << 1,
  225. priv->membase + UNIPHIER_FI2C_DTTX);
  226. /* first chunk of data */
  227. uniphier_fi2c_fill_txfifo(priv, true);
  228. }
  229. static void uniphier_fi2c_rx_init(struct uniphier_fi2c_priv *priv, u16 addr)
  230. {
  231. priv->flags |= UNIPHIER_FI2C_RD;
  232. if (likely(priv->len < 256)) {
  233. /*
  234. * If possible, use RX byte counter.
  235. * It can automatically handle NACK for the last byte.
  236. */
  237. writel(priv->len, priv->membase + UNIPHIER_FI2C_RBC);
  238. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF |
  239. UNIPHIER_FI2C_INT_RB;
  240. } else {
  241. /*
  242. * The byte counter can not count over 256. In this case,
  243. * do not use it at all. Drain data when FIFO gets full,
  244. * but treat the last portion as a special case.
  245. */
  246. writel(0, priv->membase + UNIPHIER_FI2C_RBC);
  247. priv->flags |= UNIPHIER_FI2C_MANUAL_NACK;
  248. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF;
  249. }
  250. /* set slave address with RD bit */
  251. writel(UNIPHIER_FI2C_DTTX_CMD | UNIPHIER_FI2C_DTTX_RD | addr << 1,
  252. priv->membase + UNIPHIER_FI2C_DTTX);
  253. }
  254. static void uniphier_fi2c_reset(struct uniphier_fi2c_priv *priv)
  255. {
  256. writel(UNIPHIER_FI2C_RST_RST, priv->membase + UNIPHIER_FI2C_RST);
  257. }
  258. static void uniphier_fi2c_prepare_operation(struct uniphier_fi2c_priv *priv)
  259. {
  260. writel(UNIPHIER_FI2C_BRST_FOEN | UNIPHIER_FI2C_BRST_RSCL,
  261. priv->membase + UNIPHIER_FI2C_BRST);
  262. }
  263. static void uniphier_fi2c_recover(struct uniphier_fi2c_priv *priv)
  264. {
  265. uniphier_fi2c_reset(priv);
  266. i2c_recover_bus(&priv->adap);
  267. }
  268. static int uniphier_fi2c_master_xfer_one(struct i2c_adapter *adap,
  269. struct i2c_msg *msg, bool stop)
  270. {
  271. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  272. bool is_read = msg->flags & I2C_M_RD;
  273. unsigned long time_left;
  274. dev_dbg(&adap->dev, "%s: addr=0x%02x, len=%d, stop=%d\n",
  275. is_read ? "receive" : "transmit", msg->addr, msg->len, stop);
  276. priv->len = msg->len;
  277. priv->buf = msg->buf;
  278. priv->enabled_irqs = UNIPHIER_FI2C_INT_FAULTS;
  279. priv->error = 0;
  280. priv->flags = 0;
  281. if (stop)
  282. priv->flags |= UNIPHIER_FI2C_STOP;
  283. reinit_completion(&priv->comp);
  284. uniphier_fi2c_clear_irqs(priv);
  285. writel(UNIPHIER_FI2C_RST_TBRST | UNIPHIER_FI2C_RST_RBRST,
  286. priv->membase + UNIPHIER_FI2C_RST); /* reset TX/RX FIFO */
  287. if (is_read)
  288. uniphier_fi2c_rx_init(priv, msg->addr);
  289. else
  290. uniphier_fi2c_tx_init(priv, msg->addr);
  291. uniphier_fi2c_set_irqs(priv);
  292. dev_dbg(&adap->dev, "start condition\n");
  293. writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STA,
  294. priv->membase + UNIPHIER_FI2C_CR);
  295. time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
  296. if (!time_left) {
  297. dev_err(&adap->dev, "transaction timeout.\n");
  298. uniphier_fi2c_recover(priv);
  299. return -ETIMEDOUT;
  300. }
  301. dev_dbg(&adap->dev, "complete\n");
  302. if (unlikely(priv->flags & UNIPHIER_FI2C_DEFER_STOP_COMP)) {
  303. u32 status;
  304. int ret;
  305. ret = readl_poll_timeout(priv->membase + UNIPHIER_FI2C_SR,
  306. status,
  307. (status & UNIPHIER_FI2C_SR_STS) &&
  308. !(status & UNIPHIER_FI2C_SR_BB),
  309. 1, 20);
  310. if (ret) {
  311. dev_err(&adap->dev,
  312. "stop condition was not completed.\n");
  313. uniphier_fi2c_recover(priv);
  314. return ret;
  315. }
  316. }
  317. return priv->error;
  318. }
  319. static int uniphier_fi2c_check_bus_busy(struct i2c_adapter *adap)
  320. {
  321. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  322. if (readl(priv->membase + UNIPHIER_FI2C_SR) & UNIPHIER_FI2C_SR_DB) {
  323. if (priv->busy_cnt++ > 3) {
  324. /*
  325. * If bus busy continues too long, it is probably
  326. * in a wrong state. Try bus recovery.
  327. */
  328. uniphier_fi2c_recover(priv);
  329. priv->busy_cnt = 0;
  330. }
  331. return -EAGAIN;
  332. }
  333. priv->busy_cnt = 0;
  334. return 0;
  335. }
  336. static int uniphier_fi2c_master_xfer(struct i2c_adapter *adap,
  337. struct i2c_msg *msgs, int num)
  338. {
  339. struct i2c_msg *msg, *emsg = msgs + num;
  340. int ret;
  341. ret = uniphier_fi2c_check_bus_busy(adap);
  342. if (ret)
  343. return ret;
  344. for (msg = msgs; msg < emsg; msg++) {
  345. /* If next message is read, skip the stop condition */
  346. bool stop = !(msg + 1 < emsg && msg[1].flags & I2C_M_RD);
  347. /* but, force it if I2C_M_STOP is set */
  348. if (msg->flags & I2C_M_STOP)
  349. stop = true;
  350. ret = uniphier_fi2c_master_xfer_one(adap, msg, stop);
  351. if (ret)
  352. return ret;
  353. }
  354. return num;
  355. }
  356. static u32 uniphier_fi2c_functionality(struct i2c_adapter *adap)
  357. {
  358. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  359. }
  360. static const struct i2c_algorithm uniphier_fi2c_algo = {
  361. .master_xfer = uniphier_fi2c_master_xfer,
  362. .functionality = uniphier_fi2c_functionality,
  363. };
  364. static int uniphier_fi2c_get_scl(struct i2c_adapter *adap)
  365. {
  366. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  367. return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
  368. UNIPHIER_FI2C_BM_SCLS);
  369. }
  370. static void uniphier_fi2c_set_scl(struct i2c_adapter *adap, int val)
  371. {
  372. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  373. writel(val ? UNIPHIER_FI2C_BRST_RSCL : 0,
  374. priv->membase + UNIPHIER_FI2C_BRST);
  375. }
  376. static int uniphier_fi2c_get_sda(struct i2c_adapter *adap)
  377. {
  378. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  379. return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
  380. UNIPHIER_FI2C_BM_SDAS);
  381. }
  382. static void uniphier_fi2c_unprepare_recovery(struct i2c_adapter *adap)
  383. {
  384. uniphier_fi2c_prepare_operation(i2c_get_adapdata(adap));
  385. }
  386. static struct i2c_bus_recovery_info uniphier_fi2c_bus_recovery_info = {
  387. .recover_bus = i2c_generic_scl_recovery,
  388. .get_scl = uniphier_fi2c_get_scl,
  389. .set_scl = uniphier_fi2c_set_scl,
  390. .get_sda = uniphier_fi2c_get_sda,
  391. .unprepare_recovery = uniphier_fi2c_unprepare_recovery,
  392. };
  393. static void uniphier_fi2c_hw_init(struct uniphier_fi2c_priv *priv,
  394. u32 bus_speed, unsigned long clk_rate)
  395. {
  396. u32 tmp;
  397. tmp = readl(priv->membase + UNIPHIER_FI2C_CR);
  398. tmp |= UNIPHIER_FI2C_CR_MST;
  399. writel(tmp, priv->membase + UNIPHIER_FI2C_CR);
  400. uniphier_fi2c_reset(priv);
  401. tmp = clk_rate / bus_speed;
  402. writel(tmp, priv->membase + UNIPHIER_FI2C_CYC);
  403. writel(tmp / 2, priv->membase + UNIPHIER_FI2C_LCTL);
  404. writel(tmp / 2, priv->membase + UNIPHIER_FI2C_SSUT);
  405. writel(tmp / 16, priv->membase + UNIPHIER_FI2C_DSUT);
  406. uniphier_fi2c_prepare_operation(priv);
  407. }
  408. static int uniphier_fi2c_probe(struct platform_device *pdev)
  409. {
  410. struct device *dev = &pdev->dev;
  411. struct uniphier_fi2c_priv *priv;
  412. struct resource *regs;
  413. u32 bus_speed;
  414. unsigned long clk_rate;
  415. int irq, ret;
  416. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  417. if (!priv)
  418. return -ENOMEM;
  419. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  420. priv->membase = devm_ioremap_resource(dev, regs);
  421. if (IS_ERR(priv->membase))
  422. return PTR_ERR(priv->membase);
  423. irq = platform_get_irq(pdev, 0);
  424. if (irq < 0) {
  425. dev_err(dev, "failed to get IRQ number\n");
  426. return irq;
  427. }
  428. if (of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed))
  429. bus_speed = UNIPHIER_FI2C_DEFAULT_SPEED;
  430. if (!bus_speed || bus_speed > UNIPHIER_FI2C_MAX_SPEED) {
  431. dev_err(dev, "invalid clock-frequency %d\n", bus_speed);
  432. return -EINVAL;
  433. }
  434. priv->clk = devm_clk_get(dev, NULL);
  435. if (IS_ERR(priv->clk)) {
  436. dev_err(dev, "failed to get clock\n");
  437. return PTR_ERR(priv->clk);
  438. }
  439. ret = clk_prepare_enable(priv->clk);
  440. if (ret)
  441. return ret;
  442. clk_rate = clk_get_rate(priv->clk);
  443. if (!clk_rate) {
  444. dev_err(dev, "input clock rate should not be zero\n");
  445. ret = -EINVAL;
  446. goto err;
  447. }
  448. init_completion(&priv->comp);
  449. priv->adap.owner = THIS_MODULE;
  450. priv->adap.algo = &uniphier_fi2c_algo;
  451. priv->adap.dev.parent = dev;
  452. priv->adap.dev.of_node = dev->of_node;
  453. strlcpy(priv->adap.name, "UniPhier FI2C", sizeof(priv->adap.name));
  454. priv->adap.bus_recovery_info = &uniphier_fi2c_bus_recovery_info;
  455. i2c_set_adapdata(&priv->adap, priv);
  456. platform_set_drvdata(pdev, priv);
  457. uniphier_fi2c_hw_init(priv, bus_speed, clk_rate);
  458. ret = devm_request_irq(dev, irq, uniphier_fi2c_interrupt, 0,
  459. pdev->name, priv);
  460. if (ret) {
  461. dev_err(dev, "failed to request irq %d\n", irq);
  462. goto err;
  463. }
  464. ret = i2c_add_adapter(&priv->adap);
  465. err:
  466. if (ret)
  467. clk_disable_unprepare(priv->clk);
  468. return ret;
  469. }
  470. static int uniphier_fi2c_remove(struct platform_device *pdev)
  471. {
  472. struct uniphier_fi2c_priv *priv = platform_get_drvdata(pdev);
  473. i2c_del_adapter(&priv->adap);
  474. clk_disable_unprepare(priv->clk);
  475. return 0;
  476. }
  477. static const struct of_device_id uniphier_fi2c_match[] = {
  478. { .compatible = "socionext,uniphier-fi2c" },
  479. { /* sentinel */ }
  480. };
  481. MODULE_DEVICE_TABLE(of, uniphier_fi2c_match);
  482. static struct platform_driver uniphier_fi2c_drv = {
  483. .probe = uniphier_fi2c_probe,
  484. .remove = uniphier_fi2c_remove,
  485. .driver = {
  486. .name = "uniphier-fi2c",
  487. .of_match_table = uniphier_fi2c_match,
  488. },
  489. };
  490. module_platform_driver(uniphier_fi2c_drv);
  491. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  492. MODULE_DESCRIPTION("UniPhier FIFO-builtin I2C bus driver");
  493. MODULE_LICENSE("GPL");