hdaudio.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * HD-audio core stuff
  4. */
  5. #ifndef __SOUND_HDAUDIO_H
  6. #define __SOUND_HDAUDIO_H
  7. #include <linux/device.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/io-64-nonatomic-lo-hi.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/timecounter.h>
  14. #include <sound/core.h>
  15. #include <sound/pcm.h>
  16. #include <sound/memalloc.h>
  17. #include <sound/hda_verbs.h>
  18. #include <drm/i915_component.h>
  19. /* codec node id */
  20. typedef u16 hda_nid_t;
  21. struct hdac_bus;
  22. struct hdac_stream;
  23. struct hdac_device;
  24. struct hdac_driver;
  25. struct hdac_widget_tree;
  26. struct hda_device_id;
  27. /*
  28. * exported bus type
  29. */
  30. extern struct bus_type snd_hda_bus_type;
  31. /*
  32. * generic arrays
  33. */
  34. struct snd_array {
  35. unsigned int used;
  36. unsigned int alloced;
  37. unsigned int elem_size;
  38. unsigned int alloc_align;
  39. void *list;
  40. };
  41. /*
  42. * HD-audio codec base device
  43. */
  44. struct hdac_device {
  45. struct device dev;
  46. int type;
  47. struct hdac_bus *bus;
  48. unsigned int addr; /* codec address */
  49. struct list_head list; /* list point for bus codec_list */
  50. hda_nid_t afg; /* AFG node id */
  51. hda_nid_t mfg; /* MFG node id */
  52. /* ids */
  53. unsigned int vendor_id;
  54. unsigned int subsystem_id;
  55. unsigned int revision_id;
  56. unsigned int afg_function_id;
  57. unsigned int mfg_function_id;
  58. unsigned int afg_unsol:1;
  59. unsigned int mfg_unsol:1;
  60. unsigned int power_caps; /* FG power caps */
  61. const char *vendor_name; /* codec vendor name */
  62. const char *chip_name; /* codec chip name */
  63. /* verb exec op override */
  64. int (*exec_verb)(struct hdac_device *dev, unsigned int cmd,
  65. unsigned int flags, unsigned int *res);
  66. /* widgets */
  67. unsigned int num_nodes;
  68. hda_nid_t start_nid, end_nid;
  69. /* misc flags */
  70. atomic_t in_pm; /* suspend/resume being performed */
  71. /* sysfs */
  72. struct mutex widget_lock;
  73. struct hdac_widget_tree *widgets;
  74. /* regmap */
  75. struct regmap *regmap;
  76. struct mutex regmap_lock;
  77. struct snd_array vendor_verbs;
  78. bool lazy_cache:1; /* don't wake up for writes */
  79. bool caps_overwriting:1; /* caps overwrite being in process */
  80. bool cache_coef:1; /* cache COEF read/write too */
  81. unsigned int registered:1; /* codec was registered */
  82. };
  83. /* device/driver type used for matching */
  84. enum {
  85. HDA_DEV_CORE,
  86. HDA_DEV_LEGACY,
  87. HDA_DEV_ASOC,
  88. };
  89. enum {
  90. SND_SKL_PCI_BIND_AUTO, /* automatic selection based on pci class */
  91. SND_SKL_PCI_BIND_LEGACY,/* bind only with legacy driver */
  92. SND_SKL_PCI_BIND_ASOC /* bind only with ASoC driver */
  93. };
  94. /* direction */
  95. enum {
  96. HDA_INPUT, HDA_OUTPUT
  97. };
  98. #define dev_to_hdac_dev(_dev) container_of(_dev, struct hdac_device, dev)
  99. int snd_hdac_device_init(struct hdac_device *dev, struct hdac_bus *bus,
  100. const char *name, unsigned int addr);
  101. void snd_hdac_device_exit(struct hdac_device *dev);
  102. int snd_hdac_device_register(struct hdac_device *codec);
  103. void snd_hdac_device_unregister(struct hdac_device *codec);
  104. int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name);
  105. int snd_hdac_codec_modalias(struct hdac_device *hdac, char *buf, size_t size);
  106. int snd_hdac_refresh_widgets(struct hdac_device *codec);
  107. int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
  108. unsigned int verb, unsigned int parm, unsigned int *res);
  109. int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
  110. unsigned int *res);
  111. int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
  112. int parm);
  113. int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
  114. unsigned int parm, unsigned int val);
  115. int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
  116. hda_nid_t *conn_list, int max_conns);
  117. int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
  118. hda_nid_t *start_id);
  119. unsigned int snd_hdac_calc_stream_format(unsigned int rate,
  120. unsigned int channels,
  121. snd_pcm_format_t format,
  122. unsigned int maxbps,
  123. unsigned short spdif_ctls);
  124. int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
  125. u32 *ratesp, u64 *formatsp, unsigned int *bpsp);
  126. bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
  127. unsigned int format);
  128. int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
  129. int flags, unsigned int verb, unsigned int parm);
  130. int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
  131. int flags, unsigned int verb, unsigned int parm);
  132. bool snd_hdac_check_power_state(struct hdac_device *hdac,
  133. hda_nid_t nid, unsigned int target_state);
  134. unsigned int snd_hdac_sync_power_state(struct hdac_device *hdac,
  135. hda_nid_t nid, unsigned int target_state);
  136. /**
  137. * snd_hdac_read_parm - read a codec parameter
  138. * @codec: the codec object
  139. * @nid: NID to read a parameter
  140. * @parm: parameter to read
  141. *
  142. * Returns -1 for error. If you need to distinguish the error more
  143. * strictly, use _snd_hdac_read_parm() directly.
  144. */
  145. static inline int snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid,
  146. int parm)
  147. {
  148. unsigned int val;
  149. return _snd_hdac_read_parm(codec, nid, parm, &val) < 0 ? -1 : val;
  150. }
  151. #ifdef CONFIG_PM
  152. int snd_hdac_power_up(struct hdac_device *codec);
  153. int snd_hdac_power_down(struct hdac_device *codec);
  154. int snd_hdac_power_up_pm(struct hdac_device *codec);
  155. int snd_hdac_power_down_pm(struct hdac_device *codec);
  156. int snd_hdac_keep_power_up(struct hdac_device *codec);
  157. /* call this at entering into suspend/resume callbacks in codec driver */
  158. static inline void snd_hdac_enter_pm(struct hdac_device *codec)
  159. {
  160. atomic_inc(&codec->in_pm);
  161. }
  162. /* call this at leaving from suspend/resume callbacks in codec driver */
  163. static inline void snd_hdac_leave_pm(struct hdac_device *codec)
  164. {
  165. atomic_dec(&codec->in_pm);
  166. }
  167. static inline bool snd_hdac_is_in_pm(struct hdac_device *codec)
  168. {
  169. return atomic_read(&codec->in_pm);
  170. }
  171. static inline bool snd_hdac_is_power_on(struct hdac_device *codec)
  172. {
  173. return !pm_runtime_suspended(&codec->dev);
  174. }
  175. #else
  176. static inline int snd_hdac_power_up(struct hdac_device *codec) { return 0; }
  177. static inline int snd_hdac_power_down(struct hdac_device *codec) { return 0; }
  178. static inline int snd_hdac_power_up_pm(struct hdac_device *codec) { return 0; }
  179. static inline int snd_hdac_power_down_pm(struct hdac_device *codec) { return 0; }
  180. static inline int snd_hdac_keep_power_up(struct hdac_device *codec) { return 0; }
  181. static inline void snd_hdac_enter_pm(struct hdac_device *codec) {}
  182. static inline void snd_hdac_leave_pm(struct hdac_device *codec) {}
  183. static inline bool snd_hdac_is_in_pm(struct hdac_device *codec) { return false; }
  184. static inline bool snd_hdac_is_power_on(struct hdac_device *codec) { return true; }
  185. #endif
  186. /*
  187. * HD-audio codec base driver
  188. */
  189. struct hdac_driver {
  190. struct device_driver driver;
  191. int type;
  192. const struct hda_device_id *id_table;
  193. int (*match)(struct hdac_device *dev, struct hdac_driver *drv);
  194. void (*unsol_event)(struct hdac_device *dev, unsigned int event);
  195. /* fields used by ext bus APIs */
  196. int (*probe)(struct hdac_device *dev);
  197. int (*remove)(struct hdac_device *dev);
  198. void (*shutdown)(struct hdac_device *dev);
  199. };
  200. #define drv_to_hdac_driver(_drv) container_of(_drv, struct hdac_driver, driver)
  201. const struct hda_device_id *
  202. hdac_get_device_id(struct hdac_device *hdev, struct hdac_driver *drv);
  203. /*
  204. * Bus verb operators
  205. */
  206. struct hdac_bus_ops {
  207. /* send a single command */
  208. int (*command)(struct hdac_bus *bus, unsigned int cmd);
  209. /* get a response from the last command */
  210. int (*get_response)(struct hdac_bus *bus, unsigned int addr,
  211. unsigned int *res);
  212. /* notify of codec link power-up/down */
  213. void (*link_power)(struct hdac_device *hdev, bool enable);
  214. };
  215. /*
  216. * ops used for ASoC HDA codec drivers
  217. */
  218. struct hdac_ext_bus_ops {
  219. int (*hdev_attach)(struct hdac_device *hdev);
  220. int (*hdev_detach)(struct hdac_device *hdev);
  221. };
  222. #define HDA_UNSOL_QUEUE_SIZE 64
  223. #define HDA_MAX_CODECS 8 /* limit by controller side */
  224. /*
  225. * CORB/RIRB
  226. *
  227. * Each CORB entry is 4byte, RIRB is 8byte
  228. */
  229. struct hdac_rb {
  230. __le32 *buf; /* virtual address of CORB/RIRB buffer */
  231. dma_addr_t addr; /* physical address of CORB/RIRB buffer */
  232. unsigned short rp, wp; /* RIRB read/write pointers */
  233. int cmds[HDA_MAX_CODECS]; /* number of pending requests */
  234. u32 res[HDA_MAX_CODECS]; /* last read value */
  235. };
  236. /*
  237. * HD-audio bus base driver
  238. *
  239. * @ppcap: pp capabilities pointer
  240. * @spbcap: SPIB capabilities pointer
  241. * @mlcap: MultiLink capabilities pointer
  242. * @gtscap: gts capabilities pointer
  243. * @drsmcap: dma resume capabilities pointer
  244. * @num_streams: streams supported
  245. * @idx: HDA link index
  246. * @hlink_list: link list of HDA links
  247. * @lock: lock for link and display power mgmt
  248. * @cmd_dma_state: state of cmd DMAs: CORB and RIRB
  249. */
  250. struct hdac_bus {
  251. struct device *dev;
  252. const struct hdac_bus_ops *ops;
  253. const struct hdac_ext_bus_ops *ext_ops;
  254. /* h/w resources */
  255. unsigned long addr;
  256. void __iomem *remap_addr;
  257. int irq;
  258. void __iomem *ppcap;
  259. void __iomem *spbcap;
  260. void __iomem *mlcap;
  261. void __iomem *gtscap;
  262. void __iomem *drsmcap;
  263. /* codec linked list */
  264. struct list_head codec_list;
  265. unsigned int num_codecs;
  266. /* link caddr -> codec */
  267. struct hdac_device *caddr_tbl[HDA_MAX_CODEC_ADDRESS + 1];
  268. /* unsolicited event queue */
  269. u32 unsol_queue[HDA_UNSOL_QUEUE_SIZE * 2]; /* ring buffer */
  270. unsigned int unsol_rp, unsol_wp;
  271. struct work_struct unsol_work;
  272. /* bit flags of detected codecs */
  273. unsigned long codec_mask;
  274. /* bit flags of powered codecs */
  275. unsigned long codec_powered;
  276. /* CORB/RIRB */
  277. struct hdac_rb corb;
  278. struct hdac_rb rirb;
  279. unsigned int last_cmd[HDA_MAX_CODECS]; /* last sent command */
  280. wait_queue_head_t rirb_wq;
  281. /* CORB/RIRB and position buffers */
  282. struct snd_dma_buffer rb;
  283. struct snd_dma_buffer posbuf;
  284. int dma_type; /* SNDRV_DMA_TYPE_XXX for CORB/RIRB */
  285. /* hdac_stream linked list */
  286. struct list_head stream_list;
  287. /* operation state */
  288. bool chip_init:1; /* h/w initialized */
  289. /* behavior flags */
  290. bool aligned_mmio:1; /* aligned MMIO access */
  291. bool sync_write:1; /* sync after verb write */
  292. bool use_posbuf:1; /* use position buffer */
  293. bool snoop:1; /* enable snooping */
  294. bool align_bdle_4k:1; /* BDLE align 4K boundary */
  295. bool reverse_assign:1; /* assign devices in reverse order */
  296. bool corbrp_self_clear:1; /* CORBRP clears itself after reset */
  297. bool polling_mode:1;
  298. bool needs_damn_long_delay:1;
  299. int poll_count;
  300. int bdl_pos_adj; /* BDL position adjustment */
  301. /* delay time in us for dma stop */
  302. unsigned int dma_stop_delay;
  303. /* locks */
  304. spinlock_t reg_lock;
  305. struct mutex cmd_mutex;
  306. struct mutex lock;
  307. /* DRM component interface */
  308. struct drm_audio_component *audio_component;
  309. long display_power_status;
  310. unsigned long display_power_active;
  311. /* parameters required for enhanced capabilities */
  312. int num_streams;
  313. int idx;
  314. /* link management */
  315. struct list_head hlink_list;
  316. bool cmd_dma_state;
  317. /* factor used to derive STRIPE control value */
  318. unsigned int sdo_limit;
  319. };
  320. int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
  321. const struct hdac_bus_ops *ops);
  322. void snd_hdac_bus_exit(struct hdac_bus *bus);
  323. int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
  324. unsigned int cmd, unsigned int *res);
  325. void snd_hdac_codec_link_up(struct hdac_device *codec);
  326. void snd_hdac_codec_link_down(struct hdac_device *codec);
  327. int snd_hdac_bus_send_cmd(struct hdac_bus *bus, unsigned int val);
  328. int snd_hdac_bus_get_response(struct hdac_bus *bus, unsigned int addr,
  329. unsigned int *res);
  330. int snd_hdac_bus_parse_capabilities(struct hdac_bus *bus);
  331. bool snd_hdac_bus_init_chip(struct hdac_bus *bus, bool full_reset);
  332. void snd_hdac_bus_stop_chip(struct hdac_bus *bus);
  333. void snd_hdac_bus_init_cmd_io(struct hdac_bus *bus);
  334. void snd_hdac_bus_stop_cmd_io(struct hdac_bus *bus);
  335. void snd_hdac_bus_enter_link_reset(struct hdac_bus *bus);
  336. void snd_hdac_bus_exit_link_reset(struct hdac_bus *bus);
  337. int snd_hdac_bus_reset_link(struct hdac_bus *bus, bool full_reset);
  338. void snd_hdac_bus_link_power(struct hdac_device *hdev, bool enable);
  339. void snd_hdac_bus_update_rirb(struct hdac_bus *bus);
  340. int snd_hdac_bus_handle_stream_irq(struct hdac_bus *bus, unsigned int status,
  341. void (*ack)(struct hdac_bus *,
  342. struct hdac_stream *));
  343. int snd_hdac_bus_alloc_stream_pages(struct hdac_bus *bus);
  344. void snd_hdac_bus_free_stream_pages(struct hdac_bus *bus);
  345. #ifdef CONFIG_SND_HDA_ALIGNED_MMIO
  346. unsigned int snd_hdac_aligned_read(void __iomem *addr, unsigned int mask);
  347. void snd_hdac_aligned_write(unsigned int val, void __iomem *addr,
  348. unsigned int mask);
  349. #define snd_hdac_aligned_mmio(bus) (bus)->aligned_mmio
  350. #else
  351. #define snd_hdac_aligned_mmio(bus) false
  352. #define snd_hdac_aligned_read(addr, mask) 0
  353. #define snd_hdac_aligned_write(val, addr, mask) do {} while (0)
  354. #endif
  355. static inline void snd_hdac_reg_writeb(struct hdac_bus *bus, void __iomem *addr,
  356. u8 val)
  357. {
  358. if (snd_hdac_aligned_mmio(bus))
  359. snd_hdac_aligned_write(val, addr, 0xff);
  360. else
  361. writeb(val, addr);
  362. }
  363. static inline void snd_hdac_reg_writew(struct hdac_bus *bus, void __iomem *addr,
  364. u16 val)
  365. {
  366. if (snd_hdac_aligned_mmio(bus))
  367. snd_hdac_aligned_write(val, addr, 0xffff);
  368. else
  369. writew(val, addr);
  370. }
  371. static inline u8 snd_hdac_reg_readb(struct hdac_bus *bus, void __iomem *addr)
  372. {
  373. return snd_hdac_aligned_mmio(bus) ?
  374. snd_hdac_aligned_read(addr, 0xff) : readb(addr);
  375. }
  376. static inline u16 snd_hdac_reg_readw(struct hdac_bus *bus, void __iomem *addr)
  377. {
  378. return snd_hdac_aligned_mmio(bus) ?
  379. snd_hdac_aligned_read(addr, 0xffff) : readw(addr);
  380. }
  381. #define snd_hdac_reg_writel(bus, addr, val) writel(val, addr)
  382. #define snd_hdac_reg_readl(bus, addr) readl(addr)
  383. #define snd_hdac_reg_writeq(bus, addr, val) writeq(val, addr)
  384. #define snd_hdac_reg_readq(bus, addr) readq(addr)
  385. /*
  386. * macros for easy use
  387. */
  388. #define _snd_hdac_chip_writeb(chip, reg, value) \
  389. snd_hdac_reg_writeb(chip, (chip)->remap_addr + (reg), value)
  390. #define _snd_hdac_chip_readb(chip, reg) \
  391. snd_hdac_reg_readb(chip, (chip)->remap_addr + (reg))
  392. #define _snd_hdac_chip_writew(chip, reg, value) \
  393. snd_hdac_reg_writew(chip, (chip)->remap_addr + (reg), value)
  394. #define _snd_hdac_chip_readw(chip, reg) \
  395. snd_hdac_reg_readw(chip, (chip)->remap_addr + (reg))
  396. #define _snd_hdac_chip_writel(chip, reg, value) \
  397. snd_hdac_reg_writel(chip, (chip)->remap_addr + (reg), value)
  398. #define _snd_hdac_chip_readl(chip, reg) \
  399. snd_hdac_reg_readl(chip, (chip)->remap_addr + (reg))
  400. /* read/write a register, pass without AZX_REG_ prefix */
  401. #define snd_hdac_chip_writel(chip, reg, value) \
  402. _snd_hdac_chip_writel(chip, AZX_REG_ ## reg, value)
  403. #define snd_hdac_chip_writew(chip, reg, value) \
  404. _snd_hdac_chip_writew(chip, AZX_REG_ ## reg, value)
  405. #define snd_hdac_chip_writeb(chip, reg, value) \
  406. _snd_hdac_chip_writeb(chip, AZX_REG_ ## reg, value)
  407. #define snd_hdac_chip_readl(chip, reg) \
  408. _snd_hdac_chip_readl(chip, AZX_REG_ ## reg)
  409. #define snd_hdac_chip_readw(chip, reg) \
  410. _snd_hdac_chip_readw(chip, AZX_REG_ ## reg)
  411. #define snd_hdac_chip_readb(chip, reg) \
  412. _snd_hdac_chip_readb(chip, AZX_REG_ ## reg)
  413. /* update a register, pass without AZX_REG_ prefix */
  414. #define snd_hdac_chip_updatel(chip, reg, mask, val) \
  415. snd_hdac_chip_writel(chip, reg, \
  416. (snd_hdac_chip_readl(chip, reg) & ~(mask)) | (val))
  417. #define snd_hdac_chip_updatew(chip, reg, mask, val) \
  418. snd_hdac_chip_writew(chip, reg, \
  419. (snd_hdac_chip_readw(chip, reg) & ~(mask)) | (val))
  420. #define snd_hdac_chip_updateb(chip, reg, mask, val) \
  421. snd_hdac_chip_writeb(chip, reg, \
  422. (snd_hdac_chip_readb(chip, reg) & ~(mask)) | (val))
  423. /*
  424. * HD-audio stream
  425. */
  426. struct hdac_stream {
  427. struct hdac_bus *bus;
  428. struct snd_dma_buffer bdl; /* BDL buffer */
  429. __le32 *posbuf; /* position buffer pointer */
  430. int direction; /* playback / capture (SNDRV_PCM_STREAM_*) */
  431. unsigned int bufsize; /* size of the play buffer in bytes */
  432. unsigned int period_bytes; /* size of the period in bytes */
  433. unsigned int frags; /* number for period in the play buffer */
  434. unsigned int fifo_size; /* FIFO size */
  435. void __iomem *sd_addr; /* stream descriptor pointer */
  436. u32 sd_int_sta_mask; /* stream int status mask */
  437. /* pcm support */
  438. struct snd_pcm_substream *substream; /* assigned substream,
  439. * set in PCM open
  440. */
  441. struct snd_compr_stream *cstream;
  442. unsigned int format_val; /* format value to be set in the
  443. * controller and the codec
  444. */
  445. unsigned char stream_tag; /* assigned stream */
  446. unsigned char index; /* stream index */
  447. int assigned_key; /* last device# key assigned to */
  448. bool opened:1;
  449. bool running:1;
  450. bool prepared:1;
  451. bool no_period_wakeup:1;
  452. bool locked:1;
  453. bool stripe:1; /* apply stripe control */
  454. u64 curr_pos;
  455. /* timestamp */
  456. unsigned long start_wallclk; /* start + minimum wallclk */
  457. unsigned long period_wallclk; /* wallclk for period */
  458. struct timecounter tc;
  459. struct cyclecounter cc;
  460. int delay_negative_threshold;
  461. struct list_head list;
  462. #ifdef CONFIG_SND_HDA_DSP_LOADER
  463. /* DSP access mutex */
  464. struct mutex dsp_mutex;
  465. #endif
  466. };
  467. void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev,
  468. int idx, int direction, int tag);
  469. struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,
  470. struct snd_pcm_substream *substream);
  471. void snd_hdac_stream_release_locked(struct hdac_stream *azx_dev);
  472. void snd_hdac_stream_release(struct hdac_stream *azx_dev);
  473. struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus,
  474. int dir, int stream_tag);
  475. int snd_hdac_stream_setup(struct hdac_stream *azx_dev);
  476. void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev);
  477. int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev);
  478. int snd_hdac_stream_set_params(struct hdac_stream *azx_dev,
  479. unsigned int format_val);
  480. void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start);
  481. void snd_hdac_stream_stop(struct hdac_stream *azx_dev);
  482. void snd_hdac_stop_streams(struct hdac_bus *bus);
  483. void snd_hdac_stop_streams_and_chip(struct hdac_bus *bus);
  484. void snd_hdac_stream_reset(struct hdac_stream *azx_dev);
  485. void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
  486. unsigned int streams, unsigned int reg);
  487. void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
  488. unsigned int streams);
  489. void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
  490. unsigned int streams);
  491. int snd_hdac_get_stream_stripe_ctl(struct hdac_bus *bus,
  492. struct snd_pcm_substream *substream);
  493. /*
  494. * macros for easy use
  495. */
  496. /* read/write a register, pass without AZX_REG_ prefix */
  497. #define snd_hdac_stream_writel(dev, reg, value) \
  498. snd_hdac_reg_writel((dev)->bus, (dev)->sd_addr + AZX_REG_ ## reg, value)
  499. #define snd_hdac_stream_writew(dev, reg, value) \
  500. snd_hdac_reg_writew((dev)->bus, (dev)->sd_addr + AZX_REG_ ## reg, value)
  501. #define snd_hdac_stream_writeb(dev, reg, value) \
  502. snd_hdac_reg_writeb((dev)->bus, (dev)->sd_addr + AZX_REG_ ## reg, value)
  503. #define snd_hdac_stream_readl(dev, reg) \
  504. snd_hdac_reg_readl((dev)->bus, (dev)->sd_addr + AZX_REG_ ## reg)
  505. #define snd_hdac_stream_readw(dev, reg) \
  506. snd_hdac_reg_readw((dev)->bus, (dev)->sd_addr + AZX_REG_ ## reg)
  507. #define snd_hdac_stream_readb(dev, reg) \
  508. snd_hdac_reg_readb((dev)->bus, (dev)->sd_addr + AZX_REG_ ## reg)
  509. #define snd_hdac_stream_readb_poll(dev, reg, val, cond, delay_us, timeout_us) \
  510. read_poll_timeout_atomic(snd_hdac_reg_readb, val, cond, delay_us, timeout_us, \
  511. false, (dev)->bus, (dev)->sd_addr + AZX_REG_ ## reg)
  512. #define snd_hdac_stream_readl_poll(dev, reg, val, cond, delay_us, timeout_us) \
  513. read_poll_timeout_atomic(snd_hdac_reg_readl, val, cond, delay_us, timeout_us, \
  514. false, (dev)->bus, (dev)->sd_addr + AZX_REG_ ## reg)
  515. /* update a register, pass without AZX_REG_ prefix */
  516. #define snd_hdac_stream_updatel(dev, reg, mask, val) \
  517. snd_hdac_stream_writel(dev, reg, \
  518. (snd_hdac_stream_readl(dev, reg) & \
  519. ~(mask)) | (val))
  520. #define snd_hdac_stream_updatew(dev, reg, mask, val) \
  521. snd_hdac_stream_writew(dev, reg, \
  522. (snd_hdac_stream_readw(dev, reg) & \
  523. ~(mask)) | (val))
  524. #define snd_hdac_stream_updateb(dev, reg, mask, val) \
  525. snd_hdac_stream_writeb(dev, reg, \
  526. (snd_hdac_stream_readb(dev, reg) & \
  527. ~(mask)) | (val))
  528. #ifdef CONFIG_SND_HDA_DSP_LOADER
  529. /* DSP lock helpers */
  530. #define snd_hdac_dsp_lock_init(dev) mutex_init(&(dev)->dsp_mutex)
  531. #define snd_hdac_dsp_lock(dev) mutex_lock(&(dev)->dsp_mutex)
  532. #define snd_hdac_dsp_unlock(dev) mutex_unlock(&(dev)->dsp_mutex)
  533. #define snd_hdac_stream_is_locked(dev) ((dev)->locked)
  534. /* DSP loader helpers */
  535. int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
  536. unsigned int byte_size, struct snd_dma_buffer *bufp);
  537. void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start);
  538. void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
  539. struct snd_dma_buffer *dmab);
  540. #else /* CONFIG_SND_HDA_DSP_LOADER */
  541. #define snd_hdac_dsp_lock_init(dev) do {} while (0)
  542. #define snd_hdac_dsp_lock(dev) do {} while (0)
  543. #define snd_hdac_dsp_unlock(dev) do {} while (0)
  544. #define snd_hdac_stream_is_locked(dev) 0
  545. static inline int
  546. snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
  547. unsigned int byte_size, struct snd_dma_buffer *bufp)
  548. {
  549. return 0;
  550. }
  551. static inline void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start)
  552. {
  553. }
  554. static inline void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
  555. struct snd_dma_buffer *dmab)
  556. {
  557. }
  558. #endif /* CONFIG_SND_HDA_DSP_LOADER */
  559. /*
  560. * generic array helpers
  561. */
  562. void *snd_array_new(struct snd_array *array);
  563. void snd_array_free(struct snd_array *array);
  564. static inline void snd_array_init(struct snd_array *array, unsigned int size,
  565. unsigned int align)
  566. {
  567. array->elem_size = size;
  568. array->alloc_align = align;
  569. }
  570. static inline void *snd_array_elem(struct snd_array *array, unsigned int idx)
  571. {
  572. return array->list + idx * array->elem_size;
  573. }
  574. static inline unsigned int snd_array_index(struct snd_array *array, void *ptr)
  575. {
  576. return (unsigned long)(ptr - array->list) / array->elem_size;
  577. }
  578. /* a helper macro to iterate for each snd_array element */
  579. #define snd_array_for_each(array, idx, ptr) \
  580. for ((idx) = 0, (ptr) = (array)->list; (idx) < (array)->used; \
  581. (ptr) = snd_array_elem(array, ++(idx)))
  582. #endif /* __SOUND_HDAUDIO_H */