Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Renesas R-Car VIN |
| 3 | * |
| 4 | * Copyright (C) 2016 Renesas Electronics Corp. |
| 5 | * Copyright (C) 2011-2013 Renesas Solutions Corp. |
| 6 | * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com> |
| 7 | * Copyright (C) 2008 Magnus Damm |
| 8 | * |
| 9 | * Based on the soc-camera rcar_vin driver |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the |
| 13 | * Free Software Foundation; either version 2 of the License, or (at your |
| 14 | * option) any later version. |
| 15 | */ |
| 16 | |
| 17 | #ifndef __RCAR_VIN__ |
| 18 | #define __RCAR_VIN__ |
| 19 | |
| 20 | #include <media/v4l2-async.h> |
| 21 | #include <media/v4l2-ctrls.h> |
| 22 | #include <media/v4l2-dev.h> |
| 23 | #include <media/v4l2-device.h> |
| 24 | #include <media/videobuf2-v4l2.h> |
| 25 | |
| 26 | /* Number of HW buffers */ |
| 27 | #define HW_BUFFER_NUM 3 |
| 28 | |
| 29 | /* Address alignment mask for HW buffers */ |
| 30 | #define HW_BUFFER_MASK 0x7f |
| 31 | |
Niklas Söderlund | bb3ed3f | 2018-04-14 07:57:00 -0400 | [diff] [blame] | 32 | enum model_id { |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 33 | RCAR_H1, |
| 34 | RCAR_M1, |
Niklas Söderlund | 23eb2c8 | 2016-08-15 12:06:28 -0300 | [diff] [blame] | 35 | RCAR_GEN2, |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * STOPPED - No operation in progress |
| 40 | * RUNNING - Operation in progress have buffers |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 41 | * STOPPING - Stopping operation |
| 42 | */ |
| 43 | enum rvin_dma_state { |
| 44 | STOPPED = 0, |
| 45 | RUNNING, |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 46 | STOPPING, |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * struct rvin_source_fmt - Source information |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 51 | * @width: Width from source |
| 52 | * @height: Height from source |
| 53 | */ |
| 54 | struct rvin_source_fmt { |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 55 | u32 width; |
| 56 | u32 height; |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * struct rvin_video_format - Data format stored in memory |
| 61 | * @fourcc: Pixelformat |
| 62 | * @bpp: Bytes per pixel |
| 63 | */ |
| 64 | struct rvin_video_format { |
| 65 | u32 fourcc; |
| 66 | u8 bpp; |
| 67 | }; |
| 68 | |
Niklas Söderlund | 83fba2c | 2016-08-15 12:06:34 -0300 | [diff] [blame] | 69 | /** |
| 70 | * struct rvin_graph_entity - Video endpoint from async framework |
| 71 | * @asd: sub-device descriptor for async framework |
| 72 | * @subdev: subdevice matched using async framework |
Niklas Söderlund | b50b77e | 2016-08-15 12:06:35 -0300 | [diff] [blame] | 73 | * @code: Media bus format from source |
| 74 | * @mbus_cfg: Media bus format from DT |
Niklas Söderlund | 1eb3641 | 2017-05-23 21:15:28 -0300 | [diff] [blame] | 75 | * @source_pad: source pad of remote subdevice |
| 76 | * @sink_pad: sink pad of remote subdevice |
Niklas Söderlund | 83fba2c | 2016-08-15 12:06:34 -0300 | [diff] [blame] | 77 | */ |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 78 | struct rvin_graph_entity { |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 79 | struct v4l2_async_subdev asd; |
| 80 | struct v4l2_subdev *subdev; |
Niklas Söderlund | b50b77e | 2016-08-15 12:06:35 -0300 | [diff] [blame] | 81 | |
| 82 | u32 code; |
| 83 | struct v4l2_mbus_config mbus_cfg; |
Niklas Söderlund | 1eb3641 | 2017-05-23 21:15:28 -0300 | [diff] [blame] | 84 | |
| 85 | unsigned int source_pad; |
| 86 | unsigned int sink_pad; |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /** |
Niklas Söderlund | bb3ed3f | 2018-04-14 07:57:00 -0400 | [diff] [blame] | 90 | * struct rvin_info - Information about the particular VIN implementation |
| 91 | * @model: VIN model |
Niklas Söderlund | 16cdb7d | 2018-04-14 07:57:01 -0400 | [diff] [blame^] | 92 | * @max_width: max input width the VIN supports |
| 93 | * @max_height: max input height the VIN supports |
Niklas Söderlund | bb3ed3f | 2018-04-14 07:57:00 -0400 | [diff] [blame] | 94 | */ |
| 95 | struct rvin_info { |
| 96 | enum model_id model; |
Niklas Söderlund | 16cdb7d | 2018-04-14 07:57:01 -0400 | [diff] [blame^] | 97 | |
| 98 | unsigned int max_width; |
| 99 | unsigned int max_height; |
Niklas Söderlund | bb3ed3f | 2018-04-14 07:57:00 -0400 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /** |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 103 | * struct rvin_dev - Renesas VIN device structure |
| 104 | * @dev: (OF) device |
| 105 | * @base: device I/O register space remapped to virtual memory |
Niklas Söderlund | bb3ed3f | 2018-04-14 07:57:00 -0400 | [diff] [blame] | 106 | * @info: info about VIN instance |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 107 | * |
| 108 | * @vdev: V4L2 video device associated with VIN |
| 109 | * @v4l2_dev: V4L2 device |
| 110 | * @ctrl_handler: V4L2 control handler |
| 111 | * @notifier: V4L2 asynchronous subdevs notifier |
Niklas Söderlund | 4869ce9 | 2016-08-15 12:06:29 -0300 | [diff] [blame] | 112 | * @digital: entity in the DT for local digital subdevice |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 113 | * |
| 114 | * @lock: protects @queue |
| 115 | * @queue: vb2 buffers queue |
Niklas Söderlund | 6a8ffa8 | 2018-03-13 22:49:09 -0400 | [diff] [blame] | 116 | * @scratch: cpu address for scratch buffer |
| 117 | * @scratch_phys: physical address of the scratch buffer |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 118 | * |
Niklas Söderlund | dc9aec7 | 2018-03-13 22:49:10 -0400 | [diff] [blame] | 119 | * @qlock: protects @queue_buf, @buf_list, @sequence |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 120 | * @state |
| 121 | * @queue_buf: Keeps track of buffers given to HW slot |
| 122 | * @buf_list: list of queued buffers |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 123 | * @sequence: V4L2 buffers sequence number |
| 124 | * @state: keeps track of operation state |
| 125 | * |
| 126 | * @source: active format from the video source |
| 127 | * @format: active V4L2 pixel format |
| 128 | * |
| 129 | * @crop: active cropping |
| 130 | * @compose: active composing |
| 131 | */ |
| 132 | struct rvin_dev { |
| 133 | struct device *dev; |
| 134 | void __iomem *base; |
Niklas Söderlund | bb3ed3f | 2018-04-14 07:57:00 -0400 | [diff] [blame] | 135 | const struct rvin_info *info; |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 136 | |
| 137 | struct video_device vdev; |
| 138 | struct v4l2_device v4l2_dev; |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 139 | struct v4l2_ctrl_handler ctrl_handler; |
| 140 | struct v4l2_async_notifier notifier; |
Sakari Ailus | 85999e8 | 2017-09-01 18:41:19 -0400 | [diff] [blame] | 141 | struct rvin_graph_entity *digital; |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 142 | |
| 143 | struct mutex lock; |
| 144 | struct vb2_queue queue; |
Niklas Söderlund | 6a8ffa8 | 2018-03-13 22:49:09 -0400 | [diff] [blame] | 145 | void *scratch; |
| 146 | dma_addr_t scratch_phys; |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 147 | |
| 148 | spinlock_t qlock; |
| 149 | struct vb2_v4l2_buffer *queue_buf[HW_BUFFER_NUM]; |
| 150 | struct list_head buf_list; |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 151 | unsigned int sequence; |
| 152 | enum rvin_dma_state state; |
| 153 | |
| 154 | struct rvin_source_fmt source; |
| 155 | struct v4l2_pix_format format; |
| 156 | |
| 157 | struct v4l2_rect crop; |
| 158 | struct v4l2_rect compose; |
| 159 | }; |
| 160 | |
Sakari Ailus | 85999e8 | 2017-09-01 18:41:19 -0400 | [diff] [blame] | 161 | #define vin_to_source(vin) ((vin)->digital->subdev) |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 162 | |
| 163 | /* Debug */ |
| 164 | #define vin_dbg(d, fmt, arg...) dev_dbg(d->dev, fmt, ##arg) |
| 165 | #define vin_info(d, fmt, arg...) dev_info(d->dev, fmt, ##arg) |
| 166 | #define vin_warn(d, fmt, arg...) dev_warn(d->dev, fmt, ##arg) |
| 167 | #define vin_err(d, fmt, arg...) dev_err(d->dev, fmt, ##arg) |
| 168 | |
Niklas Söderlund | d6ad012 | 2018-04-14 07:56:57 -0400 | [diff] [blame] | 169 | int rvin_dma_register(struct rvin_dev *vin, int irq); |
| 170 | void rvin_dma_unregister(struct rvin_dev *vin); |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 171 | |
Niklas Söderlund | d6ad012 | 2018-04-14 07:56:57 -0400 | [diff] [blame] | 172 | int rvin_v4l2_register(struct rvin_dev *vin); |
| 173 | void rvin_v4l2_unregister(struct rvin_dev *vin); |
Niklas Söderlund | f00add9 | 2016-04-26 10:22:19 -0300 | [diff] [blame] | 174 | |
| 175 | const struct rvin_video_format *rvin_format_from_pixel(u32 pixelformat); |
| 176 | |
| 177 | /* Cropping, composing and scaling */ |
| 178 | void rvin_scale_try(struct rvin_dev *vin, struct v4l2_pix_format *pix, |
| 179 | u32 width, u32 height); |
| 180 | void rvin_crop_scale_comp(struct rvin_dev *vin); |
| 181 | |
| 182 | #endif |