blob: 09716fab1e26a4a57831846741d06a2ee17a37b1 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Tim Howee40da862015-07-16 14:51:40 -05002/*
3 * cs4349.c -- CS4349 ALSA Soc Audio driver
4 *
5 * Copyright 2015 Cirrus Logic, Inc.
6 *
7 * Authors: Tim Howe <Tim.Howe@cirrus.com>
Tim Howee40da862015-07-16 14:51:40 -05008 */
9
10#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/delay.h>
15#include <linux/gpio.h>
akpm@linux-foundation.org091571d2015-07-17 15:43:46 -070016#include <linux/gpio/consumer.h>
Tim Howee40da862015-07-16 14:51:40 -050017#include <linux/platform_device.h>
18#include <linux/pm.h>
19#include <linux/i2c.h>
20#include <linux/of_device.h>
21#include <linux/regmap.h>
22#include <linux/slab.h>
23#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
27#include <sound/soc-dapm.h>
28#include <sound/initval.h>
29#include <sound/tlv.h>
30#include "cs4349.h"
31
32
33static const struct reg_default cs4349_reg_defaults[] = {
34 { 2, 0x00 }, /* r02 - Mode Control */
35 { 3, 0x09 }, /* r03 - Volume, Mixing and Inversion Control */
36 { 4, 0x81 }, /* r04 - Mute Control */
37 { 5, 0x00 }, /* r05 - Channel A Volume Control */
38 { 6, 0x00 }, /* r06 - Channel B Volume Control */
39 { 7, 0xB1 }, /* r07 - Ramp and Filter Control */
40 { 8, 0x1C }, /* r08 - Misc. Control */
41};
42
43/* Private data for the CS4349 */
44struct cs4349_private {
45 struct regmap *regmap;
Tim Howee40da862015-07-16 14:51:40 -050046 struct gpio_desc *reset_gpio;
47 unsigned int mode;
48 int rate;
49};
50
51static bool cs4349_readable_register(struct device *dev, unsigned int reg)
52{
53 switch (reg) {
Axel Lin0443de72015-07-19 09:14:23 +080054 case CS4349_CHIPID ... CS4349_MISC:
55 return true;
56 default:
57 return false;
58 }
59}
60
61static bool cs4349_writeable_register(struct device *dev, unsigned int reg)
62{
63 switch (reg) {
64 case CS4349_MODE ... CS4349_MISC:
Tim Howee40da862015-07-16 14:51:40 -050065 return true;
66 default:
67 return false;
68 }
69}
70
71static int cs4349_set_dai_fmt(struct snd_soc_dai *codec_dai,
72 unsigned int format)
73{
Kuninori Morimoto0ec65b62018-01-29 03:53:32 +000074 struct snd_soc_component *component = codec_dai->component;
75 struct cs4349_private *cs4349 = snd_soc_component_get_drvdata(component);
Tim Howee40da862015-07-16 14:51:40 -050076 unsigned int fmt;
77
78 fmt = format & SND_SOC_DAIFMT_FORMAT_MASK;
79
80 switch (fmt) {
81 case SND_SOC_DAIFMT_I2S:
82 case SND_SOC_DAIFMT_LEFT_J:
83 case SND_SOC_DAIFMT_RIGHT_J:
84 cs4349->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
85 break;
86 default:
87 return -EINVAL;
88 }
89
90 return 0;
91}
92
93static int cs4349_pcm_hw_params(struct snd_pcm_substream *substream,
94 struct snd_pcm_hw_params *params,
95 struct snd_soc_dai *dai)
96{
Kuninori Morimoto0ec65b62018-01-29 03:53:32 +000097 struct snd_soc_component *component = dai->component;
98 struct cs4349_private *cs4349 = snd_soc_component_get_drvdata(component);
Axel Linda304ac2015-07-19 22:42:49 +080099 int fmt, ret;
Tim Howee40da862015-07-16 14:51:40 -0500100
Tim Howee40da862015-07-16 14:51:40 -0500101 cs4349->rate = params_rate(params);
102
103 switch (cs4349->mode) {
104 case SND_SOC_DAIFMT_I2S:
Axel Linda304ac2015-07-19 22:42:49 +0800105 fmt = DIF_I2S;
Tim Howee40da862015-07-16 14:51:40 -0500106 break;
107 case SND_SOC_DAIFMT_LEFT_J:
Axel Linda304ac2015-07-19 22:42:49 +0800108 fmt = DIF_LEFT_JST;
Tim Howee40da862015-07-16 14:51:40 -0500109 break;
110 case SND_SOC_DAIFMT_RIGHT_J:
111 switch (params_width(params)) {
112 case 16:
113 fmt = DIF_RGHT_JST16;
114 break;
115 case 24:
116 fmt = DIF_RGHT_JST24;
117 break;
118 default:
119 return -EINVAL;
120 }
Tim Howee40da862015-07-16 14:51:40 -0500121 break;
122 default:
123 return -EINVAL;
124 }
125
Kuninori Morimoto0ec65b62018-01-29 03:53:32 +0000126 ret = snd_soc_component_update_bits(component, CS4349_MODE, DIF_MASK,
Axel Linda304ac2015-07-19 22:42:49 +0800127 MODE_FORMAT(fmt));
Tim Howee40da862015-07-16 14:51:40 -0500128 if (ret < 0)
129 return ret;
130
131 return 0;
132}
133
134static int cs4349_digital_mute(struct snd_soc_dai *dai, int mute)
135{
Kuninori Morimoto0ec65b62018-01-29 03:53:32 +0000136 struct snd_soc_component *component = dai->component;
Tim Howee40da862015-07-16 14:51:40 -0500137 int reg;
138
139 reg = 0;
140 if (mute)
141 reg = MUTE_AB_MASK;
142
Kuninori Morimoto0ec65b62018-01-29 03:53:32 +0000143 return snd_soc_component_update_bits(component, CS4349_MUTE, MUTE_AB_MASK, reg);
Tim Howee40da862015-07-16 14:51:40 -0500144}
145
146static DECLARE_TLV_DB_SCALE(dig_tlv, -12750, 50, 0);
147
148static const char * const chan_mix_texts[] = {
149 "Mute", "MuteA", "MuteA SwapB", "MuteA MonoB", "SwapA MuteB",
150 "BothR", "Swap", "SwapA MonoB", "MuteB", "Normal", "BothL",
151 "MonoB", "MonoA MuteB", "MonoA", "MonoA SwapB", "Mono",
152 /*Normal == Channel A = Left, Channel B = Right*/
153};
154
155static const char * const fm_texts[] = {
156 "Auto", "Single", "Double", "Quad",
157};
158
159static const char * const deemph_texts[] = {
160 "None", "44.1k", "48k", "32k",
161};
162
163static const char * const softr_zeroc_texts[] = {
164 "Immediate", "Zero Cross", "Soft Ramp", "SR on ZC",
165};
166
167static int deemph_values[] = {
168 0, 4, 8, 12,
169};
170
171static int softr_zeroc_values[] = {
172 0, 64, 128, 192,
173};
174
175static const struct soc_enum chan_mix_enum =
176 SOC_ENUM_SINGLE(CS4349_VMI, 0,
177 ARRAY_SIZE(chan_mix_texts),
178 chan_mix_texts);
179
180static const struct soc_enum fm_mode_enum =
181 SOC_ENUM_SINGLE(CS4349_MODE, 0,
182 ARRAY_SIZE(fm_texts),
183 fm_texts);
184
185static SOC_VALUE_ENUM_SINGLE_DECL(deemph_enum, CS4349_MODE, 0, DEM_MASK,
186 deemph_texts, deemph_values);
187
188static SOC_VALUE_ENUM_SINGLE_DECL(softr_zeroc_enum, CS4349_RMPFLT, 0,
189 SR_ZC_MASK, softr_zeroc_texts,
190 softr_zeroc_values);
191
192static const struct snd_kcontrol_new cs4349_snd_controls[] = {
193 SOC_DOUBLE_R_TLV("Master Playback Volume",
194 CS4349_VOLA, CS4349_VOLB, 0, 0xFF, 1, dig_tlv),
195 SOC_ENUM("Functional Mode", fm_mode_enum),
196 SOC_ENUM("De-Emphasis Control", deemph_enum),
197 SOC_ENUM("Soft Ramp Zero Cross Control", softr_zeroc_enum),
198 SOC_ENUM("Channel Mixer", chan_mix_enum),
199 SOC_SINGLE("VolA = VolB Switch", CS4349_VMI, 7, 1, 0),
200 SOC_SINGLE("InvertA Switch", CS4349_VMI, 6, 1, 0),
201 SOC_SINGLE("InvertB Switch", CS4349_VMI, 5, 1, 0),
202 SOC_SINGLE("Auto-Mute Switch", CS4349_MUTE, 7, 1, 0),
203 SOC_SINGLE("MUTEC A = B Switch", CS4349_MUTE, 5, 1, 0),
204 SOC_SINGLE("Soft Ramp Up Switch", CS4349_RMPFLT, 5, 1, 0),
205 SOC_SINGLE("Soft Ramp Down Switch", CS4349_RMPFLT, 4, 1, 0),
206 SOC_SINGLE("Slow Roll Off Filter Switch", CS4349_RMPFLT, 2, 1, 0),
207 SOC_SINGLE("Freeze Switch", CS4349_MISC, 5, 1, 0),
208 SOC_SINGLE("Popguard Switch", CS4349_MISC, 4, 1, 0),
209};
210
211static const struct snd_soc_dapm_widget cs4349_dapm_widgets[] = {
212 SND_SOC_DAPM_DAC("HiFi DAC", NULL, SND_SOC_NOPM, 0, 0),
213
214 SND_SOC_DAPM_OUTPUT("OutputA"),
215 SND_SOC_DAPM_OUTPUT("OutputB"),
216};
217
218static const struct snd_soc_dapm_route cs4349_routes[] = {
219 {"DAC Playback", NULL, "OutputA"},
220 {"DAC Playback", NULL, "OutputB"},
221
222 {"OutputA", NULL, "HiFi DAC"},
223 {"OutputB", NULL, "HiFi DAC"},
224};
225
226#define CS4349_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
227 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
228 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
229 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
230 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
231 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | \
232 SNDRV_PCM_FMTBIT_S32_LE)
233
234#define CS4349_PCM_RATES SNDRV_PCM_RATE_8000_192000
235
236static const struct snd_soc_dai_ops cs4349_dai_ops = {
237 .hw_params = cs4349_pcm_hw_params,
238 .set_fmt = cs4349_set_dai_fmt,
239 .digital_mute = cs4349_digital_mute,
240};
241
242static struct snd_soc_dai_driver cs4349_dai = {
243 .name = "cs4349_hifi",
244 .playback = {
245 .stream_name = "DAC Playback",
246 .channels_min = 1,
247 .channels_max = 2,
248 .rates = CS4349_PCM_RATES,
249 .formats = CS4349_PCM_FORMATS,
250 },
251 .ops = &cs4349_dai_ops,
252 .symmetric_rates = 1,
253};
254
Kuninori Morimoto0ec65b62018-01-29 03:53:32 +0000255static const struct snd_soc_component_driver soc_component_dev_cs4349 = {
256 .controls = cs4349_snd_controls,
257 .num_controls = ARRAY_SIZE(cs4349_snd_controls),
258 .dapm_widgets = cs4349_dapm_widgets,
259 .num_dapm_widgets = ARRAY_SIZE(cs4349_dapm_widgets),
260 .dapm_routes = cs4349_routes,
261 .num_dapm_routes = ARRAY_SIZE(cs4349_routes),
262 .idle_bias_on = 1,
263 .use_pmdown_time = 1,
264 .endianness = 1,
265 .non_legacy_dai_naming = 1,
Tim Howee40da862015-07-16 14:51:40 -0500266};
267
Axel Linb08b3382015-07-17 23:43:02 +0800268static const struct regmap_config cs4349_regmap = {
Tim Howee40da862015-07-16 14:51:40 -0500269 .reg_bits = 8,
270 .val_bits = 8,
271
Axel Lindd9283e2015-07-17 23:38:34 +0800272 .max_register = CS4349_MISC,
Tim Howee40da862015-07-16 14:51:40 -0500273 .reg_defaults = cs4349_reg_defaults,
274 .num_reg_defaults = ARRAY_SIZE(cs4349_reg_defaults),
275 .readable_reg = cs4349_readable_register,
Axel Lin0443de72015-07-19 09:14:23 +0800276 .writeable_reg = cs4349_writeable_register,
Tim Howee40da862015-07-16 14:51:40 -0500277 .cache_type = REGCACHE_RBTREE,
278};
279
280static int cs4349_i2c_probe(struct i2c_client *client,
281 const struct i2c_device_id *id)
282{
283 struct cs4349_private *cs4349;
Axel Lin44251552015-07-19 09:15:38 +0800284 int ret;
Tim Howee40da862015-07-16 14:51:40 -0500285
286 cs4349 = devm_kzalloc(&client->dev, sizeof(*cs4349), GFP_KERNEL);
287 if (!cs4349)
288 return -ENOMEM;
289
290 cs4349->regmap = devm_regmap_init_i2c(client, &cs4349_regmap);
291 if (IS_ERR(cs4349->regmap)) {
292 ret = PTR_ERR(cs4349->regmap);
293 dev_err(&client->dev, "regmap_init() failed: %d\n", ret);
294 return ret;
295 }
296
Tim Howee40da862015-07-16 14:51:40 -0500297 /* Reset the Device */
298 cs4349->reset_gpio = devm_gpiod_get_optional(&client->dev,
299 "reset", GPIOD_OUT_LOW);
300 if (IS_ERR(cs4349->reset_gpio))
301 return PTR_ERR(cs4349->reset_gpio);
302
Axel Lin6a75c0b62015-07-22 09:59:47 +0800303 gpiod_set_value_cansleep(cs4349->reset_gpio, 1);
Tim Howee40da862015-07-16 14:51:40 -0500304
305 i2c_set_clientdata(client, cs4349);
306
Kuninori Morimoto0ec65b62018-01-29 03:53:32 +0000307 return devm_snd_soc_register_component(&client->dev,
308 &soc_component_dev_cs4349,
Tim Howee40da862015-07-16 14:51:40 -0500309 &cs4349_dai, 1);
310}
311
312static int cs4349_i2c_remove(struct i2c_client *client)
313{
314 struct cs4349_private *cs4349 = i2c_get_clientdata(client);
315
Tim Howee40da862015-07-16 14:51:40 -0500316 /* Hold down reset */
Axel Lin6a75c0b62015-07-22 09:59:47 +0800317 gpiod_set_value_cansleep(cs4349->reset_gpio, 0);
Tim Howee40da862015-07-16 14:51:40 -0500318
319 return 0;
320}
321
322#ifdef CONFIG_PM
323static int cs4349_runtime_suspend(struct device *dev)
324{
325 struct cs4349_private *cs4349 = dev_get_drvdata(dev);
Tim Howee40da862015-07-16 14:51:40 -0500326 int ret;
327
Axel Linec126932015-07-22 10:22:33 +0800328 ret = regmap_update_bits(cs4349->regmap, CS4349_MISC, PWR_DWN, PWR_DWN);
Tim Howee40da862015-07-16 14:51:40 -0500329 if (ret < 0)
330 return ret;
331
332 regcache_cache_only(cs4349->regmap, true);
333
334 /* Hold down reset */
Axel Lin6a75c0b62015-07-22 09:59:47 +0800335 gpiod_set_value_cansleep(cs4349->reset_gpio, 0);
Tim Howee40da862015-07-16 14:51:40 -0500336
337 return 0;
338}
339
340static int cs4349_runtime_resume(struct device *dev)
341{
342 struct cs4349_private *cs4349 = dev_get_drvdata(dev);
Tim Howee40da862015-07-16 14:51:40 -0500343 int ret;
344
Lars-Peter Clausencb2510d2015-07-19 12:15:17 +0200345 ret = regmap_update_bits(cs4349->regmap, CS4349_MISC, PWR_DWN, 0);
Tim Howee40da862015-07-16 14:51:40 -0500346 if (ret < 0)
347 return ret;
348
Axel Lin6a75c0b62015-07-22 09:59:47 +0800349 gpiod_set_value_cansleep(cs4349->reset_gpio, 1);
Tim Howee40da862015-07-16 14:51:40 -0500350
351 regcache_cache_only(cs4349->regmap, false);
352 regcache_sync(cs4349->regmap);
353
354 return 0;
355}
356#endif
357
358static const struct dev_pm_ops cs4349_runtime_pm = {
359 SET_RUNTIME_PM_OPS(cs4349_runtime_suspend, cs4349_runtime_resume,
360 NULL)
361};
362
363static const struct of_device_id cs4349_of_match[] = {
364 { .compatible = "cirrus,cs4349", },
365 {},
366};
367
368MODULE_DEVICE_TABLE(of, cs4349_of_match);
369
370static const struct i2c_device_id cs4349_i2c_id[] = {
371 {"cs4349", 0},
372 {}
373};
374
375MODULE_DEVICE_TABLE(i2c, cs4349_i2c_id);
376
377static struct i2c_driver cs4349_i2c_driver = {
378 .driver = {
379 .name = "cs4349",
Tim Howee40da862015-07-16 14:51:40 -0500380 .of_match_table = cs4349_of_match,
381 },
382 .id_table = cs4349_i2c_id,
383 .probe = cs4349_i2c_probe,
384 .remove = cs4349_i2c_remove,
385};
386
387module_i2c_driver(cs4349_i2c_driver);
388
389MODULE_AUTHOR("Tim Howe <tim.howe@cirrus.com>");
390MODULE_DESCRIPTION("Cirrus Logic CS4349 ALSA SoC Codec Driver");
391MODULE_LICENSE("GPL");