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 static struct snd_ac97_bus soc_ac97_bus = {
38 .ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */
41 static void soc_ac97_device_release(struct device *dev)
43 kfree(to_ac97_t(dev));
47 struct snd_ac97_gpio_priv {
48 struct gpio_chip gpio_chip;
49 unsigned int gpios_set;
50 struct snd_soc_component *component;
53 static inline struct snd_soc_component *gpio_to_component(struct gpio_chip *chip)
55 struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
57 return gpio_priv->component;
60 static int snd_soc_ac97_gpio_request(struct gpio_chip *chip, unsigned offset)
62 if (offset >= AC97_NUM_GPIOS)
68 static int snd_soc_ac97_gpio_direction_in(struct gpio_chip *chip,
71 struct snd_soc_component *component = gpio_to_component(chip);
73 dev_dbg(component->dev, "set gpio %d to output\n", offset);
74 return snd_soc_component_update_bits(component, AC97_GPIO_CFG,
75 1 << offset, 1 << offset);
78 static int snd_soc_ac97_gpio_get(struct gpio_chip *chip, unsigned offset)
80 struct snd_soc_component *component = gpio_to_component(chip);
83 ret = snd_soc_component_read(component, AC97_GPIO_STATUS);
85 dev_dbg(component->dev, "get gpio %d : %d\n", offset,
88 return !!(ret & (1 << offset));
91 static void snd_soc_ac97_gpio_set(struct gpio_chip *chip, unsigned offset,
94 struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
95 struct snd_soc_component *component = gpio_to_component(chip);
97 gpio_priv->gpios_set &= ~(1 << offset);
98 gpio_priv->gpios_set |= (!!value) << offset;
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);
104 static int snd_soc_ac97_gpio_direction_out(struct gpio_chip *chip,
105 unsigned offset, int value)
107 struct snd_soc_component *component = gpio_to_component(chip);
109 dev_dbg(component->dev, "set gpio %d to output\n", offset);
110 snd_soc_ac97_gpio_set(chip, offset, value);
111 return snd_soc_component_update_bits(component, AC97_GPIO_CFG,
115 static const struct gpio_chip snd_soc_ac97_gpio_chip = {
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,
126 static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
127 struct snd_soc_component *component)
129 struct snd_ac97_gpio_priv *gpio_priv;
132 gpio_priv = devm_kzalloc(component->dev, sizeof(*gpio_priv), GFP_KERNEL);
135 ac97->gpio_priv = gpio_priv;
136 gpio_priv->component = component;
137 gpio_priv->gpio_chip = snd_soc_ac97_gpio_chip;
138 gpio_priv->gpio_chip.ngpio = AC97_NUM_GPIOS;
139 gpio_priv->gpio_chip.parent = component->dev;
140 gpio_priv->gpio_chip.base = -1;
142 ret = gpiochip_add_data(&gpio_priv->gpio_chip, gpio_priv);
144 dev_err(component->dev, "Failed to add GPIOs: %d\n", ret);
148 static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
150 gpiochip_remove(&ac97->gpio_priv->gpio_chip);
153 static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
154 struct snd_soc_component *component)
159 static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
165 * snd_soc_alloc_ac97_component() - Allocate new a AC'97 device
166 * @component: The COMPONENT for which to create the AC'97 device
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.
172 * Returns: A snd_ac97 device or a PTR_ERR in case of an error.
174 struct snd_ac97 *snd_soc_alloc_ac97_component(struct snd_soc_component *component)
176 struct snd_ac97 *ac97;
178 ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
180 return ERR_PTR(-ENOMEM);
182 ac97->bus = &soc_ac97_bus;
185 ac97->dev.bus = &ac97_bus_type;
186 ac97->dev.parent = component->card->dev;
187 ac97->dev.release = soc_ac97_device_release;
189 dev_set_name(&ac97->dev, "%d-%d:%s",
190 component->card->snd_card->number, 0,
193 device_initialize(&ac97->dev);
197 EXPORT_SYMBOL(snd_soc_alloc_ac97_component);
200 * snd_soc_new_ac97_component - initailise AC97 device
201 * @component: audio component
202 * @id: The expected device ID
203 * @id_mask: Mask that is applied to the device ID before comparing with @id
205 * Initialises AC97 component resources for use by ad-hoc devices only.
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.
211 * Returns: A PTR_ERR() on failure or a valid snd_ac97 struct on success.
213 struct snd_ac97 *snd_soc_new_ac97_component(struct snd_soc_component *component,
214 unsigned int id, unsigned int id_mask)
216 struct snd_ac97 *ac97;
219 ac97 = snd_soc_alloc_ac97_component(component);
224 ret = snd_ac97_reset(ac97, false, id, id_mask);
226 dev_err(component->dev, "Failed to reset AC97 device: %d\n",
232 ret = device_add(&ac97->dev);
236 ret = snd_soc_ac97_init_gpio(ac97, component);
243 put_device(&ac97->dev);
246 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_component);
249 * snd_soc_free_ac97_component - free AC97 component device
250 * @ac97: snd_ac97 device to be freed
252 * Frees AC97 component device resources.
254 void snd_soc_free_ac97_component(struct snd_ac97 *ac97)
256 snd_soc_ac97_free_gpio(ac97);
257 device_del(&ac97->dev);
259 put_device(&ac97->dev);
261 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_component);
263 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
265 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
267 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
269 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
271 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
275 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
277 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
281 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
283 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
285 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
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);
293 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
295 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
299 static int snd_soc_ac97_parse_pinctl(struct device *dev,
300 struct snd_ac97_reset_cfg *cfg)
303 struct pinctrl_state *state;
307 p = devm_pinctrl_get(dev);
309 dev_err(dev, "Failed to get pinctrl\n");
314 state = pinctrl_lookup_state(p, "ac97-reset");
316 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
317 return PTR_ERR(state);
319 cfg->pstate_reset = state;
321 state = pinctrl_lookup_state(p, "ac97-warm-reset");
323 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
324 return PTR_ERR(state);
326 cfg->pstate_warm_reset = state;
328 state = pinctrl_lookup_state(p, "ac97-running");
330 dev_err(dev, "Can't find pinctrl state ac97-running\n");
331 return PTR_ERR(state);
333 cfg->pstate_run = state;
335 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
337 dev_err(dev, "Can't find ac97-sync gpio\n");
340 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
342 dev_err(dev, "Failed requesting ac97-sync gpio\n");
345 cfg->gpio_sync = gpio;
347 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
349 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
352 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
354 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
357 cfg->gpio_sdata = gpio;
359 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
361 dev_err(dev, "Can't find ac97-reset gpio\n");
364 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
366 dev_err(dev, "Failed requesting ac97-reset gpio\n");
369 cfg->gpio_reset = gpio;
374 struct snd_ac97_bus_ops *soc_ac97_ops;
375 EXPORT_SYMBOL_GPL(soc_ac97_ops);
377 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
379 if (ops == soc_ac97_ops)
382 if (soc_ac97_ops && ops)
386 soc_ac97_bus.ops = ops;
390 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
393 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
395 * @pdev: platform device
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.
400 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
401 struct platform_device *pdev)
403 struct device *dev = &pdev->dev;
404 struct snd_ac97_reset_cfg cfg;
407 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
411 ret = snd_soc_set_ac97_ops(ops);
415 ops->warm_reset = snd_soc_ac97_warm_reset;
416 ops->reset = snd_soc_ac97_reset;
418 snd_ac97_rst_cfg = cfg;
421 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);