tlv320aic23.c 19 KB

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