blob: e32acae1fc6efe13f06a372f0b3b004506007217 [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) {
87 /* The LIF source format is always identical to its sink
88 * format.
89 */
90 fmt->format = *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030091 goto done;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030092 }
93
94 format->code = fmt->format.code;
95 format->width = clamp_t(unsigned int, fmt->format.width,
96 LIF_MIN_SIZE, LIF_MAX_SIZE);
97 format->height = clamp_t(unsigned int, fmt->format.height,
98 LIF_MIN_SIZE, LIF_MAX_SIZE);
99 format->field = V4L2_FIELD_NONE;
100 format->colorspace = V4L2_COLORSPACE_SRGB;
101
102 fmt->format = *format;
103
104 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200105 format = vsp1_entity_get_pad_format(&lif->entity, config,
106 LIF_PAD_SOURCE);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300107 *format = fmt->format;
108
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300109done:
110 mutex_unlock(&lif->entity.lock);
111 return ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300112}
113
Laurent Pincharteb9163d2016-06-17 21:11:26 -0300114static const struct v4l2_subdev_pad_ops lif_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200115 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300116 .enum_mbus_code = lif_enum_mbus_code,
117 .enum_frame_size = lif_enum_frame_size,
Laurent Pinchart3f557222016-02-24 21:10:13 -0300118 .get_fmt = vsp1_subdev_get_pad_format,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300119 .set_fmt = lif_set_format,
120};
121
Laurent Pincharteb9163d2016-06-17 21:11:26 -0300122static const struct v4l2_subdev_ops lif_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300123 .pad = &lif_pad_ops,
124};
125
126/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200127 * VSP1 Entity Operations
128 */
129
Laurent Pinchart83dd0192016-01-14 14:17:32 -0200130static void lif_configure(struct vsp1_entity *entity,
131 struct vsp1_pipeline *pipe,
Laurent Pinchartd21fbbb2016-09-11 19:39:30 -0300132 struct vsp1_dl_list *dl,
133 enum vsp1_entity_params params)
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200134{
135 const struct v4l2_mbus_framefmt *format;
136 struct vsp1_lif *lif = to_lif(&entity->subdev);
137 unsigned int hbth = 1300;
138 unsigned int obth = 400;
139 unsigned int lbth = 200;
140
Laurent Pinchartd21fbbb2016-09-11 19:39:30 -0300141 if (params != VSP1_ENTITY_PARAMS_INIT)
Laurent Pinchartfc845e52016-06-11 04:07:56 -0300142 return;
143
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200144 format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
145 LIF_PAD_SOURCE);
146
147 obth = min(obth, (format->width + 1) / 2 * format->height - 4);
148
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200149 vsp1_lif_write(lif, dl, VI6_LIF_CSBTH,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200150 (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
151 (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
152
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200153 vsp1_lif_write(lif, dl, VI6_LIF_CTRL,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200154 (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
155 (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
156 VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
157}
158
159static const struct vsp1_entity_operations lif_entity_ops = {
160 .configure = lif_configure,
161};
162
163/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300164 * Initialization and Cleanup
165 */
166
167struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
168{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300169 struct vsp1_lif *lif;
170 int ret;
171
172 lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
173 if (lif == NULL)
174 return ERR_PTR(-ENOMEM);
175
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200176 lif->entity.ops = &lif_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300177 lif->entity.type = VSP1_ENTITY_LIF;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300178
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200179 /* The LIF is never exposed to userspace, but media entity registration
180 * requires a function to be set. Use PROC_VIDEO_PIXEL_FORMATTER just to
181 * avoid triggering a WARN_ON(), the value won't be seen anywhere.
182 */
183 ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops,
184 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300185 if (ret < 0)
186 return ERR_PTR(ret);
187
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300188 return lif;
189}