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/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 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_add_widgets(struct snd_soc_codec *codec)
  342. {
  343. struct snd_soc_dapm_context *dapm = &codec->dapm;
  344. snd_soc_dapm_new_controls(dapm, tlv320aic23_dapm_widgets,
  345. ARRAY_SIZE(tlv320aic23_dapm_widgets));
  346. /* set up audio path interconnects */
  347. snd_soc_dapm_add_routes(dapm, 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->dapm.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");