twl4030.c 70 KB

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