2 * soc-ac97.c -- ALSA SoC Audio Layer AC97 support
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
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.
19 #include <linux/ctype.h>
20 #include <linux/delay.h>
21 #include <linux/export.h>
22 #include <linux/gpio.h>
23 #include <linux/init.h>
24 #include <linux/of_gpio.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/slab.h>
28 #include <sound/ac97_codec.h>
29 #include <sound/soc.h>
31 struct snd_ac97_reset_cfg
{
33 struct pinctrl_state
*pstate_reset
;
34 struct pinctrl_state
*pstate_warm_reset
;
35 struct pinctrl_state
*pstate_run
;
41 static struct snd_ac97_bus soc_ac97_bus
= {
42 .ops
= NULL
, /* Gets initialized in snd_soc_set_ac97_ops() */
45 static void soc_ac97_device_release(struct device
*dev
)
47 kfree(to_ac97_t(dev
));
51 * snd_soc_alloc_ac97_codec() - Allocate new a AC'97 device
52 * @codec: The CODEC for which to create the AC'97 device
54 * Allocated a new snd_ac97 device and intializes it, but does not yet register
55 * it. The caller is responsible to either call device_add(&ac97->dev) to
56 * register the device, or to call put_device(&ac97->dev) to free the device.
58 * Returns: A snd_ac97 device or a PTR_ERR in case of an error.
60 struct snd_ac97
*snd_soc_alloc_ac97_codec(struct snd_soc_codec
*codec
)
62 struct snd_ac97
*ac97
;
64 ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
66 return ERR_PTR(-ENOMEM
);
68 ac97
->bus
= &soc_ac97_bus
;
71 ac97
->dev
.bus
= &ac97_bus_type
;
72 ac97
->dev
.parent
= codec
->component
.card
->dev
;
73 ac97
->dev
.release
= soc_ac97_device_release
;
75 dev_set_name(&ac97
->dev
, "%d-%d:%s",
76 codec
->component
.card
->snd_card
->number
, 0,
77 codec
->component
.name
);
79 device_initialize(&ac97
->dev
);
83 EXPORT_SYMBOL(snd_soc_alloc_ac97_codec
);
86 * snd_soc_new_ac97_codec - initailise AC97 device
88 * @id: The expected device ID
89 * @id_mask: Mask that is applied to the device ID before comparing with @id
91 * Initialises AC97 codec resources for use by ad-hoc devices only.
93 * If @id is not 0 this function will reset the device, then read the ID from
94 * the device and check if it matches the expected ID. If it doesn't match an
95 * error will be returned and device will not be registered.
97 * Returns: A PTR_ERR() on failure or a valid snd_ac97 struct on success.
99 struct snd_ac97
*snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
100 unsigned int id
, unsigned int id_mask
)
102 struct snd_ac97
*ac97
;
105 ac97
= snd_soc_alloc_ac97_codec(codec
);
110 ret
= snd_ac97_reset(ac97
, false, id
, id_mask
);
112 dev_err(codec
->dev
, "Failed to reset AC97 device: %d\n",
118 ret
= device_add(&ac97
->dev
);
125 put_device(&ac97
->dev
);
128 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
131 * snd_soc_free_ac97_codec - free AC97 codec device
132 * @codec: audio codec
134 * Frees AC97 codec device resources.
136 void snd_soc_free_ac97_codec(struct snd_ac97
*ac97
)
138 device_del(&ac97
->dev
);
140 put_device(&ac97
->dev
);
142 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
144 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg
;
146 static void snd_soc_ac97_warm_reset(struct snd_ac97
*ac97
)
148 struct pinctrl
*pctl
= snd_ac97_rst_cfg
.pctl
;
150 pinctrl_select_state(pctl
, snd_ac97_rst_cfg
.pstate_warm_reset
);
152 gpio_direction_output(snd_ac97_rst_cfg
.gpio_sync
, 1);
156 gpio_direction_output(snd_ac97_rst_cfg
.gpio_sync
, 0);
158 pinctrl_select_state(pctl
, snd_ac97_rst_cfg
.pstate_run
);
162 static void snd_soc_ac97_reset(struct snd_ac97
*ac97
)
164 struct pinctrl
*pctl
= snd_ac97_rst_cfg
.pctl
;
166 pinctrl_select_state(pctl
, snd_ac97_rst_cfg
.pstate_reset
);
168 gpio_direction_output(snd_ac97_rst_cfg
.gpio_sync
, 0);
169 gpio_direction_output(snd_ac97_rst_cfg
.gpio_sdata
, 0);
170 gpio_direction_output(snd_ac97_rst_cfg
.gpio_reset
, 0);
174 gpio_direction_output(snd_ac97_rst_cfg
.gpio_reset
, 1);
176 pinctrl_select_state(pctl
, snd_ac97_rst_cfg
.pstate_run
);
180 static int snd_soc_ac97_parse_pinctl(struct device
*dev
,
181 struct snd_ac97_reset_cfg
*cfg
)
184 struct pinctrl_state
*state
;
188 p
= devm_pinctrl_get(dev
);
190 dev_err(dev
, "Failed to get pinctrl\n");
195 state
= pinctrl_lookup_state(p
, "ac97-reset");
197 dev_err(dev
, "Can't find pinctrl state ac97-reset\n");
198 return PTR_ERR(state
);
200 cfg
->pstate_reset
= state
;
202 state
= pinctrl_lookup_state(p
, "ac97-warm-reset");
204 dev_err(dev
, "Can't find pinctrl state ac97-warm-reset\n");
205 return PTR_ERR(state
);
207 cfg
->pstate_warm_reset
= state
;
209 state
= pinctrl_lookup_state(p
, "ac97-running");
211 dev_err(dev
, "Can't find pinctrl state ac97-running\n");
212 return PTR_ERR(state
);
214 cfg
->pstate_run
= state
;
216 gpio
= of_get_named_gpio(dev
->of_node
, "ac97-gpios", 0);
218 dev_err(dev
, "Can't find ac97-sync gpio\n");
221 ret
= devm_gpio_request(dev
, gpio
, "AC97 link sync");
223 dev_err(dev
, "Failed requesting ac97-sync gpio\n");
226 cfg
->gpio_sync
= gpio
;
228 gpio
= of_get_named_gpio(dev
->of_node
, "ac97-gpios", 1);
230 dev_err(dev
, "Can't find ac97-sdata gpio %d\n", gpio
);
233 ret
= devm_gpio_request(dev
, gpio
, "AC97 link sdata");
235 dev_err(dev
, "Failed requesting ac97-sdata gpio\n");
238 cfg
->gpio_sdata
= gpio
;
240 gpio
= of_get_named_gpio(dev
->of_node
, "ac97-gpios", 2);
242 dev_err(dev
, "Can't find ac97-reset gpio\n");
245 ret
= devm_gpio_request(dev
, gpio
, "AC97 link reset");
247 dev_err(dev
, "Failed requesting ac97-reset gpio\n");
250 cfg
->gpio_reset
= gpio
;
255 struct snd_ac97_bus_ops
*soc_ac97_ops
;
256 EXPORT_SYMBOL_GPL(soc_ac97_ops
);
258 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops
*ops
)
260 if (ops
== soc_ac97_ops
)
263 if (soc_ac97_ops
&& ops
)
267 soc_ac97_bus
.ops
= ops
;
271 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops
);
274 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
276 * This function sets the reset and warm_reset properties of ops and parses
277 * the device node of pdev to get pinctrl states and gpio numbers to use.
279 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops
*ops
,
280 struct platform_device
*pdev
)
282 struct device
*dev
= &pdev
->dev
;
283 struct snd_ac97_reset_cfg cfg
;
286 ret
= snd_soc_ac97_parse_pinctl(dev
, &cfg
);
290 ret
= snd_soc_set_ac97_ops(ops
);
294 ops
->warm_reset
= snd_soc_ac97_warm_reset
;
295 ops
->reset
= snd_soc_ac97_reset
;
297 snd_ac97_rst_cfg
= cfg
;
300 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset
);