i2c-pca-isa.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <asm/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. long ret = ~0;
  66. unsigned long timeout;
  67. if (irq > -1) {
  68. ret = wait_event_interruptible_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. while (((pca_isa_readbyte(pd, I2C_PCA_CON)
  75. & I2C_PCA_CON_SI) == 0)
  76. && (ret = time_before(jiffies, timeout)))
  77. udelay(100);
  78. }
  79. return ret > 0;
  80. }
  81. static void pca_isa_resetchip(void *pd)
  82. {
  83. /* apparently only an external reset will do it. not a lot can be done */
  84. printk(KERN_WARNING DRIVER ": Haven't figured out how to do a reset yet\n");
  85. }
  86. static irqreturn_t pca_handler(int this_irq, void *dev_id) {
  87. wake_up_interruptible(&pca_wait);
  88. return IRQ_HANDLED;
  89. }
  90. static struct i2c_algo_pca_data pca_isa_data = {
  91. /* .data intentionally left NULL, not needed with ISA */
  92. .write_byte = pca_isa_writebyte,
  93. .read_byte = pca_isa_readbyte,
  94. .wait_for_completion = pca_isa_waitforcompletion,
  95. .reset_chip = pca_isa_resetchip,
  96. };
  97. static struct i2c_adapter pca_isa_ops = {
  98. .owner = THIS_MODULE,
  99. .algo_data = &pca_isa_data,
  100. .name = "PCA9564/PCA9665 ISA Adapter",
  101. .timeout = HZ,
  102. };
  103. static int __devinit pca_isa_match(struct device *dev, unsigned int id)
  104. {
  105. int match = base != 0;
  106. if (match) {
  107. if (irq <= -1)
  108. dev_warn(dev, "Using polling mode (specify irq)\n");
  109. } else
  110. dev_err(dev, "Please specify I/O base\n");
  111. return match;
  112. }
  113. static int __devinit pca_isa_probe(struct device *dev, unsigned int id)
  114. {
  115. init_waitqueue_head(&pca_wait);
  116. dev_info(dev, "i/o base %#08lx. irq %d\n", base, irq);
  117. #ifdef CONFIG_PPC
  118. if (check_legacy_ioport(base)) {
  119. dev_err(dev, "I/O address %#08lx is not available\n", base);
  120. goto out;
  121. }
  122. #endif
  123. if (!request_region(base, IO_SIZE, "i2c-pca-isa")) {
  124. dev_err(dev, "I/O address %#08lx is in use\n", base);
  125. goto out;
  126. }
  127. if (irq > -1) {
  128. if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) {
  129. dev_err(dev, "Request irq%d failed\n", irq);
  130. goto out_region;
  131. }
  132. }
  133. pca_isa_data.i2c_clock = clock;
  134. if (i2c_pca_add_bus(&pca_isa_ops) < 0) {
  135. dev_err(dev, "Failed to add i2c bus\n");
  136. goto out_irq;
  137. }
  138. return 0;
  139. out_irq:
  140. if (irq > -1)
  141. free_irq(irq, &pca_isa_ops);
  142. out_region:
  143. release_region(base, IO_SIZE);
  144. out:
  145. return -ENODEV;
  146. }
  147. static int __devexit pca_isa_remove(struct device *dev, unsigned int id)
  148. {
  149. i2c_del_adapter(&pca_isa_ops);
  150. if (irq > -1) {
  151. disable_irq(irq);
  152. free_irq(irq, &pca_isa_ops);
  153. }
  154. release_region(base, IO_SIZE);
  155. return 0;
  156. }
  157. static struct isa_driver pca_isa_driver = {
  158. .match = pca_isa_match,
  159. .probe = pca_isa_probe,
  160. .remove = __devexit_p(pca_isa_remove),
  161. .driver = {
  162. .owner = THIS_MODULE,
  163. .name = DRIVER,
  164. }
  165. };
  166. static int __init pca_isa_init(void)
  167. {
  168. return isa_register_driver(&pca_isa_driver, 1);
  169. }
  170. static void __exit pca_isa_exit(void)
  171. {
  172. isa_unregister_driver(&pca_isa_driver);
  173. }
  174. MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
  175. MODULE_DESCRIPTION("ISA base PCA9564/PCA9665 driver");
  176. MODULE_LICENSE("GPL");
  177. module_param(base, ulong, 0);
  178. MODULE_PARM_DESC(base, "I/O base address");
  179. module_param(irq, int, 0);
  180. MODULE_PARM_DESC(irq, "IRQ");
  181. module_param(clock, int, 0);
  182. MODULE_PARM_DESC(clock, "Clock rate in hertz.\n\t\t"
  183. "For PCA9564: 330000,288000,217000,146000,"
  184. "88000,59000,44000,36000\n"
  185. "\t\tFor PCA9665:\tStandard: 60300 - 100099\n"
  186. "\t\t\t\tFast: 100100 - 400099\n"
  187. "\t\t\t\tFast+: 400100 - 10000099\n"
  188. "\t\t\t\tTurbo: Up to 1265800");
  189. module_init(pca_isa_init);
  190. module_exit(pca_isa_exit);