aboutsummaryrefslogtreecommitdiff
path: root/drivers/i2c/busses/i2c-parport.c
blob: 231145c48728e503f3b1eeefbf20aeea420edc31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// SPDX-License-Identifier: GPL-2.0-or-later
/* ------------------------------------------------------------------------ *
 * i2c-parport.c I2C bus over parallel port                                 *
 * ------------------------------------------------------------------------ *
   Copyright (C) 2003-2011 Jean Delvare <jdelvare@suse.de>

   Based on older i2c-philips-par.c driver
   Copyright (C) 1995-2000 Simon G. Vogl
   With some changes from:
   Frodo Looijaard <frodol@dds.nl>
   Kyösti Mälkki <kmalkki@cc.hut.fi>

 * ------------------------------------------------------------------------ */

#define pr_fmt(fmt) "i2c-parport: " fmt

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/parport.h>
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>
#include <linux/i2c-smbus.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/mutex.h>

#define PORT_DATA	0
#define PORT_STAT	1
#define PORT_CTRL	2

struct lineop {
	u8 val;
	u8 port;
	u8 inverted;
};

struct adapter_parm {
	struct lineop setsda;
	struct lineop setscl;
	struct lineop getsda;
	struct lineop getscl;
	struct lineop init;
	unsigned int smbus_alert:1;
};

static const struct adapter_parm adapter_parm[] = {
	/* type 0: Philips adapter */
	{
		.setsda	= { 0x80, PORT_DATA, 1 },
		.setscl	= { 0x08, PORT_CTRL, 0 },
		.getsda	= { 0x80, PORT_STAT, 0 },
		.getscl	= { 0x08, PORT_STAT, 0 },
	},
	/* type 1: home brew teletext adapter */
	{
		.setsda	= { 0x02, PORT_DATA, 0 },
		.setscl	= { 0x01, PORT_DATA, 0 },
		.getsda	= { 0x80, PORT_STAT, 1 },
	},
	/* type 2: Velleman K8000 adapter */
	{
		.setsda	= { 0x02, PORT_CTRL, 1 },
		.setscl	= { 0x08, PORT_CTRL, 1 },
		.getsda	= { 0x10, PORT_STAT, 0 },
	},
	/* type 3: ELV adapter */
	{
		.setsda	= { 0x02, PORT_DATA, 1 },
		.setscl	= { 0x01, PORT_DATA, 1 },
		.getsda	= { 0x40, PORT_STAT, 1 },
		.getscl	= { 0x08, PORT_STAT, 1 },
	},
	/* type 4: ADM1032 evaluation board */
	{
		.setsda	= { 0x02, PORT_DATA, 1 },
		.setscl	= { 0x01, PORT_DATA, 1 },
		.getsda	= { 0x10, PORT_STAT, 1 },
		.init	= { 0xf0, PORT_DATA, 0 },
		.smbus_alert = 1,
	},
	/* type 5: ADM1025, ADM1030 and ADM1031 evaluation boards */
	{
		.setsda	= { 0x02, PORT_DATA, 1 },
		.setscl	= { 0x01, PORT_DATA, 1 },
		.getsda	= { 0x10, PORT_STAT, 1 },
	},
	/* type 6: Barco LPT->DVI (K5800236) adapter */
	{
		.setsda	= { 0x02, PORT_DATA, 1 },
		.setscl	= { 0x01, PORT_DATA, 1 },
		.getsda	= { 0x20, PORT_STAT, 0 },
		.getscl	= { 0x40, PORT_STAT, 0 },
		.init	= { 0xfc, PORT_DATA, 0 },
	},
	/* type 7: One For All JP1 parallel port adapter */
	{
		.setsda	= { 0x01, PORT_DATA, 0 },
		.setscl	= { 0x02, PORT_DATA, 0 },
		.getsda	= { 0x80, PORT_STAT, 1 },
		.init	= { 0x04, PORT_DATA, 1 },
	},
	/* type 8: VCT-jig */
	{
		.setsda	= { 0x04, PORT_DATA, 1 },
		.setscl	= { 0x01, PORT_DATA, 1 },
		.getsda	= { 0x40, PORT_STAT, 0 },
		.getscl	= { 0x80, PORT_STAT, 1 },
	},
};

/* ----- Device list ------------------------------------------------------ */

struct i2c_par {
	struct pardevice *pdev;
	struct i2c_adapter adapter;
	struct i2c_algo_bit_data algo_data;
	struct i2c_smbus_alert_setup alert_data;
	struct i2c_client *ara;
	struct list_head node;
};

