Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
0002 // Copyright(c) 2015-2021 Intel Corporation.
0003 
0004 /*
0005  * SDW Intel ACPI scan helpers
0006  */
0007 
0008 #include <linux/acpi.h>
0009 #include <linux/bits.h>
0010 #include <linux/bitfield.h>
0011 #include <linux/device.h>
0012 #include <linux/errno.h>
0013 #include <linux/export.h>
0014 #include <linux/fwnode.h>
0015 #include <linux/module.h>
0016 #include <linux/soundwire/sdw_intel.h>
0017 #include <linux/string.h>
0018 
0019 #define SDW_LINK_TYPE       4 /* from Intel ACPI documentation */
0020 #define SDW_MAX_LINKS       4
0021 
0022 static int ctrl_link_mask;
0023 module_param_named(sdw_link_mask, ctrl_link_mask, int, 0444);
0024 MODULE_PARM_DESC(sdw_link_mask, "Intel link mask (one bit per link)");
0025 
0026 static bool is_link_enabled(struct fwnode_handle *fw_node, int i)
0027 {
0028     struct fwnode_handle *link;
0029     char name[32];
0030     u32 quirk_mask = 0;
0031 
0032     /* Find master handle */
0033     snprintf(name, sizeof(name),
0034          "mipi-sdw-link-%d-subproperties", i);
0035 
0036     link = fwnode_get_named_child_node(fw_node, name);
0037     if (!link)
0038         return false;
0039 
0040     fwnode_property_read_u32(link,
0041                  "intel-quirk-mask",
0042                  &quirk_mask);
0043 
0044     if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
0045         return false;
0046 
0047     return true;
0048 }
0049 
0050 static int
0051 sdw_intel_scan_controller(struct sdw_intel_acpi_info *info)
0052 {
0053     struct acpi_device *adev = acpi_fetch_acpi_dev(info->handle);
0054     int ret, i;
0055     u8 count;
0056 
0057     if (!adev)
0058         return -EINVAL;
0059 
0060     /* Found controller, find links supported */
0061     count = 0;
0062     ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
0063                         "mipi-sdw-master-count", &count, 1);
0064 
0065     /*
0066      * In theory we could check the number of links supported in
0067      * hardware, but in that step we cannot assume SoundWire IP is
0068      * powered.
0069      *
0070      * In addition, if the BIOS doesn't even provide this
0071      * 'master-count' property then all the inits based on link
0072      * masks will fail as well.
0073      *
0074      * We will check the hardware capabilities in the startup() step
0075      */
0076 
0077     if (ret) {
0078         dev_err(&adev->dev,
0079             "Failed to read mipi-sdw-master-count: %d\n", ret);
0080         return -EINVAL;
0081     }
0082 
0083     /* Check count is within bounds */
0084     if (count > SDW_MAX_LINKS) {
0085         dev_err(&adev->dev, "Link count %d exceeds max %d\n",
0086             count, SDW_MAX_LINKS);
0087         return -EINVAL;
0088     }
0089 
0090     if (!count) {
0091         dev_warn(&adev->dev, "No SoundWire links detected\n");
0092         return -EINVAL;
0093     }
0094     dev_dbg(&adev->dev, "ACPI reports %d SDW Link devices\n", count);
0095 
0096     info->count = count;
0097     info->link_mask = 0;
0098 
0099     for (i = 0; i < count; i++) {
0100         if (ctrl_link_mask && !(ctrl_link_mask & BIT(i))) {
0101             dev_dbg(&adev->dev,
0102                 "Link %d masked, will not be enabled\n", i);
0103             continue;
0104         }
0105 
0106         if (!is_link_enabled(acpi_fwnode_handle(adev), i)) {
0107             dev_dbg(&adev->dev,
0108                 "Link %d not selected in firmware\n", i);
0109             continue;
0110         }
0111 
0112         info->link_mask |= BIT(i);
0113     }
0114 
0115     return 0;
0116 }
0117 
0118 static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level,
0119                      void *cdata, void **return_value)
0120 {
0121     struct sdw_intel_acpi_info *info = cdata;
0122     acpi_status status;
0123     u64 adr;
0124 
0125     status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
0126     if (ACPI_FAILURE(status))
0127         return AE_OK; /* keep going */
0128 
0129     if (!acpi_fetch_acpi_dev(handle)) {
0130         pr_err("%s: Couldn't find ACPI handle\n", __func__);
0131         return AE_NOT_FOUND;
0132     }
0133 
0134     /*
0135      * On some Intel platforms, multiple children of the HDAS
0136      * device can be found, but only one of them is the SoundWire
0137      * controller. The SNDW device is always exposed with
0138      * Name(_ADR, 0x40000000), with bits 31..28 representing the
0139      * SoundWire link so filter accordingly
0140      */
0141     if (FIELD_GET(GENMASK(31, 28), adr) != SDW_LINK_TYPE)
0142         return AE_OK; /* keep going */
0143 
0144     /* found the correct SoundWire controller */
0145     info->handle = handle;
0146 
0147     /* device found, stop namespace walk */
0148     return AE_CTRL_TERMINATE;
0149 }
0150 
0151 /**
0152  * sdw_intel_acpi_scan() - SoundWire Intel init routine
0153  * @parent_handle: ACPI parent handle
0154  * @info: description of what firmware/DSDT tables expose
0155  *
0156  * This scans the namespace and queries firmware to figure out which
0157  * links to enable. A follow-up use of sdw_intel_probe() and
0158  * sdw_intel_startup() is required for creation of devices and bus
0159  * startup
0160  */
0161 int sdw_intel_acpi_scan(acpi_handle *parent_handle,
0162             struct sdw_intel_acpi_info *info)
0163 {
0164     acpi_status status;
0165 
0166     info->handle = NULL;
0167     /*
0168      * In the HDAS ACPI scope, 'SNDW' may be either the child of
0169      * 'HDAS' or the grandchild of 'HDAS'. So let's go through
0170      * the ACPI from 'HDAS' at max depth of 2 to find the 'SNDW'
0171      * device.
0172      */
0173     status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
0174                      parent_handle, 2,
0175                      sdw_intel_acpi_cb,
0176                      NULL, info, NULL);
0177     if (ACPI_FAILURE(status) || info->handle == NULL)
0178         return -ENODEV;
0179 
0180     return sdw_intel_scan_controller(info);
0181 }
0182 EXPORT_SYMBOL_NS(sdw_intel_acpi_scan, SND_INTEL_SOUNDWIRE_ACPI);
0183 
0184 MODULE_LICENSE("Dual BSD/GPL");
0185 MODULE_DESCRIPTION("Intel Soundwire ACPI helpers");