blob: e12764d15431be69046bd4db72b5a822986255df [file] [log] [blame]
Mark Brown5a3af122014-02-06 12:03:27 +00001/*
2 * Driver for the PCM512x CODECs
3 *
4 * Author: Mark Brown <broonie@linaro.org>
5 * Copyright 2014 Linaro Ltd
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
16
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/clk.h>
Javi Merinod48502e2015-04-16 12:43:57 -070021#include <linux/kernel.h>
Mark Brown5a3af122014-02-06 12:03:27 +000022#include <linux/pm_runtime.h>
23#include <linux/regmap.h>
24#include <linux/regulator/consumer.h>
Peter Rosinf086ba92015-01-28 15:16:10 +010025#include <linux/gcd.h>
Mark Brown5a3af122014-02-06 12:03:27 +000026#include <sound/soc.h>
27#include <sound/soc-dapm.h>
Peter Rosin81249302015-01-28 15:16:09 +010028#include <sound/pcm_params.h>
Mark Brown5a3af122014-02-06 12:03:27 +000029#include <sound/tlv.h>
30
31#include "pcm512x.h"
32
Peter Rosinf086ba92015-01-28 15:16:10 +010033#define DIV_ROUND_DOWN_ULL(ll, d) \
34 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
Peter Rosinf086ba92015-01-28 15:16:10 +010035
Mark Brown5a3af122014-02-06 12:03:27 +000036#define PCM512x_NUM_SUPPLIES 3
Mark Brown06d0ffc2014-02-06 14:33:52 +000037static const char * const pcm512x_supply_names[PCM512x_NUM_SUPPLIES] = {
Mark Brown5a3af122014-02-06 12:03:27 +000038 "AVDD",
39 "DVDD",
40 "CPVDD",
41};
42
43struct pcm512x_priv {
44 struct regmap *regmap;
45 struct clk *sclk;
46 struct regulator_bulk_data supplies[PCM512x_NUM_SUPPLIES];
47 struct notifier_block supply_nb[PCM512x_NUM_SUPPLIES];
Peter Rosin81249302015-01-28 15:16:09 +010048 int fmt;
Peter Rosinf086ba92015-01-28 15:16:10 +010049 int pll_in;
50 int pll_out;
51 int pll_r;
52 int pll_j;
53 int pll_d;
54 int pll_p;
55 unsigned long real_pll;
Peter Rosinf29933c2015-02-23 21:03:33 +010056 unsigned long overclock_pll;
57 unsigned long overclock_dac;
58 unsigned long overclock_dsp;
Mark Brown5a3af122014-02-06 12:03:27 +000059};
60
61/*
62 * We can't use the same notifier block for more than one supply and
63 * there's no way I can see to get from a callback to the caller
64 * except container_of().
65 */
66#define PCM512x_REGULATOR_EVENT(n) \
67static int pcm512x_regulator_event_##n(struct notifier_block *nb, \
68 unsigned long event, void *data) \
69{ \
70 struct pcm512x_priv *pcm512x = container_of(nb, struct pcm512x_priv, \
71 supply_nb[n]); \
72 if (event & REGULATOR_EVENT_DISABLE) { \
73 regcache_mark_dirty(pcm512x->regmap); \
74 regcache_cache_only(pcm512x->regmap, true); \
75 } \
76 return 0; \
77}
78
79PCM512x_REGULATOR_EVENT(0)
80PCM512x_REGULATOR_EVENT(1)
81PCM512x_REGULATOR_EVENT(2)
82
83static const struct reg_default pcm512x_reg_defaults[] = {
Mark Brown806d6462014-02-07 19:08:11 +000084 { PCM512x_RESET, 0x00 },
85 { PCM512x_POWER, 0x00 },
86 { PCM512x_MUTE, 0x00 },
87 { PCM512x_DSP, 0x00 },
88 { PCM512x_PLL_REF, 0x00 },
Peter Rosin81249302015-01-28 15:16:09 +010089 { PCM512x_DAC_REF, 0x00 },
Mark Brown806d6462014-02-07 19:08:11 +000090 { PCM512x_DAC_ROUTING, 0x11 },
91 { PCM512x_DSP_PROGRAM, 0x01 },
92 { PCM512x_CLKDET, 0x00 },
93 { PCM512x_AUTO_MUTE, 0x00 },
94 { PCM512x_ERROR_DETECT, 0x00 },
95 { PCM512x_DIGITAL_VOLUME_1, 0x00 },
96 { PCM512x_DIGITAL_VOLUME_2, 0x30 },
97 { PCM512x_DIGITAL_VOLUME_3, 0x30 },
98 { PCM512x_DIGITAL_MUTE_1, 0x22 },
99 { PCM512x_DIGITAL_MUTE_2, 0x00 },
100 { PCM512x_DIGITAL_MUTE_3, 0x07 },
101 { PCM512x_OUTPUT_AMPLITUDE, 0x00 },
102 { PCM512x_ANALOG_GAIN_CTRL, 0x00 },
103 { PCM512x_UNDERVOLTAGE_PROT, 0x00 },
104 { PCM512x_ANALOG_MUTE_CTRL, 0x00 },
105 { PCM512x_ANALOG_GAIN_BOOST, 0x00 },
106 { PCM512x_VCOM_CTRL_1, 0x00 },
107 { PCM512x_VCOM_CTRL_2, 0x01 },
Peter Rosin81249302015-01-28 15:16:09 +0100108 { PCM512x_BCLK_LRCLK_CFG, 0x00 },
109 { PCM512x_MASTER_MODE, 0x7c },
Peter Rosin7c4e1112015-01-28 15:16:11 +0100110 { PCM512x_GPIO_DACIN, 0x00 },
Peter Rosinf086ba92015-01-28 15:16:10 +0100111 { PCM512x_GPIO_PLLIN, 0x00 },
Peter Rosin81249302015-01-28 15:16:09 +0100112 { PCM512x_SYNCHRONIZE, 0x10 },
Peter Rosinf086ba92015-01-28 15:16:10 +0100113 { PCM512x_PLL_COEFF_0, 0x00 },
114 { PCM512x_PLL_COEFF_1, 0x00 },
115 { PCM512x_PLL_COEFF_2, 0x00 },
116 { PCM512x_PLL_COEFF_3, 0x00 },
117 { PCM512x_PLL_COEFF_4, 0x00 },
Peter Rosin81249302015-01-28 15:16:09 +0100118 { PCM512x_DSP_CLKDIV, 0x00 },
119 { PCM512x_DAC_CLKDIV, 0x00 },
120 { PCM512x_NCP_CLKDIV, 0x00 },
121 { PCM512x_OSR_CLKDIV, 0x00 },
122 { PCM512x_MASTER_CLKDIV_1, 0x00 },
123 { PCM512x_MASTER_CLKDIV_2, 0x00 },
124 { PCM512x_FS_SPEED_MODE, 0x00 },
125 { PCM512x_IDAC_1, 0x01 },
126 { PCM512x_IDAC_2, 0x00 },
Mark Brown5a3af122014-02-06 12:03:27 +0000127};
128
129static bool pcm512x_readable(struct device *dev, unsigned int reg)
130{
131 switch (reg) {
132 case PCM512x_RESET:
133 case PCM512x_POWER:
134 case PCM512x_MUTE:
135 case PCM512x_PLL_EN:
136 case PCM512x_SPI_MISO_FUNCTION:
137 case PCM512x_DSP:
138 case PCM512x_GPIO_EN:
139 case PCM512x_BCLK_LRCLK_CFG:
140 case PCM512x_DSP_GPIO_INPUT:
141 case PCM512x_MASTER_MODE:
142 case PCM512x_PLL_REF:
Peter Rosin81249302015-01-28 15:16:09 +0100143 case PCM512x_DAC_REF:
Peter Rosin7c4e1112015-01-28 15:16:11 +0100144 case PCM512x_GPIO_DACIN:
Peter Rosinf086ba92015-01-28 15:16:10 +0100145 case PCM512x_GPIO_PLLIN:
Peter Rosin81249302015-01-28 15:16:09 +0100146 case PCM512x_SYNCHRONIZE:
Mark Brown5a3af122014-02-06 12:03:27 +0000147 case PCM512x_PLL_COEFF_0:
148 case PCM512x_PLL_COEFF_1:
149 case PCM512x_PLL_COEFF_2:
150 case PCM512x_PLL_COEFF_3:
151 case PCM512x_PLL_COEFF_4:
152 case PCM512x_DSP_CLKDIV:
153 case PCM512x_DAC_CLKDIV:
154 case PCM512x_NCP_CLKDIV:
155 case PCM512x_OSR_CLKDIV:
156 case PCM512x_MASTER_CLKDIV_1:
157 case PCM512x_MASTER_CLKDIV_2:
158 case PCM512x_FS_SPEED_MODE:
159 case PCM512x_IDAC_1:
160 case PCM512x_IDAC_2:
161 case PCM512x_ERROR_DETECT:
162 case PCM512x_I2S_1:
163 case PCM512x_I2S_2:
164 case PCM512x_DAC_ROUTING:
165 case PCM512x_DSP_PROGRAM:
166 case PCM512x_CLKDET:
167 case PCM512x_AUTO_MUTE:
168 case PCM512x_DIGITAL_VOLUME_1:
169 case PCM512x_DIGITAL_VOLUME_2:
170 case PCM512x_DIGITAL_VOLUME_3:
171 case PCM512x_DIGITAL_MUTE_1:
172 case PCM512x_DIGITAL_MUTE_2:
173 case PCM512x_DIGITAL_MUTE_3:
174 case PCM512x_GPIO_OUTPUT_1:
175 case PCM512x_GPIO_OUTPUT_2:
176 case PCM512x_GPIO_OUTPUT_3:
177 case PCM512x_GPIO_OUTPUT_4:
178 case PCM512x_GPIO_OUTPUT_5:
179 case PCM512x_GPIO_OUTPUT_6:
180 case PCM512x_GPIO_CONTROL_1:
181 case PCM512x_GPIO_CONTROL_2:
182 case PCM512x_OVERFLOW:
183 case PCM512x_RATE_DET_1:
184 case PCM512x_RATE_DET_2:
185 case PCM512x_RATE_DET_3:
186 case PCM512x_RATE_DET_4:
Peter Rosinf086ba92015-01-28 15:16:10 +0100187 case PCM512x_CLOCK_STATUS:
Mark Brown5a3af122014-02-06 12:03:27 +0000188 case PCM512x_ANALOG_MUTE_DET:
189 case PCM512x_GPIN:
190 case PCM512x_DIGITAL_MUTE_DET:
Mark Brown806d6462014-02-07 19:08:11 +0000191 case PCM512x_OUTPUT_AMPLITUDE:
192 case PCM512x_ANALOG_GAIN_CTRL:
193 case PCM512x_UNDERVOLTAGE_PROT:
194 case PCM512x_ANALOG_MUTE_CTRL:
195 case PCM512x_ANALOG_GAIN_BOOST:
196 case PCM512x_VCOM_CTRL_1:
197 case PCM512x_VCOM_CTRL_2:
198 case PCM512x_CRAM_CTRL:
Peter Rosinf086ba92015-01-28 15:16:10 +0100199 case PCM512x_FLEX_A:
200 case PCM512x_FLEX_B:
Mark Brown5a3af122014-02-06 12:03:27 +0000201 return true;
202 default:
Mark Brown806d6462014-02-07 19:08:11 +0000203 /* There are 256 raw register addresses */
204 return reg < 0xff;
Mark Brown5a3af122014-02-06 12:03:27 +0000205 }
206}
207
208static bool pcm512x_volatile(struct device *dev, unsigned int reg)
209{
210 switch (reg) {
211 case PCM512x_PLL_EN:
212 case PCM512x_OVERFLOW:
213 case PCM512x_RATE_DET_1:
214 case PCM512x_RATE_DET_2:
215 case PCM512x_RATE_DET_3:
216 case PCM512x_RATE_DET_4:
Peter Rosinf086ba92015-01-28 15:16:10 +0100217 case PCM512x_CLOCK_STATUS:
Mark Brown5a3af122014-02-06 12:03:27 +0000218 case PCM512x_ANALOG_MUTE_DET:
219 case PCM512x_GPIN:
220 case PCM512x_DIGITAL_MUTE_DET:
Mark Brown806d6462014-02-07 19:08:11 +0000221 case PCM512x_CRAM_CTRL:
Mark Brown5a3af122014-02-06 12:03:27 +0000222 return true;
223 default:
Mark Brown806d6462014-02-07 19:08:11 +0000224 /* There are 256 raw register addresses */
225 return reg < 0xff;
Mark Brown5a3af122014-02-06 12:03:27 +0000226 }
227}
228
Peter Rosinf29933c2015-02-23 21:03:33 +0100229static int pcm512x_overclock_pll_get(struct snd_kcontrol *kcontrol,
230 struct snd_ctl_elem_value *ucontrol)
231{
232 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
233 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
234
235 ucontrol->value.integer.value[0] = pcm512x->overclock_pll;
236 return 0;
237}
238
239static int pcm512x_overclock_pll_put(struct snd_kcontrol *kcontrol,
240 struct snd_ctl_elem_value *ucontrol)
241{
242 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
243 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
244
245 switch (codec->dapm.bias_level) {
246 case SND_SOC_BIAS_OFF:
247 case SND_SOC_BIAS_STANDBY:
248 break;
249 default:
250 return -EBUSY;
251 }
252
253 pcm512x->overclock_pll = ucontrol->value.integer.value[0];
254 return 0;
255}
256
257static int pcm512x_overclock_dsp_get(struct snd_kcontrol *kcontrol,
258 struct snd_ctl_elem_value *ucontrol)
259{
260 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
261 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
262
263 ucontrol->value.integer.value[0] = pcm512x->overclock_dsp;
264 return 0;
265}
266
267static int pcm512x_overclock_dsp_put(struct snd_kcontrol *kcontrol,
268 struct snd_ctl_elem_value *ucontrol)
269{
270 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
271 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
272
273 switch (codec->dapm.bias_level) {
274 case SND_SOC_BIAS_OFF:
275 case SND_SOC_BIAS_STANDBY:
276 break;
277 default:
278 return -EBUSY;
279 }
280
281 pcm512x->overclock_dsp = ucontrol->value.integer.value[0];
282 return 0;
283}
284
285static int pcm512x_overclock_dac_get(struct snd_kcontrol *kcontrol,
286 struct snd_ctl_elem_value *ucontrol)
287{
288 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
289 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
290
291 ucontrol->value.integer.value[0] = pcm512x->overclock_dac;
292 return 0;
293}
294
295static int pcm512x_overclock_dac_put(struct snd_kcontrol *kcontrol,
296 struct snd_ctl_elem_value *ucontrol)
297{
298 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
299 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
300
301 switch (codec->dapm.bias_level) {
302 case SND_SOC_BIAS_OFF:
303 case SND_SOC_BIAS_STANDBY:
304 break;
305 default:
306 return -EBUSY;
307 }
308
309 pcm512x->overclock_dac = ucontrol->value.integer.value[0];
310 return 0;
311}
312
Mark Brown5a3af122014-02-06 12:03:27 +0000313static const DECLARE_TLV_DB_SCALE(digital_tlv, -10350, 50, 1);
Mark Brown5be2fc22014-02-07 19:16:56 +0000314static const DECLARE_TLV_DB_SCALE(analog_tlv, -600, 600, 0);
315static const DECLARE_TLV_DB_SCALE(boost_tlv, 0, 80, 0);
Mark Brown5a3af122014-02-06 12:03:27 +0000316
Mark Brown06d0ffc2014-02-06 14:33:52 +0000317static const char * const pcm512x_dsp_program_texts[] = {
Mark Brown5a3af122014-02-06 12:03:27 +0000318 "FIR interpolation with de-emphasis",
319 "Low latency IIR with de-emphasis",
Mark Brown5a3af122014-02-06 12:03:27 +0000320 "High attenuation with de-emphasis",
Peter Rosin3a8e5012015-01-08 22:56:30 +0100321 "Fixed process flow",
Mark Brown5a3af122014-02-06 12:03:27 +0000322 "Ringing-less low latency FIR",
323};
324
325static const unsigned int pcm512x_dsp_program_values[] = {
326 1,
327 2,
328 3,
329 5,
330 7,
331};
332
Mark Browne97db9a2014-03-07 11:43:04 +0800333static SOC_VALUE_ENUM_SINGLE_DECL(pcm512x_dsp_program,
334 PCM512x_DSP_PROGRAM, 0, 0x1f,
335 pcm512x_dsp_program_texts,
336 pcm512x_dsp_program_values);
Mark Brown5a3af122014-02-06 12:03:27 +0000337
Mark Brown06d0ffc2014-02-06 14:33:52 +0000338static const char * const pcm512x_clk_missing_text[] = {
Mark Brown5a3af122014-02-06 12:03:27 +0000339 "1s", "2s", "3s", "4s", "5s", "6s", "7s", "8s"
340};
341
342static const struct soc_enum pcm512x_clk_missing =
343 SOC_ENUM_SINGLE(PCM512x_CLKDET, 0, 8, pcm512x_clk_missing_text);
344
Mark Brown06d0ffc2014-02-06 14:33:52 +0000345static const char * const pcm512x_autom_text[] = {
Mark Brown5a3af122014-02-06 12:03:27 +0000346 "21ms", "106ms", "213ms", "533ms", "1.07s", "2.13s", "5.33s", "10.66s"
347};
348
349static const struct soc_enum pcm512x_autom_l =
350 SOC_ENUM_SINGLE(PCM512x_AUTO_MUTE, PCM512x_ATML_SHIFT, 8,
351 pcm512x_autom_text);
352
353static const struct soc_enum pcm512x_autom_r =
354 SOC_ENUM_SINGLE(PCM512x_AUTO_MUTE, PCM512x_ATMR_SHIFT, 8,
355 pcm512x_autom_text);
356
Mark Brown06d0ffc2014-02-06 14:33:52 +0000357static const char * const pcm512x_ramp_rate_text[] = {
Mark Brown5a3af122014-02-06 12:03:27 +0000358 "1 sample/update", "2 samples/update", "4 samples/update",
359 "Immediate"
360};
361
362static const struct soc_enum pcm512x_vndf =
363 SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNDF_SHIFT, 4,
364 pcm512x_ramp_rate_text);
365
366static const struct soc_enum pcm512x_vnuf =
367 SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNUF_SHIFT, 4,
368 pcm512x_ramp_rate_text);
369
370static const struct soc_enum pcm512x_vedf =
371 SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_2, PCM512x_VEDF_SHIFT, 4,
372 pcm512x_ramp_rate_text);
373
Mark Brown06d0ffc2014-02-06 14:33:52 +0000374static const char * const pcm512x_ramp_step_text[] = {
Mark Brown5a3af122014-02-06 12:03:27 +0000375 "4dB/step", "2dB/step", "1dB/step", "0.5dB/step"
376};
377
378static const struct soc_enum pcm512x_vnds =
379 SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNDS_SHIFT, 4,
380 pcm512x_ramp_step_text);
381
382static const struct soc_enum pcm512x_vnus =
383 SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNUS_SHIFT, 4,
384 pcm512x_ramp_step_text);
385
386static const struct soc_enum pcm512x_veds =
387 SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_2, PCM512x_VEDS_SHIFT, 4,
388 pcm512x_ramp_step_text);
389
390static const struct snd_kcontrol_new pcm512x_controls[] = {
Mark Brown1c6d3682014-08-08 16:04:01 +0100391SOC_DOUBLE_R_TLV("Digital Playback Volume", PCM512x_DIGITAL_VOLUME_2,
Mark Brown5a3af122014-02-06 12:03:27 +0000392 PCM512x_DIGITAL_VOLUME_3, 0, 255, 1, digital_tlv),
Howard Mitchell4d9b13c2015-03-19 12:08:30 +0000393SOC_DOUBLE_TLV("Analogue Playback Volume", PCM512x_ANALOG_GAIN_CTRL,
Mark Brown5be2fc22014-02-07 19:16:56 +0000394 PCM512x_LAGN_SHIFT, PCM512x_RAGN_SHIFT, 1, 1, analog_tlv),
Howard Mitchell4d9b13c2015-03-19 12:08:30 +0000395SOC_DOUBLE_TLV("Analogue Playback Boost Volume", PCM512x_ANALOG_GAIN_BOOST,
Mark Brown5be2fc22014-02-07 19:16:56 +0000396 PCM512x_AGBL_SHIFT, PCM512x_AGBR_SHIFT, 1, 0, boost_tlv),
Mark Brown1c6d3682014-08-08 16:04:01 +0100397SOC_DOUBLE("Digital Playback Switch", PCM512x_MUTE, PCM512x_RQML_SHIFT,
Mark Brown5a3af122014-02-06 12:03:27 +0000398 PCM512x_RQMR_SHIFT, 1, 1),
399
400SOC_SINGLE("Deemphasis Switch", PCM512x_DSP, PCM512x_DEMP_SHIFT, 1, 1),
Lars-Peter Clausen54581be2014-04-14 21:31:01 +0200401SOC_ENUM("DSP Program", pcm512x_dsp_program),
Mark Brown5a3af122014-02-06 12:03:27 +0000402
403SOC_ENUM("Clock Missing Period", pcm512x_clk_missing),
404SOC_ENUM("Auto Mute Time Left", pcm512x_autom_l),
405SOC_ENUM("Auto Mute Time Right", pcm512x_autom_r),
406SOC_SINGLE("Auto Mute Mono Switch", PCM512x_DIGITAL_MUTE_3,
407 PCM512x_ACTL_SHIFT, 1, 0),
408SOC_DOUBLE("Auto Mute Switch", PCM512x_DIGITAL_MUTE_3, PCM512x_AMLE_SHIFT,
Peter Rosin376dc492015-01-28 15:16:07 +0100409 PCM512x_AMRE_SHIFT, 1, 0),
Mark Brown5a3af122014-02-06 12:03:27 +0000410
411SOC_ENUM("Volume Ramp Down Rate", pcm512x_vndf),
412SOC_ENUM("Volume Ramp Down Step", pcm512x_vnds),
413SOC_ENUM("Volume Ramp Up Rate", pcm512x_vnuf),
414SOC_ENUM("Volume Ramp Up Step", pcm512x_vnus),
415SOC_ENUM("Volume Ramp Down Emergency Rate", pcm512x_vedf),
416SOC_ENUM("Volume Ramp Down Emergency Step", pcm512x_veds),
Peter Rosinf29933c2015-02-23 21:03:33 +0100417
418SOC_SINGLE_EXT("Max Overclock PLL", SND_SOC_NOPM, 0, 20, 0,
419 pcm512x_overclock_pll_get, pcm512x_overclock_pll_put),
420SOC_SINGLE_EXT("Max Overclock DSP", SND_SOC_NOPM, 0, 40, 0,
421 pcm512x_overclock_dsp_get, pcm512x_overclock_dsp_put),
422SOC_SINGLE_EXT("Max Overclock DAC", SND_SOC_NOPM, 0, 40, 0,
423 pcm512x_overclock_dac_get, pcm512x_overclock_dac_put),
Mark Brown5a3af122014-02-06 12:03:27 +0000424};
425
426static const struct snd_soc_dapm_widget pcm512x_dapm_widgets[] = {
427SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
428SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
429
430SND_SOC_DAPM_OUTPUT("OUTL"),
431SND_SOC_DAPM_OUTPUT("OUTR"),
432};
433
434static const struct snd_soc_dapm_route pcm512x_dapm_routes[] = {
435 { "DACL", NULL, "Playback" },
436 { "DACR", NULL, "Playback" },
437
438 { "OUTL", NULL, "DACL" },
439 { "OUTR", NULL, "DACR" },
440};
441
Peter Rosinf29933c2015-02-23 21:03:33 +0100442static unsigned long pcm512x_pll_max(struct pcm512x_priv *pcm512x)
443{
444 return 25000000 + 25000000 * pcm512x->overclock_pll / 100;
445}
446
447static unsigned long pcm512x_dsp_max(struct pcm512x_priv *pcm512x)
448{
449 return 50000000 + 50000000 * pcm512x->overclock_dsp / 100;
450}
451
452static unsigned long pcm512x_dac_max(struct pcm512x_priv *pcm512x,
453 unsigned long rate)
454{
455 return rate + rate * pcm512x->overclock_dac / 100;
456}
457
458static unsigned long pcm512x_sck_max(struct pcm512x_priv *pcm512x)
459{
460 if (!pcm512x->pll_out)
461 return 25000000;
462 return pcm512x_pll_max(pcm512x);
463}
464
465static unsigned long pcm512x_ncp_target(struct pcm512x_priv *pcm512x,
466 unsigned long dac_rate)
467{
468 /*
469 * If the DAC is not actually overclocked, use the good old
470 * NCP target rate...
471 */
472 if (dac_rate <= 6144000)
473 return 1536000;
474 /*
475 * ...but if the DAC is in fact overclocked, bump the NCP target
476 * rate to get the recommended dividers even when overclocking.
477 */
478 return pcm512x_dac_max(pcm512x, 1536000);
479}
480
Peter Rosin81249302015-01-28 15:16:09 +0100481static const u32 pcm512x_dai_rates[] = {
482 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
483 88200, 96000, 176400, 192000, 384000,
484};
485
486static const struct snd_pcm_hw_constraint_list constraints_slave = {
487 .count = ARRAY_SIZE(pcm512x_dai_rates),
488 .list = pcm512x_dai_rates,
489};
490
Peter Rosinf086ba92015-01-28 15:16:10 +0100491static int pcm512x_hw_rule_rate(struct snd_pcm_hw_params *params,
492 struct snd_pcm_hw_rule *rule)
493{
Peter Rosinf29933c2015-02-23 21:03:33 +0100494 struct pcm512x_priv *pcm512x = rule->private;
Peter Rosin9c7da1a2015-01-29 12:21:56 +0100495 struct snd_interval ranges[2];
Peter Rosinf086ba92015-01-28 15:16:10 +0100496 int frame_size;
497
498 frame_size = snd_soc_params_to_frame_size(params);
499 if (frame_size < 0)
500 return frame_size;
501
Peter Rosin9c7da1a2015-01-29 12:21:56 +0100502 switch (frame_size) {
503 case 32:
504 /* No hole when the frame size is 32. */
Peter Rosinf086ba92015-01-28 15:16:10 +0100505 return 0;
Peter Rosin9c7da1a2015-01-29 12:21:56 +0100506 case 48:
507 case 64:
508 /* There is only one hole in the range of supported
509 * rates, but it moves with the frame size.
510 */
511 memset(ranges, 0, sizeof(ranges));
512 ranges[0].min = 8000;
Peter Rosinf29933c2015-02-23 21:03:33 +0100513 ranges[0].max = pcm512x_sck_max(pcm512x) / frame_size / 2;
Peter Rosin9c7da1a2015-01-29 12:21:56 +0100514 ranges[1].min = DIV_ROUND_UP(16000000, frame_size);
515 ranges[1].max = 384000;
516 break;
517 default:
518 return -EINVAL;
519 }
Peter Rosinf086ba92015-01-28 15:16:10 +0100520
521 return snd_interval_ranges(hw_param_interval(params, rule->var),
Peter Rosin9c7da1a2015-01-29 12:21:56 +0100522 ARRAY_SIZE(ranges), ranges, 0);
Peter Rosinf086ba92015-01-28 15:16:10 +0100523}
524
Peter Rosin81249302015-01-28 15:16:09 +0100525static int pcm512x_dai_startup_master(struct snd_pcm_substream *substream,
526 struct snd_soc_dai *dai)
527{
528 struct snd_soc_codec *codec = dai->codec;
529 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
530 struct device *dev = dai->dev;
531 struct snd_pcm_hw_constraint_ratnums *constraints_no_pll;
532 struct snd_ratnum *rats_no_pll;
533
534 if (IS_ERR(pcm512x->sclk)) {
535 dev_err(dev, "Need SCLK for master mode: %ld\n",
536 PTR_ERR(pcm512x->sclk));
537 return PTR_ERR(pcm512x->sclk);
538 }
539
Peter Rosinf086ba92015-01-28 15:16:10 +0100540 if (pcm512x->pll_out)
541 return snd_pcm_hw_rule_add(substream->runtime, 0,
542 SNDRV_PCM_HW_PARAM_RATE,
543 pcm512x_hw_rule_rate,
Peter Rosinf29933c2015-02-23 21:03:33 +0100544 pcm512x,
Peter Rosinf086ba92015-01-28 15:16:10 +0100545 SNDRV_PCM_HW_PARAM_FRAME_BITS,
546 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
547
Peter Rosin81249302015-01-28 15:16:09 +0100548 constraints_no_pll = devm_kzalloc(dev, sizeof(*constraints_no_pll),
549 GFP_KERNEL);
550 if (!constraints_no_pll)
551 return -ENOMEM;
552 constraints_no_pll->nrats = 1;
553 rats_no_pll = devm_kzalloc(dev, sizeof(*rats_no_pll), GFP_KERNEL);
554 if (!rats_no_pll)
555 return -ENOMEM;
556 constraints_no_pll->rats = rats_no_pll;
557 rats_no_pll->num = clk_get_rate(pcm512x->sclk) / 64;
558 rats_no_pll->den_min = 1;
559 rats_no_pll->den_max = 128;
560 rats_no_pll->den_step = 1;
561
562 return snd_pcm_hw_constraint_ratnums(substream->runtime, 0,
563 SNDRV_PCM_HW_PARAM_RATE,
564 constraints_no_pll);
565}
566
567static int pcm512x_dai_startup_slave(struct snd_pcm_substream *substream,
568 struct snd_soc_dai *dai)
569{
570 struct snd_soc_codec *codec = dai->codec;
571 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
572 struct device *dev = dai->dev;
573 struct regmap *regmap = pcm512x->regmap;
574
575 if (IS_ERR(pcm512x->sclk)) {
576 dev_info(dev, "No SCLK, using BCLK: %ld\n",
577 PTR_ERR(pcm512x->sclk));
578
579 /* Disable reporting of missing SCLK as an error */
580 regmap_update_bits(regmap, PCM512x_ERROR_DETECT,
581 PCM512x_IDCH, PCM512x_IDCH);
582
583 /* Switch PLL input to BCLK */
584 regmap_update_bits(regmap, PCM512x_PLL_REF,
585 PCM512x_SREF, PCM512x_SREF_BCK);
586 }
587
588 return snd_pcm_hw_constraint_list(substream->runtime, 0,
589 SNDRV_PCM_HW_PARAM_RATE,
590 &constraints_slave);
591}
592
593static int pcm512x_dai_startup(struct snd_pcm_substream *substream,
594 struct snd_soc_dai *dai)
595{
596 struct snd_soc_codec *codec = dai->codec;
597 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
598
599 switch (pcm512x->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
600 case SND_SOC_DAIFMT_CBM_CFM:
Peter Rosind11c2972015-01-28 15:16:12 +0100601 case SND_SOC_DAIFMT_CBM_CFS:
Peter Rosin81249302015-01-28 15:16:09 +0100602 return pcm512x_dai_startup_master(substream, dai);
603
604 case SND_SOC_DAIFMT_CBS_CFS:
605 return pcm512x_dai_startup_slave(substream, dai);
606
607 default:
608 return -EINVAL;
609 }
610}
611
Mark Brown5a3af122014-02-06 12:03:27 +0000612static int pcm512x_set_bias_level(struct snd_soc_codec *codec,
613 enum snd_soc_bias_level level)
614{
615 struct pcm512x_priv *pcm512x = dev_get_drvdata(codec->dev);
616 int ret;
617
618 switch (level) {
619 case SND_SOC_BIAS_ON:
620 case SND_SOC_BIAS_PREPARE:
621 break;
622
623 case SND_SOC_BIAS_STANDBY:
624 ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
625 PCM512x_RQST, 0);
626 if (ret != 0) {
627 dev_err(codec->dev, "Failed to remove standby: %d\n",
628 ret);
629 return ret;
630 }
631 break;
632
633 case SND_SOC_BIAS_OFF:
634 ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
635 PCM512x_RQST, PCM512x_RQST);
636 if (ret != 0) {
637 dev_err(codec->dev, "Failed to request standby: %d\n",
638 ret);
639 return ret;
640 }
641 break;
642 }
643
644 codec->dapm.bias_level = level;
645
646 return 0;
647}
648
Peter Rosinf086ba92015-01-28 15:16:10 +0100649static unsigned long pcm512x_find_sck(struct snd_soc_dai *dai,
650 unsigned long bclk_rate)
651{
652 struct device *dev = dai->dev;
Peter Rosinf29933c2015-02-23 21:03:33 +0100653 struct snd_soc_codec *codec = dai->codec;
654 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
Peter Rosinf086ba92015-01-28 15:16:10 +0100655 unsigned long sck_rate;
656 int pow2;
657
658 /* 64 MHz <= pll_rate <= 100 MHz, VREF mode */
659 /* 16 MHz <= sck_rate <= 25 MHz, VREF mode */
660
661 /* select sck_rate as a multiple of bclk_rate but still with
662 * as many factors of 2 as possible, as that makes it easier
663 * to find a fast DAC rate
664 */
Peter Rosinf29933c2015-02-23 21:03:33 +0100665 pow2 = 1 << fls((pcm512x_pll_max(pcm512x) - 16000000) / bclk_rate);
Peter Rosinf086ba92015-01-28 15:16:10 +0100666 for (; pow2; pow2 >>= 1) {
Peter Rosinf29933c2015-02-23 21:03:33 +0100667 sck_rate = rounddown(pcm512x_pll_max(pcm512x),
668 bclk_rate * pow2);
Peter Rosinf086ba92015-01-28 15:16:10 +0100669 if (sck_rate >= 16000000)
670 break;
671 }
672 if (!pow2) {
673 dev_err(dev, "Impossible to generate a suitable SCK\n");
674 return 0;
675 }
676
677 dev_dbg(dev, "sck_rate %lu\n", sck_rate);
678 return sck_rate;
679}
680
681/* pll_rate = pllin_rate * R * J.D / P
682 * 1 <= R <= 16
683 * 1 <= J <= 63
684 * 0 <= D <= 9999
685 * 1 <= P <= 15
686 * 64 MHz <= pll_rate <= 100 MHz
687 * if D == 0
688 * 1 MHz <= pllin_rate / P <= 20 MHz
689 * else if D > 0
690 * 6.667 MHz <= pllin_rate / P <= 20 MHz
691 * 4 <= J <= 11
692 * R = 1
693 */
694static int pcm512x_find_pll_coeff(struct snd_soc_dai *dai,
695 unsigned long pllin_rate,
696 unsigned long pll_rate)
697{
698 struct device *dev = dai->dev;
699 struct snd_soc_codec *codec = dai->codec;
700 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
701 unsigned long common;
702 int R, J, D, P;
703 unsigned long K; /* 10000 * J.D */
704 unsigned long num;
705 unsigned long den;
706
707 common = gcd(pll_rate, pllin_rate);
708 dev_dbg(dev, "pll %lu pllin %lu common %lu\n",
709 pll_rate, pllin_rate, common);
710 num = pll_rate / common;
711 den = pllin_rate / common;
712
713 /* pllin_rate / P (or here, den) cannot be greater than 20 MHz */
714 if (pllin_rate / den > 20000000 && num < 8) {
Howard Mitchellf073faa2015-03-20 21:13:45 +0000715 num *= DIV_ROUND_UP(pllin_rate / den, 20000000);
716 den *= DIV_ROUND_UP(pllin_rate / den, 20000000);
Peter Rosinf086ba92015-01-28 15:16:10 +0100717 }
718 dev_dbg(dev, "num / den = %lu / %lu\n", num, den);
719
720 P = den;
721 if (den <= 15 && num <= 16 * 63
722 && 1000000 <= pllin_rate / P && pllin_rate / P <= 20000000) {
723 /* Try the case with D = 0 */
724 D = 0;
725 /* factor 'num' into J and R, such that R <= 16 and J <= 63 */
726 for (R = 16; R; R--) {
727 if (num % R)
728 continue;
729 J = num / R;
730 if (J == 0 || J > 63)
731 continue;
732
733 dev_dbg(dev, "R * J / P = %d * %d / %d\n", R, J, P);
734 pcm512x->real_pll = pll_rate;
735 goto done;
736 }
737 /* no luck */
738 }
739
740 R = 1;
741
742 if (num > 0xffffffffUL / 10000)
743 goto fallback;
744
745 /* Try to find an exact pll_rate using the D > 0 case */
746 common = gcd(10000 * num, den);
747 num = 10000 * num / common;
748 den /= common;
749 dev_dbg(dev, "num %lu den %lu common %lu\n", num, den, common);
750
751 for (P = den; P <= 15; P++) {
752 if (pllin_rate / P < 6667000 || 200000000 < pllin_rate / P)
753 continue;
754 if (num * P % den)
755 continue;
756 K = num * P / den;
757 /* J == 12 is ok if D == 0 */
758 if (K < 40000 || K > 120000)
759 continue;
760
761 J = K / 10000;
762 D = K % 10000;
763 dev_dbg(dev, "J.D / P = %d.%04d / %d\n", J, D, P);
764 pcm512x->real_pll = pll_rate;
765 goto done;
766 }
767
768 /* Fall back to an approximate pll_rate */
769
770fallback:
771 /* find smallest possible P */
772 P = DIV_ROUND_UP(pllin_rate, 20000000);
773 if (!P)
774 P = 1;
775 else if (P > 15) {
776 dev_err(dev, "Need a slower clock as pll-input\n");
777 return -EINVAL;
778 }
779 if (pllin_rate / P < 6667000) {
780 dev_err(dev, "Need a faster clock as pll-input\n");
781 return -EINVAL;
782 }
783 K = DIV_ROUND_CLOSEST_ULL(10000ULL * pll_rate * P, pllin_rate);
784 if (K < 40000)
785 K = 40000;
786 /* J == 12 is ok if D == 0 */
787 if (K > 120000)
788 K = 120000;
789 J = K / 10000;
790 D = K % 10000;
791 dev_dbg(dev, "J.D / P ~ %d.%04d / %d\n", J, D, P);
792 pcm512x->real_pll = DIV_ROUND_DOWN_ULL((u64)K * pllin_rate, 10000 * P);
793
794done:
795 pcm512x->pll_r = R;
796 pcm512x->pll_j = J;
797 pcm512x->pll_d = D;
798 pcm512x->pll_p = P;
799 return 0;
800}
801
Peter Rosin7c4e1112015-01-28 15:16:11 +0100802static unsigned long pcm512x_pllin_dac_rate(struct snd_soc_dai *dai,
803 unsigned long osr_rate,
804 unsigned long pllin_rate)
805{
806 struct snd_soc_codec *codec = dai->codec;
807 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
808 unsigned long dac_rate;
809
810 if (!pcm512x->pll_out)
811 return 0; /* no PLL to bypass, force SCK as DAC input */
812
813 if (pllin_rate % osr_rate)
814 return 0; /* futile, quit early */
815
816 /* run DAC no faster than 6144000 Hz */
Peter Rosinf29933c2015-02-23 21:03:33 +0100817 for (dac_rate = rounddown(pcm512x_dac_max(pcm512x, 6144000), osr_rate);
Peter Rosin7c4e1112015-01-28 15:16:11 +0100818 dac_rate;
819 dac_rate -= osr_rate) {
820
821 if (pllin_rate / dac_rate > 128)
822 return 0; /* DAC divider would be too big */
823
824 if (!(pllin_rate % dac_rate))
825 return dac_rate;
826
827 dac_rate -= osr_rate;
828 }
829
830 return 0;
831}
832
Peter Rosin81249302015-01-28 15:16:09 +0100833static int pcm512x_set_dividers(struct snd_soc_dai *dai,
834 struct snd_pcm_hw_params *params)
835{
836 struct device *dev = dai->dev;
837 struct snd_soc_codec *codec = dai->codec;
838 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
Peter Rosinf086ba92015-01-28 15:16:10 +0100839 unsigned long pllin_rate = 0;
840 unsigned long pll_rate;
Peter Rosin81249302015-01-28 15:16:09 +0100841 unsigned long sck_rate;
842 unsigned long mck_rate;
843 unsigned long bclk_rate;
844 unsigned long sample_rate;
845 unsigned long osr_rate;
Peter Rosin7c4e1112015-01-28 15:16:11 +0100846 unsigned long dacsrc_rate;
Peter Rosin81249302015-01-28 15:16:09 +0100847 int bclk_div;
848 int lrclk_div;
849 int dsp_div;
850 int dac_div;
851 unsigned long dac_rate;
852 int ncp_div;
853 int osr_div;
Peter Rosin81249302015-01-28 15:16:09 +0100854 int ret;
855 int idac;
856 int fssp;
Peter Rosin7c4e1112015-01-28 15:16:11 +0100857 int gpio;
Peter Rosin81249302015-01-28 15:16:09 +0100858
859 lrclk_div = snd_soc_params_to_frame_size(params);
860 if (lrclk_div == 0) {
861 dev_err(dev, "No LRCLK?\n");
862 return -EINVAL;
863 }
864
Peter Rosinf086ba92015-01-28 15:16:10 +0100865 if (!pcm512x->pll_out) {
866 sck_rate = clk_get_rate(pcm512x->sclk);
867 bclk_div = params->rate_den * 64 / lrclk_div;
868 bclk_rate = DIV_ROUND_CLOSEST(sck_rate, bclk_div);
Peter Rosin81249302015-01-28 15:16:09 +0100869
Peter Rosinf086ba92015-01-28 15:16:10 +0100870 mck_rate = sck_rate;
871 } else {
872 ret = snd_soc_params_to_bclk(params);
873 if (ret < 0) {
874 dev_err(dev, "Failed to find suitable BCLK: %d\n", ret);
875 return ret;
876 }
877 if (ret == 0) {
878 dev_err(dev, "No BCLK?\n");
879 return -EINVAL;
880 }
881 bclk_rate = ret;
882
883 pllin_rate = clk_get_rate(pcm512x->sclk);
884
885 sck_rate = pcm512x_find_sck(dai, bclk_rate);
886 if (!sck_rate)
887 return -EINVAL;
888 pll_rate = 4 * sck_rate;
889
890 ret = pcm512x_find_pll_coeff(dai, pllin_rate, pll_rate);
891 if (ret != 0)
892 return ret;
893
894 ret = regmap_write(pcm512x->regmap,
895 PCM512x_PLL_COEFF_0, pcm512x->pll_p - 1);
896 if (ret != 0) {
897 dev_err(dev, "Failed to write PLL P: %d\n", ret);
898 return ret;
899 }
900
901 ret = regmap_write(pcm512x->regmap,
902 PCM512x_PLL_COEFF_1, pcm512x->pll_j);
903 if (ret != 0) {
904 dev_err(dev, "Failed to write PLL J: %d\n", ret);
905 return ret;
906 }
907
908 ret = regmap_write(pcm512x->regmap,
909 PCM512x_PLL_COEFF_2, pcm512x->pll_d >> 8);
910 if (ret != 0) {
911 dev_err(dev, "Failed to write PLL D msb: %d\n", ret);
912 return ret;
913 }
914
915 ret = regmap_write(pcm512x->regmap,
916 PCM512x_PLL_COEFF_3, pcm512x->pll_d & 0xff);
917 if (ret != 0) {
918 dev_err(dev, "Failed to write PLL D lsb: %d\n", ret);
919 return ret;
920 }
921
922 ret = regmap_write(pcm512x->regmap,
923 PCM512x_PLL_COEFF_4, pcm512x->pll_r - 1);
924 if (ret != 0) {
925 dev_err(dev, "Failed to write PLL R: %d\n", ret);
926 return ret;
927 }
928
929 mck_rate = pcm512x->real_pll;
930
931 bclk_div = DIV_ROUND_CLOSEST(sck_rate, bclk_rate);
932 }
Peter Rosin81249302015-01-28 15:16:09 +0100933
934 if (bclk_div > 128) {
935 dev_err(dev, "Failed to find BCLK divider\n");
936 return -EINVAL;
937 }
938
939 /* the actual rate */
940 sample_rate = sck_rate / bclk_div / lrclk_div;
941 osr_rate = 16 * sample_rate;
942
943 /* run DSP no faster than 50 MHz */
Peter Rosinf29933c2015-02-23 21:03:33 +0100944 dsp_div = mck_rate > pcm512x_dsp_max(pcm512x) ? 2 : 1;
Peter Rosin81249302015-01-28 15:16:09 +0100945
Peter Rosin7c4e1112015-01-28 15:16:11 +0100946 dac_rate = pcm512x_pllin_dac_rate(dai, osr_rate, pllin_rate);
947 if (dac_rate) {
948 /* the desired clock rate is "compatible" with the pll input
949 * clock, so use that clock as dac input instead of the pll
950 * output clock since the pll will introduce jitter and thus
951 * noise.
952 */
953 dev_dbg(dev, "using pll input as dac input\n");
954 ret = regmap_update_bits(pcm512x->regmap, PCM512x_DAC_REF,
955 PCM512x_SDAC, PCM512x_SDAC_GPIO);
956 if (ret != 0) {
957 dev_err(codec->dev,
958 "Failed to set gpio as dacref: %d\n", ret);
959 return ret;
960 }
961
962 gpio = PCM512x_GREF_GPIO1 + pcm512x->pll_in - 1;
963 ret = regmap_update_bits(pcm512x->regmap, PCM512x_GPIO_DACIN,
964 PCM512x_GREF, gpio);
965 if (ret != 0) {
966 dev_err(codec->dev,
967 "Failed to set gpio %d as dacin: %d\n",
968 pcm512x->pll_in, ret);
969 return ret;
970 }
971
972 dacsrc_rate = pllin_rate;
973 } else {
974 /* run DAC no faster than 6144000 Hz */
Peter Rosinf29933c2015-02-23 21:03:33 +0100975 unsigned long dac_mul = pcm512x_dac_max(pcm512x, 6144000)
976 / osr_rate;
Peter Rosin7c4e1112015-01-28 15:16:11 +0100977 unsigned long sck_mul = sck_rate / osr_rate;
978
979 for (; dac_mul; dac_mul--) {
980 if (!(sck_mul % dac_mul))
981 break;
982 }
983 if (!dac_mul) {
984 dev_err(dev, "Failed to find DAC rate\n");
985 return -EINVAL;
986 }
987
988 dac_rate = dac_mul * osr_rate;
989 dev_dbg(dev, "dac_rate %lu sample_rate %lu\n",
990 dac_rate, sample_rate);
991
992 ret = regmap_update_bits(pcm512x->regmap, PCM512x_DAC_REF,
993 PCM512x_SDAC, PCM512x_SDAC_SCK);
994 if (ret != 0) {
995 dev_err(codec->dev,
996 "Failed to set sck as dacref: %d\n", ret);
997 return ret;
998 }
999
1000 dacsrc_rate = sck_rate;
Peter Rosin81249302015-01-28 15:16:09 +01001001 }
1002
Peter Rosin5890bd52015-02-16 22:02:47 +01001003 osr_div = DIV_ROUND_CLOSEST(dac_rate, osr_rate);
1004 if (osr_div > 128) {
1005 dev_err(dev, "Failed to find OSR divider\n");
1006 return -EINVAL;
1007 }
1008
Peter Rosin7c4e1112015-01-28 15:16:11 +01001009 dac_div = DIV_ROUND_CLOSEST(dacsrc_rate, dac_rate);
Peter Rosin81249302015-01-28 15:16:09 +01001010 if (dac_div > 128) {
1011 dev_err(dev, "Failed to find DAC divider\n");
1012 return -EINVAL;
1013 }
Peter Rosin5890bd52015-02-16 22:02:47 +01001014 dac_rate = dacsrc_rate / dac_div;
Peter Rosin81249302015-01-28 15:16:09 +01001015
Peter Rosinf29933c2015-02-23 21:03:33 +01001016 ncp_div = DIV_ROUND_CLOSEST(dac_rate,
1017 pcm512x_ncp_target(pcm512x, dac_rate));
Peter Rosin5890bd52015-02-16 22:02:47 +01001018 if (ncp_div > 128 || dac_rate / ncp_div > 2048000) {
Peter Rosin81249302015-01-28 15:16:09 +01001019 /* run NCP no faster than 2048000 Hz, but why? */
Peter Rosin5890bd52015-02-16 22:02:47 +01001020 ncp_div = DIV_ROUND_UP(dac_rate, 2048000);
Peter Rosin81249302015-01-28 15:16:09 +01001021 if (ncp_div > 128) {
1022 dev_err(dev, "Failed to find NCP divider\n");
1023 return -EINVAL;
1024 }
1025 }
1026
Peter Rosin81249302015-01-28 15:16:09 +01001027 idac = mck_rate / (dsp_div * sample_rate);
1028
1029 ret = regmap_write(pcm512x->regmap, PCM512x_DSP_CLKDIV, dsp_div - 1);
1030 if (ret != 0) {
1031 dev_err(dev, "Failed to write DSP divider: %d\n", ret);
1032 return ret;
1033 }
1034
1035 ret = regmap_write(pcm512x->regmap, PCM512x_DAC_CLKDIV, dac_div - 1);
1036 if (ret != 0) {
1037 dev_err(dev, "Failed to write DAC divider: %d\n", ret);
1038 return ret;
1039 }
1040
1041 ret = regmap_write(pcm512x->regmap, PCM512x_NCP_CLKDIV, ncp_div - 1);
1042 if (ret != 0) {
1043 dev_err(dev, "Failed to write NCP divider: %d\n", ret);
1044 return ret;
1045 }
1046
1047 ret = regmap_write(pcm512x->regmap, PCM512x_OSR_CLKDIV, osr_div - 1);
1048 if (ret != 0) {
1049 dev_err(dev, "Failed to write OSR divider: %d\n", ret);
1050 return ret;
1051 }
1052
1053 ret = regmap_write(pcm512x->regmap,
1054 PCM512x_MASTER_CLKDIV_1, bclk_div - 1);
1055 if (ret != 0) {
1056 dev_err(dev, "Failed to write BCLK divider: %d\n", ret);
1057 return ret;
1058 }
1059
1060 ret = regmap_write(pcm512x->regmap,
1061 PCM512x_MASTER_CLKDIV_2, lrclk_div - 1);
1062 if (ret != 0) {
1063 dev_err(dev, "Failed to write LRCLK divider: %d\n", ret);
1064 return ret;
1065 }
1066
1067 ret = regmap_write(pcm512x->regmap, PCM512x_IDAC_1, idac >> 8);
1068 if (ret != 0) {
1069 dev_err(dev, "Failed to write IDAC msb divider: %d\n", ret);
1070 return ret;
1071 }
1072
1073 ret = regmap_write(pcm512x->regmap, PCM512x_IDAC_2, idac & 0xff);
1074 if (ret != 0) {
1075 dev_err(dev, "Failed to write IDAC lsb divider: %d\n", ret);
1076 return ret;
1077 }
1078
Peter Rosinf29933c2015-02-23 21:03:33 +01001079 if (sample_rate <= pcm512x_dac_max(pcm512x, 48000))
Peter Rosin81249302015-01-28 15:16:09 +01001080 fssp = PCM512x_FSSP_48KHZ;
Peter Rosinf29933c2015-02-23 21:03:33 +01001081 else if (sample_rate <= pcm512x_dac_max(pcm512x, 96000))
Peter Rosin81249302015-01-28 15:16:09 +01001082 fssp = PCM512x_FSSP_96KHZ;
Peter Rosinf29933c2015-02-23 21:03:33 +01001083 else if (sample_rate <= pcm512x_dac_max(pcm512x, 192000))
Peter Rosin81249302015-01-28 15:16:09 +01001084 fssp = PCM512x_FSSP_192KHZ;
1085 else
1086 fssp = PCM512x_FSSP_384KHZ;
1087 ret = regmap_update_bits(pcm512x->regmap, PCM512x_FS_SPEED_MODE,
1088 PCM512x_FSSP, fssp);
1089 if (ret != 0) {
1090 dev_err(codec->dev, "Failed to set fs speed: %d\n", ret);
1091 return ret;
1092 }
1093
1094 dev_dbg(codec->dev, "DSP divider %d\n", dsp_div);
1095 dev_dbg(codec->dev, "DAC divider %d\n", dac_div);
1096 dev_dbg(codec->dev, "NCP divider %d\n", ncp_div);
1097 dev_dbg(codec->dev, "OSR divider %d\n", osr_div);
1098 dev_dbg(codec->dev, "BCK divider %d\n", bclk_div);
1099 dev_dbg(codec->dev, "LRCK divider %d\n", lrclk_div);
1100 dev_dbg(codec->dev, "IDAC %d\n", idac);
1101 dev_dbg(codec->dev, "1<<FSSP %d\n", 1 << fssp);
1102
1103 return 0;
1104}
1105
1106static int pcm512x_hw_params(struct snd_pcm_substream *substream,
1107 struct snd_pcm_hw_params *params,
1108 struct snd_soc_dai *dai)
1109{
1110 struct snd_soc_codec *codec = dai->codec;
1111 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
1112 int alen;
Peter Rosinf086ba92015-01-28 15:16:10 +01001113 int gpio;
Peter Rosind11c2972015-01-28 15:16:12 +01001114 int clock_output;
1115 int master_mode;
Peter Rosin81249302015-01-28 15:16:09 +01001116 int ret;
1117
1118 dev_dbg(codec->dev, "hw_params %u Hz, %u channels\n",
1119 params_rate(params),
1120 params_channels(params));
1121
1122 switch (snd_pcm_format_width(params_format(params))) {
1123 case 16:
1124 alen = PCM512x_ALEN_16;
1125 break;
1126 case 20:
1127 alen = PCM512x_ALEN_20;
1128 break;
1129 case 24:
1130 alen = PCM512x_ALEN_24;
1131 break;
1132 case 32:
1133 alen = PCM512x_ALEN_32;
1134 break;
1135 default:
1136 dev_err(codec->dev, "Bad frame size: %d\n",
1137 snd_pcm_format_width(params_format(params)));
1138 return -EINVAL;
1139 }
1140
1141 switch (pcm512x->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1142 case SND_SOC_DAIFMT_CBS_CFS:
1143 ret = regmap_update_bits(pcm512x->regmap,
1144 PCM512x_BCLK_LRCLK_CFG,
1145 PCM512x_BCKP
1146 | PCM512x_BCKO | PCM512x_LRKO,
1147 0);
1148 if (ret != 0) {
1149 dev_err(codec->dev,
1150 "Failed to enable slave mode: %d\n", ret);
1151 return ret;
1152 }
1153
1154 ret = regmap_update_bits(pcm512x->regmap, PCM512x_ERROR_DETECT,
1155 PCM512x_DCAS, 0);
1156 if (ret != 0) {
1157 dev_err(codec->dev,
1158 "Failed to enable clock divider autoset: %d\n",
1159 ret);
1160 return ret;
1161 }
1162 return 0;
1163 case SND_SOC_DAIFMT_CBM_CFM:
Peter Rosind11c2972015-01-28 15:16:12 +01001164 clock_output = PCM512x_BCKO | PCM512x_LRKO;
1165 master_mode = PCM512x_RLRK | PCM512x_RBCK;
1166 break;
1167 case SND_SOC_DAIFMT_CBM_CFS:
1168 clock_output = PCM512x_BCKO;
1169 master_mode = PCM512x_RBCK;
Peter Rosin81249302015-01-28 15:16:09 +01001170 break;
1171 default:
1172 return -EINVAL;
1173 }
1174
1175 ret = regmap_update_bits(pcm512x->regmap, PCM512x_I2S_1,
1176 PCM512x_ALEN, alen);
1177 if (ret != 0) {
1178 dev_err(codec->dev, "Failed to set frame size: %d\n", ret);
1179 return ret;
1180 }
1181
Peter Rosinf086ba92015-01-28 15:16:10 +01001182 if (pcm512x->pll_out) {
1183 ret = regmap_write(pcm512x->regmap, PCM512x_FLEX_A, 0x11);
1184 if (ret != 0) {
1185 dev_err(codec->dev, "Failed to set FLEX_A: %d\n", ret);
1186 return ret;
1187 }
Peter Rosin81249302015-01-28 15:16:09 +01001188
Peter Rosinf086ba92015-01-28 15:16:10 +01001189 ret = regmap_write(pcm512x->regmap, PCM512x_FLEX_B, 0xff);
1190 if (ret != 0) {
1191 dev_err(codec->dev, "Failed to set FLEX_B: %d\n", ret);
1192 return ret;
1193 }
1194
1195 ret = regmap_update_bits(pcm512x->regmap, PCM512x_ERROR_DETECT,
1196 PCM512x_IDFS | PCM512x_IDBK
1197 | PCM512x_IDSK | PCM512x_IDCH
1198 | PCM512x_IDCM | PCM512x_DCAS
1199 | PCM512x_IPLK,
1200 PCM512x_IDFS | PCM512x_IDBK
1201 | PCM512x_IDSK | PCM512x_IDCH
1202 | PCM512x_DCAS);
1203 if (ret != 0) {
1204 dev_err(codec->dev,
1205 "Failed to ignore auto-clock failures: %d\n",
1206 ret);
1207 return ret;
1208 }
1209 } else {
1210 ret = regmap_update_bits(pcm512x->regmap, PCM512x_ERROR_DETECT,
1211 PCM512x_IDFS | PCM512x_IDBK
1212 | PCM512x_IDSK | PCM512x_IDCH
1213 | PCM512x_IDCM | PCM512x_DCAS
1214 | PCM512x_IPLK,
1215 PCM512x_IDFS | PCM512x_IDBK
1216 | PCM512x_IDSK | PCM512x_IDCH
1217 | PCM512x_DCAS | PCM512x_IPLK);
1218 if (ret != 0) {
1219 dev_err(codec->dev,
1220 "Failed to ignore auto-clock failures: %d\n",
1221 ret);
1222 return ret;
1223 }
1224
1225 ret = regmap_update_bits(pcm512x->regmap, PCM512x_PLL_EN,
1226 PCM512x_PLLE, 0);
1227 if (ret != 0) {
1228 dev_err(codec->dev, "Failed to disable pll: %d\n", ret);
1229 return ret;
1230 }
Peter Rosin81249302015-01-28 15:16:09 +01001231 }
1232
1233 ret = pcm512x_set_dividers(dai, params);
1234 if (ret != 0)
1235 return ret;
1236
Peter Rosinf086ba92015-01-28 15:16:10 +01001237 if (pcm512x->pll_out) {
1238 ret = regmap_update_bits(pcm512x->regmap, PCM512x_PLL_REF,
1239 PCM512x_SREF, PCM512x_SREF_GPIO);
1240 if (ret != 0) {
1241 dev_err(codec->dev,
1242 "Failed to set gpio as pllref: %d\n", ret);
1243 return ret;
1244 }
1245
1246 gpio = PCM512x_GREF_GPIO1 + pcm512x->pll_in - 1;
1247 ret = regmap_update_bits(pcm512x->regmap, PCM512x_GPIO_PLLIN,
1248 PCM512x_GREF, gpio);
1249 if (ret != 0) {
1250 dev_err(codec->dev,
1251 "Failed to set gpio %d as pllin: %d\n",
1252 pcm512x->pll_in, ret);
1253 return ret;
1254 }
1255
1256 ret = regmap_update_bits(pcm512x->regmap, PCM512x_PLL_EN,
1257 PCM512x_PLLE, PCM512x_PLLE);
1258 if (ret != 0) {
1259 dev_err(codec->dev, "Failed to enable pll: %d\n", ret);
1260 return ret;
1261 }
1262 }
1263
Peter Rosin81249302015-01-28 15:16:09 +01001264 ret = regmap_update_bits(pcm512x->regmap, PCM512x_BCLK_LRCLK_CFG,
1265 PCM512x_BCKP | PCM512x_BCKO | PCM512x_LRKO,
Peter Rosind11c2972015-01-28 15:16:12 +01001266 clock_output);
Peter Rosin81249302015-01-28 15:16:09 +01001267 if (ret != 0) {
1268 dev_err(codec->dev, "Failed to enable clock output: %d\n", ret);
1269 return ret;
1270 }
1271
1272 ret = regmap_update_bits(pcm512x->regmap, PCM512x_MASTER_MODE,
1273 PCM512x_RLRK | PCM512x_RBCK,
Peter Rosind11c2972015-01-28 15:16:12 +01001274 master_mode);
Peter Rosin81249302015-01-28 15:16:09 +01001275 if (ret != 0) {
1276 dev_err(codec->dev, "Failed to enable master mode: %d\n", ret);
1277 return ret;
1278 }
1279
Peter Rosinf086ba92015-01-28 15:16:10 +01001280 if (pcm512x->pll_out) {
1281 gpio = PCM512x_G1OE << (pcm512x->pll_out - 1);
1282 ret = regmap_update_bits(pcm512x->regmap, PCM512x_GPIO_EN,
1283 gpio, gpio);
1284 if (ret != 0) {
1285 dev_err(codec->dev, "Failed to enable gpio %d: %d\n",
1286 pcm512x->pll_out, ret);
1287 return ret;
1288 }
1289
1290 gpio = PCM512x_GPIO_OUTPUT_1 + pcm512x->pll_out - 1;
1291 ret = regmap_update_bits(pcm512x->regmap, gpio,
1292 PCM512x_GxSL, PCM512x_GxSL_PLLCK);
1293 if (ret != 0) {
1294 dev_err(codec->dev, "Failed to output pll on %d: %d\n",
1295 ret, pcm512x->pll_out);
1296 return ret;
1297 }
Peter Rosinf086ba92015-01-28 15:16:10 +01001298 }
1299
Peter Rosin81249302015-01-28 15:16:09 +01001300 ret = regmap_update_bits(pcm512x->regmap, PCM512x_SYNCHRONIZE,
1301 PCM512x_RQSY, PCM512x_RQSY_HALT);
1302 if (ret != 0) {
1303 dev_err(codec->dev, "Failed to halt clocks: %d\n", ret);
1304 return ret;
1305 }
1306
1307 ret = regmap_update_bits(pcm512x->regmap, PCM512x_SYNCHRONIZE,
1308 PCM512x_RQSY, PCM512x_RQSY_RESUME);
1309 if (ret != 0) {
1310 dev_err(codec->dev, "Failed to resume clocks: %d\n", ret);
1311 return ret;
1312 }
1313
1314 return 0;
1315}
1316
1317static int pcm512x_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1318{
1319 struct snd_soc_codec *codec = dai->codec;
1320 struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
1321
1322 pcm512x->fmt = fmt;
1323
1324 return 0;
1325}
1326
1327static const struct snd_soc_dai_ops pcm512x_dai_ops = {
1328 .startup = pcm512x_dai_startup,
1329 .hw_params = pcm512x_hw_params,
1330 .set_fmt = pcm512x_set_fmt,
1331};
1332
Mark Brown5a3af122014-02-06 12:03:27 +00001333static struct snd_soc_dai_driver pcm512x_dai = {
1334 .name = "pcm512x-hifi",
1335 .playback = {
1336 .stream_name = "Playback",
1337 .channels_min = 2,
1338 .channels_max = 2,
Peter Rosin81249302015-01-28 15:16:09 +01001339 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1340 .rate_min = 8000,
1341 .rate_max = 384000,
Mark Brown5a3af122014-02-06 12:03:27 +00001342 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1343 SNDRV_PCM_FMTBIT_S24_LE |
1344 SNDRV_PCM_FMTBIT_S32_LE
1345 },
Peter Rosin81249302015-01-28 15:16:09 +01001346 .ops = &pcm512x_dai_ops,
Mark Brown5a3af122014-02-06 12:03:27 +00001347};
1348
1349static struct snd_soc_codec_driver pcm512x_codec_driver = {
1350 .set_bias_level = pcm512x_set_bias_level,
1351 .idle_bias_off = true,
1352
1353 .controls = pcm512x_controls,
1354 .num_controls = ARRAY_SIZE(pcm512x_controls),
1355 .dapm_widgets = pcm512x_dapm_widgets,
1356 .num_dapm_widgets = ARRAY_SIZE(pcm512x_dapm_widgets),
1357 .dapm_routes = pcm512x_dapm_routes,
1358 .num_dapm_routes = ARRAY_SIZE(pcm512x_dapm_routes),
1359};
1360
Mark Brown806d6462014-02-07 19:08:11 +00001361static const struct regmap_range_cfg pcm512x_range = {
1362 .name = "Pages", .range_min = PCM512x_VIRT_BASE,
1363 .range_max = PCM512x_MAX_REGISTER,
1364 .selector_reg = PCM512x_PAGE,
1365 .selector_mask = 0xff,
1366 .window_start = 0, .window_len = 0x100,
1367};
1368
Mark Brown22066222014-03-07 11:44:08 +08001369const struct regmap_config pcm512x_regmap = {
Mark Brown5a3af122014-02-06 12:03:27 +00001370 .reg_bits = 8,
1371 .val_bits = 8,
1372
1373 .readable_reg = pcm512x_readable,
1374 .volatile_reg = pcm512x_volatile,
1375
Mark Brown806d6462014-02-07 19:08:11 +00001376 .ranges = &pcm512x_range,
1377 .num_ranges = 1,
1378
Mark Brown5a3af122014-02-06 12:03:27 +00001379 .max_register = PCM512x_MAX_REGISTER,
1380 .reg_defaults = pcm512x_reg_defaults,
1381 .num_reg_defaults = ARRAY_SIZE(pcm512x_reg_defaults),
1382 .cache_type = REGCACHE_RBTREE,
1383};
Mark Brown22066222014-03-07 11:44:08 +08001384EXPORT_SYMBOL_GPL(pcm512x_regmap);
Mark Brown5a3af122014-02-06 12:03:27 +00001385
Mark Brown22066222014-03-07 11:44:08 +08001386int pcm512x_probe(struct device *dev, struct regmap *regmap)
Mark Brown5a3af122014-02-06 12:03:27 +00001387{
1388 struct pcm512x_priv *pcm512x;
1389 int i, ret;
1390
1391 pcm512x = devm_kzalloc(dev, sizeof(struct pcm512x_priv), GFP_KERNEL);
1392 if (!pcm512x)
1393 return -ENOMEM;
1394
1395 dev_set_drvdata(dev, pcm512x);
1396 pcm512x->regmap = regmap;
1397
1398 for (i = 0; i < ARRAY_SIZE(pcm512x->supplies); i++)
1399 pcm512x->supplies[i].supply = pcm512x_supply_names[i];
1400
1401 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(pcm512x->supplies),
1402 pcm512x->supplies);
1403 if (ret != 0) {
1404 dev_err(dev, "Failed to get supplies: %d\n", ret);
1405 return ret;
1406 }
1407
1408 pcm512x->supply_nb[0].notifier_call = pcm512x_regulator_event_0;
1409 pcm512x->supply_nb[1].notifier_call = pcm512x_regulator_event_1;
1410 pcm512x->supply_nb[2].notifier_call = pcm512x_regulator_event_2;
1411
1412 for (i = 0; i < ARRAY_SIZE(pcm512x->supplies); i++) {
1413 ret = regulator_register_notifier(pcm512x->supplies[i].consumer,
1414 &pcm512x->supply_nb[i]);
1415 if (ret != 0) {
1416 dev_err(dev,
1417 "Failed to register regulator notifier: %d\n",
1418 ret);
1419 }
1420 }
1421
1422 ret = regulator_bulk_enable(ARRAY_SIZE(pcm512x->supplies),
1423 pcm512x->supplies);
1424 if (ret != 0) {
1425 dev_err(dev, "Failed to enable supplies: %d\n", ret);
1426 return ret;
1427 }
1428
1429 /* Reset the device, verifying I/O in the process for I2C */
1430 ret = regmap_write(regmap, PCM512x_RESET,
1431 PCM512x_RSTM | PCM512x_RSTR);
1432 if (ret != 0) {
1433 dev_err(dev, "Failed to reset device: %d\n", ret);
1434 goto err;
1435 }
1436
1437 ret = regmap_write(regmap, PCM512x_RESET, 0);
1438 if (ret != 0) {
1439 dev_err(dev, "Failed to reset device: %d\n", ret);
1440 goto err;
1441 }
1442
1443 pcm512x->sclk = devm_clk_get(dev, NULL);
Peter Rosin81249302015-01-28 15:16:09 +01001444 if (PTR_ERR(pcm512x->sclk) == -EPROBE_DEFER)
1445 return -EPROBE_DEFER;
1446 if (!IS_ERR(pcm512x->sclk)) {
Mark Brown5a3af122014-02-06 12:03:27 +00001447 ret = clk_prepare_enable(pcm512x->sclk);
1448 if (ret != 0) {
1449 dev_err(dev, "Failed to enable SCLK: %d\n", ret);
1450 return ret;
1451 }
1452 }
1453
1454 /* Default to standby mode */
1455 ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
1456 PCM512x_RQST, PCM512x_RQST);
1457 if (ret != 0) {
1458 dev_err(dev, "Failed to request standby: %d\n",
1459 ret);
1460 goto err_clk;
1461 }
1462
1463 pm_runtime_set_active(dev);
1464 pm_runtime_enable(dev);
1465 pm_runtime_idle(dev);
1466
Peter Rosinf086ba92015-01-28 15:16:10 +01001467#ifdef CONFIG_OF
1468 if (dev->of_node) {
1469 const struct device_node *np = dev->of_node;
Peter Rosin2599a962015-01-29 12:21:55 +01001470 u32 val;
Peter Rosinf086ba92015-01-28 15:16:10 +01001471
1472 if (of_property_read_u32(np, "pll-in", &val) >= 0) {
1473 if (val > 6) {
1474 dev_err(dev, "Invalid pll-in\n");
1475 ret = -EINVAL;
1476 goto err_clk;
1477 }
1478 pcm512x->pll_in = val;
1479 }
1480
1481 if (of_property_read_u32(np, "pll-out", &val) >= 0) {
1482 if (val > 6) {
1483 dev_err(dev, "Invalid pll-out\n");
1484 ret = -EINVAL;
1485 goto err_clk;
1486 }
1487 pcm512x->pll_out = val;
1488 }
1489
1490 if (!pcm512x->pll_in != !pcm512x->pll_out) {
1491 dev_err(dev,
1492 "Error: both pll-in and pll-out, or none\n");
1493 ret = -EINVAL;
1494 goto err_clk;
1495 }
1496 if (pcm512x->pll_in && pcm512x->pll_in == pcm512x->pll_out) {
1497 dev_err(dev, "Error: pll-in == pll-out\n");
1498 ret = -EINVAL;
1499 goto err_clk;
1500 }
1501 }
1502#endif
1503
Mark Brown5a3af122014-02-06 12:03:27 +00001504 ret = snd_soc_register_codec(dev, &pcm512x_codec_driver,
1505 &pcm512x_dai, 1);
1506 if (ret != 0) {
1507 dev_err(dev, "Failed to register CODEC: %d\n", ret);
1508 goto err_pm;
1509 }
1510
1511 return 0;
1512
1513err_pm:
1514 pm_runtime_disable(dev);
1515err_clk:
1516 if (!IS_ERR(pcm512x->sclk))
1517 clk_disable_unprepare(pcm512x->sclk);
1518err:
1519 regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
1520 pcm512x->supplies);
1521 return ret;
1522}
Mark Brown22066222014-03-07 11:44:08 +08001523EXPORT_SYMBOL_GPL(pcm512x_probe);
Mark Brown5a3af122014-02-06 12:03:27 +00001524
Mark Brown22066222014-03-07 11:44:08 +08001525void pcm512x_remove(struct device *dev)
Mark Brown5a3af122014-02-06 12:03:27 +00001526{
1527 struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
1528
1529 snd_soc_unregister_codec(dev);
1530 pm_runtime_disable(dev);
1531 if (!IS_ERR(pcm512x->sclk))
1532 clk_disable_unprepare(pcm512x->sclk);
1533 regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
1534 pcm512x->supplies);
1535}
Mark Brown22066222014-03-07 11:44:08 +08001536EXPORT_SYMBOL_GPL(pcm512x_remove);
Mark Brown5a3af122014-02-06 12:03:27 +00001537
Rafael J. Wysocki641d3342014-12-13 00:42:18 +01001538#ifdef CONFIG_PM
Mark Brown5a3af122014-02-06 12:03:27 +00001539static int pcm512x_suspend(struct device *dev)
1540{
1541 struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
1542 int ret;
1543
1544 ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
1545 PCM512x_RQPD, PCM512x_RQPD);
1546 if (ret != 0) {
1547 dev_err(dev, "Failed to request power down: %d\n", ret);
1548 return ret;
1549 }
1550
1551 ret = regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
1552 pcm512x->supplies);
1553 if (ret != 0) {
1554 dev_err(dev, "Failed to disable supplies: %d\n", ret);
1555 return ret;
1556 }
1557
1558 if (!IS_ERR(pcm512x->sclk))
1559 clk_disable_unprepare(pcm512x->sclk);
1560
1561 return 0;
1562}
1563
1564static int pcm512x_resume(struct device *dev)
1565{
1566 struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
1567 int ret;
1568
1569 if (!IS_ERR(pcm512x->sclk)) {
1570 ret = clk_prepare_enable(pcm512x->sclk);
1571 if (ret != 0) {
1572 dev_err(dev, "Failed to enable SCLK: %d\n", ret);
1573 return ret;
1574 }
1575 }
1576
1577 ret = regulator_bulk_enable(ARRAY_SIZE(pcm512x->supplies),
1578 pcm512x->supplies);
1579 if (ret != 0) {
1580 dev_err(dev, "Failed to enable supplies: %d\n", ret);
1581 return ret;
1582 }
1583
1584 regcache_cache_only(pcm512x->regmap, false);
1585 ret = regcache_sync(pcm512x->regmap);
1586 if (ret != 0) {
1587 dev_err(dev, "Failed to sync cache: %d\n", ret);
1588 return ret;
1589 }
1590
1591 ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
1592 PCM512x_RQPD, 0);
1593 if (ret != 0) {
1594 dev_err(dev, "Failed to remove power down: %d\n", ret);
1595 return ret;
1596 }
1597
1598 return 0;
1599}
Sachin Kamatccffbd22014-04-04 11:29:08 +05301600#endif
Mark Brown5a3af122014-02-06 12:03:27 +00001601
Mark Brown22066222014-03-07 11:44:08 +08001602const struct dev_pm_ops pcm512x_pm_ops = {
Mark Brown5a3af122014-02-06 12:03:27 +00001603 SET_RUNTIME_PM_OPS(pcm512x_suspend, pcm512x_resume, NULL)
1604};
Mark Brown22066222014-03-07 11:44:08 +08001605EXPORT_SYMBOL_GPL(pcm512x_pm_ops);
Mark Brown5a3af122014-02-06 12:03:27 +00001606
1607MODULE_DESCRIPTION("ASoC PCM512x codec driver");
1608MODULE_AUTHOR("Mark Brown <broonie@linaro.org>");
1609MODULE_LICENSE("GPL v2");