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