ak4641.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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.enumerated.item[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.enumerated.item[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_codec *codec = dai->codec;
  244. struct ak4641_priv *ak4641 = snd_soc_codec_get_drvdata(codec);
  245. int rate = params_rate(params), fs = 256;
  246. u8 mode2;
  247. if (rate)
  248. fs = ak4641->sysclk / rate;
  249. else
  250. return -EINVAL;
  251. /* set fs */
  252. switch (fs) {
  253. case 1024:
  254. mode2 = (0x2 << 5);
  255. break;
  256. case 512:
  257. mode2 = (0x1 << 5);
  258. break;
  259. case 256:
  260. mode2 = (0x0 << 5);
  261. break;
  262. default:
  263. dev_err(codec->dev, "Error: unsupported fs=%d\n", fs);
  264. return -EINVAL;
  265. }
  266. snd_soc_update_bits(codec, AK4641_MODE2, (0x3 << 5), mode2);
  267. /* Update de-emphasis filter for the new rate */
  268. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  269. ak4641->playback_fs = rate;
  270. ak4641_set_deemph(codec);
  271. };
  272. return 0;
  273. }
  274. static int ak4641_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
  275. unsigned int fmt)
  276. {
  277. struct snd_soc_codec *codec = codec_dai->codec;
  278. u8 btif;
  279. int ret;
  280. /* interface format */
  281. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  282. case SND_SOC_DAIFMT_I2S:
  283. btif = (0x3 << 5);
  284. break;
  285. case SND_SOC_DAIFMT_LEFT_J:
  286. btif = (0x2 << 5);
  287. break;
  288. case SND_SOC_DAIFMT_DSP_A: /* MSB after FRM */
  289. btif = (0x0 << 5);
  290. break;
  291. case SND_SOC_DAIFMT_DSP_B: /* MSB during FRM */
  292. btif = (0x1 << 5);
  293. break;
  294. default:
  295. return -EINVAL;
  296. }
  297. ret = snd_soc_update_bits(codec, AK4641_BTIF, (0x3 << 5), btif);
  298. if (ret < 0)
  299. return ret;
  300. return 0;
  301. }
  302. static int ak4641_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
  303. unsigned int fmt)
  304. {
  305. struct snd_soc_codec *codec = codec_dai->codec;
  306. u8 mode1 = 0;
  307. /* interface format */
  308. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  309. case SND_SOC_DAIFMT_I2S:
  310. mode1 = 0x02;
  311. break;
  312. case SND_SOC_DAIFMT_LEFT_J:
  313. mode1 = 0x01;
  314. break;
  315. default:
  316. return -EINVAL;
  317. }
  318. return snd_soc_write(codec, AK4641_MODE1, mode1);
  319. }
  320. static int ak4641_mute(struct snd_soc_dai *dai, int mute)
  321. {
  322. struct snd_soc_codec *codec = dai->codec;
  323. return snd_soc_update_bits(codec, AK4641_DAC, 0x20, mute ? 0x20 : 0);
  324. }
  325. static int ak4641_set_bias_level(struct snd_soc_codec *codec,
  326. enum snd_soc_bias_level level)
  327. {
  328. struct ak4641_platform_data *pdata = codec->dev->platform_data;
  329. int ret;
  330. switch (level) {
  331. case SND_SOC_BIAS_ON:
  332. /* unmute */
  333. snd_soc_update_bits(codec, AK4641_DAC, 0x20, 0);
  334. break;
  335. case SND_SOC_BIAS_PREPARE:
  336. /* mute */
  337. snd_soc_update_bits(codec, AK4641_DAC, 0x20, 0x20);
  338. break;
  339. case SND_SOC_BIAS_STANDBY:
  340. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  341. if (pdata && gpio_is_valid(pdata->gpio_power))
  342. gpio_set_value(pdata->gpio_power, 1);
  343. mdelay(1);
  344. if (pdata && gpio_is_valid(pdata->gpio_npdn))
  345. gpio_set_value(pdata->gpio_npdn, 1);
  346. mdelay(1);
  347. ret = snd_soc_cache_sync(codec);
  348. if (ret) {
  349. dev_err(codec->dev,
  350. "Failed to sync cache: %d\n", ret);
  351. return ret;
  352. }
  353. }
  354. snd_soc_update_bits(codec, AK4641_PM1, 0x80, 0x80);
  355. snd_soc_update_bits(codec, AK4641_PM2, 0x80, 0);
  356. break;
  357. case SND_SOC_BIAS_OFF:
  358. snd_soc_update_bits(codec, AK4641_PM1, 0x80, 0);
  359. if (pdata && gpio_is_valid(pdata->gpio_npdn))
  360. gpio_set_value(pdata->gpio_npdn, 0);
  361. if (pdata && gpio_is_valid(pdata->gpio_power))
  362. gpio_set_value(pdata->gpio_power, 0);
  363. codec->cache_sync = 1;
  364. break;
  365. }
  366. codec->dapm.bias_level = level;
  367. return 0;
  368. }
  369. #define AK4641_RATES (SNDRV_PCM_RATE_8000_48000)
  370. #define AK4641_RATES_BT (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  371. SNDRV_PCM_RATE_16000)
  372. #define AK4641_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
  373. static const struct snd_soc_dai_ops ak4641_i2s_dai_ops = {
  374. .hw_params = ak4641_i2s_hw_params,
  375. .set_fmt = ak4641_i2s_set_dai_fmt,
  376. .digital_mute = ak4641_mute,
  377. .set_sysclk = ak4641_set_dai_sysclk,
  378. };
  379. static const struct snd_soc_dai_ops ak4641_pcm_dai_ops = {
  380. .hw_params = NULL, /* rates are controlled by BT chip */
  381. .set_fmt = ak4641_pcm_set_dai_fmt,
  382. .digital_mute = ak4641_mute,
  383. .set_sysclk = ak4641_set_dai_sysclk,
  384. };
  385. static struct snd_soc_dai_driver ak4641_dai[] = {
  386. {
  387. .name = "ak4641-hifi",
  388. .id = 1,
  389. .playback = {
  390. .stream_name = "HiFi Playback",
  391. .channels_min = 1,
  392. .channels_max = 2,
  393. .rates = AK4641_RATES,
  394. .formats = AK4641_FORMATS,
  395. },
  396. .capture = {
  397. .stream_name = "HiFi Capture",
  398. .channels_min = 1,
  399. .channels_max = 2,
  400. .rates = AK4641_RATES,
  401. .formats = AK4641_FORMATS,
  402. },
  403. .ops = &ak4641_i2s_dai_ops,
  404. .symmetric_rates = 1,
  405. },
  406. {
  407. .name = "ak4641-voice",
  408. .id = 1,
  409. .playback = {
  410. .stream_name = "Voice Playback",
  411. .channels_min = 1,
  412. .channels_max = 1,
  413. .rates = AK4641_RATES_BT,
  414. .formats = AK4641_FORMATS,
  415. },
  416. .capture = {
  417. .stream_name = "Voice Capture",
  418. .channels_min = 1,
  419. .channels_max = 1,
  420. .rates = AK4641_RATES_BT,
  421. .formats = AK4641_FORMATS,
  422. },
  423. .ops = &ak4641_pcm_dai_ops,
  424. .symmetric_rates = 1,
  425. },
  426. };
  427. static int ak4641_suspend(struct snd_soc_codec *codec)
  428. {
  429. ak4641_set_bias_level(codec, SND_SOC_BIAS_OFF);
  430. return 0;
  431. }
  432. static int ak4641_resume(struct snd_soc_codec *codec)
  433. {
  434. ak4641_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  435. return 0;
  436. }
  437. static int ak4641_probe(struct snd_soc_codec *codec)
  438. {
  439. int ret;
  440. ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_I2C);
  441. if (ret != 0) {
  442. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  443. return ret;
  444. }
  445. /* power on device */
  446. ak4641_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  447. return 0;
  448. }
  449. static int ak4641_remove(struct snd_soc_codec *codec)
  450. {
  451. ak4641_set_bias_level(codec, SND_SOC_BIAS_OFF);
  452. return 0;
  453. }
  454. static struct snd_soc_codec_driver soc_codec_dev_ak4641 = {
  455. .probe = ak4641_probe,
  456. .remove = ak4641_remove,
  457. .suspend = ak4641_suspend,
  458. .resume = ak4641_resume,
  459. .controls = ak4641_snd_controls,
  460. .num_controls = ARRAY_SIZE(ak4641_snd_controls),
  461. .dapm_widgets = ak4641_dapm_widgets,
  462. .num_dapm_widgets = ARRAY_SIZE(ak4641_dapm_widgets),
  463. .dapm_routes = ak4641_audio_map,
  464. .num_dapm_routes = ARRAY_SIZE(ak4641_audio_map),
  465. .set_bias_level = ak4641_set_bias_level,
  466. .reg_cache_size = ARRAY_SIZE(ak4641_reg),
  467. .reg_word_size = sizeof(u8),
  468. .reg_cache_default = ak4641_reg,
  469. .reg_cache_step = 1,
  470. };
  471. static int __devinit ak4641_i2c_probe(struct i2c_client *i2c,
  472. const struct i2c_device_id *id)
  473. {
  474. struct ak4641_platform_data *pdata = i2c->dev.platform_data;
  475. struct ak4641_priv *ak4641;
  476. int ret;
  477. ak4641 = devm_kzalloc(&i2c->dev, sizeof(struct ak4641_priv),
  478. GFP_KERNEL);
  479. if (!ak4641)
  480. return -ENOMEM;
  481. if (pdata) {
  482. if (gpio_is_valid(pdata->gpio_power)) {
  483. ret = gpio_request_one(pdata->gpio_power,
  484. GPIOF_OUT_INIT_LOW, "ak4641 power");
  485. if (ret)
  486. goto err_out;
  487. }
  488. if (gpio_is_valid(pdata->gpio_npdn)) {
  489. ret = gpio_request_one(pdata->gpio_npdn,
  490. GPIOF_OUT_INIT_LOW, "ak4641 npdn");
  491. if (ret)
  492. goto err_gpio;
  493. udelay(1); /* > 150 ns */
  494. gpio_set_value(pdata->gpio_npdn, 1);
  495. }
  496. }
  497. i2c_set_clientdata(i2c, ak4641);
  498. ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_ak4641,
  499. ak4641_dai, ARRAY_SIZE(ak4641_dai));
  500. if (ret != 0)
  501. goto err_gpio2;
  502. return 0;
  503. err_gpio2:
  504. if (pdata) {
  505. if (gpio_is_valid(pdata->gpio_power))
  506. gpio_set_value(pdata->gpio_power, 0);
  507. if (gpio_is_valid(pdata->gpio_npdn))
  508. gpio_free(pdata->gpio_npdn);
  509. }
  510. err_gpio:
  511. if (pdata && gpio_is_valid(pdata->gpio_power))
  512. gpio_free(pdata->gpio_power);
  513. err_out:
  514. return ret;
  515. }
  516. static int __devexit ak4641_i2c_remove(struct i2c_client *i2c)
  517. {
  518. struct ak4641_platform_data *pdata = i2c->dev.platform_data;
  519. snd_soc_unregister_codec(&i2c->dev);
  520. if (pdata) {
  521. if (gpio_is_valid(pdata->gpio_power)) {
  522. gpio_set_value(pdata->gpio_power, 0);
  523. gpio_free(pdata->gpio_power);
  524. }
  525. if (gpio_is_valid(pdata->gpio_npdn))
  526. gpio_free(pdata->gpio_npdn);
  527. }
  528. return 0;
  529. }
  530. static const struct i2c_device_id ak4641_i2c_id[] = {
  531. { "ak4641", 0 },
  532. { }
  533. };
  534. MODULE_DEVICE_TABLE(i2c, ak4641_i2c_id);
  535. static struct i2c_driver ak4641_i2c_driver = {
  536. .driver = {
  537. .name = "ak4641",
  538. .owner = THIS_MODULE,
  539. },
  540. .probe = ak4641_i2c_probe,
  541. .remove = __devexit_p(ak4641_i2c_remove),
  542. .id_table = ak4641_i2c_id,
  543. };
  544. module_i2c_driver(ak4641_i2c_driver);
  545. MODULE_DESCRIPTION("SoC AK4641 driver");
  546. MODULE_AUTHOR("Harald Welte <laforge@gnufiish.org>");
  547. MODULE_LICENSE("GPL");