blob: 30547cc3f89b374e286ddf9da1105bf9317db2df [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
Valentin Longchamp535653b2010-02-09 07:59:12 -030011#include <linux/device.h>
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030012#include <linux/i2c.h>
13#include <linux/log2.h>
Valentin Longchamp535653b2010-02-09 07:59:12 -030014#include <linux/pm.h>
15#include <linux/slab.h>
16#include <linux/videodev2.h>
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030017
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030018#include <media/soc_camera.h>
Valentin Longchamp535653b2010-02-09 07:59:12 -030019#include <media/v4l2-chip-ident.h>
20#include <media/v4l2-subdev.h>
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030021
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030022/*
23 * mt9t031 i2c address 0x5d
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030024 * The platform has to define i2c_board_info and link to it from
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030025 * struct soc_camera_link
26 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030027
28/* mt9t031 selected register addresses */
29#define MT9T031_CHIP_VERSION 0x00
30#define MT9T031_ROW_START 0x01
31#define MT9T031_COLUMN_START 0x02
32#define MT9T031_WINDOW_HEIGHT 0x03
33#define MT9T031_WINDOW_WIDTH 0x04
34#define MT9T031_HORIZONTAL_BLANKING 0x05
35#define MT9T031_VERTICAL_BLANKING 0x06
36#define MT9T031_OUTPUT_CONTROL 0x07
37#define MT9T031_SHUTTER_WIDTH_UPPER 0x08
38#define MT9T031_SHUTTER_WIDTH 0x09
39#define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
40#define MT9T031_FRAME_RESTART 0x0b
41#define MT9T031_SHUTTER_DELAY 0x0c
42#define MT9T031_RESET 0x0d
43#define MT9T031_READ_MODE_1 0x1e
44#define MT9T031_READ_MODE_2 0x20
45#define MT9T031_READ_MODE_3 0x21
46#define MT9T031_ROW_ADDRESS_MODE 0x22
47#define MT9T031_COLUMN_ADDRESS_MODE 0x23
48#define MT9T031_GLOBAL_GAIN 0x35
49#define MT9T031_CHIP_ENABLE 0xF8
50
51#define MT9T031_MAX_HEIGHT 1536
52#define MT9T031_MAX_WIDTH 2048
53#define MT9T031_MIN_HEIGHT 2
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030054#define MT9T031_MIN_WIDTH 18
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030055#define MT9T031_HORIZONTAL_BLANK 142
56#define MT9T031_VERTICAL_BLANK 25
57#define MT9T031_COLUMN_SKIP 32
58#define MT9T031_ROW_SKIP 20
59
60#define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \
61 SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \
62 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \
63 SOCAM_MASTER | SOCAM_DATAWIDTH_10)
64
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030065struct mt9t031 {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030066 struct v4l2_subdev subdev;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030067 struct v4l2_rect rect; /* Sensor window */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030068 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030069 u16 xskip;
70 u16 yskip;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -030071 unsigned int gain;
Guennadi Liakhovetski32536102009-12-11 11:14:46 -030072 unsigned short y_skip_top; /* Lines to skip at the top */
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -030073 unsigned int exposure;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030074 unsigned char autoexposure;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030075};
76
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030077static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
78{
79 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
80}
81
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030082static int reg_read(struct i2c_client *client, const u8 reg)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030083{
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030084 s32 data = i2c_smbus_read_word_data(client, reg);
85 return data < 0 ? data : swab16(data);
86}
87
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030088static int reg_write(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030089 const u16 data)
90{
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030091 return i2c_smbus_write_word_data(client, reg, swab16(data));
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030092}
93
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030094static int reg_set(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -030095 const u16 data)
96{
97 int ret;
98
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -030099 ret = reg_read(client, reg);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300100 if (ret < 0)
101 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300102 return reg_write(client, reg, ret | data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300103}
104
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300105static int reg_clear(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300106 const u16 data)
107{
108 int ret;
109
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300110 ret = reg_read(client, reg);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300111 if (ret < 0)
112 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300113 return reg_write(client, reg, ret & ~data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300114}
115
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300116static int set_shutter(struct i2c_client *client, const u32 data)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300117{
118 int ret;
119
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300120 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300121
122 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300123 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300124
125 return ret;
126}
127
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300128static int get_shutter(struct i2c_client *client, u32 *data)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300129{
130 int ret;
131
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300132 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300133 *data = ret << 16;
134
135 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300136 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300137 *data |= ret & 0xffff;
138
139 return ret < 0 ? ret : 0;
140}
141
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300142static int mt9t031_idle(struct i2c_client *client)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300143{
144 int ret;
145
146 /* Disable chip output, synchronous option update */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300147 ret = reg_write(client, MT9T031_RESET, 1);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300148 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300149 ret = reg_write(client, MT9T031_RESET, 0);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300150 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300151 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300152
153 return ret >= 0 ? 0 : -EIO;
154}
155
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300156static int mt9t031_disable(struct i2c_client *client)
157{
158 /* Disable the chip */
159 reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
160
161 return 0;
162}
163
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300164static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300165{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300166 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300167 int ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300168
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300169 if (enable)
170 /* Switch to master "normal" mode */
171 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
172 else
173 /* Stop sensor readout */
174 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
175
176 if (ret < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300177 return -EIO;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300178
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300179 return 0;
180}
181
182static int mt9t031_set_bus_param(struct soc_camera_device *icd,
183 unsigned long flags)
184{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300185 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300186
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300187 /* The caller should have queried our parameters, check anyway */
188 if (flags & ~MT9T031_BUS_PARAM)
189 return -EINVAL;
190
191 if (flags & SOCAM_PCLK_SAMPLE_FALLING)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300192 reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
Philippe Rétornazc98afbf2009-03-13 09:42:32 -0300193 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300194 reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300195
196 return 0;
197}
198
199static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
200{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300201 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300202
203 return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
204}
205
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300206enum {
207 MT9T031_CTRL_VFLIP,
208 MT9T031_CTRL_HFLIP,
209 MT9T031_CTRL_GAIN,
210 MT9T031_CTRL_EXPOSURE,
211 MT9T031_CTRL_EXPOSURE_AUTO,
212};
213
214static const struct v4l2_queryctrl mt9t031_controls[] = {
215 [MT9T031_CTRL_VFLIP] = {
216 .id = V4L2_CID_VFLIP,
217 .type = V4L2_CTRL_TYPE_BOOLEAN,
218 .name = "Flip Vertically",
219 .minimum = 0,
220 .maximum = 1,
221 .step = 1,
222 .default_value = 0,
223 },
224 [MT9T031_CTRL_HFLIP] = {
225 .id = V4L2_CID_HFLIP,
226 .type = V4L2_CTRL_TYPE_BOOLEAN,
227 .name = "Flip Horizontally",
228 .minimum = 0,
229 .maximum = 1,
230 .step = 1,
231 .default_value = 0,
232 },
233 [MT9T031_CTRL_GAIN] = {
234 .id = V4L2_CID_GAIN,
235 .type = V4L2_CTRL_TYPE_INTEGER,
236 .name = "Gain",
237 .minimum = 0,
238 .maximum = 127,
239 .step = 1,
240 .default_value = 64,
241 .flags = V4L2_CTRL_FLAG_SLIDER,
242 },
243 [MT9T031_CTRL_EXPOSURE] = {
244 .id = V4L2_CID_EXPOSURE,
245 .type = V4L2_CTRL_TYPE_INTEGER,
246 .name = "Exposure",
247 .minimum = 1,
248 .maximum = 255,
249 .step = 1,
250 .default_value = 255,
251 .flags = V4L2_CTRL_FLAG_SLIDER,
252 },
253 [MT9T031_CTRL_EXPOSURE_AUTO] = {
254 .id = V4L2_CID_EXPOSURE_AUTO,
255 .type = V4L2_CTRL_TYPE_BOOLEAN,
256 .name = "Automatic Exposure",
257 .minimum = 0,
258 .maximum = 1,
259 .step = 1,
260 .default_value = 1,
261 }
262};
263
264static struct soc_camera_ops mt9t031_ops = {
265 .set_bus_param = mt9t031_set_bus_param,
266 .query_bus_param = mt9t031_query_bus_param,
267 .controls = mt9t031_controls,
268 .num_controls = ARRAY_SIZE(mt9t031_controls),
269};
270
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300271/* target must be _even_ */
272static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300273{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300274 unsigned int skip;
275
276 if (*source < target + target / 2) {
277 *source = target;
278 return 1;
279 }
280
281 skip = min(max, *source + target / 2) / target;
282 if (skip > 8)
283 skip = 8;
284 *source = target * skip;
285
286 return skip;
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300287}
288
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300289/* rect is the sensor rectangle, the caller guarantees parameter validity */
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300290static int mt9t031_set_params(struct i2c_client *client,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300291 struct v4l2_rect *rect, u16 xskip, u16 yskip)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300292{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300293 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300294 int ret;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300295 u16 xbin, ybin;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300296 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
297 vblank = MT9T031_VERTICAL_BLANK;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300298
299 xbin = min(xskip, (u16)3);
300 ybin = min(yskip, (u16)3);
301
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300302 /*
303 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
304 * There is always a valid suitably aligned value. The worst case is
305 * xbin = 3, width = 2048. Then we will start at 36, the last read out
306 * pixel will be 2083, which is < 2085 - first black pixel.
307 *
308 * MT9T031 datasheet imposes window left border alignment, depending on
309 * the selected xskip. Failing to conform to this requirement produces
310 * dark horizontal stripes in the image. However, even obeying to this
311 * requirement doesn't eliminate the stripes in all configurations. They
312 * appear "locally reproducibly," but can differ between tests under
313 * different lighting conditions.
314 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300315 switch (xbin) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300316 case 1:
317 rect->left &= ~1;
318 break;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300319 case 2:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300320 rect->left &= ~3;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300321 break;
322 case 3:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300323 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
324 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300325 }
326
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300327 rect->top &= ~1;
328
329 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
330 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300331
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300332 /* Disable register update, reconfigure atomically */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300333 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300334 if (ret < 0)
335 return ret;
336
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300337 /* Blanking and start values - default... */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300338 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300339 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300340 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300341
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300342 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300343 /* Binning, skipping */
344 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300345 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300346 ((xbin - 1) << 4) | (xskip - 1));
347 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300348 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300349 ((ybin - 1) << 4) | (yskip - 1));
350 }
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300351 dev_dbg(&client->dev, "new physical left %u, top %u\n",
352 rect->left, rect->top);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300353
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300354 /*
355 * The caller provides a supported format, as guaranteed by
356 * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap()
357 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300358 if (ret >= 0)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300359 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300360 if (ret >= 0)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300361 ret = reg_write(client, MT9T031_ROW_START, rect->top);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300362 if (ret >= 0)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300363 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300364 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300365 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300366 rect->height + mt9t031->y_skip_top - 1);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300367 if (ret >= 0 && mt9t031->autoexposure) {
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300368 unsigned int total_h = rect->height + mt9t031->y_skip_top + vblank;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300369 ret = set_shutter(client, total_h);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300370 if (ret >= 0) {
371 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
372 const struct v4l2_queryctrl *qctrl =
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300373 &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300374 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
375 (qctrl->maximum - qctrl->minimum)) /
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300376 shutter_max + qctrl->minimum;
377 }
378 }
379
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300380 /* Re-enable register update, commit all changes */
381 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300382 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300383
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300384 if (ret >= 0) {
385 mt9t031->rect = *rect;
386 mt9t031->xskip = xskip;
387 mt9t031->yskip = yskip;
388 }
389
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300390 return ret < 0 ? ret : 0;
391}
392
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300393static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300394{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300395 struct v4l2_rect rect = a->c;
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300396 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300397 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300398
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300399 rect.width = ALIGN(rect.width, 2);
400 rect.height = ALIGN(rect.height, 2);
Guennadi Liakhovetskie3309192009-08-25 11:46:42 -0300401
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300402 soc_camera_limit_side(&rect.left, &rect.width,
403 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
Guennadi Liakhovetskie3309192009-08-25 11:46:42 -0300404
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300405 soc_camera_limit_side(&rect.top, &rect.height,
406 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
407
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300408 return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300409}
410
411static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
412{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300413 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300414 struct mt9t031 *mt9t031 = to_mt9t031(client);
415
416 a->c = mt9t031->rect;
417 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
418
419 return 0;
420}
421
422static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
423{
424 a->bounds.left = MT9T031_COLUMN_SKIP;
425 a->bounds.top = MT9T031_ROW_SKIP;
426 a->bounds.width = MT9T031_MAX_WIDTH;
427 a->bounds.height = MT9T031_MAX_HEIGHT;
428 a->defrect = a->bounds;
429 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
430 a->pixelaspect.numerator = 1;
431 a->pixelaspect.denominator = 1;
432
433 return 0;
434}
435
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300436static int mt9t031_g_fmt(struct v4l2_subdev *sd,
437 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300438{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300439 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300440 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300441
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300442 mf->width = mt9t031->rect.width / mt9t031->xskip;
443 mf->height = mt9t031->rect.height / mt9t031->yskip;
444 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
445 mf->colorspace = V4L2_COLORSPACE_SRGB;
446 mf->field = V4L2_FIELD_NONE;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300447
448 return 0;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300449}
450
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300451static int mt9t031_s_fmt(struct v4l2_subdev *sd,
452 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300453{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300454 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300455 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300456 u16 xskip, yskip;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300457 struct v4l2_rect rect = mt9t031->rect;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300458
459 /*
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300460 * try_fmt has put width and height within limits.
461 * S_FMT: use binning and skipping for scaling
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300462 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300463 xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
464 yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
465
466 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
467 mf->colorspace = V4L2_COLORSPACE_SRGB;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300468
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300469 /* mt9t031_set_params() doesn't change width and height */
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300470 return mt9t031_set_params(client, &rect, xskip, yskip);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300471}
472
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300473/*
474 * If a user window larger than sensor window is requested, we'll increase the
475 * sensor window.
476 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300477static int mt9t031_try_fmt(struct v4l2_subdev *sd,
478 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300479{
Trent Piepho653dc592009-05-30 21:45:46 -0300480 v4l_bound_align_image(
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300481 &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
482 &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
483
484 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
485 mf->colorspace = V4L2_COLORSPACE_SRGB;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300486
487 return 0;
488}
489
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300490static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
491 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300492{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300493 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300494 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300495
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300496 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300497 return -EINVAL;
498
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300499 if (id->match.addr != client->addr)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300500 return -ENODEV;
501
502 id->ident = mt9t031->model;
503 id->revision = 0;
504
505 return 0;
506}
507
508#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300509static int mt9t031_g_register(struct v4l2_subdev *sd,
510 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300511{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300512 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300513
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300514 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300515 return -EINVAL;
516
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300517 if (reg->match.addr != client->addr)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300518 return -ENODEV;
519
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300520 reg->val = reg_read(client, reg->reg);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300521
522 if (reg->val > 0xffff)
523 return -EIO;
524
525 return 0;
526}
527
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300528static int mt9t031_s_register(struct v4l2_subdev *sd,
529 struct v4l2_dbg_register *reg)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300530{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300531 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300532
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300533 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300534 return -EINVAL;
535
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300536 if (reg->match.addr != client->addr)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300537 return -ENODEV;
538
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300539 if (reg_write(client, reg->reg, reg->val) < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300540 return -EIO;
541
542 return 0;
543}
544#endif
545
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300546static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300547{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300548 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300549 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300550 int data;
551
552 switch (ctrl->id) {
553 case V4L2_CID_VFLIP:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300554 data = reg_read(client, MT9T031_READ_MODE_2);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300555 if (data < 0)
556 return -EIO;
557 ctrl->value = !!(data & 0x8000);
558 break;
559 case V4L2_CID_HFLIP:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300560 data = reg_read(client, MT9T031_READ_MODE_2);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300561 if (data < 0)
562 return -EIO;
563 ctrl->value = !!(data & 0x4000);
564 break;
565 case V4L2_CID_EXPOSURE_AUTO:
566 ctrl->value = mt9t031->autoexposure;
567 break;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300568 case V4L2_CID_GAIN:
569 ctrl->value = mt9t031->gain;
570 break;
571 case V4L2_CID_EXPOSURE:
572 ctrl->value = mt9t031->exposure;
573 break;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300574 }
575 return 0;
576}
577
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300578static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300579{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300580 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300581 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300582 const struct v4l2_queryctrl *qctrl;
583 int data;
584
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300585 switch (ctrl->id) {
586 case V4L2_CID_VFLIP:
587 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300588 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300589 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300590 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300591 if (data < 0)
592 return -EIO;
593 break;
594 case V4L2_CID_HFLIP:
595 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300596 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300597 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300598 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300599 if (data < 0)
600 return -EIO;
601 break;
602 case V4L2_CID_GAIN:
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300603 qctrl = &mt9t031_controls[MT9T031_CTRL_GAIN];
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300604 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
605 return -EINVAL;
606 /* See Datasheet Table 7, Gain settings. */
607 if (ctrl->value <= qctrl->default_value) {
608 /* Pack it into 0..1 step 0.125, register values 0..8 */
609 unsigned long range = qctrl->default_value - qctrl->minimum;
610 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
611
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300612 dev_dbg(&client->dev, "Setting gain %d\n", data);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300613 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300614 if (data < 0)
615 return -EIO;
616 } else {
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300617 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300618 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
619 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300620 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300621 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300622 1015 + range / 2) / range + 9;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300623
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300624 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300625 data = gain;
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300626 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300627 data = ((gain - 32) * 16 + 16) / 32 + 80;
628 else
Guennadi Liakhovetski70e1d352009-02-23 12:13:23 -0300629 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
630 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300631
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300632 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300633 reg_read(client, MT9T031_GLOBAL_GAIN), data);
634 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300635 if (data < 0)
636 return -EIO;
637 }
638
639 /* Success */
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300640 mt9t031->gain = ctrl->value;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300641 break;
642 case V4L2_CID_EXPOSURE:
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300643 qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300644 /* mt9t031 has maximum == default */
645 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
646 return -EINVAL;
647 else {
648 const unsigned long range = qctrl->maximum - qctrl->minimum;
649 const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
650 range / 2) / range + 1;
651 u32 old;
652
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300653 get_shutter(client, &old);
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300654 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300655 old, shutter);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300656 if (set_shutter(client, shutter) < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300657 return -EIO;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300658 mt9t031->exposure = ctrl->value;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300659 mt9t031->autoexposure = 0;
660 }
661 break;
662 case V4L2_CID_EXPOSURE_AUTO:
663 if (ctrl->value) {
664 const u16 vblank = MT9T031_VERTICAL_BLANK;
665 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300666 unsigned int total_h = mt9t031->rect.height +
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300667 mt9t031->y_skip_top + vblank;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300668
669 if (set_shutter(client, total_h) < 0)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300670 return -EIO;
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300671 qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300672 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
673 (qctrl->maximum - qctrl->minimum)) /
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300674 shutter_max + qctrl->minimum;
675 mt9t031->autoexposure = 1;
676 } else
677 mt9t031->autoexposure = 0;
678 break;
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300679 default:
680 return -EINVAL;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300681 }
682 return 0;
683}
684
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300685/*
Valentin Longchamp535653b2010-02-09 07:59:12 -0300686 * Power Management:
687 * This function does nothing for now but must be present for pm to work
688 */
689static int mt9t031_runtime_suspend(struct device *dev)
690{
691 return 0;
692}
693
694/*
695 * Power Management:
696 * COLUMN_ADDRESS_MODE and ROW_ADDRESS_MODE are not rewritten if unchanged
697 * they are however changed at reset if the platform hook is present
698 * thus we rewrite them with the values stored by the driver
699 */
700static int mt9t031_runtime_resume(struct device *dev)
701{
702 struct video_device *vdev = to_video_device(dev);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300703 struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
Valentin Longchamp535653b2010-02-09 07:59:12 -0300704 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300705 struct i2c_client *client = v4l2_get_subdevdata(sd);
Valentin Longchamp535653b2010-02-09 07:59:12 -0300706 struct mt9t031 *mt9t031 = to_mt9t031(client);
707
708 int ret;
709 u16 xbin, ybin;
710
711 xbin = min(mt9t031->xskip, (u16)3);
712 ybin = min(mt9t031->yskip, (u16)3);
713
714 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
715 ((xbin - 1) << 4) | (mt9t031->xskip - 1));
716 if (ret < 0)
717 return ret;
718
719 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
720 ((ybin - 1) << 4) | (mt9t031->yskip - 1));
721 if (ret < 0)
722 return ret;
723
724 return 0;
725}
726
727static struct dev_pm_ops mt9t031_dev_pm_ops = {
728 .runtime_suspend = mt9t031_runtime_suspend,
729 .runtime_resume = mt9t031_runtime_resume,
730};
731
732static struct device_type mt9t031_dev_type = {
733 .name = "MT9T031",
734 .pm = &mt9t031_dev_pm_ops,
735};
736
737/*
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300738 * Interface active, can use i2c. If it fails, it can indeed mean, that
739 * this wasn't our capture interface, so, we wait for the right one
740 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300741static int mt9t031_video_probe(struct i2c_client *client)
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300742{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300743 struct mt9t031 *mt9t031 = to_mt9t031(client);
Valentin Longchamp535653b2010-02-09 07:59:12 -0300744 struct video_device *vdev = soc_camera_i2c_to_vdev(client);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300745 s32 data;
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300746 int ret;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300747
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300748 /* Enable the chip */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300749 data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300750 dev_dbg(&client->dev, "write: %d\n", data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300751
752 /* Read out the chip version register */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300753 data = reg_read(client, MT9T031_CHIP_VERSION);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300754
755 switch (data) {
756 case 0x1621:
757 mt9t031->model = V4L2_IDENT_MT9T031;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300758 break;
759 default:
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300760 dev_err(&client->dev,
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300761 "No MT9T031 chip detected, register read %x\n", data);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300762 return -ENODEV;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300763 }
764
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300765 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300766
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300767 ret = mt9t031_idle(client);
768 if (ret < 0)
769 dev_err(&client->dev, "Failed to initialise the camera\n");
Valentin Longchamp535653b2010-02-09 07:59:12 -0300770 else
771 vdev->dev.type = &mt9t031_dev_type;
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300772
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300773 /* mt9t031_idle() has reset the chip to default. */
774 mt9t031->exposure = 255;
775 mt9t031->gain = 64;
776
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300777 return ret;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300778}
779
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300780static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
781{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300782 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300783 struct mt9t031 *mt9t031 = to_mt9t031(client);
784
785 *lines = mt9t031->y_skip_top;
786
787 return 0;
788}
789
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300790static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
791 .g_ctrl = mt9t031_g_ctrl,
792 .s_ctrl = mt9t031_s_ctrl,
793 .g_chip_ident = mt9t031_g_chip_ident,
794#ifdef CONFIG_VIDEO_ADV_DEBUG
795 .g_register = mt9t031_g_register,
796 .s_register = mt9t031_s_register,
797#endif
798};
799
Hans Verkuil3805f202010-05-08 17:55:00 -0300800static int mt9t031_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300801 enum v4l2_mbus_pixelcode *code)
802{
803 if (index)
804 return -EINVAL;
805
806 *code = V4L2_MBUS_FMT_SBGGR10_1X10;
807 return 0;
808}
809
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300810static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
811 .s_stream = mt9t031_s_stream,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300812 .s_mbus_fmt = mt9t031_s_fmt,
813 .g_mbus_fmt = mt9t031_g_fmt,
814 .try_mbus_fmt = mt9t031_try_fmt,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300815 .s_crop = mt9t031_s_crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300816 .g_crop = mt9t031_g_crop,
817 .cropcap = mt9t031_cropcap,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300818 .enum_mbus_fmt = mt9t031_enum_fmt,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300819};
820
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300821static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
822 .g_skip_top_lines = mt9t031_g_skip_top_lines,
823};
824
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300825static struct v4l2_subdev_ops mt9t031_subdev_ops = {
826 .core = &mt9t031_subdev_core_ops,
827 .video = &mt9t031_subdev_video_ops,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300828 .sensor = &mt9t031_subdev_sensor_ops,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300829};
830
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300831static int mt9t031_probe(struct i2c_client *client,
832 const struct i2c_device_id *did)
833{
834 struct mt9t031 *mt9t031;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300835 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300836 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300837 int ret;
838
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300839 if (icd) {
840 struct soc_camera_link *icl = to_soc_camera_link(icd);
841 if (!icl) {
842 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
843 return -EINVAL;
844 }
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300845
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300846 icd->ops = &mt9t031_ops;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300847 }
848
849 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
850 dev_warn(&adapter->dev,
851 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
852 return -EIO;
853 }
854
855 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
856 if (!mt9t031)
857 return -ENOMEM;
858
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300859 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300860
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300861 mt9t031->y_skip_top = 0;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300862 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
863 mt9t031->rect.top = MT9T031_ROW_SKIP;
864 mt9t031->rect.width = MT9T031_MAX_WIDTH;
865 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
866
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300867 /*
868 * Simulated autoexposure. If enabled, we calculate shutter width
869 * ourselves in the driver based on vertical blanking and frame width
870 */
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300871 mt9t031->autoexposure = 1;
872
873 mt9t031->xskip = 1;
874 mt9t031->yskip = 1;
875
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300876 mt9t031_idle(client);
877
878 ret = mt9t031_video_probe(client);
879
880 mt9t031_disable(client);
881
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300882 if (ret) {
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300883 if (icd)
884 icd->ops = NULL;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300885 kfree(mt9t031);
886 }
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300887
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300888 return ret;
889}
890
891static int mt9t031_remove(struct i2c_client *client)
892{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300893 struct mt9t031 *mt9t031 = to_mt9t031(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300894 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300895
Guennadi Liakhovetskia3a4ac12009-12-11 11:53:53 -0300896 if (icd)
897 icd->ops = NULL;
Guennadi Liakhovetski4e96fd02008-12-29 06:04:59 -0300898 kfree(mt9t031);
899
900 return 0;
901}
902
903static const struct i2c_device_id mt9t031_id[] = {
904 { "mt9t031", 0 },
905 { }
906};
907MODULE_DEVICE_TABLE(i2c, mt9t031_id);
908
909static struct i2c_driver mt9t031_i2c_driver = {
910 .driver = {
911 .name = "mt9t031",
912 },
913 .probe = mt9t031_probe,
914 .remove = mt9t031_remove,
915 .id_table = mt9t031_id,
916};
917
918static int __init mt9t031_mod_init(void)
919{
920 return i2c_add_driver(&mt9t031_i2c_driver);
921}
922
923static void __exit mt9t031_mod_exit(void)
924{
925 i2c_del_driver(&mt9t031_i2c_driver);
926}
927
928module_init(mt9t031_mod_init);
929module_exit(mt9t031_mod_exit);
930
931MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
932MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
933MODULE_LICENSE("GPL v2");