i2c-bcm2835.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_BITMSK_S 0x03FF
  49. #define BCM2835_I2C_CDIV_MIN 0x0002
  50. #define BCM2835_I2C_CDIV_MAX 0xFFFE
  51. #define BCM2835_I2C_TIMEOUT (msecs_to_jiffies(1000))
  52. struct bcm2835_i2c_dev {
  53. struct device *dev;
  54. void __iomem *regs;
  55. struct clk *clk;
  56. int irq;
  57. struct i2c_adapter adapter;
  58. struct completion completion;
  59. u32 msg_err;
  60. u8 *msg_buf;
  61. size_t msg_buf_remaining;
  62. };
  63. static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
  64. u32 reg, u32 val)
  65. {
  66. writel(val, i2c_dev->regs + reg);
  67. }
  68. static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
  69. {
  70. return readl(i2c_dev->regs + reg);
  71. }
  72. static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
  73. {
  74. u32 val;
  75. while (i2c_dev->msg_buf_remaining) {
  76. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  77. if (!(val & BCM2835_I2C_S_TXD))
  78. break;
  79. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
  80. *i2c_dev->msg_buf);
  81. i2c_dev->msg_buf++;
  82. i2c_dev->msg_buf_remaining--;
  83. }
  84. }
  85. static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
  86. {
  87. u32 val;
  88. while (i2c_dev->msg_buf_remaining) {
  89. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  90. if (!(val & BCM2835_I2C_S_RXD))
  91. break;
  92. *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
  93. BCM2835_I2C_FIFO);
  94. i2c_dev->msg_buf++;
  95. i2c_dev->msg_buf_remaining--;
  96. }
  97. }
  98. static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
  99. {
  100. struct bcm2835_i2c_dev *i2c_dev = data;
  101. u32 val, err;
  102. val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
  103. val &= BCM2835_I2C_BITMSK_S;
  104. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, val);
  105. err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
  106. if (err) {
  107. i2c_dev->msg_err = err;
  108. complete(&i2c_dev->completion);
  109. return IRQ_HANDLED;
  110. }
  111. if (val & BCM2835_I2C_S_RXD) {
  112. bcm2835_drain_rxfifo(i2c_dev);
  113. if (!(val & BCM2835_I2C_S_DONE))
  114. return IRQ_HANDLED;
  115. }
  116. if (val & BCM2835_I2C_S_DONE) {
  117. if (i2c_dev->msg_buf_remaining)
  118. i2c_dev->msg_err = BCM2835_I2C_S_LEN;
  119. else
  120. i2c_dev->msg_err = 0;
  121. complete(&i2c_dev->completion);
  122. return IRQ_HANDLED;
  123. }
  124. if (val & BCM2835_I2C_S_TXD) {
  125. bcm2835_fill_txfifo(i2c_dev);
  126. return IRQ_HANDLED;
  127. }
  128. return IRQ_NONE;
  129. }
  130. static int bcm2835_i2c_xfer_msg(struct bcm2835_i2c_dev *i2c_dev,
  131. struct i2c_msg *msg)
  132. {
  133. u32 c;
  134. unsigned long time_left;
  135. i2c_dev->msg_buf = msg->buf;
  136. i2c_dev->msg_buf_remaining = msg->len;
  137. reinit_completion(&i2c_dev->completion);
  138. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
  139. if (msg->flags & I2C_M_RD) {
  140. c = BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
  141. } else {
  142. c = BCM2835_I2C_C_INTT;
  143. bcm2835_fill_txfifo(i2c_dev);
  144. }
  145. c |= BCM2835_I2C_C_ST | BCM2835_I2C_C_INTD | BCM2835_I2C_C_I2CEN;
  146. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
  147. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
  148. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
  149. time_left = wait_for_completion_timeout(&i2c_dev->completion,
  150. BCM2835_I2C_TIMEOUT);
  151. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
  152. if (!time_left) {
  153. dev_err(i2c_dev->dev, "i2c transfer timed out\n");
  154. return -ETIMEDOUT;
  155. }
  156. if (likely(!i2c_dev->msg_err))
  157. return 0;
  158. if ((i2c_dev->msg_err & BCM2835_I2C_S_ERR) &&
  159. (msg->flags & I2C_M_IGNORE_NAK))
  160. return 0;
  161. dev_err(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
  162. if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
  163. return -EREMOTEIO;
  164. else
  165. return -EIO;
  166. }
  167. static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  168. int num)
  169. {
  170. struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
  171. int i;
  172. int ret = 0;
  173. for (i = 0; i < num; i++) {
  174. ret = bcm2835_i2c_xfer_msg(i2c_dev, &msgs[i]);
  175. if (ret)
  176. break;
  177. }
  178. return ret ?: i;
  179. }
  180. static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
  181. {
  182. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  183. }
  184. static const struct i2c_algorithm bcm2835_i2c_algo = {
  185. .master_xfer = bcm2835_i2c_xfer,
  186. .functionality = bcm2835_i2c_func,
  187. };
  188. static int bcm2835_i2c_probe(struct platform_device *pdev)
  189. {
  190. struct bcm2835_i2c_dev *i2c_dev;
  191. struct resource *mem, *irq;
  192. u32 bus_clk_rate, divider;
  193. int ret;
  194. struct i2c_adapter *adap;
  195. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  196. if (!i2c_dev)
  197. return -ENOMEM;
  198. platform_set_drvdata(pdev, i2c_dev);
  199. i2c_dev->dev = &pdev->dev;
  200. init_completion(&i2c_dev->completion);
  201. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  202. i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
  203. if (IS_ERR(i2c_dev->regs))
  204. return PTR_ERR(i2c_dev->regs);
  205. i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
  206. if (IS_ERR(i2c_dev->clk)) {
  207. dev_err(&pdev->dev, "Could not get clock\n");
  208. return PTR_ERR(i2c_dev->clk);
  209. }
  210. ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  211. &bus_clk_rate);
  212. if (ret < 0) {
  213. dev_warn(&pdev->dev,
  214. "Could not read clock-frequency property\n");
  215. bus_clk_rate = 100000;
  216. }
  217. divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate);
  218. /*
  219. * Per the datasheet, the register is always interpreted as an even
  220. * number, by rounding down. In other words, the LSB is ignored. So,
  221. * if the LSB is set, increment the divider to avoid any issue.
  222. */
  223. if (divider & 1)
  224. divider++;
  225. if ((divider < BCM2835_I2C_CDIV_MIN) ||
  226. (divider > BCM2835_I2C_CDIV_MAX)) {
  227. dev_err(&pdev->dev, "Invalid clock-frequency\n");
  228. return -ENODEV;
  229. }
  230. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
  231. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  232. if (!irq) {
  233. dev_err(&pdev->dev, "No IRQ resource\n");
  234. return -ENODEV;
  235. }
  236. i2c_dev->irq = irq->start;
  237. ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
  238. dev_name(&pdev->dev), i2c_dev);
  239. if (ret) {
  240. dev_err(&pdev->dev, "Could not request IRQ\n");
  241. return -ENODEV;
  242. }
  243. adap = &i2c_dev->adapter;
  244. i2c_set_adapdata(adap, i2c_dev);
  245. adap->owner = THIS_MODULE;
  246. adap->class = I2C_CLASS_DEPRECATED;
  247. strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
  248. adap->algo = &bcm2835_i2c_algo;
  249. adap->dev.parent = &pdev->dev;
  250. adap->dev.of_node = pdev->dev.of_node;
  251. bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
  252. ret = i2c_add_adapter(adap);
  253. if (ret)
  254. free_irq(i2c_dev->irq, i2c_dev);
  255. return ret;
  256. }
  257. static int bcm2835_i2c_remove(struct platform_device *pdev)
  258. {
  259. struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  260. free_irq(i2c_dev->irq, i2c_dev);
  261. i2c_del_adapter(&i2c_dev->adapter);
  262. return 0;
  263. }
  264. static const struct of_device_id bcm2835_i2c_of_match[] = {
  265. { .compatible = "brcm,bcm2835-i2c" },
  266. {},
  267. };
  268. MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
  269. static struct platform_driver bcm2835_i2c_driver = {
  270. .probe = bcm2835_i2c_probe,
  271. .remove = bcm2835_i2c_remove,
  272. .driver = {
  273. .name = "i2c-bcm2835",
  274. .of_match_table = bcm2835_i2c_of_match,
  275. },
  276. };
  277. module_platform_driver(bcm2835_i2c_driver);
  278. MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
  279. MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
  280. MODULE_LICENSE("GPL v2");
  281. MODULE_ALIAS("platform:i2c-bcm2835");