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