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