a482c458506b7491b28eb0775ca6c6be0a60b5be
[yamaha-mc1n2-audio.git] / yamaha-mc1n2-audio.c
1 /*
2  * Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 #include <linux/ioctl.h>
27
28 #define LOG_TAG "Yamaha-MC1N2-Audio"
29 #include <cutils/log.h>
30
31 #include <system/audio.h>
32
33 #include <yamaha-mc1n2-audio.h>
34
35 struct yamaha_mc1n2_audio_pdata *yamaha_mc1n2_audio_platforms[] = {
36         &galaxys2_pdata,
37 };
38
39 int yamaha_mc1n2_audio_platforms_count = sizeof(yamaha_mc1n2_audio_platforms) /
40         sizeof(struct yamaha_mc1n2_audio_pdata *);
41
42 /*
43  * IOCTL
44  */
45
46 int yamaha_mc1n2_audio_ioctl(struct yamaha_mc1n2_audio_pdata *pdata,
47         int command, struct mc1n2_ctrl_args *hw_ctrl)
48 {
49         char *hw_node = NULL;
50         int hw_fd = -1;
51         int rc = -1;
52
53         if(pdata == NULL)
54                 return -1;
55
56         hw_node = yamaha_mc1n2_audio_get_hw_node(pdata);
57         if(hw_node == NULL) {
58                 LOGE("%s: error, missing hw_node!", __func__);
59                 return -1;
60         }
61
62         hw_fd = open(hw_node, O_RDWR);
63         if(hw_fd < 0) {
64                 LOGE("%s: error, unable to open hw_node (fd is %d)!", __func__, hw_fd);
65                 return -1;
66         }
67
68         rc = ioctl(hw_fd, command, hw_ctrl);
69         if(rc < 0) {
70                 LOGE("%s: error, ioctl on hw_node failed (rc is %d)!", __func__, rc);
71                 return -1;
72         }
73
74         close(hw_fd);
75
76         return rc;
77 }
78
79 int yamaha_mc1n2_audio_ioctl_set_ctrl(struct yamaha_mc1n2_audio_pdata *pdata,
80         unsigned long command, void *data, unsigned long update_info)
81 {
82         struct mc1n2_ctrl_args hw_ctrl;
83
84         if(pdata == NULL)
85                 return -1;
86
87         memset(&hw_ctrl, 0, sizeof(hw_ctrl));
88         hw_ctrl.dCmd = command;
89         hw_ctrl.pvPrm = data;
90         hw_ctrl.dPrm = update_info;
91
92         return yamaha_mc1n2_audio_ioctl(pdata, MC1N2_IOCTL_SET_CTRL, &hw_ctrl);
93 }
94
95 int yamaha_mc1n2_audio_ioctl_notify(struct yamaha_mc1n2_audio_pdata *pdata,
96         unsigned long command)
97 {
98         struct mc1n2_ctrl_args hw_ctrl;
99
100         if(pdata == NULL)
101                 return -1;
102
103         memset(&hw_ctrl, 0, sizeof(hw_ctrl));
104         hw_ctrl.dCmd = command;
105
106         return yamaha_mc1n2_audio_ioctl(pdata, MC1N2_IOCTL_NOTIFY, &hw_ctrl);
107 }
108
109 /*
110  * Routines
111  */
112
113 int yamaha_mc1n2_audio_init(struct yamaha_mc1n2_audio_pdata *pdata)
114 {
115         struct yamaha_mc1n2_audio_params_init *params = NULL;
116         int rc = -1;
117
118         if(pdata == NULL || pdata->ops == NULL)
119                 return -1;
120
121         params = pdata->ops->params.init;
122         if(params == NULL)
123                 return -1;
124
125         rc = yamaha_mc1n2_audio_ioctl_set_ctrl(pdata, MCDRV_SET_DAC,
126                 &params->dac_info, 0x07);
127         if(rc < 0) {
128                 LOGE("SET_DAC IOCTL failed, aborting!");
129                 return -1;
130         }
131
132         rc = yamaha_mc1n2_audio_ioctl_set_ctrl(pdata, MCDRV_SET_ADC,
133                 &params->adc_info, 0x07);
134         if(rc < 0) {
135                 LOGE("SET_ADC IOCTL failed, aborting!");
136                 return -1;
137         }
138
139         rc = yamaha_mc1n2_audio_ioctl_set_ctrl(pdata, MCDRV_SET_SP,
140                 &params->sp_info, 0x00);
141         if(rc < 0) {
142                 LOGE("SET_SP IOCTL failed, aborting!");
143                 return -1;
144         }
145
146         rc = yamaha_mc1n2_audio_ioctl_set_ctrl(pdata, MCDRV_SET_PDM,
147                 &params->pdm_info, 0x7f);
148         if(rc < 0) {
149                 LOGE("SET_PDM IOCTL failed, aborting!");
150                 return -1;
151         }
152
153         rc = yamaha_mc1n2_audio_ioctl_set_ctrl(pdata, MCDRV_SET_DNG,
154                 &params->dng_info, 0x3f3f3f);
155         if(rc < 0) {
156                 LOGE("SET_DNG IOCTL failed, aborting!");
157                 return -1;
158         }
159
160         rc = yamaha_mc1n2_audio_ioctl_set_ctrl(pdata, MCDRV_SET_SYSEQ,
161                 &params->syseq_info, 0x03);
162         if(rc < 0) {
163                 LOGE("SET_SYSEQ IOCTL failed, aborting!");
164                 return -1;
165         }
166
167         return 0;
168 }
169
170 struct yamaha_mc1n2_audio_params_route *
171         yamaha_mc1n2_audio_params_route_find(struct yamaha_mc1n2_audio_pdata *pdata,
172         audio_devices_t device, audio_mode_t mode)
173 {
174         struct yamaha_mc1n2_audio_params_route *params = NULL;
175         int params_count = 0;
176         int i;
177
178         if(pdata == NULL || pdata->ops == NULL)
179                 return NULL;
180
181         params = pdata->ops->params.routes;
182         params_count = pdata->ops->params.routes_count;
183         if(params == NULL || params_count <= 0)
184                 return NULL;
185
186         for(i=0 ; i < params_count ; i++) {
187                 if(params[i].device == device && params[i].mode == mode)
188                         return &params[i];
189         }
190
191         return NULL;
192 }
193
194 int yamaha_mc1n2_audio_params_route_simple_array_merge(int length,
195         unsigned char *array_src, unsigned char *array_dst)
196 {
197         int i;
198
199         if(length <= 0 || array_src == NULL || array_dst == NULL)
200                 return -1;
201
202         for(i=0 ; i < length ; i++) {
203                 if(array_dst[i] == 0)
204                         array_dst[i] = array_src[i];
205         }
206
207         return 0;
208 }
209
210 int yamaha_mc1n2_audio_params_route_path_array_merge(int length,
211         unsigned char *array_src, unsigned char *array_dst)
212 {
213         int i, j;
214         unsigned char v;
215
216         if(length <= 0 || array_src == NULL || array_dst == NULL)
217                 return -1;
218
219         for(i=0 ; i < length ; i++) {
220                 if(array_dst[i] == 0) {
221                         array_dst[i] = array_src[i];
222                 } else {
223                         v = array_src[i];
224
225                         for(j=0; j < 8 ; j++) {
226                                 if((1 << j) & array_dst[i]) {
227                                         if(j % 2 == 0)
228                                                 v &= ~(1 << (j+1));
229                                         else
230                                                 v &= ~(1 << (j-1));
231
232                                         v |= (1 << j);
233                                 }
234                         }
235
236                         array_dst[i] = v;
237                 }
238         }
239
240         return 0;
241 }
242
243 int yamaha_mc1n2_audio_params_route_merge(
244         struct yamaha_mc1n2_audio_params_route *params_src,
245         struct yamaha_mc1n2_audio_params_route *params_dst)
246 {
247         if(params_src == NULL || params_dst == NULL)
248                 return -1;
249
250         // ae_info
251         yamaha_mc1n2_audio_params_route_simple_array_merge(sizeof(params_src->ae_info.bOnOff),
252                 &params_src->ae_info.bOnOff, &params_dst->ae_info.bOnOff);
253         yamaha_mc1n2_audio_params_route_simple_array_merge(sizeof(params_src->ae_info.abBex),
254                 params_src->ae_info.abBex, params_dst->ae_info.abBex);
255         yamaha_mc1n2_audio_params_route_simple_array_merge(sizeof(params_src->ae_info.abWide),
256                 params_src->ae_info.abWide, params_dst->ae_info.abWide);
257         yamaha_mc1n2_audio_params_route_simple_array_merge(sizeof(params_src->ae_info.abDrc),
258                 params_src->ae_info.abDrc, params_dst->ae_info.abDrc);
259         yamaha_mc1n2_audio_params_route_simple_array_merge(sizeof(params_src->ae_info.abEq5),
260                 params_src->ae_info.abEq5, params_dst->ae_info.abEq5);
261         yamaha_mc1n2_audio_params_route_simple_array_merge(sizeof(params_src->ae_info.abEq3),
262                 params_src->ae_info.abEq3, params_dst->ae_info.abEq3);
263
264         // path_info
265         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asHpOut[0].abSrcOnOff),
266                 params_src->path_info.asHpOut[0].abSrcOnOff, params_dst->path_info.asHpOut[0].abSrcOnOff);
267         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asHpOut[1].abSrcOnOff),
268                 params_src->path_info.asHpOut[1].abSrcOnOff, params_dst->path_info.asHpOut[1].abSrcOnOff);
269         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asSpOut[0].abSrcOnOff),
270                 params_src->path_info.asSpOut[0].abSrcOnOff, params_dst->path_info.asSpOut[0].abSrcOnOff);
271         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asSpOut[1].abSrcOnOff),
272                 params_src->path_info.asSpOut[1].abSrcOnOff, params_dst->path_info.asSpOut[1].abSrcOnOff);
273         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asRcOut[0].abSrcOnOff),
274                 params_src->path_info.asRcOut[0].abSrcOnOff, params_dst->path_info.asRcOut[0].abSrcOnOff);
275         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asLout1[0].abSrcOnOff),
276                 params_src->path_info.asLout1[0].abSrcOnOff, params_dst->path_info.asLout1[0].abSrcOnOff);
277         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asLout1[1].abSrcOnOff),
278                 params_src->path_info.asLout1[1].abSrcOnOff, params_dst->path_info.asLout1[1].abSrcOnOff);
279         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asLout2[0].abSrcOnOff),
280                 params_src->path_info.asLout2[0].abSrcOnOff, params_dst->path_info.asLout2[0].abSrcOnOff);
281         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asLout2[1].abSrcOnOff),
282                 params_src->path_info.asLout2[1].abSrcOnOff, params_dst->path_info.asLout2[1].abSrcOnOff);
283         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asPeak[0].abSrcOnOff),
284                 params_src->path_info.asPeak[0].abSrcOnOff, params_dst->path_info.asPeak[0].abSrcOnOff);
285         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asDit0[0].abSrcOnOff),
286                 params_src->path_info.asDit0[0].abSrcOnOff, params_dst->path_info.asDit0[0].abSrcOnOff);
287         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asDit1[0].abSrcOnOff),
288                 params_src->path_info.asDit1[0].abSrcOnOff, params_dst->path_info.asDit1[0].abSrcOnOff);
289         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asDit2[0].abSrcOnOff),
290                 params_src->path_info.asDit2[0].abSrcOnOff, params_dst->path_info.asDit2[0].abSrcOnOff);
291         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asDac[0].abSrcOnOff),
292                 params_src->path_info.asDac[0].abSrcOnOff, params_dst->path_info.asDac[0].abSrcOnOff);
293         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asDac[1].abSrcOnOff),
294                 params_src->path_info.asDac[1].abSrcOnOff, params_dst->path_info.asDac[1].abSrcOnOff);
295         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asAe[0].abSrcOnOff),
296                 params_src->path_info.asAe[0].abSrcOnOff, params_dst->path_info.asAe[0].abSrcOnOff);
297         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asCdsp[0].abSrcOnOff),
298                 params_src->path_info.asCdsp[0].abSrcOnOff, params_dst->path_info.asCdsp[0].abSrcOnOff);
299         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asCdsp[1].abSrcOnOff),
300                 params_src->path_info.asCdsp[1].abSrcOnOff, params_dst->path_info.asCdsp[1].abSrcOnOff);
301         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asCdsp[2].abSrcOnOff),
302                 params_src->path_info.asCdsp[2].abSrcOnOff, params_dst->path_info.asCdsp[2].abSrcOnOff);
303         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asCdsp[3].abSrcOnOff),
304                 params_src->path_info.asCdsp[3].abSrcOnOff, params_dst->path_info.asCdsp[3].abSrcOnOff);
305         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asAdc0[0].abSrcOnOff),
306                 params_src->path_info.asAdc0[0].abSrcOnOff, params_dst->path_info.asAdc0[0].abSrcOnOff);
307         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asAdc0[1].abSrcOnOff),
308                 params_src->path_info.asAdc0[1].abSrcOnOff, params_dst->path_info.asAdc0[1].abSrcOnOff);
309         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asAdc1[0].abSrcOnOff),
310                 params_src->path_info.asAdc1[0].abSrcOnOff, params_dst->path_info.asAdc1[0].abSrcOnOff);
311         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asMix[0].abSrcOnOff),
312                 params_src->path_info.asMix[0].abSrcOnOff, params_dst->path_info.asMix[0].abSrcOnOff);
313         yamaha_mc1n2_audio_params_route_path_array_merge(sizeof(params_src->path_info.asBias[0].abSrcOnOff),
314                 params_src->path_info.asBias[0].abSrcOnOff, params_dst->path_info.asBias[0].abSrcOnOff);
315
316         // dac_info
317         yamaha_mc1n2_audio_params_route_simple_array_merge(sizeof(params_src->dac_info.bMasterSwap),
318                 &params_src->dac_info.bMasterSwap, &params_dst->dac_info.bMasterSwap);
319         yamaha_mc1n2_audio_params_route_simple_array_merge(sizeof(params_src->dac_info.bVoiceSwap),
320                 &params_src->dac_info.bVoiceSwap, &params_dst->dac_info.bVoiceSwap);
321         yamaha_mc1n2_audio_params_route_simple_array_merge(sizeof(params_src->dac_info.bDcCut),
322                 &params_src->dac_info.bDcCut, &params_dst->dac_info.bDcCut);
323
324         return 0;
325 }
326
327 int yamaha_mc1n2_audio_route_start(struct yamaha_mc1n2_audio_pdata *pdata)
328 {
329         struct yamaha_mc1n2_audio_params_route *params_route = NULL;
330         struct yamaha_mc1n2_audio_params_init *params_init = NULL;
331         struct yamaha_mc1n2_audio_params_route *params = NULL;
332         struct yamaha_mc1n2_audio_params_route params_src;
333         struct yamaha_mc1n2_audio_params_route params_dst;
334
335         int rc;
336
337         if(pdata == NULL || pdata->ops == NULL)
338                 return -1;
339
340         params_init = pdata->ops->params.init;
341         if(params_init == NULL)
342                 return -1;
343
344         // Copy the init params
345         memcpy(&params_src.ae_info, &params_init->ae_info, sizeof(params_src.ae_info));
346         memcpy(&params_src.path_info, &params_init->path_info, sizeof(params_src.path_info));
347         memcpy(&params_src.dac_info, &params_init->dac_info, sizeof(params_src.dac_info));
348
349 output_merge:
350         if(pdata->output_state) {
351                 params_route = yamaha_mc1n2_audio_params_route_find(pdata,
352                         pdata->output_device, pdata->mode);
353                 if(params_route == NULL)
354                         goto input_merge;
355
356                 memcpy(&params_dst, params_route, sizeof(params_dst));
357                 yamaha_mc1n2_audio_params_route_merge(&params_src, &params_dst);
358                 memcpy(&params_src, &params_dst, sizeof(params_src));
359         }
360
361 input_merge:
362         if(pdata->input_state) {
363                 params_route = yamaha_mc1n2_audio_params_route_find(pdata,
364                         pdata->input_device, pdata->mode);
365                 if(params_route == NULL)
366                         goto route_start;
367
368                 memcpy(&params_dst, params_route, sizeof(params_dst));
369                 yamaha_mc1n2_audio_params_route_merge(&params_src, &params_dst);
370                 memcpy(&params_src, &params_dst, sizeof(params_src));
371         }
372
373 route_start:
374         params = &(params_src);
375
376         rc = yamaha_mc1n2_audio_ioctl_set_ctrl(pdata, MCDRV_SET_AUDIOENGINE,
377                 &params->ae_info, 0x0f);
378         if(rc < 0) {
379                 LOGE("SET_AUDIOENGINE IOCTL failed, aborting!");
380                 return -1;
381         }
382
383         rc = yamaha_mc1n2_audio_ioctl_set_ctrl(pdata, MCDRV_SET_PATH,
384                 &params->path_info, 0x00);
385         if(rc < 0) {
386                 LOGE("SET_PATH IOCTL failed, aborting!");
387                 return -1;
388         }
389
390         rc = yamaha_mc1n2_audio_ioctl_set_ctrl(pdata, MCDRV_SET_DAC,
391                 &params->dac_info, 0x07);
392         if(rc < 0) {
393                 LOGE("SET_DAC IOCTL failed, aborting!");
394                 return -1;
395         }
396
397         return 0;
398 }
399
400 int yamaha_mc1n2_audio_output_start(struct yamaha_mc1n2_audio_pdata *pdata)
401 {
402         int rc;
403
404         if(pdata == NULL || pdata->ops == NULL)
405                 return -1;
406
407         pdata->output_state = 1;
408
409         rc = yamaha_mc1n2_audio_route_start(pdata);
410         if(rc < 0) {
411                 LOGE("Route start failed, aborting!");
412                 return -1;
413         }
414
415         rc = yamaha_mc1n2_audio_ioctl_notify(pdata, MCDRV_NOTIFY_MEDIA_PLAY_START);
416         if(rc < 0) {
417                 LOGE("NOTIFY_MEDIA_PLAY_START IOCTL failed, aborting!");
418                 return -1;
419         }
420
421         return 0;
422 }
423
424 int yamaha_mc1n2_audio_output_stop(struct yamaha_mc1n2_audio_pdata *pdata)
425 {
426         int rc;
427
428         if(pdata == NULL || pdata->ops == NULL)
429                 return -1;
430
431         pdata->output_state = 0;
432
433         rc = yamaha_mc1n2_audio_route_start(pdata);
434         if(rc < 0) {
435                 LOGE("Route start failed, aborting!");
436                 return -1;
437         }
438
439         rc = yamaha_mc1n2_audio_ioctl_notify(pdata, MCDRV_NOTIFY_MEDIA_PLAY_STOP);
440         if(rc < 0) {
441                 LOGE("NOTIFY_MEDIA_PLAY_STOP IOCTL failed, aborting!");
442                 return -1;
443         }
444
445         return 0;
446 }
447
448 /*
449  * Values configuration
450  */
451
452 int yamaha_mc1n2_audio_set_route(struct yamaha_mc1n2_audio_pdata *pdata,
453         audio_devices_t device, audio_mode_t mode)
454 {
455         int changed = 0;
456
457         if(pdata == NULL)
458                 return -1;
459
460         if(audio_is_output_device(device) && pdata->output_device != device) {
461                 pdata->output_device = device;
462                 changed = 1;
463         } else if(audio_is_input_device(device) && pdata->input_device != device) {
464                 pdata->input_device = device;
465                 changed = 1;
466         }
467
468         if(pdata->mode != mode) {
469                 pdata->mode = mode;
470                 changed = 1;
471         }
472
473         if(changed && (pdata->output_state || pdata->input_state))
474                 return yamaha_mc1n2_audio_route_start(pdata);
475
476         return 0;
477 }
478
479 char *yamaha_mc1n2_audio_get_hw_node(struct yamaha_mc1n2_audio_pdata *pdata)
480 {
481         if(pdata == NULL)
482                 return NULL;
483
484         return pdata->ops->hw_node;
485 }
486
487 /*
488  * Init/Deinit
489  */
490
491 struct yamaha_mc1n2_audio_pdata *yamaha_mc1n2_audio_platform_get(
492         char *device_name)
493 {
494         int i;
495
496         if(device_name == NULL)
497                 return NULL;
498
499         LOGD("Found %d registered platforms",
500                 yamaha_mc1n2_audio_platforms_count);
501
502         for(i=0 ; i < yamaha_mc1n2_audio_platforms_count ; i++) {
503                 if(yamaha_mc1n2_audio_platforms[i] != NULL &&
504                         yamaha_mc1n2_audio_platforms[i]->name != NULL) {
505                         if(strcmp(yamaha_mc1n2_audio_platforms[i]->name, device_name) == 0) {
506                                 return yamaha_mc1n2_audio_platforms[i];
507                         }
508                 }
509         }
510
511         return NULL;
512 }
513
514
515 int yamaha_mc1n2_audio_start(struct yamaha_mc1n2_audio_pdata **pdata_p,
516         char *device_name)
517 {
518         struct yamaha_mc1n2_audio_pdata *pdata = NULL;
519         int rc;
520
521         if(pdata_p == NULL || device_name == NULL)
522                 return -1;
523
524         pdata = yamaha_mc1n2_audio_platform_get(device_name);
525         if(pdata == NULL) {
526                 LOGE("Unable to find requested platform: %s", device_name);
527                 return -1;
528         }
529
530         *pdata_p = pdata;
531
532         return 0;
533 }
534
535 int yamaha_mc1n2_audio_stop(struct yamaha_mc1n2_audio_pdata *pdata)
536 {
537         if(pdata == NULL)
538                 return -1;
539
540         return 0;
541 }