blob: 3532370255140e03f2f91289b871ff59e98762ee [file] [log] [blame]
Kuninori Morimotofe209b92018-07-30 07:51:41 +00001// SPDX-License-Identifier: GPL-2.0
2//
3// ak4642.c -- AK4642/AK4643 ALSA Soc Audio driver
4//
5// Copyright (C) 2009 Renesas Solutions Corp.
6// Kuninori Morimoto <morimoto.kuninori@renesas.com>
7//
8// Based on wm8731.c by Richard Purdie
9// Based on ak4535.c by Richard Purdie
10// Based on wm8753.c by Liam Girdwood
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090011
12/* ** CAUTION **
13 *
14 * This is very simple driver.
15 * It can use headphone output / stereo input only
16 *
Kuninori Morimoto20211392011-11-06 22:04:53 -080017 * AK4642 is tested.
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090018 * AK4643 is tested.
Kuninori Morimotoa9317e82011-11-10 16:22:05 -080019 * AK4648 is tested.
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090020 */
21
Kuninori Morimoto171a0132015-09-10 06:40:19 +000022#include <linux/clk.h>
23#include <linux/clk-provider.h>
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090024#include <linux/delay.h>
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090025#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Kuninori Morimotobbf14532013-01-10 00:29:11 -080027#include <linux/of_device.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Mark Brown4574cd92013-11-28 18:03:49 +000029#include <linux/regmap.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020030#include <sound/soc.h>
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090031#include <sound/initval.h>
Kuninori Morimotoa300de32010-07-01 14:23:45 +090032#include <sound/tlv.h>
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090033
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090034#define PW_MGMT1 0x00
35#define PW_MGMT2 0x01
36#define SG_SL1 0x02
37#define SG_SL2 0x03
38#define MD_CTL1 0x04
39#define MD_CTL2 0x05
40#define TIMER 0x06
41#define ALC_CTL1 0x07
42#define ALC_CTL2 0x08
43#define L_IVC 0x09
44#define L_DVC 0x0a
45#define ALC_CTL3 0x0b
46#define R_IVC 0x0c
47#define R_DVC 0x0d
48#define MD_CTL3 0x0e
49#define MD_CTL4 0x0f
50#define PW_MGMT3 0x10
51#define DF_S 0x11
52#define FIL3_0 0x12
53#define FIL3_1 0x13
54#define FIL3_2 0x14
55#define FIL3_3 0x15
56#define EQ_0 0x16
57#define EQ_1 0x17
58#define EQ_2 0x18
59#define EQ_3 0x19
60#define EQ_4 0x1a
61#define EQ_5 0x1b
62#define FIL1_0 0x1c
63#define FIL1_1 0x1d
64#define FIL1_2 0x1e
Axel Linf8ea6ce2015-07-01 00:56:36 +080065#define FIL1_3 0x1f /* The maximum valid register for ak4642 */
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090066#define PW_MGMT4 0x20
67#define MD_CTL5 0x21
68#define LO_MS 0x22
69#define HP_MS 0x23
Axel Linf8ea6ce2015-07-01 00:56:36 +080070#define SPK_MS 0x24 /* The maximum valid register for ak4643 */
71#define EQ_FBEQAB 0x25
72#define EQ_FBEQCD 0x26
73#define EQ_FBEQE 0x27 /* The maximum valid register for ak4648 */
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +090074
Kuninori Morimotoa3471232010-10-15 14:23:18 +090075/* PW_MGMT1*/
76#define PMVCM (1 << 6) /* VCOM Power Management */
77#define PMMIN (1 << 5) /* MIN Input Power Management */
78#define PMDAC (1 << 2) /* DAC Power Management */
79#define PMADL (1 << 0) /* MIC Amp Lch and ADC Lch Power Management */
80
Kuninori Morimoto0643ce82010-03-15 18:10:50 +090081/* PW_MGMT2 */
82#define HPMTN (1 << 6)
83#define PMHPL (1 << 5)
84#define PMHPR (1 << 4)
85#define MS (1 << 3) /* master/slave select */
86#define MCKO (1 << 1)
87#define PMPLL (1 << 0)
88
89#define PMHP_MASK (PMHPL | PMHPR)
90#define PMHP PMHP_MASK
91
Kuninori Morimotoa3471232010-10-15 14:23:18 +090092/* PW_MGMT3 */
93#define PMADR (1 << 0) /* MIC L / ADC R Power Management */
94
95/* SG_SL1 */
96#define MINS (1 << 6) /* Switch from MIN to Speaker */
97#define DACL (1 << 4) /* Switch from DAC to Stereo or Receiver */
98#define PMMP (1 << 2) /* MPWR pin Power Management */
99#define MGAIN0 (1 << 0) /* MIC amp gain*/
100
Takeshi Kihara39c26182015-03-24 05:15:22 +0000101/* SG_SL2 */
102#define LOPS (1 << 6) /* Stero Line-out Power Save Mode */
103
Kuninori Morimotoa3471232010-10-15 14:23:18 +0900104/* TIMER */
Sascha Hauerda731842014-05-14 09:37:33 +0200105#define ZTM(param) ((param & 0x3) << 4) /* ALC Zero Crossing TimeOut */
Kuninori Morimotoa3471232010-10-15 14:23:18 +0900106#define WTM(param) (((param & 0x4) << 4) | ((param & 0x3) << 2))
107
108/* ALC_CTL1 */
109#define ALC (1 << 5) /* ALC Enable */
110#define LMTH0 (1 << 0) /* ALC Limiter / Recovery Level */
111
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900112/* MD_CTL1 */
113#define PLL3 (1 << 7)
114#define PLL2 (1 << 6)
115#define PLL1 (1 << 5)
116#define PLL0 (1 << 4)
117#define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
118
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900119#define BCKO_MASK (1 << 3)
120#define BCKO_64 BCKO_MASK
121
Kuninori Morimotocb9c1302011-01-20 11:45:34 +0900122#define DIF_MASK (3 << 0)
123#define DSP (0 << 0)
124#define RIGHT_J (1 << 0)
125#define LEFT_J (2 << 0)
126#define I2S (3 << 0)
127
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900128/* MD_CTL2 */
Kuninori Morimoto544637b2015-09-15 02:42:42 +0000129#define FSs(val) (((val & 0x7) << 0) | ((val & 0x8) << 2))
130#define PSs(val) ((val & 0x3) << 6)
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900131
Kuninori Morimotoa3471232010-10-15 14:23:18 +0900132/* MD_CTL3 */
133#define BST1 (1 << 3)
134
135/* MD_CTL4 */
136#define DACH (1 << 0)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900137
Sascha Hauerd815c702014-05-14 09:37:35 +0200138struct ak4642_drvdata {
139 const struct regmap_config *regmap_config;
Sascha Hauer5cd15e22014-05-14 09:37:36 +0200140 int extended_frequencies;
Sascha Hauerd815c702014-05-14 09:37:35 +0200141};
142
143struct ak4642_priv {
144 const struct ak4642_drvdata *drvdata;
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000145 struct clk *mcko;
Sascha Hauerd815c702014-05-14 09:37:35 +0200146};
147
Kuninori Morimotoa300de32010-07-01 14:23:45 +0900148/*
149 * Playback Volume (table 39)
150 *
151 * max : 0x00 : +12.0 dB
152 * ( 0.5 dB step )
153 * min : 0xFE : -115.0 dB
154 * mute: 0xFF
155 */
Kuninori Morimoto1f99e442012-04-04 23:28:01 -0700156static const DECLARE_TLV_DB_SCALE(out_tlv, -11550, 50, 1);
Kuninori Morimotoa300de32010-07-01 14:23:45 +0900157
158static const struct snd_kcontrol_new ak4642_snd_controls[] = {
159
160 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC,
161 0, 0xFF, 1, out_tlv),
Sascha Hauer370f83a2014-05-14 09:37:34 +0200162 SOC_SINGLE("ALC Capture Switch", ALC_CTL1, 5, 1, 0),
163 SOC_SINGLE("ALC Capture ZC Switch", ALC_CTL1, 4, 1, 1),
Kuninori Morimotoa300de32010-07-01 14:23:45 +0900164};
165
Kuninori Morimotoe555cf32012-02-20 20:14:16 -0800166static const struct snd_kcontrol_new ak4642_headphone_control =
167 SOC_DAPM_SINGLE("Switch", PW_MGMT2, 6, 1, 0);
Kuninori Morimoto24747da2011-11-10 16:21:31 -0800168
Kuninori Morimotoe8c83db2011-11-10 16:21:55 -0800169static const struct snd_kcontrol_new ak4642_lout_mixer_controls[] = {
170 SOC_DAPM_SINGLE("DACL", SG_SL1, 4, 1, 0),
171};
172
Takeshi Kihara39c26182015-03-24 05:15:22 +0000173/* event handlers */
174static int ak4642_lout_event(struct snd_soc_dapm_widget *w,
175 struct snd_kcontrol *kcontrol, int event)
176{
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000177 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
Takeshi Kihara39c26182015-03-24 05:15:22 +0000178
179 switch (event) {
180 case SND_SOC_DAPM_PRE_PMD:
181 case SND_SOC_DAPM_PRE_PMU:
182 /* Power save mode ON */
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000183 snd_soc_component_update_bits(component, SG_SL2, LOPS, LOPS);
Takeshi Kihara39c26182015-03-24 05:15:22 +0000184 break;
185 case SND_SOC_DAPM_POST_PMU:
186 case SND_SOC_DAPM_POST_PMD:
187 /* Power save mode OFF */
Harunobu Kurokawafc1e65c2017-01-11 04:32:43 +0000188 msleep(300);
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000189 snd_soc_component_update_bits(component, SG_SL2, LOPS, 0);
Takeshi Kihara39c26182015-03-24 05:15:22 +0000190 break;
191 }
192
193 return 0;
194}
195
Kuninori Morimoto24747da2011-11-10 16:21:31 -0800196static const struct snd_soc_dapm_widget ak4642_dapm_widgets[] = {
197
198 /* Outputs */
199 SND_SOC_DAPM_OUTPUT("HPOUTL"),
200 SND_SOC_DAPM_OUTPUT("HPOUTR"),
Kuninori Morimotoe8c83db2011-11-10 16:21:55 -0800201 SND_SOC_DAPM_OUTPUT("LINEOUT"),
Kuninori Morimoto24747da2011-11-10 16:21:31 -0800202
Kuninori Morimotoe555cf32012-02-20 20:14:16 -0800203 SND_SOC_DAPM_PGA("HPL Out", PW_MGMT2, 5, 0, NULL, 0),
204 SND_SOC_DAPM_PGA("HPR Out", PW_MGMT2, 4, 0, NULL, 0),
205 SND_SOC_DAPM_SWITCH("Headphone Enable", SND_SOC_NOPM, 0, 0,
206 &ak4642_headphone_control),
Kuninori Morimoto24747da2011-11-10 16:21:31 -0800207
Kuninori Morimotoe555cf32012-02-20 20:14:16 -0800208 SND_SOC_DAPM_PGA("DACH", MD_CTL4, 0, 0, NULL, 0),
Kuninori Morimoto24747da2011-11-10 16:21:31 -0800209
Takeshi Kihara39c26182015-03-24 05:15:22 +0000210 SND_SOC_DAPM_MIXER_E("LINEOUT Mixer", PW_MGMT1, 3, 0,
Kuninori Morimotoe8c83db2011-11-10 16:21:55 -0800211 &ak4642_lout_mixer_controls[0],
Takeshi Kihara39c26182015-03-24 05:15:22 +0000212 ARRAY_SIZE(ak4642_lout_mixer_controls),
213 ak4642_lout_event,
214 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
215 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
Kuninori Morimotoe8c83db2011-11-10 16:21:55 -0800216
Kuninori Morimoto24747da2011-11-10 16:21:31 -0800217 /* DAC */
Kuninori Morimoto379c4b02015-03-16 04:45:54 +0000218 SND_SOC_DAPM_DAC("DAC", NULL, PW_MGMT1, 2, 0),
Kuninori Morimoto24747da2011-11-10 16:21:31 -0800219};
220
221static const struct snd_soc_dapm_route ak4642_intercon[] = {
222
223 /* Outputs */
Kuninori Morimotoe555cf32012-02-20 20:14:16 -0800224 {"HPOUTL", NULL, "HPL Out"},
225 {"HPOUTR", NULL, "HPR Out"},
Kuninori Morimotoe8c83db2011-11-10 16:21:55 -0800226 {"LINEOUT", NULL, "LINEOUT Mixer"},
Kuninori Morimoto24747da2011-11-10 16:21:31 -0800227
Kuninori Morimotoe555cf32012-02-20 20:14:16 -0800228 {"HPL Out", NULL, "Headphone Enable"},
229 {"HPR Out", NULL, "Headphone Enable"},
230
231 {"Headphone Enable", "Switch", "DACH"},
232
233 {"DACH", NULL, "DAC"},
234
Kuninori Morimotoe8c83db2011-11-10 16:21:55 -0800235 {"LINEOUT Mixer", "DACL", "DAC"},
Kuninori Morimoto379c4b02015-03-16 04:45:54 +0000236
237 { "DAC", NULL, "Playback" },
Kuninori Morimoto24747da2011-11-10 16:21:31 -0800238};
Kuninori Morimotoa300de32010-07-01 14:23:45 +0900239
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900240/*
241 * ak4642 register cache
242 */
Axel Linf8ea6ce2015-07-01 00:56:36 +0800243static const struct reg_default ak4643_reg[] = {
Mark Brown4574cd92013-11-28 18:03:49 +0000244 { 0, 0x00 }, { 1, 0x00 }, { 2, 0x01 }, { 3, 0x00 },
245 { 4, 0x02 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
246 { 8, 0xe1 }, { 9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
247 { 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0x08 },
248 { 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
249 { 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
250 { 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
251 { 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
252 { 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
253 { 36, 0x00 },
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900254};
255
Axel Linf8ea6ce2015-07-01 00:56:36 +0800256/* The default settings for 0x0 ~ 0x1f registers are the same for ak4642
257 and ak4643. So we reuse the ak4643 reg_default for ak4642.
258 The valid registers for ak4642 are 0x0 ~ 0x1f which is a subset of ak4643,
259 so define NUM_AK4642_REG_DEFAULTS for ak4642.
260*/
261#define ak4642_reg ak4643_reg
262#define NUM_AK4642_REG_DEFAULTS (FIL1_3 + 1)
263
Mark Brown4574cd92013-11-28 18:03:49 +0000264static const struct reg_default ak4648_reg[] = {
265 { 0, 0x00 }, { 1, 0x00 }, { 2, 0x01 }, { 3, 0x00 },
266 { 4, 0x02 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
267 { 8, 0xe1 }, { 9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
268 { 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0xb8 },
269 { 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
270 { 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
271 { 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
272 { 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
273 { 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
274 { 36, 0x00 }, { 37, 0x88 }, { 38, 0x88 }, { 39, 0x08 },
Kuninori Morimotoa9317e82011-11-10 16:22:05 -0800275};
276
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900277static int ak4642_dai_startup(struct snd_pcm_substream *substream,
278 struct snd_soc_dai *dai)
279{
280 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000281 struct snd_soc_component *component = dai->component;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900282
283 if (is_play) {
284 /*
285 * start headphone output
286 *
287 * PLL, Master Mode
288 * Audio I/F Format :MSB justified (ADC & DAC)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900289 * Bass Boost Level : Middle
290 *
291 * This operation came from example code of
292 * "ASAHI KASEI AK4642" (japanese) manual p97.
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900293 */
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000294 snd_soc_component_write(component, L_IVC, 0x91); /* volume */
295 snd_soc_component_write(component, R_IVC, 0x91); /* volume */
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900296 } else {
297 /*
298 * start stereo input
299 *
300 * PLL Master Mode
301 * Audio I/F Format:MSB justified (ADC & DAC)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900302 * Pre MIC AMP:+20dB
303 * MIC Power On
304 * ALC setting:Refer to Table 35
305 * ALC bit=“1”
306 *
307 * This operation came from example code of
308 * "ASAHI KASEI AK4642" (japanese) manual p94.
309 */
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000310 snd_soc_component_update_bits(component, SG_SL1, PMMP | MGAIN0, PMMP | MGAIN0);
311 snd_soc_component_write(component, TIMER, ZTM(0x3) | WTM(0x3));
312 snd_soc_component_write(component, ALC_CTL1, ALC | LMTH0);
313 snd_soc_component_update_bits(component, PW_MGMT1, PMADL, PMADL);
314 snd_soc_component_update_bits(component, PW_MGMT3, PMADR, PMADR);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900315 }
316
317 return 0;
318}
319
320static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
321 struct snd_soc_dai *dai)
322{
323 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000324 struct snd_soc_component *component = dai->component;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900325
326 if (is_play) {
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900327 } else {
328 /* stop stereo input */
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000329 snd_soc_component_update_bits(component, PW_MGMT1, PMADL, 0);
330 snd_soc_component_update_bits(component, PW_MGMT3, PMADR, 0);
331 snd_soc_component_update_bits(component, ALC_CTL1, ALC, 0);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900332 }
333}
334
335static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
336 int clk_id, unsigned int freq, int dir)
337{
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000338 struct snd_soc_component *component = codec_dai->component;
339 struct ak4642_priv *priv = snd_soc_component_get_drvdata(component);
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900340 u8 pll;
Sascha Hauer5cd15e22014-05-14 09:37:36 +0200341 int extended_freq = 0;
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900342
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900343 switch (freq) {
344 case 11289600:
345 pll = PLL2;
346 break;
347 case 12288000:
348 pll = PLL2 | PLL0;
349 break;
350 case 12000000:
351 pll = PLL2 | PLL1;
352 break;
353 case 24000000:
354 pll = PLL2 | PLL1 | PLL0;
355 break;
356 case 13500000:
357 pll = PLL3 | PLL2;
358 break;
359 case 27000000:
360 pll = PLL3 | PLL2 | PLL0;
361 break;
Sascha Hauer5cd15e22014-05-14 09:37:36 +0200362 case 19200000:
363 pll = PLL3;
364 extended_freq = 1;
365 break;
366 case 13000000:
367 pll = PLL3 | PLL2 | PLL1;
368 extended_freq = 1;
369 break;
370 case 26000000:
371 pll = PLL3 | PLL2 | PLL1 | PLL0;
372 extended_freq = 1;
373 break;
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900374 default:
375 return -EINVAL;
376 }
Sascha Hauer5cd15e22014-05-14 09:37:36 +0200377
378 if (extended_freq && !priv->drvdata->extended_frequencies)
379 return -EINVAL;
380
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000381 snd_soc_component_update_bits(component, MD_CTL1, PLL_MASK, pll);
Kuninori Morimoto4b6316b2010-03-23 16:27:28 +0900382
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900383 return 0;
384}
385
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900386static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
387{
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000388 struct snd_soc_component *component = dai->component;
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900389 u8 data;
390 u8 bcko;
391
392 data = MCKO | PMPLL; /* use MCKO */
393 bcko = 0;
394
395 /* set master/slave audio interface */
396 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
397 case SND_SOC_DAIFMT_CBM_CFM:
398 data |= MS;
399 bcko = BCKO_64;
400 break;
401 case SND_SOC_DAIFMT_CBS_CFS:
402 break;
403 default:
404 return -EINVAL;
405 }
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000406 snd_soc_component_update_bits(component, PW_MGMT2, MS | MCKO | PMPLL, data);
407 snd_soc_component_update_bits(component, MD_CTL1, BCKO_MASK, bcko);
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900408
Kuninori Morimotocb9c1302011-01-20 11:45:34 +0900409 /* format type */
410 data = 0;
411 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
412 case SND_SOC_DAIFMT_LEFT_J:
413 data = LEFT_J;
414 break;
415 case SND_SOC_DAIFMT_I2S:
416 data = I2S;
417 break;
418 /* FIXME
419 * Please add RIGHT_J / DSP support here
420 */
421 default:
422 return -EINVAL;
Kuninori Morimotocb9c1302011-01-20 11:45:34 +0900423 }
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000424 snd_soc_component_update_bits(component, MD_CTL1, DIF_MASK, data);
Kuninori Morimotocb9c1302011-01-20 11:45:34 +0900425
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900426 return 0;
427}
428
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000429static int ak4642_set_mcko(struct snd_soc_component *component,
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000430 u32 frequency)
431{
Colin Ian King836e4fe2017-06-30 10:16:36 +0100432 static const u32 fs_list[] = {
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000433 [0] = 8000,
434 [1] = 12000,
435 [2] = 16000,
436 [3] = 24000,
437 [4] = 7350,
438 [5] = 11025,
439 [6] = 14700,
440 [7] = 22050,
441 [10] = 32000,
442 [11] = 48000,
443 [14] = 29400,
444 [15] = 44100,
445 };
Colin Ian King836e4fe2017-06-30 10:16:36 +0100446 static const u32 ps_list[] = {
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000447 [0] = 256,
448 [1] = 128,
449 [2] = 64,
450 [3] = 32
451 };
452 int ps, fs;
453
454 for (ps = 0; ps < ARRAY_SIZE(ps_list); ps++) {
455 for (fs = 0; fs < ARRAY_SIZE(fs_list); fs++) {
456 if (frequency == ps_list[ps] * fs_list[fs]) {
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000457 snd_soc_component_write(component, MD_CTL2,
Kuninori Morimoto544637b2015-09-15 02:42:42 +0000458 PSs(ps) | FSs(fs));
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000459 return 0;
460 }
461 }
462 }
463
464 return 0;
465}
466
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900467static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
468 struct snd_pcm_hw_params *params,
469 struct snd_soc_dai *dai)
470{
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000471 struct snd_soc_component *component = dai->component;
472 struct ak4642_priv *priv = snd_soc_component_get_drvdata(component);
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000473 u32 rate = clk_get_rate(priv->mcko);
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900474
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000475 if (!rate)
476 rate = params_rate(params) * 256;
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900477
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000478 return ak4642_set_mcko(component, rate);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900479}
480
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000481static int ak4642_set_bias_level(struct snd_soc_component *component,
Kuninori Morimotoed2dd7d2011-11-10 16:21:01 -0800482 enum snd_soc_bias_level level)
483{
484 switch (level) {
485 case SND_SOC_BIAS_OFF:
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000486 snd_soc_component_write(component, PW_MGMT1, 0x00);
Kuninori Morimotoed2dd7d2011-11-10 16:21:01 -0800487 break;
488 default:
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000489 snd_soc_component_update_bits(component, PW_MGMT1, PMVCM, PMVCM);
Kuninori Morimotoed2dd7d2011-11-10 16:21:01 -0800490 break;
491 }
Kuninori Morimotoed2dd7d2011-11-10 16:21:01 -0800492
493 return 0;
494}
495
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100496static const struct snd_soc_dai_ops ak4642_dai_ops = {
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900497 .startup = ak4642_dai_startup,
498 .shutdown = ak4642_dai_shutdown,
499 .set_sysclk = ak4642_dai_set_sysclk,
Kuninori Morimoto0643ce82010-03-15 18:10:50 +0900500 .set_fmt = ak4642_dai_set_fmt,
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900501 .hw_params = ak4642_dai_hw_params,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900502};
503
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000504static struct snd_soc_dai_driver ak4642_dai = {
505 .name = "ak4642-hifi",
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900506 .playback = {
507 .stream_name = "Playback",
Kuninori Morimotoc01673e2015-04-10 08:46:22 +0000508 .channels_min = 2,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900509 .channels_max = 2,
510 .rates = SNDRV_PCM_RATE_8000_48000,
511 .formats = SNDRV_PCM_FMTBIT_S16_LE },
512 .capture = {
513 .stream_name = "Capture",
Kuninori Morimotoc01673e2015-04-10 08:46:22 +0000514 .channels_min = 2,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900515 .channels_max = 2,
516 .rates = SNDRV_PCM_RATE_8000_48000,
517 .formats = SNDRV_PCM_FMTBIT_S16_LE },
518 .ops = &ak4642_dai_ops,
Kuninori Morimoto1ad747c2010-03-23 16:27:38 +0900519 .symmetric_rates = 1,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900520};
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900521
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000522static int ak4642_suspend(struct snd_soc_component *component)
Peter Ujfalusia2ebd582016-05-31 13:34:59 +0300523{
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000524 struct regmap *regmap = dev_get_regmap(component->dev, NULL);
Peter Ujfalusia2ebd582016-05-31 13:34:59 +0300525
526 regcache_cache_only(regmap, true);
527 regcache_mark_dirty(regmap);
528 return 0;
529}
530
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000531static int ak4642_resume(struct snd_soc_component *component)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900532{
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000533 struct regmap *regmap = dev_get_regmap(component->dev, NULL);
Mark Brown4574cd92013-11-28 18:03:49 +0000534
Peter Ujfalusia2ebd582016-05-31 13:34:59 +0300535 regcache_cache_only(regmap, false);
Mark Brown4574cd92013-11-28 18:03:49 +0000536 regcache_sync(regmap);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900537 return 0;
538}
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000539static int ak4642_probe(struct snd_soc_component *component)
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000540{
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000541 struct ak4642_priv *priv = snd_soc_component_get_drvdata(component);
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000542
543 if (priv->mcko)
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000544 ak4642_set_mcko(component, clk_get_rate(priv->mcko));
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000545
546 return 0;
547}
548
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000549static const struct snd_soc_component_driver soc_component_dev_ak4642 = {
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000550 .probe = ak4642_probe,
Peter Ujfalusia2ebd582016-05-31 13:34:59 +0300551 .suspend = ak4642_suspend,
Kuninori Morimoto0ce75aa2010-09-16 13:06:40 +0900552 .resume = ak4642_resume,
Kuninori Morimotoed2dd7d2011-11-10 16:21:01 -0800553 .set_bias_level = ak4642_set_bias_level,
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000554 .controls = ak4642_snd_controls,
555 .num_controls = ARRAY_SIZE(ak4642_snd_controls),
556 .dapm_widgets = ak4642_dapm_widgets,
557 .num_dapm_widgets = ARRAY_SIZE(ak4642_dapm_widgets),
558 .dapm_routes = ak4642_intercon,
559 .num_dapm_routes = ARRAY_SIZE(ak4642_intercon),
560 .idle_bias_on = 1,
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000561 .endianness = 1,
562 .non_legacy_dai_naming = 1,
Kuninori Morimotoa9317e82011-11-10 16:22:05 -0800563};
564
Mark Brown4574cd92013-11-28 18:03:49 +0000565static const struct regmap_config ak4642_regmap = {
566 .reg_bits = 8,
567 .val_bits = 8,
Axel Linf8ea6ce2015-07-01 00:56:36 +0800568 .max_register = FIL1_3,
Mark Brown4574cd92013-11-28 18:03:49 +0000569 .reg_defaults = ak4642_reg,
Axel Linf8ea6ce2015-07-01 00:56:36 +0800570 .num_reg_defaults = NUM_AK4642_REG_DEFAULTS,
Mark Brownd3030d12016-05-18 18:30:39 +0100571 .cache_type = REGCACHE_RBTREE,
Axel Linf8ea6ce2015-07-01 00:56:36 +0800572};
573
574static const struct regmap_config ak4643_regmap = {
575 .reg_bits = 8,
576 .val_bits = 8,
577 .max_register = SPK_MS,
578 .reg_defaults = ak4643_reg,
579 .num_reg_defaults = ARRAY_SIZE(ak4643_reg),
Mark Brownd3030d12016-05-18 18:30:39 +0100580 .cache_type = REGCACHE_RBTREE,
Mark Brown4574cd92013-11-28 18:03:49 +0000581};
582
583static const struct regmap_config ak4648_regmap = {
584 .reg_bits = 8,
585 .val_bits = 8,
Axel Linf8ea6ce2015-07-01 00:56:36 +0800586 .max_register = EQ_FBEQE,
Mark Brown4574cd92013-11-28 18:03:49 +0000587 .reg_defaults = ak4648_reg,
588 .num_reg_defaults = ARRAY_SIZE(ak4648_reg),
Mark Brownd3030d12016-05-18 18:30:39 +0100589 .cache_type = REGCACHE_RBTREE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590};
591
Sascha Hauerd815c702014-05-14 09:37:35 +0200592static const struct ak4642_drvdata ak4642_drvdata = {
593 .regmap_config = &ak4642_regmap,
594};
595
596static const struct ak4642_drvdata ak4643_drvdata = {
Axel Linf8ea6ce2015-07-01 00:56:36 +0800597 .regmap_config = &ak4643_regmap,
Sascha Hauerd815c702014-05-14 09:37:35 +0200598};
599
600static const struct ak4642_drvdata ak4648_drvdata = {
601 .regmap_config = &ak4648_regmap,
Sascha Hauer5cd15e22014-05-14 09:37:36 +0200602 .extended_frequencies = 1,
Sascha Hauerd815c702014-05-14 09:37:35 +0200603};
604
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000605#ifdef CONFIG_COMMON_CLK
606static struct clk *ak4642_of_parse_mcko(struct device *dev)
607{
608 struct device_node *np = dev->of_node;
609 struct clk *clk;
610 const char *clk_name = np->name;
611 const char *parent_clk_name = NULL;
612 u32 rate;
613
614 if (of_property_read_u32(np, "clock-frequency", &rate))
615 return NULL;
616
617 if (of_property_read_bool(np, "clocks"))
618 parent_clk_name = of_clk_get_parent_name(np, 0);
619
620 of_property_read_string(np, "clock-output-names", &clk_name);
621
Stephen Boydb6bf3282016-04-19 18:05:04 -0700622 clk = clk_register_fixed_rate(dev, clk_name, parent_clk_name, 0, rate);
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000623 if (!IS_ERR(clk))
624 of_clk_add_provider(np, of_clk_src_simple_get, clk);
625
626 return clk;
627}
628#else
629#define ak4642_of_parse_mcko(d) 0
630#endif
631
Kiran Padwal27204ca2014-07-28 11:41:04 +0530632static const struct of_device_id ak4642_of_match[];
Bill Pemberton7a79e942012-12-07 09:26:37 -0500633static int ak4642_i2c_probe(struct i2c_client *i2c,
634 const struct i2c_device_id *id)
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900635{
Kuninori Morimoto2719a752015-09-10 06:39:59 +0000636 struct device *dev = &i2c->dev;
637 struct device_node *np = dev->of_node;
Sascha Hauerd815c702014-05-14 09:37:35 +0200638 const struct ak4642_drvdata *drvdata = NULL;
Mark Brown4574cd92013-11-28 18:03:49 +0000639 struct regmap *regmap;
Sascha Hauerd815c702014-05-14 09:37:35 +0200640 struct ak4642_priv *priv;
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000641 struct clk *mcko = NULL;
Kuninori Morimotobbf14532013-01-10 00:29:11 -0800642
Kuninori Morimotobbf14532013-01-10 00:29:11 -0800643 if (np) {
644 const struct of_device_id *of_id;
645
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000646 mcko = ak4642_of_parse_mcko(dev);
647 if (IS_ERR(mcko))
648 mcko = NULL;
649
Kuninori Morimoto2719a752015-09-10 06:39:59 +0000650 of_id = of_match_device(ak4642_of_match, dev);
Kuninori Morimotobbf14532013-01-10 00:29:11 -0800651 if (of_id)
Sascha Hauerd815c702014-05-14 09:37:35 +0200652 drvdata = of_id->data;
Kuninori Morimotobbf14532013-01-10 00:29:11 -0800653 } else {
Sascha Hauerd815c702014-05-14 09:37:35 +0200654 drvdata = (const struct ak4642_drvdata *)id->driver_data;
Kuninori Morimotobbf14532013-01-10 00:29:11 -0800655 }
656
Sascha Hauerd815c702014-05-14 09:37:35 +0200657 if (!drvdata) {
Kuninori Morimoto2719a752015-09-10 06:39:59 +0000658 dev_err(dev, "Unknown device type\n");
Kuninori Morimotobbf14532013-01-10 00:29:11 -0800659 return -EINVAL;
660 }
661
Kuninori Morimoto2719a752015-09-10 06:39:59 +0000662 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
Sascha Hauerd815c702014-05-14 09:37:35 +0200663 if (!priv)
664 return -ENOMEM;
665
666 priv->drvdata = drvdata;
Kuninori Morimoto171a0132015-09-10 06:40:19 +0000667 priv->mcko = mcko;
Sascha Hauerd815c702014-05-14 09:37:35 +0200668
669 i2c_set_clientdata(i2c, priv);
670
671 regmap = devm_regmap_init_i2c(i2c, drvdata->regmap_config);
Mark Brown4574cd92013-11-28 18:03:49 +0000672 if (IS_ERR(regmap))
673 return PTR_ERR(regmap);
674
Kuninori Morimoto43c5d0f2018-01-29 03:13:07 +0000675 return devm_snd_soc_register_component(dev,
676 &soc_component_dev_ak4642, &ak4642_dai, 1);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900677}
678
Kiran Padwal27204ca2014-07-28 11:41:04 +0530679static const struct of_device_id ak4642_of_match[] = {
Sascha Hauerd815c702014-05-14 09:37:35 +0200680 { .compatible = "asahi-kasei,ak4642", .data = &ak4642_drvdata},
681 { .compatible = "asahi-kasei,ak4643", .data = &ak4643_drvdata},
682 { .compatible = "asahi-kasei,ak4648", .data = &ak4648_drvdata},
Kuninori Morimotobbf14532013-01-10 00:29:11 -0800683 {},
684};
685MODULE_DEVICE_TABLE(of, ak4642_of_match);
686
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900687static const struct i2c_device_id ak4642_i2c_id[] = {
Sascha Hauerd815c702014-05-14 09:37:35 +0200688 { "ak4642", (kernel_ulong_t)&ak4642_drvdata },
689 { "ak4643", (kernel_ulong_t)&ak4643_drvdata },
690 { "ak4648", (kernel_ulong_t)&ak4648_drvdata },
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900691 { }
692};
693MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
694
695static struct i2c_driver ak4642_i2c_driver = {
696 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 .name = "ak4642-codec",
Kuninori Morimotobbf14532013-01-10 00:29:11 -0800698 .of_match_table = ak4642_of_match,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900699 },
Kuninori Morimoto0ce75aa2010-09-16 13:06:40 +0900700 .probe = ak4642_i2c_probe,
Kuninori Morimoto0ce75aa2010-09-16 13:06:40 +0900701 .id_table = ak4642_i2c_id,
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900702};
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900703
Mark Brown2f54d2a2013-11-28 17:24:55 +0000704module_i2c_driver(ak4642_i2c_driver);
Kuninori Morimotoa3a83d92009-08-21 10:23:41 +0900705
706MODULE_DESCRIPTION("Soc AK4642 driver");
707MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
Kuninori Morimotofe209b92018-07-30 07:51:41 +0000708MODULE_LICENSE("GPL v2");