blob: 81fc97b07751e2a2cb2c4482e6c245daf3a3074b [file] [log] [blame]
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001/*
2 * Nuvoton NAU8825 audio codec driver
3 *
4 * Copyright 2015 Google Chromium project.
5 * Author: Anatol Pomozov <anatol@chromium.org>
6 * Copyright 2015 Nuvoton Technology Corp.
7 * Co-author: Meng-Huang Kuo <mhkuo@nuvoton.com>
8 *
9 * Licensed under the GPL-2.
10 */
11
12#include <linux/module.h>
13#include <linux/delay.h>
14#include <linux/init.h>
15#include <linux/i2c.h>
16#include <linux/regmap.h>
17#include <linux/slab.h>
18#include <linux/clk.h>
Fang, Yang Ab3681302015-10-07 14:33:57 -070019#include <linux/acpi.h>
Ben Zhangc86ba612015-10-19 16:49:05 -070020#include <linux/math64.h>
Anatol Pomozov34ca27f2015-10-02 09:49:14 -070021
22#include <sound/initval.h>
23#include <sound/tlv.h>
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28#include <sound/jack.h>
29
30
31#include "nau8825.h"
32
Ben Zhangc86ba612015-10-19 16:49:05 -070033#define NAU_FREF_MAX 13500000
John Hsu407c71b2016-03-15 12:09:36 +080034#define NAU_FVCO_MAX 124000000
Ben Zhangc86ba612015-10-19 16:49:05 -070035#define NAU_FVCO_MIN 90000000
36
37struct nau8825_fll {
38 int mclk_src;
39 int ratio;
40 int fll_frac;
41 int fll_int;
42 int clk_ref_div;
43};
44
45struct nau8825_fll_attr {
46 unsigned int param;
47 unsigned int val;
48};
49
50/* scaling for mclk from sysclk_src output */
51static const struct nau8825_fll_attr mclk_src_scaling[] = {
52 { 1, 0x0 },
53 { 2, 0x2 },
54 { 4, 0x3 },
55 { 8, 0x4 },
56 { 16, 0x5 },
57 { 32, 0x6 },
58 { 3, 0x7 },
59 { 6, 0xa },
60 { 12, 0xb },
61 { 24, 0xc },
62 { 48, 0xd },
63 { 96, 0xe },
64 { 5, 0xf },
65};
66
67/* ratio for input clk freq */
68static const struct nau8825_fll_attr fll_ratio[] = {
69 { 512000, 0x01 },
70 { 256000, 0x02 },
71 { 128000, 0x04 },
72 { 64000, 0x08 },
73 { 32000, 0x10 },
74 { 8000, 0x20 },
75 { 4000, 0x40 },
76};
77
78static const struct nau8825_fll_attr fll_pre_scalar[] = {
79 { 1, 0x0 },
80 { 2, 0x1 },
81 { 4, 0x2 },
82 { 8, 0x3 },
83};
84
Anatol Pomozov34ca27f2015-10-02 09:49:14 -070085static const struct reg_default nau8825_reg_defaults[] = {
86 { NAU8825_REG_ENA_CTRL, 0x00ff },
John Hsu45d5eb32016-03-11 17:33:58 -080087 { NAU8825_REG_IIC_ADDR_SET, 0x0 },
Anatol Pomozov34ca27f2015-10-02 09:49:14 -070088 { NAU8825_REG_CLK_DIVIDER, 0x0050 },
89 { NAU8825_REG_FLL1, 0x0 },
90 { NAU8825_REG_FLL2, 0x3126 },
91 { NAU8825_REG_FLL3, 0x0008 },
92 { NAU8825_REG_FLL4, 0x0010 },
93 { NAU8825_REG_FLL5, 0x0 },
94 { NAU8825_REG_FLL6, 0x6000 },
95 { NAU8825_REG_FLL_VCO_RSV, 0xf13c },
96 { NAU8825_REG_HSD_CTRL, 0x000c },
97 { NAU8825_REG_JACK_DET_CTRL, 0x0 },
98 { NAU8825_REG_INTERRUPT_MASK, 0x0 },
99 { NAU8825_REG_INTERRUPT_DIS_CTRL, 0xffff },
100 { NAU8825_REG_SAR_CTRL, 0x0015 },
101 { NAU8825_REG_KEYDET_CTRL, 0x0110 },
102 { NAU8825_REG_VDET_THRESHOLD_1, 0x0 },
103 { NAU8825_REG_VDET_THRESHOLD_2, 0x0 },
104 { NAU8825_REG_VDET_THRESHOLD_3, 0x0 },
105 { NAU8825_REG_VDET_THRESHOLD_4, 0x0 },
106 { NAU8825_REG_GPIO34_CTRL, 0x0 },
107 { NAU8825_REG_GPIO12_CTRL, 0x0 },
108 { NAU8825_REG_TDM_CTRL, 0x0 },
109 { NAU8825_REG_I2S_PCM_CTRL1, 0x000b },
110 { NAU8825_REG_I2S_PCM_CTRL2, 0x8010 },
111 { NAU8825_REG_LEFT_TIME_SLOT, 0x0 },
112 { NAU8825_REG_RIGHT_TIME_SLOT, 0x0 },
113 { NAU8825_REG_BIQ_CTRL, 0x0 },
114 { NAU8825_REG_BIQ_COF1, 0x0 },
115 { NAU8825_REG_BIQ_COF2, 0x0 },
116 { NAU8825_REG_BIQ_COF3, 0x0 },
117 { NAU8825_REG_BIQ_COF4, 0x0 },
118 { NAU8825_REG_BIQ_COF5, 0x0 },
119 { NAU8825_REG_BIQ_COF6, 0x0 },
120 { NAU8825_REG_BIQ_COF7, 0x0 },
121 { NAU8825_REG_BIQ_COF8, 0x0 },
122 { NAU8825_REG_BIQ_COF9, 0x0 },
123 { NAU8825_REG_BIQ_COF10, 0x0 },
124 { NAU8825_REG_ADC_RATE, 0x0010 },
125 { NAU8825_REG_DAC_CTRL1, 0x0001 },
126 { NAU8825_REG_DAC_CTRL2, 0x0 },
127 { NAU8825_REG_DAC_DGAIN_CTRL, 0x0 },
128 { NAU8825_REG_ADC_DGAIN_CTRL, 0x00cf },
129 { NAU8825_REG_MUTE_CTRL, 0x0 },
130 { NAU8825_REG_HSVOL_CTRL, 0x0 },
131 { NAU8825_REG_DACL_CTRL, 0x02cf },
132 { NAU8825_REG_DACR_CTRL, 0x00cf },
133 { NAU8825_REG_ADC_DRC_KNEE_IP12, 0x1486 },
134 { NAU8825_REG_ADC_DRC_KNEE_IP34, 0x0f12 },
135 { NAU8825_REG_ADC_DRC_SLOPES, 0x25ff },
136 { NAU8825_REG_ADC_DRC_ATKDCY, 0x3457 },
137 { NAU8825_REG_DAC_DRC_KNEE_IP12, 0x1486 },
138 { NAU8825_REG_DAC_DRC_KNEE_IP34, 0x0f12 },
139 { NAU8825_REG_DAC_DRC_SLOPES, 0x25f9 },
140 { NAU8825_REG_DAC_DRC_ATKDCY, 0x3457 },
141 { NAU8825_REG_IMM_MODE_CTRL, 0x0 },
142 { NAU8825_REG_CLASSG_CTRL, 0x0 },
143 { NAU8825_REG_OPT_EFUSE_CTRL, 0x0 },
144 { NAU8825_REG_MISC_CTRL, 0x0 },
145 { NAU8825_REG_BIAS_ADJ, 0x0 },
146 { NAU8825_REG_TRIM_SETTINGS, 0x0 },
147 { NAU8825_REG_ANALOG_CONTROL_1, 0x0 },
148 { NAU8825_REG_ANALOG_CONTROL_2, 0x0 },
149 { NAU8825_REG_ANALOG_ADC_1, 0x0011 },
150 { NAU8825_REG_ANALOG_ADC_2, 0x0020 },
151 { NAU8825_REG_RDAC, 0x0008 },
152 { NAU8825_REG_MIC_BIAS, 0x0006 },
153 { NAU8825_REG_BOOST, 0x0 },
154 { NAU8825_REG_FEPGA, 0x0 },
155 { NAU8825_REG_POWER_UP_CONTROL, 0x0 },
156 { NAU8825_REG_CHARGE_PUMP, 0x0 },
157};
158
159static bool nau8825_readable_reg(struct device *dev, unsigned int reg)
160{
161 switch (reg) {
John Hsu45d5eb32016-03-11 17:33:58 -0800162 case NAU8825_REG_ENA_CTRL ... NAU8825_REG_FLL_VCO_RSV:
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700163 case NAU8825_REG_HSD_CTRL ... NAU8825_REG_JACK_DET_CTRL:
164 case NAU8825_REG_INTERRUPT_MASK ... NAU8825_REG_KEYDET_CTRL:
165 case NAU8825_REG_VDET_THRESHOLD_1 ... NAU8825_REG_DACR_CTRL:
166 case NAU8825_REG_ADC_DRC_KNEE_IP12 ... NAU8825_REG_ADC_DRC_ATKDCY:
167 case NAU8825_REG_DAC_DRC_KNEE_IP12 ... NAU8825_REG_DAC_DRC_ATKDCY:
168 case NAU8825_REG_IMM_MODE_CTRL ... NAU8825_REG_IMM_RMS_R:
169 case NAU8825_REG_CLASSG_CTRL ... NAU8825_REG_OPT_EFUSE_CTRL:
170 case NAU8825_REG_MISC_CTRL:
171 case NAU8825_REG_I2C_DEVICE_ID ... NAU8825_REG_SARDOUT_RAM_STATUS:
172 case NAU8825_REG_BIAS_ADJ:
173 case NAU8825_REG_TRIM_SETTINGS ... NAU8825_REG_ANALOG_CONTROL_2:
174 case NAU8825_REG_ANALOG_ADC_1 ... NAU8825_REG_MIC_BIAS:
175 case NAU8825_REG_BOOST ... NAU8825_REG_FEPGA:
176 case NAU8825_REG_POWER_UP_CONTROL ... NAU8825_REG_GENERAL_STATUS:
177 return true;
178 default:
179 return false;
180 }
181
182}
183
184static bool nau8825_writeable_reg(struct device *dev, unsigned int reg)
185{
186 switch (reg) {
John Hsu45d5eb32016-03-11 17:33:58 -0800187 case NAU8825_REG_RESET ... NAU8825_REG_FLL_VCO_RSV:
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700188 case NAU8825_REG_HSD_CTRL ... NAU8825_REG_JACK_DET_CTRL:
189 case NAU8825_REG_INTERRUPT_MASK:
190 case NAU8825_REG_INT_CLR_KEY_STATUS ... NAU8825_REG_KEYDET_CTRL:
191 case NAU8825_REG_VDET_THRESHOLD_1 ... NAU8825_REG_DACR_CTRL:
192 case NAU8825_REG_ADC_DRC_KNEE_IP12 ... NAU8825_REG_ADC_DRC_ATKDCY:
193 case NAU8825_REG_DAC_DRC_KNEE_IP12 ... NAU8825_REG_DAC_DRC_ATKDCY:
194 case NAU8825_REG_IMM_MODE_CTRL:
195 case NAU8825_REG_CLASSG_CTRL ... NAU8825_REG_OPT_EFUSE_CTRL:
196 case NAU8825_REG_MISC_CTRL:
197 case NAU8825_REG_BIAS_ADJ:
198 case NAU8825_REG_TRIM_SETTINGS ... NAU8825_REG_ANALOG_CONTROL_2:
199 case NAU8825_REG_ANALOG_ADC_1 ... NAU8825_REG_MIC_BIAS:
200 case NAU8825_REG_BOOST ... NAU8825_REG_FEPGA:
201 case NAU8825_REG_POWER_UP_CONTROL ... NAU8825_REG_CHARGE_PUMP:
202 return true;
203 default:
204 return false;
205 }
206}
207
208static bool nau8825_volatile_reg(struct device *dev, unsigned int reg)
209{
210 switch (reg) {
211 case NAU8825_REG_RESET:
212 case NAU8825_REG_IRQ_STATUS:
213 case NAU8825_REG_INT_CLR_KEY_STATUS:
214 case NAU8825_REG_IMM_RMS_L:
215 case NAU8825_REG_IMM_RMS_R:
216 case NAU8825_REG_I2C_DEVICE_ID:
217 case NAU8825_REG_SARDOUT_RAM_STATUS:
218 case NAU8825_REG_CHARGE_PUMP_INPUT_READ:
219 case NAU8825_REG_GENERAL_STATUS:
220 return true;
221 default:
222 return false;
223 }
224}
225
John Hsueeef16a2016-03-22 11:57:20 +0800226static int nau8825_adc_event(struct snd_soc_dapm_widget *w,
227 struct snd_kcontrol *kcontrol, int event)
228{
229 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
230 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
231
232 switch (event) {
233 case SND_SOC_DAPM_POST_PMU:
234 regmap_update_bits(nau8825->regmap, NAU8825_REG_ENA_CTRL,
235 NAU8825_ENABLE_ADC, NAU8825_ENABLE_ADC);
236 break;
237 case SND_SOC_DAPM_POST_PMD:
238 if (!nau8825->irq)
239 regmap_update_bits(nau8825->regmap,
240 NAU8825_REG_ENA_CTRL, NAU8825_ENABLE_ADC, 0);
241 break;
242 default:
243 return -EINVAL;
244 }
245
246 return 0;
247}
248
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700249static int nau8825_pump_event(struct snd_soc_dapm_widget *w,
250 struct snd_kcontrol *kcontrol, int event)
251{
John Hsu45d5eb32016-03-11 17:33:58 -0800252 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
253 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
254
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700255 switch (event) {
256 case SND_SOC_DAPM_POST_PMU:
257 /* Prevent startup click by letting charge pump to ramp up */
258 msleep(10);
John Hsu45d5eb32016-03-11 17:33:58 -0800259 regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
260 NAU8825_JAMNODCLOW, NAU8825_JAMNODCLOW);
261 break;
262 case SND_SOC_DAPM_PRE_PMD:
263 regmap_update_bits(nau8825->regmap, NAU8825_REG_CHARGE_PUMP,
264 NAU8825_JAMNODCLOW, 0);
265 break;
266 default:
267 return -EINVAL;
268 }
269
270 return 0;
271}
272
273static int nau8825_output_dac_event(struct snd_soc_dapm_widget *w,
274 struct snd_kcontrol *kcontrol, int event)
275{
276 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
277 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
278
279 switch (event) {
280 case SND_SOC_DAPM_PRE_PMU:
281 /* Disables the TESTDAC to let DAC signal pass through. */
282 regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
283 NAU8825_BIAS_TESTDAC_EN, 0);
284 break;
285 case SND_SOC_DAPM_POST_PMD:
286 regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
287 NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN);
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700288 break;
289 default:
290 return -EINVAL;
291 }
292
293 return 0;
294}
295
296static const char * const nau8825_adc_decimation[] = {
297 "32", "64", "128", "256"
298};
299
300static const struct soc_enum nau8825_adc_decimation_enum =
301 SOC_ENUM_SINGLE(NAU8825_REG_ADC_RATE, NAU8825_ADC_SYNC_DOWN_SFT,
302 ARRAY_SIZE(nau8825_adc_decimation), nau8825_adc_decimation);
303
304static const char * const nau8825_dac_oversampl[] = {
305 "64", "256", "128", "", "32"
306};
307
308static const struct soc_enum nau8825_dac_oversampl_enum =
309 SOC_ENUM_SINGLE(NAU8825_REG_DAC_CTRL1, NAU8825_DAC_OVERSAMPLE_SFT,
310 ARRAY_SIZE(nau8825_dac_oversampl), nau8825_dac_oversampl);
311
312static const DECLARE_TLV_DB_MINMAX_MUTE(adc_vol_tlv, -10300, 2400);
313static const DECLARE_TLV_DB_MINMAX_MUTE(sidetone_vol_tlv, -4200, 0);
314static const DECLARE_TLV_DB_MINMAX(dac_vol_tlv, -5400, 0);
315static const DECLARE_TLV_DB_MINMAX(fepga_gain_tlv, -100, 3600);
316static const DECLARE_TLV_DB_MINMAX_MUTE(crosstalk_vol_tlv, -9600, 2400);
317
318static const struct snd_kcontrol_new nau8825_controls[] = {
319 SOC_SINGLE_TLV("Mic Volume", NAU8825_REG_ADC_DGAIN_CTRL,
320 0, 0xff, 0, adc_vol_tlv),
321 SOC_DOUBLE_TLV("Headphone Bypass Volume", NAU8825_REG_ADC_DGAIN_CTRL,
322 12, 8, 0x0f, 0, sidetone_vol_tlv),
323 SOC_DOUBLE_TLV("Headphone Volume", NAU8825_REG_HSVOL_CTRL,
324 6, 0, 0x3f, 1, dac_vol_tlv),
325 SOC_SINGLE_TLV("Frontend PGA Volume", NAU8825_REG_POWER_UP_CONTROL,
326 8, 37, 0, fepga_gain_tlv),
327 SOC_DOUBLE_TLV("Headphone Crosstalk Volume", NAU8825_REG_DAC_DGAIN_CTRL,
328 0, 8, 0xff, 0, crosstalk_vol_tlv),
329
330 SOC_ENUM("ADC Decimation Rate", nau8825_adc_decimation_enum),
331 SOC_ENUM("DAC Oversampling Rate", nau8825_dac_oversampl_enum),
332};
333
334/* DAC Mux 0x33[9] and 0x34[9] */
335static const char * const nau8825_dac_src[] = {
336 "DACL", "DACR",
337};
338
339static SOC_ENUM_SINGLE_DECL(
340 nau8825_dacl_enum, NAU8825_REG_DACL_CTRL,
341 NAU8825_DACL_CH_SEL_SFT, nau8825_dac_src);
342
343static SOC_ENUM_SINGLE_DECL(
344 nau8825_dacr_enum, NAU8825_REG_DACR_CTRL,
345 NAU8825_DACR_CH_SEL_SFT, nau8825_dac_src);
346
347static const struct snd_kcontrol_new nau8825_dacl_mux =
348 SOC_DAPM_ENUM("DACL Source", nau8825_dacl_enum);
349
350static const struct snd_kcontrol_new nau8825_dacr_mux =
351 SOC_DAPM_ENUM("DACR Source", nau8825_dacr_enum);
352
353
354static const struct snd_soc_dapm_widget nau8825_dapm_widgets[] = {
355 SND_SOC_DAPM_AIF_OUT("AIFTX", "Capture", 0, NAU8825_REG_I2S_PCM_CTRL2,
356 15, 1),
357
358 SND_SOC_DAPM_INPUT("MIC"),
359 SND_SOC_DAPM_MICBIAS("MICBIAS", NAU8825_REG_MIC_BIAS, 8, 0),
360
361 SND_SOC_DAPM_PGA("Frontend PGA", NAU8825_REG_POWER_UP_CONTROL, 14, 0,
362 NULL, 0),
363
John Hsueeef16a2016-03-22 11:57:20 +0800364 SND_SOC_DAPM_ADC_E("ADC", NULL, SND_SOC_NOPM, 0, 0,
365 nau8825_adc_event, SND_SOC_DAPM_POST_PMU |
366 SND_SOC_DAPM_POST_PMD),
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700367 SND_SOC_DAPM_SUPPLY("ADC Clock", NAU8825_REG_ENA_CTRL, 7, 0, NULL, 0),
368 SND_SOC_DAPM_SUPPLY("ADC Power", NAU8825_REG_ANALOG_ADC_2, 6, 0, NULL,
369 0),
370
Ben Zhange6cee902016-03-25 16:10:39 -0700371 /* ADC for button press detection. A dapm supply widget is used to
372 * prevent dapm_power_widgets keeping the codec at SND_SOC_BIAS_ON
373 * during suspend.
374 */
375 SND_SOC_DAPM_SUPPLY("SAR", NAU8825_REG_SAR_CTRL,
376 NAU8825_SAR_ADC_EN_SFT, 0, NULL, 0),
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700377
John Hsu45d5eb32016-03-11 17:33:58 -0800378 SND_SOC_DAPM_PGA_S("ADACL", 2, NAU8825_REG_RDAC, 12, 0, NULL, 0),
379 SND_SOC_DAPM_PGA_S("ADACR", 2, NAU8825_REG_RDAC, 13, 0, NULL, 0),
380 SND_SOC_DAPM_PGA_S("ADACL Clock", 3, NAU8825_REG_RDAC, 8, 0, NULL, 0),
381 SND_SOC_DAPM_PGA_S("ADACR Clock", 3, NAU8825_REG_RDAC, 9, 0, NULL, 0),
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700382
383 SND_SOC_DAPM_DAC("DDACR", NULL, NAU8825_REG_ENA_CTRL,
384 NAU8825_ENABLE_DACR_SFT, 0),
385 SND_SOC_DAPM_DAC("DDACL", NULL, NAU8825_REG_ENA_CTRL,
386 NAU8825_ENABLE_DACL_SFT, 0),
387 SND_SOC_DAPM_SUPPLY("DDAC Clock", NAU8825_REG_ENA_CTRL, 6, 0, NULL, 0),
388
389 SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &nau8825_dacl_mux),
390 SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &nau8825_dacr_mux),
391
John Hsu45d5eb32016-03-11 17:33:58 -0800392 SND_SOC_DAPM_PGA_S("HP amp L", 0,
393 NAU8825_REG_CLASSG_CTRL, 1, 0, NULL, 0),
394 SND_SOC_DAPM_PGA_S("HP amp R", 0,
395 NAU8825_REG_CLASSG_CTRL, 2, 0, NULL, 0),
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700396
John Hsu45d5eb32016-03-11 17:33:58 -0800397 SND_SOC_DAPM_PGA_S("Charge Pump", 1, NAU8825_REG_CHARGE_PUMP, 5, 0,
398 nau8825_pump_event, SND_SOC_DAPM_POST_PMU |
399 SND_SOC_DAPM_PRE_PMD),
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700400
John Hsu45d5eb32016-03-11 17:33:58 -0800401 SND_SOC_DAPM_PGA_S("Output Driver R Stage 1", 4,
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700402 NAU8825_REG_POWER_UP_CONTROL, 5, 0, NULL, 0),
John Hsu45d5eb32016-03-11 17:33:58 -0800403 SND_SOC_DAPM_PGA_S("Output Driver L Stage 1", 4,
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700404 NAU8825_REG_POWER_UP_CONTROL, 4, 0, NULL, 0),
John Hsu45d5eb32016-03-11 17:33:58 -0800405 SND_SOC_DAPM_PGA_S("Output Driver R Stage 2", 5,
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700406 NAU8825_REG_POWER_UP_CONTROL, 3, 0, NULL, 0),
John Hsu45d5eb32016-03-11 17:33:58 -0800407 SND_SOC_DAPM_PGA_S("Output Driver L Stage 2", 5,
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700408 NAU8825_REG_POWER_UP_CONTROL, 2, 0, NULL, 0),
John Hsu45d5eb32016-03-11 17:33:58 -0800409 SND_SOC_DAPM_PGA_S("Output Driver R Stage 3", 6,
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700410 NAU8825_REG_POWER_UP_CONTROL, 1, 0, NULL, 0),
John Hsu45d5eb32016-03-11 17:33:58 -0800411 SND_SOC_DAPM_PGA_S("Output Driver L Stage 3", 6,
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700412 NAU8825_REG_POWER_UP_CONTROL, 0, 0, NULL, 0),
413
John Hsu45d5eb32016-03-11 17:33:58 -0800414 SND_SOC_DAPM_PGA_S("Output DACL", 7,
415 NAU8825_REG_CHARGE_PUMP, 8, 1, nau8825_output_dac_event,
416 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
417 SND_SOC_DAPM_PGA_S("Output DACR", 7,
418 NAU8825_REG_CHARGE_PUMP, 9, 1, nau8825_output_dac_event,
419 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
420
421 /* HPOL/R are ungrounded by disabling 16 Ohm pull-downs on playback */
422 SND_SOC_DAPM_PGA_S("HPOL Pulldown", 8,
423 NAU8825_REG_HSD_CTRL, 0, 1, NULL, 0),
424 SND_SOC_DAPM_PGA_S("HPOR Pulldown", 8,
425 NAU8825_REG_HSD_CTRL, 1, 1, NULL, 0),
426
427 /* High current HPOL/R boost driver */
428 SND_SOC_DAPM_PGA_S("HP Boost Driver", 9,
429 NAU8825_REG_BOOST, 9, 1, NULL, 0),
430
431 /* Class G operation control*/
432 SND_SOC_DAPM_PGA_S("Class G", 10,
433 NAU8825_REG_CLASSG_CTRL, 0, 0, NULL, 0),
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700434
435 SND_SOC_DAPM_OUTPUT("HPOL"),
436 SND_SOC_DAPM_OUTPUT("HPOR"),
437};
438
439static const struct snd_soc_dapm_route nau8825_dapm_routes[] = {
440 {"Frontend PGA", NULL, "MIC"},
441 {"ADC", NULL, "Frontend PGA"},
442 {"ADC", NULL, "ADC Clock"},
443 {"ADC", NULL, "ADC Power"},
444 {"AIFTX", NULL, "ADC"},
445
446 {"DDACL", NULL, "Playback"},
447 {"DDACR", NULL, "Playback"},
448 {"DDACL", NULL, "DDAC Clock"},
449 {"DDACR", NULL, "DDAC Clock"},
450 {"DACL Mux", "DACL", "DDACL"},
451 {"DACL Mux", "DACR", "DDACR"},
452 {"DACR Mux", "DACL", "DDACL"},
453 {"DACR Mux", "DACR", "DDACR"},
454 {"HP amp L", NULL, "DACL Mux"},
455 {"HP amp R", NULL, "DACR Mux"},
John Hsu45d5eb32016-03-11 17:33:58 -0800456 {"Charge Pump", NULL, "HP amp L"},
457 {"Charge Pump", NULL, "HP amp R"},
458 {"ADACL", NULL, "Charge Pump"},
459 {"ADACR", NULL, "Charge Pump"},
460 {"ADACL Clock", NULL, "ADACL"},
461 {"ADACR Clock", NULL, "ADACR"},
462 {"Output Driver L Stage 1", NULL, "ADACL Clock"},
463 {"Output Driver R Stage 1", NULL, "ADACR Clock"},
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700464 {"Output Driver L Stage 2", NULL, "Output Driver L Stage 1"},
465 {"Output Driver R Stage 2", NULL, "Output Driver R Stage 1"},
466 {"Output Driver L Stage 3", NULL, "Output Driver L Stage 2"},
467 {"Output Driver R Stage 3", NULL, "Output Driver R Stage 2"},
468 {"Output DACL", NULL, "Output Driver L Stage 3"},
469 {"Output DACR", NULL, "Output Driver R Stage 3"},
John Hsu45d5eb32016-03-11 17:33:58 -0800470 {"HPOL Pulldown", NULL, "Output DACL"},
471 {"HPOR Pulldown", NULL, "Output DACR"},
472 {"HP Boost Driver", NULL, "HPOL Pulldown"},
473 {"HP Boost Driver", NULL, "HPOR Pulldown"},
474 {"Class G", NULL, "HP Boost Driver"},
475 {"HPOL", NULL, "Class G"},
476 {"HPOR", NULL, "Class G"},
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700477};
478
479static int nau8825_hw_params(struct snd_pcm_substream *substream,
480 struct snd_pcm_hw_params *params,
481 struct snd_soc_dai *dai)
482{
483 struct snd_soc_codec *codec = dai->codec;
484 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
485 unsigned int val_len = 0;
486
487 switch (params_width(params)) {
488 case 16:
489 val_len |= NAU8825_I2S_DL_16;
490 break;
491 case 20:
492 val_len |= NAU8825_I2S_DL_20;
493 break;
494 case 24:
495 val_len |= NAU8825_I2S_DL_24;
496 break;
497 case 32:
498 val_len |= NAU8825_I2S_DL_32;
499 break;
500 default:
501 return -EINVAL;
502 }
503
504 regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1,
505 NAU8825_I2S_DL_MASK, val_len);
506
507 return 0;
508}
509
510static int nau8825_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
511{
512 struct snd_soc_codec *codec = codec_dai->codec;
513 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
514 unsigned int ctrl1_val = 0, ctrl2_val = 0;
515
516 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
517 case SND_SOC_DAIFMT_CBM_CFM:
518 ctrl2_val |= NAU8825_I2S_MS_MASTER;
519 break;
520 case SND_SOC_DAIFMT_CBS_CFS:
521 break;
522 default:
523 return -EINVAL;
524 }
525
526 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
527 case SND_SOC_DAIFMT_NB_NF:
528 break;
529 case SND_SOC_DAIFMT_IB_NF:
530 ctrl1_val |= NAU8825_I2S_BP_INV;
531 break;
532 default:
533 return -EINVAL;
534 }
535
536 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
537 case SND_SOC_DAIFMT_I2S:
538 ctrl1_val |= NAU8825_I2S_DF_I2S;
539 break;
540 case SND_SOC_DAIFMT_LEFT_J:
541 ctrl1_val |= NAU8825_I2S_DF_LEFT;
542 break;
543 case SND_SOC_DAIFMT_RIGHT_J:
544 ctrl1_val |= NAU8825_I2S_DF_RIGTH;
545 break;
546 case SND_SOC_DAIFMT_DSP_A:
547 ctrl1_val |= NAU8825_I2S_DF_PCM_AB;
548 break;
549 case SND_SOC_DAIFMT_DSP_B:
550 ctrl1_val |= NAU8825_I2S_DF_PCM_AB;
551 ctrl1_val |= NAU8825_I2S_PCMB_EN;
552 break;
553 default:
554 return -EINVAL;
555 }
556
557 regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1,
558 NAU8825_I2S_DL_MASK | NAU8825_I2S_DF_MASK |
559 NAU8825_I2S_BP_MASK | NAU8825_I2S_PCMB_MASK,
560 ctrl1_val);
561 regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
562 NAU8825_I2S_MS_MASK, ctrl2_val);
563
564 return 0;
565}
566
567static const struct snd_soc_dai_ops nau8825_dai_ops = {
568 .hw_params = nau8825_hw_params,
569 .set_fmt = nau8825_set_dai_fmt,
570};
571
572#define NAU8825_RATES SNDRV_PCM_RATE_8000_192000
573#define NAU8825_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
574 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
575
576static struct snd_soc_dai_driver nau8825_dai = {
577 .name = "nau8825-hifi",
578 .playback = {
579 .stream_name = "Playback",
580 .channels_min = 1,
581 .channels_max = 2,
582 .rates = NAU8825_RATES,
583 .formats = NAU8825_FORMATS,
584 },
585 .capture = {
586 .stream_name = "Capture",
587 .channels_min = 1,
588 .channels_max = 1,
589 .rates = NAU8825_RATES,
590 .formats = NAU8825_FORMATS,
591 },
592 .ops = &nau8825_dai_ops,
593};
594
595/**
596 * nau8825_enable_jack_detect - Specify a jack for event reporting
597 *
598 * @component: component to register the jack with
599 * @jack: jack to use to report headset and button events on
600 *
601 * After this function has been called the headset insert/remove and button
602 * events will be routed to the given jack. Jack can be null to stop
603 * reporting.
604 */
605int nau8825_enable_jack_detect(struct snd_soc_codec *codec,
606 struct snd_soc_jack *jack)
607{
608 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
609 struct regmap *regmap = nau8825->regmap;
610
611 nau8825->jack = jack;
612
613 /* Ground HP Outputs[1:0], needed for headset auto detection
614 * Enable Automatic Mic/Gnd switching reading on insert interrupt[6]
615 */
616 regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL,
617 NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L,
618 NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L);
619
620 regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
621 NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_EJECT_EN, 0);
622
623 return 0;
624}
625EXPORT_SYMBOL_GPL(nau8825_enable_jack_detect);
626
627
628static bool nau8825_is_jack_inserted(struct regmap *regmap)
629{
630 int status;
631
632 regmap_read(regmap, NAU8825_REG_I2C_DEVICE_ID, &status);
633 return !(status & NAU8825_GPIO2JD1);
634}
635
636static void nau8825_restart_jack_detection(struct regmap *regmap)
637{
Ben Zhange6cee902016-03-25 16:10:39 -0700638 /* Chip needs one FSCLK cycle in order to generate interrupts,
639 * as we cannot guarantee one will be provided by the system. Turning
640 * master mode on then off enables us to generate that FSCLK cycle
641 * with a minimum of contention on the clock bus.
642 */
643 regmap_update_bits(regmap, NAU8825_REG_I2S_PCM_CTRL2,
644 NAU8825_I2S_MS_MASK, NAU8825_I2S_MS_MASTER);
645 regmap_update_bits(regmap, NAU8825_REG_I2S_PCM_CTRL2,
646 NAU8825_I2S_MS_MASK, NAU8825_I2S_MS_SLAVE);
647
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700648 /* this will restart the entire jack detection process including MIC/GND
649 * switching and create interrupts. We have to go from 0 to 1 and back
650 * to 0 to restart.
651 */
652 regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
653 NAU8825_JACK_DET_RESTART, NAU8825_JACK_DET_RESTART);
654 regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
655 NAU8825_JACK_DET_RESTART, 0);
656}
657
658static void nau8825_eject_jack(struct nau8825 *nau8825)
659{
660 struct snd_soc_dapm_context *dapm = nau8825->dapm;
661 struct regmap *regmap = nau8825->regmap;
662
663 snd_soc_dapm_disable_pin(dapm, "SAR");
664 snd_soc_dapm_disable_pin(dapm, "MICBIAS");
665 /* Detach 2kOhm Resistors from MICBIAS to MICGND1/2 */
666 regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
667 NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2, 0);
668 /* ground HPL/HPR, MICGRND1/2 */
669 regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 0xf, 0xf);
670
671 snd_soc_dapm_sync(dapm);
672}
673
674static int nau8825_button_decode(int value)
675{
676 int buttons = 0;
677
678 /* The chip supports up to 8 buttons, but ALSA defines only 6 buttons */
679 if (value & BIT(0))
680 buttons |= SND_JACK_BTN_0;
681 if (value & BIT(1))
682 buttons |= SND_JACK_BTN_1;
683 if (value & BIT(2))
684 buttons |= SND_JACK_BTN_2;
685 if (value & BIT(3))
686 buttons |= SND_JACK_BTN_3;
687 if (value & BIT(4))
688 buttons |= SND_JACK_BTN_4;
689 if (value & BIT(5))
690 buttons |= SND_JACK_BTN_5;
691
692 return buttons;
693}
694
695static int nau8825_jack_insert(struct nau8825 *nau8825)
696{
697 struct regmap *regmap = nau8825->regmap;
698 struct snd_soc_dapm_context *dapm = nau8825->dapm;
699 int jack_status_reg, mic_detected;
700 int type = 0;
701
702 regmap_read(regmap, NAU8825_REG_GENERAL_STATUS, &jack_status_reg);
703 mic_detected = (jack_status_reg >> 10) & 3;
704
705 switch (mic_detected) {
706 case 0:
707 /* no mic */
708 type = SND_JACK_HEADPHONE;
709 break;
710 case 1:
711 dev_dbg(nau8825->dev, "OMTP (micgnd1) mic connected\n");
712 type = SND_JACK_HEADSET;
713
714 /* Unground MICGND1 */
715 regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 3 << 2,
716 1 << 2);
717 /* Attach 2kOhm Resistor from MICBIAS to MICGND1 */
718 regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
719 NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2,
720 NAU8825_MICBIAS_JKR2);
721 /* Attach SARADC to MICGND1 */
722 regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
723 NAU8825_SAR_INPUT_MASK,
724 NAU8825_SAR_INPUT_JKR2);
725
726 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
727 snd_soc_dapm_force_enable_pin(dapm, "SAR");
728 snd_soc_dapm_sync(dapm);
729 break;
730 case 2:
731 case 3:
732 dev_dbg(nau8825->dev, "CTIA (micgnd2) mic connected\n");
733 type = SND_JACK_HEADSET;
734
735 /* Unground MICGND2 */
736 regmap_update_bits(regmap, NAU8825_REG_HSD_CTRL, 3 << 2,
737 2 << 2);
738 /* Attach 2kOhm Resistor from MICBIAS to MICGND2 */
739 regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
740 NAU8825_MICBIAS_JKSLV | NAU8825_MICBIAS_JKR2,
741 NAU8825_MICBIAS_JKSLV);
742 /* Attach SARADC to MICGND2 */
743 regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
744 NAU8825_SAR_INPUT_MASK,
745 NAU8825_SAR_INPUT_JKSLV);
746
747 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
748 snd_soc_dapm_force_enable_pin(dapm, "SAR");
749 snd_soc_dapm_sync(dapm);
750 break;
751 }
752
John Hsu45d5eb32016-03-11 17:33:58 -0800753 /* Leaving HPOL/R grounded after jack insert by default. They will be
754 * ungrounded as part of the widget power up sequence at the beginning
755 * of playback to reduce pop.
756 */
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700757 return type;
758}
759
760#define NAU8825_BUTTONS (SND_JACK_BTN_0 | SND_JACK_BTN_1 | \
761 SND_JACK_BTN_2 | SND_JACK_BTN_3)
762
763static irqreturn_t nau8825_interrupt(int irq, void *data)
764{
765 struct nau8825 *nau8825 = (struct nau8825 *)data;
766 struct regmap *regmap = nau8825->regmap;
767 int active_irq, clear_irq = 0, event = 0, event_mask = 0;
768
Ben Zhange6cee902016-03-25 16:10:39 -0700769 if (regmap_read(regmap, NAU8825_REG_IRQ_STATUS, &active_irq)) {
770 dev_err(nau8825->dev, "failed to read irq status\n");
771 return IRQ_NONE;
772 }
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700773
774 if ((active_irq & NAU8825_JACK_EJECTION_IRQ_MASK) ==
775 NAU8825_JACK_EJECTION_DETECTED) {
776
777 nau8825_eject_jack(nau8825);
778 event_mask |= SND_JACK_HEADSET;
779 clear_irq = NAU8825_JACK_EJECTION_IRQ_MASK;
780 } else if (active_irq & NAU8825_KEY_SHORT_PRESS_IRQ) {
781 int key_status;
782
783 regmap_read(regmap, NAU8825_REG_INT_CLR_KEY_STATUS,
784 &key_status);
785
786 /* upper 8 bits of the register are for short pressed keys,
787 * lower 8 bits - for long pressed buttons
788 */
789 nau8825->button_pressed = nau8825_button_decode(
790 key_status >> 8);
791
792 event |= nau8825->button_pressed;
793 event_mask |= NAU8825_BUTTONS;
794 clear_irq = NAU8825_KEY_SHORT_PRESS_IRQ;
795 } else if (active_irq & NAU8825_KEY_RELEASE_IRQ) {
796 event_mask = NAU8825_BUTTONS;
797 clear_irq = NAU8825_KEY_RELEASE_IRQ;
798 } else if (active_irq & NAU8825_HEADSET_COMPLETION_IRQ) {
799 if (nau8825_is_jack_inserted(regmap)) {
800 event |= nau8825_jack_insert(nau8825);
801 } else {
802 dev_warn(nau8825->dev, "Headset completion IRQ fired but no headset connected\n");
803 nau8825_eject_jack(nau8825);
804 }
805
806 event_mask |= SND_JACK_HEADSET;
807 clear_irq = NAU8825_HEADSET_COMPLETION_IRQ;
808 }
809
810 if (!clear_irq)
811 clear_irq = active_irq;
812 /* clears the rightmost interruption */
813 regmap_write(regmap, NAU8825_REG_INT_CLR_KEY_STATUS, clear_irq);
814
815 if (event_mask)
816 snd_soc_jack_report(nau8825->jack, event, event_mask);
817
818 return IRQ_HANDLED;
819}
820
821static void nau8825_setup_buttons(struct nau8825 *nau8825)
822{
823 struct regmap *regmap = nau8825->regmap;
824
825 regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
826 NAU8825_SAR_TRACKING_GAIN_MASK,
827 nau8825->sar_voltage << NAU8825_SAR_TRACKING_GAIN_SFT);
828 regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
829 NAU8825_SAR_COMPARE_TIME_MASK,
830 nau8825->sar_compare_time << NAU8825_SAR_COMPARE_TIME_SFT);
831 regmap_update_bits(regmap, NAU8825_REG_SAR_CTRL,
832 NAU8825_SAR_SAMPLING_TIME_MASK,
833 nau8825->sar_sampling_time << NAU8825_SAR_SAMPLING_TIME_SFT);
834
835 regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
836 NAU8825_KEYDET_LEVELS_NR_MASK,
837 (nau8825->sar_threshold_num - 1) << NAU8825_KEYDET_LEVELS_NR_SFT);
838 regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
839 NAU8825_KEYDET_HYSTERESIS_MASK,
840 nau8825->sar_hysteresis << NAU8825_KEYDET_HYSTERESIS_SFT);
841 regmap_update_bits(regmap, NAU8825_REG_KEYDET_CTRL,
842 NAU8825_KEYDET_SHORTKEY_DEBOUNCE_MASK,
843 nau8825->key_debounce << NAU8825_KEYDET_SHORTKEY_DEBOUNCE_SFT);
844
845 regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_1,
846 (nau8825->sar_threshold[0] << 8) | nau8825->sar_threshold[1]);
847 regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_2,
848 (nau8825->sar_threshold[2] << 8) | nau8825->sar_threshold[3]);
849 regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_3,
850 (nau8825->sar_threshold[4] << 8) | nau8825->sar_threshold[5]);
851 regmap_write(regmap, NAU8825_REG_VDET_THRESHOLD_4,
852 (nau8825->sar_threshold[6] << 8) | nau8825->sar_threshold[7]);
853
854 /* Enable short press and release interruptions */
855 regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
856 NAU8825_IRQ_KEY_SHORT_PRESS_EN | NAU8825_IRQ_KEY_RELEASE_EN,
857 0);
858}
859
860static void nau8825_init_regs(struct nau8825 *nau8825)
861{
862 struct regmap *regmap = nau8825->regmap;
863
John Hsu45d5eb32016-03-11 17:33:58 -0800864 /* Latch IIC LSB value */
865 regmap_write(regmap, NAU8825_REG_IIC_ADDR_SET, 0x0001);
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700866 /* Enable Bias/Vmid */
867 regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
868 NAU8825_BIAS_VMID, NAU8825_BIAS_VMID);
869 regmap_update_bits(nau8825->regmap, NAU8825_REG_BOOST,
870 NAU8825_GLOBAL_BIAS_EN, NAU8825_GLOBAL_BIAS_EN);
871
872 /* VMID Tieoff */
873 regmap_update_bits(regmap, NAU8825_REG_BIAS_ADJ,
874 NAU8825_BIAS_VMID_SEL_MASK,
875 nau8825->vref_impedance << NAU8825_BIAS_VMID_SEL_SFT);
876 /* Disable Boost Driver, Automatic Short circuit protection enable */
877 regmap_update_bits(regmap, NAU8825_REG_BOOST,
John Hsu45d5eb32016-03-11 17:33:58 -0800878 NAU8825_PRECHARGE_DIS | NAU8825_HP_BOOST_DIS |
879 NAU8825_HP_BOOST_G_DIS | NAU8825_SHORT_SHUTDOWN_EN,
880 NAU8825_PRECHARGE_DIS | NAU8825_HP_BOOST_DIS |
881 NAU8825_HP_BOOST_G_DIS | NAU8825_SHORT_SHUTDOWN_EN);
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700882
883 regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
884 NAU8825_JKDET_OUTPUT_EN,
885 nau8825->jkdet_enable ? 0 : NAU8825_JKDET_OUTPUT_EN);
886 regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
887 NAU8825_JKDET_PULL_EN,
888 nau8825->jkdet_pull_enable ? 0 : NAU8825_JKDET_PULL_EN);
889 regmap_update_bits(regmap, NAU8825_REG_GPIO12_CTRL,
890 NAU8825_JKDET_PULL_UP,
891 nau8825->jkdet_pull_up ? NAU8825_JKDET_PULL_UP : 0);
892 regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
893 NAU8825_JACK_POLARITY,
894 /* jkdet_polarity - 1 is for active-low */
895 nau8825->jkdet_polarity ? 0 : NAU8825_JACK_POLARITY);
896
897 regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
898 NAU8825_JACK_INSERT_DEBOUNCE_MASK,
899 nau8825->jack_insert_debounce << NAU8825_JACK_INSERT_DEBOUNCE_SFT);
900 regmap_update_bits(regmap, NAU8825_REG_JACK_DET_CTRL,
901 NAU8825_JACK_EJECT_DEBOUNCE_MASK,
902 nau8825->jack_eject_debounce << NAU8825_JACK_EJECT_DEBOUNCE_SFT);
903
904 /* Mask unneeded IRQs: 1 - disable, 0 - enable */
905 regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK, 0x7ff, 0x7ff);
906
907 regmap_update_bits(regmap, NAU8825_REG_MIC_BIAS,
908 NAU8825_MICBIAS_VOLTAGE_MASK, nau8825->micbias_voltage);
909
910 if (nau8825->sar_threshold_num)
911 nau8825_setup_buttons(nau8825);
912
913 /* Default oversampling/decimations settings are unusable
914 * (audible hiss). Set it to something better.
915 */
916 regmap_update_bits(regmap, NAU8825_REG_ADC_RATE,
917 NAU8825_ADC_SYNC_DOWN_MASK, NAU8825_ADC_SYNC_DOWN_128);
918 regmap_update_bits(regmap, NAU8825_REG_DAC_CTRL1,
919 NAU8825_DAC_OVERSAMPLE_MASK, NAU8825_DAC_OVERSAMPLE_128);
John Hsu45d5eb32016-03-11 17:33:58 -0800920 /* Disable DACR/L power */
921 regmap_update_bits(regmap, NAU8825_REG_CHARGE_PUMP,
922 NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL,
923 NAU8825_POWER_DOWN_DACR | NAU8825_POWER_DOWN_DACL);
924 /* Enable TESTDAC. This sets the analog DAC inputs to a '0' input
925 * signal to avoid any glitches due to power up transients in both
926 * the analog and digital DAC circuit.
927 */
928 regmap_update_bits(nau8825->regmap, NAU8825_REG_BIAS_ADJ,
929 NAU8825_BIAS_TESTDAC_EN, NAU8825_BIAS_TESTDAC_EN);
930 /* CICCLP off */
931 regmap_update_bits(regmap, NAU8825_REG_DAC_CTRL1,
932 NAU8825_DAC_CLIP_OFF, NAU8825_DAC_CLIP_OFF);
933
934 /* Class AB bias current to 2x, DAC Capacitor enable MSB/LSB */
935 regmap_update_bits(regmap, NAU8825_REG_ANALOG_CONTROL_2,
936 NAU8825_HP_NON_CLASSG_CURRENT_2xADJ |
937 NAU8825_DAC_CAPACITOR_MSB | NAU8825_DAC_CAPACITOR_LSB,
938 NAU8825_HP_NON_CLASSG_CURRENT_2xADJ |
939 NAU8825_DAC_CAPACITOR_MSB | NAU8825_DAC_CAPACITOR_LSB);
940 /* Class G timer 64ms */
941 regmap_update_bits(regmap, NAU8825_REG_CLASSG_CTRL,
942 NAU8825_CLASSG_TIMER_MASK,
943 0x20 << NAU8825_CLASSG_TIMER_SFT);
944 /* DAC clock delay 2ns, VREF */
945 regmap_update_bits(regmap, NAU8825_REG_RDAC,
946 NAU8825_RDAC_CLK_DELAY_MASK | NAU8825_RDAC_VREF_MASK,
947 (0x2 << NAU8825_RDAC_CLK_DELAY_SFT) |
948 (0x3 << NAU8825_RDAC_VREF_SFT));
John Hsu3f039162016-03-30 14:57:11 +0800949 /* Config L/R channel */
950 regmap_update_bits(nau8825->regmap, NAU8825_REG_DACL_CTRL,
951 NAU8825_DACL_CH_SEL_MASK, NAU8825_DACL_CH_SEL_L);
952 regmap_update_bits(nau8825->regmap, NAU8825_REG_DACR_CTRL,
953 NAU8825_DACL_CH_SEL_MASK, NAU8825_DACL_CH_SEL_R);
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700954}
955
956static const struct regmap_config nau8825_regmap_config = {
957 .val_bits = 16,
958 .reg_bits = 16,
959
960 .max_register = NAU8825_REG_MAX,
961 .readable_reg = nau8825_readable_reg,
962 .writeable_reg = nau8825_writeable_reg,
963 .volatile_reg = nau8825_volatile_reg,
964
965 .cache_type = REGCACHE_RBTREE,
966 .reg_defaults = nau8825_reg_defaults,
967 .num_reg_defaults = ARRAY_SIZE(nau8825_reg_defaults),
968};
969
970static int nau8825_codec_probe(struct snd_soc_codec *codec)
971{
972 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
973 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
974
975 nau8825->dapm = dapm;
976
Anatol Pomozov34ca27f2015-10-02 09:49:14 -0700977 /* Unmask interruptions. Handler uses dapm object so we can enable
978 * interruptions only after dapm is fully initialized.
979 */
980 regmap_write(nau8825->regmap, NAU8825_REG_INTERRUPT_DIS_CTRL, 0);
981 nau8825_restart_jack_detection(nau8825->regmap);
982
983 return 0;
984}
985
Ben Zhangc86ba612015-10-19 16:49:05 -0700986/**
987 * nau8825_calc_fll_param - Calculate FLL parameters.
988 * @fll_in: external clock provided to codec.
989 * @fs: sampling rate.
990 * @fll_param: Pointer to structure of FLL parameters.
991 *
992 * Calculate FLL parameters to configure codec.
993 *
994 * Returns 0 for success or negative error code.
995 */
996static int nau8825_calc_fll_param(unsigned int fll_in, unsigned int fs,
997 struct nau8825_fll *fll_param)
998{
John Hsu407c71b2016-03-15 12:09:36 +0800999 u64 fvco, fvco_max;
1000 unsigned int fref, i, fvco_sel;
Ben Zhangc86ba612015-10-19 16:49:05 -07001001
1002 /* Ensure the reference clock frequency (FREF) is <= 13.5MHz by dividing
1003 * freq_in by 1, 2, 4, or 8 using FLL pre-scalar.
1004 * FREF = freq_in / NAU8825_FLL_REF_DIV_MASK
1005 */
1006 for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) {
1007 fref = fll_in / fll_pre_scalar[i].param;
1008 if (fref <= NAU_FREF_MAX)
1009 break;
1010 }
1011 if (i == ARRAY_SIZE(fll_pre_scalar))
1012 return -EINVAL;
1013 fll_param->clk_ref_div = fll_pre_scalar[i].val;
1014
1015 /* Choose the FLL ratio based on FREF */
1016 for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) {
1017 if (fref >= fll_ratio[i].param)
1018 break;
1019 }
1020 if (i == ARRAY_SIZE(fll_ratio))
1021 return -EINVAL;
1022 fll_param->ratio = fll_ratio[i].val;
1023
1024 /* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs.
John Hsu407c71b2016-03-15 12:09:36 +08001025 * FDCO must be within the 90MHz - 124MHz or the FFL cannot be
Ben Zhangc86ba612015-10-19 16:49:05 -07001026 * guaranteed across the full range of operation.
1027 * FDCO = freq_out * 2 * mclk_src_scaling
1028 */
John Hsu407c71b2016-03-15 12:09:36 +08001029 fvco_max = 0;
1030 fvco_sel = ARRAY_SIZE(mclk_src_scaling);
Ben Zhangc86ba612015-10-19 16:49:05 -07001031 for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) {
1032 fvco = 256 * fs * 2 * mclk_src_scaling[i].param;
John Hsu407c71b2016-03-15 12:09:36 +08001033 if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX &&
1034 fvco_max < fvco) {
1035 fvco_max = fvco;
1036 fvco_sel = i;
1037 }
Ben Zhangc86ba612015-10-19 16:49:05 -07001038 }
John Hsu407c71b2016-03-15 12:09:36 +08001039 if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel)
Ben Zhangc86ba612015-10-19 16:49:05 -07001040 return -EINVAL;
John Hsu407c71b2016-03-15 12:09:36 +08001041 fll_param->mclk_src = mclk_src_scaling[fvco_sel].val;
Ben Zhangc86ba612015-10-19 16:49:05 -07001042
1043 /* Calculate the FLL 10-bit integer input and the FLL 16-bit fractional
1044 * input based on FDCO, FREF and FLL ratio.
1045 */
1046 fvco = div_u64(fvco << 16, fref * fll_param->ratio);
1047 fll_param->fll_int = (fvco >> 16) & 0x3FF;
1048 fll_param->fll_frac = fvco & 0xFFFF;
1049 return 0;
1050}
1051
1052static void nau8825_fll_apply(struct nau8825 *nau8825,
1053 struct nau8825_fll *fll_param)
1054{
1055 regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
John Hsu407c71b2016-03-15 12:09:36 +08001056 NAU8825_CLK_SRC_MASK | NAU8825_CLK_MCLK_SRC_MASK,
1057 NAU8825_CLK_SRC_MCLK | fll_param->mclk_src);
Ben Zhangc86ba612015-10-19 16:49:05 -07001058 regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL1,
1059 NAU8825_FLL_RATIO_MASK, fll_param->ratio);
1060 /* FLL 16-bit fractional input */
1061 regmap_write(nau8825->regmap, NAU8825_REG_FLL2, fll_param->fll_frac);
1062 /* FLL 10-bit integer input */
1063 regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL3,
1064 NAU8825_FLL_INTEGER_MASK, fll_param->fll_int);
1065 /* FLL pre-scaler */
1066 regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL4,
1067 NAU8825_FLL_REF_DIV_MASK, fll_param->clk_ref_div);
1068 /* select divided VCO input */
1069 regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
John Hsu407c71b2016-03-15 12:09:36 +08001070 NAU8825_FLL_CLK_SW_MASK, NAU8825_FLL_CLK_SW_REF);
1071 /* Disable free-running mode */
1072 regmap_update_bits(nau8825->regmap,
1073 NAU8825_REG_FLL6, NAU8825_DCO_EN, 0);
1074 if (fll_param->fll_frac) {
1075 regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
1076 NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
1077 NAU8825_FLL_FTR_SW_MASK,
1078 NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
1079 NAU8825_FLL_FTR_SW_FILTER);
1080 regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL6,
1081 NAU8825_SDM_EN, NAU8825_SDM_EN);
1082 } else {
1083 regmap_update_bits(nau8825->regmap, NAU8825_REG_FLL5,
1084 NAU8825_FLL_PDB_DAC_EN | NAU8825_FLL_LOOP_FTR_EN |
1085 NAU8825_FLL_FTR_SW_MASK, NAU8825_FLL_FTR_SW_ACCU);
1086 regmap_update_bits(nau8825->regmap,
1087 NAU8825_REG_FLL6, NAU8825_SDM_EN, 0);
1088 }
Ben Zhangc86ba612015-10-19 16:49:05 -07001089}
1090
1091/* freq_out must be 256*Fs in order to achieve the best performance */
1092static int nau8825_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
1093 unsigned int freq_in, unsigned int freq_out)
1094{
1095 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
1096 struct nau8825_fll fll_param;
1097 int ret, fs;
1098
1099 fs = freq_out / 256;
1100 ret = nau8825_calc_fll_param(freq_in, fs, &fll_param);
1101 if (ret < 0) {
1102 dev_err(codec->dev, "Unsupported input clock %d\n", freq_in);
1103 return ret;
1104 }
1105 dev_dbg(codec->dev, "mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n",
1106 fll_param.mclk_src, fll_param.ratio, fll_param.fll_frac,
1107 fll_param.fll_int, fll_param.clk_ref_div);
1108
1109 nau8825_fll_apply(nau8825, &fll_param);
1110 mdelay(2);
1111 regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
1112 NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO);
1113 return 0;
1114}
1115
John Hsu70543c32016-03-15 12:08:21 +08001116static int nau8825_mclk_prepare(struct nau8825 *nau8825, unsigned int freq)
1117{
1118 int ret = 0;
1119
1120 nau8825->mclk = devm_clk_get(nau8825->dev, "mclk");
1121 if (IS_ERR(nau8825->mclk)) {
1122 dev_info(nau8825->dev, "No 'mclk' clock found, assume MCLK is managed externally");
1123 return 0;
1124 }
1125
1126 if (!nau8825->mclk_freq) {
1127 ret = clk_prepare_enable(nau8825->mclk);
1128 if (ret) {
1129 dev_err(nau8825->dev, "Unable to prepare codec mclk\n");
1130 return ret;
1131 }
1132 }
1133
1134 if (nau8825->mclk_freq != freq) {
1135 freq = clk_round_rate(nau8825->mclk, freq);
1136 ret = clk_set_rate(nau8825->mclk, freq);
1137 if (ret) {
1138 dev_err(nau8825->dev, "Unable to set mclk rate\n");
1139 return ret;
1140 }
1141 nau8825->mclk_freq = freq;
1142 }
1143
1144 return 0;
1145}
1146
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001147static int nau8825_configure_sysclk(struct nau8825 *nau8825, int clk_id,
1148 unsigned int freq)
1149{
1150 struct regmap *regmap = nau8825->regmap;
1151 int ret;
1152
1153 switch (clk_id) {
1154 case NAU8825_CLK_MCLK:
1155 regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
1156 NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_MCLK);
1157 regmap_update_bits(regmap, NAU8825_REG_FLL6, NAU8825_DCO_EN, 0);
John Hsu3a561032016-03-22 11:57:05 +08001158 /* MCLK not changed by clock tree */
1159 regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
1160 NAU8825_CLK_MCLK_SRC_MASK, 0);
John Hsu70543c32016-03-15 12:08:21 +08001161 ret = nau8825_mclk_prepare(nau8825, freq);
1162 if (ret)
1163 return ret;
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001164
1165 break;
1166 case NAU8825_CLK_INTERNAL:
1167 regmap_update_bits(regmap, NAU8825_REG_FLL6, NAU8825_DCO_EN,
1168 NAU8825_DCO_EN);
1169 regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
1170 NAU8825_CLK_SRC_MASK, NAU8825_CLK_SRC_VCO);
John Hsu3a561032016-03-22 11:57:05 +08001171 /* Decrease the VCO frequency for power saving */
1172 regmap_update_bits(regmap, NAU8825_REG_CLK_DIVIDER,
1173 NAU8825_CLK_MCLK_SRC_MASK, 0xf);
1174 regmap_update_bits(regmap, NAU8825_REG_FLL1,
1175 NAU8825_FLL_RATIO_MASK, 0x10);
1176 regmap_update_bits(regmap, NAU8825_REG_FLL6,
1177 NAU8825_SDM_EN, NAU8825_SDM_EN);
John Hsu70543c32016-03-15 12:08:21 +08001178 if (nau8825->mclk_freq) {
1179 clk_disable_unprepare(nau8825->mclk);
1180 nau8825->mclk_freq = 0;
1181 }
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001182
John Hsu70543c32016-03-15 12:08:21 +08001183 break;
1184 case NAU8825_CLK_FLL_MCLK:
1185 regmap_update_bits(regmap, NAU8825_REG_FLL3,
1186 NAU8825_FLL_CLK_SRC_MASK, NAU8825_FLL_CLK_SRC_MCLK);
1187 ret = nau8825_mclk_prepare(nau8825, freq);
1188 if (ret)
1189 return ret;
1190
1191 break;
1192 case NAU8825_CLK_FLL_BLK:
1193 regmap_update_bits(regmap, NAU8825_REG_FLL3,
1194 NAU8825_FLL_CLK_SRC_MASK, NAU8825_FLL_CLK_SRC_BLK);
1195 if (nau8825->mclk_freq) {
1196 clk_disable_unprepare(nau8825->mclk);
1197 nau8825->mclk_freq = 0;
1198 }
1199
1200 break;
1201 case NAU8825_CLK_FLL_FS:
1202 regmap_update_bits(regmap, NAU8825_REG_FLL3,
1203 NAU8825_FLL_CLK_SRC_MASK, NAU8825_FLL_CLK_SRC_FS);
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001204 if (nau8825->mclk_freq) {
1205 clk_disable_unprepare(nau8825->mclk);
1206 nau8825->mclk_freq = 0;
1207 }
1208
1209 break;
1210 default:
1211 dev_err(nau8825->dev, "Invalid clock id (%d)\n", clk_id);
1212 return -EINVAL;
1213 }
1214
1215 dev_dbg(nau8825->dev, "Sysclk is %dHz and clock id is %d\n", freq,
1216 clk_id);
1217 return 0;
1218}
1219
1220static int nau8825_set_sysclk(struct snd_soc_codec *codec, int clk_id,
1221 int source, unsigned int freq, int dir)
1222{
1223 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
1224
1225 return nau8825_configure_sysclk(nau8825, clk_id, freq);
1226}
1227
1228static int nau8825_set_bias_level(struct snd_soc_codec *codec,
1229 enum snd_soc_bias_level level)
1230{
1231 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
1232 int ret;
1233
1234 switch (level) {
1235 case SND_SOC_BIAS_ON:
1236 break;
1237
1238 case SND_SOC_BIAS_PREPARE:
1239 break;
1240
1241 case SND_SOC_BIAS_STANDBY:
1242 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
1243 if (nau8825->mclk_freq) {
1244 ret = clk_prepare_enable(nau8825->mclk);
1245 if (ret) {
1246 dev_err(nau8825->dev, "Unable to prepare codec mclk\n");
1247 return ret;
1248 }
1249 }
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001250 }
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001251 break;
1252
1253 case SND_SOC_BIAS_OFF:
1254 if (nau8825->mclk_freq)
1255 clk_disable_unprepare(nau8825->mclk);
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001256 break;
1257 }
1258 return 0;
1259}
1260
Ben Zhange6cee902016-03-25 16:10:39 -07001261#ifdef CONFIG_PM
1262static int nau8825_suspend(struct snd_soc_codec *codec)
1263{
1264 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
1265
1266 disable_irq(nau8825->irq);
1267 regcache_cache_only(nau8825->regmap, true);
1268 regcache_mark_dirty(nau8825->regmap);
1269
1270 return 0;
1271}
1272
1273static int nau8825_resume(struct snd_soc_codec *codec)
1274{
1275 struct nau8825 *nau8825 = snd_soc_codec_get_drvdata(codec);
1276
1277 /* The chip may lose power and reset in S3. regcache_sync restores
1278 * register values including configurations for sysclk, irq, and
1279 * jack/button detection.
1280 */
1281 regcache_cache_only(nau8825->regmap, false);
1282 regcache_sync(nau8825->regmap);
1283
1284 /* Check the jack plug status directly. If the headset is unplugged
1285 * during S3 when the chip has no power, there will be no jack
1286 * detection irq even after the nau8825_restart_jack_detection below,
1287 * because the chip just thinks no headset has ever been plugged in.
1288 */
1289 if (!nau8825_is_jack_inserted(nau8825->regmap)) {
1290 nau8825_eject_jack(nau8825);
1291 snd_soc_jack_report(nau8825->jack, 0, SND_JACK_HEADSET);
1292 }
1293
1294 enable_irq(nau8825->irq);
1295
1296 /* Run jack detection to check the type (OMTP or CTIA) of the headset
1297 * if there is one. This handles the case where a different type of
1298 * headset is plugged in during S3. This triggers an IRQ iff a headset
1299 * is already plugged in.
1300 */
1301 nau8825_restart_jack_detection(nau8825->regmap);
1302
1303 return 0;
1304}
1305#else
1306#define nau8825_suspend NULL
1307#define nau8825_resume NULL
1308#endif
1309
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001310static struct snd_soc_codec_driver nau8825_codec_driver = {
1311 .probe = nau8825_codec_probe,
1312 .set_sysclk = nau8825_set_sysclk,
Ben Zhangc86ba612015-10-19 16:49:05 -07001313 .set_pll = nau8825_set_pll,
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001314 .set_bias_level = nau8825_set_bias_level,
1315 .suspend_bias_off = true,
Ben Zhange6cee902016-03-25 16:10:39 -07001316 .suspend = nau8825_suspend,
1317 .resume = nau8825_resume,
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001318
1319 .controls = nau8825_controls,
1320 .num_controls = ARRAY_SIZE(nau8825_controls),
1321 .dapm_widgets = nau8825_dapm_widgets,
1322 .num_dapm_widgets = ARRAY_SIZE(nau8825_dapm_widgets),
1323 .dapm_routes = nau8825_dapm_routes,
1324 .num_dapm_routes = ARRAY_SIZE(nau8825_dapm_routes),
1325};
1326
1327static void nau8825_reset_chip(struct regmap *regmap)
1328{
1329 regmap_write(regmap, NAU8825_REG_RESET, 0x00);
1330 regmap_write(regmap, NAU8825_REG_RESET, 0x00);
1331}
1332
Ben Zhang218d2ce2015-10-19 16:49:06 -07001333static void nau8825_print_device_properties(struct nau8825 *nau8825)
1334{
1335 int i;
1336 struct device *dev = nau8825->dev;
1337
1338 dev_dbg(dev, "jkdet-enable: %d\n", nau8825->jkdet_enable);
1339 dev_dbg(dev, "jkdet-pull-enable: %d\n", nau8825->jkdet_pull_enable);
1340 dev_dbg(dev, "jkdet-pull-up: %d\n", nau8825->jkdet_pull_up);
1341 dev_dbg(dev, "jkdet-polarity: %d\n", nau8825->jkdet_polarity);
1342 dev_dbg(dev, "micbias-voltage: %d\n", nau8825->micbias_voltage);
1343 dev_dbg(dev, "vref-impedance: %d\n", nau8825->vref_impedance);
1344
1345 dev_dbg(dev, "sar-threshold-num: %d\n", nau8825->sar_threshold_num);
1346 for (i = 0; i < nau8825->sar_threshold_num; i++)
1347 dev_dbg(dev, "sar-threshold[%d]=%d\n", i,
1348 nau8825->sar_threshold[i]);
1349
1350 dev_dbg(dev, "sar-hysteresis: %d\n", nau8825->sar_hysteresis);
1351 dev_dbg(dev, "sar-voltage: %d\n", nau8825->sar_voltage);
1352 dev_dbg(dev, "sar-compare-time: %d\n", nau8825->sar_compare_time);
1353 dev_dbg(dev, "sar-sampling-time: %d\n", nau8825->sar_sampling_time);
1354 dev_dbg(dev, "short-key-debounce: %d\n", nau8825->key_debounce);
1355 dev_dbg(dev, "jack-insert-debounce: %d\n",
1356 nau8825->jack_insert_debounce);
1357 dev_dbg(dev, "jack-eject-debounce: %d\n",
1358 nau8825->jack_eject_debounce);
1359}
1360
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001361static int nau8825_read_device_properties(struct device *dev,
1362 struct nau8825 *nau8825) {
1363
1364 nau8825->jkdet_enable = device_property_read_bool(dev,
1365 "nuvoton,jkdet-enable");
1366 nau8825->jkdet_pull_enable = device_property_read_bool(dev,
1367 "nuvoton,jkdet-pull-enable");
1368 nau8825->jkdet_pull_up = device_property_read_bool(dev,
1369 "nuvoton,jkdet-pull-up");
1370 device_property_read_u32(dev, "nuvoton,jkdet-polarity",
1371 &nau8825->jkdet_polarity);
1372 device_property_read_u32(dev, "nuvoton,micbias-voltage",
1373 &nau8825->micbias_voltage);
1374 device_property_read_u32(dev, "nuvoton,vref-impedance",
1375 &nau8825->vref_impedance);
1376 device_property_read_u32(dev, "nuvoton,sar-threshold-num",
1377 &nau8825->sar_threshold_num);
1378 device_property_read_u32_array(dev, "nuvoton,sar-threshold",
1379 nau8825->sar_threshold, nau8825->sar_threshold_num);
1380 device_property_read_u32(dev, "nuvoton,sar-hysteresis",
1381 &nau8825->sar_hysteresis);
1382 device_property_read_u32(dev, "nuvoton,sar-voltage",
1383 &nau8825->sar_voltage);
1384 device_property_read_u32(dev, "nuvoton,sar-compare-time",
1385 &nau8825->sar_compare_time);
1386 device_property_read_u32(dev, "nuvoton,sar-sampling-time",
1387 &nau8825->sar_sampling_time);
1388 device_property_read_u32(dev, "nuvoton,short-key-debounce",
1389 &nau8825->key_debounce);
1390 device_property_read_u32(dev, "nuvoton,jack-insert-debounce",
1391 &nau8825->jack_insert_debounce);
1392 device_property_read_u32(dev, "nuvoton,jack-eject-debounce",
1393 &nau8825->jack_eject_debounce);
1394
1395 nau8825->mclk = devm_clk_get(dev, "mclk");
1396 if (PTR_ERR(nau8825->mclk) == -EPROBE_DEFER) {
1397 return -EPROBE_DEFER;
1398 } else if (PTR_ERR(nau8825->mclk) == -ENOENT) {
1399 /* The MCLK is managed externally or not used at all */
1400 nau8825->mclk = NULL;
1401 dev_info(dev, "No 'mclk' clock found, assume MCLK is managed externally");
1402 } else if (IS_ERR(nau8825->mclk)) {
1403 return -EINVAL;
1404 }
1405
1406 return 0;
1407}
1408
1409static int nau8825_setup_irq(struct nau8825 *nau8825)
1410{
1411 struct regmap *regmap = nau8825->regmap;
1412 int ret;
1413
1414 /* IRQ Output Enable */
1415 regmap_update_bits(regmap, NAU8825_REG_INTERRUPT_MASK,
1416 NAU8825_IRQ_OUTPUT_EN, NAU8825_IRQ_OUTPUT_EN);
1417
1418 /* Enable internal VCO needed for interruptions */
1419 nau8825_configure_sysclk(nau8825, NAU8825_CLK_INTERNAL, 0);
1420
John Hsueeef16a2016-03-22 11:57:20 +08001421 /* Enable ADC needed for interrupts */
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001422 regmap_update_bits(regmap, NAU8825_REG_ENA_CTRL,
John Hsueeef16a2016-03-22 11:57:20 +08001423 NAU8825_ENABLE_ADC, NAU8825_ENABLE_ADC);
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001424
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001425 ret = devm_request_threaded_irq(nau8825->dev, nau8825->irq, NULL,
1426 nau8825_interrupt, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1427 "nau8825", nau8825);
1428
1429 if (ret) {
1430 dev_err(nau8825->dev, "Cannot request irq %d (%d)\n",
1431 nau8825->irq, ret);
1432 return ret;
1433 }
1434
1435 return 0;
1436}
1437
1438static int nau8825_i2c_probe(struct i2c_client *i2c,
1439 const struct i2c_device_id *id)
1440{
1441 struct device *dev = &i2c->dev;
1442 struct nau8825 *nau8825 = dev_get_platdata(&i2c->dev);
1443 int ret, value;
1444
1445 if (!nau8825) {
1446 nau8825 = devm_kzalloc(dev, sizeof(*nau8825), GFP_KERNEL);
1447 if (!nau8825)
1448 return -ENOMEM;
1449 ret = nau8825_read_device_properties(dev, nau8825);
1450 if (ret)
1451 return ret;
1452 }
1453
1454 i2c_set_clientdata(i2c, nau8825);
1455
1456 nau8825->regmap = devm_regmap_init_i2c(i2c, &nau8825_regmap_config);
1457 if (IS_ERR(nau8825->regmap))
1458 return PTR_ERR(nau8825->regmap);
1459 nau8825->dev = dev;
1460 nau8825->irq = i2c->irq;
1461
Ben Zhang218d2ce2015-10-19 16:49:06 -07001462 nau8825_print_device_properties(nau8825);
1463
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001464 nau8825_reset_chip(nau8825->regmap);
1465 ret = regmap_read(nau8825->regmap, NAU8825_REG_I2C_DEVICE_ID, &value);
1466 if (ret < 0) {
1467 dev_err(dev, "Failed to read device id from the NAU8825: %d\n",
1468 ret);
1469 return ret;
1470 }
1471 if ((value & NAU8825_SOFTWARE_ID_MASK) !=
1472 NAU8825_SOFTWARE_ID_NAU8825) {
1473 dev_err(dev, "Not a NAU8825 chip\n");
1474 return -ENODEV;
1475 }
1476
1477 nau8825_init_regs(nau8825);
1478
1479 if (i2c->irq)
1480 nau8825_setup_irq(nau8825);
1481
1482 return snd_soc_register_codec(&i2c->dev, &nau8825_codec_driver,
1483 &nau8825_dai, 1);
1484}
1485
1486static int nau8825_i2c_remove(struct i2c_client *client)
1487{
1488 snd_soc_unregister_codec(&client->dev);
1489 return 0;
1490}
1491
1492static const struct i2c_device_id nau8825_i2c_ids[] = {
1493 { "nau8825", 0 },
1494 { }
1495};
1496
1497#ifdef CONFIG_OF
1498static const struct of_device_id nau8825_of_ids[] = {
1499 { .compatible = "nuvoton,nau8825", },
1500 {}
1501};
1502MODULE_DEVICE_TABLE(of, nau8825_of_ids);
1503#endif
1504
Fang, Yang Ab3681302015-10-07 14:33:57 -07001505#ifdef CONFIG_ACPI
1506static const struct acpi_device_id nau8825_acpi_match[] = {
1507 { "10508825", 0 },
1508 {},
1509};
1510MODULE_DEVICE_TABLE(acpi, nau8825_acpi_match);
1511#endif
1512
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001513static struct i2c_driver nau8825_driver = {
1514 .driver = {
1515 .name = "nau8825",
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001516 .of_match_table = of_match_ptr(nau8825_of_ids),
Fang, Yang Ab3681302015-10-07 14:33:57 -07001517 .acpi_match_table = ACPI_PTR(nau8825_acpi_match),
Anatol Pomozov34ca27f2015-10-02 09:49:14 -07001518 },
1519 .probe = nau8825_i2c_probe,
1520 .remove = nau8825_i2c_remove,
1521 .id_table = nau8825_i2c_ids,
1522};
1523module_i2c_driver(nau8825_driver);
1524
1525MODULE_DESCRIPTION("ASoC nau8825 driver");
1526MODULE_AUTHOR("Anatol Pomozov <anatol@chromium.org>");
1527MODULE_LICENSE("GPL");