i2c-pca-isa.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/i2c.h>
  30. #include <linux/i2c-algo-pca.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #include "../algos/i2c-algo-pca.h"
  34. #define IO_SIZE 4
  35. #undef DEBUG_IO
  36. //#define DEBUG_IO
  37. static unsigned long base = 0x330;
  38. static int irq = 10;
  39. /* Data sheet recommends 59kHz for 100kHz operation due to variation
  40. * in the actual clock rate */
  41. static int clock = I2C_PCA_CON_59kHz;
  42. static int own = 0x55;
  43. static wait_queue_head_t pca_wait;
  44. static int pca_isa_getown(struct i2c_algo_pca_data *adap)
  45. {
  46. return (own);
  47. }
  48. static int pca_isa_getclock(struct i2c_algo_pca_data *adap)
  49. {
  50. return (clock);
  51. }
  52. static void
  53. pca_isa_writebyte(struct i2c_algo_pca_data *adap, int reg, int val)
  54. {
  55. #ifdef DEBUG_IO
  56. static char *names[] = { "T/O", "DAT", "ADR", "CON" };
  57. printk("*** write %s at %#lx <= %#04x\n", names[reg], base+reg, val);
  58. #endif
  59. outb(val, base+reg);
  60. }
  61. static int
  62. pca_isa_readbyte(struct i2c_algo_pca_data *adap, int reg)
  63. {
  64. int res = inb(base+reg);
  65. #ifdef DEBUG_IO
  66. {
  67. static char *names[] = { "STA", "DAT", "ADR", "CON" };
  68. printk("*** read %s => %#04x\n", names[reg], res);
  69. }
  70. #endif
  71. return res;
  72. }
  73. static int pca_isa_waitforinterrupt(struct i2c_algo_pca_data *adap)
  74. {
  75. int ret = 0;
  76. if (irq > -1) {
  77. ret = wait_event_interruptible(pca_wait,
  78. pca_isa_readbyte(adap, I2C_PCA_CON) & I2C_PCA_CON_SI);
  79. } else {
  80. while ((pca_isa_readbyte(adap, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
  81. udelay(100);
  82. }
  83. return ret;
  84. }
  85. static irqreturn_t pca_handler(int this_irq, void *dev_id, struct pt_regs *regs) {
  86. wake_up_interruptible(&pca_wait);
  87. return IRQ_HANDLED;
  88. }
  89. static struct i2c_algo_pca_data pca_isa_data = {
  90. .get_own = pca_isa_getown,
  91. .get_clock = pca_isa_getclock,
  92. .write_byte = pca_isa_writebyte,
  93. .read_byte = pca_isa_readbyte,
  94. .wait_for_interrupt = pca_isa_waitforinterrupt,
  95. };
  96. static struct i2c_adapter pca_isa_ops = {
  97. .owner = THIS_MODULE,
  98. .id = I2C_HW_A_ISA,
  99. .algo_data = &pca_isa_data,
  100. .name = "PCA9564 ISA Adapter",
  101. };
  102. static int __init pca_isa_init(void)
  103. {
  104. init_waitqueue_head(&pca_wait);
  105. printk(KERN_INFO "i2c-pca-isa: i/o base %#08lx. irq %d\n", base, irq);
  106. if (!request_region(base, IO_SIZE, "i2c-pca-isa")) {
  107. printk(KERN_ERR "i2c-pca-isa: I/O address %#08lx is in use.\n", base);
  108. goto out;
  109. }
  110. if (irq > -1) {
  111. if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) {
  112. printk(KERN_ERR "i2c-pca-isa: Request irq%d failed\n", irq);
  113. goto out_region;
  114. }
  115. }
  116. if (i2c_pca_add_bus(&pca_isa_ops) < 0) {
  117. printk(KERN_ERR "i2c-pca-isa: 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 void pca_isa_exit(void)
  130. {
  131. i2c_pca_del_bus(&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. }
  138. MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
  139. MODULE_DESCRIPTION("ISA base PCA9564 driver");
  140. MODULE_LICENSE("GPL");
  141. module_param(base, ulong, 0);
  142. MODULE_PARM_DESC(base, "I/O base address");
  143. module_param(irq, int, 0);
  144. MODULE_PARM_DESC(irq, "IRQ");
  145. module_param(clock, int, 0);
  146. MODULE_PARM_DESC(clock, "Clock rate as described in table 1 of PCA9564 datasheet");
  147. module_param(own, int, 0); /* the driver can't do slave mode, so there's no real point in this */
  148. module_init(pca_isa_init);
  149. module_exit(pca_isa_exit);