1/* ------------------------------------------------------------------------ *
2 * i2c-parport.c I2C bus over parallel port                                 *
3 * ------------------------------------------------------------------------ *
4   Copyright (C) 2003-2011 Jean Delvare <jdelvare@suse.de>
5
6   Based on older i2c-philips-par.c driver
7   Copyright (C) 1995-2000 Simon G. Vogl
8   With some changes from:
9   Frodo Looijaard <frodol@dds.nl>
10   Ky��sti M��lkki <kmalkki@cc.hut.fi>
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
23#define pr_fmt(fmt) "i2c-parport: " fmt
24
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/parport.h>
30#include <linux/i2c.h>
31#include <linux/i2c-algo-bit.h>
32#include <linux/i2c-smbus.h>
33#include <linux/slab.h>
34#include <linux/list.h>
35#include <linux/mutex.h>
36#include "i2c-parport.h"
37
38/* ----- Device list ------------------------------------------------------ */
39
40struct i2c_par {
41	struct pardevice *pdev;
42	struct i2c_adapter adapter;
43	struct i2c_algo_bit_data algo_data;
44	struct i2c_smbus_alert_setup alert_data;
45	struct i2c_client *ara;
46	struct list_head node;
47};
48
49static LIST_HEAD(adapter_list);
50static DEFINE_MUTEX(adapter_list_lock);
51#define MAX_DEVICE 4
52static int parport[MAX_DEVICE] = {0, -1, -1, -1};
53
54
55/* ----- Low-level parallel port access ----------------------------------- */
56
57static void port_write_data(struct parport *p, unsigned char d)
58{
59	parport_write_data(p, d);
60}
61
62static void port_write_control(struct parport *p, unsigned char d)
63{
64	parport_write_control(p, d);
65}
66
67static unsigned char port_read_data(struct parport *p)
68{
69	return parport_read_data(p);
70}
71
72static unsigned char port_read_status(struct parport *p)
73{
74	return parport_read_status(p);
75}
76
77static unsigned char port_read_control(struct parport *p)
78{
79	return parport_read_control(p);
80}
81
82static void (* const port_write[])(struct parport *, unsigned char) = {
83	port_write_data,
84	NULL,
85	port_write_control,
86};
87
88static unsigned char (* const port_read[])(struct parport *) = {
89	port_read_data,
90	port_read_status,
91	port_read_control,
92};
93
94/* ----- Unified line operation functions --------------------------------- */
95
96static inline void line_set(struct parport *data, int state,
97	const struct lineop *op)
98{
99	u8 oldval = port_read[op->port](data);
100
101	/* Touch only the bit(s) needed */
102	if ((op->inverted && !state) || (!op->inverted && state))
103		port_write[op->port](data, oldval | op->val);
104	else
105		port_write[op->port](data, oldval & ~op->val);
106}
107
108static inline int line_get(struct parport *data,
109	const struct lineop *op)
110{
111	u8 oldval = port_read[op->port](data);
112
113	return ((op->inverted && (oldval & op->val) != op->val)
114	    || (!op->inverted && (oldval & op->val) == op->val));
115}
116
117/* ----- I2C algorithm call-back functions and structures ----------------- */
118
119static void parport_setscl(void *data, int state)
120{
121	line_set((struct parport *) data, state, &adapter_parm[type].setscl);
122}
123
124static void parport_setsda(void *data, int state)
125{
126	line_set((struct parport *) data, state, &adapter_parm[type].setsda);
127}
128
129static int parport_getscl(void *data)
130{
131	return line_get((struct parport *) data, &adapter_parm[type].getscl);
132}
133
134static int parport_getsda(void *data)
135{
136	return line_get((struct parport *) data, &adapter_parm[type].getsda);
137}
138
139/* Encapsulate the functions above in the correct structure.
140   Note that this is only a template, from which the real structures are
141   copied. The attaching code will set getscl to NULL for adapters that
142   cannot read SCL back, and will also make the data field point to
143   the parallel port structure. */
144static const struct i2c_algo_bit_data parport_algo_data = {
145	.setsda		= parport_setsda,
146	.setscl		= parport_setscl,
147	.getsda		= parport_getsda,
148	.getscl		= parport_getscl,
149	.udelay		= 10, /* ~50 kbps */
150	.timeout	= HZ,
151};
152
153/* ----- I2c and parallel port call-back functions and structures --------- */
154
155static void i2c_parport_irq(void *data)
156{
157	struct i2c_par *adapter = data;
158	struct i2c_client *ara = adapter->ara;
159
160	if (ara) {
161		dev_dbg(&ara->dev, "SMBus alert received\n");
162		i2c_handle_smbus_alert(ara);
163	} else
164		dev_dbg(&adapter->adapter.dev,
165			"SMBus alert received but no ARA client!\n");
166}
167
168static void i2c_parport_attach(struct parport *port)
169{
170	struct i2c_par *adapter;
171	int i;
172	struct pardev_cb i2c_parport_cb;
173
174	for (i = 0; i < MAX_DEVICE; i++) {
175		if (parport[i] == -1)
176			continue;
177		if (port->number == parport[i])
178			break;
179	}
180	if (i == MAX_DEVICE) {
181		pr_debug("Not using parport%d.\n", port->number);
182		return;
183	}
184
185	adapter = kzalloc(sizeof(struct i2c_par), GFP_KERNEL);
186	if (!adapter)
187		return;
188	memset(&i2c_parport_cb, 0, sizeof(i2c_parport_cb));
189	i2c_parport_cb.flags = PARPORT_FLAG_EXCL;
190	i2c_parport_cb.irq_func = i2c_parport_irq;
191	i2c_parport_cb.private = adapter;
192
193	pr_debug("attaching to %s\n", port->name);
194	parport_disable_irq(port);
195	adapter->pdev = parport_register_dev_model(port, "i2c-parport",
196						   &i2c_parport_cb, i);
197	if (!adapter->pdev) {
198		pr_err("Unable to register with parport\n");
199		goto err_free;
200	}
201
202	/* Fill the rest of the structure */
203	adapter->adapter.owner = THIS_MODULE;
204	adapter->adapter.class = I2C_CLASS_HWMON;
205	strlcpy(adapter->adapter.name, "Parallel port adapter",
206		sizeof(adapter->adapter.name));
207	adapter->algo_data = parport_algo_data;
208	/* Slow down if we can't sense SCL */
209	if (!adapter_parm[type].getscl.val) {
210		adapter->algo_data.getscl = NULL;
211		adapter->algo_data.udelay = 50; /* ~10 kbps */
212	}
213	adapter->algo_data.data = port;
214	adapter->adapter.algo_data = &adapter->algo_data;
215	adapter->adapter.dev.parent = port->physport->dev;
216
217	if (parport_claim_or_block(adapter->pdev) < 0) {
218		dev_err(&adapter->pdev->dev,
219			"Could not claim parallel port\n");
220		goto err_unregister;
221	}
222
223	/* Reset hardware to a sane state (SCL and SDA high) */
224	parport_setsda(port, 1);
225	parport_setscl(port, 1);
226	/* Other init if needed (power on...) */
227	if (adapter_parm[type].init.val) {
228		line_set(port, 1, &adapter_parm[type].init);
229		/* Give powered devices some time to settle */
230		msleep(100);
231	}
232
233	if (i2c_bit_add_bus(&adapter->adapter) < 0) {
234		dev_err(&adapter->pdev->dev, "Unable to register with I2C\n");
235		goto err_unregister;
236	}
237
238	/* Setup SMBus alert if supported */
239	if (adapter_parm[type].smbus_alert) {
240		adapter->alert_data.alert_edge_triggered = 1;
241		adapter->ara = i2c_setup_smbus_alert(&adapter->adapter,
242						     &adapter->alert_data);
243		if (adapter->ara)
244			parport_enable_irq(port);
245		else
246			dev_warn(&adapter->pdev->dev,
247				 "Failed to register ARA client\n");
248	}
249
250	/* Add the new adapter to the list */
251	mutex_lock(&adapter_list_lock);
252	list_add_tail(&adapter->node, &adapter_list);
253	mutex_unlock(&adapter_list_lock);
254	return;
255
256 err_unregister:
257	parport_release(adapter->pdev);
258	parport_unregister_device(adapter->pdev);
259 err_free:
260	kfree(adapter);
261}
262
263static void i2c_parport_detach(struct parport *port)
264{
265	struct i2c_par *adapter, *_n;
266
267	/* Walk the list */
268	mutex_lock(&adapter_list_lock);
269	list_for_each_entry_safe(adapter, _n, &adapter_list, node) {
270		if (adapter->pdev->port == port) {
271			if (adapter->ara) {
272				parport_disable_irq(port);
273				i2c_unregister_device(adapter->ara);
274			}
275			i2c_del_adapter(&adapter->adapter);
276
277			/* Un-init if needed (power off...) */
278			if (adapter_parm[type].init.val)
279				line_set(port, 0, &adapter_parm[type].init);
280
281			parport_release(adapter->pdev);
282			parport_unregister_device(adapter->pdev);
283			list_del(&adapter->node);
284			kfree(adapter);
285		}
286	}
287	mutex_unlock(&adapter_list_lock);
288}
289
290static struct parport_driver i2c_parport_driver = {
291	.name = "i2c-parport",
292	.match_port = i2c_parport_attach,
293	.detach = i2c_parport_detach,
294	.devmodel = true,
295};
296
297/* ----- Module loading, unloading and information ------------------------ */
298
299static int __init i2c_parport_init(void)
300{
301	if (type < 0) {
302		pr_warn("adapter type unspecified\n");
303		return -ENODEV;
304	}
305
306	if (type >= ARRAY_SIZE(adapter_parm)) {
307		pr_warn("invalid type (%d)\n", type);
308		return -ENODEV;
309	}
310
311	return parport_register_driver(&i2c_parport_driver);
312}
313
314static void __exit i2c_parport_exit(void)
315{
316	parport_unregister_driver(&i2c_parport_driver);
317}
318
319MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
320MODULE_DESCRIPTION("I2C bus over parallel port");
321MODULE_LICENSE("GPL");
322
323module_param_array(parport, int, NULL, 0);
324MODULE_PARM_DESC(parport,
325		 "List of parallel ports to bind to, by index.\n"
326		 " Atmost " __stringify(MAX_DEVICE) " devices are supported.\n"
327		 " Default is one device connected to parport0.\n"
328);
329
330module_init(i2c_parport_init);
331module_exit(i2c_parport_exit);
332