1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright 2011 Broadcom Corporation.  All rights reserved. */
3 
4 #ifndef __SOUND_ARM_BCM2835_H
5 #define __SOUND_ARM_BCM2835_H
6 
7 #include <linux/device.h>
8 #include <linux/wait.h>
9 #include <sound/core.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm-indirect.h>
12 
13 #include "../include/linux/raspberrypi/vchiq.h"
14 
15 #define MAX_SUBSTREAMS   (8)
16 #define AVAIL_SUBSTREAMS_MASK  (0xff)
17 
18 enum {
19 	CTRL_VOL_MUTE,
20 	CTRL_VOL_UNMUTE
21 };
22 
23 /* macros for alsa2chip and chip2alsa, instead of functions */
24 
25 // convert alsa to chip volume (defined as macro rather than function call)
26 #define alsa2chip(vol) ((uint)(-(((vol) << 8) / 100)))
27 
28 // convert chip to alsa volume
29 #define chip2alsa(vol) -(((vol) * 100) >> 8)
30 
31 #define CHIP_MIN_VOLUME		26214 /* minimum level aka mute */
32 
33 /* Some constants for values .. */
34 enum snd_bcm2835_route {
35 	AUDIO_DEST_AUTO = 0,
36 	AUDIO_DEST_HEADPHONES = 1,
37 	AUDIO_DEST_HDMI = 2,
38 	AUDIO_DEST_MAX,
39 };
40 
41 enum snd_bcm2835_ctrl {
42 	PCM_PLAYBACK_VOLUME,
43 	PCM_PLAYBACK_MUTE,
44 	PCM_PLAYBACK_DEVICE,
45 };
46 
47 struct bcm2835_vchi_ctx {
48 	struct vchiq_instance *instance;
49 };
50 
51 /* definition of the chip-specific record */
52 struct bcm2835_chip {
53 	struct snd_card *card;
54 	struct snd_pcm *pcm;
55 	struct snd_pcm *pcm_spdif;
56 	struct device *dev;
57 	struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
58 
59 	int volume;
60 	int dest;
61 	int mute;
62 
63 	unsigned int opened;
64 	unsigned int spdif_status;
65 	struct mutex audio_mutex; /* Serialize chip data access */
66 
67 	struct bcm2835_vchi_ctx *vchi_ctx;
68 };
69 
70 struct bcm2835_alsa_stream {
71 	struct bcm2835_chip *chip;
72 	struct snd_pcm_substream *substream;
73 	struct snd_pcm_indirect pcm_indirect;
74 
75 	int draining;
76 
77 	atomic_t pos;
78 	unsigned int period_offset;
79 	unsigned int buffer_size;
80 	unsigned int period_size;
81 	ktime_t interpolate_start;
82 
83 	struct bcm2835_audio_instance *instance;
84 	int idx;
85 };
86 
87 int snd_bcm2835_new_ctl(struct bcm2835_chip *chip);
88 int snd_bcm2835_new_pcm(struct bcm2835_chip *chip, const char *name,
89 			int idx, enum snd_bcm2835_route route,
90 			u32 numchannels, bool spdif);
91 
92 int snd_bcm2835_new_hdmi_ctl(struct bcm2835_chip *chip);
93 int snd_bcm2835_new_headphones_ctl(struct bcm2835_chip *chip);
94 
95 int bcm2835_new_vchi_ctx(struct device *dev, struct bcm2835_vchi_ctx *vchi_ctx);
96 void bcm2835_free_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx);
97 
98 int bcm2835_audio_open(struct bcm2835_alsa_stream *alsa_stream);
99 int bcm2835_audio_close(struct bcm2835_alsa_stream *alsa_stream);
100 int bcm2835_audio_set_params(struct bcm2835_alsa_stream *alsa_stream,
101 			     unsigned int channels, unsigned int samplerate,
102 			     unsigned int bps);
103 int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream);
104 int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream);
105 int bcm2835_audio_drain(struct bcm2835_alsa_stream *alsa_stream);
106 int bcm2835_audio_set_ctls(struct bcm2835_alsa_stream *alsa_stream);
107 int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
108 			unsigned int count,
109 			void *src);
110 void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream,
111 			   unsigned int size);
112 unsigned int bcm2835_audio_retrieve_buffers(struct bcm2835_alsa_stream *alsa_stream);
113 
114 #endif /* __SOUND_ARM_BCM2835_H */
115