i2c-owl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Actions Semiconductor Owl SoC's I2C driver
  4. *
  5. * Copyright (c) 2014 Actions Semi Inc.
  6. * Author: David Liu <liuwei@actions-semi.com>
  7. *
  8. * Copyright (c) 2018 Linaro Ltd.
  9. * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/i2c.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/of_device.h>
  18. /* I2C registers */
  19. #define OWL_I2C_REG_CTL 0x0000
  20. #define OWL_I2C_REG_CLKDIV 0x0004
  21. #define OWL_I2C_REG_STAT 0x0008
  22. #define OWL_I2C_REG_ADDR 0x000C
  23. #define OWL_I2C_REG_TXDAT 0x0010
  24. #define OWL_I2C_REG_RXDAT 0x0014
  25. #define OWL_I2C_REG_CMD 0x0018
  26. #define OWL_I2C_REG_FIFOCTL 0x001C
  27. #define OWL_I2C_REG_FIFOSTAT 0x0020
  28. #define OWL_I2C_REG_DATCNT 0x0024
  29. #define OWL_I2C_REG_RCNT 0x0028
  30. /* I2Cx_CTL Bit Mask */
  31. #define OWL_I2C_CTL_RB BIT(1)
  32. #define OWL_I2C_CTL_GBCC(x) (((x) & 0x3) << 2)
  33. #define OWL_I2C_CTL_GBCC_NONE OWL_I2C_CTL_GBCC(0)
  34. #define OWL_I2C_CTL_GBCC_START OWL_I2C_CTL_GBCC(1)
  35. #define OWL_I2C_CTL_GBCC_STOP OWL_I2C_CTL_GBCC(2)
  36. #define OWL_I2C_CTL_GBCC_RSTART OWL_I2C_CTL_GBCC(3)
  37. #define OWL_I2C_CTL_IRQE BIT(5)
  38. #define OWL_I2C_CTL_EN BIT(7)
  39. #define OWL_I2C_CTL_AE BIT(8)
  40. #define OWL_I2C_CTL_SHSM BIT(10)
  41. #define OWL_I2C_DIV_FACTOR(x) ((x) & 0xff)
  42. /* I2Cx_STAT Bit Mask */
  43. #define OWL_I2C_STAT_RACK BIT(0)
  44. #define OWL_I2C_STAT_BEB BIT(1)
  45. #define OWL_I2C_STAT_IRQP BIT(2)
  46. #define OWL_I2C_STAT_LAB BIT(3)
  47. #define OWL_I2C_STAT_STPD BIT(4)
  48. #define OWL_I2C_STAT_STAD BIT(5)
  49. #define OWL_I2C_STAT_BBB BIT(6)
  50. #define OWL_I2C_STAT_TCB BIT(7)
  51. #define OWL_I2C_STAT_LBST BIT(8)
  52. #define OWL_I2C_STAT_SAMB BIT(9)
  53. #define OWL_I2C_STAT_SRGC BIT(10)
  54. /* I2Cx_CMD Bit Mask */
  55. #define OWL_I2C_CMD_SBE BIT(0)
  56. #define OWL_I2C_CMD_RBE BIT(4)
  57. #define OWL_I2C_CMD_DE BIT(8)
  58. #define OWL_I2C_CMD_NS BIT(9)
  59. #define OWL_I2C_CMD_SE BIT(10)
  60. #define OWL_I2C_CMD_MSS BIT(11)
  61. #define OWL_I2C_CMD_WRS BIT(12)
  62. #define OWL_I2C_CMD_SECL BIT(15)
  63. #define OWL_I2C_CMD_AS(x) (((x) & 0x7) << 1)
  64. #define OWL_I2C_CMD_SAS(x) (((x) & 0x7) << 5)
  65. /* I2Cx_FIFOCTL Bit Mask */
  66. #define OWL_I2C_FIFOCTL_NIB BIT(0)
  67. #define OWL_I2C_FIFOCTL_RFR BIT(1)
  68. #define OWL_I2C_FIFOCTL_TFR BIT(2)
  69. /* I2Cc_FIFOSTAT Bit Mask */
  70. #define OWL_I2C_FIFOSTAT_RNB BIT(1)
  71. #define OWL_I2C_FIFOSTAT_RFE BIT(2)
  72. #define OWL_I2C_FIFOSTAT_TFF BIT(5)
  73. #define OWL_I2C_FIFOSTAT_TFD GENMASK(23, 16)
  74. #define OWL_I2C_FIFOSTAT_RFD GENMASK(15, 8)
  75. /* I2C bus timeout */
  76. #define OWL_I2C_TIMEOUT msecs_to_jiffies(4 * 1000)
  77. #define OWL_I2C_MAX_RETRIES 50
  78. #define OWL_I2C_DEF_SPEED_HZ 100000
  79. #define OWL_I2C_MAX_SPEED_HZ 400000
  80. struct owl_i2c_dev {
  81. struct i2c_adapter adap;
  82. struct i2c_msg *msg;
  83. struct completion msg_complete;
  84. struct clk *clk;
  85. spinlock_t lock;
  86. void __iomem *base;
  87. unsigned long clk_rate;
  88. u32 bus_freq;
  89. u32 msg_ptr;
  90. int err;
  91. };
  92. static void owl_i2c_update_reg(void __iomem *reg, unsigned int val, bool state)
  93. {
  94. unsigned int regval;
  95. regval = readl(reg);
  96. if (state)
  97. regval |= val;
  98. else
  99. regval &= ~val;
  100. writel(regval, reg);
  101. }
  102. static void owl_i2c_reset(struct owl_i2c_dev *i2c_dev)
  103. {
  104. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
  105. OWL_I2C_CTL_EN, false);
  106. mdelay(1);
  107. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
  108. OWL_I2C_CTL_EN, true);
  109. /* Clear status registers */
  110. writel(0, i2c_dev->base + OWL_I2C_REG_STAT);
  111. }
  112. static int owl_i2c_reset_fifo(struct owl_i2c_dev *i2c_dev)
  113. {
  114. unsigned int val, timeout = 0;
  115. /* Reset FIFO */
  116. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
  117. OWL_I2C_FIFOCTL_RFR | OWL_I2C_FIFOCTL_TFR,
  118. true);
  119. /* Wait 50ms for FIFO reset complete */
  120. do {
  121. val = readl(i2c_dev->base + OWL_I2C_REG_FIFOCTL);
  122. if (!(val & (OWL_I2C_FIFOCTL_RFR | OWL_I2C_FIFOCTL_TFR)))
  123. break;
  124. usleep_range(500, 1000);
  125. } while (timeout++ < OWL_I2C_MAX_RETRIES);
  126. if (timeout > OWL_I2C_MAX_RETRIES) {
  127. dev_err(&i2c_dev->adap.dev, "FIFO reset timeout\n");
  128. return -ETIMEDOUT;
  129. }
  130. return 0;
  131. }
  132. static void owl_i2c_set_freq(struct owl_i2c_dev *i2c_dev)
  133. {
  134. unsigned int val;
  135. val = DIV_ROUND_UP(i2c_dev->clk_rate, i2c_dev->bus_freq * 16);
  136. /* Set clock divider factor */
  137. writel(OWL_I2C_DIV_FACTOR(val), i2c_dev->base + OWL_I2C_REG_CLKDIV);
  138. }
  139. static irqreturn_t owl_i2c_interrupt(int irq, void *_dev)
  140. {
  141. struct owl_i2c_dev *i2c_dev = _dev;
  142. struct i2c_msg *msg = i2c_dev->msg;
  143. unsigned long flags;
  144. unsigned int stat, fifostat;
  145. spin_lock_irqsave(&i2c_dev->lock, flags);
  146. i2c_dev->err = 0;
  147. /* Handle NACK from slave */
  148. fifostat = readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT);
  149. if (fifostat & OWL_I2C_FIFOSTAT_RNB) {
  150. i2c_dev->err = -ENXIO;
  151. /* Clear NACK error bit by writing "1" */
  152. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOSTAT,
  153. OWL_I2C_FIFOSTAT_RNB, true);
  154. goto stop;
  155. }
  156. /* Handle bus error */
  157. stat = readl(i2c_dev->base + OWL_I2C_REG_STAT);
  158. if (stat & OWL_I2C_STAT_BEB) {
  159. i2c_dev->err = -EIO;
  160. /* Clear BUS error bit by writing "1" */
  161. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT,
  162. OWL_I2C_STAT_BEB, true);
  163. goto stop;
  164. }
  165. /* Handle FIFO read */
  166. if (msg->flags & I2C_M_RD) {
  167. while ((readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
  168. OWL_I2C_FIFOSTAT_RFE) && i2c_dev->msg_ptr < msg->len) {
  169. msg->buf[i2c_dev->msg_ptr++] = readl(i2c_dev->base +
  170. OWL_I2C_REG_RXDAT);
  171. }
  172. } else {
  173. /* Handle the remaining bytes which were not sent */
  174. while (!(readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
  175. OWL_I2C_FIFOSTAT_TFF) && i2c_dev->msg_ptr < msg->len) {
  176. writel(msg->buf[i2c_dev->msg_ptr++],
  177. i2c_dev->base + OWL_I2C_REG_TXDAT);
  178. }
  179. }
  180. stop:
  181. /* Clear pending interrupts */
  182. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT,
  183. OWL_I2C_STAT_IRQP, true);
  184. complete_all(&i2c_dev->msg_complete);
  185. spin_unlock_irqrestore(&i2c_dev->lock, flags);
  186. return IRQ_HANDLED;
  187. }
  188. static u32 owl_i2c_func(struct i2c_adapter *adap)
  189. {
  190. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  191. }
  192. static int owl_i2c_check_bus_busy(struct i2c_adapter *adap)
  193. {
  194. struct owl_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  195. unsigned long timeout;
  196. /* Check for Bus busy */
  197. timeout = jiffies + OWL_I2C_TIMEOUT;
  198. while (readl(i2c_dev->base + OWL_I2C_REG_STAT) & OWL_I2C_STAT_BBB) {
  199. if (time_after(jiffies, timeout)) {
  200. dev_err(&adap->dev, "Bus busy timeout\n");
  201. return -ETIMEDOUT;
  202. }
  203. }
  204. return 0;
  205. }
  206. static int owl_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  207. int num)
  208. {
  209. struct owl_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  210. struct i2c_msg *msg;
  211. unsigned long time_left, flags;
  212. unsigned int i2c_cmd, val;
  213. unsigned int addr;
  214. int ret, idx;
  215. spin_lock_irqsave(&i2c_dev->lock, flags);
  216. /* Reset I2C controller */
  217. owl_i2c_reset(i2c_dev);
  218. /* Set bus frequency */
  219. owl_i2c_set_freq(i2c_dev);
  220. /*
  221. * Spinlock should be released before calling reset FIFO and
  222. * bus busy check since those functions may sleep
  223. */
  224. spin_unlock_irqrestore(&i2c_dev->lock, flags);
  225. /* Reset FIFO */
  226. ret = owl_i2c_reset_fifo(i2c_dev);
  227. if (ret)
  228. goto unlocked_err_exit;
  229. /* Check for bus busy */
  230. ret = owl_i2c_check_bus_busy(adap);
  231. if (ret)
  232. goto unlocked_err_exit;
  233. spin_lock_irqsave(&i2c_dev->lock, flags);
  234. /* Check for Arbitration lost */
  235. val = readl(i2c_dev->base + OWL_I2C_REG_STAT);
  236. if (val & OWL_I2C_STAT_LAB) {
  237. val &= ~OWL_I2C_STAT_LAB;
  238. writel(val, i2c_dev->base + OWL_I2C_REG_STAT);
  239. ret = -EAGAIN;
  240. goto err_exit;
  241. }
  242. reinit_completion(&i2c_dev->msg_complete);
  243. /* Enable I2C controller interrupt */
  244. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
  245. OWL_I2C_CTL_IRQE, true);
  246. /*
  247. * Select: FIFO enable, Master mode, Stop enable, Data count enable,
  248. * Send start bit
  249. */
  250. i2c_cmd = OWL_I2C_CMD_SECL | OWL_I2C_CMD_MSS | OWL_I2C_CMD_SE |
  251. OWL_I2C_CMD_NS | OWL_I2C_CMD_DE | OWL_I2C_CMD_SBE;
  252. /* Handle repeated start condition */
  253. if (num > 1) {
  254. /* Set internal address length and enable repeated start */
  255. i2c_cmd |= OWL_I2C_CMD_AS(msgs[0].len + 1) |
  256. OWL_I2C_CMD_SAS(1) | OWL_I2C_CMD_RBE;
  257. /* Write slave address */
  258. addr = i2c_8bit_addr_from_msg(&msgs[0]);
  259. writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT);
  260. /* Write internal register address */
  261. for (idx = 0; idx < msgs[0].len; idx++)
  262. writel(msgs[0].buf[idx],
  263. i2c_dev->base + OWL_I2C_REG_TXDAT);
  264. msg = &msgs[1];
  265. } else {
  266. /* Set address length */
  267. i2c_cmd |= OWL_I2C_CMD_AS(1);
  268. msg = &msgs[0];
  269. }
  270. i2c_dev->msg = msg;
  271. i2c_dev->msg_ptr = 0;
  272. /* Set data count for the message */
  273. writel(msg->len, i2c_dev->base + OWL_I2C_REG_DATCNT);
  274. addr = i2c_8bit_addr_from_msg(msg);
  275. writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT);
  276. if (!(msg->flags & I2C_M_RD)) {
  277. /* Write data to FIFO */
  278. for (idx = 0; idx < msg->len; idx++) {
  279. /* Check for FIFO full */
  280. if (readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
  281. OWL_I2C_FIFOSTAT_TFF)
  282. break;
  283. writel(msg->buf[idx],
  284. i2c_dev->base + OWL_I2C_REG_TXDAT);
  285. }
  286. i2c_dev->msg_ptr = idx;
  287. }
  288. /* Ignore the NACK if needed */
  289. if (msg->flags & I2C_M_IGNORE_NAK)
  290. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
  291. OWL_I2C_FIFOCTL_NIB, true);
  292. else
  293. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
  294. OWL_I2C_FIFOCTL_NIB, false);
  295. /* Start the transfer */
  296. writel(i2c_cmd, i2c_dev->base + OWL_I2C_REG_CMD);
  297. spin_unlock_irqrestore(&i2c_dev->lock, flags);
  298. time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
  299. adap->timeout);
  300. spin_lock_irqsave(&i2c_dev->lock, flags);
  301. if (time_left == 0) {
  302. dev_err(&adap->dev, "Transaction timed out\n");
  303. /* Send stop condition and release the bus */
  304. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
  305. OWL_I2C_CTL_GBCC_STOP | OWL_I2C_CTL_RB,
  306. true);
  307. ret = -ETIMEDOUT;
  308. goto err_exit;
  309. }
  310. ret = i2c_dev->err < 0 ? i2c_dev->err : num;
  311. err_exit:
  312. spin_unlock_irqrestore(&i2c_dev->lock, flags);
  313. unlocked_err_exit:
  314. /* Disable I2C controller */
  315. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
  316. OWL_I2C_CTL_EN, false);
  317. return ret;
  318. }
  319. static const struct i2c_algorithm owl_i2c_algorithm = {
  320. .master_xfer = owl_i2c_master_xfer,
  321. .functionality = owl_i2c_func,
  322. };
  323. static const struct i2c_adapter_quirks owl_i2c_quirks = {
  324. .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST,
  325. .max_read_len = 240,
  326. .max_write_len = 240,
  327. .max_comb_1st_msg_len = 6,
  328. .max_comb_2nd_msg_len = 240,
  329. };
  330. static int owl_i2c_probe(struct platform_device *pdev)
  331. {
  332. struct device *dev = &pdev->dev;
  333. struct owl_i2c_dev *i2c_dev;
  334. struct resource *res;
  335. int ret, irq;
  336. i2c_dev = devm_kzalloc(dev, sizeof(*i2c_dev), GFP_KERNEL);
  337. if (!i2c_dev)
  338. return -ENOMEM;
  339. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  340. i2c_dev->base = devm_ioremap_resource(dev, res);
  341. if (IS_ERR(i2c_dev->base))
  342. return PTR_ERR(i2c_dev->base);
  343. irq = platform_get_irq(pdev, 0);
  344. if (irq < 0) {
  345. dev_err(dev, "failed to get IRQ number\n");
  346. return irq;
  347. }
  348. if (of_property_read_u32(dev->of_node, "clock-frequency",
  349. &i2c_dev->bus_freq))
  350. i2c_dev->bus_freq = OWL_I2C_DEF_SPEED_HZ;
  351. /* We support only frequencies of 100k and 400k for now */
  352. if (i2c_dev->bus_freq != OWL_I2C_DEF_SPEED_HZ &&
  353. i2c_dev->bus_freq != OWL_I2C_MAX_SPEED_HZ) {
  354. dev_err(dev, "invalid clock-frequency %d\n", i2c_dev->bus_freq);
  355. return -EINVAL;
  356. }
  357. i2c_dev->clk = devm_clk_get(dev, NULL);
  358. if (IS_ERR(i2c_dev->clk)) {
  359. dev_err(dev, "failed to get clock\n");
  360. return PTR_ERR(i2c_dev->clk);
  361. }
  362. ret = clk_prepare_enable(i2c_dev->clk);
  363. if (ret)
  364. return ret;
  365. i2c_dev->clk_rate = clk_get_rate(i2c_dev->clk);
  366. if (!i2c_dev->clk_rate) {
  367. dev_err(dev, "input clock rate should not be zero\n");
  368. ret = -EINVAL;
  369. goto disable_clk;
  370. }
  371. init_completion(&i2c_dev->msg_complete);
  372. spin_lock_init(&i2c_dev->lock);
  373. i2c_dev->adap.owner = THIS_MODULE;
  374. i2c_dev->adap.algo = &owl_i2c_algorithm;
  375. i2c_dev->adap.timeout = OWL_I2C_TIMEOUT;
  376. i2c_dev->adap.quirks = &owl_i2c_quirks;
  377. i2c_dev->adap.dev.parent = dev;
  378. i2c_dev->adap.dev.of_node = dev->of_node;
  379. snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name),
  380. "%s", "OWL I2C adapter");
  381. i2c_set_adapdata(&i2c_dev->adap, i2c_dev);
  382. platform_set_drvdata(pdev, i2c_dev);
  383. ret = devm_request_irq(dev, irq, owl_i2c_interrupt, 0, pdev->name,
  384. i2c_dev);
  385. if (ret) {
  386. dev_err(dev, "failed to request irq %d\n", irq);
  387. goto disable_clk;
  388. }
  389. return i2c_add_adapter(&i2c_dev->adap);
  390. disable_clk:
  391. clk_disable_unprepare(i2c_dev->clk);
  392. return ret;
  393. }
  394. static const struct of_device_id owl_i2c_of_match[] = {
  395. { .compatible = "actions,s900-i2c" },
  396. { /* sentinel */ }
  397. };
  398. MODULE_DEVICE_TABLE(of, owl_i2c_of_match);
  399. static struct platform_driver owl_i2c_driver = {
  400. .probe = owl_i2c_probe,
  401. .driver = {
  402. .name = "owl-i2c",
  403. .of_match_table = of_match_ptr(owl_i2c_of_match),
  404. },
  405. };
  406. module_platform_driver(owl_i2c_driver);
  407. MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>");
  408. MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
  409. MODULE_DESCRIPTION("Actions Semiconductor Owl SoC's I2C driver");
  410. MODULE_LICENSE("GPL");