Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 1 | /* |
| 2 | * mt9v011 -Micron 1/4-Inch VGA Digital Image Sensor |
| 3 | * |
| 4 | * Copyright (c) 2009 Mauro Carvalho Chehab (mchehab@redhat.com) |
| 5 | * This code is placed under the terms of the GNU General Public License v2 |
| 6 | */ |
| 7 | |
| 8 | #include <linux/i2c.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 10 | #include <linux/videodev2.h> |
| 11 | #include <linux/delay.h> |
Mauro Carvalho Chehab | e11206e | 2009-07-14 02:38:18 -0300 | [diff] [blame] | 12 | #include <asm/div64.h> |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 13 | #include <media/v4l2-device.h> |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 14 | #include <media/v4l2-chip-ident.h> |
Hans Verkuil | 3c7c937 | 2011-01-08 07:08:02 -0300 | [diff] [blame] | 15 | #include <media/mt9v011.h> |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 16 | |
| 17 | MODULE_DESCRIPTION("Micron mt9v011 sensor driver"); |
| 18 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 19 | MODULE_LICENSE("GPL"); |
| 20 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 21 | static int debug; |
| 22 | module_param(debug, int, 0); |
| 23 | MODULE_PARM_DESC(debug, "Debug level (0-2)"); |
| 24 | |
Hans Verkuil | 3c7c937 | 2011-01-08 07:08:02 -0300 | [diff] [blame] | 25 | #define R00_MT9V011_CHIP_VERSION 0x00 |
| 26 | #define R01_MT9V011_ROWSTART 0x01 |
| 27 | #define R02_MT9V011_COLSTART 0x02 |
| 28 | #define R03_MT9V011_HEIGHT 0x03 |
| 29 | #define R04_MT9V011_WIDTH 0x04 |
| 30 | #define R05_MT9V011_HBLANK 0x05 |
| 31 | #define R06_MT9V011_VBLANK 0x06 |
| 32 | #define R07_MT9V011_OUT_CTRL 0x07 |
| 33 | #define R09_MT9V011_SHUTTER_WIDTH 0x09 |
| 34 | #define R0A_MT9V011_CLK_SPEED 0x0a |
| 35 | #define R0B_MT9V011_RESTART 0x0b |
| 36 | #define R0C_MT9V011_SHUTTER_DELAY 0x0c |
| 37 | #define R0D_MT9V011_RESET 0x0d |
| 38 | #define R1E_MT9V011_DIGITAL_ZOOM 0x1e |
| 39 | #define R20_MT9V011_READ_MODE 0x20 |
| 40 | #define R2B_MT9V011_GREEN_1_GAIN 0x2b |
| 41 | #define R2C_MT9V011_BLUE_GAIN 0x2c |
| 42 | #define R2D_MT9V011_RED_GAIN 0x2d |
| 43 | #define R2E_MT9V011_GREEN_2_GAIN 0x2e |
| 44 | #define R35_MT9V011_GLOBAL_GAIN 0x35 |
| 45 | #define RF1_MT9V011_CHIP_ENABLE 0xf1 |
| 46 | |
| 47 | #define MT9V011_VERSION 0x8232 |
| 48 | #define MT9V011_REV_B_VERSION 0x8243 |
| 49 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 50 | /* supported controls */ |
| 51 | static struct v4l2_queryctrl mt9v011_qctrl[] = { |
| 52 | { |
| 53 | .id = V4L2_CID_GAIN, |
| 54 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 55 | .name = "Gain", |
| 56 | .minimum = 0, |
| 57 | .maximum = (1 << 10) - 1, |
| 58 | .step = 1, |
| 59 | .default_value = 0x0020, |
| 60 | .flags = 0, |
| 61 | }, { |
Johannes Obermaier | 590929f | 2011-06-02 12:43:14 -0300 | [diff] [blame^] | 62 | .id = V4L2_CID_EXPOSURE, |
| 63 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 64 | .name = "Exposure", |
| 65 | .minimum = 0, |
| 66 | .maximum = 2047, |
| 67 | .step = 1, |
| 68 | .default_value = 0x01fc, |
| 69 | .flags = 0, |
| 70 | }, { |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 71 | .id = V4L2_CID_RED_BALANCE, |
| 72 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 73 | .name = "Red Balance", |
| 74 | .minimum = -1 << 9, |
| 75 | .maximum = (1 << 9) - 1, |
| 76 | .step = 1, |
| 77 | .default_value = 0, |
| 78 | .flags = 0, |
| 79 | }, { |
| 80 | .id = V4L2_CID_BLUE_BALANCE, |
| 81 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 82 | .name = "Blue Balance", |
| 83 | .minimum = -1 << 9, |
| 84 | .maximum = (1 << 9) - 1, |
| 85 | .step = 1, |
| 86 | .default_value = 0, |
| 87 | .flags = 0, |
Mauro Carvalho Chehab | 2526ea6 | 2009-08-07 01:09:54 -0300 | [diff] [blame] | 88 | }, { |
| 89 | .id = V4L2_CID_HFLIP, |
| 90 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 91 | .name = "Mirror", |
| 92 | .minimum = 0, |
| 93 | .maximum = 1, |
| 94 | .step = 1, |
| 95 | .default_value = 0, |
| 96 | .flags = 0, |
| 97 | }, { |
| 98 | .id = V4L2_CID_VFLIP, |
| 99 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 100 | .name = "Vflip", |
| 101 | .minimum = 0, |
| 102 | .maximum = 1, |
| 103 | .step = 1, |
| 104 | .default_value = 0, |
| 105 | .flags = 0, |
| 106 | }, { |
| 107 | } |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct mt9v011 { |
| 111 | struct v4l2_subdev sd; |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 112 | unsigned width, height; |
Mauro Carvalho Chehab | e11206e | 2009-07-14 02:38:18 -0300 | [diff] [blame] | 113 | unsigned xtal; |
Mauro Carvalho Chehab | 2526ea6 | 2009-08-07 01:09:54 -0300 | [diff] [blame] | 114 | unsigned hflip:1; |
| 115 | unsigned vflip:1; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 116 | |
Johannes Obermaier | 590929f | 2011-06-02 12:43:14 -0300 | [diff] [blame^] | 117 | u16 global_gain, exposure, red_bal, blue_bal; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd) |
| 121 | { |
| 122 | return container_of(sd, struct mt9v011, sd); |
| 123 | } |
| 124 | |
| 125 | static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr) |
| 126 | { |
| 127 | struct i2c_client *c = v4l2_get_subdevdata(sd); |
| 128 | __be16 buffer; |
| 129 | int rc, val; |
| 130 | |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame] | 131 | rc = i2c_master_send(c, &addr, 1); |
| 132 | if (rc != 1) |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 133 | v4l2_dbg(0, debug, sd, |
| 134 | "i2c i/o error: rc == %d (should be 1)\n", rc); |
| 135 | |
| 136 | msleep(10); |
| 137 | |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame] | 138 | rc = i2c_master_recv(c, (char *)&buffer, 2); |
| 139 | if (rc != 2) |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 140 | v4l2_dbg(0, debug, sd, |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame] | 141 | "i2c i/o error: rc == %d (should be 2)\n", rc); |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 142 | |
| 143 | val = be16_to_cpu(buffer); |
| 144 | |
| 145 | v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val); |
| 146 | |
| 147 | return val; |
| 148 | } |
| 149 | |
| 150 | static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr, |
| 151 | u16 value) |
| 152 | { |
| 153 | struct i2c_client *c = v4l2_get_subdevdata(sd); |
| 154 | unsigned char buffer[3]; |
| 155 | int rc; |
| 156 | |
| 157 | buffer[0] = addr; |
| 158 | buffer[1] = value >> 8; |
| 159 | buffer[2] = value & 0xff; |
| 160 | |
| 161 | v4l2_dbg(2, debug, sd, |
| 162 | "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value); |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 163 | rc = i2c_master_send(c, buffer, 3); |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame] | 164 | if (rc != 3) |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 165 | v4l2_dbg(0, debug, sd, |
| 166 | "i2c i/o error: rc == %d (should be 3)\n", rc); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | struct i2c_reg_value { |
| 171 | unsigned char reg; |
| 172 | u16 value; |
| 173 | }; |
| 174 | |
| 175 | /* |
| 176 | * Values used at the original driver |
| 177 | * Some values are marked as Reserved at the datasheet |
| 178 | */ |
| 179 | static const struct i2c_reg_value mt9v011_init_default[] = { |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 180 | { R0D_MT9V011_RESET, 0x0001 }, |
| 181 | { R0D_MT9V011_RESET, 0x0000 }, |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 182 | |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 183 | { R0C_MT9V011_SHUTTER_DELAY, 0x0000 }, |
Mauro Carvalho Chehab | 6934e6f | 2009-07-04 08:15:11 -0300 | [diff] [blame] | 184 | { R09_MT9V011_SHUTTER_WIDTH, 0x1fc }, |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 185 | |
Mauro Carvalho Chehab | 6934e6f | 2009-07-04 08:15:11 -0300 | [diff] [blame] | 186 | { R0A_MT9V011_CLK_SPEED, 0x0000 }, |
| 187 | { R1E_MT9V011_DIGITAL_ZOOM, 0x0000 }, |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 188 | |
Mauro Carvalho Chehab | e11206e | 2009-07-14 02:38:18 -0300 | [diff] [blame] | 189 | { R07_MT9V011_OUT_CTRL, 0x0002 }, /* chip enable */ |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | static void set_balance(struct v4l2_subdev *sd) |
| 193 | { |
| 194 | struct mt9v011 *core = to_mt9v011(sd); |
| 195 | u16 green1_gain, green2_gain, blue_gain, red_gain; |
Johannes Obermaier | 590929f | 2011-06-02 12:43:14 -0300 | [diff] [blame^] | 196 | u16 exposure; |
| 197 | |
| 198 | exposure = core->exposure; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 199 | |
| 200 | green1_gain = core->global_gain; |
| 201 | green2_gain = core->global_gain; |
| 202 | |
| 203 | blue_gain = core->global_gain + |
| 204 | core->global_gain * core->blue_bal / (1 << 9); |
| 205 | |
| 206 | red_gain = core->global_gain + |
| 207 | core->global_gain * core->blue_bal / (1 << 9); |
| 208 | |
| 209 | mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green1_gain); |
| 210 | mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green1_gain); |
| 211 | mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain); |
| 212 | mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain); |
Johannes Obermaier | 590929f | 2011-06-02 12:43:14 -0300 | [diff] [blame^] | 213 | mt9v011_write(sd, R09_MT9V011_SHUTTER_WIDTH, exposure); |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 214 | } |
| 215 | |
Mauro Carvalho Chehab | 83053f7 | 2009-08-06 21:03:35 -0300 | [diff] [blame] | 216 | static void calc_fps(struct v4l2_subdev *sd, u32 *numerator, u32 *denominator) |
Mauro Carvalho Chehab | e11206e | 2009-07-14 02:38:18 -0300 | [diff] [blame] | 217 | { |
| 218 | struct mt9v011 *core = to_mt9v011(sd); |
| 219 | unsigned height, width, hblank, vblank, speed; |
| 220 | unsigned row_time, t_time; |
| 221 | u64 frames_per_ms; |
| 222 | unsigned tmp; |
| 223 | |
| 224 | height = mt9v011_read(sd, R03_MT9V011_HEIGHT); |
| 225 | width = mt9v011_read(sd, R04_MT9V011_WIDTH); |
| 226 | hblank = mt9v011_read(sd, R05_MT9V011_HBLANK); |
| 227 | vblank = mt9v011_read(sd, R06_MT9V011_VBLANK); |
| 228 | speed = mt9v011_read(sd, R0A_MT9V011_CLK_SPEED); |
| 229 | |
| 230 | row_time = (width + 113 + hblank) * (speed + 2); |
| 231 | t_time = row_time * (height + vblank + 1); |
| 232 | |
| 233 | frames_per_ms = core->xtal * 1000l; |
| 234 | do_div(frames_per_ms, t_time); |
| 235 | tmp = frames_per_ms; |
| 236 | |
| 237 | v4l2_dbg(1, debug, sd, "Programmed to %u.%03u fps (%d pixel clcks)\n", |
| 238 | tmp / 1000, tmp % 1000, t_time); |
Mauro Carvalho Chehab | 83053f7 | 2009-08-06 21:03:35 -0300 | [diff] [blame] | 239 | |
| 240 | if (numerator && denominator) { |
| 241 | *numerator = 1000; |
| 242 | *denominator = (u32)frames_per_ms; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static u16 calc_speed(struct v4l2_subdev *sd, u32 numerator, u32 denominator) |
| 247 | { |
| 248 | struct mt9v011 *core = to_mt9v011(sd); |
| 249 | unsigned height, width, hblank, vblank; |
| 250 | unsigned row_time, line_time; |
| 251 | u64 t_time, speed; |
| 252 | |
| 253 | /* Avoid bogus calculus */ |
| 254 | if (!numerator || !denominator) |
| 255 | return 0; |
| 256 | |
| 257 | height = mt9v011_read(sd, R03_MT9V011_HEIGHT); |
| 258 | width = mt9v011_read(sd, R04_MT9V011_WIDTH); |
| 259 | hblank = mt9v011_read(sd, R05_MT9V011_HBLANK); |
| 260 | vblank = mt9v011_read(sd, R06_MT9V011_VBLANK); |
| 261 | |
| 262 | row_time = width + 113 + hblank; |
| 263 | line_time = height + vblank + 1; |
| 264 | |
| 265 | t_time = core->xtal * ((u64)numerator); |
| 266 | /* round to the closest value */ |
| 267 | t_time += denominator / 2; |
| 268 | do_div(t_time, denominator); |
| 269 | |
| 270 | speed = t_time; |
| 271 | do_div(speed, row_time * line_time); |
| 272 | |
| 273 | /* Avoid having a negative value for speed */ |
| 274 | if (speed < 2) |
| 275 | speed = 0; |
| 276 | else |
| 277 | speed -= 2; |
| 278 | |
| 279 | /* Avoid speed overflow */ |
| 280 | if (speed > 15) |
| 281 | return 15; |
| 282 | |
| 283 | return (u16)speed; |
Mauro Carvalho Chehab | e11206e | 2009-07-14 02:38:18 -0300 | [diff] [blame] | 284 | } |
| 285 | |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 286 | static void set_res(struct v4l2_subdev *sd) |
| 287 | { |
| 288 | struct mt9v011 *core = to_mt9v011(sd); |
| 289 | unsigned vstart, hstart; |
| 290 | |
| 291 | /* |
| 292 | * The mt9v011 doesn't have scaling. So, in order to select the desired |
| 293 | * resolution, we're cropping at the middle of the sensor. |
| 294 | * hblank and vblank should be adjusted, in order to warrant that |
| 295 | * we'll preserve the line timings for 30 fps, no matter what resolution |
| 296 | * is selected. |
Mauro Carvalho Chehab | 6934e6f | 2009-07-04 08:15:11 -0300 | [diff] [blame] | 297 | * NOTE: datasheet says that width (and height) should be filled with |
| 298 | * width-1. However, this doesn't work, since one pixel per line will |
| 299 | * be missing. |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 300 | */ |
| 301 | |
Johannes Obermaier | 1048af2 | 2011-06-02 12:17:51 -0300 | [diff] [blame] | 302 | hstart = 20 + (640 - core->width) / 2; |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 303 | mt9v011_write(sd, R02_MT9V011_COLSTART, hstart); |
| 304 | mt9v011_write(sd, R04_MT9V011_WIDTH, core->width); |
| 305 | mt9v011_write(sd, R05_MT9V011_HBLANK, 771 - core->width); |
| 306 | |
Mauro Carvalho Chehab | c180604 | 2009-07-14 02:39:19 -0300 | [diff] [blame] | 307 | vstart = 8 + (480 - core->height) / 2; |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 308 | mt9v011_write(sd, R01_MT9V011_ROWSTART, vstart); |
| 309 | mt9v011_write(sd, R03_MT9V011_HEIGHT, core->height); |
| 310 | mt9v011_write(sd, R06_MT9V011_VBLANK, 508 - core->height); |
Mauro Carvalho Chehab | e11206e | 2009-07-14 02:38:18 -0300 | [diff] [blame] | 311 | |
Mauro Carvalho Chehab | 83053f7 | 2009-08-06 21:03:35 -0300 | [diff] [blame] | 312 | calc_fps(sd, NULL, NULL); |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 313 | }; |
| 314 | |
Mauro Carvalho Chehab | 2526ea6 | 2009-08-07 01:09:54 -0300 | [diff] [blame] | 315 | static void set_read_mode(struct v4l2_subdev *sd) |
| 316 | { |
| 317 | struct mt9v011 *core = to_mt9v011(sd); |
| 318 | unsigned mode = 0x1000; |
| 319 | |
| 320 | if (core->hflip) |
| 321 | mode |= 0x4000; |
| 322 | |
| 323 | if (core->vflip) |
| 324 | mode |= 0x8000; |
| 325 | |
| 326 | mt9v011_write(sd, R20_MT9V011_READ_MODE, mode); |
| 327 | } |
| 328 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 329 | static int mt9v011_reset(struct v4l2_subdev *sd, u32 val) |
| 330 | { |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 331 | int i; |
| 332 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 333 | for (i = 0; i < ARRAY_SIZE(mt9v011_init_default); i++) |
| 334 | mt9v011_write(sd, mt9v011_init_default[i].reg, |
| 335 | mt9v011_init_default[i].value); |
| 336 | |
| 337 | set_balance(sd); |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 338 | set_res(sd); |
Mauro Carvalho Chehab | 2526ea6 | 2009-08-07 01:09:54 -0300 | [diff] [blame] | 339 | set_read_mode(sd); |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 340 | |
| 341 | return 0; |
| 342 | }; |
| 343 | |
| 344 | static int mt9v011_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
| 345 | { |
| 346 | struct mt9v011 *core = to_mt9v011(sd); |
| 347 | |
| 348 | v4l2_dbg(1, debug, sd, "g_ctrl called\n"); |
| 349 | |
| 350 | switch (ctrl->id) { |
| 351 | case V4L2_CID_GAIN: |
| 352 | ctrl->value = core->global_gain; |
| 353 | return 0; |
Johannes Obermaier | 590929f | 2011-06-02 12:43:14 -0300 | [diff] [blame^] | 354 | case V4L2_CID_EXPOSURE: |
| 355 | ctrl->value = core->exposure; |
| 356 | return 0; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 357 | case V4L2_CID_RED_BALANCE: |
| 358 | ctrl->value = core->red_bal; |
| 359 | return 0; |
| 360 | case V4L2_CID_BLUE_BALANCE: |
| 361 | ctrl->value = core->blue_bal; |
| 362 | return 0; |
Mauro Carvalho Chehab | 2526ea6 | 2009-08-07 01:09:54 -0300 | [diff] [blame] | 363 | case V4L2_CID_HFLIP: |
| 364 | ctrl->value = core->hflip ? 1 : 0; |
| 365 | return 0; |
| 366 | case V4L2_CID_VFLIP: |
| 367 | ctrl->value = core->vflip ? 1 : 0; |
| 368 | return 0; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 369 | } |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | |
Mauro Carvalho Chehab | 9873740 | 2009-07-13 01:03:37 -0300 | [diff] [blame] | 373 | static int mt9v011_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc) |
| 374 | { |
| 375 | int i; |
| 376 | |
| 377 | v4l2_dbg(1, debug, sd, "queryctrl called\n"); |
| 378 | |
| 379 | for (i = 0; i < ARRAY_SIZE(mt9v011_qctrl); i++) |
| 380 | if (qc->id && qc->id == mt9v011_qctrl[i].id) { |
| 381 | memcpy(qc, &(mt9v011_qctrl[i]), |
| 382 | sizeof(*qc)); |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | return -EINVAL; |
| 387 | } |
| 388 | |
| 389 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 390 | static int mt9v011_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
| 391 | { |
| 392 | struct mt9v011 *core = to_mt9v011(sd); |
| 393 | u8 i, n; |
| 394 | n = ARRAY_SIZE(mt9v011_qctrl); |
| 395 | |
| 396 | for (i = 0; i < n; i++) { |
| 397 | if (ctrl->id != mt9v011_qctrl[i].id) |
| 398 | continue; |
| 399 | if (ctrl->value < mt9v011_qctrl[i].minimum || |
| 400 | ctrl->value > mt9v011_qctrl[i].maximum) |
| 401 | return -ERANGE; |
| 402 | v4l2_dbg(1, debug, sd, "s_ctrl: id=%d, value=%d\n", |
| 403 | ctrl->id, ctrl->value); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | switch (ctrl->id) { |
| 408 | case V4L2_CID_GAIN: |
| 409 | core->global_gain = ctrl->value; |
| 410 | break; |
Johannes Obermaier | 590929f | 2011-06-02 12:43:14 -0300 | [diff] [blame^] | 411 | case V4L2_CID_EXPOSURE: |
| 412 | core->exposure = ctrl->value; |
| 413 | break; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 414 | case V4L2_CID_RED_BALANCE: |
| 415 | core->red_bal = ctrl->value; |
| 416 | break; |
| 417 | case V4L2_CID_BLUE_BALANCE: |
| 418 | core->blue_bal = ctrl->value; |
| 419 | break; |
Mauro Carvalho Chehab | 2526ea6 | 2009-08-07 01:09:54 -0300 | [diff] [blame] | 420 | case V4L2_CID_HFLIP: |
| 421 | core->hflip = ctrl->value; |
| 422 | set_read_mode(sd); |
| 423 | return 0; |
| 424 | case V4L2_CID_VFLIP: |
| 425 | core->vflip = ctrl->value; |
| 426 | set_read_mode(sd); |
| 427 | return 0; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 428 | default: |
| 429 | return -EINVAL; |
| 430 | } |
| 431 | |
| 432 | set_balance(sd); |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
Hans Verkuil | ea01b11 | 2010-05-09 10:19:25 -0300 | [diff] [blame] | 437 | static int mt9v011_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index, |
| 438 | enum v4l2_mbus_pixelcode *code) |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 439 | { |
Hans Verkuil | ea01b11 | 2010-05-09 10:19:25 -0300 | [diff] [blame] | 440 | if (index > 0) |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 441 | return -EINVAL; |
| 442 | |
Hans Verkuil | ea01b11 | 2010-05-09 10:19:25 -0300 | [diff] [blame] | 443 | *code = V4L2_MBUS_FMT_SGRBG8_1X8; |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Hans Verkuil | ea01b11 | 2010-05-09 10:19:25 -0300 | [diff] [blame] | 447 | static int mt9v011_try_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt) |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 448 | { |
Hans Verkuil | ea01b11 | 2010-05-09 10:19:25 -0300 | [diff] [blame] | 449 | if (fmt->code != V4L2_MBUS_FMT_SGRBG8_1X8) |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 450 | return -EINVAL; |
| 451 | |
Hans Verkuil | ea01b11 | 2010-05-09 10:19:25 -0300 | [diff] [blame] | 452 | v4l_bound_align_image(&fmt->width, 48, 639, 1, |
| 453 | &fmt->height, 32, 480, 1, 0); |
| 454 | fmt->field = V4L2_FIELD_NONE; |
| 455 | fmt->colorspace = V4L2_COLORSPACE_SRGB; |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Mauro Carvalho Chehab | 83053f7 | 2009-08-06 21:03:35 -0300 | [diff] [blame] | 460 | static int mt9v011_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms) |
| 461 | { |
| 462 | struct v4l2_captureparm *cp = &parms->parm.capture; |
| 463 | |
| 464 | if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | memset(cp, 0, sizeof(struct v4l2_captureparm)); |
| 468 | cp->capability = V4L2_CAP_TIMEPERFRAME; |
| 469 | calc_fps(sd, |
| 470 | &cp->timeperframe.numerator, |
| 471 | &cp->timeperframe.denominator); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int mt9v011_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms) |
| 477 | { |
| 478 | struct v4l2_captureparm *cp = &parms->parm.capture; |
| 479 | struct v4l2_fract *tpf = &cp->timeperframe; |
| 480 | u16 speed; |
| 481 | |
| 482 | if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 483 | return -EINVAL; |
| 484 | if (cp->extendedmode != 0) |
| 485 | return -EINVAL; |
| 486 | |
| 487 | speed = calc_speed(sd, tpf->numerator, tpf->denominator); |
| 488 | |
| 489 | mt9v011_write(sd, R0A_MT9V011_CLK_SPEED, speed); |
| 490 | v4l2_dbg(1, debug, sd, "Setting speed to %d\n", speed); |
| 491 | |
| 492 | /* Recalculate and update fps info */ |
| 493 | calc_fps(sd, &tpf->numerator, &tpf->denominator); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
Hans Verkuil | ea01b11 | 2010-05-09 10:19:25 -0300 | [diff] [blame] | 498 | static int mt9v011_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt) |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 499 | { |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 500 | struct mt9v011 *core = to_mt9v011(sd); |
| 501 | int rc; |
| 502 | |
Hans Verkuil | ea01b11 | 2010-05-09 10:19:25 -0300 | [diff] [blame] | 503 | rc = mt9v011_try_mbus_fmt(sd, fmt); |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 504 | if (rc < 0) |
| 505 | return -EINVAL; |
| 506 | |
Hans Verkuil | ea01b11 | 2010-05-09 10:19:25 -0300 | [diff] [blame] | 507 | core->width = fmt->width; |
| 508 | core->height = fmt->height; |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 509 | |
| 510 | set_res(sd); |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 515 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 516 | static int mt9v011_g_register(struct v4l2_subdev *sd, |
| 517 | struct v4l2_dbg_register *reg) |
| 518 | { |
| 519 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 520 | |
| 521 | if (!v4l2_chip_match_i2c_client(client, ®->match)) |
| 522 | return -EINVAL; |
| 523 | if (!capable(CAP_SYS_ADMIN)) |
| 524 | return -EPERM; |
| 525 | |
| 526 | reg->val = mt9v011_read(sd, reg->reg & 0xff); |
| 527 | reg->size = 2; |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int mt9v011_s_register(struct v4l2_subdev *sd, |
| 533 | struct v4l2_dbg_register *reg) |
| 534 | { |
| 535 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 536 | |
| 537 | if (!v4l2_chip_match_i2c_client(client, ®->match)) |
| 538 | return -EINVAL; |
| 539 | if (!capable(CAP_SYS_ADMIN)) |
| 540 | return -EPERM; |
| 541 | |
| 542 | mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff); |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | #endif |
| 547 | |
| 548 | static int mt9v011_g_chip_ident(struct v4l2_subdev *sd, |
| 549 | struct v4l2_dbg_chip_ident *chip) |
| 550 | { |
Mauro Carvalho Chehab | 296544e | 2009-07-27 08:24:29 -0300 | [diff] [blame] | 551 | u16 version; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 552 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 553 | |
Mauro Carvalho Chehab | 296544e | 2009-07-27 08:24:29 -0300 | [diff] [blame] | 554 | version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION); |
| 555 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 556 | return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_MT9V011, |
Mauro Carvalho Chehab | 296544e | 2009-07-27 08:24:29 -0300 | [diff] [blame] | 557 | version); |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | static const struct v4l2_subdev_core_ops mt9v011_core_ops = { |
Mauro Carvalho Chehab | 9873740 | 2009-07-13 01:03:37 -0300 | [diff] [blame] | 561 | .queryctrl = mt9v011_queryctrl, |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 562 | .g_ctrl = mt9v011_g_ctrl, |
| 563 | .s_ctrl = mt9v011_s_ctrl, |
| 564 | .reset = mt9v011_reset, |
| 565 | .g_chip_ident = mt9v011_g_chip_ident, |
| 566 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 567 | .g_register = mt9v011_g_register, |
| 568 | .s_register = mt9v011_s_register, |
| 569 | #endif |
| 570 | }; |
| 571 | |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 572 | static const struct v4l2_subdev_video_ops mt9v011_video_ops = { |
Hans Verkuil | ea01b11 | 2010-05-09 10:19:25 -0300 | [diff] [blame] | 573 | .enum_mbus_fmt = mt9v011_enum_mbus_fmt, |
| 574 | .try_mbus_fmt = mt9v011_try_mbus_fmt, |
| 575 | .s_mbus_fmt = mt9v011_s_mbus_fmt, |
Mauro Carvalho Chehab | 83053f7 | 2009-08-06 21:03:35 -0300 | [diff] [blame] | 576 | .g_parm = mt9v011_g_parm, |
| 577 | .s_parm = mt9v011_s_parm, |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 578 | }; |
| 579 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 580 | static const struct v4l2_subdev_ops mt9v011_ops = { |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 581 | .core = &mt9v011_core_ops, |
| 582 | .video = &mt9v011_video_ops, |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 583 | }; |
| 584 | |
| 585 | |
| 586 | /**************************************************************************** |
| 587 | I2C Client & Driver |
| 588 | ****************************************************************************/ |
| 589 | |
| 590 | static int mt9v011_probe(struct i2c_client *c, |
| 591 | const struct i2c_device_id *id) |
| 592 | { |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 593 | u16 version; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 594 | struct mt9v011 *core; |
| 595 | struct v4l2_subdev *sd; |
| 596 | |
| 597 | /* Check if the adapter supports the needed features */ |
| 598 | if (!i2c_check_functionality(c->adapter, |
| 599 | I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) |
| 600 | return -EIO; |
| 601 | |
| 602 | core = kzalloc(sizeof(struct mt9v011), GFP_KERNEL); |
| 603 | if (!core) |
| 604 | return -ENOMEM; |
| 605 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 606 | sd = &core->sd; |
| 607 | v4l2_i2c_subdev_init(sd, c, &mt9v011_ops); |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 608 | |
| 609 | /* Check if the sensor is really a MT9V011 */ |
| 610 | version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION); |
Mauro Carvalho Chehab | 296544e | 2009-07-27 08:24:29 -0300 | [diff] [blame] | 611 | if ((version != MT9V011_VERSION) && |
| 612 | (version != MT9V011_REV_B_VERSION)) { |
| 613 | v4l2_info(sd, "*** unknown micron chip detected (0x%04x).\n", |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 614 | version); |
| 615 | kfree(core); |
| 616 | return -EINVAL; |
| 617 | } |
| 618 | |
| 619 | core->global_gain = 0x0024; |
Johannes Obermaier | 590929f | 2011-06-02 12:43:14 -0300 | [diff] [blame^] | 620 | core->exposure = 0x01fc; |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 621 | core->width = 640; |
| 622 | core->height = 480; |
Mauro Carvalho Chehab | e11206e | 2009-07-14 02:38:18 -0300 | [diff] [blame] | 623 | core->xtal = 27000000; /* Hz */ |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame] | 624 | |
Hans Verkuil | 3c7c937 | 2011-01-08 07:08:02 -0300 | [diff] [blame] | 625 | if (c->dev.platform_data) { |
| 626 | struct mt9v011_platform_data *pdata = c->dev.platform_data; |
| 627 | |
| 628 | core->xtal = pdata->xtal; |
| 629 | v4l2_dbg(1, debug, sd, "xtal set to %d.%03d MHz\n", |
| 630 | core->xtal / 1000000, (core->xtal / 1000) % 1000); |
| 631 | } |
| 632 | |
Mauro Carvalho Chehab | 296544e | 2009-07-27 08:24:29 -0300 | [diff] [blame] | 633 | v4l_info(c, "chip found @ 0x%02x (%s - chip version 0x%04x)\n", |
| 634 | c->addr << 1, c->adapter->name, version); |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | static int mt9v011_remove(struct i2c_client *c) |
| 640 | { |
| 641 | struct v4l2_subdev *sd = i2c_get_clientdata(c); |
| 642 | |
| 643 | v4l2_dbg(1, debug, sd, |
| 644 | "mt9v011.c: removing mt9v011 adapter on address 0x%x\n", |
| 645 | c->addr << 1); |
| 646 | |
| 647 | v4l2_device_unregister_subdev(sd); |
| 648 | kfree(to_mt9v011(sd)); |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | /* ----------------------------------------------------------------------- */ |
| 653 | |
| 654 | static const struct i2c_device_id mt9v011_id[] = { |
| 655 | { "mt9v011", 0 }, |
| 656 | { } |
| 657 | }; |
| 658 | MODULE_DEVICE_TABLE(i2c, mt9v011_id); |
| 659 | |
Hans Verkuil | 6ce58be | 2010-09-15 15:09:38 -0300 | [diff] [blame] | 660 | static struct i2c_driver mt9v011_driver = { |
| 661 | .driver = { |
| 662 | .owner = THIS_MODULE, |
| 663 | .name = "mt9v011", |
| 664 | }, |
| 665 | .probe = mt9v011_probe, |
| 666 | .remove = mt9v011_remove, |
| 667 | .id_table = mt9v011_id, |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 668 | }; |
Hans Verkuil | 6ce58be | 2010-09-15 15:09:38 -0300 | [diff] [blame] | 669 | |
| 670 | static __init int init_mt9v011(void) |
| 671 | { |
| 672 | return i2c_add_driver(&mt9v011_driver); |
| 673 | } |
| 674 | |
| 675 | static __exit void exit_mt9v011(void) |
| 676 | { |
| 677 | i2c_del_driver(&mt9v011_driver); |
| 678 | } |
| 679 | |
| 680 | module_init(init_mt9v011); |
| 681 | module_exit(exit_mt9v011); |