i2c-parport-light.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* ------------------------------------------------------------------------ *
  2. * i2c-parport-light.c I2C bus over parallel port *
  3. * ------------------------------------------------------------------------ *
  4. Copyright (C) 2003-2007 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/platform_device.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. #define DRVNAME "i2c-parport-light"
  33. static struct platform_device *pdev;
  34. static u16 base;
  35. module_param(base, ushort, 0);
  36. MODULE_PARM_DESC(base, "Base I/O address");
  37. /* ----- Low-level parallel port access ----------------------------------- */
  38. static inline void port_write(unsigned char p, unsigned char d)
  39. {
  40. outb(d, base+p);
  41. }
  42. static inline unsigned char port_read(unsigned char p)
  43. {
  44. return inb(base+p);
  45. }
  46. /* ----- Unified line operation functions --------------------------------- */
  47. static inline void line_set(int state, const struct lineop *op)
  48. {
  49. u8 oldval = port_read(op->port);
  50. /* Touch only the bit(s) needed */
  51. if ((op->inverted && !state) || (!op->inverted && state))
  52. port_write(op->port, oldval | op->val);
  53. else
  54. port_write(op->port, oldval & ~op->val);
  55. }
  56. static inline int line_get(const struct lineop *op)
  57. {
  58. u8 oldval = port_read(op->port);
  59. return ((op->inverted && (oldval & op->val) != op->val)
  60. || (!op->inverted && (oldval & op->val) == op->val));
  61. }
  62. /* ----- I2C algorithm call-back functions and structures ----------------- */
  63. static void parport_setscl(void *data, int state)
  64. {
  65. line_set(state, &adapter_parm[type].setscl);
  66. }
  67. static void parport_setsda(void *data, int state)
  68. {
  69. line_set(state, &adapter_parm[type].setsda);
  70. }
  71. static int parport_getscl(void *data)
  72. {
  73. return line_get(&adapter_parm[type].getscl);
  74. }
  75. static int parport_getsda(void *data)
  76. {
  77. return line_get(&adapter_parm[type].getsda);
  78. }
  79. /* Encapsulate the functions above in the correct structure
  80. Note that getscl will be set to NULL by the attaching code for adapters
  81. that cannot read SCL back */
  82. static struct i2c_algo_bit_data parport_algo_data = {
  83. .setsda = parport_setsda,
  84. .setscl = parport_setscl,
  85. .getsda = parport_getsda,
  86. .getscl = parport_getscl,
  87. .udelay = 50,
  88. .timeout = HZ,
  89. };
  90. /* ----- Driver registration ---------------------------------------------- */
  91. static struct i2c_adapter parport_adapter = {
  92. .owner = THIS_MODULE,
  93. .class = I2C_CLASS_HWMON,
  94. .id = I2C_HW_B_LP,
  95. .algo_data = &parport_algo_data,
  96. .name = "Parallel port adapter (light)",
  97. };
  98. static int __devinit i2c_parport_probe(struct platform_device *pdev)
  99. {
  100. int err;
  101. /* Reset hardware to a sane state (SCL and SDA high) */
  102. parport_setsda(NULL, 1);
  103. parport_setscl(NULL, 1);
  104. /* Other init if needed (power on...) */
  105. if (adapter_parm[type].init.val)
  106. line_set(1, &adapter_parm[type].init);
  107. parport_adapter.dev.parent = &pdev->dev;
  108. err = i2c_bit_add_bus(&parport_adapter);
  109. if (err)
  110. dev_err(&pdev->dev, "Unable to register with I2C\n");
  111. return err;
  112. }
  113. static int __devexit i2c_parport_remove(struct platform_device *pdev)
  114. {
  115. i2c_del_adapter(&parport_adapter);
  116. /* Un-init if needed (power off...) */
  117. if (adapter_parm[type].init.val)
  118. line_set(0, &adapter_parm[type].init);
  119. return 0;
  120. }
  121. static struct platform_driver i2c_parport_driver = {
  122. .driver = {
  123. .owner = THIS_MODULE,
  124. .name = DRVNAME,
  125. },
  126. .probe = i2c_parport_probe,
  127. .remove = __devexit_p(i2c_parport_remove),
  128. };
  129. static int __init i2c_parport_device_add(u16 address)
  130. {
  131. int err;
  132. pdev = platform_device_alloc(DRVNAME, -1);
  133. if (!pdev) {
  134. err = -ENOMEM;
  135. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  136. goto exit;
  137. }
  138. err = platform_device_add(pdev);
  139. if (err) {
  140. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  141. err);
  142. goto exit_device_put;
  143. }
  144. return 0;
  145. exit_device_put:
  146. platform_device_put(pdev);
  147. exit:
  148. return err;
  149. }
  150. static int __init i2c_parport_init(void)
  151. {
  152. int err;
  153. if (type < 0) {
  154. printk(KERN_ERR DRVNAME ": adapter type unspecified\n");
  155. return -ENODEV;
  156. }
  157. if (type >= ARRAY_SIZE(adapter_parm)) {
  158. printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type);
  159. return -ENODEV;
  160. }
  161. if (base == 0) {
  162. pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE);
  163. base = DEFAULT_BASE;
  164. }
  165. if (!request_region(base, 3, DRVNAME))
  166. return -EBUSY;
  167. if (!adapter_parm[type].getscl.val)
  168. parport_algo_data.getscl = NULL;
  169. /* Sets global pdev as a side effect */
  170. err = i2c_parport_device_add(base);
  171. if (err)
  172. goto exit_release;
  173. err = platform_driver_register(&i2c_parport_driver);
  174. if (err)
  175. goto exit_device;
  176. return 0;
  177. exit_device:
  178. platform_device_unregister(pdev);
  179. exit_release:
  180. release_region(base, 3);
  181. return err;
  182. }
  183. static void __exit i2c_parport_exit(void)
  184. {
  185. platform_driver_unregister(&i2c_parport_driver);
  186. platform_device_unregister(pdev);
  187. release_region(base, 3);
  188. }
  189. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  190. MODULE_DESCRIPTION("I2C bus over parallel port (light)");
  191. MODULE_LICENSE("GPL");
  192. module_init(i2c_parport_init);
  193. module_exit(i2c_parport_exit);