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