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