i2c-pca-isa.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * i2c-pca-isa.c driver for PCA9564 on ISA boards
  3. * Copyright (C) 2004 Arcom Control Systems
  4. * Copyright (C) 2008 Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/ioport.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/delay.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/wait.h>
  29. #include <linux/isa.h>
  30. #include <linux/i2c.h>
  31. #include <linux/i2c-algo-pca.h>
  32. #include <linux/io.h>
  33. #include <asm/irq.h>
  34. #define DRIVER "i2c-pca-isa"
  35. #define IO_SIZE 4
  36. static unsigned long base;
  37. static int irq = -1;
  38. /* Data sheet recommends 59kHz for 100kHz operation due to variation
  39. * in the actual clock rate */
  40. static int clock = 59000;
  41. static struct i2c_adapter pca_isa_ops;
  42. static wait_queue_head_t pca_wait;
  43. static void pca_isa_writebyte(void *pd, int reg, int val)
  44. {
  45. #ifdef DEBUG_IO
  46. static char *names[] = { "T/O", "DAT", "ADR", "CON" };
  47. printk(KERN_DEBUG "*** write %s at %#lx <= %#04x\n", names[reg],
  48. base+reg, val);
  49. #endif
  50. outb(val, base+reg);
  51. }
  52. static int pca_isa_readbyte(void *pd, int reg)
  53. {
  54. int res = inb(base+reg);
  55. #ifdef DEBUG_IO
  56. {
  57. static char *names[] = { "STA", "DAT", "ADR", "CON" };
  58. printk(KERN_DEBUG "*** read %s => %#04x\n", names[reg], res);
  59. }
  60. #endif
  61. return res;
  62. }
  63. static int pca_isa_waitforcompletion(void *pd)
  64. {
  65. unsigned long timeout;
  66. long ret;
  67. if (irq > -1) {
  68. ret = wait_event_timeout(pca_wait,
  69. pca_isa_readbyte(pd, I2C_PCA_CON)
  70. & I2C_PCA_CON_SI, pca_isa_ops.timeout);
  71. } else {
  72. /* Do polling */
  73. timeout = jiffies + pca_isa_ops.timeout;
  74. do {
  75. ret = time_before(jiffies, timeout);
  76. if (pca_isa_readbyte(pd, I2C_PCA_CON)
  77. & I2C_PCA_CON_SI)
  78. break;
  79. udelay(100);
  80. } while (ret);
  81. }
  82. return ret > 0;
  83. }
  84. static void pca_isa_resetchip(void *pd)
  85. {
  86. /* apparently only an external reset will do it. not a lot can be done */
  87. printk(KERN_WARNING DRIVER ": Haven't figured out how to do a reset yet\n");
  88. }
  89. static irqreturn_t pca_handler(int this_irq, void *dev_id) {
  90. wake_up(&pca_wait);
  91. return IRQ_HANDLED;
  92. }
  93. static struct i2c_algo_pca_data pca_isa_data = {
  94. /* .data intentionally left NULL, not needed with ISA */
  95. .write_byte = pca_isa_writebyte,
  96. .read_byte = pca_isa_readbyte,
  97. .wait_for_completion = pca_isa_waitforcompletion,
  98. .reset_chip = pca_isa_resetchip,
  99. };
  100. static struct i2c_adapter pca_isa_ops = {
  101. .owner = THIS_MODULE,
  102. .algo_data = &pca_isa_data,
  103. .name = "PCA9564/PCA9665 ISA Adapter",
  104. .timeout = HZ,
  105. };
  106. static int __devinit pca_isa_match(struct device *dev, unsigned int id)
  107. {
  108. int match = base != 0;
  109. if (match) {
  110. if (irq <= -1)
  111. dev_warn(dev, "Using polling mode (specify irq)\n");
  112. } else
  113. dev_err(dev, "Please specify I/O base\n");
  114. return match;
  115. }
  116. static int __devinit pca_isa_probe(struct device *dev, unsigned int id)
  117. {
  118. init_waitqueue_head(&pca_wait);
  119. dev_info(dev, "i/o base %#08lx. irq %d\n", base, irq);
  120. #ifdef CONFIG_PPC
  121. if (check_legacy_ioport(base)) {
  122. dev_err(dev, "I/O address %#08lx is not available\n", base);
  123. goto out;
  124. }
  125. #endif
  126. if (!request_region(base, IO_SIZE, "i2c-pca-isa")) {
  127. dev_err(dev, "I/O address %#08lx is in use\n", base);
  128. goto out;
  129. }
  130. if (irq > -1) {
  131. if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) {
  132. dev_err(dev, "Request irq%d failed\n", irq);
  133. goto out_region;
  134. }
  135. }
  136. pca_isa_data.i2c_clock = clock;
  137. if (i2c_pca_add_bus(&pca_isa_ops) < 0) {
  138. dev_err(dev, "Failed to add i2c bus\n");
  139. goto out_irq;
  140. }
  141. return 0;
  142. out_irq:
  143. if (irq > -1)
  144. free_irq(irq, &pca_isa_ops);
  145. out_region:
  146. release_region(base, IO_SIZE);
  147. out:
  148. return -ENODEV;
  149. }
  150. static int __devexit pca_isa_remove(struct device *dev, unsigned int id)
  151. {
  152. i2c_del_adapter(&pca_isa_ops);
  153. if (irq > -1) {
  154. disable_irq(irq);
  155. free_irq(irq, &pca_isa_ops);
  156. }
  157. release_region(base, IO_SIZE);
  158. return 0;
  159. }
  160. static struct isa_driver pca_isa_driver = {
  161. .match = pca_isa_match,
  162. .probe = pca_isa_probe,
  163. .remove = __devexit_p(pca_isa_remove),
  164. .driver = {
  165. .owner = THIS_MODULE,
  166. .name = DRIVER,
  167. }
  168. };
  169. static int __init pca_isa_init(void)
  170. {
  171. return isa_register_driver(&pca_isa_driver, 1);
  172. }
  173. static void __exit pca_isa_exit(void)
  174. {
  175. isa_unregister_driver(&pca_isa_driver);
  176. }
  177. MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
  178. MODULE_DESCRIPTION("ISA base PCA9564/PCA9665 driver");
  179. MODULE_LICENSE("GPL");
  180. module_param(base, ulong, 0);
  181. MODULE_PARM_DESC(base, "I/O base address");
  182. module_param(irq, int, 0);
  183. MODULE_PARM_DESC(irq, "IRQ");
  184. module_param(clock, int, 0);
  185. MODULE_PARM_DESC(clock, "Clock rate in hertz.\n\t\t"
  186. "For PCA9564: 330000,288000,217000,146000,"
  187. "88000,59000,44000,36000\n"
  188. "\t\tFor PCA9665:\tStandard: 60300 - 100099\n"
  189. "\t\t\t\tFast: 100100 - 400099\n"
  190. "\t\t\t\tFast+: 400100 - 10000099\n"
  191. "\t\t\t\tTurbo: Up to 1265800");
  192. module_init(pca_isa_init);
  193. module_exit(pca_isa_exit);