5 * linux i2c interface. Works a little bit like the scsi subsystem.
8 * i2c the basic control module (like scsi_mod)
9 * bus driver a driver with a i2c bus (hostadapter driver)
10 * chip driver a driver for a chip connected
11 * to a i2c bus (cdrom/hd driver)
13 * A device will be attached to one bus and one chip driver. Every chip
14 * driver gets a unique ID.
16 * A chip driver can provide a ioctl-like callback for the
17 * communication with other parts of the kernel (not every i2c chip is
18 * useful without other devices, a TV card tuner for example).
20 * "i2c internal" parts of the structs: only the i2c module is allowed to
21 * write to them, for others they are read-only.
25 #include <linux/version.h>
27 #define I2C_BUS_MAX 4 /* max # of bus drivers */
28 #define I2C_DRIVER_MAX 8 /* max # of chip drivers */
29 #define I2C_DEVICE_MAX 8 /* max # if devices per bus/driver */
35 #define I2C_DRIVERID_MSP3400 1
36 #define I2C_DRIVERID_TUNER 2
37 #define I2C_DRIVERID_VIDEOTEXT 3
39 #define I2C_BUSID_BT848 1 /* I2C bus on a BT848 */
40 #define I2C_BUSID_PARPORT 2 /* Bit banging on a parallel port */
43 * struct for a driver for a i2c chip (tuner, soundprocessor,
46 * a driver will register within the i2c module. The i2c module will
47 * callback the driver (i2c_attach) for every device it finds on a i2c
48 * bus at the specified address. If the driver decides to "accept"
49 * the, device, it must return a struct i2c_device, and NULL
52 * i2c_detach = i2c_attach ** -1
54 * i2c_command will be used to pass commands to the driver in a
61 char name
[32]; /* some useful label */
62 int id
; /* device type ID */
63 unsigned char addr_l
, addr_h
; /* address range of the chip */
65 int (*attach
)(struct i2c_device
*device
);
66 int (*detach
)(struct i2c_device
*device
);
67 int (*command
)(struct i2c_device
*device
,unsigned int cmd
, void *arg
);
70 struct i2c_device
*devices
[I2C_DEVICE_MAX
];
76 * this holds the informations about a i2c bus available in the system.
78 * a chip with a i2c bus interface (like bt848) registers the bus within
79 * the i2c module. This struct provides functions to access the i2c bus.
81 * One must hold the spinlock to access the i2c bus (XXX: is the irqsave
82 * required? Maybe better use a semaphore?).
83 * [-AC-] having a spinlock_irqsave is only needed if we have drivers wishing
84 * to bang their i2c bus from an interrupt.
86 * attach/detach_inform is a callback to inform the bus driver about
87 * attached chip drivers.
91 /* needed: unsigned long flags */
93 #if LINUX_VERSION_CODE >= 0x020100
95 # define LOCK_FLAGS unsigned long flags;
96 # define LOCK_I2C_BUS(bus) spin_lock_irqsave(&(bus->bus_lock),flags);
97 # define UNLOCK_I2C_BUS(bus) spin_unlock_irqrestore(&(bus->bus_lock),flags);
100 # define LOCK_I2C_BUS(bus) spin_lock(&(bus->bus_lock));
101 # define UNLOCK_I2C_BUS(bus) spin_unlock(&(bus->bus_lock));
104 # define LOCK_FLAGS unsigned long flags;
105 # define LOCK_I2C_BUS(bus) { save_flags(flags); cli(); }
106 # define UNLOCK_I2C_BUS(bus) { restore_flags(flags); }
111 char name
[32]; /* some useful label */
113 void *data
; /* free for use by the bus driver */
115 #if LINUX_VERSION_CODE >= 0x020100
119 /* attach/detach inform callbacks */
120 void (*attach_inform
)(struct i2c_bus
*bus
, int id
);
121 void (*detach_inform
)(struct i2c_bus
*bus
, int id
);
124 void (*i2c_setlines
)(struct i2c_bus
*bus
, int ctrl
, int data
);
125 int (*i2c_getdataline
)(struct i2c_bus
*bus
);
128 int (*i2c_read
)(struct i2c_bus
*bus
, unsigned char addr
);
129 int (*i2c_write
)(struct i2c_bus
*bus
, unsigned char addr
,
130 unsigned char b1
, unsigned char b2
, int both
);
132 /* internal data for i2c module */
133 struct i2c_device
*devices
[I2C_DEVICE_MAX
];
139 * This holds per-device data for a i2c device
144 char name
[32]; /* some useful label */
145 void *data
; /* free for use by the chip driver */
146 unsigned char addr
; /* chip addr */
150 struct i2c_driver
*driver
;
154 /* ------------------------------------------------------------------- */
155 /* i2c module functions */
157 /* register/unregister a i2c bus */
158 int i2c_register_bus(struct i2c_bus
*bus
);
159 int i2c_unregister_bus(struct i2c_bus
*bus
);
161 /* register/unregister a chip driver */
162 int i2c_register_driver(struct i2c_driver
*driver
);
163 int i2c_unregister_driver(struct i2c_driver
*driver
);
165 /* send a command to a chip using the ioctl-like callback interface */
166 int i2c_control_device(struct i2c_bus
*bus
, int id
,
167 unsigned int cmd
, void *arg
);
169 /* i2c bus access functions */
170 void i2c_start(struct i2c_bus
*bus
);
171 void i2c_stop(struct i2c_bus
*bus
);
172 void i2c_one(struct i2c_bus
*bus
);
173 void i2c_zero(struct i2c_bus
*bus
);
174 int i2c_ack(struct i2c_bus
*bus
);
176 int i2c_sendbyte(struct i2c_bus
*bus
,unsigned char data
,int wait_for_ack
);
177 unsigned char i2c_readbyte(struct i2c_bus
*bus
,int last
);
179 /* i2c (maybe) hardware functions */
180 int i2c_read(struct i2c_bus
*bus
, unsigned char addr
);
181 int i2c_write(struct i2c_bus
*bus
, unsigned char addr
,
182 unsigned char b1
, unsigned char b2
, int both
);