tlv320aic23.c 20 KB

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