Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  skl-nhlt.c - Intel SKL Platform NHLT parsing
0004  *
0005  *  Copyright (C) 2015 Intel Corp
0006  *  Author: Sanjiv Kumar <sanjiv.kumar@intel.com>
0007  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0008  *
0009  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0010  */
0011 #include <linux/pci.h>
0012 #include <sound/intel-nhlt.h>
0013 #include "skl.h"
0014 #include "skl-i2s.h"
0015 
0016 static void skl_nhlt_trim_space(char *trim)
0017 {
0018     char *s = trim;
0019     int cnt;
0020     int i;
0021 
0022     cnt = 0;
0023     for (i = 0; s[i]; i++) {
0024         if (!isspace(s[i]))
0025             s[cnt++] = s[i];
0026     }
0027 
0028     s[cnt] = '\0';
0029 }
0030 
0031 int skl_nhlt_update_topology_bin(struct skl_dev *skl)
0032 {
0033     struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
0034     struct hdac_bus *bus = skl_to_bus(skl);
0035     struct device *dev = bus->dev;
0036 
0037     dev_dbg(dev, "oem_id %.6s, oem_table_id %.8s oem_revision %d\n",
0038         nhlt->header.oem_id, nhlt->header.oem_table_id,
0039         nhlt->header.oem_revision);
0040 
0041     snprintf(skl->tplg_name, sizeof(skl->tplg_name), "%x-%.6s-%.8s-%d%s",
0042         skl->pci_id, nhlt->header.oem_id, nhlt->header.oem_table_id,
0043         nhlt->header.oem_revision, "-tplg.bin");
0044 
0045     skl_nhlt_trim_space(skl->tplg_name);
0046 
0047     return 0;
0048 }
0049 
0050 static ssize_t platform_id_show(struct device *dev,
0051                 struct device_attribute *attr, char *buf)
0052 {
0053     struct pci_dev *pci = to_pci_dev(dev);
0054     struct hdac_bus *bus = pci_get_drvdata(pci);
0055     struct skl_dev *skl = bus_to_skl(bus);
0056     struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
0057     char platform_id[32];
0058 
0059     sprintf(platform_id, "%x-%.6s-%.8s-%d", skl->pci_id,
0060             nhlt->header.oem_id, nhlt->header.oem_table_id,
0061             nhlt->header.oem_revision);
0062 
0063     skl_nhlt_trim_space(platform_id);
0064     return sprintf(buf, "%s\n", platform_id);
0065 }
0066 
0067 static DEVICE_ATTR_RO(platform_id);
0068 
0069 int skl_nhlt_create_sysfs(struct skl_dev *skl)
0070 {
0071     struct device *dev = &skl->pci->dev;
0072 
0073     if (sysfs_create_file(&dev->kobj, &dev_attr_platform_id.attr))
0074         dev_warn(dev, "Error creating sysfs entry\n");
0075 
0076     return 0;
0077 }
0078 
0079 void skl_nhlt_remove_sysfs(struct skl_dev *skl)
0080 {
0081     struct device *dev = &skl->pci->dev;
0082 
0083     if (skl->nhlt)
0084         sysfs_remove_file(&dev->kobj, &dev_attr_platform_id.attr);
0085 }
0086 
0087 /*
0088  * Queries NHLT for all the fmt configuration for a particular endpoint and
0089  * stores all possible rates supported in a rate table for the corresponding
0090  * sclk/sclkfs.
0091  */
0092 static void skl_get_ssp_clks(struct skl_dev *skl, struct skl_ssp_clk *ssp_clks,
0093                 struct nhlt_fmt *fmt, u8 id)
0094 {
0095     struct skl_i2s_config_blob_ext *i2s_config_ext;
0096     struct skl_i2s_config_blob_legacy *i2s_config;
0097     struct skl_clk_parent_src *parent;
0098     struct skl_ssp_clk *sclk, *sclkfs;
0099     struct nhlt_fmt_cfg *fmt_cfg;
0100     struct wav_fmt_ext *wav_fmt;
0101     unsigned long rate;
0102     int rate_index = 0;
0103     u16 channels, bps;
0104     u8 clk_src;
0105     int i, j;
0106     u32 fs;
0107 
0108     sclk = &ssp_clks[SKL_SCLK_OFS];
0109     sclkfs = &ssp_clks[SKL_SCLKFS_OFS];
0110 
0111     if (fmt->fmt_count == 0)
0112         return;
0113 
0114     fmt_cfg = (struct nhlt_fmt_cfg *)fmt->fmt_config;
0115     for (i = 0; i < fmt->fmt_count; i++) {
0116         struct nhlt_fmt_cfg *saved_fmt_cfg = fmt_cfg;
0117         bool present = false;
0118 
0119         wav_fmt = &saved_fmt_cfg->fmt_ext;
0120 
0121         channels = wav_fmt->fmt.channels;
0122         bps = wav_fmt->fmt.bits_per_sample;
0123         fs = wav_fmt->fmt.samples_per_sec;
0124 
0125         /*
0126          * In case of TDM configuration on a ssp, there can
0127          * be more than one blob in which channel masks are
0128          * different for each usecase for a specific rate and bps.
0129          * But the sclk rate will be generated for the total
0130          * number of channels used for that endpoint.
0131          *
0132          * So for the given fs and bps, choose blob which has
0133          * the superset of all channels for that endpoint and
0134          * derive the rate.
0135          */
0136         for (j = i; j < fmt->fmt_count; j++) {
0137             struct nhlt_fmt_cfg *tmp_fmt_cfg = fmt_cfg;
0138 
0139             wav_fmt = &tmp_fmt_cfg->fmt_ext;
0140             if ((fs == wav_fmt->fmt.samples_per_sec) &&
0141                (bps == wav_fmt->fmt.bits_per_sample)) {
0142                 channels = max_t(u16, channels,
0143                         wav_fmt->fmt.channels);
0144                 saved_fmt_cfg = tmp_fmt_cfg;
0145             }
0146             /* Move to the next nhlt_fmt_cfg */
0147             tmp_fmt_cfg = (struct nhlt_fmt_cfg *)(tmp_fmt_cfg->config.caps +
0148                                   tmp_fmt_cfg->config.size);
0149         }
0150 
0151         rate = channels * bps * fs;
0152 
0153         /* check if the rate is added already to the given SSP's sclk */
0154         for (j = 0; (j < SKL_MAX_CLK_RATES) &&
0155                 (sclk[id].rate_cfg[j].rate != 0); j++) {
0156             if (sclk[id].rate_cfg[j].rate == rate) {
0157                 present = true;
0158                 break;
0159             }
0160         }
0161 
0162         /* Fill rate and parent for sclk/sclkfs */
0163         if (!present) {
0164             struct nhlt_fmt_cfg *first_fmt_cfg;
0165 
0166             first_fmt_cfg = (struct nhlt_fmt_cfg *)fmt->fmt_config;
0167             i2s_config_ext = (struct skl_i2s_config_blob_ext *)
0168                         first_fmt_cfg->config.caps;
0169 
0170             /* MCLK Divider Source Select */
0171             if (is_legacy_blob(i2s_config_ext->hdr.sig)) {
0172                 i2s_config = ext_to_legacy_blob(i2s_config_ext);
0173                 clk_src = get_clk_src(i2s_config->mclk,
0174                         SKL_MNDSS_DIV_CLK_SRC_MASK);
0175             } else {
0176                 clk_src = get_clk_src(i2s_config_ext->mclk,
0177                         SKL_MNDSS_DIV_CLK_SRC_MASK);
0178             }
0179 
0180             parent = skl_get_parent_clk(clk_src);
0181 
0182             /* Move to the next nhlt_fmt_cfg */
0183             fmt_cfg = (struct nhlt_fmt_cfg *)(fmt_cfg->config.caps +
0184                               fmt_cfg->config.size);
0185             /*
0186              * Do not copy the config data if there is no parent
0187              * clock available for this clock source select
0188              */
0189             if (!parent)
0190                 continue;
0191 
0192             sclk[id].rate_cfg[rate_index].rate = rate;
0193             sclk[id].rate_cfg[rate_index].config = saved_fmt_cfg;
0194             sclkfs[id].rate_cfg[rate_index].rate = rate;
0195             sclkfs[id].rate_cfg[rate_index].config = saved_fmt_cfg;
0196             sclk[id].parent_name = parent->name;
0197             sclkfs[id].parent_name = parent->name;
0198 
0199             rate_index++;
0200         }
0201     }
0202 }
0203 
0204 static void skl_get_mclk(struct skl_dev *skl, struct skl_ssp_clk *mclk,
0205                 struct nhlt_fmt *fmt, u8 id)
0206 {
0207     struct skl_i2s_config_blob_ext *i2s_config_ext;
0208     struct skl_i2s_config_blob_legacy *i2s_config;
0209     struct nhlt_fmt_cfg *fmt_cfg;
0210     struct skl_clk_parent_src *parent;
0211     u32 clkdiv, div_ratio;
0212     u8 clk_src;
0213 
0214     fmt_cfg = (struct nhlt_fmt_cfg *)fmt->fmt_config;
0215     i2s_config_ext = (struct skl_i2s_config_blob_ext *)fmt_cfg->config.caps;
0216 
0217     /* MCLK Divider Source Select and divider */
0218     if (is_legacy_blob(i2s_config_ext->hdr.sig)) {
0219         i2s_config = ext_to_legacy_blob(i2s_config_ext);
0220         clk_src = get_clk_src(i2s_config->mclk,
0221                 SKL_MCLK_DIV_CLK_SRC_MASK);
0222         clkdiv = i2s_config->mclk.mdivr &
0223                 SKL_MCLK_DIV_RATIO_MASK;
0224     } else {
0225         clk_src = get_clk_src(i2s_config_ext->mclk,
0226                 SKL_MCLK_DIV_CLK_SRC_MASK);
0227         clkdiv = i2s_config_ext->mclk.mdivr[0] &
0228                 SKL_MCLK_DIV_RATIO_MASK;
0229     }
0230 
0231     /* bypass divider */
0232     div_ratio = 1;
0233 
0234     if (clkdiv != SKL_MCLK_DIV_RATIO_MASK)
0235         /* Divider is 2 + clkdiv */
0236         div_ratio = clkdiv + 2;
0237 
0238     /* Calculate MCLK rate from source using div value */
0239     parent = skl_get_parent_clk(clk_src);
0240     if (!parent)
0241         return;
0242 
0243     mclk[id].rate_cfg[0].rate = parent->rate/div_ratio;
0244     mclk[id].rate_cfg[0].config = fmt_cfg;
0245     mclk[id].parent_name = parent->name;
0246 }
0247 
0248 void skl_get_clks(struct skl_dev *skl, struct skl_ssp_clk *ssp_clks)
0249 {
0250     struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
0251     struct nhlt_endpoint *epnt;
0252     struct nhlt_fmt *fmt;
0253     int i;
0254     u8 id;
0255 
0256     epnt = (struct nhlt_endpoint *)nhlt->desc;
0257     for (i = 0; i < nhlt->endpoint_count; i++) {
0258         if (epnt->linktype == NHLT_LINK_SSP) {
0259             id = epnt->virtual_bus_id;
0260 
0261             fmt = (struct nhlt_fmt *)(epnt->config.caps
0262                     + epnt->config.size);
0263 
0264             skl_get_ssp_clks(skl, ssp_clks, fmt, id);
0265             skl_get_mclk(skl, ssp_clks, fmt, id);
0266         }
0267         epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
0268     }
0269 }