blob: 995607f9d3ba97a9caea35e0f4fff014866f001c [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 Liakhovetski979ea1d2009-08-25 11:43:33 -030017#include <media/v4l2-subdev.h>
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030018#include <media/v4l2-chip-ident.h>
19#include <media/soc_camera.h>
20
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030021/* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030022 * The platform has to define ctruct i2c_board_info objects and link to them
23 * from struct soc_camera_link */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030024
25static char *sensor_type;
26module_param(sensor_type, charp, S_IRUGO);
Niels de Vos61a2d072008-07-31 00:07:23 -070027MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030028
29/* mt9v022 selected register addresses */
30#define MT9V022_CHIP_VERSION 0x00
31#define MT9V022_COLUMN_START 0x01
32#define MT9V022_ROW_START 0x02
33#define MT9V022_WINDOW_HEIGHT 0x03
34#define MT9V022_WINDOW_WIDTH 0x04
35#define MT9V022_HORIZONTAL_BLANKING 0x05
36#define MT9V022_VERTICAL_BLANKING 0x06
37#define MT9V022_CHIP_CONTROL 0x07
38#define MT9V022_SHUTTER_WIDTH1 0x08
39#define MT9V022_SHUTTER_WIDTH2 0x09
40#define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
41#define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
42#define MT9V022_RESET 0x0c
43#define MT9V022_READ_MODE 0x0d
44#define MT9V022_MONITOR_MODE 0x0e
45#define MT9V022_PIXEL_OPERATION_MODE 0x0f
46#define MT9V022_LED_OUT_CONTROL 0x1b
47#define MT9V022_ADC_MODE_CONTROL 0x1c
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -030048#define MT9V022_ANALOG_GAIN 0x35
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030049#define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
50#define MT9V022_PIXCLK_FV_LV 0x74
51#define MT9V022_DIGITAL_TEST_PATTERN 0x7f
52#define MT9V022_AEC_AGC_ENABLE 0xAF
53#define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
54
55/* Progressive scan, master, defaults */
56#define MT9V022_CHIP_CONTROL_DEFAULT 0x188
57
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030058#define MT9V022_MAX_WIDTH 752
59#define MT9V022_MAX_HEIGHT 480
60#define MT9V022_MIN_WIDTH 48
61#define MT9V022_MIN_HEIGHT 32
62#define MT9V022_COLUMN_SKIP 1
63#define MT9V022_ROW_SKIP 4
64
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030065static const struct soc_camera_data_format mt9v022_colour_formats[] = {
66 /* Order important: first natively supported,
67 * second supported with a GPIO extender */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030068 {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030069 .name = "Bayer (sRGB) 10 bit",
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030070 .depth = 10,
71 .fourcc = V4L2_PIX_FMT_SBGGR16,
72 .colorspace = V4L2_COLORSPACE_SRGB,
73 }, {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030074 .name = "Bayer (sRGB) 8 bit",
75 .depth = 8,
76 .fourcc = V4L2_PIX_FMT_SBGGR8,
77 .colorspace = V4L2_COLORSPACE_SRGB,
78 }
79};
80
81static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
82 /* Order important - see above */
83 {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030084 .name = "Monochrome 10 bit",
85 .depth = 10,
86 .fourcc = V4L2_PIX_FMT_Y16,
87 }, {
88 .name = "Monochrome 8 bit",
89 .depth = 8,
90 .fourcc = V4L2_PIX_FMT_GREY,
91 },
92};
93
94struct mt9v022 {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030095 struct v4l2_subdev subdev;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030096 struct v4l2_rect rect; /* Sensor window */
97 __u32 fourcc;
Guennadi Liakhovetski7fb0fd02008-05-05 14:12:30 -030098 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030099 u16 chip_control;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300100};
101
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300102static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
103{
104 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
105}
106
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300107static int reg_read(struct i2c_client *client, const u8 reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300108{
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300109 s32 data = i2c_smbus_read_word_data(client, reg);
110 return data < 0 ? data : swab16(data);
111}
112
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300113static int reg_write(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300114 const u16 data)
115{
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300116 return i2c_smbus_write_word_data(client, reg, swab16(data));
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300117}
118
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300119static int reg_set(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300120 const u16 data)
121{
122 int ret;
123
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300124 ret = reg_read(client, reg);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300125 if (ret < 0)
126 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300127 return reg_write(client, reg, ret | data);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300128}
129
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300130static int reg_clear(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 Liakhovetskia4c56fd2009-08-25 11:53:23 -0300141static int mt9v022_init(struct i2c_client *client)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300142{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300143 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300144 int ret;
145
146 /* Almost the default mode: master, parallel, simultaneous, and an
147 * undocumented bit 0x200, which is present in table 7, but not in 8,
148 * plus snapshot mode to disable scan for now */
149 mt9v022->chip_control |= 0x10;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300150 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300151 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300152 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300153
154 /* All defaults */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300155 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300156 /* AEC, AGC on */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300157 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300158 if (!ret)
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300159 ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
160 if (!ret)
161 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
162 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300163 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300164 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300165 /* default - auto */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300166 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300167 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300168 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300169
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300170 return ret;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300171}
172
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300173static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300174{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300175 struct i2c_client *client = sd->priv;
176 struct mt9v022 *mt9v022 = to_mt9v022(client);
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300177
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300178 if (enable)
179 /* Switch to master "normal" mode */
180 mt9v022->chip_control &= ~0x10;
181 else
182 /* Switch to snapshot mode */
183 mt9v022->chip_control |= 0x10;
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300184
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300185 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300186 return -EIO;
187 return 0;
188}
189
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300190static int mt9v022_set_bus_param(struct soc_camera_device *icd,
191 unsigned long flags)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300192{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300193 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300194 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300195 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300196 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300197 int ret;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300198 u16 pixclk = 0;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300199
200 /* Only one width bit may be set */
201 if (!is_power_of_2(width_flag))
202 return -EINVAL;
203
Sascha Hauere958e272009-03-13 06:08:20 -0300204 if (icl->set_bus_param) {
205 ret = icl->set_bus_param(icl, width_flag);
206 if (ret)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300207 return ret;
Sascha Hauere958e272009-03-13 06:08:20 -0300208 } else {
209 /*
210 * Without board specific bus width settings we only support the
211 * sensors native bus width
212 */
213 if (width_flag != SOCAM_DATAWIDTH_10)
214 return -EINVAL;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300215 }
216
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300217 flags = soc_camera_apply_sensor_flags(icl, flags);
218
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300219 if (flags & SOCAM_PCLK_SAMPLE_RISING)
220 pixclk |= 0x10;
221
222 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
223 pixclk |= 0x1;
224
225 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
226 pixclk |= 0x2;
227
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300228 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300229 if (ret < 0)
230 return ret;
231
232 if (!(flags & SOCAM_MASTER))
233 mt9v022->chip_control &= ~0x8;
234
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300235 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300236 if (ret < 0)
237 return ret;
238
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300239 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300240 pixclk, mt9v022->chip_control);
241
242 return 0;
243}
244
245static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
246{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300247 struct soc_camera_link *icl = to_soc_camera_link(icd);
Sascha Hauere958e272009-03-13 06:08:20 -0300248 unsigned int width_flag;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300249
Sascha Hauere958e272009-03-13 06:08:20 -0300250 if (icl->query_bus_param)
251 width_flag = icl->query_bus_param(icl) &
252 SOCAM_DATAWIDTH_MASK;
253 else
254 width_flag = SOCAM_DATAWIDTH_10;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300255
256 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
257 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
258 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
Guennadi Liakhovetski2d9329f2009-02-23 12:12:58 -0300259 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300260 width_flag;
261}
262
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300263static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300264{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300265 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300266 struct mt9v022 *mt9v022 = to_mt9v022(client);
267 struct v4l2_rect rect = a->c;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300268 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300269 int ret;
270
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300271 /* Bayer format - even size lengths */
272 if (mt9v022->fourcc == V4L2_PIX_FMT_SBGGR8 ||
273 mt9v022->fourcc == V4L2_PIX_FMT_SBGGR16) {
274 rect.width = ALIGN(rect.width, 2);
275 rect.height = ALIGN(rect.height, 2);
276 /* Let the user play with the starting pixel */
277 }
278
279 soc_camera_limit_side(&rect.left, &rect.width,
280 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
281
282 soc_camera_limit_side(&rect.top, &rect.height,
283 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
284
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300285 /* Like in example app. Contradicts the datasheet though */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300286 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300287 if (ret >= 0) {
288 if (ret & 1) /* Autoexposure */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300289 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300290 rect.height + icd->y_skip_top + 43);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300291 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300292 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300293 rect.height + icd->y_skip_top + 43);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300294 }
295 /* Setup frame format: defaults apart from width and height */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300296 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300297 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300298 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300299 ret = reg_write(client, MT9V022_ROW_START, rect.top);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300300 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300301 /* Default 94, Phytec driver says:
302 * "width + horizontal blank >= 660" */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300303 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300304 rect.width > 660 - 43 ? 43 :
305 660 - rect.width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300306 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300307 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300308 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300309 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300310 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300311 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300312 rect.height + icd->y_skip_top);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300313
314 if (ret < 0)
315 return ret;
316
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300317 dev_dbg(&client->dev, "Frame %ux%u pixel\n", rect.width, rect.height);
318
319 mt9v022->rect = rect;
320
321 return 0;
322}
323
324static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
325{
326 struct i2c_client *client = sd->priv;
327 struct mt9v022 *mt9v022 = to_mt9v022(client);
328
329 a->c = mt9v022->rect;
330 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
331
332 return 0;
333}
334
335static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
336{
337 a->bounds.left = MT9V022_COLUMN_SKIP;
338 a->bounds.top = MT9V022_ROW_SKIP;
339 a->bounds.width = MT9V022_MAX_WIDTH;
340 a->bounds.height = MT9V022_MAX_HEIGHT;
341 a->defrect = a->bounds;
342 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
343 a->pixelaspect.numerator = 1;
344 a->pixelaspect.denominator = 1;
345
346 return 0;
347}
348
349static int mt9v022_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
350{
351 struct i2c_client *client = sd->priv;
352 struct mt9v022 *mt9v022 = to_mt9v022(client);
353 struct v4l2_pix_format *pix = &f->fmt.pix;
354
355 pix->width = mt9v022->rect.width;
356 pix->height = mt9v022->rect.height;
357 pix->pixelformat = mt9v022->fourcc;
358 pix->field = V4L2_FIELD_NONE;
359 pix->colorspace = V4L2_COLORSPACE_SRGB;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300360
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300361 return 0;
362}
363
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300364static int mt9v022_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300365{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300366 struct i2c_client *client = sd->priv;
367 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300368 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300369 struct v4l2_crop a = {
370 .c = {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300371 .left = mt9v022->rect.left,
372 .top = mt9v022->rect.top,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300373 .width = pix->width,
374 .height = pix->height,
375 },
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300376 };
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300377 int ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300378
379 /* The caller provides a supported format, as verified per call to
380 * icd->try_fmt(), datawidth is from our supported format list */
381 switch (pix->pixelformat) {
382 case V4L2_PIX_FMT_GREY:
383 case V4L2_PIX_FMT_Y16:
384 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
385 return -EINVAL;
386 break;
387 case V4L2_PIX_FMT_SBGGR8:
388 case V4L2_PIX_FMT_SBGGR16:
389 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
390 return -EINVAL;
391 break;
392 case 0:
393 /* No format change, only geometry */
394 break;
395 default:
396 return -EINVAL;
397 }
398
399 /* No support for scaling on this camera, just crop. */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300400 ret = mt9v022_s_crop(sd, &a);
401 if (!ret) {
402 pix->width = mt9v022->rect.width;
403 pix->height = mt9v022->rect.height;
404 mt9v022->fourcc = pix->pixelformat;
405 }
406
407 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300408}
409
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300410static int mt9v022_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300411{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300412 struct i2c_client *client = sd->priv;
413 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300414 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300415 int align = pix->pixelformat == V4L2_PIX_FMT_SBGGR8 ||
416 pix->pixelformat == V4L2_PIX_FMT_SBGGR16;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300417
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300418 v4l_bound_align_image(&pix->width, MT9V022_MIN_WIDTH,
419 MT9V022_MAX_WIDTH, align,
420 &pix->height, MT9V022_MIN_HEIGHT + icd->y_skip_top,
421 MT9V022_MAX_HEIGHT + icd->y_skip_top, align, 0);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300422
423 return 0;
424}
425
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300426static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
427 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300428{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300429 struct i2c_client *client = sd->priv;
430 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300431
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300432 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300433 return -EINVAL;
434
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300435 if (id->match.addr != client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300436 return -ENODEV;
437
438 id->ident = mt9v022->model;
439 id->revision = 0;
440
441 return 0;
442}
443
444#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300445static int mt9v022_g_register(struct v4l2_subdev *sd,
446 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300447{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300448 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300449
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300450 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300451 return -EINVAL;
452
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300453 if (reg->match.addr != client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300454 return -ENODEV;
455
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300456 reg->size = 2;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300457 reg->val = reg_read(client, reg->reg);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300458
459 if (reg->val > 0xffff)
460 return -EIO;
461
462 return 0;
463}
464
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300465static int mt9v022_s_register(struct v4l2_subdev *sd,
466 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300467{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300468 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300469
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300470 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300471 return -EINVAL;
472
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300473 if (reg->match.addr != client->addr)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300474 return -ENODEV;
475
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300476 if (reg_write(client, reg->reg, reg->val) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300477 return -EIO;
478
479 return 0;
480}
481#endif
482
Adrian Bunk4407a462008-04-28 17:13:51 -0300483static const struct v4l2_queryctrl mt9v022_controls[] = {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300484 {
485 .id = V4L2_CID_VFLIP,
486 .type = V4L2_CTRL_TYPE_BOOLEAN,
487 .name = "Flip Vertically",
488 .minimum = 0,
489 .maximum = 1,
490 .step = 1,
491 .default_value = 0,
492 }, {
493 .id = V4L2_CID_HFLIP,
494 .type = V4L2_CTRL_TYPE_BOOLEAN,
495 .name = "Flip Horizontally",
496 .minimum = 0,
497 .maximum = 1,
498 .step = 1,
499 .default_value = 0,
500 }, {
501 .id = V4L2_CID_GAIN,
502 .type = V4L2_CTRL_TYPE_INTEGER,
503 .name = "Analog Gain",
504 .minimum = 64,
505 .maximum = 127,
506 .step = 1,
507 .default_value = 64,
508 .flags = V4L2_CTRL_FLAG_SLIDER,
509 }, {
510 .id = V4L2_CID_EXPOSURE,
511 .type = V4L2_CTRL_TYPE_INTEGER,
512 .name = "Exposure",
513 .minimum = 1,
514 .maximum = 255,
515 .step = 1,
516 .default_value = 255,
517 .flags = V4L2_CTRL_FLAG_SLIDER,
518 }, {
519 .id = V4L2_CID_AUTOGAIN,
520 .type = V4L2_CTRL_TYPE_BOOLEAN,
521 .name = "Automatic Gain",
522 .minimum = 0,
523 .maximum = 1,
524 .step = 1,
525 .default_value = 1,
526 }, {
527 .id = V4L2_CID_EXPOSURE_AUTO,
528 .type = V4L2_CTRL_TYPE_BOOLEAN,
529 .name = "Automatic Exposure",
530 .minimum = 0,
531 .maximum = 1,
532 .step = 1,
533 .default_value = 1,
534 }
535};
536
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300537static struct soc_camera_ops mt9v022_ops = {
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300538 .set_bus_param = mt9v022_set_bus_param,
539 .query_bus_param = mt9v022_query_bus_param,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300540 .controls = mt9v022_controls,
541 .num_controls = ARRAY_SIZE(mt9v022_controls),
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300542};
543
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300544static int mt9v022_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300545{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300546 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300547 const struct v4l2_queryctrl *qctrl;
548 unsigned long range;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300549 int data;
550
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300551 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
552
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300553 switch (ctrl->id) {
554 case V4L2_CID_VFLIP:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300555 data = reg_read(client, MT9V022_READ_MODE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300556 if (data < 0)
557 return -EIO;
558 ctrl->value = !!(data & 0x10);
559 break;
560 case V4L2_CID_HFLIP:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300561 data = reg_read(client, MT9V022_READ_MODE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300562 if (data < 0)
563 return -EIO;
564 ctrl->value = !!(data & 0x20);
565 break;
566 case V4L2_CID_EXPOSURE_AUTO:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300567 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300568 if (data < 0)
569 return -EIO;
570 ctrl->value = !!(data & 0x1);
571 break;
572 case V4L2_CID_AUTOGAIN:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300573 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300574 if (data < 0)
575 return -EIO;
576 ctrl->value = !!(data & 0x2);
577 break;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300578 case V4L2_CID_GAIN:
579 data = reg_read(client, MT9V022_ANALOG_GAIN);
580 if (data < 0)
581 return -EIO;
582
583 range = qctrl->maximum - qctrl->minimum;
584 ctrl->value = ((data - 16) * range + 24) / 48 + qctrl->minimum;
585
586 break;
587 case V4L2_CID_EXPOSURE:
588 data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
589 if (data < 0)
590 return -EIO;
591
592 range = qctrl->maximum - qctrl->minimum;
593 ctrl->value = ((data - 1) * range + 239) / 479 + qctrl->minimum;
594
595 break;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300596 }
597 return 0;
598}
599
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300600static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300601{
602 int data;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300603 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300604 const struct v4l2_queryctrl *qctrl;
605
606 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300607 if (!qctrl)
608 return -EINVAL;
609
610 switch (ctrl->id) {
611 case V4L2_CID_VFLIP:
612 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300613 data = reg_set(client, MT9V022_READ_MODE, 0x10);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300614 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300615 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300616 if (data < 0)
617 return -EIO;
618 break;
619 case V4L2_CID_HFLIP:
620 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300621 data = reg_set(client, MT9V022_READ_MODE, 0x20);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300622 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300623 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300624 if (data < 0)
625 return -EIO;
626 break;
627 case V4L2_CID_GAIN:
628 /* mt9v022 has minimum == default */
629 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
630 return -EINVAL;
631 else {
632 unsigned long range = qctrl->maximum - qctrl->minimum;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300633 /* Valid values 16 to 64, 32 to 64 must be even. */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300634 unsigned long gain = ((ctrl->value - qctrl->minimum) *
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300635 48 + range / 2) / range + 16;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300636 if (gain >= 32)
637 gain &= ~1;
638 /* The user wants to set gain manually, hope, she
639 * knows, what she's doing... Switch AGC off. */
640
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300641 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300642 return -EIO;
643
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300644 dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
645 reg_read(client, MT9V022_ANALOG_GAIN), gain);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300646 if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300647 return -EIO;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300648 }
649 break;
650 case V4L2_CID_EXPOSURE:
651 /* mt9v022 has maximum == default */
652 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
653 return -EINVAL;
654 else {
655 unsigned long range = qctrl->maximum - qctrl->minimum;
656 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
657 479 + range / 2) / range + 1;
658 /* The user wants to set shutter width manually, hope,
659 * she knows, what she's doing... Switch AEC off. */
660
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300661 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300662 return -EIO;
663
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300664 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300665 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300666 shutter);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300667 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300668 shutter) < 0)
669 return -EIO;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300670 }
671 break;
672 case V4L2_CID_AUTOGAIN:
673 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300674 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300675 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300676 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300677 if (data < 0)
678 return -EIO;
679 break;
680 case V4L2_CID_EXPOSURE_AUTO:
681 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300682 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300683 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300684 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300685 if (data < 0)
686 return -EIO;
687 break;
688 }
689 return 0;
690}
691
692/* Interface active, can use i2c. If it fails, it can indeed mean, that
693 * this wasn't our capture interface, so, we wait for the right one */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300694static int mt9v022_video_probe(struct soc_camera_device *icd,
695 struct i2c_client *client)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300696{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300697 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300698 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300699 s32 data;
700 int ret;
Sascha Hauere958e272009-03-13 06:08:20 -0300701 unsigned long flags;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300702
703 if (!icd->dev.parent ||
704 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
705 return -ENODEV;
706
707 /* Read out the chip version register */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300708 data = reg_read(client, MT9V022_CHIP_VERSION);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300709
710 /* must be 0x1311 or 0x1313 */
711 if (data != 0x1311 && data != 0x1313) {
712 ret = -ENODEV;
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300713 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300714 data);
715 goto ei2c;
716 }
717
718 /* Soft reset */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300719 ret = reg_write(client, MT9V022_RESET, 1);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300720 if (ret < 0)
721 goto ei2c;
722 /* 15 clock cycles */
723 udelay(200);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300724 if (reg_read(client, MT9V022_RESET)) {
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300725 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300726 if (ret > 0)
727 ret = -EIO;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300728 goto ei2c;
729 }
730
731 /* Set monochrome or colour sensor type */
732 if (sensor_type && (!strcmp("colour", sensor_type) ||
733 !strcmp("color", sensor_type))) {
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300734 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300735 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300736 icd->formats = mt9v022_colour_formats;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300737 } else {
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300738 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300739 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300740 icd->formats = mt9v022_monochrome_formats;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300741 }
742
Sascha Hauere958e272009-03-13 06:08:20 -0300743 if (ret < 0)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300744 goto ei2c;
Sascha Hauere958e272009-03-13 06:08:20 -0300745
746 icd->num_formats = 0;
747
748 /*
749 * This is a 10bit sensor, so by default we only allow 10bit.
750 * The platform may support different bus widths due to
751 * different routing of the data lines.
752 */
753 if (icl->query_bus_param)
754 flags = icl->query_bus_param(icl);
755 else
756 flags = SOCAM_DATAWIDTH_10;
757
758 if (flags & SOCAM_DATAWIDTH_10)
759 icd->num_formats++;
760 else
761 icd->formats++;
762
763 if (flags & SOCAM_DATAWIDTH_8)
764 icd->num_formats++;
765
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300766 mt9v022->fourcc = icd->formats->fourcc;
767
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300768 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300769 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
770 "monochrome" : "colour");
771
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300772 ret = mt9v022_init(client);
773 if (ret < 0)
774 dev_err(&client->dev, "Failed to initialise the camera\n");
775
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300776ei2c:
777 return ret;
778}
779
780static void mt9v022_video_remove(struct soc_camera_device *icd)
781{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300782 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300783
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300784 dev_dbg(&icd->dev, "Video removed: %p, %p\n",
Guennadi Liakhovetskia85bdac2008-12-18 12:54:33 -0300785 icd->dev.parent, icd->vdev);
Guennadi Liakhovetski594bb462009-04-24 12:53:51 -0300786 if (icl->free_bus)
787 icl->free_bus(icl);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300788}
789
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300790static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
791 .g_ctrl = mt9v022_g_ctrl,
792 .s_ctrl = mt9v022_s_ctrl,
793 .g_chip_ident = mt9v022_g_chip_ident,
794#ifdef CONFIG_VIDEO_ADV_DEBUG
795 .g_register = mt9v022_g_register,
796 .s_register = mt9v022_s_register,
797#endif
798};
799
800static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
801 .s_stream = mt9v022_s_stream,
802 .s_fmt = mt9v022_s_fmt,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300803 .g_fmt = mt9v022_g_fmt,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300804 .try_fmt = mt9v022_try_fmt,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300805 .s_crop = mt9v022_s_crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300806 .g_crop = mt9v022_g_crop,
807 .cropcap = mt9v022_cropcap,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300808};
809
810static struct v4l2_subdev_ops mt9v022_subdev_ops = {
811 .core = &mt9v022_subdev_core_ops,
812 .video = &mt9v022_subdev_video_ops,
813};
814
Jean Delvared2653e92008-04-29 23:11:39 +0200815static int mt9v022_probe(struct i2c_client *client,
816 const struct i2c_device_id *did)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300817{
818 struct mt9v022 *mt9v022;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300819 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300820 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300821 struct soc_camera_link *icl;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300822 int ret;
823
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300824 if (!icd) {
825 dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
826 return -EINVAL;
827 }
828
829 icl = to_soc_camera_link(icd);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300830 if (!icl) {
831 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
832 return -EINVAL;
833 }
834
835 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
836 dev_warn(&adapter->dev,
837 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
838 return -EIO;
839 }
840
841 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
842 if (!mt9v022)
843 return -ENOMEM;
844
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300845 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
846
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300847 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300848
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300849 icd->ops = &mt9v022_ops;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300850 /*
851 * MT9V022 _really_ corrupts the first read out line.
852 * TODO: verify on i.MX31
853 */
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300854 icd->y_skip_top = 1;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300855
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300856 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
857 mt9v022->rect.top = MT9V022_ROW_SKIP;
858 mt9v022->rect.width = MT9V022_MAX_WIDTH;
859 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
860
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300861 ret = mt9v022_video_probe(icd, client);
862 if (ret) {
863 icd->ops = NULL;
864 i2c_set_clientdata(client, NULL);
865 kfree(mt9v022);
866 }
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300867
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300868 return ret;
869}
870
871static int mt9v022_remove(struct i2c_client *client)
872{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300873 struct mt9v022 *mt9v022 = to_mt9v022(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300874 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300875
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300876 icd->ops = NULL;
877 mt9v022_video_remove(icd);
878 i2c_set_clientdata(client, NULL);
879 client->driver = NULL;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300880 kfree(mt9v022);
881
882 return 0;
883}
Jean Delvare3760f732008-04-29 23:11:40 +0200884static const struct i2c_device_id mt9v022_id[] = {
885 { "mt9v022", 0 },
886 { }
887};
888MODULE_DEVICE_TABLE(i2c, mt9v022_id);
889
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300890static struct i2c_driver mt9v022_i2c_driver = {
891 .driver = {
892 .name = "mt9v022",
893 },
894 .probe = mt9v022_probe,
895 .remove = mt9v022_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200896 .id_table = mt9v022_id,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300897};
898
899static int __init mt9v022_mod_init(void)
900{
901 return i2c_add_driver(&mt9v022_i2c_driver);
902}
903
904static void __exit mt9v022_mod_exit(void)
905{
906 i2c_del_driver(&mt9v022_i2c_driver);
907}
908
909module_init(mt9v022_mod_init);
910module_exit(mt9v022_mod_exit);
911
912MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
913MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
914MODULE_LICENSE("GPL");