blob: b2b8380f66029f23f238cef943c8006a35aa95dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ------------------------------------------------------------------------ *
Jean Delvarec6e8bb22007-05-01 23:26:30 +02002 * i2c-parport-light.c I2C bus over parallel port *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * ------------------------------------------------------------------------ *
Jean Delvarec6e8bb22007-05-01 23:26:30 +02004 Copyright (C) 2003-2007 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
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 Engelhardt96de0e22007-10-19 23:21:04 +020010 Kyösti Mälkki <kmalkki@cc.hut.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
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 Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
Jean Delvarec6e8bb22007-05-01 23:26:30 +020030#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/ioport.h>
32#include <linux/i2c.h>
33#include <linux/i2c-algo-bit.h>
34#include <asm/io.h>
35#include "i2c-parport.h"
36
37#define DEFAULT_BASE 0x378
Jean Delvarec6e8bb22007-05-01 23:26:30 +020038#define DRVNAME "i2c-parport-light"
39
40static struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static u16 base;
43module_param(base, ushort, 0);
44MODULE_PARM_DESC(base, "Base I/O address");
45
46/* ----- Low-level parallel port access ----------------------------------- */
47
48static inline void port_write(unsigned char p, unsigned char d)
49{
50 outb(d, base+p);
51}
52
53static inline unsigned char port_read(unsigned char p)
54{
55 return inb(base+p);
56}
57
58/* ----- Unified line operation functions --------------------------------- */
59
60static inline void line_set(int state, const struct lineop *op)
61{
62 u8 oldval = port_read(op->port);
63
64 /* Touch only the bit(s) needed */
65 if ((op->inverted && !state) || (!op->inverted && state))
66 port_write(op->port, oldval | op->val);
67 else
68 port_write(op->port, oldval & ~op->val);
69}
70
71static inline int line_get(const struct lineop *op)
72{
73 u8 oldval = port_read(op->port);
74
75 return ((op->inverted && (oldval & op->val) != op->val)
76 || (!op->inverted && (oldval & op->val) == op->val));
77}
78
79/* ----- I2C algorithm call-back functions and structures ----------------- */
80
81static void parport_setscl(void *data, int state)
82{
83 line_set(state, &adapter_parm[type].setscl);
84}
85
86static void parport_setsda(void *data, int state)
87{
88 line_set(state, &adapter_parm[type].setsda);
89}
90
91static int parport_getscl(void *data)
92{
93 return line_get(&adapter_parm[type].getscl);
94}
95
96static int parport_getsda(void *data)
97{
98 return line_get(&adapter_parm[type].getsda);
99}
100
101/* Encapsulate the functions above in the correct structure
102 Note that getscl will be set to NULL by the attaching code for adapters
103 that cannot read SCL back */
104static struct i2c_algo_bit_data parport_algo_data = {
105 .setsda = parport_setsda,
106 .setscl = parport_setscl,
107 .getsda = parport_getsda,
108 .getscl = parport_getscl,
109 .udelay = 50,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .timeout = HZ,
111};
112
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200113/* ----- Driver registration ---------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static struct i2c_adapter parport_adapter = {
116 .owner = THIS_MODULE,
117 .class = I2C_CLASS_HWMON,
118 .id = I2C_HW_B_LP,
119 .algo_data = &parport_algo_data,
120 .name = "Parallel port adapter (light)",
121};
122
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200123static int __devinit i2c_parport_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200125 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
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...) */
131 if (adapter_parm[type].init.val)
132 line_set(1, &adapter_parm[type].init);
133
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200134 parport_adapter.dev.parent = &pdev->dev;
135 err = i2c_bit_add_bus(&parport_adapter);
Jean Delvare9def2552008-10-14 17:30:04 +0200136 if (err)
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200137 dev_err(&pdev->dev, "Unable to register with I2C\n");
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200138 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200141static int __devexit i2c_parport_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200143 i2c_del_adapter(&parport_adapter);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Un-init if needed (power off...) */
146 if (adapter_parm[type].init.val)
147 line_set(0, &adapter_parm[type].init);
148
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200149 return 0;
150}
151
152static struct platform_driver i2c_parport_driver = {
153 .driver = {
154 .owner = THIS_MODULE,
155 .name = DRVNAME,
156 },
157 .probe = i2c_parport_probe,
158 .remove = __devexit_p(i2c_parport_remove),
159};
160
161static int __init i2c_parport_device_add(u16 address)
162{
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200163 int err;
164
165 pdev = platform_device_alloc(DRVNAME, -1);
166 if (!pdev) {
167 err = -ENOMEM;
168 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
169 goto exit;
170 }
171
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200172 err = platform_device_add(pdev);
173 if (err) {
174 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
175 err);
176 goto exit_device_put;
177 }
178
179 return 0;
180
181exit_device_put:
182 platform_device_put(pdev);
183exit:
184 return err;
185}
186
187static int __init i2c_parport_init(void)
188{
189 int err;
190
191 if (type < 0) {
192 printk(KERN_ERR DRVNAME ": adapter type unspecified\n");
193 return -ENODEV;
194 }
195
196 if (type >= ARRAY_SIZE(adapter_parm)) {
197 printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type);
198 return -ENODEV;
199 }
200
201 if (base == 0) {
202 pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE);
203 base = DEFAULT_BASE;
204 }
205
Jean Delvare9def2552008-10-14 17:30:04 +0200206 if (!request_region(base, 3, DRVNAME))
207 return -EBUSY;
208
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200209 if (!adapter_parm[type].getscl.val)
210 parport_algo_data.getscl = NULL;
211
212 /* Sets global pdev as a side effect */
213 err = i2c_parport_device_add(base);
214 if (err)
Jean Delvare9def2552008-10-14 17:30:04 +0200215 goto exit_release;
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200216
217 err = platform_driver_register(&i2c_parport_driver);
218 if (err)
219 goto exit_device;
220
221 return 0;
222
223exit_device:
224 platform_device_unregister(pdev);
Jean Delvare9def2552008-10-14 17:30:04 +0200225exit_release:
226 release_region(base, 3);
Jean Delvarec6e8bb22007-05-01 23:26:30 +0200227 return err;
228}
229
230static void __exit i2c_parport_exit(void)
231{
232 platform_driver_unregister(&i2c_parport_driver);
233 platform_device_unregister(pdev);
Jean Delvare9def2552008-10-14 17:30:04 +0200234 release_region(base, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
238MODULE_DESCRIPTION("I2C bus over parallel port (light)");
239MODULE_LICENSE("GPL");
240
241module_init(i2c_parport_init);
242module_exit(i2c_parport_exit);