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
125 struct i2c_client *client;
126 struct v4l2_subdev sd;
127 struct media_pad pad;
128 struct gpio_desc *reset_gpio;
130 struct v4l2_ctrl_handler ctrl_handler;
131 struct v4l2_ctrl *link_freq_ctrl;
132 struct v4l2_ctrl *pclk_ctrl;
133 struct v4l2_ctrl *hblank_ctrl;
134 struct v4l2_ctrl *vblank_ctrl;
136 struct v4l2_ctrl *exp_ctrl;
137 struct v4l2_ctrl *again_ctrl;
140 const struct imx335_mode *cur_mode;
144 static const s64 link_freq[] = {
148 /* Sensor mode registers */
149 static const struct imx335_reg mode_2592x1940_regs[] = {
236 /* Supported sensor mode configurations */
237 static const struct imx335_mode supported_mode = {
243 .vblank_max = 133060,
246 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
248 .num_of_regs = ARRAY_SIZE(mode_2592x1940_regs),
249 .regs = mode_2592x1940_regs,
254 * to_imx335() - imx335 V4L2 sub-device to imx335 device.
255 * @subdev: pointer to imx335 V4L2 sub-device
257 * Return: pointer to imx335 device
259 static inline struct imx335 *to_imx335(struct v4l2_subdev *subdev)
261 return container_of(subdev, struct imx335, sd);
265 * imx335_read_reg() - Read registers.
266 * @imx335: pointer to imx335 device
267 * @reg: register address
268 * @len: length of bytes to read. Max supported bytes is 4
269 * @val: pointer to register value to be filled.
271 * Big endian register addresses with little endian values.
273 * Return: 0 if successful, error code otherwise.
275 static int imx335_read_reg(struct imx335 *imx335, u16 reg, u32 len, u32 *val)
277 struct i2c_client *client = v4l2_get_subdevdata(&imx335->sd);
278 struct i2c_msg msgs[2] = {0};
279 u8 addr_buf[2] = {0};
280 u8 data_buf[4] = {0};
283 if (WARN_ON(len > 4))
286 put_unaligned_be16(reg, addr_buf);
288 /* Write register address */
289 msgs[0].addr = client->addr;
291 msgs[0].len = ARRAY_SIZE(addr_buf);
292 msgs[0].buf = addr_buf;
294 /* Read data from register */
295 msgs[1].addr = client->addr;
296 msgs[1].flags = I2C_M_RD;
298 msgs[1].buf = data_buf;
300 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
301 if (ret != ARRAY_SIZE(msgs))
304 *val = get_unaligned_le32(data_buf);
310 * imx335_write_reg() - Write register
311 * @imx335: pointer to imx335 device
312 * @reg: register address
313 * @len: length of bytes. Max supported bytes is 4
314 * @val: register value
316 * Big endian register addresses with little endian values.
318 * Return: 0 if successful, error code otherwise.
320 static int imx335_write_reg(struct imx335 *imx335, u16 reg, u32 len, u32 val)
322 struct i2c_client *client = v4l2_get_subdevdata(&imx335->sd);
325 if (WARN_ON(len > 4))
328 put_unaligned_be16(reg, buf);
329 put_unaligned_le32(val, buf + 2);
330 if (i2c_master_send(client, buf, len + 2) != len + 2)
337 * imx335_write_regs() - Write a list of registers
338 * @imx335: pointer to imx335 device
339 * @regs: list of registers to be written
340 * @len: length of registers array
342 * Return: 0 if successful. error code otherwise.
344 static int imx335_write_regs(struct imx335 *imx335,
345 const struct imx335_reg *regs, u32 len)
350 for (i = 0; i < len; i++) {
351 ret = imx335_write_reg(imx335, regs[i].address, 1, regs[i].val);
360 * imx335_update_controls() - Update control ranges based on streaming mode
361 * @imx335: pointer to imx335 device
362 * @mode: pointer to imx335_mode sensor mode
364 * Return: 0 if successful, error code otherwise.
366 static int imx335_update_controls(struct imx335 *imx335,
367 const struct imx335_mode *mode)
371 ret = __v4l2_ctrl_s_ctrl(imx335->link_freq_ctrl, mode->link_freq_idx);
375 ret = __v4l2_ctrl_s_ctrl(imx335->hblank_ctrl, mode->hblank);
379 return __v4l2_ctrl_modify_range(imx335->vblank_ctrl, mode->vblank_min,
380 mode->vblank_max, 1, mode->vblank);
384 * imx335_update_exp_gain() - Set updated exposure and gain
385 * @imx335: pointer to imx335 device
386 * @exposure: updated exposure value
387 * @gain: updated analog gain value
389 * Return: 0 if successful, error code otherwise.
391 static int imx335_update_exp_gain(struct imx335 *imx335, u32 exposure, u32 gain)
396 lpfr = imx335->vblank + imx335->cur_mode->height;
397 shutter = lpfr - exposure;
399 dev_dbg(imx335->dev, "Set exp %u, analog gain %u, shutter %u, lpfr %u",
400 exposure, gain, shutter, lpfr);
402 ret = imx335_write_reg(imx335, IMX335_REG_HOLD, 1, 1);
406 ret = imx335_write_reg(imx335, IMX335_REG_LPFR, 3, lpfr);
408 goto error_release_group_hold;
410 ret = imx335_write_reg(imx335, IMX335_REG_SHUTTER, 3, shutter);
412 goto error_release_group_hold;
414 ret = imx335_write_reg(imx335, IMX335_REG_AGAIN, 2, gain);
416 error_release_group_hold:
417 imx335_write_reg(imx335, IMX335_REG_HOLD, 1, 0);
423 * imx335_set_ctrl() - Set subdevice control
424 * @ctrl: pointer to v4l2_ctrl structure
426 * Supported controls:
428 * - cluster controls:
429 * - V4L2_CID_ANALOGUE_GAIN
430 * - V4L2_CID_EXPOSURE
432 * Return: 0 if successful, error code otherwise.
434 static int imx335_set_ctrl(struct v4l2_ctrl *ctrl)
436 struct imx335 *imx335 =
437 container_of(ctrl->handler, struct imx335, ctrl_handler);
443 case V4L2_CID_VBLANK:
444 imx335->vblank = imx335->vblank_ctrl->val;
446 dev_dbg(imx335->dev, "Received vblank %u, new lpfr %u",
448 imx335->vblank + imx335->cur_mode->height);
450 ret = __v4l2_ctrl_modify_range(imx335->exp_ctrl,
453 imx335->cur_mode->height -
454 IMX335_EXPOSURE_OFFSET,
455 1, IMX335_EXPOSURE_DEFAULT);
457 case V4L2_CID_EXPOSURE:
458 /* Set controls only if sensor is in power on state */
459 if (!pm_runtime_get_if_in_use(imx335->dev))
462 exposure = ctrl->val;
463 analog_gain = imx335->again_ctrl->val;
465 dev_dbg(imx335->dev, "Received exp %u, analog gain %u",
466 exposure, analog_gain);
468 ret = imx335_update_exp_gain(imx335, exposure, analog_gain);
470 pm_runtime_put(imx335->dev);
474 dev_err(imx335->dev, "Invalid control %d", ctrl->id);
481 /* V4l2 subdevice control ops*/
482 static const struct v4l2_ctrl_ops imx335_ctrl_ops = {
483 .s_ctrl = imx335_set_ctrl,
487 * imx335_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
488 * @sd: pointer to imx335 V4L2 sub-device structure
489 * @sd_state: V4L2 sub-device configuration
490 * @code: V4L2 sub-device code enumeration need to be filled
492 * Return: 0 if successful, error code otherwise.
494 static int imx335_enum_mbus_code(struct v4l2_subdev *sd,
495 struct v4l2_subdev_state *sd_state,
496 struct v4l2_subdev_mbus_code_enum *code)
501 code->code = supported_mode.code;
507 * imx335_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
508 * @sd: pointer to imx335 V4L2 sub-device structure
509 * @sd_state: V4L2 sub-device configuration
510 * @fsize: V4L2 sub-device size enumeration need to be filled
512 * Return: 0 if successful, error code otherwise.
514 static int imx335_enum_frame_size(struct v4l2_subdev *sd,
515 struct v4l2_subdev_state *sd_state,
516 struct v4l2_subdev_frame_size_enum *fsize)
518 if (fsize->index > 0)
521 if (fsize->code != supported_mode.code)
524 fsize->min_width = supported_mode.width;
525 fsize->max_width = fsize->min_width;
526 fsize->min_height = supported_mode.height;
527 fsize->max_height = fsize->min_height;
533 * imx335_fill_pad_format() - Fill subdevice pad format
534 * from selected sensor mode
535 * @imx335: pointer to imx335 device
536 * @mode: pointer to imx335_mode sensor mode
537 * @fmt: V4L2 sub-device format need to be filled
539 static void imx335_fill_pad_format(struct imx335 *imx335,
540 const struct imx335_mode *mode,
541 struct v4l2_subdev_format *fmt)
543 fmt->format.width = mode->width;
544 fmt->format.height = mode->height;
545 fmt->format.code = mode->code;
546 fmt->format.field = V4L2_FIELD_NONE;
547 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
548 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
549 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
550 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
554 * imx335_get_pad_format() - Get subdevice pad format
555 * @sd: pointer to imx335 V4L2 sub-device structure
556 * @sd_state: V4L2 sub-device configuration
557 * @fmt: V4L2 sub-device format need to be set
559 * Return: 0 if successful, error code otherwise.
561 static int imx335_get_pad_format(struct v4l2_subdev *sd,
562 struct v4l2_subdev_state *sd_state,
563 struct v4l2_subdev_format *fmt)
565 struct imx335 *imx335 = to_imx335(sd);
567 mutex_lock(&imx335->mutex);
569 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
570 struct v4l2_mbus_framefmt *framefmt;
572 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
573 fmt->format = *framefmt;
575 imx335_fill_pad_format(imx335, imx335->cur_mode, fmt);
578 mutex_unlock(&imx335->mutex);
584 * imx335_set_pad_format() - Set subdevice pad format
585 * @sd: pointer to imx335 V4L2 sub-device structure
586 * @sd_state: V4L2 sub-device configuration
587 * @fmt: V4L2 sub-device format need to be set
589 * Return: 0 if successful, error code otherwise.
591 static int imx335_set_pad_format(struct v4l2_subdev *sd,
592 struct v4l2_subdev_state *sd_state,
593 struct v4l2_subdev_format *fmt)
595 struct imx335 *imx335 = to_imx335(sd);
596 const struct imx335_mode *mode;
599 mutex_lock(&imx335->mutex);
601 mode = &supported_mode;
602 imx335_fill_pad_format(imx335, mode, fmt);
604 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
605 struct v4l2_mbus_framefmt *framefmt;
607 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
608 *framefmt = fmt->format;
610 ret = imx335_update_controls(imx335, mode);
612 imx335->cur_mode = mode;
615 mutex_unlock(&imx335->mutex);
621 * imx335_init_pad_cfg() - Initialize sub-device pad configuration
622 * @sd: pointer to imx335 V4L2 sub-device structure
623 * @sd_state: V4L2 sub-device configuration
625 * Return: 0 if successful, error code otherwise.
627 static int imx335_init_pad_cfg(struct v4l2_subdev *sd,
628 struct v4l2_subdev_state *sd_state)
630 struct imx335 *imx335 = to_imx335(sd);
631 struct v4l2_subdev_format fmt = { 0 };
633 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
634 imx335_fill_pad_format(imx335, &supported_mode, &fmt);
636 return imx335_set_pad_format(sd, sd_state, &fmt);
640 * imx335_start_streaming() - Start sensor stream
641 * @imx335: pointer to imx335 device
643 * Return: 0 if successful, error code otherwise.
645 static int imx335_start_streaming(struct imx335 *imx335)
647 const struct imx335_reg_list *reg_list;
650 /* Write sensor mode registers */
651 reg_list = &imx335->cur_mode->reg_list;
652 ret = imx335_write_regs(imx335, reg_list->regs,
653 reg_list->num_of_regs);
655 dev_err(imx335->dev, "fail to write initial registers");
659 /* Setup handler will write actual exposure and gain */
660 ret = __v4l2_ctrl_handler_setup(imx335->sd.ctrl_handler);
662 dev_err(imx335->dev, "fail to setup handler");
666 /* Start streaming */
667 ret = imx335_write_reg(imx335, IMX335_REG_MODE_SELECT,
668 1, IMX335_MODE_STREAMING);
670 dev_err(imx335->dev, "fail to start streaming");
674 /* Initial regulator stabilization period */
675 usleep_range(18000, 20000);
681 * imx335_stop_streaming() - Stop sensor stream
682 * @imx335: pointer to imx335 device
684 * Return: 0 if successful, error code otherwise.
686 static int imx335_stop_streaming(struct imx335 *imx335)
688 return imx335_write_reg(imx335, IMX335_REG_MODE_SELECT,
689 1, IMX335_MODE_STANDBY);
693 * imx335_set_stream() - Enable sensor streaming
694 * @sd: pointer to imx335 subdevice
695 * @enable: set to enable sensor streaming
697 * Return: 0 if successful, error code otherwise.
699 static int imx335_set_stream(struct v4l2_subdev *sd, int enable)
701 struct imx335 *imx335 = to_imx335(sd);
704 mutex_lock(&imx335->mutex);
707 ret = pm_runtime_resume_and_get(imx335->dev);
711 ret = imx335_start_streaming(imx335);
713 goto error_power_off;
715 imx335_stop_streaming(imx335);
716 pm_runtime_put(imx335->dev);
719 mutex_unlock(&imx335->mutex);
724 pm_runtime_put(imx335->dev);
726 mutex_unlock(&imx335->mutex);
732 * imx335_detect() - Detect imx335 sensor
733 * @imx335: pointer to imx335 device
735 * Return: 0 if successful, -EIO if sensor id does not match
737 static int imx335_detect(struct imx335 *imx335)
742 ret = imx335_read_reg(imx335, IMX335_REG_ID, 2, &val);
746 if (val != IMX335_ID) {
747 dev_err(imx335->dev, "chip id mismatch: %x!=%x",
756 * imx335_parse_hw_config() - Parse HW configuration and check if supported
757 * @imx335: pointer to imx335 device
759 * Return: 0 if successful, error code otherwise.
761 static int imx335_parse_hw_config(struct imx335 *imx335)
763 struct fwnode_handle *fwnode = dev_fwnode(imx335->dev);
764 struct v4l2_fwnode_endpoint bus_cfg = {
765 .bus_type = V4L2_MBUS_CSI2_DPHY
767 struct fwnode_handle *ep;
775 /* Request optional reset pin */
776 imx335->reset_gpio = devm_gpiod_get_optional(imx335->dev, "reset",
778 if (IS_ERR(imx335->reset_gpio)) {
779 dev_err(imx335->dev, "failed to get reset gpio %ld",
780 PTR_ERR(imx335->reset_gpio));
781 return PTR_ERR(imx335->reset_gpio);
784 /* Get sensor input clock */
785 imx335->inclk = devm_clk_get(imx335->dev, NULL);
786 if (IS_ERR(imx335->inclk)) {
787 dev_err(imx335->dev, "could not get inclk");
788 return PTR_ERR(imx335->inclk);
791 rate = clk_get_rate(imx335->inclk);
792 if (rate != IMX335_INCLK_RATE) {
793 dev_err(imx335->dev, "inclk frequency mismatch");
797 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
801 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
802 fwnode_handle_put(ep);
806 if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX335_NUM_DATA_LANES) {
808 "number of CSI2 data lanes %d is not supported",
809 bus_cfg.bus.mipi_csi2.num_data_lanes);
811 goto done_endpoint_free;
814 if (!bus_cfg.nr_of_link_frequencies) {
815 dev_err(imx335->dev, "no link frequencies defined");
817 goto done_endpoint_free;
820 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
821 if (bus_cfg.link_frequencies[i] == IMX335_LINK_FREQ)
822 goto done_endpoint_free;
827 v4l2_fwnode_endpoint_free(&bus_cfg);
832 /* V4l2 subdevice ops */
833 static const struct v4l2_subdev_video_ops imx335_video_ops = {
834 .s_stream = imx335_set_stream,
837 static const struct v4l2_subdev_pad_ops imx335_pad_ops = {
838 .init_cfg = imx335_init_pad_cfg,
839 .enum_mbus_code = imx335_enum_mbus_code,
840 .enum_frame_size = imx335_enum_frame_size,
841 .get_fmt = imx335_get_pad_format,
842 .set_fmt = imx335_set_pad_format,
845 static const struct v4l2_subdev_ops imx335_subdev_ops = {
846 .video = &imx335_video_ops,
847 .pad = &imx335_pad_ops,
851 * imx335_power_on() - Sensor power on sequence
852 * @dev: pointer to i2c device
854 * Return: 0 if successful, error code otherwise.
856 static int imx335_power_on(struct device *dev)
858 struct v4l2_subdev *sd = dev_get_drvdata(dev);
859 struct imx335 *imx335 = to_imx335(sd);
862 gpiod_set_value_cansleep(imx335->reset_gpio, 1);
864 ret = clk_prepare_enable(imx335->inclk);
866 dev_err(imx335->dev, "fail to enable inclk");
870 usleep_range(20, 22);
875 gpiod_set_value_cansleep(imx335->reset_gpio, 0);
881 * imx335_power_off() - Sensor power off sequence
882 * @dev: pointer to i2c device
884 * Return: 0 if successful, error code otherwise.
886 static int imx335_power_off(struct device *dev)
888 struct v4l2_subdev *sd = dev_get_drvdata(dev);
889 struct imx335 *imx335 = to_imx335(sd);
891 gpiod_set_value_cansleep(imx335->reset_gpio, 0);
893 clk_disable_unprepare(imx335->inclk);
899 * imx335_init_controls() - Initialize sensor subdevice controls
900 * @imx335: pointer to imx335 device
902 * Return: 0 if successful, error code otherwise.
904 static int imx335_init_controls(struct imx335 *imx335)
906 struct v4l2_ctrl_handler *ctrl_hdlr = &imx335->ctrl_handler;
907 const struct imx335_mode *mode = imx335->cur_mode;
911 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
915 /* Serialize controls with sensor device */
916 ctrl_hdlr->lock = &imx335->mutex;
918 /* Initialize exposure and gain */
919 lpfr = mode->vblank + mode->height;
920 imx335->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
924 lpfr - IMX335_EXPOSURE_OFFSET,
925 IMX335_EXPOSURE_STEP,
926 IMX335_EXPOSURE_DEFAULT);
928 imx335->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
930 V4L2_CID_ANALOGUE_GAIN,
934 IMX335_AGAIN_DEFAULT);
936 v4l2_ctrl_cluster(2, &imx335->exp_ctrl);
938 imx335->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
945 /* Read only controls */
946 imx335->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
949 mode->pclk, mode->pclk,
952 imx335->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
955 ARRAY_SIZE(link_freq) -
959 if (imx335->link_freq_ctrl)
960 imx335->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
962 imx335->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
968 if (imx335->hblank_ctrl)
969 imx335->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
971 if (ctrl_hdlr->error) {
972 dev_err(imx335->dev, "control init failed: %d",
974 v4l2_ctrl_handler_free(ctrl_hdlr);
975 return ctrl_hdlr->error;
978 imx335->sd.ctrl_handler = ctrl_hdlr;
984 * imx335_probe() - I2C client device binding
985 * @client: pointer to i2c client device
987 * Return: 0 if successful, error code otherwise.
989 static int imx335_probe(struct i2c_client *client)
991 struct imx335 *imx335;
994 imx335 = devm_kzalloc(&client->dev, sizeof(*imx335), GFP_KERNEL);
998 imx335->dev = &client->dev;
1000 /* Initialize subdev */
1001 v4l2_i2c_subdev_init(&imx335->sd, client, &imx335_subdev_ops);
1003 ret = imx335_parse_hw_config(imx335);
1005 dev_err(imx335->dev, "HW configuration is not supported");
1009 mutex_init(&imx335->mutex);
1011 ret = imx335_power_on(imx335->dev);
1013 dev_err(imx335->dev, "failed to power-on the sensor");
1014 goto error_mutex_destroy;
1017 /* Check module identity */
1018 ret = imx335_detect(imx335);
1020 dev_err(imx335->dev, "failed to find sensor: %d", ret);
1021 goto error_power_off;
1024 /* Set default mode to max resolution */
1025 imx335->cur_mode = &supported_mode;
1026 imx335->vblank = imx335->cur_mode->vblank;
1028 ret = imx335_init_controls(imx335);
1030 dev_err(imx335->dev, "failed to init controls: %d", ret);
1031 goto error_power_off;
1034 /* Initialize subdev */
1035 imx335->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1036 imx335->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1038 /* Initialize source pad */
1039 imx335->pad.flags = MEDIA_PAD_FL_SOURCE;
1040 ret = media_entity_pads_init(&imx335->sd.entity, 1, &imx335->pad);
1042 dev_err(imx335->dev, "failed to init entity pads: %d", ret);
1043 goto error_handler_free;
1046 ret = v4l2_async_register_subdev_sensor(&imx335->sd);
1048 dev_err(imx335->dev,
1049 "failed to register async subdev: %d", ret);
1050 goto error_media_entity;
1053 pm_runtime_set_active(imx335->dev);
1054 pm_runtime_enable(imx335->dev);
1055 pm_runtime_idle(imx335->dev);
1060 media_entity_cleanup(&imx335->sd.entity);
1062 v4l2_ctrl_handler_free(imx335->sd.ctrl_handler);
1064 imx335_power_off(imx335->dev);
1065 error_mutex_destroy:
1066 mutex_destroy(&imx335->mutex);
1072 * imx335_remove() - I2C client device unbinding
1073 * @client: pointer to I2C client device
1075 * Return: 0 if successful, error code otherwise.
1077 static void imx335_remove(struct i2c_client *client)
1079 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1080 struct imx335 *imx335 = to_imx335(sd);
1082 v4l2_async_unregister_subdev(sd);
1083 media_entity_cleanup(&sd->entity);
1084 v4l2_ctrl_handler_free(sd->ctrl_handler);
1086 pm_runtime_disable(&client->dev);
1087 if (!pm_runtime_status_suspended(&client->dev))
1088 imx335_power_off(&client->dev);
1089 pm_runtime_set_suspended(&client->dev);
1091 mutex_destroy(&imx335->mutex);
1094 static const struct dev_pm_ops imx335_pm_ops = {
1095 SET_RUNTIME_PM_OPS(imx335_power_off, imx335_power_on, NULL)
1098 static const struct of_device_id imx335_of_match[] = {
1099 { .compatible = "sony,imx335" },
1103 MODULE_DEVICE_TABLE(of, imx335_of_match);
1105 static struct i2c_driver imx335_driver = {
1106 .probe = imx335_probe,
1107 .remove = imx335_remove,
1110 .pm = &imx335_pm_ops,
1111 .of_match_table = imx335_of_match,
1115 module_i2c_driver(imx335_driver);
1117 MODULE_DESCRIPTION("Sony imx335 sensor driver");
1118 MODULE_LICENSE("GPL");