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