Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
0002 //
0003 // This file is provided under a dual BSD/GPLv2 license.  When using or
0004 // redistributing this file, you may do so under either license.
0005 //
0006 // Copyright(c) 2018 Intel Corporation. All rights reserved.
0007 //
0008 // Authors: Keyon Jie <yang.jie@linux.intel.com>
0009 //
0010 
0011 #include <linux/module.h>
0012 #include <sound/hdaudio_ext.h>
0013 #include <sound/hda_register.h>
0014 #include <sound/hda_codec.h>
0015 #include <sound/hda_i915.h>
0016 #include <sound/sof.h>
0017 #include "../ops.h"
0018 #include "hda.h"
0019 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
0020 #include "../../codecs/hdac_hda.h"
0021 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
0022 
0023 #define CODEC_PROBE_RETRIES 3
0024 
0025 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
0026 #define IDISP_VID_INTEL 0x80860000
0027 
0028 /* load the legacy HDA codec driver */
0029 static int request_codec_module(struct hda_codec *codec)
0030 {
0031 #ifdef MODULE
0032     char alias[MODULE_NAME_LEN];
0033     const char *mod = NULL;
0034 
0035     switch (codec->probe_id) {
0036     case HDA_CODEC_ID_GENERIC:
0037 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
0038         mod = "snd-hda-codec-generic";
0039 #endif
0040         break;
0041     default:
0042         snd_hdac_codec_modalias(&codec->core, alias, sizeof(alias));
0043         mod = alias;
0044         break;
0045     }
0046 
0047     if (mod) {
0048         dev_dbg(&codec->core.dev, "loading codec module: %s\n", mod);
0049         request_module(mod);
0050     }
0051 #endif /* MODULE */
0052     return device_attach(hda_codec_dev(codec));
0053 }
0054 
0055 static int hda_codec_load_module(struct hda_codec *codec)
0056 {
0057     int ret = request_codec_module(codec);
0058 
0059     if (ret <= 0) {
0060         codec->probe_id = HDA_CODEC_ID_GENERIC;
0061         ret = request_codec_module(codec);
0062     }
0063 
0064     return ret;
0065 }
0066 
0067 /* enable controller wake up event for all codecs with jack connectors */
0068 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev, bool enable)
0069 {
0070     struct hda_bus *hbus = sof_to_hbus(sdev);
0071     struct hdac_bus *bus = sof_to_bus(sdev);
0072     struct hda_codec *codec;
0073     unsigned int mask = 0;
0074 
0075     if (enable) {
0076         list_for_each_codec(codec, hbus)
0077             if (codec->jacktbl.used)
0078                 mask |= BIT(codec->core.addr);
0079     }
0080 
0081     snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, mask);
0082 }
0083 
0084 /* check jack status after resuming from suspend mode */
0085 void hda_codec_jack_check(struct snd_sof_dev *sdev)
0086 {
0087     struct hda_bus *hbus = sof_to_hbus(sdev);
0088     struct hda_codec *codec;
0089 
0090     list_for_each_codec(codec, hbus)
0091         /*
0092          * Wake up all jack-detecting codecs regardless whether an event
0093          * has been recorded in STATESTS
0094          */
0095         if (codec->jacktbl.used)
0096             pm_request_resume(&codec->core.dev);
0097 }
0098 #else
0099 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev, bool enable) {}
0100 void hda_codec_jack_check(struct snd_sof_dev *sdev) {}
0101 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
0102 EXPORT_SYMBOL_NS(hda_codec_jack_wake_enable, SND_SOC_SOF_HDA_AUDIO_CODEC);
0103 EXPORT_SYMBOL_NS(hda_codec_jack_check, SND_SOC_SOF_HDA_AUDIO_CODEC);
0104 
0105 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
0106 #define is_generic_config(bus) \
0107     ((bus)->modelname && !strcmp((bus)->modelname, "generic"))
0108 #else
0109 #define is_generic_config(x)    0
0110 #endif
0111 
0112 /* probe individual codec */
0113 static int hda_codec_probe(struct snd_sof_dev *sdev, int address,
0114                bool hda_codec_use_common_hdmi)
0115 {
0116 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
0117     struct hdac_hda_priv *hda_priv;
0118     struct hda_codec *codec;
0119     int type = HDA_DEV_LEGACY;
0120 #endif
0121     struct hda_bus *hbus = sof_to_hbus(sdev);
0122     struct hdac_device *hdev;
0123     u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) |
0124         (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
0125     u32 resp = -1;
0126     int ret, retry = 0;
0127 
0128     do {
0129         mutex_lock(&hbus->core.cmd_mutex);
0130         snd_hdac_bus_send_cmd(&hbus->core, hda_cmd);
0131         snd_hdac_bus_get_response(&hbus->core, address, &resp);
0132         mutex_unlock(&hbus->core.cmd_mutex);
0133     } while (resp == -1 && retry++ < CODEC_PROBE_RETRIES);
0134 
0135     if (resp == -1)
0136         return -EIO;
0137     dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
0138         address, resp);
0139 
0140 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
0141     hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
0142     if (!hda_priv)
0143         return -ENOMEM;
0144 
0145     hda_priv->codec.bus = hbus;
0146     hdev = &hda_priv->codec.core;
0147     codec = &hda_priv->codec;
0148 
0149     /* only probe ASoC codec drivers for HDAC-HDMI */
0150     if (!hda_codec_use_common_hdmi && (resp & 0xFFFF0000) == IDISP_VID_INTEL)
0151         type = HDA_DEV_ASOC;
0152 
0153     ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev, type);
0154     if (ret < 0)
0155         return ret;
0156 
0157     if ((resp & 0xFFFF0000) == IDISP_VID_INTEL) {
0158         if (!hdev->bus->audio_component) {
0159             dev_dbg(sdev->dev,
0160                 "iDisp hw present but no driver\n");
0161             ret = -ENOENT;
0162             goto out;
0163         }
0164         hda_priv->need_display_power = true;
0165     }
0166 
0167     if (is_generic_config(hbus))
0168         codec->probe_id = HDA_CODEC_ID_GENERIC;
0169     else
0170         codec->probe_id = 0;
0171 
0172     if (type == HDA_DEV_LEGACY) {
0173         ret = hda_codec_load_module(codec);
0174         /*
0175          * handle ret==0 (no driver bound) as an error, but pass
0176          * other return codes without modification
0177          */
0178         if (ret == 0)
0179             ret = -ENOENT;
0180     }
0181 
0182 out:
0183     if (ret < 0) {
0184         snd_hdac_device_unregister(hdev);
0185         put_device(&hdev->dev);
0186     }
0187 #else
0188     hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
0189     if (!hdev)
0190         return -ENOMEM;
0191 
0192     ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev, HDA_DEV_ASOC);
0193 #endif
0194 
0195     return ret;
0196 }
0197 
0198 /* Codec initialization */
0199 void hda_codec_probe_bus(struct snd_sof_dev *sdev,
0200              bool hda_codec_use_common_hdmi)
0201 {
0202     struct hdac_bus *bus = sof_to_bus(sdev);
0203     int i, ret;
0204 
0205     /* probe codecs in avail slots */
0206     for (i = 0; i < HDA_MAX_CODECS; i++) {
0207 
0208         if (!(bus->codec_mask & (1 << i)))
0209             continue;
0210 
0211         ret = hda_codec_probe(sdev, i, hda_codec_use_common_hdmi);
0212         if (ret < 0) {
0213             dev_warn(bus->dev, "codec #%d probe error, ret: %d\n",
0214                  i, ret);
0215             bus->codec_mask &= ~BIT(i);
0216         }
0217     }
0218 }
0219 EXPORT_SYMBOL_NS(hda_codec_probe_bus, SND_SOC_SOF_HDA_AUDIO_CODEC);
0220 
0221 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) || \
0222     IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)
0223 
0224 void hda_codec_i915_display_power(struct snd_sof_dev *sdev, bool enable)
0225 {
0226     struct hdac_bus *bus = sof_to_bus(sdev);
0227 
0228     if (HDA_IDISP_CODEC(bus->codec_mask)) {
0229         dev_dbg(bus->dev, "Turning i915 HDAC power %d\n", enable);
0230         snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, enable);
0231     }
0232 }
0233 EXPORT_SYMBOL_NS(hda_codec_i915_display_power, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
0234 
0235 int hda_codec_i915_init(struct snd_sof_dev *sdev)
0236 {
0237     struct hdac_bus *bus = sof_to_bus(sdev);
0238     int ret;
0239 
0240     /* i915 exposes a HDA codec for HDMI audio */
0241     ret = snd_hdac_i915_init(bus);
0242     if (ret < 0)
0243         return ret;
0244 
0245     /* codec_mask not yet known, power up for probe */
0246     snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
0247 
0248     return 0;
0249 }
0250 EXPORT_SYMBOL_NS(hda_codec_i915_init, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
0251 
0252 int hda_codec_i915_exit(struct snd_sof_dev *sdev)
0253 {
0254     struct hdac_bus *bus = sof_to_bus(sdev);
0255 
0256     if (!bus->audio_component)
0257         return 0;
0258 
0259     /* power down unconditionally */
0260     snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
0261 
0262     return snd_hdac_i915_exit(bus);
0263 }
0264 EXPORT_SYMBOL_NS(hda_codec_i915_exit, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
0265 
0266 #endif
0267 
0268 MODULE_LICENSE("Dual BSD/GPL");