ak4641.c 18 KB

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