blob: a9061bff79b2189b6e542beaab7417ff7ed35a3d [file] [log] [blame]
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -03001/*
2 * Driver for MT9T031 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/videodev2.h>
12#include <linux/slab.h>
13#include <linux/i2c.h>
14#include <linux/log2.h>
15
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030016#include <media/v4l2-subdev.h>
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030017#include <media/v4l2-chip-ident.h>
18#include <media/soc_camera.h>
19
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030020/*
21 * mt9t031 i2c address 0x5d
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030022 * The platform has to define i2c_board_info and link to it from
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030023 * struct soc_camera_link
24 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030025
26/* mt9t031 selected register addresses */
27#define MT9T031_CHIP_VERSION 0x00
28#define MT9T031_ROW_START 0x01
29#define MT9T031_COLUMN_START 0x02
30#define MT9T031_WINDOW_HEIGHT 0x03
31#define MT9T031_WINDOW_WIDTH 0x04
32#define MT9T031_HORIZONTAL_BLANKING 0x05
33#define MT9T031_VERTICAL_BLANKING 0x06
34#define MT9T031_OUTPUT_CONTROL 0x07
35#define MT9T031_SHUTTER_WIDTH_UPPER 0x08
36#define MT9T031_SHUTTER_WIDTH 0x09
37#define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
38#define MT9T031_FRAME_RESTART 0x0b
39#define MT9T031_SHUTTER_DELAY 0x0c
40#define MT9T031_RESET 0x0d
41#define MT9T031_READ_MODE_1 0x1e
42#define MT9T031_READ_MODE_2 0x20
43#define MT9T031_READ_MODE_3 0x21
44#define MT9T031_ROW_ADDRESS_MODE 0x22
45#define MT9T031_COLUMN_ADDRESS_MODE 0x23
46#define MT9T031_GLOBAL_GAIN 0x35
47#define MT9T031_CHIP_ENABLE 0xF8
48
49#define MT9T031_MAX_HEIGHT 1536
50#define MT9T031_MAX_WIDTH 2048
51#define MT9T031_MIN_HEIGHT 2
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030052#define MT9T031_MIN_WIDTH 18
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030053#define MT9T031_HORIZONTAL_BLANK 142
54#define MT9T031_VERTICAL_BLANK 25
55#define MT9T031_COLUMN_SKIP 32
56#define MT9T031_ROW_SKIP 20
57
58#define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \
59 SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \
60 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \
61 SOCAM_MASTER | SOCAM_DATAWIDTH_10)
62
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030063struct mt9t031 {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030064 struct v4l2_subdev subdev;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030065 struct v4l2_rect rect; /* Sensor window */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030066 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030067 u16 xskip;
68 u16 yskip;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -030069 unsigned int gain;
Guennadi Liakhovetski32536102009-12-11 11:14:46 -030070 unsigned short y_skip_top; /* Lines to skip at the top */
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -030071 unsigned int exposure;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030072 unsigned char autoexposure;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030073};
74
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030075static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
76{
77 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
78}
79
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030080static int reg_read(struct i2c_client *client, const u8 reg)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030081{
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030082 s32 data = i2c_smbus_read_word_data(client, reg);
83 return data < 0 ? data : swab16(data);
84}
85
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030086static int reg_write(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030087 const u16 data)
88{
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030089 return i2c_smbus_write_word_data(client, reg, swab16(data));
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030090}
91
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030092static int reg_set(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030093 const u16 data)
94{
95 int ret;
96
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030097 ret = reg_read(client, reg);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030098 if (ret < 0)
99 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300100 return reg_write(client, reg, ret | data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300101}
102
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300103static int reg_clear(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300104 const u16 data)
105{
106 int ret;
107
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300108 ret = reg_read(client, reg);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300109 if (ret < 0)
110 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300111 return reg_write(client, reg, ret & ~data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300112}
113
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300114static int set_shutter(struct i2c_client *client, const u32 data)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300115{
116 int ret;
117
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300118 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300119
120 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300121 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300122
123 return ret;
124}
125
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300126static int get_shutter(struct i2c_client *client, u32 *data)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300127{
128 int ret;
129
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300130 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300131 *data = ret << 16;
132
133 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300134 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300135 *data |= ret & 0xffff;
136
137 return ret < 0 ? ret : 0;
138}
139
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300140static int mt9t031_idle(struct i2c_client *client)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300141{
142 int ret;
143
144 /* Disable chip output, synchronous option update */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300145 ret = reg_write(client, MT9T031_RESET, 1);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300146 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300147 ret = reg_write(client, MT9T031_RESET, 0);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300148 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300149 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300150
151 return ret >= 0 ? 0 : -EIO;
152}
153
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300154static int mt9t031_disable(struct i2c_client *client)
155{
156 /* Disable the chip */
157 reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
158
159 return 0;
160}
161
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300162static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300163{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300164 struct i2c_client *client = sd->priv;
165 int ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300166
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300167 if (enable)
168 /* Switch to master "normal" mode */
169 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
170 else
171 /* Stop sensor readout */
172 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
173
174 if (ret < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300175 return -EIO;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300176
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300177 return 0;
178}
179
180static int mt9t031_set_bus_param(struct soc_camera_device *icd,
181 unsigned long flags)
182{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300183 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300184
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300185 /* The caller should have queried our parameters, check anyway */
186 if (flags & ~MT9T031_BUS_PARAM)
187 return -EINVAL;
188
189 if (flags & SOCAM_PCLK_SAMPLE_FALLING)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300190 reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
Philippe Rétornazc98afbf2009-03-13 09:42:32 -0300191 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300192 reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300193
194 return 0;
195}
196
197static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
198{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300199 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300200
201 return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
202}
203
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300204enum {
205 MT9T031_CTRL_VFLIP,
206 MT9T031_CTRL_HFLIP,
207 MT9T031_CTRL_GAIN,
208 MT9T031_CTRL_EXPOSURE,
209 MT9T031_CTRL_EXPOSURE_AUTO,
210};
211
212static const struct v4l2_queryctrl mt9t031_controls[] = {
213 [MT9T031_CTRL_VFLIP] = {
214 .id = V4L2_CID_VFLIP,
215 .type = V4L2_CTRL_TYPE_BOOLEAN,
216 .name = "Flip Vertically",
217 .minimum = 0,
218 .maximum = 1,
219 .step = 1,
220 .default_value = 0,
221 },
222 [MT9T031_CTRL_HFLIP] = {
223 .id = V4L2_CID_HFLIP,
224 .type = V4L2_CTRL_TYPE_BOOLEAN,
225 .name = "Flip Horizontally",
226 .minimum = 0,
227 .maximum = 1,
228 .step = 1,
229 .default_value = 0,
230 },
231 [MT9T031_CTRL_GAIN] = {
232 .id = V4L2_CID_GAIN,
233 .type = V4L2_CTRL_TYPE_INTEGER,
234 .name = "Gain",
235 .minimum = 0,
236 .maximum = 127,
237 .step = 1,
238 .default_value = 64,
239 .flags = V4L2_CTRL_FLAG_SLIDER,
240 },
241 [MT9T031_CTRL_EXPOSURE] = {
242 .id = V4L2_CID_EXPOSURE,
243 .type = V4L2_CTRL_TYPE_INTEGER,
244 .name = "Exposure",
245 .minimum = 1,
246 .maximum = 255,
247 .step = 1,
248 .default_value = 255,
249 .flags = V4L2_CTRL_FLAG_SLIDER,
250 },
251 [MT9T031_CTRL_EXPOSURE_AUTO] = {
252 .id = V4L2_CID_EXPOSURE_AUTO,
253 .type = V4L2_CTRL_TYPE_BOOLEAN,
254 .name = "Automatic Exposure",
255 .minimum = 0,
256 .maximum = 1,
257 .step = 1,
258 .default_value = 1,
259 }
260};
261
262static struct soc_camera_ops mt9t031_ops = {
263 .set_bus_param = mt9t031_set_bus_param,
264 .query_bus_param = mt9t031_query_bus_param,
265 .controls = mt9t031_controls,
266 .num_controls = ARRAY_SIZE(mt9t031_controls),
267};
268
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300269/* target must be _even_ */
270static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300271{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300272 unsigned int skip;
273
274 if (*source < target + target / 2) {
275 *source = target;
276 return 1;
277 }
278
279 skip = min(max, *source + target / 2) / target;
280 if (skip > 8)
281 skip = 8;
282 *source = target * skip;
283
284 return skip;
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300285}
286
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300287/* rect is the sensor rectangle, the caller guarantees parameter validity */
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300288static int mt9t031_set_params(struct i2c_client *client,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300289 struct v4l2_rect *rect, u16 xskip, u16 yskip)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300290{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300291 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300292 int ret;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300293 u16 xbin, ybin;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300294 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
295 vblank = MT9T031_VERTICAL_BLANK;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300296
297 xbin = min(xskip, (u16)3);
298 ybin = min(yskip, (u16)3);
299
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300300 /*
301 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
302 * There is always a valid suitably aligned value. The worst case is
303 * xbin = 3, width = 2048. Then we will start at 36, the last read out
304 * pixel will be 2083, which is < 2085 - first black pixel.
305 *
306 * MT9T031 datasheet imposes window left border alignment, depending on
307 * the selected xskip. Failing to conform to this requirement produces
308 * dark horizontal stripes in the image. However, even obeying to this
309 * requirement doesn't eliminate the stripes in all configurations. They
310 * appear "locally reproducibly," but can differ between tests under
311 * different lighting conditions.
312 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300313 switch (xbin) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300314 case 1:
315 rect->left &= ~1;
316 break;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300317 case 2:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300318 rect->left &= ~3;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300319 break;
320 case 3:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300321 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
322 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300323 }
324
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300325 rect->top &= ~1;
326
327 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
328 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300329
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300330 /* Disable register update, reconfigure atomically */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300331 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300332 if (ret < 0)
333 return ret;
334
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300335 /* Blanking and start values - default... */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300336 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300337 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300338 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300339
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300340 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300341 /* Binning, skipping */
342 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300343 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300344 ((xbin - 1) << 4) | (xskip - 1));
345 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300346 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300347 ((ybin - 1) << 4) | (yskip - 1));
348 }
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300349 dev_dbg(&client->dev, "new physical left %u, top %u\n",
350 rect->left, rect->top);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300351
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300352 /*
353 * The caller provides a supported format, as guaranteed by
354 * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap()
355 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300356 if (ret >= 0)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300357 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300358 if (ret >= 0)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300359 ret = reg_write(client, MT9T031_ROW_START, rect->top);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300360 if (ret >= 0)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300361 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300362 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300363 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300364 rect->height + mt9t031->y_skip_top - 1);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300365 if (ret >= 0 && mt9t031->autoexposure) {
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300366 unsigned int total_h = rect->height + mt9t031->y_skip_top + vblank;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300367 ret = set_shutter(client, total_h);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300368 if (ret >= 0) {
369 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
370 const struct v4l2_queryctrl *qctrl =
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300371 &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300372 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
373 (qctrl->maximum - qctrl->minimum)) /
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300374 shutter_max + qctrl->minimum;
375 }
376 }
377
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300378 /* Re-enable register update, commit all changes */
379 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300380 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300381
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300382 if (ret >= 0) {
383 mt9t031->rect = *rect;
384 mt9t031->xskip = xskip;
385 mt9t031->yskip = yskip;
386 }
387
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300388 return ret < 0 ? ret : 0;
389}
390
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300391static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300392{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300393 struct v4l2_rect rect = a->c;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300394 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300395 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300396
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300397 rect.width = ALIGN(rect.width, 2);
398 rect.height = ALIGN(rect.height, 2);
Guennadi Liakhovetskie3309192009-08-25 11:46:42 -0300399
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300400 soc_camera_limit_side(&rect.left, &rect.width,
401 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
Guennadi Liakhovetskie3309192009-08-25 11:46:42 -0300402
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300403 soc_camera_limit_side(&rect.top, &rect.height,
404 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
405
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300406 return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300407}
408
409static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
410{
411 struct i2c_client *client = sd->priv;
412 struct mt9t031 *mt9t031 = to_mt9t031(client);
413
414 a->c = mt9t031->rect;
415 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
416
417 return 0;
418}
419
420static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
421{
422 a->bounds.left = MT9T031_COLUMN_SKIP;
423 a->bounds.top = MT9T031_ROW_SKIP;
424 a->bounds.width = MT9T031_MAX_WIDTH;
425 a->bounds.height = MT9T031_MAX_HEIGHT;
426 a->defrect = a->bounds;
427 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
428 a->pixelaspect.numerator = 1;
429 a->pixelaspect.denominator = 1;
430
431 return 0;
432}
433
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300434static int mt9t031_g_fmt(struct v4l2_subdev *sd,
435 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300436{
437 struct i2c_client *client = sd->priv;
438 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300439
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300440 mf->width = mt9t031->rect.width / mt9t031->xskip;
441 mf->height = mt9t031->rect.height / mt9t031->yskip;
442 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
443 mf->colorspace = V4L2_COLORSPACE_SRGB;
444 mf->field = V4L2_FIELD_NONE;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300445
446 return 0;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300447}
448
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300449static int mt9t031_s_fmt(struct v4l2_subdev *sd,
450 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300451{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300452 struct i2c_client *client = sd->priv;
453 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300454 u16 xskip, yskip;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300455 struct v4l2_rect rect = mt9t031->rect;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300456
457 /*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300458 * try_fmt has put width and height within limits.
459 * S_FMT: use binning and skipping for scaling
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300460 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300461 xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
462 yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
463
464 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
465 mf->colorspace = V4L2_COLORSPACE_SRGB;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300466
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300467 /* mt9t031_set_params() doesn't change width and height */
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300468 return mt9t031_set_params(client, &rect, xskip, yskip);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300469}
470
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300471/*
472 * If a user window larger than sensor window is requested, we'll increase the
473 * sensor window.
474 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300475static int mt9t031_try_fmt(struct v4l2_subdev *sd,
476 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300477{
Trent Piepho653dc592009-05-30 21:45:46 -0300478 v4l_bound_align_image(
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300479 &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
480 &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
481
482 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
483 mf->colorspace = V4L2_COLORSPACE_SRGB;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300484
485 return 0;
486}
487
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300488static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
489 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300490{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300491 struct i2c_client *client = sd->priv;
492 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300493
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300494 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300495 return -EINVAL;
496
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300497 if (id->match.addr != client->addr)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300498 return -ENODEV;
499
500 id->ident = mt9t031->model;
501 id->revision = 0;
502
503 return 0;
504}
505
506#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300507static int mt9t031_g_register(struct v4l2_subdev *sd,
508 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300509{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300510 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300511
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300512 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300513 return -EINVAL;
514
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300515 if (reg->match.addr != client->addr)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300516 return -ENODEV;
517
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300518 reg->val = reg_read(client, reg->reg);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300519
520 if (reg->val > 0xffff)
521 return -EIO;
522
523 return 0;
524}
525
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300526static int mt9t031_s_register(struct v4l2_subdev *sd,
527 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300528{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300529 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300530
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300531 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300532 return -EINVAL;
533
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300534 if (reg->match.addr != client->addr)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300535 return -ENODEV;
536
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300537 if (reg_write(client, reg->reg, reg->val) < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300538 return -EIO;
539
540 return 0;
541}
542#endif
543
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300544static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300545{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300546 struct i2c_client *client = sd->priv;
547 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300548 int data;
549
550 switch (ctrl->id) {
551 case V4L2_CID_VFLIP:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300552 data = reg_read(client, MT9T031_READ_MODE_2);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300553 if (data < 0)
554 return -EIO;
555 ctrl->value = !!(data & 0x8000);
556 break;
557 case V4L2_CID_HFLIP:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300558 data = reg_read(client, MT9T031_READ_MODE_2);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300559 if (data < 0)
560 return -EIO;
561 ctrl->value = !!(data & 0x4000);
562 break;
563 case V4L2_CID_EXPOSURE_AUTO:
564 ctrl->value = mt9t031->autoexposure;
565 break;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300566 case V4L2_CID_GAIN:
567 ctrl->value = mt9t031->gain;
568 break;
569 case V4L2_CID_EXPOSURE:
570 ctrl->value = mt9t031->exposure;
571 break;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300572 }
573 return 0;
574}
575
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300576static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300577{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300578 struct i2c_client *client = sd->priv;
579 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300580 const struct v4l2_queryctrl *qctrl;
581 int data;
582
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300583 switch (ctrl->id) {
584 case V4L2_CID_VFLIP:
585 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300586 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300587 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300588 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300589 if (data < 0)
590 return -EIO;
591 break;
592 case V4L2_CID_HFLIP:
593 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300594 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300595 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300596 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300597 if (data < 0)
598 return -EIO;
599 break;
600 case V4L2_CID_GAIN:
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300601 qctrl = &mt9t031_controls[MT9T031_CTRL_GAIN];
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300602 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
603 return -EINVAL;
604 /* See Datasheet Table 7, Gain settings. */
605 if (ctrl->value <= qctrl->default_value) {
606 /* Pack it into 0..1 step 0.125, register values 0..8 */
607 unsigned long range = qctrl->default_value - qctrl->minimum;
608 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
609
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300610 dev_dbg(&client->dev, "Setting gain %d\n", data);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300611 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300612 if (data < 0)
613 return -EIO;
614 } else {
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300615 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300616 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
617 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300618 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300619 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300620 1015 + range / 2) / range + 9;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300621
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300622 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300623 data = gain;
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300624 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300625 data = ((gain - 32) * 16 + 16) / 32 + 80;
626 else
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300627 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
628 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300629
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300630 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300631 reg_read(client, MT9T031_GLOBAL_GAIN), data);
632 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300633 if (data < 0)
634 return -EIO;
635 }
636
637 /* Success */
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300638 mt9t031->gain = ctrl->value;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300639 break;
640 case V4L2_CID_EXPOSURE:
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300641 qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300642 /* mt9t031 has maximum == default */
643 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
644 return -EINVAL;
645 else {
646 const unsigned long range = qctrl->maximum - qctrl->minimum;
647 const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
648 range / 2) / range + 1;
649 u32 old;
650
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300651 get_shutter(client, &old);
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300652 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300653 old, shutter);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300654 if (set_shutter(client, shutter) < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300655 return -EIO;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300656 mt9t031->exposure = ctrl->value;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300657 mt9t031->autoexposure = 0;
658 }
659 break;
660 case V4L2_CID_EXPOSURE_AUTO:
661 if (ctrl->value) {
662 const u16 vblank = MT9T031_VERTICAL_BLANK;
663 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300664 unsigned int total_h = mt9t031->rect.height +
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300665 mt9t031->y_skip_top + vblank;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300666
667 if (set_shutter(client, total_h) < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300668 return -EIO;
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300669 qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300670 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
671 (qctrl->maximum - qctrl->minimum)) /
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300672 shutter_max + qctrl->minimum;
673 mt9t031->autoexposure = 1;
674 } else
675 mt9t031->autoexposure = 0;
676 break;
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300677 default:
678 return -EINVAL;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300679 }
680 return 0;
681}
682
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300683/*
684 * Interface active, can use i2c. If it fails, it can indeed mean, that
685 * this wasn't our capture interface, so, we wait for the right one
686 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300687static int mt9t031_video_probe(struct i2c_client *client)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300688{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300689 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300690 s32 data;
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300691 int ret;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300692
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300693 /* Enable the chip */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300694 data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300695 dev_dbg(&client->dev, "write: %d\n", data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300696
697 /* Read out the chip version register */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300698 data = reg_read(client, MT9T031_CHIP_VERSION);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300699
700 switch (data) {
701 case 0x1621:
702 mt9t031->model = V4L2_IDENT_MT9T031;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300703 break;
704 default:
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300705 dev_err(&client->dev,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300706 "No MT9T031 chip detected, register read %x\n", data);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300707 return -ENODEV;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300708 }
709
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300710 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300711
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300712 ret = mt9t031_idle(client);
713 if (ret < 0)
714 dev_err(&client->dev, "Failed to initialise the camera\n");
715
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300716 /* mt9t031_idle() has reset the chip to default. */
717 mt9t031->exposure = 255;
718 mt9t031->gain = 64;
719
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300720 return ret;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300721}
722
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300723static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
724{
725 struct i2c_client *client = sd->priv;
726 struct mt9t031 *mt9t031 = to_mt9t031(client);
727
728 *lines = mt9t031->y_skip_top;
729
730 return 0;
731}
732
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300733static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
734 .g_ctrl = mt9t031_g_ctrl,
735 .s_ctrl = mt9t031_s_ctrl,
736 .g_chip_ident = mt9t031_g_chip_ident,
737#ifdef CONFIG_VIDEO_ADV_DEBUG
738 .g_register = mt9t031_g_register,
739 .s_register = mt9t031_s_register,
740#endif
741};
742
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300743static int mt9t031_enum_fmt(struct v4l2_subdev *sd, int index,
744 enum v4l2_mbus_pixelcode *code)
745{
746 if (index)
747 return -EINVAL;
748
749 *code = V4L2_MBUS_FMT_SBGGR10_1X10;
750 return 0;
751}
752
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300753static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
754 .s_stream = mt9t031_s_stream,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300755 .s_mbus_fmt = mt9t031_s_fmt,
756 .g_mbus_fmt = mt9t031_g_fmt,
757 .try_mbus_fmt = mt9t031_try_fmt,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300758 .s_crop = mt9t031_s_crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300759 .g_crop = mt9t031_g_crop,
760 .cropcap = mt9t031_cropcap,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300761 .enum_mbus_fmt = mt9t031_enum_fmt,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300762};
763
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300764static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
765 .g_skip_top_lines = mt9t031_g_skip_top_lines,
766};
767
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300768static struct v4l2_subdev_ops mt9t031_subdev_ops = {
769 .core = &mt9t031_subdev_core_ops,
770 .video = &mt9t031_subdev_video_ops,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300771 .sensor = &mt9t031_subdev_sensor_ops,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300772};
773
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300774static int mt9t031_probe(struct i2c_client *client,
775 const struct i2c_device_id *did)
776{
777 struct mt9t031 *mt9t031;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300778 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300779 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300780 int ret;
781
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300782 if (icd) {
783 struct soc_camera_link *icl = to_soc_camera_link(icd);
784 if (!icl) {
785 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
786 return -EINVAL;
787 }
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300788
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300789 icd->ops = &mt9t031_ops;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300790 }
791
792 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
793 dev_warn(&adapter->dev,
794 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
795 return -EIO;
796 }
797
798 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
799 if (!mt9t031)
800 return -ENOMEM;
801
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300802 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300803
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300804 mt9t031->y_skip_top = 0;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300805 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
806 mt9t031->rect.top = MT9T031_ROW_SKIP;
807 mt9t031->rect.width = MT9T031_MAX_WIDTH;
808 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
809
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300810 /*
811 * Simulated autoexposure. If enabled, we calculate shutter width
812 * ourselves in the driver based on vertical blanking and frame width
813 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300814 mt9t031->autoexposure = 1;
815
816 mt9t031->xskip = 1;
817 mt9t031->yskip = 1;
818
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300819 mt9t031_idle(client);
820
821 ret = mt9t031_video_probe(client);
822
823 mt9t031_disable(client);
824
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300825 if (ret) {
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300826 if (icd)
827 icd->ops = NULL;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300828 i2c_set_clientdata(client, NULL);
829 kfree(mt9t031);
830 }
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300831
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300832 return ret;
833}
834
835static int mt9t031_remove(struct i2c_client *client)
836{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300837 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300838 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300839
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300840 if (icd)
841 icd->ops = NULL;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300842 i2c_set_clientdata(client, NULL);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300843 client->driver = NULL;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300844 kfree(mt9t031);
845
846 return 0;
847}
848
849static const struct i2c_device_id mt9t031_id[] = {
850 { "mt9t031", 0 },
851 { }
852};
853MODULE_DEVICE_TABLE(i2c, mt9t031_id);
854
855static struct i2c_driver mt9t031_i2c_driver = {
856 .driver = {
857 .name = "mt9t031",
858 },
859 .probe = mt9t031_probe,
860 .remove = mt9t031_remove,
861 .id_table = mt9t031_id,
862};
863
864static int __init mt9t031_mod_init(void)
865{
866 return i2c_add_driver(&mt9t031_i2c_driver);
867}
868
869static void __exit mt9t031_mod_exit(void)
870{
871 i2c_del_driver(&mt9t031_i2c_driver);
872}
873
874module_init(mt9t031_mod_init);
875module_exit(mt9t031_mod_exit);
876
877MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
878MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
879MODULE_LICENSE("GPL v2");