blob: 97daa80e91048bb6d354f366b02653d49bfb371a [file] [log] [blame]
Bard Liao07cf7cba2014-06-20 14:41:13 +08001/*
2 * rt286.c -- RT286 ALSA SoC audio codec driver
3 *
4 * Copyright 2013 Realtek Semiconductor Corp.
5 * Author: Bard Liao <bardliao@realtek.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/init.h>
15#include <linux/delay.h>
16#include <linux/pm.h>
17#include <linux/i2c.h>
18#include <linux/platform_device.h>
19#include <linux/spi/spi.h>
20#include <linux/acpi.h>
21#include <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc.h>
25#include <sound/soc-dapm.h>
26#include <sound/initval.h>
27#include <sound/tlv.h>
28#include <sound/jack.h>
29#include <linux/workqueue.h>
30#include <sound/rt286.h>
31#include <sound/hda_verbs.h>
32
33#include "rt286.h"
34
35#define RT286_VENDOR_ID 0x10ec0286
36
37struct rt286_priv {
38 struct regmap *regmap;
Bard Liao6879db72014-10-31 14:52:16 +080039 struct snd_soc_codec *codec;
Bard Liao07cf7cba2014-06-20 14:41:13 +080040 struct rt286_platform_data pdata;
41 struct i2c_client *i2c;
42 struct snd_soc_jack *jack;
43 struct delayed_work jack_detect_work;
44 int sys_clk;
Bard Liao6879db72014-10-31 14:52:16 +080045 int clk_id;
Bard Liao07cf7cba2014-06-20 14:41:13 +080046 struct reg_default *index_cache;
47};
48
49static struct reg_default rt286_index_def[] = {
50 { 0x01, 0xaaaa },
51 { 0x02, 0x8aaa },
52 { 0x03, 0x0002 },
53 { 0x04, 0xaf01 },
54 { 0x08, 0x000d },
55 { 0x09, 0xd810 },
Bard Liaob7a29762014-09-26 11:06:39 +080056 { 0x0a, 0x0120 },
Bard Liao07cf7cba2014-06-20 14:41:13 +080057 { 0x0b, 0x0000 },
Bard Liaobc6c4e42014-07-07 19:15:30 +080058 { 0x0d, 0x2800 },
Bard Liao07cf7cba2014-06-20 14:41:13 +080059 { 0x0f, 0x0000 },
60 { 0x19, 0x0a17 },
61 { 0x20, 0x0020 },
62 { 0x33, 0x0208 },
63 { 0x49, 0x0004 },
64 { 0x4f, 0x50e9 },
Bard Liaob7a29762014-09-26 11:06:39 +080065 { 0x50, 0x2000 },
Bard Liao07cf7cba2014-06-20 14:41:13 +080066 { 0x63, 0x2902 },
Bard Liaobc6c4e42014-07-07 19:15:30 +080067 { 0x67, 0x1111 },
68 { 0x68, 0x1016 },
69 { 0x69, 0x273f },
Bard Liao07cf7cba2014-06-20 14:41:13 +080070};
71#define INDEX_CACHE_SIZE ARRAY_SIZE(rt286_index_def)
72
73static const struct reg_default rt286_reg[] = {
74 { 0x00170500, 0x00000400 },
75 { 0x00220000, 0x00000031 },
76 { 0x00239000, 0x0000007f },
77 { 0x0023a000, 0x0000007f },
78 { 0x00270500, 0x00000400 },
79 { 0x00370500, 0x00000400 },
80 { 0x00870500, 0x00000400 },
81 { 0x00920000, 0x00000031 },
82 { 0x00935000, 0x000000c3 },
83 { 0x00936000, 0x000000c3 },
84 { 0x00970500, 0x00000400 },
85 { 0x00b37000, 0x00000097 },
86 { 0x00b37200, 0x00000097 },
87 { 0x00b37300, 0x00000097 },
88 { 0x00c37000, 0x00000000 },
89 { 0x00c37100, 0x00000080 },
90 { 0x01270500, 0x00000400 },
91 { 0x01370500, 0x00000400 },
92 { 0x01371f00, 0x411111f0 },
93 { 0x01439000, 0x00000080 },
94 { 0x0143a000, 0x00000080 },
95 { 0x01470700, 0x00000000 },
96 { 0x01470500, 0x00000400 },
97 { 0x01470c00, 0x00000000 },
98 { 0x01470100, 0x00000000 },
99 { 0x01837000, 0x00000000 },
100 { 0x01870500, 0x00000400 },
101 { 0x02050000, 0x00000000 },
102 { 0x02139000, 0x00000080 },
103 { 0x0213a000, 0x00000080 },
104 { 0x02170100, 0x00000000 },
105 { 0x02170500, 0x00000400 },
106 { 0x02170700, 0x00000000 },
107 { 0x02270100, 0x00000000 },
108 { 0x02370100, 0x00000000 },
Bard Liao07cf7cba2014-06-20 14:41:13 +0800109 { 0x01870700, 0x00000020 },
110 { 0x00830000, 0x000000c3 },
111 { 0x00930000, 0x000000c3 },
112 { 0x01270700, 0x00000000 },
113};
114
115static bool rt286_volatile_register(struct device *dev, unsigned int reg)
116{
117 switch (reg) {
118 case 0 ... 0xff:
119 case RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID):
120 case RT286_GET_HP_SENSE:
121 case RT286_GET_MIC1_SENSE:
122 case RT286_PROC_COEF:
123 return true;
124 default:
125 return false;
126 }
127
128
129}
130
131static bool rt286_readable_register(struct device *dev, unsigned int reg)
132{
133 switch (reg) {
134 case 0 ... 0xff:
135 case RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID):
136 case RT286_GET_HP_SENSE:
137 case RT286_GET_MIC1_SENSE:
138 case RT286_SET_AUDIO_POWER:
139 case RT286_SET_HPO_POWER:
140 case RT286_SET_SPK_POWER:
141 case RT286_SET_DMIC1_POWER:
142 case RT286_SPK_MUX:
143 case RT286_HPO_MUX:
144 case RT286_ADC0_MUX:
145 case RT286_ADC1_MUX:
146 case RT286_SET_MIC1:
147 case RT286_SET_PIN_HPO:
148 case RT286_SET_PIN_SPK:
149 case RT286_SET_PIN_DMIC1:
150 case RT286_SPK_EAPD:
151 case RT286_SET_AMP_GAIN_HPO:
152 case RT286_SET_DMIC2_DEFAULT:
153 case RT286_DACL_GAIN:
154 case RT286_DACR_GAIN:
155 case RT286_ADCL_GAIN:
156 case RT286_ADCR_GAIN:
157 case RT286_MIC_GAIN:
158 case RT286_SPOL_GAIN:
159 case RT286_SPOR_GAIN:
160 case RT286_HPOL_GAIN:
161 case RT286_HPOR_GAIN:
162 case RT286_F_DAC_SWITCH:
163 case RT286_F_RECMIX_SWITCH:
164 case RT286_REC_MIC_SWITCH:
165 case RT286_REC_I2S_SWITCH:
166 case RT286_REC_LINE_SWITCH:
167 case RT286_REC_BEEP_SWITCH:
168 case RT286_DAC_FORMAT:
169 case RT286_ADC_FORMAT:
170 case RT286_COEF_INDEX:
171 case RT286_PROC_COEF:
172 case RT286_SET_AMP_GAIN_ADC_IN1:
173 case RT286_SET_AMP_GAIN_ADC_IN2:
174 case RT286_SET_POWER(RT286_DAC_OUT1):
175 case RT286_SET_POWER(RT286_DAC_OUT2):
176 case RT286_SET_POWER(RT286_ADC_IN1):
177 case RT286_SET_POWER(RT286_ADC_IN2):
178 case RT286_SET_POWER(RT286_DMIC2):
179 case RT286_SET_POWER(RT286_MIC1):
180 return true;
181 default:
182 return false;
183 }
184}
185
186static int rt286_hw_write(void *context, unsigned int reg, unsigned int value)
187{
188 struct i2c_client *client = context;
189 struct rt286_priv *rt286 = i2c_get_clientdata(client);
190 u8 data[4];
191 int ret, i;
192
193 /*handle index registers*/
194 if (reg <= 0xff) {
195 rt286_hw_write(client, RT286_COEF_INDEX, reg);
Bard Liao07cf7cba2014-06-20 14:41:13 +0800196 for (i = 0; i < INDEX_CACHE_SIZE; i++) {
197 if (reg == rt286->index_cache[i].reg) {
198 rt286->index_cache[i].def = value;
199 break;
200 }
201
202 }
Bard Liao66d627d2014-09-26 11:06:40 +0800203 reg = RT286_PROC_COEF;
Bard Liao07cf7cba2014-06-20 14:41:13 +0800204 }
205
206 data[0] = (reg >> 24) & 0xff;
207 data[1] = (reg >> 16) & 0xff;
208 /*
209 * 4 bit VID: reg should be 0
210 * 12 bit VID: value should be 0
211 * So we use an OR operator to handle it rather than use if condition.
212 */
213 data[2] = ((reg >> 8) & 0xff) | ((value >> 8) & 0xff);
214 data[3] = value & 0xff;
215
216 ret = i2c_master_send(client, data, 4);
217
218 if (ret == 4)
219 return 0;
220 else
221 pr_err("ret=%d\n", ret);
222 if (ret < 0)
223 return ret;
224 else
225 return -EIO;
226}
227
228static int rt286_hw_read(void *context, unsigned int reg, unsigned int *value)
229{
230 struct i2c_client *client = context;
231 struct i2c_msg xfer[2];
232 int ret;
233 __be32 be_reg;
234 unsigned int index, vid, buf = 0x0;
235
236 /*handle index registers*/
237 if (reg <= 0xff) {
238 rt286_hw_write(client, RT286_COEF_INDEX, reg);
239 reg = RT286_PROC_COEF;
240 }
241
242 reg = reg | 0x80000;
243 vid = (reg >> 8) & 0xfff;
244
245 if (AC_VERB_GET_AMP_GAIN_MUTE == (vid & 0xf00)) {
246 index = (reg >> 8) & 0xf;
247 reg = (reg & ~0xf0f) | index;
248 }
249 be_reg = cpu_to_be32(reg);
250
251 /* Write register */
252 xfer[0].addr = client->addr;
253 xfer[0].flags = 0;
254 xfer[0].len = 4;
255 xfer[0].buf = (u8 *)&be_reg;
256
257 /* Read data */
258 xfer[1].addr = client->addr;
259 xfer[1].flags = I2C_M_RD;
260 xfer[1].len = 4;
261 xfer[1].buf = (u8 *)&buf;
262
263 ret = i2c_transfer(client->adapter, xfer, 2);
264 if (ret < 0)
265 return ret;
266 else if (ret != 2)
267 return -EIO;
268
269 *value = be32_to_cpu(buf);
270
271 return 0;
272}
273
Thierry Reding81f3dfe2014-10-02 09:27:03 +0200274#ifdef CONFIG_PM
Bard Liao07cf7cba2014-06-20 14:41:13 +0800275static void rt286_index_sync(struct snd_soc_codec *codec)
276{
277 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
278 int i;
279
280 for (i = 0; i < INDEX_CACHE_SIZE; i++) {
281 snd_soc_write(codec, rt286->index_cache[i].reg,
282 rt286->index_cache[i].def);
283 }
284}
Thierry Reding81f3dfe2014-10-02 09:27:03 +0200285#endif
Bard Liao07cf7cba2014-06-20 14:41:13 +0800286
287static int rt286_support_power_controls[] = {
288 RT286_DAC_OUT1,
289 RT286_DAC_OUT2,
290 RT286_ADC_IN1,
291 RT286_ADC_IN2,
292 RT286_MIC1,
293 RT286_DMIC1,
294 RT286_DMIC2,
295 RT286_SPK_OUT,
296 RT286_HP_OUT,
297};
298#define RT286_POWER_REG_LEN ARRAY_SIZE(rt286_support_power_controls)
299
Bard Liao90f601e2014-07-29 13:50:57 +0800300static int rt286_jack_detect(struct rt286_priv *rt286, bool *hp, bool *mic)
Bard Liao07cf7cba2014-06-20 14:41:13 +0800301{
Bard Liao07cf7cba2014-06-20 14:41:13 +0800302 unsigned int val, buf;
Bard Liao07cf7cba2014-06-20 14:41:13 +0800303
304 *hp = false;
305 *mic = false;
306
307 if (rt286->pdata.cbj_en) {
Bard Liao90f601e2014-07-29 13:50:57 +0800308 regmap_read(rt286->regmap, RT286_GET_HP_SENSE, &buf);
Bard Liao07cf7cba2014-06-20 14:41:13 +0800309 *hp = buf & 0x80000000;
310 if (*hp) {
311 /* power on HV,VERF */
Bard Liao90f601e2014-07-29 13:50:57 +0800312 regmap_update_bits(rt286->regmap,
Bard Liao6879db72014-10-31 14:52:16 +0800313 RT286_DC_GAIN, 0x200, 0x200);
314
315 snd_soc_dapm_force_enable_pin(&rt286->codec->dapm,
316 "HV");
317 snd_soc_dapm_force_enable_pin(&rt286->codec->dapm,
318 "VREF");
Bard Liao07cf7cba2014-06-20 14:41:13 +0800319 /* power LDO1 */
Bard Liao6879db72014-10-31 14:52:16 +0800320 snd_soc_dapm_force_enable_pin(&rt286->codec->dapm,
321 "LDO1");
322 snd_soc_dapm_sync(&rt286->codec->dapm);
323
Bard Liao90f601e2014-07-29 13:50:57 +0800324 regmap_write(rt286->regmap, RT286_SET_MIC1, 0x24);
Bard Liao6879db72014-10-31 14:52:16 +0800325 msleep(50);
326
327 regmap_update_bits(rt286->regmap,
328 RT286_CBJ_CTRL1, 0xfcc0, 0xd400);
329 msleep(300);
Bard Liao90f601e2014-07-29 13:50:57 +0800330 regmap_read(rt286->regmap, RT286_CBJ_CTRL2, &val);
Bard Liao07cf7cba2014-06-20 14:41:13 +0800331
Bard Liao6879db72014-10-31 14:52:16 +0800332 if (0x0070 == (val & 0x0070)) {
333 *mic = true;
334 } else {
335 regmap_update_bits(rt286->regmap,
336 RT286_CBJ_CTRL1, 0xfcc0, 0xe400);
337 msleep(300);
Bard Liao90f601e2014-07-29 13:50:57 +0800338 regmap_read(rt286->regmap,
339 RT286_CBJ_CTRL2, &val);
Bard Liao6879db72014-10-31 14:52:16 +0800340 if (0x0070 == (val & 0x0070))
341 *mic = true;
342 else
343 *mic = false;
Bard Liao07cf7cba2014-06-20 14:41:13 +0800344 }
Bard Liao90f601e2014-07-29 13:50:57 +0800345 regmap_update_bits(rt286->regmap,
Bard Liao6879db72014-10-31 14:52:16 +0800346 RT286_DC_GAIN, 0x200, 0x0);
347
Bard Liao07cf7cba2014-06-20 14:41:13 +0800348 } else {
Bard Liao07cf7cba2014-06-20 14:41:13 +0800349 *mic = false;
Bard Liao6879db72014-10-31 14:52:16 +0800350 regmap_write(rt286->regmap, RT286_SET_MIC1, 0x20);
Bard Liao07cf7cba2014-06-20 14:41:13 +0800351 }
352 } else {
Bard Liao90f601e2014-07-29 13:50:57 +0800353 regmap_read(rt286->regmap, RT286_GET_HP_SENSE, &buf);
Bard Liao07cf7cba2014-06-20 14:41:13 +0800354 *hp = buf & 0x80000000;
Bard Liao90f601e2014-07-29 13:50:57 +0800355 regmap_read(rt286->regmap, RT286_GET_MIC1_SENSE, &buf);
Bard Liao07cf7cba2014-06-20 14:41:13 +0800356 *mic = buf & 0x80000000;
357 }
358
Bard Liao6879db72014-10-31 14:52:16 +0800359 snd_soc_dapm_disable_pin(&rt286->codec->dapm, "HV");
360 snd_soc_dapm_disable_pin(&rt286->codec->dapm, "VREF");
361 if (!*hp)
362 snd_soc_dapm_disable_pin(&rt286->codec->dapm, "LDO1");
363 snd_soc_dapm_sync(&rt286->codec->dapm);
364
Bard Liao07cf7cba2014-06-20 14:41:13 +0800365 return 0;
366}
367
368static void rt286_jack_detect_work(struct work_struct *work)
369{
370 struct rt286_priv *rt286 =
371 container_of(work, struct rt286_priv, jack_detect_work.work);
372 int status = 0;
373 bool hp = false;
374 bool mic = false;
375
Bard Liao90f601e2014-07-29 13:50:57 +0800376 rt286_jack_detect(rt286, &hp, &mic);
Bard Liao07cf7cba2014-06-20 14:41:13 +0800377
378 if (hp == true)
379 status |= SND_JACK_HEADPHONE;
380
381 if (mic == true)
382 status |= SND_JACK_MICROPHONE;
383
384 snd_soc_jack_report(rt286->jack, status,
385 SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
386}
387
388int rt286_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack)
389{
390 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
391
392 rt286->jack = jack;
393
394 /* Send an initial empty report */
395 snd_soc_jack_report(rt286->jack, 0,
396 SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
397
398 return 0;
399}
400EXPORT_SYMBOL_GPL(rt286_mic_detect);
401
Bard Liao6879db72014-10-31 14:52:16 +0800402static int is_mclk_mode(struct snd_soc_dapm_widget *source,
403 struct snd_soc_dapm_widget *sink)
404{
405 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(source->codec);
406
407 if (rt286->clk_id == RT286_SCLK_S_MCLK)
408 return 1;
409 else
410 return 0;
411}
412
Bard Liao07cf7cba2014-06-20 14:41:13 +0800413static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6350, 50, 0);
414static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
415
416static const struct snd_kcontrol_new rt286_snd_controls[] = {
417 SOC_DOUBLE_R_TLV("DAC0 Playback Volume", RT286_DACL_GAIN,
418 RT286_DACR_GAIN, 0, 0x7f, 0, out_vol_tlv),
419 SOC_DOUBLE_R_TLV("ADC0 Capture Volume", RT286_ADCL_GAIN,
420 RT286_ADCR_GAIN, 0, 0x7f, 0, out_vol_tlv),
421 SOC_SINGLE_TLV("AMIC Volume", RT286_MIC_GAIN,
422 0, 0x3, 0, mic_vol_tlv),
423 SOC_DOUBLE_R("Speaker Playback Switch", RT286_SPOL_GAIN,
424 RT286_SPOR_GAIN, RT286_MUTE_SFT, 1, 1),
425};
426
427/* Digital Mixer */
428static const struct snd_kcontrol_new rt286_front_mix[] = {
429 SOC_DAPM_SINGLE("DAC Switch", RT286_F_DAC_SWITCH,
430 RT286_MUTE_SFT, 1, 1),
431 SOC_DAPM_SINGLE("RECMIX Switch", RT286_F_RECMIX_SWITCH,
432 RT286_MUTE_SFT, 1, 1),
433};
434
435/* Analog Input Mixer */
436static const struct snd_kcontrol_new rt286_rec_mix[] = {
437 SOC_DAPM_SINGLE("Mic1 Switch", RT286_REC_MIC_SWITCH,
438 RT286_MUTE_SFT, 1, 1),
439 SOC_DAPM_SINGLE("I2S Switch", RT286_REC_I2S_SWITCH,
440 RT286_MUTE_SFT, 1, 1),
441 SOC_DAPM_SINGLE("Line1 Switch", RT286_REC_LINE_SWITCH,
442 RT286_MUTE_SFT, 1, 1),
443 SOC_DAPM_SINGLE("Beep Switch", RT286_REC_BEEP_SWITCH,
444 RT286_MUTE_SFT, 1, 1),
445};
446
447static const struct snd_kcontrol_new spo_enable_control =
448 SOC_DAPM_SINGLE("Switch", RT286_SET_PIN_SPK,
449 RT286_SET_PIN_SFT, 1, 0);
450
451static const struct snd_kcontrol_new hpol_enable_control =
452 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT286_HPOL_GAIN,
453 RT286_MUTE_SFT, 1, 1);
454
455static const struct snd_kcontrol_new hpor_enable_control =
456 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT286_HPOR_GAIN,
457 RT286_MUTE_SFT, 1, 1);
458
459/* ADC0 source */
460static const char * const rt286_adc_src[] = {
461 "Mic", "RECMIX", "Dmic"
462};
463
464static const int rt286_adc_values[] = {
465 0, 4, 5,
466};
467
468static SOC_VALUE_ENUM_SINGLE_DECL(
469 rt286_adc0_enum, RT286_ADC0_MUX, RT286_ADC_SEL_SFT,
470 RT286_ADC_SEL_MASK, rt286_adc_src, rt286_adc_values);
471
472static const struct snd_kcontrol_new rt286_adc0_mux =
473 SOC_DAPM_ENUM("ADC 0 source", rt286_adc0_enum);
474
475static SOC_VALUE_ENUM_SINGLE_DECL(
476 rt286_adc1_enum, RT286_ADC1_MUX, RT286_ADC_SEL_SFT,
477 RT286_ADC_SEL_MASK, rt286_adc_src, rt286_adc_values);
478
479static const struct snd_kcontrol_new rt286_adc1_mux =
480 SOC_DAPM_ENUM("ADC 1 source", rt286_adc1_enum);
481
482static const char * const rt286_dac_src[] = {
483 "Front", "Surround"
484};
485/* HP-OUT source */
486static SOC_ENUM_SINGLE_DECL(rt286_hpo_enum, RT286_HPO_MUX,
487 0, rt286_dac_src);
488
489static const struct snd_kcontrol_new rt286_hpo_mux =
490SOC_DAPM_ENUM("HPO source", rt286_hpo_enum);
491
492/* SPK-OUT source */
493static SOC_ENUM_SINGLE_DECL(rt286_spo_enum, RT286_SPK_MUX,
494 0, rt286_dac_src);
495
496static const struct snd_kcontrol_new rt286_spo_mux =
497SOC_DAPM_ENUM("SPO source", rt286_spo_enum);
498
499static int rt286_spk_event(struct snd_soc_dapm_widget *w,
500 struct snd_kcontrol *kcontrol, int event)
501{
502 struct snd_soc_codec *codec = w->codec;
503
504 switch (event) {
505 case SND_SOC_DAPM_POST_PMU:
506 snd_soc_write(codec,
507 RT286_SPK_EAPD, RT286_SET_EAPD_HIGH);
508 break;
509 case SND_SOC_DAPM_PRE_PMD:
510 snd_soc_write(codec,
511 RT286_SPK_EAPD, RT286_SET_EAPD_LOW);
512 break;
513
514 default:
515 return 0;
516 }
517
518 return 0;
519}
520
521static int rt286_set_dmic1_event(struct snd_soc_dapm_widget *w,
522 struct snd_kcontrol *kcontrol, int event)
523{
524 struct snd_soc_codec *codec = w->codec;
525
526 switch (event) {
527 case SND_SOC_DAPM_POST_PMU:
528 snd_soc_write(codec, RT286_SET_PIN_DMIC1, 0x20);
529 break;
530 case SND_SOC_DAPM_PRE_PMD:
531 snd_soc_write(codec, RT286_SET_PIN_DMIC1, 0);
532 break;
533 default:
534 return 0;
535 }
536
537 return 0;
538}
539
540static int rt286_adc_event(struct snd_soc_dapm_widget *w,
541 struct snd_kcontrol *kcontrol, int event)
542{
543 struct snd_soc_codec *codec = w->codec;
544 unsigned int nid;
545
546 nid = (w->reg >> 20) & 0xff;
547
548 switch (event) {
549 case SND_SOC_DAPM_POST_PMU:
550 snd_soc_update_bits(codec,
551 VERB_CMD(AC_VERB_SET_AMP_GAIN_MUTE, nid, 0),
552 0x7080, 0x7000);
553 break;
554 case SND_SOC_DAPM_PRE_PMD:
555 snd_soc_update_bits(codec,
556 VERB_CMD(AC_VERB_SET_AMP_GAIN_MUTE, nid, 0),
557 0x7080, 0x7080);
558 break;
559 default:
560 return 0;
561 }
562
563 return 0;
564}
565
Bard Liao6879db72014-10-31 14:52:16 +0800566static int rt286_vref_event(struct snd_soc_dapm_widget *w,
567 struct snd_kcontrol *kcontrol, int event)
568{
569 struct snd_soc_codec *codec = w->codec;
570
571 switch (event) {
572 case SND_SOC_DAPM_PRE_PMU:
573 snd_soc_update_bits(codec,
574 RT286_CBJ_CTRL1, 0x0400, 0x0000);
575 mdelay(50);
576 break;
577 default:
578 return 0;
579 }
580
581 return 0;
582}
583
584static int rt286_ldo2_event(struct snd_soc_dapm_widget *w,
585 struct snd_kcontrol *kcontrol, int event)
586{
587 struct snd_soc_codec *codec = w->codec;
588
589 switch (event) {
590 case SND_SOC_DAPM_POST_PMU:
591 snd_soc_update_bits(codec, RT286_POWER_CTRL2, 0x38, 0x08);
592 break;
593 case SND_SOC_DAPM_PRE_PMD:
594 snd_soc_update_bits(codec, RT286_POWER_CTRL2, 0x38, 0x30);
595 break;
596 default:
597 return 0;
598 }
599
600 return 0;
601}
602
603static int rt286_mic1_event(struct snd_soc_dapm_widget *w,
604 struct snd_kcontrol *kcontrol, int event)
605{
606 struct snd_soc_codec *codec = w->codec;
607
608 switch (event) {
609 case SND_SOC_DAPM_PRE_PMU:
610 snd_soc_update_bits(codec,
611 RT286_A_BIAS_CTRL3, 0xc000, 0x8000);
612 snd_soc_update_bits(codec,
613 RT286_A_BIAS_CTRL2, 0xc000, 0x8000);
614 break;
615 case SND_SOC_DAPM_POST_PMD:
616 snd_soc_update_bits(codec,
617 RT286_A_BIAS_CTRL3, 0xc000, 0x0000);
618 snd_soc_update_bits(codec,
619 RT286_A_BIAS_CTRL2, 0xc000, 0x0000);
620 break;
621 default:
622 return 0;
623 }
624
625 return 0;
626}
627
Bard Liao07cf7cba2014-06-20 14:41:13 +0800628static const struct snd_soc_dapm_widget rt286_dapm_widgets[] = {
Bard Liao6879db72014-10-31 14:52:16 +0800629 SND_SOC_DAPM_SUPPLY_S("HV", 1, RT286_POWER_CTRL1,
630 12, 1, NULL, 0),
631 SND_SOC_DAPM_SUPPLY("VREF", RT286_POWER_CTRL1,
632 0, 1, rt286_vref_event, SND_SOC_DAPM_PRE_PMU),
633 SND_SOC_DAPM_SUPPLY_S("LDO1", 1, RT286_POWER_CTRL2,
634 2, 0, NULL, 0),
635 SND_SOC_DAPM_SUPPLY_S("LDO2", 2, RT286_POWER_CTRL1,
636 13, 1, rt286_ldo2_event, SND_SOC_DAPM_PRE_PMD |
637 SND_SOC_DAPM_POST_PMU),
638 SND_SOC_DAPM_SUPPLY("MCLK MODE", RT286_PLL_CTRL1,
639 5, 0, NULL, 0),
640 SND_SOC_DAPM_SUPPLY("MIC1 Input Buffer", SND_SOC_NOPM,
641 0, 0, rt286_mic1_event, SND_SOC_DAPM_PRE_PMU |
642 SND_SOC_DAPM_POST_PMD),
643
Bard Liao07cf7cba2014-06-20 14:41:13 +0800644 /* Input Lines */
645 SND_SOC_DAPM_INPUT("DMIC1 Pin"),
646 SND_SOC_DAPM_INPUT("DMIC2 Pin"),
647 SND_SOC_DAPM_INPUT("MIC1"),
648 SND_SOC_DAPM_INPUT("LINE1"),
649 SND_SOC_DAPM_INPUT("Beep"),
650
651 /* DMIC */
652 SND_SOC_DAPM_PGA_E("DMIC1", RT286_SET_POWER(RT286_DMIC1), 0, 1,
653 NULL, 0, rt286_set_dmic1_event,
654 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
655 SND_SOC_DAPM_PGA("DMIC2", RT286_SET_POWER(RT286_DMIC2), 0, 1,
656 NULL, 0),
657 SND_SOC_DAPM_SUPPLY("DMIC Receiver", SND_SOC_NOPM,
658 0, 0, NULL, 0),
659
660 /* REC Mixer */
661 SND_SOC_DAPM_MIXER("RECMIX", SND_SOC_NOPM, 0, 0,
662 rt286_rec_mix, ARRAY_SIZE(rt286_rec_mix)),
663
664 /* ADCs */
665 SND_SOC_DAPM_ADC("ADC 0", NULL, SND_SOC_NOPM, 0, 0),
666 SND_SOC_DAPM_ADC("ADC 1", NULL, SND_SOC_NOPM, 0, 0),
667
668 /* ADC Mux */
669 SND_SOC_DAPM_MUX_E("ADC 0 Mux", RT286_SET_POWER(RT286_ADC_IN1), 0, 1,
670 &rt286_adc0_mux, rt286_adc_event, SND_SOC_DAPM_PRE_PMD |
671 SND_SOC_DAPM_POST_PMU),
672 SND_SOC_DAPM_MUX_E("ADC 1 Mux", RT286_SET_POWER(RT286_ADC_IN2), 0, 1,
673 &rt286_adc1_mux, rt286_adc_event, SND_SOC_DAPM_PRE_PMD |
674 SND_SOC_DAPM_POST_PMU),
675
676 /* Audio Interface */
677 SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
678 SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
679 SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
680 SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
681
682 /* Output Side */
683 /* DACs */
684 SND_SOC_DAPM_DAC("DAC 0", NULL, SND_SOC_NOPM, 0, 0),
685 SND_SOC_DAPM_DAC("DAC 1", NULL, SND_SOC_NOPM, 0, 0),
686
687 /* Output Mux */
688 SND_SOC_DAPM_MUX("SPK Mux", SND_SOC_NOPM, 0, 0, &rt286_spo_mux),
689 SND_SOC_DAPM_MUX("HPO Mux", SND_SOC_NOPM, 0, 0, &rt286_hpo_mux),
690
691 SND_SOC_DAPM_SUPPLY("HP Power", RT286_SET_PIN_HPO,
692 RT286_SET_PIN_SFT, 0, NULL, 0),
693
694 /* Output Mixer */
695 SND_SOC_DAPM_MIXER("Front", RT286_SET_POWER(RT286_DAC_OUT1), 0, 1,
696 rt286_front_mix, ARRAY_SIZE(rt286_front_mix)),
697 SND_SOC_DAPM_PGA("Surround", RT286_SET_POWER(RT286_DAC_OUT2), 0, 1,
698 NULL, 0),
699
700 /* Output Pga */
701 SND_SOC_DAPM_SWITCH_E("SPO", SND_SOC_NOPM, 0, 0,
702 &spo_enable_control, rt286_spk_event,
703 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
704 SND_SOC_DAPM_SWITCH("HPO L", SND_SOC_NOPM, 0, 0,
705 &hpol_enable_control),
706 SND_SOC_DAPM_SWITCH("HPO R", SND_SOC_NOPM, 0, 0,
707 &hpor_enable_control),
708
709 /* Output Lines */
710 SND_SOC_DAPM_OUTPUT("SPOL"),
711 SND_SOC_DAPM_OUTPUT("SPOR"),
712 SND_SOC_DAPM_OUTPUT("HPO Pin"),
713 SND_SOC_DAPM_OUTPUT("SPDIF"),
714};
715
716static const struct snd_soc_dapm_route rt286_dapm_routes[] = {
Bard Liao6879db72014-10-31 14:52:16 +0800717 {"ADC 0", NULL, "MCLK MODE", is_mclk_mode},
718 {"ADC 1", NULL, "MCLK MODE", is_mclk_mode},
719 {"Front", NULL, "MCLK MODE", is_mclk_mode},
720 {"Surround", NULL, "MCLK MODE", is_mclk_mode},
721
722 {"HP Power", NULL, "LDO1"},
723 {"HP Power", NULL, "LDO2"},
724
725 {"MIC1", NULL, "LDO1"},
726 {"MIC1", NULL, "LDO2"},
727 {"MIC1", NULL, "HV"},
728 {"MIC1", NULL, "VREF"},
729 {"MIC1", NULL, "MIC1 Input Buffer"},
730
731 {"SPO", NULL, "LDO1"},
732 {"SPO", NULL, "LDO2"},
733 {"SPO", NULL, "HV"},
734 {"SPO", NULL, "VREF"},
735
Bard Liao07cf7cba2014-06-20 14:41:13 +0800736 {"DMIC1", NULL, "DMIC1 Pin"},
737 {"DMIC2", NULL, "DMIC2 Pin"},
738 {"DMIC1", NULL, "DMIC Receiver"},
739 {"DMIC2", NULL, "DMIC Receiver"},
740
741 {"RECMIX", "Beep Switch", "Beep"},
742 {"RECMIX", "Line1 Switch", "LINE1"},
743 {"RECMIX", "Mic1 Switch", "MIC1"},
744
745 {"ADC 0 Mux", "Dmic", "DMIC1"},
746 {"ADC 0 Mux", "RECMIX", "RECMIX"},
747 {"ADC 0 Mux", "Mic", "MIC1"},
748 {"ADC 1 Mux", "Dmic", "DMIC2"},
749 {"ADC 1 Mux", "RECMIX", "RECMIX"},
750 {"ADC 1 Mux", "Mic", "MIC1"},
751
752 {"ADC 0", NULL, "ADC 0 Mux"},
753 {"ADC 1", NULL, "ADC 1 Mux"},
754
755 {"AIF1TX", NULL, "ADC 0"},
756 {"AIF2TX", NULL, "ADC 1"},
757
758 {"DAC 0", NULL, "AIF1RX"},
759 {"DAC 1", NULL, "AIF2RX"},
760
761 {"Front", "DAC Switch", "DAC 0"},
762 {"Front", "RECMIX Switch", "RECMIX"},
763
764 {"Surround", NULL, "DAC 1"},
765
766 {"SPK Mux", "Front", "Front"},
767 {"SPK Mux", "Surround", "Surround"},
768
769 {"HPO Mux", "Front", "Front"},
770 {"HPO Mux", "Surround", "Surround"},
771
772 {"SPO", "Switch", "SPK Mux"},
773 {"HPO L", "Switch", "HPO Mux"},
774 {"HPO R", "Switch", "HPO Mux"},
775 {"HPO L", NULL, "HP Power"},
776 {"HPO R", NULL, "HP Power"},
777
778 {"SPOL", NULL, "SPO"},
779 {"SPOR", NULL, "SPO"},
780 {"HPO Pin", NULL, "HPO L"},
781 {"HPO Pin", NULL, "HPO R"},
782};
783
784static int rt286_hw_params(struct snd_pcm_substream *substream,
785 struct snd_pcm_hw_params *params,
786 struct snd_soc_dai *dai)
787{
788 struct snd_soc_codec *codec = dai->codec;
789 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
790 unsigned int val = 0;
791 int d_len_code;
792
793 switch (params_rate(params)) {
794 /* bit 14 0:48K 1:44.1K */
795 case 44100:
796 val |= 0x4000;
797 break;
798 case 48000:
799 break;
800 default:
801 dev_err(codec->dev, "Unsupported sample rate %d\n",
802 params_rate(params));
803 return -EINVAL;
804 }
805 switch (rt286->sys_clk) {
806 case 12288000:
807 case 24576000:
808 if (params_rate(params) != 48000) {
809 dev_err(codec->dev, "Sys_clk is not matched (%d %d)\n",
810 params_rate(params), rt286->sys_clk);
811 return -EINVAL;
812 }
813 break;
814 case 11289600:
815 case 22579200:
816 if (params_rate(params) != 44100) {
817 dev_err(codec->dev, "Sys_clk is not matched (%d %d)\n",
818 params_rate(params), rt286->sys_clk);
819 return -EINVAL;
820 }
821 break;
822 }
823
824 if (params_channels(params) <= 16) {
825 /* bit 3:0 Number of Channel */
826 val |= (params_channels(params) - 1);
827 } else {
828 dev_err(codec->dev, "Unsupported channels %d\n",
829 params_channels(params));
830 return -EINVAL;
831 }
832
833 d_len_code = 0;
834 switch (params_width(params)) {
835 /* bit 6:4 Bits per Sample */
836 case 16:
837 d_len_code = 0;
838 val |= (0x1 << 4);
839 break;
840 case 32:
841 d_len_code = 2;
842 val |= (0x4 << 4);
843 break;
844 case 20:
845 d_len_code = 1;
846 val |= (0x2 << 4);
847 break;
848 case 24:
849 d_len_code = 2;
850 val |= (0x3 << 4);
851 break;
852 case 8:
853 d_len_code = 3;
854 break;
855 default:
856 return -EINVAL;
857 }
858
859 snd_soc_update_bits(codec,
860 RT286_I2S_CTRL1, 0x0018, d_len_code << 3);
861 dev_dbg(codec->dev, "format val = 0x%x\n", val);
862
863 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
864 snd_soc_update_bits(codec, RT286_DAC_FORMAT, 0x407f, val);
865 else
866 snd_soc_update_bits(codec, RT286_ADC_FORMAT, 0x407f, val);
867
868 return 0;
869}
870
871static int rt286_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
872{
873 struct snd_soc_codec *codec = dai->codec;
874
875 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
876 case SND_SOC_DAIFMT_CBM_CFM:
877 snd_soc_update_bits(codec,
878 RT286_I2S_CTRL1, 0x800, 0x800);
879 break;
880 case SND_SOC_DAIFMT_CBS_CFS:
881 snd_soc_update_bits(codec,
882 RT286_I2S_CTRL1, 0x800, 0x0);
883 break;
884 default:
885 return -EINVAL;
886 }
887
888 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
889 case SND_SOC_DAIFMT_I2S:
890 snd_soc_update_bits(codec,
891 RT286_I2S_CTRL1, 0x300, 0x0);
892 break;
893 case SND_SOC_DAIFMT_LEFT_J:
894 snd_soc_update_bits(codec,
895 RT286_I2S_CTRL1, 0x300, 0x1 << 8);
896 break;
897 case SND_SOC_DAIFMT_DSP_A:
898 snd_soc_update_bits(codec,
899 RT286_I2S_CTRL1, 0x300, 0x2 << 8);
900 break;
901 case SND_SOC_DAIFMT_DSP_B:
902 snd_soc_update_bits(codec,
903 RT286_I2S_CTRL1, 0x300, 0x3 << 8);
904 break;
905 default:
906 return -EINVAL;
907 }
908 /* bit 15 Stream Type 0:PCM 1:Non-PCM */
909 snd_soc_update_bits(codec, RT286_DAC_FORMAT, 0x8000, 0);
910 snd_soc_update_bits(codec, RT286_ADC_FORMAT, 0x8000, 0);
911
912 return 0;
913}
914
915static int rt286_set_dai_sysclk(struct snd_soc_dai *dai,
916 int clk_id, unsigned int freq, int dir)
917{
918 struct snd_soc_codec *codec = dai->codec;
919 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
920
921 dev_dbg(codec->dev, "%s freq=%d\n", __func__, freq);
922
923 if (RT286_SCLK_S_MCLK == clk_id) {
924 snd_soc_update_bits(codec,
925 RT286_I2S_CTRL2, 0x0100, 0x0);
926 snd_soc_update_bits(codec,
927 RT286_PLL_CTRL1, 0x20, 0x20);
928 } else {
929 snd_soc_update_bits(codec,
930 RT286_I2S_CTRL2, 0x0100, 0x0100);
931 snd_soc_update_bits(codec,
932 RT286_PLL_CTRL, 0x4, 0x4);
933 snd_soc_update_bits(codec,
934 RT286_PLL_CTRL1, 0x20, 0x0);
935 }
936
937 switch (freq) {
938 case 19200000:
939 if (RT286_SCLK_S_MCLK == clk_id) {
940 dev_err(codec->dev, "Should not use MCLK\n");
941 return -EINVAL;
942 }
943 snd_soc_update_bits(codec,
944 RT286_I2S_CTRL2, 0x40, 0x40);
945 break;
946 case 24000000:
947 if (RT286_SCLK_S_MCLK == clk_id) {
948 dev_err(codec->dev, "Should not use MCLK\n");
949 return -EINVAL;
950 }
951 snd_soc_update_bits(codec,
952 RT286_I2S_CTRL2, 0x40, 0x0);
953 break;
954 case 12288000:
955 case 11289600:
956 snd_soc_update_bits(codec,
957 RT286_I2S_CTRL2, 0x8, 0x0);
958 snd_soc_update_bits(codec,
959 RT286_CLK_DIV, 0xfc1e, 0x0004);
960 break;
961 case 24576000:
962 case 22579200:
963 snd_soc_update_bits(codec,
964 RT286_I2S_CTRL2, 0x8, 0x8);
965 snd_soc_update_bits(codec,
966 RT286_CLK_DIV, 0xfc1e, 0x5406);
967 break;
968 default:
969 dev_err(codec->dev, "Unsupported system clock\n");
970 return -EINVAL;
971 }
972
973 rt286->sys_clk = freq;
Bard Liao6879db72014-10-31 14:52:16 +0800974 rt286->clk_id = clk_id;
Bard Liao07cf7cba2014-06-20 14:41:13 +0800975
976 return 0;
977}
978
979static int rt286_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
980{
981 struct snd_soc_codec *codec = dai->codec;
982
983 dev_dbg(codec->dev, "%s ratio=%d\n", __func__, ratio);
984 if (50 == ratio)
985 snd_soc_update_bits(codec,
986 RT286_I2S_CTRL1, 0x1000, 0x1000);
987 else
988 snd_soc_update_bits(codec,
989 RT286_I2S_CTRL1, 0x1000, 0x0);
990
991
992 return 0;
993}
994
995static int rt286_set_bias_level(struct snd_soc_codec *codec,
996 enum snd_soc_bias_level level)
997{
998 switch (level) {
999 case SND_SOC_BIAS_PREPARE:
Bard Liaobc6c4e42014-07-07 19:15:30 +08001000 if (SND_SOC_BIAS_STANDBY == codec->dapm.bias_level) {
Bard Liao07cf7cba2014-06-20 14:41:13 +08001001 snd_soc_write(codec,
1002 RT286_SET_AUDIO_POWER, AC_PWRST_D0);
Bard Liaobc6c4e42014-07-07 19:15:30 +08001003 snd_soc_update_bits(codec,
1004 RT286_DC_GAIN, 0x200, 0x200);
1005 }
1006 break;
1007
1008 case SND_SOC_BIAS_ON:
1009 mdelay(10);
Bard Liao6879db72014-10-31 14:52:16 +08001010 snd_soc_update_bits(codec,
1011 RT286_CBJ_CTRL1, 0x0400, 0x0400);
1012 snd_soc_update_bits(codec,
1013 RT286_DC_GAIN, 0x200, 0x0);
1014
Bard Liao07cf7cba2014-06-20 14:41:13 +08001015 break;
1016
1017 case SND_SOC_BIAS_STANDBY:
1018 snd_soc_write(codec,
1019 RT286_SET_AUDIO_POWER, AC_PWRST_D3);
Bard Liaobc6c4e42014-07-07 19:15:30 +08001020 snd_soc_update_bits(codec,
Bard Liao6879db72014-10-31 14:52:16 +08001021 RT286_CBJ_CTRL1, 0x0400, 0x0000);
Bard Liao07cf7cba2014-06-20 14:41:13 +08001022 break;
1023
1024 default:
1025 break;
1026 }
1027 codec->dapm.bias_level = level;
1028
1029 return 0;
1030}
1031
1032static irqreturn_t rt286_irq(int irq, void *data)
1033{
1034 struct rt286_priv *rt286 = data;
1035 bool hp = false;
1036 bool mic = false;
1037 int status = 0;
1038
Bard Liao90f601e2014-07-29 13:50:57 +08001039 rt286_jack_detect(rt286, &hp, &mic);
Bard Liao07cf7cba2014-06-20 14:41:13 +08001040
1041 /* Clear IRQ */
Bard Liao90f601e2014-07-29 13:50:57 +08001042 regmap_update_bits(rt286->regmap, RT286_IRQ_CTRL, 0x1, 0x1);
Bard Liao07cf7cba2014-06-20 14:41:13 +08001043
1044 if (hp == true)
1045 status |= SND_JACK_HEADPHONE;
1046
1047 if (mic == true)
1048 status |= SND_JACK_MICROPHONE;
1049
1050 snd_soc_jack_report(rt286->jack, status,
1051 SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
1052
1053 pm_wakeup_event(&rt286->i2c->dev, 300);
1054
1055 return IRQ_HANDLED;
1056}
1057
1058static int rt286_probe(struct snd_soc_codec *codec)
1059{
1060 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
Bard Liao07cf7cba2014-06-20 14:41:13 +08001061
Bard Liao6879db72014-10-31 14:52:16 +08001062 rt286->codec = codec;
Bard Liao07cf7cba2014-06-20 14:41:13 +08001063 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
Bard Liao90f601e2014-07-29 13:50:57 +08001064
1065 if (rt286->i2c->irq) {
1066 regmap_update_bits(rt286->regmap,
1067 RT286_IRQ_CTRL, 0x2, 0x2);
1068
1069 INIT_DELAYED_WORK(&rt286->jack_detect_work,
1070 rt286_jack_detect_work);
1071 schedule_delayed_work(&rt286->jack_detect_work,
1072 msecs_to_jiffies(1250));
1073 }
Bard Liao07cf7cba2014-06-20 14:41:13 +08001074
Bard Liao07cf7cba2014-06-20 14:41:13 +08001075 return 0;
1076}
1077
1078static int rt286_remove(struct snd_soc_codec *codec)
1079{
1080 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
1081
1082 cancel_delayed_work_sync(&rt286->jack_detect_work);
1083
1084 return 0;
1085}
1086
1087#ifdef CONFIG_PM
1088static int rt286_suspend(struct snd_soc_codec *codec)
1089{
1090 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
1091
1092 regcache_cache_only(rt286->regmap, true);
1093 regcache_mark_dirty(rt286->regmap);
1094
1095 return 0;
1096}
1097
1098static int rt286_resume(struct snd_soc_codec *codec)
1099{
1100 struct rt286_priv *rt286 = snd_soc_codec_get_drvdata(codec);
1101
1102 regcache_cache_only(rt286->regmap, false);
1103 rt286_index_sync(codec);
1104 regcache_sync(rt286->regmap);
1105
1106 return 0;
1107}
1108#else
1109#define rt286_suspend NULL
1110#define rt286_resume NULL
1111#endif
1112
1113#define RT286_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
1114#define RT286_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1115 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1116
1117static const struct snd_soc_dai_ops rt286_aif_dai_ops = {
1118 .hw_params = rt286_hw_params,
1119 .set_fmt = rt286_set_dai_fmt,
1120 .set_sysclk = rt286_set_dai_sysclk,
1121 .set_bclk_ratio = rt286_set_bclk_ratio,
1122};
1123
1124static struct snd_soc_dai_driver rt286_dai[] = {
1125 {
1126 .name = "rt286-aif1",
1127 .id = RT286_AIF1,
1128 .playback = {
1129 .stream_name = "AIF1 Playback",
1130 .channels_min = 1,
1131 .channels_max = 2,
1132 .rates = RT286_STEREO_RATES,
1133 .formats = RT286_FORMATS,
1134 },
1135 .capture = {
1136 .stream_name = "AIF1 Capture",
1137 .channels_min = 1,
1138 .channels_max = 2,
1139 .rates = RT286_STEREO_RATES,
1140 .formats = RT286_FORMATS,
1141 },
1142 .ops = &rt286_aif_dai_ops,
1143 .symmetric_rates = 1,
1144 },
1145 {
1146 .name = "rt286-aif2",
1147 .id = RT286_AIF2,
1148 .playback = {
1149 .stream_name = "AIF2 Playback",
1150 .channels_min = 1,
1151 .channels_max = 2,
1152 .rates = RT286_STEREO_RATES,
1153 .formats = RT286_FORMATS,
1154 },
1155 .capture = {
1156 .stream_name = "AIF2 Capture",
1157 .channels_min = 1,
1158 .channels_max = 2,
1159 .rates = RT286_STEREO_RATES,
1160 .formats = RT286_FORMATS,
1161 },
1162 .ops = &rt286_aif_dai_ops,
1163 .symmetric_rates = 1,
1164 },
1165
1166};
1167
1168static struct snd_soc_codec_driver soc_codec_dev_rt286 = {
1169 .probe = rt286_probe,
1170 .remove = rt286_remove,
1171 .suspend = rt286_suspend,
1172 .resume = rt286_resume,
1173 .set_bias_level = rt286_set_bias_level,
1174 .idle_bias_off = true,
1175 .controls = rt286_snd_controls,
1176 .num_controls = ARRAY_SIZE(rt286_snd_controls),
1177 .dapm_widgets = rt286_dapm_widgets,
1178 .num_dapm_widgets = ARRAY_SIZE(rt286_dapm_widgets),
1179 .dapm_routes = rt286_dapm_routes,
1180 .num_dapm_routes = ARRAY_SIZE(rt286_dapm_routes),
1181};
1182
1183static const struct regmap_config rt286_regmap = {
1184 .reg_bits = 32,
1185 .val_bits = 32,
1186 .max_register = 0x02370100,
1187 .volatile_reg = rt286_volatile_register,
1188 .readable_reg = rt286_readable_register,
1189 .reg_write = rt286_hw_write,
1190 .reg_read = rt286_hw_read,
1191 .cache_type = REGCACHE_RBTREE,
1192 .reg_defaults = rt286_reg,
1193 .num_reg_defaults = ARRAY_SIZE(rt286_reg),
1194};
1195
1196static const struct i2c_device_id rt286_i2c_id[] = {
1197 {"rt286", 0},
1198 {}
1199};
1200MODULE_DEVICE_TABLE(i2c, rt286_i2c_id);
1201
1202static const struct acpi_device_id rt286_acpi_match[] = {
1203 { "INT343A", 0 },
1204 {},
1205};
1206MODULE_DEVICE_TABLE(acpi, rt286_acpi_match);
1207
1208static int rt286_i2c_probe(struct i2c_client *i2c,
1209 const struct i2c_device_id *id)
1210{
1211 struct rt286_platform_data *pdata = dev_get_platdata(&i2c->dev);
1212 struct rt286_priv *rt286;
Bard Liao61a414c2014-07-07 16:48:38 +08001213 int i, ret;
Bard Liao07cf7cba2014-06-20 14:41:13 +08001214
1215 rt286 = devm_kzalloc(&i2c->dev, sizeof(*rt286),
1216 GFP_KERNEL);
1217 if (NULL == rt286)
1218 return -ENOMEM;
1219
1220 rt286->regmap = devm_regmap_init(&i2c->dev, NULL, i2c, &rt286_regmap);
1221 if (IS_ERR(rt286->regmap)) {
1222 ret = PTR_ERR(rt286->regmap);
1223 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1224 ret);
1225 return ret;
1226 }
1227
Bard Liao4b21768a2014-07-07 16:48:37 +08001228 regmap_read(rt286->regmap,
1229 RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID), &ret);
1230 if (ret != RT286_VENDOR_ID) {
1231 dev_err(&i2c->dev,
1232 "Device with ID register %x is not rt286\n", ret);
1233 return -ENODEV;
1234 }
1235
Bard Liao07cf7cba2014-06-20 14:41:13 +08001236 rt286->index_cache = rt286_index_def;
1237 rt286->i2c = i2c;
1238 i2c_set_clientdata(i2c, rt286);
1239
1240 if (pdata)
1241 rt286->pdata = *pdata;
1242
Bard Liao61a414c2014-07-07 16:48:38 +08001243 regmap_write(rt286->regmap, RT286_SET_AUDIO_POWER, AC_PWRST_D3);
1244
1245 for (i = 0; i < RT286_POWER_REG_LEN; i++)
1246 regmap_write(rt286->regmap,
1247 RT286_SET_POWER(rt286_support_power_controls[i]),
1248 AC_PWRST_D1);
1249
1250 if (!rt286->pdata.cbj_en) {
1251 regmap_write(rt286->regmap, RT286_CBJ_CTRL2, 0x0000);
1252 regmap_write(rt286->regmap, RT286_MIC1_DET_CTRL, 0x0816);
Bard Liao61a414c2014-07-07 16:48:38 +08001253 regmap_update_bits(rt286->regmap,
1254 RT286_CBJ_CTRL1, 0xf000, 0xb000);
1255 } else {
1256 regmap_update_bits(rt286->regmap,
1257 RT286_CBJ_CTRL1, 0xf000, 0x5000);
1258 }
1259
1260 mdelay(10);
1261
1262 if (!rt286->pdata.gpio2_en)
1263 regmap_write(rt286->regmap, RT286_SET_DMIC2_DEFAULT, 0x4000);
1264 else
1265 regmap_write(rt286->regmap, RT286_SET_DMIC2_DEFAULT, 0);
1266
1267 mdelay(10);
1268
Bard Liao6879db72014-10-31 14:52:16 +08001269 regmap_write(rt286->regmap, RT286_MISC_CTRL1, 0x0000);
1270 /*Power down LDO, VREF*/
1271 regmap_update_bits(rt286->regmap, RT286_POWER_CTRL2, 0xc, 0x0);
1272 regmap_update_bits(rt286->regmap, RT286_POWER_CTRL1, 0x1001, 0x1001);
Bard Liao61a414c2014-07-07 16:48:38 +08001273
Bard Liaobc6c4e42014-07-07 19:15:30 +08001274 /*Set depop parameter*/
1275 regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL2, 0x403a, 0x401a);
1276 regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL3, 0xf777, 0x4737);
1277 regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL4, 0x00ff, 0x003f);
1278
Bard Liao61a414c2014-07-07 16:48:38 +08001279 if (rt286->i2c->irq) {
Bard Liao61a414c2014-07-07 16:48:38 +08001280 ret = request_threaded_irq(rt286->i2c->irq, NULL, rt286_irq,
1281 IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "rt286", rt286);
1282 if (ret != 0) {
1283 dev_err(&i2c->dev,
1284 "Failed to reguest IRQ: %d\n", ret);
1285 return ret;
1286 }
1287 }
1288
Bard Liao07cf7cba2014-06-20 14:41:13 +08001289 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt286,
1290 rt286_dai, ARRAY_SIZE(rt286_dai));
1291
1292 return ret;
1293}
1294
1295static int rt286_i2c_remove(struct i2c_client *i2c)
1296{
1297 struct rt286_priv *rt286 = i2c_get_clientdata(i2c);
1298
1299 if (i2c->irq)
1300 free_irq(i2c->irq, rt286);
1301 snd_soc_unregister_codec(&i2c->dev);
1302
1303 return 0;
1304}
1305
1306
Bard Liao23c4fd52014-07-14 10:18:04 +08001307static struct i2c_driver rt286_i2c_driver = {
Bard Liao07cf7cba2014-06-20 14:41:13 +08001308 .driver = {
1309 .name = "rt286",
1310 .owner = THIS_MODULE,
1311 .acpi_match_table = ACPI_PTR(rt286_acpi_match),
1312 },
1313 .probe = rt286_i2c_probe,
1314 .remove = rt286_i2c_remove,
1315 .id_table = rt286_i2c_id,
1316};
1317
1318module_i2c_driver(rt286_i2c_driver);
1319
1320MODULE_DESCRIPTION("ASoC RT286 driver");
1321MODULE_AUTHOR("Bard Liao <bardliao@realtek.com>");
1322MODULE_LICENSE("GPL");