1 /* 2 * skl-nhlt.c - Intel SKL Platform NHLT parsing 3 * 4 * Copyright (C) 2015 Intel Corp 5 * Author: Sanjiv Kumar <sanjiv.kumar@intel.com> 6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; version 2 of the License. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 18 * 19 */ 20 #include <linux/pci.h> 21 #include "skl.h" 22 #include "skl-i2s.h" 23 24 #define NHLT_ACPI_HEADER_SIG "NHLT" 25 26 /* Unique identification for getting NHLT blobs */ 27 static guid_t osc_guid = 28 GUID_INIT(0xA69F886E, 0x6CEB, 0x4594, 29 0xA4, 0x1F, 0x7B, 0x5D, 0xCE, 0x24, 0xC5, 0x53); 30 31 32 struct nhlt_acpi_table *skl_nhlt_init(struct device *dev) 33 { 34 acpi_handle handle; 35 union acpi_object *obj; 36 struct nhlt_resource_desc *nhlt_ptr = NULL; 37 struct nhlt_acpi_table *nhlt_table = NULL; 38 39 handle = ACPI_HANDLE(dev); 40 if (!handle) { 41 dev_err(dev, "Didn't find ACPI_HANDLE\n"); 42 return NULL; 43 } 44 45 obj = acpi_evaluate_dsm(handle, &osc_guid, 1, 1, NULL); 46 if (obj && obj->type == ACPI_TYPE_BUFFER) { 47 nhlt_ptr = (struct nhlt_resource_desc *)obj->buffer.pointer; 48 if (nhlt_ptr->length) 49 nhlt_table = (struct nhlt_acpi_table *) 50 memremap(nhlt_ptr->min_addr, nhlt_ptr->length, 51 MEMREMAP_WB); 52 ACPI_FREE(obj); 53 if (nhlt_table && (strncmp(nhlt_table->header.signature, 54 NHLT_ACPI_HEADER_SIG, 55 strlen(NHLT_ACPI_HEADER_SIG)) != 0)) { 56 memunmap(nhlt_table); 57 dev_err(dev, "NHLT ACPI header signature incorrect\n"); 58 return NULL; 59 } 60 return nhlt_table; 61 } 62 63 dev_err(dev, "device specific method to extract NHLT blob failed\n"); 64 return NULL; 65 } 66 67 void skl_nhlt_free(struct nhlt_acpi_table *nhlt) 68 { 69 memunmap((void *) nhlt); 70 } 71 72 static struct nhlt_specific_cfg *skl_get_specific_cfg( 73 struct device *dev, struct nhlt_fmt *fmt, 74 u8 no_ch, u32 rate, u16 bps, u8 linktype) 75 { 76 struct nhlt_specific_cfg *sp_config; 77 struct wav_fmt *wfmt; 78 struct nhlt_fmt_cfg *fmt_config = fmt->fmt_config; 79 int i; 80 81 dev_dbg(dev, "Format count =%d\n", fmt->fmt_count); 82 83 for (i = 0; i < fmt->fmt_count; i++) { 84 wfmt = &fmt_config->fmt_ext.fmt; 85 dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", wfmt->channels, 86 wfmt->bits_per_sample, wfmt->samples_per_sec); 87 if (wfmt->channels == no_ch && wfmt->bits_per_sample == bps) { 88 /* 89 * if link type is dmic ignore rate check as the blob is 90 * generic for all rates 91 */ 92 sp_config = &fmt_config->config; 93 if (linktype == NHLT_LINK_DMIC) 94 return sp_config; 95 96 if (wfmt->samples_per_sec == rate) 97 return sp_config; 98 } 99 100 fmt_config = (struct nhlt_fmt_cfg *)(fmt_config->config.caps + 101 fmt_config->config.size); 102 } 103 104 return NULL; 105 } 106 107 static void dump_config(struct device *dev, u32 instance_id, u8 linktype, 108 u8 s_fmt, u8 num_channels, u32 s_rate, u8 dirn, u16 bps) 109 { 110 dev_dbg(dev, "Input configuration\n"); 111 dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", num_channels, s_fmt, s_rate); 112 dev_dbg(dev, "vbus_id=%d link_type=%d\n", instance_id, linktype); 113 dev_dbg(dev, "bits_per_sample=%d\n", bps); 114 } 115 116 static bool skl_check_ep_match(struct device *dev, struct nhlt_endpoint *epnt, 117 u32 instance_id, u8 link_type, u8 dirn, u8 dev_type) 118 { 119 dev_dbg(dev, "vbus_id=%d link_type=%d dir=%d dev_type = %d\n", 120 epnt->virtual_bus_id, epnt->linktype, 121 epnt->direction, epnt->device_type); 122 123 if ((epnt->virtual_bus_id == instance_id) && 124 (epnt->linktype == link_type) && 125 (epnt->direction == dirn)) { 126 /* do not check dev_type for DMIC link type */ 127 if (epnt->linktype == NHLT_LINK_DMIC) 128 return true; 129 130 if (epnt->device_type == dev_type) 131 return true; 132 } 133 134 return false; 135 } 136 137 struct nhlt_specific_cfg 138 *skl_get_ep_blob(struct skl *skl, u32 instance, u8 link_type, 139 u8 s_fmt, u8 num_ch, u32 s_rate, 140 u8 dirn, u8 dev_type) 141 { 142 struct nhlt_fmt *fmt; 143 struct nhlt_endpoint *epnt; 144 struct hdac_bus *bus = skl_to_bus(skl); 145 struct device *dev = bus->dev; 146 struct nhlt_specific_cfg *sp_config; 147 struct nhlt_acpi_table *nhlt = skl->nhlt; 148 u16 bps = (s_fmt == 16) ? 16 : 32; 149 u8 j; 150 151 dump_config(dev, instance, link_type, s_fmt, num_ch, s_rate, dirn, bps); 152 153 epnt = (struct nhlt_endpoint *)nhlt->desc; 154 155 dev_dbg(dev, "endpoint count =%d\n", nhlt->endpoint_count); 156 157 for (j = 0; j < nhlt->endpoint_count; j++) { 158 if (skl_check_ep_match(dev, epnt, instance, link_type, 159 dirn, dev_type)) { 160 fmt = (struct nhlt_fmt *)(epnt->config.caps + 161 epnt->config.size); 162 sp_config = skl_get_specific_cfg(dev, fmt, num_ch, 163 s_rate, bps, link_type); 164 if (sp_config) 165 return sp_config; 166 } 167 168 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length); 169 } 170 171 return NULL; 172 } 173 174 int skl_get_dmic_geo(struct skl *skl) 175 { 176 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt; 177 struct nhlt_endpoint *epnt; 178 struct nhlt_dmic_array_config *cfg; 179 struct device *dev = &skl->pci->dev; 180 unsigned int dmic_geo = 0; 181 u8 j; 182 183 if (!nhlt) 184 return 0; 185 186 epnt = (struct nhlt_endpoint *)nhlt->desc; 187 188 for (j = 0; j < nhlt->endpoint_count; j++) { 189 if (epnt->linktype == NHLT_LINK_DMIC) { 190 cfg = (struct nhlt_dmic_array_config *) 191 (epnt->config.caps); 192 switch (cfg->array_type) { 193 case NHLT_MIC_ARRAY_2CH_SMALL: 194 case NHLT_MIC_ARRAY_2CH_BIG: 195 dmic_geo |= MIC_ARRAY_2CH; 196 break; 197 198 case NHLT_MIC_ARRAY_4CH_1ST_GEOM: 199 case NHLT_MIC_ARRAY_4CH_L_SHAPED: 200 case NHLT_MIC_ARRAY_4CH_2ND_GEOM: 201 dmic_geo |= MIC_ARRAY_4CH; 202 break; 203 204 default: 205 dev_warn(dev, "undefined DMIC array_type 0x%0x\n", 206 cfg->array_type); 207 208 } 209 } 210 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length); 211 } 212 213 return dmic_geo; 214 } 215 216 static void skl_nhlt_trim_space(char *trim) 217 { 218 char *s = trim; 219 int cnt; 220 int i; 221 222 cnt = 0; 223 for (i = 0; s[i]; i++) { 224 if (!isspace(s[i])) 225 s[cnt++] = s[i]; 226 } 227 228 s[cnt] = '\0'; 229 } 230 231 int skl_nhlt_update_topology_bin(struct skl *skl) 232 { 233 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt; 234 struct hdac_bus *bus = skl_to_bus(skl); 235 struct device *dev = bus->dev; 236 237 dev_dbg(dev, "oem_id %.6s, oem_table_id %8s oem_revision %d\n", 238 nhlt->header.oem_id, nhlt->header.oem_table_id, 239 nhlt->header.oem_revision); 240 241 snprintf(skl->tplg_name, sizeof(skl->tplg_name), "%x-%.6s-%.8s-%d%s", 242 skl->pci_id, nhlt->header.oem_id, nhlt->header.oem_table_id, 243 nhlt->header.oem_revision, "-tplg.bin"); 244 245 skl_nhlt_trim_space(skl->tplg_name); 246 247 return 0; 248 } 249 250 static ssize_t skl_nhlt_platform_id_show(struct device *dev, 251 struct device_attribute *attr, char *buf) 252 { 253 struct pci_dev *pci = to_pci_dev(dev); 254 struct hdac_bus *bus = pci_get_drvdata(pci); 255 struct skl *skl = bus_to_skl(bus); 256 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt; 257 char platform_id[32]; 258 259 sprintf(platform_id, "%x-%.6s-%.8s-%d", skl->pci_id, 260 nhlt->header.oem_id, nhlt->header.oem_table_id, 261 nhlt->header.oem_revision); 262 263 skl_nhlt_trim_space(platform_id); 264 return sprintf(buf, "%s\n", platform_id); 265 } 266 267 static DEVICE_ATTR(platform_id, 0444, skl_nhlt_platform_id_show, NULL); 268 269 int skl_nhlt_create_sysfs(struct skl *skl) 270 { 271 struct device *dev = &skl->pci->dev; 272 273 if (sysfs_create_file(&dev->kobj, &dev_attr_platform_id.attr)) 274 dev_warn(dev, "Error creating sysfs entry\n"); 275 276 return 0; 277 } 278 279 void skl_nhlt_remove_sysfs(struct skl *skl) 280 { 281 struct device *dev = &skl->pci->dev; 282 283 sysfs_remove_file(&dev->kobj, &dev_attr_platform_id.attr); 284 } 285 286 /* 287 * Queries NHLT for all the fmt configuration for a particular endpoint and 288 * stores all possible rates supported in a rate table for the corresponding 289 * sclk/sclkfs. 290 */ 291 static void skl_get_ssp_clks(struct skl *skl, struct skl_ssp_clk *ssp_clks, 292 struct nhlt_fmt *fmt, u8 id) 293 { 294 struct skl_i2s_config_blob_ext *i2s_config_ext; 295 struct skl_i2s_config_blob_legacy *i2s_config; 296 struct skl_clk_parent_src *parent; 297 struct skl_ssp_clk *sclk, *sclkfs; 298 struct nhlt_fmt_cfg *fmt_cfg; 299 struct wav_fmt_ext *wav_fmt; 300 unsigned long rate = 0; 301 bool present = false; 302 int rate_index = 0; 303 u16 channels, bps; 304 u8 clk_src; 305 int i, j; 306 u32 fs; 307 308 sclk = &ssp_clks[SKL_SCLK_OFS]; 309 sclkfs = &ssp_clks[SKL_SCLKFS_OFS]; 310 311 if (fmt->fmt_count == 0) 312 return; 313 314 for (i = 0; i < fmt->fmt_count; i++) { 315 fmt_cfg = &fmt->fmt_config[i]; 316 wav_fmt = &fmt_cfg->fmt_ext; 317 318 channels = wav_fmt->fmt.channels; 319 bps = wav_fmt->fmt.bits_per_sample; 320 fs = wav_fmt->fmt.samples_per_sec; 321 322 /* 323 * In case of TDM configuration on a ssp, there can 324 * be more than one blob in which channel masks are 325 * different for each usecase for a specific rate and bps. 326 * But the sclk rate will be generated for the total 327 * number of channels used for that endpoint. 328 * 329 * So for the given fs and bps, choose blob which has 330 * the superset of all channels for that endpoint and 331 * derive the rate. 332 */ 333 for (j = i; j < fmt->fmt_count; j++) { 334 fmt_cfg = &fmt->fmt_config[j]; 335 wav_fmt = &fmt_cfg->fmt_ext; 336 if ((fs == wav_fmt->fmt.samples_per_sec) && 337 (bps == wav_fmt->fmt.bits_per_sample)) 338 channels = max_t(u16, channels, 339 wav_fmt->fmt.channels); 340 } 341 342 rate = channels * bps * fs; 343 344 /* check if the rate is added already to the given SSP's sclk */ 345 for (j = 0; (j < SKL_MAX_CLK_RATES) && 346 (sclk[id].rate_cfg[j].rate != 0); j++) { 347 if (sclk[id].rate_cfg[j].rate == rate) { 348 present = true; 349 break; 350 } 351 } 352 353 /* Fill rate and parent for sclk/sclkfs */ 354 if (!present) { 355 i2s_config_ext = (struct skl_i2s_config_blob_ext *) 356 fmt->fmt_config[0].config.caps; 357 358 /* MCLK Divider Source Select */ 359 if (is_legacy_blob(i2s_config_ext->hdr.sig)) { 360 i2s_config = ext_to_legacy_blob(i2s_config_ext); 361 clk_src = get_clk_src(i2s_config->mclk, 362 SKL_MNDSS_DIV_CLK_SRC_MASK); 363 } else { 364 clk_src = get_clk_src(i2s_config_ext->mclk, 365 SKL_MNDSS_DIV_CLK_SRC_MASK); 366 } 367 368 parent = skl_get_parent_clk(clk_src); 369 370 /* 371 * Do not copy the config data if there is no parent 372 * clock available for this clock source select 373 */ 374 if (!parent) 375 continue; 376 377 sclk[id].rate_cfg[rate_index].rate = rate; 378 sclk[id].rate_cfg[rate_index].config = fmt_cfg; 379 sclkfs[id].rate_cfg[rate_index].rate = rate; 380 sclkfs[id].rate_cfg[rate_index].config = fmt_cfg; 381 sclk[id].parent_name = parent->name; 382 sclkfs[id].parent_name = parent->name; 383 384 rate_index++; 385 } 386 } 387 } 388 389 static void skl_get_mclk(struct skl *skl, struct skl_ssp_clk *mclk, 390 struct nhlt_fmt *fmt, u8 id) 391 { 392 struct skl_i2s_config_blob_ext *i2s_config_ext; 393 struct skl_i2s_config_blob_legacy *i2s_config; 394 struct nhlt_specific_cfg *fmt_cfg; 395 struct skl_clk_parent_src *parent; 396 u32 clkdiv, div_ratio; 397 u8 clk_src; 398 399 fmt_cfg = &fmt->fmt_config[0].config; 400 i2s_config_ext = (struct skl_i2s_config_blob_ext *)fmt_cfg->caps; 401 402 /* MCLK Divider Source Select and divider */ 403 if (is_legacy_blob(i2s_config_ext->hdr.sig)) { 404 i2s_config = ext_to_legacy_blob(i2s_config_ext); 405 clk_src = get_clk_src(i2s_config->mclk, 406 SKL_MCLK_DIV_CLK_SRC_MASK); 407 clkdiv = i2s_config->mclk.mdivr & 408 SKL_MCLK_DIV_RATIO_MASK; 409 } else { 410 clk_src = get_clk_src(i2s_config_ext->mclk, 411 SKL_MCLK_DIV_CLK_SRC_MASK); 412 clkdiv = i2s_config_ext->mclk.mdivr[0] & 413 SKL_MCLK_DIV_RATIO_MASK; 414 } 415 416 /* bypass divider */ 417 div_ratio = 1; 418 419 if (clkdiv != SKL_MCLK_DIV_RATIO_MASK) 420 /* Divider is 2 + clkdiv */ 421 div_ratio = clkdiv + 2; 422 423 /* Calculate MCLK rate from source using div value */ 424 parent = skl_get_parent_clk(clk_src); 425 if (!parent) 426 return; 427 428 mclk[id].rate_cfg[0].rate = parent->rate/div_ratio; 429 mclk[id].rate_cfg[0].config = &fmt->fmt_config[0]; 430 mclk[id].parent_name = parent->name; 431 } 432 433 void skl_get_clks(struct skl *skl, struct skl_ssp_clk *ssp_clks) 434 { 435 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt; 436 struct nhlt_endpoint *epnt; 437 struct nhlt_fmt *fmt; 438 int i; 439 u8 id; 440 441 epnt = (struct nhlt_endpoint *)nhlt->desc; 442 for (i = 0; i < nhlt->endpoint_count; i++) { 443 if (epnt->linktype == NHLT_LINK_SSP) { 444 id = epnt->virtual_bus_id; 445 446 fmt = (struct nhlt_fmt *)(epnt->config.caps 447 + epnt->config.size); 448 449 skl_get_ssp_clks(skl, ssp_clks, fmt, id); 450 skl_get_mclk(skl, ssp_clks, fmt, id); 451 } 452 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length); 453 } 454 } 455