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