Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Author: Dan Scally <djrscally@gmail.com> */
0003 
0004 #include <linux/acpi.h>
0005 #include <linux/device.h>
0006 #include <linux/i2c.h>
0007 #include <linux/pci.h>
0008 #include <linux/property.h>
0009 #include <media/v4l2-fwnode.h>
0010 
0011 #include "cio2-bridge.h"
0012 
0013 /*
0014  * Extend this array with ACPI Hardware IDs of devices known to be working
0015  * plus the number of link-frequencies expected by their drivers, along with
0016  * the frequency values in hertz. This is somewhat opportunistic way of adding
0017  * support for this for now in the hopes of a better source for the information
0018  * (possibly some encoded value in the SSDB buffer that we're unaware of)
0019  * becoming apparent in the future.
0020  *
0021  * Do not add an entry for a sensor that is not actually supported.
0022  */
0023 static const struct cio2_sensor_config cio2_supported_sensors[] = {
0024     /* Omnivision OV5693 */
0025     CIO2_SENSOR_CONFIG("INT33BE", 1, 419200000),
0026     /* Omnivision OV8865 */
0027     CIO2_SENSOR_CONFIG("INT347A", 1, 360000000),
0028     /* Omnivision OV7251 */
0029     CIO2_SENSOR_CONFIG("INT347E", 1, 319200000),
0030     /* Omnivision OV2680 */
0031     CIO2_SENSOR_CONFIG("OVTI2680", 0),
0032 };
0033 
0034 static const struct cio2_property_names prop_names = {
0035     .clock_frequency = "clock-frequency",
0036     .rotation = "rotation",
0037     .orientation = "orientation",
0038     .bus_type = "bus-type",
0039     .data_lanes = "data-lanes",
0040     .remote_endpoint = "remote-endpoint",
0041     .link_frequencies = "link-frequencies",
0042 };
0043 
0044 static const char * const cio2_vcm_types[] = {
0045     "ad5823",
0046     "dw9714",
0047     "ad5816",
0048     "dw9719",
0049     "dw9718",
0050     "dw9806b",
0051     "wv517s",
0052     "lc898122xa",
0053     "lc898212axb",
0054 };
0055 
0056 static int cio2_bridge_read_acpi_buffer(struct acpi_device *adev, char *id,
0057                     void *data, u32 size)
0058 {
0059     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0060     union acpi_object *obj;
0061     acpi_status status;
0062     int ret = 0;
0063 
0064     status = acpi_evaluate_object(adev->handle, id, NULL, &buffer);
0065     if (ACPI_FAILURE(status))
0066         return -ENODEV;
0067 
0068     obj = buffer.pointer;
0069     if (!obj) {
0070         dev_err(&adev->dev, "Couldn't locate ACPI buffer\n");
0071         return -ENODEV;
0072     }
0073 
0074     if (obj->type != ACPI_TYPE_BUFFER) {
0075         dev_err(&adev->dev, "Not an ACPI buffer\n");
0076         ret = -ENODEV;
0077         goto out_free_buff;
0078     }
0079 
0080     if (obj->buffer.length > size) {
0081         dev_err(&adev->dev, "Given buffer is too small\n");
0082         ret = -EINVAL;
0083         goto out_free_buff;
0084     }
0085 
0086     memcpy(data, obj->buffer.pointer, obj->buffer.length);
0087 
0088 out_free_buff:
0089     kfree(buffer.pointer);
0090     return ret;
0091 }
0092 
0093 static u32 cio2_bridge_parse_rotation(struct cio2_sensor *sensor)
0094 {
0095     switch (sensor->ssdb.degree) {
0096     case CIO2_SENSOR_ROTATION_NORMAL:
0097         return 0;
0098     case CIO2_SENSOR_ROTATION_INVERTED:
0099         return 180;
0100     default:
0101         dev_warn(&sensor->adev->dev,
0102              "Unknown rotation %d. Assume 0 degree rotation\n",
0103              sensor->ssdb.degree);
0104         return 0;
0105     }
0106 }
0107 
0108 static enum v4l2_fwnode_orientation cio2_bridge_parse_orientation(struct cio2_sensor *sensor)
0109 {
0110     switch (sensor->pld->panel) {
0111     case ACPI_PLD_PANEL_FRONT:
0112         return V4L2_FWNODE_ORIENTATION_FRONT;
0113     case ACPI_PLD_PANEL_BACK:
0114         return V4L2_FWNODE_ORIENTATION_BACK;
0115     case ACPI_PLD_PANEL_TOP:
0116     case ACPI_PLD_PANEL_LEFT:
0117     case ACPI_PLD_PANEL_RIGHT:
0118     case ACPI_PLD_PANEL_UNKNOWN:
0119         return V4L2_FWNODE_ORIENTATION_EXTERNAL;
0120     default:
0121         dev_warn(&sensor->adev->dev, "Unknown _PLD panel value %d\n",
0122              sensor->pld->panel);
0123         return V4L2_FWNODE_ORIENTATION_EXTERNAL;
0124     }
0125 }
0126 
0127 static void cio2_bridge_create_fwnode_properties(
0128     struct cio2_sensor *sensor,
0129     struct cio2_bridge *bridge,
0130     const struct cio2_sensor_config *cfg)
0131 {
0132     u32 rotation;
0133     enum v4l2_fwnode_orientation orientation;
0134 
0135     rotation = cio2_bridge_parse_rotation(sensor);
0136     orientation = cio2_bridge_parse_orientation(sensor);
0137 
0138     sensor->prop_names = prop_names;
0139 
0140     sensor->local_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_CIO2_ENDPOINT]);
0141     sensor->remote_ref[0] = SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_SENSOR_ENDPOINT]);
0142 
0143     sensor->dev_properties[0] = PROPERTY_ENTRY_U32(
0144                     sensor->prop_names.clock_frequency,
0145                     sensor->ssdb.mclkspeed);
0146     sensor->dev_properties[1] = PROPERTY_ENTRY_U32(
0147                     sensor->prop_names.rotation,
0148                     rotation);
0149     sensor->dev_properties[2] = PROPERTY_ENTRY_U32(
0150                     sensor->prop_names.orientation,
0151                     orientation);
0152     if (sensor->ssdb.vcmtype) {
0153         sensor->vcm_ref[0] =
0154             SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_VCM]);
0155         sensor->dev_properties[3] =
0156             PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor->vcm_ref);
0157     }
0158 
0159     sensor->ep_properties[0] = PROPERTY_ENTRY_U32(
0160                     sensor->prop_names.bus_type,
0161                     V4L2_FWNODE_BUS_TYPE_CSI2_DPHY);
0162     sensor->ep_properties[1] = PROPERTY_ENTRY_U32_ARRAY_LEN(
0163                     sensor->prop_names.data_lanes,
0164                     bridge->data_lanes,
0165                     sensor->ssdb.lanes);
0166     sensor->ep_properties[2] = PROPERTY_ENTRY_REF_ARRAY(
0167                     sensor->prop_names.remote_endpoint,
0168                     sensor->local_ref);
0169 
0170     if (cfg->nr_link_freqs > 0)
0171         sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN(
0172             sensor->prop_names.link_frequencies,
0173             cfg->link_freqs,
0174             cfg->nr_link_freqs);
0175 
0176     sensor->cio2_properties[0] = PROPERTY_ENTRY_U32_ARRAY_LEN(
0177                     sensor->prop_names.data_lanes,
0178                     bridge->data_lanes,
0179                     sensor->ssdb.lanes);
0180     sensor->cio2_properties[1] = PROPERTY_ENTRY_REF_ARRAY(
0181                     sensor->prop_names.remote_endpoint,
0182                     sensor->remote_ref);
0183 }
0184 
0185 static void cio2_bridge_init_swnode_names(struct cio2_sensor *sensor)
0186 {
0187     snprintf(sensor->node_names.remote_port,
0188          sizeof(sensor->node_names.remote_port),
0189          SWNODE_GRAPH_PORT_NAME_FMT, sensor->ssdb.link);
0190     snprintf(sensor->node_names.port,
0191          sizeof(sensor->node_names.port),
0192          SWNODE_GRAPH_PORT_NAME_FMT, 0); /* Always port 0 */
0193     snprintf(sensor->node_names.endpoint,
0194          sizeof(sensor->node_names.endpoint),
0195          SWNODE_GRAPH_ENDPOINT_NAME_FMT, 0); /* And endpoint 0 */
0196 }
0197 
0198 static void cio2_bridge_create_connection_swnodes(struct cio2_bridge *bridge,
0199                           struct cio2_sensor *sensor)
0200 {
0201     struct software_node *nodes = sensor->swnodes;
0202 
0203     cio2_bridge_init_swnode_names(sensor);
0204 
0205     nodes[SWNODE_SENSOR_HID] = NODE_SENSOR(sensor->name,
0206                            sensor->dev_properties);
0207     nodes[SWNODE_SENSOR_PORT] = NODE_PORT(sensor->node_names.port,
0208                           &nodes[SWNODE_SENSOR_HID]);
0209     nodes[SWNODE_SENSOR_ENDPOINT] = NODE_ENDPOINT(
0210                         sensor->node_names.endpoint,
0211                         &nodes[SWNODE_SENSOR_PORT],
0212                         sensor->ep_properties);
0213     nodes[SWNODE_CIO2_PORT] = NODE_PORT(sensor->node_names.remote_port,
0214                         &bridge->cio2_hid_node);
0215     nodes[SWNODE_CIO2_ENDPOINT] = NODE_ENDPOINT(
0216                         sensor->node_names.endpoint,
0217                         &nodes[SWNODE_CIO2_PORT],
0218                         sensor->cio2_properties);
0219     if (sensor->ssdb.vcmtype)
0220         nodes[SWNODE_VCM] =
0221             NODE_VCM(cio2_vcm_types[sensor->ssdb.vcmtype - 1]);
0222 }
0223 
0224 static void cio2_bridge_instantiate_vcm_i2c_client(struct cio2_sensor *sensor)
0225 {
0226     struct i2c_board_info board_info = { };
0227     char name[16];
0228 
0229     if (!sensor->ssdb.vcmtype)
0230         return;
0231 
0232     snprintf(name, sizeof(name), "%s-VCM", acpi_dev_name(sensor->adev));
0233     board_info.dev_name = name;
0234     strscpy(board_info.type, cio2_vcm_types[sensor->ssdb.vcmtype - 1],
0235         ARRAY_SIZE(board_info.type));
0236     board_info.swnode = &sensor->swnodes[SWNODE_VCM];
0237 
0238     sensor->vcm_i2c_client =
0239         i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(sensor->adev),
0240                           1, &board_info);
0241     if (IS_ERR(sensor->vcm_i2c_client)) {
0242         dev_warn(&sensor->adev->dev, "Error instantiation VCM i2c-client: %ld\n",
0243              PTR_ERR(sensor->vcm_i2c_client));
0244         sensor->vcm_i2c_client = NULL;
0245     }
0246 }
0247 
0248 static void cio2_bridge_unregister_sensors(struct cio2_bridge *bridge)
0249 {
0250     struct cio2_sensor *sensor;
0251     unsigned int i;
0252 
0253     for (i = 0; i < bridge->n_sensors; i++) {
0254         sensor = &bridge->sensors[i];
0255         software_node_unregister_nodes(sensor->swnodes);
0256         ACPI_FREE(sensor->pld);
0257         acpi_dev_put(sensor->adev);
0258         i2c_unregister_device(sensor->vcm_i2c_client);
0259     }
0260 }
0261 
0262 static int cio2_bridge_connect_sensor(const struct cio2_sensor_config *cfg,
0263                       struct cio2_bridge *bridge,
0264                       struct pci_dev *cio2)
0265 {
0266     struct fwnode_handle *fwnode;
0267     struct cio2_sensor *sensor;
0268     struct acpi_device *adev;
0269     acpi_status status;
0270     int ret;
0271 
0272     for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
0273         if (!adev->status.enabled)
0274             continue;
0275 
0276         if (bridge->n_sensors >= CIO2_NUM_PORTS) {
0277             acpi_dev_put(adev);
0278             dev_err(&cio2->dev, "Exceeded available CIO2 ports\n");
0279             return -EINVAL;
0280         }
0281 
0282         sensor = &bridge->sensors[bridge->n_sensors];
0283         strscpy(sensor->name, cfg->hid, sizeof(sensor->name));
0284 
0285         ret = cio2_bridge_read_acpi_buffer(adev, "SSDB",
0286                            &sensor->ssdb,
0287                            sizeof(sensor->ssdb));
0288         if (ret)
0289             goto err_put_adev;
0290 
0291         if (sensor->ssdb.vcmtype > ARRAY_SIZE(cio2_vcm_types)) {
0292             dev_warn(&adev->dev, "Unknown VCM type %d\n",
0293                  sensor->ssdb.vcmtype);
0294             sensor->ssdb.vcmtype = 0;
0295         }
0296 
0297         status = acpi_get_physical_device_location(adev->handle, &sensor->pld);
0298         if (ACPI_FAILURE(status)) {
0299             ret = -ENODEV;
0300             goto err_put_adev;
0301         }
0302 
0303         if (sensor->ssdb.lanes > CIO2_MAX_LANES) {
0304             dev_err(&adev->dev,
0305                 "Number of lanes in SSDB is invalid\n");
0306             ret = -EINVAL;
0307             goto err_free_pld;
0308         }
0309 
0310         cio2_bridge_create_fwnode_properties(sensor, bridge, cfg);
0311         cio2_bridge_create_connection_swnodes(bridge, sensor);
0312 
0313         ret = software_node_register_nodes(sensor->swnodes);
0314         if (ret)
0315             goto err_free_pld;
0316 
0317         fwnode = software_node_fwnode(&sensor->swnodes[
0318                               SWNODE_SENSOR_HID]);
0319         if (!fwnode) {
0320             ret = -ENODEV;
0321             goto err_free_swnodes;
0322         }
0323 
0324         sensor->adev = acpi_dev_get(adev);
0325         adev->fwnode.secondary = fwnode;
0326 
0327         cio2_bridge_instantiate_vcm_i2c_client(sensor);
0328 
0329         dev_info(&cio2->dev, "Found supported sensor %s\n",
0330              acpi_dev_name(adev));
0331 
0332         bridge->n_sensors++;
0333     }
0334 
0335     return 0;
0336 
0337 err_free_swnodes:
0338     software_node_unregister_nodes(sensor->swnodes);
0339 err_free_pld:
0340     ACPI_FREE(sensor->pld);
0341 err_put_adev:
0342     acpi_dev_put(adev);
0343     return ret;
0344 }
0345 
0346 static int cio2_bridge_connect_sensors(struct cio2_bridge *bridge,
0347                        struct pci_dev *cio2)
0348 {
0349     unsigned int i;
0350     int ret;
0351 
0352     for (i = 0; i < ARRAY_SIZE(cio2_supported_sensors); i++) {
0353         const struct cio2_sensor_config *cfg =
0354             &cio2_supported_sensors[i];
0355 
0356         ret = cio2_bridge_connect_sensor(cfg, bridge, cio2);
0357         if (ret)
0358             goto err_unregister_sensors;
0359     }
0360 
0361     return 0;
0362 
0363 err_unregister_sensors:
0364     cio2_bridge_unregister_sensors(bridge);
0365     return ret;
0366 }
0367 
0368 /*
0369  * The VCM cannot be probed until the PMIC is completely setup. We cannot rely
0370  * on -EPROBE_DEFER for this, since the consumer<->supplier relations between
0371  * the VCM and regulators/clks are not described in ACPI, instead they are
0372  * passed as board-data to the PMIC drivers. Since -PROBE_DEFER does not work
0373  * for the clks/regulators the VCM i2c-clients must not be instantiated until
0374  * the PMIC is fully setup.
0375  *
0376  * The sensor/VCM ACPI device has an ACPI _DEP on the PMIC, check this using the
0377  * acpi_dev_ready_for_enumeration() helper, like the i2c-core-acpi code does
0378  * for the sensors.
0379  */
0380 static int cio2_bridge_sensors_are_ready(void)
0381 {
0382     struct acpi_device *adev;
0383     bool ready = true;
0384     unsigned int i;
0385 
0386     for (i = 0; i < ARRAY_SIZE(cio2_supported_sensors); i++) {
0387         const struct cio2_sensor_config *cfg =
0388             &cio2_supported_sensors[i];
0389 
0390         for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) {
0391             if (!adev->status.enabled)
0392                 continue;
0393 
0394             if (!acpi_dev_ready_for_enumeration(adev))
0395                 ready = false;
0396         }
0397     }
0398 
0399     return ready;
0400 }
0401 
0402 int cio2_bridge_init(struct pci_dev *cio2)
0403 {
0404     struct device *dev = &cio2->dev;
0405     struct fwnode_handle *fwnode;
0406     struct cio2_bridge *bridge;
0407     unsigned int i;
0408     int ret;
0409 
0410     if (!cio2_bridge_sensors_are_ready())
0411         return -EPROBE_DEFER;
0412 
0413     bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
0414     if (!bridge)
0415         return -ENOMEM;
0416 
0417     strscpy(bridge->cio2_node_name, CIO2_HID,
0418         sizeof(bridge->cio2_node_name));
0419     bridge->cio2_hid_node.name = bridge->cio2_node_name;
0420 
0421     ret = software_node_register(&bridge->cio2_hid_node);
0422     if (ret < 0) {
0423         dev_err(dev, "Failed to register the CIO2 HID node\n");
0424         goto err_free_bridge;
0425     }
0426 
0427     /*
0428      * Map the lane arrangement, which is fixed for the IPU3 (meaning we
0429      * only need one, rather than one per sensor). We include it as a
0430      * member of the struct cio2_bridge rather than a global variable so
0431      * that it survives if the module is unloaded along with the rest of
0432      * the struct.
0433      */
0434     for (i = 0; i < CIO2_MAX_LANES; i++)
0435         bridge->data_lanes[i] = i + 1;
0436 
0437     ret = cio2_bridge_connect_sensors(bridge, cio2);
0438     if (ret || bridge->n_sensors == 0)
0439         goto err_unregister_cio2;
0440 
0441     dev_info(dev, "Connected %d cameras\n", bridge->n_sensors);
0442 
0443     fwnode = software_node_fwnode(&bridge->cio2_hid_node);
0444     if (!fwnode) {
0445         dev_err(dev, "Error getting fwnode from cio2 software_node\n");
0446         ret = -ENODEV;
0447         goto err_unregister_sensors;
0448     }
0449 
0450     set_secondary_fwnode(dev, fwnode);
0451 
0452     return 0;
0453 
0454 err_unregister_sensors:
0455     cio2_bridge_unregister_sensors(bridge);
0456 err_unregister_cio2:
0457     software_node_unregister(&bridge->cio2_hid_node);
0458 err_free_bridge:
0459     kfree(bridge);
0460 
0461     return ret;
0462 }