Update preset.
[bertos.git] / bertos / drv / tas5706a.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief TAS5706A Power DAC i2c driver.
34  *
35  *
36  * \author Francesco Sacchi <batt@develer.com>
37  * \author Daniele Basile <asterix@develer.com>
38  */
39
40 #include "tas5706a.h"
41
42 #include "hw/hw_tas5706a.h"
43
44 #include "cfg/cfg_tas5706a.h"
45 #include "cfg/cfg_i2c.h"
46
47 #include <cfg/module.h>
48
49 #include <drv/i2c.h>
50 #include <drv/timer.h>
51
52 typedef uint8_t tas_addr_t;
53
54 #define TAS_ADDR 0x36
55
56 #define TRIM_REG   0x1B
57 #define SYS_REG2   0x05
58 #define VOLUME_REG 0x07
59 #define MUTE_VOL 0xFF
60
61 #define DB_TO_REG(db) ((24 - (db)) * 2)
62
63 #define CH1_VOL_REG 0x08
64 #define CH2_VOL_REG 0x09
65 #define CH3_VOL_REG 0x0A
66 #define CH4_VOL_REG 0x0B
67
68
69 INLINE bool tas5706a_putc(I2c *i2c, tas_addr_t addr, uint8_t ch)
70 {
71         i2c_start_w(i2c, TAS_ADDR, 2, I2C_STOP);
72         i2c_putc(i2c, addr);
73         i2c_putc(i2c, ch);
74
75         if (i2c_error(i2c))
76                 return false;
77
78         return true;
79 }
80
81 INLINE int tas5706a_getc(I2c *i2c, tas_addr_t addr)
82 {
83         int ch;
84
85         i2c_start_w(i2c, TAS_ADDR, 2, I2C_NOSTOP);
86         i2c_putc(i2c, addr);
87         i2c_start_r(i2c, TAS_ADDR, 1, I2C_STOP);
88         ch = (int)(uint8_t)i2c_getc(i2c);
89
90         if (i2c_error(i2c))
91                 return EOF;
92
93         return ch;
94 }
95
96 void tas5706a_setVolume_3(I2c *i2c, Tas5706aCh ch, tas5706a_vol_t vol)
97 {
98         ASSERT(ch < TAS_CNT);
99         ASSERT(vol <= TAS_VOL_MAX);
100
101         tas_addr_t addr1, addr2;
102
103         switch(ch)
104         {
105                 case TAS_CH1:
106                         addr1 = CH1_VOL_REG;
107                         addr2 = CH3_VOL_REG;
108                         break;
109                 case TAS_CH2:
110                         addr1 = CH2_VOL_REG;
111                         addr2 = CH4_VOL_REG;
112                         break;
113                 default:
114                         ASSERT(0);
115                         return;
116         }
117
118         uint8_t vol_att = 0xff - ((vol * 0xff) / TAS_VOL_MAX);
119
120         tas5706a_putc(i2c, addr1, vol_att);
121         tas5706a_putc(i2c, addr2, vol_att);
122 }
123
124 void tas5706a_setLowPower_2(I2c *i2c, bool val)
125 {
126         ASSERT(i2c);
127
128         TAS5706A_SETPOWERDOWN(val);
129         TAS5706A_SETMUTE(val);
130 }
131
132
133 void tas5706a_init_1(I2c *i2c)
134 {
135         ASSERT(i2c);
136         MOD_CHECK(timer);
137
138         TAS5706A_PIN_INIT();
139         timer_delay(200);
140         TAS5706A_SETPOWERDOWN(false);
141         TAS5706A_SETMUTE(false);
142         TAS5706A_MCLK_INIT();
143         timer_delay(2);
144         TAS5706A_SETRESET(false);
145         timer_delay(20);
146         tas5706a_putc(i2c, TRIM_REG, 0x00);
147
148         tas5706a_putc(i2c, VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL));
149
150         /* Unmute */
151         tas5706a_putc(i2c, SYS_REG2, 0);
152 }