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