Commit | Line | Data |
---|---|---|
f523dd0d GL |
1 | /* |
2 | * Driver for MT9M001 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/log2.h> | |
15 | ||
979ea1dd | 16 | #include <media/v4l2-subdev.h> |
f523dd0d GL |
17 | #include <media/v4l2-chip-ident.h> |
18 | #include <media/soc_camera.h> | |
19 | ||
f523dd0d | 20 | /* mt9m001 i2c address 0x5d |
979ea1dd GL |
21 | * The platform has to define ctruct i2c_board_info objects and link to them |
22 | * from struct soc_camera_link */ | |
f523dd0d GL |
23 | |
24 | /* mt9m001 selected register addresses */ | |
25 | #define MT9M001_CHIP_VERSION 0x00 | |
26 | #define MT9M001_ROW_START 0x01 | |
27 | #define MT9M001_COLUMN_START 0x02 | |
28 | #define MT9M001_WINDOW_HEIGHT 0x03 | |
29 | #define MT9M001_WINDOW_WIDTH 0x04 | |
30 | #define MT9M001_HORIZONTAL_BLANKING 0x05 | |
31 | #define MT9M001_VERTICAL_BLANKING 0x06 | |
32 | #define MT9M001_OUTPUT_CONTROL 0x07 | |
33 | #define MT9M001_SHUTTER_WIDTH 0x09 | |
34 | #define MT9M001_FRAME_RESTART 0x0b | |
35 | #define MT9M001_SHUTTER_DELAY 0x0c | |
36 | #define MT9M001_RESET 0x0d | |
37 | #define MT9M001_READ_OPTIONS1 0x1e | |
38 | #define MT9M001_READ_OPTIONS2 0x20 | |
39 | #define MT9M001_GLOBAL_GAIN 0x35 | |
40 | #define MT9M001_CHIP_ENABLE 0xF1 | |
41 | ||
6a6c8786 GL |
42 | #define MT9M001_MAX_WIDTH 1280 |
43 | #define MT9M001_MAX_HEIGHT 1024 | |
44 | #define MT9M001_MIN_WIDTH 48 | |
45 | #define MT9M001_MIN_HEIGHT 32 | |
46 | #define MT9M001_COLUMN_SKIP 20 | |
47 | #define MT9M001_ROW_SKIP 12 | |
48 | ||
f523dd0d | 49 | static const struct soc_camera_data_format mt9m001_colour_formats[] = { |
bb55de3b GL |
50 | /* Order important: first natively supported, |
51 | * second supported with a GPIO extender */ | |
f523dd0d | 52 | { |
bb55de3b GL |
53 | .name = "Bayer (sRGB) 10 bit", |
54 | .depth = 10, | |
55 | .fourcc = V4L2_PIX_FMT_SBGGR16, | |
56 | .colorspace = V4L2_COLORSPACE_SRGB, | |
57 | }, { | |
58 | .name = "Bayer (sRGB) 8 bit", | |
59 | .depth = 8, | |
f523dd0d GL |
60 | .fourcc = V4L2_PIX_FMT_SBGGR8, |
61 | .colorspace = V4L2_COLORSPACE_SRGB, | |
62 | } | |
63 | }; | |
64 | ||
65 | static const struct soc_camera_data_format mt9m001_monochrome_formats[] = { | |
bb55de3b | 66 | /* Order important - see above */ |
f523dd0d GL |
67 | { |
68 | .name = "Monochrome 10 bit", | |
69 | .depth = 10, | |
70 | .fourcc = V4L2_PIX_FMT_Y16, | |
71 | }, { | |
72 | .name = "Monochrome 8 bit", | |
73 | .depth = 8, | |
74 | .fourcc = V4L2_PIX_FMT_GREY, | |
75 | }, | |
76 | }; | |
77 | ||
78 | struct mt9m001 { | |
979ea1dd | 79 | struct v4l2_subdev subdev; |
6a6c8786 GL |
80 | struct v4l2_rect rect; /* Sensor window */ |
81 | __u32 fourcc; | |
f523dd0d | 82 | int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */ |
96c75399 GL |
83 | unsigned int gain; |
84 | unsigned int exposure; | |
32536108 | 85 | unsigned short y_skip_top; /* Lines to skip at the top */ |
f523dd0d | 86 | unsigned char autoexposure; |
f523dd0d GL |
87 | }; |
88 | ||
979ea1dd GL |
89 | static struct mt9m001 *to_mt9m001(const struct i2c_client *client) |
90 | { | |
91 | return container_of(i2c_get_clientdata(client), struct mt9m001, subdev); | |
92 | } | |
93 | ||
9538e1c2 | 94 | static int reg_read(struct i2c_client *client, const u8 reg) |
f523dd0d | 95 | { |
f523dd0d GL |
96 | s32 data = i2c_smbus_read_word_data(client, reg); |
97 | return data < 0 ? data : swab16(data); | |
98 | } | |
99 | ||
9538e1c2 | 100 | static int reg_write(struct i2c_client *client, const u8 reg, |
f523dd0d GL |
101 | const u16 data) |
102 | { | |
9538e1c2 | 103 | return i2c_smbus_write_word_data(client, reg, swab16(data)); |
f523dd0d GL |
104 | } |
105 | ||
9538e1c2 | 106 | static int reg_set(struct i2c_client *client, const u8 reg, |
f523dd0d GL |
107 | const u16 data) |
108 | { | |
109 | int ret; | |
110 | ||
9538e1c2 | 111 | ret = reg_read(client, reg); |
f523dd0d GL |
112 | if (ret < 0) |
113 | return ret; | |
9538e1c2 | 114 | return reg_write(client, reg, ret | data); |
f523dd0d GL |
115 | } |
116 | ||
9538e1c2 | 117 | static int reg_clear(struct i2c_client *client, const u8 reg, |
f523dd0d GL |
118 | const u16 data) |
119 | { | |
120 | int ret; | |
121 | ||
9538e1c2 | 122 | ret = reg_read(client, reg); |
f523dd0d GL |
123 | if (ret < 0) |
124 | return ret; | |
9538e1c2 | 125 | return reg_write(client, reg, ret & ~data); |
f523dd0d GL |
126 | } |
127 | ||
a4c56fd8 | 128 | static int mt9m001_init(struct i2c_client *client) |
f523dd0d GL |
129 | { |
130 | int ret; | |
131 | ||
85f8be68 | 132 | dev_dbg(&client->dev, "%s\n", __func__); |
f523dd0d | 133 | |
979ea1dd | 134 | /* |
96c75399 GL |
135 | * We don't know, whether platform provides reset, issue a soft reset |
136 | * too. This returns all registers to their default values. | |
979ea1dd GL |
137 | */ |
138 | ret = reg_write(client, MT9M001_RESET, 1); | |
139 | if (!ret) | |
140 | ret = reg_write(client, MT9M001_RESET, 0); | |
81034663 | 141 | |
11211641 GL |
142 | /* Disable chip, synchronous option update */ |
143 | if (!ret) | |
9538e1c2 | 144 | ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0); |
f523dd0d | 145 | |
11211641 | 146 | return ret; |
f523dd0d GL |
147 | } |
148 | ||
979ea1dd | 149 | static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable) |
f523dd0d | 150 | { |
979ea1dd | 151 | struct i2c_client *client = sd->priv; |
9538e1c2 | 152 | |
979ea1dd GL |
153 | /* Switch to master "normal" mode or stop sensor readout */ |
154 | if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0) | |
f523dd0d GL |
155 | return -EIO; |
156 | return 0; | |
157 | } | |
158 | ||
ad5f2e85 GL |
159 | static int mt9m001_set_bus_param(struct soc_camera_device *icd, |
160 | unsigned long flags) | |
f523dd0d | 161 | { |
40e2e092 | 162 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
36034dc3 | 163 | unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK; |
f523dd0d | 164 | |
36034dc3 SH |
165 | /* Only one width bit may be set */ |
166 | if (!is_power_of_2(width_flag)) | |
167 | return -EINVAL; | |
f523dd0d | 168 | |
36034dc3 SH |
169 | if (icl->set_bus_param) |
170 | return icl->set_bus_param(icl, width_flag); | |
f523dd0d | 171 | |
36034dc3 SH |
172 | /* |
173 | * Without board specific bus width settings we only support the | |
174 | * sensors native bus width | |
175 | */ | |
176 | if (width_flag == SOCAM_DATAWIDTH_10) | |
177 | return 0; | |
f523dd0d | 178 | |
36034dc3 | 179 | return -EINVAL; |
ad5f2e85 GL |
180 | } |
181 | ||
182 | static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd) | |
183 | { | |
40e2e092 | 184 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
bd73b36f | 185 | /* MT9M001 has all capture_format parameters fixed */ |
e951cbf2 | 186 | unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING | |
bd73b36f | 187 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH | |
2d9329f3 | 188 | SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER; |
ad5f2e85 | 189 | |
36034dc3 SH |
190 | if (icl->query_bus_param) |
191 | flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK; | |
192 | else | |
193 | flags |= SOCAM_DATAWIDTH_10; | |
ad5f2e85 | 194 | |
bd73b36f | 195 | return soc_camera_apply_sensor_flags(icl, flags); |
ad5f2e85 GL |
196 | } |
197 | ||
08590b96 | 198 | static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a) |
ad5f2e85 | 199 | { |
08590b96 | 200 | struct i2c_client *client = sd->priv; |
979ea1dd | 201 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
6a6c8786 | 202 | struct v4l2_rect rect = a->c; |
08590b96 | 203 | struct soc_camera_device *icd = client->dev.platform_data; |
ad5f2e85 GL |
204 | int ret; |
205 | const u16 hblank = 9, vblank = 25; | |
96c75399 | 206 | unsigned int total_h; |
ad5f2e85 | 207 | |
6a6c8786 GL |
208 | if (mt9m001->fourcc == V4L2_PIX_FMT_SBGGR8 || |
209 | mt9m001->fourcc == V4L2_PIX_FMT_SBGGR16) | |
210 | /* | |
211 | * Bayer format - even number of rows for simplicity, | |
212 | * but let the user play with the top row. | |
213 | */ | |
214 | rect.height = ALIGN(rect.height, 2); | |
215 | ||
216 | /* Datasheet requirement: see register description */ | |
217 | rect.width = ALIGN(rect.width, 2); | |
218 | rect.left = ALIGN(rect.left, 2); | |
219 | ||
220 | soc_camera_limit_side(&rect.left, &rect.width, | |
221 | MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH); | |
222 | ||
223 | soc_camera_limit_side(&rect.top, &rect.height, | |
224 | MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT); | |
225 | ||
32536108 | 226 | total_h = rect.height + mt9m001->y_skip_top + vblank; |
96c75399 | 227 | |
f523dd0d | 228 | /* Blanking and start values - default... */ |
9538e1c2 | 229 | ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank); |
11211641 | 230 | if (!ret) |
9538e1c2 | 231 | ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank); |
f523dd0d GL |
232 | |
233 | /* The caller provides a supported format, as verified per | |
d8fac217 | 234 | * call to icd->try_fmt() */ |
11211641 | 235 | if (!ret) |
6a6c8786 | 236 | ret = reg_write(client, MT9M001_COLUMN_START, rect.left); |
11211641 | 237 | if (!ret) |
6a6c8786 | 238 | ret = reg_write(client, MT9M001_ROW_START, rect.top); |
11211641 | 239 | if (!ret) |
6a6c8786 | 240 | ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1); |
11211641 | 241 | if (!ret) |
9538e1c2 | 242 | ret = reg_write(client, MT9M001_WINDOW_HEIGHT, |
32536108 | 243 | rect.height + mt9m001->y_skip_top - 1); |
11211641 | 244 | if (!ret && mt9m001->autoexposure) { |
96c75399 | 245 | ret = reg_write(client, MT9M001_SHUTTER_WIDTH, total_h); |
11211641 | 246 | if (!ret) { |
f523dd0d GL |
247 | const struct v4l2_queryctrl *qctrl = |
248 | soc_camera_find_qctrl(icd->ops, | |
249 | V4L2_CID_EXPOSURE); | |
96c75399 GL |
250 | mt9m001->exposure = (524 + (total_h - 1) * |
251 | (qctrl->maximum - qctrl->minimum)) / | |
f523dd0d GL |
252 | 1048 + qctrl->minimum; |
253 | } | |
254 | } | |
255 | ||
6a6c8786 GL |
256 | if (!ret) |
257 | mt9m001->rect = rect; | |
258 | ||
11211641 | 259 | return ret; |
f523dd0d GL |
260 | } |
261 | ||
6a6c8786 GL |
262 | static int mt9m001_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a) |
263 | { | |
264 | struct i2c_client *client = sd->priv; | |
265 | struct mt9m001 *mt9m001 = to_mt9m001(client); | |
266 | ||
267 | a->c = mt9m001->rect; | |
268 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
269 | ||
270 | return 0; | |
271 | } | |
272 | ||
273 | static int mt9m001_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a) | |
274 | { | |
275 | a->bounds.left = MT9M001_COLUMN_SKIP; | |
276 | a->bounds.top = MT9M001_ROW_SKIP; | |
277 | a->bounds.width = MT9M001_MAX_WIDTH; | |
278 | a->bounds.height = MT9M001_MAX_HEIGHT; | |
279 | a->defrect = a->bounds; | |
280 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
281 | a->pixelaspect.numerator = 1; | |
282 | a->pixelaspect.denominator = 1; | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | static int mt9m001_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f) | |
288 | { | |
289 | struct i2c_client *client = sd->priv; | |
290 | struct mt9m001 *mt9m001 = to_mt9m001(client); | |
291 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
292 | ||
293 | pix->width = mt9m001->rect.width; | |
294 | pix->height = mt9m001->rect.height; | |
295 | pix->pixelformat = mt9m001->fourcc; | |
296 | pix->field = V4L2_FIELD_NONE; | |
297 | pix->colorspace = V4L2_COLORSPACE_SRGB; | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
979ea1dd | 302 | static int mt9m001_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f) |
09e231b3 | 303 | { |
979ea1dd | 304 | struct i2c_client *client = sd->priv; |
6a6c8786 GL |
305 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
306 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
08590b96 GL |
307 | struct v4l2_crop a = { |
308 | .c = { | |
6a6c8786 GL |
309 | .left = mt9m001->rect.left, |
310 | .top = mt9m001->rect.top, | |
311 | .width = pix->width, | |
312 | .height = pix->height, | |
08590b96 | 313 | }, |
09e231b3 | 314 | }; |
6a6c8786 | 315 | int ret; |
09e231b3 GL |
316 | |
317 | /* No support for scaling so far, just crop. TODO: use skipping */ | |
6a6c8786 GL |
318 | ret = mt9m001_s_crop(sd, &a); |
319 | if (!ret) { | |
320 | pix->width = mt9m001->rect.width; | |
321 | pix->height = mt9m001->rect.height; | |
322 | mt9m001->fourcc = pix->pixelformat; | |
323 | } | |
324 | ||
325 | return ret; | |
09e231b3 GL |
326 | } |
327 | ||
979ea1dd | 328 | static int mt9m001_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f) |
f523dd0d | 329 | { |
979ea1dd | 330 | struct i2c_client *client = sd->priv; |
32536108 | 331 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
64f5905e GL |
332 | struct v4l2_pix_format *pix = &f->fmt.pix; |
333 | ||
6a6c8786 GL |
334 | v4l_bound_align_image(&pix->width, MT9M001_MIN_WIDTH, |
335 | MT9M001_MAX_WIDTH, 1, | |
32536108 GL |
336 | &pix->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top, |
337 | MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0); | |
6a6c8786 GL |
338 | |
339 | if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8 || | |
340 | pix->pixelformat == V4L2_PIX_FMT_SBGGR16) | |
341 | pix->height = ALIGN(pix->height - 1, 2); | |
f523dd0d GL |
342 | |
343 | return 0; | |
344 | } | |
345 | ||
979ea1dd GL |
346 | static int mt9m001_g_chip_ident(struct v4l2_subdev *sd, |
347 | struct v4l2_dbg_chip_ident *id) | |
f523dd0d | 348 | { |
979ea1dd GL |
349 | struct i2c_client *client = sd->priv; |
350 | struct mt9m001 *mt9m001 = to_mt9m001(client); | |
f523dd0d | 351 | |
aecde8b5 | 352 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
f523dd0d GL |
353 | return -EINVAL; |
354 | ||
40e2e092 | 355 | if (id->match.addr != client->addr) |
f523dd0d GL |
356 | return -ENODEV; |
357 | ||
358 | id->ident = mt9m001->model; | |
359 | id->revision = 0; | |
360 | ||
361 | return 0; | |
362 | } | |
363 | ||
364 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
979ea1dd GL |
365 | static int mt9m001_g_register(struct v4l2_subdev *sd, |
366 | struct v4l2_dbg_register *reg) | |
f523dd0d | 367 | { |
979ea1dd | 368 | struct i2c_client *client = sd->priv; |
f523dd0d | 369 | |
aecde8b5 | 370 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
f523dd0d GL |
371 | return -EINVAL; |
372 | ||
9538e1c2 | 373 | if (reg->match.addr != client->addr) |
f523dd0d GL |
374 | return -ENODEV; |
375 | ||
aecde8b5 | 376 | reg->size = 2; |
9538e1c2 | 377 | reg->val = reg_read(client, reg->reg); |
f523dd0d GL |
378 | |
379 | if (reg->val > 0xffff) | |
380 | return -EIO; | |
381 | ||
382 | return 0; | |
383 | } | |
384 | ||
979ea1dd GL |
385 | static int mt9m001_s_register(struct v4l2_subdev *sd, |
386 | struct v4l2_dbg_register *reg) | |
f523dd0d | 387 | { |
979ea1dd | 388 | struct i2c_client *client = sd->priv; |
f523dd0d | 389 | |
aecde8b5 | 390 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
f523dd0d GL |
391 | return -EINVAL; |
392 | ||
9538e1c2 | 393 | if (reg->match.addr != client->addr) |
f523dd0d GL |
394 | return -ENODEV; |
395 | ||
9538e1c2 | 396 | if (reg_write(client, reg->reg, reg->val) < 0) |
f523dd0d GL |
397 | return -EIO; |
398 | ||
399 | return 0; | |
400 | } | |
401 | #endif | |
402 | ||
4407a463 | 403 | static const struct v4l2_queryctrl mt9m001_controls[] = { |
f523dd0d GL |
404 | { |
405 | .id = V4L2_CID_VFLIP, | |
406 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
407 | .name = "Flip Vertically", | |
408 | .minimum = 0, | |
409 | .maximum = 1, | |
410 | .step = 1, | |
411 | .default_value = 0, | |
412 | }, { | |
413 | .id = V4L2_CID_GAIN, | |
414 | .type = V4L2_CTRL_TYPE_INTEGER, | |
415 | .name = "Gain", | |
416 | .minimum = 0, | |
417 | .maximum = 127, | |
418 | .step = 1, | |
419 | .default_value = 64, | |
420 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
421 | }, { | |
422 | .id = V4L2_CID_EXPOSURE, | |
423 | .type = V4L2_CTRL_TYPE_INTEGER, | |
424 | .name = "Exposure", | |
425 | .minimum = 1, | |
426 | .maximum = 255, | |
427 | .step = 1, | |
428 | .default_value = 255, | |
429 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
430 | }, { | |
431 | .id = V4L2_CID_EXPOSURE_AUTO, | |
432 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
433 | .name = "Automatic Exposure", | |
434 | .minimum = 0, | |
435 | .maximum = 1, | |
436 | .step = 1, | |
437 | .default_value = 1, | |
438 | } | |
439 | }; | |
440 | ||
f523dd0d | 441 | static struct soc_camera_ops mt9m001_ops = { |
ad5f2e85 GL |
442 | .set_bus_param = mt9m001_set_bus_param, |
443 | .query_bus_param = mt9m001_query_bus_param, | |
f523dd0d GL |
444 | .controls = mt9m001_controls, |
445 | .num_controls = ARRAY_SIZE(mt9m001_controls), | |
f523dd0d GL |
446 | }; |
447 | ||
979ea1dd | 448 | static int mt9m001_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
f523dd0d | 449 | { |
979ea1dd GL |
450 | struct i2c_client *client = sd->priv; |
451 | struct mt9m001 *mt9m001 = to_mt9m001(client); | |
f523dd0d GL |
452 | int data; |
453 | ||
454 | switch (ctrl->id) { | |
455 | case V4L2_CID_VFLIP: | |
9538e1c2 | 456 | data = reg_read(client, MT9M001_READ_OPTIONS2); |
f523dd0d GL |
457 | if (data < 0) |
458 | return -EIO; | |
459 | ctrl->value = !!(data & 0x8000); | |
460 | break; | |
461 | case V4L2_CID_EXPOSURE_AUTO: | |
462 | ctrl->value = mt9m001->autoexposure; | |
463 | break; | |
96c75399 GL |
464 | case V4L2_CID_GAIN: |
465 | ctrl->value = mt9m001->gain; | |
466 | break; | |
467 | case V4L2_CID_EXPOSURE: | |
468 | ctrl->value = mt9m001->exposure; | |
469 | break; | |
f523dd0d GL |
470 | } |
471 | return 0; | |
472 | } | |
473 | ||
979ea1dd | 474 | static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
f523dd0d | 475 | { |
979ea1dd GL |
476 | struct i2c_client *client = sd->priv; |
477 | struct mt9m001 *mt9m001 = to_mt9m001(client); | |
478 | struct soc_camera_device *icd = client->dev.platform_data; | |
f523dd0d GL |
479 | const struct v4l2_queryctrl *qctrl; |
480 | int data; | |
481 | ||
482 | qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id); | |
483 | ||
484 | if (!qctrl) | |
485 | return -EINVAL; | |
486 | ||
487 | switch (ctrl->id) { | |
488 | case V4L2_CID_VFLIP: | |
489 | if (ctrl->value) | |
9538e1c2 | 490 | data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000); |
f523dd0d | 491 | else |
9538e1c2 | 492 | data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000); |
f523dd0d GL |
493 | if (data < 0) |
494 | return -EIO; | |
495 | break; | |
496 | case V4L2_CID_GAIN: | |
497 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
498 | return -EINVAL; | |
499 | /* See Datasheet Table 7, Gain settings. */ | |
500 | if (ctrl->value <= qctrl->default_value) { | |
501 | /* Pack it into 0..1 step 0.125, register values 0..8 */ | |
502 | unsigned long range = qctrl->default_value - qctrl->minimum; | |
503 | data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range; | |
504 | ||
85f8be68 | 505 | dev_dbg(&client->dev, "Setting gain %d\n", data); |
9538e1c2 | 506 | data = reg_write(client, MT9M001_GLOBAL_GAIN, data); |
f523dd0d GL |
507 | if (data < 0) |
508 | return -EIO; | |
509 | } else { | |
510 | /* Pack it into 1.125..15 variable step, register values 9..67 */ | |
511 | /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ | |
512 | unsigned long range = qctrl->maximum - qctrl->default_value - 1; | |
513 | unsigned long gain = ((ctrl->value - qctrl->default_value - 1) * | |
514 | 111 + range / 2) / range + 9; | |
515 | ||
516 | if (gain <= 32) | |
517 | data = gain; | |
518 | else if (gain <= 64) | |
519 | data = ((gain - 32) * 16 + 16) / 32 + 80; | |
520 | else | |
521 | data = ((gain - 64) * 7 + 28) / 56 + 96; | |
522 | ||
85f8be68 | 523 | dev_dbg(&client->dev, "Setting gain from %d to %d\n", |
9538e1c2 GL |
524 | reg_read(client, MT9M001_GLOBAL_GAIN), data); |
525 | data = reg_write(client, MT9M001_GLOBAL_GAIN, data); | |
f523dd0d GL |
526 | if (data < 0) |
527 | return -EIO; | |
528 | } | |
529 | ||
530 | /* Success */ | |
96c75399 | 531 | mt9m001->gain = ctrl->value; |
f523dd0d GL |
532 | break; |
533 | case V4L2_CID_EXPOSURE: | |
534 | /* mt9m001 has maximum == default */ | |
535 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
536 | return -EINVAL; | |
537 | else { | |
538 | unsigned long range = qctrl->maximum - qctrl->minimum; | |
539 | unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 + | |
540 | range / 2) / range + 1; | |
541 | ||
85f8be68 GL |
542 | dev_dbg(&client->dev, |
543 | "Setting shutter width from %d to %lu\n", | |
544 | reg_read(client, MT9M001_SHUTTER_WIDTH), | |
545 | shutter); | |
9538e1c2 | 546 | if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0) |
f523dd0d | 547 | return -EIO; |
96c75399 | 548 | mt9m001->exposure = ctrl->value; |
f523dd0d GL |
549 | mt9m001->autoexposure = 0; |
550 | } | |
551 | break; | |
552 | case V4L2_CID_EXPOSURE_AUTO: | |
553 | if (ctrl->value) { | |
554 | const u16 vblank = 25; | |
96c75399 | 555 | unsigned int total_h = mt9m001->rect.height + |
32536108 | 556 | mt9m001->y_skip_top + vblank; |
a0705b07 | 557 | if (reg_write(client, MT9M001_SHUTTER_WIDTH, |
96c75399 | 558 | total_h) < 0) |
f523dd0d GL |
559 | return -EIO; |
560 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); | |
96c75399 GL |
561 | mt9m001->exposure = (524 + (total_h - 1) * |
562 | (qctrl->maximum - qctrl->minimum)) / | |
f523dd0d GL |
563 | 1048 + qctrl->minimum; |
564 | mt9m001->autoexposure = 1; | |
565 | } else | |
566 | mt9m001->autoexposure = 0; | |
567 | break; | |
568 | } | |
569 | return 0; | |
570 | } | |
571 | ||
572 | /* Interface active, can use i2c. If it fails, it can indeed mean, that | |
573 | * this wasn't our capture interface, so, we wait for the right one */ | |
40e2e092 GL |
574 | static int mt9m001_video_probe(struct soc_camera_device *icd, |
575 | struct i2c_client *client) | |
f523dd0d | 576 | { |
979ea1dd | 577 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
40e2e092 | 578 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
f523dd0d | 579 | s32 data; |
36034dc3 | 580 | unsigned long flags; |
a4c56fd8 | 581 | int ret; |
f523dd0d GL |
582 | |
583 | /* We must have a parent by now. And it cannot be a wrong one. | |
584 | * So this entire test is completely redundant. */ | |
585 | if (!icd->dev.parent || | |
586 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) | |
587 | return -ENODEV; | |
588 | ||
589 | /* Enable the chip */ | |
9538e1c2 | 590 | data = reg_write(client, MT9M001_CHIP_ENABLE, 1); |
85f8be68 | 591 | dev_dbg(&client->dev, "write: %d\n", data); |
f523dd0d GL |
592 | |
593 | /* Read out the chip version register */ | |
9538e1c2 | 594 | data = reg_read(client, MT9M001_CHIP_VERSION); |
f523dd0d GL |
595 | |
596 | /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */ | |
597 | switch (data) { | |
598 | case 0x8411: | |
599 | case 0x8421: | |
600 | mt9m001->model = V4L2_IDENT_MT9M001C12ST; | |
26f1b942 | 601 | icd->formats = mt9m001_colour_formats; |
f523dd0d GL |
602 | break; |
603 | case 0x8431: | |
604 | mt9m001->model = V4L2_IDENT_MT9M001C12STM; | |
26f1b942 | 605 | icd->formats = mt9m001_monochrome_formats; |
f523dd0d GL |
606 | break; |
607 | default: | |
85f8be68 | 608 | dev_err(&client->dev, |
f523dd0d | 609 | "No MT9M001 chip detected, register read %x\n", data); |
40e2e092 | 610 | return -ENODEV; |
f523dd0d GL |
611 | } |
612 | ||
36034dc3 SH |
613 | icd->num_formats = 0; |
614 | ||
615 | /* | |
616 | * This is a 10bit sensor, so by default we only allow 10bit. | |
617 | * The platform may support different bus widths due to | |
618 | * different routing of the data lines. | |
619 | */ | |
620 | if (icl->query_bus_param) | |
621 | flags = icl->query_bus_param(icl); | |
622 | else | |
623 | flags = SOCAM_DATAWIDTH_10; | |
624 | ||
625 | if (flags & SOCAM_DATAWIDTH_10) | |
626 | icd->num_formats++; | |
627 | else | |
628 | icd->formats++; | |
629 | ||
630 | if (flags & SOCAM_DATAWIDTH_8) | |
631 | icd->num_formats++; | |
632 | ||
6a6c8786 GL |
633 | mt9m001->fourcc = icd->formats->fourcc; |
634 | ||
85f8be68 | 635 | dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data, |
f523dd0d GL |
636 | data == 0x8431 ? "C12STM" : "C12ST"); |
637 | ||
a4c56fd8 GL |
638 | ret = mt9m001_init(client); |
639 | if (ret < 0) | |
640 | dev_err(&client->dev, "Failed to initialise the camera\n"); | |
641 | ||
96c75399 GL |
642 | /* mt9m001_init() has reset the chip, returning registers to defaults */ |
643 | mt9m001->gain = 64; | |
644 | mt9m001->exposure = 255; | |
645 | ||
a4c56fd8 | 646 | return ret; |
f523dd0d GL |
647 | } |
648 | ||
649 | static void mt9m001_video_remove(struct soc_camera_device *icd) | |
650 | { | |
40e2e092 | 651 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
f523dd0d | 652 | |
6a6c8786 | 653 | dev_dbg(&icd->dev, "Video removed: %p, %p\n", |
a85bdace | 654 | icd->dev.parent, icd->vdev); |
594bb46d GL |
655 | if (icl->free_bus) |
656 | icl->free_bus(icl); | |
f523dd0d GL |
657 | } |
658 | ||
32536108 GL |
659 | static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines) |
660 | { | |
661 | struct i2c_client *client = sd->priv; | |
662 | struct mt9m001 *mt9m001 = to_mt9m001(client); | |
663 | ||
664 | *lines = mt9m001->y_skip_top; | |
665 | ||
666 | return 0; | |
667 | } | |
668 | ||
979ea1dd GL |
669 | static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = { |
670 | .g_ctrl = mt9m001_g_ctrl, | |
671 | .s_ctrl = mt9m001_s_ctrl, | |
672 | .g_chip_ident = mt9m001_g_chip_ident, | |
673 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
674 | .g_register = mt9m001_g_register, | |
675 | .s_register = mt9m001_s_register, | |
676 | #endif | |
677 | }; | |
678 | ||
679 | static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = { | |
680 | .s_stream = mt9m001_s_stream, | |
681 | .s_fmt = mt9m001_s_fmt, | |
6a6c8786 | 682 | .g_fmt = mt9m001_g_fmt, |
979ea1dd | 683 | .try_fmt = mt9m001_try_fmt, |
08590b96 | 684 | .s_crop = mt9m001_s_crop, |
6a6c8786 GL |
685 | .g_crop = mt9m001_g_crop, |
686 | .cropcap = mt9m001_cropcap, | |
979ea1dd GL |
687 | }; |
688 | ||
32536108 GL |
689 | static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = { |
690 | .g_skip_top_lines = mt9m001_g_skip_top_lines, | |
691 | }; | |
692 | ||
979ea1dd GL |
693 | static struct v4l2_subdev_ops mt9m001_subdev_ops = { |
694 | .core = &mt9m001_subdev_core_ops, | |
695 | .video = &mt9m001_subdev_video_ops, | |
32536108 | 696 | .sensor = &mt9m001_subdev_sensor_ops, |
979ea1dd GL |
697 | }; |
698 | ||
d2653e92 JD |
699 | static int mt9m001_probe(struct i2c_client *client, |
700 | const struct i2c_device_id *did) | |
f523dd0d GL |
701 | { |
702 | struct mt9m001 *mt9m001; | |
40e2e092 | 703 | struct soc_camera_device *icd = client->dev.platform_data; |
f523dd0d | 704 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
40e2e092 | 705 | struct soc_camera_link *icl; |
f523dd0d GL |
706 | int ret; |
707 | ||
40e2e092 GL |
708 | if (!icd) { |
709 | dev_err(&client->dev, "MT9M001: missing soc-camera data!\n"); | |
710 | return -EINVAL; | |
711 | } | |
712 | ||
713 | icl = to_soc_camera_link(icd); | |
f523dd0d GL |
714 | if (!icl) { |
715 | dev_err(&client->dev, "MT9M001 driver needs platform data\n"); | |
716 | return -EINVAL; | |
717 | } | |
718 | ||
719 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { | |
720 | dev_warn(&adapter->dev, | |
721 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); | |
722 | return -EIO; | |
723 | } | |
724 | ||
725 | mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL); | |
726 | if (!mt9m001) | |
727 | return -ENOMEM; | |
728 | ||
979ea1dd | 729 | v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops); |
f523dd0d GL |
730 | |
731 | /* Second stage probe - when a capture adapter is there */ | |
a0705b07 | 732 | icd->ops = &mt9m001_ops; |
6a6c8786 | 733 | |
32536108 | 734 | mt9m001->y_skip_top = 0; |
6a6c8786 GL |
735 | mt9m001->rect.left = MT9M001_COLUMN_SKIP; |
736 | mt9m001->rect.top = MT9M001_ROW_SKIP; | |
737 | mt9m001->rect.width = MT9M001_MAX_WIDTH; | |
738 | mt9m001->rect.height = MT9M001_MAX_HEIGHT; | |
739 | ||
f523dd0d GL |
740 | /* Simulated autoexposure. If enabled, we calculate shutter width |
741 | * ourselves in the driver based on vertical blanking and frame width */ | |
742 | mt9m001->autoexposure = 1; | |
743 | ||
40e2e092 GL |
744 | ret = mt9m001_video_probe(icd, client); |
745 | if (ret) { | |
746 | icd->ops = NULL; | |
747 | i2c_set_clientdata(client, NULL); | |
748 | kfree(mt9m001); | |
749 | } | |
f523dd0d | 750 | |
f523dd0d GL |
751 | return ret; |
752 | } | |
753 | ||
754 | static int mt9m001_remove(struct i2c_client *client) | |
755 | { | |
979ea1dd | 756 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
40e2e092 | 757 | struct soc_camera_device *icd = client->dev.platform_data; |
f523dd0d | 758 | |
40e2e092 GL |
759 | icd->ops = NULL; |
760 | mt9m001_video_remove(icd); | |
761 | i2c_set_clientdata(client, NULL); | |
762 | client->driver = NULL; | |
f523dd0d GL |
763 | kfree(mt9m001); |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
3760f736 JD |
768 | static const struct i2c_device_id mt9m001_id[] = { |
769 | { "mt9m001", 0 }, | |
770 | { } | |
771 | }; | |
772 | MODULE_DEVICE_TABLE(i2c, mt9m001_id); | |
773 | ||
f523dd0d GL |
774 | static struct i2c_driver mt9m001_i2c_driver = { |
775 | .driver = { | |
776 | .name = "mt9m001", | |
777 | }, | |
778 | .probe = mt9m001_probe, | |
779 | .remove = mt9m001_remove, | |
3760f736 | 780 | .id_table = mt9m001_id, |
f523dd0d GL |
781 | }; |
782 | ||
783 | static int __init mt9m001_mod_init(void) | |
784 | { | |
785 | return i2c_add_driver(&mt9m001_i2c_driver); | |
786 | } | |
787 | ||
788 | static void __exit mt9m001_mod_exit(void) | |
789 | { | |
790 | i2c_del_driver(&mt9m001_i2c_driver); | |
791 | } | |
792 | ||
793 | module_init(mt9m001_mod_init); | |
794 | module_exit(mt9m001_mod_exit); | |
795 | ||
796 | MODULE_DESCRIPTION("Micron MT9M001 Camera driver"); | |
797 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); | |
798 | MODULE_LICENSE("GPL"); |