static LIST_HEAD(adapter_list);
static DEFINE_MUTEX(adapter_list_lock);

#define MAX_DEVICE 4
static int parport[MAX_DEVICE] = {0, -1, -1, -1};
module_param_array(parport, int, NULL, 0);
MODULE_PARM_DESC(parport,
		 "List of parallel ports to bind to, by index.\n"
		 " At most " __stringify(MAX_DEVICE) " devices are supported.\n"
		 " Default is one device connected to parport0.\n"
);

static int type = -1;
module_param(type, int, 0);
MODULE_PARM_DESC(type,
	"Type of adapter:\n"
	" 0 = Philips adapter\n"
	" 1 = home brew teletext adapter\n"
	" 2 = Velleman K8000 adapter\n"
	" 3 = ELV adapter\n"
	" 4 = ADM1032 evaluation board\n"
	" 5 = ADM1025, ADM1030 and ADM1031 evaluation boards\n"
	" 6 = Barco LPT->DVI (K5800236) adapter\n"
	" 7 = One For All JP1 parallel port adapter\n"
	" 8 = VCT-jig\n"
);

/* ----- Low-level parallel port access ----------------------------------- */

static void port_write_data(struct parport *p, unsigned char d)
{
	parport_write_data(p, d);
}

static void port_write_control(struct parport *p, unsigned char d)
{
	parport_write_control(p, d);
}

static unsigned char port_read_data(struct parport *p)
{
	return parport_read_data(p);
}

static unsigned char port_read_status(struct parport *p)
{
	return parport_read_status(p);
}

static unsigned char port_read_control(struct parport *p)
{
	return parport_read_control(p);
}

static void (* const port_write[])(struct parport *, unsigned char) = {
	port_write_data,
	NULL,
	port_write_control,
};

static unsigned char (* const port_read[])(struct parport *) = {
	port_read_data,
	port_read_status,
	port_read_control,
};

/* ----- Unified line operation functions --------------------------------- */

static inline void line_set(struct parport *data, int state,
	const struct lineop *op)
{
	u8 oldval = port_read[op->port](data);

	/* Touch only the bit(s) needed */
	if ((op->inverted && !state) || (!op->inverted && state))
		port_write[op->port](data, oldval | op->val);
	else
		port_write[op->port](data, oldval & ~op->val);
}

static inline int line_get(struct parport *data,
	const struct lineop *op)
{
	u8 oldval = port_read[op->port](data);

	return ((op->inverted && (oldval & op->val) != op->val)
	    || (!op->inverted && (oldval & op->val) == op->val));
}

/* ----- I2C algorithm call-back functions and structures ----------------- */

static void parport_setscl(void *data, int state)
{
	line_set((struct parport *) data, state, &adapter_parm[type].setscl);
}

static void parport_setsda(void *data, int state)
{
	line_set((struct parport *) data, state, &adapter_parm[type].setsda);
}

static int parport_getscl(void *data)
{
	return line_get((struct parport *) data, &adapter_parm[type].getscl);
}

static int parport_getsda(void *data)
{
	return line_get((struct parport *) data, &adapter_parm[type].getsda);
}

/* Encapsulate the functions above in the correct structure.
   Note that this is only a template, from which the real structures are
   copied. The attaching code will set getscl to NULL for adapters that
   cannot read SCL back, and will also make the data field point to
   the parallel port structure. */
static const struct i2c_algo_bit_data parport_algo_data = {
	.setsda		= parport_setsda,
	.setscl		= parport_setscl,
	.getsda		= parport_getsda,
	.getscl		= parport_getscl,
	.udelay		= 10, /* ~50 kbps */
	.timeout	= HZ,
};

/* ----- I2c and parallel port call-back functions and structures --------- */

static void i2c_parport_irq(void *data)
{
	struct i2c_par *adapter = data;
	struct i2c_client *ara = adapter->ara;

	if (ara) {
		dev_dbg(&ara->dev, "SMBus alert received\n");
		i2c_handle_smbus_alert(ara);
	} else
		dev_dbg(&adapter->adapter.dev,
			"SMBus alert received but no ARA client!\n");
}

