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; |
| 59 | |
| 60 | u16 global_gain, red_bal, blue_bal; |
| 61 | }; |
| 62 | |
| 63 | static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd) |
| 64 | { |
| 65 | return container_of(sd, struct mt9v011, sd); |
| 66 | } |
| 67 | |
| 68 | static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr) |
| 69 | { |
| 70 | struct i2c_client *c = v4l2_get_subdevdata(sd); |
| 71 | __be16 buffer; |
| 72 | int rc, val; |
| 73 | |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame^] | 74 | rc = i2c_master_send(c, &addr, 1); |
| 75 | if (rc != 1) |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 76 | v4l2_dbg(0, debug, sd, |
| 77 | "i2c i/o error: rc == %d (should be 1)\n", rc); |
| 78 | |
| 79 | msleep(10); |
| 80 | |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame^] | 81 | rc = i2c_master_recv(c, (char *)&buffer, 2); |
| 82 | if (rc != 2) |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 83 | v4l2_dbg(0, debug, sd, |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame^] | 84 | "i2c i/o error: rc == %d (should be 2)\n", rc); |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 85 | |
| 86 | val = be16_to_cpu(buffer); |
| 87 | |
| 88 | v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val); |
| 89 | |
| 90 | return val; |
| 91 | } |
| 92 | |
| 93 | static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr, |
| 94 | u16 value) |
| 95 | { |
| 96 | struct i2c_client *c = v4l2_get_subdevdata(sd); |
| 97 | unsigned char buffer[3]; |
| 98 | int rc; |
| 99 | |
| 100 | buffer[0] = addr; |
| 101 | buffer[1] = value >> 8; |
| 102 | buffer[2] = value & 0xff; |
| 103 | |
| 104 | v4l2_dbg(2, debug, sd, |
| 105 | "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value); |
Mauro Carvalho Chehab | fbe2800 | 2009-06-29 11:12:05 -0300 | [diff] [blame^] | 106 | rc = i2c_master_send(c, &buffer, 3); |
| 107 | if (rc != 3) |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 108 | v4l2_dbg(0, debug, sd, |
| 109 | "i2c i/o error: rc == %d (should be 3)\n", rc); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | struct i2c_reg_value { |
| 114 | unsigned char reg; |
| 115 | u16 value; |
| 116 | }; |
| 117 | |
| 118 | /* |
| 119 | * Values used at the original driver |
| 120 | * Some values are marked as Reserved at the datasheet |
| 121 | */ |
| 122 | static const struct i2c_reg_value mt9v011_init_default[] = { |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 123 | { R0D_MT9V011_RESET, 0x0001 }, |
| 124 | { R0D_MT9V011_RESET, 0x0000 }, |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 125 | |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 126 | { R01_MT9V011_ROWSTART, 0x0008 }, |
| 127 | { R02_MT9V011_COLSTART, 0x0014 }, |
| 128 | { R03_MT9V011_HEIGHT, 0x01e0 }, |
| 129 | { R04_MT9V011_WIDTH, 0x0280 }, |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 130 | { R05_MT9V011_HBLANK, 0x007b }, |
| 131 | { R06_MT9V011_VBLANK, 0x001c }, |
| 132 | { R09_MT9V011_SHUTTER_WIDTH, 0x0418 }, |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 133 | { R0A_MT9V011_CLK_SPEED, 0x0000 }, |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 134 | { R0C_MT9V011_SHUTTER_DELAY, 0x0000 }, |
| 135 | { R1E_MT9V011_DIGITAL_ZOOM, 0x0000 }, |
| 136 | { R20_MT9V011_READ_MODE, 0x1100 }, |
| 137 | |
| 138 | /* |
| 139 | * Those registers are not docummented at the datasheet. |
| 140 | * However, the original driver initializes them |
| 141 | */ |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 142 | { 0x30, 0x0005 }, |
| 143 | { 0x34, 0x0100 }, |
| 144 | { 0x3d, 0x068f }, |
| 145 | { 0x40, 0x01e0 }, |
| 146 | { 0x52, 0x0100 }, |
| 147 | { 0x58, 0x0038 }, /* Datasheet default 0x0078 */ |
| 148 | { 0x59, 0x0723 }, /* Datasheet default 0x0703 */ |
| 149 | { 0x62, 0x041a }, /* Datasheet default 0x0418 */ |
Mauro Carvalho Chehab | afe09f8 | 2009-06-29 11:03:22 -0300 | [diff] [blame] | 150 | |
| 151 | { R07_MT9V011_OUT_CTRL, 0x000a }, /* chip enable */ |
Mauro Carvalho Chehab | 7dfba00 | 2009-06-29 05:41:26 -0300 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | static void set_balance(struct v4l2_subdev *sd) |
| 155 | { |
| 156 | struct mt9v011 *core = to_mt9v011(sd); |
| 157 | u16 green1_gain, green2_gain, blue_gain, red_gain; |
| 158 | |
| 159 | green1_gain = core->global_gain; |
| 160 | green2_gain = core->global_gain; |
| 161 | |
| 162 | blue_gain = core->global_gain + |
| 163 | core->global_gain * core->blue_bal / (1 << 9); |
| 164 | |
| 165 | red_gain = core->global_gain + |
| 166 | core->global_gain * core->blue_bal / (1 << 9); |
| 167 | |
| 168 | mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green1_gain); |
| 169 | mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green1_gain); |
| 170 | mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain); |
| 171 | mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain); |
| 172 | } |
| 173 | |
| 174 | static int mt9v011_reset(struct v4l2_subdev *sd, u32 val) |
| 175 | { |
| 176 | u16 version; |
| 177 | int i; |
| 178 | |
| 179 | version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION); |
| 180 | |
| 181 | if (version != MT9V011_VERSION) { |
| 182 | v4l2_info(sd, "*** unknown micron chip detected (0x%04x.\n", |
| 183 | version); |
| 184 | return -EINVAL; |
| 185 | |
| 186 | } |
| 187 | |
| 188 | for (i = 0; i < ARRAY_SIZE(mt9v011_init_default); i++) |
| 189 | mt9v011_write(sd, mt9v011_init_default[i].reg, |
| 190 | mt9v011_init_default[i].value); |
| 191 | |
| 192 | set_balance(sd); |
| 193 | |
| 194 | return 0; |
| 195 | }; |
| 196 | |
| 197 | static int mt9v011_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
| 198 | { |
| 199 | struct mt9v011 *core = to_mt9v011(sd); |
| 200 | |
| 201 | v4l2_dbg(1, debug, sd, "g_ctrl called\n"); |
| 202 | |
| 203 | switch (ctrl->id) { |
| 204 | case V4L2_CID_GAIN: |
| 205 | ctrl->value = core->global_gain; |
| 206 | return 0; |
| 207 | case V4L2_CID_RED_BALANCE: |
| 208 | ctrl->value = core->red_bal; |
| 209 | return 0; |
| 210 | case V4L2_CID_BLUE_BALANCE: |
| 211 | ctrl->value = core->blue_bal; |
| 212 | return 0; |
| 213 | } |
| 214 | return -EINVAL; |
| 215 | } |
| 216 | |
| 217 | static int mt9v011_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl) |
| 218 | { |
| 219 | struct mt9v011 *core = to_mt9v011(sd); |
| 220 | u8 i, n; |
| 221 | n = ARRAY_SIZE(mt9v011_qctrl); |
| 222 | |
| 223 | for (i = 0; i < n; i++) { |
| 224 | if (ctrl->id != mt9v011_qctrl[i].id) |
| 225 | continue; |
| 226 | if (ctrl->value < mt9v011_qctrl[i].minimum || |
| 227 | ctrl->value > mt9v011_qctrl[i].maximum) |
| 228 | return -ERANGE; |
| 229 | v4l2_dbg(1, debug, sd, "s_ctrl: id=%d, value=%d\n", |
| 230 | ctrl->id, ctrl->value); |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | switch (ctrl->id) { |
| 235 | case V4L2_CID_GAIN: |
| 236 | core->global_gain = ctrl->value; |
| 237 | break; |
| 238 | case V4L2_CID_RED_BALANCE: |
| 239 | core->red_bal = ctrl->value; |
| 240 | break; |
| 241 | case V4L2_CID_BLUE_BALANCE: |
| 242 | core->blue_bal = ctrl->value; |
| 243 | break; |
| 244 | default: |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
| 248 | set_balance(sd); |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 254 | static int mt9v011_g_register(struct v4l2_subdev *sd, |
| 255 | struct v4l2_dbg_register *reg) |
| 256 | { |
| 257 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 258 | |
| 259 | if (!v4l2_chip_match_i2c_client(client, ®->match)) |
| 260 | return -EINVAL; |
| 261 | if (!capable(CAP_SYS_ADMIN)) |
| 262 | return -EPERM; |
| 263 | |
| 264 | reg->val = mt9v011_read(sd, reg->reg & 0xff); |
| 265 | reg->size = 2; |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int mt9v011_s_register(struct v4l2_subdev *sd, |
| 271 | struct v4l2_dbg_register *reg) |
| 272 | { |
| 273 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 274 | |
| 275 | if (!v4l2_chip_match_i2c_client(client, ®->match)) |
| 276 | return -EINVAL; |
| 277 | if (!capable(CAP_SYS_ADMIN)) |
| 278 | return -EPERM; |
| 279 | |
| 280 | mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | #endif |
| 285 | |
| 286 | static int mt9v011_g_chip_ident(struct v4l2_subdev *sd, |
| 287 | struct v4l2_dbg_chip_ident *chip) |
| 288 | { |
| 289 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 290 | |
| 291 | return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_MT9V011, |
| 292 | MT9V011_VERSION); |
| 293 | } |
| 294 | |
| 295 | static const struct v4l2_subdev_core_ops mt9v011_core_ops = { |
| 296 | .g_ctrl = mt9v011_g_ctrl, |
| 297 | .s_ctrl = mt9v011_s_ctrl, |
| 298 | .reset = mt9v011_reset, |
| 299 | .g_chip_ident = mt9v011_g_chip_ident, |
| 300 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 301 | .g_register = mt9v011_g_register, |
| 302 | .s_register = mt9v011_s_register, |
| 303 | #endif |
| 304 | }; |
| 305 | |
| 306 | static const struct v4l2_subdev_ops mt9v011_ops = { |
| 307 | .core = &mt9v011_core_ops, |
| 308 | }; |
| 309 | |
| 310 | |
| 311 | /**************************************************************************** |
| 312 | I2C Client & Driver |
| 313 | ****************************************************************************/ |
| 314 | |
| 315 | static int mt9v011_probe(struct i2c_client *c, |
| 316 | const struct i2c_device_id *id) |
| 317 | { |
| 318 | struct mt9v011 *core; |
| 319 | struct v4l2_subdev *sd; |
| 320 | |
| 321 | /* Check if the adapter supports the needed features */ |
| 322 | if (!i2c_check_functionality(c->adapter, |
| 323 | I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) |
| 324 | return -EIO; |
| 325 | |
| 326 | core = kzalloc(sizeof(struct mt9v011), GFP_KERNEL); |
| 327 | if (!core) |
| 328 | return -ENOMEM; |
| 329 | |
| 330 | core->global_gain = 0x0024; |
| 331 | |
| 332 | sd = &core->sd; |
| 333 | v4l2_i2c_subdev_init(sd, c, &mt9v011_ops); |
| 334 | v4l_info(c, "chip found @ 0x%02x (%s)\n", |
| 335 | c->addr << 1, c->adapter->name); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int mt9v011_remove(struct i2c_client *c) |
| 341 | { |
| 342 | struct v4l2_subdev *sd = i2c_get_clientdata(c); |
| 343 | |
| 344 | v4l2_dbg(1, debug, sd, |
| 345 | "mt9v011.c: removing mt9v011 adapter on address 0x%x\n", |
| 346 | c->addr << 1); |
| 347 | |
| 348 | v4l2_device_unregister_subdev(sd); |
| 349 | kfree(to_mt9v011(sd)); |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* ----------------------------------------------------------------------- */ |
| 354 | |
| 355 | static const struct i2c_device_id mt9v011_id[] = { |
| 356 | { "mt9v011", 0 }, |
| 357 | { } |
| 358 | }; |
| 359 | MODULE_DEVICE_TABLE(i2c, mt9v011_id); |
| 360 | |
| 361 | static struct v4l2_i2c_driver_data v4l2_i2c_data = { |
| 362 | .name = "mt9v011", |
| 363 | .probe = mt9v011_probe, |
| 364 | .remove = mt9v011_remove, |
| 365 | .id_table = mt9v011_id, |
| 366 | }; |