i2c-bcm2835.c 11 KB

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