1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sony imx334 sensor driver
5 * Copyright (C) 2021 Intel Corporation
7 #include <asm/unaligned.h>
10 #include <linux/delay.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
15 #include <media/v4l2-ctrls.h>
16 #include <media/v4l2-fwnode.h>
17 #include <media/v4l2-subdev.h>
20 #define IMX334_REG_MODE_SELECT 0x3000
21 #define IMX334_MODE_STANDBY 0x01
22 #define IMX334_MODE_STREAMING 0x00
25 #define IMX334_REG_LPFR 0x3030
28 #define IMX334_REG_ID 0x3044
29 #define IMX334_ID 0x1e
31 /* Exposure control */
32 #define IMX334_REG_SHUTTER 0x3058
33 #define IMX334_EXPOSURE_MIN 1
34 #define IMX334_EXPOSURE_OFFSET 5
35 #define IMX334_EXPOSURE_STEP 1
36 #define IMX334_EXPOSURE_DEFAULT 0x0648
38 /* Analog gain control */
39 #define IMX334_REG_AGAIN 0x30e8
40 #define IMX334_AGAIN_MIN 0
41 #define IMX334_AGAIN_MAX 240
42 #define IMX334_AGAIN_STEP 1
43 #define IMX334_AGAIN_DEFAULT 0
45 /* Group hold register */
46 #define IMX334_REG_HOLD 0x3001
48 /* Input clock rate */
49 #define IMX334_INCLK_RATE 24000000
51 /* CSI2 HW configuration */
52 #define IMX334_LINK_FREQ 891000000
53 #define IMX334_NUM_DATA_LANES 4
55 #define IMX334_REG_MIN 0x00
56 #define IMX334_REG_MAX 0xfffff
59 * struct imx334_reg - imx334 sensor register
60 * @address: Register address
61 * @val: Register value
69 * struct imx334_reg_list - imx334 sensor register list
70 * @num_of_regs: Number of registers in the list
71 * @regs: Pointer to register list
73 struct imx334_reg_list {
75 const struct imx334_reg *regs;
79 * struct imx334_mode - imx334 sensor mode structure
81 * @height: Frame height
83 * @hblank: Horizontal blanking in lines
84 * @vblank: Vertical blanking in lines
85 * @vblank_min: Minimal vertical blanking in lines
86 * @vblank_max: Maximum vertical blanking in lines
87 * @pclk: Sensor pixel clock
88 * @link_freq_idx: Link frequency index
89 * @reg_list: Register list for sensor mode
101 struct imx334_reg_list reg_list;
105 * struct imx334 - imx334 sensor device structure
106 * @dev: Pointer to generic device
107 * @client: Pointer to i2c client
108 * @sd: V4L2 sub-device
109 * @pad: Media pad. Only one pad supported
110 * @reset_gpio: Sensor reset gpio
111 * @inclk: Sensor input clock
112 * @ctrl_handler: V4L2 control handler
113 * @link_freq_ctrl: Pointer to link frequency control
114 * @pclk_ctrl: Pointer to pixel clock control
115 * @hblank_ctrl: Pointer to horizontal blanking control
116 * @vblank_ctrl: Pointer to vertical blanking control
117 * @exp_ctrl: Pointer to exposure control
118 * @again_ctrl: Pointer to analog gain control
119 * @vblank: Vertical blanking in lines
120 * @cur_mode: Pointer to current selected sensor mode
121 * @mutex: Mutex for serializing sensor controls
122 * @streaming: Flag indicating streaming state
126 struct i2c_client *client;
127 struct v4l2_subdev sd;
128 struct media_pad pad;
129 struct gpio_desc *reset_gpio;
131 struct v4l2_ctrl_handler ctrl_handler;
132 struct v4l2_ctrl *link_freq_ctrl;
133 struct v4l2_ctrl *pclk_ctrl;
134 struct v4l2_ctrl *hblank_ctrl;
135 struct v4l2_ctrl *vblank_ctrl;
137 struct v4l2_ctrl *exp_ctrl;
138 struct v4l2_ctrl *again_ctrl;
141 const struct imx334_mode *cur_mode;
146 static const s64 link_freq[] = {
150 /* Sensor mode registers */
151 static const struct imx334_reg mode_3840x2160_regs[] = {
246 /* Supported sensor mode configurations */
247 static const struct imx334_mode supported_mode = {
253 .vblank_max = 132840,
256 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
258 .num_of_regs = ARRAY_SIZE(mode_3840x2160_regs),
259 .regs = mode_3840x2160_regs,
264 * to_imx334() - imv334 V4L2 sub-device to imx334 device.
265 * @subdev: pointer to imx334 V4L2 sub-device
267 * Return: pointer to imx334 device
269 static inline struct imx334 *to_imx334(struct v4l2_subdev *subdev)
271 return container_of(subdev, struct imx334, sd);
275 * imx334_read_reg() - Read registers.
276 * @imx334: pointer to imx334 device
277 * @reg: register address
278 * @len: length of bytes to read. Max supported bytes is 4
279 * @val: pointer to register value to be filled.
281 * Big endian register addresses with little endian values.
283 * Return: 0 if successful, error code otherwise.
285 static int imx334_read_reg(struct imx334 *imx334, u16 reg, u32 len, u32 *val)
287 struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd);
288 struct i2c_msg msgs[2] = {0};
289 u8 addr_buf[2] = {0};
290 u8 data_buf[4] = {0};
293 if (WARN_ON(len > 4))
296 put_unaligned_be16(reg, addr_buf);
298 /* Write register address */
299 msgs[0].addr = client->addr;
301 msgs[0].len = ARRAY_SIZE(addr_buf);
302 msgs[0].buf = addr_buf;
304 /* Read data from register */
305 msgs[1].addr = client->addr;
306 msgs[1].flags = I2C_M_RD;
308 msgs[1].buf = data_buf;
310 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
311 if (ret != ARRAY_SIZE(msgs))
314 *val = get_unaligned_le32(data_buf);
320 * imx334_write_reg() - Write register
321 * @imx334: pointer to imx334 device
322 * @reg: register address
323 * @len: length of bytes. Max supported bytes is 4
324 * @val: register value
326 * Big endian register addresses with little endian values.
328 * Return: 0 if successful, error code otherwise.
330 static int imx334_write_reg(struct imx334 *imx334, u16 reg, u32 len, u32 val)
332 struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd);
335 if (WARN_ON(len > 4))
338 put_unaligned_be16(reg, buf);
339 put_unaligned_le32(val, buf + 2);
340 if (i2c_master_send(client, buf, len + 2) != len + 2)
347 * imx334_write_regs() - Write a list of registers
348 * @imx334: pointer to imx334 device
349 * @regs: list of registers to be written
350 * @len: length of registers array
352 * Return: 0 if successful, error code otherwise.
354 static int imx334_write_regs(struct imx334 *imx334,
355 const struct imx334_reg *regs, u32 len)
360 for (i = 0; i < len; i++) {
361 ret = imx334_write_reg(imx334, regs[i].address, 1, regs[i].val);
370 * imx334_update_controls() - Update control ranges based on streaming mode
371 * @imx334: pointer to imx334 device
372 * @mode: pointer to imx334_mode sensor mode
374 * Return: 0 if successful, error code otherwise.
376 static int imx334_update_controls(struct imx334 *imx334,
377 const struct imx334_mode *mode)
381 ret = __v4l2_ctrl_s_ctrl(imx334->link_freq_ctrl, mode->link_freq_idx);
385 ret = __v4l2_ctrl_s_ctrl(imx334->hblank_ctrl, mode->hblank);
389 return __v4l2_ctrl_modify_range(imx334->vblank_ctrl, mode->vblank_min,
390 mode->vblank_max, 1, mode->vblank);
394 * imx334_update_exp_gain() - Set updated exposure and gain
395 * @imx334: pointer to imx334 device
396 * @exposure: updated exposure value
397 * @gain: updated analog gain value
399 * Return: 0 if successful, error code otherwise.
401 static int imx334_update_exp_gain(struct imx334 *imx334, u32 exposure, u32 gain)
406 lpfr = imx334->vblank + imx334->cur_mode->height;
407 shutter = lpfr - exposure;
409 dev_dbg(imx334->dev, "Set long exp %u analog gain %u sh0 %u lpfr %u",
410 exposure, gain, shutter, lpfr);
412 ret = imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 1);
416 ret = imx334_write_reg(imx334, IMX334_REG_LPFR, 3, lpfr);
418 goto error_release_group_hold;
420 ret = imx334_write_reg(imx334, IMX334_REG_SHUTTER, 3, shutter);
422 goto error_release_group_hold;
424 ret = imx334_write_reg(imx334, IMX334_REG_AGAIN, 1, gain);
426 error_release_group_hold:
427 imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 0);
433 * imx334_set_ctrl() - Set subdevice control
434 * @ctrl: pointer to v4l2_ctrl structure
436 * Supported controls:
438 * - cluster controls:
439 * - V4L2_CID_ANALOGUE_GAIN
440 * - V4L2_CID_EXPOSURE
442 * Return: 0 if successful, error code otherwise.
444 static int imx334_set_ctrl(struct v4l2_ctrl *ctrl)
446 struct imx334 *imx334 =
447 container_of(ctrl->handler, struct imx334, ctrl_handler);
453 case V4L2_CID_VBLANK:
454 imx334->vblank = imx334->vblank_ctrl->val;
456 dev_dbg(imx334->dev, "Received vblank %u, new lpfr %u",
458 imx334->vblank + imx334->cur_mode->height);
460 ret = __v4l2_ctrl_modify_range(imx334->exp_ctrl,
463 imx334->cur_mode->height -
464 IMX334_EXPOSURE_OFFSET,
465 1, IMX334_EXPOSURE_DEFAULT);
467 case V4L2_CID_EXPOSURE:
469 /* Set controls only if sensor is in power on state */
470 if (!pm_runtime_get_if_in_use(imx334->dev))
473 exposure = ctrl->val;
474 analog_gain = imx334->again_ctrl->val;
476 dev_dbg(imx334->dev, "Received exp %u analog gain %u",
477 exposure, analog_gain);
479 ret = imx334_update_exp_gain(imx334, exposure, analog_gain);
481 pm_runtime_put(imx334->dev);
485 dev_err(imx334->dev, "Invalid control %d", ctrl->id);
492 /* V4l2 subdevice control ops*/
493 static const struct v4l2_ctrl_ops imx334_ctrl_ops = {
494 .s_ctrl = imx334_set_ctrl,
498 * imx334_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
499 * @sd: pointer to imx334 V4L2 sub-device structure
500 * @sd_state: V4L2 sub-device state
501 * @code: V4L2 sub-device code enumeration need to be filled
503 * Return: 0 if successful, error code otherwise.
505 static int imx334_enum_mbus_code(struct v4l2_subdev *sd,
506 struct v4l2_subdev_state *sd_state,
507 struct v4l2_subdev_mbus_code_enum *code)
512 code->code = supported_mode.code;
518 * imx334_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
519 * @sd: pointer to imx334 V4L2 sub-device structure
520 * @sd_state: V4L2 sub-device state
521 * @fsize: V4L2 sub-device size enumeration need to be filled
523 * Return: 0 if successful, error code otherwise.
525 static int imx334_enum_frame_size(struct v4l2_subdev *sd,
526 struct v4l2_subdev_state *sd_state,
527 struct v4l2_subdev_frame_size_enum *fsize)
529 if (fsize->index > 0)
532 if (fsize->code != supported_mode.code)
535 fsize->min_width = supported_mode.width;
536 fsize->max_width = fsize->min_width;
537 fsize->min_height = supported_mode.height;
538 fsize->max_height = fsize->min_height;
544 * imx334_fill_pad_format() - Fill subdevice pad format
545 * from selected sensor mode
546 * @imx334: pointer to imx334 device
547 * @mode: pointer to imx334_mode sensor mode
548 * @fmt: V4L2 sub-device format need to be filled
550 static void imx334_fill_pad_format(struct imx334 *imx334,
551 const struct imx334_mode *mode,
552 struct v4l2_subdev_format *fmt)
554 fmt->format.width = mode->width;
555 fmt->format.height = mode->height;
556 fmt->format.code = mode->code;
557 fmt->format.field = V4L2_FIELD_NONE;
558 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
559 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
560 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
561 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
565 * imx334_get_pad_format() - Get subdevice pad format
566 * @sd: pointer to imx334 V4L2 sub-device structure
567 * @sd_state: V4L2 sub-device state
568 * @fmt: V4L2 sub-device format need to be set
570 * Return: 0 if successful, error code otherwise.
572 static int imx334_get_pad_format(struct v4l2_subdev *sd,
573 struct v4l2_subdev_state *sd_state,
574 struct v4l2_subdev_format *fmt)
576 struct imx334 *imx334 = to_imx334(sd);
578 mutex_lock(&imx334->mutex);
580 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
581 struct v4l2_mbus_framefmt *framefmt;
583 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
584 fmt->format = *framefmt;
586 imx334_fill_pad_format(imx334, imx334->cur_mode, fmt);
589 mutex_unlock(&imx334->mutex);
595 * imx334_set_pad_format() - Set subdevice pad format
596 * @sd: pointer to imx334 V4L2 sub-device structure
597 * @sd_state: V4L2 sub-device state
598 * @fmt: V4L2 sub-device format need to be set
600 * Return: 0 if successful, error code otherwise.
602 static int imx334_set_pad_format(struct v4l2_subdev *sd,
603 struct v4l2_subdev_state *sd_state,
604 struct v4l2_subdev_format *fmt)
606 struct imx334 *imx334 = to_imx334(sd);
607 const struct imx334_mode *mode;
610 mutex_lock(&imx334->mutex);
612 mode = &supported_mode;
613 imx334_fill_pad_format(imx334, mode, fmt);
615 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
616 struct v4l2_mbus_framefmt *framefmt;
618 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
619 *framefmt = fmt->format;
621 ret = imx334_update_controls(imx334, mode);
623 imx334->cur_mode = mode;
626 mutex_unlock(&imx334->mutex);
632 * imx334_init_pad_cfg() - Initialize sub-device pad configuration
633 * @sd: pointer to imx334 V4L2 sub-device structure
634 * @sd_state: V4L2 sub-device state
636 * Return: 0 if successful, error code otherwise.
638 static int imx334_init_pad_cfg(struct v4l2_subdev *sd,
639 struct v4l2_subdev_state *sd_state)
641 struct imx334 *imx334 = to_imx334(sd);
642 struct v4l2_subdev_format fmt = { 0 };
644 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
645 imx334_fill_pad_format(imx334, &supported_mode, &fmt);
647 return imx334_set_pad_format(sd, sd_state, &fmt);
651 * imx334_start_streaming() - Start sensor stream
652 * @imx334: pointer to imx334 device
654 * Return: 0 if successful, error code otherwise.
656 static int imx334_start_streaming(struct imx334 *imx334)
658 const struct imx334_reg_list *reg_list;
661 /* Write sensor mode registers */
662 reg_list = &imx334->cur_mode->reg_list;
663 ret = imx334_write_regs(imx334, reg_list->regs,
664 reg_list->num_of_regs);
666 dev_err(imx334->dev, "fail to write initial registers");
670 /* Setup handler will write actual exposure and gain */
671 ret = __v4l2_ctrl_handler_setup(imx334->sd.ctrl_handler);
673 dev_err(imx334->dev, "fail to setup handler");
677 /* Start streaming */
678 ret = imx334_write_reg(imx334, IMX334_REG_MODE_SELECT,
679 1, IMX334_MODE_STREAMING);
681 dev_err(imx334->dev, "fail to start streaming");
689 * imx334_stop_streaming() - Stop sensor stream
690 * @imx334: pointer to imx334 device
692 * Return: 0 if successful, error code otherwise.
694 static int imx334_stop_streaming(struct imx334 *imx334)
696 return imx334_write_reg(imx334, IMX334_REG_MODE_SELECT,
697 1, IMX334_MODE_STANDBY);
701 * imx334_set_stream() - Enable sensor streaming
702 * @sd: pointer to imx334 subdevice
703 * @enable: set to enable sensor streaming
705 * Return: 0 if successful, error code otherwise.
707 static int imx334_set_stream(struct v4l2_subdev *sd, int enable)
709 struct imx334 *imx334 = to_imx334(sd);
712 mutex_lock(&imx334->mutex);
714 if (imx334->streaming == enable) {
715 mutex_unlock(&imx334->mutex);
720 ret = pm_runtime_resume_and_get(imx334->dev);
724 ret = imx334_start_streaming(imx334);
726 goto error_power_off;
728 imx334_stop_streaming(imx334);
729 pm_runtime_put(imx334->dev);
732 imx334->streaming = enable;
734 mutex_unlock(&imx334->mutex);
739 pm_runtime_put(imx334->dev);
741 mutex_unlock(&imx334->mutex);
747 * imx334_detect() - Detect imx334 sensor
748 * @imx334: pointer to imx334 device
750 * Return: 0 if successful, -EIO if sensor id does not match
752 static int imx334_detect(struct imx334 *imx334)
757 ret = imx334_read_reg(imx334, IMX334_REG_ID, 2, &val);
761 if (val != IMX334_ID) {
762 dev_err(imx334->dev, "chip id mismatch: %x!=%x",
771 * imx334_parse_hw_config() - Parse HW configuration and check if supported
772 * @imx334: pointer to imx334 device
774 * Return: 0 if successful, error code otherwise.
776 static int imx334_parse_hw_config(struct imx334 *imx334)
778 struct fwnode_handle *fwnode = dev_fwnode(imx334->dev);
779 struct v4l2_fwnode_endpoint bus_cfg = {
780 .bus_type = V4L2_MBUS_CSI2_DPHY
782 struct fwnode_handle *ep;
790 /* Request optional reset pin */
791 imx334->reset_gpio = devm_gpiod_get_optional(imx334->dev, "reset",
793 if (IS_ERR(imx334->reset_gpio)) {
794 dev_err(imx334->dev, "failed to get reset gpio %ld",
795 PTR_ERR(imx334->reset_gpio));
796 return PTR_ERR(imx334->reset_gpio);
799 /* Get sensor input clock */
800 imx334->inclk = devm_clk_get(imx334->dev, NULL);
801 if (IS_ERR(imx334->inclk)) {
802 dev_err(imx334->dev, "could not get inclk");
803 return PTR_ERR(imx334->inclk);
806 rate = clk_get_rate(imx334->inclk);
807 if (rate != IMX334_INCLK_RATE) {
808 dev_err(imx334->dev, "inclk frequency mismatch");
812 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
816 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
817 fwnode_handle_put(ep);
821 if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX334_NUM_DATA_LANES) {
823 "number of CSI2 data lanes %d is not supported",
824 bus_cfg.bus.mipi_csi2.num_data_lanes);
826 goto done_endpoint_free;
829 if (!bus_cfg.nr_of_link_frequencies) {
830 dev_err(imx334->dev, "no link frequencies defined");
832 goto done_endpoint_free;
835 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
836 if (bus_cfg.link_frequencies[i] == IMX334_LINK_FREQ)
837 goto done_endpoint_free;
842 v4l2_fwnode_endpoint_free(&bus_cfg);
847 /* V4l2 subdevice ops */
848 static const struct v4l2_subdev_video_ops imx334_video_ops = {
849 .s_stream = imx334_set_stream,
852 static const struct v4l2_subdev_pad_ops imx334_pad_ops = {
853 .init_cfg = imx334_init_pad_cfg,
854 .enum_mbus_code = imx334_enum_mbus_code,
855 .enum_frame_size = imx334_enum_frame_size,
856 .get_fmt = imx334_get_pad_format,
857 .set_fmt = imx334_set_pad_format,
860 static const struct v4l2_subdev_ops imx334_subdev_ops = {
861 .video = &imx334_video_ops,
862 .pad = &imx334_pad_ops,
866 * imx334_power_on() - Sensor power on sequence
867 * @dev: pointer to i2c device
869 * Return: 0 if successful, error code otherwise.
871 static int imx334_power_on(struct device *dev)
873 struct v4l2_subdev *sd = dev_get_drvdata(dev);
874 struct imx334 *imx334 = to_imx334(sd);
877 gpiod_set_value_cansleep(imx334->reset_gpio, 1);
879 ret = clk_prepare_enable(imx334->inclk);
881 dev_err(imx334->dev, "fail to enable inclk");
885 usleep_range(18000, 20000);
890 gpiod_set_value_cansleep(imx334->reset_gpio, 0);
896 * imx334_power_off() - Sensor power off sequence
897 * @dev: pointer to i2c device
899 * Return: 0 if successful, error code otherwise.
901 static int imx334_power_off(struct device *dev)
903 struct v4l2_subdev *sd = dev_get_drvdata(dev);
904 struct imx334 *imx334 = to_imx334(sd);
906 gpiod_set_value_cansleep(imx334->reset_gpio, 0);
908 clk_disable_unprepare(imx334->inclk);
914 * imx334_init_controls() - Initialize sensor subdevice controls
915 * @imx334: pointer to imx334 device
917 * Return: 0 if successful, error code otherwise.
919 static int imx334_init_controls(struct imx334 *imx334)
921 struct v4l2_ctrl_handler *ctrl_hdlr = &imx334->ctrl_handler;
922 const struct imx334_mode *mode = imx334->cur_mode;
926 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
930 /* Serialize controls with sensor device */
931 ctrl_hdlr->lock = &imx334->mutex;
933 /* Initialize exposure and gain */
934 lpfr = mode->vblank + mode->height;
935 imx334->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
939 lpfr - IMX334_EXPOSURE_OFFSET,
940 IMX334_EXPOSURE_STEP,
941 IMX334_EXPOSURE_DEFAULT);
943 imx334->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
945 V4L2_CID_ANALOGUE_GAIN,
949 IMX334_AGAIN_DEFAULT);
951 v4l2_ctrl_cluster(2, &imx334->exp_ctrl);
953 imx334->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
960 /* Read only controls */
961 imx334->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
964 mode->pclk, mode->pclk,
967 imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
970 ARRAY_SIZE(link_freq) -
974 if (imx334->link_freq_ctrl)
975 imx334->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
977 imx334->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
983 if (imx334->hblank_ctrl)
984 imx334->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
986 if (ctrl_hdlr->error) {
987 dev_err(imx334->dev, "control init failed: %d",
989 v4l2_ctrl_handler_free(ctrl_hdlr);
990 return ctrl_hdlr->error;
993 imx334->sd.ctrl_handler = ctrl_hdlr;
999 * imx334_probe() - I2C client device binding
1000 * @client: pointer to i2c client device
1002 * Return: 0 if successful, error code otherwise.
1004 static int imx334_probe(struct i2c_client *client)
1006 struct imx334 *imx334;
1009 imx334 = devm_kzalloc(&client->dev, sizeof(*imx334), GFP_KERNEL);
1013 imx334->dev = &client->dev;
1015 /* Initialize subdev */
1016 v4l2_i2c_subdev_init(&imx334->sd, client, &imx334_subdev_ops);
1018 ret = imx334_parse_hw_config(imx334);
1020 dev_err(imx334->dev, "HW configuration is not supported");
1024 mutex_init(&imx334->mutex);
1026 ret = imx334_power_on(imx334->dev);
1028 dev_err(imx334->dev, "failed to power-on the sensor");
1029 goto error_mutex_destroy;
1032 /* Check module identity */
1033 ret = imx334_detect(imx334);
1035 dev_err(imx334->dev, "failed to find sensor: %d", ret);
1036 goto error_power_off;
1039 /* Set default mode to max resolution */
1040 imx334->cur_mode = &supported_mode;
1041 imx334->vblank = imx334->cur_mode->vblank;
1043 ret = imx334_init_controls(imx334);
1045 dev_err(imx334->dev, "failed to init controls: %d", ret);
1046 goto error_power_off;
1049 /* Initialize subdev */
1050 imx334->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1051 imx334->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1053 /* Initialize source pad */
1054 imx334->pad.flags = MEDIA_PAD_FL_SOURCE;
1055 ret = media_entity_pads_init(&imx334->sd.entity, 1, &imx334->pad);
1057 dev_err(imx334->dev, "failed to init entity pads: %d", ret);
1058 goto error_handler_free;
1061 ret = v4l2_async_register_subdev_sensor(&imx334->sd);
1063 dev_err(imx334->dev,
1064 "failed to register async subdev: %d", ret);
1065 goto error_media_entity;
1068 pm_runtime_set_active(imx334->dev);
1069 pm_runtime_enable(imx334->dev);
1070 pm_runtime_idle(imx334->dev);
1075 media_entity_cleanup(&imx334->sd.entity);
1077 v4l2_ctrl_handler_free(imx334->sd.ctrl_handler);
1079 imx334_power_off(imx334->dev);
1080 error_mutex_destroy:
1081 mutex_destroy(&imx334->mutex);
1087 * imx334_remove() - I2C client device unbinding
1088 * @client: pointer to I2C client device
1090 * Return: 0 if successful, error code otherwise.
1092 static void imx334_remove(struct i2c_client *client)
1094 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1095 struct imx334 *imx334 = to_imx334(sd);
1097 v4l2_async_unregister_subdev(sd);
1098 media_entity_cleanup(&sd->entity);
1099 v4l2_ctrl_handler_free(sd->ctrl_handler);
1101 pm_runtime_disable(&client->dev);
1102 pm_runtime_suspended(&client->dev);
1104 mutex_destroy(&imx334->mutex);
1107 static const struct dev_pm_ops imx334_pm_ops = {
1108 SET_RUNTIME_PM_OPS(imx334_power_off, imx334_power_on, NULL)
1111 static const struct of_device_id imx334_of_match[] = {
1112 { .compatible = "sony,imx334" },
1116 MODULE_DEVICE_TABLE(of, imx334_of_match);
1118 static struct i2c_driver imx334_driver = {
1119 .probe_new = imx334_probe,
1120 .remove = imx334_remove,
1123 .pm = &imx334_pm_ops,
1124 .of_match_table = imx334_of_match,
1128 module_i2c_driver(imx334_driver);
1130 MODULE_DESCRIPTION("Sony imx334 sensor driver");
1131 MODULE_LICENSE("GPL");