i2c-uniphier-f.c 18 KB

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