i2c-parport-light.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. struct resource *res;
  102. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  103. if (!request_region(res->start, res->end - res->start + 1, DRVNAME))
  104. return -EBUSY;
  105. /* Reset hardware to a sane state (SCL and SDA high) */
  106. parport_setsda(NULL, 1);
  107. parport_setscl(NULL, 1);
  108. /* Other init if needed (power on...) */
  109. if (adapter_parm[type].init.val)
  110. line_set(1, &adapter_parm[type].init);
  111. parport_adapter.dev.parent = &pdev->dev;
  112. err = i2c_bit_add_bus(&parport_adapter);
  113. if (err) {
  114. dev_err(&pdev->dev, "Unable to register with I2C\n");
  115. goto exit_region;
  116. }
  117. return 0;
  118. exit_region:
  119. release_region(res->start, res->end - res->start + 1);
  120. return err;
  121. }
  122. static int __devexit i2c_parport_remove(struct platform_device *pdev)
  123. {
  124. struct resource *res;
  125. i2c_del_adapter(&parport_adapter);
  126. /* Un-init if needed (power off...) */
  127. if (adapter_parm[type].init.val)
  128. line_set(0, &adapter_parm[type].init);
  129. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  130. release_region(res->start, res->end - res->start + 1);
  131. return 0;
  132. }
  133. static struct platform_driver i2c_parport_driver = {
  134. .driver = {
  135. .owner = THIS_MODULE,
  136. .name = DRVNAME,
  137. },
  138. .probe = i2c_parport_probe,
  139. .remove = __devexit_p(i2c_parport_remove),
  140. };
  141. static int __init i2c_parport_device_add(u16 address)
  142. {
  143. struct resource res = {
  144. .start = address,
  145. .end = address + 2,
  146. .name = DRVNAME,
  147. .flags = IORESOURCE_IO,
  148. };
  149. int err;
  150. pdev = platform_device_alloc(DRVNAME, -1);
  151. if (!pdev) {
  152. err = -ENOMEM;
  153. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  154. goto exit;
  155. }
  156. err = platform_device_add_resources(pdev, &res, 1);
  157. if (err) {
  158. printk(KERN_ERR DRVNAME ": Device resource addition failed "
  159. "(%d)\n", err);
  160. goto exit_device_put;
  161. }
  162. err = platform_device_add(pdev);
  163. if (err) {
  164. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  165. err);
  166. goto exit_device_put;
  167. }
  168. return 0;
  169. exit_device_put:
  170. platform_device_put(pdev);
  171. exit:
  172. return err;
  173. }
  174. static int __init i2c_parport_init(void)
  175. {
  176. int err;
  177. if (type < 0) {
  178. printk(KERN_ERR DRVNAME ": adapter type unspecified\n");
  179. return -ENODEV;
  180. }
  181. if (type >= ARRAY_SIZE(adapter_parm)) {
  182. printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type);
  183. return -ENODEV;
  184. }
  185. if (base == 0) {
  186. pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE);
  187. base = DEFAULT_BASE;
  188. }
  189. if (!adapter_parm[type].getscl.val)
  190. parport_algo_data.getscl = NULL;
  191. /* Sets global pdev as a side effect */
  192. err = i2c_parport_device_add(base);
  193. if (err)
  194. goto exit;
  195. err = platform_driver_register(&i2c_parport_driver);
  196. if (err)
  197. goto exit_device;
  198. return 0;
  199. exit_device:
  200. platform_device_unregister(pdev);
  201. exit:
  202. return err;
  203. }
  204. static void __exit i2c_parport_exit(void)
  205. {
  206. platform_driver_unregister(&i2c_parport_driver);
  207. platform_device_unregister(pdev);
  208. }
  209. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  210. MODULE_DESCRIPTION("I2C bus over parallel port (light)");
  211. MODULE_LICENSE("GPL");
  212. module_init(i2c_parport_init);
  213. module_exit(i2c_parport_exit);