Lines Matching full:sensor

141 static int ar0521_write_regs(struct ar0521_dev *sensor, const __be16 *data,  in ar0521_write_regs()  argument
144 struct i2c_client *client = sensor->i2c_client; in ar0521_write_regs()
156 v4l2_err(&sensor->sd, "%s: I2C write error\n", __func__); in ar0521_write_regs()
163 static int ar0521_write_reg(struct ar0521_dev *sensor, u16 reg, u16 val) in ar0521_write_reg() argument
167 return ar0521_write_regs(sensor, buf, 2); in ar0521_write_reg()
170 static int ar0521_set_geometry(struct ar0521_dev *sensor) in ar0521_set_geometry() argument
173 u16 x = (AR0521_WIDTH_MAX - sensor->fmt.width) / 2; in ar0521_set_geometry()
174 u16 y = ((AR0521_HEIGHT_MAX - sensor->fmt.height) / 2) & ~1; in ar0521_set_geometry()
177 be(sensor->total_height), in ar0521_set_geometry()
178 be(sensor->total_width), in ar0521_set_geometry()
181 be(x + sensor->fmt.width - 1), in ar0521_set_geometry()
182 be(y + sensor->fmt.height - 1), in ar0521_set_geometry()
183 be(sensor->fmt.width), in ar0521_set_geometry()
184 be(sensor->fmt.height) in ar0521_set_geometry()
187 return ar0521_write_regs(sensor, regs, ARRAY_SIZE(regs)); in ar0521_set_geometry()
190 static int ar0521_set_gains(struct ar0521_dev *sensor) in ar0521_set_gains() argument
192 int green = sensor->ctrls.gain->val; in ar0521_set_gains()
193 int red = max(green + sensor->ctrls.red_balance->val, 0); in ar0521_set_gains()
194 int blue = max(green + sensor->ctrls.blue_balance->val, 0); in ar0521_set_gains()
208 return ar0521_write_regs(sensor, regs, ARRAY_SIZE(regs)); in ar0521_set_gains()
211 static u32 calc_pll(struct ar0521_dev *sensor, int num, u32 freq, u16 *pre_ptr, in calc_pll() argument
220 sensor->extclk_freq); in calc_pll()
226 if (sensor->extclk_freq * (u64)new_mult < AR0521_PLL_MIN * in calc_pll()
229 if (sensor->extclk_freq * (u64)new_mult > AR0521_PLL_MAX * in calc_pll()
232 new_pll = div64_round_up(sensor->extclk_freq * (u64)new_mult, in calc_pll()
241 pll = div64_round(sensor->extclk_freq * (u64)mult, pre); in calc_pll()
248 static void ar0521_calc_mode(struct ar0521_dev *sensor) in ar0521_calc_mode() argument
250 unsigned int speed_mod = 4 / sensor->lane_count; /* 1 with 4 DDR lanes */ in ar0521_calc_mode()
251 u16 total_width = max(sensor->fmt.width + AR0521_WIDTH_BLANKING_MIN, in ar0521_calc_mode()
253 u16 total_height = sensor->fmt.height + AR0521_HEIGHT_BLANKING_MIN; in ar0521_calc_mode()
259 pix_clk = calc_pll(sensor, 1, pix_clk * (DIV / 2), &sensor->pll_pre, in ar0521_calc_mode()
260 &sensor->pll_mult); in ar0521_calc_mode()
262 calc_pll(sensor, 2, pix_clk * (DIV / 2) * speed_mod, &sensor->pll_pre2, in ar0521_calc_mode()
263 &sensor->pll_mult2); in ar0521_calc_mode()
265 sensor->total_width = total_width; in ar0521_calc_mode()
266 sensor->total_height = total_height; in ar0521_calc_mode()
269 static int ar0521_write_mode(struct ar0521_dev *sensor) in ar0521_write_mode() argument
275 /* 0x304 */ be((sensor->pll_pre2 << 8) | sensor->pll_pre), in ar0521_write_mode()
276 /* 0x306 */ be((sensor->pll_mult2 << 8) | sensor->pll_mult), in ar0521_write_mode()
283 ret = ar0521_write_reg(sensor, AR0521_REG_RESET, in ar0521_write_mode()
288 ret = ar0521_set_geometry(sensor); in ar0521_write_mode()
292 ret = ar0521_write_regs(sensor, pll_regs, ARRAY_SIZE(pll_regs)); in ar0521_write_mode()
296 ret = ar0521_write_reg(sensor, AR0521_REG_COARSE_INTEGRATION_TIME, in ar0521_write_mode()
297 sensor->ctrls.exposure->val); in ar0521_write_mode()
301 ret = ar0521_write_reg(sensor, AR0521_REG_RESET, in ar0521_write_mode()
307 ret = ar0521_write_reg(sensor, AR0521_REG_TEST_PATTERN_MODE, in ar0521_write_mode()
308 sensor->ctrls.test_pattern->val); in ar0521_write_mode()
312 static int ar0521_set_stream(struct ar0521_dev *sensor, bool on) in ar0521_set_stream() argument
317 ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev); in ar0521_set_stream()
321 ar0521_calc_mode(sensor); in ar0521_set_stream()
322 ret = ar0521_write_mode(sensor); in ar0521_set_stream()
326 ret = ar0521_set_gains(sensor); in ar0521_set_stream()
331 ret = ar0521_write_reg(sensor, AR0521_REG_HISPI_CONTROL_STATUS, in ar0521_set_stream()
337 ret = ar0521_write_reg(sensor, AR0521_REG_RESET, in ar0521_set_stream()
346 pm_runtime_put(&sensor->i2c_client->dev); in ar0521_set_stream()
351 * Reset gain, the sensor may produce all white pixels without in ar0521_set_stream()
354 ret = ar0521_write_reg(sensor, AR0521_REG_GLOBAL_GAIN, 0x2000); in ar0521_set_stream()
359 ret = ar0521_write_reg(sensor, AR0521_REG_RESET, in ar0521_set_stream()
364 pm_runtime_put(&sensor->i2c_client->dev); in ar0521_set_stream()
387 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_get_fmt() local
390 mutex_lock(&sensor->lock); in ar0521_get_fmt()
393 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state, 0 in ar0521_get_fmt()
396 fmt = &sensor->fmt; in ar0521_get_fmt()
400 mutex_unlock(&sensor->lock); in ar0521_get_fmt()
408 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_set_fmt() local
412 mutex_lock(&sensor->lock); in ar0521_set_fmt()
420 sensor->fmt = format->format; in ar0521_set_fmt()
421 ar0521_calc_mode(sensor); in ar0521_set_fmt()
424 mutex_unlock(&sensor->lock); in ar0521_set_fmt()
431 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_s_ctrl() local
439 sensor->total_width = sensor->fmt.width + in ar0521_s_ctrl()
440 sensor->ctrls.hblank->val; in ar0521_s_ctrl()
441 sensor->total_height = sensor->fmt.width + in ar0521_s_ctrl()
442 sensor->ctrls.vblank->val; in ar0521_s_ctrl()
449 /* access the sensor only if it's powered up */ in ar0521_s_ctrl()
450 if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev)) in ar0521_s_ctrl()
456 ret = ar0521_set_geometry(sensor); in ar0521_s_ctrl()
461 ret = ar0521_set_gains(sensor); in ar0521_s_ctrl()
464 ret = ar0521_write_reg(sensor, in ar0521_s_ctrl()
469 ret = ar0521_write_reg(sensor, AR0521_REG_TEST_PATTERN_MODE, in ar0521_s_ctrl()
474 pm_runtime_put(&sensor->i2c_client->dev); in ar0521_s_ctrl()
489 static int ar0521_init_controls(struct ar0521_dev *sensor) in ar0521_init_controls() argument
492 struct ar0521_ctrls *ctrls = &sensor->ctrls; in ar0521_init_controls()
499 hdl->lock = &sensor->lock; in ar0521_init_controls()
537 sensor->sd.ctrl_handler = hdl; in ar0521_init_controls()
715 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_power_off() local
718 clk_disable_unprepare(sensor->extclk); in ar0521_power_off()
720 if (sensor->reset_gpio) in ar0521_power_off()
721 gpiod_set_value(sensor->reset_gpio, 1); /* assert RESET signal */ in ar0521_power_off()
724 if (sensor->supplies[i]) in ar0521_power_off()
725 regulator_disable(sensor->supplies[i]); in ar0521_power_off()
733 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_power_on() local
738 if (sensor->supplies[cnt]) { in ar0521_power_on()
739 ret = regulator_enable(sensor->supplies[cnt]); in ar0521_power_on()
746 ret = clk_prepare_enable(sensor->extclk); in ar0521_power_on()
748 v4l2_err(&sensor->sd, "error enabling sensor clock\n"); in ar0521_power_on()
753 if (sensor->reset_gpio) in ar0521_power_on()
755 gpiod_set_value(sensor->reset_gpio, 0); in ar0521_power_on()
759 ret = ar0521_write_regs(sensor, initial_regs[cnt].data, in ar0521_power_on()
765 ret = ar0521_write_reg(sensor, AR0521_REG_SERIAL_FORMAT, in ar0521_power_on()
767 sensor->lane_count); in ar0521_power_on()
772 ret = ar0521_write_reg(sensor, AR0521_REG_HISPI_TEST_MODE, in ar0521_power_on()
773 ((0x40 << sensor->lane_count) - 0x40) | in ar0521_power_on()
778 ret = ar0521_write_reg(sensor, AR0521_REG_ROW_SPEED, 0x110 | in ar0521_power_on()
779 4 / sensor->lane_count); in ar0521_power_on()
793 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_enum_mbus_code() local
798 code->code = sensor->fmt.code; in ar0521_enum_mbus_code()
804 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_pre_streamon() local
810 ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev); in ar0521_pre_streamon()
815 ret = ar0521_write_reg(sensor, AR0521_REG_HISPI_CONTROL_STATUS, in ar0521_pre_streamon()
821 ret = ar0521_write_reg(sensor, AR0521_REG_RESET, in ar0521_pre_streamon()
829 pm_runtime_put(&sensor->i2c_client->dev); in ar0521_pre_streamon()
835 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_post_streamoff() local
837 pm_runtime_put(&sensor->i2c_client->dev); in ar0521_post_streamoff()
843 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_s_stream() local
846 mutex_lock(&sensor->lock); in ar0521_s_stream()
848 ret = ar0521_set_stream(sensor, enable); in ar0521_s_stream()
850 sensor->streaming = enable; in ar0521_s_stream()
852 mutex_unlock(&sensor->lock); in ar0521_s_stream()
881 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_suspend() local
883 if (sensor->streaming) in ar0521_suspend()
884 ar0521_set_stream(sensor, 0); in ar0521_suspend()
892 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_resume() local
894 if (sensor->streaming) in ar0521_resume()
895 return ar0521_set_stream(sensor, 1); in ar0521_resume()
907 struct ar0521_dev *sensor; in ar0521_probe() local
911 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); in ar0521_probe()
912 if (!sensor) in ar0521_probe()
915 sensor->i2c_client = client; in ar0521_probe()
916 sensor->fmt.width = AR0521_WIDTH_MAX; in ar0521_probe()
917 sensor->fmt.height = AR0521_HEIGHT_MAX; in ar0521_probe()
938 sensor->lane_count = ep.bus.mipi_csi2.num_data_lanes; in ar0521_probe()
939 switch (sensor->lane_count) { in ar0521_probe()
950 sensor->extclk = devm_clk_get(dev, "extclk"); in ar0521_probe()
951 if (IS_ERR(sensor->extclk)) { in ar0521_probe()
953 return PTR_ERR(sensor->extclk); in ar0521_probe()
956 sensor->extclk_freq = clk_get_rate(sensor->extclk); in ar0521_probe()
958 if (sensor->extclk_freq < AR0521_EXTCLK_MIN || in ar0521_probe()
959 sensor->extclk_freq > AR0521_EXTCLK_MAX) { in ar0521_probe()
961 sensor->extclk_freq); in ar0521_probe()
966 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", in ar0521_probe()
969 v4l2_i2c_subdev_init(&sensor->sd, client, &ar0521_subdev_ops); in ar0521_probe()
971 sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; in ar0521_probe()
972 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; in ar0521_probe()
973 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in ar0521_probe()
974 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); in ar0521_probe()
987 sensor->supplies[cnt] = supply; in ar0521_probe()
990 mutex_init(&sensor->lock); in ar0521_probe()
992 ret = ar0521_init_controls(sensor); in ar0521_probe()
996 ar0521_adj_fmt(&sensor->fmt); in ar0521_probe()
998 ret = v4l2_async_register_subdev(&sensor->sd); in ar0521_probe()
1012 v4l2_async_unregister_subdev(&sensor->sd); in ar0521_probe()
1013 media_entity_cleanup(&sensor->sd.entity); in ar0521_probe()
1015 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ar0521_probe()
1017 media_entity_cleanup(&sensor->sd.entity); in ar0521_probe()
1018 mutex_destroy(&sensor->lock); in ar0521_probe()
1025 struct ar0521_dev *sensor = to_ar0521_dev(sd); in ar0521_remove() local
1027 v4l2_async_unregister_subdev(&sensor->sd); in ar0521_remove()
1028 media_entity_cleanup(&sensor->sd.entity); in ar0521_remove()
1029 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ar0521_remove()
1034 mutex_destroy(&sensor->lock); in ar0521_remove()