Remove from wizart i2c backend selection. Add deprecate switch to disable old i2c...
[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 #if !CONFIG_I2C_DISABLE_OLD_API
70
71 static bool tas5706a_send(tas_addr_t addr, const void *buf, size_t len)
72 {
73         bool ret = i2c_start_w(TAS_ADDR) && i2c_put(addr) && i2c_send(buf, len);
74         i2c_stop();
75         return ret;
76 }
77
78 INLINE bool tas5706a_put(tas_addr_t addr, uint8_t ch)
79 {
80         return tas5706a_send(addr, &ch, sizeof(ch));
81 }
82
83 static bool tas5706a_recv(tas_addr_t addr, void *buf, size_t len)
84 {
85         bool ret = i2c_start_w(TAS_ADDR) && i2c_put(addr) && i2c_start_r(TAS_ADDR) && i2c_recv(buf, len);
86         i2c_stop();
87         return ret;
88 }
89
90 INLINE int tas5706a_get(tas_addr_t addr)
91 {
92         uint8_t ch;
93         if (tas5706a_recv(addr, &ch, sizeof(ch)))
94                 return (int)(uint8_t)ch;
95         else
96                 return EOF;
97 }
98
99 void tas5706a_init_0(void)
100 {
101         MOD_CHECK(i2c);
102         MOD_CHECK(timer);
103         TAS5706A_PIN_INIT();
104         timer_delay(200);
105         TAS5706A_SETPOWERDOWN(false);
106         TAS5706A_SETMUTE(false);
107         TAS5706A_MCLK_INIT();
108         timer_delay(2);
109         TAS5706A_SETRESET(false);
110         timer_delay(20);
111         tas5706a_put(TRIM_REG, 0x00);
112
113         tas5706a_put(VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL));
114
115         /* Unmute */
116         tas5706a_put(SYS_REG2, 0);
117 }
118
119 void tas5706a_setVolume_2(Tas5706aCh ch, tas5706a_vol_t vol)
120 {
121         ASSERT(ch < TAS_CNT);
122         ASSERT(vol <= TAS_VOL_MAX);
123
124         tas_addr_t addr1, addr2;
125
126         switch(ch)
127         {
128                 case TAS_CH1:
129                         addr1 = CH1_VOL_REG;
130                         addr2 = CH3_VOL_REG;
131                         break;
132                 case TAS_CH2:
133                         addr1 = CH2_VOL_REG;
134                         addr2 = CH4_VOL_REG;
135                         break;
136                 default:
137                         ASSERT(0);
138                         return;
139         }
140
141         uint8_t vol_att = 0xff - ((vol * 0xff) / TAS_VOL_MAX);
142
143         tas5706a_put(addr1, vol_att);
144         tas5706a_put(addr2, vol_att);
145 }
146
147 void tas5706a_setLowPower_1(bool val)
148 {
149         TAS5706A_SETPOWERDOWN(val);
150         TAS5706A_SETMUTE(val);
151 }
152 #endif /* !CONFIG_I2C_DISABLE_OLD_API */
153
154 /*
155  * New API
156  */
157 INLINE bool tas5706a_putc(I2c *i2c, tas_addr_t addr, uint8_t ch)
158 {
159         i2c_start_w(i2c, TAS_ADDR, 2, I2C_STOP);
160         i2c_putc(i2c, addr);
161         i2c_putc(i2c, ch);
162
163         if (i2c_error(i2c))
164                 return false;
165
166         return true;
167 }
168
169 void tas5706a_setVolume_3(I2c *i2c, Tas5706aCh ch, tas5706a_vol_t vol)
170 {
171         ASSERT(ch < TAS_CNT);
172         ASSERT(vol <= TAS_VOL_MAX);
173
174         tas_addr_t addr1, addr2;
175
176         switch(ch)
177         {
178                 case TAS_CH1:
179                         addr1 = CH1_VOL_REG;
180                         addr2 = CH3_VOL_REG;
181                         break;
182                 case TAS_CH2:
183                         addr1 = CH2_VOL_REG;
184                         addr2 = CH4_VOL_REG;
185                         break;
186                 default:
187                         ASSERT(0);
188                         return;
189         }
190
191         uint8_t vol_att = 0xff - ((vol * 0xff) / TAS_VOL_MAX);
192
193         tas5706a_putc(i2c, addr1, vol_att);
194         tas5706a_putc(i2c, addr2, vol_att);
195 }
196
197 void tas5706a_setLowPower_2(I2c *i2c, bool val)
198 {
199         ASSERT(i2c);
200
201         TAS5706A_SETPOWERDOWN(val);
202         TAS5706A_SETMUTE(val);
203 }
204
205
206 void tas5706a_init_1(I2c *i2c)
207 {
208         ASSERT(i2c);
209         MOD_CHECK(timer);
210
211         TAS5706A_PIN_INIT();
212         timer_delay(200);
213         TAS5706A_SETPOWERDOWN(false);
214         TAS5706A_SETMUTE(false);
215         TAS5706A_MCLK_INIT();
216         timer_delay(2);
217         TAS5706A_SETRESET(false);
218         timer_delay(20);
219         tas5706a_putc(i2c, TRIM_REG, 0x00);
220
221         tas5706a_putc(i2c, VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL));
222
223         /* Unmute */
224         tas5706a_putc(i2c, SYS_REG2, 0);
225 }