bcm27xx: add support for linux v5.15
[openwrt/staging/chunkeey.git] / target / linux / bcm27xx / patches-5.15 / 950-0573-media-i2c-ov5647-Support-HFLIP-and-VFLIP.patch
1 From 08cc1dcf1437d65dbb36dda27db29ec3a689101f Mon Sep 17 00:00:00 2001
2 From: David Plowman <david.plowman@raspberrypi.com>
3 Date: Mon, 22 Nov 2021 13:10:39 +0000
4 Subject: [PATCH] media: i2c: ov5647: Support HFLIP and VFLIP
5
6 Add these missing V4L2 controls. Tested binned and full resolution
7 modes in all four orientations using Raspberry Pi running libcamera.
8
9 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
10 ---
11 drivers/media/i2c/ov5647.c | 76 +++++++++++++++++++++++++++++++++++---
12 1 file changed, 70 insertions(+), 6 deletions(-)
13
14 --- a/drivers/media/i2c/ov5647.c
15 +++ b/drivers/media/i2c/ov5647.c
16 @@ -54,6 +54,8 @@
17 #define OV5647_REG_GAIN_LO 0x350b
18 #define OV5647_REG_VTS_HI 0x380e
19 #define OV5647_REG_VTS_LO 0x380f
20 +#define OV5647_REG_VFLIP 0x3820
21 +#define OV5647_REG_HFLIP 0x3821
22 #define OV5647_REG_FRAME_OFF_NUMBER 0x4202
23 #define OV5647_REG_MIPI_CTRL00 0x4800
24 #define OV5647_REG_MIPI_CTRL14 0x4814
25 @@ -108,6 +110,8 @@ struct ov5647 {
26 struct v4l2_ctrl *hblank;
27 struct v4l2_ctrl *vblank;
28 struct v4l2_ctrl *exposure;
29 + struct v4l2_ctrl *hflip;
30 + struct v4l2_ctrl *vflip;
31 bool streaming;
32 };
33
34 @@ -136,7 +140,7 @@ static struct regval_list ov5647_2592x19
35 {0x3036, 0x69},
36 {0x303c, 0x11},
37 {0x3106, 0xf5},
38 - {0x3821, 0x06},
39 + {0x3821, 0x00},
40 {0x3820, 0x00},
41 {0x3827, 0xec},
42 {0x370c, 0x03},
43 @@ -225,7 +229,7 @@ static struct regval_list ov5647_1080p30
44 {0x3036, 0x62},
45 {0x303c, 0x11},
46 {0x3106, 0xf5},
47 - {0x3821, 0x06},
48 + {0x3821, 0x00},
49 {0x3820, 0x00},
50 {0x3827, 0xec},
51 {0x370c, 0x03},
52 @@ -389,7 +393,7 @@ static struct regval_list ov5647_2x2binn
53 {0x4800, 0x24},
54 {0x3503, 0x03},
55 {0x3820, 0x41},
56 - {0x3821, 0x07},
57 + {0x3821, 0x01},
58 {0x350a, 0x00},
59 {0x350b, 0x10},
60 {0x3500, 0x00},
61 @@ -405,7 +409,7 @@ static struct regval_list ov5647_640x480
62 {0x3035, 0x11},
63 {0x3036, 0x46},
64 {0x303c, 0x11},
65 - {0x3821, 0x07},
66 + {0x3821, 0x01},
67 {0x3820, 0x41},
68 {0x370c, 0x03},
69 {0x3612, 0x59},
70 @@ -946,6 +950,25 @@ static const struct v4l2_subdev_video_op
71 .s_stream = ov5647_s_stream,
72 };
73
74 +/* This function returns the mbus code for the current settings of the
75 + HFLIP and VFLIP controls. */
76 +
77 +static u32 ov5647_get_mbus_code(struct v4l2_subdev *sd)
78 +{
79 + struct ov5647 *sensor = to_sensor(sd);
80 + /* The control values are only 0 or 1. */
81 + int index = sensor->hflip->val | (sensor->vflip->val << 1);
82 +
83 + static const u32 codes[4] = {
84 + MEDIA_BUS_FMT_SGBRG10_1X10,
85 + MEDIA_BUS_FMT_SBGGR10_1X10,
86 + MEDIA_BUS_FMT_SRGGB10_1X10,
87 + MEDIA_BUS_FMT_SGRBG10_1X10
88 + };
89 +
90 + return codes[index];
91 +}
92 +
93 static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
94 struct v4l2_subdev_state *sd_state,
95 struct v4l2_subdev_mbus_code_enum *code)
96 @@ -953,7 +976,7 @@ static int ov5647_enum_mbus_code(struct
97 if (code->index > 0)
98 return -EINVAL;
99
100 - code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
101 + code->code = ov5647_get_mbus_code(sd);
102
103 return 0;
104 }
105 @@ -964,7 +987,7 @@ static int ov5647_enum_frame_size(struct
106 {
107 const struct v4l2_mbus_framefmt *fmt;
108
109 - if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10 ||
110 + if (fse->code != ov5647_get_mbus_code(sd) ||
111 fse->index >= ARRAY_SIZE(ov5647_modes))
112 return -EINVAL;
113
114 @@ -997,6 +1020,8 @@ static int ov5647_get_pad_fmt(struct v4l
115 }
116
117 *fmt = *sensor_format;
118 + /* The code we pass back must reflect the current h/vflips. */
119 + fmt->code = ov5647_get_mbus_code(sd);
120 mutex_unlock(&sensor->lock);
121
122 return 0;
123 @@ -1044,6 +1069,8 @@ static int ov5647_set_pad_fmt(struct v4l
124 exposure_def);
125 }
126 *fmt = mode->format;
127 + /* The code we pass back must reflect the current h/vflips. */
128 + fmt->code = ov5647_get_mbus_code(sd);
129 mutex_unlock(&sensor->lock);
130
131 return 0;
132 @@ -1219,6 +1246,25 @@ static int ov5647_s_exposure(struct v4l2
133 return ov5647_write(sd, OV5647_REG_EXP_LO, (val & 0xf) << 4);
134 }
135
136 +static int ov5647_s_flip( struct v4l2_subdev *sd, u16 reg, u32 ctrl_val)
137 +{
138 + int ret;
139 + u8 reg_val;
140 +
141 + /* Set or clear bit 1 and leave everything else alone. */
142 + ret = ov5647_read(sd, reg, &reg_val);
143 + if (ret == 0) {
144 + if (ctrl_val)
145 + reg_val |= 2;
146 + else
147 + reg_val &= ~2;
148 +
149 + ret = ov5647_write(sd, reg, reg_val);
150 + }
151 +
152 + return ret;
153 +}
154 +
155 static int ov5647_s_ctrl(struct v4l2_ctrl *ctrl)
156 {
157 struct ov5647 *sensor = container_of(ctrl->handler,
158 @@ -1277,6 +1323,14 @@ static int ov5647_s_ctrl(struct v4l2_ctr
159 /* Read-only, but we adjust it based on mode. */
160 break;
161
162 + case V4L2_CID_HFLIP:
163 + /* There's an in-built hflip in the sensor, so account for that here. */
164 + ov5647_s_flip(sd, OV5647_REG_HFLIP, !ctrl->val);
165 + break;
166 + case V4L2_CID_VFLIP:
167 + ov5647_s_flip(sd, OV5647_REG_VFLIP, ctrl->val);
168 + break;
169 +
170 default:
171 dev_info(&client->dev,
172 "Control (id:0x%x, val:0x%x) not supported\n",
173 @@ -1343,6 +1397,16 @@ static int ov5647_init_controls(struct o
174 sensor->mode->vts -
175 sensor->mode->format.height);
176
177 + sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
178 + V4L2_CID_HFLIP, 0, 1, 1, 0);
179 + if (sensor->hflip)
180 + sensor->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
181 +
182 + sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
183 + V4L2_CID_VFLIP, 0, 1, 1, 0);
184 + if (sensor->vflip)
185 + sensor->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
186 +
187 v4l2_fwnode_device_parse(dev, &props);
188
189 v4l2_ctrl_new_fwnode_properties(&sensor->ctrls, &ov5647_ctrl_ops,