1 /*
2  * Copyright 2021-2022 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _FSL_PCM512X_H_
8 #define _FSL_PCM512X_H_
9 
10 #include "fsl_codec_i2c.h"
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup pcm512x
15  * @ingroup codec
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 /*! @name Driver version */
23 /*@{*/
24 /*! @brief CLOCK driver version 2.2.0 */
25 #define FSL_PCM512X_DRIVER_VERSION (MAKE_VERSION(0, 1, 0))
26 /*@}*/
27 
28 /*! @brief PCM512x handle size */
29 #ifndef PCM512X_I2C_HANDLER_SIZE
30 #define PCM512X_I2C_HANDLER_SIZE CODEC_I2C_MASTER_HANDLER_SIZE
31 #endif
32 
33 /*! @brief Define the register addresses of PCM512x. */
34 #define PCM512x_RESET             (  1)
35 #define PCM512x_POWER             (  2)
36 #define PCM512x_MUTE              (  3)
37 #define PCM512x_PLL_EN            (  4)
38 #define PCM512x_GPIO_EN           (  8)
39 #define PCM512x_BCLK_LRCLK_CFG    (  9)
40 #define PCM512x_MASTER_MODE       ( 12)
41 #define PCM512x_DAC_REF           ( 14)
42 #define PCM512x_SYNCHRONIZE       ( 19)
43 #define PCM512x_DSP_CLKDIV        ( 27)
44 #define PCM512x_DAC_CLKDIV        ( 28)
45 #define PCM512x_NCP_CLKDIV        ( 29)
46 #define PCM512x_OSR_CLKDIV        ( 30)
47 #define PCM512x_MASTER_CLKDIV_1   ( 32)
48 #define PCM512x_MASTER_CLKDIV_2   ( 33)
49 #define PCM512x_FS_SPEED_MODE     ( 34)
50 #define PCM512x_IDAC_1            ( 35)
51 #define PCM512x_IDAC_2            ( 36)
52 #define PCM512x_ERROR_DETECT      ( 37)
53 #define PCM512x_I2S_1             ( 40)
54 #define PCM512x_GPIO_OUTPUT_3     ( 82)
55 #define PCM512x_GPIO_OUTPUT_4     ( 83)
56 #define PCM512x_GPIO_OUTPUT_6     ( 85)
57 #define PCM512x_GPIO_CONTROL_1    ( 86)
58 #define PCM512x_ANALOG_MUTE_DET   (108)
59 
60 /*! @brief Page 0, Register 1 - reset */
61 #define PCM512x_RSTR (1 << 0)
62 #define PCM512x_RSTM (1 << 4)
63 
64 /*! @brief Page 0, Register 2 - power */
65 #define PCM512x_RQPD       (1 << 0)
66 #define PCM512x_RQST       (1 << 4)
67 
68 /*! @brief Page 0, Register 3 - mute */
69 #define PCM512x_RQMR (1 << 0)
70 #define PCM512x_RQML (1 << 4)
71 
72 /*! @brief Page 0, Register 4 - PLL */
73 #define PCM512x_PLLE       (1 << 0)
74 #define PCM512x_PLCK       (1 << 4)
75 
76 /*! @brief Page 0, Register 9 - BCK, LRCLK configuration */
77 #define PCM512x_BCKO       (1 << 4)
78 #define PCM512x_LRKO       (1 << 0)
79 #define PCM512x_BCKP       (1 << 5)
80 
81 /*! @brief Page 0, Register 12 - Master mode BCK, LRCLK reset */
82 #define PCM512x_RLRK       (1 << 0)
83 #define PCM512x_RBCK       (1 << 1)
84 
85 /*! @brief Page 0, Register 14 - DAC reference */
86 #define PCM512x_SDAC        (7 << 4)
87 #define PCM512x_SDAC_SCK    (3 << 4)
88 
89 /*! @brief Page 0, Register 19 - synchronize */
90 #define PCM512x_RQSY        (1 << 0)
91 #define PCM512x_RQSY_RESUME (0 << 0)
92 #define PCM512x_RQSY_HALT   (1 << 0)
93 
94 /*! @brief Page 0, Register 34 - fs speed mode */
95 #define PCM512x_FSSP        (3 << 0)
96 #define PCM512x_FSSP_48KHZ  (0 << 0)
97 #define PCM512x_FSSP_96KHZ  (1 << 0)
98 #define PCM512x_FSSP_192KHZ (2 << 0)
99 #define PCM512x_FSSP_384KHZ (3 << 0)
100 
101 /*! @brief Page 0, Register 37 - Error detection */
102 #define PCM512x_IPLK (1 << 0)
103 #define PCM512x_DCAS (1 << 1)
104 #define PCM512x_IDCM (1 << 2)
105 #define PCM512x_IDCH (1 << 3)
106 #define PCM512x_IDSK (1 << 4)
107 #define PCM512x_IDBK (1 << 5)
108 #define PCM512x_IDFS (1 << 6)
109 
110 /*! @brief Page 0, Register 40 - I2S configuration */
111 #define PCM512x_ALEN       (3 << 0)
112 #define PCM512x_ALEN_16    (0 << 0)
113 #define PCM512x_ALEN_20    (1 << 0)
114 #define PCM512x_ALEN_24    (2 << 0)
115 #define PCM512x_ALEN_32    (3 << 0)
116 #define PCM512x_AFMT       (3 << 4)
117 
118 /*! @brief PCM512x I2C baudrate */
119 #define PCM512X_I2C_BAUDRATE (100000U)
120 
121 /*! @brief audio sample rate definition
122  * @anchor _pcm512x_sample_rate
123  */
124 enum
125 {
126     kPCM512x_AudioSampleRate8KHz     = 8000U,    /*!< Sample rate 8000 Hz */
127     kPCM512x_AudioSampleRate11025Hz  = 11025U,   /*!< Sample rate 11025 Hz */
128     kPCM512x_AudioSampleRate12KHz    = 12000U,   /*!< Sample rate 12000 Hz */
129     kPCM512x_AudioSampleRate16KHz    = 16000U,   /*!< Sample rate 16000 Hz */
130     kPCM512x_AudioSampleRate22050Hz  = 22050U,   /*!< Sample rate 22050 Hz */
131     kPCM512x_AudioSampleRate24KHz    = 24000U,   /*!< Sample rate 24000 Hz */
132     kPCM512x_AudioSampleRate32KHz    = 32000U,   /*!< Sample rate 32000 Hz */
133     kPCM512x_AudioSampleRate44100Hz  = 44100U,   /*!< Sample rate 44100 Hz */
134     kPCM512x_AudioSampleRate48KHz    = 48000U,   /*!< Sample rate 48000 Hz */
135     kPCM512x_AudioSampleRate88200Hz  = 88200U,   /*!< Sample rate 88200 Hz */
136     kPCM512x_AudioSampleRate96KHz    = 96000U,   /*!< Sample rate 96000 Hz */
137     kPCM512x_AudioSampleRate176400Hz = 176400U,  /*!< Sample rate 176400 Hz */
138     kPCM512x_AudioSampleRate192KHz   = 192000U,  /*!< Sample rate 192000 Hz */
139     kPCM512x_AudioSampleRate384KHz   = 384000U,  /*!< Sample rate 384000 Hz */
140 };
141 
142 /*! @brief audio bit width
143  * @anchor _pcm512x_audio_bit_width
144  */
145 enum
146 {
147     kPCM512x_AudioBitWidth16bit = 16U, /*!< audio bit width 16 */
148     kPCM512x_AudioBitWidth20bit = 20U, /*!< audio bit width 20 */
149     kPCM512x_AudioBitWidth24bit = 24U, /*!< audio bit width 24 */
150     kPCM512x_AudioBitWidth32bit = 32U, /*!< audio bit width 32 */
151 };
152 
153 /*! @brief PCM512x sysclk source */
154 typedef enum _pcm512x_sysclk_source
155 {
156     kPCM512x_SysClkSourceMclk        = 0U, /*!< sysclk source from external MCLK */
157     kPCM512x_SysClkSourceInternalPLL = 1U, /*!< sysclk source from internal PLL */
158 } pcm512x_sysclk_source_t;
159 
160 /*! @brief PCM512x audio format */
161 typedef struct _pcm512x_audio_format
162 {
163     uint32_t mclk_HZ;    /*!< master clock frequency */
164     uint32_t sampleRate; /*!< sample rate */
165     uint32_t bitWidth;   /*!< bit width */
166 } pcm512x_audio_format_t;
167 
168 /*! @brief Initialize structure of PCM512x */
169 typedef struct pcm512x_config
170 {
171     pcm512x_audio_format_t format;              /*!< Audio format */
172     bool master_slave;                          /*!< Master or slave. */
173     uint8_t slaveAddress;                       /*!< PCM512x device address */
174     codec_i2c_config_t i2cConfig;               /*!< i2c configuration */
175     uint8_t gpio_led;                           /*!< led gpio number */
176     uint8_t gpio_osc44;                         /*!< oscillator 44.1 gpio number */
177     uint8_t gpio_osc48;                         /*!< oscillator 48 gpio number */
178 } pcm512x_config_t;
179 
180 /*! @brief PCM512x codec handler
181  */
182 typedef struct _pcm512x_handle
183 {
184     const pcm512x_config_t *config;              /*!< PCM512x config pointer */
185     uint8_t i2cHandle[PCM512X_I2C_HANDLER_SIZE]; /*!< i2c handle */
186 } pcm512x_handle_t;
187 
188 /*******************************************************************************
189  * API
190  ******************************************************************************/
191 #if defined(__cplusplus)
192 extern "C" {
193 #endif
194 
195 /*!
196  * @brief PCM512x initialise function.
197  *
198  * @param handle PCM512x handle structure.
199  * @param config PCM512x configuration structure.
200  */
201 status_t PCM512x_Init(pcm512x_handle_t *handle, const pcm512x_config_t *config);
202 
203 /*!
204  * @brief Deinit the PCM512x codec.
205  *
206  * This function close all modules in PCM512x to save power.
207  *
208  * @param handle PCM512x handle structure pointer.
209  */
210 status_t PCM512x_Deinit(pcm512x_handle_t *handle);
211 
212 /*!
213  * @brief Mute PCM512x.
214  *
215  * @param handle PCM512x handle structure.
216  * @param isEnabled Mute or unmute, 1 represent mute.
217  */
218 status_t PCM512x_SetMute(pcm512x_handle_t *handle, bool isEnabled);
219 
220 /*!
221  * @brief Configure the data format of audio data.
222  *
223  * This function would configure the registers about the sample rate, bit depths.
224  *
225  * @param handle PCM512x handle structure pointer.
226  * @param sysclk system clock of the codec which can be generated by MCLK or PLL output.
227  * @param sample_rate Sample rate of audio file running in PCM512x.
228  * @param bits Bit depth of audio file.
229  */
230 status_t PCM512x_SetFormat(pcm512x_handle_t *handle, uint32_t sysclk, uint32_t sample_rate, uint32_t bits);
231 
232 /*!
233  * @brief Write register to PCM512x using I2C.
234  *
235  * @param handle PCM512x handle structure.
236  * @param reg The register address in PCM512x.
237  * @param val Value needs to write into the register.
238  */
239 status_t PCM512x_WriteReg(pcm512x_handle_t *handle, uint8_t reg, uint8_t val);
240 
241 /*!
242  * @brief Read register from PCM512x using I2C.
243  * @param handle PCM512x handle structure.
244  * @param reg The register address in PCM512x.
245  * @param val Value written to.
246  */
247 status_t PCM512x_ReadReg(pcm512x_handle_t *handle, uint8_t reg, uint8_t *val);
248 
249 /*!
250  * @brief Modify some bits in the register using I2C.
251  * @param handle PCM512x handle structure.
252  * @param reg The register address in PCM512x.
253  * @param mask The mask code for the bits want to write. The bit you want to write should be 0.
254  * @param val Value needs to write into the register.
255  */
256 status_t PCM512x_ModifyReg(pcm512x_handle_t *handle, uint8_t reg, uint8_t mask, uint8_t val);
257 
258 #if defined(__cplusplus)
259 }
260 #endif
261 
262 /*! @} */
263 
264 #endif /* _FSL_PCM512X_H_ */
265 
266 /*******************************************************************************
267  * API
268  ******************************************************************************/
269