Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 1 | /* |
| 2 | * vimc-sensor.c Virtual Media Controller Driver |
| 3 | * |
| 4 | * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/freezer.h> |
| 19 | #include <linux/kthread.h> |
| 20 | #include <linux/v4l2-mediabus.h> |
| 21 | #include <linux/vmalloc.h> |
| 22 | #include <media/v4l2-subdev.h> |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 23 | #include <media/v4l2-tpg.h> |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 24 | |
| 25 | #include "vimc-sensor.h" |
| 26 | |
| 27 | struct vimc_sen_device { |
| 28 | struct vimc_ent_device ved; |
| 29 | struct v4l2_subdev sd; |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 30 | struct tpg_data tpg; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 31 | struct task_struct *kthread_sen; |
| 32 | u8 *frame; |
| 33 | /* The active format */ |
| 34 | struct v4l2_mbus_framefmt mbus_format; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 35 | }; |
| 36 | |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 37 | static const struct v4l2_mbus_framefmt fmt_default = { |
| 38 | .width = 640, |
| 39 | .height = 480, |
| 40 | .code = MEDIA_BUS_FMT_RGB888_1X24, |
| 41 | .field = V4L2_FIELD_NONE, |
| 42 | .colorspace = V4L2_COLORSPACE_DEFAULT, |
| 43 | }; |
| 44 | |
| 45 | static int vimc_sen_init_cfg(struct v4l2_subdev *sd, |
| 46 | struct v4l2_subdev_pad_config *cfg) |
| 47 | { |
| 48 | unsigned int i; |
| 49 | |
| 50 | for (i = 0; i < sd->entity.num_pads; i++) { |
| 51 | struct v4l2_mbus_framefmt *mf; |
| 52 | |
| 53 | mf = v4l2_subdev_get_try_format(sd, cfg, i); |
| 54 | *mf = fmt_default; |
| 55 | } |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 60 | static int vimc_sen_enum_mbus_code(struct v4l2_subdev *sd, |
| 61 | struct v4l2_subdev_pad_config *cfg, |
| 62 | struct v4l2_subdev_mbus_code_enum *code) |
| 63 | { |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 64 | const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 65 | |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 66 | if (!vpix) |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 67 | return -EINVAL; |
| 68 | |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 69 | code->code = vpix->code; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int vimc_sen_enum_frame_size(struct v4l2_subdev *sd, |
| 75 | struct v4l2_subdev_pad_config *cfg, |
| 76 | struct v4l2_subdev_frame_size_enum *fse) |
| 77 | { |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 78 | const struct vimc_pix_map *vpix; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 79 | |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 80 | if (fse->index) |
| 81 | return -EINVAL; |
| 82 | |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 83 | /* Only accept code in the pix map table */ |
| 84 | vpix = vimc_pix_map_by_code(fse->code); |
| 85 | if (!vpix) |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 86 | return -EINVAL; |
| 87 | |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 88 | fse->min_width = VIMC_FRAME_MIN_WIDTH; |
| 89 | fse->max_width = VIMC_FRAME_MAX_WIDTH; |
| 90 | fse->min_height = VIMC_FRAME_MIN_HEIGHT; |
| 91 | fse->max_height = VIMC_FRAME_MAX_HEIGHT; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int vimc_sen_get_fmt(struct v4l2_subdev *sd, |
| 97 | struct v4l2_subdev_pad_config *cfg, |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 98 | struct v4l2_subdev_format *fmt) |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 99 | { |
| 100 | struct vimc_sen_device *vsen = |
| 101 | container_of(sd, struct vimc_sen_device, sd); |
| 102 | |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 103 | fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ? |
| 104 | *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) : |
| 105 | vsen->mbus_format; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 110 | static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen) |
| 111 | { |
| 112 | const struct vimc_pix_map *vpix = |
| 113 | vimc_pix_map_by_code(vsen->mbus_format.code); |
| 114 | |
| 115 | tpg_reset_source(&vsen->tpg, vsen->mbus_format.width, |
| 116 | vsen->mbus_format.height, vsen->mbus_format.field); |
| 117 | tpg_s_bytesperline(&vsen->tpg, 0, vsen->mbus_format.width * vpix->bpp); |
| 118 | tpg_s_buf_height(&vsen->tpg, vsen->mbus_format.height); |
| 119 | tpg_s_fourcc(&vsen->tpg, vpix->pixelformat); |
| 120 | /* TODO: add support for V4L2_FIELD_ALTERNATE */ |
| 121 | tpg_s_field(&vsen->tpg, vsen->mbus_format.field, false); |
| 122 | tpg_s_colorspace(&vsen->tpg, vsen->mbus_format.colorspace); |
| 123 | tpg_s_ycbcr_enc(&vsen->tpg, vsen->mbus_format.ycbcr_enc); |
| 124 | tpg_s_quantization(&vsen->tpg, vsen->mbus_format.quantization); |
| 125 | tpg_s_xfer_func(&vsen->tpg, vsen->mbus_format.xfer_func); |
| 126 | } |
| 127 | |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 128 | static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt) |
| 129 | { |
| 130 | const struct vimc_pix_map *vpix; |
| 131 | |
| 132 | /* Only accept code in the pix map table */ |
| 133 | vpix = vimc_pix_map_by_code(fmt->code); |
| 134 | if (!vpix) |
| 135 | fmt->code = fmt_default.code; |
| 136 | |
| 137 | fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH, |
| 138 | VIMC_FRAME_MAX_WIDTH) & ~1; |
| 139 | fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT, |
| 140 | VIMC_FRAME_MAX_HEIGHT) & ~1; |
| 141 | |
| 142 | /* TODO: add support for V4L2_FIELD_ALTERNATE */ |
| 143 | if (fmt->field == V4L2_FIELD_ANY || fmt->field == V4L2_FIELD_ALTERNATE) |
| 144 | fmt->field = fmt_default.field; |
| 145 | |
| 146 | vimc_colorimetry_clamp(fmt); |
| 147 | } |
| 148 | |
| 149 | static int vimc_sen_set_fmt(struct v4l2_subdev *sd, |
| 150 | struct v4l2_subdev_pad_config *cfg, |
| 151 | struct v4l2_subdev_format *fmt) |
| 152 | { |
| 153 | struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd); |
| 154 | struct v4l2_mbus_framefmt *mf; |
| 155 | |
| 156 | if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) { |
| 157 | /* Do not change the format while stream is on */ |
| 158 | if (vsen->frame) |
| 159 | return -EBUSY; |
| 160 | |
| 161 | mf = &vsen->mbus_format; |
| 162 | } else { |
| 163 | mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad); |
| 164 | } |
| 165 | |
| 166 | /* Set the new format */ |
| 167 | vimc_sen_adjust_fmt(&fmt->format); |
| 168 | |
| 169 | dev_dbg(vsen->sd.v4l2_dev->mdev->dev, "%s: format update: " |
| 170 | "old:%dx%d (0x%x, %d, %d, %d, %d) " |
| 171 | "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsen->sd.name, |
| 172 | /* old */ |
| 173 | mf->width, mf->height, mf->code, |
| 174 | mf->colorspace, mf->quantization, |
| 175 | mf->xfer_func, mf->ycbcr_enc, |
| 176 | /* new */ |
| 177 | fmt->format.width, fmt->format.height, fmt->format.code, |
| 178 | fmt->format.colorspace, fmt->format.quantization, |
| 179 | fmt->format.xfer_func, fmt->format.ycbcr_enc); |
| 180 | |
| 181 | *mf = fmt->format; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 186 | static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops = { |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 187 | .init_cfg = vimc_sen_init_cfg, |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 188 | .enum_mbus_code = vimc_sen_enum_mbus_code, |
| 189 | .enum_frame_size = vimc_sen_enum_frame_size, |
| 190 | .get_fmt = vimc_sen_get_fmt, |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 191 | .set_fmt = vimc_sen_set_fmt, |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 192 | }; |
| 193 | |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 194 | static int vimc_sen_tpg_thread(void *data) |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 195 | { |
| 196 | struct vimc_sen_device *vsen = data; |
| 197 | unsigned int i; |
| 198 | |
| 199 | set_freezable(); |
| 200 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 201 | |
| 202 | for (;;) { |
| 203 | try_to_freeze(); |
| 204 | if (kthread_should_stop()) |
| 205 | break; |
| 206 | |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 207 | tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 208 | |
| 209 | /* Send the frame to all source pads */ |
| 210 | for (i = 0; i < vsen->sd.entity.num_pads; i++) |
| 211 | vimc_propagate_frame(&vsen->sd.entity.pads[i], |
| 212 | vsen->frame); |
| 213 | |
| 214 | /* 60 frames per second */ |
| 215 | schedule_timeout(HZ/60); |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable) |
| 222 | { |
| 223 | struct vimc_sen_device *vsen = |
| 224 | container_of(sd, struct vimc_sen_device, sd); |
| 225 | int ret; |
| 226 | |
| 227 | if (enable) { |
| 228 | const struct vimc_pix_map *vpix; |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 229 | unsigned int frame_size; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 230 | |
| 231 | if (vsen->kthread_sen) |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 232 | /* tpg is already executing */ |
| 233 | return 0; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 234 | |
| 235 | /* Calculate the frame size */ |
| 236 | vpix = vimc_pix_map_by_code(vsen->mbus_format.code); |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 237 | frame_size = vsen->mbus_format.width * vpix->bpp * |
| 238 | vsen->mbus_format.height; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * Allocate the frame buffer. Use vmalloc to be able to |
| 242 | * allocate a large amount of memory |
| 243 | */ |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 244 | vsen->frame = vmalloc(frame_size); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 245 | if (!vsen->frame) |
| 246 | return -ENOMEM; |
| 247 | |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 248 | /* configure the test pattern generator */ |
| 249 | vimc_sen_tpg_s_format(vsen); |
| 250 | |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 251 | /* Initialize the image generator thread */ |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 252 | vsen->kthread_sen = kthread_run(vimc_sen_tpg_thread, vsen, |
| 253 | "%s-sen", vsen->sd.v4l2_dev->name); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 254 | if (IS_ERR(vsen->kthread_sen)) { |
| 255 | dev_err(vsen->sd.v4l2_dev->dev, |
| 256 | "%s: kernel_thread() failed\n", vsen->sd.name); |
| 257 | vfree(vsen->frame); |
| 258 | vsen->frame = NULL; |
| 259 | return PTR_ERR(vsen->kthread_sen); |
| 260 | } |
| 261 | } else { |
| 262 | if (!vsen->kthread_sen) |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 263 | return 0; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 264 | |
| 265 | /* Stop image generator */ |
| 266 | ret = kthread_stop(vsen->kthread_sen); |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 267 | if (ret) |
| 268 | return ret; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 269 | |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 270 | vsen->kthread_sen = NULL; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 271 | vfree(vsen->frame); |
| 272 | vsen->frame = NULL; |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 273 | return 0; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | struct v4l2_subdev_video_ops vimc_sen_video_ops = { |
| 280 | .s_stream = vimc_sen_s_stream, |
| 281 | }; |
| 282 | |
| 283 | static const struct v4l2_subdev_ops vimc_sen_ops = { |
| 284 | .pad = &vimc_sen_pad_ops, |
| 285 | .video = &vimc_sen_video_ops, |
| 286 | }; |
| 287 | |
| 288 | static void vimc_sen_destroy(struct vimc_ent_device *ved) |
| 289 | { |
| 290 | struct vimc_sen_device *vsen = |
| 291 | container_of(ved, struct vimc_sen_device, ved); |
| 292 | |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 293 | vimc_ent_sd_unregister(ved, &vsen->sd); |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 294 | tpg_free(&vsen->tpg); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 295 | kfree(vsen); |
| 296 | } |
| 297 | |
| 298 | struct vimc_ent_device *vimc_sen_create(struct v4l2_device *v4l2_dev, |
| 299 | const char *const name, |
| 300 | u16 num_pads, |
| 301 | const unsigned long *pads_flag) |
| 302 | { |
| 303 | struct vimc_sen_device *vsen; |
| 304 | unsigned int i; |
| 305 | int ret; |
| 306 | |
| 307 | /* NOTE: a sensor node may be created with more then one pad */ |
| 308 | if (!name || !num_pads || !pads_flag) |
| 309 | return ERR_PTR(-EINVAL); |
| 310 | |
| 311 | /* check if all pads are sources */ |
| 312 | for (i = 0; i < num_pads; i++) |
| 313 | if (!(pads_flag[i] & MEDIA_PAD_FL_SOURCE)) |
| 314 | return ERR_PTR(-EINVAL); |
| 315 | |
| 316 | /* Allocate the vsen struct */ |
| 317 | vsen = kzalloc(sizeof(*vsen), GFP_KERNEL); |
| 318 | if (!vsen) |
| 319 | return ERR_PTR(-ENOMEM); |
| 320 | |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 321 | /* Initialize ved and sd */ |
| 322 | ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev, name, |
| 323 | MEDIA_ENT_F_CAM_SENSOR, num_pads, pads_flag, |
| 324 | &vimc_sen_ops, vimc_sen_destroy); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 325 | if (ret) |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 326 | goto err_free_vsen; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 327 | |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 328 | /* Initialize the frame format */ |
| 329 | vsen->mbus_format = fmt_default; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 330 | |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 331 | /* Initialize the test pattern generator */ |
| 332 | tpg_init(&vsen->tpg, vsen->mbus_format.width, |
| 333 | vsen->mbus_format.height); |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 334 | ret = tpg_alloc(&vsen->tpg, VIMC_FRAME_MAX_WIDTH); |
Helen Fornazier | 554946c | 2017-06-19 14:00:10 -0300 | [diff] [blame] | 335 | if (ret) |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 336 | goto err_unregister_ent_sd; |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 337 | |
| 338 | return &vsen->ved; |
| 339 | |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 340 | err_unregister_ent_sd: |
| 341 | vimc_ent_sd_unregister(&vsen->ved, &vsen->sd); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 342 | err_free_vsen: |
| 343 | kfree(vsen); |
| 344 | |
| 345 | return ERR_PTR(ret); |
| 346 | } |