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_kzalloc(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_kzalloc(dev
, clock
->nfreqs
*
872 sizeof(*clock
->freq
), GFP_KERNEL
);
876 for (j
= 0; j
< clock
->nfreqs
; j
++)
877 clock
->freq
[j
] = res
->clock_rate
[i
][j
];
882 csid
->vdda
= devm_regulator_get(dev
, res
->regulator
[0]);
883 if (IS_ERR(csid
->vdda
)) {
884 dev_err(dev
, "could not get regulator\n");
885 return PTR_ERR(csid
->vdda
);
888 init_completion(&csid
->reset_complete
);
894 * msm_csid_get_csid_id - Get CSID HW module id
895 * @entity: Pointer to CSID media entity structure
896 * @id: Return CSID HW module id here
898 void msm_csid_get_csid_id(struct media_entity
*entity
, u8
*id
)
900 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
901 struct csid_device
*csid
= v4l2_get_subdevdata(sd
);
907 * csid_get_lane_assign - Calculate CSI2 lane assign configuration parameter
908 * @lane_cfg - CSI2 lane configuration
912 static u32
csid_get_lane_assign(struct csiphy_lanes_cfg
*lane_cfg
)
917 for (i
= 0; i
< lane_cfg
->num_data
; i
++)
918 lane_assign
|= lane_cfg
->data
[i
].pos
<< (i
* 4);
924 * csid_link_setup - Setup CSID connections
925 * @entity: Pointer to media entity structure
926 * @local: Pointer to local pad
927 * @remote: Pointer to remote pad
930 * Return 0 on success
932 static int csid_link_setup(struct media_entity
*entity
,
933 const struct media_pad
*local
,
934 const struct media_pad
*remote
, u32 flags
)
936 if (flags
& MEDIA_LNK_FL_ENABLED
)
937 if (media_entity_remote_pad(local
))
940 if ((local
->flags
& MEDIA_PAD_FL_SINK
) &&
941 (flags
& MEDIA_LNK_FL_ENABLED
)) {
942 struct v4l2_subdev
*sd
;
943 struct csid_device
*csid
;
944 struct csiphy_device
*csiphy
;
945 struct csiphy_lanes_cfg
*lane_cfg
;
946 struct v4l2_subdev_format format
= { 0 };
948 sd
= media_entity_to_v4l2_subdev(entity
);
949 csid
= v4l2_get_subdevdata(sd
);
951 /* If test generator is enabled */
952 /* do not allow a link from CSIPHY to CSID */
953 if (csid
->testgen_mode
->cur
.val
!= 0)
956 sd
= media_entity_to_v4l2_subdev(remote
->entity
);
957 csiphy
= v4l2_get_subdevdata(sd
);
959 /* If a sensor is not linked to CSIPHY */
960 /* do no allow a link from CSIPHY to CSID */
961 if (!csiphy
->cfg
.csi2
)
964 csid
->phy
.csiphy_id
= csiphy
->id
;
966 lane_cfg
= &csiphy
->cfg
.csi2
->lane_cfg
;
967 csid
->phy
.lane_cnt
= lane_cfg
->num_data
;
968 csid
->phy
.lane_assign
= csid_get_lane_assign(lane_cfg
);
970 /* Reset format on source pad to sink pad format */
971 format
.pad
= MSM_CSID_PAD_SRC
;
972 format
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
973 csid_set_format(&csid
->subdev
, NULL
, &format
);
979 static const struct v4l2_subdev_core_ops csid_core_ops
= {
980 .s_power
= csid_set_power
,
983 static const struct v4l2_subdev_video_ops csid_video_ops
= {
984 .s_stream
= csid_set_stream
,
987 static const struct v4l2_subdev_pad_ops csid_pad_ops
= {
988 .enum_mbus_code
= csid_enum_mbus_code
,
989 .enum_frame_size
= csid_enum_frame_size
,
990 .get_fmt
= csid_get_format
,
991 .set_fmt
= csid_set_format
,
994 static const struct v4l2_subdev_ops csid_v4l2_ops
= {
995 .core
= &csid_core_ops
,
996 .video
= &csid_video_ops
,
997 .pad
= &csid_pad_ops
,
1000 static const struct v4l2_subdev_internal_ops csid_v4l2_internal_ops
= {
1001 .open
= csid_init_formats
,
1004 static const struct media_entity_operations csid_media_ops
= {
1005 .link_setup
= csid_link_setup
,
1006 .link_validate
= v4l2_subdev_link_validate
,
1010 * msm_csid_register_entity - Register subdev node for CSID module
1011 * @csid: CSID device
1012 * @v4l2_dev: V4L2 device
1014 * Return 0 on success or a negative error code otherwise
1016 int msm_csid_register_entity(struct csid_device
*csid
,
1017 struct v4l2_device
*v4l2_dev
)
1019 struct v4l2_subdev
*sd
= &csid
->subdev
;
1020 struct media_pad
*pads
= csid
->pads
;
1021 struct device
*dev
= to_device_index(csid
, csid
->id
);
1024 v4l2_subdev_init(sd
, &csid_v4l2_ops
);
1025 sd
->internal_ops
= &csid_v4l2_internal_ops
;
1026 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1027 snprintf(sd
->name
, ARRAY_SIZE(sd
->name
), "%s%d",
1028 MSM_CSID_NAME
, csid
->id
);
1029 v4l2_set_subdevdata(sd
, csid
);
1031 ret
= v4l2_ctrl_handler_init(&csid
->ctrls
, 1);
1033 dev_err(dev
, "Failed to init ctrl handler: %d\n", ret
);
1037 csid
->testgen_mode
= v4l2_ctrl_new_std_menu_items(&csid
->ctrls
,
1038 &csid_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
1039 ARRAY_SIZE(csid_test_pattern_menu
) - 1, 0, 0,
1040 csid_test_pattern_menu
);
1042 if (csid
->ctrls
.error
) {
1043 dev_err(dev
, "Failed to init ctrl: %d\n", csid
->ctrls
.error
);
1044 ret
= csid
->ctrls
.error
;
1048 csid
->subdev
.ctrl_handler
= &csid
->ctrls
;
1050 ret
= csid_init_formats(sd
, NULL
);
1052 dev_err(dev
, "Failed to init format: %d\n", ret
);
1056 pads
[MSM_CSID_PAD_SINK
].flags
= MEDIA_PAD_FL_SINK
;
1057 pads
[MSM_CSID_PAD_SRC
].flags
= MEDIA_PAD_FL_SOURCE
;
1059 sd
->entity
.function
= MEDIA_ENT_F_IO_V4L
;
1060 sd
->entity
.ops
= &csid_media_ops
;
1061 ret
= media_entity_pads_init(&sd
->entity
, MSM_CSID_PADS_NUM
, pads
);
1063 dev_err(dev
, "Failed to init media entity: %d\n", ret
);
1067 ret
= v4l2_device_register_subdev(v4l2_dev
, sd
);
1069 dev_err(dev
, "Failed to register subdev: %d\n", ret
);
1076 media_entity_cleanup(&sd
->entity
);
1078 v4l2_ctrl_handler_free(&csid
->ctrls
);
1084 * msm_csid_unregister_entity - Unregister CSID module subdev node
1085 * @csid: CSID device
1087 void msm_csid_unregister_entity(struct csid_device
*csid
)
1089 v4l2_device_unregister_subdev(&csid
->subdev
);
1090 media_entity_cleanup(&csid
->subdev
.entity
);
1091 v4l2_ctrl_handler_free(&csid
->ctrls
);