GNU Linux-libre 4.19.286-gnu1
[releases.git] / sound / soc / codecs / es8328.c
1 /*
2  * es8328.c  --  ES8328 ALSA SoC Audio driver
3  *
4  * Copyright 2014 Sutajio Ko-Usagi PTE LTD
5  *
6  * Author: Sean Cross <xobs@kosagi.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/of_device.h>
16 #include <linux/module.h>
17 #include <linux/pm.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 #include <linux/regulator/consumer.h>
21 #include <sound/core.h>
22 #include <sound/initval.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/tlv.h>
27 #include "es8328.h"
28
29 static const unsigned int rates_12288[] = {
30         8000, 12000, 16000, 24000, 32000, 48000, 96000,
31 };
32
33 static const int ratios_12288[] = {
34         10, 7, 6, 4, 3, 2, 0,
35 };
36
37 static const struct snd_pcm_hw_constraint_list constraints_12288 = {
38         .count  = ARRAY_SIZE(rates_12288),
39         .list   = rates_12288,
40 };
41
42 static const unsigned int rates_11289[] = {
43         8018, 11025, 22050, 44100, 88200,
44 };
45
46 static const int ratios_11289[] = {
47         9, 7, 4, 2, 0,
48 };
49
50 static const struct snd_pcm_hw_constraint_list constraints_11289 = {
51         .count  = ARRAY_SIZE(rates_11289),
52         .list   = rates_11289,
53 };
54
55 /* regulator supplies for sgtl5000, VDDD is an optional external supply */
56 enum sgtl5000_regulator_supplies {
57         DVDD,
58         AVDD,
59         PVDD,
60         HPVDD,
61         ES8328_SUPPLY_NUM
62 };
63
64 /* vddd is optional supply */
65 static const char * const supply_names[ES8328_SUPPLY_NUM] = {
66         "DVDD",
67         "AVDD",
68         "PVDD",
69         "HPVDD",
70 };
71
72 #define ES8328_RATES (SNDRV_PCM_RATE_192000 | \
73                 SNDRV_PCM_RATE_96000 | \
74                 SNDRV_PCM_RATE_88200 | \
75                 SNDRV_PCM_RATE_8000_48000)
76 #define ES8328_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
77                 SNDRV_PCM_FMTBIT_S18_3LE | \
78                 SNDRV_PCM_FMTBIT_S20_3LE | \
79                 SNDRV_PCM_FMTBIT_S24_LE | \
80                 SNDRV_PCM_FMTBIT_S32_LE)
81
82 struct es8328_priv {
83         struct regmap *regmap;
84         struct clk *clk;
85         int playback_fs;
86         bool deemph;
87         int mclkdiv2;
88         const struct snd_pcm_hw_constraint_list *sysclk_constraints;
89         const int *mclk_ratios;
90         bool master;
91         struct regulator_bulk_data supplies[ES8328_SUPPLY_NUM];
92 };
93
94 /*
95  * ES8328 Controls
96  */
97
98 static const char * const adcpol_txt[] = {"Normal", "L Invert", "R Invert",
99                                           "L + R Invert"};
100 static SOC_ENUM_SINGLE_DECL(adcpol,
101                             ES8328_ADCCONTROL6, 6, adcpol_txt);
102
103 static const DECLARE_TLV_DB_SCALE(play_tlv, -3000, 100, 0);
104 static const DECLARE_TLV_DB_SCALE(dac_adc_tlv, -9600, 50, 0);
105 static const DECLARE_TLV_DB_SCALE(pga_tlv, 0, 300, 0);
106 static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
107 static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 300, 0);
108
109 static const struct {
110         int rate;
111         unsigned int val;
112 } deemph_settings[] = {
113         { 0,     ES8328_DACCONTROL6_DEEMPH_OFF },
114         { 32000, ES8328_DACCONTROL6_DEEMPH_32k },
115         { 44100, ES8328_DACCONTROL6_DEEMPH_44_1k },
116         { 48000, ES8328_DACCONTROL6_DEEMPH_48k },
117 };
118
119 static int es8328_set_deemph(struct snd_soc_component *component)
120 {
121         struct es8328_priv *es8328 = snd_soc_component_get_drvdata(component);
122         int val, i, best;
123
124         /*
125          * If we're using deemphasis select the nearest available sample
126          * rate.
127          */
128         if (es8328->deemph) {
129                 best = 0;
130                 for (i = 1; i < ARRAY_SIZE(deemph_settings); i++) {
131                         if (abs(deemph_settings[i].rate - es8328->playback_fs) <
132                             abs(deemph_settings[best].rate - es8328->playback_fs))
133                                 best = i;
134                 }
135
136                 val = deemph_settings[best].val;
137         } else {
138                 val = ES8328_DACCONTROL6_DEEMPH_OFF;
139         }
140
141         dev_dbg(component->dev, "Set deemphasis %d\n", val);
142
143         return snd_soc_component_update_bits(component, ES8328_DACCONTROL6,
144                         ES8328_DACCONTROL6_DEEMPH_MASK, val);
145 }
146
147 static int es8328_get_deemph(struct snd_kcontrol *kcontrol,
148                              struct snd_ctl_elem_value *ucontrol)
149 {
150         struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
151         struct es8328_priv *es8328 = snd_soc_component_get_drvdata(component);
152
153         ucontrol->value.integer.value[0] = es8328->deemph;
154         return 0;
155 }
156
157 static int es8328_put_deemph(struct snd_kcontrol *kcontrol,
158                              struct snd_ctl_elem_value *ucontrol)
159 {
160         struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
161         struct es8328_priv *es8328 = snd_soc_component_get_drvdata(component);
162         unsigned int deemph = ucontrol->value.integer.value[0];
163         int ret;
164
165         if (deemph > 1)
166                 return -EINVAL;
167
168         if (es8328->deemph == deemph)
169                 return 0;
170
171         ret = es8328_set_deemph(component);
172         if (ret < 0)
173                 return ret;
174
175         es8328->deemph = deemph;
176
177         return 1;
178 }
179
180
181
182 static const struct snd_kcontrol_new es8328_snd_controls[] = {
183         SOC_DOUBLE_R_TLV("Capture Digital Volume",
184                 ES8328_ADCCONTROL8, ES8328_ADCCONTROL9,
185                  0, 0xc0, 1, dac_adc_tlv),
186         SOC_SINGLE("Capture ZC Switch", ES8328_ADCCONTROL7, 6, 1, 0),
187
188         SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
189                     es8328_get_deemph, es8328_put_deemph),
190
191         SOC_ENUM("Capture Polarity", adcpol),
192
193         SOC_SINGLE_TLV("Left Mixer Left Bypass Volume",
194                         ES8328_DACCONTROL17, 3, 7, 1, bypass_tlv),
195         SOC_SINGLE_TLV("Left Mixer Right Bypass Volume",
196                         ES8328_DACCONTROL19, 3, 7, 1, bypass_tlv),
197         SOC_SINGLE_TLV("Right Mixer Left Bypass Volume",
198                         ES8328_DACCONTROL18, 3, 7, 1, bypass_tlv),
199         SOC_SINGLE_TLV("Right Mixer Right Bypass Volume",
200                         ES8328_DACCONTROL20, 3, 7, 1, bypass_tlv),
201
202         SOC_DOUBLE_R_TLV("PCM Volume",
203                         ES8328_LDACVOL, ES8328_RDACVOL,
204                         0, ES8328_DACVOL_MAX, 1, dac_adc_tlv),
205
206         SOC_DOUBLE_R_TLV("Output 1 Playback Volume",
207                         ES8328_LOUT1VOL, ES8328_ROUT1VOL,
208                         0, ES8328_OUT1VOL_MAX, 0, play_tlv),
209
210         SOC_DOUBLE_R_TLV("Output 2 Playback Volume",
211                         ES8328_LOUT2VOL, ES8328_ROUT2VOL,
212                         0, ES8328_OUT2VOL_MAX, 0, play_tlv),
213
214         SOC_DOUBLE_TLV("Mic PGA Volume", ES8328_ADCCONTROL1,
215                         4, 0, 8, 0, mic_tlv),
216 };
217
218 /*
219  * DAPM Controls
220  */
221
222 static const char * const es8328_line_texts[] = {
223         "Line 1", "Line 2", "PGA", "Differential"};
224
225 static const struct soc_enum es8328_lline_enum =
226         SOC_ENUM_SINGLE(ES8328_DACCONTROL16, 3,
227                               ARRAY_SIZE(es8328_line_texts),
228                               es8328_line_texts);
229 static const struct snd_kcontrol_new es8328_left_line_controls =
230         SOC_DAPM_ENUM("Route", es8328_lline_enum);
231
232 static const struct soc_enum es8328_rline_enum =
233         SOC_ENUM_SINGLE(ES8328_DACCONTROL16, 0,
234                               ARRAY_SIZE(es8328_line_texts),
235                               es8328_line_texts);
236 static const struct snd_kcontrol_new es8328_right_line_controls =
237         SOC_DAPM_ENUM("Route", es8328_rline_enum);
238
239 /* Left Mixer */
240 static const struct snd_kcontrol_new es8328_left_mixer_controls[] = {
241         SOC_DAPM_SINGLE("Playback Switch", ES8328_DACCONTROL17, 7, 1, 0),
242         SOC_DAPM_SINGLE("Left Bypass Switch", ES8328_DACCONTROL17, 6, 1, 0),
243         SOC_DAPM_SINGLE("Right Playback Switch", ES8328_DACCONTROL18, 7, 1, 0),
244         SOC_DAPM_SINGLE("Right Bypass Switch", ES8328_DACCONTROL18, 6, 1, 0),
245 };
246
247 /* Right Mixer */
248 static const struct snd_kcontrol_new es8328_right_mixer_controls[] = {
249         SOC_DAPM_SINGLE("Left Playback Switch", ES8328_DACCONTROL19, 7, 1, 0),
250         SOC_DAPM_SINGLE("Left Bypass Switch", ES8328_DACCONTROL19, 6, 1, 0),
251         SOC_DAPM_SINGLE("Playback Switch", ES8328_DACCONTROL20, 7, 1, 0),
252         SOC_DAPM_SINGLE("Right Bypass Switch", ES8328_DACCONTROL20, 6, 1, 0),
253 };
254
255 static const char * const es8328_pga_sel[] = {
256         "Line 1", "Line 2", "Line 3", "Differential"};
257
258 /* Left PGA Mux */
259 static const struct soc_enum es8328_lpga_enum =
260         SOC_ENUM_SINGLE(ES8328_ADCCONTROL2, 6,
261                               ARRAY_SIZE(es8328_pga_sel),
262                               es8328_pga_sel);
263 static const struct snd_kcontrol_new es8328_left_pga_controls =
264         SOC_DAPM_ENUM("Route", es8328_lpga_enum);
265
266 /* Right PGA Mux */
267 static const struct soc_enum es8328_rpga_enum =
268         SOC_ENUM_SINGLE(ES8328_ADCCONTROL2, 4,
269                               ARRAY_SIZE(es8328_pga_sel),
270                               es8328_pga_sel);
271 static const struct snd_kcontrol_new es8328_right_pga_controls =
272         SOC_DAPM_ENUM("Route", es8328_rpga_enum);
273
274 /* Differential Mux */
275 static const char * const es8328_diff_sel[] = {"Line 1", "Line 2"};
276 static SOC_ENUM_SINGLE_DECL(diffmux,
277                             ES8328_ADCCONTROL3, 7, es8328_diff_sel);
278 static const struct snd_kcontrol_new es8328_diffmux_controls =
279         SOC_DAPM_ENUM("Route", diffmux);
280
281 /* Mono ADC Mux */
282 static const char * const es8328_mono_mux[] = {"Stereo", "Mono (Left)",
283         "Mono (Right)", "Digital Mono"};
284 static SOC_ENUM_SINGLE_DECL(monomux,
285                             ES8328_ADCCONTROL3, 3, es8328_mono_mux);
286 static const struct snd_kcontrol_new es8328_monomux_controls =
287         SOC_DAPM_ENUM("Route", monomux);
288
289 static const struct snd_soc_dapm_widget es8328_dapm_widgets[] = {
290         SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
291                 &es8328_diffmux_controls),
292         SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
293                 &es8328_monomux_controls),
294         SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
295                 &es8328_monomux_controls),
296
297         SND_SOC_DAPM_MUX("Left PGA Mux", ES8328_ADCPOWER,
298                         ES8328_ADCPOWER_AINL_OFF, 1,
299                         &es8328_left_pga_controls),
300         SND_SOC_DAPM_MUX("Right PGA Mux", ES8328_ADCPOWER,
301                         ES8328_ADCPOWER_AINR_OFF, 1,
302                         &es8328_right_pga_controls),
303
304         SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
305                 &es8328_left_line_controls),
306         SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
307                 &es8328_right_line_controls),
308
309         SND_SOC_DAPM_ADC("Right ADC", "Right Capture", ES8328_ADCPOWER,
310                         ES8328_ADCPOWER_ADCR_OFF, 1),
311         SND_SOC_DAPM_ADC("Left ADC", "Left Capture", ES8328_ADCPOWER,
312                         ES8328_ADCPOWER_ADCL_OFF, 1),
313
314         SND_SOC_DAPM_SUPPLY("Mic Bias", ES8328_ADCPOWER,
315                         ES8328_ADCPOWER_MIC_BIAS_OFF, 1, NULL, 0),
316         SND_SOC_DAPM_SUPPLY("Mic Bias Gen", ES8328_ADCPOWER,
317                         ES8328_ADCPOWER_ADC_BIAS_GEN_OFF, 1, NULL, 0),
318
319         SND_SOC_DAPM_SUPPLY("DAC STM", ES8328_CHIPPOWER,
320                         ES8328_CHIPPOWER_DACSTM_RESET, 1, NULL, 0),
321         SND_SOC_DAPM_SUPPLY("ADC STM", ES8328_CHIPPOWER,
322                         ES8328_CHIPPOWER_ADCSTM_RESET, 1, NULL, 0),
323
324         SND_SOC_DAPM_SUPPLY("DAC DIG", ES8328_CHIPPOWER,
325                         ES8328_CHIPPOWER_DACDIG_OFF, 1, NULL, 0),
326         SND_SOC_DAPM_SUPPLY("ADC DIG", ES8328_CHIPPOWER,
327                         ES8328_CHIPPOWER_ADCDIG_OFF, 1, NULL, 0),
328
329         SND_SOC_DAPM_SUPPLY("DAC DLL", ES8328_CHIPPOWER,
330                         ES8328_CHIPPOWER_DACDLL_OFF, 1, NULL, 0),
331         SND_SOC_DAPM_SUPPLY("ADC DLL", ES8328_CHIPPOWER,
332                         ES8328_CHIPPOWER_ADCDLL_OFF, 1, NULL, 0),
333
334         SND_SOC_DAPM_SUPPLY("ADC Vref", ES8328_CHIPPOWER,
335                         ES8328_CHIPPOWER_ADCVREF_OFF, 1, NULL, 0),
336         SND_SOC_DAPM_SUPPLY("DAC Vref", ES8328_CHIPPOWER,
337                         ES8328_CHIPPOWER_DACVREF_OFF, 1, NULL, 0),
338
339         SND_SOC_DAPM_DAC("Right DAC", "Right Playback", ES8328_DACPOWER,
340                         ES8328_DACPOWER_RDAC_OFF, 1),
341         SND_SOC_DAPM_DAC("Left DAC", "Left Playback", ES8328_DACPOWER,
342                         ES8328_DACPOWER_LDAC_OFF, 1),
343
344         SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
345                 &es8328_left_mixer_controls[0],
346                 ARRAY_SIZE(es8328_left_mixer_controls)),
347         SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
348                 &es8328_right_mixer_controls[0],
349                 ARRAY_SIZE(es8328_right_mixer_controls)),
350
351         SND_SOC_DAPM_PGA("Right Out 2", ES8328_DACPOWER,
352                         ES8328_DACPOWER_ROUT2_ON, 0, NULL, 0),
353         SND_SOC_DAPM_PGA("Left Out 2", ES8328_DACPOWER,
354                         ES8328_DACPOWER_LOUT2_ON, 0, NULL, 0),
355         SND_SOC_DAPM_PGA("Right Out 1", ES8328_DACPOWER,
356                         ES8328_DACPOWER_ROUT1_ON, 0, NULL, 0),
357         SND_SOC_DAPM_PGA("Left Out 1", ES8328_DACPOWER,
358                         ES8328_DACPOWER_LOUT1_ON, 0, NULL, 0),
359
360         SND_SOC_DAPM_OUTPUT("LOUT1"),
361         SND_SOC_DAPM_OUTPUT("ROUT1"),
362         SND_SOC_DAPM_OUTPUT("LOUT2"),
363         SND_SOC_DAPM_OUTPUT("ROUT2"),
364
365         SND_SOC_DAPM_INPUT("LINPUT1"),
366         SND_SOC_DAPM_INPUT("LINPUT2"),
367         SND_SOC_DAPM_INPUT("RINPUT1"),
368         SND_SOC_DAPM_INPUT("RINPUT2"),
369 };
370
371 static const struct snd_soc_dapm_route es8328_dapm_routes[] = {
372
373         { "Left Line Mux", "Line 1", "LINPUT1" },
374         { "Left Line Mux", "Line 2", "LINPUT2" },
375         { "Left Line Mux", "PGA", "Left PGA Mux" },
376         { "Left Line Mux", "Differential", "Differential Mux" },
377
378         { "Right Line Mux", "Line 1", "RINPUT1" },
379         { "Right Line Mux", "Line 2", "RINPUT2" },
380         { "Right Line Mux", "PGA", "Right PGA Mux" },
381         { "Right Line Mux", "Differential", "Differential Mux" },
382
383         { "Left PGA Mux", "Line 1", "LINPUT1" },
384         { "Left PGA Mux", "Line 2", "LINPUT2" },
385         { "Left PGA Mux", "Differential", "Differential Mux" },
386
387         { "Right PGA Mux", "Line 1", "RINPUT1" },
388         { "Right PGA Mux", "Line 2", "RINPUT2" },
389         { "Right PGA Mux", "Differential", "Differential Mux" },
390
391         { "Differential Mux", "Line 1", "LINPUT1" },
392         { "Differential Mux", "Line 1", "RINPUT1" },
393         { "Differential Mux", "Line 2", "LINPUT2" },
394         { "Differential Mux", "Line 2", "RINPUT2" },
395
396         { "Left ADC Mux", "Stereo", "Left PGA Mux" },
397         { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
398         { "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
399
400         { "Right ADC Mux", "Stereo", "Right PGA Mux" },
401         { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
402         { "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
403
404         { "Left ADC", NULL, "Left ADC Mux" },
405         { "Right ADC", NULL, "Right ADC Mux" },
406
407         { "ADC DIG", NULL, "ADC STM" },
408         { "ADC DIG", NULL, "ADC Vref" },
409         { "ADC DIG", NULL, "ADC DLL" },
410
411         { "Left ADC", NULL, "ADC DIG" },
412         { "Right ADC", NULL, "ADC DIG" },
413
414         { "Mic Bias", NULL, "Mic Bias Gen" },
415
416         { "Left Line Mux", "Line 1", "LINPUT1" },
417         { "Left Line Mux", "Line 2", "LINPUT2" },
418         { "Left Line Mux", "PGA", "Left PGA Mux" },
419         { "Left Line Mux", "Differential", "Differential Mux" },
420
421         { "Right Line Mux", "Line 1", "RINPUT1" },
422         { "Right Line Mux", "Line 2", "RINPUT2" },
423         { "Right Line Mux", "PGA", "Right PGA Mux" },
424         { "Right Line Mux", "Differential", "Differential Mux" },
425
426         { "Left Out 1", NULL, "Left DAC" },
427         { "Right Out 1", NULL, "Right DAC" },
428         { "Left Out 2", NULL, "Left DAC" },
429         { "Right Out 2", NULL, "Right DAC" },
430
431         { "Left Mixer", "Playback Switch", "Left DAC" },
432         { "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
433         { "Left Mixer", "Right Playback Switch", "Right DAC" },
434         { "Left Mixer", "Right Bypass Switch", "Right Line Mux" },
435
436         { "Right Mixer", "Left Playback Switch", "Left DAC" },
437         { "Right Mixer", "Left Bypass Switch", "Left Line Mux" },
438         { "Right Mixer", "Playback Switch", "Right DAC" },
439         { "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
440
441         { "DAC DIG", NULL, "DAC STM" },
442         { "DAC DIG", NULL, "DAC Vref" },
443         { "DAC DIG", NULL, "DAC DLL" },
444
445         { "Left DAC", NULL, "DAC DIG" },
446         { "Right DAC", NULL, "DAC DIG" },
447
448         { "Left Out 1", NULL, "Left Mixer" },
449         { "LOUT1", NULL, "Left Out 1" },
450         { "Right Out 1", NULL, "Right Mixer" },
451         { "ROUT1", NULL, "Right Out 1" },
452
453         { "Left Out 2", NULL, "Left Mixer" },
454         { "LOUT2", NULL, "Left Out 2" },
455         { "Right Out 2", NULL, "Right Mixer" },
456         { "ROUT2", NULL, "Right Out 2" },
457 };
458
459 static int es8328_mute(struct snd_soc_dai *dai, int mute)
460 {
461         return snd_soc_component_update_bits(dai->component, ES8328_DACCONTROL3,
462                         ES8328_DACCONTROL3_DACMUTE,
463                         mute ? ES8328_DACCONTROL3_DACMUTE : 0);
464 }
465
466 static int es8328_startup(struct snd_pcm_substream *substream,
467                           struct snd_soc_dai *dai)
468 {
469         struct snd_soc_component *component = dai->component;
470         struct es8328_priv *es8328 = snd_soc_component_get_drvdata(component);
471
472         if (es8328->master && es8328->sysclk_constraints)
473                 snd_pcm_hw_constraint_list(substream->runtime, 0,
474                                 SNDRV_PCM_HW_PARAM_RATE,
475                                 es8328->sysclk_constraints);
476
477         return 0;
478 }
479
480 static int es8328_hw_params(struct snd_pcm_substream *substream,
481         struct snd_pcm_hw_params *params,
482         struct snd_soc_dai *dai)
483 {
484         struct snd_soc_component *component = dai->component;
485         struct es8328_priv *es8328 = snd_soc_component_get_drvdata(component);
486         int i;
487         int reg;
488         int wl;
489         int ratio;
490
491         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
492                 reg = ES8328_DACCONTROL2;
493         else
494                 reg = ES8328_ADCCONTROL5;
495
496         if (es8328->master) {
497                 if (!es8328->sysclk_constraints) {
498                         dev_err(component->dev, "No MCLK configured\n");
499                         return -EINVAL;
500                 }
501
502                 for (i = 0; i < es8328->sysclk_constraints->count; i++)
503                         if (es8328->sysclk_constraints->list[i] ==
504                             params_rate(params))
505                                 break;
506
507                 if (i == es8328->sysclk_constraints->count) {
508                         dev_err(component->dev,
509                                 "LRCLK %d unsupported with current clock\n",
510                                 params_rate(params));
511                         return -EINVAL;
512                 }
513                 ratio = es8328->mclk_ratios[i];
514         } else {
515                 ratio = 0;
516                 es8328->mclkdiv2 = 0;
517         }
518
519         snd_soc_component_update_bits(component, ES8328_MASTERMODE,
520                         ES8328_MASTERMODE_MCLKDIV2,
521                         es8328->mclkdiv2 ? ES8328_MASTERMODE_MCLKDIV2 : 0);
522
523         switch (params_width(params)) {
524         case 16:
525                 wl = 3;
526                 break;
527         case 18:
528                 wl = 2;
529                 break;
530         case 20:
531                 wl = 1;
532                 break;
533         case 24:
534                 wl = 0;
535                 break;
536         case 32:
537                 wl = 4;
538                 break;
539         default:
540                 return -EINVAL;
541         }
542
543         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
544                 snd_soc_component_update_bits(component, ES8328_DACCONTROL1,
545                                 ES8328_DACCONTROL1_DACWL_MASK,
546                                 wl << ES8328_DACCONTROL1_DACWL_SHIFT);
547
548                 es8328->playback_fs = params_rate(params);
549                 es8328_set_deemph(component);
550         } else
551                 snd_soc_component_update_bits(component, ES8328_ADCCONTROL4,
552                                 ES8328_ADCCONTROL4_ADCWL_MASK,
553                                 wl << ES8328_ADCCONTROL4_ADCWL_SHIFT);
554
555         return snd_soc_component_update_bits(component, reg, ES8328_RATEMASK, ratio);
556 }
557
558 static int es8328_set_sysclk(struct snd_soc_dai *codec_dai,
559                 int clk_id, unsigned int freq, int dir)
560 {
561         struct snd_soc_component *component = codec_dai->component;
562         struct es8328_priv *es8328 = snd_soc_component_get_drvdata(component);
563         int mclkdiv2 = 0;
564
565         switch (freq) {
566         case 0:
567                 es8328->sysclk_constraints = NULL;
568                 es8328->mclk_ratios = NULL;
569                 break;
570         case 22579200:
571                 mclkdiv2 = 1;
572                 /* fallthru */
573         case 11289600:
574                 es8328->sysclk_constraints = &constraints_11289;
575                 es8328->mclk_ratios = ratios_11289;
576                 break;
577         case 24576000:
578                 mclkdiv2 = 1;
579                 /* fallthru */
580         case 12288000:
581                 es8328->sysclk_constraints = &constraints_12288;
582                 es8328->mclk_ratios = ratios_12288;
583                 break;
584         default:
585                 return -EINVAL;
586         }
587
588         es8328->mclkdiv2 = mclkdiv2;
589         return 0;
590 }
591
592 static int es8328_set_dai_fmt(struct snd_soc_dai *codec_dai,
593                 unsigned int fmt)
594 {
595         struct snd_soc_component *component = codec_dai->component;
596         struct es8328_priv *es8328 = snd_soc_component_get_drvdata(component);
597         u8 dac_mode = 0;
598         u8 adc_mode = 0;
599
600         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
601         case SND_SOC_DAIFMT_CBM_CFM:
602                 /* Master serial port mode, with BCLK generated automatically */
603                 snd_soc_component_update_bits(component, ES8328_MASTERMODE,
604                                     ES8328_MASTERMODE_MSC,
605                                     ES8328_MASTERMODE_MSC);
606                 es8328->master = true;
607                 break;
608         case SND_SOC_DAIFMT_CBS_CFS:
609                 /* Slave serial port mode */
610                 snd_soc_component_update_bits(component, ES8328_MASTERMODE,
611                                     ES8328_MASTERMODE_MSC, 0);
612                 es8328->master = false;
613                 break;
614         default:
615                 return -EINVAL;
616         }
617
618         /* interface format */
619         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
620         case SND_SOC_DAIFMT_I2S:
621                 dac_mode |= ES8328_DACCONTROL1_DACFORMAT_I2S;
622                 adc_mode |= ES8328_ADCCONTROL4_ADCFORMAT_I2S;
623                 break;
624         case SND_SOC_DAIFMT_RIGHT_J:
625                 dac_mode |= ES8328_DACCONTROL1_DACFORMAT_RJUST;
626                 adc_mode |= ES8328_ADCCONTROL4_ADCFORMAT_RJUST;
627                 break;
628         case SND_SOC_DAIFMT_LEFT_J:
629                 dac_mode |= ES8328_DACCONTROL1_DACFORMAT_LJUST;
630                 adc_mode |= ES8328_ADCCONTROL4_ADCFORMAT_LJUST;
631                 break;
632         default:
633                 return -EINVAL;
634         }
635
636         /* clock inversion */
637         if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF)
638                 return -EINVAL;
639
640         snd_soc_component_update_bits(component, ES8328_DACCONTROL1,
641                         ES8328_DACCONTROL1_DACFORMAT_MASK, dac_mode);
642         snd_soc_component_update_bits(component, ES8328_ADCCONTROL4,
643                         ES8328_ADCCONTROL4_ADCFORMAT_MASK, adc_mode);
644
645         return 0;
646 }
647
648 static int es8328_set_bias_level(struct snd_soc_component *component,
649                                  enum snd_soc_bias_level level)
650 {
651         switch (level) {
652         case SND_SOC_BIAS_ON:
653                 break;
654
655         case SND_SOC_BIAS_PREPARE:
656                 /* VREF, VMID=2x50k, digital enabled */
657                 snd_soc_component_write(component, ES8328_CHIPPOWER, 0);
658                 snd_soc_component_update_bits(component, ES8328_CONTROL1,
659                                 ES8328_CONTROL1_VMIDSEL_MASK |
660                                 ES8328_CONTROL1_ENREF,
661                                 ES8328_CONTROL1_VMIDSEL_50k |
662                                 ES8328_CONTROL1_ENREF);
663                 break;
664
665         case SND_SOC_BIAS_STANDBY:
666                 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
667                         snd_soc_component_update_bits(component, ES8328_CONTROL1,
668                                         ES8328_CONTROL1_VMIDSEL_MASK |
669                                         ES8328_CONTROL1_ENREF,
670                                         ES8328_CONTROL1_VMIDSEL_5k |
671                                         ES8328_CONTROL1_ENREF);
672
673                         /* Charge caps */
674                         msleep(100);
675                 }
676
677                 snd_soc_component_write(component, ES8328_CONTROL2,
678                                 ES8328_CONTROL2_OVERCURRENT_ON |
679                                 ES8328_CONTROL2_THERMAL_SHUTDOWN_ON);
680
681                 /* VREF, VMID=2*500k, digital stopped */
682                 snd_soc_component_update_bits(component, ES8328_CONTROL1,
683                                 ES8328_CONTROL1_VMIDSEL_MASK |
684                                 ES8328_CONTROL1_ENREF,
685                                 ES8328_CONTROL1_VMIDSEL_500k |
686                                 ES8328_CONTROL1_ENREF);
687                 break;
688
689         case SND_SOC_BIAS_OFF:
690                 snd_soc_component_update_bits(component, ES8328_CONTROL1,
691                                 ES8328_CONTROL1_VMIDSEL_MASK |
692                                 ES8328_CONTROL1_ENREF,
693                                 0);
694                 break;
695         }
696         return 0;
697 }
698
699 static const struct snd_soc_dai_ops es8328_dai_ops = {
700         .startup        = es8328_startup,
701         .hw_params      = es8328_hw_params,
702         .digital_mute   = es8328_mute,
703         .set_sysclk     = es8328_set_sysclk,
704         .set_fmt        = es8328_set_dai_fmt,
705 };
706
707 static struct snd_soc_dai_driver es8328_dai = {
708         .name = "es8328-hifi-analog",
709         .playback = {
710                 .stream_name = "Playback",
711                 .channels_min = 2,
712                 .channels_max = 2,
713                 .rates = ES8328_RATES,
714                 .formats = ES8328_FORMATS,
715         },
716         .capture = {
717                 .stream_name = "Capture",
718                 .channels_min = 2,
719                 .channels_max = 2,
720                 .rates = ES8328_RATES,
721                 .formats = ES8328_FORMATS,
722         },
723         .ops = &es8328_dai_ops,
724         .symmetric_rates = 1,
725 };
726
727 static int es8328_suspend(struct snd_soc_component *component)
728 {
729         struct es8328_priv *es8328;
730         int ret;
731
732         es8328 = snd_soc_component_get_drvdata(component);
733
734         clk_disable_unprepare(es8328->clk);
735
736         ret = regulator_bulk_disable(ARRAY_SIZE(es8328->supplies),
737                         es8328->supplies);
738         if (ret) {
739                 dev_err(component->dev, "unable to disable regulators\n");
740                 return ret;
741         }
742         return 0;
743 }
744
745 static int es8328_resume(struct snd_soc_component *component)
746 {
747         struct regmap *regmap = dev_get_regmap(component->dev, NULL);
748         struct es8328_priv *es8328;
749         int ret;
750
751         es8328 = snd_soc_component_get_drvdata(component);
752
753         ret = clk_prepare_enable(es8328->clk);
754         if (ret) {
755                 dev_err(component->dev, "unable to enable clock\n");
756                 return ret;
757         }
758
759         ret = regulator_bulk_enable(ARRAY_SIZE(es8328->supplies),
760                                         es8328->supplies);
761         if (ret) {
762                 dev_err(component->dev, "unable to enable regulators\n");
763                 return ret;
764         }
765
766         regcache_mark_dirty(regmap);
767         ret = regcache_sync(regmap);
768         if (ret) {
769                 dev_err(component->dev, "unable to sync regcache\n");
770                 return ret;
771         }
772
773         return 0;
774 }
775
776 static int es8328_component_probe(struct snd_soc_component *component)
777 {
778         struct es8328_priv *es8328;
779         int ret;
780
781         es8328 = snd_soc_component_get_drvdata(component);
782
783         ret = regulator_bulk_enable(ARRAY_SIZE(es8328->supplies),
784                                         es8328->supplies);
785         if (ret) {
786                 dev_err(component->dev, "unable to enable regulators\n");
787                 return ret;
788         }
789
790         /* Setup clocks */
791         es8328->clk = devm_clk_get(component->dev, NULL);
792         if (IS_ERR(es8328->clk)) {
793                 dev_err(component->dev, "codec clock missing or invalid\n");
794                 ret = PTR_ERR(es8328->clk);
795                 goto clk_fail;
796         }
797
798         ret = clk_prepare_enable(es8328->clk);
799         if (ret) {
800                 dev_err(component->dev, "unable to prepare codec clk\n");
801                 goto clk_fail;
802         }
803
804         return 0;
805
806 clk_fail:
807         regulator_bulk_disable(ARRAY_SIZE(es8328->supplies),
808                                es8328->supplies);
809         return ret;
810 }
811
812 static void es8328_remove(struct snd_soc_component *component)
813 {
814         struct es8328_priv *es8328;
815
816         es8328 = snd_soc_component_get_drvdata(component);
817
818         if (es8328->clk)
819                 clk_disable_unprepare(es8328->clk);
820
821         regulator_bulk_disable(ARRAY_SIZE(es8328->supplies),
822                                es8328->supplies);
823 }
824
825 const struct regmap_config es8328_regmap_config = {
826         .reg_bits       = 8,
827         .val_bits       = 8,
828         .max_register   = ES8328_REG_MAX,
829         .cache_type     = REGCACHE_RBTREE,
830         .use_single_rw  = true,
831 };
832 EXPORT_SYMBOL_GPL(es8328_regmap_config);
833
834 static const struct snd_soc_component_driver es8328_component_driver = {
835         .probe                  = es8328_component_probe,
836         .remove                 = es8328_remove,
837         .suspend                = es8328_suspend,
838         .resume                 = es8328_resume,
839         .set_bias_level         = es8328_set_bias_level,
840         .controls               = es8328_snd_controls,
841         .num_controls           = ARRAY_SIZE(es8328_snd_controls),
842         .dapm_widgets           = es8328_dapm_widgets,
843         .num_dapm_widgets       = ARRAY_SIZE(es8328_dapm_widgets),
844         .dapm_routes            = es8328_dapm_routes,
845         .num_dapm_routes        = ARRAY_SIZE(es8328_dapm_routes),
846         .suspend_bias_off       = 1,
847         .idle_bias_on           = 1,
848         .use_pmdown_time        = 1,
849         .endianness             = 1,
850         .non_legacy_dai_naming  = 1,
851 };
852
853 int es8328_probe(struct device *dev, struct regmap *regmap)
854 {
855         struct es8328_priv *es8328;
856         int ret;
857         int i;
858
859         if (IS_ERR(regmap))
860                 return PTR_ERR(regmap);
861
862         es8328 = devm_kzalloc(dev, sizeof(*es8328), GFP_KERNEL);
863         if (es8328 == NULL)
864                 return -ENOMEM;
865
866         es8328->regmap = regmap;
867
868         for (i = 0; i < ARRAY_SIZE(es8328->supplies); i++)
869                 es8328->supplies[i].supply = supply_names[i];
870
871         ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(es8328->supplies),
872                                 es8328->supplies);
873         if (ret) {
874                 dev_err(dev, "unable to get regulators\n");
875                 return ret;
876         }
877
878         dev_set_drvdata(dev, es8328);
879
880         return devm_snd_soc_register_component(dev,
881                         &es8328_component_driver, &es8328_dai, 1);
882 }
883 EXPORT_SYMBOL_GPL(es8328_probe);
884
885 MODULE_DESCRIPTION("ASoC ES8328 driver");
886 MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
887 MODULE_LICENSE("GPL");