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