2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "wm8731.hpp"
|
2016-01-31 03:34:24 -05:00
|
|
|
#include "utility.hpp"
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
namespace wolfson {
|
|
|
|
namespace wm8731 {
|
|
|
|
|
2017-08-06 15:08:12 -04:00
|
|
|
void WM8731::configure_interface_i2s_slave() {
|
2023-05-18 16:16:05 -04:00
|
|
|
write(DigitalAudioInterfaceFormat{
|
|
|
|
.format = 2,
|
|
|
|
.iwl = 0,
|
|
|
|
.lrp = 0,
|
|
|
|
.lrswap = 0,
|
|
|
|
.ms = 0,
|
|
|
|
.bclkinv = 0,
|
|
|
|
.reserved0 = 0,
|
|
|
|
});
|
2017-08-06 15:08:12 -04:00
|
|
|
}
|
|
|
|
|
2017-08-06 15:43:39 -04:00
|
|
|
void WM8731::configure_interface_i2s_master() {
|
2023-05-18 16:16:05 -04:00
|
|
|
write(DigitalAudioInterfaceFormat{
|
|
|
|
.format = 2,
|
|
|
|
.iwl = 0,
|
|
|
|
.lrp = 0,
|
|
|
|
.lrswap = 0,
|
|
|
|
.ms = 1,
|
|
|
|
.bclkinv = 0,
|
|
|
|
.reserved0 = 0,
|
|
|
|
});
|
2017-08-06 15:43:39 -04:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
void WM8731::init() {
|
2023-05-18 16:16:05 -04:00
|
|
|
reset();
|
|
|
|
|
|
|
|
write(PowerDownControl{
|
|
|
|
.lineinpd = 1,
|
|
|
|
.micpd = 0,
|
|
|
|
.adcpd = 0,
|
|
|
|
.dacpd = 0,
|
|
|
|
.outpd = 0,
|
|
|
|
.oscpd = 1,
|
|
|
|
.clkoutpd = 1,
|
|
|
|
.poweroff = 0,
|
|
|
|
.reserved0 = 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// write(SamplingControl {
|
|
|
|
// .usb_normal = 0,
|
|
|
|
// .bosr = 0,
|
|
|
|
// .sr = 0,
|
|
|
|
// .clkidiv2 = 0,
|
|
|
|
// .clkodiv2 = 0,
|
|
|
|
// .reserved0 = 0,
|
|
|
|
// });
|
|
|
|
|
|
|
|
configure_interface_i2s_slave();
|
|
|
|
|
|
|
|
write(DigitalAudioPathControl{
|
|
|
|
.adchpd = 0,
|
|
|
|
.deemp = 0,
|
|
|
|
.dacmu = 0,
|
|
|
|
.hpor = 0,
|
|
|
|
.reserved0 = 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
write(AnalogAudioPathControl{
|
|
|
|
.micboost = 0, // Disable 20dB boost by default
|
|
|
|
.mutemic = 0, // Disable mute (unmute)
|
|
|
|
.insel = 1, // Microphone input to ADC
|
|
|
|
.bypass = 0,
|
|
|
|
.dacsel = 1,
|
|
|
|
.sidetone = 0,
|
|
|
|
.sideatt = 0,
|
|
|
|
.reserved0 = 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
write(ActiveControl{
|
|
|
|
.active = 1,
|
|
|
|
.reserved0 = 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// set_line_in_volume(0.0_dB);
|
|
|
|
headphone_mute();
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2017-05-31 14:34:16 -04:00
|
|
|
bool WM8731::detected() {
|
2023-05-18 16:16:05 -04:00
|
|
|
return reset();
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2017-05-31 14:28:00 -04:00
|
|
|
bool WM8731::reset() {
|
2023-05-18 16:16:05 -04:00
|
|
|
return write(0x0f, 0);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2017-05-31 14:28:00 -04:00
|
|
|
bool WM8731::write(const Register reg) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return write(toUType(reg), map.w[toUType(reg)]);
|
2015-12-14 16:57:45 -05:00
|
|
|
}
|
|
|
|
|
2017-05-31 14:28:00 -04:00
|
|
|
bool WM8731::write(const address_t reg_address, const reg_t value) {
|
2023-11-17 13:10:11 -05:00
|
|
|
map.w[reg_address] = value; // Save data written in case this fn is called from Debug->Peripherals app
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
const uint16_t word = (reg_address << 9) | value;
|
|
|
|
const std::array<uint8_t, 2> values{
|
|
|
|
static_cast<uint8_t>(word >> 8),
|
|
|
|
static_cast<uint8_t>(word & 0xff),
|
|
|
|
};
|
|
|
|
return bus.transmit(bus_address, values.data(), values.size());
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2023-11-17 13:10:11 -05:00
|
|
|
/* WM8731 is a write-only device; the read function only returns the value we last wrote */
|
2017-05-31 14:42:12 -04:00
|
|
|
uint32_t WM8731::reg_read(const size_t reg_address) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return map.w[reg_address];
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2023-11-17 13:10:11 -05:00
|
|
|
void WM8731::reg_write(const size_t reg_address, uint32_t value) {
|
|
|
|
write(reg_address, value);
|
|
|
|
}
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
void WM8731::write(const LeftLineIn value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
map.r.left_line_in = value;
|
|
|
|
write(Register::LeftLineIn);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WM8731::write(const RightLineIn value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
map.r.right_line_in = value;
|
|
|
|
write(Register::RightLineIn);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WM8731::write(const LeftHeadphoneOut value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
map.r.left_headphone_out = value;
|
|
|
|
write(Register::LeftHeadphoneOut);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WM8731::write(const RightHeadphoneOut value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
map.r.right_headphone_out = value;
|
|
|
|
write(Register::RightHeadphoneOut);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WM8731::write(const AnalogAudioPathControl value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
map.r.analog_audio_path_control = value;
|
|
|
|
write(Register::AnalogAudioPathControl);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WM8731::write(const DigitalAudioPathControl value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
map.r.digital_audio_path_control = value;
|
|
|
|
write(Register::DigitalAudioPathControl);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WM8731::write(const PowerDownControl value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
map.r.power_down_control = value;
|
|
|
|
write(Register::PowerDownControl);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WM8731::write(const DigitalAudioInterfaceFormat value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
map.r.digital_audio_interface_format = value;
|
|
|
|
write(Register::DigitalAudioInterfaceFormat);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WM8731::write(const SamplingControl value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
map.r.sampling_control = value;
|
|
|
|
write(Register::SamplingControl);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WM8731::write(const ActiveControl value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
map.r.active_control = value;
|
|
|
|
write(Register::ActiveControl);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
} /* namespace wm8731 */
|
|
|
|
} /* namespace wolfson */
|