i2c-pca-isa.c 5.2 KB

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