Merged revisions 4004-4036,4039-4048,4050-4095,4097-4100 via svnmerge from
[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
51 #define TAS_ADDR 0x36
52
53 typedef uint8_t tas_addr_t;
54
55 static bool tas5706a_send(tas_addr_t addr, const void *buf, size_t len)
56 {
57         bool ret = i2c_start_w(TAS_ADDR) && i2c_put(addr) && i2c_send(buf, len);
58         i2c_stop();
59         return ret;
60 }
61
62 INLINE bool tas5706a_putc(tas_addr_t addr, uint8_t ch)
63 {
64         return tas5706a_send(addr, &ch, sizeof(ch));
65 }
66
67 static bool tas5706a_recv(tas_addr_t addr, void *buf, size_t len)
68 {
69         bool ret = i2c_start_w(TAS_ADDR) && i2c_put(addr) && i2c_start_r(TAS_ADDR) && i2c_recv(buf, len);
70         i2c_stop();
71         return ret;
72 }
73
74 INLINE int tas5706a_getc(tas_addr_t addr)
75 {
76         uint8_t ch;
77         if (tas5706a_recv(addr, &ch, sizeof(ch)))
78                 return (int)(uint8_t)ch;
79         else
80                 return EOF;
81 }
82
83 #define TRIM_REG   0x1B
84 #define SYS_REG2   0x05
85 #define VOLUME_REG 0x07
86 #define MUTE_VOL 0xFF
87
88 #define DB_TO_REG(db) ((24 - (db)) * 2)
89
90 void tas5706a_init(void)
91 {
92         MOD_CHECK(i2c);
93         MOD_CHECK(timer);
94         TAS5706A_PIN_INIT();
95         timer_delay(200);
96         TAS5706A_SETPOWERDOWN(false);
97         TAS5706A_SETMUTE(false);
98         TAS5706A_MCLK_INIT();
99         timer_delay(2);
100         TAS5706A_SETRESET(false);
101         timer_delay(20);
102         tas5706a_putc(TRIM_REG, 0x00);
103
104         tas5706a_putc(VOLUME_REG, DB_TO_REG(CONFIG_TAS_MAX_VOL));
105
106         /* Unmute */
107         tas5706a_putc(SYS_REG2, 0);
108 }
109
110 #define CH1_VOL_REG 0x08
111 #define CH2_VOL_REG 0x09
112 #define CH3_VOL_REG 0x0A
113 #define CH4_VOL_REG 0x0B
114
115 void tas5706a_setVolume(Tas5706aCh ch, tas5706a_vol_t vol)
116 {
117         ASSERT(ch < TAS_CNT);
118         ASSERT(vol <= TAS_VOL_MAX);
119
120         tas_addr_t addr1, addr2;
121
122         switch(ch)
123         {
124                 case TAS_CH1:
125                         addr1 = CH1_VOL_REG;
126                         addr2 = CH3_VOL_REG;
127                         break;
128                 case TAS_CH2:
129                         addr1 = CH2_VOL_REG;
130                         addr2 = CH4_VOL_REG;
131                         break;
132                 default:
133                         ASSERT(0);
134                         return;
135         }
136
137         uint8_t vol_att = 0xff - ((vol * 0xff) / TAS_VOL_MAX);
138
139         tas5706a_putc(addr1, vol_att);
140         tas5706a_putc(addr2, vol_att);
141 }
142
143 void tas5706a_setLowPower(bool val)
144 {
145         TAS5706A_SETPOWERDOWN(val);
146         TAS5706A_SETMUTE(val);
147 }
148