tlv320aic23.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 AUDIO_NAME "tlv320aic23"
  36. #define AIC23_VERSION "0.1"
  37. struct tlv320aic23_srate_reg_info {
  38. u32 sample_rate;
  39. u8 control; /* SR3, SR2, SR1, SR0 and BOSR */
  40. u8 divider; /* if 0 CLKIN = MCLK, if 1 CLKIN = MCLK/2 */
  41. };
  42. /*
  43. * AIC23 register cache
  44. */
  45. static const u16 tlv320aic23_reg[] = {
  46. 0x0097, 0x0097, 0x00F9, 0x00F9, /* 0 */
  47. 0x001A, 0x0004, 0x0007, 0x0001, /* 4 */
  48. 0x0020, 0x0000, 0x0000, 0x0000, /* 8 */
  49. 0x0000, 0x0000, 0x0000, 0x0000, /* 12 */
  50. };
  51. /*
  52. * read tlv320aic23 register cache
  53. */
  54. static inline unsigned int tlv320aic23_read_reg_cache(struct snd_soc_codec
  55. *codec, unsigned int reg)
  56. {
  57. u16 *cache = codec->reg_cache;
  58. if (reg >= ARRAY_SIZE(tlv320aic23_reg))
  59. return -1;
  60. return cache[reg];
  61. }
  62. /*
  63. * write tlv320aic23 register cache
  64. */
  65. static inline void tlv320aic23_write_reg_cache(struct snd_soc_codec *codec,
  66. u8 reg, u16 value)
  67. {
  68. u16 *cache = codec->reg_cache;
  69. if (reg >= ARRAY_SIZE(tlv320aic23_reg))
  70. return;
  71. cache[reg] = value;
  72. }
  73. /*
  74. * write to the tlv320aic23 register space
  75. */
  76. static int tlv320aic23_write(struct snd_soc_codec *codec, unsigned int reg,
  77. unsigned int value)
  78. {
  79. u8 data;
  80. /* TLV320AIC23 has 7 bit address and 9 bits of data
  81. * so we need to switch one data bit into reg and rest
  82. * of data into val
  83. */
  84. if ((reg < 0 || reg > 9) && (reg != 15)) {
  85. printk(KERN_WARNING "%s Invalid register R%d\n", __func__, reg);
  86. return -1;
  87. }
  88. data = (reg << 1) | (value >> 8 & 0x01);
  89. tlv320aic23_write_reg_cache(codec, reg, value);
  90. if (codec->hw_write(codec->control_data, data,
  91. (value & 0xff)) == 0)
  92. return 0;
  93. printk(KERN_ERR "%s cannot write %03x to register R%d\n", __func__,
  94. value, reg);
  95. return -EIO;
  96. }
  97. static const char *rec_src_text[] = { "Line", "Mic" };
  98. static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  99. static const struct soc_enum rec_src_enum =
  100. SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
  101. static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
  102. SOC_DAPM_ENUM("Input Select", rec_src_enum);
  103. static const struct soc_enum tlv320aic23_rec_src =
  104. SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
  105. static const struct soc_enum tlv320aic23_deemph =
  106. SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text);
  107. static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
  108. static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
  109. static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
  110. static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
  111. struct snd_ctl_elem_value *ucontrol)
  112. {
  113. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  114. u16 val, reg;
  115. val = (ucontrol->value.integer.value[0] & 0x07);
  116. /* linear conversion to userspace
  117. * 000 = -6db
  118. * 001 = -9db
  119. * 010 = -12db
  120. * 011 = -18db (Min)
  121. * 100 = 0db (Max)
  122. */
  123. val = (val >= 4) ? 4 : (3 - val);
  124. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG) & (~0x1C0);
  125. tlv320aic23_write(codec, TLV320AIC23_ANLG, reg | (val << 6));
  126. return 0;
  127. }
  128. static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
  129. struct snd_ctl_elem_value *ucontrol)
  130. {
  131. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  132. u16 val;
  133. val = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG) & (0x1C0);
  134. val = val >> 6;
  135. val = (val >= 4) ? 4 : (3 - val);
  136. ucontrol->value.integer.value[0] = val;
  137. return 0;
  138. }
  139. #define SOC_TLV320AIC23_SINGLE_TLV(xname, reg, shift, max, invert, tlv_array) \
  140. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  141. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
  142. SNDRV_CTL_ELEM_ACCESS_READWRITE,\
  143. .tlv.p = (tlv_array), \
  144. .info = snd_soc_info_volsw, .get = snd_soc_tlv320aic23_get_volsw,\
  145. .put = snd_soc_tlv320aic23_put_volsw, \
  146. .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
  147. static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
  148. SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
  149. TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
  150. SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
  151. SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
  152. TLV320AIC23_RINVOL, 7, 1, 0),
  153. SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
  154. TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
  155. SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
  156. SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
  157. SOC_TLV320AIC23_SINGLE_TLV("Sidetone Volume", TLV320AIC23_ANLG,
  158. 6, 4, 0, sidetone_vol_tlv),
  159. SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
  160. };
  161. /* add non dapm controls */
  162. static int tlv320aic23_add_controls(struct snd_soc_codec *codec)
  163. {
  164. int err, i;
  165. for (i = 0; i < ARRAY_SIZE(tlv320aic23_snd_controls); i++) {
  166. err = snd_ctl_add(codec->card,
  167. snd_soc_cnew(&tlv320aic23_snd_controls[i],
  168. codec, NULL));
  169. if (err < 0)
  170. return err;
  171. }
  172. return 0;
  173. }
  174. /* PGA Mixer controls for Line and Mic switch */
  175. static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
  176. SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
  177. SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
  178. SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
  179. };
  180. static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
  181. SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
  182. SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
  183. SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
  184. &tlv320aic23_rec_src_mux_controls),
  185. SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
  186. &tlv320aic23_output_mixer_controls[0],
  187. ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
  188. SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
  189. SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
  190. SND_SOC_DAPM_OUTPUT("LHPOUT"),
  191. SND_SOC_DAPM_OUTPUT("RHPOUT"),
  192. SND_SOC_DAPM_OUTPUT("LOUT"),
  193. SND_SOC_DAPM_OUTPUT("ROUT"),
  194. SND_SOC_DAPM_INPUT("LLINEIN"),
  195. SND_SOC_DAPM_INPUT("RLINEIN"),
  196. SND_SOC_DAPM_INPUT("MICIN"),
  197. };
  198. static const struct snd_soc_dapm_route intercon[] = {
  199. /* Output Mixer */
  200. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  201. {"Output Mixer", "Playback Switch", "DAC"},
  202. {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
  203. /* Outputs */
  204. {"RHPOUT", NULL, "Output Mixer"},
  205. {"LHPOUT", NULL, "Output Mixer"},
  206. {"LOUT", NULL, "Output Mixer"},
  207. {"ROUT", NULL, "Output Mixer"},
  208. /* Inputs */
  209. {"Line Input", "NULL", "LLINEIN"},
  210. {"Line Input", "NULL", "RLINEIN"},
  211. {"Mic Input", "NULL", "MICIN"},
  212. /* input mux */
  213. {"Capture Source", "Line", "Line Input"},
  214. {"Capture Source", "Mic", "Mic Input"},
  215. {"ADC", NULL, "Capture Source"},
  216. };
  217. /* tlv320aic23 related */
  218. static const struct tlv320aic23_srate_reg_info srate_reg_info[] = {
  219. {4000, 0x06, 1}, /* 4000 */
  220. {8000, 0x06, 0}, /* 8000 */
  221. {16000, 0x0C, 1}, /* 16000 */
  222. {22050, 0x11, 1}, /* 22050 */
  223. {24000, 0x00, 1}, /* 24000 */
  224. {32000, 0x0C, 0}, /* 32000 */
  225. {44100, 0x11, 0}, /* 44100 */
  226. {48000, 0x00, 0}, /* 48000 */
  227. {88200, 0x1F, 0}, /* 88200 */
  228. {96000, 0x0E, 0}, /* 96000 */
  229. };
  230. static int tlv320aic23_add_widgets(struct snd_soc_codec *codec)
  231. {
  232. snd_soc_dapm_new_controls(codec, tlv320aic23_dapm_widgets,
  233. ARRAY_SIZE(tlv320aic23_dapm_widgets));
  234. /* set up audio path interconnects */
  235. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  236. snd_soc_dapm_new_widgets(codec);
  237. return 0;
  238. }
  239. static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
  240. struct snd_pcm_hw_params *params)
  241. {
  242. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  243. struct snd_soc_device *socdev = rtd->socdev;
  244. struct snd_soc_codec *codec = socdev->codec;
  245. u16 iface_reg, data;
  246. u8 count = 0;
  247. iface_reg =
  248. tlv320aic23_read_reg_cache(codec,
  249. TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
  250. /* Search for the right sample rate */
  251. /* Verify what happens if the rate is not supported
  252. * now it goes to 96Khz */
  253. while ((srate_reg_info[count].sample_rate != params_rate(params)) &&
  254. (count < ARRAY_SIZE(srate_reg_info))) {
  255. count++;
  256. }
  257. data = (srate_reg_info[count].divider << TLV320AIC23_CLKIN_SHIFT) |
  258. (srate_reg_info[count]. control << TLV320AIC23_BOSR_SHIFT) |
  259. TLV320AIC23_USB_CLK_ON;
  260. tlv320aic23_write(codec, TLV320AIC23_SRATE, data);
  261. switch (params_format(params)) {
  262. case SNDRV_PCM_FORMAT_S16_LE:
  263. break;
  264. case SNDRV_PCM_FORMAT_S20_3LE:
  265. iface_reg |= (0x01 << 2);
  266. break;
  267. case SNDRV_PCM_FORMAT_S24_LE:
  268. iface_reg |= (0x02 << 2);
  269. break;
  270. case SNDRV_PCM_FORMAT_S32_LE:
  271. iface_reg |= (0x03 << 2);
  272. break;
  273. }
  274. tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
  275. return 0;
  276. }
  277. static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream)
  278. {
  279. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  280. struct snd_soc_device *socdev = rtd->socdev;
  281. struct snd_soc_codec *codec = socdev->codec;
  282. /* set active */
  283. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001);
  284. return 0;
  285. }
  286. static void tlv320aic23_shutdown(struct snd_pcm_substream *substream)
  287. {
  288. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  289. struct snd_soc_device *socdev = rtd->socdev;
  290. struct snd_soc_codec *codec = socdev->codec;
  291. /* deactivate */
  292. if (!codec->active) {
  293. udelay(50);
  294. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
  295. }
  296. }
  297. static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
  298. {
  299. struct snd_soc_codec *codec = dai->codec;
  300. u16 reg;
  301. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT);
  302. if (mute)
  303. reg |= TLV320AIC23_DACM_MUTE;
  304. else
  305. reg &= ~TLV320AIC23_DACM_MUTE;
  306. tlv320aic23_write(codec, TLV320AIC23_DIGT, reg);
  307. return 0;
  308. }
  309. static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
  310. unsigned int fmt)
  311. {
  312. struct snd_soc_codec *codec = codec_dai->codec;
  313. u16 iface_reg;
  314. iface_reg =
  315. tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
  316. /* set master/slave audio interface */
  317. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  318. case SND_SOC_DAIFMT_CBM_CFM:
  319. iface_reg |= TLV320AIC23_MS_MASTER;
  320. break;
  321. case SND_SOC_DAIFMT_CBS_CFS:
  322. break;
  323. default:
  324. return -EINVAL;
  325. }
  326. /* interface format */
  327. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  328. case SND_SOC_DAIFMT_I2S:
  329. iface_reg |= TLV320AIC23_FOR_I2S;
  330. break;
  331. case SND_SOC_DAIFMT_DSP_A:
  332. iface_reg |= TLV320AIC23_FOR_DSP;
  333. break;
  334. case SND_SOC_DAIFMT_RIGHT_J:
  335. break;
  336. case SND_SOC_DAIFMT_LEFT_J:
  337. iface_reg |= TLV320AIC23_FOR_LJUST;
  338. break;
  339. default:
  340. return -EINVAL;
  341. }
  342. tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
  343. return 0;
  344. }
  345. static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  346. int clk_id, unsigned int freq, int dir)
  347. {
  348. struct snd_soc_codec *codec = codec_dai->codec;
  349. switch (freq) {
  350. case 12000000:
  351. return 0;
  352. }
  353. return -EINVAL;
  354. }
  355. static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
  356. enum snd_soc_bias_level level)
  357. {
  358. u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f;
  359. switch (level) {
  360. case SND_SOC_BIAS_ON:
  361. /* vref/mid, osc on, dac unmute */
  362. tlv320aic23_write(codec, TLV320AIC23_PWR, reg);
  363. break;
  364. case SND_SOC_BIAS_PREPARE:
  365. break;
  366. case SND_SOC_BIAS_STANDBY:
  367. /* everything off except vref/vmid, */
  368. tlv320aic23_write(codec, TLV320AIC23_PWR, reg | 0x0040);
  369. break;
  370. case SND_SOC_BIAS_OFF:
  371. /* everything off, dac mute, inactive */
  372. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
  373. tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff);
  374. break;
  375. }
  376. codec->bias_level = level;
  377. return 0;
  378. }
  379. #define AIC23_RATES SNDRV_PCM_RATE_8000_96000
  380. #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  381. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  382. struct snd_soc_dai tlv320aic23_dai = {
  383. .name = "tlv320aic23",
  384. .playback = {
  385. .stream_name = "Playback",
  386. .channels_min = 2,
  387. .channels_max = 2,
  388. .rates = AIC23_RATES,
  389. .formats = AIC23_FORMATS,},
  390. .capture = {
  391. .stream_name = "Capture",
  392. .channels_min = 2,
  393. .channels_max = 2,
  394. .rates = AIC23_RATES,
  395. .formats = AIC23_FORMATS,},
  396. .ops = {
  397. .prepare = tlv320aic23_pcm_prepare,
  398. .hw_params = tlv320aic23_hw_params,
  399. .shutdown = tlv320aic23_shutdown,
  400. },
  401. .dai_ops = {
  402. .digital_mute = tlv320aic23_mute,
  403. .set_fmt = tlv320aic23_set_dai_fmt,
  404. .set_sysclk = tlv320aic23_set_dai_sysclk,
  405. }
  406. };
  407. EXPORT_SYMBOL_GPL(tlv320aic23_dai);
  408. static int tlv320aic23_suspend(struct platform_device *pdev,
  409. pm_message_t state)
  410. {
  411. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  412. struct snd_soc_codec *codec = socdev->codec;
  413. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
  414. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
  415. return 0;
  416. }
  417. static int tlv320aic23_resume(struct platform_device *pdev)
  418. {
  419. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  420. struct snd_soc_codec *codec = socdev->codec;
  421. int i;
  422. u16 reg;
  423. /* Sync reg_cache with the hardware */
  424. for (reg = 0; reg < ARRAY_SIZE(tlv320aic23_reg); i++) {
  425. u16 val = tlv320aic23_read_reg_cache(codec, reg);
  426. tlv320aic23_write(codec, reg, val);
  427. }
  428. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  429. tlv320aic23_set_bias_level(codec, codec->suspend_bias_level);
  430. return 0;
  431. }
  432. /*
  433. * initialise the AIC23 driver
  434. * register the mixer and dsp interfaces with the kernel
  435. */
  436. static int tlv320aic23_init(struct snd_soc_device *socdev)
  437. {
  438. struct snd_soc_codec *codec = socdev->codec;
  439. int ret = 0;
  440. u16 reg;
  441. codec->name = "tlv320aic23";
  442. codec->owner = THIS_MODULE;
  443. codec->read = tlv320aic23_read_reg_cache;
  444. codec->write = tlv320aic23_write;
  445. codec->set_bias_level = tlv320aic23_set_bias_level;
  446. codec->dai = &tlv320aic23_dai;
  447. codec->num_dai = 1;
  448. codec->reg_cache_size = ARRAY_SIZE(tlv320aic23_reg);
  449. codec->reg_cache =
  450. kmemdup(tlv320aic23_reg, sizeof(tlv320aic23_reg), GFP_KERNEL);
  451. if (codec->reg_cache == NULL)
  452. return -ENOMEM;
  453. /* Reset codec */
  454. tlv320aic23_write(codec, TLV320AIC23_RESET, 0);
  455. /* register pcms */
  456. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  457. if (ret < 0) {
  458. printk(KERN_ERR "tlv320aic23: failed to create pcms\n");
  459. goto pcm_err;
  460. }
  461. /* power on device */
  462. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  463. tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
  464. /* Unmute input */
  465. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL);
  466. tlv320aic23_write(codec, TLV320AIC23_LINVOL,
  467. (reg & (~TLV320AIC23_LIM_MUTED)) |
  468. (TLV320AIC23_LRS_ENABLED));
  469. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL);
  470. tlv320aic23_write(codec, TLV320AIC23_RINVOL,
  471. (reg & (~TLV320AIC23_LIM_MUTED)) |
  472. TLV320AIC23_LRS_ENABLED);
  473. reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG);
  474. tlv320aic23_write(codec, TLV320AIC23_ANLG,
  475. (reg) & (~TLV320AIC23_BYPASS_ON) &
  476. (~TLV320AIC23_MICM_MUTED));
  477. /* Default output volume */
  478. tlv320aic23_write(codec, TLV320AIC23_LCHNVOL,
  479. TLV320AIC23_DEFAULT_OUT_VOL &
  480. TLV320AIC23_OUT_VOL_MASK);
  481. tlv320aic23_write(codec, TLV320AIC23_RCHNVOL,
  482. TLV320AIC23_DEFAULT_OUT_VOL &
  483. TLV320AIC23_OUT_VOL_MASK);
  484. tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1);
  485. tlv320aic23_add_controls(codec);
  486. tlv320aic23_add_widgets(codec);
  487. ret = snd_soc_register_card(socdev);
  488. if (ret < 0) {
  489. printk(KERN_ERR "tlv320aic23: failed to register card\n");
  490. goto card_err;
  491. }
  492. return ret;
  493. card_err:
  494. snd_soc_free_pcms(socdev);
  495. snd_soc_dapm_free(socdev);
  496. pcm_err:
  497. kfree(codec->reg_cache);
  498. return ret;
  499. }
  500. static struct snd_soc_device *tlv320aic23_socdev;
  501. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  502. /*
  503. * If the i2c layer weren't so broken, we could pass this kind of data
  504. * around
  505. */
  506. static int tlv320aic23_codec_probe(struct i2c_client *i2c,
  507. const struct i2c_device_id *i2c_id)
  508. {
  509. struct snd_soc_device *socdev = tlv320aic23_socdev;
  510. struct snd_soc_codec *codec = socdev->codec;
  511. int ret;
  512. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  513. return -EINVAL;
  514. i2c_set_clientdata(i2c, codec);
  515. codec->control_data = i2c;
  516. ret = tlv320aic23_init(socdev);
  517. if (ret < 0) {
  518. printk(KERN_ERR "tlv320aic23: failed to initialise AIC23\n");
  519. goto err;
  520. }
  521. return ret;
  522. err:
  523. kfree(codec);
  524. kfree(i2c);
  525. return ret;
  526. }
  527. static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
  528. {
  529. put_device(&i2c->dev);
  530. return 0;
  531. }
  532. static const struct i2c_device_id tlv320aic23_id[] = {
  533. {"tlv320aic23", 0},
  534. {}
  535. };
  536. MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
  537. static struct i2c_driver tlv320aic23_i2c_driver = {
  538. .driver = {
  539. .name = "tlv320aic23",
  540. },
  541. .probe = tlv320aic23_codec_probe,
  542. .remove = __exit_p(tlv320aic23_i2c_remove),
  543. .id_table = tlv320aic23_id,
  544. };
  545. #endif
  546. static int tlv320aic23_probe(struct platform_device *pdev)
  547. {
  548. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  549. struct snd_soc_codec *codec;
  550. int ret = 0;
  551. printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
  552. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  553. if (codec == NULL)
  554. return -ENOMEM;
  555. socdev->codec = codec;
  556. mutex_init(&codec->mutex);
  557. INIT_LIST_HEAD(&codec->dapm_widgets);
  558. INIT_LIST_HEAD(&codec->dapm_paths);
  559. tlv320aic23_socdev = socdev;
  560. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  561. codec->hw_write = (hw_write_t) i2c_smbus_write_byte_data;
  562. codec->hw_read = NULL;
  563. ret = i2c_add_driver(&tlv320aic23_i2c_driver);
  564. if (ret != 0)
  565. printk(KERN_ERR "can't add i2c driver");
  566. #endif
  567. return ret;
  568. }
  569. static int tlv320aic23_remove(struct platform_device *pdev)
  570. {
  571. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  572. struct snd_soc_codec *codec = socdev->codec;
  573. if (codec->control_data)
  574. tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
  575. snd_soc_free_pcms(socdev);
  576. snd_soc_dapm_free(socdev);
  577. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  578. i2c_del_driver(&tlv320aic23_i2c_driver);
  579. #endif
  580. kfree(codec->reg_cache);
  581. kfree(codec);
  582. return 0;
  583. }
  584. struct snd_soc_codec_device soc_codec_dev_tlv320aic23 = {
  585. .probe = tlv320aic23_probe,
  586. .remove = tlv320aic23_remove,
  587. .suspend = tlv320aic23_suspend,
  588. .resume = tlv320aic23_resume,
  589. };
  590. EXPORT_SYMBOL_GPL(soc_codec_dev_tlv320aic23);
  591. MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
  592. MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
  593. MODULE_LICENSE("GPL");