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