i2c-bcm2835.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * BCM2835 master mode driver
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/clkdev.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/completion.h>
  9. #include <linux/err.h>
  10. #include <linux/i2c.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #define BCM2835_I2C_C 0x0
  18. #define BCM2835_I2C_S 0x4
  19. #define BCM2835_I2C_DLEN 0x8
  20. #define BCM2835_I2C_A 0xc
  21. #define BCM2835_I2C_FIFO 0x10
  22. #define BCM2835_I2C_DIV 0x14
  23. #define BCM2835_I2C_DEL 0x18
  24. /*
  25. * 16-bit field for the number of SCL cycles to wait after rising SCL
  26. * before deciding the slave is not responding. 0 disables the
  27. * timeout detection.
  28. */
  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. int irq;
  56. struct i2c_adapter adapter;
  57. struct completion completion;
  58. struct i2c_msg *curr_msg;
  59. struct clk *bus_clk;
  60. int num_msgs;
  61. u32 msg_err;
  62. u8 *msg_buf;
  63. size_t msg_buf_remaining;
  64. };
  65. static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
  66. u32 reg, u32 val)
  67. {
  68. writel(val, i2c_dev->regs + reg);
  69. }
  70. static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
  71. {
  72. return readl(i2c_dev->regs + reg);
  73. }
  74. #define to_clk_bcm2835_i2c(_hw) container_of(_hw, struct clk_bcm2835_i2c, hw)
  75. struct clk_bcm2835_i2c {
  76. struct clk_hw hw;
  77. struct bcm2835_i2c_dev *i2c_dev;
  78. };
  79. static int clk_bcm2835_i2c_calc_divider(unsigned long rate,
  80. unsigned long parent_rate)
  81. {
  82. u32 divider = DIV_ROUND_UP(parent_rate, rate);
  83. /*
  84. * Per the datasheet, the register is always interpreted as an even
  85. * number, by rounding down. In other words, the LSB is ignored. So,
  86. * if the LSB is set, increment the divider to avoid any issue.
  87. */
  88. if (divider & 1)
  89. divider++;
  90. if ((divider < BCM2835_I2C_CDIV_MIN) ||
  91. (divider > BCM2835_I2C_CDIV_MAX))
  92. return -EINVAL;
  93. return divider;
  94. }
  95. static int clk_bcm2835_i2c_set_rate(struct clk_hw *hw, unsigned long rate,
  96. unsigned long parent_rate)
  97. {
  98. struct clk_bcm2835_i2c *div = to_clk_bcm2835_i2c(hw);
  99. u32 redl, fedl;
  100. u32 divider = clk_bcm2835_i2c_calc_divider(rate, parent_rate);
  101. if (divider == -EINVAL)
  102. return -EINVAL;
  103. bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_DIV, divider);
  104. /*
  105. * Number of core clocks to wait after falling edge before
  106. * outputting the next data bit. Note that both FEDL and REDL
  107. * can't be greater than CDIV/2.
  108. */
  109. fedl = max(divider / 16, 1u);
  110. /*
  111. * Number of core clocks to wait after rising edge before
  112. * sampling the next incoming data bit.
  113. */
  114. redl = max(divider / 4, 1u);
  115. bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_DEL,
  116. (fedl << BCM2835_I2C_FEDL_SHIFT) |
  117. (redl << BCM2835_I2C_REDL_SHIFT));
  118. return 0;
  119. }
  120. static long clk_bcm2835_i2c_round_rate(struct clk_hw *hw, unsigned long rate,
  121. unsigned long *parent_rate)
  122. {
  123. u32 divider = clk_bcm2835_i2c_calc_divider(rate, *parent_rate);
  124. return DIV_ROUND_UP(*parent_rate, divider);
  125. }
  126. static unsigned long clk_bcm2835_i2c_recalc_rate(struct clk_hw *hw,
  127. unsigned long parent_rate)
  128. {
  129. struct clk_bcm2835_i2c *div = to_clk_bcm2835_i2c(hw);
  130. u32 divider = bcm2835_i2c_readl(div->i2c_dev, BCM2835_I2C_DIV);
  131. return DIV_ROUND_UP(parent_rate, divider);
  132. }
  133. static const struct clk_ops clk_bcm2835_i2c_ops = {
  134. .set_rate = clk_bcm2835_i2c_set_rate,
  135. .round_rate = clk_bcm2835_i2c_round_rate,
  136. .recalc_rate = clk_bcm2835_i2c_recalc_rate,
  137. };
  138. static struct clk *bcm2835_i2c_register_div(struct device *dev,
  139. struct clk *mclk,
  140. struct bcm2835_i2c_dev *i2c_dev)
  141. {
  142. struct clk_init_data init;
  143. struct clk_bcm2835_i2c *priv;
  144. char name[32];
  145. const char *mclk_name;
  146. snprintf(name, sizeof(name), "%s_div", dev_name(dev));
  147. mclk_name = __clk_get_name(mclk);
  148. init.ops = &clk_bcm2835_i2c_ops;
  149. init.name = name;
  150. init.parent_names = (const char* []) { mclk_name };
  151. init.num_parents = 1;
  152. init.flags = 0;
  153. priv = devm_kzalloc(dev, sizeof(struct clk_bcm2835_i2c), GFP_KERNEL);
  154. if (priv == NULL)
  155. return ERR_PTR(-ENOMEM);
  156. priv->hw.init = &init;
  157. priv->i2c_dev = i2c_dev;
  158. clk_hw_register_clkdev(&priv->hw, "div", dev_name(dev));
  159. return devm_clk_register(dev, &priv->hw);
  160. }
  161. static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
  162. {
  163. u32 val;
  164. while (i2c_dev->msg_buf_remaining) {
  165. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  166. if (!(val & BCM2835_I2C_S_TXD))
  167. break;
  168. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
  169. *i2c_dev->msg_buf);
  170. i2c_dev->msg_buf++;
  171. i2c_dev->msg_buf_remaining--;
  172. }
  173. }
  174. static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
  175. {
  176. u32 val;
  177. while (i2c_dev->msg_buf_remaining) {
  178. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  179. if (!(val & BCM2835_I2C_S_RXD))
  180. break;
  181. *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
  182. BCM2835_I2C_FIFO);
  183. i2c_dev->msg_buf++;
  184. i2c_dev->msg_buf_remaining--;
  185. }
  186. }
  187. /*
  188. * Repeated Start Condition (Sr)
  189. * The BCM2835 ARM Peripherals datasheet mentions a way to trigger a Sr when it
  190. * talks about reading from a slave with 10 bit address. This is achieved by
  191. * issuing a write, poll the I2CS.TA flag and wait for it to be set, and then
  192. * issue a read.
  193. * A comment in https://github.com/raspberrypi/linux/issues/254 shows how the
  194. * firmware actually does it using polling and says that it's a workaround for
  195. * a problem in the state machine.
  196. * It turns out that it is possible to use the TXW interrupt to know when the
  197. * transfer is active, provided the FIFO has not been prefilled.
  198. */
  199. static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev *i2c_dev)
  200. {
  201. u32 c = BCM2835_I2C_C_ST | BCM2835_I2C_C_I2CEN;
  202. struct i2c_msg *msg = i2c_dev->curr_msg;
  203. bool last_msg = (i2c_dev->num_msgs == 1);
  204. if (!i2c_dev->num_msgs)
  205. return;
  206. i2c_dev->num_msgs--;
  207. i2c_dev->msg_buf = msg->buf;
  208. i2c_dev->msg_buf_remaining = msg->len;
  209. if (msg->flags & I2C_M_RD)
  210. c |= BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
  211. else
  212. c |= BCM2835_I2C_C_INTT;
  213. if (last_msg)
  214. c |= BCM2835_I2C_C_INTD;
  215. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
  216. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
  217. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
  218. }
  219. static void bcm2835_i2c_finish_transfer(struct bcm2835_i2c_dev *i2c_dev)
  220. {
  221. i2c_dev->curr_msg = NULL;
  222. i2c_dev->num_msgs = 0;
  223. i2c_dev->msg_buf = NULL;
  224. i2c_dev->msg_buf_remaining = 0;
  225. }
  226. /*
  227. * Note about I2C_C_CLEAR on error:
  228. * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in
  229. * non-idle state and I2C_C_READ, it sets an abort_rx flag and runs through
  230. * the state machine to send a NACK and a STOP. Since we're setting CLEAR
  231. * without I2CEN, that NACK will be hanging around queued up for next time
  232. * we start the engine.
  233. */
  234. static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
  235. {
  236. struct bcm2835_i2c_dev *i2c_dev = data;
  237. u32 val, err;
  238. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  239. err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
  240. if (err) {
  241. i2c_dev->msg_err = err;
  242. goto complete;
  243. }
  244. if (val & BCM2835_I2C_S_DONE) {
  245. if (!i2c_dev->curr_msg) {
  246. dev_err(i2c_dev->dev, "Got unexpected interrupt (from firmware?)\n");
  247. } else if (i2c_dev->curr_msg->flags & I2C_M_RD) {
  248. bcm2835_drain_rxfifo(i2c_dev);
  249. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  250. }
  251. if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
  252. i2c_dev->msg_err = BCM2835_I2C_S_LEN;
  253. else
  254. i2c_dev->msg_err = 0;
  255. goto complete;
  256. }
  257. if (val & BCM2835_I2C_S_TXW) {
  258. if (!i2c_dev->msg_buf_remaining) {
  259. i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
  260. goto complete;
  261. }
  262. bcm2835_fill_txfifo(i2c_dev);
  263. if (i2c_dev->num_msgs && !i2c_dev->msg_buf_remaining) {
  264. i2c_dev->curr_msg++;
  265. bcm2835_i2c_start_transfer(i2c_dev);
  266. }
  267. return IRQ_HANDLED;
  268. }
  269. if (val & BCM2835_I2C_S_RXR) {
  270. if (!i2c_dev->msg_buf_remaining) {
  271. i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
  272. goto complete;
  273. }
  274. bcm2835_drain_rxfifo(i2c_dev);
  275. return IRQ_HANDLED;
  276. }
  277. return IRQ_NONE;
  278. complete:
  279. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
  280. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, BCM2835_I2C_S_CLKT |
  281. BCM2835_I2C_S_ERR | BCM2835_I2C_S_DONE);
  282. complete(&i2c_dev->completion);
  283. return IRQ_HANDLED;
  284. }
  285. static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  286. int num)
  287. {
  288. struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  289. unsigned long time_left;
  290. int i;
  291. for (i = 0; i < (num - 1); i++)
  292. if (msgs[i].flags & I2C_M_RD) {
  293. dev_warn_once(i2c_dev->dev,
  294. "only one read message supported, has to be last\n");
  295. return -EOPNOTSUPP;
  296. }
  297. i2c_dev->curr_msg = msgs;
  298. i2c_dev->num_msgs = num;
  299. reinit_completion(&i2c_dev->completion);
  300. bcm2835_i2c_start_transfer(i2c_dev);
  301. time_left = wait_for_completion_timeout(&i2c_dev->completion,
  302. adap->timeout);
  303. bcm2835_i2c_finish_transfer(i2c_dev);
  304. if (!time_left) {
  305. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C,
  306. BCM2835_I2C_C_CLEAR);
  307. dev_err(i2c_dev->dev, "i2c transfer timed out\n");
  308. return -ETIMEDOUT;
  309. }
  310. if (!i2c_dev->msg_err)
  311. return num;
  312. dev_dbg(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
  313. if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
  314. return -EREMOTEIO;
  315. return -EIO;
  316. }
  317. static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
  318. {
  319. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  320. }
  321. static const struct i2c_algorithm bcm2835_i2c_algo = {
  322. .master_xfer = bcm2835_i2c_xfer,
  323. .functionality = bcm2835_i2c_func,
  324. };
  325. /*
  326. * The BCM2835 was reported to have problems with clock stretching:
  327. * https://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
  328. * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
  329. */
  330. static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
  331. .flags = I2C_AQ_NO_CLK_STRETCH,
  332. };
  333. static int bcm2835_i2c_probe(struct platform_device *pdev)
  334. {
  335. struct bcm2835_i2c_dev *i2c_dev;
  336. struct resource *mem;
  337. int ret;
  338. struct i2c_adapter *adap;
  339. struct clk *mclk;
  340. u32 bus_clk_rate;
  341. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  342. if (!i2c_dev)
  343. return -ENOMEM;
  344. platform_set_drvdata(pdev, i2c_dev);
  345. i2c_dev->dev = &pdev->dev;
  346. init_completion(&i2c_dev->completion);
  347. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  348. i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
  349. if (IS_ERR(i2c_dev->regs))
  350. return PTR_ERR(i2c_dev->regs);
  351. mclk = devm_clk_get(&pdev->dev, NULL);
  352. if (IS_ERR(mclk))
  353. return dev_err_probe(&pdev->dev, PTR_ERR(mclk),
  354. "Could not get clock\n");
  355. i2c_dev->bus_clk = bcm2835_i2c_register_div(&pdev->dev, mclk, i2c_dev);
  356. if (IS_ERR(i2c_dev->bus_clk)) {
  357. dev_err(&pdev->dev, "Could not register clock\n");
  358. return PTR_ERR(i2c_dev->bus_clk);
  359. }
  360. ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  361. &bus_clk_rate);
  362. if (ret < 0) {
  363. dev_warn(&pdev->dev,
  364. "Could not read clock-frequency property\n");
  365. bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ;
  366. }
  367. ret = clk_set_rate_exclusive(i2c_dev->bus_clk, bus_clk_rate);
  368. if (ret < 0) {
  369. dev_err(&pdev->dev, "Could not set clock frequency\n");
  370. return ret;
  371. }
  372. ret = clk_prepare_enable(i2c_dev->bus_clk);
  373. if (ret) {
  374. dev_err(&pdev->dev, "Couldn't prepare clock");
  375. goto err_put_exclusive_rate;
  376. }
  377. i2c_dev->irq = platform_get_irq(pdev, 0);
  378. if (i2c_dev->irq < 0) {
  379. ret = i2c_dev->irq;
  380. goto err_disable_unprepare_clk;
  381. }
  382. ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
  383. dev_name(&pdev->dev), i2c_dev);
  384. if (ret) {
  385. dev_err(&pdev->dev, "Could not request IRQ\n");
  386. goto err_disable_unprepare_clk;
  387. }
  388. adap = &i2c_dev->adapter;
  389. i2c_set_adapdata(adap, i2c_dev);
  390. adap->owner = THIS_MODULE;
  391. adap->class = I2C_CLASS_DEPRECATED;
  392. snprintf(adap->name, sizeof(adap->name), "bcm2835 (%s)",
  393. of_node_full_name(pdev->dev.of_node));
  394. adap->algo = &bcm2835_i2c_algo;
  395. adap->dev.parent = &pdev->dev;
  396. adap->dev.of_node = pdev->dev.of_node;
  397. adap->quirks = of_device_get_match_data(&pdev->dev);
  398. /*
  399. * Disable the hardware clock stretching timeout. SMBUS
  400. * specifies a limit for how long the device can stretch the
  401. * clock, but core I2C doesn't.
  402. */
  403. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_CLKT, 0);
  404. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
  405. ret = i2c_add_adapter(adap);
  406. if (ret)
  407. goto err_free_irq;
  408. return 0;
  409. err_free_irq:
  410. free_irq(i2c_dev->irq, i2c_dev);
  411. err_disable_unprepare_clk:
  412. clk_disable_unprepare(i2c_dev->bus_clk);
  413. err_put_exclusive_rate:
  414. clk_rate_exclusive_put(i2c_dev->bus_clk);
  415. return ret;
  416. }
  417. static int bcm2835_i2c_remove(struct platform_device *pdev)
  418. {
  419. struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  420. clk_rate_exclusive_put(i2c_dev->bus_clk);
  421. clk_disable_unprepare(i2c_dev->bus_clk);
  422. free_irq(i2c_dev->irq, i2c_dev);
  423. i2c_del_adapter(&i2c_dev->adapter);
  424. return 0;
  425. }
  426. static const struct of_device_id bcm2835_i2c_of_match[] = {
  427. { .compatible = "brcm,bcm2711-i2c" },
  428. { .compatible = "brcm,bcm2835-i2c", .data = &bcm2835_i2c_quirks },
  429. {},
  430. };
  431. MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
  432. static struct platform_driver bcm2835_i2c_driver = {
  433. .probe = bcm2835_i2c_probe,
  434. .remove = bcm2835_i2c_remove,
  435. .driver = {
  436. .name = "i2c-bcm2835",
  437. .of_match_table = bcm2835_i2c_of_match,
  438. },
  439. };
  440. module_platform_driver(bcm2835_i2c_driver);
  441. MODULE_AUTHOR("Stephen Warren <[email protected]>");
  442. MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
  443. MODULE_LICENSE("GPL v2");
  444. MODULE_ALIAS("platform:i2c-bcm2835");