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