i2c-parport-light.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* ------------------------------------------------------------------------ *
  2. * i2c-parport-light.c I2C bus over parallel port *
  3. * ------------------------------------------------------------------------ *
  4. Copyright (C) 2003-2010 Jean Delvare <jdelvare@suse.de>
  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. * ------------------------------------------------------------------------ */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/ioport.h>
  25. #include <linux/i2c.h>
  26. #include <linux/i2c-algo-bit.h>
  27. #include <linux/i2c-smbus.h>
  28. #include <linux/io.h>
  29. #include "i2c-parport.h"
  30. #define DEFAULT_BASE 0x378
  31. #define DRVNAME "i2c-parport-light"
  32. static struct platform_device *pdev;
  33. static u16 base;
  34. module_param_hw(base, ushort, ioport, 0);
  35. MODULE_PARM_DESC(base, "Base I/O address");
  36. static int irq;
  37. module_param_hw(irq, int, irq, 0);
  38. MODULE_PARM_DESC(irq, "IRQ (optional)");
  39. /* ----- Low-level parallel port access ----------------------------------- */
  40. static inline void port_write(unsigned char p, unsigned char d)
  41. {
  42. outb(d, base+p);
  43. }
  44. static inline unsigned char port_read(unsigned char p)
  45. {
  46. return inb(base+p);
  47. }
  48. /* ----- Unified line operation functions --------------------------------- */
  49. static inline void line_set(int state, const struct lineop *op)
  50. {
  51. u8 oldval = port_read(op->port);
  52. /* Touch only the bit(s) needed */
  53. if ((op->inverted && !state) || (!op->inverted && state))
  54. port_write(op->port, oldval | op->val);
  55. else
  56. port_write(op->port, oldval & ~op->val);
  57. }
  58. static inline int line_get(const struct lineop *op)
  59. {
  60. u8 oldval = port_read(op->port);
  61. return ((op->inverted && (oldval & op->val) != op->val)
  62. || (!op->inverted && (oldval & op->val) == op->val));
  63. }
  64. /* ----- I2C algorithm call-back functions and structures ----------------- */
  65. static void parport_setscl(void *data, int state)
  66. {
  67. line_set(state, &adapter_parm[type].setscl);
  68. }
  69. static void parport_setsda(void *data, int state)
  70. {
  71. line_set(state, &adapter_parm[type].setsda);
  72. }
  73. static int parport_getscl(void *data)
  74. {
  75. return line_get(&adapter_parm[type].getscl);
  76. }
  77. static int parport_getsda(void *data)
  78. {
  79. return line_get(&adapter_parm[type].getsda);
  80. }
  81. /* Encapsulate the functions above in the correct structure
  82. Note that getscl will be set to NULL by the attaching code for adapters
  83. that cannot read SCL back */
  84. static struct i2c_algo_bit_data parport_algo_data = {
  85. .setsda = parport_setsda,
  86. .setscl = parport_setscl,
  87. .getsda = parport_getsda,
  88. .getscl = parport_getscl,
  89. .udelay = 50,
  90. .timeout = HZ,
  91. };
  92. /* ----- Driver registration ---------------------------------------------- */
  93. static struct i2c_adapter parport_adapter = {
  94. .owner = THIS_MODULE,
  95. .class = I2C_CLASS_HWMON,
  96. .algo_data = &parport_algo_data,
  97. .name = "Parallel port adapter (light)",
  98. };
  99. /* SMBus alert support */
  100. static struct i2c_smbus_alert_setup alert_data = {
  101. };
  102. static struct i2c_client *ara;
  103. static struct lineop parport_ctrl_irq = {
  104. .val = (1 << 4),
  105. .port = PORT_CTRL,
  106. };
  107. static int i2c_parport_probe(struct platform_device *pdev)
  108. {
  109. int err;
  110. /* Reset hardware to a sane state (SCL and SDA high) */
  111. parport_setsda(NULL, 1);
  112. parport_setscl(NULL, 1);
  113. /* Other init if needed (power on...) */
  114. if (adapter_parm[type].init.val) {
  115. line_set(1, &adapter_parm[type].init);
  116. /* Give powered devices some time to settle */
  117. msleep(100);
  118. }
  119. parport_adapter.dev.parent = &pdev->dev;
  120. err = i2c_bit_add_bus(&parport_adapter);
  121. if (err) {
  122. dev_err(&pdev->dev, "Unable to register with I2C\n");
  123. return err;
  124. }
  125. /* Setup SMBus alert if supported */
  126. if (adapter_parm[type].smbus_alert && irq) {
  127. alert_data.irq = irq;
  128. ara = i2c_setup_smbus_alert(&parport_adapter, &alert_data);
  129. if (ara)
  130. line_set(1, &parport_ctrl_irq);
  131. else
  132. dev_warn(&pdev->dev, "Failed to register ARA client\n");
  133. }
  134. return 0;
  135. }
  136. static int i2c_parport_remove(struct platform_device *pdev)
  137. {
  138. if (ara) {
  139. line_set(0, &parport_ctrl_irq);
  140. i2c_unregister_device(ara);
  141. ara = NULL;
  142. }
  143. i2c_del_adapter(&parport_adapter);
  144. /* Un-init if needed (power off...) */
  145. if (adapter_parm[type].init.val)
  146. line_set(0, &adapter_parm[type].init);
  147. return 0;
  148. }
  149. static struct platform_driver i2c_parport_driver = {
  150. .driver = {
  151. .name = DRVNAME,
  152. },
  153. .probe = i2c_parport_probe,
  154. .remove = i2c_parport_remove,
  155. };
  156. static int __init i2c_parport_device_add(u16 address)
  157. {
  158. int err;
  159. pdev = platform_device_alloc(DRVNAME, -1);
  160. if (!pdev) {
  161. err = -ENOMEM;
  162. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  163. goto exit;
  164. }
  165. err = platform_device_add(pdev);
  166. if (err) {
  167. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  168. err);
  169. goto exit_device_put;
  170. }
  171. return 0;
  172. exit_device_put:
  173. platform_device_put(pdev);
  174. exit:
  175. return err;
  176. }
  177. static int __init i2c_parport_init(void)
  178. {
  179. int err;
  180. if (type < 0) {
  181. printk(KERN_ERR DRVNAME ": adapter type unspecified\n");
  182. return -ENODEV;
  183. }
  184. if (type >= ARRAY_SIZE(adapter_parm)) {
  185. printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type);
  186. return -ENODEV;
  187. }
  188. if (base == 0) {
  189. pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE);
  190. base = DEFAULT_BASE;
  191. }
  192. if (!request_region(base, 3, DRVNAME))
  193. return -EBUSY;
  194. if (irq != 0)
  195. pr_info(DRVNAME ": using irq %d\n", irq);
  196. if (!adapter_parm[type].getscl.val)
  197. parport_algo_data.getscl = NULL;
  198. /* Sets global pdev as a side effect */
  199. err = i2c_parport_device_add(base);
  200. if (err)
  201. goto exit_release;
  202. err = platform_driver_register(&i2c_parport_driver);
  203. if (err)
  204. goto exit_device;
  205. return 0;
  206. exit_device:
  207. platform_device_unregister(pdev);
  208. exit_release:
  209. release_region(base, 3);
  210. return err;
  211. }
  212. static void __exit i2c_parport_exit(void)
  213. {
  214. platform_driver_unregister(&i2c_parport_driver);
  215. platform_device_unregister(pdev);
  216. release_region(base, 3);
  217. }
  218. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  219. MODULE_DESCRIPTION("I2C bus over parallel port (light)");
  220. MODULE_LICENSE("GPL");
  221. module_init(i2c_parport_init);
  222. module_exit(i2c_parport_exit);