Commit | Line | Data |
---|---|---|
4e96fd08 GL |
1 | /* |
2 | * Driver for MT9T031 CMOS Image Sensor from Micron | |
3 | * | |
4 | * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.de> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
11 | #include <linux/videodev2.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/i2c.h> | |
14 | #include <linux/log2.h> | |
15 | ||
16 | #include <media/v4l2-common.h> | |
17 | #include <media/v4l2-chip-ident.h> | |
18 | #include <media/soc_camera.h> | |
19 | ||
20 | /* mt9t031 i2c address 0x5d | |
21 | * The platform has to define i2c_board_info | |
22 | * and call i2c_register_board_info() */ | |
23 | ||
24 | /* mt9t031 selected register addresses */ | |
25 | #define MT9T031_CHIP_VERSION 0x00 | |
26 | #define MT9T031_ROW_START 0x01 | |
27 | #define MT9T031_COLUMN_START 0x02 | |
28 | #define MT9T031_WINDOW_HEIGHT 0x03 | |
29 | #define MT9T031_WINDOW_WIDTH 0x04 | |
30 | #define MT9T031_HORIZONTAL_BLANKING 0x05 | |
31 | #define MT9T031_VERTICAL_BLANKING 0x06 | |
32 | #define MT9T031_OUTPUT_CONTROL 0x07 | |
33 | #define MT9T031_SHUTTER_WIDTH_UPPER 0x08 | |
34 | #define MT9T031_SHUTTER_WIDTH 0x09 | |
35 | #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a | |
36 | #define MT9T031_FRAME_RESTART 0x0b | |
37 | #define MT9T031_SHUTTER_DELAY 0x0c | |
38 | #define MT9T031_RESET 0x0d | |
39 | #define MT9T031_READ_MODE_1 0x1e | |
40 | #define MT9T031_READ_MODE_2 0x20 | |
41 | #define MT9T031_READ_MODE_3 0x21 | |
42 | #define MT9T031_ROW_ADDRESS_MODE 0x22 | |
43 | #define MT9T031_COLUMN_ADDRESS_MODE 0x23 | |
44 | #define MT9T031_GLOBAL_GAIN 0x35 | |
45 | #define MT9T031_CHIP_ENABLE 0xF8 | |
46 | ||
47 | #define MT9T031_MAX_HEIGHT 1536 | |
48 | #define MT9T031_MAX_WIDTH 2048 | |
49 | #define MT9T031_MIN_HEIGHT 2 | |
50 | #define MT9T031_MIN_WIDTH 2 | |
51 | #define MT9T031_HORIZONTAL_BLANK 142 | |
52 | #define MT9T031_VERTICAL_BLANK 25 | |
53 | #define MT9T031_COLUMN_SKIP 32 | |
54 | #define MT9T031_ROW_SKIP 20 | |
55 | ||
56 | #define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \ | |
57 | SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \ | |
58 | SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \ | |
59 | SOCAM_MASTER | SOCAM_DATAWIDTH_10) | |
60 | ||
61 | static const struct soc_camera_data_format mt9t031_colour_formats[] = { | |
62 | { | |
63 | .name = "Bayer (sRGB) 10 bit", | |
64 | .depth = 10, | |
65 | .fourcc = V4L2_PIX_FMT_SGRBG10, | |
66 | .colorspace = V4L2_COLORSPACE_SRGB, | |
67 | } | |
68 | }; | |
69 | ||
70 | struct mt9t031 { | |
4e96fd08 GL |
71 | int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */ |
72 | unsigned char autoexposure; | |
73 | u16 xskip; | |
74 | u16 yskip; | |
75 | }; | |
76 | ||
9538e1c2 | 77 | static int reg_read(struct i2c_client *client, const u8 reg) |
4e96fd08 | 78 | { |
4e96fd08 GL |
79 | s32 data = i2c_smbus_read_word_data(client, reg); |
80 | return data < 0 ? data : swab16(data); | |
81 | } | |
82 | ||
9538e1c2 | 83 | static int reg_write(struct i2c_client *client, const u8 reg, |
4e96fd08 GL |
84 | const u16 data) |
85 | { | |
9538e1c2 | 86 | return i2c_smbus_write_word_data(client, reg, swab16(data)); |
4e96fd08 GL |
87 | } |
88 | ||
9538e1c2 | 89 | static int reg_set(struct i2c_client *client, const u8 reg, |
4e96fd08 GL |
90 | const u16 data) |
91 | { | |
92 | int ret; | |
93 | ||
9538e1c2 | 94 | ret = reg_read(client, reg); |
4e96fd08 GL |
95 | if (ret < 0) |
96 | return ret; | |
9538e1c2 | 97 | return reg_write(client, reg, ret | data); |
4e96fd08 GL |
98 | } |
99 | ||
9538e1c2 | 100 | static int reg_clear(struct i2c_client *client, const u8 reg, |
4e96fd08 GL |
101 | const u16 data) |
102 | { | |
103 | int ret; | |
104 | ||
9538e1c2 | 105 | ret = reg_read(client, reg); |
4e96fd08 GL |
106 | if (ret < 0) |
107 | return ret; | |
9538e1c2 | 108 | return reg_write(client, reg, ret & ~data); |
4e96fd08 GL |
109 | } |
110 | ||
9538e1c2 | 111 | static int set_shutter(struct i2c_client *client, const u32 data) |
4e96fd08 GL |
112 | { |
113 | int ret; | |
114 | ||
9538e1c2 | 115 | ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16); |
4e96fd08 GL |
116 | |
117 | if (ret >= 0) | |
9538e1c2 | 118 | ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff); |
4e96fd08 GL |
119 | |
120 | return ret; | |
121 | } | |
122 | ||
9538e1c2 | 123 | static int get_shutter(struct i2c_client *client, u32 *data) |
4e96fd08 GL |
124 | { |
125 | int ret; | |
126 | ||
9538e1c2 | 127 | ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER); |
4e96fd08 GL |
128 | *data = ret << 16; |
129 | ||
130 | if (ret >= 0) | |
9538e1c2 | 131 | ret = reg_read(client, MT9T031_SHUTTER_WIDTH); |
4e96fd08 GL |
132 | *data |= ret & 0xffff; |
133 | ||
134 | return ret < 0 ? ret : 0; | |
135 | } | |
136 | ||
137 | static int mt9t031_init(struct soc_camera_device *icd) | |
138 | { | |
40e2e092 GL |
139 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
140 | struct soc_camera_link *icl = to_soc_camera_link(icd); | |
4e96fd08 GL |
141 | int ret; |
142 | ||
f9826514 | 143 | if (icl->power) { |
9538e1c2 | 144 | ret = icl->power(&client->dev, 1); |
f9826514 GL |
145 | if (ret < 0) { |
146 | dev_err(icd->vdev->parent, | |
147 | "Platform failed to power-on the camera.\n"); | |
148 | return ret; | |
149 | } | |
150 | } | |
151 | ||
4e96fd08 | 152 | /* Disable chip output, synchronous option update */ |
9538e1c2 | 153 | ret = reg_write(client, MT9T031_RESET, 1); |
4e96fd08 | 154 | if (ret >= 0) |
9538e1c2 | 155 | ret = reg_write(client, MT9T031_RESET, 0); |
4e96fd08 | 156 | if (ret >= 0) |
9538e1c2 | 157 | ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2); |
4e96fd08 | 158 | |
f9826514 | 159 | if (ret < 0 && icl->power) |
9538e1c2 | 160 | icl->power(&client->dev, 0); |
f9826514 | 161 | |
4e96fd08 GL |
162 | return ret >= 0 ? 0 : -EIO; |
163 | } | |
164 | ||
165 | static int mt9t031_release(struct soc_camera_device *icd) | |
166 | { | |
40e2e092 GL |
167 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
168 | struct soc_camera_link *icl = to_soc_camera_link(icd); | |
f9826514 | 169 | |
4e96fd08 | 170 | /* Disable the chip */ |
9538e1c2 | 171 | reg_clear(client, MT9T031_OUTPUT_CONTROL, 2); |
f9826514 GL |
172 | |
173 | if (icl->power) | |
9538e1c2 | 174 | icl->power(&client->dev, 0); |
f9826514 | 175 | |
4e96fd08 GL |
176 | return 0; |
177 | } | |
178 | ||
179 | static int mt9t031_start_capture(struct soc_camera_device *icd) | |
180 | { | |
40e2e092 | 181 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
9538e1c2 | 182 | |
4e96fd08 | 183 | /* Switch to master "normal" mode */ |
9538e1c2 | 184 | if (reg_set(client, MT9T031_OUTPUT_CONTROL, 2) < 0) |
4e96fd08 GL |
185 | return -EIO; |
186 | return 0; | |
187 | } | |
188 | ||
189 | static int mt9t031_stop_capture(struct soc_camera_device *icd) | |
190 | { | |
40e2e092 | 191 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
9538e1c2 | 192 | |
4e96fd08 | 193 | /* Stop sensor readout */ |
9538e1c2 | 194 | if (reg_clear(client, MT9T031_OUTPUT_CONTROL, 2) < 0) |
4e96fd08 GL |
195 | return -EIO; |
196 | return 0; | |
197 | } | |
198 | ||
199 | static int mt9t031_set_bus_param(struct soc_camera_device *icd, | |
200 | unsigned long flags) | |
201 | { | |
40e2e092 | 202 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
9538e1c2 | 203 | |
4e96fd08 GL |
204 | /* The caller should have queried our parameters, check anyway */ |
205 | if (flags & ~MT9T031_BUS_PARAM) | |
206 | return -EINVAL; | |
207 | ||
208 | if (flags & SOCAM_PCLK_SAMPLE_FALLING) | |
9538e1c2 | 209 | reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000); |
c98afbfc | 210 | else |
9538e1c2 | 211 | reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000); |
4e96fd08 GL |
212 | |
213 | return 0; | |
214 | } | |
215 | ||
216 | static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd) | |
217 | { | |
40e2e092 | 218 | struct soc_camera_link *icl = to_soc_camera_link(icd); |
4e96fd08 GL |
219 | |
220 | return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM); | |
221 | } | |
222 | ||
70e1d353 GL |
223 | /* Round up minima and round down maxima */ |
224 | static void recalculate_limits(struct soc_camera_device *icd, | |
225 | u16 xskip, u16 yskip) | |
226 | { | |
227 | icd->x_min = (MT9T031_COLUMN_SKIP + xskip - 1) / xskip; | |
228 | icd->y_min = (MT9T031_ROW_SKIP + yskip - 1) / yskip; | |
229 | icd->width_min = (MT9T031_MIN_WIDTH + xskip - 1) / xskip; | |
230 | icd->height_min = (MT9T031_MIN_HEIGHT + yskip - 1) / yskip; | |
231 | icd->width_max = MT9T031_MAX_WIDTH / xskip; | |
232 | icd->height_max = MT9T031_MAX_HEIGHT / yskip; | |
233 | } | |
234 | ||
09e231b3 GL |
235 | static int mt9t031_set_params(struct soc_camera_device *icd, |
236 | struct v4l2_rect *rect, u16 xskip, u16 yskip) | |
4e96fd08 | 237 | { |
40e2e092 GL |
238 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
239 | struct mt9t031 *mt9t031 = i2c_get_clientdata(client); | |
4e96fd08 | 240 | int ret; |
09e231b3 | 241 | u16 xbin, ybin, width, height, left, top; |
4e96fd08 GL |
242 | const u16 hblank = MT9T031_HORIZONTAL_BLANK, |
243 | vblank = MT9T031_VERTICAL_BLANK; | |
4e96fd08 | 244 | |
70e1d353 GL |
245 | /* Make sure we don't exceed sensor limits */ |
246 | if (rect->left + rect->width > icd->width_max) | |
247 | rect->left = (icd->width_max - rect->width) / 2 + icd->x_min; | |
248 | ||
249 | if (rect->top + rect->height > icd->height_max) | |
250 | rect->top = (icd->height_max - rect->height) / 2 + icd->y_min; | |
251 | ||
252 | width = rect->width * xskip; | |
253 | height = rect->height * yskip; | |
254 | left = rect->left * xskip; | |
255 | top = rect->top * yskip; | |
256 | ||
4e96fd08 GL |
257 | xbin = min(xskip, (u16)3); |
258 | ybin = min(yskip, (u16)3); | |
259 | ||
70e1d353 GL |
260 | dev_dbg(&icd->dev, "xskip %u, width %u/%u, yskip %u, height %u/%u\n", |
261 | xskip, width, rect->width, yskip, height, rect->height); | |
4e96fd08 | 262 | |
70e1d353 | 263 | /* Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper */ |
4e96fd08 GL |
264 | switch (xbin) { |
265 | case 2: | |
70e1d353 | 266 | left = (left + 3) & ~3; |
4e96fd08 GL |
267 | break; |
268 | case 3: | |
70e1d353 | 269 | left = roundup(left, 6); |
4e96fd08 GL |
270 | } |
271 | ||
272 | switch (ybin) { | |
273 | case 2: | |
70e1d353 | 274 | top = (top + 3) & ~3; |
4e96fd08 GL |
275 | break; |
276 | case 3: | |
70e1d353 | 277 | top = roundup(top, 6); |
4e96fd08 GL |
278 | } |
279 | ||
70e1d353 | 280 | /* Disable register update, reconfigure atomically */ |
9538e1c2 | 281 | ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1); |
70e1d353 GL |
282 | if (ret < 0) |
283 | return ret; | |
284 | ||
4e96fd08 | 285 | /* Blanking and start values - default... */ |
9538e1c2 | 286 | ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank); |
4e96fd08 | 287 | if (ret >= 0) |
9538e1c2 | 288 | ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank); |
4e96fd08 | 289 | |
09e231b3 | 290 | if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) { |
4e96fd08 GL |
291 | /* Binning, skipping */ |
292 | if (ret >= 0) | |
9538e1c2 | 293 | ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE, |
4e96fd08 GL |
294 | ((xbin - 1) << 4) | (xskip - 1)); |
295 | if (ret >= 0) | |
9538e1c2 | 296 | ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE, |
4e96fd08 GL |
297 | ((ybin - 1) << 4) | (yskip - 1)); |
298 | } | |
70e1d353 | 299 | dev_dbg(&icd->dev, "new physical left %u, top %u\n", left, top); |
4e96fd08 GL |
300 | |
301 | /* The caller provides a supported format, as guaranteed by | |
302 | * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap() */ | |
303 | if (ret >= 0) | |
9538e1c2 | 304 | ret = reg_write(client, MT9T031_COLUMN_START, left); |
4e96fd08 | 305 | if (ret >= 0) |
9538e1c2 | 306 | ret = reg_write(client, MT9T031_ROW_START, top); |
4e96fd08 | 307 | if (ret >= 0) |
9538e1c2 | 308 | ret = reg_write(client, MT9T031_WINDOW_WIDTH, width - 1); |
4e96fd08 | 309 | if (ret >= 0) |
9538e1c2 | 310 | ret = reg_write(client, MT9T031_WINDOW_HEIGHT, |
4e96fd08 GL |
311 | height + icd->y_skip_top - 1); |
312 | if (ret >= 0 && mt9t031->autoexposure) { | |
9538e1c2 | 313 | ret = set_shutter(client, height + icd->y_skip_top + vblank); |
4e96fd08 GL |
314 | if (ret >= 0) { |
315 | const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank; | |
316 | const struct v4l2_queryctrl *qctrl = | |
317 | soc_camera_find_qctrl(icd->ops, | |
318 | V4L2_CID_EXPOSURE); | |
319 | icd->exposure = (shutter_max / 2 + (height + | |
320 | icd->y_skip_top + vblank - 1) * | |
321 | (qctrl->maximum - qctrl->minimum)) / | |
322 | shutter_max + qctrl->minimum; | |
323 | } | |
324 | } | |
325 | ||
09e231b3 GL |
326 | /* Re-enable register update, commit all changes */ |
327 | if (ret >= 0) | |
9538e1c2 | 328 | ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1); |
09e231b3 GL |
329 | |
330 | return ret < 0 ? ret : 0; | |
331 | } | |
332 | ||
333 | static int mt9t031_set_crop(struct soc_camera_device *icd, | |
334 | struct v4l2_rect *rect) | |
335 | { | |
40e2e092 GL |
336 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
337 | struct mt9t031 *mt9t031 = i2c_get_clientdata(client); | |
09e231b3 GL |
338 | |
339 | /* CROP - no change in scaling, or in limits */ | |
340 | return mt9t031_set_params(icd, rect, mt9t031->xskip, mt9t031->yskip); | |
341 | } | |
342 | ||
343 | static int mt9t031_set_fmt(struct soc_camera_device *icd, | |
344 | struct v4l2_format *f) | |
345 | { | |
40e2e092 GL |
346 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
347 | struct mt9t031 *mt9t031 = i2c_get_clientdata(client); | |
09e231b3 GL |
348 | int ret; |
349 | u16 xskip, yskip; | |
350 | struct v4l2_rect rect = { | |
351 | .left = icd->x_current, | |
352 | .top = icd->y_current, | |
353 | .width = f->fmt.pix.width, | |
354 | .height = f->fmt.pix.height, | |
355 | }; | |
356 | ||
357 | /* | |
358 | * try_fmt has put rectangle within limits. | |
359 | * S_FMT - use binning and skipping for scaling, recalculate | |
360 | * limits, used for cropping | |
361 | */ | |
362 | /* Is this more optimal than just a division? */ | |
363 | for (xskip = 8; xskip > 1; xskip--) | |
364 | if (rect.width * xskip <= MT9T031_MAX_WIDTH) | |
365 | break; | |
366 | ||
367 | for (yskip = 8; yskip > 1; yskip--) | |
368 | if (rect.height * yskip <= MT9T031_MAX_HEIGHT) | |
369 | break; | |
370 | ||
371 | recalculate_limits(icd, xskip, yskip); | |
372 | ||
373 | ret = mt9t031_set_params(icd, &rect, xskip, yskip); | |
374 | if (!ret) { | |
4e96fd08 GL |
375 | mt9t031->xskip = xskip; |
376 | mt9t031->yskip = yskip; | |
377 | } | |
378 | ||
09e231b3 | 379 | return ret; |
4e96fd08 GL |
380 | } |
381 | ||
382 | static int mt9t031_try_fmt(struct soc_camera_device *icd, | |
383 | struct v4l2_format *f) | |
384 | { | |
385 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
386 | ||
653dc59b TP |
387 | v4l_bound_align_image( |
388 | &pix->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1, | |
389 | &pix->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0); | |
4e96fd08 GL |
390 | |
391 | return 0; | |
392 | } | |
393 | ||
394 | static int mt9t031_get_chip_id(struct soc_camera_device *icd, | |
aecde8b5 | 395 | struct v4l2_dbg_chip_ident *id) |
4e96fd08 | 396 | { |
40e2e092 GL |
397 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
398 | struct mt9t031 *mt9t031 = i2c_get_clientdata(client); | |
4e96fd08 | 399 | |
aecde8b5 | 400 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
4e96fd08 GL |
401 | return -EINVAL; |
402 | ||
40e2e092 | 403 | if (id->match.addr != client->addr) |
4e96fd08 GL |
404 | return -ENODEV; |
405 | ||
406 | id->ident = mt9t031->model; | |
407 | id->revision = 0; | |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
412 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
413 | static int mt9t031_get_register(struct soc_camera_device *icd, | |
aecde8b5 | 414 | struct v4l2_dbg_register *reg) |
4e96fd08 | 415 | { |
40e2e092 | 416 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
4e96fd08 | 417 | |
aecde8b5 | 418 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
4e96fd08 GL |
419 | return -EINVAL; |
420 | ||
9538e1c2 | 421 | if (reg->match.addr != client->addr) |
4e96fd08 GL |
422 | return -ENODEV; |
423 | ||
9538e1c2 | 424 | reg->val = reg_read(client, reg->reg); |
4e96fd08 GL |
425 | |
426 | if (reg->val > 0xffff) | |
427 | return -EIO; | |
428 | ||
429 | return 0; | |
430 | } | |
431 | ||
432 | static int mt9t031_set_register(struct soc_camera_device *icd, | |
aecde8b5 | 433 | struct v4l2_dbg_register *reg) |
4e96fd08 | 434 | { |
40e2e092 | 435 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
4e96fd08 | 436 | |
aecde8b5 | 437 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
4e96fd08 GL |
438 | return -EINVAL; |
439 | ||
9538e1c2 | 440 | if (reg->match.addr != client->addr) |
4e96fd08 GL |
441 | return -ENODEV; |
442 | ||
9538e1c2 | 443 | if (reg_write(client, reg->reg, reg->val) < 0) |
4e96fd08 GL |
444 | return -EIO; |
445 | ||
446 | return 0; | |
447 | } | |
448 | #endif | |
449 | ||
450 | static const struct v4l2_queryctrl mt9t031_controls[] = { | |
451 | { | |
452 | .id = V4L2_CID_VFLIP, | |
453 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
454 | .name = "Flip Vertically", | |
455 | .minimum = 0, | |
456 | .maximum = 1, | |
457 | .step = 1, | |
458 | .default_value = 0, | |
70e1d353 GL |
459 | }, { |
460 | .id = V4L2_CID_HFLIP, | |
461 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
462 | .name = "Flip Horizontally", | |
463 | .minimum = 0, | |
464 | .maximum = 1, | |
465 | .step = 1, | |
466 | .default_value = 0, | |
4e96fd08 GL |
467 | }, { |
468 | .id = V4L2_CID_GAIN, | |
469 | .type = V4L2_CTRL_TYPE_INTEGER, | |
470 | .name = "Gain", | |
471 | .minimum = 0, | |
472 | .maximum = 127, | |
473 | .step = 1, | |
474 | .default_value = 64, | |
475 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
476 | }, { | |
477 | .id = V4L2_CID_EXPOSURE, | |
478 | .type = V4L2_CTRL_TYPE_INTEGER, | |
479 | .name = "Exposure", | |
480 | .minimum = 1, | |
481 | .maximum = 255, | |
482 | .step = 1, | |
483 | .default_value = 255, | |
484 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
485 | }, { | |
486 | .id = V4L2_CID_EXPOSURE_AUTO, | |
487 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
488 | .name = "Automatic Exposure", | |
489 | .minimum = 0, | |
490 | .maximum = 1, | |
491 | .step = 1, | |
492 | .default_value = 1, | |
493 | } | |
494 | }; | |
495 | ||
4e96fd08 GL |
496 | static int mt9t031_get_control(struct soc_camera_device *, struct v4l2_control *); |
497 | static int mt9t031_set_control(struct soc_camera_device *, struct v4l2_control *); | |
498 | ||
499 | static struct soc_camera_ops mt9t031_ops = { | |
500 | .owner = THIS_MODULE, | |
4e96fd08 GL |
501 | .init = mt9t031_init, |
502 | .release = mt9t031_release, | |
503 | .start_capture = mt9t031_start_capture, | |
504 | .stop_capture = mt9t031_stop_capture, | |
09e231b3 | 505 | .set_crop = mt9t031_set_crop, |
4e96fd08 GL |
506 | .set_fmt = mt9t031_set_fmt, |
507 | .try_fmt = mt9t031_try_fmt, | |
508 | .set_bus_param = mt9t031_set_bus_param, | |
509 | .query_bus_param = mt9t031_query_bus_param, | |
510 | .controls = mt9t031_controls, | |
511 | .num_controls = ARRAY_SIZE(mt9t031_controls), | |
512 | .get_control = mt9t031_get_control, | |
513 | .set_control = mt9t031_set_control, | |
514 | .get_chip_id = mt9t031_get_chip_id, | |
515 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
516 | .get_register = mt9t031_get_register, | |
517 | .set_register = mt9t031_set_register, | |
518 | #endif | |
519 | }; | |
520 | ||
521 | static int mt9t031_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl) | |
522 | { | |
40e2e092 GL |
523 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
524 | struct mt9t031 *mt9t031 = i2c_get_clientdata(client); | |
4e96fd08 GL |
525 | int data; |
526 | ||
527 | switch (ctrl->id) { | |
528 | case V4L2_CID_VFLIP: | |
9538e1c2 | 529 | data = reg_read(client, MT9T031_READ_MODE_2); |
4e96fd08 GL |
530 | if (data < 0) |
531 | return -EIO; | |
532 | ctrl->value = !!(data & 0x8000); | |
533 | break; | |
534 | case V4L2_CID_HFLIP: | |
9538e1c2 | 535 | data = reg_read(client, MT9T031_READ_MODE_2); |
4e96fd08 GL |
536 | if (data < 0) |
537 | return -EIO; | |
538 | ctrl->value = !!(data & 0x4000); | |
539 | break; | |
540 | case V4L2_CID_EXPOSURE_AUTO: | |
541 | ctrl->value = mt9t031->autoexposure; | |
542 | break; | |
543 | } | |
544 | return 0; | |
545 | } | |
546 | ||
547 | static int mt9t031_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl) | |
548 | { | |
40e2e092 GL |
549 | struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd)); |
550 | struct mt9t031 *mt9t031 = i2c_get_clientdata(client); | |
4e96fd08 GL |
551 | const struct v4l2_queryctrl *qctrl; |
552 | int data; | |
553 | ||
554 | qctrl = soc_camera_find_qctrl(&mt9t031_ops, ctrl->id); | |
555 | ||
556 | if (!qctrl) | |
557 | return -EINVAL; | |
558 | ||
559 | switch (ctrl->id) { | |
560 | case V4L2_CID_VFLIP: | |
561 | if (ctrl->value) | |
9538e1c2 | 562 | data = reg_set(client, MT9T031_READ_MODE_2, 0x8000); |
4e96fd08 | 563 | else |
9538e1c2 | 564 | data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000); |
4e96fd08 GL |
565 | if (data < 0) |
566 | return -EIO; | |
567 | break; | |
568 | case V4L2_CID_HFLIP: | |
569 | if (ctrl->value) | |
9538e1c2 | 570 | data = reg_set(client, MT9T031_READ_MODE_2, 0x4000); |
4e96fd08 | 571 | else |
9538e1c2 | 572 | data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000); |
4e96fd08 GL |
573 | if (data < 0) |
574 | return -EIO; | |
575 | break; | |
576 | case V4L2_CID_GAIN: | |
577 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
578 | return -EINVAL; | |
579 | /* See Datasheet Table 7, Gain settings. */ | |
580 | if (ctrl->value <= qctrl->default_value) { | |
581 | /* Pack it into 0..1 step 0.125, register values 0..8 */ | |
582 | unsigned long range = qctrl->default_value - qctrl->minimum; | |
583 | data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range; | |
584 | ||
585 | dev_dbg(&icd->dev, "Setting gain %d\n", data); | |
9538e1c2 | 586 | data = reg_write(client, MT9T031_GLOBAL_GAIN, data); |
4e96fd08 GL |
587 | if (data < 0) |
588 | return -EIO; | |
589 | } else { | |
70e1d353 | 590 | /* Pack it into 1.125..128 variable step, register values 9..0x7860 */ |
4e96fd08 GL |
591 | /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ |
592 | unsigned long range = qctrl->maximum - qctrl->default_value - 1; | |
70e1d353 | 593 | /* calculated gain: map 65..127 to 9..1024 step 0.125 */ |
4e96fd08 | 594 | unsigned long gain = ((ctrl->value - qctrl->default_value - 1) * |
70e1d353 | 595 | 1015 + range / 2) / range + 9; |
4e96fd08 | 596 | |
70e1d353 | 597 | if (gain <= 32) /* calculated gain 9..32 -> 9..32 */ |
4e96fd08 | 598 | data = gain; |
70e1d353 | 599 | else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */ |
4e96fd08 GL |
600 | data = ((gain - 32) * 16 + 16) / 32 + 80; |
601 | else | |
70e1d353 GL |
602 | /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */ |
603 | data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60; | |
4e96fd08 | 604 | |
70e1d353 | 605 | dev_dbg(&icd->dev, "Setting gain from 0x%x to 0x%x\n", |
9538e1c2 GL |
606 | reg_read(client, MT9T031_GLOBAL_GAIN), data); |
607 | data = reg_write(client, MT9T031_GLOBAL_GAIN, data); | |
4e96fd08 GL |
608 | if (data < 0) |
609 | return -EIO; | |
610 | } | |
611 | ||
612 | /* Success */ | |
613 | icd->gain = ctrl->value; | |
614 | break; | |
615 | case V4L2_CID_EXPOSURE: | |
616 | /* mt9t031 has maximum == default */ | |
617 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
618 | return -EINVAL; | |
619 | else { | |
620 | const unsigned long range = qctrl->maximum - qctrl->minimum; | |
621 | const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 + | |
622 | range / 2) / range + 1; | |
623 | u32 old; | |
624 | ||
9538e1c2 | 625 | get_shutter(client, &old); |
4e96fd08 GL |
626 | dev_dbg(&icd->dev, "Setting shutter width from %u to %u\n", |
627 | old, shutter); | |
9538e1c2 | 628 | if (set_shutter(client, shutter) < 0) |
4e96fd08 GL |
629 | return -EIO; |
630 | icd->exposure = ctrl->value; | |
631 | mt9t031->autoexposure = 0; | |
632 | } | |
633 | break; | |
634 | case V4L2_CID_EXPOSURE_AUTO: | |
635 | if (ctrl->value) { | |
636 | const u16 vblank = MT9T031_VERTICAL_BLANK; | |
637 | const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank; | |
9538e1c2 | 638 | if (set_shutter(client, icd->height + |
4e96fd08 GL |
639 | icd->y_skip_top + vblank) < 0) |
640 | return -EIO; | |
641 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); | |
642 | icd->exposure = (shutter_max / 2 + (icd->height + | |
643 | icd->y_skip_top + vblank - 1) * | |
644 | (qctrl->maximum - qctrl->minimum)) / | |
645 | shutter_max + qctrl->minimum; | |
646 | mt9t031->autoexposure = 1; | |
647 | } else | |
648 | mt9t031->autoexposure = 0; | |
649 | break; | |
650 | } | |
651 | return 0; | |
652 | } | |
653 | ||
654 | /* Interface active, can use i2c. If it fails, it can indeed mean, that | |
655 | * this wasn't our capture interface, so, we wait for the right one */ | |
40e2e092 GL |
656 | static int mt9t031_video_probe(struct soc_camera_device *icd, |
657 | struct i2c_client *client) | |
4e96fd08 | 658 | { |
40e2e092 | 659 | struct mt9t031 *mt9t031 = i2c_get_clientdata(client); |
4e96fd08 GL |
660 | s32 data; |
661 | int ret; | |
662 | ||
663 | /* We must have a parent by now. And it cannot be a wrong one. | |
664 | * So this entire test is completely redundant. */ | |
665 | if (!icd->dev.parent || | |
666 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) | |
667 | return -ENODEV; | |
668 | ||
40e2e092 GL |
669 | /* Switch master clock on */ |
670 | ret = soc_camera_video_start(icd, &client->dev); | |
671 | if (ret) | |
672 | return ret; | |
673 | ||
4e96fd08 | 674 | /* Enable the chip */ |
9538e1c2 | 675 | data = reg_write(client, MT9T031_CHIP_ENABLE, 1); |
4e96fd08 GL |
676 | dev_dbg(&icd->dev, "write: %d\n", data); |
677 | ||
678 | /* Read out the chip version register */ | |
9538e1c2 | 679 | data = reg_read(client, MT9T031_CHIP_VERSION); |
4e96fd08 | 680 | |
40e2e092 GL |
681 | soc_camera_video_stop(icd); |
682 | ||
4e96fd08 GL |
683 | switch (data) { |
684 | case 0x1621: | |
685 | mt9t031->model = V4L2_IDENT_MT9T031; | |
686 | icd->formats = mt9t031_colour_formats; | |
687 | icd->num_formats = ARRAY_SIZE(mt9t031_colour_formats); | |
688 | break; | |
689 | default: | |
4e96fd08 GL |
690 | dev_err(&icd->dev, |
691 | "No MT9T031 chip detected, register read %x\n", data); | |
40e2e092 | 692 | return -ENODEV; |
4e96fd08 GL |
693 | } |
694 | ||
695 | dev_info(&icd->dev, "Detected a MT9T031 chip ID %x\n", data); | |
696 | ||
4e96fd08 | 697 | return 0; |
4e96fd08 GL |
698 | } |
699 | ||
700 | static int mt9t031_probe(struct i2c_client *client, | |
701 | const struct i2c_device_id *did) | |
702 | { | |
703 | struct mt9t031 *mt9t031; | |
40e2e092 | 704 | struct soc_camera_device *icd = client->dev.platform_data; |
4e96fd08 | 705 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
40e2e092 | 706 | struct soc_camera_link *icl; |
4e96fd08 GL |
707 | int ret; |
708 | ||
40e2e092 GL |
709 | if (!icd) { |
710 | dev_err(&client->dev, "MT9T031: missing soc-camera data!\n"); | |
711 | return -EINVAL; | |
712 | } | |
713 | ||
714 | icl = to_soc_camera_link(icd); | |
4e96fd08 GL |
715 | if (!icl) { |
716 | dev_err(&client->dev, "MT9T031 driver needs platform data\n"); | |
717 | return -EINVAL; | |
718 | } | |
719 | ||
720 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { | |
721 | dev_warn(&adapter->dev, | |
722 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); | |
723 | return -EIO; | |
724 | } | |
725 | ||
726 | mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL); | |
727 | if (!mt9t031) | |
728 | return -ENOMEM; | |
729 | ||
4e96fd08 GL |
730 | i2c_set_clientdata(client, mt9t031); |
731 | ||
732 | /* Second stage probe - when a capture adapter is there */ | |
4e96fd08 | 733 | icd->ops = &mt9t031_ops; |
4e96fd08 GL |
734 | icd->x_min = MT9T031_COLUMN_SKIP; |
735 | icd->y_min = MT9T031_ROW_SKIP; | |
736 | icd->x_current = icd->x_min; | |
737 | icd->y_current = icd->y_min; | |
738 | icd->width_min = MT9T031_MIN_WIDTH; | |
739 | icd->width_max = MT9T031_MAX_WIDTH; | |
740 | icd->height_min = MT9T031_MIN_HEIGHT; | |
741 | icd->height_max = MT9T031_MAX_HEIGHT; | |
742 | icd->y_skip_top = 0; | |
4e96fd08 GL |
743 | /* Simulated autoexposure. If enabled, we calculate shutter width |
744 | * ourselves in the driver based on vertical blanking and frame width */ | |
745 | mt9t031->autoexposure = 1; | |
746 | ||
747 | mt9t031->xskip = 1; | |
748 | mt9t031->yskip = 1; | |
749 | ||
40e2e092 GL |
750 | ret = mt9t031_video_probe(icd, client); |
751 | if (ret) { | |
752 | icd->ops = NULL; | |
753 | i2c_set_clientdata(client, NULL); | |
754 | kfree(mt9t031); | |
755 | } | |
4e96fd08 | 756 | |
4e96fd08 GL |
757 | return ret; |
758 | } | |
759 | ||
760 | static int mt9t031_remove(struct i2c_client *client) | |
761 | { | |
762 | struct mt9t031 *mt9t031 = i2c_get_clientdata(client); | |
40e2e092 | 763 | struct soc_camera_device *icd = client->dev.platform_data; |
4e96fd08 | 764 | |
40e2e092 | 765 | icd->ops = NULL; |
4e96fd08 | 766 | i2c_set_clientdata(client, NULL); |
40e2e092 | 767 | client->driver = NULL; |
4e96fd08 GL |
768 | kfree(mt9t031); |
769 | ||
770 | return 0; | |
771 | } | |
772 | ||
773 | static const struct i2c_device_id mt9t031_id[] = { | |
774 | { "mt9t031", 0 }, | |
775 | { } | |
776 | }; | |
777 | MODULE_DEVICE_TABLE(i2c, mt9t031_id); | |
778 | ||
779 | static struct i2c_driver mt9t031_i2c_driver = { | |
780 | .driver = { | |
781 | .name = "mt9t031", | |
782 | }, | |
783 | .probe = mt9t031_probe, | |
784 | .remove = mt9t031_remove, | |
785 | .id_table = mt9t031_id, | |
786 | }; | |
787 | ||
788 | static int __init mt9t031_mod_init(void) | |
789 | { | |
790 | return i2c_add_driver(&mt9t031_i2c_driver); | |
791 | } | |
792 | ||
793 | static void __exit mt9t031_mod_exit(void) | |
794 | { | |
795 | i2c_del_driver(&mt9t031_i2c_driver); | |
796 | } | |
797 | ||
798 | module_init(mt9t031_mod_init); | |
799 | module_exit(mt9t031_mod_exit); | |
800 | ||
801 | MODULE_DESCRIPTION("Micron MT9T031 Camera driver"); | |
802 | MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>"); | |
803 | MODULE_LICENSE("GPL v2"); |