i2c-bcm2835.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * BCM2835 master mode driver
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/completion.h>
  15. #include <linux/err.h>
  16. #include <linux/i2c.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #define BCM2835_I2C_C 0x0
  23. #define BCM2835_I2C_S 0x4
  24. #define BCM2835_I2C_DLEN 0x8
  25. #define BCM2835_I2C_A 0xc
  26. #define BCM2835_I2C_FIFO 0x10
  27. #define BCM2835_I2C_DIV 0x14
  28. #define BCM2835_I2C_DEL 0x18
  29. #define BCM2835_I2C_CLKT 0x1c
  30. #define BCM2835_I2C_C_READ BIT(0)
  31. #define BCM2835_I2C_C_CLEAR BIT(4) /* bits 4 and 5 both clear */
  32. #define BCM2835_I2C_C_ST BIT(7)
  33. #define BCM2835_I2C_C_INTD BIT(8)
  34. #define BCM2835_I2C_C_INTT BIT(9)
  35. #define BCM2835_I2C_C_INTR BIT(10)
  36. #define BCM2835_I2C_C_I2CEN BIT(15)
  37. #define BCM2835_I2C_S_TA BIT(0)
  38. #define BCM2835_I2C_S_DONE BIT(1)
  39. #define BCM2835_I2C_S_TXW BIT(2)
  40. #define BCM2835_I2C_S_RXR BIT(3)
  41. #define BCM2835_I2C_S_TXD BIT(4)
  42. #define BCM2835_I2C_S_RXD BIT(5)
  43. #define BCM2835_I2C_S_TXE BIT(6)
  44. #define BCM2835_I2C_S_RXF BIT(7)
  45. #define BCM2835_I2C_S_ERR BIT(8)
  46. #define BCM2835_I2C_S_CLKT BIT(9)
  47. #define BCM2835_I2C_S_LEN BIT(10) /* Fake bit for SW error reporting */
  48. #define BCM2835_I2C_BITMSK_S 0x03FF
  49. #define BCM2835_I2C_CDIV_MIN 0x0002
  50. #define BCM2835_I2C_CDIV_MAX 0xFFFE
  51. #define BCM2835_I2C_TIMEOUT (msecs_to_jiffies(1000))
  52. struct bcm2835_i2c_dev {
  53. struct device *dev;
  54. void __iomem *regs;
  55. struct clk *clk;
  56. int irq;
  57. struct i2c_adapter adapter;
  58. struct completion completion;
  59. struct i2c_msg *curr_msg;
  60. u32 msg_err;
  61. u8 *msg_buf;
  62. size_t msg_buf_remaining;
  63. };
  64. static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
  65. u32 reg, u32 val)
  66. {
  67. writel(val, i2c_dev->regs + reg);
  68. }
  69. static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
  70. {
  71. return readl(i2c_dev->regs + reg);
  72. }
  73. static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
  74. {
  75. u32 val;
  76. while (i2c_dev->msg_buf_remaining) {
  77. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  78. if (!(val & BCM2835_I2C_S_TXD))
  79. break;
  80. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
  81. *i2c_dev->msg_buf);
  82. i2c_dev->msg_buf++;
  83. i2c_dev->msg_buf_remaining--;
  84. }
  85. }
  86. static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
  87. {
  88. u32 val;
  89. while (i2c_dev->msg_buf_remaining) {
  90. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  91. if (!(val & BCM2835_I2C_S_RXD))
  92. break;
  93. *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
  94. BCM2835_I2C_FIFO);
  95. i2c_dev->msg_buf++;
  96. i2c_dev->msg_buf_remaining--;
  97. }
  98. }
  99. static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
  100. {
  101. struct bcm2835_i2c_dev *i2c_dev = data;
  102. u32 val, err;
  103. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  104. val &= BCM2835_I2C_BITMSK_S;
  105. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, val);
  106. err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
  107. if (err) {
  108. i2c_dev->msg_err = err;
  109. complete(&i2c_dev->completion);
  110. return IRQ_HANDLED;
  111. }
  112. if (val & BCM2835_I2C_S_DONE) {
  113. if (i2c_dev->curr_msg->flags & I2C_M_RD) {
  114. bcm2835_drain_rxfifo(i2c_dev);
  115. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  116. }
  117. if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
  118. i2c_dev->msg_err = BCM2835_I2C_S_LEN;
  119. else
  120. i2c_dev->msg_err = 0;
  121. complete(&i2c_dev->completion);
  122. return IRQ_HANDLED;
  123. }
  124. if (val & BCM2835_I2C_S_TXW) {
  125. bcm2835_fill_txfifo(i2c_dev);
  126. return IRQ_HANDLED;
  127. }
  128. if (val & BCM2835_I2C_S_RXR) {
  129. bcm2835_drain_rxfifo(i2c_dev);
  130. return IRQ_HANDLED;
  131. }
  132. return IRQ_NONE;
  133. }
  134. static int bcm2835_i2c_xfer_msg(struct bcm2835_i2c_dev *i2c_dev,
  135. struct i2c_msg *msg)
  136. {
  137. u32 c;
  138. unsigned long time_left;
  139. i2c_dev->curr_msg = msg;
  140. i2c_dev->msg_buf = msg->buf;
  141. i2c_dev->msg_buf_remaining = msg->len;
  142. reinit_completion(&i2c_dev->completion);
  143. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
  144. if (msg->flags & I2C_M_RD) {
  145. c = BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
  146. } else {
  147. c = BCM2835_I2C_C_INTT;
  148. bcm2835_fill_txfifo(i2c_dev);
  149. }
  150. c |= BCM2835_I2C_C_ST | BCM2835_I2C_C_INTD | BCM2835_I2C_C_I2CEN;
  151. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
  152. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
  153. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
  154. time_left = wait_for_completion_timeout(&i2c_dev->completion,
  155. BCM2835_I2C_TIMEOUT);
  156. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
  157. if (!time_left) {
  158. dev_err(i2c_dev->dev, "i2c transfer timed out\n");
  159. return -ETIMEDOUT;
  160. }
  161. if (likely(!i2c_dev->msg_err))
  162. return 0;
  163. if ((i2c_dev->msg_err & BCM2835_I2C_S_ERR) &&
  164. (msg->flags & I2C_M_IGNORE_NAK))
  165. return 0;
  166. dev_err(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
  167. if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
  168. return -EREMOTEIO;
  169. else
  170. return -EIO;
  171. }
  172. static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  173. int num)
  174. {
  175. struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  176. int i;
  177. int ret = 0;
  178. for (i = 0; i < num; i++) {
  179. ret = bcm2835_i2c_xfer_msg(i2c_dev, &msgs[i]);
  180. if (ret)
  181. break;
  182. }
  183. return ret ?: i;
  184. }
  185. static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
  186. {
  187. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  188. }
  189. static const struct i2c_algorithm bcm2835_i2c_algo = {
  190. .master_xfer = bcm2835_i2c_xfer,
  191. .functionality = bcm2835_i2c_func,
  192. };
  193. /*
  194. * This HW was reported to have problems with clock stretching:
  195. * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
  196. * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
  197. */
  198. static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
  199. .flags = I2C_AQ_NO_CLK_STRETCH,
  200. };
  201. static int bcm2835_i2c_probe(struct platform_device *pdev)
  202. {
  203. struct bcm2835_i2c_dev *i2c_dev;
  204. struct resource *mem, *irq;
  205. u32 bus_clk_rate, divider;
  206. int ret;
  207. struct i2c_adapter *adap;
  208. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  209. if (!i2c_dev)
  210. return -ENOMEM;
  211. platform_set_drvdata(pdev, i2c_dev);
  212. i2c_dev->dev = &pdev->dev;
  213. init_completion(&i2c_dev->completion);
  214. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  215. i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
  216. if (IS_ERR(i2c_dev->regs))
  217. return PTR_ERR(i2c_dev->regs);
  218. i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
  219. if (IS_ERR(i2c_dev->clk)) {
  220. if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER)
  221. dev_err(&pdev->dev, "Could not get clock\n");
  222. return PTR_ERR(i2c_dev->clk);
  223. }
  224. ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  225. &bus_clk_rate);
  226. if (ret < 0) {
  227. dev_warn(&pdev->dev,
  228. "Could not read clock-frequency property\n");
  229. bus_clk_rate = 100000;
  230. }
  231. divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate);
  232. /*
  233. * Per the datasheet, the register is always interpreted as an even
  234. * number, by rounding down. In other words, the LSB is ignored. So,
  235. * if the LSB is set, increment the divider to avoid any issue.
  236. */
  237. if (divider & 1)
  238. divider++;
  239. if ((divider < BCM2835_I2C_CDIV_MIN) ||
  240. (divider > BCM2835_I2C_CDIV_MAX)) {
  241. dev_err(&pdev->dev, "Invalid clock-frequency\n");
  242. return -ENODEV;
  243. }
  244. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
  245. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  246. if (!irq) {
  247. dev_err(&pdev->dev, "No IRQ resource\n");
  248. return -ENODEV;
  249. }
  250. i2c_dev->irq = irq->start;
  251. ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
  252. dev_name(&pdev->dev), i2c_dev);
  253. if (ret) {
  254. dev_err(&pdev->dev, "Could not request IRQ\n");
  255. return -ENODEV;
  256. }
  257. adap = &i2c_dev->adapter;
  258. i2c_set_adapdata(adap, i2c_dev);
  259. adap->owner = THIS_MODULE;
  260. adap->class = I2C_CLASS_DEPRECATED;
  261. strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
  262. adap->algo = &bcm2835_i2c_algo;
  263. adap->dev.parent = &pdev->dev;
  264. adap->dev.of_node = pdev->dev.of_node;
  265. adap->quirks = &bcm2835_i2c_quirks;
  266. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
  267. ret = i2c_add_adapter(adap);
  268. if (ret)
  269. free_irq(i2c_dev->irq, i2c_dev);
  270. return ret;
  271. }
  272. static int bcm2835_i2c_remove(struct platform_device *pdev)
  273. {
  274. struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  275. free_irq(i2c_dev->irq, i2c_dev);
  276. i2c_del_adapter(&i2c_dev->adapter);
  277. return 0;
  278. }
  279. static const struct of_device_id bcm2835_i2c_of_match[] = {
  280. { .compatible = "brcm,bcm2835-i2c" },
  281. {},
  282. };
  283. MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
  284. static struct platform_driver bcm2835_i2c_driver = {
  285. .probe = bcm2835_i2c_probe,
  286. .remove = bcm2835_i2c_remove,
  287. .driver = {
  288. .name = "i2c-bcm2835",
  289. .of_match_table = bcm2835_i2c_of_match,
  290. },
  291. };
  292. module_platform_driver(bcm2835_i2c_driver);
  293. MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
  294. MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
  295. MODULE_LICENSE("GPL v2");
  296. MODULE_ALIAS("platform:i2c-bcm2835");