blob: 579e142b4e94d828e85fb436910fed6ea10fb541 [file] [log] [blame]
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03001/*
2 * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface
3 *
Laurent Pinchart8a1edc52014-02-06 14:42:31 -03004 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03005 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/device.h>
15#include <linux/gfp.h>
16
17#include <media/v4l2-subdev.h>
18
19#include "vsp1.h"
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020020#include "vsp1_dl.h"
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030021#include "vsp1_lif.h"
22
23#define LIF_MIN_SIZE 2U
24#define LIF_MAX_SIZE 2048U
25
26/* -----------------------------------------------------------------------------
27 * Device Access
28 */
29
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020030static inline void vsp1_lif_write(struct vsp1_lif *lif, struct vsp1_dl_list *dl,
31 u32 reg, u32 data)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030032{
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020033 vsp1_dl_list_write(dl, reg, data);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030034}
35
36/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -020037 * V4L2 Subdevice Operations
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030038 */
39
40static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080041 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030042 struct v4l2_subdev_mbus_code_enum *code)
43{
44 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030045 MEDIA_BUS_FMT_ARGB8888_1X32,
46 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030047 };
48
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -030049 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
50 ARRAY_SIZE(codes));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030051}
52
53static int lif_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080054 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030055 struct v4l2_subdev_frame_size_enum *fse)
56{
Laurent Pinchart076e8342016-02-24 20:25:42 -030057 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LIF_MIN_SIZE,
58 LIF_MIN_SIZE, LIF_MAX_SIZE,
59 LIF_MAX_SIZE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030060}
61
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -020062static int lif_set_format(struct v4l2_subdev *subdev,
63 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030064 struct v4l2_subdev_format *fmt)
65{
66 struct vsp1_lif *lif = to_lif(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020067 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030068 struct v4l2_mbus_framefmt *format;
69
Laurent Pincharte790c3c2015-11-15 19:14:22 -020070 config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
71 if (!config)
72 return -EINVAL;
73
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030074 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030075 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
76 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
77 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030078
Laurent Pincharte790c3c2015-11-15 19:14:22 -020079 format = vsp1_entity_get_pad_format(&lif->entity, config, fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030080
81 if (fmt->pad == LIF_PAD_SOURCE) {
82 /* The LIF source format is always identical to its sink
83 * format.
84 */
85 fmt->format = *format;
86 return 0;
87 }
88
89 format->code = fmt->format.code;
90 format->width = clamp_t(unsigned int, fmt->format.width,
91 LIF_MIN_SIZE, LIF_MAX_SIZE);
92 format->height = clamp_t(unsigned int, fmt->format.height,
93 LIF_MIN_SIZE, LIF_MAX_SIZE);
94 format->field = V4L2_FIELD_NONE;
95 format->colorspace = V4L2_COLORSPACE_SRGB;
96
97 fmt->format = *format;
98
99 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200100 format = vsp1_entity_get_pad_format(&lif->entity, config,
101 LIF_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300102 *format = fmt->format;
103
104 return 0;
105}
106
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300107static struct v4l2_subdev_pad_ops lif_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200108 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300109 .enum_mbus_code = lif_enum_mbus_code,
110 .enum_frame_size = lif_enum_frame_size,
Laurent Pinchart3f557222016-02-24 21:10:13 -0300111 .get_fmt = vsp1_subdev_get_pad_format,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300112 .set_fmt = lif_set_format,
113};
114
115static struct v4l2_subdev_ops lif_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300116 .pad = &lif_pad_ops,
117};
118
119/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200120 * VSP1 Entity Operations
121 */
122
Laurent Pinchart83dd0192016-01-14 14:17:32 -0200123static void lif_configure(struct vsp1_entity *entity,
124 struct vsp1_pipeline *pipe,
125 struct vsp1_dl_list *dl)
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200126{
127 const struct v4l2_mbus_framefmt *format;
128 struct vsp1_lif *lif = to_lif(&entity->subdev);
129 unsigned int hbth = 1300;
130 unsigned int obth = 400;
131 unsigned int lbth = 200;
132
133 format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
134 LIF_PAD_SOURCE);
135
136 obth = min(obth, (format->width + 1) / 2 * format->height - 4);
137
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200138 vsp1_lif_write(lif, dl, VI6_LIF_CSBTH,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200139 (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
140 (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
141
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200142 vsp1_lif_write(lif, dl, VI6_LIF_CTRL,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200143 (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
144 (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
145 VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
146}
147
148static const struct vsp1_entity_operations lif_entity_ops = {
149 .configure = lif_configure,
150};
151
152/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300153 * Initialization and Cleanup
154 */
155
156struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
157{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300158 struct vsp1_lif *lif;
159 int ret;
160
161 lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
162 if (lif == NULL)
163 return ERR_PTR(-ENOMEM);
164
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200165 lif->entity.ops = &lif_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300166 lif->entity.type = VSP1_ENTITY_LIF;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300167
Laurent Pinchart823329d2015-11-15 19:42:01 -0200168 ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300169 if (ret < 0)
170 return ERR_PTR(ret);
171
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300172 return lif;
173}