V4L/DVB (13656): tw9910: tw9910_set_hsync clean up
[deliverable/linux.git] / drivers / media / video / mt9t031.c
CommitLineData
4e96fd08
GL
1/*
2 * Driver for MT9T031 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.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>
4e96fd08
GL
17#include <media/v4l2-chip-ident.h>
18#include <media/soc_camera.h>
19
5d28d525
GL
20/*
21 * mt9t031 i2c address 0x5d
979ea1dd 22 * The platform has to define i2c_board_info and link to it from
5d28d525
GL
23 * struct soc_camera_link
24 */
4e96fd08
GL
25
26/* mt9t031 selected register addresses */
27#define MT9T031_CHIP_VERSION 0x00
28#define MT9T031_ROW_START 0x01
29#define MT9T031_COLUMN_START 0x02
30#define MT9T031_WINDOW_HEIGHT 0x03
31#define MT9T031_WINDOW_WIDTH 0x04
32#define MT9T031_HORIZONTAL_BLANKING 0x05
33#define MT9T031_VERTICAL_BLANKING 0x06
34#define MT9T031_OUTPUT_CONTROL 0x07
35#define MT9T031_SHUTTER_WIDTH_UPPER 0x08
36#define MT9T031_SHUTTER_WIDTH 0x09
37#define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
38#define MT9T031_FRAME_RESTART 0x0b
39#define MT9T031_SHUTTER_DELAY 0x0c
40#define MT9T031_RESET 0x0d
41#define MT9T031_READ_MODE_1 0x1e
42#define MT9T031_READ_MODE_2 0x20
43#define MT9T031_READ_MODE_3 0x21
44#define MT9T031_ROW_ADDRESS_MODE 0x22
45#define MT9T031_COLUMN_ADDRESS_MODE 0x23
46#define MT9T031_GLOBAL_GAIN 0x35
47#define MT9T031_CHIP_ENABLE 0xF8
48
49#define MT9T031_MAX_HEIGHT 1536
50#define MT9T031_MAX_WIDTH 2048
51#define MT9T031_MIN_HEIGHT 2
6a6c8786 52#define MT9T031_MIN_WIDTH 18
4e96fd08
GL
53#define MT9T031_HORIZONTAL_BLANK 142
54#define MT9T031_VERTICAL_BLANK 25
55#define MT9T031_COLUMN_SKIP 32
56#define MT9T031_ROW_SKIP 20
57
58#define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \
59 SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \
60 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \
61 SOCAM_MASTER | SOCAM_DATAWIDTH_10)
62
63static const struct soc_camera_data_format mt9t031_colour_formats[] = {
64 {
65 .name = "Bayer (sRGB) 10 bit",
66 .depth = 10,
67 .fourcc = V4L2_PIX_FMT_SGRBG10,
68 .colorspace = V4L2_COLORSPACE_SRGB,
69 }
70};
71
72struct mt9t031 {
979ea1dd 73 struct v4l2_subdev subdev;
6a6c8786 74 struct v4l2_rect rect; /* Sensor window */
4e96fd08 75 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
4e96fd08
GL
76 u16 xskip;
77 u16 yskip;
96c75399 78 unsigned int gain;
32536108 79 unsigned short y_skip_top; /* Lines to skip at the top */
96c75399 80 unsigned int exposure;
6a6c8786 81 unsigned char autoexposure;
4e96fd08
GL
82};
83
979ea1dd
GL
84static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
85{
86 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
87}
88
9538e1c2 89static int reg_read(struct i2c_client *client, const u8 reg)
4e96fd08 90{
4e96fd08
GL
91 s32 data = i2c_smbus_read_word_data(client, reg);
92 return data < 0 ? data : swab16(data);
93}
94
9538e1c2 95static int reg_write(struct i2c_client *client, const u8 reg,
4e96fd08
GL
96 const u16 data)
97{
9538e1c2 98 return i2c_smbus_write_word_data(client, reg, swab16(data));
4e96fd08
GL
99}
100
9538e1c2 101static int reg_set(struct i2c_client *client, const u8 reg,
4e96fd08
GL
102 const u16 data)
103{
104 int ret;
105
9538e1c2 106 ret = reg_read(client, reg);
4e96fd08
GL
107 if (ret < 0)
108 return ret;
9538e1c2 109 return reg_write(client, reg, ret | data);
4e96fd08
GL
110}
111
9538e1c2 112static int reg_clear(struct i2c_client *client, const u8 reg,
4e96fd08
GL
113 const u16 data)
114{
115 int ret;
116
9538e1c2 117 ret = reg_read(client, reg);
4e96fd08
GL
118 if (ret < 0)
119 return ret;
9538e1c2 120 return reg_write(client, reg, ret & ~data);
4e96fd08
GL
121}
122
9538e1c2 123static int set_shutter(struct i2c_client *client, const u32 data)
4e96fd08
GL
124{
125 int ret;
126
9538e1c2 127 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
4e96fd08
GL
128
129 if (ret >= 0)
9538e1c2 130 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
4e96fd08
GL
131
132 return ret;
133}
134
9538e1c2 135static int get_shutter(struct i2c_client *client, u32 *data)
4e96fd08
GL
136{
137 int ret;
138
9538e1c2 139 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
4e96fd08
GL
140 *data = ret << 16;
141
142 if (ret >= 0)
9538e1c2 143 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
4e96fd08
GL
144 *data |= ret & 0xffff;
145
146 return ret < 0 ? ret : 0;
147}
148
979ea1dd 149static int mt9t031_idle(struct i2c_client *client)
4e96fd08
GL
150{
151 int ret;
152
153 /* Disable chip output, synchronous option update */
9538e1c2 154 ret = reg_write(client, MT9T031_RESET, 1);
4e96fd08 155 if (ret >= 0)
9538e1c2 156 ret = reg_write(client, MT9T031_RESET, 0);
4e96fd08 157 if (ret >= 0)
9538e1c2 158 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
4e96fd08
GL
159
160 return ret >= 0 ? 0 : -EIO;
161}
162
979ea1dd 163static int mt9t031_disable(struct i2c_client *client)
4e96fd08
GL
164{
165 /* Disable the chip */
9538e1c2 166 reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
f9826514 167
4e96fd08
GL
168 return 0;
169}
170
979ea1dd
GL
171static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
172{
173 struct i2c_client *client = sd->priv;
174 int ret;
175
176 if (enable)
177 /* Switch to master "normal" mode */
178 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
179 else
180 /* Stop sensor readout */
181 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
182
183 if (ret < 0)
4e96fd08 184 return -EIO;
979ea1dd 185
4e96fd08
GL
186 return 0;
187}
188
189static int mt9t031_set_bus_param(struct soc_camera_device *icd,
190 unsigned long flags)
191{
40e2e092 192 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
9538e1c2 193
4e96fd08
GL
194 /* The caller should have queried our parameters, check anyway */
195 if (flags & ~MT9T031_BUS_PARAM)
196 return -EINVAL;
197
198 if (flags & SOCAM_PCLK_SAMPLE_FALLING)
9538e1c2 199 reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
c98afbfc 200 else
9538e1c2 201 reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
4e96fd08
GL
202
203 return 0;
204}
205
206static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
207{
40e2e092 208 struct soc_camera_link *icl = to_soc_camera_link(icd);
4e96fd08
GL
209
210 return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
211}
212
6a6c8786
GL
213/* target must be _even_ */
214static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
70e1d353 215{
6a6c8786
GL
216 unsigned int skip;
217
218 if (*source < target + target / 2) {
219 *source = target;
220 return 1;
221 }
222
223 skip = min(max, *source + target / 2) / target;
224 if (skip > 8)
225 skip = 8;
226 *source = target * skip;
227
228 return skip;
70e1d353
GL
229}
230
6a6c8786 231/* rect is the sensor rectangle, the caller guarantees parameter validity */
09e231b3
GL
232static int mt9t031_set_params(struct soc_camera_device *icd,
233 struct v4l2_rect *rect, u16 xskip, u16 yskip)
4e96fd08 234{
40e2e092 235 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
979ea1dd 236 struct mt9t031 *mt9t031 = to_mt9t031(client);
4e96fd08 237 int ret;
6a6c8786 238 u16 xbin, ybin;
4e96fd08
GL
239 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
240 vblank = MT9T031_VERTICAL_BLANK;
4e96fd08
GL
241
242 xbin = min(xskip, (u16)3);
243 ybin = min(yskip, (u16)3);
244
6a6c8786
GL
245 /*
246 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
247 * There is always a valid suitably aligned value. The worst case is
248 * xbin = 3, width = 2048. Then we will start at 36, the last read out
249 * pixel will be 2083, which is < 2085 - first black pixel.
250 *
251 * MT9T031 datasheet imposes window left border alignment, depending on
252 * the selected xskip. Failing to conform to this requirement produces
253 * dark horizontal stripes in the image. However, even obeying to this
254 * requirement doesn't eliminate the stripes in all configurations. They
255 * appear "locally reproducibly," but can differ between tests under
256 * different lighting conditions.
257 */
4e96fd08 258 switch (xbin) {
6a6c8786
GL
259 case 1:
260 rect->left &= ~1;
4e96fd08 261 break;
4e96fd08 262 case 2:
6a6c8786 263 rect->left &= ~3;
4e96fd08
GL
264 break;
265 case 3:
6a6c8786
GL
266 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
267 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
4e96fd08
GL
268 }
269
6a6c8786
GL
270 rect->top &= ~1;
271
272 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
273 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
274
70e1d353 275 /* Disable register update, reconfigure atomically */
9538e1c2 276 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
70e1d353
GL
277 if (ret < 0)
278 return ret;
279
4e96fd08 280 /* Blanking and start values - default... */
9538e1c2 281 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
4e96fd08 282 if (ret >= 0)
9538e1c2 283 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
4e96fd08 284
09e231b3 285 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
4e96fd08
GL
286 /* Binning, skipping */
287 if (ret >= 0)
9538e1c2 288 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
4e96fd08
GL
289 ((xbin - 1) << 4) | (xskip - 1));
290 if (ret >= 0)
9538e1c2 291 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
4e96fd08
GL
292 ((ybin - 1) << 4) | (yskip - 1));
293 }
6a6c8786
GL
294 dev_dbg(&client->dev, "new physical left %u, top %u\n",
295 rect->left, rect->top);
4e96fd08 296
5d28d525
GL
297 /*
298 * The caller provides a supported format, as guaranteed by
299 * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap()
300 */
4e96fd08 301 if (ret >= 0)
6a6c8786 302 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
4e96fd08 303 if (ret >= 0)
6a6c8786 304 ret = reg_write(client, MT9T031_ROW_START, rect->top);
4e96fd08 305 if (ret >= 0)
6a6c8786 306 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
4e96fd08 307 if (ret >= 0)
9538e1c2 308 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
32536108 309 rect->height + mt9t031->y_skip_top - 1);
4e96fd08 310 if (ret >= 0 && mt9t031->autoexposure) {
32536108 311 unsigned int total_h = rect->height + mt9t031->y_skip_top + vblank;
96c75399 312 ret = set_shutter(client, total_h);
4e96fd08
GL
313 if (ret >= 0) {
314 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
315 const struct v4l2_queryctrl *qctrl =
316 soc_camera_find_qctrl(icd->ops,
317 V4L2_CID_EXPOSURE);
96c75399
GL
318 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
319 (qctrl->maximum - qctrl->minimum)) /
4e96fd08
GL
320 shutter_max + qctrl->minimum;
321 }
322 }
323
09e231b3
GL
324 /* Re-enable register update, commit all changes */
325 if (ret >= 0)
9538e1c2 326 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
09e231b3 327
6a6c8786
GL
328 if (ret >= 0) {
329 mt9t031->rect = *rect;
330 mt9t031->xskip = xskip;
331 mt9t031->yskip = yskip;
332 }
333
09e231b3
GL
334 return ret < 0 ? ret : 0;
335}
336
08590b96 337static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
09e231b3 338{
6a6c8786 339 struct v4l2_rect rect = a->c;
08590b96 340 struct i2c_client *client = sd->priv;
979ea1dd 341 struct mt9t031 *mt9t031 = to_mt9t031(client);
08590b96 342 struct soc_camera_device *icd = client->dev.platform_data;
09e231b3 343
6a6c8786
GL
344 rect.width = ALIGN(rect.width, 2);
345 rect.height = ALIGN(rect.height, 2);
346
347 soc_camera_limit_side(&rect.left, &rect.width,
348 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
349
350 soc_camera_limit_side(&rect.top, &rect.height,
351 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
352
353 return mt9t031_set_params(icd, &rect, mt9t031->xskip, mt9t031->yskip);
354}
355
356static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
357{
358 struct i2c_client *client = sd->priv;
359 struct mt9t031 *mt9t031 = to_mt9t031(client);
360
361 a->c = mt9t031->rect;
362 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
e330919a 363
6a6c8786
GL
364 return 0;
365}
366
367static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
368{
369 a->bounds.left = MT9T031_COLUMN_SKIP;
370 a->bounds.top = MT9T031_ROW_SKIP;
371 a->bounds.width = MT9T031_MAX_WIDTH;
372 a->bounds.height = MT9T031_MAX_HEIGHT;
373 a->defrect = a->bounds;
374 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
375 a->pixelaspect.numerator = 1;
376 a->pixelaspect.denominator = 1;
e330919a 377
6a6c8786
GL
378 return 0;
379}
380
381static int mt9t031_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
382{
383 struct i2c_client *client = sd->priv;
384 struct mt9t031 *mt9t031 = to_mt9t031(client);
385 struct v4l2_pix_format *pix = &f->fmt.pix;
386
387 pix->width = mt9t031->rect.width / mt9t031->xskip;
388 pix->height = mt9t031->rect.height / mt9t031->yskip;
389 pix->pixelformat = V4L2_PIX_FMT_SGRBG10;
390 pix->field = V4L2_FIELD_NONE;
391 pix->colorspace = V4L2_COLORSPACE_SRGB;
392
393 return 0;
09e231b3
GL
394}
395
979ea1dd 396static int mt9t031_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
09e231b3 397{
979ea1dd
GL
398 struct i2c_client *client = sd->priv;
399 struct mt9t031 *mt9t031 = to_mt9t031(client);
400 struct soc_camera_device *icd = client->dev.platform_data;
6a6c8786 401 struct v4l2_pix_format *pix = &f->fmt.pix;
09e231b3 402 u16 xskip, yskip;
6a6c8786 403 struct v4l2_rect rect = mt9t031->rect;
09e231b3
GL
404
405 /*
6a6c8786
GL
406 * try_fmt has put width and height within limits.
407 * S_FMT: use binning and skipping for scaling
09e231b3 408 */
6a6c8786
GL
409 xskip = mt9t031_skip(&rect.width, pix->width, MT9T031_MAX_WIDTH);
410 yskip = mt9t031_skip(&rect.height, pix->height, MT9T031_MAX_HEIGHT);
4e96fd08 411
6a6c8786
GL
412 /* mt9t031_set_params() doesn't change width and height */
413 return mt9t031_set_params(icd, &rect, xskip, yskip);
4e96fd08
GL
414}
415
6a6c8786
GL
416/*
417 * If a user window larger than sensor window is requested, we'll increase the
418 * sensor window.
419 */
979ea1dd 420static int mt9t031_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
4e96fd08
GL
421{
422 struct v4l2_pix_format *pix = &f->fmt.pix;
423
653dc59b
TP
424 v4l_bound_align_image(
425 &pix->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
426 &pix->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
4e96fd08
GL
427
428 return 0;
429}
430
979ea1dd
GL
431static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
432 struct v4l2_dbg_chip_ident *id)
4e96fd08 433{
979ea1dd
GL
434 struct i2c_client *client = sd->priv;
435 struct mt9t031 *mt9t031 = to_mt9t031(client);
4e96fd08 436
aecde8b5 437 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
4e96fd08
GL
438 return -EINVAL;
439
40e2e092 440 if (id->match.addr != client->addr)
4e96fd08
GL
441 return -ENODEV;
442
443 id->ident = mt9t031->model;
444 id->revision = 0;
445
446 return 0;
447}
448
449#ifdef CONFIG_VIDEO_ADV_DEBUG
979ea1dd
GL
450static int mt9t031_g_register(struct v4l2_subdev *sd,
451 struct v4l2_dbg_register *reg)
4e96fd08 452{
979ea1dd 453 struct i2c_client *client = sd->priv;
4e96fd08 454
aecde8b5 455 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
4e96fd08
GL
456 return -EINVAL;
457
9538e1c2 458 if (reg->match.addr != client->addr)
4e96fd08
GL
459 return -ENODEV;
460
9538e1c2 461 reg->val = reg_read(client, reg->reg);
4e96fd08
GL
462
463 if (reg->val > 0xffff)
464 return -EIO;
465
466 return 0;
467}
468
979ea1dd
GL
469static int mt9t031_s_register(struct v4l2_subdev *sd,
470 struct v4l2_dbg_register *reg)
4e96fd08 471{
979ea1dd 472 struct i2c_client *client = sd->priv;
4e96fd08 473
aecde8b5 474 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
4e96fd08
GL
475 return -EINVAL;
476
9538e1c2 477 if (reg->match.addr != client->addr)
4e96fd08
GL
478 return -ENODEV;
479
9538e1c2 480 if (reg_write(client, reg->reg, reg->val) < 0)
4e96fd08
GL
481 return -EIO;
482
483 return 0;
484}
485#endif
486
487static const struct v4l2_queryctrl mt9t031_controls[] = {
488 {
489 .id = V4L2_CID_VFLIP,
490 .type = V4L2_CTRL_TYPE_BOOLEAN,
491 .name = "Flip Vertically",
492 .minimum = 0,
493 .maximum = 1,
494 .step = 1,
495 .default_value = 0,
70e1d353
GL
496 }, {
497 .id = V4L2_CID_HFLIP,
498 .type = V4L2_CTRL_TYPE_BOOLEAN,
499 .name = "Flip Horizontally",
500 .minimum = 0,
501 .maximum = 1,
502 .step = 1,
503 .default_value = 0,
4e96fd08
GL
504 }, {
505 .id = V4L2_CID_GAIN,
506 .type = V4L2_CTRL_TYPE_INTEGER,
507 .name = "Gain",
508 .minimum = 0,
509 .maximum = 127,
510 .step = 1,
511 .default_value = 64,
512 .flags = V4L2_CTRL_FLAG_SLIDER,
513 }, {
514 .id = V4L2_CID_EXPOSURE,
515 .type = V4L2_CTRL_TYPE_INTEGER,
516 .name = "Exposure",
517 .minimum = 1,
518 .maximum = 255,
519 .step = 1,
520 .default_value = 255,
521 .flags = V4L2_CTRL_FLAG_SLIDER,
522 }, {
523 .id = V4L2_CID_EXPOSURE_AUTO,
524 .type = V4L2_CTRL_TYPE_BOOLEAN,
525 .name = "Automatic Exposure",
526 .minimum = 0,
527 .maximum = 1,
528 .step = 1,
529 .default_value = 1,
530 }
531};
532
4e96fd08 533static struct soc_camera_ops mt9t031_ops = {
4e96fd08
GL
534 .set_bus_param = mt9t031_set_bus_param,
535 .query_bus_param = mt9t031_query_bus_param,
536 .controls = mt9t031_controls,
537 .num_controls = ARRAY_SIZE(mt9t031_controls),
4e96fd08
GL
538};
539
979ea1dd 540static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
4e96fd08 541{
979ea1dd
GL
542 struct i2c_client *client = sd->priv;
543 struct mt9t031 *mt9t031 = to_mt9t031(client);
4e96fd08
GL
544 int data;
545
546 switch (ctrl->id) {
547 case V4L2_CID_VFLIP:
9538e1c2 548 data = reg_read(client, MT9T031_READ_MODE_2);
4e96fd08
GL
549 if (data < 0)
550 return -EIO;
551 ctrl->value = !!(data & 0x8000);
552 break;
553 case V4L2_CID_HFLIP:
9538e1c2 554 data = reg_read(client, MT9T031_READ_MODE_2);
4e96fd08
GL
555 if (data < 0)
556 return -EIO;
557 ctrl->value = !!(data & 0x4000);
558 break;
559 case V4L2_CID_EXPOSURE_AUTO:
560 ctrl->value = mt9t031->autoexposure;
561 break;
96c75399
GL
562 case V4L2_CID_GAIN:
563 ctrl->value = mt9t031->gain;
564 break;
565 case V4L2_CID_EXPOSURE:
566 ctrl->value = mt9t031->exposure;
567 break;
4e96fd08
GL
568 }
569 return 0;
570}
571
979ea1dd 572static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
4e96fd08 573{
979ea1dd
GL
574 struct i2c_client *client = sd->priv;
575 struct mt9t031 *mt9t031 = to_mt9t031(client);
576 struct soc_camera_device *icd = client->dev.platform_data;
4e96fd08
GL
577 const struct v4l2_queryctrl *qctrl;
578 int data;
579
580 qctrl = soc_camera_find_qctrl(&mt9t031_ops, ctrl->id);
581
582 if (!qctrl)
583 return -EINVAL;
584
585 switch (ctrl->id) {
586 case V4L2_CID_VFLIP:
587 if (ctrl->value)
9538e1c2 588 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
4e96fd08 589 else
9538e1c2 590 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
4e96fd08
GL
591 if (data < 0)
592 return -EIO;
593 break;
594 case V4L2_CID_HFLIP:
595 if (ctrl->value)
9538e1c2 596 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
4e96fd08 597 else
9538e1c2 598 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
4e96fd08
GL
599 if (data < 0)
600 return -EIO;
601 break;
602 case V4L2_CID_GAIN:
603 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
604 return -EINVAL;
605 /* See Datasheet Table 7, Gain settings. */
606 if (ctrl->value <= qctrl->default_value) {
607 /* Pack it into 0..1 step 0.125, register values 0..8 */
608 unsigned long range = qctrl->default_value - qctrl->minimum;
609 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
610
85f8be68 611 dev_dbg(&client->dev, "Setting gain %d\n", data);
9538e1c2 612 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
4e96fd08
GL
613 if (data < 0)
614 return -EIO;
615 } else {
70e1d353 616 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
4e96fd08
GL
617 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
618 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
70e1d353 619 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
4e96fd08 620 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
70e1d353 621 1015 + range / 2) / range + 9;
4e96fd08 622
70e1d353 623 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
4e96fd08 624 data = gain;
70e1d353 625 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
4e96fd08
GL
626 data = ((gain - 32) * 16 + 16) / 32 + 80;
627 else
70e1d353
GL
628 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
629 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
4e96fd08 630
85f8be68 631 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
9538e1c2
GL
632 reg_read(client, MT9T031_GLOBAL_GAIN), data);
633 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
4e96fd08
GL
634 if (data < 0)
635 return -EIO;
636 }
637
638 /* Success */
96c75399 639 mt9t031->gain = ctrl->value;
4e96fd08
GL
640 break;
641 case V4L2_CID_EXPOSURE:
642 /* mt9t031 has maximum == default */
643 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
644 return -EINVAL;
645 else {
646 const unsigned long range = qctrl->maximum - qctrl->minimum;
647 const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
648 range / 2) / range + 1;
649 u32 old;
650
9538e1c2 651 get_shutter(client, &old);
85f8be68 652 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
4e96fd08 653 old, shutter);
9538e1c2 654 if (set_shutter(client, shutter) < 0)
4e96fd08 655 return -EIO;
96c75399 656 mt9t031->exposure = ctrl->value;
4e96fd08
GL
657 mt9t031->autoexposure = 0;
658 }
659 break;
660 case V4L2_CID_EXPOSURE_AUTO:
661 if (ctrl->value) {
662 const u16 vblank = MT9T031_VERTICAL_BLANK;
663 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
96c75399 664 unsigned int total_h = mt9t031->rect.height +
32536108 665 mt9t031->y_skip_top + vblank;
96c75399
GL
666
667 if (set_shutter(client, total_h) < 0)
4e96fd08
GL
668 return -EIO;
669 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
96c75399
GL
670 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
671 (qctrl->maximum - qctrl->minimum)) /
4e96fd08
GL
672 shutter_max + qctrl->minimum;
673 mt9t031->autoexposure = 1;
674 } else
675 mt9t031->autoexposure = 0;
676 break;
677 }
678 return 0;
679}
680
5d28d525
GL
681/*
682 * Interface active, can use i2c. If it fails, it can indeed mean, that
683 * this wasn't our capture interface, so, we wait for the right one
684 */
979ea1dd 685static int mt9t031_video_probe(struct i2c_client *client)
4e96fd08 686{
979ea1dd
GL
687 struct soc_camera_device *icd = client->dev.platform_data;
688 struct mt9t031 *mt9t031 = to_mt9t031(client);
4e96fd08 689 s32 data;
a4c56fd8 690 int ret;
4e96fd08 691
4e96fd08 692 /* Enable the chip */
9538e1c2 693 data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
85f8be68 694 dev_dbg(&client->dev, "write: %d\n", data);
4e96fd08
GL
695
696 /* Read out the chip version register */
9538e1c2 697 data = reg_read(client, MT9T031_CHIP_VERSION);
4e96fd08
GL
698
699 switch (data) {
700 case 0x1621:
701 mt9t031->model = V4L2_IDENT_MT9T031;
702 icd->formats = mt9t031_colour_formats;
703 icd->num_formats = ARRAY_SIZE(mt9t031_colour_formats);
704 break;
705 default:
85f8be68 706 dev_err(&client->dev,
4e96fd08 707 "No MT9T031 chip detected, register read %x\n", data);
40e2e092 708 return -ENODEV;
4e96fd08
GL
709 }
710
85f8be68 711 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
4e96fd08 712
a4c56fd8
GL
713 ret = mt9t031_idle(client);
714 if (ret < 0)
715 dev_err(&client->dev, "Failed to initialise the camera\n");
716
96c75399
GL
717 /* mt9t031_idle() has reset the chip to default. */
718 mt9t031->exposure = 255;
719 mt9t031->gain = 64;
720
a4c56fd8 721 return ret;
4e96fd08
GL
722}
723
32536108
GL
724static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
725{
726 struct i2c_client *client = sd->priv;
727 struct mt9t031 *mt9t031 = to_mt9t031(client);
728
729 *lines = mt9t031->y_skip_top;
730
731 return 0;
732}
733
979ea1dd
GL
734static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
735 .g_ctrl = mt9t031_g_ctrl,
736 .s_ctrl = mt9t031_s_ctrl,
737 .g_chip_ident = mt9t031_g_chip_ident,
738#ifdef CONFIG_VIDEO_ADV_DEBUG
739 .g_register = mt9t031_g_register,
740 .s_register = mt9t031_s_register,
741#endif
742};
743
744static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
745 .s_stream = mt9t031_s_stream,
746 .s_fmt = mt9t031_s_fmt,
6a6c8786 747 .g_fmt = mt9t031_g_fmt,
979ea1dd 748 .try_fmt = mt9t031_try_fmt,
08590b96 749 .s_crop = mt9t031_s_crop,
6a6c8786
GL
750 .g_crop = mt9t031_g_crop,
751 .cropcap = mt9t031_cropcap,
979ea1dd
GL
752};
753
32536108
GL
754static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
755 .g_skip_top_lines = mt9t031_g_skip_top_lines,
756};
757
979ea1dd
GL
758static struct v4l2_subdev_ops mt9t031_subdev_ops = {
759 .core = &mt9t031_subdev_core_ops,
760 .video = &mt9t031_subdev_video_ops,
32536108 761 .sensor = &mt9t031_subdev_sensor_ops,
979ea1dd
GL
762};
763
4e96fd08
GL
764static int mt9t031_probe(struct i2c_client *client,
765 const struct i2c_device_id *did)
766{
767 struct mt9t031 *mt9t031;
40e2e092 768 struct soc_camera_device *icd = client->dev.platform_data;
4e96fd08 769 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
40e2e092 770 struct soc_camera_link *icl;
4e96fd08
GL
771 int ret;
772
40e2e092
GL
773 if (!icd) {
774 dev_err(&client->dev, "MT9T031: missing soc-camera data!\n");
775 return -EINVAL;
776 }
777
778 icl = to_soc_camera_link(icd);
4e96fd08
GL
779 if (!icl) {
780 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
781 return -EINVAL;
782 }
783
784 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
785 dev_warn(&adapter->dev,
786 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
787 return -EIO;
788 }
789
790 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
791 if (!mt9t031)
792 return -ENOMEM;
793
979ea1dd 794 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
4e96fd08
GL
795
796 /* Second stage probe - when a capture adapter is there */
a0705b07 797 icd->ops = &mt9t031_ops;
6a6c8786 798
32536108 799 mt9t031->y_skip_top = 0;
6a6c8786
GL
800 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
801 mt9t031->rect.top = MT9T031_ROW_SKIP;
802 mt9t031->rect.width = MT9T031_MAX_WIDTH;
803 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
804
5d28d525
GL
805 /*
806 * Simulated autoexposure. If enabled, we calculate shutter width
807 * ourselves in the driver based on vertical blanking and frame width
808 */
4e96fd08
GL
809 mt9t031->autoexposure = 1;
810
811 mt9t031->xskip = 1;
812 mt9t031->yskip = 1;
813
979ea1dd
GL
814 mt9t031_idle(client);
815
816 ret = mt9t031_video_probe(client);
817
818 mt9t031_disable(client);
819
40e2e092
GL
820 if (ret) {
821 icd->ops = NULL;
822 i2c_set_clientdata(client, NULL);
823 kfree(mt9t031);
824 }
4e96fd08 825
4e96fd08
GL
826 return ret;
827}
828
829static int mt9t031_remove(struct i2c_client *client)
830{
979ea1dd 831 struct mt9t031 *mt9t031 = to_mt9t031(client);
40e2e092 832 struct soc_camera_device *icd = client->dev.platform_data;
4e96fd08 833
40e2e092 834 icd->ops = NULL;
4e96fd08 835 i2c_set_clientdata(client, NULL);
40e2e092 836 client->driver = NULL;
4e96fd08
GL
837 kfree(mt9t031);
838
839 return 0;
840}
841
842static const struct i2c_device_id mt9t031_id[] = {
843 { "mt9t031", 0 },
844 { }
845};
846MODULE_DEVICE_TABLE(i2c, mt9t031_id);
847
848static struct i2c_driver mt9t031_i2c_driver = {
849 .driver = {
850 .name = "mt9t031",
851 },
852 .probe = mt9t031_probe,
853 .remove = mt9t031_remove,
854 .id_table = mt9t031_id,
855};
856
857static int __init mt9t031_mod_init(void)
858{
859 return i2c_add_driver(&mt9t031_i2c_driver);
860}
861
862static void __exit mt9t031_mod_exit(void)
863{
864 i2c_del_driver(&mt9t031_i2c_driver);
865}
866
867module_init(mt9t031_mod_init);
868module_exit(mt9t031_mod_exit);
869
870MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
871MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
872MODULE_LICENSE("GPL v2");
This page took 0.356749 seconds and 5 git commands to generate.