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