tlv320aic23.c 19 KB

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