blob: f234ba602049969eaa396b59e1fea07890baa16d [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
20/* mt9t031 i2c address 0x5d
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030021 * The platform has to define i2c_board_info and link to it from
22 * struct soc_camera_link */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030023
24/* mt9t031 selected register addresses */
25#define MT9T031_CHIP_VERSION 0x00
26#define MT9T031_ROW_START 0x01
27#define MT9T031_COLUMN_START 0x02
28#define MT9T031_WINDOW_HEIGHT 0x03
29#define MT9T031_WINDOW_WIDTH 0x04
30#define MT9T031_HORIZONTAL_BLANKING 0x05
31#define MT9T031_VERTICAL_BLANKING 0x06
32#define MT9T031_OUTPUT_CONTROL 0x07
33#define MT9T031_SHUTTER_WIDTH_UPPER 0x08
34#define MT9T031_SHUTTER_WIDTH 0x09
35#define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
36#define MT9T031_FRAME_RESTART 0x0b
37#define MT9T031_SHUTTER_DELAY 0x0c
38#define MT9T031_RESET 0x0d
39#define MT9T031_READ_MODE_1 0x1e
40#define MT9T031_READ_MODE_2 0x20
41#define MT9T031_READ_MODE_3 0x21
42#define MT9T031_ROW_ADDRESS_MODE 0x22
43#define MT9T031_COLUMN_ADDRESS_MODE 0x23
44#define MT9T031_GLOBAL_GAIN 0x35
45#define MT9T031_CHIP_ENABLE 0xF8
46
47#define MT9T031_MAX_HEIGHT 1536
48#define MT9T031_MAX_WIDTH 2048
49#define MT9T031_MIN_HEIGHT 2
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030050#define MT9T031_MIN_WIDTH 18
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030051#define MT9T031_HORIZONTAL_BLANK 142
52#define MT9T031_VERTICAL_BLANK 25
53#define MT9T031_COLUMN_SKIP 32
54#define MT9T031_ROW_SKIP 20
55
56#define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \
57 SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \
58 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \
59 SOCAM_MASTER | SOCAM_DATAWIDTH_10)
60
61static const struct soc_camera_data_format mt9t031_colour_formats[] = {
62 {
63 .name = "Bayer (sRGB) 10 bit",
64 .depth = 10,
65 .fourcc = V4L2_PIX_FMT_SGRBG10,
66 .colorspace = V4L2_COLORSPACE_SRGB,
67 }
68};
69
70struct mt9t031 {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030071 struct v4l2_subdev subdev;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030072 struct v4l2_rect rect; /* Sensor window */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030073 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030074 u16 xskip;
75 u16 yskip;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030076 unsigned char autoexposure;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030077};
78
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030079static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
80{
81 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
82}
83
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030084static int reg_read(struct i2c_client *client, const u8 reg)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030085{
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030086 s32 data = i2c_smbus_read_word_data(client, reg);
87 return data < 0 ? data : swab16(data);
88}
89
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030090static int reg_write(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030091 const u16 data)
92{
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030093 return i2c_smbus_write_word_data(client, reg, swab16(data));
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030094}
95
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030096static int reg_set(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030097 const u16 data)
98{
99 int ret;
100
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300101 ret = reg_read(client, reg);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300102 if (ret < 0)
103 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300104 return reg_write(client, reg, ret | data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300105}
106
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300107static int reg_clear(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300108 const u16 data)
109{
110 int ret;
111
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300112 ret = reg_read(client, reg);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300113 if (ret < 0)
114 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300115 return reg_write(client, reg, ret & ~data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300116}
117
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300118static int set_shutter(struct i2c_client *client, const u32 data)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300119{
120 int ret;
121
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300122 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300123
124 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300125 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300126
127 return ret;
128}
129
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300130static int get_shutter(struct i2c_client *client, u32 *data)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300131{
132 int ret;
133
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300134 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300135 *data = ret << 16;
136
137 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300138 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300139 *data |= ret & 0xffff;
140
141 return ret < 0 ? ret : 0;
142}
143
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300144static int mt9t031_idle(struct i2c_client *client)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300145{
146 int ret;
147
148 /* Disable chip output, synchronous option update */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300149 ret = reg_write(client, MT9T031_RESET, 1);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300150 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300151 ret = reg_write(client, MT9T031_RESET, 0);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300152 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300153 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300154
155 return ret >= 0 ? 0 : -EIO;
156}
157
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300158static int mt9t031_disable(struct i2c_client *client)
159{
160 /* Disable the chip */
161 reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
162
163 return 0;
164}
165
166static int mt9t031_init(struct soc_camera_device *icd)
167{
168 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
169
170 return mt9t031_idle(client);
171}
172
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300173static int mt9t031_release(struct soc_camera_device *icd)
174{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300175 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
Guennadi Liakhovetskif9826512009-04-03 10:14:02 -0300176
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300177 return mt9t031_disable(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300178}
179
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300180static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300181{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300182 struct i2c_client *client = sd->priv;
183 int ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300184
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300185 if (enable)
186 /* Switch to master "normal" mode */
187 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
188 else
189 /* Stop sensor readout */
190 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
191
192 if (ret < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300193 return -EIO;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300194
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300195 return 0;
196}
197
198static int mt9t031_set_bus_param(struct soc_camera_device *icd,
199 unsigned long flags)
200{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300201 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300202
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300203 /* The caller should have queried our parameters, check anyway */
204 if (flags & ~MT9T031_BUS_PARAM)
205 return -EINVAL;
206
207 if (flags & SOCAM_PCLK_SAMPLE_FALLING)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300208 reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
Philippe Rétornazc98afbf2009-03-13 09:42:32 -0300209 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300210 reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300211
212 return 0;
213}
214
215static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
216{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300217 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300218
219 return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
220}
221
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300222/* target must be _even_ */
223static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300224{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300225 unsigned int skip;
226
227 if (*source < target + target / 2) {
228 *source = target;
229 return 1;
230 }
231
232 skip = min(max, *source + target / 2) / target;
233 if (skip > 8)
234 skip = 8;
235 *source = target * skip;
236
237 return skip;
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300238}
239
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300240/* rect is the sensor rectangle, the caller guarantees parameter validity */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300241static int mt9t031_set_params(struct soc_camera_device *icd,
242 struct v4l2_rect *rect, u16 xskip, u16 yskip)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300243{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300244 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300245 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300246 int ret;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300247 u16 xbin, ybin;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300248 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
249 vblank = MT9T031_VERTICAL_BLANK;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300250
251 xbin = min(xskip, (u16)3);
252 ybin = min(yskip, (u16)3);
253
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300254 /*
255 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
256 * There is always a valid suitably aligned value. The worst case is
257 * xbin = 3, width = 2048. Then we will start at 36, the last read out
258 * pixel will be 2083, which is < 2085 - first black pixel.
259 *
260 * MT9T031 datasheet imposes window left border alignment, depending on
261 * the selected xskip. Failing to conform to this requirement produces
262 * dark horizontal stripes in the image. However, even obeying to this
263 * requirement doesn't eliminate the stripes in all configurations. They
264 * appear "locally reproducibly," but can differ between tests under
265 * different lighting conditions.
266 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300267 switch (xbin) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300268 case 1:
269 rect->left &= ~1;
270 break;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300271 case 2:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300272 rect->left &= ~3;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300273 break;
274 case 3:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300275 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
276 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300277 }
278
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300279 rect->top &= ~1;
280
281 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
282 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300283
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300284 /* Disable register update, reconfigure atomically */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300285 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300286 if (ret < 0)
287 return ret;
288
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300289 /* Blanking and start values - default... */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300290 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300291 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300292 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300293
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300294 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300295 /* Binning, skipping */
296 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300297 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300298 ((xbin - 1) << 4) | (xskip - 1));
299 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300300 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300301 ((ybin - 1) << 4) | (yskip - 1));
302 }
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300303 dev_dbg(&client->dev, "new physical left %u, top %u\n",
304 rect->left, rect->top);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300305
306 /* The caller provides a supported format, as guaranteed by
307 * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap() */
308 if (ret >= 0)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300309 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300310 if (ret >= 0)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300311 ret = reg_write(client, MT9T031_ROW_START, rect->top);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300312 if (ret >= 0)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300313 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300314 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300315 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300316 rect->height + icd->y_skip_top - 1);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300317 if (ret >= 0 && mt9t031->autoexposure) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300318 ret = set_shutter(client,
319 rect->height + icd->y_skip_top + vblank);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300320 if (ret >= 0) {
321 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
322 const struct v4l2_queryctrl *qctrl =
323 soc_camera_find_qctrl(icd->ops,
324 V4L2_CID_EXPOSURE);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300325 icd->exposure = (shutter_max / 2 + (rect->height +
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300326 icd->y_skip_top + vblank - 1) *
327 (qctrl->maximum - qctrl->minimum)) /
328 shutter_max + qctrl->minimum;
329 }
330 }
331
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300332 /* Re-enable register update, commit all changes */
333 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300334 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300335
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300336 if (ret >= 0) {
337 mt9t031->rect = *rect;
338 mt9t031->xskip = xskip;
339 mt9t031->yskip = yskip;
340 }
341
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300342 return ret < 0 ? ret : 0;
343}
344
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300345static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300346{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300347 struct v4l2_rect rect = a->c;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300348 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300349 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300350 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300351
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300352 rect.width = ALIGN(rect.width, 2);
353 rect.height = ALIGN(rect.height, 2);
Guennadi Liakhovetskie3309192009-08-25 11:46:42 -0300354
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300355 soc_camera_limit_side(&rect.left, &rect.width,
356 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
Guennadi Liakhovetskie3309192009-08-25 11:46:42 -0300357
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300358 soc_camera_limit_side(&rect.top, &rect.height,
359 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
360
361 return mt9t031_set_params(icd, &rect, mt9t031->xskip, mt9t031->yskip);
362}
363
364static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
365{
366 struct i2c_client *client = sd->priv;
367 struct mt9t031 *mt9t031 = to_mt9t031(client);
368
369 a->c = mt9t031->rect;
370 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
371
372 return 0;
373}
374
375static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
376{
377 a->bounds.left = MT9T031_COLUMN_SKIP;
378 a->bounds.top = MT9T031_ROW_SKIP;
379 a->bounds.width = MT9T031_MAX_WIDTH;
380 a->bounds.height = MT9T031_MAX_HEIGHT;
381 a->defrect = a->bounds;
382 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
383 a->pixelaspect.numerator = 1;
384 a->pixelaspect.denominator = 1;
385
386 return 0;
387}
388
389static int mt9t031_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
390{
391 struct i2c_client *client = sd->priv;
392 struct mt9t031 *mt9t031 = to_mt9t031(client);
393 struct v4l2_pix_format *pix = &f->fmt.pix;
394
395 pix->width = mt9t031->rect.width / mt9t031->xskip;
396 pix->height = mt9t031->rect.height / mt9t031->yskip;
397 pix->pixelformat = V4L2_PIX_FMT_SGRBG10;
398 pix->field = V4L2_FIELD_NONE;
399 pix->colorspace = V4L2_COLORSPACE_SRGB;
400
401 return 0;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300402}
403
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300404static int mt9t031_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300405{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300406 struct i2c_client *client = sd->priv;
407 struct mt9t031 *mt9t031 = to_mt9t031(client);
408 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300409 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300410 u16 xskip, yskip;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300411 struct v4l2_rect rect = mt9t031->rect;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300412
413 /*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300414 * try_fmt has put width and height within limits.
415 * S_FMT: use binning and skipping for scaling
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300416 */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300417 xskip = mt9t031_skip(&rect.width, pix->width, MT9T031_MAX_WIDTH);
418 yskip = mt9t031_skip(&rect.height, pix->height, MT9T031_MAX_HEIGHT);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300419
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300420 /* mt9t031_set_params() doesn't change width and height */
421 return mt9t031_set_params(icd, &rect, xskip, yskip);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300422}
423
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300424/*
425 * If a user window larger than sensor window is requested, we'll increase the
426 * sensor window.
427 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300428static int mt9t031_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300429{
430 struct v4l2_pix_format *pix = &f->fmt.pix;
431
Trent Piepho653dc592009-05-30 21:45:46 -0300432 v4l_bound_align_image(
433 &pix->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
434 &pix->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300435
436 return 0;
437}
438
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300439static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
440 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300441{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300442 struct i2c_client *client = sd->priv;
443 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300444
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300445 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300446 return -EINVAL;
447
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300448 if (id->match.addr != client->addr)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300449 return -ENODEV;
450
451 id->ident = mt9t031->model;
452 id->revision = 0;
453
454 return 0;
455}
456
457#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300458static int mt9t031_g_register(struct v4l2_subdev *sd,
459 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300460{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300461 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300462
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300463 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300464 return -EINVAL;
465
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300466 if (reg->match.addr != client->addr)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300467 return -ENODEV;
468
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300469 reg->val = reg_read(client, reg->reg);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300470
471 if (reg->val > 0xffff)
472 return -EIO;
473
474 return 0;
475}
476
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300477static int mt9t031_s_register(struct v4l2_subdev *sd,
478 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300479{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300480 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300481
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300482 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300483 return -EINVAL;
484
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300485 if (reg->match.addr != client->addr)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300486 return -ENODEV;
487
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300488 if (reg_write(client, reg->reg, reg->val) < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300489 return -EIO;
490
491 return 0;
492}
493#endif
494
495static const struct v4l2_queryctrl mt9t031_controls[] = {
496 {
497 .id = V4L2_CID_VFLIP,
498 .type = V4L2_CTRL_TYPE_BOOLEAN,
499 .name = "Flip Vertically",
500 .minimum = 0,
501 .maximum = 1,
502 .step = 1,
503 .default_value = 0,
504 }, {
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300505 .id = V4L2_CID_HFLIP,
506 .type = V4L2_CTRL_TYPE_BOOLEAN,
507 .name = "Flip Horizontally",
508 .minimum = 0,
509 .maximum = 1,
510 .step = 1,
511 .default_value = 0,
512 }, {
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300513 .id = V4L2_CID_GAIN,
514 .type = V4L2_CTRL_TYPE_INTEGER,
515 .name = "Gain",
516 .minimum = 0,
517 .maximum = 127,
518 .step = 1,
519 .default_value = 64,
520 .flags = V4L2_CTRL_FLAG_SLIDER,
521 }, {
522 .id = V4L2_CID_EXPOSURE,
523 .type = V4L2_CTRL_TYPE_INTEGER,
524 .name = "Exposure",
525 .minimum = 1,
526 .maximum = 255,
527 .step = 1,
528 .default_value = 255,
529 .flags = V4L2_CTRL_FLAG_SLIDER,
530 }, {
531 .id = V4L2_CID_EXPOSURE_AUTO,
532 .type = V4L2_CTRL_TYPE_BOOLEAN,
533 .name = "Automatic Exposure",
534 .minimum = 0,
535 .maximum = 1,
536 .step = 1,
537 .default_value = 1,
538 }
539};
540
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300541static struct soc_camera_ops mt9t031_ops = {
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300542 .init = mt9t031_init,
543 .release = mt9t031_release,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300544 .set_bus_param = mt9t031_set_bus_param,
545 .query_bus_param = mt9t031_query_bus_param,
546 .controls = mt9t031_controls,
547 .num_controls = ARRAY_SIZE(mt9t031_controls),
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300548};
549
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300550static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300551{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300552 struct i2c_client *client = sd->priv;
553 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300554 int data;
555
556 switch (ctrl->id) {
557 case V4L2_CID_VFLIP:
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 & 0x8000);
562 break;
563 case V4L2_CID_HFLIP:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300564 data = reg_read(client, MT9T031_READ_MODE_2);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300565 if (data < 0)
566 return -EIO;
567 ctrl->value = !!(data & 0x4000);
568 break;
569 case V4L2_CID_EXPOSURE_AUTO:
570 ctrl->value = mt9t031->autoexposure;
571 break;
572 }
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);
580 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300581 const struct v4l2_queryctrl *qctrl;
582 int data;
583
584 qctrl = soc_camera_find_qctrl(&mt9t031_ops, ctrl->id);
585
586 if (!qctrl)
587 return -EINVAL;
588
589 switch (ctrl->id) {
590 case V4L2_CID_VFLIP:
591 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300592 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300593 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300594 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300595 if (data < 0)
596 return -EIO;
597 break;
598 case V4L2_CID_HFLIP:
599 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300600 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300601 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300602 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300603 if (data < 0)
604 return -EIO;
605 break;
606 case V4L2_CID_GAIN:
607 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
608 return -EINVAL;
609 /* See Datasheet Table 7, Gain settings. */
610 if (ctrl->value <= qctrl->default_value) {
611 /* Pack it into 0..1 step 0.125, register values 0..8 */
612 unsigned long range = qctrl->default_value - qctrl->minimum;
613 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
614
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300615 dev_dbg(&client->dev, "Setting gain %d\n", data);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300616 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300617 if (data < 0)
618 return -EIO;
619 } else {
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300620 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300621 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
622 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300623 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300624 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300625 1015 + range / 2) / range + 9;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300626
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300627 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300628 data = gain;
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300629 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300630 data = ((gain - 32) * 16 + 16) / 32 + 80;
631 else
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300632 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
633 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300634
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300635 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300636 reg_read(client, MT9T031_GLOBAL_GAIN), data);
637 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300638 if (data < 0)
639 return -EIO;
640 }
641
642 /* Success */
643 icd->gain = ctrl->value;
644 break;
645 case V4L2_CID_EXPOSURE:
646 /* mt9t031 has maximum == default */
647 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
648 return -EINVAL;
649 else {
650 const unsigned long range = qctrl->maximum - qctrl->minimum;
651 const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
652 range / 2) / range + 1;
653 u32 old;
654
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300655 get_shutter(client, &old);
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300656 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300657 old, shutter);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300658 if (set_shutter(client, shutter) < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300659 return -EIO;
660 icd->exposure = ctrl->value;
661 mt9t031->autoexposure = 0;
662 }
663 break;
664 case V4L2_CID_EXPOSURE_AUTO:
665 if (ctrl->value) {
666 const u16 vblank = MT9T031_VERTICAL_BLANK;
667 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300668 if (set_shutter(client, mt9t031->rect.height +
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300669 icd->y_skip_top + vblank) < 0)
670 return -EIO;
671 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300672 icd->exposure = (shutter_max / 2 +
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300673 (mt9t031->rect.height +
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300674 icd->y_skip_top + vblank - 1) *
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300675 (qctrl->maximum - qctrl->minimum)) /
676 shutter_max + qctrl->minimum;
677 mt9t031->autoexposure = 1;
678 } else
679 mt9t031->autoexposure = 0;
680 break;
681 }
682 return 0;
683}
684
685/* Interface active, can use i2c. If it fails, it can indeed mean, that
686 * this wasn't our capture interface, so, we wait for the right one */
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 soc_camera_device *icd = client->dev.platform_data;
690 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300691 s32 data;
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;
703 icd->formats = mt9t031_colour_formats;
704 icd->num_formats = ARRAY_SIZE(mt9t031_colour_formats);
705 break;
706 default:
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300707 dev_err(&client->dev,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300708 "No MT9T031 chip detected, register read %x\n", data);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300709 return -ENODEV;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300710 }
711
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300712 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300713
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300714 return 0;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300715}
716
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300717static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
718 .g_ctrl = mt9t031_g_ctrl,
719 .s_ctrl = mt9t031_s_ctrl,
720 .g_chip_ident = mt9t031_g_chip_ident,
721#ifdef CONFIG_VIDEO_ADV_DEBUG
722 .g_register = mt9t031_g_register,
723 .s_register = mt9t031_s_register,
724#endif
725};
726
727static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
728 .s_stream = mt9t031_s_stream,
729 .s_fmt = mt9t031_s_fmt,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300730 .g_fmt = mt9t031_g_fmt,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300731 .try_fmt = mt9t031_try_fmt,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300732 .s_crop = mt9t031_s_crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300733 .g_crop = mt9t031_g_crop,
734 .cropcap = mt9t031_cropcap,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300735};
736
737static struct v4l2_subdev_ops mt9t031_subdev_ops = {
738 .core = &mt9t031_subdev_core_ops,
739 .video = &mt9t031_subdev_video_ops,
740};
741
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300742static int mt9t031_probe(struct i2c_client *client,
743 const struct i2c_device_id *did)
744{
745 struct mt9t031 *mt9t031;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300746 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300747 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300748 struct soc_camera_link *icl;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300749 int ret;
750
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300751 if (!icd) {
752 dev_err(&client->dev, "MT9T031: missing soc-camera data!\n");
753 return -EINVAL;
754 }
755
756 icl = to_soc_camera_link(icd);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300757 if (!icl) {
758 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
759 return -EINVAL;
760 }
761
762 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
763 dev_warn(&adapter->dev,
764 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
765 return -EIO;
766 }
767
768 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
769 if (!mt9t031)
770 return -ENOMEM;
771
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300772 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300773
774 /* Second stage probe - when a capture adapter is there */
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300775 icd->ops = &mt9t031_ops;
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300776 icd->y_skip_top = 0;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300777
778 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
779 mt9t031->rect.top = MT9T031_ROW_SKIP;
780 mt9t031->rect.width = MT9T031_MAX_WIDTH;
781 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
782
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300783 /* Simulated autoexposure. If enabled, we calculate shutter width
784 * ourselves in the driver based on vertical blanking and frame width */
785 mt9t031->autoexposure = 1;
786
787 mt9t031->xskip = 1;
788 mt9t031->yskip = 1;
789
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300790 mt9t031_idle(client);
791
792 ret = mt9t031_video_probe(client);
793
794 mt9t031_disable(client);
795
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300796 if (ret) {
797 icd->ops = NULL;
798 i2c_set_clientdata(client, NULL);
799 kfree(mt9t031);
800 }
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300801
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300802 return ret;
803}
804
805static int mt9t031_remove(struct i2c_client *client)
806{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300807 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300808 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300809
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300810 icd->ops = NULL;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300811 i2c_set_clientdata(client, NULL);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300812 client->driver = NULL;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300813 kfree(mt9t031);
814
815 return 0;
816}
817
818static const struct i2c_device_id mt9t031_id[] = {
819 { "mt9t031", 0 },
820 { }
821};
822MODULE_DEVICE_TABLE(i2c, mt9t031_id);
823
824static struct i2c_driver mt9t031_i2c_driver = {
825 .driver = {
826 .name = "mt9t031",
827 },
828 .probe = mt9t031_probe,
829 .remove = mt9t031_remove,
830 .id_table = mt9t031_id,
831};
832
833static int __init mt9t031_mod_init(void)
834{
835 return i2c_add_driver(&mt9t031_i2c_driver);
836}
837
838static void __exit mt9t031_mod_exit(void)
839{
840 i2c_del_driver(&mt9t031_i2c_driver);
841}
842
843module_init(mt9t031_mod_init);
844module_exit(mt9t031_mod_exit);
845
846MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
847MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
848MODULE_LICENSE("GPL v2");