Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / drv / wm8731.h
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 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Daniele Basile <asterix@develer.com>
34  *
35  * \brief WM8731 Audio codec 2 wire driver.
36  *
37  * $WIZ$ module_name = "wm8731"
38  * $WIZ$ module_configuration = "bertos/cfg/cfg_wm8731.h"
39  * $WIZ$ module_hw = "bertos/hw/hw_wm8731.h"
40  * $WIZ$ module_depends = "i2c"
41  *
42  */
43
44 #ifndef DRV_WM8731_H
45 #define DRV_WM8731_H
46
47 #include <cpu/types.h>
48
49 #include <drv/i2c.h>
50
51 /* Reset register*/
52 #define WM8731_REG_RESET                           0x0F
53
54 /* Left Line in register */
55 #define WM8731_REG_LEFT_LINEIN                     0x0
56 #define WM8731_LINVOL_BITS_MASK                   0x1F // Left line input volume control
57 #define WM8731_LINMUTE_BIT                           7 // Left line input mute to ADC
58 #define WM8731_LRINBOTH_BIT                          8 // Left to right channel line input volume and mute data load control
59
60 /* Right Line in register */
61 #define WM8731_REG_RIGHT_LINEIN                    0x1
62 #define WM8731_RINVOL_BITS_MASK                   0x1F // Right line input volume control
63 #define WM8731_RINMUTE_BIT                           7 // Right line input mute to ADC
64 #define WM8731_RLINBOTH_BIT                          8 // Right to right channel line input volume and mute data load control
65
66 /* Left Headphone out register*/
67 #define WM8731_REG_LEFT_HPOUT                      0x2
68 #define WM8731_LHPVOL_BITS_MASK                   0x7F // Left channel headphone output volume control
69 #define WM8731_LZCEN_BIT                             7 // Left channel zero cross detect enable
70 #define WM8731_LRHPBOTH_BIT                          8 // Left to right channel headphone volume, mute and zero cross data load control
71
72 /* Right Headphone out register*/
73 #define WM8731_REG_RIGHT_HPOUT                      0x3
74 #define WM8731_RHPVOL_BITS_MASK                    0x7F // Right channel headphone output volume control
75 #define WM8731_RZCEN_BIT                              7 // Right channel zero cross detect enable
76 #define WM8731_RLHPBOTH_BIT                           8 // Right to right channel headphone volume, mute and zero cross data load control
77
78 /* Analogue audio path control register*/
79 #define WM8731_REG_ANALOGUE_PATH_CTRL              0x4
80 #define WM8731_MICBOOST                              0 // Microphone Input Level Boost
81 #define WM8731_MUTEMIC                               1 // Line input Mute to ADC
82 /**
83  * Input selector
84  * $WIZ$ wm8731_insel = "WM8731_INSEL_MIC", "WM8731_INSEL_LINEIN"
85  */
86 #define WM8731_INSEL                                 2 // Microphone/Line Select to ADC
87 #define WM8731_INSEL_MIC              BV(WM8731_INSEL) // Microphone Select to ADC
88 #define WM8731_INSEL_LINEIN          ~BV(WM8731_INSEL) // Line in Select to ADC
89 /**
90  * Input to output
91  * $WIZ$ wm8731_bypass = "WM8731_BYPASS", "WM8731_DACSEL", "WM8731_SIDETONE"
92  */
93 #define WM8731_BYPASS                            BV(3) // Bypass switch
94 #define WM8731_DACSEL                            BV(4) // DAC select
95 #define WM8731_SIDETONE                          BV(5) // Side tone switch
96 /**
97  * Side tone attenuation
98  * $WIZ$ wm8731_sideatt = "WM8731_SIDEATT_15dB", "WM8731_SIDEATT_12dB", "WM8731_SIDEATT_9dB", "WM8731_SIDEATT_6dB"
99  */
100 #define WM8731_SIDEATT_MASK                       0xC0 // Side tone attenuation
101 #define WM8731_SIDEATT_15dB                       0xC0 // -15dB
102 #define WM8731_SIDEATT_12dB                       0x80 // -12dB
103 #define WM8731_SIDEATT_9dB                        0x40 // -9dB
104 #define WM8731_SIDEATT_6dB                        0x00 // -6dB
105
106
107 /* Digital audio path control register*/
108 #define WM8731_REG_DIGITAL_PATH_CTRL               0x5
109 #define WM8731_ADCHPD                                0 // ADC high pass filter enable
110 /**
111  * De-emphasis control
112  * $WIZ$ wm8731_deemp = "WM8731_DEEMP_48kHz", "WM8731_DEEMP_44k1Hz", "WM8731_DEEMP_32kHz", "WM8731_DEEMP_DISABLE"
113  */
114 #define WM8731_DEEMP_MASK                          0x6 // De-emphasis control
115 #define WM8731_DEEMP_48kHz                         0x6 // 48kHz
116 #define WM8731_DEEMP_44k1Hz                        0x4 // 44.1kHz
117 #define WM8731_DEEMP_32kHz                         0x2 // 32kHz
118 #define WM8731_DEEMP_DISABLE                       0x0 // Disable
119 #define WM8731_DACMU                                 3 // DAC Soft Mute control
120
121 /* Power down control register*/
122 #define WM8731_REG_PWDOWN_CTRL                     0x6
123 #define WM8731_LINEINPD                           BV(0) ///< LineIn power down
124 #define WM8731_MICPD                              BV(1) ///< Mic power down
125 #define WM8731_ADCPD                              BV(2) ///< ADC power down
126 #define WM8731_DACPD                              BV(3) ///< DAC power down
127 #define WM8731_OUTPD                              BV(4) ///< OUT power down
128 #define WM8731_OSCBIT                             BV(5) ///< OSC power down
129 #define WM8731_CLKOUTPD                           BV(6) ///< CLKOUT powerdown
130 #define WM8731_POWEROFF_BIT                          7 // Power off device
131
132 /* Interface format register*/
133 #define WM8731_REG_DA_INTERFACE_FORMAT             0x7
134 /**
135  * Audio format
136  * $WIZ$ wm8731_fmt = "WM8731_FORMAT_MSB_LEFT_JUSTIFIED", "WM8731_FORMAT_MSB_RIGHT_JUSTIFIED", "WM8731_FORMAT_I2S", "WM8731_FORMAT_DSP"
137  */
138 #define WM8731_FORMAT_BITS_MASK                    0x3 // Format
139 #define WM8731_FORMAT_MSB_LEFT_JUSTIFIED           0x0 // MSB-First, left justified
140 #define WM8731_FORMAT_MSB_RIGHT_JUSTIFIED          0x1 // MSB-First, right justified
141 #define WM8731_FORMAT_I2S                          0x2 //I2S Format, MSB-First left-1 justified
142 #define WM8731_FORMAT_DSP                          0x3 //DSP Mode, frame sync + 2 data packed words
143 /**
144  * Audio format
145  * $WIZ$ wm8731_databit = "WM8731_IWL_16_BIT", "WM8731_IWL_20_BIT", "WM8731_IWL_24_BIT", "WM8731_IWL_32_BIT"
146  */
147 #define WM8731_IWL_BITS                            0xC // Input audio data bit length select
148 #define WM8731_IWL_16_BIT                          0x0 // 16 bit
149 #define WM8731_IWL_20_BIT                          0x4 // 20 bit
150 #define WM8731_IWL_24_BIT                          0x8 // 24 bit
151 #define WM8731_IWL_32_BIT                          0xC // 32 bit
152 #define WM8731_IRP_BITS                              4 //  DACLRC phase control
153 #define WM8731_IRSWAP_BIT                            5 //  DAC Left right clock swap
154 #define WM8731_MS_BIT                                6 //  Master slave mode control
155 #define WM8731_BCLKINV_BIT                           7 // Bit clock invert
156
157 /* Sampling control*/
158 #define WM8731_REG_SAMPLECTRL                      0x8
159 #define WM8731_USBNORMAL_BIT                         0 // Mode select, usb mode, normal mode
160 #define WM8731_BOSR_BIT                              1 // Base over-sampling rate
161 #define WM8731_SR_BITS_MASK                       0x3C // Sample rate control
162 #define WM8731_CLKIDIV2_BIT                          6 // Core clock divider select
163 #define WM8731_CLKODIV2_BIT                          7 // CLKOUT divider select
164 /**
165  * Sampling rate control
166  * $WIZ$ wm8731_sample_rate = "WM8731_NORMAL_256FS_48KHZ", "WM8731_NORMAL_256FS_44_1KHZ", "WM8731_NORMAL_384FS_44_1KHZ"
167  */
168 #define WM8731_NORMAL_256FS_48KHZ      0
169 #define WM8731_NORMAL_256FS_44_1KHZ    0x20
170 #define WM8731_NORMAL_384FS_44_1KHZ    0x22
171 #define WM8731_NORMAL_128FS_44_1KHZ    0x3E
172
173 /* Active control register*/
174 #define WM8731_REG_ACTIVE_CTRL                     0x9
175 #define WM8731_ACTIVE_BIT                            0 // Activate interface
176
177
178 typedef struct Wm8731
179 {
180         I2c *i2c;
181         uint8_t addr;
182 } Wm8731;
183
184
185 /**
186  * Devices which is possible to set volume
187  * \}
188  */
189 #define         WM8731_LINE_IN        BV(0)
190 #define         WM8731_HEADPHONE      BV(1)
191 /* \} */
192
193 /**
194  * Set the volume of select device line in or Headphone.
195  * The volume is express in %, and the volume range is 0-100%.
196  */
197 void wm8731_setVolume(Wm8731 *ctx, uint16_t device, uint8_t volume);
198 void wm8731_powerOn(Wm8731 *ctx);
199 void wm8731_powerOff(Wm8731 *ctx);
200 void wm8731_powerOnDevices(Wm8731 *ctx, uint16_t device);
201 void wm8731_powerOffDevices(Wm8731 *ctx, uint16_t device);
202
203 void wm8731_init(Wm8731 *ctx, I2c *i2c, uint8_t codec_addr);
204
205 #endif /* DRV_WM8731_H */