sn95031.c 29 KB

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