diff options
Diffstat (limited to 'firmware/sound.c')
-rw-r--r-- | firmware/sound.c | 158 |
1 files changed, 144 insertions, 14 deletions
diff --git a/firmware/sound.c b/firmware/sound.c index 9ab545f51d..0516987e26 100644 --- a/firmware/sound.c +++ b/firmware/sound.c | |||
@@ -63,6 +63,7 @@ static const struct sound_settings_info sound_settings_table[] = { | |||
63 | #elif defined(HAVE_UDA1380) | 63 | #elif defined(HAVE_UDA1380) |
64 | [SOUND_BASS] = {"dB", 0, 2, 0, 24, 0, sound_set_bass}, | 64 | [SOUND_BASS] = {"dB", 0, 2, 0, 24, 0, sound_set_bass}, |
65 | [SOUND_TREBLE] = {"dB", 0, 2, 0, 6, 0, sound_set_treble}, | 65 | [SOUND_TREBLE] = {"dB", 0, 2, 0, 6, 0, sound_set_treble}, |
66 | [SOUND_SCALING] = {"", 0, 1, 0, 1, 0, sound_set_scaling}, | ||
66 | #else /* MAS3507D */ | 67 | #else /* MAS3507D */ |
67 | [SOUND_BASS] = {"dB", 0, 1, -15, 15, 7, sound_set_bass}, | 68 | [SOUND_BASS] = {"dB", 0, 1, -15, 15, 7, sound_set_bass}, |
68 | [SOUND_TREBLE] = {"dB", 0, 1, -15, 15, 7, sound_set_treble}, | 69 | [SOUND_TREBLE] = {"dB", 0, 1, -15, 15, 7, sound_set_treble}, |
@@ -251,6 +252,20 @@ static int tenthdb2master(int db) { | |||
251 | else /* 0.25 dB steps */ | 252 | else /* 0.25 dB steps */ |
252 | return -db * 2 / 5; | 253 | return -db * 2 / 5; |
253 | } | 254 | } |
255 | |||
256 | static int tenthdb2mixer(int db) { | ||
257 | if (db < -720) | ||
258 | return 228; | ||
259 | else if (db < -660) | ||
260 | return 224; | ||
261 | else if (db < -600) | ||
262 | return 216; | ||
263 | else if (db < -470) | ||
264 | return (460 - db) / 5; | ||
265 | else | ||
266 | return -db * 2 / 5; | ||
267 | } | ||
268 | |||
254 | #endif | 269 | #endif |
255 | 270 | ||
256 | #if (CONFIG_CODEC == MAS3507D) || defined HAVE_UDA1380 | 271 | #if (CONFIG_CODEC == MAS3507D) || defined HAVE_UDA1380 |
@@ -263,6 +278,89 @@ int current_balance = 0; /* -960..+960 -840..+840 */ | |||
263 | int current_treble = 0; /* -150..+150 0.. +60 */ | 278 | int current_treble = 0; /* -150..+150 0.. +60 */ |
264 | int current_bass = 0; /* -150..+150 0..+240 */ | 279 | int current_bass = 0; /* -150..+150 0..+240 */ |
265 | 280 | ||
281 | #ifdef HAVE_UDA1380 | ||
282 | |||
283 | #define MODIFYING_VOLUME 0 | ||
284 | #define MODIFYING_BASS 1 | ||
285 | #define MODIFYING_BALANCE 2 | ||
286 | #define MODIFYING_NONE 3 | ||
287 | |||
288 | int current_scaling_mode = SOUND_SCALE_VOLUME; | ||
289 | int current_start_l_r = VOLUME_MAX; | ||
290 | |||
291 | static void set_prescaled_volume_and_bass(int scaling_mode, bool just_balance) | ||
292 | { | ||
293 | int l = VOLUME_MAX, r = VOLUME_MAX, prescale; | ||
294 | int new_bass = 0, new_volume = 0; | ||
295 | |||
296 | if(scaling_mode == SOUND_SCALE_OFF) | ||
297 | { | ||
298 | if (current_volume > VOLUME_MAX) | ||
299 | current_volume = VOLUME_MAX; | ||
300 | new_bass = (current_bass/10) >> 1; | ||
301 | new_volume = tenthdb2mixer(current_volume); | ||
302 | |||
303 | } | ||
304 | else if(scaling_mode == SOUND_SCALE_BASS) | ||
305 | { | ||
306 | if (current_volume > VOLUME_MAX) | ||
307 | current_volume = VOLUME_MAX; | ||
308 | |||
309 | prescale = ((VOLUME_MAX - current_volume) / 20); | ||
310 | |||
311 | if ((current_bass + current_volume) <= VOLUME_MAX) | ||
312 | new_bass = current_bass / 20; | ||
313 | else | ||
314 | new_bass = prescale; /* limit bass with volume */ | ||
315 | |||
316 | new_volume = tenthdb2mixer(current_volume); | ||
317 | |||
318 | } | ||
319 | else if(scaling_mode == SOUND_SCALE_VOLUME) | ||
320 | { | ||
321 | prescale = current_bass; | ||
322 | if (prescale < 0) | ||
323 | prescale = 0; | ||
324 | |||
325 | new_bass = (current_bass/10) >> 1; | ||
326 | new_volume = prescale*2/5; | ||
327 | |||
328 | if (current_volume + prescale > VOLUME_MAX) | ||
329 | prescale = VOLUME_MAX - current_volume; | ||
330 | |||
331 | l = r = current_volume + prescale; | ||
332 | } | ||
333 | else if(scaling_mode == SOUND_SCALE_CURRENT) | ||
334 | { | ||
335 | l = r = current_start_l_r; | ||
336 | |||
337 | } | ||
338 | |||
339 | if(!just_balance) | ||
340 | { | ||
341 | uda1380_set_bass(new_bass); | ||
342 | uda1380_set_mixer_vol(new_volume, new_volume); | ||
343 | } | ||
344 | |||
345 | current_start_l_r = l; | ||
346 | |||
347 | if (current_balance > 0) { | ||
348 | l -= current_balance; | ||
349 | if (l < VOLUME_MIN) | ||
350 | l = VOLUME_MIN; | ||
351 | } | ||
352 | if (current_balance < 0) { | ||
353 | r += current_balance; | ||
354 | if (r < VOLUME_MIN) | ||
355 | r = VOLUME_MIN; | ||
356 | } | ||
357 | |||
358 | uda1380_set_master_vol(tenthdb2master(l), tenthdb2master(r)); | ||
359 | } | ||
360 | |||
361 | #endif | ||
362 | |||
363 | #if CONFIG_CODEC == MAS3507D | ||
266 | static void set_prescaled_volume(void) | 364 | static void set_prescaled_volume(void) |
267 | { | 365 | { |
268 | int prescale; | 366 | int prescale; |
@@ -273,12 +371,7 @@ static void set_prescaled_volume(void) | |||
273 | prescale = 0; /* no need to prescale if we don't boost | 371 | prescale = 0; /* no need to prescale if we don't boost |
274 | bass or treble */ | 372 | bass or treble */ |
275 | 373 | ||
276 | #if CONFIG_CODEC == MAS3507D | ||
277 | mas_writereg(MAS_REG_KPRESCALE, prescale_table[prescale/10]); | 374 | mas_writereg(MAS_REG_KPRESCALE, prescale_table[prescale/10]); |
278 | #else /* UDA1380 */ | ||
279 | uda1380_set_mixer_vol(prescale*2/5, prescale*2/5); | ||
280 | /* The needed range of 0..-24 dB is fortunately linear */ | ||
281 | #endif | ||
282 | 375 | ||
283 | /* gain up the analog volume to compensate the prescale gain reduction, | 376 | /* gain up the analog volume to compensate the prescale gain reduction, |
284 | * but limit to the possible maximum */ | 377 | * but limit to the possible maximum */ |
@@ -299,12 +392,28 @@ static void set_prescaled_volume(void) | |||
299 | r = VOLUME_MIN; | 392 | r = VOLUME_MIN; |
300 | } | 393 | } |
301 | 394 | ||
302 | #if CONFIG_CODEC == MAS3507D | ||
303 | dac_volume(tenthdb2reg(l), tenthdb2reg(r), false); | 395 | dac_volume(tenthdb2reg(l), tenthdb2reg(r), false); |
304 | #else /* UDA1380 */ | 396 | } |
305 | uda1380_set_master_vol(tenthdb2master(l), tenthdb2master(r)); | ||
306 | #endif | 397 | #endif |
398 | |||
399 | #ifdef HAVE_UDA1380 | ||
400 | static void set_prescaled_volume(int modifying) | ||
401 | { | ||
402 | if (modifying == MODIFYING_BALANCE) | ||
403 | set_prescaled_volume_and_bass(current_scaling_mode,true); | ||
404 | else if (current_scaling_mode == SOUND_SCALE_OFF) | ||
405 | set_prescaled_volume_and_bass(current_scaling_mode,false); | ||
406 | else if(( (modifying == MODIFYING_BASS) | ||
407 | && (current_scaling_mode == SOUND_SCALE_CURRENT)) || | ||
408 | (current_scaling_mode == SOUND_SCALE_VOLUME)) | ||
409 | set_prescaled_volume_and_bass(SOUND_SCALE_VOLUME,false); | ||
410 | else if(( (modifying == MODIFYING_VOLUME) | ||
411 | && (current_scaling_mode == SOUND_SCALE_CURRENT)) || | ||
412 | (current_scaling_mode == SOUND_SCALE_BASS)) | ||
413 | set_prescaled_volume_and_bass(SOUND_SCALE_BASS,false); | ||
307 | } | 414 | } |
415 | #endif | ||
416 | |||
308 | #endif /* (CONFIG_CODEC == MAS3507D) || defined HAVE_UDA1380 */ | 417 | #endif /* (CONFIG_CODEC == MAS3507D) || defined HAVE_UDA1380 */ |
309 | #endif /* !SIMULATOR */ | 418 | #endif /* !SIMULATOR */ |
310 | 419 | ||
@@ -402,9 +511,12 @@ void sound_set_volume(int value) | |||
402 | #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) | 511 | #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) |
403 | int tmp = 0x7f00 * value / 100; | 512 | int tmp = 0x7f00 * value / 100; |
404 | mas_codec_writereg(0x10, tmp & 0xff00); | 513 | mas_codec_writereg(0x10, tmp & 0xff00); |
405 | #elif (CONFIG_CODEC == MAS3507D) || defined HAVE_UDA1380 | 514 | #elif (CONFIG_CODEC == MAS3507D) |
515 | current_volume = VOLUME_MIN + (value * VOLUME_RANGE / 100); | ||
516 | set_prescaled_volume(); /* tenth of dB */ | ||
517 | #elif defined HAVE_UDA1380 | ||
406 | current_volume = VOLUME_MIN + (value * VOLUME_RANGE / 100); | 518 | current_volume = VOLUME_MIN + (value * VOLUME_RANGE / 100); |
407 | set_prescaled_volume(); /* tenth of dB */ | 519 | set_prescaled_volume(MODIFYING_VOLUME); /* tenth of dB */ |
408 | #elif (CONFIG_CPU == PP5020) | 520 | #elif (CONFIG_CPU == PP5020) |
409 | /* TODO: Implement sound_set_volume() */ | 521 | /* TODO: Implement sound_set_volume() */ |
410 | (void)value; | 522 | (void)value; |
@@ -418,9 +530,12 @@ void sound_set_balance(int value) | |||
418 | #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) | 530 | #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) |
419 | int tmp = ((value * 127 / 100) & 0xff) << 8; | 531 | int tmp = ((value * 127 / 100) & 0xff) << 8; |
420 | mas_codec_writereg(0x11, tmp & 0xff00); | 532 | mas_codec_writereg(0x11, tmp & 0xff00); |
421 | #elif CONFIG_CODEC == MAS3507D || defined HAVE_UDA1380 | 533 | #elif CONFIG_CODEC == MAS3507D |
422 | current_balance = value * VOLUME_RANGE / 100; /* tenth of dB */ | 534 | current_balance = value * VOLUME_RANGE / 100; /* tenth of dB */ |
423 | set_prescaled_volume(); | 535 | set_prescaled_volume(); |
536 | #elif defined HAVE_UDA1380 | ||
537 | current_balance = value * VOLUME_RANGE / 100; /* tenth of dB */ | ||
538 | set_prescaled_volume(MODIFYING_BALANCE); | ||
424 | #elif (CONFIG_CPU == PP5020) | 539 | #elif (CONFIG_CPU == PP5020) |
425 | /* TODO: Implement sound_set_balance() */ | 540 | /* TODO: Implement sound_set_balance() */ |
426 | (void)value; | 541 | (void)value; |
@@ -439,9 +554,8 @@ void sound_set_bass(int value) | |||
439 | current_bass = value * 10; | 554 | current_bass = value * 10; |
440 | set_prescaled_volume(); | 555 | set_prescaled_volume(); |
441 | #elif defined(HAVE_UDA1380) | 556 | #elif defined(HAVE_UDA1380) |
442 | uda1380_set_bass(value >> 1); | ||
443 | current_bass = value * 10; | 557 | current_bass = value * 10; |
444 | set_prescaled_volume(); | 558 | set_prescaled_volume(MODIFYING_BASS); |
445 | #elif (CONFIG_CPU == PP5020) | 559 | #elif (CONFIG_CPU == PP5020) |
446 | /* TODO: Implement sound_set_bass() */ | 560 | /* TODO: Implement sound_set_bass() */ |
447 | (void)value; | 561 | (void)value; |
@@ -462,7 +576,6 @@ void sound_set_treble(int value) | |||
462 | #elif defined(HAVE_UDA1380) | 576 | #elif defined(HAVE_UDA1380) |
463 | uda1380_set_treble(value >> 1); | 577 | uda1380_set_treble(value >> 1); |
464 | current_treble = value * 10; | 578 | current_treble = value * 10; |
465 | set_prescaled_volume(); | ||
466 | #elif (CONFIG_CPU == PP5020) | 579 | #elif (CONFIG_CPU == PP5020) |
467 | /* TODO: Implement sound_set_treble() */ | 580 | /* TODO: Implement sound_set_treble() */ |
468 | (void)value; | 581 | (void)value; |
@@ -486,6 +599,16 @@ void sound_set_stereo_width(int value) | |||
486 | set_channel_config(); | 599 | set_channel_config(); |
487 | } | 600 | } |
488 | 601 | ||
602 | #ifdef HAVE_UDA1380 | ||
603 | void sound_set_scaling(int value) | ||
604 | { | ||
605 | current_scaling_mode = value; | ||
606 | if(!audio_is_initialized) | ||
607 | return; | ||
608 | set_prescaled_volume(MODIFYING_NONE); | ||
609 | } | ||
610 | #endif | ||
611 | |||
489 | #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) | 612 | #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) |
490 | void sound_set_loudness(int value) | 613 | void sound_set_loudness(int value) |
491 | { | 614 | { |
@@ -603,6 +726,13 @@ void sound_set_stereo_width(int value) | |||
603 | (void)value; | 726 | (void)value; |
604 | } | 727 | } |
605 | 728 | ||
729 | #ifdef HAVE_UDA1380 | ||
730 | void sound_set_scaling(int value) | ||
731 | { | ||
732 | (void)value; | ||
733 | } | ||
734 | #endif | ||
735 | |||
606 | #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) | 736 | #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) |
607 | void sound_set_loudness(int value) | 737 | void sound_set_loudness(int value) |
608 | { | 738 | { |