1db2a4165SFrank Mandarino /* 2db2a4165SFrank Mandarino * soc-core.c -- ALSA SoC Audio Layer 3db2a4165SFrank Mandarino * 4db2a4165SFrank Mandarino * Copyright 2005 Wolfson Microelectronics PLC. 50664d888SLiam Girdwood * Copyright 2005 Openedhand Ltd. 6f0fba2adSLiam Girdwood * Copyright (C) 2010 Slimlogic Ltd. 7f0fba2adSLiam Girdwood * Copyright (C) 2010 Texas Instruments Inc. 80664d888SLiam Girdwood * 9d331124dSLiam Girdwood * Author: Liam Girdwood <lrg@slimlogic.co.uk> 100664d888SLiam Girdwood * with code, comments and ideas from :- 110664d888SLiam Girdwood * Richard Purdie <richard@openedhand.com> 12db2a4165SFrank Mandarino * 13db2a4165SFrank Mandarino * This program is free software; you can redistribute it and/or modify it 14db2a4165SFrank Mandarino * under the terms of the GNU General Public License as published by the 15db2a4165SFrank Mandarino * Free Software Foundation; either version 2 of the License, or (at your 16db2a4165SFrank Mandarino * option) any later version. 17db2a4165SFrank Mandarino * 18db2a4165SFrank Mandarino * TODO: 19db2a4165SFrank Mandarino * o Add hw rules to enforce rates, etc. 20db2a4165SFrank Mandarino * o More testing with other codecs/machines. 21db2a4165SFrank Mandarino * o Add more codecs and platforms to ensure good API coverage. 22db2a4165SFrank Mandarino * o Support TDM on PCM and I2S 23db2a4165SFrank Mandarino */ 24db2a4165SFrank Mandarino 25db2a4165SFrank Mandarino #include <linux/module.h> 26db2a4165SFrank Mandarino #include <linux/moduleparam.h> 27db2a4165SFrank Mandarino #include <linux/init.h> 28db2a4165SFrank Mandarino #include <linux/delay.h> 29db2a4165SFrank Mandarino #include <linux/pm.h> 30db2a4165SFrank Mandarino #include <linux/bitops.h> 3112ef193dSTroy Kisky #include <linux/debugfs.h> 32db2a4165SFrank Mandarino #include <linux/platform_device.h> 33741a509fSMarkus Pargmann #include <linux/pinctrl/consumer.h> 34f0e8ed85SMark Brown #include <linux/ctype.h> 355a0e3ad6STejun Heo #include <linux/slab.h> 36bec4fa05SStephen Warren #include <linux/of.h> 37741a509fSMarkus Pargmann #include <linux/gpio.h> 38741a509fSMarkus Pargmann #include <linux/of_gpio.h> 39474828a4SMarek Vasut #include <sound/ac97_codec.h> 40db2a4165SFrank Mandarino #include <sound/core.h> 413028eb8cSMark Brown #include <sound/jack.h> 42db2a4165SFrank Mandarino #include <sound/pcm.h> 43db2a4165SFrank Mandarino #include <sound/pcm_params.h> 44db2a4165SFrank Mandarino #include <sound/soc.h> 4501d7584cSLiam Girdwood #include <sound/soc-dpcm.h> 46db2a4165SFrank Mandarino #include <sound/initval.h> 47db2a4165SFrank Mandarino 48a8b1d34fSMark Brown #define CREATE_TRACE_POINTS 49a8b1d34fSMark Brown #include <trace/events/asoc.h> 50a8b1d34fSMark Brown 51f0fba2adSLiam Girdwood #define NAME_SIZE 32 52f0fba2adSLiam Girdwood 53384c89e2SMark Brown #ifdef CONFIG_DEBUG_FS 548a9dab1aSMark Brown struct dentry *snd_soc_debugfs_root; 558a9dab1aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); 56384c89e2SMark Brown #endif 57384c89e2SMark Brown 58c5af3a2eSMark Brown static DEFINE_MUTEX(client_mutex); 5912a48a8cSMark Brown static LIST_HEAD(platform_list); 600d0cf00aSMark Brown static LIST_HEAD(codec_list); 61030e79f6SKuninori Morimoto static LIST_HEAD(component_list); 62c5af3a2eSMark Brown 63db2a4165SFrank Mandarino /* 64db2a4165SFrank Mandarino * This is a timeout to do a DAPM powerdown after a stream is closed(). 65db2a4165SFrank Mandarino * It can be used to eliminate pops between different playback streams, e.g. 66db2a4165SFrank Mandarino * between two audio tracks. 67db2a4165SFrank Mandarino */ 68db2a4165SFrank Mandarino static int pmdown_time = 5000; 69db2a4165SFrank Mandarino module_param(pmdown_time, int, 0); 70db2a4165SFrank Mandarino MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); 71db2a4165SFrank Mandarino 72741a509fSMarkus Pargmann struct snd_ac97_reset_cfg { 73741a509fSMarkus Pargmann struct pinctrl *pctl; 74741a509fSMarkus Pargmann struct pinctrl_state *pstate_reset; 75741a509fSMarkus Pargmann struct pinctrl_state *pstate_warm_reset; 76741a509fSMarkus Pargmann struct pinctrl_state *pstate_run; 77741a509fSMarkus Pargmann int gpio_sdata; 78741a509fSMarkus Pargmann int gpio_sync; 79741a509fSMarkus Pargmann int gpio_reset; 80741a509fSMarkus Pargmann }; 81741a509fSMarkus Pargmann 822bc9a81eSDimitris Papastamos /* returns the minimum number of bytes needed to represent 832bc9a81eSDimitris Papastamos * a particular given value */ 842bc9a81eSDimitris Papastamos static int min_bytes_needed(unsigned long val) 852bc9a81eSDimitris Papastamos { 862bc9a81eSDimitris Papastamos int c = 0; 872bc9a81eSDimitris Papastamos int i; 882bc9a81eSDimitris Papastamos 892bc9a81eSDimitris Papastamos for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c) 902bc9a81eSDimitris Papastamos if (val & (1UL << i)) 912bc9a81eSDimitris Papastamos break; 922bc9a81eSDimitris Papastamos c = (sizeof val * 8) - c; 932bc9a81eSDimitris Papastamos if (!c || (c % 8)) 942bc9a81eSDimitris Papastamos c = (c + 8) / 8; 952bc9a81eSDimitris Papastamos else 962bc9a81eSDimitris Papastamos c /= 8; 972bc9a81eSDimitris Papastamos return c; 982bc9a81eSDimitris Papastamos } 992bc9a81eSDimitris Papastamos 10013fd179fSDimitris Papastamos /* fill buf which is 'len' bytes with a formatted 10113fd179fSDimitris Papastamos * string of the form 'reg: value\n' */ 10213fd179fSDimitris Papastamos static int format_register_str(struct snd_soc_codec *codec, 10313fd179fSDimitris Papastamos unsigned int reg, char *buf, size_t len) 1042624d5faSMark Brown { 10500b317a4SStephen Warren int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 10600b317a4SStephen Warren int regsize = codec->driver->reg_word_size * 2; 10713fd179fSDimitris Papastamos int ret; 10813fd179fSDimitris Papastamos char tmpbuf[len + 1]; 10913fd179fSDimitris Papastamos char regbuf[regsize + 1]; 11013fd179fSDimitris Papastamos 11113fd179fSDimitris Papastamos /* since tmpbuf is allocated on the stack, warn the callers if they 11213fd179fSDimitris Papastamos * try to abuse this function */ 11313fd179fSDimitris Papastamos WARN_ON(len > 63); 11413fd179fSDimitris Papastamos 11513fd179fSDimitris Papastamos /* +2 for ': ' and + 1 for '\n' */ 11613fd179fSDimitris Papastamos if (wordsize + regsize + 2 + 1 != len) 11713fd179fSDimitris Papastamos return -EINVAL; 11813fd179fSDimitris Papastamos 11913fd179fSDimitris Papastamos ret = snd_soc_read(codec, reg); 12013fd179fSDimitris Papastamos if (ret < 0) { 12113fd179fSDimitris Papastamos memset(regbuf, 'X', regsize); 12213fd179fSDimitris Papastamos regbuf[regsize] = '\0'; 12313fd179fSDimitris Papastamos } else { 12413fd179fSDimitris Papastamos snprintf(regbuf, regsize + 1, "%.*x", regsize, ret); 12513fd179fSDimitris Papastamos } 12613fd179fSDimitris Papastamos 12713fd179fSDimitris Papastamos /* prepare the buffer */ 12813fd179fSDimitris Papastamos snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf); 12913fd179fSDimitris Papastamos /* copy it back to the caller without the '\0' */ 13013fd179fSDimitris Papastamos memcpy(buf, tmpbuf, len); 13113fd179fSDimitris Papastamos 13213fd179fSDimitris Papastamos return 0; 13313fd179fSDimitris Papastamos } 13413fd179fSDimitris Papastamos 13513fd179fSDimitris Papastamos /* codec register dump */ 13613fd179fSDimitris Papastamos static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf, 13713fd179fSDimitris Papastamos size_t count, loff_t pos) 13813fd179fSDimitris Papastamos { 13913fd179fSDimitris Papastamos int i, step = 1; 1402bc9a81eSDimitris Papastamos int wordsize, regsize; 14113fd179fSDimitris Papastamos int len; 14213fd179fSDimitris Papastamos size_t total = 0; 14313fd179fSDimitris Papastamos loff_t p = 0; 1442bc9a81eSDimitris Papastamos 14500b317a4SStephen Warren wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; 14600b317a4SStephen Warren regsize = codec->driver->reg_word_size * 2; 1472624d5faSMark Brown 14813fd179fSDimitris Papastamos len = wordsize + regsize + 2 + 1; 14913fd179fSDimitris Papastamos 150f0fba2adSLiam Girdwood if (!codec->driver->reg_cache_size) 1512624d5faSMark Brown return 0; 1522624d5faSMark Brown 153f0fba2adSLiam Girdwood if (codec->driver->reg_cache_step) 154f0fba2adSLiam Girdwood step = codec->driver->reg_cache_step; 1552624d5faSMark Brown 156f0fba2adSLiam Girdwood for (i = 0; i < codec->driver->reg_cache_size; i += step) { 15713fd179fSDimitris Papastamos /* only support larger than PAGE_SIZE bytes debugfs 15813fd179fSDimitris Papastamos * entries for the default case */ 15913fd179fSDimitris Papastamos if (p >= pos) { 16013fd179fSDimitris Papastamos if (total + len >= count - 1) 1612624d5faSMark Brown break; 16213fd179fSDimitris Papastamos format_register_str(codec, i, buf + total, len); 16313fd179fSDimitris Papastamos total += len; 16413fd179fSDimitris Papastamos } 16513fd179fSDimitris Papastamos p += len; 16613fd179fSDimitris Papastamos } 1672624d5faSMark Brown 16813fd179fSDimitris Papastamos total = min(total, count - 1); 1692624d5faSMark Brown 17013fd179fSDimitris Papastamos return total; 1712624d5faSMark Brown } 17213fd179fSDimitris Papastamos 1732624d5faSMark Brown static ssize_t codec_reg_show(struct device *dev, 1742624d5faSMark Brown struct device_attribute *attr, char *buf) 1752624d5faSMark Brown { 17636ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 177f0fba2adSLiam Girdwood 17813fd179fSDimitris Papastamos return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0); 1792624d5faSMark Brown } 1802624d5faSMark Brown 1812624d5faSMark Brown static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); 1822624d5faSMark Brown 183dbe21408SMark Brown static ssize_t pmdown_time_show(struct device *dev, 184dbe21408SMark Brown struct device_attribute *attr, char *buf) 185dbe21408SMark Brown { 18636ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 187dbe21408SMark Brown 188f0fba2adSLiam Girdwood return sprintf(buf, "%ld\n", rtd->pmdown_time); 189dbe21408SMark Brown } 190dbe21408SMark Brown 191dbe21408SMark Brown static ssize_t pmdown_time_set(struct device *dev, 192dbe21408SMark Brown struct device_attribute *attr, 193dbe21408SMark Brown const char *buf, size_t count) 194dbe21408SMark Brown { 19536ae1a96SMark Brown struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 196c593b520SMark Brown int ret; 197dbe21408SMark Brown 198b785a492SJingoo Han ret = kstrtol(buf, 10, &rtd->pmdown_time); 199c593b520SMark Brown if (ret) 200c593b520SMark Brown return ret; 201dbe21408SMark Brown 202dbe21408SMark Brown return count; 203dbe21408SMark Brown } 204dbe21408SMark Brown 205dbe21408SMark Brown static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); 206dbe21408SMark Brown 2072624d5faSMark Brown #ifdef CONFIG_DEBUG_FS 2082624d5faSMark Brown static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, 2092624d5faSMark Brown size_t count, loff_t *ppos) 2102624d5faSMark Brown { 2112624d5faSMark Brown ssize_t ret; 2122624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 21313fd179fSDimitris Papastamos char *buf; 21413fd179fSDimitris Papastamos 21513fd179fSDimitris Papastamos if (*ppos < 0 || !count) 21613fd179fSDimitris Papastamos return -EINVAL; 21713fd179fSDimitris Papastamos 21813fd179fSDimitris Papastamos buf = kmalloc(count, GFP_KERNEL); 2192624d5faSMark Brown if (!buf) 2202624d5faSMark Brown return -ENOMEM; 22113fd179fSDimitris Papastamos 22213fd179fSDimitris Papastamos ret = soc_codec_reg_show(codec, buf, count, *ppos); 22313fd179fSDimitris Papastamos if (ret >= 0) { 22413fd179fSDimitris Papastamos if (copy_to_user(user_buf, buf, ret)) { 22513fd179fSDimitris Papastamos kfree(buf); 22613fd179fSDimitris Papastamos return -EFAULT; 22713fd179fSDimitris Papastamos } 22813fd179fSDimitris Papastamos *ppos += ret; 22913fd179fSDimitris Papastamos } 23013fd179fSDimitris Papastamos 2312624d5faSMark Brown kfree(buf); 2322624d5faSMark Brown return ret; 2332624d5faSMark Brown } 2342624d5faSMark Brown 2352624d5faSMark Brown static ssize_t codec_reg_write_file(struct file *file, 2362624d5faSMark Brown const char __user *user_buf, size_t count, loff_t *ppos) 2372624d5faSMark Brown { 2382624d5faSMark Brown char buf[32]; 23934e268d8SStephen Boyd size_t buf_size; 2402624d5faSMark Brown char *start = buf; 2412624d5faSMark Brown unsigned long reg, value; 2422624d5faSMark Brown struct snd_soc_codec *codec = file->private_data; 243b785a492SJingoo Han int ret; 2442624d5faSMark Brown 2452624d5faSMark Brown buf_size = min(count, (sizeof(buf)-1)); 2462624d5faSMark Brown if (copy_from_user(buf, user_buf, buf_size)) 2472624d5faSMark Brown return -EFAULT; 2482624d5faSMark Brown buf[buf_size] = 0; 2492624d5faSMark Brown 2502624d5faSMark Brown while (*start == ' ') 2512624d5faSMark Brown start++; 2522624d5faSMark Brown reg = simple_strtoul(start, &start, 16); 2532624d5faSMark Brown while (*start == ' ') 2542624d5faSMark Brown start++; 255b785a492SJingoo Han ret = kstrtoul(start, 16, &value); 256b785a492SJingoo Han if (ret) 257b785a492SJingoo Han return ret; 2580d51a9cbSMark Brown 2590d51a9cbSMark Brown /* Userspace has been fiddling around behind the kernel's back */ 260373d4d09SRusty Russell add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE); 2610d51a9cbSMark Brown 262e4f078d8SDimitris Papastamos snd_soc_write(codec, reg, value); 2632624d5faSMark Brown return buf_size; 2642624d5faSMark Brown } 2652624d5faSMark Brown 2662624d5faSMark Brown static const struct file_operations codec_reg_fops = { 267234e3405SStephen Boyd .open = simple_open, 2682624d5faSMark Brown .read = codec_reg_read_file, 2692624d5faSMark Brown .write = codec_reg_write_file, 2706038f373SArnd Bergmann .llseek = default_llseek, 2712624d5faSMark Brown }; 2722624d5faSMark Brown 273e73f3de5SRussell King static struct dentry *soc_debugfs_create_dir(struct dentry *parent, 274e73f3de5SRussell King const char *fmt, ...) 275e73f3de5SRussell King { 276e73f3de5SRussell King struct dentry *de; 277e73f3de5SRussell King va_list ap; 278e73f3de5SRussell King char *s; 279e73f3de5SRussell King 280e73f3de5SRussell King va_start(ap, fmt); 281e73f3de5SRussell King s = kvasprintf(GFP_KERNEL, fmt, ap); 282e73f3de5SRussell King va_end(ap); 283e73f3de5SRussell King 284e73f3de5SRussell King if (!s) 285e73f3de5SRussell King return NULL; 286e73f3de5SRussell King 287e73f3de5SRussell King de = debugfs_create_dir(s, parent); 288e73f3de5SRussell King kfree(s); 289e73f3de5SRussell King 290e73f3de5SRussell King return de; 291e73f3de5SRussell King } 292e73f3de5SRussell King 2932624d5faSMark Brown static void soc_init_codec_debugfs(struct snd_soc_codec *codec) 2942624d5faSMark Brown { 295d6ce4cf3SJarkko Nikula struct dentry *debugfs_card_root = codec->card->debugfs_card_root; 296d6ce4cf3SJarkko Nikula 297e73f3de5SRussell King codec->debugfs_codec_root = soc_debugfs_create_dir(debugfs_card_root, 298*211bcc6cSMark Brown "codec:%s", 299*211bcc6cSMark Brown codec->component.name); 3002624d5faSMark Brown if (!codec->debugfs_codec_root) { 30110e8aa9aSMichał Mirosław dev_warn(codec->dev, 30210e8aa9aSMichał Mirosław "ASoC: Failed to create codec debugfs directory\n"); 3032624d5faSMark Brown return; 3042624d5faSMark Brown } 3052624d5faSMark Brown 306aaee8ef1SMark Brown debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root, 307aaee8ef1SMark Brown &codec->cache_sync); 308aaee8ef1SMark Brown debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root, 309aaee8ef1SMark Brown &codec->cache_only); 310aaee8ef1SMark Brown 3112624d5faSMark Brown codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, 3122624d5faSMark Brown codec->debugfs_codec_root, 3132624d5faSMark Brown codec, &codec_reg_fops); 3142624d5faSMark Brown if (!codec->debugfs_reg) 31510e8aa9aSMichał Mirosław dev_warn(codec->dev, 31610e8aa9aSMichał Mirosław "ASoC: Failed to create codec register debugfs file\n"); 3172624d5faSMark Brown 3188eecaf62SLars-Peter Clausen snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root); 3192624d5faSMark Brown } 3202624d5faSMark Brown 3212624d5faSMark Brown static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 3222624d5faSMark Brown { 3232624d5faSMark Brown debugfs_remove_recursive(codec->debugfs_codec_root); 3242624d5faSMark Brown } 3252624d5faSMark Brown 326731f1ab2SSebastien Guiriec static void soc_init_platform_debugfs(struct snd_soc_platform *platform) 327731f1ab2SSebastien Guiriec { 328731f1ab2SSebastien Guiriec struct dentry *debugfs_card_root = platform->card->debugfs_card_root; 329731f1ab2SSebastien Guiriec 330e73f3de5SRussell King platform->debugfs_platform_root = soc_debugfs_create_dir(debugfs_card_root, 331*211bcc6cSMark Brown "platform:%s", 332*211bcc6cSMark Brown platform->component.name); 333731f1ab2SSebastien Guiriec if (!platform->debugfs_platform_root) { 334731f1ab2SSebastien Guiriec dev_warn(platform->dev, 335f110bfc7SLiam Girdwood "ASoC: Failed to create platform debugfs directory\n"); 336731f1ab2SSebastien Guiriec return; 337731f1ab2SSebastien Guiriec } 338731f1ab2SSebastien Guiriec 339bc9af9faSLars-Peter Clausen snd_soc_dapm_debugfs_init(&platform->component.dapm, 340731f1ab2SSebastien Guiriec platform->debugfs_platform_root); 341731f1ab2SSebastien Guiriec } 342731f1ab2SSebastien Guiriec 343731f1ab2SSebastien Guiriec static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform) 344731f1ab2SSebastien Guiriec { 345731f1ab2SSebastien Guiriec debugfs_remove_recursive(platform->debugfs_platform_root); 346731f1ab2SSebastien Guiriec } 347731f1ab2SSebastien Guiriec 348c3c5a19aSMark Brown static ssize_t codec_list_read_file(struct file *file, char __user *user_buf, 349c3c5a19aSMark Brown size_t count, loff_t *ppos) 350c3c5a19aSMark Brown { 351c3c5a19aSMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3522b194f9dSMark Brown ssize_t len, ret = 0; 353c3c5a19aSMark Brown struct snd_soc_codec *codec; 354c3c5a19aSMark Brown 355c3c5a19aSMark Brown if (!buf) 356c3c5a19aSMark Brown return -ENOMEM; 357c3c5a19aSMark Brown 3582b194f9dSMark Brown list_for_each_entry(codec, &codec_list, list) { 3592b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 360f4333203SLars-Peter Clausen codec->component.name); 3612b194f9dSMark Brown if (len >= 0) 3622b194f9dSMark Brown ret += len; 3632b194f9dSMark Brown if (ret > PAGE_SIZE) { 3642b194f9dSMark Brown ret = PAGE_SIZE; 3652b194f9dSMark Brown break; 3662b194f9dSMark Brown } 3672b194f9dSMark Brown } 368c3c5a19aSMark Brown 369c3c5a19aSMark Brown if (ret >= 0) 370c3c5a19aSMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 371c3c5a19aSMark Brown 372c3c5a19aSMark Brown kfree(buf); 373c3c5a19aSMark Brown 374c3c5a19aSMark Brown return ret; 375c3c5a19aSMark Brown } 376c3c5a19aSMark Brown 377c3c5a19aSMark Brown static const struct file_operations codec_list_fops = { 378c3c5a19aSMark Brown .read = codec_list_read_file, 379c3c5a19aSMark Brown .llseek = default_llseek,/* read accesses f_pos */ 380c3c5a19aSMark Brown }; 381c3c5a19aSMark Brown 382f3208780SMark Brown static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, 383f3208780SMark Brown size_t count, loff_t *ppos) 384f3208780SMark Brown { 385f3208780SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3862b194f9dSMark Brown ssize_t len, ret = 0; 3871438c2f6SLars-Peter Clausen struct snd_soc_component *component; 388f3208780SMark Brown struct snd_soc_dai *dai; 389f3208780SMark Brown 390f3208780SMark Brown if (!buf) 391f3208780SMark Brown return -ENOMEM; 392f3208780SMark Brown 3931438c2f6SLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 3941438c2f6SLars-Peter Clausen list_for_each_entry(dai, &component->dai_list, list) { 3951438c2f6SLars-Peter Clausen len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 3961438c2f6SLars-Peter Clausen dai->name); 3972b194f9dSMark Brown if (len >= 0) 3982b194f9dSMark Brown ret += len; 3992b194f9dSMark Brown if (ret > PAGE_SIZE) { 4002b194f9dSMark Brown ret = PAGE_SIZE; 4012b194f9dSMark Brown break; 4022b194f9dSMark Brown } 4032b194f9dSMark Brown } 4041438c2f6SLars-Peter Clausen } 405f3208780SMark Brown 406f3208780SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 407f3208780SMark Brown 408f3208780SMark Brown kfree(buf); 409f3208780SMark Brown 410f3208780SMark Brown return ret; 411f3208780SMark Brown } 412f3208780SMark Brown 413f3208780SMark Brown static const struct file_operations dai_list_fops = { 414f3208780SMark Brown .read = dai_list_read_file, 415f3208780SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 416f3208780SMark Brown }; 417f3208780SMark Brown 41819c7ac27SMark Brown static ssize_t platform_list_read_file(struct file *file, 41919c7ac27SMark Brown char __user *user_buf, 42019c7ac27SMark Brown size_t count, loff_t *ppos) 42119c7ac27SMark Brown { 42219c7ac27SMark Brown char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 4232b194f9dSMark Brown ssize_t len, ret = 0; 42419c7ac27SMark Brown struct snd_soc_platform *platform; 42519c7ac27SMark Brown 42619c7ac27SMark Brown if (!buf) 42719c7ac27SMark Brown return -ENOMEM; 42819c7ac27SMark Brown 4292b194f9dSMark Brown list_for_each_entry(platform, &platform_list, list) { 4302b194f9dSMark Brown len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", 431f4333203SLars-Peter Clausen platform->component.name); 4322b194f9dSMark Brown if (len >= 0) 4332b194f9dSMark Brown ret += len; 4342b194f9dSMark Brown if (ret > PAGE_SIZE) { 4352b194f9dSMark Brown ret = PAGE_SIZE; 4362b194f9dSMark Brown break; 4372b194f9dSMark Brown } 4382b194f9dSMark Brown } 43919c7ac27SMark Brown 44019c7ac27SMark Brown ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 44119c7ac27SMark Brown 44219c7ac27SMark Brown kfree(buf); 44319c7ac27SMark Brown 44419c7ac27SMark Brown return ret; 44519c7ac27SMark Brown } 44619c7ac27SMark Brown 44719c7ac27SMark Brown static const struct file_operations platform_list_fops = { 44819c7ac27SMark Brown .read = platform_list_read_file, 44919c7ac27SMark Brown .llseek = default_llseek,/* read accesses f_pos */ 45019c7ac27SMark Brown }; 45119c7ac27SMark Brown 452a6052154SJarkko Nikula static void soc_init_card_debugfs(struct snd_soc_card *card) 453a6052154SJarkko Nikula { 454a6052154SJarkko Nikula card->debugfs_card_root = debugfs_create_dir(card->name, 4558a9dab1aSMark Brown snd_soc_debugfs_root); 4563a45b867SJarkko Nikula if (!card->debugfs_card_root) { 457a6052154SJarkko Nikula dev_warn(card->dev, 4587c08be84SLothar Waßmann "ASoC: Failed to create card debugfs directory\n"); 4593a45b867SJarkko Nikula return; 4603a45b867SJarkko Nikula } 4613a45b867SJarkko Nikula 4623a45b867SJarkko Nikula card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, 4633a45b867SJarkko Nikula card->debugfs_card_root, 4643a45b867SJarkko Nikula &card->pop_time); 4653a45b867SJarkko Nikula if (!card->debugfs_pop_time) 4663a45b867SJarkko Nikula dev_warn(card->dev, 467f110bfc7SLiam Girdwood "ASoC: Failed to create pop time debugfs file\n"); 468a6052154SJarkko Nikula } 469a6052154SJarkko Nikula 470a6052154SJarkko Nikula static void soc_cleanup_card_debugfs(struct snd_soc_card *card) 471a6052154SJarkko Nikula { 472a6052154SJarkko Nikula debugfs_remove_recursive(card->debugfs_card_root); 473a6052154SJarkko Nikula } 474a6052154SJarkko Nikula 4752624d5faSMark Brown #else 4762624d5faSMark Brown 4772624d5faSMark Brown static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) 4782624d5faSMark Brown { 4792624d5faSMark Brown } 4802624d5faSMark Brown 4812624d5faSMark Brown static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) 4822624d5faSMark Brown { 4832624d5faSMark Brown } 484b95fccbcSAxel Lin 485731f1ab2SSebastien Guiriec static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform) 486731f1ab2SSebastien Guiriec { 487731f1ab2SSebastien Guiriec } 488731f1ab2SSebastien Guiriec 489731f1ab2SSebastien Guiriec static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform) 490731f1ab2SSebastien Guiriec { 491731f1ab2SSebastien Guiriec } 492731f1ab2SSebastien Guiriec 493b95fccbcSAxel Lin static inline void soc_init_card_debugfs(struct snd_soc_card *card) 494b95fccbcSAxel Lin { 495b95fccbcSAxel Lin } 496b95fccbcSAxel Lin 497b95fccbcSAxel Lin static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) 498b95fccbcSAxel Lin { 499b95fccbcSAxel Lin } 5002624d5faSMark Brown #endif 5012624d5faSMark Brown 50247c88fffSLiam Girdwood struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card, 50347c88fffSLiam Girdwood const char *dai_link, int stream) 50447c88fffSLiam Girdwood { 50547c88fffSLiam Girdwood int i; 50647c88fffSLiam Girdwood 50747c88fffSLiam Girdwood for (i = 0; i < card->num_links; i++) { 50847c88fffSLiam Girdwood if (card->rtd[i].dai_link->no_pcm && 50947c88fffSLiam Girdwood !strcmp(card->rtd[i].dai_link->name, dai_link)) 51047c88fffSLiam Girdwood return card->rtd[i].pcm->streams[stream].substream; 51147c88fffSLiam Girdwood } 512f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link); 51347c88fffSLiam Girdwood return NULL; 51447c88fffSLiam Girdwood } 51547c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream); 51647c88fffSLiam Girdwood 51747c88fffSLiam Girdwood struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card, 51847c88fffSLiam Girdwood const char *dai_link) 51947c88fffSLiam Girdwood { 52047c88fffSLiam Girdwood int i; 52147c88fffSLiam Girdwood 52247c88fffSLiam Girdwood for (i = 0; i < card->num_links; i++) { 52347c88fffSLiam Girdwood if (!strcmp(card->rtd[i].dai_link->name, dai_link)) 52447c88fffSLiam Girdwood return &card->rtd[i]; 52547c88fffSLiam Girdwood } 526f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link); 52747c88fffSLiam Girdwood return NULL; 52847c88fffSLiam Girdwood } 52947c88fffSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); 53047c88fffSLiam Girdwood 531db2a4165SFrank Mandarino #ifdef CONFIG_SND_SOC_AC97_BUS 532db2a4165SFrank Mandarino /* unregister ac97 codec */ 533db2a4165SFrank Mandarino static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) 534db2a4165SFrank Mandarino { 535db2a4165SFrank Mandarino if (codec->ac97->dev.bus) 536db2a4165SFrank Mandarino device_unregister(&codec->ac97->dev); 537db2a4165SFrank Mandarino return 0; 538db2a4165SFrank Mandarino } 539db2a4165SFrank Mandarino 540db2a4165SFrank Mandarino /* stop no dev release warning */ 541db2a4165SFrank Mandarino static void soc_ac97_device_release(struct device *dev){} 542db2a4165SFrank Mandarino 543db2a4165SFrank Mandarino /* register ac97 codec to bus */ 544db2a4165SFrank Mandarino static int soc_ac97_dev_register(struct snd_soc_codec *codec) 545db2a4165SFrank Mandarino { 546db2a4165SFrank Mandarino int err; 547db2a4165SFrank Mandarino 548db2a4165SFrank Mandarino codec->ac97->dev.bus = &ac97_bus_type; 5494ac5c61fSMark Brown codec->ac97->dev.parent = codec->card->dev; 550db2a4165SFrank Mandarino codec->ac97->dev.release = soc_ac97_device_release; 551db2a4165SFrank Mandarino 552bb072bf0SKay Sievers dev_set_name(&codec->ac97->dev, "%d-%d:%s", 553f4333203SLars-Peter Clausen codec->card->snd_card->number, 0, codec->component.name); 554db2a4165SFrank Mandarino err = device_register(&codec->ac97->dev); 555db2a4165SFrank Mandarino if (err < 0) { 556f110bfc7SLiam Girdwood dev_err(codec->dev, "ASoC: Can't register ac97 bus\n"); 557db2a4165SFrank Mandarino codec->ac97->dev.bus = NULL; 558db2a4165SFrank Mandarino return err; 559db2a4165SFrank Mandarino } 560db2a4165SFrank Mandarino return 0; 561db2a4165SFrank Mandarino } 562db2a4165SFrank Mandarino #endif 563db2a4165SFrank Mandarino 5649d58a077SRichard Fitzgerald static void codec2codec_close_delayed_work(struct work_struct *work) 5659d58a077SRichard Fitzgerald { 5669d58a077SRichard Fitzgerald /* Currently nothing to do for c2c links 5679d58a077SRichard Fitzgerald * Since c2c links are internal nodes in the DAPM graph and 5689d58a077SRichard Fitzgerald * don't interface with the outside world or application layer 5699d58a077SRichard Fitzgerald * we don't have to do any special handling on close. 5709d58a077SRichard Fitzgerald */ 5719d58a077SRichard Fitzgerald } 5729d58a077SRichard Fitzgerald 5736f8ab4acSMark Brown #ifdef CONFIG_PM_SLEEP 574db2a4165SFrank Mandarino /* powers down audio subsystem for suspend */ 5756f8ab4acSMark Brown int snd_soc_suspend(struct device *dev) 576db2a4165SFrank Mandarino { 5776f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 5782eea392dSJarkko Nikula struct snd_soc_codec *codec; 579db2a4165SFrank Mandarino int i; 580db2a4165SFrank Mandarino 581e3509ff0SDaniel Mack /* If the initialization of this soc device failed, there is no codec 582e3509ff0SDaniel Mack * associated with it. Just bail out in this case. 583e3509ff0SDaniel Mack */ 584f0fba2adSLiam Girdwood if (list_empty(&card->codec_dev_list)) 585e3509ff0SDaniel Mack return 0; 586e3509ff0SDaniel Mack 5876ed25978SAndy Green /* Due to the resume being scheduled into a workqueue we could 5886ed25978SAndy Green * suspend before that's finished - wait for it to complete. 5896ed25978SAndy Green */ 590f0fba2adSLiam Girdwood snd_power_lock(card->snd_card); 591f0fba2adSLiam Girdwood snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); 592f0fba2adSLiam Girdwood snd_power_unlock(card->snd_card); 5936ed25978SAndy Green 5946ed25978SAndy Green /* we're going to block userspace touching us until resume completes */ 595f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); 5966ed25978SAndy Green 597a00f90f9SMark Brown /* mute any active DACs */ 598f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 599f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 600f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 6013efab7dcSMark Brown 602f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6033efab7dcSMark Brown continue; 6043efab7dcSMark Brown 605f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 606f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 1); 607db2a4165SFrank Mandarino } 608db2a4165SFrank Mandarino 6094ccab3e7SLiam Girdwood /* suspend all pcms */ 610f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 611f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6123efab7dcSMark Brown continue; 6133efab7dcSMark Brown 614f0fba2adSLiam Girdwood snd_pcm_suspend_all(card->rtd[i].pcm); 6153efab7dcSMark Brown } 6164ccab3e7SLiam Girdwood 61787506549SMark Brown if (card->suspend_pre) 61870b2ac12SMark Brown card->suspend_pre(card); 619db2a4165SFrank Mandarino 620f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 621f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 622f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 6233efab7dcSMark Brown 624f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6253efab7dcSMark Brown continue; 6263efab7dcSMark Brown 627f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control) 628f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 629f0fba2adSLiam Girdwood if (platform->driver->suspend && !platform->suspended) { 630f0fba2adSLiam Girdwood platform->driver->suspend(cpu_dai); 631f0fba2adSLiam Girdwood platform->suspended = 1; 632f0fba2adSLiam Girdwood } 633db2a4165SFrank Mandarino } 634db2a4165SFrank Mandarino 635db2a4165SFrank Mandarino /* close any waiting streams and save state */ 636f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 63743829731STejun Heo flush_delayed_work(&card->rtd[i].delayed_work); 638ce6120ccSLiam Girdwood card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level; 639f0fba2adSLiam Girdwood } 640db2a4165SFrank Mandarino 641f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 6423efab7dcSMark Brown 643f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6443efab7dcSMark Brown continue; 6453efab7dcSMark Brown 6467bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 6477bd3a6f3SMark Brown SNDRV_PCM_STREAM_PLAYBACK, 648db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 649f0fba2adSLiam Girdwood 6507bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 6517bd3a6f3SMark Brown SNDRV_PCM_STREAM_CAPTURE, 652db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_SUSPEND); 653db2a4165SFrank Mandarino } 654db2a4165SFrank Mandarino 655e2d32ff6SMark Brown /* Recheck all analogue paths too */ 656e2d32ff6SMark Brown dapm_mark_io_dirty(&card->dapm); 657e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 658e2d32ff6SMark Brown 659f0fba2adSLiam Girdwood /* suspend all CODECs */ 6602eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 6611547aba9SMark Brown /* If there are paths active then the CODEC will be held with 6621547aba9SMark Brown * bias _ON and should not be suspended. */ 663f0fba2adSLiam Girdwood if (!codec->suspended && codec->driver->suspend) { 664ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 6651547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 666125a25daSMark Brown /* 667125a25daSMark Brown * If the CODEC is capable of idle 668125a25daSMark Brown * bias off then being in STANDBY 669125a25daSMark Brown * means it's doing something, 670125a25daSMark Brown * otherwise fall through. 671125a25daSMark Brown */ 672125a25daSMark Brown if (codec->dapm.idle_bias_off) { 673125a25daSMark Brown dev_dbg(codec->dev, 67410e8aa9aSMichał Mirosław "ASoC: idle_bias_off CODEC on over suspend\n"); 675125a25daSMark Brown break; 676125a25daSMark Brown } 6771547aba9SMark Brown case SND_SOC_BIAS_OFF: 67884b315eeSLars-Peter Clausen codec->driver->suspend(codec); 679f0fba2adSLiam Girdwood codec->suspended = 1; 6807be4ba24SMark Brown codec->cache_sync = 1; 681e2c330b9SLars-Peter Clausen if (codec->component.regmap) 682e2c330b9SLars-Peter Clausen regcache_mark_dirty(codec->component.regmap); 683988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 684988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(codec->dev); 6851547aba9SMark Brown break; 6861547aba9SMark Brown default: 68710e8aa9aSMichał Mirosław dev_dbg(codec->dev, 68810e8aa9aSMichał Mirosław "ASoC: CODEC is on over suspend\n"); 6891547aba9SMark Brown break; 6901547aba9SMark Brown } 6911547aba9SMark Brown } 692f0fba2adSLiam Girdwood } 693db2a4165SFrank Mandarino 694f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 695f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 6963efab7dcSMark Brown 697f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 6983efab7dcSMark Brown continue; 6993efab7dcSMark Brown 700f0fba2adSLiam Girdwood if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control) 701f0fba2adSLiam Girdwood cpu_dai->driver->suspend(cpu_dai); 702988e8cc4SNicolin Chen 703988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 704988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 705db2a4165SFrank Mandarino } 706db2a4165SFrank Mandarino 70787506549SMark Brown if (card->suspend_post) 70870b2ac12SMark Brown card->suspend_post(card); 709db2a4165SFrank Mandarino 710db2a4165SFrank Mandarino return 0; 711db2a4165SFrank Mandarino } 7126f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_suspend); 713db2a4165SFrank Mandarino 7146ed25978SAndy Green /* deferred resume work, so resume can complete before we finished 7156ed25978SAndy Green * setting our codec back up, which can be very slow on I2C 7166ed25978SAndy Green */ 7176ed25978SAndy Green static void soc_resume_deferred(struct work_struct *work) 718db2a4165SFrank Mandarino { 719f0fba2adSLiam Girdwood struct snd_soc_card *card = 720f0fba2adSLiam Girdwood container_of(work, struct snd_soc_card, deferred_resume_work); 7212eea392dSJarkko Nikula struct snd_soc_codec *codec; 722db2a4165SFrank Mandarino int i; 723db2a4165SFrank Mandarino 7246ed25978SAndy Green /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, 7256ed25978SAndy Green * so userspace apps are blocked from touching us 7266ed25978SAndy Green */ 7276ed25978SAndy Green 728f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: starting resume work\n"); 7296ed25978SAndy Green 7309949788bSMark Brown /* Bring us up into D2 so that DAPM starts enabling things */ 731f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); 7329949788bSMark Brown 73387506549SMark Brown if (card->resume_pre) 73470b2ac12SMark Brown card->resume_pre(card); 735db2a4165SFrank Mandarino 736f0fba2adSLiam Girdwood /* resume AC97 DAIs */ 737f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 738f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 7393efab7dcSMark Brown 740f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7413efab7dcSMark Brown continue; 7423efab7dcSMark Brown 743f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control) 744f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 745db2a4165SFrank Mandarino } 746db2a4165SFrank Mandarino 7472eea392dSJarkko Nikula list_for_each_entry(codec, &card->codec_dev_list, card_list) { 7481547aba9SMark Brown /* If the CODEC was idle over suspend then it will have been 7491547aba9SMark Brown * left with bias OFF or STANDBY and suspended so we must now 7501547aba9SMark Brown * resume. Otherwise the suspend was suppressed. 7511547aba9SMark Brown */ 752f0fba2adSLiam Girdwood if (codec->driver->resume && codec->suspended) { 753ce6120ccSLiam Girdwood switch (codec->dapm.bias_level) { 7541547aba9SMark Brown case SND_SOC_BIAS_STANDBY: 7551547aba9SMark Brown case SND_SOC_BIAS_OFF: 756f0fba2adSLiam Girdwood codec->driver->resume(codec); 757f0fba2adSLiam Girdwood codec->suspended = 0; 7581547aba9SMark Brown break; 7591547aba9SMark Brown default: 76010e8aa9aSMichał Mirosław dev_dbg(codec->dev, 76110e8aa9aSMichał Mirosław "ASoC: CODEC was on over suspend\n"); 7621547aba9SMark Brown break; 7631547aba9SMark Brown } 7641547aba9SMark Brown } 765f0fba2adSLiam Girdwood } 766db2a4165SFrank Mandarino 767f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 7683efab7dcSMark Brown 769f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7703efab7dcSMark Brown continue; 7713efab7dcSMark Brown 7727bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 773d9b0951bSLiam Girdwood SNDRV_PCM_STREAM_PLAYBACK, 774db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 775f0fba2adSLiam Girdwood 7767bd3a6f3SMark Brown snd_soc_dapm_stream_event(&card->rtd[i], 777d9b0951bSLiam Girdwood SNDRV_PCM_STREAM_CAPTURE, 778db2a4165SFrank Mandarino SND_SOC_DAPM_STREAM_RESUME); 779db2a4165SFrank Mandarino } 780db2a4165SFrank Mandarino 7813ff3f64bSMark Brown /* unmute any active DACs */ 782f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 783f0fba2adSLiam Girdwood struct snd_soc_dai *dai = card->rtd[i].codec_dai; 784f0fba2adSLiam Girdwood struct snd_soc_dai_driver *drv = dai->driver; 7853efab7dcSMark Brown 786f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7873efab7dcSMark Brown continue; 7883efab7dcSMark Brown 789f0fba2adSLiam Girdwood if (drv->ops->digital_mute && dai->playback_active) 790f0fba2adSLiam Girdwood drv->ops->digital_mute(dai, 0); 791db2a4165SFrank Mandarino } 792db2a4165SFrank Mandarino 793f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 794f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 795f0fba2adSLiam Girdwood struct snd_soc_platform *platform = card->rtd[i].platform; 7963efab7dcSMark Brown 797f0fba2adSLiam Girdwood if (card->rtd[i].dai_link->ignore_suspend) 7983efab7dcSMark Brown continue; 7993efab7dcSMark Brown 800f0fba2adSLiam Girdwood if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control) 801f0fba2adSLiam Girdwood cpu_dai->driver->resume(cpu_dai); 802f0fba2adSLiam Girdwood if (platform->driver->resume && platform->suspended) { 803f0fba2adSLiam Girdwood platform->driver->resume(cpu_dai); 804f0fba2adSLiam Girdwood platform->suspended = 0; 805f0fba2adSLiam Girdwood } 806db2a4165SFrank Mandarino } 807db2a4165SFrank Mandarino 80887506549SMark Brown if (card->resume_post) 80970b2ac12SMark Brown card->resume_post(card); 810db2a4165SFrank Mandarino 811f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: resume work completed\n"); 8126ed25978SAndy Green 8136ed25978SAndy Green /* userspace can access us now we are back as we were before */ 814f0fba2adSLiam Girdwood snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); 815e2d32ff6SMark Brown 816e2d32ff6SMark Brown /* Recheck all analogue paths too */ 817e2d32ff6SMark Brown dapm_mark_io_dirty(&card->dapm); 818e2d32ff6SMark Brown snd_soc_dapm_sync(&card->dapm); 8196ed25978SAndy Green } 8206ed25978SAndy Green 8216ed25978SAndy Green /* powers up audio subsystem after a suspend */ 8226f8ab4acSMark Brown int snd_soc_resume(struct device *dev) 8236ed25978SAndy Green { 8246f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 82582e14e8bSStephen Warren int i, ac97_control = 0; 826b9dd94a8SPeter Ujfalusi 8275ff1ddf2SEric Miao /* If the initialization of this soc device failed, there is no codec 8285ff1ddf2SEric Miao * associated with it. Just bail out in this case. 8295ff1ddf2SEric Miao */ 8305ff1ddf2SEric Miao if (list_empty(&card->codec_dev_list)) 8315ff1ddf2SEric Miao return 0; 8325ff1ddf2SEric Miao 833988e8cc4SNicolin Chen /* activate pins from sleep state */ 834988e8cc4SNicolin Chen for (i = 0; i < card->num_rtd; i++) { 835988e8cc4SNicolin Chen struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 836988e8cc4SNicolin Chen struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai; 837988e8cc4SNicolin Chen if (cpu_dai->active) 838988e8cc4SNicolin Chen pinctrl_pm_select_default_state(cpu_dai->dev); 839988e8cc4SNicolin Chen if (codec_dai->active) 840988e8cc4SNicolin Chen pinctrl_pm_select_default_state(codec_dai->dev); 841988e8cc4SNicolin Chen } 842988e8cc4SNicolin Chen 84364ab9baaSMark Brown /* AC97 devices might have other drivers hanging off them so 84464ab9baaSMark Brown * need to resume immediately. Other drivers don't have that 84564ab9baaSMark Brown * problem and may take a substantial amount of time to resume 84664ab9baaSMark Brown * due to I/O costs and anti-pop so handle them out of line. 84764ab9baaSMark Brown */ 848f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 849f0fba2adSLiam Girdwood struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 85082e14e8bSStephen Warren ac97_control |= cpu_dai->driver->ac97_control; 85182e14e8bSStephen Warren } 85282e14e8bSStephen Warren if (ac97_control) { 853f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Resuming AC97 immediately\n"); 85464ab9baaSMark Brown soc_resume_deferred(&card->deferred_resume_work); 85564ab9baaSMark Brown } else { 856f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: Scheduling resume work\n"); 8576308419aSMark Brown if (!schedule_work(&card->deferred_resume_work)) 858f110bfc7SLiam Girdwood dev_err(dev, "ASoC: resume work item may be lost\n"); 859f0fba2adSLiam Girdwood } 8606ed25978SAndy Green 861db2a4165SFrank Mandarino return 0; 862db2a4165SFrank Mandarino } 8636f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_resume); 864db2a4165SFrank Mandarino #else 8656f8ab4acSMark Brown #define snd_soc_suspend NULL 8666f8ab4acSMark Brown #define snd_soc_resume NULL 867db2a4165SFrank Mandarino #endif 868db2a4165SFrank Mandarino 86985e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops null_dai_ops = { 87002a06d30SBarry Song }; 87102a06d30SBarry Song 87212023a9aSMisael Lopez Cruz static struct snd_soc_codec *soc_find_codec(const struct device_node *codec_of_node, 87312023a9aSMisael Lopez Cruz const char *codec_name) 87412023a9aSMisael Lopez Cruz { 87512023a9aSMisael Lopez Cruz struct snd_soc_codec *codec; 87612023a9aSMisael Lopez Cruz 87712023a9aSMisael Lopez Cruz list_for_each_entry(codec, &codec_list, list) { 87812023a9aSMisael Lopez Cruz if (codec_of_node) { 87912023a9aSMisael Lopez Cruz if (codec->dev->of_node != codec_of_node) 88012023a9aSMisael Lopez Cruz continue; 88112023a9aSMisael Lopez Cruz } else { 882f4333203SLars-Peter Clausen if (strcmp(codec->component.name, codec_name)) 88312023a9aSMisael Lopez Cruz continue; 88412023a9aSMisael Lopez Cruz } 88512023a9aSMisael Lopez Cruz 88612023a9aSMisael Lopez Cruz return codec; 88712023a9aSMisael Lopez Cruz } 88812023a9aSMisael Lopez Cruz 88912023a9aSMisael Lopez Cruz return NULL; 89012023a9aSMisael Lopez Cruz } 89112023a9aSMisael Lopez Cruz 89212023a9aSMisael Lopez Cruz static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec, 89312023a9aSMisael Lopez Cruz const char *codec_dai_name) 89412023a9aSMisael Lopez Cruz { 89512023a9aSMisael Lopez Cruz struct snd_soc_dai *codec_dai; 89612023a9aSMisael Lopez Cruz 89712023a9aSMisael Lopez Cruz list_for_each_entry(codec_dai, &codec->component.dai_list, list) { 89812023a9aSMisael Lopez Cruz if (!strcmp(codec_dai->name, codec_dai_name)) { 89912023a9aSMisael Lopez Cruz return codec_dai; 90012023a9aSMisael Lopez Cruz } 90112023a9aSMisael Lopez Cruz } 90212023a9aSMisael Lopez Cruz 90312023a9aSMisael Lopez Cruz return NULL; 90412023a9aSMisael Lopez Cruz } 90512023a9aSMisael Lopez Cruz 906f0fba2adSLiam Girdwood static int soc_bind_dai_link(struct snd_soc_card *card, int num) 907db2a4165SFrank Mandarino { 908f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 909f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 9101438c2f6SLars-Peter Clausen struct snd_soc_component *component; 911435c5e25SMark Brown struct snd_soc_platform *platform; 91212023a9aSMisael Lopez Cruz struct snd_soc_dai *cpu_dai; 913848dd8beSMark Brown const char *platform_name; 914db2a4165SFrank Mandarino 915f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num); 9166308419aSMark Brown 917b19e6e7bSMark Brown /* Find CPU DAI from registered DAIs*/ 9181438c2f6SLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 919bc92657aSStephen Warren if (dai_link->cpu_of_node && 9201438c2f6SLars-Peter Clausen component->dev->of_node != dai_link->cpu_of_node) 9215a504963SStephen Warren continue; 922bc92657aSStephen Warren if (dai_link->cpu_name && 9231438c2f6SLars-Peter Clausen strcmp(dev_name(component->dev), dai_link->cpu_name)) 9242610ab77SStephen Warren continue; 9251438c2f6SLars-Peter Clausen list_for_each_entry(cpu_dai, &component->dai_list, list) { 926bc92657aSStephen Warren if (dai_link->cpu_dai_name && 927bc92657aSStephen Warren strcmp(cpu_dai->name, dai_link->cpu_dai_name)) 928bc92657aSStephen Warren continue; 9292610ab77SStephen Warren 930f0fba2adSLiam Girdwood rtd->cpu_dai = cpu_dai; 931f0fba2adSLiam Girdwood } 9321438c2f6SLars-Peter Clausen } 933b19e6e7bSMark Brown 934b19e6e7bSMark Brown if (!rtd->cpu_dai) { 935f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: CPU DAI %s not registered\n", 936f0fba2adSLiam Girdwood dai_link->cpu_dai_name); 937b19e6e7bSMark Brown return -EPROBE_DEFER; 938c5af3a2eSMark Brown } 939c5af3a2eSMark Brown 94012023a9aSMisael Lopez Cruz /* Find CODEC from registered list */ 94112023a9aSMisael Lopez Cruz rtd->codec = soc_find_codec(dai_link->codec_of_node, 94212023a9aSMisael Lopez Cruz dai_link->codec_name); 943b19e6e7bSMark Brown if (!rtd->codec) { 944f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: CODEC %s not registered\n", 945f0fba2adSLiam Girdwood dai_link->codec_name); 946b19e6e7bSMark Brown return -EPROBE_DEFER; 947b19e6e7bSMark Brown } 948848dd8beSMark Brown 94912023a9aSMisael Lopez Cruz /* Find CODEC DAI from registered list */ 95012023a9aSMisael Lopez Cruz rtd->codec_dai = soc_find_codec_dai(rtd->codec, 95112023a9aSMisael Lopez Cruz dai_link->codec_dai_name); 95212023a9aSMisael Lopez Cruz if (!rtd->codec_dai) { 95312023a9aSMisael Lopez Cruz dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n", 95412023a9aSMisael Lopez Cruz dai_link->codec_dai_name); 95512023a9aSMisael Lopez Cruz return -EPROBE_DEFER; 95612023a9aSMisael Lopez Cruz } 95712023a9aSMisael Lopez Cruz 958848dd8beSMark Brown /* if there's no platform we match on the empty platform */ 959848dd8beSMark Brown platform_name = dai_link->platform_name; 9605a504963SStephen Warren if (!platform_name && !dai_link->platform_of_node) 961848dd8beSMark Brown platform_name = "snd-soc-dummy"; 962848dd8beSMark Brown 963b19e6e7bSMark Brown /* find one from the set of registered platforms */ 964f0fba2adSLiam Girdwood list_for_each_entry(platform, &platform_list, list) { 9655a504963SStephen Warren if (dai_link->platform_of_node) { 9665a504963SStephen Warren if (platform->dev->of_node != 9675a504963SStephen Warren dai_link->platform_of_node) 9685a504963SStephen Warren continue; 9695a504963SStephen Warren } else { 970f4333203SLars-Peter Clausen if (strcmp(platform->component.name, platform_name)) 9712610ab77SStephen Warren continue; 9725a504963SStephen Warren } 9732610ab77SStephen Warren 974f0fba2adSLiam Girdwood rtd->platform = platform; 975f0fba2adSLiam Girdwood } 976b19e6e7bSMark Brown if (!rtd->platform) { 977f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: platform %s not registered\n", 978f0fba2adSLiam Girdwood dai_link->platform_name); 979b19e6e7bSMark Brown return -EPROBE_DEFER; 980f0fba2adSLiam Girdwood } 981b19e6e7bSMark Brown 982b19e6e7bSMark Brown card->num_rtd++; 983b19e6e7bSMark Brown 984b19e6e7bSMark Brown return 0; 9856b05eda6SMark Brown } 9866b05eda6SMark Brown 987d12cd198SStephen Warren static int soc_remove_platform(struct snd_soc_platform *platform) 988d12cd198SStephen Warren { 989d12cd198SStephen Warren int ret; 990d12cd198SStephen Warren 991d12cd198SStephen Warren if (platform->driver->remove) { 992d12cd198SStephen Warren ret = platform->driver->remove(platform); 993d12cd198SStephen Warren if (ret < 0) 994f110bfc7SLiam Girdwood dev_err(platform->dev, "ASoC: failed to remove %d\n", 995f110bfc7SLiam Girdwood ret); 996d12cd198SStephen Warren } 997d12cd198SStephen Warren 998d12cd198SStephen Warren /* Make sure all DAPM widgets are freed */ 999bc9af9faSLars-Peter Clausen snd_soc_dapm_free(&platform->component.dapm); 1000d12cd198SStephen Warren 1001d12cd198SStephen Warren soc_cleanup_platform_debugfs(platform); 1002d12cd198SStephen Warren platform->probed = 0; 1003d12cd198SStephen Warren list_del(&platform->card_list); 1004d12cd198SStephen Warren module_put(platform->dev->driver->owner); 1005d12cd198SStephen Warren 1006d12cd198SStephen Warren return 0; 1007d12cd198SStephen Warren } 1008d12cd198SStephen Warren 1009589c3563SJarkko Nikula static void soc_remove_codec(struct snd_soc_codec *codec) 1010589c3563SJarkko Nikula { 1011589c3563SJarkko Nikula int err; 1012589c3563SJarkko Nikula 1013589c3563SJarkko Nikula if (codec->driver->remove) { 1014589c3563SJarkko Nikula err = codec->driver->remove(codec); 1015589c3563SJarkko Nikula if (err < 0) 1016f110bfc7SLiam Girdwood dev_err(codec->dev, "ASoC: failed to remove %d\n", err); 1017589c3563SJarkko Nikula } 1018589c3563SJarkko Nikula 1019589c3563SJarkko Nikula /* Make sure all DAPM widgets are freed */ 1020589c3563SJarkko Nikula snd_soc_dapm_free(&codec->dapm); 1021589c3563SJarkko Nikula 1022589c3563SJarkko Nikula soc_cleanup_codec_debugfs(codec); 1023589c3563SJarkko Nikula codec->probed = 0; 1024589c3563SJarkko Nikula list_del(&codec->card_list); 1025589c3563SJarkko Nikula module_put(codec->dev->driver->owner); 1026589c3563SJarkko Nikula } 1027589c3563SJarkko Nikula 1028b0aa88afSMisael Lopez Cruz static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order) 1029b0aa88afSMisael Lopez Cruz { 1030b0aa88afSMisael Lopez Cruz int err; 1031b0aa88afSMisael Lopez Cruz 1032b0aa88afSMisael Lopez Cruz if (codec_dai && codec_dai->probed && 1033b0aa88afSMisael Lopez Cruz codec_dai->driver->remove_order == order) { 1034b0aa88afSMisael Lopez Cruz if (codec_dai->driver->remove) { 1035b0aa88afSMisael Lopez Cruz err = codec_dai->driver->remove(codec_dai); 1036b0aa88afSMisael Lopez Cruz if (err < 0) 1037b0aa88afSMisael Lopez Cruz dev_err(codec_dai->dev, 1038b0aa88afSMisael Lopez Cruz "ASoC: failed to remove %s: %d\n", 1039b0aa88afSMisael Lopez Cruz codec_dai->name, err); 1040b0aa88afSMisael Lopez Cruz } 1041b0aa88afSMisael Lopez Cruz codec_dai->probed = 0; 1042b0aa88afSMisael Lopez Cruz } 1043b0aa88afSMisael Lopez Cruz } 1044b0aa88afSMisael Lopez Cruz 104562ae68faSStephen Warren static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order) 1046f0fba2adSLiam Girdwood { 1047f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1048f0fba2adSLiam Girdwood struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai; 1049f0fba2adSLiam Girdwood int err; 1050f0fba2adSLiam Girdwood 1051f0fba2adSLiam Girdwood /* unregister the rtd device */ 1052f0fba2adSLiam Girdwood if (rtd->dev_registered) { 105336ae1a96SMark Brown device_remove_file(rtd->dev, &dev_attr_pmdown_time); 105436ae1a96SMark Brown device_remove_file(rtd->dev, &dev_attr_codec_reg); 105536ae1a96SMark Brown device_unregister(rtd->dev); 1056f0fba2adSLiam Girdwood rtd->dev_registered = 0; 105702a06d30SBarry Song } 105802a06d30SBarry Song 1059f0fba2adSLiam Girdwood /* remove the CODEC DAI */ 1060b0aa88afSMisael Lopez Cruz soc_remove_codec_dai(codec_dai, order); 1061f0fba2adSLiam Girdwood 1062f0fba2adSLiam Girdwood /* remove the cpu_dai */ 10630168bf0dSLiam Girdwood if (cpu_dai && cpu_dai->probed && 10640168bf0dSLiam Girdwood cpu_dai->driver->remove_order == order) { 1065f0fba2adSLiam Girdwood if (cpu_dai->driver->remove) { 1066f0fba2adSLiam Girdwood err = cpu_dai->driver->remove(cpu_dai); 1067f0fba2adSLiam Girdwood if (err < 0) 1068f110bfc7SLiam Girdwood dev_err(cpu_dai->dev, 1069f110bfc7SLiam Girdwood "ASoC: failed to remove %s: %d\n", 10700837fc62SFabio Estevam cpu_dai->name, err); 1071f0fba2adSLiam Girdwood } 1072f0fba2adSLiam Girdwood cpu_dai->probed = 0; 10739420d97bSLars-Peter Clausen if (!cpu_dai->codec) 1074f0fba2adSLiam Girdwood module_put(cpu_dai->dev->driver->owner); 1075f0fba2adSLiam Girdwood } 1076f0fba2adSLiam Girdwood } 1077f0fba2adSLiam Girdwood 107862ae68faSStephen Warren static void soc_remove_link_components(struct snd_soc_card *card, int num, 107962ae68faSStephen Warren int order) 108062ae68faSStephen Warren { 108162ae68faSStephen Warren struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 108262ae68faSStephen Warren struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 108362ae68faSStephen Warren struct snd_soc_dai *codec_dai = rtd->codec_dai; 108462ae68faSStephen Warren struct snd_soc_platform *platform = rtd->platform; 108562ae68faSStephen Warren struct snd_soc_codec *codec; 108662ae68faSStephen Warren 108762ae68faSStephen Warren /* remove the platform */ 108862ae68faSStephen Warren if (platform && platform->probed && 108962ae68faSStephen Warren platform->driver->remove_order == order) { 109062ae68faSStephen Warren soc_remove_platform(platform); 109162ae68faSStephen Warren } 109262ae68faSStephen Warren 109362ae68faSStephen Warren /* remove the CODEC-side CODEC */ 109462ae68faSStephen Warren if (codec_dai) { 109562ae68faSStephen Warren codec = codec_dai->codec; 109662ae68faSStephen Warren if (codec && codec->probed && 109762ae68faSStephen Warren codec->driver->remove_order == order) 109862ae68faSStephen Warren soc_remove_codec(codec); 109962ae68faSStephen Warren } 110062ae68faSStephen Warren 110162ae68faSStephen Warren /* remove any CPU-side CODEC */ 110262ae68faSStephen Warren if (cpu_dai) { 110362ae68faSStephen Warren codec = cpu_dai->codec; 110462ae68faSStephen Warren if (codec && codec->probed && 110562ae68faSStephen Warren codec->driver->remove_order == order) 110662ae68faSStephen Warren soc_remove_codec(codec); 110762ae68faSStephen Warren } 110862ae68faSStephen Warren } 110962ae68faSStephen Warren 11100671fd8eSKuninori Morimoto static void soc_remove_dai_links(struct snd_soc_card *card) 11110671fd8eSKuninori Morimoto { 11120168bf0dSLiam Girdwood int dai, order; 11130671fd8eSKuninori Morimoto 11140168bf0dSLiam Girdwood for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 11150168bf0dSLiam Girdwood order++) { 11160168bf0dSLiam Girdwood for (dai = 0; dai < card->num_rtd; dai++) 111762ae68faSStephen Warren soc_remove_link_dais(card, dai, order); 11180168bf0dSLiam Girdwood } 111962ae68faSStephen Warren 112062ae68faSStephen Warren for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 112162ae68faSStephen Warren order++) { 112262ae68faSStephen Warren for (dai = 0; dai < card->num_rtd; dai++) 112362ae68faSStephen Warren soc_remove_link_components(card, dai, order); 112462ae68faSStephen Warren } 112562ae68faSStephen Warren 11260671fd8eSKuninori Morimoto card->num_rtd = 0; 11270671fd8eSKuninori Morimoto } 11280671fd8eSKuninori Morimoto 1129ead9b919SJarkko Nikula static void soc_set_name_prefix(struct snd_soc_card *card, 113094f99c87SLars-Peter Clausen struct snd_soc_component *component) 1131ead9b919SJarkko Nikula { 1132ead9b919SJarkko Nikula int i; 1133ead9b919SJarkko Nikula 1134ff819b83SDimitris Papastamos if (card->codec_conf == NULL) 1135ead9b919SJarkko Nikula return; 1136ead9b919SJarkko Nikula 1137ff819b83SDimitris Papastamos for (i = 0; i < card->num_configs; i++) { 1138ff819b83SDimitris Papastamos struct snd_soc_codec_conf *map = &card->codec_conf[i]; 113994f99c87SLars-Peter Clausen if (map->of_node && component->dev->of_node != map->of_node) 11403ca041edSSebastian Reichel continue; 114194f99c87SLars-Peter Clausen if (map->dev_name && strcmp(component->name, map->dev_name)) 11423ca041edSSebastian Reichel continue; 114394f99c87SLars-Peter Clausen component->name_prefix = map->name_prefix; 1144ead9b919SJarkko Nikula break; 1145ead9b919SJarkko Nikula } 1146ead9b919SJarkko Nikula } 1147ead9b919SJarkko Nikula 1148589c3563SJarkko Nikula static int soc_probe_codec(struct snd_soc_card *card, 1149589c3563SJarkko Nikula struct snd_soc_codec *codec) 1150589c3563SJarkko Nikula { 1151589c3563SJarkko Nikula int ret = 0; 115289b95ac0SMark Brown const struct snd_soc_codec_driver *driver = codec->driver; 1153888df395SMark Brown struct snd_soc_dai *dai; 1154589c3563SJarkko Nikula 1155589c3563SJarkko Nikula codec->card = card; 1156589c3563SJarkko Nikula codec->dapm.card = card; 115794f99c87SLars-Peter Clausen soc_set_name_prefix(card, &codec->component); 1158589c3563SJarkko Nikula 115970d29331SJarkko Nikula if (!try_module_get(codec->dev->driver->owner)) 116070d29331SJarkko Nikula return -ENODEV; 116170d29331SJarkko Nikula 1162d5d1e0beSLars-Peter Clausen soc_init_codec_debugfs(codec); 1163d5d1e0beSLars-Peter Clausen 1164b318ad50SNariman Poushin if (driver->dapm_widgets) { 1165b318ad50SNariman Poushin ret = snd_soc_dapm_new_controls(&codec->dapm, 1166b318ad50SNariman Poushin driver->dapm_widgets, 116777530150SLars-Peter Clausen driver->num_dapm_widgets); 116877530150SLars-Peter Clausen 1169b318ad50SNariman Poushin if (ret != 0) { 1170b318ad50SNariman Poushin dev_err(codec->dev, 1171b318ad50SNariman Poushin "Failed to create new controls %d\n", ret); 1172b318ad50SNariman Poushin goto err_probe; 1173b318ad50SNariman Poushin } 1174b318ad50SNariman Poushin } 1175b318ad50SNariman Poushin 1176888df395SMark Brown /* Create DAPM widgets for each DAI stream */ 1177261edc70SNariman Poushin list_for_each_entry(dai, &codec->component.dai_list, list) { 1178261edc70SNariman Poushin ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai); 1179261edc70SNariman Poushin 1180261edc70SNariman Poushin if (ret != 0) { 1181261edc70SNariman Poushin dev_err(codec->dev, 1182261edc70SNariman Poushin "Failed to create DAI widgets %d\n", ret); 1183261edc70SNariman Poushin goto err_probe; 1184261edc70SNariman Poushin } 1185261edc70SNariman Poushin } 1186888df395SMark Brown 118733c5f969SMark Brown codec->dapm.idle_bias_off = driver->idle_bias_off; 118833c5f969SMark Brown 118989b95ac0SMark Brown if (driver->probe) { 119089b95ac0SMark Brown ret = driver->probe(codec); 1191589c3563SJarkko Nikula if (ret < 0) { 1192589c3563SJarkko Nikula dev_err(codec->dev, 1193f110bfc7SLiam Girdwood "ASoC: failed to probe CODEC %d\n", ret); 119470d29331SJarkko Nikula goto err_probe; 1195589c3563SJarkko Nikula } 1196ff541f4bSChuansheng Liu WARN(codec->dapm.idle_bias_off && 1197ff541f4bSChuansheng Liu codec->dapm.bias_level != SND_SOC_BIAS_OFF, 119810e8aa9aSMichał Mirosław "codec %s can not start from non-off bias with idle_bias_off==1\n", 1199f4333203SLars-Peter Clausen codec->component.name); 1200589c3563SJarkko Nikula } 1201589c3563SJarkko Nikula 1202b7af1dafSMark Brown if (driver->controls) 1203022658beSLiam Girdwood snd_soc_add_codec_controls(codec, driver->controls, 1204b7af1dafSMark Brown driver->num_controls); 120589b95ac0SMark Brown if (driver->dapm_routes) 120689b95ac0SMark Brown snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes, 120789b95ac0SMark Brown driver->num_dapm_routes); 120889b95ac0SMark Brown 1209589c3563SJarkko Nikula /* mark codec as probed and add to card codec list */ 1210589c3563SJarkko Nikula codec->probed = 1; 1211589c3563SJarkko Nikula list_add(&codec->card_list, &card->codec_dev_list); 12127be31be8SJarkko Nikula list_add(&codec->dapm.list, &card->dapm_list); 1213589c3563SJarkko Nikula 121470d29331SJarkko Nikula return 0; 121570d29331SJarkko Nikula 121670d29331SJarkko Nikula err_probe: 1217d5d1e0beSLars-Peter Clausen soc_cleanup_codec_debugfs(codec); 121870d29331SJarkko Nikula module_put(codec->dev->driver->owner); 121970d29331SJarkko Nikula 1220589c3563SJarkko Nikula return ret; 1221589c3563SJarkko Nikula } 1222589c3563SJarkko Nikula 1223956245e9SLiam Girdwood static int soc_probe_platform(struct snd_soc_card *card, 1224956245e9SLiam Girdwood struct snd_soc_platform *platform) 1225956245e9SLiam Girdwood { 1226956245e9SLiam Girdwood int ret = 0; 1227956245e9SLiam Girdwood const struct snd_soc_platform_driver *driver = platform->driver; 12281438c2f6SLars-Peter Clausen struct snd_soc_component *component; 1229be09ad90SLiam Girdwood struct snd_soc_dai *dai; 1230956245e9SLiam Girdwood 1231956245e9SLiam Girdwood platform->card = card; 1232bc9af9faSLars-Peter Clausen platform->component.dapm.card = card; 1233956245e9SLiam Girdwood 1234956245e9SLiam Girdwood if (!try_module_get(platform->dev->driver->owner)) 1235956245e9SLiam Girdwood return -ENODEV; 1236956245e9SLiam Girdwood 1237731f1ab2SSebastien Guiriec soc_init_platform_debugfs(platform); 1238731f1ab2SSebastien Guiriec 1239cb2cf612SLiam Girdwood if (driver->dapm_widgets) 1240bc9af9faSLars-Peter Clausen snd_soc_dapm_new_controls(&platform->component.dapm, 1241cb2cf612SLiam Girdwood driver->dapm_widgets, driver->num_dapm_widgets); 1242cb2cf612SLiam Girdwood 1243be09ad90SLiam Girdwood /* Create DAPM widgets for each DAI stream */ 12441438c2f6SLars-Peter Clausen list_for_each_entry(component, &component_list, list) { 12451438c2f6SLars-Peter Clausen if (component->dev != platform->dev) 1246be09ad90SLiam Girdwood continue; 12471438c2f6SLars-Peter Clausen list_for_each_entry(dai, &component->dai_list, list) 1248bc9af9faSLars-Peter Clausen snd_soc_dapm_new_dai_widgets(&platform->component.dapm, 1249bc9af9faSLars-Peter Clausen dai); 1250be09ad90SLiam Girdwood } 1251be09ad90SLiam Girdwood 1252bc9af9faSLars-Peter Clausen platform->component.dapm.idle_bias_off = 1; 12533fec6b6dSStephen Warren 1254956245e9SLiam Girdwood if (driver->probe) { 1255956245e9SLiam Girdwood ret = driver->probe(platform); 1256956245e9SLiam Girdwood if (ret < 0) { 1257956245e9SLiam Girdwood dev_err(platform->dev, 1258f110bfc7SLiam Girdwood "ASoC: failed to probe platform %d\n", ret); 1259956245e9SLiam Girdwood goto err_probe; 1260956245e9SLiam Girdwood } 1261956245e9SLiam Girdwood } 1262956245e9SLiam Girdwood 1263cb2cf612SLiam Girdwood if (driver->controls) 1264cb2cf612SLiam Girdwood snd_soc_add_platform_controls(platform, driver->controls, 1265cb2cf612SLiam Girdwood driver->num_controls); 1266cb2cf612SLiam Girdwood if (driver->dapm_routes) 1267bc9af9faSLars-Peter Clausen snd_soc_dapm_add_routes(&platform->component.dapm, 1268bc9af9faSLars-Peter Clausen driver->dapm_routes, driver->num_dapm_routes); 1269cb2cf612SLiam Girdwood 1270956245e9SLiam Girdwood /* mark platform as probed and add to card platform list */ 1271956245e9SLiam Girdwood platform->probed = 1; 1272956245e9SLiam Girdwood list_add(&platform->card_list, &card->platform_dev_list); 1273bc9af9faSLars-Peter Clausen list_add(&platform->component.dapm.list, &card->dapm_list); 1274956245e9SLiam Girdwood 1275956245e9SLiam Girdwood return 0; 1276956245e9SLiam Girdwood 1277956245e9SLiam Girdwood err_probe: 127802db1103SLiam Girdwood soc_cleanup_platform_debugfs(platform); 1279956245e9SLiam Girdwood module_put(platform->dev->driver->owner); 1280956245e9SLiam Girdwood 1281956245e9SLiam Girdwood return ret; 1282956245e9SLiam Girdwood } 1283956245e9SLiam Girdwood 128436ae1a96SMark Brown static void rtd_release(struct device *dev) 128536ae1a96SMark Brown { 128636ae1a96SMark Brown kfree(dev); 128736ae1a96SMark Brown } 1288f0fba2adSLiam Girdwood 1289503ae5e0SMisael Lopez Cruz static int soc_aux_dev_init(struct snd_soc_card *card, 1290503ae5e0SMisael Lopez Cruz struct snd_soc_codec *codec, 1291503ae5e0SMisael Lopez Cruz int num) 1292503ae5e0SMisael Lopez Cruz { 1293503ae5e0SMisael Lopez Cruz struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 1294503ae5e0SMisael Lopez Cruz struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 1295503ae5e0SMisael Lopez Cruz int ret; 1296503ae5e0SMisael Lopez Cruz 1297503ae5e0SMisael Lopez Cruz rtd->card = card; 1298503ae5e0SMisael Lopez Cruz 1299503ae5e0SMisael Lopez Cruz /* do machine specific initialization */ 1300503ae5e0SMisael Lopez Cruz if (aux_dev->init) { 1301503ae5e0SMisael Lopez Cruz ret = aux_dev->init(&codec->dapm); 1302503ae5e0SMisael Lopez Cruz if (ret < 0) 1303503ae5e0SMisael Lopez Cruz return ret; 1304503ae5e0SMisael Lopez Cruz } 1305503ae5e0SMisael Lopez Cruz 1306503ae5e0SMisael Lopez Cruz rtd->codec = codec; 1307503ae5e0SMisael Lopez Cruz 1308503ae5e0SMisael Lopez Cruz return 0; 1309503ae5e0SMisael Lopez Cruz } 1310503ae5e0SMisael Lopez Cruz 1311503ae5e0SMisael Lopez Cruz static int soc_dai_link_init(struct snd_soc_card *card, 1312503ae5e0SMisael Lopez Cruz struct snd_soc_codec *codec, 1313503ae5e0SMisael Lopez Cruz int num) 1314503ae5e0SMisael Lopez Cruz { 1315503ae5e0SMisael Lopez Cruz struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1316503ae5e0SMisael Lopez Cruz struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1317503ae5e0SMisael Lopez Cruz int ret; 1318503ae5e0SMisael Lopez Cruz 1319503ae5e0SMisael Lopez Cruz rtd->card = card; 1320503ae5e0SMisael Lopez Cruz 1321503ae5e0SMisael Lopez Cruz /* do machine specific initialization */ 1322503ae5e0SMisael Lopez Cruz if (dai_link->init) { 1323503ae5e0SMisael Lopez Cruz ret = dai_link->init(rtd); 1324503ae5e0SMisael Lopez Cruz if (ret < 0) 1325503ae5e0SMisael Lopez Cruz return ret; 1326503ae5e0SMisael Lopez Cruz } 1327503ae5e0SMisael Lopez Cruz 1328503ae5e0SMisael Lopez Cruz rtd->codec = codec; 1329503ae5e0SMisael Lopez Cruz 1330503ae5e0SMisael Lopez Cruz return 0; 1331503ae5e0SMisael Lopez Cruz } 1332503ae5e0SMisael Lopez Cruz 1333589c3563SJarkko Nikula static int soc_post_component_init(struct snd_soc_card *card, 1334589c3563SJarkko Nikula struct snd_soc_codec *codec, 1335589c3563SJarkko Nikula int num, int dailess) 1336589c3563SJarkko Nikula { 1337589c3563SJarkko Nikula struct snd_soc_dai_link *dai_link = NULL; 1338589c3563SJarkko Nikula struct snd_soc_aux_dev *aux_dev = NULL; 1339589c3563SJarkko Nikula struct snd_soc_pcm_runtime *rtd; 13406479f15aSLars-Peter Clausen const char *name; 1341589c3563SJarkko Nikula int ret = 0; 1342589c3563SJarkko Nikula 1343589c3563SJarkko Nikula if (!dailess) { 1344589c3563SJarkko Nikula dai_link = &card->dai_link[num]; 1345589c3563SJarkko Nikula rtd = &card->rtd[num]; 1346589c3563SJarkko Nikula name = dai_link->name; 1347503ae5e0SMisael Lopez Cruz ret = soc_dai_link_init(card, codec, num); 1348589c3563SJarkko Nikula } else { 1349589c3563SJarkko Nikula aux_dev = &card->aux_dev[num]; 1350589c3563SJarkko Nikula rtd = &card->rtd_aux[num]; 1351589c3563SJarkko Nikula name = aux_dev->name; 1352503ae5e0SMisael Lopez Cruz ret = soc_aux_dev_init(card, codec, num); 1353589c3563SJarkko Nikula } 1354589c3563SJarkko Nikula 1355589c3563SJarkko Nikula if (ret < 0) { 1356f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret); 1357589c3563SJarkko Nikula return ret; 1358589c3563SJarkko Nikula } 1359589c3563SJarkko Nikula 1360589c3563SJarkko Nikula /* register the rtd device */ 136136ae1a96SMark Brown rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL); 136236ae1a96SMark Brown if (!rtd->dev) 136336ae1a96SMark Brown return -ENOMEM; 136436ae1a96SMark Brown device_initialize(rtd->dev); 136536ae1a96SMark Brown rtd->dev->parent = card->dev; 136636ae1a96SMark Brown rtd->dev->release = rtd_release; 136736ae1a96SMark Brown rtd->dev->init_name = name; 136836ae1a96SMark Brown dev_set_drvdata(rtd->dev, rtd); 1369b8c0dab9SLiam Girdwood mutex_init(&rtd->pcm_mutex); 137001d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients); 137101d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients); 137201d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients); 137301d7584cSLiam Girdwood INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients); 137436ae1a96SMark Brown ret = device_add(rtd->dev); 1375589c3563SJarkko Nikula if (ret < 0) { 1376865df9cbSChuansheng Liu /* calling put_device() here to free the rtd->dev */ 1377865df9cbSChuansheng Liu put_device(rtd->dev); 1378589c3563SJarkko Nikula dev_err(card->dev, 1379f110bfc7SLiam Girdwood "ASoC: failed to register runtime device: %d\n", ret); 1380589c3563SJarkko Nikula return ret; 1381589c3563SJarkko Nikula } 1382589c3563SJarkko Nikula rtd->dev_registered = 1; 1383589c3563SJarkko Nikula 1384589c3563SJarkko Nikula /* add DAPM sysfs entries for this codec */ 138536ae1a96SMark Brown ret = snd_soc_dapm_sys_add(rtd->dev); 1386589c3563SJarkko Nikula if (ret < 0) 1387589c3563SJarkko Nikula dev_err(codec->dev, 1388f110bfc7SLiam Girdwood "ASoC: failed to add codec dapm sysfs entries: %d\n", ret); 1389589c3563SJarkko Nikula 1390589c3563SJarkko Nikula /* add codec sysfs entries */ 139136ae1a96SMark Brown ret = device_create_file(rtd->dev, &dev_attr_codec_reg); 1392589c3563SJarkko Nikula if (ret < 0) 1393589c3563SJarkko Nikula dev_err(codec->dev, 1394f110bfc7SLiam Girdwood "ASoC: failed to add codec sysfs files: %d\n", ret); 1395589c3563SJarkko Nikula 1396f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS 1397f86dcef8SLiam Girdwood /* add DPCM sysfs entries */ 1398cd0f8911SLiam Girdwood if (!dailess && !dai_link->dynamic) 1399f86dcef8SLiam Girdwood goto out; 1400f86dcef8SLiam Girdwood 1401f86dcef8SLiam Girdwood ret = soc_dpcm_debugfs_add(rtd); 1402f86dcef8SLiam Girdwood if (ret < 0) 1403f110bfc7SLiam Girdwood dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret); 1404f86dcef8SLiam Girdwood 1405f86dcef8SLiam Girdwood out: 1406f86dcef8SLiam Girdwood #endif 1407589c3563SJarkko Nikula return 0; 1408589c3563SJarkko Nikula } 1409589c3563SJarkko Nikula 141062ae68faSStephen Warren static int soc_probe_link_components(struct snd_soc_card *card, int num, 141162ae68faSStephen Warren int order) 141262ae68faSStephen Warren { 141362ae68faSStephen Warren struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 141462ae68faSStephen Warren struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 141562ae68faSStephen Warren struct snd_soc_dai *codec_dai = rtd->codec_dai; 141662ae68faSStephen Warren struct snd_soc_platform *platform = rtd->platform; 141762ae68faSStephen Warren int ret; 141862ae68faSStephen Warren 141962ae68faSStephen Warren /* probe the CPU-side component, if it is a CODEC */ 142062ae68faSStephen Warren if (cpu_dai->codec && 142162ae68faSStephen Warren !cpu_dai->codec->probed && 142262ae68faSStephen Warren cpu_dai->codec->driver->probe_order == order) { 142362ae68faSStephen Warren ret = soc_probe_codec(card, cpu_dai->codec); 142462ae68faSStephen Warren if (ret < 0) 142562ae68faSStephen Warren return ret; 142662ae68faSStephen Warren } 142762ae68faSStephen Warren 142862ae68faSStephen Warren /* probe the CODEC-side component */ 142962ae68faSStephen Warren if (!codec_dai->codec->probed && 143062ae68faSStephen Warren codec_dai->codec->driver->probe_order == order) { 143162ae68faSStephen Warren ret = soc_probe_codec(card, codec_dai->codec); 143262ae68faSStephen Warren if (ret < 0) 143362ae68faSStephen Warren return ret; 143462ae68faSStephen Warren } 143562ae68faSStephen Warren 143662ae68faSStephen Warren /* probe the platform */ 143762ae68faSStephen Warren if (!platform->probed && 143862ae68faSStephen Warren platform->driver->probe_order == order) { 143962ae68faSStephen Warren ret = soc_probe_platform(card, platform); 144062ae68faSStephen Warren if (ret < 0) 144162ae68faSStephen Warren return ret; 144262ae68faSStephen Warren } 144362ae68faSStephen Warren 144462ae68faSStephen Warren return 0; 144562ae68faSStephen Warren } 144662ae68faSStephen Warren 1447b0aa88afSMisael Lopez Cruz static int soc_probe_codec_dai(struct snd_soc_card *card, 1448b0aa88afSMisael Lopez Cruz struct snd_soc_dai *codec_dai, 1449b0aa88afSMisael Lopez Cruz int order) 1450b0aa88afSMisael Lopez Cruz { 1451b0aa88afSMisael Lopez Cruz int ret; 1452b0aa88afSMisael Lopez Cruz 1453b0aa88afSMisael Lopez Cruz if (!codec_dai->probed && codec_dai->driver->probe_order == order) { 1454b0aa88afSMisael Lopez Cruz if (codec_dai->driver->probe) { 1455b0aa88afSMisael Lopez Cruz ret = codec_dai->driver->probe(codec_dai); 1456b0aa88afSMisael Lopez Cruz if (ret < 0) { 1457b0aa88afSMisael Lopez Cruz dev_err(codec_dai->dev, 1458b0aa88afSMisael Lopez Cruz "ASoC: failed to probe CODEC DAI %s: %d\n", 1459b0aa88afSMisael Lopez Cruz codec_dai->name, ret); 1460b0aa88afSMisael Lopez Cruz return ret; 1461b0aa88afSMisael Lopez Cruz } 1462b0aa88afSMisael Lopez Cruz } 1463b0aa88afSMisael Lopez Cruz 1464b0aa88afSMisael Lopez Cruz /* mark codec_dai as probed and add to card dai list */ 1465b0aa88afSMisael Lopez Cruz codec_dai->probed = 1; 1466b0aa88afSMisael Lopez Cruz } 1467b0aa88afSMisael Lopez Cruz 1468b0aa88afSMisael Lopez Cruz return 0; 1469b0aa88afSMisael Lopez Cruz } 1470b0aa88afSMisael Lopez Cruz 14712436a723SMisael Lopez Cruz static int soc_link_dai_widgets(struct snd_soc_card *card, 14722436a723SMisael Lopez Cruz struct snd_soc_dai_link *dai_link, 14732436a723SMisael Lopez Cruz struct snd_soc_dai *cpu_dai, 14742436a723SMisael Lopez Cruz struct snd_soc_dai *codec_dai) 14752436a723SMisael Lopez Cruz { 14762436a723SMisael Lopez Cruz struct snd_soc_dapm_widget *play_w, *capture_w; 14772436a723SMisael Lopez Cruz int ret; 14782436a723SMisael Lopez Cruz 14792436a723SMisael Lopez Cruz /* link the DAI widgets */ 14802436a723SMisael Lopez Cruz play_w = codec_dai->playback_widget; 14812436a723SMisael Lopez Cruz capture_w = cpu_dai->capture_widget; 14822436a723SMisael Lopez Cruz if (play_w && capture_w) { 14832436a723SMisael Lopez Cruz ret = snd_soc_dapm_new_pcm(card, dai_link->params, 14842436a723SMisael Lopez Cruz capture_w, play_w); 14852436a723SMisael Lopez Cruz if (ret != 0) { 14862436a723SMisael Lopez Cruz dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", 14872436a723SMisael Lopez Cruz play_w->name, capture_w->name, ret); 14882436a723SMisael Lopez Cruz return ret; 14892436a723SMisael Lopez Cruz } 14902436a723SMisael Lopez Cruz } 14912436a723SMisael Lopez Cruz 14922436a723SMisael Lopez Cruz play_w = cpu_dai->playback_widget; 14932436a723SMisael Lopez Cruz capture_w = codec_dai->capture_widget; 14942436a723SMisael Lopez Cruz if (play_w && capture_w) { 14952436a723SMisael Lopez Cruz ret = snd_soc_dapm_new_pcm(card, dai_link->params, 14962436a723SMisael Lopez Cruz capture_w, play_w); 14972436a723SMisael Lopez Cruz if (ret != 0) { 14982436a723SMisael Lopez Cruz dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", 14992436a723SMisael Lopez Cruz play_w->name, capture_w->name, ret); 15002436a723SMisael Lopez Cruz return ret; 15012436a723SMisael Lopez Cruz } 15022436a723SMisael Lopez Cruz } 15032436a723SMisael Lopez Cruz 15042436a723SMisael Lopez Cruz return 0; 15052436a723SMisael Lopez Cruz } 15062436a723SMisael Lopez Cruz 150762ae68faSStephen Warren static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order) 1508f0fba2adSLiam Girdwood { 1509f0fba2adSLiam Girdwood struct snd_soc_dai_link *dai_link = &card->dai_link[num]; 1510f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; 1511f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 1512f0fba2adSLiam Girdwood struct snd_soc_platform *platform = rtd->platform; 1513c74184edSMark Brown struct snd_soc_dai *codec_dai = rtd->codec_dai; 1514c74184edSMark Brown struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1515f0fba2adSLiam Girdwood int ret; 1516f0fba2adSLiam Girdwood 1517f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n", 15180168bf0dSLiam Girdwood card->name, num, order); 1519f0fba2adSLiam Girdwood 1520f0fba2adSLiam Girdwood /* config components */ 1521f0fba2adSLiam Girdwood cpu_dai->platform = platform; 1522f0fba2adSLiam Girdwood codec_dai->card = card; 1523f0fba2adSLiam Girdwood cpu_dai->card = card; 1524f0fba2adSLiam Girdwood 1525f0fba2adSLiam Girdwood /* set default power off timeout */ 1526f0fba2adSLiam Girdwood rtd->pmdown_time = pmdown_time; 1527f0fba2adSLiam Girdwood 1528f0fba2adSLiam Girdwood /* probe the cpu_dai */ 15290168bf0dSLiam Girdwood if (!cpu_dai->probed && 15300168bf0dSLiam Girdwood cpu_dai->driver->probe_order == order) { 1531a9db7dbeSStephen Warren if (!cpu_dai->codec) { 153261b61e3cSLiam Girdwood if (!try_module_get(cpu_dai->dev->driver->owner)) 153361b61e3cSLiam Girdwood return -ENODEV; 1534a9db7dbeSStephen Warren } 1535be09ad90SLiam Girdwood 1536f0fba2adSLiam Girdwood if (cpu_dai->driver->probe) { 1537f0fba2adSLiam Girdwood ret = cpu_dai->driver->probe(cpu_dai); 1538f0fba2adSLiam Girdwood if (ret < 0) { 1539f110bfc7SLiam Girdwood dev_err(cpu_dai->dev, 1540f110bfc7SLiam Girdwood "ASoC: failed to probe CPU DAI %s: %d\n", 15410837fc62SFabio Estevam cpu_dai->name, ret); 154261b61e3cSLiam Girdwood module_put(cpu_dai->dev->driver->owner); 1543f0fba2adSLiam Girdwood return ret; 1544f0fba2adSLiam Girdwood } 1545f0fba2adSLiam Girdwood } 1546f0fba2adSLiam Girdwood cpu_dai->probed = 1; 1547f0fba2adSLiam Girdwood } 1548f0fba2adSLiam Girdwood 1549f0fba2adSLiam Girdwood /* probe the CODEC DAI */ 1550b0aa88afSMisael Lopez Cruz ret = soc_probe_codec_dai(card, codec_dai, order); 1551b0aa88afSMisael Lopez Cruz if (ret) 1552f0fba2adSLiam Girdwood return ret; 1553f0fba2adSLiam Girdwood 15540168bf0dSLiam Girdwood /* complete DAI probe during last probe */ 15550168bf0dSLiam Girdwood if (order != SND_SOC_COMP_ORDER_LAST) 15560168bf0dSLiam Girdwood return 0; 15570168bf0dSLiam Girdwood 1558589c3563SJarkko Nikula ret = soc_post_component_init(card, codec, num, 0); 1559589c3563SJarkko Nikula if (ret) 1560f0fba2adSLiam Girdwood return ret; 1561f0fba2adSLiam Girdwood 156236ae1a96SMark Brown ret = device_create_file(rtd->dev, &dev_attr_pmdown_time); 1563f0fba2adSLiam Girdwood if (ret < 0) 1564f110bfc7SLiam Girdwood dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n", 1565f110bfc7SLiam Girdwood ret); 1566f0fba2adSLiam Girdwood 15671245b700SNamarta Kohli if (cpu_dai->driver->compress_dai) { 15681245b700SNamarta Kohli /*create compress_device"*/ 15691245b700SNamarta Kohli ret = soc_new_compress(rtd, num); 15701245b700SNamarta Kohli if (ret < 0) { 1571f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: can't create compress %s\n", 15721245b700SNamarta Kohli dai_link->stream_name); 15731245b700SNamarta Kohli return ret; 15741245b700SNamarta Kohli } 15751245b700SNamarta Kohli } else { 15761245b700SNamarta Kohli 1577c74184edSMark Brown if (!dai_link->params) { 1578f0fba2adSLiam Girdwood /* create the pcm */ 1579f0fba2adSLiam Girdwood ret = soc_new_pcm(rtd, num); 1580f0fba2adSLiam Girdwood if (ret < 0) { 1581f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", 15820837fc62SFabio Estevam dai_link->stream_name, ret); 1583f0fba2adSLiam Girdwood return ret; 1584f0fba2adSLiam Girdwood } 1585c74184edSMark Brown } else { 15869d58a077SRichard Fitzgerald INIT_DELAYED_WORK(&rtd->delayed_work, 15879d58a077SRichard Fitzgerald codec2codec_close_delayed_work); 15889d58a077SRichard Fitzgerald 1589c74184edSMark Brown /* link the DAI widgets */ 15902436a723SMisael Lopez Cruz ret = soc_link_dai_widgets(card, dai_link, 15912436a723SMisael Lopez Cruz cpu_dai, codec_dai); 15922436a723SMisael Lopez Cruz if (ret) 1593c74184edSMark Brown return ret; 1594c74184edSMark Brown } 1595c74184edSMark Brown } 1596c74184edSMark Brown 1597f0fba2adSLiam Girdwood /* add platform data for AC97 devices */ 1598f0fba2adSLiam Girdwood if (rtd->codec_dai->driver->ac97_control) 1599f0fba2adSLiam Girdwood snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata); 1600f0fba2adSLiam Girdwood 1601f0fba2adSLiam Girdwood return 0; 1602f0fba2adSLiam Girdwood } 1603f0fba2adSLiam Girdwood 1604f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 160502c9c7b9SMisael Lopez Cruz static int soc_register_ac97_codec(struct snd_soc_codec *codec, 160602c9c7b9SMisael Lopez Cruz struct snd_soc_dai *codec_dai) 1607f0fba2adSLiam Girdwood { 1608f0fba2adSLiam Girdwood int ret; 1609f0fba2adSLiam Girdwood 1610f0fba2adSLiam Girdwood /* Only instantiate AC97 if not already done by the adaptor 1611f0fba2adSLiam Girdwood * for the generic AC97 subsystem. 16126b05eda6SMark Brown */ 161302c9c7b9SMisael Lopez Cruz if (codec_dai->driver->ac97_control && !codec->ac97_registered) { 16140562f788SMika Westerberg /* 16150562f788SMika Westerberg * It is possible that the AC97 device is already registered to 16160562f788SMika Westerberg * the device subsystem. This happens when the device is created 16170562f788SMika Westerberg * via snd_ac97_mixer(). Currently only SoC codec that does so 16180562f788SMika Westerberg * is the generic AC97 glue but others migh emerge. 16190562f788SMika Westerberg * 16200562f788SMika Westerberg * In those cases we don't try to register the device again. 16210562f788SMika Westerberg */ 162202c9c7b9SMisael Lopez Cruz if (!codec->ac97_created) 16230562f788SMika Westerberg return 0; 1624f0fba2adSLiam Girdwood 162502c9c7b9SMisael Lopez Cruz ret = soc_ac97_dev_register(codec); 1626f0fba2adSLiam Girdwood if (ret < 0) { 162702c9c7b9SMisael Lopez Cruz dev_err(codec->dev, 1628f110bfc7SLiam Girdwood "ASoC: AC97 device register failed: %d\n", ret); 1629f0fba2adSLiam Girdwood return ret; 1630435c5e25SMark Brown } 1631435c5e25SMark Brown 163202c9c7b9SMisael Lopez Cruz codec->ac97_registered = 1; 1633f0fba2adSLiam Girdwood } 1634f0fba2adSLiam Girdwood return 0; 1635f0fba2adSLiam Girdwood } 1636435c5e25SMark Brown 163702c9c7b9SMisael Lopez Cruz static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd) 163802c9c7b9SMisael Lopez Cruz { 163902c9c7b9SMisael Lopez Cruz return soc_register_ac97_codec(rtd->codec, rtd->codec_dai); 164002c9c7b9SMisael Lopez Cruz } 164102c9c7b9SMisael Lopez Cruz 164202c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_codec(struct snd_soc_codec *codec) 1643f0fba2adSLiam Girdwood { 1644f0fba2adSLiam Girdwood if (codec->ac97_registered) { 1645f0fba2adSLiam Girdwood soc_ac97_dev_unregister(codec); 1646f0fba2adSLiam Girdwood codec->ac97_registered = 0; 1647f0fba2adSLiam Girdwood } 1648f0fba2adSLiam Girdwood } 164902c9c7b9SMisael Lopez Cruz 165002c9c7b9SMisael Lopez Cruz static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd) 165102c9c7b9SMisael Lopez Cruz { 165202c9c7b9SMisael Lopez Cruz soc_unregister_ac97_codec(rtd->codec); 165302c9c7b9SMisael Lopez Cruz } 1654f0fba2adSLiam Girdwood #endif 1655435c5e25SMark Brown 16566b0a0b3bSLars-Peter Clausen static struct snd_soc_codec *soc_find_matching_codec(struct snd_soc_card *card, 16576b0a0b3bSLars-Peter Clausen int num) 1658b19e6e7bSMark Brown { 1659b19e6e7bSMark Brown struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 1660b19e6e7bSMark Brown struct snd_soc_codec *codec; 1661b19e6e7bSMark Brown 1662b19e6e7bSMark Brown /* find CODEC from registered CODECs */ 1663b19e6e7bSMark Brown list_for_each_entry(codec, &codec_list, list) { 16643ca041edSSebastian Reichel if (aux_dev->codec_of_node && 16653ca041edSSebastian Reichel (codec->dev->of_node != aux_dev->codec_of_node)) 16663ca041edSSebastian Reichel continue; 1667f4333203SLars-Peter Clausen if (aux_dev->codec_name && 1668f4333203SLars-Peter Clausen strcmp(codec->component.name, aux_dev->codec_name)) 16693ca041edSSebastian Reichel continue; 16703ca041edSSebastian Reichel return codec; 1671b19e6e7bSMark Brown } 1672b19e6e7bSMark Brown 16733ca041edSSebastian Reichel return NULL; 16743ca041edSSebastian Reichel } 1675fb099cb7SMark Brown 16763ca041edSSebastian Reichel static int soc_check_aux_dev(struct snd_soc_card *card, int num) 16773ca041edSSebastian Reichel { 16783ca041edSSebastian Reichel struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 16793ca041edSSebastian Reichel const char *codecname = aux_dev->codec_name; 16803ca041edSSebastian Reichel struct snd_soc_codec *codec = soc_find_matching_codec(card, num); 16813ca041edSSebastian Reichel 16823ca041edSSebastian Reichel if (codec) 16833ca041edSSebastian Reichel return 0; 16843ca041edSSebastian Reichel if (aux_dev->codec_of_node) 16853ca041edSSebastian Reichel codecname = of_node_full_name(aux_dev->codec_of_node); 16863ca041edSSebastian Reichel 16873ca041edSSebastian Reichel dev_err(card->dev, "ASoC: %s not registered\n", codecname); 1688b19e6e7bSMark Brown return -EPROBE_DEFER; 1689b19e6e7bSMark Brown } 1690b19e6e7bSMark Brown 16912eea392dSJarkko Nikula static int soc_probe_aux_dev(struct snd_soc_card *card, int num) 16922eea392dSJarkko Nikula { 16932eea392dSJarkko Nikula struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; 16943ca041edSSebastian Reichel const char *codecname = aux_dev->codec_name; 1695676ad98aSJarkko Nikula int ret = -ENODEV; 16963ca041edSSebastian Reichel struct snd_soc_codec *codec = soc_find_matching_codec(card, num); 16972eea392dSJarkko Nikula 16983ca041edSSebastian Reichel if (!codec) { 16993ca041edSSebastian Reichel if (aux_dev->codec_of_node) 17003ca041edSSebastian Reichel codecname = of_node_full_name(aux_dev->codec_of_node); 17013ca041edSSebastian Reichel 1702676ad98aSJarkko Nikula /* codec not found */ 17033ca041edSSebastian Reichel dev_err(card->dev, "ASoC: codec %s not found", codecname); 1704b19e6e7bSMark Brown return -EPROBE_DEFER; 17053ca041edSSebastian Reichel } 17062eea392dSJarkko Nikula 17073ca041edSSebastian Reichel if (codec->probed) { 17083ca041edSSebastian Reichel dev_err(codec->dev, "ASoC: codec already probed"); 17093ca041edSSebastian Reichel return -EBUSY; 17103ca041edSSebastian Reichel } 17113ca041edSSebastian Reichel 1712589c3563SJarkko Nikula ret = soc_probe_codec(card, codec); 17132eea392dSJarkko Nikula if (ret < 0) 1714589c3563SJarkko Nikula return ret; 17152eea392dSJarkko Nikula 1716589c3563SJarkko Nikula ret = soc_post_component_init(card, codec, num, 1); 17172eea392dSJarkko Nikula 17182eea392dSJarkko Nikula return ret; 17192eea392dSJarkko Nikula } 17202eea392dSJarkko Nikula 17212eea392dSJarkko Nikula static void soc_remove_aux_dev(struct snd_soc_card *card, int num) 17222eea392dSJarkko Nikula { 17232eea392dSJarkko Nikula struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; 17242eea392dSJarkko Nikula struct snd_soc_codec *codec = rtd->codec; 17252eea392dSJarkko Nikula 17262eea392dSJarkko Nikula /* unregister the rtd device */ 17272eea392dSJarkko Nikula if (rtd->dev_registered) { 172836ae1a96SMark Brown device_remove_file(rtd->dev, &dev_attr_codec_reg); 1729d3bf1561SChuansheng Liu device_unregister(rtd->dev); 17302eea392dSJarkko Nikula rtd->dev_registered = 0; 17312eea392dSJarkko Nikula } 17322eea392dSJarkko Nikula 1733589c3563SJarkko Nikula if (codec && codec->probed) 1734589c3563SJarkko Nikula soc_remove_codec(codec); 17352eea392dSJarkko Nikula } 17362eea392dSJarkko Nikula 1737f90fb3f7SLars-Peter Clausen static int snd_soc_init_codec_cache(struct snd_soc_codec *codec) 1738fdf0f54dSDimitris Papastamos { 1739fdf0f54dSDimitris Papastamos int ret; 1740fdf0f54dSDimitris Papastamos 1741fdf0f54dSDimitris Papastamos if (codec->cache_init) 1742fdf0f54dSDimitris Papastamos return 0; 1743fdf0f54dSDimitris Papastamos 1744fdf0f54dSDimitris Papastamos ret = snd_soc_cache_init(codec); 1745fdf0f54dSDimitris Papastamos if (ret < 0) { 174610e8aa9aSMichał Mirosław dev_err(codec->dev, 174710e8aa9aSMichał Mirosław "ASoC: Failed to set cache compression type: %d\n", 174810e8aa9aSMichał Mirosław ret); 1749fdf0f54dSDimitris Papastamos return ret; 1750fdf0f54dSDimitris Papastamos } 1751fdf0f54dSDimitris Papastamos codec->cache_init = 1; 1752fdf0f54dSDimitris Papastamos return 0; 1753fdf0f54dSDimitris Papastamos } 1754fdf0f54dSDimitris Papastamos 1755b19e6e7bSMark Brown static int snd_soc_instantiate_card(struct snd_soc_card *card) 1756f0fba2adSLiam Girdwood { 1757fdf0f54dSDimitris Papastamos struct snd_soc_codec *codec; 175875d9ac46SMark Brown struct snd_soc_dai_link *dai_link; 1759f04209a7SMark Brown int ret, i, order, dai_fmt; 176096dd3622SMark Brown 176101b9d99aSLiam Girdwood mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); 1762f0fba2adSLiam Girdwood 1763b19e6e7bSMark Brown /* bind DAIs */ 1764b19e6e7bSMark Brown for (i = 0; i < card->num_links; i++) { 1765b19e6e7bSMark Brown ret = soc_bind_dai_link(card, i); 1766b19e6e7bSMark Brown if (ret != 0) 1767b19e6e7bSMark Brown goto base_error; 1768db2a4165SFrank Mandarino } 1769db2a4165SFrank Mandarino 1770b19e6e7bSMark Brown /* check aux_devs too */ 1771b19e6e7bSMark Brown for (i = 0; i < card->num_aux_devs; i++) { 1772b19e6e7bSMark Brown ret = soc_check_aux_dev(card, i); 1773b19e6e7bSMark Brown if (ret != 0) 1774b19e6e7bSMark Brown goto base_error; 1775db2a4165SFrank Mandarino } 1776db2a4165SFrank Mandarino 1777fdf0f54dSDimitris Papastamos /* initialize the register cache for each available codec */ 1778fdf0f54dSDimitris Papastamos list_for_each_entry(codec, &codec_list, list) { 1779fdf0f54dSDimitris Papastamos if (codec->cache_init) 1780fdf0f54dSDimitris Papastamos continue; 1781f90fb3f7SLars-Peter Clausen ret = snd_soc_init_codec_cache(codec); 1782b19e6e7bSMark Brown if (ret < 0) 1783b19e6e7bSMark Brown goto base_error; 1784fdf0f54dSDimitris Papastamos } 1785fdf0f54dSDimitris Papastamos 1786f0fba2adSLiam Girdwood /* card bind complete so register a sound card */ 1787102b5a8dSTakashi Iwai ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 1788f0fba2adSLiam Girdwood card->owner, 0, &card->snd_card); 1789f0fba2adSLiam Girdwood if (ret < 0) { 179010e8aa9aSMichał Mirosław dev_err(card->dev, 179110e8aa9aSMichał Mirosław "ASoC: can't create sound card for card %s: %d\n", 179210e8aa9aSMichał Mirosław card->name, ret); 1793b19e6e7bSMark Brown goto base_error; 1794db2a4165SFrank Mandarino } 1795db2a4165SFrank Mandarino 1796e37a4970SMark Brown card->dapm.bias_level = SND_SOC_BIAS_OFF; 1797e37a4970SMark Brown card->dapm.dev = card->dev; 1798e37a4970SMark Brown card->dapm.card = card; 1799e37a4970SMark Brown list_add(&card->dapm.list, &card->dapm_list); 1800e37a4970SMark Brown 1801d5d1e0beSLars-Peter Clausen #ifdef CONFIG_DEBUG_FS 1802d5d1e0beSLars-Peter Clausen snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); 1803d5d1e0beSLars-Peter Clausen #endif 1804d5d1e0beSLars-Peter Clausen 180588ee1c61SMark Brown #ifdef CONFIG_PM_SLEEP 18066ed25978SAndy Green /* deferred resume work */ 18076308419aSMark Brown INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); 18081301a964SRandy Dunlap #endif 18096ed25978SAndy Green 18109a841ebbSMark Brown if (card->dapm_widgets) 18119a841ebbSMark Brown snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, 18129a841ebbSMark Brown card->num_dapm_widgets); 18139a841ebbSMark Brown 1814f0fba2adSLiam Girdwood /* initialise the sound card only once */ 1815f0fba2adSLiam Girdwood if (card->probe) { 1816e7361ec4SMark Brown ret = card->probe(card); 1817f0fba2adSLiam Girdwood if (ret < 0) 1818f0fba2adSLiam Girdwood goto card_probe_error; 1819f0fba2adSLiam Girdwood } 1820f0fba2adSLiam Girdwood 182162ae68faSStephen Warren /* probe all components used by DAI links on this card */ 18220168bf0dSLiam Girdwood for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 18230168bf0dSLiam Girdwood order++) { 1824fe3e78e0SMark Brown for (i = 0; i < card->num_links; i++) { 182562ae68faSStephen Warren ret = soc_probe_link_components(card, i, order); 182662ae68faSStephen Warren if (ret < 0) { 1827f110bfc7SLiam Girdwood dev_err(card->dev, 1828f110bfc7SLiam Girdwood "ASoC: failed to instantiate card %d\n", 1829f110bfc7SLiam Girdwood ret); 183062ae68faSStephen Warren goto probe_dai_err; 183162ae68faSStephen Warren } 183262ae68faSStephen Warren } 183362ae68faSStephen Warren } 183462ae68faSStephen Warren 183562ae68faSStephen Warren /* probe all DAI links on this card */ 183662ae68faSStephen Warren for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; 183762ae68faSStephen Warren order++) { 183862ae68faSStephen Warren for (i = 0; i < card->num_links; i++) { 183962ae68faSStephen Warren ret = soc_probe_link_dais(card, i, order); 1840fe3e78e0SMark Brown if (ret < 0) { 1841f110bfc7SLiam Girdwood dev_err(card->dev, 1842f110bfc7SLiam Girdwood "ASoC: failed to instantiate card %d\n", 1843f110bfc7SLiam Girdwood ret); 1844f0fba2adSLiam Girdwood goto probe_dai_err; 1845fe3e78e0SMark Brown } 1846fe3e78e0SMark Brown } 18470168bf0dSLiam Girdwood } 1848fe3e78e0SMark Brown 18492eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) { 18502eea392dSJarkko Nikula ret = soc_probe_aux_dev(card, i); 18512eea392dSJarkko Nikula if (ret < 0) { 1852f110bfc7SLiam Girdwood dev_err(card->dev, 1853f110bfc7SLiam Girdwood "ASoC: failed to add auxiliary devices %d\n", 1854f110bfc7SLiam Girdwood ret); 18552eea392dSJarkko Nikula goto probe_aux_dev_err; 18562eea392dSJarkko Nikula } 18572eea392dSJarkko Nikula } 18582eea392dSJarkko Nikula 1859888df395SMark Brown snd_soc_dapm_link_dai_widgets(card); 1860b893ea5fSLiam Girdwood snd_soc_dapm_connect_dai_link_widgets(card); 1861888df395SMark Brown 1862b7af1dafSMark Brown if (card->controls) 1863022658beSLiam Girdwood snd_soc_add_card_controls(card, card->controls, card->num_controls); 1864b7af1dafSMark Brown 1865b8ad29deSMark Brown if (card->dapm_routes) 1866b8ad29deSMark Brown snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, 1867b8ad29deSMark Brown card->num_dapm_routes); 1868b8ad29deSMark Brown 186975d9ac46SMark Brown for (i = 0; i < card->num_links; i++) { 187075d9ac46SMark Brown dai_link = &card->dai_link[i]; 1871f04209a7SMark Brown dai_fmt = dai_link->dai_fmt; 187275d9ac46SMark Brown 1873f04209a7SMark Brown if (dai_fmt) { 187475d9ac46SMark Brown ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai, 1875f04209a7SMark Brown dai_fmt); 18765e4ba569SShawn Guo if (ret != 0 && ret != -ENOTSUPP) 187775d9ac46SMark Brown dev_warn(card->rtd[i].codec_dai->dev, 1878f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 187975d9ac46SMark Brown ret); 1880f04209a7SMark Brown } 1881f04209a7SMark Brown 1882f04209a7SMark Brown /* If this is a regular CPU link there will be a platform */ 1883fe33d4c5SStephen Warren if (dai_fmt && 1884fe33d4c5SStephen Warren (dai_link->platform_name || dai_link->platform_of_node)) { 1885f04209a7SMark Brown ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, 1886f04209a7SMark Brown dai_fmt); 1887f04209a7SMark Brown if (ret != 0 && ret != -ENOTSUPP) 1888f04209a7SMark Brown dev_warn(card->rtd[i].cpu_dai->dev, 1889f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 1890f04209a7SMark Brown ret); 1891f04209a7SMark Brown } else if (dai_fmt) { 1892f04209a7SMark Brown /* Flip the polarity for the "CPU" end */ 1893f04209a7SMark Brown dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK; 1894f04209a7SMark Brown switch (dai_link->dai_fmt & 1895f04209a7SMark Brown SND_SOC_DAIFMT_MASTER_MASK) { 1896f04209a7SMark Brown case SND_SOC_DAIFMT_CBM_CFM: 1897f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 1898f04209a7SMark Brown break; 1899f04209a7SMark Brown case SND_SOC_DAIFMT_CBM_CFS: 1900f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; 1901f04209a7SMark Brown break; 1902f04209a7SMark Brown case SND_SOC_DAIFMT_CBS_CFM: 1903f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; 1904f04209a7SMark Brown break; 1905f04209a7SMark Brown case SND_SOC_DAIFMT_CBS_CFS: 1906f04209a7SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 1907f04209a7SMark Brown break; 1908f04209a7SMark Brown } 190975d9ac46SMark Brown 191075d9ac46SMark Brown ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, 1911f04209a7SMark Brown dai_fmt); 19125e4ba569SShawn Guo if (ret != 0 && ret != -ENOTSUPP) 191375d9ac46SMark Brown dev_warn(card->rtd[i].cpu_dai->dev, 1914f110bfc7SLiam Girdwood "ASoC: Failed to set DAI format: %d\n", 191575d9ac46SMark Brown ret); 191675d9ac46SMark Brown } 191775d9ac46SMark Brown } 191875d9ac46SMark Brown 1919f0fba2adSLiam Girdwood snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), 1920fe3e78e0SMark Brown "%s", card->name); 1921f0fba2adSLiam Girdwood snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), 192222de71baSLiam Girdwood "%s", card->long_name ? card->long_name : card->name); 1923f0e8ed85SMark Brown snprintf(card->snd_card->driver, sizeof(card->snd_card->driver), 1924f0e8ed85SMark Brown "%s", card->driver_name ? card->driver_name : card->name); 1925f0e8ed85SMark Brown for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) { 1926f0e8ed85SMark Brown switch (card->snd_card->driver[i]) { 1927f0e8ed85SMark Brown case '_': 1928f0e8ed85SMark Brown case '-': 1929f0e8ed85SMark Brown case '\0': 1930f0e8ed85SMark Brown break; 1931f0e8ed85SMark Brown default: 1932f0e8ed85SMark Brown if (!isalnum(card->snd_card->driver[i])) 1933f0e8ed85SMark Brown card->snd_card->driver[i] = '_'; 1934f0e8ed85SMark Brown break; 1935f0e8ed85SMark Brown } 1936f0e8ed85SMark Brown } 1937fe3e78e0SMark Brown 193828e9ad92SMark Brown if (card->late_probe) { 193928e9ad92SMark Brown ret = card->late_probe(card); 194028e9ad92SMark Brown if (ret < 0) { 1941f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n", 194228e9ad92SMark Brown card->name, ret); 194328e9ad92SMark Brown goto probe_aux_dev_err; 194428e9ad92SMark Brown } 194528e9ad92SMark Brown } 194628e9ad92SMark Brown 19471633281bSStephen Warren if (card->fully_routed) 19487df37884SLars-Peter Clausen snd_soc_dapm_auto_nc_pins(card); 19491633281bSStephen Warren 1950824ef826SLars-Peter Clausen snd_soc_dapm_new_widgets(card); 19518c193b8dSLars-Peter Clausen 1952f0fba2adSLiam Girdwood ret = snd_card_register(card->snd_card); 1953fe3e78e0SMark Brown if (ret < 0) { 1954f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: failed to register soundcard %d\n", 1955f110bfc7SLiam Girdwood ret); 19566b3ed785SAxel Lin goto probe_aux_dev_err; 1957fe3e78e0SMark Brown } 1958fe3e78e0SMark Brown 1959fe3e78e0SMark Brown #ifdef CONFIG_SND_SOC_AC97_BUS 1960f0fba2adSLiam Girdwood /* register any AC97 codecs */ 1961f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 1962f0fba2adSLiam Girdwood ret = soc_register_ac97_dai_link(&card->rtd[i]); 1963fe3e78e0SMark Brown if (ret < 0) { 196410e8aa9aSMichał Mirosław dev_err(card->dev, 196510e8aa9aSMichał Mirosław "ASoC: failed to register AC97: %d\n", ret); 19666b3ed785SAxel Lin while (--i >= 0) 196702c9c7b9SMisael Lopez Cruz soc_unregister_ac97_dai_link(&card->rtd[i]); 19686b3ed785SAxel Lin goto probe_aux_dev_err; 1969fe3e78e0SMark Brown } 1970fe3e78e0SMark Brown } 1971fe3e78e0SMark Brown #endif 1972fe3e78e0SMark Brown 1973435c5e25SMark Brown card->instantiated = 1; 19744f4c0072SMark Brown snd_soc_dapm_sync(&card->dapm); 1975f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1976b19e6e7bSMark Brown 1977b19e6e7bSMark Brown return 0; 1978db2a4165SFrank Mandarino 19792eea392dSJarkko Nikula probe_aux_dev_err: 19802eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 19812eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 19822eea392dSJarkko Nikula 1983f0fba2adSLiam Girdwood probe_dai_err: 19840671fd8eSKuninori Morimoto soc_remove_dai_links(card); 1985fe3e78e0SMark Brown 1986f0fba2adSLiam Girdwood card_probe_error: 198787506549SMark Brown if (card->remove) 1988e7361ec4SMark Brown card->remove(card); 1989f0fba2adSLiam Girdwood 1990f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 1991f0fba2adSLiam Girdwood 1992b19e6e7bSMark Brown base_error: 1993f0fba2adSLiam Girdwood mutex_unlock(&card->mutex); 1994db2a4165SFrank Mandarino 1995b19e6e7bSMark Brown return ret; 1996435c5e25SMark Brown } 1997435c5e25SMark Brown 1998435c5e25SMark Brown /* probes a new socdev */ 1999435c5e25SMark Brown static int soc_probe(struct platform_device *pdev) 2000435c5e25SMark Brown { 2001f0fba2adSLiam Girdwood struct snd_soc_card *card = platform_get_drvdata(pdev); 2002435c5e25SMark Brown 200370a7ca34SVinod Koul /* 200470a7ca34SVinod Koul * no card, so machine driver should be registering card 200570a7ca34SVinod Koul * we should not be here in that case so ret error 200670a7ca34SVinod Koul */ 200770a7ca34SVinod Koul if (!card) 200870a7ca34SVinod Koul return -EINVAL; 200970a7ca34SVinod Koul 2010fe4085e8SMark Brown dev_warn(&pdev->dev, 2011f110bfc7SLiam Girdwood "ASoC: machine %s should use snd_soc_register_card()\n", 2012fe4085e8SMark Brown card->name); 2013fe4085e8SMark Brown 2014435c5e25SMark Brown /* Bodge while we unpick instantiation */ 2015435c5e25SMark Brown card->dev = &pdev->dev; 2016f0fba2adSLiam Girdwood 201728d528c8SMark Brown return snd_soc_register_card(card); 2018435c5e25SMark Brown } 2019435c5e25SMark Brown 2020b0e26485SVinod Koul static int soc_cleanup_card_resources(struct snd_soc_card *card) 2021db2a4165SFrank Mandarino { 2022db2a4165SFrank Mandarino int i; 2023db2a4165SFrank Mandarino 2024f0fba2adSLiam Girdwood /* make sure any delayed work runs */ 2025f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 2026f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 202743829731STejun Heo flush_delayed_work(&rtd->delayed_work); 2028db2a4165SFrank Mandarino } 2029db2a4165SFrank Mandarino 20302eea392dSJarkko Nikula /* remove auxiliary devices */ 20312eea392dSJarkko Nikula for (i = 0; i < card->num_aux_devs; i++) 20322eea392dSJarkko Nikula soc_remove_aux_dev(card, i); 20332eea392dSJarkko Nikula 2034f0fba2adSLiam Girdwood /* remove and free each DAI */ 20350671fd8eSKuninori Morimoto soc_remove_dai_links(card); 2036f0fba2adSLiam Girdwood 2037a6052154SJarkko Nikula soc_cleanup_card_debugfs(card); 2038a6052154SJarkko Nikula 2039f0fba2adSLiam Girdwood /* remove the card */ 204087506549SMark Brown if (card->remove) 2041e7361ec4SMark Brown card->remove(card); 2042f0fba2adSLiam Girdwood 20430aaae527SLars-Peter Clausen snd_soc_dapm_free(&card->dapm); 20440aaae527SLars-Peter Clausen 2045f0fba2adSLiam Girdwood snd_card_free(card->snd_card); 2046b0e26485SVinod Koul return 0; 2047b0e26485SVinod Koul 2048b2dfa62cSGuennadi Liakhovetski } 2049b0e26485SVinod Koul 2050b0e26485SVinod Koul /* removes a socdev */ 2051b0e26485SVinod Koul static int soc_remove(struct platform_device *pdev) 2052b0e26485SVinod Koul { 2053b0e26485SVinod Koul struct snd_soc_card *card = platform_get_drvdata(pdev); 2054b0e26485SVinod Koul 2055c5af3a2eSMark Brown snd_soc_unregister_card(card); 2056db2a4165SFrank Mandarino return 0; 2057db2a4165SFrank Mandarino } 2058db2a4165SFrank Mandarino 20596f8ab4acSMark Brown int snd_soc_poweroff(struct device *dev) 206051737470SMark Brown { 20616f8ab4acSMark Brown struct snd_soc_card *card = dev_get_drvdata(dev); 2062f0fba2adSLiam Girdwood int i; 206351737470SMark Brown 206451737470SMark Brown if (!card->instantiated) 2065416356fcSMark Brown return 0; 206651737470SMark Brown 206751737470SMark Brown /* Flush out pmdown_time work - we actually do want to run it 206851737470SMark Brown * now, we're shutting down so no imminent restart. */ 2069f0fba2adSLiam Girdwood for (i = 0; i < card->num_rtd; i++) { 2070f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; 207143829731STejun Heo flush_delayed_work(&rtd->delayed_work); 2072f0fba2adSLiam Girdwood } 207351737470SMark Brown 2074f0fba2adSLiam Girdwood snd_soc_dapm_shutdown(card); 2075416356fcSMark Brown 2076988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 2077988e8cc4SNicolin Chen for (i = 0; i < card->num_rtd; i++) { 2078988e8cc4SNicolin Chen struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 2079988e8cc4SNicolin Chen struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai; 2080988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(codec_dai->dev); 2081988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 2082988e8cc4SNicolin Chen } 2083988e8cc4SNicolin Chen 2084416356fcSMark Brown return 0; 208551737470SMark Brown } 20866f8ab4acSMark Brown EXPORT_SYMBOL_GPL(snd_soc_poweroff); 208751737470SMark Brown 20886f8ab4acSMark Brown const struct dev_pm_ops snd_soc_pm_ops = { 2089b1dd5897SViresh Kumar .suspend = snd_soc_suspend, 2090b1dd5897SViresh Kumar .resume = snd_soc_resume, 2091b1dd5897SViresh Kumar .freeze = snd_soc_suspend, 2092b1dd5897SViresh Kumar .thaw = snd_soc_resume, 20936f8ab4acSMark Brown .poweroff = snd_soc_poweroff, 2094b1dd5897SViresh Kumar .restore = snd_soc_resume, 2095416356fcSMark Brown }; 2096deb2607eSStephen Warren EXPORT_SYMBOL_GPL(snd_soc_pm_ops); 2097416356fcSMark Brown 2098db2a4165SFrank Mandarino /* ASoC platform driver */ 2099db2a4165SFrank Mandarino static struct platform_driver soc_driver = { 2100db2a4165SFrank Mandarino .driver = { 2101db2a4165SFrank Mandarino .name = "soc-audio", 21028b45a209SKay Sievers .owner = THIS_MODULE, 21036f8ab4acSMark Brown .pm = &snd_soc_pm_ops, 2104db2a4165SFrank Mandarino }, 2105db2a4165SFrank Mandarino .probe = soc_probe, 2106db2a4165SFrank Mandarino .remove = soc_remove, 2107db2a4165SFrank Mandarino }; 2108db2a4165SFrank Mandarino 2109096e49d5SMark Brown /** 2110db2a4165SFrank Mandarino * snd_soc_new_ac97_codec - initailise AC97 device 2111db2a4165SFrank Mandarino * @codec: audio codec 2112db2a4165SFrank Mandarino * @ops: AC97 bus operations 2113db2a4165SFrank Mandarino * @num: AC97 codec number 2114db2a4165SFrank Mandarino * 2115db2a4165SFrank Mandarino * Initialises AC97 codec resources for use by ad-hoc devices only. 2116db2a4165SFrank Mandarino */ 2117db2a4165SFrank Mandarino int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, 2118db2a4165SFrank Mandarino struct snd_ac97_bus_ops *ops, int num) 2119db2a4165SFrank Mandarino { 2120db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 2121db2a4165SFrank Mandarino 2122db2a4165SFrank Mandarino codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); 2123db2a4165SFrank Mandarino if (codec->ac97 == NULL) { 2124db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2125db2a4165SFrank Mandarino return -ENOMEM; 2126db2a4165SFrank Mandarino } 2127db2a4165SFrank Mandarino 2128db2a4165SFrank Mandarino codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); 2129db2a4165SFrank Mandarino if (codec->ac97->bus == NULL) { 2130db2a4165SFrank Mandarino kfree(codec->ac97); 2131db2a4165SFrank Mandarino codec->ac97 = NULL; 2132db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2133db2a4165SFrank Mandarino return -ENOMEM; 2134db2a4165SFrank Mandarino } 2135db2a4165SFrank Mandarino 2136db2a4165SFrank Mandarino codec->ac97->bus->ops = ops; 2137db2a4165SFrank Mandarino codec->ac97->num = num; 21380562f788SMika Westerberg 21390562f788SMika Westerberg /* 21400562f788SMika Westerberg * Mark the AC97 device to be created by us. This way we ensure that the 21410562f788SMika Westerberg * device will be registered with the device subsystem later on. 21420562f788SMika Westerberg */ 21430562f788SMika Westerberg codec->ac97_created = 1; 21440562f788SMika Westerberg 2145db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2146db2a4165SFrank Mandarino return 0; 2147db2a4165SFrank Mandarino } 2148db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); 2149db2a4165SFrank Mandarino 2150741a509fSMarkus Pargmann static struct snd_ac97_reset_cfg snd_ac97_rst_cfg; 2151741a509fSMarkus Pargmann 2152741a509fSMarkus Pargmann static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97) 2153741a509fSMarkus Pargmann { 2154741a509fSMarkus Pargmann struct pinctrl *pctl = snd_ac97_rst_cfg.pctl; 2155741a509fSMarkus Pargmann 2156741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset); 2157741a509fSMarkus Pargmann 2158741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1); 2159741a509fSMarkus Pargmann 2160741a509fSMarkus Pargmann udelay(10); 2161741a509fSMarkus Pargmann 2162741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0); 2163741a509fSMarkus Pargmann 2164741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run); 2165741a509fSMarkus Pargmann msleep(2); 2166741a509fSMarkus Pargmann } 2167741a509fSMarkus Pargmann 2168741a509fSMarkus Pargmann static void snd_soc_ac97_reset(struct snd_ac97 *ac97) 2169741a509fSMarkus Pargmann { 2170741a509fSMarkus Pargmann struct pinctrl *pctl = snd_ac97_rst_cfg.pctl; 2171741a509fSMarkus Pargmann 2172741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset); 2173741a509fSMarkus Pargmann 2174741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0); 2175741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0); 2176741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0); 2177741a509fSMarkus Pargmann 2178741a509fSMarkus Pargmann udelay(10); 2179741a509fSMarkus Pargmann 2180741a509fSMarkus Pargmann gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1); 2181741a509fSMarkus Pargmann 2182741a509fSMarkus Pargmann pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run); 2183741a509fSMarkus Pargmann msleep(2); 2184741a509fSMarkus Pargmann } 2185741a509fSMarkus Pargmann 2186741a509fSMarkus Pargmann static int snd_soc_ac97_parse_pinctl(struct device *dev, 2187741a509fSMarkus Pargmann struct snd_ac97_reset_cfg *cfg) 2188741a509fSMarkus Pargmann { 2189741a509fSMarkus Pargmann struct pinctrl *p; 2190741a509fSMarkus Pargmann struct pinctrl_state *state; 2191741a509fSMarkus Pargmann int gpio; 2192741a509fSMarkus Pargmann int ret; 2193741a509fSMarkus Pargmann 2194741a509fSMarkus Pargmann p = devm_pinctrl_get(dev); 2195741a509fSMarkus Pargmann if (IS_ERR(p)) { 2196741a509fSMarkus Pargmann dev_err(dev, "Failed to get pinctrl\n"); 2197b7580cdeSChristoph Jaeger return PTR_ERR(p); 2198741a509fSMarkus Pargmann } 2199741a509fSMarkus Pargmann cfg->pctl = p; 2200741a509fSMarkus Pargmann 2201741a509fSMarkus Pargmann state = pinctrl_lookup_state(p, "ac97-reset"); 2202741a509fSMarkus Pargmann if (IS_ERR(state)) { 2203741a509fSMarkus Pargmann dev_err(dev, "Can't find pinctrl state ac97-reset\n"); 2204b7580cdeSChristoph Jaeger return PTR_ERR(state); 2205741a509fSMarkus Pargmann } 2206741a509fSMarkus Pargmann cfg->pstate_reset = state; 2207741a509fSMarkus Pargmann 2208741a509fSMarkus Pargmann state = pinctrl_lookup_state(p, "ac97-warm-reset"); 2209741a509fSMarkus Pargmann if (IS_ERR(state)) { 2210741a509fSMarkus Pargmann dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n"); 2211b7580cdeSChristoph Jaeger return PTR_ERR(state); 2212741a509fSMarkus Pargmann } 2213741a509fSMarkus Pargmann cfg->pstate_warm_reset = state; 2214741a509fSMarkus Pargmann 2215741a509fSMarkus Pargmann state = pinctrl_lookup_state(p, "ac97-running"); 2216741a509fSMarkus Pargmann if (IS_ERR(state)) { 2217741a509fSMarkus Pargmann dev_err(dev, "Can't find pinctrl state ac97-running\n"); 2218b7580cdeSChristoph Jaeger return PTR_ERR(state); 2219741a509fSMarkus Pargmann } 2220741a509fSMarkus Pargmann cfg->pstate_run = state; 2221741a509fSMarkus Pargmann 2222741a509fSMarkus Pargmann gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0); 2223741a509fSMarkus Pargmann if (gpio < 0) { 2224741a509fSMarkus Pargmann dev_err(dev, "Can't find ac97-sync gpio\n"); 2225741a509fSMarkus Pargmann return gpio; 2226741a509fSMarkus Pargmann } 2227741a509fSMarkus Pargmann ret = devm_gpio_request(dev, gpio, "AC97 link sync"); 2228741a509fSMarkus Pargmann if (ret) { 2229741a509fSMarkus Pargmann dev_err(dev, "Failed requesting ac97-sync gpio\n"); 2230741a509fSMarkus Pargmann return ret; 2231741a509fSMarkus Pargmann } 2232741a509fSMarkus Pargmann cfg->gpio_sync = gpio; 2233741a509fSMarkus Pargmann 2234741a509fSMarkus Pargmann gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1); 2235741a509fSMarkus Pargmann if (gpio < 0) { 2236741a509fSMarkus Pargmann dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio); 2237741a509fSMarkus Pargmann return gpio; 2238741a509fSMarkus Pargmann } 2239741a509fSMarkus Pargmann ret = devm_gpio_request(dev, gpio, "AC97 link sdata"); 2240741a509fSMarkus Pargmann if (ret) { 2241741a509fSMarkus Pargmann dev_err(dev, "Failed requesting ac97-sdata gpio\n"); 2242741a509fSMarkus Pargmann return ret; 2243741a509fSMarkus Pargmann } 2244741a509fSMarkus Pargmann cfg->gpio_sdata = gpio; 2245741a509fSMarkus Pargmann 2246741a509fSMarkus Pargmann gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2); 2247741a509fSMarkus Pargmann if (gpio < 0) { 2248741a509fSMarkus Pargmann dev_err(dev, "Can't find ac97-reset gpio\n"); 2249741a509fSMarkus Pargmann return gpio; 2250741a509fSMarkus Pargmann } 2251741a509fSMarkus Pargmann ret = devm_gpio_request(dev, gpio, "AC97 link reset"); 2252741a509fSMarkus Pargmann if (ret) { 2253741a509fSMarkus Pargmann dev_err(dev, "Failed requesting ac97-reset gpio\n"); 2254741a509fSMarkus Pargmann return ret; 2255741a509fSMarkus Pargmann } 2256741a509fSMarkus Pargmann cfg->gpio_reset = gpio; 2257741a509fSMarkus Pargmann 2258741a509fSMarkus Pargmann return 0; 2259741a509fSMarkus Pargmann } 2260741a509fSMarkus Pargmann 2261b047e1ccSMark Brown struct snd_ac97_bus_ops *soc_ac97_ops; 2262f74b5e25SKevin Hilman EXPORT_SYMBOL_GPL(soc_ac97_ops); 2263b047e1ccSMark Brown 2264b047e1ccSMark Brown int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops) 2265b047e1ccSMark Brown { 2266b047e1ccSMark Brown if (ops == soc_ac97_ops) 2267b047e1ccSMark Brown return 0; 2268b047e1ccSMark Brown 2269b047e1ccSMark Brown if (soc_ac97_ops && ops) 2270b047e1ccSMark Brown return -EBUSY; 2271b047e1ccSMark Brown 2272b047e1ccSMark Brown soc_ac97_ops = ops; 2273b047e1ccSMark Brown 2274b047e1ccSMark Brown return 0; 2275b047e1ccSMark Brown } 2276b047e1ccSMark Brown EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops); 2277b047e1ccSMark Brown 2278db2a4165SFrank Mandarino /** 2279741a509fSMarkus Pargmann * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions 2280741a509fSMarkus Pargmann * 2281741a509fSMarkus Pargmann * This function sets the reset and warm_reset properties of ops and parses 2282741a509fSMarkus Pargmann * the device node of pdev to get pinctrl states and gpio numbers to use. 2283741a509fSMarkus Pargmann */ 2284741a509fSMarkus Pargmann int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops, 2285741a509fSMarkus Pargmann struct platform_device *pdev) 2286741a509fSMarkus Pargmann { 2287741a509fSMarkus Pargmann struct device *dev = &pdev->dev; 2288741a509fSMarkus Pargmann struct snd_ac97_reset_cfg cfg; 2289741a509fSMarkus Pargmann int ret; 2290741a509fSMarkus Pargmann 2291741a509fSMarkus Pargmann ret = snd_soc_ac97_parse_pinctl(dev, &cfg); 2292741a509fSMarkus Pargmann if (ret) 2293741a509fSMarkus Pargmann return ret; 2294741a509fSMarkus Pargmann 2295741a509fSMarkus Pargmann ret = snd_soc_set_ac97_ops(ops); 2296741a509fSMarkus Pargmann if (ret) 2297741a509fSMarkus Pargmann return ret; 2298741a509fSMarkus Pargmann 2299741a509fSMarkus Pargmann ops->warm_reset = snd_soc_ac97_warm_reset; 2300741a509fSMarkus Pargmann ops->reset = snd_soc_ac97_reset; 2301741a509fSMarkus Pargmann 2302741a509fSMarkus Pargmann snd_ac97_rst_cfg = cfg; 2303741a509fSMarkus Pargmann return 0; 2304741a509fSMarkus Pargmann } 2305741a509fSMarkus Pargmann EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset); 2306741a509fSMarkus Pargmann 2307741a509fSMarkus Pargmann /** 2308db2a4165SFrank Mandarino * snd_soc_free_ac97_codec - free AC97 codec device 2309db2a4165SFrank Mandarino * @codec: audio codec 2310db2a4165SFrank Mandarino * 2311db2a4165SFrank Mandarino * Frees AC97 codec device resources. 2312db2a4165SFrank Mandarino */ 2313db2a4165SFrank Mandarino void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) 2314db2a4165SFrank Mandarino { 2315db2a4165SFrank Mandarino mutex_lock(&codec->mutex); 2316f0fba2adSLiam Girdwood #ifdef CONFIG_SND_SOC_AC97_BUS 231702c9c7b9SMisael Lopez Cruz soc_unregister_ac97_codec(codec); 2318f0fba2adSLiam Girdwood #endif 2319db2a4165SFrank Mandarino kfree(codec->ac97->bus); 2320db2a4165SFrank Mandarino kfree(codec->ac97); 2321db2a4165SFrank Mandarino codec->ac97 = NULL; 23220562f788SMika Westerberg codec->ac97_created = 0; 2323db2a4165SFrank Mandarino mutex_unlock(&codec->mutex); 2324db2a4165SFrank Mandarino } 2325db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); 2326db2a4165SFrank Mandarino 2327db2a4165SFrank Mandarino /** 2328db2a4165SFrank Mandarino * snd_soc_cnew - create new control 2329db2a4165SFrank Mandarino * @_template: control template 2330db2a4165SFrank Mandarino * @data: control private data 2331ac11a2b3SMark Brown * @long_name: control long name 2332efb7ac3fSMark Brown * @prefix: control name prefix 2333db2a4165SFrank Mandarino * 2334db2a4165SFrank Mandarino * Create a new mixer control from a template control. 2335db2a4165SFrank Mandarino * 2336db2a4165SFrank Mandarino * Returns 0 for success, else error. 2337db2a4165SFrank Mandarino */ 2338db2a4165SFrank Mandarino struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, 23393056557fSMark Brown void *data, const char *long_name, 2340efb7ac3fSMark Brown const char *prefix) 2341db2a4165SFrank Mandarino { 2342db2a4165SFrank Mandarino struct snd_kcontrol_new template; 2343efb7ac3fSMark Brown struct snd_kcontrol *kcontrol; 2344efb7ac3fSMark Brown char *name = NULL; 2345db2a4165SFrank Mandarino 2346db2a4165SFrank Mandarino memcpy(&template, _template, sizeof(template)); 2347db2a4165SFrank Mandarino template.index = 0; 2348db2a4165SFrank Mandarino 2349efb7ac3fSMark Brown if (!long_name) 2350efb7ac3fSMark Brown long_name = template.name; 2351efb7ac3fSMark Brown 2352efb7ac3fSMark Brown if (prefix) { 23532b581074SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); 2354efb7ac3fSMark Brown if (!name) 2355efb7ac3fSMark Brown return NULL; 2356efb7ac3fSMark Brown 2357efb7ac3fSMark Brown template.name = name; 2358efb7ac3fSMark Brown } else { 2359efb7ac3fSMark Brown template.name = long_name; 2360efb7ac3fSMark Brown } 2361efb7ac3fSMark Brown 2362efb7ac3fSMark Brown kcontrol = snd_ctl_new1(&template, data); 2363efb7ac3fSMark Brown 2364efb7ac3fSMark Brown kfree(name); 2365efb7ac3fSMark Brown 2366efb7ac3fSMark Brown return kcontrol; 2367db2a4165SFrank Mandarino } 2368db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_cnew); 2369db2a4165SFrank Mandarino 2370022658beSLiam Girdwood static int snd_soc_add_controls(struct snd_card *card, struct device *dev, 2371022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls, 2372022658beSLiam Girdwood const char *prefix, void *data) 2373022658beSLiam Girdwood { 2374022658beSLiam Girdwood int err, i; 2375022658beSLiam Girdwood 2376022658beSLiam Girdwood for (i = 0; i < num_controls; i++) { 2377022658beSLiam Girdwood const struct snd_kcontrol_new *control = &controls[i]; 2378022658beSLiam Girdwood err = snd_ctl_add(card, snd_soc_cnew(control, data, 2379022658beSLiam Girdwood control->name, prefix)); 2380022658beSLiam Girdwood if (err < 0) { 2381f110bfc7SLiam Girdwood dev_err(dev, "ASoC: Failed to add %s: %d\n", 2382f110bfc7SLiam Girdwood control->name, err); 2383022658beSLiam Girdwood return err; 2384022658beSLiam Girdwood } 2385022658beSLiam Girdwood } 2386022658beSLiam Girdwood 2387022658beSLiam Girdwood return 0; 2388022658beSLiam Girdwood } 2389022658beSLiam Girdwood 23904fefd698SDimitris Papastamos struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card, 23914fefd698SDimitris Papastamos const char *name) 23924fefd698SDimitris Papastamos { 23934fefd698SDimitris Papastamos struct snd_card *card = soc_card->snd_card; 23944fefd698SDimitris Papastamos struct snd_kcontrol *kctl; 23954fefd698SDimitris Papastamos 23964fefd698SDimitris Papastamos if (unlikely(!name)) 23974fefd698SDimitris Papastamos return NULL; 23984fefd698SDimitris Papastamos 23994fefd698SDimitris Papastamos list_for_each_entry(kctl, &card->controls, list) 24004fefd698SDimitris Papastamos if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) 24014fefd698SDimitris Papastamos return kctl; 24024fefd698SDimitris Papastamos return NULL; 24034fefd698SDimitris Papastamos } 24044fefd698SDimitris Papastamos EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol); 24054fefd698SDimitris Papastamos 2406db2a4165SFrank Mandarino /** 2407022658beSLiam Girdwood * snd_soc_add_codec_controls - add an array of controls to a codec. 2408022658beSLiam Girdwood * Convenience function to add a list of controls. Many codecs were 24093e8e1952SIan Molton * duplicating this code. 24103e8e1952SIan Molton * 24113e8e1952SIan Molton * @codec: codec to add controls to 24123e8e1952SIan Molton * @controls: array of controls to add 24133e8e1952SIan Molton * @num_controls: number of elements in the array 24143e8e1952SIan Molton * 24153e8e1952SIan Molton * Return 0 for success, else error. 24163e8e1952SIan Molton */ 2417022658beSLiam Girdwood int snd_soc_add_codec_controls(struct snd_soc_codec *codec, 24183e8e1952SIan Molton const struct snd_kcontrol_new *controls, int num_controls) 24193e8e1952SIan Molton { 2420f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 24213e8e1952SIan Molton 2422022658beSLiam Girdwood return snd_soc_add_controls(card, codec->dev, controls, num_controls, 242394f99c87SLars-Peter Clausen codec->component.name_prefix, &codec->component); 24243e8e1952SIan Molton } 2425022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls); 24263e8e1952SIan Molton 24273e8e1952SIan Molton /** 2428a491a5c8SLiam Girdwood * snd_soc_add_platform_controls - add an array of controls to a platform. 2429022658beSLiam Girdwood * Convenience function to add a list of controls. 2430a491a5c8SLiam Girdwood * 2431a491a5c8SLiam Girdwood * @platform: platform to add controls to 2432a491a5c8SLiam Girdwood * @controls: array of controls to add 2433a491a5c8SLiam Girdwood * @num_controls: number of elements in the array 2434a491a5c8SLiam Girdwood * 2435a491a5c8SLiam Girdwood * Return 0 for success, else error. 2436a491a5c8SLiam Girdwood */ 2437a491a5c8SLiam Girdwood int snd_soc_add_platform_controls(struct snd_soc_platform *platform, 2438a491a5c8SLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2439a491a5c8SLiam Girdwood { 2440a491a5c8SLiam Girdwood struct snd_card *card = platform->card->snd_card; 2441a491a5c8SLiam Girdwood 2442022658beSLiam Girdwood return snd_soc_add_controls(card, platform->dev, controls, num_controls, 2443907fe36aSLars-Peter Clausen NULL, &platform->component); 2444a491a5c8SLiam Girdwood } 2445a491a5c8SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls); 2446a491a5c8SLiam Girdwood 2447a491a5c8SLiam Girdwood /** 2448022658beSLiam Girdwood * snd_soc_add_card_controls - add an array of controls to a SoC card. 2449022658beSLiam Girdwood * Convenience function to add a list of controls. 2450022658beSLiam Girdwood * 2451022658beSLiam Girdwood * @soc_card: SoC card to add controls to 2452022658beSLiam Girdwood * @controls: array of controls to add 2453022658beSLiam Girdwood * @num_controls: number of elements in the array 2454022658beSLiam Girdwood * 2455022658beSLiam Girdwood * Return 0 for success, else error. 2456022658beSLiam Girdwood */ 2457022658beSLiam Girdwood int snd_soc_add_card_controls(struct snd_soc_card *soc_card, 2458022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2459022658beSLiam Girdwood { 2460022658beSLiam Girdwood struct snd_card *card = soc_card->snd_card; 2461022658beSLiam Girdwood 2462022658beSLiam Girdwood return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, 2463022658beSLiam Girdwood NULL, soc_card); 2464022658beSLiam Girdwood } 2465022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); 2466022658beSLiam Girdwood 2467022658beSLiam Girdwood /** 2468022658beSLiam Girdwood * snd_soc_add_dai_controls - add an array of controls to a DAI. 2469022658beSLiam Girdwood * Convienience function to add a list of controls. 2470022658beSLiam Girdwood * 2471022658beSLiam Girdwood * @dai: DAI to add controls to 2472022658beSLiam Girdwood * @controls: array of controls to add 2473022658beSLiam Girdwood * @num_controls: number of elements in the array 2474022658beSLiam Girdwood * 2475022658beSLiam Girdwood * Return 0 for success, else error. 2476022658beSLiam Girdwood */ 2477022658beSLiam Girdwood int snd_soc_add_dai_controls(struct snd_soc_dai *dai, 2478022658beSLiam Girdwood const struct snd_kcontrol_new *controls, int num_controls) 2479022658beSLiam Girdwood { 2480022658beSLiam Girdwood struct snd_card *card = dai->card->snd_card; 2481022658beSLiam Girdwood 2482022658beSLiam Girdwood return snd_soc_add_controls(card, dai->dev, controls, num_controls, 2483022658beSLiam Girdwood NULL, dai); 2484022658beSLiam Girdwood } 2485022658beSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); 2486022658beSLiam Girdwood 2487022658beSLiam Girdwood /** 2488db2a4165SFrank Mandarino * snd_soc_info_enum_double - enumerated double mixer info callback 2489db2a4165SFrank Mandarino * @kcontrol: mixer control 2490db2a4165SFrank Mandarino * @uinfo: control element information 2491db2a4165SFrank Mandarino * 2492db2a4165SFrank Mandarino * Callback to provide information about a double enumerated 2493db2a4165SFrank Mandarino * mixer control. 2494db2a4165SFrank Mandarino * 2495db2a4165SFrank Mandarino * Returns 0 for success. 2496db2a4165SFrank Mandarino */ 2497db2a4165SFrank Mandarino int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, 2498db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2499db2a4165SFrank Mandarino { 2500db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 2501db2a4165SFrank Mandarino 2502db2a4165SFrank Mandarino uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2503db2a4165SFrank Mandarino uinfo->count = e->shift_l == e->shift_r ? 1 : 2; 25049a8d38dbSTakashi Iwai uinfo->value.enumerated.items = e->items; 2505db2a4165SFrank Mandarino 25069a8d38dbSTakashi Iwai if (uinfo->value.enumerated.item >= e->items) 25079a8d38dbSTakashi Iwai uinfo->value.enumerated.item = e->items - 1; 2508a19685cbSTakashi Iwai strlcpy(uinfo->value.enumerated.name, 2509a19685cbSTakashi Iwai e->texts[uinfo->value.enumerated.item], 2510a19685cbSTakashi Iwai sizeof(uinfo->value.enumerated.name)); 2511db2a4165SFrank Mandarino return 0; 2512db2a4165SFrank Mandarino } 2513db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); 2514db2a4165SFrank Mandarino 2515db2a4165SFrank Mandarino /** 2516db2a4165SFrank Mandarino * snd_soc_get_enum_double - enumerated double mixer get callback 2517db2a4165SFrank Mandarino * @kcontrol: mixer control 2518ac11a2b3SMark Brown * @ucontrol: control element information 2519db2a4165SFrank Mandarino * 2520db2a4165SFrank Mandarino * Callback to get the value of a double enumerated mixer. 2521db2a4165SFrank Mandarino * 2522db2a4165SFrank Mandarino * Returns 0 for success. 2523db2a4165SFrank Mandarino */ 2524db2a4165SFrank Mandarino int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, 2525db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2526db2a4165SFrank Mandarino { 2527907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 2528db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 252929ae2fa5SLars-Peter Clausen unsigned int val, item; 253029ae2fa5SLars-Peter Clausen unsigned int reg_val; 2531907fe36aSLars-Peter Clausen int ret; 2532db2a4165SFrank Mandarino 2533907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, e->reg, ®_val); 2534907fe36aSLars-Peter Clausen if (ret) 2535907fe36aSLars-Peter Clausen return ret; 253629ae2fa5SLars-Peter Clausen val = (reg_val >> e->shift_l) & e->mask; 253729ae2fa5SLars-Peter Clausen item = snd_soc_enum_val_to_item(e, val); 253829ae2fa5SLars-Peter Clausen ucontrol->value.enumerated.item[0] = item; 253929ae2fa5SLars-Peter Clausen if (e->shift_l != e->shift_r) { 254029ae2fa5SLars-Peter Clausen val = (reg_val >> e->shift_l) & e->mask; 254129ae2fa5SLars-Peter Clausen item = snd_soc_enum_val_to_item(e, val); 254229ae2fa5SLars-Peter Clausen ucontrol->value.enumerated.item[1] = item; 254329ae2fa5SLars-Peter Clausen } 2544db2a4165SFrank Mandarino 2545db2a4165SFrank Mandarino return 0; 2546db2a4165SFrank Mandarino } 2547db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); 2548db2a4165SFrank Mandarino 2549db2a4165SFrank Mandarino /** 2550db2a4165SFrank Mandarino * snd_soc_put_enum_double - enumerated double mixer put callback 2551db2a4165SFrank Mandarino * @kcontrol: mixer control 2552ac11a2b3SMark Brown * @ucontrol: control element information 2553db2a4165SFrank Mandarino * 2554db2a4165SFrank Mandarino * Callback to set the value of a double enumerated mixer. 2555db2a4165SFrank Mandarino * 2556db2a4165SFrank Mandarino * Returns 0 for success. 2557db2a4165SFrank Mandarino */ 2558db2a4165SFrank Mandarino int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, 2559db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2560db2a4165SFrank Mandarino { 2561907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 2562db2a4165SFrank Mandarino struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 256329ae2fa5SLars-Peter Clausen unsigned int *item = ucontrol->value.enumerated.item; 256446f5822fSDaniel Ribeiro unsigned int val; 256586767b7dSLars-Peter Clausen unsigned int mask; 2566db2a4165SFrank Mandarino 256729ae2fa5SLars-Peter Clausen if (item[0] >= e->items) 2568db2a4165SFrank Mandarino return -EINVAL; 256929ae2fa5SLars-Peter Clausen val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; 257086767b7dSLars-Peter Clausen mask = e->mask << e->shift_l; 2571db2a4165SFrank Mandarino if (e->shift_l != e->shift_r) { 257229ae2fa5SLars-Peter Clausen if (item[1] >= e->items) 2573db2a4165SFrank Mandarino return -EINVAL; 257429ae2fa5SLars-Peter Clausen val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r; 257586767b7dSLars-Peter Clausen mask |= e->mask << e->shift_r; 2576db2a4165SFrank Mandarino } 2577db2a4165SFrank Mandarino 2578907fe36aSLars-Peter Clausen return snd_soc_component_update_bits(component, e->reg, mask, val); 2579db2a4165SFrank Mandarino } 2580db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); 2581db2a4165SFrank Mandarino 2582db2a4165SFrank Mandarino /** 2583f227b88fSMarkus Pargmann * snd_soc_read_signed - Read a codec register and interprete as signed value 2584907fe36aSLars-Peter Clausen * @component: component 2585f227b88fSMarkus Pargmann * @reg: Register to read 2586f227b88fSMarkus Pargmann * @mask: Mask to use after shifting the register value 2587f227b88fSMarkus Pargmann * @shift: Right shift of register value 2588f227b88fSMarkus Pargmann * @sign_bit: Bit that describes if a number is negative or not. 2589907fe36aSLars-Peter Clausen * @signed_val: Pointer to where the read value should be stored 25902e72f8e3SPeter Ujfalusi * 2591f227b88fSMarkus Pargmann * This functions reads a codec register. The register value is shifted right 2592f227b88fSMarkus Pargmann * by 'shift' bits and masked with the given 'mask'. Afterwards it translates 2593f227b88fSMarkus Pargmann * the given registervalue into a signed integer if sign_bit is non-zero. 25942e72f8e3SPeter Ujfalusi * 2595907fe36aSLars-Peter Clausen * Returns 0 on sucess, otherwise an error value 25962e72f8e3SPeter Ujfalusi */ 2597907fe36aSLars-Peter Clausen static int snd_soc_read_signed(struct snd_soc_component *component, 2598907fe36aSLars-Peter Clausen unsigned int reg, unsigned int mask, unsigned int shift, 2599907fe36aSLars-Peter Clausen unsigned int sign_bit, int *signed_val) 26002e72f8e3SPeter Ujfalusi { 2601f227b88fSMarkus Pargmann int ret; 260246f5822fSDaniel Ribeiro unsigned int val; 26032e72f8e3SPeter Ujfalusi 2604907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg, &val); 2605907fe36aSLars-Peter Clausen if (ret < 0) 2606907fe36aSLars-Peter Clausen return ret; 26072e72f8e3SPeter Ujfalusi 2608907fe36aSLars-Peter Clausen val = (val >> shift) & mask; 2609907fe36aSLars-Peter Clausen 2610907fe36aSLars-Peter Clausen if (!sign_bit) { 2611907fe36aSLars-Peter Clausen *signed_val = val; 2612907fe36aSLars-Peter Clausen return 0; 2613907fe36aSLars-Peter Clausen } 2614f227b88fSMarkus Pargmann 2615f227b88fSMarkus Pargmann /* non-negative number */ 2616907fe36aSLars-Peter Clausen if (!(val & BIT(sign_bit))) { 2617907fe36aSLars-Peter Clausen *signed_val = val; 2618907fe36aSLars-Peter Clausen return 0; 2619907fe36aSLars-Peter Clausen } 2620f227b88fSMarkus Pargmann 2621f227b88fSMarkus Pargmann ret = val; 2622f227b88fSMarkus Pargmann 2623f227b88fSMarkus Pargmann /* 2624f227b88fSMarkus Pargmann * The register most probably does not contain a full-sized int. 2625f227b88fSMarkus Pargmann * Instead we have an arbitrary number of bits in a signed 2626f227b88fSMarkus Pargmann * representation which has to be translated into a full-sized int. 2627f227b88fSMarkus Pargmann * This is done by filling up all bits above the sign-bit. 2628f227b88fSMarkus Pargmann */ 2629f227b88fSMarkus Pargmann ret |= ~((int)(BIT(sign_bit) - 1)); 2630f227b88fSMarkus Pargmann 2631907fe36aSLars-Peter Clausen *signed_val = ret; 2632907fe36aSLars-Peter Clausen 2633907fe36aSLars-Peter Clausen return 0; 26342e72f8e3SPeter Ujfalusi } 26352e72f8e3SPeter Ujfalusi 26362e72f8e3SPeter Ujfalusi /** 2637db2a4165SFrank Mandarino * snd_soc_info_volsw - single mixer info callback 2638db2a4165SFrank Mandarino * @kcontrol: mixer control 2639db2a4165SFrank Mandarino * @uinfo: control element information 2640db2a4165SFrank Mandarino * 2641e8f5a103SPeter Ujfalusi * Callback to provide information about a single mixer control, or a double 2642e8f5a103SPeter Ujfalusi * mixer control that spans 2 registers. 2643db2a4165SFrank Mandarino * 2644db2a4165SFrank Mandarino * Returns 0 for success. 2645db2a4165SFrank Mandarino */ 2646db2a4165SFrank Mandarino int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, 2647db2a4165SFrank Mandarino struct snd_ctl_elem_info *uinfo) 2648db2a4165SFrank Mandarino { 26494eaa9819SJon Smirl struct soc_mixer_control *mc = 26504eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2651d11bb4a9SPeter Ujfalusi int platform_max; 2652db2a4165SFrank Mandarino 2653d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2654d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2655d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2656d11bb4a9SPeter Ujfalusi 2657d11bb4a9SPeter Ujfalusi if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) 2658a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2659a7a4ac86SPhilipp Zabel else 2660a7a4ac86SPhilipp Zabel uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2661a7a4ac86SPhilipp Zabel 2662e8f5a103SPeter Ujfalusi uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1; 2663db2a4165SFrank Mandarino uinfo->value.integer.min = 0; 2664f227b88fSMarkus Pargmann uinfo->value.integer.max = platform_max - mc->min; 2665db2a4165SFrank Mandarino return 0; 2666db2a4165SFrank Mandarino } 2667db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_info_volsw); 2668db2a4165SFrank Mandarino 2669db2a4165SFrank Mandarino /** 2670db2a4165SFrank Mandarino * snd_soc_get_volsw - single mixer get callback 2671db2a4165SFrank Mandarino * @kcontrol: mixer control 2672ac11a2b3SMark Brown * @ucontrol: control element information 2673db2a4165SFrank Mandarino * 2674f7915d99SPeter Ujfalusi * Callback to get the value of a single mixer control, or a double mixer 2675f7915d99SPeter Ujfalusi * control that spans 2 registers. 2676db2a4165SFrank Mandarino * 2677db2a4165SFrank Mandarino * Returns 0 for success. 2678db2a4165SFrank Mandarino */ 2679db2a4165SFrank Mandarino int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 2680db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2681db2a4165SFrank Mandarino { 2682907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 26834eaa9819SJon Smirl struct soc_mixer_control *mc = 26844eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2685815ecf8dSJon Smirl unsigned int reg = mc->reg; 2686f7915d99SPeter Ujfalusi unsigned int reg2 = mc->rreg; 2687815ecf8dSJon Smirl unsigned int shift = mc->shift; 2688815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 26894eaa9819SJon Smirl int max = mc->max; 2690f227b88fSMarkus Pargmann int min = mc->min; 2691f227b88fSMarkus Pargmann int sign_bit = mc->sign_bit; 2692815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2693815ecf8dSJon Smirl unsigned int invert = mc->invert; 2694907fe36aSLars-Peter Clausen int val; 2695907fe36aSLars-Peter Clausen int ret; 2696db2a4165SFrank Mandarino 2697f227b88fSMarkus Pargmann if (sign_bit) 2698f227b88fSMarkus Pargmann mask = BIT(sign_bit + 1) - 1; 2699f227b88fSMarkus Pargmann 2700907fe36aSLars-Peter Clausen ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val); 2701907fe36aSLars-Peter Clausen if (ret) 2702907fe36aSLars-Peter Clausen return ret; 2703907fe36aSLars-Peter Clausen 2704907fe36aSLars-Peter Clausen ucontrol->value.integer.value[0] = val - min; 2705f7915d99SPeter Ujfalusi if (invert) 2706db2a4165SFrank Mandarino ucontrol->value.integer.value[0] = 2707a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[0]; 2708f7915d99SPeter Ujfalusi 2709f7915d99SPeter Ujfalusi if (snd_soc_volsw_is_stereo(mc)) { 2710f7915d99SPeter Ujfalusi if (reg == reg2) 2711907fe36aSLars-Peter Clausen ret = snd_soc_read_signed(component, reg, mask, rshift, 2712907fe36aSLars-Peter Clausen sign_bit, &val); 2713f7915d99SPeter Ujfalusi else 2714907fe36aSLars-Peter Clausen ret = snd_soc_read_signed(component, reg2, mask, shift, 2715907fe36aSLars-Peter Clausen sign_bit, &val); 2716907fe36aSLars-Peter Clausen if (ret) 2717907fe36aSLars-Peter Clausen return ret; 2718907fe36aSLars-Peter Clausen 2719907fe36aSLars-Peter Clausen ucontrol->value.integer.value[1] = val - min; 2720f7915d99SPeter Ujfalusi if (invert) 2721db2a4165SFrank Mandarino ucontrol->value.integer.value[1] = 2722a7a4ac86SPhilipp Zabel max - ucontrol->value.integer.value[1]; 2723db2a4165SFrank Mandarino } 2724db2a4165SFrank Mandarino 2725db2a4165SFrank Mandarino return 0; 2726db2a4165SFrank Mandarino } 2727db2a4165SFrank Mandarino EXPORT_SYMBOL_GPL(snd_soc_get_volsw); 2728db2a4165SFrank Mandarino 2729db2a4165SFrank Mandarino /** 2730db2a4165SFrank Mandarino * snd_soc_put_volsw - single mixer put callback 2731db2a4165SFrank Mandarino * @kcontrol: mixer control 2732ac11a2b3SMark Brown * @ucontrol: control element information 2733db2a4165SFrank Mandarino * 2734974815baSPeter Ujfalusi * Callback to set the value of a single mixer control, or a double mixer 2735974815baSPeter Ujfalusi * control that spans 2 registers. 2736db2a4165SFrank Mandarino * 2737db2a4165SFrank Mandarino * Returns 0 for success. 2738db2a4165SFrank Mandarino */ 2739db2a4165SFrank Mandarino int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 2740db2a4165SFrank Mandarino struct snd_ctl_elem_value *ucontrol) 2741db2a4165SFrank Mandarino { 2742907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 27434eaa9819SJon Smirl struct soc_mixer_control *mc = 27444eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2745815ecf8dSJon Smirl unsigned int reg = mc->reg; 2746974815baSPeter Ujfalusi unsigned int reg2 = mc->rreg; 2747815ecf8dSJon Smirl unsigned int shift = mc->shift; 2748815ecf8dSJon Smirl unsigned int rshift = mc->rshift; 27494eaa9819SJon Smirl int max = mc->max; 2750f227b88fSMarkus Pargmann int min = mc->min; 2751f227b88fSMarkus Pargmann unsigned int sign_bit = mc->sign_bit; 2752815ecf8dSJon Smirl unsigned int mask = (1 << fls(max)) - 1; 2753815ecf8dSJon Smirl unsigned int invert = mc->invert; 2754974815baSPeter Ujfalusi int err; 2755939d9f16SNenghua Cao bool type_2r = false; 2756974815baSPeter Ujfalusi unsigned int val2 = 0; 2757974815baSPeter Ujfalusi unsigned int val, val_mask; 2758db2a4165SFrank Mandarino 2759f227b88fSMarkus Pargmann if (sign_bit) 2760f227b88fSMarkus Pargmann mask = BIT(sign_bit + 1) - 1; 2761f227b88fSMarkus Pargmann 2762f227b88fSMarkus Pargmann val = ((ucontrol->value.integer.value[0] + min) & mask); 2763db2a4165SFrank Mandarino if (invert) 2764a7a4ac86SPhilipp Zabel val = max - val; 2765db2a4165SFrank Mandarino val_mask = mask << shift; 2766db2a4165SFrank Mandarino val = val << shift; 2767974815baSPeter Ujfalusi if (snd_soc_volsw_is_stereo(mc)) { 2768f227b88fSMarkus Pargmann val2 = ((ucontrol->value.integer.value[1] + min) & mask); 2769db2a4165SFrank Mandarino if (invert) 2770a7a4ac86SPhilipp Zabel val2 = max - val2; 2771974815baSPeter Ujfalusi if (reg == reg2) { 2772db2a4165SFrank Mandarino val_mask |= mask << rshift; 2773db2a4165SFrank Mandarino val |= val2 << rshift; 2774974815baSPeter Ujfalusi } else { 2775db2a4165SFrank Mandarino val2 = val2 << shift; 2776939d9f16SNenghua Cao type_2r = true; 2777974815baSPeter Ujfalusi } 2778974815baSPeter Ujfalusi } 2779907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, reg, val_mask, val); 27803ff3f64bSMark Brown if (err < 0) 2781db2a4165SFrank Mandarino return err; 2782db2a4165SFrank Mandarino 2783974815baSPeter Ujfalusi if (type_2r) 2784907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, reg2, val_mask, 2785907fe36aSLars-Peter Clausen val2); 2786974815baSPeter Ujfalusi 2787db2a4165SFrank Mandarino return err; 2788db2a4165SFrank Mandarino } 2789974815baSPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_put_volsw); 2790db2a4165SFrank Mandarino 2791e13ac2e9SMark Brown /** 27921d99f243SBrian Austin * snd_soc_get_volsw_sx - single mixer get callback 27931d99f243SBrian Austin * @kcontrol: mixer control 27941d99f243SBrian Austin * @ucontrol: control element information 27951d99f243SBrian Austin * 27961d99f243SBrian Austin * Callback to get the value of a single mixer control, or a double mixer 27971d99f243SBrian Austin * control that spans 2 registers. 27981d99f243SBrian Austin * 27991d99f243SBrian Austin * Returns 0 for success. 28001d99f243SBrian Austin */ 28011d99f243SBrian Austin int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol, 28021d99f243SBrian Austin struct snd_ctl_elem_value *ucontrol) 28031d99f243SBrian Austin { 2804907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 28051d99f243SBrian Austin struct soc_mixer_control *mc = 28061d99f243SBrian Austin (struct soc_mixer_control *)kcontrol->private_value; 28071d99f243SBrian Austin unsigned int reg = mc->reg; 28081d99f243SBrian Austin unsigned int reg2 = mc->rreg; 28091d99f243SBrian Austin unsigned int shift = mc->shift; 28101d99f243SBrian Austin unsigned int rshift = mc->rshift; 28111d99f243SBrian Austin int max = mc->max; 28121d99f243SBrian Austin int min = mc->min; 28131d99f243SBrian Austin int mask = (1 << (fls(min + max) - 1)) - 1; 2814907fe36aSLars-Peter Clausen unsigned int val; 2815907fe36aSLars-Peter Clausen int ret; 28161d99f243SBrian Austin 2817907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg, &val); 2818907fe36aSLars-Peter Clausen if (ret < 0) 2819907fe36aSLars-Peter Clausen return ret; 28201d99f243SBrian Austin 2821907fe36aSLars-Peter Clausen ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask; 2822907fe36aSLars-Peter Clausen 2823907fe36aSLars-Peter Clausen if (snd_soc_volsw_is_stereo(mc)) { 2824907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg2, &val); 2825907fe36aSLars-Peter Clausen if (ret < 0) 2826907fe36aSLars-Peter Clausen return ret; 2827907fe36aSLars-Peter Clausen 2828907fe36aSLars-Peter Clausen val = ((val >> rshift) - min) & mask; 2829907fe36aSLars-Peter Clausen ucontrol->value.integer.value[1] = val; 2830907fe36aSLars-Peter Clausen } 28311d99f243SBrian Austin 28321d99f243SBrian Austin return 0; 28331d99f243SBrian Austin } 28341d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx); 28351d99f243SBrian Austin 28361d99f243SBrian Austin /** 28371d99f243SBrian Austin * snd_soc_put_volsw_sx - double mixer set callback 28381d99f243SBrian Austin * @kcontrol: mixer control 28391d99f243SBrian Austin * @uinfo: control element information 28401d99f243SBrian Austin * 28411d99f243SBrian Austin * Callback to set the value of a double mixer control that spans 2 registers. 28421d99f243SBrian Austin * 28431d99f243SBrian Austin * Returns 0 for success. 28441d99f243SBrian Austin */ 28451d99f243SBrian Austin int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol, 28461d99f243SBrian Austin struct snd_ctl_elem_value *ucontrol) 28471d99f243SBrian Austin { 2848907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 28491d99f243SBrian Austin struct soc_mixer_control *mc = 28501d99f243SBrian Austin (struct soc_mixer_control *)kcontrol->private_value; 28511d99f243SBrian Austin 28521d99f243SBrian Austin unsigned int reg = mc->reg; 28531d99f243SBrian Austin unsigned int reg2 = mc->rreg; 28541d99f243SBrian Austin unsigned int shift = mc->shift; 28551d99f243SBrian Austin unsigned int rshift = mc->rshift; 28561d99f243SBrian Austin int max = mc->max; 28571d99f243SBrian Austin int min = mc->min; 28581d99f243SBrian Austin int mask = (1 << (fls(min + max) - 1)) - 1; 285927f1d759SBrian Austin int err = 0; 28601a39019eSBard Liao unsigned int val, val_mask, val2 = 0; 28611d99f243SBrian Austin 28621d99f243SBrian Austin val_mask = mask << shift; 28631d99f243SBrian Austin val = (ucontrol->value.integer.value[0] + min) & mask; 28641d99f243SBrian Austin val = val << shift; 28651d99f243SBrian Austin 2866907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, reg, val_mask, val); 2867d055852eSMukund Navada if (err < 0) 28681d99f243SBrian Austin return err; 28691d99f243SBrian Austin 28701d99f243SBrian Austin if (snd_soc_volsw_is_stereo(mc)) { 28711d99f243SBrian Austin val_mask = mask << rshift; 28721d99f243SBrian Austin val2 = (ucontrol->value.integer.value[1] + min) & mask; 28731d99f243SBrian Austin val2 = val2 << rshift; 28741d99f243SBrian Austin 2875907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, reg2, val_mask, 2876907fe36aSLars-Peter Clausen val2); 28771d99f243SBrian Austin } 2878907fe36aSLars-Peter Clausen return err; 28791d99f243SBrian Austin } 28801d99f243SBrian Austin EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx); 28811d99f243SBrian Austin 28821d99f243SBrian Austin /** 2883e13ac2e9SMark Brown * snd_soc_info_volsw_s8 - signed mixer info callback 2884e13ac2e9SMark Brown * @kcontrol: mixer control 2885e13ac2e9SMark Brown * @uinfo: control element information 2886e13ac2e9SMark Brown * 2887e13ac2e9SMark Brown * Callback to provide information about a signed mixer control. 2888e13ac2e9SMark Brown * 2889e13ac2e9SMark Brown * Returns 0 for success. 2890e13ac2e9SMark Brown */ 2891e13ac2e9SMark Brown int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, 2892e13ac2e9SMark Brown struct snd_ctl_elem_info *uinfo) 2893e13ac2e9SMark Brown { 28944eaa9819SJon Smirl struct soc_mixer_control *mc = 28954eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2896d11bb4a9SPeter Ujfalusi int platform_max; 28974eaa9819SJon Smirl int min = mc->min; 2898e13ac2e9SMark Brown 2899d11bb4a9SPeter Ujfalusi if (!mc->platform_max) 2900d11bb4a9SPeter Ujfalusi mc->platform_max = mc->max; 2901d11bb4a9SPeter Ujfalusi platform_max = mc->platform_max; 2902d11bb4a9SPeter Ujfalusi 2903e13ac2e9SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2904e13ac2e9SMark Brown uinfo->count = 2; 2905e13ac2e9SMark Brown uinfo->value.integer.min = 0; 2906d11bb4a9SPeter Ujfalusi uinfo->value.integer.max = platform_max - min; 2907e13ac2e9SMark Brown return 0; 2908e13ac2e9SMark Brown } 2909e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); 2910e13ac2e9SMark Brown 2911e13ac2e9SMark Brown /** 2912e13ac2e9SMark Brown * snd_soc_get_volsw_s8 - signed mixer get callback 2913e13ac2e9SMark Brown * @kcontrol: mixer control 2914ac11a2b3SMark Brown * @ucontrol: control element information 2915e13ac2e9SMark Brown * 2916e13ac2e9SMark Brown * Callback to get the value of a signed mixer control. 2917e13ac2e9SMark Brown * 2918e13ac2e9SMark Brown * Returns 0 for success. 2919e13ac2e9SMark Brown */ 2920e13ac2e9SMark Brown int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, 2921e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2922e13ac2e9SMark Brown { 29234eaa9819SJon Smirl struct soc_mixer_control *mc = 29244eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2925907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 2926815ecf8dSJon Smirl unsigned int reg = mc->reg; 2927907fe36aSLars-Peter Clausen unsigned int val; 29284eaa9819SJon Smirl int min = mc->min; 2929907fe36aSLars-Peter Clausen int ret; 2930907fe36aSLars-Peter Clausen 2931907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg, &val); 2932907fe36aSLars-Peter Clausen if (ret) 2933907fe36aSLars-Peter Clausen return ret; 2934e13ac2e9SMark Brown 2935e13ac2e9SMark Brown ucontrol->value.integer.value[0] = 2936e13ac2e9SMark Brown ((signed char)(val & 0xff))-min; 2937e13ac2e9SMark Brown ucontrol->value.integer.value[1] = 2938e13ac2e9SMark Brown ((signed char)((val >> 8) & 0xff))-min; 2939e13ac2e9SMark Brown return 0; 2940e13ac2e9SMark Brown } 2941e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); 2942e13ac2e9SMark Brown 2943e13ac2e9SMark Brown /** 2944e13ac2e9SMark Brown * snd_soc_put_volsw_sgn - signed mixer put callback 2945e13ac2e9SMark Brown * @kcontrol: mixer control 2946ac11a2b3SMark Brown * @ucontrol: control element information 2947e13ac2e9SMark Brown * 2948e13ac2e9SMark Brown * Callback to set the value of a signed mixer control. 2949e13ac2e9SMark Brown * 2950e13ac2e9SMark Brown * Returns 0 for success. 2951e13ac2e9SMark Brown */ 2952e13ac2e9SMark Brown int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, 2953e13ac2e9SMark Brown struct snd_ctl_elem_value *ucontrol) 2954e13ac2e9SMark Brown { 29554eaa9819SJon Smirl struct soc_mixer_control *mc = 29564eaa9819SJon Smirl (struct soc_mixer_control *)kcontrol->private_value; 2957907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 2958815ecf8dSJon Smirl unsigned int reg = mc->reg; 29594eaa9819SJon Smirl int min = mc->min; 296046f5822fSDaniel Ribeiro unsigned int val; 2961e13ac2e9SMark Brown 2962e13ac2e9SMark Brown val = (ucontrol->value.integer.value[0]+min) & 0xff; 2963e13ac2e9SMark Brown val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; 2964e13ac2e9SMark Brown 2965907fe36aSLars-Peter Clausen return snd_soc_component_update_bits(component, reg, 0xffff, val); 2966e13ac2e9SMark Brown } 2967e13ac2e9SMark Brown EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); 2968e13ac2e9SMark Brown 29698c6529dbSLiam Girdwood /** 29706c9d8cf6SAdam Thomson * snd_soc_info_volsw_range - single mixer info callback with range. 29716c9d8cf6SAdam Thomson * @kcontrol: mixer control 29726c9d8cf6SAdam Thomson * @uinfo: control element information 29736c9d8cf6SAdam Thomson * 29746c9d8cf6SAdam Thomson * Callback to provide information, within a range, about a single 29756c9d8cf6SAdam Thomson * mixer control. 29766c9d8cf6SAdam Thomson * 29776c9d8cf6SAdam Thomson * returns 0 for success. 29786c9d8cf6SAdam Thomson */ 29796c9d8cf6SAdam Thomson int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol, 29806c9d8cf6SAdam Thomson struct snd_ctl_elem_info *uinfo) 29816c9d8cf6SAdam Thomson { 29826c9d8cf6SAdam Thomson struct soc_mixer_control *mc = 29836c9d8cf6SAdam Thomson (struct soc_mixer_control *)kcontrol->private_value; 29846c9d8cf6SAdam Thomson int platform_max; 29856c9d8cf6SAdam Thomson int min = mc->min; 29866c9d8cf6SAdam Thomson 29876c9d8cf6SAdam Thomson if (!mc->platform_max) 29886c9d8cf6SAdam Thomson mc->platform_max = mc->max; 29896c9d8cf6SAdam Thomson platform_max = mc->platform_max; 29906c9d8cf6SAdam Thomson 29916c9d8cf6SAdam Thomson uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 29929bde4f0bSMark Brown uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1; 29936c9d8cf6SAdam Thomson uinfo->value.integer.min = 0; 29946c9d8cf6SAdam Thomson uinfo->value.integer.max = platform_max - min; 29956c9d8cf6SAdam Thomson 29966c9d8cf6SAdam Thomson return 0; 29976c9d8cf6SAdam Thomson } 29986c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range); 29996c9d8cf6SAdam Thomson 30006c9d8cf6SAdam Thomson /** 30016c9d8cf6SAdam Thomson * snd_soc_put_volsw_range - single mixer put value callback with range. 30026c9d8cf6SAdam Thomson * @kcontrol: mixer control 30036c9d8cf6SAdam Thomson * @ucontrol: control element information 30046c9d8cf6SAdam Thomson * 30056c9d8cf6SAdam Thomson * Callback to set the value, within a range, for a single mixer control. 30066c9d8cf6SAdam Thomson * 30076c9d8cf6SAdam Thomson * Returns 0 for success. 30086c9d8cf6SAdam Thomson */ 30096c9d8cf6SAdam Thomson int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol, 30106c9d8cf6SAdam Thomson struct snd_ctl_elem_value *ucontrol) 30116c9d8cf6SAdam Thomson { 30126c9d8cf6SAdam Thomson struct soc_mixer_control *mc = 30136c9d8cf6SAdam Thomson (struct soc_mixer_control *)kcontrol->private_value; 3014907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 30156c9d8cf6SAdam Thomson unsigned int reg = mc->reg; 30169bde4f0bSMark Brown unsigned int rreg = mc->rreg; 30176c9d8cf6SAdam Thomson unsigned int shift = mc->shift; 30186c9d8cf6SAdam Thomson int min = mc->min; 30196c9d8cf6SAdam Thomson int max = mc->max; 30206c9d8cf6SAdam Thomson unsigned int mask = (1 << fls(max)) - 1; 30216c9d8cf6SAdam Thomson unsigned int invert = mc->invert; 30226c9d8cf6SAdam Thomson unsigned int val, val_mask; 30239bde4f0bSMark Brown int ret; 30246c9d8cf6SAdam Thomson 30256c9d8cf6SAdam Thomson val = ((ucontrol->value.integer.value[0] + min) & mask); 30266c9d8cf6SAdam Thomson if (invert) 30276c9d8cf6SAdam Thomson val = max - val; 30286c9d8cf6SAdam Thomson val_mask = mask << shift; 30296c9d8cf6SAdam Thomson val = val << shift; 30306c9d8cf6SAdam Thomson 3031907fe36aSLars-Peter Clausen ret = snd_soc_component_update_bits(component, reg, val_mask, val); 30320eaa6ccaSJoonyoung Shim if (ret < 0) 30339bde4f0bSMark Brown return ret; 30349bde4f0bSMark Brown 30359bde4f0bSMark Brown if (snd_soc_volsw_is_stereo(mc)) { 30369bde4f0bSMark Brown val = ((ucontrol->value.integer.value[1] + min) & mask); 30379bde4f0bSMark Brown if (invert) 30389bde4f0bSMark Brown val = max - val; 30399bde4f0bSMark Brown val_mask = mask << shift; 30409bde4f0bSMark Brown val = val << shift; 30419bde4f0bSMark Brown 3042907fe36aSLars-Peter Clausen ret = snd_soc_component_update_bits(component, rreg, val_mask, 3043907fe36aSLars-Peter Clausen val); 30449bde4f0bSMark Brown } 30459bde4f0bSMark Brown 30469bde4f0bSMark Brown return ret; 30476c9d8cf6SAdam Thomson } 30486c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range); 30496c9d8cf6SAdam Thomson 30506c9d8cf6SAdam Thomson /** 30516c9d8cf6SAdam Thomson * snd_soc_get_volsw_range - single mixer get callback with range 30526c9d8cf6SAdam Thomson * @kcontrol: mixer control 30536c9d8cf6SAdam Thomson * @ucontrol: control element information 30546c9d8cf6SAdam Thomson * 30556c9d8cf6SAdam Thomson * Callback to get the value, within a range, of a single mixer control. 30566c9d8cf6SAdam Thomson * 30576c9d8cf6SAdam Thomson * Returns 0 for success. 30586c9d8cf6SAdam Thomson */ 30596c9d8cf6SAdam Thomson int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol, 30606c9d8cf6SAdam Thomson struct snd_ctl_elem_value *ucontrol) 30616c9d8cf6SAdam Thomson { 3062907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 30636c9d8cf6SAdam Thomson struct soc_mixer_control *mc = 30646c9d8cf6SAdam Thomson (struct soc_mixer_control *)kcontrol->private_value; 30656c9d8cf6SAdam Thomson unsigned int reg = mc->reg; 30669bde4f0bSMark Brown unsigned int rreg = mc->rreg; 30676c9d8cf6SAdam Thomson unsigned int shift = mc->shift; 30686c9d8cf6SAdam Thomson int min = mc->min; 30696c9d8cf6SAdam Thomson int max = mc->max; 30706c9d8cf6SAdam Thomson unsigned int mask = (1 << fls(max)) - 1; 30716c9d8cf6SAdam Thomson unsigned int invert = mc->invert; 3072907fe36aSLars-Peter Clausen unsigned int val; 3073907fe36aSLars-Peter Clausen int ret; 30746c9d8cf6SAdam Thomson 3075907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg, &val); 3076907fe36aSLars-Peter Clausen if (ret) 3077907fe36aSLars-Peter Clausen return ret; 3078907fe36aSLars-Peter Clausen 3079907fe36aSLars-Peter Clausen ucontrol->value.integer.value[0] = (val >> shift) & mask; 30806c9d8cf6SAdam Thomson if (invert) 30816c9d8cf6SAdam Thomson ucontrol->value.integer.value[0] = 30826c9d8cf6SAdam Thomson max - ucontrol->value.integer.value[0]; 30836c9d8cf6SAdam Thomson ucontrol->value.integer.value[0] = 30846c9d8cf6SAdam Thomson ucontrol->value.integer.value[0] - min; 30856c9d8cf6SAdam Thomson 30869bde4f0bSMark Brown if (snd_soc_volsw_is_stereo(mc)) { 3087907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, rreg, &val); 3088907fe36aSLars-Peter Clausen if (ret) 3089907fe36aSLars-Peter Clausen return ret; 3090907fe36aSLars-Peter Clausen 3091907fe36aSLars-Peter Clausen ucontrol->value.integer.value[1] = (val >> shift) & mask; 30929bde4f0bSMark Brown if (invert) 30939bde4f0bSMark Brown ucontrol->value.integer.value[1] = 30949bde4f0bSMark Brown max - ucontrol->value.integer.value[1]; 30959bde4f0bSMark Brown ucontrol->value.integer.value[1] = 30969bde4f0bSMark Brown ucontrol->value.integer.value[1] - min; 30979bde4f0bSMark Brown } 30989bde4f0bSMark Brown 30996c9d8cf6SAdam Thomson return 0; 31006c9d8cf6SAdam Thomson } 31016c9d8cf6SAdam Thomson EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range); 31026c9d8cf6SAdam Thomson 31036c9d8cf6SAdam Thomson /** 3104637d3847SPeter Ujfalusi * snd_soc_limit_volume - Set new limit to an existing volume control. 3105637d3847SPeter Ujfalusi * 3106637d3847SPeter Ujfalusi * @codec: where to look for the control 3107637d3847SPeter Ujfalusi * @name: Name of the control 3108637d3847SPeter Ujfalusi * @max: new maximum limit 3109637d3847SPeter Ujfalusi * 3110637d3847SPeter Ujfalusi * Return 0 for success, else error. 3111637d3847SPeter Ujfalusi */ 3112637d3847SPeter Ujfalusi int snd_soc_limit_volume(struct snd_soc_codec *codec, 3113637d3847SPeter Ujfalusi const char *name, int max) 3114637d3847SPeter Ujfalusi { 3115f0fba2adSLiam Girdwood struct snd_card *card = codec->card->snd_card; 3116637d3847SPeter Ujfalusi struct snd_kcontrol *kctl; 3117637d3847SPeter Ujfalusi struct soc_mixer_control *mc; 3118637d3847SPeter Ujfalusi int found = 0; 3119637d3847SPeter Ujfalusi int ret = -EINVAL; 3120637d3847SPeter Ujfalusi 3121637d3847SPeter Ujfalusi /* Sanity check for name and max */ 3122637d3847SPeter Ujfalusi if (unlikely(!name || max <= 0)) 3123637d3847SPeter Ujfalusi return -EINVAL; 3124637d3847SPeter Ujfalusi 3125637d3847SPeter Ujfalusi list_for_each_entry(kctl, &card->controls, list) { 3126637d3847SPeter Ujfalusi if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) { 3127637d3847SPeter Ujfalusi found = 1; 3128637d3847SPeter Ujfalusi break; 3129637d3847SPeter Ujfalusi } 3130637d3847SPeter Ujfalusi } 3131637d3847SPeter Ujfalusi if (found) { 3132637d3847SPeter Ujfalusi mc = (struct soc_mixer_control *)kctl->private_value; 3133637d3847SPeter Ujfalusi if (max <= mc->max) { 3134d11bb4a9SPeter Ujfalusi mc->platform_max = max; 3135637d3847SPeter Ujfalusi ret = 0; 3136637d3847SPeter Ujfalusi } 3137637d3847SPeter Ujfalusi } 3138637d3847SPeter Ujfalusi return ret; 3139637d3847SPeter Ujfalusi } 3140637d3847SPeter Ujfalusi EXPORT_SYMBOL_GPL(snd_soc_limit_volume); 3141637d3847SPeter Ujfalusi 314271d08516SMark Brown int snd_soc_bytes_info(struct snd_kcontrol *kcontrol, 314371d08516SMark Brown struct snd_ctl_elem_info *uinfo) 314471d08516SMark Brown { 3145907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 314671d08516SMark Brown struct soc_bytes *params = (void *)kcontrol->private_value; 314771d08516SMark Brown 314871d08516SMark Brown uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; 3149907fe36aSLars-Peter Clausen uinfo->count = params->num_regs * component->val_bytes; 315071d08516SMark Brown 315171d08516SMark Brown return 0; 315271d08516SMark Brown } 315371d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_info); 315471d08516SMark Brown 315571d08516SMark Brown int snd_soc_bytes_get(struct snd_kcontrol *kcontrol, 315671d08516SMark Brown struct snd_ctl_elem_value *ucontrol) 315771d08516SMark Brown { 3158907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 315971d08516SMark Brown struct soc_bytes *params = (void *)kcontrol->private_value; 316071d08516SMark Brown int ret; 316171d08516SMark Brown 3162907fe36aSLars-Peter Clausen if (component->regmap) 3163907fe36aSLars-Peter Clausen ret = regmap_raw_read(component->regmap, params->base, 316471d08516SMark Brown ucontrol->value.bytes.data, 3165907fe36aSLars-Peter Clausen params->num_regs * component->val_bytes); 316671d08516SMark Brown else 316771d08516SMark Brown ret = -EINVAL; 316871d08516SMark Brown 3169f831b055SMark Brown /* Hide any masked bytes to ensure consistent data reporting */ 3170f831b055SMark Brown if (ret == 0 && params->mask) { 3171907fe36aSLars-Peter Clausen switch (component->val_bytes) { 3172f831b055SMark Brown case 1: 3173f831b055SMark Brown ucontrol->value.bytes.data[0] &= ~params->mask; 3174f831b055SMark Brown break; 3175f831b055SMark Brown case 2: 3176f831b055SMark Brown ((u16 *)(&ucontrol->value.bytes.data))[0] 31778f1ec93aSCharles Keepax &= cpu_to_be16(~params->mask); 3178f831b055SMark Brown break; 3179f831b055SMark Brown case 4: 3180f831b055SMark Brown ((u32 *)(&ucontrol->value.bytes.data))[0] 31818f1ec93aSCharles Keepax &= cpu_to_be32(~params->mask); 3182f831b055SMark Brown break; 3183f831b055SMark Brown default: 3184f831b055SMark Brown return -EINVAL; 3185f831b055SMark Brown } 3186f831b055SMark Brown } 3187f831b055SMark Brown 318871d08516SMark Brown return ret; 318971d08516SMark Brown } 319071d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_get); 319171d08516SMark Brown 319271d08516SMark Brown int snd_soc_bytes_put(struct snd_kcontrol *kcontrol, 319371d08516SMark Brown struct snd_ctl_elem_value *ucontrol) 319471d08516SMark Brown { 3195907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 319671d08516SMark Brown struct soc_bytes *params = (void *)kcontrol->private_value; 3197f831b055SMark Brown int ret, len; 3198a1a564edSNenghua Cao unsigned int val, mask; 3199f831b055SMark Brown void *data; 320071d08516SMark Brown 3201907fe36aSLars-Peter Clausen if (!component->regmap) 3202f831b055SMark Brown return -EINVAL; 3203f831b055SMark Brown 3204907fe36aSLars-Peter Clausen len = params->num_regs * component->val_bytes; 3205f831b055SMark Brown 3206b5a8fe43SMark Brown data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA); 3207b5a8fe43SMark Brown if (!data) 3208b5a8fe43SMark Brown return -ENOMEM; 3209b5a8fe43SMark Brown 3210f831b055SMark Brown /* 3211f831b055SMark Brown * If we've got a mask then we need to preserve the register 3212f831b055SMark Brown * bits. We shouldn't modify the incoming data so take a 3213f831b055SMark Brown * copy. 3214f831b055SMark Brown */ 3215f831b055SMark Brown if (params->mask) { 3216907fe36aSLars-Peter Clausen ret = regmap_read(component->regmap, params->base, &val); 3217f831b055SMark Brown if (ret != 0) 3218e8b18addSWei Yongjun goto out; 3219f831b055SMark Brown 3220f831b055SMark Brown val &= params->mask; 3221f831b055SMark Brown 3222907fe36aSLars-Peter Clausen switch (component->val_bytes) { 3223f831b055SMark Brown case 1: 3224f831b055SMark Brown ((u8 *)data)[0] &= ~params->mask; 3225f831b055SMark Brown ((u8 *)data)[0] |= val; 3226f831b055SMark Brown break; 3227f831b055SMark Brown case 2: 3228a1a564edSNenghua Cao mask = ~params->mask; 3229907fe36aSLars-Peter Clausen ret = regmap_parse_val(component->regmap, 3230a1a564edSNenghua Cao &mask, &mask); 3231a1a564edSNenghua Cao if (ret != 0) 3232a1a564edSNenghua Cao goto out; 3233a1a564edSNenghua Cao 3234a1a564edSNenghua Cao ((u16 *)data)[0] &= mask; 3235a1a564edSNenghua Cao 3236907fe36aSLars-Peter Clausen ret = regmap_parse_val(component->regmap, 3237a1a564edSNenghua Cao &val, &val); 3238a1a564edSNenghua Cao if (ret != 0) 3239a1a564edSNenghua Cao goto out; 3240a1a564edSNenghua Cao 3241a1a564edSNenghua Cao ((u16 *)data)[0] |= val; 3242f831b055SMark Brown break; 3243f831b055SMark Brown case 4: 3244a1a564edSNenghua Cao mask = ~params->mask; 3245907fe36aSLars-Peter Clausen ret = regmap_parse_val(component->regmap, 3246a1a564edSNenghua Cao &mask, &mask); 3247a1a564edSNenghua Cao if (ret != 0) 3248a1a564edSNenghua Cao goto out; 3249a1a564edSNenghua Cao 3250a1a564edSNenghua Cao ((u32 *)data)[0] &= mask; 3251a1a564edSNenghua Cao 3252907fe36aSLars-Peter Clausen ret = regmap_parse_val(component->regmap, 3253a1a564edSNenghua Cao &val, &val); 3254a1a564edSNenghua Cao if (ret != 0) 3255a1a564edSNenghua Cao goto out; 3256a1a564edSNenghua Cao 3257a1a564edSNenghua Cao ((u32 *)data)[0] |= val; 3258f831b055SMark Brown break; 3259f831b055SMark Brown default: 3260e8b18addSWei Yongjun ret = -EINVAL; 3261e8b18addSWei Yongjun goto out; 3262f831b055SMark Brown } 3263f831b055SMark Brown } 3264f831b055SMark Brown 3265907fe36aSLars-Peter Clausen ret = regmap_raw_write(component->regmap, params->base, 3266f831b055SMark Brown data, len); 3267f831b055SMark Brown 3268e8b18addSWei Yongjun out: 3269f831b055SMark Brown kfree(data); 327071d08516SMark Brown 327171d08516SMark Brown return ret; 327271d08516SMark Brown } 327371d08516SMark Brown EXPORT_SYMBOL_GPL(snd_soc_bytes_put); 327471d08516SMark Brown 3275d9881208SVinod Koul int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol, 3276d9881208SVinod Koul struct snd_ctl_elem_info *ucontrol) 3277d9881208SVinod Koul { 3278d9881208SVinod Koul struct soc_bytes_ext *params = (void *)kcontrol->private_value; 3279d9881208SVinod Koul 3280d9881208SVinod Koul ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES; 3281d9881208SVinod Koul ucontrol->count = params->max; 3282d9881208SVinod Koul 3283d9881208SVinod Koul return 0; 3284d9881208SVinod Koul } 3285d9881208SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext); 3286d9881208SVinod Koul 3287b6f4bb38Sapatard@mandriva.com /** 32884183eed2SKristoffer KARLSSON * snd_soc_info_xr_sx - signed multi register info callback 32894183eed2SKristoffer KARLSSON * @kcontrol: mreg control 32904183eed2SKristoffer KARLSSON * @uinfo: control element information 32914183eed2SKristoffer KARLSSON * 32924183eed2SKristoffer KARLSSON * Callback to provide information of a control that can 32934183eed2SKristoffer KARLSSON * span multiple codec registers which together 32944183eed2SKristoffer KARLSSON * forms a single signed value in a MSB/LSB manner. 32954183eed2SKristoffer KARLSSON * 32964183eed2SKristoffer KARLSSON * Returns 0 for success. 32974183eed2SKristoffer KARLSSON */ 32984183eed2SKristoffer KARLSSON int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol, 32994183eed2SKristoffer KARLSSON struct snd_ctl_elem_info *uinfo) 33004183eed2SKristoffer KARLSSON { 33014183eed2SKristoffer KARLSSON struct soc_mreg_control *mc = 33024183eed2SKristoffer KARLSSON (struct soc_mreg_control *)kcontrol->private_value; 33034183eed2SKristoffer KARLSSON uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 33044183eed2SKristoffer KARLSSON uinfo->count = 1; 33054183eed2SKristoffer KARLSSON uinfo->value.integer.min = mc->min; 33064183eed2SKristoffer KARLSSON uinfo->value.integer.max = mc->max; 33074183eed2SKristoffer KARLSSON 33084183eed2SKristoffer KARLSSON return 0; 33094183eed2SKristoffer KARLSSON } 33104183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx); 33114183eed2SKristoffer KARLSSON 33124183eed2SKristoffer KARLSSON /** 33134183eed2SKristoffer KARLSSON * snd_soc_get_xr_sx - signed multi register get callback 33144183eed2SKristoffer KARLSSON * @kcontrol: mreg control 33154183eed2SKristoffer KARLSSON * @ucontrol: control element information 33164183eed2SKristoffer KARLSSON * 33174183eed2SKristoffer KARLSSON * Callback to get the value of a control that can span 33184183eed2SKristoffer KARLSSON * multiple codec registers which together forms a single 33194183eed2SKristoffer KARLSSON * signed value in a MSB/LSB manner. The control supports 33204183eed2SKristoffer KARLSSON * specifying total no of bits used to allow for bitfields 33214183eed2SKristoffer KARLSSON * across the multiple codec registers. 33224183eed2SKristoffer KARLSSON * 33234183eed2SKristoffer KARLSSON * Returns 0 for success. 33244183eed2SKristoffer KARLSSON */ 33254183eed2SKristoffer KARLSSON int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol, 33264183eed2SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 33274183eed2SKristoffer KARLSSON { 3328907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 33294183eed2SKristoffer KARLSSON struct soc_mreg_control *mc = 33304183eed2SKristoffer KARLSSON (struct soc_mreg_control *)kcontrol->private_value; 33314183eed2SKristoffer KARLSSON unsigned int regbase = mc->regbase; 33324183eed2SKristoffer KARLSSON unsigned int regcount = mc->regcount; 3333907fe36aSLars-Peter Clausen unsigned int regwshift = component->val_bytes * BITS_PER_BYTE; 33344183eed2SKristoffer KARLSSON unsigned int regwmask = (1<<regwshift)-1; 33354183eed2SKristoffer KARLSSON unsigned int invert = mc->invert; 33364183eed2SKristoffer KARLSSON unsigned long mask = (1UL<<mc->nbits)-1; 33374183eed2SKristoffer KARLSSON long min = mc->min; 33384183eed2SKristoffer KARLSSON long max = mc->max; 33394183eed2SKristoffer KARLSSON long val = 0; 3340907fe36aSLars-Peter Clausen unsigned int regval; 33414183eed2SKristoffer KARLSSON unsigned int i; 3342907fe36aSLars-Peter Clausen int ret; 33434183eed2SKristoffer KARLSSON 33444183eed2SKristoffer KARLSSON for (i = 0; i < regcount; i++) { 3345907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, regbase+i, ®val); 3346907fe36aSLars-Peter Clausen if (ret) 3347907fe36aSLars-Peter Clausen return ret; 3348907fe36aSLars-Peter Clausen val |= (regval & regwmask) << (regwshift*(regcount-i-1)); 33494183eed2SKristoffer KARLSSON } 33504183eed2SKristoffer KARLSSON val &= mask; 33514183eed2SKristoffer KARLSSON if (min < 0 && val > max) 33524183eed2SKristoffer KARLSSON val |= ~mask; 33534183eed2SKristoffer KARLSSON if (invert) 33544183eed2SKristoffer KARLSSON val = max - val; 33554183eed2SKristoffer KARLSSON ucontrol->value.integer.value[0] = val; 33564183eed2SKristoffer KARLSSON 33574183eed2SKristoffer KARLSSON return 0; 33584183eed2SKristoffer KARLSSON } 33594183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx); 33604183eed2SKristoffer KARLSSON 33614183eed2SKristoffer KARLSSON /** 33624183eed2SKristoffer KARLSSON * snd_soc_put_xr_sx - signed multi register get callback 33634183eed2SKristoffer KARLSSON * @kcontrol: mreg control 33644183eed2SKristoffer KARLSSON * @ucontrol: control element information 33654183eed2SKristoffer KARLSSON * 33664183eed2SKristoffer KARLSSON * Callback to set the value of a control that can span 33674183eed2SKristoffer KARLSSON * multiple codec registers which together forms a single 33684183eed2SKristoffer KARLSSON * signed value in a MSB/LSB manner. The control supports 33694183eed2SKristoffer KARLSSON * specifying total no of bits used to allow for bitfields 33704183eed2SKristoffer KARLSSON * across the multiple codec registers. 33714183eed2SKristoffer KARLSSON * 33724183eed2SKristoffer KARLSSON * Returns 0 for success. 33734183eed2SKristoffer KARLSSON */ 33744183eed2SKristoffer KARLSSON int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol, 33754183eed2SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 33764183eed2SKristoffer KARLSSON { 3377907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 33784183eed2SKristoffer KARLSSON struct soc_mreg_control *mc = 33794183eed2SKristoffer KARLSSON (struct soc_mreg_control *)kcontrol->private_value; 33804183eed2SKristoffer KARLSSON unsigned int regbase = mc->regbase; 33814183eed2SKristoffer KARLSSON unsigned int regcount = mc->regcount; 3382907fe36aSLars-Peter Clausen unsigned int regwshift = component->val_bytes * BITS_PER_BYTE; 33834183eed2SKristoffer KARLSSON unsigned int regwmask = (1<<regwshift)-1; 33844183eed2SKristoffer KARLSSON unsigned int invert = mc->invert; 33854183eed2SKristoffer KARLSSON unsigned long mask = (1UL<<mc->nbits)-1; 33864183eed2SKristoffer KARLSSON long max = mc->max; 33874183eed2SKristoffer KARLSSON long val = ucontrol->value.integer.value[0]; 33884183eed2SKristoffer KARLSSON unsigned int i, regval, regmask; 33894183eed2SKristoffer KARLSSON int err; 33904183eed2SKristoffer KARLSSON 33914183eed2SKristoffer KARLSSON if (invert) 33924183eed2SKristoffer KARLSSON val = max - val; 33934183eed2SKristoffer KARLSSON val &= mask; 33944183eed2SKristoffer KARLSSON for (i = 0; i < regcount; i++) { 33954183eed2SKristoffer KARLSSON regval = (val >> (regwshift*(regcount-i-1))) & regwmask; 33964183eed2SKristoffer KARLSSON regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask; 3397907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, regbase+i, 33984183eed2SKristoffer KARLSSON regmask, regval); 33994183eed2SKristoffer KARLSSON if (err < 0) 34004183eed2SKristoffer KARLSSON return err; 34014183eed2SKristoffer KARLSSON } 34024183eed2SKristoffer KARLSSON 34034183eed2SKristoffer KARLSSON return 0; 34044183eed2SKristoffer KARLSSON } 34054183eed2SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx); 34064183eed2SKristoffer KARLSSON 34074183eed2SKristoffer KARLSSON /** 3408dd7b10b3SKristoffer KARLSSON * snd_soc_get_strobe - strobe get callback 3409dd7b10b3SKristoffer KARLSSON * @kcontrol: mixer control 3410dd7b10b3SKristoffer KARLSSON * @ucontrol: control element information 3411dd7b10b3SKristoffer KARLSSON * 3412dd7b10b3SKristoffer KARLSSON * Callback get the value of a strobe mixer control. 3413dd7b10b3SKristoffer KARLSSON * 3414dd7b10b3SKristoffer KARLSSON * Returns 0 for success. 3415dd7b10b3SKristoffer KARLSSON */ 3416dd7b10b3SKristoffer KARLSSON int snd_soc_get_strobe(struct snd_kcontrol *kcontrol, 3417dd7b10b3SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 3418dd7b10b3SKristoffer KARLSSON { 3419907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 3420dd7b10b3SKristoffer KARLSSON struct soc_mixer_control *mc = 3421dd7b10b3SKristoffer KARLSSON (struct soc_mixer_control *)kcontrol->private_value; 3422dd7b10b3SKristoffer KARLSSON unsigned int reg = mc->reg; 3423dd7b10b3SKristoffer KARLSSON unsigned int shift = mc->shift; 3424dd7b10b3SKristoffer KARLSSON unsigned int mask = 1 << shift; 3425dd7b10b3SKristoffer KARLSSON unsigned int invert = mc->invert != 0; 3426907fe36aSLars-Peter Clausen unsigned int val; 3427907fe36aSLars-Peter Clausen int ret; 3428907fe36aSLars-Peter Clausen 3429907fe36aSLars-Peter Clausen ret = snd_soc_component_read(component, reg, &val); 3430907fe36aSLars-Peter Clausen if (ret) 3431907fe36aSLars-Peter Clausen return ret; 3432907fe36aSLars-Peter Clausen 3433907fe36aSLars-Peter Clausen val &= mask; 3434dd7b10b3SKristoffer KARLSSON 3435dd7b10b3SKristoffer KARLSSON if (shift != 0 && val != 0) 3436dd7b10b3SKristoffer KARLSSON val = val >> shift; 3437dd7b10b3SKristoffer KARLSSON ucontrol->value.enumerated.item[0] = val ^ invert; 3438dd7b10b3SKristoffer KARLSSON 3439dd7b10b3SKristoffer KARLSSON return 0; 3440dd7b10b3SKristoffer KARLSSON } 3441dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_get_strobe); 3442dd7b10b3SKristoffer KARLSSON 3443dd7b10b3SKristoffer KARLSSON /** 3444dd7b10b3SKristoffer KARLSSON * snd_soc_put_strobe - strobe put callback 3445dd7b10b3SKristoffer KARLSSON * @kcontrol: mixer control 3446dd7b10b3SKristoffer KARLSSON * @ucontrol: control element information 3447dd7b10b3SKristoffer KARLSSON * 3448dd7b10b3SKristoffer KARLSSON * Callback strobe a register bit to high then low (or the inverse) 3449dd7b10b3SKristoffer KARLSSON * in one pass of a single mixer enum control. 3450dd7b10b3SKristoffer KARLSSON * 3451dd7b10b3SKristoffer KARLSSON * Returns 1 for success. 3452dd7b10b3SKristoffer KARLSSON */ 3453dd7b10b3SKristoffer KARLSSON int snd_soc_put_strobe(struct snd_kcontrol *kcontrol, 3454dd7b10b3SKristoffer KARLSSON struct snd_ctl_elem_value *ucontrol) 3455dd7b10b3SKristoffer KARLSSON { 3456907fe36aSLars-Peter Clausen struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 3457dd7b10b3SKristoffer KARLSSON struct soc_mixer_control *mc = 3458dd7b10b3SKristoffer KARLSSON (struct soc_mixer_control *)kcontrol->private_value; 3459dd7b10b3SKristoffer KARLSSON unsigned int reg = mc->reg; 3460dd7b10b3SKristoffer KARLSSON unsigned int shift = mc->shift; 3461dd7b10b3SKristoffer KARLSSON unsigned int mask = 1 << shift; 3462dd7b10b3SKristoffer KARLSSON unsigned int invert = mc->invert != 0; 3463dd7b10b3SKristoffer KARLSSON unsigned int strobe = ucontrol->value.enumerated.item[0] != 0; 3464dd7b10b3SKristoffer KARLSSON unsigned int val1 = (strobe ^ invert) ? mask : 0; 3465dd7b10b3SKristoffer KARLSSON unsigned int val2 = (strobe ^ invert) ? 0 : mask; 3466dd7b10b3SKristoffer KARLSSON int err; 3467dd7b10b3SKristoffer KARLSSON 3468907fe36aSLars-Peter Clausen err = snd_soc_component_update_bits(component, reg, mask, val1); 3469dd7b10b3SKristoffer KARLSSON if (err < 0) 3470dd7b10b3SKristoffer KARLSSON return err; 3471dd7b10b3SKristoffer KARLSSON 3472907fe36aSLars-Peter Clausen return snd_soc_component_update_bits(component, reg, mask, val2); 3473dd7b10b3SKristoffer KARLSSON } 3474dd7b10b3SKristoffer KARLSSON EXPORT_SYMBOL_GPL(snd_soc_put_strobe); 3475dd7b10b3SKristoffer KARLSSON 3476dd7b10b3SKristoffer KARLSSON /** 34778c6529dbSLiam Girdwood * snd_soc_dai_set_sysclk - configure DAI system or master clock. 34788c6529dbSLiam Girdwood * @dai: DAI 34798c6529dbSLiam Girdwood * @clk_id: DAI specific clock ID 34808c6529dbSLiam Girdwood * @freq: new clock frequency in Hz 34818c6529dbSLiam Girdwood * @dir: new clock direction - input/output. 34828c6529dbSLiam Girdwood * 34838c6529dbSLiam Girdwood * Configures the DAI master (MCLK) or system (SYSCLK) clocking. 34848c6529dbSLiam Girdwood */ 34858c6529dbSLiam Girdwood int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, 34868c6529dbSLiam Girdwood unsigned int freq, int dir) 34878c6529dbSLiam Girdwood { 3488f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_sysclk) 3489f0fba2adSLiam Girdwood return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); 3490ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_sysclk) 3491da1c6ea6SMark Brown return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0, 3492ec4ee52aSMark Brown freq, dir); 34938c6529dbSLiam Girdwood else 34941104a9c8SMark Brown return -ENOTSUPP; 34958c6529dbSLiam Girdwood } 34968c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); 34978c6529dbSLiam Girdwood 34988c6529dbSLiam Girdwood /** 3499ec4ee52aSMark Brown * snd_soc_codec_set_sysclk - configure CODEC system or master clock. 3500ec4ee52aSMark Brown * @codec: CODEC 3501ec4ee52aSMark Brown * @clk_id: DAI specific clock ID 3502da1c6ea6SMark Brown * @source: Source for the clock 3503ec4ee52aSMark Brown * @freq: new clock frequency in Hz 3504ec4ee52aSMark Brown * @dir: new clock direction - input/output. 3505ec4ee52aSMark Brown * 3506ec4ee52aSMark Brown * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. 3507ec4ee52aSMark Brown */ 3508ec4ee52aSMark Brown int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id, 3509da1c6ea6SMark Brown int source, unsigned int freq, int dir) 3510ec4ee52aSMark Brown { 3511ec4ee52aSMark Brown if (codec->driver->set_sysclk) 3512da1c6ea6SMark Brown return codec->driver->set_sysclk(codec, clk_id, source, 3513da1c6ea6SMark Brown freq, dir); 3514ec4ee52aSMark Brown else 35151104a9c8SMark Brown return -ENOTSUPP; 3516ec4ee52aSMark Brown } 3517ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk); 3518ec4ee52aSMark Brown 3519ec4ee52aSMark Brown /** 35208c6529dbSLiam Girdwood * snd_soc_dai_set_clkdiv - configure DAI clock dividers. 35218c6529dbSLiam Girdwood * @dai: DAI 3522ac11a2b3SMark Brown * @div_id: DAI specific clock divider ID 35238c6529dbSLiam Girdwood * @div: new clock divisor. 35248c6529dbSLiam Girdwood * 35258c6529dbSLiam Girdwood * Configures the clock dividers. This is used to derive the best DAI bit and 35268c6529dbSLiam Girdwood * frame clocks from the system or master clock. It's best to set the DAI bit 35278c6529dbSLiam Girdwood * and frame clocks as low as possible to save system power. 35288c6529dbSLiam Girdwood */ 35298c6529dbSLiam Girdwood int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, 35308c6529dbSLiam Girdwood int div_id, int div) 35318c6529dbSLiam Girdwood { 3532f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_clkdiv) 3533f0fba2adSLiam Girdwood return dai->driver->ops->set_clkdiv(dai, div_id, div); 35348c6529dbSLiam Girdwood else 35358c6529dbSLiam Girdwood return -EINVAL; 35368c6529dbSLiam Girdwood } 35378c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); 35388c6529dbSLiam Girdwood 35398c6529dbSLiam Girdwood /** 35408c6529dbSLiam Girdwood * snd_soc_dai_set_pll - configure DAI PLL. 35418c6529dbSLiam Girdwood * @dai: DAI 35428c6529dbSLiam Girdwood * @pll_id: DAI specific PLL ID 354385488037SMark Brown * @source: DAI specific source for the PLL 35448c6529dbSLiam Girdwood * @freq_in: PLL input clock frequency in Hz 35458c6529dbSLiam Girdwood * @freq_out: requested PLL output clock frequency in Hz 35468c6529dbSLiam Girdwood * 35478c6529dbSLiam Girdwood * Configures and enables PLL to generate output clock based on input clock. 35488c6529dbSLiam Girdwood */ 354985488037SMark Brown int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, 355085488037SMark Brown unsigned int freq_in, unsigned int freq_out) 35518c6529dbSLiam Girdwood { 3552f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_pll) 3553f0fba2adSLiam Girdwood return dai->driver->ops->set_pll(dai, pll_id, source, 355485488037SMark Brown freq_in, freq_out); 3555ec4ee52aSMark Brown else if (dai->codec && dai->codec->driver->set_pll) 3556ec4ee52aSMark Brown return dai->codec->driver->set_pll(dai->codec, pll_id, source, 3557ec4ee52aSMark Brown freq_in, freq_out); 35588c6529dbSLiam Girdwood else 35598c6529dbSLiam Girdwood return -EINVAL; 35608c6529dbSLiam Girdwood } 35618c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); 35628c6529dbSLiam Girdwood 3563ec4ee52aSMark Brown /* 3564ec4ee52aSMark Brown * snd_soc_codec_set_pll - configure codec PLL. 3565ec4ee52aSMark Brown * @codec: CODEC 3566ec4ee52aSMark Brown * @pll_id: DAI specific PLL ID 3567ec4ee52aSMark Brown * @source: DAI specific source for the PLL 3568ec4ee52aSMark Brown * @freq_in: PLL input clock frequency in Hz 3569ec4ee52aSMark Brown * @freq_out: requested PLL output clock frequency in Hz 3570ec4ee52aSMark Brown * 3571ec4ee52aSMark Brown * Configures and enables PLL to generate output clock based on input clock. 3572ec4ee52aSMark Brown */ 3573ec4ee52aSMark Brown int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source, 3574ec4ee52aSMark Brown unsigned int freq_in, unsigned int freq_out) 3575ec4ee52aSMark Brown { 3576ec4ee52aSMark Brown if (codec->driver->set_pll) 3577ec4ee52aSMark Brown return codec->driver->set_pll(codec, pll_id, source, 3578ec4ee52aSMark Brown freq_in, freq_out); 3579ec4ee52aSMark Brown else 3580ec4ee52aSMark Brown return -EINVAL; 3581ec4ee52aSMark Brown } 3582ec4ee52aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll); 3583ec4ee52aSMark Brown 35848c6529dbSLiam Girdwood /** 3585e54cf76bSLiam Girdwood * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio. 3586e54cf76bSLiam Girdwood * @dai: DAI 3587e54cf76bSLiam Girdwood * @ratio Ratio of BCLK to Sample rate. 3588e54cf76bSLiam Girdwood * 3589e54cf76bSLiam Girdwood * Configures the DAI for a preset BCLK to sample rate ratio. 3590e54cf76bSLiam Girdwood */ 3591e54cf76bSLiam Girdwood int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) 3592e54cf76bSLiam Girdwood { 3593e54cf76bSLiam Girdwood if (dai->driver && dai->driver->ops->set_bclk_ratio) 3594e54cf76bSLiam Girdwood return dai->driver->ops->set_bclk_ratio(dai, ratio); 3595e54cf76bSLiam Girdwood else 3596e54cf76bSLiam Girdwood return -EINVAL; 3597e54cf76bSLiam Girdwood } 3598e54cf76bSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio); 3599e54cf76bSLiam Girdwood 3600e54cf76bSLiam Girdwood /** 36018c6529dbSLiam Girdwood * snd_soc_dai_set_fmt - configure DAI hardware audio format. 36028c6529dbSLiam Girdwood * @dai: DAI 36038c6529dbSLiam Girdwood * @fmt: SND_SOC_DAIFMT_ format value. 36048c6529dbSLiam Girdwood * 36058c6529dbSLiam Girdwood * Configures the DAI hardware format and clocking. 36068c6529dbSLiam Girdwood */ 36078c6529dbSLiam Girdwood int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 36088c6529dbSLiam Girdwood { 36095e4ba569SShawn Guo if (dai->driver == NULL) 36108c6529dbSLiam Girdwood return -EINVAL; 36115e4ba569SShawn Guo if (dai->driver->ops->set_fmt == NULL) 36125e4ba569SShawn Guo return -ENOTSUPP; 36135e4ba569SShawn Guo return dai->driver->ops->set_fmt(dai, fmt); 36148c6529dbSLiam Girdwood } 36158c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); 36168c6529dbSLiam Girdwood 36178c6529dbSLiam Girdwood /** 3618e5c21514SXiubo Li * snd_soc_xlate_tdm_slot - generate tx/rx slot mask. 361989c67857SXiubo Li * @slots: Number of slots in use. 362089c67857SXiubo Li * @tx_mask: bitmask representing active TX slots. 362189c67857SXiubo Li * @rx_mask: bitmask representing active RX slots. 362289c67857SXiubo Li * 362389c67857SXiubo Li * Generates the TDM tx and rx slot default masks for DAI. 362489c67857SXiubo Li */ 3625e5c21514SXiubo Li static int snd_soc_xlate_tdm_slot_mask(unsigned int slots, 362689c67857SXiubo Li unsigned int *tx_mask, 362789c67857SXiubo Li unsigned int *rx_mask) 362889c67857SXiubo Li { 362989c67857SXiubo Li if (*tx_mask || *rx_mask) 363089c67857SXiubo Li return 0; 363189c67857SXiubo Li 363289c67857SXiubo Li if (!slots) 363389c67857SXiubo Li return -EINVAL; 363489c67857SXiubo Li 363589c67857SXiubo Li *tx_mask = (1 << slots) - 1; 363689c67857SXiubo Li *rx_mask = (1 << slots) - 1; 363789c67857SXiubo Li 363889c67857SXiubo Li return 0; 363989c67857SXiubo Li } 364089c67857SXiubo Li 364189c67857SXiubo Li /** 36428c6529dbSLiam Girdwood * snd_soc_dai_set_tdm_slot - configure DAI TDM. 36438c6529dbSLiam Girdwood * @dai: DAI 3644a5479e38SDaniel Ribeiro * @tx_mask: bitmask representing active TX slots. 3645a5479e38SDaniel Ribeiro * @rx_mask: bitmask representing active RX slots. 36468c6529dbSLiam Girdwood * @slots: Number of slots in use. 3647a5479e38SDaniel Ribeiro * @slot_width: Width in bits for each slot. 36488c6529dbSLiam Girdwood * 36498c6529dbSLiam Girdwood * Configures a DAI for TDM operation. Both mask and slots are codec and DAI 36508c6529dbSLiam Girdwood * specific. 36518c6529dbSLiam Girdwood */ 36528c6529dbSLiam Girdwood int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, 3653a5479e38SDaniel Ribeiro unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 36548c6529dbSLiam Girdwood { 3655e5c21514SXiubo Li if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask) 3656e5c21514SXiubo Li dai->driver->ops->xlate_tdm_slot_mask(slots, 365789c67857SXiubo Li &tx_mask, &rx_mask); 365889c67857SXiubo Li else 3659e5c21514SXiubo Li snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask); 366089c67857SXiubo Li 3661f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tdm_slot) 3662f0fba2adSLiam Girdwood return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, 3663a5479e38SDaniel Ribeiro slots, slot_width); 36648c6529dbSLiam Girdwood else 3665b2cbb6e1SXiubo Li return -ENOTSUPP; 36668c6529dbSLiam Girdwood } 36678c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); 36688c6529dbSLiam Girdwood 36698c6529dbSLiam Girdwood /** 3670472df3cbSBarry Song * snd_soc_dai_set_channel_map - configure DAI audio channel map 3671472df3cbSBarry Song * @dai: DAI 3672472df3cbSBarry Song * @tx_num: how many TX channels 3673472df3cbSBarry Song * @tx_slot: pointer to an array which imply the TX slot number channel 3674472df3cbSBarry Song * 0~num-1 uses 3675472df3cbSBarry Song * @rx_num: how many RX channels 3676472df3cbSBarry Song * @rx_slot: pointer to an array which imply the RX slot number channel 3677472df3cbSBarry Song * 0~num-1 uses 3678472df3cbSBarry Song * 3679472df3cbSBarry Song * configure the relationship between channel number and TDM slot number. 3680472df3cbSBarry Song */ 3681472df3cbSBarry Song int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, 3682472df3cbSBarry Song unsigned int tx_num, unsigned int *tx_slot, 3683472df3cbSBarry Song unsigned int rx_num, unsigned int *rx_slot) 3684472df3cbSBarry Song { 3685f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_channel_map) 3686f0fba2adSLiam Girdwood return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, 3687472df3cbSBarry Song rx_num, rx_slot); 3688472df3cbSBarry Song else 3689472df3cbSBarry Song return -EINVAL; 3690472df3cbSBarry Song } 3691472df3cbSBarry Song EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); 3692472df3cbSBarry Song 3693472df3cbSBarry Song /** 36948c6529dbSLiam Girdwood * snd_soc_dai_set_tristate - configure DAI system or master clock. 36958c6529dbSLiam Girdwood * @dai: DAI 36968c6529dbSLiam Girdwood * @tristate: tristate enable 36978c6529dbSLiam Girdwood * 36988c6529dbSLiam Girdwood * Tristates the DAI so that others can use it. 36998c6529dbSLiam Girdwood */ 37008c6529dbSLiam Girdwood int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) 37018c6529dbSLiam Girdwood { 3702f0fba2adSLiam Girdwood if (dai->driver && dai->driver->ops->set_tristate) 3703f0fba2adSLiam Girdwood return dai->driver->ops->set_tristate(dai, tristate); 37048c6529dbSLiam Girdwood else 37058c6529dbSLiam Girdwood return -EINVAL; 37068c6529dbSLiam Girdwood } 37078c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); 37088c6529dbSLiam Girdwood 37098c6529dbSLiam Girdwood /** 37108c6529dbSLiam Girdwood * snd_soc_dai_digital_mute - configure DAI system or master clock. 37118c6529dbSLiam Girdwood * @dai: DAI 37128c6529dbSLiam Girdwood * @mute: mute enable 3713da18396fSMark Brown * @direction: stream to mute 37148c6529dbSLiam Girdwood * 37158c6529dbSLiam Girdwood * Mutes the DAI DAC. 37168c6529dbSLiam Girdwood */ 3717da18396fSMark Brown int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute, 3718da18396fSMark Brown int direction) 37198c6529dbSLiam Girdwood { 3720da18396fSMark Brown if (!dai->driver) 3721da18396fSMark Brown return -ENOTSUPP; 3722da18396fSMark Brown 3723da18396fSMark Brown if (dai->driver->ops->mute_stream) 3724da18396fSMark Brown return dai->driver->ops->mute_stream(dai, mute, direction); 3725da18396fSMark Brown else if (direction == SNDRV_PCM_STREAM_PLAYBACK && 3726da18396fSMark Brown dai->driver->ops->digital_mute) 3727f0fba2adSLiam Girdwood return dai->driver->ops->digital_mute(dai, mute); 37288c6529dbSLiam Girdwood else 372904570c62SMark Brown return -ENOTSUPP; 37308c6529dbSLiam Girdwood } 37318c6529dbSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); 37328c6529dbSLiam Girdwood 3733c5af3a2eSMark Brown /** 3734c5af3a2eSMark Brown * snd_soc_register_card - Register a card with the ASoC core 3735c5af3a2eSMark Brown * 3736ac11a2b3SMark Brown * @card: Card to register 3737c5af3a2eSMark Brown * 3738c5af3a2eSMark Brown */ 373970a7ca34SVinod Koul int snd_soc_register_card(struct snd_soc_card *card) 3740c5af3a2eSMark Brown { 3741b19e6e7bSMark Brown int i, ret; 3742f0fba2adSLiam Girdwood 3743c5af3a2eSMark Brown if (!card->name || !card->dev) 3744c5af3a2eSMark Brown return -EINVAL; 3745c5af3a2eSMark Brown 37465a504963SStephen Warren for (i = 0; i < card->num_links; i++) { 37475a504963SStephen Warren struct snd_soc_dai_link *link = &card->dai_link[i]; 37485a504963SStephen Warren 37495a504963SStephen Warren /* 37505a504963SStephen Warren * Codec must be specified by 1 of name or OF node, 37515a504963SStephen Warren * not both or neither. 37525a504963SStephen Warren */ 37535a504963SStephen Warren if (!!link->codec_name == !!link->codec_of_node) { 375410e8aa9aSMichał Mirosław dev_err(card->dev, 375510e8aa9aSMichał Mirosław "ASoC: Neither/both codec name/of_node are set for %s\n", 375610e8aa9aSMichał Mirosław link->name); 37575a504963SStephen Warren return -EINVAL; 37585a504963SStephen Warren } 3759bc92657aSStephen Warren /* Codec DAI name must be specified */ 3760bc92657aSStephen Warren if (!link->codec_dai_name) { 376110e8aa9aSMichał Mirosław dev_err(card->dev, 376210e8aa9aSMichał Mirosław "ASoC: codec_dai_name not set for %s\n", 376310e8aa9aSMichał Mirosław link->name); 3764bc92657aSStephen Warren return -EINVAL; 3765bc92657aSStephen Warren } 37665a504963SStephen Warren 37675a504963SStephen Warren /* 37685a504963SStephen Warren * Platform may be specified by either name or OF node, but 37695a504963SStephen Warren * can be left unspecified, and a dummy platform will be used. 37705a504963SStephen Warren */ 37715a504963SStephen Warren if (link->platform_name && link->platform_of_node) { 377210e8aa9aSMichał Mirosław dev_err(card->dev, 377310e8aa9aSMichał Mirosław "ASoC: Both platform name/of_node are set for %s\n", 377410e8aa9aSMichał Mirosław link->name); 37755a504963SStephen Warren return -EINVAL; 37765a504963SStephen Warren } 37775a504963SStephen Warren 37785a504963SStephen Warren /* 3779bc92657aSStephen Warren * CPU device may be specified by either name or OF node, but 3780bc92657aSStephen Warren * can be left unspecified, and will be matched based on DAI 3781bc92657aSStephen Warren * name alone.. 37825a504963SStephen Warren */ 3783bc92657aSStephen Warren if (link->cpu_name && link->cpu_of_node) { 378410e8aa9aSMichał Mirosław dev_err(card->dev, 378510e8aa9aSMichał Mirosław "ASoC: Neither/both cpu name/of_node are set for %s\n", 378610e8aa9aSMichał Mirosław link->name); 3787bc92657aSStephen Warren return -EINVAL; 3788bc92657aSStephen Warren } 3789bc92657aSStephen Warren /* 3790bc92657aSStephen Warren * At least one of CPU DAI name or CPU device name/node must be 3791bc92657aSStephen Warren * specified 3792bc92657aSStephen Warren */ 3793bc92657aSStephen Warren if (!link->cpu_dai_name && 3794bc92657aSStephen Warren !(link->cpu_name || link->cpu_of_node)) { 379510e8aa9aSMichał Mirosław dev_err(card->dev, 379610e8aa9aSMichał Mirosław "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", 379710e8aa9aSMichał Mirosław link->name); 37985a504963SStephen Warren return -EINVAL; 37995a504963SStephen Warren } 38005a504963SStephen Warren } 38015a504963SStephen Warren 3802ed77cc12SMark Brown dev_set_drvdata(card->dev, card); 3803ed77cc12SMark Brown 3804111c6419SStephen Warren snd_soc_initialize_card_lists(card); 3805111c6419SStephen Warren 3806150dd2f8SVinod Koul soc_init_card_debugfs(card); 3807150dd2f8SVinod Koul 3808181a6892SMark Brown card->rtd = devm_kzalloc(card->dev, 3809181a6892SMark Brown sizeof(struct snd_soc_pcm_runtime) * 38102eea392dSJarkko Nikula (card->num_links + card->num_aux_devs), 3811f0fba2adSLiam Girdwood GFP_KERNEL); 3812f0fba2adSLiam Girdwood if (card->rtd == NULL) 3813f0fba2adSLiam Girdwood return -ENOMEM; 3814a7dbb603SLiam Girdwood card->num_rtd = 0; 38152eea392dSJarkko Nikula card->rtd_aux = &card->rtd[card->num_links]; 3816f0fba2adSLiam Girdwood 3817f0fba2adSLiam Girdwood for (i = 0; i < card->num_links; i++) 3818f0fba2adSLiam Girdwood card->rtd[i].dai_link = &card->dai_link[i]; 3819f0fba2adSLiam Girdwood 3820db432b41SMark Brown INIT_LIST_HEAD(&card->dapm_dirty); 3821c5af3a2eSMark Brown card->instantiated = 0; 3822f0fba2adSLiam Girdwood mutex_init(&card->mutex); 3823a73fb2dfSLiam Girdwood mutex_init(&card->dapm_mutex); 3824c5af3a2eSMark Brown 3825b19e6e7bSMark Brown ret = snd_soc_instantiate_card(card); 3826b19e6e7bSMark Brown if (ret != 0) 3827b19e6e7bSMark Brown soc_cleanup_card_debugfs(card); 3828c5af3a2eSMark Brown 3829988e8cc4SNicolin Chen /* deactivate pins to sleep state */ 3830988e8cc4SNicolin Chen for (i = 0; i < card->num_rtd; i++) { 3831988e8cc4SNicolin Chen struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; 3832988e8cc4SNicolin Chen struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai; 3833988e8cc4SNicolin Chen if (!codec_dai->active) 3834988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(codec_dai->dev); 3835988e8cc4SNicolin Chen if (!cpu_dai->active) 3836988e8cc4SNicolin Chen pinctrl_pm_select_sleep_state(cpu_dai->dev); 3837988e8cc4SNicolin Chen } 3838988e8cc4SNicolin Chen 3839b19e6e7bSMark Brown return ret; 3840c5af3a2eSMark Brown } 384170a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_register_card); 3842c5af3a2eSMark Brown 3843c5af3a2eSMark Brown /** 3844c5af3a2eSMark Brown * snd_soc_unregister_card - Unregister a card with the ASoC core 3845c5af3a2eSMark Brown * 3846ac11a2b3SMark Brown * @card: Card to unregister 3847c5af3a2eSMark Brown * 3848c5af3a2eSMark Brown */ 384970a7ca34SVinod Koul int snd_soc_unregister_card(struct snd_soc_card *card) 3850c5af3a2eSMark Brown { 3851b0e26485SVinod Koul if (card->instantiated) 3852b0e26485SVinod Koul soc_cleanup_card_resources(card); 3853f110bfc7SLiam Girdwood dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); 3854c5af3a2eSMark Brown 3855c5af3a2eSMark Brown return 0; 3856c5af3a2eSMark Brown } 385770a7ca34SVinod Koul EXPORT_SYMBOL_GPL(snd_soc_unregister_card); 3858c5af3a2eSMark Brown 3859f0fba2adSLiam Girdwood /* 3860f0fba2adSLiam Girdwood * Simplify DAI link configuration by removing ".-1" from device names 3861f0fba2adSLiam Girdwood * and sanitizing names. 3862f0fba2adSLiam Girdwood */ 38630b9a214aSDimitris Papastamos static char *fmt_single_name(struct device *dev, int *id) 3864f0fba2adSLiam Girdwood { 3865f0fba2adSLiam Girdwood char *found, name[NAME_SIZE]; 3866f0fba2adSLiam Girdwood int id1, id2; 3867f0fba2adSLiam Girdwood 3868f0fba2adSLiam Girdwood if (dev_name(dev) == NULL) 3869f0fba2adSLiam Girdwood return NULL; 3870f0fba2adSLiam Girdwood 387158818a77SDimitris Papastamos strlcpy(name, dev_name(dev), NAME_SIZE); 3872f0fba2adSLiam Girdwood 3873f0fba2adSLiam Girdwood /* are we a "%s.%d" name (platform and SPI components) */ 3874f0fba2adSLiam Girdwood found = strstr(name, dev->driver->name); 3875f0fba2adSLiam Girdwood if (found) { 3876f0fba2adSLiam Girdwood /* get ID */ 3877f0fba2adSLiam Girdwood if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { 3878f0fba2adSLiam Girdwood 3879f0fba2adSLiam Girdwood /* discard ID from name if ID == -1 */ 3880f0fba2adSLiam Girdwood if (*id == -1) 3881f0fba2adSLiam Girdwood found[strlen(dev->driver->name)] = '\0'; 3882f0fba2adSLiam Girdwood } 3883f0fba2adSLiam Girdwood 3884f0fba2adSLiam Girdwood } else { 3885f0fba2adSLiam Girdwood /* I2C component devices are named "bus-addr" */ 3886f0fba2adSLiam Girdwood if (sscanf(name, "%x-%x", &id1, &id2) == 2) { 3887f0fba2adSLiam Girdwood char tmp[NAME_SIZE]; 3888f0fba2adSLiam Girdwood 3889f0fba2adSLiam Girdwood /* create unique ID number from I2C addr and bus */ 389005899446SJarkko Nikula *id = ((id1 & 0xffff) << 16) + id2; 3891f0fba2adSLiam Girdwood 3892f0fba2adSLiam Girdwood /* sanitize component name for DAI link creation */ 3893f0fba2adSLiam Girdwood snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); 389458818a77SDimitris Papastamos strlcpy(name, tmp, NAME_SIZE); 3895f0fba2adSLiam Girdwood } else 3896f0fba2adSLiam Girdwood *id = 0; 3897f0fba2adSLiam Girdwood } 3898f0fba2adSLiam Girdwood 3899f0fba2adSLiam Girdwood return kstrdup(name, GFP_KERNEL); 3900f0fba2adSLiam Girdwood } 3901f0fba2adSLiam Girdwood 3902f0fba2adSLiam Girdwood /* 3903f0fba2adSLiam Girdwood * Simplify DAI link naming for single devices with multiple DAIs by removing 3904f0fba2adSLiam Girdwood * any ".-1" and using the DAI name (instead of device name). 3905f0fba2adSLiam Girdwood */ 3906f0fba2adSLiam Girdwood static inline char *fmt_multiple_name(struct device *dev, 3907f0fba2adSLiam Girdwood struct snd_soc_dai_driver *dai_drv) 3908f0fba2adSLiam Girdwood { 3909f0fba2adSLiam Girdwood if (dai_drv->name == NULL) { 391010e8aa9aSMichał Mirosław dev_err(dev, 391110e8aa9aSMichał Mirosław "ASoC: error - multiple DAI %s registered with no name\n", 391210e8aa9aSMichał Mirosław dev_name(dev)); 3913f0fba2adSLiam Girdwood return NULL; 3914f0fba2adSLiam Girdwood } 3915f0fba2adSLiam Girdwood 3916f0fba2adSLiam Girdwood return kstrdup(dai_drv->name, GFP_KERNEL); 3917f0fba2adSLiam Girdwood } 3918f0fba2adSLiam Girdwood 39199115171aSMark Brown /** 392032c9ba54SLars-Peter Clausen * snd_soc_unregister_dai - Unregister DAIs from the ASoC core 39219115171aSMark Brown * 392232c9ba54SLars-Peter Clausen * @component: The component for which the DAIs should be unregistered 39239115171aSMark Brown */ 392432c9ba54SLars-Peter Clausen static void snd_soc_unregister_dais(struct snd_soc_component *component) 39259115171aSMark Brown { 39265c1d5f09SLars-Peter Clausen struct snd_soc_dai *dai, *_dai; 39279115171aSMark Brown 39285c1d5f09SLars-Peter Clausen list_for_each_entry_safe(dai, _dai, &component->dai_list, list) { 392932c9ba54SLars-Peter Clausen dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n", 393032c9ba54SLars-Peter Clausen dai->name); 39319115171aSMark Brown list_del(&dai->list); 3932f0fba2adSLiam Girdwood kfree(dai->name); 3933f0fba2adSLiam Girdwood kfree(dai); 39349115171aSMark Brown } 393532c9ba54SLars-Peter Clausen } 39369115171aSMark Brown 39379115171aSMark Brown /** 393832c9ba54SLars-Peter Clausen * snd_soc_register_dais - Register a DAI with the ASoC core 39399115171aSMark Brown * 39406106d129SLars-Peter Clausen * @component: The component the DAIs are registered for 39416106d129SLars-Peter Clausen * @dai_drv: DAI driver to use for the DAIs 3942ac11a2b3SMark Brown * @count: Number of DAIs 394332c9ba54SLars-Peter Clausen * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the 394432c9ba54SLars-Peter Clausen * parent's name. 39459115171aSMark Brown */ 394632c9ba54SLars-Peter Clausen static int snd_soc_register_dais(struct snd_soc_component *component, 3947bb13109dSLars-Peter Clausen struct snd_soc_dai_driver *dai_drv, size_t count, 3948bb13109dSLars-Peter Clausen bool legacy_dai_naming) 39499115171aSMark Brown { 39506106d129SLars-Peter Clausen struct device *dev = component->dev; 3951f0fba2adSLiam Girdwood struct snd_soc_dai *dai; 395232c9ba54SLars-Peter Clausen unsigned int i; 395332c9ba54SLars-Peter Clausen int ret; 3954f0fba2adSLiam Girdwood 3955f110bfc7SLiam Girdwood dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count); 39569115171aSMark Brown 3957bb13109dSLars-Peter Clausen component->dai_drv = dai_drv; 3958bb13109dSLars-Peter Clausen component->num_dai = count; 3959bb13109dSLars-Peter Clausen 39609115171aSMark Brown for (i = 0; i < count; i++) { 3961f0fba2adSLiam Girdwood 3962f0fba2adSLiam Girdwood dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); 3963c46e0079SAxel Lin if (dai == NULL) { 3964c46e0079SAxel Lin ret = -ENOMEM; 3965c46e0079SAxel Lin goto err; 3966c46e0079SAxel Lin } 3967f0fba2adSLiam Girdwood 396832c9ba54SLars-Peter Clausen /* 396932c9ba54SLars-Peter Clausen * Back in the old days when we still had component-less DAIs, 397032c9ba54SLars-Peter Clausen * instead of having a static name, component-less DAIs would 397132c9ba54SLars-Peter Clausen * inherit the name of the parent device so it is possible to 397232c9ba54SLars-Peter Clausen * register multiple instances of the DAI. We still need to keep 397332c9ba54SLars-Peter Clausen * the same naming style even though those DAIs are not 397432c9ba54SLars-Peter Clausen * component-less anymore. 397532c9ba54SLars-Peter Clausen */ 397632c9ba54SLars-Peter Clausen if (count == 1 && legacy_dai_naming) { 3977db2a4165SFrank Mandarino dai->name = fmt_single_name(dev, &dai->id); 397832c9ba54SLars-Peter Clausen } else { 3979f0fba2adSLiam Girdwood dai->name = fmt_multiple_name(dev, &dai_drv[i]); 398032c9ba54SLars-Peter Clausen if (dai_drv[i].id) 398132c9ba54SLars-Peter Clausen dai->id = dai_drv[i].id; 398232c9ba54SLars-Peter Clausen else 398332c9ba54SLars-Peter Clausen dai->id = i; 398432c9ba54SLars-Peter Clausen } 3985f0fba2adSLiam Girdwood if (dai->name == NULL) { 3986f0fba2adSLiam Girdwood kfree(dai); 398732c9ba54SLars-Peter Clausen ret = -ENOMEM; 39889115171aSMark Brown goto err; 39899115171aSMark Brown } 39909115171aSMark Brown 39916106d129SLars-Peter Clausen dai->component = component; 3992f0fba2adSLiam Girdwood dai->dev = dev; 3993f0fba2adSLiam Girdwood dai->driver = &dai_drv[i]; 3994f0fba2adSLiam Girdwood if (!dai->driver->ops) 3995f0fba2adSLiam Girdwood dai->driver->ops = &null_dai_ops; 3996f0fba2adSLiam Girdwood 39971438c2f6SLars-Peter Clausen list_add(&dai->list, &component->dai_list); 3998054880feSMark Brown 39999115171aSMark Brown dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); 4000f0fba2adSLiam Girdwood } 4001f0fba2adSLiam Girdwood 40029115171aSMark Brown return 0; 40039115171aSMark Brown 40049115171aSMark Brown err: 400532c9ba54SLars-Peter Clausen snd_soc_unregister_dais(component); 40069115171aSMark Brown 40079115171aSMark Brown return ret; 40089115171aSMark Brown } 40099115171aSMark Brown 401014e8bdebSLars-Peter Clausen static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm, 401114e8bdebSLars-Peter Clausen enum snd_soc_dapm_type type, int subseq) 401214e8bdebSLars-Peter Clausen { 401314e8bdebSLars-Peter Clausen struct snd_soc_component *component = dapm->component; 401414e8bdebSLars-Peter Clausen 401514e8bdebSLars-Peter Clausen component->driver->seq_notifier(component, type, subseq); 401614e8bdebSLars-Peter Clausen } 401714e8bdebSLars-Peter Clausen 401814e8bdebSLars-Peter Clausen static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm, 401914e8bdebSLars-Peter Clausen int event) 402014e8bdebSLars-Peter Clausen { 402114e8bdebSLars-Peter Clausen struct snd_soc_component *component = dapm->component; 402214e8bdebSLars-Peter Clausen 402314e8bdebSLars-Peter Clausen return component->driver->stream_event(component, event); 402414e8bdebSLars-Peter Clausen } 402514e8bdebSLars-Peter Clausen 4026bb13109dSLars-Peter Clausen static int snd_soc_component_initialize(struct snd_soc_component *component, 4027bb13109dSLars-Peter Clausen const struct snd_soc_component_driver *driver, struct device *dev) 4028d191bd8dSKuninori Morimoto { 4029ce0fc93aSLars-Peter Clausen struct snd_soc_dapm_context *dapm; 4030ce0fc93aSLars-Peter Clausen 4031bb13109dSLars-Peter Clausen component->name = fmt_single_name(dev, &component->id); 4032bb13109dSLars-Peter Clausen if (!component->name) { 4033bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to allocate name\n"); 4034d191bd8dSKuninori Morimoto return -ENOMEM; 4035d191bd8dSKuninori Morimoto } 4036d191bd8dSKuninori Morimoto 4037bb13109dSLars-Peter Clausen component->dev = dev; 4038bb13109dSLars-Peter Clausen component->driver = driver; 4039e2c330b9SLars-Peter Clausen 4040ce0fc93aSLars-Peter Clausen if (!component->dapm_ptr) 4041ce0fc93aSLars-Peter Clausen component->dapm_ptr = &component->dapm; 4042ce0fc93aSLars-Peter Clausen 4043ce0fc93aSLars-Peter Clausen dapm = component->dapm_ptr; 4044ce0fc93aSLars-Peter Clausen dapm->dev = dev; 4045ce0fc93aSLars-Peter Clausen dapm->component = component; 4046ce0fc93aSLars-Peter Clausen dapm->bias_level = SND_SOC_BIAS_OFF; 404714e8bdebSLars-Peter Clausen if (driver->seq_notifier) 404814e8bdebSLars-Peter Clausen dapm->seq_notifier = snd_soc_component_seq_notifier; 404914e8bdebSLars-Peter Clausen if (driver->stream_event) 405014e8bdebSLars-Peter Clausen dapm->stream_event = snd_soc_component_stream_event; 4051ce0fc93aSLars-Peter Clausen 4052bb13109dSLars-Peter Clausen INIT_LIST_HEAD(&component->dai_list); 4053bb13109dSLars-Peter Clausen mutex_init(&component->io_mutex); 4054bb13109dSLars-Peter Clausen 4055bb13109dSLars-Peter Clausen return 0; 4056d191bd8dSKuninori Morimoto } 4057d191bd8dSKuninori Morimoto 4058bb13109dSLars-Peter Clausen static void snd_soc_component_add_unlocked(struct snd_soc_component *component) 4059bb13109dSLars-Peter Clausen { 4060bb13109dSLars-Peter Clausen list_add(&component->list, &component_list); 4061d191bd8dSKuninori Morimoto } 4062d191bd8dSKuninori Morimoto 4063bb13109dSLars-Peter Clausen static void snd_soc_component_add(struct snd_soc_component *component) 4064bb13109dSLars-Peter Clausen { 4065d191bd8dSKuninori Morimoto mutex_lock(&client_mutex); 4066bb13109dSLars-Peter Clausen snd_soc_component_add_unlocked(component); 4067d191bd8dSKuninori Morimoto mutex_unlock(&client_mutex); 4068bb13109dSLars-Peter Clausen } 4069d191bd8dSKuninori Morimoto 4070bb13109dSLars-Peter Clausen static void snd_soc_component_cleanup(struct snd_soc_component *component) 4071bb13109dSLars-Peter Clausen { 4072bb13109dSLars-Peter Clausen snd_soc_unregister_dais(component); 4073bb13109dSLars-Peter Clausen kfree(component->name); 4074bb13109dSLars-Peter Clausen } 4075d191bd8dSKuninori Morimoto 4076bb13109dSLars-Peter Clausen static void snd_soc_component_del_unlocked(struct snd_soc_component *component) 4077bb13109dSLars-Peter Clausen { 4078bb13109dSLars-Peter Clausen list_del(&component->list); 4079bb13109dSLars-Peter Clausen } 4080d191bd8dSKuninori Morimoto 4081bb13109dSLars-Peter Clausen static void snd_soc_component_del(struct snd_soc_component *component) 4082bb13109dSLars-Peter Clausen { 4083bb13109dSLars-Peter Clausen mutex_lock(&client_mutex); 4084bb13109dSLars-Peter Clausen snd_soc_component_del_unlocked(component); 4085bb13109dSLars-Peter Clausen mutex_unlock(&client_mutex); 4086d191bd8dSKuninori Morimoto } 4087d191bd8dSKuninori Morimoto 4088d191bd8dSKuninori Morimoto int snd_soc_register_component(struct device *dev, 4089d191bd8dSKuninori Morimoto const struct snd_soc_component_driver *cmpnt_drv, 4090d191bd8dSKuninori Morimoto struct snd_soc_dai_driver *dai_drv, 4091d191bd8dSKuninori Morimoto int num_dai) 4092d191bd8dSKuninori Morimoto { 4093d191bd8dSKuninori Morimoto struct snd_soc_component *cmpnt; 4094bb13109dSLars-Peter Clausen int ret; 4095d191bd8dSKuninori Morimoto 4096bb13109dSLars-Peter Clausen cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL); 4097d191bd8dSKuninori Morimoto if (!cmpnt) { 4098d191bd8dSKuninori Morimoto dev_err(dev, "ASoC: Failed to allocate memory\n"); 4099d191bd8dSKuninori Morimoto return -ENOMEM; 4100d191bd8dSKuninori Morimoto } 4101d191bd8dSKuninori Morimoto 4102bb13109dSLars-Peter Clausen ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev); 4103bb13109dSLars-Peter Clausen if (ret) 4104bb13109dSLars-Peter Clausen goto err_free; 4105bb13109dSLars-Peter Clausen 41063d59400fSLars-Peter Clausen cmpnt->ignore_pmdown_time = true; 410798e639fbSLars-Peter Clausen cmpnt->registered_as_component = true; 41083d59400fSLars-Peter Clausen 4109bb13109dSLars-Peter Clausen ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true); 4110bb13109dSLars-Peter Clausen if (ret < 0) { 4111bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret); 4112bb13109dSLars-Peter Clausen goto err_cleanup; 4113bb13109dSLars-Peter Clausen } 4114bb13109dSLars-Peter Clausen 4115bb13109dSLars-Peter Clausen snd_soc_component_add(cmpnt); 4116bb13109dSLars-Peter Clausen 4117bb13109dSLars-Peter Clausen return 0; 4118bb13109dSLars-Peter Clausen 4119bb13109dSLars-Peter Clausen err_cleanup: 4120bb13109dSLars-Peter Clausen snd_soc_component_cleanup(cmpnt); 4121bb13109dSLars-Peter Clausen err_free: 4122bb13109dSLars-Peter Clausen kfree(cmpnt); 4123bb13109dSLars-Peter Clausen return ret; 4124d191bd8dSKuninori Morimoto } 4125d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_register_component); 4126d191bd8dSKuninori Morimoto 4127d191bd8dSKuninori Morimoto /** 4128d191bd8dSKuninori Morimoto * snd_soc_unregister_component - Unregister a component from the ASoC core 4129d191bd8dSKuninori Morimoto * 4130d191bd8dSKuninori Morimoto */ 4131d191bd8dSKuninori Morimoto void snd_soc_unregister_component(struct device *dev) 4132d191bd8dSKuninori Morimoto { 4133d191bd8dSKuninori Morimoto struct snd_soc_component *cmpnt; 4134d191bd8dSKuninori Morimoto 4135d191bd8dSKuninori Morimoto list_for_each_entry(cmpnt, &component_list, list) { 413698e639fbSLars-Peter Clausen if (dev == cmpnt->dev && cmpnt->registered_as_component) 4137d191bd8dSKuninori Morimoto goto found; 4138d191bd8dSKuninori Morimoto } 4139d191bd8dSKuninori Morimoto return; 4140d191bd8dSKuninori Morimoto 4141d191bd8dSKuninori Morimoto found: 4142bb13109dSLars-Peter Clausen snd_soc_component_del(cmpnt); 4143bb13109dSLars-Peter Clausen snd_soc_component_cleanup(cmpnt); 4144bb13109dSLars-Peter Clausen kfree(cmpnt); 4145d191bd8dSKuninori Morimoto } 4146d191bd8dSKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_unregister_component); 4147d191bd8dSKuninori Morimoto 4148e2c330b9SLars-Peter Clausen static int snd_soc_platform_drv_write(struct snd_soc_component *component, 4149e2c330b9SLars-Peter Clausen unsigned int reg, unsigned int val) 4150e2c330b9SLars-Peter Clausen { 4151e2c330b9SLars-Peter Clausen struct snd_soc_platform *platform = snd_soc_component_to_platform(component); 4152e2c330b9SLars-Peter Clausen 4153e2c330b9SLars-Peter Clausen return platform->driver->write(platform, reg, val); 4154e2c330b9SLars-Peter Clausen } 4155e2c330b9SLars-Peter Clausen 4156e2c330b9SLars-Peter Clausen static int snd_soc_platform_drv_read(struct snd_soc_component *component, 4157e2c330b9SLars-Peter Clausen unsigned int reg, unsigned int *val) 4158e2c330b9SLars-Peter Clausen { 4159e2c330b9SLars-Peter Clausen struct snd_soc_platform *platform = snd_soc_component_to_platform(component); 4160e2c330b9SLars-Peter Clausen 4161e2c330b9SLars-Peter Clausen *val = platform->driver->read(platform, reg); 4162e2c330b9SLars-Peter Clausen 4163e2c330b9SLars-Peter Clausen return 0; 4164e2c330b9SLars-Peter Clausen } 4165e2c330b9SLars-Peter Clausen 4166d191bd8dSKuninori Morimoto /** 416771a45cdaSLars-Peter Clausen * snd_soc_add_platform - Add a platform to the ASoC core 416871a45cdaSLars-Peter Clausen * @dev: The parent device for the platform 416971a45cdaSLars-Peter Clausen * @platform: The platform to add 417071a45cdaSLars-Peter Clausen * @platform_driver: The driver for the platform 417112a48a8cSMark Brown */ 417271a45cdaSLars-Peter Clausen int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform, 4173d79e57dbSLars-Peter Clausen const struct snd_soc_platform_driver *platform_drv) 417412a48a8cSMark Brown { 4175b37f1d12SLars-Peter Clausen int ret; 4176b37f1d12SLars-Peter Clausen 4177bb13109dSLars-Peter Clausen ret = snd_soc_component_initialize(&platform->component, 4178bb13109dSLars-Peter Clausen &platform_drv->component_driver, dev); 4179bb13109dSLars-Peter Clausen if (ret) 4180bb13109dSLars-Peter Clausen return ret; 4181bb13109dSLars-Peter Clausen 4182f0fba2adSLiam Girdwood platform->dev = dev; 4183f0fba2adSLiam Girdwood platform->driver = platform_drv; 4184e2c330b9SLars-Peter Clausen if (platform_drv->write) 4185e2c330b9SLars-Peter Clausen platform->component.write = snd_soc_platform_drv_write; 4186e2c330b9SLars-Peter Clausen if (platform_drv->read) 4187e2c330b9SLars-Peter Clausen platform->component.read = snd_soc_platform_drv_read; 418812a48a8cSMark Brown 418912a48a8cSMark Brown mutex_lock(&client_mutex); 4190bb13109dSLars-Peter Clausen snd_soc_component_add_unlocked(&platform->component); 419112a48a8cSMark Brown list_add(&platform->list, &platform_list); 419212a48a8cSMark Brown mutex_unlock(&client_mutex); 419312a48a8cSMark Brown 4194f4333203SLars-Peter Clausen dev_dbg(dev, "ASoC: Registered platform '%s'\n", 4195f4333203SLars-Peter Clausen platform->component.name); 419612a48a8cSMark Brown 419712a48a8cSMark Brown return 0; 419812a48a8cSMark Brown } 419971a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_add_platform); 420071a45cdaSLars-Peter Clausen 420171a45cdaSLars-Peter Clausen /** 420271a45cdaSLars-Peter Clausen * snd_soc_register_platform - Register a platform with the ASoC core 420371a45cdaSLars-Peter Clausen * 420471a45cdaSLars-Peter Clausen * @platform: platform to register 420571a45cdaSLars-Peter Clausen */ 420671a45cdaSLars-Peter Clausen int snd_soc_register_platform(struct device *dev, 420771a45cdaSLars-Peter Clausen const struct snd_soc_platform_driver *platform_drv) 420871a45cdaSLars-Peter Clausen { 420971a45cdaSLars-Peter Clausen struct snd_soc_platform *platform; 421071a45cdaSLars-Peter Clausen int ret; 421171a45cdaSLars-Peter Clausen 421271a45cdaSLars-Peter Clausen dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev)); 421371a45cdaSLars-Peter Clausen 421471a45cdaSLars-Peter Clausen platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); 421571a45cdaSLars-Peter Clausen if (platform == NULL) 421671a45cdaSLars-Peter Clausen return -ENOMEM; 421771a45cdaSLars-Peter Clausen 421871a45cdaSLars-Peter Clausen ret = snd_soc_add_platform(dev, platform, platform_drv); 421971a45cdaSLars-Peter Clausen if (ret) 422071a45cdaSLars-Peter Clausen kfree(platform); 422171a45cdaSLars-Peter Clausen 422271a45cdaSLars-Peter Clausen return ret; 422371a45cdaSLars-Peter Clausen } 422412a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_platform); 422512a48a8cSMark Brown 422612a48a8cSMark Brown /** 422771a45cdaSLars-Peter Clausen * snd_soc_remove_platform - Remove a platform from the ASoC core 422871a45cdaSLars-Peter Clausen * @platform: the platform to remove 422971a45cdaSLars-Peter Clausen */ 423071a45cdaSLars-Peter Clausen void snd_soc_remove_platform(struct snd_soc_platform *platform) 423171a45cdaSLars-Peter Clausen { 4232b37f1d12SLars-Peter Clausen 423371a45cdaSLars-Peter Clausen mutex_lock(&client_mutex); 423471a45cdaSLars-Peter Clausen list_del(&platform->list); 4235bb13109dSLars-Peter Clausen snd_soc_component_del_unlocked(&platform->component); 423671a45cdaSLars-Peter Clausen mutex_unlock(&client_mutex); 423771a45cdaSLars-Peter Clausen 4238bb13109dSLars-Peter Clausen snd_soc_component_cleanup(&platform->component); 4239bb13109dSLars-Peter Clausen 424071a45cdaSLars-Peter Clausen dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n", 4241f4333203SLars-Peter Clausen platform->component.name); 424271a45cdaSLars-Peter Clausen } 424371a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_remove_platform); 424471a45cdaSLars-Peter Clausen 424571a45cdaSLars-Peter Clausen struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev) 424671a45cdaSLars-Peter Clausen { 424771a45cdaSLars-Peter Clausen struct snd_soc_platform *platform; 424871a45cdaSLars-Peter Clausen 424971a45cdaSLars-Peter Clausen list_for_each_entry(platform, &platform_list, list) { 425071a45cdaSLars-Peter Clausen if (dev == platform->dev) 425171a45cdaSLars-Peter Clausen return platform; 425271a45cdaSLars-Peter Clausen } 425371a45cdaSLars-Peter Clausen 425471a45cdaSLars-Peter Clausen return NULL; 425571a45cdaSLars-Peter Clausen } 425671a45cdaSLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_lookup_platform); 425771a45cdaSLars-Peter Clausen 425871a45cdaSLars-Peter Clausen /** 425912a48a8cSMark Brown * snd_soc_unregister_platform - Unregister a platform from the ASoC core 426012a48a8cSMark Brown * 4261ac11a2b3SMark Brown * @platform: platform to unregister 426212a48a8cSMark Brown */ 4263f0fba2adSLiam Girdwood void snd_soc_unregister_platform(struct device *dev) 426412a48a8cSMark Brown { 4265f0fba2adSLiam Girdwood struct snd_soc_platform *platform; 4266f0fba2adSLiam Girdwood 426771a45cdaSLars-Peter Clausen platform = snd_soc_lookup_platform(dev); 426871a45cdaSLars-Peter Clausen if (!platform) 4269f0fba2adSLiam Girdwood return; 4270f0fba2adSLiam Girdwood 427171a45cdaSLars-Peter Clausen snd_soc_remove_platform(platform); 4272f0fba2adSLiam Girdwood kfree(platform); 427312a48a8cSMark Brown } 427412a48a8cSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); 427512a48a8cSMark Brown 4276151ab22cSMark Brown static u64 codec_format_map[] = { 4277151ab22cSMark Brown SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, 4278151ab22cSMark Brown SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, 4279151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, 4280151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, 4281151ab22cSMark Brown SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, 4282151ab22cSMark Brown SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, 4283151ab22cSMark Brown SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 4284151ab22cSMark Brown SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, 4285151ab22cSMark Brown SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, 4286151ab22cSMark Brown SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, 4287151ab22cSMark Brown SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, 4288151ab22cSMark Brown SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, 4289151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, 4290151ab22cSMark Brown SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, 4291151ab22cSMark Brown SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE 4292151ab22cSMark Brown | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, 4293151ab22cSMark Brown }; 4294151ab22cSMark Brown 4295151ab22cSMark Brown /* Fix up the DAI formats for endianness: codecs don't actually see 4296151ab22cSMark Brown * the endianness of the data but we're using the CPU format 4297151ab22cSMark Brown * definitions which do need to include endianness so we ensure that 4298151ab22cSMark Brown * codec DAIs always have both big and little endian variants set. 4299151ab22cSMark Brown */ 4300151ab22cSMark Brown static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) 4301151ab22cSMark Brown { 4302151ab22cSMark Brown int i; 4303151ab22cSMark Brown 4304151ab22cSMark Brown for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) 4305151ab22cSMark Brown if (stream->formats & codec_format_map[i]) 4306151ab22cSMark Brown stream->formats |= codec_format_map[i]; 4307151ab22cSMark Brown } 4308151ab22cSMark Brown 4309e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_write(struct snd_soc_component *component, 4310e2c330b9SLars-Peter Clausen unsigned int reg, unsigned int val) 4311e2c330b9SLars-Peter Clausen { 4312e2c330b9SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 4313e2c330b9SLars-Peter Clausen 4314e2c330b9SLars-Peter Clausen return codec->driver->write(codec, reg, val); 4315e2c330b9SLars-Peter Clausen } 4316e2c330b9SLars-Peter Clausen 4317e2c330b9SLars-Peter Clausen static int snd_soc_codec_drv_read(struct snd_soc_component *component, 4318e2c330b9SLars-Peter Clausen unsigned int reg, unsigned int *val) 4319e2c330b9SLars-Peter Clausen { 4320e2c330b9SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 4321e2c330b9SLars-Peter Clausen 4322e2c330b9SLars-Peter Clausen *val = codec->driver->read(codec, reg); 4323e2c330b9SLars-Peter Clausen 4324e2c330b9SLars-Peter Clausen return 0; 4325e2c330b9SLars-Peter Clausen } 4326e2c330b9SLars-Peter Clausen 432768f831c2SLars-Peter Clausen static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm, 432868f831c2SLars-Peter Clausen enum snd_soc_bias_level level) 432968f831c2SLars-Peter Clausen { 433068f831c2SLars-Peter Clausen struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm); 433168f831c2SLars-Peter Clausen 433268f831c2SLars-Peter Clausen return codec->driver->set_bias_level(codec, level); 433368f831c2SLars-Peter Clausen } 433468f831c2SLars-Peter Clausen 43350d0cf00aSMark Brown /** 43360d0cf00aSMark Brown * snd_soc_register_codec - Register a codec with the ASoC core 43370d0cf00aSMark Brown * 4338ac11a2b3SMark Brown * @codec: codec to register 43390d0cf00aSMark Brown */ 4340f0fba2adSLiam Girdwood int snd_soc_register_codec(struct device *dev, 4341001ae4c0SMark Brown const struct snd_soc_codec_driver *codec_drv, 4342001ae4c0SMark Brown struct snd_soc_dai_driver *dai_drv, 4343001ae4c0SMark Brown int num_dai) 43440d0cf00aSMark Brown { 4345f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 4346bb13109dSLars-Peter Clausen struct snd_soc_dai *dai; 4347a39f75f7SXiubo Li struct regmap *regmap; 4348f0fba2adSLiam Girdwood int ret, i; 4349151ab22cSMark Brown 4350f0fba2adSLiam Girdwood dev_dbg(dev, "codec register %s\n", dev_name(dev)); 43510d0cf00aSMark Brown 4352f0fba2adSLiam Girdwood codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 4353f0fba2adSLiam Girdwood if (codec == NULL) 4354f0fba2adSLiam Girdwood return -ENOMEM; 43550d0cf00aSMark Brown 4356ce0fc93aSLars-Peter Clausen codec->component.dapm_ptr = &codec->dapm; 4357ce0fc93aSLars-Peter Clausen 4358bb13109dSLars-Peter Clausen ret = snd_soc_component_initialize(&codec->component, 4359bb13109dSLars-Peter Clausen &codec_drv->component_driver, dev); 4360bb13109dSLars-Peter Clausen if (ret) 4361bb13109dSLars-Peter Clausen goto err_free; 4362bb13109dSLars-Peter Clausen 4363e2c330b9SLars-Peter Clausen if (codec_drv->write) 4364e2c330b9SLars-Peter Clausen codec->component.write = snd_soc_codec_drv_write; 4365e2c330b9SLars-Peter Clausen if (codec_drv->read) 4366e2c330b9SLars-Peter Clausen codec->component.read = snd_soc_codec_drv_read; 43673d59400fSLars-Peter Clausen codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time; 4368ce6120ccSLiam Girdwood codec->dapm.codec = codec; 436914e8bdebSLars-Peter Clausen if (codec_drv->seq_notifier) 4370474b62d6SMark Brown codec->dapm.seq_notifier = codec_drv->seq_notifier; 437168f831c2SLars-Peter Clausen if (codec_drv->set_bias_level) 437268f831c2SLars-Peter Clausen codec->dapm.set_bias_level = snd_soc_codec_set_bias_level; 4373f0fba2adSLiam Girdwood codec->dev = dev; 4374f0fba2adSLiam Girdwood codec->driver = codec_drv; 4375e2c330b9SLars-Peter Clausen codec->component.val_bytes = codec_drv->reg_word_size; 4376f0fba2adSLiam Girdwood mutex_init(&codec->mutex); 4377f0fba2adSLiam Girdwood 4378e2c330b9SLars-Peter Clausen if (!codec->component.write) { 4379a39f75f7SXiubo Li if (codec_drv->get_regmap) 4380a39f75f7SXiubo Li regmap = codec_drv->get_regmap(dev); 4381a39f75f7SXiubo Li else 4382a39f75f7SXiubo Li regmap = dev_get_regmap(dev, NULL); 4383a39f75f7SXiubo Li 4384a39f75f7SXiubo Li if (regmap) { 4385e2c330b9SLars-Peter Clausen ret = snd_soc_component_init_io(&codec->component, 4386e2c330b9SLars-Peter Clausen regmap); 4387e2c330b9SLars-Peter Clausen if (ret) { 4388a39f75f7SXiubo Li dev_err(codec->dev, 4389a39f75f7SXiubo Li "Failed to set cache I/O:%d\n", 4390a39f75f7SXiubo Li ret); 4391bb13109dSLars-Peter Clausen goto err_cleanup; 4392a39f75f7SXiubo Li } 4393a39f75f7SXiubo Li } 4394a39f75f7SXiubo Li } 4395a39f75f7SXiubo Li 4396f0fba2adSLiam Girdwood for (i = 0; i < num_dai; i++) { 4397f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].playback); 4398f0fba2adSLiam Girdwood fixup_codec_formats(&dai_drv[i].capture); 4399f0fba2adSLiam Girdwood } 4400f0fba2adSLiam Girdwood 4401bb13109dSLars-Peter Clausen ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false); 4402bb13109dSLars-Peter Clausen if (ret < 0) { 4403bb13109dSLars-Peter Clausen dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret); 4404bb13109dSLars-Peter Clausen goto err_cleanup; 4405bb13109dSLars-Peter Clausen } 4406bb13109dSLars-Peter Clausen 4407bb13109dSLars-Peter Clausen list_for_each_entry(dai, &codec->component.dai_list, list) 4408bb13109dSLars-Peter Clausen dai->codec = codec; 4409bb13109dSLars-Peter Clausen 4410054880feSMark Brown mutex_lock(&client_mutex); 4411bb13109dSLars-Peter Clausen snd_soc_component_add_unlocked(&codec->component); 4412054880feSMark Brown list_add(&codec->list, &codec_list); 4413054880feSMark Brown mutex_unlock(&client_mutex); 4414054880feSMark Brown 4415f4333203SLars-Peter Clausen dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", 4416f4333203SLars-Peter Clausen codec->component.name); 44170d0cf00aSMark Brown return 0; 4418f0fba2adSLiam Girdwood 4419bb13109dSLars-Peter Clausen err_cleanup: 4420bb13109dSLars-Peter Clausen snd_soc_component_cleanup(&codec->component); 4421bb13109dSLars-Peter Clausen err_free: 4422f0fba2adSLiam Girdwood kfree(codec); 4423f0fba2adSLiam Girdwood return ret; 44240d0cf00aSMark Brown } 44250d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_register_codec); 44260d0cf00aSMark Brown 44270d0cf00aSMark Brown /** 44280d0cf00aSMark Brown * snd_soc_unregister_codec - Unregister a codec from the ASoC core 44290d0cf00aSMark Brown * 4430ac11a2b3SMark Brown * @codec: codec to unregister 44310d0cf00aSMark Brown */ 4432f0fba2adSLiam Girdwood void snd_soc_unregister_codec(struct device *dev) 44330d0cf00aSMark Brown { 4434f0fba2adSLiam Girdwood struct snd_soc_codec *codec; 4435f0fba2adSLiam Girdwood 4436f0fba2adSLiam Girdwood list_for_each_entry(codec, &codec_list, list) { 4437f0fba2adSLiam Girdwood if (dev == codec->dev) 4438f0fba2adSLiam Girdwood goto found; 4439f0fba2adSLiam Girdwood } 4440f0fba2adSLiam Girdwood return; 4441f0fba2adSLiam Girdwood 4442f0fba2adSLiam Girdwood found: 4443f0fba2adSLiam Girdwood 44440d0cf00aSMark Brown mutex_lock(&client_mutex); 44450d0cf00aSMark Brown list_del(&codec->list); 4446bb13109dSLars-Peter Clausen snd_soc_component_del_unlocked(&codec->component); 44470d0cf00aSMark Brown mutex_unlock(&client_mutex); 44480d0cf00aSMark Brown 4449f4333203SLars-Peter Clausen dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", 4450f4333203SLars-Peter Clausen codec->component.name); 4451f0fba2adSLiam Girdwood 4452bb13109dSLars-Peter Clausen snd_soc_component_cleanup(&codec->component); 44537a30a3dbSDimitris Papastamos snd_soc_cache_exit(codec); 4454f0fba2adSLiam Girdwood kfree(codec); 44550d0cf00aSMark Brown } 44560d0cf00aSMark Brown EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); 44570d0cf00aSMark Brown 4458bec4fa05SStephen Warren /* Retrieve a card's name from device tree */ 4459bec4fa05SStephen Warren int snd_soc_of_parse_card_name(struct snd_soc_card *card, 4460bec4fa05SStephen Warren const char *propname) 4461bec4fa05SStephen Warren { 4462bec4fa05SStephen Warren struct device_node *np = card->dev->of_node; 4463bec4fa05SStephen Warren int ret; 4464bec4fa05SStephen Warren 4465bec4fa05SStephen Warren ret = of_property_read_string_index(np, propname, 0, &card->name); 4466bec4fa05SStephen Warren /* 4467bec4fa05SStephen Warren * EINVAL means the property does not exist. This is fine providing 4468bec4fa05SStephen Warren * card->name was previously set, which is checked later in 4469bec4fa05SStephen Warren * snd_soc_register_card. 4470bec4fa05SStephen Warren */ 4471bec4fa05SStephen Warren if (ret < 0 && ret != -EINVAL) { 4472bec4fa05SStephen Warren dev_err(card->dev, 4473f110bfc7SLiam Girdwood "ASoC: Property '%s' could not be read: %d\n", 4474bec4fa05SStephen Warren propname, ret); 4475bec4fa05SStephen Warren return ret; 4476bec4fa05SStephen Warren } 4477bec4fa05SStephen Warren 4478bec4fa05SStephen Warren return 0; 4479bec4fa05SStephen Warren } 4480bec4fa05SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); 4481bec4fa05SStephen Warren 44829a6d4860SXiubo Li static const struct snd_soc_dapm_widget simple_widgets[] = { 44839a6d4860SXiubo Li SND_SOC_DAPM_MIC("Microphone", NULL), 44849a6d4860SXiubo Li SND_SOC_DAPM_LINE("Line", NULL), 44859a6d4860SXiubo Li SND_SOC_DAPM_HP("Headphone", NULL), 44869a6d4860SXiubo Li SND_SOC_DAPM_SPK("Speaker", NULL), 44879a6d4860SXiubo Li }; 44889a6d4860SXiubo Li 44899a6d4860SXiubo Li int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card, 44909a6d4860SXiubo Li const char *propname) 44919a6d4860SXiubo Li { 44929a6d4860SXiubo Li struct device_node *np = card->dev->of_node; 44939a6d4860SXiubo Li struct snd_soc_dapm_widget *widgets; 44949a6d4860SXiubo Li const char *template, *wname; 44959a6d4860SXiubo Li int i, j, num_widgets, ret; 44969a6d4860SXiubo Li 44979a6d4860SXiubo Li num_widgets = of_property_count_strings(np, propname); 44989a6d4860SXiubo Li if (num_widgets < 0) { 44999a6d4860SXiubo Li dev_err(card->dev, 45009a6d4860SXiubo Li "ASoC: Property '%s' does not exist\n", propname); 45019a6d4860SXiubo Li return -EINVAL; 45029a6d4860SXiubo Li } 45039a6d4860SXiubo Li if (num_widgets & 1) { 45049a6d4860SXiubo Li dev_err(card->dev, 45059a6d4860SXiubo Li "ASoC: Property '%s' length is not even\n", propname); 45069a6d4860SXiubo Li return -EINVAL; 45079a6d4860SXiubo Li } 45089a6d4860SXiubo Li 45099a6d4860SXiubo Li num_widgets /= 2; 45109a6d4860SXiubo Li if (!num_widgets) { 45119a6d4860SXiubo Li dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 45129a6d4860SXiubo Li propname); 45139a6d4860SXiubo Li return -EINVAL; 45149a6d4860SXiubo Li } 45159a6d4860SXiubo Li 45169a6d4860SXiubo Li widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets), 45179a6d4860SXiubo Li GFP_KERNEL); 45189a6d4860SXiubo Li if (!widgets) { 45199a6d4860SXiubo Li dev_err(card->dev, 45209a6d4860SXiubo Li "ASoC: Could not allocate memory for widgets\n"); 45219a6d4860SXiubo Li return -ENOMEM; 45229a6d4860SXiubo Li } 45239a6d4860SXiubo Li 45249a6d4860SXiubo Li for (i = 0; i < num_widgets; i++) { 45259a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 45269a6d4860SXiubo Li 2 * i, &template); 45279a6d4860SXiubo Li if (ret) { 45289a6d4860SXiubo Li dev_err(card->dev, 45299a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 45309a6d4860SXiubo Li propname, 2 * i, ret); 45319a6d4860SXiubo Li return -EINVAL; 45329a6d4860SXiubo Li } 45339a6d4860SXiubo Li 45349a6d4860SXiubo Li for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) { 45359a6d4860SXiubo Li if (!strncmp(template, simple_widgets[j].name, 45369a6d4860SXiubo Li strlen(simple_widgets[j].name))) { 45379a6d4860SXiubo Li widgets[i] = simple_widgets[j]; 45389a6d4860SXiubo Li break; 45399a6d4860SXiubo Li } 45409a6d4860SXiubo Li } 45419a6d4860SXiubo Li 45429a6d4860SXiubo Li if (j >= ARRAY_SIZE(simple_widgets)) { 45439a6d4860SXiubo Li dev_err(card->dev, 45449a6d4860SXiubo Li "ASoC: DAPM widget '%s' is not supported\n", 45459a6d4860SXiubo Li template); 45469a6d4860SXiubo Li return -EINVAL; 45479a6d4860SXiubo Li } 45489a6d4860SXiubo Li 45499a6d4860SXiubo Li ret = of_property_read_string_index(np, propname, 45509a6d4860SXiubo Li (2 * i) + 1, 45519a6d4860SXiubo Li &wname); 45529a6d4860SXiubo Li if (ret) { 45539a6d4860SXiubo Li dev_err(card->dev, 45549a6d4860SXiubo Li "ASoC: Property '%s' index %d read error:%d\n", 45559a6d4860SXiubo Li propname, (2 * i) + 1, ret); 45569a6d4860SXiubo Li return -EINVAL; 45579a6d4860SXiubo Li } 45589a6d4860SXiubo Li 45599a6d4860SXiubo Li widgets[i].name = wname; 45609a6d4860SXiubo Li } 45619a6d4860SXiubo Li 45629a6d4860SXiubo Li card->dapm_widgets = widgets; 45639a6d4860SXiubo Li card->num_dapm_widgets = num_widgets; 45649a6d4860SXiubo Li 45659a6d4860SXiubo Li return 0; 45669a6d4860SXiubo Li } 45679a6d4860SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets); 45689a6d4860SXiubo Li 456989c67857SXiubo Li int snd_soc_of_parse_tdm_slot(struct device_node *np, 457089c67857SXiubo Li unsigned int *slots, 457189c67857SXiubo Li unsigned int *slot_width) 457289c67857SXiubo Li { 457389c67857SXiubo Li u32 val; 457489c67857SXiubo Li int ret; 457589c67857SXiubo Li 457689c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-num")) { 457789c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-num", &val); 457889c67857SXiubo Li if (ret) 457989c67857SXiubo Li return ret; 458089c67857SXiubo Li 458189c67857SXiubo Li if (slots) 458289c67857SXiubo Li *slots = val; 458389c67857SXiubo Li } 458489c67857SXiubo Li 458589c67857SXiubo Li if (of_property_read_bool(np, "dai-tdm-slot-width")) { 458689c67857SXiubo Li ret = of_property_read_u32(np, "dai-tdm-slot-width", &val); 458789c67857SXiubo Li if (ret) 458889c67857SXiubo Li return ret; 458989c67857SXiubo Li 459089c67857SXiubo Li if (slot_width) 459189c67857SXiubo Li *slot_width = val; 459289c67857SXiubo Li } 459389c67857SXiubo Li 459489c67857SXiubo Li return 0; 459589c67857SXiubo Li } 459689c67857SXiubo Li EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot); 459789c67857SXiubo Li 4598a4a54dd5SStephen Warren int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, 4599a4a54dd5SStephen Warren const char *propname) 4600a4a54dd5SStephen Warren { 4601a4a54dd5SStephen Warren struct device_node *np = card->dev->of_node; 4602a4a54dd5SStephen Warren int num_routes; 4603a4a54dd5SStephen Warren struct snd_soc_dapm_route *routes; 4604a4a54dd5SStephen Warren int i, ret; 4605a4a54dd5SStephen Warren 4606a4a54dd5SStephen Warren num_routes = of_property_count_strings(np, propname); 4607c34ce320SRichard Zhao if (num_routes < 0 || num_routes & 1) { 460810e8aa9aSMichał Mirosław dev_err(card->dev, 460910e8aa9aSMichał Mirosław "ASoC: Property '%s' does not exist or its length is not even\n", 461010e8aa9aSMichał Mirosław propname); 4611a4a54dd5SStephen Warren return -EINVAL; 4612a4a54dd5SStephen Warren } 4613a4a54dd5SStephen Warren num_routes /= 2; 4614a4a54dd5SStephen Warren if (!num_routes) { 4615f110bfc7SLiam Girdwood dev_err(card->dev, "ASoC: Property '%s's length is zero\n", 4616a4a54dd5SStephen Warren propname); 4617a4a54dd5SStephen Warren return -EINVAL; 4618a4a54dd5SStephen Warren } 4619a4a54dd5SStephen Warren 4620a4a54dd5SStephen Warren routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes), 4621a4a54dd5SStephen Warren GFP_KERNEL); 4622a4a54dd5SStephen Warren if (!routes) { 4623a4a54dd5SStephen Warren dev_err(card->dev, 4624f110bfc7SLiam Girdwood "ASoC: Could not allocate DAPM route table\n"); 4625a4a54dd5SStephen Warren return -EINVAL; 4626a4a54dd5SStephen Warren } 4627a4a54dd5SStephen Warren 4628a4a54dd5SStephen Warren for (i = 0; i < num_routes; i++) { 4629a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 4630a4a54dd5SStephen Warren 2 * i, &routes[i].sink); 4631a4a54dd5SStephen Warren if (ret) { 4632c871bd0bSMark Brown dev_err(card->dev, 4633c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 4634c871bd0bSMark Brown propname, 2 * i, ret); 4635a4a54dd5SStephen Warren return -EINVAL; 4636a4a54dd5SStephen Warren } 4637a4a54dd5SStephen Warren ret = of_property_read_string_index(np, propname, 4638a4a54dd5SStephen Warren (2 * i) + 1, &routes[i].source); 4639a4a54dd5SStephen Warren if (ret) { 4640a4a54dd5SStephen Warren dev_err(card->dev, 4641c871bd0bSMark Brown "ASoC: Property '%s' index %d could not be read: %d\n", 4642c871bd0bSMark Brown propname, (2 * i) + 1, ret); 4643a4a54dd5SStephen Warren return -EINVAL; 4644a4a54dd5SStephen Warren } 4645a4a54dd5SStephen Warren } 4646a4a54dd5SStephen Warren 4647a4a54dd5SStephen Warren card->num_dapm_routes = num_routes; 4648a4a54dd5SStephen Warren card->dapm_routes = routes; 4649a4a54dd5SStephen Warren 4650a4a54dd5SStephen Warren return 0; 4651a4a54dd5SStephen Warren } 4652a4a54dd5SStephen Warren EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); 4653a4a54dd5SStephen Warren 4654a7930ed4SKuninori Morimoto unsigned int snd_soc_of_parse_daifmt(struct device_node *np, 4655389cb834SJyri Sarha const char *prefix, 4656389cb834SJyri Sarha struct device_node **bitclkmaster, 4657389cb834SJyri Sarha struct device_node **framemaster) 4658a7930ed4SKuninori Morimoto { 4659a7930ed4SKuninori Morimoto int ret, i; 4660a7930ed4SKuninori Morimoto char prop[128]; 4661a7930ed4SKuninori Morimoto unsigned int format = 0; 4662a7930ed4SKuninori Morimoto int bit, frame; 4663a7930ed4SKuninori Morimoto const char *str; 4664a7930ed4SKuninori Morimoto struct { 4665a7930ed4SKuninori Morimoto char *name; 4666a7930ed4SKuninori Morimoto unsigned int val; 4667a7930ed4SKuninori Morimoto } of_fmt_table[] = { 4668a7930ed4SKuninori Morimoto { "i2s", SND_SOC_DAIFMT_I2S }, 4669a7930ed4SKuninori Morimoto { "right_j", SND_SOC_DAIFMT_RIGHT_J }, 4670a7930ed4SKuninori Morimoto { "left_j", SND_SOC_DAIFMT_LEFT_J }, 4671a7930ed4SKuninori Morimoto { "dsp_a", SND_SOC_DAIFMT_DSP_A }, 4672a7930ed4SKuninori Morimoto { "dsp_b", SND_SOC_DAIFMT_DSP_B }, 4673a7930ed4SKuninori Morimoto { "ac97", SND_SOC_DAIFMT_AC97 }, 4674a7930ed4SKuninori Morimoto { "pdm", SND_SOC_DAIFMT_PDM}, 4675a7930ed4SKuninori Morimoto { "msb", SND_SOC_DAIFMT_MSB }, 4676a7930ed4SKuninori Morimoto { "lsb", SND_SOC_DAIFMT_LSB }, 4677a7930ed4SKuninori Morimoto }; 4678a7930ed4SKuninori Morimoto 4679a7930ed4SKuninori Morimoto if (!prefix) 4680a7930ed4SKuninori Morimoto prefix = ""; 4681a7930ed4SKuninori Morimoto 4682a7930ed4SKuninori Morimoto /* 4683a7930ed4SKuninori Morimoto * check "[prefix]format = xxx" 4684a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_FORMAT_MASK area 4685a7930ed4SKuninori Morimoto */ 4686a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sformat", prefix); 4687a7930ed4SKuninori Morimoto ret = of_property_read_string(np, prop, &str); 4688a7930ed4SKuninori Morimoto if (ret == 0) { 4689a7930ed4SKuninori Morimoto for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { 4690a7930ed4SKuninori Morimoto if (strcmp(str, of_fmt_table[i].name) == 0) { 4691a7930ed4SKuninori Morimoto format |= of_fmt_table[i].val; 4692a7930ed4SKuninori Morimoto break; 4693a7930ed4SKuninori Morimoto } 4694a7930ed4SKuninori Morimoto } 4695a7930ed4SKuninori Morimoto } 4696a7930ed4SKuninori Morimoto 4697a7930ed4SKuninori Morimoto /* 46988c2d6a9fSKuninori Morimoto * check "[prefix]continuous-clock" 4699a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_CLOCK_MASK area 4700a7930ed4SKuninori Morimoto */ 47018c2d6a9fSKuninori Morimoto snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); 47028c2d6a9fSKuninori Morimoto if (of_get_property(np, prop, NULL)) 47038c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_CONT; 47048c2d6a9fSKuninori Morimoto else 47058c2d6a9fSKuninori Morimoto format |= SND_SOC_DAIFMT_GATED; 4706a7930ed4SKuninori Morimoto 4707a7930ed4SKuninori Morimoto /* 4708a7930ed4SKuninori Morimoto * check "[prefix]bitclock-inversion" 4709a7930ed4SKuninori Morimoto * check "[prefix]frame-inversion" 4710a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_INV_MASK area 4711a7930ed4SKuninori Morimoto */ 4712a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); 4713a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 4714a7930ed4SKuninori Morimoto 4715a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); 4716a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 4717a7930ed4SKuninori Morimoto 4718a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 4719a7930ed4SKuninori Morimoto case 0x11: 4720a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_IF; 4721a7930ed4SKuninori Morimoto break; 4722a7930ed4SKuninori Morimoto case 0x10: 4723a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_IB_NF; 4724a7930ed4SKuninori Morimoto break; 4725a7930ed4SKuninori Morimoto case 0x01: 4726a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_NB_IF; 4727a7930ed4SKuninori Morimoto break; 4728a7930ed4SKuninori Morimoto default: 4729a7930ed4SKuninori Morimoto /* SND_SOC_DAIFMT_NB_NF is default */ 4730a7930ed4SKuninori Morimoto break; 4731a7930ed4SKuninori Morimoto } 4732a7930ed4SKuninori Morimoto 4733a7930ed4SKuninori Morimoto /* 4734a7930ed4SKuninori Morimoto * check "[prefix]bitclock-master" 4735a7930ed4SKuninori Morimoto * check "[prefix]frame-master" 4736a7930ed4SKuninori Morimoto * SND_SOC_DAIFMT_MASTER_MASK area 4737a7930ed4SKuninori Morimoto */ 4738a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); 4739a7930ed4SKuninori Morimoto bit = !!of_get_property(np, prop, NULL); 4740389cb834SJyri Sarha if (bit && bitclkmaster) 4741389cb834SJyri Sarha *bitclkmaster = of_parse_phandle(np, prop, 0); 4742a7930ed4SKuninori Morimoto 4743a7930ed4SKuninori Morimoto snprintf(prop, sizeof(prop), "%sframe-master", prefix); 4744a7930ed4SKuninori Morimoto frame = !!of_get_property(np, prop, NULL); 4745389cb834SJyri Sarha if (frame && framemaster) 4746389cb834SJyri Sarha *framemaster = of_parse_phandle(np, prop, 0); 4747a7930ed4SKuninori Morimoto 4748a7930ed4SKuninori Morimoto switch ((bit << 4) + frame) { 4749a7930ed4SKuninori Morimoto case 0x11: 4750a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFM; 4751a7930ed4SKuninori Morimoto break; 4752a7930ed4SKuninori Morimoto case 0x10: 4753a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBM_CFS; 4754a7930ed4SKuninori Morimoto break; 4755a7930ed4SKuninori Morimoto case 0x01: 4756a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFM; 4757a7930ed4SKuninori Morimoto break; 4758a7930ed4SKuninori Morimoto default: 4759a7930ed4SKuninori Morimoto format |= SND_SOC_DAIFMT_CBS_CFS; 4760a7930ed4SKuninori Morimoto break; 4761a7930ed4SKuninori Morimoto } 4762a7930ed4SKuninori Morimoto 4763a7930ed4SKuninori Morimoto return format; 4764a7930ed4SKuninori Morimoto } 4765a7930ed4SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); 4766a7930ed4SKuninori Morimoto 4767cb470087SKuninori Morimoto int snd_soc_of_get_dai_name(struct device_node *of_node, 4768cb470087SKuninori Morimoto const char **dai_name) 4769cb470087SKuninori Morimoto { 4770cb470087SKuninori Morimoto struct snd_soc_component *pos; 4771cb470087SKuninori Morimoto struct of_phandle_args args; 4772cb470087SKuninori Morimoto int ret; 4773cb470087SKuninori Morimoto 4774cb470087SKuninori Morimoto ret = of_parse_phandle_with_args(of_node, "sound-dai", 4775cb470087SKuninori Morimoto "#sound-dai-cells", 0, &args); 4776cb470087SKuninori Morimoto if (ret) 4777cb470087SKuninori Morimoto return ret; 4778cb470087SKuninori Morimoto 4779cb470087SKuninori Morimoto ret = -EPROBE_DEFER; 4780cb470087SKuninori Morimoto 4781cb470087SKuninori Morimoto mutex_lock(&client_mutex); 4782cb470087SKuninori Morimoto list_for_each_entry(pos, &component_list, list) { 4783cb470087SKuninori Morimoto if (pos->dev->of_node != args.np) 4784cb470087SKuninori Morimoto continue; 4785cb470087SKuninori Morimoto 47866833c452SKuninori Morimoto if (pos->driver->of_xlate_dai_name) { 47876833c452SKuninori Morimoto ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name); 47886833c452SKuninori Morimoto } else { 47896833c452SKuninori Morimoto int id = -1; 47906833c452SKuninori Morimoto 47916833c452SKuninori Morimoto switch (args.args_count) { 47926833c452SKuninori Morimoto case 0: 47936833c452SKuninori Morimoto id = 0; /* same as dai_drv[0] */ 47946833c452SKuninori Morimoto break; 47956833c452SKuninori Morimoto case 1: 47966833c452SKuninori Morimoto id = args.args[0]; 47976833c452SKuninori Morimoto break; 47986833c452SKuninori Morimoto default: 47996833c452SKuninori Morimoto /* not supported */ 4800cb470087SKuninori Morimoto break; 4801cb470087SKuninori Morimoto } 4802cb470087SKuninori Morimoto 48036833c452SKuninori Morimoto if (id < 0 || id >= pos->num_dai) { 48046833c452SKuninori Morimoto ret = -EINVAL; 48053dcba280SNicolin Chen continue; 48066833c452SKuninori Morimoto } 4807e41975edSXiubo Li 4808e41975edSXiubo Li ret = 0; 4809e41975edSXiubo Li 4810e41975edSXiubo Li *dai_name = pos->dai_drv[id].name; 4811e41975edSXiubo Li if (!*dai_name) 4812e41975edSXiubo Li *dai_name = pos->name; 48136833c452SKuninori Morimoto } 48146833c452SKuninori Morimoto 4815cb470087SKuninori Morimoto break; 4816cb470087SKuninori Morimoto } 4817cb470087SKuninori Morimoto mutex_unlock(&client_mutex); 4818cb470087SKuninori Morimoto 4819cb470087SKuninori Morimoto of_node_put(args.np); 4820cb470087SKuninori Morimoto 4821cb470087SKuninori Morimoto return ret; 4822cb470087SKuninori Morimoto } 4823cb470087SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); 4824cb470087SKuninori Morimoto 4825c9b3a40fSTakashi Iwai static int __init snd_soc_init(void) 4826db2a4165SFrank Mandarino { 4827db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS 48288a9dab1aSMark Brown snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); 48298a9dab1aSMark Brown if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) { 48300837fc62SFabio Estevam pr_warn("ASoC: Failed to create debugfs directory\n"); 48318a9dab1aSMark Brown snd_soc_debugfs_root = NULL; 4832db2a4165SFrank Mandarino } 4833c3c5a19aSMark Brown 48348a9dab1aSMark Brown if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL, 4835c3c5a19aSMark Brown &codec_list_fops)) 4836c3c5a19aSMark Brown pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); 4837c3c5a19aSMark Brown 48388a9dab1aSMark Brown if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, 4839f3208780SMark Brown &dai_list_fops)) 4840f3208780SMark Brown pr_warn("ASoC: Failed to create DAI list debugfs file\n"); 484119c7ac27SMark Brown 48428a9dab1aSMark Brown if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL, 484319c7ac27SMark Brown &platform_list_fops)) 484419c7ac27SMark Brown pr_warn("ASoC: Failed to create platform list debugfs file\n"); 4845db2a4165SFrank Mandarino #endif 4846db2a4165SFrank Mandarino 4847fb257897SMark Brown snd_soc_util_init(); 4848fb257897SMark Brown 4849db2a4165SFrank Mandarino return platform_driver_register(&soc_driver); 4850db2a4165SFrank Mandarino } 48514abe8e16SMark Brown module_init(snd_soc_init); 4852db2a4165SFrank Mandarino 4853db2a4165SFrank Mandarino static void __exit snd_soc_exit(void) 4854db2a4165SFrank Mandarino { 4855fb257897SMark Brown snd_soc_util_exit(); 4856fb257897SMark Brown 4857db2a4165SFrank Mandarino #ifdef CONFIG_DEBUG_FS 48588a9dab1aSMark Brown debugfs_remove_recursive(snd_soc_debugfs_root); 4859db2a4165SFrank Mandarino #endif 4860db2a4165SFrank Mandarino platform_driver_unregister(&soc_driver); 4861db2a4165SFrank Mandarino } 4862db2a4165SFrank Mandarino module_exit(snd_soc_exit); 4863db2a4165SFrank Mandarino 4864db2a4165SFrank Mandarino /* Module information */ 4865db2a4165SFrank Mandarino MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 4866db2a4165SFrank Mandarino MODULE_DESCRIPTION("ALSA SoC Core"); 4867db2a4165SFrank Mandarino MODULE_LICENSE("GPL"); 4868db2a4165SFrank Mandarino MODULE_ALIAS("platform:soc-audio"); 4869