[media] gscpa_m5602: Convert to the control framework
[kernel/kernel-generic.git] / drivers / media / usb / gspca / m5602 / m5602_ov7660.c
1 /*
2  * Driver for the ov7660 sensor
3  *
4  * Copyright (C) 2009 Erik AndrĂ©n
5  * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
6  * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
7  *
8  * Portions of code to USB interface and ALi driver software,
9  * Copyright (c) 2006 Willem Duinker
10  * v4l2 interface modeled after the V4L2 driver
11  * for SN9C10x PC Camera Controllers
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation, version 2.
16  *
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include "m5602_ov7660.h"
22
23 static int ov7660_s_ctrl(struct v4l2_ctrl *ctrl);
24 static void ov7660_dump_registers(struct sd *sd);
25
26 static struct v4l2_pix_format ov7660_modes[] = {
27         {
28                 640,
29                 480,
30                 V4L2_PIX_FMT_SBGGR8,
31                 V4L2_FIELD_NONE,
32                 .sizeimage =
33                         640 * 480,
34                 .bytesperline = 640,
35                 .colorspace = V4L2_COLORSPACE_SRGB,
36                 .priv = 0
37         }
38 };
39
40 static const struct v4l2_ctrl_ops ov7660_ctrl_ops = {
41         .s_ctrl = ov7660_s_ctrl,
42 };
43
44 int ov7660_probe(struct sd *sd)
45 {
46         int err = 0, i;
47         u8 prod_id = 0, ver_id = 0;
48
49         if (force_sensor) {
50                 if (force_sensor == OV7660_SENSOR) {
51                         pr_info("Forcing an %s sensor\n", ov7660.name);
52                         goto sensor_found;
53                 }
54                 /* If we want to force another sensor,
55                 don't try to probe this one */
56                 return -ENODEV;
57         }
58
59         /* Do the preinit */
60         for (i = 0; i < ARRAY_SIZE(preinit_ov7660) && !err; i++) {
61                 u8 data[2];
62
63                 if (preinit_ov7660[i][0] == BRIDGE) {
64                         err = m5602_write_bridge(sd,
65                                 preinit_ov7660[i][1],
66                                 preinit_ov7660[i][2]);
67                 } else {
68                         data[0] = preinit_ov7660[i][2];
69                         err = m5602_write_sensor(sd,
70                                 preinit_ov7660[i][1], data, 1);
71                 }
72         }
73         if (err < 0)
74                 return err;
75
76         if (m5602_read_sensor(sd, OV7660_PID, &prod_id, 1))
77                 return -ENODEV;
78
79         if (m5602_read_sensor(sd, OV7660_VER, &ver_id, 1))
80                 return -ENODEV;
81
82         pr_info("Sensor reported 0x%x%x\n", prod_id, ver_id);
83
84         if ((prod_id == 0x76) && (ver_id == 0x60)) {
85                 pr_info("Detected a ov7660 sensor\n");
86                 goto sensor_found;
87         }
88         return -ENODEV;
89
90 sensor_found:
91         sd->gspca_dev.cam.cam_mode = ov7660_modes;
92         sd->gspca_dev.cam.nmodes = ARRAY_SIZE(ov7660_modes);
93
94         return 0;
95 }
96
97 int ov7660_init(struct sd *sd)
98 {
99         int i, err = 0;
100
101         /* Init the sensor */
102         for (i = 0; i < ARRAY_SIZE(init_ov7660); i++) {
103                 u8 data[2];
104
105                 if (init_ov7660[i][0] == BRIDGE) {
106                         err = m5602_write_bridge(sd,
107                                 init_ov7660[i][1],
108                                 init_ov7660[i][2]);
109                 } else {
110                         data[0] = init_ov7660[i][2];
111                         err = m5602_write_sensor(sd,
112                                 init_ov7660[i][1], data, 1);
113                 }
114         }
115
116         if (dump_sensor)
117                 ov7660_dump_registers(sd);
118
119         return 0;
120 }
121
122 int ov7660_init_controls(struct sd *sd)
123 {
124         struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
125
126         sd->gspca_dev.vdev.ctrl_handler = hdl;
127         v4l2_ctrl_handler_init(hdl, 6);
128
129         v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_AUTO_WHITE_BALANCE,
130                           0, 1, 1, 1);
131         v4l2_ctrl_new_std_menu(hdl, &ov7660_ctrl_ops,
132                           V4L2_CID_EXPOSURE_AUTO, 1, 0, V4L2_EXPOSURE_AUTO);
133
134         sd->autogain = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops,
135                                          V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
136         sd->gain = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_GAIN, 0,
137                                      255, 1, OV7660_DEFAULT_GAIN);
138
139         sd->hflip = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_HFLIP,
140                                       0, 1, 1, 0);
141         sd->vflip = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_VFLIP,
142                                       0, 1, 1, 0);
143
144         if (hdl->error) {
145                 pr_err("Could not initialize controls\n");
146                 return hdl->error;
147         }
148
149         v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, false);
150         v4l2_ctrl_cluster(2, &sd->hflip);
151
152         return 0;
153 }
154
155 int ov7660_start(struct sd *sd)
156 {
157         return 0;
158 }
159
160 int ov7660_stop(struct sd *sd)
161 {
162         return 0;
163 }
164
165 void ov7660_disconnect(struct sd *sd)
166 {
167         ov7660_stop(sd);
168
169         sd->sensor = NULL;
170 }
171
172 static int ov7660_set_gain(struct gspca_dev *gspca_dev, __s32 val)
173 {
174         int err;
175         u8 i2c_data = val;
176         struct sd *sd = (struct sd *) gspca_dev;
177
178         PDEBUG(D_V4L2, "Setting gain to %d", val);
179
180         err = m5602_write_sensor(sd, OV7660_GAIN, &i2c_data, 1);
181         return err;
182 }
183
184 static int ov7660_set_auto_white_balance(struct gspca_dev *gspca_dev,
185                                          __s32 val)
186 {
187         int err;
188         u8 i2c_data;
189         struct sd *sd = (struct sd *) gspca_dev;
190
191         PDEBUG(D_V4L2, "Set auto white balance to %d", val);
192
193         err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
194         if (err < 0)
195                 return err;
196
197         i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
198         err = m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
199
200         return err;
201 }
202
203 static int ov7660_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
204 {
205         int err;
206         u8 i2c_data;
207         struct sd *sd = (struct sd *) gspca_dev;
208
209         PDEBUG(D_V4L2, "Set auto gain control to %d", val);
210
211         err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
212         if (err < 0)
213                 return err;
214
215         i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
216
217         return m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
218 }
219
220 static int ov7660_set_auto_exposure(struct gspca_dev *gspca_dev,
221                                     __s32 val)
222 {
223         int err;
224         u8 i2c_data;
225         struct sd *sd = (struct sd *) gspca_dev;
226
227         PDEBUG(D_V4L2, "Set auto exposure control to %d", val);
228
229         err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
230         if (err < 0)
231                 return err;
232
233         val = (val == V4L2_EXPOSURE_AUTO);
234         i2c_data = ((i2c_data & 0xfe) | ((val & 0x01) << 0));
235
236         return m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
237 }
238
239 static int ov7660_set_hvflip(struct gspca_dev *gspca_dev)
240 {
241         int err;
242         u8 i2c_data;
243         struct sd *sd = (struct sd *) gspca_dev;
244
245         PDEBUG(D_V4L2, "Set hvflip to %d, %d", sd->hflip->val, sd->vflip->val);
246
247         i2c_data = (sd->hflip->val << 5) | (sd->vflip->val << 4);
248
249         err = m5602_write_sensor(sd, OV7660_MVFP, &i2c_data, 1);
250
251         return err;
252 }
253
254 static int ov7660_s_ctrl(struct v4l2_ctrl *ctrl)
255 {
256         struct gspca_dev *gspca_dev =
257                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
258         struct sd *sd = (struct sd *) gspca_dev;
259         int err;
260
261         if (!gspca_dev->streaming)
262                 return 0;
263
264         switch (ctrl->id) {
265         case V4L2_CID_AUTO_WHITE_BALANCE:
266                 err = ov7660_set_auto_white_balance(gspca_dev, ctrl->val);
267                 break;
268         case V4L2_CID_EXPOSURE_AUTO:
269                 err = ov7660_set_auto_exposure(gspca_dev, ctrl->val);
270                 break;
271         case V4L2_CID_AUTOGAIN:
272                 err = ov7660_set_auto_gain(gspca_dev, ctrl->val);
273                 if (err || ctrl->val)
274                         return err;
275                 err = ov7660_set_gain(gspca_dev, sd->gain->val);
276                 break;
277         case V4L2_CID_HFLIP:
278                 err = ov7660_set_hvflip(gspca_dev);
279                 break;
280         default:
281                 return -EINVAL;
282         }
283
284         return err;
285 }
286
287 static void ov7660_dump_registers(struct sd *sd)
288 {
289         int address;
290         pr_info("Dumping the ov7660 register state\n");
291         for (address = 0; address < 0xa9; address++) {
292                 u8 value;
293                 m5602_read_sensor(sd, address, &value, 1);
294                 pr_info("register 0x%x contains 0x%x\n", address, value);
295         }
296
297         pr_info("ov7660 register state dump complete\n");
298
299         pr_info("Probing for which registers that are read/write\n");
300         for (address = 0; address < 0xff; address++) {
301                 u8 old_value, ctrl_value;
302                 u8 test_value[2] = {0xff, 0xff};
303
304                 m5602_read_sensor(sd, address, &old_value, 1);
305                 m5602_write_sensor(sd, address, test_value, 1);
306                 m5602_read_sensor(sd, address, &ctrl_value, 1);
307
308                 if (ctrl_value == test_value[0])
309                         pr_info("register 0x%x is writeable\n", address);
310                 else
311                         pr_info("register 0x%x is read only\n", address);
312
313                 /* Restore original value */
314                 m5602_write_sensor(sd, address, &old_value, 1);
315         }
316 }