Merge tag 'char-misc-4.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-block.git] / drivers / media / platform / vsp1 / vsp1_lif.c
CommitLineData
26e0ca22
LP
1/*
2 * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface
3 *
8a1edc55 4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
26e0ca22
LP
5 *
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
26e0ca22
LP
29static inline void vsp1_lif_write(struct vsp1_lif *lif, u32 reg, u32 data)
30{
1517b039 31 vsp1_mod_write(&lif->entity, reg, data);
26e0ca22
LP
32}
33
34/* -----------------------------------------------------------------------------
35 * V4L2 Subdevice Core Operations
36 */
37
38static int lif_s_stream(struct v4l2_subdev *subdev, int enable)
39{
40 const struct v4l2_mbus_framefmt *format;
41 struct vsp1_lif *lif = to_lif(subdev);
42 unsigned int hbth = 1300;
43 unsigned int obth = 400;
44 unsigned int lbth = 200;
45
46 if (!enable) {
1517b039 47 vsp1_write(lif->entity.vsp1, VI6_LIF_CTRL, 0);
26e0ca22
LP
48 return 0;
49 }
50
51 format = &lif->entity.formats[LIF_PAD_SOURCE];
52
53 obth = min(obth, (format->width + 1) / 2 * format->height - 4);
54
55 vsp1_lif_write(lif, VI6_LIF_CSBTH,
56 (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
57 (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
58
59 vsp1_lif_write(lif, VI6_LIF_CTRL,
60 (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
61 (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
62 VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
63
64 return 0;
65}
66
67/* -----------------------------------------------------------------------------
68 * V4L2 Subdevice Pad Operations
69 */
70
71static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
f7234138 72 struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
73 struct v4l2_subdev_mbus_code_enum *code)
74{
75 static const unsigned int codes[] = {
27ffaeb0
BB
76 MEDIA_BUS_FMT_ARGB8888_1X32,
77 MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22 78 };
3f1ccf16 79 struct vsp1_lif *lif = to_lif(subdev);
26e0ca22
LP
80
81 if (code->pad == LIF_PAD_SINK) {
82 if (code->index >= ARRAY_SIZE(codes))
83 return -EINVAL;
84
85 code->code = codes[code->index];
86 } else {
87 struct v4l2_mbus_framefmt *format;
88
89 /* The LIF can't perform format conversion, the sink format is
90 * always identical to the source format.
91 */
92 if (code->index)
93 return -EINVAL;
94
3f1ccf16
HV
95 format = vsp1_entity_get_pad_format(&lif->entity, cfg,
96 LIF_PAD_SINK, code->which);
26e0ca22
LP
97 code->code = format->code;
98 }
99
100 return 0;
101}
102
103static int lif_enum_frame_size(struct v4l2_subdev *subdev,
f7234138 104 struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
105 struct v4l2_subdev_frame_size_enum *fse)
106{
5778e749 107 struct vsp1_lif *lif = to_lif(subdev);
26e0ca22
LP
108 struct v4l2_mbus_framefmt *format;
109
5778e749
HV
110 format = vsp1_entity_get_pad_format(&lif->entity, cfg, LIF_PAD_SINK,
111 fse->which);
26e0ca22
LP
112
113 if (fse->index || fse->code != format->code)
114 return -EINVAL;
115
116 if (fse->pad == LIF_PAD_SINK) {
117 fse->min_width = LIF_MIN_SIZE;
118 fse->max_width = LIF_MAX_SIZE;
119 fse->min_height = LIF_MIN_SIZE;
120 fse->max_height = LIF_MAX_SIZE;
121 } else {
122 fse->min_width = format->width;
123 fse->max_width = format->width;
124 fse->min_height = format->height;
125 fse->max_height = format->height;
126 }
127
128 return 0;
129}
130
f7234138 131static int lif_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
132 struct v4l2_subdev_format *fmt)
133{
134 struct vsp1_lif *lif = to_lif(subdev);
135
f7234138 136 fmt->format = *vsp1_entity_get_pad_format(&lif->entity, cfg, fmt->pad,
26e0ca22
LP
137 fmt->which);
138
139 return 0;
140}
141
f7234138 142static int lif_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
143 struct v4l2_subdev_format *fmt)
144{
145 struct vsp1_lif *lif = to_lif(subdev);
146 struct v4l2_mbus_framefmt *format;
147
148 /* Default to YUV if the requested format is not supported. */
27ffaeb0
BB
149 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
150 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
151 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
26e0ca22 152
f7234138 153 format = vsp1_entity_get_pad_format(&lif->entity, cfg, fmt->pad,
26e0ca22
LP
154 fmt->which);
155
156 if (fmt->pad == LIF_PAD_SOURCE) {
157 /* The LIF source format is always identical to its sink
158 * format.
159 */
160 fmt->format = *format;
161 return 0;
162 }
163
164 format->code = fmt->format.code;
165 format->width = clamp_t(unsigned int, fmt->format.width,
166 LIF_MIN_SIZE, LIF_MAX_SIZE);
167 format->height = clamp_t(unsigned int, fmt->format.height,
168 LIF_MIN_SIZE, LIF_MAX_SIZE);
169 format->field = V4L2_FIELD_NONE;
170 format->colorspace = V4L2_COLORSPACE_SRGB;
171
172 fmt->format = *format;
173
174 /* Propagate the format to the source pad. */
f7234138 175 format = vsp1_entity_get_pad_format(&lif->entity, cfg, LIF_PAD_SOURCE,
26e0ca22
LP
176 fmt->which);
177 *format = fmt->format;
178
179 return 0;
180}
181
182/* -----------------------------------------------------------------------------
183 * V4L2 Subdevice Operations
184 */
185
186static struct v4l2_subdev_video_ops lif_video_ops = {
187 .s_stream = lif_s_stream,
188};
189
190static struct v4l2_subdev_pad_ops lif_pad_ops = {
191 .enum_mbus_code = lif_enum_mbus_code,
192 .enum_frame_size = lif_enum_frame_size,
193 .get_fmt = lif_get_format,
194 .set_fmt = lif_set_format,
195};
196
197static struct v4l2_subdev_ops lif_ops = {
198 .video = &lif_video_ops,
199 .pad = &lif_pad_ops,
200};
201
202/* -----------------------------------------------------------------------------
203 * Initialization and Cleanup
204 */
205
206struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
207{
208 struct v4l2_subdev *subdev;
209 struct vsp1_lif *lif;
210 int ret;
211
212 lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
213 if (lif == NULL)
214 return ERR_PTR(-ENOMEM);
215
216 lif->entity.type = VSP1_ENTITY_LIF;
26e0ca22
LP
217
218 ret = vsp1_entity_init(vsp1, &lif->entity, 2);
219 if (ret < 0)
220 return ERR_PTR(ret);
221
222 /* Initialize the V4L2 subdev. */
223 subdev = &lif->entity.subdev;
224 v4l2_subdev_init(subdev, &lif_ops);
225
babca007 226 subdev->entity.ops = &vsp1->media_ops;
26e0ca22
LP
227 subdev->internal_ops = &vsp1_subdev_internal_ops;
228 snprintf(subdev->name, sizeof(subdev->name), "%s lif",
229 dev_name(vsp1->dev));
230 v4l2_set_subdevdata(subdev, lif);
231 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
232
233 vsp1_entity_init_formats(subdev, NULL);
234
235 return lif;
236}