]>
Commit | Line | Data |
---|---|---|
336b8423 LPC |
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 | ||
eda1a701 LPC |
41 | static struct snd_ac97_bus soc_ac97_bus = { |
42 | .ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */ | |
43 | }; | |
44 | ||
336b8423 LPC |
45 | static void soc_ac97_device_release(struct device *dev) |
46 | { | |
47 | kfree(to_ac97_t(dev)); | |
48 | } | |
49 | ||
50 | /** | |
51 | * snd_soc_new_ac97_codec - initailise AC97 device | |
52 | * @codec: audio codec | |
336b8423 LPC |
53 | * |
54 | * Initialises AC97 codec resources for use by ad-hoc devices only. | |
55 | */ | |
eda1a701 | 56 | int snd_soc_new_ac97_codec(struct snd_soc_codec *codec) |
336b8423 | 57 | { |
6794f709 LPC |
58 | int ret; |
59 | ||
336b8423 LPC |
60 | codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); |
61 | if (codec->ac97 == NULL) | |
62 | return -ENOMEM; | |
63 | ||
eda1a701 LPC |
64 | codec->ac97->bus = &soc_ac97_bus; |
65 | codec->ac97->num = 0; | |
6794f709 LPC |
66 | |
67 | codec->ac97->dev.bus = &ac97_bus_type; | |
68 | codec->ac97->dev.parent = codec->component.card->dev; | |
336b8423 LPC |
69 | codec->ac97->dev.release = soc_ac97_device_release; |
70 | ||
6794f709 LPC |
71 | dev_set_name(&codec->ac97->dev, "%d-%d:%s", |
72 | codec->component.card->snd_card->number, 0, | |
73 | codec->component.name); | |
74 | ||
75 | ret = device_register(&codec->ac97->dev); | |
76 | if (ret) | |
77 | put_device(&codec->ac97->dev); | |
336b8423 | 78 | |
6794f709 | 79 | return ret; |
336b8423 LPC |
80 | } |
81 | EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); | |
82 | ||
83 | /** | |
84 | * snd_soc_free_ac97_codec - free AC97 codec device | |
85 | * @codec: audio codec | |
86 | * | |
87 | * Frees AC97 codec device resources. | |
88 | */ | |
89 | void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) | |
90 | { | |
6794f709 | 91 | device_del(&codec->ac97->dev); |
336b8423 LPC |
92 | codec->ac97->bus = NULL; |
93 | put_device(&codec->ac97->dev); | |
94 | codec->ac97 = NULL; | |
336b8423 LPC |
95 | } |
96 | EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); | |
97 | ||
98 | static struct snd_ac97_reset_cfg snd_ac97_rst_cfg; | |
99 | ||
100 | static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97) | |
101 | { | |
102 | struct pinctrl *pctl = snd_ac97_rst_cfg.pctl; | |
103 | ||
104 | pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset); | |
105 | ||
106 | gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1); | |
107 | ||
108 | udelay(10); | |
109 | ||
110 | gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0); | |
111 | ||
112 | pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run); | |
113 | msleep(2); | |
114 | } | |
115 | ||
116 | static void snd_soc_ac97_reset(struct snd_ac97 *ac97) | |
117 | { | |
118 | struct pinctrl *pctl = snd_ac97_rst_cfg.pctl; | |
119 | ||
120 | pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset); | |
121 | ||
122 | gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0); | |
123 | gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0); | |
124 | gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0); | |
125 | ||
126 | udelay(10); | |
127 | ||
128 | gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1); | |
129 | ||
130 | pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run); | |
131 | msleep(2); | |
132 | } | |
133 | ||
134 | static int snd_soc_ac97_parse_pinctl(struct device *dev, | |
135 | struct snd_ac97_reset_cfg *cfg) | |
136 | { | |
137 | struct pinctrl *p; | |
138 | struct pinctrl_state *state; | |
139 | int gpio; | |
140 | int ret; | |
141 | ||
142 | p = devm_pinctrl_get(dev); | |
143 | if (IS_ERR(p)) { | |
144 | dev_err(dev, "Failed to get pinctrl\n"); | |
145 | return PTR_ERR(p); | |
146 | } | |
147 | cfg->pctl = p; | |
148 | ||
149 | state = pinctrl_lookup_state(p, "ac97-reset"); | |
150 | if (IS_ERR(state)) { | |
151 | dev_err(dev, "Can't find pinctrl state ac97-reset\n"); | |
152 | return PTR_ERR(state); | |
153 | } | |
154 | cfg->pstate_reset = state; | |
155 | ||
156 | state = pinctrl_lookup_state(p, "ac97-warm-reset"); | |
157 | if (IS_ERR(state)) { | |
158 | dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n"); | |
159 | return PTR_ERR(state); | |
160 | } | |
161 | cfg->pstate_warm_reset = state; | |
162 | ||
163 | state = pinctrl_lookup_state(p, "ac97-running"); | |
164 | if (IS_ERR(state)) { | |
165 | dev_err(dev, "Can't find pinctrl state ac97-running\n"); | |
166 | return PTR_ERR(state); | |
167 | } | |
168 | cfg->pstate_run = state; | |
169 | ||
170 | gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0); | |
171 | if (gpio < 0) { | |
172 | dev_err(dev, "Can't find ac97-sync gpio\n"); | |
173 | return gpio; | |
174 | } | |
175 | ret = devm_gpio_request(dev, gpio, "AC97 link sync"); | |
176 | if (ret) { | |
177 | dev_err(dev, "Failed requesting ac97-sync gpio\n"); | |
178 | return ret; | |
179 | } | |
180 | cfg->gpio_sync = gpio; | |
181 | ||
182 | gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1); | |
183 | if (gpio < 0) { | |
184 | dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio); | |
185 | return gpio; | |
186 | } | |
187 | ret = devm_gpio_request(dev, gpio, "AC97 link sdata"); | |
188 | if (ret) { | |
189 | dev_err(dev, "Failed requesting ac97-sdata gpio\n"); | |
190 | return ret; | |
191 | } | |
192 | cfg->gpio_sdata = gpio; | |
193 | ||
194 | gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2); | |
195 | if (gpio < 0) { | |
196 | dev_err(dev, "Can't find ac97-reset gpio\n"); | |
197 | return gpio; | |
198 | } | |
199 | ret = devm_gpio_request(dev, gpio, "AC97 link reset"); | |
200 | if (ret) { | |
201 | dev_err(dev, "Failed requesting ac97-reset gpio\n"); | |
202 | return ret; | |
203 | } | |
204 | cfg->gpio_reset = gpio; | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
209 | struct snd_ac97_bus_ops *soc_ac97_ops; | |
210 | EXPORT_SYMBOL_GPL(soc_ac97_ops); | |
211 | ||
212 | int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops) | |
213 | { | |
214 | if (ops == soc_ac97_ops) | |
215 | return 0; | |
216 | ||
217 | if (soc_ac97_ops && ops) | |
218 | return -EBUSY; | |
219 | ||
220 | soc_ac97_ops = ops; | |
eda1a701 | 221 | soc_ac97_bus.ops = ops; |
336b8423 LPC |
222 | |
223 | return 0; | |
224 | } | |
225 | EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops); | |
226 | ||
227 | /** | |
228 | * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions | |
229 | * | |
230 | * This function sets the reset and warm_reset properties of ops and parses | |
231 | * the device node of pdev to get pinctrl states and gpio numbers to use. | |
232 | */ | |
233 | int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops, | |
234 | struct platform_device *pdev) | |
235 | { | |
236 | struct device *dev = &pdev->dev; | |
237 | struct snd_ac97_reset_cfg cfg; | |
238 | int ret; | |
239 | ||
240 | ret = snd_soc_ac97_parse_pinctl(dev, &cfg); | |
241 | if (ret) | |
242 | return ret; | |
243 | ||
244 | ret = snd_soc_set_ac97_ops(ops); | |
245 | if (ret) | |
246 | return ret; | |
247 | ||
248 | ops->warm_reset = snd_soc_ac97_warm_reset; | |
249 | ops->reset = snd_soc_ac97_reset; | |
250 | ||
251 | snd_ac97_rst_cfg = cfg; | |
252 | return 0; | |
253 | } | |
254 | EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset); |