Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / platform / vsp1 / vsp1_lif.c
blobe6fa16d7fda897d342ef99274d31cef4948a050e
1 /*
2 * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <linux/device.h>
15 #include <linux/gfp.h>
17 #include <media/v4l2-subdev.h>
19 #include "vsp1.h"
20 #include "vsp1_dl.h"
21 #include "vsp1_lif.h"
23 #define LIF_MIN_SIZE 2U
24 #define LIF_MAX_SIZE 8190U
26 /* -----------------------------------------------------------------------------
27 * Device Access
30 static inline void vsp1_lif_write(struct vsp1_lif *lif, struct vsp1_dl_list *dl,
31 u32 reg, u32 data)
33 vsp1_dl_list_write(dl, reg + lif->entity.index * VI6_LIF_OFFSET, data);
36 /* -----------------------------------------------------------------------------
37 * V4L2 Subdevice Operations
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)
44 static const unsigned int codes[] = {
45 MEDIA_BUS_FMT_ARGB8888_1X32,
46 MEDIA_BUS_FMT_AYUV8_1X32,
49 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
50 ARRAY_SIZE(codes));
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)
57 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LIF_MIN_SIZE,
58 LIF_MIN_SIZE, LIF_MAX_SIZE,
59 LIF_MAX_SIZE);
62 static int lif_set_format(struct v4l2_subdev *subdev,
63 struct v4l2_subdev_pad_config *cfg,
64 struct v4l2_subdev_format *fmt)
66 struct vsp1_lif *lif = to_lif(subdev);
67 struct v4l2_subdev_pad_config *config;
68 struct v4l2_mbus_framefmt *format;
69 int ret = 0;
71 mutex_lock(&lif->entity.lock);
73 config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
74 if (!config) {
75 ret = -EINVAL;
76 goto done;
79 /* Default to YUV if the requested format is not supported. */
80 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
81 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
82 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
84 format = vsp1_entity_get_pad_format(&lif->entity, config, fmt->pad);
86 if (fmt->pad == LIF_PAD_SOURCE) {
88 * The LIF source format is always identical to its sink
89 * format.
91 fmt->format = *format;
92 goto done;
95 format->code = fmt->format.code;
96 format->width = clamp_t(unsigned int, fmt->format.width,
97 LIF_MIN_SIZE, LIF_MAX_SIZE);
98 format->height = clamp_t(unsigned int, fmt->format.height,
99 LIF_MIN_SIZE, LIF_MAX_SIZE);
100 format->field = V4L2_FIELD_NONE;
101 format->colorspace = V4L2_COLORSPACE_SRGB;
103 fmt->format = *format;
105 /* Propagate the format to the source pad. */
106 format = vsp1_entity_get_pad_format(&lif->entity, config,
107 LIF_PAD_SOURCE);
108 *format = fmt->format;
110 done:
111 mutex_unlock(&lif->entity.lock);
112 return ret;
115 static const struct v4l2_subdev_pad_ops lif_pad_ops = {
116 .init_cfg = vsp1_entity_init_cfg,
117 .enum_mbus_code = lif_enum_mbus_code,
118 .enum_frame_size = lif_enum_frame_size,
119 .get_fmt = vsp1_subdev_get_pad_format,
120 .set_fmt = lif_set_format,
123 static const struct v4l2_subdev_ops lif_ops = {
124 .pad = &lif_pad_ops,
127 /* -----------------------------------------------------------------------------
128 * VSP1 Entity Operations
131 static void lif_configure(struct vsp1_entity *entity,
132 struct vsp1_pipeline *pipe,
133 struct vsp1_dl_list *dl,
134 enum vsp1_entity_params params)
136 const struct v4l2_mbus_framefmt *format;
137 struct vsp1_lif *lif = to_lif(&entity->subdev);
138 unsigned int hbth = 1300;
139 unsigned int obth = 400;
140 unsigned int lbth = 200;
142 if (params != VSP1_ENTITY_PARAMS_INIT)
143 return;
145 format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
146 LIF_PAD_SOURCE);
148 obth = min(obth, (format->width + 1) / 2 * format->height - 4);
150 vsp1_lif_write(lif, dl, VI6_LIF_CSBTH,
151 (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
152 (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
154 vsp1_lif_write(lif, dl, VI6_LIF_CTRL,
155 (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
156 (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
157 VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
160 static const struct vsp1_entity_operations lif_entity_ops = {
161 .configure = lif_configure,
164 /* -----------------------------------------------------------------------------
165 * Initialization and Cleanup
168 struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1, unsigned int index)
170 struct vsp1_lif *lif;
171 int ret;
173 lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
174 if (lif == NULL)
175 return ERR_PTR(-ENOMEM);
177 lif->entity.ops = &lif_entity_ops;
178 lif->entity.type = VSP1_ENTITY_LIF;
179 lif->entity.index = index;
182 * The LIF is never exposed to userspace, but media entity registration
183 * requires a function to be set. Use PROC_VIDEO_PIXEL_FORMATTER just to
184 * avoid triggering a WARN_ON(), the value won't be seen anywhere.
186 ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops,
187 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
188 if (ret < 0)
189 return ERR_PTR(ret);
191 return lif;