hdaudio.h 19 KB

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