i2c-pca-isa.c 4.6 KB

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