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 #define MSM_CSID_NAME "msm_csid"
30 const char * const csid_testgen_modes[] = {
33 "Alternating 0x55/0xAA",
44 u32 csid_find_code(u32 *codes, unsigned int ncodes,
45 unsigned int match_format_idx, u32 match_code)
49 if (!match_code && (match_format_idx >= ncodes))
52 for (i = 0; i < ncodes; i++)
54 if (codes[i] == match_code)
57 if (i == match_format_idx)
64 const struct csid_format *csid_get_fmt_entry(const struct csid_format *formats,
65 unsigned int nformats,
70 for (i = 0; i < nformats; i++)
71 if (code == formats[i].code)
74 WARN(1, "Unknown format\n");
80 * csid_set_clock_rates - Calculate and set clock rates on CSID module
81 * @csiphy: CSID device
83 static int csid_set_clock_rates(struct csid_device *csid)
85 struct device *dev = csid->camss->dev;
86 const struct csid_format *fmt;
91 fmt = csid_get_fmt_entry(csid->formats, csid->nformats,
92 csid->fmt[MSM_CSIPHY_PAD_SINK].code);
93 link_freq = camss_get_link_freq(&csid->subdev.entity, fmt->bpp,
98 for (i = 0; i < csid->nclocks; i++) {
99 struct camss_clock *clock = &csid->clock[i];
101 if (!strcmp(clock->name, "csi0") ||
102 !strcmp(clock->name, "csi1") ||
103 !strcmp(clock->name, "csi2") ||
104 !strcmp(clock->name, "csi3")) {
105 u64 min_rate = link_freq / 4;
108 camss_add_clock_margin(&min_rate);
110 for (j = 0; j < clock->nfreqs; j++)
111 if (min_rate < clock->freq[j])
114 if (j == clock->nfreqs) {
116 "Pixel clock is too high for CSID\n");
120 /* if sensor pixel clock is not available */
121 /* set highest possible CSID clock rate */
123 j = clock->nfreqs - 1;
125 rate = clk_round_rate(clock->clk, clock->freq[j]);
127 dev_err(dev, "clk round rate failed: %ld\n",
132 ret = clk_set_rate(clock->clk, rate);
134 dev_err(dev, "clk set rate failed: %d\n", ret);
137 } else if (clock->nfreqs) {
138 clk_set_rate(clock->clk, clock->freq[0]);
146 * csid_set_power - Power on/off CSID module
147 * @sd: CSID V4L2 subdevice
148 * @on: Requested power state
150 * Return 0 on success or a negative error code otherwise
152 static int csid_set_power(struct v4l2_subdev *sd, int on)
154 struct csid_device *csid = v4l2_get_subdevdata(sd);
155 struct device *dev = csid->camss->dev;
159 ret = pm_runtime_get_sync(dev);
161 pm_runtime_put_sync(dev);
165 ret = regulator_enable(csid->vdda);
167 pm_runtime_put_sync(dev);
171 ret = csid_set_clock_rates(csid);
173 regulator_disable(csid->vdda);
174 pm_runtime_put_sync(dev);
178 ret = camss_enable_clocks(csid->nclocks, csid->clock, dev);
180 regulator_disable(csid->vdda);
181 pm_runtime_put_sync(dev);
185 enable_irq(csid->irq);
187 ret = csid->ops->reset(csid);
189 disable_irq(csid->irq);
190 camss_disable_clocks(csid->nclocks, csid->clock);
191 regulator_disable(csid->vdda);
192 pm_runtime_put_sync(dev);
196 csid->ops->hw_version(csid);
198 disable_irq(csid->irq);
199 camss_disable_clocks(csid->nclocks, csid->clock);
200 ret = regulator_disable(csid->vdda);
201 pm_runtime_put_sync(dev);
208 * csid_set_stream - Enable/disable streaming on CSID module
209 * @sd: CSID V4L2 subdevice
210 * @enable: Requested streaming state
212 * Main configuration of CSID module is also done here.
214 * Return 0 on success or a negative error code otherwise
216 static int csid_set_stream(struct v4l2_subdev *sd, int enable)
218 struct csid_device *csid = v4l2_get_subdevdata(sd);
222 ret = v4l2_ctrl_handler_setup(&csid->ctrls);
224 dev_err(csid->camss->dev,
225 "could not sync v4l2 controls: %d\n", ret);
229 if (!csid->testgen.enabled &&
230 !media_entity_remote_pad(&csid->pads[MSM_CSID_PAD_SINK]))
234 csid->ops->configure_stream(csid, enable);
240 * __csid_get_format - Get pointer to format structure
242 * @cfg: V4L2 subdev pad configuration
243 * @pad: pad from which format is requested
244 * @which: TRY or ACTIVE format
246 * Return pointer to TRY or ACTIVE format structure
248 static struct v4l2_mbus_framefmt *
249 __csid_get_format(struct csid_device *csid,
250 struct v4l2_subdev_pad_config *cfg,
252 enum v4l2_subdev_format_whence which)
254 if (which == V4L2_SUBDEV_FORMAT_TRY)
255 return v4l2_subdev_get_try_format(&csid->subdev, cfg, pad);
257 return &csid->fmt[pad];
261 * csid_try_format - Handle try format by pad subdev method
263 * @cfg: V4L2 subdev pad configuration
264 * @pad: pad on which format is requested
265 * @fmt: pointer to v4l2 format structure
266 * @which: wanted subdev format
268 static void csid_try_format(struct csid_device *csid,
269 struct v4l2_subdev_pad_config *cfg,
271 struct v4l2_mbus_framefmt *fmt,
272 enum v4l2_subdev_format_whence which)
277 case MSM_CSID_PAD_SINK:
278 /* Set format on sink pad */
280 for (i = 0; i < csid->nformats; i++)
281 if (fmt->code == csid->formats[i].code)
284 /* If not found, use UYVY as default */
285 if (i >= csid->nformats)
286 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
288 fmt->width = clamp_t(u32, fmt->width, 1, 8191);
289 fmt->height = clamp_t(u32, fmt->height, 1, 8191);
291 fmt->field = V4L2_FIELD_NONE;
292 fmt->colorspace = V4L2_COLORSPACE_SRGB;
296 case MSM_CSID_PAD_SRC:
297 if (csid->testgen_mode->cur.val == 0) {
298 /* Test generator is disabled, */
299 /* keep pad formats in sync */
300 u32 code = fmt->code;
302 *fmt = *__csid_get_format(csid, cfg,
303 MSM_CSID_PAD_SINK, which);
304 fmt->code = csid->ops->src_pad_code(csid, fmt->code, 0, code);
306 /* Test generator is enabled, set format on source */
307 /* pad to allow test generator usage */
309 for (i = 0; i < csid->nformats; i++)
310 if (csid->formats[i].code == fmt->code)
313 /* If not found, use UYVY as default */
314 if (i >= csid->nformats)
315 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
317 fmt->width = clamp_t(u32, fmt->width, 1, 8191);
318 fmt->height = clamp_t(u32, fmt->height, 1, 8191);
320 fmt->field = V4L2_FIELD_NONE;
325 fmt->colorspace = V4L2_COLORSPACE_SRGB;
329 * csid_enum_mbus_code - Handle pixel format enumeration
330 * @sd: CSID V4L2 subdevice
331 * @cfg: V4L2 subdev pad configuration
332 * @code: pointer to v4l2_subdev_mbus_code_enum structure
333 * return -EINVAL or zero on success
335 static int csid_enum_mbus_code(struct v4l2_subdev *sd,
336 struct v4l2_subdev_pad_config *cfg,
337 struct v4l2_subdev_mbus_code_enum *code)
339 struct csid_device *csid = v4l2_get_subdevdata(sd);
341 if (code->pad == MSM_CSID_PAD_SINK) {
342 if (code->index >= csid->nformats)
345 code->code = csid->formats[code->index].code;
347 if (csid->testgen_mode->cur.val == 0) {
348 struct v4l2_mbus_framefmt *sink_fmt;
350 sink_fmt = __csid_get_format(csid, cfg,
354 code->code = csid->ops->src_pad_code(csid, sink_fmt->code,
359 if (code->index >= csid->nformats)
362 code->code = csid->formats[code->index].code;
370 * csid_enum_frame_size - Handle frame size enumeration
371 * @sd: CSID V4L2 subdevice
372 * @cfg: V4L2 subdev pad configuration
373 * @fse: pointer to v4l2_subdev_frame_size_enum structure
374 * return -EINVAL or zero on success
376 static int csid_enum_frame_size(struct v4l2_subdev *sd,
377 struct v4l2_subdev_pad_config *cfg,
378 struct v4l2_subdev_frame_size_enum *fse)
380 struct csid_device *csid = v4l2_get_subdevdata(sd);
381 struct v4l2_mbus_framefmt format;
386 format.code = fse->code;
389 csid_try_format(csid, cfg, fse->pad, &format, fse->which);
390 fse->min_width = format.width;
391 fse->min_height = format.height;
393 if (format.code != fse->code)
396 format.code = fse->code;
399 csid_try_format(csid, cfg, fse->pad, &format, fse->which);
400 fse->max_width = format.width;
401 fse->max_height = format.height;
407 * csid_get_format - Handle get format by pads subdev method
408 * @sd: CSID V4L2 subdevice
409 * @cfg: V4L2 subdev pad configuration
410 * @fmt: pointer to v4l2 subdev format structure
412 * Return -EINVAL or zero on success
414 static int csid_get_format(struct v4l2_subdev *sd,
415 struct v4l2_subdev_pad_config *cfg,
416 struct v4l2_subdev_format *fmt)
418 struct csid_device *csid = v4l2_get_subdevdata(sd);
419 struct v4l2_mbus_framefmt *format;
421 format = __csid_get_format(csid, cfg, fmt->pad, fmt->which);
425 fmt->format = *format;
431 * csid_set_format - Handle set format by pads subdev method
432 * @sd: CSID V4L2 subdevice
433 * @cfg: V4L2 subdev pad configuration
434 * @fmt: pointer to v4l2 subdev format structure
436 * Return -EINVAL or zero on success
438 static int csid_set_format(struct v4l2_subdev *sd,
439 struct v4l2_subdev_pad_config *cfg,
440 struct v4l2_subdev_format *fmt)
442 struct csid_device *csid = v4l2_get_subdevdata(sd);
443 struct v4l2_mbus_framefmt *format;
445 format = __csid_get_format(csid, cfg, fmt->pad, fmt->which);
449 csid_try_format(csid, cfg, fmt->pad, &fmt->format, fmt->which);
450 *format = fmt->format;
452 /* Propagate the format from sink to source */
453 if (fmt->pad == MSM_CSID_PAD_SINK) {
454 format = __csid_get_format(csid, cfg, MSM_CSID_PAD_SRC,
457 *format = fmt->format;
458 csid_try_format(csid, cfg, MSM_CSID_PAD_SRC, format,
466 * csid_init_formats - Initialize formats on all pads
467 * @sd: CSID V4L2 subdevice
468 * @fh: V4L2 subdev file handle
470 * Initialize all pad formats with default values.
472 * Return 0 on success or a negative error code otherwise
474 static int csid_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
476 struct v4l2_subdev_format format = {
477 .pad = MSM_CSID_PAD_SINK,
478 .which = fh ? V4L2_SUBDEV_FORMAT_TRY :
479 V4L2_SUBDEV_FORMAT_ACTIVE,
481 .code = MEDIA_BUS_FMT_UYVY8_2X8,
487 return csid_set_format(sd, fh ? fh->pad : NULL, &format);
491 * csid_set_test_pattern - Set test generator's pattern mode
493 * @value: desired test pattern mode
495 * Return 0 on success or a negative error code otherwise
497 static int csid_set_test_pattern(struct csid_device *csid, s32 value)
499 struct csid_testgen_config *tg = &csid->testgen;
501 /* If CSID is linked to CSIPHY, do not allow to enable test generator */
502 if (value && media_entity_remote_pad(&csid->pads[MSM_CSID_PAD_SINK]))
505 tg->enabled = !!value;
507 return csid->ops->configure_testgen_pattern(csid, value);
511 * csid_s_ctrl - Handle set control subdev method
512 * @ctrl: pointer to v4l2 control structure
514 * Return 0 on success or a negative error code otherwise
516 static int csid_s_ctrl(struct v4l2_ctrl *ctrl)
518 struct csid_device *csid = container_of(ctrl->handler,
519 struct csid_device, ctrls);
523 case V4L2_CID_TEST_PATTERN:
524 ret = csid_set_test_pattern(csid, ctrl->val);
531 static const struct v4l2_ctrl_ops csid_ctrl_ops = {
532 .s_ctrl = csid_s_ctrl,
536 * msm_csid_subdev_init - Initialize CSID device structure and resources
538 * @res: CSID module resources table
539 * @id: CSID module id
541 * Return 0 on success or a negative error code otherwise
543 int msm_csid_subdev_init(struct camss *camss, struct csid_device *csid,
544 const struct resources *res, u8 id)
546 struct device *dev = camss->dev;
547 struct platform_device *pdev = to_platform_device(dev);
555 if (camss->version == CAMSS_8x16) {
556 csid->ops = &csid_ops_4_1;
557 } else if (camss->version == CAMSS_8x96 ||
558 camss->version == CAMSS_660) {
559 csid->ops = &csid_ops_4_7;
560 } else if (camss->version == CAMSS_845) {
561 csid->ops = &csid_ops_170;
565 csid->ops->subdev_init(csid);
569 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res->reg[0]);
570 csid->base = devm_ioremap_resource(dev, r);
571 if (IS_ERR(csid->base))
572 return PTR_ERR(csid->base);
576 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
579 dev_err(dev, "missing IRQ\n");
583 csid->irq = r->start;
584 snprintf(csid->irq_name, sizeof(csid->irq_name), "%s_%s%d",
585 dev_name(dev), MSM_CSID_NAME, csid->id);
586 ret = devm_request_irq(dev, csid->irq, csid->ops->isr,
587 IRQF_TRIGGER_RISING, csid->irq_name, csid);
589 dev_err(dev, "request_irq failed: %d\n", ret);
593 disable_irq(csid->irq);
598 while (res->clock[csid->nclocks])
601 csid->clock = devm_kcalloc(dev, csid->nclocks, sizeof(*csid->clock),
606 for (i = 0; i < csid->nclocks; i++) {
607 struct camss_clock *clock = &csid->clock[i];
609 clock->clk = devm_clk_get(dev, res->clock[i]);
610 if (IS_ERR(clock->clk))
611 return PTR_ERR(clock->clk);
613 clock->name = res->clock[i];
616 while (res->clock_rate[i][clock->nfreqs])
619 if (!clock->nfreqs) {
624 clock->freq = devm_kcalloc(dev,
626 sizeof(*clock->freq),
631 for (j = 0; j < clock->nfreqs; j++)
632 clock->freq[j] = res->clock_rate[i][j];
637 csid->vdda = devm_regulator_get(dev, res->regulator[0]);
638 if (IS_ERR(csid->vdda)) {
639 dev_err(dev, "could not get regulator\n");
640 return PTR_ERR(csid->vdda);
643 init_completion(&csid->reset_complete);
649 * msm_csid_get_csid_id - Get CSID HW module id
650 * @entity: Pointer to CSID media entity structure
651 * @id: Return CSID HW module id here
653 void msm_csid_get_csid_id(struct media_entity *entity, u8 *id)
655 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
656 struct csid_device *csid = v4l2_get_subdevdata(sd);
662 * csid_get_lane_assign - Calculate CSI2 lane assign configuration parameter
663 * @lane_cfg - CSI2 lane configuration
667 static u32 csid_get_lane_assign(struct csiphy_lanes_cfg *lane_cfg)
672 for (i = 0; i < lane_cfg->num_data; i++)
673 lane_assign |= lane_cfg->data[i].pos << (i * 4);
679 * csid_link_setup - Setup CSID connections
680 * @entity: Pointer to media entity structure
681 * @local: Pointer to local pad
682 * @remote: Pointer to remote pad
685 * Return 0 on success
687 static int csid_link_setup(struct media_entity *entity,
688 const struct media_pad *local,
689 const struct media_pad *remote, u32 flags)
691 if (flags & MEDIA_LNK_FL_ENABLED)
692 if (media_entity_remote_pad(local))
695 if ((local->flags & MEDIA_PAD_FL_SINK) &&
696 (flags & MEDIA_LNK_FL_ENABLED)) {
697 struct v4l2_subdev *sd;
698 struct csid_device *csid;
699 struct csiphy_device *csiphy;
700 struct csiphy_lanes_cfg *lane_cfg;
701 struct v4l2_subdev_format format = { 0 };
703 sd = media_entity_to_v4l2_subdev(entity);
704 csid = v4l2_get_subdevdata(sd);
706 /* If test generator is enabled */
707 /* do not allow a link from CSIPHY to CSID */
708 if (csid->testgen_mode->cur.val != 0)
711 sd = media_entity_to_v4l2_subdev(remote->entity);
712 csiphy = v4l2_get_subdevdata(sd);
714 /* If a sensor is not linked to CSIPHY */
715 /* do no allow a link from CSIPHY to CSID */
716 if (!csiphy->cfg.csi2)
719 csid->phy.csiphy_id = csiphy->id;
721 lane_cfg = &csiphy->cfg.csi2->lane_cfg;
722 csid->phy.lane_cnt = lane_cfg->num_data;
723 csid->phy.lane_assign = csid_get_lane_assign(lane_cfg);
725 /* Reset format on source pad to sink pad format */
726 format.pad = MSM_CSID_PAD_SRC;
727 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
728 csid_set_format(&csid->subdev, NULL, &format);
734 static const struct v4l2_subdev_core_ops csid_core_ops = {
735 .s_power = csid_set_power,
736 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
737 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
740 static const struct v4l2_subdev_video_ops csid_video_ops = {
741 .s_stream = csid_set_stream,
744 static const struct v4l2_subdev_pad_ops csid_pad_ops = {
745 .enum_mbus_code = csid_enum_mbus_code,
746 .enum_frame_size = csid_enum_frame_size,
747 .get_fmt = csid_get_format,
748 .set_fmt = csid_set_format,
751 static const struct v4l2_subdev_ops csid_v4l2_ops = {
752 .core = &csid_core_ops,
753 .video = &csid_video_ops,
754 .pad = &csid_pad_ops,
757 static const struct v4l2_subdev_internal_ops csid_v4l2_internal_ops = {
758 .open = csid_init_formats,
761 static const struct media_entity_operations csid_media_ops = {
762 .link_setup = csid_link_setup,
763 .link_validate = v4l2_subdev_link_validate,
767 * msm_csid_register_entity - Register subdev node for CSID module
769 * @v4l2_dev: V4L2 device
771 * Return 0 on success or a negative error code otherwise
773 int msm_csid_register_entity(struct csid_device *csid,
774 struct v4l2_device *v4l2_dev)
776 struct v4l2_subdev *sd = &csid->subdev;
777 struct media_pad *pads = csid->pads;
778 struct device *dev = csid->camss->dev;
781 v4l2_subdev_init(sd, &csid_v4l2_ops);
782 sd->internal_ops = &csid_v4l2_internal_ops;
783 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
784 V4L2_SUBDEV_FL_HAS_EVENTS;
785 snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d",
786 MSM_CSID_NAME, csid->id);
787 v4l2_set_subdevdata(sd, csid);
789 ret = v4l2_ctrl_handler_init(&csid->ctrls, 1);
791 dev_err(dev, "Failed to init ctrl handler: %d\n", ret);
795 csid->testgen_mode = v4l2_ctrl_new_std_menu_items(&csid->ctrls,
796 &csid_ctrl_ops, V4L2_CID_TEST_PATTERN,
797 csid->testgen.nmodes, 0, 0,
798 csid->testgen.modes);
800 if (csid->ctrls.error) {
801 dev_err(dev, "Failed to init ctrl: %d\n", csid->ctrls.error);
802 ret = csid->ctrls.error;
806 csid->subdev.ctrl_handler = &csid->ctrls;
808 ret = csid_init_formats(sd, NULL);
810 dev_err(dev, "Failed to init format: %d\n", ret);
814 pads[MSM_CSID_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
815 pads[MSM_CSID_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE;
817 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
818 sd->entity.ops = &csid_media_ops;
819 ret = media_entity_pads_init(&sd->entity, MSM_CSID_PADS_NUM, pads);
821 dev_err(dev, "Failed to init media entity: %d\n", ret);
825 ret = v4l2_device_register_subdev(v4l2_dev, sd);
827 dev_err(dev, "Failed to register subdev: %d\n", ret);
834 media_entity_cleanup(&sd->entity);
836 v4l2_ctrl_handler_free(&csid->ctrls);
842 * msm_csid_unregister_entity - Unregister CSID module subdev node
845 void msm_csid_unregister_entity(struct csid_device *csid)
847 v4l2_device_unregister_subdev(&csid->subdev);
848 media_entity_cleanup(&csid->subdev.entity);
849 v4l2_ctrl_handler_free(&csid->ctrls);