2005-04-16 16:20:36 -06:00
|
|
|
/* ------------------------------------------------------------------------ *
|
|
|
|
* i2c-parport.c I2C bus over parallel port *
|
|
|
|
* ------------------------------------------------------------------------ *
|
2014-01-29 12:40:08 -07:00
|
|
|
Copyright (C) 2003-2011 Jean Delvare <jdelvare@suse.de>
|
2011-05-24 12:58:49 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
Based on older i2c-philips-par.c driver
|
|
|
|
Copyright (C) 1995-2000 Simon G. Vogl
|
|
|
|
With some changes from:
|
|
|
|
Frodo Looijaard <frodol@dds.nl>
|
2007-10-19 15:21:04 -06:00
|
|
|
Kyösti Mälkki <kmalkki@cc.hut.fi>
|
2011-05-24 12:58:49 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
* ------------------------------------------------------------------------ */
|
|
|
|
|
2015-06-18 07:03:47 -06:00
|
|
|
#define pr_fmt(fmt) "i2c-parport: " fmt
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
2010-03-02 04:23:41 -07:00
|
|
|
#include <linux/delay.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/parport.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/i2c-algo-bit.h>
|
2010-03-02 04:23:44 -07:00
|
|
|
#include <linux/i2c-smbus.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 02:04:11 -06:00
|
|
|
#include <linux/slab.h>
|
2011-05-01 10:18:49 -06:00
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/mutex.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include "i2c-parport.h"
|
|
|
|
|
|
|
|
/* ----- Device list ------------------------------------------------------ */
|
|
|
|
|
|
|
|
struct i2c_par {
|
|
|
|
struct pardevice *pdev;
|
|
|
|
struct i2c_adapter adapter;
|
|
|
|
struct i2c_algo_bit_data algo_data;
|
2010-03-02 04:23:44 -07:00
|
|
|
struct i2c_smbus_alert_setup alert_data;
|
|
|
|
struct i2c_client *ara;
|
2011-05-01 10:18:49 -06:00
|
|
|
struct list_head node;
|
2005-04-16 16:20:36 -06:00
|
|
|
};
|
|
|
|
|
2011-05-01 10:18:49 -06:00
|
|
|
static LIST_HEAD(adapter_list);
|
|
|
|
static DEFINE_MUTEX(adapter_list_lock);
|
2015-05-20 09:26:59 -06:00
|
|
|
#define MAX_DEVICE 4
|
|
|
|
static int parport[MAX_DEVICE] = {0, -1, -1, -1};
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* ----- 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);
|
|
|
|
}
|
|
|
|
|
2011-05-24 12:58:49 -06:00
|
|
|
static void (* const port_write[])(struct parport *, unsigned char) = {
|
2005-04-16 16:20:36 -06:00
|
|
|
port_write_data,
|
|
|
|
NULL,
|
|
|
|
port_write_control,
|
|
|
|
};
|
|
|
|
|
2011-05-24 12:58:49 -06:00
|
|
|
static unsigned char (* const port_read[])(struct parport *) = {
|
2005-04-16 16:20:36 -06:00
|
|
|
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
|
2005-05-19 13:39:06 -06:00
|
|
|
cannot read SCL back, and will also make the data field point to
|
2005-04-16 16:20:36 -06:00
|
|
|
the parallel port structure. */
|
2010-05-21 10:40:57 -06:00
|
|
|
static const struct i2c_algo_bit_data parport_algo_data = {
|
2005-04-16 16:20:36 -06:00
|
|
|
.setsda = parport_setsda,
|
|
|
|
.setscl = parport_setscl,
|
|
|
|
.getsda = parport_getsda,
|
|
|
|
.getscl = parport_getscl,
|
2007-05-01 15:26:30 -06:00
|
|
|
.udelay = 10, /* ~50 kbps */
|
2005-04-16 16:20:36 -06:00
|
|
|
.timeout = HZ,
|
2011-05-24 12:58:49 -06:00
|
|
|
};
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* ----- I2c and parallel port call-back functions and structures --------- */
|
|
|
|
|
2012-10-05 14:23:53 -06:00
|
|
|
static void i2c_parport_irq(void *data)
|
2010-03-02 04:23:44 -07:00
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2011-05-24 12:58:49 -06:00
|
|
|
static void i2c_parport_attach(struct parport *port)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
struct i2c_par *adapter;
|
2015-05-20 09:26:59 -06:00
|
|
|
int i;
|
|
|
|
struct pardev_cb i2c_parport_cb;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_DEVICE; i++) {
|
|
|
|
if (parport[i] == -1)
|
|
|
|
continue;
|
|
|
|
if (port->number == parport[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == MAX_DEVICE) {
|
2015-06-18 07:03:47 -06:00
|
|
|
pr_debug("Not using parport%d.\n", port->number);
|
2015-05-20 09:26:59 -06:00
|
|
|
return;
|
|
|
|
}
|
2011-05-24 12:58:49 -06:00
|
|
|
|
2005-10-17 15:09:43 -06:00
|
|
|
adapter = kzalloc(sizeof(struct i2c_par), GFP_KERNEL);
|
2015-06-18 07:03:47 -06:00
|
|
|
if (!adapter)
|
2005-04-16 16:20:36 -06:00
|
|
|
return;
|
2015-05-21 00:16:31 -06:00
|
|
|
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;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2015-06-18 07:03:47 -06:00
|
|
|
pr_debug("attaching to %s\n", port->name);
|
2010-03-02 04:23:44 -07:00
|
|
|
parport_disable_irq(port);
|
2015-05-21 00:16:31 -06:00
|
|
|
adapter->pdev = parport_register_dev_model(port, "i2c-parport",
|
|
|
|
&i2c_parport_cb, i);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (!adapter->pdev) {
|
2015-06-18 07:03:47 -06:00
|
|
|
pr_err("Unable to register with parport\n");
|
2011-05-24 12:58:49 -06:00
|
|
|
goto err_free;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill the rest of the structure */
|
2007-05-01 15:26:29 -06:00
|
|
|
adapter->adapter.owner = THIS_MODULE;
|
|
|
|
adapter->adapter.class = I2C_CLASS_HWMON;
|
|
|
|
strlcpy(adapter->adapter.name, "Parallel port adapter",
|
|
|
|
sizeof(adapter->adapter.name));
|
2005-04-16 16:20:36 -06:00
|
|
|
adapter->algo_data = parport_algo_data;
|
2007-05-01 15:26:30 -06:00
|
|
|
/* Slow down if we can't sense SCL */
|
|
|
|
if (!adapter_parm[type].getscl.val) {
|
2005-04-16 16:20:36 -06:00
|
|
|
adapter->algo_data.getscl = NULL;
|
2007-05-01 15:26:30 -06:00
|
|
|
adapter->algo_data.udelay = 50; /* ~10 kbps */
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
adapter->algo_data.data = port;
|
|
|
|
adapter->adapter.algo_data = &adapter->algo_data;
|
2007-05-08 01:27:42 -06:00
|
|
|
adapter->adapter.dev.parent = port->physport->dev;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (parport_claim_or_block(adapter->pdev) < 0) {
|
2015-06-18 07:03:46 -06:00
|
|
|
dev_err(&adapter->pdev->dev,
|
|
|
|
"Could not claim parallel port\n");
|
2011-05-24 12:58:49 -06:00
|
|
|
goto err_unregister;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset hardware to a sane state (SCL and SDA high) */
|
|
|
|
parport_setsda(port, 1);
|
|
|
|
parport_setscl(port, 1);
|
|
|
|
/* Other init if needed (power on...) */
|
2010-03-02 04:23:41 -07:00
|
|
|
if (adapter_parm[type].init.val) {
|
2005-04-16 16:20:36 -06:00
|
|
|
line_set(port, 1, &adapter_parm[type].init);
|
2010-03-02 04:23:41 -07:00
|
|
|
/* Give powered devices some time to settle */
|
|
|
|
msleep(100);
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (i2c_bit_add_bus(&adapter->adapter) < 0) {
|
2015-06-18 07:03:46 -06:00
|
|
|
dev_err(&adapter->pdev->dev, "Unable to register with I2C\n");
|
2011-05-24 12:58:49 -06:00
|
|
|
goto err_unregister;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2010-03-02 04:23:44 -07:00
|
|
|
/* Setup SMBus alert if supported */
|
|
|
|
if (adapter_parm[type].smbus_alert) {
|
|
|
|
adapter->ara = i2c_setup_smbus_alert(&adapter->adapter,
|
|
|
|
&adapter->alert_data);
|
|
|
|
if (adapter->ara)
|
|
|
|
parport_enable_irq(port);
|
|
|
|
else
|
2015-06-18 07:03:46 -06:00
|
|
|
dev_warn(&adapter->pdev->dev,
|
|
|
|
"Failed to register ARA client\n");
|
2010-03-02 04:23:44 -07:00
|
|
|
}
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* Add the new adapter to the list */
|
2011-05-01 10:18:49 -06:00
|
|
|
mutex_lock(&adapter_list_lock);
|
|
|
|
list_add_tail(&adapter->node, &adapter_list);
|
|
|
|
mutex_unlock(&adapter_list_lock);
|
2011-05-24 12:58:49 -06:00
|
|
|
return;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2011-05-24 12:58:49 -06:00
|
|
|
err_unregister:
|
2008-11-28 07:24:39 -07:00
|
|
|
parport_release(adapter->pdev);
|
2005-04-16 16:20:36 -06:00
|
|
|
parport_unregister_device(adapter->pdev);
|
2011-05-24 12:58:49 -06:00
|
|
|
err_free:
|
2005-04-16 16:20:36 -06:00
|
|
|
kfree(adapter);
|
|
|
|
}
|
|
|
|
|
2011-05-24 12:58:49 -06:00
|
|
|
static void i2c_parport_detach(struct parport *port)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2011-05-01 10:18:49 -06:00
|
|
|
struct i2c_par *adapter, *_n;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* Walk the list */
|
2011-05-01 10:18:49 -06:00
|
|
|
mutex_lock(&adapter_list_lock);
|
|
|
|
list_for_each_entry_safe(adapter, _n, &adapter_list, node) {
|
2005-04-16 16:20:36 -06:00
|
|
|
if (adapter->pdev->port == port) {
|
2010-03-02 04:23:44 -07:00
|
|
|
if (adapter->ara) {
|
|
|
|
parport_disable_irq(port);
|
|
|
|
i2c_unregister_device(adapter->ara);
|
|
|
|
}
|
2007-05-01 15:26:30 -06:00
|
|
|
i2c_del_adapter(&adapter->adapter);
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* Un-init if needed (power off...) */
|
|
|
|
if (adapter_parm[type].init.val)
|
|
|
|
line_set(port, 0, &adapter_parm[type].init);
|
2011-05-24 12:58:49 -06:00
|
|
|
|
2008-11-28 07:24:39 -07:00
|
|
|
parport_release(adapter->pdev);
|
2005-04-16 16:20:36 -06:00
|
|
|
parport_unregister_device(adapter->pdev);
|
2011-05-01 10:18:49 -06:00
|
|
|
list_del(&adapter->node);
|
2005-04-16 16:20:36 -06:00
|
|
|
kfree(adapter);
|
|
|
|
}
|
|
|
|
}
|
2011-05-01 10:18:49 -06:00
|
|
|
mutex_unlock(&adapter_list_lock);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2005-10-07 16:17:35 -06:00
|
|
|
static struct parport_driver i2c_parport_driver = {
|
2015-05-21 00:16:31 -06:00
|
|
|
.name = "i2c-parport",
|
|
|
|
.match_port = i2c_parport_attach,
|
|
|
|
.detach = i2c_parport_detach,
|
|
|
|
.devmodel = true,
|
2005-04-16 16:20:36 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ----- Module loading, unloading and information ------------------------ */
|
|
|
|
|
|
|
|
static int __init i2c_parport_init(void)
|
|
|
|
{
|
2006-03-23 08:50:25 -07:00
|
|
|
if (type < 0) {
|
2015-06-18 07:03:47 -06:00
|
|
|
pr_warn("adapter type unspecified\n");
|
2006-03-23 08:50:25 -07:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type >= ARRAY_SIZE(adapter_parm)) {
|
2015-06-18 07:03:47 -06:00
|
|
|
pr_warn("invalid type (%d)\n", type);
|
2006-03-23 08:50:25 -07:00
|
|
|
return -ENODEV;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2006-01-09 15:19:51 -07:00
|
|
|
|
2005-10-07 16:17:35 -06:00
|
|
|
return parport_register_driver(&i2c_parport_driver);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit i2c_parport_exit(void)
|
|
|
|
{
|
2005-10-07 16:17:35 -06:00
|
|
|
parport_unregister_driver(&i2c_parport_driver);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2014-01-29 12:40:08 -07:00
|
|
|
MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
|
2005-04-16 16:20:36 -06:00
|
|
|
MODULE_DESCRIPTION("I2C bus over parallel port");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
2015-05-20 09:26:59 -06:00
|
|
|
module_param_array(parport, int, NULL, 0);
|
|
|
|
MODULE_PARM_DESC(parport,
|
|
|
|
"List of parallel ports to bind to, by index.\n"
|
|
|
|
" Atmost " __stringify(MAX_DEVICE) " devices are supported.\n"
|
|
|
|
" Default is one device connected to parport0.\n"
|
|
|
|
);
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
module_init(i2c_parport_init);
|
|
|
|
module_exit(i2c_parport_exit);
|