1 // SPDX-License-Identifier: GPL-2.0
5 * Qualcomm MSM Camera Subsystem - CSIPHY Module
7 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
8 * Copyright (C) 2016-2018 Linaro Ltd.
10 #include <linux/clk.h>
11 #include <linux/delay.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 <media/media-entity.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-subdev.h>
22 #include "camss-csiphy.h"
25 #define MSM_CSIPHY_NAME "msm_csiphy"
27 struct csiphy_format {
32 static const struct csiphy_format csiphy_formats_8x16[] = {
33 { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
34 { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
35 { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
36 { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
37 { MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
38 { MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
39 { MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
40 { MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
41 { MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
42 { MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
43 { MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
44 { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
45 { MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
46 { MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
47 { MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
48 { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
49 { MEDIA_BUS_FMT_Y10_1X10, 10 },
52 static const struct csiphy_format csiphy_formats_8x96[] = {
53 { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
54 { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
55 { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
56 { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
57 { MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
58 { MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
59 { MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
60 { MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
61 { MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
62 { MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
63 { MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
64 { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
65 { MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
66 { MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
67 { MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
68 { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
69 { MEDIA_BUS_FMT_SBGGR14_1X14, 14 },
70 { MEDIA_BUS_FMT_SGBRG14_1X14, 14 },
71 { MEDIA_BUS_FMT_SGRBG14_1X14, 14 },
72 { MEDIA_BUS_FMT_SRGGB14_1X14, 14 },
73 { MEDIA_BUS_FMT_Y10_1X10, 10 },
77 * csiphy_get_bpp - map media bus format to bits per pixel
78 * @formats: supported media bus formats array
79 * @nformats: size of @formats array
80 * @code: media bus format code
82 * Return number of bits per pixel
84 static u8 csiphy_get_bpp(const struct csiphy_format *formats,
85 unsigned int nformats, u32 code)
89 for (i = 0; i < nformats; i++)
90 if (code == formats[i].code)
91 return formats[i].bpp;
93 WARN(1, "Unknown format\n");
95 return formats[0].bpp;
99 * csiphy_set_clock_rates - Calculate and set clock rates on CSIPHY module
100 * @csiphy: CSIPHY device
102 static int csiphy_set_clock_rates(struct csiphy_device *csiphy)
104 struct device *dev = csiphy->camss->dev;
109 ret = camss_get_pixel_clock(&csiphy->subdev.entity, &pixel_clock);
113 for (i = 0; i < csiphy->nclocks; i++) {
114 struct camss_clock *clock = &csiphy->clock[i];
116 if (!strcmp(clock->name, "csiphy0_timer") ||
117 !strcmp(clock->name, "csiphy1_timer") ||
118 !strcmp(clock->name, "csiphy2_timer")) {
119 u8 bpp = csiphy_get_bpp(csiphy->formats,
121 csiphy->fmt[MSM_CSIPHY_PAD_SINK].code);
122 u8 num_lanes = csiphy->cfg.csi2->lane_cfg.num_data;
123 u64 min_rate = pixel_clock * bpp / (2 * num_lanes * 4);
126 camss_add_clock_margin(&min_rate);
128 for (j = 0; j < clock->nfreqs; j++)
129 if (min_rate < clock->freq[j])
132 if (j == clock->nfreqs) {
134 "Pixel clock is too high for CSIPHY\n");
138 /* if sensor pixel clock is not available */
139 /* set highest possible CSIPHY clock rate */
141 j = clock->nfreqs - 1;
143 round_rate = clk_round_rate(clock->clk, clock->freq[j]);
144 if (round_rate < 0) {
145 dev_err(dev, "clk round rate failed: %ld\n",
150 csiphy->timer_clk_rate = round_rate;
152 ret = clk_set_rate(clock->clk, csiphy->timer_clk_rate);
154 dev_err(dev, "clk set rate failed: %d\n", ret);
164 * csiphy_set_power - Power on/off CSIPHY module
165 * @sd: CSIPHY V4L2 subdevice
166 * @on: Requested power state
168 * Return 0 on success or a negative error code otherwise
170 static int csiphy_set_power(struct v4l2_subdev *sd, int on)
172 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
173 struct device *dev = csiphy->camss->dev;
178 ret = pm_runtime_get_sync(dev);
180 pm_runtime_put_sync(dev);
184 ret = csiphy_set_clock_rates(csiphy);
186 pm_runtime_put_sync(dev);
190 ret = camss_enable_clocks(csiphy->nclocks, csiphy->clock, dev);
192 pm_runtime_put_sync(dev);
196 enable_irq(csiphy->irq);
198 csiphy->ops->reset(csiphy);
200 csiphy->ops->hw_version_read(csiphy, dev);
202 disable_irq(csiphy->irq);
204 camss_disable_clocks(csiphy->nclocks, csiphy->clock);
206 pm_runtime_put_sync(dev);
213 * csiphy_get_lane_mask - Calculate CSI2 lane mask configuration parameter
214 * @lane_cfg - CSI2 lane configuration
218 static u8 csiphy_get_lane_mask(struct csiphy_lanes_cfg *lane_cfg)
223 lane_mask = 1 << lane_cfg->clk.pos;
225 for (i = 0; i < lane_cfg->num_data; i++)
226 lane_mask |= 1 << lane_cfg->data[i].pos;
232 * csiphy_stream_on - Enable streaming on CSIPHY module
233 * @csiphy: CSIPHY device
235 * Helper function to enable streaming on CSIPHY module.
236 * Main configuration of CSIPHY module is also done here.
238 * Return 0 on success or a negative error code otherwise
240 static int csiphy_stream_on(struct csiphy_device *csiphy)
242 struct csiphy_config *cfg = &csiphy->cfg;
244 u8 lane_mask = csiphy_get_lane_mask(&cfg->csi2->lane_cfg);
245 u8 bpp = csiphy_get_bpp(csiphy->formats, csiphy->nformats,
246 csiphy->fmt[MSM_CSIPHY_PAD_SINK].code);
250 ret = camss_get_pixel_clock(&csiphy->subdev.entity, &pixel_clock);
252 dev_err(csiphy->camss->dev,
253 "Cannot get CSI2 transmitter's pixel clock\n");
257 dev_err(csiphy->camss->dev,
258 "Got pixel clock == 0, cannot continue\n");
262 val = readl_relaxed(csiphy->base_clk_mux);
263 if (cfg->combo_mode && (lane_mask & 0x18) == 0x18) {
265 val |= cfg->csid_id << 4;
270 writel_relaxed(val, csiphy->base_clk_mux);
273 csiphy->ops->lanes_enable(csiphy, cfg, pixel_clock, bpp, lane_mask);
279 * csiphy_stream_off - Disable streaming on CSIPHY module
280 * @csiphy: CSIPHY device
282 * Helper function to disable streaming on CSIPHY module
284 static void csiphy_stream_off(struct csiphy_device *csiphy)
286 csiphy->ops->lanes_disable(csiphy, &csiphy->cfg);
291 * csiphy_set_stream - Enable/disable streaming on CSIPHY module
292 * @sd: CSIPHY V4L2 subdevice
293 * @enable: Requested streaming state
295 * Return 0 on success or a negative error code otherwise
297 static int csiphy_set_stream(struct v4l2_subdev *sd, int enable)
299 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
303 ret = csiphy_stream_on(csiphy);
305 csiphy_stream_off(csiphy);
311 * __csiphy_get_format - Get pointer to format structure
312 * @csiphy: CSIPHY device
313 * @cfg: V4L2 subdev pad configuration
314 * @pad: pad from which format is requested
315 * @which: TRY or ACTIVE format
317 * Return pointer to TRY or ACTIVE format structure
319 static struct v4l2_mbus_framefmt *
320 __csiphy_get_format(struct csiphy_device *csiphy,
321 struct v4l2_subdev_pad_config *cfg,
323 enum v4l2_subdev_format_whence which)
325 if (which == V4L2_SUBDEV_FORMAT_TRY)
326 return v4l2_subdev_get_try_format(&csiphy->subdev, cfg, pad);
328 return &csiphy->fmt[pad];
332 * csiphy_try_format - Handle try format by pad subdev method
333 * @csiphy: CSIPHY device
334 * @cfg: V4L2 subdev pad configuration
335 * @pad: pad on which format is requested
336 * @fmt: pointer to v4l2 format structure
337 * @which: wanted subdev format
339 static void csiphy_try_format(struct csiphy_device *csiphy,
340 struct v4l2_subdev_pad_config *cfg,
342 struct v4l2_mbus_framefmt *fmt,
343 enum v4l2_subdev_format_whence which)
348 case MSM_CSIPHY_PAD_SINK:
349 /* Set format on sink pad */
351 for (i = 0; i < csiphy->nformats; i++)
352 if (fmt->code == csiphy->formats[i].code)
355 /* If not found, use UYVY as default */
356 if (i >= csiphy->nformats)
357 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
359 fmt->width = clamp_t(u32, fmt->width, 1, 8191);
360 fmt->height = clamp_t(u32, fmt->height, 1, 8191);
362 fmt->field = V4L2_FIELD_NONE;
363 fmt->colorspace = V4L2_COLORSPACE_SRGB;
367 case MSM_CSIPHY_PAD_SRC:
368 /* Set and return a format same as sink pad */
370 *fmt = *__csiphy_get_format(csiphy, cfg, MSM_CSID_PAD_SINK,
378 * csiphy_enum_mbus_code - Handle pixel format enumeration
379 * @sd: CSIPHY V4L2 subdevice
380 * @cfg: V4L2 subdev pad configuration
381 * @code: pointer to v4l2_subdev_mbus_code_enum structure
382 * return -EINVAL or zero on success
384 static int csiphy_enum_mbus_code(struct v4l2_subdev *sd,
385 struct v4l2_subdev_pad_config *cfg,
386 struct v4l2_subdev_mbus_code_enum *code)
388 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
389 struct v4l2_mbus_framefmt *format;
391 if (code->pad == MSM_CSIPHY_PAD_SINK) {
392 if (code->index >= csiphy->nformats)
395 code->code = csiphy->formats[code->index].code;
400 format = __csiphy_get_format(csiphy, cfg, MSM_CSIPHY_PAD_SINK,
403 code->code = format->code;
410 * csiphy_enum_frame_size - Handle frame size enumeration
411 * @sd: CSIPHY V4L2 subdevice
412 * @cfg: V4L2 subdev pad configuration
413 * @fse: pointer to v4l2_subdev_frame_size_enum structure
414 * return -EINVAL or zero on success
416 static int csiphy_enum_frame_size(struct v4l2_subdev *sd,
417 struct v4l2_subdev_pad_config *cfg,
418 struct v4l2_subdev_frame_size_enum *fse)
420 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
421 struct v4l2_mbus_framefmt format;
426 format.code = fse->code;
429 csiphy_try_format(csiphy, cfg, fse->pad, &format, fse->which);
430 fse->min_width = format.width;
431 fse->min_height = format.height;
433 if (format.code != fse->code)
436 format.code = fse->code;
439 csiphy_try_format(csiphy, cfg, fse->pad, &format, fse->which);
440 fse->max_width = format.width;
441 fse->max_height = format.height;
447 * csiphy_get_format - Handle get format by pads subdev method
448 * @sd: CSIPHY V4L2 subdevice
449 * @cfg: V4L2 subdev pad configuration
450 * @fmt: pointer to v4l2 subdev format structure
452 * Return -EINVAL or zero on success
454 static int csiphy_get_format(struct v4l2_subdev *sd,
455 struct v4l2_subdev_pad_config *cfg,
456 struct v4l2_subdev_format *fmt)
458 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
459 struct v4l2_mbus_framefmt *format;
461 format = __csiphy_get_format(csiphy, cfg, fmt->pad, fmt->which);
465 fmt->format = *format;
471 * csiphy_set_format - Handle set format by pads subdev method
472 * @sd: CSIPHY V4L2 subdevice
473 * @cfg: V4L2 subdev pad configuration
474 * @fmt: pointer to v4l2 subdev format structure
476 * Return -EINVAL or zero on success
478 static int csiphy_set_format(struct v4l2_subdev *sd,
479 struct v4l2_subdev_pad_config *cfg,
480 struct v4l2_subdev_format *fmt)
482 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
483 struct v4l2_mbus_framefmt *format;
485 format = __csiphy_get_format(csiphy, cfg, fmt->pad, fmt->which);
489 csiphy_try_format(csiphy, cfg, fmt->pad, &fmt->format, fmt->which);
490 *format = fmt->format;
492 /* Propagate the format from sink to source */
493 if (fmt->pad == MSM_CSIPHY_PAD_SINK) {
494 format = __csiphy_get_format(csiphy, cfg, MSM_CSIPHY_PAD_SRC,
497 *format = fmt->format;
498 csiphy_try_format(csiphy, cfg, MSM_CSIPHY_PAD_SRC, format,
506 * csiphy_init_formats - Initialize formats on all pads
507 * @sd: CSIPHY V4L2 subdevice
508 * @fh: V4L2 subdev file handle
510 * Initialize all pad formats with default values.
512 * Return 0 on success or a negative error code otherwise
514 static int csiphy_init_formats(struct v4l2_subdev *sd,
515 struct v4l2_subdev_fh *fh)
517 struct v4l2_subdev_format format = {
518 .pad = MSM_CSIPHY_PAD_SINK,
519 .which = fh ? V4L2_SUBDEV_FORMAT_TRY :
520 V4L2_SUBDEV_FORMAT_ACTIVE,
522 .code = MEDIA_BUS_FMT_UYVY8_2X8,
528 return csiphy_set_format(sd, fh ? fh->pad : NULL, &format);
532 * msm_csiphy_subdev_init - Initialize CSIPHY device structure and resources
533 * @csiphy: CSIPHY device
534 * @res: CSIPHY module resources table
535 * @id: CSIPHY module id
537 * Return 0 on success or a negative error code otherwise
539 int msm_csiphy_subdev_init(struct camss *camss,
540 struct csiphy_device *csiphy,
541 const struct resources *res, u8 id)
543 struct device *dev = camss->dev;
544 struct platform_device *pdev = to_platform_device(dev);
549 csiphy->camss = camss;
551 csiphy->cfg.combo_mode = 0;
553 if (camss->version == CAMSS_8x16) {
554 csiphy->ops = &csiphy_ops_2ph_1_0;
555 csiphy->formats = csiphy_formats_8x16;
556 csiphy->nformats = ARRAY_SIZE(csiphy_formats_8x16);
557 } else if (camss->version == CAMSS_8x96) {
558 csiphy->ops = &csiphy_ops_3ph_1_0;
559 csiphy->formats = csiphy_formats_8x96;
560 csiphy->nformats = ARRAY_SIZE(csiphy_formats_8x96);
567 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res->reg[0]);
568 csiphy->base = devm_ioremap_resource(dev, r);
569 if (IS_ERR(csiphy->base)) {
570 dev_err(dev, "could not map memory\n");
571 return PTR_ERR(csiphy->base);
574 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res->reg[1]);
575 csiphy->base_clk_mux = devm_ioremap_resource(dev, r);
576 if (IS_ERR(csiphy->base_clk_mux)) {
577 dev_err(dev, "could not map memory\n");
578 return PTR_ERR(csiphy->base_clk_mux);
583 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
586 dev_err(dev, "missing IRQ\n");
590 csiphy->irq = r->start;
591 snprintf(csiphy->irq_name, sizeof(csiphy->irq_name), "%s_%s%d",
592 dev_name(dev), MSM_CSIPHY_NAME, csiphy->id);
594 ret = devm_request_irq(dev, csiphy->irq, csiphy->ops->isr,
595 IRQF_TRIGGER_RISING, csiphy->irq_name, csiphy);
597 dev_err(dev, "request_irq failed: %d\n", ret);
601 disable_irq(csiphy->irq);
606 while (res->clock[csiphy->nclocks])
609 csiphy->clock = devm_kcalloc(dev,
610 csiphy->nclocks, sizeof(*csiphy->clock),
615 for (i = 0; i < csiphy->nclocks; i++) {
616 struct camss_clock *clock = &csiphy->clock[i];
618 clock->clk = devm_clk_get(dev, res->clock[i]);
619 if (IS_ERR(clock->clk))
620 return PTR_ERR(clock->clk);
622 clock->name = res->clock[i];
625 while (res->clock_rate[i][clock->nfreqs])
628 if (!clock->nfreqs) {
633 clock->freq = devm_kcalloc(dev,
635 sizeof(*clock->freq),
640 for (j = 0; j < clock->nfreqs; j++)
641 clock->freq[j] = res->clock_rate[i][j];
648 * csiphy_link_setup - Setup CSIPHY connections
649 * @entity: Pointer to media entity structure
650 * @local: Pointer to local pad
651 * @remote: Pointer to remote pad
654 * Rreturn 0 on success
656 static int csiphy_link_setup(struct media_entity *entity,
657 const struct media_pad *local,
658 const struct media_pad *remote, u32 flags)
660 if ((local->flags & MEDIA_PAD_FL_SOURCE) &&
661 (flags & MEDIA_LNK_FL_ENABLED)) {
662 struct v4l2_subdev *sd;
663 struct csiphy_device *csiphy;
664 struct csid_device *csid;
666 if (media_entity_remote_pad(local))
669 sd = media_entity_to_v4l2_subdev(entity);
670 csiphy = v4l2_get_subdevdata(sd);
672 sd = media_entity_to_v4l2_subdev(remote->entity);
673 csid = v4l2_get_subdevdata(sd);
675 csiphy->cfg.csid_id = csid->id;
681 static const struct v4l2_subdev_core_ops csiphy_core_ops = {
682 .s_power = csiphy_set_power,
685 static const struct v4l2_subdev_video_ops csiphy_video_ops = {
686 .s_stream = csiphy_set_stream,
689 static const struct v4l2_subdev_pad_ops csiphy_pad_ops = {
690 .enum_mbus_code = csiphy_enum_mbus_code,
691 .enum_frame_size = csiphy_enum_frame_size,
692 .get_fmt = csiphy_get_format,
693 .set_fmt = csiphy_set_format,
696 static const struct v4l2_subdev_ops csiphy_v4l2_ops = {
697 .core = &csiphy_core_ops,
698 .video = &csiphy_video_ops,
699 .pad = &csiphy_pad_ops,
702 static const struct v4l2_subdev_internal_ops csiphy_v4l2_internal_ops = {
703 .open = csiphy_init_formats,
706 static const struct media_entity_operations csiphy_media_ops = {
707 .link_setup = csiphy_link_setup,
708 .link_validate = v4l2_subdev_link_validate,
712 * msm_csiphy_register_entity - Register subdev node for CSIPHY module
713 * @csiphy: CSIPHY device
714 * @v4l2_dev: V4L2 device
716 * Return 0 on success or a negative error code otherwise
718 int msm_csiphy_register_entity(struct csiphy_device *csiphy,
719 struct v4l2_device *v4l2_dev)
721 struct v4l2_subdev *sd = &csiphy->subdev;
722 struct media_pad *pads = csiphy->pads;
723 struct device *dev = csiphy->camss->dev;
726 v4l2_subdev_init(sd, &csiphy_v4l2_ops);
727 sd->internal_ops = &csiphy_v4l2_internal_ops;
728 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
729 snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d",
730 MSM_CSIPHY_NAME, csiphy->id);
731 v4l2_set_subdevdata(sd, csiphy);
733 ret = csiphy_init_formats(sd, NULL);
735 dev_err(dev, "Failed to init format: %d\n", ret);
739 pads[MSM_CSIPHY_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
740 pads[MSM_CSIPHY_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE;
742 sd->entity.function = MEDIA_ENT_F_IO_V4L;
743 sd->entity.ops = &csiphy_media_ops;
744 ret = media_entity_pads_init(&sd->entity, MSM_CSIPHY_PADS_NUM, pads);
746 dev_err(dev, "Failed to init media entity: %d\n", ret);
750 ret = v4l2_device_register_subdev(v4l2_dev, sd);
752 dev_err(dev, "Failed to register subdev: %d\n", ret);
753 media_entity_cleanup(&sd->entity);
760 * msm_csiphy_unregister_entity - Unregister CSIPHY module subdev node
761 * @csiphy: CSIPHY device
763 void msm_csiphy_unregister_entity(struct csiphy_device *csiphy)
765 v4l2_device_unregister_subdev(&csiphy->subdev);
766 media_entity_cleanup(&csiphy->subdev.entity);