summaryrefslogtreecommitdiff
path: root/common/audio_codec_i2s_rx.c
blob: 700fc41024b1b94fa108f9f6f644a16974e3e96d (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
/*
 * Copyright 2019 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "audio_codec.h"
#include "console.h"
#include "host_command.h"

#define CPRINTS(format, args...) cprints(CC_AUDIO_CODEC, format, ## args)

static uint8_t i2s_rx_enabled;

static enum ec_status i2s_rx_enable(struct host_cmd_handler_args *args)
{
	if (i2s_rx_enabled)
		return EC_RES_BUSY;

	if (audio_codec_i2s_rx_enable() != EC_SUCCESS)
		return EC_RES_ERROR;

	i2s_rx_enabled = 1;

	return EC_RES_SUCCESS;
}

static enum ec_status i2s_rx_disable(struct host_cmd_handler_args *args)
{
	if (!i2s_rx_enabled)
		return EC_RES_BUSY;

	if (audio_codec_i2s_rx_disable() != EC_SUCCESS)
		return EC_RES_ERROR;

	i2s_rx_enabled = 0;

	return EC_RES_SUCCESS;
}

static enum ec_status
i2s_rx_set_sample_depth(struct host_cmd_handler_args *args)
{
	const struct ec_param_ec_codec_i2s_rx *p = args->params;
	const uint8_t depth = p->set_sample_depth_param.depth;

	if (i2s_rx_enabled)
		return EC_RES_BUSY;
	if (depth >= EC_CODEC_I2S_RX_SAMPLE_DEPTH_COUNT)
		return EC_RES_INVALID_PARAM;

	if (audio_codec_i2s_rx_set_sample_depth(depth) != EC_SUCCESS)
		return EC_RES_ERROR;

	return EC_RES_SUCCESS;
}

static enum ec_status i2s_rx_set_daifmt(struct host_cmd_handler_args *args)
{
	const struct ec_param_ec_codec_i2s_rx *p = args->params;
	const uint8_t daifmt = p->set_daifmt_param.daifmt;

	if (i2s_rx_enabled)
		return EC_RES_BUSY;
	if (daifmt >= EC_CODEC_I2S_RX_DAIFMT_COUNT)
		return EC_RES_INVALID_PARAM;

	if (audio_codec_i2s_rx_set_daifmt(daifmt) != EC_SUCCESS)
		return EC_RES_ERROR;

	return EC_RES_SUCCESS;
}

static enum ec_status i2s_rx_set_bclk(struct host_cmd_handler_args *args)
{
	const struct ec_param_ec_codec_i2s_rx *p = args->params;

	if (i2s_rx_enabled)
		return EC_RES_BUSY;

	if (audio_codec_i2s_rx_set_bclk(p->set_bclk_param.bclk) != EC_SUCCESS)
		return EC_RES_ERROR;

	return EC_RES_SUCCESS;
}

static enum ec_status (*sub_cmds[])(struct host_cmd_handler_args *) = {
	[EC_CODEC_I2S_RX_ENABLE] = i2s_rx_enable,
	[EC_CODEC_I2S_RX_DISABLE] = i2s_rx_disable,
	[EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH] = i2s_rx_set_sample_depth,
	[EC_CODEC_I2S_RX_SET_DAIFMT] = i2s_rx_set_daifmt,
	[EC_CODEC_I2S_RX_SET_BCLK] = i2s_rx_set_bclk,
};

#ifdef DEBUG_AUDIO_CODEC
static char *strcmd[] = {
	[EC_CODEC_I2S_RX_ENABLE] = "EC_CODEC_I2S_RX_ENABLE",
	[EC_CODEC_I2S_RX_DISABLE] = "EC_CODEC_I2S_RX_DISABLE",
	[EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH] = "EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH",
	[EC_CODEC_I2S_RX_SET_DAIFMT] = "EC_CODEC_I2S_RX_SET_DAIFMT",
	[EC_CODEC_I2S_RX_SET_BCLK] = "EC_CODEC_I2S_RX_SET_BCLK",
};
BUILD_ASSERT(ARRAY_SIZE(sub_cmds) == ARRAY_SIZE(strcmd));
#endif

static enum ec_status i2s_rx_host_command(struct host_cmd_handler_args *args)
{
	const struct ec_param_ec_codec_i2s_rx *p = args->params;

#ifdef DEBUG_AUDIO_CODEC
	CPRINTS("I2S RX subcommand: %s", strcmd[p->cmd]);
#endif

	if (p->cmd < EC_CODEC_I2S_RX_SUBCMD_COUNT)
		return sub_cmds[p->cmd](args);

	return EC_RES_INVALID_PARAM;
}
DECLARE_HOST_COMMAND(EC_CMD_EC_CODEC_I2S_RX,
	i2s_rx_host_command, EC_VER_MASK(0));