i2c-bcm2835.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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->flags & I2C_M_RD) {
  171. bcm2835_drain_rxfifo(i2c_dev);
  172. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  173. }
  174. if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
  175. i2c_dev->msg_err = BCM2835_I2C_S_LEN;
  176. else
  177. i2c_dev->msg_err = 0;
  178. goto complete;
  179. }
  180. if (val & BCM2835_I2C_S_TXW) {
  181. if (!i2c_dev->msg_buf_remaining) {
  182. i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
  183. goto complete;
  184. }
  185. bcm2835_fill_txfifo(i2c_dev);
  186. if (i2c_dev->num_msgs && !i2c_dev->msg_buf_remaining) {
  187. i2c_dev->curr_msg++;
  188. bcm2835_i2c_start_transfer(i2c_dev);
  189. }
  190. return IRQ_HANDLED;
  191. }
  192. if (val & BCM2835_I2C_S_RXR) {
  193. if (!i2c_dev->msg_buf_remaining) {
  194. i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
  195. goto complete;
  196. }
  197. bcm2835_drain_rxfifo(i2c_dev);
  198. return IRQ_HANDLED;
  199. }
  200. return IRQ_NONE;
  201. complete:
  202. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
  203. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, BCM2835_I2C_S_CLKT |
  204. BCM2835_I2C_S_ERR | BCM2835_I2C_S_DONE);
  205. complete(&i2c_dev->completion);
  206. return IRQ_HANDLED;
  207. }
  208. static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  209. int num)
  210. {
  211. struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  212. unsigned long time_left;
  213. int i, ret;
  214. for (i = 0; i < (num - 1); i++)
  215. if (msgs[i].flags & I2C_M_RD) {
  216. dev_warn_once(i2c_dev->dev,
  217. "only one read message supported, has to be last\n");
  218. return -EOPNOTSUPP;
  219. }
  220. ret = bcm2835_i2c_set_divider(i2c_dev);
  221. if (ret)
  222. return ret;
  223. i2c_dev->curr_msg = msgs;
  224. i2c_dev->num_msgs = num;
  225. reinit_completion(&i2c_dev->completion);
  226. bcm2835_i2c_start_transfer(i2c_dev);
  227. time_left = wait_for_completion_timeout(&i2c_dev->completion,
  228. adap->timeout);
  229. if (!time_left) {
  230. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C,
  231. BCM2835_I2C_C_CLEAR);
  232. dev_err(i2c_dev->dev, "i2c transfer timed out\n");
  233. return -ETIMEDOUT;
  234. }
  235. if (!i2c_dev->msg_err)
  236. return num;
  237. dev_dbg(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
  238. if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
  239. return -EREMOTEIO;
  240. return -EIO;
  241. }
  242. static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
  243. {
  244. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  245. }
  246. static const struct i2c_algorithm bcm2835_i2c_algo = {
  247. .master_xfer = bcm2835_i2c_xfer,
  248. .functionality = bcm2835_i2c_func,
  249. };
  250. /*
  251. * This HW was reported to have problems with clock stretching:
  252. * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
  253. * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
  254. */
  255. static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
  256. .flags = I2C_AQ_NO_CLK_STRETCH,
  257. };
  258. static int bcm2835_i2c_probe(struct platform_device *pdev)
  259. {
  260. struct bcm2835_i2c_dev *i2c_dev;
  261. struct resource *mem, *irq;
  262. int ret;
  263. struct i2c_adapter *adap;
  264. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  265. if (!i2c_dev)
  266. return -ENOMEM;
  267. platform_set_drvdata(pdev, i2c_dev);
  268. i2c_dev->dev = &pdev->dev;
  269. init_completion(&i2c_dev->completion);
  270. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  271. i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
  272. if (IS_ERR(i2c_dev->regs))
  273. return PTR_ERR(i2c_dev->regs);
  274. i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
  275. if (IS_ERR(i2c_dev->clk)) {
  276. if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER)
  277. dev_err(&pdev->dev, "Could not get clock\n");
  278. return PTR_ERR(i2c_dev->clk);
  279. }
  280. ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  281. &i2c_dev->bus_clk_rate);
  282. if (ret < 0) {
  283. dev_warn(&pdev->dev,
  284. "Could not read clock-frequency property\n");
  285. i2c_dev->bus_clk_rate = 100000;
  286. }
  287. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  288. if (!irq) {
  289. dev_err(&pdev->dev, "No IRQ resource\n");
  290. return -ENODEV;
  291. }
  292. i2c_dev->irq = irq->start;
  293. ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
  294. dev_name(&pdev->dev), i2c_dev);
  295. if (ret) {
  296. dev_err(&pdev->dev, "Could not request IRQ\n");
  297. return -ENODEV;
  298. }
  299. adap = &i2c_dev->adapter;
  300. i2c_set_adapdata(adap, i2c_dev);
  301. adap->owner = THIS_MODULE;
  302. adap->class = I2C_CLASS_DEPRECATED;
  303. strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
  304. adap->algo = &bcm2835_i2c_algo;
  305. adap->dev.parent = &pdev->dev;
  306. adap->dev.of_node = pdev->dev.of_node;
  307. adap->quirks = &bcm2835_i2c_quirks;
  308. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
  309. ret = i2c_add_adapter(adap);
  310. if (ret)
  311. free_irq(i2c_dev->irq, i2c_dev);
  312. return ret;
  313. }
  314. static int bcm2835_i2c_remove(struct platform_device *pdev)
  315. {
  316. struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  317. free_irq(i2c_dev->irq, i2c_dev);
  318. i2c_del_adapter(&i2c_dev->adapter);
  319. return 0;
  320. }
  321. static const struct of_device_id bcm2835_i2c_of_match[] = {
  322. { .compatible = "brcm,bcm2835-i2c" },
  323. {},
  324. };
  325. MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
  326. static struct platform_driver bcm2835_i2c_driver = {
  327. .probe = bcm2835_i2c_probe,
  328. .remove = bcm2835_i2c_remove,
  329. .driver = {
  330. .name = "i2c-bcm2835",
  331. .of_match_table = bcm2835_i2c_of_match,
  332. },
  333. };
  334. module_platform_driver(bcm2835_i2c_driver);
  335. MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
  336. MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
  337. MODULE_LICENSE("GPL v2");
  338. MODULE_ALIAS("platform:i2c-bcm2835");