mc13783.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  3. * Copyright 2009 Sascha Hauer, s.hauer@pengutronix.de
  4. * Copyright 2012 Philippe Retornaz, philippe.retornaz@epfl.ch
  5. *
  6. * Initial development of this code was funded by
  7. * Phytec Messtechnik GmbH, http://www.phytec.de
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. * MA 02110-1301, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/device.h>
  25. #include <linux/of.h>
  26. #include <linux/mfd/mc13xxx.h>
  27. #include <linux/slab.h>
  28. #include <sound/core.h>
  29. #include <sound/control.h>
  30. #include <sound/pcm.h>
  31. #include <sound/soc.h>
  32. #include <sound/initval.h>
  33. #include <sound/soc-dapm.h>
  34. #include <linux/regmap.h>
  35. #include "mc13783.h"
  36. #define AUDIO_RX0_ALSPEN (1 << 5)
  37. #define AUDIO_RX0_ALSPSEL (1 << 7)
  38. #define AUDIO_RX0_ADDCDC (1 << 21)
  39. #define AUDIO_RX0_ADDSTDC (1 << 22)
  40. #define AUDIO_RX0_ADDRXIN (1 << 23)
  41. #define AUDIO_RX1_PGARXEN (1 << 0);
  42. #define AUDIO_RX1_PGASTEN (1 << 5)
  43. #define AUDIO_RX1_ARXINEN (1 << 10)
  44. #define AUDIO_TX_AMC1REN (1 << 5)
  45. #define AUDIO_TX_AMC1LEN (1 << 7)
  46. #define AUDIO_TX_AMC2EN (1 << 9)
  47. #define AUDIO_TX_ATXINEN (1 << 11)
  48. #define AUDIO_TX_RXINREC (1 << 13)
  49. #define SSI_NETWORK_CDCTXRXSLOT(x) (((x) & 0x3) << 2)
  50. #define SSI_NETWORK_CDCTXSECSLOT(x) (((x) & 0x3) << 4)
  51. #define SSI_NETWORK_CDCRXSECSLOT(x) (((x) & 0x3) << 6)
  52. #define SSI_NETWORK_CDCRXSECGAIN(x) (((x) & 0x3) << 8)
  53. #define SSI_NETWORK_CDCSUMGAIN(x) (1 << 10)
  54. #define SSI_NETWORK_CDCFSDLY(x) (1 << 11)
  55. #define SSI_NETWORK_DAC_SLOTS_8 (1 << 12)
  56. #define SSI_NETWORK_DAC_SLOTS_4 (2 << 12)
  57. #define SSI_NETWORK_DAC_SLOTS_2 (3 << 12)
  58. #define SSI_NETWORK_DAC_SLOT_MASK (3 << 12)
  59. #define SSI_NETWORK_DAC_RXSLOT_0_1 (0 << 14)
  60. #define SSI_NETWORK_DAC_RXSLOT_2_3 (1 << 14)
  61. #define SSI_NETWORK_DAC_RXSLOT_4_5 (2 << 14)
  62. #define SSI_NETWORK_DAC_RXSLOT_6_7 (3 << 14)
  63. #define SSI_NETWORK_DAC_RXSLOT_MASK (3 << 14)
  64. #define SSI_NETWORK_STDCRXSECSLOT(x) (((x) & 0x3) << 16)
  65. #define SSI_NETWORK_STDCRXSECGAIN(x) (((x) & 0x3) << 18)
  66. #define SSI_NETWORK_STDCSUMGAIN (1 << 20)
  67. /*
  68. * MC13783_AUDIO_CODEC and MC13783_AUDIO_DAC mostly share the same
  69. * register layout
  70. */
  71. #define AUDIO_SSI_SEL (1 << 0)
  72. #define AUDIO_CLK_SEL (1 << 1)
  73. #define AUDIO_CSM (1 << 2)
  74. #define AUDIO_BCL_INV (1 << 3)
  75. #define AUDIO_CFS_INV (1 << 4)
  76. #define AUDIO_CFS(x) (((x) & 0x3) << 5)
  77. #define AUDIO_CLK(x) (((x) & 0x7) << 7)
  78. #define AUDIO_C_EN (1 << 11)
  79. #define AUDIO_C_CLK_EN (1 << 12)
  80. #define AUDIO_C_RESET (1 << 15)
  81. #define AUDIO_CODEC_CDCFS8K16K (1 << 10)
  82. #define AUDIO_DAC_CFS_DLY_B (1 << 10)
  83. struct mc13783_priv {
  84. struct mc13xxx *mc13xxx;
  85. struct regmap *regmap;
  86. enum mc13783_ssi_port adc_ssi_port;
  87. enum mc13783_ssi_port dac_ssi_port;
  88. };
  89. /* Mapping between sample rates and register value */
  90. static unsigned int mc13783_rates[] = {
  91. 8000, 11025, 12000, 16000,
  92. 22050, 24000, 32000, 44100,
  93. 48000, 64000, 96000
  94. };
  95. static int mc13783_pcm_hw_params_dac(struct snd_pcm_substream *substream,
  96. struct snd_pcm_hw_params *params,
  97. struct snd_soc_dai *dai)
  98. {
  99. struct snd_soc_codec *codec = dai->codec;
  100. unsigned int rate = params_rate(params);
  101. int i;
  102. for (i = 0; i < ARRAY_SIZE(mc13783_rates); i++) {
  103. if (rate == mc13783_rates[i]) {
  104. snd_soc_update_bits(codec, MC13783_AUDIO_DAC,
  105. 0xf << 17, i << 17);
  106. return 0;
  107. }
  108. }
  109. return -EINVAL;
  110. }
  111. static int mc13783_pcm_hw_params_codec(struct snd_pcm_substream *substream,
  112. struct snd_pcm_hw_params *params,
  113. struct snd_soc_dai *dai)
  114. {
  115. struct snd_soc_codec *codec = dai->codec;
  116. unsigned int rate = params_rate(params);
  117. unsigned int val;
  118. switch (rate) {
  119. case 8000:
  120. val = 0;
  121. break;
  122. case 16000:
  123. val = AUDIO_CODEC_CDCFS8K16K;
  124. break;
  125. default:
  126. return -EINVAL;
  127. }
  128. snd_soc_update_bits(codec, MC13783_AUDIO_CODEC, AUDIO_CODEC_CDCFS8K16K,
  129. val);
  130. return 0;
  131. }
  132. static int mc13783_pcm_hw_params_sync(struct snd_pcm_substream *substream,
  133. struct snd_pcm_hw_params *params,
  134. struct snd_soc_dai *dai)
  135. {
  136. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  137. return mc13783_pcm_hw_params_dac(substream, params, dai);
  138. else
  139. return mc13783_pcm_hw_params_codec(substream, params, dai);
  140. }
  141. static int mc13783_set_fmt(struct snd_soc_dai *dai, unsigned int fmt,
  142. unsigned int reg)
  143. {
  144. struct snd_soc_codec *codec = dai->codec;
  145. unsigned int val = 0;
  146. unsigned int mask = AUDIO_CFS(3) | AUDIO_BCL_INV | AUDIO_CFS_INV |
  147. AUDIO_CSM | AUDIO_C_CLK_EN | AUDIO_C_RESET;
  148. /* DAI mode */
  149. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  150. case SND_SOC_DAIFMT_I2S:
  151. val |= AUDIO_CFS(2);
  152. break;
  153. case SND_SOC_DAIFMT_DSP_A:
  154. val |= AUDIO_CFS(1);
  155. break;
  156. default:
  157. return -EINVAL;
  158. }
  159. /* DAI clock inversion */
  160. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  161. case SND_SOC_DAIFMT_NB_NF:
  162. val |= AUDIO_BCL_INV;
  163. break;
  164. case SND_SOC_DAIFMT_NB_IF:
  165. val |= AUDIO_BCL_INV | AUDIO_CFS_INV;
  166. break;
  167. case SND_SOC_DAIFMT_IB_NF:
  168. break;
  169. case SND_SOC_DAIFMT_IB_IF:
  170. val |= AUDIO_CFS_INV;
  171. break;
  172. }
  173. /* DAI clock master masks */
  174. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  175. case SND_SOC_DAIFMT_CBM_CFM:
  176. val |= AUDIO_C_CLK_EN;
  177. break;
  178. case SND_SOC_DAIFMT_CBS_CFS:
  179. val |= AUDIO_CSM;
  180. break;
  181. case SND_SOC_DAIFMT_CBM_CFS:
  182. case SND_SOC_DAIFMT_CBS_CFM:
  183. return -EINVAL;
  184. }
  185. val |= AUDIO_C_RESET;
  186. snd_soc_update_bits(codec, reg, mask, val);
  187. return 0;
  188. }
  189. static int mc13783_set_fmt_async(struct snd_soc_dai *dai, unsigned int fmt)
  190. {
  191. if (dai->id == MC13783_ID_STEREO_DAC)
  192. return mc13783_set_fmt(dai, fmt, MC13783_AUDIO_DAC);
  193. else
  194. return mc13783_set_fmt(dai, fmt, MC13783_AUDIO_CODEC);
  195. }
  196. static int mc13783_set_fmt_sync(struct snd_soc_dai *dai, unsigned int fmt)
  197. {
  198. int ret;
  199. ret = mc13783_set_fmt(dai, fmt, MC13783_AUDIO_DAC);
  200. if (ret)
  201. return ret;
  202. /*
  203. * In synchronous mode force the voice codec into slave mode
  204. * so that the clock / framesync from the stereo DAC is used
  205. */
  206. fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  207. fmt |= SND_SOC_DAIFMT_CBS_CFS;
  208. ret = mc13783_set_fmt(dai, fmt, MC13783_AUDIO_CODEC);
  209. return ret;
  210. }
  211. static int mc13783_sysclk[] = {
  212. 13000000,
  213. 15360000,
  214. 16800000,
  215. -1,
  216. 26000000,
  217. -1, /* 12000000, invalid for voice codec */
  218. -1, /* 3686400, invalid for voice codec */
  219. 33600000,
  220. };
  221. static int mc13783_set_sysclk(struct snd_soc_dai *dai,
  222. int clk_id, unsigned int freq, int dir,
  223. unsigned int reg)
  224. {
  225. struct snd_soc_codec *codec = dai->codec;
  226. int clk;
  227. unsigned int val = 0;
  228. unsigned int mask = AUDIO_CLK(0x7) | AUDIO_CLK_SEL;
  229. for (clk = 0; clk < ARRAY_SIZE(mc13783_sysclk); clk++) {
  230. if (mc13783_sysclk[clk] < 0)
  231. continue;
  232. if (mc13783_sysclk[clk] == freq)
  233. break;
  234. }
  235. if (clk == ARRAY_SIZE(mc13783_sysclk))
  236. return -EINVAL;
  237. if (clk_id == MC13783_CLK_CLIB)
  238. val |= AUDIO_CLK_SEL;
  239. val |= AUDIO_CLK(clk);
  240. snd_soc_update_bits(codec, reg, mask, val);
  241. return 0;
  242. }
  243. static int mc13783_set_sysclk_dac(struct snd_soc_dai *dai,
  244. int clk_id, unsigned int freq, int dir)
  245. {
  246. return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_DAC);
  247. }
  248. static int mc13783_set_sysclk_codec(struct snd_soc_dai *dai,
  249. int clk_id, unsigned int freq, int dir)
  250. {
  251. return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_CODEC);
  252. }
  253. static int mc13783_set_sysclk_sync(struct snd_soc_dai *dai,
  254. int clk_id, unsigned int freq, int dir)
  255. {
  256. int ret;
  257. ret = mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_DAC);
  258. if (ret)
  259. return ret;
  260. return mc13783_set_sysclk(dai, clk_id, freq, dir, MC13783_AUDIO_CODEC);
  261. }
  262. static int mc13783_set_tdm_slot_dac(struct snd_soc_dai *dai,
  263. unsigned int tx_mask, unsigned int rx_mask, int slots,
  264. int slot_width)
  265. {
  266. struct snd_soc_codec *codec = dai->codec;
  267. unsigned int val = 0;
  268. unsigned int mask = SSI_NETWORK_DAC_SLOT_MASK |
  269. SSI_NETWORK_DAC_RXSLOT_MASK;
  270. switch (slots) {
  271. case 2:
  272. val |= SSI_NETWORK_DAC_SLOTS_2;
  273. break;
  274. case 4:
  275. val |= SSI_NETWORK_DAC_SLOTS_4;
  276. break;
  277. case 8:
  278. val |= SSI_NETWORK_DAC_SLOTS_8;
  279. break;
  280. default:
  281. return -EINVAL;
  282. }
  283. switch (rx_mask) {
  284. case 0x03:
  285. val |= SSI_NETWORK_DAC_RXSLOT_0_1;
  286. break;
  287. case 0x0c:
  288. val |= SSI_NETWORK_DAC_RXSLOT_2_3;
  289. break;
  290. case 0x30:
  291. val |= SSI_NETWORK_DAC_RXSLOT_4_5;
  292. break;
  293. case 0xc0:
  294. val |= SSI_NETWORK_DAC_RXSLOT_6_7;
  295. break;
  296. default:
  297. return -EINVAL;
  298. }
  299. snd_soc_update_bits(codec, MC13783_SSI_NETWORK, mask, val);
  300. return 0;
  301. }
  302. static int mc13783_set_tdm_slot_codec(struct snd_soc_dai *dai,
  303. unsigned int tx_mask, unsigned int rx_mask, int slots,
  304. int slot_width)
  305. {
  306. struct snd_soc_codec *codec = dai->codec;
  307. unsigned int val = 0;
  308. unsigned int mask = 0x3f;
  309. if (slots != 4)
  310. return -EINVAL;
  311. if (tx_mask != 0x3)
  312. return -EINVAL;
  313. val |= (0x00 << 2); /* primary timeslot RX/TX(?) is 0 */
  314. val |= (0x01 << 4); /* secondary timeslot TX is 1 */
  315. snd_soc_update_bits(codec, MC13783_SSI_NETWORK, mask, val);
  316. return 0;
  317. }
  318. static int mc13783_set_tdm_slot_sync(struct snd_soc_dai *dai,
  319. unsigned int tx_mask, unsigned int rx_mask, int slots,
  320. int slot_width)
  321. {
  322. int ret;
  323. ret = mc13783_set_tdm_slot_dac(dai, tx_mask, rx_mask, slots,
  324. slot_width);
  325. if (ret)
  326. return ret;
  327. ret = mc13783_set_tdm_slot_codec(dai, tx_mask, rx_mask, slots,
  328. slot_width);
  329. return ret;
  330. }
  331. static const struct snd_kcontrol_new mc1l_amp_ctl =
  332. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 7, 1, 0);
  333. static const struct snd_kcontrol_new mc1r_amp_ctl =
  334. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 5, 1, 0);
  335. static const struct snd_kcontrol_new mc2_amp_ctl =
  336. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 9, 1, 0);
  337. static const struct snd_kcontrol_new atx_amp_ctl =
  338. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_TX, 11, 1, 0);
  339. /* Virtual mux. The chip does the input selection automatically
  340. * as soon as we enable one input. */
  341. static const char * const adcl_enum_text[] = {
  342. "MC1L", "RXINL",
  343. };
  344. static SOC_ENUM_SINGLE_VIRT_DECL(adcl_enum, adcl_enum_text);
  345. static const struct snd_kcontrol_new left_input_mux =
  346. SOC_DAPM_ENUM("Route", adcl_enum);
  347. static const char * const adcr_enum_text[] = {
  348. "MC1R", "MC2", "RXINR", "TXIN",
  349. };
  350. static SOC_ENUM_SINGLE_VIRT_DECL(adcr_enum, adcr_enum_text);
  351. static const struct snd_kcontrol_new right_input_mux =
  352. SOC_DAPM_ENUM("Route", adcr_enum);
  353. static const struct snd_kcontrol_new samp_ctl =
  354. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 3, 1, 0);
  355. static const char * const speaker_amp_source_text[] = {
  356. "CODEC", "Right"
  357. };
  358. static SOC_ENUM_SINGLE_DECL(speaker_amp_source, MC13783_AUDIO_RX0, 4,
  359. speaker_amp_source_text);
  360. static const struct snd_kcontrol_new speaker_amp_source_mux =
  361. SOC_DAPM_ENUM("Speaker Amp Source MUX", speaker_amp_source);
  362. static const char * const headset_amp_source_text[] = {
  363. "CODEC", "Mixer"
  364. };
  365. static SOC_ENUM_SINGLE_DECL(headset_amp_source, MC13783_AUDIO_RX0, 11,
  366. headset_amp_source_text);
  367. static const struct snd_kcontrol_new headset_amp_source_mux =
  368. SOC_DAPM_ENUM("Headset Amp Source MUX", headset_amp_source);
  369. static const struct snd_kcontrol_new cdcout_ctl =
  370. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 18, 1, 0);
  371. static const struct snd_kcontrol_new adc_bypass_ctl =
  372. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_CODEC, 16, 1, 0);
  373. static const struct snd_kcontrol_new lamp_ctl =
  374. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 5, 1, 0);
  375. static const struct snd_kcontrol_new hlamp_ctl =
  376. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 10, 1, 0);
  377. static const struct snd_kcontrol_new hramp_ctl =
  378. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 9, 1, 0);
  379. static const struct snd_kcontrol_new llamp_ctl =
  380. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 16, 1, 0);
  381. static const struct snd_kcontrol_new lramp_ctl =
  382. SOC_DAPM_SINGLE("Switch", MC13783_AUDIO_RX0, 15, 1, 0);
  383. static const struct snd_soc_dapm_widget mc13783_dapm_widgets[] = {
  384. /* Input */
  385. SND_SOC_DAPM_INPUT("MC1LIN"),
  386. SND_SOC_DAPM_INPUT("MC1RIN"),
  387. SND_SOC_DAPM_INPUT("MC2IN"),
  388. SND_SOC_DAPM_INPUT("RXINR"),
  389. SND_SOC_DAPM_INPUT("RXINL"),
  390. SND_SOC_DAPM_INPUT("TXIN"),
  391. SND_SOC_DAPM_SUPPLY("MC1 Bias", MC13783_AUDIO_TX, 0, 0, NULL, 0),
  392. SND_SOC_DAPM_SUPPLY("MC2 Bias", MC13783_AUDIO_TX, 1, 0, NULL, 0),
  393. SND_SOC_DAPM_SWITCH("MC1L Amp", MC13783_AUDIO_TX, 7, 0, &mc1l_amp_ctl),
  394. SND_SOC_DAPM_SWITCH("MC1R Amp", MC13783_AUDIO_TX, 5, 0, &mc1r_amp_ctl),
  395. SND_SOC_DAPM_SWITCH("MC2 Amp", MC13783_AUDIO_TX, 9, 0, &mc2_amp_ctl),
  396. SND_SOC_DAPM_SWITCH("TXIN Amp", MC13783_AUDIO_TX, 11, 0, &atx_amp_ctl),
  397. SND_SOC_DAPM_MUX("PGA Left Input Mux", SND_SOC_NOPM, 0, 0,
  398. &left_input_mux),
  399. SND_SOC_DAPM_MUX("PGA Right Input Mux", SND_SOC_NOPM, 0, 0,
  400. &right_input_mux),
  401. SND_SOC_DAPM_MUX("Speaker Amp Source MUX", SND_SOC_NOPM, 0, 0,
  402. &speaker_amp_source_mux),
  403. SND_SOC_DAPM_MUX("Headset Amp Source MUX", SND_SOC_NOPM, 0, 0,
  404. &headset_amp_source_mux),
  405. SND_SOC_DAPM_PGA("PGA Left Input", SND_SOC_NOPM, 0, 0, NULL, 0),
  406. SND_SOC_DAPM_PGA("PGA Right Input", SND_SOC_NOPM, 0, 0, NULL, 0),
  407. SND_SOC_DAPM_ADC("ADC", "Capture", MC13783_AUDIO_CODEC, 11, 0),
  408. SND_SOC_DAPM_SUPPLY("ADC_Reset", MC13783_AUDIO_CODEC, 15, 0, NULL, 0),
  409. SND_SOC_DAPM_PGA("Voice CODEC PGA", MC13783_AUDIO_RX1, 0, 0, NULL, 0),
  410. SND_SOC_DAPM_SWITCH("Voice CODEC Bypass", MC13783_AUDIO_CODEC, 16, 0,
  411. &adc_bypass_ctl),
  412. /* Output */
  413. SND_SOC_DAPM_SUPPLY("DAC_E", MC13783_AUDIO_DAC, 11, 0, NULL, 0),
  414. SND_SOC_DAPM_SUPPLY("DAC_Reset", MC13783_AUDIO_DAC, 15, 0, NULL, 0),
  415. SND_SOC_DAPM_OUTPUT("RXOUTL"),
  416. SND_SOC_DAPM_OUTPUT("RXOUTR"),
  417. SND_SOC_DAPM_OUTPUT("HSL"),
  418. SND_SOC_DAPM_OUTPUT("HSR"),
  419. SND_SOC_DAPM_OUTPUT("LSPL"),
  420. SND_SOC_DAPM_OUTPUT("LSP"),
  421. SND_SOC_DAPM_OUTPUT("SP"),
  422. SND_SOC_DAPM_OUTPUT("CDCOUT"),
  423. SND_SOC_DAPM_SWITCH("CDCOUT Switch", MC13783_AUDIO_RX0, 18, 0,
  424. &cdcout_ctl),
  425. SND_SOC_DAPM_SWITCH("Speaker Amp Switch", MC13783_AUDIO_RX0, 3, 0,
  426. &samp_ctl),
  427. SND_SOC_DAPM_SWITCH("Loudspeaker Amp", SND_SOC_NOPM, 0, 0, &lamp_ctl),
  428. SND_SOC_DAPM_SWITCH("Headset Amp Left", MC13783_AUDIO_RX0, 10, 0,
  429. &hlamp_ctl),
  430. SND_SOC_DAPM_SWITCH("Headset Amp Right", MC13783_AUDIO_RX0, 9, 0,
  431. &hramp_ctl),
  432. SND_SOC_DAPM_SWITCH("Line out Amp Left", MC13783_AUDIO_RX0, 16, 0,
  433. &llamp_ctl),
  434. SND_SOC_DAPM_SWITCH("Line out Amp Right", MC13783_AUDIO_RX0, 15, 0,
  435. &lramp_ctl),
  436. SND_SOC_DAPM_DAC("DAC", "Playback", MC13783_AUDIO_RX0, 22, 0),
  437. SND_SOC_DAPM_PGA("DAC PGA", MC13783_AUDIO_RX1, 5, 0, NULL, 0),
  438. };
  439. static struct snd_soc_dapm_route mc13783_routes[] = {
  440. /* Input */
  441. { "MC1L Amp", NULL, "MC1LIN"},
  442. { "MC1R Amp", NULL, "MC1RIN" },
  443. { "MC2 Amp", NULL, "MC2IN" },
  444. { "TXIN Amp", NULL, "TXIN"},
  445. { "PGA Left Input Mux", "MC1L", "MC1L Amp" },
  446. { "PGA Left Input Mux", "RXINL", "RXINL"},
  447. { "PGA Right Input Mux", "MC1R", "MC1R Amp" },
  448. { "PGA Right Input Mux", "MC2", "MC2 Amp"},
  449. { "PGA Right Input Mux", "TXIN", "TXIN Amp"},
  450. { "PGA Right Input Mux", "RXINR", "RXINR"},
  451. { "PGA Left Input", NULL, "PGA Left Input Mux"},
  452. { "PGA Right Input", NULL, "PGA Right Input Mux"},
  453. { "ADC", NULL, "PGA Left Input"},
  454. { "ADC", NULL, "PGA Right Input"},
  455. { "ADC", NULL, "ADC_Reset"},
  456. { "Voice CODEC PGA", "Voice CODEC Bypass", "ADC" },
  457. { "Speaker Amp Source MUX", "CODEC", "Voice CODEC PGA"},
  458. { "Speaker Amp Source MUX", "Right", "DAC PGA"},
  459. { "Headset Amp Source MUX", "CODEC", "Voice CODEC PGA"},
  460. { "Headset Amp Source MUX", "Mixer", "DAC PGA"},
  461. /* Output */
  462. { "HSL", NULL, "Headset Amp Left" },
  463. { "HSR", NULL, "Headset Amp Right"},
  464. { "RXOUTL", NULL, "Line out Amp Left"},
  465. { "RXOUTR", NULL, "Line out Amp Right"},
  466. { "SP", "Speaker Amp Switch", "Speaker Amp Source MUX"},
  467. { "LSP", "Loudspeaker Amp", "Speaker Amp Source MUX"},
  468. { "HSL", "Headset Amp Left", "Headset Amp Source MUX"},
  469. { "HSR", "Headset Amp Right", "Headset Amp Source MUX"},
  470. { "Line out Amp Left", NULL, "DAC PGA"},
  471. { "Line out Amp Right", NULL, "DAC PGA"},
  472. { "DAC PGA", NULL, "DAC"},
  473. { "DAC", NULL, "DAC_E"},
  474. { "CDCOUT", "CDCOUT Switch", "Voice CODEC PGA"},
  475. };
  476. static const char * const mc13783_3d_mixer[] = {"Stereo", "Phase Mix",
  477. "Mono", "Mono Mix"};
  478. static SOC_ENUM_SINGLE_DECL(mc13783_enum_3d_mixer,
  479. MC13783_AUDIO_RX1, 16,
  480. mc13783_3d_mixer);
  481. static struct snd_kcontrol_new mc13783_control_list[] = {
  482. SOC_SINGLE("Loudspeaker enable", MC13783_AUDIO_RX0, 5, 1, 0),
  483. SOC_SINGLE("PCM Playback Volume", MC13783_AUDIO_RX1, 6, 15, 0),
  484. SOC_SINGLE("PCM Playback Switch", MC13783_AUDIO_RX1, 5, 1, 0),
  485. SOC_DOUBLE("PCM Capture Volume", MC13783_AUDIO_TX, 19, 14, 31, 0),
  486. SOC_ENUM("3D Control", mc13783_enum_3d_mixer),
  487. SOC_SINGLE("CDCOUT Switch", MC13783_AUDIO_RX0, 18, 1, 0),
  488. SOC_SINGLE("Earpiece Amp Switch", MC13783_AUDIO_RX0, 3, 1, 0),
  489. SOC_DOUBLE("Headset Amp Switch", MC13783_AUDIO_RX0, 10, 9, 1, 0),
  490. SOC_DOUBLE("Line out Amp Switch", MC13783_AUDIO_RX0, 16, 15, 1, 0),
  491. SOC_SINGLE("PCM Capture Mixin Switch", MC13783_AUDIO_RX0, 22, 1, 0),
  492. SOC_SINGLE("Line in Capture Mixin Switch", MC13783_AUDIO_RX0, 23, 1, 0),
  493. SOC_SINGLE("CODEC Capture Volume", MC13783_AUDIO_RX1, 1, 15, 0),
  494. SOC_SINGLE("CODEC Capture Mixin Switch", MC13783_AUDIO_RX0, 21, 1, 0),
  495. SOC_SINGLE("Line in Capture Volume", MC13783_AUDIO_RX1, 12, 15, 0),
  496. SOC_SINGLE("Line in Capture Switch", MC13783_AUDIO_RX1, 10, 1, 0),
  497. SOC_SINGLE("MC1 Capture Bias Switch", MC13783_AUDIO_TX, 0, 1, 0),
  498. SOC_SINGLE("MC2 Capture Bias Switch", MC13783_AUDIO_TX, 1, 1, 0),
  499. };
  500. static int mc13783_probe(struct snd_soc_codec *codec)
  501. {
  502. struct mc13783_priv *priv = snd_soc_codec_get_drvdata(codec);
  503. /* these are the reset values */
  504. mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_RX0, 0x25893);
  505. mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_RX1, 0x00d35A);
  506. mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_TX, 0x420000);
  507. mc13xxx_reg_write(priv->mc13xxx, MC13783_SSI_NETWORK, 0x013060);
  508. mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_CODEC, 0x180027);
  509. mc13xxx_reg_write(priv->mc13xxx, MC13783_AUDIO_DAC, 0x0e0004);
  510. if (priv->adc_ssi_port == MC13783_SSI1_PORT)
  511. mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_CODEC,
  512. AUDIO_SSI_SEL, 0);
  513. else
  514. mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_CODEC,
  515. AUDIO_SSI_SEL, AUDIO_SSI_SEL);
  516. if (priv->dac_ssi_port == MC13783_SSI1_PORT)
  517. mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_DAC,
  518. AUDIO_SSI_SEL, 0);
  519. else
  520. mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_DAC,
  521. AUDIO_SSI_SEL, AUDIO_SSI_SEL);
  522. return 0;
  523. }
  524. static int mc13783_remove(struct snd_soc_codec *codec)
  525. {
  526. struct mc13783_priv *priv = snd_soc_codec_get_drvdata(codec);
  527. /* Make sure VAUDIOON is off */
  528. mc13xxx_reg_rmw(priv->mc13xxx, MC13783_AUDIO_RX0, 0x3, 0);
  529. return 0;
  530. }
  531. #define MC13783_RATES_RECORD (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000)
  532. #define MC13783_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  533. SNDRV_PCM_FMTBIT_S24_LE)
  534. static const struct snd_soc_dai_ops mc13783_ops_dac = {
  535. .hw_params = mc13783_pcm_hw_params_dac,
  536. .set_fmt = mc13783_set_fmt_async,
  537. .set_sysclk = mc13783_set_sysclk_dac,
  538. .set_tdm_slot = mc13783_set_tdm_slot_dac,
  539. };
  540. static const struct snd_soc_dai_ops mc13783_ops_codec = {
  541. .hw_params = mc13783_pcm_hw_params_codec,
  542. .set_fmt = mc13783_set_fmt_async,
  543. .set_sysclk = mc13783_set_sysclk_codec,
  544. .set_tdm_slot = mc13783_set_tdm_slot_codec,
  545. };
  546. /*
  547. * The mc13783 has two SSI ports, both of them can be routed either
  548. * to the voice codec or the stereo DAC. When two different SSI ports
  549. * are used for the voice codec and the stereo DAC we can do different
  550. * formats and sysclock settings for playback and capture
  551. * (mc13783-hifi-playback and mc13783-hifi-capture). Using the same port
  552. * forces us to use symmetric rates (mc13783-hifi).
  553. */
  554. static struct snd_soc_dai_driver mc13783_dai_async[] = {
  555. {
  556. .name = "mc13783-hifi-playback",
  557. .id = MC13783_ID_STEREO_DAC,
  558. .playback = {
  559. .stream_name = "Playback",
  560. .channels_min = 2,
  561. .channels_max = 2,
  562. .rates = SNDRV_PCM_RATE_8000_96000,
  563. .formats = MC13783_FORMATS,
  564. },
  565. .ops = &mc13783_ops_dac,
  566. }, {
  567. .name = "mc13783-hifi-capture",
  568. .id = MC13783_ID_STEREO_CODEC,
  569. .capture = {
  570. .stream_name = "Capture",
  571. .channels_min = 2,
  572. .channels_max = 2,
  573. .rates = MC13783_RATES_RECORD,
  574. .formats = MC13783_FORMATS,
  575. },
  576. .ops = &mc13783_ops_codec,
  577. },
  578. };
  579. static const struct snd_soc_dai_ops mc13783_ops_sync = {
  580. .hw_params = mc13783_pcm_hw_params_sync,
  581. .set_fmt = mc13783_set_fmt_sync,
  582. .set_sysclk = mc13783_set_sysclk_sync,
  583. .set_tdm_slot = mc13783_set_tdm_slot_sync,
  584. };
  585. static struct snd_soc_dai_driver mc13783_dai_sync[] = {
  586. {
  587. .name = "mc13783-hifi",
  588. .id = MC13783_ID_SYNC,
  589. .playback = {
  590. .stream_name = "Playback",
  591. .channels_min = 2,
  592. .channels_max = 2,
  593. .rates = SNDRV_PCM_RATE_8000_96000,
  594. .formats = MC13783_FORMATS,
  595. },
  596. .capture = {
  597. .stream_name = "Capture",
  598. .channels_min = 2,
  599. .channels_max = 2,
  600. .rates = MC13783_RATES_RECORD,
  601. .formats = MC13783_FORMATS,
  602. },
  603. .ops = &mc13783_ops_sync,
  604. .symmetric_rates = 1,
  605. }
  606. };
  607. static struct regmap *mc13783_get_regmap(struct device *dev)
  608. {
  609. return dev_get_regmap(dev->parent, NULL);
  610. }
  611. static struct snd_soc_codec_driver soc_codec_dev_mc13783 = {
  612. .probe = mc13783_probe,
  613. .remove = mc13783_remove,
  614. .get_regmap = mc13783_get_regmap,
  615. .component_driver = {
  616. .controls = mc13783_control_list,
  617. .num_controls = ARRAY_SIZE(mc13783_control_list),
  618. .dapm_widgets = mc13783_dapm_widgets,
  619. .num_dapm_widgets = ARRAY_SIZE(mc13783_dapm_widgets),
  620. .dapm_routes = mc13783_routes,
  621. .num_dapm_routes = ARRAY_SIZE(mc13783_routes),
  622. },
  623. };
  624. static int __init mc13783_codec_probe(struct platform_device *pdev)
  625. {
  626. struct mc13783_priv *priv;
  627. struct mc13xxx_codec_platform_data *pdata = pdev->dev.platform_data;
  628. struct device_node *np;
  629. int ret;
  630. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  631. if (!priv)
  632. return -ENOMEM;
  633. if (pdata) {
  634. priv->adc_ssi_port = pdata->adc_ssi_port;
  635. priv->dac_ssi_port = pdata->dac_ssi_port;
  636. } else {
  637. np = of_get_child_by_name(pdev->dev.parent->of_node, "codec");
  638. if (!np)
  639. return -ENOSYS;
  640. ret = of_property_read_u32(np, "adc-port", &priv->adc_ssi_port);
  641. if (ret) {
  642. of_node_put(np);
  643. return ret;
  644. }
  645. ret = of_property_read_u32(np, "dac-port", &priv->dac_ssi_port);
  646. if (ret) {
  647. of_node_put(np);
  648. return ret;
  649. }
  650. of_node_put(np);
  651. }
  652. dev_set_drvdata(&pdev->dev, priv);
  653. priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
  654. if (priv->adc_ssi_port == priv->dac_ssi_port)
  655. ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_mc13783,
  656. mc13783_dai_sync, ARRAY_SIZE(mc13783_dai_sync));
  657. else
  658. ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_mc13783,
  659. mc13783_dai_async, ARRAY_SIZE(mc13783_dai_async));
  660. return ret;
  661. }
  662. static int mc13783_codec_remove(struct platform_device *pdev)
  663. {
  664. snd_soc_unregister_codec(&pdev->dev);
  665. return 0;
  666. }
  667. static struct platform_driver mc13783_codec_driver = {
  668. .driver = {
  669. .name = "mc13783-codec",
  670. },
  671. .remove = mc13783_codec_remove,
  672. };
  673. module_platform_driver_probe(mc13783_codec_driver, mc13783_codec_probe);
  674. MODULE_DESCRIPTION("ASoC MC13783 driver");
  675. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  676. MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
  677. MODULE_LICENSE("GPL");