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