root/drivers/media/platform/vimc/vimc-scaler.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. vimc_sca_init_cfg
  2. vimc_sca_enum_mbus_code
  3. vimc_sca_enum_frame_size
  4. vimc_sca_get_fmt
  5. vimc_sca_adjust_sink_fmt
  6. vimc_sca_set_fmt
  7. vimc_sca_s_stream
  8. vimc_sca_fill_pix
  9. vimc_sca_scale_pix
  10. vimc_sca_fill_src_frame
  11. vimc_sca_process_frame
  12. vimc_sca_release
  13. vimc_sca_comp_unbind
  14. vimc_sca_comp_bind
  15. vimc_sca_probe
  16. vimc_sca_remove

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  * vimc-scaler.c Virtual Media Controller Driver
   4  *
   5  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
   6  */
   7 
   8 #include <linux/component.h>
   9 #include <linux/module.h>
  10 #include <linux/mod_devicetable.h>
  11 #include <linux/platform_device.h>
  12 #include <linux/vmalloc.h>
  13 #include <linux/v4l2-mediabus.h>
  14 #include <media/v4l2-subdev.h>
  15 
  16 #include "vimc-common.h"
  17 
  18 #define VIMC_SCA_DRV_NAME "vimc-scaler"
  19 
  20 static unsigned int sca_mult = 3;
  21 module_param(sca_mult, uint, 0000);
  22 MODULE_PARM_DESC(sca_mult, " the image size multiplier");
  23 
  24 #define IS_SINK(pad)    (!pad)
  25 #define IS_SRC(pad)     (pad)
  26 #define MAX_ZOOM        8
  27 
  28 struct vimc_sca_device {
  29         struct vimc_ent_device ved;
  30         struct v4l2_subdev sd;
  31         struct device *dev;
  32         /* NOTE: the source fmt is the same as the sink
  33          * with the width and hight multiplied by mult
  34          */
  35         struct v4l2_mbus_framefmt sink_fmt;
  36         /* Values calculated when the stream starts */
  37         u8 *src_frame;
  38         unsigned int src_line_size;
  39         unsigned int bpp;
  40 };
  41 
  42 static const struct v4l2_mbus_framefmt sink_fmt_default = {
  43         .width = 640,
  44         .height = 480,
  45         .code = MEDIA_BUS_FMT_RGB888_1X24,
  46         .field = V4L2_FIELD_NONE,
  47         .colorspace = V4L2_COLORSPACE_DEFAULT,
  48 };
  49 
  50 static int vimc_sca_init_cfg(struct v4l2_subdev *sd,
  51                              struct v4l2_subdev_pad_config *cfg)
  52 {
  53         struct v4l2_mbus_framefmt *mf;
  54         unsigned int i;
  55 
  56         mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  57         *mf = sink_fmt_default;
  58 
  59         for (i = 1; i < sd->entity.num_pads; i++) {
  60                 mf = v4l2_subdev_get_try_format(sd, cfg, i);
  61                 *mf = sink_fmt_default;
  62                 mf->width = mf->width * sca_mult;
  63                 mf->height = mf->height * sca_mult;
  64         }
  65 
  66         return 0;
  67 }
  68 
  69 static int vimc_sca_enum_mbus_code(struct v4l2_subdev *sd,
  70                                    struct v4l2_subdev_pad_config *cfg,
  71                                    struct v4l2_subdev_mbus_code_enum *code)
  72 {
  73         const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
  74 
  75         /* We don't support bayer format */
  76         if (!vpix || vpix->bayer)
  77                 return -EINVAL;
  78 
  79         code->code = vpix->code;
  80 
  81         return 0;
  82 }
  83 
  84 static int vimc_sca_enum_frame_size(struct v4l2_subdev *sd,
  85                                     struct v4l2_subdev_pad_config *cfg,
  86                                     struct v4l2_subdev_frame_size_enum *fse)
  87 {
  88         const struct vimc_pix_map *vpix;
  89 
  90         if (fse->index)
  91                 return -EINVAL;
  92 
  93         /* Only accept code in the pix map table in non bayer format */
  94         vpix = vimc_pix_map_by_code(fse->code);
  95         if (!vpix || vpix->bayer)
  96                 return -EINVAL;
  97 
  98         fse->min_width = VIMC_FRAME_MIN_WIDTH;
  99         fse->min_height = VIMC_FRAME_MIN_HEIGHT;
 100 
 101         if (IS_SINK(fse->pad)) {
 102                 fse->max_width = VIMC_FRAME_MAX_WIDTH;
 103                 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
 104         } else {
 105                 fse->max_width = VIMC_FRAME_MAX_WIDTH * MAX_ZOOM;
 106                 fse->max_height = VIMC_FRAME_MAX_HEIGHT * MAX_ZOOM;
 107         }
 108 
 109         return 0;
 110 }
 111 
 112 static int vimc_sca_get_fmt(struct v4l2_subdev *sd,
 113                             struct v4l2_subdev_pad_config *cfg,
 114                             struct v4l2_subdev_format *format)
 115 {
 116         struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
 117 
 118         /* Get the current sink format */
 119         format->format = (format->which == V4L2_SUBDEV_FORMAT_TRY) ?
 120                          *v4l2_subdev_get_try_format(sd, cfg, 0) :
 121                          vsca->sink_fmt;
 122 
 123         /* Scale the frame size for the source pad */
 124         if (IS_SRC(format->pad)) {
 125                 format->format.width = vsca->sink_fmt.width * sca_mult;
 126                 format->format.height = vsca->sink_fmt.height * sca_mult;
 127         }
 128 
 129         return 0;
 130 }
 131 
 132 static void vimc_sca_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
 133 {
 134         const struct vimc_pix_map *vpix;
 135 
 136         /* Only accept code in the pix map table in non bayer format */
 137         vpix = vimc_pix_map_by_code(fmt->code);
 138         if (!vpix || vpix->bayer)
 139                 fmt->code = sink_fmt_default.code;
 140 
 141         fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
 142                              VIMC_FRAME_MAX_WIDTH) & ~1;
 143         fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
 144                               VIMC_FRAME_MAX_HEIGHT) & ~1;
 145 
 146         if (fmt->field == V4L2_FIELD_ANY)
 147                 fmt->field = sink_fmt_default.field;
 148 
 149         vimc_colorimetry_clamp(fmt);
 150 }
 151 
 152 static int vimc_sca_set_fmt(struct v4l2_subdev *sd,
 153                             struct v4l2_subdev_pad_config *cfg,
 154                             struct v4l2_subdev_format *fmt)
 155 {
 156         struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
 157         struct v4l2_mbus_framefmt *sink_fmt;
 158 
 159         if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
 160                 /* Do not change the format while stream is on */
 161                 if (vsca->src_frame)
 162                         return -EBUSY;
 163 
 164                 sink_fmt = &vsca->sink_fmt;
 165         } else {
 166                 sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
 167         }
 168 
 169         /*
 170          * Do not change the format of the source pad,
 171          * it is propagated from the sink
 172          */
 173         if (IS_SRC(fmt->pad)) {
 174                 fmt->format = *sink_fmt;
 175                 fmt->format.width = sink_fmt->width * sca_mult;
 176                 fmt->format.height = sink_fmt->height * sca_mult;
 177         } else {
 178                 /* Set the new format in the sink pad */
 179                 vimc_sca_adjust_sink_fmt(&fmt->format);
 180 
 181                 dev_dbg(vsca->dev, "%s: sink format update: "
 182                         "old:%dx%d (0x%x, %d, %d, %d, %d) "
 183                         "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsca->sd.name,
 184                         /* old */
 185                         sink_fmt->width, sink_fmt->height, sink_fmt->code,
 186                         sink_fmt->colorspace, sink_fmt->quantization,
 187                         sink_fmt->xfer_func, sink_fmt->ycbcr_enc,
 188                         /* new */
 189                         fmt->format.width, fmt->format.height, fmt->format.code,
 190                         fmt->format.colorspace, fmt->format.quantization,
 191                         fmt->format.xfer_func, fmt->format.ycbcr_enc);
 192 
 193                 *sink_fmt = fmt->format;
 194         }
 195 
 196         return 0;
 197 }
 198 
 199 static const struct v4l2_subdev_pad_ops vimc_sca_pad_ops = {
 200         .init_cfg               = vimc_sca_init_cfg,
 201         .enum_mbus_code         = vimc_sca_enum_mbus_code,
 202         .enum_frame_size        = vimc_sca_enum_frame_size,
 203         .get_fmt                = vimc_sca_get_fmt,
 204         .set_fmt                = vimc_sca_set_fmt,
 205 };
 206 
 207 static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
 208 {
 209         struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
 210 
 211         if (enable) {
 212                 const struct vimc_pix_map *vpix;
 213                 unsigned int frame_size;
 214 
 215                 if (vsca->src_frame)
 216                         return 0;
 217 
 218                 /* Save the bytes per pixel of the sink */
 219                 vpix = vimc_pix_map_by_code(vsca->sink_fmt.code);
 220                 vsca->bpp = vpix->bpp;
 221 
 222                 /* Calculate the width in bytes of the src frame */
 223                 vsca->src_line_size = vsca->sink_fmt.width *
 224                                       sca_mult * vsca->bpp;
 225 
 226                 /* Calculate the frame size of the source pad */
 227                 frame_size = vsca->src_line_size * vsca->sink_fmt.height *
 228                              sca_mult;
 229 
 230                 /* Allocate the frame buffer. Use vmalloc to be able to
 231                  * allocate a large amount of memory
 232                  */
 233                 vsca->src_frame = vmalloc(frame_size);
 234                 if (!vsca->src_frame)
 235                         return -ENOMEM;
 236 
 237         } else {
 238                 if (!vsca->src_frame)
 239                         return 0;
 240 
 241                 vfree(vsca->src_frame);
 242                 vsca->src_frame = NULL;
 243         }
 244 
 245         return 0;
 246 }
 247 
 248 static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
 249         .s_stream = vimc_sca_s_stream,
 250 };
 251 
 252 static const struct v4l2_subdev_ops vimc_sca_ops = {
 253         .pad = &vimc_sca_pad_ops,
 254         .video = &vimc_sca_video_ops,
 255 };
 256 
 257 static void vimc_sca_fill_pix(u8 *const ptr,
 258                               const u8 *const pixel,
 259                               const unsigned int bpp)
 260 {
 261         unsigned int i;
 262 
 263         /* copy the pixel to the pointer */
 264         for (i = 0; i < bpp; i++)
 265                 ptr[i] = pixel[i];
 266 }
 267 
 268 static void vimc_sca_scale_pix(const struct vimc_sca_device *const vsca,
 269                                const unsigned int lin, const unsigned int col,
 270                                const u8 *const sink_frame)
 271 {
 272         unsigned int i, j, index;
 273         const u8 *pixel;
 274 
 275         /* Point to the pixel value in position (lin, col) in the sink frame */
 276         index = VIMC_FRAME_INDEX(lin, col,
 277                                  vsca->sink_fmt.width,
 278                                  vsca->bpp);
 279         pixel = &sink_frame[index];
 280 
 281         dev_dbg(vsca->dev,
 282                 "sca: %s: --- scale_pix sink pos %dx%d, index %d ---\n",
 283                 vsca->sd.name, lin, col, index);
 284 
 285         /* point to the place we are going to put the first pixel
 286          * in the scaled src frame
 287          */
 288         index = VIMC_FRAME_INDEX(lin * sca_mult, col * sca_mult,
 289                                  vsca->sink_fmt.width * sca_mult, vsca->bpp);
 290 
 291         dev_dbg(vsca->dev, "sca: %s: scale_pix src pos %dx%d, index %d\n",
 292                 vsca->sd.name, lin * sca_mult, col * sca_mult, index);
 293 
 294         /* Repeat this pixel mult times */
 295         for (i = 0; i < sca_mult; i++) {
 296                 /* Iterate through each beginning of a
 297                  * pixel repetition in a line
 298                  */
 299                 for (j = 0; j < sca_mult * vsca->bpp; j += vsca->bpp) {
 300                         dev_dbg(vsca->dev,
 301                                 "sca: %s: sca: scale_pix src pos %d\n",
 302                                 vsca->sd.name, index + j);
 303 
 304                         /* copy the pixel to the position index + j */
 305                         vimc_sca_fill_pix(&vsca->src_frame[index + j],
 306                                           pixel, vsca->bpp);
 307                 }
 308 
 309                 /* move the index to the next line */
 310                 index += vsca->src_line_size;
 311         }
 312 }
 313 
 314 static void vimc_sca_fill_src_frame(const struct vimc_sca_device *const vsca,
 315                                     const u8 *const sink_frame)
 316 {
 317         unsigned int i, j;
 318 
 319         /* Scale each pixel from the original sink frame */
 320         /* TODO: implement scale down, only scale up is supported for now */
 321         for (i = 0; i < vsca->sink_fmt.height; i++)
 322                 for (j = 0; j < vsca->sink_fmt.width; j++)
 323                         vimc_sca_scale_pix(vsca, i, j, sink_frame);
 324 }
 325 
 326 static void *vimc_sca_process_frame(struct vimc_ent_device *ved,
 327                                     const void *sink_frame)
 328 {
 329         struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
 330                                                     ved);
 331 
 332         /* If the stream in this node is not active, just return */
 333         if (!vsca->src_frame)
 334                 return ERR_PTR(-EINVAL);
 335 
 336         vimc_sca_fill_src_frame(vsca, sink_frame);
 337 
 338         return vsca->src_frame;
 339 };
 340 
 341 static void vimc_sca_release(struct v4l2_subdev *sd)
 342 {
 343         struct vimc_sca_device *vsca =
 344                                 container_of(sd, struct vimc_sca_device, sd);
 345 
 346         vimc_pads_cleanup(vsca->ved.pads);
 347         kfree(vsca);
 348 }
 349 
 350 static const struct v4l2_subdev_internal_ops vimc_sca_int_ops = {
 351         .release = vimc_sca_release,
 352 };
 353 
 354 static void vimc_sca_comp_unbind(struct device *comp, struct device *master,
 355                                  void *master_data)
 356 {
 357         struct vimc_ent_device *ved = dev_get_drvdata(comp);
 358         struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
 359                                                     ved);
 360 
 361         vimc_ent_sd_unregister(ved, &vsca->sd);
 362 }
 363 
 364 
 365 static int vimc_sca_comp_bind(struct device *comp, struct device *master,
 366                               void *master_data)
 367 {
 368         struct v4l2_device *v4l2_dev = master_data;
 369         struct vimc_platform_data *pdata = comp->platform_data;
 370         struct vimc_sca_device *vsca;
 371         int ret;
 372 
 373         /* Allocate the vsca struct */
 374         vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
 375         if (!vsca)
 376                 return -ENOMEM;
 377 
 378         /* Initialize ved and sd */
 379         ret = vimc_ent_sd_register(&vsca->ved, &vsca->sd, v4l2_dev,
 380                                    pdata->entity_name,
 381                                    MEDIA_ENT_F_PROC_VIDEO_SCALER, 2,
 382                                    (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
 383                                    MEDIA_PAD_FL_SOURCE},
 384                                    &vimc_sca_int_ops, &vimc_sca_ops);
 385         if (ret) {
 386                 kfree(vsca);
 387                 return ret;
 388         }
 389 
 390         vsca->ved.process_frame = vimc_sca_process_frame;
 391         dev_set_drvdata(comp, &vsca->ved);
 392         vsca->dev = comp;
 393 
 394         /* Initialize the frame format */
 395         vsca->sink_fmt = sink_fmt_default;
 396 
 397         return 0;
 398 }
 399 
 400 static const struct component_ops vimc_sca_comp_ops = {
 401         .bind = vimc_sca_comp_bind,
 402         .unbind = vimc_sca_comp_unbind,
 403 };
 404 
 405 static int vimc_sca_probe(struct platform_device *pdev)
 406 {
 407         return component_add(&pdev->dev, &vimc_sca_comp_ops);
 408 }
 409 
 410 static int vimc_sca_remove(struct platform_device *pdev)
 411 {
 412         component_del(&pdev->dev, &vimc_sca_comp_ops);
 413 
 414         return 0;
 415 }
 416 
 417 static const struct platform_device_id vimc_sca_driver_ids[] = {
 418         {
 419                 .name           = VIMC_SCA_DRV_NAME,
 420         },
 421         { }
 422 };
 423 
 424 static struct platform_driver vimc_sca_pdrv = {
 425         .probe          = vimc_sca_probe,
 426         .remove         = vimc_sca_remove,
 427         .id_table       = vimc_sca_driver_ids,
 428         .driver         = {
 429                 .name   = VIMC_SCA_DRV_NAME,
 430         },
 431 };
 432 
 433 module_platform_driver(vimc_sca_pdrv);
 434 
 435 MODULE_DEVICE_TABLE(platform, vimc_sca_driver_ids);
 436 
 437 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Scaler");
 438 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
 439 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */