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