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
56 unsigned hflip:1;
57 unsigned vflip:1;
63 static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd) in to_mt9v011() argument
65 return container_of(sd, struct mt9v011, sd); in to_mt9v011()
68 static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr) in mt9v011_read() argument
70 struct i2c_client *c = v4l2_get_subdevdata(sd); in mt9v011_read()
74 rc = i2c_master_send(c, &addr, 1); in mt9v011_read()
75 if (rc != 1) in mt9v011_read()
76 v4l2_dbg(0, debug, sd, in mt9v011_read()
77 "i2c i/o error: rc == %d (should be 1)\n", rc); in mt9v011_read()
83 v4l2_dbg(0, debug, sd, in mt9v011_read()
88 v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val); in mt9v011_read()
93 static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr, in mt9v011_write() argument
96 struct i2c_client *c = v4l2_get_subdevdata(sd); in mt9v011_write()
101 buffer[1] = value >> 8; in mt9v011_write()
104 v4l2_dbg(2, debug, sd, in mt9v011_write()
108 v4l2_dbg(0, debug, sd, in mt9v011_write()
157 digitalgain = 1; in calc_mt9v011_gain()
164 analogmult = 1; in calc_mt9v011_gain()
173 static void set_balance(struct v4l2_subdev *sd) in set_balance() argument
175 struct mt9v011 *core = to_mt9v011(sd); in set_balance()
180 exposure = core->exposure; in set_balance()
182 green_gain = calc_mt9v011_gain(core->global_gain); in set_balance()
184 bal = core->global_gain; in set_balance()
185 bal += (core->blue_bal * core->global_gain / (1 << 7)); in set_balance()
188 bal = core->global_gain; in set_balance()
189 bal += (core->red_bal * core->global_gain / (1 << 7)); in set_balance()
192 mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green_gain); in set_balance()
193 mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green_gain); in set_balance()
194 mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain); in set_balance()
195 mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain); in set_balance()
196 mt9v011_write(sd, R09_MT9V011_SHUTTER_WIDTH, exposure); in set_balance()
199 static void calc_fps(struct v4l2_subdev *sd, u32 *numerator, u32 *denominator) in calc_fps() argument
201 struct mt9v011 *core = to_mt9v011(sd); in calc_fps()
207 height = mt9v011_read(sd, R03_MT9V011_HEIGHT); in calc_fps()
208 width = mt9v011_read(sd, R04_MT9V011_WIDTH); in calc_fps()
209 hblank = mt9v011_read(sd, R05_MT9V011_HBLANK); in calc_fps()
210 vblank = mt9v011_read(sd, R06_MT9V011_VBLANK); in calc_fps()
211 speed = mt9v011_read(sd, R0A_MT9V011_CLK_SPEED); in calc_fps()
214 t_time = row_time * (height + vblank + 1); in calc_fps()
216 frames_per_ms = core->xtal * 1000l; in calc_fps()
220 v4l2_dbg(1, debug, sd, "Programmed to %u.%03u fps (%d pixel clcks)\n", in calc_fps()
229 static u16 calc_speed(struct v4l2_subdev *sd, u32 numerator, u32 denominator) in calc_speed() argument
231 struct mt9v011 *core = to_mt9v011(sd); in calc_speed()
240 height = mt9v011_read(sd, R03_MT9V011_HEIGHT); in calc_speed()
241 width = mt9v011_read(sd, R04_MT9V011_WIDTH); in calc_speed()
242 hblank = mt9v011_read(sd, R05_MT9V011_HBLANK); in calc_speed()
243 vblank = mt9v011_read(sd, R06_MT9V011_VBLANK); in calc_speed()
246 line_time = height + vblank + 1; in calc_speed()
248 t_time = core->xtal * ((u64)numerator); in calc_speed()
260 speed -= 2; in calc_speed()
269 static void set_res(struct v4l2_subdev *sd) in set_res() argument
271 struct mt9v011 *core = to_mt9v011(sd); in set_res()
281 * width-1. However, this doesn't work, since one pixel per line will in set_res()
285 hstart = 20 + (640 - core->width) / 2; in set_res()
286 mt9v011_write(sd, R02_MT9V011_COLSTART, hstart); in set_res()
287 mt9v011_write(sd, R04_MT9V011_WIDTH, core->width); in set_res()
288 mt9v011_write(sd, R05_MT9V011_HBLANK, 771 - core->width); in set_res()
290 vstart = 8 + (480 - core->height) / 2; in set_res()
291 mt9v011_write(sd, R01_MT9V011_ROWSTART, vstart); in set_res()
292 mt9v011_write(sd, R03_MT9V011_HEIGHT, core->height); in set_res()
293 mt9v011_write(sd, R06_MT9V011_VBLANK, 508 - core->height); in set_res()
295 calc_fps(sd, NULL, NULL); in set_res()
298 static void set_read_mode(struct v4l2_subdev *sd) in set_read_mode() argument
300 struct mt9v011 *core = to_mt9v011(sd); in set_read_mode()
303 if (core->hflip) in set_read_mode()
306 if (core->vflip) in set_read_mode()
309 mt9v011_write(sd, R20_MT9V011_READ_MODE, mode); in set_read_mode()
312 static int mt9v011_reset(struct v4l2_subdev *sd, u32 val) in mt9v011_reset() argument
317 mt9v011_write(sd, mt9v011_init_default[i].reg, in mt9v011_reset()
320 set_balance(sd); in mt9v011_reset()
321 set_res(sd); in mt9v011_reset()
322 set_read_mode(sd); in mt9v011_reset()
327 static int mt9v011_enum_mbus_code(struct v4l2_subdev *sd, in mt9v011_enum_mbus_code() argument
331 if (code->pad || code->index > 0) in mt9v011_enum_mbus_code()
332 return -EINVAL; in mt9v011_enum_mbus_code()
334 code->code = MEDIA_BUS_FMT_SGRBG8_1X8; in mt9v011_enum_mbus_code()
338 static int mt9v011_set_fmt(struct v4l2_subdev *sd, in mt9v011_set_fmt() argument
342 struct v4l2_mbus_framefmt *fmt = &format->format; in mt9v011_set_fmt()
343 struct mt9v011 *core = to_mt9v011(sd); in mt9v011_set_fmt()
345 if (format->pad || fmt->code != MEDIA_BUS_FMT_SGRBG8_1X8) in mt9v011_set_fmt()
346 return -EINVAL; in mt9v011_set_fmt()
348 v4l_bound_align_image(&fmt->width, 48, 639, 1, in mt9v011_set_fmt()
349 &fmt->height, 32, 480, 1, 0); in mt9v011_set_fmt()
350 fmt->field = V4L2_FIELD_NONE; in mt9v011_set_fmt()
351 fmt->colorspace = V4L2_COLORSPACE_SRGB; in mt9v011_set_fmt()
353 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) { in mt9v011_set_fmt()
354 core->width = fmt->width; in mt9v011_set_fmt()
355 core->height = fmt->height; in mt9v011_set_fmt()
357 set_res(sd); in mt9v011_set_fmt()
365 static int mt9v011_get_frame_interval(struct v4l2_subdev *sd, in mt9v011_get_frame_interval() argument
373 if (ival->which != V4L2_SUBDEV_FORMAT_ACTIVE) in mt9v011_get_frame_interval()
374 return -EINVAL; in mt9v011_get_frame_interval()
376 calc_fps(sd, in mt9v011_get_frame_interval()
377 &ival->interval.numerator, in mt9v011_get_frame_interval()
378 &ival->interval.denominator); in mt9v011_get_frame_interval()
383 static int mt9v011_set_frame_interval(struct v4l2_subdev *sd, in mt9v011_set_frame_interval() argument
387 struct v4l2_fract *tpf = &ival->interval; in mt9v011_set_frame_interval()
394 if (ival->which != V4L2_SUBDEV_FORMAT_ACTIVE) in mt9v011_set_frame_interval()
395 return -EINVAL; in mt9v011_set_frame_interval()
397 speed = calc_speed(sd, tpf->numerator, tpf->denominator); in mt9v011_set_frame_interval()
399 mt9v011_write(sd, R0A_MT9V011_CLK_SPEED, speed); in mt9v011_set_frame_interval()
400 v4l2_dbg(1, debug, sd, "Setting speed to %d\n", speed); in mt9v011_set_frame_interval()
403 calc_fps(sd, &tpf->numerator, &tpf->denominator); in mt9v011_set_frame_interval()
409 static int mt9v011_g_register(struct v4l2_subdev *sd, in mt9v011_g_register() argument
412 reg->val = mt9v011_read(sd, reg->reg & 0xff); in mt9v011_g_register()
413 reg->size = 2; in mt9v011_g_register()
418 static int mt9v011_s_register(struct v4l2_subdev *sd, in mt9v011_s_register() argument
421 mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff); in mt9v011_s_register()
430 container_of(ctrl->handler, struct mt9v011, ctrls); in mt9v011_s_ctrl()
431 struct v4l2_subdev *sd = &core->sd; in mt9v011_s_ctrl() local
433 switch (ctrl->id) { in mt9v011_s_ctrl()
435 core->global_gain = ctrl->val; in mt9v011_s_ctrl()
438 core->exposure = ctrl->val; in mt9v011_s_ctrl()
441 core->red_bal = ctrl->val; in mt9v011_s_ctrl()
444 core->blue_bal = ctrl->val; in mt9v011_s_ctrl()
447 core->hflip = ctrl->val; in mt9v011_s_ctrl()
448 set_read_mode(sd); in mt9v011_s_ctrl()
451 core->vflip = ctrl->val; in mt9v011_s_ctrl()
452 set_read_mode(sd); in mt9v011_s_ctrl()
455 return -EINVAL; in mt9v011_s_ctrl()
458 set_balance(sd); in mt9v011_s_ctrl()
495 struct v4l2_subdev *sd; in mt9v011_probe() local
499 if (!i2c_check_functionality(c->adapter, in mt9v011_probe()
501 return -EIO; in mt9v011_probe()
503 core = devm_kzalloc(&c->dev, sizeof(struct mt9v011), GFP_KERNEL); in mt9v011_probe()
505 return -ENOMEM; in mt9v011_probe()
507 sd = &core->sd; in mt9v011_probe()
508 v4l2_i2c_subdev_init(sd, c, &mt9v011_ops); in mt9v011_probe()
510 core->pad.flags = MEDIA_PAD_FL_SOURCE; in mt9v011_probe()
511 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR; in mt9v011_probe()
513 ret = media_entity_pads_init(&sd->entity, 1, &core->pad); in mt9v011_probe()
518 version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION); in mt9v011_probe()
521 v4l2_info(sd, "*** unknown micron chip detected (0x%04x).\n", in mt9v011_probe()
523 return -EINVAL; in mt9v011_probe()
526 v4l2_ctrl_handler_init(&core->ctrls, 5); in mt9v011_probe()
527 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
528 V4L2_CID_GAIN, 0, (1 << 12) - 1 - 0x20, 1, 0x20); in mt9v011_probe()
529 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
530 V4L2_CID_EXPOSURE, 0, 2047, 1, 0x01fc); in mt9v011_probe()
531 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
532 V4L2_CID_RED_BALANCE, -(1 << 9), (1 << 9) - 1, 1, 0); in mt9v011_probe()
533 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
534 V4L2_CID_BLUE_BALANCE, -(1 << 9), (1 << 9) - 1, 1, 0); in mt9v011_probe()
535 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
536 V4L2_CID_HFLIP, 0, 1, 1, 0); in mt9v011_probe()
537 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops, in mt9v011_probe()
538 V4L2_CID_VFLIP, 0, 1, 1, 0); in mt9v011_probe()
540 if (core->ctrls.error) { in mt9v011_probe()
541 int ret = core->ctrls.error; in mt9v011_probe()
543 v4l2_err(sd, "control initialization error %d\n", ret); in mt9v011_probe()
544 v4l2_ctrl_handler_free(&core->ctrls); in mt9v011_probe()
547 core->sd.ctrl_handler = &core->ctrls; in mt9v011_probe()
549 core->global_gain = 0x0024; in mt9v011_probe()
550 core->exposure = 0x01fc; in mt9v011_probe()
551 core->width = 640; in mt9v011_probe()
552 core->height = 480; in mt9v011_probe()
553 core->xtal = 27000000; /* Hz */ in mt9v011_probe()
555 if (c->dev.platform_data) { in mt9v011_probe()
556 struct mt9v011_platform_data *pdata = c->dev.platform_data; in mt9v011_probe()
558 core->xtal = pdata->xtal; in mt9v011_probe()
559 v4l2_dbg(1, debug, sd, "xtal set to %d.%03d MHz\n", in mt9v011_probe()
560 core->xtal / 1000000, (core->xtal / 1000) % 1000); in mt9v011_probe()
563 v4l_info(c, "chip found @ 0x%02x (%s - chip version 0x%04x)\n", in mt9v011_probe()
564 c->addr << 1, c->adapter->name, version); in mt9v011_probe()
571 struct v4l2_subdev *sd = i2c_get_clientdata(c); in mt9v011_remove() local
572 struct mt9v011 *core = to_mt9v011(sd); in mt9v011_remove()
574 v4l2_dbg(1, debug, sd, in mt9v011_remove()
576 c->addr << 1); in mt9v011_remove()
578 v4l2_device_unregister_subdev(sd); in mt9v011_remove()
579 v4l2_ctrl_handler_free(&core->ctrls); in mt9v011_remove()
582 /* ----------------------------------------------------------------------- */