Implement RGB gain/balance
[microdia.git] / sn9c20x.c
blob64712b1c9b7c84b338ac2cad8c5c468771f26f16
1 /**
2 * @file sn9c20x.c
3 * @author Dave Neuer
4 * @date 2008-03-02
5 * @version v0.0.0
7 * @brief Common functions and data for the Sonix SN9C20x webcam bridge chips.
9 * @note Copyright (C) Dave Neuer
11 * @par Licences
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/delay.h>
29 #include <linux/errno.h>
30 #include <linux/string.h>
31 #include "microdia.h"
32 #include "sn9c20x.h"
35 int sn9c20x_i2c_ack_wait(struct usb_microdia *, bool, bool *);
38 /**
39 * @brief Initializes IC2-registers 0x10c0-0x10c7
41 * @param dev Pointer to the device
42 * @param flags The appropriate flags for bus speed and physical connection
43 * @param slave The id of the I2C slave device
45 * @return Zero (success) or negative (USB-error value)
48 int sn9c20x_initialize_i2c(struct usb_microdia * dev, __u8 flags, __u8 slave)
50 __u8 buf[8];
51 int ret;
53 buf[0] = 0x81 & flags;
54 buf[1] = slave;
55 buf[2] = 0x00;
56 buf[3] = 0x00;
57 buf[4] = 0x00;
58 buf[5] = 0x00;
59 buf[6] = 0x00;
60 buf[7] = 0x00;
61 ret = usb_microdia_control_write(dev, 0x10c0, buf, 8);
62 if (ret < 0)
63 return ret;
64 else
65 return 0;
68 /**
69 * @brief Read up to 5 bytes of data from an I2C slave
71 * @param dev Pointer to the device
72 * @param slave The id of the I2C slave device
73 * @param nbytes Number of bytes to read
74 * @param address The address of the register on the slave to read
75 * @param flags The appropriate flags for bus speed and physical connection
76 * @param result A pointer to the location at which the result should be stored
78 * @return Zero for success or a negative error value
81 int sn9c20x_read_i2c_data(struct usb_microdia * dev, __u8 slave, __u8 nbytes, __u8 address,
82 __u8 flags, __u8 * result)
84 int ret, i, j;
85 __u8 row[5];
87 if(!dev || nbytes > 4)
88 return -EINVAL;
90 /* first, we must do a dummy write of just the address */
91 ret = sn9c20x_write_i2c_data(dev, slave, 0, address, flags, NULL);
92 if(ret < 0)
93 return ret;
95 memset(row, 0, 5);
96 /* now we issue the same command but with the read bit set
97 * and no slave register address */
98 ret = sn9c20x_write_i2c_data(dev, slave, nbytes - 1, 0, flags |
99 SN9C20X_I2C_READ, row);
100 if(ret < 0)
101 return ret;
103 /* finally, ask the bridge for the data */
104 ret = usb_microdia_control_read(dev, 0x10c2, row, 5);
105 if(ret < 0)
106 return ret;
108 for(i = 0, j = 5 - nbytes; i < nbytes; i++, j++)
109 result[i] = row[j];
111 return 0;
115 * @brief Read up to 4 bytes of data from an I2C slave an return them as 16bit values
117 * @param dev Pointer to the device
118 * @param slave The id of the I2C slave device
119 * @param datalen Number of 16bit values to read
120 * @param address The address of the register on the slave to read
121 * @param flags The appropriate flags for bus speed and physical connection
122 * @param result A pointer to the location at which the result should be stored
124 * @return Zero for success or a negative error value
127 int sn9c20x_read_i2c_data16(struct usb_microdia * dev, __u8 slave, __u8 datalen, __u8 address,
128 __u8 flags, __u16 * result)
130 __u8 result8[4];
131 __u8 k;
132 int ret;
134 if(datalen > 2) return -EINVAL;
135 ret = sn9c20x_read_i2c_data(dev, slave, 2*datalen, address, flags, result8);
136 for (k=0; k<datalen; k++)
137 result[k] = (result8[k*2] << 8) | result8[k*2+1];
138 return ret;
141 static const char *wasread = "read from";
142 static const char *waswrite = "write to";
145 * @brief Write up to 5 bytes of data to an I2C slave
147 * @param dev Pointer to the device
148 * @param slave The id of the I2C slave device
149 * @param nbytes The number of bytes of data
150 * @param address The address of the register on the slave to write
151 * @param flags The appropriate flags for bus speed and physical connection
152 * @param data An array containing the data to write
154 * @return Zero for success or a negative error value
157 int sn9c20x_write_i2c_data(struct usb_microdia * dev, __u8 slave, __u8 nbytes,
158 __u8 address, __u8 flags, const __u8 data[nbytes])
160 int ret, i;
161 __u8 row[8];
162 bool slave_error = 0;
164 if(!dev || (nbytes > 0 && !data) || nbytes > 4)
165 return -EINVAL;
167 /* from the point of view of the bridge, the length
168 * includes the address */
169 row[0] = flags | ((nbytes + 1) << 4);
170 row[1] = slave;
171 row[2] = address;
172 row[7] = 0x10; /* I think this means we want an ack */
174 for(i = 0; i < 4; i++)
175 row[i + 3] = i < nbytes ? data[i] : 0;
177 UDIA_DEBUG("I2C %s %02x: %02x %02x %02x %02x %02x %02x %02x %02x\n",
178 (flags & SN9C20X_I2C_READ ? wasread : waswrite), address,
179 row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]);
181 ret = usb_microdia_control_write(dev, 0x10c0, row, 8);
182 if(ret >= 0)
183 ret = sn9c20x_i2c_ack_wait(dev, flags & SN9C20X_I2C_400KHZ,
184 &slave_error);
186 if(slave_error) {
187 UDIA_ERROR("I2C slave 0x%02x returned error during %s address 0x%02x\n",
188 slave,
189 (flags & SN9C20X_I2C_READ ? wasread : waswrite),
190 address);
191 return -1000; // there should be no interference with USB errors
194 if(ret < 0) {
195 /* we got no ack */
196 UDIA_ERROR("No ack from I2C slave 0x%02x for %s address 0x%02x\n",
197 slave,
198 (flags & SN9C20X_I2C_READ ? wasread : waswrite),
199 address);
200 return ret;
203 return 0;
207 * @brief Write up to 2 16bit values als single bytes to an I2C slave
209 * @param dev Pointer to the device
210 * @param slave The id of the I2C slave device
211 * @param datalen The number of 16bit data values to write
212 * @param address The address of the register on the slave to write
213 * @param flags The appropriate flags for bus speed and physical connection
214 * @param data An array containing the data to write
216 * @return Zero for success or a negative error value
219 int sn9c20x_write_i2c_data16(struct usb_microdia * dev, __u8 slave, __u8 datalen,
220 __u8 address, __u8 flags, const __u16 data[datalen])
222 __u8 data8[4];
223 __u8 k;
224 int ret;
226 if(datalen > 2) return -EINVAL;
227 for (k=0; k<datalen; k++)
229 data8[k*2] = data[k] >> 8;
230 data8[k*2+1] = data[k] & 0xff;
232 ret = sn9c20x_write_i2c_data(dev, slave, 2*datalen, address, flags, data8);
233 return ret;
237 * @brief Wait until the I2C slave is ready for the next operation
239 * @param dev Pointer to the device
240 * @param highspeed
241 * @param slave_error
243 * @return Zero for success or a negative error value
246 int sn9c20x_i2c_ack_wait(struct usb_microdia *dev, bool highspeed, bool * slave_error)
248 int ret, i;
249 __u8 readbuf;
250 int delay = highspeed ? 100 : 400;
252 for(i = 0; i< 5; i++) {
253 ret = usb_microdia_control_read(dev, 0x10c0, &readbuf, 1);
255 if(ret < 0)
256 return ret;
257 else if(readbuf & SN9C20X_I2C_ERROR) {
258 *slave_error = 1;
259 /* probably should come up w/ an error value and
260 * return it via the error return */
261 return 0;
263 else if(readbuf & SN9C20X_I2C_READY)
264 return 0;
265 else
266 udelay(delay);
269 return -EBUSY;
273 int sn9c20x_set_contrast(struct usb_microdia *dev)
275 /* from 0x26 to 0x4b */
276 __u8 brightness_contrast[21] = {0x16, 0x0, 0x2b, 0x0, 0x8, 0x0, 0xf6, 0x0f,
277 0xd2, 0x0f, 0x38, 0x0, 0x34, 0x0, 0xcf, 0x0f,
278 0xfd, 0x0f, 0x0, 0x0, 0x0};
279 __u8 contrast_val = (dev->vsettings.contrast >> 8) * 0x25 / 0x100;
280 __u8 brightness_val = dev->vsettings.brightness >> 8;
282 brightness_val -= 0x80;
283 brightness_contrast[18] = brightness_val;
285 contrast_val += 0x26;
286 brightness_contrast[2] = contrast_val;
287 brightness_contrast[0] = 0x13 + (brightness_contrast[2] - 0x26) * 0x13 / 0x25;
288 brightness_contrast[4] = 0x7 + (brightness_contrast[2] - 0x26) * 0x7 / 0x25;
290 return usb_microdia_control_write(dev, 0x10e1, brightness_contrast, 21);
293 int sn9c20x_set_brightness(struct usb_microdia *dev)
295 return dev_microdia_camera_set_contrast(dev);
298 int sn9c20x_set_gamma(struct usb_microdia *dev)
300 int value = (dev->vsettings.whiteness >> 8) * 0xb8 / 0x100;
301 int r = 0;
303 __u8 gamma_val[17] = {0x0a, 0x13, 0x25, 0x37, 0x45, 0x55, 0x65, 0x74,
304 0x83, 0x92, 0xa1, 0xb0, 0xbf, 0xce, 0xdf, 0xea, 0xf5};
306 gamma_val[0] = 0x0a;
307 gamma_val[1] = 0x13 + (value * (0xcb - 0x13) / 0xb8);
308 gamma_val[2] = 0x25 + (value * (0xee - 0x25) / 0xb8);
309 gamma_val[3] = 0x37 + (value * (0xfa - 0x37) / 0xb8);
310 gamma_val[4] = 0x45 + (value * (0xfc - 0x45) / 0xb8);
311 gamma_val[5] = 0x55 + (value * (0xfb - 0x55) / 0xb8);
312 gamma_val[6] = 0x65 + (value * (0xfc - 0x65) / 0xb8);
313 gamma_val[7] = 0x74 + (value * (0xfd - 0x74) / 0xb8);
314 gamma_val[8] = 0x83 + (value * (0xfe - 0x83) / 0xb8);
315 gamma_val[9] = 0x92 + (value * (0xfc - 0x92) / 0xb8);
316 gamma_val[10] = 0xa1 + (value * (0xfc - 0xa1) / 0xb8);
317 gamma_val[11] = 0xb0 + (value * (0xfc - 0xb0) / 0xb8);
318 gamma_val[12] = 0xbf + (value * (0xfb - 0xbf) / 0xb8);
319 gamma_val[13] = 0xce + (value * (0xfb - 0xce) / 0xb8);
320 gamma_val[14] = 0xdf + (value * (0xfd - 0xdf) / 0xb8);
321 gamma_val[15] = 0xea + (value * (0xf9 - 0xea) / 0xb8);
322 gamma_val[16] = 0xf5;
324 r = usb_microdia_control_write(dev, 0x1190, gamma_val, 17);
326 return r;
329 int sn9c20x_set_sharpness(struct usb_microdia *dev)
331 __u8 val[1];
332 int ret;
334 ret = usb_microdia_control_read(dev, SN9C20X_SHARPNESS, val, 1);
335 if (ret < 0)
336 return ret;
337 val[0] = (val[0] & 0xc0) | (dev->vsettings.sharpness & 0x3f);
338 ret = usb_microdia_control_write(dev, SN9C20X_SHARPNESS, val, 1);
339 if (ret < 0)
340 return ret;
341 else
342 return 0;
345 int sn9c20x_set_rgb_gain(struct usb_microdia *dev)
347 __u8 val[4];
348 int ret;
350 memcpy(&val, &(dev->vsettings.rgb_gain), 4);
351 ret = usb_microdia_control_write(dev, SN9C20X_RED_GAIN, val, 4);
352 if (ret < 0)
353 return ret;
354 else
355 return 0;