1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Greybus audio driver
4  * Copyright 2015 Google Inc.
5  * Copyright 2015 Linaro Ltd.
6  */
7 
8 #ifndef __LINUX_GBAUDIO_CODEC_H
9 #define __LINUX_GBAUDIO_CODEC_H
10 
11 #include <linux/greybus.h>
12 #include <sound/soc.h>
13 #include <sound/jack.h>
14 
15 #define NAME_SIZE	32
16 #define MAX_DAIS	2	/* APB1, APB2 */
17 
18 enum {
19 	APB1_PCM = 0,
20 	APB2_PCM,
21 	NUM_CODEC_DAIS,
22 };
23 
24 /*
25  * device_type should be same as defined in audio.h
26  * (Android media layer)
27  */
28 enum {
29 	GBAUDIO_DEVICE_NONE                     = 0x0,
30 	/* reserved bits */
31 	GBAUDIO_DEVICE_BIT_IN                   = 0x80000000,
32 	GBAUDIO_DEVICE_BIT_DEFAULT              = 0x40000000,
33 	/* output devices */
34 	GBAUDIO_DEVICE_OUT_SPEAKER              = 0x2,
35 	GBAUDIO_DEVICE_OUT_WIRED_HEADSET        = 0x4,
36 	GBAUDIO_DEVICE_OUT_WIRED_HEADPHONE      = 0x8,
37 	/* input devices */
38 	GBAUDIO_DEVICE_IN_BUILTIN_MIC           = GBAUDIO_DEVICE_BIT_IN | 0x4,
39 	GBAUDIO_DEVICE_IN_WIRED_HEADSET         = GBAUDIO_DEVICE_BIT_IN | 0x10,
40 };
41 
42 #define GBCODEC_JACK_MASK		0x0000FFFF
43 #define GBCODEC_JACK_BUTTON_MASK	0xFFFF0000
44 
45 enum gbaudio_codec_state {
46 	GBAUDIO_CODEC_SHUTDOWN = 0,
47 	GBAUDIO_CODEC_STARTUP,
48 	GBAUDIO_CODEC_HWPARAMS,
49 	GBAUDIO_CODEC_PREPARE,
50 	GBAUDIO_CODEC_START,
51 	GBAUDIO_CODEC_STOP,
52 };
53 
54 struct gbaudio_stream_params {
55 	int state;
56 	u8 sig_bits, channels;
57 	u32 format, rate;
58 };
59 
60 struct gbaudio_codec_dai {
61 	int id;
62 	/* runtime params for playback/capture streams */
63 	struct gbaudio_stream_params params[2];
64 	struct list_head list;
65 };
66 
67 struct gbaudio_codec_info {
68 	struct device *dev;
69 	struct snd_soc_codec *codec;
70 	struct list_head module_list;
71 	/* to maintain runtime stream params for each DAI */
72 	struct list_head dai_list;
73 	struct mutex lock;
74 };
75 
76 struct gbaudio_widget {
77 	__u8 id;
78 	const char *name;
79 	struct list_head list;
80 };
81 
82 struct gbaudio_control {
83 	__u8 id;
84 	char *name;
85 	char *wname;
86 	const char * const *texts;
87 	int items;
88 	struct list_head list;
89 };
90 
91 struct gbaudio_data_connection {
92 	int id;
93 	__le16 data_cport;
94 	struct gb_connection *connection;
95 	struct list_head list;
96 	/* maintain runtime state for playback/capture stream */
97 	int state[2];
98 };
99 
100 /* stream direction */
101 #define GB_PLAYBACK	BIT(0)
102 #define GB_CAPTURE	BIT(1)
103 
104 enum gbaudio_module_state {
105 	GBAUDIO_MODULE_OFF = 0,
106 	GBAUDIO_MODULE_ON,
107 };
108 
109 struct gbaudio_module_info {
110 	/* module info */
111 	struct device *dev;
112 	int dev_id;	/* check if it should be bundle_id/hd_cport_id */
113 	int vid;
114 	int pid;
115 	int type;
116 	int set_uevent;
117 	char vstr[NAME_SIZE];
118 	char pstr[NAME_SIZE];
119 	struct list_head list;
120 	/* need to share this info to above user space */
121 	int manager_id;
122 	char name[NAME_SIZE];
123 	unsigned int ip_devices;
124 	unsigned int op_devices;
125 
126 	/* jack related */
127 	char jack_name[NAME_SIZE];
128 	char button_name[NAME_SIZE];
129 	int jack_type;
130 	int jack_mask;
131 	int button_mask;
132 	int button_status;
133 	struct snd_soc_jack headset_jack;
134 	struct snd_soc_jack button_jack;
135 
136 	/* connection info */
137 	struct gb_connection *mgmt_connection;
138 	size_t num_data_connections;
139 	struct list_head data_list;
140 
141 	/* topology related */
142 	int num_dais;
143 	int num_controls;
144 	int num_dapm_widgets;
145 	int num_dapm_routes;
146 	unsigned long dai_offset;
147 	unsigned long widget_offset;
148 	unsigned long control_offset;
149 	unsigned long route_offset;
150 	struct snd_kcontrol_new *controls;
151 	struct snd_soc_dapm_widget *dapm_widgets;
152 	struct snd_soc_dapm_route *dapm_routes;
153 	struct snd_soc_dai_driver *dais;
154 
155 	struct list_head widget_list;
156 	struct list_head ctl_list;
157 	struct list_head widget_ctl_list;
158 
159 	struct gb_audio_topology *topology;
160 };
161 
162 int gbaudio_tplg_parse_data(struct gbaudio_module_info *module,
163 			    struct gb_audio_topology *tplg_data);
164 void gbaudio_tplg_release(struct gbaudio_module_info *module);
165 
166 int gbaudio_module_update(struct gbaudio_codec_info *codec,
167 			  struct snd_soc_dapm_widget *w,
168 			  struct gbaudio_module_info *module,
169 			  int enable);
170 int gbaudio_register_module(struct gbaudio_module_info *module);
171 void gbaudio_unregister_module(struct gbaudio_module_info *module);
172 
173 /* protocol related */
174 int gb_audio_gb_get_topology(struct gb_connection *connection,
175 			     struct gb_audio_topology **topology);
176 int gb_audio_gb_get_control(struct gb_connection *connection,
177 			    u8 control_id, u8 index,
178 			    struct gb_audio_ctl_elem_value *value);
179 int gb_audio_gb_set_control(struct gb_connection *connection,
180 			    u8 control_id, u8 index,
181 			    struct gb_audio_ctl_elem_value *value);
182 int gb_audio_gb_enable_widget(struct gb_connection *connection,
183 			      u8 widget_id);
184 int gb_audio_gb_disable_widget(struct gb_connection *connection,
185 			       u8 widget_id);
186 int gb_audio_gb_get_pcm(struct gb_connection *connection,
187 			u16 data_cport, u32 *format,
188 			u32 *rate, u8 *channels,
189 			u8 *sig_bits);
190 int gb_audio_gb_set_pcm(struct gb_connection *connection,
191 			u16 data_cport, u32 format,
192 			u32 rate, u8 channels,
193 			u8 sig_bits);
194 int gb_audio_gb_set_tx_data_size(struct gb_connection *connection,
195 				 u16 data_cport, u16 size);
196 int gb_audio_gb_activate_tx(struct gb_connection *connection,
197 			    u16 data_cport);
198 int gb_audio_gb_deactivate_tx(struct gb_connection *connection,
199 			      u16 data_cport);
200 int gb_audio_gb_set_rx_data_size(struct gb_connection *connection,
201 				 u16 data_cport, u16 size);
202 int gb_audio_gb_activate_rx(struct gb_connection *connection,
203 			    u16 data_cport);
204 int gb_audio_gb_deactivate_rx(struct gb_connection *connection,
205 			      u16 data_cport);
206 int gb_audio_apbridgea_set_config(struct gb_connection *connection,
207 				  __u16 i2s_port, __u32 format,
208 				  __u32 rate, __u32 mclk_freq);
209 int gb_audio_apbridgea_register_cport(struct gb_connection *connection,
210 				      __u16 i2s_port, __u16 cportid,
211 				      __u8 direction);
212 int gb_audio_apbridgea_unregister_cport(struct gb_connection *connection,
213 					__u16 i2s_port, __u16 cportid,
214 					__u8 direction);
215 int gb_audio_apbridgea_set_tx_data_size(struct gb_connection *connection,
216 					__u16 i2s_port, __u16 size);
217 int gb_audio_apbridgea_prepare_tx(struct gb_connection *connection,
218 				  __u16 i2s_port);
219 int gb_audio_apbridgea_start_tx(struct gb_connection *connection,
220 				__u16 i2s_port, __u64 timestamp);
221 int gb_audio_apbridgea_stop_tx(struct gb_connection *connection,
222 			       __u16 i2s_port);
223 int gb_audio_apbridgea_shutdown_tx(struct gb_connection *connection,
224 				   __u16 i2s_port);
225 int gb_audio_apbridgea_set_rx_data_size(struct gb_connection *connection,
226 					__u16 i2s_port, __u16 size);
227 int gb_audio_apbridgea_prepare_rx(struct gb_connection *connection,
228 				  __u16 i2s_port);
229 int gb_audio_apbridgea_start_rx(struct gb_connection *connection,
230 				__u16 i2s_port);
231 int gb_audio_apbridgea_stop_rx(struct gb_connection *connection,
232 			       __u16 i2s_port);
233 int gb_audio_apbridgea_shutdown_rx(struct gb_connection *connection,
234 				   __u16 i2s_port);
235 
236 #endif /* __LINUX_GBAUDIO_CODEC_H */
237