File Index
Files and directories
Sign in
FILE

soc-ac97.c

File preview with syntax highlighting.

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