blob: 823643932dde2eb6208fd290f90254a2416e29bc [file] [log] [blame]
Miguel Aguilarb56e9722010-03-11 09:32:59 -06001/*
2 * ALSA SoC CQ0093 Voice Codec Driver for DaVinci platforms
3 *
4 * Copyright (C) 2010 Texas Instruments, Inc
5 *
6 * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/init.h>
25#include <linux/io.h>
26#include <linux/delay.h>
27#include <linux/pm.h>
28#include <linux/platform_device.h>
29#include <linux/device.h>
Tejun Heo923a0042010-03-30 02:52:29 +090030#include <linux/slab.h>
Miguel Aguilarb56e9722010-03-11 09:32:59 -060031#include <linux/clk.h>
32#include <linux/mfd/davinci_voicecodec.h>
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000033#include <linux/spi/spi.h>
Miguel Aguilarb56e9722010-03-11 09:32:59 -060034
35#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
39#include <sound/soc-dai.h>
40#include <sound/soc-dapm.h>
41#include <sound/initval.h>
42
43#include <mach/dm365.h>
44
Miguel Aguilarb56e9722010-03-11 09:32:59 -060045static inline unsigned int cq93vc_read(struct snd_soc_codec *codec,
46 unsigned int reg)
47{
48 struct davinci_vc *davinci_vc = codec->control_data;
49
50 return readl(davinci_vc->base + reg);
51}
52
53static inline int cq93vc_write(struct snd_soc_codec *codec, unsigned int reg,
54 unsigned int value)
55{
56 struct davinci_vc *davinci_vc = codec->control_data;
57
58 writel(value, davinci_vc->base + reg);
59
60 return 0;
61}
62
63static const struct snd_kcontrol_new cq93vc_snd_controls[] = {
64 SOC_SINGLE("PGA Capture Volume", DAVINCI_VC_REG05, 0, 0x03, 0),
65 SOC_SINGLE("Mono DAC Playback Volume", DAVINCI_VC_REG09, 0, 0x3f, 0),
66};
67
68static int cq93vc_mute(struct snd_soc_dai *dai, int mute)
69{
70 struct snd_soc_codec *codec = dai->codec;
71 u8 reg = cq93vc_read(codec, DAVINCI_VC_REG09) & ~DAVINCI_VC_REG09_MUTE;
72
73 if (mute)
74 cq93vc_write(codec, DAVINCI_VC_REG09,
75 reg | DAVINCI_VC_REG09_MUTE);
76 else
77 cq93vc_write(codec, DAVINCI_VC_REG09, reg);
78
79 return 0;
80}
81
82static int cq93vc_set_dai_sysclk(struct snd_soc_dai *codec_dai,
83 int clk_id, unsigned int freq, int dir)
84{
85 struct snd_soc_codec *codec = codec_dai->codec;
86 struct davinci_vc *davinci_vc = codec->control_data;
87
88 switch (freq) {
89 case 22579200:
90 case 27000000:
91 case 33868800:
92 davinci_vc->cq93vc.sysclk = freq;
93 return 0;
94 }
95
96 return -EINVAL;
97}
98
99static int cq93vc_set_bias_level(struct snd_soc_codec *codec,
100 enum snd_soc_bias_level level)
101{
102 switch (level) {
103 case SND_SOC_BIAS_ON:
104 cq93vc_write(codec, DAVINCI_VC_REG12,
105 DAVINCI_VC_REG12_POWER_ALL_ON);
106 break;
107 case SND_SOC_BIAS_PREPARE:
108 break;
109 case SND_SOC_BIAS_STANDBY:
110 cq93vc_write(codec, DAVINCI_VC_REG12,
111 DAVINCI_VC_REG12_POWER_ALL_OFF);
112 break;
113 case SND_SOC_BIAS_OFF:
114 /* force all power off */
115 cq93vc_write(codec, DAVINCI_VC_REG12,
116 DAVINCI_VC_REG12_POWER_ALL_OFF);
117 break;
118 }
119 codec->bias_level = level;
120
121 return 0;
122}
123
124#define CQ93VC_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000)
125#define CQ93VC_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
126
127static struct snd_soc_dai_ops cq93vc_dai_ops = {
128 .digital_mute = cq93vc_mute,
129 .set_sysclk = cq93vc_set_dai_sysclk,
130};
131
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000132static struct snd_soc_dai_driver cq93vc_dai = {
133 .name = "cq93vc-hifi",
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600134 .playback = {
135 .stream_name = "Playback",
136 .channels_min = 1,
137 .channels_max = 2,
138 .rates = CQ93VC_RATES,
139 .formats = CQ93VC_FORMATS,},
140 .capture = {
141 .stream_name = "Capture",
142 .channels_min = 1,
143 .channels_max = 2,
144 .rates = CQ93VC_RATES,
145 .formats = CQ93VC_FORMATS,},
146 .ops = &cq93vc_dai_ops,
147};
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600148
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000149static int cq93vc_resume(struct snd_soc_codec *codec)
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600150{
Mark Brown29e189c2010-05-07 20:30:00 +0100151 cq93vc_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600152
153 return 0;
154}
155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156static int cq93vc_probe(struct snd_soc_codec *codec)
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600157{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000158 struct davinci_vc *davinci_vc = codec->dev->platform_data;
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000160 davinci_vc->cq93vc.codec = codec;
161 codec->control_data = davinci_vc;
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600162
163 /* Set controls */
164 snd_soc_add_controls(codec, cq93vc_snd_controls,
165 ARRAY_SIZE(cq93vc_snd_controls));
166
167 /* Off, with power on */
168 cq93vc_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
169
170 return 0;
171}
172
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173static int cq93vc_remove(struct snd_soc_codec *codec)
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600174{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000175 cq93vc_set_bias_level(codec, SND_SOC_BIAS_OFF);
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600176
177 return 0;
178}
179
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000180static struct snd_soc_codec_driver soc_codec_dev_cq93vc = {
181 .read = cq93vc_read,
182 .write = cq93vc_write,
183 .set_bias_level = cq93vc_set_bias_level,
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600184 .probe = cq93vc_probe,
185 .remove = cq93vc_remove,
186 .resume = cq93vc_resume,
187};
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600188
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000189static int cq93vc_platform_probe(struct platform_device *pdev)
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600190{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000191 return snd_soc_register_codec(&pdev->dev,
192 &soc_codec_dev_cq93vc, &cq93vc_dai, 1);
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600193}
194
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000195static int cq93vc_platform_remove(struct platform_device *pdev)
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600196{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000197 snd_soc_unregister_codec(&pdev->dev);
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600198 return 0;
199}
200
201static struct platform_driver cq93vc_codec_driver = {
202 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000203 .name = "cq93vc-codec",
204 .owner = THIS_MODULE,
205 },
206
207 .probe = cq93vc_platform_probe,
208 .remove = __devexit_p(cq93vc_platform_remove),
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600209};
210
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000211static int __init cq93vc_init(void)
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600212{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000213 return platform_driver_register(&cq93vc_codec_driver);
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600214}
215module_init(cq93vc_init);
216
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000217static void __exit cq93vc_exit(void)
Miguel Aguilarb56e9722010-03-11 09:32:59 -0600218{
219 platform_driver_unregister(&cq93vc_codec_driver);
220}
221module_exit(cq93vc_exit);
222
223MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC CQ0093 Voice Codec Driver");
224MODULE_AUTHOR("Miguel Aguilar");
225MODULE_LICENSE("GPL");