tlv320aic23.c 21 KB

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