blob: 7918d0327400bcee861df1b9af097e42fcb8ebd5 [file] [log] [blame]
Dave Chapman9581ad32006-02-13 13:48:08 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
Barry Wardelldf0dc222006-12-18 01:52:21 +000010 * Driver for WM8758 audio codec - based on datasheet for WM8983
Dave Chapman9581ad32006-02-13 13:48:08 +000011 *
12 * Based on code from the ipodlinux project - http://ipodlinux.org/
13 * Adapted for Rockbox in December 2005
14 *
15 * Original file: linux/arch/armnommu/mach-ipod/audio.c
16 *
17 * Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org)
18 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000019 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
Dave Chapman9581ad32006-02-13 13:48:08 +000023 *
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
25 * KIND, either express or implied.
26 *
27 ****************************************************************************/
Dave Chapman9581ad32006-02-13 13:48:08 +000028#include "system.h"
Dave Chapman9581ad32006-02-13 13:48:08 +000029#include "string.h"
Dave Chapman9581ad32006-02-13 13:48:08 +000030#include "audio.h"
31
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +000032#include "wmcodec.h"
Marcoen Hirschberg85f06b82007-05-22 20:39:50 +000033#include "audiohw.h"
Daniel Ankersacbbe822006-12-09 19:18:18 +000034#include "i2s.h"
Dave Chapman9581ad32006-02-13 13:48:08 +000035
Christian Gmeinercdbf33a2007-05-22 15:56:05 +000036const struct sound_settings_info audiohw_settings[] = {
37 [SOUND_VOLUME] = {"dB", 0, 1, -58, 6, -25},
Dan Evertond7e1f772007-11-24 07:51:00 +000038 [SOUND_BASS] = {"dB", 0, 1, -12, 12, 0},
39 [SOUND_TREBLE] = {"dB", 0, 1, -12, 12, 0},
Marcoen Hirschbergdf31f5f2007-12-10 11:14:28 +000040 [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
Christian Gmeinercdbf33a2007-05-22 15:56:05 +000041 [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
Thom Johansen8f721922007-10-09 21:29:20 +000042 [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100},
Peter D'Hoye825d89f2008-05-25 21:19:07 +000043#ifdef HAVE_RECORDING
Christian Gmeinercdbf33a2007-05-22 15:56:05 +000044 [SOUND_LEFT_GAIN] = {"dB", 1, 1,-128, 96, 0},
45 [SOUND_RIGHT_GAIN] = {"dB", 1, 1,-128, 96, 0},
46 [SOUND_MIC_GAIN] = {"dB", 1, 1,-128, 108, 16},
Peter D'Hoye825d89f2008-05-25 21:19:07 +000047#endif
Dan Evertond7e1f772007-11-24 07:51:00 +000048 [SOUND_BASS_CUTOFF] = {"", 0, 1, 1, 4, 1},
49 [SOUND_TREBLE_CUTOFF] = {"", 0, 1, 1, 4, 1},
Christian Gmeinercdbf33a2007-05-22 15:56:05 +000050};
51
Dan Evertond7e1f772007-11-24 07:51:00 +000052/* shadow registers */
53unsigned int eq1_reg;
54unsigned int eq5_reg;
55
Marcoen Hirschberg1b967f42006-12-06 13:34:15 +000056/* convert tenth of dB volume (-57..6) to master volume register value */
57int tenthdb2master(int db)
58{
59 /* +6 to -57dB in 1dB steps == 64 levels = 6 bits */
60 /* 0111111 == +6dB (0x3f) = 63) */
61 /* 0111001 == 0dB (0x39) = 57) */
62 /* 0000001 == -56dB (0x01) = */
63 /* 0000000 == -57dB (0x00) */
64
65 /* 1000000 == Mute (0x40) */
66
67 if (db < VOLUME_MIN) {
68 return 0x40;
69 } else {
70 return((db/10)+57);
71 }
72}
73
74/* convert tenth of dB volume (-780..0) to mixer volume register value */
75int tenthdb2mixer(int db)
76{
77 if (db < -660) /* 1.5 dB steps */
78 return (2640 - db) / 15;
79 else if (db < -600) /* 0.75 dB steps */
80 return (990 - db) * 2 / 15;
81 else if (db < -460) /* 0.5 dB steps */
82 return (460 - db) / 5;
83 else /* 0.25 dB steps */
84 return -db * 2 / 5;
85}
86
Dave Chapman9581ad32006-02-13 13:48:08 +000087#define IPOD_PCM_LEVEL 0x65 /* -6dB */
88
89//#define BASSCTRL 0x
90//#define TREBCTRL 0x0b
91
Dave Chapman9581ad32006-02-13 13:48:08 +000092/* Silently enable / disable audio output */
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +000093void audiohw_enable_output(bool enable)
Dave Chapman9581ad32006-02-13 13:48:08 +000094{
95 if (enable)
96 {
97 /* reset the I2S controller into known state */
98 i2s_reset();
99
Dave Chapman800023b2006-02-20 22:46:58 +0000100 /* TODO: Review the power-up sequence to prevent pops */
101
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000102 wmcodec_write(RESET, 0x1ff); /*Reset*/
Dave Chapman9581ad32006-02-13 13:48:08 +0000103
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000104 wmcodec_write(PWRMGMT1, 0x2b);
105 wmcodec_write(PWRMGMT2, 0x180);
106 wmcodec_write(PWRMGMT3, 0x6f);
Dave Chapman9581ad32006-02-13 13:48:08 +0000107
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000108 wmcodec_write(AINTFCE, 0x10);
109 wmcodec_write(CLKCTRL, 0x49);
Dave Chapman9581ad32006-02-13 13:48:08 +0000110
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000111 wmcodec_write(OUTCTRL, 1);
Dave Chapman9581ad32006-02-13 13:48:08 +0000112
113 /* The iPod can handle multiple frequencies, but fix at 44.1KHz
114 for now */
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000115 audiohw_set_sample_rate(WM8758_44100HZ);
Dave Chapman9581ad32006-02-13 13:48:08 +0000116
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000117 wmcodec_write(LOUTMIX,0x1); /* Enable mixer */
118 wmcodec_write(ROUTMIX,0x1); /* Enable mixer */
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000119 audiohw_mute(0);
Dave Chapman9581ad32006-02-13 13:48:08 +0000120 } else {
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000121 audiohw_mute(1);
Dave Chapman9581ad32006-02-13 13:48:08 +0000122 }
123}
124
Christian Gmeiner06971be2008-02-13 11:19:23 +0000125void audiohw_set_master_vol(int vol_l, int vol_r)
Dave Chapman9581ad32006-02-13 13:48:08 +0000126{
Dave Chapman9581ad32006-02-13 13:48:08 +0000127 /* OUT1 */
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000128 wmcodec_write(LOUT1VOL, 0x080 | vol_l);
129 wmcodec_write(ROUT1VOL, 0x180 | vol_r);
Thom Johansen473fc2b2006-03-22 13:04:49 +0000130}
131
Christian Gmeiner06971be2008-02-13 11:19:23 +0000132void audiohw_set_lineout_vol(int vol_l, int vol_r)
Thom Johansen473fc2b2006-03-22 13:04:49 +0000133{
Dave Chapman9581ad32006-02-13 13:48:08 +0000134 /* OUT2 */
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000135 wmcodec_write(LOUT2VOL, vol_l);
136 wmcodec_write(ROUT2VOL, 0x100 | vol_r);
Dave Chapman9581ad32006-02-13 13:48:08 +0000137}
138
Christian Gmeiner06971be2008-02-13 11:19:23 +0000139void audiohw_set_mixer_vol(int channel1, int channel2)
Dave Chapman9581ad32006-02-13 13:48:08 +0000140{
141 (void)channel1;
142 (void)channel2;
Dave Chapman9581ad32006-02-13 13:48:08 +0000143}
144
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000145void audiohw_set_bass(int value)
Dave Chapman9581ad32006-02-13 13:48:08 +0000146{
Dan Evertond7e1f772007-11-24 07:51:00 +0000147 eq1_reg = (eq1_reg & ~EQ_GAIN_MASK) | EQ_GAIN_VALUE(value);
148 wmcodec_write(EQ1, 0x100 | eq1_reg);
149}
150
151void audiohw_set_bass_cutoff(int value)
152{
153 eq1_reg = (eq1_reg & ~EQ_CUTOFF_MASK) | EQ_CUTOFF_VALUE(value);
154 wmcodec_write(EQ1, 0x100 | eq1_reg);
Dave Chapman9581ad32006-02-13 13:48:08 +0000155}
156
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000157void audiohw_set_treble(int value)
Dave Chapman9581ad32006-02-13 13:48:08 +0000158{
Dan Evertond7e1f772007-11-24 07:51:00 +0000159 eq5_reg = (eq5_reg & ~EQ_GAIN_MASK) | EQ_GAIN_VALUE(value);
160 wmcodec_write(EQ5, eq5_reg);
161}
162
163void audiohw_set_treble_cutoff(int value)
164{
165 eq5_reg = (eq5_reg & ~EQ_CUTOFF_MASK) | EQ_CUTOFF_VALUE(value);
166 wmcodec_write(EQ5, eq5_reg);
Dave Chapman9581ad32006-02-13 13:48:08 +0000167}
168
Christian Gmeinerd1178d22007-06-13 06:33:40 +0000169void audiohw_mute(bool mute)
Dave Chapman9581ad32006-02-13 13:48:08 +0000170{
171 if (mute)
172 {
173 /* Set DACMU = 1 to soft-mute the audio DACs. */
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000174 wmcodec_write(DACCTRL, 0x40);
Dave Chapman9581ad32006-02-13 13:48:08 +0000175 } else {
176 /* Set DACMU = 0 to soft-un-mute the audio DACs. */
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000177 wmcodec_write(DACCTRL, 0x0);
Dave Chapman9581ad32006-02-13 13:48:08 +0000178 }
Dave Chapman9581ad32006-02-13 13:48:08 +0000179}
180
181/* Nice shutdown of WM8758 codec */
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000182void audiohw_close(void)
Dave Chapman9581ad32006-02-13 13:48:08 +0000183{
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000184 audiohw_mute(1);
Dave Chapman9581ad32006-02-13 13:48:08 +0000185
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000186 wmcodec_write(PWRMGMT3, 0x0);
Dave Chapman9581ad32006-02-13 13:48:08 +0000187
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000188 wmcodec_write(PWRMGMT1, 0x0);
Dave Chapman9581ad32006-02-13 13:48:08 +0000189
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000190 wmcodec_write(PWRMGMT2, 0x40);
Dave Chapman9581ad32006-02-13 13:48:08 +0000191}
192
193/* Change the order of the noise shaper, 5th order is recommended above 32kHz */
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000194void audiohw_set_nsorder(int order)
Dave Chapman9581ad32006-02-13 13:48:08 +0000195{
196 (void)order;
197}
198
199/* Note: Disable output before calling this function */
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000200void audiohw_set_sample_rate(int sampling_control)
Dave Chapman9581ad32006-02-13 13:48:08 +0000201{
202 /**** We force 44.1KHz for now. ****/
203 (void)sampling_control;
204
205 /* set clock div */
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000206 wmcodec_write(CLKCTRL, 1 | (0 << 2) | (2 << 5));
Dave Chapman9581ad32006-02-13 13:48:08 +0000207
208 /* setup PLL for MHZ=11.2896 */
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000209 wmcodec_write(PLLN, (1 << 4) | 0x7);
210 wmcodec_write(PLLK1, 0x21);
211 wmcodec_write(PLLK2, 0x161);
212 wmcodec_write(PLLK3, 0x26);
Dave Chapman9581ad32006-02-13 13:48:08 +0000213
214 /* set clock div */
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000215 wmcodec_write(CLKCTRL, 1 | (1 << 2) | (2 << 5) | (1 << 8));
Dave Chapman9581ad32006-02-13 13:48:08 +0000216
217 /* set srate */
Marcoen Hirschberg1d7ebdf2006-10-20 17:12:42 +0000218 wmcodec_write(SRATECTRL, (0 << 1));
Dave Chapman9581ad32006-02-13 13:48:08 +0000219}
220
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000221void audiohw_enable_recording(bool source_mic)
Dave Chapman9581ad32006-02-13 13:48:08 +0000222{
Barry Wardelldf0dc222006-12-18 01:52:21 +0000223 (void)source_mic; /* We only have a line-in (I think) */
224
225 /* reset the I2S controller into known state */
226 i2s_reset();
227
228 wmcodec_write(RESET, 0x1ff); /*Reset*/
229
230 wmcodec_write(PWRMGMT1, 0x2b);
231 wmcodec_write(PWRMGMT2, 0x18f); /* Enable ADC - 0x0c enables left/right PGA input, and 0x03 turns on power to the ADCs */
232 wmcodec_write(PWRMGMT3, 0x6f);
233
234 wmcodec_write(AINTFCE, 0x10);
235 wmcodec_write(CLKCTRL, 0x49);
236
237 wmcodec_write(OUTCTRL, 1);
238
239 /* The iPod can handle multiple frequencies, but fix at 44.1KHz
240 for now */
Barry Wardellca7a2642006-12-18 02:18:56 +0000241 audiohw_set_sample_rate(WM8758_44100HZ);
Barry Wardelldf0dc222006-12-18 01:52:21 +0000242
243 wmcodec_write(INCTRL,0x44); /* Connect L2 and R2 inputs */
244
245 /* Set L2/R2_2BOOSTVOL to 0db (bits 4-6) */
246 /* 000 = disabled
247 001 = -12dB
248 010 = -9dB
249 011 = -6dB
250 100 = -3dB
251 101 = 0dB
252 110 = 3dB
253 111 = 6dB
254 */
255 wmcodec_write(LADCBOOST,0x50);
256 wmcodec_write(RADCBOOST,0x50);
257
258 /* Set L/R input PGA Volume to 0db */
259 // wm8758_write(LINPGAVOL,0x3f);
260 // wm8758_write(RINPGAVOL,0x13f);
261
262 /* Enable monitoring */
263 wmcodec_write(LOUTMIX,0x17); /* Enable output mixer - BYPL2LMIX @ 0db*/
264 wmcodec_write(ROUTMIX,0x17); /* Enable output mixer - BYPR2RMIX @ 0db*/
265
Barry Wardellca7a2642006-12-18 02:18:56 +0000266 audiohw_mute(0);
Dave Chapman9581ad32006-02-13 13:48:08 +0000267}
268
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000269void audiohw_disable_recording(void) {
Barry Wardellca7a2642006-12-18 02:18:56 +0000270 audiohw_mute(1);
Dave Chapman9581ad32006-02-13 13:48:08 +0000271
Barry Wardelldf0dc222006-12-18 01:52:21 +0000272 wmcodec_write(PWRMGMT3, 0x0);
273
274 wmcodec_write(PWRMGMT1, 0x0);
275
276 wmcodec_write(PWRMGMT2, 0x40);
Dave Chapman9581ad32006-02-13 13:48:08 +0000277}
278
Marcoen Hirschberg77d039b2006-12-06 10:24:59 +0000279void audiohw_set_recvol(int left, int right, int type) {
Dave Chapman9581ad32006-02-13 13:48:08 +0000280
281 (void)left;
282 (void)right;
283 (void)type;
284}
285
Christian Gmeinerc583f3c2007-11-19 15:50:52 +0000286void audiohw_set_monitor(bool enable) {
Dave Chapman9581ad32006-02-13 13:48:08 +0000287
288 (void)enable;
289}
Dan Everton8030c802006-08-14 10:52:05 +0000290