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