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