summaryrefslogtreecommitdiff
path: root/src/lpe_ctl.c
blob: e0850b839b841381c22e9edac1ed5525ac6b3455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * sound-plugin-hal-clovertrail
 *
 * Copyright (C) 2013  Intel Corporation. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <mm_debug.h>
#include "lpe_ctl.h"
#define INTEL_SST_CTRL "/dev/intel_sst_ctrl"

static int prepare_module_header(snd_ppp_params_t *ppp_params, int algo_id, int stream_id, int enable_status)
{
    ppp_params->str_id   = stream_id;
    ppp_params->algo_id  = algo_id;
    ppp_params->enable   = enable_status;
    ppp_params->size     = 0;
    ppp_params->params = (unsigned char *)ppp_params + sizeof(snd_ppp_params_t);
    return 0;
}

static int add_parameter_blocks(snd_ppp_params_t *ppp_params, int param_type, int param_length, char *param_data)
{
    unsigned char *uptr;
    ipc_ia_params_block_t *pbptr;

    uptr = (unsigned char *)ppp_params->params + ppp_params->size;
    pbptr = (ipc_ia_params_block_t *)uptr;
    pbptr->type = param_type;
    pbptr->size = param_length;
    memcpy(pbptr->params, param_data, param_length);
    ppp_params->size += sizeof(ipc_ia_params_block_t) + param_length;
    return 0;
}

static int set_parameters(snd_ppp_params_t *ppp_params)
{
    int ret, handle;

    handle = open(INTEL_SST_CTRL, O_RDWR);
    if ( handle < 0) {
        debug_log("%s open failed: %s", INTEL_SST_CTRL, strerror(errno));
        return -1;
    }

    ret = ioctl(handle, SNDRV_SST_SET_ALGO, ppp_params);
    if (ret != 0) {
        debug_log("ioctl cmd SET_ALGO failed: %s", strerror(errno));
        ret = -1;
    }
    debug_log("set parameters success");
    close(handle);
    return ret;

}

static int get_parameters(snd_ppp_params_t *ppp_params)
{
    int ret, handle;
    unsigned char * charp;
    unsigned int i, dump_size=0;
    handle = open(INTEL_SST_CTRL, O_RDWR);
    if (handle < 0) {
        debug_log("%s open failed: %s", INTEL_SST_CTRL, strerror(errno));
        return -1;
    }
    ret = ioctl(handle, SNDRV_SST_GET_ALGO, ppp_params);
    if ( ret != 0) {
        debug_log("ioctl cmd GET_ALGO failed");
        ret = -1;
    }
    close(handle);

    if (!ret) {
        debug_log("get parameters success: Algo ID 0x%x Str ID %d, Enable %d, Operation %d, Size %d\n",
             ppp_params->algo_id, ppp_params->str_id, ppp_params->enable, ppp_params->reserved, ppp_params->size);
        debug_log("Dump recieved data, size=%d \n", ppp_params->size);
        dump_size = ppp_params->size;
        charp = (unsigned char *)ppp_params->params;
        for (i = 0; i < dump_size; i++, charp++)
            debug_log("%02x ", *charp);
    }
    return ret;
}

int lpe_get_parameter(int str_id, int algo_id, int param_id, int param_length)
{
    unsigned int param_data[64];
    char acparams[64];
    snd_ppp_params_t *ppp_params = (snd_ppp_params_t *)acparams;

    debug_log("lpe_get_parameter: str_id=0x%x, algo_id=0x%x, param_id=0x%x, param_length=%d\n",
            str_id, algo_id, param_id, param_length);
    // Prepare header and add data to parameter block
    prepare_module_header(ppp_params, algo_id, str_id, DONT_CHANGE_MOD_STATE);
    add_parameter_blocks(ppp_params, param_id, param_length, (char*)param_data);

    if (get_parameters(ppp_params) != 0) {
        debug_log("Unable to get parameter\n");
        return -1;
    }
    return 0;
}

int lpe_set_parameter(int str_id, int algo_id, int param_id, int param_length, char *param_data)
{
    char acparams[32];
    snd_ppp_params_t *ppp_params = (snd_ppp_params_t *)acparams;

    debug_log("lpe_set_parameter: str_id=0x%x, algo_id=0x%x, param_id=0x%x, param_length=%d\n",
            str_id, algo_id, param_id, param_length);
    // Prepare header and add data to parameter block
    prepare_module_header(ppp_params, algo_id, str_id, DONT_CHANGE_MOD_STATE);
    add_parameter_blocks(ppp_params, param_id, param_length, param_data);

    if (set_parameters(ppp_params) != 0) {
        debug_log("Unable to set parameter\n");
        return -1;
    }
    return 0;
}

int change_algo_state(int str_id, int algo_id, int state)
{
    char acparams[64];
    snd_ppp_params_t *ppp_params = (snd_ppp_params_t *)acparams;

    debug_log("change_algo_state: str_id=0x%x, algo_id=0x%x, state=%d\n", str_id, algo_id, state);
    prepare_module_header(ppp_params, algo_id, str_id, state);
    if (set_parameters(ppp_params) != 0) {
        debug_log("Unable to set parameter\n");
        return -1;
    }
    return 0;
}

int check_algo_state(int str_id, int algo_id)
{
    char acparams[64];
    snd_ppp_params_t *ppp_params = (snd_ppp_params_t *)acparams;

    debug_log("check_algo_state: str_id=0x%x, algo_id=0x%x\n", str_id, algo_id);
    prepare_module_header(ppp_params, algo_id, str_id, DONT_CHANGE_MOD_STATE);
    if (get_parameters(ppp_params) != 0) {
        debug_log("Unable to get parameter\n");
        return -1;
    }
    if (ppp_params->enable) {
        debug_log ("State: enable\n");
    } else {
        debug_log ("State: disable\n");
    }
    return 0;
}

int lpe_setup_clv()
{
    unsigned int param_data[32];

    debug_log("CLV: Enable MIC1_PATH/GAIN_CTRL\n");
    change_algo_state(0x05, LPE_ALGO_TYPE_VOL_CTRL, ENABLE);
    debug_log("CLV: Enable MIC2_PATH/GAIN_CTRL\n");
    change_algo_state(0x25, LPE_ALGO_TYPE_VOL_CTRL, ENABLE);

    param_data[0]=20;
    debug_log("Set MIC1_PATH/GAIN_CTRL/GAIN to value 20\n");
    lpe_set_parameter(0x05, LPE_ALGO_TYPE_VOL_CTRL, ALGO_PARAM_VOL_CTRL_GAIN, 1, (char *)param_data);
    debug_log("Set MIC2_PATH/GAIN_CTRL/GAIN to value 20\n");
    lpe_set_parameter(0x25, LPE_ALGO_TYPE_VOL_CTRL, ALGO_PARAM_VOL_CTRL_GAIN, 1, (char *)param_data);

    return 0;
}