ak4641.c 17 KB

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