4 * Qualcomm MSM Camera Subsystem - CSID (CSI Decoder) Module
6 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
7 * Copyright (C) 2015-2017 Linaro Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 and
11 * only version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 #include <linux/clk.h>
19 #include <linux/completion.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/consumer.h>
25 #include <media/media-entity.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-subdev.h>
29 #include "camss-csid.h"
32 #define MSM_CSID_NAME "msm_csid"
34 #define CAMSS_CSID_HW_VERSION 0x0
35 #define CAMSS_CSID_CORE_CTRL_0 0x004
36 #define CAMSS_CSID_CORE_CTRL_1 0x008
37 #define CAMSS_CSID_RST_CMD 0x00c
38 #define CAMSS_CSID_CID_LUT_VC_n(n) (0x010 + 0x4 * (n))
39 #define CAMSS_CSID_CID_n_CFG(n) (0x020 + 0x4 * (n))
40 #define CAMSS_CSID_IRQ_CLEAR_CMD 0x060
41 #define CAMSS_CSID_IRQ_MASK 0x064
42 #define CAMSS_CSID_IRQ_STATUS 0x068
43 #define CAMSS_CSID_TG_CTRL 0x0a0
44 #define CAMSS_CSID_TG_CTRL_DISABLE 0xa06436
45 #define CAMSS_CSID_TG_CTRL_ENABLE 0xa06437
46 #define CAMSS_CSID_TG_VC_CFG 0x0a4
47 #define CAMSS_CSID_TG_VC_CFG_H_BLANKING 0x3ff
48 #define CAMSS_CSID_TG_VC_CFG_V_BLANKING 0x7f
49 #define CAMSS_CSID_TG_DT_n_CGG_0(n) (0x0ac + 0xc * (n))
50 #define CAMSS_CSID_TG_DT_n_CGG_1(n) (0x0b0 + 0xc * (n))
51 #define CAMSS_CSID_TG_DT_n_CGG_2(n) (0x0b4 + 0xc * (n))
53 #define DATA_TYPE_EMBEDDED_DATA_8BIT 0x12
54 #define DATA_TYPE_YUV422_8BIT 0x1e
55 #define DATA_TYPE_RAW_6BIT 0x28
56 #define DATA_TYPE_RAW_8BIT 0x2a
57 #define DATA_TYPE_RAW_10BIT 0x2b
58 #define DATA_TYPE_RAW_12BIT 0x2c
60 #define DECODE_FORMAT_UNCOMPRESSED_6_BIT 0x0
61 #define DECODE_FORMAT_UNCOMPRESSED_8_BIT 0x1
62 #define DECODE_FORMAT_UNCOMPRESSED_10_BIT 0x2
63 #define DECODE_FORMAT_UNCOMPRESSED_12_BIT 0x3
65 #define CSID_RESET_TIMEOUT_MS 500
72 u8 spp; /* bus samples per pixel */
75 static const struct csid_fmts csid_input_fmts[] = {
77 MEDIA_BUS_FMT_UYVY8_2X8,
78 DATA_TYPE_YUV422_8BIT,
79 DECODE_FORMAT_UNCOMPRESSED_8_BIT,
84 MEDIA_BUS_FMT_VYUY8_2X8,
85 DATA_TYPE_YUV422_8BIT,
86 DECODE_FORMAT_UNCOMPRESSED_8_BIT,
91 MEDIA_BUS_FMT_YUYV8_2X8,
92 DATA_TYPE_YUV422_8BIT,
93 DECODE_FORMAT_UNCOMPRESSED_8_BIT,
98 MEDIA_BUS_FMT_YVYU8_2X8,
99 DATA_TYPE_YUV422_8BIT,
100 DECODE_FORMAT_UNCOMPRESSED_8_BIT,
105 MEDIA_BUS_FMT_SBGGR8_1X8,
107 DECODE_FORMAT_UNCOMPRESSED_8_BIT,
112 MEDIA_BUS_FMT_SGBRG8_1X8,
114 DECODE_FORMAT_UNCOMPRESSED_8_BIT,
119 MEDIA_BUS_FMT_SGRBG8_1X8,
121 DECODE_FORMAT_UNCOMPRESSED_8_BIT,
126 MEDIA_BUS_FMT_SRGGB8_1X8,
128 DECODE_FORMAT_UNCOMPRESSED_8_BIT,
133 MEDIA_BUS_FMT_SBGGR10_1X10,
135 DECODE_FORMAT_UNCOMPRESSED_10_BIT,
140 MEDIA_BUS_FMT_SGBRG10_1X10,
142 DECODE_FORMAT_UNCOMPRESSED_10_BIT,
147 MEDIA_BUS_FMT_SGRBG10_1X10,
149 DECODE_FORMAT_UNCOMPRESSED_10_BIT,
154 MEDIA_BUS_FMT_SRGGB10_1X10,
156 DECODE_FORMAT_UNCOMPRESSED_10_BIT,
161 MEDIA_BUS_FMT_SBGGR12_1X12,
163 DECODE_FORMAT_UNCOMPRESSED_12_BIT,
168 MEDIA_BUS_FMT_SGBRG12_1X12,
170 DECODE_FORMAT_UNCOMPRESSED_12_BIT,
175 MEDIA_BUS_FMT_SGRBG12_1X12,
177 DECODE_FORMAT_UNCOMPRESSED_12_BIT,
182 MEDIA_BUS_FMT_SRGGB12_1X12,
184 DECODE_FORMAT_UNCOMPRESSED_12_BIT,
190 static const struct csid_fmts *csid_get_fmt_entry(u32 code)
194 for (i = 0; i < ARRAY_SIZE(csid_input_fmts); i++)
195 if (code == csid_input_fmts[i].code)
196 return &csid_input_fmts[i];
198 WARN(1, "Unknown format\n");
200 return &csid_input_fmts[0];
204 * csid_isr - CSID module interrupt handler
205 * @irq: Interrupt line
208 * Return IRQ_HANDLED on success
210 static irqreturn_t csid_isr(int irq, void *dev)
212 struct csid_device *csid = dev;
215 value = readl_relaxed(csid->base + CAMSS_CSID_IRQ_STATUS);
216 writel_relaxed(value, csid->base + CAMSS_CSID_IRQ_CLEAR_CMD);
218 if ((value >> 11) & 0x1)
219 complete(&csid->reset_complete);
225 * csid_set_clock_rates - Calculate and set clock rates on CSID module
226 * @csiphy: CSID device
228 static int csid_set_clock_rates(struct csid_device *csid)
230 struct device *dev = to_device_index(csid, csid->id);
235 ret = camss_get_pixel_clock(&csid->subdev.entity, &pixel_clock);
239 for (i = 0; i < csid->nclocks; i++) {
240 struct camss_clock *clock = &csid->clock[i];
242 if (!strcmp(clock->name, "csi0") ||
243 !strcmp(clock->name, "csi1")) {
244 u8 bpp = csid_get_fmt_entry(
245 csid->fmt[MSM_CSIPHY_PAD_SINK].code)->bpp;
246 u8 num_lanes = csid->phy.lane_cnt;
247 u64 min_rate = pixel_clock * bpp / (2 * num_lanes * 4);
250 camss_add_clock_margin(&min_rate);
252 for (j = 0; j < clock->nfreqs; j++)
253 if (min_rate < clock->freq[j])
256 if (j == clock->nfreqs) {
258 "Pixel clock is too high for CSID\n");
262 /* if sensor pixel clock is not available */
263 /* set highest possible CSID clock rate */
265 j = clock->nfreqs - 1;
267 rate = clk_round_rate(clock->clk, clock->freq[j]);
269 dev_err(dev, "clk round rate failed: %ld\n",
274 ret = clk_set_rate(clock->clk, rate);
276 dev_err(dev, "clk set rate failed: %d\n", ret);
286 * csid_reset - Trigger reset on CSID module and wait to complete
289 * Return 0 on success or a negative error code otherwise
291 static int csid_reset(struct csid_device *csid)
295 reinit_completion(&csid->reset_complete);
297 writel_relaxed(0x7fff, csid->base + CAMSS_CSID_RST_CMD);
299 time = wait_for_completion_timeout(&csid->reset_complete,
300 msecs_to_jiffies(CSID_RESET_TIMEOUT_MS));
302 dev_err(to_device_index(csid, csid->id),
303 "CSID reset timeout\n");
311 * csid_set_power - Power on/off CSID module
312 * @sd: CSID V4L2 subdevice
313 * @on: Requested power state
315 * Return 0 on success or a negative error code otherwise
317 static int csid_set_power(struct v4l2_subdev *sd, int on)
319 struct csid_device *csid = v4l2_get_subdevdata(sd);
320 struct device *dev = to_device_index(csid, csid->id);
326 ret = regulator_enable(csid->vdda);
330 ret = csid_set_clock_rates(csid);
332 regulator_disable(csid->vdda);
336 ret = camss_enable_clocks(csid->nclocks, csid->clock, dev);
338 regulator_disable(csid->vdda);
342 enable_irq(csid->irq);
344 ret = csid_reset(csid);
346 disable_irq(csid->irq);
347 camss_disable_clocks(csid->nclocks, csid->clock);
348 regulator_disable(csid->vdda);
352 hw_version = readl_relaxed(csid->base + CAMSS_CSID_HW_VERSION);
353 dev_dbg(dev, "CSID HW Version = 0x%08x\n", hw_version);
355 disable_irq(csid->irq);
356 camss_disable_clocks(csid->nclocks, csid->clock);
357 ret = regulator_disable(csid->vdda);
364 * csid_set_stream - Enable/disable streaming on CSID module
365 * @sd: CSID V4L2 subdevice
366 * @enable: Requested streaming state
368 * Main configuration of CSID module is also done here.
370 * Return 0 on success or a negative error code otherwise
372 static int csid_set_stream(struct v4l2_subdev *sd, int enable)
374 struct csid_device *csid = v4l2_get_subdevdata(sd);
375 struct csid_testgen_config *tg = &csid->testgen;
379 u8 vc = 0; /* Virtual Channel 0 */
380 u8 cid = vc * 4; /* id of Virtual Channel and Data Type set */
384 ret = v4l2_ctrl_handler_setup(&csid->ctrls);
386 dev_err(to_device_index(csid, csid->id),
387 "could not sync v4l2 controls: %d\n", ret);
392 !media_entity_remote_pad(&csid->pads[MSM_CSID_PAD_SINK]))
395 dt = csid_get_fmt_entry(csid->fmt[MSM_CSID_PAD_SRC].code)->
399 /* Config Test Generator */
400 struct v4l2_mbus_framefmt *f =
401 &csid->fmt[MSM_CSID_PAD_SRC];
402 u8 bpp = csid_get_fmt_entry(f->code)->bpp;
403 u8 spp = csid_get_fmt_entry(f->code)->spp;
404 u32 num_bytes_per_line = f->width * bpp * spp / 8;
405 u32 num_lines = f->height;
407 /* 31:24 V blank, 23:13 H blank, 3:2 num of active DT */
409 val = ((CAMSS_CSID_TG_VC_CFG_V_BLANKING & 0xff) << 24) |
410 ((CAMSS_CSID_TG_VC_CFG_H_BLANKING & 0x7ff) << 13);
411 writel_relaxed(val, csid->base + CAMSS_CSID_TG_VC_CFG);
413 /* 28:16 bytes per lines, 12:0 num of lines */
414 val = ((num_bytes_per_line & 0x1fff) << 16) |
415 (num_lines & 0x1fff);
416 writel_relaxed(val, csid->base +
417 CAMSS_CSID_TG_DT_n_CGG_0(0));
421 writel_relaxed(val, csid->base +
422 CAMSS_CSID_TG_DT_n_CGG_1(0));
424 /* 2:0 output test pattern */
425 val = tg->payload_mode;
426 writel_relaxed(val, csid->base +
427 CAMSS_CSID_TG_DT_n_CGG_2(0));
429 struct csid_phy_config *phy = &csid->phy;
431 val = phy->lane_cnt - 1;
432 val |= phy->lane_assign << 4;
435 csid->base + CAMSS_CSID_CORE_CTRL_0);
437 val = phy->csiphy_id << 17;
441 csid->base + CAMSS_CSID_CORE_CTRL_1);
446 dt_shift = (cid % 4) * 8;
447 df = csid_get_fmt_entry(csid->fmt[MSM_CSID_PAD_SINK].code)->
450 val = readl_relaxed(csid->base + CAMSS_CSID_CID_LUT_VC_n(vc));
451 val &= ~(0xff << dt_shift);
452 val |= dt << dt_shift;
453 writel_relaxed(val, csid->base + CAMSS_CSID_CID_LUT_VC_n(vc));
455 val = (df << 4) | 0x3;
456 writel_relaxed(val, csid->base + CAMSS_CSID_CID_n_CFG(cid));
459 val = CAMSS_CSID_TG_CTRL_ENABLE;
460 writel_relaxed(val, csid->base + CAMSS_CSID_TG_CTRL);
464 val = CAMSS_CSID_TG_CTRL_DISABLE;
465 writel_relaxed(val, csid->base + CAMSS_CSID_TG_CTRL);
473 * __csid_get_format - Get pointer to format structure
475 * @cfg: V4L2 subdev pad configuration
476 * @pad: pad from which format is requested
477 * @which: TRY or ACTIVE format
479 * Return pointer to TRY or ACTIVE format structure
481 static struct v4l2_mbus_framefmt *
482 __csid_get_format(struct csid_device *csid,
483 struct v4l2_subdev_pad_config *cfg,
485 enum v4l2_subdev_format_whence which)
487 if (which == V4L2_SUBDEV_FORMAT_TRY)
488 return v4l2_subdev_get_try_format(&csid->subdev, cfg, pad);
490 return &csid->fmt[pad];
494 * csid_try_format - Handle try format by pad subdev method
496 * @cfg: V4L2 subdev pad configuration
497 * @pad: pad on which format is requested
498 * @fmt: pointer to v4l2 format structure
499 * @which: wanted subdev format
501 static void csid_try_format(struct csid_device *csid,
502 struct v4l2_subdev_pad_config *cfg,
504 struct v4l2_mbus_framefmt *fmt,
505 enum v4l2_subdev_format_whence which)
510 case MSM_CSID_PAD_SINK:
511 /* Set format on sink pad */
513 for (i = 0; i < ARRAY_SIZE(csid_input_fmts); i++)
514 if (fmt->code == csid_input_fmts[i].code)
517 /* If not found, use UYVY as default */
518 if (i >= ARRAY_SIZE(csid_input_fmts))
519 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
521 fmt->width = clamp_t(u32, fmt->width, 1, 8191);
522 fmt->height = clamp_t(u32, fmt->height, 1, 8191);
524 fmt->field = V4L2_FIELD_NONE;
525 fmt->colorspace = V4L2_COLORSPACE_SRGB;
529 case MSM_CSID_PAD_SRC:
530 if (csid->testgen_mode->cur.val == 0) {
531 /* Test generator is disabled, keep pad formats */
532 /* in sync - set and return a format same as sink pad */
533 struct v4l2_mbus_framefmt format;
535 format = *__csid_get_format(csid, cfg,
536 MSM_CSID_PAD_SINK, which);
539 /* Test generator is enabled, set format on source*/
540 /* pad to allow test generator usage */
542 for (i = 0; i < ARRAY_SIZE(csid_input_fmts); i++)
543 if (csid_input_fmts[i].code == fmt->code)
546 /* If not found, use UYVY as default */
547 if (i >= ARRAY_SIZE(csid_input_fmts))
548 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
550 fmt->width = clamp_t(u32, fmt->width, 1, 8191);
551 fmt->height = clamp_t(u32, fmt->height, 1, 8191);
553 fmt->field = V4L2_FIELD_NONE;
558 fmt->colorspace = V4L2_COLORSPACE_SRGB;
562 * csid_enum_mbus_code - Handle pixel format enumeration
563 * @sd: CSID V4L2 subdevice
564 * @cfg: V4L2 subdev pad configuration
565 * @code: pointer to v4l2_subdev_mbus_code_enum structure
566 * return -EINVAL or zero on success
568 static int csid_enum_mbus_code(struct v4l2_subdev *sd,
569 struct v4l2_subdev_pad_config *cfg,
570 struct v4l2_subdev_mbus_code_enum *code)
572 struct csid_device *csid = v4l2_get_subdevdata(sd);
573 struct v4l2_mbus_framefmt *format;
575 if (code->pad == MSM_CSID_PAD_SINK) {
576 if (code->index >= ARRAY_SIZE(csid_input_fmts))
579 code->code = csid_input_fmts[code->index].code;
581 if (csid->testgen_mode->cur.val == 0) {
585 format = __csid_get_format(csid, cfg, MSM_CSID_PAD_SINK,
588 code->code = format->code;
590 if (code->index >= ARRAY_SIZE(csid_input_fmts))
593 code->code = csid_input_fmts[code->index].code;
601 * csid_enum_frame_size - Handle frame size enumeration
602 * @sd: CSID V4L2 subdevice
603 * @cfg: V4L2 subdev pad configuration
604 * @fse: pointer to v4l2_subdev_frame_size_enum structure
605 * return -EINVAL or zero on success
607 static int csid_enum_frame_size(struct v4l2_subdev *sd,
608 struct v4l2_subdev_pad_config *cfg,
609 struct v4l2_subdev_frame_size_enum *fse)
611 struct csid_device *csid = v4l2_get_subdevdata(sd);
612 struct v4l2_mbus_framefmt format;
617 format.code = fse->code;
620 csid_try_format(csid, cfg, fse->pad, &format, fse->which);
621 fse->min_width = format.width;
622 fse->min_height = format.height;
624 if (format.code != fse->code)
627 format.code = fse->code;
630 csid_try_format(csid, cfg, fse->pad, &format, fse->which);
631 fse->max_width = format.width;
632 fse->max_height = format.height;
638 * csid_get_format - Handle get format by pads subdev method
639 * @sd: CSID V4L2 subdevice
640 * @cfg: V4L2 subdev pad configuration
641 * @fmt: pointer to v4l2 subdev format structure
643 * Return -EINVAL or zero on success
645 static int csid_get_format(struct v4l2_subdev *sd,
646 struct v4l2_subdev_pad_config *cfg,
647 struct v4l2_subdev_format *fmt)
649 struct csid_device *csid = v4l2_get_subdevdata(sd);
650 struct v4l2_mbus_framefmt *format;
652 format = __csid_get_format(csid, cfg, fmt->pad, fmt->which);
656 fmt->format = *format;
662 * csid_set_format - Handle set format by pads subdev method
663 * @sd: CSID V4L2 subdevice
664 * @cfg: V4L2 subdev pad configuration
665 * @fmt: pointer to v4l2 subdev format structure
667 * Return -EINVAL or zero on success
669 static int csid_set_format(struct v4l2_subdev *sd,
670 struct v4l2_subdev_pad_config *cfg,
671 struct v4l2_subdev_format *fmt)
673 struct csid_device *csid = v4l2_get_subdevdata(sd);
674 struct v4l2_mbus_framefmt *format;
676 format = __csid_get_format(csid, cfg, fmt->pad, fmt->which);
680 csid_try_format(csid, cfg, fmt->pad, &fmt->format, fmt->which);
681 *format = fmt->format;
683 /* Propagate the format from sink to source */
684 if (fmt->pad == MSM_CSID_PAD_SINK) {
685 format = __csid_get_format(csid, cfg, MSM_CSID_PAD_SRC,
688 *format = fmt->format;
689 csid_try_format(csid, cfg, MSM_CSID_PAD_SRC, format,
697 * csid_init_formats - Initialize formats on all pads
698 * @sd: CSID V4L2 subdevice
699 * @fh: V4L2 subdev file handle
701 * Initialize all pad formats with default values.
703 * Return 0 on success or a negative error code otherwise
705 static int csid_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
707 struct v4l2_subdev_format format = {
708 .pad = MSM_CSID_PAD_SINK,
709 .which = fh ? V4L2_SUBDEV_FORMAT_TRY :
710 V4L2_SUBDEV_FORMAT_ACTIVE,
712 .code = MEDIA_BUS_FMT_UYVY8_2X8,
718 return csid_set_format(sd, fh ? fh->pad : NULL, &format);
721 static const char * const csid_test_pattern_menu[] = {
724 "Alternating 0x55/0xAA",
727 "Pseudo-random Data",
731 * csid_set_test_pattern - Set test generator's pattern mode
733 * @value: desired test pattern mode
735 * Return 0 on success or a negative error code otherwise
737 static int csid_set_test_pattern(struct csid_device *csid, s32 value)
739 struct csid_testgen_config *tg = &csid->testgen;
741 /* If CSID is linked to CSIPHY, do not allow to enable test generator */
742 if (value && media_entity_remote_pad(&csid->pads[MSM_CSID_PAD_SINK]))
745 tg->enabled = !!value;
749 tg->payload_mode = CSID_PAYLOAD_MODE_INCREMENTING;
752 tg->payload_mode = CSID_PAYLOAD_MODE_ALTERNATING_55_AA;
755 tg->payload_mode = CSID_PAYLOAD_MODE_ALL_ZEROES;
758 tg->payload_mode = CSID_PAYLOAD_MODE_ALL_ONES;
761 tg->payload_mode = CSID_PAYLOAD_MODE_RANDOM;
769 * csid_s_ctrl - Handle set control subdev method
770 * @ctrl: pointer to v4l2 control structure
772 * Return 0 on success or a negative error code otherwise
774 static int csid_s_ctrl(struct v4l2_ctrl *ctrl)
776 struct csid_device *csid = container_of(ctrl->handler,
777 struct csid_device, ctrls);
781 case V4L2_CID_TEST_PATTERN:
782 ret = csid_set_test_pattern(csid, ctrl->val);
789 static const struct v4l2_ctrl_ops csid_ctrl_ops = {
790 .s_ctrl = csid_s_ctrl,
794 * msm_csid_subdev_init - Initialize CSID device structure and resources
796 * @res: CSID module resources table
797 * @id: CSID module id
799 * Return 0 on success or a negative error code otherwise
801 int msm_csid_subdev_init(struct csid_device *csid,
802 const struct resources *res, u8 id)
804 struct device *dev = to_device_index(csid, id);
805 struct platform_device *pdev = to_platform_device(dev);
814 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res->reg[0]);
815 csid->base = devm_ioremap_resource(dev, r);
816 if (IS_ERR(csid->base)) {
817 dev_err(dev, "could not map memory\n");
818 return PTR_ERR(csid->base);
823 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
826 dev_err(dev, "missing IRQ\n");
830 csid->irq = r->start;
831 snprintf(csid->irq_name, sizeof(csid->irq_name), "%s_%s%d",
832 dev_name(dev), MSM_CSID_NAME, csid->id);
833 ret = devm_request_irq(dev, csid->irq, csid_isr,
834 IRQF_TRIGGER_RISING, csid->irq_name, csid);
836 dev_err(dev, "request_irq failed: %d\n", ret);
840 disable_irq(csid->irq);
845 while (res->clock[csid->nclocks])
848 csid->clock = devm_kcalloc(dev, csid->nclocks, sizeof(*csid->clock),
853 for (i = 0; i < csid->nclocks; i++) {
854 struct camss_clock *clock = &csid->clock[i];
856 clock->clk = devm_clk_get(dev, res->clock[i]);
857 if (IS_ERR(clock->clk))
858 return PTR_ERR(clock->clk);
860 clock->name = res->clock[i];
863 while (res->clock_rate[i][clock->nfreqs])
866 if (!clock->nfreqs) {
871 clock->freq = devm_kcalloc(dev,
873 sizeof(*clock->freq),
878 for (j = 0; j < clock->nfreqs; j++)
879 clock->freq[j] = res->clock_rate[i][j];
884 csid->vdda = devm_regulator_get(dev, res->regulator[0]);
885 if (IS_ERR(csid->vdda)) {
886 dev_err(dev, "could not get regulator\n");
887 return PTR_ERR(csid->vdda);
890 init_completion(&csid->reset_complete);
896 * msm_csid_get_csid_id - Get CSID HW module id
897 * @entity: Pointer to CSID media entity structure
898 * @id: Return CSID HW module id here
900 void msm_csid_get_csid_id(struct media_entity *entity, u8 *id)
902 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
903 struct csid_device *csid = v4l2_get_subdevdata(sd);
909 * csid_get_lane_assign - Calculate CSI2 lane assign configuration parameter
910 * @lane_cfg - CSI2 lane configuration
914 static u32 csid_get_lane_assign(struct csiphy_lanes_cfg *lane_cfg)
919 for (i = 0; i < lane_cfg->num_data; i++)
920 lane_assign |= lane_cfg->data[i].pos << (i * 4);
926 * csid_link_setup - Setup CSID connections
927 * @entity: Pointer to media entity structure
928 * @local: Pointer to local pad
929 * @remote: Pointer to remote pad
932 * Return 0 on success
934 static int csid_link_setup(struct media_entity *entity,
935 const struct media_pad *local,
936 const struct media_pad *remote, u32 flags)
938 if (flags & MEDIA_LNK_FL_ENABLED)
939 if (media_entity_remote_pad(local))
942 if ((local->flags & MEDIA_PAD_FL_SINK) &&
943 (flags & MEDIA_LNK_FL_ENABLED)) {
944 struct v4l2_subdev *sd;
945 struct csid_device *csid;
946 struct csiphy_device *csiphy;
947 struct csiphy_lanes_cfg *lane_cfg;
948 struct v4l2_subdev_format format = { 0 };
950 sd = media_entity_to_v4l2_subdev(entity);
951 csid = v4l2_get_subdevdata(sd);
953 /* If test generator is enabled */
954 /* do not allow a link from CSIPHY to CSID */
955 if (csid->testgen_mode->cur.val != 0)
958 sd = media_entity_to_v4l2_subdev(remote->entity);
959 csiphy = v4l2_get_subdevdata(sd);
961 /* If a sensor is not linked to CSIPHY */
962 /* do no allow a link from CSIPHY to CSID */
963 if (!csiphy->cfg.csi2)
966 csid->phy.csiphy_id = csiphy->id;
968 lane_cfg = &csiphy->cfg.csi2->lane_cfg;
969 csid->phy.lane_cnt = lane_cfg->num_data;
970 csid->phy.lane_assign = csid_get_lane_assign(lane_cfg);
972 /* Reset format on source pad to sink pad format */
973 format.pad = MSM_CSID_PAD_SRC;
974 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
975 csid_set_format(&csid->subdev, NULL, &format);
981 static const struct v4l2_subdev_core_ops csid_core_ops = {
982 .s_power = csid_set_power,
985 static const struct v4l2_subdev_video_ops csid_video_ops = {
986 .s_stream = csid_set_stream,
989 static const struct v4l2_subdev_pad_ops csid_pad_ops = {
990 .enum_mbus_code = csid_enum_mbus_code,
991 .enum_frame_size = csid_enum_frame_size,
992 .get_fmt = csid_get_format,
993 .set_fmt = csid_set_format,
996 static const struct v4l2_subdev_ops csid_v4l2_ops = {
997 .core = &csid_core_ops,
998 .video = &csid_video_ops,
999 .pad = &csid_pad_ops,
1002 static const struct v4l2_subdev_internal_ops csid_v4l2_internal_ops = {
1003 .open = csid_init_formats,
1006 static const struct media_entity_operations csid_media_ops = {
1007 .link_setup = csid_link_setup,
1008 .link_validate = v4l2_subdev_link_validate,
1012 * msm_csid_register_entity - Register subdev node for CSID module
1013 * @csid: CSID device
1014 * @v4l2_dev: V4L2 device
1016 * Return 0 on success or a negative error code otherwise
1018 int msm_csid_register_entity(struct csid_device *csid,
1019 struct v4l2_device *v4l2_dev)
1021 struct v4l2_subdev *sd = &csid->subdev;
1022 struct media_pad *pads = csid->pads;
1023 struct device *dev = to_device_index(csid, csid->id);
1026 v4l2_subdev_init(sd, &csid_v4l2_ops);
1027 sd->internal_ops = &csid_v4l2_internal_ops;
1028 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1029 snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d",
1030 MSM_CSID_NAME, csid->id);
1031 v4l2_set_subdevdata(sd, csid);
1033 ret = v4l2_ctrl_handler_init(&csid->ctrls, 1);
1035 dev_err(dev, "Failed to init ctrl handler: %d\n", ret);
1039 csid->testgen_mode = v4l2_ctrl_new_std_menu_items(&csid->ctrls,
1040 &csid_ctrl_ops, V4L2_CID_TEST_PATTERN,
1041 ARRAY_SIZE(csid_test_pattern_menu) - 1, 0, 0,
1042 csid_test_pattern_menu);
1044 if (csid->ctrls.error) {
1045 dev_err(dev, "Failed to init ctrl: %d\n", csid->ctrls.error);
1046 ret = csid->ctrls.error;
1050 csid->subdev.ctrl_handler = &csid->ctrls;
1052 ret = csid_init_formats(sd, NULL);
1054 dev_err(dev, "Failed to init format: %d\n", ret);
1058 pads[MSM_CSID_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1059 pads[MSM_CSID_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE;
1061 sd->entity.function = MEDIA_ENT_F_IO_V4L;
1062 sd->entity.ops = &csid_media_ops;
1063 ret = media_entity_pads_init(&sd->entity, MSM_CSID_PADS_NUM, pads);
1065 dev_err(dev, "Failed to init media entity: %d\n", ret);
1069 ret = v4l2_device_register_subdev(v4l2_dev, sd);
1071 dev_err(dev, "Failed to register subdev: %d\n", ret);
1078 media_entity_cleanup(&sd->entity);
1080 v4l2_ctrl_handler_free(&csid->ctrls);
1086 * msm_csid_unregister_entity - Unregister CSID module subdev node
1087 * @csid: CSID device
1089 void msm_csid_unregister_entity(struct csid_device *csid)
1091 v4l2_device_unregister_subdev(&csid->subdev);
1092 media_entity_cleanup(&csid->subdev.entity);
1093 v4l2_ctrl_handler_free(&csid->ctrls);