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 | ||
42 | static const struct soc_camera_data_format mt9m001_colour_formats[] = { | |
bb55de3b GL |
43 | /* Order important: first natively supported, |
44 | * second supported with a GPIO extender */ | |
f523dd0d | 45 | { |
bb55de3b GL |
46 | .name = "Bayer (sRGB) 10 bit", |
47 | .depth = 10, | |
48 | .fourcc = V4L2_PIX_FMT_SBGGR16, | |
49 | .colorspace = V4L2_COLORSPACE_SRGB, | |
50 | }, { | |
51 | .name = "Bayer (sRGB) 8 bit", | |
52 | .depth = 8, | |
f523dd0d GL |
53 | .fourcc = V4L2_PIX_FMT_SBGGR8, |
54 | .colorspace = V4L2_COLORSPACE_SRGB, | |
55 | } | |
56 | }; | |
57 | ||
58 | static const struct soc_camera_data_format mt9m001_monochrome_formats[] = { | |
bb55de3b | 59 | /* Order important - see above */ |
f523dd0d GL |
60 | { |
61 | .name = "Monochrome 10 bit", | |
62 | .depth = 10, | |
63 | .fourcc = V4L2_PIX_FMT_Y16, | |
64 | }, { | |
65 | .name = "Monochrome 8 bit", | |
66 | .depth = 8, | |
67 | .fourcc = V4L2_PIX_FMT_GREY, | |
68 | }, | |
69 | }; | |
70 | ||
71 | struct mt9m001 { | |
979ea1dd | 72 | struct v4l2_subdev subdev; |
f523dd0d | 73 | int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */ |
f523dd0d | 74 | unsigned char autoexposure; |
f523dd0d GL |
75 | }; |
76 | ||
979ea1dd GL |
77 | static struct mt9m001 *to_mt9m001(const struct i2c_client *client) |
78 | { | |
79 | return container_of(i2c_get_clientdata(client), struct mt9m001, subdev); | |
80 | } | |
81 | ||
9538e1c2 | 82 | static int reg_read(struct i2c_client *client, const u8 reg) |
f523dd0d | 83 | { |
f523dd0d GL |
84 | s32 data = i2c_smbus_read_word_data(client, reg); |
85 | return data < 0 ? data : swab16(data); | |
86 | } | |
87 | ||
9538e1c2 | 88 | static int reg_write(struct i2c_client *client, const u8 reg, |
f523dd0d GL |
89 | const u16 data) |
90 | { | |
9538e1c2 | 91 | return i2c_smbus_write_word_data(client, reg, swab16(data)); |
f523dd0d GL |
92 | } |
93 | ||
9538e1c2 | 94 | static int reg_set(struct i2c_client *client, const u8 reg, |
f523dd0d GL |
95 | const u16 data) |
96 | { | |
97 | int ret; | |
98 | ||
9538e1c2 | 99 | ret = reg_read(client, reg); |
f523dd0d GL |
100 | if (ret < 0) |
101 | return ret; | |
9538e1c2 | 102 | return reg_write(client, reg, ret | data); |
f523dd0d GL |
103 | } |
104 | ||
9538e1c2 | 105 | static int reg_clear(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 | ||
116 | static int mt9m001_init(struct soc_camera_device *icd) | |
117 | { | |
40e2e092 | 118 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
f523dd0d GL |
119 | int ret; |
120 | ||
40e2e092 | 121 | dev_dbg(&icd->dev, "%s\n", __func__); |
f523dd0d | 122 | |
979ea1dd GL |
123 | /* |
124 | * We don't know, whether platform provides reset, | |
125 | * issue a soft reset too | |
126 | */ | |
127 | ret = reg_write(client, MT9M001_RESET, 1); | |
128 | if (!ret) | |
129 | ret = reg_write(client, MT9M001_RESET, 0); | |
81034663 | 130 | |
11211641 GL |
131 | /* Disable chip, synchronous option update */ |
132 | if (!ret) | |
9538e1c2 | 133 | ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0); |
f523dd0d | 134 | |
11211641 | 135 | return ret; |
f523dd0d GL |
136 | } |
137 | ||
138 | static int mt9m001_release(struct soc_camera_device *icd) | |
139 | { | |
40e2e092 | 140 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
81034663 | 141 | |
f523dd0d | 142 | /* Disable the chip */ |
9538e1c2 | 143 | reg_write(client, MT9M001_OUTPUT_CONTROL, 0); |
81034663 | 144 | |
f523dd0d GL |
145 | return 0; |
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 GL |
199 | struct v4l2_rect *rect = &a->c; |
200 | struct i2c_client *client = sd->priv; | |
979ea1dd | 201 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
08590b96 | 202 | struct soc_camera_device *icd = client->dev.platform_data; |
ad5f2e85 GL |
203 | int ret; |
204 | const u16 hblank = 9, vblank = 25; | |
205 | ||
f523dd0d | 206 | /* Blanking and start values - default... */ |
9538e1c2 | 207 | ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank); |
11211641 | 208 | if (!ret) |
9538e1c2 | 209 | ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank); |
f523dd0d GL |
210 | |
211 | /* The caller provides a supported format, as verified per | |
d8fac217 | 212 | * call to icd->try_fmt() */ |
11211641 | 213 | if (!ret) |
9538e1c2 | 214 | ret = reg_write(client, MT9M001_COLUMN_START, rect->left); |
11211641 | 215 | if (!ret) |
9538e1c2 | 216 | ret = reg_write(client, MT9M001_ROW_START, rect->top); |
11211641 | 217 | if (!ret) |
9538e1c2 | 218 | ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect->width - 1); |
11211641 | 219 | if (!ret) |
9538e1c2 | 220 | ret = reg_write(client, MT9M001_WINDOW_HEIGHT, |
f523dd0d | 221 | rect->height + icd->y_skip_top - 1); |
11211641 | 222 | if (!ret && mt9m001->autoexposure) { |
9538e1c2 | 223 | ret = reg_write(client, MT9M001_SHUTTER_WIDTH, |
f523dd0d | 224 | rect->height + icd->y_skip_top + vblank); |
11211641 | 225 | if (!ret) { |
f523dd0d GL |
226 | const struct v4l2_queryctrl *qctrl = |
227 | soc_camera_find_qctrl(icd->ops, | |
228 | V4L2_CID_EXPOSURE); | |
229 | icd->exposure = (524 + (rect->height + icd->y_skip_top + | |
230 | vblank - 1) * | |
231 | (qctrl->maximum - qctrl->minimum)) / | |
232 | 1048 + qctrl->minimum; | |
233 | } | |
234 | } | |
235 | ||
11211641 | 236 | return ret; |
f523dd0d GL |
237 | } |
238 | ||
979ea1dd | 239 | static int mt9m001_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f) |
09e231b3 | 240 | { |
979ea1dd GL |
241 | struct i2c_client *client = sd->priv; |
242 | struct soc_camera_device *icd = client->dev.platform_data; | |
08590b96 GL |
243 | struct v4l2_crop a = { |
244 | .c = { | |
245 | .left = icd->rect_current.left, | |
246 | .top = icd->rect_current.top, | |
247 | .width = f->fmt.pix.width, | |
248 | .height = f->fmt.pix.height, | |
249 | }, | |
09e231b3 GL |
250 | }; |
251 | ||
252 | /* No support for scaling so far, just crop. TODO: use skipping */ | |
08590b96 | 253 | return mt9m001_s_crop(sd, &a); |
09e231b3 GL |
254 | } |
255 | ||
979ea1dd | 256 | static int mt9m001_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f) |
f523dd0d | 257 | { |
979ea1dd GL |
258 | struct i2c_client *client = sd->priv; |
259 | struct soc_camera_device *icd = client->dev.platform_data; | |
64f5905e GL |
260 | struct v4l2_pix_format *pix = &f->fmt.pix; |
261 | ||
653dc59b TP |
262 | v4l_bound_align_image(&pix->width, 48, 1280, 1, |
263 | &pix->height, 32 + icd->y_skip_top, | |
264 | 1024 + icd->y_skip_top, 0, 0); | |
f523dd0d GL |
265 | |
266 | return 0; | |
267 | } | |
268 | ||
979ea1dd GL |
269 | static int mt9m001_g_chip_ident(struct v4l2_subdev *sd, |
270 | struct v4l2_dbg_chip_ident *id) | |
f523dd0d | 271 | { |
979ea1dd GL |
272 | struct i2c_client *client = sd->priv; |
273 | struct mt9m001 *mt9m001 = to_mt9m001(client); | |
f523dd0d | 274 | |
aecde8b5 | 275 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
f523dd0d GL |
276 | return -EINVAL; |
277 | ||
40e2e092 | 278 | if (id->match.addr != client->addr) |
f523dd0d GL |
279 | return -ENODEV; |
280 | ||
281 | id->ident = mt9m001->model; | |
282 | id->revision = 0; | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
979ea1dd GL |
288 | static int mt9m001_g_register(struct v4l2_subdev *sd, |
289 | struct v4l2_dbg_register *reg) | |
f523dd0d | 290 | { |
979ea1dd | 291 | struct i2c_client *client = sd->priv; |
f523dd0d | 292 | |
aecde8b5 | 293 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
f523dd0d GL |
294 | return -EINVAL; |
295 | ||
9538e1c2 | 296 | if (reg->match.addr != client->addr) |
f523dd0d GL |
297 | return -ENODEV; |
298 | ||
aecde8b5 | 299 | reg->size = 2; |
9538e1c2 | 300 | reg->val = reg_read(client, reg->reg); |
f523dd0d GL |
301 | |
302 | if (reg->val > 0xffff) | |
303 | return -EIO; | |
304 | ||
305 | return 0; | |
306 | } | |
307 | ||
979ea1dd GL |
308 | static int mt9m001_s_register(struct v4l2_subdev *sd, |
309 | struct v4l2_dbg_register *reg) | |
f523dd0d | 310 | { |
979ea1dd | 311 | struct i2c_client *client = sd->priv; |
f523dd0d | 312 | |
aecde8b5 | 313 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
f523dd0d GL |
314 | return -EINVAL; |
315 | ||
9538e1c2 | 316 | if (reg->match.addr != client->addr) |
f523dd0d GL |
317 | return -ENODEV; |
318 | ||
9538e1c2 | 319 | if (reg_write(client, reg->reg, reg->val) < 0) |
f523dd0d GL |
320 | return -EIO; |
321 | ||
322 | return 0; | |
323 | } | |
324 | #endif | |
325 | ||
4407a463 | 326 | static const struct v4l2_queryctrl mt9m001_controls[] = { |
f523dd0d GL |
327 | { |
328 | .id = V4L2_CID_VFLIP, | |
329 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
330 | .name = "Flip Vertically", | |
331 | .minimum = 0, | |
332 | .maximum = 1, | |
333 | .step = 1, | |
334 | .default_value = 0, | |
335 | }, { | |
336 | .id = V4L2_CID_GAIN, | |
337 | .type = V4L2_CTRL_TYPE_INTEGER, | |
338 | .name = "Gain", | |
339 | .minimum = 0, | |
340 | .maximum = 127, | |
341 | .step = 1, | |
342 | .default_value = 64, | |
343 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
344 | }, { | |
345 | .id = V4L2_CID_EXPOSURE, | |
346 | .type = V4L2_CTRL_TYPE_INTEGER, | |
347 | .name = "Exposure", | |
348 | .minimum = 1, | |
349 | .maximum = 255, | |
350 | .step = 1, | |
351 | .default_value = 255, | |
352 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
353 | }, { | |
354 | .id = V4L2_CID_EXPOSURE_AUTO, | |
355 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
356 | .name = "Automatic Exposure", | |
357 | .minimum = 0, | |
358 | .maximum = 1, | |
359 | .step = 1, | |
360 | .default_value = 1, | |
361 | } | |
362 | }; | |
363 | ||
f523dd0d | 364 | static struct soc_camera_ops mt9m001_ops = { |
f523dd0d GL |
365 | .init = mt9m001_init, |
366 | .release = mt9m001_release, | |
ad5f2e85 GL |
367 | .set_bus_param = mt9m001_set_bus_param, |
368 | .query_bus_param = mt9m001_query_bus_param, | |
f523dd0d GL |
369 | .controls = mt9m001_controls, |
370 | .num_controls = ARRAY_SIZE(mt9m001_controls), | |
f523dd0d GL |
371 | }; |
372 | ||
979ea1dd | 373 | static int mt9m001_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
f523dd0d | 374 | { |
979ea1dd GL |
375 | struct i2c_client *client = sd->priv; |
376 | struct mt9m001 *mt9m001 = to_mt9m001(client); | |
f523dd0d GL |
377 | int data; |
378 | ||
379 | switch (ctrl->id) { | |
380 | case V4L2_CID_VFLIP: | |
9538e1c2 | 381 | data = reg_read(client, MT9M001_READ_OPTIONS2); |
f523dd0d GL |
382 | if (data < 0) |
383 | return -EIO; | |
384 | ctrl->value = !!(data & 0x8000); | |
385 | break; | |
386 | case V4L2_CID_EXPOSURE_AUTO: | |
387 | ctrl->value = mt9m001->autoexposure; | |
388 | break; | |
389 | } | |
390 | return 0; | |
391 | } | |
392 | ||
979ea1dd | 393 | static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
f523dd0d | 394 | { |
979ea1dd GL |
395 | struct i2c_client *client = sd->priv; |
396 | struct mt9m001 *mt9m001 = to_mt9m001(client); | |
397 | struct soc_camera_device *icd = client->dev.platform_data; | |
f523dd0d GL |
398 | const struct v4l2_queryctrl *qctrl; |
399 | int data; | |
400 | ||
401 | qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id); | |
402 | ||
403 | if (!qctrl) | |
404 | return -EINVAL; | |
405 | ||
406 | switch (ctrl->id) { | |
407 | case V4L2_CID_VFLIP: | |
408 | if (ctrl->value) | |
9538e1c2 | 409 | data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000); |
f523dd0d | 410 | else |
9538e1c2 | 411 | data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000); |
f523dd0d GL |
412 | if (data < 0) |
413 | return -EIO; | |
414 | break; | |
415 | case V4L2_CID_GAIN: | |
416 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
417 | return -EINVAL; | |
418 | /* See Datasheet Table 7, Gain settings. */ | |
419 | if (ctrl->value <= qctrl->default_value) { | |
420 | /* Pack it into 0..1 step 0.125, register values 0..8 */ | |
421 | unsigned long range = qctrl->default_value - qctrl->minimum; | |
422 | data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range; | |
423 | ||
424 | dev_dbg(&icd->dev, "Setting gain %d\n", data); | |
9538e1c2 | 425 | data = reg_write(client, MT9M001_GLOBAL_GAIN, data); |
f523dd0d GL |
426 | if (data < 0) |
427 | return -EIO; | |
428 | } else { | |
429 | /* Pack it into 1.125..15 variable step, register values 9..67 */ | |
430 | /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ | |
431 | unsigned long range = qctrl->maximum - qctrl->default_value - 1; | |
432 | unsigned long gain = ((ctrl->value - qctrl->default_value - 1) * | |
433 | 111 + range / 2) / range + 9; | |
434 | ||
435 | if (gain <= 32) | |
436 | data = gain; | |
437 | else if (gain <= 64) | |
438 | data = ((gain - 32) * 16 + 16) / 32 + 80; | |
439 | else | |
440 | data = ((gain - 64) * 7 + 28) / 56 + 96; | |
441 | ||
442 | dev_dbg(&icd->dev, "Setting gain from %d to %d\n", | |
9538e1c2 GL |
443 | reg_read(client, MT9M001_GLOBAL_GAIN), data); |
444 | data = reg_write(client, MT9M001_GLOBAL_GAIN, data); | |
f523dd0d GL |
445 | if (data < 0) |
446 | return -EIO; | |
447 | } | |
448 | ||
449 | /* Success */ | |
450 | icd->gain = ctrl->value; | |
451 | break; | |
452 | case V4L2_CID_EXPOSURE: | |
453 | /* mt9m001 has maximum == default */ | |
454 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
455 | return -EINVAL; | |
456 | else { | |
457 | unsigned long range = qctrl->maximum - qctrl->minimum; | |
458 | unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 + | |
459 | range / 2) / range + 1; | |
460 | ||
461 | dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n", | |
9538e1c2 GL |
462 | reg_read(client, MT9M001_SHUTTER_WIDTH), shutter); |
463 | if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0) | |
f523dd0d GL |
464 | return -EIO; |
465 | icd->exposure = ctrl->value; | |
466 | mt9m001->autoexposure = 0; | |
467 | } | |
468 | break; | |
469 | case V4L2_CID_EXPOSURE_AUTO: | |
470 | if (ctrl->value) { | |
471 | const u16 vblank = 25; | |
a0705b07 GL |
472 | if (reg_write(client, MT9M001_SHUTTER_WIDTH, |
473 | icd->rect_current.height + | |
f523dd0d GL |
474 | icd->y_skip_top + vblank) < 0) |
475 | return -EIO; | |
476 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); | |
a0705b07 GL |
477 | icd->exposure = (524 + (icd->rect_current.height + |
478 | icd->y_skip_top + vblank - 1) * | |
f523dd0d GL |
479 | (qctrl->maximum - qctrl->minimum)) / |
480 | 1048 + qctrl->minimum; | |
481 | mt9m001->autoexposure = 1; | |
482 | } else | |
483 | mt9m001->autoexposure = 0; | |
484 | break; | |
485 | } | |
486 | return 0; | |
487 | } | |
488 | ||
489 | /* Interface active, can use i2c. If it fails, it can indeed mean, that | |
490 | * this wasn't our capture interface, so, we wait for the right one */ | |
40e2e092 GL |
491 | static int mt9m001_video_probe(struct soc_camera_device *icd, |
492 | struct i2c_client *client) | |
f523dd0d | 493 | { |
979ea1dd | 494 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
40e2e092 | 495 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
f523dd0d | 496 | s32 data; |
36034dc3 | 497 | unsigned long flags; |
f523dd0d GL |
498 | |
499 | /* We must have a parent by now. And it cannot be a wrong one. | |
500 | * So this entire test is completely redundant. */ | |
501 | if (!icd->dev.parent || | |
502 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) | |
503 | return -ENODEV; | |
504 | ||
505 | /* Enable the chip */ | |
9538e1c2 | 506 | data = reg_write(client, MT9M001_CHIP_ENABLE, 1); |
f523dd0d GL |
507 | dev_dbg(&icd->dev, "write: %d\n", data); |
508 | ||
509 | /* Read out the chip version register */ | |
9538e1c2 | 510 | data = reg_read(client, MT9M001_CHIP_VERSION); |
f523dd0d GL |
511 | |
512 | /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */ | |
513 | switch (data) { | |
514 | case 0x8411: | |
515 | case 0x8421: | |
516 | mt9m001->model = V4L2_IDENT_MT9M001C12ST; | |
26f1b942 | 517 | icd->formats = mt9m001_colour_formats; |
f523dd0d GL |
518 | break; |
519 | case 0x8431: | |
520 | mt9m001->model = V4L2_IDENT_MT9M001C12STM; | |
26f1b942 | 521 | icd->formats = mt9m001_monochrome_formats; |
f523dd0d GL |
522 | break; |
523 | default: | |
f523dd0d GL |
524 | dev_err(&icd->dev, |
525 | "No MT9M001 chip detected, register read %x\n", data); | |
40e2e092 | 526 | return -ENODEV; |
f523dd0d GL |
527 | } |
528 | ||
36034dc3 SH |
529 | icd->num_formats = 0; |
530 | ||
531 | /* | |
532 | * This is a 10bit sensor, so by default we only allow 10bit. | |
533 | * The platform may support different bus widths due to | |
534 | * different routing of the data lines. | |
535 | */ | |
536 | if (icl->query_bus_param) | |
537 | flags = icl->query_bus_param(icl); | |
538 | else | |
539 | flags = SOCAM_DATAWIDTH_10; | |
540 | ||
541 | if (flags & SOCAM_DATAWIDTH_10) | |
542 | icd->num_formats++; | |
543 | else | |
544 | icd->formats++; | |
545 | ||
546 | if (flags & SOCAM_DATAWIDTH_8) | |
547 | icd->num_formats++; | |
548 | ||
f523dd0d GL |
549 | dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data, |
550 | data == 0x8431 ? "C12STM" : "C12ST"); | |
551 | ||
f523dd0d | 552 | return 0; |
f523dd0d GL |
553 | } |
554 | ||
555 | static void mt9m001_video_remove(struct soc_camera_device *icd) | |
556 | { | |
40e2e092 GL |
557 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
558 | struct soc_camera_link *icl = to_soc_camera_link(icd); | |
f523dd0d | 559 | |
40e2e092 | 560 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", client->addr, |
a85bdace | 561 | icd->dev.parent, icd->vdev); |
594bb46d GL |
562 | if (icl->free_bus) |
563 | icl->free_bus(icl); | |
f523dd0d GL |
564 | } |
565 | ||
979ea1dd GL |
566 | static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = { |
567 | .g_ctrl = mt9m001_g_ctrl, | |
568 | .s_ctrl = mt9m001_s_ctrl, | |
569 | .g_chip_ident = mt9m001_g_chip_ident, | |
570 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
571 | .g_register = mt9m001_g_register, | |
572 | .s_register = mt9m001_s_register, | |
573 | #endif | |
574 | }; | |
575 | ||
576 | static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = { | |
577 | .s_stream = mt9m001_s_stream, | |
578 | .s_fmt = mt9m001_s_fmt, | |
579 | .try_fmt = mt9m001_try_fmt, | |
08590b96 | 580 | .s_crop = mt9m001_s_crop, |
979ea1dd GL |
581 | }; |
582 | ||
583 | static struct v4l2_subdev_ops mt9m001_subdev_ops = { | |
584 | .core = &mt9m001_subdev_core_ops, | |
585 | .video = &mt9m001_subdev_video_ops, | |
586 | }; | |
587 | ||
d2653e92 JD |
588 | static int mt9m001_probe(struct i2c_client *client, |
589 | const struct i2c_device_id *did) | |
f523dd0d GL |
590 | { |
591 | struct mt9m001 *mt9m001; | |
40e2e092 | 592 | struct soc_camera_device *icd = client->dev.platform_data; |
f523dd0d | 593 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
40e2e092 | 594 | struct soc_camera_link *icl; |
f523dd0d GL |
595 | int ret; |
596 | ||
40e2e092 GL |
597 | if (!icd) { |
598 | dev_err(&client->dev, "MT9M001: missing soc-camera data!\n"); | |
599 | return -EINVAL; | |
600 | } | |
601 | ||
602 | icl = to_soc_camera_link(icd); | |
f523dd0d GL |
603 | if (!icl) { |
604 | dev_err(&client->dev, "MT9M001 driver needs platform data\n"); | |
605 | return -EINVAL; | |
606 | } | |
607 | ||
608 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { | |
609 | dev_warn(&adapter->dev, | |
610 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); | |
611 | return -EIO; | |
612 | } | |
613 | ||
614 | mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL); | |
615 | if (!mt9m001) | |
616 | return -ENOMEM; | |
617 | ||
979ea1dd | 618 | v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops); |
f523dd0d GL |
619 | |
620 | /* Second stage probe - when a capture adapter is there */ | |
a0705b07 GL |
621 | icd->ops = &mt9m001_ops; |
622 | icd->rect_max.left = 20; | |
623 | icd->rect_max.top = 12; | |
624 | icd->rect_max.width = 1280; | |
625 | icd->rect_max.height = 1024; | |
626 | icd->rect_current.left = 20; | |
627 | icd->rect_current.top = 12; | |
628 | icd->width_min = 48; | |
629 | icd->height_min = 32; | |
630 | icd->y_skip_top = 1; | |
f523dd0d GL |
631 | /* Simulated autoexposure. If enabled, we calculate shutter width |
632 | * ourselves in the driver based on vertical blanking and frame width */ | |
633 | mt9m001->autoexposure = 1; | |
634 | ||
40e2e092 GL |
635 | ret = mt9m001_video_probe(icd, client); |
636 | if (ret) { | |
637 | icd->ops = NULL; | |
638 | i2c_set_clientdata(client, NULL); | |
639 | kfree(mt9m001); | |
640 | } | |
f523dd0d | 641 | |
f523dd0d GL |
642 | return ret; |
643 | } | |
644 | ||
645 | static int mt9m001_remove(struct i2c_client *client) | |
646 | { | |
979ea1dd | 647 | struct mt9m001 *mt9m001 = to_mt9m001(client); |
40e2e092 | 648 | struct soc_camera_device *icd = client->dev.platform_data; |
f523dd0d | 649 | |
40e2e092 GL |
650 | icd->ops = NULL; |
651 | mt9m001_video_remove(icd); | |
652 | i2c_set_clientdata(client, NULL); | |
653 | client->driver = NULL; | |
f523dd0d GL |
654 | kfree(mt9m001); |
655 | ||
656 | return 0; | |
657 | } | |
658 | ||
3760f736 JD |
659 | static const struct i2c_device_id mt9m001_id[] = { |
660 | { "mt9m001", 0 }, | |
661 | { } | |
662 | }; | |
663 | MODULE_DEVICE_TABLE(i2c, mt9m001_id); | |
664 | ||
f523dd0d GL |
665 | static struct i2c_driver mt9m001_i2c_driver = { |
666 | .driver = { | |
667 | .name = "mt9m001", | |
668 | }, | |
669 | .probe = mt9m001_probe, | |
670 | .remove = mt9m001_remove, | |
3760f736 | 671 | .id_table = mt9m001_id, |
f523dd0d GL |
672 | }; |
673 | ||
674 | static int __init mt9m001_mod_init(void) | |
675 | { | |
676 | return i2c_add_driver(&mt9m001_i2c_driver); | |
677 | } | |
678 | ||
679 | static void __exit mt9m001_mod_exit(void) | |
680 | { | |
681 | i2c_del_driver(&mt9m001_i2c_driver); | |
682 | } | |
683 | ||
684 | module_init(mt9m001_mod_init); | |
685 | module_exit(mt9m001_mod_exit); | |
686 | ||
687 | MODULE_DESCRIPTION("Micron MT9M001 Camera driver"); | |
688 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); | |
689 | MODULE_LICENSE("GPL"); |