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