4 * Qualcomm MSM Camera Subsystem - CSID Module
6 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
7 * Copyright (C) 2015-2016 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>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/consumer.h>
24 #include <media/media-entity.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-subdev.h>
31 #define MSM_CSID_NAME "msm_csid"
33 #define CAMSS_CSID_HW_VERSION 0x0
34 #define CAMSS_CSID_CORE_CTRL_0 0x004
35 #define CAMSS_CSID_CORE_CTRL_1 0x008
36 #define CAMSS_CSID_RST_CMD 0x00c
37 #define CAMSS_CSID_CID_LUT_VC_n(n) (0x010 + 0x4 * (n))
38 #define CAMSS_CSID_CID_n_CFG(n) (0x020 + 0x4 * (n))
39 #define CAMSS_CSID_IRQ_CLEAR_CMD 0x060
40 #define CAMSS_CSID_IRQ_MASK 0x064
41 #define CAMSS_CSID_IRQ_STATUS 0x068
42 #define CAMSS_CSID_TG_CTRL 0x0a0
43 #define CAMSS_CSID_TG_VC_CFG 0x0a4
44 #define CAMSS_CSID_TG_VC_CFG_H_BLANKING 0x3ff
45 #define CAMSS_CSID_TG_VC_CFG_V_BLANKING 0x7f
46 #define CAMSS_CSID_TG_DT_n_CGG_0(n) (0x0ac + 0xc * (n))
47 #define CAMSS_CSID_TG_DT_n_CGG_1(n) (0x0b0 + 0xc * (n))
48 #define CAMSS_CSID_TG_DT_n_CGG_2(n) (0x0b4 + 0xc * (n))
51 * csid_isr - CSID module interrupt handler
52 * @irq: Interrupt line
55 * Return IRQ_HANDLED on success
57 static irqreturn_t csid_isr(int irq, void *dev)
59 struct csid_device *csid = dev;
62 value = readl(csid->base + CAMSS_CSID_IRQ_STATUS);
63 writel(value, csid->base + CAMSS_CSID_IRQ_CLEAR_CMD);
65 if ((value >> 11) & 0x1)
66 complete(&csid->reset_complete);
72 * csid_enable_clocks - Enable clocks for CSID module and
73 * set clock rates where needed
76 * Return 0 on success or a negative error code otherwise
78 static int csid_enable_clocks(int nclocks, struct clk **clock, s32 *clock_rate)
84 for (i = 0; i < nclocks; i++) {
86 clk_rate = clk_round_rate(clock[i], clock_rate[i]);
88 pr_err("clock round rate failed\n");
92 ret = clk_set_rate(clock[i], clk_rate);
94 pr_err("clock set rate failed\n");
98 ret = clk_prepare_enable(clock[i]);
100 pr_err("clock enable failed\n");
108 for (i--; i >= 0; i--)
109 clk_disable_unprepare(clock[i]);
115 * csid_disable_clocks - Disable clocks for CSID module
118 static void csid_disable_clocks(int nclocks, struct clk **clock)
122 for (i = nclocks - 1; i >= 0; i--)
123 clk_disable_unprepare(clock[i]);
127 * csid_set_power - Power on/off CSID module
128 * @sd: CSID V4L2 subdevice
129 * @on: Requested power state
131 * Return 0 on success or a negative error code otherwise
133 static int csid_set_power(struct v4l2_subdev *sd, int on)
135 struct csid_device *csid = v4l2_get_subdevdata(sd);
138 dev_err(csid->camss->dev, "%s: Enter, csid%d on = %d\n",
139 __func__, csid->id, on);
144 ret = regulator_enable(csid->vdda);
148 ret = csid_enable_clocks(csid->nclocks, csid->clock,
153 enable_irq(csid->irq);
155 hw_version = readl(csid->base + CAMSS_CSID_HW_VERSION);
156 dev_err(csid->camss->dev, "CSID HW Version = 0x%08x\n", hw_version);
158 disable_irq(csid->irq);
160 csid_disable_clocks(csid->nclocks, csid->clock);
162 ret = regulator_disable(csid->vdda);
167 dev_err(csid->camss->dev, "%s: Exit, csid%d on = %d\n",
168 __func__, csid->id, on);
173 #define DATA_TYPE_YUV422_8BIT 0x1e
176 * csid_get_data_type - map media but format to data type
177 * @fmt media bus format code
179 * Return data type code
181 static u8 csid_get_data_type(u32 fmt)
184 case MEDIA_BUS_FMT_UYVY8_2X8:
185 return DATA_TYPE_YUV422_8BIT;
191 #define DECODE_FORMAT_UNCOMPRESSED_8_BIT 0x1
194 * csid_get_decode_format - map media but format to decode format
195 * @fmt media bus format code
197 * Return decode format code
199 static u8 csid_get_decode_format(u32 fmt)
202 case MEDIA_BUS_FMT_UYVY8_2X8:
203 return DECODE_FORMAT_UNCOMPRESSED_8_BIT;
210 * csid_set_stream - Enable/disable streaming on CSID module
211 * @sd: CSID V4L2 subdevice
212 * @enable: Requested streaming state
214 * Main configuration of CSID module is also done here.
216 * Return 0 on success or a negative error code otherwise
218 static int csid_set_stream(struct v4l2_subdev *sd, int enable)
220 struct csid_device *csid = v4l2_get_subdevdata(sd);
221 struct csid_testgen_config *tg = &csid->testgen;
223 dev_err(csid->camss->dev, "%s: Enter, csid%d enable = %d\n",
224 __func__, csid->id, enable);
227 u8 vc = 0; /* TODO: How to get this from sensor? */
233 ret = v4l2_ctrl_handler_setup(&csid->ctrls);
235 dev_err(csid->camss->dev,
236 "could not sync v4l2 controls\n");
241 !media_entity_remote_pad(&csid->pads[MSM_CSID_PAD_SINK])) {
246 writel(0x7FFF, csid->base + CAMSS_CSID_RST_CMD);
247 wait_for_completion(&csid->reset_complete);
249 dt = csid_get_data_type(csid->fmt[MSM_CSID_PAD_SRC].code);
252 /* Config Test Generator */
253 u32 num_bytes_per_line =
254 csid->fmt[MSM_CSID_PAD_SRC].width * 2;
255 u32 num_lines = csid->fmt[MSM_CSID_PAD_SRC].height;
257 /* 31:24 V blank, 23:13 H blank, 3:2 num of active DT */
259 val = ((CAMSS_CSID_TG_VC_CFG_V_BLANKING & 0xff) << 24) |
260 ((CAMSS_CSID_TG_VC_CFG_H_BLANKING & 0x7ff) << 13);
261 writel(val, csid->base + CAMSS_CSID_TG_VC_CFG);
263 /* 28:16 bytes per lines, 12:0 num of lines */
264 val = ((num_bytes_per_line & 0x1FFF) << 16) |
265 (num_lines & 0x1FFF);
266 writel(val, csid->base + CAMSS_CSID_TG_DT_n_CGG_0(0));
270 writel(val, csid->base + CAMSS_CSID_TG_DT_n_CGG_1(0));
272 /* 2:0 output random */
273 val = tg->payload_mode;
274 writel(val, csid->base + CAMSS_CSID_TG_DT_n_CGG_2(0));
276 struct csid_phy_config *phy = &csid->phy;
278 val = phy->lane_cnt - 1;
279 val |= phy->lane_assign << 4;
281 writel(val, csid->base + CAMSS_CSID_CORE_CTRL_0);
283 val = phy->csiphy_id << 17;
286 writel(val, csid->base + CAMSS_CSID_CORE_CTRL_1);
291 dt_shift = (cid % 4) * 8;
292 df = csid_get_decode_format(csid->fmt[MSM_CSID_PAD_SINK].code);
294 val = readl(csid->base + CAMSS_CSID_CID_LUT_VC_n(vc));
295 val &= ~(0xff << dt_shift);
296 val |= dt << dt_shift;
297 writel(val, csid->base + CAMSS_CSID_CID_LUT_VC_n(vc));
299 val = (df << 4) | 0x3;
300 writel(val, csid->base + CAMSS_CSID_CID_n_CFG(cid));
304 writel(val, csid->base + CAMSS_CSID_TG_CTRL);
308 u32 val = 0x00a06436;
309 writel(val, csid->base + CAMSS_CSID_TG_CTRL);
317 * __csid_get_format - Get pointer to format structure
319 * @cfg: V4L2 subdev pad configuration
320 * @pad: pad from which format is requested
321 * @which: TRY or ACTIVE format
323 * Return pointer to TRY or ACTIVE format structure
325 static struct v4l2_mbus_framefmt *
326 __csid_get_format(struct csid_device *csid,
327 struct v4l2_subdev_pad_config *cfg,
329 enum v4l2_subdev_format_whence which)
331 if (which == V4L2_SUBDEV_FORMAT_TRY)
332 return v4l2_subdev_get_try_format(&csid->subdev, cfg, pad);
334 return &csid->fmt[pad];
338 * csid_get_format - Handle get format by pads subdev method
339 * @sd: CSID V4L2 subdevice
340 * @cfg: V4L2 subdev pad configuration
341 * @fmt: pointer to v4l2 subdev format structure
343 * Return -EINVAL or zero on success
345 static int csid_get_format(struct v4l2_subdev *sd,
346 struct v4l2_subdev_pad_config *cfg,
347 struct v4l2_subdev_format *fmt)
349 struct csid_device *csid = v4l2_get_subdevdata(sd);
350 struct v4l2_mbus_framefmt *format;
352 format = __csid_get_format(csid, cfg, fmt->pad, fmt->which);
356 fmt->format = *format;
362 * csid_set_format - Handle set format by pads subdev method
363 * @sd: CSID V4L2 subdevice
364 * @cfg: V4L2 subdev pad configuration
365 * @fmt: pointer to v4l2 subdev format structure
367 * Return -EINVAL or zero on success
369 static int csid_set_format(struct v4l2_subdev *sd,
370 struct v4l2_subdev_pad_config *cfg,
371 struct v4l2_subdev_format *fmt)
373 struct csid_device *csid = v4l2_get_subdevdata(sd);
374 struct v4l2_mbus_framefmt *format;
376 if (fmt->pad == MSM_CSID_PAD_SINK) {
377 /* Set format on sink pad */
378 format = __csid_get_format(csid, cfg, fmt->pad,
383 if (fmt->format.field == V4L2_FIELD_ANY)
384 fmt->format.field = V4L2_FIELD_NONE;
386 *format = fmt->format;
388 /* Reset format on source pad */
389 format = __csid_get_format(csid, cfg, MSM_CSID_PAD_SRC,
394 *format = fmt->format;
396 if (media_entity_remote_pad(&csid->pads[MSM_CSID_PAD_SINK])) {
397 /* CSID is linked to CSIPHY */
398 /* Reset format on source pad to sink pad format */
400 format = __csid_get_format(csid, cfg, MSM_CSID_PAD_SINK,
405 fmt->format = *format;
407 format = __csid_get_format(csid, cfg, fmt->pad,
412 *format = fmt->format;
414 /* CSID is not linked to CSIPHY */
415 /* Set format on source pad to allow */
416 /* test generator usage */
418 format = __csid_get_format(csid, cfg, fmt->pad,
423 /* Accept only YUV422 format */
424 fmt->format.code = MEDIA_BUS_FMT_UYVY8_2X8;
425 fmt->format.field = V4L2_FIELD_NONE;
427 *format = fmt->format;
434 static const char * const csid_test_pattern_menu[] = {
443 static int csid_set_test_pattern(struct csid_device *csid, s32 value)
445 struct csid_testgen_config *tg = &csid->testgen;
447 /* If CSID is linked to CSIPHY, do not allow to enable test generator */
448 if (value && media_entity_remote_pad(&csid->pads[MSM_CSID_PAD_SINK]))
451 tg->enabled = !!value;
455 tg->payload_mode = CSID_PAYLOAD_MODE_INCREMENTING;
458 tg->payload_mode = CSID_PAYLOAD_MODE_ALTERNATING_55_AA;
461 tg->payload_mode = CSID_PAYLOAD_MODE_ALL_ZEROES;
464 tg->payload_mode = CSID_PAYLOAD_MODE_ALL_ONES;
467 tg->payload_mode = CSID_PAYLOAD_MODE_RANDOM;
474 static int csid_s_ctrl(struct v4l2_ctrl *ctrl)
476 struct csid_device *csid = container_of(ctrl->handler,
477 struct csid_device, ctrls);
481 case V4L2_CID_TEST_PATTERN:
482 ret = csid_set_test_pattern(csid, ctrl->val);
489 static struct v4l2_ctrl_ops csid_ctrl_ops = {
490 .s_ctrl = csid_s_ctrl,
494 * msm_csid_subdev_init - Initialize CSID device structure and resources
496 * @camss: Camera sub-system structure
497 * @res: CSID module resources table
498 * @id: CSID module id
500 * Return 0 on success or a negative error code otherwise
502 int msm_csid_subdev_init(struct csid_device *csid, struct camss *camss,
503 struct resources *res, u8 id)
505 struct device *dev = camss->dev;
506 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
517 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res->reg[0]);
518 csid->base = devm_ioremap_resource(dev, r);
519 if (IS_ERR(csid->base)) {
520 dev_err(dev, "could not map memory\n");
521 return PTR_ERR(csid->base);
526 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res->interrupt[0]);
527 csid->irq = r->start;
528 if (IS_ERR_VALUE(csid->irq))
531 ret = devm_request_irq(dev, csid->irq, csid_isr,
532 IRQF_TRIGGER_RISING, dev_name(dev), csid);
534 dev_err(dev, "request_irq failed\n");
538 disable_irq(csid->irq);
544 while (res->clock[i++])
547 csid->clock = devm_kzalloc(dev, csid->nclocks * sizeof(*csid->clock),
550 dev_err(dev, "could not allocate memory\n");
554 csid->clock_rate = devm_kzalloc(dev, csid->nclocks *
555 sizeof(*csid->clock_rate), GFP_KERNEL);
556 if (!csid->clock_rate) {
557 dev_err(dev, "could not allocate memory\n");
561 for (i = 0; i < csid->nclocks; i++) {
562 csid->clock[i] = devm_clk_get(dev, res->clock[i]);
563 if (IS_ERR(csid->clock[i]))
564 return PTR_ERR(csid->clock[i]);
565 csid->clock_rate[i] = res->clock_rate[i];
570 csid->vdda = devm_regulator_get(dev, res->regulator[0]);
571 if (IS_ERR(csid->vdda)) {
572 dev_err(dev, "could not get regulator\n");
573 return PTR_ERR(csid->vdda);
576 init_completion(&csid->reset_complete);
582 * csid_get_lane_assign - Calculate CSI2 lane assign configuration parameter
583 * @lane_cfg - CSI2 lane configuration
587 static u32 csid_get_lane_assign(struct camss_csiphy_lanes_cfg *lanecfg)
592 for (i = 0; i < lanecfg->num_data; i++)
593 lane_assign |= lanecfg->data[i].pos << (i * 4);
599 * csid_link_setup - Setup CSID connections
600 * @entity: Pointer to media entity structure
601 * @local: Pointer to local pad
602 * @remote: Pointer to remote pad
605 * Rreturn 0 on success
607 static int csid_link_setup(struct media_entity *entity,
608 const struct media_pad *local,
609 const struct media_pad *remote, u32 flags)
611 if ((local->flags & MEDIA_PAD_FL_SINK) &&
612 (flags & MEDIA_LNK_FL_ENABLED)) {
613 struct v4l2_subdev *sd;
614 struct csid_device *csid;
615 struct csiphy_device *csiphy;
616 struct camss_csiphy_lanes_cfg *lanecfg;
618 sd = container_of(entity, struct v4l2_subdev, entity);
619 csid = v4l2_get_subdevdata(sd);
621 /* If test generator is enabled
622 * do not allow a link from CSIPHY to CSID */
623 if (csid->testgen_mode->cur.val != 0)
626 sd = container_of(remote->entity, struct v4l2_subdev, entity);
627 csiphy = v4l2_get_subdevdata(sd);
629 /* If a sensor is not linked to CSIPHY
630 * do no allow a link from CSIPHY to CSID */
631 if (!csiphy->cfg.csi2)
634 csid->phy.csiphy_id = csiphy->id;
636 lanecfg = &csiphy->cfg.csi2->lanecfg;
637 csid->phy.lane_cnt = lanecfg->num_data;
638 csid->phy.lane_assign = csid_get_lane_assign(lanecfg);
644 static const struct v4l2_subdev_core_ops csid_core_ops = {
645 .s_power = csid_set_power,
648 static const struct v4l2_subdev_video_ops csid_video_ops = {
649 .s_stream = csid_set_stream,
652 static const struct v4l2_subdev_pad_ops csid_pad_ops = {
653 .get_fmt = csid_get_format,
654 .set_fmt = csid_set_format,
657 static const struct v4l2_subdev_ops csid_v4l2_ops = {
658 .core = &csid_core_ops,
659 .video = &csid_video_ops,
660 .pad = &csid_pad_ops,
663 static const struct v4l2_subdev_internal_ops csid_v4l2_internal_ops;
665 static const struct media_entity_operations csid_media_ops = {
666 .link_setup = csid_link_setup,
667 .link_validate = v4l2_subdev_link_validate,
671 * msm_csid_register_entities - Register subdev node for CSID module
673 * @v4l2_dev: V4L2 device
675 * Return 0 on success or a negative error code otherwise
677 int msm_csid_register_entities(struct csid_device *csid,
678 struct v4l2_device *v4l2_dev)
680 struct v4l2_subdev *sd = &csid->subdev;
681 struct media_pad *pads = csid->pads;
684 v4l2_subdev_init(sd, &csid_v4l2_ops);
685 sd->internal_ops = &csid_v4l2_internal_ops;
686 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
687 snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d",
688 MSM_CSID_NAME, csid->id);
689 v4l2_set_subdevdata(sd, csid);
691 v4l2_ctrl_handler_init(&csid->ctrls, 1);
692 csid->testgen_mode = v4l2_ctrl_new_std_menu_items(&csid->ctrls,
693 &csid_ctrl_ops, V4L2_CID_TEST_PATTERN,
694 ARRAY_SIZE(csid_test_pattern_menu) - 1, 0, 0,
695 csid_test_pattern_menu);
697 if (csid->ctrls.error) {
698 dev_err(csid->camss->dev, "failed to init ctrl: %d\n",
700 ret = csid->ctrls.error;
704 csid->subdev.ctrl_handler = &csid->ctrls;
706 pads[MSM_CSID_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
707 pads[MSM_CSID_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE;
709 sd->entity.ops = &csid_media_ops;
710 ret = media_entity_init(&sd->entity, MSM_CSID_PADS_NUM, pads, 0);
712 dev_err(csid->camss->dev, "failed to init media entity");
716 ret = v4l2_device_register_subdev(v4l2_dev, sd);
718 dev_err(csid->camss->dev, "failed to register subdev");
725 media_entity_cleanup(&sd->entity);
727 v4l2_ctrl_handler_free(&csid->ctrls);
733 * msm_csid_unregister_entities - Unregister CSID module subdev node
736 void msm_csid_unregister_entities(struct csid_device *csid)
738 v4l2_device_unregister_subdev(&csid->subdev);
739 v4l2_ctrl_handler_free(&csid->ctrls);