summaryrefslogtreecommitdiff
path: root/sound/soc/fsl/fsl_sai_sysfs.c
blob: 920ebcccc4276ec1282dd45cc5caa929a756c2b7 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// SPDX-License-Identifier: GPL-2.0+
// Copyright 2020 NXP Semiconductors.

#include <linux/clk.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/kdev_t.h>
#include <linux/pm_runtime.h>
#include <linux/mfd/syscon/imx7-iomuxc-gpr.h>

#include "fsl_sai.h"

static ssize_t tx_bitcnt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	/* read bitcounter */
	regmap_read(sai->regmap, FSL_SAI_TBCTN, &val);

	return sprintf(buf, "%u\n", val);
}
static DEVICE_ATTR_RO(tx_bitcnt);

static ssize_t tx_timestamp_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	/* read timestamp */
	regmap_read(sai->regmap, FSL_SAI_TTCTN, &val);

	return sprintf(buf, "%u\n", val);
}
static DEVICE_ATTR_RO(tx_timestamp);

static ssize_t tx_bitcnt_latched_timestamp_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	/* read timestamp */
	regmap_read(sai->regmap, FSL_SAI_TTCAP, &val);

	return sprintf(buf, "%u\n", val);
}
static DEVICE_ATTR_RO(tx_bitcnt_latched_timestamp);

static ssize_t
tx_timestamp_enable_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	if (kstrtouint(buf, 0, &val))
		return -EINVAL;

	if (val != 0)
		val = FSL_SAI_xTCTL_TSEN;
	regmap_update_bits(sai->regmap, FSL_SAI_TTCTL, FSL_SAI_xTCTL_TSEN, val);
	return n;
}
static DEVICE_ATTR_WO(tx_timestamp_enable);

static ssize_t
tx_timestamp_increment_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	if (kstrtouint(buf, 0, &val))
		return -EINVAL;

	if (val != 0)
		val = FSL_SAI_xTCTL_TSINC;
	regmap_update_bits(sai->regmap, FSL_SAI_TTCTL, FSL_SAI_xTCTL_TSINC, val);
	return n;
}
static DEVICE_ATTR_WO(tx_timestamp_increment);

static ssize_t
tx_bitcnt_reset_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	if (kstrtouint(buf, 0, &val))
		return -EINVAL;

	if (val != 0)
		val = FSL_SAI_xTCTL_RBC;
	regmap_update_bits(sai->regmap, FSL_SAI_TTCTL, FSL_SAI_xTCTL_RBC, val);
	return n;
}
static DEVICE_ATTR_WO(tx_bitcnt_reset);

static ssize_t
tx_timestamp_reset_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	if (kstrtouint(buf, 0, &val))
		return -EINVAL;

	if (val != 0)
		val = FSL_SAI_xTCTL_RTSC;
	regmap_update_bits(sai->regmap, FSL_SAI_TTCTL, FSL_SAI_xTCTL_RTSC, val);
	return n;
}
static DEVICE_ATTR_WO(tx_timestamp_reset);


static ssize_t rx_bitcnt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	/* read bitcounter */
	regmap_read(sai->regmap, FSL_SAI_RBCTN, &val);

	return sprintf(buf, "%u\n", val);
}
static DEVICE_ATTR_RO(rx_bitcnt);

static ssize_t rx_timestamp_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	/* read timestamp */
	regmap_read(sai->regmap, FSL_SAI_RTCTN, &val);

	return sprintf(buf, "%u\n", val);
}
static DEVICE_ATTR_RO(rx_timestamp);

static ssize_t rx_bitcnt_latched_timestamp_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	/* read timestamp */
	regmap_read(sai->regmap, FSL_SAI_RTCAP, &val);

	return sprintf(buf, "%u\n", val);
}
static DEVICE_ATTR_RO(rx_bitcnt_latched_timestamp);

static ssize_t
rx_timestamp_enable_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	if (kstrtouint(buf, 0, &val))
		return -EINVAL;

	if (val != 0)
		val = FSL_SAI_xTCTL_TSEN;
	regmap_update_bits(sai->regmap, FSL_SAI_RTCTL, FSL_SAI_xTCTL_TSEN, val);
	return n;
}
static DEVICE_ATTR_WO(rx_timestamp_enable);

static ssize_t
rx_timestamp_increment_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	if (kstrtouint(buf, 0, &val))
		return -EINVAL;

	if (val != 0)
		val = FSL_SAI_xTCTL_TSINC;
	regmap_update_bits(sai->regmap, FSL_SAI_RTCTL, FSL_SAI_xTCTL_TSINC, val);
	return n;
}
static DEVICE_ATTR_WO(rx_timestamp_increment);

