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> |
| 9 | #include <linux/videodev2.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <media/v4l2-device.h> |
| 12 | #include "mt9v011.h" |
| 13 | #include <media/v4l2-i2c-drv.h> |
| 14 | #include <media/v4l2-chip-ident.h> |
| 15 | |
| 16 | MODULE_DESCRIPTION("Micron mt9v011 sensor driver"); |
| 17 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 18 | MODULE_LICENSE("GPL"); |
| 19 | |
| 20 | |
| 21 | static int debug; |
| 22 | module_param(debug, int, 0); |
| 23 | MODULE_PARM_DESC(debug, "Debug level (0-2)"); |
| 24 | |
| 25 | /* supported controls */ |
| 26 | static struct v4l2_queryctrl mt9v011_qctrl[] = { |
| 27 | { |
| 28 | .id = V4L2_CID_GAIN, |
| 29 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 30 | .name = "Gain", |
| 31 | .minimum = 0, |
| 32 | .maximum = (1 << 10) - 1, |
| 33 | .step = 1, |
| 34 | .default_value = 0x0020, |
| 35 | .flags = 0, |
| 36 | }, { |
| 37 | .id = V4L2_CID_RED_BALANCE, |
| 38 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 39 | .name = "Red Balance", |
| 40 | .minimum = -1 << 9, |
| 41 | .maximum = (1 << 9) - 1, |
| 42 | .step = 1, |
| 43 | .default_value = 0, |
| 44 | .flags = 0, |
| 45 | }, { |
| 46 | .id = V4L2_CID_BLUE_BALANCE, |
| 47 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 48 | .name = "Blue Balance", |
| 49 | .minimum = -1 << 9, |
| 50 | .maximum = (1 << 9) - 1, |
| 51 | .step = 1, |
| 52 | .default_value = 0, |
| 53 | .flags = 0, |
| 54 | }, |
| 55 | }; |
| 56 | |
| 57 | struct mt9v011 { |
| 58 | struct v4l2_subdev sd; |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame^] | 59 | unsigned width, height; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 60 | |
| 61 | u16 global_gain, red_bal, blue_bal; |
| 62 | }; |
| 63 | |
| 64 | static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd) |
| 65 | { |
| 66 | return container_of(sd, struct mt9v011, sd); |
| 67 | } |
| 68 | |
| 69 | static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr) |
| 70 | { |
| 71 | struct i2c_client *c = v4l2_get_subdevdata(sd); |
| 72 | __be16 buffer; |
| 73 | int rc, val; |
| 74 | |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame] | 75 | rc = i2c_master_send(c, &addr, 1); |
| 76 | if (rc != 1) |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 77 | v4l2_dbg(0, debug, sd, |
| 78 | "i2c i/o error: rc == %d (should be 1)\n", rc); |
| 79 | |
| 80 | msleep(10); |
| 81 | |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame] | 82 | rc = i2c_master_recv(c, (char *)&buffer, 2); |
| 83 | if (rc != 2) |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 84 | v4l2_dbg(0, debug, sd, |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame] | 85 | "i2c i/o error: rc == %d (should be 2)\n", rc); |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 86 | |
| 87 | val = be16_to_cpu(buffer); |
| 88 | |
| 89 | v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val); |
| 90 | |
| 91 | return val; |
| 92 | } |
| 93 | |
| 94 | static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr, |
| 95 | u16 value) |
| 96 | { |
| 97 | struct i2c_client *c = v4l2_get_subdevdata(sd); |
| 98 | unsigned char buffer[3]; |
| 99 | int rc; |
| 100 | |
| 101 | buffer[0] = addr; |
| 102 | buffer[1] = value >> 8; |
| 103 | buffer[2] = value & 0xff; |
| 104 | |
| 105 | v4l2_dbg(2, debug, sd, |
| 106 | "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value); |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame^] | 107 | rc = i2c_master_send(c, buffer, 3); |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame] | 108 | if (rc != 3) |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 109 | v4l2_dbg(0, debug, sd, |
| 110 | "i2c i/o error: rc == %d (should be 3)\n", rc); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | struct i2c_reg_value { |
| 115 | unsigned char reg; |
| 116 | u16 value; |
| 117 | }; |
| 118 | |
| 119 | /* |
| 120 | * Values used at the original driver |
| 121 | * Some values are marked as Reserved at the datasheet |
| 122 | */ |
| 123 | static const struct i2c_reg_value mt9v011_init_default[] = { |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 124 | { R0D_MT9V011_RESET, 0x0001 }, |
| 125 | { R0D_MT9V011_RESET, 0x0000 }, |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 126 | |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 127 | { R09_MT9V011_SHUTTER_WIDTH, 0x0418 }, |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 128 | { R0A_MT9V011_CLK_SPEED, 0x0000 }, |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 129 | { R0C_MT9V011_SHUTTER_DELAY, 0x0000 }, |
| 130 | { R1E_MT9V011_DIGITAL_ZOOM, 0x0000 }, |
| 131 | { R20_MT9V011_READ_MODE, 0x1100 }, |
| 132 | |
| 133 | /* |
| 134 | * Those registers are not docummented at the datasheet. |
| 135 | * However, the original driver initializes them |
| 136 | */ |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 137 | { 0x30, 0x0005 }, |
| 138 | { 0x34, 0x0100 }, |
| 139 | { 0x3d, 0x068f }, |
| 140 | { 0x40, 0x01e0 }, |
| 141 | { 0x52, 0x0100 }, |
| 142 | { 0x58, 0x0038 }, /* Datasheet default 0x0078 */ |
| 143 | { 0x59, 0x0723 }, /* Datasheet default 0x0703 */ |
| 144 | { 0x62, 0x041a }, /* Datasheet default 0x0418 */ |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 145 | |
| 146 | { R07_MT9V011_OUT_CTRL, 0x000a }, /* chip enable */ |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | static void set_balance(struct v4l2_subdev *sd) |
| 150 | { |
| 151 | struct mt9v011 *core = to_mt9v011(sd); |
| 152 | u16 green1_gain, green2_gain, blue_gain, red_gain; |
| 153 | |
| 154 | green1_gain = core->global_gain; |
| 155 | green2_gain = core->global_gain; |
| 156 | |
| 157 | blue_gain = core->global_gain + |
| 158 | core->global_gain * core->blue_bal / (1 << 9); |
| 159 | |
| 160 | red_gain = core->global_gain + |
| 161 | core->global_gain * core->blue_bal / (1 << 9); |
| 162 | |
| 163 | mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green1_gain); |
| 164 | mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green1_gain); |
| 165 | mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain); |
| 166 | mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain); |
| 167 | } |
| 168 | |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame^] | 169 | static void set_res(struct v4l2_subdev *sd) |
| 170 | { |
| 171 | struct mt9v011 *core = to_mt9v011(sd); |
| 172 | unsigned vstart, hstart; |
| 173 | |
| 174 | /* |
| 175 | * The mt9v011 doesn't have scaling. So, in order to select the desired |
| 176 | * resolution, we're cropping at the middle of the sensor. |
| 177 | * hblank and vblank should be adjusted, in order to warrant that |
| 178 | * we'll preserve the line timings for 30 fps, no matter what resolution |
| 179 | * is selected. |
| 180 | */ |
| 181 | |
| 182 | hstart = 14 + (640 - core->width) / 2; |
| 183 | mt9v011_write(sd, R02_MT9V011_COLSTART, hstart); |
| 184 | mt9v011_write(sd, R04_MT9V011_WIDTH, core->width); |
| 185 | mt9v011_write(sd, R05_MT9V011_HBLANK, 771 - core->width); |
| 186 | |
| 187 | vstart = 8 + (640 - core->height) / 2; |
| 188 | mt9v011_write(sd, R01_MT9V011_ROWSTART, vstart); |
| 189 | mt9v011_write(sd, R03_MT9V011_HEIGHT, core->height); |
| 190 | mt9v011_write(sd, R06_MT9V011_VBLANK, 508 - core->height); |
| 191 | }; |
| 192 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 193 | static int mt9v011_reset(struct v4l2_subdev *sd, u32 val) |
| 194 | { |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 195 | int i; |
| 196 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 197 | for (i = 0; i < ARRAY_SIZE(mt9v011_init_default); i++) |
| 198 | mt9v011_write(sd, mt9v011_init_default[i].reg, |
| 199 | mt9v011_init_default[i].value); |
| 200 | |
| 201 | set_balance(sd); |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame^] | 202 | set_res(sd); |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 203 | |
| 204 | return 0; |
| 205 | }; |
| 206 | |
| 207 | static int mt9v011_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
| 208 | { |
| 209 | struct mt9v011 *core = to_mt9v011(sd); |
| 210 | |
| 211 | v4l2_dbg(1, debug, sd, "g_ctrl called\n"); |
| 212 | |
| 213 | switch (ctrl->id) { |
| 214 | case V4L2_CID_GAIN: |
| 215 | ctrl->value = core->global_gain; |
| 216 | return 0; |
| 217 | case V4L2_CID_RED_BALANCE: |
| 218 | ctrl->value = core->red_bal; |
| 219 | return 0; |
| 220 | case V4L2_CID_BLUE_BALANCE: |
| 221 | ctrl->value = core->blue_bal; |
| 222 | return 0; |
| 223 | } |
| 224 | return -EINVAL; |
| 225 | } |
| 226 | |
| 227 | static int mt9v011_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
| 228 | { |
| 229 | struct mt9v011 *core = to_mt9v011(sd); |
| 230 | u8 i, n; |
| 231 | n = ARRAY_SIZE(mt9v011_qctrl); |
| 232 | |
| 233 | for (i = 0; i < n; i++) { |
| 234 | if (ctrl->id != mt9v011_qctrl[i].id) |
| 235 | continue; |
| 236 | if (ctrl->value < mt9v011_qctrl[i].minimum || |
| 237 | ctrl->value > mt9v011_qctrl[i].maximum) |
| 238 | return -ERANGE; |
| 239 | v4l2_dbg(1, debug, sd, "s_ctrl: id=%d, value=%d\n", |
| 240 | ctrl->id, ctrl->value); |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | switch (ctrl->id) { |
| 245 | case V4L2_CID_GAIN: |
| 246 | core->global_gain = ctrl->value; |
| 247 | break; |
| 248 | case V4L2_CID_RED_BALANCE: |
| 249 | core->red_bal = ctrl->value; |
| 250 | break; |
| 251 | case V4L2_CID_BLUE_BALANCE: |
| 252 | core->blue_bal = ctrl->value; |
| 253 | break; |
| 254 | default: |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
| 258 | set_balance(sd); |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame^] | 263 | static int mt9v011_enum_fmt(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt) |
| 264 | { |
| 265 | if (fmt->index > 0) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | fmt->flags = 0; |
| 269 | strcpy(fmt->description, "8 bpp Bayer GRGR..BGBG"); |
| 270 | fmt->pixelformat = V4L2_PIX_FMT_SGRBG8; |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static int mt9v011_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt) |
| 276 | { |
| 277 | struct v4l2_pix_format *pix = &fmt->fmt.pix; |
| 278 | |
| 279 | if (pix->pixelformat != V4L2_PIX_FMT_SGRBG8) |
| 280 | return -EINVAL; |
| 281 | |
| 282 | v4l_bound_align_image(&pix->width, 48, 639, 1, |
| 283 | &pix->height, 32, 480, 1, 0); |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int mt9v011_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt) |
| 289 | { |
| 290 | struct v4l2_pix_format *pix = &fmt->fmt.pix; |
| 291 | struct mt9v011 *core = to_mt9v011(sd); |
| 292 | int rc; |
| 293 | |
| 294 | rc = mt9v011_try_fmt(sd, fmt); |
| 295 | if (rc < 0) |
| 296 | return -EINVAL; |
| 297 | |
| 298 | core->width = pix->width; |
| 299 | core->height = pix->height; |
| 300 | |
| 301 | set_res(sd); |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 307 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 308 | static int mt9v011_g_register(struct v4l2_subdev *sd, |
| 309 | struct v4l2_dbg_register *reg) |
| 310 | { |
| 311 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 312 | |
| 313 | if (!v4l2_chip_match_i2c_client(client, ®->match)) |
| 314 | return -EINVAL; |
| 315 | if (!capable(CAP_SYS_ADMIN)) |
| 316 | return -EPERM; |
| 317 | |
| 318 | reg->val = mt9v011_read(sd, reg->reg & 0xff); |
| 319 | reg->size = 2; |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static int mt9v011_s_register(struct v4l2_subdev *sd, |
| 325 | struct v4l2_dbg_register *reg) |
| 326 | { |
| 327 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 328 | |
| 329 | if (!v4l2_chip_match_i2c_client(client, ®->match)) |
| 330 | return -EINVAL; |
| 331 | if (!capable(CAP_SYS_ADMIN)) |
| 332 | return -EPERM; |
| 333 | |
| 334 | mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | #endif |
| 339 | |
| 340 | static int mt9v011_g_chip_ident(struct v4l2_subdev *sd, |
| 341 | struct v4l2_dbg_chip_ident *chip) |
| 342 | { |
| 343 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 344 | |
| 345 | return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_MT9V011, |
| 346 | MT9V011_VERSION); |
| 347 | } |
| 348 | |
| 349 | static const struct v4l2_subdev_core_ops mt9v011_core_ops = { |
| 350 | .g_ctrl = mt9v011_g_ctrl, |
| 351 | .s_ctrl = mt9v011_s_ctrl, |
| 352 | .reset = mt9v011_reset, |
| 353 | .g_chip_ident = mt9v011_g_chip_ident, |
| 354 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 355 | .g_register = mt9v011_g_register, |
| 356 | .s_register = mt9v011_s_register, |
| 357 | #endif |
| 358 | }; |
| 359 | |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame^] | 360 | static const struct v4l2_subdev_video_ops mt9v011_video_ops = { |
| 361 | .enum_fmt = mt9v011_enum_fmt, |
| 362 | .try_fmt = mt9v011_try_fmt, |
| 363 | .s_fmt = mt9v011_s_fmt, |
| 364 | }; |
| 365 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 366 | static const struct v4l2_subdev_ops mt9v011_ops = { |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame^] | 367 | .core = &mt9v011_core_ops, |
| 368 | .video = &mt9v011_video_ops, |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 369 | }; |
| 370 | |
| 371 | |
| 372 | /**************************************************************************** |
| 373 | I2C Client & Driver |
| 374 | ****************************************************************************/ |
| 375 | |
| 376 | static int mt9v011_probe(struct i2c_client *c, |
| 377 | const struct i2c_device_id *id) |
| 378 | { |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame^] | 379 | u16 version; |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 380 | struct mt9v011 *core; |
| 381 | struct v4l2_subdev *sd; |
| 382 | |
| 383 | /* Check if the adapter supports the needed features */ |
| 384 | if (!i2c_check_functionality(c->adapter, |
| 385 | I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) |
| 386 | return -EIO; |
| 387 | |
| 388 | core = kzalloc(sizeof(struct mt9v011), GFP_KERNEL); |
| 389 | if (!core) |
| 390 | return -ENOMEM; |
| 391 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 392 | sd = &core->sd; |
| 393 | v4l2_i2c_subdev_init(sd, c, &mt9v011_ops); |
Mauro Carvalho Chehab | 27fe4a3 | 2009-07-04 08:03:48 -0300 | [diff] [blame^] | 394 | |
| 395 | /* Check if the sensor is really a MT9V011 */ |
| 396 | version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION); |
| 397 | if (version != MT9V011_VERSION) { |
| 398 | v4l2_info(sd, "*** unknown micron chip detected (0x%04x.\n", |
| 399 | version); |
| 400 | kfree(core); |
| 401 | return -EINVAL; |
| 402 | } |
| 403 | |
| 404 | core->global_gain = 0x0024; |
| 405 | core->width = 640; |
| 406 | core->height = 480; |
| 407 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 408 | v4l_info(c, "chip found @ 0x%02x (%s)\n", |
| 409 | c->addr << 1, c->adapter->name); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int mt9v011_remove(struct i2c_client *c) |
| 415 | { |
| 416 | struct v4l2_subdev *sd = i2c_get_clientdata(c); |
| 417 | |
| 418 | v4l2_dbg(1, debug, sd, |
| 419 | "mt9v011.c: removing mt9v011 adapter on address 0x%x\n", |
| 420 | c->addr << 1); |
| 421 | |
| 422 | v4l2_device_unregister_subdev(sd); |
| 423 | kfree(to_mt9v011(sd)); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* ----------------------------------------------------------------------- */ |
| 428 | |
| 429 | static const struct i2c_device_id mt9v011_id[] = { |
| 430 | { "mt9v011", 0 }, |
| 431 | { } |
| 432 | }; |
| 433 | MODULE_DEVICE_TABLE(i2c, mt9v011_id); |
| 434 | |
| 435 | static struct v4l2_i2c_driver_data v4l2_i2c_data = { |
| 436 | .name = "mt9v011", |
| 437 | .probe = mt9v011_probe, |
| 438 | .remove = mt9v011_remove, |
| 439 | .id_table = mt9v011_id, |
| 440 | }; |