i2c-owl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. goto stop;
  152. }
  153. /* Handle bus error */
  154. stat = readl(i2c_dev->base + OWL_I2C_REG_STAT);
  155. if (stat & OWL_I2C_STAT_BEB) {
  156. i2c_dev->err = -EIO;
  157. goto stop;
  158. }
  159. /* Handle FIFO read */
  160. if (msg->flags & I2C_M_RD) {
  161. while ((readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
  162. OWL_I2C_FIFOSTAT_RFE) && i2c_dev->msg_ptr < msg->len) {
  163. msg->buf[i2c_dev->msg_ptr++] = readl(i2c_dev->base +
  164. OWL_I2C_REG_RXDAT);
  165. }
  166. } else {
  167. /* Handle the remaining bytes which were not sent */
  168. while (!(readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
  169. OWL_I2C_FIFOSTAT_TFF) && i2c_dev->msg_ptr < msg->len) {
  170. writel(msg->buf[i2c_dev->msg_ptr++],
  171. i2c_dev->base + OWL_I2C_REG_TXDAT);
  172. }
  173. }
  174. stop:
  175. /* Clear pending interrupts */
  176. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT,
  177. OWL_I2C_STAT_IRQP, true);
  178. complete_all(&i2c_dev->msg_complete);
  179. spin_unlock_irqrestore(&i2c_dev->lock, flags);
  180. return IRQ_HANDLED;
  181. }
  182. static u32 owl_i2c_func(struct i2c_adapter *adap)
  183. {
  184. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  185. }
  186. static int owl_i2c_check_bus_busy(struct i2c_adapter *adap)
  187. {
  188. struct owl_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  189. unsigned long timeout;
  190. /* Check for Bus busy */
  191. timeout = jiffies + OWL_I2C_TIMEOUT;
  192. while (readl(i2c_dev->base + OWL_I2C_REG_STAT) & OWL_I2C_STAT_BBB) {
  193. if (time_after(jiffies, timeout)) {
  194. dev_err(&adap->dev, "Bus busy timeout\n");
  195. return -ETIMEDOUT;
  196. }
  197. }
  198. return 0;
  199. }
  200. static int owl_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  201. int num)
  202. {
  203. struct owl_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  204. struct i2c_msg *msg;
  205. unsigned long time_left, flags;
  206. unsigned int i2c_cmd, val;
  207. unsigned int addr;
  208. int ret, idx;
  209. spin_lock_irqsave(&i2c_dev->lock, flags);
  210. /* Reset I2C controller */
  211. owl_i2c_reset(i2c_dev);
  212. /* Set bus frequency */
  213. owl_i2c_set_freq(i2c_dev);
  214. /*
  215. * Spinlock should be released before calling reset FIFO and
  216. * bus busy check since those functions may sleep
  217. */
  218. spin_unlock_irqrestore(&i2c_dev->lock, flags);
  219. /* Reset FIFO */
  220. ret = owl_i2c_reset_fifo(i2c_dev);
  221. if (ret)
  222. goto unlocked_err_exit;
  223. /* Check for bus busy */
  224. ret = owl_i2c_check_bus_busy(adap);
  225. if (ret)
  226. goto unlocked_err_exit;
  227. spin_lock_irqsave(&i2c_dev->lock, flags);
  228. /* Check for Arbitration lost */
  229. val = readl(i2c_dev->base + OWL_I2C_REG_STAT);
  230. if (val & OWL_I2C_STAT_LAB) {
  231. val &= ~OWL_I2C_STAT_LAB;
  232. writel(val, i2c_dev->base + OWL_I2C_REG_STAT);
  233. ret = -EAGAIN;
  234. goto err_exit;
  235. }
  236. reinit_completion(&i2c_dev->msg_complete);
  237. /* Enable I2C controller interrupt */
  238. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
  239. OWL_I2C_CTL_IRQE, true);
  240. /*
  241. * Select: FIFO enable, Master mode, Stop enable, Data count enable,
  242. * Send start bit
  243. */
  244. i2c_cmd = OWL_I2C_CMD_SECL | OWL_I2C_CMD_MSS | OWL_I2C_CMD_SE |
  245. OWL_I2C_CMD_NS | OWL_I2C_CMD_DE | OWL_I2C_CMD_SBE;
  246. /* Handle repeated start condition */
  247. if (num > 1) {
  248. /* Set internal address length and enable repeated start */
  249. i2c_cmd |= OWL_I2C_CMD_AS(msgs[0].len + 1) |
  250. OWL_I2C_CMD_SAS(1) | OWL_I2C_CMD_RBE;
  251. /* Write slave address */
  252. addr = i2c_8bit_addr_from_msg(&msgs[0]);
  253. writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT);
  254. /* Write internal register address */
  255. for (idx = 0; idx < msgs[0].len; idx++)
  256. writel(msgs[0].buf[idx],
  257. i2c_dev->base + OWL_I2C_REG_TXDAT);
  258. msg = &msgs[1];
  259. } else {
  260. /* Set address length */
  261. i2c_cmd |= OWL_I2C_CMD_AS(1);
  262. msg = &msgs[0];
  263. }
  264. i2c_dev->msg = msg;
  265. i2c_dev->msg_ptr = 0;
  266. /* Set data count for the message */
  267. writel(msg->len, i2c_dev->base + OWL_I2C_REG_DATCNT);
  268. addr = i2c_8bit_addr_from_msg(msg);
  269. writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT);
  270. if (!(msg->flags & I2C_M_RD)) {
  271. /* Write data to FIFO */
  272. for (idx = 0; idx < msg->len; idx++) {
  273. /* Check for FIFO full */
  274. if (readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
  275. OWL_I2C_FIFOSTAT_TFF)
  276. break;
  277. writel(msg->buf[idx],
  278. i2c_dev->base + OWL_I2C_REG_TXDAT);
  279. }
  280. i2c_dev->msg_ptr = idx;
  281. }
  282. /* Ignore the NACK if needed */
  283. if (msg->flags & I2C_M_IGNORE_NAK)
  284. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
  285. OWL_I2C_FIFOCTL_NIB, true);
  286. else
  287. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
  288. OWL_I2C_FIFOCTL_NIB, false);
  289. /* Start the transfer */
  290. writel(i2c_cmd, i2c_dev->base + OWL_I2C_REG_CMD);
  291. spin_unlock_irqrestore(&i2c_dev->lock, flags);
  292. time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
  293. adap->timeout);
  294. spin_lock_irqsave(&i2c_dev->lock, flags);
  295. if (time_left == 0) {
  296. dev_err(&adap->dev, "Transaction timed out\n");
  297. /* Send stop condition and release the bus */
  298. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
  299. OWL_I2C_CTL_GBCC_STOP | OWL_I2C_CTL_RB,
  300. true);
  301. ret = -ETIMEDOUT;
  302. goto err_exit;
  303. }
  304. ret = i2c_dev->err < 0 ? i2c_dev->err : num;
  305. err_exit:
  306. spin_unlock_irqrestore(&i2c_dev->lock, flags);
  307. unlocked_err_exit:
  308. /* Disable I2C controller */
  309. owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
  310. OWL_I2C_CTL_EN, false);
  311. return ret;
  312. }
  313. static const struct i2c_algorithm owl_i2c_algorithm = {
  314. .master_xfer = owl_i2c_master_xfer,
  315. .functionality = owl_i2c_func,
  316. };
  317. static const struct i2c_adapter_quirks owl_i2c_quirks = {
  318. .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST,
  319. .max_read_len = 240,
  320. .max_write_len = 240,
  321. .max_comb_1st_msg_len = 6,
  322. .max_comb_2nd_msg_len = 240,
  323. };
  324. static int owl_i2c_probe(struct platform_device *pdev)
  325. {
  326. struct device *dev = &pdev->dev;
  327. struct owl_i2c_dev *i2c_dev;
  328. struct resource *res;
  329. int ret, irq;
  330. i2c_dev = devm_kzalloc(dev, sizeof(*i2c_dev), GFP_KERNEL);
  331. if (!i2c_dev)
  332. return -ENOMEM;
  333. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  334. i2c_dev->base = devm_ioremap_resource(dev, res);
  335. if (IS_ERR(i2c_dev->base))
  336. return PTR_ERR(i2c_dev->base);
  337. irq = platform_get_irq(pdev, 0);
  338. if (irq < 0) {
  339. dev_err(dev, "failed to get IRQ number\n");
  340. return irq;
  341. }
  342. if (of_property_read_u32(dev->of_node, "clock-frequency",
  343. &i2c_dev->bus_freq))
  344. i2c_dev->bus_freq = OWL_I2C_DEF_SPEED_HZ;
  345. /* We support only frequencies of 100k and 400k for now */
  346. if (i2c_dev->bus_freq != OWL_I2C_DEF_SPEED_HZ &&
  347. i2c_dev->bus_freq != OWL_I2C_MAX_SPEED_HZ) {
  348. dev_err(dev, "invalid clock-frequency %d\n", i2c_dev->bus_freq);
  349. return -EINVAL;
  350. }
  351. i2c_dev->clk = devm_clk_get(dev, NULL);
  352. if (IS_ERR(i2c_dev->clk)) {
  353. dev_err(dev, "failed to get clock\n");
  354. return PTR_ERR(i2c_dev->clk);
  355. }
  356. ret = clk_prepare_enable(i2c_dev->clk);
  357. if (ret)
  358. return ret;
  359. i2c_dev->clk_rate = clk_get_rate(i2c_dev->clk);
  360. if (!i2c_dev->clk_rate) {
  361. dev_err(dev, "input clock rate should not be zero\n");
  362. ret = -EINVAL;
  363. goto disable_clk;
  364. }
  365. init_completion(&i2c_dev->msg_complete);
  366. spin_lock_init(&i2c_dev->lock);
  367. i2c_dev->adap.owner = THIS_MODULE;
  368. i2c_dev->adap.algo = &owl_i2c_algorithm;
  369. i2c_dev->adap.timeout = OWL_I2C_TIMEOUT;
  370. i2c_dev->adap.quirks = &owl_i2c_quirks;
  371. i2c_dev->adap.dev.parent = dev;
  372. i2c_dev->adap.dev.of_node = dev->of_node;
  373. snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name),
  374. "%s", "OWL I2C adapter");
  375. i2c_set_adapdata(&i2c_dev->adap, i2c_dev);
  376. platform_set_drvdata(pdev, i2c_dev);
  377. ret = devm_request_irq(dev, irq, owl_i2c_interrupt, 0, pdev->name,
  378. i2c_dev);
  379. if (ret) {
  380. dev_err(dev, "failed to request irq %d\n", irq);
  381. goto disable_clk;
  382. }
  383. return i2c_add_adapter(&i2c_dev->adap);
  384. disable_clk:
  385. clk_disable_unprepare(i2c_dev->clk);
  386. return ret;
  387. }
  388. static const struct of_device_id owl_i2c_of_match[] = {
  389. { .compatible = "actions,s900-i2c" },
  390. { /* sentinel */ }
  391. };
  392. MODULE_DEVICE_TABLE(of, owl_i2c_of_match);
  393. static struct platform_driver owl_i2c_driver = {
  394. .probe = owl_i2c_probe,
  395. .driver = {
  396. .name = "owl-i2c",
  397. .of_match_table = of_match_ptr(owl_i2c_of_match),
  398. },
  399. };
  400. module_platform_driver(owl_i2c_driver);
  401. MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>");
  402. MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
  403. MODULE_DESCRIPTION("Actions Semiconductor Owl SoC's I2C driver");
  404. MODULE_LICENSE("GPL");