i2c-xlp9xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright (c) 2003-2015 Broadcom Corporation
  3. *
  4. * This file is licensed under the terms of the GNU General Public
  5. * License version 2. This program is licensed "as is" without any
  6. * warranty of any kind, whether express or implied.
  7. */
  8. #include <linux/completion.h>
  9. #include <linux/i2c.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #define XLP9XX_I2C_DIV 0x0
  17. #define XLP9XX_I2C_CTRL 0x1
  18. #define XLP9XX_I2C_CMD 0x2
  19. #define XLP9XX_I2C_STATUS 0x3
  20. #define XLP9XX_I2C_MTXFIFO 0x4
  21. #define XLP9XX_I2C_MRXFIFO 0x5
  22. #define XLP9XX_I2C_MFIFOCTRL 0x6
  23. #define XLP9XX_I2C_STXFIFO 0x7
  24. #define XLP9XX_I2C_SRXFIFO 0x8
  25. #define XLP9XX_I2C_SFIFOCTRL 0x9
  26. #define XLP9XX_I2C_SLAVEADDR 0xA
  27. #define XLP9XX_I2C_OWNADDR 0xB
  28. #define XLP9XX_I2C_FIFOWCNT 0xC
  29. #define XLP9XX_I2C_INTEN 0xD
  30. #define XLP9XX_I2C_INTST 0xE
  31. #define XLP9XX_I2C_WAITCNT 0xF
  32. #define XLP9XX_I2C_TIMEOUT 0X10
  33. #define XLP9XX_I2C_GENCALLADDR 0x11
  34. #define XLP9XX_I2C_CMD_START BIT(7)
  35. #define XLP9XX_I2C_CMD_STOP BIT(6)
  36. #define XLP9XX_I2C_CMD_READ BIT(5)
  37. #define XLP9XX_I2C_CMD_WRITE BIT(4)
  38. #define XLP9XX_I2C_CMD_ACK BIT(3)
  39. #define XLP9XX_I2C_CTRL_MCTLEN_SHIFT 16
  40. #define XLP9XX_I2C_CTRL_MCTLEN_MASK 0xffff0000
  41. #define XLP9XX_I2C_CTRL_RST BIT(8)
  42. #define XLP9XX_I2C_CTRL_EN BIT(6)
  43. #define XLP9XX_I2C_CTRL_MASTER BIT(4)
  44. #define XLP9XX_I2C_CTRL_FIFORD BIT(1)
  45. #define XLP9XX_I2C_CTRL_ADDMODE BIT(0)
  46. #define XLP9XX_I2C_INTEN_NACKADDR BIT(25)
  47. #define XLP9XX_I2C_INTEN_SADDR BIT(13)
  48. #define XLP9XX_I2C_INTEN_DATADONE BIT(12)
  49. #define XLP9XX_I2C_INTEN_ARLOST BIT(11)
  50. #define XLP9XX_I2C_INTEN_MFIFOFULL BIT(4)
  51. #define XLP9XX_I2C_INTEN_MFIFOEMTY BIT(3)
  52. #define XLP9XX_I2C_INTEN_MFIFOHI BIT(2)
  53. #define XLP9XX_I2C_INTEN_BUSERR BIT(0)
  54. #define XLP9XX_I2C_MFIFOCTRL_HITH_SHIFT 8
  55. #define XLP9XX_I2C_MFIFOCTRL_LOTH_SHIFT 0
  56. #define XLP9XX_I2C_MFIFOCTRL_RST BIT(16)
  57. #define XLP9XX_I2C_SLAVEADDR_RW BIT(0)
  58. #define XLP9XX_I2C_SLAVEADDR_ADDR_SHIFT 1
  59. #define XLP9XX_I2C_IP_CLK_FREQ 133000000UL
  60. #define XLP9XX_I2C_DEFAULT_FREQ 100000
  61. #define XLP9XX_I2C_HIGH_FREQ 400000
  62. #define XLP9XX_I2C_FIFO_SIZE 0x80U
  63. #define XLP9XX_I2C_TIMEOUT_MS 1000
  64. #define XLP9XX_I2C_FIFO_WCNT_MASK 0xff
  65. #define XLP9XX_I2C_STATUS_ERRMASK (XLP9XX_I2C_INTEN_ARLOST | \
  66. XLP9XX_I2C_INTEN_NACKADDR | XLP9XX_I2C_INTEN_BUSERR)
  67. struct xlp9xx_i2c_dev {
  68. struct device *dev;
  69. struct i2c_adapter adapter;
  70. struct completion msg_complete;
  71. int irq;
  72. bool msg_read;
  73. u32 __iomem *base;
  74. u32 msg_buf_remaining;
  75. u32 msg_len;
  76. u32 clk_hz;
  77. u32 msg_err;
  78. u8 *msg_buf;
  79. };
  80. static inline void xlp9xx_write_i2c_reg(struct xlp9xx_i2c_dev *priv,
  81. unsigned long reg, u32 val)
  82. {
  83. writel(val, priv->base + reg);
  84. }
  85. static inline u32 xlp9xx_read_i2c_reg(struct xlp9xx_i2c_dev *priv,
  86. unsigned long reg)
  87. {
  88. return readl(priv->base + reg);
  89. }
  90. static void xlp9xx_i2c_mask_irq(struct xlp9xx_i2c_dev *priv, u32 mask)
  91. {
  92. u32 inten;
  93. inten = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_INTEN) & ~mask;
  94. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, inten);
  95. }
  96. static void xlp9xx_i2c_unmask_irq(struct xlp9xx_i2c_dev *priv, u32 mask)
  97. {
  98. u32 inten;
  99. inten = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_INTEN) | mask;
  100. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, inten);
  101. }
  102. static void xlp9xx_i2c_update_rx_fifo_thres(struct xlp9xx_i2c_dev *priv)
  103. {
  104. u32 thres;
  105. thres = min(priv->msg_buf_remaining, XLP9XX_I2C_FIFO_SIZE);
  106. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_MFIFOCTRL,
  107. thres << XLP9XX_I2C_MFIFOCTRL_HITH_SHIFT);
  108. }
  109. static void xlp9xx_i2c_fill_tx_fifo(struct xlp9xx_i2c_dev *priv)
  110. {
  111. u32 len, i;
  112. u8 *buf = priv->msg_buf;
  113. len = min(priv->msg_buf_remaining, XLP9XX_I2C_FIFO_SIZE);
  114. for (i = 0; i < len; i++)
  115. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_MTXFIFO, buf[i]);
  116. priv->msg_buf_remaining -= len;
  117. priv->msg_buf += len;
  118. }
  119. static void xlp9xx_i2c_drain_rx_fifo(struct xlp9xx_i2c_dev *priv)
  120. {
  121. u32 len, i;
  122. u8 *buf = priv->msg_buf;
  123. len = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_FIFOWCNT) &
  124. XLP9XX_I2C_FIFO_WCNT_MASK;
  125. len = min(priv->msg_buf_remaining, len);
  126. for (i = 0; i < len; i++, buf++)
  127. *buf = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_MRXFIFO);
  128. priv->msg_buf_remaining -= len;
  129. priv->msg_buf = buf;
  130. if (priv->msg_buf_remaining)
  131. xlp9xx_i2c_update_rx_fifo_thres(priv);
  132. }
  133. static irqreturn_t xlp9xx_i2c_isr(int irq, void *dev_id)
  134. {
  135. struct xlp9xx_i2c_dev *priv = dev_id;
  136. u32 status;
  137. status = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_INTST);
  138. if (status == 0)
  139. return IRQ_NONE;
  140. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTST, status);
  141. if (status & XLP9XX_I2C_STATUS_ERRMASK) {
  142. priv->msg_err = status;
  143. goto xfer_done;
  144. }
  145. /* SADDR ACK for SMBUS_QUICK */
  146. if ((status & XLP9XX_I2C_INTEN_SADDR) && (priv->msg_len == 0))
  147. goto xfer_done;
  148. if (!priv->msg_read) {
  149. if (status & XLP9XX_I2C_INTEN_MFIFOEMTY) {
  150. /* TX FIFO got empty, fill it up again */
  151. if (priv->msg_buf_remaining)
  152. xlp9xx_i2c_fill_tx_fifo(priv);
  153. else
  154. xlp9xx_i2c_mask_irq(priv,
  155. XLP9XX_I2C_INTEN_MFIFOEMTY);
  156. }
  157. } else {
  158. if (status & (XLP9XX_I2C_INTEN_DATADONE |
  159. XLP9XX_I2C_INTEN_MFIFOHI)) {
  160. /* data is in FIFO, read it */
  161. if (priv->msg_buf_remaining)
  162. xlp9xx_i2c_drain_rx_fifo(priv);
  163. }
  164. }
  165. /* Transfer complete */
  166. if (status & XLP9XX_I2C_INTEN_DATADONE)
  167. goto xfer_done;
  168. return IRQ_HANDLED;
  169. xfer_done:
  170. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, 0);
  171. complete(&priv->msg_complete);
  172. return IRQ_HANDLED;
  173. }
  174. static int xlp9xx_i2c_init(struct xlp9xx_i2c_dev *priv)
  175. {
  176. u32 prescale;
  177. /*
  178. * The controller uses 5 * SCL clock internally.
  179. * So prescale value should be divided by 5.
  180. */
  181. prescale = DIV_ROUND_UP(XLP9XX_I2C_IP_CLK_FREQ, priv->clk_hz);
  182. prescale = ((prescale - 8) / 5) - 1;
  183. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CTRL, XLP9XX_I2C_CTRL_RST);
  184. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CTRL, XLP9XX_I2C_CTRL_EN |
  185. XLP9XX_I2C_CTRL_MASTER);
  186. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_DIV, prescale);
  187. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, 0);
  188. return 0;
  189. }
  190. static int xlp9xx_i2c_xfer_msg(struct xlp9xx_i2c_dev *priv, struct i2c_msg *msg,
  191. int last_msg)
  192. {
  193. unsigned long timeleft;
  194. u32 intr_mask, cmd, val;
  195. priv->msg_buf = msg->buf;
  196. priv->msg_buf_remaining = priv->msg_len = msg->len;
  197. priv->msg_err = 0;
  198. priv->msg_read = (msg->flags & I2C_M_RD);
  199. reinit_completion(&priv->msg_complete);
  200. /* Reset FIFO */
  201. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_MFIFOCTRL,
  202. XLP9XX_I2C_MFIFOCTRL_RST);
  203. /* set FIFO threshold if reading */
  204. if (priv->msg_read)
  205. xlp9xx_i2c_update_rx_fifo_thres(priv);
  206. /* set slave addr */
  207. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_SLAVEADDR,
  208. (msg->addr << XLP9XX_I2C_SLAVEADDR_ADDR_SHIFT) |
  209. (priv->msg_read ? XLP9XX_I2C_SLAVEADDR_RW : 0));
  210. /* Build control word for transfer */
  211. val = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_CTRL);
  212. if (!priv->msg_read)
  213. val &= ~XLP9XX_I2C_CTRL_FIFORD;
  214. else
  215. val |= XLP9XX_I2C_CTRL_FIFORD; /* read */
  216. if (msg->flags & I2C_M_TEN)
  217. val |= XLP9XX_I2C_CTRL_ADDMODE; /* 10-bit address mode*/
  218. else
  219. val &= ~XLP9XX_I2C_CTRL_ADDMODE;
  220. /* set data length to be transferred */
  221. val = (val & ~XLP9XX_I2C_CTRL_MCTLEN_MASK) |
  222. (msg->len << XLP9XX_I2C_CTRL_MCTLEN_SHIFT);
  223. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CTRL, val);
  224. /* fill fifo during tx */
  225. if (!priv->msg_read)
  226. xlp9xx_i2c_fill_tx_fifo(priv);
  227. /* set interrupt mask */
  228. intr_mask = (XLP9XX_I2C_INTEN_ARLOST | XLP9XX_I2C_INTEN_BUSERR |
  229. XLP9XX_I2C_INTEN_NACKADDR | XLP9XX_I2C_INTEN_DATADONE);
  230. if (priv->msg_read) {
  231. intr_mask |= XLP9XX_I2C_INTEN_MFIFOHI;
  232. if (msg->len == 0)
  233. intr_mask |= XLP9XX_I2C_INTEN_SADDR;
  234. } else {
  235. if (msg->len == 0)
  236. intr_mask |= XLP9XX_I2C_INTEN_SADDR;
  237. else
  238. intr_mask |= XLP9XX_I2C_INTEN_MFIFOEMTY;
  239. }
  240. xlp9xx_i2c_unmask_irq(priv, intr_mask);
  241. /* set cmd reg */
  242. cmd = XLP9XX_I2C_CMD_START;
  243. cmd |= (priv->msg_read ? XLP9XX_I2C_CMD_READ : XLP9XX_I2C_CMD_WRITE);
  244. if (last_msg)
  245. cmd |= XLP9XX_I2C_CMD_STOP;
  246. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CMD, cmd);
  247. timeleft = msecs_to_jiffies(XLP9XX_I2C_TIMEOUT_MS);
  248. timeleft = wait_for_completion_timeout(&priv->msg_complete, timeleft);
  249. if (priv->msg_err) {
  250. dev_dbg(priv->dev, "transfer error %x!\n", priv->msg_err);
  251. if (priv->msg_err & XLP9XX_I2C_INTEN_BUSERR)
  252. xlp9xx_i2c_init(priv);
  253. return -EIO;
  254. }
  255. if (timeleft == 0) {
  256. dev_dbg(priv->dev, "i2c transfer timed out!\n");
  257. xlp9xx_i2c_init(priv);
  258. return -ETIMEDOUT;
  259. }
  260. return 0;
  261. }
  262. static int xlp9xx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  263. int num)
  264. {
  265. int i, ret;
  266. struct xlp9xx_i2c_dev *priv = i2c_get_adapdata(adap);
  267. for (i = 0; i < num; i++) {
  268. ret = xlp9xx_i2c_xfer_msg(priv, &msgs[i], i == num - 1);
  269. if (ret != 0)
  270. return ret;
  271. }
  272. return num;
  273. }
  274. static u32 xlp9xx_i2c_functionality(struct i2c_adapter *adapter)
  275. {
  276. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C |
  277. I2C_FUNC_10BIT_ADDR;
  278. }
  279. static struct i2c_algorithm xlp9xx_i2c_algo = {
  280. .master_xfer = xlp9xx_i2c_xfer,
  281. .functionality = xlp9xx_i2c_functionality,
  282. };
  283. static int xlp9xx_i2c_get_frequency(struct platform_device *pdev,
  284. struct xlp9xx_i2c_dev *priv)
  285. {
  286. struct device_node *np = pdev->dev.of_node;
  287. u32 freq;
  288. int err;
  289. err = of_property_read_u32(np, "clock-frequency", &freq);
  290. if (err) {
  291. freq = XLP9XX_I2C_DEFAULT_FREQ;
  292. dev_dbg(&pdev->dev, "using default frequency %u\n", freq);
  293. } else if (freq == 0 || freq > XLP9XX_I2C_HIGH_FREQ) {
  294. dev_warn(&pdev->dev, "invalid frequency %u, using default\n",
  295. freq);
  296. freq = XLP9XX_I2C_DEFAULT_FREQ;
  297. }
  298. priv->clk_hz = freq;
  299. return 0;
  300. }
  301. static int xlp9xx_i2c_probe(struct platform_device *pdev)
  302. {
  303. struct xlp9xx_i2c_dev *priv;
  304. struct resource *res;
  305. int err = 0;
  306. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  307. if (!priv)
  308. return -ENOMEM;
  309. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  310. priv->base = devm_ioremap_resource(&pdev->dev, res);
  311. if (IS_ERR(priv->base))
  312. return PTR_ERR(priv->base);
  313. priv->irq = platform_get_irq(pdev, 0);
  314. if (priv->irq <= 0) {
  315. dev_err(&pdev->dev, "invalid irq!\n");
  316. return priv->irq;
  317. }
  318. xlp9xx_i2c_get_frequency(pdev, priv);
  319. xlp9xx_i2c_init(priv);
  320. err = devm_request_irq(&pdev->dev, priv->irq, xlp9xx_i2c_isr, 0,
  321. pdev->name, priv);
  322. if (err) {
  323. dev_err(&pdev->dev, "IRQ request failed!\n");
  324. return err;
  325. }
  326. init_completion(&priv->msg_complete);
  327. priv->adapter.dev.parent = &pdev->dev;
  328. priv->adapter.algo = &xlp9xx_i2c_algo;
  329. priv->adapter.dev.of_node = pdev->dev.of_node;
  330. priv->dev = &pdev->dev;
  331. snprintf(priv->adapter.name, sizeof(priv->adapter.name), "xlp9xx-i2c");
  332. i2c_set_adapdata(&priv->adapter, priv);
  333. err = i2c_add_adapter(&priv->adapter);
  334. if (err) {
  335. dev_err(&pdev->dev, "failed to add I2C adapter!\n");
  336. return err;
  337. }
  338. platform_set_drvdata(pdev, priv);
  339. dev_dbg(&pdev->dev, "I2C bus:%d added\n", priv->adapter.nr);
  340. return 0;
  341. }
  342. static int xlp9xx_i2c_remove(struct platform_device *pdev)
  343. {
  344. struct xlp9xx_i2c_dev *priv;
  345. priv = platform_get_drvdata(pdev);
  346. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, 0);
  347. synchronize_irq(priv->irq);
  348. i2c_del_adapter(&priv->adapter);
  349. xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CTRL, 0);
  350. return 0;
  351. }
  352. static const struct of_device_id xlp9xx_i2c_of_match[] = {
  353. { .compatible = "netlogic,xlp980-i2c", },
  354. { /* sentinel */ },
  355. };
  356. static struct platform_driver xlp9xx_i2c_driver = {
  357. .probe = xlp9xx_i2c_probe,
  358. .remove = xlp9xx_i2c_remove,
  359. .driver = {
  360. .name = "xlp9xx-i2c",
  361. .of_match_table = xlp9xx_i2c_of_match,
  362. },
  363. };
  364. module_platform_driver(xlp9xx_i2c_driver);
  365. MODULE_AUTHOR("Subhendu Sekhar Behera <sbehera@broadcom.com>");
  366. MODULE_DESCRIPTION("XLP9XX/5XX I2C Bus Controller Driver");
  367. MODULE_LICENSE("GPL v2");