PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/i2c/busses/i2c-bcm2708.c

https://github.com/alwold/rpi-linux
C | 400 lines | 288 code | 76 blank | 36 comment | 27 complexity | 978fd467524e2fb7f94d45df60ddd35c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Driver for Broadcom BCM2708 BSC Controllers
  3. *
  4. * Copyright (C) 2012 Chris Boot & Frank Buss
  5. *
  6. * This driver is inspired by:
  7. * i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/clk.h>
  27. #include <linux/err.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/io.h>
  30. #include <linux/slab.h>
  31. #include <linux/i2c.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/sched.h>
  34. #include <linux/wait.h>
  35. /* BSC register offsets */
  36. #define BSC_C 0x00
  37. #define BSC_S 0x04
  38. #define BSC_DLEN 0x08
  39. #define BSC_A 0x0c
  40. #define BSC_FIFO 0x10
  41. #define BSC_DIV 0x14
  42. #define BSC_DEL 0x18
  43. #define BSC_CLKT 0x1c
  44. /* Bitfields in BSC_C */
  45. #define BSC_C_I2CEN 0x00008000
  46. #define BSC_C_INTR 0x00000400
  47. #define BSC_C_INTT 0x00000200
  48. #define BSC_C_INTD 0x00000100
  49. #define BSC_C_ST 0x00000080
  50. #define BSC_C_CLEAR_1 0x00000020
  51. #define BSC_C_CLEAR_2 0x00000010
  52. #define BSC_C_READ 0x00000001
  53. /* Bitfields in BSC_S */
  54. #define BSC_S_CLKT 0x00000200
  55. #define BSC_S_ERR 0x00000100
  56. #define BSC_S_RXF 0x00000080
  57. #define BSC_S_TXE 0x00000040
  58. #define BSC_S_RXD 0x00000020
  59. #define BSC_S_TXD 0x00000010
  60. #define BSC_S_RXR 0x00000008
  61. #define BSC_S_TXW 0x00000004
  62. #define BSC_S_DONE 0x00000002
  63. #define BSC_S_TA 0x00000001
  64. #define I2C_TIMEOUT_MS 150
  65. #define DRV_NAME "bcm2708_i2c"
  66. static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
  67. module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  68. MODULE_PARM_DESC(baudrate, "The I2C baudrate");
  69. struct bcm2708_i2c {
  70. struct i2c_adapter adapter;
  71. spinlock_t lock;
  72. void __iomem *base;
  73. int irq;
  74. struct clk *clk;
  75. struct completion done;
  76. struct i2c_msg *msg;
  77. int pos;
  78. int nmsgs;
  79. bool error;
  80. };
  81. /*
  82. * This function sets the ALT mode on the I2C pins so that we can use them with
  83. * the BSC hardware.
  84. *
  85. * FIXME: This is a hack. Use pinmux / pinctrl.
  86. */
  87. static void bcm2708_i2c_init_pinmode(void)
  88. {
  89. #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  90. #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  91. int pin;
  92. u32 *gpio = ioremap(0x20200000, SZ_16K);
  93. /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
  94. for (pin = 0; pin <= 3; pin++) {
  95. INP_GPIO(pin); /* set mode to GPIO input first */
  96. SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  97. }
  98. iounmap(gpio);
  99. #undef INP_GPIO
  100. #undef SET_GPIO_ALT
  101. }
  102. static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
  103. {
  104. return readl(bi->base + reg);
  105. }
  106. static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
  107. {
  108. writel(val, bi->base + reg);
  109. }
  110. static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
  111. {
  112. bcm2708_wr(bi, BSC_C, 0);
  113. bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
  114. }
  115. static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
  116. {
  117. while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
  118. bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
  119. }
  120. static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
  121. {
  122. while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
  123. bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  124. }
  125. static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
  126. {
  127. unsigned long bus_hz;
  128. u32 cdiv;
  129. u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
  130. bus_hz = clk_get_rate(bi->clk);
  131. cdiv = bus_hz / baudrate;
  132. if (bi->msg->flags & I2C_M_RD)
  133. c |= BSC_C_INTR | BSC_C_READ;
  134. else
  135. c |= BSC_C_INTT;
  136. bcm2708_wr(bi, BSC_DIV, cdiv);
  137. bcm2708_wr(bi, BSC_A, bi->msg->addr);
  138. bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  139. bcm2708_wr(bi, BSC_C, c);
  140. }
  141. static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
  142. {
  143. struct bcm2708_i2c *bi = dev_id;
  144. bool handled = true;
  145. u32 s;
  146. spin_lock(&bi->lock);
  147. s = bcm2708_rd(bi, BSC_S);
  148. if (s & (BSC_S_CLKT | BSC_S_ERR)) {
  149. bcm2708_bsc_reset(bi);
  150. bi->error = true;
  151. /* wake up our bh */
  152. complete(&bi->done);
  153. } else if (s & BSC_S_DONE) {
  154. bi->nmsgs--;
  155. if (bi->msg->flags & I2C_M_RD)
  156. bcm2708_bsc_fifo_drain(bi);
  157. bcm2708_bsc_reset(bi);
  158. if (bi->nmsgs) {
  159. /* advance to next message */
  160. bi->msg++;
  161. bi->pos = 0;
  162. bcm2708_bsc_setup(bi);
  163. } else {
  164. /* wake up our bh */
  165. complete(&bi->done);
  166. }
  167. } else if (s & BSC_S_TXW) {
  168. bcm2708_bsc_fifo_fill(bi);
  169. } else if (s & BSC_S_RXR) {
  170. bcm2708_bsc_fifo_drain(bi);
  171. } else {
  172. handled = false;
  173. }
  174. spin_unlock(&bi->lock);
  175. return handled ? IRQ_HANDLED : IRQ_NONE;
  176. }
  177. static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
  178. struct i2c_msg *msgs, int num)
  179. {
  180. struct bcm2708_i2c *bi = adap->algo_data;
  181. unsigned long flags;
  182. int ret;
  183. spin_lock_irqsave(&bi->lock, flags);
  184. INIT_COMPLETION(bi->done);
  185. bi->msg = msgs;
  186. bi->pos = 0;
  187. bi->nmsgs = num;
  188. bi->error = false;
  189. spin_unlock_irqrestore(&bi->lock, flags);
  190. bcm2708_bsc_setup(bi);
  191. ret = wait_for_completion_timeout(&bi->done,
  192. msecs_to_jiffies(I2C_TIMEOUT_MS));
  193. if (ret == 0) {
  194. dev_err(&adap->dev, "transfer timed out\n");
  195. spin_lock_irqsave(&bi->lock, flags);
  196. bcm2708_bsc_reset(bi);
  197. spin_unlock_irqrestore(&bi->lock, flags);
  198. return -ETIMEDOUT;
  199. }
  200. return bi->error ? -EIO : num;
  201. }
  202. static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
  203. {
  204. return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
  205. }
  206. static struct i2c_algorithm bcm2708_i2c_algorithm = {
  207. .master_xfer = bcm2708_i2c_master_xfer,
  208. .functionality = bcm2708_i2c_functionality,
  209. };
  210. static int __devinit bcm2708_i2c_probe(struct platform_device *pdev)
  211. {
  212. struct resource *regs;
  213. int irq, err = -ENOMEM;
  214. struct clk *clk;
  215. struct bcm2708_i2c *bi;
  216. struct i2c_adapter *adap;
  217. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  218. if (!regs) {
  219. dev_err(&pdev->dev, "could not get IO memory\n");
  220. return -ENXIO;
  221. }
  222. irq = platform_get_irq(pdev, 0);
  223. if (irq < 0) {
  224. dev_err(&pdev->dev, "could not get IRQ\n");
  225. return irq;
  226. }
  227. clk = clk_get(&pdev->dev, NULL);
  228. if (IS_ERR(clk)) {
  229. dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  230. return PTR_ERR(clk);
  231. }
  232. bcm2708_i2c_init_pinmode();
  233. bi = kzalloc(sizeof(*bi), GFP_KERNEL);
  234. if (!bi)
  235. goto out_clk_put;
  236. platform_set_drvdata(pdev, bi);
  237. adap = &bi->adapter;
  238. adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
  239. adap->algo = &bcm2708_i2c_algorithm;
  240. adap->algo_data = bi;
  241. adap->dev.parent = &pdev->dev;
  242. adap->nr = pdev->id;
  243. strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
  244. switch (pdev->id) {
  245. case 0:
  246. adap->class = I2C_CLASS_HWMON;
  247. break;
  248. case 1:
  249. adap->class = I2C_CLASS_DDC;
  250. break;
  251. default:
  252. dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
  253. err = -ENXIO;
  254. goto out_free_bi;
  255. }
  256. spin_lock_init(&bi->lock);
  257. init_completion(&bi->done);
  258. bi->base = ioremap(regs->start, resource_size(regs));
  259. if (!bi->base) {
  260. dev_err(&pdev->dev, "could not remap memory\n");
  261. goto out_free_bi;
  262. }
  263. bi->irq = irq;
  264. bi->clk = clk;
  265. err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
  266. dev_name(&pdev->dev), bi);
  267. if (err) {
  268. dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  269. goto out_iounmap;
  270. }
  271. bcm2708_bsc_reset(bi);
  272. err = i2c_add_numbered_adapter(adap);
  273. if (err < 0) {
  274. dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
  275. goto out_free_irq;
  276. }
  277. dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %dk)\n",
  278. pdev->id, (unsigned long)regs->start, irq, baudrate/1000);
  279. return 0;
  280. out_free_irq:
  281. free_irq(bi->irq, bi);
  282. out_iounmap:
  283. iounmap(bi->base);
  284. out_free_bi:
  285. kfree(bi);
  286. out_clk_put:
  287. clk_put(clk);
  288. return err;
  289. }
  290. static int __devexit bcm2708_i2c_remove(struct platform_device *pdev)
  291. {
  292. struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
  293. platform_set_drvdata(pdev, NULL);
  294. i2c_del_adapter(&bi->adapter);
  295. free_irq(bi->irq, bi);
  296. iounmap(bi->base);
  297. clk_disable(bi->clk);
  298. clk_put(bi->clk);
  299. kfree(bi);
  300. return 0;
  301. }
  302. static struct platform_driver bcm2708_i2c_driver = {
  303. .driver = {
  304. .name = DRV_NAME,
  305. .owner = THIS_MODULE,
  306. },
  307. .probe = bcm2708_i2c_probe,
  308. .remove = __devexit_p(bcm2708_i2c_remove),
  309. };
  310. // module_platform_driver(bcm2708_i2c_driver);
  311. static int __init bcm2708_i2c_init(void)
  312. {
  313. return platform_driver_register(&bcm2708_i2c_driver);
  314. }
  315. static void __exit bcm2708_i2c_exit(void)
  316. {
  317. platform_driver_unregister(&bcm2708_i2c_driver);
  318. }
  319. module_init(bcm2708_i2c_init);
  320. module_exit(bcm2708_i2c_exit);
  321. MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
  322. MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  323. MODULE_LICENSE("GPL v2");
  324. MODULE_ALIAS("platform:" DRV_NAME);