wcd938x.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/slab.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/device.h>
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/component.h>
  12. #include <sound/soc.h>
  13. #include <sound/tlv.h>
  14. #include <soc/soundwire.h>
  15. #include <linux/regmap.h>
  16. #include <sound/soc.h>
  17. #include <sound/soc-dapm.h>
  18. #include <asoc/wcdcal-hwdep.h>
  19. #include <asoc/msm-cdc-pinctrl.h>
  20. #include <asoc/msm-cdc-supply.h>
  21. #include <dt-bindings/sound/audio-codec-port-types.h>
  22. #include "internal.h"
  23. #include "wcd938x-registers.h"
  24. #define WCD938X_DRV_NAME "wcd938x_codec"
  25. #define NUM_SWRS_DT_PARAMS 5
  26. #define WCD938X_VERSION_1_0 1
  27. #define WCD938X_VERSION_ENTRY_SIZE 32
  28. #define EAR_RX_PATH_AUX 1
  29. #define ADC_MODE_VAL_HIFI 0x01
  30. #define ADC_MODE_VAL_LO_HIF 0x02
  31. #define ADC_MODE_VAL_NORMAL 0x03
  32. #define ADC_MODE_VAL_LP 0x05
  33. #define ADC_MODE_VAL_ULP1 0x09
  34. #define ADC_MODE_VAL_ULP2 0x0B
  35. enum {
  36. WCD9380 = 0,
  37. WCD9385 = 5,
  38. };
  39. enum {
  40. CODEC_TX = 0,
  41. CODEC_RX,
  42. };
  43. enum {
  44. WCD_ADC1 = 0,
  45. WCD_ADC2,
  46. WCD_ADC3,
  47. WCD_ADC4,
  48. ALLOW_BUCK_DISABLE,
  49. HPH_COMP_DELAY,
  50. HPH_PA_DELAY,
  51. };
  52. enum {
  53. ADC_MODE_INVALID = 0,
  54. ADC_MODE_HIFI,
  55. ADC_MODE_LO_HIF,
  56. ADC_MODE_NORMAL,
  57. ADC_MODE_LP,
  58. ADC_MODE_ULP1,
  59. ADC_MODE_ULP2,
  60. };
  61. static const DECLARE_TLV_DB_SCALE(line_gain, 0, 7, 1);
  62. static const DECLARE_TLV_DB_SCALE(analog_gain, 0, 25, 1);
  63. static int wcd938x_handle_post_irq(void *data);
  64. static int wcd938x_reset(struct device *dev);
  65. static int wcd938x_reset_low(struct device *dev);
  66. static const struct regmap_irq wcd938x_irqs[WCD938X_NUM_IRQS] = {
  67. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_BUTTON_PRESS_DET, 0, 0x01),
  68. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_BUTTON_RELEASE_DET, 0, 0x02),
  69. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_ELECT_INS_REM_DET, 0, 0x04),
  70. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_ELECT_INS_REM_LEG_DET, 0, 0x08),
  71. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_SW_DET, 0, 0x10),
  72. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_OCP_INT, 0, 0x20),
  73. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_CNP_INT, 0, 0x40),
  74. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_OCP_INT, 0, 0x80),
  75. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_CNP_INT, 1, 0x01),
  76. REGMAP_IRQ_REG(WCD938X_IRQ_EAR_CNP_INT, 1, 0x02),
  77. REGMAP_IRQ_REG(WCD938X_IRQ_EAR_SCD_INT, 1, 0x04),
  78. REGMAP_IRQ_REG(WCD938X_IRQ_AUX_CNP_INT, 1, 0x08),
  79. REGMAP_IRQ_REG(WCD938X_IRQ_AUX_SCD_INT, 1, 0x10),
  80. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_PDM_WD_INT, 1, 0x20),
  81. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_PDM_WD_INT, 1, 0x40),
  82. REGMAP_IRQ_REG(WCD938X_IRQ_AUX_PDM_WD_INT, 1, 0x80),
  83. REGMAP_IRQ_REG(WCD938X_IRQ_LDORT_SCD_INT, 2, 0x01),
  84. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_MOISTURE_INT, 2, 0x02),
  85. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_SURGE_DET_INT, 2, 0x04),
  86. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_SURGE_DET_INT, 2, 0x08),
  87. };
  88. static struct regmap_irq_chip wcd938x_regmap_irq_chip = {
  89. .name = "wcd938x",
  90. .irqs = wcd938x_irqs,
  91. .num_irqs = ARRAY_SIZE(wcd938x_irqs),
  92. .num_regs = 3,
  93. .status_base = WCD938X_DIGITAL_INTR_STATUS_0,
  94. .mask_base = WCD938X_DIGITAL_INTR_MASK_0,
  95. .type_base = WCD938X_DIGITAL_INTR_LEVEL_0,
  96. .ack_base = WCD938X_DIGITAL_INTR_CLEAR_0,
  97. .use_ack = 1,
  98. .runtime_pm = false,
  99. .handle_post_irq = wcd938x_handle_post_irq,
  100. .irq_drv_data = NULL,
  101. };
  102. static int wcd938x_handle_post_irq(void *data)
  103. {
  104. struct wcd938x_priv *wcd938x = data;
  105. u32 sts1 = 0, sts2 = 0, sts3 = 0;
  106. regmap_read(wcd938x->regmap, WCD938X_DIGITAL_INTR_STATUS_0, &sts1);
  107. regmap_read(wcd938x->regmap, WCD938X_DIGITAL_INTR_STATUS_1, &sts2);
  108. regmap_read(wcd938x->regmap, WCD938X_DIGITAL_INTR_STATUS_2, &sts3);
  109. wcd938x->tx_swr_dev->slave_irq_pending =
  110. ((sts1 || sts2 || sts3) ? true : false);
  111. return IRQ_HANDLED;
  112. }
  113. static int wcd938x_init_reg(struct snd_soc_component *component)
  114. {
  115. snd_soc_component_update_bits(component, WCD938X_SLEEP_CTL, 0x0E, 0x0E);
  116. snd_soc_component_update_bits(component, WCD938X_SLEEP_CTL, 0x80, 0x80);
  117. /* 1 msec delay as per HW requirement */
  118. usleep_range(1000, 1010);
  119. snd_soc_component_update_bits(component, WCD938X_SLEEP_CTL, 0x40, 0x40);
  120. /* 1 msec delay as per HW requirement */
  121. usleep_range(1000, 1010);
  122. snd_soc_component_update_bits(component, WCD938X_LDORXTX_CONFIG,
  123. 0x10, 0x00);
  124. snd_soc_component_update_bits(component, WCD938X_BIAS_VBG_FINE_ADJ,
  125. 0xF0, 0x80);
  126. snd_soc_component_update_bits(component, WCD938X_ANA_BIAS, 0x80, 0x80);
  127. snd_soc_component_update_bits(component, WCD938X_ANA_BIAS, 0x40, 0x40);
  128. /* 10 msec delay as per HW requirement */
  129. usleep_range(10000, 10010);
  130. snd_soc_component_update_bits(component, WCD938X_ANA_BIAS, 0x40, 0x00);
  131. snd_soc_component_update_bits(component, WCD938X_HPH_OCP_CTL,
  132. 0xFF, 0x3A);
  133. snd_soc_component_update_bits(component, WCD938X_RX_OCP_CTL,
  134. 0x0F, 0x02);
  135. snd_soc_component_update_bits(component, WCD938X_HPH_R_TEST,
  136. 0x01, 0x01);
  137. snd_soc_component_update_bits(component, WCD938X_HPH_L_TEST,
  138. 0x01, 0x01);
  139. snd_soc_component_update_bits(component,
  140. WCD938X_HPH_NEW_INT_RDAC_GAIN_CTL,
  141. 0xF0, 0x00);
  142. snd_soc_component_update_bits(component,
  143. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_L_NEW,
  144. 0x1F, 0x15);
  145. snd_soc_component_update_bits(component,
  146. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R_NEW,
  147. 0x1F, 0x15);
  148. snd_soc_component_update_bits(component, WCD938X_HPH_REFBUFF_UHQA_CTL,
  149. 0xC0, 0x80);
  150. snd_soc_component_update_bits(component, WCD938X_DIGITAL_CDC_DMIC_CTL,
  151. 0x02, 0x02);
  152. snd_soc_component_update_bits(component,
  153. WCD938X_TX_COM_NEW_INT_TXFE_ICTRL_STG2CASC_ULP,
  154. 0xFF, 0x14);
  155. snd_soc_component_update_bits(component,
  156. WCD938X_TX_COM_NEW_INT_TXFE_ICTRL_STG2MAIN_ULP,
  157. 0x1F, 0x08);
  158. snd_soc_component_update_bits(component,
  159. WCD938X_DIGITAL_TX_REQ_FB_CTL_0, 0xFF, 0x55);
  160. snd_soc_component_update_bits(component,
  161. WCD938X_DIGITAL_TX_REQ_FB_CTL_1, 0xFF, 0x44);
  162. snd_soc_component_update_bits(component,
  163. WCD938X_DIGITAL_TX_REQ_FB_CTL_2, 0xFF, 0x11);
  164. snd_soc_component_update_bits(component,
  165. WCD938X_DIGITAL_TX_REQ_FB_CTL_3, 0xFF, 0x00);
  166. snd_soc_component_update_bits(component,
  167. WCD938X_DIGITAL_TX_REQ_FB_CTL_4, 0xFF, 0x00);
  168. return 0;
  169. }
  170. static int wcd938x_set_port_params(struct snd_soc_component *component,
  171. u8 slv_prt_type, u8 *port_id, u8 *num_ch,
  172. u8 *ch_mask, u32 *ch_rate,
  173. u8 *port_type, u8 path)
  174. {
  175. int i, j;
  176. u8 num_ports = 0;
  177. struct codec_port_info (*map)[MAX_PORT][MAX_CH_PER_PORT];
  178. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  179. switch (path) {
  180. case CODEC_RX:
  181. map = &wcd938x->rx_port_mapping;
  182. num_ports = wcd938x->num_rx_ports;
  183. break;
  184. case CODEC_TX:
  185. map = &wcd938x->tx_port_mapping;
  186. num_ports = wcd938x->num_tx_ports;
  187. break;
  188. default:
  189. dev_err(component->dev, "%s Invalid path selected %u\n",
  190. __func__, path);
  191. return -EINVAL;
  192. }
  193. for (i = 0; i <= num_ports; i++) {
  194. for (j = 0; j < MAX_CH_PER_PORT; j++) {
  195. if ((*map)[i][j].slave_port_type == slv_prt_type)
  196. goto found;
  197. }
  198. }
  199. found:
  200. if (i > num_ports || j == MAX_CH_PER_PORT) {
  201. dev_err(component->dev, "%s Failed to find slave port for type %u\n",
  202. __func__, slv_prt_type);
  203. return -EINVAL;
  204. }
  205. *port_id = i;
  206. *num_ch = (*map)[i][j].num_ch;
  207. *ch_mask = (*map)[i][j].ch_mask;
  208. *ch_rate = (*map)[i][j].ch_rate;
  209. *port_type = (*map)[i][j].master_port_type;
  210. return 0;
  211. }
  212. static int wcd938x_parse_port_mapping(struct device *dev,
  213. char *prop, u8 path)
  214. {
  215. u32 *dt_array, map_size, map_length;
  216. u32 port_num = 0, ch_mask, ch_rate, old_port_num = 0;
  217. u32 slave_port_type, master_port_type;
  218. u32 i, ch_iter = 0;
  219. int ret = 0;
  220. u8 *num_ports = NULL;
  221. struct codec_port_info (*map)[MAX_PORT][MAX_CH_PER_PORT];
  222. struct wcd938x_priv *wcd938x = dev_get_drvdata(dev);
  223. switch (path) {
  224. case CODEC_RX:
  225. map = &wcd938x->rx_port_mapping;
  226. num_ports = &wcd938x->num_rx_ports;
  227. break;
  228. case CODEC_TX:
  229. map = &wcd938x->tx_port_mapping;
  230. num_ports = &wcd938x->num_tx_ports;
  231. break;
  232. default:
  233. dev_err(dev, "%s Invalid path selected %u\n",
  234. __func__, path);
  235. return -EINVAL;
  236. }
  237. if (!of_find_property(dev->of_node, prop,
  238. &map_size)) {
  239. dev_err(dev, "missing port mapping prop %s\n", prop);
  240. ret = -EINVAL;
  241. goto err_port_map;
  242. }
  243. map_length = map_size / (NUM_SWRS_DT_PARAMS * sizeof(u32));
  244. dt_array = kzalloc(map_size, GFP_KERNEL);
  245. if (!dt_array) {
  246. ret = -ENOMEM;
  247. goto err_alloc;
  248. }
  249. ret = of_property_read_u32_array(dev->of_node, prop, dt_array,
  250. NUM_SWRS_DT_PARAMS * map_length);
  251. if (ret) {
  252. dev_err(dev, "%s: Failed to read port mapping from prop %s\n",
  253. __func__, prop);
  254. goto err_pdata_fail;
  255. }
  256. for (i = 0; i < map_length; i++) {
  257. port_num = dt_array[NUM_SWRS_DT_PARAMS * i];
  258. slave_port_type = dt_array[NUM_SWRS_DT_PARAMS * i + 1];
  259. ch_mask = dt_array[NUM_SWRS_DT_PARAMS * i + 2];
  260. ch_rate = dt_array[NUM_SWRS_DT_PARAMS * i + 3];
  261. master_port_type = dt_array[NUM_SWRS_DT_PARAMS * i + 4];
  262. if (port_num != old_port_num)
  263. ch_iter = 0;
  264. (*map)[port_num][ch_iter].slave_port_type = slave_port_type;
  265. (*map)[port_num][ch_iter].ch_mask = ch_mask;
  266. (*map)[port_num][ch_iter].master_port_type = master_port_type;
  267. (*map)[port_num][ch_iter].num_ch = __sw_hweight8(ch_mask);
  268. (*map)[port_num][ch_iter++].ch_rate = ch_rate;
  269. old_port_num = port_num;
  270. }
  271. *num_ports = port_num;
  272. kfree(dt_array);
  273. return 0;
  274. err_pdata_fail:
  275. kfree(dt_array);
  276. err_alloc:
  277. err_port_map:
  278. return ret;
  279. }
  280. static int wcd938x_tx_connect_port(struct snd_soc_component *component,
  281. u8 slv_port_type, u8 enable)
  282. {
  283. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  284. u8 port_id, num_ch, ch_mask, port_type;
  285. u32 ch_rate;
  286. u8 num_port = 1;
  287. int ret = 0;
  288. ret = wcd938x_set_port_params(component, slv_port_type, &port_id,
  289. &num_ch, &ch_mask, &ch_rate,
  290. &port_type, CODEC_TX);
  291. if (ret)
  292. return ret;
  293. if (enable)
  294. ret = swr_connect_port(wcd938x->tx_swr_dev, &port_id,
  295. num_port, &ch_mask, &ch_rate,
  296. &num_ch, &port_type);
  297. else
  298. ret = swr_disconnect_port(wcd938x->tx_swr_dev, &port_id,
  299. num_port, &ch_mask, &port_type);
  300. return ret;
  301. }
  302. static int wcd938x_rx_connect_port(struct snd_soc_component *component,
  303. u8 slv_port_type, u8 enable)
  304. {
  305. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  306. u8 port_id, num_ch, ch_mask, port_type;
  307. u32 ch_rate;
  308. u8 num_port = 1;
  309. int ret = 0;
  310. ret = wcd938x_set_port_params(component, slv_port_type, &port_id,
  311. &num_ch, &ch_mask, &ch_rate,
  312. &port_type, CODEC_RX);
  313. if (ret)
  314. return ret;
  315. if (enable)
  316. ret = swr_connect_port(wcd938x->rx_swr_dev, &port_id,
  317. num_port, &ch_mask, &ch_rate,
  318. &num_ch, &port_type);
  319. else
  320. ret = swr_disconnect_port(wcd938x->rx_swr_dev, &port_id,
  321. num_port, &ch_mask, &port_type);
  322. return ret;
  323. }
  324. static int wcd938x_rx_clk_enable(struct snd_soc_component *component)
  325. {
  326. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  327. if (wcd938x->rx_clk_cnt == 0) {
  328. snd_soc_component_update_bits(component,
  329. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x01, 0x01);
  330. snd_soc_component_update_bits(component,
  331. WCD938X_ANA_RX_SUPPLIES, 0x01, 0x01);
  332. snd_soc_component_update_bits(component,
  333. WCD938X_DIGITAL_CDC_RX0_CTL, 0x40, 0x00);
  334. snd_soc_component_update_bits(component,
  335. WCD938X_DIGITAL_CDC_RX1_CTL, 0x40, 0x00);
  336. snd_soc_component_update_bits(component,
  337. WCD938X_DIGITAL_CDC_RX2_CTL, 0x40, 0x00);
  338. snd_soc_component_update_bits(component,
  339. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x02, 0x02);
  340. snd_soc_component_update_bits(component,
  341. WCD938X_AUX_AUXPA, 0x10, 0x10);
  342. }
  343. wcd938x->rx_clk_cnt++;
  344. return 0;
  345. }
  346. static int wcd938x_rx_clk_disable(struct snd_soc_component *component)
  347. {
  348. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  349. wcd938x->rx_clk_cnt--;
  350. if (wcd938x->rx_clk_cnt == 0) {
  351. snd_soc_component_update_bits(component,
  352. WCD938X_ANA_RX_SUPPLIES, 0x40, 0x00);
  353. snd_soc_component_update_bits(component,
  354. WCD938X_ANA_RX_SUPPLIES, 0x80, 0x00);
  355. snd_soc_component_update_bits(component,
  356. WCD938X_ANA_RX_SUPPLIES, 0x01, 0x00);
  357. snd_soc_component_update_bits(component,
  358. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x02, 0x00);
  359. snd_soc_component_update_bits(component,
  360. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x01, 0x00);
  361. }
  362. return 0;
  363. }
  364. /*
  365. * wcd938x_soc_get_mbhc: get wcd938x_mbhc handle of corresponding component
  366. * @component: handle to snd_soc_component *
  367. *
  368. * return wcd938x_mbhc handle or error code in case of failure
  369. */
  370. struct wcd938x_mbhc *wcd938x_soc_get_mbhc(struct snd_soc_component *component)
  371. {
  372. struct wcd938x_priv *wcd938x;
  373. if (!component) {
  374. pr_err("%s: Invalid params, NULL component\n", __func__);
  375. return NULL;
  376. }
  377. wcd938x = snd_soc_component_get_drvdata(component);
  378. if (!wcd938x) {
  379. pr_err("%s: wcd938x is NULL\n", __func__);
  380. return NULL;
  381. }
  382. return wcd938x->mbhc;
  383. }
  384. EXPORT_SYMBOL(wcd938x_soc_get_mbhc);
  385. static int wcd938x_codec_hphl_dac_event(struct snd_soc_dapm_widget *w,
  386. struct snd_kcontrol *kcontrol,
  387. int event)
  388. {
  389. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  390. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  391. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  392. w->name, event);
  393. switch (event) {
  394. case SND_SOC_DAPM_PRE_PMU:
  395. wcd938x_rx_clk_enable(component);
  396. snd_soc_component_update_bits(component,
  397. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x01, 0x01);
  398. snd_soc_component_update_bits(component,
  399. WCD938X_DIGITAL_CDC_HPH_GAIN_CTL, 0x04, 0x04);
  400. snd_soc_component_update_bits(component,
  401. WCD938X_HPH_RDAC_CLK_CTL1, 0x80, 0x00);
  402. break;
  403. case SND_SOC_DAPM_POST_PMU:
  404. snd_soc_component_update_bits(component,
  405. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_L, 0x0F, 0x02);
  406. if (wcd938x->comp1_enable) {
  407. snd_soc_component_update_bits(component,
  408. WCD938X_DIGITAL_CDC_COMP_CTL_0, 0x02, 0x02);
  409. /* 5msec compander delay as per HW requirement */
  410. if (!wcd938x->comp2_enable ||
  411. (snd_soc_component_read32(component,
  412. WCD938X_DIGITAL_CDC_COMP_CTL_0) & 0x01))
  413. usleep_range(5000, 5010);
  414. snd_soc_component_update_bits(component,
  415. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x00);
  416. } else {
  417. snd_soc_component_update_bits(component,
  418. WCD938X_DIGITAL_CDC_COMP_CTL_0,
  419. 0x02, 0x00);
  420. snd_soc_component_update_bits(component,
  421. WCD938X_HPH_L_EN, 0x20, 0x20);
  422. }
  423. break;
  424. case SND_SOC_DAPM_POST_PMD:
  425. snd_soc_component_update_bits(component,
  426. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R,
  427. 0x0F, 0x01);
  428. break;
  429. }
  430. return 0;
  431. }
  432. static int wcd938x_codec_hphr_dac_event(struct snd_soc_dapm_widget *w,
  433. struct snd_kcontrol *kcontrol,
  434. int event)
  435. {
  436. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  437. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  438. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  439. w->name, event);
  440. switch (event) {
  441. case SND_SOC_DAPM_PRE_PMU:
  442. wcd938x_rx_clk_enable(component);
  443. snd_soc_component_update_bits(component,
  444. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x02, 0x02);
  445. snd_soc_component_update_bits(component,
  446. WCD938X_DIGITAL_CDC_HPH_GAIN_CTL, 0x08, 0x08);
  447. snd_soc_component_update_bits(component,
  448. WCD938X_HPH_RDAC_CLK_CTL1, 0x80, 0x00);
  449. break;
  450. case SND_SOC_DAPM_POST_PMU:
  451. snd_soc_component_update_bits(component,
  452. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R, 0x0F, 0x02);
  453. if (wcd938x->comp2_enable) {
  454. snd_soc_component_update_bits(component,
  455. WCD938X_DIGITAL_CDC_COMP_CTL_0, 0x01, 0x01);
  456. /* 5msec compander delay as per HW requirement */
  457. if (!wcd938x->comp1_enable ||
  458. (snd_soc_component_read32(component,
  459. WCD938X_DIGITAL_CDC_COMP_CTL_0) & 0x02))
  460. usleep_range(5000, 5010);
  461. snd_soc_component_update_bits(component,
  462. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x00);
  463. } else {
  464. snd_soc_component_update_bits(component,
  465. WCD938X_DIGITAL_CDC_COMP_CTL_0,
  466. 0x01, 0x00);
  467. snd_soc_component_update_bits(component,
  468. WCD938X_HPH_R_EN, 0x20, 0x20);
  469. }
  470. break;
  471. case SND_SOC_DAPM_POST_PMD:
  472. snd_soc_component_update_bits(component,
  473. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R,
  474. 0x0F, 0x01);
  475. break;
  476. }
  477. return 0;
  478. }
  479. static int wcd938x_codec_ear_dac_event(struct snd_soc_dapm_widget *w,
  480. struct snd_kcontrol *kcontrol,
  481. int event)
  482. {
  483. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  484. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  485. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  486. w->name, event);
  487. switch (event) {
  488. case SND_SOC_DAPM_PRE_PMU:
  489. wcd938x_rx_clk_enable(component);
  490. snd_soc_component_update_bits(component,
  491. WCD938X_DIGITAL_CDC_HPH_GAIN_CTL, 0x04, 0x04);
  492. snd_soc_component_update_bits(component,
  493. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x01, 0x01);
  494. snd_soc_component_update_bits(component,
  495. WCD938X_DIGITAL_CDC_COMP_CTL_0, 0x02, 0x02);
  496. /* 5 msec delay as per HW requirement */
  497. usleep_range(5000, 5010);
  498. if (wcd938x->flyback_cur_det_disable == 0)
  499. snd_soc_component_update_bits(component,
  500. WCD938X_FLYBACK_EN,
  501. 0x04, 0x00);
  502. wcd938x->flyback_cur_det_disable++;
  503. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  504. WCD_CLSH_EVENT_PRE_DAC,
  505. WCD_CLSH_STATE_EAR,
  506. wcd938x->hph_mode);
  507. break;
  508. case SND_SOC_DAPM_POST_PMD:
  509. break;
  510. };
  511. return 0;
  512. }
  513. static int wcd938x_codec_aux_dac_event(struct snd_soc_dapm_widget *w,
  514. struct snd_kcontrol *kcontrol,
  515. int event)
  516. {
  517. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  518. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  519. int ret = 0;
  520. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  521. w->name, event);
  522. switch (event) {
  523. case SND_SOC_DAPM_PRE_PMU:
  524. wcd938x_rx_clk_enable(component);
  525. snd_soc_component_update_bits(component,
  526. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x04, 0x04);
  527. snd_soc_component_update_bits(component,
  528. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x04, 0x04);
  529. snd_soc_component_update_bits(component,
  530. WCD938X_DIGITAL_CDC_AUX_GAIN_CTL, 0x01, 0x01);
  531. if (wcd938x->flyback_cur_det_disable == 0)
  532. snd_soc_component_update_bits(component,
  533. WCD938X_FLYBACK_EN,
  534. 0x04, 0x00);
  535. wcd938x->flyback_cur_det_disable++;
  536. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  537. WCD_CLSH_EVENT_PRE_DAC,
  538. WCD_CLSH_STATE_AUX,
  539. wcd938x->hph_mode);
  540. break;
  541. case SND_SOC_DAPM_POST_PMD:
  542. snd_soc_component_update_bits(component,
  543. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x04, 0x00);
  544. break;
  545. };
  546. return ret;
  547. }
  548. static int wcd938x_codec_enable_hphr_pa(struct snd_soc_dapm_widget *w,
  549. struct snd_kcontrol *kcontrol,
  550. int event)
  551. {
  552. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  553. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  554. int ret = 0;
  555. int hph_mode = wcd938x->hph_mode;
  556. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  557. w->name, event);
  558. switch (event) {
  559. case SND_SOC_DAPM_PRE_PMU:
  560. if (wcd938x->ldoh)
  561. snd_soc_component_update_bits(component,
  562. WCD938X_LDOH_MODE,
  563. 0x80, 0x80);
  564. if (wcd938x->update_wcd_event)
  565. wcd938x->update_wcd_event(wcd938x->handle,
  566. WCD_BOLERO_EVT_RX_MUTE,
  567. (WCD_RX2 << 0x10 | 0x1));
  568. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  569. wcd938x->rx_swr_dev->dev_num,
  570. true);
  571. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  572. WCD_CLSH_EVENT_PRE_DAC,
  573. WCD_CLSH_STATE_HPHR,
  574. hph_mode);
  575. wcd_clsh_set_hph_mode(component, CLS_H_HIFI);
  576. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  577. 0x10, 0x10);
  578. wcd_clsh_set_hph_mode(component, hph_mode);
  579. /* 100 usec delay as per HW requirement */
  580. usleep_range(100, 110);
  581. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  582. snd_soc_component_update_bits(component,
  583. WCD938X_DIGITAL_PDM_WD_CTL1, 0x17, 0x13);
  584. break;
  585. case SND_SOC_DAPM_POST_PMU:
  586. /*
  587. * 7ms sleep is required if compander is enabled as per
  588. * HW requirement. If compander is disabled, then
  589. * 20ms delay is required.
  590. */
  591. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  592. if (!wcd938x->comp2_enable)
  593. usleep_range(20000, 20100);
  594. else
  595. usleep_range(7000, 7100);
  596. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  597. }
  598. snd_soc_component_update_bits(component,
  599. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x02);
  600. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  601. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  602. snd_soc_component_update_bits(component,
  603. WCD938X_ANA_RX_SUPPLIES, 0x02, 0x02);
  604. if (wcd938x->update_wcd_event)
  605. wcd938x->update_wcd_event(wcd938x->handle,
  606. WCD_BOLERO_EVT_RX_MUTE,
  607. (WCD_RX2 << 0x10));
  608. wcd_enable_irq(&wcd938x->irq_info,
  609. WCD938X_IRQ_HPHR_PDM_WD_INT);
  610. break;
  611. case SND_SOC_DAPM_PRE_PMD:
  612. wcd_disable_irq(&wcd938x->irq_info,
  613. WCD938X_IRQ_HPHR_PDM_WD_INT);
  614. if (wcd938x->update_wcd_event)
  615. wcd938x->update_wcd_event(wcd938x->handle,
  616. WCD_BOLERO_EVT_RX_MUTE,
  617. (WCD_RX2 << 0x10 | 0x1));
  618. if (wcd938x->update_wcd_event)
  619. wcd938x->update_wcd_event(wcd938x->handle,
  620. WCD_BOLERO_EVT_RX_COMPANDER_SOFT_RST,
  621. (WCD_RX2 << 0x10));
  622. /* 7 msec delay as per HW requirement */
  623. usleep_range(7000, 7100);
  624. if (wcd938x->update_wcd_event)
  625. wcd938x->update_wcd_event(wcd938x->handle,
  626. WCD_BOLERO_EVT_RX_MUTE,
  627. (WCD_RX2 << 0x10 | 0x0));
  628. /* 20 msec delay as per HW requirement */
  629. usleep_range(21000, 21100);
  630. if (wcd938x->update_wcd_event)
  631. wcd938x->update_wcd_event(wcd938x->handle,
  632. WCD_BOLERO_EVT_RX_MUTE,
  633. (WCD_RX2 << 0x10 | 0x1));
  634. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  635. 0x40, 0x00);
  636. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  637. WCD_EVENT_PRE_HPHR_PA_OFF,
  638. &wcd938x->mbhc->wcd_mbhc);
  639. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  640. break;
  641. case SND_SOC_DAPM_POST_PMD:
  642. /*
  643. * 7ms sleep is required if compander is enabled as per
  644. * HW requirement. If compander is disabled, then
  645. * 20ms delay is required.
  646. */
  647. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  648. if (!wcd938x->comp2_enable)
  649. usleep_range(20000, 20100);
  650. else
  651. usleep_range(7000, 7100);
  652. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  653. }
  654. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  655. WCD_EVENT_POST_HPHR_PA_OFF,
  656. &wcd938x->mbhc->wcd_mbhc);
  657. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  658. 0x10, 0x00);
  659. /* 20 msec delay as per HW requirement */
  660. usleep_range(20000, 20100);
  661. snd_soc_component_update_bits(component,
  662. WCD938X_DIGITAL_PDM_WD_CTL1, 0x17, 0x00);
  663. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  664. WCD_CLSH_EVENT_POST_PA,
  665. WCD_CLSH_STATE_HPHR,
  666. hph_mode);
  667. if (wcd938x->ldoh)
  668. snd_soc_component_update_bits(component,
  669. WCD938X_LDOH_MODE,
  670. 0x80, 0x00);
  671. break;
  672. };
  673. return ret;
  674. }
  675. static int wcd938x_codec_enable_hphl_pa(struct snd_soc_dapm_widget *w,
  676. struct snd_kcontrol *kcontrol,
  677. int event)
  678. {
  679. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  680. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  681. int ret = 0;
  682. int hph_mode = wcd938x->hph_mode;
  683. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  684. w->name, event);
  685. switch (event) {
  686. case SND_SOC_DAPM_PRE_PMU:
  687. if (wcd938x->ldoh)
  688. snd_soc_component_update_bits(component,
  689. WCD938X_LDOH_MODE,
  690. 0x80, 0x80);
  691. if (wcd938x->update_wcd_event)
  692. wcd938x->update_wcd_event(wcd938x->handle,
  693. WCD_BOLERO_EVT_RX_MUTE,
  694. (WCD_RX1 << 0x10 | 0x01));
  695. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  696. wcd938x->rx_swr_dev->dev_num,
  697. true);
  698. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  699. WCD_CLSH_EVENT_PRE_DAC,
  700. WCD_CLSH_STATE_HPHL,
  701. hph_mode);
  702. wcd_clsh_set_hph_mode(component, CLS_H_HIFI);
  703. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  704. 0x20, 0x20);
  705. wcd_clsh_set_hph_mode(component, hph_mode);
  706. /* 100 usec delay as per HW requirement */
  707. usleep_range(100, 110);
  708. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  709. snd_soc_component_update_bits(component,
  710. WCD938X_DIGITAL_PDM_WD_CTL0, 0x17, 0x13);
  711. break;
  712. case SND_SOC_DAPM_POST_PMU:
  713. /*
  714. * 7ms sleep is required if compander is enabled as per
  715. * HW requirement. If compander is disabled, then
  716. * 20ms delay is required.
  717. */
  718. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  719. if (!wcd938x->comp1_enable)
  720. usleep_range(20000, 20100);
  721. else
  722. usleep_range(7000, 7100);
  723. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  724. }
  725. snd_soc_component_update_bits(component,
  726. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x02);
  727. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  728. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  729. snd_soc_component_update_bits(component,
  730. WCD938X_ANA_RX_SUPPLIES, 0x02, 0x02);
  731. if (wcd938x->update_wcd_event)
  732. wcd938x->update_wcd_event(wcd938x->handle,
  733. WCD_BOLERO_EVT_RX_MUTE,
  734. (WCD_RX1 << 0x10));
  735. wcd_enable_irq(&wcd938x->irq_info,
  736. WCD938X_IRQ_HPHL_PDM_WD_INT);
  737. break;
  738. case SND_SOC_DAPM_PRE_PMD:
  739. wcd_disable_irq(&wcd938x->irq_info,
  740. WCD938X_IRQ_HPHL_PDM_WD_INT);
  741. if (wcd938x->update_wcd_event)
  742. wcd938x->update_wcd_event(wcd938x->handle,
  743. WCD_BOLERO_EVT_RX_MUTE,
  744. (WCD_RX1 << 0x10 | 0x1));
  745. if (wcd938x->update_wcd_event)
  746. wcd938x->update_wcd_event(wcd938x->handle,
  747. WCD_BOLERO_EVT_RX_COMPANDER_SOFT_RST,
  748. (WCD_RX1 << 0x10));
  749. /* 7 msec delay as per HW requirement */
  750. usleep_range(7000, 7100);
  751. if (wcd938x->update_wcd_event)
  752. wcd938x->update_wcd_event(wcd938x->handle,
  753. WCD_BOLERO_EVT_RX_MUTE,
  754. (WCD_RX1 << 0x10 | 0x0));
  755. /* 20 msec delay as per HW requirement */
  756. usleep_range(21000, 21100);
  757. if (wcd938x->update_wcd_event)
  758. wcd938x->update_wcd_event(wcd938x->handle,
  759. WCD_BOLERO_EVT_RX_MUTE,
  760. (WCD_RX1 << 0x10 | 0x1));
  761. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  762. 0x80, 0x00);
  763. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  764. WCD_EVENT_PRE_HPHL_PA_OFF,
  765. &wcd938x->mbhc->wcd_mbhc);
  766. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  767. break;
  768. case SND_SOC_DAPM_POST_PMD:
  769. /*
  770. * 7ms sleep is required if compander is enabled as per
  771. * HW requirement. If compander is disabled, then
  772. * 20ms delay is required.
  773. */
  774. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  775. if (!wcd938x->comp1_enable)
  776. usleep_range(21000, 21100);
  777. else
  778. usleep_range(7000, 7100);
  779. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  780. }
  781. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  782. WCD_EVENT_POST_HPHL_PA_OFF,
  783. &wcd938x->mbhc->wcd_mbhc);
  784. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  785. 0x20, 0x00);
  786. /* 20 msec delay as per HW requirement */
  787. usleep_range(21000, 21100);
  788. snd_soc_component_update_bits(component,
  789. WCD938X_DIGITAL_PDM_WD_CTL0, 0x17, 0x00);
  790. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  791. WCD_CLSH_EVENT_POST_PA,
  792. WCD_CLSH_STATE_HPHL,
  793. hph_mode);
  794. if (wcd938x->ldoh)
  795. snd_soc_component_update_bits(component,
  796. WCD938X_LDOH_MODE,
  797. 0x80, 0x00);
  798. break;
  799. };
  800. return ret;
  801. }
  802. static int wcd938x_codec_enable_aux_pa(struct snd_soc_dapm_widget *w,
  803. struct snd_kcontrol *kcontrol,
  804. int event)
  805. {
  806. struct snd_soc_component *component =
  807. snd_soc_dapm_to_component(w->dapm);
  808. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  809. int hph_mode = wcd938x->hph_mode;
  810. int ret = 0;
  811. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  812. w->name, event);
  813. switch (event) {
  814. case SND_SOC_DAPM_PRE_PMU:
  815. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  816. wcd938x->rx_swr_dev->dev_num,
  817. true);
  818. snd_soc_component_update_bits(component,
  819. WCD938X_DIGITAL_PDM_WD_CTL2, 0x05, 0x05);
  820. break;
  821. case SND_SOC_DAPM_POST_PMU:
  822. /* 1 msec delay as per HW requirement */
  823. usleep_range(1000, 1010);
  824. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  825. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  826. snd_soc_component_update_bits(component,
  827. WCD938X_ANA_RX_SUPPLIES,
  828. 0x02, 0x02);
  829. if (wcd938x->update_wcd_event)
  830. wcd938x->update_wcd_event(wcd938x->handle,
  831. WCD_BOLERO_EVT_RX_MUTE,
  832. (WCD_RX3 << 0x10));
  833. wcd_enable_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT);
  834. break;
  835. case SND_SOC_DAPM_PRE_PMD:
  836. wcd_disable_irq(&wcd938x->irq_info,
  837. WCD938X_IRQ_AUX_PDM_WD_INT);
  838. if (wcd938x->update_wcd_event)
  839. wcd938x->update_wcd_event(wcd938x->handle,
  840. WCD_BOLERO_EVT_RX_MUTE,
  841. (WCD_RX3 << 0x10 | 0x1));
  842. break;
  843. case SND_SOC_DAPM_POST_PMD:
  844. /* 1 msec delay as per HW requirement */
  845. usleep_range(1000, 1010);
  846. snd_soc_component_update_bits(component,
  847. WCD938X_DIGITAL_PDM_WD_CTL2, 0x05, 0x00);
  848. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  849. WCD_CLSH_EVENT_POST_PA,
  850. WCD_CLSH_STATE_AUX,
  851. hph_mode);
  852. wcd938x->flyback_cur_det_disable--;
  853. if (wcd938x->flyback_cur_det_disable == 0)
  854. snd_soc_component_update_bits(component,
  855. WCD938X_FLYBACK_EN,
  856. 0x04, 0x04);
  857. break;
  858. };
  859. return ret;
  860. }
  861. static int wcd938x_codec_enable_ear_pa(struct snd_soc_dapm_widget *w,
  862. struct snd_kcontrol *kcontrol,
  863. int event)
  864. {
  865. struct snd_soc_component *component =
  866. snd_soc_dapm_to_component(w->dapm);
  867. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  868. int hph_mode = wcd938x->hph_mode;
  869. int ret = 0;
  870. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  871. w->name, event);
  872. switch (event) {
  873. case SND_SOC_DAPM_PRE_PMU:
  874. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  875. wcd938x->rx_swr_dev->dev_num,
  876. true);
  877. /*
  878. * Enable watchdog interrupt for HPHL or AUX
  879. * depending on mux value
  880. */
  881. wcd938x->ear_rx_path =
  882. snd_soc_component_read32(
  883. component, WCD938X_DIGITAL_CDC_EAR_PATH_CTL);
  884. if (wcd938x->ear_rx_path & EAR_RX_PATH_AUX)
  885. snd_soc_component_update_bits(component,
  886. WCD938X_DIGITAL_PDM_WD_CTL2,
  887. 0x05, 0x05);
  888. else
  889. snd_soc_component_update_bits(component,
  890. WCD938X_DIGITAL_PDM_WD_CTL0,
  891. 0x17, 0x13);
  892. break;
  893. case SND_SOC_DAPM_POST_PMU:
  894. /* 6 msec delay as per HW requirement */
  895. usleep_range(6000, 6010);
  896. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  897. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  898. snd_soc_component_update_bits(component,
  899. WCD938X_ANA_RX_SUPPLIES,
  900. 0x02, 0x02);
  901. if (wcd938x->update_wcd_event)
  902. wcd938x->update_wcd_event(wcd938x->handle,
  903. WCD_BOLERO_EVT_RX_MUTE,
  904. (WCD_RX1 << 0x10));
  905. break;
  906. case SND_SOC_DAPM_PRE_PMD:
  907. if (wcd938x->update_wcd_event)
  908. wcd938x->update_wcd_event(wcd938x->handle,
  909. WCD_BOLERO_EVT_RX_MUTE,
  910. (WCD_RX1 << 0x10 | 0x1));
  911. break;
  912. case SND_SOC_DAPM_POST_PMD:
  913. /* 7 msec delay as per HW requirement */
  914. usleep_range(7000, 7010);
  915. if (wcd938x->ear_rx_path & EAR_RX_PATH_AUX)
  916. snd_soc_component_update_bits(component,
  917. WCD938X_DIGITAL_PDM_WD_CTL2,
  918. 0x05, 0x00);
  919. else
  920. snd_soc_component_update_bits(component,
  921. WCD938X_DIGITAL_PDM_WD_CTL0,
  922. 0x17, 0x00);
  923. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  924. WCD_CLSH_EVENT_POST_PA,
  925. WCD_CLSH_STATE_EAR,
  926. hph_mode);
  927. wcd938x->flyback_cur_det_disable--;
  928. if (wcd938x->flyback_cur_det_disable == 0)
  929. snd_soc_component_update_bits(component,
  930. WCD938X_FLYBACK_EN,
  931. 0x04, 0x04);
  932. break;
  933. };
  934. return ret;
  935. }
  936. static int wcd938x_enable_clsh(struct snd_soc_dapm_widget *w,
  937. struct snd_kcontrol *kcontrol,
  938. int event)
  939. {
  940. struct snd_soc_component *component =
  941. snd_soc_dapm_to_component(w->dapm);
  942. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  943. int mode = wcd938x->hph_mode;
  944. int ret = 0;
  945. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  946. w->name, event);
  947. if (mode == CLS_H_LOHIFI || mode == CLS_H_ULP ||
  948. mode == CLS_H_HIFI || mode == CLS_H_LP) {
  949. wcd938x_rx_connect_port(component, CLSH,
  950. SND_SOC_DAPM_EVENT_ON(event));
  951. }
  952. if (SND_SOC_DAPM_EVENT_OFF(event))
  953. ret = swr_slvdev_datapath_control(
  954. wcd938x->rx_swr_dev,
  955. wcd938x->rx_swr_dev->dev_num,
  956. false);
  957. return ret;
  958. }
  959. static int wcd938x_enable_rx1(struct snd_soc_dapm_widget *w,
  960. struct snd_kcontrol *kcontrol,
  961. int event)
  962. {
  963. struct snd_soc_component *component =
  964. snd_soc_dapm_to_component(w->dapm);
  965. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  966. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  967. w->name, event);
  968. switch (event) {
  969. case SND_SOC_DAPM_PRE_PMU:
  970. wcd938x_rx_connect_port(component, HPH_L, true);
  971. if (wcd938x->comp1_enable)
  972. wcd938x_rx_connect_port(component, COMP_L, true);
  973. break;
  974. case SND_SOC_DAPM_POST_PMD:
  975. wcd938x_rx_connect_port(component, HPH_L, false);
  976. if (wcd938x->comp1_enable)
  977. wcd938x_rx_connect_port(component, COMP_L, false);
  978. wcd938x_rx_clk_disable(component);
  979. snd_soc_component_update_bits(component,
  980. WCD938X_DIGITAL_CDC_DIG_CLK_CTL,
  981. 0x01, 0x00);
  982. break;
  983. };
  984. return 0;
  985. }
  986. static int wcd938x_enable_rx2(struct snd_soc_dapm_widget *w,
  987. struct snd_kcontrol *kcontrol, int event)
  988. {
  989. struct snd_soc_component *component =
  990. snd_soc_dapm_to_component(w->dapm);
  991. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  992. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  993. w->name, event);
  994. switch (event) {
  995. case SND_SOC_DAPM_PRE_PMU:
  996. wcd938x_rx_connect_port(component, HPH_R, true);
  997. if (wcd938x->comp2_enable)
  998. wcd938x_rx_connect_port(component, COMP_R, true);
  999. break;
  1000. case SND_SOC_DAPM_POST_PMD:
  1001. wcd938x_rx_connect_port(component, HPH_R, false);
  1002. if (wcd938x->comp2_enable)
  1003. wcd938x_rx_connect_port(component, COMP_R, false);
  1004. wcd938x_rx_clk_disable(component);
  1005. snd_soc_component_update_bits(component,
  1006. WCD938X_DIGITAL_CDC_DIG_CLK_CTL,
  1007. 0x02, 0x00);
  1008. break;
  1009. };
  1010. return 0;
  1011. }
  1012. static int wcd938x_enable_rx3(struct snd_soc_dapm_widget *w,
  1013. struct snd_kcontrol *kcontrol,
  1014. int event)
  1015. {
  1016. struct snd_soc_component *component =
  1017. snd_soc_dapm_to_component(w->dapm);
  1018. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1019. w->name, event);
  1020. switch (event) {
  1021. case SND_SOC_DAPM_PRE_PMU:
  1022. wcd938x_rx_connect_port(component, LO, true);
  1023. break;
  1024. case SND_SOC_DAPM_POST_PMD:
  1025. wcd938x_rx_connect_port(component, LO, false);
  1026. /* 6 msec delay as per HW requirement */
  1027. usleep_range(6000, 6010);
  1028. wcd938x_rx_clk_disable(component);
  1029. snd_soc_component_update_bits(component,
  1030. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x04, 0x00);
  1031. break;
  1032. }
  1033. return 0;
  1034. }
  1035. static int wcd938x_codec_enable_dmic(struct snd_soc_dapm_widget *w,
  1036. struct snd_kcontrol *kcontrol,
  1037. int event)
  1038. {
  1039. struct snd_soc_component *component =
  1040. snd_soc_dapm_to_component(w->dapm);
  1041. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1042. u16 dmic_clk_reg, dmic_clk_en_reg;
  1043. s32 *dmic_clk_cnt;
  1044. u8 dmic_ctl_shift = 0;
  1045. u8 dmic_clk_shift = 0;
  1046. u8 dmic_clk_mask = 0;
  1047. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1048. w->name, event);
  1049. switch (w->shift) {
  1050. case 0:
  1051. case 1:
  1052. dmic_clk_cnt = &(wcd938x->dmic_0_1_clk_cnt);
  1053. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_1_2;
  1054. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC1_CTL;
  1055. dmic_clk_mask = 0x0F;
  1056. dmic_clk_shift = 0x00;
  1057. dmic_ctl_shift = 0x00;
  1058. break;
  1059. case 2:
  1060. case 3:
  1061. dmic_clk_cnt = &(wcd938x->dmic_2_3_clk_cnt);
  1062. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_1_2;
  1063. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC2_CTL;
  1064. dmic_clk_mask = 0xF0;
  1065. dmic_clk_shift = 0x04;
  1066. dmic_ctl_shift = 0x01;
  1067. break;
  1068. case 4:
  1069. case 5:
  1070. dmic_clk_cnt = &(wcd938x->dmic_4_5_clk_cnt);
  1071. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_3_4;
  1072. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC3_CTL;
  1073. dmic_clk_mask = 0x0F;
  1074. dmic_clk_shift = 0x00;
  1075. dmic_ctl_shift = 0x02;
  1076. break;
  1077. case 6:
  1078. case 7:
  1079. dmic_clk_cnt = &(wcd938x->dmic_6_7_clk_cnt);
  1080. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_3_4;
  1081. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC4_CTL;
  1082. dmic_clk_mask = 0xF0;
  1083. dmic_clk_shift = 0x04;
  1084. dmic_ctl_shift = 0x03;
  1085. break;
  1086. default:
  1087. dev_err(component->dev, "%s: Invalid DMIC Selection\n",
  1088. __func__);
  1089. return -EINVAL;
  1090. };
  1091. dev_dbg(component->dev, "%s: event %d DMIC%d dmic_clk_cnt %d\n",
  1092. __func__, event, (w->shift +1), *dmic_clk_cnt);
  1093. switch (event) {
  1094. case SND_SOC_DAPM_PRE_PMU:
  1095. snd_soc_component_update_bits(component,
  1096. WCD938X_DIGITAL_CDC_AMIC_CTL,
  1097. (0x01 << dmic_ctl_shift), 0x00);
  1098. /* 250us sleep as per HW requirement */
  1099. usleep_range(250, 260);
  1100. /* Setting DMIC clock rate to 2.4MHz */
  1101. snd_soc_component_update_bits(component,
  1102. dmic_clk_reg, dmic_clk_mask,
  1103. (0x03 << dmic_clk_shift));
  1104. snd_soc_component_update_bits(component,
  1105. dmic_clk_en_reg, 0x08, 0x08);
  1106. /* enable clock scaling */
  1107. snd_soc_component_update_bits(component,
  1108. WCD938X_DIGITAL_CDC_DMIC_CTL, 0x06, 0x06);
  1109. wcd938x_tx_connect_port(component, DMIC0 + (w->shift), true);
  1110. break;
  1111. case SND_SOC_DAPM_POST_PMD:
  1112. wcd938x_tx_connect_port(component, DMIC0 + (w->shift), false);
  1113. snd_soc_component_update_bits(component,
  1114. WCD938X_DIGITAL_CDC_AMIC_CTL,
  1115. (0x01 << dmic_ctl_shift),
  1116. (0x01 << dmic_ctl_shift));
  1117. snd_soc_component_update_bits(component,
  1118. dmic_clk_en_reg, 0x08, 0x00);
  1119. break;
  1120. };
  1121. return 0;
  1122. }
  1123. /*
  1124. * wcd938x_get_micb_vout_ctl_val: converts micbias from volts to register value
  1125. * @micb_mv: micbias in mv
  1126. *
  1127. * return register value converted
  1128. */
  1129. int wcd938x_get_micb_vout_ctl_val(u32 micb_mv)
  1130. {
  1131. /* min micbias voltage is 1V and maximum is 2.85V */
  1132. if (micb_mv < 1000 || micb_mv > 2850) {
  1133. pr_err("%s: unsupported micbias voltage\n", __func__);
  1134. return -EINVAL;
  1135. }
  1136. return (micb_mv - 1000) / 50;
  1137. }
  1138. EXPORT_SYMBOL(wcd938x_get_micb_vout_ctl_val);
  1139. /*
  1140. * wcd938x_mbhc_micb_adjust_voltage: adjust specific micbias voltage
  1141. * @component: handle to snd_soc_component *
  1142. * @req_volt: micbias voltage to be set
  1143. * @micb_num: micbias to be set, e.g. micbias1 or micbias2
  1144. *
  1145. * return 0 if adjustment is success or error code in case of failure
  1146. */
  1147. int wcd938x_mbhc_micb_adjust_voltage(struct snd_soc_component *component,
  1148. int req_volt, int micb_num)
  1149. {
  1150. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1151. int cur_vout_ctl, req_vout_ctl;
  1152. int micb_reg, micb_val, micb_en;
  1153. int ret = 0;
  1154. switch (micb_num) {
  1155. case MIC_BIAS_1:
  1156. micb_reg = WCD938X_ANA_MICB1;
  1157. break;
  1158. case MIC_BIAS_2:
  1159. micb_reg = WCD938X_ANA_MICB2;
  1160. break;
  1161. case MIC_BIAS_3:
  1162. micb_reg = WCD938X_ANA_MICB3;
  1163. break;
  1164. case MIC_BIAS_4:
  1165. micb_reg = WCD938X_ANA_MICB4;
  1166. break;
  1167. default:
  1168. return -EINVAL;
  1169. }
  1170. mutex_lock(&wcd938x->micb_lock);
  1171. /*
  1172. * If requested micbias voltage is same as current micbias
  1173. * voltage, then just return. Otherwise, adjust voltage as
  1174. * per requested value. If micbias is already enabled, then
  1175. * to avoid slow micbias ramp-up or down enable pull-up
  1176. * momentarily, change the micbias value and then re-enable
  1177. * micbias.
  1178. */
  1179. micb_val = snd_soc_component_read32(component, micb_reg);
  1180. micb_en = (micb_val & 0xC0) >> 6;
  1181. cur_vout_ctl = micb_val & 0x3F;
  1182. req_vout_ctl = wcd938x_get_micb_vout_ctl_val(req_volt);
  1183. if (req_vout_ctl < 0) {
  1184. ret = -EINVAL;
  1185. goto exit;
  1186. }
  1187. if (cur_vout_ctl == req_vout_ctl) {
  1188. ret = 0;
  1189. goto exit;
  1190. }
  1191. dev_dbg(component->dev, "%s: micb_num: %d, cur_mv: %d, req_mv: %d, micb_en: %d\n",
  1192. __func__, micb_num, WCD_VOUT_CTL_TO_MICB(cur_vout_ctl),
  1193. req_volt, micb_en);
  1194. if (micb_en == 0x1)
  1195. snd_soc_component_update_bits(component, micb_reg, 0xC0, 0x80);
  1196. snd_soc_component_update_bits(component, micb_reg, 0x3F, req_vout_ctl);
  1197. if (micb_en == 0x1) {
  1198. snd_soc_component_update_bits(component, micb_reg, 0xC0, 0x40);
  1199. /*
  1200. * Add 2ms delay as per HW requirement after enabling
  1201. * micbias
  1202. */
  1203. usleep_range(2000, 2100);
  1204. }
  1205. exit:
  1206. mutex_unlock(&wcd938x->micb_lock);
  1207. return ret;
  1208. }
  1209. EXPORT_SYMBOL(wcd938x_mbhc_micb_adjust_voltage);
  1210. static int wcd938x_tx_swr_ctrl(struct snd_soc_dapm_widget *w,
  1211. struct snd_kcontrol *kcontrol,
  1212. int event)
  1213. {
  1214. struct snd_soc_component *component =
  1215. snd_soc_dapm_to_component(w->dapm);
  1216. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1217. int ret = 0;
  1218. switch (event) {
  1219. case SND_SOC_DAPM_PRE_PMU:
  1220. ret = swr_slvdev_datapath_control(wcd938x->tx_swr_dev,
  1221. wcd938x->tx_swr_dev->dev_num,
  1222. true);
  1223. break;
  1224. case SND_SOC_DAPM_POST_PMD:
  1225. ret = swr_slvdev_datapath_control(wcd938x->tx_swr_dev,
  1226. wcd938x->tx_swr_dev->dev_num,
  1227. false);
  1228. break;
  1229. };
  1230. return ret;
  1231. }
  1232. static int wcd938x_get_adc_mode(int val)
  1233. {
  1234. int ret = 0;
  1235. switch (val) {
  1236. case ADC_MODE_INVALID:
  1237. ret = ADC_MODE_VAL_NORMAL;
  1238. break;
  1239. case ADC_MODE_HIFI:
  1240. ret = ADC_MODE_VAL_HIFI;
  1241. break;
  1242. case ADC_MODE_LO_HIF:
  1243. ret = ADC_MODE_VAL_LO_HIF;
  1244. break;
  1245. case ADC_MODE_NORMAL:
  1246. ret = ADC_MODE_VAL_NORMAL;
  1247. break;
  1248. case ADC_MODE_LP:
  1249. ret = ADC_MODE_VAL_LP;
  1250. break;
  1251. case ADC_MODE_ULP1:
  1252. ret = ADC_MODE_VAL_ULP1;
  1253. break;
  1254. case ADC_MODE_ULP2:
  1255. ret = ADC_MODE_VAL_ULP2;
  1256. break;
  1257. default:
  1258. ret = -EINVAL;
  1259. pr_err("%s: invalid ADC mode value %d\n", __func__, val);
  1260. break;
  1261. }
  1262. return ret;
  1263. }
  1264. static int wcd938x_codec_enable_adc(struct snd_soc_dapm_widget *w,
  1265. struct snd_kcontrol *kcontrol,
  1266. int event){
  1267. int mode;
  1268. struct snd_soc_component *component =
  1269. snd_soc_dapm_to_component(w->dapm);
  1270. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1271. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1272. w->name, event);
  1273. switch (event) {
  1274. case SND_SOC_DAPM_PRE_PMU:
  1275. mode = wcd938x_get_adc_mode(wcd938x->tx_mode[w->shift]);
  1276. if (mode < 0) {
  1277. dev_info(component->dev,
  1278. "%s: invalid mode, setting to normal mode\n",
  1279. __func__);
  1280. mode = ADC_MODE_VAL_NORMAL;
  1281. }
  1282. snd_soc_component_update_bits(component,
  1283. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x80, 0x80);
  1284. snd_soc_component_update_bits(component,
  1285. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x08, 0x08);
  1286. snd_soc_component_update_bits(component,
  1287. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x10);
  1288. switch (w->shift) {
  1289. case 0:
  1290. snd_soc_component_update_bits(component,
  1291. WCD938X_DIGITAL_CDC_TX_ANA_MODE_0_1, 0x0F,
  1292. mode);
  1293. break;
  1294. case 1:
  1295. snd_soc_component_update_bits(component,
  1296. WCD938X_DIGITAL_CDC_TX_ANA_MODE_0_1, 0xF0,
  1297. mode << 4);
  1298. break;
  1299. case 2:
  1300. snd_soc_component_update_bits(component,
  1301. WCD938X_DIGITAL_CDC_TX_ANA_MODE_2_3, 0x0F,
  1302. mode);
  1303. break;
  1304. case 3:
  1305. snd_soc_component_update_bits(component,
  1306. WCD938X_DIGITAL_CDC_TX_ANA_MODE_2_3, 0xF0,
  1307. mode << 4);
  1308. break;
  1309. default:
  1310. break;
  1311. }
  1312. set_bit(w->shift, &wcd938x->status_mask);
  1313. wcd938x_tx_connect_port(component, ADC1 + (w->shift), true);
  1314. break;
  1315. case SND_SOC_DAPM_POST_PMD:
  1316. wcd938x_tx_connect_port(component, ADC1 + (w->shift), false);
  1317. snd_soc_component_update_bits(component,
  1318. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x08, 0x00);
  1319. clear_bit(w->shift, &wcd938x->status_mask);
  1320. break;
  1321. };
  1322. return 0;
  1323. }
  1324. int wcd938x_tx_channel_config(struct snd_soc_component *component,
  1325. int channel, int mode)
  1326. {
  1327. int reg = WCD938X_ANA_TX_CH2, mask = 0, val = 0;
  1328. int ret = 0;
  1329. switch (channel) {
  1330. case 0:
  1331. reg = WCD938X_ANA_TX_CH2;
  1332. mask = 0x40;
  1333. break;
  1334. case 1:
  1335. reg = WCD938X_ANA_TX_CH2;
  1336. mask = 0x20;
  1337. break;
  1338. case 2:
  1339. reg = WCD938X_ANA_TX_CH4;
  1340. mask = 0x40;
  1341. break;
  1342. case 3:
  1343. reg = WCD938X_ANA_TX_CH4;
  1344. mask = 0x20;
  1345. break;
  1346. default:
  1347. pr_err("%s: Invalid channel num %d\n", __func__, channel);
  1348. ret = -EINVAL;
  1349. break;
  1350. }
  1351. if (!mode)
  1352. val = 0x00;
  1353. else
  1354. val = mask;
  1355. if (!ret)
  1356. snd_soc_component_update_bits(component, reg, mask, val);
  1357. return ret;
  1358. }
  1359. static int wcd938x_enable_req(struct snd_soc_dapm_widget *w,
  1360. struct snd_kcontrol *kcontrol, int event)
  1361. {
  1362. struct snd_soc_component *component =
  1363. snd_soc_dapm_to_component(w->dapm);
  1364. int ret = 0;
  1365. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1366. w->name, event);
  1367. switch (event) {
  1368. case SND_SOC_DAPM_PRE_PMU:
  1369. snd_soc_component_update_bits(component,
  1370. WCD938X_DIGITAL_CDC_REQ_CTL, 0x02, 0x02);
  1371. snd_soc_component_update_bits(component,
  1372. WCD938X_DIGITAL_CDC_REQ_CTL, 0x01, 0x00);
  1373. ret = wcd938x_tx_channel_config(component, w->shift, 1);
  1374. snd_soc_component_update_bits(component,
  1375. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x30, 0x30);
  1376. snd_soc_component_update_bits(component,
  1377. WCD938X_ANA_TX_CH1, 0x80, 0x80);
  1378. snd_soc_component_update_bits(component,
  1379. WCD938X_ANA_TX_CH2, 0x80, 0x80);
  1380. ret |= wcd938x_tx_channel_config(component, w->shift, 0);
  1381. break;
  1382. case SND_SOC_DAPM_POST_PMD:
  1383. snd_soc_component_update_bits(component,
  1384. WCD938X_ANA_TX_CH1, 0x80, 0x00);
  1385. snd_soc_component_update_bits(component,
  1386. WCD938X_ANA_TX_CH2, 0x80, 0x00);
  1387. snd_soc_component_update_bits(component,
  1388. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x10, 0x00);
  1389. snd_soc_component_update_bits(component,
  1390. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x00);
  1391. snd_soc_component_update_bits(component,
  1392. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x80, 0x00);
  1393. break;
  1394. };
  1395. return ret;
  1396. }
  1397. int wcd938x_micbias_control(struct snd_soc_component *component,
  1398. int micb_num, int req, bool is_dapm)
  1399. {
  1400. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1401. int micb_index = micb_num - 1;
  1402. u16 micb_reg;
  1403. int pre_off_event = 0, post_off_event = 0;
  1404. int post_on_event = 0, post_dapm_off = 0;
  1405. int post_dapm_on = 0;
  1406. if ((micb_index < 0) || (micb_index > WCD938X_MAX_MICBIAS - 1)) {
  1407. dev_err(component->dev,
  1408. "%s: Invalid micbias index, micb_ind:%d\n",
  1409. __func__, micb_index);
  1410. return -EINVAL;
  1411. }
  1412. if (NULL == wcd938x) {
  1413. dev_err(component->dev,
  1414. "%s: wcd938x private data is NULL\n", __func__);
  1415. return -EINVAL;
  1416. }
  1417. switch (micb_num) {
  1418. case MIC_BIAS_1:
  1419. micb_reg = WCD938X_ANA_MICB1;
  1420. break;
  1421. case MIC_BIAS_2:
  1422. micb_reg = WCD938X_ANA_MICB2;
  1423. pre_off_event = WCD_EVENT_PRE_MICBIAS_2_OFF;
  1424. post_off_event = WCD_EVENT_POST_MICBIAS_2_OFF;
  1425. post_on_event = WCD_EVENT_POST_MICBIAS_2_ON;
  1426. post_dapm_on = WCD_EVENT_POST_DAPM_MICBIAS_2_ON;
  1427. post_dapm_off = WCD_EVENT_POST_DAPM_MICBIAS_2_OFF;
  1428. break;
  1429. case MIC_BIAS_3:
  1430. micb_reg = WCD938X_ANA_MICB3;
  1431. break;
  1432. case MIC_BIAS_4:
  1433. micb_reg = WCD938X_ANA_MICB4;
  1434. break;
  1435. default:
  1436. dev_err(component->dev, "%s: Invalid micbias number: %d\n",
  1437. __func__, micb_num);
  1438. return -EINVAL;
  1439. };
  1440. mutex_lock(&wcd938x->micb_lock);
  1441. switch (req) {
  1442. case MICB_PULLUP_ENABLE:
  1443. wcd938x->pullup_ref[micb_index]++;
  1444. if ((wcd938x->pullup_ref[micb_index] == 1) &&
  1445. (wcd938x->micb_ref[micb_index] == 0))
  1446. snd_soc_component_update_bits(component, micb_reg,
  1447. 0xC0, 0x80);
  1448. break;
  1449. case MICB_PULLUP_DISABLE:
  1450. if (wcd938x->pullup_ref[micb_index] > 0)
  1451. wcd938x->pullup_ref[micb_index]--;
  1452. if ((wcd938x->pullup_ref[micb_index] == 0) &&
  1453. (wcd938x->micb_ref[micb_index] == 0))
  1454. snd_soc_component_update_bits(component, micb_reg,
  1455. 0xC0, 0x00);
  1456. break;
  1457. case MICB_ENABLE:
  1458. wcd938x->micb_ref[micb_index]++;
  1459. if (wcd938x->micb_ref[micb_index] == 1) {
  1460. snd_soc_component_update_bits(component,
  1461. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0xE0, 0xE0);
  1462. snd_soc_component_update_bits(component,
  1463. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x10);
  1464. snd_soc_component_update_bits(component,
  1465. WCD938X_DIGITAL_CDC_ANA_TX_CLK_CTL, 0x01, 0x01);
  1466. snd_soc_component_update_bits(component,
  1467. WCD938X_MICB1_TEST_CTL_2, 0x01, 0x01);
  1468. snd_soc_component_update_bits(component,
  1469. WCD938X_MICB2_TEST_CTL_2, 0x01, 0x01);
  1470. snd_soc_component_update_bits(component,
  1471. WCD938X_MICB3_TEST_CTL_2, 0x01, 0x01);
  1472. snd_soc_component_update_bits(component,
  1473. WCD938X_MICB4_TEST_CTL_2, 0x01, 0x01);
  1474. snd_soc_component_update_bits(component,
  1475. micb_reg, 0xC0, 0x40);
  1476. if (post_on_event)
  1477. blocking_notifier_call_chain(
  1478. &wcd938x->mbhc->notifier,
  1479. post_on_event,
  1480. &wcd938x->mbhc->wcd_mbhc);
  1481. }
  1482. if (is_dapm && post_dapm_on && wcd938x->mbhc)
  1483. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  1484. post_dapm_on,
  1485. &wcd938x->mbhc->wcd_mbhc);
  1486. break;
  1487. case MICB_DISABLE:
  1488. if (wcd938x->micb_ref[micb_index] > 0)
  1489. wcd938x->micb_ref[micb_index]--;
  1490. if ((wcd938x->micb_ref[micb_index] == 0) &&
  1491. (wcd938x->pullup_ref[micb_index] > 0))
  1492. snd_soc_component_update_bits(component, micb_reg,
  1493. 0xC0, 0x80);
  1494. else if ((wcd938x->micb_ref[micb_index] == 0) &&
  1495. (wcd938x->pullup_ref[micb_index] == 0)) {
  1496. if (pre_off_event && wcd938x->mbhc)
  1497. blocking_notifier_call_chain(
  1498. &wcd938x->mbhc->notifier,
  1499. pre_off_event,
  1500. &wcd938x->mbhc->wcd_mbhc);
  1501. snd_soc_component_update_bits(component, micb_reg,
  1502. 0xC0, 0x00);
  1503. if (post_off_event && wcd938x->mbhc)
  1504. blocking_notifier_call_chain(
  1505. &wcd938x->mbhc->notifier,
  1506. post_off_event,
  1507. &wcd938x->mbhc->wcd_mbhc);
  1508. }
  1509. if (is_dapm && post_dapm_off && wcd938x->mbhc)
  1510. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  1511. post_dapm_off,
  1512. &wcd938x->mbhc->wcd_mbhc);
  1513. break;
  1514. };
  1515. dev_dbg(component->dev,
  1516. "%s: micb_num:%d, micb_ref: %d, pullup_ref: %d\n",
  1517. __func__, micb_num, wcd938x->micb_ref[micb_index],
  1518. wcd938x->pullup_ref[micb_index]);
  1519. mutex_unlock(&wcd938x->micb_lock);
  1520. return 0;
  1521. }
  1522. EXPORT_SYMBOL(wcd938x_micbias_control);
  1523. static int wcd938x_get_logical_addr(struct swr_device *swr_dev)
  1524. {
  1525. int ret = 0;
  1526. uint8_t devnum = 0;
  1527. ret = swr_get_logical_dev_num(swr_dev, swr_dev->addr, &devnum);
  1528. if (ret) {
  1529. dev_err(&swr_dev->dev,
  1530. "%s get devnum %d for dev addr %lx failed\n",
  1531. __func__, devnum, swr_dev->addr);
  1532. swr_remove_device(swr_dev);
  1533. return ret;
  1534. }
  1535. swr_dev->dev_num = devnum;
  1536. return 0;
  1537. }
  1538. static int wcd938x_event_notify(struct notifier_block *block,
  1539. unsigned long val,
  1540. void *data)
  1541. {
  1542. u16 event = (val & 0xffff);
  1543. int ret = 0;
  1544. struct wcd938x_priv *wcd938x = dev_get_drvdata((struct device *)data);
  1545. struct snd_soc_component *component = wcd938x->component;
  1546. struct wcd_mbhc *mbhc;
  1547. switch (event) {
  1548. case BOLERO_WCD_EVT_TX_CH_HOLD_CLEAR:
  1549. if (test_bit(WCD_ADC1, &wcd938x->status_mask)) {
  1550. snd_soc_component_update_bits(component,
  1551. WCD938X_ANA_TX_CH2, 0x40, 0x00);
  1552. clear_bit(WCD_ADC1, &wcd938x->status_mask);
  1553. }
  1554. if (test_bit(WCD_ADC2, &wcd938x->status_mask)) {
  1555. snd_soc_component_update_bits(component,
  1556. WCD938X_ANA_TX_CH2, 0x20, 0x00);
  1557. clear_bit(WCD_ADC2, &wcd938x->status_mask);
  1558. }
  1559. if (test_bit(WCD_ADC3, &wcd938x->status_mask)) {
  1560. snd_soc_component_update_bits(component,
  1561. WCD938X_ANA_TX_CH4, 0x40, 0x00);
  1562. clear_bit(WCD_ADC3, &wcd938x->status_mask);
  1563. }
  1564. if (test_bit(WCD_ADC4, &wcd938x->status_mask)) {
  1565. snd_soc_component_update_bits(component,
  1566. WCD938X_ANA_TX_CH4, 0x20, 0x00);
  1567. clear_bit(WCD_ADC4, &wcd938x->status_mask);
  1568. }
  1569. break;
  1570. case BOLERO_WCD_EVT_PA_OFF_PRE_SSR:
  1571. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  1572. 0xC0, 0x00);
  1573. snd_soc_component_update_bits(component, WCD938X_ANA_EAR,
  1574. 0x80, 0x00);
  1575. snd_soc_component_update_bits(component, WCD938X_AUX_AUXPA,
  1576. 0x80, 0x00);
  1577. break;
  1578. case BOLERO_WCD_EVT_SSR_DOWN:
  1579. mbhc = &wcd938x->mbhc->wcd_mbhc;
  1580. wcd938x_mbhc_ssr_down(wcd938x->mbhc, component);
  1581. wcd938x_reset_low(wcd938x->dev);
  1582. break;
  1583. case BOLERO_WCD_EVT_SSR_UP:
  1584. wcd938x_reset(wcd938x->dev);
  1585. wcd938x_get_logical_addr(wcd938x->tx_swr_dev);
  1586. wcd938x_get_logical_addr(wcd938x->rx_swr_dev);
  1587. wcd938x_init_reg(component);
  1588. regcache_mark_dirty(wcd938x->regmap);
  1589. regcache_sync(wcd938x->regmap);
  1590. /* Initialize MBHC module */
  1591. mbhc = &wcd938x->mbhc->wcd_mbhc;
  1592. ret = wcd938x_mbhc_post_ssr_init(wcd938x->mbhc, component);
  1593. if (ret) {
  1594. dev_err(component->dev, "%s: mbhc initialization failed\n",
  1595. __func__);
  1596. } else {
  1597. wcd938x_mbhc_hs_detect(component, mbhc->mbhc_cfg);
  1598. }
  1599. break;
  1600. case BOLERO_WCD_EVT_CLK_NOTIFY:
  1601. snd_soc_component_update_bits(component,
  1602. WCD938X_DIGITAL_TOP_CLK_CFG, 0x06,
  1603. ((val >> 0x10) << 0x01));
  1604. break;
  1605. default:
  1606. dev_dbg(component->dev, "%s: invalid event %d\n", __func__, event);
  1607. break;
  1608. }
  1609. return 0;
  1610. }
  1611. static int __wcd938x_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1612. int event)
  1613. {
  1614. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  1615. int micb_num;
  1616. dev_dbg(component->dev, "%s: wname: %s, event: %d\n",
  1617. __func__, w->name, event);
  1618. if (strnstr(w->name, "MIC BIAS1", sizeof("MIC BIAS1")))
  1619. micb_num = MIC_BIAS_1;
  1620. else if (strnstr(w->name, "MIC BIAS2", sizeof("MIC BIAS2")))
  1621. micb_num = MIC_BIAS_2;
  1622. else if (strnstr(w->name, "MIC BIAS3", sizeof("MIC BIAS3")))
  1623. micb_num = MIC_BIAS_3;
  1624. else if (strnstr(w->name, "MIC BIAS4", sizeof("MIC BIAS4")))
  1625. micb_num = MIC_BIAS_4;
  1626. else
  1627. return -EINVAL;
  1628. switch (event) {
  1629. case SND_SOC_DAPM_PRE_PMU:
  1630. wcd938x_micbias_control(component, micb_num,
  1631. MICB_ENABLE, true);
  1632. break;
  1633. case SND_SOC_DAPM_POST_PMU:
  1634. /* 1 msec delay as per HW requirement */
  1635. usleep_range(1000, 1100);
  1636. break;
  1637. case SND_SOC_DAPM_POST_PMD:
  1638. wcd938x_micbias_control(component, micb_num,
  1639. MICB_DISABLE, true);
  1640. break;
  1641. };
  1642. return 0;
  1643. }
  1644. static int wcd938x_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1645. struct snd_kcontrol *kcontrol,
  1646. int event)
  1647. {
  1648. return __wcd938x_codec_enable_micbias(w, event);
  1649. }
  1650. static inline int wcd938x_tx_path_get(const char *wname,
  1651. unsigned int *path_num)
  1652. {
  1653. int ret = 0;
  1654. char *widget_name = NULL;
  1655. char *w_name = NULL;
  1656. char *path_num_char = NULL;
  1657. char *path_name = NULL;
  1658. widget_name = kstrndup(wname, 9, GFP_KERNEL);
  1659. if (!widget_name)
  1660. return -EINVAL;
  1661. w_name = widget_name;
  1662. path_name = strsep(&widget_name, " ");
  1663. if (!path_name) {
  1664. pr_err("%s: Invalid widget name = %s\n",
  1665. __func__, widget_name);
  1666. ret = -EINVAL;
  1667. goto err;
  1668. }
  1669. path_num_char = strpbrk(path_name, "0123");
  1670. if (!path_num_char) {
  1671. pr_err("%s: tx path index not found\n",
  1672. __func__);
  1673. ret = -EINVAL;
  1674. goto err;
  1675. }
  1676. ret = kstrtouint(path_num_char, 10, path_num);
  1677. if (ret < 0)
  1678. pr_err("%s: Invalid tx path = %s\n",
  1679. __func__, w_name);
  1680. err:
  1681. kfree(w_name);
  1682. return ret;
  1683. }
  1684. static int wcd938x_tx_mode_get(struct snd_kcontrol *kcontrol,
  1685. struct snd_ctl_elem_value *ucontrol)
  1686. {
  1687. struct snd_soc_component *component =
  1688. snd_soc_kcontrol_component(kcontrol);
  1689. struct wcd938x_priv *wcd938x = NULL;
  1690. int ret = 0;
  1691. unsigned int path = 0;
  1692. if (!component)
  1693. return -EINVAL;
  1694. wcd938x = snd_soc_component_get_drvdata(component);
  1695. if (!wcd938x)
  1696. return -EINVAL;
  1697. ret = wcd938x_tx_path_get(kcontrol->id.name, &path);
  1698. if (ret < 0)
  1699. return ret;
  1700. ucontrol->value.integer.value[0] = wcd938x->tx_mode[path];
  1701. return 0;
  1702. }
  1703. static int wcd938x_tx_mode_put(struct snd_kcontrol *kcontrol,
  1704. struct snd_ctl_elem_value *ucontrol)
  1705. {
  1706. struct snd_soc_component *component =
  1707. snd_soc_kcontrol_component(kcontrol);
  1708. struct wcd938x_priv *wcd938x = NULL;
  1709. u32 mode_val;
  1710. unsigned int path = 0;
  1711. int ret = 0;
  1712. if (!component)
  1713. return -EINVAL;
  1714. wcd938x = snd_soc_component_get_drvdata(component);
  1715. if (!wcd938x)
  1716. return -EINVAL;
  1717. ret = wcd938x_tx_path_get(kcontrol->id.name, &path);
  1718. if (ret)
  1719. return ret;
  1720. mode_val = ucontrol->value.enumerated.item[0];
  1721. dev_dbg(component->dev, "%s: mode: %d\n", __func__, mode_val);
  1722. wcd938x->tx_mode[path] = mode_val;
  1723. return 0;
  1724. }
  1725. static int wcd938x_rx_hph_mode_get(struct snd_kcontrol *kcontrol,
  1726. struct snd_ctl_elem_value *ucontrol)
  1727. {
  1728. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  1729. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1730. ucontrol->value.integer.value[0] = wcd938x->hph_mode;
  1731. return 0;
  1732. }
  1733. static int wcd938x_rx_hph_mode_put(struct snd_kcontrol *kcontrol,
  1734. struct snd_ctl_elem_value *ucontrol)
  1735. {
  1736. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  1737. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1738. u32 mode_val;
  1739. mode_val = ucontrol->value.enumerated.item[0];
  1740. dev_dbg(component->dev, "%s: mode: %d\n", __func__, mode_val);
  1741. if (mode_val == 0) {
  1742. dev_info(component->dev,
  1743. "%s:Invalid HPH Mode, default to class_AB\n",
  1744. __func__);
  1745. mode_val = 3; /* enum will be updated later */
  1746. }
  1747. wcd938x->hph_mode = mode_val;
  1748. return 0;
  1749. }
  1750. static int wcd938x_get_compander(struct snd_kcontrol *kcontrol,
  1751. struct snd_ctl_elem_value *ucontrol)
  1752. {
  1753. struct snd_soc_component *component =
  1754. snd_soc_kcontrol_component(kcontrol);
  1755. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1756. bool hphr;
  1757. struct soc_multi_mixer_control *mc;
  1758. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  1759. hphr = mc->shift;
  1760. ucontrol->value.integer.value[0] = hphr ? wcd938x->comp2_enable :
  1761. wcd938x->comp1_enable;
  1762. return 0;
  1763. }
  1764. static int wcd938x_set_compander(struct snd_kcontrol *kcontrol,
  1765. struct snd_ctl_elem_value *ucontrol)
  1766. {
  1767. struct snd_soc_component *component =
  1768. snd_soc_kcontrol_component(kcontrol);
  1769. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1770. int value = ucontrol->value.integer.value[0];
  1771. bool hphr;
  1772. struct soc_multi_mixer_control *mc;
  1773. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  1774. hphr = mc->shift;
  1775. if (hphr)
  1776. wcd938x->comp2_enable = value;
  1777. else
  1778. wcd938x->comp1_enable = value;
  1779. return 0;
  1780. }
  1781. static int wcd938x_ldoh_get(struct snd_kcontrol *kcontrol,
  1782. struct snd_ctl_elem_value *ucontrol)
  1783. {
  1784. struct snd_soc_component *component =
  1785. snd_soc_kcontrol_component(kcontrol);
  1786. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1787. ucontrol->value.integer.value[0] = wcd938x->ldoh;
  1788. return 0;
  1789. }
  1790. static int wcd938x_ldoh_put(struct snd_kcontrol *kcontrol,
  1791. struct snd_ctl_elem_value *ucontrol)
  1792. {
  1793. struct snd_soc_component *component =
  1794. snd_soc_kcontrol_component(kcontrol);
  1795. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1796. wcd938x->ldoh = ucontrol->value.integer.value[0];
  1797. return 0;
  1798. }
  1799. static const char * const tx_mode_mux_text_wcd9380[] = {
  1800. "ADC_INVALID", "ADC_HIFI", "ADC_LO_HIF", "ADC_NORMAL", "ADC_LP",
  1801. };
  1802. static const struct soc_enum tx_mode_mux_enum_wcd9380 =
  1803. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(tx_mode_mux_text_wcd9380),
  1804. tx_mode_mux_text_wcd9380);
  1805. static const char * const tx_mode_mux_text[] = {
  1806. "ADC_INVALID", "ADC_HIFI", "ADC_LO_HIF", "ADC_NORMAL", "ADC_LP",
  1807. "ADC_ULP1", "ADC_ULP2",
  1808. };
  1809. static const struct soc_enum tx_mode_mux_enum =
  1810. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(tx_mode_mux_text),
  1811. tx_mode_mux_text);
  1812. static const char * const rx_hph_mode_mux_text[] = {
  1813. "CLS_H_INVALID", "CLS_H_HIFI", "CLS_H_LP", "CLS_AB", "CLS_H_LOHIFI",
  1814. "CLS_H_ULP", "CLS_AB_HIFI", "CLS_AB_LP", "CLS_AB_LOHIFI",
  1815. };
  1816. static const struct soc_enum rx_hph_mode_mux_enum =
  1817. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(rx_hph_mode_mux_text),
  1818. rx_hph_mode_mux_text);
  1819. static const struct snd_kcontrol_new wcd9380_snd_controls[] = {
  1820. SOC_ENUM_EXT("TX0 MODE", tx_mode_mux_enum_wcd9380,
  1821. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1822. SOC_ENUM_EXT("TX1 MODE", tx_mode_mux_enum_wcd9380,
  1823. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1824. SOC_ENUM_EXT("TX2 MODE", tx_mode_mux_enum_wcd9380,
  1825. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1826. SOC_ENUM_EXT("TX3 MODE", tx_mode_mux_enum_wcd9380,
  1827. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1828. };
  1829. static const struct snd_kcontrol_new wcd9385_snd_controls[] = {
  1830. SOC_ENUM_EXT("TX0 MODE", tx_mode_mux_enum,
  1831. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1832. SOC_ENUM_EXT("TX1 MODE", tx_mode_mux_enum,
  1833. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1834. SOC_ENUM_EXT("TX2 MODE", tx_mode_mux_enum,
  1835. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1836. SOC_ENUM_EXT("TX3 MODE", tx_mode_mux_enum,
  1837. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  1838. };
  1839. static const struct snd_kcontrol_new wcd938x_snd_controls[] = {
  1840. SOC_ENUM_EXT("RX HPH Mode", rx_hph_mode_mux_enum,
  1841. wcd938x_rx_hph_mode_get, wcd938x_rx_hph_mode_put),
  1842. SOC_SINGLE_EXT("HPHL_COMP Switch", SND_SOC_NOPM, 0, 1, 0,
  1843. wcd938x_get_compander, wcd938x_set_compander),
  1844. SOC_SINGLE_EXT("HPHR_COMP Switch", SND_SOC_NOPM, 1, 1, 0,
  1845. wcd938x_get_compander, wcd938x_set_compander),
  1846. SOC_SINGLE_EXT("LDOH Enable", SND_SOC_NOPM, 0, 1, 0,
  1847. wcd938x_ldoh_get, wcd938x_ldoh_put),
  1848. SOC_SINGLE_TLV("HPHL Volume", WCD938X_HPH_L_EN, 0, 20, 1, line_gain),
  1849. SOC_SINGLE_TLV("HPHR Volume", WCD938X_HPH_R_EN, 0, 20, 1, line_gain),
  1850. SOC_SINGLE_TLV("ADC1 Volume", WCD938X_ANA_TX_CH1, 0, 20, 0,
  1851. analog_gain),
  1852. SOC_SINGLE_TLV("ADC2 Volume", WCD938X_ANA_TX_CH2, 0, 20, 0,
  1853. analog_gain),
  1854. SOC_SINGLE_TLV("ADC3 Volume", WCD938X_ANA_TX_CH3, 0, 20, 0,
  1855. analog_gain),
  1856. SOC_SINGLE_TLV("ADC4 Volume", WCD938X_ANA_TX_CH4, 0, 20, 0,
  1857. analog_gain),
  1858. };
  1859. static const struct snd_kcontrol_new adc1_switch[] = {
  1860. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1861. };
  1862. static const struct snd_kcontrol_new adc2_switch[] = {
  1863. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1864. };
  1865. static const struct snd_kcontrol_new adc3_switch[] = {
  1866. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1867. };
  1868. static const struct snd_kcontrol_new adc4_switch[] = {
  1869. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1870. };
  1871. static const struct snd_kcontrol_new dmic1_switch[] = {
  1872. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1873. };
  1874. static const struct snd_kcontrol_new dmic2_switch[] = {
  1875. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1876. };
  1877. static const struct snd_kcontrol_new dmic3_switch[] = {
  1878. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1879. };
  1880. static const struct snd_kcontrol_new dmic4_switch[] = {
  1881. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1882. };
  1883. static const struct snd_kcontrol_new dmic5_switch[] = {
  1884. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1885. };
  1886. static const struct snd_kcontrol_new dmic6_switch[] = {
  1887. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1888. };
  1889. static const struct snd_kcontrol_new dmic7_switch[] = {
  1890. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1891. };
  1892. static const struct snd_kcontrol_new dmic8_switch[] = {
  1893. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1894. };
  1895. static const struct snd_kcontrol_new ear_rdac_switch[] = {
  1896. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1897. };
  1898. static const struct snd_kcontrol_new aux_rdac_switch[] = {
  1899. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1900. };
  1901. static const struct snd_kcontrol_new hphl_rdac_switch[] = {
  1902. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1903. };
  1904. static const struct snd_kcontrol_new hphr_rdac_switch[] = {
  1905. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1906. };
  1907. static const char * const adc2_mux_text[] = {
  1908. "INP2", "INP3"
  1909. };
  1910. static const struct soc_enum adc2_enum =
  1911. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 7,
  1912. ARRAY_SIZE(adc2_mux_text), adc2_mux_text);
  1913. static const struct snd_kcontrol_new tx_adc2_mux =
  1914. SOC_DAPM_ENUM("ADC2 MUX Mux", adc2_enum);
  1915. static const char * const adc3_mux_text[] = {
  1916. "INP4", "INP6"
  1917. };
  1918. static const struct soc_enum adc3_enum =
  1919. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 6,
  1920. ARRAY_SIZE(adc3_mux_text), adc3_mux_text);
  1921. static const struct snd_kcontrol_new tx_adc3_mux =
  1922. SOC_DAPM_ENUM("ADC3 MUX Mux", adc3_enum);
  1923. static const char * const adc4_mux_text[] = {
  1924. "INP5", "INP7"
  1925. };
  1926. static const struct soc_enum adc4_enum =
  1927. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 5,
  1928. ARRAY_SIZE(adc4_mux_text), adc4_mux_text);
  1929. static const struct snd_kcontrol_new tx_adc4_mux =
  1930. SOC_DAPM_ENUM("ADC4 MUX Mux", adc4_enum);
  1931. static const char * const rdac3_mux_text[] = {
  1932. "RX1", "RX3"
  1933. };
  1934. static const char * const hdr12_mux_text[] = {
  1935. "NO_HDR12", "HDR12"
  1936. };
  1937. static const struct soc_enum hdr12_enum =
  1938. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 4,
  1939. ARRAY_SIZE(hdr12_mux_text), hdr12_mux_text);
  1940. static const struct snd_kcontrol_new tx_hdr12_mux =
  1941. SOC_DAPM_ENUM("HDR12 MUX Mux", hdr12_enum);
  1942. static const char * const hdr34_mux_text[] = {
  1943. "NO_HDR34", "HDR34"
  1944. };
  1945. static const struct soc_enum hdr34_enum =
  1946. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 3,
  1947. ARRAY_SIZE(hdr34_mux_text), hdr34_mux_text);
  1948. static const struct snd_kcontrol_new tx_hdr34_mux =
  1949. SOC_DAPM_ENUM("HDR34 MUX Mux", hdr34_enum);
  1950. static const struct soc_enum rdac3_enum =
  1951. SOC_ENUM_SINGLE(WCD938X_DIGITAL_CDC_EAR_PATH_CTL, 0,
  1952. ARRAY_SIZE(rdac3_mux_text), rdac3_mux_text);
  1953. static const struct snd_kcontrol_new rx_rdac3_mux =
  1954. SOC_DAPM_ENUM("RDAC3_MUX Mux", rdac3_enum);
  1955. static const struct snd_soc_dapm_widget wcd938x_dapm_widgets[] = {
  1956. /*input widgets*/
  1957. SND_SOC_DAPM_INPUT("AMIC1"),
  1958. SND_SOC_DAPM_INPUT("AMIC2"),
  1959. SND_SOC_DAPM_INPUT("AMIC3"),
  1960. SND_SOC_DAPM_INPUT("AMIC4"),
  1961. SND_SOC_DAPM_INPUT("AMIC5"),
  1962. SND_SOC_DAPM_INPUT("AMIC6"),
  1963. SND_SOC_DAPM_INPUT("AMIC7"),
  1964. SND_SOC_DAPM_INPUT("IN1_HPHL"),
  1965. SND_SOC_DAPM_INPUT("IN2_HPHR"),
  1966. SND_SOC_DAPM_INPUT("IN3_AUX"),
  1967. /*tx widgets*/
  1968. SND_SOC_DAPM_ADC_E("ADC1", NULL, SND_SOC_NOPM, 0, 0,
  1969. wcd938x_codec_enable_adc,
  1970. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1971. SND_SOC_DAPM_ADC_E("ADC2", NULL, SND_SOC_NOPM, 1, 0,
  1972. wcd938x_codec_enable_adc,
  1973. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1974. SND_SOC_DAPM_ADC_E("ADC3", NULL, SND_SOC_NOPM, 2, 0,
  1975. wcd938x_codec_enable_adc,
  1976. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1977. SND_SOC_DAPM_ADC_E("ADC4", NULL, SND_SOC_NOPM, 3, 0,
  1978. wcd938x_codec_enable_adc,
  1979. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1980. SND_SOC_DAPM_ADC_E("DMIC1", NULL, SND_SOC_NOPM, 0, 0,
  1981. wcd938x_codec_enable_dmic,
  1982. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1983. SND_SOC_DAPM_ADC_E("DMIC2", NULL, SND_SOC_NOPM, 1, 0,
  1984. wcd938x_codec_enable_dmic,
  1985. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1986. SND_SOC_DAPM_ADC_E("DMIC3", NULL, SND_SOC_NOPM, 2, 0,
  1987. wcd938x_codec_enable_dmic,
  1988. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1989. SND_SOC_DAPM_ADC_E("DMIC4", NULL, SND_SOC_NOPM, 3, 0,
  1990. wcd938x_codec_enable_dmic,
  1991. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1992. SND_SOC_DAPM_ADC_E("DMIC5", NULL, SND_SOC_NOPM, 4, 0,
  1993. wcd938x_codec_enable_dmic,
  1994. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1995. SND_SOC_DAPM_ADC_E("DMIC6", NULL, SND_SOC_NOPM, 5, 0,
  1996. wcd938x_codec_enable_dmic,
  1997. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1998. SND_SOC_DAPM_ADC_E("DMIC7", NULL, SND_SOC_NOPM, 6, 0,
  1999. wcd938x_codec_enable_dmic,
  2000. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2001. SND_SOC_DAPM_ADC_E("DMIC8", NULL, SND_SOC_NOPM, 7, 0,
  2002. wcd938x_codec_enable_dmic,
  2003. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2004. SND_SOC_DAPM_MIXER_E("ADC1 REQ", SND_SOC_NOPM, 0, 0,
  2005. NULL, 0, wcd938x_enable_req,
  2006. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2007. SND_SOC_DAPM_MIXER_E("ADC2 REQ", SND_SOC_NOPM, 1, 0,
  2008. NULL, 0, wcd938x_enable_req,
  2009. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2010. SND_SOC_DAPM_MIXER_E("ADC3 REQ", SND_SOC_NOPM, 2, 0,
  2011. NULL, 0, wcd938x_enable_req,
  2012. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2013. SND_SOC_DAPM_MIXER_E("ADC4 REQ", SND_SOC_NOPM, 3, 0,
  2014. NULL, 0, wcd938x_enable_req,
  2015. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2016. SND_SOC_DAPM_MUX("ADC2 MUX", SND_SOC_NOPM, 0, 0,
  2017. &tx_adc2_mux),
  2018. SND_SOC_DAPM_MUX("ADC3 MUX", SND_SOC_NOPM, 0, 0,
  2019. &tx_adc3_mux),
  2020. SND_SOC_DAPM_MUX("ADC4 MUX", SND_SOC_NOPM, 0, 0,
  2021. &tx_adc4_mux),
  2022. SND_SOC_DAPM_MUX("HDR12 MUX", SND_SOC_NOPM, 0, 0,
  2023. &tx_hdr12_mux),
  2024. SND_SOC_DAPM_MUX("HDR34 MUX", SND_SOC_NOPM, 0, 0,
  2025. &tx_hdr34_mux),
  2026. /*tx mixers*/
  2027. SND_SOC_DAPM_MIXER_E("ADC1_MIXER", SND_SOC_NOPM, 0, 0,
  2028. adc1_switch, ARRAY_SIZE(adc1_switch),
  2029. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2030. SND_SOC_DAPM_POST_PMD),
  2031. SND_SOC_DAPM_MIXER_E("ADC2_MIXER", SND_SOC_NOPM, 0, 0,
  2032. adc2_switch, ARRAY_SIZE(adc2_switch),
  2033. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2034. SND_SOC_DAPM_POST_PMD),
  2035. SND_SOC_DAPM_MIXER_E("ADC3_MIXER", SND_SOC_NOPM, 0, 0, adc3_switch,
  2036. ARRAY_SIZE(adc3_switch), wcd938x_tx_swr_ctrl,
  2037. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2038. SND_SOC_DAPM_MIXER_E("ADC4_MIXER", SND_SOC_NOPM, 0, 0, adc4_switch,
  2039. ARRAY_SIZE(adc4_switch), wcd938x_tx_swr_ctrl,
  2040. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2041. SND_SOC_DAPM_MIXER_E("DMIC1_MIXER", SND_SOC_NOPM, 0,
  2042. 0, dmic1_switch, ARRAY_SIZE(dmic1_switch),
  2043. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2044. SND_SOC_DAPM_POST_PMD),
  2045. SND_SOC_DAPM_MIXER_E("DMIC2_MIXER", SND_SOC_NOPM, 0,
  2046. 0, dmic2_switch, ARRAY_SIZE(dmic2_switch),
  2047. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2048. SND_SOC_DAPM_POST_PMD),
  2049. SND_SOC_DAPM_MIXER_E("DMIC3_MIXER", SND_SOC_NOPM, 0,
  2050. 0, dmic3_switch, ARRAY_SIZE(dmic3_switch),
  2051. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2052. SND_SOC_DAPM_POST_PMD),
  2053. SND_SOC_DAPM_MIXER_E("DMIC4_MIXER", SND_SOC_NOPM, 0,
  2054. 0, dmic4_switch, ARRAY_SIZE(dmic4_switch),
  2055. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2056. SND_SOC_DAPM_POST_PMD),
  2057. SND_SOC_DAPM_MIXER_E("DMIC5_MIXER", SND_SOC_NOPM, 0,
  2058. 0, dmic5_switch, ARRAY_SIZE(dmic5_switch),
  2059. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2060. SND_SOC_DAPM_POST_PMD),
  2061. SND_SOC_DAPM_MIXER_E("DMIC6_MIXER", SND_SOC_NOPM, 0,
  2062. 0, dmic6_switch, ARRAY_SIZE(dmic6_switch),
  2063. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2064. SND_SOC_DAPM_POST_PMD),
  2065. SND_SOC_DAPM_MIXER_E("DMIC7_MIXER", SND_SOC_NOPM, 0,
  2066. 0, dmic7_switch, ARRAY_SIZE(dmic7_switch),
  2067. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2068. SND_SOC_DAPM_POST_PMD),
  2069. SND_SOC_DAPM_MIXER_E("DMIC8_MIXER", SND_SOC_NOPM, 0,
  2070. 0, dmic8_switch, ARRAY_SIZE(dmic8_switch),
  2071. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2072. SND_SOC_DAPM_POST_PMD),
  2073. /* micbias widgets*/
  2074. SND_SOC_DAPM_MICBIAS_E("MIC BIAS1", SND_SOC_NOPM, 0, 0,
  2075. wcd938x_codec_enable_micbias,
  2076. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2077. SND_SOC_DAPM_POST_PMD),
  2078. SND_SOC_DAPM_MICBIAS_E("MIC BIAS2", SND_SOC_NOPM, 0, 0,
  2079. wcd938x_codec_enable_micbias,
  2080. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2081. SND_SOC_DAPM_POST_PMD),
  2082. SND_SOC_DAPM_MICBIAS_E("MIC BIAS3", SND_SOC_NOPM, 0, 0,
  2083. wcd938x_codec_enable_micbias,
  2084. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2085. SND_SOC_DAPM_POST_PMD),
  2086. SND_SOC_DAPM_MICBIAS_E("MIC BIAS4", SND_SOC_NOPM, 0, 0,
  2087. wcd938x_codec_enable_micbias,
  2088. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2089. SND_SOC_DAPM_POST_PMD),
  2090. SND_SOC_DAPM_SUPPLY_S("CLS_H_PORT", 1, SND_SOC_NOPM, 0, 0,
  2091. wcd938x_enable_clsh,
  2092. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2093. /*rx widgets*/
  2094. SND_SOC_DAPM_PGA_E("EAR PGA", WCD938X_ANA_EAR, 7, 0, NULL, 0,
  2095. wcd938x_codec_enable_ear_pa,
  2096. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2097. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2098. SND_SOC_DAPM_PGA_E("AUX PGA", WCD938X_AUX_AUXPA, 7, 0, NULL, 0,
  2099. wcd938x_codec_enable_aux_pa,
  2100. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2101. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2102. SND_SOC_DAPM_PGA_E("HPHL PGA", WCD938X_ANA_HPH, 7, 0, NULL, 0,
  2103. wcd938x_codec_enable_hphl_pa,
  2104. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2105. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2106. SND_SOC_DAPM_PGA_E("HPHR PGA", WCD938X_ANA_HPH, 6, 0, NULL, 0,
  2107. wcd938x_codec_enable_hphr_pa,
  2108. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2109. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2110. SND_SOC_DAPM_DAC_E("RDAC1", NULL, SND_SOC_NOPM, 0, 0,
  2111. wcd938x_codec_hphl_dac_event,
  2112. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2113. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2114. SND_SOC_DAPM_DAC_E("RDAC2", NULL, SND_SOC_NOPM, 0, 0,
  2115. wcd938x_codec_hphr_dac_event,
  2116. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2117. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2118. SND_SOC_DAPM_DAC_E("RDAC3", NULL, SND_SOC_NOPM, 0, 0,
  2119. wcd938x_codec_ear_dac_event,
  2120. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2121. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2122. SND_SOC_DAPM_DAC_E("RDAC4", NULL, SND_SOC_NOPM, 0, 0,
  2123. wcd938x_codec_aux_dac_event,
  2124. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2125. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2126. SND_SOC_DAPM_MUX("RDAC3_MUX", SND_SOC_NOPM, 0, 0, &rx_rdac3_mux),
  2127. SND_SOC_DAPM_MIXER_E("RX1", SND_SOC_NOPM, 0, 0, NULL, 0,
  2128. wcd938x_enable_rx1, SND_SOC_DAPM_PRE_PMU |
  2129. SND_SOC_DAPM_POST_PMD),
  2130. SND_SOC_DAPM_MIXER_E("RX2", SND_SOC_NOPM, 0, 0, NULL, 0,
  2131. wcd938x_enable_rx2, SND_SOC_DAPM_PRE_PMU |
  2132. SND_SOC_DAPM_POST_PMD),
  2133. SND_SOC_DAPM_MIXER_E("RX3", SND_SOC_NOPM, 0, 0, NULL, 0,
  2134. wcd938x_enable_rx3, SND_SOC_DAPM_PRE_PMU |
  2135. SND_SOC_DAPM_POST_PMD),
  2136. /* rx mixer widgets*/
  2137. SND_SOC_DAPM_MIXER("EAR_RDAC", SND_SOC_NOPM, 0, 0,
  2138. ear_rdac_switch, ARRAY_SIZE(ear_rdac_switch)),
  2139. SND_SOC_DAPM_MIXER("AUX_RDAC", SND_SOC_NOPM, 0, 0,
  2140. aux_rdac_switch, ARRAY_SIZE(aux_rdac_switch)),
  2141. SND_SOC_DAPM_MIXER("HPHL_RDAC", SND_SOC_NOPM, 0, 0,
  2142. hphl_rdac_switch, ARRAY_SIZE(hphl_rdac_switch)),
  2143. SND_SOC_DAPM_MIXER("HPHR_RDAC", SND_SOC_NOPM, 0, 0,
  2144. hphr_rdac_switch, ARRAY_SIZE(hphr_rdac_switch)),
  2145. /*output widgets tx*/
  2146. SND_SOC_DAPM_OUTPUT("ADC1_OUTPUT"),
  2147. SND_SOC_DAPM_OUTPUT("ADC2_OUTPUT"),
  2148. SND_SOC_DAPM_OUTPUT("ADC3_OUTPUT"),
  2149. SND_SOC_DAPM_OUTPUT("ADC4_OUTPUT"),
  2150. SND_SOC_DAPM_OUTPUT("DMIC1_OUTPUT"),
  2151. SND_SOC_DAPM_OUTPUT("DMIC2_OUTPUT"),
  2152. SND_SOC_DAPM_OUTPUT("DMIC3_OUTPUT"),
  2153. SND_SOC_DAPM_OUTPUT("DMIC4_OUTPUT"),
  2154. SND_SOC_DAPM_OUTPUT("DMIC5_OUTPUT"),
  2155. SND_SOC_DAPM_OUTPUT("DMIC6_OUTPUT"),
  2156. SND_SOC_DAPM_OUTPUT("DMIC7_OUTPUT"),
  2157. SND_SOC_DAPM_OUTPUT("DMIC8_OUTPUT"),
  2158. /*output widgets rx*/
  2159. SND_SOC_DAPM_OUTPUT("EAR"),
  2160. SND_SOC_DAPM_OUTPUT("AUX"),
  2161. SND_SOC_DAPM_OUTPUT("HPHL"),
  2162. SND_SOC_DAPM_OUTPUT("HPHR"),
  2163. };
  2164. static const struct snd_soc_dapm_route wcd938x_audio_map[] = {
  2165. {"ADC1_OUTPUT", NULL, "ADC1_MIXER"},
  2166. {"ADC1_MIXER", "Switch", "ADC1 REQ"},
  2167. {"ADC1 REQ", NULL, "ADC1"},
  2168. {"ADC1", NULL, "AMIC1"},
  2169. {"ADC2_OUTPUT", NULL, "ADC2_MIXER"},
  2170. {"ADC2_MIXER", "Switch", "ADC2 REQ"},
  2171. {"ADC2 REQ", NULL, "ADC2"},
  2172. {"ADC2", NULL, "HDR12 MUX"},
  2173. {"HDR12 MUX", "NO_HDR12", "ADC2 MUX"},
  2174. {"HDR12 MUX", "HDR12", "AMIC1"},
  2175. {"ADC2 MUX", "INP3", "AMIC3"},
  2176. {"ADC2 MUX", "INP2", "AMIC2"},
  2177. {"ADC3_OUTPUT", NULL, "ADC3_MIXER"},
  2178. {"ADC3_MIXER", "Switch", "ADC3 REQ"},
  2179. {"ADC3 REQ", NULL, "ADC3"},
  2180. {"ADC3", NULL, "HDR34 MUX"},
  2181. {"HDR34 MUX", "NO_HDR34", "ADC3 MUX"},
  2182. {"HDR34 MUX", "HDR34", "AMIC5"},
  2183. {"ADC3 MUX", "INP4", "AMIC4"},
  2184. {"ADC3 MUX", "INP6", "AMIC6"},
  2185. {"ADC4_OUTPUT", NULL, "ADC4_MIXER"},
  2186. {"ADC4_MIXER", "Switch", "ADC4 REQ"},
  2187. {"ADC4 REQ", NULL, "ADC4"},
  2188. {"ADC4", NULL, "ADC4 MUX"},
  2189. {"ADC4 MUX", "INP5", "AMIC5"},
  2190. {"ADC4 MUX", "INP7", "AMIC7"},
  2191. {"DMIC1_OUTPUT", NULL, "DMIC1_MIXER"},
  2192. {"DMIC1_MIXER", "Switch", "DMIC1"},
  2193. {"DMIC2_OUTPUT", NULL, "DMIC2_MIXER"},
  2194. {"DMIC2_MIXER", "Switch", "DMIC2"},
  2195. {"DMIC3_OUTPUT", NULL, "DMIC3_MIXER"},
  2196. {"DMIC3_MIXER", "Switch", "DMIC3"},
  2197. {"DMIC4_OUTPUT", NULL, "DMIC4_MIXER"},
  2198. {"DMIC4_MIXER", "Switch", "DMIC4"},
  2199. {"DMIC5_OUTPUT", NULL, "DMIC5_MIXER"},
  2200. {"DMIC5_MIXER", "Switch", "DMIC5"},
  2201. {"DMIC6_OUTPUT", NULL, "DMIC6_MIXER"},
  2202. {"DMIC6_MIXER", "Switch", "DMIC6"},
  2203. {"DMIC7_OUTPUT", NULL, "DMIC7_MIXER"},
  2204. {"DMIC7_MIXER", "Switch", "DMIC7"},
  2205. {"DMIC8_OUTPUT", NULL, "DMIC8_MIXER"},
  2206. {"DMIC8_MIXER", "Switch", "DMIC8"},
  2207. {"IN1_HPHL", NULL, "CLS_H_PORT"},
  2208. {"RX1", NULL, "IN1_HPHL"},
  2209. {"RDAC1", NULL, "RX1"},
  2210. {"HPHL_RDAC", "Switch", "RDAC1"},
  2211. {"HPHL PGA", NULL, "HPHL_RDAC"},
  2212. {"HPHL", NULL, "HPHL PGA"},
  2213. {"IN2_HPHR", NULL, "CLS_H_PORT"},
  2214. {"RX2", NULL, "IN2_HPHR"},
  2215. {"RDAC2", NULL, "RX2"},
  2216. {"HPHR_RDAC", "Switch", "RDAC2"},
  2217. {"HPHR PGA", NULL, "HPHR_RDAC"},
  2218. {"HPHR", NULL, "HPHR PGA"},
  2219. {"IN3_AUX", NULL, "CLS_H_PORT"},
  2220. {"RX3", NULL, "IN3_AUX"},
  2221. {"RDAC4", NULL, "RX3"},
  2222. {"AUX_RDAC", "Switch", "RDAC4"},
  2223. {"AUX PGA", NULL, "AUX_RDAC"},
  2224. {"AUX", NULL, "AUX PGA"},
  2225. {"RDAC3_MUX", "RX3", "RX3"},
  2226. {"RDAC3_MUX", "RX1", "RX1"},
  2227. {"RDAC3", NULL, "RDAC3_MUX"},
  2228. {"EAR_RDAC", "Switch", "RDAC3"},
  2229. {"EAR PGA", NULL, "EAR_RDAC"},
  2230. {"EAR", NULL, "EAR PGA"},
  2231. };
  2232. static ssize_t wcd938x_version_read(struct snd_info_entry *entry,
  2233. void *file_private_data,
  2234. struct file *file,
  2235. char __user *buf, size_t count,
  2236. loff_t pos)
  2237. {
  2238. struct wcd938x_priv *priv;
  2239. char buffer[WCD938X_VERSION_ENTRY_SIZE];
  2240. int len = 0;
  2241. priv = (struct wcd938x_priv *) entry->private_data;
  2242. if (!priv) {
  2243. pr_err("%s: wcd938x priv is null\n", __func__);
  2244. return -EINVAL;
  2245. }
  2246. switch (priv->version) {
  2247. case WCD938X_VERSION_1_0:
  2248. len = snprintf(buffer, sizeof(buffer), "WCD938X_1_0\n");
  2249. break;
  2250. default:
  2251. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  2252. }
  2253. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  2254. }
  2255. static struct snd_info_entry_ops wcd938x_info_ops = {
  2256. .read = wcd938x_version_read,
  2257. };
  2258. /*
  2259. * wcd938x_info_create_codec_entry - creates wcd938x module
  2260. * @codec_root: The parent directory
  2261. * @component: component instance
  2262. *
  2263. * Creates wcd938x module and version entry under the given
  2264. * parent directory.
  2265. *
  2266. * Return: 0 on success or negative error code on failure.
  2267. */
  2268. int wcd938x_info_create_codec_entry(struct snd_info_entry *codec_root,
  2269. struct snd_soc_component *component)
  2270. {
  2271. struct snd_info_entry *version_entry;
  2272. struct wcd938x_priv *priv;
  2273. struct snd_soc_card *card;
  2274. if (!codec_root || !component)
  2275. return -EINVAL;
  2276. priv = snd_soc_component_get_drvdata(component);
  2277. if (priv->entry) {
  2278. dev_dbg(priv->dev,
  2279. "%s:wcd938x module already created\n", __func__);
  2280. return 0;
  2281. }
  2282. card = component->card;
  2283. priv->entry = snd_info_create_subdir(codec_root->module,
  2284. "wcd938x", codec_root);
  2285. if (!priv->entry) {
  2286. dev_dbg(component->dev, "%s: failed to create wcd938x entry\n",
  2287. __func__);
  2288. return -ENOMEM;
  2289. }
  2290. version_entry = snd_info_create_card_entry(card->snd_card,
  2291. "version",
  2292. priv->entry);
  2293. if (!version_entry) {
  2294. dev_dbg(component->dev, "%s: failed to create wcd938x version entry\n",
  2295. __func__);
  2296. return -ENOMEM;
  2297. }
  2298. version_entry->private_data = priv;
  2299. version_entry->size = WCD938X_VERSION_ENTRY_SIZE;
  2300. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  2301. version_entry->c.ops = &wcd938x_info_ops;
  2302. if (snd_info_register(version_entry) < 0) {
  2303. snd_info_free_entry(version_entry);
  2304. return -ENOMEM;
  2305. }
  2306. priv->version_entry = version_entry;
  2307. return 0;
  2308. }
  2309. EXPORT_SYMBOL(wcd938x_info_create_codec_entry);
  2310. static int wcd938x_soc_codec_probe(struct snd_soc_component *component)
  2311. {
  2312. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2313. struct snd_soc_dapm_context *dapm =
  2314. snd_soc_component_get_dapm(component);
  2315. int variant;
  2316. int ret = -EINVAL;
  2317. dev_info(component->dev, "%s()\n", __func__);
  2318. wcd938x = snd_soc_component_get_drvdata(component);
  2319. if (!wcd938x)
  2320. return -EINVAL;
  2321. wcd938x->component = component;
  2322. snd_soc_component_init_regmap(component, wcd938x->regmap);
  2323. variant = (snd_soc_component_read32(component,
  2324. WCD938X_DIGITAL_EFUSE_REG_0) & 0x1E) >> 1;
  2325. wcd938x->variant = variant;
  2326. wcd938x->fw_data = devm_kzalloc(component->dev,
  2327. sizeof(*(wcd938x->fw_data)),
  2328. GFP_KERNEL);
  2329. if (!wcd938x->fw_data) {
  2330. dev_err(component->dev, "Failed to allocate fw_data\n");
  2331. ret = -ENOMEM;
  2332. goto err;
  2333. }
  2334. set_bit(WCD9XXX_MBHC_CAL, wcd938x->fw_data->cal_bit);
  2335. ret = wcd_cal_create_hwdep(wcd938x->fw_data,
  2336. WCD9XXX_CODEC_HWDEP_NODE, component);
  2337. if (ret < 0) {
  2338. dev_err(component->dev, "%s hwdep failed %d\n", __func__, ret);
  2339. goto err_hwdep;
  2340. }
  2341. ret = wcd938x_mbhc_init(&wcd938x->mbhc, component, wcd938x->fw_data);
  2342. if (ret) {
  2343. pr_err("%s: mbhc initialization failed\n", __func__);
  2344. goto err_hwdep;
  2345. }
  2346. snd_soc_dapm_ignore_suspend(dapm, "AMIC1");
  2347. snd_soc_dapm_ignore_suspend(dapm, "AMIC2");
  2348. snd_soc_dapm_ignore_suspend(dapm, "AMIC3");
  2349. snd_soc_dapm_ignore_suspend(dapm, "AMIC4");
  2350. snd_soc_dapm_ignore_suspend(dapm, "AMIC5");
  2351. snd_soc_dapm_ignore_suspend(dapm, "AMIC6");
  2352. snd_soc_dapm_ignore_suspend(dapm, "AMIC7");
  2353. snd_soc_dapm_ignore_suspend(dapm, "DMIC1_OUTPUT");
  2354. snd_soc_dapm_ignore_suspend(dapm, "DMIC2_OUTPUT");
  2355. snd_soc_dapm_ignore_suspend(dapm, "DMIC3_OUTPUT");
  2356. snd_soc_dapm_ignore_suspend(dapm, "DMIC4_OUTPUT");
  2357. snd_soc_dapm_ignore_suspend(dapm, "DMIC5_OUTPUT");
  2358. snd_soc_dapm_ignore_suspend(dapm, "DMIC6_OUTPUT");
  2359. snd_soc_dapm_ignore_suspend(dapm, "DMIC7_OUTPUT");
  2360. snd_soc_dapm_ignore_suspend(dapm, "DMIC8_OUTPUT");
  2361. snd_soc_dapm_ignore_suspend(dapm, "ADC1_OUTPUT");
  2362. snd_soc_dapm_ignore_suspend(dapm, "ADC2_OUTPUT");
  2363. snd_soc_dapm_ignore_suspend(dapm, "ADC3_OUTPUT");
  2364. snd_soc_dapm_ignore_suspend(dapm, "ADC4_OUTPUT");
  2365. snd_soc_dapm_ignore_suspend(dapm, "IN1_HPHL");
  2366. snd_soc_dapm_ignore_suspend(dapm, "IN2_HPHR");
  2367. snd_soc_dapm_ignore_suspend(dapm, "IN3_AUX");
  2368. snd_soc_dapm_ignore_suspend(dapm, "EAR");
  2369. snd_soc_dapm_ignore_suspend(dapm, "AUX");
  2370. snd_soc_dapm_ignore_suspend(dapm, "HPHL");
  2371. snd_soc_dapm_ignore_suspend(dapm, "HPHR");
  2372. snd_soc_dapm_sync(dapm);
  2373. wcd_cls_h_init(&wcd938x->clsh_info);
  2374. wcd938x_init_reg(component);
  2375. if (wcd938x->variant == WCD9380) {
  2376. ret = snd_soc_add_component_controls(component, wcd9380_snd_controls,
  2377. ARRAY_SIZE(wcd9380_snd_controls));
  2378. if (ret < 0) {
  2379. dev_err(component->dev,
  2380. "%s: Failed to add snd ctrls for variant: %d\n",
  2381. __func__, wcd938x->variant);
  2382. goto err_hwdep;
  2383. }
  2384. }
  2385. if (wcd938x->variant == WCD9385) {
  2386. ret = snd_soc_add_component_controls(component, wcd9385_snd_controls,
  2387. ARRAY_SIZE(wcd9385_snd_controls));
  2388. if (ret < 0) {
  2389. dev_err(component->dev,
  2390. "%s: Failed to add snd ctrls for variant: %d\n",
  2391. __func__, wcd938x->variant);
  2392. goto err_hwdep;
  2393. }
  2394. }
  2395. wcd938x->version = WCD938X_VERSION_1_0;
  2396. /* Register event notifier */
  2397. wcd938x->nblock.notifier_call = wcd938x_event_notify;
  2398. if (wcd938x->register_notifier) {
  2399. ret = wcd938x->register_notifier(wcd938x->handle,
  2400. &wcd938x->nblock,
  2401. true);
  2402. if (ret) {
  2403. dev_err(component->dev,
  2404. "%s: Failed to register notifier %d\n",
  2405. __func__, ret);
  2406. return ret;
  2407. }
  2408. }
  2409. return ret;
  2410. err_hwdep:
  2411. wcd938x->fw_data = NULL;
  2412. err:
  2413. return ret;
  2414. }
  2415. static void wcd938x_soc_codec_remove(struct snd_soc_component *component)
  2416. {
  2417. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2418. if (!wcd938x) {
  2419. dev_err(component->dev, "%s: wcd938x is already NULL\n",
  2420. __func__);
  2421. return;
  2422. }
  2423. if (wcd938x->register_notifier)
  2424. wcd938x->register_notifier(wcd938x->handle,
  2425. &wcd938x->nblock,
  2426. false);
  2427. }
  2428. static struct snd_soc_component_driver soc_codec_dev_wcd938x = {
  2429. .name = WCD938X_DRV_NAME,
  2430. .probe = wcd938x_soc_codec_probe,
  2431. .remove = wcd938x_soc_codec_remove,
  2432. .controls = wcd938x_snd_controls,
  2433. .num_controls = ARRAY_SIZE(wcd938x_snd_controls),
  2434. .dapm_widgets = wcd938x_dapm_widgets,
  2435. .num_dapm_widgets = ARRAY_SIZE(wcd938x_dapm_widgets),
  2436. .dapm_routes = wcd938x_audio_map,
  2437. .num_dapm_routes = ARRAY_SIZE(wcd938x_audio_map),
  2438. };
  2439. static int wcd938x_reset(struct device *dev)
  2440. {
  2441. struct wcd938x_priv *wcd938x = NULL;
  2442. int rc = 0;
  2443. int value = 0;
  2444. if (!dev)
  2445. return -ENODEV;
  2446. wcd938x = dev_get_drvdata(dev);
  2447. if (!wcd938x)
  2448. return -EINVAL;
  2449. if (!wcd938x->rst_np) {
  2450. dev_err(dev, "%s: reset gpio device node not specified\n",
  2451. __func__);
  2452. return -EINVAL;
  2453. }
  2454. value = msm_cdc_pinctrl_get_state(wcd938x->rst_np);
  2455. if (value > 0)
  2456. return 0;
  2457. rc = msm_cdc_pinctrl_select_sleep_state(wcd938x->rst_np);
  2458. if (rc) {
  2459. dev_err(dev, "%s: wcd sleep state request fail!\n",
  2460. __func__);
  2461. return rc;
  2462. }
  2463. /* 20us sleep required after pulling the reset gpio to LOW */
  2464. usleep_range(20, 30);
  2465. rc = msm_cdc_pinctrl_select_active_state(wcd938x->rst_np);
  2466. if (rc) {
  2467. dev_err(dev, "%s: wcd active state request fail!\n",
  2468. __func__);
  2469. return rc;
  2470. }
  2471. /* 20us sleep required after pulling the reset gpio to HIGH */
  2472. usleep_range(20, 30);
  2473. return rc;
  2474. }
  2475. static int wcd938x_read_of_property_u32(struct device *dev, const char *name,
  2476. u32 *val)
  2477. {
  2478. int rc = 0;
  2479. rc = of_property_read_u32(dev->of_node, name, val);
  2480. if (rc)
  2481. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  2482. __func__, name, dev->of_node->full_name);
  2483. return rc;
  2484. }
  2485. static void wcd938x_dt_parse_micbias_info(struct device *dev,
  2486. struct wcd938x_micbias_setting *mb)
  2487. {
  2488. u32 prop_val = 0;
  2489. int rc = 0;
  2490. /* MB1 */
  2491. if (of_find_property(dev->of_node, "qcom,cdc-micbias1-mv",
  2492. NULL)) {
  2493. rc = wcd938x_read_of_property_u32(dev,
  2494. "qcom,cdc-micbias1-mv",
  2495. &prop_val);
  2496. if (!rc)
  2497. mb->micb1_mv = prop_val;
  2498. } else {
  2499. dev_info(dev, "%s: Micbias1 DT property not found\n",
  2500. __func__);
  2501. }
  2502. /* MB2 */
  2503. if (of_find_property(dev->of_node, "qcom,cdc-micbias2-mv",
  2504. NULL)) {
  2505. rc = wcd938x_read_of_property_u32(dev,
  2506. "qcom,cdc-micbias2-mv",
  2507. &prop_val);
  2508. if (!rc)
  2509. mb->micb2_mv = prop_val;
  2510. } else {
  2511. dev_info(dev, "%s: Micbias2 DT property not found\n",
  2512. __func__);
  2513. }
  2514. /* MB3 */
  2515. if (of_find_property(dev->of_node, "qcom,cdc-micbias3-mv",
  2516. NULL)) {
  2517. rc = wcd938x_read_of_property_u32(dev,
  2518. "qcom,cdc-micbias3-mv",
  2519. &prop_val);
  2520. if (!rc)
  2521. mb->micb3_mv = prop_val;
  2522. } else {
  2523. dev_info(dev, "%s: Micbias3 DT property not found\n",
  2524. __func__);
  2525. }
  2526. }
  2527. static int wcd938x_reset_low(struct device *dev)
  2528. {
  2529. struct wcd938x_priv *wcd938x = NULL;
  2530. int rc = 0;
  2531. if (!dev)
  2532. return -ENODEV;
  2533. wcd938x = dev_get_drvdata(dev);
  2534. if (!wcd938x)
  2535. return -EINVAL;
  2536. if (!wcd938x->rst_np) {
  2537. dev_err(dev, "%s: reset gpio device node not specified\n",
  2538. __func__);
  2539. return -EINVAL;
  2540. }
  2541. rc = msm_cdc_pinctrl_select_sleep_state(wcd938x->rst_np);
  2542. if (rc) {
  2543. dev_err(dev, "%s: wcd sleep state request fail!\n",
  2544. __func__);
  2545. return rc;
  2546. }
  2547. /* 20us sleep required after pulling the reset gpio to LOW */
  2548. usleep_range(20, 30);
  2549. return rc;
  2550. }
  2551. struct wcd938x_pdata *wcd938x_populate_dt_data(struct device *dev)
  2552. {
  2553. struct wcd938x_pdata *pdata = NULL;
  2554. pdata = devm_kzalloc(dev, sizeof(struct wcd938x_pdata),
  2555. GFP_KERNEL);
  2556. if (!pdata)
  2557. return NULL;
  2558. pdata->rst_np = of_parse_phandle(dev->of_node,
  2559. "qcom,wcd-rst-gpio-node", 0);
  2560. if (!pdata->rst_np) {
  2561. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  2562. __func__, "qcom,wcd-rst-gpio-node",
  2563. dev->of_node->full_name);
  2564. return NULL;
  2565. }
  2566. /* Parse power supplies */
  2567. msm_cdc_get_power_supplies(dev, &pdata->regulator,
  2568. &pdata->num_supplies);
  2569. if (!pdata->regulator || (pdata->num_supplies <= 0)) {
  2570. dev_err(dev, "%s: no power supplies defined for codec\n",
  2571. __func__);
  2572. return NULL;
  2573. }
  2574. pdata->rx_slave = of_parse_phandle(dev->of_node, "qcom,rx-slave", 0);
  2575. pdata->tx_slave = of_parse_phandle(dev->of_node, "qcom,tx-slave", 0);
  2576. wcd938x_dt_parse_micbias_info(dev, &pdata->micbias);
  2577. return pdata;
  2578. }
  2579. static irqreturn_t wcd938x_wd_handle_irq(int irq, void *data)
  2580. {
  2581. pr_err_ratelimited("%s: Watchdog interrupt for irq =%d triggered\n",
  2582. __func__, irq);
  2583. return IRQ_HANDLED;
  2584. }
  2585. static int wcd938x_bind(struct device *dev)
  2586. {
  2587. int ret = 0, i = 0;
  2588. struct wcd938x_pdata *pdata = dev_get_platdata(dev);
  2589. struct wcd938x_priv *wcd938x = dev_get_drvdata(dev);
  2590. /*
  2591. * Add 5msec delay to provide sufficient time for
  2592. * soundwire auto enumeration of slave devices as
  2593. * as per HW requirement.
  2594. */
  2595. usleep_range(5000, 5010);
  2596. ret = component_bind_all(dev, wcd938x);
  2597. if (ret) {
  2598. dev_err(dev, "%s: Slave bind failed, ret = %d\n",
  2599. __func__, ret);
  2600. return ret;
  2601. }
  2602. wcd938x->rx_swr_dev = get_matching_swr_slave_device(pdata->rx_slave);
  2603. if (!wcd938x->rx_swr_dev) {
  2604. dev_err(dev, "%s: Could not find RX swr slave device\n",
  2605. __func__);
  2606. ret = -ENODEV;
  2607. goto err;
  2608. }
  2609. wcd938x->tx_swr_dev = get_matching_swr_slave_device(pdata->tx_slave);
  2610. if (!wcd938x->tx_swr_dev) {
  2611. dev_err(dev, "%s: Could not find TX swr slave device\n",
  2612. __func__);
  2613. ret = -ENODEV;
  2614. goto err;
  2615. }
  2616. wcd938x->regmap = devm_regmap_init_swr(wcd938x->tx_swr_dev,
  2617. &wcd938x_regmap_config);
  2618. if (!wcd938x->regmap) {
  2619. dev_err(dev, "%s: Regmap init failed\n",
  2620. __func__);
  2621. goto err;
  2622. }
  2623. /* Set all interupts as edge triggered */
  2624. for (i = 0; i < wcd938x_regmap_irq_chip.num_regs; i++)
  2625. regmap_write(wcd938x->regmap,
  2626. (WCD938X_DIGITAL_INTR_LEVEL_0 + i), 0);
  2627. wcd938x_regmap_irq_chip.irq_drv_data = wcd938x;
  2628. wcd938x->irq_info.wcd_regmap_irq_chip = &wcd938x_regmap_irq_chip;
  2629. wcd938x->irq_info.codec_name = "WCD938X";
  2630. wcd938x->irq_info.regmap = wcd938x->regmap;
  2631. wcd938x->irq_info.dev = dev;
  2632. ret = wcd_irq_init(&wcd938x->irq_info, &wcd938x->virq);
  2633. if (ret) {
  2634. dev_err(wcd938x->dev, "%s: IRQ init failed: %d\n",
  2635. __func__, ret);
  2636. goto err;
  2637. }
  2638. wcd938x->tx_swr_dev->slave_irq = wcd938x->virq;
  2639. /* Request for watchdog interrupt */
  2640. wcd_request_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHR_PDM_WD_INT,
  2641. "HPHR PDM WD INT", wcd938x_wd_handle_irq, NULL);
  2642. wcd_request_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHL_PDM_WD_INT,
  2643. "HPHL PDM WD INT", wcd938x_wd_handle_irq, NULL);
  2644. wcd_request_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT,
  2645. "AUX PDM WD INT", wcd938x_wd_handle_irq, NULL);
  2646. /* Disable watchdog interrupt for HPH and AUX */
  2647. wcd_disable_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHR_PDM_WD_INT);
  2648. wcd_disable_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHL_PDM_WD_INT);
  2649. wcd_disable_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT);
  2650. ret = snd_soc_register_component(dev, &soc_codec_dev_wcd938x,
  2651. NULL, 0);
  2652. if (ret) {
  2653. dev_err(dev, "%s: Codec registration failed\n",
  2654. __func__);
  2655. goto err_irq;
  2656. }
  2657. return ret;
  2658. err_irq:
  2659. wcd_irq_exit(&wcd938x->irq_info, wcd938x->virq);
  2660. err:
  2661. component_unbind_all(dev, wcd938x);
  2662. return ret;
  2663. }
  2664. static void wcd938x_unbind(struct device *dev)
  2665. {
  2666. struct wcd938x_priv *wcd938x = dev_get_drvdata(dev);
  2667. wcd_free_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHR_PDM_WD_INT, NULL);
  2668. wcd_free_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHL_PDM_WD_INT, NULL);
  2669. wcd_free_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT, NULL);
  2670. wcd_irq_exit(&wcd938x->irq_info, wcd938x->virq);
  2671. snd_soc_unregister_component(dev);
  2672. component_unbind_all(dev, wcd938x);
  2673. }
  2674. static const struct of_device_id wcd938x_dt_match[] = {
  2675. { .compatible = "qcom,wcd938x-codec" },
  2676. {}
  2677. };
  2678. static const struct component_master_ops wcd938x_comp_ops = {
  2679. .bind = wcd938x_bind,
  2680. .unbind = wcd938x_unbind,
  2681. };
  2682. static int wcd938x_compare_of(struct device *dev, void *data)
  2683. {
  2684. return dev->of_node == data;
  2685. }
  2686. static void wcd938x_release_of(struct device *dev, void *data)
  2687. {
  2688. of_node_put(data);
  2689. }
  2690. static int wcd938x_add_slave_components(struct device *dev,
  2691. struct component_match **matchptr)
  2692. {
  2693. struct device_node *np, *rx_node, *tx_node;
  2694. np = dev->of_node;
  2695. rx_node = of_parse_phandle(np, "qcom,rx-slave", 0);
  2696. if (!rx_node) {
  2697. dev_err(dev, "%s: Rx-slave node not defined\n", __func__);
  2698. return -ENODEV;
  2699. }
  2700. of_node_get(rx_node);
  2701. component_match_add_release(dev, matchptr,
  2702. wcd938x_release_of,
  2703. wcd938x_compare_of,
  2704. rx_node);
  2705. tx_node = of_parse_phandle(np, "qcom,tx-slave", 0);
  2706. if (!tx_node) {
  2707. dev_err(dev, "%s: Tx-slave node not defined\n", __func__);
  2708. return -ENODEV;
  2709. }
  2710. of_node_get(tx_node);
  2711. component_match_add_release(dev, matchptr,
  2712. wcd938x_release_of,
  2713. wcd938x_compare_of,
  2714. tx_node);
  2715. return 0;
  2716. }
  2717. static int wcd938x_wakeup(void *handle, bool enable)
  2718. {
  2719. struct wcd938x_priv *priv;
  2720. if (!handle) {
  2721. pr_err("%s: NULL handle\n", __func__);
  2722. return -EINVAL;
  2723. }
  2724. priv = (struct wcd938x_priv *)handle;
  2725. if (!priv->tx_swr_dev) {
  2726. pr_err("%s: tx swr dev is NULL\n", __func__);
  2727. return -EINVAL;
  2728. }
  2729. if (enable)
  2730. return swr_device_wakeup_vote(priv->tx_swr_dev);
  2731. else
  2732. return swr_device_wakeup_unvote(priv->tx_swr_dev);
  2733. }
  2734. static int wcd938x_probe(struct platform_device *pdev)
  2735. {
  2736. struct component_match *match = NULL;
  2737. struct wcd938x_priv *wcd938x = NULL;
  2738. struct wcd938x_pdata *pdata = NULL;
  2739. struct wcd_ctrl_platform_data *plat_data = NULL;
  2740. struct device *dev = &pdev->dev;
  2741. int ret;
  2742. wcd938x = devm_kzalloc(dev, sizeof(struct wcd938x_priv),
  2743. GFP_KERNEL);
  2744. if (!wcd938x)
  2745. return -ENOMEM;
  2746. dev_set_drvdata(dev, wcd938x);
  2747. wcd938x->dev = dev;
  2748. pdata = wcd938x_populate_dt_data(dev);
  2749. if (!pdata) {
  2750. dev_err(dev, "%s: Fail to obtain platform data\n", __func__);
  2751. return -EINVAL;
  2752. }
  2753. dev->platform_data = pdata;
  2754. wcd938x->rst_np = pdata->rst_np;
  2755. ret = msm_cdc_init_supplies(dev, &wcd938x->supplies,
  2756. pdata->regulator, pdata->num_supplies);
  2757. if (!wcd938x->supplies) {
  2758. dev_err(dev, "%s: Cannot init wcd supplies\n",
  2759. __func__);
  2760. return ret;
  2761. }
  2762. plat_data = dev_get_platdata(dev->parent);
  2763. if (!plat_data) {
  2764. dev_err(dev, "%s: platform data from parent is NULL\n",
  2765. __func__);
  2766. return -EINVAL;
  2767. }
  2768. wcd938x->handle = (void *)plat_data->handle;
  2769. if (!wcd938x->handle) {
  2770. dev_err(dev, "%s: handle is NULL\n", __func__);
  2771. return -EINVAL;
  2772. }
  2773. wcd938x->update_wcd_event = plat_data->update_wcd_event;
  2774. if (!wcd938x->update_wcd_event) {
  2775. dev_err(dev, "%s: update_wcd_event api is null!\n",
  2776. __func__);
  2777. return -EINVAL;
  2778. }
  2779. wcd938x->register_notifier = plat_data->register_notifier;
  2780. if (!wcd938x->register_notifier) {
  2781. dev_err(dev, "%s: register_notifier api is null!\n",
  2782. __func__);
  2783. return -EINVAL;
  2784. }
  2785. ret = msm_cdc_enable_static_supplies(&pdev->dev, wcd938x->supplies,
  2786. pdata->regulator,
  2787. pdata->num_supplies);
  2788. if (ret) {
  2789. dev_err(dev, "%s: wcd static supply enable failed!\n",
  2790. __func__);
  2791. return ret;
  2792. }
  2793. ret = wcd938x_parse_port_mapping(dev, "qcom,rx_swr_ch_map",
  2794. CODEC_RX);
  2795. ret |= wcd938x_parse_port_mapping(dev, "qcom,tx_swr_ch_map",
  2796. CODEC_TX);
  2797. if (ret) {
  2798. dev_err(dev, "Failed to read port mapping\n");
  2799. goto err;
  2800. }
  2801. mutex_init(&wcd938x->micb_lock);
  2802. ret = wcd938x_add_slave_components(dev, &match);
  2803. if (ret)
  2804. goto err_lock_init;
  2805. wcd938x_reset(dev);
  2806. wcd938x->wakeup = wcd938x_wakeup;
  2807. return component_master_add_with_match(dev,
  2808. &wcd938x_comp_ops, match);
  2809. err_lock_init:
  2810. mutex_destroy(&wcd938x->micb_lock);
  2811. err:
  2812. return ret;
  2813. }
  2814. static int wcd938x_remove(struct platform_device *pdev)
  2815. {
  2816. struct wcd938x_priv *wcd938x = NULL;
  2817. wcd938x = platform_get_drvdata(pdev);
  2818. component_master_del(&pdev->dev, &wcd938x_comp_ops);
  2819. mutex_destroy(&wcd938x->micb_lock);
  2820. dev_set_drvdata(&pdev->dev, NULL);
  2821. return 0;
  2822. }
  2823. #ifdef CONFIG_PM_SLEEP
  2824. static int wcd938x_suspend(struct device *dev)
  2825. {
  2826. return 0;
  2827. }
  2828. static int wcd938x_resume(struct device *dev)
  2829. {
  2830. return 0;
  2831. }
  2832. static const struct dev_pm_ops wcd938x_dev_pm_ops = {
  2833. SET_SYSTEM_SLEEP_PM_OPS(
  2834. wcd938x_suspend,
  2835. wcd938x_resume
  2836. )
  2837. };
  2838. #endif
  2839. static struct platform_driver wcd938x_codec_driver = {
  2840. .probe = wcd938x_probe,
  2841. .remove = wcd938x_remove,
  2842. .driver = {
  2843. .name = "wcd938x_codec",
  2844. .owner = THIS_MODULE,
  2845. .of_match_table = of_match_ptr(wcd938x_dt_match),
  2846. #ifdef CONFIG_PM_SLEEP
  2847. .pm = &wcd938x_dev_pm_ops,
  2848. #endif
  2849. .suppress_bind_attrs = true,
  2850. },
  2851. };
  2852. module_platform_driver(wcd938x_codec_driver);
  2853. MODULE_DESCRIPTION("WCD938X Codec driver");
  2854. MODULE_LICENSE("GPL v2");