Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 1 | /* |
| 2 | * vsp1_clu.c -- R-Car VSP1 Cubic Look-Up Table |
| 3 | * |
| 4 | * Copyright (C) 2015-2016 Renesas Electronics Corporation |
| 5 | * |
| 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/slab.h> |
| 16 | |
| 17 | #include <media/v4l2-subdev.h> |
| 18 | |
| 19 | #include "vsp1.h" |
| 20 | #include "vsp1_clu.h" |
| 21 | #include "vsp1_dl.h" |
| 22 | |
| 23 | #define CLU_MIN_SIZE 4U |
| 24 | #define CLU_MAX_SIZE 8190U |
| 25 | |
| 26 | /* ----------------------------------------------------------------------------- |
| 27 | * Device Access |
| 28 | */ |
| 29 | |
| 30 | static inline void vsp1_clu_write(struct vsp1_clu *clu, struct vsp1_dl_list *dl, |
| 31 | u32 reg, u32 data) |
| 32 | { |
| 33 | vsp1_dl_list_write(dl, reg, data); |
| 34 | } |
| 35 | |
| 36 | /* ----------------------------------------------------------------------------- |
| 37 | * Controls |
| 38 | */ |
| 39 | |
| 40 | #define V4L2_CID_VSP1_CLU_TABLE (V4L2_CID_USER_BASE | 0x1001) |
| 41 | #define V4L2_CID_VSP1_CLU_MODE (V4L2_CID_USER_BASE | 0x1002) |
| 42 | #define V4L2_CID_VSP1_CLU_MODE_2D 0 |
| 43 | #define V4L2_CID_VSP1_CLU_MODE_3D 1 |
| 44 | |
| 45 | static int clu_set_table(struct vsp1_clu *clu, struct v4l2_ctrl *ctrl) |
| 46 | { |
| 47 | struct vsp1_dl_body *dlb; |
| 48 | unsigned int i; |
| 49 | |
| 50 | dlb = vsp1_dl_fragment_alloc(clu->entity.vsp1, 1 + 17 * 17 * 17); |
| 51 | if (!dlb) |
| 52 | return -ENOMEM; |
| 53 | |
| 54 | vsp1_dl_fragment_write(dlb, VI6_CLU_ADDR, 0); |
| 55 | for (i = 0; i < 17 * 17 * 17; ++i) |
| 56 | vsp1_dl_fragment_write(dlb, VI6_CLU_DATA, ctrl->p_new.p_u32[i]); |
| 57 | |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 58 | spin_lock_irq(&clu->lock); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 59 | swap(clu->clu, dlb); |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 60 | spin_unlock_irq(&clu->lock); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 61 | |
| 62 | vsp1_dl_fragment_free(dlb); |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int clu_s_ctrl(struct v4l2_ctrl *ctrl) |
| 67 | { |
| 68 | struct vsp1_clu *clu = |
| 69 | container_of(ctrl->handler, struct vsp1_clu, ctrls); |
| 70 | |
| 71 | switch (ctrl->id) { |
| 72 | case V4L2_CID_VSP1_CLU_TABLE: |
| 73 | clu_set_table(clu, ctrl); |
| 74 | break; |
| 75 | |
| 76 | case V4L2_CID_VSP1_CLU_MODE: |
| 77 | clu->mode = ctrl->val; |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static const struct v4l2_ctrl_ops clu_ctrl_ops = { |
| 85 | .s_ctrl = clu_s_ctrl, |
| 86 | }; |
| 87 | |
| 88 | static const struct v4l2_ctrl_config clu_table_control = { |
| 89 | .ops = &clu_ctrl_ops, |
| 90 | .id = V4L2_CID_VSP1_CLU_TABLE, |
| 91 | .name = "Look-Up Table", |
| 92 | .type = V4L2_CTRL_TYPE_U32, |
| 93 | .min = 0x00000000, |
| 94 | .max = 0x00ffffff, |
| 95 | .step = 1, |
| 96 | .def = 0, |
| 97 | .dims = { 17, 17, 17 }, |
| 98 | }; |
| 99 | |
| 100 | static const char * const clu_mode_menu[] = { |
| 101 | "2D", |
| 102 | "3D", |
| 103 | NULL, |
| 104 | }; |
| 105 | |
| 106 | static const struct v4l2_ctrl_config clu_mode_control = { |
| 107 | .ops = &clu_ctrl_ops, |
| 108 | .id = V4L2_CID_VSP1_CLU_MODE, |
| 109 | .name = "Mode", |
| 110 | .type = V4L2_CTRL_TYPE_MENU, |
| 111 | .min = 0, |
| 112 | .max = 1, |
| 113 | .def = 1, |
| 114 | .qmenu = clu_mode_menu, |
| 115 | }; |
| 116 | |
| 117 | /* ----------------------------------------------------------------------------- |
| 118 | * V4L2 Subdevice Pad Operations |
| 119 | */ |
| 120 | |
| 121 | static int clu_enum_mbus_code(struct v4l2_subdev *subdev, |
| 122 | struct v4l2_subdev_pad_config *cfg, |
| 123 | struct v4l2_subdev_mbus_code_enum *code) |
| 124 | { |
| 125 | static const unsigned int codes[] = { |
| 126 | MEDIA_BUS_FMT_ARGB8888_1X32, |
| 127 | MEDIA_BUS_FMT_AHSV8888_1X32, |
| 128 | MEDIA_BUS_FMT_AYUV8_1X32, |
| 129 | }; |
| 130 | |
| 131 | return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes, |
| 132 | ARRAY_SIZE(codes)); |
| 133 | } |
| 134 | |
| 135 | static int clu_enum_frame_size(struct v4l2_subdev *subdev, |
| 136 | struct v4l2_subdev_pad_config *cfg, |
| 137 | struct v4l2_subdev_frame_size_enum *fse) |
| 138 | { |
| 139 | return vsp1_subdev_enum_frame_size(subdev, cfg, fse, CLU_MIN_SIZE, |
| 140 | CLU_MIN_SIZE, CLU_MAX_SIZE, |
| 141 | CLU_MAX_SIZE); |
| 142 | } |
| 143 | |
| 144 | static int clu_set_format(struct v4l2_subdev *subdev, |
| 145 | struct v4l2_subdev_pad_config *cfg, |
| 146 | struct v4l2_subdev_format *fmt) |
| 147 | { |
| 148 | struct vsp1_clu *clu = to_clu(subdev); |
| 149 | struct v4l2_subdev_pad_config *config; |
| 150 | struct v4l2_mbus_framefmt *format; |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 151 | int ret = 0; |
| 152 | |
| 153 | mutex_lock(&clu->entity.lock); |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 154 | |
| 155 | config = vsp1_entity_get_pad_config(&clu->entity, cfg, fmt->which); |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 156 | if (!config) { |
| 157 | ret = -EINVAL; |
| 158 | goto done; |
| 159 | } |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 160 | |
| 161 | /* Default to YUV if the requested format is not supported. */ |
| 162 | if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 && |
| 163 | fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 && |
| 164 | fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32) |
| 165 | fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32; |
| 166 | |
| 167 | format = vsp1_entity_get_pad_format(&clu->entity, config, fmt->pad); |
| 168 | |
| 169 | if (fmt->pad == CLU_PAD_SOURCE) { |
| 170 | /* The CLU output format can't be modified. */ |
| 171 | fmt->format = *format; |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 172 | goto done; |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | format->code = fmt->format.code; |
| 176 | format->width = clamp_t(unsigned int, fmt->format.width, |
| 177 | CLU_MIN_SIZE, CLU_MAX_SIZE); |
| 178 | format->height = clamp_t(unsigned int, fmt->format.height, |
| 179 | CLU_MIN_SIZE, CLU_MAX_SIZE); |
| 180 | format->field = V4L2_FIELD_NONE; |
| 181 | format->colorspace = V4L2_COLORSPACE_SRGB; |
| 182 | |
| 183 | fmt->format = *format; |
| 184 | |
| 185 | /* Propagate the format to the source pad. */ |
| 186 | format = vsp1_entity_get_pad_format(&clu->entity, config, |
| 187 | CLU_PAD_SOURCE); |
| 188 | *format = fmt->format; |
| 189 | |
Laurent Pinchart | 34e77ed | 2016-06-26 08:09:31 -0300 | [diff] [blame] | 190 | done: |
| 191 | mutex_unlock(&clu->entity.lock); |
| 192 | return ret; |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* ----------------------------------------------------------------------------- |
| 196 | * V4L2 Subdevice Operations |
| 197 | */ |
| 198 | |
| 199 | static const struct v4l2_subdev_pad_ops clu_pad_ops = { |
| 200 | .init_cfg = vsp1_entity_init_cfg, |
| 201 | .enum_mbus_code = clu_enum_mbus_code, |
| 202 | .enum_frame_size = clu_enum_frame_size, |
| 203 | .get_fmt = vsp1_subdev_get_pad_format, |
| 204 | .set_fmt = clu_set_format, |
| 205 | }; |
| 206 | |
| 207 | static const struct v4l2_subdev_ops clu_ops = { |
| 208 | .pad = &clu_pad_ops, |
| 209 | }; |
| 210 | |
| 211 | /* ----------------------------------------------------------------------------- |
| 212 | * VSP1 Entity Operations |
| 213 | */ |
| 214 | |
| 215 | static void clu_configure(struct vsp1_entity *entity, |
| 216 | struct vsp1_pipeline *pipe, |
Laurent Pinchart | d21fbbb | 2016-09-11 19:39:30 -0300 | [diff] [blame] | 217 | struct vsp1_dl_list *dl, |
| 218 | enum vsp1_entity_params params) |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 219 | { |
| 220 | struct vsp1_clu *clu = to_clu(&entity->subdev); |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 221 | struct vsp1_dl_body *dlb; |
| 222 | unsigned long flags; |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 223 | u32 ctrl = VI6_CLU_CTRL_AAI | VI6_CLU_CTRL_MVS | VI6_CLU_CTRL_EN; |
| 224 | |
Laurent Pinchart | d21fbbb | 2016-09-11 19:39:30 -0300 | [diff] [blame] | 225 | switch (params) { |
| 226 | case VSP1_ENTITY_PARAMS_INIT: { |
Mauro Carvalho Chehab | b618739 | 2016-09-19 15:18:01 -0300 | [diff] [blame^] | 227 | /* |
| 228 | * The format can't be changed during streaming, only verify it |
Laurent Pinchart | d21fbbb | 2016-09-11 19:39:30 -0300 | [diff] [blame] | 229 | * at setup time and store the information internally for future |
| 230 | * runtime configuration calls. |
| 231 | */ |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 232 | struct v4l2_mbus_framefmt *format; |
| 233 | |
| 234 | format = vsp1_entity_get_pad_format(&clu->entity, |
| 235 | clu->entity.config, |
| 236 | CLU_PAD_SINK); |
| 237 | clu->yuv_mode = format->code == MEDIA_BUS_FMT_AYUV8_1X32; |
Laurent Pinchart | d21fbbb | 2016-09-11 19:39:30 -0300 | [diff] [blame] | 238 | break; |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 239 | } |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 240 | |
Laurent Pinchart | 8ddf378 | 2016-09-12 09:50:13 -0300 | [diff] [blame] | 241 | case VSP1_ENTITY_PARAMS_PARTITION: |
| 242 | break; |
| 243 | |
Laurent Pinchart | d21fbbb | 2016-09-11 19:39:30 -0300 | [diff] [blame] | 244 | case VSP1_ENTITY_PARAMS_RUNTIME: |
| 245 | /* 2D mode can only be used with the YCbCr pixel encoding. */ |
| 246 | if (clu->mode == V4L2_CID_VSP1_CLU_MODE_2D && clu->yuv_mode) |
| 247 | ctrl |= VI6_CLU_CTRL_AX1I_2D | VI6_CLU_CTRL_AX2I_2D |
| 248 | | VI6_CLU_CTRL_OS0_2D | VI6_CLU_CTRL_OS1_2D |
| 249 | | VI6_CLU_CTRL_OS2_2D | VI6_CLU_CTRL_M2D; |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 250 | |
Laurent Pinchart | d21fbbb | 2016-09-11 19:39:30 -0300 | [diff] [blame] | 251 | vsp1_clu_write(clu, dl, VI6_CLU_CTRL, ctrl); |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 252 | |
Laurent Pinchart | d21fbbb | 2016-09-11 19:39:30 -0300 | [diff] [blame] | 253 | spin_lock_irqsave(&clu->lock, flags); |
| 254 | dlb = clu->clu; |
| 255 | clu->clu = NULL; |
| 256 | spin_unlock_irqrestore(&clu->lock, flags); |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 257 | |
Laurent Pinchart | d21fbbb | 2016-09-11 19:39:30 -0300 | [diff] [blame] | 258 | if (dlb) |
| 259 | vsp1_dl_list_add_fragment(dl, dlb); |
| 260 | break; |
| 261 | } |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static const struct vsp1_entity_operations clu_entity_ops = { |
| 265 | .configure = clu_configure, |
| 266 | }; |
| 267 | |
| 268 | /* ----------------------------------------------------------------------------- |
| 269 | * Initialization and Cleanup |
| 270 | */ |
| 271 | |
| 272 | struct vsp1_clu *vsp1_clu_create(struct vsp1_device *vsp1) |
| 273 | { |
| 274 | struct vsp1_clu *clu; |
| 275 | int ret; |
| 276 | |
| 277 | clu = devm_kzalloc(vsp1->dev, sizeof(*clu), GFP_KERNEL); |
| 278 | if (clu == NULL) |
| 279 | return ERR_PTR(-ENOMEM); |
| 280 | |
Laurent Pinchart | 20fab5e | 2016-06-11 04:11:27 -0300 | [diff] [blame] | 281 | spin_lock_init(&clu->lock); |
| 282 | |
Laurent Pinchart | 1fd87bf | 2015-11-11 23:04:44 -0200 | [diff] [blame] | 283 | clu->entity.ops = &clu_entity_ops; |
| 284 | clu->entity.type = VSP1_ENTITY_CLU; |
| 285 | |
| 286 | ret = vsp1_entity_init(vsp1, &clu->entity, "clu", 2, &clu_ops, |
| 287 | MEDIA_ENT_F_PROC_VIDEO_LUT); |
| 288 | if (ret < 0) |
| 289 | return ERR_PTR(ret); |
| 290 | |
| 291 | /* Initialize the control handler. */ |
| 292 | v4l2_ctrl_handler_init(&clu->ctrls, 2); |
| 293 | v4l2_ctrl_new_custom(&clu->ctrls, &clu_table_control, NULL); |
| 294 | v4l2_ctrl_new_custom(&clu->ctrls, &clu_mode_control, NULL); |
| 295 | |
| 296 | clu->entity.subdev.ctrl_handler = &clu->ctrls; |
| 297 | |
| 298 | if (clu->ctrls.error) { |
| 299 | dev_err(vsp1->dev, "clu: failed to initialize controls\n"); |
| 300 | ret = clu->ctrls.error; |
| 301 | vsp1_entity_destroy(&clu->entity); |
| 302 | return ERR_PTR(ret); |
| 303 | } |
| 304 | |
| 305 | v4l2_ctrl_handler_setup(&clu->ctrls); |
| 306 | |
| 307 | return clu; |
| 308 | } |