sn95031.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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 inline unsigned int sn95031_read(struct snd_soc_codec *codec,
  144. unsigned int reg)
  145. {
  146. u8 value = 0;
  147. int ret;
  148. ret = intel_scu_ipc_ioread8(reg, &value);
  149. if (ret)
  150. pr_err("read of %x failed, err %d\n", reg, ret);
  151. return value;
  152. }
  153. static inline int sn95031_write(struct snd_soc_codec *codec,
  154. unsigned int reg, unsigned int value)
  155. {
  156. int ret;
  157. ret = intel_scu_ipc_iowrite8(reg, value);
  158. if (ret)
  159. pr_err("write of %x failed, err %d\n", reg, ret);
  160. return ret;
  161. }
  162. static int sn95031_set_vaud_bias(struct snd_soc_codec *codec,
  163. enum snd_soc_bias_level level)
  164. {
  165. switch (level) {
  166. case SND_SOC_BIAS_ON:
  167. break;
  168. case SND_SOC_BIAS_PREPARE:
  169. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
  170. pr_debug("vaud_bias powering up pll\n");
  171. /* power up the pll */
  172. snd_soc_write(codec, SN95031_AUDPLLCTRL, BIT(5));
  173. /* enable pcm 2 */
  174. snd_soc_update_bits(codec, SN95031_PCM2C2,
  175. BIT(0), BIT(0));
  176. }
  177. break;
  178. case SND_SOC_BIAS_STANDBY:
  179. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  180. pr_debug("vaud_bias power up rail\n");
  181. /* power up the rail */
  182. snd_soc_write(codec, SN95031_VAUD,
  183. BIT(2)|BIT(1)|BIT(0));
  184. msleep(1);
  185. } else if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
  186. /* turn off pcm */
  187. pr_debug("vaud_bias power dn pcm\n");
  188. snd_soc_update_bits(codec, SN95031_PCM2C2, BIT(0), 0);
  189. snd_soc_write(codec, SN95031_AUDPLLCTRL, 0);
  190. }
  191. break;
  192. case SND_SOC_BIAS_OFF:
  193. pr_debug("vaud_bias _OFF doing rail shutdown\n");
  194. snd_soc_write(codec, SN95031_VAUD, BIT(3));
  195. break;
  196. }
  197. codec->dapm.bias_level = level;
  198. return 0;
  199. }
  200. static int sn95031_vhs_event(struct snd_soc_dapm_widget *w,
  201. struct snd_kcontrol *kcontrol, int event)
  202. {
  203. if (SND_SOC_DAPM_EVENT_ON(event)) {
  204. pr_debug("VHS SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
  205. /* power up the rail */
  206. snd_soc_write(w->codec, SN95031_VHSP, 0x3D);
  207. snd_soc_write(w->codec, SN95031_VHSN, 0x3F);
  208. msleep(1);
  209. } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
  210. pr_debug("VHS SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
  211. snd_soc_write(w->codec, SN95031_VHSP, 0xC4);
  212. snd_soc_write(w->codec, SN95031_VHSN, 0x04);
  213. }
  214. return 0;
  215. }
  216. static int sn95031_vihf_event(struct snd_soc_dapm_widget *w,
  217. struct snd_kcontrol *kcontrol, int event)
  218. {
  219. if (SND_SOC_DAPM_EVENT_ON(event)) {
  220. pr_debug("VIHF SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
  221. /* power up the rail */
  222. snd_soc_write(w->codec, SN95031_VIHF, 0x27);
  223. msleep(1);
  224. } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
  225. pr_debug("VIHF SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
  226. snd_soc_write(w->codec, SN95031_VIHF, 0x24);
  227. }
  228. return 0;
  229. }
  230. static int sn95031_dmic12_event(struct snd_soc_dapm_widget *w,
  231. struct snd_kcontrol *k, int event)
  232. {
  233. unsigned int ldo = 0, clk_dir = 0, data_dir = 0;
  234. if (SND_SOC_DAPM_EVENT_ON(event)) {
  235. ldo = BIT(5)|BIT(4);
  236. clk_dir = BIT(0);
  237. data_dir = BIT(7);
  238. }
  239. /* program DMIC LDO, clock and set clock */
  240. snd_soc_update_bits(w->codec, SN95031_MICBIAS, BIT(5)|BIT(4), ldo);
  241. snd_soc_update_bits(w->codec, SN95031_DMICBUF0123, BIT(0), clk_dir);
  242. snd_soc_update_bits(w->codec, SN95031_DMICBUF0123, BIT(7), data_dir);
  243. return 0;
  244. }
  245. static int sn95031_dmic34_event(struct snd_soc_dapm_widget *w,
  246. struct snd_kcontrol *k, int event)
  247. {
  248. unsigned int ldo = 0, clk_dir = 0, data_dir = 0;
  249. if (SND_SOC_DAPM_EVENT_ON(event)) {
  250. ldo = BIT(5)|BIT(4);
  251. clk_dir = BIT(2);
  252. data_dir = BIT(1);
  253. }
  254. /* program DMIC LDO, clock and set clock */
  255. snd_soc_update_bits(w->codec, SN95031_MICBIAS, BIT(5)|BIT(4), ldo);
  256. snd_soc_update_bits(w->codec, SN95031_DMICBUF0123, BIT(2), clk_dir);
  257. snd_soc_update_bits(w->codec, SN95031_DMICBUF45, BIT(1), data_dir);
  258. return 0;
  259. }
  260. static int sn95031_dmic56_event(struct snd_soc_dapm_widget *w,
  261. struct snd_kcontrol *k, int event)
  262. {
  263. unsigned int ldo = 0;
  264. if (SND_SOC_DAPM_EVENT_ON(event))
  265. ldo = BIT(7)|BIT(6);
  266. /* program DMIC LDO */
  267. snd_soc_update_bits(w->codec, SN95031_MICBIAS, BIT(7)|BIT(6), ldo);
  268. return 0;
  269. }
  270. /* mux controls */
  271. static const char *sn95031_mic_texts[] = { "AMIC", "LineIn" };
  272. static const struct soc_enum sn95031_micl_enum =
  273. SOC_ENUM_SINGLE(SN95031_ADCCONFIG, 1, 2, sn95031_mic_texts);
  274. static const struct snd_kcontrol_new sn95031_micl_mux_control =
  275. SOC_DAPM_ENUM("Route", sn95031_micl_enum);
  276. static const struct soc_enum sn95031_micr_enum =
  277. SOC_ENUM_SINGLE(SN95031_ADCCONFIG, 3, 2, sn95031_mic_texts);
  278. static const struct snd_kcontrol_new sn95031_micr_mux_control =
  279. SOC_DAPM_ENUM("Route", sn95031_micr_enum);
  280. static const char *sn95031_input_texts[] = { "DMIC1", "DMIC2", "DMIC3",
  281. "DMIC4", "DMIC5", "DMIC6",
  282. "ADC Left", "ADC Right" };
  283. static const struct soc_enum sn95031_input1_enum =
  284. SOC_ENUM_SINGLE(SN95031_AUDIOMUX12, 0, 8, sn95031_input_texts);
  285. static const struct snd_kcontrol_new sn95031_input1_mux_control =
  286. SOC_DAPM_ENUM("Route", sn95031_input1_enum);
  287. static const struct soc_enum sn95031_input2_enum =
  288. SOC_ENUM_SINGLE(SN95031_AUDIOMUX12, 4, 8, sn95031_input_texts);
  289. static const struct snd_kcontrol_new sn95031_input2_mux_control =
  290. SOC_DAPM_ENUM("Route", sn95031_input2_enum);
  291. static const struct soc_enum sn95031_input3_enum =
  292. SOC_ENUM_SINGLE(SN95031_AUDIOMUX34, 0, 8, sn95031_input_texts);
  293. static const struct snd_kcontrol_new sn95031_input3_mux_control =
  294. SOC_DAPM_ENUM("Route", sn95031_input3_enum);
  295. static const struct soc_enum sn95031_input4_enum =
  296. SOC_ENUM_SINGLE(SN95031_AUDIOMUX34, 4, 8, sn95031_input_texts);
  297. static const struct snd_kcontrol_new sn95031_input4_mux_control =
  298. SOC_DAPM_ENUM("Route", sn95031_input4_enum);
  299. /* capture path controls */
  300. static const char *sn95031_micmode_text[] = {"Single Ended", "Differential"};
  301. /* 0dB to 30dB in 10dB steps */
  302. static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 10, 0);
  303. static const struct soc_enum sn95031_micmode1_enum =
  304. SOC_ENUM_SINGLE(SN95031_MICAMP1, 1, 2, sn95031_micmode_text);
  305. static const struct soc_enum sn95031_micmode2_enum =
  306. SOC_ENUM_SINGLE(SN95031_MICAMP2, 1, 2, sn95031_micmode_text);
  307. static const char *sn95031_dmic_cfg_text[] = {"GPO", "DMIC"};
  308. static const struct soc_enum sn95031_dmic12_cfg_enum =
  309. SOC_ENUM_SINGLE(SN95031_DMICMUX, 0, 2, sn95031_dmic_cfg_text);
  310. static const struct soc_enum sn95031_dmic34_cfg_enum =
  311. SOC_ENUM_SINGLE(SN95031_DMICMUX, 1, 2, sn95031_dmic_cfg_text);
  312. static const struct soc_enum sn95031_dmic56_cfg_enum =
  313. SOC_ENUM_SINGLE(SN95031_DMICMUX, 2, 2, sn95031_dmic_cfg_text);
  314. static const struct snd_kcontrol_new sn95031_snd_controls[] = {
  315. SOC_ENUM("Mic1Mode Capture Route", sn95031_micmode1_enum),
  316. SOC_ENUM("Mic2Mode Capture Route", sn95031_micmode2_enum),
  317. SOC_ENUM("DMIC12 Capture Route", sn95031_dmic12_cfg_enum),
  318. SOC_ENUM("DMIC34 Capture Route", sn95031_dmic34_cfg_enum),
  319. SOC_ENUM("DMIC56 Capture Route", sn95031_dmic56_cfg_enum),
  320. SOC_SINGLE_TLV("Mic1 Capture Volume", SN95031_MICAMP1,
  321. 2, 4, 0, mic_tlv),
  322. SOC_SINGLE_TLV("Mic2 Capture Volume", SN95031_MICAMP2,
  323. 2, 4, 0, mic_tlv),
  324. };
  325. /* DAPM widgets */
  326. static const struct snd_soc_dapm_widget sn95031_dapm_widgets[] = {
  327. /* all end points mic, hs etc */
  328. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  329. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  330. SND_SOC_DAPM_OUTPUT("EPOUT"),
  331. SND_SOC_DAPM_OUTPUT("IHFOUTL"),
  332. SND_SOC_DAPM_OUTPUT("IHFOUTR"),
  333. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  334. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  335. SND_SOC_DAPM_OUTPUT("VIB1OUT"),
  336. SND_SOC_DAPM_OUTPUT("VIB2OUT"),
  337. SND_SOC_DAPM_INPUT("AMIC1"), /* headset mic */
  338. SND_SOC_DAPM_INPUT("AMIC2"),
  339. SND_SOC_DAPM_INPUT("DMIC1"),
  340. SND_SOC_DAPM_INPUT("DMIC2"),
  341. SND_SOC_DAPM_INPUT("DMIC3"),
  342. SND_SOC_DAPM_INPUT("DMIC4"),
  343. SND_SOC_DAPM_INPUT("DMIC5"),
  344. SND_SOC_DAPM_INPUT("DMIC6"),
  345. SND_SOC_DAPM_INPUT("LINEINL"),
  346. SND_SOC_DAPM_INPUT("LINEINR"),
  347. SND_SOC_DAPM_MICBIAS("AMIC1Bias", SN95031_MICBIAS, 2, 0),
  348. SND_SOC_DAPM_MICBIAS("AMIC2Bias", SN95031_MICBIAS, 3, 0),
  349. SND_SOC_DAPM_MICBIAS("DMIC12Bias", SN95031_DMICMUX, 3, 0),
  350. SND_SOC_DAPM_MICBIAS("DMIC34Bias", SN95031_DMICMUX, 4, 0),
  351. SND_SOC_DAPM_MICBIAS("DMIC56Bias", SN95031_DMICMUX, 5, 0),
  352. SND_SOC_DAPM_SUPPLY("DMIC12supply", SN95031_DMICLK, 0, 0,
  353. sn95031_dmic12_event,
  354. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  355. SND_SOC_DAPM_SUPPLY("DMIC34supply", SN95031_DMICLK, 1, 0,
  356. sn95031_dmic34_event,
  357. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  358. SND_SOC_DAPM_SUPPLY("DMIC56supply", SN95031_DMICLK, 2, 0,
  359. sn95031_dmic56_event,
  360. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  361. SND_SOC_DAPM_AIF_OUT("PCM_Out", "Capture", 0,
  362. SND_SOC_NOPM, 0, 0),
  363. SND_SOC_DAPM_SUPPLY("Headset Rail", SND_SOC_NOPM, 0, 0,
  364. sn95031_vhs_event,
  365. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  366. SND_SOC_DAPM_SUPPLY("Speaker Rail", SND_SOC_NOPM, 0, 0,
  367. sn95031_vihf_event,
  368. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  369. /* playback path driver enables */
  370. SND_SOC_DAPM_PGA("Headset Left Playback",
  371. SN95031_DRIVEREN, 0, 0, NULL, 0),
  372. SND_SOC_DAPM_PGA("Headset Right Playback",
  373. SN95031_DRIVEREN, 1, 0, NULL, 0),
  374. SND_SOC_DAPM_PGA("Speaker Left Playback",
  375. SN95031_DRIVEREN, 2, 0, NULL, 0),
  376. SND_SOC_DAPM_PGA("Speaker Right Playback",
  377. SN95031_DRIVEREN, 3, 0, NULL, 0),
  378. SND_SOC_DAPM_PGA("Vibra1 Playback",
  379. SN95031_DRIVEREN, 4, 0, NULL, 0),
  380. SND_SOC_DAPM_PGA("Vibra2 Playback",
  381. SN95031_DRIVEREN, 5, 0, NULL, 0),
  382. SND_SOC_DAPM_PGA("Earpiece Playback",
  383. SN95031_DRIVEREN, 6, 0, NULL, 0),
  384. SND_SOC_DAPM_PGA("Lineout Left Playback",
  385. SN95031_LOCTL, 0, 0, NULL, 0),
  386. SND_SOC_DAPM_PGA("Lineout Right Playback",
  387. SN95031_LOCTL, 4, 0, NULL, 0),
  388. /* playback path filter enable */
  389. SND_SOC_DAPM_PGA("Headset Left Filter",
  390. SN95031_HSEPRXCTRL, 4, 0, NULL, 0),
  391. SND_SOC_DAPM_PGA("Headset Right Filter",
  392. SN95031_HSEPRXCTRL, 5, 0, NULL, 0),
  393. SND_SOC_DAPM_PGA("Speaker Left Filter",
  394. SN95031_IHFRXCTRL, 0, 0, NULL, 0),
  395. SND_SOC_DAPM_PGA("Speaker Right Filter",
  396. SN95031_IHFRXCTRL, 1, 0, NULL, 0),
  397. /* DACs */
  398. SND_SOC_DAPM_DAC("HSDAC Left", "Headset",
  399. SN95031_DACCONFIG, 0, 0),
  400. SND_SOC_DAPM_DAC("HSDAC Right", "Headset",
  401. SN95031_DACCONFIG, 1, 0),
  402. SND_SOC_DAPM_DAC("IHFDAC Left", "Speaker",
  403. SN95031_DACCONFIG, 2, 0),
  404. SND_SOC_DAPM_DAC("IHFDAC Right", "Speaker",
  405. SN95031_DACCONFIG, 3, 0),
  406. SND_SOC_DAPM_DAC("Vibra1 DAC", "Vibra1",
  407. SN95031_VIB1C5, 1, 0),
  408. SND_SOC_DAPM_DAC("Vibra2 DAC", "Vibra2",
  409. SN95031_VIB2C5, 1, 0),
  410. /* capture widgets */
  411. SND_SOC_DAPM_PGA("LineIn Enable Left", SN95031_MICAMP1,
  412. 7, 0, NULL, 0),
  413. SND_SOC_DAPM_PGA("LineIn Enable Right", SN95031_MICAMP2,
  414. 7, 0, NULL, 0),
  415. SND_SOC_DAPM_PGA("MIC1 Enable", SN95031_MICAMP1, 0, 0, NULL, 0),
  416. SND_SOC_DAPM_PGA("MIC2 Enable", SN95031_MICAMP2, 0, 0, NULL, 0),
  417. SND_SOC_DAPM_PGA("TX1 Enable", SN95031_AUDIOTXEN, 2, 0, NULL, 0),
  418. SND_SOC_DAPM_PGA("TX2 Enable", SN95031_AUDIOTXEN, 3, 0, NULL, 0),
  419. SND_SOC_DAPM_PGA("TX3 Enable", SN95031_AUDIOTXEN, 4, 0, NULL, 0),
  420. SND_SOC_DAPM_PGA("TX4 Enable", SN95031_AUDIOTXEN, 5, 0, NULL, 0),
  421. /* ADC have null stream as they will be turned ON by TX path */
  422. SND_SOC_DAPM_ADC("ADC Left", NULL,
  423. SN95031_ADCCONFIG, 0, 0),
  424. SND_SOC_DAPM_ADC("ADC Right", NULL,
  425. SN95031_ADCCONFIG, 2, 0),
  426. SND_SOC_DAPM_MUX("Mic_InputL Capture Route",
  427. SND_SOC_NOPM, 0, 0, &sn95031_micl_mux_control),
  428. SND_SOC_DAPM_MUX("Mic_InputR Capture Route",
  429. SND_SOC_NOPM, 0, 0, &sn95031_micr_mux_control),
  430. SND_SOC_DAPM_MUX("Txpath1 Capture Route",
  431. SND_SOC_NOPM, 0, 0, &sn95031_input1_mux_control),
  432. SND_SOC_DAPM_MUX("Txpath2 Capture Route",
  433. SND_SOC_NOPM, 0, 0, &sn95031_input2_mux_control),
  434. SND_SOC_DAPM_MUX("Txpath3 Capture Route",
  435. SND_SOC_NOPM, 0, 0, &sn95031_input3_mux_control),
  436. SND_SOC_DAPM_MUX("Txpath4 Capture Route",
  437. SND_SOC_NOPM, 0, 0, &sn95031_input4_mux_control),
  438. };
  439. static const struct snd_soc_dapm_route sn95031_audio_map[] = {
  440. /* headset and earpiece map */
  441. { "HPOUTL", NULL, "Headset Rail"},
  442. { "HPOUTR", NULL, "Headset Rail"},
  443. { "HPOUTL", NULL, "Headset Left Playback" },
  444. { "HPOUTR", NULL, "Headset Right Playback" },
  445. { "EPOUT", NULL, "Earpiece Playback" },
  446. { "Headset Left Playback", NULL, "Headset Left Filter"},
  447. { "Headset Right Playback", NULL, "Headset Right Filter"},
  448. { "Earpiece Playback", NULL, "Headset Left Filter"},
  449. { "Headset Left Filter", NULL, "HSDAC Left"},
  450. { "Headset Right Filter", NULL, "HSDAC Right"},
  451. /* speaker map */
  452. { "IHFOUTL", NULL, "Speaker Rail"},
  453. { "IHFOUTR", NULL, "Speaker Rail"},
  454. { "IHFOUTL", "NULL", "Speaker Left Playback"},
  455. { "IHFOUTR", "NULL", "Speaker Right Playback"},
  456. { "Speaker Left Playback", NULL, "Speaker Left Filter"},
  457. { "Speaker Right Playback", NULL, "Speaker Right Filter"},
  458. { "Speaker Left Filter", NULL, "IHFDAC Left"},
  459. { "Speaker Right Filter", NULL, "IHFDAC Right"},
  460. /* vibra map */
  461. { "VIB1OUT", NULL, "Vibra1 Playback"},
  462. { "Vibra1 Playback", NULL, "Vibra1 DAC"},
  463. { "VIB2OUT", NULL, "Vibra2 Playback"},
  464. { "Vibra2 Playback", NULL, "Vibra2 DAC"},
  465. /* lineout */
  466. { "LINEOUTL", NULL, "Lineout Left Playback"},
  467. { "LINEOUTR", NULL, "Lineout Right Playback"},
  468. { "Lineout Left Playback", NULL, "Headset Left Filter"},
  469. { "Lineout Left Playback", NULL, "Speaker Left Filter"},
  470. { "Lineout Left Playback", NULL, "Vibra1 DAC"},
  471. { "Lineout Right Playback", NULL, "Headset Right Filter"},
  472. { "Lineout Right Playback", NULL, "Speaker Right Filter"},
  473. { "Lineout Right Playback", NULL, "Vibra2 DAC"},
  474. /* Headset (AMIC1) mic */
  475. { "AMIC1Bias", NULL, "AMIC1"},
  476. { "MIC1 Enable", NULL, "AMIC1Bias"},
  477. { "Mic_InputL Capture Route", "AMIC", "MIC1 Enable"},
  478. /* AMIC2 */
  479. { "AMIC2Bias", NULL, "AMIC2"},
  480. { "MIC2 Enable", NULL, "AMIC2Bias"},
  481. { "Mic_InputR Capture Route", "AMIC", "MIC2 Enable"},
  482. /* Linein */
  483. { "LineIn Enable Left", NULL, "LINEINL"},
  484. { "LineIn Enable Right", NULL, "LINEINR"},
  485. { "Mic_InputL Capture Route", "LineIn", "LineIn Enable Left"},
  486. { "Mic_InputR Capture Route", "LineIn", "LineIn Enable Right"},
  487. /* ADC connection */
  488. { "ADC Left", NULL, "Mic_InputL Capture Route"},
  489. { "ADC Right", NULL, "Mic_InputR Capture Route"},
  490. /*DMIC connections */
  491. { "DMIC1", NULL, "DMIC12supply"},
  492. { "DMIC2", NULL, "DMIC12supply"},
  493. { "DMIC3", NULL, "DMIC34supply"},
  494. { "DMIC4", NULL, "DMIC34supply"},
  495. { "DMIC5", NULL, "DMIC56supply"},
  496. { "DMIC6", NULL, "DMIC56supply"},
  497. { "DMIC12Bias", NULL, "DMIC1"},
  498. { "DMIC12Bias", NULL, "DMIC2"},
  499. { "DMIC34Bias", NULL, "DMIC3"},
  500. { "DMIC34Bias", NULL, "DMIC4"},
  501. { "DMIC56Bias", NULL, "DMIC5"},
  502. { "DMIC56Bias", NULL, "DMIC6"},
  503. /*TX path inputs*/
  504. { "Txpath1 Capture Route", "ADC Left", "ADC Left"},
  505. { "Txpath2 Capture Route", "ADC Left", "ADC Left"},
  506. { "Txpath3 Capture Route", "ADC Left", "ADC Left"},
  507. { "Txpath4 Capture Route", "ADC Left", "ADC Left"},
  508. { "Txpath1 Capture Route", "ADC Right", "ADC Right"},
  509. { "Txpath2 Capture Route", "ADC Right", "ADC Right"},
  510. { "Txpath3 Capture Route", "ADC Right", "ADC Right"},
  511. { "Txpath4 Capture Route", "ADC Right", "ADC Right"},
  512. { "Txpath1 Capture Route", "DMIC1", "DMIC1"},
  513. { "Txpath2 Capture Route", "DMIC1", "DMIC1"},
  514. { "Txpath3 Capture Route", "DMIC1", "DMIC1"},
  515. { "Txpath4 Capture Route", "DMIC1", "DMIC1"},
  516. { "Txpath1 Capture Route", "DMIC2", "DMIC2"},
  517. { "Txpath2 Capture Route", "DMIC2", "DMIC2"},
  518. { "Txpath3 Capture Route", "DMIC2", "DMIC2"},
  519. { "Txpath4 Capture Route", "DMIC2", "DMIC2"},
  520. { "Txpath1 Capture Route", "DMIC3", "DMIC3"},
  521. { "Txpath2 Capture Route", "DMIC3", "DMIC3"},
  522. { "Txpath3 Capture Route", "DMIC3", "DMIC3"},
  523. { "Txpath4 Capture Route", "DMIC3", "DMIC3"},
  524. { "Txpath1 Capture Route", "DMIC4", "DMIC4"},
  525. { "Txpath2 Capture Route", "DMIC4", "DMIC4"},
  526. { "Txpath3 Capture Route", "DMIC4", "DMIC4"},
  527. { "Txpath4 Capture Route", "DMIC4", "DMIC4"},
  528. { "Txpath1 Capture Route", "DMIC5", "DMIC5"},
  529. { "Txpath2 Capture Route", "DMIC5", "DMIC5"},
  530. { "Txpath3 Capture Route", "DMIC5", "DMIC5"},
  531. { "Txpath4 Capture Route", "DMIC5", "DMIC5"},
  532. { "Txpath1 Capture Route", "DMIC6", "DMIC6"},
  533. { "Txpath2 Capture Route", "DMIC6", "DMIC6"},
  534. { "Txpath3 Capture Route", "DMIC6", "DMIC6"},
  535. { "Txpath4 Capture Route", "DMIC6", "DMIC6"},
  536. /* tx path */
  537. { "TX1 Enable", NULL, "Txpath1 Capture Route"},
  538. { "TX2 Enable", NULL, "Txpath2 Capture Route"},
  539. { "TX3 Enable", NULL, "Txpath3 Capture Route"},
  540. { "TX4 Enable", NULL, "Txpath4 Capture Route"},
  541. { "PCM_Out", NULL, "TX1 Enable"},
  542. { "PCM_Out", NULL, "TX2 Enable"},
  543. { "PCM_Out", NULL, "TX3 Enable"},
  544. { "PCM_Out", NULL, "TX4 Enable"},
  545. };
  546. /* speaker and headset mutes, for audio pops and clicks */
  547. static int sn95031_pcm_hs_mute(struct snd_soc_dai *dai, int mute)
  548. {
  549. snd_soc_update_bits(dai->codec,
  550. SN95031_HSLVOLCTRL, BIT(7), (!mute << 7));
  551. snd_soc_update_bits(dai->codec,
  552. SN95031_HSRVOLCTRL, BIT(7), (!mute << 7));
  553. return 0;
  554. }
  555. static int sn95031_pcm_spkr_mute(struct snd_soc_dai *dai, int mute)
  556. {
  557. snd_soc_update_bits(dai->codec,
  558. SN95031_IHFLVOLCTRL, BIT(7), (!mute << 7));
  559. snd_soc_update_bits(dai->codec,
  560. SN95031_IHFRVOLCTRL, BIT(7), (!mute << 7));
  561. return 0;
  562. }
  563. static int sn95031_pcm_hw_params(struct snd_pcm_substream *substream,
  564. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  565. {
  566. unsigned int format, rate;
  567. switch (params_format(params)) {
  568. case SNDRV_PCM_FORMAT_S16_LE:
  569. format = BIT(4)|BIT(5);
  570. break;
  571. case SNDRV_PCM_FORMAT_S24_LE:
  572. format = 0;
  573. break;
  574. default:
  575. return -EINVAL;
  576. }
  577. snd_soc_update_bits(dai->codec, SN95031_PCM2C2,
  578. BIT(4)|BIT(5), format);
  579. switch (params_rate(params)) {
  580. case 48000:
  581. pr_debug("RATE_48000\n");
  582. rate = 0;
  583. break;
  584. case 44100:
  585. pr_debug("RATE_44100\n");
  586. rate = BIT(7);
  587. break;
  588. default:
  589. pr_err("ERR rate %d\n", params_rate(params));
  590. return -EINVAL;
  591. }
  592. snd_soc_update_bits(dai->codec, SN95031_PCM1C1, BIT(7), rate);
  593. return 0;
  594. }
  595. /* Codec DAI section */
  596. static const struct snd_soc_dai_ops sn95031_headset_dai_ops = {
  597. .digital_mute = sn95031_pcm_hs_mute,
  598. .hw_params = sn95031_pcm_hw_params,
  599. };
  600. static const struct snd_soc_dai_ops sn95031_speaker_dai_ops = {
  601. .digital_mute = sn95031_pcm_spkr_mute,
  602. .hw_params = sn95031_pcm_hw_params,
  603. };
  604. static const struct snd_soc_dai_ops sn95031_vib1_dai_ops = {
  605. .hw_params = sn95031_pcm_hw_params,
  606. };
  607. static const struct snd_soc_dai_ops sn95031_vib2_dai_ops = {
  608. .hw_params = sn95031_pcm_hw_params,
  609. };
  610. static struct snd_soc_dai_driver sn95031_dais[] = {
  611. {
  612. .name = "SN95031 Headset",
  613. .playback = {
  614. .stream_name = "Headset",
  615. .channels_min = 2,
  616. .channels_max = 2,
  617. .rates = SN95031_RATES,
  618. .formats = SN95031_FORMATS,
  619. },
  620. .capture = {
  621. .stream_name = "Capture",
  622. .channels_min = 1,
  623. .channels_max = 5,
  624. .rates = SN95031_RATES,
  625. .formats = SN95031_FORMATS,
  626. },
  627. .ops = &sn95031_headset_dai_ops,
  628. },
  629. { .name = "SN95031 Speaker",
  630. .playback = {
  631. .stream_name = "Speaker",
  632. .channels_min = 2,
  633. .channels_max = 2,
  634. .rates = SN95031_RATES,
  635. .formats = SN95031_FORMATS,
  636. },
  637. .ops = &sn95031_speaker_dai_ops,
  638. },
  639. { .name = "SN95031 Vibra1",
  640. .playback = {
  641. .stream_name = "Vibra1",
  642. .channels_min = 1,
  643. .channels_max = 1,
  644. .rates = SN95031_RATES,
  645. .formats = SN95031_FORMATS,
  646. },
  647. .ops = &sn95031_vib1_dai_ops,
  648. },
  649. { .name = "SN95031 Vibra2",
  650. .playback = {
  651. .stream_name = "Vibra2",
  652. .channels_min = 1,
  653. .channels_max = 1,
  654. .rates = SN95031_RATES,
  655. .formats = SN95031_FORMATS,
  656. },
  657. .ops = &sn95031_vib2_dai_ops,
  658. },
  659. };
  660. static inline void sn95031_disable_jack_btn(struct snd_soc_codec *codec)
  661. {
  662. snd_soc_write(codec, SN95031_BTNCTRL2, 0x00);
  663. }
  664. static inline void sn95031_enable_jack_btn(struct snd_soc_codec *codec)
  665. {
  666. snd_soc_write(codec, SN95031_BTNCTRL1, 0x77);
  667. snd_soc_write(codec, SN95031_BTNCTRL2, 0x01);
  668. }
  669. static int sn95031_get_headset_state(struct snd_soc_jack *mfld_jack)
  670. {
  671. int micbias = sn95031_get_mic_bias(mfld_jack->codec);
  672. int jack_type = snd_soc_jack_get_type(mfld_jack, micbias);
  673. pr_debug("jack type detected = %d\n", jack_type);
  674. if (jack_type == SND_JACK_HEADSET)
  675. sn95031_enable_jack_btn(mfld_jack->codec);
  676. return jack_type;
  677. }
  678. void sn95031_jack_detection(struct mfld_jack_data *jack_data)
  679. {
  680. unsigned int status;
  681. unsigned int mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_HEADSET;
  682. pr_debug("interrupt id read in sram = 0x%x\n", jack_data->intr_id);
  683. if (jack_data->intr_id & 0x1) {
  684. pr_debug("short_push detected\n");
  685. status = SND_JACK_HEADSET | SND_JACK_BTN_0;
  686. } else if (jack_data->intr_id & 0x2) {
  687. pr_debug("long_push detected\n");
  688. status = SND_JACK_HEADSET | SND_JACK_BTN_1;
  689. } else if (jack_data->intr_id & 0x4) {
  690. pr_debug("headset or headphones inserted\n");
  691. status = sn95031_get_headset_state(jack_data->mfld_jack);
  692. } else if (jack_data->intr_id & 0x8) {
  693. pr_debug("headset or headphones removed\n");
  694. status = 0;
  695. sn95031_disable_jack_btn(jack_data->mfld_jack->codec);
  696. } else {
  697. pr_err("unidentified interrupt\n");
  698. return;
  699. }
  700. snd_soc_jack_report(jack_data->mfld_jack, status, mask);
  701. /*button pressed and released so we send explicit button release */
  702. if ((status & SND_JACK_BTN_0) | (status & SND_JACK_BTN_1))
  703. snd_soc_jack_report(jack_data->mfld_jack,
  704. SND_JACK_HEADSET, mask);
  705. }
  706. EXPORT_SYMBOL_GPL(sn95031_jack_detection);
  707. /* codec registration */
  708. static int sn95031_codec_probe(struct snd_soc_codec *codec)
  709. {
  710. pr_debug("codec_probe called\n");
  711. /* PCM interface config
  712. * This sets the pcm rx slot conguration to max 6 slots
  713. * for max 4 dais (2 stereo and 2 mono)
  714. */
  715. snd_soc_write(codec, SN95031_PCM2RXSLOT01, 0x10);
  716. snd_soc_write(codec, SN95031_PCM2RXSLOT23, 0x32);
  717. snd_soc_write(codec, SN95031_PCM2RXSLOT45, 0x54);
  718. snd_soc_write(codec, SN95031_PCM2TXSLOT01, 0x10);
  719. snd_soc_write(codec, SN95031_PCM2TXSLOT23, 0x32);
  720. /* pcm port setting
  721. * This sets the pcm port to slave and clock at 19.2Mhz which
  722. * can support 6slots, sampling rate set per stream in hw-params
  723. */
  724. snd_soc_write(codec, SN95031_PCM1C1, 0x00);
  725. snd_soc_write(codec, SN95031_PCM2C1, 0x01);
  726. snd_soc_write(codec, SN95031_PCM2C2, 0x0A);
  727. snd_soc_write(codec, SN95031_HSMIXER, BIT(0)|BIT(4));
  728. /* vendor vibra workround, the vibras are muted by
  729. * custom register so unmute them
  730. */
  731. snd_soc_write(codec, SN95031_SSR5, 0x80);
  732. snd_soc_write(codec, SN95031_SSR6, 0x80);
  733. snd_soc_write(codec, SN95031_VIB1C5, 0x00);
  734. snd_soc_write(codec, SN95031_VIB2C5, 0x00);
  735. /* configure vibras for pcm port */
  736. snd_soc_write(codec, SN95031_VIB1C3, 0x00);
  737. snd_soc_write(codec, SN95031_VIB2C3, 0x00);
  738. /* soft mute ramp time */
  739. snd_soc_write(codec, SN95031_SOFTMUTE, 0x3);
  740. /* fix the initial volume at 1dB,
  741. * default in +9dB,
  742. * 1dB give optimal swing on DAC, amps
  743. */
  744. snd_soc_write(codec, SN95031_HSLVOLCTRL, 0x08);
  745. snd_soc_write(codec, SN95031_HSRVOLCTRL, 0x08);
  746. snd_soc_write(codec, SN95031_IHFLVOLCTRL, 0x08);
  747. snd_soc_write(codec, SN95031_IHFRVOLCTRL, 0x08);
  748. /* dac mode and lineout workaround */
  749. snd_soc_write(codec, SN95031_SSR2, 0x10);
  750. snd_soc_write(codec, SN95031_SSR3, 0x40);
  751. snd_soc_add_codec_controls(codec, sn95031_snd_controls,
  752. ARRAY_SIZE(sn95031_snd_controls));
  753. return 0;
  754. }
  755. static int sn95031_codec_remove(struct snd_soc_codec *codec)
  756. {
  757. pr_debug("codec_remove called\n");
  758. sn95031_set_vaud_bias(codec, SND_SOC_BIAS_OFF);
  759. return 0;
  760. }
  761. struct snd_soc_codec_driver sn95031_codec = {
  762. .probe = sn95031_codec_probe,
  763. .remove = sn95031_codec_remove,
  764. .read = sn95031_read,
  765. .write = sn95031_write,
  766. .set_bias_level = sn95031_set_vaud_bias,
  767. .idle_bias_off = true,
  768. .dapm_widgets = sn95031_dapm_widgets,
  769. .num_dapm_widgets = ARRAY_SIZE(sn95031_dapm_widgets),
  770. .dapm_routes = sn95031_audio_map,
  771. .num_dapm_routes = ARRAY_SIZE(sn95031_audio_map),
  772. };
  773. static int __devinit sn95031_device_probe(struct platform_device *pdev)
  774. {
  775. pr_debug("codec device probe called for %s\n", dev_name(&pdev->dev));
  776. return snd_soc_register_codec(&pdev->dev, &sn95031_codec,
  777. sn95031_dais, ARRAY_SIZE(sn95031_dais));
  778. }
  779. static int __devexit sn95031_device_remove(struct platform_device *pdev)
  780. {
  781. pr_debug("codec device remove called\n");
  782. snd_soc_unregister_codec(&pdev->dev);
  783. return 0;
  784. }
  785. static struct platform_driver sn95031_codec_driver = {
  786. .driver = {
  787. .name = "sn95031",
  788. .owner = THIS_MODULE,
  789. },
  790. .probe = sn95031_device_probe,
  791. .remove = __devexit_p(sn95031_device_remove),
  792. };
  793. module_platform_driver(sn95031_codec_driver);
  794. MODULE_DESCRIPTION("ASoC TI SN95031 codec driver");
  795. MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
  796. MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
  797. MODULE_LICENSE("GPL v2");
  798. MODULE_ALIAS("platform:sn95031");