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