Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
Michael Sevakis | 15b7321 | 2007-10-02 07:48:50 +0000 | [diff] [blame] | 10 | * Driver for WM8721 audio codec |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 11 | * |
| 12 | * Based on code from the ipodlinux project - http://ipodlinux.org/ |
| 13 | * Adapted for Rockbox in January 2006 |
| 14 | * |
| 15 | * Original file: linux/arch/armnommu/mach-ipod/audio.c |
| 16 | * |
| 17 | * Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org) |
| 18 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 19 | * 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 Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 23 | * |
| 24 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 25 | * KIND, either express or implied. |
| 26 | * |
| 27 | ****************************************************************************/ |
Christian Gmeiner | 98e4f6f | 2007-05-22 16:12:30 +0000 | [diff] [blame] | 28 | #include "config.h" |
Christian Gmeiner | bcdf43d | 2007-05-20 23:10:15 +0000 | [diff] [blame] | 29 | #include "logf.h" |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 30 | #include "system.h" |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 31 | #include "string.h" |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 32 | #include "audio.h" |
| 33 | |
Marcoen Hirschberg | 1d7ebdf | 2006-10-20 17:12:42 +0000 | [diff] [blame] | 34 | #include "wmcodec.h" |
Marcoen Hirschberg | 85f06b8 | 2007-05-22 20:39:50 +0000 | [diff] [blame] | 35 | #include "audiohw.h" |
Daniel Ankers | acbbe82 | 2006-12-09 19:18:18 +0000 | [diff] [blame] | 36 | #include "i2s.h" |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 37 | |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 38 | #define IPOD_PCM_LEVEL 0x65 /* -6dB */ |
| 39 | |
Jens Arnold | 72f1027 | 2007-01-14 12:38:40 +0000 | [diff] [blame] | 40 | /* use zero crossing to reduce clicks during volume changes */ |
| 41 | #define VOLUME_ZC_WAIT (1<<7) |
| 42 | |
Christian Gmeiner | cdbf33a | 2007-05-22 15:56:05 +0000 | [diff] [blame] | 43 | const struct sound_settings_info audiohw_settings[] = { |
| 44 | [SOUND_VOLUME] = {"dB", 0, 1, -74, 6, -25}, |
| 45 | /* HAVE_SW_TONE_CONTROLS */ |
| 46 | [SOUND_BASS] = {"dB", 0, 1, -24, 24, 0}, |
| 47 | [SOUND_TREBLE] = {"dB", 0, 1, -24, 24, 0}, |
Marcoen Hirschberg | df31f5f | 2007-12-10 11:14:28 +0000 | [diff] [blame] | 48 | [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0}, |
Christian Gmeiner | cdbf33a | 2007-05-22 15:56:05 +0000 | [diff] [blame] | 49 | [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0}, |
Thom Johansen | 8f72192 | 2007-10-09 21:29:20 +0000 | [diff] [blame] | 50 | [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100}, |
Christian Gmeiner | cdbf33a | 2007-05-22 15:56:05 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Marcoen Hirschberg | 1b967f4 | 2006-12-06 13:34:15 +0000 | [diff] [blame] | 53 | /* convert tenth of dB volume (-730..60) to master volume register value */ |
| 54 | int tenthdb2master(int db) |
| 55 | { |
| 56 | /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */ |
| 57 | /* 1111111 == +6dB (0x7f) */ |
| 58 | /* 1111001 == 0dB (0x79) */ |
| 59 | /* 0110000 == -73dB (0x30 */ |
| 60 | /* 0101111 == mute (0x2f) */ |
| 61 | |
| 62 | if (db < VOLUME_MIN) { |
| 63 | return 0x2f; |
| 64 | } else { |
| 65 | return((db/10)+0x30+73); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /* convert tenth of dB volume (-780..0) to mixer volume register value */ |
| 70 | int tenthdb2mixer(int db) |
| 71 | { |
| 72 | if (db < -660) /* 1.5 dB steps */ |
| 73 | return (2640 - db) / 15; |
| 74 | else if (db < -600) /* 0.75 dB steps */ |
| 75 | return (990 - db) * 2 / 15; |
| 76 | else if (db < -460) /* 0.5 dB steps */ |
| 77 | return (460 - db) / 5; |
| 78 | else /* 0.25 dB steps */ |
| 79 | return -db * 2 / 5; |
| 80 | } |
Dave Chapman | 2f76763 | 2006-02-26 15:59:46 +0000 | [diff] [blame] | 81 | |
Christian Gmeiner | d1178d2 | 2007-06-13 06:33:40 +0000 | [diff] [blame] | 82 | void audiohw_mute(bool mute) |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 83 | { |
| 84 | if (mute) |
| 85 | { |
| 86 | /* Set DACMU = 1 to soft-mute the audio DACs. */ |
Michael Sevakis | b6dab33 | 2007-10-02 08:30:30 +0000 | [diff] [blame] | 87 | wmcodec_write(DAPCTRL, 0x8); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 88 | } else { |
| 89 | /* Set DACMU = 0 to soft-un-mute the audio DACs. */ |
Michael Sevakis | b6dab33 | 2007-10-02 08:30:30 +0000 | [diff] [blame] | 90 | wmcodec_write(DAPCTRL, 0x0); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 91 | } |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 92 | } |
Dave Chapman | 2f76763 | 2006-02-26 15:59:46 +0000 | [diff] [blame] | 93 | |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 94 | /** From ipodLinux **/ |
| 95 | static void codec_set_active(int active) |
| 96 | { |
| 97 | /* set active to 0x0 or 0x1 */ |
| 98 | if (active) { |
Marcoen Hirschberg | 1d7ebdf | 2006-10-20 17:12:42 +0000 | [diff] [blame] | 99 | wmcodec_write(ACTIVECTRL, 0x01); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 100 | } else { |
Marcoen Hirschberg | 1d7ebdf | 2006-10-20 17:12:42 +0000 | [diff] [blame] | 101 | wmcodec_write(ACTIVECTRL, 0x00); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 105 | |
| 106 | /* Silently enable / disable audio output */ |
Marcoen Hirschberg | 77d039b | 2006-12-06 10:24:59 +0000 | [diff] [blame] | 107 | void audiohw_enable_output(bool enable) |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 108 | { |
Jens Arnold | 7e0ea21 | 2006-03-30 20:18:02 +0000 | [diff] [blame] | 109 | if (enable) |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 110 | { |
| 111 | /* reset the I2S controller into known state */ |
| 112 | i2s_reset(); |
| 113 | |
Marcoen Hirschberg | 1d7ebdf | 2006-10-20 17:12:42 +0000 | [diff] [blame] | 114 | wmcodec_write(RESET, 0x0); /*Reset*/ |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 115 | |
| 116 | codec_set_active(0x0); |
| 117 | |
| 118 | /* DACSEL=1 */ |
Marcoen Hirschberg | 1d7ebdf | 2006-10-20 17:12:42 +0000 | [diff] [blame] | 119 | wmcodec_write(0x4, 0x10); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 120 | |
| 121 | /* set power register to POWEROFF=0 on OUTPD=0, DACPD=0 */ |
Michael Sevakis | b6dab33 | 2007-10-02 08:30:30 +0000 | [diff] [blame] | 122 | wmcodec_write(PDCTRL, 0x67); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 123 | |
| 124 | /* BCLKINV=0(Dont invert BCLK) MS=1(Enable Master) LRSWAP=0 LRP=0 */ |
| 125 | /* IWL=00(16 bit) FORMAT=10(I2S format) */ |
Marcoen Hirschberg | 1d7ebdf | 2006-10-20 17:12:42 +0000 | [diff] [blame] | 126 | wmcodec_write(AINTFCE, 0x42); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 127 | |
Michael Sevakis | 15b7321 | 2007-10-02 07:48:50 +0000 | [diff] [blame] | 128 | audiohw_set_sample_rate(WM8721_USB24_44100HZ); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 129 | |
| 130 | /* set the volume to -6dB */ |
Marcoen Hirschberg | 1d7ebdf | 2006-10-20 17:12:42 +0000 | [diff] [blame] | 131 | wmcodec_write(LOUTVOL, IPOD_PCM_LEVEL); |
| 132 | wmcodec_write(ROUTVOL, 0x100 | IPOD_PCM_LEVEL); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 133 | |
| 134 | /* ACTIVE=1 */ |
| 135 | codec_set_active(1); |
| 136 | |
| 137 | /* 5. Set DACMU = 0 to soft-un-mute the audio DACs. */ |
Michael Sevakis | b6dab33 | 2007-10-02 08:30:30 +0000 | [diff] [blame] | 138 | wmcodec_write(DAPCTRL, 0x0); |
Barry Wardell | e379225 | 2006-08-30 22:31:05 +0000 | [diff] [blame] | 139 | |
Marcoen Hirschberg | 77d039b | 2006-12-06 10:24:59 +0000 | [diff] [blame] | 140 | audiohw_mute(0); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 141 | } else { |
Marcoen Hirschberg | 77d039b | 2006-12-06 10:24:59 +0000 | [diff] [blame] | 142 | audiohw_mute(1); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Christian Gmeiner | 06971be | 2008-02-13 11:19:23 +0000 | [diff] [blame] | 146 | void audiohw_set_master_vol(int vol_l, int vol_r) |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 147 | { |
| 148 | /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */ |
| 149 | /* 1111111 == +6dB */ |
| 150 | /* 1111001 == 0dB */ |
| 151 | /* 0110000 == -73dB */ |
| 152 | /* 0101111 == mute (0x2f) */ |
Jens Arnold | 72f1027 | 2007-01-14 12:38:40 +0000 | [diff] [blame] | 153 | wmcodec_write(LOUTVOL, VOLUME_ZC_WAIT | vol_l); |
| 154 | wmcodec_write(ROUTVOL, VOLUME_ZC_WAIT | vol_r); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Michael Sevakis | 15b7321 | 2007-10-02 07:48:50 +0000 | [diff] [blame] | 157 | /* Nice shutdown of WM8721 codec */ |
Marcoen Hirschberg | 77d039b | 2006-12-06 10:24:59 +0000 | [diff] [blame] | 158 | void audiohw_close(void) |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 159 | { |
| 160 | /* set DACMU=1 DEEMPH=0 */ |
Michael Sevakis | b6dab33 | 2007-10-02 08:30:30 +0000 | [diff] [blame] | 161 | wmcodec_write(DAPCTRL, 0x8); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 162 | |
| 163 | /* ACTIVE=0 */ |
| 164 | codec_set_active(0x0); |
| 165 | |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 166 | /* set DACSEL=0, MUTEMIC=1 */ |
Marcoen Hirschberg | 1d7ebdf | 2006-10-20 17:12:42 +0000 | [diff] [blame] | 167 | wmcodec_write(0x4, 0x2); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 168 | |
| 169 | /* set POWEROFF=0 OUTPD=0 DACPD=1 */ |
Michael Sevakis | b6dab33 | 2007-10-02 08:30:30 +0000 | [diff] [blame] | 170 | wmcodec_write(PDCTRL, 0x6f); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 171 | |
| 172 | /* set POWEROFF=1 OUTPD=1 DACPD=1 */ |
Michael Sevakis | b6dab33 | 2007-10-02 08:30:30 +0000 | [diff] [blame] | 173 | wmcodec_write(PDCTRL, 0xff); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* Change the order of the noise shaper, 5th order is recommended above 32kHz */ |
Marcoen Hirschberg | 77d039b | 2006-12-06 10:24:59 +0000 | [diff] [blame] | 177 | void audiohw_set_nsorder(int order) |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 178 | { |
| 179 | (void)order; |
| 180 | } |
| 181 | |
Marcoen Hirschberg | 77d039b | 2006-12-06 10:24:59 +0000 | [diff] [blame] | 182 | void audiohw_set_sample_rate(int sampling_control) |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 183 | { |
| 184 | codec_set_active(0x0); |
Marcoen Hirschberg | 1d7ebdf | 2006-10-20 17:12:42 +0000 | [diff] [blame] | 185 | wmcodec_write(SAMPCTRL, sampling_control); |
Dave Chapman | 465596b | 2006-02-05 17:16:34 +0000 | [diff] [blame] | 186 | codec_set_active(0x1); |
| 187 | } |