twl4030.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ALSA SoC TWL4030 codec driver
  4. *
  5. * Author: Steve Sakoman, <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/init.h>
  10. #include <linux/delay.h>
  11. #include <linux/pm.h>
  12. #include <linux/i2c.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/of.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/mfd/twl.h>
  17. #include <linux/slab.h>
  18. #include <linux/gpio.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #include <sound/initval.h>
  24. #include <sound/tlv.h>
  25. /* Register descriptions are here */
  26. #include <linux/mfd/twl4030-audio.h>
  27. /* TWL4030 PMBR1 Register */
  28. #define TWL4030_PMBR1_REG 0x0D
  29. /* TWL4030 PMBR1 Register GPIO6 mux bits */
  30. #define TWL4030_GPIO6_PWM0_MUTE(value) ((value & 0x03) << 2)
  31. #define TWL4030_CACHEREGNUM (TWL4030_REG_MISC_SET_2 + 1)
  32. struct twl4030_board_params {
  33. unsigned int digimic_delay; /* in ms */
  34. unsigned int ramp_delay_value;
  35. unsigned int offset_cncl_path;
  36. unsigned int hs_extmute:1;
  37. int hs_extmute_gpio;
  38. };
  39. /* codec private data */
  40. struct twl4030_priv {
  41. unsigned int codec_powered;
  42. /* reference counts of AIF/APLL users */
  43. unsigned int apll_enabled;
  44. struct snd_pcm_substream *master_substream;
  45. struct snd_pcm_substream *slave_substream;
  46. unsigned int configured;
  47. unsigned int rate;
  48. unsigned int sample_bits;
  49. unsigned int channels;
  50. unsigned int sysclk;
  51. /* Output (with associated amp) states */
  52. u8 hsl_enabled, hsr_enabled;
  53. u8 earpiece_enabled;
  54. u8 predrivel_enabled, predriver_enabled;
  55. u8 carkitl_enabled, carkitr_enabled;
  56. u8 ctl_cache[TWL4030_REG_PRECKR_CTL - TWL4030_REG_EAR_CTL + 1];
  57. struct twl4030_board_params *board_params;
  58. };
  59. static void tw4030_init_ctl_cache(struct twl4030_priv *twl4030)
  60. {
  61. int i;
  62. u8 byte;
  63. for (i = TWL4030_REG_EAR_CTL; i <= TWL4030_REG_PRECKR_CTL; i++) {
  64. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte, i);
  65. twl4030->ctl_cache[i - TWL4030_REG_EAR_CTL] = byte;
  66. }
  67. }
  68. static unsigned int twl4030_read(struct snd_soc_component *component, unsigned int reg)
  69. {
  70. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  71. u8 value = 0;
  72. if (reg >= TWL4030_CACHEREGNUM)
  73. return -EIO;
  74. switch (reg) {
  75. case TWL4030_REG_EAR_CTL:
  76. case TWL4030_REG_PREDL_CTL:
  77. case TWL4030_REG_PREDR_CTL:
  78. case TWL4030_REG_PRECKL_CTL:
  79. case TWL4030_REG_PRECKR_CTL:
  80. case TWL4030_REG_HS_GAIN_SET:
  81. value = twl4030->ctl_cache[reg - TWL4030_REG_EAR_CTL];
  82. break;
  83. default:
  84. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &value, reg);
  85. break;
  86. }
  87. return value;
  88. }
  89. static bool twl4030_can_write_to_chip(struct twl4030_priv *twl4030,
  90. unsigned int reg)
  91. {
  92. bool write_to_reg = false;
  93. /* Decide if the given register can be written */
  94. switch (reg) {
  95. case TWL4030_REG_EAR_CTL:
  96. if (twl4030->earpiece_enabled)
  97. write_to_reg = true;
  98. break;
  99. case TWL4030_REG_PREDL_CTL:
  100. if (twl4030->predrivel_enabled)
  101. write_to_reg = true;
  102. break;
  103. case TWL4030_REG_PREDR_CTL:
  104. if (twl4030->predriver_enabled)
  105. write_to_reg = true;
  106. break;
  107. case TWL4030_REG_PRECKL_CTL:
  108. if (twl4030->carkitl_enabled)
  109. write_to_reg = true;
  110. break;
  111. case TWL4030_REG_PRECKR_CTL:
  112. if (twl4030->carkitr_enabled)
  113. write_to_reg = true;
  114. break;
  115. case TWL4030_REG_HS_GAIN_SET:
  116. if (twl4030->hsl_enabled || twl4030->hsr_enabled)
  117. write_to_reg = true;
  118. break;
  119. default:
  120. /* All other register can be written */
  121. write_to_reg = true;
  122. break;
  123. }
  124. return write_to_reg;
  125. }
  126. static int twl4030_write(struct snd_soc_component *component, unsigned int reg,
  127. unsigned int value)
  128. {
  129. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  130. /* Update the ctl cache */
  131. switch (reg) {
  132. case TWL4030_REG_EAR_CTL:
  133. case TWL4030_REG_PREDL_CTL:
  134. case TWL4030_REG_PREDR_CTL:
  135. case TWL4030_REG_PRECKL_CTL:
  136. case TWL4030_REG_PRECKR_CTL:
  137. case TWL4030_REG_HS_GAIN_SET:
  138. twl4030->ctl_cache[reg - TWL4030_REG_EAR_CTL] = value;
  139. break;
  140. default:
  141. break;
  142. }
  143. if (twl4030_can_write_to_chip(twl4030, reg))
  144. return twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, value, reg);
  145. return 0;
  146. }
  147. static inline void twl4030_wait_ms(int time)
  148. {
  149. if (time < 60) {
  150. time *= 1000;
  151. usleep_range(time, time + 500);
  152. } else {
  153. msleep(time);
  154. }
  155. }
  156. static void twl4030_codec_enable(struct snd_soc_component *component, int enable)
  157. {
  158. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  159. int mode;
  160. if (enable == twl4030->codec_powered)
  161. return;
  162. if (enable)
  163. mode = twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER);
  164. else
  165. mode = twl4030_audio_disable_resource(TWL4030_AUDIO_RES_POWER);
  166. if (mode >= 0)
  167. twl4030->codec_powered = enable;
  168. /* REVISIT: this delay is present in TI sample drivers */
  169. /* but there seems to be no TRM requirement for it */
  170. udelay(10);
  171. }
  172. static void
  173. twl4030_get_board_param_values(struct twl4030_board_params *board_params,
  174. struct device_node *node)
  175. {
  176. int value;
  177. of_property_read_u32(node, "ti,digimic_delay", &board_params->digimic_delay);
  178. of_property_read_u32(node, "ti,ramp_delay_value", &board_params->ramp_delay_value);
  179. of_property_read_u32(node, "ti,offset_cncl_path", &board_params->offset_cncl_path);
  180. if (!of_property_read_u32(node, "ti,hs_extmute", &value))
  181. board_params->hs_extmute = value;
  182. board_params->hs_extmute_gpio = of_get_named_gpio(node, "ti,hs_extmute_gpio", 0);
  183. if (gpio_is_valid(board_params->hs_extmute_gpio))
  184. board_params->hs_extmute = 1;
  185. }
  186. static struct twl4030_board_params*
  187. twl4030_get_board_params(struct snd_soc_component *component)
  188. {
  189. struct twl4030_board_params *board_params = NULL;
  190. struct device_node *twl4030_codec_node = NULL;
  191. twl4030_codec_node = of_get_child_by_name(component->dev->parent->of_node,
  192. "codec");
  193. if (twl4030_codec_node) {
  194. board_params = devm_kzalloc(component->dev,
  195. sizeof(struct twl4030_board_params),
  196. GFP_KERNEL);
  197. if (!board_params) {
  198. of_node_put(twl4030_codec_node);
  199. return NULL;
  200. }
  201. twl4030_get_board_param_values(board_params, twl4030_codec_node);
  202. of_node_put(twl4030_codec_node);
  203. }
  204. return board_params;
  205. }
  206. static void twl4030_init_chip(struct snd_soc_component *component)
  207. {
  208. struct twl4030_board_params *board_params;
  209. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  210. u8 reg, byte;
  211. int i = 0;
  212. board_params = twl4030_get_board_params(component);
  213. if (board_params && board_params->hs_extmute) {
  214. if (gpio_is_valid(board_params->hs_extmute_gpio)) {
  215. int ret;
  216. if (!board_params->hs_extmute_gpio)
  217. dev_warn(component->dev,
  218. "Extmute GPIO is 0 is this correct?\n");
  219. ret = gpio_request_one(board_params->hs_extmute_gpio,
  220. GPIOF_OUT_INIT_LOW,
  221. "hs_extmute");
  222. if (ret) {
  223. dev_err(component->dev,
  224. "Failed to get hs_extmute GPIO\n");
  225. board_params->hs_extmute_gpio = -1;
  226. }
  227. } else {
  228. u8 pin_mux;
  229. /* Set TWL4030 GPIO6 as EXTMUTE signal */
  230. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &pin_mux,
  231. TWL4030_PMBR1_REG);
  232. pin_mux &= ~TWL4030_GPIO6_PWM0_MUTE(0x03);
  233. pin_mux |= TWL4030_GPIO6_PWM0_MUTE(0x02);
  234. twl_i2c_write_u8(TWL4030_MODULE_INTBR, pin_mux,
  235. TWL4030_PMBR1_REG);
  236. }
  237. }
  238. /* Initialize the local ctl register cache */
  239. tw4030_init_ctl_cache(twl4030);
  240. /* anti-pop when changing analog gain */
  241. reg = twl4030_read(component, TWL4030_REG_MISC_SET_1);
  242. twl4030_write(component, TWL4030_REG_MISC_SET_1,
  243. reg | TWL4030_SMOOTH_ANAVOL_EN);
  244. twl4030_write(component, TWL4030_REG_OPTION,
  245. TWL4030_ATXL1_EN | TWL4030_ATXR1_EN |
  246. TWL4030_ARXL2_EN | TWL4030_ARXR2_EN);
  247. /* REG_ARXR2_APGA_CTL reset according to the TRM: 0dB, DA_EN */
  248. twl4030_write(component, TWL4030_REG_ARXR2_APGA_CTL, 0x32);
  249. /* Machine dependent setup */
  250. if (!board_params)
  251. return;
  252. twl4030->board_params = board_params;
  253. reg = twl4030_read(component, TWL4030_REG_HS_POPN_SET);
  254. reg &= ~TWL4030_RAMP_DELAY;
  255. reg |= (board_params->ramp_delay_value << 2);
  256. twl4030_write(component, TWL4030_REG_HS_POPN_SET, reg);
  257. /* initiate offset cancellation */
  258. twl4030_codec_enable(component, 1);
  259. reg = twl4030_read(component, TWL4030_REG_ANAMICL);
  260. reg &= ~TWL4030_OFFSET_CNCL_SEL;
  261. reg |= board_params->offset_cncl_path;
  262. twl4030_write(component, TWL4030_REG_ANAMICL,
  263. reg | TWL4030_CNCL_OFFSET_START);
  264. /*
  265. * Wait for offset cancellation to complete.
  266. * Since this takes a while, do not slam the i2c.
  267. * Start polling the status after ~20ms.
  268. */
  269. msleep(20);
  270. do {
  271. usleep_range(1000, 2000);
  272. twl_set_regcache_bypass(TWL4030_MODULE_AUDIO_VOICE, true);
  273. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte,
  274. TWL4030_REG_ANAMICL);
  275. twl_set_regcache_bypass(TWL4030_MODULE_AUDIO_VOICE, false);
  276. } while ((i++ < 100) &&
  277. ((byte & TWL4030_CNCL_OFFSET_START) ==
  278. TWL4030_CNCL_OFFSET_START));
  279. twl4030_codec_enable(component, 0);
  280. }
  281. static void twl4030_apll_enable(struct snd_soc_component *component, int enable)
  282. {
  283. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  284. if (enable) {
  285. twl4030->apll_enabled++;
  286. if (twl4030->apll_enabled == 1)
  287. twl4030_audio_enable_resource(
  288. TWL4030_AUDIO_RES_APLL);
  289. } else {
  290. twl4030->apll_enabled--;
  291. if (!twl4030->apll_enabled)
  292. twl4030_audio_disable_resource(
  293. TWL4030_AUDIO_RES_APLL);
  294. }
  295. }
  296. /* Earpiece */
  297. static const struct snd_kcontrol_new twl4030_dapm_earpiece_controls[] = {
  298. SOC_DAPM_SINGLE("Voice", TWL4030_REG_EAR_CTL, 0, 1, 0),
  299. SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_EAR_CTL, 1, 1, 0),
  300. SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_EAR_CTL, 2, 1, 0),
  301. SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_EAR_CTL, 3, 1, 0),
  302. };
  303. /* PreDrive Left */
  304. static const struct snd_kcontrol_new twl4030_dapm_predrivel_controls[] = {
  305. SOC_DAPM_SINGLE("Voice", TWL4030_REG_PREDL_CTL, 0, 1, 0),
  306. SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_PREDL_CTL, 1, 1, 0),
  307. SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PREDL_CTL, 2, 1, 0),
  308. SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PREDL_CTL, 3, 1, 0),
  309. };
  310. /* PreDrive Right */
  311. static const struct snd_kcontrol_new twl4030_dapm_predriver_controls[] = {
  312. SOC_DAPM_SINGLE("Voice", TWL4030_REG_PREDR_CTL, 0, 1, 0),
  313. SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_PREDR_CTL, 1, 1, 0),
  314. SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PREDR_CTL, 2, 1, 0),
  315. SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PREDR_CTL, 3, 1, 0),
  316. };
  317. /* Headset Left */
  318. static const struct snd_kcontrol_new twl4030_dapm_hsol_controls[] = {
  319. SOC_DAPM_SINGLE("Voice", TWL4030_REG_HS_SEL, 0, 1, 0),
  320. SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_HS_SEL, 1, 1, 0),
  321. SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_HS_SEL, 2, 1, 0),
  322. };
  323. /* Headset Right */
  324. static const struct snd_kcontrol_new twl4030_dapm_hsor_controls[] = {
  325. SOC_DAPM_SINGLE("Voice", TWL4030_REG_HS_SEL, 3, 1, 0),
  326. SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_HS_SEL, 4, 1, 0),
  327. SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_HS_SEL, 5, 1, 0),
  328. };
  329. /* Carkit Left */
  330. static const struct snd_kcontrol_new twl4030_dapm_carkitl_controls[] = {
  331. SOC_DAPM_SINGLE("Voice", TWL4030_REG_PRECKL_CTL, 0, 1, 0),
  332. SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_PRECKL_CTL, 1, 1, 0),
  333. SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PRECKL_CTL, 2, 1, 0),
  334. };
  335. /* Carkit Right */
  336. static const struct snd_kcontrol_new twl4030_dapm_carkitr_controls[] = {
  337. SOC_DAPM_SINGLE("Voice", TWL4030_REG_PRECKR_CTL, 0, 1, 0),
  338. SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_PRECKR_CTL, 1, 1, 0),
  339. SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PRECKR_CTL, 2, 1, 0),
  340. };
  341. /* Handsfree Left */
  342. static const char *twl4030_handsfreel_texts[] =
  343. {"Voice", "AudioL1", "AudioL2", "AudioR2"};
  344. static SOC_ENUM_SINGLE_DECL(twl4030_handsfreel_enum,
  345. TWL4030_REG_HFL_CTL, 0,
  346. twl4030_handsfreel_texts);
  347. static const struct snd_kcontrol_new twl4030_dapm_handsfreel_control =
  348. SOC_DAPM_ENUM("Route", twl4030_handsfreel_enum);
  349. /* Handsfree Left virtual mute */
  350. static const struct snd_kcontrol_new twl4030_dapm_handsfreelmute_control =
  351. SOC_DAPM_SINGLE_VIRT("Switch", 1);
  352. /* Handsfree Right */
  353. static const char *twl4030_handsfreer_texts[] =
  354. {"Voice", "AudioR1", "AudioR2", "AudioL2"};
  355. static SOC_ENUM_SINGLE_DECL(twl4030_handsfreer_enum,
  356. TWL4030_REG_HFR_CTL, 0,
  357. twl4030_handsfreer_texts);
  358. static const struct snd_kcontrol_new twl4030_dapm_handsfreer_control =
  359. SOC_DAPM_ENUM("Route", twl4030_handsfreer_enum);
  360. /* Handsfree Right virtual mute */
  361. static const struct snd_kcontrol_new twl4030_dapm_handsfreermute_control =
  362. SOC_DAPM_SINGLE_VIRT("Switch", 1);
  363. /* Vibra */
  364. /* Vibra audio path selection */
  365. static const char *twl4030_vibra_texts[] =
  366. {"AudioL1", "AudioR1", "AudioL2", "AudioR2"};
  367. static SOC_ENUM_SINGLE_DECL(twl4030_vibra_enum,
  368. TWL4030_REG_VIBRA_CTL, 2,
  369. twl4030_vibra_texts);
  370. static const struct snd_kcontrol_new twl4030_dapm_vibra_control =
  371. SOC_DAPM_ENUM("Route", twl4030_vibra_enum);
  372. /* Vibra path selection: local vibrator (PWM) or audio driven */
  373. static const char *twl4030_vibrapath_texts[] =
  374. {"Local vibrator", "Audio"};
  375. static SOC_ENUM_SINGLE_DECL(twl4030_vibrapath_enum,
  376. TWL4030_REG_VIBRA_CTL, 4,
  377. twl4030_vibrapath_texts);
  378. static const struct snd_kcontrol_new twl4030_dapm_vibrapath_control =
  379. SOC_DAPM_ENUM("Route", twl4030_vibrapath_enum);
  380. /* Left analog microphone selection */
  381. static const struct snd_kcontrol_new twl4030_dapm_analoglmic_controls[] = {
  382. SOC_DAPM_SINGLE("Main Mic Capture Switch",
  383. TWL4030_REG_ANAMICL, 0, 1, 0),
  384. SOC_DAPM_SINGLE("Headset Mic Capture Switch",
  385. TWL4030_REG_ANAMICL, 1, 1, 0),
  386. SOC_DAPM_SINGLE("AUXL Capture Switch",
  387. TWL4030_REG_ANAMICL, 2, 1, 0),
  388. SOC_DAPM_SINGLE("Carkit Mic Capture Switch",
  389. TWL4030_REG_ANAMICL, 3, 1, 0),
  390. };
  391. /* Right analog microphone selection */
  392. static const struct snd_kcontrol_new twl4030_dapm_analogrmic_controls[] = {
  393. SOC_DAPM_SINGLE("Sub Mic Capture Switch", TWL4030_REG_ANAMICR, 0, 1, 0),
  394. SOC_DAPM_SINGLE("AUXR Capture Switch", TWL4030_REG_ANAMICR, 2, 1, 0),
  395. };
  396. /* TX1 L/R Analog/Digital microphone selection */
  397. static const char *twl4030_micpathtx1_texts[] =
  398. {"Analog", "Digimic0"};
  399. static SOC_ENUM_SINGLE_DECL(twl4030_micpathtx1_enum,
  400. TWL4030_REG_ADCMICSEL, 0,
  401. twl4030_micpathtx1_texts);
  402. static const struct snd_kcontrol_new twl4030_dapm_micpathtx1_control =
  403. SOC_DAPM_ENUM("Route", twl4030_micpathtx1_enum);
  404. /* TX2 L/R Analog/Digital microphone selection */
  405. static const char *twl4030_micpathtx2_texts[] =
  406. {"Analog", "Digimic1"};
  407. static SOC_ENUM_SINGLE_DECL(twl4030_micpathtx2_enum,
  408. TWL4030_REG_ADCMICSEL, 2,
  409. twl4030_micpathtx2_texts);
  410. static const struct snd_kcontrol_new twl4030_dapm_micpathtx2_control =
  411. SOC_DAPM_ENUM("Route", twl4030_micpathtx2_enum);
  412. /* Analog bypass for AudioR1 */
  413. static const struct snd_kcontrol_new twl4030_dapm_abypassr1_control =
  414. SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXR1_APGA_CTL, 2, 1, 0);
  415. /* Analog bypass for AudioL1 */
  416. static const struct snd_kcontrol_new twl4030_dapm_abypassl1_control =
  417. SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXL1_APGA_CTL, 2, 1, 0);
  418. /* Analog bypass for AudioR2 */
  419. static const struct snd_kcontrol_new twl4030_dapm_abypassr2_control =
  420. SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXR2_APGA_CTL, 2, 1, 0);
  421. /* Analog bypass for AudioL2 */
  422. static const struct snd_kcontrol_new twl4030_dapm_abypassl2_control =
  423. SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXL2_APGA_CTL, 2, 1, 0);
  424. /* Analog bypass for Voice */
  425. static const struct snd_kcontrol_new twl4030_dapm_abypassv_control =
  426. SOC_DAPM_SINGLE("Switch", TWL4030_REG_VDL_APGA_CTL, 2, 1, 0);
  427. /* Digital bypass gain, mute instead of -30dB */
  428. static const DECLARE_TLV_DB_RANGE(twl4030_dapm_dbypass_tlv,
  429. 0, 1, TLV_DB_SCALE_ITEM(-3000, 600, 1),
  430. 2, 3, TLV_DB_SCALE_ITEM(-2400, 0, 0),
  431. 4, 7, TLV_DB_SCALE_ITEM(-1800, 600, 0)
  432. );
  433. /* Digital bypass left (TX1L -> RX2L) */
  434. static const struct snd_kcontrol_new twl4030_dapm_dbypassl_control =
  435. SOC_DAPM_SINGLE_TLV("Volume",
  436. TWL4030_REG_ATX2ARXPGA, 3, 7, 0,
  437. twl4030_dapm_dbypass_tlv);
  438. /* Digital bypass right (TX1R -> RX2R) */
  439. static const struct snd_kcontrol_new twl4030_dapm_dbypassr_control =
  440. SOC_DAPM_SINGLE_TLV("Volume",
  441. TWL4030_REG_ATX2ARXPGA, 0, 7, 0,
  442. twl4030_dapm_dbypass_tlv);
  443. /*
  444. * Voice Sidetone GAIN volume control:
  445. * from -51 to -10 dB in 1 dB steps (mute instead of -51 dB)
  446. */
  447. static DECLARE_TLV_DB_SCALE(twl4030_dapm_dbypassv_tlv, -5100, 100, 1);
  448. /* Digital bypass voice: sidetone (VUL -> VDL)*/
  449. static const struct snd_kcontrol_new twl4030_dapm_dbypassv_control =
  450. SOC_DAPM_SINGLE_TLV("Volume",
  451. TWL4030_REG_VSTPGA, 0, 0x29, 0,
  452. twl4030_dapm_dbypassv_tlv);
  453. /*
  454. * Output PGA builder:
  455. * Handle the muting and unmuting of the given output (turning off the
  456. * amplifier associated with the output pin)
  457. * On mute bypass the reg_cache and write 0 to the register
  458. * On unmute: restore the register content from the reg_cache
  459. * Outputs handled in this way: Earpiece, PreDrivL/R, CarkitL/R
  460. */
  461. #define TWL4030_OUTPUT_PGA(pin_name, reg, mask) \
  462. static int pin_name##pga_event(struct snd_soc_dapm_widget *w, \
  463. struct snd_kcontrol *kcontrol, int event) \
  464. { \
  465. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); \
  466. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component); \
  467. \
  468. switch (event) { \
  469. case SND_SOC_DAPM_POST_PMU: \
  470. twl4030->pin_name##_enabled = 1; \
  471. twl4030_write(component, reg, twl4030_read(component, reg)); \
  472. break; \
  473. case SND_SOC_DAPM_POST_PMD: \
  474. twl4030->pin_name##_enabled = 0; \
  475. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, 0, reg); \
  476. break; \
  477. } \
  478. return 0; \
  479. }
  480. TWL4030_OUTPUT_PGA(earpiece, TWL4030_REG_EAR_CTL, TWL4030_EAR_GAIN);
  481. TWL4030_OUTPUT_PGA(predrivel, TWL4030_REG_PREDL_CTL, TWL4030_PREDL_GAIN);
  482. TWL4030_OUTPUT_PGA(predriver, TWL4030_REG_PREDR_CTL, TWL4030_PREDR_GAIN);
  483. TWL4030_OUTPUT_PGA(carkitl, TWL4030_REG_PRECKL_CTL, TWL4030_PRECKL_GAIN);
  484. TWL4030_OUTPUT_PGA(carkitr, TWL4030_REG_PRECKR_CTL, TWL4030_PRECKR_GAIN);
  485. static void handsfree_ramp(struct snd_soc_component *component, int reg, int ramp)
  486. {
  487. unsigned char hs_ctl;
  488. hs_ctl = twl4030_read(component, reg);
  489. if (ramp) {
  490. /* HF ramp-up */
  491. hs_ctl |= TWL4030_HF_CTL_REF_EN;
  492. twl4030_write(component, reg, hs_ctl);
  493. udelay(10);
  494. hs_ctl |= TWL4030_HF_CTL_RAMP_EN;
  495. twl4030_write(component, reg, hs_ctl);
  496. udelay(40);
  497. hs_ctl |= TWL4030_HF_CTL_LOOP_EN;
  498. hs_ctl |= TWL4030_HF_CTL_HB_EN;
  499. twl4030_write(component, reg, hs_ctl);
  500. } else {
  501. /* HF ramp-down */
  502. hs_ctl &= ~TWL4030_HF_CTL_LOOP_EN;
  503. hs_ctl &= ~TWL4030_HF_CTL_HB_EN;
  504. twl4030_write(component, reg, hs_ctl);
  505. hs_ctl &= ~TWL4030_HF_CTL_RAMP_EN;
  506. twl4030_write(component, reg, hs_ctl);
  507. udelay(40);
  508. hs_ctl &= ~TWL4030_HF_CTL_REF_EN;
  509. twl4030_write(component, reg, hs_ctl);
  510. }
  511. }
  512. static int handsfreelpga_event(struct snd_soc_dapm_widget *w,
  513. struct snd_kcontrol *kcontrol, int event)
  514. {
  515. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  516. switch (event) {
  517. case SND_SOC_DAPM_POST_PMU:
  518. handsfree_ramp(component, TWL4030_REG_HFL_CTL, 1);
  519. break;
  520. case SND_SOC_DAPM_POST_PMD:
  521. handsfree_ramp(component, TWL4030_REG_HFL_CTL, 0);
  522. break;
  523. }
  524. return 0;
  525. }
  526. static int handsfreerpga_event(struct snd_soc_dapm_widget *w,
  527. struct snd_kcontrol *kcontrol, int event)
  528. {
  529. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  530. switch (event) {
  531. case SND_SOC_DAPM_POST_PMU:
  532. handsfree_ramp(component, TWL4030_REG_HFR_CTL, 1);
  533. break;
  534. case SND_SOC_DAPM_POST_PMD:
  535. handsfree_ramp(component, TWL4030_REG_HFR_CTL, 0);
  536. break;
  537. }
  538. return 0;
  539. }
  540. static int vibramux_event(struct snd_soc_dapm_widget *w,
  541. struct snd_kcontrol *kcontrol, int event)
  542. {
  543. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  544. twl4030_write(component, TWL4030_REG_VIBRA_SET, 0xff);
  545. return 0;
  546. }
  547. static int apll_event(struct snd_soc_dapm_widget *w,
  548. struct snd_kcontrol *kcontrol, int event)
  549. {
  550. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  551. switch (event) {
  552. case SND_SOC_DAPM_PRE_PMU:
  553. twl4030_apll_enable(component, 1);
  554. break;
  555. case SND_SOC_DAPM_POST_PMD:
  556. twl4030_apll_enable(component, 0);
  557. break;
  558. }
  559. return 0;
  560. }
  561. static int aif_event(struct snd_soc_dapm_widget *w,
  562. struct snd_kcontrol *kcontrol, int event)
  563. {
  564. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  565. u8 audio_if;
  566. audio_if = twl4030_read(component, TWL4030_REG_AUDIO_IF);
  567. switch (event) {
  568. case SND_SOC_DAPM_PRE_PMU:
  569. /* Enable AIF */
  570. /* enable the PLL before we use it to clock the DAI */
  571. twl4030_apll_enable(component, 1);
  572. twl4030_write(component, TWL4030_REG_AUDIO_IF,
  573. audio_if | TWL4030_AIF_EN);
  574. break;
  575. case SND_SOC_DAPM_POST_PMD:
  576. /* disable the DAI before we stop it's source PLL */
  577. twl4030_write(component, TWL4030_REG_AUDIO_IF,
  578. audio_if & ~TWL4030_AIF_EN);
  579. twl4030_apll_enable(component, 0);
  580. break;
  581. }
  582. return 0;
  583. }
  584. static void headset_ramp(struct snd_soc_component *component, int ramp)
  585. {
  586. unsigned char hs_gain, hs_pop;
  587. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  588. struct twl4030_board_params *board_params = twl4030->board_params;
  589. /* Base values for ramp delay calculation: 2^19 - 2^26 */
  590. unsigned int ramp_base[] = {524288, 1048576, 2097152, 4194304,
  591. 8388608, 16777216, 33554432, 67108864};
  592. unsigned int delay;
  593. hs_gain = twl4030_read(component, TWL4030_REG_HS_GAIN_SET);
  594. hs_pop = twl4030_read(component, TWL4030_REG_HS_POPN_SET);
  595. delay = (ramp_base[(hs_pop & TWL4030_RAMP_DELAY) >> 2] /
  596. twl4030->sysclk) + 1;
  597. /* Enable external mute control, this dramatically reduces
  598. * the pop-noise */
  599. if (board_params && board_params->hs_extmute) {
  600. if (gpio_is_valid(board_params->hs_extmute_gpio)) {
  601. gpio_set_value(board_params->hs_extmute_gpio, 1);
  602. } else {
  603. hs_pop |= TWL4030_EXTMUTE;
  604. twl4030_write(component, TWL4030_REG_HS_POPN_SET, hs_pop);
  605. }
  606. }
  607. if (ramp) {
  608. /* Headset ramp-up according to the TRM */
  609. hs_pop |= TWL4030_VMID_EN;
  610. twl4030_write(component, TWL4030_REG_HS_POPN_SET, hs_pop);
  611. /* Actually write to the register */
  612. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, hs_gain,
  613. TWL4030_REG_HS_GAIN_SET);
  614. hs_pop |= TWL4030_RAMP_EN;
  615. twl4030_write(component, TWL4030_REG_HS_POPN_SET, hs_pop);
  616. /* Wait ramp delay time + 1, so the VMID can settle */
  617. twl4030_wait_ms(delay);
  618. } else {
  619. /* Headset ramp-down _not_ according to
  620. * the TRM, but in a way that it is working */
  621. hs_pop &= ~TWL4030_RAMP_EN;
  622. twl4030_write(component, TWL4030_REG_HS_POPN_SET, hs_pop);
  623. /* Wait ramp delay time + 1, so the VMID can settle */
  624. twl4030_wait_ms(delay);
  625. /* Bypass the reg_cache to mute the headset */
  626. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, hs_gain & (~0x0f),
  627. TWL4030_REG_HS_GAIN_SET);
  628. hs_pop &= ~TWL4030_VMID_EN;
  629. twl4030_write(component, TWL4030_REG_HS_POPN_SET, hs_pop);
  630. }
  631. /* Disable external mute */
  632. if (board_params && board_params->hs_extmute) {
  633. if (gpio_is_valid(board_params->hs_extmute_gpio)) {
  634. gpio_set_value(board_params->hs_extmute_gpio, 0);
  635. } else {
  636. hs_pop &= ~TWL4030_EXTMUTE;
  637. twl4030_write(component, TWL4030_REG_HS_POPN_SET, hs_pop);
  638. }
  639. }
  640. }
  641. static int headsetlpga_event(struct snd_soc_dapm_widget *w,
  642. struct snd_kcontrol *kcontrol, int event)
  643. {
  644. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  645. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  646. switch (event) {
  647. case SND_SOC_DAPM_POST_PMU:
  648. /* Do the ramp-up only once */
  649. if (!twl4030->hsr_enabled)
  650. headset_ramp(component, 1);
  651. twl4030->hsl_enabled = 1;
  652. break;
  653. case SND_SOC_DAPM_POST_PMD:
  654. /* Do the ramp-down only if both headsetL/R is disabled */
  655. if (!twl4030->hsr_enabled)
  656. headset_ramp(component, 0);
  657. twl4030->hsl_enabled = 0;
  658. break;
  659. }
  660. return 0;
  661. }
  662. static int headsetrpga_event(struct snd_soc_dapm_widget *w,
  663. struct snd_kcontrol *kcontrol, int event)
  664. {
  665. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  666. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  667. switch (event) {
  668. case SND_SOC_DAPM_POST_PMU:
  669. /* Do the ramp-up only once */
  670. if (!twl4030->hsl_enabled)
  671. headset_ramp(component, 1);
  672. twl4030->hsr_enabled = 1;
  673. break;
  674. case SND_SOC_DAPM_POST_PMD:
  675. /* Do the ramp-down only if both headsetL/R is disabled */
  676. if (!twl4030->hsl_enabled)
  677. headset_ramp(component, 0);
  678. twl4030->hsr_enabled = 0;
  679. break;
  680. }
  681. return 0;
  682. }
  683. static int digimic_event(struct snd_soc_dapm_widget *w,
  684. struct snd_kcontrol *kcontrol, int event)
  685. {
  686. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  687. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  688. struct twl4030_board_params *board_params = twl4030->board_params;
  689. if (board_params && board_params->digimic_delay)
  690. twl4030_wait_ms(board_params->digimic_delay);
  691. return 0;
  692. }
  693. /*
  694. * Some of the gain controls in TWL (mostly those which are associated with
  695. * the outputs) are implemented in an interesting way:
  696. * 0x0 : Power down (mute)
  697. * 0x1 : 6dB
  698. * 0x2 : 0 dB
  699. * 0x3 : -6 dB
  700. * Inverting not going to help with these.
  701. * Custom volsw and volsw_2r get/put functions to handle these gain bits.
  702. */
  703. static int snd_soc_get_volsw_twl4030(struct snd_kcontrol *kcontrol,
  704. struct snd_ctl_elem_value *ucontrol)
  705. {
  706. struct soc_mixer_control *mc =
  707. (struct soc_mixer_control *)kcontrol->private_value;
  708. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  709. unsigned int reg = mc->reg;
  710. unsigned int shift = mc->shift;
  711. unsigned int rshift = mc->rshift;
  712. int max = mc->max;
  713. int mask = (1 << fls(max)) - 1;
  714. ucontrol->value.integer.value[0] =
  715. (twl4030_read(component, reg) >> shift) & mask;
  716. if (ucontrol->value.integer.value[0])
  717. ucontrol->value.integer.value[0] =
  718. max + 1 - ucontrol->value.integer.value[0];
  719. if (shift != rshift) {
  720. ucontrol->value.integer.value[1] =
  721. (twl4030_read(component, reg) >> rshift) & mask;
  722. if (ucontrol->value.integer.value[1])
  723. ucontrol->value.integer.value[1] =
  724. max + 1 - ucontrol->value.integer.value[1];
  725. }
  726. return 0;
  727. }
  728. static int snd_soc_put_volsw_twl4030(struct snd_kcontrol *kcontrol,
  729. struct snd_ctl_elem_value *ucontrol)
  730. {
  731. struct soc_mixer_control *mc =
  732. (struct soc_mixer_control *)kcontrol->private_value;
  733. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  734. unsigned int reg = mc->reg;
  735. unsigned int shift = mc->shift;
  736. unsigned int rshift = mc->rshift;
  737. int max = mc->max;
  738. int mask = (1 << fls(max)) - 1;
  739. unsigned short val, val2, val_mask;
  740. val = (ucontrol->value.integer.value[0] & mask);
  741. val_mask = mask << shift;
  742. if (val)
  743. val = max + 1 - val;
  744. val = val << shift;
  745. if (shift != rshift) {
  746. val2 = (ucontrol->value.integer.value[1] & mask);
  747. val_mask |= mask << rshift;
  748. if (val2)
  749. val2 = max + 1 - val2;
  750. val |= val2 << rshift;
  751. }
  752. return snd_soc_component_update_bits(component, reg, val_mask, val);
  753. }
  754. static int snd_soc_get_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
  755. struct snd_ctl_elem_value *ucontrol)
  756. {
  757. struct soc_mixer_control *mc =
  758. (struct soc_mixer_control *)kcontrol->private_value;
  759. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  760. unsigned int reg = mc->reg;
  761. unsigned int reg2 = mc->rreg;
  762. unsigned int shift = mc->shift;
  763. int max = mc->max;
  764. int mask = (1<<fls(max))-1;
  765. ucontrol->value.integer.value[0] =
  766. (twl4030_read(component, reg) >> shift) & mask;
  767. ucontrol->value.integer.value[1] =
  768. (twl4030_read(component, reg2) >> shift) & mask;
  769. if (ucontrol->value.integer.value[0])
  770. ucontrol->value.integer.value[0] =
  771. max + 1 - ucontrol->value.integer.value[0];
  772. if (ucontrol->value.integer.value[1])
  773. ucontrol->value.integer.value[1] =
  774. max + 1 - ucontrol->value.integer.value[1];
  775. return 0;
  776. }
  777. static int snd_soc_put_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
  778. struct snd_ctl_elem_value *ucontrol)
  779. {
  780. struct soc_mixer_control *mc =
  781. (struct soc_mixer_control *)kcontrol->private_value;
  782. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  783. unsigned int reg = mc->reg;
  784. unsigned int reg2 = mc->rreg;
  785. unsigned int shift = mc->shift;
  786. int max = mc->max;
  787. int mask = (1 << fls(max)) - 1;
  788. int err;
  789. unsigned short val, val2, val_mask;
  790. val_mask = mask << shift;
  791. val = (ucontrol->value.integer.value[0] & mask);
  792. val2 = (ucontrol->value.integer.value[1] & mask);
  793. if (val)
  794. val = max + 1 - val;
  795. if (val2)
  796. val2 = max + 1 - val2;
  797. val = val << shift;
  798. val2 = val2 << shift;
  799. err = snd_soc_component_update_bits(component, reg, val_mask, val);
  800. if (err < 0)
  801. return err;
  802. err = snd_soc_component_update_bits(component, reg2, val_mask, val2);
  803. return err;
  804. }
  805. /* Codec operation modes */
  806. static const char *twl4030_op_modes_texts[] = {
  807. "Option 2 (voice/audio)", "Option 1 (audio)"
  808. };
  809. static SOC_ENUM_SINGLE_DECL(twl4030_op_modes_enum,
  810. TWL4030_REG_CODEC_MODE, 0,
  811. twl4030_op_modes_texts);
  812. static int snd_soc_put_twl4030_opmode_enum_double(struct snd_kcontrol *kcontrol,
  813. struct snd_ctl_elem_value *ucontrol)
  814. {
  815. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  816. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  817. if (twl4030->configured) {
  818. dev_err(component->dev,
  819. "operation mode cannot be changed on-the-fly\n");
  820. return -EBUSY;
  821. }
  822. return snd_soc_put_enum_double(kcontrol, ucontrol);
  823. }
  824. /*
  825. * FGAIN volume control:
  826. * from -62 to 0 dB in 1 dB steps (mute instead of -63 dB)
  827. */
  828. static DECLARE_TLV_DB_SCALE(digital_fine_tlv, -6300, 100, 1);
  829. /*
  830. * CGAIN volume control:
  831. * 0 dB to 12 dB in 6 dB steps
  832. * value 2 and 3 means 12 dB
  833. */
  834. static DECLARE_TLV_DB_SCALE(digital_coarse_tlv, 0, 600, 0);
  835. /*
  836. * Voice Downlink GAIN volume control:
  837. * from -37 to 12 dB in 1 dB steps (mute instead of -37 dB)
  838. */
  839. static DECLARE_TLV_DB_SCALE(digital_voice_downlink_tlv, -3700, 100, 1);
  840. /*
  841. * Analog playback gain
  842. * -24 dB to 12 dB in 2 dB steps
  843. */
  844. static DECLARE_TLV_DB_SCALE(analog_tlv, -2400, 200, 0);
  845. /*
  846. * Gain controls tied to outputs
  847. * -6 dB to 6 dB in 6 dB steps (mute instead of -12)
  848. */
  849. static DECLARE_TLV_DB_SCALE(output_tvl, -1200, 600, 1);
  850. /*
  851. * Gain control for earpiece amplifier
  852. * 0 dB to 12 dB in 6 dB steps (mute instead of -6)
  853. */
  854. static DECLARE_TLV_DB_SCALE(output_ear_tvl, -600, 600, 1);
  855. /*
  856. * Capture gain after the ADCs
  857. * from 0 dB to 31 dB in 1 dB steps
  858. */
  859. static DECLARE_TLV_DB_SCALE(digital_capture_tlv, 0, 100, 0);
  860. /*
  861. * Gain control for input amplifiers
  862. * 0 dB to 30 dB in 6 dB steps
  863. */
  864. static DECLARE_TLV_DB_SCALE(input_gain_tlv, 0, 600, 0);
  865. /* AVADC clock priority */
  866. static const char *twl4030_avadc_clk_priority_texts[] = {
  867. "Voice high priority", "HiFi high priority"
  868. };
  869. static SOC_ENUM_SINGLE_DECL(twl4030_avadc_clk_priority_enum,
  870. TWL4030_REG_AVADC_CTL, 2,
  871. twl4030_avadc_clk_priority_texts);
  872. static const char *twl4030_rampdelay_texts[] = {
  873. "27/20/14 ms", "55/40/27 ms", "109/81/55 ms", "218/161/109 ms",
  874. "437/323/218 ms", "874/645/437 ms", "1748/1291/874 ms",
  875. "3495/2581/1748 ms"
  876. };
  877. static SOC_ENUM_SINGLE_DECL(twl4030_rampdelay_enum,
  878. TWL4030_REG_HS_POPN_SET, 2,
  879. twl4030_rampdelay_texts);
  880. /* Vibra H-bridge direction mode */
  881. static const char *twl4030_vibradirmode_texts[] = {
  882. "Vibra H-bridge direction", "Audio data MSB",
  883. };
  884. static SOC_ENUM_SINGLE_DECL(twl4030_vibradirmode_enum,
  885. TWL4030_REG_VIBRA_CTL, 5,
  886. twl4030_vibradirmode_texts);
  887. /* Vibra H-bridge direction */
  888. static const char *twl4030_vibradir_texts[] = {
  889. "Positive polarity", "Negative polarity",
  890. };
  891. static SOC_ENUM_SINGLE_DECL(twl4030_vibradir_enum,
  892. TWL4030_REG_VIBRA_CTL, 1,
  893. twl4030_vibradir_texts);
  894. /* Digimic Left and right swapping */
  895. static const char *twl4030_digimicswap_texts[] = {
  896. "Not swapped", "Swapped",
  897. };
  898. static SOC_ENUM_SINGLE_DECL(twl4030_digimicswap_enum,
  899. TWL4030_REG_MISC_SET_1, 0,
  900. twl4030_digimicswap_texts);
  901. static const struct snd_kcontrol_new twl4030_snd_controls[] = {
  902. /* Codec operation mode control */
  903. SOC_ENUM_EXT("Codec Operation Mode", twl4030_op_modes_enum,
  904. snd_soc_get_enum_double,
  905. snd_soc_put_twl4030_opmode_enum_double),
  906. /* Common playback gain controls */
  907. SOC_DOUBLE_R_TLV("DAC1 Digital Fine Playback Volume",
  908. TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
  909. 0, 0x3f, 0, digital_fine_tlv),
  910. SOC_DOUBLE_R_TLV("DAC2 Digital Fine Playback Volume",
  911. TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
  912. 0, 0x3f, 0, digital_fine_tlv),
  913. SOC_DOUBLE_R_TLV("DAC1 Digital Coarse Playback Volume",
  914. TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
  915. 6, 0x2, 0, digital_coarse_tlv),
  916. SOC_DOUBLE_R_TLV("DAC2 Digital Coarse Playback Volume",
  917. TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
  918. 6, 0x2, 0, digital_coarse_tlv),
  919. SOC_DOUBLE_R_TLV("DAC1 Analog Playback Volume",
  920. TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
  921. 3, 0x12, 1, analog_tlv),
  922. SOC_DOUBLE_R_TLV("DAC2 Analog Playback Volume",
  923. TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
  924. 3, 0x12, 1, analog_tlv),
  925. SOC_DOUBLE_R("DAC1 Analog Playback Switch",
  926. TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
  927. 1, 1, 0),
  928. SOC_DOUBLE_R("DAC2 Analog Playback Switch",
  929. TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
  930. 1, 1, 0),
  931. /* Common voice downlink gain controls */
  932. SOC_SINGLE_TLV("DAC Voice Digital Downlink Volume",
  933. TWL4030_REG_VRXPGA, 0, 0x31, 0, digital_voice_downlink_tlv),
  934. SOC_SINGLE_TLV("DAC Voice Analog Downlink Volume",
  935. TWL4030_REG_VDL_APGA_CTL, 3, 0x12, 1, analog_tlv),
  936. SOC_SINGLE("DAC Voice Analog Downlink Switch",
  937. TWL4030_REG_VDL_APGA_CTL, 1, 1, 0),
  938. /* Separate output gain controls */
  939. SOC_DOUBLE_R_EXT_TLV("PreDriv Playback Volume",
  940. TWL4030_REG_PREDL_CTL, TWL4030_REG_PREDR_CTL,
  941. 4, 3, 0, snd_soc_get_volsw_r2_twl4030,
  942. snd_soc_put_volsw_r2_twl4030, output_tvl),
  943. SOC_DOUBLE_EXT_TLV("Headset Playback Volume",
  944. TWL4030_REG_HS_GAIN_SET, 0, 2, 3, 0, snd_soc_get_volsw_twl4030,
  945. snd_soc_put_volsw_twl4030, output_tvl),
  946. SOC_DOUBLE_R_EXT_TLV("Carkit Playback Volume",
  947. TWL4030_REG_PRECKL_CTL, TWL4030_REG_PRECKR_CTL,
  948. 4, 3, 0, snd_soc_get_volsw_r2_twl4030,
  949. snd_soc_put_volsw_r2_twl4030, output_tvl),
  950. SOC_SINGLE_EXT_TLV("Earpiece Playback Volume",
  951. TWL4030_REG_EAR_CTL, 4, 3, 0, snd_soc_get_volsw_twl4030,
  952. snd_soc_put_volsw_twl4030, output_ear_tvl),
  953. /* Common capture gain controls */
  954. SOC_DOUBLE_R_TLV("TX1 Digital Capture Volume",
  955. TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA,
  956. 0, 0x1f, 0, digital_capture_tlv),
  957. SOC_DOUBLE_R_TLV("TX2 Digital Capture Volume",
  958. TWL4030_REG_AVTXL2PGA, TWL4030_REG_AVTXR2PGA,
  959. 0, 0x1f, 0, digital_capture_tlv),
  960. SOC_DOUBLE_TLV("Analog Capture Volume", TWL4030_REG_ANAMIC_GAIN,
  961. 0, 3, 5, 0, input_gain_tlv),
  962. SOC_ENUM("AVADC Clock Priority", twl4030_avadc_clk_priority_enum),
  963. SOC_ENUM("HS ramp delay", twl4030_rampdelay_enum),
  964. SOC_ENUM("Vibra H-bridge mode", twl4030_vibradirmode_enum),
  965. SOC_ENUM("Vibra H-bridge direction", twl4030_vibradir_enum),
  966. SOC_ENUM("Digimic LR Swap", twl4030_digimicswap_enum),
  967. };
  968. static const struct snd_soc_dapm_widget twl4030_dapm_widgets[] = {
  969. /* Left channel inputs */
  970. SND_SOC_DAPM_INPUT("MAINMIC"),
  971. SND_SOC_DAPM_INPUT("HSMIC"),
  972. SND_SOC_DAPM_INPUT("AUXL"),
  973. SND_SOC_DAPM_INPUT("CARKITMIC"),
  974. /* Right channel inputs */
  975. SND_SOC_DAPM_INPUT("SUBMIC"),
  976. SND_SOC_DAPM_INPUT("AUXR"),
  977. /* Digital microphones (Stereo) */
  978. SND_SOC_DAPM_INPUT("DIGIMIC0"),
  979. SND_SOC_DAPM_INPUT("DIGIMIC1"),
  980. /* Outputs */
  981. SND_SOC_DAPM_OUTPUT("EARPIECE"),
  982. SND_SOC_DAPM_OUTPUT("PREDRIVEL"),
  983. SND_SOC_DAPM_OUTPUT("PREDRIVER"),
  984. SND_SOC_DAPM_OUTPUT("HSOL"),
  985. SND_SOC_DAPM_OUTPUT("HSOR"),
  986. SND_SOC_DAPM_OUTPUT("CARKITL"),
  987. SND_SOC_DAPM_OUTPUT("CARKITR"),
  988. SND_SOC_DAPM_OUTPUT("HFL"),
  989. SND_SOC_DAPM_OUTPUT("HFR"),
  990. SND_SOC_DAPM_OUTPUT("VIBRA"),
  991. /* AIF and APLL clocks for running DAIs (including loopback) */
  992. SND_SOC_DAPM_OUTPUT("Virtual HiFi OUT"),
  993. SND_SOC_DAPM_INPUT("Virtual HiFi IN"),
  994. SND_SOC_DAPM_OUTPUT("Virtual Voice OUT"),
  995. /* DACs */
  996. SND_SOC_DAPM_DAC("DAC Right1", NULL, SND_SOC_NOPM, 0, 0),
  997. SND_SOC_DAPM_DAC("DAC Left1", NULL, SND_SOC_NOPM, 0, 0),
  998. SND_SOC_DAPM_DAC("DAC Right2", NULL, SND_SOC_NOPM, 0, 0),
  999. SND_SOC_DAPM_DAC("DAC Left2", NULL, SND_SOC_NOPM, 0, 0),
  1000. SND_SOC_DAPM_DAC("DAC Voice", NULL, SND_SOC_NOPM, 0, 0),
  1001. SND_SOC_DAPM_AIF_IN("VAIFIN", "Voice Playback", 0,
  1002. TWL4030_REG_VOICE_IF, 6, 0),
  1003. /* Analog bypasses */
  1004. SND_SOC_DAPM_SWITCH("Right1 Analog Loopback", SND_SOC_NOPM, 0, 0,
  1005. &twl4030_dapm_abypassr1_control),
  1006. SND_SOC_DAPM_SWITCH("Left1 Analog Loopback", SND_SOC_NOPM, 0, 0,
  1007. &twl4030_dapm_abypassl1_control),
  1008. SND_SOC_DAPM_SWITCH("Right2 Analog Loopback", SND_SOC_NOPM, 0, 0,
  1009. &twl4030_dapm_abypassr2_control),
  1010. SND_SOC_DAPM_SWITCH("Left2 Analog Loopback", SND_SOC_NOPM, 0, 0,
  1011. &twl4030_dapm_abypassl2_control),
  1012. SND_SOC_DAPM_SWITCH("Voice Analog Loopback", SND_SOC_NOPM, 0, 0,
  1013. &twl4030_dapm_abypassv_control),
  1014. /* Master analog loopback switch */
  1015. SND_SOC_DAPM_SUPPLY("FM Loop Enable", TWL4030_REG_MISC_SET_1, 5, 0,
  1016. NULL, 0),
  1017. /* Digital bypasses */
  1018. SND_SOC_DAPM_SWITCH("Left Digital Loopback", SND_SOC_NOPM, 0, 0,
  1019. &twl4030_dapm_dbypassl_control),
  1020. SND_SOC_DAPM_SWITCH("Right Digital Loopback", SND_SOC_NOPM, 0, 0,
  1021. &twl4030_dapm_dbypassr_control),
  1022. SND_SOC_DAPM_SWITCH("Voice Digital Loopback", SND_SOC_NOPM, 0, 0,
  1023. &twl4030_dapm_dbypassv_control),
  1024. /* Digital mixers, power control for the physical DACs */
  1025. SND_SOC_DAPM_MIXER("Digital R1 Playback Mixer",
  1026. TWL4030_REG_AVDAC_CTL, 0, 0, NULL, 0),
  1027. SND_SOC_DAPM_MIXER("Digital L1 Playback Mixer",
  1028. TWL4030_REG_AVDAC_CTL, 1, 0, NULL, 0),
  1029. SND_SOC_DAPM_MIXER("Digital R2 Playback Mixer",
  1030. TWL4030_REG_AVDAC_CTL, 2, 0, NULL, 0),
  1031. SND_SOC_DAPM_MIXER("Digital L2 Playback Mixer",
  1032. TWL4030_REG_AVDAC_CTL, 3, 0, NULL, 0),
  1033. SND_SOC_DAPM_MIXER("Digital Voice Playback Mixer",
  1034. TWL4030_REG_AVDAC_CTL, 4, 0, NULL, 0),
  1035. /* Analog mixers, power control for the physical PGAs */
  1036. SND_SOC_DAPM_MIXER("Analog R1 Playback Mixer",
  1037. TWL4030_REG_ARXR1_APGA_CTL, 0, 0, NULL, 0),
  1038. SND_SOC_DAPM_MIXER("Analog L1 Playback Mixer",
  1039. TWL4030_REG_ARXL1_APGA_CTL, 0, 0, NULL, 0),
  1040. SND_SOC_DAPM_MIXER("Analog R2 Playback Mixer",
  1041. TWL4030_REG_ARXR2_APGA_CTL, 0, 0, NULL, 0),
  1042. SND_SOC_DAPM_MIXER("Analog L2 Playback Mixer",
  1043. TWL4030_REG_ARXL2_APGA_CTL, 0, 0, NULL, 0),
  1044. SND_SOC_DAPM_MIXER("Analog Voice Playback Mixer",
  1045. TWL4030_REG_VDL_APGA_CTL, 0, 0, NULL, 0),
  1046. SND_SOC_DAPM_SUPPLY("APLL Enable", SND_SOC_NOPM, 0, 0, apll_event,
  1047. SND_SOC_DAPM_PRE_PMU|SND_SOC_DAPM_POST_PMD),
  1048. SND_SOC_DAPM_SUPPLY("AIF Enable", SND_SOC_NOPM, 0, 0, aif_event,
  1049. SND_SOC_DAPM_PRE_PMU|SND_SOC_DAPM_POST_PMD),
  1050. /* Output MIXER controls */
  1051. /* Earpiece */
  1052. SND_SOC_DAPM_MIXER("Earpiece Mixer", SND_SOC_NOPM, 0, 0,
  1053. &twl4030_dapm_earpiece_controls[0],
  1054. ARRAY_SIZE(twl4030_dapm_earpiece_controls)),
  1055. SND_SOC_DAPM_PGA_E("Earpiece PGA", SND_SOC_NOPM,
  1056. 0, 0, NULL, 0, earpiecepga_event,
  1057. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
  1058. /* PreDrivL/R */
  1059. SND_SOC_DAPM_MIXER("PredriveL Mixer", SND_SOC_NOPM, 0, 0,
  1060. &twl4030_dapm_predrivel_controls[0],
  1061. ARRAY_SIZE(twl4030_dapm_predrivel_controls)),
  1062. SND_SOC_DAPM_PGA_E("PredriveL PGA", SND_SOC_NOPM,
  1063. 0, 0, NULL, 0, predrivelpga_event,
  1064. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
  1065. SND_SOC_DAPM_MIXER("PredriveR Mixer", SND_SOC_NOPM, 0, 0,
  1066. &twl4030_dapm_predriver_controls[0],
  1067. ARRAY_SIZE(twl4030_dapm_predriver_controls)),
  1068. SND_SOC_DAPM_PGA_E("PredriveR PGA", SND_SOC_NOPM,
  1069. 0, 0, NULL, 0, predriverpga_event,
  1070. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
  1071. /* HeadsetL/R */
  1072. SND_SOC_DAPM_MIXER("HeadsetL Mixer", SND_SOC_NOPM, 0, 0,
  1073. &twl4030_dapm_hsol_controls[0],
  1074. ARRAY_SIZE(twl4030_dapm_hsol_controls)),
  1075. SND_SOC_DAPM_PGA_E("HeadsetL PGA", SND_SOC_NOPM,
  1076. 0, 0, NULL, 0, headsetlpga_event,
  1077. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
  1078. SND_SOC_DAPM_MIXER("HeadsetR Mixer", SND_SOC_NOPM, 0, 0,
  1079. &twl4030_dapm_hsor_controls[0],
  1080. ARRAY_SIZE(twl4030_dapm_hsor_controls)),
  1081. SND_SOC_DAPM_PGA_E("HeadsetR PGA", SND_SOC_NOPM,
  1082. 0, 0, NULL, 0, headsetrpga_event,
  1083. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
  1084. /* CarkitL/R */
  1085. SND_SOC_DAPM_MIXER("CarkitL Mixer", SND_SOC_NOPM, 0, 0,
  1086. &twl4030_dapm_carkitl_controls[0],
  1087. ARRAY_SIZE(twl4030_dapm_carkitl_controls)),
  1088. SND_SOC_DAPM_PGA_E("CarkitL PGA", SND_SOC_NOPM,
  1089. 0, 0, NULL, 0, carkitlpga_event,
  1090. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
  1091. SND_SOC_DAPM_MIXER("CarkitR Mixer", SND_SOC_NOPM, 0, 0,
  1092. &twl4030_dapm_carkitr_controls[0],
  1093. ARRAY_SIZE(twl4030_dapm_carkitr_controls)),
  1094. SND_SOC_DAPM_PGA_E("CarkitR PGA", SND_SOC_NOPM,
  1095. 0, 0, NULL, 0, carkitrpga_event,
  1096. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
  1097. /* Output MUX controls */
  1098. /* HandsfreeL/R */
  1099. SND_SOC_DAPM_MUX("HandsfreeL Mux", SND_SOC_NOPM, 0, 0,
  1100. &twl4030_dapm_handsfreel_control),
  1101. SND_SOC_DAPM_SWITCH("HandsfreeL", SND_SOC_NOPM, 0, 0,
  1102. &twl4030_dapm_handsfreelmute_control),
  1103. SND_SOC_DAPM_PGA_E("HandsfreeL PGA", SND_SOC_NOPM,
  1104. 0, 0, NULL, 0, handsfreelpga_event,
  1105. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
  1106. SND_SOC_DAPM_MUX("HandsfreeR Mux", SND_SOC_NOPM, 5, 0,
  1107. &twl4030_dapm_handsfreer_control),
  1108. SND_SOC_DAPM_SWITCH("HandsfreeR", SND_SOC_NOPM, 0, 0,
  1109. &twl4030_dapm_handsfreermute_control),
  1110. SND_SOC_DAPM_PGA_E("HandsfreeR PGA", SND_SOC_NOPM,
  1111. 0, 0, NULL, 0, handsfreerpga_event,
  1112. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
  1113. /* Vibra */
  1114. SND_SOC_DAPM_MUX_E("Vibra Mux", TWL4030_REG_VIBRA_CTL, 0, 0,
  1115. &twl4030_dapm_vibra_control, vibramux_event,
  1116. SND_SOC_DAPM_PRE_PMU),
  1117. SND_SOC_DAPM_MUX("Vibra Route", SND_SOC_NOPM, 0, 0,
  1118. &twl4030_dapm_vibrapath_control),
  1119. /* Introducing four virtual ADC, since TWL4030 have four channel for
  1120. capture */
  1121. SND_SOC_DAPM_ADC("ADC Virtual Left1", NULL, SND_SOC_NOPM, 0, 0),
  1122. SND_SOC_DAPM_ADC("ADC Virtual Right1", NULL, SND_SOC_NOPM, 0, 0),
  1123. SND_SOC_DAPM_ADC("ADC Virtual Left2", NULL, SND_SOC_NOPM, 0, 0),
  1124. SND_SOC_DAPM_ADC("ADC Virtual Right2", NULL, SND_SOC_NOPM, 0, 0),
  1125. SND_SOC_DAPM_AIF_OUT("VAIFOUT", "Voice Capture", 0,
  1126. TWL4030_REG_VOICE_IF, 5, 0),
  1127. /* Analog/Digital mic path selection.
  1128. TX1 Left/Right: either analog Left/Right or Digimic0
  1129. TX2 Left/Right: either analog Left/Right or Digimic1 */
  1130. SND_SOC_DAPM_MUX("TX1 Capture Route", SND_SOC_NOPM, 0, 0,
  1131. &twl4030_dapm_micpathtx1_control),
  1132. SND_SOC_DAPM_MUX("TX2 Capture Route", SND_SOC_NOPM, 0, 0,
  1133. &twl4030_dapm_micpathtx2_control),
  1134. /* Analog input mixers for the capture amplifiers */
  1135. SND_SOC_DAPM_MIXER("Analog Left",
  1136. TWL4030_REG_ANAMICL, 4, 0,
  1137. &twl4030_dapm_analoglmic_controls[0],
  1138. ARRAY_SIZE(twl4030_dapm_analoglmic_controls)),
  1139. SND_SOC_DAPM_MIXER("Analog Right",
  1140. TWL4030_REG_ANAMICR, 4, 0,
  1141. &twl4030_dapm_analogrmic_controls[0],
  1142. ARRAY_SIZE(twl4030_dapm_analogrmic_controls)),
  1143. SND_SOC_DAPM_PGA("ADC Physical Left",
  1144. TWL4030_REG_AVADC_CTL, 3, 0, NULL, 0),
  1145. SND_SOC_DAPM_PGA("ADC Physical Right",
  1146. TWL4030_REG_AVADC_CTL, 1, 0, NULL, 0),
  1147. SND_SOC_DAPM_PGA_E("Digimic0 Enable",
  1148. TWL4030_REG_ADCMICSEL, 1, 0, NULL, 0,
  1149. digimic_event, SND_SOC_DAPM_POST_PMU),
  1150. SND_SOC_DAPM_PGA_E("Digimic1 Enable",
  1151. TWL4030_REG_ADCMICSEL, 3, 0, NULL, 0,
  1152. digimic_event, SND_SOC_DAPM_POST_PMU),
  1153. SND_SOC_DAPM_SUPPLY("micbias1 select", TWL4030_REG_MICBIAS_CTL, 5, 0,
  1154. NULL, 0),
  1155. SND_SOC_DAPM_SUPPLY("micbias2 select", TWL4030_REG_MICBIAS_CTL, 6, 0,
  1156. NULL, 0),
  1157. /* Microphone bias */
  1158. SND_SOC_DAPM_SUPPLY("Mic Bias 1",
  1159. TWL4030_REG_MICBIAS_CTL, 0, 0, NULL, 0),
  1160. SND_SOC_DAPM_SUPPLY("Mic Bias 2",
  1161. TWL4030_REG_MICBIAS_CTL, 1, 0, NULL, 0),
  1162. SND_SOC_DAPM_SUPPLY("Headset Mic Bias",
  1163. TWL4030_REG_MICBIAS_CTL, 2, 0, NULL, 0),
  1164. SND_SOC_DAPM_SUPPLY("VIF Enable", TWL4030_REG_VOICE_IF, 0, 0, NULL, 0),
  1165. };
  1166. static const struct snd_soc_dapm_route intercon[] = {
  1167. /* Stream -> DAC mapping */
  1168. {"DAC Right1", NULL, "HiFi Playback"},
  1169. {"DAC Left1", NULL, "HiFi Playback"},
  1170. {"DAC Right2", NULL, "HiFi Playback"},
  1171. {"DAC Left2", NULL, "HiFi Playback"},
  1172. {"DAC Voice", NULL, "VAIFIN"},
  1173. /* ADC -> Stream mapping */
  1174. {"HiFi Capture", NULL, "ADC Virtual Left1"},
  1175. {"HiFi Capture", NULL, "ADC Virtual Right1"},
  1176. {"HiFi Capture", NULL, "ADC Virtual Left2"},
  1177. {"HiFi Capture", NULL, "ADC Virtual Right2"},
  1178. {"VAIFOUT", NULL, "ADC Virtual Left2"},
  1179. {"VAIFOUT", NULL, "ADC Virtual Right2"},
  1180. {"VAIFOUT", NULL, "VIF Enable"},
  1181. {"Digital L1 Playback Mixer", NULL, "DAC Left1"},
  1182. {"Digital R1 Playback Mixer", NULL, "DAC Right1"},
  1183. {"Digital L2 Playback Mixer", NULL, "DAC Left2"},
  1184. {"Digital R2 Playback Mixer", NULL, "DAC Right2"},
  1185. {"Digital Voice Playback Mixer", NULL, "DAC Voice"},
  1186. /* Supply for the digital part (APLL) */
  1187. {"Digital Voice Playback Mixer", NULL, "APLL Enable"},
  1188. {"DAC Left1", NULL, "AIF Enable"},
  1189. {"DAC Right1", NULL, "AIF Enable"},
  1190. {"DAC Left2", NULL, "AIF Enable"},
  1191. {"DAC Right1", NULL, "AIF Enable"},
  1192. {"DAC Voice", NULL, "VIF Enable"},
  1193. {"Digital R2 Playback Mixer", NULL, "AIF Enable"},
  1194. {"Digital L2 Playback Mixer", NULL, "AIF Enable"},
  1195. {"Analog L1 Playback Mixer", NULL, "Digital L1 Playback Mixer"},
  1196. {"Analog R1 Playback Mixer", NULL, "Digital R1 Playback Mixer"},
  1197. {"Analog L2 Playback Mixer", NULL, "Digital L2 Playback Mixer"},
  1198. {"Analog R2 Playback Mixer", NULL, "Digital R2 Playback Mixer"},
  1199. {"Analog Voice Playback Mixer", NULL, "Digital Voice Playback Mixer"},
  1200. /* Internal playback routings */
  1201. /* Earpiece */
  1202. {"Earpiece Mixer", "Voice", "Analog Voice Playback Mixer"},
  1203. {"Earpiece Mixer", "AudioL1", "Analog L1 Playback Mixer"},
  1204. {"Earpiece Mixer", "AudioL2", "Analog L2 Playback Mixer"},
  1205. {"Earpiece Mixer", "AudioR1", "Analog R1 Playback Mixer"},
  1206. {"Earpiece PGA", NULL, "Earpiece Mixer"},
  1207. /* PreDrivL */
  1208. {"PredriveL Mixer", "Voice", "Analog Voice Playback Mixer"},
  1209. {"PredriveL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
  1210. {"PredriveL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
  1211. {"PredriveL Mixer", "AudioR2", "Analog R2 Playback Mixer"},
  1212. {"PredriveL PGA", NULL, "PredriveL Mixer"},
  1213. /* PreDrivR */
  1214. {"PredriveR Mixer", "Voice", "Analog Voice Playback Mixer"},
  1215. {"PredriveR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
  1216. {"PredriveR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
  1217. {"PredriveR Mixer", "AudioL2", "Analog L2 Playback Mixer"},
  1218. {"PredriveR PGA", NULL, "PredriveR Mixer"},
  1219. /* HeadsetL */
  1220. {"HeadsetL Mixer", "Voice", "Analog Voice Playback Mixer"},
  1221. {"HeadsetL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
  1222. {"HeadsetL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
  1223. {"HeadsetL PGA", NULL, "HeadsetL Mixer"},
  1224. /* HeadsetR */
  1225. {"HeadsetR Mixer", "Voice", "Analog Voice Playback Mixer"},
  1226. {"HeadsetR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
  1227. {"HeadsetR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
  1228. {"HeadsetR PGA", NULL, "HeadsetR Mixer"},
  1229. /* CarkitL */
  1230. {"CarkitL Mixer", "Voice", "Analog Voice Playback Mixer"},
  1231. {"CarkitL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
  1232. {"CarkitL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
  1233. {"CarkitL PGA", NULL, "CarkitL Mixer"},
  1234. /* CarkitR */
  1235. {"CarkitR Mixer", "Voice", "Analog Voice Playback Mixer"},
  1236. {"CarkitR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
  1237. {"CarkitR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
  1238. {"CarkitR PGA", NULL, "CarkitR Mixer"},
  1239. /* HandsfreeL */
  1240. {"HandsfreeL Mux", "Voice", "Analog Voice Playback Mixer"},
  1241. {"HandsfreeL Mux", "AudioL1", "Analog L1 Playback Mixer"},
  1242. {"HandsfreeL Mux", "AudioL2", "Analog L2 Playback Mixer"},
  1243. {"HandsfreeL Mux", "AudioR2", "Analog R2 Playback Mixer"},
  1244. {"HandsfreeL", "Switch", "HandsfreeL Mux"},
  1245. {"HandsfreeL PGA", NULL, "HandsfreeL"},
  1246. /* HandsfreeR */
  1247. {"HandsfreeR Mux", "Voice", "Analog Voice Playback Mixer"},
  1248. {"HandsfreeR Mux", "AudioR1", "Analog R1 Playback Mixer"},
  1249. {"HandsfreeR Mux", "AudioR2", "Analog R2 Playback Mixer"},
  1250. {"HandsfreeR Mux", "AudioL2", "Analog L2 Playback Mixer"},
  1251. {"HandsfreeR", "Switch", "HandsfreeR Mux"},
  1252. {"HandsfreeR PGA", NULL, "HandsfreeR"},
  1253. /* Vibra */
  1254. {"Vibra Mux", "AudioL1", "DAC Left1"},
  1255. {"Vibra Mux", "AudioR1", "DAC Right1"},
  1256. {"Vibra Mux", "AudioL2", "DAC Left2"},
  1257. {"Vibra Mux", "AudioR2", "DAC Right2"},
  1258. /* outputs */
  1259. /* Must be always connected (for AIF and APLL) */
  1260. {"Virtual HiFi OUT", NULL, "DAC Left1"},
  1261. {"Virtual HiFi OUT", NULL, "DAC Right1"},
  1262. {"Virtual HiFi OUT", NULL, "DAC Left2"},
  1263. {"Virtual HiFi OUT", NULL, "DAC Right2"},
  1264. /* Must be always connected (for APLL) */
  1265. {"Virtual Voice OUT", NULL, "Digital Voice Playback Mixer"},
  1266. /* Physical outputs */
  1267. {"EARPIECE", NULL, "Earpiece PGA"},
  1268. {"PREDRIVEL", NULL, "PredriveL PGA"},
  1269. {"PREDRIVER", NULL, "PredriveR PGA"},
  1270. {"HSOL", NULL, "HeadsetL PGA"},
  1271. {"HSOR", NULL, "HeadsetR PGA"},
  1272. {"CARKITL", NULL, "CarkitL PGA"},
  1273. {"CARKITR", NULL, "CarkitR PGA"},
  1274. {"HFL", NULL, "HandsfreeL PGA"},
  1275. {"HFR", NULL, "HandsfreeR PGA"},
  1276. {"Vibra Route", "Audio", "Vibra Mux"},
  1277. {"VIBRA", NULL, "Vibra Route"},
  1278. /* Capture path */
  1279. /* Must be always connected (for AIF and APLL) */
  1280. {"ADC Virtual Left1", NULL, "Virtual HiFi IN"},
  1281. {"ADC Virtual Right1", NULL, "Virtual HiFi IN"},
  1282. {"ADC Virtual Left2", NULL, "Virtual HiFi IN"},
  1283. {"ADC Virtual Right2", NULL, "Virtual HiFi IN"},
  1284. /* Physical inputs */
  1285. {"Analog Left", "Main Mic Capture Switch", "MAINMIC"},
  1286. {"Analog Left", "Headset Mic Capture Switch", "HSMIC"},
  1287. {"Analog Left", "AUXL Capture Switch", "AUXL"},
  1288. {"Analog Left", "Carkit Mic Capture Switch", "CARKITMIC"},
  1289. {"Analog Right", "Sub Mic Capture Switch", "SUBMIC"},
  1290. {"Analog Right", "AUXR Capture Switch", "AUXR"},
  1291. {"ADC Physical Left", NULL, "Analog Left"},
  1292. {"ADC Physical Right", NULL, "Analog Right"},
  1293. {"Digimic0 Enable", NULL, "DIGIMIC0"},
  1294. {"Digimic1 Enable", NULL, "DIGIMIC1"},
  1295. {"DIGIMIC0", NULL, "micbias1 select"},
  1296. {"DIGIMIC1", NULL, "micbias2 select"},
  1297. /* TX1 Left capture path */
  1298. {"TX1 Capture Route", "Analog", "ADC Physical Left"},
  1299. {"TX1 Capture Route", "Digimic0", "Digimic0 Enable"},
  1300. /* TX1 Right capture path */
  1301. {"TX1 Capture Route", "Analog", "ADC Physical Right"},
  1302. {"TX1 Capture Route", "Digimic0", "Digimic0 Enable"},
  1303. /* TX2 Left capture path */
  1304. {"TX2 Capture Route", "Analog", "ADC Physical Left"},
  1305. {"TX2 Capture Route", "Digimic1", "Digimic1 Enable"},
  1306. /* TX2 Right capture path */
  1307. {"TX2 Capture Route", "Analog", "ADC Physical Right"},
  1308. {"TX2 Capture Route", "Digimic1", "Digimic1 Enable"},
  1309. {"ADC Virtual Left1", NULL, "TX1 Capture Route"},
  1310. {"ADC Virtual Right1", NULL, "TX1 Capture Route"},
  1311. {"ADC Virtual Left2", NULL, "TX2 Capture Route"},
  1312. {"ADC Virtual Right2", NULL, "TX2 Capture Route"},
  1313. {"ADC Virtual Left1", NULL, "AIF Enable"},
  1314. {"ADC Virtual Right1", NULL, "AIF Enable"},
  1315. {"ADC Virtual Left2", NULL, "AIF Enable"},
  1316. {"ADC Virtual Right2", NULL, "AIF Enable"},
  1317. /* Analog bypass routes */
  1318. {"Right1 Analog Loopback", "Switch", "Analog Right"},
  1319. {"Left1 Analog Loopback", "Switch", "Analog Left"},
  1320. {"Right2 Analog Loopback", "Switch", "Analog Right"},
  1321. {"Left2 Analog Loopback", "Switch", "Analog Left"},
  1322. {"Voice Analog Loopback", "Switch", "Analog Left"},
  1323. /* Supply for the Analog loopbacks */
  1324. {"Right1 Analog Loopback", NULL, "FM Loop Enable"},
  1325. {"Left1 Analog Loopback", NULL, "FM Loop Enable"},
  1326. {"Right2 Analog Loopback", NULL, "FM Loop Enable"},
  1327. {"Left2 Analog Loopback", NULL, "FM Loop Enable"},
  1328. {"Voice Analog Loopback", NULL, "FM Loop Enable"},
  1329. {"Analog R1 Playback Mixer", NULL, "Right1 Analog Loopback"},
  1330. {"Analog L1 Playback Mixer", NULL, "Left1 Analog Loopback"},
  1331. {"Analog R2 Playback Mixer", NULL, "Right2 Analog Loopback"},
  1332. {"Analog L2 Playback Mixer", NULL, "Left2 Analog Loopback"},
  1333. {"Analog Voice Playback Mixer", NULL, "Voice Analog Loopback"},
  1334. /* Digital bypass routes */
  1335. {"Right Digital Loopback", "Volume", "TX1 Capture Route"},
  1336. {"Left Digital Loopback", "Volume", "TX1 Capture Route"},
  1337. {"Voice Digital Loopback", "Volume", "TX2 Capture Route"},
  1338. {"Digital R2 Playback Mixer", NULL, "Right Digital Loopback"},
  1339. {"Digital L2 Playback Mixer", NULL, "Left Digital Loopback"},
  1340. {"Digital Voice Playback Mixer", NULL, "Voice Digital Loopback"},
  1341. };
  1342. static int twl4030_set_bias_level(struct snd_soc_component *component,
  1343. enum snd_soc_bias_level level)
  1344. {
  1345. switch (level) {
  1346. case SND_SOC_BIAS_ON:
  1347. break;
  1348. case SND_SOC_BIAS_PREPARE:
  1349. break;
  1350. case SND_SOC_BIAS_STANDBY:
  1351. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
  1352. twl4030_codec_enable(component, 1);
  1353. break;
  1354. case SND_SOC_BIAS_OFF:
  1355. twl4030_codec_enable(component, 0);
  1356. break;
  1357. }
  1358. return 0;
  1359. }
  1360. static void twl4030_constraints(struct twl4030_priv *twl4030,
  1361. struct snd_pcm_substream *mst_substream)
  1362. {
  1363. struct snd_pcm_substream *slv_substream;
  1364. /* Pick the stream, which need to be constrained */
  1365. if (mst_substream == twl4030->master_substream)
  1366. slv_substream = twl4030->slave_substream;
  1367. else if (mst_substream == twl4030->slave_substream)
  1368. slv_substream = twl4030->master_substream;
  1369. else /* This should not happen.. */
  1370. return;
  1371. /* Set the constraints according to the already configured stream */
  1372. snd_pcm_hw_constraint_single(slv_substream->runtime,
  1373. SNDRV_PCM_HW_PARAM_RATE,
  1374. twl4030->rate);
  1375. snd_pcm_hw_constraint_single(slv_substream->runtime,
  1376. SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1377. twl4030->sample_bits);
  1378. snd_pcm_hw_constraint_single(slv_substream->runtime,
  1379. SNDRV_PCM_HW_PARAM_CHANNELS,
  1380. twl4030->channels);
  1381. }
  1382. /* In case of 4 channel mode, the RX1 L/R for playback and the TX2 L/R for
  1383. * capture has to be enabled/disabled. */
  1384. static void twl4030_tdm_enable(struct snd_soc_component *component, int direction,
  1385. int enable)
  1386. {
  1387. u8 reg, mask;
  1388. reg = twl4030_read(component, TWL4030_REG_OPTION);
  1389. if (direction == SNDRV_PCM_STREAM_PLAYBACK)
  1390. mask = TWL4030_ARXL1_VRX_EN | TWL4030_ARXR1_EN;
  1391. else
  1392. mask = TWL4030_ATXL2_VTXL_EN | TWL4030_ATXR2_VTXR_EN;
  1393. if (enable)
  1394. reg |= mask;
  1395. else
  1396. reg &= ~mask;
  1397. twl4030_write(component, TWL4030_REG_OPTION, reg);
  1398. }
  1399. static int twl4030_startup(struct snd_pcm_substream *substream,
  1400. struct snd_soc_dai *dai)
  1401. {
  1402. struct snd_soc_component *component = dai->component;
  1403. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  1404. if (twl4030->master_substream) {
  1405. twl4030->slave_substream = substream;
  1406. /* The DAI has one configuration for playback and capture, so
  1407. * if the DAI has been already configured then constrain this
  1408. * substream to match it. */
  1409. if (twl4030->configured)
  1410. twl4030_constraints(twl4030, twl4030->master_substream);
  1411. } else {
  1412. if (!(twl4030_read(component, TWL4030_REG_CODEC_MODE) &
  1413. TWL4030_OPTION_1)) {
  1414. /* In option2 4 channel is not supported, set the
  1415. * constraint for the first stream for channels, the
  1416. * second stream will 'inherit' this cosntraint */
  1417. snd_pcm_hw_constraint_single(substream->runtime,
  1418. SNDRV_PCM_HW_PARAM_CHANNELS,
  1419. 2);
  1420. }
  1421. twl4030->master_substream = substream;
  1422. }
  1423. return 0;
  1424. }
  1425. static void twl4030_shutdown(struct snd_pcm_substream *substream,
  1426. struct snd_soc_dai *dai)
  1427. {
  1428. struct snd_soc_component *component = dai->component;
  1429. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  1430. if (twl4030->master_substream == substream)
  1431. twl4030->master_substream = twl4030->slave_substream;
  1432. twl4030->slave_substream = NULL;
  1433. /* If all streams are closed, or the remaining stream has not yet
  1434. * been configured than set the DAI as not configured. */
  1435. if (!twl4030->master_substream)
  1436. twl4030->configured = 0;
  1437. else if (!twl4030->master_substream->runtime->channels)
  1438. twl4030->configured = 0;
  1439. /* If the closing substream had 4 channel, do the necessary cleanup */
  1440. if (substream->runtime->channels == 4)
  1441. twl4030_tdm_enable(component, substream->stream, 0);
  1442. }
  1443. static int twl4030_hw_params(struct snd_pcm_substream *substream,
  1444. struct snd_pcm_hw_params *params,
  1445. struct snd_soc_dai *dai)
  1446. {
  1447. struct snd_soc_component *component = dai->component;
  1448. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  1449. u8 mode, old_mode, format, old_format;
  1450. /* If the substream has 4 channel, do the necessary setup */
  1451. if (params_channels(params) == 4) {
  1452. format = twl4030_read(component, TWL4030_REG_AUDIO_IF);
  1453. mode = twl4030_read(component, TWL4030_REG_CODEC_MODE);
  1454. /* Safety check: are we in the correct operating mode and
  1455. * the interface is in TDM mode? */
  1456. if ((mode & TWL4030_OPTION_1) &&
  1457. ((format & TWL4030_AIF_FORMAT) == TWL4030_AIF_FORMAT_TDM))
  1458. twl4030_tdm_enable(component, substream->stream, 1);
  1459. else
  1460. return -EINVAL;
  1461. }
  1462. if (twl4030->configured)
  1463. /* Ignoring hw_params for already configured DAI */
  1464. return 0;
  1465. /* bit rate */
  1466. old_mode = twl4030_read(component,
  1467. TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ;
  1468. mode = old_mode & ~TWL4030_APLL_RATE;
  1469. switch (params_rate(params)) {
  1470. case 8000:
  1471. mode |= TWL4030_APLL_RATE_8000;
  1472. break;
  1473. case 11025:
  1474. mode |= TWL4030_APLL_RATE_11025;
  1475. break;
  1476. case 12000:
  1477. mode |= TWL4030_APLL_RATE_12000;
  1478. break;
  1479. case 16000:
  1480. mode |= TWL4030_APLL_RATE_16000;
  1481. break;
  1482. case 22050:
  1483. mode |= TWL4030_APLL_RATE_22050;
  1484. break;
  1485. case 24000:
  1486. mode |= TWL4030_APLL_RATE_24000;
  1487. break;
  1488. case 32000:
  1489. mode |= TWL4030_APLL_RATE_32000;
  1490. break;
  1491. case 44100:
  1492. mode |= TWL4030_APLL_RATE_44100;
  1493. break;
  1494. case 48000:
  1495. mode |= TWL4030_APLL_RATE_48000;
  1496. break;
  1497. case 96000:
  1498. mode |= TWL4030_APLL_RATE_96000;
  1499. break;
  1500. default:
  1501. dev_err(component->dev, "%s: unknown rate %d\n", __func__,
  1502. params_rate(params));
  1503. return -EINVAL;
  1504. }
  1505. /* sample size */
  1506. old_format = twl4030_read(component, TWL4030_REG_AUDIO_IF);
  1507. format = old_format;
  1508. format &= ~TWL4030_DATA_WIDTH;
  1509. switch (params_width(params)) {
  1510. case 16:
  1511. format |= TWL4030_DATA_WIDTH_16S_16W;
  1512. break;
  1513. case 32:
  1514. format |= TWL4030_DATA_WIDTH_32S_24W;
  1515. break;
  1516. default:
  1517. dev_err(component->dev, "%s: unsupported bits/sample %d\n",
  1518. __func__, params_width(params));
  1519. return -EINVAL;
  1520. }
  1521. if (format != old_format || mode != old_mode) {
  1522. if (twl4030->codec_powered) {
  1523. /*
  1524. * If the codec is powered, than we need to toggle the
  1525. * codec power.
  1526. */
  1527. twl4030_codec_enable(component, 0);
  1528. twl4030_write(component, TWL4030_REG_CODEC_MODE, mode);
  1529. twl4030_write(component, TWL4030_REG_AUDIO_IF, format);
  1530. twl4030_codec_enable(component, 1);
  1531. } else {
  1532. twl4030_write(component, TWL4030_REG_CODEC_MODE, mode);
  1533. twl4030_write(component, TWL4030_REG_AUDIO_IF, format);
  1534. }
  1535. }
  1536. /* Store the important parameters for the DAI configuration and set
  1537. * the DAI as configured */
  1538. twl4030->configured = 1;
  1539. twl4030->rate = params_rate(params);
  1540. twl4030->sample_bits = hw_param_interval(params,
  1541. SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
  1542. twl4030->channels = params_channels(params);
  1543. /* If both playback and capture streams are open, and one of them
  1544. * is setting the hw parameters right now (since we are here), set
  1545. * constraints to the other stream to match the current one. */
  1546. if (twl4030->slave_substream)
  1547. twl4030_constraints(twl4030, substream);
  1548. return 0;
  1549. }
  1550. static int twl4030_set_dai_sysclk(struct snd_soc_dai *codec_dai, int clk_id,
  1551. unsigned int freq, int dir)
  1552. {
  1553. struct snd_soc_component *component = codec_dai->component;
  1554. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  1555. switch (freq) {
  1556. case 19200000:
  1557. case 26000000:
  1558. case 38400000:
  1559. break;
  1560. default:
  1561. dev_err(component->dev, "Unsupported HFCLKIN: %u\n", freq);
  1562. return -EINVAL;
  1563. }
  1564. if ((freq / 1000) != twl4030->sysclk) {
  1565. dev_err(component->dev,
  1566. "Mismatch in HFCLKIN: %u (configured: %u)\n",
  1567. freq, twl4030->sysclk * 1000);
  1568. return -EINVAL;
  1569. }
  1570. return 0;
  1571. }
  1572. static int twl4030_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  1573. {
  1574. struct snd_soc_component *component = codec_dai->component;
  1575. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  1576. u8 old_format, format;
  1577. /* get format */
  1578. old_format = twl4030_read(component, TWL4030_REG_AUDIO_IF);
  1579. format = old_format;
  1580. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  1581. case SND_SOC_DAIFMT_CBP_CFP:
  1582. format &= ~(TWL4030_AIF_SLAVE_EN);
  1583. format &= ~(TWL4030_CLK256FS_EN);
  1584. break;
  1585. case SND_SOC_DAIFMT_CBC_CFC:
  1586. format |= TWL4030_AIF_SLAVE_EN;
  1587. format |= TWL4030_CLK256FS_EN;
  1588. break;
  1589. default:
  1590. return -EINVAL;
  1591. }
  1592. /* interface format */
  1593. format &= ~TWL4030_AIF_FORMAT;
  1594. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1595. case SND_SOC_DAIFMT_I2S:
  1596. format |= TWL4030_AIF_FORMAT_CODEC;
  1597. break;
  1598. case SND_SOC_DAIFMT_DSP_A:
  1599. format |= TWL4030_AIF_FORMAT_TDM;
  1600. break;
  1601. default:
  1602. return -EINVAL;
  1603. }
  1604. if (format != old_format) {
  1605. if (twl4030->codec_powered) {
  1606. /*
  1607. * If the codec is powered, than we need to toggle the
  1608. * codec power.
  1609. */
  1610. twl4030_codec_enable(component, 0);
  1611. twl4030_write(component, TWL4030_REG_AUDIO_IF, format);
  1612. twl4030_codec_enable(component, 1);
  1613. } else {
  1614. twl4030_write(component, TWL4030_REG_AUDIO_IF, format);
  1615. }
  1616. }
  1617. return 0;
  1618. }
  1619. static int twl4030_set_tristate(struct snd_soc_dai *dai, int tristate)
  1620. {
  1621. struct snd_soc_component *component = dai->component;
  1622. u8 reg = twl4030_read(component, TWL4030_REG_AUDIO_IF);
  1623. if (tristate)
  1624. reg |= TWL4030_AIF_TRI_EN;
  1625. else
  1626. reg &= ~TWL4030_AIF_TRI_EN;
  1627. return twl4030_write(component, TWL4030_REG_AUDIO_IF, reg);
  1628. }
  1629. /* In case of voice mode, the RX1 L(VRX) for downlink and the TX2 L/R
  1630. * (VTXL, VTXR) for uplink has to be enabled/disabled. */
  1631. static void twl4030_voice_enable(struct snd_soc_component *component, int direction,
  1632. int enable)
  1633. {
  1634. u8 reg, mask;
  1635. reg = twl4030_read(component, TWL4030_REG_OPTION);
  1636. if (direction == SNDRV_PCM_STREAM_PLAYBACK)
  1637. mask = TWL4030_ARXL1_VRX_EN;
  1638. else
  1639. mask = TWL4030_ATXL2_VTXL_EN | TWL4030_ATXR2_VTXR_EN;
  1640. if (enable)
  1641. reg |= mask;
  1642. else
  1643. reg &= ~mask;
  1644. twl4030_write(component, TWL4030_REG_OPTION, reg);
  1645. }
  1646. static int twl4030_voice_startup(struct snd_pcm_substream *substream,
  1647. struct snd_soc_dai *dai)
  1648. {
  1649. struct snd_soc_component *component = dai->component;
  1650. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  1651. u8 mode;
  1652. /* If the system master clock is not 26MHz, the voice PCM interface is
  1653. * not available.
  1654. */
  1655. if (twl4030->sysclk != 26000) {
  1656. dev_err(component->dev,
  1657. "%s: HFCLKIN is %u KHz, voice interface needs 26MHz\n",
  1658. __func__, twl4030->sysclk);
  1659. return -EINVAL;
  1660. }
  1661. /* If the codec mode is not option2, the voice PCM interface is not
  1662. * available.
  1663. */
  1664. mode = twl4030_read(component, TWL4030_REG_CODEC_MODE)
  1665. & TWL4030_OPT_MODE;
  1666. if (mode != TWL4030_OPTION_2) {
  1667. dev_err(component->dev, "%s: the codec mode is not option2\n",
  1668. __func__);
  1669. return -EINVAL;
  1670. }
  1671. return 0;
  1672. }
  1673. static void twl4030_voice_shutdown(struct snd_pcm_substream *substream,
  1674. struct snd_soc_dai *dai)
  1675. {
  1676. struct snd_soc_component *component = dai->component;
  1677. /* Enable voice digital filters */
  1678. twl4030_voice_enable(component, substream->stream, 0);
  1679. }
  1680. static int twl4030_voice_hw_params(struct snd_pcm_substream *substream,
  1681. struct snd_pcm_hw_params *params,
  1682. struct snd_soc_dai *dai)
  1683. {
  1684. struct snd_soc_component *component = dai->component;
  1685. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  1686. u8 old_mode, mode;
  1687. /* Enable voice digital filters */
  1688. twl4030_voice_enable(component, substream->stream, 1);
  1689. /* bit rate */
  1690. old_mode = twl4030_read(component,
  1691. TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ;
  1692. mode = old_mode;
  1693. switch (params_rate(params)) {
  1694. case 8000:
  1695. mode &= ~(TWL4030_SEL_16K);
  1696. break;
  1697. case 16000:
  1698. mode |= TWL4030_SEL_16K;
  1699. break;
  1700. default:
  1701. dev_err(component->dev, "%s: unknown rate %d\n", __func__,
  1702. params_rate(params));
  1703. return -EINVAL;
  1704. }
  1705. if (mode != old_mode) {
  1706. if (twl4030->codec_powered) {
  1707. /*
  1708. * If the codec is powered, than we need to toggle the
  1709. * codec power.
  1710. */
  1711. twl4030_codec_enable(component, 0);
  1712. twl4030_write(component, TWL4030_REG_CODEC_MODE, mode);
  1713. twl4030_codec_enable(component, 1);
  1714. } else {
  1715. twl4030_write(component, TWL4030_REG_CODEC_MODE, mode);
  1716. }
  1717. }
  1718. return 0;
  1719. }
  1720. static int twl4030_voice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  1721. int clk_id, unsigned int freq, int dir)
  1722. {
  1723. struct snd_soc_component *component = codec_dai->component;
  1724. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  1725. if (freq != 26000000) {
  1726. dev_err(component->dev,
  1727. "%s: HFCLKIN is %u KHz, voice interface needs 26MHz\n",
  1728. __func__, freq / 1000);
  1729. return -EINVAL;
  1730. }
  1731. if ((freq / 1000) != twl4030->sysclk) {
  1732. dev_err(component->dev,
  1733. "Mismatch in HFCLKIN: %u (configured: %u)\n",
  1734. freq, twl4030->sysclk * 1000);
  1735. return -EINVAL;
  1736. }
  1737. return 0;
  1738. }
  1739. static int twl4030_voice_set_dai_fmt(struct snd_soc_dai *codec_dai,
  1740. unsigned int fmt)
  1741. {
  1742. struct snd_soc_component *component = codec_dai->component;
  1743. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  1744. u8 old_format, format;
  1745. /* get format */
  1746. old_format = twl4030_read(component, TWL4030_REG_VOICE_IF);
  1747. format = old_format;
  1748. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  1749. case SND_SOC_DAIFMT_CBP_CFP:
  1750. format &= ~(TWL4030_VIF_SLAVE_EN);
  1751. break;
  1752. case SND_SOC_DAIFMT_CBS_CFS:
  1753. format |= TWL4030_VIF_SLAVE_EN;
  1754. break;
  1755. default:
  1756. return -EINVAL;
  1757. }
  1758. /* clock inversion */
  1759. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1760. case SND_SOC_DAIFMT_IB_NF:
  1761. format &= ~(TWL4030_VIF_FORMAT);
  1762. break;
  1763. case SND_SOC_DAIFMT_NB_IF:
  1764. format |= TWL4030_VIF_FORMAT;
  1765. break;
  1766. default:
  1767. return -EINVAL;
  1768. }
  1769. if (format != old_format) {
  1770. if (twl4030->codec_powered) {
  1771. /*
  1772. * If the codec is powered, than we need to toggle the
  1773. * codec power.
  1774. */
  1775. twl4030_codec_enable(component, 0);
  1776. twl4030_write(component, TWL4030_REG_VOICE_IF, format);
  1777. twl4030_codec_enable(component, 1);
  1778. } else {
  1779. twl4030_write(component, TWL4030_REG_VOICE_IF, format);
  1780. }
  1781. }
  1782. return 0;
  1783. }
  1784. static int twl4030_voice_set_tristate(struct snd_soc_dai *dai, int tristate)
  1785. {
  1786. struct snd_soc_component *component = dai->component;
  1787. u8 reg = twl4030_read(component, TWL4030_REG_VOICE_IF);
  1788. if (tristate)
  1789. reg |= TWL4030_VIF_TRI_EN;
  1790. else
  1791. reg &= ~TWL4030_VIF_TRI_EN;
  1792. return twl4030_write(component, TWL4030_REG_VOICE_IF, reg);
  1793. }
  1794. #define TWL4030_RATES (SNDRV_PCM_RATE_8000_48000)
  1795. #define TWL4030_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1796. static const struct snd_soc_dai_ops twl4030_dai_hifi_ops = {
  1797. .startup = twl4030_startup,
  1798. .shutdown = twl4030_shutdown,
  1799. .hw_params = twl4030_hw_params,
  1800. .set_sysclk = twl4030_set_dai_sysclk,
  1801. .set_fmt = twl4030_set_dai_fmt,
  1802. .set_tristate = twl4030_set_tristate,
  1803. };
  1804. static const struct snd_soc_dai_ops twl4030_dai_voice_ops = {
  1805. .startup = twl4030_voice_startup,
  1806. .shutdown = twl4030_voice_shutdown,
  1807. .hw_params = twl4030_voice_hw_params,
  1808. .set_sysclk = twl4030_voice_set_dai_sysclk,
  1809. .set_fmt = twl4030_voice_set_dai_fmt,
  1810. .set_tristate = twl4030_voice_set_tristate,
  1811. };
  1812. static struct snd_soc_dai_driver twl4030_dai[] = {
  1813. {
  1814. .name = "twl4030-hifi",
  1815. .playback = {
  1816. .stream_name = "HiFi Playback",
  1817. .channels_min = 2,
  1818. .channels_max = 4,
  1819. .rates = TWL4030_RATES | SNDRV_PCM_RATE_96000,
  1820. .formats = TWL4030_FORMATS,
  1821. .sig_bits = 24,},
  1822. .capture = {
  1823. .stream_name = "HiFi Capture",
  1824. .channels_min = 2,
  1825. .channels_max = 4,
  1826. .rates = TWL4030_RATES,
  1827. .formats = TWL4030_FORMATS,
  1828. .sig_bits = 24,},
  1829. .ops = &twl4030_dai_hifi_ops,
  1830. },
  1831. {
  1832. .name = "twl4030-voice",
  1833. .playback = {
  1834. .stream_name = "Voice Playback",
  1835. .channels_min = 1,
  1836. .channels_max = 1,
  1837. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
  1838. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  1839. .capture = {
  1840. .stream_name = "Voice Capture",
  1841. .channels_min = 1,
  1842. .channels_max = 2,
  1843. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
  1844. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  1845. .ops = &twl4030_dai_voice_ops,
  1846. },
  1847. };
  1848. static int twl4030_soc_probe(struct snd_soc_component *component)
  1849. {
  1850. struct twl4030_priv *twl4030;
  1851. twl4030 = devm_kzalloc(component->dev, sizeof(struct twl4030_priv),
  1852. GFP_KERNEL);
  1853. if (!twl4030)
  1854. return -ENOMEM;
  1855. snd_soc_component_set_drvdata(component, twl4030);
  1856. /* Set the defaults, and power up the codec */
  1857. twl4030->sysclk = twl4030_audio_get_mclk() / 1000;
  1858. twl4030_init_chip(component);
  1859. return 0;
  1860. }
  1861. static void twl4030_soc_remove(struct snd_soc_component *component)
  1862. {
  1863. struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
  1864. struct twl4030_board_params *board_params = twl4030->board_params;
  1865. if (board_params && board_params->hs_extmute &&
  1866. gpio_is_valid(board_params->hs_extmute_gpio))
  1867. gpio_free(board_params->hs_extmute_gpio);
  1868. }
  1869. static const struct snd_soc_component_driver soc_component_dev_twl4030 = {
  1870. .probe = twl4030_soc_probe,
  1871. .remove = twl4030_soc_remove,
  1872. .read = twl4030_read,
  1873. .write = twl4030_write,
  1874. .set_bias_level = twl4030_set_bias_level,
  1875. .controls = twl4030_snd_controls,
  1876. .num_controls = ARRAY_SIZE(twl4030_snd_controls),
  1877. .dapm_widgets = twl4030_dapm_widgets,
  1878. .num_dapm_widgets = ARRAY_SIZE(twl4030_dapm_widgets),
  1879. .dapm_routes = intercon,
  1880. .num_dapm_routes = ARRAY_SIZE(intercon),
  1881. .use_pmdown_time = 1,
  1882. .endianness = 1,
  1883. };
  1884. static int twl4030_codec_probe(struct platform_device *pdev)
  1885. {
  1886. return devm_snd_soc_register_component(&pdev->dev,
  1887. &soc_component_dev_twl4030,
  1888. twl4030_dai, ARRAY_SIZE(twl4030_dai));
  1889. }
  1890. MODULE_ALIAS("platform:twl4030-codec");
  1891. static struct platform_driver twl4030_codec_driver = {
  1892. .probe = twl4030_codec_probe,
  1893. .driver = {
  1894. .name = "twl4030-codec",
  1895. },
  1896. };
  1897. module_platform_driver(twl4030_codec_driver);
  1898. MODULE_DESCRIPTION("ASoC TWL4030 codec driver");
  1899. MODULE_AUTHOR("Steve Sakoman");
  1900. MODULE_LICENSE("GPL");