blob: 992ab8cb89ae0bc59671b4cb57f1d0617e484002 [file] [log] [blame]
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -03001/*
2 * Driver for MT9M001 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/videodev2.h>
12#include <linux/slab.h>
13#include <linux/i2c.h>
14#include <linux/log2.h>
15
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030016#include <media/v4l2-subdev.h>
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030017#include <media/v4l2-chip-ident.h>
18#include <media/soc_camera.h>
19
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030020/*
21 * mt9m001 i2c address 0x5d
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030022 * The platform has to define ctruct i2c_board_info objects and link to them
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030023 * from struct soc_camera_link
24 */
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030025
26/* mt9m001 selected register addresses */
27#define MT9M001_CHIP_VERSION 0x00
28#define MT9M001_ROW_START 0x01
29#define MT9M001_COLUMN_START 0x02
30#define MT9M001_WINDOW_HEIGHT 0x03
31#define MT9M001_WINDOW_WIDTH 0x04
32#define MT9M001_HORIZONTAL_BLANKING 0x05
33#define MT9M001_VERTICAL_BLANKING 0x06
34#define MT9M001_OUTPUT_CONTROL 0x07
35#define MT9M001_SHUTTER_WIDTH 0x09
36#define MT9M001_FRAME_RESTART 0x0b
37#define MT9M001_SHUTTER_DELAY 0x0c
38#define MT9M001_RESET 0x0d
39#define MT9M001_READ_OPTIONS1 0x1e
40#define MT9M001_READ_OPTIONS2 0x20
41#define MT9M001_GLOBAL_GAIN 0x35
42#define MT9M001_CHIP_ENABLE 0xF1
43
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030044#define MT9M001_MAX_WIDTH 1280
45#define MT9M001_MAX_HEIGHT 1024
46#define MT9M001_MIN_WIDTH 48
47#define MT9M001_MIN_HEIGHT 32
48#define MT9M001_COLUMN_SKIP 20
49#define MT9M001_ROW_SKIP 12
50
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030051/* MT9M001 has only one fixed colorspace per pixelcode */
52struct mt9m001_datafmt {
53 enum v4l2_mbus_pixelcode code;
54 enum v4l2_colorspace colorspace;
55};
56
57/* Find a data format by a pixel code in an array */
58static const struct mt9m001_datafmt *mt9m001_find_datafmt(
59 enum v4l2_mbus_pixelcode code, const struct mt9m001_datafmt *fmt,
60 int n)
61{
62 int i;
63 for (i = 0; i < n; i++)
64 if (fmt[i].code == code)
65 return fmt + i;
66
67 return NULL;
68}
69
70static const struct mt9m001_datafmt mt9m001_colour_fmts[] = {
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030071 /*
72 * Order important: first natively supported,
73 * second supported with a GPIO extender
74 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030075 {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
76 {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030077};
78
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030079static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030080 /* Order important - see above */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030081 {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
82 {V4L2_MBUS_FMT_GREY8_1X8, V4L2_COLORSPACE_JPEG},
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030083};
84
85struct mt9m001 {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030086 struct v4l2_subdev subdev;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030087 struct v4l2_rect rect; /* Sensor window */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030088 const struct mt9m001_datafmt *fmt;
89 const struct mt9m001_datafmt *fmts;
90 int num_fmts;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030091 int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -030092 unsigned int gain;
93 unsigned int exposure;
Guennadi Liakhovetski32536102009-12-11 11:14:46 -030094 unsigned short y_skip_top; /* Lines to skip at the top */
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030095 unsigned char autoexposure;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030096};
97
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030098static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
99{
100 return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
101}
102
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300103static int reg_read(struct i2c_client *client, const u8 reg)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300104{
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300105 s32 data = i2c_smbus_read_word_data(client, reg);
106 return data < 0 ? data : swab16(data);
107}
108
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300109static int reg_write(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300110 const u16 data)
111{
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300112 return i2c_smbus_write_word_data(client, reg, swab16(data));
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300113}
114
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300115static int reg_set(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300116 const u16 data)
117{
118 int ret;
119
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300120 ret = reg_read(client, reg);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300121 if (ret < 0)
122 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300123 return reg_write(client, reg, ret | data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300124}
125
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300126static int reg_clear(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300127 const u16 data)
128{
129 int ret;
130
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300131 ret = reg_read(client, reg);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300132 if (ret < 0)
133 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300134 return reg_write(client, reg, ret & ~data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300135}
136
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300137static int mt9m001_init(struct i2c_client *client)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300138{
139 int ret;
140
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300141 dev_dbg(&client->dev, "%s\n", __func__);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300142
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300143 /*
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300144 * We don't know, whether platform provides reset, issue a soft reset
145 * too. This returns all registers to their default values.
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300146 */
147 ret = reg_write(client, MT9M001_RESET, 1);
148 if (!ret)
149 ret = reg_write(client, MT9M001_RESET, 0);
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300150
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300151 /* Disable chip, synchronous option update */
152 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300153 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300154
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300155 return ret;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300156}
157
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300158static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300159{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300160 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300161
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300162 /* Switch to master "normal" mode or stop sensor readout */
163 if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300164 return -EIO;
165 return 0;
166}
167
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300168static int mt9m001_set_bus_param(struct soc_camera_device *icd,
169 unsigned long flags)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300170{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300171 struct soc_camera_link *icl = to_soc_camera_link(icd);
Sascha Hauer36034dc2009-03-13 06:08:20 -0300172 unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300173
Sascha Hauer36034dc2009-03-13 06:08:20 -0300174 /* Only one width bit may be set */
175 if (!is_power_of_2(width_flag))
176 return -EINVAL;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300177
Sascha Hauer36034dc2009-03-13 06:08:20 -0300178 if (icl->set_bus_param)
179 return icl->set_bus_param(icl, width_flag);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300180
Sascha Hauer36034dc2009-03-13 06:08:20 -0300181 /*
182 * Without board specific bus width settings we only support the
183 * sensors native bus width
184 */
185 if (width_flag == SOCAM_DATAWIDTH_10)
186 return 0;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300187
Sascha Hauer36034dc2009-03-13 06:08:20 -0300188 return -EINVAL;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300189}
190
191static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
192{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300193 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300194 /* MT9M001 has all capture_format parameters fixed */
Guennadi Liakhovetskie951cbf2009-03-31 03:44:22 -0300195 unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300196 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
Guennadi Liakhovetski2d9329f2009-02-23 12:12:58 -0300197 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300198
Sascha Hauer36034dc2009-03-13 06:08:20 -0300199 if (icl->query_bus_param)
200 flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
201 else
202 flags |= SOCAM_DATAWIDTH_10;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300203
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300204 return soc_camera_apply_sensor_flags(icl, flags);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300205}
206
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300207static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300208{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300209 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300210 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300211 struct v4l2_rect rect = a->c;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300212 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300213 int ret;
214 const u16 hblank = 9, vblank = 25;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300215 unsigned int total_h;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300216
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300217 if (mt9m001->fmts == mt9m001_colour_fmts)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300218 /*
219 * Bayer format - even number of rows for simplicity,
220 * but let the user play with the top row.
221 */
222 rect.height = ALIGN(rect.height, 2);
223
224 /* Datasheet requirement: see register description */
225 rect.width = ALIGN(rect.width, 2);
226 rect.left = ALIGN(rect.left, 2);
227
228 soc_camera_limit_side(&rect.left, &rect.width,
229 MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH);
230
231 soc_camera_limit_side(&rect.top, &rect.height,
232 MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT);
233
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300234 total_h = rect.height + mt9m001->y_skip_top + vblank;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300235
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300236 /* Blanking and start values - default... */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300237 ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300238 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300239 ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300240
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300241 /*
242 * The caller provides a supported format, as verified per
243 * call to icd->try_fmt()
244 */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300245 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300246 ret = reg_write(client, MT9M001_COLUMN_START, rect.left);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300247 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300248 ret = reg_write(client, MT9M001_ROW_START, rect.top);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300249 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300250 ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300251 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300252 ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300253 rect.height + mt9m001->y_skip_top - 1);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300254 if (!ret && mt9m001->autoexposure) {
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300255 ret = reg_write(client, MT9M001_SHUTTER_WIDTH, total_h);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300256 if (!ret) {
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300257 const struct v4l2_queryctrl *qctrl =
258 soc_camera_find_qctrl(icd->ops,
259 V4L2_CID_EXPOSURE);
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300260 mt9m001->exposure = (524 + (total_h - 1) *
261 (qctrl->maximum - qctrl->minimum)) /
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300262 1048 + qctrl->minimum;
263 }
264 }
265
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300266 if (!ret)
267 mt9m001->rect = rect;
268
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300269 return ret;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300270}
271
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300272static int mt9m001_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
273{
274 struct i2c_client *client = sd->priv;
275 struct mt9m001 *mt9m001 = to_mt9m001(client);
276
277 a->c = mt9m001->rect;
278 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
279
280 return 0;
281}
282
283static int mt9m001_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
284{
285 a->bounds.left = MT9M001_COLUMN_SKIP;
286 a->bounds.top = MT9M001_ROW_SKIP;
287 a->bounds.width = MT9M001_MAX_WIDTH;
288 a->bounds.height = MT9M001_MAX_HEIGHT;
289 a->defrect = a->bounds;
290 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
291 a->pixelaspect.numerator = 1;
292 a->pixelaspect.denominator = 1;
293
294 return 0;
295}
296
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300297static int mt9m001_g_fmt(struct v4l2_subdev *sd,
298 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300299{
300 struct i2c_client *client = sd->priv;
301 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300302
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300303 mf->width = mt9m001->rect.width;
304 mf->height = mt9m001->rect.height;
305 mf->code = mt9m001->fmt->code;
306 mf->colorspace = mt9m001->fmt->colorspace;
307 mf->field = V4L2_FIELD_NONE;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300308
309 return 0;
310}
311
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300312static int mt9m001_s_fmt(struct v4l2_subdev *sd,
313 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300314{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300315 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300316 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300317 struct v4l2_crop a = {
318 .c = {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300319 .left = mt9m001->rect.left,
320 .top = mt9m001->rect.top,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300321 .width = mf->width,
322 .height = mf->height,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300323 },
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300324 };
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300325 int ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300326
327 /* No support for scaling so far, just crop. TODO: use skipping */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300328 ret = mt9m001_s_crop(sd, &a);
329 if (!ret) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300330 mf->width = mt9m001->rect.width;
331 mf->height = mt9m001->rect.height;
332 mt9m001->fmt = mt9m001_find_datafmt(mf->code,
333 mt9m001->fmts, mt9m001->num_fmts);
334 mf->colorspace = mt9m001->fmt->colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300335 }
336
337 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300338}
339
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300340static int mt9m001_try_fmt(struct v4l2_subdev *sd,
341 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300342{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300343 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300344 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300345 const struct mt9m001_datafmt *fmt;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300346
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300347 v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300348 MT9M001_MAX_WIDTH, 1,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300349 &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300350 MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300351
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300352 if (mt9m001->fmts == mt9m001_colour_fmts)
353 mf->height = ALIGN(mf->height - 1, 2);
354
355 fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts,
356 mt9m001->num_fmts);
357 if (!fmt) {
358 fmt = mt9m001->fmt;
359 mf->code = fmt->code;
360 }
361
362 mf->colorspace = fmt->colorspace;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300363
364 return 0;
365}
366
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300367static int mt9m001_g_chip_ident(struct v4l2_subdev *sd,
368 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300369{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300370 struct i2c_client *client = sd->priv;
371 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300372
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300373 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300374 return -EINVAL;
375
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300376 if (id->match.addr != client->addr)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300377 return -ENODEV;
378
379 id->ident = mt9m001->model;
380 id->revision = 0;
381
382 return 0;
383}
384
385#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300386static int mt9m001_g_register(struct v4l2_subdev *sd,
387 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300388{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300389 struct i2c_client *client = sd->priv;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300390
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300391 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300392 return -EINVAL;
393
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300394 if (reg->match.addr != client->addr)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300395 return -ENODEV;
396
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300397 reg->size = 2;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300398 reg->val = reg_read(client, reg->reg);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300399
400 if (reg->val > 0xffff)
401 return -EIO;
402
403 return 0;
404}
405
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300406static int mt9m001_s_register(struct v4l2_subdev *sd,
407 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300408{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300409 struct i2c_client *client = sd->priv;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300410
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300411 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300412 return -EINVAL;
413
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300414 if (reg->match.addr != client->addr)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300415 return -ENODEV;
416
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300417 if (reg_write(client, reg->reg, reg->val) < 0)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300418 return -EIO;
419
420 return 0;
421}
422#endif
423
Adrian Bunk4407a462008-04-28 17:13:51 -0300424static const struct v4l2_queryctrl mt9m001_controls[] = {
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300425 {
426 .id = V4L2_CID_VFLIP,
427 .type = V4L2_CTRL_TYPE_BOOLEAN,
428 .name = "Flip Vertically",
429 .minimum = 0,
430 .maximum = 1,
431 .step = 1,
432 .default_value = 0,
433 }, {
434 .id = V4L2_CID_GAIN,
435 .type = V4L2_CTRL_TYPE_INTEGER,
436 .name = "Gain",
437 .minimum = 0,
438 .maximum = 127,
439 .step = 1,
440 .default_value = 64,
441 .flags = V4L2_CTRL_FLAG_SLIDER,
442 }, {
443 .id = V4L2_CID_EXPOSURE,
444 .type = V4L2_CTRL_TYPE_INTEGER,
445 .name = "Exposure",
446 .minimum = 1,
447 .maximum = 255,
448 .step = 1,
449 .default_value = 255,
450 .flags = V4L2_CTRL_FLAG_SLIDER,
451 }, {
452 .id = V4L2_CID_EXPOSURE_AUTO,
453 .type = V4L2_CTRL_TYPE_BOOLEAN,
454 .name = "Automatic Exposure",
455 .minimum = 0,
456 .maximum = 1,
457 .step = 1,
458 .default_value = 1,
459 }
460};
461
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300462static struct soc_camera_ops mt9m001_ops = {
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300463 .set_bus_param = mt9m001_set_bus_param,
464 .query_bus_param = mt9m001_query_bus_param,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300465 .controls = mt9m001_controls,
466 .num_controls = ARRAY_SIZE(mt9m001_controls),
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300467};
468
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300469static int mt9m001_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300470{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300471 struct i2c_client *client = sd->priv;
472 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300473 int data;
474
475 switch (ctrl->id) {
476 case V4L2_CID_VFLIP:
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300477 data = reg_read(client, MT9M001_READ_OPTIONS2);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300478 if (data < 0)
479 return -EIO;
480 ctrl->value = !!(data & 0x8000);
481 break;
482 case V4L2_CID_EXPOSURE_AUTO:
483 ctrl->value = mt9m001->autoexposure;
484 break;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300485 case V4L2_CID_GAIN:
486 ctrl->value = mt9m001->gain;
487 break;
488 case V4L2_CID_EXPOSURE:
489 ctrl->value = mt9m001->exposure;
490 break;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300491 }
492 return 0;
493}
494
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300495static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300496{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300497 struct i2c_client *client = sd->priv;
498 struct mt9m001 *mt9m001 = to_mt9m001(client);
499 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300500 const struct v4l2_queryctrl *qctrl;
501 int data;
502
503 qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
504
505 if (!qctrl)
506 return -EINVAL;
507
508 switch (ctrl->id) {
509 case V4L2_CID_VFLIP:
510 if (ctrl->value)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300511 data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300512 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300513 data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300514 if (data < 0)
515 return -EIO;
516 break;
517 case V4L2_CID_GAIN:
518 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
519 return -EINVAL;
520 /* See Datasheet Table 7, Gain settings. */
521 if (ctrl->value <= qctrl->default_value) {
522 /* Pack it into 0..1 step 0.125, register values 0..8 */
523 unsigned long range = qctrl->default_value - qctrl->minimum;
524 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
525
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300526 dev_dbg(&client->dev, "Setting gain %d\n", data);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300527 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300528 if (data < 0)
529 return -EIO;
530 } else {
531 /* Pack it into 1.125..15 variable step, register values 9..67 */
532 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
533 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
534 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
535 111 + range / 2) / range + 9;
536
537 if (gain <= 32)
538 data = gain;
539 else if (gain <= 64)
540 data = ((gain - 32) * 16 + 16) / 32 + 80;
541 else
542 data = ((gain - 64) * 7 + 28) / 56 + 96;
543
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300544 dev_dbg(&client->dev, "Setting gain from %d to %d\n",
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300545 reg_read(client, MT9M001_GLOBAL_GAIN), data);
546 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300547 if (data < 0)
548 return -EIO;
549 }
550
551 /* Success */
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300552 mt9m001->gain = ctrl->value;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300553 break;
554 case V4L2_CID_EXPOSURE:
555 /* mt9m001 has maximum == default */
556 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
557 return -EINVAL;
558 else {
559 unsigned long range = qctrl->maximum - qctrl->minimum;
560 unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
561 range / 2) / range + 1;
562
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300563 dev_dbg(&client->dev,
564 "Setting shutter width from %d to %lu\n",
565 reg_read(client, MT9M001_SHUTTER_WIDTH),
566 shutter);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300567 if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300568 return -EIO;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300569 mt9m001->exposure = ctrl->value;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300570 mt9m001->autoexposure = 0;
571 }
572 break;
573 case V4L2_CID_EXPOSURE_AUTO:
574 if (ctrl->value) {
575 const u16 vblank = 25;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300576 unsigned int total_h = mt9m001->rect.height +
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300577 mt9m001->y_skip_top + vblank;
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300578 if (reg_write(client, MT9M001_SHUTTER_WIDTH,
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300579 total_h) < 0)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300580 return -EIO;
581 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300582 mt9m001->exposure = (524 + (total_h - 1) *
583 (qctrl->maximum - qctrl->minimum)) /
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300584 1048 + qctrl->minimum;
585 mt9m001->autoexposure = 1;
586 } else
587 mt9m001->autoexposure = 0;
588 break;
589 }
590 return 0;
591}
592
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300593/*
594 * Interface active, can use i2c. If it fails, it can indeed mean, that
595 * this wasn't our capture interface, so, we wait for the right one
596 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300597static int mt9m001_video_probe(struct soc_camera_device *icd,
598 struct i2c_client *client)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300599{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300600 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300601 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300602 s32 data;
Sascha Hauer36034dc2009-03-13 06:08:20 -0300603 unsigned long flags;
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300604 int ret;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300605
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300606 /*
607 * We must have a parent by now. And it cannot be a wrong one.
608 * So this entire test is completely redundant.
609 */
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300610 if (!icd->dev.parent ||
611 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
612 return -ENODEV;
613
614 /* Enable the chip */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300615 data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300616 dev_dbg(&client->dev, "write: %d\n", data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300617
618 /* Read out the chip version register */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300619 data = reg_read(client, MT9M001_CHIP_VERSION);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300620
621 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
622 switch (data) {
623 case 0x8411:
624 case 0x8421:
625 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300626 mt9m001->fmts = mt9m001_colour_fmts;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300627 break;
628 case 0x8431:
629 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300630 mt9m001->fmts = mt9m001_monochrome_fmts;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300631 break;
632 default:
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300633 dev_err(&client->dev,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300634 "No MT9M001 chip detected, register read %x\n", data);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300635 return -ENODEV;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300636 }
637
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300638 mt9m001->num_fmts = 0;
Sascha Hauer36034dc2009-03-13 06:08:20 -0300639
640 /*
641 * This is a 10bit sensor, so by default we only allow 10bit.
642 * The platform may support different bus widths due to
643 * different routing of the data lines.
644 */
645 if (icl->query_bus_param)
646 flags = icl->query_bus_param(icl);
647 else
648 flags = SOCAM_DATAWIDTH_10;
649
650 if (flags & SOCAM_DATAWIDTH_10)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300651 mt9m001->num_fmts++;
Sascha Hauer36034dc2009-03-13 06:08:20 -0300652 else
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300653 mt9m001->fmts++;
Sascha Hauer36034dc2009-03-13 06:08:20 -0300654
655 if (flags & SOCAM_DATAWIDTH_8)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300656 mt9m001->num_fmts++;
Sascha Hauer36034dc2009-03-13 06:08:20 -0300657
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300658 mt9m001->fmt = &mt9m001->fmts[0];
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300659
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300660 dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300661 data == 0x8431 ? "C12STM" : "C12ST");
662
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300663 ret = mt9m001_init(client);
664 if (ret < 0)
665 dev_err(&client->dev, "Failed to initialise the camera\n");
666
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300667 /* mt9m001_init() has reset the chip, returning registers to defaults */
668 mt9m001->gain = 64;
669 mt9m001->exposure = 255;
670
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300671 return ret;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300672}
673
674static void mt9m001_video_remove(struct soc_camera_device *icd)
675{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300676 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300677
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300678 dev_dbg(&icd->dev, "Video removed: %p, %p\n",
Guennadi Liakhovetskia85bdac2008-12-18 12:54:33 -0300679 icd->dev.parent, icd->vdev);
Guennadi Liakhovetski594bb462009-04-24 12:53:51 -0300680 if (icl->free_bus)
681 icl->free_bus(icl);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300682}
683
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300684static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
685{
686 struct i2c_client *client = sd->priv;
687 struct mt9m001 *mt9m001 = to_mt9m001(client);
688
689 *lines = mt9m001->y_skip_top;
690
691 return 0;
692}
693
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300694static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
695 .g_ctrl = mt9m001_g_ctrl,
696 .s_ctrl = mt9m001_s_ctrl,
697 .g_chip_ident = mt9m001_g_chip_ident,
698#ifdef CONFIG_VIDEO_ADV_DEBUG
699 .g_register = mt9m001_g_register,
700 .s_register = mt9m001_s_register,
701#endif
702};
703
Hans Verkuil3805f202010-05-08 17:55:00 -0300704static int mt9m001_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300705 enum v4l2_mbus_pixelcode *code)
706{
707 struct i2c_client *client = sd->priv;
708 struct mt9m001 *mt9m001 = to_mt9m001(client);
709
Hans Verkuil3805f202010-05-08 17:55:00 -0300710 if (index >= mt9m001->num_fmts)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300711 return -EINVAL;
712
713 *code = mt9m001->fmts[index].code;
714 return 0;
715}
716
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300717static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
718 .s_stream = mt9m001_s_stream,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300719 .s_mbus_fmt = mt9m001_s_fmt,
720 .g_mbus_fmt = mt9m001_g_fmt,
721 .try_mbus_fmt = mt9m001_try_fmt,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300722 .s_crop = mt9m001_s_crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300723 .g_crop = mt9m001_g_crop,
724 .cropcap = mt9m001_cropcap,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300725 .enum_mbus_fmt = mt9m001_enum_fmt,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300726};
727
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300728static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
729 .g_skip_top_lines = mt9m001_g_skip_top_lines,
730};
731
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300732static struct v4l2_subdev_ops mt9m001_subdev_ops = {
733 .core = &mt9m001_subdev_core_ops,
734 .video = &mt9m001_subdev_video_ops,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300735 .sensor = &mt9m001_subdev_sensor_ops,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300736};
737
Jean Delvared2653e92008-04-29 23:11:39 +0200738static int mt9m001_probe(struct i2c_client *client,
739 const struct i2c_device_id *did)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300740{
741 struct mt9m001 *mt9m001;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300742 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300743 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300744 struct soc_camera_link *icl;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300745 int ret;
746
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300747 if (!icd) {
748 dev_err(&client->dev, "MT9M001: missing soc-camera data!\n");
749 return -EINVAL;
750 }
751
752 icl = to_soc_camera_link(icd);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300753 if (!icl) {
754 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
755 return -EINVAL;
756 }
757
758 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
759 dev_warn(&adapter->dev,
760 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
761 return -EIO;
762 }
763
764 mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
765 if (!mt9m001)
766 return -ENOMEM;
767
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300768 v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300769
770 /* Second stage probe - when a capture adapter is there */
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300771 icd->ops = &mt9m001_ops;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300772
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300773 mt9m001->y_skip_top = 0;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300774 mt9m001->rect.left = MT9M001_COLUMN_SKIP;
775 mt9m001->rect.top = MT9M001_ROW_SKIP;
776 mt9m001->rect.width = MT9M001_MAX_WIDTH;
777 mt9m001->rect.height = MT9M001_MAX_HEIGHT;
778
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300779 /*
780 * Simulated autoexposure. If enabled, we calculate shutter width
781 * ourselves in the driver based on vertical blanking and frame width
782 */
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300783 mt9m001->autoexposure = 1;
784
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300785 ret = mt9m001_video_probe(icd, client);
786 if (ret) {
787 icd->ops = NULL;
788 i2c_set_clientdata(client, NULL);
789 kfree(mt9m001);
790 }
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300791
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300792 return ret;
793}
794
795static int mt9m001_remove(struct i2c_client *client)
796{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300797 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300798 struct soc_camera_device *icd = client->dev.platform_data;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300799
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300800 icd->ops = NULL;
801 mt9m001_video_remove(icd);
802 i2c_set_clientdata(client, NULL);
803 client->driver = NULL;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300804 kfree(mt9m001);
805
806 return 0;
807}
808
Jean Delvare3760f732008-04-29 23:11:40 +0200809static const struct i2c_device_id mt9m001_id[] = {
810 { "mt9m001", 0 },
811 { }
812};
813MODULE_DEVICE_TABLE(i2c, mt9m001_id);
814
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300815static struct i2c_driver mt9m001_i2c_driver = {
816 .driver = {
817 .name = "mt9m001",
818 },
819 .probe = mt9m001_probe,
820 .remove = mt9m001_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200821 .id_table = mt9m001_id,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300822};
823
824static int __init mt9m001_mod_init(void)
825{
826 return i2c_add_driver(&mt9m001_i2c_driver);
827}
828
829static void __exit mt9m001_mod_exit(void)
830{
831 i2c_del_driver(&mt9m001_i2c_driver);
832}
833
834module_init(mt9m001_mod_init);
835module_exit(mt9m001_mod_exit);
836
837MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
838MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
839MODULE_LICENSE("GPL");