2005-04-17 06:20:36 +08:00
|
|
|
/* ------------------------------------------------------------------------ *
|
|
|
|
* i2c-parport.c I2C bus over parallel port *
|
|
|
|
* ------------------------------------------------------------------------ *
|
2011-05-02 00:18:49 +08:00
|
|
|
Copyright (C) 2003-2011 Jean Delvare <khali@linux-fr.org>
|
2005-04-17 06:20:36 +08: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-20 05:21:04 +08:00
|
|
|
Kyösti Mälkki <kmalkki@cc.hut.fi>
|
2005-04-17 06:20:36 +08: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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
2010-03-02 19:23:41 +08:00
|
|
|
#include <linux/delay.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/parport.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/i2c-algo-bit.h>
|
2010-03-02 19:23:44 +08: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 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2011-05-02 00:18:49 +08:00
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/mutex.h>
|
2005-04-17 06:20:36 +08: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 19:23:44 +08:00
|
|
|
struct i2c_smbus_alert_setup alert_data;
|
|
|
|
struct i2c_client *ara;
|
2011-05-02 00:18:49 +08:00
|
|
|
struct list_head node;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2011-05-02 00:18:49 +08:00
|
|
|
static LIST_HEAD(adapter_list);
|
|
|
|
static DEFINE_MUTEX(adapter_list_lock);
|
2005-04-17 06:20:36 +08: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void (*port_write[])(struct parport *, unsigned char) = {
|
|
|
|
port_write_data,
|
|
|
|
NULL,
|
|
|
|
port_write_control,
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned char (*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
|
2005-05-20 03:39:06 +08:00
|
|
|
cannot read SCL back, and will also make the data field point to
|
2005-04-17 06:20:36 +08:00
|
|
|
the parallel port structure. */
|
2010-05-22 00:40:57 +08:00
|
|
|
static const struct i2c_algo_bit_data parport_algo_data = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.setsda = parport_setsda,
|
|
|
|
.setscl = parport_setscl,
|
|
|
|
.getsda = parport_getsda,
|
|
|
|
.getscl = parport_getscl,
|
2007-05-02 05:26:30 +08:00
|
|
|
.udelay = 10, /* ~50 kbps */
|
2005-04-17 06:20:36 +08:00
|
|
|
.timeout = HZ,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ----- I2c and parallel port call-back functions and structures --------- */
|
|
|
|
|
2010-03-02 19:23:44 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static void i2c_parport_attach (struct parport *port)
|
|
|
|
{
|
|
|
|
struct i2c_par *adapter;
|
|
|
|
|
2005-10-18 05:09:43 +08:00
|
|
|
adapter = kzalloc(sizeof(struct i2c_par), GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (adapter == NULL) {
|
2005-10-18 05:09:43 +08:00
|
|
|
printk(KERN_ERR "i2c-parport: Failed to kzalloc\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_debug("i2c-parport: attaching to %s\n", port->name);
|
2010-03-02 19:23:44 +08:00
|
|
|
parport_disable_irq(port);
|
2005-04-17 06:20:36 +08:00
|
|
|
adapter->pdev = parport_register_device(port, "i2c-parport",
|
2010-03-02 19:23:44 +08:00
|
|
|
NULL, NULL, i2c_parport_irq, PARPORT_FLAG_EXCL, adapter);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!adapter->pdev) {
|
|
|
|
printk(KERN_ERR "i2c-parport: Unable to register with parport\n");
|
|
|
|
goto ERROR0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill the rest of the structure */
|
2007-05-02 05:26:29 +08: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-17 06:20:36 +08:00
|
|
|
adapter->algo_data = parport_algo_data;
|
2007-05-02 05:26:30 +08:00
|
|
|
/* Slow down if we can't sense SCL */
|
|
|
|
if (!adapter_parm[type].getscl.val) {
|
2005-04-17 06:20:36 +08:00
|
|
|
adapter->algo_data.getscl = NULL;
|
2007-05-02 05:26:30 +08:00
|
|
|
adapter->algo_data.udelay = 50; /* ~10 kbps */
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
adapter->algo_data.data = port;
|
|
|
|
adapter->adapter.algo_data = &adapter->algo_data;
|
2007-05-08 15:27:42 +08:00
|
|
|
adapter->adapter.dev.parent = port->physport->dev;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (parport_claim_or_block(adapter->pdev) < 0) {
|
|
|
|
printk(KERN_ERR "i2c-parport: Could not claim parallel port\n");
|
|
|
|
goto ERROR1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 19:23:41 +08:00
|
|
|
if (adapter_parm[type].init.val) {
|
2005-04-17 06:20:36 +08:00
|
|
|
line_set(port, 1, &adapter_parm[type].init);
|
2010-03-02 19:23:41 +08:00
|
|
|
/* Give powered devices some time to settle */
|
|
|
|
msleep(100);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (i2c_bit_add_bus(&adapter->adapter) < 0) {
|
|
|
|
printk(KERN_ERR "i2c-parport: Unable to register with I2C\n");
|
|
|
|
goto ERROR1;
|
|
|
|
}
|
|
|
|
|
2010-03-02 19:23:44 +08:00
|
|
|
/* Setup SMBus alert if supported */
|
|
|
|
if (adapter_parm[type].smbus_alert) {
|
|
|
|
adapter->alert_data.alert_edge_triggered = 1;
|
|
|
|
adapter->ara = i2c_setup_smbus_alert(&adapter->adapter,
|
|
|
|
&adapter->alert_data);
|
|
|
|
if (adapter->ara)
|
|
|
|
parport_enable_irq(port);
|
|
|
|
else
|
|
|
|
printk(KERN_WARNING "i2c-parport: Failed to register "
|
|
|
|
"ARA client\n");
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Add the new adapter to the list */
|
2011-05-02 00:18:49 +08:00
|
|
|
mutex_lock(&adapter_list_lock);
|
|
|
|
list_add_tail(&adapter->node, &adapter_list);
|
|
|
|
mutex_unlock(&adapter_list_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
ERROR1:
|
2008-11-28 22:24:39 +08:00
|
|
|
parport_release(adapter->pdev);
|
2005-04-17 06:20:36 +08:00
|
|
|
parport_unregister_device(adapter->pdev);
|
|
|
|
ERROR0:
|
|
|
|
kfree(adapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void i2c_parport_detach (struct parport *port)
|
|
|
|
{
|
2011-05-02 00:18:49 +08:00
|
|
|
struct i2c_par *adapter, *_n;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Walk the list */
|
2011-05-02 00:18:49 +08:00
|
|
|
mutex_lock(&adapter_list_lock);
|
|
|
|
list_for_each_entry_safe(adapter, _n, &adapter_list, node) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (adapter->pdev->port == port) {
|
2010-03-02 19:23:44 +08:00
|
|
|
if (adapter->ara) {
|
|
|
|
parport_disable_irq(port);
|
|
|
|
i2c_unregister_device(adapter->ara);
|
|
|
|
}
|
2007-05-02 05:26:30 +08:00
|
|
|
i2c_del_adapter(&adapter->adapter);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Un-init if needed (power off...) */
|
|
|
|
if (adapter_parm[type].init.val)
|
|
|
|
line_set(port, 0, &adapter_parm[type].init);
|
|
|
|
|
2008-11-28 22:24:39 +08:00
|
|
|
parport_release(adapter->pdev);
|
2005-04-17 06:20:36 +08:00
|
|
|
parport_unregister_device(adapter->pdev);
|
2011-05-02 00:18:49 +08:00
|
|
|
list_del(&adapter->node);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(adapter);
|
|
|
|
}
|
|
|
|
}
|
2011-05-02 00:18:49 +08:00
|
|
|
mutex_unlock(&adapter_list_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-10-08 06:17:35 +08:00
|
|
|
static struct parport_driver i2c_parport_driver = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.name = "i2c-parport",
|
|
|
|
.attach = i2c_parport_attach,
|
|
|
|
.detach = i2c_parport_detach,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ----- Module loading, unloading and information ------------------------ */
|
|
|
|
|
|
|
|
static int __init i2c_parport_init(void)
|
|
|
|
{
|
2006-03-23 23:50:25 +08:00
|
|
|
if (type < 0) {
|
|
|
|
printk(KERN_WARNING "i2c-parport: adapter type unspecified\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type >= ARRAY_SIZE(adapter_parm)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
printk(KERN_WARNING "i2c-parport: invalid type (%d)\n", type);
|
2006-03-23 23:50:25 +08:00
|
|
|
return -ENODEV;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-01-10 06:19:51 +08:00
|
|
|
|
2005-10-08 06:17:35 +08:00
|
|
|
return parport_register_driver(&i2c_parport_driver);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit i2c_parport_exit(void)
|
|
|
|
{
|
2005-10-08 06:17:35 +08:00
|
|
|
parport_unregister_driver(&i2c_parport_driver);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
|
|
|
|
MODULE_DESCRIPTION("I2C bus over parallel port");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
module_init(i2c_parport_init);
|
|
|
|
module_exit(i2c_parport_exit);
|