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