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
38 #define I2C_DRIVERID_VIDEODECODER 4
39 #define I2C_DRIVERID_VIDEOENCODER 5
41 #define I2C_BUSID_BT848 1 /* I2C bus on a BT848 */
42 #define I2C_BUSID_PARPORT 2 /* Bit banging on a parallel port */
43 #define I2C_BUSID_BUZ 3
44 #define I2C_BUSID_ZORAN 4
45 #define I2C_BUSID_CYBER2000 5
48 * struct for a driver for a i2c chip (tuner, soundprocessor,
51 * a driver will register within the i2c module. The i2c module will
52 * callback the driver (i2c_attach) for every device it finds on a i2c
53 * bus at the specified address. If the driver decides to "accept"
54 * the, device, it must return a struct i2c_device, and NULL
57 * i2c_detach = i2c_attach ** -1
59 * i2c_command will be used to pass commands to the driver in a
66 char name
[32]; /* some useful label */
67 int id
; /* device type ID */
68 unsigned char addr_l
, addr_h
; /* address range of the chip */
70 int (*attach
)(struct i2c_device
*device
);
71 int (*detach
)(struct i2c_device
*device
);
72 int (*command
)(struct i2c_device
*device
,unsigned int cmd
, void *arg
);
75 struct i2c_device
*devices
[I2C_DEVICE_MAX
];
81 * this holds the informations about a i2c bus available in the system.
83 * a chip with a i2c bus interface (like bt848) registers the bus within
84 * the i2c module. This struct provides functions to access the i2c bus.
86 * One must hold the spinlock to access the i2c bus (XXX: is the irqsave
87 * required? Maybe better use a semaphore?).
88 * [-AC-] having a spinlock_irqsave is only needed if we have drivers wishing
89 * to bang their i2c bus from an interrupt.
91 * attach/detach_inform is a callback to inform the bus driver about
92 * attached chip drivers.
96 /* needed: unsigned long flags */
98 #if LINUX_VERSION_CODE >= 0x020100
100 # define LOCK_FLAGS unsigned long flags;
101 # define LOCK_I2C_BUS(bus) spin_lock_irqsave(&(bus->bus_lock),flags);
102 # define UNLOCK_I2C_BUS(bus) spin_unlock_irqrestore(&(bus->bus_lock),flags);
105 # define LOCK_I2C_BUS(bus) spin_lock(&(bus->bus_lock));
106 # define UNLOCK_I2C_BUS(bus) spin_unlock(&(bus->bus_lock));
109 # define LOCK_FLAGS unsigned long flags;
110 # define LOCK_I2C_BUS(bus) { save_flags(flags); cli(); }
111 # define UNLOCK_I2C_BUS(bus) { restore_flags(flags); }
116 char name
[32]; /* some useful label */
118 void *data
; /* free for use by the bus driver */
120 #if LINUX_VERSION_CODE >= 0x020100
124 /* attach/detach inform callbacks */
125 void (*attach_inform
)(struct i2c_bus
*bus
, int id
);
126 void (*detach_inform
)(struct i2c_bus
*bus
, int id
);
129 void (*i2c_setlines
)(struct i2c_bus
*bus
, int ctrl
, int data
);
130 int (*i2c_getdataline
)(struct i2c_bus
*bus
);
133 int (*i2c_read
)(struct i2c_bus
*bus
, unsigned char addr
);
134 int (*i2c_write
)(struct i2c_bus
*bus
, unsigned char addr
,
135 unsigned char b1
, unsigned char b2
, int both
);
137 /* internal data for i2c module */
138 struct i2c_device
*devices
[I2C_DEVICE_MAX
];
144 * This holds per-device data for a i2c device
149 char name
[32]; /* some useful label */
150 void *data
; /* free for use by the chip driver */
151 unsigned char addr
; /* chip addr */
155 struct i2c_driver
*driver
;
159 /* ------------------------------------------------------------------- */
160 /* i2c module functions */
162 /* register/unregister a i2c bus */
163 int i2c_register_bus(struct i2c_bus
*bus
);
164 int i2c_unregister_bus(struct i2c_bus
*bus
);
166 /* register/unregister a chip driver */
167 int i2c_register_driver(struct i2c_driver
*driver
);
168 int i2c_unregister_driver(struct i2c_driver
*driver
);
170 /* send a command to a chip using the ioctl-like callback interface */
171 int i2c_control_device(struct i2c_bus
*bus
, int id
,
172 unsigned int cmd
, void *arg
);
174 /* i2c bus access functions */
175 void i2c_start(struct i2c_bus
*bus
);
176 void i2c_stop(struct i2c_bus
*bus
);
177 void i2c_one(struct i2c_bus
*bus
);
178 void i2c_zero(struct i2c_bus
*bus
);
179 int i2c_ack(struct i2c_bus
*bus
);
181 int i2c_sendbyte(struct i2c_bus
*bus
,unsigned char data
,int wait_for_ack
);
182 unsigned char i2c_readbyte(struct i2c_bus
*bus
,int last
);
184 /* i2c (maybe) hardware functions */
185 int i2c_read(struct i2c_bus
*bus
, unsigned char addr
);
186 int i2c_write(struct i2c_bus
*bus
, unsigned char addr
,
187 unsigned char b1
, unsigned char b2
, int both
);