blob: 702487f895b39e293254ed806de279f7933f5445 [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"
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020020#include "vsp1_dl.h"
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030021#include "vsp1_lif.h"
22
23#define LIF_MIN_SIZE 2U
Laurent Pinchart0d268dc2016-03-25 06:51:06 -030024#define LIF_MAX_SIZE 8190U
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030025
26/* -----------------------------------------------------------------------------
27 * Device Access
28 */
29
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020030static inline void vsp1_lif_write(struct vsp1_lif *lif, struct vsp1_dl_list *dl,
31 u32 reg, u32 data)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030032{
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020033 vsp1_dl_list_write(dl, reg, data);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030034}
35
36/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -020037 * V4L2 Subdevice Operations
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030038 */
39
40static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080041 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030042 struct v4l2_subdev_mbus_code_enum *code)
43{
44 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030045 MEDIA_BUS_FMT_ARGB8888_1X32,
46 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030047 };
48
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -030049 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
50 ARRAY_SIZE(codes));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030051}
52
53static int lif_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080054 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030055 struct v4l2_subdev_frame_size_enum *fse)
56{
Laurent Pinchart076e8342016-02-24 20:25:42 -030057 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LIF_MIN_SIZE,
58 LIF_MIN_SIZE, LIF_MAX_SIZE,
59 LIF_MAX_SIZE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030060}
61
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -020062static int lif_set_format(struct v4l2_subdev *subdev,
63 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030064 struct v4l2_subdev_format *fmt)
65{
66 struct vsp1_lif *lif = to_lif(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020067 struct v4l2_subdev_pad_config *config;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030068 struct v4l2_mbus_framefmt *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030069 int ret = 0;
70
71 mutex_lock(&lif->entity.lock);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030072
Laurent Pincharte790c3c2015-11-15 19:14:22 -020073 config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030074 if (!config) {
75 ret = -EINVAL;
76 goto done;
77 }
Laurent Pincharte790c3c2015-11-15 19:14:22 -020078
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030079 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030080 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;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030083
Laurent Pincharte790c3c2015-11-15 19:14:22 -020084 format = vsp1_entity_get_pad_format(&lif->entity, config, fmt->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030085
86 if (fmt->pad == LIF_PAD_SOURCE) {
Laurent Pinchart9dbed952017-02-26 10:29:50 -030087 /*
88 * The LIF source format is always identical to its sink
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030089 * format.
90 */
91 fmt->format = *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030092 goto done;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030093 }
94
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;
102
103 fmt->format = *format;
104
105 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200106 format = vsp1_entity_get_pad_format(&lif->entity, config,
107 LIF_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300108 *format = fmt->format;
109
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300110done:
111 mutex_unlock(&lif->entity.lock);
112 return ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300113}
114
Laurent Pincharteb9163d2016-06-17 21:11:26 -0300115static const struct v4l2_subdev_pad_ops lif_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200116 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300117 .enum_mbus_code = lif_enum_mbus_code,
118 .enum_frame_size = lif_enum_frame_size,
Laurent Pinchart3f557222016-02-24 21:10:13 -0300119 .get_fmt = vsp1_subdev_get_pad_format,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300120 .set_fmt = lif_set_format,
121};
122
Laurent Pincharteb9163d2016-06-17 21:11:26 -0300123static const struct v4l2_subdev_ops lif_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300124 .pad = &lif_pad_ops,
125};
126
127/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200128 * VSP1 Entity Operations
129 */
130
Laurent Pinchart83dd0192016-01-14 14:17:32 -0200131static void lif_configure(struct vsp1_entity *entity,
132 struct vsp1_pipeline *pipe,
Laurent Pinchartd21fbbb2016-09-11 19:39:30 -0300133 struct vsp1_dl_list *dl,
134 enum vsp1_entity_params params)
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200135{
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;
141
Laurent Pinchartd21fbbb2016-09-11 19:39:30 -0300142 if (params != VSP1_ENTITY_PARAMS_INIT)
Laurent Pinchartfc845e52016-06-11 04:07:56 -0300143 return;
144
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200145 format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
146 LIF_PAD_SOURCE);
147
148 obth = min(obth, (format->width + 1) / 2 * format->height - 4);
149
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200150 vsp1_lif_write(lif, dl, VI6_LIF_CSBTH,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200151 (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
152 (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
153
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200154 vsp1_lif_write(lif, dl, VI6_LIF_CTRL,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200155 (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);
158}
159
160static const struct vsp1_entity_operations lif_entity_ops = {
161 .configure = lif_configure,
162};
163
164/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300165 * Initialization and Cleanup
166 */
167
168struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
169{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300170 struct vsp1_lif *lif;
171 int ret;
172
173 lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
174 if (lif == NULL)
175 return ERR_PTR(-ENOMEM);
176
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200177 lif->entity.ops = &lif_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300178 lif->entity.type = VSP1_ENTITY_LIF;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300179
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300180 /*
181 * The LIF is never exposed to userspace, but media entity registration
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200182 * requires a function to be set. Use PROC_VIDEO_PIXEL_FORMATTER just to
183 * avoid triggering a WARN_ON(), the value won't be seen anywhere.
184 */
185 ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops,
186 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300187 if (ret < 0)
188 return ERR_PTR(ret);
189
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300190 return lif;
191}