Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 2 | /* |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 3 | * vimc-common.h Virtual Media Controller Driver |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com> |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 6 | */ |
| 7 | |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 8 | #ifndef _VIMC_COMMON_H_ |
| 9 | #define _VIMC_COMMON_H_ |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 10 | |
| 11 | #include <linux/slab.h> |
Helen Fornazier | 5ba0ae4 | 2017-06-19 14:00:11 -0300 | [diff] [blame] | 12 | #include <media/media-device.h> |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 13 | #include <media/v4l2-device.h> |
| 14 | |
Helen Fornazier | b6c61a6 | 2019-03-13 14:29:37 -0400 | [diff] [blame] | 15 | #include "vimc-streamer.h" |
| 16 | |
Hans Verkuil | 04ee6d6 | 2019-01-14 10:27:45 -0200 | [diff] [blame] | 17 | #define VIMC_PDEV_NAME "vimc" |
| 18 | |
Hans Verkuil | 52cf1d9 | 2017-11-06 05:20:01 -0500 | [diff] [blame] | 19 | /* VIMC-specific controls */ |
| 20 | #define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000) |
| 21 | #define VIMC_CID_VIMC_CLASS (0x00f00000 | 1) |
| 22 | #define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0) |
| 23 | |
Helen Fornazier | 88ad71a | 2017-06-19 14:00:16 -0300 | [diff] [blame] | 24 | #define VIMC_FRAME_MAX_WIDTH 4096 |
| 25 | #define VIMC_FRAME_MAX_HEIGHT 2160 |
| 26 | #define VIMC_FRAME_MIN_WIDTH 16 |
| 27 | #define VIMC_FRAME_MIN_HEIGHT 16 |
| 28 | |
Helen Fornazier | 88f42bf | 2017-06-19 14:00:19 -0300 | [diff] [blame] | 29 | #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp) |
| 30 | |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 31 | /** |
Helen Fornazier | 441c0db | 2017-06-19 14:00:15 -0300 | [diff] [blame] | 32 | * struct vimc_colorimetry_clamp - Adjust colorimetry parameters |
| 33 | * |
| 34 | * @fmt: the pointer to struct v4l2_pix_format or |
| 35 | * struct v4l2_mbus_framefmt |
| 36 | * |
| 37 | * Entities must check if colorimetry given by the userspace is valid, if not |
| 38 | * then set them as DEFAULT |
| 39 | */ |
| 40 | #define vimc_colorimetry_clamp(fmt) \ |
| 41 | do { \ |
| 42 | if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \ |
| 43 | || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \ |
| 44 | (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \ |
| 45 | (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \ |
| 46 | (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \ |
| 47 | (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \ |
| 48 | } \ |
| 49 | if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \ |
| 50 | (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \ |
| 51 | if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \ |
| 52 | (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \ |
| 53 | if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \ |
| 54 | (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \ |
| 55 | } while (0) |
| 56 | |
| 57 | /** |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 58 | * struct vimc_platform_data - platform data to components |
| 59 | * |
| 60 | * @entity_name: The name of the entity to be created |
| 61 | * |
| 62 | * Board setup code will often provide additional information using the device's |
| 63 | * platform_data field to hold additional information. |
| 64 | * When injecting a new platform_device in the component system the core needs |
| 65 | * to provide to the corresponding submodules the name of the entity that should |
| 66 | * be used when registering the subdevice in the Media Controller system. |
| 67 | */ |
| 68 | struct vimc_platform_data { |
| 69 | char entity_name[32]; |
| 70 | }; |
| 71 | |
| 72 | /** |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 73 | * struct vimc_ent_device - core struct that represents a node in the topology |
| 74 | * |
| 75 | * @ent: the pointer to struct media_entity for the node |
| 76 | * @pads: the list of pads of the node |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 77 | * @process_frame: callback send a frame to that node |
Helen Fornazier | 288a22d | 2017-06-19 14:00:14 -0300 | [diff] [blame] | 78 | * @vdev_get_format: callback that returns the current format a pad, used |
| 79 | * only when is_media_entity_v4l2_video_device(ent) returns |
| 80 | * true |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 81 | * |
| 82 | * Each node of the topology must create a vimc_ent_device struct. Depending on |
| 83 | * the node it will be of an instance of v4l2_subdev or video_device struct |
| 84 | * where both contains a struct media_entity. |
| 85 | * Those structures should embedded the vimc_ent_device struct through |
| 86 | * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the |
| 87 | * vimc_ent_device struct to be retrieved from the corresponding struct |
| 88 | * media_entity |
| 89 | */ |
| 90 | struct vimc_ent_device { |
| 91 | struct media_entity *ent; |
| 92 | struct media_pad *pads; |
Helen Fornazier | b6c61a6 | 2019-03-13 14:29:37 -0400 | [diff] [blame] | 93 | struct vimc_stream *stream; |
Lucas A. M. Magalhães | adc589d | 2019-01-21 20:05:01 -0500 | [diff] [blame] | 94 | void * (*process_frame)(struct vimc_ent_device *ved, |
| 95 | const void *frame); |
Helen Fornazier | 288a22d | 2017-06-19 14:00:14 -0300 | [diff] [blame] | 96 | void (*vdev_get_format)(struct vimc_ent_device *ved, |
| 97 | struct v4l2_pix_format *fmt); |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | /** |
Helen Fornazier | b6c61a6 | 2019-03-13 14:29:37 -0400 | [diff] [blame] | 101 | * vimc_mbus_code_supported - helper to check supported mbus codes |
| 102 | * |
| 103 | * Helper function to check if mbus code is enumerated by vimc_enum_mbus_code() |
| 104 | */ |
| 105 | bool vimc_mbus_code_supported(__u32 code); |
| 106 | |
| 107 | /** |
| 108 | * vimc_enum_mbus_code - enumerate mbus codes |
| 109 | * |
| 110 | * Helper function to be pluged in .enum_mbus_code from |
| 111 | * struct v4l2_subdev_pad_ops. |
| 112 | */ |
| 113 | int vimc_enum_mbus_code(struct v4l2_subdev *sd, |
| 114 | struct v4l2_subdev_pad_config *cfg, |
| 115 | struct v4l2_subdev_mbus_code_enum *code); |
| 116 | |
| 117 | /** |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 118 | * vimc_pads_init - initialize pads |
| 119 | * |
| 120 | * @num_pads: number of pads to initialize |
| 121 | * @pads_flags: flags to use in each pad |
| 122 | * |
| 123 | * Helper functions to allocate/initialize pads |
| 124 | */ |
| 125 | struct media_pad *vimc_pads_init(u16 num_pads, |
| 126 | const unsigned long *pads_flag); |
| 127 | |
| 128 | /** |
| 129 | * vimc_pads_cleanup - free pads |
| 130 | * |
| 131 | * @pads: pointer to the pads |
| 132 | * |
| 133 | * Helper function to free the pads initialized with vimc_pads_init |
| 134 | */ |
| 135 | static inline void vimc_pads_cleanup(struct media_pad *pads) |
| 136 | { |
| 137 | kfree(pads); |
| 138 | } |
| 139 | |
| 140 | /** |
Helen Fornazier | bf5fb95 | 2017-06-19 14:00:13 -0300 | [diff] [blame] | 141 | * vimc_pipeline_s_stream - start stream through the pipeline |
| 142 | * |
| 143 | * @ent: the pointer to struct media_entity for the node |
| 144 | * @enable: 1 to start the stream and 0 to stop |
| 145 | * |
| 146 | * Helper function to call the s_stream of the subdevices connected |
| 147 | * in all the sink pads of the entity |
| 148 | */ |
| 149 | int vimc_pipeline_s_stream(struct media_entity *ent, int enable); |
| 150 | |
| 151 | /** |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 152 | * vimc_ent_sd_register - initialize and register a subdev node |
| 153 | * |
| 154 | * @ved: the vimc_ent_device struct to be initialize |
| 155 | * @sd: the v4l2_subdev struct to be initialize and registered |
| 156 | * @v4l2_dev: the v4l2 device to register the v4l2_subdev |
| 157 | * @name: name of the sub-device. Please notice that the name must be |
| 158 | * unique. |
| 159 | * @function: media entity function defined by MEDIA_ENT_F_* macros |
| 160 | * @num_pads: number of pads to initialize |
| 161 | * @pads_flag: flags to use in each pad |
Hans Verkuil | 2b177f2 | 2019-03-05 03:36:20 -0500 | [diff] [blame] | 162 | * @sd_int_ops: pointer to &struct v4l2_subdev_internal_ops |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 163 | * @sd_ops: pointer to &struct v4l2_subdev_ops. |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 164 | * |
| 165 | * Helper function initialize and register the struct vimc_ent_device and struct |
| 166 | * v4l2_subdev which represents a subdev node in the topology |
| 167 | */ |
| 168 | int vimc_ent_sd_register(struct vimc_ent_device *ved, |
| 169 | struct v4l2_subdev *sd, |
| 170 | struct v4l2_device *v4l2_dev, |
| 171 | const char *const name, |
| 172 | u32 function, |
| 173 | u16 num_pads, |
| 174 | const unsigned long *pads_flag, |
Hans Verkuil | 2b177f2 | 2019-03-05 03:36:20 -0500 | [diff] [blame] | 175 | const struct v4l2_subdev_internal_ops *sd_int_ops, |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 176 | const struct v4l2_subdev_ops *sd_ops); |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 177 | |
| 178 | /** |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 179 | * vimc_ent_sd_unregister - cleanup and unregister a subdev node |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 180 | * |
Helen Fornazier | 4a29b70 | 2017-06-19 14:00:18 -0300 | [diff] [blame] | 181 | * @ved: the vimc_ent_device struct to be cleaned up |
| 182 | * @sd: the v4l2_subdev struct to be unregistered |
Helen Fornazier | c149543 | 2017-06-19 14:00:12 -0300 | [diff] [blame] | 183 | * |
| 184 | * Helper function cleanup and unregister the struct vimc_ent_device and struct |
| 185 | * v4l2_subdev which represents a subdev node in the topology |
| 186 | */ |
| 187 | void vimc_ent_sd_unregister(struct vimc_ent_device *ved, |
| 188 | struct v4l2_subdev *sd); |
| 189 | |
Helen Fornazier | 288a22d | 2017-06-19 14:00:14 -0300 | [diff] [blame] | 190 | /** |
| 191 | * vimc_link_validate - validates a media link |
| 192 | * |
| 193 | * @link: pointer to &struct media_link |
| 194 | * |
| 195 | * This function calls validates if a media link is valid for streaming. |
| 196 | */ |
| 197 | int vimc_link_validate(struct media_link *link); |
| 198 | |
Helen Koike | f2fe890 | 2017-04-07 14:55:19 -0300 | [diff] [blame] | 199 | #endif |