b4b99fc4a47235d0a1f916077cd040c244f7c00a
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.15 / 950-0720-media-i2c-Provide-ov7251_check_hwcfg.patch
1 From 3b21da2334113027d91829e1fabe332cf21522a3 Mon Sep 17 00:00:00 2001
2 From: Daniel Scally <djrscally@gmail.com>
3 Date: Tue, 15 Feb 2022 23:07:31 +0000
4 Subject: [PATCH] media: i2c: Provide ov7251_check_hwcfg()
5
6 Move the endpoint checking from .probe() to a dedicated function,
7 and additionally check that the firmware provided link frequencies
8 are a match for those supported by the driver. Finally, return
9 -EPROBE_DEFER if the endpoint is not available, as it could be built
10 by the ipu3-cio2 driver if that probes later.
11
12 Signed-off-by: Daniel Scally <djrscally@gmail.com>
13 ---
14 drivers/media/i2c/ov7251.c | 84 ++++++++++++++++++++++++++++++--------
15 1 file changed, 66 insertions(+), 18 deletions(-)
16
17 --- a/drivers/media/i2c/ov7251.c
18 +++ b/drivers/media/i2c/ov7251.c
19 @@ -1255,11 +1255,74 @@ static const struct v4l2_subdev_ops ov72
20 .pad = &ov7251_subdev_pad_ops,
21 };
22
23 +static int ov7251_check_hwcfg(struct ov7251 *ov7251)
24 +{
25 + struct fwnode_handle *fwnode = dev_fwnode(ov7251->dev);
26 + struct v4l2_fwnode_endpoint bus_cfg = {
27 + .bus_type = V4L2_MBUS_CSI2_DPHY,
28 + };
29 + struct fwnode_handle *endpoint;
30 + bool freq_found;
31 + int i, j;
32 + int ret;
33 +
34 + endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
35 + if (!endpoint)
36 + return -EPROBE_DEFER; /* could be provided by cio2-bridge */
37 +
38 + ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
39 + fwnode_handle_put(endpoint);
40 + if (ret)
41 + return dev_err_probe(ov7251->dev, ret,
42 + "parsing endpoint node failed\n");
43 +
44 + if (bus_cfg.bus_type != V4L2_MBUS_CSI2_DPHY) {
45 + ret = -EINVAL;
46 + dev_err(ov7251->dev, "invalid bus type (%u), must be (%u)\n",
47 + bus_cfg.bus_type, V4L2_MBUS_CSI2_DPHY);
48 + goto out_free_bus_cfg;
49 + }
50 +
51 + if (bus_cfg.bus.mipi_csi2.num_data_lanes != 1) {
52 + dev_err(ov7251->dev, "only a 1-lane CSI2 config is supported");
53 + ret = -EINVAL;
54 + goto out_free_bus_cfg;
55 + }
56 +
57 + if (!bus_cfg.nr_of_link_frequencies) {
58 + dev_err(ov7251->dev, "no link frequencies defined\n");
59 + ret = -EINVAL;
60 + goto out_free_bus_cfg;
61 + }
62 +
63 + freq_found = false;
64 + for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
65 + if (freq_found)
66 + break;
67 +
68 + for (j = 0; j < ARRAY_SIZE(link_freq); j++)
69 + if (bus_cfg.link_frequencies[i] == link_freq[j]) {
70 + freq_found = true;
71 + break;
72 + }
73 + }
74 +
75 + if (i == bus_cfg.nr_of_link_frequencies) {
76 + dev_err(ov7251->dev, "no supported link freq found\n");
77 + ret = -EINVAL;
78 + goto out_free_bus_cfg;
79 + }
80 +
81 +out_free_bus_cfg:
82 + v4l2_fwnode_endpoint_free(&bus_cfg);
83 +
84 + return ret;
85 +}
86 +
87 static int ov7251_probe(struct i2c_client *client)
88 {
89 struct v4l2_fwnode_device_properties props;
90 struct device *dev = &client->dev;
91 - struct fwnode_handle *endpoint;
92 struct ov7251 *ov7251;
93 u8 chip_id_high, chip_id_low, chip_rev;
94 int ret;
95 @@ -1271,24 +1334,9 @@ static int ov7251_probe(struct i2c_clien
96 ov7251->i2c_client = client;
97 ov7251->dev = dev;
98
99 - endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
100 - if (!endpoint) {
101 - dev_err(dev, "endpoint node not found\n");
102 - return -EINVAL;
103 - }
104 -
105 - ret = v4l2_fwnode_endpoint_parse(endpoint, &ov7251->ep);
106 - fwnode_handle_put(endpoint);
107 - if (ret < 0) {
108 - dev_err(dev, "parsing endpoint node failed\n");
109 + ret = ov7251_check_hwcfg(ov7251);
110 + if (ret)
111 return ret;
112 - }
113 -
114 - if (ov7251->ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
115 - dev_err(dev, "invalid bus type (%u), must be CSI2 (%u)\n",
116 - ov7251->ep.bus_type, V4L2_MBUS_CSI2_DPHY);
117 - return -EINVAL;
118 - }
119
120 /* get system clock (xclk) */
121 ov7251->xclk = devm_clk_get(dev, "xclk");