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