Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // soc-ac97.c  --  ALSA SoC Audio Layer AC97 support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) // Copyright 2005 Wolfson Microelectronics PLC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) // Copyright 2005 Openedhand Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) // Copyright (C) 2010 Slimlogic Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) // Copyright (C) 2010 Texas Instruments Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) // Author: Liam Girdwood <lrg@slimlogic.co.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) //         with code, comments and ideas from :-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) //         Richard Purdie <richard@openedhand.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/of_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/pinctrl/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <sound/ac97_codec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <sound/soc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct snd_ac97_reset_cfg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct pinctrl *pctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct pinctrl_state *pstate_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct pinctrl_state *pstate_warm_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct pinctrl_state *pstate_run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	int gpio_sdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	int gpio_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int gpio_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) struct snd_ac97_gpio_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #ifdef CONFIG_GPIOLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct gpio_chip gpio_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	unsigned int gpios_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct snd_soc_component *component;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static struct snd_ac97_bus soc_ac97_bus = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	.ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static void soc_ac97_device_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	kfree(to_ac97_t(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #ifdef CONFIG_GPIOLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static inline struct snd_soc_component *gpio_to_component(struct gpio_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return gpio_priv->component;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static int snd_soc_ac97_gpio_request(struct gpio_chip *chip, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (offset >= AC97_NUM_GPIOS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static int snd_soc_ac97_gpio_direction_in(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 					  unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct snd_soc_component *component = gpio_to_component(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	dev_dbg(component->dev, "set gpio %d to output\n", offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return snd_soc_component_update_bits(component, AC97_GPIO_CFG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 				   1 << offset, 1 << offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static int snd_soc_ac97_gpio_get(struct gpio_chip *chip, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct snd_soc_component *component = gpio_to_component(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	ret = snd_soc_component_read(component, AC97_GPIO_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	dev_dbg(component->dev, "get gpio %d : %d\n", offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		ret & (1 << offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	return !!(ret & (1 << offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static void snd_soc_ac97_gpio_set(struct gpio_chip *chip, unsigned offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				  int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct snd_soc_component *component = gpio_to_component(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	gpio_priv->gpios_set &= ~(1 << offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	gpio_priv->gpios_set |= (!!value) << offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	snd_soc_component_write(component, AC97_GPIO_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				gpio_priv->gpios_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	dev_dbg(component->dev, "set gpio %d to %d\n", offset, !!value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int snd_soc_ac97_gpio_direction_out(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				     unsigned offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct snd_soc_component *component = gpio_to_component(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	dev_dbg(component->dev, "set gpio %d to output\n", offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	snd_soc_ac97_gpio_set(chip, offset, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	return snd_soc_component_update_bits(component, AC97_GPIO_CFG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 					     1 << offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static const struct gpio_chip snd_soc_ac97_gpio_chip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	.label			= "snd_soc_ac97",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	.owner			= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	.request		= snd_soc_ac97_gpio_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	.direction_input	= snd_soc_ac97_gpio_direction_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	.get			= snd_soc_ac97_gpio_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	.direction_output	= snd_soc_ac97_gpio_direction_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	.set			= snd_soc_ac97_gpio_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	.can_sleep		= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				  struct snd_soc_component *component)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct snd_ac97_gpio_priv *gpio_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	gpio_priv = devm_kzalloc(component->dev, sizeof(*gpio_priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (!gpio_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	ac97->gpio_priv = gpio_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	gpio_priv->component = component;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	gpio_priv->gpio_chip = snd_soc_ac97_gpio_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	gpio_priv->gpio_chip.ngpio = AC97_NUM_GPIOS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	gpio_priv->gpio_chip.parent = component->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	gpio_priv->gpio_chip.base = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	ret = gpiochip_add_data(&gpio_priv->gpio_chip, gpio_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		dev_err(component->dev, "Failed to add GPIOs: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	gpiochip_remove(&ac97->gpio_priv->gpio_chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				  struct snd_soc_component *component)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * snd_soc_alloc_ac97_component() - Allocate new a AC'97 device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * @component: The COMPONENT for which to create the AC'97 device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * Allocated a new snd_ac97 device and intializes it, but does not yet register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  * it. The caller is responsible to either call device_add(&ac97->dev) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * register the device, or to call put_device(&ac97->dev) to free the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * Returns: A snd_ac97 device or a PTR_ERR in case of an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct snd_ac97 *snd_soc_alloc_ac97_component(struct snd_soc_component *component)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	struct snd_ac97 *ac97;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (ac97 == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	ac97->bus = &soc_ac97_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	ac97->num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	ac97->dev.bus = &ac97_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	ac97->dev.parent = component->card->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	ac97->dev.release = soc_ac97_device_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	dev_set_name(&ac97->dev, "%d-%d:%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		     component->card->snd_card->number, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		     component->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	device_initialize(&ac97->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return ac97;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) EXPORT_SYMBOL(snd_soc_alloc_ac97_component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  * snd_soc_new_ac97_component - initailise AC97 device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  * @component: audio component
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  * @id: The expected device ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * @id_mask: Mask that is applied to the device ID before comparing with @id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  * Initialises AC97 component resources for use by ad-hoc devices only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  * If @id is not 0 this function will reset the device, then read the ID from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * the device and check if it matches the expected ID. If it doesn't match an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * error will be returned and device will not be registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * Returns: A PTR_ERR() on failure or a valid snd_ac97 struct on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct snd_ac97 *snd_soc_new_ac97_component(struct snd_soc_component *component,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	unsigned int id, unsigned int id_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct snd_ac97 *ac97;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	ac97 = snd_soc_alloc_ac97_component(component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (IS_ERR(ac97))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		return ac97;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		ret = snd_ac97_reset(ac97, false, id, id_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			dev_err(component->dev, "Failed to reset AC97 device: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	ret = device_add(&ac97->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	ret = snd_soc_ac97_init_gpio(ac97, component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	return ac97;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) err_put_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	put_device(&ac97->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) EXPORT_SYMBOL_GPL(snd_soc_new_ac97_component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  * snd_soc_free_ac97_component - free AC97 component device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  * @ac97: snd_ac97 device to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  * Frees AC97 component device resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) void snd_soc_free_ac97_component(struct snd_ac97 *ac97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	snd_soc_ac97_free_gpio(ac97);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	device_del(&ac97->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	ac97->bus = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	put_device(&ac97->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) EXPORT_SYMBOL_GPL(snd_soc_free_ac97_component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	udelay(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	msleep(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	udelay(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	msleep(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int snd_soc_ac97_parse_pinctl(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		struct snd_ac97_reset_cfg *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	struct pinctrl *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	struct pinctrl_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	int gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	p = devm_pinctrl_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (IS_ERR(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		dev_err(dev, "Failed to get pinctrl\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		return PTR_ERR(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	cfg->pctl = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	state = pinctrl_lookup_state(p, "ac97-reset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (IS_ERR(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		dev_err(dev, "Can't find pinctrl state ac97-reset\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		return PTR_ERR(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	cfg->pstate_reset = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	state = pinctrl_lookup_state(p, "ac97-warm-reset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (IS_ERR(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return PTR_ERR(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	cfg->pstate_warm_reset = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	state = pinctrl_lookup_state(p, "ac97-running");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (IS_ERR(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		dev_err(dev, "Can't find pinctrl state ac97-running\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		return PTR_ERR(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	cfg->pstate_run = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (gpio < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		dev_err(dev, "Can't find ac97-sync gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		return gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	ret = devm_gpio_request(dev, gpio, "AC97 link sync");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		dev_err(dev, "Failed requesting ac97-sync gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	cfg->gpio_sync = gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	if (gpio < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		return gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		dev_err(dev, "Failed requesting ac97-sdata gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	cfg->gpio_sdata = gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (gpio < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		dev_err(dev, "Can't find ac97-reset gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		return gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	ret = devm_gpio_request(dev, gpio, "AC97 link reset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		dev_err(dev, "Failed requesting ac97-reset gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	cfg->gpio_reset = gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) struct snd_ac97_bus_ops *soc_ac97_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) EXPORT_SYMBOL_GPL(soc_ac97_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	if (ops == soc_ac97_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (soc_ac97_ops && ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	soc_ac97_ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	soc_ac97_bus.ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)  * @ops: bus ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)  * @pdev: platform device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)  * This function sets the reset and warm_reset properties of ops and parses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)  * the device node of pdev to get pinctrl states and gpio numbers to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	struct snd_ac97_reset_cfg cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	ret = snd_soc_set_ac97_ops(ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	ops->warm_reset = snd_soc_ac97_warm_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	ops->reset = snd_soc_ac97_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	snd_ac97_rst_cfg = cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);