i2c-xlp9xx.c 12 KB

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