ak4641.c 18 KB

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