1 // SPDX-License-Identifier: GPL-2.0
5 * Qualcomm MSM Camera Subsystem - CSID (CSI Decoder) Module
7 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
8 * Copyright (C) 2015-2018 Linaro Ltd.
10 #include <linux/clk.h>
11 #include <linux/completion.h>
12 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regulator/consumer.h>
19 #include <media/media-entity.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-subdev.h>
24 #include "camss-csid.h"
25 #include "camss-csid-gen1.h"
28 /* offset of CSID registers in VFE region for VFE 480 */
29 #define VFE_480_CSID_OFFSET 0x1200
30 #define VFE_480_LITE_CSID_OFFSET 0x200
32 #define MSM_CSID_NAME "msm_csid"
34 const char * const csid_testgen_modes[] = {
37 "Alternating 0x55/0xAA",
48 u32 csid_find_code(u32 *codes, unsigned int ncodes,
49 unsigned int match_format_idx, u32 match_code)
53 if (!match_code && (match_format_idx >= ncodes))
56 for (i = 0; i < ncodes; i++)
58 if (codes[i] == match_code)
61 if (i == match_format_idx)
68 const struct csid_format *csid_get_fmt_entry(const struct csid_format *formats,
69 unsigned int nformats,
74 for (i = 0; i < nformats; i++)
75 if (code == formats[i].code)
78 WARN(1, "Unknown format\n");
84 * csid_set_clock_rates - Calculate and set clock rates on CSID module
85 * @csiphy: CSID device
87 static int csid_set_clock_rates(struct csid_device *csid)
89 struct device *dev = csid->camss->dev;
90 const struct csid_format *fmt;
95 fmt = csid_get_fmt_entry(csid->formats, csid->nformats,
96 csid->fmt[MSM_CSIPHY_PAD_SINK].code);
97 link_freq = camss_get_link_freq(&csid->subdev.entity, fmt->bpp,
102 for (i = 0; i < csid->nclocks; i++) {
103 struct camss_clock *clock = &csid->clock[i];
105 if (!strcmp(clock->name, "csi0") ||
106 !strcmp(clock->name, "csi1") ||
107 !strcmp(clock->name, "csi2") ||
108 !strcmp(clock->name, "csi3")) {
109 u64 min_rate = link_freq / 4;
112 camss_add_clock_margin(&min_rate);
114 for (j = 0; j < clock->nfreqs; j++)
115 if (min_rate < clock->freq[j])
118 if (j == clock->nfreqs) {
120 "Pixel clock is too high for CSID\n");
124 /* if sensor pixel clock is not available */
125 /* set highest possible CSID clock rate */
127 j = clock->nfreqs - 1;
129 rate = clk_round_rate(clock->clk, clock->freq[j]);
131 dev_err(dev, "clk round rate failed: %ld\n",
136 ret = clk_set_rate(clock->clk, rate);
138 dev_err(dev, "clk set rate failed: %d\n", ret);
141 } else if (clock->nfreqs) {
142 clk_set_rate(clock->clk, clock->freq[0]);
150 * csid_set_power - Power on/off CSID module
151 * @sd: CSID V4L2 subdevice
152 * @on: Requested power state
154 * Return 0 on success or a negative error code otherwise
156 static int csid_set_power(struct v4l2_subdev *sd, int on)
158 struct csid_device *csid = v4l2_get_subdevdata(sd);
159 struct camss *camss = csid->camss;
160 struct device *dev = camss->dev;
161 struct vfe_device *vfe = &camss->vfe[csid->id];
166 * From SDM845 onwards, the VFE needs to be powered on before
167 * switching on the CSID. Do so unconditionally, as there is no
168 * drawback in following the same powering order on older SoCs.
174 ret = pm_runtime_resume_and_get(dev);
178 ret = regulator_bulk_enable(csid->num_supplies,
181 pm_runtime_put_sync(dev);
185 ret = csid_set_clock_rates(csid);
187 regulator_bulk_disable(csid->num_supplies,
189 pm_runtime_put_sync(dev);
193 ret = camss_enable_clocks(csid->nclocks, csid->clock, dev);
195 regulator_bulk_disable(csid->num_supplies,
197 pm_runtime_put_sync(dev);
201 csid->phy.need_vc_update = true;
203 enable_irq(csid->irq);
205 ret = csid->ops->reset(csid);
207 disable_irq(csid->irq);
208 camss_disable_clocks(csid->nclocks, csid->clock);
209 regulator_bulk_disable(csid->num_supplies,
211 pm_runtime_put_sync(dev);
215 csid->ops->hw_version(csid);
217 disable_irq(csid->irq);
218 camss_disable_clocks(csid->nclocks, csid->clock);
219 regulator_bulk_disable(csid->num_supplies,
221 pm_runtime_put_sync(dev);
229 * csid_set_stream - Enable/disable streaming on CSID module
230 * @sd: CSID V4L2 subdevice
231 * @enable: Requested streaming state
233 * Main configuration of CSID module is also done here.
235 * Return 0 on success or a negative error code otherwise
237 static int csid_set_stream(struct v4l2_subdev *sd, int enable)
239 struct csid_device *csid = v4l2_get_subdevdata(sd);
243 ret = v4l2_ctrl_handler_setup(&csid->ctrls);
245 dev_err(csid->camss->dev,
246 "could not sync v4l2 controls: %d\n", ret);
250 if (!csid->testgen.enabled &&
251 !media_pad_remote_pad_first(&csid->pads[MSM_CSID_PAD_SINK]))
255 if (csid->phy.need_vc_update) {
256 csid->ops->configure_stream(csid, enable);
257 csid->phy.need_vc_update = false;
264 * __csid_get_format - Get pointer to format structure
266 * @cfg: V4L2 subdev pad configuration
267 * @pad: pad from which format is requested
268 * @which: TRY or ACTIVE format
270 * Return pointer to TRY or ACTIVE format structure
272 static struct v4l2_mbus_framefmt *
273 __csid_get_format(struct csid_device *csid,
274 struct v4l2_subdev_state *sd_state,
276 enum v4l2_subdev_format_whence which)
278 if (which == V4L2_SUBDEV_FORMAT_TRY)
279 return v4l2_subdev_get_try_format(&csid->subdev, sd_state,
282 return &csid->fmt[pad];
286 * csid_try_format - Handle try format by pad subdev method
288 * @cfg: V4L2 subdev pad configuration
289 * @pad: pad on which format is requested
290 * @fmt: pointer to v4l2 format structure
291 * @which: wanted subdev format
293 static void csid_try_format(struct csid_device *csid,
294 struct v4l2_subdev_state *sd_state,
296 struct v4l2_mbus_framefmt *fmt,
297 enum v4l2_subdev_format_whence which)
302 case MSM_CSID_PAD_SINK:
303 /* Set format on sink pad */
305 for (i = 0; i < csid->nformats; i++)
306 if (fmt->code == csid->formats[i].code)
309 /* If not found, use UYVY as default */
310 if (i >= csid->nformats)
311 fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
313 fmt->width = clamp_t(u32, fmt->width, 1, 8191);
314 fmt->height = clamp_t(u32, fmt->height, 1, 8191);
316 fmt->field = V4L2_FIELD_NONE;
317 fmt->colorspace = V4L2_COLORSPACE_SRGB;
321 case MSM_CSID_PAD_SRC:
322 if (csid->testgen_mode->cur.val == 0) {
323 /* Test generator is disabled, */
324 /* keep pad formats in sync */
325 u32 code = fmt->code;
327 *fmt = *__csid_get_format(csid, sd_state,
328 MSM_CSID_PAD_SINK, which);
329 fmt->code = csid->ops->src_pad_code(csid, fmt->code, 0, code);
331 /* Test generator is enabled, set format on source */
332 /* pad to allow test generator usage */
334 for (i = 0; i < csid->nformats; i++)
335 if (csid->formats[i].code == fmt->code)
338 /* If not found, use UYVY as default */
339 if (i >= csid->nformats)
340 fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
342 fmt->width = clamp_t(u32, fmt->width, 1, 8191);
343 fmt->height = clamp_t(u32, fmt->height, 1, 8191);
345 fmt->field = V4L2_FIELD_NONE;
350 fmt->colorspace = V4L2_COLORSPACE_SRGB;
354 * csid_enum_mbus_code - Handle pixel format enumeration
355 * @sd: CSID V4L2 subdevice
356 * @cfg: V4L2 subdev pad configuration
357 * @code: pointer to v4l2_subdev_mbus_code_enum structure
358 * return -EINVAL or zero on success
360 static int csid_enum_mbus_code(struct v4l2_subdev *sd,
361 struct v4l2_subdev_state *sd_state,
362 struct v4l2_subdev_mbus_code_enum *code)
364 struct csid_device *csid = v4l2_get_subdevdata(sd);
366 if (code->pad == MSM_CSID_PAD_SINK) {
367 if (code->index >= csid->nformats)
370 code->code = csid->formats[code->index].code;
372 if (csid->testgen_mode->cur.val == 0) {
373 struct v4l2_mbus_framefmt *sink_fmt;
375 sink_fmt = __csid_get_format(csid, sd_state,
379 code->code = csid->ops->src_pad_code(csid, sink_fmt->code,
384 if (code->index >= csid->nformats)
387 code->code = csid->formats[code->index].code;
395 * csid_enum_frame_size - Handle frame size enumeration
396 * @sd: CSID V4L2 subdevice
397 * @cfg: V4L2 subdev pad configuration
398 * @fse: pointer to v4l2_subdev_frame_size_enum structure
399 * return -EINVAL or zero on success
401 static int csid_enum_frame_size(struct v4l2_subdev *sd,
402 struct v4l2_subdev_state *sd_state,
403 struct v4l2_subdev_frame_size_enum *fse)
405 struct csid_device *csid = v4l2_get_subdevdata(sd);
406 struct v4l2_mbus_framefmt format;
411 format.code = fse->code;
414 csid_try_format(csid, sd_state, fse->pad, &format, fse->which);
415 fse->min_width = format.width;
416 fse->min_height = format.height;
418 if (format.code != fse->code)
421 format.code = fse->code;
424 csid_try_format(csid, sd_state, fse->pad, &format, fse->which);
425 fse->max_width = format.width;
426 fse->max_height = format.height;
432 * csid_get_format - Handle get format by pads subdev method
433 * @sd: CSID V4L2 subdevice
434 * @cfg: V4L2 subdev pad configuration
435 * @fmt: pointer to v4l2 subdev format structure
437 * Return -EINVAL or zero on success
439 static int csid_get_format(struct v4l2_subdev *sd,
440 struct v4l2_subdev_state *sd_state,
441 struct v4l2_subdev_format *fmt)
443 struct csid_device *csid = v4l2_get_subdevdata(sd);
444 struct v4l2_mbus_framefmt *format;
446 format = __csid_get_format(csid, sd_state, fmt->pad, fmt->which);
450 fmt->format = *format;
456 * csid_set_format - Handle set format by pads subdev method
457 * @sd: CSID V4L2 subdevice
458 * @cfg: V4L2 subdev pad configuration
459 * @fmt: pointer to v4l2 subdev format structure
461 * Return -EINVAL or zero on success
463 static int csid_set_format(struct v4l2_subdev *sd,
464 struct v4l2_subdev_state *sd_state,
465 struct v4l2_subdev_format *fmt)
467 struct csid_device *csid = v4l2_get_subdevdata(sd);
468 struct v4l2_mbus_framefmt *format;
471 format = __csid_get_format(csid, sd_state, fmt->pad, fmt->which);
475 csid_try_format(csid, sd_state, fmt->pad, &fmt->format, fmt->which);
476 *format = fmt->format;
478 /* Propagate the format from sink to source pads */
479 if (fmt->pad == MSM_CSID_PAD_SINK) {
480 for (i = MSM_CSID_PAD_FIRST_SRC; i < MSM_CSID_PADS_NUM; ++i) {
481 format = __csid_get_format(csid, sd_state, i, fmt->which);
483 *format = fmt->format;
484 csid_try_format(csid, sd_state, i, format, fmt->which);
492 * csid_init_formats - Initialize formats on all pads
493 * @sd: CSID V4L2 subdevice
494 * @fh: V4L2 subdev file handle
496 * Initialize all pad formats with default values.
498 * Return 0 on success or a negative error code otherwise
500 static int csid_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
502 struct v4l2_subdev_format format = {
503 .pad = MSM_CSID_PAD_SINK,
504 .which = fh ? V4L2_SUBDEV_FORMAT_TRY :
505 V4L2_SUBDEV_FORMAT_ACTIVE,
507 .code = MEDIA_BUS_FMT_UYVY8_1X16,
513 return csid_set_format(sd, fh ? fh->state : NULL, &format);
517 * csid_set_test_pattern - Set test generator's pattern mode
519 * @value: desired test pattern mode
521 * Return 0 on success or a negative error code otherwise
523 static int csid_set_test_pattern(struct csid_device *csid, s32 value)
525 struct csid_testgen_config *tg = &csid->testgen;
527 /* If CSID is linked to CSIPHY, do not allow to enable test generator */
528 if (value && media_pad_remote_pad_first(&csid->pads[MSM_CSID_PAD_SINK]))
531 tg->enabled = !!value;
533 return csid->ops->configure_testgen_pattern(csid, value);
537 * csid_s_ctrl - Handle set control subdev method
538 * @ctrl: pointer to v4l2 control structure
540 * Return 0 on success or a negative error code otherwise
542 static int csid_s_ctrl(struct v4l2_ctrl *ctrl)
544 struct csid_device *csid = container_of(ctrl->handler,
545 struct csid_device, ctrls);
549 case V4L2_CID_TEST_PATTERN:
550 ret = csid_set_test_pattern(csid, ctrl->val);
557 static const struct v4l2_ctrl_ops csid_ctrl_ops = {
558 .s_ctrl = csid_s_ctrl,
562 * msm_csid_subdev_init - Initialize CSID device structure and resources
564 * @res: CSID module resources table
565 * @id: CSID module id
567 * Return 0 on success or a negative error code otherwise
569 int msm_csid_subdev_init(struct camss *camss, struct csid_device *csid,
570 const struct camss_subdev_resources *res, u8 id)
572 struct device *dev = camss->dev;
573 struct platform_device *pdev = to_platform_device(dev);
579 csid->ops = res->ops;
581 csid->ops->subdev_init(csid);
585 if (camss->res->version == CAMSS_8250) {
586 /* for titan 480, CSID registers are inside the VFE region,
587 * between the VFE "top" and "bus" registers. this requires
588 * VFE to be initialized before CSID
590 if (id >= 2) /* VFE/CSID lite */
591 csid->base = camss->vfe[id].base + VFE_480_LITE_CSID_OFFSET;
593 csid->base = camss->vfe[id].base + VFE_480_CSID_OFFSET;
595 csid->base = devm_platform_ioremap_resource_byname(pdev, res->reg[0]);
596 if (IS_ERR(csid->base))
597 return PTR_ERR(csid->base);
602 ret = platform_get_irq_byname(pdev, res->interrupt[0]);
607 snprintf(csid->irq_name, sizeof(csid->irq_name), "%s_%s%d",
608 dev_name(dev), MSM_CSID_NAME, csid->id);
609 ret = devm_request_irq(dev, csid->irq, csid->ops->isr,
610 IRQF_TRIGGER_RISING | IRQF_NO_AUTOEN,
611 csid->irq_name, csid);
613 dev_err(dev, "request_irq failed: %d\n", ret);
620 while (res->clock[csid->nclocks])
623 csid->clock = devm_kcalloc(dev, csid->nclocks, sizeof(*csid->clock),
628 for (i = 0; i < csid->nclocks; i++) {
629 struct camss_clock *clock = &csid->clock[i];
631 clock->clk = devm_clk_get(dev, res->clock[i]);
632 if (IS_ERR(clock->clk))
633 return PTR_ERR(clock->clk);
635 clock->name = res->clock[i];
638 while (res->clock_rate[i][clock->nfreqs])
641 if (!clock->nfreqs) {
646 clock->freq = devm_kcalloc(dev,
648 sizeof(*clock->freq),
653 for (j = 0; j < clock->nfreqs; j++)
654 clock->freq[j] = res->clock_rate[i][j];
658 for (i = 0; i < ARRAY_SIZE(res->regulators); i++) {
659 if (res->regulators[i])
660 csid->num_supplies++;
663 if (csid->num_supplies) {
664 csid->supplies = devm_kmalloc_array(camss->dev,
666 sizeof(*csid->supplies),
672 for (i = 0; i < csid->num_supplies; i++)
673 csid->supplies[i].supply = res->regulators[i];
675 ret = devm_regulator_bulk_get(camss->dev, csid->num_supplies,
680 init_completion(&csid->reset_complete);
686 * msm_csid_get_csid_id - Get CSID HW module id
687 * @entity: Pointer to CSID media entity structure
688 * @id: Return CSID HW module id here
690 void msm_csid_get_csid_id(struct media_entity *entity, u8 *id)
692 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
693 struct csid_device *csid = v4l2_get_subdevdata(sd);
699 * csid_get_lane_assign - Calculate CSI2 lane assign configuration parameter
700 * @lane_cfg - CSI2 lane configuration
704 static u32 csid_get_lane_assign(struct csiphy_lanes_cfg *lane_cfg)
709 for (i = 0; i < lane_cfg->num_data; i++)
710 lane_assign |= lane_cfg->data[i].pos << (i * 4);
716 * csid_link_setup - Setup CSID connections
717 * @entity: Pointer to media entity structure
718 * @local: Pointer to local pad
719 * @remote: Pointer to remote pad
722 * Return 0 on success
724 static int csid_link_setup(struct media_entity *entity,
725 const struct media_pad *local,
726 const struct media_pad *remote, u32 flags)
728 if (flags & MEDIA_LNK_FL_ENABLED)
729 if (media_pad_remote_pad_first(local))
732 if ((local->flags & MEDIA_PAD_FL_SINK) &&
733 (flags & MEDIA_LNK_FL_ENABLED)) {
734 struct v4l2_subdev *sd;
735 struct csid_device *csid;
736 struct csiphy_device *csiphy;
737 struct csiphy_lanes_cfg *lane_cfg;
739 sd = media_entity_to_v4l2_subdev(entity);
740 csid = v4l2_get_subdevdata(sd);
742 /* If test generator is enabled */
743 /* do not allow a link from CSIPHY to CSID */
744 if (csid->testgen_mode->cur.val != 0)
747 sd = media_entity_to_v4l2_subdev(remote->entity);
748 csiphy = v4l2_get_subdevdata(sd);
750 /* If a sensor is not linked to CSIPHY */
751 /* do no allow a link from CSIPHY to CSID */
752 if (!csiphy->cfg.csi2)
755 csid->phy.csiphy_id = csiphy->id;
757 lane_cfg = &csiphy->cfg.csi2->lane_cfg;
758 csid->phy.lane_cnt = lane_cfg->num_data;
759 csid->phy.lane_assign = csid_get_lane_assign(lane_cfg);
761 /* Decide which virtual channels to enable based on which source pads are enabled */
762 if (local->flags & MEDIA_PAD_FL_SOURCE) {
763 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
764 struct csid_device *csid = v4l2_get_subdevdata(sd);
765 struct device *dev = csid->camss->dev;
767 if (flags & MEDIA_LNK_FL_ENABLED)
768 csid->phy.en_vc |= BIT(local->index - 1);
770 csid->phy.en_vc &= ~BIT(local->index - 1);
772 csid->phy.need_vc_update = true;
774 dev_dbg(dev, "%s: Enabled CSID virtual channels mask 0x%x\n",
775 __func__, csid->phy.en_vc);
781 static const struct v4l2_subdev_core_ops csid_core_ops = {
782 .s_power = csid_set_power,
783 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
784 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
787 static const struct v4l2_subdev_video_ops csid_video_ops = {
788 .s_stream = csid_set_stream,
791 static const struct v4l2_subdev_pad_ops csid_pad_ops = {
792 .enum_mbus_code = csid_enum_mbus_code,
793 .enum_frame_size = csid_enum_frame_size,
794 .get_fmt = csid_get_format,
795 .set_fmt = csid_set_format,
798 static const struct v4l2_subdev_ops csid_v4l2_ops = {
799 .core = &csid_core_ops,
800 .video = &csid_video_ops,
801 .pad = &csid_pad_ops,
804 static const struct v4l2_subdev_internal_ops csid_v4l2_internal_ops = {
805 .open = csid_init_formats,
808 static const struct media_entity_operations csid_media_ops = {
809 .link_setup = csid_link_setup,
810 .link_validate = v4l2_subdev_link_validate,
814 * msm_csid_register_entity - Register subdev node for CSID module
816 * @v4l2_dev: V4L2 device
818 * Return 0 on success or a negative error code otherwise
820 int msm_csid_register_entity(struct csid_device *csid,
821 struct v4l2_device *v4l2_dev)
823 struct v4l2_subdev *sd = &csid->subdev;
824 struct media_pad *pads = csid->pads;
825 struct device *dev = csid->camss->dev;
829 v4l2_subdev_init(sd, &csid_v4l2_ops);
830 sd->internal_ops = &csid_v4l2_internal_ops;
831 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
832 V4L2_SUBDEV_FL_HAS_EVENTS;
833 snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d",
834 MSM_CSID_NAME, csid->id);
835 v4l2_set_subdevdata(sd, csid);
837 ret = v4l2_ctrl_handler_init(&csid->ctrls, 1);
839 dev_err(dev, "Failed to init ctrl handler: %d\n", ret);
843 csid->testgen_mode = v4l2_ctrl_new_std_menu_items(&csid->ctrls,
844 &csid_ctrl_ops, V4L2_CID_TEST_PATTERN,
845 csid->testgen.nmodes, 0, 0,
846 csid->testgen.modes);
848 if (csid->ctrls.error) {
849 dev_err(dev, "Failed to init ctrl: %d\n", csid->ctrls.error);
850 ret = csid->ctrls.error;
854 csid->subdev.ctrl_handler = &csid->ctrls;
856 ret = csid_init_formats(sd, NULL);
858 dev_err(dev, "Failed to init format: %d\n", ret);
862 pads[MSM_CSID_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
863 for (i = MSM_CSID_PAD_FIRST_SRC; i < MSM_CSID_PADS_NUM; ++i)
864 pads[i].flags = MEDIA_PAD_FL_SOURCE;
866 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
867 sd->entity.ops = &csid_media_ops;
868 ret = media_entity_pads_init(&sd->entity, MSM_CSID_PADS_NUM, pads);
870 dev_err(dev, "Failed to init media entity: %d\n", ret);
874 ret = v4l2_device_register_subdev(v4l2_dev, sd);
876 dev_err(dev, "Failed to register subdev: %d\n", ret);
883 media_entity_cleanup(&sd->entity);
885 v4l2_ctrl_handler_free(&csid->ctrls);
891 * msm_csid_unregister_entity - Unregister CSID module subdev node
894 void msm_csid_unregister_entity(struct csid_device *csid)
896 v4l2_device_unregister_subdev(&csid->subdev);
897 media_entity_cleanup(&csid->subdev.entity);
898 v4l2_ctrl_handler_free(&csid->ctrls);