tlv320aic23.c 19 KB

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