static void i2c_parport_attach(struct parport *port)
{
	struct i2c_par *adapter;
	int i;
	struct pardev_cb i2c_parport_cb;

	if (type < 0) {
		pr_warn("adapter type unspecified\n");
		return;
	}

	if (type >= ARRAY_SIZE(adapter_parm)) {
		pr_warn("invalid type (%d)\n", type);
		return;
	}

	for (i = 0; i < MAX_DEVICE; i++) {
		if (parport[i] == -1)
			continue;
		if (port->number == parport[i])
			break;
	}
	if (i == MAX_DEVICE) {
		pr_debug("Not using parport%d.\n", port->number);
		return;
	}

	adapter = kzalloc(sizeof(struct i2c_par), GFP_KERNEL);
	if (!adapter)
		return;
	memset(&i2c_parport_cb, 0, sizeof(i2c_parport_cb));
	i2c_parport_cb.flags = PARPORT_FLAG_EXCL;
	i2c_parport_cb.irq_func = i2c_parport_irq;
	i2c_parport_cb.private = adapter;

	pr_debug("attaching to %s\n", port->name);
	parport_disable_irq(port);
	adapter->pdev = parport_register_dev_model(port, "i2c-parport",
						   &i2c_parport_cb, i);
	if (!adapter->pdev) {
		pr_err("Unable to register with parport\n");
		goto err_free;
	}

	/* Fill the rest of the structure */
	adapter->adapter.owner = THIS_MODULE;
	adapter->adapter.class = I2C_CLASS_HWMON;
	strlcpy(adapter->adapter.name, "Parallel port adapter",
		sizeof(adapter->adapter.name));
	adapter->algo_data = parport_algo_data;
	/* Slow down if we can't sense SCL */
	if (!adapter_parm[type].getscl.val) {
		adapter->algo_data.getscl = NULL;
		adapter->algo_data.udelay = 50; /* ~10 kbps */
	}
	adapter->algo_data.data = port;
	adapter->adapter.algo_data = &adapter->algo_data;
	adapter->adapter.dev.parent = port->physport->dev;

	if (parport_claim_or_block(adapter->pdev) < 0) {
		dev_err(&adapter->pdev->dev,
			"Could not claim parallel port\n");
		goto err_unregister;
	}

	/* Reset hardware to a sane state (SCL and SDA high) */
	parport_setsda(port, 1);
	parport_setscl(port, 1);
	/* Other init if needed (power on...) */
	if (adapter_parm[type].init.val) {
		line_set(port, 1, &adapter_parm[type].init);
		/* Give powered devices some time to settle */
		msleep(100);
	}

	if (i2c_bit_add_bus(&adapter->adapter) < 0) {
		dev_err(&adapter->pdev->dev, "Unable to register with I2C\n");
		goto err_unregister;
	}

	/* Setup SMBus alert if supported */
	if (adapter_parm[type].smbus_alert) {
		struct i2c_client *ara;

		ara = i2c_new_smbus_alert_device(&adapter->adapter,
						 &adapter->alert_data);
		if (!IS_ERR(ara)) {
			adapter->ara = ara;
			parport_enable_irq(port);
		} else {
			dev_warn(&adapter->pdev->dev,
				 "Failed to register ARA client\n");
		}
	}

	/* Add the new adapter to the list */
	mutex_lock(&adapter_list_lock);
	list_add_tail(&adapter->node, &adapter_list);
	mutex_unlock(&adapter_list_lock);
	return;

 err_unregister:
	parport_release(adapter->pdev);
	parport_unregister_device(adapter->pdev);
 err_free:
	kfree(adapter);
}

static void i2c_parport_detach(struct parport *port)
{
	struct i2c_par *adapter, *_n;

	/* Walk the list */
	mutex_lock(&adapter_list_lock);
	list_for_each_entry_safe(adapter, _n, &adapter_list, node) {
		if (adapter->pdev->port == port) {
			if (adapter->ara) {
				parport_disable_irq(port);
				i2c_unregister_device(adapter->ara);
			}
			i2c_del_adapter(&adapter->adapter);

			/* Un-init if needed (power off...) */
			if (adapter_parm[type].init.val)
				line_set(port, 0, &adapter_parm[type].init);

			parport_release(adapter->pdev);
			parport_unregister_device(adapter->pdev);
			list_del(&adapter->node);
			kfree(adapter);
		}
	}
	mutex_unlock(&adapter_list_lock);
}

static struct parport_driver i2c_parport_driver = {
	.name = "i2c-parport",
	.match_port = i2c_parport_attach,
	.detach = i2c_parport_detach,
	.devmodel = true,
};
module_parport_driver(i2c_parport_driver);

MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
MODULE_DESCRIPTION("I2C bus over parallel port");
MODULE_LICENSE("GPL");