Commit | Line | Data |
---|---|---|
db2a4165 FM |
1 | /* |
2 | * soc-core.c -- ALSA SoC Audio Layer | |
3 | * | |
4 | * Copyright 2005 Wolfson Microelectronics PLC. | |
0664d888 LG |
5 | * Copyright 2005 Openedhand Ltd. |
6 | * | |
d331124d | 7 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
0664d888 LG |
8 | * with code, comments and ideas from :- |
9 | * Richard Purdie <richard@openedhand.com> | |
db2a4165 FM |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU General Public License as published by the | |
13 | * Free Software Foundation; either version 2 of the License, or (at your | |
14 | * option) any later version. | |
15 | * | |
db2a4165 FM |
16 | * TODO: |
17 | * o Add hw rules to enforce rates, etc. | |
18 | * o More testing with other codecs/machines. | |
19 | * o Add more codecs and platforms to ensure good API coverage. | |
20 | * o Support TDM on PCM and I2S | |
21 | */ | |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/moduleparam.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/delay.h> | |
27 | #include <linux/pm.h> | |
28 | #include <linux/bitops.h> | |
29 | #include <linux/platform_device.h> | |
db2a4165 FM |
30 | #include <sound/core.h> |
31 | #include <sound/pcm.h> | |
32 | #include <sound/pcm_params.h> | |
33 | #include <sound/soc.h> | |
34 | #include <sound/soc-dapm.h> | |
35 | #include <sound/initval.h> | |
36 | ||
37 | /* debug */ | |
38 | #define SOC_DEBUG 0 | |
39 | #if SOC_DEBUG | |
40 | #define dbg(format, arg...) printk(format, ## arg) | |
41 | #else | |
42 | #define dbg(format, arg...) | |
43 | #endif | |
a71a468a | 44 | |
db2a4165 FM |
45 | static DEFINE_MUTEX(pcm_mutex); |
46 | static DEFINE_MUTEX(io_mutex); | |
db2a4165 FM |
47 | static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq); |
48 | ||
db2a4165 FM |
49 | /* |
50 | * This is a timeout to do a DAPM powerdown after a stream is closed(). | |
51 | * It can be used to eliminate pops between different playback streams, e.g. | |
52 | * between two audio tracks. | |
53 | */ | |
54 | static int pmdown_time = 5000; | |
55 | module_param(pmdown_time, int, 0); | |
56 | MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); | |
57 | ||
965ac42c LG |
58 | /* |
59 | * This function forces any delayed work to be queued and run. | |
60 | */ | |
61 | static int run_delayed_work(struct delayed_work *dwork) | |
62 | { | |
63 | int ret; | |
64 | ||
65 | /* cancel any work waiting to be queued. */ | |
66 | ret = cancel_delayed_work(dwork); | |
67 | ||
68 | /* if there was any work waiting then we run it now and | |
69 | * wait for it's completion */ | |
70 | if (ret) { | |
71 | schedule_delayed_work(dwork, 0); | |
72 | flush_scheduled_work(); | |
73 | } | |
74 | return ret; | |
75 | } | |
76 | ||
db2a4165 FM |
77 | #ifdef CONFIG_SND_SOC_AC97_BUS |
78 | /* unregister ac97 codec */ | |
79 | static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) | |
80 | { | |
81 | if (codec->ac97->dev.bus) | |
82 | device_unregister(&codec->ac97->dev); | |
83 | return 0; | |
84 | } | |
85 | ||
86 | /* stop no dev release warning */ | |
87 | static void soc_ac97_device_release(struct device *dev){} | |
88 | ||
89 | /* register ac97 codec to bus */ | |
90 | static int soc_ac97_dev_register(struct snd_soc_codec *codec) | |
91 | { | |
92 | int err; | |
93 | ||
94 | codec->ac97->dev.bus = &ac97_bus_type; | |
95 | codec->ac97->dev.parent = NULL; | |
96 | codec->ac97->dev.release = soc_ac97_device_release; | |
97 | ||
98 | snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s", | |
99 | codec->card->number, 0, codec->name); | |
100 | err = device_register(&codec->ac97->dev); | |
101 | if (err < 0) { | |
102 | snd_printk(KERN_ERR "Can't register ac97 bus\n"); | |
103 | codec->ac97->dev.bus = NULL; | |
104 | return err; | |
105 | } | |
106 | return 0; | |
107 | } | |
108 | #endif | |
109 | ||
3ff3f64b | 110 | static inline const char *get_dai_name(int type) |
db2a4165 | 111 | { |
3ff3f64b | 112 | switch (type) { |
a68660e0 | 113 | case SND_SOC_DAI_AC97_BUS: |
db2a4165 FM |
114 | case SND_SOC_DAI_AC97: |
115 | return "AC97"; | |
116 | case SND_SOC_DAI_I2S: | |
117 | return "I2S"; | |
118 | case SND_SOC_DAI_PCM: | |
119 | return "PCM"; | |
120 | } | |
121 | return NULL; | |
122 | } | |
123 | ||
db2a4165 FM |
124 | /* |
125 | * Called by ALSA when a PCM substream is opened, the runtime->hw record is | |
126 | * then initialized and any private data can be allocated. This also calls | |
127 | * startup for the cpu DAI, platform, machine and codec DAI. | |
128 | */ | |
129 | static int soc_pcm_open(struct snd_pcm_substream *substream) | |
130 | { | |
131 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
132 | struct snd_soc_device *socdev = rtd->socdev; | |
133 | struct snd_pcm_runtime *runtime = substream->runtime; | |
cb666e5b | 134 | struct snd_soc_dai_link *machine = rtd->dai; |
db2a4165 | 135 | struct snd_soc_platform *platform = socdev->platform; |
3c4b266f LG |
136 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
137 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
db2a4165 FM |
138 | int ret = 0; |
139 | ||
140 | mutex_lock(&pcm_mutex); | |
141 | ||
142 | /* startup the audio subsystem */ | |
cb666e5b LG |
143 | if (cpu_dai->ops.startup) { |
144 | ret = cpu_dai->ops.startup(substream); | |
db2a4165 FM |
145 | if (ret < 0) { |
146 | printk(KERN_ERR "asoc: can't open interface %s\n", | |
cb666e5b | 147 | cpu_dai->name); |
db2a4165 FM |
148 | goto out; |
149 | } | |
150 | } | |
151 | ||
152 | if (platform->pcm_ops->open) { | |
153 | ret = platform->pcm_ops->open(substream); | |
154 | if (ret < 0) { | |
155 | printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); | |
156 | goto platform_err; | |
157 | } | |
158 | } | |
159 | ||
cb666e5b LG |
160 | if (codec_dai->ops.startup) { |
161 | ret = codec_dai->ops.startup(substream); | |
db2a4165 | 162 | if (ret < 0) { |
cb666e5b LG |
163 | printk(KERN_ERR "asoc: can't open codec %s\n", |
164 | codec_dai->name); | |
165 | goto codec_dai_err; | |
db2a4165 FM |
166 | } |
167 | } | |
168 | ||
cb666e5b LG |
169 | if (machine->ops && machine->ops->startup) { |
170 | ret = machine->ops->startup(substream); | |
db2a4165 | 171 | if (ret < 0) { |
cb666e5b LG |
172 | printk(KERN_ERR "asoc: %s startup failed\n", machine->name); |
173 | goto machine_err; | |
db2a4165 FM |
174 | } |
175 | } | |
176 | ||
db2a4165 FM |
177 | /* Check that the codec and cpu DAI's are compatible */ |
178 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
179 | runtime->hw.rate_min = | |
3ff3f64b MB |
180 | max(codec_dai->playback.rate_min, |
181 | cpu_dai->playback.rate_min); | |
db2a4165 | 182 | runtime->hw.rate_max = |
3ff3f64b MB |
183 | min(codec_dai->playback.rate_max, |
184 | cpu_dai->playback.rate_max); | |
db2a4165 | 185 | runtime->hw.channels_min = |
cb666e5b LG |
186 | max(codec_dai->playback.channels_min, |
187 | cpu_dai->playback.channels_min); | |
db2a4165 | 188 | runtime->hw.channels_max = |
cb666e5b LG |
189 | min(codec_dai->playback.channels_max, |
190 | cpu_dai->playback.channels_max); | |
191 | runtime->hw.formats = | |
192 | codec_dai->playback.formats & cpu_dai->playback.formats; | |
193 | runtime->hw.rates = | |
194 | codec_dai->playback.rates & cpu_dai->playback.rates; | |
db2a4165 FM |
195 | } else { |
196 | runtime->hw.rate_min = | |
3ff3f64b MB |
197 | max(codec_dai->capture.rate_min, |
198 | cpu_dai->capture.rate_min); | |
db2a4165 | 199 | runtime->hw.rate_max = |
3ff3f64b MB |
200 | min(codec_dai->capture.rate_max, |
201 | cpu_dai->capture.rate_max); | |
db2a4165 | 202 | runtime->hw.channels_min = |
cb666e5b LG |
203 | max(codec_dai->capture.channels_min, |
204 | cpu_dai->capture.channels_min); | |
db2a4165 | 205 | runtime->hw.channels_max = |
cb666e5b LG |
206 | min(codec_dai->capture.channels_max, |
207 | cpu_dai->capture.channels_max); | |
208 | runtime->hw.formats = | |
209 | codec_dai->capture.formats & cpu_dai->capture.formats; | |
210 | runtime->hw.rates = | |
211 | codec_dai->capture.rates & cpu_dai->capture.rates; | |
db2a4165 FM |
212 | } |
213 | ||
214 | snd_pcm_limit_hw_rates(runtime); | |
215 | if (!runtime->hw.rates) { | |
216 | printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", | |
cb666e5b LG |
217 | codec_dai->name, cpu_dai->name); |
218 | goto machine_err; | |
db2a4165 FM |
219 | } |
220 | if (!runtime->hw.formats) { | |
221 | printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", | |
cb666e5b LG |
222 | codec_dai->name, cpu_dai->name); |
223 | goto machine_err; | |
db2a4165 FM |
224 | } |
225 | if (!runtime->hw.channels_min || !runtime->hw.channels_max) { | |
226 | printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", | |
cb666e5b LG |
227 | codec_dai->name, cpu_dai->name); |
228 | goto machine_err; | |
db2a4165 FM |
229 | } |
230 | ||
3ff3f64b | 231 | dbg("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name); |
b5c5fd24 LG |
232 | dbg("asoc: rate mask 0x%x\n", runtime->hw.rates); |
233 | dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, | |
234 | runtime->hw.channels_max); | |
235 | dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, | |
236 | runtime->hw.rate_max); | |
db2a4165 | 237 | |
db2a4165 | 238 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
cb666e5b | 239 | cpu_dai->playback.active = codec_dai->playback.active = 1; |
db2a4165 | 240 | else |
cb666e5b LG |
241 | cpu_dai->capture.active = codec_dai->capture.active = 1; |
242 | cpu_dai->active = codec_dai->active = 1; | |
243 | cpu_dai->runtime = runtime; | |
db2a4165 FM |
244 | socdev->codec->active++; |
245 | mutex_unlock(&pcm_mutex); | |
246 | return 0; | |
247 | ||
cb666e5b | 248 | machine_err: |
db2a4165 FM |
249 | if (machine->ops && machine->ops->shutdown) |
250 | machine->ops->shutdown(substream); | |
251 | ||
cb666e5b | 252 | codec_dai_err: |
db2a4165 FM |
253 | if (platform->pcm_ops->close) |
254 | platform->pcm_ops->close(substream); | |
255 | ||
256 | platform_err: | |
cb666e5b LG |
257 | if (cpu_dai->ops.shutdown) |
258 | cpu_dai->ops.shutdown(substream); | |
db2a4165 FM |
259 | out: |
260 | mutex_unlock(&pcm_mutex); | |
261 | return ret; | |
262 | } | |
263 | ||
264 | /* | |
3a4fa0a2 | 265 | * Power down the audio subsystem pmdown_time msecs after close is called. |
db2a4165 FM |
266 | * This is to ensure there are no pops or clicks in between any music tracks |
267 | * due to DAPM power cycling. | |
268 | */ | |
4484bb2e | 269 | static void close_delayed_work(struct work_struct *work) |
db2a4165 | 270 | { |
4484bb2e AM |
271 | struct snd_soc_device *socdev = |
272 | container_of(work, struct snd_soc_device, delayed_work.work); | |
db2a4165 | 273 | struct snd_soc_codec *codec = socdev->codec; |
3c4b266f | 274 | struct snd_soc_dai *codec_dai; |
db2a4165 FM |
275 | int i; |
276 | ||
277 | mutex_lock(&pcm_mutex); | |
3ff3f64b | 278 | for (i = 0; i < codec->num_dai; i++) { |
db2a4165 FM |
279 | codec_dai = &codec->dai[i]; |
280 | ||
281 | dbg("pop wq checking: %s status: %s waiting: %s\n", | |
282 | codec_dai->playback.stream_name, | |
283 | codec_dai->playback.active ? "active" : "inactive", | |
284 | codec_dai->pop_wait ? "yes" : "no"); | |
285 | ||
286 | /* are we waiting on this codec DAI stream */ | |
287 | if (codec_dai->pop_wait == 1) { | |
288 | ||
0be9898a | 289 | /* Reduce power if no longer active */ |
3c1c47e0 LG |
290 | if (codec->active == 0) { |
291 | dbg("pop wq D1 %s %s\n", codec->name, | |
292 | codec_dai->playback.stream_name); | |
0be9898a MB |
293 | snd_soc_dapm_set_bias_level(socdev, |
294 | SND_SOC_BIAS_PREPARE); | |
3c1c47e0 LG |
295 | } |
296 | ||
db2a4165 | 297 | codec_dai->pop_wait = 0; |
0b4d221b LG |
298 | snd_soc_dapm_stream_event(codec, |
299 | codec_dai->playback.stream_name, | |
db2a4165 FM |
300 | SND_SOC_DAPM_STREAM_STOP); |
301 | ||
0be9898a | 302 | /* Fall into standby if no longer active */ |
db2a4165 FM |
303 | if (codec->active == 0) { |
304 | dbg("pop wq D3 %s %s\n", codec->name, | |
305 | codec_dai->playback.stream_name); | |
0be9898a MB |
306 | snd_soc_dapm_set_bias_level(socdev, |
307 | SND_SOC_BIAS_STANDBY); | |
db2a4165 FM |
308 | } |
309 | } | |
310 | } | |
311 | mutex_unlock(&pcm_mutex); | |
312 | } | |
313 | ||
314 | /* | |
315 | * Called by ALSA when a PCM substream is closed. Private data can be | |
316 | * freed here. The cpu DAI, codec DAI, machine and platform are also | |
317 | * shutdown. | |
318 | */ | |
319 | static int soc_codec_close(struct snd_pcm_substream *substream) | |
320 | { | |
321 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
322 | struct snd_soc_device *socdev = rtd->socdev; | |
cb666e5b | 323 | struct snd_soc_dai_link *machine = rtd->dai; |
db2a4165 | 324 | struct snd_soc_platform *platform = socdev->platform; |
3c4b266f LG |
325 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
326 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
db2a4165 FM |
327 | struct snd_soc_codec *codec = socdev->codec; |
328 | ||
329 | mutex_lock(&pcm_mutex); | |
330 | ||
331 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
cb666e5b | 332 | cpu_dai->playback.active = codec_dai->playback.active = 0; |
db2a4165 | 333 | else |
cb666e5b | 334 | cpu_dai->capture.active = codec_dai->capture.active = 0; |
db2a4165 | 335 | |
cb666e5b LG |
336 | if (codec_dai->playback.active == 0 && |
337 | codec_dai->capture.active == 0) { | |
338 | cpu_dai->active = codec_dai->active = 0; | |
db2a4165 FM |
339 | } |
340 | codec->active--; | |
341 | ||
6010b2da MB |
342 | /* Muting the DAC suppresses artifacts caused during digital |
343 | * shutdown, for example from stopping clocks. | |
344 | */ | |
345 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
346 | snd_soc_dai_digital_mute(codec_dai, 1); | |
347 | ||
cb666e5b LG |
348 | if (cpu_dai->ops.shutdown) |
349 | cpu_dai->ops.shutdown(substream); | |
db2a4165 | 350 | |
cb666e5b LG |
351 | if (codec_dai->ops.shutdown) |
352 | codec_dai->ops.shutdown(substream); | |
db2a4165 FM |
353 | |
354 | if (machine->ops && machine->ops->shutdown) | |
355 | machine->ops->shutdown(substream); | |
356 | ||
357 | if (platform->pcm_ops->close) | |
358 | platform->pcm_ops->close(substream); | |
cb666e5b | 359 | cpu_dai->runtime = NULL; |
db2a4165 FM |
360 | |
361 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
362 | /* start delayed pop wq here for playback streams */ | |
cb666e5b | 363 | codec_dai->pop_wait = 1; |
4bb09523 | 364 | schedule_delayed_work(&socdev->delayed_work, |
db2a4165 FM |
365 | msecs_to_jiffies(pmdown_time)); |
366 | } else { | |
367 | /* capture streams can be powered down now */ | |
cb666e5b | 368 | snd_soc_dapm_stream_event(codec, |
0b4d221b LG |
369 | codec_dai->capture.stream_name, |
370 | SND_SOC_DAPM_STREAM_STOP); | |
db2a4165 | 371 | |
0b4d221b | 372 | if (codec->active == 0 && codec_dai->pop_wait == 0) |
0be9898a MB |
373 | snd_soc_dapm_set_bias_level(socdev, |
374 | SND_SOC_BIAS_STANDBY); | |
db2a4165 FM |
375 | } |
376 | ||
377 | mutex_unlock(&pcm_mutex); | |
378 | return 0; | |
379 | } | |
380 | ||
381 | /* | |
382 | * Called by ALSA when the PCM substream is prepared, can set format, sample | |
383 | * rate, etc. This function is non atomic and can be called multiple times, | |
384 | * it can refer to the runtime info. | |
385 | */ | |
386 | static int soc_pcm_prepare(struct snd_pcm_substream *substream) | |
387 | { | |
388 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
389 | struct snd_soc_device *socdev = rtd->socdev; | |
cb666e5b | 390 | struct snd_soc_dai_link *machine = rtd->dai; |
db2a4165 | 391 | struct snd_soc_platform *platform = socdev->platform; |
3c4b266f LG |
392 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
393 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
db2a4165 FM |
394 | struct snd_soc_codec *codec = socdev->codec; |
395 | int ret = 0; | |
396 | ||
397 | mutex_lock(&pcm_mutex); | |
cb666e5b LG |
398 | |
399 | if (machine->ops && machine->ops->prepare) { | |
400 | ret = machine->ops->prepare(substream); | |
401 | if (ret < 0) { | |
402 | printk(KERN_ERR "asoc: machine prepare error\n"); | |
403 | goto out; | |
404 | } | |
405 | } | |
406 | ||
db2a4165 FM |
407 | if (platform->pcm_ops->prepare) { |
408 | ret = platform->pcm_ops->prepare(substream); | |
a71a468a LG |
409 | if (ret < 0) { |
410 | printk(KERN_ERR "asoc: platform prepare error\n"); | |
db2a4165 | 411 | goto out; |
a71a468a | 412 | } |
db2a4165 FM |
413 | } |
414 | ||
cb666e5b LG |
415 | if (codec_dai->ops.prepare) { |
416 | ret = codec_dai->ops.prepare(substream); | |
a71a468a LG |
417 | if (ret < 0) { |
418 | printk(KERN_ERR "asoc: codec DAI prepare error\n"); | |
db2a4165 | 419 | goto out; |
a71a468a | 420 | } |
db2a4165 FM |
421 | } |
422 | ||
cb666e5b LG |
423 | if (cpu_dai->ops.prepare) { |
424 | ret = cpu_dai->ops.prepare(substream); | |
425 | if (ret < 0) { | |
426 | printk(KERN_ERR "asoc: cpu DAI prepare error\n"); | |
427 | goto out; | |
428 | } | |
429 | } | |
db2a4165 FM |
430 | |
431 | /* we only want to start a DAPM playback stream if we are not waiting | |
432 | * on an existing one stopping */ | |
cb666e5b | 433 | if (codec_dai->pop_wait) { |
db2a4165 FM |
434 | /* we are waiting for the delayed work to start */ |
435 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) | |
cb666e5b LG |
436 | snd_soc_dapm_stream_event(socdev->codec, |
437 | codec_dai->capture.stream_name, | |
db2a4165 FM |
438 | SND_SOC_DAPM_STREAM_START); |
439 | else { | |
cb666e5b | 440 | codec_dai->pop_wait = 0; |
4484bb2e | 441 | cancel_delayed_work(&socdev->delayed_work); |
8c6529db | 442 | snd_soc_dai_digital_mute(codec_dai, 0); |
db2a4165 FM |
443 | } |
444 | } else { | |
445 | /* no delayed work - do we need to power up codec */ | |
0be9898a | 446 | if (codec->bias_level != SND_SOC_BIAS_ON) { |
db2a4165 | 447 | |
0be9898a MB |
448 | snd_soc_dapm_set_bias_level(socdev, |
449 | SND_SOC_BIAS_PREPARE); | |
db2a4165 FM |
450 | |
451 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
452 | snd_soc_dapm_stream_event(codec, | |
cb666e5b | 453 | codec_dai->playback.stream_name, |
db2a4165 FM |
454 | SND_SOC_DAPM_STREAM_START); |
455 | else | |
456 | snd_soc_dapm_stream_event(codec, | |
cb666e5b | 457 | codec_dai->capture.stream_name, |
db2a4165 FM |
458 | SND_SOC_DAPM_STREAM_START); |
459 | ||
0be9898a | 460 | snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON); |
8c6529db | 461 | snd_soc_dai_digital_mute(codec_dai, 0); |
db2a4165 FM |
462 | |
463 | } else { | |
464 | /* codec already powered - power on widgets */ | |
465 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
466 | snd_soc_dapm_stream_event(codec, | |
cb666e5b | 467 | codec_dai->playback.stream_name, |
db2a4165 FM |
468 | SND_SOC_DAPM_STREAM_START); |
469 | else | |
470 | snd_soc_dapm_stream_event(codec, | |
cb666e5b | 471 | codec_dai->capture.stream_name, |
db2a4165 | 472 | SND_SOC_DAPM_STREAM_START); |
8c6529db LG |
473 | |
474 | snd_soc_dai_digital_mute(codec_dai, 0); | |
db2a4165 FM |
475 | } |
476 | } | |
477 | ||
478 | out: | |
479 | mutex_unlock(&pcm_mutex); | |
480 | return ret; | |
481 | } | |
482 | ||
483 | /* | |
484 | * Called by ALSA when the hardware params are set by application. This | |
485 | * function can also be called multiple times and can allocate buffers | |
486 | * (using snd_pcm_lib_* ). It's non-atomic. | |
487 | */ | |
488 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, | |
489 | struct snd_pcm_hw_params *params) | |
490 | { | |
491 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
492 | struct snd_soc_device *socdev = rtd->socdev; | |
cb666e5b | 493 | struct snd_soc_dai_link *machine = rtd->dai; |
db2a4165 | 494 | struct snd_soc_platform *platform = socdev->platform; |
3c4b266f LG |
495 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
496 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
db2a4165 FM |
497 | int ret = 0; |
498 | ||
499 | mutex_lock(&pcm_mutex); | |
500 | ||
cb666e5b LG |
501 | if (machine->ops && machine->ops->hw_params) { |
502 | ret = machine->ops->hw_params(substream, params); | |
503 | if (ret < 0) { | |
504 | printk(KERN_ERR "asoc: machine hw_params failed\n"); | |
db2a4165 | 505 | goto out; |
cb666e5b | 506 | } |
db2a4165 FM |
507 | } |
508 | ||
cb666e5b LG |
509 | if (codec_dai->ops.hw_params) { |
510 | ret = codec_dai->ops.hw_params(substream, params); | |
db2a4165 FM |
511 | if (ret < 0) { |
512 | printk(KERN_ERR "asoc: can't set codec %s hw params\n", | |
cb666e5b LG |
513 | codec_dai->name); |
514 | goto codec_err; | |
db2a4165 FM |
515 | } |
516 | } | |
517 | ||
cb666e5b LG |
518 | if (cpu_dai->ops.hw_params) { |
519 | ret = cpu_dai->ops.hw_params(substream, params); | |
db2a4165 | 520 | if (ret < 0) { |
3ff3f64b | 521 | printk(KERN_ERR "asoc: interface %s hw params failed\n", |
cb666e5b | 522 | cpu_dai->name); |
db2a4165 FM |
523 | goto interface_err; |
524 | } | |
525 | } | |
526 | ||
527 | if (platform->pcm_ops->hw_params) { | |
528 | ret = platform->pcm_ops->hw_params(substream, params); | |
529 | if (ret < 0) { | |
3ff3f64b | 530 | printk(KERN_ERR "asoc: platform %s hw params failed\n", |
db2a4165 FM |
531 | platform->name); |
532 | goto platform_err; | |
533 | } | |
534 | } | |
535 | ||
db2a4165 FM |
536 | out: |
537 | mutex_unlock(&pcm_mutex); | |
538 | return ret; | |
539 | ||
db2a4165 | 540 | platform_err: |
cb666e5b LG |
541 | if (cpu_dai->ops.hw_free) |
542 | cpu_dai->ops.hw_free(substream); | |
db2a4165 FM |
543 | |
544 | interface_err: | |
cb666e5b LG |
545 | if (codec_dai->ops.hw_free) |
546 | codec_dai->ops.hw_free(substream); | |
547 | ||
548 | codec_err: | |
3ff3f64b | 549 | if (machine->ops && machine->ops->hw_free) |
cb666e5b | 550 | machine->ops->hw_free(substream); |
db2a4165 FM |
551 | |
552 | mutex_unlock(&pcm_mutex); | |
553 | return ret; | |
554 | } | |
555 | ||
556 | /* | |
557 | * Free's resources allocated by hw_params, can be called multiple times | |
558 | */ | |
559 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) | |
560 | { | |
561 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
562 | struct snd_soc_device *socdev = rtd->socdev; | |
cb666e5b | 563 | struct snd_soc_dai_link *machine = rtd->dai; |
db2a4165 | 564 | struct snd_soc_platform *platform = socdev->platform; |
3c4b266f LG |
565 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
566 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
db2a4165 | 567 | struct snd_soc_codec *codec = socdev->codec; |
db2a4165 FM |
568 | |
569 | mutex_lock(&pcm_mutex); | |
570 | ||
571 | /* apply codec digital mute */ | |
8c6529db LG |
572 | if (!codec->active) |
573 | snd_soc_dai_digital_mute(codec_dai, 1); | |
db2a4165 FM |
574 | |
575 | /* free any machine hw params */ | |
576 | if (machine->ops && machine->ops->hw_free) | |
577 | machine->ops->hw_free(substream); | |
578 | ||
579 | /* free any DMA resources */ | |
580 | if (platform->pcm_ops->hw_free) | |
581 | platform->pcm_ops->hw_free(substream); | |
582 | ||
583 | /* now free hw params for the DAI's */ | |
cb666e5b LG |
584 | if (codec_dai->ops.hw_free) |
585 | codec_dai->ops.hw_free(substream); | |
db2a4165 | 586 | |
cb666e5b LG |
587 | if (cpu_dai->ops.hw_free) |
588 | cpu_dai->ops.hw_free(substream); | |
db2a4165 FM |
589 | |
590 | mutex_unlock(&pcm_mutex); | |
591 | return 0; | |
592 | } | |
593 | ||
594 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
595 | { | |
596 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
597 | struct snd_soc_device *socdev = rtd->socdev; | |
cb666e5b | 598 | struct snd_soc_dai_link *machine = rtd->dai; |
db2a4165 | 599 | struct snd_soc_platform *platform = socdev->platform; |
3c4b266f LG |
600 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
601 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
db2a4165 FM |
602 | int ret; |
603 | ||
cb666e5b LG |
604 | if (codec_dai->ops.trigger) { |
605 | ret = codec_dai->ops.trigger(substream, cmd); | |
db2a4165 FM |
606 | if (ret < 0) |
607 | return ret; | |
608 | } | |
609 | ||
610 | if (platform->pcm_ops->trigger) { | |
611 | ret = platform->pcm_ops->trigger(substream, cmd); | |
612 | if (ret < 0) | |
613 | return ret; | |
614 | } | |
615 | ||
cb666e5b LG |
616 | if (cpu_dai->ops.trigger) { |
617 | ret = cpu_dai->ops.trigger(substream, cmd); | |
db2a4165 FM |
618 | if (ret < 0) |
619 | return ret; | |
620 | } | |
621 | return 0; | |
622 | } | |
623 | ||
624 | /* ASoC PCM operations */ | |
625 | static struct snd_pcm_ops soc_pcm_ops = { | |
626 | .open = soc_pcm_open, | |
627 | .close = soc_codec_close, | |
628 | .hw_params = soc_pcm_hw_params, | |
629 | .hw_free = soc_pcm_hw_free, | |
630 | .prepare = soc_pcm_prepare, | |
631 | .trigger = soc_pcm_trigger, | |
632 | }; | |
633 | ||
634 | #ifdef CONFIG_PM | |
635 | /* powers down audio subsystem for suspend */ | |
636 | static int soc_suspend(struct platform_device *pdev, pm_message_t state) | |
637 | { | |
3ff3f64b MB |
638 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
639 | struct snd_soc_machine *machine = socdev->machine; | |
640 | struct snd_soc_platform *platform = socdev->platform; | |
641 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; | |
db2a4165 FM |
642 | struct snd_soc_codec *codec = socdev->codec; |
643 | int i; | |
644 | ||
6ed25978 AG |
645 | /* Due to the resume being scheduled into a workqueue we could |
646 | * suspend before that's finished - wait for it to complete. | |
647 | */ | |
648 | snd_power_lock(codec->card); | |
649 | snd_power_wait(codec->card, SNDRV_CTL_POWER_D0); | |
650 | snd_power_unlock(codec->card); | |
651 | ||
652 | /* we're going to block userspace touching us until resume completes */ | |
653 | snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot); | |
654 | ||
db2a4165 | 655 | /* mute any active DAC's */ |
3ff3f64b | 656 | for (i = 0; i < machine->num_links; i++) { |
3c4b266f | 657 | struct snd_soc_dai *dai = machine->dai_link[i].codec_dai; |
cb666e5b LG |
658 | if (dai->dai_ops.digital_mute && dai->playback.active) |
659 | dai->dai_ops.digital_mute(dai, 1); | |
db2a4165 FM |
660 | } |
661 | ||
4ccab3e7 LG |
662 | /* suspend all pcms */ |
663 | for (i = 0; i < machine->num_links; i++) | |
664 | snd_pcm_suspend_all(machine->dai_link[i].pcm); | |
665 | ||
db2a4165 FM |
666 | if (machine->suspend_pre) |
667 | machine->suspend_pre(pdev, state); | |
668 | ||
3ff3f64b | 669 | for (i = 0; i < machine->num_links; i++) { |
3c4b266f | 670 | struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
db2a4165 FM |
671 | if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97) |
672 | cpu_dai->suspend(pdev, cpu_dai); | |
673 | if (platform->suspend) | |
674 | platform->suspend(pdev, cpu_dai); | |
675 | } | |
676 | ||
677 | /* close any waiting streams and save state */ | |
965ac42c | 678 | run_delayed_work(&socdev->delayed_work); |
0be9898a | 679 | codec->suspend_bias_level = codec->bias_level; |
db2a4165 | 680 | |
3ff3f64b | 681 | for (i = 0; i < codec->num_dai; i++) { |
db2a4165 FM |
682 | char *stream = codec->dai[i].playback.stream_name; |
683 | if (stream != NULL) | |
684 | snd_soc_dapm_stream_event(codec, stream, | |
685 | SND_SOC_DAPM_STREAM_SUSPEND); | |
686 | stream = codec->dai[i].capture.stream_name; | |
687 | if (stream != NULL) | |
688 | snd_soc_dapm_stream_event(codec, stream, | |
689 | SND_SOC_DAPM_STREAM_SUSPEND); | |
690 | } | |
691 | ||
692 | if (codec_dev->suspend) | |
693 | codec_dev->suspend(pdev, state); | |
694 | ||
3ff3f64b | 695 | for (i = 0; i < machine->num_links; i++) { |
3c4b266f | 696 | struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
db2a4165 FM |
697 | if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97) |
698 | cpu_dai->suspend(pdev, cpu_dai); | |
699 | } | |
700 | ||
701 | if (machine->suspend_post) | |
702 | machine->suspend_post(pdev, state); | |
703 | ||
704 | return 0; | |
705 | } | |
706 | ||
6ed25978 AG |
707 | /* deferred resume work, so resume can complete before we finished |
708 | * setting our codec back up, which can be very slow on I2C | |
709 | */ | |
710 | static void soc_resume_deferred(struct work_struct *work) | |
db2a4165 | 711 | { |
6ed25978 AG |
712 | struct snd_soc_device *socdev = container_of(work, |
713 | struct snd_soc_device, | |
714 | deferred_resume_work); | |
3ff3f64b MB |
715 | struct snd_soc_machine *machine = socdev->machine; |
716 | struct snd_soc_platform *platform = socdev->platform; | |
717 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; | |
db2a4165 | 718 | struct snd_soc_codec *codec = socdev->codec; |
6ed25978 | 719 | struct platform_device *pdev = to_platform_device(socdev->dev); |
db2a4165 FM |
720 | int i; |
721 | ||
6ed25978 AG |
722 | /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, |
723 | * so userspace apps are blocked from touching us | |
724 | */ | |
725 | ||
726 | dev_info(socdev->dev, "starting resume work\n"); | |
727 | ||
db2a4165 FM |
728 | if (machine->resume_pre) |
729 | machine->resume_pre(pdev); | |
730 | ||
3ff3f64b | 731 | for (i = 0; i < machine->num_links; i++) { |
3c4b266f | 732 | struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
db2a4165 FM |
733 | if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97) |
734 | cpu_dai->resume(pdev, cpu_dai); | |
735 | } | |
736 | ||
737 | if (codec_dev->resume) | |
738 | codec_dev->resume(pdev); | |
739 | ||
3ff3f64b MB |
740 | for (i = 0; i < codec->num_dai; i++) { |
741 | char *stream = codec->dai[i].playback.stream_name; | |
db2a4165 FM |
742 | if (stream != NULL) |
743 | snd_soc_dapm_stream_event(codec, stream, | |
744 | SND_SOC_DAPM_STREAM_RESUME); | |
745 | stream = codec->dai[i].capture.stream_name; | |
746 | if (stream != NULL) | |
747 | snd_soc_dapm_stream_event(codec, stream, | |
748 | SND_SOC_DAPM_STREAM_RESUME); | |
749 | } | |
750 | ||
3ff3f64b MB |
751 | /* unmute any active DACs */ |
752 | for (i = 0; i < machine->num_links; i++) { | |
3c4b266f | 753 | struct snd_soc_dai *dai = machine->dai_link[i].codec_dai; |
cb666e5b LG |
754 | if (dai->dai_ops.digital_mute && dai->playback.active) |
755 | dai->dai_ops.digital_mute(dai, 0); | |
db2a4165 FM |
756 | } |
757 | ||
3ff3f64b | 758 | for (i = 0; i < machine->num_links; i++) { |
3c4b266f | 759 | struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
db2a4165 FM |
760 | if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97) |
761 | cpu_dai->resume(pdev, cpu_dai); | |
762 | if (platform->resume) | |
763 | platform->resume(pdev, cpu_dai); | |
764 | } | |
765 | ||
766 | if (machine->resume_post) | |
767 | machine->resume_post(pdev); | |
768 | ||
6ed25978 AG |
769 | dev_info(socdev->dev, "resume work completed\n"); |
770 | ||
771 | /* userspace can access us now we are back as we were before */ | |
772 | snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0); | |
773 | } | |
774 | ||
775 | /* powers up audio subsystem after a suspend */ | |
776 | static int soc_resume(struct platform_device *pdev) | |
777 | { | |
778 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
779 | ||
780 | dev_info(socdev->dev, "scheduling resume work\n"); | |
781 | ||
782 | if (!schedule_work(&socdev->deferred_resume_work)) | |
783 | dev_err(socdev->dev, "work item may be lost\n"); | |
784 | ||
db2a4165 FM |
785 | return 0; |
786 | } | |
787 | ||
788 | #else | |
789 | #define soc_suspend NULL | |
790 | #define soc_resume NULL | |
791 | #endif | |
792 | ||
793 | /* probes a new socdev */ | |
794 | static int soc_probe(struct platform_device *pdev) | |
795 | { | |
796 | int ret = 0, i; | |
797 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
798 | struct snd_soc_machine *machine = socdev->machine; | |
799 | struct snd_soc_platform *platform = socdev->platform; | |
800 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; | |
801 | ||
802 | if (machine->probe) { | |
803 | ret = machine->probe(pdev); | |
3ff3f64b | 804 | if (ret < 0) |
db2a4165 FM |
805 | return ret; |
806 | } | |
807 | ||
808 | for (i = 0; i < machine->num_links; i++) { | |
3c4b266f | 809 | struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
db2a4165 | 810 | if (cpu_dai->probe) { |
bdb92876 | 811 | ret = cpu_dai->probe(pdev, cpu_dai); |
3ff3f64b | 812 | if (ret < 0) |
db2a4165 FM |
813 | goto cpu_dai_err; |
814 | } | |
815 | } | |
816 | ||
817 | if (codec_dev->probe) { | |
818 | ret = codec_dev->probe(pdev); | |
3ff3f64b | 819 | if (ret < 0) |
db2a4165 FM |
820 | goto cpu_dai_err; |
821 | } | |
822 | ||
823 | if (platform->probe) { | |
824 | ret = platform->probe(pdev); | |
3ff3f64b | 825 | if (ret < 0) |
db2a4165 FM |
826 | goto platform_err; |
827 | } | |
828 | ||
829 | /* DAPM stream work */ | |
4484bb2e | 830 | INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work); |
1301a964 | 831 | #ifdef CONFIG_PM |
6ed25978 AG |
832 | /* deferred resume work */ |
833 | INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred); | |
1301a964 | 834 | #endif |
6ed25978 | 835 | |
db2a4165 FM |
836 | return 0; |
837 | ||
db2a4165 FM |
838 | platform_err: |
839 | if (codec_dev->remove) | |
840 | codec_dev->remove(pdev); | |
841 | ||
842 | cpu_dai_err: | |
18b9b3d9 | 843 | for (i--; i >= 0; i--) { |
3c4b266f | 844 | struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
db2a4165 | 845 | if (cpu_dai->remove) |
bdb92876 | 846 | cpu_dai->remove(pdev, cpu_dai); |
db2a4165 FM |
847 | } |
848 | ||
849 | if (machine->remove) | |
850 | machine->remove(pdev); | |
851 | ||
852 | return ret; | |
853 | } | |
854 | ||
855 | /* removes a socdev */ | |
856 | static int soc_remove(struct platform_device *pdev) | |
857 | { | |
858 | int i; | |
859 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
860 | struct snd_soc_machine *machine = socdev->machine; | |
861 | struct snd_soc_platform *platform = socdev->platform; | |
862 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; | |
863 | ||
965ac42c LG |
864 | run_delayed_work(&socdev->delayed_work); |
865 | ||
db2a4165 FM |
866 | if (platform->remove) |
867 | platform->remove(pdev); | |
868 | ||
869 | if (codec_dev->remove) | |
870 | codec_dev->remove(pdev); | |
871 | ||
872 | for (i = 0; i < machine->num_links; i++) { | |
3c4b266f | 873 | struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
db2a4165 | 874 | if (cpu_dai->remove) |
bdb92876 | 875 | cpu_dai->remove(pdev, cpu_dai); |
db2a4165 FM |
876 | } |
877 | ||
878 | if (machine->remove) | |
879 | machine->remove(pdev); | |
880 | ||
881 | return 0; | |
882 | } | |
883 | ||
884 | /* ASoC platform driver */ | |
885 | static struct platform_driver soc_driver = { | |
886 | .driver = { | |
887 | .name = "soc-audio", | |
8b45a209 | 888 | .owner = THIS_MODULE, |
db2a4165 FM |
889 | }, |
890 | .probe = soc_probe, | |
891 | .remove = soc_remove, | |
892 | .suspend = soc_suspend, | |
893 | .resume = soc_resume, | |
894 | }; | |
895 | ||
896 | /* create a new pcm */ | |
897 | static int soc_new_pcm(struct snd_soc_device *socdev, | |
898 | struct snd_soc_dai_link *dai_link, int num) | |
899 | { | |
900 | struct snd_soc_codec *codec = socdev->codec; | |
3c4b266f LG |
901 | struct snd_soc_dai *codec_dai = dai_link->codec_dai; |
902 | struct snd_soc_dai *cpu_dai = dai_link->cpu_dai; | |
db2a4165 FM |
903 | struct snd_soc_pcm_runtime *rtd; |
904 | struct snd_pcm *pcm; | |
905 | char new_name[64]; | |
906 | int ret = 0, playback = 0, capture = 0; | |
907 | ||
908 | rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL); | |
909 | if (rtd == NULL) | |
910 | return -ENOMEM; | |
cb666e5b LG |
911 | |
912 | rtd->dai = dai_link; | |
db2a4165 | 913 | rtd->socdev = socdev; |
cb666e5b | 914 | codec_dai->codec = socdev->codec; |
db2a4165 FM |
915 | |
916 | /* check client and interface hw capabilities */ | |
3ff3f64b | 917 | sprintf(new_name, "%s %s-%s-%d", dai_link->stream_name, codec_dai->name, |
db2a4165 FM |
918 | get_dai_name(cpu_dai->type), num); |
919 | ||
920 | if (codec_dai->playback.channels_min) | |
921 | playback = 1; | |
922 | if (codec_dai->capture.channels_min) | |
923 | capture = 1; | |
924 | ||
925 | ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback, | |
926 | capture, &pcm); | |
927 | if (ret < 0) { | |
3ff3f64b MB |
928 | printk(KERN_ERR "asoc: can't create pcm for codec %s\n", |
929 | codec->name); | |
db2a4165 FM |
930 | kfree(rtd); |
931 | return ret; | |
932 | } | |
933 | ||
4ccab3e7 | 934 | dai_link->pcm = pcm; |
db2a4165 FM |
935 | pcm->private_data = rtd; |
936 | soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap; | |
937 | soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer; | |
938 | soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl; | |
939 | soc_pcm_ops.copy = socdev->platform->pcm_ops->copy; | |
940 | soc_pcm_ops.silence = socdev->platform->pcm_ops->silence; | |
941 | soc_pcm_ops.ack = socdev->platform->pcm_ops->ack; | |
942 | soc_pcm_ops.page = socdev->platform->pcm_ops->page; | |
943 | ||
944 | if (playback) | |
945 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); | |
946 | ||
947 | if (capture) | |
948 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); | |
949 | ||
950 | ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm); | |
951 | if (ret < 0) { | |
952 | printk(KERN_ERR "asoc: platform pcm constructor failed\n"); | |
953 | kfree(rtd); | |
954 | return ret; | |
955 | } | |
956 | ||
957 | pcm->private_free = socdev->platform->pcm_free; | |
958 | printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, | |
959 | cpu_dai->name); | |
960 | return ret; | |
961 | } | |
962 | ||
963 | /* codec register dump */ | |
964 | static ssize_t codec_reg_show(struct device *dev, | |
965 | struct device_attribute *attr, char *buf) | |
966 | { | |
967 | struct snd_soc_device *devdata = dev_get_drvdata(dev); | |
968 | struct snd_soc_codec *codec = devdata->codec; | |
969 | int i, step = 1, count = 0; | |
970 | ||
971 | if (!codec->reg_cache_size) | |
972 | return 0; | |
973 | ||
974 | if (codec->reg_cache_step) | |
975 | step = codec->reg_cache_step; | |
976 | ||
977 | count += sprintf(buf, "%s registers\n", codec->name); | |
58cd33c0 MB |
978 | for (i = 0; i < codec->reg_cache_size; i += step) { |
979 | count += sprintf(buf + count, "%2x: ", i); | |
980 | if (count >= PAGE_SIZE - 1) | |
981 | break; | |
982 | ||
983 | if (codec->display_register) | |
984 | count += codec->display_register(codec, buf + count, | |
985 | PAGE_SIZE - count, i); | |
986 | else | |
987 | count += snprintf(buf + count, PAGE_SIZE - count, | |
988 | "%4x", codec->read(codec, i)); | |
989 | ||
990 | if (count >= PAGE_SIZE - 1) | |
991 | break; | |
992 | ||
993 | count += snprintf(buf + count, PAGE_SIZE - count, "\n"); | |
994 | if (count >= PAGE_SIZE - 1) | |
995 | break; | |
996 | } | |
997 | ||
998 | /* Truncate count; min() would cause a warning */ | |
999 | if (count >= PAGE_SIZE) | |
1000 | count = PAGE_SIZE - 1; | |
db2a4165 FM |
1001 | |
1002 | return count; | |
1003 | } | |
1004 | static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); | |
1005 | ||
1006 | /** | |
1007 | * snd_soc_new_ac97_codec - initailise AC97 device | |
1008 | * @codec: audio codec | |
1009 | * @ops: AC97 bus operations | |
1010 | * @num: AC97 codec number | |
1011 | * | |
1012 | * Initialises AC97 codec resources for use by ad-hoc devices only. | |
1013 | */ | |
1014 | int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, | |
1015 | struct snd_ac97_bus_ops *ops, int num) | |
1016 | { | |
1017 | mutex_lock(&codec->mutex); | |
1018 | ||
1019 | codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); | |
1020 | if (codec->ac97 == NULL) { | |
1021 | mutex_unlock(&codec->mutex); | |
1022 | return -ENOMEM; | |
1023 | } | |
1024 | ||
1025 | codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); | |
1026 | if (codec->ac97->bus == NULL) { | |
1027 | kfree(codec->ac97); | |
1028 | codec->ac97 = NULL; | |
1029 | mutex_unlock(&codec->mutex); | |
1030 | return -ENOMEM; | |
1031 | } | |
1032 | ||
1033 | codec->ac97->bus->ops = ops; | |
1034 | codec->ac97->num = num; | |
1035 | mutex_unlock(&codec->mutex); | |
1036 | return 0; | |
1037 | } | |
1038 | EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); | |
1039 | ||
1040 | /** | |
1041 | * snd_soc_free_ac97_codec - free AC97 codec device | |
1042 | * @codec: audio codec | |
1043 | * | |
1044 | * Frees AC97 codec device resources. | |
1045 | */ | |
1046 | void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) | |
1047 | { | |
1048 | mutex_lock(&codec->mutex); | |
1049 | kfree(codec->ac97->bus); | |
1050 | kfree(codec->ac97); | |
1051 | codec->ac97 = NULL; | |
1052 | mutex_unlock(&codec->mutex); | |
1053 | } | |
1054 | EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); | |
1055 | ||
1056 | /** | |
1057 | * snd_soc_update_bits - update codec register bits | |
1058 | * @codec: audio codec | |
1059 | * @reg: codec register | |
1060 | * @mask: register mask | |
1061 | * @value: new value | |
1062 | * | |
1063 | * Writes new register value. | |
1064 | * | |
1065 | * Returns 1 for change else 0. | |
1066 | */ | |
1067 | int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, | |
1068 | unsigned short mask, unsigned short value) | |
1069 | { | |
1070 | int change; | |
1071 | unsigned short old, new; | |
1072 | ||
1073 | mutex_lock(&io_mutex); | |
1074 | old = snd_soc_read(codec, reg); | |
1075 | new = (old & ~mask) | value; | |
1076 | change = old != new; | |
1077 | if (change) | |
1078 | snd_soc_write(codec, reg, new); | |
1079 | ||
1080 | mutex_unlock(&io_mutex); | |
1081 | return change; | |
1082 | } | |
1083 | EXPORT_SYMBOL_GPL(snd_soc_update_bits); | |
1084 | ||
1085 | /** | |
1086 | * snd_soc_test_bits - test register for change | |
1087 | * @codec: audio codec | |
1088 | * @reg: codec register | |
1089 | * @mask: register mask | |
1090 | * @value: new value | |
1091 | * | |
1092 | * Tests a register with a new value and checks if the new value is | |
1093 | * different from the old value. | |
1094 | * | |
1095 | * Returns 1 for change else 0. | |
1096 | */ | |
1097 | int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, | |
1098 | unsigned short mask, unsigned short value) | |
1099 | { | |
1100 | int change; | |
1101 | unsigned short old, new; | |
1102 | ||
1103 | mutex_lock(&io_mutex); | |
1104 | old = snd_soc_read(codec, reg); | |
1105 | new = (old & ~mask) | value; | |
1106 | change = old != new; | |
1107 | mutex_unlock(&io_mutex); | |
1108 | ||
1109 | return change; | |
1110 | } | |
1111 | EXPORT_SYMBOL_GPL(snd_soc_test_bits); | |
1112 | ||
db2a4165 FM |
1113 | /** |
1114 | * snd_soc_new_pcms - create new sound card and pcms | |
1115 | * @socdev: the SoC audio device | |
1116 | * | |
1117 | * Create a new sound card based upon the codec and interface pcms. | |
1118 | * | |
1119 | * Returns 0 for success, else error. | |
1120 | */ | |
bc7320c5 | 1121 | int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid) |
db2a4165 FM |
1122 | { |
1123 | struct snd_soc_codec *codec = socdev->codec; | |
1124 | struct snd_soc_machine *machine = socdev->machine; | |
1125 | int ret = 0, i; | |
1126 | ||
1127 | mutex_lock(&codec->mutex); | |
1128 | ||
1129 | /* register a sound card */ | |
1130 | codec->card = snd_card_new(idx, xid, codec->owner, 0); | |
1131 | if (!codec->card) { | |
1132 | printk(KERN_ERR "asoc: can't create sound card for codec %s\n", | |
1133 | codec->name); | |
1134 | mutex_unlock(&codec->mutex); | |
1135 | return -ENODEV; | |
1136 | } | |
1137 | ||
1138 | codec->card->dev = socdev->dev; | |
1139 | codec->card->private_data = codec; | |
1140 | strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver)); | |
1141 | ||
1142 | /* create the pcms */ | |
3ff3f64b | 1143 | for (i = 0; i < machine->num_links; i++) { |
db2a4165 FM |
1144 | ret = soc_new_pcm(socdev, &machine->dai_link[i], i); |
1145 | if (ret < 0) { | |
1146 | printk(KERN_ERR "asoc: can't create pcm %s\n", | |
1147 | machine->dai_link[i].stream_name); | |
1148 | mutex_unlock(&codec->mutex); | |
1149 | return ret; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | mutex_unlock(&codec->mutex); | |
1154 | return ret; | |
1155 | } | |
1156 | EXPORT_SYMBOL_GPL(snd_soc_new_pcms); | |
1157 | ||
1158 | /** | |
1159 | * snd_soc_register_card - register sound card | |
1160 | * @socdev: the SoC audio device | |
1161 | * | |
1162 | * Register a SoC sound card. Also registers an AC97 device if the | |
1163 | * codec is AC97 for ad hoc devices. | |
1164 | * | |
1165 | * Returns 0 for success, else error. | |
1166 | */ | |
1167 | int snd_soc_register_card(struct snd_soc_device *socdev) | |
1168 | { | |
1169 | struct snd_soc_codec *codec = socdev->codec; | |
1170 | struct snd_soc_machine *machine = socdev->machine; | |
12e74f7d | 1171 | int ret = 0, i, ac97 = 0, err = 0; |
db2a4165 | 1172 | |
3ff3f64b | 1173 | for (i = 0; i < machine->num_links; i++) { |
12e74f7d LG |
1174 | if (socdev->machine->dai_link[i].init) { |
1175 | err = socdev->machine->dai_link[i].init(codec); | |
1176 | if (err < 0) { | |
1177 | printk(KERN_ERR "asoc: failed to init %s\n", | |
1178 | socdev->machine->dai_link[i].stream_name); | |
1179 | continue; | |
1180 | } | |
1181 | } | |
3ff3f64b | 1182 | if (socdev->machine->dai_link[i].codec_dai->type == |
a68660e0 | 1183 | SND_SOC_DAI_AC97_BUS) |
db2a4165 FM |
1184 | ac97 = 1; |
1185 | } | |
1186 | snprintf(codec->card->shortname, sizeof(codec->card->shortname), | |
1187 | "%s", machine->name); | |
1188 | snprintf(codec->card->longname, sizeof(codec->card->longname), | |
1189 | "%s (%s)", machine->name, codec->name); | |
1190 | ||
1191 | ret = snd_card_register(codec->card); | |
1192 | if (ret < 0) { | |
3ff3f64b | 1193 | printk(KERN_ERR "asoc: failed to register soundcard for %s\n", |
db2a4165 | 1194 | codec->name); |
12e74f7d | 1195 | goto out; |
db2a4165 FM |
1196 | } |
1197 | ||
08c8efe6 | 1198 | mutex_lock(&codec->mutex); |
db2a4165 | 1199 | #ifdef CONFIG_SND_SOC_AC97_BUS |
12e74f7d LG |
1200 | if (ac97) { |
1201 | ret = soc_ac97_dev_register(codec); | |
1202 | if (ret < 0) { | |
1203 | printk(KERN_ERR "asoc: AC97 device register failed\n"); | |
1204 | snd_card_free(codec->card); | |
08c8efe6 | 1205 | mutex_unlock(&codec->mutex); |
12e74f7d LG |
1206 | goto out; |
1207 | } | |
1208 | } | |
db2a4165 FM |
1209 | #endif |
1210 | ||
12e74f7d LG |
1211 | err = snd_soc_dapm_sys_add(socdev->dev); |
1212 | if (err < 0) | |
1213 | printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n"); | |
1214 | ||
1215 | err = device_create_file(socdev->dev, &dev_attr_codec_reg); | |
1216 | if (err < 0) | |
3ff3f64b | 1217 | printk(KERN_WARNING "asoc: failed to add codec sysfs files\n"); |
08c8efe6 | 1218 | |
db2a4165 | 1219 | mutex_unlock(&codec->mutex); |
08c8efe6 MB |
1220 | |
1221 | out: | |
db2a4165 FM |
1222 | return ret; |
1223 | } | |
1224 | EXPORT_SYMBOL_GPL(snd_soc_register_card); | |
1225 | ||
1226 | /** | |
1227 | * snd_soc_free_pcms - free sound card and pcms | |
1228 | * @socdev: the SoC audio device | |
1229 | * | |
1230 | * Frees sound card and pcms associated with the socdev. | |
1231 | * Also unregister the codec if it is an AC97 device. | |
1232 | */ | |
1233 | void snd_soc_free_pcms(struct snd_soc_device *socdev) | |
1234 | { | |
1235 | struct snd_soc_codec *codec = socdev->codec; | |
a68660e0 | 1236 | #ifdef CONFIG_SND_SOC_AC97_BUS |
3c4b266f | 1237 | struct snd_soc_dai *codec_dai; |
a68660e0 LG |
1238 | int i; |
1239 | #endif | |
db2a4165 FM |
1240 | |
1241 | mutex_lock(&codec->mutex); | |
1242 | #ifdef CONFIG_SND_SOC_AC97_BUS | |
3ff3f64b | 1243 | for (i = 0; i < codec->num_dai; i++) { |
a68660e0 LG |
1244 | codec_dai = &codec->dai[i]; |
1245 | if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) { | |
1246 | soc_ac97_dev_unregister(codec); | |
1247 | goto free_card; | |
1248 | } | |
1249 | } | |
1250 | free_card: | |
db2a4165 FM |
1251 | #endif |
1252 | ||
1253 | if (codec->card) | |
1254 | snd_card_free(codec->card); | |
1255 | device_remove_file(socdev->dev, &dev_attr_codec_reg); | |
1256 | mutex_unlock(&codec->mutex); | |
1257 | } | |
1258 | EXPORT_SYMBOL_GPL(snd_soc_free_pcms); | |
1259 | ||
1260 | /** | |
1261 | * snd_soc_set_runtime_hwparams - set the runtime hardware parameters | |
1262 | * @substream: the pcm substream | |
1263 | * @hw: the hardware parameters | |
1264 | * | |
1265 | * Sets the substream runtime hardware parameters. | |
1266 | */ | |
1267 | int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, | |
1268 | const struct snd_pcm_hardware *hw) | |
1269 | { | |
1270 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1271 | runtime->hw.info = hw->info; | |
1272 | runtime->hw.formats = hw->formats; | |
1273 | runtime->hw.period_bytes_min = hw->period_bytes_min; | |
1274 | runtime->hw.period_bytes_max = hw->period_bytes_max; | |
1275 | runtime->hw.periods_min = hw->periods_min; | |
1276 | runtime->hw.periods_max = hw->periods_max; | |
1277 | runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; | |
1278 | runtime->hw.fifo_size = hw->fifo_size; | |
1279 | return 0; | |
1280 | } | |
1281 | EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); | |
1282 | ||
1283 | /** | |
1284 | * snd_soc_cnew - create new control | |
1285 | * @_template: control template | |
1286 | * @data: control private data | |
1287 | * @lnng_name: control long name | |
1288 | * | |
1289 | * Create a new mixer control from a template control. | |
1290 | * | |
1291 | * Returns 0 for success, else error. | |
1292 | */ | |
1293 | struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, | |
1294 | void *data, char *long_name) | |
1295 | { | |
1296 | struct snd_kcontrol_new template; | |
1297 | ||
1298 | memcpy(&template, _template, sizeof(template)); | |
1299 | if (long_name) | |
1300 | template.name = long_name; | |
db2a4165 FM |
1301 | template.index = 0; |
1302 | ||
1303 | return snd_ctl_new1(&template, data); | |
1304 | } | |
1305 | EXPORT_SYMBOL_GPL(snd_soc_cnew); | |
1306 | ||
1307 | /** | |
1308 | * snd_soc_info_enum_double - enumerated double mixer info callback | |
1309 | * @kcontrol: mixer control | |
1310 | * @uinfo: control element information | |
1311 | * | |
1312 | * Callback to provide information about a double enumerated | |
1313 | * mixer control. | |
1314 | * | |
1315 | * Returns 0 for success. | |
1316 | */ | |
1317 | int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, | |
1318 | struct snd_ctl_elem_info *uinfo) | |
1319 | { | |
1320 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
1321 | ||
1322 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1323 | uinfo->count = e->shift_l == e->shift_r ? 1 : 2; | |
f8ba0b7b | 1324 | uinfo->value.enumerated.items = e->max; |
db2a4165 | 1325 | |
f8ba0b7b JS |
1326 | if (uinfo->value.enumerated.item > e->max - 1) |
1327 | uinfo->value.enumerated.item = e->max - 1; | |
db2a4165 FM |
1328 | strcpy(uinfo->value.enumerated.name, |
1329 | e->texts[uinfo->value.enumerated.item]); | |
1330 | return 0; | |
1331 | } | |
1332 | EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); | |
1333 | ||
1334 | /** | |
1335 | * snd_soc_get_enum_double - enumerated double mixer get callback | |
1336 | * @kcontrol: mixer control | |
1337 | * @uinfo: control element information | |
1338 | * | |
1339 | * Callback to get the value of a double enumerated mixer. | |
1340 | * | |
1341 | * Returns 0 for success. | |
1342 | */ | |
1343 | int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, | |
1344 | struct snd_ctl_elem_value *ucontrol) | |
1345 | { | |
1346 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
1347 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
1348 | unsigned short val, bitmask; | |
1349 | ||
f8ba0b7b | 1350 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
db2a4165 FM |
1351 | ; |
1352 | val = snd_soc_read(codec, e->reg); | |
3ff3f64b MB |
1353 | ucontrol->value.enumerated.item[0] |
1354 | = (val >> e->shift_l) & (bitmask - 1); | |
db2a4165 FM |
1355 | if (e->shift_l != e->shift_r) |
1356 | ucontrol->value.enumerated.item[1] = | |
1357 | (val >> e->shift_r) & (bitmask - 1); | |
1358 | ||
1359 | return 0; | |
1360 | } | |
1361 | EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); | |
1362 | ||
1363 | /** | |
1364 | * snd_soc_put_enum_double - enumerated double mixer put callback | |
1365 | * @kcontrol: mixer control | |
1366 | * @uinfo: control element information | |
1367 | * | |
1368 | * Callback to set the value of a double enumerated mixer. | |
1369 | * | |
1370 | * Returns 0 for success. | |
1371 | */ | |
1372 | int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, | |
1373 | struct snd_ctl_elem_value *ucontrol) | |
1374 | { | |
1375 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
1376 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
1377 | unsigned short val; | |
1378 | unsigned short mask, bitmask; | |
1379 | ||
f8ba0b7b | 1380 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
db2a4165 | 1381 | ; |
f8ba0b7b | 1382 | if (ucontrol->value.enumerated.item[0] > e->max - 1) |
db2a4165 FM |
1383 | return -EINVAL; |
1384 | val = ucontrol->value.enumerated.item[0] << e->shift_l; | |
1385 | mask = (bitmask - 1) << e->shift_l; | |
1386 | if (e->shift_l != e->shift_r) { | |
f8ba0b7b | 1387 | if (ucontrol->value.enumerated.item[1] > e->max - 1) |
db2a4165 FM |
1388 | return -EINVAL; |
1389 | val |= ucontrol->value.enumerated.item[1] << e->shift_r; | |
1390 | mask |= (bitmask - 1) << e->shift_r; | |
1391 | } | |
1392 | ||
1393 | return snd_soc_update_bits(codec, e->reg, mask, val); | |
1394 | } | |
1395 | EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); | |
1396 | ||
1397 | /** | |
1398 | * snd_soc_info_enum_ext - external enumerated single mixer info callback | |
1399 | * @kcontrol: mixer control | |
1400 | * @uinfo: control element information | |
1401 | * | |
1402 | * Callback to provide information about an external enumerated | |
1403 | * single mixer. | |
1404 | * | |
1405 | * Returns 0 for success. | |
1406 | */ | |
1407 | int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, | |
1408 | struct snd_ctl_elem_info *uinfo) | |
1409 | { | |
1410 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
1411 | ||
1412 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1413 | uinfo->count = 1; | |
f8ba0b7b | 1414 | uinfo->value.enumerated.items = e->max; |
db2a4165 | 1415 | |
f8ba0b7b JS |
1416 | if (uinfo->value.enumerated.item > e->max - 1) |
1417 | uinfo->value.enumerated.item = e->max - 1; | |
db2a4165 FM |
1418 | strcpy(uinfo->value.enumerated.name, |
1419 | e->texts[uinfo->value.enumerated.item]); | |
1420 | return 0; | |
1421 | } | |
1422 | EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); | |
1423 | ||
1424 | /** | |
1425 | * snd_soc_info_volsw_ext - external single mixer info callback | |
1426 | * @kcontrol: mixer control | |
1427 | * @uinfo: control element information | |
1428 | * | |
1429 | * Callback to provide information about a single external mixer control. | |
1430 | * | |
1431 | * Returns 0 for success. | |
1432 | */ | |
1433 | int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, | |
1434 | struct snd_ctl_elem_info *uinfo) | |
1435 | { | |
a7a4ac86 PZ |
1436 | int max = kcontrol->private_value; |
1437 | ||
1438 | if (max == 1) | |
1439 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
1440 | else | |
1441 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
db2a4165 | 1442 | |
db2a4165 FM |
1443 | uinfo->count = 1; |
1444 | uinfo->value.integer.min = 0; | |
a7a4ac86 | 1445 | uinfo->value.integer.max = max; |
db2a4165 FM |
1446 | return 0; |
1447 | } | |
1448 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); | |
1449 | ||
db2a4165 FM |
1450 | /** |
1451 | * snd_soc_info_volsw - single mixer info callback | |
1452 | * @kcontrol: mixer control | |
1453 | * @uinfo: control element information | |
1454 | * | |
1455 | * Callback to provide information about a single mixer control. | |
1456 | * | |
1457 | * Returns 0 for success. | |
1458 | */ | |
1459 | int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, | |
1460 | struct snd_ctl_elem_info *uinfo) | |
1461 | { | |
4eaa9819 JS |
1462 | struct soc_mixer_control *mc = |
1463 | (struct soc_mixer_control *)kcontrol->private_value; | |
1464 | int max = mc->max; | |
815ecf8d JS |
1465 | unsigned int shift = mc->min; |
1466 | unsigned int rshift = mc->rshift; | |
db2a4165 | 1467 | |
a7a4ac86 PZ |
1468 | if (max == 1) |
1469 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
1470 | else | |
1471 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
1472 | ||
db2a4165 FM |
1473 | uinfo->count = shift == rshift ? 1 : 2; |
1474 | uinfo->value.integer.min = 0; | |
a7a4ac86 | 1475 | uinfo->value.integer.max = max; |
db2a4165 FM |
1476 | return 0; |
1477 | } | |
1478 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw); | |
1479 | ||
1480 | /** | |
1481 | * snd_soc_get_volsw - single mixer get callback | |
1482 | * @kcontrol: mixer control | |
1483 | * @uinfo: control element information | |
1484 | * | |
1485 | * Callback to get the value of a single mixer control. | |
1486 | * | |
1487 | * Returns 0 for success. | |
1488 | */ | |
1489 | int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, | |
1490 | struct snd_ctl_elem_value *ucontrol) | |
1491 | { | |
4eaa9819 JS |
1492 | struct soc_mixer_control *mc = |
1493 | (struct soc_mixer_control *)kcontrol->private_value; | |
db2a4165 | 1494 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d JS |
1495 | unsigned int reg = mc->reg; |
1496 | unsigned int shift = mc->shift; | |
1497 | unsigned int rshift = mc->rshift; | |
4eaa9819 | 1498 | int max = mc->max; |
815ecf8d JS |
1499 | unsigned int mask = (1 << fls(max)) - 1; |
1500 | unsigned int invert = mc->invert; | |
db2a4165 FM |
1501 | |
1502 | ucontrol->value.integer.value[0] = | |
1503 | (snd_soc_read(codec, reg) >> shift) & mask; | |
1504 | if (shift != rshift) | |
1505 | ucontrol->value.integer.value[1] = | |
1506 | (snd_soc_read(codec, reg) >> rshift) & mask; | |
1507 | if (invert) { | |
1508 | ucontrol->value.integer.value[0] = | |
a7a4ac86 | 1509 | max - ucontrol->value.integer.value[0]; |
db2a4165 FM |
1510 | if (shift != rshift) |
1511 | ucontrol->value.integer.value[1] = | |
a7a4ac86 | 1512 | max - ucontrol->value.integer.value[1]; |
db2a4165 FM |
1513 | } |
1514 | ||
1515 | return 0; | |
1516 | } | |
1517 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw); | |
1518 | ||
1519 | /** | |
1520 | * snd_soc_put_volsw - single mixer put callback | |
1521 | * @kcontrol: mixer control | |
1522 | * @uinfo: control element information | |
1523 | * | |
1524 | * Callback to set the value of a single mixer control. | |
1525 | * | |
1526 | * Returns 0 for success. | |
1527 | */ | |
1528 | int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, | |
1529 | struct snd_ctl_elem_value *ucontrol) | |
1530 | { | |
4eaa9819 JS |
1531 | struct soc_mixer_control *mc = |
1532 | (struct soc_mixer_control *)kcontrol->private_value; | |
db2a4165 | 1533 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d JS |
1534 | unsigned int reg = mc->reg; |
1535 | unsigned int shift = mc->shift; | |
1536 | unsigned int rshift = mc->rshift; | |
4eaa9819 | 1537 | int max = mc->max; |
815ecf8d JS |
1538 | unsigned int mask = (1 << fls(max)) - 1; |
1539 | unsigned int invert = mc->invert; | |
db2a4165 FM |
1540 | unsigned short val, val2, val_mask; |
1541 | ||
1542 | val = (ucontrol->value.integer.value[0] & mask); | |
1543 | if (invert) | |
a7a4ac86 | 1544 | val = max - val; |
db2a4165 FM |
1545 | val_mask = mask << shift; |
1546 | val = val << shift; | |
1547 | if (shift != rshift) { | |
1548 | val2 = (ucontrol->value.integer.value[1] & mask); | |
1549 | if (invert) | |
a7a4ac86 | 1550 | val2 = max - val2; |
db2a4165 FM |
1551 | val_mask |= mask << rshift; |
1552 | val |= val2 << rshift; | |
1553 | } | |
a7a4ac86 | 1554 | return snd_soc_update_bits(codec, reg, val_mask, val); |
db2a4165 FM |
1555 | } |
1556 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw); | |
1557 | ||
1558 | /** | |
1559 | * snd_soc_info_volsw_2r - double mixer info callback | |
1560 | * @kcontrol: mixer control | |
1561 | * @uinfo: control element information | |
1562 | * | |
1563 | * Callback to provide information about a double mixer control that | |
1564 | * spans 2 codec registers. | |
1565 | * | |
1566 | * Returns 0 for success. | |
1567 | */ | |
1568 | int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, | |
1569 | struct snd_ctl_elem_info *uinfo) | |
1570 | { | |
4eaa9819 JS |
1571 | struct soc_mixer_control *mc = |
1572 | (struct soc_mixer_control *)kcontrol->private_value; | |
1573 | int max = mc->max; | |
a7a4ac86 PZ |
1574 | |
1575 | if (max == 1) | |
1576 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
1577 | else | |
1578 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
db2a4165 | 1579 | |
db2a4165 FM |
1580 | uinfo->count = 2; |
1581 | uinfo->value.integer.min = 0; | |
a7a4ac86 | 1582 | uinfo->value.integer.max = max; |
db2a4165 FM |
1583 | return 0; |
1584 | } | |
1585 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); | |
1586 | ||
1587 | /** | |
1588 | * snd_soc_get_volsw_2r - double mixer get callback | |
1589 | * @kcontrol: mixer control | |
1590 | * @uinfo: control element information | |
1591 | * | |
1592 | * Callback to get the value of a double mixer control that spans 2 registers. | |
1593 | * | |
1594 | * Returns 0 for success. | |
1595 | */ | |
1596 | int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, | |
1597 | struct snd_ctl_elem_value *ucontrol) | |
1598 | { | |
4eaa9819 JS |
1599 | struct soc_mixer_control *mc = |
1600 | (struct soc_mixer_control *)kcontrol->private_value; | |
db2a4165 | 1601 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d JS |
1602 | unsigned int reg = mc->reg; |
1603 | unsigned int reg2 = mc->rreg; | |
1604 | unsigned int shift = mc->shift; | |
4eaa9819 | 1605 | int max = mc->max; |
815ecf8d JS |
1606 | unsigned int mask = (1<<fls(max))-1; |
1607 | unsigned int invert = mc->invert; | |
db2a4165 FM |
1608 | |
1609 | ucontrol->value.integer.value[0] = | |
1610 | (snd_soc_read(codec, reg) >> shift) & mask; | |
1611 | ucontrol->value.integer.value[1] = | |
1612 | (snd_soc_read(codec, reg2) >> shift) & mask; | |
1613 | if (invert) { | |
1614 | ucontrol->value.integer.value[0] = | |
a7a4ac86 | 1615 | max - ucontrol->value.integer.value[0]; |
db2a4165 | 1616 | ucontrol->value.integer.value[1] = |
a7a4ac86 | 1617 | max - ucontrol->value.integer.value[1]; |
db2a4165 FM |
1618 | } |
1619 | ||
1620 | return 0; | |
1621 | } | |
1622 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); | |
1623 | ||
1624 | /** | |
1625 | * snd_soc_put_volsw_2r - double mixer set callback | |
1626 | * @kcontrol: mixer control | |
1627 | * @uinfo: control element information | |
1628 | * | |
1629 | * Callback to set the value of a double mixer control that spans 2 registers. | |
1630 | * | |
1631 | * Returns 0 for success. | |
1632 | */ | |
1633 | int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, | |
1634 | struct snd_ctl_elem_value *ucontrol) | |
1635 | { | |
4eaa9819 JS |
1636 | struct soc_mixer_control *mc = |
1637 | (struct soc_mixer_control *)kcontrol->private_value; | |
db2a4165 | 1638 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d JS |
1639 | unsigned int reg = mc->reg; |
1640 | unsigned int reg2 = mc->rreg; | |
1641 | unsigned int shift = mc->shift; | |
4eaa9819 | 1642 | int max = mc->max; |
815ecf8d JS |
1643 | unsigned int mask = (1 << fls(max)) - 1; |
1644 | unsigned int invert = mc->invert; | |
db2a4165 FM |
1645 | int err; |
1646 | unsigned short val, val2, val_mask; | |
1647 | ||
1648 | val_mask = mask << shift; | |
1649 | val = (ucontrol->value.integer.value[0] & mask); | |
1650 | val2 = (ucontrol->value.integer.value[1] & mask); | |
1651 | ||
1652 | if (invert) { | |
a7a4ac86 PZ |
1653 | val = max - val; |
1654 | val2 = max - val2; | |
db2a4165 FM |
1655 | } |
1656 | ||
1657 | val = val << shift; | |
1658 | val2 = val2 << shift; | |
1659 | ||
3ff3f64b MB |
1660 | err = snd_soc_update_bits(codec, reg, val_mask, val); |
1661 | if (err < 0) | |
db2a4165 FM |
1662 | return err; |
1663 | ||
1664 | err = snd_soc_update_bits(codec, reg2, val_mask, val2); | |
1665 | return err; | |
1666 | } | |
1667 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); | |
1668 | ||
e13ac2e9 MB |
1669 | /** |
1670 | * snd_soc_info_volsw_s8 - signed mixer info callback | |
1671 | * @kcontrol: mixer control | |
1672 | * @uinfo: control element information | |
1673 | * | |
1674 | * Callback to provide information about a signed mixer control. | |
1675 | * | |
1676 | * Returns 0 for success. | |
1677 | */ | |
1678 | int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, | |
1679 | struct snd_ctl_elem_info *uinfo) | |
1680 | { | |
4eaa9819 JS |
1681 | struct soc_mixer_control *mc = |
1682 | (struct soc_mixer_control *)kcontrol->private_value; | |
1683 | int max = mc->max; | |
1684 | int min = mc->min; | |
e13ac2e9 MB |
1685 | |
1686 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
1687 | uinfo->count = 2; | |
1688 | uinfo->value.integer.min = 0; | |
1689 | uinfo->value.integer.max = max-min; | |
1690 | return 0; | |
1691 | } | |
1692 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); | |
1693 | ||
1694 | /** | |
1695 | * snd_soc_get_volsw_s8 - signed mixer get callback | |
1696 | * @kcontrol: mixer control | |
1697 | * @uinfo: control element information | |
1698 | * | |
1699 | * Callback to get the value of a signed mixer control. | |
1700 | * | |
1701 | * Returns 0 for success. | |
1702 | */ | |
1703 | int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, | |
1704 | struct snd_ctl_elem_value *ucontrol) | |
1705 | { | |
4eaa9819 JS |
1706 | struct soc_mixer_control *mc = |
1707 | (struct soc_mixer_control *)kcontrol->private_value; | |
e13ac2e9 | 1708 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d | 1709 | unsigned int reg = mc->reg; |
4eaa9819 | 1710 | int min = mc->min; |
e13ac2e9 MB |
1711 | int val = snd_soc_read(codec, reg); |
1712 | ||
1713 | ucontrol->value.integer.value[0] = | |
1714 | ((signed char)(val & 0xff))-min; | |
1715 | ucontrol->value.integer.value[1] = | |
1716 | ((signed char)((val >> 8) & 0xff))-min; | |
1717 | return 0; | |
1718 | } | |
1719 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); | |
1720 | ||
1721 | /** | |
1722 | * snd_soc_put_volsw_sgn - signed mixer put callback | |
1723 | * @kcontrol: mixer control | |
1724 | * @uinfo: control element information | |
1725 | * | |
1726 | * Callback to set the value of a signed mixer control. | |
1727 | * | |
1728 | * Returns 0 for success. | |
1729 | */ | |
1730 | int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, | |
1731 | struct snd_ctl_elem_value *ucontrol) | |
1732 | { | |
4eaa9819 JS |
1733 | struct soc_mixer_control *mc = |
1734 | (struct soc_mixer_control *)kcontrol->private_value; | |
e13ac2e9 | 1735 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d | 1736 | unsigned int reg = mc->reg; |
4eaa9819 | 1737 | int min = mc->min; |
e13ac2e9 MB |
1738 | unsigned short val; |
1739 | ||
1740 | val = (ucontrol->value.integer.value[0]+min) & 0xff; | |
1741 | val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; | |
1742 | ||
1743 | return snd_soc_update_bits(codec, reg, 0xffff, val); | |
1744 | } | |
1745 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); | |
1746 | ||
8c6529db LG |
1747 | /** |
1748 | * snd_soc_dai_set_sysclk - configure DAI system or master clock. | |
1749 | * @dai: DAI | |
1750 | * @clk_id: DAI specific clock ID | |
1751 | * @freq: new clock frequency in Hz | |
1752 | * @dir: new clock direction - input/output. | |
1753 | * | |
1754 | * Configures the DAI master (MCLK) or system (SYSCLK) clocking. | |
1755 | */ | |
1756 | int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, | |
1757 | unsigned int freq, int dir) | |
1758 | { | |
1759 | if (dai->dai_ops.set_sysclk) | |
1760 | return dai->dai_ops.set_sysclk(dai, clk_id, freq, dir); | |
1761 | else | |
1762 | return -EINVAL; | |
1763 | } | |
1764 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); | |
1765 | ||
1766 | /** | |
1767 | * snd_soc_dai_set_clkdiv - configure DAI clock dividers. | |
1768 | * @dai: DAI | |
1769 | * @clk_id: DAI specific clock divider ID | |
1770 | * @div: new clock divisor. | |
1771 | * | |
1772 | * Configures the clock dividers. This is used to derive the best DAI bit and | |
1773 | * frame clocks from the system or master clock. It's best to set the DAI bit | |
1774 | * and frame clocks as low as possible to save system power. | |
1775 | */ | |
1776 | int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, | |
1777 | int div_id, int div) | |
1778 | { | |
1779 | if (dai->dai_ops.set_clkdiv) | |
1780 | return dai->dai_ops.set_clkdiv(dai, div_id, div); | |
1781 | else | |
1782 | return -EINVAL; | |
1783 | } | |
1784 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); | |
1785 | ||
1786 | /** | |
1787 | * snd_soc_dai_set_pll - configure DAI PLL. | |
1788 | * @dai: DAI | |
1789 | * @pll_id: DAI specific PLL ID | |
1790 | * @freq_in: PLL input clock frequency in Hz | |
1791 | * @freq_out: requested PLL output clock frequency in Hz | |
1792 | * | |
1793 | * Configures and enables PLL to generate output clock based on input clock. | |
1794 | */ | |
1795 | int snd_soc_dai_set_pll(struct snd_soc_dai *dai, | |
1796 | int pll_id, unsigned int freq_in, unsigned int freq_out) | |
1797 | { | |
1798 | if (dai->dai_ops.set_pll) | |
1799 | return dai->dai_ops.set_pll(dai, pll_id, freq_in, freq_out); | |
1800 | else | |
1801 | return -EINVAL; | |
1802 | } | |
1803 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); | |
1804 | ||
1805 | /** | |
1806 | * snd_soc_dai_set_fmt - configure DAI hardware audio format. | |
1807 | * @dai: DAI | |
1808 | * @clk_id: DAI specific clock ID | |
1809 | * @fmt: SND_SOC_DAIFMT_ format value. | |
1810 | * | |
1811 | * Configures the DAI hardware format and clocking. | |
1812 | */ | |
1813 | int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
1814 | { | |
1815 | if (dai->dai_ops.set_fmt) | |
1816 | return dai->dai_ops.set_fmt(dai, fmt); | |
1817 | else | |
1818 | return -EINVAL; | |
1819 | } | |
1820 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); | |
1821 | ||
1822 | /** | |
1823 | * snd_soc_dai_set_tdm_slot - configure DAI TDM. | |
1824 | * @dai: DAI | |
1825 | * @mask: DAI specific mask representing used slots. | |
1826 | * @slots: Number of slots in use. | |
1827 | * | |
1828 | * Configures a DAI for TDM operation. Both mask and slots are codec and DAI | |
1829 | * specific. | |
1830 | */ | |
1831 | int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, | |
1832 | unsigned int mask, int slots) | |
1833 | { | |
1834 | if (dai->dai_ops.set_sysclk) | |
1835 | return dai->dai_ops.set_tdm_slot(dai, mask, slots); | |
1836 | else | |
1837 | return -EINVAL; | |
1838 | } | |
1839 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); | |
1840 | ||
1841 | /** | |
1842 | * snd_soc_dai_set_tristate - configure DAI system or master clock. | |
1843 | * @dai: DAI | |
1844 | * @tristate: tristate enable | |
1845 | * | |
1846 | * Tristates the DAI so that others can use it. | |
1847 | */ | |
1848 | int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) | |
1849 | { | |
1850 | if (dai->dai_ops.set_sysclk) | |
1851 | return dai->dai_ops.set_tristate(dai, tristate); | |
1852 | else | |
1853 | return -EINVAL; | |
1854 | } | |
1855 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); | |
1856 | ||
1857 | /** | |
1858 | * snd_soc_dai_digital_mute - configure DAI system or master clock. | |
1859 | * @dai: DAI | |
1860 | * @mute: mute enable | |
1861 | * | |
1862 | * Mutes the DAI DAC. | |
1863 | */ | |
1864 | int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) | |
1865 | { | |
1866 | if (dai->dai_ops.digital_mute) | |
1867 | return dai->dai_ops.digital_mute(dai, mute); | |
1868 | else | |
1869 | return -EINVAL; | |
1870 | } | |
1871 | EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); | |
1872 | ||
db2a4165 FM |
1873 | static int __devinit snd_soc_init(void) |
1874 | { | |
1875 | printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION); | |
1876 | return platform_driver_register(&soc_driver); | |
1877 | } | |
1878 | ||
1879 | static void snd_soc_exit(void) | |
1880 | { | |
3ff3f64b | 1881 | platform_driver_unregister(&soc_driver); |
db2a4165 FM |
1882 | } |
1883 | ||
1884 | module_init(snd_soc_init); | |
1885 | module_exit(snd_soc_exit); | |
1886 | ||
1887 | /* Module information */ | |
d331124d | 1888 | MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); |
db2a4165 FM |
1889 | MODULE_DESCRIPTION("ALSA SoC Core"); |
1890 | MODULE_LICENSE("GPL"); | |
8b45a209 | 1891 | MODULE_ALIAS("platform:soc-audio"); |