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