wcd937x.c 91 KB

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