i2c-bcm2835.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_CDIV_MIN 0x0002
  49. #define BCM2835_I2C_CDIV_MAX 0xFFFE
  50. struct bcm2835_i2c_dev {
  51. struct device *dev;
  52. void __iomem *regs;
  53. struct clk *clk;
  54. int irq;
  55. u32 bus_clk_rate;
  56. struct i2c_adapter adapter;
  57. struct completion completion;
  58. struct i2c_msg *curr_msg;
  59. int num_msgs;
  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 int bcm2835_i2c_set_divider(struct bcm2835_i2c_dev *i2c_dev)
  74. {
  75. u32 divider;
  76. divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk),
  77. i2c_dev->bus_clk_rate);
  78. /*
  79. * Per the datasheet, the register is always interpreted as an even
  80. * number, by rounding down. In other words, the LSB is ignored. So,
  81. * if the LSB is set, increment the divider to avoid any issue.
  82. */
  83. if (divider & 1)
  84. divider++;
  85. if ((divider < BCM2835_I2C_CDIV_MIN) ||
  86. (divider > BCM2835_I2C_CDIV_MAX)) {
  87. dev_err_ratelimited(i2c_dev->dev, "Invalid clock-frequency\n");
  88. return -EINVAL;
  89. }
  90. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
  91. return 0;
  92. }
  93. static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
  94. {
  95. u32 val;
  96. while (i2c_dev->msg_buf_remaining) {
  97. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  98. if (!(val & BCM2835_I2C_S_TXD))
  99. break;
  100. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
  101. *i2c_dev->msg_buf);
  102. i2c_dev->msg_buf++;
  103. i2c_dev->msg_buf_remaining--;
  104. }
  105. }
  106. static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
  107. {
  108. u32 val;
  109. while (i2c_dev->msg_buf_remaining) {
  110. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  111. if (!(val & BCM2835_I2C_S_RXD))
  112. break;
  113. *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
  114. BCM2835_I2C_FIFO);
  115. i2c_dev->msg_buf++;
  116. i2c_dev->msg_buf_remaining--;
  117. }
  118. }
  119. /*
  120. * Repeated Start Condition (Sr)
  121. * The BCM2835 ARM Peripherals datasheet mentions a way to trigger a Sr when it
  122. * talks about reading from a slave with 10 bit address. This is achieved by
  123. * issuing a write, poll the I2CS.TA flag and wait for it to be set, and then
  124. * issue a read.
  125. * A comment in https://github.com/raspberrypi/linux/issues/254 shows how the
  126. * firmware actually does it using polling and says that it's a workaround for
  127. * a problem in the state machine.
  128. * It turns out that it is possible to use the TXW interrupt to know when the
  129. * transfer is active, provided the FIFO has not been prefilled.
  130. */
  131. static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev *i2c_dev)
  132. {
  133. u32 c = BCM2835_I2C_C_ST | BCM2835_I2C_C_I2CEN;
  134. struct i2c_msg *msg = i2c_dev->curr_msg;
  135. bool last_msg = (i2c_dev->num_msgs == 1);
  136. if (!i2c_dev->num_msgs)
  137. return;
  138. i2c_dev->num_msgs--;
  139. i2c_dev->msg_buf = msg->buf;
  140. i2c_dev->msg_buf_remaining = msg->len;
  141. if (msg->flags & I2C_M_RD)
  142. c |= BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
  143. else
  144. c |= BCM2835_I2C_C_INTT;
  145. if (last_msg)
  146. c |= BCM2835_I2C_C_INTD;
  147. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
  148. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
  149. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
  150. }
  151. /*
  152. * Note about I2C_C_CLEAR on error:
  153. * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in
  154. * non-idle state and I2C_C_READ, it sets an abort_rx flag and runs through
  155. * the state machine to send a NACK and a STOP. Since we're setting CLEAR
  156. * without I2CEN, that NACK will be hanging around queued up for next time
  157. * we start the engine.
  158. */
  159. static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
  160. {
  161. struct bcm2835_i2c_dev *i2c_dev = data;
  162. u32 val, err;
  163. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  164. err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
  165. if (err) {
  166. i2c_dev->msg_err = err;
  167. goto complete;
  168. }
  169. if (val & BCM2835_I2C_S_DONE) {
  170. if (!i2c_dev->curr_msg) {
  171. dev_err(i2c_dev->dev, "Got unexpected interrupt (from firmware?)\n");
  172. } else if (i2c_dev->curr_msg->flags & I2C_M_RD) {
  173. bcm2835_drain_rxfifo(i2c_dev);
  174. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  175. }
  176. if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
  177. i2c_dev->msg_err = BCM2835_I2C_S_LEN;
  178. else
  179. i2c_dev->msg_err = 0;
  180. goto complete;
  181. }
  182. if (val & BCM2835_I2C_S_TXW) {
  183. if (!i2c_dev->msg_buf_remaining) {
  184. i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
  185. goto complete;
  186. }
  187. bcm2835_fill_txfifo(i2c_dev);
  188. if (i2c_dev->num_msgs && !i2c_dev->msg_buf_remaining) {
  189. i2c_dev->curr_msg++;
  190. bcm2835_i2c_start_transfer(i2c_dev);
  191. }
  192. return IRQ_HANDLED;
  193. }
  194. if (val & BCM2835_I2C_S_RXR) {
  195. if (!i2c_dev->msg_buf_remaining) {
  196. i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
  197. goto complete;
  198. }
  199. bcm2835_drain_rxfifo(i2c_dev);
  200. return IRQ_HANDLED;
  201. }
  202. return IRQ_NONE;
  203. complete:
  204. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
  205. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, BCM2835_I2C_S_CLKT |
  206. BCM2835_I2C_S_ERR | BCM2835_I2C_S_DONE);
  207. complete(&i2c_dev->completion);
  208. return IRQ_HANDLED;
  209. }
  210. static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  211. int num)
  212. {
  213. struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  214. unsigned long time_left;
  215. int i, ret;
  216. for (i = 0; i < (num - 1); i++)
  217. if (msgs[i].flags & I2C_M_RD) {
  218. dev_warn_once(i2c_dev->dev,
  219. "only one read message supported, has to be last\n");
  220. return -EOPNOTSUPP;
  221. }
  222. ret = bcm2835_i2c_set_divider(i2c_dev);
  223. if (ret)
  224. return ret;
  225. i2c_dev->curr_msg = msgs;
  226. i2c_dev->num_msgs = num;
  227. reinit_completion(&i2c_dev->completion);
  228. bcm2835_i2c_start_transfer(i2c_dev);
  229. time_left = wait_for_completion_timeout(&i2c_dev->completion,
  230. adap->timeout);
  231. if (!time_left) {
  232. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C,
  233. BCM2835_I2C_C_CLEAR);
  234. dev_err(i2c_dev->dev, "i2c transfer timed out\n");
  235. return -ETIMEDOUT;
  236. }
  237. if (!i2c_dev->msg_err)
  238. return num;
  239. dev_dbg(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
  240. if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
  241. return -EREMOTEIO;
  242. return -EIO;
  243. }
  244. static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
  245. {
  246. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  247. }
  248. static const struct i2c_algorithm bcm2835_i2c_algo = {
  249. .master_xfer = bcm2835_i2c_xfer,
  250. .functionality = bcm2835_i2c_func,
  251. };
  252. /*
  253. * This HW was reported to have problems with clock stretching:
  254. * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
  255. * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
  256. */
  257. static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
  258. .flags = I2C_AQ_NO_CLK_STRETCH,
  259. };
  260. static int bcm2835_i2c_probe(struct platform_device *pdev)
  261. {
  262. struct bcm2835_i2c_dev *i2c_dev;
  263. struct resource *mem, *irq;
  264. int ret;
  265. struct i2c_adapter *adap;
  266. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  267. if (!i2c_dev)
  268. return -ENOMEM;
  269. platform_set_drvdata(pdev, i2c_dev);
  270. i2c_dev->dev = &pdev->dev;
  271. init_completion(&i2c_dev->completion);
  272. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  273. i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
  274. if (IS_ERR(i2c_dev->regs))
  275. return PTR_ERR(i2c_dev->regs);
  276. i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
  277. if (IS_ERR(i2c_dev->clk)) {
  278. if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER)
  279. dev_err(&pdev->dev, "Could not get clock\n");
  280. return PTR_ERR(i2c_dev->clk);
  281. }
  282. ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  283. &i2c_dev->bus_clk_rate);
  284. if (ret < 0) {
  285. dev_warn(&pdev->dev,
  286. "Could not read clock-frequency property\n");
  287. i2c_dev->bus_clk_rate = 100000;
  288. }
  289. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  290. if (!irq) {
  291. dev_err(&pdev->dev, "No IRQ resource\n");
  292. return -ENODEV;
  293. }
  294. i2c_dev->irq = irq->start;
  295. ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
  296. dev_name(&pdev->dev), i2c_dev);
  297. if (ret) {
  298. dev_err(&pdev->dev, "Could not request IRQ\n");
  299. return -ENODEV;
  300. }
  301. adap = &i2c_dev->adapter;
  302. i2c_set_adapdata(adap, i2c_dev);
  303. adap->owner = THIS_MODULE;
  304. adap->class = I2C_CLASS_DEPRECATED;
  305. strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
  306. adap->algo = &bcm2835_i2c_algo;
  307. adap->dev.parent = &pdev->dev;
  308. adap->dev.of_node = pdev->dev.of_node;
  309. adap->quirks = &bcm2835_i2c_quirks;
  310. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
  311. ret = i2c_add_adapter(adap);
  312. if (ret)
  313. free_irq(i2c_dev->irq, i2c_dev);
  314. return ret;
  315. }
  316. static int bcm2835_i2c_remove(struct platform_device *pdev)
  317. {
  318. struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  319. free_irq(i2c_dev->irq, i2c_dev);
  320. i2c_del_adapter(&i2c_dev->adapter);
  321. return 0;
  322. }
  323. static const struct of_device_id bcm2835_i2c_of_match[] = {
  324. { .compatible = "brcm,bcm2835-i2c" },
  325. {},
  326. };
  327. MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
  328. static struct platform_driver bcm2835_i2c_driver = {
  329. .probe = bcm2835_i2c_probe,
  330. .remove = bcm2835_i2c_remove,
  331. .driver = {
  332. .name = "i2c-bcm2835",
  333. .of_match_table = bcm2835_i2c_of_match,
  334. },
  335. };
  336. module_platform_driver(bcm2835_i2c_driver);
  337. MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
  338. MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
  339. MODULE_LICENSE("GPL v2");
  340. MODULE_ALIAS("platform:i2c-bcm2835");