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/list.h>
9 #include <linux/interrupt.h>
10 #include <linux/wait.h>
11 #include <sound/core.h>
12 #include <sound/initval.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
15 #include <sound/pcm-indirect.h>
16 #include <linux/workqueue.h>
17 
18 #include "interface/vchi/vchi.h"
19 
20 /*
21  * #define AUDIO_DEBUG_ENABLE
22  * #define AUDIO_VERBOSE_DEBUG_ENABLE
23  */
24 
25 /* Debug macros */
26 
27 #ifdef AUDIO_DEBUG_ENABLE
28 #ifdef AUDIO_VERBOSE_DEBUG_ENABLE
29 
30 #define audio_debug(fmt, arg...) \
31 	pr_info("%s:%d " fmt, __func__, __LINE__, ##arg)
32 
33 #define audio_info(fmt, arg...) \
34 	pr_info("%s:%d " fmt, __func__, __LINE__, ##arg)
35 
36 #else
37 
38 #define audio_debug(fmt, arg...)
39 
40 #define audio_info(fmt, arg...)
41 
42 #endif /* AUDIO_VERBOSE_DEBUG_ENABLE */
43 
44 #else
45 
46 #define audio_debug(fmt, arg...)
47 
48 #define audio_info(fmt, arg...)
49 
50 #endif /* AUDIO_DEBUG_ENABLE */
51 
52 #define audio_error(fmt, arg...) \
53 	pr_err("%s:%d " fmt, __func__, __LINE__, ##arg)
54 
55 #define audio_warning(fmt, arg...) \
56 	pr_warn("%s:%d " fmt, __func__, __LINE__, ##arg)
57 
58 #define audio_alert(fmt, arg...) \
59 	pr_alert("%s:%d " fmt, __func__, __LINE__, ##arg)
60 
61 #define MAX_SUBSTREAMS   (8)
62 #define AVAIL_SUBSTREAMS_MASK  (0xff)
63 
64 enum {
65 	CTRL_VOL_MUTE,
66 	CTRL_VOL_UNMUTE
67 };
68 
69 /* macros for alsa2chip and chip2alsa, instead of functions */
70 
71 // convert alsa to chip volume (defined as macro rather than function call)
72 #define alsa2chip(vol) (uint)(-(((vol) << 8) / 100))
73 
74 // convert chip to alsa volume
75 #define chip2alsa(vol) -(((vol) * 100) >> 8)
76 
77 /* Some constants for values .. */
78 enum snd_bcm2835_route {
79 	AUDIO_DEST_AUTO = 0,
80 	AUDIO_DEST_HEADPHONES = 1,
81 	AUDIO_DEST_HDMI = 2,
82 	AUDIO_DEST_MAX,
83 };
84 
85 enum snd_bcm2835_ctrl {
86 	PCM_PLAYBACK_VOLUME,
87 	PCM_PLAYBACK_MUTE,
88 	PCM_PLAYBACK_DEVICE,
89 };
90 
91 struct bcm2835_vchi_ctx {
92 	VCHI_INSTANCE_T vchi_instance;
93 	VCHI_CONNECTION_T *vchi_connection;
94 };
95 
96 /* definition of the chip-specific record */
97 struct bcm2835_chip {
98 	struct snd_card *card;
99 	struct snd_pcm *pcm;
100 	struct snd_pcm *pcm_spdif;
101 	/* Bitmat for valid reg_base and irq numbers */
102 	unsigned int avail_substreams;
103 	struct device *dev;
104 	struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
105 
106 	int volume;
107 	int old_volume; /* stores the volume value whist muted */
108 	int dest;
109 	int mute;
110 
111 	unsigned int opened;
112 	unsigned int spdif_status;
113 	struct mutex audio_mutex;
114 
115 	struct bcm2835_vchi_ctx *vchi_ctx;
116 };
117 
118 struct bcm2835_alsa_stream {
119 	struct bcm2835_chip *chip;
120 	struct snd_pcm_substream *substream;
121 	struct snd_pcm_indirect pcm_indirect;
122 
123 	spinlock_t lock;
124 
125 	int open;
126 	int running;
127 	int draining;
128 
129 	int channels;
130 	int params_rate;
131 	int pcm_format_width;
132 
133 	unsigned int pos;
134 	unsigned int buffer_size;
135 	unsigned int period_size;
136 
137 	atomic_t retrieved;
138 	struct bcm2835_audio_instance *instance;
139 	struct workqueue_struct *my_wq;
140 	int idx;
141 };
142 
143 int snd_bcm2835_new_ctl(struct bcm2835_chip *chip);
144 int snd_bcm2835_new_pcm(struct bcm2835_chip *chip, u32 numchannels);
145 int snd_bcm2835_new_spdif_pcm(struct bcm2835_chip *chip);
146 int snd_bcm2835_new_simple_pcm(struct bcm2835_chip *chip,
147 			       const char *name,
148 			       enum snd_bcm2835_route route,
149 			       u32 numchannels);
150 
151 int snd_bcm2835_new_hdmi_ctl(struct bcm2835_chip *chip);
152 int snd_bcm2835_new_headphones_ctl(struct bcm2835_chip *chip);
153 
154 int bcm2835_new_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx);
155 void bcm2835_free_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx);
156 
157 int bcm2835_audio_open(struct bcm2835_alsa_stream *alsa_stream);
158 int bcm2835_audio_close(struct bcm2835_alsa_stream *alsa_stream);
159 int bcm2835_audio_set_params(struct bcm2835_alsa_stream *alsa_stream,
160 			     unsigned int channels, unsigned int samplerate,
161 			     unsigned int bps);
162 int bcm2835_audio_setup(struct bcm2835_alsa_stream *alsa_stream);
163 int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream);
164 int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream);
165 int bcm2835_audio_set_ctls(struct bcm2835_chip *chip);
166 int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
167 			unsigned int count,
168 			void *src);
169 void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream);
170 unsigned int bcm2835_audio_retrieve_buffers(struct bcm2835_alsa_stream *alsa_stream);
171 void bcm2835_audio_flush_buffers(struct bcm2835_alsa_stream *alsa_stream);
172 void bcm2835_audio_flush_playback_buffers(struct bcm2835_alsa_stream *alsa_stream);
173 
174 #endif /* __SOUND_ARM_BCM2835_H */
175