Commit | Line | Data |
---|---|---|
7397bfbe GL |
1 | /* |
2 | * Driver for MT9V022 CMOS Image Sensor from Micron | |
3 | * | |
4 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
11 | #include <linux/videodev2.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/i2c.h> | |
14 | #include <linux/delay.h> | |
15 | #include <linux/log2.h> | |
91e64c88 | 16 | #include <linux/gpio.h> |
7397bfbe GL |
17 | |
18 | #include <media/v4l2-common.h> | |
19 | #include <media/v4l2-chip-ident.h> | |
20 | #include <media/soc_camera.h> | |
21 | ||
7397bfbe GL |
22 | /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c |
23 | * The platform has to define i2c_board_info | |
24 | * and call i2c_register_board_info() */ | |
25 | ||
26 | static char *sensor_type; | |
27 | module_param(sensor_type, charp, S_IRUGO); | |
61a2d07d | 28 | MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\""); |
7397bfbe GL |
29 | |
30 | /* mt9v022 selected register addresses */ | |
31 | #define MT9V022_CHIP_VERSION 0x00 | |
32 | #define MT9V022_COLUMN_START 0x01 | |
33 | #define MT9V022_ROW_START 0x02 | |
34 | #define MT9V022_WINDOW_HEIGHT 0x03 | |
35 | #define MT9V022_WINDOW_WIDTH 0x04 | |
36 | #define MT9V022_HORIZONTAL_BLANKING 0x05 | |
37 | #define MT9V022_VERTICAL_BLANKING 0x06 | |
38 | #define MT9V022_CHIP_CONTROL 0x07 | |
39 | #define MT9V022_SHUTTER_WIDTH1 0x08 | |
40 | #define MT9V022_SHUTTER_WIDTH2 0x09 | |
41 | #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a | |
42 | #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b | |
43 | #define MT9V022_RESET 0x0c | |
44 | #define MT9V022_READ_MODE 0x0d | |
45 | #define MT9V022_MONITOR_MODE 0x0e | |
46 | #define MT9V022_PIXEL_OPERATION_MODE 0x0f | |
47 | #define MT9V022_LED_OUT_CONTROL 0x1b | |
48 | #define MT9V022_ADC_MODE_CONTROL 0x1c | |
49 | #define MT9V022_ANALOG_GAIN 0x34 | |
50 | #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47 | |
51 | #define MT9V022_PIXCLK_FV_LV 0x74 | |
52 | #define MT9V022_DIGITAL_TEST_PATTERN 0x7f | |
53 | #define MT9V022_AEC_AGC_ENABLE 0xAF | |
54 | #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD | |
55 | ||
56 | /* Progressive scan, master, defaults */ | |
57 | #define MT9V022_CHIP_CONTROL_DEFAULT 0x188 | |
58 | ||
bb55de3b GL |
59 | static const struct soc_camera_data_format mt9v022_colour_formats[] = { |
60 | /* Order important: first natively supported, | |
61 | * second supported with a GPIO extender */ | |
7397bfbe | 62 | { |
bb55de3b | 63 | .name = "Bayer (sRGB) 10 bit", |
7397bfbe GL |
64 | .depth = 10, |
65 | .fourcc = V4L2_PIX_FMT_SBGGR16, | |
66 | .colorspace = V4L2_COLORSPACE_SRGB, | |
67 | }, { | |
bb55de3b GL |
68 | .name = "Bayer (sRGB) 8 bit", |
69 | .depth = 8, | |
70 | .fourcc = V4L2_PIX_FMT_SBGGR8, | |
71 | .colorspace = V4L2_COLORSPACE_SRGB, | |
72 | } | |
73 | }; | |
74 | ||
75 | static const struct soc_camera_data_format mt9v022_monochrome_formats[] = { | |
76 | /* Order important - see above */ | |
77 | { | |
7397bfbe GL |
78 | .name = "Monochrome 10 bit", |
79 | .depth = 10, | |
80 | .fourcc = V4L2_PIX_FMT_Y16, | |
81 | }, { | |
82 | .name = "Monochrome 8 bit", | |
83 | .depth = 8, | |
84 | .fourcc = V4L2_PIX_FMT_GREY, | |
85 | }, | |
86 | }; | |
87 | ||
88 | struct mt9v022 { | |
89 | struct i2c_client *client; | |
90 | struct soc_camera_device icd; | |
7fb0fd05 | 91 | int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */ |
7397bfbe GL |
92 | int switch_gpio; |
93 | u16 chip_control; | |
94 | unsigned char datawidth; | |
95 | }; | |
96 | ||
97 | static int reg_read(struct soc_camera_device *icd, const u8 reg) | |
98 | { | |
99 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
100 | struct i2c_client *client = mt9v022->client; | |
101 | s32 data = i2c_smbus_read_word_data(client, reg); | |
102 | return data < 0 ? data : swab16(data); | |
103 | } | |
104 | ||
105 | static int reg_write(struct soc_camera_device *icd, const u8 reg, | |
106 | const u16 data) | |
107 | { | |
108 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
109 | return i2c_smbus_write_word_data(mt9v022->client, reg, swab16(data)); | |
110 | } | |
111 | ||
112 | static int reg_set(struct soc_camera_device *icd, const u8 reg, | |
113 | const u16 data) | |
114 | { | |
115 | int ret; | |
116 | ||
117 | ret = reg_read(icd, reg); | |
118 | if (ret < 0) | |
119 | return ret; | |
120 | return reg_write(icd, reg, ret | data); | |
121 | } | |
122 | ||
123 | static int reg_clear(struct soc_camera_device *icd, const u8 reg, | |
124 | const u16 data) | |
125 | { | |
126 | int ret; | |
127 | ||
128 | ret = reg_read(icd, reg); | |
129 | if (ret < 0) | |
130 | return ret; | |
131 | return reg_write(icd, reg, ret & ~data); | |
132 | } | |
133 | ||
134 | static int mt9v022_init(struct soc_camera_device *icd) | |
135 | { | |
136 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
137 | int ret; | |
138 | ||
139 | /* Almost the default mode: master, parallel, simultaneous, and an | |
140 | * undocumented bit 0x200, which is present in table 7, but not in 8, | |
141 | * plus snapshot mode to disable scan for now */ | |
142 | mt9v022->chip_control |= 0x10; | |
143 | ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control); | |
144 | if (ret >= 0) | |
145 | reg_write(icd, MT9V022_READ_MODE, 0x300); | |
146 | ||
147 | /* All defaults */ | |
148 | if (ret >= 0) | |
149 | /* AEC, AGC on */ | |
150 | ret = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x3); | |
151 | if (ret >= 0) | |
152 | ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480); | |
153 | if (ret >= 0) | |
154 | /* default - auto */ | |
155 | ret = reg_clear(icd, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1); | |
156 | if (ret >= 0) | |
157 | ret = reg_write(icd, MT9V022_DIGITAL_TEST_PATTERN, 0); | |
158 | ||
159 | return ret >= 0 ? 0 : -EIO; | |
160 | } | |
161 | ||
162 | static int mt9v022_release(struct soc_camera_device *icd) | |
163 | { | |
164 | /* Nothing? */ | |
165 | return 0; | |
166 | } | |
167 | ||
168 | static int mt9v022_start_capture(struct soc_camera_device *icd) | |
169 | { | |
170 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
171 | /* Switch to master "normal" mode */ | |
172 | mt9v022->chip_control &= ~0x10; | |
173 | if (reg_write(icd, MT9V022_CHIP_CONTROL, | |
174 | mt9v022->chip_control) < 0) | |
175 | return -EIO; | |
176 | return 0; | |
177 | } | |
178 | ||
179 | static int mt9v022_stop_capture(struct soc_camera_device *icd) | |
180 | { | |
181 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
182 | /* Switch to snapshot mode */ | |
183 | mt9v022->chip_control |= 0x10; | |
184 | if (reg_write(icd, MT9V022_CHIP_CONTROL, | |
185 | mt9v022->chip_control) < 0) | |
186 | return -EIO; | |
187 | return 0; | |
188 | } | |
189 | ||
190 | static int bus_switch_request(struct mt9v022 *mt9v022, struct soc_camera_link *icl) | |
191 | { | |
192 | #ifdef CONFIG_MT9V022_PCA9536_SWITCH | |
193 | int ret; | |
194 | unsigned int gpio = icl->gpio; | |
195 | ||
b4333a3b | 196 | if (gpio_is_valid(gpio)) { |
7397bfbe GL |
197 | /* We have a data bus switch. */ |
198 | ret = gpio_request(gpio, "mt9v022"); | |
199 | if (ret < 0) { | |
200 | dev_err(&mt9v022->client->dev, "Cannot get GPIO %u\n", gpio); | |
201 | return ret; | |
202 | } | |
203 | ||
204 | ret = gpio_direction_output(gpio, 0); | |
205 | if (ret < 0) { | |
206 | dev_err(&mt9v022->client->dev, | |
207 | "Cannot set GPIO %u to output\n", gpio); | |
208 | gpio_free(gpio); | |
209 | return ret; | |
210 | } | |
211 | } | |
212 | ||
213 | mt9v022->switch_gpio = gpio; | |
214 | #else | |
b4333a3b | 215 | mt9v022->switch_gpio = -EINVAL; |
7397bfbe GL |
216 | #endif |
217 | return 0; | |
218 | } | |
219 | ||
220 | static void bus_switch_release(struct mt9v022 *mt9v022) | |
221 | { | |
222 | #ifdef CONFIG_MT9V022_PCA9536_SWITCH | |
b4333a3b | 223 | if (gpio_is_valid(mt9v022->switch_gpio)) |
7397bfbe GL |
224 | gpio_free(mt9v022->switch_gpio); |
225 | #endif | |
226 | } | |
227 | ||
228 | static int bus_switch_act(struct mt9v022 *mt9v022, int go8bit) | |
229 | { | |
230 | #ifdef CONFIG_MT9V022_PCA9536_SWITCH | |
b4333a3b | 231 | if (!gpio_is_valid(mt9v022->switch_gpio)) |
7397bfbe GL |
232 | return -ENODEV; |
233 | ||
234 | gpio_set_value_cansleep(mt9v022->switch_gpio, go8bit); | |
235 | return 0; | |
236 | #else | |
237 | return -ENODEV; | |
238 | #endif | |
239 | } | |
240 | ||
ad5f2e85 GL |
241 | static int bus_switch_possible(struct mt9v022 *mt9v022) |
242 | { | |
243 | #ifdef CONFIG_MT9V022_PCA9536_SWITCH | |
244 | return gpio_is_valid(mt9v022->switch_gpio); | |
245 | #else | |
246 | return 0; | |
247 | #endif | |
248 | } | |
249 | ||
250 | static int mt9v022_set_bus_param(struct soc_camera_device *icd, | |
251 | unsigned long flags) | |
7397bfbe GL |
252 | { |
253 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
ad5f2e85 | 254 | unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK; |
7397bfbe | 255 | int ret; |
ad5f2e85 | 256 | u16 pixclk = 0; |
7397bfbe GL |
257 | |
258 | /* Only one width bit may be set */ | |
259 | if (!is_power_of_2(width_flag)) | |
260 | return -EINVAL; | |
261 | ||
ad5f2e85 GL |
262 | if ((mt9v022->datawidth != 10 && (width_flag == SOCAM_DATAWIDTH_10)) || |
263 | (mt9v022->datawidth != 9 && (width_flag == SOCAM_DATAWIDTH_9)) || | |
264 | (mt9v022->datawidth != 8 && (width_flag == SOCAM_DATAWIDTH_8))) { | |
265 | /* Well, we actually only can do 10 or 8 bits... */ | |
266 | if (width_flag == SOCAM_DATAWIDTH_9) | |
267 | return -EINVAL; | |
268 | ||
269 | ret = bus_switch_act(mt9v022, | |
270 | width_flag == SOCAM_DATAWIDTH_8); | |
271 | if (ret < 0) | |
272 | return ret; | |
273 | ||
274 | mt9v022->datawidth = width_flag == SOCAM_DATAWIDTH_8 ? 8 : 10; | |
275 | } | |
276 | ||
277 | if (flags & SOCAM_PCLK_SAMPLE_RISING) | |
278 | pixclk |= 0x10; | |
279 | ||
280 | if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH)) | |
281 | pixclk |= 0x1; | |
282 | ||
283 | if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH)) | |
284 | pixclk |= 0x2; | |
285 | ||
286 | ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk); | |
287 | if (ret < 0) | |
288 | return ret; | |
289 | ||
290 | if (!(flags & SOCAM_MASTER)) | |
291 | mt9v022->chip_control &= ~0x8; | |
292 | ||
293 | ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control); | |
294 | if (ret < 0) | |
295 | return ret; | |
296 | ||
297 | dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n", | |
298 | pixclk, mt9v022->chip_control); | |
299 | ||
300 | return 0; | |
301 | } | |
302 | ||
303 | static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd) | |
304 | { | |
305 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
306 | unsigned int width_flag = SOCAM_DATAWIDTH_10; | |
307 | ||
308 | if (bus_switch_possible(mt9v022)) | |
309 | width_flag |= SOCAM_DATAWIDTH_8; | |
310 | ||
311 | return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING | | |
312 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW | | |
313 | SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW | | |
314 | SOCAM_MASTER | SOCAM_SLAVE | | |
315 | width_flag; | |
316 | } | |
317 | ||
318 | static int mt9v022_set_fmt_cap(struct soc_camera_device *icd, | |
319 | __u32 pixfmt, struct v4l2_rect *rect) | |
320 | { | |
321 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
322 | int ret; | |
323 | ||
7397bfbe GL |
324 | /* The caller provides a supported format, as verified per call to |
325 | * icd->try_fmt_cap(), datawidth is from our supported format list */ | |
326 | switch (pixfmt) { | |
327 | case V4L2_PIX_FMT_GREY: | |
328 | case V4L2_PIX_FMT_Y16: | |
329 | if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM) | |
330 | return -EINVAL; | |
331 | break; | |
332 | case V4L2_PIX_FMT_SBGGR8: | |
333 | case V4L2_PIX_FMT_SBGGR16: | |
334 | if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC) | |
335 | return -EINVAL; | |
336 | break; | |
337 | case 0: | |
338 | /* No format change, only geometry */ | |
339 | break; | |
340 | default: | |
341 | return -EINVAL; | |
342 | } | |
343 | ||
344 | /* Like in example app. Contradicts the datasheet though */ | |
345 | ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE); | |
346 | if (ret >= 0) { | |
347 | if (ret & 1) /* Autoexposure */ | |
348 | ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, | |
349 | rect->height + icd->y_skip_top + 43); | |
350 | else | |
351 | ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH, | |
352 | rect->height + icd->y_skip_top + 43); | |
353 | } | |
354 | /* Setup frame format: defaults apart from width and height */ | |
355 | if (ret >= 0) | |
356 | ret = reg_write(icd, MT9V022_COLUMN_START, rect->left); | |
357 | if (ret >= 0) | |
358 | ret = reg_write(icd, MT9V022_ROW_START, rect->top); | |
359 | if (ret >= 0) | |
360 | /* Default 94, Phytec driver says: | |
361 | * "width + horizontal blank >= 660" */ | |
362 | ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING, | |
363 | rect->width > 660 - 43 ? 43 : | |
364 | 660 - rect->width); | |
365 | if (ret >= 0) | |
366 | ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45); | |
367 | if (ret >= 0) | |
368 | ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width); | |
369 | if (ret >= 0) | |
370 | ret = reg_write(icd, MT9V022_WINDOW_HEIGHT, | |
371 | rect->height + icd->y_skip_top); | |
372 | ||
373 | if (ret < 0) | |
374 | return ret; | |
375 | ||
376 | dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height); | |
377 | ||
7397bfbe GL |
378 | return 0; |
379 | } | |
380 | ||
381 | static int mt9v022_try_fmt_cap(struct soc_camera_device *icd, | |
382 | struct v4l2_format *f) | |
383 | { | |
384 | if (f->fmt.pix.height < 32 + icd->y_skip_top) | |
385 | f->fmt.pix.height = 32 + icd->y_skip_top; | |
386 | if (f->fmt.pix.height > 480 + icd->y_skip_top) | |
387 | f->fmt.pix.height = 480 + icd->y_skip_top; | |
388 | if (f->fmt.pix.width < 48) | |
389 | f->fmt.pix.width = 48; | |
390 | if (f->fmt.pix.width > 752) | |
391 | f->fmt.pix.width = 752; | |
392 | f->fmt.pix.width &= ~0x03; /* ? */ | |
393 | ||
394 | return 0; | |
395 | } | |
396 | ||
397 | static int mt9v022_get_chip_id(struct soc_camera_device *icd, | |
398 | struct v4l2_chip_ident *id) | |
399 | { | |
400 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
401 | ||
402 | if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR) | |
403 | return -EINVAL; | |
404 | ||
405 | if (id->match_chip != mt9v022->client->addr) | |
406 | return -ENODEV; | |
407 | ||
408 | id->ident = mt9v022->model; | |
409 | id->revision = 0; | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
415 | static int mt9v022_get_register(struct soc_camera_device *icd, | |
416 | struct v4l2_register *reg) | |
417 | { | |
418 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
419 | ||
420 | if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) | |
421 | return -EINVAL; | |
422 | ||
423 | if (reg->match_chip != mt9v022->client->addr) | |
424 | return -ENODEV; | |
425 | ||
426 | reg->val = reg_read(icd, reg->reg); | |
427 | ||
428 | if (reg->val > 0xffff) | |
429 | return -EIO; | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | static int mt9v022_set_register(struct soc_camera_device *icd, | |
435 | struct v4l2_register *reg) | |
436 | { | |
437 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
438 | ||
439 | if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) | |
440 | return -EINVAL; | |
441 | ||
442 | if (reg->match_chip != mt9v022->client->addr) | |
443 | return -ENODEV; | |
444 | ||
445 | if (reg_write(icd, reg->reg, reg->val) < 0) | |
446 | return -EIO; | |
447 | ||
448 | return 0; | |
449 | } | |
450 | #endif | |
451 | ||
4407a463 | 452 | static const struct v4l2_queryctrl mt9v022_controls[] = { |
7397bfbe GL |
453 | { |
454 | .id = V4L2_CID_VFLIP, | |
455 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
456 | .name = "Flip Vertically", | |
457 | .minimum = 0, | |
458 | .maximum = 1, | |
459 | .step = 1, | |
460 | .default_value = 0, | |
461 | }, { | |
462 | .id = V4L2_CID_HFLIP, | |
463 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
464 | .name = "Flip Horizontally", | |
465 | .minimum = 0, | |
466 | .maximum = 1, | |
467 | .step = 1, | |
468 | .default_value = 0, | |
469 | }, { | |
470 | .id = V4L2_CID_GAIN, | |
471 | .type = V4L2_CTRL_TYPE_INTEGER, | |
472 | .name = "Analog Gain", | |
473 | .minimum = 64, | |
474 | .maximum = 127, | |
475 | .step = 1, | |
476 | .default_value = 64, | |
477 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
478 | }, { | |
479 | .id = V4L2_CID_EXPOSURE, | |
480 | .type = V4L2_CTRL_TYPE_INTEGER, | |
481 | .name = "Exposure", | |
482 | .minimum = 1, | |
483 | .maximum = 255, | |
484 | .step = 1, | |
485 | .default_value = 255, | |
486 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
487 | }, { | |
488 | .id = V4L2_CID_AUTOGAIN, | |
489 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
490 | .name = "Automatic Gain", | |
491 | .minimum = 0, | |
492 | .maximum = 1, | |
493 | .step = 1, | |
494 | .default_value = 1, | |
495 | }, { | |
496 | .id = V4L2_CID_EXPOSURE_AUTO, | |
497 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
498 | .name = "Automatic Exposure", | |
499 | .minimum = 0, | |
500 | .maximum = 1, | |
501 | .step = 1, | |
502 | .default_value = 1, | |
503 | } | |
504 | }; | |
505 | ||
26f1b942 GL |
506 | static int mt9v022_video_probe(struct soc_camera_device *); |
507 | static void mt9v022_video_remove(struct soc_camera_device *); | |
508 | static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *); | |
509 | static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *); | |
7397bfbe GL |
510 | |
511 | static struct soc_camera_ops mt9v022_ops = { | |
512 | .owner = THIS_MODULE, | |
26f1b942 GL |
513 | .probe = mt9v022_video_probe, |
514 | .remove = mt9v022_video_remove, | |
7397bfbe GL |
515 | .init = mt9v022_init, |
516 | .release = mt9v022_release, | |
517 | .start_capture = mt9v022_start_capture, | |
518 | .stop_capture = mt9v022_stop_capture, | |
ad5f2e85 | 519 | .set_fmt_cap = mt9v022_set_fmt_cap, |
7397bfbe | 520 | .try_fmt_cap = mt9v022_try_fmt_cap, |
ad5f2e85 GL |
521 | .set_bus_param = mt9v022_set_bus_param, |
522 | .query_bus_param = mt9v022_query_bus_param, | |
7397bfbe GL |
523 | .controls = mt9v022_controls, |
524 | .num_controls = ARRAY_SIZE(mt9v022_controls), | |
525 | .get_control = mt9v022_get_control, | |
526 | .set_control = mt9v022_set_control, | |
527 | .get_chip_id = mt9v022_get_chip_id, | |
528 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
529 | .get_register = mt9v022_get_register, | |
530 | .set_register = mt9v022_set_register, | |
531 | #endif | |
532 | }; | |
533 | ||
534 | static int mt9v022_get_control(struct soc_camera_device *icd, | |
535 | struct v4l2_control *ctrl) | |
536 | { | |
537 | int data; | |
538 | ||
539 | switch (ctrl->id) { | |
540 | case V4L2_CID_VFLIP: | |
541 | data = reg_read(icd, MT9V022_READ_MODE); | |
542 | if (data < 0) | |
543 | return -EIO; | |
544 | ctrl->value = !!(data & 0x10); | |
545 | break; | |
546 | case V4L2_CID_HFLIP: | |
547 | data = reg_read(icd, MT9V022_READ_MODE); | |
548 | if (data < 0) | |
549 | return -EIO; | |
550 | ctrl->value = !!(data & 0x20); | |
551 | break; | |
552 | case V4L2_CID_EXPOSURE_AUTO: | |
553 | data = reg_read(icd, MT9V022_AEC_AGC_ENABLE); | |
554 | if (data < 0) | |
555 | return -EIO; | |
556 | ctrl->value = !!(data & 0x1); | |
557 | break; | |
558 | case V4L2_CID_AUTOGAIN: | |
559 | data = reg_read(icd, MT9V022_AEC_AGC_ENABLE); | |
560 | if (data < 0) | |
561 | return -EIO; | |
562 | ctrl->value = !!(data & 0x2); | |
563 | break; | |
564 | } | |
565 | return 0; | |
566 | } | |
567 | ||
568 | static int mt9v022_set_control(struct soc_camera_device *icd, | |
569 | struct v4l2_control *ctrl) | |
570 | { | |
571 | int data; | |
572 | const struct v4l2_queryctrl *qctrl; | |
573 | ||
574 | qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id); | |
575 | ||
576 | if (!qctrl) | |
577 | return -EINVAL; | |
578 | ||
579 | switch (ctrl->id) { | |
580 | case V4L2_CID_VFLIP: | |
581 | if (ctrl->value) | |
582 | data = reg_set(icd, MT9V022_READ_MODE, 0x10); | |
583 | else | |
584 | data = reg_clear(icd, MT9V022_READ_MODE, 0x10); | |
585 | if (data < 0) | |
586 | return -EIO; | |
587 | break; | |
588 | case V4L2_CID_HFLIP: | |
589 | if (ctrl->value) | |
590 | data = reg_set(icd, MT9V022_READ_MODE, 0x20); | |
591 | else | |
592 | data = reg_clear(icd, MT9V022_READ_MODE, 0x20); | |
593 | if (data < 0) | |
594 | return -EIO; | |
595 | break; | |
596 | case V4L2_CID_GAIN: | |
597 | /* mt9v022 has minimum == default */ | |
598 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
599 | return -EINVAL; | |
600 | else { | |
601 | unsigned long range = qctrl->maximum - qctrl->minimum; | |
602 | /* Datasheet says 16 to 64. autogain only works properly | |
603 | * after setting gain to maximum 14. Larger values | |
604 | * produce "white fly" noise effect. On the whole, | |
605 | * manually setting analog gain does no good. */ | |
606 | unsigned long gain = ((ctrl->value - qctrl->minimum) * | |
607 | 10 + range / 2) / range + 4; | |
608 | if (gain >= 32) | |
609 | gain &= ~1; | |
610 | /* The user wants to set gain manually, hope, she | |
611 | * knows, what she's doing... Switch AGC off. */ | |
612 | ||
613 | if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0) | |
614 | return -EIO; | |
615 | ||
616 | dev_info(&icd->dev, "Setting gain from %d to %lu\n", | |
617 | reg_read(icd, MT9V022_ANALOG_GAIN), gain); | |
618 | if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0) | |
619 | return -EIO; | |
620 | icd->gain = ctrl->value; | |
621 | } | |
622 | break; | |
623 | case V4L2_CID_EXPOSURE: | |
624 | /* mt9v022 has maximum == default */ | |
625 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
626 | return -EINVAL; | |
627 | else { | |
628 | unsigned long range = qctrl->maximum - qctrl->minimum; | |
629 | unsigned long shutter = ((ctrl->value - qctrl->minimum) * | |
630 | 479 + range / 2) / range + 1; | |
631 | /* The user wants to set shutter width manually, hope, | |
632 | * she knows, what she's doing... Switch AEC off. */ | |
633 | ||
634 | if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0) | |
635 | return -EIO; | |
636 | ||
637 | dev_dbg(&icd->dev, "Shutter width from %d to %lu\n", | |
638 | reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH), | |
639 | shutter); | |
640 | if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH, | |
641 | shutter) < 0) | |
642 | return -EIO; | |
643 | icd->exposure = ctrl->value; | |
644 | } | |
645 | break; | |
646 | case V4L2_CID_AUTOGAIN: | |
647 | if (ctrl->value) | |
648 | data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2); | |
649 | else | |
650 | data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2); | |
651 | if (data < 0) | |
652 | return -EIO; | |
653 | break; | |
654 | case V4L2_CID_EXPOSURE_AUTO: | |
655 | if (ctrl->value) | |
656 | data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1); | |
657 | else | |
658 | data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1); | |
659 | if (data < 0) | |
660 | return -EIO; | |
661 | break; | |
662 | } | |
663 | return 0; | |
664 | } | |
665 | ||
666 | /* Interface active, can use i2c. If it fails, it can indeed mean, that | |
667 | * this wasn't our capture interface, so, we wait for the right one */ | |
668 | static int mt9v022_video_probe(struct soc_camera_device *icd) | |
669 | { | |
670 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
671 | s32 data; | |
672 | int ret; | |
673 | ||
674 | if (!icd->dev.parent || | |
675 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) | |
676 | return -ENODEV; | |
677 | ||
678 | /* Read out the chip version register */ | |
679 | data = reg_read(icd, MT9V022_CHIP_VERSION); | |
680 | ||
681 | /* must be 0x1311 or 0x1313 */ | |
682 | if (data != 0x1311 && data != 0x1313) { | |
683 | ret = -ENODEV; | |
684 | dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n", | |
685 | data); | |
686 | goto ei2c; | |
687 | } | |
688 | ||
689 | /* Soft reset */ | |
690 | ret = reg_write(icd, MT9V022_RESET, 1); | |
691 | if (ret < 0) | |
692 | goto ei2c; | |
693 | /* 15 clock cycles */ | |
694 | udelay(200); | |
695 | if (reg_read(icd, MT9V022_RESET)) { | |
696 | dev_err(&icd->dev, "Resetting MT9V022 failed!\n"); | |
697 | goto ei2c; | |
698 | } | |
699 | ||
700 | /* Set monochrome or colour sensor type */ | |
701 | if (sensor_type && (!strcmp("colour", sensor_type) || | |
702 | !strcmp("color", sensor_type))) { | |
703 | ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11); | |
704 | mt9v022->model = V4L2_IDENT_MT9V022IX7ATC; | |
26f1b942 | 705 | icd->formats = mt9v022_colour_formats; |
bb55de3b | 706 | if (mt9v022->client->dev.platform_data) |
26f1b942 | 707 | icd->num_formats = ARRAY_SIZE(mt9v022_colour_formats); |
bb55de3b | 708 | else |
26f1b942 | 709 | icd->num_formats = 1; |
7397bfbe GL |
710 | } else { |
711 | ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11); | |
712 | mt9v022->model = V4L2_IDENT_MT9V022IX7ATM; | |
26f1b942 | 713 | icd->formats = mt9v022_monochrome_formats; |
bb55de3b | 714 | if (mt9v022->client->dev.platform_data) |
26f1b942 | 715 | icd->num_formats = ARRAY_SIZE(mt9v022_monochrome_formats); |
bb55de3b | 716 | else |
26f1b942 | 717 | icd->num_formats = 1; |
7397bfbe GL |
718 | } |
719 | ||
720 | if (ret >= 0) | |
721 | ret = soc_camera_video_start(icd); | |
722 | if (ret < 0) | |
723 | goto eisis; | |
724 | ||
725 | dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n", | |
726 | data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ? | |
727 | "monochrome" : "colour"); | |
728 | ||
729 | return 0; | |
730 | ||
731 | eisis: | |
732 | ei2c: | |
733 | return ret; | |
734 | } | |
735 | ||
736 | static void mt9v022_video_remove(struct soc_camera_device *icd) | |
737 | { | |
738 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); | |
739 | ||
740 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr, | |
741 | mt9v022->icd.dev.parent, mt9v022->icd.vdev); | |
742 | soc_camera_video_stop(&mt9v022->icd); | |
743 | } | |
744 | ||
d2653e92 JD |
745 | static int mt9v022_probe(struct i2c_client *client, |
746 | const struct i2c_device_id *did) | |
7397bfbe GL |
747 | { |
748 | struct mt9v022 *mt9v022; | |
749 | struct soc_camera_device *icd; | |
750 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); | |
751 | struct soc_camera_link *icl = client->dev.platform_data; | |
752 | int ret; | |
753 | ||
754 | if (!icl) { | |
755 | dev_err(&client->dev, "MT9V022 driver needs platform data\n"); | |
756 | return -EINVAL; | |
757 | } | |
758 | ||
759 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { | |
760 | dev_warn(&adapter->dev, | |
761 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); | |
762 | return -EIO; | |
763 | } | |
764 | ||
765 | mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL); | |
766 | if (!mt9v022) | |
767 | return -ENOMEM; | |
768 | ||
769 | mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT; | |
770 | mt9v022->client = client; | |
771 | i2c_set_clientdata(client, mt9v022); | |
772 | ||
773 | icd = &mt9v022->icd; | |
7397bfbe GL |
774 | icd->ops = &mt9v022_ops; |
775 | icd->control = &client->dev; | |
776 | icd->x_min = 1; | |
777 | icd->y_min = 4; | |
778 | icd->x_current = 1; | |
779 | icd->y_current = 4; | |
780 | icd->width_min = 48; | |
781 | icd->width_max = 752; | |
782 | icd->height_min = 32; | |
783 | icd->height_max = 480; | |
784 | icd->y_skip_top = 1; | |
785 | icd->iface = icl->bus_id; | |
786 | /* Default datawidth - this is the only width this camera (normally) | |
787 | * supports. It is only with extra logic that it can support | |
788 | * other widths. Therefore it seems to be a sensible default. */ | |
789 | mt9v022->datawidth = 10; | |
790 | ||
791 | ret = bus_switch_request(mt9v022, icl); | |
792 | if (ret) | |
793 | goto eswinit; | |
794 | ||
795 | ret = soc_camera_device_register(icd); | |
796 | if (ret) | |
797 | goto eisdr; | |
798 | ||
799 | return 0; | |
800 | ||
801 | eisdr: | |
802 | bus_switch_release(mt9v022); | |
803 | eswinit: | |
804 | kfree(mt9v022); | |
805 | return ret; | |
806 | } | |
807 | ||
808 | static int mt9v022_remove(struct i2c_client *client) | |
809 | { | |
810 | struct mt9v022 *mt9v022 = i2c_get_clientdata(client); | |
811 | ||
812 | soc_camera_device_unregister(&mt9v022->icd); | |
813 | bus_switch_release(mt9v022); | |
814 | kfree(mt9v022); | |
815 | ||
816 | return 0; | |
817 | } | |
3760f736 JD |
818 | static const struct i2c_device_id mt9v022_id[] = { |
819 | { "mt9v022", 0 }, | |
820 | { } | |
821 | }; | |
822 | MODULE_DEVICE_TABLE(i2c, mt9v022_id); | |
823 | ||
7397bfbe GL |
824 | static struct i2c_driver mt9v022_i2c_driver = { |
825 | .driver = { | |
826 | .name = "mt9v022", | |
827 | }, | |
828 | .probe = mt9v022_probe, | |
829 | .remove = mt9v022_remove, | |
3760f736 | 830 | .id_table = mt9v022_id, |
7397bfbe GL |
831 | }; |
832 | ||
833 | static int __init mt9v022_mod_init(void) | |
834 | { | |
835 | return i2c_add_driver(&mt9v022_i2c_driver); | |
836 | } | |
837 | ||
838 | static void __exit mt9v022_mod_exit(void) | |
839 | { | |
840 | i2c_del_driver(&mt9v022_i2c_driver); | |
841 | } | |
842 | ||
843 | module_init(mt9v022_mod_init); | |
844 | module_exit(mt9v022_mod_exit); | |
845 | ||
846 | MODULE_DESCRIPTION("Micron MT9V022 Camera driver"); | |
847 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); | |
848 | MODULE_LICENSE("GPL"); |