i2c-parport-light.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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(base, ushort, 0);
  35. MODULE_PARM_DESC(base, "Base I/O address");
  36. static int irq;
  37. module_param(irq, int, 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. .alert_edge_triggered = 1,
  102. };
  103. static struct i2c_client *ara;
  104. static struct lineop parport_ctrl_irq = {
  105. .val = (1 << 4),
  106. .port = PORT_CTRL,
  107. };
  108. static int i2c_parport_probe(struct platform_device *pdev)
  109. {
  110. int err;
  111. /* Reset hardware to a sane state (SCL and SDA high) */
  112. parport_setsda(NULL, 1);
  113. parport_setscl(NULL, 1);
  114. /* Other init if needed (power on...) */
  115. if (adapter_parm[type].init.val) {
  116. line_set(1, &adapter_parm[type].init);
  117. /* Give powered devices some time to settle */
  118. msleep(100);
  119. }
  120. parport_adapter.dev.parent = &pdev->dev;
  121. err = i2c_bit_add_bus(&parport_adapter);
  122. if (err) {
  123. dev_err(&pdev->dev, "Unable to register with I2C\n");
  124. return err;
  125. }
  126. /* Setup SMBus alert if supported */
  127. if (adapter_parm[type].smbus_alert && irq) {
  128. alert_data.irq = irq;
  129. ara = i2c_setup_smbus_alert(&parport_adapter, &alert_data);
  130. if (ara)
  131. line_set(1, &parport_ctrl_irq);
  132. else
  133. dev_warn(&pdev->dev, "Failed to register ARA client\n");
  134. }
  135. return 0;
  136. }
  137. static int i2c_parport_remove(struct platform_device *pdev)
  138. {
  139. if (ara) {
  140. line_set(0, &parport_ctrl_irq);
  141. i2c_unregister_device(ara);
  142. ara = NULL;
  143. }
  144. i2c_del_adapter(&parport_adapter);
  145. /* Un-init if needed (power off...) */
  146. if (adapter_parm[type].init.val)
  147. line_set(0, &adapter_parm[type].init);
  148. return 0;
  149. }
  150. static struct platform_driver i2c_parport_driver = {
  151. .driver = {
  152. .name = DRVNAME,
  153. },
  154. .probe = i2c_parport_probe,
  155. .remove = i2c_parport_remove,
  156. };
  157. static int __init i2c_parport_device_add(u16 address)
  158. {
  159. int err;
  160. pdev = platform_device_alloc(DRVNAME, -1);
  161. if (!pdev) {
  162. err = -ENOMEM;
  163. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  164. goto exit;
  165. }
  166. err = platform_device_add(pdev);
  167. if (err) {
  168. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  169. err);
  170. goto exit_device_put;
  171. }
  172. return 0;
  173. exit_device_put:
  174. platform_device_put(pdev);
  175. exit:
  176. return err;
  177. }
  178. static int __init i2c_parport_init(void)
  179. {
  180. int err;
  181. if (type < 0) {
  182. printk(KERN_ERR DRVNAME ": adapter type unspecified\n");
  183. return -ENODEV;
  184. }
  185. if (type >= ARRAY_SIZE(adapter_parm)) {
  186. printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type);
  187. return -ENODEV;
  188. }
  189. if (base == 0) {
  190. pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE);
  191. base = DEFAULT_BASE;
  192. }
  193. if (!request_region(base, 3, DRVNAME))
  194. return -EBUSY;
  195. if (irq != 0)
  196. pr_info(DRVNAME ": using irq %d\n", irq);
  197. if (!adapter_parm[type].getscl.val)
  198. parport_algo_data.getscl = NULL;
  199. /* Sets global pdev as a side effect */
  200. err = i2c_parport_device_add(base);
  201. if (err)
  202. goto exit_release;
  203. err = platform_driver_register(&i2c_parport_driver);
  204. if (err)
  205. goto exit_device;
  206. return 0;
  207. exit_device:
  208. platform_device_unregister(pdev);
  209. exit_release:
  210. release_region(base, 3);
  211. return err;
  212. }
  213. static void __exit i2c_parport_exit(void)
  214. {
  215. platform_driver_unregister(&i2c_parport_driver);
  216. platform_device_unregister(pdev);
  217. release_region(base, 3);
  218. }
  219. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  220. MODULE_DESCRIPTION("I2C bus over parallel port (light)");
  221. MODULE_LICENSE("GPL");
  222. module_init(i2c_parport_init);
  223. module_exit(i2c_parport_exit);