| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------ * | 
|  | 2 | * i2c-parport.c I2C bus over parallel port                                 * | 
|  | 3 | * ------------------------------------------------------------------------ * | 
|  | 4 | Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org> | 
|  | 5 |  | 
|  | 6 | Based on older i2c-velleman.c driver | 
|  | 7 | Copyright (C) 1995-2000 Simon G. Vogl | 
|  | 8 | With some changes from: | 
|  | 9 | Frodo Looijaard <frodol@dds.nl> | 
|  | 10 | Kyösti Mälkki <kmalkki@cc.hut.fi> | 
|  | 11 |  | 
|  | 12 | This program is free software; you can redistribute it and/or modify | 
|  | 13 | it under the terms of the GNU General Public License as published by | 
|  | 14 | the Free Software Foundation; either version 2 of the License, or | 
|  | 15 | (at your option) any later version. | 
|  | 16 |  | 
|  | 17 | This program is distributed in the hope that it will be useful, | 
|  | 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 20 | GNU General Public License for more details. | 
|  | 21 |  | 
|  | 22 | You should have received a copy of the GNU General Public License | 
|  | 23 | along with this program; if not, write to the Free Software | 
|  | 24 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 25 | * ------------------------------------------------------------------------ */ | 
|  | 26 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/kernel.h> | 
|  | 28 | #include <linux/module.h> | 
|  | 29 | #include <linux/init.h> | 
|  | 30 | #include <linux/ioport.h> | 
|  | 31 | #include <linux/i2c.h> | 
|  | 32 | #include <linux/i2c-algo-bit.h> | 
|  | 33 | #include <asm/io.h> | 
|  | 34 | #include "i2c-parport.h" | 
|  | 35 |  | 
|  | 36 | #define DEFAULT_BASE 0x378 | 
|  | 37 |  | 
|  | 38 | static u16 base; | 
|  | 39 | module_param(base, ushort, 0); | 
|  | 40 | MODULE_PARM_DESC(base, "Base I/O address"); | 
|  | 41 |  | 
|  | 42 | /* ----- Low-level parallel port access ----------------------------------- */ | 
|  | 43 |  | 
|  | 44 | static inline void port_write(unsigned char p, unsigned char d) | 
|  | 45 | { | 
|  | 46 | outb(d, base+p); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | static inline unsigned char port_read(unsigned char p) | 
|  | 50 | { | 
|  | 51 | return inb(base+p); | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | /* ----- Unified line operation functions --------------------------------- */ | 
|  | 55 |  | 
|  | 56 | static inline void line_set(int state, const struct lineop *op) | 
|  | 57 | { | 
|  | 58 | u8 oldval = port_read(op->port); | 
|  | 59 |  | 
|  | 60 | /* Touch only the bit(s) needed */ | 
|  | 61 | if ((op->inverted && !state) || (!op->inverted && state)) | 
|  | 62 | port_write(op->port, oldval | op->val); | 
|  | 63 | else | 
|  | 64 | port_write(op->port, oldval & ~op->val); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | static inline int line_get(const struct lineop *op) | 
|  | 68 | { | 
|  | 69 | u8 oldval = port_read(op->port); | 
|  | 70 |  | 
|  | 71 | return ((op->inverted && (oldval & op->val) != op->val) | 
|  | 72 | || (!op->inverted && (oldval & op->val) == op->val)); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | /* ----- I2C algorithm call-back functions and structures ----------------- */ | 
|  | 76 |  | 
|  | 77 | static void parport_setscl(void *data, int state) | 
|  | 78 | { | 
|  | 79 | line_set(state, &adapter_parm[type].setscl); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | static void parport_setsda(void *data, int state) | 
|  | 83 | { | 
|  | 84 | line_set(state, &adapter_parm[type].setsda); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | static int parport_getscl(void *data) | 
|  | 88 | { | 
|  | 89 | return line_get(&adapter_parm[type].getscl); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | static int parport_getsda(void *data) | 
|  | 93 | { | 
|  | 94 | return line_get(&adapter_parm[type].getsda); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | /* Encapsulate the functions above in the correct structure | 
|  | 98 | Note that getscl will be set to NULL by the attaching code for adapters | 
|  | 99 | that cannot read SCL back */ | 
|  | 100 | static struct i2c_algo_bit_data parport_algo_data = { | 
|  | 101 | .setsda		= parport_setsda, | 
|  | 102 | .setscl		= parport_setscl, | 
|  | 103 | .getsda		= parport_getsda, | 
|  | 104 | .getscl		= parport_getscl, | 
|  | 105 | .udelay		= 50, | 
|  | 106 | .mdelay		= 50, | 
|  | 107 | .timeout	= HZ, | 
|  | 108 | }; | 
|  | 109 |  | 
|  | 110 | /* ----- I2c structure ---------------------------------------------------- */ | 
|  | 111 |  | 
|  | 112 | static struct i2c_adapter parport_adapter = { | 
|  | 113 | .owner		= THIS_MODULE, | 
|  | 114 | .class		= I2C_CLASS_HWMON, | 
|  | 115 | .id		= I2C_HW_B_LP, | 
|  | 116 | .algo_data	= &parport_algo_data, | 
|  | 117 | .name		= "Parallel port adapter (light)", | 
|  | 118 | }; | 
|  | 119 |  | 
|  | 120 | /* ----- Module loading, unloading and information ------------------------ */ | 
|  | 121 |  | 
|  | 122 | static int __init i2c_parport_init(void) | 
|  | 123 | { | 
|  | 124 | int type_count; | 
|  | 125 |  | 
|  | 126 | type_count = sizeof(adapter_parm)/sizeof(struct adapter_parm); | 
|  | 127 | if (type < 0 || type >= type_count) { | 
|  | 128 | printk(KERN_WARNING "i2c-parport: invalid type (%d)\n", type); | 
|  | 129 | type = 0; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | if (base == 0) { | 
|  | 133 | printk(KERN_INFO "i2c-parport: using default base 0x%x\n", DEFAULT_BASE); | 
|  | 134 | base = DEFAULT_BASE; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | if (!request_region(base, 3, "i2c-parport")) | 
|  | 138 | return -ENODEV; | 
|  | 139 |  | 
|  | 140 | if (!adapter_parm[type].getscl.val) | 
|  | 141 | parport_algo_data.getscl = NULL; | 
|  | 142 |  | 
|  | 143 | /* Reset hardware to a sane state (SCL and SDA high) */ | 
|  | 144 | parport_setsda(NULL, 1); | 
|  | 145 | parport_setscl(NULL, 1); | 
|  | 146 | /* Other init if needed (power on...) */ | 
|  | 147 | if (adapter_parm[type].init.val) | 
|  | 148 | line_set(1, &adapter_parm[type].init); | 
|  | 149 |  | 
|  | 150 | if (i2c_bit_add_bus(&parport_adapter) < 0) { | 
|  | 151 | printk(KERN_ERR "i2c-parport: Unable to register with I2C\n"); | 
|  | 152 | release_region(base, 3); | 
|  | 153 | return -ENODEV; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | return 0; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | static void __exit i2c_parport_exit(void) | 
|  | 160 | { | 
|  | 161 | /* Un-init if needed (power off...) */ | 
|  | 162 | if (adapter_parm[type].init.val) | 
|  | 163 | line_set(0, &adapter_parm[type].init); | 
|  | 164 |  | 
|  | 165 | i2c_bit_del_bus(&parport_adapter); | 
|  | 166 | release_region(base, 3); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | 
|  | 170 | MODULE_DESCRIPTION("I2C bus over parallel port (light)"); | 
|  | 171 | MODULE_LICENSE("GPL"); | 
|  | 172 |  | 
|  | 173 | module_init(i2c_parport_init); | 
|  | 174 | module_exit(i2c_parport_exit); |