blob: 72479247522a8fe923235a88b342145ed0d39cf3 [file] [log] [blame]
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -03001/*
2 * Driver for MT9V022 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.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/delay.h>
15#include <linux/log2.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040016#include <linux/module.h>
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030017
Guennadi Liakhovetskie8e2c702011-07-27 05:53:33 -030018#include <media/soc_camera.h>
19#include <media/soc_mediabus.h>
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030020#include <media/v4l2-subdev.h>
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030021#include <media/v4l2-chip-ident.h>
Hans Verkuilab7b50a2011-09-07 05:22:39 -030022#include <media/v4l2-ctrls.h>
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030023
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030024/*
25 * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
Peter Meerwald22cf83f2012-06-13 10:48:11 -030026 * The platform has to define struct i2c_board_info objects and link to them
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030027 * from struct soc_camera_link
28 */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030029
30static char *sensor_type;
31module_param(sensor_type, charp, S_IRUGO);
Niels de Vos61a2d072008-07-31 00:07:23 -070032MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030033
34/* mt9v022 selected register addresses */
35#define MT9V022_CHIP_VERSION 0x00
36#define MT9V022_COLUMN_START 0x01
37#define MT9V022_ROW_START 0x02
38#define MT9V022_WINDOW_HEIGHT 0x03
39#define MT9V022_WINDOW_WIDTH 0x04
40#define MT9V022_HORIZONTAL_BLANKING 0x05
41#define MT9V022_VERTICAL_BLANKING 0x06
42#define MT9V022_CHIP_CONTROL 0x07
43#define MT9V022_SHUTTER_WIDTH1 0x08
44#define MT9V022_SHUTTER_WIDTH2 0x09
45#define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
46#define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
47#define MT9V022_RESET 0x0c
48#define MT9V022_READ_MODE 0x0d
49#define MT9V022_MONITOR_MODE 0x0e
50#define MT9V022_PIXEL_OPERATION_MODE 0x0f
51#define MT9V022_LED_OUT_CONTROL 0x1b
52#define MT9V022_ADC_MODE_CONTROL 0x1c
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -030053#define MT9V022_ANALOG_GAIN 0x35
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030054#define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
55#define MT9V022_PIXCLK_FV_LV 0x74
56#define MT9V022_DIGITAL_TEST_PATTERN 0x7f
57#define MT9V022_AEC_AGC_ENABLE 0xAF
58#define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
59
60/* Progressive scan, master, defaults */
61#define MT9V022_CHIP_CONTROL_DEFAULT 0x188
62
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030063#define MT9V022_MAX_WIDTH 752
64#define MT9V022_MAX_HEIGHT 480
65#define MT9V022_MIN_WIDTH 48
66#define MT9V022_MIN_HEIGHT 32
67#define MT9V022_COLUMN_SKIP 1
68#define MT9V022_ROW_SKIP 4
69
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030070/* MT9V022 has only one fixed colorspace per pixelcode */
71struct mt9v022_datafmt {
72 enum v4l2_mbus_pixelcode code;
73 enum v4l2_colorspace colorspace;
74};
75
76/* Find a data format by a pixel code in an array */
77static const struct mt9v022_datafmt *mt9v022_find_datafmt(
78 enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
79 int n)
80{
81 int i;
82 for (i = 0; i < n; i++)
83 if (fmt[i].code == code)
84 return fmt + i;
85
86 return NULL;
87}
88
89static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030090 /*
91 * Order important: first natively supported,
92 * second supported with a GPIO extender
93 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030094 {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
95 {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030096};
97
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030098static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030099 /* Order important - see above */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300100 {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
Laurent Pinchart076704332010-09-28 07:01:44 -0300101 {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300102};
103
104struct mt9v022 {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300105 struct v4l2_subdev subdev;
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300106 struct v4l2_ctrl_handler hdl;
107 struct {
108 /* exposure/auto-exposure cluster */
109 struct v4l2_ctrl *autoexposure;
110 struct v4l2_ctrl *exposure;
111 };
112 struct {
113 /* gain/auto-gain cluster */
114 struct v4l2_ctrl *autogain;
115 struct v4l2_ctrl *gain;
116 };
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300117 struct v4l2_rect rect; /* Sensor window */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300118 const struct mt9v022_datafmt *fmt;
119 const struct mt9v022_datafmt *fmts;
120 int num_fmts;
Guennadi Liakhovetski7fb0fd02008-05-05 14:12:30 -0300121 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300122 u16 chip_control;
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300123 unsigned short y_skip_top; /* Lines to skip at the top */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300124};
125
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300126static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
127{
128 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
129}
130
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300131static int reg_read(struct i2c_client *client, const u8 reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300132{
Jonathan Cameron3f877042011-10-21 09:30:25 -0300133 return i2c_smbus_read_word_swapped(client, reg);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300134}
135
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300136static int reg_write(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300137 const u16 data)
138{
Jonathan Cameron3f877042011-10-21 09:30:25 -0300139 return i2c_smbus_write_word_swapped(client, reg, data);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300140}
141
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300142static int reg_set(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300143 const u16 data)
144{
145 int ret;
146
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300147 ret = reg_read(client, reg);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300148 if (ret < 0)
149 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300150 return reg_write(client, reg, ret | data);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300151}
152
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300153static int reg_clear(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300154 const u16 data)
155{
156 int ret;
157
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300158 ret = reg_read(client, reg);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300159 if (ret < 0)
160 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300161 return reg_write(client, reg, ret & ~data);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300162}
163
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300164static int mt9v022_init(struct i2c_client *client)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300165{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300166 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300167 int ret;
168
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300169 /*
170 * Almost the default mode: master, parallel, simultaneous, and an
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300171 * undocumented bit 0x200, which is present in table 7, but not in 8,
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300172 * plus snapshot mode to disable scan for now
173 */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300174 mt9v022->chip_control |= 0x10;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300175 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300176 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300177 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300178
179 /* All defaults */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300180 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300181 /* AEC, AGC on */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300182 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300183 if (!ret)
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300184 ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
185 if (!ret)
186 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
187 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300188 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300189 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300190 /* default - auto */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300191 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300192 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300193 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300194 if (!ret)
195 return v4l2_ctrl_handler_setup(&mt9v022->hdl);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300196
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300197 return ret;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300198}
199
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300200static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300201{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300202 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300203 struct mt9v022 *mt9v022 = to_mt9v022(client);
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300204
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300205 if (enable)
206 /* Switch to master "normal" mode */
207 mt9v022->chip_control &= ~0x10;
208 else
209 /* Switch to snapshot mode */
210 mt9v022->chip_control |= 0x10;
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300211
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300212 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300213 return -EIO;
214 return 0;
215}
216
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300217static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300218{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300219 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300220 struct mt9v022 *mt9v022 = to_mt9v022(client);
221 struct v4l2_rect rect = a->c;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300222 int ret;
223
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300224 /* Bayer format - even size lengths */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300225 if (mt9v022->fmts == mt9v022_colour_fmts) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300226 rect.width = ALIGN(rect.width, 2);
227 rect.height = ALIGN(rect.height, 2);
228 /* Let the user play with the starting pixel */
229 }
230
231 soc_camera_limit_side(&rect.left, &rect.width,
232 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
233
234 soc_camera_limit_side(&rect.top, &rect.height,
235 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
236
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300237 /* Like in example app. Contradicts the datasheet though */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300238 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300239 if (ret >= 0) {
240 if (ret & 1) /* Autoexposure */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300241 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300242 rect.height + mt9v022->y_skip_top + 43);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300243 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300244 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300245 rect.height + mt9v022->y_skip_top + 43);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300246 }
247 /* Setup frame format: defaults apart from width and height */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300248 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300249 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300250 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300251 ret = reg_write(client, MT9V022_ROW_START, rect.top);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300252 if (!ret)
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300253 /*
254 * Default 94, Phytec driver says:
255 * "width + horizontal blank >= 660"
256 */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300257 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300258 rect.width > 660 - 43 ? 43 :
259 660 - rect.width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300260 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300261 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300262 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300263 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300264 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300265 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300266 rect.height + mt9v022->y_skip_top);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300267
268 if (ret < 0)
269 return ret;
270
Márton Némethe26b3142010-02-24 17:13:29 -0300271 dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300272
273 mt9v022->rect = rect;
274
275 return 0;
276}
277
278static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
279{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300280 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300281 struct mt9v022 *mt9v022 = to_mt9v022(client);
282
283 a->c = mt9v022->rect;
284 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
285
286 return 0;
287}
288
289static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
290{
291 a->bounds.left = MT9V022_COLUMN_SKIP;
292 a->bounds.top = MT9V022_ROW_SKIP;
293 a->bounds.width = MT9V022_MAX_WIDTH;
294 a->bounds.height = MT9V022_MAX_HEIGHT;
295 a->defrect = a->bounds;
296 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
297 a->pixelaspect.numerator = 1;
298 a->pixelaspect.denominator = 1;
299
300 return 0;
301}
302
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300303static int mt9v022_g_fmt(struct v4l2_subdev *sd,
304 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300305{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300306 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300307 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300308
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300309 mf->width = mt9v022->rect.width;
310 mf->height = mt9v022->rect.height;
311 mf->code = mt9v022->fmt->code;
312 mf->colorspace = mt9v022->fmt->colorspace;
313 mf->field = V4L2_FIELD_NONE;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300314
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300315 return 0;
316}
317
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300318static int mt9v022_s_fmt(struct v4l2_subdev *sd,
319 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300320{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300321 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300322 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300323 struct v4l2_crop a = {
324 .c = {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300325 .left = mt9v022->rect.left,
326 .top = mt9v022->rect.top,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300327 .width = mf->width,
328 .height = mf->height,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300329 },
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300330 };
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300331 int ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300332
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300333 /*
334 * The caller provides a supported format, as verified per call to
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300335 * .try_mbus_fmt(), datawidth is from our supported format list
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300336 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300337 switch (mf->code) {
Laurent Pinchart076704332010-09-28 07:01:44 -0300338 case V4L2_MBUS_FMT_Y8_1X8:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300339 case V4L2_MBUS_FMT_Y10_1X10:
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300340 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
341 return -EINVAL;
342 break;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300343 case V4L2_MBUS_FMT_SBGGR8_1X8:
344 case V4L2_MBUS_FMT_SBGGR10_1X10:
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300345 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
346 return -EINVAL;
347 break;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300348 default:
349 return -EINVAL;
350 }
351
352 /* No support for scaling on this camera, just crop. */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300353 ret = mt9v022_s_crop(sd, &a);
354 if (!ret) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300355 mf->width = mt9v022->rect.width;
356 mf->height = mt9v022->rect.height;
357 mt9v022->fmt = mt9v022_find_datafmt(mf->code,
358 mt9v022->fmts, mt9v022->num_fmts);
359 mf->colorspace = mt9v022->fmt->colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300360 }
361
362 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300363}
364
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300365static int mt9v022_try_fmt(struct v4l2_subdev *sd,
366 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300367{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300368 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300369 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300370 const struct mt9v022_datafmt *fmt;
371 int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
372 mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300373
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300374 v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300375 MT9V022_MAX_WIDTH, align,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300376 &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300377 MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300378
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300379 fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
380 mt9v022->num_fmts);
381 if (!fmt) {
382 fmt = mt9v022->fmt;
383 mf->code = fmt->code;
384 }
385
386 mf->colorspace = fmt->colorspace;
387
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300388 return 0;
389}
390
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300391static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
392 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300393{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300394 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300395 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300396
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300397 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300398 return -EINVAL;
399
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300400 if (id->match.addr != client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300401 return -ENODEV;
402
403 id->ident = mt9v022->model;
404 id->revision = 0;
405
406 return 0;
407}
408
409#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300410static int mt9v022_g_register(struct v4l2_subdev *sd,
411 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300412{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300413 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300414
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300415 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300416 return -EINVAL;
417
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300418 if (reg->match.addr != client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300419 return -ENODEV;
420
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300421 reg->size = 2;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300422 reg->val = reg_read(client, reg->reg);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300423
424 if (reg->val > 0xffff)
425 return -EIO;
426
427 return 0;
428}
429
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300430static int mt9v022_s_register(struct v4l2_subdev *sd,
431 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300432{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300433 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300434
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300435 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300436 return -EINVAL;
437
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300438 if (reg->match.addr != client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300439 return -ENODEV;
440
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300441 if (reg_write(client, reg->reg, reg->val) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300442 return -EIO;
443
444 return 0;
445}
446#endif
447
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300448static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300449{
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300450 struct mt9v022 *mt9v022 = container_of(ctrl->handler,
451 struct mt9v022, hdl);
452 struct v4l2_subdev *sd = &mt9v022->subdev;
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300453 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300454 struct v4l2_ctrl *gain = mt9v022->gain;
455 struct v4l2_ctrl *exp = mt9v022->exposure;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300456 unsigned long range;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300457 int data;
458
459 switch (ctrl->id) {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300460 case V4L2_CID_AUTOGAIN:
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300461 data = reg_read(client, MT9V022_ANALOG_GAIN);
462 if (data < 0)
463 return -EIO;
464
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300465 range = gain->maximum - gain->minimum;
466 gain->val = ((data - 16) * range + 24) / 48 + gain->minimum;
467 return 0;
468 case V4L2_CID_EXPOSURE_AUTO:
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300469 data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
470 if (data < 0)
471 return -EIO;
472
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300473 range = exp->maximum - exp->minimum;
474 exp->val = ((data - 1) * range + 239) / 479 + exp->minimum;
475 return 0;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300476 }
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300477 return -EINVAL;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300478}
479
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300480static int mt9v022_s_ctrl(struct v4l2_ctrl *ctrl)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300481{
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300482 struct mt9v022 *mt9v022 = container_of(ctrl->handler,
483 struct mt9v022, hdl);
484 struct v4l2_subdev *sd = &mt9v022->subdev;
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300485 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300486 int data;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300487
488 switch (ctrl->id) {
489 case V4L2_CID_VFLIP:
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300490 if (ctrl->val)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300491 data = reg_set(client, MT9V022_READ_MODE, 0x10);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300492 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300493 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300494 if (data < 0)
495 return -EIO;
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300496 return 0;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300497 case V4L2_CID_HFLIP:
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300498 if (ctrl->val)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300499 data = reg_set(client, MT9V022_READ_MODE, 0x20);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300500 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300501 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300502 if (data < 0)
503 return -EIO;
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300504 return 0;
505 case V4L2_CID_AUTOGAIN:
506 if (ctrl->val) {
507 if (reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
508 return -EIO;
509 } else {
510 struct v4l2_ctrl *gain = mt9v022->gain;
511 /* mt9v022 has minimum == default */
512 unsigned long range = gain->maximum - gain->minimum;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300513 /* Valid values 16 to 64, 32 to 64 must be even. */
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300514 unsigned long gain_val = ((gain->val - gain->minimum) *
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300515 48 + range / 2) / range + 16;
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300516
517 if (gain_val >= 32)
518 gain_val &= ~1;
519
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300520 /*
521 * The user wants to set gain manually, hope, she
522 * knows, what she's doing... Switch AGC off.
523 */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300524 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300525 return -EIO;
526
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300527 dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300528 reg_read(client, MT9V022_ANALOG_GAIN), gain_val);
529 if (reg_write(client, MT9V022_ANALOG_GAIN, gain_val) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300530 return -EIO;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300531 }
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300532 return 0;
533 case V4L2_CID_EXPOSURE_AUTO:
534 if (ctrl->val == V4L2_EXPOSURE_AUTO) {
535 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
536 } else {
537 struct v4l2_ctrl *exp = mt9v022->exposure;
538 unsigned long range = exp->maximum - exp->minimum;
539 unsigned long shutter = ((exp->val - exp->minimum) *
540 479 + range / 2) / range + 1;
541
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300542 /*
543 * The user wants to set shutter width manually, hope,
544 * she knows, what she's doing... Switch AEC off.
545 */
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300546 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
547 if (data < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300548 return -EIO;
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300549 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300550 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
551 shutter);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300552 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300553 shutter) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300554 return -EIO;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300555 }
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300556 return 0;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300557 }
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300558 return -EINVAL;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300559}
560
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300561/*
562 * Interface active, can use i2c. If it fails, it can indeed mean, that
563 * this wasn't our capture interface, so, we wait for the right one
564 */
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300565static int mt9v022_video_probe(struct i2c_client *client)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300566{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300567 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300568 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300569 s32 data;
570 int ret;
Sascha Hauere958e272009-03-13 06:08:20 -0300571 unsigned long flags;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300572
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300573 /* Read out the chip version register */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300574 data = reg_read(client, MT9V022_CHIP_VERSION);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300575
576 /* must be 0x1311 or 0x1313 */
577 if (data != 0x1311 && data != 0x1313) {
578 ret = -ENODEV;
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300579 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300580 data);
581 goto ei2c;
582 }
583
584 /* Soft reset */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300585 ret = reg_write(client, MT9V022_RESET, 1);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300586 if (ret < 0)
587 goto ei2c;
588 /* 15 clock cycles */
589 udelay(200);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300590 if (reg_read(client, MT9V022_RESET)) {
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300591 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300592 if (ret > 0)
593 ret = -EIO;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300594 goto ei2c;
595 }
596
597 /* Set monochrome or colour sensor type */
598 if (sensor_type && (!strcmp("colour", sensor_type) ||
599 !strcmp("color", sensor_type))) {
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300600 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300601 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300602 mt9v022->fmts = mt9v022_colour_fmts;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300603 } else {
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300604 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300605 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300606 mt9v022->fmts = mt9v022_monochrome_fmts;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300607 }
608
Sascha Hauere958e272009-03-13 06:08:20 -0300609 if (ret < 0)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300610 goto ei2c;
Sascha Hauere958e272009-03-13 06:08:20 -0300611
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300612 mt9v022->num_fmts = 0;
Sascha Hauere958e272009-03-13 06:08:20 -0300613
614 /*
615 * This is a 10bit sensor, so by default we only allow 10bit.
616 * The platform may support different bus widths due to
617 * different routing of the data lines.
618 */
619 if (icl->query_bus_param)
620 flags = icl->query_bus_param(icl);
621 else
622 flags = SOCAM_DATAWIDTH_10;
623
624 if (flags & SOCAM_DATAWIDTH_10)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300625 mt9v022->num_fmts++;
Sascha Hauere958e272009-03-13 06:08:20 -0300626 else
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300627 mt9v022->fmts++;
Sascha Hauere958e272009-03-13 06:08:20 -0300628
629 if (flags & SOCAM_DATAWIDTH_8)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300630 mt9v022->num_fmts++;
Sascha Hauere958e272009-03-13 06:08:20 -0300631
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300632 mt9v022->fmt = &mt9v022->fmts[0];
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300633
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300634 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300635 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
636 "monochrome" : "colour");
637
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300638 ret = mt9v022_init(client);
639 if (ret < 0)
640 dev_err(&client->dev, "Failed to initialise the camera\n");
641
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300642ei2c:
643 return ret;
644}
645
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300646static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
647{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300648 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300649 struct mt9v022 *mt9v022 = to_mt9v022(client);
650
651 *lines = mt9v022->y_skip_top;
652
653 return 0;
654}
655
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300656static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
657 .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
658 .s_ctrl = mt9v022_s_ctrl,
659};
660
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300661static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300662 .g_chip_ident = mt9v022_g_chip_ident,
663#ifdef CONFIG_VIDEO_ADV_DEBUG
664 .g_register = mt9v022_g_register,
665 .s_register = mt9v022_s_register,
666#endif
667};
668
Hans Verkuil3805f202010-05-08 17:55:00 -0300669static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300670 enum v4l2_mbus_pixelcode *code)
671{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300672 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300673 struct mt9v022 *mt9v022 = to_mt9v022(client);
674
Hans Verkuil3805f202010-05-08 17:55:00 -0300675 if (index >= mt9v022->num_fmts)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300676 return -EINVAL;
677
678 *code = mt9v022->fmts[index].code;
679 return 0;
680}
681
Guennadi Liakhovetskie8e2c702011-07-27 05:53:33 -0300682static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
683 struct v4l2_mbus_config *cfg)
684{
685 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300686 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
Guennadi Liakhovetskie8e2c702011-07-27 05:53:33 -0300687
688 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
689 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
690 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
691 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
692 V4L2_MBUS_DATA_ACTIVE_HIGH;
693 cfg->type = V4L2_MBUS_PARALLEL;
694 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
695
696 return 0;
697}
698
699static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
700 const struct v4l2_mbus_config *cfg)
701{
702 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski443f4832011-09-09 07:06:50 -0300703 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
Guennadi Liakhovetskie8e2c702011-07-27 05:53:33 -0300704 struct mt9v022 *mt9v022 = to_mt9v022(client);
705 unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
Guennadi Liakhovetski443f4832011-09-09 07:06:50 -0300706 unsigned int bps = soc_mbus_get_fmtdesc(mt9v022->fmt->code)->bits_per_sample;
Guennadi Liakhovetskie8e2c702011-07-27 05:53:33 -0300707 int ret;
708 u16 pixclk = 0;
709
Guennadi Liakhovetskie8e2c702011-07-27 05:53:33 -0300710 if (icl->set_bus_param) {
711 ret = icl->set_bus_param(icl, 1 << (bps - 1));
712 if (ret)
713 return ret;
714 } else if (bps != 10) {
715 /*
716 * Without board specific bus width settings we only support the
717 * sensors native bus width
718 */
719 return -EINVAL;
720 }
721
722 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
723 pixclk |= 0x10;
724
725 if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
726 pixclk |= 0x1;
727
728 if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
729 pixclk |= 0x2;
730
731 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
732 if (ret < 0)
733 return ret;
734
735 if (!(flags & V4L2_MBUS_MASTER))
736 mt9v022->chip_control &= ~0x8;
737
738 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
739 if (ret < 0)
740 return ret;
741
742 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
743 pixclk, mt9v022->chip_control);
744
745 return 0;
746}
747
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300748static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
749 .s_stream = mt9v022_s_stream,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300750 .s_mbus_fmt = mt9v022_s_fmt,
751 .g_mbus_fmt = mt9v022_g_fmt,
752 .try_mbus_fmt = mt9v022_try_fmt,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300753 .s_crop = mt9v022_s_crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300754 .g_crop = mt9v022_g_crop,
755 .cropcap = mt9v022_cropcap,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300756 .enum_mbus_fmt = mt9v022_enum_fmt,
Guennadi Liakhovetskie8e2c702011-07-27 05:53:33 -0300757 .g_mbus_config = mt9v022_g_mbus_config,
758 .s_mbus_config = mt9v022_s_mbus_config,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300759};
760
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300761static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
762 .g_skip_top_lines = mt9v022_g_skip_top_lines,
763};
764
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300765static struct v4l2_subdev_ops mt9v022_subdev_ops = {
766 .core = &mt9v022_subdev_core_ops,
767 .video = &mt9v022_subdev_video_ops,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300768 .sensor = &mt9v022_subdev_sensor_ops,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300769};
770
Jean Delvared2653e92008-04-29 23:11:39 +0200771static int mt9v022_probe(struct i2c_client *client,
772 const struct i2c_device_id *did)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300773{
774 struct mt9v022 *mt9v022;
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300775 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300776 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300777 int ret;
778
779 if (!icl) {
780 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
781 return -EINVAL;
782 }
783
784 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
785 dev_warn(&adapter->dev,
786 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
787 return -EIO;
788 }
789
790 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
791 if (!mt9v022)
792 return -ENOMEM;
793
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300794 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300795 v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
796 v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
797 V4L2_CID_VFLIP, 0, 1, 1, 0);
798 v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
799 V4L2_CID_HFLIP, 0, 1, 1, 0);
800 mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
801 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
802 mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
803 V4L2_CID_GAIN, 0, 127, 1, 64);
804
805 /*
806 * Simulated autoexposure. If enabled, we calculate shutter width
807 * ourselves in the driver based on vertical blanking and frame width
808 */
809 mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
810 &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
811 V4L2_EXPOSURE_AUTO);
812 mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
813 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
814
815 mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
816 if (mt9v022->hdl.error) {
817 int err = mt9v022->hdl.error;
818
819 kfree(mt9v022);
820 return err;
821 }
822 v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
823 V4L2_EXPOSURE_MANUAL, true);
824 v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300825
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300826 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300827
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300828 /*
829 * MT9V022 _really_ corrupts the first read out line.
830 * TODO: verify on i.MX31
831 */
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300832 mt9v022->y_skip_top = 1;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300833 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
834 mt9v022->rect.top = MT9V022_ROW_SKIP;
835 mt9v022->rect.width = MT9V022_MAX_WIDTH;
836 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
837
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300838 ret = mt9v022_video_probe(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300839 if (ret) {
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300840 v4l2_ctrl_handler_free(&mt9v022->hdl);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300841 kfree(mt9v022);
842 }
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300843
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300844 return ret;
845}
846
847static int mt9v022_remove(struct i2c_client *client)
848{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300849 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300850 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300851
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300852 v4l2_device_unregister_subdev(&mt9v022->subdev);
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300853 if (icl->free_bus)
854 icl->free_bus(icl);
Hans Verkuilab7b50a2011-09-07 05:22:39 -0300855 v4l2_ctrl_handler_free(&mt9v022->hdl);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300856 kfree(mt9v022);
857
858 return 0;
859}
Jean Delvare3760f732008-04-29 23:11:40 +0200860static const struct i2c_device_id mt9v022_id[] = {
861 { "mt9v022", 0 },
862 { }
863};
864MODULE_DEVICE_TABLE(i2c, mt9v022_id);
865
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300866static struct i2c_driver mt9v022_i2c_driver = {
867 .driver = {
868 .name = "mt9v022",
869 },
870 .probe = mt9v022_probe,
871 .remove = mt9v022_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200872 .id_table = mt9v022_id,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300873};
874
Axel Linc6e8d862012-02-12 06:56:32 -0300875module_i2c_driver(mt9v022_i2c_driver);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300876
877MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
878MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
879MODULE_LICENSE("GPL");