Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 1 | /* |
| 2 | * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface |
| 3 | * |
Laurent Pinchart | 8a1edc5 | 2014-02-06 14:42:31 -0300 | [diff] [blame] | 4 | * Copyright (C) 2013-2014 Renesas Electronics Corporation |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 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/gfp.h> |
| 16 | |
| 17 | #include <media/v4l2-subdev.h> |
| 18 | |
| 19 | #include "vsp1.h" |
Laurent Pinchart | 5e8dbbf | 2015-11-22 20:29:25 -0200 | [diff] [blame] | 20 | #include "vsp1_dl.h" |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 21 | #include "vsp1_lif.h" |
| 22 | |
| 23 | #define LIF_MIN_SIZE 2U |
| 24 | #define LIF_MAX_SIZE 2048U |
| 25 | |
| 26 | /* ----------------------------------------------------------------------------- |
| 27 | * Device Access |
| 28 | */ |
| 29 | |
Laurent Pinchart | 5e8dbbf | 2015-11-22 20:29:25 -0200 | [diff] [blame] | 30 | static inline void vsp1_lif_write(struct vsp1_lif *lif, struct vsp1_dl_list *dl, |
| 31 | u32 reg, u32 data) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 32 | { |
Laurent Pinchart | 5e8dbbf | 2015-11-22 20:29:25 -0200 | [diff] [blame] | 33 | vsp1_dl_list_write(dl, reg, data); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame] | 37 | * V4L2 Subdevice Operations |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 38 | */ |
| 39 | |
| 40 | static int lif_enum_mbus_code(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 41 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 42 | struct v4l2_subdev_mbus_code_enum *code) |
| 43 | { |
| 44 | static const unsigned int codes[] = { |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 45 | MEDIA_BUS_FMT_ARGB8888_1X32, |
| 46 | MEDIA_BUS_FMT_AYUV8_1X32, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 47 | }; |
Hans Verkuil | 3f1ccf1 | 2015-03-04 01:47:57 -0800 | [diff] [blame] | 48 | struct vsp1_lif *lif = to_lif(subdev); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 49 | |
| 50 | if (code->pad == LIF_PAD_SINK) { |
| 51 | if (code->index >= ARRAY_SIZE(codes)) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | code->code = codes[code->index]; |
| 55 | } else { |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 56 | struct v4l2_subdev_pad_config *config; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 57 | struct v4l2_mbus_framefmt *format; |
| 58 | |
| 59 | /* The LIF can't perform format conversion, the sink format is |
| 60 | * always identical to the source format. |
| 61 | */ |
| 62 | if (code->index) |
| 63 | return -EINVAL; |
| 64 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 65 | config = vsp1_entity_get_pad_config(&lif->entity, cfg, |
| 66 | code->which); |
| 67 | if (!config) |
| 68 | return -EINVAL; |
| 69 | |
| 70 | format = vsp1_entity_get_pad_format(&lif->entity, config, |
| 71 | LIF_PAD_SINK); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 72 | code->code = format->code; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int lif_enum_frame_size(struct v4l2_subdev *subdev, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 79 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 80 | struct v4l2_subdev_frame_size_enum *fse) |
| 81 | { |
Hans Verkuil | 5778e74 | 2015-03-04 01:47:58 -0800 | [diff] [blame] | 82 | struct vsp1_lif *lif = to_lif(subdev); |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 83 | struct v4l2_subdev_pad_config *config; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 84 | struct v4l2_mbus_framefmt *format; |
| 85 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 86 | config = vsp1_entity_get_pad_config(&lif->entity, cfg, fse->which); |
| 87 | if (!config) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | format = vsp1_entity_get_pad_format(&lif->entity, config, LIF_PAD_SINK); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 91 | |
| 92 | if (fse->index || fse->code != format->code) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | if (fse->pad == LIF_PAD_SINK) { |
| 96 | fse->min_width = LIF_MIN_SIZE; |
| 97 | fse->max_width = LIF_MAX_SIZE; |
| 98 | fse->min_height = LIF_MIN_SIZE; |
| 99 | fse->max_height = LIF_MAX_SIZE; |
| 100 | } else { |
| 101 | fse->min_width = format->width; |
| 102 | fse->max_width = format->width; |
| 103 | fse->min_height = format->height; |
| 104 | fse->max_height = format->height; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Laurent Pinchart | 1bd0a1b | 2015-11-01 15:18:32 -0200 | [diff] [blame] | 110 | static int lif_get_format(struct v4l2_subdev *subdev, |
| 111 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 112 | struct v4l2_subdev_format *fmt) |
| 113 | { |
| 114 | struct vsp1_lif *lif = to_lif(subdev); |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 115 | struct v4l2_subdev_pad_config *config; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 116 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 117 | config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which); |
| 118 | if (!config) |
| 119 | return -EINVAL; |
| 120 | |
| 121 | fmt->format = *vsp1_entity_get_pad_format(&lif->entity, config, |
| 122 | fmt->pad); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Laurent Pinchart | 1bd0a1b | 2015-11-01 15:18:32 -0200 | [diff] [blame] | 127 | static int lif_set_format(struct v4l2_subdev *subdev, |
| 128 | struct v4l2_subdev_pad_config *cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 129 | struct v4l2_subdev_format *fmt) |
| 130 | { |
| 131 | struct vsp1_lif *lif = to_lif(subdev); |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 132 | struct v4l2_subdev_pad_config *config; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 133 | struct v4l2_mbus_framefmt *format; |
| 134 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 135 | config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which); |
| 136 | if (!config) |
| 137 | return -EINVAL; |
| 138 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 139 | /* Default to YUV if the requested format is not supported. */ |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 140 | if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 && |
| 141 | fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32) |
| 142 | fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 143 | |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 144 | format = vsp1_entity_get_pad_format(&lif->entity, config, fmt->pad); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 145 | |
| 146 | if (fmt->pad == LIF_PAD_SOURCE) { |
| 147 | /* The LIF source format is always identical to its sink |
| 148 | * format. |
| 149 | */ |
| 150 | fmt->format = *format; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | format->code = fmt->format.code; |
| 155 | format->width = clamp_t(unsigned int, fmt->format.width, |
| 156 | LIF_MIN_SIZE, LIF_MAX_SIZE); |
| 157 | format->height = clamp_t(unsigned int, fmt->format.height, |
| 158 | LIF_MIN_SIZE, LIF_MAX_SIZE); |
| 159 | format->field = V4L2_FIELD_NONE; |
| 160 | format->colorspace = V4L2_COLORSPACE_SRGB; |
| 161 | |
| 162 | fmt->format = *format; |
| 163 | |
| 164 | /* Propagate the format to the source pad. */ |
Laurent Pinchart | e790c3c | 2015-11-15 19:14:22 -0200 | [diff] [blame] | 165 | format = vsp1_entity_get_pad_format(&lif->entity, config, |
| 166 | LIF_PAD_SOURCE); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 167 | *format = fmt->format; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 172 | static struct v4l2_subdev_pad_ops lif_pad_ops = { |
Laurent Pinchart | 0efdf0f | 2015-11-15 20:09:08 -0200 | [diff] [blame] | 173 | .init_cfg = vsp1_entity_init_cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 174 | .enum_mbus_code = lif_enum_mbus_code, |
| 175 | .enum_frame_size = lif_enum_frame_size, |
| 176 | .get_fmt = lif_get_format, |
| 177 | .set_fmt = lif_set_format, |
| 178 | }; |
| 179 | |
| 180 | static struct v4l2_subdev_ops lif_ops = { |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 181 | .pad = &lif_pad_ops, |
| 182 | }; |
| 183 | |
| 184 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame] | 185 | * VSP1 Entity Operations |
| 186 | */ |
| 187 | |
Laurent Pinchart | 83dd019 | 2016-01-14 14:17:32 -0200 | [diff] [blame^] | 188 | static void lif_configure(struct vsp1_entity *entity, |
| 189 | struct vsp1_pipeline *pipe, |
| 190 | struct vsp1_dl_list *dl) |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame] | 191 | { |
| 192 | const struct v4l2_mbus_framefmt *format; |
| 193 | struct vsp1_lif *lif = to_lif(&entity->subdev); |
| 194 | unsigned int hbth = 1300; |
| 195 | unsigned int obth = 400; |
| 196 | unsigned int lbth = 200; |
| 197 | |
| 198 | format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config, |
| 199 | LIF_PAD_SOURCE); |
| 200 | |
| 201 | obth = min(obth, (format->width + 1) / 2 * format->height - 4); |
| 202 | |
Laurent Pinchart | 5e8dbbf | 2015-11-22 20:29:25 -0200 | [diff] [blame] | 203 | vsp1_lif_write(lif, dl, VI6_LIF_CSBTH, |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame] | 204 | (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) | |
| 205 | (lbth << VI6_LIF_CSBTH_LBTH_SHIFT)); |
| 206 | |
Laurent Pinchart | 5e8dbbf | 2015-11-22 20:29:25 -0200 | [diff] [blame] | 207 | vsp1_lif_write(lif, dl, VI6_LIF_CTRL, |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame] | 208 | (obth << VI6_LIF_CTRL_OBTH_SHIFT) | |
| 209 | (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) | |
| 210 | VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN); |
| 211 | } |
| 212 | |
| 213 | static const struct vsp1_entity_operations lif_entity_ops = { |
| 214 | .configure = lif_configure, |
| 215 | }; |
| 216 | |
| 217 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 218 | * Initialization and Cleanup |
| 219 | */ |
| 220 | |
| 221 | struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1) |
| 222 | { |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 223 | struct vsp1_lif *lif; |
| 224 | int ret; |
| 225 | |
| 226 | lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL); |
| 227 | if (lif == NULL) |
| 228 | return ERR_PTR(-ENOMEM); |
| 229 | |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame] | 230 | lif->entity.ops = &lif_entity_ops; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 231 | lif->entity.type = VSP1_ENTITY_LIF; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 232 | |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 233 | ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 234 | if (ret < 0) |
| 235 | return ERR_PTR(ret); |
| 236 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 237 | return lif; |
| 238 | } |