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/platform_device.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 snd_soc_codec *codec;
  32. unsigned int sysclk;
  33. int deemph;
  34. int playback_fs;
  35. };
  36. /*
  37. * ak4641 register cache
  38. */
  39. static const u8 ak4641_reg[AK4641_CACHEREGNUM] = {
  40. 0x00, 0x80, 0x00, 0x80,
  41. 0x02, 0x00, 0x11, 0x05,
  42. 0x00, 0x00, 0x36, 0x10,
  43. 0x00, 0x00, 0x57, 0x00,
  44. 0x88, 0x88, 0x08, 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_kcontrol_chip(kcontrol);
  67. struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
  68. int deemph = ucontrol->value.enumerated.item[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_kcontrol_chip(kcontrol);
  78. struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
  79. ucontrol->value.enumerated.item[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 const struct soc_enum ak4641_mono_out_enum =
  96. SOC_ENUM_SINGLE(AK4641_SIG1, 6, 2, ak4641_mono_out);
  97. static const struct soc_enum ak4641_hp_out_enum =
  98. SOC_ENUM_SINGLE(AK4641_MODE2, 2, 2, ak4641_hp_out);
  99. static const struct soc_enum ak4641_mic_select_enum =
  100. SOC_ENUM_SINGLE(AK4641_MIC, 1, 2, ak4641_mic_select);
  101. static const struct soc_enum ak4641_mic_or_dac_enum =
  102. SOC_ENUM_SINGLE(AK4641_BTIF, 4, 2, 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_ADC("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_pcm_runtime *rtd = substream->private_data;
  246. struct snd_soc_codec *codec = rtd->codec;
  247. struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
  248. int rate = params_rate(params), fs = 256;
  249. u8 mode2;
  250. if (rate)
  251. fs = ak4641->sysclk / rate;
  252. else
  253. return -EINVAL;
  254. /* set fs */
  255. switch (fs) {
  256. case 1024:
  257. mode2 = (0x2 << 5);
  258. break;
  259. case 512:
  260. mode2 = (0x1 << 5);
  261. break;
  262. case 256:
  263. mode2 = (0x0 << 5);
  264. break;
  265. default:
  266. dev_err(codec->dev, "Error: unsupported fs=%d\n", fs);
  267. return -EINVAL;
  268. }
  269. snd_soc_update_bits(codec, AK4641_MODE2, (0x3 << 5), mode2);
  270. /* Update de-emphasis filter for the new rate */
  271. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  272. ak4641->playback_fs = rate;
  273. ak4641_set_deemph(codec);
  274. };
  275. return 0;
  276. }
  277. static int ak4641_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
  278. unsigned int fmt)
  279. {
  280. struct snd_soc_codec *codec = codec_dai->codec;
  281. u8 btif;
  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. return snd_soc_update_bits(codec, AK4641_BTIF, (0x3 << 5), btif);
  300. }
  301. static int ak4641_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
  302. unsigned int fmt)
  303. {
  304. struct snd_soc_codec *codec = codec_dai->codec;
  305. u8 mode1 = 0;
  306. /* interface format */
  307. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  308. case SND_SOC_DAIFMT_I2S:
  309. mode1 = 0x02;
  310. break;
  311. case SND_SOC_DAIFMT_LEFT_J:
  312. mode1 = 0x01;
  313. break;
  314. default:
  315. return -EINVAL;
  316. }
  317. return snd_soc_write(codec, AK4641_MODE1, mode1);
  318. }
  319. static int ak4641_mute(struct snd_soc_dai *dai, int mute)
  320. {
  321. struct snd_soc_codec *codec = dai->codec;
  322. return snd_soc_update_bits(codec, AK4641_DAC, 0x20, mute ? 0x20 : 0);
  323. }
  324. static int ak4641_set_bias_level(struct snd_soc_codec *codec,
  325. enum snd_soc_bias_level level)
  326. {
  327. struct ak4641_platform_data *pdata = codec->dev->platform_data;
  328. int ret;
  329. switch (level) {
  330. case SND_SOC_BIAS_ON:
  331. /* unmute */
  332. snd_soc_update_bits(codec, AK4641_DAC, 0x20, 0);
  333. break;
  334. case SND_SOC_BIAS_PREPARE:
  335. /* mute */
  336. snd_soc_update_bits(codec, AK4641_DAC, 0x20, 0x20);
  337. break;
  338. case SND_SOC_BIAS_STANDBY:
  339. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  340. if (pdata && gpio_is_valid(pdata->gpio_power))
  341. gpio_set_value(pdata->gpio_power, 1);
  342. mdelay(1);
  343. if (pdata && gpio_is_valid(pdata->gpio_npdn))
  344. gpio_set_value(pdata->gpio_npdn, 1);
  345. mdelay(1);
  346. ret = snd_soc_cache_sync(codec);
  347. if (ret) {
  348. dev_err(codec->dev,
  349. "Failed to sync cache: %d\n", ret);
  350. return ret;
  351. }
  352. }
  353. snd_soc_update_bits(codec, AK4641_PM1, 0x80, 0x80);
  354. snd_soc_update_bits(codec, AK4641_PM2, 0x80, 0);
  355. break;
  356. case SND_SOC_BIAS_OFF:
  357. snd_soc_update_bits(codec, AK4641_PM1, 0x80, 0);
  358. if (pdata && gpio_is_valid(pdata->gpio_npdn))
  359. gpio_set_value(pdata->gpio_npdn, 0);
  360. if (pdata && gpio_is_valid(pdata->gpio_power))
  361. gpio_set_value(pdata->gpio_power, 0);
  362. codec->cache_sync = 1;
  363. break;
  364. }
  365. codec->dapm.bias_level = level;
  366. return 0;
  367. }
  368. #define AK4641_RATES (SNDRV_PCM_RATE_8000_48000)
  369. #define AK4641_RATES_BT (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  370. SNDRV_PCM_RATE_16000)
  371. #define AK4641_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
  372. static struct snd_soc_dai_ops ak4641_i2s_dai_ops = {
  373. .hw_params = ak4641_i2s_hw_params,
  374. .set_fmt = ak4641_i2s_set_dai_fmt,
  375. .digital_mute = ak4641_mute,
  376. .set_sysclk = ak4641_set_dai_sysclk,
  377. };
  378. static struct snd_soc_dai_ops ak4641_pcm_dai_ops = {
  379. .hw_params = NULL, /* rates are controlled by BT chip */
  380. .set_fmt = ak4641_pcm_set_dai_fmt,
  381. .digital_mute = ak4641_mute,
  382. .set_sysclk = ak4641_set_dai_sysclk,
  383. };
  384. struct snd_soc_dai_driver ak4641_dai[] = {
  385. {
  386. .name = "ak4641-hifi",
  387. .id = 1,
  388. .playback = {
  389. .stream_name = "HiFi Playback",
  390. .channels_min = 1,
  391. .channels_max = 2,
  392. .rates = AK4641_RATES,
  393. .formats = AK4641_FORMATS,
  394. },
  395. .capture = {
  396. .stream_name = "HiFi Capture",
  397. .channels_min = 1,
  398. .channels_max = 2,
  399. .rates = AK4641_RATES,
  400. .formats = AK4641_FORMATS,
  401. },
  402. .ops = &ak4641_i2s_dai_ops,
  403. .symmetric_rates = 1,
  404. },
  405. {
  406. .name = "ak4641-voice",
  407. .id = 1,
  408. .playback = {
  409. .stream_name = "Voice Playback",
  410. .channels_min = 1,
  411. .channels_max = 1,
  412. .rates = AK4641_RATES_BT,
  413. .formats = AK4641_FORMATS,
  414. },
  415. .capture = {
  416. .stream_name = "Voice Capture",
  417. .channels_min = 1,
  418. .channels_max = 1,
  419. .rates = AK4641_RATES_BT,
  420. .formats = AK4641_FORMATS,
  421. },
  422. .ops = &ak4641_pcm_dai_ops,
  423. .symmetric_rates = 1,
  424. },
  425. };
  426. static int ak4641_suspend(struct snd_soc_codec *codec, pm_message_t state)
  427. {
  428. ak4641_set_bias_level(codec, SND_SOC_BIAS_OFF);
  429. return 0;
  430. }
  431. static int ak4641_resume(struct snd_soc_codec *codec)
  432. {
  433. ak4641_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  434. return 0;
  435. }
  436. static int ak4641_probe(struct snd_soc_codec *codec)
  437. {
  438. struct ak4641_platform_data *pdata = codec->dev->platform_data;
  439. int ret;
  440. if (pdata) {
  441. if (gpio_is_valid(pdata->gpio_power)) {
  442. ret = gpio_request_one(pdata->gpio_power,
  443. GPIOF_OUT_INIT_LOW, "ak4641 power");
  444. if (ret)
  445. goto err_out;
  446. }
  447. if (gpio_is_valid(pdata->gpio_npdn)) {
  448. ret = gpio_request_one(pdata->gpio_npdn,
  449. GPIOF_OUT_INIT_LOW, "ak4641 npdn");
  450. if (ret)
  451. goto err_gpio;
  452. udelay(1); /* > 150 ns */
  453. gpio_set_value(pdata->gpio_npdn, 1);
  454. }
  455. }
  456. ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_I2C);
  457. if (ret != 0) {
  458. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  459. goto err_register;
  460. }
  461. /* power on device */
  462. ak4641_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  463. return 0;
  464. err_register:
  465. if (pdata) {
  466. if (gpio_is_valid(pdata->gpio_power))
  467. gpio_set_value(pdata->gpio_power, 0);
  468. if (gpio_is_valid(pdata->gpio_npdn))
  469. gpio_free(pdata->gpio_npdn);
  470. }
  471. err_gpio:
  472. if (pdata && gpio_is_valid(pdata->gpio_power))
  473. gpio_free(pdata->gpio_power);
  474. err_out:
  475. return ret;
  476. }
  477. static int ak4641_remove(struct snd_soc_codec *codec)
  478. {
  479. struct ak4641_platform_data *pdata = codec->dev->platform_data;
  480. ak4641_set_bias_level(codec, SND_SOC_BIAS_OFF);
  481. if (pdata) {
  482. if (gpio_is_valid(pdata->gpio_power)) {
  483. gpio_set_value(pdata->gpio_power, 0);
  484. gpio_free(pdata->gpio_power);
  485. }
  486. if (gpio_is_valid(pdata->gpio_npdn))
  487. gpio_free(pdata->gpio_npdn);
  488. }
  489. return 0;
  490. }
  491. static struct snd_soc_codec_driver soc_codec_dev_ak4641 = {
  492. .probe = ak4641_probe,
  493. .remove = ak4641_remove,
  494. .suspend = ak4641_suspend,
  495. .resume = ak4641_resume,
  496. .controls = ak4641_snd_controls,
  497. .num_controls = ARRAY_SIZE(ak4641_snd_controls),
  498. .dapm_widgets = ak4641_dapm_widgets,
  499. .num_dapm_widgets = ARRAY_SIZE(ak4641_dapm_widgets),
  500. .dapm_routes = ak4641_audio_map,
  501. .num_dapm_routes = ARRAY_SIZE(ak4641_audio_map),
  502. .set_bias_level = ak4641_set_bias_level,
  503. .reg_cache_size = ARRAY_SIZE(ak4641_reg),
  504. .reg_word_size = sizeof(u8),
  505. .reg_cache_default = ak4641_reg,
  506. .reg_cache_step = 1,
  507. };
  508. static int __devinit ak4641_i2c_probe(struct i2c_client *i2c,
  509. const struct i2c_device_id *id)
  510. {
  511. struct ak4641_priv *ak4641;
  512. int ret;
  513. ak4641 = kzalloc(sizeof(struct ak4641_priv), GFP_KERNEL);
  514. if (!ak4641)
  515. return -ENOMEM;
  516. i2c_set_clientdata(i2c, ak4641);
  517. ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_ak4641,
  518. ak4641_dai, ARRAY_SIZE(ak4641_dai));
  519. if (ret < 0)
  520. kfree(ak4641);
  521. return ret;
  522. }
  523. static int __devexit ak4641_i2c_remove(struct i2c_client *i2c)
  524. {
  525. snd_soc_unregister_codec(&i2c->dev);
  526. kfree(i2c_get_clientdata(i2c));
  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");