1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sony imx335 Camera 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 IMX335_REG_MODE_SELECT 0x3000
21 #define IMX335_MODE_STANDBY 0x01
22 #define IMX335_MODE_STREAMING 0x00
25 #define IMX335_REG_LPFR 0x3030
28 #define IMX335_REG_ID 0x3912
29 #define IMX335_ID 0x00
31 /* Exposure control */
32 #define IMX335_REG_SHUTTER 0x3058
33 #define IMX335_EXPOSURE_MIN 1
34 #define IMX335_EXPOSURE_OFFSET 9
35 #define IMX335_EXPOSURE_STEP 1
36 #define IMX335_EXPOSURE_DEFAULT 0x0648
38 /* Analog gain control */
39 #define IMX335_REG_AGAIN 0x30e8
40 #define IMX335_AGAIN_MIN 0
41 #define IMX335_AGAIN_MAX 240
42 #define IMX335_AGAIN_STEP 1
43 #define IMX335_AGAIN_DEFAULT 0
45 /* Group hold register */
46 #define IMX335_REG_HOLD 0x3001
48 /* Input clock rate */
49 #define IMX335_INCLK_RATE 24000000
51 /* CSI2 HW configuration */
52 #define IMX335_LINK_FREQ 594000000
53 #define IMX335_NUM_DATA_LANES 4
55 #define IMX335_REG_MIN 0x00
56 #define IMX335_REG_MAX 0xfffff
59 * struct imx335_reg - imx335 sensor register
60 * @address: Register address
61 * @val: Register value
69 * struct imx335_reg_list - imx335 sensor register list
70 * @num_of_regs: Number of registers in the list
71 * @regs: Pointer to register list
73 struct imx335_reg_list {
75 const struct imx335_reg *regs;
79 * struct imx335_mode - imx335 sensor mode structure
81 * @height: Frame height
83 * @hblank: Horizontal blanking in lines
84 * @vblank: Vertical blanking in lines
85 * @vblank_min: Minimum 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 imx335_reg_list reg_list;
105 * struct imx335 - imx335 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 imx335_mode *cur_mode;
146 static const s64 link_freq[] = {
150 /* Sensor mode registers */
151 static const struct imx335_reg mode_2592x1940_regs[] = {
238 /* Supported sensor mode configurations */
239 static const struct imx335_mode supported_mode = {
245 .vblank_max = 133060,
248 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
250 .num_of_regs = ARRAY_SIZE(mode_2592x1940_regs),
251 .regs = mode_2592x1940_regs,
256 * to_imx335() - imx335 V4L2 sub-device to imx335 device.
257 * @subdev: pointer to imx335 V4L2 sub-device
259 * Return: pointer to imx335 device
261 static inline struct imx335 *to_imx335(struct v4l2_subdev *subdev)
263 return container_of(subdev, struct imx335, sd);
267 * imx335_read_reg() - Read registers.
268 * @imx335: pointer to imx335 device
269 * @reg: register address
270 * @len: length of bytes to read. Max supported bytes is 4
271 * @val: pointer to register value to be filled.
273 * Big endian register addresses with little endian values.
275 * Return: 0 if successful, error code otherwise.
277 static int imx335_read_reg(struct imx335 *imx335, u16 reg, u32 len, u32 *val)
279 struct i2c_client *client = v4l2_get_subdevdata(&imx335->sd);
280 struct i2c_msg msgs[2] = {0};
281 u8 addr_buf[2] = {0};
282 u8 data_buf[4] = {0};
285 if (WARN_ON(len > 4))
288 put_unaligned_be16(reg, addr_buf);
290 /* Write register address */
291 msgs[0].addr = client->addr;
293 msgs[0].len = ARRAY_SIZE(addr_buf);
294 msgs[0].buf = addr_buf;
296 /* Read data from register */
297 msgs[1].addr = client->addr;
298 msgs[1].flags = I2C_M_RD;
300 msgs[1].buf = data_buf;
302 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
303 if (ret != ARRAY_SIZE(msgs))
306 *val = get_unaligned_le32(data_buf);
312 * imx335_write_reg() - Write register
313 * @imx335: pointer to imx335 device
314 * @reg: register address
315 * @len: length of bytes. Max supported bytes is 4
316 * @val: register value
318 * Big endian register addresses with little endian values.
320 * Return: 0 if successful, error code otherwise.
322 static int imx335_write_reg(struct imx335 *imx335, u16 reg, u32 len, u32 val)
324 struct i2c_client *client = v4l2_get_subdevdata(&imx335->sd);
327 if (WARN_ON(len > 4))
330 put_unaligned_be16(reg, buf);
331 put_unaligned_le32(val, buf + 2);
332 if (i2c_master_send(client, buf, len + 2) != len + 2)
339 * imx335_write_regs() - Write a list of registers
340 * @imx335: pointer to imx335 device
341 * @regs: list of registers to be written
342 * @len: length of registers array
344 * Return: 0 if successful. error code otherwise.
346 static int imx335_write_regs(struct imx335 *imx335,
347 const struct imx335_reg *regs, u32 len)
352 for (i = 0; i < len; i++) {
353 ret = imx335_write_reg(imx335, regs[i].address, 1, regs[i].val);
362 * imx335_update_controls() - Update control ranges based on streaming mode
363 * @imx335: pointer to imx335 device
364 * @mode: pointer to imx335_mode sensor mode
366 * Return: 0 if successful, error code otherwise.
368 static int imx335_update_controls(struct imx335 *imx335,
369 const struct imx335_mode *mode)
373 ret = __v4l2_ctrl_s_ctrl(imx335->link_freq_ctrl, mode->link_freq_idx);
377 ret = __v4l2_ctrl_s_ctrl(imx335->hblank_ctrl, mode->hblank);
381 return __v4l2_ctrl_modify_range(imx335->vblank_ctrl, mode->vblank_min,
382 mode->vblank_max, 1, mode->vblank);
386 * imx335_update_exp_gain() - Set updated exposure and gain
387 * @imx335: pointer to imx335 device
388 * @exposure: updated exposure value
389 * @gain: updated analog gain value
391 * Return: 0 if successful, error code otherwise.
393 static int imx335_update_exp_gain(struct imx335 *imx335, u32 exposure, u32 gain)
398 lpfr = imx335->vblank + imx335->cur_mode->height;
399 shutter = lpfr - exposure;
401 dev_dbg(imx335->dev, "Set exp %u, analog gain %u, shutter %u, lpfr %u",
402 exposure, gain, shutter, lpfr);
404 ret = imx335_write_reg(imx335, IMX335_REG_HOLD, 1, 1);
408 ret = imx335_write_reg(imx335, IMX335_REG_LPFR, 3, lpfr);
410 goto error_release_group_hold;
412 ret = imx335_write_reg(imx335, IMX335_REG_SHUTTER, 3, shutter);
414 goto error_release_group_hold;
416 ret = imx335_write_reg(imx335, IMX335_REG_AGAIN, 2, gain);
418 error_release_group_hold:
419 imx335_write_reg(imx335, IMX335_REG_HOLD, 1, 0);
425 * imx335_set_ctrl() - Set subdevice control
426 * @ctrl: pointer to v4l2_ctrl structure
428 * Supported controls:
430 * - cluster controls:
431 * - V4L2_CID_ANALOGUE_GAIN
432 * - V4L2_CID_EXPOSURE
434 * Return: 0 if successful, error code otherwise.
436 static int imx335_set_ctrl(struct v4l2_ctrl *ctrl)
438 struct imx335 *imx335 =
439 container_of(ctrl->handler, struct imx335, ctrl_handler);
445 case V4L2_CID_VBLANK:
446 imx335->vblank = imx335->vblank_ctrl->val;
448 dev_dbg(imx335->dev, "Received vblank %u, new lpfr %u",
450 imx335->vblank + imx335->cur_mode->height);
452 ret = __v4l2_ctrl_modify_range(imx335->exp_ctrl,
455 imx335->cur_mode->height -
456 IMX335_EXPOSURE_OFFSET,
457 1, IMX335_EXPOSURE_DEFAULT);
459 case V4L2_CID_EXPOSURE:
460 /* Set controls only if sensor is in power on state */
461 if (!pm_runtime_get_if_in_use(imx335->dev))
464 exposure = ctrl->val;
465 analog_gain = imx335->again_ctrl->val;
467 dev_dbg(imx335->dev, "Received exp %u, analog gain %u",
468 exposure, analog_gain);
470 ret = imx335_update_exp_gain(imx335, exposure, analog_gain);
472 pm_runtime_put(imx335->dev);
476 dev_err(imx335->dev, "Invalid control %d", ctrl->id);
483 /* V4l2 subdevice control ops*/
484 static const struct v4l2_ctrl_ops imx335_ctrl_ops = {
485 .s_ctrl = imx335_set_ctrl,
489 * imx335_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
490 * @sd: pointer to imx335 V4L2 sub-device structure
491 * @sd_state: V4L2 sub-device configuration
492 * @code: V4L2 sub-device code enumeration need to be filled
494 * Return: 0 if successful, error code otherwise.
496 static int imx335_enum_mbus_code(struct v4l2_subdev *sd,
497 struct v4l2_subdev_state *sd_state,
498 struct v4l2_subdev_mbus_code_enum *code)
503 code->code = supported_mode.code;
509 * imx335_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
510 * @sd: pointer to imx335 V4L2 sub-device structure
511 * @sd_state: V4L2 sub-device configuration
512 * @fsize: V4L2 sub-device size enumeration need to be filled
514 * Return: 0 if successful, error code otherwise.
516 static int imx335_enum_frame_size(struct v4l2_subdev *sd,
517 struct v4l2_subdev_state *sd_state,
518 struct v4l2_subdev_frame_size_enum *fsize)
520 if (fsize->index > 0)
523 if (fsize->code != supported_mode.code)
526 fsize->min_width = supported_mode.width;
527 fsize->max_width = fsize->min_width;
528 fsize->min_height = supported_mode.height;
529 fsize->max_height = fsize->min_height;
535 * imx335_fill_pad_format() - Fill subdevice pad format
536 * from selected sensor mode
537 * @imx335: pointer to imx335 device
538 * @mode: pointer to imx335_mode sensor mode
539 * @fmt: V4L2 sub-device format need to be filled
541 static void imx335_fill_pad_format(struct imx335 *imx335,
542 const struct imx335_mode *mode,
543 struct v4l2_subdev_format *fmt)
545 fmt->format.width = mode->width;
546 fmt->format.height = mode->height;
547 fmt->format.code = mode->code;
548 fmt->format.field = V4L2_FIELD_NONE;
549 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
550 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
551 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
552 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
556 * imx335_get_pad_format() - Get subdevice pad format
557 * @sd: pointer to imx335 V4L2 sub-device structure
558 * @sd_state: V4L2 sub-device configuration
559 * @fmt: V4L2 sub-device format need to be set
561 * Return: 0 if successful, error code otherwise.
563 static int imx335_get_pad_format(struct v4l2_subdev *sd,
564 struct v4l2_subdev_state *sd_state,
565 struct v4l2_subdev_format *fmt)
567 struct imx335 *imx335 = to_imx335(sd);
569 mutex_lock(&imx335->mutex);
571 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
572 struct v4l2_mbus_framefmt *framefmt;
574 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
575 fmt->format = *framefmt;
577 imx335_fill_pad_format(imx335, imx335->cur_mode, fmt);
580 mutex_unlock(&imx335->mutex);
586 * imx335_set_pad_format() - Set subdevice pad format
587 * @sd: pointer to imx335 V4L2 sub-device structure
588 * @sd_state: V4L2 sub-device configuration
589 * @fmt: V4L2 sub-device format need to be set
591 * Return: 0 if successful, error code otherwise.
593 static int imx335_set_pad_format(struct v4l2_subdev *sd,
594 struct v4l2_subdev_state *sd_state,
595 struct v4l2_subdev_format *fmt)
597 struct imx335 *imx335 = to_imx335(sd);
598 const struct imx335_mode *mode;
601 mutex_lock(&imx335->mutex);
603 mode = &supported_mode;
604 imx335_fill_pad_format(imx335, mode, fmt);
606 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
607 struct v4l2_mbus_framefmt *framefmt;
609 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
610 *framefmt = fmt->format;
612 ret = imx335_update_controls(imx335, mode);
614 imx335->cur_mode = mode;
617 mutex_unlock(&imx335->mutex);
623 * imx335_init_pad_cfg() - Initialize sub-device pad configuration
624 * @sd: pointer to imx335 V4L2 sub-device structure
625 * @sd_state: V4L2 sub-device configuration
627 * Return: 0 if successful, error code otherwise.
629 static int imx335_init_pad_cfg(struct v4l2_subdev *sd,
630 struct v4l2_subdev_state *sd_state)
632 struct imx335 *imx335 = to_imx335(sd);
633 struct v4l2_subdev_format fmt = { 0 };
635 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
636 imx335_fill_pad_format(imx335, &supported_mode, &fmt);
638 return imx335_set_pad_format(sd, sd_state, &fmt);
642 * imx335_start_streaming() - Start sensor stream
643 * @imx335: pointer to imx335 device
645 * Return: 0 if successful, error code otherwise.
647 static int imx335_start_streaming(struct imx335 *imx335)
649 const struct imx335_reg_list *reg_list;
652 /* Write sensor mode registers */
653 reg_list = &imx335->cur_mode->reg_list;
654 ret = imx335_write_regs(imx335, reg_list->regs,
655 reg_list->num_of_regs);
657 dev_err(imx335->dev, "fail to write initial registers");
661 /* Setup handler will write actual exposure and gain */
662 ret = __v4l2_ctrl_handler_setup(imx335->sd.ctrl_handler);
664 dev_err(imx335->dev, "fail to setup handler");
668 /* Start streaming */
669 ret = imx335_write_reg(imx335, IMX335_REG_MODE_SELECT,
670 1, IMX335_MODE_STREAMING);
672 dev_err(imx335->dev, "fail to start streaming");
676 /* Initial regulator stabilization period */
677 usleep_range(18000, 20000);
683 * imx335_stop_streaming() - Stop sensor stream
684 * @imx335: pointer to imx335 device
686 * Return: 0 if successful, error code otherwise.
688 static int imx335_stop_streaming(struct imx335 *imx335)
690 return imx335_write_reg(imx335, IMX335_REG_MODE_SELECT,
691 1, IMX335_MODE_STANDBY);
695 * imx335_set_stream() - Enable sensor streaming
696 * @sd: pointer to imx335 subdevice
697 * @enable: set to enable sensor streaming
699 * Return: 0 if successful, error code otherwise.
701 static int imx335_set_stream(struct v4l2_subdev *sd, int enable)
703 struct imx335 *imx335 = to_imx335(sd);
706 mutex_lock(&imx335->mutex);
708 if (imx335->streaming == enable) {
709 mutex_unlock(&imx335->mutex);
714 ret = pm_runtime_resume_and_get(imx335->dev);
718 ret = imx335_start_streaming(imx335);
720 goto error_power_off;
722 imx335_stop_streaming(imx335);
723 pm_runtime_put(imx335->dev);
726 imx335->streaming = enable;
728 mutex_unlock(&imx335->mutex);
733 pm_runtime_put(imx335->dev);
735 mutex_unlock(&imx335->mutex);
741 * imx335_detect() - Detect imx335 sensor
742 * @imx335: pointer to imx335 device
744 * Return: 0 if successful, -EIO if sensor id does not match
746 static int imx335_detect(struct imx335 *imx335)
751 ret = imx335_read_reg(imx335, IMX335_REG_ID, 2, &val);
755 if (val != IMX335_ID) {
756 dev_err(imx335->dev, "chip id mismatch: %x!=%x",
765 * imx335_parse_hw_config() - Parse HW configuration and check if supported
766 * @imx335: pointer to imx335 device
768 * Return: 0 if successful, error code otherwise.
770 static int imx335_parse_hw_config(struct imx335 *imx335)
772 struct fwnode_handle *fwnode = dev_fwnode(imx335->dev);
773 struct v4l2_fwnode_endpoint bus_cfg = {
774 .bus_type = V4L2_MBUS_CSI2_DPHY
776 struct fwnode_handle *ep;
784 /* Request optional reset pin */
785 imx335->reset_gpio = devm_gpiod_get_optional(imx335->dev, "reset",
787 if (IS_ERR(imx335->reset_gpio)) {
788 dev_err(imx335->dev, "failed to get reset gpio %ld",
789 PTR_ERR(imx335->reset_gpio));
790 return PTR_ERR(imx335->reset_gpio);
793 /* Get sensor input clock */
794 imx335->inclk = devm_clk_get(imx335->dev, NULL);
795 if (IS_ERR(imx335->inclk)) {
796 dev_err(imx335->dev, "could not get inclk");
797 return PTR_ERR(imx335->inclk);
800 rate = clk_get_rate(imx335->inclk);
801 if (rate != IMX335_INCLK_RATE) {
802 dev_err(imx335->dev, "inclk frequency mismatch");
806 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
810 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
811 fwnode_handle_put(ep);
815 if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX335_NUM_DATA_LANES) {
817 "number of CSI2 data lanes %d is not supported",
818 bus_cfg.bus.mipi_csi2.num_data_lanes);
820 goto done_endpoint_free;
823 if (!bus_cfg.nr_of_link_frequencies) {
824 dev_err(imx335->dev, "no link frequencies defined");
826 goto done_endpoint_free;
829 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
830 if (bus_cfg.link_frequencies[i] == IMX335_LINK_FREQ)
831 goto done_endpoint_free;
836 v4l2_fwnode_endpoint_free(&bus_cfg);
841 /* V4l2 subdevice ops */
842 static const struct v4l2_subdev_video_ops imx335_video_ops = {
843 .s_stream = imx335_set_stream,
846 static const struct v4l2_subdev_pad_ops imx335_pad_ops = {
847 .init_cfg = imx335_init_pad_cfg,
848 .enum_mbus_code = imx335_enum_mbus_code,
849 .enum_frame_size = imx335_enum_frame_size,
850 .get_fmt = imx335_get_pad_format,
851 .set_fmt = imx335_set_pad_format,
854 static const struct v4l2_subdev_ops imx335_subdev_ops = {
855 .video = &imx335_video_ops,
856 .pad = &imx335_pad_ops,
860 * imx335_power_on() - Sensor power on sequence
861 * @dev: pointer to i2c device
863 * Return: 0 if successful, error code otherwise.
865 static int imx335_power_on(struct device *dev)
867 struct v4l2_subdev *sd = dev_get_drvdata(dev);
868 struct imx335 *imx335 = to_imx335(sd);
871 gpiod_set_value_cansleep(imx335->reset_gpio, 1);
873 ret = clk_prepare_enable(imx335->inclk);
875 dev_err(imx335->dev, "fail to enable inclk");
879 usleep_range(20, 22);
884 gpiod_set_value_cansleep(imx335->reset_gpio, 0);
890 * imx335_power_off() - Sensor power off sequence
891 * @dev: pointer to i2c device
893 * Return: 0 if successful, error code otherwise.
895 static int imx335_power_off(struct device *dev)
897 struct v4l2_subdev *sd = dev_get_drvdata(dev);
898 struct imx335 *imx335 = to_imx335(sd);
900 gpiod_set_value_cansleep(imx335->reset_gpio, 0);
902 clk_disable_unprepare(imx335->inclk);
908 * imx335_init_controls() - Initialize sensor subdevice controls
909 * @imx335: pointer to imx335 device
911 * Return: 0 if successful, error code otherwise.
913 static int imx335_init_controls(struct imx335 *imx335)
915 struct v4l2_ctrl_handler *ctrl_hdlr = &imx335->ctrl_handler;
916 const struct imx335_mode *mode = imx335->cur_mode;
920 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
924 /* Serialize controls with sensor device */
925 ctrl_hdlr->lock = &imx335->mutex;
927 /* Initialize exposure and gain */
928 lpfr = mode->vblank + mode->height;
929 imx335->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
933 lpfr - IMX335_EXPOSURE_OFFSET,
934 IMX335_EXPOSURE_STEP,
935 IMX335_EXPOSURE_DEFAULT);
937 imx335->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
939 V4L2_CID_ANALOGUE_GAIN,
943 IMX335_AGAIN_DEFAULT);
945 v4l2_ctrl_cluster(2, &imx335->exp_ctrl);
947 imx335->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
954 /* Read only controls */
955 imx335->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
958 mode->pclk, mode->pclk,
961 imx335->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
964 ARRAY_SIZE(link_freq) -
968 if (imx335->link_freq_ctrl)
969 imx335->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
971 imx335->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
977 if (imx335->hblank_ctrl)
978 imx335->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
980 if (ctrl_hdlr->error) {
981 dev_err(imx335->dev, "control init failed: %d",
983 v4l2_ctrl_handler_free(ctrl_hdlr);
984 return ctrl_hdlr->error;
987 imx335->sd.ctrl_handler = ctrl_hdlr;
993 * imx335_probe() - I2C client device binding
994 * @client: pointer to i2c client device
996 * Return: 0 if successful, error code otherwise.
998 static int imx335_probe(struct i2c_client *client)
1000 struct imx335 *imx335;
1003 imx335 = devm_kzalloc(&client->dev, sizeof(*imx335), GFP_KERNEL);
1007 imx335->dev = &client->dev;
1009 /* Initialize subdev */
1010 v4l2_i2c_subdev_init(&imx335->sd, client, &imx335_subdev_ops);
1012 ret = imx335_parse_hw_config(imx335);
1014 dev_err(imx335->dev, "HW configuration is not supported");
1018 mutex_init(&imx335->mutex);
1020 ret = imx335_power_on(imx335->dev);
1022 dev_err(imx335->dev, "failed to power-on the sensor");
1023 goto error_mutex_destroy;
1026 /* Check module identity */
1027 ret = imx335_detect(imx335);
1029 dev_err(imx335->dev, "failed to find sensor: %d", ret);
1030 goto error_power_off;
1033 /* Set default mode to max resolution */
1034 imx335->cur_mode = &supported_mode;
1035 imx335->vblank = imx335->cur_mode->vblank;
1037 ret = imx335_init_controls(imx335);
1039 dev_err(imx335->dev, "failed to init controls: %d", ret);
1040 goto error_power_off;
1043 /* Initialize subdev */
1044 imx335->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1045 imx335->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1047 /* Initialize source pad */
1048 imx335->pad.flags = MEDIA_PAD_FL_SOURCE;
1049 ret = media_entity_pads_init(&imx335->sd.entity, 1, &imx335->pad);
1051 dev_err(imx335->dev, "failed to init entity pads: %d", ret);
1052 goto error_handler_free;
1055 ret = v4l2_async_register_subdev_sensor(&imx335->sd);
1057 dev_err(imx335->dev,
1058 "failed to register async subdev: %d", ret);
1059 goto error_media_entity;
1062 pm_runtime_set_active(imx335->dev);
1063 pm_runtime_enable(imx335->dev);
1064 pm_runtime_idle(imx335->dev);
1069 media_entity_cleanup(&imx335->sd.entity);
1071 v4l2_ctrl_handler_free(imx335->sd.ctrl_handler);
1073 imx335_power_off(imx335->dev);
1074 error_mutex_destroy:
1075 mutex_destroy(&imx335->mutex);
1081 * imx335_remove() - I2C client device unbinding
1082 * @client: pointer to I2C client device
1084 * Return: 0 if successful, error code otherwise.
1086 static void imx335_remove(struct i2c_client *client)
1088 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1089 struct imx335 *imx335 = to_imx335(sd);
1091 v4l2_async_unregister_subdev(sd);
1092 media_entity_cleanup(&sd->entity);
1093 v4l2_ctrl_handler_free(sd->ctrl_handler);
1095 pm_runtime_disable(&client->dev);
1096 if (!pm_runtime_status_suspended(&client->dev))
1097 imx335_power_off(&client->dev);
1098 pm_runtime_set_suspended(&client->dev);
1100 mutex_destroy(&imx335->mutex);
1103 static const struct dev_pm_ops imx335_pm_ops = {
1104 SET_RUNTIME_PM_OPS(imx335_power_off, imx335_power_on, NULL)
1107 static const struct of_device_id imx335_of_match[] = {
1108 { .compatible = "sony,imx335" },
1112 MODULE_DEVICE_TABLE(of, imx335_of_match);
1114 static struct i2c_driver imx335_driver = {
1115 .probe = imx335_probe,
1116 .remove = imx335_remove,
1119 .pm = &imx335_pm_ops,
1120 .of_match_table = imx335_of_match,
1124 module_i2c_driver(imx335_driver);
1126 MODULE_DESCRIPTION("Sony imx335 sensor driver");
1127 MODULE_LICENSE("GPL");