i2c-pca-platform.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * i2c_pca_platform.c
  3. *
  4. * Platform driver for the PCA9564 I2C controller.
  5. *
  6. * Copyright (C) 2008 Pengutronix
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/delay.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/errno.h>
  19. #include <linux/i2c.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/i2c-algo-pca.h>
  23. #include <linux/i2c-pca-platform.h>
  24. #include <linux/gpio.h>
  25. #include <linux/io.h>
  26. #include <asm/irq.h>
  27. struct i2c_pca_pf_data {
  28. void __iomem *reg_base;
  29. int irq; /* if 0, use polling */
  30. int gpio;
  31. wait_queue_head_t wait;
  32. struct i2c_adapter adap;
  33. struct i2c_algo_pca_data algo_data;
  34. unsigned long io_base;
  35. unsigned long io_size;
  36. };
  37. /* Read/Write functions for different register alignments */
  38. static int i2c_pca_pf_readbyte8(void *pd, int reg)
  39. {
  40. struct i2c_pca_pf_data *i2c = pd;
  41. return ioread8(i2c->reg_base + reg);
  42. }
  43. static int i2c_pca_pf_readbyte16(void *pd, int reg)
  44. {
  45. struct i2c_pca_pf_data *i2c = pd;
  46. return ioread8(i2c->reg_base + reg * 2);
  47. }
  48. static int i2c_pca_pf_readbyte32(void *pd, int reg)
  49. {
  50. struct i2c_pca_pf_data *i2c = pd;
  51. return ioread8(i2c->reg_base + reg * 4);
  52. }
  53. static void i2c_pca_pf_writebyte8(void *pd, int reg, int val)
  54. {
  55. struct i2c_pca_pf_data *i2c = pd;
  56. iowrite8(val, i2c->reg_base + reg);
  57. }
  58. static void i2c_pca_pf_writebyte16(void *pd, int reg, int val)
  59. {
  60. struct i2c_pca_pf_data *i2c = pd;
  61. iowrite8(val, i2c->reg_base + reg * 2);
  62. }
  63. static void i2c_pca_pf_writebyte32(void *pd, int reg, int val)
  64. {
  65. struct i2c_pca_pf_data *i2c = pd;
  66. iowrite8(val, i2c->reg_base + reg * 4);
  67. }
  68. static int i2c_pca_pf_waitforcompletion(void *pd)
  69. {
  70. struct i2c_pca_pf_data *i2c = pd;
  71. unsigned long timeout;
  72. long ret;
  73. if (i2c->irq) {
  74. ret = wait_event_timeout(i2c->wait,
  75. i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
  76. & I2C_PCA_CON_SI, i2c->adap.timeout);
  77. } else {
  78. /* Do polling */
  79. timeout = jiffies + i2c->adap.timeout;
  80. do {
  81. ret = time_before(jiffies, timeout);
  82. if (i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
  83. & I2C_PCA_CON_SI)
  84. break;
  85. udelay(100);
  86. } while (ret);
  87. }
  88. return ret > 0;
  89. }
  90. static void i2c_pca_pf_dummyreset(void *pd)
  91. {
  92. struct i2c_pca_pf_data *i2c = pd;
  93. printk(KERN_WARNING "%s: No reset-pin found. Chip may get stuck!\n",
  94. i2c->adap.name);
  95. }
  96. static void i2c_pca_pf_resetchip(void *pd)
  97. {
  98. struct i2c_pca_pf_data *i2c = pd;
  99. gpio_set_value(i2c->gpio, 0);
  100. ndelay(100);
  101. gpio_set_value(i2c->gpio, 1);
  102. }
  103. static irqreturn_t i2c_pca_pf_handler(int this_irq, void *dev_id)
  104. {
  105. struct i2c_pca_pf_data *i2c = dev_id;
  106. if ((i2c->algo_data.read_byte(i2c, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
  107. return IRQ_NONE;
  108. wake_up(&i2c->wait);
  109. return IRQ_HANDLED;
  110. }
  111. static int __devinit i2c_pca_pf_probe(struct platform_device *pdev)
  112. {
  113. struct i2c_pca_pf_data *i2c;
  114. struct resource *res;
  115. struct i2c_pca9564_pf_platform_data *platform_data =
  116. pdev->dev.platform_data;
  117. int ret = 0;
  118. int irq;
  119. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  120. irq = platform_get_irq(pdev, 0);
  121. /* If irq is 0, we do polling. */
  122. if (res == NULL) {
  123. ret = -ENODEV;
  124. goto e_print;
  125. }
  126. if (!request_mem_region(res->start, resource_size(res), res->name)) {
  127. ret = -ENOMEM;
  128. goto e_print;
  129. }
  130. i2c = kzalloc(sizeof(struct i2c_pca_pf_data), GFP_KERNEL);
  131. if (!i2c) {
  132. ret = -ENOMEM;
  133. goto e_alloc;
  134. }
  135. init_waitqueue_head(&i2c->wait);
  136. i2c->reg_base = ioremap(res->start, resource_size(res));
  137. if (!i2c->reg_base) {
  138. ret = -ENOMEM;
  139. goto e_remap;
  140. }
  141. i2c->io_base = res->start;
  142. i2c->io_size = resource_size(res);
  143. i2c->irq = irq;
  144. i2c->adap.nr = pdev->id >= 0 ? pdev->id : 0;
  145. i2c->adap.owner = THIS_MODULE;
  146. snprintf(i2c->adap.name, sizeof(i2c->adap.name),
  147. "PCA9564/PCA9665 at 0x%08lx",
  148. (unsigned long) res->start);
  149. i2c->adap.algo_data = &i2c->algo_data;
  150. i2c->adap.dev.parent = &pdev->dev;
  151. if (platform_data) {
  152. i2c->adap.timeout = platform_data->timeout;
  153. i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed;
  154. i2c->gpio = platform_data->gpio;
  155. } else {
  156. i2c->adap.timeout = HZ;
  157. i2c->algo_data.i2c_clock = 59000;
  158. i2c->gpio = -1;
  159. }
  160. i2c->algo_data.data = i2c;
  161. i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion;
  162. i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset;
  163. switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
  164. case IORESOURCE_MEM_32BIT:
  165. i2c->algo_data.write_byte = i2c_pca_pf_writebyte32;
  166. i2c->algo_data.read_byte = i2c_pca_pf_readbyte32;
  167. break;
  168. case IORESOURCE_MEM_16BIT:
  169. i2c->algo_data.write_byte = i2c_pca_pf_writebyte16;
  170. i2c->algo_data.read_byte = i2c_pca_pf_readbyte16;
  171. break;
  172. case IORESOURCE_MEM_8BIT:
  173. default:
  174. i2c->algo_data.write_byte = i2c_pca_pf_writebyte8;
  175. i2c->algo_data.read_byte = i2c_pca_pf_readbyte8;
  176. break;
  177. }
  178. /* Use gpio_is_valid() when in mainline */
  179. if (i2c->gpio > -1) {
  180. ret = gpio_request(i2c->gpio, i2c->adap.name);
  181. if (ret == 0) {
  182. gpio_direction_output(i2c->gpio, 1);
  183. i2c->algo_data.reset_chip = i2c_pca_pf_resetchip;
  184. } else {
  185. printk(KERN_WARNING "%s: Registering gpio failed!\n",
  186. i2c->adap.name);
  187. i2c->gpio = ret;
  188. }
  189. }
  190. if (irq) {
  191. ret = request_irq(irq, i2c_pca_pf_handler,
  192. IRQF_TRIGGER_FALLING, pdev->name, i2c);
  193. if (ret)
  194. goto e_reqirq;
  195. }
  196. if (i2c_pca_add_numbered_bus(&i2c->adap) < 0) {
  197. ret = -ENODEV;
  198. goto e_adapt;
  199. }
  200. platform_set_drvdata(pdev, i2c);
  201. printk(KERN_INFO "%s registered.\n", i2c->adap.name);
  202. return 0;
  203. e_adapt:
  204. if (irq)
  205. free_irq(irq, i2c);
  206. e_reqirq:
  207. if (i2c->gpio > -1)
  208. gpio_free(i2c->gpio);
  209. iounmap(i2c->reg_base);
  210. e_remap:
  211. kfree(i2c);
  212. e_alloc:
  213. release_mem_region(res->start, resource_size(res));
  214. e_print:
  215. printk(KERN_ERR "Registering PCA9564/PCA9665 FAILED! (%d)\n", ret);
  216. return ret;
  217. }
  218. static int __devexit i2c_pca_pf_remove(struct platform_device *pdev)
  219. {
  220. struct i2c_pca_pf_data *i2c = platform_get_drvdata(pdev);
  221. platform_set_drvdata(pdev, NULL);
  222. i2c_del_adapter(&i2c->adap);
  223. if (i2c->irq)
  224. free_irq(i2c->irq, i2c);
  225. if (i2c->gpio > -1)
  226. gpio_free(i2c->gpio);
  227. iounmap(i2c->reg_base);
  228. release_mem_region(i2c->io_base, i2c->io_size);
  229. kfree(i2c);
  230. return 0;
  231. }
  232. static struct platform_driver i2c_pca_pf_driver = {
  233. .probe = i2c_pca_pf_probe,
  234. .remove = __devexit_p(i2c_pca_pf_remove),
  235. .driver = {
  236. .name = "i2c-pca-platform",
  237. .owner = THIS_MODULE,
  238. },
  239. };
  240. module_platform_driver(i2c_pca_pf_driver);
  241. MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
  242. MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver");
  243. MODULE_LICENSE("GPL");