sn95031.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /*
  2. * sn95031.c - TI sn95031 Codec driver
  3. *
  4. * Copyright (C) 2010 Intel Corp
  5. * Author: Vinod Koul <vinod.koul@intel.com>
  6. * Author: Harsha Priya <priya.harsha@intel.com>
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  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 as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. *
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/platform_device.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/module.h>
  31. #include <asm/intel_scu_ipc.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #include <sound/soc-dapm.h>
  36. #include <sound/initval.h>
  37. #include <sound/tlv.h>
  38. #include <sound/jack.h>
  39. #include "sn95031.h"
  40. #define SN95031_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100)
  41. #define SN95031_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  42. /* adc helper functions */
  43. /* enables mic bias voltage */
  44. static void sn95031_enable_mic_bias(struct snd_soc_codec *codec)
  45. {
  46. snd_soc_write(codec, SN95031_VAUD, BIT(2)|BIT(1)|BIT(0));
  47. snd_soc_update_bits(codec, SN95031_MICBIAS, BIT(2), BIT(2));
  48. }
  49. /* Enable/Disable the ADC depending on the argument */
  50. static void configure_adc(struct snd_soc_codec *sn95031_codec, int val)
  51. {
  52. int value = snd_soc_read(sn95031_codec, SN95031_ADC1CNTL1);
  53. if (val) {
  54. /* Enable and start the ADC */
  55. value |= (SN95031_ADC_ENBL | SN95031_ADC_START);
  56. value &= (~SN95031_ADC_NO_LOOP);
  57. } else {
  58. /* Just stop the ADC */
  59. value &= (~SN95031_ADC_START);
  60. }
  61. snd_soc_write(sn95031_codec, SN95031_ADC1CNTL1, value);
  62. }
  63. /*
  64. * finds an empty channel for conversion
  65. * If the ADC is not enabled then start using 0th channel
  66. * itself. Otherwise find an empty channel by looking for a
  67. * channel in which the stopbit is set to 1. returns the index
  68. * of the first free channel if succeeds or an error code.
  69. *
  70. * Context: can sleep
  71. *
  72. */
  73. static int find_free_channel(struct snd_soc_codec *sn95031_codec)
  74. {
  75. int i, value;
  76. /* check whether ADC is enabled */
  77. value = snd_soc_read(sn95031_codec, SN95031_ADC1CNTL1);
  78. if ((value & SN95031_ADC_ENBL) == 0)
  79. return 0;
  80. /* ADC is already enabled; Looking for an empty channel */
  81. for (i = 0; i < SN95031_ADC_CHANLS_MAX; i++) {
  82. value = snd_soc_read(sn95031_codec,
  83. SN95031_ADC_CHNL_START_ADDR + i);
  84. if (value & SN95031_STOPBIT_MASK)
  85. break;
  86. }
  87. return (i == SN95031_ADC_CHANLS_MAX) ? (-EINVAL) : i;
  88. }
  89. /* Initialize the ADC for reading micbias values. Can sleep. */
  90. static int sn95031_initialize_adc(struct snd_soc_codec *sn95031_codec)
  91. {
  92. int base_addr, chnl_addr;
  93. int value;
  94. int channel_index;
  95. /* Index of the first channel in which the stop bit is set */
  96. channel_index = find_free_channel(sn95031_codec);
  97. if (channel_index < 0) {
  98. pr_err("No free ADC channels");
  99. return channel_index;
  100. }
  101. base_addr = SN95031_ADC_CHNL_START_ADDR + channel_index;
  102. if (!(channel_index == 0 || channel_index == SN95031_ADC_LOOP_MAX)) {
  103. /* Reset stop bit for channels other than 0 and 12 */
  104. value = snd_soc_read(sn95031_codec, base_addr);
  105. /* Set the stop bit to zero */
  106. snd_soc_write(sn95031_codec, base_addr, value & 0xEF);
  107. /* Index of the first free channel */
  108. base_addr++;
  109. channel_index++;
  110. }
  111. /* Since this is the last channel, set the stop bit
  112. to 1 by ORing the DIE_SENSOR_CODE with 0x10 */
  113. snd_soc_write(sn95031_codec, base_addr,
  114. SN95031_AUDIO_DETECT_CODE | 0x10);
  115. chnl_addr = SN95031_ADC_DATA_START_ADDR + 2 * channel_index;
  116. pr_debug("mid_initialize : %x", chnl_addr);
  117. configure_adc(sn95031_codec, 1);
  118. return chnl_addr;
  119. }
  120. /* reads the ADC registers and gets the mic bias value in mV. */
  121. static unsigned int sn95031_get_mic_bias(struct snd_soc_codec *codec)
  122. {
  123. u16 adc_adr = sn95031_initialize_adc(codec);
  124. u16 adc_val1, adc_val2;
  125. unsigned int mic_bias;
  126. sn95031_enable_mic_bias(codec);
  127. /* Enable the sound card for conversion before reading */
  128. snd_soc_write(codec, SN95031_ADC1CNTL3, 0x05);
  129. /* Re-toggle the RRDATARD bit */
  130. snd_soc_write(codec, SN95031_ADC1CNTL3, 0x04);
  131. /* Read the higher bits of data */
  132. msleep(1000);
  133. adc_val1 = snd_soc_read(codec, adc_adr);
  134. adc_adr++;
  135. adc_val2 = snd_soc_read(codec, adc_adr);
  136. /* Adding lower two bits to the higher bits */
  137. mic_bias = (adc_val1 << 2) + (adc_val2 & 3);
  138. mic_bias = (mic_bias * SN95031_ADC_ONE_LSB_MULTIPLIER) / 1000;
  139. pr_debug("mic bias = %dmV\n", mic_bias);
  140. return mic_bias;
  141. }
  142. /*end - adc helper functions */
  143. static int sn95031_read(void *ctx, unsigned int reg, unsigned int *val)
  144. {
  145. u8 value = 0;
  146. int ret;
  147. ret = intel_scu_ipc_ioread8(reg, &value);
  148. if (ret == 0)
  149. *val = value;
  150. return ret;
  151. }
  152. static int sn95031_write(void *ctx, unsigned int reg, unsigned int value)
  153. {
  154. return intel_scu_ipc_iowrite8(reg, value);
  155. }
  156. static const struct regmap_config sn95031_regmap = {
  157. .reg_read = sn95031_read,
  158. .reg_write = sn95031_write,
  159. };
  160. static int sn95031_set_vaud_bias(struct snd_soc_codec *codec,
  161. enum snd_soc_bias_level level)
  162. {
  163. switch (level) {
  164. case SND_SOC_BIAS_ON:
  165. break;
  166. case SND_SOC_BIAS_PREPARE:
  167. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
  168. pr_debug("vaud_bias powering up pll\n");
  169. /* power up the pll */
  170. snd_soc_write(codec, SN95031_AUDPLLCTRL, BIT(5));
  171. /* enable pcm 2 */
  172. snd_soc_update_bits(codec, SN95031_PCM2C2,
  173. BIT(0), BIT(0));
  174. }
  175. break;
  176. case SND_SOC_BIAS_STANDBY:
  177. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  178. pr_debug("vaud_bias power up rail\n");
  179. /* power up the rail */
  180. snd_soc_write(codec, SN95031_VAUD,
  181. BIT(2)|BIT(1)|BIT(0));
  182. msleep(1);
  183. } else if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
  184. /* turn off pcm */
  185. pr_debug("vaud_bias power dn pcm\n");
  186. snd_soc_update_bits(codec, SN95031_PCM2C2, BIT(0), 0);
  187. snd_soc_write(codec, SN95031_AUDPLLCTRL, 0);
  188. }
  189. break;
  190. case SND_SOC_BIAS_OFF:
  191. pr_debug("vaud_bias _OFF doing rail shutdown\n");
  192. snd_soc_write(codec, SN95031_VAUD, BIT(3));
  193. break;
  194. }
  195. codec->dapm.bias_level = level;
  196. return 0;
  197. }
  198. static int sn95031_vhs_event(struct snd_soc_dapm_widget *w,
  199. struct snd_kcontrol *kcontrol, int event)
  200. {
  201. if (SND_SOC_DAPM_EVENT_ON(event)) {
  202. pr_debug("VHS SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
  203. /* power up the rail */
  204. snd_soc_write(w->codec, SN95031_VHSP, 0x3D);
  205. snd_soc_write(w->codec, SN95031_VHSN, 0x3F);
  206. msleep(1);
  207. } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
  208. pr_debug("VHS SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
  209. snd_soc_write(w->codec, SN95031_VHSP, 0xC4);
  210. snd_soc_write(w->codec, SN95031_VHSN, 0x04);
  211. }
  212. return 0;
  213. }
  214. static int sn95031_vihf_event(struct snd_soc_dapm_widget *w,
  215. struct snd_kcontrol *kcontrol, int event)
  216. {
  217. if (SND_SOC_DAPM_EVENT_ON(event)) {
  218. pr_debug("VIHF SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
  219. /* power up the rail */
  220. snd_soc_write(w->codec, SN95031_VIHF, 0x27);
  221. msleep(1);
  222. } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
  223. pr_debug("VIHF SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
  224. snd_soc_write(w->codec, SN95031_VIHF, 0x24);
  225. }
  226. return 0;
  227. }
  228. static int sn95031_dmic12_event(struct snd_soc_dapm_widget *w,
  229. struct snd_kcontrol *k, int event)
  230. {
  231. unsigned int ldo = 0, clk_dir = 0, data_dir = 0;
  232. if (SND_SOC_DAPM_EVENT_ON(event)) {
  233. ldo = BIT(5)|BIT(4);
  234. clk_dir = BIT(0);
  235. data_dir = BIT(7);
  236. }
  237. /* program DMIC LDO, clock and set clock */
  238. snd_soc_update_bits(w->codec, SN95031_MICBIAS, BIT(5)|BIT(4), ldo);
  239. snd_soc_update_bits(w->codec, SN95031_DMICBUF0123, BIT(0), clk_dir);
  240. snd_soc_update_bits(w->codec, SN95031_DMICBUF0123, BIT(7), data_dir);
  241. return 0;
  242. }
  243. static int sn95031_dmic34_event(struct snd_soc_dapm_widget *w,
  244. struct snd_kcontrol *k, int event)
  245. {
  246. unsigned int ldo = 0, clk_dir = 0, data_dir = 0;
  247. if (SND_SOC_DAPM_EVENT_ON(event)) {
  248. ldo = BIT(5)|BIT(4);
  249. clk_dir = BIT(2);
  250. data_dir = BIT(1);
  251. }
  252. /* program DMIC LDO, clock and set clock */
  253. snd_soc_update_bits(w->codec, SN95031_MICBIAS, BIT(5)|BIT(4), ldo);
  254. snd_soc_update_bits(w->codec, SN95031_DMICBUF0123, BIT(2), clk_dir);
  255. snd_soc_update_bits(w->codec, SN95031_DMICBUF45, BIT(1), data_dir);
  256. return 0;
  257. }
  258. static int sn95031_dmic56_event(struct snd_soc_dapm_widget *w,
  259. struct snd_kcontrol *k, int event)
  260. {
  261. unsigned int ldo = 0;
  262. if (SND_SOC_DAPM_EVENT_ON(event))
  263. ldo = BIT(7)|BIT(6);
  264. /* program DMIC LDO */
  265. snd_soc_update_bits(w->codec, SN95031_MICBIAS, BIT(7)|BIT(6), ldo);
  266. return 0;
  267. }
  268. /* mux controls */
  269. static const char *sn95031_mic_texts[] = { "AMIC", "LineIn" };
  270. static const struct soc_enum sn95031_micl_enum =
  271. SOC_ENUM_SINGLE(SN95031_ADCCONFIG, 1, 2, sn95031_mic_texts);
  272. static const struct snd_kcontrol_new sn95031_micl_mux_control =
  273. SOC_DAPM_ENUM("Route", sn95031_micl_enum);
  274. static const struct soc_enum sn95031_micr_enum =
  275. SOC_ENUM_SINGLE(SN95031_ADCCONFIG, 3, 2, sn95031_mic_texts);
  276. static const struct snd_kcontrol_new sn95031_micr_mux_control =
  277. SOC_DAPM_ENUM("Route", sn95031_micr_enum);
  278. static const char *sn95031_input_texts[] = { "DMIC1", "DMIC2", "DMIC3",
  279. "DMIC4", "DMIC5", "DMIC6",
  280. "ADC Left", "ADC Right" };
  281. static const struct soc_enum sn95031_input1_enum =
  282. SOC_ENUM_SINGLE(SN95031_AUDIOMUX12, 0, 8, sn95031_input_texts);
  283. static const struct snd_kcontrol_new sn95031_input1_mux_control =
  284. SOC_DAPM_ENUM("Route", sn95031_input1_enum);
  285. static const struct soc_enum sn95031_input2_enum =
  286. SOC_ENUM_SINGLE(SN95031_AUDIOMUX12, 4, 8, sn95031_input_texts);
  287. static const struct snd_kcontrol_new sn95031_input2_mux_control =
  288. SOC_DAPM_ENUM("Route", sn95031_input2_enum);
  289. static const struct soc_enum sn95031_input3_enum =
  290. SOC_ENUM_SINGLE(SN95031_AUDIOMUX34, 0, 8, sn95031_input_texts);
  291. static const struct snd_kcontrol_new sn95031_input3_mux_control =
  292. SOC_DAPM_ENUM("Route", sn95031_input3_enum);
  293. static const struct soc_enum sn95031_input4_enum =
  294. SOC_ENUM_SINGLE(SN95031_AUDIOMUX34, 4, 8, sn95031_input_texts);
  295. static const struct snd_kcontrol_new sn95031_input4_mux_control =
  296. SOC_DAPM_ENUM("Route", sn95031_input4_enum);
  297. /* capture path controls */
  298. static const char *sn95031_micmode_text[] = {"Single Ended", "Differential"};
  299. /* 0dB to 30dB in 10dB steps */
  300. static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 10, 0);
  301. static const struct soc_enum sn95031_micmode1_enum =
  302. SOC_ENUM_SINGLE(SN95031_MICAMP1, 1, 2, sn95031_micmode_text);
  303. static const struct soc_enum sn95031_micmode2_enum =
  304. SOC_ENUM_SINGLE(SN95031_MICAMP2, 1, 2, sn95031_micmode_text);
  305. static const char *sn95031_dmic_cfg_text[] = {"GPO", "DMIC"};
  306. static const struct soc_enum sn95031_dmic12_cfg_enum =
  307. SOC_ENUM_SINGLE(SN95031_DMICMUX, 0, 2, sn95031_dmic_cfg_text);
  308. static const struct soc_enum sn95031_dmic34_cfg_enum =
  309. SOC_ENUM_SINGLE(SN95031_DMICMUX, 1, 2, sn95031_dmic_cfg_text);
  310. static const struct soc_enum sn95031_dmic56_cfg_enum =
  311. SOC_ENUM_SINGLE(SN95031_DMICMUX, 2, 2, sn95031_dmic_cfg_text);
  312. static const struct snd_kcontrol_new sn95031_snd_controls[] = {
  313. SOC_ENUM("Mic1Mode Capture Route", sn95031_micmode1_enum),
  314. SOC_ENUM("Mic2Mode Capture Route", sn95031_micmode2_enum),
  315. SOC_ENUM("DMIC12 Capture Route", sn95031_dmic12_cfg_enum),
  316. SOC_ENUM("DMIC34 Capture Route", sn95031_dmic34_cfg_enum),
  317. SOC_ENUM("DMIC56 Capture Route", sn95031_dmic56_cfg_enum),
  318. SOC_SINGLE_TLV("Mic1 Capture Volume", SN95031_MICAMP1,
  319. 2, 4, 0, mic_tlv),
  320. SOC_SINGLE_TLV("Mic2 Capture Volume", SN95031_MICAMP2,
  321. 2, 4, 0, mic_tlv),
  322. };
  323. /* DAPM widgets */
  324. static const struct snd_soc_dapm_widget sn95031_dapm_widgets[] = {
  325. /* all end points mic, hs etc */
  326. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  327. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  328. SND_SOC_DAPM_OUTPUT("EPOUT"),
  329. SND_SOC_DAPM_OUTPUT("IHFOUTL"),
  330. SND_SOC_DAPM_OUTPUT("IHFOUTR"),
  331. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  332. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  333. SND_SOC_DAPM_OUTPUT("VIB1OUT"),
  334. SND_SOC_DAPM_OUTPUT("VIB2OUT"),
  335. SND_SOC_DAPM_INPUT("AMIC1"), /* headset mic */
  336. SND_SOC_DAPM_INPUT("AMIC2"),
  337. SND_SOC_DAPM_INPUT("DMIC1"),
  338. SND_SOC_DAPM_INPUT("DMIC2"),
  339. SND_SOC_DAPM_INPUT("DMIC3"),
  340. SND_SOC_DAPM_INPUT("DMIC4"),
  341. SND_SOC_DAPM_INPUT("DMIC5"),
  342. SND_SOC_DAPM_INPUT("DMIC6"),
  343. SND_SOC_DAPM_INPUT("LINEINL"),
  344. SND_SOC_DAPM_INPUT("LINEINR"),
  345. SND_SOC_DAPM_MICBIAS("AMIC1Bias", SN95031_MICBIAS, 2, 0),
  346. SND_SOC_DAPM_MICBIAS("AMIC2Bias", SN95031_MICBIAS, 3, 0),
  347. SND_SOC_DAPM_MICBIAS("DMIC12Bias", SN95031_DMICMUX, 3, 0),
  348. SND_SOC_DAPM_MICBIAS("DMIC34Bias", SN95031_DMICMUX, 4, 0),
  349. SND_SOC_DAPM_MICBIAS("DMIC56Bias", SN95031_DMICMUX, 5, 0),
  350. SND_SOC_DAPM_SUPPLY("DMIC12supply", SN95031_DMICLK, 0, 0,
  351. sn95031_dmic12_event,
  352. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  353. SND_SOC_DAPM_SUPPLY("DMIC34supply", SN95031_DMICLK, 1, 0,
  354. sn95031_dmic34_event,
  355. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  356. SND_SOC_DAPM_SUPPLY("DMIC56supply", SN95031_DMICLK, 2, 0,
  357. sn95031_dmic56_event,
  358. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  359. SND_SOC_DAPM_AIF_OUT("PCM_Out", "Capture", 0,
  360. SND_SOC_NOPM, 0, 0),
  361. SND_SOC_DAPM_SUPPLY("Headset Rail", SND_SOC_NOPM, 0, 0,
  362. sn95031_vhs_event,
  363. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  364. SND_SOC_DAPM_SUPPLY("Speaker Rail", SND_SOC_NOPM, 0, 0,
  365. sn95031_vihf_event,
  366. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  367. /* playback path driver enables */
  368. SND_SOC_DAPM_PGA("Headset Left Playback",
  369. SN95031_DRIVEREN, 0, 0, NULL, 0),
  370. SND_SOC_DAPM_PGA("Headset Right Playback",
  371. SN95031_DRIVEREN, 1, 0, NULL, 0),
  372. SND_SOC_DAPM_PGA("Speaker Left Playback",
  373. SN95031_DRIVEREN, 2, 0, NULL, 0),
  374. SND_SOC_DAPM_PGA("Speaker Right Playback",
  375. SN95031_DRIVEREN, 3, 0, NULL, 0),
  376. SND_SOC_DAPM_PGA("Vibra1 Playback",
  377. SN95031_DRIVEREN, 4, 0, NULL, 0),
  378. SND_SOC_DAPM_PGA("Vibra2 Playback",
  379. SN95031_DRIVEREN, 5, 0, NULL, 0),
  380. SND_SOC_DAPM_PGA("Earpiece Playback",
  381. SN95031_DRIVEREN, 6, 0, NULL, 0),
  382. SND_SOC_DAPM_PGA("Lineout Left Playback",
  383. SN95031_LOCTL, 0, 0, NULL, 0),
  384. SND_SOC_DAPM_PGA("Lineout Right Playback",
  385. SN95031_LOCTL, 4, 0, NULL, 0),
  386. /* playback path filter enable */
  387. SND_SOC_DAPM_PGA("Headset Left Filter",
  388. SN95031_HSEPRXCTRL, 4, 0, NULL, 0),
  389. SND_SOC_DAPM_PGA("Headset Right Filter",
  390. SN95031_HSEPRXCTRL, 5, 0, NULL, 0),
  391. SND_SOC_DAPM_PGA("Speaker Left Filter",
  392. SN95031_IHFRXCTRL, 0, 0, NULL, 0),
  393. SND_SOC_DAPM_PGA("Speaker Right Filter",
  394. SN95031_IHFRXCTRL, 1, 0, NULL, 0),
  395. /* DACs */
  396. SND_SOC_DAPM_DAC("HSDAC Left", "Headset",
  397. SN95031_DACCONFIG, 0, 0),
  398. SND_SOC_DAPM_DAC("HSDAC Right", "Headset",
  399. SN95031_DACCONFIG, 1, 0),
  400. SND_SOC_DAPM_DAC("IHFDAC Left", "Speaker",
  401. SN95031_DACCONFIG, 2, 0),
  402. SND_SOC_DAPM_DAC("IHFDAC Right", "Speaker",
  403. SN95031_DACCONFIG, 3, 0),
  404. SND_SOC_DAPM_DAC("Vibra1 DAC", "Vibra1",
  405. SN95031_VIB1C5, 1, 0),
  406. SND_SOC_DAPM_DAC("Vibra2 DAC", "Vibra2",
  407. SN95031_VIB2C5, 1, 0),
  408. /* capture widgets */
  409. SND_SOC_DAPM_PGA("LineIn Enable Left", SN95031_MICAMP1,
  410. 7, 0, NULL, 0),
  411. SND_SOC_DAPM_PGA("LineIn Enable Right", SN95031_MICAMP2,
  412. 7, 0, NULL, 0),
  413. SND_SOC_DAPM_PGA("MIC1 Enable", SN95031_MICAMP1, 0, 0, NULL, 0),
  414. SND_SOC_DAPM_PGA("MIC2 Enable", SN95031_MICAMP2, 0, 0, NULL, 0),
  415. SND_SOC_DAPM_PGA("TX1 Enable", SN95031_AUDIOTXEN, 2, 0, NULL, 0),
  416. SND_SOC_DAPM_PGA("TX2 Enable", SN95031_AUDIOTXEN, 3, 0, NULL, 0),
  417. SND_SOC_DAPM_PGA("TX3 Enable", SN95031_AUDIOTXEN, 4, 0, NULL, 0),
  418. SND_SOC_DAPM_PGA("TX4 Enable", SN95031_AUDIOTXEN, 5, 0, NULL, 0),
  419. /* ADC have null stream as they will be turned ON by TX path */
  420. SND_SOC_DAPM_ADC("ADC Left", NULL,
  421. SN95031_ADCCONFIG, 0, 0),
  422. SND_SOC_DAPM_ADC("ADC Right", NULL,
  423. SN95031_ADCCONFIG, 2, 0),
  424. SND_SOC_DAPM_MUX("Mic_InputL Capture Route",
  425. SND_SOC_NOPM, 0, 0, &sn95031_micl_mux_control),
  426. SND_SOC_DAPM_MUX("Mic_InputR Capture Route",
  427. SND_SOC_NOPM, 0, 0, &sn95031_micr_mux_control),
  428. SND_SOC_DAPM_MUX("Txpath1 Capture Route",
  429. SND_SOC_NOPM, 0, 0, &sn95031_input1_mux_control),
  430. SND_SOC_DAPM_MUX("Txpath2 Capture Route",
  431. SND_SOC_NOPM, 0, 0, &sn95031_input2_mux_control),
  432. SND_SOC_DAPM_MUX("Txpath3 Capture Route",
  433. SND_SOC_NOPM, 0, 0, &sn95031_input3_mux_control),
  434. SND_SOC_DAPM_MUX("Txpath4 Capture Route",
  435. SND_SOC_NOPM, 0, 0, &sn95031_input4_mux_control),
  436. };
  437. static const struct snd_soc_dapm_route sn95031_audio_map[] = {
  438. /* headset and earpiece map */
  439. { "HPOUTL", NULL, "Headset Rail"},
  440. { "HPOUTR", NULL, "Headset Rail"},
  441. { "HPOUTL", NULL, "Headset Left Playback" },
  442. { "HPOUTR", NULL, "Headset Right Playback" },
  443. { "EPOUT", NULL, "Earpiece Playback" },
  444. { "Headset Left Playback", NULL, "Headset Left Filter"},
  445. { "Headset Right Playback", NULL, "Headset Right Filter"},
  446. { "Earpiece Playback", NULL, "Headset Left Filter"},
  447. { "Headset Left Filter", NULL, "HSDAC Left"},
  448. { "Headset Right Filter", NULL, "HSDAC Right"},
  449. /* speaker map */
  450. { "IHFOUTL", NULL, "Speaker Rail"},
  451. { "IHFOUTR", NULL, "Speaker Rail"},
  452. { "IHFOUTL", "NULL", "Speaker Left Playback"},
  453. { "IHFOUTR", "NULL", "Speaker Right Playback"},
  454. { "Speaker Left Playback", NULL, "Speaker Left Filter"},
  455. { "Speaker Right Playback", NULL, "Speaker Right Filter"},
  456. { "Speaker Left Filter", NULL, "IHFDAC Left"},
  457. { "Speaker Right Filter", NULL, "IHFDAC Right"},
  458. /* vibra map */
  459. { "VIB1OUT", NULL, "Vibra1 Playback"},
  460. { "Vibra1 Playback", NULL, "Vibra1 DAC"},
  461. { "VIB2OUT", NULL, "Vibra2 Playback"},
  462. { "Vibra2 Playback", NULL, "Vibra2 DAC"},
  463. /* lineout */
  464. { "LINEOUTL", NULL, "Lineout Left Playback"},
  465. { "LINEOUTR", NULL, "Lineout Right Playback"},
  466. { "Lineout Left Playback", NULL, "Headset Left Filter"},
  467. { "Lineout Left Playback", NULL, "Speaker Left Filter"},
  468. { "Lineout Left Playback", NULL, "Vibra1 DAC"},
  469. { "Lineout Right Playback", NULL, "Headset Right Filter"},
  470. { "Lineout Right Playback", NULL, "Speaker Right Filter"},
  471. { "Lineout Right Playback", NULL, "Vibra2 DAC"},
  472. /* Headset (AMIC1) mic */
  473. { "AMIC1Bias", NULL, "AMIC1"},
  474. { "MIC1 Enable", NULL, "AMIC1Bias"},
  475. { "Mic_InputL Capture Route", "AMIC", "MIC1 Enable"},
  476. /* AMIC2 */
  477. { "AMIC2Bias", NULL, "AMIC2"},
  478. { "MIC2 Enable", NULL, "AMIC2Bias"},
  479. { "Mic_InputR Capture Route", "AMIC", "MIC2 Enable"},
  480. /* Linein */
  481. { "LineIn Enable Left", NULL, "LINEINL"},
  482. { "LineIn Enable Right", NULL, "LINEINR"},
  483. { "Mic_InputL Capture Route", "LineIn", "LineIn Enable Left"},
  484. { "Mic_InputR Capture Route", "LineIn", "LineIn Enable Right"},
  485. /* ADC connection */
  486. { "ADC Left", NULL, "Mic_InputL Capture Route"},
  487. { "ADC Right", NULL, "Mic_InputR Capture Route"},
  488. /*DMIC connections */
  489. { "DMIC1", NULL, "DMIC12supply"},
  490. { "DMIC2", NULL, "DMIC12supply"},
  491. { "DMIC3", NULL, "DMIC34supply"},
  492. { "DMIC4", NULL, "DMIC34supply"},
  493. { "DMIC5", NULL, "DMIC56supply"},
  494. { "DMIC6", NULL, "DMIC56supply"},
  495. { "DMIC12Bias", NULL, "DMIC1"},
  496. { "DMIC12Bias", NULL, "DMIC2"},
  497. { "DMIC34Bias", NULL, "DMIC3"},
  498. { "DMIC34Bias", NULL, "DMIC4"},
  499. { "DMIC56Bias", NULL, "DMIC5"},
  500. { "DMIC56Bias", NULL, "DMIC6"},
  501. /*TX path inputs*/
  502. { "Txpath1 Capture Route", "ADC Left", "ADC Left"},
  503. { "Txpath2 Capture Route", "ADC Left", "ADC Left"},
  504. { "Txpath3 Capture Route", "ADC Left", "ADC Left"},
  505. { "Txpath4 Capture Route", "ADC Left", "ADC Left"},
  506. { "Txpath1 Capture Route", "ADC Right", "ADC Right"},
  507. { "Txpath2 Capture Route", "ADC Right", "ADC Right"},
  508. { "Txpath3 Capture Route", "ADC Right", "ADC Right"},
  509. { "Txpath4 Capture Route", "ADC Right", "ADC Right"},
  510. { "Txpath1 Capture Route", "DMIC1", "DMIC1"},
  511. { "Txpath2 Capture Route", "DMIC1", "DMIC1"},
  512. { "Txpath3 Capture Route", "DMIC1", "DMIC1"},
  513. { "Txpath4 Capture Route", "DMIC1", "DMIC1"},
  514. { "Txpath1 Capture Route", "DMIC2", "DMIC2"},
  515. { "Txpath2 Capture Route", "DMIC2", "DMIC2"},
  516. { "Txpath3 Capture Route", "DMIC2", "DMIC2"},
  517. { "Txpath4 Capture Route", "DMIC2", "DMIC2"},
  518. { "Txpath1 Capture Route", "DMIC3", "DMIC3"},
  519. { "Txpath2 Capture Route", "DMIC3", "DMIC3"},
  520. { "Txpath3 Capture Route", "DMIC3", "DMIC3"},
  521. { "Txpath4 Capture Route", "DMIC3", "DMIC3"},
  522. { "Txpath1 Capture Route", "DMIC4", "DMIC4"},
  523. { "Txpath2 Capture Route", "DMIC4", "DMIC4"},
  524. { "Txpath3 Capture Route", "DMIC4", "DMIC4"},
  525. { "Txpath4 Capture Route", "DMIC4", "DMIC4"},
  526. { "Txpath1 Capture Route", "DMIC5", "DMIC5"},
  527. { "Txpath2 Capture Route", "DMIC5", "DMIC5"},
  528. { "Txpath3 Capture Route", "DMIC5", "DMIC5"},
  529. { "Txpath4 Capture Route", "DMIC5", "DMIC5"},
  530. { "Txpath1 Capture Route", "DMIC6", "DMIC6"},
  531. { "Txpath2 Capture Route", "DMIC6", "DMIC6"},
  532. { "Txpath3 Capture Route", "DMIC6", "DMIC6"},
  533. { "Txpath4 Capture Route", "DMIC6", "DMIC6"},
  534. /* tx path */
  535. { "TX1 Enable", NULL, "Txpath1 Capture Route"},
  536. { "TX2 Enable", NULL, "Txpath2 Capture Route"},
  537. { "TX3 Enable", NULL, "Txpath3 Capture Route"},
  538. { "TX4 Enable", NULL, "Txpath4 Capture Route"},
  539. { "PCM_Out", NULL, "TX1 Enable"},
  540. { "PCM_Out", NULL, "TX2 Enable"},
  541. { "PCM_Out", NULL, "TX3 Enable"},
  542. { "PCM_Out", NULL, "TX4 Enable"},
  543. };
  544. /* speaker and headset mutes, for audio pops and clicks */
  545. static int sn95031_pcm_hs_mute(struct snd_soc_dai *dai, int mute)
  546. {
  547. snd_soc_update_bits(dai->codec,
  548. SN95031_HSLVOLCTRL, BIT(7), (!mute << 7));
  549. snd_soc_update_bits(dai->codec,
  550. SN95031_HSRVOLCTRL, BIT(7), (!mute << 7));
  551. return 0;
  552. }
  553. static int sn95031_pcm_spkr_mute(struct snd_soc_dai *dai, int mute)
  554. {
  555. snd_soc_update_bits(dai->codec,
  556. SN95031_IHFLVOLCTRL, BIT(7), (!mute << 7));
  557. snd_soc_update_bits(dai->codec,
  558. SN95031_IHFRVOLCTRL, BIT(7), (!mute << 7));
  559. return 0;
  560. }
  561. static int sn95031_pcm_hw_params(struct snd_pcm_substream *substream,
  562. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  563. {
  564. unsigned int format, rate;
  565. switch (params_format(params)) {
  566. case SNDRV_PCM_FORMAT_S16_LE:
  567. format = BIT(4)|BIT(5);
  568. break;
  569. case SNDRV_PCM_FORMAT_S24_LE:
  570. format = 0;
  571. break;
  572. default:
  573. return -EINVAL;
  574. }
  575. snd_soc_update_bits(dai->codec, SN95031_PCM2C2,
  576. BIT(4)|BIT(5), format);
  577. switch (params_rate(params)) {
  578. case 48000:
  579. pr_debug("RATE_48000\n");
  580. rate = 0;
  581. break;
  582. case 44100:
  583. pr_debug("RATE_44100\n");
  584. rate = BIT(7);
  585. break;
  586. default:
  587. pr_err("ERR rate %d\n", params_rate(params));
  588. return -EINVAL;
  589. }
  590. snd_soc_update_bits(dai->codec, SN95031_PCM1C1, BIT(7), rate);
  591. return 0;
  592. }
  593. /* Codec DAI section */
  594. static const struct snd_soc_dai_ops sn95031_headset_dai_ops = {
  595. .digital_mute = sn95031_pcm_hs_mute,
  596. .hw_params = sn95031_pcm_hw_params,
  597. };
  598. static const struct snd_soc_dai_ops sn95031_speaker_dai_ops = {
  599. .digital_mute = sn95031_pcm_spkr_mute,
  600. .hw_params = sn95031_pcm_hw_params,
  601. };
  602. static const struct snd_soc_dai_ops sn95031_vib1_dai_ops = {
  603. .hw_params = sn95031_pcm_hw_params,
  604. };
  605. static const struct snd_soc_dai_ops sn95031_vib2_dai_ops = {
  606. .hw_params = sn95031_pcm_hw_params,
  607. };
  608. static struct snd_soc_dai_driver sn95031_dais[] = {
  609. {
  610. .name = "SN95031 Headset",
  611. .playback = {
  612. .stream_name = "Headset",
  613. .channels_min = 2,
  614. .channels_max = 2,
  615. .rates = SN95031_RATES,
  616. .formats = SN95031_FORMATS,
  617. },
  618. .capture = {
  619. .stream_name = "Capture",
  620. .channels_min = 1,
  621. .channels_max = 5,
  622. .rates = SN95031_RATES,
  623. .formats = SN95031_FORMATS,
  624. },
  625. .ops = &sn95031_headset_dai_ops,
  626. },
  627. { .name = "SN95031 Speaker",
  628. .playback = {
  629. .stream_name = "Speaker",
  630. .channels_min = 2,
  631. .channels_max = 2,
  632. .rates = SN95031_RATES,
  633. .formats = SN95031_FORMATS,
  634. },
  635. .ops = &sn95031_speaker_dai_ops,
  636. },
  637. { .name = "SN95031 Vibra1",
  638. .playback = {
  639. .stream_name = "Vibra1",
  640. .channels_min = 1,
  641. .channels_max = 1,
  642. .rates = SN95031_RATES,
  643. .formats = SN95031_FORMATS,
  644. },
  645. .ops = &sn95031_vib1_dai_ops,
  646. },
  647. { .name = "SN95031 Vibra2",
  648. .playback = {
  649. .stream_name = "Vibra2",
  650. .channels_min = 1,
  651. .channels_max = 1,
  652. .rates = SN95031_RATES,
  653. .formats = SN95031_FORMATS,
  654. },
  655. .ops = &sn95031_vib2_dai_ops,
  656. },
  657. };
  658. static inline void sn95031_disable_jack_btn(struct snd_soc_codec *codec)
  659. {
  660. snd_soc_write(codec, SN95031_BTNCTRL2, 0x00);
  661. }
  662. static inline void sn95031_enable_jack_btn(struct snd_soc_codec *codec)
  663. {
  664. snd_soc_write(codec, SN95031_BTNCTRL1, 0x77);
  665. snd_soc_write(codec, SN95031_BTNCTRL2, 0x01);
  666. }
  667. static int sn95031_get_headset_state(struct snd_soc_jack *mfld_jack)
  668. {
  669. int micbias = sn95031_get_mic_bias(mfld_jack->codec);
  670. int jack_type = snd_soc_jack_get_type(mfld_jack, micbias);
  671. pr_debug("jack type detected = %d\n", jack_type);
  672. if (jack_type == SND_JACK_HEADSET)
  673. sn95031_enable_jack_btn(mfld_jack->codec);
  674. return jack_type;
  675. }
  676. void sn95031_jack_detection(struct mfld_jack_data *jack_data)
  677. {
  678. unsigned int status;
  679. unsigned int mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_HEADSET;
  680. pr_debug("interrupt id read in sram = 0x%x\n", jack_data->intr_id);
  681. if (jack_data->intr_id & 0x1) {
  682. pr_debug("short_push detected\n");
  683. status = SND_JACK_HEADSET | SND_JACK_BTN_0;
  684. } else if (jack_data->intr_id & 0x2) {
  685. pr_debug("long_push detected\n");
  686. status = SND_JACK_HEADSET | SND_JACK_BTN_1;
  687. } else if (jack_data->intr_id & 0x4) {
  688. pr_debug("headset or headphones inserted\n");
  689. status = sn95031_get_headset_state(jack_data->mfld_jack);
  690. } else if (jack_data->intr_id & 0x8) {
  691. pr_debug("headset or headphones removed\n");
  692. status = 0;
  693. sn95031_disable_jack_btn(jack_data->mfld_jack->codec);
  694. } else {
  695. pr_err("unidentified interrupt\n");
  696. return;
  697. }
  698. snd_soc_jack_report(jack_data->mfld_jack, status, mask);
  699. /*button pressed and released so we send explicit button release */
  700. if ((status & SND_JACK_BTN_0) | (status & SND_JACK_BTN_1))
  701. snd_soc_jack_report(jack_data->mfld_jack,
  702. SND_JACK_HEADSET, mask);
  703. }
  704. EXPORT_SYMBOL_GPL(sn95031_jack_detection);
  705. /* codec registration */
  706. static int sn95031_codec_probe(struct snd_soc_codec *codec)
  707. {
  708. pr_debug("codec_probe called\n");
  709. snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
  710. /* PCM interface config
  711. * This sets the pcm rx slot conguration to max 6 slots
  712. * for max 4 dais (2 stereo and 2 mono)
  713. */
  714. snd_soc_write(codec, SN95031_PCM2RXSLOT01, 0x10);
  715. snd_soc_write(codec, SN95031_PCM2RXSLOT23, 0x32);
  716. snd_soc_write(codec, SN95031_PCM2RXSLOT45, 0x54);
  717. snd_soc_write(codec, SN95031_PCM2TXSLOT01, 0x10);
  718. snd_soc_write(codec, SN95031_PCM2TXSLOT23, 0x32);
  719. /* pcm port setting
  720. * This sets the pcm port to slave and clock at 19.2Mhz which
  721. * can support 6slots, sampling rate set per stream in hw-params
  722. */
  723. snd_soc_write(codec, SN95031_PCM1C1, 0x00);
  724. snd_soc_write(codec, SN95031_PCM2C1, 0x01);
  725. snd_soc_write(codec, SN95031_PCM2C2, 0x0A);
  726. snd_soc_write(codec, SN95031_HSMIXER, BIT(0)|BIT(4));
  727. /* vendor vibra workround, the vibras are muted by
  728. * custom register so unmute them
  729. */
  730. snd_soc_write(codec, SN95031_SSR5, 0x80);
  731. snd_soc_write(codec, SN95031_SSR6, 0x80);
  732. snd_soc_write(codec, SN95031_VIB1C5, 0x00);
  733. snd_soc_write(codec, SN95031_VIB2C5, 0x00);
  734. /* configure vibras for pcm port */
  735. snd_soc_write(codec, SN95031_VIB1C3, 0x00);
  736. snd_soc_write(codec, SN95031_VIB2C3, 0x00);
  737. /* soft mute ramp time */
  738. snd_soc_write(codec, SN95031_SOFTMUTE, 0x3);
  739. /* fix the initial volume at 1dB,
  740. * default in +9dB,
  741. * 1dB give optimal swing on DAC, amps
  742. */
  743. snd_soc_write(codec, SN95031_HSLVOLCTRL, 0x08);
  744. snd_soc_write(codec, SN95031_HSRVOLCTRL, 0x08);
  745. snd_soc_write(codec, SN95031_IHFLVOLCTRL, 0x08);
  746. snd_soc_write(codec, SN95031_IHFRVOLCTRL, 0x08);
  747. /* dac mode and lineout workaround */
  748. snd_soc_write(codec, SN95031_SSR2, 0x10);
  749. snd_soc_write(codec, SN95031_SSR3, 0x40);
  750. snd_soc_add_codec_controls(codec, sn95031_snd_controls,
  751. ARRAY_SIZE(sn95031_snd_controls));
  752. return 0;
  753. }
  754. static int sn95031_codec_remove(struct snd_soc_codec *codec)
  755. {
  756. pr_debug("codec_remove called\n");
  757. sn95031_set_vaud_bias(codec, SND_SOC_BIAS_OFF);
  758. return 0;
  759. }
  760. static struct snd_soc_codec_driver sn95031_codec = {
  761. .probe = sn95031_codec_probe,
  762. .remove = sn95031_codec_remove,
  763. .set_bias_level = sn95031_set_vaud_bias,
  764. .idle_bias_off = true,
  765. .dapm_widgets = sn95031_dapm_widgets,
  766. .num_dapm_widgets = ARRAY_SIZE(sn95031_dapm_widgets),
  767. .dapm_routes = sn95031_audio_map,
  768. .num_dapm_routes = ARRAY_SIZE(sn95031_audio_map),
  769. };
  770. static int sn95031_device_probe(struct platform_device *pdev)
  771. {
  772. struct regmap *regmap;
  773. pr_debug("codec device probe called for %s\n", dev_name(&pdev->dev));
  774. regmap = devm_regmap_init(&pdev->dev, NULL, NULL, &sn95031_regmap);
  775. if (IS_ERR(regmap))
  776. return PTR_ERR(regmap);
  777. return snd_soc_register_codec(&pdev->dev, &sn95031_codec,
  778. sn95031_dais, ARRAY_SIZE(sn95031_dais));
  779. }
  780. static int sn95031_device_remove(struct platform_device *pdev)
  781. {
  782. pr_debug("codec device remove called\n");
  783. snd_soc_unregister_codec(&pdev->dev);
  784. return 0;
  785. }
  786. static struct platform_driver sn95031_codec_driver = {
  787. .driver = {
  788. .name = "sn95031",
  789. .owner = THIS_MODULE,
  790. },
  791. .probe = sn95031_device_probe,
  792. .remove = sn95031_device_remove,
  793. };
  794. module_platform_driver(sn95031_codec_driver);
  795. MODULE_DESCRIPTION("ASoC TI SN95031 codec driver");
  796. MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
  797. MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
  798. MODULE_LICENSE("GPL v2");
  799. MODULE_ALIAS("platform:sn95031");