Commit c6e8bb2c authored by Jean Delvare's avatar Jean Delvare Committed by Jean Delvare

i2c-parport-light: Port to the new device driver model

Also fix a small race on driver unload: we need to unregister the
i2c adapter before we power it off.
Signed-off-by: default avatarJean Delvare <khali@linux-fr.org>
parent 3af07bd2
/* ------------------------------------------------------------------------ * /* ------------------------------------------------------------------------ *
* i2c-parport.c I2C bus over parallel port * * i2c-parport-light.c I2C bus over parallel port *
* ------------------------------------------------------------------------ * * ------------------------------------------------------------------------ *
Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org> Copyright (C) 2003-2007 Jean Delvare <khali@linux-fr.org>
Based on older i2c-velleman.c driver Based on older i2c-velleman.c driver
Copyright (C) 1995-2000 Simon G. Vogl Copyright (C) 1995-2000 Simon G. Vogl
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/ioport.h> #include <linux/ioport.h>
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/i2c-algo-bit.h> #include <linux/i2c-algo-bit.h>
...@@ -34,6 +35,9 @@ ...@@ -34,6 +35,9 @@
#include "i2c-parport.h" #include "i2c-parport.h"
#define DEFAULT_BASE 0x378 #define DEFAULT_BASE 0x378
#define DRVNAME "i2c-parport-light"
static struct platform_device *pdev;
static u16 base; static u16 base;
module_param(base, ushort, 0); module_param(base, ushort, 0);
...@@ -106,7 +110,7 @@ static struct i2c_algo_bit_data parport_algo_data = { ...@@ -106,7 +110,7 @@ static struct i2c_algo_bit_data parport_algo_data = {
.timeout = HZ, .timeout = HZ,
}; };
/* ----- I2c structure ---------------------------------------------------- */ /* ----- Driver registration ---------------------------------------------- */
static struct i2c_adapter parport_adapter = { static struct i2c_adapter parport_adapter = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
...@@ -116,55 +120,141 @@ static struct i2c_adapter parport_adapter = { ...@@ -116,55 +120,141 @@ static struct i2c_adapter parport_adapter = {
.name = "Parallel port adapter (light)", .name = "Parallel port adapter (light)",
}; };
/* ----- Module loading, unloading and information ------------------------ */ static int __devinit i2c_parport_probe(struct platform_device *pdev)
{
int err;
struct resource *res;
res = platform_get_resource(pdev, IORESOURCE_IO, 0);
if (!request_region(res->start, res->end - res->start + 1, DRVNAME))
return -EBUSY;
/* Reset hardware to a sane state (SCL and SDA high) */
parport_setsda(NULL, 1);
parport_setscl(NULL, 1);
/* Other init if needed (power on...) */
if (adapter_parm[type].init.val)
line_set(1, &adapter_parm[type].init);
parport_adapter.dev.parent = &pdev->dev;
err = i2c_bit_add_bus(&parport_adapter);
if (err) {
dev_err(&pdev->dev, "Unable to register with I2C\n");
goto exit_region;
}
return 0;
exit_region:
release_region(res->start, res->end - res->start + 1);
return err;
}
static int __devexit i2c_parport_remove(struct platform_device *pdev)
{
struct resource *res;
i2c_del_adapter(&parport_adapter);
/* Un-init if needed (power off...) */
if (adapter_parm[type].init.val)
line_set(0, &adapter_parm[type].init);
res = platform_get_resource(pdev, IORESOURCE_IO, 0);
release_region(res->start, res->end - res->start + 1);
return 0;
}
static struct platform_driver i2c_parport_driver = {
.driver = {
.owner = THIS_MODULE,
.name = DRVNAME,
},
.probe = i2c_parport_probe,
.remove = __devexit_p(i2c_parport_remove),
};
static int __init i2c_parport_device_add(u16 address)
{
struct resource res = {
.start = address,
.end = address + 2,
.name = DRVNAME,
.flags = IORESOURCE_IO,
};
int err;
pdev = platform_device_alloc(DRVNAME, -1);
if (!pdev) {
err = -ENOMEM;
printk(KERN_ERR DRVNAME ": Device allocation failed\n");
goto exit;
}
err = platform_device_add_resources(pdev, &res, 1);
if (err) {
printk(KERN_ERR DRVNAME ": Device resource addition failed "
"(%d)\n", err);
goto exit_device_put;
}
err = platform_device_add(pdev);
if (err) {
printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
err);
goto exit_device_put;
}
return 0;
exit_device_put:
platform_device_put(pdev);
exit:
return err;
}
static int __init i2c_parport_init(void) static int __init i2c_parport_init(void)
{ {
int err;
if (type < 0) { if (type < 0) {
printk(KERN_WARNING "i2c-parport: adapter type unspecified\n"); printk(KERN_ERR DRVNAME ": adapter type unspecified\n");
return -ENODEV; return -ENODEV;
} }
if (type >= ARRAY_SIZE(adapter_parm)) { if (type >= ARRAY_SIZE(adapter_parm)) {
printk(KERN_WARNING "i2c-parport: invalid type (%d)\n", type); printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type);
return -ENODEV; return -ENODEV;
} }
if (base == 0) { if (base == 0) {
printk(KERN_INFO "i2c-parport: using default base 0x%x\n", DEFAULT_BASE); pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE);
base = DEFAULT_BASE; base = DEFAULT_BASE;
} }
if (!request_region(base, 3, "i2c-parport"))
return -ENODEV;
if (!adapter_parm[type].getscl.val) if (!adapter_parm[type].getscl.val)
parport_algo_data.getscl = NULL; parport_algo_data.getscl = NULL;
/* Reset hardware to a sane state (SCL and SDA high) */ /* Sets global pdev as a side effect */
parport_setsda(NULL, 1); err = i2c_parport_device_add(base);
parport_setscl(NULL, 1); if (err)
/* Other init if needed (power on...) */ goto exit;
if (adapter_parm[type].init.val)
line_set(1, &adapter_parm[type].init);
if (i2c_bit_add_bus(&parport_adapter) < 0) { err = platform_driver_register(&i2c_parport_driver);
printk(KERN_ERR "i2c-parport: Unable to register with I2C\n"); if (err)
release_region(base, 3); goto exit_device;
return -ENODEV;
}
return 0; return 0;
exit_device:
platform_device_unregister(pdev);
exit:
return err;
} }
static void __exit i2c_parport_exit(void) static void __exit i2c_parport_exit(void)
{ {
/* Un-init if needed (power off...) */ platform_driver_unregister(&i2c_parport_driver);
if (adapter_parm[type].init.val) platform_device_unregister(pdev);
line_set(0, &adapter_parm[type].init);
i2c_del_adapter(&parport_adapter);
release_region(base, 3);
} }
MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment