tlv320aic23.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * ALSA SoC TLV320AIC23 codec driver
  3. *
  4. * Author: Arun KS, <arunks@mistralsolutions.com>
  5. * Copyright: (C) 2008 Mistral Solutions Pvt Ltd.,
  6. *
  7. * Based on sound/soc/codecs/wm8731.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. * Notes:
  14. * The AIC23 is a driver for a low power stereo audio
  15. * codec tlv320aic23
  16. *
  17. * The machine layer should disable unsupported inputs/outputs by
  18. * snd_soc_dapm_disable_pin(codec, "LHPOUT"), etc.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/pm.h>
  25. #include <linux/i2c.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <sound/core.h>
  29. #include <sound/pcm.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/soc.h>
  32. #include <sound/tlv.h>
  33. #include <sound/initval.h>
  34. #include "tlv320aic23.h"
  35. #define AIC23_VERSION "0.1"
  36. /*
  37. * AIC23 register cache
  38. */
  39. static const u16 tlv320aic23_reg[] = {
  40. 0x0097, 0x0097, 0x00F9, 0x00F9, /* 0 */
  41. 0x001A, 0x0004, 0x0007, 0x0001, /* 4 */
  42. 0x0020, 0x0000, 0x0000, 0x0000, /* 8 */
  43. 0x0000, 0x0000, 0x0000, 0x0000, /* 12 */
  44. };
  45. static const char *rec_src_text[] = { "Line", "Mic" };
  46. static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  47. static const struct soc_enum rec_src_enum =
  48. SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
  49. static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
  50. SOC_DAPM_ENUM("Input Select", rec_src_enum);
  51. static const struct soc_enum tlv320aic23_rec_src =
  52. SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
  53. static const struct soc_enum tlv320aic23_deemph =
  54. SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text);
  55. static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
  56. static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
  57. static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
  58. static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
  59. struct snd_ctl_elem_value *ucontrol)
  60. {
  61. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  62. u16 val, reg;
  63. val = (ucontrol->value.integer.value[0] & 0x07);
  64. /* linear conversion to userspace
  65. * 000 = -6db
  66. * 001 = -9db
  67. * 010 = -12db
  68. * 011 = -18db (Min)
  69. * 100 = 0db (Max)
  70. */
  71. val = (val >= 4) ? 4 : (3 - val);
  72. reg = snd_soc_read(codec, TLV320AIC23_ANLG) & (~0x1C0);
  73. snd_soc_write(codec, TLV320AIC23_ANLG, reg | (val << 6));
  74. return 0;
  75. }
  76. static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
  77. struct snd_ctl_elem_value *ucontrol)
  78. {
  79. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  80. u16 val;
  81. val = snd_soc_read(codec, TLV320AIC23_ANLG) & (0x1C0);
  82. val = val >> 6;
  83. val = (val >= 4) ? 4 : (3 - val);
  84. ucontrol->value.integer.value[0] = val;
  85. return 0;
  86. }
  87. static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
  88. SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
  89. TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
  90. SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
  91. SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
  92. TLV320AIC23_RINVOL, 7, 1, 0),
  93. SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
  94. TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
  95. SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
  96. SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
  97. SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0,
  98. snd_soc_tlv320aic23_get_volsw,
  99. snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv),
  100. SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
  101. };
  102. /* PGA Mixer controls for Line and Mic switch */
  103. static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
  104. SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
  105. SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
  106. SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
  107. };
  108. static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
  109. SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
  110. SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
  111. SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
  112. &tlv320aic23_rec_src_mux_controls),
  113. SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
  114. &tlv320aic23_output_mixer_controls[0],
  115. ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
  116. SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
  117. SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
  118. SND_SOC_DAPM_OUTPUT("LHPOUT"),
  119. SND_SOC_DAPM_OUTPUT("RHPOUT"),
  120. SND_SOC_DAPM_OUTPUT("LOUT"),
  121. SND_SOC_DAPM_OUTPUT("ROUT"),
  122. SND_SOC_DAPM_INPUT("LLINEIN"),
  123. SND_SOC_DAPM_INPUT("RLINEIN"),
  124. SND_SOC_DAPM_INPUT("MICIN"),
  125. };
  126. static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
  127. /* Output Mixer */
  128. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  129. {"Output Mixer", "Playback Switch", "DAC"},
  130. {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
  131. /* Outputs */
  132. {"RHPOUT", NULL, "Output Mixer"},
  133. {"LHPOUT", NULL, "Output Mixer"},
  134. {"LOUT", NULL, "Output Mixer"},
  135. {"ROUT", NULL, "Output Mixer"},
  136. /* Inputs */
  137. {"Line Input", "NULL", "LLINEIN"},
  138. {"Line Input", "NULL", "RLINEIN"},
  139. {"Mic Input", "NULL", "MICIN"},
  140. /* input mux */
  141. {"Capture Source", "Line", "Line Input"},
  142. {"Capture Source", "Mic", "Mic Input"},
  143. {"ADC", NULL, "Capture Source"},
  144. };
  145. /* AIC23 driver data */
  146. struct aic23 {
  147. enum snd_soc_control_type control_type;
  148. int mclk;
  149. int requested_adc;
  150. int requested_dac;
  151. };
  152. /*
  153. * Common Crystals used
  154. * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
  155. * 12.0000 Mhz /125 = *96k /136 = 88.235K
  156. * 12.2880 Mhz /128 = *96k /192 = 64k
  157. * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
  158. * 18.4320 Mhz /128 = 144k /192 = *96k
  159. */
  160. /*
  161. * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
  162. * USB BOSR 0-250/2 = 125, 1-272/2 = 136
  163. */
  164. static const int bosr_usb_divisor_table[] = {
  165. 128, 125, 192, 136
  166. };
  167. #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
  168. #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
  169. static const unsigned short sr_valid_mask[] = {
  170. LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
  171. LOWER_GROUP, /* Usb, bosr - 0*/
  172. LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
  173. UPPER_GROUP, /* Usb, bosr - 1*/
  174. };
  175. /*
  176. * Every divisor is a factor of 11*12
  177. */
  178. #define SR_MULT (11*12)
  179. #define A(x) (SR_MULT/x)
  180. static const unsigned char sr_adc_mult_table[] = {
  181. A(2), A(2), A(12), A(12), 0, 0, A(3), A(1),
  182. A(2), A(2), A(11), A(11), 0, 0, 0, A(1)
  183. };
  184. static const unsigned char sr_dac_mult_table[] = {
  185. A(2), A(12), A(2), A(12), 0, 0, A(3), A(1),
  186. A(2), A(11), A(2), A(11), 0, 0, 0, A(1)
  187. };
  188. static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
  189. int dac, int dac_l, int dac_h, int need_dac)
  190. {
  191. if ((adc >= adc_l) && (adc <= adc_h) &&
  192. (dac >= dac_l) && (dac <= dac_h)) {
  193. int diff_adc = need_adc - adc;
  194. int diff_dac = need_dac - dac;
  195. return abs(diff_adc) + abs(diff_dac);
  196. }
  197. return UINT_MAX;
  198. }
  199. static int find_rate(int mclk, u32 need_adc, u32 need_dac)
  200. {
  201. int i, j;
  202. int best_i = -1;
  203. int best_j = -1;
  204. int best_div = 0;
  205. unsigned best_score = UINT_MAX;
  206. int adc_l, adc_h, dac_l, dac_h;
  207. need_adc *= SR_MULT;
  208. need_dac *= SR_MULT;
  209. /*
  210. * rates given are +/- 1/32
  211. */
  212. adc_l = need_adc - (need_adc >> 5);
  213. adc_h = need_adc + (need_adc >> 5);
  214. dac_l = need_dac - (need_dac >> 5);
  215. dac_h = need_dac + (need_dac >> 5);
  216. for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
  217. int base = mclk / bosr_usb_divisor_table[i];
  218. int mask = sr_valid_mask[i];
  219. for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
  220. j++, mask >>= 1) {
  221. int adc;
  222. int dac;
  223. int score;
  224. if ((mask & 1) == 0)
  225. continue;
  226. adc = base * sr_adc_mult_table[j];
  227. dac = base * sr_dac_mult_table[j];
  228. score = get_score(adc, adc_l, adc_h, need_adc,
  229. dac, dac_l, dac_h, need_dac);
  230. if (best_score > score) {
  231. best_score = score;
  232. best_i = i;
  233. best_j = j;
  234. best_div = 0;
  235. }
  236. score = get_score((adc >> 1), adc_l, adc_h, need_adc,
  237. (dac >> 1), dac_l, dac_h, need_dac);
  238. /* prefer to have a /2 */
  239. if ((score != UINT_MAX) && (best_score >= score)) {
  240. best_score = score;
  241. best_i = i;
  242. best_j = j;
  243. best_div = 1;
  244. }
  245. }
  246. }
  247. return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
  248. }
  249. #ifdef DEBUG
  250. static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk,
  251. u32 *sample_rate_adc, u32 *sample_rate_dac)
  252. {
  253. int src = snd_soc_read(codec, TLV320AIC23_SRATE);
  254. int sr = (src >> 2) & 0x0f;
  255. int val = (mclk / bosr_usb_divisor_table[src & 3]);
  256. int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
  257. int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
  258. if (src & TLV320AIC23_CLKIN_HALF) {
  259. adc >>= 1;
  260. dac >>= 1;
  261. }
  262. *sample_rate_adc = adc;
  263. *sample_rate_dac = dac;
  264. }
  265. #endif
  266. static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk,
  267. u32 sample_rate_adc, u32 sample_rate_dac)
  268. {
  269. /* Search for the right sample rate */
  270. int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
  271. if (data < 0) {
  272. printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
  273. __func__, sample_rate_adc, sample_rate_dac);
  274. return -EINVAL;
  275. }
  276. snd_soc_write(codec, TLV320AIC23_SRATE, data);
  277. #ifdef DEBUG
  278. {
  279. u32 adc, dac;
  280. get_current_sample_rates(codec, mclk, &adc, &dac);
  281. printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
  282. adc, dac, data);
  283. }
  284. #endif
  285. return 0;
  286. }
  287. static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
  288. struct snd_pcm_hw_params *params,
  289. struct snd_soc_dai *dai)
  290. {
  291. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  292. struct snd_soc_codec *codec = rtd->codec;
  293. u16 iface_reg;
  294. int ret;
  295. struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
  296. u32 sample_rate_adc = aic23->requested_adc;
  297. u32 sample_rate_dac = aic23->requested_dac;
  298. u32 sample_rate = params_rate(params);
  299. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  300. aic23->requested_dac = sample_rate_dac = sample_rate;
  301. if (!sample_rate_adc)
  302. sample_rate_adc = sample_rate;
  303. } else {
  304. aic23->requested_adc = sample_rate_adc = sample_rate;
  305. if (!sample_rate_dac)
  306. sample_rate_dac = sample_rate;
  307. }
  308. ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
  309. sample_rate_dac);
  310. if (ret < 0)
  311. return ret;
  312. iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
  313. switch (params_format(params)) {
  314. case SNDRV_PCM_FORMAT_S16_LE:
  315. break;
  316. case SNDRV_PCM_FORMAT_S20_3LE:
  317. iface_reg |= (0x01 << 2);
  318. break;
  319. case SNDRV_PCM_FORMAT_S24_LE:
  320. iface_reg |= (0x02 << 2);
  321. break;
  322. case SNDRV_PCM_FORMAT_S32_LE:
  323. iface_reg |= (0x03 << 2);
  324. break;
  325. }
  326. snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
  327. return 0;
  328. }
  329. static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
  330. struct snd_soc_dai *dai)
  331. {
  332. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  333. struct snd_soc_codec *codec = rtd->codec;
  334. /* set active */
  335. snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0001);
  336. return 0;
  337. }
  338. static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
  339. struct snd_soc_dai *dai)
  340. {
  341. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  342. struct snd_soc_codec *codec = rtd->codec;
  343. struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
  344. /* deactivate */
  345. if (!codec->active) {
  346. udelay(50);
  347. snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0);
  348. }
  349. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  350. aic23->requested_dac = 0;
  351. else
  352. aic23->requested_adc = 0;
  353. }
  354. static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
  355. {
  356. struct snd_soc_codec *codec = dai->codec;
  357. u16 reg;
  358. reg = snd_soc_read(codec, TLV320AIC23_DIGT);
  359. if (mute)
  360. reg |= TLV320AIC23_DACM_MUTE;
  361. else
  362. reg &= ~TLV320AIC23_DACM_MUTE;
  363. snd_soc_write(codec, TLV320AIC23_DIGT, reg);
  364. return 0;
  365. }
  366. static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
  367. unsigned int fmt)
  368. {
  369. struct snd_soc_codec *codec = codec_dai->codec;
  370. u16 iface_reg;
  371. iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
  372. /* set master/slave audio interface */
  373. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  374. case SND_SOC_DAIFMT_CBM_CFM:
  375. iface_reg |= TLV320AIC23_MS_MASTER;
  376. break;
  377. case SND_SOC_DAIFMT_CBS_CFS:
  378. iface_reg &= ~TLV320AIC23_MS_MASTER;
  379. break;
  380. default:
  381. return -EINVAL;
  382. }
  383. /* interface format */
  384. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  385. case SND_SOC_DAIFMT_I2S:
  386. iface_reg |= TLV320AIC23_FOR_I2S;
  387. break;
  388. case SND_SOC_DAIFMT_DSP_A:
  389. iface_reg |= TLV320AIC23_LRP_ON;
  390. case SND_SOC_DAIFMT_DSP_B:
  391. iface_reg |= TLV320AIC23_FOR_DSP;
  392. break;
  393. case SND_SOC_DAIFMT_RIGHT_J:
  394. break;
  395. case SND_SOC_DAIFMT_LEFT_J:
  396. iface_reg |= TLV320AIC23_FOR_LJUST;
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
  402. return 0;
  403. }
  404. static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  405. int clk_id, unsigned int freq, int dir)
  406. {
  407. struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
  408. aic23->mclk = freq;
  409. return 0;
  410. }
  411. static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
  412. enum snd_soc_bias_level level)
  413. {
  414. u16 reg = snd_soc_read(codec, TLV320AIC23_PWR) & 0xff7f;
  415. switch (level) {
  416. case SND_SOC_BIAS_ON:
  417. /* vref/mid, osc on, dac unmute */
  418. reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
  419. TLV320AIC23_DAC_OFF);
  420. snd_soc_write(codec, TLV320AIC23_PWR, reg);
  421. break;
  422. case SND_SOC_BIAS_PREPARE:
  423. break;
  424. case SND_SOC_BIAS_STANDBY:
  425. /* everything off except vref/vmid, */
  426. snd_soc_write(codec, TLV320AIC23_PWR,
  427. reg | TLV320AIC23_CLK_OFF);
  428. break;
  429. case SND_SOC_BIAS_OFF:
  430. /* everything off, dac mute, inactive */
  431. snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0);
  432. snd_soc_write(codec, TLV320AIC23_PWR, 0xffff);
  433. break;
  434. }
  435. codec->dapm.bias_level = level;
  436. return 0;
  437. }
  438. #define AIC23_RATES SNDRV_PCM_RATE_8000_96000
  439. #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  440. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  441. static struct snd_soc_dai_ops tlv320aic23_dai_ops = {
  442. .prepare = tlv320aic23_pcm_prepare,
  443. .hw_params = tlv320aic23_hw_params,
  444. .shutdown = tlv320aic23_shutdown,
  445. .digital_mute = tlv320aic23_mute,
  446. .set_fmt = tlv320aic23_set_dai_fmt,
  447. .set_sysclk = tlv320aic23_set_dai_sysclk,
  448. };
  449. static struct snd_soc_dai_driver tlv320aic23_dai = {
  450. .name = "tlv320aic23-hifi",
  451. .playback = {
  452. .stream_name = "Playback",
  453. .channels_min = 2,
  454. .channels_max = 2,
  455. .rates = AIC23_RATES,
  456. .formats = AIC23_FORMATS,},
  457. .capture = {
  458. .stream_name = "Capture",
  459. .channels_min = 2,
  460. .channels_max = 2,
  461. .rates = AIC23_RATES,
  462. .formats = AIC23_FORMATS,},
  463. .ops = &tlv320aic23_dai_ops,
  464. };
  465. static int tlv320aic23_suspend(struct snd_soc_codec *codec,
  466. pm_message_t state)
  467. {
  468. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
  469. return 0;
  470. }
  471. static int tlv320aic23_resume(struct snd_soc_codec *codec)
  472. {
  473. snd_soc_cache_sync(codec);
  474. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  475. return 0;
  476. }
  477. static int tlv320aic23_probe(struct snd_soc_codec *codec)
  478. {
  479. struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
  480. int ret;
  481. printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
  482. ret = snd_soc_codec_set_cache_io(codec, 7, 9, aic23->control_type);
  483. if (ret < 0) {
  484. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  485. return ret;
  486. }
  487. /* Reset codec */
  488. snd_soc_write(codec, TLV320AIC23_RESET, 0);
  489. /* Write the register default value to cache for reserved registers,
  490. * so the write to the these registers are suppressed by the cache
  491. * restore code when it skips writes of default registers.
  492. */
  493. snd_soc_cache_write(codec, 0x0A, 0);
  494. snd_soc_cache_write(codec, 0x0B, 0);
  495. snd_soc_cache_write(codec, 0x0C, 0);
  496. snd_soc_cache_write(codec, 0x0D, 0);
  497. snd_soc_cache_write(codec, 0x0E, 0);
  498. /* power on device */
  499. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  500. snd_soc_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
  501. /* Unmute input */
  502. snd_soc_update_bits(codec, TLV320AIC23_LINVOL,
  503. TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
  504. snd_soc_update_bits(codec, TLV320AIC23_RINVOL,
  505. TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
  506. snd_soc_update_bits(codec, TLV320AIC23_ANLG,
  507. TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED,
  508. 0);
  509. /* Default output volume */
  510. snd_soc_write(codec, TLV320AIC23_LCHNVOL,
  511. TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
  512. snd_soc_write(codec, TLV320AIC23_RCHNVOL,
  513. TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
  514. snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x1);
  515. snd_soc_add_controls(codec, tlv320aic23_snd_controls,
  516. ARRAY_SIZE(tlv320aic23_snd_controls));
  517. return 0;
  518. }
  519. static int tlv320aic23_remove(struct snd_soc_codec *codec)
  520. {
  521. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
  522. return 0;
  523. }
  524. static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = {
  525. .reg_cache_size = ARRAY_SIZE(tlv320aic23_reg),
  526. .reg_word_size = sizeof(u16),
  527. .reg_cache_default = tlv320aic23_reg,
  528. .probe = tlv320aic23_probe,
  529. .remove = tlv320aic23_remove,
  530. .suspend = tlv320aic23_suspend,
  531. .resume = tlv320aic23_resume,
  532. .set_bias_level = tlv320aic23_set_bias_level,
  533. .dapm_widgets = tlv320aic23_dapm_widgets,
  534. .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets),
  535. .dapm_routes = tlv320aic23_intercon,
  536. .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon),
  537. };
  538. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  539. /*
  540. * If the i2c layer weren't so broken, we could pass this kind of data
  541. * around
  542. */
  543. static int tlv320aic23_codec_probe(struct i2c_client *i2c,
  544. const struct i2c_device_id *i2c_id)
  545. {
  546. struct aic23 *aic23;
  547. int ret;
  548. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  549. return -EINVAL;
  550. aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL);
  551. if (aic23 == NULL)
  552. return -ENOMEM;
  553. i2c_set_clientdata(i2c, aic23);
  554. aic23->control_type = SND_SOC_I2C;
  555. ret = snd_soc_register_codec(&i2c->dev,
  556. &soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1);
  557. if (ret < 0)
  558. kfree(aic23);
  559. return ret;
  560. }
  561. static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
  562. {
  563. snd_soc_unregister_codec(&i2c->dev);
  564. kfree(i2c_get_clientdata(i2c));
  565. return 0;
  566. }
  567. static const struct i2c_device_id tlv320aic23_id[] = {
  568. {"tlv320aic23", 0},
  569. {}
  570. };
  571. MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
  572. static struct i2c_driver tlv320aic23_i2c_driver = {
  573. .driver = {
  574. .name = "tlv320aic23-codec",
  575. },
  576. .probe = tlv320aic23_codec_probe,
  577. .remove = __exit_p(tlv320aic23_i2c_remove),
  578. .id_table = tlv320aic23_id,
  579. };
  580. #endif
  581. static int __init tlv320aic23_modinit(void)
  582. {
  583. int ret;
  584. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  585. ret = i2c_add_driver(&tlv320aic23_i2c_driver);
  586. if (ret != 0) {
  587. printk(KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n",
  588. ret);
  589. }
  590. #endif
  591. return ret;
  592. }
  593. module_init(tlv320aic23_modinit);
  594. static void __exit tlv320aic23_exit(void)
  595. {
  596. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  597. i2c_del_driver(&tlv320aic23_i2c_driver);
  598. #endif
  599. }
  600. module_exit(tlv320aic23_exit);
  601. MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
  602. MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
  603. MODULE_LICENSE("GPL");