Lines Matching refs:imx334
125 struct imx334 { struct
489 static inline struct imx334 *to_imx334(struct v4l2_subdev *subdev) in to_imx334()
491 return container_of(subdev, struct imx334, sd); in to_imx334()
505 static int imx334_read_reg(struct imx334 *imx334, u16 reg, u32 len, u32 *val) in imx334_read_reg() argument
507 struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd); in imx334_read_reg()
550 static int imx334_write_reg(struct imx334 *imx334, u16 reg, u32 len, u32 val) in imx334_write_reg() argument
552 struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd); in imx334_write_reg()
574 static int imx334_write_regs(struct imx334 *imx334, in imx334_write_regs() argument
581 ret = imx334_write_reg(imx334, regs[i].address, 1, regs[i].val); in imx334_write_regs()
596 static int imx334_update_controls(struct imx334 *imx334, in imx334_update_controls() argument
601 ret = __v4l2_ctrl_s_ctrl(imx334->link_freq_ctrl, mode->link_freq_idx); in imx334_update_controls()
605 ret = __v4l2_ctrl_modify_range(imx334->pclk_ctrl, mode->pclk, in imx334_update_controls()
610 ret = __v4l2_ctrl_modify_range(imx334->hblank_ctrl, mode->hblank, in imx334_update_controls()
615 ret = __v4l2_ctrl_modify_range(imx334->vblank_ctrl, mode->vblank_min, in imx334_update_controls()
620 return __v4l2_ctrl_s_ctrl(imx334->vblank_ctrl, mode->vblank); in imx334_update_controls()
631 static int imx334_update_exp_gain(struct imx334 *imx334, u32 exposure, u32 gain) in imx334_update_exp_gain() argument
636 lpfr = imx334->vblank + imx334->cur_mode->height; in imx334_update_exp_gain()
639 dev_dbg(imx334->dev, "Set long exp %u analog gain %u sh0 %u lpfr %u", in imx334_update_exp_gain()
642 ret = imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 1); in imx334_update_exp_gain()
646 ret = imx334_write_reg(imx334, IMX334_REG_LPFR, 3, lpfr); in imx334_update_exp_gain()
650 ret = imx334_write_reg(imx334, IMX334_REG_SHUTTER, 3, shutter); in imx334_update_exp_gain()
654 ret = imx334_write_reg(imx334, IMX334_REG_AGAIN, 1, gain); in imx334_update_exp_gain()
657 imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 0); in imx334_update_exp_gain()
676 struct imx334 *imx334 = in imx334_set_ctrl() local
677 container_of(ctrl->handler, struct imx334, ctrl_handler); in imx334_set_ctrl()
684 imx334->vblank = imx334->vblank_ctrl->val; in imx334_set_ctrl()
686 dev_dbg(imx334->dev, "Received vblank %u, new lpfr %u", in imx334_set_ctrl()
687 imx334->vblank, in imx334_set_ctrl()
688 imx334->vblank + imx334->cur_mode->height); in imx334_set_ctrl()
690 ret = __v4l2_ctrl_modify_range(imx334->exp_ctrl, in imx334_set_ctrl()
692 imx334->vblank + in imx334_set_ctrl()
693 imx334->cur_mode->height - in imx334_set_ctrl()
700 if (!pm_runtime_get_if_in_use(imx334->dev)) in imx334_set_ctrl()
704 analog_gain = imx334->again_ctrl->val; in imx334_set_ctrl()
706 dev_dbg(imx334->dev, "Received exp %u analog gain %u", in imx334_set_ctrl()
709 ret = imx334_update_exp_gain(imx334, exposure, analog_gain); in imx334_set_ctrl()
711 pm_runtime_put(imx334->dev); in imx334_set_ctrl()
720 dev_err(imx334->dev, "Invalid control %d", ctrl->id); in imx334_set_ctrl()
732 static int imx334_get_format_code(struct imx334 *imx334, u32 code) in imx334_get_format_code() argument
776 struct imx334 *imx334 = to_imx334(sd); in imx334_enum_frame_size() local
782 code = imx334_get_format_code(imx334, fsize->code); in imx334_enum_frame_size()
802 static void imx334_fill_pad_format(struct imx334 *imx334, in imx334_fill_pad_format() argument
827 struct imx334 *imx334 = to_imx334(sd); in imx334_get_pad_format() local
829 mutex_lock(&imx334->mutex); in imx334_get_pad_format()
837 fmt->format.code = imx334->cur_code; in imx334_get_pad_format()
838 imx334_fill_pad_format(imx334, imx334->cur_mode, fmt); in imx334_get_pad_format()
841 mutex_unlock(&imx334->mutex); in imx334_get_pad_format()
858 struct imx334 *imx334 = to_imx334(sd); in imx334_set_pad_format() local
862 mutex_lock(&imx334->mutex); in imx334_set_pad_format()
869 imx334_fill_pad_format(imx334, mode, fmt); in imx334_set_pad_format()
870 fmt->format.code = imx334_get_format_code(imx334, fmt->format.code); in imx334_set_pad_format()
877 } else if (imx334->cur_mode != mode || imx334->cur_code != fmt->format.code) { in imx334_set_pad_format()
878 imx334->cur_code = fmt->format.code; in imx334_set_pad_format()
879 ret = imx334_update_controls(imx334, mode); in imx334_set_pad_format()
881 imx334->cur_mode = mode; in imx334_set_pad_format()
884 mutex_unlock(&imx334->mutex); in imx334_set_pad_format()
899 struct imx334 *imx334 = to_imx334(sd); in imx334_init_pad_cfg() local
904 mutex_lock(&imx334->mutex); in imx334_init_pad_cfg()
906 imx334_fill_pad_format(imx334, imx334->cur_mode, &fmt); in imx334_init_pad_cfg()
908 __v4l2_ctrl_modify_range(imx334->link_freq_ctrl, 0, in imx334_init_pad_cfg()
909 __fls(imx334->menu_skip_mask), in imx334_init_pad_cfg()
910 ~(imx334->menu_skip_mask), in imx334_init_pad_cfg()
911 __ffs(imx334->menu_skip_mask)); in imx334_init_pad_cfg()
913 mutex_unlock(&imx334->mutex); in imx334_init_pad_cfg()
918 static int imx334_set_framefmt(struct imx334 *imx334) in imx334_set_framefmt() argument
920 switch (imx334->cur_code) { in imx334_set_framefmt()
922 return imx334_write_regs(imx334, raw10_framefmt_regs, in imx334_set_framefmt()
926 return imx334_write_regs(imx334, raw12_framefmt_regs, in imx334_set_framefmt()
939 static int imx334_start_streaming(struct imx334 *imx334) in imx334_start_streaming() argument
945 reg_list = &imx334->cur_mode->reg_list; in imx334_start_streaming()
946 ret = imx334_write_regs(imx334, reg_list->regs, in imx334_start_streaming()
949 dev_err(imx334->dev, "fail to write initial registers"); in imx334_start_streaming()
953 ret = imx334_set_framefmt(imx334); in imx334_start_streaming()
955 dev_err(imx334->dev, "%s failed to set frame format: %d\n", in imx334_start_streaming()
961 ret = __v4l2_ctrl_handler_setup(imx334->sd.ctrl_handler); in imx334_start_streaming()
963 dev_err(imx334->dev, "fail to setup handler"); in imx334_start_streaming()
968 ret = imx334_write_reg(imx334, IMX334_REG_MODE_SELECT, in imx334_start_streaming()
971 dev_err(imx334->dev, "fail to start streaming"); in imx334_start_streaming()
984 static int imx334_stop_streaming(struct imx334 *imx334) in imx334_stop_streaming() argument
986 return imx334_write_reg(imx334, IMX334_REG_MODE_SELECT, in imx334_stop_streaming()
999 struct imx334 *imx334 = to_imx334(sd); in imx334_set_stream() local
1002 mutex_lock(&imx334->mutex); in imx334_set_stream()
1004 if (imx334->streaming == enable) { in imx334_set_stream()
1005 mutex_unlock(&imx334->mutex); in imx334_set_stream()
1010 ret = pm_runtime_resume_and_get(imx334->dev); in imx334_set_stream()
1014 ret = imx334_start_streaming(imx334); in imx334_set_stream()
1018 imx334_stop_streaming(imx334); in imx334_set_stream()
1019 pm_runtime_put(imx334->dev); in imx334_set_stream()
1022 imx334->streaming = enable; in imx334_set_stream()
1024 mutex_unlock(&imx334->mutex); in imx334_set_stream()
1029 pm_runtime_put(imx334->dev); in imx334_set_stream()
1031 mutex_unlock(&imx334->mutex); in imx334_set_stream()
1042 static int imx334_detect(struct imx334 *imx334) in imx334_detect() argument
1047 ret = imx334_read_reg(imx334, IMX334_REG_ID, 2, &val); in imx334_detect()
1052 dev_err(imx334->dev, "chip id mismatch: %x!=%x", in imx334_detect()
1066 static int imx334_parse_hw_config(struct imx334 *imx334) in imx334_parse_hw_config() argument
1068 struct fwnode_handle *fwnode = dev_fwnode(imx334->dev); in imx334_parse_hw_config()
1081 imx334->reset_gpio = devm_gpiod_get_optional(imx334->dev, "reset", in imx334_parse_hw_config()
1083 if (IS_ERR(imx334->reset_gpio)) { in imx334_parse_hw_config()
1084 dev_err(imx334->dev, "failed to get reset gpio %ld", in imx334_parse_hw_config()
1085 PTR_ERR(imx334->reset_gpio)); in imx334_parse_hw_config()
1086 return PTR_ERR(imx334->reset_gpio); in imx334_parse_hw_config()
1090 imx334->inclk = devm_clk_get(imx334->dev, NULL); in imx334_parse_hw_config()
1091 if (IS_ERR(imx334->inclk)) { in imx334_parse_hw_config()
1092 dev_err(imx334->dev, "could not get inclk"); in imx334_parse_hw_config()
1093 return PTR_ERR(imx334->inclk); in imx334_parse_hw_config()
1096 rate = clk_get_rate(imx334->inclk); in imx334_parse_hw_config()
1098 dev_err(imx334->dev, "inclk frequency mismatch"); in imx334_parse_hw_config()
1112 dev_err(imx334->dev, in imx334_parse_hw_config()
1120 dev_err(imx334->dev, "no link frequencies defined"); in imx334_parse_hw_config()
1128 set_bit(j, &imx334->menu_skip_mask); in imx334_parse_hw_config()
1134 ret = dev_err_probe(imx334->dev, -EINVAL, in imx334_parse_hw_config()
1173 struct imx334 *imx334 = to_imx334(sd); in imx334_power_on() local
1176 gpiod_set_value_cansleep(imx334->reset_gpio, 1); in imx334_power_on()
1178 ret = clk_prepare_enable(imx334->inclk); in imx334_power_on()
1180 dev_err(imx334->dev, "fail to enable inclk"); in imx334_power_on()
1189 gpiod_set_value_cansleep(imx334->reset_gpio, 0); in imx334_power_on()
1203 struct imx334 *imx334 = to_imx334(sd); in imx334_power_off() local
1205 gpiod_set_value_cansleep(imx334->reset_gpio, 0); in imx334_power_off()
1207 clk_disable_unprepare(imx334->inclk); in imx334_power_off()
1218 static int imx334_init_controls(struct imx334 *imx334) in imx334_init_controls() argument
1220 struct v4l2_ctrl_handler *ctrl_hdlr = &imx334->ctrl_handler; in imx334_init_controls()
1221 const struct imx334_mode *mode = imx334->cur_mode; in imx334_init_controls()
1230 ctrl_hdlr->lock = &imx334->mutex; in imx334_init_controls()
1234 imx334->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx334_init_controls()
1242 imx334->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx334_init_controls()
1250 v4l2_ctrl_cluster(2, &imx334->exp_ctrl); in imx334_init_controls()
1252 imx334->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx334_init_controls()
1260 imx334->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx334_init_controls()
1266 imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, in imx334_init_controls()
1269 __fls(imx334->menu_skip_mask), in imx334_init_controls()
1270 __ffs(imx334->menu_skip_mask), in imx334_init_controls()
1273 if (imx334->link_freq_ctrl) in imx334_init_controls()
1274 imx334->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx334_init_controls()
1276 imx334->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx334_init_controls()
1282 if (imx334->hblank_ctrl) in imx334_init_controls()
1283 imx334->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx334_init_controls()
1286 dev_err(imx334->dev, "control init failed: %d", in imx334_init_controls()
1292 imx334->sd.ctrl_handler = ctrl_hdlr; in imx334_init_controls()
1305 struct imx334 *imx334; in imx334_probe() local
1308 imx334 = devm_kzalloc(&client->dev, sizeof(*imx334), GFP_KERNEL); in imx334_probe()
1309 if (!imx334) in imx334_probe()
1312 imx334->dev = &client->dev; in imx334_probe()
1315 v4l2_i2c_subdev_init(&imx334->sd, client, &imx334_subdev_ops); in imx334_probe()
1317 ret = imx334_parse_hw_config(imx334); in imx334_probe()
1319 dev_err(imx334->dev, "HW configuration is not supported"); in imx334_probe()
1323 mutex_init(&imx334->mutex); in imx334_probe()
1325 ret = imx334_power_on(imx334->dev); in imx334_probe()
1327 dev_err(imx334->dev, "failed to power-on the sensor"); in imx334_probe()
1332 ret = imx334_detect(imx334); in imx334_probe()
1334 dev_err(imx334->dev, "failed to find sensor: %d", ret); in imx334_probe()
1339 imx334->cur_mode = &supported_modes[__ffs(imx334->menu_skip_mask)]; in imx334_probe()
1340 imx334->cur_code = imx334_mbus_codes[0]; in imx334_probe()
1341 imx334->vblank = imx334->cur_mode->vblank; in imx334_probe()
1343 ret = imx334_init_controls(imx334); in imx334_probe()
1345 dev_err(imx334->dev, "failed to init controls: %d", ret); in imx334_probe()
1350 imx334->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in imx334_probe()
1351 imx334->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in imx334_probe()
1354 imx334->pad.flags = MEDIA_PAD_FL_SOURCE; in imx334_probe()
1355 ret = media_entity_pads_init(&imx334->sd.entity, 1, &imx334->pad); in imx334_probe()
1357 dev_err(imx334->dev, "failed to init entity pads: %d", ret); in imx334_probe()
1361 ret = v4l2_async_register_subdev_sensor(&imx334->sd); in imx334_probe()
1363 dev_err(imx334->dev, in imx334_probe()
1368 pm_runtime_set_active(imx334->dev); in imx334_probe()
1369 pm_runtime_enable(imx334->dev); in imx334_probe()
1370 pm_runtime_idle(imx334->dev); in imx334_probe()
1375 media_entity_cleanup(&imx334->sd.entity); in imx334_probe()
1377 v4l2_ctrl_handler_free(imx334->sd.ctrl_handler); in imx334_probe()
1379 imx334_power_off(imx334->dev); in imx334_probe()
1381 mutex_destroy(&imx334->mutex); in imx334_probe()
1395 struct imx334 *imx334 = to_imx334(sd); in imx334_remove() local
1404 mutex_destroy(&imx334->mutex); in imx334_remove()