Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for MT9M001 CMOS Image Sensor from Micron |
| 3 | * |
| 4 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/videodev2.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/log2.h> |
| 15 | |
| 16 | #include <media/v4l2-common.h> |
| 17 | #include <media/v4l2-chip-ident.h> |
| 18 | #include <media/soc_camera.h> |
| 19 | |
Guennadi Liakhovetski | ef6ad5c | 2008-04-22 14:42:08 -0300 | [diff] [blame] | 20 | #ifdef CONFIG_MT9M001_PCA9536_SWITCH |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 21 | #include <asm/gpio.h> |
Guennadi Liakhovetski | ef6ad5c | 2008-04-22 14:42:08 -0300 | [diff] [blame] | 22 | #endif |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 23 | |
| 24 | /* mt9m001 i2c address 0x5d |
| 25 | * The platform has to define i2c_board_info |
| 26 | * and call i2c_register_board_info() */ |
| 27 | |
| 28 | /* mt9m001 selected register addresses */ |
| 29 | #define MT9M001_CHIP_VERSION 0x00 |
| 30 | #define MT9M001_ROW_START 0x01 |
| 31 | #define MT9M001_COLUMN_START 0x02 |
| 32 | #define MT9M001_WINDOW_HEIGHT 0x03 |
| 33 | #define MT9M001_WINDOW_WIDTH 0x04 |
| 34 | #define MT9M001_HORIZONTAL_BLANKING 0x05 |
| 35 | #define MT9M001_VERTICAL_BLANKING 0x06 |
| 36 | #define MT9M001_OUTPUT_CONTROL 0x07 |
| 37 | #define MT9M001_SHUTTER_WIDTH 0x09 |
| 38 | #define MT9M001_FRAME_RESTART 0x0b |
| 39 | #define MT9M001_SHUTTER_DELAY 0x0c |
| 40 | #define MT9M001_RESET 0x0d |
| 41 | #define MT9M001_READ_OPTIONS1 0x1e |
| 42 | #define MT9M001_READ_OPTIONS2 0x20 |
| 43 | #define MT9M001_GLOBAL_GAIN 0x35 |
| 44 | #define MT9M001_CHIP_ENABLE 0xF1 |
| 45 | |
| 46 | static const struct soc_camera_data_format mt9m001_colour_formats[] = { |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 47 | /* Order important: first natively supported, |
| 48 | * second supported with a GPIO extender */ |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 49 | { |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 50 | .name = "Bayer (sRGB) 10 bit", |
| 51 | .depth = 10, |
| 52 | .fourcc = V4L2_PIX_FMT_SBGGR16, |
| 53 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 54 | }, { |
| 55 | .name = "Bayer (sRGB) 8 bit", |
| 56 | .depth = 8, |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 57 | .fourcc = V4L2_PIX_FMT_SBGGR8, |
| 58 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | static const struct soc_camera_data_format mt9m001_monochrome_formats[] = { |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 63 | /* Order important - see above */ |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 64 | { |
| 65 | .name = "Monochrome 10 bit", |
| 66 | .depth = 10, |
| 67 | .fourcc = V4L2_PIX_FMT_Y16, |
| 68 | }, { |
| 69 | .name = "Monochrome 8 bit", |
| 70 | .depth = 8, |
| 71 | .fourcc = V4L2_PIX_FMT_GREY, |
| 72 | }, |
| 73 | }; |
| 74 | |
| 75 | struct mt9m001 { |
| 76 | struct i2c_client *client; |
| 77 | struct soc_camera_device icd; |
| 78 | int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */ |
| 79 | int switch_gpio; |
| 80 | unsigned char autoexposure; |
| 81 | unsigned char datawidth; |
| 82 | }; |
| 83 | |
| 84 | static int reg_read(struct soc_camera_device *icd, const u8 reg) |
| 85 | { |
| 86 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 87 | struct i2c_client *client = mt9m001->client; |
| 88 | s32 data = i2c_smbus_read_word_data(client, reg); |
| 89 | return data < 0 ? data : swab16(data); |
| 90 | } |
| 91 | |
| 92 | static int reg_write(struct soc_camera_device *icd, const u8 reg, |
| 93 | const u16 data) |
| 94 | { |
| 95 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 96 | return i2c_smbus_write_word_data(mt9m001->client, reg, swab16(data)); |
| 97 | } |
| 98 | |
| 99 | static int reg_set(struct soc_camera_device *icd, const u8 reg, |
| 100 | const u16 data) |
| 101 | { |
| 102 | int ret; |
| 103 | |
| 104 | ret = reg_read(icd, reg); |
| 105 | if (ret < 0) |
| 106 | return ret; |
| 107 | return reg_write(icd, reg, ret | data); |
| 108 | } |
| 109 | |
| 110 | static int reg_clear(struct soc_camera_device *icd, const u8 reg, |
| 111 | const u16 data) |
| 112 | { |
| 113 | int ret; |
| 114 | |
| 115 | ret = reg_read(icd, reg); |
| 116 | if (ret < 0) |
| 117 | return ret; |
| 118 | return reg_write(icd, reg, ret & ~data); |
| 119 | } |
| 120 | |
| 121 | static int mt9m001_init(struct soc_camera_device *icd) |
| 122 | { |
| 123 | int ret; |
| 124 | |
| 125 | /* Disable chip, synchronous option update */ |
| 126 | dev_dbg(icd->vdev->dev, "%s\n", __FUNCTION__); |
| 127 | |
| 128 | ret = reg_write(icd, MT9M001_RESET, 1); |
| 129 | if (ret >= 0) |
| 130 | ret = reg_write(icd, MT9M001_RESET, 0); |
| 131 | if (ret >= 0) |
| 132 | ret = reg_write(icd, MT9M001_OUTPUT_CONTROL, 0); |
| 133 | |
| 134 | return ret >= 0 ? 0 : -EIO; |
| 135 | } |
| 136 | |
| 137 | static int mt9m001_release(struct soc_camera_device *icd) |
| 138 | { |
| 139 | /* Disable the chip */ |
| 140 | reg_write(icd, MT9M001_OUTPUT_CONTROL, 0); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int mt9m001_start_capture(struct soc_camera_device *icd) |
| 145 | { |
| 146 | /* Switch to master "normal" mode */ |
| 147 | if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 2) < 0) |
| 148 | return -EIO; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int mt9m001_stop_capture(struct soc_camera_device *icd) |
| 153 | { |
| 154 | /* Stop sensor readout */ |
| 155 | if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 0) < 0) |
| 156 | return -EIO; |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int bus_switch_request(struct mt9m001 *mt9m001, |
| 161 | struct soc_camera_link *icl) |
| 162 | { |
| 163 | #ifdef CONFIG_MT9M001_PCA9536_SWITCH |
| 164 | int ret; |
| 165 | unsigned int gpio = icl->gpio; |
| 166 | |
Guennadi Liakhovetski | b4333a3 | 2008-04-22 14:42:08 -0300 | [diff] [blame] | 167 | if (gpio_is_valid(gpio)) { |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 168 | /* We have a data bus switch. */ |
| 169 | ret = gpio_request(gpio, "mt9m001"); |
| 170 | if (ret < 0) { |
| 171 | dev_err(&mt9m001->client->dev, "Cannot get GPIO %u\n", |
| 172 | gpio); |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | ret = gpio_direction_output(gpio, 0); |
| 177 | if (ret < 0) { |
| 178 | dev_err(&mt9m001->client->dev, |
| 179 | "Cannot set GPIO %u to output\n", gpio); |
| 180 | gpio_free(gpio); |
| 181 | return ret; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | mt9m001->switch_gpio = gpio; |
| 186 | #else |
Guennadi Liakhovetski | b4333a3 | 2008-04-22 14:42:08 -0300 | [diff] [blame] | 187 | mt9m001->switch_gpio = -EINVAL; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 188 | #endif |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static void bus_switch_release(struct mt9m001 *mt9m001) |
| 193 | { |
| 194 | #ifdef CONFIG_MT9M001_PCA9536_SWITCH |
Guennadi Liakhovetski | b4333a3 | 2008-04-22 14:42:08 -0300 | [diff] [blame] | 195 | if (gpio_is_valid(mt9m001->switch_gpio)) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 196 | gpio_free(mt9m001->switch_gpio); |
| 197 | #endif |
| 198 | } |
| 199 | |
| 200 | static int bus_switch_act(struct mt9m001 *mt9m001, int go8bit) |
| 201 | { |
| 202 | #ifdef CONFIG_MT9M001_PCA9536_SWITCH |
Guennadi Liakhovetski | b4333a3 | 2008-04-22 14:42:08 -0300 | [diff] [blame] | 203 | if (!gpio_is_valid(mt9m001->switch_gpio)) |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 204 | return -ENODEV; |
| 205 | |
| 206 | gpio_set_value_cansleep(mt9m001->switch_gpio, go8bit); |
| 207 | return 0; |
| 208 | #else |
| 209 | return -ENODEV; |
| 210 | #endif |
| 211 | } |
| 212 | |
| 213 | static int mt9m001_set_capture_format(struct soc_camera_device *icd, |
| 214 | __u32 pixfmt, struct v4l2_rect *rect, unsigned int flags) |
| 215 | { |
| 216 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 217 | unsigned int width_flag = flags & (IS_DATAWIDTH_10 | IS_DATAWIDTH_9 | |
| 218 | IS_DATAWIDTH_8); |
| 219 | int ret; |
| 220 | const u16 hblank = 9, vblank = 25; |
| 221 | |
| 222 | /* MT9M001 has all capture_format parameters fixed */ |
| 223 | if (!(flags & IS_MASTER) || |
| 224 | !(flags & IS_PCLK_SAMPLE_RISING) || |
| 225 | !(flags & IS_HSYNC_ACTIVE_HIGH) || |
| 226 | !(flags & IS_VSYNC_ACTIVE_HIGH)) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | /* Only one width bit may be set */ |
| 230 | if (!is_power_of_2(width_flag)) |
| 231 | return -EINVAL; |
| 232 | |
| 233 | if ((mt9m001->datawidth != 10 && (width_flag == IS_DATAWIDTH_10)) || |
| 234 | (mt9m001->datawidth != 9 && (width_flag == IS_DATAWIDTH_9)) || |
| 235 | (mt9m001->datawidth != 8 && (width_flag == IS_DATAWIDTH_8))) { |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 236 | /* Well, we actually only can do 10 or 8 bits... */ |
| 237 | if (width_flag == IS_DATAWIDTH_9) |
| 238 | return -EINVAL; |
| 239 | ret = bus_switch_act(mt9m001, |
| 240 | width_flag == IS_DATAWIDTH_8); |
| 241 | if (ret < 0) |
| 242 | return ret; |
| 243 | |
| 244 | mt9m001->datawidth = width_flag == IS_DATAWIDTH_8 ? 8 : 10; |
| 245 | } |
| 246 | |
| 247 | /* Blanking and start values - default... */ |
| 248 | ret = reg_write(icd, MT9M001_HORIZONTAL_BLANKING, hblank); |
| 249 | if (ret >= 0) |
| 250 | ret = reg_write(icd, MT9M001_VERTICAL_BLANKING, vblank); |
| 251 | |
| 252 | /* The caller provides a supported format, as verified per |
| 253 | * call to icd->try_fmt_cap() */ |
| 254 | if (ret >= 0) |
| 255 | ret = reg_write(icd, MT9M001_COLUMN_START, rect->left); |
| 256 | if (ret >= 0) |
| 257 | ret = reg_write(icd, MT9M001_ROW_START, rect->top); |
| 258 | if (ret >= 0) |
| 259 | ret = reg_write(icd, MT9M001_WINDOW_WIDTH, rect->width - 1); |
| 260 | if (ret >= 0) |
| 261 | ret = reg_write(icd, MT9M001_WINDOW_HEIGHT, |
| 262 | rect->height + icd->y_skip_top - 1); |
| 263 | if (ret >= 0 && mt9m001->autoexposure) { |
| 264 | ret = reg_write(icd, MT9M001_SHUTTER_WIDTH, |
| 265 | rect->height + icd->y_skip_top + vblank); |
| 266 | if (ret >= 0) { |
| 267 | const struct v4l2_queryctrl *qctrl = |
| 268 | soc_camera_find_qctrl(icd->ops, |
| 269 | V4L2_CID_EXPOSURE); |
| 270 | icd->exposure = (524 + (rect->height + icd->y_skip_top + |
| 271 | vblank - 1) * |
| 272 | (qctrl->maximum - qctrl->minimum)) / |
| 273 | 1048 + qctrl->minimum; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return ret < 0 ? ret : 0; |
| 278 | } |
| 279 | |
| 280 | static int mt9m001_try_fmt_cap(struct soc_camera_device *icd, |
| 281 | struct v4l2_format *f) |
| 282 | { |
| 283 | if (f->fmt.pix.height < 32 + icd->y_skip_top) |
| 284 | f->fmt.pix.height = 32 + icd->y_skip_top; |
| 285 | if (f->fmt.pix.height > 1024 + icd->y_skip_top) |
| 286 | f->fmt.pix.height = 1024 + icd->y_skip_top; |
| 287 | if (f->fmt.pix.width < 48) |
| 288 | f->fmt.pix.width = 48; |
| 289 | if (f->fmt.pix.width > 1280) |
| 290 | f->fmt.pix.width = 1280; |
| 291 | f->fmt.pix.width &= ~0x01; /* has to be even, unsure why was ~3 */ |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static int mt9m001_get_chip_id(struct soc_camera_device *icd, |
| 297 | struct v4l2_chip_ident *id) |
| 298 | { |
| 299 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 300 | |
| 301 | if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR) |
| 302 | return -EINVAL; |
| 303 | |
| 304 | if (id->match_chip != mt9m001->client->addr) |
| 305 | return -ENODEV; |
| 306 | |
| 307 | id->ident = mt9m001->model; |
| 308 | id->revision = 0; |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 314 | static int mt9m001_get_register(struct soc_camera_device *icd, |
| 315 | struct v4l2_register *reg) |
| 316 | { |
| 317 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 318 | |
| 319 | if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
| 320 | return -EINVAL; |
| 321 | |
| 322 | if (reg->match_chip != mt9m001->client->addr) |
| 323 | return -ENODEV; |
| 324 | |
| 325 | reg->val = reg_read(icd, reg->reg); |
| 326 | |
| 327 | if (reg->val > 0xffff) |
| 328 | return -EIO; |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static int mt9m001_set_register(struct soc_camera_device *icd, |
| 334 | struct v4l2_register *reg) |
| 335 | { |
| 336 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 337 | |
| 338 | if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
| 339 | return -EINVAL; |
| 340 | |
| 341 | if (reg->match_chip != mt9m001->client->addr) |
| 342 | return -ENODEV; |
| 343 | |
| 344 | if (reg_write(icd, reg->reg, reg->val) < 0) |
| 345 | return -EIO; |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | #endif |
| 350 | |
| 351 | static unsigned int mt9m001_get_datawidth(struct soc_camera_device *icd) |
| 352 | { |
| 353 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 354 | return mt9m001->datawidth; |
| 355 | } |
| 356 | |
| 357 | const struct v4l2_queryctrl mt9m001_controls[] = { |
| 358 | { |
| 359 | .id = V4L2_CID_VFLIP, |
| 360 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 361 | .name = "Flip Vertically", |
| 362 | .minimum = 0, |
| 363 | .maximum = 1, |
| 364 | .step = 1, |
| 365 | .default_value = 0, |
| 366 | }, { |
| 367 | .id = V4L2_CID_GAIN, |
| 368 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 369 | .name = "Gain", |
| 370 | .minimum = 0, |
| 371 | .maximum = 127, |
| 372 | .step = 1, |
| 373 | .default_value = 64, |
| 374 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 375 | }, { |
| 376 | .id = V4L2_CID_EXPOSURE, |
| 377 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 378 | .name = "Exposure", |
| 379 | .minimum = 1, |
| 380 | .maximum = 255, |
| 381 | .step = 1, |
| 382 | .default_value = 255, |
| 383 | .flags = V4L2_CTRL_FLAG_SLIDER, |
| 384 | }, { |
| 385 | .id = V4L2_CID_EXPOSURE_AUTO, |
| 386 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 387 | .name = "Automatic Exposure", |
| 388 | .minimum = 0, |
| 389 | .maximum = 1, |
| 390 | .step = 1, |
| 391 | .default_value = 1, |
| 392 | } |
| 393 | }; |
| 394 | |
| 395 | static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl); |
| 396 | static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl); |
| 397 | |
| 398 | static struct soc_camera_ops mt9m001_ops = { |
| 399 | .owner = THIS_MODULE, |
| 400 | .init = mt9m001_init, |
| 401 | .release = mt9m001_release, |
| 402 | .start_capture = mt9m001_start_capture, |
| 403 | .stop_capture = mt9m001_stop_capture, |
| 404 | .set_capture_format = mt9m001_set_capture_format, |
| 405 | .try_fmt_cap = mt9m001_try_fmt_cap, |
| 406 | .formats = NULL, /* Filled in later depending on the */ |
| 407 | .num_formats = 0, /* camera type and data widths */ |
| 408 | .get_datawidth = mt9m001_get_datawidth, |
| 409 | .controls = mt9m001_controls, |
| 410 | .num_controls = ARRAY_SIZE(mt9m001_controls), |
| 411 | .get_control = mt9m001_get_control, |
| 412 | .set_control = mt9m001_set_control, |
| 413 | .get_chip_id = mt9m001_get_chip_id, |
| 414 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 415 | .get_register = mt9m001_get_register, |
| 416 | .set_register = mt9m001_set_register, |
| 417 | #endif |
| 418 | }; |
| 419 | |
| 420 | static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl) |
| 421 | { |
| 422 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 423 | int data; |
| 424 | |
| 425 | switch (ctrl->id) { |
| 426 | case V4L2_CID_VFLIP: |
| 427 | data = reg_read(icd, MT9M001_READ_OPTIONS2); |
| 428 | if (data < 0) |
| 429 | return -EIO; |
| 430 | ctrl->value = !!(data & 0x8000); |
| 431 | break; |
| 432 | case V4L2_CID_EXPOSURE_AUTO: |
| 433 | ctrl->value = mt9m001->autoexposure; |
| 434 | break; |
| 435 | } |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl) |
| 440 | { |
| 441 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 442 | const struct v4l2_queryctrl *qctrl; |
| 443 | int data; |
| 444 | |
| 445 | qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id); |
| 446 | |
| 447 | if (!qctrl) |
| 448 | return -EINVAL; |
| 449 | |
| 450 | switch (ctrl->id) { |
| 451 | case V4L2_CID_VFLIP: |
| 452 | if (ctrl->value) |
| 453 | data = reg_set(icd, MT9M001_READ_OPTIONS2, 0x8000); |
| 454 | else |
| 455 | data = reg_clear(icd, MT9M001_READ_OPTIONS2, 0x8000); |
| 456 | if (data < 0) |
| 457 | return -EIO; |
| 458 | break; |
| 459 | case V4L2_CID_GAIN: |
| 460 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) |
| 461 | return -EINVAL; |
| 462 | /* See Datasheet Table 7, Gain settings. */ |
| 463 | if (ctrl->value <= qctrl->default_value) { |
| 464 | /* Pack it into 0..1 step 0.125, register values 0..8 */ |
| 465 | unsigned long range = qctrl->default_value - qctrl->minimum; |
| 466 | data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range; |
| 467 | |
| 468 | dev_dbg(&icd->dev, "Setting gain %d\n", data); |
| 469 | data = reg_write(icd, MT9M001_GLOBAL_GAIN, data); |
| 470 | if (data < 0) |
| 471 | return -EIO; |
| 472 | } else { |
| 473 | /* Pack it into 1.125..15 variable step, register values 9..67 */ |
| 474 | /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ |
| 475 | unsigned long range = qctrl->maximum - qctrl->default_value - 1; |
| 476 | unsigned long gain = ((ctrl->value - qctrl->default_value - 1) * |
| 477 | 111 + range / 2) / range + 9; |
| 478 | |
| 479 | if (gain <= 32) |
| 480 | data = gain; |
| 481 | else if (gain <= 64) |
| 482 | data = ((gain - 32) * 16 + 16) / 32 + 80; |
| 483 | else |
| 484 | data = ((gain - 64) * 7 + 28) / 56 + 96; |
| 485 | |
| 486 | dev_dbg(&icd->dev, "Setting gain from %d to %d\n", |
| 487 | reg_read(icd, MT9M001_GLOBAL_GAIN), data); |
| 488 | data = reg_write(icd, MT9M001_GLOBAL_GAIN, data); |
| 489 | if (data < 0) |
| 490 | return -EIO; |
| 491 | } |
| 492 | |
| 493 | /* Success */ |
| 494 | icd->gain = ctrl->value; |
| 495 | break; |
| 496 | case V4L2_CID_EXPOSURE: |
| 497 | /* mt9m001 has maximum == default */ |
| 498 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) |
| 499 | return -EINVAL; |
| 500 | else { |
| 501 | unsigned long range = qctrl->maximum - qctrl->minimum; |
| 502 | unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 + |
| 503 | range / 2) / range + 1; |
| 504 | |
| 505 | dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n", |
| 506 | reg_read(icd, MT9M001_SHUTTER_WIDTH), shutter); |
| 507 | if (reg_write(icd, MT9M001_SHUTTER_WIDTH, shutter) < 0) |
| 508 | return -EIO; |
| 509 | icd->exposure = ctrl->value; |
| 510 | mt9m001->autoexposure = 0; |
| 511 | } |
| 512 | break; |
| 513 | case V4L2_CID_EXPOSURE_AUTO: |
| 514 | if (ctrl->value) { |
| 515 | const u16 vblank = 25; |
| 516 | if (reg_write(icd, MT9M001_SHUTTER_WIDTH, icd->height + |
| 517 | icd->y_skip_top + vblank) < 0) |
| 518 | return -EIO; |
| 519 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); |
| 520 | icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) * |
| 521 | (qctrl->maximum - qctrl->minimum)) / |
| 522 | 1048 + qctrl->minimum; |
| 523 | mt9m001->autoexposure = 1; |
| 524 | } else |
| 525 | mt9m001->autoexposure = 0; |
| 526 | break; |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | /* Interface active, can use i2c. If it fails, it can indeed mean, that |
| 532 | * this wasn't our capture interface, so, we wait for the right one */ |
| 533 | static int mt9m001_video_probe(struct soc_camera_device *icd) |
| 534 | { |
| 535 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 536 | s32 data; |
| 537 | int ret; |
| 538 | |
| 539 | /* We must have a parent by now. And it cannot be a wrong one. |
| 540 | * So this entire test is completely redundant. */ |
| 541 | if (!icd->dev.parent || |
| 542 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) |
| 543 | return -ENODEV; |
| 544 | |
| 545 | /* Enable the chip */ |
| 546 | data = reg_write(&mt9m001->icd, MT9M001_CHIP_ENABLE, 1); |
| 547 | dev_dbg(&icd->dev, "write: %d\n", data); |
| 548 | |
| 549 | /* Read out the chip version register */ |
| 550 | data = reg_read(icd, MT9M001_CHIP_VERSION); |
| 551 | |
| 552 | /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */ |
| 553 | switch (data) { |
| 554 | case 0x8411: |
| 555 | case 0x8421: |
| 556 | mt9m001->model = V4L2_IDENT_MT9M001C12ST; |
| 557 | mt9m001_ops.formats = mt9m001_colour_formats; |
Guennadi Liakhovetski | bb55de3 | 2008-04-22 14:45:13 -0300 | [diff] [blame] | 558 | if (mt9m001->client->dev.platform_data) |
| 559 | mt9m001_ops.num_formats = ARRAY_SIZE(mt9m001_colour_formats); |
| 560 | else |
| 561 | mt9m001_ops.num_formats = 1; |
Guennadi Liakhovetski | f523dd0d | 2008-04-22 14:42:04 -0300 | [diff] [blame] | 562 | break; |
| 563 | case 0x8431: |
| 564 | mt9m001->model = V4L2_IDENT_MT9M001C12STM; |
| 565 | mt9m001_ops.formats = mt9m001_monochrome_formats; |
| 566 | if (mt9m001->client->dev.platform_data) |
| 567 | mt9m001_ops.num_formats = ARRAY_SIZE(mt9m001_monochrome_formats); |
| 568 | else |
| 569 | mt9m001_ops.num_formats = 1; |
| 570 | break; |
| 571 | default: |
| 572 | ret = -ENODEV; |
| 573 | dev_err(&icd->dev, |
| 574 | "No MT9M001 chip detected, register read %x\n", data); |
| 575 | goto ei2c; |
| 576 | } |
| 577 | |
| 578 | dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data, |
| 579 | data == 0x8431 ? "C12STM" : "C12ST"); |
| 580 | |
| 581 | /* Now that we know the model, we can start video */ |
| 582 | ret = soc_camera_video_start(icd); |
| 583 | if (ret) |
| 584 | goto eisis; |
| 585 | |
| 586 | return 0; |
| 587 | |
| 588 | eisis: |
| 589 | ei2c: |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | static void mt9m001_video_remove(struct soc_camera_device *icd) |
| 594 | { |
| 595 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
| 596 | |
| 597 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr, |
| 598 | mt9m001->icd.dev.parent, mt9m001->icd.vdev); |
| 599 | soc_camera_video_stop(&mt9m001->icd); |
| 600 | } |
| 601 | |
| 602 | static int mt9m001_probe(struct i2c_client *client) |
| 603 | { |
| 604 | struct mt9m001 *mt9m001; |
| 605 | struct soc_camera_device *icd; |
| 606 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 607 | struct soc_camera_link *icl = client->dev.platform_data; |
| 608 | int ret; |
| 609 | |
| 610 | if (!icl) { |
| 611 | dev_err(&client->dev, "MT9M001 driver needs platform data\n"); |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | |
| 615 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 616 | dev_warn(&adapter->dev, |
| 617 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 618 | return -EIO; |
| 619 | } |
| 620 | |
| 621 | mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL); |
| 622 | if (!mt9m001) |
| 623 | return -ENOMEM; |
| 624 | |
| 625 | mt9m001->client = client; |
| 626 | i2c_set_clientdata(client, mt9m001); |
| 627 | |
| 628 | /* Second stage probe - when a capture adapter is there */ |
| 629 | icd = &mt9m001->icd; |
| 630 | icd->probe = mt9m001_video_probe; |
| 631 | icd->remove = mt9m001_video_remove; |
| 632 | icd->ops = &mt9m001_ops; |
| 633 | icd->control = &client->dev; |
| 634 | icd->x_min = 20; |
| 635 | icd->y_min = 12; |
| 636 | icd->x_current = 20; |
| 637 | icd->y_current = 12; |
| 638 | icd->width_min = 48; |
| 639 | icd->width_max = 1280; |
| 640 | icd->height_min = 32; |
| 641 | icd->height_max = 1024; |
| 642 | icd->y_skip_top = 1; |
| 643 | icd->iface = icl->bus_id; |
| 644 | /* Default datawidth - this is the only width this camera (normally) |
| 645 | * supports. It is only with extra logic that it can support |
| 646 | * other widths. Therefore it seems to be a sensible default. */ |
| 647 | mt9m001->datawidth = 10; |
| 648 | /* Simulated autoexposure. If enabled, we calculate shutter width |
| 649 | * ourselves in the driver based on vertical blanking and frame width */ |
| 650 | mt9m001->autoexposure = 1; |
| 651 | |
| 652 | ret = bus_switch_request(mt9m001, icl); |
| 653 | if (ret) |
| 654 | goto eswinit; |
| 655 | |
| 656 | ret = soc_camera_device_register(icd); |
| 657 | if (ret) |
| 658 | goto eisdr; |
| 659 | |
| 660 | return 0; |
| 661 | |
| 662 | eisdr: |
| 663 | bus_switch_release(mt9m001); |
| 664 | eswinit: |
| 665 | kfree(mt9m001); |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | static int mt9m001_remove(struct i2c_client *client) |
| 670 | { |
| 671 | struct mt9m001 *mt9m001 = i2c_get_clientdata(client); |
| 672 | |
| 673 | soc_camera_device_unregister(&mt9m001->icd); |
| 674 | bus_switch_release(mt9m001); |
| 675 | kfree(mt9m001); |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | static struct i2c_driver mt9m001_i2c_driver = { |
| 681 | .driver = { |
| 682 | .name = "mt9m001", |
| 683 | }, |
| 684 | .probe = mt9m001_probe, |
| 685 | .remove = mt9m001_remove, |
| 686 | }; |
| 687 | |
| 688 | static int __init mt9m001_mod_init(void) |
| 689 | { |
| 690 | return i2c_add_driver(&mt9m001_i2c_driver); |
| 691 | } |
| 692 | |
| 693 | static void __exit mt9m001_mod_exit(void) |
| 694 | { |
| 695 | i2c_del_driver(&mt9m001_i2c_driver); |
| 696 | } |
| 697 | |
| 698 | module_init(mt9m001_mod_init); |
| 699 | module_exit(mt9m001_mod_exit); |
| 700 | |
| 701 | MODULE_DESCRIPTION("Micron MT9M001 Camera driver"); |
| 702 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); |
| 703 | MODULE_LICENSE("GPL"); |