blob: f80c0fcf4cb18d0238433ea44ddbc79502e65663 [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2010, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#include <linux/module.h>
14#include <linux/delay.h>
15#include <linux/err.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/platform_device.h>
19#include <linux/mfd/marimba.h>
20#include <linux/mfd/timpani-audio.h>
21#include <sound/soc.h>
22#include <sound/initval.h>
23#include <sound/soc-dapm.h>
24/* Debug purpose */
25#include <linux/gpio.h>
26#include <linux/clk.h>
27#include <mach/mpp.h>
28/* End of debug purpose */
29
30#define ADIE_CODEC_MAX 2
31
32struct adie_codec_register {
33 u8 reg;
34 u8 mask;
35 u8 val;
36};
37
38static struct adie_codec_register dmic_on[] = {
39 {0x80, 0x05, 0x05},
40 {0x80, 0x05, 0x00},
41 {0x83, 0x0C, 0x00},
42 {0x8A, 0xF0, 0x30},
43 {0x86, 0xFF, 0xAC},
44 {0x87, 0xFF, 0xAC},
45 {0x8A, 0xF0, 0xF0},
46 {0x82, 0x1F, 0x1E},
47 {0x83, 0x0C, 0x0C},
48 {0x92, 0x3F, 0x21},
49 {0x94, 0x3F, 0x24},
50 {0xA3, 0x39, 0x01},
51 {0xA8, 0x0F, 0x00},
52 {0xAB, 0x3F, 0x00},
53 {0x86, 0xFF, 0x00},
54 {0x87, 0xFF, 0x00},
55 {0x8A, 0xF0, 0xC0},
56};
57
58static struct adie_codec_register dmic_off[] = {
59 {0x8A, 0xF0, 0xF0},
60 {0x83, 0x0C, 0x00},
61 {0x92, 0xFF, 0x00},
62 {0x94, 0xFF, 0x1B},
63};
64
65static struct adie_codec_register spk_on[] = {
66 {0x80, 0x02, 0x02},
67 {0x80, 0x02, 0x00},
68 {0x83, 0x03, 0x00},
69 {0x8A, 0x0F, 0x03},
70 {0xA3, 0x02, 0x02},
71 {0x84, 0xFF, 0x00},
72 {0x85, 0xFF, 0x00},
73 {0x8A, 0x0F, 0x0C},
74 {0x81, 0xFF, 0x0E},
75 {0x83, 0x03, 0x03},
76 {0x24, 0x6F, 0x6C},
77 {0xB7, 0x01, 0x01},
78 {0x31, 0x01, 0x01},
79 {0x32, 0xF8, 0x08},
80 {0x32, 0xF8, 0x48},
81 {0x32, 0xF8, 0xF8},
82 {0xE0, 0xFE, 0xAC},
83 {0xE1, 0xFE, 0xAC},
84 {0x3A, 0x24, 0x24},
85 {0xE0, 0xFE, 0x3C},
86 {0xE1, 0xFE, 0x3C},
87 {0xE0, 0xFE, 0x1C},
88 {0xE1, 0xFE, 0x1C},
89 {0xE0, 0xFE, 0x10},
90 {0xE1, 0xFE, 0x10},
91};
92
93static struct adie_codec_register spk_off[] = {
94 {0x8A, 0x0F, 0x0F},
95 {0xE0, 0xFE, 0x1C},
96 {0xE1, 0xFE, 0x1C},
97 {0xE0, 0xFE, 0x3C},
98 {0xE1, 0xFE, 0x3C},
99 {0xE0, 0xFC, 0xAC},
100 {0xE1, 0xFC, 0xAC},
101 {0x32, 0xF8, 0x00},
102 {0x31, 0x05, 0x00},
103 {0x3A, 0x24, 0x00},
104};
105
106static struct adie_codec_register spk_mute[] = {
107 {0x84, 0xFF, 0xAC},
108 {0x85, 0xFF, 0xAC},
109 {0x8A, 0x0F, 0x0C},
110};
111
112static struct adie_codec_register spk_unmute[] = {
113 {0x84, 0xFF, 0x00},
114 {0x85, 0xFF, 0x00},
115 {0x8A, 0x0F, 0x0C},
116};
117
118struct adie_codec_path {
119 int rate; /* sample rate of path */
120 u32 reg_owner;
121};
122
123struct timpani_drv_data { /* member undecided */
124 struct snd_soc_codec codec;
125 struct adie_codec_path path[ADIE_CODEC_MAX];
126 u32 ref_cnt;
127 struct marimba_codec_platform_data *codec_pdata;
128};
129
130static struct snd_soc_codec *timpani_codec;
131
132enum /* regaccess blk id */
133{
134 RA_BLOCK_RX1 = 0,
135 RA_BLOCK_RX2,
136 RA_BLOCK_TX1,
137 RA_BLOCK_TX2,
138 RA_BLOCK_LB,
139 RA_BLOCK_SHARED_RX_LB,
140 RA_BLOCK_SHARED_TX,
141 RA_BLOCK_TXFE1,
142 RA_BLOCK_TXFE2,
143 RA_BLOCK_PA_COMMON,
144 RA_BLOCK_PA_EAR,
145 RA_BLOCK_PA_HPH,
146 RA_BLOCK_PA_LINE,
147 RA_BLOCK_PA_AUX,
148 RA_BLOCK_ADC,
149 RA_BLOCK_DMIC,
150 RA_BLOCK_TX_I2S,
151 RA_BLOCK_DRV,
152 RA_BLOCK_TEST,
153 RA_BLOCK_RESERVED,
154 RA_BLOCK_NUM,
155};
156
157enum /* regaccess onwer ID */
158{
159 RA_OWNER_NONE = 0,
160 RA_OWNER_PATH_RX1,
161 RA_OWNER_PATH_RX2,
162 RA_OWNER_PATH_TX1,
163 RA_OWNER_PATH_TX2,
164 RA_OWNER_PATH_LB,
165 RA_OWNER_DRV,
166 RA_OWNER_NUM,
167};
168
169struct reg_acc_blk_cfg {
170 u8 valid_owners[RA_OWNER_NUM];
171};
172
173struct timpani_regaccess {
174 u8 reg_addr;
175 u8 blk_mask[RA_BLOCK_NUM];
176 u8 reg_mask;
177 u8 reg_default;
178};
179
180static unsigned int timpani_codec_read(struct snd_soc_codec *codec,
181 unsigned int reg)
182{
183 struct marimba *pdrv = codec->control_data;
184 int rc;
185 u8 val;
186
187 rc = marimba_read(pdrv, reg, &val, 1);
188 if (IS_ERR_VALUE(rc)) {
189 pr_err("%s: fail to write reg %x\n", __func__, reg);
190 return 0;
191 }
192 return val;
193}
194
195static int timpani_codec_write(struct snd_soc_codec *codec, unsigned int reg,
196 unsigned int value)
197{
198 struct marimba *pdrv = codec->control_data;
199 int rc;
200
201 rc = marimba_write_bit_mask(pdrv, reg, (u8 *)&value, 1, 0xFF);
202 if (IS_ERR_VALUE(rc)) {
203 pr_err("%s: fail to write reg %x\n", __func__, reg);
204 return -EIO;
205 }
206 pr_debug("%s: write reg %x val %x\n", __func__, reg, value);
207 return 0;
208}
209
210static void timpani_codec_bring_up(struct snd_soc_codec *codec)
211{
212 struct timpani_drv_data *timpani = snd_soc_codec_get_drvdata(codec);
213 int rc;
214
215 if (timpani->codec_pdata &&
216 timpani->codec_pdata->marimba_codec_power) {
217 if (timpani->ref_cnt)
218 return;
219 /* Codec power up sequence */
220 rc = timpani->codec_pdata->marimba_codec_power(1);
221 if (rc)
222 pr_err("%s: could not power up timpani "
223 "codec\n", __func__);
224 else {
225 timpani_codec_write(codec, 0xFF, 0x08);
226 timpani_codec_write(codec, 0xFF, 0x0A);
227 timpani_codec_write(codec, 0xFF, 0x0E);
228 timpani_codec_write(codec, 0xFF, 0x07);
229 timpani_codec_write(codec, 0xFF, 0x17);
230 timpani_codec_write(codec, TIMPANI_A_MREF, 0x22);
231 msleep(15);
232 timpani->ref_cnt++;
233 }
234 }
235}
236
237static void timpani_codec_bring_down(struct snd_soc_codec *codec)
238{
239 struct timpani_drv_data *timpani = snd_soc_codec_get_drvdata(codec);
240 int rc;
241
242 if (timpani->codec_pdata &&
243 timpani->codec_pdata->marimba_codec_power) {
244 timpani->ref_cnt--;
245 if (timpani->ref_cnt >= 1)
246 return;
247 timpani_codec_write(codec, TIMPANI_A_MREF, TIMPANI_MREF_POR);
248 timpani_codec_write(codec, 0xFF, 0x07);
249 timpani_codec_write(codec, 0xFF, 0x06);
250 timpani_codec_write(codec, 0xFF, 0x0E);
251 timpani_codec_write(codec, 0xFF, 0x08);
252 rc = timpani->codec_pdata->marimba_codec_power(0);
253 if (rc)
254 pr_err("%s: could not power down timpani "
255 "codec\n", __func__);
256 }
257}
258
259static void timpani_dmic_config(struct snd_soc_codec *codec, int on)
260{
261 struct adie_codec_register *regs;
262 int regs_sz, i;
263
264 if (on) {
265 regs = dmic_on;
266 regs_sz = ARRAY_SIZE(dmic_on);
267 } else {
268 regs = dmic_off;
269 regs_sz = ARRAY_SIZE(dmic_off);
270 }
271
272 for (i = 0; i < regs_sz; i++)
273 timpani_codec_write(codec, regs[i].reg,
274 (regs[i].mask & regs[i].val));
275}
276
277static void timpani_spk_config(struct snd_soc_codec *codec, int on)
278{
279 struct adie_codec_register *regs;
280 int regs_sz, i;
281
282 if (on) {
283 regs = spk_on;
284 regs_sz = ARRAY_SIZE(spk_on);
285 } else {
286 regs = spk_off;
287 regs_sz = ARRAY_SIZE(spk_off);
288 }
289
290 for (i = 0; i < regs_sz; i++)
291 timpani_codec_write(codec, regs[i].reg,
292 (regs[i].mask & regs[i].val));
293}
294
295static int timpani_startup(struct snd_pcm_substream *substream,
296 struct snd_soc_dai *dai)
297{
298 struct snd_soc_pcm_runtime *rtd = substream->private_data;
299 struct snd_soc_device *socdev = rtd->socdev;
300 struct snd_soc_codec *codec = socdev->card->codec;
301
302 pr_info("%s()\n", __func__);
303
304 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
305 pr_info("%s: playback\n", __func__);
306 timpani_codec_bring_up(codec);
307 timpani_spk_config(codec, 1);
308 } else {
309 pr_info("%s: Capture\n", __func__);
310 timpani_codec_bring_up(codec);
311 timpani_dmic_config(codec, 1);
312 }
313 return 0;
314}
315
316static void timpani_shutdown(struct snd_pcm_substream *substream,
317 struct snd_soc_dai *dai)
318{
319 struct snd_soc_pcm_runtime *rtd = substream->private_data;
320 struct snd_soc_device *socdev = rtd->socdev;
321 struct snd_soc_codec *codec = socdev->card->codec;
322
323 pr_info("%s()\n", __func__);
324 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
325 timpani_codec_bring_down(codec);
326 timpani_spk_config(codec, 0);
327 } else {
328 timpani_codec_bring_down(codec);
329 timpani_dmic_config(codec, 0);
330 }
331 return;
332}
333
334int digital_mute(struct snd_soc_dai *dai, int mute)
335{
336 struct snd_soc_codec *codec = dai->codec;
337 struct adie_codec_register *regs;
338 int regs_sz, i;
339
340 if (mute) {
341 regs = spk_mute;
342 regs_sz = ARRAY_SIZE(spk_mute);
343 } else {
344 regs = spk_unmute;
345 regs_sz = ARRAY_SIZE(spk_unmute);
346 }
347
348 for (i = 0; i < regs_sz; i++) {
349 timpani_codec_write(codec, regs[i].reg,
350 (regs[i].mask & regs[i].val));
351 msleep(10);
352 }
353
354 return 0;
355}
356
357static struct snd_soc_dai_ops timpani_dai_ops = {
358 .startup = timpani_startup,
359 .shutdown = timpani_shutdown,
360};
361
362struct snd_soc_dai timpani_codec_dai[] = {
363 {
364 .name = "TIMPANI Rx",
365 .playback = {
366 .stream_name = "Handset Playback",
367 .rates = SNDRV_PCM_RATE_8000_96000,
368 .formats = SNDRV_PCM_FMTBIT_S16_LE,
369 .rate_max = 96000,
370 .rate_min = 8000,
371 .channels_min = 1,
372 .channels_max = 2,
373 },
374 .ops = &timpani_dai_ops,
375 },
376 {
377 .name = "TIMPANI Tx",
378 .capture = {
379 .stream_name = "Handset Capture",
380 .rates = SNDRV_PCM_RATE_8000_96000,
381 .formats = SNDRV_PCM_FMTBIT_S16_LE,
382 .rate_max = 96000,
383 .rate_min = 8000,
384 .channels_min = 1,
385 .channels_max = 2,
386 },
387 .ops = &timpani_dai_ops,
388 }
389};
390
391static int timpani_soc_probe(struct platform_device *pdev)
392{
393 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
394 struct snd_soc_codec *codec;
395 int ret = 0;
396
397 if (!timpani_codec) {
398 dev_err(&pdev->dev, "core driver not yet probed\n");
399 return -ENODEV;
400 }
401
402 socdev->card->codec = timpani_codec;
403 codec = timpani_codec;
404
405 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
406 if (ret < 0)
407 dev_err(codec->dev, "failed to create pcms\n");
408 return ret;
409}
410
411/* power down chip */
412static int timpani_soc_remove(struct platform_device *pdev)
413{
414 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
415
416 snd_soc_free_pcms(socdev);
417 return 0;
418}
419
420struct snd_soc_codec_device soc_codec_dev_timpani = {
421 .probe = timpani_soc_probe,
422 .remove = timpani_soc_remove,
423};
424EXPORT_SYMBOL_GPL(soc_codec_dev_timpani);
425
426static int timpani_codec_probe(struct platform_device *pdev)
427{
428 struct snd_soc_codec *codec;
429 struct timpani_drv_data *priv;
430
431 pr_info("%s()\n", __func__);
432 priv = kzalloc(sizeof(struct timpani_drv_data), GFP_KERNEL);
433 if (priv == NULL)
434 return -ENOMEM;
435
436 codec = &priv->codec;
437 snd_soc_codec_set_drvdata(codec, priv);
438 priv->codec_pdata = pdev->dev.platform_data;
439
440 mutex_init(&codec->mutex);
441 INIT_LIST_HEAD(&codec->dapm_widgets);
442 INIT_LIST_HEAD(&codec->dapm_paths);
443
444 codec->name = "TIMPANI";
445 codec->owner = THIS_MODULE;
446 codec->read = timpani_codec_read;
447 codec->write = timpani_codec_write;
448 codec->dai = timpani_codec_dai;
449 codec->num_dai = ARRAY_SIZE(timpani_codec_dai);
450 codec->control_data = platform_get_drvdata(pdev);
451 timpani_codec = codec;
452
453 snd_soc_register_dais(timpani_codec_dai, ARRAY_SIZE(timpani_codec_dai));
454 snd_soc_register_codec(codec);
455
456 return 0;
457}
458
459static struct platform_driver timpani_codec_driver = {
460 .probe = timpani_codec_probe,
461 .driver = {
462 .name = "timpani_codec",
463 .owner = THIS_MODULE,
464 },
465};
466
467static int __init timpani_codec_init(void)
468{
469 return platform_driver_register(&timpani_codec_driver);
470}
471
472static void __exit timpani_codec_exit(void)
473{
474 platform_driver_unregister(&timpani_codec_driver);
475}
476
477module_init(timpani_codec_init);
478module_exit(timpani_codec_exit);
479
480MODULE_DESCRIPTION("Timpani codec driver");
481MODULE_VERSION("1.0");
482MODULE_LICENSE("GPL v2");