Laurent Pinchart | 1c4b5f4 | 2018-04-22 17:33:20 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 2 | /* |
| 3 | * vsp1_clu.c -- R-Car VSP1 Cubic Look-Up Table |
| 4 | * |
| 5 | * Copyright (C) 2015-2016 Renesas Electronics Corporation |
| 6 | * |
| 7 | * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/slab.h> |
| 12 | |
| 13 | #include <media/v4l2-subdev.h> |
| 14 | |
| 15 | #include "vsp1.h" |
| 16 | #include "vsp1_clu.h" |
| 17 | #include "vsp1_dl.h" |
| 18 | |
| 19 | #define CLU_MIN_SIZE 4U |
| 20 | #define CLU_MAX_SIZE 8190U |
| 21 | |
Kieran Bingham | 5d7936b | 2018-05-18 16:41:59 -0400 | [diff] [blame] | 22 | #define CLU_SIZE (17 * 17 * 17) |
| 23 | |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 24 | /* ----------------------------------------------------------------------------- |
| 25 | * Device Access |
| 26 | */ |
| 27 | |
Kieran Bingham | 12832dd | 2018-05-18 16:42:02 -0400 | [diff] [blame] | 28 | static inline void vsp1_clu_write(struct vsp1_clu *clu, |
| 29 | struct vsp1_dl_body *dlb, u32 reg, u32 data) |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 30 | { |
Kieran Bingham | 12832dd | 2018-05-18 16:42:02 -0400 | [diff] [blame] | 31 | vsp1_dl_body_write(dlb, reg, data); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* ----------------------------------------------------------------------------- |
| 35 | * Controls |
| 36 | */ |
| 37 | |
| 38 | #define V4L2_CID_VSP1_CLU_TABLE (V4L2_CID_USER_BASE | 0x1001) |
| 39 | #define V4L2_CID_VSP1_CLU_MODE (V4L2_CID_USER_BASE | 0x1002) |
| 40 | #define V4L2_CID_VSP1_CLU_MODE_2D 0 |
| 41 | #define V4L2_CID_VSP1_CLU_MODE_3D 1 |
| 42 | |
| 43 | static int clu_set_table(struct vsp1_clu *clu, struct v4l2_ctrl *ctrl) |
| 44 | { |
| 45 | struct vsp1_dl_body *dlb; |
| 46 | unsigned int i; |
| 47 | |
Kieran Bingham | 5d7936b | 2018-05-18 16:41:59 -0400 | [diff] [blame] | 48 | dlb = vsp1_dl_body_get(clu->pool); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 49 | if (!dlb) |
| 50 | return -ENOMEM; |
| 51 | |
Kieran Bingham | 764dfee | 2018-05-18 16:41:56 -0400 | [diff] [blame] | 52 | vsp1_dl_body_write(dlb, VI6_CLU_ADDR, 0); |
Kieran Bingham | 5d7936b | 2018-05-18 16:41:59 -0400 | [diff] [blame] | 53 | for (i = 0; i < CLU_SIZE; ++i) |
Kieran Bingham | 764dfee | 2018-05-18 16:41:56 -0400 | [diff] [blame] | 54 | vsp1_dl_body_write(dlb, VI6_CLU_DATA, ctrl->p_new.p_u32[i]); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 55 | |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 56 | spin_lock_irq(&clu->lock); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 57 | swap(clu->clu, dlb); |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 58 | spin_unlock_irq(&clu->lock); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 59 | |
Kieran Bingham | 5d7936b | 2018-05-18 16:41:59 -0400 | [diff] [blame] | 60 | vsp1_dl_body_put(dlb); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static int clu_s_ctrl(struct v4l2_ctrl *ctrl) |
| 65 | { |
| 66 | struct vsp1_clu *clu = |
| 67 | container_of(ctrl->handler, struct vsp1_clu, ctrls); |
| 68 | |
| 69 | switch (ctrl->id) { |
| 70 | case V4L2_CID_VSP1_CLU_TABLE: |
| 71 | clu_set_table(clu, ctrl); |
| 72 | break; |
| 73 | |
| 74 | case V4L2_CID_VSP1_CLU_MODE: |
| 75 | clu->mode = ctrl->val; |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static const struct v4l2_ctrl_ops clu_ctrl_ops = { |
| 83 | .s_ctrl = clu_s_ctrl, |
| 84 | }; |
| 85 | |
| 86 | static const struct v4l2_ctrl_config clu_table_control = { |
| 87 | .ops = &clu_ctrl_ops, |
| 88 | .id = V4L2_CID_VSP1_CLU_TABLE, |
| 89 | .name = "Look-Up Table", |
| 90 | .type = V4L2_CTRL_TYPE_U32, |
| 91 | .min = 0x00000000, |
| 92 | .max = 0x00ffffff, |
| 93 | .step = 1, |
| 94 | .def = 0, |
| 95 | .dims = { 17, 17, 17 }, |
| 96 | }; |
| 97 | |
| 98 | static const char * const clu_mode_menu[] = { |
| 99 | "2D", |
| 100 | "3D", |
| 101 | NULL, |
| 102 | }; |
| 103 | |
| 104 | static const struct v4l2_ctrl_config clu_mode_control = { |
| 105 | .ops = &clu_ctrl_ops, |
| 106 | .id = V4L2_CID_VSP1_CLU_MODE, |
| 107 | .name = "Mode", |
| 108 | .type = V4L2_CTRL_TYPE_MENU, |
| 109 | .min = 0, |
| 110 | .max = 1, |
| 111 | .def = 1, |
| 112 | .qmenu = clu_mode_menu, |
| 113 | }; |
| 114 | |
| 115 | /* ----------------------------------------------------------------------------- |
| 116 | * V4L2 Subdevice Pad Operations |
| 117 | */ |
| 118 | |
Laurent Pinchart | b4ccae1 | 2017-11-27 14:59:45 -0500 | [diff] [blame] | 119 | static const unsigned int clu_codes[] = { |
| 120 | MEDIA_BUS_FMT_ARGB8888_1X32, |
| 121 | MEDIA_BUS_FMT_AHSV8888_1X32, |
| 122 | MEDIA_BUS_FMT_AYUV8_1X32, |
| 123 | }; |
| 124 | |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 125 | static int clu_enum_mbus_code(struct v4l2_subdev *subdev, |
| 126 | struct v4l2_subdev_pad_config *cfg, |
| 127 | struct v4l2_subdev_mbus_code_enum *code) |
| 128 | { |
Laurent Pinchart | b4ccae1 | 2017-11-27 14:59:45 -0500 | [diff] [blame] | 129 | return vsp1_subdev_enum_mbus_code(subdev, cfg, code, clu_codes, |
| 130 | ARRAY_SIZE(clu_codes)); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static int clu_enum_frame_size(struct v4l2_subdev *subdev, |
| 134 | struct v4l2_subdev_pad_config *cfg, |
| 135 | struct v4l2_subdev_frame_size_enum *fse) |
| 136 | { |
| 137 | return vsp1_subdev_enum_frame_size(subdev, cfg, fse, CLU_MIN_SIZE, |
| 138 | CLU_MIN_SIZE, CLU_MAX_SIZE, |
| 139 | CLU_MAX_SIZE); |
| 140 | } |
| 141 | |
| 142 | static int clu_set_format(struct v4l2_subdev *subdev, |
| 143 | struct v4l2_subdev_pad_config *cfg, |
| 144 | struct v4l2_subdev_format *fmt) |
| 145 | { |
Laurent Pinchart | b4ccae1 | 2017-11-27 14:59:45 -0500 | [diff] [blame] | 146 | return vsp1_subdev_set_pad_format(subdev, cfg, fmt, clu_codes, |
| 147 | ARRAY_SIZE(clu_codes), |
| 148 | CLU_MIN_SIZE, CLU_MIN_SIZE, |
| 149 | CLU_MAX_SIZE, CLU_MAX_SIZE); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* ----------------------------------------------------------------------------- |
| 153 | * V4L2 Subdevice Operations |
| 154 | */ |
| 155 | |
| 156 | static const struct v4l2_subdev_pad_ops clu_pad_ops = { |
| 157 | .init_cfg = vsp1_entity_init_cfg, |
| 158 | .enum_mbus_code = clu_enum_mbus_code, |
| 159 | .enum_frame_size = clu_enum_frame_size, |
| 160 | .get_fmt = vsp1_subdev_get_pad_format, |
| 161 | .set_fmt = clu_set_format, |
| 162 | }; |
| 163 | |
| 164 | static const struct v4l2_subdev_ops clu_ops = { |
| 165 | .pad = &clu_pad_ops, |
| 166 | }; |
| 167 | |
| 168 | /* ----------------------------------------------------------------------------- |
| 169 | * VSP1 Entity Operations |
| 170 | */ |
| 171 | |
Kieran Bingham | 46ce363 | 2018-05-18 16:42:01 -0400 | [diff] [blame] | 172 | static void clu_configure_stream(struct vsp1_entity *entity, |
| 173 | struct vsp1_pipeline *pipe, |
Laurent Pinchart | b36c604 | 2019-03-11 20:13:43 +0200 | [diff] [blame] | 174 | struct vsp1_dl_list *dl, |
Kieran Bingham | 12832dd | 2018-05-18 16:42:02 -0400 | [diff] [blame] | 175 | struct vsp1_dl_body *dlb) |
Kieran Bingham | 46ce363 | 2018-05-18 16:42:01 -0400 | [diff] [blame] | 176 | { |
| 177 | struct vsp1_clu *clu = to_clu(&entity->subdev); |
| 178 | struct v4l2_mbus_framefmt *format; |
| 179 | |
| 180 | /* |
| 181 | * The yuv_mode can't be changed during streaming. Cache it internally |
| 182 | * for future runtime configuration calls. |
| 183 | */ |
| 184 | format = vsp1_entity_get_pad_format(&clu->entity, |
| 185 | clu->entity.config, |
| 186 | CLU_PAD_SINK); |
| 187 | clu->yuv_mode = format->code == MEDIA_BUS_FMT_AYUV8_1X32; |
| 188 | } |
| 189 | |
| 190 | static void clu_configure_frame(struct vsp1_entity *entity, |
| 191 | struct vsp1_pipeline *pipe, |
Kieran Bingham | 12832dd | 2018-05-18 16:42:02 -0400 | [diff] [blame] | 192 | struct vsp1_dl_list *dl, |
| 193 | struct vsp1_dl_body *dlb) |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 194 | { |
| 195 | struct vsp1_clu *clu = to_clu(&entity->subdev); |
Kieran Bingham | 12832dd | 2018-05-18 16:42:02 -0400 | [diff] [blame] | 196 | struct vsp1_dl_body *clu_dlb; |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 197 | unsigned long flags; |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 198 | u32 ctrl = VI6_CLU_CTRL_AAI | VI6_CLU_CTRL_MVS | VI6_CLU_CTRL_EN; |
| 199 | |
Kieran Bingham | 46ce363 | 2018-05-18 16:42:01 -0400 | [diff] [blame] | 200 | /* 2D mode can only be used with the YCbCr pixel encoding. */ |
| 201 | if (clu->mode == V4L2_CID_VSP1_CLU_MODE_2D && clu->yuv_mode) |
| 202 | ctrl |= VI6_CLU_CTRL_AX1I_2D | VI6_CLU_CTRL_AX2I_2D |
| 203 | | VI6_CLU_CTRL_OS0_2D | VI6_CLU_CTRL_OS1_2D |
| 204 | | VI6_CLU_CTRL_OS2_2D | VI6_CLU_CTRL_M2D; |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 205 | |
Kieran Bingham | 12832dd | 2018-05-18 16:42:02 -0400 | [diff] [blame] | 206 | vsp1_clu_write(clu, dlb, VI6_CLU_CTRL, ctrl); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 207 | |
Kieran Bingham | 46ce363 | 2018-05-18 16:42:01 -0400 | [diff] [blame] | 208 | spin_lock_irqsave(&clu->lock, flags); |
Kieran Bingham | 12832dd | 2018-05-18 16:42:02 -0400 | [diff] [blame] | 209 | clu_dlb = clu->clu; |
Kieran Bingham | 46ce363 | 2018-05-18 16:42:01 -0400 | [diff] [blame] | 210 | clu->clu = NULL; |
| 211 | spin_unlock_irqrestore(&clu->lock, flags); |
Laurent Pinchart | 8ddf378 | 2016-09-12 09:50:13 -0300 | [diff] [blame] | 212 | |
Kieran Bingham | 12832dd | 2018-05-18 16:42:02 -0400 | [diff] [blame] | 213 | if (clu_dlb) { |
| 214 | vsp1_dl_list_add_body(dl, clu_dlb); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 215 | |
Kieran Bingham | 46ce363 | 2018-05-18 16:42:01 -0400 | [diff] [blame] | 216 | /* Release our local reference. */ |
Kieran Bingham | 12832dd | 2018-05-18 16:42:02 -0400 | [diff] [blame] | 217 | vsp1_dl_body_put(clu_dlb); |
Laurent Pinchart | d21fbbb | 2016-09-11 19:39:30 -0300 | [diff] [blame] | 218 | } |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 219 | } |
| 220 | |
Kieran Bingham | 5d7936b | 2018-05-18 16:41:59 -0400 | [diff] [blame] | 221 | static void clu_destroy(struct vsp1_entity *entity) |
| 222 | { |
| 223 | struct vsp1_clu *clu = to_clu(&entity->subdev); |
| 224 | |
| 225 | vsp1_dl_body_pool_destroy(clu->pool); |
| 226 | } |
| 227 | |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 228 | static const struct vsp1_entity_operations clu_entity_ops = { |
Kieran Bingham | 46ce363 | 2018-05-18 16:42:01 -0400 | [diff] [blame] | 229 | .configure_stream = clu_configure_stream, |
| 230 | .configure_frame = clu_configure_frame, |
Kieran Bingham | 5d7936b | 2018-05-18 16:41:59 -0400 | [diff] [blame] | 231 | .destroy = clu_destroy, |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | /* ----------------------------------------------------------------------------- |
| 235 | * Initialization and Cleanup |
| 236 | */ |
| 237 | |
| 238 | struct vsp1_clu *vsp1_clu_create(struct vsp1_device *vsp1) |
| 239 | { |
| 240 | struct vsp1_clu *clu; |
| 241 | int ret; |
| 242 | |
| 243 | clu = devm_kzalloc(vsp1->dev, sizeof(*clu), GFP_KERNEL); |
| 244 | if (clu == NULL) |
| 245 | return ERR_PTR(-ENOMEM); |
| 246 | |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 247 | spin_lock_init(&clu->lock); |
| 248 | |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 249 | clu->entity.ops = &clu_entity_ops; |
| 250 | clu->entity.type = VSP1_ENTITY_CLU; |
| 251 | |
| 252 | ret = vsp1_entity_init(vsp1, &clu->entity, "clu", 2, &clu_ops, |
| 253 | MEDIA_ENT_F_PROC_VIDEO_LUT); |
| 254 | if (ret < 0) |
| 255 | return ERR_PTR(ret); |
| 256 | |
Kieran Bingham | 5d7936b | 2018-05-18 16:41:59 -0400 | [diff] [blame] | 257 | /* |
| 258 | * Pre-allocate a body pool, with 3 bodies allowing a userspace update |
| 259 | * before the hardware has committed a previous set of tables, handling |
| 260 | * both the queued and pending dl entries. One extra entry is added to |
| 261 | * the CLU_SIZE to allow for the VI6_CLU_ADDR header. |
| 262 | */ |
| 263 | clu->pool = vsp1_dl_body_pool_create(clu->entity.vsp1, 3, CLU_SIZE + 1, |
| 264 | 0); |
| 265 | if (!clu->pool) |
| 266 | return ERR_PTR(-ENOMEM); |
| 267 | |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 268 | /* Initialize the control handler. */ |
| 269 | v4l2_ctrl_handler_init(&clu->ctrls, 2); |
| 270 | v4l2_ctrl_new_custom(&clu->ctrls, &clu_table_control, NULL); |
| 271 | v4l2_ctrl_new_custom(&clu->ctrls, &clu_mode_control, NULL); |
| 272 | |
| 273 | clu->entity.subdev.ctrl_handler = &clu->ctrls; |
| 274 | |
| 275 | if (clu->ctrls.error) { |
| 276 | dev_err(vsp1->dev, "clu: failed to initialize controls\n"); |
| 277 | ret = clu->ctrls.error; |
| 278 | vsp1_entity_destroy(&clu->entity); |
| 279 | return ERR_PTR(ret); |
| 280 | } |
| 281 | |
| 282 | v4l2_ctrl_handler_setup(&clu->ctrls); |
| 283 | |
| 284 | return clu; |
| 285 | } |