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 u8 bpp = csiphy_get_bpp(csiphy->formats, csiphy->nformats,
110 csiphy->fmt[MSM_CSIPHY_PAD_SINK].code);
111 u8 num_lanes = csiphy->cfg.csi2->lane_cfg.num_data;
113 link_freq = camss_get_link_freq(&csiphy->subdev.entity, bpp, num_lanes);
117 for (i = 0; i < csiphy->nclocks; i++) {
118 struct camss_clock *clock = &csiphy->clock[i];
120 if (csiphy->rate_set[i]) {
121 u64 min_rate = link_freq / 4;
124 camss_add_clock_margin(&min_rate);
126 for (j = 0; j < clock->nfreqs; j++)
127 if (min_rate < clock->freq[j])
130 if (j == clock->nfreqs) {
132 "Pixel clock is too high for CSIPHY\n");
136 /* if sensor pixel clock is not available */
137 /* set highest possible CSIPHY clock rate */
139 j = clock->nfreqs - 1;
141 round_rate = clk_round_rate(clock->clk, clock->freq[j]);
142 if (round_rate < 0) {
143 dev_err(dev, "clk round rate failed: %ld\n",
148 csiphy->timer_clk_rate = round_rate;
150 ret = clk_set_rate(clock->clk, csiphy->timer_clk_rate);
152 dev_err(dev, "clk set rate failed: %d\n", ret);
162 * csiphy_set_power - Power on/off CSIPHY module
163 * @sd: CSIPHY V4L2 subdevice
164 * @on: Requested power state
166 * Return 0 on success or a negative error code otherwise
168 static int csiphy_set_power(struct v4l2_subdev *sd, int on)
170 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
171 struct device *dev = csiphy->camss->dev;
176 ret = pm_runtime_get_sync(dev);
178 pm_runtime_put_sync(dev);
182 ret = csiphy_set_clock_rates(csiphy);
184 pm_runtime_put_sync(dev);
188 ret = camss_enable_clocks(csiphy->nclocks, csiphy->clock, dev);
190 pm_runtime_put_sync(dev);
194 enable_irq(csiphy->irq);
196 csiphy->ops->reset(csiphy);
198 csiphy->ops->hw_version_read(csiphy, dev);
200 disable_irq(csiphy->irq);
202 camss_disable_clocks(csiphy->nclocks, csiphy->clock);
204 pm_runtime_put_sync(dev);
211 * csiphy_get_lane_mask - Calculate CSI2 lane mask configuration parameter
212 * @lane_cfg - CSI2 lane configuration
216 static u8 csiphy_get_lane_mask(struct csiphy_lanes_cfg *lane_cfg)
221 lane_mask = 1 << lane_cfg->clk.pos;
223 for (i = 0; i < lane_cfg->num_data; i++)
224 lane_mask |= 1 << lane_cfg->data[i].pos;
230 * csiphy_stream_on - Enable streaming on CSIPHY module
231 * @csiphy: CSIPHY device
233 * Helper function to enable streaming on CSIPHY module.
234 * Main configuration of CSIPHY module is also done here.
236 * Return 0 on success or a negative error code otherwise
238 static int csiphy_stream_on(struct csiphy_device *csiphy)
240 struct csiphy_config *cfg = &csiphy->cfg;
242 u8 lane_mask = csiphy_get_lane_mask(&cfg->csi2->lane_cfg);
243 u8 bpp = csiphy_get_bpp(csiphy->formats, csiphy->nformats,
244 csiphy->fmt[MSM_CSIPHY_PAD_SINK].code);
245 u8 num_lanes = csiphy->cfg.csi2->lane_cfg.num_data;
248 link_freq = camss_get_link_freq(&csiphy->subdev.entity, bpp, num_lanes);
251 dev_err(csiphy->camss->dev,
252 "Cannot get CSI2 transmitter's link frequency\n");
256 val = readl_relaxed(csiphy->base_clk_mux);
257 if (cfg->combo_mode && (lane_mask & 0x18) == 0x18) {
259 val |= cfg->csid_id << 4;
264 writel_relaxed(val, csiphy->base_clk_mux);
267 csiphy->ops->lanes_enable(csiphy, cfg, link_freq, lane_mask);
273 * csiphy_stream_off - Disable streaming on CSIPHY module
274 * @csiphy: CSIPHY device
276 * Helper function to disable streaming on CSIPHY module
278 static void csiphy_stream_off(struct csiphy_device *csiphy)
280 csiphy->ops->lanes_disable(csiphy, &csiphy->cfg);
285 * csiphy_set_stream - Enable/disable streaming on CSIPHY module
286 * @sd: CSIPHY V4L2 subdevice
287 * @enable: Requested streaming state
289 * Return 0 on success or a negative error code otherwise
291 static int csiphy_set_stream(struct v4l2_subdev *sd, int enable)
293 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
297 ret = csiphy_stream_on(csiphy);
299 csiphy_stream_off(csiphy);
305 * __csiphy_get_format - Get pointer to format structure
306 * @csiphy: CSIPHY device
307 * @cfg: V4L2 subdev pad configuration
308 * @pad: pad from which format is requested
309 * @which: TRY or ACTIVE format
311 * Return pointer to TRY or ACTIVE format structure
313 static struct v4l2_mbus_framefmt *
314 __csiphy_get_format(struct csiphy_device *csiphy,
315 struct v4l2_subdev_pad_config *cfg,
317 enum v4l2_subdev_format_whence which)
319 if (which == V4L2_SUBDEV_FORMAT_TRY)
320 return v4l2_subdev_get_try_format(&csiphy->subdev, cfg, pad);
322 return &csiphy->fmt[pad];
326 * csiphy_try_format - Handle try format by pad subdev method
327 * @csiphy: CSIPHY device
328 * @cfg: V4L2 subdev pad configuration
329 * @pad: pad on which format is requested
330 * @fmt: pointer to v4l2 format structure
331 * @which: wanted subdev format
333 static void csiphy_try_format(struct csiphy_device *csiphy,
334 struct v4l2_subdev_pad_config *cfg,
336 struct v4l2_mbus_framefmt *fmt,
337 enum v4l2_subdev_format_whence which)
342 case MSM_CSIPHY_PAD_SINK:
343 /* Set format on sink pad */
345 for (i = 0; i < csiphy->nformats; i++)
346 if (fmt->code == csiphy->formats[i].code)
349 /* If not found, use UYVY as default */
350 if (i >= csiphy->nformats)
351 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
353 fmt->width = clamp_t(u32, fmt->width, 1, 8191);
354 fmt->height = clamp_t(u32, fmt->height, 1, 8191);
356 fmt->field = V4L2_FIELD_NONE;
357 fmt->colorspace = V4L2_COLORSPACE_SRGB;
361 case MSM_CSIPHY_PAD_SRC:
362 /* Set and return a format same as sink pad */
364 *fmt = *__csiphy_get_format(csiphy, cfg, MSM_CSID_PAD_SINK,
372 * csiphy_enum_mbus_code - Handle pixel format enumeration
373 * @sd: CSIPHY V4L2 subdevice
374 * @cfg: V4L2 subdev pad configuration
375 * @code: pointer to v4l2_subdev_mbus_code_enum structure
376 * return -EINVAL or zero on success
378 static int csiphy_enum_mbus_code(struct v4l2_subdev *sd,
379 struct v4l2_subdev_pad_config *cfg,
380 struct v4l2_subdev_mbus_code_enum *code)
382 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
383 struct v4l2_mbus_framefmt *format;
385 if (code->pad == MSM_CSIPHY_PAD_SINK) {
386 if (code->index >= csiphy->nformats)
389 code->code = csiphy->formats[code->index].code;
394 format = __csiphy_get_format(csiphy, cfg, MSM_CSIPHY_PAD_SINK,
397 code->code = format->code;
404 * csiphy_enum_frame_size - Handle frame size enumeration
405 * @sd: CSIPHY V4L2 subdevice
406 * @cfg: V4L2 subdev pad configuration
407 * @fse: pointer to v4l2_subdev_frame_size_enum structure
408 * return -EINVAL or zero on success
410 static int csiphy_enum_frame_size(struct v4l2_subdev *sd,
411 struct v4l2_subdev_pad_config *cfg,
412 struct v4l2_subdev_frame_size_enum *fse)
414 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
415 struct v4l2_mbus_framefmt format;
420 format.code = fse->code;
423 csiphy_try_format(csiphy, cfg, fse->pad, &format, fse->which);
424 fse->min_width = format.width;
425 fse->min_height = format.height;
427 if (format.code != fse->code)
430 format.code = fse->code;
433 csiphy_try_format(csiphy, cfg, fse->pad, &format, fse->which);
434 fse->max_width = format.width;
435 fse->max_height = format.height;
441 * csiphy_get_format - Handle get format by pads subdev method
442 * @sd: CSIPHY V4L2 subdevice
443 * @cfg: V4L2 subdev pad configuration
444 * @fmt: pointer to v4l2 subdev format structure
446 * Return -EINVAL or zero on success
448 static int csiphy_get_format(struct v4l2_subdev *sd,
449 struct v4l2_subdev_pad_config *cfg,
450 struct v4l2_subdev_format *fmt)
452 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
453 struct v4l2_mbus_framefmt *format;
455 format = __csiphy_get_format(csiphy, cfg, fmt->pad, fmt->which);
459 fmt->format = *format;
465 * csiphy_set_format - Handle set format by pads subdev method
466 * @sd: CSIPHY V4L2 subdevice
467 * @cfg: V4L2 subdev pad configuration
468 * @fmt: pointer to v4l2 subdev format structure
470 * Return -EINVAL or zero on success
472 static int csiphy_set_format(struct v4l2_subdev *sd,
473 struct v4l2_subdev_pad_config *cfg,
474 struct v4l2_subdev_format *fmt)
476 struct csiphy_device *csiphy = v4l2_get_subdevdata(sd);
477 struct v4l2_mbus_framefmt *format;
479 format = __csiphy_get_format(csiphy, cfg, fmt->pad, fmt->which);
483 csiphy_try_format(csiphy, cfg, fmt->pad, &fmt->format, fmt->which);
484 *format = fmt->format;
486 /* Propagate the format from sink to source */
487 if (fmt->pad == MSM_CSIPHY_PAD_SINK) {
488 format = __csiphy_get_format(csiphy, cfg, MSM_CSIPHY_PAD_SRC,
491 *format = fmt->format;
492 csiphy_try_format(csiphy, cfg, MSM_CSIPHY_PAD_SRC, format,
500 * csiphy_init_formats - Initialize formats on all pads
501 * @sd: CSIPHY V4L2 subdevice
502 * @fh: V4L2 subdev file handle
504 * Initialize all pad formats with default values.
506 * Return 0 on success or a negative error code otherwise
508 static int csiphy_init_formats(struct v4l2_subdev *sd,
509 struct v4l2_subdev_fh *fh)
511 struct v4l2_subdev_format format = {
512 .pad = MSM_CSIPHY_PAD_SINK,
513 .which = fh ? V4L2_SUBDEV_FORMAT_TRY :
514 V4L2_SUBDEV_FORMAT_ACTIVE,
516 .code = MEDIA_BUS_FMT_UYVY8_2X8,
522 return csiphy_set_format(sd, fh ? fh->pad : NULL, &format);
526 * msm_csiphy_subdev_init - Initialize CSIPHY device structure and resources
527 * @csiphy: CSIPHY device
528 * @res: CSIPHY module resources table
529 * @id: CSIPHY module id
531 * Return 0 on success or a negative error code otherwise
533 int msm_csiphy_subdev_init(struct camss *camss,
534 struct csiphy_device *csiphy,
535 const struct resources *res, u8 id)
537 struct device *dev = camss->dev;
538 struct platform_device *pdev = to_platform_device(dev);
543 csiphy->camss = camss;
545 csiphy->cfg.combo_mode = 0;
547 if (camss->version == CAMSS_8x16) {
548 csiphy->ops = &csiphy_ops_2ph_1_0;
549 csiphy->formats = csiphy_formats_8x16;
550 csiphy->nformats = ARRAY_SIZE(csiphy_formats_8x16);
551 } else if (camss->version == CAMSS_8x96 ||
552 camss->version == CAMSS_660) {
553 csiphy->ops = &csiphy_ops_3ph_1_0;
554 csiphy->formats = csiphy_formats_8x96;
555 csiphy->nformats = ARRAY_SIZE(csiphy_formats_8x96);
562 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res->reg[0]);
563 csiphy->base = devm_ioremap_resource(dev, r);
564 if (IS_ERR(csiphy->base)) {
565 dev_err(dev, "could not map memory\n");
566 return PTR_ERR(csiphy->base);
569 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res->reg[1]);
570 csiphy->base_clk_mux = devm_ioremap_resource(dev, r);
571 if (IS_ERR(csiphy->base_clk_mux)) {
572 dev_err(dev, "could not map memory\n");
573 return PTR_ERR(csiphy->base_clk_mux);
578 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
581 dev_err(dev, "missing IRQ\n");
585 csiphy->irq = r->start;
586 snprintf(csiphy->irq_name, sizeof(csiphy->irq_name), "%s_%s%d",
587 dev_name(dev), MSM_CSIPHY_NAME, csiphy->id);
589 ret = devm_request_irq(dev, csiphy->irq, csiphy->ops->isr,
590 IRQF_TRIGGER_RISING, csiphy->irq_name, csiphy);
592 dev_err(dev, "request_irq failed: %d\n", ret);
596 disable_irq(csiphy->irq);
601 while (res->clock[csiphy->nclocks])
604 csiphy->clock = devm_kcalloc(dev,
605 csiphy->nclocks, sizeof(*csiphy->clock),
610 csiphy->rate_set = devm_kcalloc(dev,
612 sizeof(*csiphy->rate_set),
614 if (!csiphy->rate_set)
617 for (i = 0; i < csiphy->nclocks; i++) {
618 struct camss_clock *clock = &csiphy->clock[i];
620 clock->clk = devm_clk_get(dev, res->clock[i]);
621 if (IS_ERR(clock->clk))
622 return PTR_ERR(clock->clk);
624 clock->name = res->clock[i];
627 while (res->clock_rate[i][clock->nfreqs])
630 if (!clock->nfreqs) {
635 clock->freq = devm_kcalloc(dev,
637 sizeof(*clock->freq),
642 for (j = 0; j < clock->nfreqs; j++)
643 clock->freq[j] = res->clock_rate[i][j];
645 if (!strcmp(clock->name, "csiphy0_timer") ||
646 !strcmp(clock->name, "csiphy1_timer") ||
647 !strcmp(clock->name, "csiphy2_timer"))
648 csiphy->rate_set[i] = true;
650 if (camss->version == CAMSS_660 &&
651 (!strcmp(clock->name, "csi0_phy") ||
652 !strcmp(clock->name, "csi1_phy") ||
653 !strcmp(clock->name, "csi2_phy")))
654 csiphy->rate_set[i] = true;
661 * csiphy_link_setup - Setup CSIPHY connections
662 * @entity: Pointer to media entity structure
663 * @local: Pointer to local pad
664 * @remote: Pointer to remote pad
667 * Rreturn 0 on success
669 static int csiphy_link_setup(struct media_entity *entity,
670 const struct media_pad *local,
671 const struct media_pad *remote, u32 flags)
673 if ((local->flags & MEDIA_PAD_FL_SOURCE) &&
674 (flags & MEDIA_LNK_FL_ENABLED)) {
675 struct v4l2_subdev *sd;
676 struct csiphy_device *csiphy;
677 struct csid_device *csid;
679 if (media_entity_remote_pad(local))
682 sd = media_entity_to_v4l2_subdev(entity);
683 csiphy = v4l2_get_subdevdata(sd);
685 sd = media_entity_to_v4l2_subdev(remote->entity);
686 csid = v4l2_get_subdevdata(sd);
688 csiphy->cfg.csid_id = csid->id;
694 static const struct v4l2_subdev_core_ops csiphy_core_ops = {
695 .s_power = csiphy_set_power,
698 static const struct v4l2_subdev_video_ops csiphy_video_ops = {
699 .s_stream = csiphy_set_stream,
702 static const struct v4l2_subdev_pad_ops csiphy_pad_ops = {
703 .enum_mbus_code = csiphy_enum_mbus_code,
704 .enum_frame_size = csiphy_enum_frame_size,
705 .get_fmt = csiphy_get_format,
706 .set_fmt = csiphy_set_format,
709 static const struct v4l2_subdev_ops csiphy_v4l2_ops = {
710 .core = &csiphy_core_ops,
711 .video = &csiphy_video_ops,
712 .pad = &csiphy_pad_ops,
715 static const struct v4l2_subdev_internal_ops csiphy_v4l2_internal_ops = {
716 .open = csiphy_init_formats,
719 static const struct media_entity_operations csiphy_media_ops = {
720 .link_setup = csiphy_link_setup,
721 .link_validate = v4l2_subdev_link_validate,
725 * msm_csiphy_register_entity - Register subdev node for CSIPHY module
726 * @csiphy: CSIPHY device
727 * @v4l2_dev: V4L2 device
729 * Return 0 on success or a negative error code otherwise
731 int msm_csiphy_register_entity(struct csiphy_device *csiphy,
732 struct v4l2_device *v4l2_dev)
734 struct v4l2_subdev *sd = &csiphy->subdev;
735 struct media_pad *pads = csiphy->pads;
736 struct device *dev = csiphy->camss->dev;
739 v4l2_subdev_init(sd, &csiphy_v4l2_ops);
740 sd->internal_ops = &csiphy_v4l2_internal_ops;
741 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
742 snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d",
743 MSM_CSIPHY_NAME, csiphy->id);
744 v4l2_set_subdevdata(sd, csiphy);
746 ret = csiphy_init_formats(sd, NULL);
748 dev_err(dev, "Failed to init format: %d\n", ret);
752 pads[MSM_CSIPHY_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
753 pads[MSM_CSIPHY_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE;
755 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
756 sd->entity.ops = &csiphy_media_ops;
757 ret = media_entity_pads_init(&sd->entity, MSM_CSIPHY_PADS_NUM, pads);
759 dev_err(dev, "Failed to init media entity: %d\n", ret);
763 ret = v4l2_device_register_subdev(v4l2_dev, sd);
765 dev_err(dev, "Failed to register subdev: %d\n", ret);
766 media_entity_cleanup(&sd->entity);
773 * msm_csiphy_unregister_entity - Unregister CSIPHY module subdev node
774 * @csiphy: CSIPHY device
776 void msm_csiphy_unregister_entity(struct csiphy_device *csiphy)
778 v4l2_device_unregister_subdev(&csiphy->subdev);
779 media_entity_cleanup(&csiphy->subdev.entity);