2 * tc358749x.c TC358749XBG ALSA SoC audio codec driver
4 * Copyright (c) 2016 Fuzhou Rockchip Electronics Co., Ltd
5 * Author: Roy <luoxiaotan@rock-chips.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.*
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/gpio.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/init.h>
26 #include <linux/i2c.h>
27 #include <linux/of_gpio.h>
28 #include <linux/regmap.h>
29 #include <sound/soc.h>
31 #include "tc358749x.h"
33 static int snd_tc358749x_dai_hw_params(struct snd_pcm_substream *substream,
34 struct snd_pcm_hw_params *params,
35 struct snd_soc_dai *codec_dai)
37 struct snd_soc_codec *codec = codec_dai->codec;
40 switch (params_rate(params)) {
63 dev_err(codec->dev, "Enter:%s, %d, Error rate=%d\n",
64 __func__, __LINE__, params_rate(params));
67 snd_soc_update_bits(codec, TC358749X_FS_SET, FS_SET_MASK, fs);
71 static int snd_tc358749x_mute(struct snd_soc_dai *dai, int mute)
73 struct snd_soc_codec *codec = dai->codec;
76 snd_soc_update_bits(codec, TC358749X_FORCE_MUTE,
77 FORCE_DMUTE_MASK, MUTE);
79 snd_soc_update_bits(codec, TC358749X_FORCE_MUTE,
80 FORCE_DMUTE_MASK, !MUTE);
85 static const struct snd_soc_dai_ops tc358749x_dai_ops = {
86 .hw_params = snd_tc358749x_dai_hw_params,
87 .digital_mute = snd_tc358749x_mute,
90 static struct snd_soc_dai_driver tc358749x_dai = {
91 .name = "tc358749x-audio",
93 .stream_name = "Playback",
96 .rates = SNDRV_PCM_RATE_32000 |
97 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
98 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
99 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
100 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
104 .stream_name = "Capture",
107 .rates = SNDRV_PCM_RATE_32000 |
108 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
109 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
110 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
111 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
113 .ops = &tc358749x_dai_ops,
116 static int tc358749x_probe(struct snd_soc_codec *codec)
118 snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
122 static int tc358749_set_bias_level(struct snd_soc_codec *codec,
123 enum snd_soc_bias_level level)
126 case SND_SOC_BIAS_ON:
129 case SND_SOC_BIAS_PREPARE:
130 snd_soc_update_bits(codec, TC358749X_FORCE_MUTE,
131 FORCE_DMUTE_MASK, !MUTE);
134 case SND_SOC_BIAS_STANDBY:
137 case SND_SOC_BIAS_OFF:
138 snd_soc_update_bits(codec, TC358749X_FORCE_MUTE,
139 FORCE_DMUTE_MASK, MUTE);
146 static struct snd_soc_codec_driver soc_codec_dev_tc358749x = {
147 .probe = tc358749x_probe,
148 .set_bias_level = tc358749_set_bias_level,
151 static bool tc358749x_readable_register(struct device *dev, unsigned int reg)
154 case TC358749X_FORCE_MUTE:
155 case TC358749X_FS_SET:
162 static const struct reg_default tc358749x_reg_defaults[] = {
163 { TC358749X_FORCE_MUTE, 0xb1 },
164 { TC358749X_FS_SET, 0x00 },
167 const struct regmap_config tc358749x_regmap_config = {
170 .max_register = TC358749X_FS_SET,
171 .cache_type = REGCACHE_RBTREE,
172 .reg_defaults = tc358749x_reg_defaults,
173 .num_reg_defaults = ARRAY_SIZE(tc358749x_reg_defaults),
174 .readable_reg = tc358749x_readable_register,
177 static const struct i2c_device_id tc358749x_i2c_id[] = {
182 MODULE_DEVICE_TABLE(i2c, tc358749x_i2c_id);
184 static int tc358749x_parse_dts(struct i2c_client *i2c,
185 struct tc358749x_priv *tc358749x)
188 struct device *dev = &i2c->dev;
190 tc358749x->gpio_int = devm_gpiod_get_optional(dev, "int",
192 if (IS_ERR(tc358749x->gpio_int)) {
193 ret = PTR_ERR(tc358749x->gpio_int);
194 dev_err(&i2c->dev, "Unable to claim gpio \"int\".\n");
197 /* I2C Slave Address selection through boot-strap */
198 gpiod_direction_output(tc358749x->gpio_int, 0);
200 tc358749x->gpio_power = devm_gpiod_get_optional(dev, "power",
202 if (IS_ERR(tc358749x->gpio_power)) {
203 ret = PTR_ERR(tc358749x->gpio_power);
204 dev_err(&i2c->dev, "Unable to claim gpio \"power\".\n");
207 gpiod_direction_output(tc358749x->gpio_power, 1);
209 tc358749x->gpio_power18 = devm_gpiod_get_optional(dev, "power18",
211 if (IS_ERR(tc358749x->gpio_power18)) {
212 ret = PTR_ERR(tc358749x->gpio_power18);
213 dev_err(&i2c->dev, "Unable to claim gpio \"power18\".\n");
216 gpiod_direction_output(tc358749x->gpio_power18, 1);
218 tc358749x->gpio_power33 = devm_gpiod_get_optional(dev, "power33",
220 if (IS_ERR(tc358749x->gpio_power33)) {
221 ret = PTR_ERR(tc358749x->gpio_power33);
222 dev_err(&i2c->dev, "Unable to claim gpio \"power33\".\n");
225 gpiod_direction_output(tc358749x->gpio_power33, 1);
227 tc358749x->gpio_csi_ctl = devm_gpiod_get_optional(dev, "csi-ctl",
229 if (IS_ERR(tc358749x->gpio_csi_ctl)) {
230 ret = PTR_ERR(tc358749x->gpio_csi_ctl);
231 dev_err(&i2c->dev, "Unable to claim gpio \"csi-ctl\".\n");
234 gpiod_direction_output(tc358749x->gpio_csi_ctl, 0);
236 tc358749x->gpio_reset = devm_gpiod_get_optional(dev, "reset",
238 if (IS_ERR(tc358749x->gpio_reset)) {
239 ret = PTR_ERR(tc358749x->gpio_reset);
240 dev_err(&i2c->dev, "Unable to claim gpio \"reset\".\n");
243 gpiod_direction_output(tc358749x->gpio_reset, 1);
245 tc358749x->gpio_stanby = devm_gpiod_get_optional(dev, "stanby",
247 if (IS_ERR(tc358749x->gpio_stanby)) {
248 ret = PTR_ERR(tc358749x->gpio_stanby);
249 dev_err(&i2c->dev, "Unable to claim gpio \"stanby\".\n");
252 gpiod_direction_output(tc358749x->gpio_stanby, 1);
254 /* Wait 10ms tc358749x lock I2C Slave address */
255 usleep_range(10000, 11000);
256 /* after I2C address has been lock and set it input */
257 gpiod_direction_input(tc358749x->gpio_int);
261 static int tc358749x_i2c_probe(struct i2c_client *i2c,
262 const struct i2c_device_id *id)
264 struct tc358749x_priv *tc358749x;
267 tc358749x = devm_kzalloc(&i2c->dev, sizeof(*tc358749x),
272 i2c_set_clientdata(i2c, tc358749x);
273 tc358749x_parse_dts(i2c, tc358749x);
275 tc358749x->regmap = devm_regmap_init_i2c(i2c, &tc358749x_regmap_config);
276 if (IS_ERR(tc358749x->regmap)) {
277 ret = PTR_ERR(tc358749x->regmap);
278 dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
282 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_tc358749x,
285 dev_info(&i2c->dev, "%s success\n", __func__);
289 static int tc358749x_i2c_remove(struct i2c_client *i2c)
291 snd_soc_unregister_codec(&i2c->dev);
296 static struct i2c_driver tc358749x_i2c_driver = {
300 .probe = tc358749x_i2c_probe,
301 .remove = tc358749x_i2c_remove,
302 .id_table = tc358749x_i2c_id,
304 module_i2c_driver(tc358749x_i2c_driver);
306 MODULE_AUTHOR("Roy <luoxiaotan@rock-chips.com>");
307 MODULE_DESCRIPTION("TC358749X HDMI Audio RX ASoC Interface");
308 MODULE_LICENSE("GPL");