wcd938x.c 87 KB

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