tlv320aic23.c 21 KB

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