18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * sysfs support for HD-audio core device 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/slab.h> 78c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 88c2ecf20Sopenharmony_ci#include <linux/device.h> 98c2ecf20Sopenharmony_ci#include <sound/core.h> 108c2ecf20Sopenharmony_ci#include <sound/hdaudio.h> 118c2ecf20Sopenharmony_ci#include "local.h" 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_cistruct hdac_widget_tree { 148c2ecf20Sopenharmony_ci struct kobject *root; 158c2ecf20Sopenharmony_ci struct kobject *afg; 168c2ecf20Sopenharmony_ci struct kobject **nodes; 178c2ecf20Sopenharmony_ci}; 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define CODEC_ATTR(type) \ 208c2ecf20Sopenharmony_cistatic ssize_t type##_show(struct device *dev, \ 218c2ecf20Sopenharmony_ci struct device_attribute *attr, \ 228c2ecf20Sopenharmony_ci char *buf) \ 238c2ecf20Sopenharmony_ci{ \ 248c2ecf20Sopenharmony_ci struct hdac_device *codec = dev_to_hdac_dev(dev); \ 258c2ecf20Sopenharmony_ci return sprintf(buf, "0x%x\n", codec->type); \ 268c2ecf20Sopenharmony_ci} \ 278c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(type) 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define CODEC_ATTR_STR(type) \ 308c2ecf20Sopenharmony_cistatic ssize_t type##_show(struct device *dev, \ 318c2ecf20Sopenharmony_ci struct device_attribute *attr, \ 328c2ecf20Sopenharmony_ci char *buf) \ 338c2ecf20Sopenharmony_ci{ \ 348c2ecf20Sopenharmony_ci struct hdac_device *codec = dev_to_hdac_dev(dev); \ 358c2ecf20Sopenharmony_ci return sprintf(buf, "%s\n", \ 368c2ecf20Sopenharmony_ci codec->type ? codec->type : ""); \ 378c2ecf20Sopenharmony_ci} \ 388c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(type) 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ciCODEC_ATTR(type); 418c2ecf20Sopenharmony_ciCODEC_ATTR(vendor_id); 428c2ecf20Sopenharmony_ciCODEC_ATTR(subsystem_id); 438c2ecf20Sopenharmony_ciCODEC_ATTR(revision_id); 448c2ecf20Sopenharmony_ciCODEC_ATTR(afg); 458c2ecf20Sopenharmony_ciCODEC_ATTR(mfg); 468c2ecf20Sopenharmony_ciCODEC_ATTR_STR(vendor_name); 478c2ecf20Sopenharmony_ciCODEC_ATTR_STR(chip_name); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 508c2ecf20Sopenharmony_ci char *buf) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci return snd_hdac_codec_modalias(dev_to_hdac_dev(dev), buf, 256); 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(modalias); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic struct attribute *hdac_dev_attrs[] = { 578c2ecf20Sopenharmony_ci &dev_attr_type.attr, 588c2ecf20Sopenharmony_ci &dev_attr_vendor_id.attr, 598c2ecf20Sopenharmony_ci &dev_attr_subsystem_id.attr, 608c2ecf20Sopenharmony_ci &dev_attr_revision_id.attr, 618c2ecf20Sopenharmony_ci &dev_attr_afg.attr, 628c2ecf20Sopenharmony_ci &dev_attr_mfg.attr, 638c2ecf20Sopenharmony_ci &dev_attr_vendor_name.attr, 648c2ecf20Sopenharmony_ci &dev_attr_chip_name.attr, 658c2ecf20Sopenharmony_ci &dev_attr_modalias.attr, 668c2ecf20Sopenharmony_ci NULL 678c2ecf20Sopenharmony_ci}; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistatic struct attribute_group hdac_dev_attr_group = { 708c2ecf20Sopenharmony_ci .attrs = hdac_dev_attrs, 718c2ecf20Sopenharmony_ci}; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ciconst struct attribute_group *hdac_dev_attr_groups[] = { 748c2ecf20Sopenharmony_ci &hdac_dev_attr_group, 758c2ecf20Sopenharmony_ci NULL 768c2ecf20Sopenharmony_ci}; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/* 798c2ecf20Sopenharmony_ci * Widget tree sysfs 808c2ecf20Sopenharmony_ci * 818c2ecf20Sopenharmony_ci * This is a tree showing the attributes of each widget. It appears like 828c2ecf20Sopenharmony_ci * /sys/bus/hdaudioC0D0/widgets/04/caps 838c2ecf20Sopenharmony_ci */ 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistruct widget_attribute; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistruct widget_attribute { 888c2ecf20Sopenharmony_ci struct attribute attr; 898c2ecf20Sopenharmony_ci ssize_t (*show)(struct hdac_device *codec, hda_nid_t nid, 908c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf); 918c2ecf20Sopenharmony_ci ssize_t (*store)(struct hdac_device *codec, hda_nid_t nid, 928c2ecf20Sopenharmony_ci struct widget_attribute *attr, 938c2ecf20Sopenharmony_ci const char *buf, size_t count); 948c2ecf20Sopenharmony_ci}; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistatic int get_codec_nid(struct kobject *kobj, struct hdac_device **codecp) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci struct device *dev = kobj_to_dev(kobj->parent->parent); 998c2ecf20Sopenharmony_ci int nid; 1008c2ecf20Sopenharmony_ci ssize_t ret; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci ret = kstrtoint(kobj->name, 16, &nid); 1038c2ecf20Sopenharmony_ci if (ret < 0) 1048c2ecf20Sopenharmony_ci return ret; 1058c2ecf20Sopenharmony_ci *codecp = dev_to_hdac_dev(dev); 1068c2ecf20Sopenharmony_ci return nid; 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic ssize_t widget_attr_show(struct kobject *kobj, struct attribute *attr, 1108c2ecf20Sopenharmony_ci char *buf) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci struct widget_attribute *wid_attr = 1138c2ecf20Sopenharmony_ci container_of(attr, struct widget_attribute, attr); 1148c2ecf20Sopenharmony_ci struct hdac_device *codec; 1158c2ecf20Sopenharmony_ci int nid; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci if (!wid_attr->show) 1188c2ecf20Sopenharmony_ci return -EIO; 1198c2ecf20Sopenharmony_ci nid = get_codec_nid(kobj, &codec); 1208c2ecf20Sopenharmony_ci if (nid < 0) 1218c2ecf20Sopenharmony_ci return nid; 1228c2ecf20Sopenharmony_ci return wid_attr->show(codec, nid, wid_attr, buf); 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic ssize_t widget_attr_store(struct kobject *kobj, struct attribute *attr, 1268c2ecf20Sopenharmony_ci const char *buf, size_t count) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci struct widget_attribute *wid_attr = 1298c2ecf20Sopenharmony_ci container_of(attr, struct widget_attribute, attr); 1308c2ecf20Sopenharmony_ci struct hdac_device *codec; 1318c2ecf20Sopenharmony_ci int nid; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci if (!wid_attr->store) 1348c2ecf20Sopenharmony_ci return -EIO; 1358c2ecf20Sopenharmony_ci nid = get_codec_nid(kobj, &codec); 1368c2ecf20Sopenharmony_ci if (nid < 0) 1378c2ecf20Sopenharmony_ci return nid; 1388c2ecf20Sopenharmony_ci return wid_attr->store(codec, nid, wid_attr, buf, count); 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistatic const struct sysfs_ops widget_sysfs_ops = { 1428c2ecf20Sopenharmony_ci .show = widget_attr_show, 1438c2ecf20Sopenharmony_ci .store = widget_attr_store, 1448c2ecf20Sopenharmony_ci}; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic void widget_release(struct kobject *kobj) 1478c2ecf20Sopenharmony_ci{ 1488c2ecf20Sopenharmony_ci kfree(kobj); 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic struct kobj_type widget_ktype = { 1528c2ecf20Sopenharmony_ci .release = widget_release, 1538c2ecf20Sopenharmony_ci .sysfs_ops = &widget_sysfs_ops, 1548c2ecf20Sopenharmony_ci}; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci#define WIDGET_ATTR_RO(_name) \ 1578c2ecf20Sopenharmony_ci struct widget_attribute wid_attr_##_name = __ATTR_RO(_name) 1588c2ecf20Sopenharmony_ci#define WIDGET_ATTR_RW(_name) \ 1598c2ecf20Sopenharmony_ci struct widget_attribute wid_attr_##_name = __ATTR_RW(_name) 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic ssize_t caps_show(struct hdac_device *codec, hda_nid_t nid, 1628c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci return sprintf(buf, "0x%08x\n", get_wcaps(codec, nid)); 1658c2ecf20Sopenharmony_ci} 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_cistatic ssize_t pin_caps_show(struct hdac_device *codec, hda_nid_t nid, 1688c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) 1718c2ecf20Sopenharmony_ci return 0; 1728c2ecf20Sopenharmony_ci return sprintf(buf, "0x%08x\n", 1738c2ecf20Sopenharmony_ci snd_hdac_read_parm(codec, nid, AC_PAR_PIN_CAP)); 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic ssize_t pin_cfg_show(struct hdac_device *codec, hda_nid_t nid, 1778c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci unsigned int val; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) 1828c2ecf20Sopenharmony_ci return 0; 1838c2ecf20Sopenharmony_ci if (snd_hdac_read(codec, nid, AC_VERB_GET_CONFIG_DEFAULT, 0, &val)) 1848c2ecf20Sopenharmony_ci return 0; 1858c2ecf20Sopenharmony_ci return sprintf(buf, "0x%08x\n", val); 1868c2ecf20Sopenharmony_ci} 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_cistatic bool has_pcm_cap(struct hdac_device *codec, hda_nid_t nid) 1898c2ecf20Sopenharmony_ci{ 1908c2ecf20Sopenharmony_ci if (nid == codec->afg || nid == codec->mfg) 1918c2ecf20Sopenharmony_ci return true; 1928c2ecf20Sopenharmony_ci switch (get_wcaps_type(get_wcaps(codec, nid))) { 1938c2ecf20Sopenharmony_ci case AC_WID_AUD_OUT: 1948c2ecf20Sopenharmony_ci case AC_WID_AUD_IN: 1958c2ecf20Sopenharmony_ci return true; 1968c2ecf20Sopenharmony_ci default: 1978c2ecf20Sopenharmony_ci return false; 1988c2ecf20Sopenharmony_ci } 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cistatic ssize_t pcm_caps_show(struct hdac_device *codec, hda_nid_t nid, 2028c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf) 2038c2ecf20Sopenharmony_ci{ 2048c2ecf20Sopenharmony_ci if (!has_pcm_cap(codec, nid)) 2058c2ecf20Sopenharmony_ci return 0; 2068c2ecf20Sopenharmony_ci return sprintf(buf, "0x%08x\n", 2078c2ecf20Sopenharmony_ci snd_hdac_read_parm(codec, nid, AC_PAR_PCM)); 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_cistatic ssize_t pcm_formats_show(struct hdac_device *codec, hda_nid_t nid, 2118c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci if (!has_pcm_cap(codec, nid)) 2148c2ecf20Sopenharmony_ci return 0; 2158c2ecf20Sopenharmony_ci return sprintf(buf, "0x%08x\n", 2168c2ecf20Sopenharmony_ci snd_hdac_read_parm(codec, nid, AC_PAR_STREAM)); 2178c2ecf20Sopenharmony_ci} 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_cistatic ssize_t amp_in_caps_show(struct hdac_device *codec, hda_nid_t nid, 2208c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf) 2218c2ecf20Sopenharmony_ci{ 2228c2ecf20Sopenharmony_ci if (nid != codec->afg && !(get_wcaps(codec, nid) & AC_WCAP_IN_AMP)) 2238c2ecf20Sopenharmony_ci return 0; 2248c2ecf20Sopenharmony_ci return sprintf(buf, "0x%08x\n", 2258c2ecf20Sopenharmony_ci snd_hdac_read_parm(codec, nid, AC_PAR_AMP_IN_CAP)); 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic ssize_t amp_out_caps_show(struct hdac_device *codec, hda_nid_t nid, 2298c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci if (nid != codec->afg && !(get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)) 2328c2ecf20Sopenharmony_ci return 0; 2338c2ecf20Sopenharmony_ci return sprintf(buf, "0x%08x\n", 2348c2ecf20Sopenharmony_ci snd_hdac_read_parm(codec, nid, AC_PAR_AMP_OUT_CAP)); 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_cistatic ssize_t power_caps_show(struct hdac_device *codec, hda_nid_t nid, 2388c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf) 2398c2ecf20Sopenharmony_ci{ 2408c2ecf20Sopenharmony_ci if (nid != codec->afg && !(get_wcaps(codec, nid) & AC_WCAP_POWER)) 2418c2ecf20Sopenharmony_ci return 0; 2428c2ecf20Sopenharmony_ci return sprintf(buf, "0x%08x\n", 2438c2ecf20Sopenharmony_ci snd_hdac_read_parm(codec, nid, AC_PAR_POWER_STATE)); 2448c2ecf20Sopenharmony_ci} 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cistatic ssize_t gpio_caps_show(struct hdac_device *codec, hda_nid_t nid, 2478c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf) 2488c2ecf20Sopenharmony_ci{ 2498c2ecf20Sopenharmony_ci return sprintf(buf, "0x%08x\n", 2508c2ecf20Sopenharmony_ci snd_hdac_read_parm(codec, nid, AC_PAR_GPIO_CAP)); 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_cistatic ssize_t connections_show(struct hdac_device *codec, hda_nid_t nid, 2548c2ecf20Sopenharmony_ci struct widget_attribute *attr, char *buf) 2558c2ecf20Sopenharmony_ci{ 2568c2ecf20Sopenharmony_ci hda_nid_t list[32]; 2578c2ecf20Sopenharmony_ci int i, nconns; 2588c2ecf20Sopenharmony_ci ssize_t ret = 0; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci nconns = snd_hdac_get_connections(codec, nid, list, ARRAY_SIZE(list)); 2618c2ecf20Sopenharmony_ci if (nconns <= 0) 2628c2ecf20Sopenharmony_ci return nconns; 2638c2ecf20Sopenharmony_ci for (i = 0; i < nconns; i++) 2648c2ecf20Sopenharmony_ci ret += sprintf(buf + ret, "%s0x%02x", i ? " " : "", list[i]); 2658c2ecf20Sopenharmony_ci ret += sprintf(buf + ret, "\n"); 2668c2ecf20Sopenharmony_ci return ret; 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic WIDGET_ATTR_RO(caps); 2708c2ecf20Sopenharmony_cistatic WIDGET_ATTR_RO(pin_caps); 2718c2ecf20Sopenharmony_cistatic WIDGET_ATTR_RO(pin_cfg); 2728c2ecf20Sopenharmony_cistatic WIDGET_ATTR_RO(pcm_caps); 2738c2ecf20Sopenharmony_cistatic WIDGET_ATTR_RO(pcm_formats); 2748c2ecf20Sopenharmony_cistatic WIDGET_ATTR_RO(amp_in_caps); 2758c2ecf20Sopenharmony_cistatic WIDGET_ATTR_RO(amp_out_caps); 2768c2ecf20Sopenharmony_cistatic WIDGET_ATTR_RO(power_caps); 2778c2ecf20Sopenharmony_cistatic WIDGET_ATTR_RO(gpio_caps); 2788c2ecf20Sopenharmony_cistatic WIDGET_ATTR_RO(connections); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_cistatic struct attribute *widget_node_attrs[] = { 2818c2ecf20Sopenharmony_ci &wid_attr_caps.attr, 2828c2ecf20Sopenharmony_ci &wid_attr_pin_caps.attr, 2838c2ecf20Sopenharmony_ci &wid_attr_pin_cfg.attr, 2848c2ecf20Sopenharmony_ci &wid_attr_pcm_caps.attr, 2858c2ecf20Sopenharmony_ci &wid_attr_pcm_formats.attr, 2868c2ecf20Sopenharmony_ci &wid_attr_amp_in_caps.attr, 2878c2ecf20Sopenharmony_ci &wid_attr_amp_out_caps.attr, 2888c2ecf20Sopenharmony_ci &wid_attr_power_caps.attr, 2898c2ecf20Sopenharmony_ci &wid_attr_connections.attr, 2908c2ecf20Sopenharmony_ci NULL, 2918c2ecf20Sopenharmony_ci}; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_cistatic struct attribute *widget_afg_attrs[] = { 2948c2ecf20Sopenharmony_ci &wid_attr_pcm_caps.attr, 2958c2ecf20Sopenharmony_ci &wid_attr_pcm_formats.attr, 2968c2ecf20Sopenharmony_ci &wid_attr_amp_in_caps.attr, 2978c2ecf20Sopenharmony_ci &wid_attr_amp_out_caps.attr, 2988c2ecf20Sopenharmony_ci &wid_attr_power_caps.attr, 2998c2ecf20Sopenharmony_ci &wid_attr_gpio_caps.attr, 3008c2ecf20Sopenharmony_ci NULL, 3018c2ecf20Sopenharmony_ci}; 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_cistatic const struct attribute_group widget_node_group = { 3048c2ecf20Sopenharmony_ci .attrs = widget_node_attrs, 3058c2ecf20Sopenharmony_ci}; 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_cistatic const struct attribute_group widget_afg_group = { 3088c2ecf20Sopenharmony_ci .attrs = widget_afg_attrs, 3098c2ecf20Sopenharmony_ci}; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_cistatic void free_widget_node(struct kobject *kobj, 3128c2ecf20Sopenharmony_ci const struct attribute_group *group) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci if (kobj) { 3158c2ecf20Sopenharmony_ci sysfs_remove_group(kobj, group); 3168c2ecf20Sopenharmony_ci kobject_put(kobj); 3178c2ecf20Sopenharmony_ci } 3188c2ecf20Sopenharmony_ci} 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_cistatic void widget_tree_free(struct hdac_device *codec) 3218c2ecf20Sopenharmony_ci{ 3228c2ecf20Sopenharmony_ci struct hdac_widget_tree *tree = codec->widgets; 3238c2ecf20Sopenharmony_ci struct kobject **p; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci if (!tree) 3268c2ecf20Sopenharmony_ci return; 3278c2ecf20Sopenharmony_ci free_widget_node(tree->afg, &widget_afg_group); 3288c2ecf20Sopenharmony_ci if (tree->nodes) { 3298c2ecf20Sopenharmony_ci for (p = tree->nodes; *p; p++) 3308c2ecf20Sopenharmony_ci free_widget_node(*p, &widget_node_group); 3318c2ecf20Sopenharmony_ci kfree(tree->nodes); 3328c2ecf20Sopenharmony_ci } 3338c2ecf20Sopenharmony_ci kobject_put(tree->root); 3348c2ecf20Sopenharmony_ci kfree(tree); 3358c2ecf20Sopenharmony_ci codec->widgets = NULL; 3368c2ecf20Sopenharmony_ci} 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_cistatic int add_widget_node(struct kobject *parent, hda_nid_t nid, 3398c2ecf20Sopenharmony_ci const struct attribute_group *group, 3408c2ecf20Sopenharmony_ci struct kobject **res) 3418c2ecf20Sopenharmony_ci{ 3428c2ecf20Sopenharmony_ci struct kobject *kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); 3438c2ecf20Sopenharmony_ci int err; 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci if (!kobj) 3468c2ecf20Sopenharmony_ci return -ENOMEM; 3478c2ecf20Sopenharmony_ci kobject_init(kobj, &widget_ktype); 3488c2ecf20Sopenharmony_ci err = kobject_add(kobj, parent, "%02x", nid); 3498c2ecf20Sopenharmony_ci if (err < 0) { 3508c2ecf20Sopenharmony_ci kobject_put(kobj); 3518c2ecf20Sopenharmony_ci return err; 3528c2ecf20Sopenharmony_ci } 3538c2ecf20Sopenharmony_ci err = sysfs_create_group(kobj, group); 3548c2ecf20Sopenharmony_ci if (err < 0) { 3558c2ecf20Sopenharmony_ci kobject_put(kobj); 3568c2ecf20Sopenharmony_ci return err; 3578c2ecf20Sopenharmony_ci } 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci *res = kobj; 3608c2ecf20Sopenharmony_ci return 0; 3618c2ecf20Sopenharmony_ci} 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_cistatic int widget_tree_create(struct hdac_device *codec) 3648c2ecf20Sopenharmony_ci{ 3658c2ecf20Sopenharmony_ci struct hdac_widget_tree *tree; 3668c2ecf20Sopenharmony_ci int i, err; 3678c2ecf20Sopenharmony_ci hda_nid_t nid; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci tree = codec->widgets = kzalloc(sizeof(*tree), GFP_KERNEL); 3708c2ecf20Sopenharmony_ci if (!tree) 3718c2ecf20Sopenharmony_ci return -ENOMEM; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci tree->root = kobject_create_and_add("widgets", &codec->dev.kobj); 3748c2ecf20Sopenharmony_ci if (!tree->root) 3758c2ecf20Sopenharmony_ci return -ENOMEM; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci tree->nodes = kcalloc(codec->num_nodes + 1, sizeof(*tree->nodes), 3788c2ecf20Sopenharmony_ci GFP_KERNEL); 3798c2ecf20Sopenharmony_ci if (!tree->nodes) 3808c2ecf20Sopenharmony_ci return -ENOMEM; 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) { 3838c2ecf20Sopenharmony_ci err = add_widget_node(tree->root, nid, &widget_node_group, 3848c2ecf20Sopenharmony_ci &tree->nodes[i]); 3858c2ecf20Sopenharmony_ci if (err < 0) 3868c2ecf20Sopenharmony_ci return err; 3878c2ecf20Sopenharmony_ci } 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci if (codec->afg) { 3908c2ecf20Sopenharmony_ci err = add_widget_node(tree->root, codec->afg, 3918c2ecf20Sopenharmony_ci &widget_afg_group, &tree->afg); 3928c2ecf20Sopenharmony_ci if (err < 0) 3938c2ecf20Sopenharmony_ci return err; 3948c2ecf20Sopenharmony_ci } 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci kobject_uevent(tree->root, KOBJ_CHANGE); 3978c2ecf20Sopenharmony_ci return 0; 3988c2ecf20Sopenharmony_ci} 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci/* call with codec->widget_lock held */ 4018c2ecf20Sopenharmony_ciint hda_widget_sysfs_init(struct hdac_device *codec) 4028c2ecf20Sopenharmony_ci{ 4038c2ecf20Sopenharmony_ci int err; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci if (codec->widgets) 4068c2ecf20Sopenharmony_ci return 0; /* already created */ 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci err = widget_tree_create(codec); 4098c2ecf20Sopenharmony_ci if (err < 0) { 4108c2ecf20Sopenharmony_ci widget_tree_free(codec); 4118c2ecf20Sopenharmony_ci return err; 4128c2ecf20Sopenharmony_ci } 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci return 0; 4158c2ecf20Sopenharmony_ci} 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci/* call with codec->widget_lock held */ 4188c2ecf20Sopenharmony_civoid hda_widget_sysfs_exit(struct hdac_device *codec) 4198c2ecf20Sopenharmony_ci{ 4208c2ecf20Sopenharmony_ci widget_tree_free(codec); 4218c2ecf20Sopenharmony_ci} 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci/* call with codec->widget_lock held */ 4248c2ecf20Sopenharmony_ciint hda_widget_sysfs_reinit(struct hdac_device *codec, 4258c2ecf20Sopenharmony_ci hda_nid_t start_nid, int num_nodes) 4268c2ecf20Sopenharmony_ci{ 4278c2ecf20Sopenharmony_ci struct hdac_widget_tree *tree; 4288c2ecf20Sopenharmony_ci hda_nid_t end_nid = start_nid + num_nodes; 4298c2ecf20Sopenharmony_ci hda_nid_t nid; 4308c2ecf20Sopenharmony_ci int i; 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci if (!codec->widgets) 4338c2ecf20Sopenharmony_ci return 0; 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci tree = kmemdup(codec->widgets, sizeof(*tree), GFP_KERNEL); 4368c2ecf20Sopenharmony_ci if (!tree) 4378c2ecf20Sopenharmony_ci return -ENOMEM; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci tree->nodes = kcalloc(num_nodes + 1, sizeof(*tree->nodes), GFP_KERNEL); 4408c2ecf20Sopenharmony_ci if (!tree->nodes) { 4418c2ecf20Sopenharmony_ci kfree(tree); 4428c2ecf20Sopenharmony_ci return -ENOMEM; 4438c2ecf20Sopenharmony_ci } 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci /* prune non-existing nodes */ 4468c2ecf20Sopenharmony_ci for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) { 4478c2ecf20Sopenharmony_ci if (nid < start_nid || nid >= end_nid) 4488c2ecf20Sopenharmony_ci free_widget_node(codec->widgets->nodes[i], 4498c2ecf20Sopenharmony_ci &widget_node_group); 4508c2ecf20Sopenharmony_ci } 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci /* add new nodes */ 4538c2ecf20Sopenharmony_ci for (i = 0, nid = start_nid; i < num_nodes; i++, nid++) { 4548c2ecf20Sopenharmony_ci if (nid < codec->start_nid || nid >= codec->end_nid) 4558c2ecf20Sopenharmony_ci add_widget_node(tree->root, nid, &widget_node_group, 4568c2ecf20Sopenharmony_ci &tree->nodes[i]); 4578c2ecf20Sopenharmony_ci else 4588c2ecf20Sopenharmony_ci tree->nodes[i] = 4598c2ecf20Sopenharmony_ci codec->widgets->nodes[nid - codec->start_nid]; 4608c2ecf20Sopenharmony_ci } 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci /* replace with the new tree */ 4638c2ecf20Sopenharmony_ci kfree(codec->widgets->nodes); 4648c2ecf20Sopenharmony_ci kfree(codec->widgets); 4658c2ecf20Sopenharmony_ci codec->widgets = tree; 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci kobject_uevent(tree->root, KOBJ_CHANGE); 4688c2ecf20Sopenharmony_ci return 0; 4698c2ecf20Sopenharmony_ci} 470