hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2023 Rockchip Electronics Co., Ltd.
 */
 
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/regmap.h>
#include <sound/core.h>
#include <sound/control.h>
#include <sound/soc.h>
 
#include "tda7803.h"
 
#define TDA7803_SAMPLE_RATE \
       (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
        SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
 
struct tda7803_priv {
   struct regmap *regmap;
   u32 input_format;
};
 
static int tda7803_startup(struct snd_pcm_substream *substream,
              struct snd_soc_dai *dai)
{
   struct snd_soc_component *component = dai->component;
   struct tda7803_priv *tda7803 = snd_soc_component_get_drvdata(component);
   struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
   struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
   int val = 0;
 
   snd_soc_component_write(component, TDA7803_REG2, DIGITAL_MUTE_OFF |
               CH2_4_UMUTE | CH1_3_UMUTE |
               MUTE_TIME_SETTING_1_45MS);
   snd_soc_component_write(component, TDA7803_REG7, AMPLIEFIR_SWITCH_ON);
 
   switch (tda7803->input_format) {
   case 0:
       val = INPUT_FORMAT_TDM_8CH_MODEL1;
       break;
   case 1:
       val = INPUT_FORMAT_TDM_8CH_MODEL2;
       break;
   default:
       return -EINVAL;
   }
 
   snd_soc_dai_set_fmt(codec_dai, val);
 
   return 0;
}
 
static int tda7803_hw_params(struct snd_pcm_substream *substream,
                struct snd_pcm_hw_params *params,
                struct snd_soc_dai *dai)
{
   struct snd_soc_component *component = dai->component;
   int val = 0;
 
   switch (params_rate(params)) {
   case 44100:
       val = SAMPLE_FREQUENCY_RANGE_44100HZ;
       break;
   case 48000:
       val = SAMPLE_FREQUENCY_RANGE_48000HZ;
       break;
   case 96000:
       val = SAMPLE_FREQUENCY_RANGE_96000HZ;
       break;
   case 192000:
       val = SAMPLE_FREQUENCY_RANGE_192000HZ;
       break;
   default:
       return -EINVAL;
   }
 
   snd_soc_component_write(component, TDA7803_REG3, val);
 
   return 0;
}
 
static int tda7803_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
   struct snd_soc_component *component = dai->component;
 
   snd_soc_component_write(component, TDA7803_REG3, fmt);
 
   return 0;
}
 
static const struct snd_soc_dai_ops tda7803_ops = {
   .startup = tda7803_startup,
   .hw_params = tda7803_hw_params,
   .set_fmt = tda7803_set_fmt,
};
 
static struct snd_soc_dai_driver tda7803_dai = {
   .name = "tda7803-hifi",
   .playback = {
       .stream_name = "Playback",
       .channels_min = 2,
       .channels_max = 8,
       .rates = TDA7803_SAMPLE_RATE,
       .formats = SNDRV_PCM_FMTBIT_S32_LE |
              SNDRV_PCM_FMTBIT_S24_LE |
              SNDRV_PCM_FMTBIT_S16_LE,
   },
   .ops = &tda7803_ops,
};
 
static const struct snd_soc_component_driver soc_codec_dev_tda7803 = {
   .name = "tda7803",
};
 
static const struct regmap_config tda7803_i2c_regmap = {
   .reg_bits = 8,
   .val_bits = 8,
   .max_register = TDA7803_REGMAX,
   .cache_type = REGCACHE_RBTREE,
};
 
static int tda7803_i2c_probe(struct i2c_client *i2c,
                const struct i2c_device_id *id)
{
   struct tda7803_priv *tda7803;
   int val;
 
   tda7803 = devm_kzalloc(&i2c->dev, sizeof(*tda7803), GFP_KERNEL);
   if (!tda7803)
       return -ENOMEM;
 
   i2c_set_clientdata(i2c, tda7803);
 
   tda7803->regmap = devm_regmap_init_i2c(i2c, &tda7803_i2c_regmap);
   if (IS_ERR(tda7803->regmap))
       return PTR_ERR(tda7803->regmap);
 
   if (!device_property_read_u32(&i2c->dev, "st,tda7803-format", &val))
       tda7803->input_format = val;
 
   return devm_snd_soc_register_component(&i2c->dev,
                          &soc_codec_dev_tda7803,
                          &tda7803_dai, 1);
}
 
static const struct i2c_device_id tda7803_i2c_id[] = {
   { "tda7803", 0 },
   { }
};
MODULE_DEVICE_TABLE(i2c, tda7803_i2c_id);
 
static const struct of_device_id tda7803_of_match[] = {
   { .compatible = "st,tda7803" },
   { },
};
 
static struct i2c_driver tda7803_i2c_driver = {
   .driver = {
       .name   = "tda7803",
       .of_match_table = of_match_ptr(tda7803_of_match),
   },
   .probe          = tda7803_i2c_probe,
   .id_table       = tda7803_i2c_id,
};
module_i2c_driver(tda7803_i2c_driver);
 
MODULE_AUTHOR("Jun Zeng <jun.zeng@rock-chips.com>");
MODULE_DESCRIPTION("TDA7803 audio processor driver");
MODULE_LICENSE("GPL");