blob: d8d8d3b6c129bc9ffad8dd94a7078fe70cd1caa2 [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"
20#include "vsp1_lif.h"
21
22#define LIF_MIN_SIZE 2U
23#define LIF_MAX_SIZE 2048U
24
25/* -----------------------------------------------------------------------------
26 * Device Access
27 */
28
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030029static inline void vsp1_lif_write(struct vsp1_lif *lif, u32 reg, u32 data)
30{
Takashi Saito1517b032015-09-07 01:40:25 -030031 vsp1_mod_write(&lif->entity, reg, data);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030032}
33
34/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -020035 * V4L2 Subdevice Operations
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030036 */
37
38static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080039 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030040 struct v4l2_subdev_mbus_code_enum *code)
41{
42 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030043 MEDIA_BUS_FMT_ARGB8888_1X32,
44 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030045 };
Hans Verkuil3f1ccf12015-03-04 01:47:57 -080046 struct vsp1_lif *lif = to_lif(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030047
48 if (code->pad == LIF_PAD_SINK) {
49 if (code->index >= ARRAY_SIZE(codes))
50 return -EINVAL;
51
52 code->code = codes[code->index];
53 } else {
Laurent Pincharte790c3c2015-11-15 19:14:22 -020054 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030055 struct v4l2_mbus_framefmt *format;
56
57 /* The LIF can't perform format conversion, the sink format is
58 * always identical to the source format.
59 */
60 if (code->index)
61 return -EINVAL;
62
Laurent Pincharte790c3c2015-11-15 19:14:22 -020063 config = vsp1_entity_get_pad_config(&lif->entity, cfg,
64 code->which);
65 if (!config)
66 return -EINVAL;
67
68 format = vsp1_entity_get_pad_format(&lif->entity, config,
69 LIF_PAD_SINK);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030070 code->code = format->code;
71 }
72
73 return 0;
74}
75
76static int lif_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080077 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030078 struct v4l2_subdev_frame_size_enum *fse)
79{
Hans Verkuil5778e742015-03-04 01:47:58 -080080 struct vsp1_lif *lif = to_lif(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020081 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030082 struct v4l2_mbus_framefmt *format;
83
Laurent Pincharte790c3c2015-11-15 19:14:22 -020084 config = vsp1_entity_get_pad_config(&lif->entity, cfg, fse->which);
85 if (!config)
86 return -EINVAL;
87
88 format = vsp1_entity_get_pad_format(&lif->entity, config, LIF_PAD_SINK);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030089
90 if (fse->index || fse->code != format->code)
91 return -EINVAL;
92
93 if (fse->pad == LIF_PAD_SINK) {
94 fse->min_width = LIF_MIN_SIZE;
95 fse->max_width = LIF_MAX_SIZE;
96 fse->min_height = LIF_MIN_SIZE;
97 fse->max_height = LIF_MAX_SIZE;
98 } else {
99 fse->min_width = format->width;
100 fse->max_width = format->width;
101 fse->min_height = format->height;
102 fse->max_height = format->height;
103 }
104
105 return 0;
106}
107
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200108static int lif_get_format(struct v4l2_subdev *subdev,
109 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300110 struct v4l2_subdev_format *fmt)
111{
112 struct vsp1_lif *lif = to_lif(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200113 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300114
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200115 config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
116 if (!config)
117 return -EINVAL;
118
119 fmt->format = *vsp1_entity_get_pad_format(&lif->entity, config,
120 fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300121
122 return 0;
123}
124
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200125static int lif_set_format(struct v4l2_subdev *subdev,
126 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300127 struct v4l2_subdev_format *fmt)
128{
129 struct vsp1_lif *lif = to_lif(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200130 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300131 struct v4l2_mbus_framefmt *format;
132
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200133 config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
134 if (!config)
135 return -EINVAL;
136
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300137 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300138 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
139 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
140 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300141
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200142 format = vsp1_entity_get_pad_format(&lif->entity, config, fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300143
144 if (fmt->pad == LIF_PAD_SOURCE) {
145 /* The LIF source format is always identical to its sink
146 * format.
147 */
148 fmt->format = *format;
149 return 0;
150 }
151
152 format->code = fmt->format.code;
153 format->width = clamp_t(unsigned int, fmt->format.width,
154 LIF_MIN_SIZE, LIF_MAX_SIZE);
155 format->height = clamp_t(unsigned int, fmt->format.height,
156 LIF_MIN_SIZE, LIF_MAX_SIZE);
157 format->field = V4L2_FIELD_NONE;
158 format->colorspace = V4L2_COLORSPACE_SRGB;
159
160 fmt->format = *format;
161
162 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200163 format = vsp1_entity_get_pad_format(&lif->entity, config,
164 LIF_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300165 *format = fmt->format;
166
167 return 0;
168}
169
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300170static struct v4l2_subdev_pad_ops lif_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200171 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300172 .enum_mbus_code = lif_enum_mbus_code,
173 .enum_frame_size = lif_enum_frame_size,
174 .get_fmt = lif_get_format,
175 .set_fmt = lif_set_format,
176};
177
178static struct v4l2_subdev_ops lif_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300179 .pad = &lif_pad_ops,
180};
181
182/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200183 * VSP1 Entity Operations
184 */
185
186static void lif_configure(struct vsp1_entity *entity)
187{
188 const struct v4l2_mbus_framefmt *format;
189 struct vsp1_lif *lif = to_lif(&entity->subdev);
190 unsigned int hbth = 1300;
191 unsigned int obth = 400;
192 unsigned int lbth = 200;
193
194 format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
195 LIF_PAD_SOURCE);
196
197 obth = min(obth, (format->width + 1) / 2 * format->height - 4);
198
199 vsp1_lif_write(lif, VI6_LIF_CSBTH,
200 (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
201 (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
202
203 vsp1_lif_write(lif, VI6_LIF_CTRL,
204 (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
205 (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
206 VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
207}
208
209static const struct vsp1_entity_operations lif_entity_ops = {
210 .configure = lif_configure,
211};
212
213/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300214 * Initialization and Cleanup
215 */
216
217struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
218{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300219 struct vsp1_lif *lif;
220 int ret;
221
222 lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
223 if (lif == NULL)
224 return ERR_PTR(-ENOMEM);
225
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200226 lif->entity.ops = &lif_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300227 lif->entity.type = VSP1_ENTITY_LIF;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300228
Laurent Pinchart823329d2015-11-15 19:42:01 -0200229 ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300230 if (ret < 0)
231 return ERR_PTR(ret);
232
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300233 return lif;
234}