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 | ||
16 | #include <media/v4l2-common.h> | |
17 | #include <media/v4l2-chip-ident.h> | |
18 | #include <media/soc_camera.h> | |
19 | ||
f523dd0d GL |
20 | /* mt9m001 i2c address 0x5d |
21 | * The platform has to define i2c_board_info | |
22 | * and call i2c_register_board_info() */ | |
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 { | |
f523dd0d | 72 | int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */ |
f523dd0d | 73 | unsigned char autoexposure; |
f523dd0d GL |
74 | }; |
75 | ||
9538e1c2 | 76 | static int reg_read(struct i2c_client *client, const u8 reg) |
f523dd0d | 77 | { |
f523dd0d GL |
78 | s32 data = i2c_smbus_read_word_data(client, reg); |
79 | return data < 0 ? data : swab16(data); | |
80 | } | |
81 | ||
9538e1c2 | 82 | static int reg_write(struct i2c_client *client, const u8 reg, |
f523dd0d GL |
83 | const u16 data) |
84 | { | |
9538e1c2 | 85 | return i2c_smbus_write_word_data(client, reg, swab16(data)); |
f523dd0d GL |
86 | } |
87 | ||
9538e1c2 | 88 | static int reg_set(struct i2c_client *client, const u8 reg, |
f523dd0d GL |
89 | const u16 data) |
90 | { | |
91 | int ret; | |
92 | ||
9538e1c2 | 93 | ret = reg_read(client, reg); |
f523dd0d GL |
94 | if (ret < 0) |
95 | return ret; | |
9538e1c2 | 96 | return reg_write(client, reg, ret | data); |
f523dd0d GL |
97 | } |
98 | ||
9538e1c2 | 99 | static int reg_clear(struct i2c_client *client, const u8 reg, |
f523dd0d GL |
100 | const u16 data) |
101 | { | |
102 | int ret; | |
103 | ||
9538e1c2 | 104 | ret = reg_read(client, reg); |
f523dd0d GL |
105 | if (ret < 0) |
106 | return ret; | |
9538e1c2 | 107 | return reg_write(client, reg, ret & ~data); |
f523dd0d GL |
108 | } |
109 | ||
110 | static int mt9m001_init(struct soc_camera_device *icd) | |
111 | { | |
40e2e092 GL |
112 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
113 | struct soc_camera_link *icl = to_soc_camera_link(icd); | |
f523dd0d GL |
114 | int ret; |
115 | ||
40e2e092 | 116 | dev_dbg(&icd->dev, "%s\n", __func__); |
f523dd0d | 117 | |
81034663 | 118 | if (icl->power) { |
9538e1c2 | 119 | ret = icl->power(&client->dev, 1); |
81034663 SH |
120 | if (ret < 0) { |
121 | dev_err(icd->vdev->parent, | |
122 | "Platform failed to power-on the camera.\n"); | |
123 | return ret; | |
124 | } | |
125 | } | |
126 | ||
127 | /* The camera could have been already on, we reset it additionally */ | |
128 | if (icl->reset) | |
9538e1c2 | 129 | ret = icl->reset(&client->dev); |
81034663 SH |
130 | else |
131 | ret = -ENODEV; | |
132 | ||
133 | if (ret < 0) { | |
134 | /* Either no platform reset, or platform reset failed */ | |
9538e1c2 | 135 | ret = reg_write(client, MT9M001_RESET, 1); |
81034663 | 136 | if (!ret) |
9538e1c2 | 137 | ret = reg_write(client, MT9M001_RESET, 0); |
81034663 | 138 | } |
11211641 GL |
139 | /* Disable chip, synchronous option update */ |
140 | if (!ret) | |
9538e1c2 | 141 | ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0); |
f523dd0d | 142 | |
11211641 | 143 | return ret; |
f523dd0d GL |
144 | } |
145 | ||
146 | static int mt9m001_release(struct soc_camera_device *icd) | |
147 | { | |
40e2e092 GL |
148 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
149 | struct soc_camera_link *icl = to_soc_camera_link(icd); | |
81034663 | 150 | |
f523dd0d | 151 | /* Disable the chip */ |
9538e1c2 | 152 | reg_write(client, MT9M001_OUTPUT_CONTROL, 0); |
81034663 SH |
153 | |
154 | if (icl->power) | |
9538e1c2 | 155 | icl->power(&client->dev, 0); |
81034663 | 156 | |
f523dd0d GL |
157 | return 0; |
158 | } | |
159 | ||
160 | static int mt9m001_start_capture(struct soc_camera_device *icd) | |
161 | { | |
40e2e092 | 162 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
9538e1c2 | 163 | |
f523dd0d | 164 | /* Switch to master "normal" mode */ |
9538e1c2 | 165 | if (reg_write(client, MT9M001_OUTPUT_CONTROL, 2) < 0) |
f523dd0d GL |
166 | return -EIO; |
167 | return 0; | |
168 | } | |
169 | ||
170 | static int mt9m001_stop_capture(struct soc_camera_device *icd) | |
171 | { | |
40e2e092 | 172 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
9538e1c2 | 173 | |
f523dd0d | 174 | /* Stop sensor readout */ |
9538e1c2 | 175 | if (reg_write(client, MT9M001_OUTPUT_CONTROL, 0) < 0) |
f523dd0d GL |
176 | return -EIO; |
177 | return 0; | |
178 | } | |
179 | ||
ad5f2e85 GL |
180 | static int mt9m001_set_bus_param(struct soc_camera_device *icd, |
181 | unsigned long flags) | |
f523dd0d | 182 | { |
40e2e092 | 183 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
36034dc3 | 184 | unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK; |
f523dd0d | 185 | |
36034dc3 SH |
186 | /* Only one width bit may be set */ |
187 | if (!is_power_of_2(width_flag)) | |
188 | return -EINVAL; | |
f523dd0d | 189 | |
36034dc3 SH |
190 | if (icl->set_bus_param) |
191 | return icl->set_bus_param(icl, width_flag); | |
f523dd0d | 192 | |
36034dc3 SH |
193 | /* |
194 | * Without board specific bus width settings we only support the | |
195 | * sensors native bus width | |
196 | */ | |
197 | if (width_flag == SOCAM_DATAWIDTH_10) | |
198 | return 0; | |
f523dd0d | 199 | |
36034dc3 | 200 | return -EINVAL; |
ad5f2e85 GL |
201 | } |
202 | ||
203 | static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd) | |
204 | { | |
40e2e092 | 205 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
bd73b36f | 206 | /* MT9M001 has all capture_format parameters fixed */ |
e951cbf2 | 207 | unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING | |
bd73b36f | 208 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH | |
2d9329f3 | 209 | SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER; |
ad5f2e85 | 210 | |
36034dc3 SH |
211 | if (icl->query_bus_param) |
212 | flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK; | |
213 | else | |
214 | flags |= SOCAM_DATAWIDTH_10; | |
ad5f2e85 | 215 | |
bd73b36f | 216 | return soc_camera_apply_sensor_flags(icl, flags); |
ad5f2e85 GL |
217 | } |
218 | ||
09e231b3 GL |
219 | static int mt9m001_set_crop(struct soc_camera_device *icd, |
220 | struct v4l2_rect *rect) | |
ad5f2e85 | 221 | { |
40e2e092 GL |
222 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
223 | struct mt9m001 *mt9m001 = i2c_get_clientdata(client); | |
ad5f2e85 GL |
224 | int ret; |
225 | const u16 hblank = 9, vblank = 25; | |
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) |
9538e1c2 | 235 | ret = reg_write(client, MT9M001_COLUMN_START, rect->left); |
11211641 | 236 | if (!ret) |
9538e1c2 | 237 | ret = reg_write(client, MT9M001_ROW_START, rect->top); |
11211641 | 238 | if (!ret) |
9538e1c2 | 239 | ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect->width - 1); |
11211641 | 240 | if (!ret) |
9538e1c2 | 241 | ret = reg_write(client, MT9M001_WINDOW_HEIGHT, |
f523dd0d | 242 | rect->height + icd->y_skip_top - 1); |
11211641 | 243 | if (!ret && mt9m001->autoexposure) { |
9538e1c2 | 244 | ret = reg_write(client, MT9M001_SHUTTER_WIDTH, |
f523dd0d | 245 | rect->height + icd->y_skip_top + vblank); |
11211641 | 246 | if (!ret) { |
f523dd0d GL |
247 | const struct v4l2_queryctrl *qctrl = |
248 | soc_camera_find_qctrl(icd->ops, | |
249 | V4L2_CID_EXPOSURE); | |
250 | icd->exposure = (524 + (rect->height + icd->y_skip_top + | |
251 | vblank - 1) * | |
252 | (qctrl->maximum - qctrl->minimum)) / | |
253 | 1048 + qctrl->minimum; | |
254 | } | |
255 | } | |
256 | ||
11211641 | 257 | return ret; |
f523dd0d GL |
258 | } |
259 | ||
09e231b3 GL |
260 | static int mt9m001_set_fmt(struct soc_camera_device *icd, |
261 | struct v4l2_format *f) | |
262 | { | |
263 | struct v4l2_rect rect = { | |
264 | .left = icd->x_current, | |
265 | .top = icd->y_current, | |
266 | .width = f->fmt.pix.width, | |
267 | .height = f->fmt.pix.height, | |
268 | }; | |
269 | ||
270 | /* No support for scaling so far, just crop. TODO: use skipping */ | |
271 | return mt9m001_set_crop(icd, &rect); | |
272 | } | |
273 | ||
d8fac217 GL |
274 | static int mt9m001_try_fmt(struct soc_camera_device *icd, |
275 | struct v4l2_format *f) | |
f523dd0d | 276 | { |
64f5905e GL |
277 | struct v4l2_pix_format *pix = &f->fmt.pix; |
278 | ||
653dc59b TP |
279 | v4l_bound_align_image(&pix->width, 48, 1280, 1, |
280 | &pix->height, 32 + icd->y_skip_top, | |
281 | 1024 + icd->y_skip_top, 0, 0); | |
f523dd0d GL |
282 | |
283 | return 0; | |
284 | } | |
285 | ||
286 | static int mt9m001_get_chip_id(struct soc_camera_device *icd, | |
aecde8b5 | 287 | struct v4l2_dbg_chip_ident *id) |
f523dd0d | 288 | { |
40e2e092 GL |
289 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
290 | struct mt9m001 *mt9m001 = i2c_get_clientdata(client); | |
f523dd0d | 291 | |
aecde8b5 | 292 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
f523dd0d GL |
293 | return -EINVAL; |
294 | ||
40e2e092 | 295 | if (id->match.addr != client->addr) |
f523dd0d GL |
296 | return -ENODEV; |
297 | ||
298 | id->ident = mt9m001->model; | |
299 | id->revision = 0; | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
305 | static int mt9m001_get_register(struct soc_camera_device *icd, | |
aecde8b5 | 306 | struct v4l2_dbg_register *reg) |
f523dd0d | 307 | { |
40e2e092 | 308 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
f523dd0d | 309 | |
aecde8b5 | 310 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
f523dd0d GL |
311 | return -EINVAL; |
312 | ||
9538e1c2 | 313 | if (reg->match.addr != client->addr) |
f523dd0d GL |
314 | return -ENODEV; |
315 | ||
aecde8b5 | 316 | reg->size = 2; |
9538e1c2 | 317 | reg->val = reg_read(client, reg->reg); |
f523dd0d GL |
318 | |
319 | if (reg->val > 0xffff) | |
320 | return -EIO; | |
321 | ||
322 | return 0; | |
323 | } | |
324 | ||
325 | static int mt9m001_set_register(struct soc_camera_device *icd, | |
aecde8b5 | 326 | struct v4l2_dbg_register *reg) |
f523dd0d | 327 | { |
40e2e092 | 328 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
f523dd0d | 329 | |
aecde8b5 | 330 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
f523dd0d GL |
331 | return -EINVAL; |
332 | ||
9538e1c2 | 333 | if (reg->match.addr != client->addr) |
f523dd0d GL |
334 | return -ENODEV; |
335 | ||
9538e1c2 | 336 | if (reg_write(client, reg->reg, reg->val) < 0) |
f523dd0d GL |
337 | return -EIO; |
338 | ||
339 | return 0; | |
340 | } | |
341 | #endif | |
342 | ||
4407a463 | 343 | static const struct v4l2_queryctrl mt9m001_controls[] = { |
f523dd0d GL |
344 | { |
345 | .id = V4L2_CID_VFLIP, | |
346 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
347 | .name = "Flip Vertically", | |
348 | .minimum = 0, | |
349 | .maximum = 1, | |
350 | .step = 1, | |
351 | .default_value = 0, | |
352 | }, { | |
353 | .id = V4L2_CID_GAIN, | |
354 | .type = V4L2_CTRL_TYPE_INTEGER, | |
355 | .name = "Gain", | |
356 | .minimum = 0, | |
357 | .maximum = 127, | |
358 | .step = 1, | |
359 | .default_value = 64, | |
360 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
361 | }, { | |
362 | .id = V4L2_CID_EXPOSURE, | |
363 | .type = V4L2_CTRL_TYPE_INTEGER, | |
364 | .name = "Exposure", | |
365 | .minimum = 1, | |
366 | .maximum = 255, | |
367 | .step = 1, | |
368 | .default_value = 255, | |
369 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
370 | }, { | |
371 | .id = V4L2_CID_EXPOSURE_AUTO, | |
372 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
373 | .name = "Automatic Exposure", | |
374 | .minimum = 0, | |
375 | .maximum = 1, | |
376 | .step = 1, | |
377 | .default_value = 1, | |
378 | } | |
379 | }; | |
380 | ||
26f1b942 GL |
381 | static int mt9m001_get_control(struct soc_camera_device *, struct v4l2_control *); |
382 | static int mt9m001_set_control(struct soc_camera_device *, struct v4l2_control *); | |
f523dd0d GL |
383 | |
384 | static struct soc_camera_ops mt9m001_ops = { | |
385 | .owner = THIS_MODULE, | |
386 | .init = mt9m001_init, | |
387 | .release = mt9m001_release, | |
388 | .start_capture = mt9m001_start_capture, | |
389 | .stop_capture = mt9m001_stop_capture, | |
09e231b3 | 390 | .set_crop = mt9m001_set_crop, |
d8fac217 GL |
391 | .set_fmt = mt9m001_set_fmt, |
392 | .try_fmt = mt9m001_try_fmt, | |
ad5f2e85 GL |
393 | .set_bus_param = mt9m001_set_bus_param, |
394 | .query_bus_param = mt9m001_query_bus_param, | |
f523dd0d GL |
395 | .controls = mt9m001_controls, |
396 | .num_controls = ARRAY_SIZE(mt9m001_controls), | |
397 | .get_control = mt9m001_get_control, | |
398 | .set_control = mt9m001_set_control, | |
399 | .get_chip_id = mt9m001_get_chip_id, | |
400 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
401 | .get_register = mt9m001_get_register, | |
402 | .set_register = mt9m001_set_register, | |
403 | #endif | |
404 | }; | |
405 | ||
406 | static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl) | |
407 | { | |
40e2e092 GL |
408 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
409 | struct mt9m001 *mt9m001 = i2c_get_clientdata(client); | |
f523dd0d GL |
410 | int data; |
411 | ||
412 | switch (ctrl->id) { | |
413 | case V4L2_CID_VFLIP: | |
9538e1c2 | 414 | data = reg_read(client, MT9M001_READ_OPTIONS2); |
f523dd0d GL |
415 | if (data < 0) |
416 | return -EIO; | |
417 | ctrl->value = !!(data & 0x8000); | |
418 | break; | |
419 | case V4L2_CID_EXPOSURE_AUTO: | |
420 | ctrl->value = mt9m001->autoexposure; | |
421 | break; | |
422 | } | |
423 | return 0; | |
424 | } | |
425 | ||
426 | static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl) | |
427 | { | |
40e2e092 GL |
428 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
429 | struct mt9m001 *mt9m001 = i2c_get_clientdata(client); | |
f523dd0d GL |
430 | const struct v4l2_queryctrl *qctrl; |
431 | int data; | |
432 | ||
433 | qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id); | |
434 | ||
435 | if (!qctrl) | |
436 | return -EINVAL; | |
437 | ||
438 | switch (ctrl->id) { | |
439 | case V4L2_CID_VFLIP: | |
440 | if (ctrl->value) | |
9538e1c2 | 441 | data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000); |
f523dd0d | 442 | else |
9538e1c2 | 443 | data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000); |
f523dd0d GL |
444 | if (data < 0) |
445 | return -EIO; | |
446 | break; | |
447 | case V4L2_CID_GAIN: | |
448 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
449 | return -EINVAL; | |
450 | /* See Datasheet Table 7, Gain settings. */ | |
451 | if (ctrl->value <= qctrl->default_value) { | |
452 | /* Pack it into 0..1 step 0.125, register values 0..8 */ | |
453 | unsigned long range = qctrl->default_value - qctrl->minimum; | |
454 | data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range; | |
455 | ||
456 | dev_dbg(&icd->dev, "Setting gain %d\n", data); | |
9538e1c2 | 457 | data = reg_write(client, MT9M001_GLOBAL_GAIN, data); |
f523dd0d GL |
458 | if (data < 0) |
459 | return -EIO; | |
460 | } else { | |
461 | /* Pack it into 1.125..15 variable step, register values 9..67 */ | |
462 | /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ | |
463 | unsigned long range = qctrl->maximum - qctrl->default_value - 1; | |
464 | unsigned long gain = ((ctrl->value - qctrl->default_value - 1) * | |
465 | 111 + range / 2) / range + 9; | |
466 | ||
467 | if (gain <= 32) | |
468 | data = gain; | |
469 | else if (gain <= 64) | |
470 | data = ((gain - 32) * 16 + 16) / 32 + 80; | |
471 | else | |
472 | data = ((gain - 64) * 7 + 28) / 56 + 96; | |
473 | ||
474 | dev_dbg(&icd->dev, "Setting gain from %d to %d\n", | |
9538e1c2 GL |
475 | reg_read(client, MT9M001_GLOBAL_GAIN), data); |
476 | data = reg_write(client, MT9M001_GLOBAL_GAIN, data); | |
f523dd0d GL |
477 | if (data < 0) |
478 | return -EIO; | |
479 | } | |
480 | ||
481 | /* Success */ | |
482 | icd->gain = ctrl->value; | |
483 | break; | |
484 | case V4L2_CID_EXPOSURE: | |
485 | /* mt9m001 has maximum == default */ | |
486 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
487 | return -EINVAL; | |
488 | else { | |
489 | unsigned long range = qctrl->maximum - qctrl->minimum; | |
490 | unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 + | |
491 | range / 2) / range + 1; | |
492 | ||
493 | dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n", | |
9538e1c2 GL |
494 | reg_read(client, MT9M001_SHUTTER_WIDTH), shutter); |
495 | if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0) | |
f523dd0d GL |
496 | return -EIO; |
497 | icd->exposure = ctrl->value; | |
498 | mt9m001->autoexposure = 0; | |
499 | } | |
500 | break; | |
501 | case V4L2_CID_EXPOSURE_AUTO: | |
502 | if (ctrl->value) { | |
503 | const u16 vblank = 25; | |
9538e1c2 | 504 | if (reg_write(client, MT9M001_SHUTTER_WIDTH, icd->height + |
f523dd0d GL |
505 | icd->y_skip_top + vblank) < 0) |
506 | return -EIO; | |
507 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); | |
508 | icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) * | |
509 | (qctrl->maximum - qctrl->minimum)) / | |
510 | 1048 + qctrl->minimum; | |
511 | mt9m001->autoexposure = 1; | |
512 | } else | |
513 | mt9m001->autoexposure = 0; | |
514 | break; | |
515 | } | |
516 | return 0; | |
517 | } | |
518 | ||
519 | /* Interface active, can use i2c. If it fails, it can indeed mean, that | |
520 | * this wasn't our capture interface, so, we wait for the right one */ | |
40e2e092 GL |
521 | static int mt9m001_video_probe(struct soc_camera_device *icd, |
522 | struct i2c_client *client) | |
f523dd0d | 523 | { |
40e2e092 GL |
524 | struct mt9m001 *mt9m001 = i2c_get_clientdata(client); |
525 | struct soc_camera_link *icl = to_soc_camera_link(icd); | |
f523dd0d GL |
526 | s32 data; |
527 | int ret; | |
36034dc3 | 528 | unsigned long flags; |
f523dd0d GL |
529 | |
530 | /* We must have a parent by now. And it cannot be a wrong one. | |
531 | * So this entire test is completely redundant. */ | |
532 | if (!icd->dev.parent || | |
533 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) | |
534 | return -ENODEV; | |
535 | ||
40e2e092 GL |
536 | /* Switch master clock on */ |
537 | ret = soc_camera_video_start(icd, &client->dev); | |
538 | if (ret) | |
539 | return ret; | |
540 | ||
f523dd0d | 541 | /* Enable the chip */ |
9538e1c2 | 542 | data = reg_write(client, MT9M001_CHIP_ENABLE, 1); |
f523dd0d GL |
543 | dev_dbg(&icd->dev, "write: %d\n", data); |
544 | ||
545 | /* Read out the chip version register */ | |
9538e1c2 | 546 | data = reg_read(client, MT9M001_CHIP_VERSION); |
f523dd0d | 547 | |
40e2e092 GL |
548 | soc_camera_video_stop(icd); |
549 | ||
f523dd0d GL |
550 | /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */ |
551 | switch (data) { | |
552 | case 0x8411: | |
553 | case 0x8421: | |
554 | mt9m001->model = V4L2_IDENT_MT9M001C12ST; | |
26f1b942 | 555 | icd->formats = mt9m001_colour_formats; |
f523dd0d GL |
556 | break; |
557 | case 0x8431: | |
558 | mt9m001->model = V4L2_IDENT_MT9M001C12STM; | |
26f1b942 | 559 | icd->formats = mt9m001_monochrome_formats; |
f523dd0d GL |
560 | break; |
561 | default: | |
f523dd0d GL |
562 | dev_err(&icd->dev, |
563 | "No MT9M001 chip detected, register read %x\n", data); | |
40e2e092 | 564 | return -ENODEV; |
f523dd0d GL |
565 | } |
566 | ||
36034dc3 SH |
567 | icd->num_formats = 0; |
568 | ||
569 | /* | |
570 | * This is a 10bit sensor, so by default we only allow 10bit. | |
571 | * The platform may support different bus widths due to | |
572 | * different routing of the data lines. | |
573 | */ | |
574 | if (icl->query_bus_param) | |
575 | flags = icl->query_bus_param(icl); | |
576 | else | |
577 | flags = SOCAM_DATAWIDTH_10; | |
578 | ||
579 | if (flags & SOCAM_DATAWIDTH_10) | |
580 | icd->num_formats++; | |
581 | else | |
582 | icd->formats++; | |
583 | ||
584 | if (flags & SOCAM_DATAWIDTH_8) | |
585 | icd->num_formats++; | |
586 | ||
f523dd0d GL |
587 | dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data, |
588 | data == 0x8431 ? "C12STM" : "C12ST"); | |
589 | ||
f523dd0d | 590 | return 0; |
f523dd0d GL |
591 | } |
592 | ||
593 | static void mt9m001_video_remove(struct soc_camera_device *icd) | |
594 | { | |
40e2e092 GL |
595 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
596 | struct soc_camera_link *icl = to_soc_camera_link(icd); | |
f523dd0d | 597 | |
40e2e092 | 598 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", client->addr, |
a85bdace | 599 | icd->dev.parent, icd->vdev); |
594bb46d GL |
600 | if (icl->free_bus) |
601 | icl->free_bus(icl); | |
f523dd0d GL |
602 | } |
603 | ||
d2653e92 JD |
604 | static int mt9m001_probe(struct i2c_client *client, |
605 | const struct i2c_device_id *did) | |
f523dd0d GL |
606 | { |
607 | struct mt9m001 *mt9m001; | |
40e2e092 | 608 | struct soc_camera_device *icd = client->dev.platform_data; |
f523dd0d | 609 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
40e2e092 | 610 | struct soc_camera_link *icl; |
f523dd0d GL |
611 | int ret; |
612 | ||
40e2e092 GL |
613 | if (!icd) { |
614 | dev_err(&client->dev, "MT9M001: missing soc-camera data!\n"); | |
615 | return -EINVAL; | |
616 | } | |
617 | ||
618 | icl = to_soc_camera_link(icd); | |
f523dd0d GL |
619 | if (!icl) { |
620 | dev_err(&client->dev, "MT9M001 driver needs platform data\n"); | |
621 | return -EINVAL; | |
622 | } | |
623 | ||
624 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { | |
625 | dev_warn(&adapter->dev, | |
626 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); | |
627 | return -EIO; | |
628 | } | |
629 | ||
630 | mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL); | |
631 | if (!mt9m001) | |
632 | return -ENOMEM; | |
633 | ||
f523dd0d GL |
634 | i2c_set_clientdata(client, mt9m001); |
635 | ||
636 | /* Second stage probe - when a capture adapter is there */ | |
f523dd0d | 637 | icd->ops = &mt9m001_ops; |
f523dd0d GL |
638 | icd->x_min = 20; |
639 | icd->y_min = 12; | |
640 | icd->x_current = 20; | |
641 | icd->y_current = 12; | |
642 | icd->width_min = 48; | |
643 | icd->width_max = 1280; | |
644 | icd->height_min = 32; | |
645 | icd->height_max = 1024; | |
646 | icd->y_skip_top = 1; | |
f523dd0d GL |
647 | /* Simulated autoexposure. If enabled, we calculate shutter width |
648 | * ourselves in the driver based on vertical blanking and frame width */ | |
649 | mt9m001->autoexposure = 1; | |
650 | ||
40e2e092 GL |
651 | ret = mt9m001_video_probe(icd, client); |
652 | if (ret) { | |
653 | icd->ops = NULL; | |
654 | i2c_set_clientdata(client, NULL); | |
655 | kfree(mt9m001); | |
656 | } | |
f523dd0d | 657 | |
f523dd0d GL |
658 | return ret; |
659 | } | |
660 | ||
661 | static int mt9m001_remove(struct i2c_client *client) | |
662 | { | |
663 | struct mt9m001 *mt9m001 = i2c_get_clientdata(client); | |
40e2e092 | 664 | struct soc_camera_device *icd = client->dev.platform_data; |
f523dd0d | 665 | |
40e2e092 GL |
666 | icd->ops = NULL; |
667 | mt9m001_video_remove(icd); | |
668 | i2c_set_clientdata(client, NULL); | |
669 | client->driver = NULL; | |
f523dd0d GL |
670 | kfree(mt9m001); |
671 | ||
672 | return 0; | |
673 | } | |
674 | ||
3760f736 JD |
675 | static const struct i2c_device_id mt9m001_id[] = { |
676 | { "mt9m001", 0 }, | |
677 | { } | |
678 | }; | |
679 | MODULE_DEVICE_TABLE(i2c, mt9m001_id); | |
680 | ||
f523dd0d GL |
681 | static struct i2c_driver mt9m001_i2c_driver = { |
682 | .driver = { | |
683 | .name = "mt9m001", | |
684 | }, | |
685 | .probe = mt9m001_probe, | |
686 | .remove = mt9m001_remove, | |
3760f736 | 687 | .id_table = mt9m001_id, |
f523dd0d GL |
688 | }; |
689 | ||
690 | static int __init mt9m001_mod_init(void) | |
691 | { | |
692 | return i2c_add_driver(&mt9m001_i2c_driver); | |
693 | } | |
694 | ||
695 | static void __exit mt9m001_mod_exit(void) | |
696 | { | |
697 | i2c_del_driver(&mt9m001_i2c_driver); | |
698 | } | |
699 | ||
700 | module_init(mt9m001_mod_init); | |
701 | module_exit(mt9m001_mod_exit); | |
702 | ||
703 | MODULE_DESCRIPTION("Micron MT9M001 Camera driver"); | |
704 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); | |
705 | MODULE_LICENSE("GPL"); |