static ssize_t
rx_bitcnt_reset_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	if (kstrtouint(buf, 0, &val))
		return -EINVAL;

	if (val != 0)
		val = FSL_SAI_xTCTL_RBC;
	regmap_update_bits(sai->regmap, FSL_SAI_RTCTL, FSL_SAI_xTCTL_RBC, val);
	return n;
}
static DEVICE_ATTR_WO(rx_bitcnt_reset);

static ssize_t
rx_timestamp_reset_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned int val = 0;

	if (kstrtouint(buf, 0, &val))
		return -EINVAL;

	if (val != 0)
		val = FSL_SAI_xTCTL_RTSC;
	regmap_update_bits(sai->regmap, FSL_SAI_RTCTL, FSL_SAI_xTCTL_RTSC, val);
	return n;
}
static DEVICE_ATTR_WO(rx_timestamp_reset);

static ssize_t
rx_monitor_spdif_store(struct device *dev, struct device_attribute *attr,
			const char *buf, size_t n)
{
	struct fsl_sai *sai = dev_get_drvdata(dev);
	unsigned char offset = sai->soc->reg_offset;
	unsigned int val = 0;
	bool enable = false;
	unsigned int reg;
	unsigned int shift;
	unsigned int mask;

	if (kstrtouint(buf, 0, &val))
		return -EINVAL;

	if (val != 0)
		enable = true;

	if (pm_runtime_active(&sai->pdev->dev) && enable) {
		dev_err(dev, "device is busy\n");
		return -EBUSY;
	}

	reg = IOMUXC_GPR6 + (sai->gpr_idx - 1) / 2 * 4;
	shift = ((sai->gpr_idx - 1) % 2) * 16;
	mask = 0x1F << shift;

	if (enable) {
		pm_runtime_get_sync(&sai->pdev->dev);
		/* Fix to MCLK3 */
		regmap_update_bits(sai->regmap_gpr, reg, mask, 0xF << shift);
		regmap_update_bits(sai->regmap, FSL_SAI_xCR2(false, offset),
				   FSL_SAI_CR2_MSEL_MASK, FSL_SAI_CR2_MSEL(0x3));
		regmap_update_bits(sai->regmap, FSL_SAI_xCR2(false, offset),
				   FSL_SAI_CR2_DIV_MASK, 0x0);
		regmap_update_bits(sai->regmap, FSL_SAI_xCR2(false, offset),
				   FSL_SAI_CR2_BCD_MSTR, FSL_SAI_CR2_BCD_MSTR);
		regmap_update_bits(sai->regmap, FSL_SAI_xCR4(false, offset),
				   FSL_SAI_CR4_FSD_MSTR, FSL_SAI_CR4_FSD_MSTR);
		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(false, offset),
				   FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
		sai->monitor_spdif_start = true;
	} else {
		if (sai->monitor_spdif_start) {
			regmap_update_bits(sai->regmap, FSL_SAI_xCSR(false, offset),
					   FSL_SAI_CSR_TERE, 0);
			pm_runtime_put_sync(&sai->pdev->dev);
			sai->monitor_spdif_start = false;
		}
	}

	return n;
}
static DEVICE_ATTR_WO(rx_monitor_spdif);

static struct attribute *fsl_sai_attrs[] = {
	&dev_attr_tx_bitcnt.attr,
	&dev_attr_rx_bitcnt.attr,
	&dev_attr_tx_timestamp.attr,
	&dev_attr_rx_timestamp.attr,
	&dev_attr_tx_bitcnt_latched_timestamp.attr,
	&dev_attr_rx_bitcnt_latched_timestamp.attr,
	&dev_attr_tx_timestamp_enable.attr,
	&dev_attr_rx_timestamp_enable.attr,
	&dev_attr_tx_timestamp_increment.attr,
	&dev_attr_rx_timestamp_increment.attr,
	&dev_attr_tx_bitcnt_reset.attr,
	&dev_attr_rx_bitcnt_reset.attr,
	&dev_attr_tx_timestamp_reset.attr,
	&dev_attr_rx_timestamp_reset.attr,
	NULL,
	NULL,
};

static struct attribute_group fsl_sai_attr_group = {
	.attrs = fsl_sai_attrs,
};

const struct attribute_group *fsl_sai_get_dev_attribute_group(bool monitor_spdif)
{
	if (monitor_spdif)
		fsl_sai_attrs[ARRAY_SIZE(fsl_sai_attrs) - 2] = &dev_attr_rx_monitor_spdif.attr;

	return &fsl_sai_attr_group;
}