1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* ------------------------------------------------------------------------ *
3 * i2c-parport-light.c I2C bus over parallel port *
4 * ------------------------------------------------------------------------ *
5 Copyright (C) 2003-2010 Jean Delvare <jdelvare@suse.de>
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>
11 Kyösti Mälkki <kmalkki@cc.hut.fi>
13 * ------------------------------------------------------------------------ */
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/platform_device.h>
20 #include <linux/ioport.h>
21 #include <linux/i2c.h>
22 #include <linux/i2c-algo-bit.h>
23 #include <linux/i2c-smbus.h>
25 #include "i2c-parport.h"
27 #define DEFAULT_BASE 0x378
28 #define DRVNAME "i2c-parport-light"
30 static struct platform_device
*pdev
;
33 module_param_hw(base
, ushort
, ioport
, 0);
34 MODULE_PARM_DESC(base
, "Base I/O address");
37 module_param_hw(irq
, int, irq
, 0);
38 MODULE_PARM_DESC(irq
, "IRQ (optional)");
40 /* ----- Low-level parallel port access ----------------------------------- */
42 static inline void port_write(unsigned char p
, unsigned char d
)
47 static inline unsigned char port_read(unsigned char p
)
52 /* ----- Unified line operation functions --------------------------------- */
54 static inline void line_set(int state
, const struct lineop
*op
)
56 u8 oldval
= port_read(op
->port
);
58 /* Touch only the bit(s) needed */
59 if ((op
->inverted
&& !state
) || (!op
->inverted
&& state
))
60 port_write(op
->port
, oldval
| op
->val
);
62 port_write(op
->port
, oldval
& ~op
->val
);
65 static inline int line_get(const struct lineop
*op
)
67 u8 oldval
= port_read(op
->port
);
69 return ((op
->inverted
&& (oldval
& op
->val
) != op
->val
)
70 || (!op
->inverted
&& (oldval
& op
->val
) == op
->val
));
73 /* ----- I2C algorithm call-back functions and structures ----------------- */
75 static void parport_setscl(void *data
, int state
)
77 line_set(state
, &adapter_parm
[type
].setscl
);
80 static void parport_setsda(void *data
, int state
)
82 line_set(state
, &adapter_parm
[type
].setsda
);
85 static int parport_getscl(void *data
)
87 return line_get(&adapter_parm
[type
].getscl
);
90 static int parport_getsda(void *data
)
92 return line_get(&adapter_parm
[type
].getsda
);
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 */
98 static struct i2c_algo_bit_data parport_algo_data
= {
99 .setsda
= parport_setsda
,
100 .setscl
= parport_setscl
,
101 .getsda
= parport_getsda
,
102 .getscl
= parport_getscl
,
107 /* ----- Driver registration ---------------------------------------------- */
109 static struct i2c_adapter parport_adapter
= {
110 .owner
= THIS_MODULE
,
111 .class = I2C_CLASS_HWMON
,
112 .algo_data
= &parport_algo_data
,
113 .name
= "Parallel port adapter (light)",
116 /* SMBus alert support */
117 static struct i2c_smbus_alert_setup alert_data
= {
119 static struct i2c_client
*ara
;
120 static struct lineop parport_ctrl_irq
= {
125 static int i2c_parport_probe(struct platform_device
*pdev
)
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...) */
133 if (adapter_parm
[type
].init
.val
) {
134 line_set(1, &adapter_parm
[type
].init
);
135 /* Give powered devices some time to settle */
139 parport_adapter
.dev
.parent
= &pdev
->dev
;
140 err
= i2c_bit_add_bus(&parport_adapter
);
142 dev_err(&pdev
->dev
, "Unable to register with I2C\n");
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
);
151 line_set(1, &parport_ctrl_irq
);
153 dev_warn(&pdev
->dev
, "Failed to register ARA client\n");
159 static int i2c_parport_remove(struct platform_device
*pdev
)
162 line_set(0, &parport_ctrl_irq
);
163 i2c_unregister_device(ara
);
166 i2c_del_adapter(&parport_adapter
);
168 /* Un-init if needed (power off...) */
169 if (adapter_parm
[type
].init
.val
)
170 line_set(0, &adapter_parm
[type
].init
);
175 static struct platform_driver i2c_parport_driver
= {
179 .probe
= i2c_parport_probe
,
180 .remove
= i2c_parport_remove
,
183 static int __init
i2c_parport_device_add(u16 address
)
187 pdev
= platform_device_alloc(DRVNAME
, -1);
190 printk(KERN_ERR DRVNAME
": Device allocation failed\n");
194 err
= platform_device_add(pdev
);
196 printk(KERN_ERR DRVNAME
": Device addition failed (%d)\n",
198 goto exit_device_put
;
204 platform_device_put(pdev
);
209 static int __init
i2c_parport_init(void)
214 printk(KERN_ERR DRVNAME
": adapter type unspecified\n");
218 if (type
>= ARRAY_SIZE(adapter_parm
)) {
219 printk(KERN_ERR DRVNAME
": invalid type (%d)\n", type
);
224 pr_info(DRVNAME
": using default base 0x%x\n", DEFAULT_BASE
);
228 if (!request_region(base
, 3, DRVNAME
))
232 pr_info(DRVNAME
": using irq %d\n", irq
);
234 if (!adapter_parm
[type
].getscl
.val
)
235 parport_algo_data
.getscl
= NULL
;
237 /* Sets global pdev as a side effect */
238 err
= i2c_parport_device_add(base
);
242 err
= platform_driver_register(&i2c_parport_driver
);
249 platform_device_unregister(pdev
);
251 release_region(base
, 3);
255 static void __exit
i2c_parport_exit(void)
257 platform_driver_unregister(&i2c_parport_driver
);
258 platform_device_unregister(pdev
);
259 release_region(base
, 3);
262 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
263 MODULE_DESCRIPTION("I2C bus over parallel port (light)");
264 MODULE_LICENSE("GPL");
266 module_init(i2c_parport_init
);
267 module_exit(i2c_parport_exit
);