cs43l22.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /****************************************************************************
  2. * include/nuttx/audio/cs43l22.h
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Reference:
  8. * "CS43L22 Ultra Low Power CODEC for Portable Audio Applications, Pre-
  9. * Production", September 2012, Rev 3.3, Wolfson Microelectronics
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. * 3. Neither the name NuttX nor the names of its contributors may be
  22. * used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. #ifndef __INCLUDE_NUTTX_AUDIO_CS43L22_H
  40. #define __INCLUDE_NUTTX_AUDIO_CS43L22_H
  41. /****************************************************************************
  42. * Included Files
  43. ****************************************************************************/
  44. #include <stdint.h>
  45. #include <stdbool.h>
  46. #include <nuttx/irq.h>
  47. #ifdef CONFIG_AUDIO_CS43L22
  48. /****************************************************************************
  49. * Pre-processor Definitions
  50. ****************************************************************************/
  51. /* Configuration ************************************************************
  52. *
  53. * CONFIG_AUDIO_CS43L22 - Enables CS43L22 support
  54. * CONFIG_CS43L22_INITVOLUME - The initial volume level
  55. * in the range {0..1000}
  56. * CONFIG_CS43L22_INFLIGHT - Maximum number of buffers that the CS43L22
  57. * driver will send to the I2S driver before any have completed.
  58. * CONFIG_CS43L22_MSG_PRIO - Priority of messages sent to the CS43L22
  59. * worker thread.
  60. * CONFIG_CS43L22_BUFFER_SIZE - Preferred buffer size
  61. * CONFIG_CS43L22_NUM_BUFFERS - Preferred number of buffers
  62. * CONFIG_CS43L22_WORKER_STACKSIZE - Stack size to use when creating the the
  63. * CS43L22 worker thread.
  64. * CONFIG_CS43L22_REGDUMP - Enable logic to dump all CS43L22 registers to
  65. * the SYSLOG device.
  66. */
  67. /* Pre-requisites */
  68. #ifndef CONFIG_AUDIO
  69. # error CONFIG_AUDIO is required for audio subsystem support
  70. #endif
  71. #ifndef CONFIG_I2S
  72. # error CONFIG_I2S is required by the CS43L22 driver
  73. #endif
  74. #ifndef CONFIG_I2C
  75. # error CONFIG_I2C is required by the CS43L22 driver
  76. #endif
  77. #ifndef CONFIG_SCHED_WORKQUEUE
  78. # error CONFIG_SCHED_WORKQUEUE is required by the CS43L22 driver
  79. #endif
  80. /* Default configuration values */
  81. #ifndef CONFIG_CS43L22_INITVOLUME
  82. # define CONFIG_CS43L22_INITVOLUME 400
  83. #endif
  84. #ifndef CONFIG_CS43L22_INFLIGHT
  85. # define CONFIG_CS43L22_INFLIGHT 2
  86. #endif
  87. #if CONFIG_CS43L22_INFLIGHT > 255
  88. # error CONFIG_CS43L22_INFLIGHT must fit in a uint8_t
  89. #endif
  90. #ifndef CONFIG_CS43L22_MSG_PRIO
  91. # define CONFIG_CS43L22_MSG_PRIO 1
  92. #endif
  93. #ifndef CONFIG_CS43L22_BUFFER_SIZE
  94. # define CONFIG_CS43L22_BUFFER_SIZE 8192
  95. #endif
  96. #ifndef CONFIG_CS43L22_NUM_BUFFERS
  97. # define CONFIG_CS43L22_NUM_BUFFERS 4
  98. #endif
  99. #ifndef CONFIG_CS43L22_WORKER_STACKSIZE
  100. # define CONFIG_CS43L22_WORKER_STACKSIZE 768
  101. #endif
  102. /* Helper macros ************************************************************/
  103. #define CS43L22_ATTACH(s,isr,arg) ((s)->attach(s,isr,arg))
  104. #define CS43L22_DETACH(s) ((s)->attach(s,NULL,NULL))
  105. #define CS43L22_ENABLE(s) ((s)->enable(s,true))
  106. #define CS43L22_DISABLE(s) ((s)->enable(s,false))
  107. #define CS43L22_RESTORE(s,e) ((s)->enable(s,e))
  108. #define CS43L22_HW_RESET(s) ((s)->reset(s))
  109. /****************************************************************************
  110. * Public Types
  111. ****************************************************************************/
  112. /* This is the type of the CS43L22 interrupt handler. The lower level code
  113. * will intercept the interrupt and provide the upper level with the private
  114. * data that was provided when the interrupt was attached.
  115. */
  116. struct cs43l22_lower_s; /* Forward reference. Defined below */
  117. typedef CODE int (*cs43l22_handler_t)
  118. (FAR const struct cs43l22_lower_s *lower,
  119. FAR void *arg);
  120. /* A reference to a structure of this type must be passed to the CS43L22
  121. * driver. This structure provides information about the configuration
  122. * of the CS43L22 and provides some board-specific hooks.
  123. *
  124. * Memory for this structure is provided by the caller. It is not copied
  125. * by the driver and is presumed to persist while the driver is active.
  126. */
  127. struct cs43l22_lower_s
  128. {
  129. /* I2C characterization */
  130. uint32_t frequency; /* Initial I2C frequency */
  131. uint8_t address; /* 7-bit I2C address (only bits 0-6 used) */
  132. /* Clocking is provided via MCLK. The CS43L22 driver will need to know
  133. * the frequency of MCLK in order to generate the correct bitrates.
  134. */
  135. uint32_t mclk; /* CS43L22 Master clock frequency */
  136. /* IRQ/GPIO access callbacks. These operations all hidden behind
  137. * callbacks to isolate the CS43L22 driver from differences in GPIO
  138. * interrupt handling by varying boards and MCUs. If possible,
  139. * interrupts should be configured on both rising and falling edges
  140. * so that contact and loss-of-contact events can be detected.
  141. *
  142. * attach - Attach or detach the CS43L22 interrupt handler to the GPIO
  143. * interrupt
  144. * enable - Enable or disable the GPIO interrupt. Returns the
  145. * previous interrupt state.
  146. * reset - HW reset of the CS43L22 chip
  147. */
  148. CODE int (*attach)(FAR const struct cs43l22_lower_s *lower,
  149. cs43l22_handler_t isr, FAR void *arg);
  150. CODE bool (*enable)(FAR const struct cs43l22_lower_s *lower, bool enable);
  151. CODE void (*reset)(FAR const struct cs43l22_lower_s *lower);
  152. };
  153. /****************************************************************************
  154. * Public Data
  155. ****************************************************************************/
  156. #ifdef __cplusplus
  157. #define EXTERN extern "C"
  158. extern "C"
  159. {
  160. #else
  161. #define EXTERN extern
  162. #endif
  163. /****************************************************************************
  164. * Public Function Prototypes
  165. ****************************************************************************/
  166. /****************************************************************************
  167. * Name: cs43l22_initialize
  168. *
  169. * Description:
  170. * Initialize the CS43L22 device.
  171. *
  172. * Input Parameters:
  173. * i2c - An I2C driver instance
  174. * i2s - An I2S driver instance
  175. * lower - Persistent board configuration data
  176. *
  177. * Returned Value:
  178. * A new lower half audio interface for the CS43L22 device is returned on
  179. * success; NULL is returned on failure.
  180. *
  181. ****************************************************************************/
  182. struct i2c_master_s; /* Forward reference. Defined in include/nuttx/i2c/i2c_master.h */
  183. struct i2s_dev_s; /* Forward reference. Defined in include/nuttx/audio/i2s.h */
  184. struct audio_lowerhalf_s; /* Forward reference. Defined in nuttx/audio/audio.h */
  185. FAR struct audio_lowerhalf_s *
  186. cs43l22_initialize(FAR struct i2c_master_s *i2c, FAR struct i2s_dev_s *i2s,
  187. FAR const struct cs43l22_lower_s *lower);
  188. /****************************************************************************
  189. * Name: cs43l22_dump_registers
  190. *
  191. * Description:
  192. * Dump the contents of all CS43L22 registers to the syslog device
  193. *
  194. * Input Parameters:
  195. * dev - The device instance returned by cs43l22_initialize
  196. *
  197. * Returned Value:
  198. * None.
  199. *
  200. ****************************************************************************/
  201. #ifdef CONFIG_CS43L22_REGDUMP
  202. void cs43l22_dump_registers(FAR struct audio_lowerhalf_s *dev,
  203. FAR const char *msg);
  204. #else
  205. /* This eliminates the need for any conditional compilation in the
  206. * including file.
  207. */
  208. # define cs43l22_dump_registers(d,m)
  209. #endif
  210. /****************************************************************************
  211. * Name: cs43l22_clock_analysis
  212. *
  213. * Description:
  214. * Analyze the settings in the clock chain and dump to syslog.
  215. *
  216. * Input Parameters:
  217. * dev - The device instance returned by cs43l22_initialize
  218. *
  219. * Returned Value:
  220. * None.
  221. *
  222. ****************************************************************************/
  223. #ifdef CONFIG_CS43L22_CLKDEBUG
  224. void cs43l22_clock_analysis(FAR struct audio_lowerhalf_s *dev,
  225. FAR const char *msg);
  226. #else
  227. /* This eliminates the need for any conditional compilation in the
  228. * including file.
  229. */
  230. # define cs43l22_clock_analysis(d,m)
  231. #endif
  232. #undef EXTERN
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif /* CONFIG_AUDIO_CS43L22 */
  237. #endif /* __INCLUDE_NUTTX_AUDIO_CS43L22_H */