cs42l51.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * cs42l51.c
  3. *
  4. * ASoC Driver for Cirrus Logic CS42L51 codecs
  5. *
  6. * Copyright (c) 2010 Arnaud Patard <apatard@mandriva.com>
  7. *
  8. * Based on cs4270.c - Copyright (c) Freescale Semiconductor
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * For now:
  20. * - Only I2C is support. Not SPI
  21. * - master mode *NOT* supported
  22. */
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <sound/core.h>
  27. #include <sound/soc.h>
  28. #include <sound/tlv.h>
  29. #include <sound/initval.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/pcm.h>
  32. #include <linux/i2c.h>
  33. #include "cs42l51.h"
  34. enum master_slave_mode {
  35. MODE_SLAVE,
  36. MODE_SLAVE_AUTO,
  37. MODE_MASTER,
  38. };
  39. struct cs42l51_private {
  40. enum snd_soc_control_type control_type;
  41. unsigned int mclk;
  42. unsigned int audio_mode; /* The mode (I2S or left-justified) */
  43. enum master_slave_mode func;
  44. };
  45. #define CS42L51_FORMATS ( \
  46. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  47. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
  48. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
  49. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
  50. static int cs42l51_fill_cache(struct snd_soc_codec *codec)
  51. {
  52. u8 *cache = codec->reg_cache + 1;
  53. struct i2c_client *i2c_client = to_i2c_client(codec->dev);
  54. s32 length;
  55. length = i2c_smbus_read_i2c_block_data(i2c_client,
  56. CS42L51_FIRSTREG | 0x80, CS42L51_NUMREGS, cache);
  57. if (length != CS42L51_NUMREGS) {
  58. dev_err(&i2c_client->dev,
  59. "I2C read failure, addr=0x%x (ret=%d vs %d)\n",
  60. i2c_client->addr, length, CS42L51_NUMREGS);
  61. return -EIO;
  62. }
  63. return 0;
  64. }
  65. static int cs42l51_get_chan_mix(struct snd_kcontrol *kcontrol,
  66. struct snd_ctl_elem_value *ucontrol)
  67. {
  68. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  69. unsigned long value = snd_soc_read(codec, CS42L51_PCM_MIXER)&3;
  70. switch (value) {
  71. default:
  72. case 0:
  73. ucontrol->value.integer.value[0] = 0;
  74. break;
  75. /* same value : (L+R)/2 and (R+L)/2 */
  76. case 1:
  77. case 2:
  78. ucontrol->value.integer.value[0] = 1;
  79. break;
  80. case 3:
  81. ucontrol->value.integer.value[0] = 2;
  82. break;
  83. }
  84. return 0;
  85. }
  86. #define CHAN_MIX_NORMAL 0x00
  87. #define CHAN_MIX_BOTH 0x55
  88. #define CHAN_MIX_SWAP 0xFF
  89. static int cs42l51_set_chan_mix(struct snd_kcontrol *kcontrol,
  90. struct snd_ctl_elem_value *ucontrol)
  91. {
  92. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  93. unsigned char val;
  94. switch (ucontrol->value.integer.value[0]) {
  95. default:
  96. case 0:
  97. val = CHAN_MIX_NORMAL;
  98. break;
  99. case 1:
  100. val = CHAN_MIX_BOTH;
  101. break;
  102. case 2:
  103. val = CHAN_MIX_SWAP;
  104. break;
  105. }
  106. snd_soc_write(codec, CS42L51_PCM_MIXER, val);
  107. return 1;
  108. }
  109. static const DECLARE_TLV_DB_SCALE(adc_pcm_tlv, -5150, 50, 0);
  110. static const DECLARE_TLV_DB_SCALE(tone_tlv, -1050, 150, 0);
  111. /* This is a lie. after -102 db, it stays at -102 */
  112. /* maybe a range would be better */
  113. static const DECLARE_TLV_DB_SCALE(aout_tlv, -11550, 50, 0);
  114. static const DECLARE_TLV_DB_SCALE(boost_tlv, 1600, 1600, 0);
  115. static const char *chan_mix[] = {
  116. "L R",
  117. "L+R",
  118. "R L",
  119. };
  120. static const struct soc_enum cs42l51_chan_mix =
  121. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(chan_mix), chan_mix);
  122. static const struct snd_kcontrol_new cs42l51_snd_controls[] = {
  123. SOC_DOUBLE_R_SX_TLV("PCM Playback Volume",
  124. CS42L51_PCMA_VOL, CS42L51_PCMB_VOL,
  125. 7, 0xffffff99, 0x18, adc_pcm_tlv),
  126. SOC_DOUBLE_R("PCM Playback Switch",
  127. CS42L51_PCMA_VOL, CS42L51_PCMB_VOL, 7, 1, 1),
  128. SOC_DOUBLE_R_SX_TLV("Analog Playback Volume",
  129. CS42L51_AOUTA_VOL, CS42L51_AOUTB_VOL,
  130. 8, 0xffffff19, 0x18, aout_tlv),
  131. SOC_DOUBLE_R_SX_TLV("ADC Mixer Volume",
  132. CS42L51_ADCA_VOL, CS42L51_ADCB_VOL,
  133. 7, 0xffffff99, 0x18, adc_pcm_tlv),
  134. SOC_DOUBLE_R("ADC Mixer Switch",
  135. CS42L51_ADCA_VOL, CS42L51_ADCB_VOL, 7, 1, 1),
  136. SOC_SINGLE("Playback Deemphasis Switch", CS42L51_DAC_CTL, 3, 1, 0),
  137. SOC_SINGLE("Auto-Mute Switch", CS42L51_DAC_CTL, 2, 1, 0),
  138. SOC_SINGLE("Soft Ramp Switch", CS42L51_DAC_CTL, 1, 1, 0),
  139. SOC_SINGLE("Zero Cross Switch", CS42L51_DAC_CTL, 0, 0, 0),
  140. SOC_DOUBLE_TLV("Mic Boost Volume",
  141. CS42L51_MIC_CTL, 0, 1, 1, 0, boost_tlv),
  142. SOC_SINGLE_TLV("Bass Volume", CS42L51_TONE_CTL, 0, 0xf, 1, tone_tlv),
  143. SOC_SINGLE_TLV("Treble Volume", CS42L51_TONE_CTL, 4, 0xf, 1, tone_tlv),
  144. SOC_ENUM_EXT("PCM channel mixer",
  145. cs42l51_chan_mix,
  146. cs42l51_get_chan_mix, cs42l51_set_chan_mix),
  147. };
  148. /*
  149. * to power down, one must:
  150. * 1.) Enable the PDN bit
  151. * 2.) enable power-down for the select channels
  152. * 3.) disable the PDN bit.
  153. */
  154. static int cs42l51_pdn_event(struct snd_soc_dapm_widget *w,
  155. struct snd_kcontrol *kcontrol, int event)
  156. {
  157. unsigned long value;
  158. value = snd_soc_read(w->codec, CS42L51_POWER_CTL1);
  159. value &= ~CS42L51_POWER_CTL1_PDN;
  160. switch (event) {
  161. case SND_SOC_DAPM_PRE_PMD:
  162. value |= CS42L51_POWER_CTL1_PDN;
  163. break;
  164. default:
  165. case SND_SOC_DAPM_POST_PMD:
  166. break;
  167. }
  168. snd_soc_update_bits(w->codec, CS42L51_POWER_CTL1,
  169. CS42L51_POWER_CTL1_PDN, value);
  170. return 0;
  171. }
  172. static const char *cs42l51_dac_names[] = {"Direct PCM",
  173. "DSP PCM", "ADC"};
  174. static const struct soc_enum cs42l51_dac_mux_enum =
  175. SOC_ENUM_SINGLE(CS42L51_DAC_CTL, 6, 3, cs42l51_dac_names);
  176. static const struct snd_kcontrol_new cs42l51_dac_mux_controls =
  177. SOC_DAPM_ENUM("Route", cs42l51_dac_mux_enum);
  178. static const char *cs42l51_adcl_names[] = {"AIN1 Left", "AIN2 Left",
  179. "MIC Left", "MIC+preamp Left"};
  180. static const struct soc_enum cs42l51_adcl_mux_enum =
  181. SOC_ENUM_SINGLE(CS42L51_ADC_INPUT, 4, 4, cs42l51_adcl_names);
  182. static const struct snd_kcontrol_new cs42l51_adcl_mux_controls =
  183. SOC_DAPM_ENUM("Route", cs42l51_adcl_mux_enum);
  184. static const char *cs42l51_adcr_names[] = {"AIN1 Right", "AIN2 Right",
  185. "MIC Right", "MIC+preamp Right"};
  186. static const struct soc_enum cs42l51_adcr_mux_enum =
  187. SOC_ENUM_SINGLE(CS42L51_ADC_INPUT, 6, 4, cs42l51_adcr_names);
  188. static const struct snd_kcontrol_new cs42l51_adcr_mux_controls =
  189. SOC_DAPM_ENUM("Route", cs42l51_adcr_mux_enum);
  190. static const struct snd_soc_dapm_widget cs42l51_dapm_widgets[] = {
  191. SND_SOC_DAPM_MICBIAS("Mic Bias", CS42L51_MIC_POWER_CTL, 1, 1),
  192. SND_SOC_DAPM_PGA_E("Left PGA", CS42L51_POWER_CTL1, 3, 1, NULL, 0,
  193. cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
  194. SND_SOC_DAPM_PGA_E("Right PGA", CS42L51_POWER_CTL1, 4, 1, NULL, 0,
  195. cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
  196. SND_SOC_DAPM_ADC_E("Left ADC", "Left HiFi Capture",
  197. CS42L51_POWER_CTL1, 1, 1,
  198. cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
  199. SND_SOC_DAPM_ADC_E("Right ADC", "Right HiFi Capture",
  200. CS42L51_POWER_CTL1, 2, 1,
  201. cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
  202. SND_SOC_DAPM_DAC_E("Left DAC", "Left HiFi Playback",
  203. CS42L51_POWER_CTL1, 5, 1,
  204. cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
  205. SND_SOC_DAPM_DAC_E("Right DAC", "Right HiFi Playback",
  206. CS42L51_POWER_CTL1, 6, 1,
  207. cs42l51_pdn_event, SND_SOC_DAPM_PRE_POST_PMD),
  208. /* analog/mic */
  209. SND_SOC_DAPM_INPUT("AIN1L"),
  210. SND_SOC_DAPM_INPUT("AIN1R"),
  211. SND_SOC_DAPM_INPUT("AIN2L"),
  212. SND_SOC_DAPM_INPUT("AIN2R"),
  213. SND_SOC_DAPM_INPUT("MICL"),
  214. SND_SOC_DAPM_INPUT("MICR"),
  215. SND_SOC_DAPM_MIXER("Mic Preamp Left",
  216. CS42L51_MIC_POWER_CTL, 2, 1, NULL, 0),
  217. SND_SOC_DAPM_MIXER("Mic Preamp Right",
  218. CS42L51_MIC_POWER_CTL, 3, 1, NULL, 0),
  219. /* HP */
  220. SND_SOC_DAPM_OUTPUT("HPL"),
  221. SND_SOC_DAPM_OUTPUT("HPR"),
  222. /* mux */
  223. SND_SOC_DAPM_MUX("DAC Mux", SND_SOC_NOPM, 0, 0,
  224. &cs42l51_dac_mux_controls),
  225. SND_SOC_DAPM_MUX("PGA-ADC Mux Left", SND_SOC_NOPM, 0, 0,
  226. &cs42l51_adcl_mux_controls),
  227. SND_SOC_DAPM_MUX("PGA-ADC Mux Right", SND_SOC_NOPM, 0, 0,
  228. &cs42l51_adcr_mux_controls),
  229. };
  230. static const struct snd_soc_dapm_route cs42l51_routes[] = {
  231. {"HPL", NULL, "Left DAC"},
  232. {"HPR", NULL, "Right DAC"},
  233. {"Left ADC", NULL, "Left PGA"},
  234. {"Right ADC", NULL, "Right PGA"},
  235. {"Mic Preamp Left", NULL, "MICL"},
  236. {"Mic Preamp Right", NULL, "MICR"},
  237. {"PGA-ADC Mux Left", "AIN1 Left", "AIN1L" },
  238. {"PGA-ADC Mux Left", "AIN2 Left", "AIN2L" },
  239. {"PGA-ADC Mux Left", "MIC Left", "MICL" },
  240. {"PGA-ADC Mux Left", "MIC+preamp Left", "Mic Preamp Left" },
  241. {"PGA-ADC Mux Right", "AIN1 Right", "AIN1R" },
  242. {"PGA-ADC Mux Right", "AIN2 Right", "AIN2R" },
  243. {"PGA-ADC Mux Right", "MIC Right", "MICR" },
  244. {"PGA-ADC Mux Right", "MIC+preamp Right", "Mic Preamp Right" },
  245. {"Left PGA", NULL, "PGA-ADC Mux Left"},
  246. {"Right PGA", NULL, "PGA-ADC Mux Right"},
  247. };
  248. static int cs42l51_set_dai_fmt(struct snd_soc_dai *codec_dai,
  249. unsigned int format)
  250. {
  251. struct snd_soc_codec *codec = codec_dai->codec;
  252. struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
  253. int ret = 0;
  254. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  255. case SND_SOC_DAIFMT_I2S:
  256. case SND_SOC_DAIFMT_LEFT_J:
  257. case SND_SOC_DAIFMT_RIGHT_J:
  258. cs42l51->audio_mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
  259. break;
  260. default:
  261. dev_err(codec->dev, "invalid DAI format\n");
  262. ret = -EINVAL;
  263. }
  264. switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
  265. case SND_SOC_DAIFMT_CBM_CFM:
  266. cs42l51->func = MODE_MASTER;
  267. break;
  268. case SND_SOC_DAIFMT_CBS_CFS:
  269. cs42l51->func = MODE_SLAVE_AUTO;
  270. break;
  271. default:
  272. ret = -EINVAL;
  273. break;
  274. }
  275. return ret;
  276. }
  277. struct cs42l51_ratios {
  278. unsigned int ratio;
  279. unsigned char speed_mode;
  280. unsigned char mclk;
  281. };
  282. static struct cs42l51_ratios slave_ratios[] = {
  283. { 512, CS42L51_QSM_MODE, 0 }, { 768, CS42L51_QSM_MODE, 0 },
  284. { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
  285. { 2048, CS42L51_QSM_MODE, 0 }, { 3072, CS42L51_QSM_MODE, 0 },
  286. { 256, CS42L51_HSM_MODE, 0 }, { 384, CS42L51_HSM_MODE, 0 },
  287. { 512, CS42L51_HSM_MODE, 0 }, { 768, CS42L51_HSM_MODE, 0 },
  288. { 1024, CS42L51_HSM_MODE, 0 }, { 1536, CS42L51_HSM_MODE, 0 },
  289. { 128, CS42L51_SSM_MODE, 0 }, { 192, CS42L51_SSM_MODE, 0 },
  290. { 256, CS42L51_SSM_MODE, 0 }, { 384, CS42L51_SSM_MODE, 0 },
  291. { 512, CS42L51_SSM_MODE, 0 }, { 768, CS42L51_SSM_MODE, 0 },
  292. { 128, CS42L51_DSM_MODE, 0 }, { 192, CS42L51_DSM_MODE, 0 },
  293. { 256, CS42L51_DSM_MODE, 0 }, { 384, CS42L51_DSM_MODE, 0 },
  294. };
  295. static struct cs42l51_ratios slave_auto_ratios[] = {
  296. { 1024, CS42L51_QSM_MODE, 0 }, { 1536, CS42L51_QSM_MODE, 0 },
  297. { 2048, CS42L51_QSM_MODE, 1 }, { 3072, CS42L51_QSM_MODE, 1 },
  298. { 512, CS42L51_HSM_MODE, 0 }, { 768, CS42L51_HSM_MODE, 0 },
  299. { 1024, CS42L51_HSM_MODE, 1 }, { 1536, CS42L51_HSM_MODE, 1 },
  300. { 256, CS42L51_SSM_MODE, 0 }, { 384, CS42L51_SSM_MODE, 0 },
  301. { 512, CS42L51_SSM_MODE, 1 }, { 768, CS42L51_SSM_MODE, 1 },
  302. { 128, CS42L51_DSM_MODE, 0 }, { 192, CS42L51_DSM_MODE, 0 },
  303. { 256, CS42L51_DSM_MODE, 1 }, { 384, CS42L51_DSM_MODE, 1 },
  304. };
  305. static int cs42l51_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  306. int clk_id, unsigned int freq, int dir)
  307. {
  308. struct snd_soc_codec *codec = codec_dai->codec;
  309. struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
  310. cs42l51->mclk = freq;
  311. return 0;
  312. }
  313. static int cs42l51_hw_params(struct snd_pcm_substream *substream,
  314. struct snd_pcm_hw_params *params,
  315. struct snd_soc_dai *dai)
  316. {
  317. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  318. struct snd_soc_codec *codec = rtd->codec;
  319. struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
  320. int ret;
  321. unsigned int i;
  322. unsigned int rate;
  323. unsigned int ratio;
  324. struct cs42l51_ratios *ratios = NULL;
  325. int nr_ratios = 0;
  326. int intf_ctl, power_ctl, fmt;
  327. switch (cs42l51->func) {
  328. case MODE_MASTER:
  329. return -EINVAL;
  330. case MODE_SLAVE:
  331. ratios = slave_ratios;
  332. nr_ratios = ARRAY_SIZE(slave_ratios);
  333. break;
  334. case MODE_SLAVE_AUTO:
  335. ratios = slave_auto_ratios;
  336. nr_ratios = ARRAY_SIZE(slave_auto_ratios);
  337. break;
  338. }
  339. /* Figure out which MCLK/LRCK ratio to use */
  340. rate = params_rate(params); /* Sampling rate, in Hz */
  341. ratio = cs42l51->mclk / rate; /* MCLK/LRCK ratio */
  342. for (i = 0; i < nr_ratios; i++) {
  343. if (ratios[i].ratio == ratio)
  344. break;
  345. }
  346. if (i == nr_ratios) {
  347. /* We did not find a matching ratio */
  348. dev_err(codec->dev, "could not find matching ratio\n");
  349. return -EINVAL;
  350. }
  351. intf_ctl = snd_soc_read(codec, CS42L51_INTF_CTL);
  352. power_ctl = snd_soc_read(codec, CS42L51_MIC_POWER_CTL);
  353. intf_ctl &= ~(CS42L51_INTF_CTL_MASTER | CS42L51_INTF_CTL_ADC_I2S
  354. | CS42L51_INTF_CTL_DAC_FORMAT(7));
  355. power_ctl &= ~(CS42L51_MIC_POWER_CTL_SPEED(3)
  356. | CS42L51_MIC_POWER_CTL_MCLK_DIV2);
  357. switch (cs42l51->func) {
  358. case MODE_MASTER:
  359. intf_ctl |= CS42L51_INTF_CTL_MASTER;
  360. power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
  361. break;
  362. case MODE_SLAVE:
  363. power_ctl |= CS42L51_MIC_POWER_CTL_SPEED(ratios[i].speed_mode);
  364. break;
  365. case MODE_SLAVE_AUTO:
  366. power_ctl |= CS42L51_MIC_POWER_CTL_AUTO;
  367. break;
  368. }
  369. switch (cs42l51->audio_mode) {
  370. case SND_SOC_DAIFMT_I2S:
  371. intf_ctl |= CS42L51_INTF_CTL_ADC_I2S;
  372. intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_I2S);
  373. break;
  374. case SND_SOC_DAIFMT_LEFT_J:
  375. intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(CS42L51_DAC_DIF_LJ24);
  376. break;
  377. case SND_SOC_DAIFMT_RIGHT_J:
  378. switch (params_format(params)) {
  379. case SNDRV_PCM_FORMAT_S16_LE:
  380. case SNDRV_PCM_FORMAT_S16_BE:
  381. fmt = CS42L51_DAC_DIF_RJ16;
  382. break;
  383. case SNDRV_PCM_FORMAT_S18_3LE:
  384. case SNDRV_PCM_FORMAT_S18_3BE:
  385. fmt = CS42L51_DAC_DIF_RJ18;
  386. break;
  387. case SNDRV_PCM_FORMAT_S20_3LE:
  388. case SNDRV_PCM_FORMAT_S20_3BE:
  389. fmt = CS42L51_DAC_DIF_RJ20;
  390. break;
  391. case SNDRV_PCM_FORMAT_S24_LE:
  392. case SNDRV_PCM_FORMAT_S24_BE:
  393. fmt = CS42L51_DAC_DIF_RJ24;
  394. break;
  395. default:
  396. dev_err(codec->dev, "unknown format\n");
  397. return -EINVAL;
  398. }
  399. intf_ctl |= CS42L51_INTF_CTL_DAC_FORMAT(fmt);
  400. break;
  401. default:
  402. dev_err(codec->dev, "unknown format\n");
  403. return -EINVAL;
  404. }
  405. if (ratios[i].mclk)
  406. power_ctl |= CS42L51_MIC_POWER_CTL_MCLK_DIV2;
  407. ret = snd_soc_write(codec, CS42L51_INTF_CTL, intf_ctl);
  408. if (ret < 0)
  409. return ret;
  410. ret = snd_soc_write(codec, CS42L51_MIC_POWER_CTL, power_ctl);
  411. if (ret < 0)
  412. return ret;
  413. return 0;
  414. }
  415. static int cs42l51_dai_mute(struct snd_soc_dai *dai, int mute)
  416. {
  417. struct snd_soc_codec *codec = dai->codec;
  418. int reg;
  419. int mask = CS42L51_DAC_OUT_CTL_DACA_MUTE|CS42L51_DAC_OUT_CTL_DACB_MUTE;
  420. reg = snd_soc_read(codec, CS42L51_DAC_OUT_CTL);
  421. if (mute)
  422. reg |= mask;
  423. else
  424. reg &= ~mask;
  425. return snd_soc_write(codec, CS42L51_DAC_OUT_CTL, reg);
  426. }
  427. static struct snd_soc_dai_ops cs42l51_dai_ops = {
  428. .hw_params = cs42l51_hw_params,
  429. .set_sysclk = cs42l51_set_dai_sysclk,
  430. .set_fmt = cs42l51_set_dai_fmt,
  431. .digital_mute = cs42l51_dai_mute,
  432. };
  433. static struct snd_soc_dai_driver cs42l51_dai = {
  434. .name = "cs42l51-hifi",
  435. .playback = {
  436. .stream_name = "Playback",
  437. .channels_min = 1,
  438. .channels_max = 2,
  439. .rates = SNDRV_PCM_RATE_8000_96000,
  440. .formats = CS42L51_FORMATS,
  441. },
  442. .capture = {
  443. .stream_name = "Capture",
  444. .channels_min = 1,
  445. .channels_max = 2,
  446. .rates = SNDRV_PCM_RATE_8000_96000,
  447. .formats = CS42L51_FORMATS,
  448. },
  449. .ops = &cs42l51_dai_ops,
  450. };
  451. static int cs42l51_probe(struct snd_soc_codec *codec)
  452. {
  453. struct cs42l51_private *cs42l51 = snd_soc_codec_get_drvdata(codec);
  454. struct snd_soc_dapm_context *dapm = &codec->dapm;
  455. int ret, reg;
  456. ret = cs42l51_fill_cache(codec);
  457. if (ret < 0) {
  458. dev_err(codec->dev, "failed to fill register cache\n");
  459. return ret;
  460. }
  461. ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs42l51->control_type);
  462. if (ret < 0) {
  463. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  464. return ret;
  465. }
  466. /*
  467. * DAC configuration
  468. * - Use signal processor
  469. * - auto mute
  470. * - vol changes immediate
  471. * - no de-emphasize
  472. */
  473. reg = CS42L51_DAC_CTL_DATA_SEL(1)
  474. | CS42L51_DAC_CTL_AMUTE | CS42L51_DAC_CTL_DACSZ(0);
  475. ret = snd_soc_write(codec, CS42L51_DAC_CTL, reg);
  476. if (ret < 0)
  477. return ret;
  478. snd_soc_add_controls(codec, cs42l51_snd_controls,
  479. ARRAY_SIZE(cs42l51_snd_controls));
  480. snd_soc_dapm_new_controls(dapm, cs42l51_dapm_widgets,
  481. ARRAY_SIZE(cs42l51_dapm_widgets));
  482. snd_soc_dapm_add_routes(dapm, cs42l51_routes,
  483. ARRAY_SIZE(cs42l51_routes));
  484. return 0;
  485. }
  486. static struct snd_soc_codec_driver soc_codec_device_cs42l51 = {
  487. .probe = cs42l51_probe,
  488. .reg_cache_size = CS42L51_NUMREGS,
  489. .reg_word_size = sizeof(u8),
  490. };
  491. static int cs42l51_i2c_probe(struct i2c_client *i2c_client,
  492. const struct i2c_device_id *id)
  493. {
  494. struct cs42l51_private *cs42l51;
  495. int ret;
  496. /* Verify that we have a CS42L51 */
  497. ret = i2c_smbus_read_byte_data(i2c_client, CS42L51_CHIP_REV_ID);
  498. if (ret < 0) {
  499. dev_err(&i2c_client->dev, "failed to read I2C\n");
  500. goto error;
  501. }
  502. if ((ret != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_A)) &&
  503. (ret != CS42L51_MK_CHIP_REV(CS42L51_CHIP_ID, CS42L51_CHIP_REV_B))) {
  504. dev_err(&i2c_client->dev, "Invalid chip id\n");
  505. ret = -ENODEV;
  506. goto error;
  507. }
  508. dev_info(&i2c_client->dev, "found device cs42l51 rev %d\n",
  509. ret & 7);
  510. cs42l51 = kzalloc(sizeof(struct cs42l51_private), GFP_KERNEL);
  511. if (!cs42l51) {
  512. dev_err(&i2c_client->dev, "could not allocate codec\n");
  513. return -ENOMEM;
  514. }
  515. i2c_set_clientdata(i2c_client, cs42l51);
  516. cs42l51->control_type = SND_SOC_I2C;
  517. ret = snd_soc_register_codec(&i2c_client->dev,
  518. &soc_codec_device_cs42l51, &cs42l51_dai, 1);
  519. if (ret < 0)
  520. kfree(cs42l51);
  521. error:
  522. return ret;
  523. }
  524. static int cs42l51_i2c_remove(struct i2c_client *client)
  525. {
  526. struct cs42l51_private *cs42l51 = i2c_get_clientdata(client);
  527. snd_soc_unregister_codec(&client->dev);
  528. kfree(cs42l51);
  529. return 0;
  530. }
  531. static const struct i2c_device_id cs42l51_id[] = {
  532. {"cs42l51", 0},
  533. {}
  534. };
  535. MODULE_DEVICE_TABLE(i2c, cs42l51_id);
  536. static struct i2c_driver cs42l51_i2c_driver = {
  537. .driver = {
  538. .name = "cs42l51-codec",
  539. .owner = THIS_MODULE,
  540. },
  541. .id_table = cs42l51_id,
  542. .probe = cs42l51_i2c_probe,
  543. .remove = cs42l51_i2c_remove,
  544. };
  545. static int __init cs42l51_init(void)
  546. {
  547. int ret;
  548. ret = i2c_add_driver(&cs42l51_i2c_driver);
  549. if (ret != 0) {
  550. printk(KERN_ERR "%s: can't add i2c driver\n", __func__);
  551. return ret;
  552. }
  553. return 0;
  554. }
  555. module_init(cs42l51_init);
  556. static void __exit cs42l51_exit(void)
  557. {
  558. i2c_del_driver(&cs42l51_i2c_driver);
  559. }
  560. module_exit(cs42l51_exit);
  561. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
  562. MODULE_DESCRIPTION("Cirrus Logic CS42L51 ALSA SoC Codec Driver");
  563. MODULE_LICENSE("GPL");