1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/slab.h>
26#include <sound/core.h>
27#include <sound/soc.h>
28#include <sound/initval.h>
29#include <linux/i2c.h>
30#include <linux/delay.h>
31#include <linux/regulator/consumer.h>
32#include <linux/gpio/consumer.h>
33#include <linux/of_device.h>
34
35
36
37
38
39
40
41#define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
42 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
43 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
44 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
45 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
46 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
47
48
49#define CS4270_CHIPID 0x01
50#define CS4270_PWRCTL 0x02
51#define CS4270_MODE 0x03
52#define CS4270_FORMAT 0x04
53#define CS4270_TRANS 0x05
54#define CS4270_MUTE 0x06
55#define CS4270_VOLA 0x07
56#define CS4270_VOLB 0x08
57
58#define CS4270_FIRSTREG 0x01
59#define CS4270_LASTREG 0x08
60#define CS4270_NUMREGS (CS4270_LASTREG - CS4270_FIRSTREG + 1)
61#define CS4270_I2C_INCR 0x80
62
63
64#define CS4270_CHIPID_ID 0xF0
65#define CS4270_CHIPID_REV 0x0F
66#define CS4270_PWRCTL_FREEZE 0x80
67#define CS4270_PWRCTL_PDN_ADC 0x20
68#define CS4270_PWRCTL_PDN_DAC 0x02
69#define CS4270_PWRCTL_PDN 0x01
70#define CS4270_PWRCTL_PDN_ALL \
71 (CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
72#define CS4270_MODE_SPEED_MASK 0x30
73#define CS4270_MODE_1X 0x00
74#define CS4270_MODE_2X 0x10
75#define CS4270_MODE_4X 0x20
76#define CS4270_MODE_SLAVE 0x30
77#define CS4270_MODE_DIV_MASK 0x0E
78#define CS4270_MODE_DIV1 0x00
79#define CS4270_MODE_DIV15 0x02
80#define CS4270_MODE_DIV2 0x04
81#define CS4270_MODE_DIV3 0x06
82#define CS4270_MODE_DIV4 0x08
83#define CS4270_MODE_POPGUARD 0x01
84#define CS4270_FORMAT_FREEZE_A 0x80
85#define CS4270_FORMAT_FREEZE_B 0x40
86#define CS4270_FORMAT_LOOPBACK 0x20
87#define CS4270_FORMAT_DAC_MASK 0x18
88#define CS4270_FORMAT_DAC_LJ 0x00
89#define CS4270_FORMAT_DAC_I2S 0x08
90#define CS4270_FORMAT_DAC_RJ16 0x18
91#define CS4270_FORMAT_DAC_RJ24 0x10
92#define CS4270_FORMAT_ADC_MASK 0x01
93#define CS4270_FORMAT_ADC_LJ 0x00
94#define CS4270_FORMAT_ADC_I2S 0x01
95#define CS4270_TRANS_ONE_VOL 0x80
96#define CS4270_TRANS_SOFT 0x40
97#define CS4270_TRANS_ZERO 0x20
98#define CS4270_TRANS_INV_ADC_A 0x08
99#define CS4270_TRANS_INV_ADC_B 0x10
100#define CS4270_TRANS_INV_DAC_A 0x02
101#define CS4270_TRANS_INV_DAC_B 0x04
102#define CS4270_TRANS_DEEMPH 0x01
103#define CS4270_MUTE_AUTO 0x20
104#define CS4270_MUTE_ADC_A 0x08
105#define CS4270_MUTE_ADC_B 0x10
106#define CS4270_MUTE_POLARITY 0x04
107#define CS4270_MUTE_DAC_A 0x01
108#define CS4270_MUTE_DAC_B 0x02
109
110
111
112
113
114
115
116static const struct reg_default cs4270_reg_defaults[] = {
117 { 2, 0x00 },
118 { 3, 0x30 },
119 { 4, 0x00 },
120 { 5, 0x60 },
121 { 6, 0x20 },
122 { 7, 0x00 },
123 { 8, 0x00 },
124};
125
126static const char *supply_names[] = {
127 "va", "vd", "vlc"
128};
129
130
131struct cs4270_private {
132 struct regmap *regmap;
133 unsigned int mclk;
134 unsigned int mode;
135 unsigned int slave_mode;
136 unsigned int manual_mute;
137
138
139 struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
140
141
142 struct gpio_desc *reset_gpio;
143};
144
145static const struct snd_soc_dapm_widget cs4270_dapm_widgets[] = {
146SND_SOC_DAPM_INPUT("AINL"),
147SND_SOC_DAPM_INPUT("AINR"),
148
149SND_SOC_DAPM_OUTPUT("AOUTL"),
150SND_SOC_DAPM_OUTPUT("AOUTR"),
151};
152
153static const struct snd_soc_dapm_route cs4270_dapm_routes[] = {
154 { "Capture", NULL, "AINL" },
155 { "Capture", NULL, "AINR" },
156
157 { "AOUTL", NULL, "Playback" },
158 { "AOUTR", NULL, "Playback" },
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
193struct cs4270_mode_ratios {
194 unsigned int ratio;
195 u8 speed_mode;
196 u8 mclk;
197};
198
199static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
200 {64, CS4270_MODE_4X, CS4270_MODE_DIV1},
201#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
202 {96, CS4270_MODE_4X, CS4270_MODE_DIV15},
203#endif
204 {128, CS4270_MODE_2X, CS4270_MODE_DIV1},
205 {192, CS4270_MODE_4X, CS4270_MODE_DIV3},
206 {256, CS4270_MODE_1X, CS4270_MODE_DIV1},
207 {384, CS4270_MODE_2X, CS4270_MODE_DIV3},
208 {512, CS4270_MODE_1X, CS4270_MODE_DIV2},
209 {768, CS4270_MODE_1X, CS4270_MODE_DIV3},
210 {1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
211};
212
213
214#define NUM_MCLK_RATIOS ARRAY_SIZE(cs4270_mode_ratios)
215
216static bool cs4270_reg_is_readable(struct device *dev, unsigned int reg)
217{
218 return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG);
219}
220
221static bool cs4270_reg_is_volatile(struct device *dev, unsigned int reg)
222{
223
224 if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
225 return true;
226
227 return reg == CS4270_CHIPID;
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
257static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
258 int clk_id, unsigned int freq, int dir)
259{
260 struct snd_soc_component *component = codec_dai->component;
261 struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
262
263 cs4270->mclk = freq;
264 return 0;
265}
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
281 unsigned int format)
282{
283 struct snd_soc_component *component = codec_dai->component;
284 struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
285
286
287 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
288 case SND_SOC_DAIFMT_I2S:
289 case SND_SOC_DAIFMT_LEFT_J:
290 cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
291 break;
292 default:
293 dev_err(component->dev, "invalid dai format\n");
294 return -EINVAL;
295 }
296
297
298 switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
299 case SND_SOC_DAIFMT_CBS_CFS:
300 cs4270->slave_mode = 1;
301 break;
302 case SND_SOC_DAIFMT_CBM_CFM:
303 cs4270->slave_mode = 0;
304 break;
305 default:
306
307 dev_err(component->dev, "Unknown master/slave configuration\n");
308 return -EINVAL;
309 }
310
311 return 0;
312}
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328static int cs4270_hw_params(struct snd_pcm_substream *substream,
329 struct snd_pcm_hw_params *params,
330 struct snd_soc_dai *dai)
331{
332 struct snd_soc_component *component = dai->component;
333 struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
334 int ret;
335 unsigned int i;
336 unsigned int rate;
337 unsigned int ratio;
338 int reg;
339
340
341
342 rate = params_rate(params);
343 ratio = cs4270->mclk / rate;
344
345 for (i = 0; i < NUM_MCLK_RATIOS; i++) {
346 if (cs4270_mode_ratios[i].ratio == ratio)
347 break;
348 }
349
350 if (i == NUM_MCLK_RATIOS) {
351
352 dev_err(component->dev, "could not find matching ratio\n");
353 return -EINVAL;
354 }
355
356
357
358 reg = snd_soc_component_read(component, CS4270_MODE);
359 reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
360 reg |= cs4270_mode_ratios[i].mclk;
361
362 if (cs4270->slave_mode)
363 reg |= CS4270_MODE_SLAVE;
364 else
365 reg |= cs4270_mode_ratios[i].speed_mode;
366
367 ret = snd_soc_component_write(component, CS4270_MODE, reg);
368 if (ret < 0) {
369 dev_err(component->dev, "i2c write failed\n");
370 return ret;
371 }
372
373
374
375 reg = snd_soc_component_read(component, CS4270_FORMAT);
376 reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);
377
378 switch (cs4270->mode) {
379 case SND_SOC_DAIFMT_I2S:
380 reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
381 break;
382 case SND_SOC_DAIFMT_LEFT_J:
383 reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
384 break;
385 default:
386 dev_err(component->dev, "unknown dai format\n");
387 return -EINVAL;
388 }
389
390 ret = snd_soc_component_write(component, CS4270_FORMAT, reg);
391 if (ret < 0) {
392 dev_err(component->dev, "i2c write failed\n");
393 return ret;
394 }
395
396 return ret;
397}
398
399
400
401
402
403
404
405
406
407
408
409static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute, int direction)
410{
411 struct snd_soc_component *component = dai->component;
412 struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
413 int reg6;
414
415 reg6 = snd_soc_component_read(component, CS4270_MUTE);
416
417 if (mute)
418 reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
419 else {
420 reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
421 reg6 |= cs4270->manual_mute;
422 }
423
424 return snd_soc_component_write(component, CS4270_MUTE, reg6);
425}
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
442 struct snd_ctl_elem_value *ucontrol)
443{
444 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
445 struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
446 int left = !ucontrol->value.integer.value[0];
447 int right = !ucontrol->value.integer.value[1];
448
449 cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
450 (right ? CS4270_MUTE_DAC_B : 0);
451
452 return snd_soc_put_volsw(kcontrol, ucontrol);
453}
454
455
456static const struct snd_kcontrol_new cs4270_snd_controls[] = {
457 SOC_DOUBLE_R("Master Playback Volume",
458 CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
459 SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
460 SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
461 SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
462 SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
463 SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
464 SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
465 SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
466 SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
467 snd_soc_get_volsw, cs4270_soc_put_mute),
468};
469
470static const struct snd_soc_dai_ops cs4270_dai_ops = {
471 .hw_params = cs4270_hw_params,
472 .set_sysclk = cs4270_set_dai_sysclk,
473 .set_fmt = cs4270_set_dai_fmt,
474 .mute_stream = cs4270_dai_mute,
475 .no_capture_mute = 1,
476};
477
478static struct snd_soc_dai_driver cs4270_dai = {
479 .name = "cs4270-hifi",
480 .playback = {
481 .stream_name = "Playback",
482 .channels_min = 2,
483 .channels_max = 2,
484 .rates = SNDRV_PCM_RATE_CONTINUOUS,
485 .rate_min = 4000,
486 .rate_max = 216000,
487 .formats = CS4270_FORMATS,
488 },
489 .capture = {
490 .stream_name = "Capture",
491 .channels_min = 2,
492 .channels_max = 2,
493 .rates = SNDRV_PCM_RATE_CONTINUOUS,
494 .rate_min = 4000,
495 .rate_max = 216000,
496 .formats = CS4270_FORMATS,
497 },
498 .ops = &cs4270_dai_ops,
499};
500
501
502
503
504
505
506
507
508static int cs4270_probe(struct snd_soc_component *component)
509{
510 struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
511 int ret;
512
513
514
515
516
517
518 ret = snd_soc_component_update_bits(component, CS4270_MUTE, CS4270_MUTE_AUTO, 0);
519 if (ret < 0) {
520 dev_err(component->dev, "i2c write failed\n");
521 return ret;
522 }
523
524
525
526
527
528
529 ret = snd_soc_component_update_bits(component, CS4270_TRANS,
530 CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0);
531 if (ret < 0) {
532 dev_err(component->dev, "i2c write failed\n");
533 return ret;
534 }
535
536 ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
537 cs4270->supplies);
538
539 return ret;
540}
541
542
543
544
545
546
547
548static void cs4270_remove(struct snd_soc_component *component)
549{
550 struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
551
552 regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
553};
554
555#ifdef CONFIG_PM
556
557
558
559
560
561
562
563
564
565
566static int cs4270_soc_suspend(struct snd_soc_component *component)
567{
568 struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
569 int reg, ret;
570
571 reg = snd_soc_component_read(component, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
572 if (reg < 0)
573 return reg;
574
575 ret = snd_soc_component_write(component, CS4270_PWRCTL, reg);
576 if (ret < 0)
577 return ret;
578
579 regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
580 cs4270->supplies);
581
582 return 0;
583}
584
585static int cs4270_soc_resume(struct snd_soc_component *component)
586{
587 struct cs4270_private *cs4270 = snd_soc_component_get_drvdata(component);
588 int reg, ret;
589
590 ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
591 cs4270->supplies);
592 if (ret != 0)
593 return ret;
594
595
596
597 ndelay(500);
598
599
600 regcache_sync(cs4270->regmap);
601
602
603 reg = snd_soc_component_read(component, CS4270_PWRCTL);
604 reg &= ~CS4270_PWRCTL_PDN_ALL;
605
606 return snd_soc_component_write(component, CS4270_PWRCTL, reg);
607}
608#else
609#define cs4270_soc_suspend NULL
610#define cs4270_soc_resume NULL
611#endif
612
613
614
615
616static const struct snd_soc_component_driver soc_component_device_cs4270 = {
617 .probe = cs4270_probe,
618 .remove = cs4270_remove,
619 .suspend = cs4270_soc_suspend,
620 .resume = cs4270_soc_resume,
621 .controls = cs4270_snd_controls,
622 .num_controls = ARRAY_SIZE(cs4270_snd_controls),
623 .dapm_widgets = cs4270_dapm_widgets,
624 .num_dapm_widgets = ARRAY_SIZE(cs4270_dapm_widgets),
625 .dapm_routes = cs4270_dapm_routes,
626 .num_dapm_routes = ARRAY_SIZE(cs4270_dapm_routes),
627 .idle_bias_on = 1,
628 .use_pmdown_time = 1,
629 .endianness = 1,
630 .non_legacy_dai_naming = 1,
631};
632
633
634
635
636static const struct of_device_id cs4270_of_match[] = {
637 { .compatible = "cirrus,cs4270", },
638 { }
639};
640MODULE_DEVICE_TABLE(of, cs4270_of_match);
641
642static const struct regmap_config cs4270_regmap = {
643 .reg_bits = 8,
644 .val_bits = 8,
645 .max_register = CS4270_LASTREG,
646 .reg_defaults = cs4270_reg_defaults,
647 .num_reg_defaults = ARRAY_SIZE(cs4270_reg_defaults),
648 .cache_type = REGCACHE_RBTREE,
649 .write_flag_mask = CS4270_I2C_INCR,
650
651 .readable_reg = cs4270_reg_is_readable,
652 .volatile_reg = cs4270_reg_is_volatile,
653};
654
655
656
657
658
659
660
661
662static int cs4270_i2c_remove(struct i2c_client *i2c_client)
663{
664 struct cs4270_private *cs4270 = i2c_get_clientdata(i2c_client);
665
666 gpiod_set_value_cansleep(cs4270->reset_gpio, 0);
667
668 return 0;
669}
670
671
672
673
674
675
676
677
678
679static int cs4270_i2c_probe(struct i2c_client *i2c_client,
680 const struct i2c_device_id *id)
681{
682 struct cs4270_private *cs4270;
683 unsigned int val;
684 int ret, i;
685
686 cs4270 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs4270_private),
687 GFP_KERNEL);
688 if (!cs4270)
689 return -ENOMEM;
690
691
692 for (i = 0; i < ARRAY_SIZE(supply_names); i++)
693 cs4270->supplies[i].supply = supply_names[i];
694
695 ret = devm_regulator_bulk_get(&i2c_client->dev,
696 ARRAY_SIZE(cs4270->supplies),
697 cs4270->supplies);
698 if (ret < 0)
699 return ret;
700
701
702 cs4270->reset_gpio = devm_gpiod_get_optional(&i2c_client->dev, "reset",
703 GPIOD_OUT_LOW);
704 if (IS_ERR(cs4270->reset_gpio)) {
705 dev_dbg(&i2c_client->dev, "Error getting CS4270 reset GPIO\n");
706 return PTR_ERR(cs4270->reset_gpio);
707 }
708
709 if (cs4270->reset_gpio) {
710 dev_dbg(&i2c_client->dev, "Found reset GPIO\n");
711 gpiod_set_value_cansleep(cs4270->reset_gpio, 1);
712 }
713
714
715 ndelay(500);
716
717 cs4270->regmap = devm_regmap_init_i2c(i2c_client, &cs4270_regmap);
718 if (IS_ERR(cs4270->regmap))
719 return PTR_ERR(cs4270->regmap);
720
721
722 ret = regmap_read(cs4270->regmap, CS4270_CHIPID, &val);
723 if (ret < 0) {
724 dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
725 i2c_client->addr);
726 return ret;
727 }
728
729 if ((val & 0xF0) != 0xC0) {
730 dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
731 i2c_client->addr);
732 return -ENODEV;
733 }
734
735 dev_info(&i2c_client->dev, "found device at i2c address %X\n",
736 i2c_client->addr);
737 dev_info(&i2c_client->dev, "hardware revision %X\n", val & 0xF);
738
739 i2c_set_clientdata(i2c_client, cs4270);
740
741 ret = devm_snd_soc_register_component(&i2c_client->dev,
742 &soc_component_device_cs4270, &cs4270_dai, 1);
743 return ret;
744}
745
746
747
748
749static const struct i2c_device_id cs4270_id[] = {
750 {"cs4270", 0},
751 {}
752};
753MODULE_DEVICE_TABLE(i2c, cs4270_id);
754
755
756
757
758
759
760
761static struct i2c_driver cs4270_i2c_driver = {
762 .driver = {
763 .name = "cs4270",
764 .of_match_table = cs4270_of_match,
765 },
766 .id_table = cs4270_id,
767 .probe = cs4270_i2c_probe,
768 .remove = cs4270_i2c_remove,
769};
770
771module_i2c_driver(cs4270_i2c_driver);
772
773MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
774MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
775MODULE_LICENSE("GPL");
776