Add support for new api.
[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  */
38
39 #include "tas5706a.h"
40
41 #include "hw/hw_tas5706a.h"
42
43 #include "cfg/cfg_tas5706a.h"
44
45 #include <cfg/module.h>
46
47 #include <drv/i2c.h>
48 #include <drv/timer.h>
49
50 typedef uint8_t tas_addr_t;
51
52 #define TAS_ADDR 0x36
53
54 #define TRIM_REG   0x1B
55 #define SYS_REG2   0x05
56 #define VOLUME_REG 0x07
57 #define MUTE_VOL 0xFF
58
59 #define DB_TO_REG(db) ((24 - (db)) * 2)
60
61 #define CH1_VOL_REG 0x08
62 #define CH2_VOL_REG 0x09
63 #define CH3_VOL_REG 0x0A
64 #define CH4_VOL_REG 0x0B
65
66 static bool tas5706a_send(tas_addr_t addr, const void *buf, size_t len)
67 {
68         bool ret = i2c_start_w(TAS_ADDR) && i2c_put(addr) && i2c_send(buf, len);
69         i2c_stop();
70         return ret;
71 }
72
73 INLINE bool tas5706a_put(tas_addr_t addr, uint8_t ch)
74 {
75         return tas5706a_send(addr, &ch, sizeof(ch));
76 }
77
78 static bool tas5706a_recv(tas_addr_t addr, void *buf, size_t len)
79 {
80         bool ret = i2c_start_w(TAS_ADDR) && i2c_put(addr) && i2c_start_r(TAS_ADDR) && i2c_recv(buf, len);
81         i2c_stop();
82         return ret;
83 }
84
85 INLINE int tas5706a_get(tas_addr_t addr)
86 {
87         uint8_t ch;
88         if (tas5706a_recv(addr, &ch, sizeof(ch)))
89                 return (int)(uint8_t)ch;
90         else
91                 return EOF;
92 }
93
94 void tas5706a_init_0(void)
95 {
96         MOD_CHECK(i2c);
97         MOD_CHECK(timer);
98         TAS5706A_PIN_INIT();
99         timer_delay(200);
100         TAS5706A_SETPOWERDOWN(false);
101         TAS5706A_SETMUTE(false);
102         TAS5706A_MCLK_INIT();
103         timer_delay(2);
104         TAS5706A_SETRESET(false);
105         timer_delay(20);
106         tas5706a_put(TRIM_REG, 0x00);
107
108         tas5706a_put(VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL));
109
110         /* Unmute */
111         tas5706a_put(SYS_REG2, 0);
112 }
113
114 void tas5706a_setVolume_2(Tas5706aCh ch, tas5706a_vol_t vol)
115 {
116         ASSERT(ch < TAS_CNT);
117         ASSERT(vol <= TAS_VOL_MAX);
118
119         tas_addr_t addr1, addr2;
120
121         switch(ch)
122         {
123                 case TAS_CH1:
124                         addr1 = CH1_VOL_REG;
125                         addr2 = CH3_VOL_REG;
126                         break;
127                 case TAS_CH2:
128                         addr1 = CH2_VOL_REG;
129                         addr2 = CH4_VOL_REG;
130                         break;
131                 default:
132                         ASSERT(0);
133                         return;
134         }
135
136         uint8_t vol_att = 0xff - ((vol * 0xff) / TAS_VOL_MAX);
137
138         tas5706a_put(addr1, vol_att);
139         tas5706a_put(addr2, vol_att);
140 }
141
142 void tas5706a_setLowPower_1(bool val)
143 {
144         TAS5706A_SETPOWERDOWN(val);
145         TAS5706A_SETMUTE(val);
146 }
147
148 /*
149  * New API
150  */
151
152 INLINE bool tas5706a_putc(I2c *i2c, tas_addr_t addr, uint8_t ch)
153 {
154         i2c_start_w(i2c, TAS_ADDR, 2, I2C_STOP);
155         i2c_putc(i2c, addr);
156         i2c_putc(i2c, ch);
157
158         if (i2c_error(i2c))
159                 return false;
160
161         return true;
162 }
163
164 void tas5706a_setVolume_3(I2c *i2c, Tas5706aCh ch, tas5706a_vol_t vol)
165 {
166         ASSERT(ch < TAS_CNT);
167         ASSERT(vol <= TAS_VOL_MAX);
168
169         tas_addr_t addr1, addr2;
170
171         switch(ch)
172         {
173                 case TAS_CH1:
174                         addr1 = CH1_VOL_REG;
175                         addr2 = CH3_VOL_REG;
176                         break;
177                 case TAS_CH2:
178                         addr1 = CH2_VOL_REG;
179                         addr2 = CH4_VOL_REG;
180                         break;
181                 default:
182                         ASSERT(0);
183                         return;
184         }
185
186         uint8_t vol_att = 0xff - ((vol * 0xff) / TAS_VOL_MAX);
187
188         tas5706a_putc(i2c, addr1, vol_att);
189         tas5706a_putc(i2c, addr2, vol_att);
190 }
191
192 void tas5706a_setLowPower_2(I2c *i2c, bool val)
193 {
194         ASSERT(i2c);
195
196         TAS5706A_SETPOWERDOWN(val);
197         TAS5706A_SETMUTE(val);
198 }
199
200
201 void tas5706a_init_1(I2c *i2c)
202 {
203         ASSERT(i2c);
204         MOD_CHECK(timer);
205
206         TAS5706A_PIN_INIT();
207         timer_delay(200);
208         TAS5706A_SETPOWERDOWN(false);
209         TAS5706A_SETMUTE(false);
210         TAS5706A_MCLK_INIT();
211         timer_delay(2);
212         TAS5706A_SETRESET(false);
213         timer_delay(20);
214         tas5706a_putc(i2c, TRIM_REG, 0x00);
215
216         tas5706a_putc(i2c, VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL));
217
218         /* Unmute */
219         tas5706a_putc(i2c, SYS_REG2, 0);
220 }