1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Andy Young
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
34 #include "i2c-coldfire.h"
38 /* ------------------------------------------------- */
39 /* Local functions and variables */
40 /* ------------------------------------------------- */
42 int uda1380_write_reg(unsigned char reg
, unsigned short value
);
43 unsigned short uda1380_regs
[0x30];
47 /* Definition of a playback configuration to start with */
49 #define NUM_DEFAULT_REGS 13
50 unsigned short uda1380_defaults
[2*NUM_DEFAULT_REGS
] =
52 REG_0
, EN_DAC
| EN_INT
| EN_DEC
| SYSCLK_256FS
| WSPLL_25_50
,
53 REG_I2S
, I2S_IFMT_IIS
,
55 /* PON_HP & PON_DAC is enabled later */
56 REG_AMIX
, AMIX_RIGHT(0x3f) | AMIX_LEFT(0x3f),
58 REG_MASTER_VOL
, MASTER_VOL_LEFT(0x20) | MASTER_VOL_RIGHT(0x20),
60 REG_MIX_VOL
, MIX_VOL_CH_1(0) | MIX_VOL_CH_2(0xff),
63 /* Bass and tremble = 0 dB */
64 REG_MUTE
, MUTE_MASTER
| MUTE_CH2
,
65 /* Mute everything to start with */
66 REG_MIX_CTL
, MIX_CTL_MIX
,
76 /* Returns 0 if register was written or -1 if write failed */
77 int uda1380_write_reg(unsigned char reg
, unsigned short value
)
79 unsigned char data
[4];
81 data
[0] = UDA1380_ADDR
;
84 data
[3] = value
& 0xff;
86 if (i2c_write(1, data
, 4) != 4)
88 DEBUGF("uda1380 error reg=0x%x", reg
);
92 uda1380_regs
[reg
] = value
;
98 * Sets left and right master volume (0(max) to 252(muted))
100 int uda1380_set_master_vol(int vol_l
, int vol_r
)
102 return uda1380_write_reg(REG_MASTER_VOL
,
103 MASTER_VOL_LEFT(vol_l
) | MASTER_VOL_RIGHT(vol_r
));
107 * Sets mixer volume for both channels (0(max) to 228(muted))
109 int uda1380_set_mixer_vol(int channel1
, int channel2
)
111 return uda1380_write_reg(REG_MIX_VOL
,
112 MIX_VOL_CH_1(channel1
) | MIX_VOL_CH_2(channel2
));
116 * Sets the bass value (0-12)
118 void uda1380_set_bass(int value
)
120 uda1380_write_reg(REG_EQ
, (uda1380_regs
[REG_EQ
] & ~BASS_MASK
)
121 | BASSL(value
) | BASSR(value
));
125 * Sets the treble value (0-3)
127 void uda1380_set_treble(int value
)
129 uda1380_write_reg(REG_EQ
, (uda1380_regs
[REG_EQ
] & ~TREBLE_MASK
)
130 | TREBLEL(value
) | TREBLER(value
));
134 * Mute (mute=1) or enable sound (mute=0)
137 int uda1380_mute(int mute
)
139 unsigned int value
= uda1380_regs
[REG_MUTE
];
142 value
= value
| MUTE_MASTER
;
144 value
= value
& ~MUTE_MASTER
;
146 return uda1380_write_reg(REG_MUTE
, value
);
149 /* Returns 0 if successful or -1 if some register failed */
150 int uda1380_set_regs(void)
153 memset(uda1380_regs
, 0, sizeof(uda1380_regs
));
155 /* Initialize all registers */
156 for (i
=0; i
<NUM_DEFAULT_REGS
; i
++)
158 unsigned char reg
= uda1380_defaults
[i
*2+0];
159 unsigned short value
= uda1380_defaults
[i
*2+1];
161 if (uda1380_write_reg(reg
, value
) == -1)
168 /* Silently enable / disable audio output */
169 void uda1380_enable_output(bool enable
)
172 uda1380_write_reg(REG_PWR
, uda1380_regs
[REG_PWR
] | PON_DAC
| PON_HP
);
174 uda1380_write_reg(REG_MUTE
, MUTE_MASTER
);
175 uda1380_write_reg(REG_PWR
, uda1380_regs
[REG_PWR
] & ~PON_DAC
);
179 void uda1380_reset(void)
181 #ifdef IRIVER_H300_SERIES
182 int mask
= set_irq_level(HIGHEST_IRQ_LEVEL
);
183 pcf50606_write(0x3b, 0x00); /* GPOOD2 high Z */
184 pcf50606_write(0x3b, 0x07); /* GPOOD2 low */
188 or_l(1<<29, &GPIO_OUT
);
189 or_l(1<<29, &GPIO_ENABLE
);
190 or_l(1<<29, &GPIO_FUNCTION
);
192 and_l(~(1<<29), &GPIO_OUT
);
196 /* Initialize UDA1380 codec with default register values (uda1380_defaults) */
197 int uda1380_init(void)
204 if (uda1380_set_regs() == -1)
210 /* Nice shutdown of UDA1380 codec */
211 void uda1380_close(void)
213 /* First enable mute and sleep a while */
214 uda1380_write_reg(REG_MUTE
, MUTE_MASTER
);
217 /* Then power off the rest of the chip */
218 uda1380_write_reg(REG_PWR
, 0);
219 uda1380_write_reg(REG_0
, 0); /* Disable codec */
223 * Calling this function enables the UDA1380 to send
224 * sound samples over the I2S bus, which is connected
225 * to the processor's IIS1 interface.
227 * source_mic: true=record from microphone, false=record from line-in (or radio)
229 void uda1380_enable_recording(bool source_mic
)
231 uda1380_write_reg(REG_0
, uda1380_regs
[REG_0
] | EN_ADC
);
235 /* VGA_GAIN: 0=0 dB, F=30dB */
236 uda1380_write_reg(REG_PWR
, uda1380_regs
[REG_PWR
] | PON_LNA
| PON_ADCL
);
237 uda1380_write_reg(REG_ADC
, (uda1380_regs
[REG_ADC
] & VGA_GAIN_MASK
)
238 | SEL_LNA
| SEL_MIC
| EN_DCFIL
);
239 uda1380_write_reg(REG_PGA
, 0);
242 /* PGA_GAIN: 0=0 dB, F=24dB */
243 uda1380_write_reg(REG_PWR
, uda1380_regs
[REG_PWR
] | PON_PGAL
| PON_ADCL
244 | PON_PGAR
| PON_ADCR
);
245 uda1380_write_reg(REG_ADC
, EN_DCFIL
);
246 uda1380_write_reg(REG_PGA
, (uda1380_regs
[REG_PGA
] & PGA_GAIN_MASK
)
247 | PGA_GAINL(0) | PGA_GAINR(0));
252 uda1380_write_reg(REG_I2S
, uda1380_regs
[REG_I2S
] | I2S_MODE_MASTER
);
253 uda1380_write_reg(REG_MIX_CTL
, MIX_MODE(1));
258 * Stop sending samples on the I2S bus
260 void uda1380_disable_recording(void)
262 uda1380_write_reg(REG_PGA
, MUTE_ADC
);
265 uda1380_write_reg(REG_I2S
, I2S_IFMT_IIS
);
266 uda1380_write_reg(REG_PWR
, uda1380_regs
[REG_PWR
] & ~(PON_LNA
| PON_ADCL
267 | PON_ADCR
| PON_PGAL
269 uda1380_write_reg(REG_0
, uda1380_regs
[REG_0
] & ~EN_ADC
);
270 uda1380_write_reg(REG_ADC
, SKIP_DCFIL
);
274 * Set recording gain and volume
276 * type: params: ranges:
277 * AUDIO_GAIN_MIC: left -128 .. 108 -> -64 .. 54 dB gain
278 * AUDIO_GAIN_LINEIN left & right -128 .. 96 -> -64 .. 48 dB gain
280 * Note: - For all types the value 0 gives 0 dB gain.
281 * - order of setting both values determines if the small glitch will
282 be a peak or a dip. The small glitch is caused by the time between
283 setting the two gains
285 void uda1380_set_recvol(int left
, int right
, int type
)
287 int left_ag
, right_ag
;
292 left_ag
= MIN(MAX(0, left
/ 4), 15);
295 if(left
< recgain_mic
)
297 uda1380_write_reg(REG_DEC_VOL
, DEC_VOLL(left
)
299 uda1380_write_reg(REG_ADC
, (uda1380_regs
[REG_ADC
]
301 | VGA_GAIN(left_ag
));
305 uda1380_write_reg(REG_ADC
, (uda1380_regs
[REG_ADC
]
307 | VGA_GAIN(left_ag
));
308 uda1380_write_reg(REG_DEC_VOL
, DEC_VOLL(left
)
312 logf("Mic: %dA/%dD", left_ag
, left
);
315 case AUDIO_GAIN_LINEIN
:
316 left_ag
= MIN(MAX(0, left
/ 6), 8);
318 right_ag
= MIN(MAX(0, right
/ 6), 8);
319 right
-= right_ag
* 6;
321 if(left
< recgain_line
)
323 /* for this order we can combine both registers,
324 making the glitch even smaller */
325 unsigned char data
[6];
326 unsigned short value_dec
;
327 unsigned short value_pga
;
328 value_dec
= DEC_VOLL(left
) | DEC_VOLR(right
);
329 value_pga
= (uda1380_regs
[REG_PGA
] & ~PGA_GAIN_MASK
)
330 | PGA_GAINL(left_ag
) | PGA_GAINR(right_ag
);
332 data
[0] = UDA1380_ADDR
;
333 data
[1] = REG_DEC_VOL
;
334 data
[2] = value_dec
>> 8;
335 data
[3] = value_dec
& 0xff;
336 data
[4] = value_pga
>> 8;
337 data
[5] = value_pga
& 0xff;
339 if (i2c_write(1, data
, 6) != 6)
341 DEBUGF("uda1380 error reg=combi rec gain");
345 uda1380_regs
[REG_DEC_VOL
] = value_dec
;
346 uda1380_regs
[REG_PGA
] = value_pga
;
351 uda1380_write_reg(REG_PGA
, (uda1380_regs
[REG_PGA
]
354 | PGA_GAINR(right_ag
));
355 uda1380_write_reg(REG_DEC_VOL
, DEC_VOLL(left
)
360 logf("Line L: %dA/%dD", left_ag
, left
);
361 logf("Line R: %dA/%dD", right_ag
, right
);
368 * Enable or disable recording monitor (so one can listen to the recording)
371 void uda1380_set_monitor(int enable
)
373 if (enable
) /* enable channel 2 */
374 uda1380_write_reg(REG_MUTE
, uda1380_regs
[REG_MUTE
] & ~MUTE_CH2
);
375 else /* mute channel 2 */
376 uda1380_write_reg(REG_MUTE
, uda1380_regs
[REG_MUTE
] | MUTE_CH2
);
379 /* Change the order of the noise chaper,
380 5th order is recommended above 32kHz */
381 void uda1380_set_nsorder(int order
)
386 uda1380_write_reg(REG_MIX_CTL
, uda1380_regs
[REG_MIX_CTL
]
391 uda1380_write_reg(REG_MIX_CTL
, uda1380_regs
[REG_MIX_CTL
]