Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for MT9V022 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/delay.h> |
| 15 | #include <linux/log2.h> |
| 16 | |
| 17 | #include <media/v4l2-common.h> |
| 18 | #include <media/v4l2-chip-ident.h> |
| 19 | #include <media/soc_camera.h> |
| 20 | |
Guennadi Liakhovetski | ef6ad5c | 2008-04-22 14:42:08 -0300 | [diff] [blame^] | 21 | #ifdef CONFIG_MT9M001_PCA9536_SWITCH |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 22 | #include <asm/gpio.h> |
Guennadi Liakhovetski | ef6ad5c | 2008-04-22 14:42:08 -0300 | [diff] [blame^] | 23 | #endif |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 24 | |
| 25 | /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c |
| 26 | * The platform has to define i2c_board_info |
| 27 | * and call i2c_register_board_info() */ |
| 28 | |
| 29 | static char *sensor_type; |
| 30 | module_param(sensor_type, charp, S_IRUGO); |
| 31 | MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"\n"); |
| 32 | |
| 33 | /* mt9v022 selected register addresses */ |
| 34 | #define MT9V022_CHIP_VERSION 0x00 |
| 35 | #define MT9V022_COLUMN_START 0x01 |
| 36 | #define MT9V022_ROW_START 0x02 |
| 37 | #define MT9V022_WINDOW_HEIGHT 0x03 |
| 38 | #define MT9V022_WINDOW_WIDTH 0x04 |
| 39 | #define MT9V022_HORIZONTAL_BLANKING 0x05 |
| 40 | #define MT9V022_VERTICAL_BLANKING 0x06 |
| 41 | #define MT9V022_CHIP_CONTROL 0x07 |
| 42 | #define MT9V022_SHUTTER_WIDTH1 0x08 |
| 43 | #define MT9V022_SHUTTER_WIDTH2 0x09 |
| 44 | #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a |
| 45 | #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b |
| 46 | #define MT9V022_RESET 0x0c |
| 47 | #define MT9V022_READ_MODE 0x0d |
| 48 | #define MT9V022_MONITOR_MODE 0x0e |
| 49 | #define MT9V022_PIXEL_OPERATION_MODE 0x0f |
| 50 | #define MT9V022_LED_OUT_CONTROL 0x1b |
| 51 | #define MT9V022_ADC_MODE_CONTROL 0x1c |
| 52 | #define MT9V022_ANALOG_GAIN 0x34 |
| 53 | #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47 |
| 54 | #define MT9V022_PIXCLK_FV_LV 0x74 |
| 55 | #define MT9V022_DIGITAL_TEST_PATTERN 0x7f |
| 56 | #define MT9V022_AEC_AGC_ENABLE 0xAF |
| 57 | #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD |
| 58 | |
| 59 | /* Progressive scan, master, defaults */ |
| 60 | #define MT9V022_CHIP_CONTROL_DEFAULT 0x188 |
| 61 | |
| 62 | static const struct soc_camera_data_format mt9v022_formats[] = { |
| 63 | { |
| 64 | .name = "RGB Bayer (sRGB)", |
| 65 | .depth = 8, |
| 66 | .fourcc = V4L2_PIX_FMT_SBGGR8, |
| 67 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 68 | }, { |
| 69 | .name = "RGB Bayer (sRGB)", |
| 70 | .depth = 10, |
| 71 | .fourcc = V4L2_PIX_FMT_SBGGR16, |
| 72 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 73 | }, { |
| 74 | .name = "Monochrome 10 bit", |
| 75 | .depth = 10, |
| 76 | .fourcc = V4L2_PIX_FMT_Y16, |
| 77 | }, { |
| 78 | .name = "Monochrome 8 bit", |
| 79 | .depth = 8, |
| 80 | .fourcc = V4L2_PIX_FMT_GREY, |
| 81 | }, |
| 82 | }; |
| 83 | |
| 84 | struct mt9v022 { |
| 85 | struct i2c_client *client; |
| 86 | struct soc_camera_device icd; |
| 87 | int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */ |
| 88 | int switch_gpio; |
| 89 | u16 chip_control; |
| 90 | unsigned char datawidth; |
| 91 | }; |
| 92 | |
| 93 | static int reg_read(struct soc_camera_device *icd, const u8 reg) |
| 94 | { |
| 95 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 96 | struct i2c_client *client = mt9v022->client; |
| 97 | s32 data = i2c_smbus_read_word_data(client, reg); |
| 98 | return data < 0 ? data : swab16(data); |
| 99 | } |
| 100 | |
| 101 | static int reg_write(struct soc_camera_device *icd, const u8 reg, |
| 102 | const u16 data) |
| 103 | { |
| 104 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 105 | return i2c_smbus_write_word_data(mt9v022->client, reg, swab16(data)); |
| 106 | } |
| 107 | |
| 108 | static int reg_set(struct soc_camera_device *icd, const u8 reg, |
| 109 | const u16 data) |
| 110 | { |
| 111 | int ret; |
| 112 | |
| 113 | ret = reg_read(icd, reg); |
| 114 | if (ret < 0) |
| 115 | return ret; |
| 116 | return reg_write(icd, reg, ret | data); |
| 117 | } |
| 118 | |
| 119 | static int reg_clear(struct soc_camera_device *icd, const u8 reg, |
| 120 | const u16 data) |
| 121 | { |
| 122 | int ret; |
| 123 | |
| 124 | ret = reg_read(icd, reg); |
| 125 | if (ret < 0) |
| 126 | return ret; |
| 127 | return reg_write(icd, reg, ret & ~data); |
| 128 | } |
| 129 | |
| 130 | static int mt9v022_init(struct soc_camera_device *icd) |
| 131 | { |
| 132 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 133 | int ret; |
| 134 | |
| 135 | /* Almost the default mode: master, parallel, simultaneous, and an |
| 136 | * undocumented bit 0x200, which is present in table 7, but not in 8, |
| 137 | * plus snapshot mode to disable scan for now */ |
| 138 | mt9v022->chip_control |= 0x10; |
| 139 | ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control); |
| 140 | if (ret >= 0) |
| 141 | reg_write(icd, MT9V022_READ_MODE, 0x300); |
| 142 | |
| 143 | /* All defaults */ |
| 144 | if (ret >= 0) |
| 145 | /* AEC, AGC on */ |
| 146 | ret = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x3); |
| 147 | if (ret >= 0) |
| 148 | ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480); |
| 149 | if (ret >= 0) |
| 150 | /* default - auto */ |
| 151 | ret = reg_clear(icd, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1); |
| 152 | if (ret >= 0) |
| 153 | ret = reg_write(icd, MT9V022_DIGITAL_TEST_PATTERN, 0); |
| 154 | |
| 155 | return ret >= 0 ? 0 : -EIO; |
| 156 | } |
| 157 | |
| 158 | static int mt9v022_release(struct soc_camera_device *icd) |
| 159 | { |
| 160 | /* Nothing? */ |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int mt9v022_start_capture(struct soc_camera_device *icd) |
| 165 | { |
| 166 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 167 | /* Switch to master "normal" mode */ |
| 168 | mt9v022->chip_control &= ~0x10; |
| 169 | if (reg_write(icd, MT9V022_CHIP_CONTROL, |
| 170 | mt9v022->chip_control) < 0) |
| 171 | return -EIO; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int mt9v022_stop_capture(struct soc_camera_device *icd) |
| 176 | { |
| 177 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 178 | /* Switch to snapshot mode */ |
| 179 | mt9v022->chip_control |= 0x10; |
| 180 | if (reg_write(icd, MT9V022_CHIP_CONTROL, |
| 181 | mt9v022->chip_control) < 0) |
| 182 | return -EIO; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int bus_switch_request(struct mt9v022 *mt9v022, struct soc_camera_link *icl) |
| 187 | { |
| 188 | #ifdef CONFIG_MT9V022_PCA9536_SWITCH |
| 189 | int ret; |
| 190 | unsigned int gpio = icl->gpio; |
| 191 | |
Guennadi Liakhovetski | b4333a3 | 2008-04-22 14:42:08 -0300 | [diff] [blame] | 192 | if (gpio_is_valid(gpio)) { |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 193 | /* We have a data bus switch. */ |
| 194 | ret = gpio_request(gpio, "mt9v022"); |
| 195 | if (ret < 0) { |
| 196 | dev_err(&mt9v022->client->dev, "Cannot get GPIO %u\n", gpio); |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | ret = gpio_direction_output(gpio, 0); |
| 201 | if (ret < 0) { |
| 202 | dev_err(&mt9v022->client->dev, |
| 203 | "Cannot set GPIO %u to output\n", gpio); |
| 204 | gpio_free(gpio); |
| 205 | return ret; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | mt9v022->switch_gpio = gpio; |
| 210 | #else |
Guennadi Liakhovetski | b4333a3 | 2008-04-22 14:42:08 -0300 | [diff] [blame] | 211 | mt9v022->switch_gpio = -EINVAL; |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 212 | #endif |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static void bus_switch_release(struct mt9v022 *mt9v022) |
| 217 | { |
| 218 | #ifdef CONFIG_MT9V022_PCA9536_SWITCH |
Guennadi Liakhovetski | b4333a3 | 2008-04-22 14:42:08 -0300 | [diff] [blame] | 219 | if (gpio_is_valid(mt9v022->switch_gpio)) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 220 | gpio_free(mt9v022->switch_gpio); |
| 221 | #endif |
| 222 | } |
| 223 | |
| 224 | static int bus_switch_act(struct mt9v022 *mt9v022, int go8bit) |
| 225 | { |
| 226 | #ifdef CONFIG_MT9V022_PCA9536_SWITCH |
Guennadi Liakhovetski | b4333a3 | 2008-04-22 14:42:08 -0300 | [diff] [blame] | 227 | if (!gpio_is_valid(mt9v022->switch_gpio)) |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 228 | return -ENODEV; |
| 229 | |
| 230 | gpio_set_value_cansleep(mt9v022->switch_gpio, go8bit); |
| 231 | return 0; |
| 232 | #else |
| 233 | return -ENODEV; |
| 234 | #endif |
| 235 | } |
| 236 | |
| 237 | static int mt9v022_set_capture_format(struct soc_camera_device *icd, |
| 238 | __u32 pixfmt, struct v4l2_rect *rect, unsigned int flags) |
| 239 | { |
| 240 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 241 | unsigned int width_flag = flags & (IS_DATAWIDTH_10 | IS_DATAWIDTH_9 | |
| 242 | IS_DATAWIDTH_8); |
| 243 | u16 pixclk = 0; |
| 244 | int ret; |
| 245 | |
| 246 | /* Only one width bit may be set */ |
| 247 | if (!is_power_of_2(width_flag)) |
| 248 | return -EINVAL; |
| 249 | |
| 250 | /* The caller provides a supported format, as verified per call to |
| 251 | * icd->try_fmt_cap(), datawidth is from our supported format list */ |
| 252 | switch (pixfmt) { |
| 253 | case V4L2_PIX_FMT_GREY: |
| 254 | case V4L2_PIX_FMT_Y16: |
| 255 | if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM) |
| 256 | return -EINVAL; |
| 257 | break; |
| 258 | case V4L2_PIX_FMT_SBGGR8: |
| 259 | case V4L2_PIX_FMT_SBGGR16: |
| 260 | if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC) |
| 261 | return -EINVAL; |
| 262 | break; |
| 263 | case 0: |
| 264 | /* No format change, only geometry */ |
| 265 | break; |
| 266 | default: |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | /* Like in example app. Contradicts the datasheet though */ |
| 271 | ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE); |
| 272 | if (ret >= 0) { |
| 273 | if (ret & 1) /* Autoexposure */ |
| 274 | ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, |
| 275 | rect->height + icd->y_skip_top + 43); |
| 276 | else |
| 277 | ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH, |
| 278 | rect->height + icd->y_skip_top + 43); |
| 279 | } |
| 280 | /* Setup frame format: defaults apart from width and height */ |
| 281 | if (ret >= 0) |
| 282 | ret = reg_write(icd, MT9V022_COLUMN_START, rect->left); |
| 283 | if (ret >= 0) |
| 284 | ret = reg_write(icd, MT9V022_ROW_START, rect->top); |
| 285 | if (ret >= 0) |
| 286 | /* Default 94, Phytec driver says: |
| 287 | * "width + horizontal blank >= 660" */ |
| 288 | ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING, |
| 289 | rect->width > 660 - 43 ? 43 : |
| 290 | 660 - rect->width); |
| 291 | if (ret >= 0) |
| 292 | ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45); |
| 293 | if (ret >= 0) |
| 294 | ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width); |
| 295 | if (ret >= 0) |
| 296 | ret = reg_write(icd, MT9V022_WINDOW_HEIGHT, |
| 297 | rect->height + icd->y_skip_top); |
| 298 | |
| 299 | if (ret < 0) |
| 300 | return ret; |
| 301 | |
| 302 | dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height); |
| 303 | |
| 304 | if ((mt9v022->datawidth != 10 && (width_flag == IS_DATAWIDTH_10)) || |
| 305 | (mt9v022->datawidth != 9 && (width_flag == IS_DATAWIDTH_9)) || |
| 306 | (mt9v022->datawidth != 8 && (width_flag == IS_DATAWIDTH_8))) { |
Guennadi Liakhovetski | 7397bfbe | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 307 | /* Well, we actually only can do 10 or 8 bits... */ |
| 308 | if (width_flag == IS_DATAWIDTH_9) |
| 309 | return -EINVAL; |
| 310 | |
| 311 | ret = bus_switch_act(mt9v022, |
| 312 | width_flag == IS_DATAWIDTH_8); |
| 313 | if (ret < 0) |
| 314 | return ret; |
| 315 | |
| 316 | mt9v022->datawidth = width_flag == IS_DATAWIDTH_8 ? 8 : 10; |
| 317 | } |
| 318 | |
| 319 | if (flags & IS_PCLK_SAMPLE_RISING) |
| 320 | pixclk |= 0x10; |
| 321 | |
| 322 | if (!(flags & IS_HSYNC_ACTIVE_HIGH)) |
| 323 | pixclk |= 0x1; |
| 324 | |
| 325 | if (!(flags & IS_VSYNC_ACTIVE_HIGH)) |
| 326 | pixclk |= 0x2; |
| 327 | |
| 328 | ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk); |
| 329 | if (ret < 0) |
| 330 | return ret; |
| 331 | |
| 332 | if (!(flags & IS_MASTER)) |
| 333 | mt9v022->chip_control &= ~0x8; |
| 334 | |
| 335 | ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control); |
| 336 | if (ret < 0) |
| 337 | return ret; |
| 338 | |
| 339 | dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n", |
| 340 | pixclk, mt9v022->chip_control); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int mt9v022_try_fmt_cap(struct soc_camera_device *icd, |
| 346 | struct v4l2_format *f) |
| 347 | { |
| 348 | if (f->fmt.pix.height < 32 + icd->y_skip_top) |
| 349 | f->fmt.pix.height = 32 + icd->y_skip_top; |
| 350 | if (f->fmt.pix.height > 480 + icd->y_skip_top) |
| 351 | f->fmt.pix.height = 480 + icd->y_skip_top; |
| 352 | if (f->fmt.pix.width < 48) |
| 353 | f->fmt.pix.width = 48; |
| 354 | if (f->fmt.pix.width > 752) |
| 355 | f->fmt.pix.width = 752; |
| 356 | f->fmt.pix.width &= ~0x03; /* ? */ |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int mt9v022_get_chip_id(struct soc_camera_device *icd, |
| 362 | struct v4l2_chip_ident *id) |
| 363 | { |
| 364 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 365 | |
| 366 | if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR) |
| 367 | return -EINVAL; |
| 368 | |
| 369 | if (id->match_chip != mt9v022->client->addr) |
| 370 | return -ENODEV; |
| 371 | |
| 372 | id->ident = mt9v022->model; |
| 373 | id->revision = 0; |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 379 | static int mt9v022_get_register(struct soc_camera_device *icd, |
| 380 | struct v4l2_register *reg) |
| 381 | { |
| 382 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 383 | |
| 384 | if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
| 385 | return -EINVAL; |
| 386 | |
| 387 | if (reg->match_chip != mt9v022->client->addr) |
| 388 | return -ENODEV; |
| 389 | |
| 390 | reg->val = reg_read(icd, reg->reg); |
| 391 | |
| 392 | if (reg->val > 0xffff) |
| 393 | return -EIO; |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static int mt9v022_set_register(struct soc_camera_device *icd, |
| 399 | struct v4l2_register *reg) |
| 400 | { |
| 401 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 402 | |
| 403 | if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
| 404 | return -EINVAL; |
| 405 | |
| 406 | if (reg->match_chip != mt9v022->client->addr) |
| 407 | return -ENODEV; |
| 408 | |
| 409 | if (reg_write(icd, reg->reg, reg->val) < 0) |
| 410 | return -EIO; |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | #endif |
| 415 | |
| 416 | static unsigned int mt9v022_get_datawidth(struct soc_camera_device *icd) |
| 417 | { |
| 418 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 419 | return mt9v022->datawidth; |
| 420 | } |
| 421 | |
| 422 | const struct v4l2_queryctrl mt9v022_controls[] = { |
| 423 | { |
| 424 | .id = V4L2_CID_VFLIP, |
| 425 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 426 | .name = "Flip Vertically", |
| 427 | .minimum = 0, |
| 428 | .maximum = 1, |
| 429 | .step = 1, |
| 430 | .default_value = 0, |
| 431 | }, { |
| 432 | .id = V4L2_CID_HFLIP, |
| 433 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 434 | .name = "Flip Horizontally", |
| 435 | .minimum = 0, |
| 436 | .maximum = 1, |
| 437 | .step = 1, |
| 438 | .default_value = 0, |
| 439 | }, { |
| 440 | .id = V4L2_CID_GAIN, |
| 441 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 442 | .name = "Analog Gain", |
| 443 | .minimum = 64, |
| 444 | .maximum = 127, |
| 445 | .step = 1, |
| 446 | .default_value = 64, |
| 447 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 448 | }, { |
| 449 | .id = V4L2_CID_EXPOSURE, |
| 450 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 451 | .name = "Exposure", |
| 452 | .minimum = 1, |
| 453 | .maximum = 255, |
| 454 | .step = 1, |
| 455 | .default_value = 255, |
| 456 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 457 | }, { |
| 458 | .id = V4L2_CID_AUTOGAIN, |
| 459 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 460 | .name = "Automatic Gain", |
| 461 | .minimum = 0, |
| 462 | .maximum = 1, |
| 463 | .step = 1, |
| 464 | .default_value = 1, |
| 465 | }, { |
| 466 | .id = V4L2_CID_EXPOSURE_AUTO, |
| 467 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 468 | .name = "Automatic Exposure", |
| 469 | .minimum = 0, |
| 470 | .maximum = 1, |
| 471 | .step = 1, |
| 472 | .default_value = 1, |
| 473 | } |
| 474 | }; |
| 475 | |
| 476 | static int mt9v022_get_control(struct soc_camera_device *icd, |
| 477 | struct v4l2_control *ctrl); |
| 478 | static int mt9v022_set_control(struct soc_camera_device *icd, |
| 479 | struct v4l2_control *ctrl); |
| 480 | |
| 481 | static struct soc_camera_ops mt9v022_ops = { |
| 482 | .owner = THIS_MODULE, |
| 483 | .init = mt9v022_init, |
| 484 | .release = mt9v022_release, |
| 485 | .start_capture = mt9v022_start_capture, |
| 486 | .stop_capture = mt9v022_stop_capture, |
| 487 | .set_capture_format = mt9v022_set_capture_format, |
| 488 | .try_fmt_cap = mt9v022_try_fmt_cap, |
| 489 | .formats = mt9v022_formats, |
| 490 | .num_formats = ARRAY_SIZE(mt9v022_formats), |
| 491 | .get_datawidth = mt9v022_get_datawidth, |
| 492 | .controls = mt9v022_controls, |
| 493 | .num_controls = ARRAY_SIZE(mt9v022_controls), |
| 494 | .get_control = mt9v022_get_control, |
| 495 | .set_control = mt9v022_set_control, |
| 496 | .get_chip_id = mt9v022_get_chip_id, |
| 497 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 498 | .get_register = mt9v022_get_register, |
| 499 | .set_register = mt9v022_set_register, |
| 500 | #endif |
| 501 | }; |
| 502 | |
| 503 | static int mt9v022_get_control(struct soc_camera_device *icd, |
| 504 | struct v4l2_control *ctrl) |
| 505 | { |
| 506 | int data; |
| 507 | |
| 508 | switch (ctrl->id) { |
| 509 | case V4L2_CID_VFLIP: |
| 510 | data = reg_read(icd, MT9V022_READ_MODE); |
| 511 | if (data < 0) |
| 512 | return -EIO; |
| 513 | ctrl->value = !!(data & 0x10); |
| 514 | break; |
| 515 | case V4L2_CID_HFLIP: |
| 516 | data = reg_read(icd, MT9V022_READ_MODE); |
| 517 | if (data < 0) |
| 518 | return -EIO; |
| 519 | ctrl->value = !!(data & 0x20); |
| 520 | break; |
| 521 | case V4L2_CID_EXPOSURE_AUTO: |
| 522 | data = reg_read(icd, MT9V022_AEC_AGC_ENABLE); |
| 523 | if (data < 0) |
| 524 | return -EIO; |
| 525 | ctrl->value = !!(data & 0x1); |
| 526 | break; |
| 527 | case V4L2_CID_AUTOGAIN: |
| 528 | data = reg_read(icd, MT9V022_AEC_AGC_ENABLE); |
| 529 | if (data < 0) |
| 530 | return -EIO; |
| 531 | ctrl->value = !!(data & 0x2); |
| 532 | break; |
| 533 | } |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int mt9v022_set_control(struct soc_camera_device *icd, |
| 538 | struct v4l2_control *ctrl) |
| 539 | { |
| 540 | int data; |
| 541 | const struct v4l2_queryctrl *qctrl; |
| 542 | |
| 543 | qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id); |
| 544 | |
| 545 | if (!qctrl) |
| 546 | return -EINVAL; |
| 547 | |
| 548 | switch (ctrl->id) { |
| 549 | case V4L2_CID_VFLIP: |
| 550 | if (ctrl->value) |
| 551 | data = reg_set(icd, MT9V022_READ_MODE, 0x10); |
| 552 | else |
| 553 | data = reg_clear(icd, MT9V022_READ_MODE, 0x10); |
| 554 | if (data < 0) |
| 555 | return -EIO; |
| 556 | break; |
| 557 | case V4L2_CID_HFLIP: |
| 558 | if (ctrl->value) |
| 559 | data = reg_set(icd, MT9V022_READ_MODE, 0x20); |
| 560 | else |
| 561 | data = reg_clear(icd, MT9V022_READ_MODE, 0x20); |
| 562 | if (data < 0) |
| 563 | return -EIO; |
| 564 | break; |
| 565 | case V4L2_CID_GAIN: |
| 566 | /* mt9v022 has minimum == default */ |
| 567 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) |
| 568 | return -EINVAL; |
| 569 | else { |
| 570 | unsigned long range = qctrl->maximum - qctrl->minimum; |
| 571 | /* Datasheet says 16 to 64. autogain only works properly |
| 572 | * after setting gain to maximum 14. Larger values |
| 573 | * produce "white fly" noise effect. On the whole, |
| 574 | * manually setting analog gain does no good. */ |
| 575 | unsigned long gain = ((ctrl->value - qctrl->minimum) * |
| 576 | 10 + range / 2) / range + 4; |
| 577 | if (gain >= 32) |
| 578 | gain &= ~1; |
| 579 | /* The user wants to set gain manually, hope, she |
| 580 | * knows, what she's doing... Switch AGC off. */ |
| 581 | |
| 582 | if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0) |
| 583 | return -EIO; |
| 584 | |
| 585 | dev_info(&icd->dev, "Setting gain from %d to %lu\n", |
| 586 | reg_read(icd, MT9V022_ANALOG_GAIN), gain); |
| 587 | if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0) |
| 588 | return -EIO; |
| 589 | icd->gain = ctrl->value; |
| 590 | } |
| 591 | break; |
| 592 | case V4L2_CID_EXPOSURE: |
| 593 | /* mt9v022 has maximum == default */ |
| 594 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) |
| 595 | return -EINVAL; |
| 596 | else { |
| 597 | unsigned long range = qctrl->maximum - qctrl->minimum; |
| 598 | unsigned long shutter = ((ctrl->value - qctrl->minimum) * |
| 599 | 479 + range / 2) / range + 1; |
| 600 | /* The user wants to set shutter width manually, hope, |
| 601 | * she knows, what she's doing... Switch AEC off. */ |
| 602 | |
| 603 | if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0) |
| 604 | return -EIO; |
| 605 | |
| 606 | dev_dbg(&icd->dev, "Shutter width from %d to %lu\n", |
| 607 | reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH), |
| 608 | shutter); |
| 609 | if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH, |
| 610 | shutter) < 0) |
| 611 | return -EIO; |
| 612 | icd->exposure = ctrl->value; |
| 613 | } |
| 614 | break; |
| 615 | case V4L2_CID_AUTOGAIN: |
| 616 | if (ctrl->value) |
| 617 | data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2); |
| 618 | else |
| 619 | data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2); |
| 620 | if (data < 0) |
| 621 | return -EIO; |
| 622 | break; |
| 623 | case V4L2_CID_EXPOSURE_AUTO: |
| 624 | if (ctrl->value) |
| 625 | data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1); |
| 626 | else |
| 627 | data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1); |
| 628 | if (data < 0) |
| 629 | return -EIO; |
| 630 | break; |
| 631 | } |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | /* Interface active, can use i2c. If it fails, it can indeed mean, that |
| 636 | * this wasn't our capture interface, so, we wait for the right one */ |
| 637 | static int mt9v022_video_probe(struct soc_camera_device *icd) |
| 638 | { |
| 639 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 640 | s32 data; |
| 641 | int ret; |
| 642 | |
| 643 | if (!icd->dev.parent || |
| 644 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) |
| 645 | return -ENODEV; |
| 646 | |
| 647 | /* Read out the chip version register */ |
| 648 | data = reg_read(icd, MT9V022_CHIP_VERSION); |
| 649 | |
| 650 | /* must be 0x1311 or 0x1313 */ |
| 651 | if (data != 0x1311 && data != 0x1313) { |
| 652 | ret = -ENODEV; |
| 653 | dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n", |
| 654 | data); |
| 655 | goto ei2c; |
| 656 | } |
| 657 | |
| 658 | /* Soft reset */ |
| 659 | ret = reg_write(icd, MT9V022_RESET, 1); |
| 660 | if (ret < 0) |
| 661 | goto ei2c; |
| 662 | /* 15 clock cycles */ |
| 663 | udelay(200); |
| 664 | if (reg_read(icd, MT9V022_RESET)) { |
| 665 | dev_err(&icd->dev, "Resetting MT9V022 failed!\n"); |
| 666 | goto ei2c; |
| 667 | } |
| 668 | |
| 669 | /* Set monochrome or colour sensor type */ |
| 670 | if (sensor_type && (!strcmp("colour", sensor_type) || |
| 671 | !strcmp("color", sensor_type))) { |
| 672 | ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11); |
| 673 | mt9v022->model = V4L2_IDENT_MT9V022IX7ATC; |
| 674 | } else { |
| 675 | ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11); |
| 676 | mt9v022->model = V4L2_IDENT_MT9V022IX7ATM; |
| 677 | } |
| 678 | |
| 679 | if (ret >= 0) |
| 680 | ret = soc_camera_video_start(icd); |
| 681 | if (ret < 0) |
| 682 | goto eisis; |
| 683 | |
| 684 | dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n", |
| 685 | data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ? |
| 686 | "monochrome" : "colour"); |
| 687 | |
| 688 | return 0; |
| 689 | |
| 690 | eisis: |
| 691 | ei2c: |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | static void mt9v022_video_remove(struct soc_camera_device *icd) |
| 696 | { |
| 697 | struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd); |
| 698 | |
| 699 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr, |
| 700 | mt9v022->icd.dev.parent, mt9v022->icd.vdev); |
| 701 | soc_camera_video_stop(&mt9v022->icd); |
| 702 | } |
| 703 | |
| 704 | static int mt9v022_probe(struct i2c_client *client) |
| 705 | { |
| 706 | struct mt9v022 *mt9v022; |
| 707 | struct soc_camera_device *icd; |
| 708 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 709 | struct soc_camera_link *icl = client->dev.platform_data; |
| 710 | int ret; |
| 711 | |
| 712 | if (!icl) { |
| 713 | dev_err(&client->dev, "MT9V022 driver needs platform data\n"); |
| 714 | return -EINVAL; |
| 715 | } |
| 716 | |
| 717 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 718 | dev_warn(&adapter->dev, |
| 719 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 720 | return -EIO; |
| 721 | } |
| 722 | |
| 723 | mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL); |
| 724 | if (!mt9v022) |
| 725 | return -ENOMEM; |
| 726 | |
| 727 | mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT; |
| 728 | mt9v022->client = client; |
| 729 | i2c_set_clientdata(client, mt9v022); |
| 730 | |
| 731 | icd = &mt9v022->icd; |
| 732 | icd->probe = mt9v022_video_probe; |
| 733 | icd->remove = mt9v022_video_remove; |
| 734 | icd->ops = &mt9v022_ops; |
| 735 | icd->control = &client->dev; |
| 736 | icd->x_min = 1; |
| 737 | icd->y_min = 4; |
| 738 | icd->x_current = 1; |
| 739 | icd->y_current = 4; |
| 740 | icd->width_min = 48; |
| 741 | icd->width_max = 752; |
| 742 | icd->height_min = 32; |
| 743 | icd->height_max = 480; |
| 744 | icd->y_skip_top = 1; |
| 745 | icd->iface = icl->bus_id; |
| 746 | /* Default datawidth - this is the only width this camera (normally) |
| 747 | * supports. It is only with extra logic that it can support |
| 748 | * other widths. Therefore it seems to be a sensible default. */ |
| 749 | mt9v022->datawidth = 10; |
| 750 | |
| 751 | ret = bus_switch_request(mt9v022, icl); |
| 752 | if (ret) |
| 753 | goto eswinit; |
| 754 | |
| 755 | ret = soc_camera_device_register(icd); |
| 756 | if (ret) |
| 757 | goto eisdr; |
| 758 | |
| 759 | return 0; |
| 760 | |
| 761 | eisdr: |
| 762 | bus_switch_release(mt9v022); |
| 763 | eswinit: |
| 764 | kfree(mt9v022); |
| 765 | return ret; |
| 766 | } |
| 767 | |
| 768 | static int mt9v022_remove(struct i2c_client *client) |
| 769 | { |
| 770 | struct mt9v022 *mt9v022 = i2c_get_clientdata(client); |
| 771 | |
| 772 | soc_camera_device_unregister(&mt9v022->icd); |
| 773 | bus_switch_release(mt9v022); |
| 774 | kfree(mt9v022); |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | static struct i2c_driver mt9v022_i2c_driver = { |
| 780 | .driver = { |
| 781 | .name = "mt9v022", |
| 782 | }, |
| 783 | .probe = mt9v022_probe, |
| 784 | .remove = mt9v022_remove, |
| 785 | }; |
| 786 | |
| 787 | static int __init mt9v022_mod_init(void) |
| 788 | { |
| 789 | return i2c_add_driver(&mt9v022_i2c_driver); |
| 790 | } |
| 791 | |
| 792 | static void __exit mt9v022_mod_exit(void) |
| 793 | { |
| 794 | i2c_del_driver(&mt9v022_i2c_driver); |
| 795 | } |
| 796 | |
| 797 | module_init(mt9v022_mod_init); |
| 798 | module_exit(mt9v022_mod_exit); |
| 799 | |
| 800 | MODULE_DESCRIPTION("Micron MT9V022 Camera driver"); |
| 801 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); |
| 802 | MODULE_LICENSE("GPL"); |