blob: 2fc6ca22f31c7b0c80310c35756a696e663585d4 [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>
16
Guennadi Liakhovetskie8e2c702011-07-27 05:53:33 -030017#include <media/soc_camera.h>
18#include <media/soc_mediabus.h>
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030019#include <media/v4l2-subdev.h>
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030020#include <media/v4l2-chip-ident.h>
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030021
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030022/*
23 * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030024 * The platform has to define ctruct i2c_board_info objects and link to them
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030025 * from struct soc_camera_link
26 */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030027
28static char *sensor_type;
29module_param(sensor_type, charp, S_IRUGO);
Niels de Vos61a2d072008-07-31 00:07:23 -070030MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030031
32/* mt9v022 selected register addresses */
33#define MT9V022_CHIP_VERSION 0x00
34#define MT9V022_COLUMN_START 0x01
35#define MT9V022_ROW_START 0x02
36#define MT9V022_WINDOW_HEIGHT 0x03
37#define MT9V022_WINDOW_WIDTH 0x04
38#define MT9V022_HORIZONTAL_BLANKING 0x05
39#define MT9V022_VERTICAL_BLANKING 0x06
40#define MT9V022_CHIP_CONTROL 0x07
41#define MT9V022_SHUTTER_WIDTH1 0x08
42#define MT9V022_SHUTTER_WIDTH2 0x09
43#define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
44#define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
45#define MT9V022_RESET 0x0c
46#define MT9V022_READ_MODE 0x0d
47#define MT9V022_MONITOR_MODE 0x0e
48#define MT9V022_PIXEL_OPERATION_MODE 0x0f
49#define MT9V022_LED_OUT_CONTROL 0x1b
50#define MT9V022_ADC_MODE_CONTROL 0x1c
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -030051#define MT9V022_ANALOG_GAIN 0x35
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030052#define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
53#define MT9V022_PIXCLK_FV_LV 0x74
54#define MT9V022_DIGITAL_TEST_PATTERN 0x7f
55#define MT9V022_AEC_AGC_ENABLE 0xAF
56#define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
57
58/* Progressive scan, master, defaults */
59#define MT9V022_CHIP_CONTROL_DEFAULT 0x188
60
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030061#define MT9V022_MAX_WIDTH 752
62#define MT9V022_MAX_HEIGHT 480
63#define MT9V022_MIN_WIDTH 48
64#define MT9V022_MIN_HEIGHT 32
65#define MT9V022_COLUMN_SKIP 1
66#define MT9V022_ROW_SKIP 4
67
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030068/* MT9V022 has only one fixed colorspace per pixelcode */
69struct mt9v022_datafmt {
70 enum v4l2_mbus_pixelcode code;
71 enum v4l2_colorspace colorspace;
72};
73
74/* Find a data format by a pixel code in an array */
75static const struct mt9v022_datafmt *mt9v022_find_datafmt(
76 enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
77 int n)
78{
79 int i;
80 for (i = 0; i < n; i++)
81 if (fmt[i].code == code)
82 return fmt + i;
83
84 return NULL;
85}
86
87static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030088 /*
89 * Order important: first natively supported,
90 * second supported with a GPIO extender
91 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030092 {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
93 {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030094};
95
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030096static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030097 /* Order important - see above */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030098 {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
Laurent Pinchart076704332010-09-28 07:01:44 -030099 {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300100};
101
102struct mt9v022 {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300103 struct v4l2_subdev subdev;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300104 struct v4l2_rect rect; /* Sensor window */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300105 const struct mt9v022_datafmt *fmt;
106 const struct mt9v022_datafmt *fmts;
107 int num_fmts;
Guennadi Liakhovetski7fb0fd02008-05-05 14:12:30 -0300108 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300109 u16 chip_control;
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300110 unsigned short y_skip_top; /* Lines to skip at the top */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300111};
112
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300113static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
114{
115 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
116}
117
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300118static int reg_read(struct i2c_client *client, const u8 reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300119{
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300120 s32 data = i2c_smbus_read_word_data(client, reg);
121 return data < 0 ? data : swab16(data);
122}
123
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300124static int reg_write(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300125 const u16 data)
126{
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300127 return i2c_smbus_write_word_data(client, reg, swab16(data));
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300128}
129
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300130static int reg_set(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300131 const u16 data)
132{
133 int ret;
134
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300135 ret = reg_read(client, reg);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300136 if (ret < 0)
137 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300138 return reg_write(client, reg, ret | data);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300139}
140
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300141static int reg_clear(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300142 const u16 data)
143{
144 int ret;
145
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300146 ret = reg_read(client, reg);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300147 if (ret < 0)
148 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300149 return reg_write(client, reg, ret & ~data);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300150}
151
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300152static int mt9v022_init(struct i2c_client *client)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300153{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300154 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300155 int ret;
156
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300157 /*
158 * Almost the default mode: master, parallel, simultaneous, and an
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300159 * undocumented bit 0x200, which is present in table 7, but not in 8,
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300160 * plus snapshot mode to disable scan for now
161 */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300162 mt9v022->chip_control |= 0x10;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300163 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300164 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300165 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300166
167 /* All defaults */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300168 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300169 /* AEC, AGC on */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300170 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300171 if (!ret)
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300172 ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
173 if (!ret)
174 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
175 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300176 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300177 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300178 /* default - auto */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300179 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300180 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300181 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300182
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300183 return ret;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300184}
185
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300186static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300187{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300188 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300189 struct mt9v022 *mt9v022 = to_mt9v022(client);
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300190
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300191 if (enable)
192 /* Switch to master "normal" mode */
193 mt9v022->chip_control &= ~0x10;
194 else
195 /* Switch to snapshot mode */
196 mt9v022->chip_control |= 0x10;
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300197
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300198 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300199 return -EIO;
200 return 0;
201}
202
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300203static int mt9v022_set_bus_param(struct soc_camera_device *icd,
204 unsigned long flags)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300205{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300206 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300207 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300208 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300209 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300210 int ret;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300211 u16 pixclk = 0;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300212
213 /* Only one width bit may be set */
214 if (!is_power_of_2(width_flag))
215 return -EINVAL;
216
Sascha Hauere958e272009-03-13 06:08:20 -0300217 if (icl->set_bus_param) {
218 ret = icl->set_bus_param(icl, width_flag);
219 if (ret)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300220 return ret;
Sascha Hauere958e272009-03-13 06:08:20 -0300221 } else {
222 /*
223 * Without board specific bus width settings we only support the
224 * sensors native bus width
225 */
226 if (width_flag != SOCAM_DATAWIDTH_10)
227 return -EINVAL;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300228 }
229
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300230 flags = soc_camera_apply_sensor_flags(icl, flags);
231
Teresa Gámeza1de7a02011-04-14 11:39:57 -0300232 if (flags & SOCAM_PCLK_SAMPLE_FALLING)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300233 pixclk |= 0x10;
234
235 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
236 pixclk |= 0x1;
237
238 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
239 pixclk |= 0x2;
240
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300241 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300242 if (ret < 0)
243 return ret;
244
245 if (!(flags & SOCAM_MASTER))
246 mt9v022->chip_control &= ~0x8;
247
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300248 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300249 if (ret < 0)
250 return ret;
251
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300252 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300253 pixclk, mt9v022->chip_control);
254
255 return 0;
256}
257
258static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
259{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300260 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski4a9ce752010-02-04 11:17:48 +0100261 unsigned int flags = SOCAM_MASTER | SOCAM_SLAVE |
262 SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300263 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
264 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
Guennadi Liakhovetski4a9ce752010-02-04 11:17:48 +0100265 SOCAM_DATA_ACTIVE_HIGH;
266
267 if (icl->query_bus_param)
268 flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
269 else
270 flags |= SOCAM_DATAWIDTH_10;
271
272 return soc_camera_apply_sensor_flags(icl, flags);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300273}
274
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300275static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300276{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300277 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300278 struct mt9v022 *mt9v022 = to_mt9v022(client);
279 struct v4l2_rect rect = a->c;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300280 int ret;
281
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300282 /* Bayer format - even size lengths */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300283 if (mt9v022->fmts == mt9v022_colour_fmts) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300284 rect.width = ALIGN(rect.width, 2);
285 rect.height = ALIGN(rect.height, 2);
286 /* Let the user play with the starting pixel */
287 }
288
289 soc_camera_limit_side(&rect.left, &rect.width,
290 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
291
292 soc_camera_limit_side(&rect.top, &rect.height,
293 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
294
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300295 /* Like in example app. Contradicts the datasheet though */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300296 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300297 if (ret >= 0) {
298 if (ret & 1) /* Autoexposure */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300299 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300300 rect.height + mt9v022->y_skip_top + 43);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300301 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300302 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300303 rect.height + mt9v022->y_skip_top + 43);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300304 }
305 /* Setup frame format: defaults apart from width and height */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300306 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300307 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300308 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300309 ret = reg_write(client, MT9V022_ROW_START, rect.top);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300310 if (!ret)
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300311 /*
312 * Default 94, Phytec driver says:
313 * "width + horizontal blank >= 660"
314 */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300315 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300316 rect.width > 660 - 43 ? 43 :
317 660 - rect.width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300318 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300319 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300320 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300321 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300322 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300323 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300324 rect.height + mt9v022->y_skip_top);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300325
326 if (ret < 0)
327 return ret;
328
Márton Némethe26b3142010-02-24 17:13:29 -0300329 dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300330
331 mt9v022->rect = rect;
332
333 return 0;
334}
335
336static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
337{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300338 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300339 struct mt9v022 *mt9v022 = to_mt9v022(client);
340
341 a->c = mt9v022->rect;
342 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
343
344 return 0;
345}
346
347static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
348{
349 a->bounds.left = MT9V022_COLUMN_SKIP;
350 a->bounds.top = MT9V022_ROW_SKIP;
351 a->bounds.width = MT9V022_MAX_WIDTH;
352 a->bounds.height = MT9V022_MAX_HEIGHT;
353 a->defrect = a->bounds;
354 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
355 a->pixelaspect.numerator = 1;
356 a->pixelaspect.denominator = 1;
357
358 return 0;
359}
360
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300361static int mt9v022_g_fmt(struct v4l2_subdev *sd,
362 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300363{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300364 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300365 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300366
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300367 mf->width = mt9v022->rect.width;
368 mf->height = mt9v022->rect.height;
369 mf->code = mt9v022->fmt->code;
370 mf->colorspace = mt9v022->fmt->colorspace;
371 mf->field = V4L2_FIELD_NONE;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300372
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300373 return 0;
374}
375
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300376static int mt9v022_s_fmt(struct v4l2_subdev *sd,
377 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300378{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300379 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300380 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300381 struct v4l2_crop a = {
382 .c = {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300383 .left = mt9v022->rect.left,
384 .top = mt9v022->rect.top,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300385 .width = mf->width,
386 .height = mf->height,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300387 },
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300388 };
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300389 int ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300390
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300391 /*
392 * The caller provides a supported format, as verified per call to
393 * icd->try_fmt(), datawidth is from our supported format list
394 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300395 switch (mf->code) {
Laurent Pinchart076704332010-09-28 07:01:44 -0300396 case V4L2_MBUS_FMT_Y8_1X8:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300397 case V4L2_MBUS_FMT_Y10_1X10:
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300398 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
399 return -EINVAL;
400 break;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300401 case V4L2_MBUS_FMT_SBGGR8_1X8:
402 case V4L2_MBUS_FMT_SBGGR10_1X10:
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300403 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
404 return -EINVAL;
405 break;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300406 default:
407 return -EINVAL;
408 }
409
410 /* No support for scaling on this camera, just crop. */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300411 ret = mt9v022_s_crop(sd, &a);
412 if (!ret) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300413 mf->width = mt9v022->rect.width;
414 mf->height = mt9v022->rect.height;
415 mt9v022->fmt = mt9v022_find_datafmt(mf->code,
416 mt9v022->fmts, mt9v022->num_fmts);
417 mf->colorspace = mt9v022->fmt->colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300418 }
419
420 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300421}
422
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300423static int mt9v022_try_fmt(struct v4l2_subdev *sd,
424 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300425{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300426 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300427 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300428 const struct mt9v022_datafmt *fmt;
429 int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
430 mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300431
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300432 v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300433 MT9V022_MAX_WIDTH, align,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300434 &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300435 MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300436
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300437 fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
438 mt9v022->num_fmts);
439 if (!fmt) {
440 fmt = mt9v022->fmt;
441 mf->code = fmt->code;
442 }
443
444 mf->colorspace = fmt->colorspace;
445
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300446 return 0;
447}
448
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300449static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
450 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300451{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300452 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300453 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300454
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300455 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300456 return -EINVAL;
457
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300458 if (id->match.addr != client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300459 return -ENODEV;
460
461 id->ident = mt9v022->model;
462 id->revision = 0;
463
464 return 0;
465}
466
467#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300468static int mt9v022_g_register(struct v4l2_subdev *sd,
469 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300470{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300471 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300472
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300473 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300474 return -EINVAL;
475
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300476 if (reg->match.addr != client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300477 return -ENODEV;
478
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300479 reg->size = 2;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300480 reg->val = reg_read(client, reg->reg);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300481
482 if (reg->val > 0xffff)
483 return -EIO;
484
485 return 0;
486}
487
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300488static int mt9v022_s_register(struct v4l2_subdev *sd,
489 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300490{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300491 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300492
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300493 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300494 return -EINVAL;
495
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300496 if (reg->match.addr != client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300497 return -ENODEV;
498
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300499 if (reg_write(client, reg->reg, reg->val) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300500 return -EIO;
501
502 return 0;
503}
504#endif
505
Adrian Bunk4407a462008-04-28 17:13:51 -0300506static const struct v4l2_queryctrl mt9v022_controls[] = {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300507 {
508 .id = V4L2_CID_VFLIP,
509 .type = V4L2_CTRL_TYPE_BOOLEAN,
510 .name = "Flip Vertically",
511 .minimum = 0,
512 .maximum = 1,
513 .step = 1,
514 .default_value = 0,
515 }, {
516 .id = V4L2_CID_HFLIP,
517 .type = V4L2_CTRL_TYPE_BOOLEAN,
518 .name = "Flip Horizontally",
519 .minimum = 0,
520 .maximum = 1,
521 .step = 1,
522 .default_value = 0,
523 }, {
524 .id = V4L2_CID_GAIN,
525 .type = V4L2_CTRL_TYPE_INTEGER,
526 .name = "Analog Gain",
527 .minimum = 64,
528 .maximum = 127,
529 .step = 1,
530 .default_value = 64,
531 .flags = V4L2_CTRL_FLAG_SLIDER,
532 }, {
533 .id = V4L2_CID_EXPOSURE,
534 .type = V4L2_CTRL_TYPE_INTEGER,
535 .name = "Exposure",
536 .minimum = 1,
537 .maximum = 255,
538 .step = 1,
539 .default_value = 255,
540 .flags = V4L2_CTRL_FLAG_SLIDER,
541 }, {
542 .id = V4L2_CID_AUTOGAIN,
543 .type = V4L2_CTRL_TYPE_BOOLEAN,
544 .name = "Automatic Gain",
545 .minimum = 0,
546 .maximum = 1,
547 .step = 1,
548 .default_value = 1,
549 }, {
550 .id = V4L2_CID_EXPOSURE_AUTO,
551 .type = V4L2_CTRL_TYPE_BOOLEAN,
552 .name = "Automatic Exposure",
553 .minimum = 0,
554 .maximum = 1,
555 .step = 1,
556 .default_value = 1,
557 }
558};
559
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300560static struct soc_camera_ops mt9v022_ops = {
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300561 .set_bus_param = mt9v022_set_bus_param,
562 .query_bus_param = mt9v022_query_bus_param,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300563 .controls = mt9v022_controls,
564 .num_controls = ARRAY_SIZE(mt9v022_controls),
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300565};
566
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300567static int mt9v022_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300568{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300569 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300570 const struct v4l2_queryctrl *qctrl;
571 unsigned long range;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300572 int data;
573
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300574 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
575
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300576 switch (ctrl->id) {
577 case V4L2_CID_VFLIP:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300578 data = reg_read(client, MT9V022_READ_MODE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300579 if (data < 0)
580 return -EIO;
581 ctrl->value = !!(data & 0x10);
582 break;
583 case V4L2_CID_HFLIP:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300584 data = reg_read(client, MT9V022_READ_MODE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300585 if (data < 0)
586 return -EIO;
587 ctrl->value = !!(data & 0x20);
588 break;
589 case V4L2_CID_EXPOSURE_AUTO:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300590 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300591 if (data < 0)
592 return -EIO;
593 ctrl->value = !!(data & 0x1);
594 break;
595 case V4L2_CID_AUTOGAIN:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300596 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300597 if (data < 0)
598 return -EIO;
599 ctrl->value = !!(data & 0x2);
600 break;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300601 case V4L2_CID_GAIN:
602 data = reg_read(client, MT9V022_ANALOG_GAIN);
603 if (data < 0)
604 return -EIO;
605
606 range = qctrl->maximum - qctrl->minimum;
607 ctrl->value = ((data - 16) * range + 24) / 48 + qctrl->minimum;
608
609 break;
610 case V4L2_CID_EXPOSURE:
611 data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
612 if (data < 0)
613 return -EIO;
614
615 range = qctrl->maximum - qctrl->minimum;
616 ctrl->value = ((data - 1) * range + 239) / 479 + qctrl->minimum;
617
618 break;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300619 }
620 return 0;
621}
622
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300623static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300624{
625 int data;
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300626 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300627 const struct v4l2_queryctrl *qctrl;
628
629 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300630 if (!qctrl)
631 return -EINVAL;
632
633 switch (ctrl->id) {
634 case V4L2_CID_VFLIP:
635 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300636 data = reg_set(client, MT9V022_READ_MODE, 0x10);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300637 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300638 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300639 if (data < 0)
640 return -EIO;
641 break;
642 case V4L2_CID_HFLIP:
643 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300644 data = reg_set(client, MT9V022_READ_MODE, 0x20);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300645 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300646 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300647 if (data < 0)
648 return -EIO;
649 break;
650 case V4L2_CID_GAIN:
651 /* mt9v022 has minimum == default */
652 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
653 return -EINVAL;
654 else {
655 unsigned long range = qctrl->maximum - qctrl->minimum;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300656 /* Valid values 16 to 64, 32 to 64 must be even. */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300657 unsigned long gain = ((ctrl->value - qctrl->minimum) *
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300658 48 + range / 2) / range + 16;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300659 if (gain >= 32)
660 gain &= ~1;
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300661 /*
662 * The user wants to set gain manually, hope, she
663 * knows, what she's doing... Switch AGC off.
664 */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300665
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300666 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300667 return -EIO;
668
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300669 dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
670 reg_read(client, MT9V022_ANALOG_GAIN), gain);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300671 if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300672 return -EIO;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300673 }
674 break;
675 case V4L2_CID_EXPOSURE:
676 /* mt9v022 has maximum == default */
677 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
678 return -EINVAL;
679 else {
680 unsigned long range = qctrl->maximum - qctrl->minimum;
681 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
682 479 + range / 2) / range + 1;
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300683 /*
684 * The user wants to set shutter width manually, hope,
685 * she knows, what she's doing... Switch AEC off.
686 */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300687
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300688 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300689 return -EIO;
690
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300691 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300692 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300693 shutter);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300694 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300695 shutter) < 0)
696 return -EIO;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300697 }
698 break;
699 case V4L2_CID_AUTOGAIN:
700 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300701 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300702 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300703 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300704 if (data < 0)
705 return -EIO;
706 break;
707 case V4L2_CID_EXPOSURE_AUTO:
708 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300709 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300710 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300711 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300712 if (data < 0)
713 return -EIO;
714 break;
715 }
716 return 0;
717}
718
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300719/*
720 * Interface active, can use i2c. If it fails, it can indeed mean, that
721 * this wasn't our capture interface, so, we wait for the right one
722 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300723static int mt9v022_video_probe(struct soc_camera_device *icd,
724 struct i2c_client *client)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300725{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300726 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300727 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300728 s32 data;
729 int ret;
Sascha Hauere958e272009-03-13 06:08:20 -0300730 unsigned long flags;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300731
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300732 /* We must have a parent by now. And it cannot be a wrong one. */
733 BUG_ON(!icd->parent ||
734 to_soc_camera_host(icd->parent)->nr != icd->iface);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300735
736 /* Read out the chip version register */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300737 data = reg_read(client, MT9V022_CHIP_VERSION);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300738
739 /* must be 0x1311 or 0x1313 */
740 if (data != 0x1311 && data != 0x1313) {
741 ret = -ENODEV;
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300742 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300743 data);
744 goto ei2c;
745 }
746
747 /* Soft reset */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300748 ret = reg_write(client, MT9V022_RESET, 1);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300749 if (ret < 0)
750 goto ei2c;
751 /* 15 clock cycles */
752 udelay(200);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300753 if (reg_read(client, MT9V022_RESET)) {
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300754 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300755 if (ret > 0)
756 ret = -EIO;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300757 goto ei2c;
758 }
759
760 /* Set monochrome or colour sensor type */
761 if (sensor_type && (!strcmp("colour", sensor_type) ||
762 !strcmp("color", sensor_type))) {
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300763 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300764 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300765 mt9v022->fmts = mt9v022_colour_fmts;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300766 } else {
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300767 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300768 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300769 mt9v022->fmts = mt9v022_monochrome_fmts;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300770 }
771
Sascha Hauere958e272009-03-13 06:08:20 -0300772 if (ret < 0)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300773 goto ei2c;
Sascha Hauere958e272009-03-13 06:08:20 -0300774
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300775 mt9v022->num_fmts = 0;
Sascha Hauere958e272009-03-13 06:08:20 -0300776
777 /*
778 * This is a 10bit sensor, so by default we only allow 10bit.
779 * The platform may support different bus widths due to
780 * different routing of the data lines.
781 */
782 if (icl->query_bus_param)
783 flags = icl->query_bus_param(icl);
784 else
785 flags = SOCAM_DATAWIDTH_10;
786
787 if (flags & SOCAM_DATAWIDTH_10)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300788 mt9v022->num_fmts++;
Sascha Hauere958e272009-03-13 06:08:20 -0300789 else
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300790 mt9v022->fmts++;
Sascha Hauere958e272009-03-13 06:08:20 -0300791
792 if (flags & SOCAM_DATAWIDTH_8)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300793 mt9v022->num_fmts++;
Sascha Hauere958e272009-03-13 06:08:20 -0300794
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300795 mt9v022->fmt = &mt9v022->fmts[0];
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300796
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300797 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300798 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
799 "monochrome" : "colour");
800
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300801 ret = mt9v022_init(client);
802 if (ret < 0)
803 dev_err(&client->dev, "Failed to initialise the camera\n");
804
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300805ei2c:
806 return ret;
807}
808
809static void mt9v022_video_remove(struct soc_camera_device *icd)
810{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300811 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300812
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300813 dev_dbg(icd->pdev, "Video removed: %p, %p\n",
814 icd->parent, icd->vdev);
Guennadi Liakhovetski594bb462009-04-24 12:53:51 -0300815 if (icl->free_bus)
816 icl->free_bus(icl);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300817}
818
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300819static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
820{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300821 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300822 struct mt9v022 *mt9v022 = to_mt9v022(client);
823
824 *lines = mt9v022->y_skip_top;
825
826 return 0;
827}
828
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300829static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
830 .g_ctrl = mt9v022_g_ctrl,
831 .s_ctrl = mt9v022_s_ctrl,
832 .g_chip_ident = mt9v022_g_chip_ident,
833#ifdef CONFIG_VIDEO_ADV_DEBUG
834 .g_register = mt9v022_g_register,
835 .s_register = mt9v022_s_register,
836#endif
837};
838
Hans Verkuil3805f202010-05-08 17:55:00 -0300839static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300840 enum v4l2_mbus_pixelcode *code)
841{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300842 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300843 struct mt9v022 *mt9v022 = to_mt9v022(client);
844
Hans Verkuil3805f202010-05-08 17:55:00 -0300845 if (index >= mt9v022->num_fmts)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300846 return -EINVAL;
847
848 *code = mt9v022->fmts[index].code;
849 return 0;
850}
851
Guennadi Liakhovetskie8e2c702011-07-27 05:53:33 -0300852static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
853 struct v4l2_mbus_config *cfg)
854{
855 struct i2c_client *client = v4l2_get_subdevdata(sd);
856 struct soc_camera_device *icd = client->dev.platform_data;
857 struct soc_camera_link *icl = to_soc_camera_link(icd);
858
859 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
860 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
861 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
862 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
863 V4L2_MBUS_DATA_ACTIVE_HIGH;
864 cfg->type = V4L2_MBUS_PARALLEL;
865 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
866
867 return 0;
868}
869
870static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
871 const struct v4l2_mbus_config *cfg)
872{
873 struct i2c_client *client = v4l2_get_subdevdata(sd);
874 struct soc_camera_device *icd = client->dev.platform_data;
875 struct soc_camera_link *icl = to_soc_camera_link(icd);
876 struct mt9v022 *mt9v022 = to_mt9v022(client);
877 unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
878 unsigned int bps = soc_mbus_get_fmtdesc(icd->current_fmt->code)->bits_per_sample;
879 int ret;
880 u16 pixclk = 0;
881
882 dev_info(icd->pdev, "set %d: %s, %dbps\n", icd->current_fmt->code,
883 icd->current_fmt->host_fmt->name, bps);
884
885 if (icl->set_bus_param) {
886 ret = icl->set_bus_param(icl, 1 << (bps - 1));
887 if (ret)
888 return ret;
889 } else if (bps != 10) {
890 /*
891 * Without board specific bus width settings we only support the
892 * sensors native bus width
893 */
894 return -EINVAL;
895 }
896
897 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
898 pixclk |= 0x10;
899
900 if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
901 pixclk |= 0x1;
902
903 if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
904 pixclk |= 0x2;
905
906 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
907 if (ret < 0)
908 return ret;
909
910 if (!(flags & V4L2_MBUS_MASTER))
911 mt9v022->chip_control &= ~0x8;
912
913 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
914 if (ret < 0)
915 return ret;
916
917 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
918 pixclk, mt9v022->chip_control);
919
920 return 0;
921}
922
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300923static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
924 .s_stream = mt9v022_s_stream,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300925 .s_mbus_fmt = mt9v022_s_fmt,
926 .g_mbus_fmt = mt9v022_g_fmt,
927 .try_mbus_fmt = mt9v022_try_fmt,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300928 .s_crop = mt9v022_s_crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300929 .g_crop = mt9v022_g_crop,
930 .cropcap = mt9v022_cropcap,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300931 .enum_mbus_fmt = mt9v022_enum_fmt,
Guennadi Liakhovetskie8e2c702011-07-27 05:53:33 -0300932 .g_mbus_config = mt9v022_g_mbus_config,
933 .s_mbus_config = mt9v022_s_mbus_config,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300934};
935
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300936static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
937 .g_skip_top_lines = mt9v022_g_skip_top_lines,
938};
939
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300940static struct v4l2_subdev_ops mt9v022_subdev_ops = {
941 .core = &mt9v022_subdev_core_ops,
942 .video = &mt9v022_subdev_video_ops,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300943 .sensor = &mt9v022_subdev_sensor_ops,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300944};
945
Jean Delvared2653e92008-04-29 23:11:39 +0200946static int mt9v022_probe(struct i2c_client *client,
947 const struct i2c_device_id *did)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300948{
949 struct mt9v022 *mt9v022;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300950 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300951 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300952 struct soc_camera_link *icl;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300953 int ret;
954
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300955 if (!icd) {
956 dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
957 return -EINVAL;
958 }
959
960 icl = to_soc_camera_link(icd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300961 if (!icl) {
962 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
963 return -EINVAL;
964 }
965
966 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
967 dev_warn(&adapter->dev,
968 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
969 return -EIO;
970 }
971
972 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
973 if (!mt9v022)
974 return -ENOMEM;
975
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300976 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
977
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300978 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300979
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300980 icd->ops = &mt9v022_ops;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300981 /*
982 * MT9V022 _really_ corrupts the first read out line.
983 * TODO: verify on i.MX31
984 */
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300985 mt9v022->y_skip_top = 1;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300986 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
987 mt9v022->rect.top = MT9V022_ROW_SKIP;
988 mt9v022->rect.width = MT9V022_MAX_WIDTH;
989 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
990
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300991 ret = mt9v022_video_probe(icd, client);
992 if (ret) {
993 icd->ops = NULL;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300994 kfree(mt9v022);
995 }
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300996
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300997 return ret;
998}
999
1000static int mt9v022_remove(struct i2c_client *client)
1001{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001002 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001003 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -03001004
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001005 icd->ops = NULL;
1006 mt9v022_video_remove(icd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -03001007 kfree(mt9v022);
1008
1009 return 0;
1010}
Jean Delvare3760f732008-04-29 23:11:40 +02001011static const struct i2c_device_id mt9v022_id[] = {
1012 { "mt9v022", 0 },
1013 { }
1014};
1015MODULE_DEVICE_TABLE(i2c, mt9v022_id);
1016
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -03001017static struct i2c_driver mt9v022_i2c_driver = {
1018 .driver = {
1019 .name = "mt9v022",
1020 },
1021 .probe = mt9v022_probe,
1022 .remove = mt9v022_remove,
Jean Delvare3760f732008-04-29 23:11:40 +02001023 .id_table = mt9v022_id,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -03001024};
1025
1026static int __init mt9v022_mod_init(void)
1027{
1028 return i2c_add_driver(&mt9v022_i2c_driver);
1029}
1030
1031static void __exit mt9v022_mod_exit(void)
1032{
1033 i2c_del_driver(&mt9v022_i2c_driver);
1034}
1035
1036module_init(mt9v022_mod_init);
1037module_exit(mt9v022_mod_exit);
1038
1039MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
1040MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1041MODULE_LICENSE("GPL");