ak4641.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ak4641.c -- AK4641 ALSA Soc Audio driver
  4. *
  5. * Copyright (C) 2008 Harald Welte <laforge@gnufiish.org>
  6. * Copyright (C) 2011 Dmitry Artamonow <mad_soft@inbox.ru>
  7. *
  8. * Based on ak4535.c by Richard Purdie
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/gpio.h>
  14. #include <linux/pm.h>
  15. #include <linux/i2c.h>
  16. #include <linux/regmap.h>
  17. #include <linux/slab.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <sound/initval.h>
  23. #include <sound/tlv.h>
  24. #include <sound/ak4641.h>
  25. /* AK4641 register space */
  26. #define AK4641_PM1 0x00
  27. #define AK4641_PM2 0x01
  28. #define AK4641_SIG1 0x02
  29. #define AK4641_SIG2 0x03
  30. #define AK4641_MODE1 0x04
  31. #define AK4641_MODE2 0x05
  32. #define AK4641_DAC 0x06
  33. #define AK4641_MIC 0x07
  34. #define AK4641_TIMER 0x08
  35. #define AK4641_ALC1 0x09
  36. #define AK4641_ALC2 0x0a
  37. #define AK4641_PGA 0x0b
  38. #define AK4641_LATT 0x0c
  39. #define AK4641_RATT 0x0d
  40. #define AK4641_VOL 0x0e
  41. #define AK4641_STATUS 0x0f
  42. #define AK4641_EQLO 0x10
  43. #define AK4641_EQMID 0x11
  44. #define AK4641_EQHI 0x12
  45. #define AK4641_BTIF 0x13
  46. /* codec private data */
  47. struct ak4641_priv {
  48. struct regmap *regmap;
  49. unsigned int sysclk;
  50. int deemph;
  51. int playback_fs;
  52. };
  53. /*
  54. * ak4641 register cache
  55. */
  56. static const struct reg_default ak4641_reg_defaults[] = {
  57. { 0, 0x00 }, { 1, 0x80 }, { 2, 0x00 }, { 3, 0x80 },
  58. { 4, 0x02 }, { 5, 0x00 }, { 6, 0x11 }, { 7, 0x05 },
  59. { 8, 0x00 }, { 9, 0x00 }, { 10, 0x36 }, { 11, 0x10 },
  60. { 12, 0x00 }, { 13, 0x00 }, { 14, 0x57 }, { 15, 0x00 },
  61. { 16, 0x88 }, { 17, 0x88 }, { 18, 0x08 }, { 19, 0x08 }
  62. };
  63. static const int deemph_settings[] = {44100, 0, 48000, 32000};
  64. static int ak4641_set_deemph(struct snd_soc_component *component)
  65. {
  66. struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
  67. int i, best = 0;
  68. for (i = 0 ; i < ARRAY_SIZE(deemph_settings); i++) {
  69. /* if deemphasis is on, select the nearest available rate */
  70. if (ak4641->deemph && deemph_settings[i] != 0 &&
  71. abs(deemph_settings[i] - ak4641->playback_fs) <
  72. abs(deemph_settings[best] - ak4641->playback_fs))
  73. best = i;
  74. if (!ak4641->deemph && deemph_settings[i] == 0)
  75. best = i;
  76. }
  77. dev_dbg(component->dev, "Set deemphasis %d\n", best);
  78. return snd_soc_component_update_bits(component, AK4641_DAC, 0x3, best);
  79. }
  80. static int ak4641_put_deemph(struct snd_kcontrol *kcontrol,
  81. struct snd_ctl_elem_value *ucontrol)
  82. {
  83. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  84. struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
  85. int deemph = ucontrol->value.integer.value[0];
  86. if (deemph > 1)
  87. return -EINVAL;
  88. ak4641->deemph = deemph;
  89. return ak4641_set_deemph(component);
  90. }
  91. static int ak4641_get_deemph(struct snd_kcontrol *kcontrol,
  92. struct snd_ctl_elem_value *ucontrol)
  93. {
  94. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  95. struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
  96. ucontrol->value.integer.value[0] = ak4641->deemph;
  97. return 0;
  98. };
  99. static const char *ak4641_mono_out[] = {"(L + R)/2", "Hi-Z"};
  100. static const char *ak4641_hp_out[] = {"Stereo", "Mono"};
  101. static const char *ak4641_mic_select[] = {"Internal", "External"};
  102. static const char *ak4641_mic_or_dac[] = {"Microphone", "Voice DAC"};
  103. static const DECLARE_TLV_DB_SCALE(mono_gain_tlv, -1700, 2300, 0);
  104. static const DECLARE_TLV_DB_SCALE(mic_boost_tlv, 0, 2000, 0);
  105. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1050, 150, 0);
  106. static const DECLARE_TLV_DB_SCALE(master_tlv, -12750, 50, 0);
  107. static const DECLARE_TLV_DB_SCALE(mic_stereo_sidetone_tlv, -2700, 300, 0);
  108. static const DECLARE_TLV_DB_SCALE(mic_mono_sidetone_tlv, -400, 400, 0);
  109. static const DECLARE_TLV_DB_SCALE(capture_tlv, -800, 50, 0);
  110. static const DECLARE_TLV_DB_SCALE(alc_tlv, -800, 50, 0);
  111. static const DECLARE_TLV_DB_SCALE(aux_in_tlv, -2100, 300, 0);
  112. static SOC_ENUM_SINGLE_DECL(ak4641_mono_out_enum,
  113. AK4641_SIG1, 6, ak4641_mono_out);
  114. static SOC_ENUM_SINGLE_DECL(ak4641_hp_out_enum,
  115. AK4641_MODE2, 2, ak4641_hp_out);
  116. static SOC_ENUM_SINGLE_DECL(ak4641_mic_select_enum,
  117. AK4641_MIC, 1, ak4641_mic_select);
  118. static SOC_ENUM_SINGLE_DECL(ak4641_mic_or_dac_enum,
  119. AK4641_BTIF, 4, ak4641_mic_or_dac);
  120. static const struct snd_kcontrol_new ak4641_snd_controls[] = {
  121. SOC_ENUM("Mono 1 Output", ak4641_mono_out_enum),
  122. SOC_SINGLE_TLV("Mono 1 Gain Volume", AK4641_SIG1, 7, 1, 1,
  123. mono_gain_tlv),
  124. SOC_ENUM("Headphone Output", ak4641_hp_out_enum),
  125. SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
  126. ak4641_get_deemph, ak4641_put_deemph),
  127. SOC_SINGLE_TLV("Mic Boost Volume", AK4641_MIC, 0, 1, 0, mic_boost_tlv),
  128. SOC_SINGLE("ALC Operation Time", AK4641_TIMER, 0, 3, 0),
  129. SOC_SINGLE("ALC Recovery Time", AK4641_TIMER, 2, 3, 0),
  130. SOC_SINGLE("ALC ZC Time", AK4641_TIMER, 4, 3, 0),
  131. SOC_SINGLE("ALC 1 Switch", AK4641_ALC1, 5, 1, 0),
  132. SOC_SINGLE_TLV("ALC Volume", AK4641_ALC2, 0, 71, 0, alc_tlv),
  133. SOC_SINGLE("Left Out Enable Switch", AK4641_SIG2, 1, 1, 0),
  134. SOC_SINGLE("Right Out Enable Switch", AK4641_SIG2, 0, 1, 0),
  135. SOC_SINGLE_TLV("Capture Volume", AK4641_PGA, 0, 71, 0, capture_tlv),
  136. SOC_DOUBLE_R_TLV("Master Playback Volume", AK4641_LATT,
  137. AK4641_RATT, 0, 255, 1, master_tlv),
  138. SOC_SINGLE_TLV("AUX In Volume", AK4641_VOL, 0, 15, 0, aux_in_tlv),
  139. SOC_SINGLE("Equalizer Switch", AK4641_DAC, 2, 1, 0),
  140. SOC_SINGLE_TLV("EQ1 100 Hz Volume", AK4641_EQLO, 0, 15, 1, eq_tlv),
  141. SOC_SINGLE_TLV("EQ2 250 Hz Volume", AK4641_EQLO, 4, 15, 1, eq_tlv),
  142. SOC_SINGLE_TLV("EQ3 1 kHz Volume", AK4641_EQMID, 0, 15, 1, eq_tlv),
  143. SOC_SINGLE_TLV("EQ4 3.5 kHz Volume", AK4641_EQMID, 4, 15, 1, eq_tlv),
  144. SOC_SINGLE_TLV("EQ5 10 kHz Volume", AK4641_EQHI, 0, 15, 1, eq_tlv),
  145. };
  146. /* Mono 1 Mixer */
  147. static const struct snd_kcontrol_new ak4641_mono1_mixer_controls[] = {
  148. SOC_DAPM_SINGLE_TLV("Mic Mono Sidetone Volume", AK4641_VOL, 7, 1, 0,
  149. mic_mono_sidetone_tlv),
  150. SOC_DAPM_SINGLE("Mic Mono Sidetone Switch", AK4641_SIG1, 4, 1, 0),
  151. SOC_DAPM_SINGLE("Mono Playback Switch", AK4641_SIG1, 5, 1, 0),
  152. };
  153. /* Stereo Mixer */
  154. static const struct snd_kcontrol_new ak4641_stereo_mixer_controls[] = {
  155. SOC_DAPM_SINGLE_TLV("Mic Sidetone Volume", AK4641_VOL, 4, 7, 0,
  156. mic_stereo_sidetone_tlv),
  157. SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4641_SIG2, 4, 1, 0),
  158. SOC_DAPM_SINGLE("Playback Switch", AK4641_SIG2, 7, 1, 0),
  159. SOC_DAPM_SINGLE("Aux Bypass Switch", AK4641_SIG2, 5, 1, 0),
  160. };
  161. /* Input Mixer */
  162. static const struct snd_kcontrol_new ak4641_input_mixer_controls[] = {
  163. SOC_DAPM_SINGLE("Mic Capture Switch", AK4641_MIC, 2, 1, 0),
  164. SOC_DAPM_SINGLE("Aux Capture Switch", AK4641_MIC, 5, 1, 0),
  165. };
  166. /* Mic mux */
  167. static const struct snd_kcontrol_new ak4641_mic_mux_control =
  168. SOC_DAPM_ENUM("Mic Select", ak4641_mic_select_enum);
  169. /* Input mux */
  170. static const struct snd_kcontrol_new ak4641_input_mux_control =
  171. SOC_DAPM_ENUM("Input Select", ak4641_mic_or_dac_enum);
  172. /* mono 2 switch */
  173. static const struct snd_kcontrol_new ak4641_mono2_control =
  174. SOC_DAPM_SINGLE("Switch", AK4641_SIG1, 0, 1, 0);
  175. /* ak4641 dapm widgets */
  176. static const struct snd_soc_dapm_widget ak4641_dapm_widgets[] = {
  177. SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM, 0, 0,
  178. &ak4641_stereo_mixer_controls[0],
  179. ARRAY_SIZE(ak4641_stereo_mixer_controls)),
  180. SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM, 0, 0,
  181. &ak4641_mono1_mixer_controls[0],
  182. ARRAY_SIZE(ak4641_mono1_mixer_controls)),
  183. SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0,
  184. &ak4641_input_mixer_controls[0],
  185. ARRAY_SIZE(ak4641_input_mixer_controls)),
  186. SND_SOC_DAPM_MUX("Mic Mux", SND_SOC_NOPM, 0, 0,
  187. &ak4641_mic_mux_control),
  188. SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
  189. &ak4641_input_mux_control),
  190. SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM, 0, 0,
  191. &ak4641_mono2_control),
  192. SND_SOC_DAPM_OUTPUT("LOUT"),
  193. SND_SOC_DAPM_OUTPUT("ROUT"),
  194. SND_SOC_DAPM_OUTPUT("MOUT1"),
  195. SND_SOC_DAPM_OUTPUT("MOUT2"),
  196. SND_SOC_DAPM_OUTPUT("MICOUT"),
  197. SND_SOC_DAPM_ADC("ADC", "HiFi Capture", AK4641_PM1, 0, 0),
  198. SND_SOC_DAPM_PGA("Mic", AK4641_PM1, 1, 0, NULL, 0),
  199. SND_SOC_DAPM_PGA("AUX In", AK4641_PM1, 2, 0, NULL, 0),
  200. SND_SOC_DAPM_PGA("Mono Out", AK4641_PM1, 3, 0, NULL, 0),
  201. SND_SOC_DAPM_PGA("Line Out", AK4641_PM1, 4, 0, NULL, 0),
  202. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", AK4641_PM2, 0, 0),
  203. SND_SOC_DAPM_PGA("Mono Out 2", AK4641_PM2, 3, 0, NULL, 0),
  204. SND_SOC_DAPM_ADC("Voice ADC", "Voice Capture", AK4641_BTIF, 0, 0),
  205. SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", AK4641_BTIF, 1, 0),
  206. SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4641_MIC, 3, 0),
  207. SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4641_MIC, 4, 0),
  208. SND_SOC_DAPM_INPUT("MICIN"),
  209. SND_SOC_DAPM_INPUT("MICEXT"),
  210. SND_SOC_DAPM_INPUT("AUX"),
  211. SND_SOC_DAPM_INPUT("AIN"),
  212. };
  213. static const struct snd_soc_dapm_route ak4641_audio_map[] = {
  214. /* Stereo Mixer */
  215. {"Stereo Mixer", "Playback Switch", "DAC"},
  216. {"Stereo Mixer", "Mic Sidetone Switch", "Input Mux"},
  217. {"Stereo Mixer", "Aux Bypass Switch", "AUX In"},
  218. /* Mono 1 Mixer */
  219. {"Mono1 Mixer", "Mic Mono Sidetone Switch", "Input Mux"},
  220. {"Mono1 Mixer", "Mono Playback Switch", "DAC"},
  221. /* Mic */
  222. {"Mic", NULL, "AIN"},
  223. {"Mic Mux", "Internal", "Mic Int Bias"},
  224. {"Mic Mux", "External", "Mic Ext Bias"},
  225. {"Mic Int Bias", NULL, "MICIN"},
  226. {"Mic Ext Bias", NULL, "MICEXT"},
  227. {"MICOUT", NULL, "Mic Mux"},
  228. /* Input Mux */
  229. {"Input Mux", "Microphone", "Mic"},
  230. {"Input Mux", "Voice DAC", "Voice DAC"},
  231. /* Line Out */
  232. {"LOUT", NULL, "Line Out"},
  233. {"ROUT", NULL, "Line Out"},
  234. {"Line Out", NULL, "Stereo Mixer"},
  235. /* Mono 1 Out */
  236. {"MOUT1", NULL, "Mono Out"},
  237. {"Mono Out", NULL, "Mono1 Mixer"},
  238. /* Mono 2 Out */
  239. {"MOUT2", NULL, "Mono 2 Enable"},
  240. {"Mono 2 Enable", "Switch", "Mono Out 2"},
  241. {"Mono Out 2", NULL, "Stereo Mixer"},
  242. {"Voice ADC", NULL, "Mono 2 Enable"},
  243. /* Aux In */
  244. {"AUX In", NULL, "AUX"},
  245. /* ADC */
  246. {"ADC", NULL, "Input Mixer"},
  247. {"Input Mixer", "Mic Capture Switch", "Mic"},
  248. {"Input Mixer", "Aux Capture Switch", "AUX In"},
  249. };
  250. static int ak4641_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  251. int clk_id, unsigned int freq, int dir)
  252. {
  253. struct snd_soc_component *component = codec_dai->component;
  254. struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
  255. ak4641->sysclk = freq;
  256. return 0;
  257. }
  258. static int ak4641_i2s_hw_params(struct snd_pcm_substream *substream,
  259. struct snd_pcm_hw_params *params,
  260. struct snd_soc_dai *dai)
  261. {
  262. struct snd_soc_component *component = dai->component;
  263. struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
  264. int rate = params_rate(params), fs = 256;
  265. u8 mode2;
  266. if (rate)
  267. fs = ak4641->sysclk / rate;
  268. else
  269. return -EINVAL;
  270. /* set fs */
  271. switch (fs) {
  272. case 1024:
  273. mode2 = (0x2 << 5);
  274. break;
  275. case 512:
  276. mode2 = (0x1 << 5);
  277. break;
  278. case 256:
  279. mode2 = (0x0 << 5);
  280. break;
  281. default:
  282. dev_err(component->dev, "Error: unsupported fs=%d\n", fs);
  283. return -EINVAL;
  284. }
  285. snd_soc_component_update_bits(component, AK4641_MODE2, (0x3 << 5), mode2);
  286. /* Update de-emphasis filter for the new rate */
  287. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  288. ak4641->playback_fs = rate;
  289. ak4641_set_deemph(component);
  290. }
  291. return 0;
  292. }
  293. static int ak4641_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
  294. unsigned int fmt)
  295. {
  296. struct snd_soc_component *component = codec_dai->component;
  297. u8 btif;
  298. int ret;
  299. /* interface format */
  300. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  301. case SND_SOC_DAIFMT_I2S:
  302. btif = (0x3 << 5);
  303. break;
  304. case SND_SOC_DAIFMT_LEFT_J:
  305. btif = (0x2 << 5);
  306. break;
  307. case SND_SOC_DAIFMT_DSP_A: /* MSB after FRM */
  308. btif = (0x0 << 5);
  309. break;
  310. case SND_SOC_DAIFMT_DSP_B: /* MSB during FRM */
  311. btif = (0x1 << 5);
  312. break;
  313. default:
  314. return -EINVAL;
  315. }
  316. ret = snd_soc_component_update_bits(component, AK4641_BTIF, (0x3 << 5), btif);
  317. if (ret < 0)
  318. return ret;
  319. return 0;
  320. }
  321. static int ak4641_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
  322. unsigned int fmt)
  323. {
  324. struct snd_soc_component *component = codec_dai->component;
  325. u8 mode1 = 0;
  326. /* interface format */
  327. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  328. case SND_SOC_DAIFMT_I2S:
  329. mode1 = 0x02;
  330. break;
  331. case SND_SOC_DAIFMT_LEFT_J:
  332. mode1 = 0x01;
  333. break;
  334. default:
  335. return -EINVAL;
  336. }
  337. return snd_soc_component_write(component, AK4641_MODE1, mode1);
  338. }
  339. static int ak4641_mute(struct snd_soc_dai *dai, int mute, int direction)
  340. {
  341. struct snd_soc_component *component = dai->component;
  342. return snd_soc_component_update_bits(component, AK4641_DAC, 0x20, mute ? 0x20 : 0);
  343. }
  344. static int ak4641_set_bias_level(struct snd_soc_component *component,
  345. enum snd_soc_bias_level level)
  346. {
  347. struct ak4641_priv *ak4641 = snd_soc_component_get_drvdata(component);
  348. struct ak4641_platform_data *pdata = component->dev->platform_data;
  349. int ret;
  350. switch (level) {
  351. case SND_SOC_BIAS_ON:
  352. /* unmute */
  353. snd_soc_component_update_bits(component, AK4641_DAC, 0x20, 0);
  354. break;
  355. case SND_SOC_BIAS_PREPARE:
  356. /* mute */
  357. snd_soc_component_update_bits(component, AK4641_DAC, 0x20, 0x20);
  358. break;
  359. case SND_SOC_BIAS_STANDBY:
  360. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
  361. if (pdata && gpio_is_valid(pdata->gpio_power))
  362. gpio_set_value(pdata->gpio_power, 1);
  363. mdelay(1);
  364. if (pdata && gpio_is_valid(pdata->gpio_npdn))
  365. gpio_set_value(pdata->gpio_npdn, 1);
  366. mdelay(1);
  367. ret = regcache_sync(ak4641->regmap);
  368. if (ret) {
  369. dev_err(component->dev,
  370. "Failed to sync cache: %d\n", ret);
  371. return ret;
  372. }
  373. }
  374. snd_soc_component_update_bits(component, AK4641_PM1, 0x80, 0x80);
  375. snd_soc_component_update_bits(component, AK4641_PM2, 0x80, 0);
  376. break;
  377. case SND_SOC_BIAS_OFF:
  378. snd_soc_component_update_bits(component, AK4641_PM1, 0x80, 0);
  379. if (pdata && gpio_is_valid(pdata->gpio_npdn))
  380. gpio_set_value(pdata->gpio_npdn, 0);
  381. if (pdata && gpio_is_valid(pdata->gpio_power))
  382. gpio_set_value(pdata->gpio_power, 0);
  383. regcache_mark_dirty(ak4641->regmap);
  384. break;
  385. }
  386. return 0;
  387. }
  388. #define AK4641_RATES (SNDRV_PCM_RATE_8000_48000)
  389. #define AK4641_RATES_BT (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  390. SNDRV_PCM_RATE_16000)
  391. #define AK4641_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
  392. static const struct snd_soc_dai_ops ak4641_i2s_dai_ops = {
  393. .hw_params = ak4641_i2s_hw_params,
  394. .set_fmt = ak4641_i2s_set_dai_fmt,
  395. .mute_stream = ak4641_mute,
  396. .set_sysclk = ak4641_set_dai_sysclk,
  397. .no_capture_mute = 1,
  398. };
  399. static const struct snd_soc_dai_ops ak4641_pcm_dai_ops = {
  400. .hw_params = NULL, /* rates are controlled by BT chip */
  401. .set_fmt = ak4641_pcm_set_dai_fmt,
  402. .mute_stream = ak4641_mute,
  403. .set_sysclk = ak4641_set_dai_sysclk,
  404. .no_capture_mute = 1,
  405. };
  406. static struct snd_soc_dai_driver ak4641_dai[] = {
  407. {
  408. .name = "ak4641-hifi",
  409. .id = 1,
  410. .playback = {
  411. .stream_name = "HiFi Playback",
  412. .channels_min = 1,
  413. .channels_max = 2,
  414. .rates = AK4641_RATES,
  415. .formats = AK4641_FORMATS,
  416. },
  417. .capture = {
  418. .stream_name = "HiFi Capture",
  419. .channels_min = 1,
  420. .channels_max = 2,
  421. .rates = AK4641_RATES,
  422. .formats = AK4641_FORMATS,
  423. },
  424. .ops = &ak4641_i2s_dai_ops,
  425. .symmetric_rates = 1,
  426. },
  427. {
  428. .name = "ak4641-voice",
  429. .id = 1,
  430. .playback = {
  431. .stream_name = "Voice Playback",
  432. .channels_min = 1,
  433. .channels_max = 1,
  434. .rates = AK4641_RATES_BT,
  435. .formats = AK4641_FORMATS,
  436. },
  437. .capture = {
  438. .stream_name = "Voice Capture",
  439. .channels_min = 1,
  440. .channels_max = 1,
  441. .rates = AK4641_RATES_BT,
  442. .formats = AK4641_FORMATS,
  443. },
  444. .ops = &ak4641_pcm_dai_ops,
  445. .symmetric_rates = 1,
  446. },
  447. };
  448. static const struct snd_soc_component_driver soc_component_dev_ak4641 = {
  449. .controls = ak4641_snd_controls,
  450. .num_controls = ARRAY_SIZE(ak4641_snd_controls),
  451. .dapm_widgets = ak4641_dapm_widgets,
  452. .num_dapm_widgets = ARRAY_SIZE(ak4641_dapm_widgets),
  453. .dapm_routes = ak4641_audio_map,
  454. .num_dapm_routes = ARRAY_SIZE(ak4641_audio_map),
  455. .set_bias_level = ak4641_set_bias_level,
  456. .suspend_bias_off = 1,
  457. .idle_bias_on = 1,
  458. .use_pmdown_time = 1,
  459. .endianness = 1,
  460. .non_legacy_dai_naming = 1,
  461. };
  462. static const struct regmap_config ak4641_regmap = {
  463. .reg_bits = 8,
  464. .val_bits = 8,
  465. .max_register = AK4641_BTIF,
  466. .reg_defaults = ak4641_reg_defaults,
  467. .num_reg_defaults = ARRAY_SIZE(ak4641_reg_defaults),
  468. .cache_type = REGCACHE_RBTREE,
  469. };
  470. static int ak4641_i2c_probe(struct i2c_client *i2c,
  471. const struct i2c_device_id *id)
  472. {
  473. struct ak4641_platform_data *pdata = i2c->dev.platform_data;
  474. struct ak4641_priv *ak4641;
  475. int ret;
  476. ak4641 = devm_kzalloc(&i2c->dev, sizeof(struct ak4641_priv),
  477. GFP_KERNEL);
  478. if (!ak4641)
  479. return -ENOMEM;
  480. ak4641->regmap = devm_regmap_init_i2c(i2c, &ak4641_regmap);
  481. if (IS_ERR(ak4641->regmap))
  482. return PTR_ERR(ak4641->regmap);
  483. if (pdata) {
  484. if (gpio_is_valid(pdata->gpio_power)) {
  485. ret = gpio_request_one(pdata->gpio_power,
  486. GPIOF_OUT_INIT_LOW, "ak4641 power");
  487. if (ret)
  488. goto err_out;
  489. }
  490. if (gpio_is_valid(pdata->gpio_npdn)) {
  491. ret = gpio_request_one(pdata->gpio_npdn,
  492. GPIOF_OUT_INIT_LOW, "ak4641 npdn");
  493. if (ret)
  494. goto err_gpio;
  495. udelay(1); /* > 150 ns */
  496. gpio_set_value(pdata->gpio_npdn, 1);
  497. }
  498. }
  499. i2c_set_clientdata(i2c, ak4641);
  500. ret = devm_snd_soc_register_component(&i2c->dev,
  501. &soc_component_dev_ak4641,
  502. ak4641_dai, ARRAY_SIZE(ak4641_dai));
  503. if (ret != 0)
  504. goto err_gpio2;
  505. return 0;
  506. err_gpio2:
  507. if (pdata) {
  508. if (gpio_is_valid(pdata->gpio_power))
  509. gpio_set_value(pdata->gpio_power, 0);
  510. if (gpio_is_valid(pdata->gpio_npdn))
  511. gpio_free(pdata->gpio_npdn);
  512. }
  513. err_gpio:
  514. if (pdata && gpio_is_valid(pdata->gpio_power))
  515. gpio_free(pdata->gpio_power);
  516. err_out:
  517. return ret;
  518. }
  519. static int ak4641_i2c_remove(struct i2c_client *i2c)
  520. {
  521. struct ak4641_platform_data *pdata = i2c->dev.platform_data;
  522. if (pdata) {
  523. if (gpio_is_valid(pdata->gpio_power)) {
  524. gpio_set_value(pdata->gpio_power, 0);
  525. gpio_free(pdata->gpio_power);
  526. }
  527. if (gpio_is_valid(pdata->gpio_npdn))
  528. gpio_free(pdata->gpio_npdn);
  529. }
  530. return 0;
  531. }
  532. static const struct i2c_device_id ak4641_i2c_id[] = {
  533. { "ak4641", 0 },
  534. { }
  535. };
  536. MODULE_DEVICE_TABLE(i2c, ak4641_i2c_id);
  537. static struct i2c_driver ak4641_i2c_driver = {
  538. .driver = {
  539. .name = "ak4641",
  540. },
  541. .probe = ak4641_i2c_probe,
  542. .remove = ak4641_i2c_remove,
  543. .id_table = ak4641_i2c_id,
  544. };
  545. module_i2c_driver(ak4641_i2c_driver);
  546. MODULE_DESCRIPTION("SoC AK4641 driver");
  547. MODULE_AUTHOR("Harald Welte <laforge@gnufiish.org>");
  548. MODULE_LICENSE("GPL");