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
47 * struct for a driver for a i2c chip (tuner, soundprocessor,
50 * a driver will register within the i2c module. The i2c module will
51 * callback the driver (i2c_attach) for every device it finds on a i2c
52 * bus at the specified address. If the driver decides to "accept"
53 * the, device, it must return a struct i2c_device, and NULL
56 * i2c_detach = i2c_attach ** -1
58 * i2c_command will be used to pass commands to the driver in a
65 char name
[32]; /* some useful label */
66 int id
; /* device type ID */
67 unsigned char addr_l
, addr_h
; /* address range of the chip */
69 int (*attach
)(struct i2c_device
*device
);
70 int (*detach
)(struct i2c_device
*device
);
71 int (*command
)(struct i2c_device
*device
,unsigned int cmd
, void *arg
);
74 struct i2c_device
*devices
[I2C_DEVICE_MAX
];
80 * this holds the informations about a i2c bus available in the system.
82 * a chip with a i2c bus interface (like bt848) registers the bus within
83 * the i2c module. This struct provides functions to access the i2c bus.
85 * One must hold the spinlock to access the i2c bus (XXX: is the irqsave
86 * required? Maybe better use a semaphore?).
87 * [-AC-] having a spinlock_irqsave is only needed if we have drivers wishing
88 * to bang their i2c bus from an interrupt.
90 * attach/detach_inform is a callback to inform the bus driver about
91 * attached chip drivers.
95 /* needed: unsigned long flags */
97 #if LINUX_VERSION_CODE >= 0x020100
99 # define LOCK_FLAGS unsigned long flags;
100 # define LOCK_I2C_BUS(bus) spin_lock_irqsave(&(bus->bus_lock),flags);
101 # define UNLOCK_I2C_BUS(bus) spin_unlock_irqrestore(&(bus->bus_lock),flags);
104 # define LOCK_I2C_BUS(bus) spin_lock(&(bus->bus_lock));
105 # define UNLOCK_I2C_BUS(bus) spin_unlock(&(bus->bus_lock));
108 # define LOCK_FLAGS unsigned long flags;
109 # define LOCK_I2C_BUS(bus) { save_flags(flags); cli(); }
110 # define UNLOCK_I2C_BUS(bus) { restore_flags(flags); }
115 char name
[32]; /* some useful label */
117 void *data
; /* free for use by the bus driver */
119 #if LINUX_VERSION_CODE >= 0x020100
123 /* attach/detach inform callbacks */
124 void (*attach_inform
)(struct i2c_bus
*bus
, int id
);
125 void (*detach_inform
)(struct i2c_bus
*bus
, int id
);
128 void (*i2c_setlines
)(struct i2c_bus
*bus
, int ctrl
, int data
);
129 int (*i2c_getdataline
)(struct i2c_bus
*bus
);
132 int (*i2c_read
)(struct i2c_bus
*bus
, unsigned char addr
);
133 int (*i2c_write
)(struct i2c_bus
*bus
, unsigned char addr
,
134 unsigned char b1
, unsigned char b2
, int both
);
136 /* internal data for i2c module */
137 struct i2c_device
*devices
[I2C_DEVICE_MAX
];
143 * This holds per-device data for a i2c device
148 char name
[32]; /* some useful label */
149 void *data
; /* free for use by the chip driver */
150 unsigned char addr
; /* chip addr */
154 struct i2c_driver
*driver
;
158 /* ------------------------------------------------------------------- */
159 /* i2c module functions */
161 /* register/unregister a i2c bus */
162 int i2c_register_bus(struct i2c_bus
*bus
);
163 int i2c_unregister_bus(struct i2c_bus
*bus
);
165 /* register/unregister a chip driver */
166 int i2c_register_driver(struct i2c_driver
*driver
);
167 int i2c_unregister_driver(struct i2c_driver
*driver
);
169 /* send a command to a chip using the ioctl-like callback interface */
170 int i2c_control_device(struct i2c_bus
*bus
, int id
,
171 unsigned int cmd
, void *arg
);
173 /* i2c bus access functions */
174 void i2c_start(struct i2c_bus
*bus
);
175 void i2c_stop(struct i2c_bus
*bus
);
176 void i2c_one(struct i2c_bus
*bus
);
177 void i2c_zero(struct i2c_bus
*bus
);
178 int i2c_ack(struct i2c_bus
*bus
);
180 int i2c_sendbyte(struct i2c_bus
*bus
,unsigned char data
,int wait_for_ack
);
181 unsigned char i2c_readbyte(struct i2c_bus
*bus
,int last
);
183 /* i2c (maybe) hardware functions */
184 int i2c_read(struct i2c_bus
*bus
, unsigned char addr
);
185 int i2c_write(struct i2c_bus
*bus
, unsigned char addr
,
186 unsigned char b1
, unsigned char b2
, int both
);