xref: /linux/sound/soc/soc-ac97.c (revision 3b27d13940c3710a1128527c43719cb0bb05d73b)
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>
23 #include <linux/init.h>
24 #include <linux/of_gpio.h>
25 #include <linux/of.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/slab.h>
28 #include <sound/ac97_codec.h>
29 #include <sound/soc.h>
30 
31 struct snd_ac97_reset_cfg {
32 	struct pinctrl *pctl;
33 	struct pinctrl_state *pstate_reset;
34 	struct pinctrl_state *pstate_warm_reset;
35 	struct pinctrl_state *pstate_run;
36 	int gpio_sdata;
37 	int gpio_sync;
38 	int gpio_reset;
39 };
40 
41 static struct snd_ac97_bus soc_ac97_bus = {
42 	.ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */
43 };
44 
45 static void soc_ac97_device_release(struct device *dev)
46 {
47 	kfree(to_ac97_t(dev));
48 }
49 
50 /**
51  * snd_soc_alloc_ac97_codec() - Allocate new a AC'97 device
52  * @codec: The CODEC for which to create the AC'97 device
53  *
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.
57  *
58  * Returns: A snd_ac97 device or a PTR_ERR in case of an error.
59  */
60 struct snd_ac97 *snd_soc_alloc_ac97_codec(struct snd_soc_codec *codec)
61 {
62 	struct snd_ac97 *ac97;
63 
64 	ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
65 	if (ac97 == NULL)
66 		return ERR_PTR(-ENOMEM);
67 
68 	ac97->bus = &soc_ac97_bus;
69 	ac97->num = 0;
70 
71 	ac97->dev.bus = &ac97_bus_type;
72 	ac97->dev.parent = codec->component.card->dev;
73 	ac97->dev.release = soc_ac97_device_release;
74 
75 	dev_set_name(&ac97->dev, "%d-%d:%s",
76 		     codec->component.card->snd_card->number, 0,
77 		     codec->component.name);
78 
79 	device_initialize(&ac97->dev);
80 
81 	return ac97;
82 }
83 EXPORT_SYMBOL(snd_soc_alloc_ac97_codec);
84 
85 /**
86  * snd_soc_new_ac97_codec - initailise AC97 device
87  * @codec: audio codec
88  *
89  * Initialises AC97 codec resources for use by ad-hoc devices only.
90  */
91 struct snd_ac97 *snd_soc_new_ac97_codec(struct snd_soc_codec *codec)
92 {
93 	struct snd_ac97 *ac97;
94 	int ret;
95 
96 	ac97 = snd_soc_alloc_ac97_codec(codec);
97 	if (IS_ERR(ac97))
98 		return ac97;
99 
100 	ret = device_add(&ac97->dev);
101 	if (ret) {
102 		put_device(&ac97->dev);
103 		return ERR_PTR(ret);
104 	}
105 
106 	return ac97;
107 }
108 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
109 
110 /**
111  * snd_soc_free_ac97_codec - free AC97 codec device
112  * @codec: audio codec
113  *
114  * Frees AC97 codec device resources.
115  */
116 void snd_soc_free_ac97_codec(struct snd_ac97 *ac97)
117 {
118 	device_del(&ac97->dev);
119 	ac97->bus = NULL;
120 	put_device(&ac97->dev);
121 }
122 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
123 
124 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
125 
126 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
127 {
128 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
129 
130 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
131 
132 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
133 
134 	udelay(10);
135 
136 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
137 
138 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
139 	msleep(2);
140 }
141 
142 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
143 {
144 	struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
145 
146 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
147 
148 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
149 	gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
150 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
151 
152 	udelay(10);
153 
154 	gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
155 
156 	pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
157 	msleep(2);
158 }
159 
160 static int snd_soc_ac97_parse_pinctl(struct device *dev,
161 		struct snd_ac97_reset_cfg *cfg)
162 {
163 	struct pinctrl *p;
164 	struct pinctrl_state *state;
165 	int gpio;
166 	int ret;
167 
168 	p = devm_pinctrl_get(dev);
169 	if (IS_ERR(p)) {
170 		dev_err(dev, "Failed to get pinctrl\n");
171 		return PTR_ERR(p);
172 	}
173 	cfg->pctl = p;
174 
175 	state = pinctrl_lookup_state(p, "ac97-reset");
176 	if (IS_ERR(state)) {
177 		dev_err(dev, "Can't find pinctrl state ac97-reset\n");
178 		return PTR_ERR(state);
179 	}
180 	cfg->pstate_reset = state;
181 
182 	state = pinctrl_lookup_state(p, "ac97-warm-reset");
183 	if (IS_ERR(state)) {
184 		dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
185 		return PTR_ERR(state);
186 	}
187 	cfg->pstate_warm_reset = state;
188 
189 	state = pinctrl_lookup_state(p, "ac97-running");
190 	if (IS_ERR(state)) {
191 		dev_err(dev, "Can't find pinctrl state ac97-running\n");
192 		return PTR_ERR(state);
193 	}
194 	cfg->pstate_run = state;
195 
196 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
197 	if (gpio < 0) {
198 		dev_err(dev, "Can't find ac97-sync gpio\n");
199 		return gpio;
200 	}
201 	ret = devm_gpio_request(dev, gpio, "AC97 link sync");
202 	if (ret) {
203 		dev_err(dev, "Failed requesting ac97-sync gpio\n");
204 		return ret;
205 	}
206 	cfg->gpio_sync = gpio;
207 
208 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
209 	if (gpio < 0) {
210 		dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
211 		return gpio;
212 	}
213 	ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
214 	if (ret) {
215 		dev_err(dev, "Failed requesting ac97-sdata gpio\n");
216 		return ret;
217 	}
218 	cfg->gpio_sdata = gpio;
219 
220 	gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
221 	if (gpio < 0) {
222 		dev_err(dev, "Can't find ac97-reset gpio\n");
223 		return gpio;
224 	}
225 	ret = devm_gpio_request(dev, gpio, "AC97 link reset");
226 	if (ret) {
227 		dev_err(dev, "Failed requesting ac97-reset gpio\n");
228 		return ret;
229 	}
230 	cfg->gpio_reset = gpio;
231 
232 	return 0;
233 }
234 
235 struct snd_ac97_bus_ops *soc_ac97_ops;
236 EXPORT_SYMBOL_GPL(soc_ac97_ops);
237 
238 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
239 {
240 	if (ops == soc_ac97_ops)
241 		return 0;
242 
243 	if (soc_ac97_ops && ops)
244 		return -EBUSY;
245 
246 	soc_ac97_ops = ops;
247 	soc_ac97_bus.ops = ops;
248 
249 	return 0;
250 }
251 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
252 
253 /**
254  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
255  *
256  * This function sets the reset and warm_reset properties of ops and parses
257  * the device node of pdev to get pinctrl states and gpio numbers to use.
258  */
259 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
260 		struct platform_device *pdev)
261 {
262 	struct device *dev = &pdev->dev;
263 	struct snd_ac97_reset_cfg cfg;
264 	int ret;
265 
266 	ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
267 	if (ret)
268 		return ret;
269 
270 	ret = snd_soc_set_ac97_ops(ops);
271 	if (ret)
272 		return ret;
273 
274 	ops->warm_reset = snd_soc_ac97_warm_reset;
275 	ops->reset = snd_soc_ac97_reset;
276 
277 	snd_ac97_rst_cfg = cfg;
278 	return 0;
279 }
280 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
281