/drivers/i2c/busses/i2c-parport-light.c

https://bitbucket.org/evzijst/gittest · C · 175 lines · 106 code · 32 blank · 37 comment · 18 complexity · aae3d79a3a4046f6b302431bb55a99c0 MD5 · raw file

  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/config.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/ioport.h>
  27. #include <linux/i2c.h>
  28. #include <linux/i2c-algo-bit.h>
  29. #include <asm/io.h>
  30. #include "i2c-parport.h"
  31. #define DEFAULT_BASE 0x378
  32. static u16 base;
  33. module_param(base, ushort, 0);
  34. MODULE_PARM_DESC(base, "Base I/O address");
  35. /* ----- Low-level parallel port access ----------------------------------- */
  36. static inline void port_write(unsigned char p, unsigned char d)
  37. {
  38. outb(d, base+p);
  39. }
  40. static inline unsigned char port_read(unsigned char p)
  41. {
  42. return inb(base+p);
  43. }
  44. /* ----- Unified line operation functions --------------------------------- */
  45. static inline void line_set(int state, const struct lineop *op)
  46. {
  47. u8 oldval = port_read(op->port);
  48. /* Touch only the bit(s) needed */
  49. if ((op->inverted && !state) || (!op->inverted && state))
  50. port_write(op->port, oldval | op->val);
  51. else
  52. port_write(op->port, oldval & ~op->val);
  53. }
  54. static inline int line_get(const struct lineop *op)
  55. {
  56. u8 oldval = port_read(op->port);
  57. return ((op->inverted && (oldval & op->val) != op->val)
  58. || (!op->inverted && (oldval & op->val) == op->val));
  59. }
  60. /* ----- I2C algorithm call-back functions and structures ----------------- */
  61. static void parport_setscl(void *data, int state)
  62. {
  63. line_set(state, &adapter_parm[type].setscl);
  64. }
  65. static void parport_setsda(void *data, int state)
  66. {
  67. line_set(state, &adapter_parm[type].setsda);
  68. }
  69. static int parport_getscl(void *data)
  70. {
  71. return line_get(&adapter_parm[type].getscl);
  72. }
  73. static int parport_getsda(void *data)
  74. {
  75. return line_get(&adapter_parm[type].getsda);
  76. }
  77. /* Encapsulate the functions above in the correct structure
  78. Note that getscl will be set to NULL by the attaching code for adapters
  79. that cannot read SCL back */
  80. static struct i2c_algo_bit_data parport_algo_data = {
  81. .setsda = parport_setsda,
  82. .setscl = parport_setscl,
  83. .getsda = parport_getsda,
  84. .getscl = parport_getscl,
  85. .udelay = 50,
  86. .mdelay = 50,
  87. .timeout = HZ,
  88. };
  89. /* ----- I2c structure ---------------------------------------------------- */
  90. static struct i2c_adapter parport_adapter = {
  91. .owner = THIS_MODULE,
  92. .class = I2C_CLASS_HWMON,
  93. .id = I2C_HW_B_LP,
  94. .algo_data = &parport_algo_data,
  95. .name = "Parallel port adapter (light)",
  96. };
  97. /* ----- Module loading, unloading and information ------------------------ */
  98. static int __init i2c_parport_init(void)
  99. {
  100. int type_count;
  101. type_count = sizeof(adapter_parm)/sizeof(struct adapter_parm);
  102. if (type < 0 || type >= type_count) {
  103. printk(KERN_WARNING "i2c-parport: invalid type (%d)\n", type);
  104. type = 0;
  105. }
  106. if (base == 0) {
  107. printk(KERN_INFO "i2c-parport: using default base 0x%x\n", DEFAULT_BASE);
  108. base = DEFAULT_BASE;
  109. }
  110. if (!request_region(base, 3, "i2c-parport"))
  111. return -ENODEV;
  112. if (!adapter_parm[type].getscl.val)
  113. parport_algo_data.getscl = NULL;
  114. /* Reset hardware to a sane state (SCL and SDA high) */
  115. parport_setsda(NULL, 1);
  116. parport_setscl(NULL, 1);
  117. /* Other init if needed (power on...) */
  118. if (adapter_parm[type].init.val)
  119. line_set(1, &adapter_parm[type].init);
  120. if (i2c_bit_add_bus(&parport_adapter) < 0) {
  121. printk(KERN_ERR "i2c-parport: Unable to register with I2C\n");
  122. release_region(base, 3);
  123. return -ENODEV;
  124. }
  125. return 0;
  126. }
  127. static void __exit i2c_parport_exit(void)
  128. {
  129. /* Un-init if needed (power off...) */
  130. if (adapter_parm[type].init.val)
  131. line_set(0, &adapter_parm[type].init);
  132. i2c_bit_del_bus(&parport_adapter);
  133. release_region(base, 3);
  134. }
  135. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  136. MODULE_DESCRIPTION("I2C bus over parallel port (light)");
  137. MODULE_LICENSE("GPL");
  138. module_init(i2c_parport_init);
  139. module_exit(i2c_parport_exit);