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