tlv320aic23.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/soc.h>
  31. #include <sound/soc-dapm.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 < 0 || 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. struct snd_soc_codec codec;
  201. int mclk;
  202. int requested_adc;
  203. int requested_dac;
  204. };
  205. /*
  206. * Common Crystals used
  207. * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
  208. * 12.0000 Mhz /125 = *96k /136 = 88.235K
  209. * 12.2880 Mhz /128 = *96k /192 = 64k
  210. * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
  211. * 18.4320 Mhz /128 = 144k /192 = *96k
  212. */
  213. /*
  214. * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
  215. * USB BOSR 0-250/2 = 125, 1-272/2 = 136
  216. */
  217. static const int bosr_usb_divisor_table[] = {
  218. 128, 125, 192, 136
  219. };
  220. #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
  221. #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
  222. static const unsigned short sr_valid_mask[] = {
  223. LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
  224. LOWER_GROUP, /* Usb, bosr - 0*/
  225. LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
  226. UPPER_GROUP, /* Usb, bosr - 1*/
  227. };
  228. /*
  229. * Every divisor is a factor of 11*12
  230. */
  231. #define SR_MULT (11*12)
  232. #define A(x) (SR_MULT/x)
  233. static const unsigned char sr_adc_mult_table[] = {
  234. A(2), A(2), A(12), A(12), 0, 0, A(3), A(1),
  235. A(2), A(2), A(11), A(11), 0, 0, 0, A(1)
  236. };
  237. static const unsigned char sr_dac_mult_table[] = {
  238. A(2), A(12), A(2), A(12), 0, 0, A(3), A(1),
  239. A(2), A(11), A(2), A(11), 0, 0, 0, A(1)
  240. };
  241. static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
  242. int dac, int dac_l, int dac_h, int need_dac)
  243. {
  244. if ((adc >= adc_l) && (adc <= adc_h) &&
  245. (dac >= dac_l) && (dac <= dac_h)) {
  246. int diff_adc = need_adc - adc;
  247. int diff_dac = need_dac - dac;
  248. return abs(diff_adc) + abs(diff_dac);
  249. }
  250. return UINT_MAX;
  251. }
  252. static int find_rate(int mclk, u32 need_adc, u32 need_dac)
  253. {
  254. int i, j;
  255. int best_i = -1;
  256. int best_j = -1;
  257. int best_div = 0;
  258. unsigned best_score = UINT_MAX;
  259. int adc_l, adc_h, dac_l, dac_h;
  260. need_adc *= SR_MULT;
  261. need_dac *= SR_MULT;
  262. /*
  263. * rates given are +/- 1/32
  264. */
  265. adc_l = need_adc - (need_adc >> 5);
  266. adc_h = need_adc + (need_adc >> 5);
  267. dac_l = need_dac - (need_dac >> 5);
  268. dac_h = need_dac + (need_dac >> 5);
  269. for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
  270. int base = mclk / bosr_usb_divisor_table[i];
  271. int mask = sr_valid_mask[i];
  272. for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
  273. j++, mask >>= 1) {
  274. int adc;
  275. int dac;
  276. int score;
  277. if ((mask & 1) == 0)
  278. continue;
  279. adc = base * sr_adc_mult_table[j];
  280. dac = base * sr_dac_mult_table[j];
  281. score = get_score(adc, adc_l, adc_h, need_adc,
  282. dac, dac_l, dac_h, need_dac);
  283. if (best_score > score) {
  284. best_score = score;
  285. best_i = i;
  286. best_j = j;
  287. best_div = 0;
  288. }
  289. score = get_score((adc >> 1), adc_l, adc_h, need_adc,
  290. (dac >> 1), dac_l, dac_h, need_dac);
  291. /* prefer to have a /2 */
  292. if ((score != UINT_MAX) && (best_score >= score)) {
  293. best_score = score;
  294. best_i = i;
  295. best_j = j;
  296. best_div = 1;
  297. }
  298. }
  299. }
  300. return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
  301. }
  302. #ifdef DEBUG
  303. static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk,
  304. u32 *sample_rate_adc, u32 *sample_rate_dac)
  305. {
  306. int src = tlv320aic23_read_reg_cache(codec, TLV320AIC23_SRATE);
  307. int sr = (src >> 2) & 0x0f;
  308. int val = (mclk / bosr_usb_divisor_table[src & 3]);
  309. int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
  310. int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
  311. if (src & TLV320AIC23_CLKIN_HALF) {
  312. adc >>= 1;
  313. dac >>= 1;
  314. }
  315. *sample_rate_adc = adc;
  316. *sample_rate_dac = dac;
  317. }
  318. #endif
  319. static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk,
  320. u32 sample_rate_adc, u32 sample_rate_dac)
  321. {
  322. /* Search for the right sample rate */
  323. int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
  324. if (data < 0) {
  325. printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
  326. __func__, sample_rate_adc, sample_rate_dac);
  327. return -EINVAL;
  328. }
  329. tlv320aic23_write(codec, TLV320AIC23_SRATE, data);
  330. #ifdef DEBUG
  331. {
  332. u32 adc, dac;
  333. get_current_sample_rates(codec, mclk, &adc, &dac);
  334. printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
  335. adc, dac, data);
  336. }
  337. #endif
  338. return 0;
  339. }
  340. static int tlv320aic23_add_widgets(struct snd_soc_codec *codec)
  341. {
  342. snd_soc_dapm_new_controls(codec, tlv320aic23_dapm_widgets,
  343. ARRAY_SIZE(tlv320aic23_dapm_widgets));
  344. /* set up audio path interconnects */
  345. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  346. snd_soc_dapm_new_widgets(codec);
  347. return 0;
  348. }
  349. static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
  350. struct snd_pcm_hw_params *params,
  351. struct snd_soc_dai *dai)
  352. {
  353. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  354. struct snd_soc_device *socdev = rtd->socdev;
  355. struct snd_soc_codec *codec = socdev->card->codec;
  356. u16 iface_reg;
  357. int ret;
  358. struct aic23 *aic23 = container_of(codec, struct aic23, 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_device *socdev = rtd->socdev;
  399. struct snd_soc_codec *codec = socdev->card->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_device *socdev = rtd->socdev;
  409. struct snd_soc_codec *codec = socdev->card->codec;
  410. struct aic23 *aic23 = container_of(codec, struct aic23, codec);
  411. /* deactivate */
  412. if (!codec->active) {
  413. udelay(50);
  414. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
  415. }
  416. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  417. aic23->requested_dac = 0;
  418. else
  419. aic23->requested_adc = 0;
  420. }
  421. static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
  422. {
  423. struct snd_soc_codec *codec = dai->codec;
  424. u16 reg;
  425. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT);
  426. if (mute)
  427. reg |= TLV320AIC23_DACM_MUTE;
  428. else
  429. reg &= ~TLV320AIC23_DACM_MUTE;
  430. tlv320aic23_write(codec, TLV320AIC23_DIGT, reg);
  431. return 0;
  432. }
  433. static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
  434. unsigned int fmt)
  435. {
  436. struct snd_soc_codec *codec = codec_dai->codec;
  437. u16 iface_reg;
  438. iface_reg =
  439. tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
  440. /* set master/slave audio interface */
  441. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  442. case SND_SOC_DAIFMT_CBM_CFM:
  443. iface_reg |= TLV320AIC23_MS_MASTER;
  444. break;
  445. case SND_SOC_DAIFMT_CBS_CFS:
  446. break;
  447. default:
  448. return -EINVAL;
  449. }
  450. /* interface format */
  451. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  452. case SND_SOC_DAIFMT_I2S:
  453. iface_reg |= TLV320AIC23_FOR_I2S;
  454. break;
  455. case SND_SOC_DAIFMT_DSP_A:
  456. iface_reg |= TLV320AIC23_LRP_ON;
  457. case SND_SOC_DAIFMT_DSP_B:
  458. iface_reg |= TLV320AIC23_FOR_DSP;
  459. break;
  460. case SND_SOC_DAIFMT_RIGHT_J:
  461. break;
  462. case SND_SOC_DAIFMT_LEFT_J:
  463. iface_reg |= TLV320AIC23_FOR_LJUST;
  464. break;
  465. default:
  466. return -EINVAL;
  467. }
  468. tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
  469. return 0;
  470. }
  471. static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  472. int clk_id, unsigned int freq, int dir)
  473. {
  474. struct snd_soc_codec *codec = codec_dai->codec;
  475. struct aic23 *aic23 = container_of(codec, struct aic23, codec);
  476. aic23->mclk = freq;
  477. return 0;
  478. }
  479. static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
  480. enum snd_soc_bias_level level)
  481. {
  482. u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f;
  483. switch (level) {
  484. case SND_SOC_BIAS_ON:
  485. /* vref/mid, osc on, dac unmute */
  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 | 0x0040);
  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. struct snd_soc_dai tlv320aic23_dai = {
  515. .name = "tlv320aic23",
  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. EXPORT_SYMBOL_GPL(tlv320aic23_dai);
  531. static int tlv320aic23_suspend(struct platform_device *pdev,
  532. pm_message_t state)
  533. {
  534. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  535. struct snd_soc_codec *codec = socdev->card->codec;
  536. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
  537. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
  538. return 0;
  539. }
  540. static int tlv320aic23_resume(struct platform_device *pdev)
  541. {
  542. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  543. struct snd_soc_codec *codec = socdev->card->codec;
  544. int i;
  545. u16 reg;
  546. /* Sync reg_cache with the hardware */
  547. for (reg = 0; reg < ARRAY_SIZE(tlv320aic23_reg); i++) {
  548. u16 val = tlv320aic23_read_reg_cache(codec, reg);
  549. tlv320aic23_write(codec, reg, val);
  550. }
  551. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  552. tlv320aic23_set_bias_level(codec, codec->suspend_bias_level);
  553. return 0;
  554. }
  555. /*
  556. * initialise the AIC23 driver
  557. * register the mixer and dsp interfaces with the kernel
  558. */
  559. static int tlv320aic23_init(struct snd_soc_device *socdev)
  560. {
  561. struct snd_soc_codec *codec = socdev->card->codec;
  562. int ret = 0;
  563. u16 reg;
  564. codec->name = "tlv320aic23";
  565. codec->owner = THIS_MODULE;
  566. codec->read = tlv320aic23_read_reg_cache;
  567. codec->write = tlv320aic23_write;
  568. codec->set_bias_level = tlv320aic23_set_bias_level;
  569. codec->dai = &tlv320aic23_dai;
  570. codec->num_dai = 1;
  571. codec->reg_cache_size = ARRAY_SIZE(tlv320aic23_reg);
  572. codec->reg_cache =
  573. kmemdup(tlv320aic23_reg, sizeof(tlv320aic23_reg), GFP_KERNEL);
  574. if (codec->reg_cache == NULL)
  575. return -ENOMEM;
  576. /* Reset codec */
  577. tlv320aic23_write(codec, TLV320AIC23_RESET, 0);
  578. /* register pcms */
  579. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  580. if (ret < 0) {
  581. printk(KERN_ERR "tlv320aic23: failed to create pcms\n");
  582. goto pcm_err;
  583. }
  584. /* power on device */
  585. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  586. tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
  587. /* Unmute input */
  588. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL);
  589. tlv320aic23_write(codec, TLV320AIC23_LINVOL,
  590. (reg & (~TLV320AIC23_LIM_MUTED)) |
  591. (TLV320AIC23_LRS_ENABLED));
  592. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL);
  593. tlv320aic23_write(codec, TLV320AIC23_RINVOL,
  594. (reg & (~TLV320AIC23_LIM_MUTED)) |
  595. TLV320AIC23_LRS_ENABLED);
  596. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG);
  597. tlv320aic23_write(codec, TLV320AIC23_ANLG,
  598. (reg) & (~TLV320AIC23_BYPASS_ON) &
  599. (~TLV320AIC23_MICM_MUTED));
  600. /* Default output volume */
  601. tlv320aic23_write(codec, TLV320AIC23_LCHNVOL,
  602. TLV320AIC23_DEFAULT_OUT_VOL &
  603. TLV320AIC23_OUT_VOL_MASK);
  604. tlv320aic23_write(codec, TLV320AIC23_RCHNVOL,
  605. TLV320AIC23_DEFAULT_OUT_VOL &
  606. TLV320AIC23_OUT_VOL_MASK);
  607. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1);
  608. snd_soc_add_controls(codec, tlv320aic23_snd_controls,
  609. ARRAY_SIZE(tlv320aic23_snd_controls));
  610. tlv320aic23_add_widgets(codec);
  611. ret = snd_soc_init_card(socdev);
  612. if (ret < 0) {
  613. printk(KERN_ERR "tlv320aic23: failed to register card\n");
  614. goto card_err;
  615. }
  616. return ret;
  617. card_err:
  618. snd_soc_free_pcms(socdev);
  619. snd_soc_dapm_free(socdev);
  620. pcm_err:
  621. kfree(codec->reg_cache);
  622. return ret;
  623. }
  624. static struct snd_soc_device *tlv320aic23_socdev;
  625. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  626. /*
  627. * If the i2c layer weren't so broken, we could pass this kind of data
  628. * around
  629. */
  630. static int tlv320aic23_codec_probe(struct i2c_client *i2c,
  631. const struct i2c_device_id *i2c_id)
  632. {
  633. struct snd_soc_device *socdev = tlv320aic23_socdev;
  634. struct snd_soc_codec *codec = socdev->card->codec;
  635. int ret;
  636. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  637. return -EINVAL;
  638. i2c_set_clientdata(i2c, codec);
  639. codec->control_data = i2c;
  640. ret = tlv320aic23_init(socdev);
  641. if (ret < 0) {
  642. printk(KERN_ERR "tlv320aic23: failed to initialise AIC23\n");
  643. goto err;
  644. }
  645. return ret;
  646. err:
  647. kfree(codec);
  648. kfree(i2c);
  649. return ret;
  650. }
  651. static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
  652. {
  653. put_device(&i2c->dev);
  654. return 0;
  655. }
  656. static const struct i2c_device_id tlv320aic23_id[] = {
  657. {"tlv320aic23", 0},
  658. {}
  659. };
  660. MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
  661. static struct i2c_driver tlv320aic23_i2c_driver = {
  662. .driver = {
  663. .name = "tlv320aic23",
  664. },
  665. .probe = tlv320aic23_codec_probe,
  666. .remove = __exit_p(tlv320aic23_i2c_remove),
  667. .id_table = tlv320aic23_id,
  668. };
  669. #endif
  670. static int tlv320aic23_probe(struct platform_device *pdev)
  671. {
  672. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  673. struct snd_soc_codec *codec;
  674. struct aic23 *aic23;
  675. int ret = 0;
  676. printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
  677. aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL);
  678. if (aic23 == NULL)
  679. return -ENOMEM;
  680. codec = &aic23->codec;
  681. socdev->card->codec = codec;
  682. mutex_init(&codec->mutex);
  683. INIT_LIST_HEAD(&codec->dapm_widgets);
  684. INIT_LIST_HEAD(&codec->dapm_paths);
  685. tlv320aic23_socdev = socdev;
  686. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  687. codec->hw_write = (hw_write_t) i2c_master_send;
  688. codec->hw_read = NULL;
  689. ret = i2c_add_driver(&tlv320aic23_i2c_driver);
  690. if (ret != 0)
  691. printk(KERN_ERR "can't add i2c driver");
  692. #endif
  693. return ret;
  694. }
  695. static int tlv320aic23_remove(struct platform_device *pdev)
  696. {
  697. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  698. struct snd_soc_codec *codec = socdev->card->codec;
  699. struct aic23 *aic23 = container_of(codec, struct aic23, codec);
  700. if (codec->control_data)
  701. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
  702. snd_soc_free_pcms(socdev);
  703. snd_soc_dapm_free(socdev);
  704. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  705. i2c_del_driver(&tlv320aic23_i2c_driver);
  706. #endif
  707. kfree(codec->reg_cache);
  708. kfree(aic23);
  709. return 0;
  710. }
  711. struct snd_soc_codec_device soc_codec_dev_tlv320aic23 = {
  712. .probe = tlv320aic23_probe,
  713. .remove = tlv320aic23_remove,
  714. .suspend = tlv320aic23_suspend,
  715. .resume = tlv320aic23_resume,
  716. };
  717. EXPORT_SYMBOL_GPL(soc_codec_dev_tlv320aic23);
  718. static int __init tlv320aic23_modinit(void)
  719. {
  720. return snd_soc_register_dai(&tlv320aic23_dai);
  721. }
  722. module_init(tlv320aic23_modinit);
  723. static void __exit tlv320aic23_exit(void)
  724. {
  725. snd_soc_unregister_dai(&tlv320aic23_dai);
  726. }
  727. module_exit(tlv320aic23_exit);
  728. MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
  729. MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
  730. MODULE_LICENSE("GPL");