1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * vsp1_lif.c  --  R-Car VSP1 LCD Controller Interface
4  *
5  * Copyright (C) 2013-2014 Renesas Electronics Corporation
6  *
7  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8  */
9 
10 #include <linux/device.h>
11 #include <linux/gfp.h>
12 
13 #include <media/v4l2-subdev.h>
14 
15 #include "vsp1.h"
16 #include "vsp1_dl.h"
17 #include "vsp1_lif.h"
18 
19 #define LIF_MIN_SIZE				2U
20 #define LIF_MAX_SIZE				8190U
21 
22 /* -----------------------------------------------------------------------------
23  * Device Access
24  */
25 
vsp1_lif_write(struct vsp1_lif * lif,struct vsp1_dl_body * dlb,u32 reg,u32 data)26 static inline void vsp1_lif_write(struct vsp1_lif *lif,
27 				  struct vsp1_dl_body *dlb, u32 reg, u32 data)
28 {
29 	vsp1_dl_body_write(dlb, reg + lif->entity.index * VI6_LIF_OFFSET,
30 			       data);
31 }
32 
33 /* -----------------------------------------------------------------------------
34  * V4L2 Subdevice Operations
35  */
36 
37 static const unsigned int lif_codes[] = {
38 	MEDIA_BUS_FMT_ARGB8888_1X32,
39 	MEDIA_BUS_FMT_AYUV8_1X32,
40 };
41 
lif_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)42 static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
43 			      struct v4l2_subdev_state *sd_state,
44 			      struct v4l2_subdev_mbus_code_enum *code)
45 {
46 	return vsp1_subdev_enum_mbus_code(subdev, sd_state, code, lif_codes,
47 					  ARRAY_SIZE(lif_codes));
48 }
49 
lif_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)50 static int lif_enum_frame_size(struct v4l2_subdev *subdev,
51 			       struct v4l2_subdev_state *sd_state,
52 			       struct v4l2_subdev_frame_size_enum *fse)
53 {
54 	return vsp1_subdev_enum_frame_size(subdev, sd_state, fse,
55 					   LIF_MIN_SIZE,
56 					   LIF_MIN_SIZE, LIF_MAX_SIZE,
57 					   LIF_MAX_SIZE);
58 }
59 
lif_set_format(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)60 static int lif_set_format(struct v4l2_subdev *subdev,
61 			  struct v4l2_subdev_state *sd_state,
62 			  struct v4l2_subdev_format *fmt)
63 {
64 	return vsp1_subdev_set_pad_format(subdev, sd_state, fmt, lif_codes,
65 					  ARRAY_SIZE(lif_codes),
66 					  LIF_MIN_SIZE, LIF_MIN_SIZE,
67 					  LIF_MAX_SIZE, LIF_MAX_SIZE);
68 }
69 
70 static const struct v4l2_subdev_pad_ops lif_pad_ops = {
71 	.init_cfg = vsp1_entity_init_cfg,
72 	.enum_mbus_code = lif_enum_mbus_code,
73 	.enum_frame_size = lif_enum_frame_size,
74 	.get_fmt = vsp1_subdev_get_pad_format,
75 	.set_fmt = lif_set_format,
76 };
77 
78 static const struct v4l2_subdev_ops lif_ops = {
79 	.pad    = &lif_pad_ops,
80 };
81 
82 /* -----------------------------------------------------------------------------
83  * VSP1 Entity Operations
84  */
85 
lif_configure_stream(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_list * dl,struct vsp1_dl_body * dlb)86 static void lif_configure_stream(struct vsp1_entity *entity,
87 				 struct vsp1_pipeline *pipe,
88 				 struct vsp1_dl_list *dl,
89 				 struct vsp1_dl_body *dlb)
90 {
91 	const struct v4l2_mbus_framefmt *format;
92 	struct vsp1_lif *lif = to_lif(&entity->subdev);
93 	unsigned int hbth;
94 	unsigned int obth;
95 	unsigned int lbth;
96 
97 	format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
98 					    LIF_PAD_SOURCE);
99 
100 	switch (entity->vsp1->version & VI6_IP_VERSION_MODEL_MASK) {
101 	case VI6_IP_VERSION_MODEL_VSPD_GEN2:
102 	case VI6_IP_VERSION_MODEL_VSPD_V2H:
103 		hbth = 1536;
104 		obth = min(128U, (format->width + 1) / 2 * format->height - 4);
105 		lbth = 1520;
106 		break;
107 
108 	case VI6_IP_VERSION_MODEL_VSPDL_GEN3:
109 	case VI6_IP_VERSION_MODEL_VSPD_V3:
110 	case VI6_IP_VERSION_MODEL_VSPD_RZG2L:
111 		hbth = 0;
112 		obth = 1500;
113 		lbth = 0;
114 		break;
115 
116 	case VI6_IP_VERSION_MODEL_VSPD_GEN3:
117 	case VI6_IP_VERSION_MODEL_VSPD_GEN4:
118 	default:
119 		hbth = 0;
120 		obth = 3000;
121 		lbth = 0;
122 		break;
123 	}
124 
125 	vsp1_lif_write(lif, dlb, VI6_LIF_CSBTH,
126 			(hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
127 			(lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
128 
129 	vsp1_lif_write(lif, dlb, VI6_LIF_CTRL,
130 			(obth << VI6_LIF_CTRL_OBTH_SHIFT) |
131 			(format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
132 			VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
133 
134 	/*
135 	 * On R-Car V3M and RZ/G2L the LIF0 buffer attribute register has to be
136 	 * set to a non-default value to guarantee proper operation (otherwise
137 	 * artifacts may appear on the output). The value required by the
138 	 * manual is not explained but is likely a buffer size or threshold.
139 	 */
140 	if (vsp1_feature(entity->vsp1, VSP1_HAS_NON_ZERO_LBA))
141 		vsp1_lif_write(lif, dlb, VI6_LIF_LBA,
142 			       VI6_LIF_LBA_LBA0 |
143 			       (1536 << VI6_LIF_LBA_LBA1_SHIFT));
144 }
145 
146 static const struct vsp1_entity_operations lif_entity_ops = {
147 	.configure_stream = lif_configure_stream,
148 };
149 
150 /* -----------------------------------------------------------------------------
151  * Initialization and Cleanup
152  */
153 
vsp1_lif_create(struct vsp1_device * vsp1,unsigned int index)154 struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1, unsigned int index)
155 {
156 	struct vsp1_lif *lif;
157 	int ret;
158 
159 	lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
160 	if (lif == NULL)
161 		return ERR_PTR(-ENOMEM);
162 
163 	lif->entity.ops = &lif_entity_ops;
164 	lif->entity.type = VSP1_ENTITY_LIF;
165 	lif->entity.index = index;
166 
167 	/*
168 	 * The LIF is never exposed to userspace, but media entity registration
169 	 * requires a function to be set. Use PROC_VIDEO_PIXEL_FORMATTER just to
170 	 * avoid triggering a WARN_ON(), the value won't be seen anywhere.
171 	 */
172 	ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops,
173 			       MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
174 	if (ret < 0)
175 		return ERR_PTR(ret);
176 
177 	return lif;
178 }
179