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