tlv320aic23.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. struct snd_soc_dapm_context *dapm = &codec->dapm;
  345. snd_soc_dapm_new_controls(dapm, tlv320aic23_dapm_widgets,
  346. ARRAY_SIZE(tlv320aic23_dapm_widgets));
  347. /* set up audio path interconnects */
  348. snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
  349. return 0;
  350. }
  351. static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
  352. struct snd_pcm_hw_params *params,
  353. struct snd_soc_dai *dai)
  354. {
  355. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  356. struct snd_soc_codec *codec = rtd->codec;
  357. u16 iface_reg;
  358. int ret;
  359. struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
  360. u32 sample_rate_adc = aic23->requested_adc;
  361. u32 sample_rate_dac = aic23->requested_dac;
  362. u32 sample_rate = params_rate(params);
  363. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  364. aic23->requested_dac = sample_rate_dac = sample_rate;
  365. if (!sample_rate_adc)
  366. sample_rate_adc = sample_rate;
  367. } else {
  368. aic23->requested_adc = sample_rate_adc = sample_rate;
  369. if (!sample_rate_dac)
  370. sample_rate_dac = sample_rate;
  371. }
  372. ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
  373. sample_rate_dac);
  374. if (ret < 0)
  375. return ret;
  376. iface_reg =
  377. tlv320aic23_read_reg_cache(codec,
  378. TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
  379. switch (params_format(params)) {
  380. case SNDRV_PCM_FORMAT_S16_LE:
  381. break;
  382. case SNDRV_PCM_FORMAT_S20_3LE:
  383. iface_reg |= (0x01 << 2);
  384. break;
  385. case SNDRV_PCM_FORMAT_S24_LE:
  386. iface_reg |= (0x02 << 2);
  387. break;
  388. case SNDRV_PCM_FORMAT_S32_LE:
  389. iface_reg |= (0x03 << 2);
  390. break;
  391. }
  392. tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
  393. return 0;
  394. }
  395. static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
  396. struct snd_soc_dai *dai)
  397. {
  398. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  399. struct snd_soc_codec *codec = rtd->codec;
  400. /* set active */
  401. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001);
  402. return 0;
  403. }
  404. static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
  405. struct snd_soc_dai *dai)
  406. {
  407. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  408. struct snd_soc_codec *codec = rtd->codec;
  409. struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
  410. /* deactivate */
  411. if (!codec->active) {
  412. udelay(50);
  413. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
  414. }
  415. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  416. aic23->requested_dac = 0;
  417. else
  418. aic23->requested_adc = 0;
  419. }
  420. static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
  421. {
  422. struct snd_soc_codec *codec = dai->codec;
  423. u16 reg;
  424. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT);
  425. if (mute)
  426. reg |= TLV320AIC23_DACM_MUTE;
  427. else
  428. reg &= ~TLV320AIC23_DACM_MUTE;
  429. tlv320aic23_write(codec, TLV320AIC23_DIGT, reg);
  430. return 0;
  431. }
  432. static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
  433. unsigned int fmt)
  434. {
  435. struct snd_soc_codec *codec = codec_dai->codec;
  436. u16 iface_reg;
  437. iface_reg =
  438. tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
  439. /* set master/slave audio interface */
  440. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  441. case SND_SOC_DAIFMT_CBM_CFM:
  442. iface_reg |= TLV320AIC23_MS_MASTER;
  443. break;
  444. case SND_SOC_DAIFMT_CBS_CFS:
  445. break;
  446. default:
  447. return -EINVAL;
  448. }
  449. /* interface format */
  450. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  451. case SND_SOC_DAIFMT_I2S:
  452. iface_reg |= TLV320AIC23_FOR_I2S;
  453. break;
  454. case SND_SOC_DAIFMT_DSP_A:
  455. iface_reg |= TLV320AIC23_LRP_ON;
  456. case SND_SOC_DAIFMT_DSP_B:
  457. iface_reg |= TLV320AIC23_FOR_DSP;
  458. break;
  459. case SND_SOC_DAIFMT_RIGHT_J:
  460. break;
  461. case SND_SOC_DAIFMT_LEFT_J:
  462. iface_reg |= TLV320AIC23_FOR_LJUST;
  463. break;
  464. default:
  465. return -EINVAL;
  466. }
  467. tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
  468. return 0;
  469. }
  470. static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  471. int clk_id, unsigned int freq, int dir)
  472. {
  473. struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
  474. aic23->mclk = freq;
  475. return 0;
  476. }
  477. static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
  478. enum snd_soc_bias_level level)
  479. {
  480. u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f;
  481. switch (level) {
  482. case SND_SOC_BIAS_ON:
  483. /* vref/mid, osc on, dac unmute */
  484. reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
  485. TLV320AIC23_DAC_OFF);
  486. tlv320aic23_write(codec, TLV320AIC23_PWR, reg);
  487. break;
  488. case SND_SOC_BIAS_PREPARE:
  489. break;
  490. case SND_SOC_BIAS_STANDBY:
  491. /* everything off except vref/vmid, */
  492. tlv320aic23_write(codec, TLV320AIC23_PWR, reg | \
  493. TLV320AIC23_CLK_OFF);
  494. break;
  495. case SND_SOC_BIAS_OFF:
  496. /* everything off, dac mute, inactive */
  497. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
  498. tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff);
  499. break;
  500. }
  501. codec->dapm.bias_level = level;
  502. return 0;
  503. }
  504. #define AIC23_RATES SNDRV_PCM_RATE_8000_96000
  505. #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  506. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  507. static struct snd_soc_dai_ops tlv320aic23_dai_ops = {
  508. .prepare = tlv320aic23_pcm_prepare,
  509. .hw_params = tlv320aic23_hw_params,
  510. .shutdown = tlv320aic23_shutdown,
  511. .digital_mute = tlv320aic23_mute,
  512. .set_fmt = tlv320aic23_set_dai_fmt,
  513. .set_sysclk = tlv320aic23_set_dai_sysclk,
  514. };
  515. static struct snd_soc_dai_driver tlv320aic23_dai = {
  516. .name = "tlv320aic23-hifi",
  517. .playback = {
  518. .stream_name = "Playback",
  519. .channels_min = 2,
  520. .channels_max = 2,
  521. .rates = AIC23_RATES,
  522. .formats = AIC23_FORMATS,},
  523. .capture = {
  524. .stream_name = "Capture",
  525. .channels_min = 2,
  526. .channels_max = 2,
  527. .rates = AIC23_RATES,
  528. .formats = AIC23_FORMATS,},
  529. .ops = &tlv320aic23_dai_ops,
  530. };
  531. static int tlv320aic23_suspend(struct snd_soc_codec *codec,
  532. pm_message_t state)
  533. {
  534. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
  535. return 0;
  536. }
  537. static int tlv320aic23_resume(struct snd_soc_codec *codec)
  538. {
  539. u16 reg;
  540. /* Sync reg_cache with the hardware */
  541. for (reg = 0; reg <= TLV320AIC23_ACTIVE; reg++) {
  542. u16 val = tlv320aic23_read_reg_cache(codec, reg);
  543. tlv320aic23_write(codec, reg, val);
  544. }
  545. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  546. return 0;
  547. }
  548. static int tlv320aic23_probe(struct snd_soc_codec *codec)
  549. {
  550. struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
  551. int reg;
  552. printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
  553. codec->control_data = aic23->control_data;
  554. codec->hw_write = (hw_write_t)i2c_master_send;
  555. codec->hw_read = NULL;
  556. /* Reset codec */
  557. tlv320aic23_write(codec, TLV320AIC23_RESET, 0);
  558. /* power on device */
  559. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  560. tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
  561. /* Unmute input */
  562. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL);
  563. tlv320aic23_write(codec, TLV320AIC23_LINVOL,
  564. (reg & (~TLV320AIC23_LIM_MUTED)) |
  565. (TLV320AIC23_LRS_ENABLED));
  566. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL);
  567. tlv320aic23_write(codec, TLV320AIC23_RINVOL,
  568. (reg & (~TLV320AIC23_LIM_MUTED)) |
  569. TLV320AIC23_LRS_ENABLED);
  570. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG);
  571. tlv320aic23_write(codec, TLV320AIC23_ANLG,
  572. (reg) & (~TLV320AIC23_BYPASS_ON) &
  573. (~TLV320AIC23_MICM_MUTED));
  574. /* Default output volume */
  575. tlv320aic23_write(codec, TLV320AIC23_LCHNVOL,
  576. TLV320AIC23_DEFAULT_OUT_VOL &
  577. TLV320AIC23_OUT_VOL_MASK);
  578. tlv320aic23_write(codec, TLV320AIC23_RCHNVOL,
  579. TLV320AIC23_DEFAULT_OUT_VOL &
  580. TLV320AIC23_OUT_VOL_MASK);
  581. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1);
  582. snd_soc_add_controls(codec, tlv320aic23_snd_controls,
  583. ARRAY_SIZE(tlv320aic23_snd_controls));
  584. tlv320aic23_add_widgets(codec);
  585. return 0;
  586. }
  587. static int tlv320aic23_remove(struct snd_soc_codec *codec)
  588. {
  589. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
  590. return 0;
  591. }
  592. static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = {
  593. .reg_cache_size = ARRAY_SIZE(tlv320aic23_reg),
  594. .reg_word_size = sizeof(u16),
  595. .reg_cache_default = tlv320aic23_reg,
  596. .probe = tlv320aic23_probe,
  597. .remove = tlv320aic23_remove,
  598. .suspend = tlv320aic23_suspend,
  599. .resume = tlv320aic23_resume,
  600. .read = tlv320aic23_read_reg_cache,
  601. .write = tlv320aic23_write,
  602. .set_bias_level = tlv320aic23_set_bias_level,
  603. };
  604. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  605. /*
  606. * If the i2c layer weren't so broken, we could pass this kind of data
  607. * around
  608. */
  609. static int tlv320aic23_codec_probe(struct i2c_client *i2c,
  610. const struct i2c_device_id *i2c_id)
  611. {
  612. struct aic23 *aic23;
  613. int ret;
  614. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  615. return -EINVAL;
  616. aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL);
  617. if (aic23 == NULL)
  618. return -ENOMEM;
  619. i2c_set_clientdata(i2c, aic23);
  620. aic23->control_data = i2c;
  621. aic23->control_type = SND_SOC_I2C;
  622. ret = snd_soc_register_codec(&i2c->dev,
  623. &soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1);
  624. if (ret < 0)
  625. kfree(aic23);
  626. return ret;
  627. }
  628. static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
  629. {
  630. snd_soc_unregister_codec(&i2c->dev);
  631. kfree(i2c_get_clientdata(i2c));
  632. return 0;
  633. }
  634. static const struct i2c_device_id tlv320aic23_id[] = {
  635. {"tlv320aic23", 0},
  636. {}
  637. };
  638. MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
  639. static struct i2c_driver tlv320aic23_i2c_driver = {
  640. .driver = {
  641. .name = "tlv320aic23-codec",
  642. },
  643. .probe = tlv320aic23_codec_probe,
  644. .remove = __exit_p(tlv320aic23_i2c_remove),
  645. .id_table = tlv320aic23_id,
  646. };
  647. #endif
  648. static int __init tlv320aic23_modinit(void)
  649. {
  650. int ret;
  651. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  652. ret = i2c_add_driver(&tlv320aic23_i2c_driver);
  653. if (ret != 0) {
  654. printk(KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n",
  655. ret);
  656. }
  657. #endif
  658. return ret;
  659. }
  660. module_init(tlv320aic23_modinit);
  661. static void __exit tlv320aic23_exit(void)
  662. {
  663. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  664. i2c_del_driver(&tlv320aic23_i2c_driver);
  665. #endif
  666. }
  667. module_exit(tlv320aic23_exit);
  668. MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
  669. MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
  670. MODULE_LICENSE("GPL");