i2c-pca-platform.c 6.4 KB

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