blob: e274cf19a3ea22a1448f340f7a779c5a1274d528 [file] [log] [blame]
Jean-François Moine618a8642010-06-05 07:45:04 -03001/*
2 * SQ930x subdriver
3 *
4 * Copyright (C) 2010 Jean-François Moine <http://moinejf.free.fr>
5 * Copyright (C) 2006 -2008 Gerard Klaver <gerard at gkall dot hobby dot nl>
6 * Copyright (C) 2007 Sam Revitch <samr7@cs.washington.edu>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Joe Perches133a9fe2011-08-21 19:56:57 -030023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Jean-François Moine618a8642010-06-05 07:45:04 -030025#define MODULE_NAME "sq930x"
26
27#include "gspca.h"
Jean-François Moine618a8642010-06-05 07:45:04 -030028
29MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>\n"
30 "Gerard Klaver <gerard at gkall dot hobby dot nl\n"
31 "Sam Revitch <samr7@cs.washington.edu>");
32MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver");
33MODULE_LICENSE("GPL");
34
Jean-François Moine618a8642010-06-05 07:45:04 -030035/* Structure to hold all of our device specific stuff */
36struct sd {
37 struct gspca_dev gspca_dev; /* !! must be the first item */
38
Hans Verkuildf0df1a2012-05-16 08:29:16 -030039 struct { /* exposure/gain control cluster */
40 struct v4l2_ctrl *exposure;
41 struct v4l2_ctrl *gain;
42 };
Jean-François Moine618a8642010-06-05 07:45:04 -030043
Jean-François Moine618a8642010-06-05 07:45:04 -030044 u8 do_ctrl;
Jean-François Moine82d2c7a2010-07-26 07:23:00 -030045 u8 gpio[2];
Jean-François Moine618a8642010-06-05 07:45:04 -030046 u8 sensor;
Jean-François Moine11ce8842010-07-26 06:39:40 -030047 u8 type;
48#define Generic 0
49#define Creative_live_motion 1
50};
51enum sensors {
Jean-François Moine618a8642010-06-05 07:45:04 -030052 SENSOR_ICX098BQ,
Jean-François Moine618a8642010-06-05 07:45:04 -030053 SENSOR_LZ24BP,
Jean-François Moinea68f7232010-06-24 05:02:57 -030054 SENSOR_MI0360,
Jean-François Moine6e80cc52010-07-26 07:27:13 -030055 SENSOR_MT9V111, /* = MI360SOC */
Jean-François Moinea68f7232010-06-24 05:02:57 -030056 SENSOR_OV7660,
57 SENSOR_OV9630,
Jean-François Moine618a8642010-06-05 07:45:04 -030058};
59
Jean-François Moine618a8642010-06-05 07:45:04 -030060static struct v4l2_pix_format vga_mode[] = {
Jean-François Moine82d2c7a2010-07-26 07:23:00 -030061 {320, 240, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
Jean-François Moine618a8642010-06-05 07:45:04 -030062 .bytesperline = 320,
Jean-François Moine82d2c7a2010-07-26 07:23:00 -030063 .sizeimage = 320 * 240,
64 .colorspace = V4L2_COLORSPACE_SRGB,
65 .priv = 0},
66 {640, 480, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
Jean-François Moine618a8642010-06-05 07:45:04 -030067 .bytesperline = 640,
Jean-François Moine82d2c7a2010-07-26 07:23:00 -030068 .sizeimage = 640 * 480,
69 .colorspace = V4L2_COLORSPACE_SRGB,
70 .priv = 1},
Jean-François Moine618a8642010-06-05 07:45:04 -030071};
72
Jean-François Moine618a8642010-06-05 07:45:04 -030073/* sq930x registers */
74#define SQ930_CTRL_UCBUS_IO 0x0001
75#define SQ930_CTRL_I2C_IO 0x0002
76#define SQ930_CTRL_GPIO 0x0005
77#define SQ930_CTRL_CAP_START 0x0010
78#define SQ930_CTRL_CAP_STOP 0x0011
79#define SQ930_CTRL_SET_EXPOSURE 0x001d
80#define SQ930_CTRL_RESET 0x001e
81#define SQ930_CTRL_GET_DEV_INFO 0x001f
82
83/* gpio 1 (8..15) */
84#define SQ930_GPIO_DFL_I2C_SDA 0x0001
85#define SQ930_GPIO_DFL_I2C_SCL 0x0002
86#define SQ930_GPIO_RSTBAR 0x0004
87#define SQ930_GPIO_EXTRA1 0x0040
88#define SQ930_GPIO_EXTRA2 0x0080
89/* gpio 3 (24..31) */
90#define SQ930_GPIO_POWER 0x0200
91#define SQ930_GPIO_DFL_LED 0x1000
92
93struct ucbus_write_cmd {
94 u16 bw_addr;
95 u8 bw_data;
96};
97struct i2c_write_cmd {
98 u8 reg;
99 u16 val;
100};
101
102static const struct ucbus_write_cmd icx098bq_start_0[] = {
103 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xce},
104 {0xf802, 0xc1}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x0e},
105 {0xf80a, 0x01}, {0xf80b, 0xee}, {0xf807, 0x60}, {0xf80c, 0x02},
106 {0xf80d, 0xf0}, {0xf80e, 0x03}, {0xf80f, 0x0a}, {0xf81c, 0x02},
107 {0xf81d, 0xf0}, {0xf81e, 0x03}, {0xf81f, 0x0a}, {0xf83a, 0x00},
108 {0xf83b, 0x10}, {0xf83c, 0x00}, {0xf83d, 0x4e}, {0xf810, 0x04},
109 {0xf811, 0x00}, {0xf812, 0x02}, {0xf813, 0x10}, {0xf803, 0x00},
110 {0xf814, 0x01}, {0xf815, 0x18}, {0xf816, 0x00}, {0xf817, 0x48},
111 {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
112 {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
113 {0xf823, 0x07}, {0xf824, 0xff}, {0xf825, 0x03}, {0xf826, 0xff},
114 {0xf827, 0x06}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
115 {0xf82b, 0x0c}, {0xf82c, 0xfd}, {0xf82d, 0x01}, {0xf82e, 0x00},
116 {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
117 {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
118 {0xf854, 0x00}, {0xf855, 0x18}, {0xf856, 0x00}, {0xf857, 0x3c},
119 {0xf858, 0x00}, {0xf859, 0x0c}, {0xf85a, 0x00}, {0xf85b, 0x30},
120 {0xf85c, 0x00}, {0xf85d, 0x0c}, {0xf85e, 0x00}, {0xf85f, 0x30},
121 {0xf860, 0x00}, {0xf861, 0x48}, {0xf862, 0x01}, {0xf863, 0xdc},
122 {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
123 {0xf868, 0xff}, {0xf869, 0x70}, {0xf86c, 0xff}, {0xf86d, 0x00},
124 {0xf86a, 0xff}, {0xf86b, 0x48}, {0xf86e, 0xff}, {0xf86f, 0x00},
125 {0xf870, 0x01}, {0xf871, 0xdb}, {0xf872, 0x01}, {0xf873, 0xfa},
126 {0xf874, 0x01}, {0xf875, 0xdb}, {0xf876, 0x01}, {0xf877, 0xfa},
127 {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
128 {0xf800, 0x03}
129};
130static const struct ucbus_write_cmd icx098bq_start_1[] = {
131 {0xf5f0, 0x00}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
132 {0xf5f4, 0xc0},
133 {0xf5f0, 0x49}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
134 {0xf5f4, 0xc0},
135 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
136 {0xf5f9, 0x00}
137};
138
139static const struct ucbus_write_cmd icx098bq_start_2[] = {
140 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x82}, {0xf806, 0x00},
141 {0xf807, 0x7f}, {0xf800, 0x03},
142 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x40}, {0xf806, 0x00},
143 {0xf807, 0x7f}, {0xf800, 0x03},
144 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xcf}, {0xf806, 0xd0},
145 {0xf807, 0x7f}, {0xf800, 0x03},
146 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
147 {0xf807, 0x7f}, {0xf800, 0x03}
148};
149
150static const struct ucbus_write_cmd lz24bp_start_0[] = {
151 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xbe},
152 {0xf802, 0xc6}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x06},
153 {0xf80a, 0x01}, {0xf80b, 0xfe}, {0xf807, 0x84}, {0xf80c, 0x02},
154 {0xf80d, 0xf7}, {0xf80e, 0x03}, {0xf80f, 0x0b}, {0xf81c, 0x00},
155 {0xf81d, 0x49}, {0xf81e, 0x03}, {0xf81f, 0x0b}, {0xf83a, 0x00},
156 {0xf83b, 0x01}, {0xf83c, 0x00}, {0xf83d, 0x6b}, {0xf810, 0x03},
157 {0xf811, 0x10}, {0xf812, 0x02}, {0xf813, 0x6f}, {0xf803, 0x00},
158 {0xf814, 0x00}, {0xf815, 0x44}, {0xf816, 0x00}, {0xf817, 0x48},
159 {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
160 {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
161 {0xf823, 0x07}, {0xf824, 0xfd}, {0xf825, 0x07}, {0xf826, 0xf0},
162 {0xf827, 0x0c}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
163 {0xf82b, 0x0c}, {0xf82c, 0xfc}, {0xf82d, 0x01}, {0xf82e, 0x00},
164 {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
165 {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
166 {0xf854, 0x00}, {0xf855, 0x0c}, {0xf856, 0x00}, {0xf857, 0x30},
167 {0xf858, 0x00}, {0xf859, 0x18}, {0xf85a, 0x00}, {0xf85b, 0x3c},
168 {0xf85c, 0x00}, {0xf85d, 0x18}, {0xf85e, 0x00}, {0xf85f, 0x3c},
169 {0xf860, 0xff}, {0xf861, 0x37}, {0xf862, 0xff}, {0xf863, 0x1d},
170 {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
171 {0xf868, 0x00}, {0xf869, 0x37}, {0xf86c, 0x02}, {0xf86d, 0x1d},
172 {0xf86a, 0x00}, {0xf86b, 0x37}, {0xf86e, 0x02}, {0xf86f, 0x1d},
173 {0xf870, 0x01}, {0xf871, 0xc6}, {0xf872, 0x02}, {0xf873, 0x04},
174 {0xf874, 0x01}, {0xf875, 0xc6}, {0xf876, 0x02}, {0xf877, 0x04},
175 {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
176 {0xf800, 0x03}
177};
178static const struct ucbus_write_cmd lz24bp_start_1_gen[] = {
179 {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
180 {0xf5f4, 0xb3},
181 {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
182 {0xf5f4, 0xb3},
183 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
184 {0xf5f9, 0x00}
185};
186
187static const struct ucbus_write_cmd lz24bp_start_1_clm[] = {
188 {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
189 {0xf5f4, 0xc0},
190 {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
191 {0xf5f4, 0xc0},
192 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
193 {0xf5f9, 0x00}
194};
195
196static const struct ucbus_write_cmd lz24bp_start_2[] = {
197 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x80}, {0xf806, 0x00},
198 {0xf807, 0x7f}, {0xf800, 0x03},
199 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x4e}, {0xf806, 0x00},
200 {0xf807, 0x7f}, {0xf800, 0x03},
201 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xc0}, {0xf806, 0x48},
202 {0xf807, 0x7f}, {0xf800, 0x03},
203 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
204 {0xf807, 0x7f}, {0xf800, 0x03}
205};
206
207static const struct ucbus_write_cmd mi0360_start_0[] = {
208 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0xcc}, {0xf333, 0xcc},
209 {0xf334, 0xcc}, {0xf335, 0xcc}, {0xf33f, 0x00}
210};
211static const struct i2c_write_cmd mi0360_init_23[] = {
212 {0x30, 0x0040}, /* reserved - def 0x0005 */
213 {0x31, 0x0000}, /* reserved - def 0x002a */
214 {0x34, 0x0100}, /* reserved - def 0x0100 */
215 {0x3d, 0x068f}, /* reserved - def 0x068f */
216};
217static const struct i2c_write_cmd mi0360_init_24[] = {
218 {0x03, 0x01e5}, /* window height */
219 {0x04, 0x0285}, /* window width */
220};
221static const struct i2c_write_cmd mi0360_init_25[] = {
222 {0x35, 0x0020}, /* global gain */
223 {0x2b, 0x0020}, /* green1 gain */
224 {0x2c, 0x002a}, /* blue gain */
225 {0x2d, 0x0028}, /* red gain */
226 {0x2e, 0x0020}, /* green2 gain */
227};
228static const struct ucbus_write_cmd mi0360_start_1[] = {
229 {0xf5f0, 0x11}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
230 {0xf5f4, 0xa6},
231 {0xf5f0, 0x51}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
232 {0xf5f4, 0xa6},
233 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
234 {0xf5f9, 0x00}
235};
236static const struct i2c_write_cmd mi0360_start_2[] = {
237 {0x62, 0x041d}, /* reserved - def 0x0418 */
238};
239static const struct i2c_write_cmd mi0360_start_3[] = {
240 {0x05, 0x007b}, /* horiz blanking */
241};
242static const struct i2c_write_cmd mi0360_start_4[] = {
243 {0x05, 0x03f5}, /* horiz blanking */
244};
245
Jean-François Moinea68f7232010-06-24 05:02:57 -0300246static const struct i2c_write_cmd mt9v111_init_0[] = {
Jean-François Moine1676e4a2010-07-06 05:14:57 -0300247 {0x01, 0x0001}, /* select IFP/SOC registers */
248 {0x06, 0x300c}, /* operating mode control */
249 {0x08, 0xcc00}, /* output format control (RGB) */
Jean-François Moine6e80cc52010-07-26 07:27:13 -0300250 {0x01, 0x0004}, /* select sensor core registers */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300251};
252static const struct i2c_write_cmd mt9v111_init_1[] = {
Jean-François Moine1676e4a2010-07-06 05:14:57 -0300253 {0x03, 0x01e5}, /* window height */
254 {0x04, 0x0285}, /* window width */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300255};
256static const struct i2c_write_cmd mt9v111_init_2[] = {
257 {0x30, 0x7800},
258 {0x31, 0x0000},
Jean-François Moine1676e4a2010-07-06 05:14:57 -0300259 {0x07, 0x3002}, /* output control */
260 {0x35, 0x0020}, /* global gain */
261 {0x2b, 0x0020}, /* green1 gain */
262 {0x2c, 0x0020}, /* blue gain */
263 {0x2d, 0x0020}, /* red gain */
264 {0x2e, 0x0020}, /* green2 gain */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300265};
266static const struct ucbus_write_cmd mt9v111_start_1[] = {
267 {0xf5f0, 0x11}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
268 {0xf5f4, 0xaa},
269 {0xf5f0, 0x51}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
270 {0xf5f4, 0xaa},
271 {0xf5fa, 0x00}, {0xf5f6, 0x0a}, {0xf5f7, 0x0a}, {0xf5f8, 0x0a},
272 {0xf5f9, 0x0a}
273};
274static const struct i2c_write_cmd mt9v111_init_3[] = {
275 {0x62, 0x0405},
276};
277static const struct i2c_write_cmd mt9v111_init_4[] = {
Jean-François Moine585d4882010-07-26 07:25:44 -0300278/* {0x05, 0x00ce}, */
279 {0x05, 0x005d}, /* horizontal blanking */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300280};
281
282static const struct ucbus_write_cmd ov7660_start_0[] = {
283 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0xc0},
284 {0xf334, 0x39}, {0xf335, 0xe7}, {0xf33f, 0x03}
285};
286
287static const struct ucbus_write_cmd ov9630_start_0[] = {
288 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0x00},
289 {0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03}
290};
291
Jean-François Moine6e80cc52010-07-26 07:27:13 -0300292/* start parameters indexed by [sensor][mode] */
Jean-François Moine618a8642010-06-05 07:45:04 -0300293static const struct cap_s {
294 u8 cc_sizeid;
295 u8 cc_bytes[32];
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300296} capconfig[4][2] = {
Jean-François Moine618a8642010-06-05 07:45:04 -0300297 [SENSOR_ICX098BQ] = {
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300298 {2, /* Bayer 320x240 */
299 {0x05, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
300 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0,
302 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
303 {4, /* Bayer 640x480 */
Jean-François Moine618a8642010-06-05 07:45:04 -0300304 {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
305 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
Jean-François Moine618a8642010-06-05 07:45:04 -0300308 },
309 [SENSOR_LZ24BP] = {
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300310 {2, /* Bayer 320x240 */
311 {0x05, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
312 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
313 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
314 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
315 {4, /* Bayer 640x480 */
Jean-François Moine618a8642010-06-05 07:45:04 -0300316 {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
317 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300318 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
319 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
Jean-François Moine618a8642010-06-05 07:45:04 -0300320 },
321 [SENSOR_MI0360] = {
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300322 {2, /* Bayer 320x240 */
323 {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
324 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
325 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
327 {4, /* Bayer 640x480 */
Jean-François Moine618a8642010-06-05 07:45:04 -0300328 {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
Jean-François Moine618a8642010-06-05 07:45:04 -0300329 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300330 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
331 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
Jean-François Moine618a8642010-06-05 07:45:04 -0300332 },
Jean-François Moinea68f7232010-06-24 05:02:57 -0300333 [SENSOR_MT9V111] = {
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300334 {2, /* Bayer 320x240 */
335 {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
336 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
337 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
338 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
339 {4, /* Bayer 640x480 */
340 {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
Jean-François Moinea68f7232010-06-24 05:02:57 -0300341 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300342 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
Jean-François Moinea68f7232010-06-24 05:02:57 -0300344 },
345};
346
347struct sensor_s {
348 const char *name;
349 u8 i2c_addr;
350 u8 i2c_dum;
351 u8 gpio[5];
352 u8 cmd_len;
353 const struct ucbus_write_cmd *cmd;
354};
355
356static const struct sensor_s sensor_tb[] = {
357 [SENSOR_ICX098BQ] = {
358 "icx098bp",
359 0x00, 0x00,
360 {0,
361 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
362 SQ930_GPIO_DFL_I2C_SDA,
363 0,
364 SQ930_GPIO_RSTBAR
365 },
366 8, icx098bq_start_0
367 },
368 [SENSOR_LZ24BP] = {
369 "lz24bp",
370 0x00, 0x00,
371 {0,
372 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
373 SQ930_GPIO_DFL_I2C_SDA,
374 0,
375 SQ930_GPIO_RSTBAR
376 },
377 8, lz24bp_start_0
378 },
379 [SENSOR_MI0360] = {
380 "mi0360",
381 0x5d, 0x80,
382 {SQ930_GPIO_RSTBAR,
383 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
384 SQ930_GPIO_DFL_I2C_SDA,
385 0,
386 0
387 },
388 7, mi0360_start_0
389 },
390 [SENSOR_MT9V111] = {
391 "mt9v111",
392 0x5c, 0x7f,
393 {SQ930_GPIO_RSTBAR,
394 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
395 SQ930_GPIO_DFL_I2C_SDA,
396 0,
397 0
398 },
399 7, mi0360_start_0
400 },
401 [SENSOR_OV7660] = {
402 "ov7660",
403 0x21, 0x00,
404 {0,
405 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
406 SQ930_GPIO_DFL_I2C_SDA,
407 0,
408 SQ930_GPIO_RSTBAR
409 },
410 7, ov7660_start_0
411 },
412 [SENSOR_OV9630] = {
413 "ov9630",
414 0x30, 0x00,
415 {0,
416 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
417 SQ930_GPIO_DFL_I2C_SDA,
418 0,
419 SQ930_GPIO_RSTBAR
420 },
421 7, ov9630_start_0
422 },
Jean-François Moine618a8642010-06-05 07:45:04 -0300423};
424
425static void reg_r(struct gspca_dev *gspca_dev,
426 u16 value, int len)
427{
Jean-François Moine7d716a32010-06-24 04:57:12 -0300428 int ret;
429
430 if (gspca_dev->usb_err < 0)
431 return;
432 ret = usb_control_msg(gspca_dev->dev,
Jean-François Moine618a8642010-06-05 07:45:04 -0300433 usb_rcvctrlpipe(gspca_dev->dev, 0),
434 0x0c,
435 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
436 value, 0, gspca_dev->usb_buf, len,
437 500);
Jean-François Moine7d716a32010-06-24 04:57:12 -0300438 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300439 pr_err("reg_r %04x failed %d\n", value, ret);
Jean-François Moine7d716a32010-06-24 04:57:12 -0300440 gspca_dev->usb_err = ret;
441 }
Jean-François Moine618a8642010-06-05 07:45:04 -0300442}
443
444static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
445{
446 int ret;
447
448 if (gspca_dev->usb_err < 0)
449 return;
450 PDEBUG(D_USBO, "reg_w v: %04x i: %04x", value, index);
451 ret = usb_control_msg(gspca_dev->dev,
452 usb_sndctrlpipe(gspca_dev->dev, 0),
453 0x0c, /* request */
454 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
455 value, index, NULL, 0,
456 500);
457 msleep(30);
458 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300459 pr_err("reg_w %04x %04x failed %d\n", value, index, ret);
Jean-François Moine618a8642010-06-05 07:45:04 -0300460 gspca_dev->usb_err = ret;
461 }
462}
463
464static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
465 const u8 *data, int len)
466{
467 int ret;
468
469 if (gspca_dev->usb_err < 0)
470 return;
471 PDEBUG(D_USBO, "reg_wb v: %04x i: %04x %02x...%02x",
472 value, index, *data, data[len - 1]);
473 memcpy(gspca_dev->usb_buf, data, len);
474 ret = usb_control_msg(gspca_dev->dev,
475 usb_sndctrlpipe(gspca_dev->dev, 0),
476 0x0c, /* request */
477 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
478 value, index, gspca_dev->usb_buf, len,
479 1000);
480 msleep(30);
481 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300482 pr_err("reg_wb %04x %04x failed %d\n", value, index, ret);
Jean-François Moine618a8642010-06-05 07:45:04 -0300483 gspca_dev->usb_err = ret;
484 }
485}
486
Jean-François Moinea68f7232010-06-24 05:02:57 -0300487static void i2c_write(struct sd *sd,
Jean-François Moine618a8642010-06-05 07:45:04 -0300488 const struct i2c_write_cmd *cmd,
489 int ncmds)
490{
Jean-François Moinea68f7232010-06-24 05:02:57 -0300491 struct gspca_dev *gspca_dev = &sd->gspca_dev;
492 const struct sensor_s *sensor;
Jean-François Moine618a8642010-06-05 07:45:04 -0300493 u16 val, idx;
494 u8 *buf;
495 int ret;
496
497 if (gspca_dev->usb_err < 0)
498 return;
499
Jean-François Moinea68f7232010-06-24 05:02:57 -0300500 sensor = &sensor_tb[sd->sensor];
501
502 val = (sensor->i2c_addr << 8) | SQ930_CTRL_I2C_IO;
Jean-François Moine618a8642010-06-05 07:45:04 -0300503 idx = (cmd->val & 0xff00) | cmd->reg;
504
505 buf = gspca_dev->usb_buf;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300506 *buf++ = sensor->i2c_dum;
Jean-François Moine618a8642010-06-05 07:45:04 -0300507 *buf++ = cmd->val;
508
509 while (--ncmds > 0) {
510 cmd++;
511 *buf++ = cmd->reg;
512 *buf++ = cmd->val >> 8;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300513 *buf++ = sensor->i2c_dum;
Jean-François Moine618a8642010-06-05 07:45:04 -0300514 *buf++ = cmd->val;
515 }
516
517 PDEBUG(D_USBO, "i2c_w v: %04x i: %04x %02x...%02x",
518 val, idx, gspca_dev->usb_buf[0], buf[-1]);
519 ret = usb_control_msg(gspca_dev->dev,
520 usb_sndctrlpipe(gspca_dev->dev, 0),
521 0x0c, /* request */
522 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
523 val, idx,
524 gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
525 500);
526 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300527 pr_err("i2c_write failed %d\n", ret);
Jean-François Moine618a8642010-06-05 07:45:04 -0300528 gspca_dev->usb_err = ret;
529 }
530}
531
532static void ucbus_write(struct gspca_dev *gspca_dev,
533 const struct ucbus_write_cmd *cmd,
534 int ncmds,
535 int batchsize)
536{
537 u8 *buf;
538 u16 val, idx;
539 int len, ret;
540
541 if (gspca_dev->usb_err < 0)
542 return;
543
Jean-François Moine618a8642010-06-05 07:45:04 -0300544 if ((batchsize - 1) * 3 > USB_BUF_SZ) {
Theodore Kilgorec93396e2013-02-04 13:17:55 -0300545 PERR("Bug: usb_buf overflow\n");
Jean-François Moine618a8642010-06-05 07:45:04 -0300546 gspca_dev->usb_err = -ENOMEM;
547 return;
548 }
Jean-François Moine618a8642010-06-05 07:45:04 -0300549
550 for (;;) {
551 len = ncmds;
552 if (len > batchsize)
553 len = batchsize;
554 ncmds -= len;
555
556 val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
557 idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
558
559 buf = gspca_dev->usb_buf;
560 while (--len > 0) {
561 cmd++;
562 *buf++ = cmd->bw_addr;
563 *buf++ = cmd->bw_addr >> 8;
564 *buf++ = cmd->bw_data;
565 }
566 if (buf != gspca_dev->usb_buf)
567 PDEBUG(D_USBO, "ucbus v: %04x i: %04x %02x...%02x",
568 val, idx,
569 gspca_dev->usb_buf[0], buf[-1]);
570 else
571 PDEBUG(D_USBO, "ucbus v: %04x i: %04x",
572 val, idx);
573 ret = usb_control_msg(gspca_dev->dev,
574 usb_sndctrlpipe(gspca_dev->dev, 0),
575 0x0c, /* request */
576 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
577 val, idx,
578 gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
579 500);
580 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300581 pr_err("ucbus_write failed %d\n", ret);
Jean-François Moine618a8642010-06-05 07:45:04 -0300582 gspca_dev->usb_err = ret;
583 return;
584 }
585 msleep(30);
586 if (ncmds <= 0)
587 break;
588 cmd++;
589 }
590}
591
592static void gpio_set(struct sd *sd, u16 val, u16 mask)
593{
594 struct gspca_dev *gspca_dev = &sd->gspca_dev;
595
596 if (mask & 0x00ff) {
597 sd->gpio[0] &= ~mask;
598 sd->gpio[0] |= val;
599 reg_w(gspca_dev, 0x0100 | SQ930_CTRL_GPIO,
600 ~sd->gpio[0] << 8);
601 }
602 mask >>= 8;
603 val >>= 8;
604 if (mask) {
605 sd->gpio[1] &= ~mask;
606 sd->gpio[1] |= val;
607 reg_w(gspca_dev, 0x0300 | SQ930_CTRL_GPIO,
608 ~sd->gpio[1] << 8);
609 }
610}
611
Jean-François Moinea68f7232010-06-24 05:02:57 -0300612static void gpio_init(struct sd *sd,
613 const u8 *gpio)
614{
615 gpio_set(sd, *gpio++, 0x000f);
616 gpio_set(sd, *gpio++, 0x000f);
617 gpio_set(sd, *gpio++, 0x000f);
618 gpio_set(sd, *gpio++, 0x000f);
619 gpio_set(sd, *gpio, 0x000f);
620}
621
622static void bridge_init(struct sd *sd)
Jean-François Moine618a8642010-06-05 07:45:04 -0300623{
624 static const struct ucbus_write_cmd clkfreq_cmd = {
625 0xf031, 0 /* SQ930_CLKFREQ_60MHZ */
626 };
627
628 ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
629
630 gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300631}
632
633static void cmos_probe(struct gspca_dev *gspca_dev)
634{
635 struct sd *sd = (struct sd *) gspca_dev;
636 int i;
637 const struct sensor_s *sensor;
638 static const u8 probe_order[] = {
639/* SENSOR_LZ24BP, (tested as ccd) */
640 SENSOR_OV9630,
641 SENSOR_MI0360,
642 SENSOR_OV7660,
643 SENSOR_MT9V111,
644 };
645
646 for (i = 0; i < ARRAY_SIZE(probe_order); i++) {
647 sensor = &sensor_tb[probe_order[i]];
648 ucbus_write(&sd->gspca_dev, sensor->cmd, sensor->cmd_len, 8);
649 gpio_init(sd, sensor->gpio);
650 msleep(100);
651 reg_r(gspca_dev, (sensor->i2c_addr << 8) | 0x001c, 1);
652 msleep(100);
653 if (gspca_dev->usb_buf[0] != 0)
654 break;
655 }
Jean-François Moine3ebd1792010-12-25 13:07:57 -0300656 if (i >= ARRAY_SIZE(probe_order)) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300657 pr_err("Unknown sensor\n");
Jean-François Moine3ebd1792010-12-25 13:07:57 -0300658 gspca_dev->usb_err = -EINVAL;
659 return;
660 }
661 sd->sensor = probe_order[i];
Jean-François Moineefd37692010-12-25 13:11:54 -0300662 switch (sd->sensor) {
663 case SENSOR_OV7660:
664 case SENSOR_OV9630:
Joe Perches133a9fe2011-08-21 19:56:57 -0300665 pr_err("Sensor %s not yet treated\n",
666 sensor_tb[sd->sensor].name);
Jean-François Moineefd37692010-12-25 13:11:54 -0300667 gspca_dev->usb_err = -EINVAL;
668 break;
669 }
Jean-François Moinea68f7232010-06-24 05:02:57 -0300670}
671
672static void mt9v111_init(struct gspca_dev *gspca_dev)
673{
674 int i, nwait;
675 static const u8 cmd_001b[] = {
676 0x00, 0x3b, 0xf6, 0x01, 0x03, 0x02, 0x00, 0x00,
677 0x00, 0x00, 0x00
678 };
679 static const u8 cmd_011b[][7] = {
680 {0x10, 0x01, 0x66, 0x08, 0x00, 0x00, 0x00},
681 {0x01, 0x00, 0x1a, 0x04, 0x00, 0x00, 0x00},
682 {0x20, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00},
683 {0x02, 0x01, 0xae, 0x01, 0x00, 0x00, 0x00},
684 };
685
686 reg_wb(gspca_dev, 0x001b, 0x0000, cmd_001b, sizeof cmd_001b);
687 for (i = 0; i < ARRAY_SIZE(cmd_011b); i++) {
688 reg_wb(gspca_dev, 0x001b, 0x0000, cmd_011b[i],
689 ARRAY_SIZE(cmd_011b[0]));
690 msleep(400);
691 nwait = 20;
692 for (;;) {
693 reg_r(gspca_dev, 0x031b, 1);
694 if (gspca_dev->usb_buf[0] == 0
695 || gspca_dev->usb_err != 0)
696 break;
697 if (--nwait < 0) {
698 PDEBUG(D_PROBE, "mt9v111_init timeout");
699 gspca_dev->usb_err = -ETIME;
700 return;
701 }
702 msleep(50);
703 }
704 }
705}
706
707static void global_init(struct sd *sd, int first_time)
708{
Jean-François Moine618a8642010-06-05 07:45:04 -0300709 switch (sd->sensor) {
710 case SENSOR_ICX098BQ:
711 if (first_time)
712 ucbus_write(&sd->gspca_dev,
713 icx098bq_start_0,
714 8, 8);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300715 gpio_init(sd, sensor_tb[sd->sensor].gpio);
Jean-François Moine618a8642010-06-05 07:45:04 -0300716 break;
717 case SENSOR_LZ24BP:
718 if (sd->type != Creative_live_motion)
719 gpio_set(sd, SQ930_GPIO_EXTRA1, 0x00ff);
720 else
721 gpio_set(sd, 0, 0x00ff);
722 msleep(50);
723 if (first_time)
724 ucbus_write(&sd->gspca_dev,
725 lz24bp_start_0,
726 8, 8);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300727 gpio_init(sd, sensor_tb[sd->sensor].gpio);
Jean-François Moine618a8642010-06-05 07:45:04 -0300728 break;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300729 case SENSOR_MI0360:
730 if (first_time)
Jean-François Moine618a8642010-06-05 07:45:04 -0300731 ucbus_write(&sd->gspca_dev,
732 mi0360_start_0,
733 ARRAY_SIZE(mi0360_start_0),
734 8);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300735 gpio_init(sd, sensor_tb[sd->sensor].gpio);
Jean-François Moine618a8642010-06-05 07:45:04 -0300736 gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
737 break;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300738 default:
739/* case SENSOR_MT9V111: */
740 if (first_time)
741 mt9v111_init(&sd->gspca_dev);
742 else
743 gpio_init(sd, sensor_tb[sd->sensor].gpio);
744 break;
Jean-François Moine618a8642010-06-05 07:45:04 -0300745 }
746}
747
748static void lz24bp_ppl(struct sd *sd, u16 ppl)
749{
750 struct ucbus_write_cmd cmds[2] = {
751 {0xf810, ppl >> 8},
752 {0xf811, ppl}
753 };
754
755 ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
756}
757
Hans Verkuildf0df1a2012-05-16 08:29:16 -0300758static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 gain)
Jean-François Moine618a8642010-06-05 07:45:04 -0300759{
760 struct sd *sd = (struct sd *) gspca_dev;
761 int i, integclks, intstartclk, frameclks, min_frclk;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300762 const struct sensor_s *sensor;
Jean-François Moine618a8642010-06-05 07:45:04 -0300763 u16 cmd;
764 u8 buf[15];
765
Hans Verkuildf0df1a2012-05-16 08:29:16 -0300766 integclks = expo;
Jean-François Moine618a8642010-06-05 07:45:04 -0300767 i = 0;
768 cmd = SQ930_CTRL_SET_EXPOSURE;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300769
770 switch (sd->sensor) {
771 case SENSOR_ICX098BQ: /* ccd */
772 case SENSOR_LZ24BP:
Jean-François Moine618a8642010-06-05 07:45:04 -0300773 min_frclk = sd->sensor == SENSOR_ICX098BQ ? 0x210 : 0x26f;
774 if (integclks >= min_frclk) {
775 intstartclk = 0;
776 frameclks = integclks;
777 } else {
778 intstartclk = min_frclk - integclks;
779 frameclks = min_frclk;
780 }
781 buf[i++] = intstartclk >> 8;
782 buf[i++] = intstartclk;
783 buf[i++] = frameclks >> 8;
784 buf[i++] = frameclks;
Hans Verkuildf0df1a2012-05-16 08:29:16 -0300785 buf[i++] = gain;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300786 break;
787 default: /* cmos */
788/* case SENSOR_MI0360: */
789/* case SENSOR_MT9V111: */
790 cmd |= 0x0100;
791 sensor = &sensor_tb[sd->sensor];
792 buf[i++] = sensor->i2c_addr; /* i2c_slave_addr */
793 buf[i++] = 0x08; /* 2 * ni2c */
794 buf[i++] = 0x09; /* reg = shutter width */
795 buf[i++] = integclks >> 8; /* val H */
796 buf[i++] = sensor->i2c_dum;
797 buf[i++] = integclks; /* val L */
798 buf[i++] = 0x35; /* reg = global gain */
799 buf[i++] = 0x00; /* val H */
800 buf[i++] = sensor->i2c_dum;
Hans Verkuildf0df1a2012-05-16 08:29:16 -0300801 buf[i++] = 0x80 + gain / 2; /* val L */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300802 buf[i++] = 0x00;
803 buf[i++] = 0x00;
804 buf[i++] = 0x00;
805 buf[i++] = 0x00;
806 buf[i++] = 0x83;
807 break;
Jean-François Moine618a8642010-06-05 07:45:04 -0300808 }
809 reg_wb(gspca_dev, cmd, 0, buf, i);
810}
811
812/* This function is called at probe time just before sd_init */
813static int sd_config(struct gspca_dev *gspca_dev,
814 const struct usb_device_id *id)
815{
816 struct sd *sd = (struct sd *) gspca_dev;
817 struct cam *cam = &gspca_dev->cam;
818
819 sd->sensor = id->driver_info >> 8;
820 sd->type = id->driver_info;
821
822 cam->cam_mode = vga_mode;
823 cam->nmodes = ARRAY_SIZE(vga_mode);
824
825 cam->bulk = 1;
Jean-François Moine618a8642010-06-05 07:45:04 -0300826
Jean-François Moine618a8642010-06-05 07:45:04 -0300827 return 0;
828}
829
830/* this function is called at probe and resume time */
831static int sd_init(struct gspca_dev *gspca_dev)
832{
833 struct sd *sd = (struct sd *) gspca_dev;
834
835 sd->gpio[0] = sd->gpio[1] = 0xff; /* force gpio rewrite */
836
Jean-François Moinea68f7232010-06-24 05:02:57 -0300837/*fixme: is this needed for icx098bp and mi0360?
Jean-François Moine618a8642010-06-05 07:45:04 -0300838 if (sd->sensor != SENSOR_LZ24BP)
839 reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300840 */
Jean-François Moine618a8642010-06-05 07:45:04 -0300841
842 reg_r(gspca_dev, SQ930_CTRL_GET_DEV_INFO, 8);
Jean-François Moine3ebd1792010-12-25 13:07:57 -0300843 if (gspca_dev->usb_err < 0)
844 return gspca_dev->usb_err;
845
Jean-François Moine618a8642010-06-05 07:45:04 -0300846/* it returns:
847 * 03 00 12 93 0b f6 c9 00 live! ultra
848 * 03 00 07 93 0b f6 ca 00 live! ultra for notebook
849 * 03 00 12 93 0b fe c8 00 Trust WB-3500T
850 * 02 00 06 93 0b fe c8 00 Joy-IT 318S
851 * 03 00 12 93 0b f6 cf 00 icam tracer - sensor icx098bq
852 * 02 00 12 93 0b fe cf 00 ProQ Motion Webcam
853 *
854 * byte
855 * 0: 02 = usb 1.0 (12Mbit) / 03 = usb2.0 (480Mbit)
856 * 1: 00
857 * 2: 06 / 07 / 12 = mode webcam? firmware??
858 * 3: 93 chip = 930b (930b or 930c)
859 * 4: 0b
Jean-François Moinea68f7232010-06-24 05:02:57 -0300860 * 5: f6 = cdd (icx098bq, lz24bp) / fe or de = cmos (i2c) (other sensors)
Jean-François Moine618a8642010-06-05 07:45:04 -0300861 * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
862 * 7: 00
863 */
Andy Shevchenko70aa3452012-08-07 12:43:07 -0300864 PDEBUG(D_PROBE, "info: %*ph", 8, gspca_dev->usb_buf);
Jean-François Moine618a8642010-06-05 07:45:04 -0300865
Jean-François Moinea68f7232010-06-24 05:02:57 -0300866 bridge_init(sd);
867
868 if (sd->sensor == SENSOR_MI0360) {
869
870 /* no sensor probe for icam tracer */
Jean-François Moinef2c5d922010-12-25 13:19:59 -0300871 if (gspca_dev->usb_buf[5] == 0xf6) /* if ccd */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300872 sd->sensor = SENSOR_ICX098BQ;
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300873 else
Jean-François Moinea68f7232010-06-24 05:02:57 -0300874 cmos_probe(gspca_dev);
Jean-François Moine618a8642010-06-05 07:45:04 -0300875 }
Jean-François Moine3ebd1792010-12-25 13:07:57 -0300876 if (gspca_dev->usb_err >= 0) {
877 PDEBUG(D_PROBE, "Sensor %s", sensor_tb[sd->sensor].name);
878 global_init(sd, 1);
879 }
Jean-François Moine618a8642010-06-05 07:45:04 -0300880 return gspca_dev->usb_err;
881}
882
Jean-François Moine618a8642010-06-05 07:45:04 -0300883/* send the start/stop commands to the webcam */
884static void send_start(struct gspca_dev *gspca_dev)
885{
886 struct sd *sd = (struct sd *) gspca_dev;
887 const struct cap_s *cap;
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300888 int mode;
Jean-François Moine618a8642010-06-05 07:45:04 -0300889
890 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
891 cap = &capconfig[sd->sensor][mode];
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300892 reg_wb(gspca_dev, 0x0900 | SQ930_CTRL_CAP_START,
893 0x0a00 | cap->cc_sizeid,
Jean-François Moine618a8642010-06-05 07:45:04 -0300894 cap->cc_bytes, 32);
Jean-François Moine6e80cc52010-07-26 07:27:13 -0300895}
896
Jean-François Moine618a8642010-06-05 07:45:04 -0300897static void send_stop(struct gspca_dev *gspca_dev)
898{
899 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
Jean-François Moine6e80cc52010-07-26 07:27:13 -0300900}
Jean-François Moine618a8642010-06-05 07:45:04 -0300901
902/* function called at start time before URB creation */
903static int sd_isoc_init(struct gspca_dev *gspca_dev)
904{
905 struct sd *sd = (struct sd *) gspca_dev;
906
907 gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */
908 sd->do_ctrl = 0;
Ondrej Zary1966bc22013-08-30 17:54:23 -0300909 gspca_dev->cam.bulk_size = gspca_dev->pixfmt.width *
910 gspca_dev->pixfmt.height + 8;
Jean-François Moine618a8642010-06-05 07:45:04 -0300911 return 0;
912}
913
914/* start the capture */
915static int sd_start(struct gspca_dev *gspca_dev)
916{
917 struct sd *sd = (struct sd *) gspca_dev;
918 int mode;
919
Jean-François Moinea68f7232010-06-24 05:02:57 -0300920 bridge_init(sd);
Jean-François Moine618a8642010-06-05 07:45:04 -0300921 global_init(sd, 0);
922 msleep(100);
923
924 switch (sd->sensor) {
925 case SENSOR_ICX098BQ:
926 ucbus_write(gspca_dev, icx098bq_start_0,
927 ARRAY_SIZE(icx098bq_start_0),
928 8);
929 ucbus_write(gspca_dev, icx098bq_start_1,
930 ARRAY_SIZE(icx098bq_start_1),
931 5);
932 ucbus_write(gspca_dev, icx098bq_start_2,
933 ARRAY_SIZE(icx098bq_start_2),
934 6);
935 msleep(50);
936
937 /* 1st start */
938 send_start(gspca_dev);
939 gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
940 msleep(70);
941 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
942 gpio_set(sd, 0x7f, 0x00ff);
943
944 /* 2nd start */
945 send_start(gspca_dev);
946 gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
947 goto out;
948 case SENSOR_LZ24BP:
949 ucbus_write(gspca_dev, lz24bp_start_0,
950 ARRAY_SIZE(lz24bp_start_0),
951 8);
952 if (sd->type != Creative_live_motion)
953 ucbus_write(gspca_dev, lz24bp_start_1_gen,
954 ARRAY_SIZE(lz24bp_start_1_gen),
955 5);
956 else
957 ucbus_write(gspca_dev, lz24bp_start_1_clm,
958 ARRAY_SIZE(lz24bp_start_1_clm),
959 5);
960 ucbus_write(gspca_dev, lz24bp_start_2,
961 ARRAY_SIZE(lz24bp_start_2),
962 6);
963 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
Jean-François Moine82d2c7a2010-07-26 07:23:00 -0300964 lz24bp_ppl(sd, mode == 1 ? 0x0564 : 0x0310);
Jean-François Moine618a8642010-06-05 07:45:04 -0300965 msleep(10);
966 break;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300967 case SENSOR_MI0360:
Jean-François Moine618a8642010-06-05 07:45:04 -0300968 ucbus_write(gspca_dev, mi0360_start_0,
969 ARRAY_SIZE(mi0360_start_0),
970 8);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300971 i2c_write(sd, mi0360_init_23,
Jean-François Moine618a8642010-06-05 07:45:04 -0300972 ARRAY_SIZE(mi0360_init_23));
Jean-François Moinea68f7232010-06-24 05:02:57 -0300973 i2c_write(sd, mi0360_init_24,
Jean-François Moine618a8642010-06-05 07:45:04 -0300974 ARRAY_SIZE(mi0360_init_24));
Jean-François Moinea68f7232010-06-24 05:02:57 -0300975 i2c_write(sd, mi0360_init_25,
Jean-François Moine618a8642010-06-05 07:45:04 -0300976 ARRAY_SIZE(mi0360_init_25));
977 ucbus_write(gspca_dev, mi0360_start_1,
978 ARRAY_SIZE(mi0360_start_1),
979 5);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300980 i2c_write(sd, mi0360_start_2,
Jean-François Moine618a8642010-06-05 07:45:04 -0300981 ARRAY_SIZE(mi0360_start_2));
Jean-François Moinea68f7232010-06-24 05:02:57 -0300982 i2c_write(sd, mi0360_start_3,
Jean-François Moine618a8642010-06-05 07:45:04 -0300983 ARRAY_SIZE(mi0360_start_3));
984
985 /* 1st start */
986 send_start(gspca_dev);
987 msleep(60);
Jean-François Moine6e80cc52010-07-26 07:27:13 -0300988 send_stop(gspca_dev);
Jean-François Moine618a8642010-06-05 07:45:04 -0300989
Jean-François Moinea68f7232010-06-24 05:02:57 -0300990 i2c_write(sd,
Jean-François Moine618a8642010-06-05 07:45:04 -0300991 mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
992 break;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300993 default:
994/* case SENSOR_MT9V111: */
995 ucbus_write(gspca_dev, mi0360_start_0,
996 ARRAY_SIZE(mi0360_start_0),
997 8);
998 i2c_write(sd, mt9v111_init_0,
999 ARRAY_SIZE(mt9v111_init_0));
1000 i2c_write(sd, mt9v111_init_1,
1001 ARRAY_SIZE(mt9v111_init_1));
1002 i2c_write(sd, mt9v111_init_2,
1003 ARRAY_SIZE(mt9v111_init_2));
1004 ucbus_write(gspca_dev, mt9v111_start_1,
1005 ARRAY_SIZE(mt9v111_start_1),
Jean-François Moine46631172010-07-26 06:50:31 -03001006 5);
Jean-François Moinea68f7232010-06-24 05:02:57 -03001007 i2c_write(sd, mt9v111_init_3,
1008 ARRAY_SIZE(mt9v111_init_3));
1009 i2c_write(sd, mt9v111_init_4,
1010 ARRAY_SIZE(mt9v111_init_4));
1011 break;
Jean-François Moine618a8642010-06-05 07:45:04 -03001012 }
1013
1014 send_start(gspca_dev);
1015out:
1016 msleep(1000);
1017
Jean-François Moinea68f7232010-06-24 05:02:57 -03001018 if (sd->sensor == SENSOR_MT9V111)
1019 gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
1020
Jean-François Moine618a8642010-06-05 07:45:04 -03001021 sd->do_ctrl = 1; /* set the exposure */
1022
1023 return gspca_dev->usb_err;
1024}
1025
1026static void sd_stopN(struct gspca_dev *gspca_dev)
1027{
Jean-François Moinea68f7232010-06-24 05:02:57 -03001028 struct sd *sd = (struct sd *) gspca_dev;
1029
1030 if (sd->sensor == SENSOR_MT9V111)
1031 gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
Jean-François Moine618a8642010-06-05 07:45:04 -03001032 send_stop(gspca_dev);
1033}
1034
1035/* function called when the application gets a new frame */
1036/* It sets the exposure if required and restart the bulk transfer. */
1037static void sd_dq_callback(struct gspca_dev *gspca_dev)
1038{
1039 struct sd *sd = (struct sd *) gspca_dev;
1040 int ret;
1041
1042 if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
1043 return;
1044 sd->do_ctrl = 0;
1045
Hans Verkuildf0df1a2012-05-16 08:29:16 -03001046 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure),
1047 v4l2_ctrl_g_ctrl(sd->gain));
Jean-François Moine618a8642010-06-05 07:45:04 -03001048
1049 gspca_dev->cam.bulk_nurbs = 1;
1050 ret = usb_submit_urb(gspca_dev->urb[0], GFP_ATOMIC);
1051 if (ret < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -03001052 pr_err("sd_dq_callback() err %d\n", ret);
Jean-François Moine618a8642010-06-05 07:45:04 -03001053
1054 /* wait a little time, otherwise the webcam crashes */
1055 msleep(100);
1056}
1057
Jean-François Moine618a8642010-06-05 07:45:04 -03001058static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1059 u8 *data, /* isoc packet */
1060 int len) /* iso packet length */
1061{
1062 struct sd *sd = (struct sd *) gspca_dev;
Jean-François Moine618a8642010-06-05 07:45:04 -03001063
Jean-François Moine82d2c7a2010-07-26 07:23:00 -03001064 if (sd->do_ctrl)
1065 gspca_dev->cam.bulk_nurbs = 0;
1066 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
1067 gspca_frame_add(gspca_dev, INTER_PACKET, data, len - 8);
1068 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
Jean-François Moine618a8642010-06-05 07:45:04 -03001069}
1070
Hans Verkuildf0df1a2012-05-16 08:29:16 -03001071static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
Jean-François Moine618a8642010-06-05 07:45:04 -03001072{
Hans Verkuildf0df1a2012-05-16 08:29:16 -03001073 struct gspca_dev *gspca_dev =
1074 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
Jean-François Moine618a8642010-06-05 07:45:04 -03001075 struct sd *sd = (struct sd *) gspca_dev;
1076
Hans Verkuildf0df1a2012-05-16 08:29:16 -03001077 gspca_dev->usb_err = 0;
1078
1079 if (!gspca_dev->streaming)
1080 return 0;
1081
1082 switch (ctrl->id) {
1083 case V4L2_CID_EXPOSURE:
1084 setexposure(gspca_dev, ctrl->val, sd->gain->val);
1085 break;
1086 }
1087 return gspca_dev->usb_err;
Jean-François Moine618a8642010-06-05 07:45:04 -03001088}
1089
Hans Verkuildf0df1a2012-05-16 08:29:16 -03001090static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1091 .s_ctrl = sd_s_ctrl,
1092};
1093
1094static int sd_init_controls(struct gspca_dev *gspca_dev)
Jean-François Moine618a8642010-06-05 07:45:04 -03001095{
Hans Verkuildf0df1a2012-05-16 08:29:16 -03001096 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
Jean-François Moine618a8642010-06-05 07:45:04 -03001097 struct sd *sd = (struct sd *) gspca_dev;
1098
Hans Verkuildf0df1a2012-05-16 08:29:16 -03001099 gspca_dev->vdev.ctrl_handler = hdl;
1100 v4l2_ctrl_handler_init(hdl, 2);
1101 sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1102 V4L2_CID_EXPOSURE, 1, 0xfff, 1, 0x356);
1103 sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1104 V4L2_CID_GAIN, 1, 255, 1, 0x8d);
Jean-François Moine618a8642010-06-05 07:45:04 -03001105
Hans Verkuildf0df1a2012-05-16 08:29:16 -03001106 if (hdl->error) {
1107 pr_err("Could not initialize controls\n");
1108 return hdl->error;
1109 }
1110 v4l2_ctrl_cluster(2, &sd->exposure);
Jean-François Moine618a8642010-06-05 07:45:04 -03001111 return 0;
1112}
1113
Jean-François Moine618a8642010-06-05 07:45:04 -03001114/* sub-driver description */
1115static const struct sd_desc sd_desc = {
1116 .name = MODULE_NAME,
Jean-François Moine618a8642010-06-05 07:45:04 -03001117 .config = sd_config,
1118 .init = sd_init,
Hans Verkuildf0df1a2012-05-16 08:29:16 -03001119 .init_controls = sd_init_controls,
Jean-François Moine618a8642010-06-05 07:45:04 -03001120 .isoc_init = sd_isoc_init,
1121 .start = sd_start,
1122 .stopN = sd_stopN,
1123 .pkt_scan = sd_pkt_scan,
1124 .dq_callback = sd_dq_callback,
Jean-François Moine618a8642010-06-05 07:45:04 -03001125};
1126
1127/* Table of supported USB devices */
1128#define ST(sensor, type) \
1129 .driver_info = (SENSOR_ ## sensor << 8) \
1130 | (type)
Jean-François Moine95c967c2011-01-13 05:20:29 -03001131static const struct usb_device_id device_table[] = {
Jean-François Moine618a8642010-06-05 07:45:04 -03001132 {USB_DEVICE(0x041e, 0x4038), ST(MI0360, 0)},
1133 {USB_DEVICE(0x041e, 0x403c), ST(LZ24BP, 0)},
1134 {USB_DEVICE(0x041e, 0x403d), ST(LZ24BP, 0)},
1135 {USB_DEVICE(0x041e, 0x4041), ST(LZ24BP, Creative_live_motion)},
Jean-François Moinea68f7232010-06-24 05:02:57 -03001136 {USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)},
Jean-François Moine618a8642010-06-05 07:45:04 -03001137 {USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
1138 {}
1139};
1140MODULE_DEVICE_TABLE(usb, device_table);
1141
1142
1143/* -- device connect -- */
1144static int sd_probe(struct usb_interface *intf,
1145 const struct usb_device_id *id)
1146{
1147 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1148 THIS_MODULE);
1149}
1150
1151static struct usb_driver sd_driver = {
1152 .name = MODULE_NAME,
1153 .id_table = device_table,
1154 .probe = sd_probe,
1155 .disconnect = gspca_disconnect,
1156#ifdef CONFIG_PM
1157 .suspend = gspca_suspend,
1158 .resume = gspca_resume,
Hans de Goede8bb58962012-06-30 06:44:47 -03001159 .reset_resume = gspca_resume,
Jean-François Moine618a8642010-06-05 07:45:04 -03001160#endif
1161};
1162
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001163module_usb_driver(sd_driver);