“a08485d8fdf6f67ca5f173b68d8f873c574745f2”上不存在“README.md”
i2c-parport-light.c 6.8 KB
Newer Older
L
Linus Torvalds 已提交
1
/* ------------------------------------------------------------------------ *
2
 * i2c-parport-light.c I2C bus over parallel port                           *
L
Linus Torvalds 已提交
3
 * ------------------------------------------------------------------------ *
4
   Copyright (C) 2003-2010 Jean Delvare <khali@linux-fr.org>
5

L
Linus Torvalds 已提交
6 7 8 9
   Based on older i2c-velleman.c driver
   Copyright (C) 1995-2000 Simon G. Vogl
   With some changes from:
   Frodo Looijaard <frodol@dds.nl>
10
   Kyösti Mälkki <kmalkki@cc.hut.fi>
11

L
Linus Torvalds 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   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>
30
#include <linux/delay.h>
31
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
32 33 34
#include <linux/ioport.h>
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>
35
#include <linux/i2c-smbus.h>
36
#include <linux/io.h>
L
Linus Torvalds 已提交
37 38 39
#include "i2c-parport.h"

#define DEFAULT_BASE 0x378
40 41 42
#define DRVNAME "i2c-parport-light"

static struct platform_device *pdev;
L
Linus Torvalds 已提交
43 44 45 46 47

static u16 base;
module_param(base, ushort, 0);
MODULE_PARM_DESC(base, "Base I/O address");

48 49 50 51
static int irq;
module_param(irq, int, 0);
MODULE_PARM_DESC(irq, "IRQ (optional)");

L
Linus Torvalds 已提交
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
/* ----- Low-level parallel port access ----------------------------------- */

static inline void port_write(unsigned char p, unsigned char d)
{
	outb(d, base+p);
}

static inline unsigned char port_read(unsigned char p)
{
	return inb(base+p);
}

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

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

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

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

	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(state, &adapter_parm[type].setscl);
}

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

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

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

/* Encapsulate the functions above in the correct structure
   Note that getscl will be set to NULL by the attaching code for adapters
   that cannot read SCL back */
static struct i2c_algo_bit_data parport_algo_data = {
	.setsda		= parport_setsda,
	.setscl		= parport_setscl,
	.getsda		= parport_getsda,
	.getscl		= parport_getscl,
	.udelay		= 50,
	.timeout	= HZ,
117
};
L
Linus Torvalds 已提交
118

119
/* ----- Driver registration ---------------------------------------------- */
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127

static struct i2c_adapter parport_adapter = {
	.owner		= THIS_MODULE,
	.class		= I2C_CLASS_HWMON,
	.algo_data	= &parport_algo_data,
	.name		= "Parallel port adapter (light)",
};

128 129 130 131 132 133 134
/* SMBus alert support */
static struct i2c_smbus_alert_setup alert_data = {
	.alert_edge_triggered	= 1,
};
static struct i2c_client *ara;
static struct lineop parport_ctrl_irq = {
	.val		= (1 << 4),
135
	.port		= PORT_CTRL,
136 137
};

138
static int i2c_parport_probe(struct platform_device *pdev)
139 140 141 142 143 144 145
{
	int err;

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

	parport_adapter.dev.parent = &pdev->dev;
	err = i2c_bit_add_bus(&parport_adapter);
154
	if (err) {
155
		dev_err(&pdev->dev, "Unable to register with I2C\n");
156 157 158 159 160 161 162 163 164 165 166 167 168 169
		return err;
	}

	/* Setup SMBus alert if supported */
	if (adapter_parm[type].smbus_alert && irq) {
		alert_data.irq = irq;
		ara = i2c_setup_smbus_alert(&parport_adapter, &alert_data);
		if (ara)
			line_set(1, &parport_ctrl_irq);
		else
			dev_warn(&pdev->dev, "Failed to register ARA client\n");
	}

	return 0;
170 171
}

172
static int i2c_parport_remove(struct platform_device *pdev)
173
{
174 175 176 177 178
	if (ara) {
		line_set(0, &parport_ctrl_irq);
		i2c_unregister_device(ara);
		ara = NULL;
	}
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	i2c_del_adapter(&parport_adapter);

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

	return 0;
}

static struct platform_driver i2c_parport_driver = {
	.driver = {
		.owner	= THIS_MODULE,
		.name	= DRVNAME,
	},
	.probe		= i2c_parport_probe,
194
	.remove		= i2c_parport_remove,
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
};

static int __init i2c_parport_device_add(u16 address)
{
	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(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;
}
L
Linus Torvalds 已提交
222 223 224

static int __init i2c_parport_init(void)
{
225 226
	int err;

227
	if (type < 0) {
228
		printk(KERN_ERR DRVNAME ": adapter type unspecified\n");
229 230 231 232
		return -ENODEV;
	}

	if (type >= ARRAY_SIZE(adapter_parm)) {
233
		printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type);
234
		return -ENODEV;
L
Linus Torvalds 已提交
235
	}
236

L
Linus Torvalds 已提交
237
	if (base == 0) {
238
		pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE);
L
Linus Torvalds 已提交
239 240 241
		base = DEFAULT_BASE;
	}

242 243 244
	if (!request_region(base, 3, DRVNAME))
		return -EBUSY;

245 246 247
	if (irq != 0)
		pr_info(DRVNAME ": using irq %d\n", irq);

248
	if (!adapter_parm[type].getscl.val)
L
Linus Torvalds 已提交
249 250
		parport_algo_data.getscl = NULL;

251 252 253
	/* Sets global pdev as a side effect */
	err = i2c_parport_device_add(base);
	if (err)
254
		goto exit_release;
L
Linus Torvalds 已提交
255

256 257 258
	err = platform_driver_register(&i2c_parport_driver);
	if (err)
		goto exit_device;
259

L
Linus Torvalds 已提交
260
	return 0;
261 262 263

exit_device:
	platform_device_unregister(pdev);
264 265
exit_release:
	release_region(base, 3);
266
	return err;
L
Linus Torvalds 已提交
267 268 269 270
}

static void __exit i2c_parport_exit(void)
{
271 272
	platform_driver_unregister(&i2c_parport_driver);
	platform_device_unregister(pdev);
273
	release_region(base, 3);
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281
}

MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
MODULE_DESCRIPTION("I2C bus over parallel port (light)");
MODULE_LICENSE("GPL");

module_init(i2c_parport_init);
module_exit(i2c_parport_exit);
新手
引导
客服 返回
顶部