i2c-parport-light.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* ------------------------------------------------------------------------ *
  2. * i2c-parport.c I2C bus over parallel port *
  3. * ------------------------------------------------------------------------ *
  4. Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
  5. Based on older i2c-velleman.c driver
  6. Copyright (C) 1995-2000 Simon G. Vogl
  7. With some changes from:
  8. Frodo Looijaard <frodol@dds.nl>
  9. Kyösti Mälkki <kmalkki@cc.hut.fi>
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. * ------------------------------------------------------------------------ */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/ioport.h>
  26. #include <linux/i2c.h>
  27. #include <linux/i2c-algo-bit.h>
  28. #include <asm/io.h>
  29. #include "i2c-parport.h"
  30. #define DEFAULT_BASE 0x378
  31. static u16 base;
  32. module_param(base, ushort, 0);
  33. MODULE_PARM_DESC(base, "Base I/O address");
  34. /* ----- Low-level parallel port access ----------------------------------- */
  35. static inline void port_write(unsigned char p, unsigned char d)
  36. {
  37. outb(d, base+p);
  38. }
  39. static inline unsigned char port_read(unsigned char p)
  40. {
  41. return inb(base+p);
  42. }
  43. /* ----- Unified line operation functions --------------------------------- */
  44. static inline void line_set(int state, const struct lineop *op)
  45. {
  46. u8 oldval = port_read(op->port);
  47. /* Touch only the bit(s) needed */
  48. if ((op->inverted && !state) || (!op->inverted && state))
  49. port_write(op->port, oldval | op->val);
  50. else
  51. port_write(op->port, oldval & ~op->val);
  52. }
  53. static inline int line_get(const struct lineop *op)
  54. {
  55. u8 oldval = port_read(op->port);
  56. return ((op->inverted && (oldval & op->val) != op->val)
  57. || (!op->inverted && (oldval & op->val) == op->val));
  58. }
  59. /* ----- I2C algorithm call-back functions and structures ----------------- */
  60. static void parport_setscl(void *data, int state)
  61. {
  62. line_set(state, &adapter_parm[type].setscl);
  63. }
  64. static void parport_setsda(void *data, int state)
  65. {
  66. line_set(state, &adapter_parm[type].setsda);
  67. }
  68. static int parport_getscl(void *data)
  69. {
  70. return line_get(&adapter_parm[type].getscl);
  71. }
  72. static int parport_getsda(void *data)
  73. {
  74. return line_get(&adapter_parm[type].getsda);
  75. }
  76. /* Encapsulate the functions above in the correct structure
  77. Note that getscl will be set to NULL by the attaching code for adapters
  78. that cannot read SCL back */
  79. static struct i2c_algo_bit_data parport_algo_data = {
  80. .setsda = parport_setsda,
  81. .setscl = parport_setscl,
  82. .getsda = parport_getsda,
  83. .getscl = parport_getscl,
  84. .udelay = 50,
  85. .mdelay = 50,
  86. .timeout = HZ,
  87. };
  88. /* ----- I2c structure ---------------------------------------------------- */
  89. static struct i2c_adapter parport_adapter = {
  90. .owner = THIS_MODULE,
  91. .class = I2C_CLASS_HWMON,
  92. .id = I2C_HW_B_LP,
  93. .algo_data = &parport_algo_data,
  94. .name = "Parallel port adapter (light)",
  95. };
  96. /* ----- Module loading, unloading and information ------------------------ */
  97. static int __init i2c_parport_init(void)
  98. {
  99. if (type < 0) {
  100. printk(KERN_WARNING "i2c-parport: adapter type unspecified\n");
  101. return -ENODEV;
  102. }
  103. if (type >= ARRAY_SIZE(adapter_parm)) {
  104. printk(KERN_WARNING "i2c-parport: invalid type (%d)\n", type);
  105. return -ENODEV;
  106. }
  107. if (base == 0) {
  108. printk(KERN_INFO "i2c-parport: using default base 0x%x\n", DEFAULT_BASE);
  109. base = DEFAULT_BASE;
  110. }
  111. if (!request_region(base, 3, "i2c-parport"))
  112. return -ENODEV;
  113. if (!adapter_parm[type].getscl.val)
  114. parport_algo_data.getscl = NULL;
  115. /* Reset hardware to a sane state (SCL and SDA high) */
  116. parport_setsda(NULL, 1);
  117. parport_setscl(NULL, 1);
  118. /* Other init if needed (power on...) */
  119. if (adapter_parm[type].init.val)
  120. line_set(1, &adapter_parm[type].init);
  121. if (i2c_bit_add_bus(&parport_adapter) < 0) {
  122. printk(KERN_ERR "i2c-parport: Unable to register with I2C\n");
  123. release_region(base, 3);
  124. return -ENODEV;
  125. }
  126. return 0;
  127. }
  128. static void __exit i2c_parport_exit(void)
  129. {
  130. /* Un-init if needed (power off...) */
  131. if (adapter_parm[type].init.val)
  132. line_set(0, &adapter_parm[type].init);
  133. i2c_bit_del_bus(&parport_adapter);
  134. release_region(base, 3);
  135. }
  136. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  137. MODULE_DESCRIPTION("I2C bus over parallel port (light)");
  138. MODULE_LICENSE("GPL");
  139. module_init(i2c_parport_init);
  140. module_exit(i2c_parport_exit);