1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-ac97.c -- ALSA SoC Audio Layer AC97 support
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
10 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
11 // with code, comments and ideas from :-
12 // Richard Purdie <richard@openedhand.com>
14 #include <linux/ctype.h>
15 #include <linux/delay.h>
16 #include <linux/export.h>
17 #include <linux/gpio.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/init.h>
20 #include <linux/of_gpio.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/slab.h>
24 #include <sound/ac97_codec.h>
25 #include <sound/soc.h>
27 struct snd_ac97_reset_cfg {
29 struct pinctrl_state *pstate_reset;
30 struct pinctrl_state *pstate_warm_reset;
31 struct pinctrl_state *pstate_run;
37 struct snd_ac97_gpio_priv {
39 struct gpio_chip gpio_chip;
41 unsigned int gpios_set;
42 struct snd_soc_component *component;
45 static struct snd_ac97_bus soc_ac97_bus = {
46 .ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */
49 static void soc_ac97_device_release(struct device *dev)
51 kfree(to_ac97_t(dev));
55 static inline struct snd_soc_component *gpio_to_component(struct gpio_chip *chip)
57 struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
59 return gpio_priv->component;
62 static int snd_soc_ac97_gpio_request(struct gpio_chip *chip, unsigned offset)
64 if (offset >= AC97_NUM_GPIOS)
70 static int snd_soc_ac97_gpio_direction_in(struct gpio_chip *chip,
73 struct snd_soc_component *component = gpio_to_component(chip);
75 dev_dbg(component->dev, "set gpio %d to output\n", offset);
76 return snd_soc_component_update_bits(component, AC97_GPIO_CFG,
77 1 << offset, 1 << offset);
80 static int snd_soc_ac97_gpio_get(struct gpio_chip *chip, unsigned offset)
82 struct snd_soc_component *component = gpio_to_component(chip);
85 if (snd_soc_component_read(component, AC97_GPIO_STATUS, &ret) < 0)
88 dev_dbg(component->dev, "get gpio %d : %d\n", offset,
89 ret < 0 ? ret : ret & (1 << offset));
91 return ret < 0 ? ret : !!(ret & (1 << offset));
94 static void snd_soc_ac97_gpio_set(struct gpio_chip *chip, unsigned offset,
97 struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
98 struct snd_soc_component *component = gpio_to_component(chip);
100 gpio_priv->gpios_set &= ~(1 << offset);
101 gpio_priv->gpios_set |= (!!value) << offset;
102 snd_soc_component_write(component, AC97_GPIO_STATUS,
103 gpio_priv->gpios_set);
104 dev_dbg(component->dev, "set gpio %d to %d\n", offset, !!value);
107 static int snd_soc_ac97_gpio_direction_out(struct gpio_chip *chip,
108 unsigned offset, int value)
110 struct snd_soc_component *component = gpio_to_component(chip);
112 dev_dbg(component->dev, "set gpio %d to output\n", offset);
113 snd_soc_ac97_gpio_set(chip, offset, value);
114 return snd_soc_component_update_bits(component, AC97_GPIO_CFG,
118 static const struct gpio_chip snd_soc_ac97_gpio_chip = {
119 .label = "snd_soc_ac97",
120 .owner = THIS_MODULE,
121 .request = snd_soc_ac97_gpio_request,
122 .direction_input = snd_soc_ac97_gpio_direction_in,
123 .get = snd_soc_ac97_gpio_get,
124 .direction_output = snd_soc_ac97_gpio_direction_out,
125 .set = snd_soc_ac97_gpio_set,
129 static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
130 struct snd_soc_component *component)
132 struct snd_ac97_gpio_priv *gpio_priv;
135 gpio_priv = devm_kzalloc(component->dev, sizeof(*gpio_priv), GFP_KERNEL);
138 ac97->gpio_priv = gpio_priv;
139 gpio_priv->component = component;
140 gpio_priv->gpio_chip = snd_soc_ac97_gpio_chip;
141 gpio_priv->gpio_chip.ngpio = AC97_NUM_GPIOS;
142 gpio_priv->gpio_chip.parent = component->dev;
143 gpio_priv->gpio_chip.base = -1;
145 ret = gpiochip_add_data(&gpio_priv->gpio_chip, gpio_priv);
147 dev_err(component->dev, "Failed to add GPIOs: %d\n", ret);
151 static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
153 gpiochip_remove(&ac97->gpio_priv->gpio_chip);
156 static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
157 struct snd_soc_component *component)
162 static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
168 * snd_soc_alloc_ac97_component() - Allocate new a AC'97 device
169 * @component: The COMPONENT for which to create the AC'97 device
171 * Allocated a new snd_ac97 device and intializes it, but does not yet register
172 * it. The caller is responsible to either call device_add(&ac97->dev) to
173 * register the device, or to call put_device(&ac97->dev) to free the device.
175 * Returns: A snd_ac97 device or a PTR_ERR in case of an error.
177 struct snd_ac97 *snd_soc_alloc_ac97_component(struct snd_soc_component *component)
179 struct snd_ac97 *ac97;
181 ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
183 return ERR_PTR(-ENOMEM);
185 ac97->bus = &soc_ac97_bus;
188 ac97->dev.bus = &ac97_bus_type;
189 ac97->dev.parent = component->card->dev;
190 ac97->dev.release = soc_ac97_device_release;
192 dev_set_name(&ac97->dev, "%d-%d:%s",
193 component->card->snd_card->number, 0,
196 device_initialize(&ac97->dev);
200 EXPORT_SYMBOL(snd_soc_alloc_ac97_component);
203 * snd_soc_new_ac97_component - initailise AC97 device
204 * @component: audio component
205 * @id: The expected device ID
206 * @id_mask: Mask that is applied to the device ID before comparing with @id
208 * Initialises AC97 component resources for use by ad-hoc devices only.
210 * If @id is not 0 this function will reset the device, then read the ID from
211 * the device and check if it matches the expected ID. If it doesn't match an
212 * error will be returned and device will not be registered.
214 * Returns: A PTR_ERR() on failure or a valid snd_ac97 struct on success.
216 struct snd_ac97 *snd_soc_new_ac97_component(struct snd_soc_component *component,
217 unsigned int id, unsigned int id_mask)
219 struct snd_ac97 *ac97;
222 ac97 = snd_soc_alloc_ac97_component(component);
227 ret = snd_ac97_reset(ac97, false, id, id_mask);
229 dev_err(component->dev, "Failed to reset AC97 device: %d\n",
235 ret = device_add(&ac97->dev);
239 ret = snd_soc_ac97_init_gpio(ac97, component);
246 put_device(&ac97->dev);
249 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_component);
252 * snd_soc_free_ac97_component - free AC97 component device
253 * @ac97: snd_ac97 device to be freed
255 * Frees AC97 component device resources.
257 void snd_soc_free_ac97_component(struct snd_ac97 *ac97)
259 snd_soc_ac97_free_gpio(ac97);
260 device_del(&ac97->dev);
262 put_device(&ac97->dev);
264 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_component);
266 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
268 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
270 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
272 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
274 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
278 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
280 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
284 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
286 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
288 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
290 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
291 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
292 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
296 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
298 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
302 static int snd_soc_ac97_parse_pinctl(struct device *dev,
303 struct snd_ac97_reset_cfg *cfg)
306 struct pinctrl_state *state;
310 p = devm_pinctrl_get(dev);
312 dev_err(dev, "Failed to get pinctrl\n");
317 state = pinctrl_lookup_state(p, "ac97-reset");
319 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
320 return PTR_ERR(state);
322 cfg->pstate_reset = state;
324 state = pinctrl_lookup_state(p, "ac97-warm-reset");
326 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
327 return PTR_ERR(state);
329 cfg->pstate_warm_reset = state;
331 state = pinctrl_lookup_state(p, "ac97-running");
333 dev_err(dev, "Can't find pinctrl state ac97-running\n");
334 return PTR_ERR(state);
336 cfg->pstate_run = state;
338 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
340 dev_err(dev, "Can't find ac97-sync gpio\n");
343 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
345 dev_err(dev, "Failed requesting ac97-sync gpio\n");
348 cfg->gpio_sync = gpio;
350 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
352 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
355 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
357 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
360 cfg->gpio_sdata = gpio;
362 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
364 dev_err(dev, "Can't find ac97-reset gpio\n");
367 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
369 dev_err(dev, "Failed requesting ac97-reset gpio\n");
372 cfg->gpio_reset = gpio;
377 struct snd_ac97_bus_ops *soc_ac97_ops;
378 EXPORT_SYMBOL_GPL(soc_ac97_ops);
380 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
382 if (ops == soc_ac97_ops)
385 if (soc_ac97_ops && ops)
389 soc_ac97_bus.ops = ops;
393 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
396 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
398 * This function sets the reset and warm_reset properties of ops and parses
399 * the device node of pdev to get pinctrl states and gpio numbers to use.
401 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
402 struct platform_device *pdev)
404 struct device *dev = &pdev->dev;
405 struct snd_ac97_reset_cfg cfg;
408 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
412 ret = snd_soc_set_ac97_ops(ops);
416 ops->warm_reset = snd_soc_ac97_warm_reset;
417 ops->reset = snd_soc_ac97_reset;
419 snd_ac97_rst_cfg = cfg;
422 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);