i2c-bcm2835.c 11 KB

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