1 /* 2 * Copyright 2017-2019 NXP 3 * All rights reserved. 4 * 5 * 6 * SPDX-License-Identifier: BSD-3-Clause 7 */ 8 9 #ifndef _FSL_CS42888_H_ 10 #define _FSL_CS42888_H_ 11 12 #include "fsl_codec_i2c.h" 13 14 /*! 15 * @addtogroup cs42888 16 * @ingroup codec 17 * @{ 18 */ 19 20 /******************************************************************************* 21 * Definitions 22 ******************************************************************************/ 23 /*! @name Driver version */ 24 /*@{*/ 25 /*! @brief cs42888 driver version 2.1.3. */ 26 #define FSL_CS42888_DRIVER_VERSION (MAKE_VERSION(2, 1, 3)) 27 /*@}*/ 28 29 /*! @brief CS42888 handle size */ 30 #ifndef CS42888_I2C_HANDLER_SIZE 31 #define CS42888_I2C_HANDLER_SIZE CODEC_I2C_MASTER_HANDLER_SIZE 32 #endif 33 34 /*! @brief Define the register address of CS42888. */ 35 #define CS42888_ID 0x01U 36 #define CS42888_POWER_CONTROL 0x02U 37 #define CS42888_FUNCTIONAL_MODE 0x03U 38 #define CS42888_INTERFACE_FORMATS 0x04U 39 #define CS42888_ADC_CONTROL 0x05U 40 #define CS42888_TRANSITION_CONTROL 0x06U 41 #define CS42888_CHANNEL_MUTE 0x07U 42 #define CS42888_VOL_CONTROL_AOUT1 0x08U 43 #define CS42888_VOL_CONTROL_AOUT2 0x09U 44 #define CS42888_VOL_CONTROL_AOUT3 0x0AU 45 #define CS42888_VOL_CONTROL_AOUT4 0x0BU 46 #define CS42888_VOL_CONTROL_AOUT5 0x0CU 47 #define CS42888_VOL_CONTROL_AOUT6 0x0DU 48 #define CS42888_VOL_CONTROL_AOUT7 0x0EU 49 #define CS42888_VOL_CONTROL_AOUT8 0x0FU 50 #define CS42888_DAC_CHANNEL_INVERT 0x10U 51 #define CS42888_VOL_CONTROL_AIN1 0x11U 52 #define CS42888_VOL_CONTROL_AIN2 0x12U 53 #define CS42888_VOL_CONTROL_AIN3 0x13U 54 #define CS42888_VOL_CONTROL_AIN4 0x14U 55 #define CS42888_ADC_CHANNEL_INVERT 0x17U 56 #define CS42888_STATUS_CONTROL 0x18U 57 #define CS42888_STATUS 0x19U 58 #define CS42888_STATUS_MASK 0x1AU 59 #define CS42888_MUTEC_PIN_CONTROL 0x1BU 60 61 #define CS42888_POWER_CONTROL_PDN_MASK 0x1U 62 #define CS42888_POWER_CONTROL_PDN_SHIFT 0U 63 #define CS42888_POWER_CONTROL_PDN(x) \ 64 (((uint8_t)((uint8_t)(x) << CS42888_POWER_CONTROL_PDN_SHIFT)) & CS42888_POWER_CONTROL_PDN_MASK) 65 #define CS42888_POWER_CONTROL_PDN_DAC1_MASK 0x2U 66 #define CS42888_POWER_CONTROL_PDN_DAC1_SHIFT 2U 67 #define CS42888_POWER_CONTROL_PDN_DAC1(x) \ 68 (((uint8_t)((uint8_t)(x) << CS42888_POWER_CONTROL_PDN_DAC1_SHIFT)) & CS42888_POWER_CONTROL_PDN_DAC1_MASK) 69 #define CS42888_POWER_CONTROL_PDN_DAC2_MASK 0x4U 70 #define CS42888_POWER_CONTROL_PDN_DAC2_SHIFT 3U 71 #define CS42888_POWER_CONTROL_PDN_DAC2(x) \ 72 (((uint8_t)((uint8_t)(x) << CS42888_POWER_CONTROL_PDN_DAC2_SHIFT)) & CS42888_POWER_CONTROL_PDN_DAC2_MASK) 73 #define CS42888_POWER_CONTROL_PDN_DAC3_MASK 0x8U 74 #define CS42888_POWER_CONTROL_PDN_DAC3_SHIFT 4U 75 #define CS42888_POWER_CONTROL_PDN_DAC3(x) \ 76 (((uint8_t)((uint8_t)(x) << CS42888_POWER_CONTROL_PDN_DAC3_SHIFT)) & CS42888_POWER_CONTROL_PDN_DAC3_MASK) 77 #define CS42888_POWER_CONTROL_PDN_DAC4_MASK 0x10U 78 #define CS42888_POWER_CONTROL_PDN_DAC4_SHIFT 5U 79 #define CS42888_POWER_CONTROL_PDN_DAC4(x) \ 80 (((uint8_t)((uint8_t)(x) << CS42888_POWER_CONTROL_PDN_DAC4_SHIFT)) & CS42888_POWER_CONTROL_PDN_DAC4_MASK) 81 #define CS42888_POWER_CONTROL_PDN_ADC1_MASK 0x20U 82 #define CS42888_POWER_CONTROL_PDN_ADC1_SHIFT 5U 83 #define CS42888_POWER_CONTROL_PDN_ADC1(x) \ 84 (((uint8_t)((uint8_t)(x) << CS42888_POWER_CONTROL_PDN_ADC1_SHIFT)) & CS42888_POWER_CONTROL_PDN_ADC1_MASK) 85 #define CS42888_POWER_CONTROL_PDN_ADC2_MASK 0x40U 86 #define CS42888_POWER_CONTROL_PDN_ADC2_SHIFT 6U 87 #define CS42888_POWER_CONTROL_PDN_ADC2(x) \ 88 (((uint8_t)((uint8_t)(x) << CS42888_POWER_CONTROL_PDN_ADC2_SHIFT)) & CS42888_POWER_CONTROL_PDN_ADC2_MASK) 89 90 #define CS42888_FUNCTIONAL_MODE_ADC_FM_MASK 0x30U 91 #define CS42888_FUNCTIONAL_MODE_ADC_FM_SHIFT 4U 92 #define CS42888_FUNCTIONAL_MODE_ADC_FM(x) \ 93 (((uint8_t)((uint8_t)(x) << CS42888_FUNCTIONAL_MODE_ADC_FM_SHIFT)) & CS42888_FUNCTIONAL_MODE_ADC_FM_MASK) 94 #define CS42888_FUNCTIONAL_MODE_DAC_FM_MASK 0xC0U 95 #define CS42888_FUNCTIONAL_MODE_DAC_FM_SHIFT 6U 96 #define CS42888_FUNCTIONAL_MODE_DAC_FM(x) \ 97 (((uint8_t)((uint8_t)(x) << CS42888_FUNCTIONAL_MODE_DAC_FM_SHIFT)) & CS42888_FUNCTIONAL_MODE_DAC_FM_MASK) 98 99 #define CS42888_INTERFACE_FORMATS_DAC_DIF_MASK 0x38U 100 #define CS42888_INTERFACE_FORMATS_DAC_DIF_SHIFT 3U 101 #define CS42888_INTERFACE_FORMATS_DAC_DIF(x) \ 102 (((uint8_t)((uint8_t)(x) << CS42888_INTERFACE_FORMATS_DAC_DIF_SHIFT)) & CS42888_INTERFACE_FORMATS_DAC_DIF_MASK) 103 104 /*! @brief CS42888 volume setting range */ 105 #define CS42888_AOUT_MAX_VOLUME_VALUE 0xFFU 106 107 /*! @brief Cache register number */ 108 #define CS42888_CACHEREGNUM 28U 109 110 /*! @brief CS42888 I2C address. */ 111 #define CS42888_I2C_ADDR 0x48U 112 113 /*! @brief CS42888 I2C baudrate */ 114 #define CS42888_I2C_BITRATE (100000U) 115 116 /*! @brief cs42888 reset function pointer */ 117 typedef void (*cs42888_reset)(bool state); 118 119 /*! @brief CS42888 support modes. */ 120 typedef enum _CS42888_func_mode 121 { 122 kCS42888_ModeMasterSSM = 0x0, /*!< master single speed mode */ 123 kCS42888_ModeMasterDSM = 0x1, /*!< master dual speed mode */ 124 kCS42888_ModeMasterQSM = 0x2, /*!< master quad speed mode */ 125 kCS42888_ModeSlave = 0x3, /*!< master single speed mode */ 126 } cs42888_func_mode; 127 128 /*! @brief Modules in CS42888 board. */ 129 typedef enum _CS42888_module 130 { 131 kCS42888_ModuleDACPair1 = 0x2, /*!< DAC pair1 (AOUT1 and AOUT2) module in CS42888 */ 132 kCS42888_ModuleDACPair2 = 0x4, /*!< DAC pair2 (AOUT3 and AOUT4) module in CS42888 */ 133 kCS42888_ModuleDACPair3 = 0x8, /*!< DAC pair3 (AOUT5 and AOUT6) module in CS42888 */ 134 kCS42888_ModuleDACPair4 = 0x10, /*!< DAC pair4 (AOUT7 and AOUT8) module in CS42888 */ 135 kCS42888_ModuleADCPair1 = 0x20, /*!< ADC pair1 (AIN1 and AIN2) module in CS42888 */ 136 kCS42888_ModuleADCPair2 = 0x40, /*!< ADC pair2 (AIN3 and AIN4) module in CS42888 */ 137 } cs42888_module_t; 138 139 /*! @brief CS42888 supported audio bus type. */ 140 typedef enum _CS42888_bus 141 { 142 kCS42888_BusLeftJustified = 0x0, /*!< Left justified format, up to 24 bits.*/ 143 kCS42888_BusI2S = 0x1, /*!< I2S format, up to 24 bits */ 144 kCS42888_BusRightJustified = 0x2, /*!< Right justified, can support 16bits and 24 bits */ 145 kCS42888_BusOL1 = 0x4, /*!< One-Line #1 mode */ 146 kCS42888_BusOL2 = 0x5, /*!< One-Line #2 mode */ 147 kCS42888_BusTDM = 0x6 /*!< TDM mode */ 148 } cs42888_bus_t; 149 150 /*! @brief CS428888 play channel 151 * @anchor _cs42888_play_channel 152 */ 153 enum 154 { 155 kCS42888_AOUT1 = 1U, /*!< aout1 */ 156 kCS42888_AOUT2 = 2U, /*!< aout2 */ 157 kCS42888_AOUT3 = 3U, /*!< aout3 */ 158 kCS42888_AOUT4 = 4U, /*!< aout4 */ 159 kCS42888_AOUT5 = 5U, /*!< aout5 */ 160 kCS42888_AOUT6 = 6U, /*!< aout6 */ 161 kCS42888_AOUT7 = 7U, /*!< aout7 */ 162 kCS42888_AOUT8 = 8U, /*!< aout8 */ 163 }; 164 165 /*! @brief cs42888 audio format */ 166 typedef struct _cs42888_audio_format 167 { 168 uint32_t mclk_HZ; /*!< master clock frequency */ 169 uint32_t sampleRate; /*!< sample rate */ 170 uint32_t bitWidth; /*!< bit width */ 171 } cs42888_audio_format_t; 172 173 /*! @brief Initialize structure of CS42888 */ 174 typedef struct cs42888_config 175 { 176 cs42888_bus_t bus; /*!< Audio transfer protocol */ 177 cs42888_audio_format_t format; /*!< cs42888 audio format */ 178 cs42888_func_mode ADCMode; /*!< CS42888 ADC function mode. */ 179 cs42888_func_mode DACMode; /*!< CS42888 DAC function mode. */ 180 bool master; /*!< true is master, false is slave */ 181 codec_i2c_config_t i2cConfig; /*!< i2c bus configuration */ 182 uint8_t slaveAddress; /*!< slave address */ 183 cs42888_reset reset; /*!< reset function pointer */ 184 } cs42888_config_t; 185 186 /*! @brief cs42888 handler */ 187 typedef struct _cs42888_handle 188 { 189 cs42888_config_t *config; /*!< cs42888 config pointer */ 190 uint8_t i2cHandle[CS42888_I2C_HANDLER_SIZE]; /*!< i2c handle pointer */ 191 } cs42888_handle_t; 192 193 /******************************************************************************* 194 * API 195 ******************************************************************************/ 196 #if defined(__cplusplus) 197 extern "C" { 198 #endif 199 200 /*! 201 * @brief CS42888 initialize function. 202 * 203 * The second parameter is NULL to CS42888 in this version. If users want 204 * to change the settings, they have to use cs42888_write_reg() or cs42888_modify_reg() 205 * to set the register value of CS42888. 206 * Note: If the codec_config is NULL, it would initialize CS42888 using default settings. 207 * The default setting: 208 * codec_config->bus = kCS42888_BusI2S 209 * codec_config->ADCmode = kCS42888_ModeSlave 210 * codec_config->DACmode = kCS42888_ModeSlave 211 * 212 * @param handle CS42888 handle structure. 213 * @param config CS42888 configuration structure. 214 */ 215 status_t CS42888_Init(cs42888_handle_t *handle, cs42888_config_t *config); 216 217 /*! 218 * @brief Deinit the CS42888 codec. 219 * 220 * This function close all modules in CS42888 to save power. 221 * 222 * @param handle CS42888 handle structure pointer. 223 */ 224 status_t CS42888_Deinit(cs42888_handle_t *handle); 225 226 /*! 227 * @brief Set the audio transfer protocol. 228 * 229 * CS42888 only supports I2S, left justified, right justified, PCM A, PCM B format. 230 * 231 * @param handle CS42888 handle structure. 232 * @param protocol Audio data transfer protocol. 233 * @param bitWidth bit width 234 */ 235 status_t CS42888_SetProtocol(cs42888_handle_t *handle, cs42888_bus_t protocol, uint32_t bitWidth); 236 237 /*! 238 * @brief Set CS42888 to differernt working mode. 239 * 240 * @deprecated api, Do not use it anymore. It has been superceded by @ref CS42888_SelectFunctionalMode. 241 * 242 * @param handle CS42888 handle structure. 243 * @param mode differenht working mode of CS42888. 244 */ 245 void CS42888_SetFuncMode(cs42888_handle_t *handle, cs42888_func_mode mode); 246 247 /*! 248 * @brief Set CS42888 to differernt functional mode. 249 * 250 * @param handle CS42888 handle structure. 251 * @param adcMode differenht working mode of CS42888. 252 * @param dacMode differenht working mode of CS42888. 253 */ 254 status_t CS42888_SelectFunctionalMode(cs42888_handle_t *handle, cs42888_func_mode adcMode, cs42888_func_mode dacMode); 255 256 /*! 257 * @brief Set the volume of different modules in CS42888. 258 * 259 * This function would set the volume of CS42888 modules. Uses need to appoint the module. 260 * The function assume that left channel and right channel has the same volume. 261 * 262 * @param handle CS42888 handle structure. 263 * @param channel AOUT channel, it shall be 1~8. 264 * @param volume Volume value need to be set. 265 */ 266 status_t CS42888_SetAOUTVolume(cs42888_handle_t *handle, uint8_t channel, uint8_t volume); 267 268 /*! 269 * @brief Set the volume of different modules in CS42888. 270 * 271 * This function would set the volume of CS42888 modules. Uses need to appoint the module. 272 * The function assume that left channel and right channel has the same volume. 273 * 274 * @param handle CS42888 handle structure. 275 * @param channel AIN channel, it shall be 1~4. 276 * @param volume Volume value need to be set. 277 */ 278 status_t CS42888_SetAINVolume(cs42888_handle_t *handle, uint8_t channel, uint8_t volume); 279 280 /*! 281 * @brief Get the volume of different AOUT channel in CS42888. 282 * 283 * This function gets the volume of CS42888 modules. Uses need to appoint the module. 284 * The function assume that left channel and right channel has the same volume. 285 * 286 * @param handle CS42888 handle structure. 287 * @param channel AOUT channel, it shall be 1~8. 288 */ 289 uint8_t CS42888_GetAOUTVolume(cs42888_handle_t *handle, uint8_t channel); 290 291 /*! 292 * @brief Get the volume of different AIN channel in CS42888. 293 * 294 * This function gets the volume of CS42888 modules. Uses need to appoint the module. 295 * The function assume that left channel and right channel has the same volume. 296 * 297 * @param handle CS42888 handle structure. 298 * @param channel AIN channel, it shall be 1~4. 299 */ 300 uint8_t CS42888_GetAINVolume(cs42888_handle_t *handle, uint8_t channel); 301 302 /*! 303 * @brief Mute modules in CS42888. 304 * 305 * @param handle CS42888 handle structure. 306 * @param channelMask Channel mask for mute. Mute channel 0, it shall be 0x1, while mute channel 0 and 1, it shall 307 * be 0x3. Mute all channel, it shall be 0xFF. Each bit represent one channel, 1 means mute, 0 means unmute. 308 */ 309 status_t CS42888_SetMute(cs42888_handle_t *handle, uint8_t channelMask); 310 311 /*! 312 * @brief Mute channel modules in CS42888. 313 * 314 * @param handle CS42888 handle structure. 315 * @param channel reference _cs42888_play_channel. 316 * @param isMute true is mute, falase is unmute. 317 */ 318 status_t CS42888_SetChannelMute(cs42888_handle_t *handle, uint8_t channel, bool isMute); 319 320 /*! 321 * @brief Enable/disable expected devices. 322 * 323 * @param handle CS42888 handle structure. 324 * @param module Module expected to enable. 325 * @param isEnabled Enable or disable moudles. 326 */ 327 status_t CS42888_SetModule(cs42888_handle_t *handle, cs42888_module_t module, bool isEnabled); 328 329 /*! 330 * @brief Configure the data format of audio data. 331 * 332 * This function would configure the registers about the sample rate, bit depths. 333 * 334 * @param handle CS42888 handle structure pointer. 335 * @param mclk Master clock frequency of I2S. 336 * @param sample_rate Sample rate of audio file running in CS42888. CS42888 now 337 * supports 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k, 44.1k, 48k and 96k sample rate. 338 * @param bits Bit depth of audio file (CS42888 only supports 16bit, 20bit, 24bit 339 * and 32 bit in HW). 340 */ 341 status_t CS42888_ConfigDataFormat(cs42888_handle_t *handle, uint32_t mclk, uint32_t sample_rate, uint32_t bits); 342 343 /*! 344 * @brief Write register to CS42888 using I2C. 345 * 346 * @param handle CS42888 handle structure. 347 * @param reg The register address in CS42888. 348 * @param val Value needs to write into the register. 349 */ 350 status_t CS42888_WriteReg(cs42888_handle_t *handle, uint8_t reg, uint8_t val); 351 352 /*! 353 * @brief Read register from CS42888 using I2C. 354 * @param handle CS42888 handle structure. 355 * @param reg The register address in CS42888. 356 * @param val Value written to. 357 */ 358 status_t CS42888_ReadReg(cs42888_handle_t *handle, uint8_t reg, uint8_t *val); 359 360 /*! 361 * @brief Modify some bits in the register using I2C. 362 * @param handle CS42888 handle structure. 363 * @param reg The register address in CS42888. 364 * @param mask The mask code for the bits want to write. The bit you want to write should be 0. 365 * @param val Value needs to write into the register. 366 */ 367 status_t CS42888_ModifyReg(cs42888_handle_t *handle, uint8_t reg, uint8_t mask, uint8_t val); 368 369 #if defined(__cplusplus) 370 } 371 #endif 372 373 /*! @} */ 374 375 #endif /* _FSL_CS42888_H_ */ 376