i2c-parport-light.c 6.9 KB

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