Merge branch 'parisc-4.8-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[cascardo/linux.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_dl.h"
21 #include "vsp1_lif.h"
22
23 #define LIF_MIN_SIZE                            2U
24 #define LIF_MAX_SIZE                            8190U
25
26 /* -----------------------------------------------------------------------------
27  * Device Access
28  */
29
30 static inline void vsp1_lif_write(struct vsp1_lif *lif, struct vsp1_dl_list *dl,
31                                   u32 reg, u32 data)
32 {
33         vsp1_dl_list_write(dl, reg, data);
34 }
35
36 /* -----------------------------------------------------------------------------
37  * V4L2 Subdevice Operations
38  */
39
40 static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
41                               struct v4l2_subdev_pad_config *cfg,
42                               struct v4l2_subdev_mbus_code_enum *code)
43 {
44         static const unsigned int codes[] = {
45                 MEDIA_BUS_FMT_ARGB8888_1X32,
46                 MEDIA_BUS_FMT_AYUV8_1X32,
47         };
48
49         return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
50                                           ARRAY_SIZE(codes));
51 }
52
53 static int lif_enum_frame_size(struct v4l2_subdev *subdev,
54                                struct v4l2_subdev_pad_config *cfg,
55                                struct v4l2_subdev_frame_size_enum *fse)
56 {
57         return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LIF_MIN_SIZE,
58                                            LIF_MIN_SIZE, LIF_MAX_SIZE,
59                                            LIF_MAX_SIZE);
60 }
61
62 static int lif_set_format(struct v4l2_subdev *subdev,
63                           struct v4l2_subdev_pad_config *cfg,
64                           struct v4l2_subdev_format *fmt)
65 {
66         struct vsp1_lif *lif = to_lif(subdev);
67         struct v4l2_subdev_pad_config *config;
68         struct v4l2_mbus_framefmt *format;
69
70         config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
71         if (!config)
72                 return -EINVAL;
73
74         /* Default to YUV if the requested format is not supported. */
75         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;
78
79         format = vsp1_entity_get_pad_format(&lif->entity, config, fmt->pad);
80
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. */
100         format = vsp1_entity_get_pad_format(&lif->entity, config,
101                                             LIF_PAD_SOURCE);
102         *format = fmt->format;
103
104         return 0;
105 }
106
107 static const struct v4l2_subdev_pad_ops lif_pad_ops = {
108         .init_cfg = vsp1_entity_init_cfg,
109         .enum_mbus_code = lif_enum_mbus_code,
110         .enum_frame_size = lif_enum_frame_size,
111         .get_fmt = vsp1_subdev_get_pad_format,
112         .set_fmt = lif_set_format,
113 };
114
115 static const struct v4l2_subdev_ops lif_ops = {
116         .pad    = &lif_pad_ops,
117 };
118
119 /* -----------------------------------------------------------------------------
120  * VSP1 Entity Operations
121  */
122
123 static void lif_configure(struct vsp1_entity *entity,
124                           struct vsp1_pipeline *pipe,
125                           struct vsp1_dl_list *dl, bool full)
126 {
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         if (!full)
134                 return;
135
136         format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
137                                             LIF_PAD_SOURCE);
138
139         obth = min(obth, (format->width + 1) / 2 * format->height - 4);
140
141         vsp1_lif_write(lif, dl, VI6_LIF_CSBTH,
142                         (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
143                         (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
144
145         vsp1_lif_write(lif, dl, VI6_LIF_CTRL,
146                         (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
147                         (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
148                         VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
149 }
150
151 static const struct vsp1_entity_operations lif_entity_ops = {
152         .configure = lif_configure,
153 };
154
155 /* -----------------------------------------------------------------------------
156  * Initialization and Cleanup
157  */
158
159 struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
160 {
161         struct vsp1_lif *lif;
162         int ret;
163
164         lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
165         if (lif == NULL)
166                 return ERR_PTR(-ENOMEM);
167
168         lif->entity.ops = &lif_entity_ops;
169         lif->entity.type = VSP1_ENTITY_LIF;
170
171         /* The LIF is never exposed to userspace, but media entity registration
172          * requires a function to be set. Use PROC_VIDEO_PIXEL_FORMATTER just to
173          * avoid triggering a WARN_ON(), the value won't be seen anywhere.
174          */
175         ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops,
176                                MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
177         if (ret < 0)
178                 return ERR_PTR(ret);
179
180         return lif;
181 }