1 #ifndef I2C_H
2 #define I2C_H
3 
4 /*
5  * linux i2c interface.  Works a little bit like the scsi subsystem.
6  * There are:
7  *
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)
12  *
13  * A device will be attached to one bus and one chip driver.  Every chip
14  * driver gets a unique ID.
15  *
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).
19  *
20  * "i2c internal" parts of the structs: only the i2c module is allowed to
21  * write to them, for others they are read-only.
22  *
23  */
24 
25 #include <linux/version.h>
26 
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 */
30 
31 struct i2c_bus;
32 struct i2c_driver;
33 struct i2c_device;
34 
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
40 
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
46 
47 /*
48  * struct for a driver for a i2c chip (tuner, soundprocessor,
49  * videotext, ... ).
50  *
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
55  * otherwise.
56  *
57  * i2c_detach = i2c_attach ** -1
58  *
59  * i2c_command will be used to pass commands to the driver in a
60  * ioctl-line manner.
61  *
62  */
63 
64 struct i2c_driver
65 {
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 */
69 
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);
73 
74     /* i2c internal */
75     struct i2c_device   *devices[I2C_DEVICE_MAX];
76     int                 devcount;
77 };
78 
79 
80 /*
81  * this holds the informations about a i2c bus available in the system.
82  *
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.
85  *
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.
90  *
91  * attach/detach_inform is a callback to inform the bus driver about
92  * attached chip drivers.
93  *
94  */
95 
96 /* needed: unsigned long flags */
97 
98 #if LINUX_VERSION_CODE >= 0x020100
99 # if 0
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);
103 # else
104 #  define LOCK_FLAGS
105 #  define LOCK_I2C_BUS(bus)    spin_lock(&(bus->bus_lock));
106 #  define UNLOCK_I2C_BUS(bus)  spin_unlock(&(bus->bus_lock));
107 # endif
108 #else
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);     }
112 #endif
113 
114 struct i2c_bus
115 {
116 	char  name[32];         /* some useful label */
117 	int   id;
118 	void  *data;            /* free for use by the bus driver */
119 
120 #if LINUX_VERSION_CODE >= 0x020100
121 	spinlock_t bus_lock;
122 #endif
123 
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);
127 
128 	/* Software I2C */
129 	void    (*i2c_setlines)(struct i2c_bus *bus, int ctrl, int data);
130 	int     (*i2c_getdataline)(struct i2c_bus *bus);
131 
132 	/* Hardware I2C */
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);
136 
137 	/* internal data for i2c module */
138 	struct i2c_device   *devices[I2C_DEVICE_MAX];
139 	int                 devcount;
140 };
141 
142 
143 /*
144  *	This holds per-device data for a i2c device
145  */
146 
147 struct i2c_device
148 {
149 	char           name[32];         /* some useful label */
150 	void           *data;            /* free for use by the chip driver */
151 	unsigned char  addr;             /* chip addr */
152 
153 	/* i2c internal */
154 	struct i2c_bus     *bus;
155 	struct i2c_driver  *driver;
156 };
157 
158 
159 /* ------------------------------------------------------------------- */
160 /* i2c module functions                                                */
161 
162 /* register/unregister a i2c bus */
163 int i2c_register_bus(struct i2c_bus *bus);
164 int i2c_unregister_bus(struct i2c_bus *bus);
165 
166 /* register/unregister a chip driver */
167 int i2c_register_driver(struct i2c_driver *driver);
168 int i2c_unregister_driver(struct i2c_driver *driver);
169 
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);
173 
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);
180 
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);
183 
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);
188 
189 int	i2c_init(void);
190 #endif /* I2C_H */
191