hdaudio.h 25 KB

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