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