Path: blob/master/drivers/i2c/busses/i2c-parport-light.c
15111 views
/* ------------------------------------------------------------------------ *1* i2c-parport-light.c I2C bus over parallel port *2* ------------------------------------------------------------------------ *3Copyright (C) 2003-2010 Jean Delvare <[email protected]>45Based on older i2c-velleman.c driver6Copyright (C) 1995-2000 Simon G. Vogl7With some changes from:8Frodo Looijaard <[email protected]>9Kyösti Mälkki <[email protected]>1011This program is free software; you can redistribute it and/or modify12it under the terms of the GNU General Public License as published by13the Free Software Foundation; either version 2 of the License, or14(at your option) any later version.1516This program is distributed in the hope that it will be useful,17but WITHOUT ANY WARRANTY; without even the implied warranty of18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19GNU General Public License for more details.2021You should have received a copy of the GNU General Public License22along with this program; if not, write to the Free Software23Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.24* ------------------------------------------------------------------------ */2526#include <linux/kernel.h>27#include <linux/module.h>28#include <linux/init.h>29#include <linux/delay.h>30#include <linux/platform_device.h>31#include <linux/ioport.h>32#include <linux/i2c.h>33#include <linux/i2c-algo-bit.h>34#include <linux/i2c-smbus.h>35#include <linux/io.h>36#include "i2c-parport.h"3738#define DEFAULT_BASE 0x37839#define DRVNAME "i2c-parport-light"4041static struct platform_device *pdev;4243static u16 base;44module_param(base, ushort, 0);45MODULE_PARM_DESC(base, "Base I/O address");4647static int irq;48module_param(irq, int, 0);49MODULE_PARM_DESC(irq, "IRQ (optional)");5051/* ----- Low-level parallel port access ----------------------------------- */5253static inline void port_write(unsigned char p, unsigned char d)54{55outb(d, base+p);56}5758static inline unsigned char port_read(unsigned char p)59{60return inb(base+p);61}6263/* ----- Unified line operation functions --------------------------------- */6465static inline void line_set(int state, const struct lineop *op)66{67u8 oldval = port_read(op->port);6869/* Touch only the bit(s) needed */70if ((op->inverted && !state) || (!op->inverted && state))71port_write(op->port, oldval | op->val);72else73port_write(op->port, oldval & ~op->val);74}7576static inline int line_get(const struct lineop *op)77{78u8 oldval = port_read(op->port);7980return ((op->inverted && (oldval & op->val) != op->val)81|| (!op->inverted && (oldval & op->val) == op->val));82}8384/* ----- I2C algorithm call-back functions and structures ----------------- */8586static void parport_setscl(void *data, int state)87{88line_set(state, &adapter_parm[type].setscl);89}9091static void parport_setsda(void *data, int state)92{93line_set(state, &adapter_parm[type].setsda);94}9596static int parport_getscl(void *data)97{98return line_get(&adapter_parm[type].getscl);99}100101static int parport_getsda(void *data)102{103return line_get(&adapter_parm[type].getsda);104}105106/* Encapsulate the functions above in the correct structure107Note that getscl will be set to NULL by the attaching code for adapters108that cannot read SCL back */109static struct i2c_algo_bit_data parport_algo_data = {110.setsda = parport_setsda,111.setscl = parport_setscl,112.getsda = parport_getsda,113.getscl = parport_getscl,114.udelay = 50,115.timeout = HZ,116};117118/* ----- Driver registration ---------------------------------------------- */119120static struct i2c_adapter parport_adapter = {121.owner = THIS_MODULE,122.class = I2C_CLASS_HWMON,123.algo_data = &parport_algo_data,124.name = "Parallel port adapter (light)",125};126127/* SMBus alert support */128static struct i2c_smbus_alert_setup alert_data = {129.alert_edge_triggered = 1,130};131static struct i2c_client *ara;132static struct lineop parport_ctrl_irq = {133.val = (1 << 4),134.port = PORT_CTRL,135};136137static int __devinit i2c_parport_probe(struct platform_device *pdev)138{139int err;140141/* Reset hardware to a sane state (SCL and SDA high) */142parport_setsda(NULL, 1);143parport_setscl(NULL, 1);144/* Other init if needed (power on...) */145if (adapter_parm[type].init.val) {146line_set(1, &adapter_parm[type].init);147/* Give powered devices some time to settle */148msleep(100);149}150151parport_adapter.dev.parent = &pdev->dev;152err = i2c_bit_add_bus(&parport_adapter);153if (err) {154dev_err(&pdev->dev, "Unable to register with I2C\n");155return err;156}157158/* Setup SMBus alert if supported */159if (adapter_parm[type].smbus_alert && irq) {160alert_data.irq = irq;161ara = i2c_setup_smbus_alert(&parport_adapter, &alert_data);162if (ara)163line_set(1, &parport_ctrl_irq);164else165dev_warn(&pdev->dev, "Failed to register ARA client\n");166}167168return 0;169}170171static int __devexit i2c_parport_remove(struct platform_device *pdev)172{173if (ara) {174line_set(0, &parport_ctrl_irq);175i2c_unregister_device(ara);176ara = NULL;177}178i2c_del_adapter(&parport_adapter);179180/* Un-init if needed (power off...) */181if (adapter_parm[type].init.val)182line_set(0, &adapter_parm[type].init);183184return 0;185}186187static struct platform_driver i2c_parport_driver = {188.driver = {189.owner = THIS_MODULE,190.name = DRVNAME,191},192.probe = i2c_parport_probe,193.remove = __devexit_p(i2c_parport_remove),194};195196static int __init i2c_parport_device_add(u16 address)197{198int err;199200pdev = platform_device_alloc(DRVNAME, -1);201if (!pdev) {202err = -ENOMEM;203printk(KERN_ERR DRVNAME ": Device allocation failed\n");204goto exit;205}206207err = platform_device_add(pdev);208if (err) {209printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",210err);211goto exit_device_put;212}213214return 0;215216exit_device_put:217platform_device_put(pdev);218exit:219return err;220}221222static int __init i2c_parport_init(void)223{224int err;225226if (type < 0) {227printk(KERN_ERR DRVNAME ": adapter type unspecified\n");228return -ENODEV;229}230231if (type >= ARRAY_SIZE(adapter_parm)) {232printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type);233return -ENODEV;234}235236if (base == 0) {237pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE);238base = DEFAULT_BASE;239}240241if (!request_region(base, 3, DRVNAME))242return -EBUSY;243244if (irq != 0)245pr_info(DRVNAME ": using irq %d\n", irq);246247if (!adapter_parm[type].getscl.val)248parport_algo_data.getscl = NULL;249250/* Sets global pdev as a side effect */251err = i2c_parport_device_add(base);252if (err)253goto exit_release;254255err = platform_driver_register(&i2c_parport_driver);256if (err)257goto exit_device;258259return 0;260261exit_device:262platform_device_unregister(pdev);263exit_release:264release_region(base, 3);265return err;266}267268static void __exit i2c_parport_exit(void)269{270platform_driver_unregister(&i2c_parport_driver);271platform_device_unregister(pdev);272release_region(base, 3);273}274275MODULE_AUTHOR("Jean Delvare <[email protected]>");276MODULE_DESCRIPTION("I2C bus over parallel port (light)");277MODULE_LICENSE("GPL");278279module_init(i2c_parport_init);280module_exit(i2c_parport_exit);281282283