Lines Matching +full:1 +full:- +full:sd
1 // SPDX-License-Identifier: GPL-2.0
3 // mt9v011 -Micron 1/4-Inch VGA Digital Image Sensor
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-ctrls.h>
23 MODULE_PARM_DESC(debug, "Debug level (0-2)");
51 struct v4l2_subdev sd; member
58 unsigned hflip:1;
59 unsigned vflip:1;
65 static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd) in to_mt9v011() argument
67 return container_of(sd, struct mt9v011, sd); in to_mt9v011()
70 static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr) in mt9v011_read() argument
72 struct i2c_client *c = v4l2_get_subdevdata(sd); in mt9v011_read()
76 rc = i2c_master_send(c, &addr, 1); in mt9v011_read()
77 if (rc != 1) in mt9v011_read()
78 v4l2_dbg(0, debug, sd, in mt9v011_read()
79 "i2c i/o error: rc == %d (should be 1)\n", rc); in mt9v011_read()
85 v4l2_dbg(0, debug, sd, in mt9v011_read()
90 v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val); in mt9v011_read()
95 static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr, in mt9v011_write() argument
98 struct i2c_client *c = v4l2_get_subdevdata(sd); in mt9v011_write()
103 buffer[1] = value >> 8; in mt9v011_write()
106 v4l2_dbg(2, debug, sd, in mt9v011_write()
110 v4l2_dbg(0, debug, sd, in mt9v011_write()
159 digitalgain = 1; in calc_mt9v011_gain()
166 analogmult = 1; in calc_mt9v011_gain()
175 static void set_balance(struct v4l2_subdev *sd) in set_balance() argument
177 struct mt9v011 *core = to_mt9v011(sd); in set_balance()
182 exposure = core->exposure; in set_balance()
184 green_gain = calc_mt9v011_gain(core->global_gain); in set_balance()
186 bal = core->global_gain; in set_balance()
187 bal += (core->blue_bal * core->global_gain / (1 << 7)); in set_balance()
190 bal = core->global_gain; in set_balance()
191 bal += (core->red_bal * core->global_gain / (1 << 7)); in set_balance()
194 mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green_gain); in set_balance()
195 mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green_gain); in set_balance()
196 mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain); in set_balance()
197 mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain); in set_balance()
198 mt9v011_write(sd, R09_MT9V011_SHUTTER_WIDTH, exposure); in set_balance()
201 static void calc_fps(struct v4l2_subdev *sd, u32 *numerator, u32 *denominator) in calc_fps() argument
203 struct mt9v011 *core = to_mt9v011(sd); in calc_fps()
209 height = mt9v011_read(sd, R03_MT9V011_HEIGHT); in calc_fps()
210 width = mt9v011_read(sd, R04_MT9V011_WIDTH); in calc_fps()
211 hblank = mt9v011_read(sd, R05_MT9V011_HBLANK); in calc_fps()
212 vblank = mt9v011_read(sd, R06_MT9V011_VBLANK); in calc_fps()
213 speed = mt9v011_read(sd, R0A_MT9V011_CLK_SPEED); in calc_fps()
216 t_time = row_time * (height + vblank + 1); in calc_fps()
218 frames_per_ms = core->xtal * 1000l; in calc_fps()
222 v4l2_dbg(1, debug, sd, "Programmed to %u.%03u fps (%d pixel clcks)\n", in calc_fps()
231 static u16 calc_speed(struct v4l2_subdev *sd, u32 numerator, u32 denominator) in calc_speed() argument
233 struct mt9v011 *core = to_mt9v011(sd); in calc_speed()
242 height = mt9v011_read(sd, R03_MT9V011_HEIGHT); in calc_speed()
243 width = mt9v011_read(sd, R04_MT9V011_WIDTH); in calc_speed()
244 hblank = mt9v011_read(sd, R05_MT9V011_HBLANK); in calc_speed()
245 vblank = mt9v011_read(sd, R06_MT9V011_VBLANK); in calc_speed()
248 line_time = height + vblank + 1; in calc_speed()
250 t_time = core->xtal * ((u64)numerator); in calc_speed()
262 speed -= 2; in calc_speed()
271 static void set_res(struct v4l2_subdev *sd) in set_res() argument
273 struct mt9v011 *core = to_mt9v011(sd); in set_res()
283 * width-1. However, this doesn't work, since one pixel per line will in set_res()
287 hstart = 20 + (640 - core->width) / 2; in set_res()
288 mt9v011_write(sd, R02_MT9V011_COLSTART, hstart); in set_res()
289 mt9v011_write(sd, R04_MT9V011_WIDTH, core->width); in set_res()
290 mt9v011_write(sd, R05_MT9V011_HBLANK, 771 - core->width); in set_res()
292 vstart = 8 + (480 - core->height) / 2; in set_res()
293 mt9v011_write(sd, R01_MT9V011_ROWSTART, vstart); in set_res()
294 mt9v011_write(sd, R03_MT9V011_HEIGHT, core->height); in set_res()
295 mt9v011_write(sd, R06_MT9V011_VBLANK, 508 - core->height); in set_res()
297 calc_fps(sd, NULL, NULL); in set_res()
300 static void set_read_mode(struct v4l2_subdev *sd) in set_read_mode() argument
302 struct mt9v011 *core = to_mt9v011(sd); in set_read_mode()
305 if (core->hflip) in set_read_mode()
308 if (core->vflip) in set_read_mode()
311 mt9v011_write(sd, R20_MT9V011_READ_MODE, mode); in set_read_mode()
314 static int mt9v011_reset(struct v4l2_subdev *sd, u32 val) in mt9v011_reset() argument
319 mt9v011_write(sd, mt9v011_init_default[i].reg, in mt9v011_reset()
322 set_balance(sd); in mt9v011_reset()
323 set_res(sd); in mt9v011_reset()
324 set_read_mode(sd); in mt9v011_reset()
329 static int mt9v011_enum_mbus_code(struct v4l2_subdev *sd, in mt9v011_enum_mbus_code() argument
333 if (code->pad || code->index > 0) in mt9v011_enum_mbus_code()
334 return -EINVAL; in mt9v011_enum_mbus_code()
336 code->code = MEDIA_BUS_FMT_SGRBG8_1X8; in mt9v011_enum_mbus_code()
340 static int mt9v011_set_fmt(struct v4l2_subdev *sd, in mt9v011_set_fmt() argument
344 struct v4l2_mbus_framefmt *fmt = &format->format; in mt9v011_set_fmt()
345 struct mt9v011 *core = to_mt9v011(sd); in mt9v011_set_fmt()
347 if (format->pad || fmt->code != MEDIA_BUS_FMT_SGRBG8_1X8) in mt9v011_set_fmt()
348 return -EINVAL; in mt9v011_set_fmt()
350 v4l_bound_align_image(&fmt->width, 48, 639, 1, in mt9v011_set_fmt()
351 &fmt->height, 32, 480, 1, 0); in mt9v011_set_fmt()
352 fmt->field = V4L2_FIELD_NONE; in mt9v011_set_fmt()
353 fmt->colorspace = V4L2_COLORSPACE_SRGB; in mt9v011_set_fmt()
355 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) { in mt9v011_set_fmt()
356 core->width = fmt->width; in mt9v011_set_fmt()
357 core->height = fmt->height; in mt9v011_set_fmt()
359 set_res(sd); in mt9v011_set_fmt()
361 cfg->try_fmt = *fmt; in mt9v011_set_fmt()
367 static int mt9v011_g_frame_interval(struct v4l2_subdev *sd, in mt9v011_g_frame_interval() argument
370 calc_fps(sd, in mt9v011_g_frame_interval()
371 &ival->interval.numerator, in mt9v011_g_frame_interval()
372 &ival->interval.denominator); in mt9v011_g_frame_interval()
377 static int mt9v011_s_frame_interval(struct v4l2_subdev *sd, in mt9v011_s_frame_interval() argument
380 struct v4l2_fract *tpf = &ival->interval; in mt9v011_s_frame_interval()
383 speed = calc_speed(sd, tpf->numerator, tpf->denominator); in mt9v011_s_frame_interval()
385 mt9v011_write(sd, R0A_MT9V011_CLK_SPEED, speed); in mt9v011_s_frame_interval()
386 v4l2_dbg(1, debug, sd, "Setting speed to %d\n", speed); in mt9v011_s_frame_interval()
389 calc_fps(sd, &tpf->numerator, &tpf->denominator); in mt9v011_s_frame_interval()
395 static int mt9v011_g_register(struct v4l2_subdev *sd, in mt9v011_g_register() argument
398 reg->val = mt9v011_read(sd, reg->reg & 0xff); in mt9v011_g_register()
399 reg->size = 2; in mt9v011_g_register()
404 static int mt9v011_s_register(struct v4l2_subdev *sd, in mt9v011_s_register() argument
407 mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff); in mt9v011_s_register()
416 container_of(ctrl->handler, struct mt9v011, ctrls); in mt9v011_s_ctrl()
417 struct v4l2_subdev *sd = &core->sd; in mt9v011_s_ctrl() local
419 switch (ctrl->id) { in mt9v011_s_ctrl()
421 core->global_gain = ctrl->val; in mt9v011_s_ctrl()
424 core->exposure = ctrl->val; in mt9v011_s_ctrl()
427 core->red_bal = ctrl->val; in mt9v011_s_ctrl()
430 core->blue_bal = ctrl->val; in mt9v011_s_ctrl()
433 core->hflip = ctrl->val; in mt9v011_s_ctrl()
434 set_read_mode(sd); in mt9v011_s_ctrl()
437 core->vflip = ctrl->val; in mt9v011_s_ctrl()
438 set_read_mode(sd); in mt9v011_s_ctrl()
441 return -EINVAL; in mt9v011_s_ctrl()
444 set_balance(sd); in mt9v011_s_ctrl()
486 struct v4l2_subdev *sd; in mt9v011_probe() local
492 if (!i2c_check_functionality(c->adapter, in mt9v011_probe()
494 return -EIO; in mt9v011_probe()
496 core = devm_kzalloc(&c->dev, sizeof(struct mt9v011), GFP_KERNEL); in mt9v011_probe()
498 return -ENOMEM; in mt9v011_probe()
500 sd = &core->sd; in mt9v011_probe()
501 v4l2_i2c_subdev_init(sd, c, &mt9v011_ops); in mt9v011_probe()
504 core->pad.flags = MEDIA_PAD_FL_SOURCE; in mt9v011_probe()
505 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR; in mt9v011_probe()
507 ret = media_entity_pads_init(&sd->entity, 1, &core->pad); in mt9v011_probe()
513 version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION); in mt9v011_probe()
516 v4l2_info(sd, "*** unknown micron chip detected (0x%04x).\n", in mt9v011_probe()
518 return -EINVAL; in mt9v011_probe()
521 v4l2_ctrl_handler_init(&core->ctrls, 5); in mt9v011_probe()
522 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
523 V4L2_CID_GAIN, 0, (1 << 12) - 1 - 0x20, 1, 0x20); in mt9v011_probe()
524 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
525 V4L2_CID_EXPOSURE, 0, 2047, 1, 0x01fc); in mt9v011_probe()
526 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
527 V4L2_CID_RED_BALANCE, -(1 << 9), (1 << 9) - 1, 1, 0); in mt9v011_probe()
528 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
529 V4L2_CID_BLUE_BALANCE, -(1 << 9), (1 << 9) - 1, 1, 0); in mt9v011_probe()
530 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
531 V4L2_CID_HFLIP, 0, 1, 1, 0); in mt9v011_probe()
532 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
533 V4L2_CID_VFLIP, 0, 1, 1, 0); in mt9v011_probe()
535 if (core->ctrls.error) { in mt9v011_probe()
536 int ret = core->ctrls.error; in mt9v011_probe()
538 v4l2_err(sd, "control initialization error %d\n", ret); in mt9v011_probe()
539 v4l2_ctrl_handler_free(&core->ctrls); in mt9v011_probe()
542 core->sd.ctrl_handler = &core->ctrls; in mt9v011_probe()
544 core->global_gain = 0x0024; in mt9v011_probe()
545 core->exposure = 0x01fc; in mt9v011_probe()
546 core->width = 640; in mt9v011_probe()
547 core->height = 480; in mt9v011_probe()
548 core->xtal = 27000000; /* Hz */ in mt9v011_probe()
550 if (c->dev.platform_data) { in mt9v011_probe()
551 struct mt9v011_platform_data *pdata = c->dev.platform_data; in mt9v011_probe()
553 core->xtal = pdata->xtal; in mt9v011_probe()
554 v4l2_dbg(1, debug, sd, "xtal set to %d.%03d MHz\n", in mt9v011_probe()
555 core->xtal / 1000000, (core->xtal / 1000) % 1000); in mt9v011_probe()
558 v4l_info(c, "chip found @ 0x%02x (%s - chip version 0x%04x)\n", in mt9v011_probe()
559 c->addr << 1, c->adapter->name, version); in mt9v011_probe()
566 struct v4l2_subdev *sd = i2c_get_clientdata(c); in mt9v011_remove() local
567 struct mt9v011 *core = to_mt9v011(sd); in mt9v011_remove()
569 v4l2_dbg(1, debug, sd, in mt9v011_remove()
571 c->addr << 1); in mt9v011_remove()
573 v4l2_device_unregister_subdev(sd); in mt9v011_remove()
574 v4l2_ctrl_handler_free(&core->ctrls); in mt9v011_remove()
579 /* ----------------------------------------------------------------------- */