Skip to content

Commit 6d54a48

Browse files
committed
Implement a user-space driver for headphones amplifier TP16130 (V5).
1 parent f9546b3 commit 6d54a48

5 files changed

+163
-2
lines changed

CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,18 @@ if (("$ENV{ZYNTHIAN_WIRING_LAYOUT}" STREQUAL "Z2_V1") OR ("$ENV{ZYNTHIAN_WIRING_
199199

200200
elseif ("$ENV{ZYNTHIAN_WIRING_LAYOUT}" STREQUAL "V5")
201201
message("++ Using wiringPi")
202-
add_library(zyncore SHARED zyncore.c zyncontrol.h zyncontrol_v5.c zynpot.h zynpot.c zynrv112.h zynrv112.c zynads1115.h zynads1115.c zynmcp23017.h zynmcp23017.c zyncoder.h zyncoder.c zynmidirouter.h zynmidirouter.c zynmaster.h zynmaster.c)
202+
add_library(zyncore SHARED zyncore.c zyncontrol.h zyncontrol_v5.c tpa6130.c tpa6130.c zynpot.h zynpot.c zynrv112.h zynrv112.c zynads1115.h zynads1115.c zynmcp23017.h zynmcp23017.c zyncoder.h zyncoder.c zynmidirouter.h zynmidirouter.c zynmaster.h zynmaster.c)
203203
target_link_libraries(zyncore wiringPi jack lo)
204+
add_executable(tpa6130_set_volume tpa6130_set_volume.c tpa6130.c)
205+
target_link_libraries(tpa6130_set_volume wiringPi)
204206

205207
elseif ("$ENV{ZYNTHIAN_WIRING_LAYOUT}" STREQUAL "V5_ZYNFACE")
206208
message("++ Using wiringPi")
207209
message("++ Building Zynaptik support")
208-
add_library(zyncore SHARED zyncore.c zyncontrol.h zyncontrol_v5.c zynpot.h zynpot.c zynrv112.h zynrv112.c zynads1115.h zynads1115.c zynmcp23017.h zynmcp23017.c zyncoder.h zyncoder.c zynmidirouter.h zynmidirouter.c zynmaster.h zynmaster.c zynaptik.h zynaptik.c)
210+
add_library(zyncore SHARED zyncore.c zyncontrol.h zyncontrol_v5.c tpa6130.c tpa6130.c zynrv112.h zynrv112.c zynads1115.h zynads1115.c zynmcp23017.h zynmcp23017.c zyncoder.h zyncoder.c zynmidirouter.h zynmidirouter.c zynmaster.h zynmaster.c zynaptik.h zynaptik.c)
209211
target_link_libraries(zyncore wiringPi jack MCP4728 lo)
212+
add_executable(tpa6130_set_volume tpa6130_set_volume.c tpa6130.c)
213+
target_link_libraries(tpa6130_set_volume wiringPi)
210214

211215
elseif (NOT ZYNTHIAN_FORCE_WIRINGPI_EMU AND HAVE_WIRINGPI_LIB)
212216
message("++ Using wiringPi")

tpa6130.c

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* ******************************************************************
3+
* ZYNTHIAN PROJECT: Headphones Volume Control for TPA6130 amplifier
4+
*
5+
* Library for interfacing the TPA6130 headphones amplifier.
6+
* It implements the volume control using I2C
7+
*
8+
* Copyright (C) 2015-2023 Fernando Moyano <[email protected]>
9+
*
10+
* ******************************************************************
11+
*
12+
* This program is free software; you can redistribute it and/or
13+
* modify it under the terms of the GNU General Public License as
14+
* published by the Free Software Foundation; either version 2 of
15+
* the License, or any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
*
22+
* For a full copy of the GNU General Public License see the LICENSE.txt file.
23+
*
24+
* ******************************************************************
25+
*/
26+
27+
#include <wiringPi.h>
28+
#include <wiringPiI2C.h>
29+
#include "tpa6130.h"
30+
31+
//-------------------------------------------------------------------
32+
33+
//#define DEBUG
34+
35+
#define TPA6130_I2C_ADDRESS 0x60
36+
#define AMP_MAX_VOL 0x3F // 0-63
37+
38+
int tpa6130_fd = 0x0;
39+
40+
//-------------------------------------------------------------------
41+
42+
uint8_t tpa6130_set_volume(uint8_t vol) {
43+
wiringPiI2CWriteReg8(tpa6130_fd, 0x2, AMP_MAX_VOL & vol);
44+
return vol;
45+
}
46+
47+
uint8_t tpa6130_get_volume() {
48+
return wiringPiI2CReadReg8(tpa6130_fd, 0x2) & 0x3C;
49+
}
50+
51+
uint8_t tpa6130_get_volume_max() {
52+
return AMP_MAX_VOL;
53+
}
54+
55+
void tpa6130_init() {
56+
tpa6130_fd = wiringPiI2CSetup(TPA6130_I2C_ADDRESS);
57+
wiringPiI2CWriteReg8(tpa6130_fd, 0x1, 0xC0);
58+
tpa6130_set_volume(20);
59+
}
60+
61+
void tpa6130_end() {
62+
wiringPiI2CWriteReg8(tpa6130_fd, 0x1, 0x00);
63+
}
64+
65+
//-------------------------------------------------------------------

tpa6130.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* ******************************************************************
3+
* ZYNTHIAN PROJECT: Headphones Volume Control for TPA6130 amplifier
4+
*
5+
* Library for interfacing the TPA6130 headphones amplifier.
6+
* It implements the volume control using I2C
7+
*
8+
* Copyright (C) 2015-2023 Fernando Moyano <[email protected]>
9+
*
10+
* ******************************************************************
11+
*
12+
* This program is free software; you can redistribute it and/or
13+
* modify it under the terms of the GNU General Public License as
14+
* published by the Free Software Foundation; either version 2 of
15+
* the License, or any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
*
22+
* For a full copy of the GNU General Public License see the LICENSE.txt file.
23+
*
24+
* ******************************************************************
25+
*/
26+
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#include <stdint.h>
30+
#include <unistd.h>
31+
32+
//-------------------------------------------------------------------
33+
34+
uint8_t tpa6130_set_volume(uint8_t vol);
35+
uint8_t tpa6130_get_volume();
36+
uint8_t tpa6130_get_volume_max();
37+
void tpa6130_init();
38+
void tpa6130_end();
39+
40+
//-------------------------------------------------------------------

tpa6130_set_volume.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* ******************************************************************
3+
* ZYNTHIAN PROJECT: Headphones Volume Control for TPA6130 amplifier
4+
*
5+
* Basic CLI for setting volume
6+
* Usage:
7+
* tpa6130_set_volume [0-15]
8+
*
9+
* Copyright (C) 2015-2023 Fernando Moyano <[email protected]>
10+
*
11+
* ******************************************************************
12+
*
13+
* This program is free software; you can redistribute it and/or
14+
* modify it under the terms of the GNU General Public License as
15+
* published by the Free Software Foundation; either version 2 of
16+
* the License, or any later version.
17+
*
18+
* This program is distributed in the hope that it will be useful,
19+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
* GNU General Public License for more details.
22+
*
23+
* For a full copy of the GNU General Public License see the LICENSE.txt file.
24+
*
25+
* ******************************************************************
26+
*/
27+
28+
#include <wiringPi.h>
29+
#include "tpa6130.h"
30+
31+
//-------------------------------------------------------------------
32+
33+
int main(int argc, char *argv[]) {
34+
uint8_t vol = 10;
35+
if (argc>=2) {
36+
vol = atoi(argv[1]);
37+
}
38+
wiringPiSetup();
39+
tpa6130_init();
40+
printf("Setting TPA6130 volume to %d\n", vol);
41+
tpa6130_set_volume(vol);
42+
//tpa6130_end();
43+
}
44+
45+
//-------------------------------------------------------------------

zyncontrol_v5.c

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "zynpot.h"
3030
#include "zyncoder.h"
31+
#include "tpa6130.h"
3132

3233
//-----------------------------------------------------------------------------
3334
// Zynface V5
@@ -132,8 +133,13 @@ void end_zynpots() {
132133
// Zyncontrol Initialization
133134
//-----------------------------------------------------------------------------
134135

136+
uint8_t set_hpvol(uint8_t vol) { return tpa6130_set_volume(vol); }
137+
uint8_t get_hpvol() { return tpa6130_get_volume(); }
138+
uint8_t get_hpvol_max() { return tpa6130_get_volume_max(); }
139+
135140
int init_zyncontrol() {
136141
wiringPiSetup();
142+
tpa6130_init();
137143
init_zynmcp23017s();
138144
init_zynswitches();
139145
init_zynpots();
@@ -151,6 +157,7 @@ int end_zyncontrol() {
151157
reset_zyncoders();
152158
reset_zynswitches();
153159
reset_zynmcp23017s();
160+
tpa6130_end();
154161
return 1;
155162
}
156163

0 commit comments

Comments
 (0)