hdaudio.h 20 KB

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