blob: 90da699601ea334d056008d4334c828dac3695a0 [file] [log] [blame]
Robert Jarzmik77110ab2008-08-14 12:02:51 -03001/*
Mike Rapoportd7f83a52008-12-18 11:44:15 -03002 * Driver for MT9M111/MT9M112 CMOS Image Sensor from Micron
Robert Jarzmik77110ab2008-08-14 12:02:51 -03003 *
4 * Copyright (C) 2008, Robert Jarzmik <robert.jarzmik@free.fr>
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#include <linux/videodev2.h>
11#include <linux/slab.h>
12#include <linux/i2c.h>
13#include <linux/log2.h>
14#include <linux/gpio.h>
15#include <linux/delay.h>
16
17#include <media/v4l2-common.h>
18#include <media/v4l2-chip-ident.h>
19#include <media/soc_camera.h>
20
21/*
Mike Rapoportd7f83a52008-12-18 11:44:15 -030022 * mt9m111 and mt9m112 i2c address is 0x5d or 0x48 (depending on SAddr pin)
Robert Jarzmik77110ab2008-08-14 12:02:51 -030023 * The platform has to define i2c_board_info and call i2c_register_board_info()
24 */
25
26/* mt9m111: Sensor register addresses */
27#define MT9M111_CHIP_VERSION 0x000
28#define MT9M111_ROW_START 0x001
29#define MT9M111_COLUMN_START 0x002
30#define MT9M111_WINDOW_HEIGHT 0x003
31#define MT9M111_WINDOW_WIDTH 0x004
32#define MT9M111_HORIZONTAL_BLANKING_B 0x005
33#define MT9M111_VERTICAL_BLANKING_B 0x006
34#define MT9M111_HORIZONTAL_BLANKING_A 0x007
35#define MT9M111_VERTICAL_BLANKING_A 0x008
36#define MT9M111_SHUTTER_WIDTH 0x009
37#define MT9M111_ROW_SPEED 0x00a
38#define MT9M111_EXTRA_DELAY 0x00b
39#define MT9M111_SHUTTER_DELAY 0x00c
40#define MT9M111_RESET 0x00d
41#define MT9M111_READ_MODE_B 0x020
42#define MT9M111_READ_MODE_A 0x021
43#define MT9M111_FLASH_CONTROL 0x023
44#define MT9M111_GREEN1_GAIN 0x02b
45#define MT9M111_BLUE_GAIN 0x02c
46#define MT9M111_RED_GAIN 0x02d
47#define MT9M111_GREEN2_GAIN 0x02e
48#define MT9M111_GLOBAL_GAIN 0x02f
49#define MT9M111_CONTEXT_CONTROL 0x0c8
50#define MT9M111_PAGE_MAP 0x0f0
51#define MT9M111_BYTE_WISE_ADDR 0x0f1
52
53#define MT9M111_RESET_SYNC_CHANGES (1 << 15)
54#define MT9M111_RESET_RESTART_BAD_FRAME (1 << 9)
55#define MT9M111_RESET_SHOW_BAD_FRAMES (1 << 8)
56#define MT9M111_RESET_RESET_SOC (1 << 5)
57#define MT9M111_RESET_OUTPUT_DISABLE (1 << 4)
58#define MT9M111_RESET_CHIP_ENABLE (1 << 3)
59#define MT9M111_RESET_ANALOG_STANDBY (1 << 2)
60#define MT9M111_RESET_RESTART_FRAME (1 << 1)
61#define MT9M111_RESET_RESET_MODE (1 << 0)
62
63#define MT9M111_RMB_MIRROR_COLS (1 << 1)
64#define MT9M111_RMB_MIRROR_ROWS (1 << 0)
65#define MT9M111_CTXT_CTRL_RESTART (1 << 15)
66#define MT9M111_CTXT_CTRL_DEFECTCOR_B (1 << 12)
67#define MT9M111_CTXT_CTRL_RESIZE_B (1 << 10)
68#define MT9M111_CTXT_CTRL_CTRL2_B (1 << 9)
69#define MT9M111_CTXT_CTRL_GAMMA_B (1 << 8)
70#define MT9M111_CTXT_CTRL_XENON_EN (1 << 7)
71#define MT9M111_CTXT_CTRL_READ_MODE_B (1 << 3)
72#define MT9M111_CTXT_CTRL_LED_FLASH_EN (1 << 2)
73#define MT9M111_CTXT_CTRL_VBLANK_SEL_B (1 << 1)
74#define MT9M111_CTXT_CTRL_HBLANK_SEL_B (1 << 0)
75/*
76 * mt9m111: Colorpipe register addresses (0x100..0x1ff)
77 */
78#define MT9M111_OPER_MODE_CTRL 0x106
79#define MT9M111_OUTPUT_FORMAT_CTRL 0x108
80#define MT9M111_REDUCER_XZOOM_B 0x1a0
81#define MT9M111_REDUCER_XSIZE_B 0x1a1
82#define MT9M111_REDUCER_YZOOM_B 0x1a3
83#define MT9M111_REDUCER_YSIZE_B 0x1a4
84#define MT9M111_REDUCER_XZOOM_A 0x1a6
85#define MT9M111_REDUCER_XSIZE_A 0x1a7
86#define MT9M111_REDUCER_YZOOM_A 0x1a9
87#define MT9M111_REDUCER_YSIZE_A 0x1aa
88
89#define MT9M111_OUTPUT_FORMAT_CTRL2_A 0x13a
90#define MT9M111_OUTPUT_FORMAT_CTRL2_B 0x19b
91
92#define MT9M111_OPMODE_AUTOEXPO_EN (1 << 14)
Robert Jarzmik39bf3722008-12-18 11:29:05 -030093#define MT9M111_OPMODE_AUTOWHITEBAL_EN (1 << 1)
Robert Jarzmik77110ab2008-08-14 12:02:51 -030094
95#define MT9M111_OUTFMT_PROCESSED_BAYER (1 << 14)
96#define MT9M111_OUTFMT_BYPASS_IFP (1 << 10)
97#define MT9M111_OUTFMT_INV_PIX_CLOCK (1 << 9)
98#define MT9M111_OUTFMT_RGB (1 << 8)
99#define MT9M111_OUTFMT_RGB565 (0x0 << 6)
100#define MT9M111_OUTFMT_RGB555 (0x1 << 6)
101#define MT9M111_OUTFMT_RGB444x (0x2 << 6)
102#define MT9M111_OUTFMT_RGBx444 (0x3 << 6)
103#define MT9M111_OUTFMT_TST_RAMP_OFF (0x0 << 4)
104#define MT9M111_OUTFMT_TST_RAMP_COL (0x1 << 4)
105#define MT9M111_OUTFMT_TST_RAMP_ROW (0x2 << 4)
106#define MT9M111_OUTFMT_TST_RAMP_FRAME (0x3 << 4)
107#define MT9M111_OUTFMT_SHIFT_3_UP (1 << 3)
108#define MT9M111_OUTFMT_AVG_CHROMA (1 << 2)
109#define MT9M111_OUTFMT_SWAP_YCbCr_C_Y (1 << 1)
110#define MT9M111_OUTFMT_SWAP_RGB_EVEN (1 << 1)
111#define MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr (1 << 0)
112/*
113 * mt9m111: Camera control register addresses (0x200..0x2ff not implemented)
114 */
115
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300116#define reg_read(reg) mt9m111_reg_read(client, MT9M111_##reg)
117#define reg_write(reg, val) mt9m111_reg_write(client, MT9M111_##reg, (val))
118#define reg_set(reg, val) mt9m111_reg_set(client, MT9M111_##reg, (val))
119#define reg_clear(reg, val) mt9m111_reg_clear(client, MT9M111_##reg, (val))
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300120
121#define MT9M111_MIN_DARK_ROWS 8
122#define MT9M111_MIN_DARK_COLS 24
123#define MT9M111_MAX_HEIGHT 1024
124#define MT9M111_MAX_WIDTH 1280
125
126#define COL_FMT(_name, _depth, _fourcc, _colorspace) \
127 { .name = _name, .depth = _depth, .fourcc = _fourcc, \
128 .colorspace = _colorspace }
129#define RGB_FMT(_name, _depth, _fourcc) \
130 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_SRGB)
Robert Jarzmik88f4b892008-12-17 14:05:31 -0300131#define JPG_FMT(_name, _depth, _fourcc) \
132 COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300133
134static const struct soc_camera_data_format mt9m111_colour_formats[] = {
Robert Jarzmik88f4b892008-12-17 14:05:31 -0300135 JPG_FMT("CbYCrY 16 bit", 16, V4L2_PIX_FMT_UYVY),
136 JPG_FMT("CrYCbY 16 bit", 16, V4L2_PIX_FMT_VYUY),
137 JPG_FMT("YCbYCr 16 bit", 16, V4L2_PIX_FMT_YUYV),
138 JPG_FMT("YCrYCb 16 bit", 16, V4L2_PIX_FMT_YVYU),
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300139 RGB_FMT("RGB 565", 16, V4L2_PIX_FMT_RGB565),
140 RGB_FMT("RGB 555", 16, V4L2_PIX_FMT_RGB555),
141 RGB_FMT("Bayer (sRGB) 10 bit", 10, V4L2_PIX_FMT_SBGGR16),
142 RGB_FMT("Bayer (sRGB) 8 bit", 8, V4L2_PIX_FMT_SBGGR8),
143};
144
145enum mt9m111_context {
146 HIGHPOWER = 0,
147 LOWPOWER,
148};
149
150struct mt9m111 {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300151 struct v4l2_subdev subdev;
Mike Rapoportd7f83a52008-12-18 11:44:15 -0300152 int model; /* V4L2_IDENT_MT9M11x* codes from v4l2-chip-ident.h */
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300153 enum mt9m111_context context;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300154 struct v4l2_rect rect;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300155 u32 pixfmt;
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300156 unsigned int gain;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300157 unsigned char autoexposure;
158 unsigned char datawidth;
159 unsigned int powered:1;
160 unsigned int hflip:1;
161 unsigned int vflip:1;
162 unsigned int swap_rgb_even_odd:1;
163 unsigned int swap_rgb_red_blue:1;
164 unsigned int swap_yuv_y_chromas:1;
165 unsigned int swap_yuv_cb_cr:1;
Robert Jarzmik39bf3722008-12-18 11:29:05 -0300166 unsigned int autowhitebalance:1;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300167};
168
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300169static struct mt9m111 *to_mt9m111(const struct i2c_client *client)
170{
171 return container_of(i2c_get_clientdata(client), struct mt9m111, subdev);
172}
173
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300174static int reg_page_map_set(struct i2c_client *client, const u16 reg)
175{
176 int ret;
177 u16 page;
178 static int lastpage = -1; /* PageMap cache value */
179
180 page = (reg >> 8);
181 if (page == lastpage)
182 return 0;
183 if (page > 2)
184 return -EINVAL;
185
186 ret = i2c_smbus_write_word_data(client, MT9M111_PAGE_MAP, swab16(page));
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300187 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300188 lastpage = page;
189 return ret;
190}
191
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300192static int mt9m111_reg_read(struct i2c_client *client, const u16 reg)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300193{
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300194 int ret;
195
196 ret = reg_page_map_set(client, reg);
197 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300198 ret = swab16(i2c_smbus_read_word_data(client, reg & 0xff));
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300199
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300200 dev_dbg(&client->dev, "read reg.%03x -> %04x\n", reg, ret);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300201 return ret;
202}
203
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300204static int mt9m111_reg_write(struct i2c_client *client, const u16 reg,
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300205 const u16 data)
206{
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300207 int ret;
208
209 ret = reg_page_map_set(client, reg);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300210 if (!ret)
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300211 ret = i2c_smbus_write_word_data(client, reg & 0xff,
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300212 swab16(data));
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300213 dev_dbg(&client->dev, "write reg.%03x = %04x -> %d\n", reg, data, ret);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300214 return ret;
215}
216
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300217static int mt9m111_reg_set(struct i2c_client *client, const u16 reg,
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300218 const u16 data)
219{
220 int ret;
221
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300222 ret = mt9m111_reg_read(client, reg);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300223 if (ret >= 0)
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300224 ret = mt9m111_reg_write(client, reg, ret | data);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300225 return ret;
226}
227
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300228static int mt9m111_reg_clear(struct i2c_client *client, const u16 reg,
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300229 const u16 data)
230{
231 int ret;
232
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300233 ret = mt9m111_reg_read(client, reg);
234 return mt9m111_reg_write(client, reg, ret & ~data);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300235}
236
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300237static int mt9m111_set_context(struct i2c_client *client,
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300238 enum mt9m111_context ctxt)
239{
240 int valB = MT9M111_CTXT_CTRL_RESTART | MT9M111_CTXT_CTRL_DEFECTCOR_B
241 | MT9M111_CTXT_CTRL_RESIZE_B | MT9M111_CTXT_CTRL_CTRL2_B
242 | MT9M111_CTXT_CTRL_GAMMA_B | MT9M111_CTXT_CTRL_READ_MODE_B
243 | MT9M111_CTXT_CTRL_VBLANK_SEL_B
244 | MT9M111_CTXT_CTRL_HBLANK_SEL_B;
245 int valA = MT9M111_CTXT_CTRL_RESTART;
246
247 if (ctxt == HIGHPOWER)
248 return reg_write(CONTEXT_CONTROL, valB);
249 else
250 return reg_write(CONTEXT_CONTROL, valA);
251}
252
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300253static int mt9m111_setup_rect(struct i2c_client *client,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300254 struct v4l2_rect *rect)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300255{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300256 struct mt9m111 *mt9m111 = to_mt9m111(client);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300257 int ret, is_raw_format;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300258 int width = rect->width;
259 int height = rect->height;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300260
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300261 if (mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR8 ||
262 mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR16)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300263 is_raw_format = 1;
264 else
265 is_raw_format = 0;
266
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300267 ret = reg_write(COLUMN_START, rect->left);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300268 if (!ret)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300269 ret = reg_write(ROW_START, rect->top);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300270
271 if (is_raw_format) {
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300272 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300273 ret = reg_write(WINDOW_WIDTH, width);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300274 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300275 ret = reg_write(WINDOW_HEIGHT, height);
276 } else {
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300277 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300278 ret = reg_write(REDUCER_XZOOM_B, MT9M111_MAX_WIDTH);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300279 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300280 ret = reg_write(REDUCER_YZOOM_B, MT9M111_MAX_HEIGHT);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300281 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300282 ret = reg_write(REDUCER_XSIZE_B, width);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300283 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300284 ret = reg_write(REDUCER_YSIZE_B, height);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300285 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300286 ret = reg_write(REDUCER_XZOOM_A, MT9M111_MAX_WIDTH);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300287 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300288 ret = reg_write(REDUCER_YZOOM_A, MT9M111_MAX_HEIGHT);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300289 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300290 ret = reg_write(REDUCER_XSIZE_A, width);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300291 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300292 ret = reg_write(REDUCER_YSIZE_A, height);
293 }
294
295 return ret;
296}
297
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300298static int mt9m111_setup_pixfmt(struct i2c_client *client, u16 outfmt)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300299{
300 int ret;
301
302 ret = reg_write(OUTPUT_FORMAT_CTRL2_A, outfmt);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300303 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300304 ret = reg_write(OUTPUT_FORMAT_CTRL2_B, outfmt);
305 return ret;
306}
307
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300308static int mt9m111_setfmt_bayer8(struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300309{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300310 return mt9m111_setup_pixfmt(client, MT9M111_OUTFMT_PROCESSED_BAYER);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300311}
312
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300313static int mt9m111_setfmt_bayer10(struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300314{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300315 return mt9m111_setup_pixfmt(client, MT9M111_OUTFMT_BYPASS_IFP);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300316}
317
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300318static int mt9m111_setfmt_rgb565(struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300319{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300320 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300321 int val = 0;
322
323 if (mt9m111->swap_rgb_red_blue)
324 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
325 if (mt9m111->swap_rgb_even_odd)
326 val |= MT9M111_OUTFMT_SWAP_RGB_EVEN;
327 val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565;
328
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300329 return mt9m111_setup_pixfmt(client, val);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300330}
331
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300332static int mt9m111_setfmt_rgb555(struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300333{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300334 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300335 int val = 0;
336
337 if (mt9m111->swap_rgb_red_blue)
338 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
339 if (mt9m111->swap_rgb_even_odd)
340 val |= MT9M111_OUTFMT_SWAP_RGB_EVEN;
341 val |= MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB555;
342
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300343 return mt9m111_setup_pixfmt(client, val);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300344}
345
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300346static int mt9m111_setfmt_yuv(struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300347{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300348 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300349 int val = 0;
350
351 if (mt9m111->swap_yuv_cb_cr)
352 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
353 if (mt9m111->swap_yuv_y_chromas)
354 val |= MT9M111_OUTFMT_SWAP_YCbCr_C_Y;
355
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300356 return mt9m111_setup_pixfmt(client, val);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300357}
358
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300359static int mt9m111_enable(struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300360{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300361 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300362 int ret;
363
364 ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300365 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300366 mt9m111->powered = 1;
367 return ret;
368}
369
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300370static int mt9m111_reset(struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300371{
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300372 int ret;
373
374 ret = reg_set(RESET, MT9M111_RESET_RESET_MODE);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300375 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300376 ret = reg_set(RESET, MT9M111_RESET_RESET_SOC);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300377 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300378 ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE
379 | MT9M111_RESET_RESET_SOC);
Antonio Ospiteafb13682009-02-23 12:13:24 -0300380
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300381 return ret;
382}
383
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300384static unsigned long mt9m111_query_bus_param(struct soc_camera_device *icd)
385{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300386 struct soc_camera_link *icl = to_soc_camera_link(icd);
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300387 unsigned long flags = SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300388 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
Guennadi Liakhovetski2d9329f2009-02-23 12:12:58 -0300389 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8;
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300390
391 return soc_camera_apply_sensor_flags(icl, flags);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300392}
393
394static int mt9m111_set_bus_param(struct soc_camera_device *icd, unsigned long f)
395{
396 return 0;
397}
398
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300399static int mt9m111_make_rect(struct i2c_client *client,
400 struct v4l2_rect *rect)
401{
402 struct mt9m111 *mt9m111 = to_mt9m111(client);
403
404 if (mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR8 ||
405 mt9m111->pixfmt == V4L2_PIX_FMT_SBGGR16) {
406 /* Bayer format - even size lengths */
407 rect->width = ALIGN(rect->width, 2);
408 rect->height = ALIGN(rect->height, 2);
409 /* Let the user play with the starting pixel */
410 }
411
412 /* FIXME: the datasheet doesn't specify minimum sizes */
413 soc_camera_limit_side(&rect->left, &rect->width,
414 MT9M111_MIN_DARK_COLS, 2, MT9M111_MAX_WIDTH);
415
416 soc_camera_limit_side(&rect->top, &rect->height,
417 MT9M111_MIN_DARK_ROWS, 2, MT9M111_MAX_HEIGHT);
418
419 return mt9m111_setup_rect(client, rect);
420}
421
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300422static int mt9m111_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300423{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300424 struct v4l2_rect rect = a->c;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300425 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300426 struct mt9m111 *mt9m111 = to_mt9m111(client);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300427 int ret;
428
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300429 dev_dbg(&client->dev, "%s left=%d, top=%d, width=%d, height=%d\n",
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300430 __func__, rect.left, rect.top, rect.width, rect.height);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300431
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300432 ret = mt9m111_make_rect(client, &rect);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300433 if (!ret)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300434 mt9m111->rect = rect;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300435 return ret;
436}
437
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300438static int mt9m111_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
439{
440 struct i2c_client *client = sd->priv;
441 struct mt9m111 *mt9m111 = to_mt9m111(client);
442
443 a->c = mt9m111->rect;
444 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
445
446 return 0;
447}
448
449static int mt9m111_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
450{
451 a->bounds.left = MT9M111_MIN_DARK_COLS;
452 a->bounds.top = MT9M111_MIN_DARK_ROWS;
453 a->bounds.width = MT9M111_MAX_WIDTH;
454 a->bounds.height = MT9M111_MAX_HEIGHT;
455 a->defrect = a->bounds;
456 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
457 a->pixelaspect.numerator = 1;
458 a->pixelaspect.denominator = 1;
459
460 return 0;
461}
462
463static int mt9m111_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
464{
465 struct i2c_client *client = sd->priv;
466 struct mt9m111 *mt9m111 = to_mt9m111(client);
467 struct v4l2_pix_format *pix = &f->fmt.pix;
468
469 pix->width = mt9m111->rect.width;
470 pix->height = mt9m111->rect.height;
471 pix->pixelformat = mt9m111->pixfmt;
472 pix->field = V4L2_FIELD_NONE;
473 pix->colorspace = V4L2_COLORSPACE_SRGB;
474
475 return 0;
476}
477
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300478static int mt9m111_set_pixfmt(struct i2c_client *client, u32 pixfmt)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300479{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300480 struct mt9m111 *mt9m111 = to_mt9m111(client);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300481 int ret;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300482
483 switch (pixfmt) {
484 case V4L2_PIX_FMT_SBGGR8:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300485 ret = mt9m111_setfmt_bayer8(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300486 break;
487 case V4L2_PIX_FMT_SBGGR16:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300488 ret = mt9m111_setfmt_bayer10(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300489 break;
490 case V4L2_PIX_FMT_RGB555:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300491 ret = mt9m111_setfmt_rgb555(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300492 break;
493 case V4L2_PIX_FMT_RGB565:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300494 ret = mt9m111_setfmt_rgb565(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300495 break;
Robert Jarzmik88f4b892008-12-17 14:05:31 -0300496 case V4L2_PIX_FMT_UYVY:
497 mt9m111->swap_yuv_y_chromas = 0;
498 mt9m111->swap_yuv_cb_cr = 0;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300499 ret = mt9m111_setfmt_yuv(client);
Robert Jarzmik88f4b892008-12-17 14:05:31 -0300500 break;
501 case V4L2_PIX_FMT_VYUY:
502 mt9m111->swap_yuv_y_chromas = 0;
503 mt9m111->swap_yuv_cb_cr = 1;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300504 ret = mt9m111_setfmt_yuv(client);
Robert Jarzmik88f4b892008-12-17 14:05:31 -0300505 break;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300506 case V4L2_PIX_FMT_YUYV:
Robert Jarzmik88f4b892008-12-17 14:05:31 -0300507 mt9m111->swap_yuv_y_chromas = 1;
508 mt9m111->swap_yuv_cb_cr = 0;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300509 ret = mt9m111_setfmt_yuv(client);
Robert Jarzmik88f4b892008-12-17 14:05:31 -0300510 break;
511 case V4L2_PIX_FMT_YVYU:
512 mt9m111->swap_yuv_y_chromas = 1;
513 mt9m111->swap_yuv_cb_cr = 1;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300514 ret = mt9m111_setfmt_yuv(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300515 break;
516 default:
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300517 dev_err(&client->dev, "Pixel format not handled : %x\n",
518 pixfmt);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300519 ret = -EINVAL;
520 }
521
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300522 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300523 mt9m111->pixfmt = pixfmt;
524
525 return ret;
526}
527
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300528static int mt9m111_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300529{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300530 struct i2c_client *client = sd->priv;
531 struct mt9m111 *mt9m111 = to_mt9m111(client);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300532 struct v4l2_pix_format *pix = &f->fmt.pix;
533 struct v4l2_rect rect = {
534 .left = mt9m111->rect.left,
535 .top = mt9m111->rect.top,
536 .width = pix->width,
537 .height = pix->height,
538 };
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300539 int ret;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300540
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300541 dev_dbg(&client->dev,
542 "%s fmt=%x left=%d, top=%d, width=%d, height=%d\n", __func__,
543 pix->pixelformat, rect.left, rect.top, rect.width, rect.height);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300544
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300545 ret = mt9m111_make_rect(client, &rect);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300546 if (!ret)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300547 ret = mt9m111_set_pixfmt(client, pix->pixelformat);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300548 if (!ret)
549 mt9m111->rect = rect;
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300550 return ret;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300551}
552
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300553static int mt9m111_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300554{
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300555 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300556 bool bayer = pix->pixelformat == V4L2_PIX_FMT_SBGGR8 ||
557 pix->pixelformat == V4L2_PIX_FMT_SBGGR16;
558
559 /*
560 * With Bayer format enforce even side lengths, but let the user play
561 * with the starting pixel
562 */
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300563
564 if (pix->height > MT9M111_MAX_HEIGHT)
565 pix->height = MT9M111_MAX_HEIGHT;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300566 else if (pix->height < 2)
567 pix->height = 2;
568 else if (bayer)
569 pix->height = ALIGN(pix->height, 2);
570
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300571 if (pix->width > MT9M111_MAX_WIDTH)
572 pix->width = MT9M111_MAX_WIDTH;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300573 else if (pix->width < 2)
574 pix->width = 2;
575 else if (bayer)
576 pix->width = ALIGN(pix->width, 2);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300577
578 return 0;
579}
580
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300581static int mt9m111_g_chip_ident(struct v4l2_subdev *sd,
582 struct v4l2_dbg_chip_ident *id)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300583{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300584 struct i2c_client *client = sd->priv;
585 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300586
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300587 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300588 return -EINVAL;
589
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300590 if (id->match.addr != client->addr)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300591 return -ENODEV;
592
593 id->ident = mt9m111->model;
594 id->revision = 0;
595
596 return 0;
597}
598
599#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300600static int mt9m111_g_register(struct v4l2_subdev *sd,
601 struct v4l2_dbg_register *reg)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300602{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300603 struct i2c_client *client = sd->priv;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300604 int val;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300605
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300606 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300607 return -EINVAL;
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300608 if (reg->match.addr != client->addr)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300609 return -ENODEV;
610
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300611 val = mt9m111_reg_read(client, reg->reg);
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300612 reg->size = 2;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300613 reg->val = (u64)val;
614
615 if (reg->val > 0xffff)
616 return -EIO;
617
618 return 0;
619}
620
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300621static int mt9m111_s_register(struct v4l2_subdev *sd,
622 struct v4l2_dbg_register *reg)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300623{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300624 struct i2c_client *client = sd->priv;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300625
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300626 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300627 return -EINVAL;
628
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300629 if (reg->match.addr != client->addr)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300630 return -ENODEV;
631
Guennadi Liakhovetski9538e1c2009-04-24 12:57:01 -0300632 if (mt9m111_reg_write(client, reg->reg, reg->val) < 0)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300633 return -EIO;
634
635 return 0;
636}
637#endif
638
639static const struct v4l2_queryctrl mt9m111_controls[] = {
640 {
641 .id = V4L2_CID_VFLIP,
642 .type = V4L2_CTRL_TYPE_BOOLEAN,
643 .name = "Flip Verticaly",
644 .minimum = 0,
645 .maximum = 1,
646 .step = 1,
647 .default_value = 0,
648 }, {
649 .id = V4L2_CID_HFLIP,
650 .type = V4L2_CTRL_TYPE_BOOLEAN,
651 .name = "Flip Horizontaly",
652 .minimum = 0,
653 .maximum = 1,
654 .step = 1,
655 .default_value = 0,
656 }, { /* gain = 1/32*val (=>gain=1 if val==32) */
657 .id = V4L2_CID_GAIN,
658 .type = V4L2_CTRL_TYPE_INTEGER,
659 .name = "Gain",
660 .minimum = 0,
661 .maximum = 63 * 2 * 2,
662 .step = 1,
663 .default_value = 32,
664 .flags = V4L2_CTRL_FLAG_SLIDER,
665 }, {
666 .id = V4L2_CID_EXPOSURE_AUTO,
667 .type = V4L2_CTRL_TYPE_BOOLEAN,
668 .name = "Auto Exposure",
669 .minimum = 0,
670 .maximum = 1,
671 .step = 1,
672 .default_value = 1,
673 }
674};
675
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300676static int mt9m111_resume(struct soc_camera_device *icd);
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300677static int mt9m111_suspend(struct soc_camera_device *icd, pm_message_t state);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300678
679static struct soc_camera_ops mt9m111_ops = {
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300680 .suspend = mt9m111_suspend,
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300681 .resume = mt9m111_resume,
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300682 .query_bus_param = mt9m111_query_bus_param,
683 .set_bus_param = mt9m111_set_bus_param,
684 .controls = mt9m111_controls,
685 .num_controls = ARRAY_SIZE(mt9m111_controls),
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300686};
687
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300688static int mt9m111_set_flip(struct i2c_client *client, int flip, int mask)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300689{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300690 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300691 int ret;
692
693 if (mt9m111->context == HIGHPOWER) {
694 if (flip)
695 ret = reg_set(READ_MODE_B, mask);
696 else
697 ret = reg_clear(READ_MODE_B, mask);
698 } else {
699 if (flip)
700 ret = reg_set(READ_MODE_A, mask);
701 else
702 ret = reg_clear(READ_MODE_A, mask);
703 }
704
705 return ret;
706}
707
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300708static int mt9m111_get_global_gain(struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300709{
roel kluin0f28b792008-12-17 14:01:07 -0300710 int data;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300711
712 data = reg_read(GLOBAL_GAIN);
713 if (data >= 0)
roel kluin0f28b792008-12-17 14:01:07 -0300714 return (data & 0x2f) * (1 << ((data >> 10) & 1)) *
715 (1 << ((data >> 9) & 1));
716 return data;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300717}
roel kluin0f28b792008-12-17 14:01:07 -0300718
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300719static int mt9m111_set_global_gain(struct i2c_client *client, int gain)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300720{
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300721 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300722 u16 val;
723
724 if (gain > 63 * 2 * 2)
725 return -EINVAL;
726
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300727 mt9m111->gain = gain;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300728 if ((gain >= 64 * 2) && (gain < 63 * 2 * 2))
729 val = (1 << 10) | (1 << 9) | (gain / 4);
730 else if ((gain >= 64) && (gain < 64 * 2))
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300731 val = (1 << 9) | (gain / 2);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300732 else
733 val = gain;
734
735 return reg_write(GLOBAL_GAIN, val);
736}
737
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300738static int mt9m111_set_autoexposure(struct i2c_client *client, int on)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300739{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300740 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300741 int ret;
742
743 if (on)
744 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
745 else
746 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
747
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300748 if (!ret)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300749 mt9m111->autoexposure = on;
750
751 return ret;
752}
Robert Jarzmik39bf3722008-12-18 11:29:05 -0300753
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300754static int mt9m111_set_autowhitebalance(struct i2c_client *client, int on)
Robert Jarzmik39bf3722008-12-18 11:29:05 -0300755{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300756 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik39bf3722008-12-18 11:29:05 -0300757 int ret;
758
759 if (on)
760 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
761 else
762 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
763
764 if (!ret)
765 mt9m111->autowhitebalance = on;
766
767 return ret;
768}
769
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300770static int mt9m111_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300771{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300772 struct i2c_client *client = sd->priv;
773 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300774 int data;
775
776 switch (ctrl->id) {
777 case V4L2_CID_VFLIP:
778 if (mt9m111->context == HIGHPOWER)
779 data = reg_read(READ_MODE_B);
780 else
781 data = reg_read(READ_MODE_A);
782
783 if (data < 0)
784 return -EIO;
785 ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS);
786 break;
787 case V4L2_CID_HFLIP:
788 if (mt9m111->context == HIGHPOWER)
789 data = reg_read(READ_MODE_B);
790 else
791 data = reg_read(READ_MODE_A);
792
793 if (data < 0)
794 return -EIO;
795 ctrl->value = !!(data & MT9M111_RMB_MIRROR_COLS);
796 break;
797 case V4L2_CID_GAIN:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300798 data = mt9m111_get_global_gain(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300799 if (data < 0)
800 return data;
801 ctrl->value = data;
802 break;
803 case V4L2_CID_EXPOSURE_AUTO:
804 ctrl->value = mt9m111->autoexposure;
805 break;
Robert Jarzmik39bf3722008-12-18 11:29:05 -0300806 case V4L2_CID_AUTO_WHITE_BALANCE:
807 ctrl->value = mt9m111->autowhitebalance;
808 break;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300809 }
810 return 0;
811}
812
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300813static int mt9m111_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300814{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300815 struct i2c_client *client = sd->priv;
816 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300817 const struct v4l2_queryctrl *qctrl;
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300818 int ret;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300819
820 qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300821 if (!qctrl)
822 return -EINVAL;
823
824 switch (ctrl->id) {
825 case V4L2_CID_VFLIP:
826 mt9m111->vflip = ctrl->value;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300827 ret = mt9m111_set_flip(client, ctrl->value,
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300828 MT9M111_RMB_MIRROR_ROWS);
829 break;
830 case V4L2_CID_HFLIP:
831 mt9m111->hflip = ctrl->value;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300832 ret = mt9m111_set_flip(client, ctrl->value,
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300833 MT9M111_RMB_MIRROR_COLS);
834 break;
835 case V4L2_CID_GAIN:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300836 ret = mt9m111_set_global_gain(client, ctrl->value);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300837 break;
838 case V4L2_CID_EXPOSURE_AUTO:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300839 ret = mt9m111_set_autoexposure(client, ctrl->value);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300840 break;
Robert Jarzmik39bf3722008-12-18 11:29:05 -0300841 case V4L2_CID_AUTO_WHITE_BALANCE:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300842 ret = mt9m111_set_autowhitebalance(client, ctrl->value);
Robert Jarzmik39bf3722008-12-18 11:29:05 -0300843 break;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300844 default:
845 ret = -EINVAL;
846 }
847
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300848 return ret;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300849}
850
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300851static int mt9m111_suspend(struct soc_camera_device *icd, pm_message_t state)
852{
853 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
854 struct mt9m111 *mt9m111 = to_mt9m111(client);
855
856 mt9m111->gain = mt9m111_get_global_gain(client);
857
858 return 0;
859}
860
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300861static int mt9m111_restore_state(struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300862{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300863 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300864
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300865 mt9m111_set_context(client, mt9m111->context);
866 mt9m111_set_pixfmt(client, mt9m111->pixfmt);
867 mt9m111_setup_rect(client, &mt9m111->rect);
868 mt9m111_set_flip(client, mt9m111->hflip, MT9M111_RMB_MIRROR_COLS);
869 mt9m111_set_flip(client, mt9m111->vflip, MT9M111_RMB_MIRROR_ROWS);
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300870 mt9m111_set_global_gain(client, mt9m111->gain);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300871 mt9m111_set_autoexposure(client, mt9m111->autoexposure);
872 mt9m111_set_autowhitebalance(client, mt9m111->autowhitebalance);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300873 return 0;
874}
875
876static int mt9m111_resume(struct soc_camera_device *icd)
877{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300878 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300879 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300880 int ret = 0;
881
882 if (mt9m111->powered) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300883 ret = mt9m111_enable(client);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300884 if (!ret)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300885 ret = mt9m111_reset(client);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300886 if (!ret)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300887 ret = mt9m111_restore_state(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300888 }
889 return ret;
890}
891
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300892static int mt9m111_init(struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300893{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300894 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300895 int ret;
896
897 mt9m111->context = HIGHPOWER;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300898 ret = mt9m111_enable(client);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300899 if (!ret)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300900 ret = mt9m111_reset(client);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300901 if (!ret)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300902 ret = mt9m111_set_context(client, mt9m111->context);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300903 if (!ret)
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300904 ret = mt9m111_set_autoexposure(client, mt9m111->autoexposure);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300905 if (ret)
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300906 dev_err(&client->dev, "mt9m11x init failed: %d\n", ret);
Guennadi Liakhovetski506c6292008-08-14 12:03:49 -0300907 return ret;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300908}
909
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300910/*
911 * Interface active, can use i2c. If it fails, it can indeed mean, that
912 * this wasn't our capture interface, so, we wait for the right one
913 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300914static int mt9m111_video_probe(struct soc_camera_device *icd,
915 struct i2c_client *client)
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300916{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300917 struct mt9m111 *mt9m111 = to_mt9m111(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300918 s32 data;
919 int ret;
920
921 /*
922 * We must have a parent by now. And it cannot be a wrong one.
923 * So this entire test is completely redundant.
924 */
925 if (!icd->dev.parent ||
926 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
927 return -ENODEV;
928
Guennadi Liakhovetskia4c56fd2009-08-25 11:53:23 -0300929 mt9m111->autoexposure = 1;
930 mt9m111->autowhitebalance = 1;
931
932 mt9m111->swap_rgb_even_odd = 1;
933 mt9m111->swap_rgb_red_blue = 1;
934
935 ret = mt9m111_init(client);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300936 if (ret)
937 goto ei2c;
938
939 data = reg_read(CHIP_VERSION);
940
941 switch (data) {
Mike Rapoportd7f83a52008-12-18 11:44:15 -0300942 case 0x143a: /* MT9M111 */
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300943 mt9m111->model = V4L2_IDENT_MT9M111;
Mike Rapoportd7f83a52008-12-18 11:44:15 -0300944 break;
945 case 0x148c: /* MT9M112 */
946 mt9m111->model = V4L2_IDENT_MT9M112;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300947 break;
948 default:
949 ret = -ENODEV;
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300950 dev_err(&client->dev,
Mike Rapoportd7f83a52008-12-18 11:44:15 -0300951 "No MT9M11x chip detected, register read %x\n", data);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300952 goto ei2c;
953 }
954
Mike Rapoportd7f83a52008-12-18 11:44:15 -0300955 icd->formats = mt9m111_colour_formats;
956 icd->num_formats = ARRAY_SIZE(mt9m111_colour_formats);
957
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300958 dev_info(&client->dev, "Detected a MT9M11x chip ID %x\n", data);
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300959
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300960ei2c:
961 return ret;
962}
963
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300964static struct v4l2_subdev_core_ops mt9m111_subdev_core_ops = {
965 .g_ctrl = mt9m111_g_ctrl,
966 .s_ctrl = mt9m111_s_ctrl,
967 .g_chip_ident = mt9m111_g_chip_ident,
968#ifdef CONFIG_VIDEO_ADV_DEBUG
969 .g_register = mt9m111_g_register,
970 .s_register = mt9m111_s_register,
971#endif
972};
973
974static struct v4l2_subdev_video_ops mt9m111_subdev_video_ops = {
975 .s_fmt = mt9m111_s_fmt,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300976 .g_fmt = mt9m111_g_fmt,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300977 .try_fmt = mt9m111_try_fmt,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300978 .s_crop = mt9m111_s_crop,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300979 .g_crop = mt9m111_g_crop,
980 .cropcap = mt9m111_cropcap,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300981};
982
983static struct v4l2_subdev_ops mt9m111_subdev_ops = {
984 .core = &mt9m111_subdev_core_ops,
985 .video = &mt9m111_subdev_video_ops,
986};
987
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300988static int mt9m111_probe(struct i2c_client *client,
989 const struct i2c_device_id *did)
990{
991 struct mt9m111 *mt9m111;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300992 struct soc_camera_device *icd = client->dev.platform_data;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300993 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300994 struct soc_camera_link *icl;
Robert Jarzmik77110ab2008-08-14 12:02:51 -0300995 int ret;
996
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300997 if (!icd) {
998 dev_err(&client->dev, "MT9M11x: missing soc-camera data!\n");
999 return -EINVAL;
1000 }
1001
1002 icl = to_soc_camera_link(icd);
Robert Jarzmik77110ab2008-08-14 12:02:51 -03001003 if (!icl) {
Mike Rapoportd7f83a52008-12-18 11:44:15 -03001004 dev_err(&client->dev, "MT9M11x driver needs platform data\n");
Robert Jarzmik77110ab2008-08-14 12:02:51 -03001005 return -EINVAL;
1006 }
1007
1008 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
1009 dev_warn(&adapter->dev,
1010 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
1011 return -EIO;
1012 }
1013
1014 mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL);
1015 if (!mt9m111)
1016 return -ENOMEM;
1017
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001018 v4l2_i2c_subdev_init(&mt9m111->subdev, client, &mt9m111_subdev_ops);
Robert Jarzmik77110ab2008-08-14 12:02:51 -03001019
1020 /* Second stage probe - when a capture adapter is there */
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -03001021 icd->ops = &mt9m111_ops;
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -03001022 icd->y_skip_top = 0;
Robert Jarzmik77110ab2008-08-14 12:02:51 -03001023
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -03001024 mt9m111->rect.left = MT9M111_MIN_DARK_COLS;
1025 mt9m111->rect.top = MT9M111_MIN_DARK_ROWS;
1026 mt9m111->rect.width = MT9M111_MAX_WIDTH;
1027 mt9m111->rect.height = MT9M111_MAX_HEIGHT;
1028
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001029 ret = mt9m111_video_probe(icd, client);
1030 if (ret) {
1031 icd->ops = NULL;
1032 i2c_set_clientdata(client, NULL);
1033 kfree(mt9m111);
1034 }
Robert Jarzmik77110ab2008-08-14 12:02:51 -03001035
Robert Jarzmik77110ab2008-08-14 12:02:51 -03001036 return ret;
1037}
1038
1039static int mt9m111_remove(struct i2c_client *client)
1040{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001041 struct mt9m111 *mt9m111 = to_mt9m111(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001042 struct soc_camera_device *icd = client->dev.platform_data;
1043
1044 icd->ops = NULL;
1045 i2c_set_clientdata(client, NULL);
1046 client->driver = NULL;
Robert Jarzmik77110ab2008-08-14 12:02:51 -03001047 kfree(mt9m111);
1048
1049 return 0;
1050}
1051
1052static const struct i2c_device_id mt9m111_id[] = {
1053 { "mt9m111", 0 },
1054 { }
1055};
1056MODULE_DEVICE_TABLE(i2c, mt9m111_id);
1057
1058static struct i2c_driver mt9m111_i2c_driver = {
1059 .driver = {
1060 .name = "mt9m111",
1061 },
1062 .probe = mt9m111_probe,
1063 .remove = mt9m111_remove,
1064 .id_table = mt9m111_id,
1065};
1066
1067static int __init mt9m111_mod_init(void)
1068{
1069 return i2c_add_driver(&mt9m111_i2c_driver);
1070}
1071
1072static void __exit mt9m111_mod_exit(void)
1073{
1074 i2c_del_driver(&mt9m111_i2c_driver);
1075}
1076
1077module_init(mt9m111_mod_init);
1078module_exit(mt9m111_mod_exit);
1079
Mike Rapoportd7f83a52008-12-18 11:44:15 -03001080MODULE_DESCRIPTION("Micron MT9M111/MT9M112 Camera driver");
Robert Jarzmik77110ab2008-08-14 12:02:51 -03001081MODULE_AUTHOR("Robert Jarzmik");
1082MODULE_LICENSE("GPL");