media: ipu-bridge: Only keep PLD around while parsing
[platform/kernel/linux-starfive.git] / drivers / media / pci / intel / ipu-bridge.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
3
4 #include <linux/acpi.h>
5 #include <linux/device.h>
6 #include <linux/i2c.h>
7 #include <linux/property.h>
8 #include <media/v4l2-fwnode.h>
9
10 #include "ipu-bridge.h"
11
12 /*
13  * Extend this array with ACPI Hardware IDs of devices known to be working
14  * plus the number of link-frequencies expected by their drivers, along with
15  * the frequency values in hertz. This is somewhat opportunistic way of adding
16  * support for this for now in the hopes of a better source for the information
17  * (possibly some encoded value in the SSDB buffer that we're unaware of)
18  * becoming apparent in the future.
19  *
20  * Do not add an entry for a sensor that is not actually supported.
21  */
22 static const struct ipu_sensor_config ipu_supported_sensors[] = {
23         /* Omnivision OV5693 */
24         IPU_SENSOR_CONFIG("INT33BE", 1, 419200000),
25         /* Omnivision OV8865 */
26         IPU_SENSOR_CONFIG("INT347A", 1, 360000000),
27         /* Omnivision OV7251 */
28         IPU_SENSOR_CONFIG("INT347E", 1, 319200000),
29         /* Omnivision OV2680 */
30         IPU_SENSOR_CONFIG("OVTI2680", 0),
31         /* Omnivision ov8856 */
32         IPU_SENSOR_CONFIG("OVTI8856", 3, 180000000, 360000000, 720000000),
33         /* Omnivision ov2740 */
34         IPU_SENSOR_CONFIG("INT3474", 1, 360000000),
35         /* Hynix hi556 */
36         IPU_SENSOR_CONFIG("INT3537", 1, 437000000),
37         /* Omnivision ov13b10 */
38         IPU_SENSOR_CONFIG("OVTIDB10", 1, 560000000),
39 };
40
41 static const struct ipu_property_names prop_names = {
42         .clock_frequency = "clock-frequency",
43         .rotation = "rotation",
44         .orientation = "orientation",
45         .bus_type = "bus-type",
46         .data_lanes = "data-lanes",
47         .remote_endpoint = "remote-endpoint",
48         .link_frequencies = "link-frequencies",
49 };
50
51 static const char * const ipu_vcm_types[] = {
52         "ad5823",
53         "dw9714",
54         "ad5816",
55         "dw9719",
56         "dw9718",
57         "dw9806b",
58         "wv517s",
59         "lc898122xa",
60         "lc898212axb",
61 };
62
63 static int ipu_bridge_read_acpi_buffer(struct acpi_device *adev, char *id,
64                                        void *data, u32 size)
65 {
66         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
67         union acpi_object *obj;
68         acpi_status status;
69         int ret = 0;
70
71         status = acpi_evaluate_object(adev->handle, id, NULL, &buffer);
72         if (ACPI_FAILURE(status))
73                 return -ENODEV;
74
75         obj = buffer.pointer;
76         if (!obj) {
77                 dev_err(&adev->dev, "Couldn't locate ACPI buffer\n");
78                 return -ENODEV;
79         }
80
81         if (obj->type != ACPI_TYPE_BUFFER) {
82                 dev_err(&adev->dev, "Not an ACPI buffer\n");
83                 ret = -ENODEV;
84                 goto out_free_buff;
85         }
86
87         if (obj->buffer.length > size) {
88                 dev_err(&adev->dev, "Given buffer is too small\n");
89                 ret = -EINVAL;
90                 goto out_free_buff;
91         }
92
93         memcpy(data, obj->buffer.pointer, obj->buffer.length);
94
95 out_free_buff:
96         kfree(buffer.pointer);
97         return ret;
98 }
99
100 static u32 ipu_bridge_parse_rotation(struct ipu_sensor *sensor)
101 {
102         switch (sensor->ssdb.degree) {
103         case IPU_SENSOR_ROTATION_NORMAL:
104                 return 0;
105         case IPU_SENSOR_ROTATION_INVERTED:
106                 return 180;
107         default:
108                 dev_warn(&sensor->adev->dev,
109                          "Unknown rotation %d. Assume 0 degree rotation\n",
110                          sensor->ssdb.degree);
111                 return 0;
112         }
113 }
114
115 static enum v4l2_fwnode_orientation ipu_bridge_parse_orientation(struct acpi_device *adev)
116 {
117         enum v4l2_fwnode_orientation orientation;
118         struct acpi_pld_info *pld;
119         acpi_status status;
120
121         status = acpi_get_physical_device_location(adev->handle, &pld);
122         if (ACPI_FAILURE(status)) {
123                 dev_warn(&adev->dev, "_PLD call failed, using default orientation\n");
124                 return V4L2_FWNODE_ORIENTATION_EXTERNAL;
125         }
126
127         switch (pld->panel) {
128         case ACPI_PLD_PANEL_FRONT:
129                 orientation = V4L2_FWNODE_ORIENTATION_FRONT;
130                 break;
131         case ACPI_PLD_PANEL_BACK:
132                 orientation = V4L2_FWNODE_ORIENTATION_BACK;
133                 break;
134         case ACPI_PLD_PANEL_TOP:
135         case ACPI_PLD_PANEL_LEFT:
136         case ACPI_PLD_PANEL_RIGHT:
137         case ACPI_PLD_PANEL_UNKNOWN:
138                 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
139                 break;
140         default:
141                 dev_warn(&adev->dev, "Unknown _PLD panel val %d\n", pld->panel);
142                 orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL;
143                 break;
144         }
145
146         ACPI_FREE(pld);
147         return orientation;
148 }
149
150 static void ipu_bridge_create_fwnode_properties(
151         struct ipu_sensor *sensor,
152         struct ipu_bridge *bridge,
153         const struct ipu_sensor_config *cfg)
154 {
155         u32 rotation;
156         enum v4l2_fwnode_orientation orientation;
157
158         rotation = ipu_bridge_parse_rotation(sensor);
159         orientation = ipu_bridge_parse_orientation(sensor->adev);
160
161         sensor->prop_names = prop_names;
162
163         sensor->local_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_IPU_ENDPOINT]);
164         sensor->remote_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_SENSOR_ENDPOINT]);
165
166         sensor->dev_properties[0] = PROPERTY_ENTRY_U32(
167                                         sensor->prop_names.clock_frequency,
168                                         sensor->ssdb.mclkspeed);
169         sensor->dev_properties[1] = PROPERTY_ENTRY_U32(
170                                         sensor->prop_names.rotation,
171                                         rotation);
172         sensor->dev_properties[2] = PROPERTY_ENTRY_U32(
173                                         sensor->prop_names.orientation,
174                                         orientation);
175         if (sensor->ssdb.vcmtype) {
176                 sensor->vcm_ref[0] =
177                         SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_VCM]);
178                 sensor->dev_properties[3] =
179                         PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor->vcm_ref);
180         }
181
182         sensor->ep_properties[0] = PROPERTY_ENTRY_U32(
183                                         sensor->prop_names.bus_type,
184                                         V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
185         sensor->ep_properties[1] = PROPERTY_ENTRY_U32_ARRAY_LEN(
186                                         sensor->prop_names.data_lanes,
187                                         bridge->data_lanes,
188                                         sensor->ssdb.lanes);
189         sensor->ep_properties[2] = PROPERTY_ENTRY_REF_ARRAY(
190                                         sensor->prop_names.remote_endpoint,
191                                         sensor->local_ref);
192
193         if (cfg->nr_link_freqs > 0)
194                 sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN(
195                         sensor->prop_names.link_frequencies,
196                         cfg->link_freqs,
197                         cfg->nr_link_freqs);
198
199         sensor->ipu_properties[0] = PROPERTY_ENTRY_U32_ARRAY_LEN(
200                                         sensor->prop_names.data_lanes,
201                                         bridge->data_lanes,
202                                         sensor->ssdb.lanes);
203         sensor->ipu_properties[1] = PROPERTY_ENTRY_REF_ARRAY(
204                                         sensor->prop_names.remote_endpoint,
205                                         sensor->remote_ref);
206 }
207
208 static void ipu_bridge_init_swnode_names(struct ipu_sensor *sensor)
209 {
210         snprintf(sensor->node_names.remote_port,
211                  sizeof(sensor->node_names.remote_port),
212                  SWNODE_GRAPH_PORT_NAME_FMT, sensor->ssdb.link);
213         snprintf(sensor->node_names.port,
214                  sizeof(sensor->node_names.port),
215                  SWNODE_GRAPH_PORT_NAME_FMT, 0); /* Always port 0 */
216         snprintf(sensor->node_names.endpoint,
217                  sizeof(sensor->node_names.endpoint),
218                  SWNODE_GRAPH_ENDPOINT_NAME_FMT, 0); /* And endpoint 0 */
219         if (sensor->ssdb.vcmtype) {
220                 /* append ssdb.link to distinguish nodes with same model VCM */
221                 snprintf(sensor->node_names.vcm, sizeof(sensor->node_names.vcm),
222                          "%s-%u", ipu_vcm_types[sensor->ssdb.vcmtype - 1],
223                          sensor->ssdb.link);
224         }
225 }
226
227 static void ipu_bridge_init_swnode_group(struct ipu_sensor *sensor)
228 {
229         struct software_node *nodes = sensor->swnodes;
230
231         sensor->group[SWNODE_SENSOR_HID] = &nodes[SWNODE_SENSOR_HID];
232         sensor->group[SWNODE_SENSOR_PORT] = &nodes[SWNODE_SENSOR_PORT];
233         sensor->group[SWNODE_SENSOR_ENDPOINT] = &nodes[SWNODE_SENSOR_ENDPOINT];
234         sensor->group[SWNODE_IPU_PORT] = &nodes[SWNODE_IPU_PORT];
235         sensor->group[SWNODE_IPU_ENDPOINT] = &nodes[SWNODE_IPU_ENDPOINT];
236         if (sensor->ssdb.vcmtype)
237                 sensor->group[SWNODE_VCM] =  &nodes[SWNODE_VCM];
238 }
239
240 static void ipu_bridge_create_connection_swnodes(struct ipu_bridge *bridge,
241                                                  struct ipu_sensor *sensor)
242 {
243         struct software_node *nodes = sensor->swnodes;
244
245         ipu_bridge_init_swnode_names(sensor);
246
247         nodes[SWNODE_SENSOR_HID] = NODE_SENSOR(sensor->name,
248                                                sensor->dev_properties);
249         nodes[SWNODE_SENSOR_PORT] = NODE_PORT(sensor->node_names.port,
250                                               &nodes[SWNODE_SENSOR_HID]);
251         nodes[SWNODE_SENSOR_ENDPOINT] = NODE_ENDPOINT(
252                                                 sensor->node_names.endpoint,
253                                                 &nodes[SWNODE_SENSOR_PORT],
254                                                 sensor->ep_properties);
255         nodes[SWNODE_IPU_PORT] = NODE_PORT(sensor->node_names.remote_port,
256                                            &bridge->ipu_hid_node);
257         nodes[SWNODE_IPU_ENDPOINT] = NODE_ENDPOINT(
258                                                 sensor->node_names.endpoint,
259                                                 &nodes[SWNODE_IPU_PORT],
260                                                 sensor->ipu_properties);
261         nodes[SWNODE_VCM] = NODE_VCM(sensor->node_names.vcm);
262
263         ipu_bridge_init_swnode_group(sensor);
264 }
265
266 static void ipu_bridge_instantiate_vcm_i2c_client(struct ipu_sensor *sensor)
267 {
268         struct i2c_board_info board_info = { };
269         char name[16];
270
271         if (!sensor->ssdb.vcmtype)
272                 return;
273
274         snprintf(name, sizeof(name), "%s-VCM", acpi_dev_name(sensor->adev));
275         board_info.dev_name = name;
276         strscpy(board_info.type, ipu_vcm_types[sensor->ssdb.vcmtype - 1],
277                 ARRAY_SIZE(board_info.type));
278         board_info.swnode = &sensor->swnodes[SWNODE_VCM];
279
280         sensor->vcm_i2c_client =
281                 i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(sensor->adev),
282                                               1, &board_info);
283         if (IS_ERR(sensor->vcm_i2c_client)) {
284                 dev_warn(&sensor->adev->dev, "Error instantiation VCM i2c-client: %ld\n",
285                          PTR_ERR(sensor->vcm_i2c_client));
286                 sensor->vcm_i2c_client = NULL;
287         }
288 }
289
290 static void ipu_bridge_unregister_sensors(struct ipu_bridge *bridge)
291 {
292         struct ipu_sensor *sensor;
293         unsigned int i;
294
295         for (i = 0; i < bridge->n_sensors; i++) {
296                 sensor = &bridge->sensors[i];
297                 software_node_unregister_node_group(sensor->group);
298                 acpi_dev_put(sensor->adev);
299                 i2c_unregister_device(sensor->vcm_i2c_client);
300         }
301 }
302
303 static int ipu_bridge_connect_sensor(const struct ipu_sensor_config *cfg,
304                                      struct ipu_bridge *bridge)
305 {
306         struct fwnode_handle *fwnode, *primary;
307         struct ipu_sensor *sensor;
308         struct acpi_device *adev;
309         int ret;
310
311         for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
312                 if (!adev->status.enabled)
313                         continue;
314
315                 if (bridge->n_sensors >= IPU_MAX_PORTS) {
316                         acpi_dev_put(adev);
317                         dev_err(bridge->dev, "Exceeded available IPU ports\n");
318                         return -EINVAL;
319                 }
320
321                 sensor = &bridge->sensors[bridge->n_sensors];
322                 /*
323                  * Borrow our adev ref to the sensor for now, on success
324                  * acpi_dev_get(adev) is done further below.
325                  */
326                 sensor->adev = adev;
327
328                 ret = ipu_bridge_read_acpi_buffer(adev, "SSDB",
329                                                   &sensor->ssdb,
330                                                   sizeof(sensor->ssdb));
331                 if (ret)
332                         goto err_put_adev;
333
334                 snprintf(sensor->name, sizeof(sensor->name), "%s-%u",
335                          cfg->hid, sensor->ssdb.link);
336
337                 if (sensor->ssdb.vcmtype > ARRAY_SIZE(ipu_vcm_types)) {
338                         dev_warn(&adev->dev, "Unknown VCM type %d\n",
339                                  sensor->ssdb.vcmtype);
340                         sensor->ssdb.vcmtype = 0;
341                 }
342
343                 if (sensor->ssdb.lanes > IPU_MAX_LANES) {
344                         dev_err(&adev->dev,
345                                 "Number of lanes in SSDB is invalid\n");
346                         ret = -EINVAL;
347                         goto err_put_adev;
348                 }
349
350                 ipu_bridge_create_fwnode_properties(sensor, bridge, cfg);
351                 ipu_bridge_create_connection_swnodes(bridge, sensor);
352
353                 ret = software_node_register_node_group(sensor->group);
354                 if (ret)
355                         goto err_put_adev;
356
357                 fwnode = software_node_fwnode(&sensor->swnodes[
358                                                       SWNODE_SENSOR_HID]);
359                 if (!fwnode) {
360                         ret = -ENODEV;
361                         goto err_free_swnodes;
362                 }
363
364                 sensor->adev = acpi_dev_get(adev);
365
366                 primary = acpi_fwnode_handle(adev);
367                 primary->secondary = fwnode;
368
369                 ipu_bridge_instantiate_vcm_i2c_client(sensor);
370
371                 dev_info(bridge->dev, "Found supported sensor %s\n",
372                          acpi_dev_name(adev));
373
374                 bridge->n_sensors++;
375         }
376
377         return 0;
378
379 err_free_swnodes:
380         software_node_unregister_node_group(sensor->group);
381 err_put_adev:
382         acpi_dev_put(adev);
383         return ret;
384 }
385
386 static int ipu_bridge_connect_sensors(struct ipu_bridge *bridge)
387 {
388         unsigned int i;
389         int ret;
390
391         for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
392                 const struct ipu_sensor_config *cfg =
393                         &ipu_supported_sensors[i];
394
395                 ret = ipu_bridge_connect_sensor(cfg, bridge);
396                 if (ret)
397                         goto err_unregister_sensors;
398         }
399
400         return 0;
401
402 err_unregister_sensors:
403         ipu_bridge_unregister_sensors(bridge);
404         return ret;
405 }
406
407 /*
408  * The VCM cannot be probed until the PMIC is completely setup. We cannot rely
409  * on -EPROBE_DEFER for this, since the consumer<->supplier relations between
410  * the VCM and regulators/clks are not described in ACPI, instead they are
411  * passed as board-data to the PMIC drivers. Since -PROBE_DEFER does not work
412  * for the clks/regulators the VCM i2c-clients must not be instantiated until
413  * the PMIC is fully setup.
414  *
415  * The sensor/VCM ACPI device has an ACPI _DEP on the PMIC, check this using the
416  * acpi_dev_ready_for_enumeration() helper, like the i2c-core-acpi code does
417  * for the sensors.
418  */
419 static int ipu_bridge_sensors_are_ready(void)
420 {
421         struct acpi_device *adev;
422         bool ready = true;
423         unsigned int i;
424
425         for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) {
426                 const struct ipu_sensor_config *cfg =
427                         &ipu_supported_sensors[i];
428
429                 for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
430                         if (!adev->status.enabled)
431                                 continue;
432
433                         if (!acpi_dev_ready_for_enumeration(adev))
434                                 ready = false;
435                 }
436         }
437
438         return ready;
439 }
440
441 int ipu_bridge_init(struct device *dev)
442 {
443         struct fwnode_handle *fwnode;
444         struct ipu_bridge *bridge;
445         unsigned int i;
446         int ret;
447
448         if (!ipu_bridge_sensors_are_ready())
449                 return -EPROBE_DEFER;
450
451         bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
452         if (!bridge)
453                 return -ENOMEM;
454
455         strscpy(bridge->ipu_node_name, IPU_HID,
456                 sizeof(bridge->ipu_node_name));
457         bridge->ipu_hid_node.name = bridge->ipu_node_name;
458         bridge->dev = dev;
459
460         ret = software_node_register(&bridge->ipu_hid_node);
461         if (ret < 0) {
462                 dev_err(dev, "Failed to register the IPU HID node\n");
463                 goto err_free_bridge;
464         }
465
466         /*
467          * Map the lane arrangement, which is fixed for the IPU3 (meaning we
468          * only need one, rather than one per sensor). We include it as a
469          * member of the struct ipu_bridge rather than a global variable so
470          * that it survives if the module is unloaded along with the rest of
471          * the struct.
472          */
473         for (i = 0; i < IPU_MAX_LANES; i++)
474                 bridge->data_lanes[i] = i + 1;
475
476         ret = ipu_bridge_connect_sensors(bridge);
477         if (ret || bridge->n_sensors == 0)
478                 goto err_unregister_ipu;
479
480         dev_info(dev, "Connected %d cameras\n", bridge->n_sensors);
481
482         fwnode = software_node_fwnode(&bridge->ipu_hid_node);
483         if (!fwnode) {
484                 dev_err(dev, "Error getting fwnode from ipu software_node\n");
485                 ret = -ENODEV;
486                 goto err_unregister_sensors;
487         }
488
489         set_secondary_fwnode(dev, fwnode);
490
491         return 0;
492
493 err_unregister_sensors:
494         ipu_bridge_unregister_sensors(bridge);
495 err_unregister_ipu:
496         software_node_unregister(&bridge->ipu_hid_node);
497 err_free_bridge:
498         kfree(bridge);
499
500         return ret;
501 }
502 EXPORT_SYMBOL_NS_GPL(ipu_bridge_init, INTEL_IPU_BRIDGE);
503
504 MODULE_LICENSE("GPL");
505 MODULE_DESCRIPTION("Intel IPU Sensors Bridge driver");