i2c-pca-isa.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * i2c-pca-isa.c driver for PCA9564 on ISA boards
  3. * Copyright (C) 2004 Arcom Control Systems
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/ioport.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/pci.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. #include "../algos/i2c-algo-pca.h"
  35. #define IO_SIZE 4
  36. #undef DEBUG_IO
  37. //#define DEBUG_IO
  38. static unsigned long base = 0x330;
  39. static int irq = 10;
  40. /* Data sheet recommends 59kHz for 100kHz operation due to variation
  41. * in the actual clock rate */
  42. static int clock = I2C_PCA_CON_59kHz;
  43. static int own = 0x55;
  44. static wait_queue_head_t pca_wait;
  45. static int pca_isa_getown(struct i2c_algo_pca_data *adap)
  46. {
  47. return (own);
  48. }
  49. static int pca_isa_getclock(struct i2c_algo_pca_data *adap)
  50. {
  51. return (clock);
  52. }
  53. static void
  54. pca_isa_writebyte(struct i2c_algo_pca_data *adap, int reg, int val)
  55. {
  56. #ifdef DEBUG_IO
  57. static char *names[] = { "T/O", "DAT", "ADR", "CON" };
  58. printk("*** write %s at %#lx <= %#04x\n", names[reg], base+reg, val);
  59. #endif
  60. outb(val, base+reg);
  61. }
  62. static int
  63. pca_isa_readbyte(struct i2c_algo_pca_data *adap, int reg)
  64. {
  65. int res = inb(base+reg);
  66. #ifdef DEBUG_IO
  67. {
  68. static char *names[] = { "STA", "DAT", "ADR", "CON" };
  69. printk("*** read %s => %#04x\n", names[reg], res);
  70. }
  71. #endif
  72. return res;
  73. }
  74. static int pca_isa_waitforinterrupt(struct i2c_algo_pca_data *adap)
  75. {
  76. int ret = 0;
  77. if (irq > -1) {
  78. ret = wait_event_interruptible(pca_wait,
  79. pca_isa_readbyte(adap, I2C_PCA_CON) & I2C_PCA_CON_SI);
  80. } else {
  81. while ((pca_isa_readbyte(adap, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
  82. udelay(100);
  83. }
  84. return ret;
  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. .get_own = pca_isa_getown,
  92. .get_clock = pca_isa_getclock,
  93. .write_byte = pca_isa_writebyte,
  94. .read_byte = pca_isa_readbyte,
  95. .wait_for_interrupt = pca_isa_waitforinterrupt,
  96. };
  97. static struct i2c_adapter pca_isa_ops = {
  98. .owner = THIS_MODULE,
  99. .id = I2C_HW_A_ISA,
  100. .algo_data = &pca_isa_data,
  101. .name = "PCA9564 ISA Adapter",
  102. };
  103. static int __devinit pca_isa_probe(struct device *dev, unsigned int id)
  104. {
  105. init_waitqueue_head(&pca_wait);
  106. dev_info(dev, "i/o base %#08lx. irq %d\n", base, irq);
  107. if (!request_region(base, IO_SIZE, "i2c-pca-isa")) {
  108. dev_err(dev, "I/O address %#08lx is in use\n", base);
  109. goto out;
  110. }
  111. if (irq > -1) {
  112. if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) {
  113. dev_err(dev, "Request irq%d failed\n", irq);
  114. goto out_region;
  115. }
  116. }
  117. if (i2c_pca_add_bus(&pca_isa_ops) < 0) {
  118. dev_err(dev, "Failed to add i2c bus\n");
  119. goto out_irq;
  120. }
  121. return 0;
  122. out_irq:
  123. if (irq > -1)
  124. free_irq(irq, &pca_isa_ops);
  125. out_region:
  126. release_region(base, IO_SIZE);
  127. out:
  128. return -ENODEV;
  129. }
  130. static int __devexit pca_isa_remove(struct device *dev, unsigned int id)
  131. {
  132. i2c_del_adapter(&pca_isa_ops);
  133. if (irq > 0) {
  134. disable_irq(irq);
  135. free_irq(irq, &pca_isa_ops);
  136. }
  137. release_region(base, IO_SIZE);
  138. return 0;
  139. }
  140. static struct isa_driver pca_isa_driver = {
  141. .probe = pca_isa_probe,
  142. .remove = __devexit_p(pca_isa_remove),
  143. .driver = {
  144. .owner = THIS_MODULE,
  145. .name = "i2c-pca-isa",
  146. }
  147. };
  148. static int __init pca_isa_init(void)
  149. {
  150. return isa_register_driver(&pca_isa_driver, 1);
  151. }
  152. static void __exit pca_isa_exit(void)
  153. {
  154. isa_unregister_driver(&pca_isa_driver);
  155. }
  156. MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
  157. MODULE_DESCRIPTION("ISA base PCA9564 driver");
  158. MODULE_LICENSE("GPL");
  159. module_param(base, ulong, 0);
  160. MODULE_PARM_DESC(base, "I/O base address");
  161. module_param(irq, int, 0);
  162. MODULE_PARM_DESC(irq, "IRQ");
  163. module_param(clock, int, 0);
  164. MODULE_PARM_DESC(clock, "Clock rate as described in table 1 of PCA9564 datasheet");
  165. module_param(own, int, 0); /* the driver can't do slave mode, so there's no real point in this */
  166. module_init(pca_isa_init);
  167. module_exit(pca_isa_exit);