i2c-pca-isa.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/wait.h>
  28. #include <linux/isa.h>
  29. #include <linux/i2c.h>
  30. #include <linux/i2c-algo-pca.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #define DRIVER "i2c-pca-isa"
  34. #define IO_SIZE 4
  35. static unsigned long base = 0x330;
  36. static int irq = 10;
  37. /* Data sheet recommends 59kHz for 100kHz operation due to variation
  38. * in the actual clock rate */
  39. static int clock = I2C_PCA_CON_59kHz;
  40. static wait_queue_head_t pca_wait;
  41. static void pca_isa_writebyte(void *pd, int reg, int val)
  42. {
  43. #ifdef DEBUG_IO
  44. static char *names[] = { "T/O", "DAT", "ADR", "CON" };
  45. printk("*** write %s at %#lx <= %#04x\n", names[reg], base+reg, val);
  46. #endif
  47. outb(val, base+reg);
  48. }
  49. static int pca_isa_readbyte(void *pd, int reg)
  50. {
  51. int res = inb(base+reg);
  52. #ifdef DEBUG_IO
  53. {
  54. static char *names[] = { "STA", "DAT", "ADR", "CON" };
  55. printk("*** read %s => %#04x\n", names[reg], res);
  56. }
  57. #endif
  58. return res;
  59. }
  60. static int pca_isa_waitforcompletion(void *pd)
  61. {
  62. int ret = 0;
  63. if (irq > -1) {
  64. ret = wait_event_interruptible(pca_wait,
  65. pca_isa_readbyte(pd, I2C_PCA_CON) & I2C_PCA_CON_SI);
  66. } else {
  67. while ((pca_isa_readbyte(pd, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
  68. udelay(100);
  69. }
  70. return ret;
  71. }
  72. static void pca_isa_resetchip(void *pd)
  73. {
  74. /* apparently only an external reset will do it. not a lot can be done */
  75. printk(KERN_WARNING DRIVER ": Haven't figured out how to do a reset yet\n");
  76. }
  77. static irqreturn_t pca_handler(int this_irq, void *dev_id) {
  78. wake_up_interruptible(&pca_wait);
  79. return IRQ_HANDLED;
  80. }
  81. static struct i2c_algo_pca_data pca_isa_data = {
  82. /* .data intentionally left NULL, not needed with ISA */
  83. .write_byte = pca_isa_writebyte,
  84. .read_byte = pca_isa_readbyte,
  85. .wait_for_completion = pca_isa_waitforcompletion,
  86. .reset_chip = pca_isa_resetchip,
  87. };
  88. static struct i2c_adapter pca_isa_ops = {
  89. .owner = THIS_MODULE,
  90. .id = I2C_HW_A_ISA,
  91. .algo_data = &pca_isa_data,
  92. .name = "PCA9564 ISA Adapter",
  93. .timeout = 100,
  94. };
  95. static int __devinit pca_isa_probe(struct device *dev, unsigned int id)
  96. {
  97. init_waitqueue_head(&pca_wait);
  98. dev_info(dev, "i/o base %#08lx. irq %d\n", base, irq);
  99. #ifdef CONFIG_PPC_MERGE
  100. if (check_legacy_ioport(base)) {
  101. dev_err(dev, "I/O address %#08lx is not available\n", base);
  102. goto out;
  103. }
  104. #endif
  105. if (!request_region(base, IO_SIZE, "i2c-pca-isa")) {
  106. dev_err(dev, "I/O address %#08lx is in use\n", base);
  107. goto out;
  108. }
  109. if (irq > -1) {
  110. if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) {
  111. dev_err(dev, "Request irq%d failed\n", irq);
  112. goto out_region;
  113. }
  114. }
  115. pca_isa_data.i2c_clock = clock;
  116. if (i2c_pca_add_bus(&pca_isa_ops) < 0) {
  117. dev_err(dev, "Failed to add i2c bus\n");
  118. goto out_irq;
  119. }
  120. return 0;
  121. out_irq:
  122. if (irq > -1)
  123. free_irq(irq, &pca_isa_ops);
  124. out_region:
  125. release_region(base, IO_SIZE);
  126. out:
  127. return -ENODEV;
  128. }
  129. static int __devexit pca_isa_remove(struct device *dev, unsigned int id)
  130. {
  131. i2c_del_adapter(&pca_isa_ops);
  132. if (irq > 0) {
  133. disable_irq(irq);
  134. free_irq(irq, &pca_isa_ops);
  135. }
  136. release_region(base, IO_SIZE);
  137. return 0;
  138. }
  139. static struct isa_driver pca_isa_driver = {
  140. .probe = pca_isa_probe,
  141. .remove = __devexit_p(pca_isa_remove),
  142. .driver = {
  143. .owner = THIS_MODULE,
  144. .name = DRIVER,
  145. }
  146. };
  147. static int __init pca_isa_init(void)
  148. {
  149. return isa_register_driver(&pca_isa_driver, 1);
  150. }
  151. static void __exit pca_isa_exit(void)
  152. {
  153. isa_unregister_driver(&pca_isa_driver);
  154. }
  155. MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
  156. MODULE_DESCRIPTION("ISA base PCA9564 driver");
  157. MODULE_LICENSE("GPL");
  158. module_param(base, ulong, 0);
  159. MODULE_PARM_DESC(base, "I/O base address");
  160. module_param(irq, int, 0);
  161. MODULE_PARM_DESC(irq, "IRQ");
  162. module_param(clock, int, 0);
  163. MODULE_PARM_DESC(clock, "Clock rate as described in table 1 of PCA9564 datasheet");
  164. module_init(pca_isa_init);
  165. module_exit(pca_isa_exit);