blob: 19f8a07764f97cf3b5e56b3c083c463b31cb99ed [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>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040015#include <linux/module.h>
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030016
Guennadi Liakhovetski32ca2082011-07-26 11:46:02 -030017#include <media/soc_camera.h>
18#include <media/soc_mediabus.h>
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030019#include <media/v4l2-subdev.h>
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030020#include <media/v4l2-chip-ident.h>
Hans Verkuil2dd7d292011-09-07 06:04:30 -030021#include <media/v4l2-ctrls.h>
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030022
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030023/*
24 * mt9m001 i2c address 0x5d
Peter Meerwald22cf83f2012-06-13 10:48:11 -030025 * The platform has to define struct i2c_board_info objects and link to them
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030026 * from struct soc_camera_link
27 */
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030028
29/* mt9m001 selected register addresses */
30#define MT9M001_CHIP_VERSION 0x00
31#define MT9M001_ROW_START 0x01
32#define MT9M001_COLUMN_START 0x02
33#define MT9M001_WINDOW_HEIGHT 0x03
34#define MT9M001_WINDOW_WIDTH 0x04
35#define MT9M001_HORIZONTAL_BLANKING 0x05
36#define MT9M001_VERTICAL_BLANKING 0x06
37#define MT9M001_OUTPUT_CONTROL 0x07
38#define MT9M001_SHUTTER_WIDTH 0x09
39#define MT9M001_FRAME_RESTART 0x0b
40#define MT9M001_SHUTTER_DELAY 0x0c
41#define MT9M001_RESET 0x0d
42#define MT9M001_READ_OPTIONS1 0x1e
43#define MT9M001_READ_OPTIONS2 0x20
44#define MT9M001_GLOBAL_GAIN 0x35
45#define MT9M001_CHIP_ENABLE 0xF1
46
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030047#define MT9M001_MAX_WIDTH 1280
48#define MT9M001_MAX_HEIGHT 1024
49#define MT9M001_MIN_WIDTH 48
50#define MT9M001_MIN_HEIGHT 32
51#define MT9M001_COLUMN_SKIP 20
52#define MT9M001_ROW_SKIP 12
53
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030054/* MT9M001 has only one fixed colorspace per pixelcode */
55struct mt9m001_datafmt {
56 enum v4l2_mbus_pixelcode code;
57 enum v4l2_colorspace colorspace;
58};
59
60/* Find a data format by a pixel code in an array */
61static const struct mt9m001_datafmt *mt9m001_find_datafmt(
62 enum v4l2_mbus_pixelcode code, const struct mt9m001_datafmt *fmt,
63 int n)
64{
65 int i;
66 for (i = 0; i < n; i++)
67 if (fmt[i].code == code)
68 return fmt + i;
69
70 return NULL;
71}
72
73static const struct mt9m001_datafmt mt9m001_colour_fmts[] = {
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -030074 /*
75 * Order important: first natively supported,
76 * second supported with a GPIO extender
77 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030078 {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
79 {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030080};
81
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030082static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030083 /* Order important - see above */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030084 {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
Laurent Pinchart076704332010-09-28 07:01:44 -030085 {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -030086};
87
88struct mt9m001 {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030089 struct v4l2_subdev subdev;
Hans Verkuil2dd7d292011-09-07 06:04:30 -030090 struct v4l2_ctrl_handler hdl;
91 struct {
92 /* exposure/auto-exposure cluster */
93 struct v4l2_ctrl *autoexposure;
94 struct v4l2_ctrl *exposure;
95 };
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -030096 struct v4l2_rect rect; /* Sensor window */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030097 const struct mt9m001_datafmt *fmt;
98 const struct mt9m001_datafmt *fmts;
99 int num_fmts;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300100 int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300101 unsigned int total_h;
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300102 unsigned short y_skip_top; /* Lines to skip at the top */
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300103};
104
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300105static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
106{
107 return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
108}
109
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300110static int reg_read(struct i2c_client *client, const u8 reg)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300111{
Jonathan Cameron3f877042011-10-21 09:30:25 -0300112 return i2c_smbus_read_word_swapped(client, reg);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300113}
114
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300115static int reg_write(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300116 const u16 data)
117{
Jonathan Cameron3f877042011-10-21 09:30:25 -0300118 return i2c_smbus_write_word_swapped(client, reg, data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300119}
120
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300121static int reg_set(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300122 const u16 data)
123{
124 int ret;
125
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300126 ret = reg_read(client, reg);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300127 if (ret < 0)
128 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300129 return reg_write(client, reg, ret | data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300130}
131
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300132static int reg_clear(struct i2c_client *client, const u8 reg,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300133 const u16 data)
134{
135 int ret;
136
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300137 ret = reg_read(client, reg);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300138 if (ret < 0)
139 return ret;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300140 return reg_write(client, reg, ret & ~data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300141}
142
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300143static int mt9m001_init(struct i2c_client *client)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300144{
145 int ret;
146
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300147 dev_dbg(&client->dev, "%s\n", __func__);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300148
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300149 /*
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300150 * We don't know, whether platform provides reset, issue a soft reset
151 * too. This returns all registers to their default values.
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300152 */
153 ret = reg_write(client, MT9M001_RESET, 1);
154 if (!ret)
155 ret = reg_write(client, MT9M001_RESET, 0);
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300156
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300157 /* Disable chip, synchronous option update */
158 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300159 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300160
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300161 return ret;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300162}
163
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300164static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300165{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300166 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300167
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300168 /* Switch to master "normal" mode or stop sensor readout */
169 if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300170 return -EIO;
171 return 0;
172}
173
Hans Verkuil4f996592012-09-05 05:10:48 -0300174static int mt9m001_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300175{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300176 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300177 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300178 struct v4l2_rect rect = a->c;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300179 int ret;
180 const u16 hblank = 9, vblank = 25;
181
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300182 if (mt9m001->fmts == mt9m001_colour_fmts)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300183 /*
184 * Bayer format - even number of rows for simplicity,
185 * but let the user play with the top row.
186 */
187 rect.height = ALIGN(rect.height, 2);
188
189 /* Datasheet requirement: see register description */
190 rect.width = ALIGN(rect.width, 2);
191 rect.left = ALIGN(rect.left, 2);
192
193 soc_camera_limit_side(&rect.left, &rect.width,
194 MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH);
195
196 soc_camera_limit_side(&rect.top, &rect.height,
197 MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT);
198
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300199 mt9m001->total_h = rect.height + mt9m001->y_skip_top + vblank;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300200
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300201 /* Blanking and start values - default... */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300202 ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300203 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300204 ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300205
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300206 /*
207 * The caller provides a supported format, as verified per
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300208 * call to .try_mbus_fmt()
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300209 */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300210 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300211 ret = reg_write(client, MT9M001_COLUMN_START, rect.left);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300212 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300213 ret = reg_write(client, MT9M001_ROW_START, rect.top);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300214 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300215 ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300216 if (!ret)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300217 ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300218 rect.height + mt9m001->y_skip_top - 1);
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300219 if (!ret && v4l2_ctrl_g_ctrl(mt9m001->autoexposure) == V4L2_EXPOSURE_AUTO)
220 ret = reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300221
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300222 if (!ret)
223 mt9m001->rect = rect;
224
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300225 return ret;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300226}
227
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300228static int mt9m001_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
229{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300230 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300231 struct mt9m001 *mt9m001 = to_mt9m001(client);
232
233 a->c = mt9m001->rect;
234 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
235
236 return 0;
237}
238
239static int mt9m001_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
240{
241 a->bounds.left = MT9M001_COLUMN_SKIP;
242 a->bounds.top = MT9M001_ROW_SKIP;
243 a->bounds.width = MT9M001_MAX_WIDTH;
244 a->bounds.height = MT9M001_MAX_HEIGHT;
245 a->defrect = a->bounds;
246 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
247 a->pixelaspect.numerator = 1;
248 a->pixelaspect.denominator = 1;
249
250 return 0;
251}
252
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300253static int mt9m001_g_fmt(struct v4l2_subdev *sd,
254 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300255{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300256 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300257 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300258
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300259 mf->width = mt9m001->rect.width;
260 mf->height = mt9m001->rect.height;
261 mf->code = mt9m001->fmt->code;
262 mf->colorspace = mt9m001->fmt->colorspace;
263 mf->field = V4L2_FIELD_NONE;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300264
265 return 0;
266}
267
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300268static int mt9m001_s_fmt(struct v4l2_subdev *sd,
269 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300270{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300271 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300272 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300273 struct v4l2_crop a = {
274 .c = {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300275 .left = mt9m001->rect.left,
276 .top = mt9m001->rect.top,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300277 .width = mf->width,
278 .height = mf->height,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300279 },
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300280 };
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300281 int ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300282
283 /* No support for scaling so far, just crop. TODO: use skipping */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300284 ret = mt9m001_s_crop(sd, &a);
285 if (!ret) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300286 mf->width = mt9m001->rect.width;
287 mf->height = mt9m001->rect.height;
288 mt9m001->fmt = mt9m001_find_datafmt(mf->code,
289 mt9m001->fmts, mt9m001->num_fmts);
290 mf->colorspace = mt9m001->fmt->colorspace;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300291 }
292
293 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300294}
295
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300296static int mt9m001_try_fmt(struct v4l2_subdev *sd,
297 struct v4l2_mbus_framefmt *mf)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300298{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300299 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300300 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300301 const struct mt9m001_datafmt *fmt;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300302
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300303 v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300304 MT9M001_MAX_WIDTH, 1,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300305 &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300306 MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300307
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300308 if (mt9m001->fmts == mt9m001_colour_fmts)
309 mf->height = ALIGN(mf->height - 1, 2);
310
311 fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts,
312 mt9m001->num_fmts);
313 if (!fmt) {
314 fmt = mt9m001->fmt;
315 mf->code = fmt->code;
316 }
317
318 mf->colorspace = fmt->colorspace;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300319
320 return 0;
321}
322
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300323static int mt9m001_g_chip_ident(struct v4l2_subdev *sd,
324 struct v4l2_dbg_chip_ident *id)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300325{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300326 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300327 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300328
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300329 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300330 return -EINVAL;
331
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300332 if (id->match.addr != client->addr)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300333 return -ENODEV;
334
335 id->ident = mt9m001->model;
336 id->revision = 0;
337
338 return 0;
339}
340
341#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300342static int mt9m001_g_register(struct v4l2_subdev *sd,
343 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300344{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300345 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300346
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300347 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300348 return -EINVAL;
349
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300350 if (reg->match.addr != client->addr)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300351 return -ENODEV;
352
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300353 reg->size = 2;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300354 reg->val = reg_read(client, reg->reg);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300355
356 if (reg->val > 0xffff)
357 return -EIO;
358
359 return 0;
360}
361
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300362static int mt9m001_s_register(struct v4l2_subdev *sd,
363 struct v4l2_dbg_register *reg)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300364{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300365 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300366
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300367 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300368 return -EINVAL;
369
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300370 if (reg->match.addr != client->addr)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300371 return -ENODEV;
372
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300373 if (reg_write(client, reg->reg, reg->val) < 0)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300374 return -EIO;
375
376 return 0;
377}
378#endif
379
Laurent Pinchart4ec10ba2012-07-20 10:19:50 -0300380static int mt9m001_s_power(struct v4l2_subdev *sd, int on)
381{
382 struct i2c_client *client = v4l2_get_subdevdata(sd);
383 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
384
385 return soc_camera_set_power(&client->dev, icl, on);
386}
387
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300388static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300389{
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300390 struct mt9m001 *mt9m001 = container_of(ctrl->handler,
391 struct mt9m001, hdl);
392 s32 min, max;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300393
394 switch (ctrl->id) {
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300395 case V4L2_CID_EXPOSURE_AUTO:
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300396 min = mt9m001->exposure->minimum;
397 max = mt9m001->exposure->maximum;
398 mt9m001->exposure->val =
399 (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300400 break;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300401 }
402 return 0;
403}
404
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300405static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300406{
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300407 struct mt9m001 *mt9m001 = container_of(ctrl->handler,
408 struct mt9m001, hdl);
409 struct v4l2_subdev *sd = &mt9m001->subdev;
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300410 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300411 struct v4l2_ctrl *exp = mt9m001->exposure;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300412 int data;
413
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300414 switch (ctrl->id) {
415 case V4L2_CID_VFLIP:
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300416 if (ctrl->val)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300417 data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300418 else
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300419 data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300420 if (data < 0)
421 return -EIO;
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300422 return 0;
423
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300424 case V4L2_CID_GAIN:
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300425 /* See Datasheet Table 7, Gain settings. */
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300426 if (ctrl->val <= ctrl->default_value) {
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300427 /* Pack it into 0..1 step 0.125, register values 0..8 */
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300428 unsigned long range = ctrl->default_value - ctrl->minimum;
429 data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300430
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300431 dev_dbg(&client->dev, "Setting gain %d\n", data);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300432 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300433 if (data < 0)
434 return -EIO;
435 } else {
436 /* Pack it into 1.125..15 variable step, register values 9..67 */
437 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300438 unsigned long range = ctrl->maximum - ctrl->default_value - 1;
439 unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300440 111 + range / 2) / range + 9;
441
442 if (gain <= 32)
443 data = gain;
444 else if (gain <= 64)
445 data = ((gain - 32) * 16 + 16) / 32 + 80;
446 else
447 data = ((gain - 64) * 7 + 28) / 56 + 96;
448
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300449 dev_dbg(&client->dev, "Setting gain from %d to %d\n",
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300450 reg_read(client, MT9M001_GLOBAL_GAIN), data);
451 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300452 if (data < 0)
453 return -EIO;
454 }
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300455 return 0;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300456
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300457 case V4L2_CID_EXPOSURE_AUTO:
458 if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
459 unsigned long range = exp->maximum - exp->minimum;
460 unsigned long shutter = ((exp->val - exp->minimum) * 1048 +
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300461 range / 2) / range + 1;
462
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300463 dev_dbg(&client->dev,
464 "Setting shutter width from %d to %lu\n",
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300465 reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300466 if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300467 return -EIO;
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300468 } else {
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300469 const u16 vblank = 25;
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300470
471 mt9m001->total_h = mt9m001->rect.height +
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300472 mt9m001->y_skip_top + vblank;
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300473 if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300474 return -EIO;
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300475 }
476 return 0;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300477 }
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300478 return -EINVAL;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300479}
480
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300481/*
482 * Interface active, can use i2c. If it fails, it can indeed mean, that
483 * this wasn't our capture interface, so, we wait for the right one
484 */
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300485static int mt9m001_video_probe(struct soc_camera_link *icl,
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300486 struct i2c_client *client)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300487{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300488 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300489 s32 data;
Sascha Hauer36034dc2009-03-13 06:08:20 -0300490 unsigned long flags;
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300491 int ret;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300492
Laurent Pinchart4bbc6d52012-07-18 10:54:04 -0300493 ret = mt9m001_s_power(&mt9m001->subdev, 1);
494 if (ret < 0)
495 return ret;
496
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300497 /* Enable the chip */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300498 data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300499 dev_dbg(&client->dev, "write: %d\n", data);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300500
501 /* Read out the chip version register */
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300502 data = reg_read(client, MT9M001_CHIP_VERSION);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300503
504 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
505 switch (data) {
506 case 0x8411:
507 case 0x8421:
508 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300509 mt9m001->fmts = mt9m001_colour_fmts;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300510 break;
511 case 0x8431:
512 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300513 mt9m001->fmts = mt9m001_monochrome_fmts;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300514 break;
515 default:
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300516 dev_err(&client->dev,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300517 "No MT9M001 chip detected, register read %x\n", data);
Laurent Pinchart4bbc6d52012-07-18 10:54:04 -0300518 ret = -ENODEV;
519 goto done;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300520 }
521
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300522 mt9m001->num_fmts = 0;
Sascha Hauer36034dc2009-03-13 06:08:20 -0300523
524 /*
525 * This is a 10bit sensor, so by default we only allow 10bit.
526 * The platform may support different bus widths due to
527 * different routing of the data lines.
528 */
529 if (icl->query_bus_param)
530 flags = icl->query_bus_param(icl);
531 else
532 flags = SOCAM_DATAWIDTH_10;
533
534 if (flags & SOCAM_DATAWIDTH_10)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300535 mt9m001->num_fmts++;
Sascha Hauer36034dc2009-03-13 06:08:20 -0300536 else
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300537 mt9m001->fmts++;
Sascha Hauer36034dc2009-03-13 06:08:20 -0300538
539 if (flags & SOCAM_DATAWIDTH_8)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300540 mt9m001->num_fmts++;
Sascha Hauer36034dc2009-03-13 06:08:20 -0300541
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300542 mt9m001->fmt = &mt9m001->fmts[0];
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300543
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300544 dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300545 data == 0x8431 ? "C12STM" : "C12ST");
546
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300547 ret = mt9m001_init(client);
Laurent Pinchart4bbc6d52012-07-18 10:54:04 -0300548 if (ret < 0) {
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300549 dev_err(&client->dev, "Failed to initialise the camera\n");
Laurent Pinchart4bbc6d52012-07-18 10:54:04 -0300550 goto done;
551 }
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300552
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300553 /* mt9m001_init() has reset the chip, returning registers to defaults */
Laurent Pinchart4bbc6d52012-07-18 10:54:04 -0300554 ret = v4l2_ctrl_handler_setup(&mt9m001->hdl);
555
556done:
557 mt9m001_s_power(&mt9m001->subdev, 0);
558 return ret;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300559}
560
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300561static void mt9m001_video_remove(struct soc_camera_link *icl)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300562{
Guennadi Liakhovetski594bb462009-04-24 12:53:51 -0300563 if (icl->free_bus)
564 icl->free_bus(icl);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300565}
566
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300567static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
568{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300569 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300570 struct mt9m001 *mt9m001 = to_mt9m001(client);
571
572 *lines = mt9m001->y_skip_top;
573
574 return 0;
575}
576
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300577static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
578 .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
579 .s_ctrl = mt9m001_s_ctrl,
580};
581
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300582static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300583 .g_chip_ident = mt9m001_g_chip_ident,
584#ifdef CONFIG_VIDEO_ADV_DEBUG
585 .g_register = mt9m001_g_register,
586 .s_register = mt9m001_s_register,
587#endif
Laurent Pinchart4ec10ba2012-07-20 10:19:50 -0300588 .s_power = mt9m001_s_power,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300589};
590
Hans Verkuil3805f202010-05-08 17:55:00 -0300591static int mt9m001_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300592 enum v4l2_mbus_pixelcode *code)
593{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300594 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300595 struct mt9m001 *mt9m001 = to_mt9m001(client);
596
Hans Verkuil3805f202010-05-08 17:55:00 -0300597 if (index >= mt9m001->num_fmts)
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300598 return -EINVAL;
599
600 *code = mt9m001->fmts[index].code;
601 return 0;
602}
603
Guennadi Liakhovetski32ca2082011-07-26 11:46:02 -0300604static int mt9m001_g_mbus_config(struct v4l2_subdev *sd,
605 struct v4l2_mbus_config *cfg)
606{
607 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300608 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
Guennadi Liakhovetski32ca2082011-07-26 11:46:02 -0300609
610 /* MT9M001 has all capture_format parameters fixed */
611 cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
612 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
613 V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER;
614 cfg->type = V4L2_MBUS_PARALLEL;
615 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
616
617 return 0;
618}
619
620static int mt9m001_s_mbus_config(struct v4l2_subdev *sd,
621 const struct v4l2_mbus_config *cfg)
622{
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300623 const struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300624 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
Guennadi Liakhovetski443f4832011-09-09 07:06:50 -0300625 struct mt9m001 *mt9m001 = to_mt9m001(client);
626 unsigned int bps = soc_mbus_get_fmtdesc(mt9m001->fmt->code)->bits_per_sample;
Guennadi Liakhovetski32ca2082011-07-26 11:46:02 -0300627
628 if (icl->set_bus_param)
629 return icl->set_bus_param(icl, 1 << (bps - 1));
630
631 /*
632 * Without board specific bus width settings we only support the
633 * sensors native bus width
634 */
635 return bps == 10 ? 0 : -EINVAL;
636}
637
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300638static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
639 .s_stream = mt9m001_s_stream,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300640 .s_mbus_fmt = mt9m001_s_fmt,
641 .g_mbus_fmt = mt9m001_g_fmt,
642 .try_mbus_fmt = mt9m001_try_fmt,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300643 .s_crop = mt9m001_s_crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300644 .g_crop = mt9m001_g_crop,
645 .cropcap = mt9m001_cropcap,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300646 .enum_mbus_fmt = mt9m001_enum_fmt,
Guennadi Liakhovetski32ca2082011-07-26 11:46:02 -0300647 .g_mbus_config = mt9m001_g_mbus_config,
648 .s_mbus_config = mt9m001_s_mbus_config,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300649};
650
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300651static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
652 .g_skip_top_lines = mt9m001_g_skip_top_lines,
653};
654
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300655static struct v4l2_subdev_ops mt9m001_subdev_ops = {
656 .core = &mt9m001_subdev_core_ops,
657 .video = &mt9m001_subdev_video_ops,
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300658 .sensor = &mt9m001_subdev_sensor_ops,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300659};
660
Jean Delvared2653e92008-04-29 23:11:39 +0200661static int mt9m001_probe(struct i2c_client *client,
662 const struct i2c_device_id *did)
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300663{
664 struct mt9m001 *mt9m001;
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300665 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300666 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300667 int ret;
668
669 if (!icl) {
670 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
671 return -EINVAL;
672 }
673
674 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
675 dev_warn(&adapter->dev,
676 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
677 return -EIO;
678 }
679
680 mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
681 if (!mt9m001)
682 return -ENOMEM;
683
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300684 v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300685 v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
686 v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
687 V4L2_CID_VFLIP, 0, 1, 1, 0);
688 v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
689 V4L2_CID_GAIN, 0, 127, 1, 64);
690 mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
691 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
692 /*
693 * Simulated autoexposure. If enabled, we calculate shutter width
694 * ourselves in the driver based on vertical blanking and frame width
695 */
696 mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
697 &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
698 V4L2_EXPOSURE_AUTO);
699 mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
700 if (mt9m001->hdl.error) {
701 int err = mt9m001->hdl.error;
702
703 kfree(mt9m001);
704 return err;
705 }
706 v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
707 V4L2_EXPOSURE_MANUAL, true);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300708
709 /* Second stage probe - when a capture adapter is there */
Guennadi Liakhovetski32536102009-12-11 11:14:46 -0300710 mt9m001->y_skip_top = 0;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300711 mt9m001->rect.left = MT9M001_COLUMN_SKIP;
712 mt9m001->rect.top = MT9M001_ROW_SKIP;
713 mt9m001->rect.width = MT9M001_MAX_WIDTH;
714 mt9m001->rect.height = MT9M001_MAX_HEIGHT;
715
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300716 ret = mt9m001_video_probe(icl, client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300717 if (ret) {
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300718 v4l2_ctrl_handler_free(&mt9m001->hdl);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300719 kfree(mt9m001);
720 }
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300721
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300722 return ret;
723}
724
725static int mt9m001_remove(struct i2c_client *client)
726{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300727 struct mt9m001 *mt9m001 = to_mt9m001(client);
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300728 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300729
Hans Verkuil2dd7d292011-09-07 06:04:30 -0300730 v4l2_device_unregister_subdev(&mt9m001->subdev);
731 v4l2_ctrl_handler_free(&mt9m001->hdl);
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300732 mt9m001_video_remove(icl);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300733 kfree(mt9m001);
734
735 return 0;
736}
737
Jean Delvare3760f732008-04-29 23:11:40 +0200738static const struct i2c_device_id mt9m001_id[] = {
739 { "mt9m001", 0 },
740 { }
741};
742MODULE_DEVICE_TABLE(i2c, mt9m001_id);
743
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300744static struct i2c_driver mt9m001_i2c_driver = {
745 .driver = {
746 .name = "mt9m001",
747 },
748 .probe = mt9m001_probe,
749 .remove = mt9m001_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200750 .id_table = mt9m001_id,
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300751};
752
Axel Linc6e8d862012-02-12 06:56:32 -0300753module_i2c_driver(mt9m001_i2c_driver);
Guennadi Liakhovetskif523dd02008-04-22 14:42:04 -0300754
755MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
756MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
757MODULE_LICENSE("GPL");