blob: b20b842f06ba6c79858dca2e746857ee60308055 [file] [log] [blame]
Laurent Pinchart1c4b5f42018-04-22 17:33:20 -04001// SPDX-License-Identifier: GPL-2.0+
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03002/*
3 * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface
4 *
Laurent Pinchart8a1edc52014-02-06 14:42:31 -03005 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03006 *
7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03008 */
9
10#include <linux/device.h>
11#include <linux/gfp.h>
12
13#include <media/v4l2-subdev.h>
14
15#include "vsp1.h"
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020016#include "vsp1_dl.h"
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030017#include "vsp1_lif.h"
18
19#define LIF_MIN_SIZE 2U
Laurent Pinchart0d268dc2016-03-25 06:51:06 -030020#define LIF_MAX_SIZE 8190U
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030021
22/* -----------------------------------------------------------------------------
23 * Device Access
24 */
25
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020026static inline void vsp1_lif_write(struct vsp1_lif *lif, struct vsp1_dl_list *dl,
27 u32 reg, u32 data)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030028{
Laurent Pinchart3be0bf92017-06-21 16:10:18 +030029 vsp1_dl_list_write(dl, reg + lif->entity.index * VI6_LIF_OFFSET, data);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030030}
31
32/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -020033 * V4L2 Subdevice Operations
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030034 */
35
36static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080037 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030038 struct v4l2_subdev_mbus_code_enum *code)
39{
40 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030041 MEDIA_BUS_FMT_ARGB8888_1X32,
42 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030043 };
44
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -030045 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
46 ARRAY_SIZE(codes));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030047}
48
49static int lif_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080050 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030051 struct v4l2_subdev_frame_size_enum *fse)
52{
Laurent Pinchart076e8342016-02-24 20:25:42 -030053 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LIF_MIN_SIZE,
54 LIF_MIN_SIZE, LIF_MAX_SIZE,
55 LIF_MAX_SIZE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030056}
57
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -020058static int lif_set_format(struct v4l2_subdev *subdev,
59 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030060 struct v4l2_subdev_format *fmt)
61{
62 struct vsp1_lif *lif = to_lif(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020063 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030064 struct v4l2_mbus_framefmt *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030065 int ret = 0;
66
67 mutex_lock(&lif->entity.lock);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030068
Laurent Pincharte790c3c2015-11-15 19:14:22 -020069 config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030070 if (!config) {
71 ret = -EINVAL;
72 goto done;
73 }
Laurent Pincharte790c3c2015-11-15 19:14:22 -020074
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030075 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030076 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
77 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
78 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030079
Laurent Pincharte790c3c2015-11-15 19:14:22 -020080 format = vsp1_entity_get_pad_format(&lif->entity, config, fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030081
82 if (fmt->pad == LIF_PAD_SOURCE) {
Laurent Pinchart9dbed952017-02-26 10:29:50 -030083 /*
84 * The LIF source format is always identical to its sink
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030085 * format.
86 */
87 fmt->format = *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030088 goto done;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030089 }
90
91 format->code = fmt->format.code;
92 format->width = clamp_t(unsigned int, fmt->format.width,
93 LIF_MIN_SIZE, LIF_MAX_SIZE);
94 format->height = clamp_t(unsigned int, fmt->format.height,
95 LIF_MIN_SIZE, LIF_MAX_SIZE);
96 format->field = V4L2_FIELD_NONE;
97 format->colorspace = V4L2_COLORSPACE_SRGB;
98
99 fmt->format = *format;
100
101 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200102 format = vsp1_entity_get_pad_format(&lif->entity, config,
103 LIF_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300104 *format = fmt->format;
105
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300106done:
107 mutex_unlock(&lif->entity.lock);
108 return ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300109}
110
Laurent Pincharteb9163d2016-06-17 21:11:26 -0300111static const struct v4l2_subdev_pad_ops lif_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200112 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300113 .enum_mbus_code = lif_enum_mbus_code,
114 .enum_frame_size = lif_enum_frame_size,
Laurent Pinchart3f557222016-02-24 21:10:13 -0300115 .get_fmt = vsp1_subdev_get_pad_format,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300116 .set_fmt = lif_set_format,
117};
118
Laurent Pincharteb9163d2016-06-17 21:11:26 -0300119static const struct v4l2_subdev_ops lif_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300120 .pad = &lif_pad_ops,
121};
122
123/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200124 * VSP1 Entity Operations
125 */
126
Laurent Pinchart83dd0192016-01-14 14:17:32 -0200127static void lif_configure(struct vsp1_entity *entity,
128 struct vsp1_pipeline *pipe,
Laurent Pinchartd21fbbb2016-09-11 19:39:30 -0300129 struct vsp1_dl_list *dl,
130 enum vsp1_entity_params params)
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200131{
132 const struct v4l2_mbus_framefmt *format;
133 struct vsp1_lif *lif = to_lif(&entity->subdev);
134 unsigned int hbth = 1300;
135 unsigned int obth = 400;
136 unsigned int lbth = 200;
137
Laurent Pinchartd21fbbb2016-09-11 19:39:30 -0300138 if (params != VSP1_ENTITY_PARAMS_INIT)
Laurent Pinchartfc845e52016-06-11 04:07:56 -0300139 return;
140
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200141 format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
142 LIF_PAD_SOURCE);
143
144 obth = min(obth, (format->width + 1) / 2 * format->height - 4);
145
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200146 vsp1_lif_write(lif, dl, VI6_LIF_CSBTH,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200147 (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
148 (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
149
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200150 vsp1_lif_write(lif, dl, VI6_LIF_CTRL,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200151 (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
152 (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
153 VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
Sergei Shtylyov7f43ff92018-01-18 09:05:51 -0500154
155 /*
156 * On R-Car V3M the LIF0 buffer attribute register has to be set to a
157 * non-default value to guarantee proper operation (otherwise artifacts
158 * may appear on the output). The value required by the manual is not
159 * explained but is likely a buffer size or threshold.
160 */
161 if ((entity->vsp1->version & VI6_IP_VERSION_MASK) ==
162 (VI6_IP_VERSION_MODEL_VSPD_V3 | VI6_IP_VERSION_SOC_V3M))
163 vsp1_lif_write(lif, dl, VI6_LIF_LBA,
164 VI6_LIF_LBA_LBA0 |
165 (1536 << VI6_LIF_LBA_LBA1_SHIFT));
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200166}
167
168static const struct vsp1_entity_operations lif_entity_ops = {
169 .configure = lif_configure,
170};
171
172/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300173 * Initialization and Cleanup
174 */
175
Laurent Pinchart3be0bf92017-06-21 16:10:18 +0300176struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1, unsigned int index)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300177{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300178 struct vsp1_lif *lif;
179 int ret;
180
181 lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
182 if (lif == NULL)
183 return ERR_PTR(-ENOMEM);
184
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200185 lif->entity.ops = &lif_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300186 lif->entity.type = VSP1_ENTITY_LIF;
Laurent Pinchart3be0bf92017-06-21 16:10:18 +0300187 lif->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300188
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300189 /*
190 * The LIF is never exposed to userspace, but media entity registration
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200191 * requires a function to be set. Use PROC_VIDEO_PIXEL_FORMATTER just to
192 * avoid triggering a WARN_ON(), the value won't be seen anywhere.
193 */
194 ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops,
195 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300196 if (ret < 0)
197 return ERR_PTR(ret);
198
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300199 return lif;
200}