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