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 * @cfg: V4L2 sub-device pad configuration
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_pad_config *cfg,
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 * @cfg: V4L2 sub-device pad configuration
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_pad_config *cfg,
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 * @cfg: V4L2 sub-device pad configuration
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_pad_config *cfg,
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, cfg, 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 * @cfg: V4L2 sub-device pad configuration
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_pad_config *cfg,
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, cfg, 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 * @cfg: V4L2 sub-device pad configuration
636 * Return: 0 if successful, error code otherwise.
638 static int imx334_init_pad_cfg(struct v4l2_subdev *sd,
639 struct v4l2_subdev_pad_config *cfg)
641 struct imx334 *imx334 = to_imx334(sd);
642 struct v4l2_subdev_format fmt = { 0 };
644 fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
645 imx334_fill_pad_format(imx334, &supported_mode, &fmt);
647 return imx334_set_pad_format(sd, cfg, &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_get_sync(imx334->dev);
722 goto error_power_off;
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);
740 mutex_unlock(&imx334->mutex);
746 * imx334_detect() - Detect imx334 sensor
747 * @imx334: pointer to imx334 device
749 * Return: 0 if successful, -EIO if sensor id does not match
751 static int imx334_detect(struct imx334 *imx334)
756 ret = imx334_read_reg(imx334, IMX334_REG_ID, 2, &val);
760 if (val != IMX334_ID) {
761 dev_err(imx334->dev, "chip id mismatch: %x!=%x",
770 * imx334_parse_hw_config() - Parse HW configuration and check if supported
771 * @imx334: pointer to imx334 device
773 * Return: 0 if successful, error code otherwise.
775 static int imx334_parse_hw_config(struct imx334 *imx334)
777 struct fwnode_handle *fwnode = dev_fwnode(imx334->dev);
778 struct v4l2_fwnode_endpoint bus_cfg = {
779 .bus_type = V4L2_MBUS_CSI2_DPHY
781 struct fwnode_handle *ep;
789 /* Request optional reset pin */
790 imx334->reset_gpio = devm_gpiod_get_optional(imx334->dev, "reset",
792 if (IS_ERR(imx334->reset_gpio)) {
793 dev_err(imx334->dev, "failed to get reset gpio %ld",
794 PTR_ERR(imx334->reset_gpio));
795 return PTR_ERR(imx334->reset_gpio);
798 /* Get sensor input clock */
799 imx334->inclk = devm_clk_get(imx334->dev, NULL);
800 if (IS_ERR(imx334->inclk)) {
801 dev_err(imx334->dev, "could not get inclk");
802 return PTR_ERR(imx334->inclk);
805 rate = clk_get_rate(imx334->inclk);
806 if (rate != IMX334_INCLK_RATE) {
807 dev_err(imx334->dev, "inclk frequency mismatch");
811 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
815 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
816 fwnode_handle_put(ep);
820 if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX334_NUM_DATA_LANES) {
822 "number of CSI2 data lanes %d is not supported",
823 bus_cfg.bus.mipi_csi2.num_data_lanes);
825 goto done_endpoint_free;
828 if (!bus_cfg.nr_of_link_frequencies) {
829 dev_err(imx334->dev, "no link frequencies defined");
831 goto done_endpoint_free;
834 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
835 if (bus_cfg.link_frequencies[i] == IMX334_LINK_FREQ)
836 goto done_endpoint_free;
841 v4l2_fwnode_endpoint_free(&bus_cfg);
846 /* V4l2 subdevice ops */
847 static const struct v4l2_subdev_video_ops imx334_video_ops = {
848 .s_stream = imx334_set_stream,
851 static const struct v4l2_subdev_pad_ops imx334_pad_ops = {
852 .init_cfg = imx334_init_pad_cfg,
853 .enum_mbus_code = imx334_enum_mbus_code,
854 .enum_frame_size = imx334_enum_frame_size,
855 .get_fmt = imx334_get_pad_format,
856 .set_fmt = imx334_set_pad_format,
859 static const struct v4l2_subdev_ops imx334_subdev_ops = {
860 .video = &imx334_video_ops,
861 .pad = &imx334_pad_ops,
865 * imx334_power_on() - Sensor power on sequence
866 * @dev: pointer to i2c device
868 * Return: 0 if successful, error code otherwise.
870 static int imx334_power_on(struct device *dev)
872 struct v4l2_subdev *sd = dev_get_drvdata(dev);
873 struct imx334 *imx334 = to_imx334(sd);
876 gpiod_set_value_cansleep(imx334->reset_gpio, 1);
878 ret = clk_prepare_enable(imx334->inclk);
880 dev_err(imx334->dev, "fail to enable inclk");
884 usleep_range(18000, 20000);
889 gpiod_set_value_cansleep(imx334->reset_gpio, 0);
895 * imx334_power_off() - Sensor power off sequence
896 * @dev: pointer to i2c device
898 * Return: 0 if successful, error code otherwise.
900 static int imx334_power_off(struct device *dev)
902 struct v4l2_subdev *sd = dev_get_drvdata(dev);
903 struct imx334 *imx334 = to_imx334(sd);
905 gpiod_set_value_cansleep(imx334->reset_gpio, 0);
907 clk_disable_unprepare(imx334->inclk);
913 * imx334_init_controls() - Initialize sensor subdevice controls
914 * @imx334: pointer to imx334 device
916 * Return: 0 if successful, error code otherwise.
918 static int imx334_init_controls(struct imx334 *imx334)
920 struct v4l2_ctrl_handler *ctrl_hdlr = &imx334->ctrl_handler;
921 const struct imx334_mode *mode = imx334->cur_mode;
925 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
929 /* Serialize controls with sensor device */
930 ctrl_hdlr->lock = &imx334->mutex;
932 /* Initialize exposure and gain */
933 lpfr = mode->vblank + mode->height;
934 imx334->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
938 lpfr - IMX334_EXPOSURE_OFFSET,
939 IMX334_EXPOSURE_STEP,
940 IMX334_EXPOSURE_DEFAULT);
942 imx334->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
944 V4L2_CID_ANALOGUE_GAIN,
948 IMX334_AGAIN_DEFAULT);
950 v4l2_ctrl_cluster(2, &imx334->exp_ctrl);
952 imx334->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
959 /* Read only controls */
960 imx334->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
963 mode->pclk, mode->pclk,
966 imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
969 ARRAY_SIZE(link_freq) -
973 if (imx334->link_freq_ctrl)
974 imx334->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
976 imx334->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
982 if (imx334->hblank_ctrl)
983 imx334->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
985 if (ctrl_hdlr->error) {
986 dev_err(imx334->dev, "control init failed: %d",
988 v4l2_ctrl_handler_free(ctrl_hdlr);
989 return ctrl_hdlr->error;
992 imx334->sd.ctrl_handler = ctrl_hdlr;
998 * imx334_probe() - I2C client device binding
999 * @client: pointer to i2c client device
1001 * Return: 0 if successful, error code otherwise.
1003 static int imx334_probe(struct i2c_client *client)
1005 struct imx334 *imx334;
1008 imx334 = devm_kzalloc(&client->dev, sizeof(*imx334), GFP_KERNEL);
1012 imx334->dev = &client->dev;
1014 /* Initialize subdev */
1015 v4l2_i2c_subdev_init(&imx334->sd, client, &imx334_subdev_ops);
1017 ret = imx334_parse_hw_config(imx334);
1019 dev_err(imx334->dev, "HW configuration is not supported");
1023 mutex_init(&imx334->mutex);
1025 ret = imx334_power_on(imx334->dev);
1027 dev_err(imx334->dev, "failed to power-on the sensor");
1028 goto error_mutex_destroy;
1031 /* Check module identity */
1032 ret = imx334_detect(imx334);
1034 dev_err(imx334->dev, "failed to find sensor: %d", ret);
1035 goto error_power_off;
1038 /* Set default mode to max resolution */
1039 imx334->cur_mode = &supported_mode;
1040 imx334->vblank = imx334->cur_mode->vblank;
1042 ret = imx334_init_controls(imx334);
1044 dev_err(imx334->dev, "failed to init controls: %d", ret);
1045 goto error_power_off;
1048 /* Initialize subdev */
1049 imx334->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1050 imx334->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1052 /* Initialize source pad */
1053 imx334->pad.flags = MEDIA_PAD_FL_SOURCE;
1054 ret = media_entity_pads_init(&imx334->sd.entity, 1, &imx334->pad);
1056 dev_err(imx334->dev, "failed to init entity pads: %d", ret);
1057 goto error_handler_free;
1060 ret = v4l2_async_register_subdev_sensor(&imx334->sd);
1062 dev_err(imx334->dev,
1063 "failed to register async subdev: %d", ret);
1064 goto error_media_entity;
1067 pm_runtime_set_active(imx334->dev);
1068 pm_runtime_enable(imx334->dev);
1069 pm_runtime_idle(imx334->dev);
1074 media_entity_cleanup(&imx334->sd.entity);
1076 v4l2_ctrl_handler_free(imx334->sd.ctrl_handler);
1078 imx334_power_off(imx334->dev);
1079 error_mutex_destroy:
1080 mutex_destroy(&imx334->mutex);
1086 * imx334_remove() - I2C client device unbinding
1087 * @client: pointer to I2C client device
1089 * Return: 0 if successful, error code otherwise.
1091 static int imx334_remove(struct i2c_client *client)
1093 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1094 struct imx334 *imx334 = to_imx334(sd);
1096 v4l2_async_unregister_subdev(sd);
1097 media_entity_cleanup(&sd->entity);
1098 v4l2_ctrl_handler_free(sd->ctrl_handler);
1100 pm_runtime_disable(&client->dev);
1101 pm_runtime_suspended(&client->dev);
1103 mutex_destroy(&imx334->mutex);
1108 static const struct dev_pm_ops imx334_pm_ops = {
1109 SET_RUNTIME_PM_OPS(imx334_power_off, imx334_power_on, NULL)
1112 static const struct of_device_id imx334_of_match[] = {
1113 { .compatible = "sony,imx334" },
1117 MODULE_DEVICE_TABLE(of, imx334_of_match);
1119 static struct i2c_driver imx334_driver = {
1120 .probe_new = imx334_probe,
1121 .remove = imx334_remove,
1124 .pm = &imx334_pm_ops,
1125 .of_match_table = imx334_of_match,
1129 module_i2c_driver(imx334_driver);
1131 MODULE_DESCRIPTION("Sony imx334 sensor driver");
1132 MODULE_LICENSE("GPL");