wsa884x.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/device.h>
  11. #include <linux/printk.h>
  12. #include <linux/bitops.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/delay.h>
  16. #include <linux/kernel.h>
  17. #include <linux/gpio.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/regmap.h>
  21. #include <linux/debugfs.h>
  22. #include <soc/soundwire.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dapm.h>
  27. #include <sound/tlv.h>
  28. #include <asoc/msm-cdc-pinctrl.h>
  29. #include <asoc/msm-cdc-supply.h>
  30. #include "wsa884x-registers.h"
  31. #include "wsa884x.h"
  32. #include "internal.h"
  33. #include "asoc/bolero-slave-internal.h"
  34. #include <linux/qti-regmap-debugfs.h>
  35. #define T1_TEMP -10
  36. #define T2_TEMP 150
  37. #define LOW_TEMP_THRESHOLD 5
  38. #define HIGH_TEMP_THRESHOLD 45
  39. #define TEMP_INVALID 0xFFFF
  40. #define WSA884X_TEMP_RETRY 3
  41. #define WSA884X_IRQ_RETRY 2
  42. #define PBR_MAX_VOLTAGE 20
  43. #define PBR_MAX_CODE 255
  44. #define WSA884X_IDLE_DETECT_NG_BLOCK_MASK 0x38
  45. #define MAX_NAME_LEN 40
  46. #define WSA884X_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  47. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |\
  48. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000 |\
  49. SNDRV_PCM_RATE_384000)
  50. /* Fractional Rates */
  51. #define WSA884X_FRAC_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |\
  52. SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_352800)
  53. #define WSA884X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  54. SNDRV_PCM_FMTBIT_S24_LE |\
  55. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  56. #define REG_FIELD_VALUE(register_name, field_name, value) \
  57. WSA884X_##register_name, FIELD_MASK(register_name, field_name), \
  58. value << FIELD_SHIFT(register_name, field_name)
  59. enum {
  60. IDLE_DETECT,
  61. NG1,
  62. NG2,
  63. NG3,
  64. };
  65. struct wsa_temp_register {
  66. u8 d1_msb;
  67. u8 d1_lsb;
  68. u8 d2_msb;
  69. u8 d2_lsb;
  70. u8 dmeas_msb;
  71. u8 dmeas_lsb;
  72. };
  73. enum {
  74. COMP_OFFSET0,
  75. COMP_OFFSET1,
  76. COMP_OFFSET2,
  77. COMP_OFFSET3,
  78. COMP_OFFSET4,
  79. };
  80. #define WSA884X_VTH_TO_REG(vth) \
  81. ((vth) != 0 ? (((vth) - 150) * PBR_MAX_CODE / (PBR_MAX_VOLTAGE * 100) + 1) : 0)
  82. struct wsa_reg_mask_val {
  83. u16 reg;
  84. u8 mask;
  85. u8 val;
  86. };
  87. static const struct wsa_reg_mask_val reg_init[] = {
  88. {REG_FIELD_VALUE(CKWD_CTL_1, VPP_SW_CTL, 0x00)},
  89. {REG_FIELD_VALUE(CDC_SPK_DSM_A2_0, COEF_A2, 0x0A)},
  90. {REG_FIELD_VALUE(CDC_SPK_DSM_A2_1, COEF_A2, 0x08)},
  91. {REG_FIELD_VALUE(CDC_SPK_DSM_A3_0, COEF_A3, 0xF3)},
  92. {REG_FIELD_VALUE(CDC_SPK_DSM_A3_1, COEF_A3, 0x07)},
  93. {REG_FIELD_VALUE(CDC_SPK_DSM_A4_0, COEF_A4, 0x79)},
  94. {REG_FIELD_VALUE(CDC_SPK_DSM_A5_0, COEF_A5, 0x0B)},
  95. {REG_FIELD_VALUE(CDC_SPK_DSM_A6_0, COEF_A6, 0x8A)},
  96. {REG_FIELD_VALUE(CDC_SPK_DSM_A7_0, COEF_A7, 0x9B)},
  97. {REG_FIELD_VALUE(CDC_SPK_DSM_C_0, COEF_C3, 0x06)},
  98. {REG_FIELD_VALUE(CDC_SPK_DSM_C_0, COEF_C2, 0x08)},
  99. {REG_FIELD_VALUE(CDC_SPK_DSM_C_2, COEF_C7, 0x0F)},
  100. {REG_FIELD_VALUE(CDC_SPK_DSM_C_3, COEF_C7, 0x20)},
  101. {REG_FIELD_VALUE(CDC_SPK_DSM_R1, SAT_LIMIT_R1, 0x83)},
  102. {REG_FIELD_VALUE(CDC_SPK_DSM_R2, SAT_LIMIT_R2, 0x7F)},
  103. {REG_FIELD_VALUE(CDC_SPK_DSM_R3, SAT_LIMIT_R3, 0x9D)},
  104. {REG_FIELD_VALUE(CDC_SPK_DSM_R4, SAT_LIMIT_R4, 0x82)},
  105. {REG_FIELD_VALUE(CDC_SPK_DSM_R5, SAT_LIMIT_R5, 0x8B)},
  106. {REG_FIELD_VALUE(CDC_SPK_DSM_R6, SAT_LIMIT_R6, 0x9B)},
  107. {REG_FIELD_VALUE(CDC_SPK_DSM_R7, SAT_LIMIT_R7, 0x3F)},
  108. {REG_FIELD_VALUE(BOP_DEGLITCH_CTL, BOP_DEGLITCH_SETTING, 0x08)},
  109. {REG_FIELD_VALUE(VBAT_THRM_FLT_CTL, VBAT_COEF_SEL, 0x04)},
  110. {REG_FIELD_VALUE(CLSH_CTL_0, DLY_CODE, 0x06)},
  111. {REG_FIELD_VALUE(CLSH_SOFT_MAX, SOFT_MAX, 0xFF)},
  112. {REG_FIELD_VALUE(OTP_REG_38, BOOST_ILIM_TUNE, 0x00)},
  113. {REG_FIELD_VALUE(OTP_REG_40, ISENSE_RESCAL, 0x08)},
  114. {REG_FIELD_VALUE(STB_CTRL1, SLOPE_COMP_CURRENT, 0x0D)},
  115. {REG_FIELD_VALUE(ILIM_CTRL1, ILIM_OFFSET_PB, 0x03)},
  116. {REG_FIELD_VALUE(CURRENT_LIMIT, CURRENT_LIMIT, 0x09)},
  117. {REG_FIELD_VALUE(CKWD_CTL_1, CKWD_VCOMP_VREF_SEL, 0x13)},
  118. {REG_FIELD_VALUE(BOP2_PROG, BOP2_VTH, 0x06)},
  119. {REG_FIELD_VALUE(BOP2_PROG, BOP2_HYST, 0x06)},
  120. {REG_FIELD_VALUE(VBAT_CAL_CTL, RESERVE, 0x02)},
  121. {REG_FIELD_VALUE(REF_CTRL, BG_RDY_SEL, 0x01)},
  122. {REG_FIELD_VALUE(ZX_CTRL1, ZX_DET_SW_SEL, 0x03)},
  123. };
  124. static int wsa884x_handle_post_irq(void *data);
  125. static int wsa884x_get_temperature(struct snd_soc_component *component,
  126. int *temp);
  127. enum {
  128. WSA8840 = 0,
  129. WSA8845 = 5,
  130. WSA8845H = 0xC,
  131. };
  132. enum {
  133. SPKR_STATUS = 0,
  134. WSA_SUPPLIES_LPM_MODE,
  135. SPKR_ADIE_LB,
  136. };
  137. enum {
  138. WSA884X_IRQ_INT_SAF2WAR = 0,
  139. WSA884X_IRQ_INT_WAR2SAF,
  140. WSA884X_IRQ_INT_DISABLE,
  141. WSA884X_IRQ_INT_OCP,
  142. WSA884X_IRQ_INT_CLIP,
  143. WSA884X_IRQ_INT_PDM_WD,
  144. WSA884X_IRQ_INT_CLK_WD,
  145. WSA884X_IRQ_INT_INTR_PIN,
  146. WSA884X_IRQ_INT_UVLO,
  147. WSA884X_IRQ_INT_PA_ON_ERR,
  148. WSA884X_NUM_IRQS,
  149. };
  150. static const struct regmap_irq wsa884x_irqs[WSA884X_NUM_IRQS] = {
  151. REGMAP_IRQ_REG(WSA884X_IRQ_INT_SAF2WAR, 0, 0x01),
  152. REGMAP_IRQ_REG(WSA884X_IRQ_INT_WAR2SAF, 0, 0x02),
  153. REGMAP_IRQ_REG(WSA884X_IRQ_INT_DISABLE, 0, 0x04),
  154. REGMAP_IRQ_REG(WSA884X_IRQ_INT_OCP, 0, 0x08),
  155. REGMAP_IRQ_REG(WSA884X_IRQ_INT_CLIP, 0, 0x10),
  156. REGMAP_IRQ_REG(WSA884X_IRQ_INT_PDM_WD, 0, 0x20),
  157. REGMAP_IRQ_REG(WSA884X_IRQ_INT_CLK_WD, 0, 0x40),
  158. REGMAP_IRQ_REG(WSA884X_IRQ_INT_INTR_PIN, 0, 0x80),
  159. REGMAP_IRQ_REG(WSA884X_IRQ_INT_UVLO, 1, 0x01),
  160. REGMAP_IRQ_REG(WSA884X_IRQ_INT_PA_ON_ERR, 1, 0x02),
  161. };
  162. static struct regmap_irq_chip wsa884x_regmap_irq_chip = {
  163. .name = "wsa884x",
  164. .irqs = wsa884x_irqs,
  165. .num_irqs = ARRAY_SIZE(wsa884x_irqs),
  166. .num_regs = 2,
  167. .status_base = WSA884X_INTR_STATUS0,
  168. .mask_base = WSA884X_INTR_MASK0,
  169. .type_base = WSA884X_INTR_LEVEL0,
  170. .ack_base = WSA884X_INTR_CLEAR0,
  171. .use_ack = 1,
  172. .runtime_pm = false,
  173. .handle_post_irq = wsa884x_handle_post_irq,
  174. .irq_drv_data = NULL,
  175. };
  176. static int wsa884x_handle_post_irq(void *data)
  177. {
  178. struct wsa884x_priv *wsa884x = data;
  179. u32 sts1 = 0, sts2 = 0;
  180. int retry = WSA884X_IRQ_RETRY;
  181. struct snd_soc_component *component = NULL;
  182. if (!wsa884x)
  183. return IRQ_NONE;
  184. component = wsa884x->component;
  185. if (!wsa884x->pa_mute) {
  186. do {
  187. wsa884x->pa_mute = 0;
  188. snd_soc_component_update_bits(component,
  189. REG_FIELD_VALUE(PA_FSM_EN, GLOBAL_PA_EN, 0x01));
  190. usleep_range(1000, 1100);
  191. regmap_read(wsa884x->regmap, WSA884X_INTR_STATUS0, &sts1);
  192. regmap_read(wsa884x->regmap, WSA884X_INTR_STATUS1, &sts2);
  193. wsa884x->swr_slave->slave_irq_pending =
  194. ((sts1 || sts2) ? true : false);
  195. pr_debug("%s: IRQs Sts0: %x, Sts1: %x\n", __func__,
  196. sts1, sts2);
  197. if (wsa884x->swr_slave->slave_irq_pending) {
  198. pr_debug("%s: IRQ retries left: %0d\n",
  199. __func__, retry);
  200. snd_soc_component_update_bits(component,
  201. REG_FIELD_VALUE(PA_FSM_EN, GLOBAL_PA_EN, 0x00));
  202. wsa884x->pa_mute = 1;
  203. if (retry--)
  204. usleep_range(1000, 1100);
  205. } else {
  206. break;
  207. }
  208. } while (retry);
  209. }
  210. return IRQ_HANDLED;
  211. }
  212. #ifdef CONFIG_DEBUG_FS
  213. static int codec_debug_open(struct inode *inode, struct file *file)
  214. {
  215. file->private_data = inode->i_private;
  216. return 0;
  217. }
  218. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  219. {
  220. char *token;
  221. int base, cnt;
  222. token = strsep(&buf, " ");
  223. for (cnt = 0; cnt < num_of_par; cnt++) {
  224. if (token) {
  225. if ((token[1] == 'x') || (token[1] == 'X'))
  226. base = 16;
  227. else
  228. base = 10;
  229. if (kstrtou32(token, base, &param1[cnt]) != 0)
  230. return -EINVAL;
  231. token = strsep(&buf, " ");
  232. } else {
  233. return -EINVAL;
  234. }
  235. }
  236. return 0;
  237. }
  238. static bool is_swr_slave_reg_readable(int reg)
  239. {
  240. int ret = true;
  241. if (((reg > 0x46) && (reg < 0x4A)) ||
  242. ((reg > 0x4A) && (reg < 0x50)) ||
  243. ((reg > 0x55) && (reg < 0x60)) ||
  244. ((reg > 0x60) && (reg < 0x70)) ||
  245. ((reg > 0x70) && (reg < 0xC0)) ||
  246. ((reg > 0xC1) && (reg < 0xC8)) ||
  247. ((reg > 0xC8) && (reg < 0xD0)) ||
  248. ((reg > 0xD0) && (reg < 0xE0)) ||
  249. ((reg > 0xE0) && (reg < 0xF0)) ||
  250. ((reg > 0xF0) && (reg < 0x100)) ||
  251. ((reg > 0x105) && (reg < 0x120)) ||
  252. ((reg > 0x205) && (reg < 0x220)) ||
  253. ((reg > 0x305) && (reg < 0x320)) ||
  254. ((reg > 0x405) && (reg < 0x420)) ||
  255. ((reg > 0x505) && (reg < 0x520)) ||
  256. ((reg > 0x605) && (reg < 0x620)) ||
  257. ((reg > 0x127) && (reg < 0x130)) ||
  258. ((reg > 0x227) && (reg < 0x230)) ||
  259. ((reg > 0x327) && (reg < 0x330)) ||
  260. ((reg > 0x427) && (reg < 0x430)) ||
  261. ((reg > 0x527) && (reg < 0x530)) ||
  262. ((reg > 0x627) && (reg < 0x630)) ||
  263. ((reg > 0x137) && (reg < 0x200)) ||
  264. ((reg > 0x237) && (reg < 0x300)) ||
  265. ((reg > 0x337) && (reg < 0x400)) ||
  266. ((reg > 0x437) && (reg < 0x500)) ||
  267. ((reg > 0x537) && (reg < 0x600)) ||
  268. ((reg > 0x637) && (reg < 0xF00)) ||
  269. ((reg > 0xF05) && (reg < 0xF20)) ||
  270. ((reg > 0xF25) && (reg < 0xF30)) ||
  271. ((reg > 0xF35) && (reg < 0x2000)))
  272. ret = false;
  273. return ret;
  274. }
  275. static ssize_t swr_slave_reg_show(struct swr_device *pdev, char __user *ubuf,
  276. size_t count, loff_t *ppos)
  277. {
  278. int i, reg_val, len;
  279. ssize_t total = 0;
  280. char tmp_buf[SWR_SLV_MAX_BUF_LEN];
  281. if (!ubuf || !ppos)
  282. return 0;
  283. for (i = (((int) *ppos/BYTES_PER_LINE) + SWR_SLV_START_REG_ADDR);
  284. i <= SWR_SLV_MAX_REG_ADDR; i++) {
  285. if (!is_swr_slave_reg_readable(i))
  286. continue;
  287. swr_read(pdev, pdev->dev_num, i, &reg_val, 1);
  288. len = snprintf(tmp_buf, sizeof(tmp_buf), "0x%.3x: 0x%.2x\n", i,
  289. (reg_val & 0xFF));
  290. if (len < 0) {
  291. pr_err_ratelimited("%s: fail to fill the buffer\n", __func__);
  292. total = -EFAULT;
  293. goto copy_err;
  294. }
  295. if ((total + len) >= count - 1)
  296. break;
  297. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  298. pr_err_ratelimited("%s: fail to copy reg dump\n", __func__);
  299. total = -EFAULT;
  300. goto copy_err;
  301. }
  302. total += len;
  303. *ppos += len;
  304. }
  305. copy_err:
  306. *ppos = SWR_SLV_MAX_REG_ADDR * BYTES_PER_LINE;
  307. return total;
  308. }
  309. static ssize_t codec_debug_dump(struct file *file, char __user *ubuf,
  310. size_t count, loff_t *ppos)
  311. {
  312. struct swr_device *pdev;
  313. if (!count || !file || !ppos || !ubuf)
  314. return -EINVAL;
  315. pdev = file->private_data;
  316. if (!pdev)
  317. return -EINVAL;
  318. if (*ppos < 0)
  319. return -EINVAL;
  320. return swr_slave_reg_show(pdev, ubuf, count, ppos);
  321. }
  322. static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
  323. size_t count, loff_t *ppos)
  324. {
  325. char lbuf[SWR_SLV_RD_BUF_LEN];
  326. struct swr_device *pdev = NULL;
  327. struct wsa884x_priv *wsa884x = NULL;
  328. if (!count || !file || !ppos || !ubuf)
  329. return -EINVAL;
  330. pdev = file->private_data;
  331. if (!pdev)
  332. return -EINVAL;
  333. wsa884x = swr_get_dev_data(pdev);
  334. if (!wsa884x)
  335. return -EINVAL;
  336. if (*ppos < 0)
  337. return -EINVAL;
  338. snprintf(lbuf, sizeof(lbuf), "0x%x\n",
  339. (wsa884x->read_data & 0xFF));
  340. return simple_read_from_buffer(ubuf, count, ppos, lbuf,
  341. strnlen(lbuf, 7));
  342. }
  343. static ssize_t codec_debug_peek_write(struct file *file,
  344. const char __user *ubuf, size_t cnt, loff_t *ppos)
  345. {
  346. char lbuf[SWR_SLV_WR_BUF_LEN];
  347. int rc = 0;
  348. u32 param[5];
  349. struct swr_device *pdev = NULL;
  350. struct wsa884x_priv *wsa884x = NULL;
  351. if (!cnt || !file || !ppos || !ubuf)
  352. return -EINVAL;
  353. pdev = file->private_data;
  354. if (!pdev)
  355. return -EINVAL;
  356. wsa884x = swr_get_dev_data(pdev);
  357. if (!wsa884x)
  358. return -EINVAL;
  359. if (*ppos < 0)
  360. return -EINVAL;
  361. if (cnt > sizeof(lbuf) - 1)
  362. return -EINVAL;
  363. rc = copy_from_user(lbuf, ubuf, cnt);
  364. if (rc)
  365. return -EFAULT;
  366. lbuf[cnt] = '\0';
  367. rc = get_parameters(lbuf, param, 1);
  368. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) && (rc == 0)))
  369. return -EINVAL;
  370. swr_read(pdev, pdev->dev_num, param[0], &wsa884x->read_data, 1);
  371. if (rc == 0)
  372. rc = cnt;
  373. else
  374. pr_err_ratelimited("%s: rc = %d\n", __func__, rc);
  375. return rc;
  376. }
  377. static ssize_t codec_debug_write(struct file *file,
  378. const char __user *ubuf, size_t cnt, loff_t *ppos)
  379. {
  380. char lbuf[SWR_SLV_WR_BUF_LEN];
  381. int rc = 0;
  382. u32 param[5];
  383. struct swr_device *pdev;
  384. if (!file || !ppos || !ubuf)
  385. return -EINVAL;
  386. pdev = file->private_data;
  387. if (!pdev)
  388. return -EINVAL;
  389. if (cnt > sizeof(lbuf) - 1)
  390. return -EINVAL;
  391. rc = copy_from_user(lbuf, ubuf, cnt);
  392. if (rc)
  393. return -EFAULT;
  394. lbuf[cnt] = '\0';
  395. rc = get_parameters(lbuf, param, 2);
  396. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) &&
  397. (param[1] <= 0xFF) && (rc == 0)))
  398. return -EINVAL;
  399. swr_write(pdev, pdev->dev_num, param[0], &param[1]);
  400. if (rc == 0)
  401. rc = cnt;
  402. else
  403. pr_err_ratelimited("%s: rc = %d\n", __func__, rc);
  404. return rc;
  405. }
  406. static const struct file_operations codec_debug_write_ops = {
  407. .open = codec_debug_open,
  408. .write = codec_debug_write,
  409. };
  410. static const struct file_operations codec_debug_read_ops = {
  411. .open = codec_debug_open,
  412. .read = codec_debug_read,
  413. .write = codec_debug_peek_write,
  414. };
  415. static const struct file_operations codec_debug_dump_ops = {
  416. .open = codec_debug_open,
  417. .read = codec_debug_dump,
  418. };
  419. #endif
  420. static void wsa884x_regcache_sync(struct wsa884x_priv *wsa884x)
  421. {
  422. mutex_lock(&wsa884x->res_lock);
  423. regcache_mark_dirty(wsa884x->regmap);
  424. regcache_sync(wsa884x->regmap);
  425. mutex_unlock(&wsa884x->res_lock);
  426. }
  427. static irqreturn_t wsa884x_saf2war_handle_irq(int irq, void *data)
  428. {
  429. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  430. __func__, irq);
  431. return IRQ_HANDLED;
  432. }
  433. static irqreturn_t wsa884x_war2saf_handle_irq(int irq, void *data)
  434. {
  435. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  436. __func__, irq);
  437. return IRQ_HANDLED;
  438. }
  439. static irqreturn_t wsa884x_otp_handle_irq(int irq, void *data)
  440. {
  441. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  442. __func__, irq);
  443. return IRQ_HANDLED;
  444. }
  445. static irqreturn_t wsa884x_ocp_handle_irq(int irq, void *data)
  446. {
  447. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  448. __func__, irq);
  449. return IRQ_HANDLED;
  450. }
  451. static irqreturn_t wsa884x_clip_handle_irq(int irq, void *data)
  452. {
  453. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  454. __func__, irq);
  455. return IRQ_HANDLED;
  456. }
  457. static irqreturn_t wsa884x_pdm_wd_handle_irq(int irq, void *data)
  458. {
  459. struct wsa884x_priv *wsa884x = data;
  460. struct snd_soc_component *component = NULL;
  461. if (!wsa884x)
  462. return IRQ_NONE;
  463. component = wsa884x->component;
  464. snd_soc_component_update_bits(component,
  465. REG_FIELD_VALUE(PDM_WD_CTL, PDM_WD_EN, 0x00));
  466. snd_soc_component_update_bits(component,
  467. REG_FIELD_VALUE(PDM_WD_CTL, PDM_WD_EN, 0x01));
  468. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  469. __func__, irq);
  470. return IRQ_HANDLED;
  471. }
  472. static irqreturn_t wsa884x_clk_wd_handle_irq(int irq, void *data)
  473. {
  474. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  475. __func__, irq);
  476. return IRQ_HANDLED;
  477. }
  478. static irqreturn_t wsa884x_ext_int_handle_irq(int irq, void *data)
  479. {
  480. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  481. __func__, irq);
  482. return IRQ_HANDLED;
  483. }
  484. static irqreturn_t wsa884x_uvlo_handle_irq(int irq, void *data)
  485. {
  486. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  487. __func__, irq);
  488. return IRQ_HANDLED;
  489. }
  490. static irqreturn_t wsa884x_pa_on_err_handle_irq(int irq, void *data)
  491. {
  492. u8 pa_fsm_sta = 0, pa_fsm_err = 0;
  493. struct wsa884x_priv *wsa884x = data;
  494. struct snd_soc_component *component = NULL;
  495. if (!wsa884x)
  496. return IRQ_NONE;
  497. component = wsa884x->component;
  498. if (!component)
  499. return IRQ_NONE;
  500. snd_soc_component_update_bits(component,
  501. REG_FIELD_VALUE(PA_FSM_EN, GLOBAL_PA_EN, 0x00));
  502. pa_fsm_sta = (snd_soc_component_read(component, WSA884X_PA_FSM_STA1)
  503. & 0x1F);
  504. if (pa_fsm_sta)
  505. pa_fsm_err = snd_soc_component_read(component,
  506. WSA884X_PA_FSM_ERR_COND0);
  507. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  508. __func__, irq);
  509. snd_soc_component_update_bits(component, WSA884X_PA_FSM_CTL0,
  510. 0x10, 0x00);
  511. snd_soc_component_update_bits(component, WSA884X_PA_FSM_CTL0,
  512. 0x10, 0x10);
  513. snd_soc_component_update_bits(component, WSA884X_PA_FSM_CTL0,
  514. 0x10, 0x00);
  515. return IRQ_HANDLED;
  516. }
  517. static int wsa884x_set_gain_parameters(struct snd_soc_component *component)
  518. {
  519. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  520. u8 igain;
  521. u8 vgain;
  522. switch (wsa884x->bat_cfg) {
  523. case CONFIG_1S:
  524. case EXT_1S:
  525. switch (wsa884x->system_gain) {
  526. case G_21_DB:
  527. wsa884x->comp_offset = COMP_OFFSET0;
  528. wsa884x->min_gain = G_0_DB;
  529. wsa884x->pa_aux_gain = PA_AUX_0_DB;
  530. break;
  531. case G_19P5_DB:
  532. wsa884x->comp_offset = COMP_OFFSET1;
  533. wsa884x->min_gain = G_M1P5_DB;
  534. wsa884x->pa_aux_gain = PA_AUX_M1P5_DB;
  535. break;
  536. case G_18_DB:
  537. wsa884x->comp_offset = COMP_OFFSET2;
  538. wsa884x->min_gain = G_M3_DB;
  539. wsa884x->pa_aux_gain = PA_AUX_M3_DB;
  540. break;
  541. case G_16P5_DB:
  542. wsa884x->comp_offset = COMP_OFFSET3;
  543. wsa884x->min_gain = G_M4P5_DB;
  544. wsa884x->pa_aux_gain = PA_AUX_M4P5_DB;
  545. break;
  546. default:
  547. wsa884x->comp_offset = COMP_OFFSET4;
  548. wsa884x->min_gain = G_M6_DB;
  549. wsa884x->pa_aux_gain = PA_AUX_M6_DB;
  550. break;
  551. }
  552. break;
  553. case CONFIG_3S:
  554. case EXT_3S:
  555. wsa884x->comp_offset = COMP_OFFSET0;
  556. wsa884x->min_gain = G_7P5_DB;
  557. wsa884x->pa_aux_gain = PA_AUX_7P5_DB;
  558. break;
  559. case EXT_ABOVE_3S:
  560. wsa884x->comp_offset = COMP_OFFSET0;
  561. wsa884x->min_gain = G_12_DB;
  562. wsa884x->pa_aux_gain = PA_AUX_12_DB;
  563. break;
  564. default:
  565. wsa884x->comp_offset = COMP_OFFSET0;
  566. wsa884x->min_gain = G_0_DB;
  567. wsa884x->pa_aux_gain = PA_AUX_0_DB;
  568. break;
  569. }
  570. igain = isense_gain_data[wsa884x->system_gain][wsa884x->rload];
  571. vgain = vsense_gain_data[wsa884x->system_gain];
  572. snd_soc_component_update_bits(component,
  573. REG_FIELD_VALUE(ISENSE2, ISENSE_GAIN_CTL, igain));
  574. snd_soc_component_update_bits(component,
  575. REG_FIELD_VALUE(VSENSE1, GAIN_VSENSE_FE, vgain));
  576. snd_soc_component_update_bits(component,
  577. REG_FIELD_VALUE(GAIN_RAMPING_MIN, MIN_GAIN, wsa884x->min_gain));
  578. if (wsa884x->comp_enable) {
  579. snd_soc_component_update_bits(component,
  580. REG_FIELD_VALUE(DRE_CTL_0, OFFSET,
  581. wsa884x->comp_offset));
  582. snd_soc_component_update_bits(component,
  583. REG_FIELD_VALUE(DRE_CTL_1, CSR_GAIN_EN, 0x00));
  584. } else {
  585. wsa884x->pa_aux_gain = pa_aux_no_comp[wsa884x->pa_gain];
  586. snd_soc_component_update_bits(component,
  587. REG_FIELD_VALUE(DRE_CTL_1, CSR_GAIN_EN, 0x01));
  588. snd_soc_component_update_bits(component,
  589. REG_FIELD_VALUE(DRE_CTL_1, CSR_GAIN, wsa884x->pa_gain));
  590. }
  591. return 0;
  592. }
  593. static int wsa884x_set_pbr_parameters(struct snd_soc_component *component)
  594. {
  595. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  596. int vth1_reg_val;
  597. int vth2_reg_val;
  598. int vth3_reg_val;
  599. int vth4_reg_val;
  600. int vth5_reg_val;
  601. int vth6_reg_val;
  602. int vth7_reg_val;
  603. int vth8_reg_val;
  604. int vth9_reg_val;
  605. int vth10_reg_val;
  606. int vth11_reg_val;
  607. int vth12_reg_val;
  608. int vth13_reg_val;
  609. int vth14_reg_val;
  610. int vth15_reg_val;
  611. int vth1_val = pbr_vth1_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  612. int vth2_val = pbr_vth2_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  613. int vth3_val = pbr_vth3_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  614. int vth4_val = pbr_vth4_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  615. int vth5_val = pbr_vth5_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  616. int vth6_val = pbr_vth6_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  617. int vth7_val = pbr_vth7_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  618. int vth8_val = pbr_vth8_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  619. int vth9_val = pbr_vth9_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  620. int vth10_val = pbr_vth10_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  621. int vth11_val = pbr_vth11_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  622. int vth12_val = pbr_vth12_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  623. int vth13_val = pbr_vth13_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  624. int vth14_val = pbr_vth14_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  625. int vth15_val = pbr_vth15_data[wsa884x->system_gain][wsa884x->bat_cfg][wsa884x->rload];
  626. vth1_reg_val = WSA884X_VTH_TO_REG(vth1_val);
  627. vth2_reg_val = WSA884X_VTH_TO_REG(vth2_val);
  628. vth3_reg_val = WSA884X_VTH_TO_REG(vth3_val);
  629. vth4_reg_val = WSA884X_VTH_TO_REG(vth4_val);
  630. vth5_reg_val = WSA884X_VTH_TO_REG(vth5_val);
  631. vth6_reg_val = WSA884X_VTH_TO_REG(vth6_val);
  632. vth7_reg_val = WSA884X_VTH_TO_REG(vth7_val);
  633. vth8_reg_val = WSA884X_VTH_TO_REG(vth8_val);
  634. vth9_reg_val = WSA884X_VTH_TO_REG(vth9_val);
  635. vth10_reg_val = WSA884X_VTH_TO_REG(vth10_val);
  636. vth11_reg_val = WSA884X_VTH_TO_REG(vth11_val);
  637. vth12_reg_val = WSA884X_VTH_TO_REG(vth12_val);
  638. vth13_reg_val = WSA884X_VTH_TO_REG(vth13_val);
  639. vth14_reg_val = WSA884X_VTH_TO_REG(vth14_val);
  640. vth15_reg_val = WSA884X_VTH_TO_REG(vth15_val);
  641. snd_soc_component_write(component, WSA884X_CLSH_VTH1, vth1_reg_val);
  642. snd_soc_component_write(component, WSA884X_CLSH_VTH2, vth2_reg_val);
  643. snd_soc_component_write(component, WSA884X_CLSH_VTH3, vth3_reg_val);
  644. snd_soc_component_write(component, WSA884X_CLSH_VTH4, vth4_reg_val);
  645. snd_soc_component_write(component, WSA884X_CLSH_VTH5, vth5_reg_val);
  646. snd_soc_component_write(component, WSA884X_CLSH_VTH6, vth6_reg_val);
  647. snd_soc_component_write(component, WSA884X_CLSH_VTH7, vth7_reg_val);
  648. snd_soc_component_write(component, WSA884X_CLSH_VTH8, vth8_reg_val);
  649. snd_soc_component_write(component, WSA884X_CLSH_VTH9, vth9_reg_val);
  650. snd_soc_component_write(component, WSA884X_CLSH_VTH10, vth10_reg_val);
  651. snd_soc_component_write(component, WSA884X_CLSH_VTH11, vth11_reg_val);
  652. snd_soc_component_write(component, WSA884X_CLSH_VTH12, vth12_reg_val);
  653. snd_soc_component_write(component, WSA884X_CLSH_VTH13, vth13_reg_val);
  654. snd_soc_component_write(component, WSA884X_CLSH_VTH14, vth14_reg_val);
  655. snd_soc_component_write(component, WSA884X_CLSH_VTH15, vth15_reg_val);
  656. return 0;
  657. }
  658. static void wsa_noise_gate_write(struct snd_soc_component *component,
  659. int imode)
  660. {
  661. switch (imode) {
  662. case NG1:
  663. snd_soc_component_update_bits(component, WSA884X_PA_FSM_CTL1,
  664. WSA884X_IDLE_DETECT_NG_BLOCK_MASK, 0x30);
  665. break;
  666. case NG2:
  667. snd_soc_component_update_bits(component, WSA884X_PA_FSM_CTL1,
  668. WSA884X_IDLE_DETECT_NG_BLOCK_MASK, 0x20);
  669. break;
  670. case NG3:
  671. snd_soc_component_update_bits(component, WSA884X_PA_FSM_CTL1,
  672. WSA884X_IDLE_DETECT_NG_BLOCK_MASK, 0x10);
  673. break;
  674. default:
  675. snd_soc_component_update_bits(component, WSA884X_PA_FSM_CTL1,
  676. WSA884X_IDLE_DETECT_NG_BLOCK_MASK, 0x8);
  677. break;
  678. }
  679. }
  680. static int wsa_dev_mode_get(struct snd_kcontrol *kcontrol,
  681. struct snd_ctl_elem_value *ucontrol)
  682. {
  683. struct snd_soc_component *component =
  684. snd_soc_kcontrol_component(kcontrol);
  685. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  686. ucontrol->value.integer.value[0] = wsa884x->dev_mode;
  687. dev_dbg(component->dev, "%s: mode = 0x%x\n", __func__,
  688. wsa884x->dev_mode);
  689. return 0;
  690. }
  691. static int wsa_dev_mode_put(struct snd_kcontrol *kcontrol,
  692. struct snd_ctl_elem_value *ucontrol)
  693. {
  694. struct snd_soc_component *component =
  695. snd_soc_kcontrol_component(kcontrol);
  696. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  697. int dev_mode;
  698. int wsa_dev_index;
  699. dev_mode = ucontrol->value.integer.value[0];
  700. dev_dbg(component->dev, "%s: Dev Mode current: %d, new: %d\n",
  701. __func__, wsa884x->dev_mode, dev_mode);
  702. /* Check if input parameter is in range */
  703. wsa_dev_index = (wsa884x->dev_index - 1) % 2;
  704. if ((dev_mode + wsa_dev_index * 2) < (MAX_DEV_MODE * 2)) {
  705. wsa884x->dev_mode = dev_mode;
  706. wsa884x->system_gain = wsa884x->sys_gains[dev_mode + wsa_dev_index * 2];
  707. } else {
  708. return -EINVAL;
  709. }
  710. return 0;
  711. }
  712. static const char * const wsa_pa_gain_text[] = {
  713. "G_21_DB", "G_19P5_DB" "G_18_DB", "G_16P5_DB", "G_15_DB", "G_13P5_DB",
  714. "G_12_DB", "G_10P5_DB", "G_9_DB", "G_7P5_DB", "G_6_DB", "G_4P5_DB",
  715. "G_3_DB", "G_1P5_DB", "G_0_DB", "G_M1P5_DB", "G_M3_DB", "G_M4P5_DB"
  716. "G_M6_DB", "G_M7P5_DB", "G_M9_DB"
  717. };
  718. static const struct soc_enum wsa_pa_gain_enum =
  719. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_pa_gain_text), wsa_pa_gain_text);
  720. static int wsa_pa_gain_get(struct snd_kcontrol *kcontrol,
  721. struct snd_ctl_elem_value *ucontrol)
  722. {
  723. struct snd_soc_component *component =
  724. snd_soc_kcontrol_component(kcontrol);
  725. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  726. ucontrol->value.integer.value[0] = wsa884x->pa_gain;
  727. dev_dbg(component->dev, "%s: PA gain = 0x%x\n", __func__,
  728. wsa884x->pa_gain);
  729. return 0;
  730. }
  731. static int wsa_pa_gain_put(struct snd_kcontrol *kcontrol,
  732. struct snd_ctl_elem_value *ucontrol)
  733. {
  734. struct snd_soc_component *component =
  735. snd_soc_kcontrol_component(kcontrol);
  736. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  737. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  738. __func__, ucontrol->value.integer.value[0]);
  739. wsa884x->pa_gain = ucontrol->value.integer.value[0];
  740. return 0;
  741. }
  742. static int wsa_get_temp(struct snd_kcontrol *kcontrol,
  743. struct snd_ctl_elem_value *ucontrol)
  744. {
  745. struct snd_soc_component *component =
  746. snd_soc_kcontrol_component(kcontrol);
  747. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  748. int temp = 0;
  749. if (test_bit(SPKR_STATUS, &wsa884x->status_mask))
  750. temp = wsa884x->curr_temp;
  751. else
  752. wsa884x_get_temperature(component, &temp);
  753. ucontrol->value.integer.value[0] = temp;
  754. return 0;
  755. }
  756. static ssize_t wsa884x_codec_version_read(struct snd_info_entry *entry,
  757. void *file_private_data, struct file *file,
  758. char __user *buf, size_t count, loff_t pos)
  759. {
  760. struct wsa884x_priv *wsa884x;
  761. char buffer[WSA884X_VERSION_ENTRY_SIZE];
  762. int len = 0;
  763. wsa884x = (struct wsa884x_priv *) entry->private_data;
  764. if (!wsa884x) {
  765. pr_err_ratelimited("%s: wsa884x priv is null\n", __func__);
  766. return -EINVAL;
  767. }
  768. switch (wsa884x->version) {
  769. case WSA884X_VERSION_1_0:
  770. len = snprintf(buffer, sizeof(buffer), "WSA884X_1_0\n");
  771. break;
  772. default:
  773. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  774. break;
  775. }
  776. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  777. }
  778. static struct snd_info_entry_ops wsa884x_codec_info_ops = {
  779. .read = wsa884x_codec_version_read,
  780. };
  781. static ssize_t wsa884x_variant_read(struct snd_info_entry *entry,
  782. void *file_private_data,
  783. struct file *file,
  784. char __user *buf, size_t count,
  785. loff_t pos)
  786. {
  787. struct wsa884x_priv *wsa884x;
  788. char buffer[WSA884X_VARIANT_ENTRY_SIZE];
  789. int len = 0;
  790. wsa884x = (struct wsa884x_priv *) entry->private_data;
  791. if (!wsa884x) {
  792. pr_err_ratelimited("%s: wsa884x priv is null\n", __func__);
  793. return -EINVAL;
  794. }
  795. switch (wsa884x->variant) {
  796. case WSA8840:
  797. len = snprintf(buffer, sizeof(buffer), "WSA8840\n");
  798. break;
  799. case WSA8845:
  800. len = snprintf(buffer, sizeof(buffer), "WSA8845\n");
  801. break;
  802. case WSA8845H:
  803. len = snprintf(buffer, sizeof(buffer), "WSA8845H\n");
  804. break;
  805. default:
  806. len = snprintf(buffer, sizeof(buffer), "UNDEFINED\n");
  807. break;
  808. }
  809. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  810. }
  811. static struct snd_info_entry_ops wsa884x_variant_ops = {
  812. .read = wsa884x_variant_read,
  813. };
  814. /*
  815. * wsa884x_codec_info_create_codec_entry - creates wsa884x module
  816. * @codec_root: The parent directory
  817. * @component: Codec instance
  818. *
  819. * Creates wsa884x module and version entry under the given
  820. * parent directory.
  821. *
  822. * Return: 0 on success or negative error code on failure.
  823. */
  824. int wsa884x_codec_info_create_codec_entry(struct snd_info_entry *codec_root,
  825. struct snd_soc_component *component)
  826. {
  827. struct snd_info_entry *version_entry;
  828. struct snd_info_entry *variant_entry;
  829. struct wsa884x_priv *wsa884x;
  830. struct snd_soc_card *card;
  831. char name[80];
  832. if (!codec_root || !component)
  833. return -EINVAL;
  834. wsa884x = snd_soc_component_get_drvdata(component);
  835. if (wsa884x->entry) {
  836. dev_dbg(wsa884x->dev,
  837. "%s:wsa884x module already created\n", __func__);
  838. return 0;
  839. }
  840. card = component->card;
  841. snprintf(name, sizeof(name), "%s.%llx", "wsa884x",
  842. wsa884x->swr_slave->addr);
  843. wsa884x->entry = snd_info_create_module_entry(codec_root->module,
  844. (const char *)name,
  845. codec_root);
  846. if (!wsa884x->entry) {
  847. dev_dbg(component->dev, "%s: failed to create wsa884x entry\n",
  848. __func__);
  849. return -ENOMEM;
  850. }
  851. wsa884x->entry->mode = S_IFDIR | 0555;
  852. if (snd_info_register(wsa884x->entry) < 0) {
  853. snd_info_free_entry(wsa884x->entry);
  854. return -ENOMEM;
  855. }
  856. version_entry = snd_info_create_card_entry(card->snd_card,
  857. "version",
  858. wsa884x->entry);
  859. if (!version_entry) {
  860. dev_dbg(component->dev, "%s: failed to create wsa884x version entry\n",
  861. __func__);
  862. snd_info_free_entry(wsa884x->entry);
  863. return -ENOMEM;
  864. }
  865. version_entry->private_data = wsa884x;
  866. version_entry->size = WSA884X_VERSION_ENTRY_SIZE;
  867. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  868. version_entry->c.ops = &wsa884x_codec_info_ops;
  869. if (snd_info_register(version_entry) < 0) {
  870. snd_info_free_entry(version_entry);
  871. snd_info_free_entry(wsa884x->entry);
  872. return -ENOMEM;
  873. }
  874. wsa884x->version_entry = version_entry;
  875. variant_entry = snd_info_create_card_entry(card->snd_card,
  876. "variant",
  877. wsa884x->entry);
  878. if (!variant_entry) {
  879. dev_dbg(component->dev,
  880. "%s: failed to create wsa884x variant entry\n",
  881. __func__);
  882. snd_info_free_entry(version_entry);
  883. snd_info_free_entry(wsa884x->entry);
  884. return -ENOMEM;
  885. }
  886. variant_entry->private_data = wsa884x;
  887. variant_entry->size = WSA884X_VARIANT_ENTRY_SIZE;
  888. variant_entry->content = SNDRV_INFO_CONTENT_DATA;
  889. variant_entry->c.ops = &wsa884x_variant_ops;
  890. if (snd_info_register(variant_entry) < 0) {
  891. snd_info_free_entry(variant_entry);
  892. snd_info_free_entry(version_entry);
  893. snd_info_free_entry(wsa884x->entry);
  894. return -ENOMEM;
  895. }
  896. wsa884x->variant_entry = variant_entry;
  897. return 0;
  898. }
  899. EXPORT_SYMBOL(wsa884x_codec_info_create_codec_entry);
  900. /*
  901. * wsa884x_codec_get_dev_num - returns swr device number
  902. * @component: Codec instance
  903. *
  904. * Return: swr device number on success or negative error
  905. * code on failure.
  906. */
  907. int wsa884x_codec_get_dev_num(struct snd_soc_component *component)
  908. {
  909. struct wsa884x_priv *wsa884x;
  910. if (!component)
  911. return -EINVAL;
  912. wsa884x = snd_soc_component_get_drvdata(component);
  913. if (!wsa884x) {
  914. pr_err_ratelimited("%s: wsa884x component is NULL\n", __func__);
  915. return -EINVAL;
  916. }
  917. return wsa884x->swr_slave->dev_num;
  918. }
  919. EXPORT_SYMBOL(wsa884x_codec_get_dev_num);
  920. static int wsa884x_get_dev_num(struct snd_kcontrol *kcontrol,
  921. struct snd_ctl_elem_value *ucontrol)
  922. {
  923. struct snd_soc_component *component =
  924. snd_soc_kcontrol_component(kcontrol);
  925. struct wsa884x_priv *wsa884x;
  926. if (!component)
  927. return -EINVAL;
  928. wsa884x = snd_soc_component_get_drvdata(component);
  929. if (!wsa884x) {
  930. pr_err_ratelimited("%s: wsa884x component is NULL\n", __func__);
  931. return -EINVAL;
  932. }
  933. ucontrol->value.integer.value[0] = wsa884x->swr_slave->dev_num;
  934. return 0;
  935. }
  936. static int wsa884x_get_compander(struct snd_kcontrol *kcontrol,
  937. struct snd_ctl_elem_value *ucontrol)
  938. {
  939. struct snd_soc_component *component =
  940. snd_soc_kcontrol_component(kcontrol);
  941. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  942. ucontrol->value.integer.value[0] = wsa884x->comp_enable;
  943. return 0;
  944. }
  945. /*
  946. * wsa884x_validate_dt_configuration_params - returns 1 or 0
  947. * Return: 0 Valid configuration, 1 Invalid configuration
  948. */
  949. static bool wsa884x_validate_dt_configuration_params(struct snd_soc_component *component,
  950. u8 irload, u8 ibat_cfg_dts, u8 isystem_gain)
  951. {
  952. u8 bat_cfg_reg;
  953. bool is_invalid_flag = true;
  954. bat_cfg_reg = snd_soc_component_read(component, WSA884X_VPHX_SYS_EN_STATUS);
  955. if ((ibat_cfg_dts == EXT_1S) || (ibat_cfg_dts == EXT_2S) || (ibat_cfg_dts == EXT_3S))
  956. ibat_cfg_dts = EXT_ABOVE_3S;
  957. if ((WSA_4_OHMS <= irload && irload < WSA_MAX_OHMS) &&
  958. (G_21_DB <= isystem_gain && isystem_gain < G_MAX_DB) &&
  959. (EXT_ABOVE_3S <= ibat_cfg_dts && ibat_cfg_dts < CONFIG_MAX) &&
  960. (ibat_cfg_dts == bat_cfg_reg))
  961. is_invalid_flag = false;
  962. return is_invalid_flag;
  963. }
  964. static int wsa884x_set_compander(struct snd_kcontrol *kcontrol,
  965. struct snd_ctl_elem_value *ucontrol)
  966. {
  967. struct snd_soc_component *component =
  968. snd_soc_kcontrol_component(kcontrol);
  969. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  970. int value = ucontrol->value.integer.value[0];
  971. dev_dbg(component->dev, "%s: Compander enable current %d, new %d\n",
  972. __func__, wsa884x->comp_enable, value);
  973. wsa884x->comp_enable = value;
  974. return 0;
  975. }
  976. static int wsa884x_get_visense(struct snd_kcontrol *kcontrol,
  977. struct snd_ctl_elem_value *ucontrol)
  978. {
  979. struct snd_soc_component *component =
  980. snd_soc_kcontrol_component(kcontrol);
  981. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  982. ucontrol->value.integer.value[0] = wsa884x->visense_enable;
  983. return 0;
  984. }
  985. static int wsa884x_set_visense(struct snd_kcontrol *kcontrol,
  986. struct snd_ctl_elem_value *ucontrol)
  987. {
  988. struct snd_soc_component *component =
  989. snd_soc_kcontrol_component(kcontrol);
  990. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  991. int value = ucontrol->value.integer.value[0];
  992. dev_dbg(component->dev, "%s: VIsense enable current %d, new %d\n",
  993. __func__, wsa884x->visense_enable, value);
  994. wsa884x->visense_enable = value;
  995. return 0;
  996. }
  997. static int wsa884x_get_pbr(struct snd_kcontrol *kcontrol,
  998. struct snd_ctl_elem_value *ucontrol)
  999. {
  1000. struct snd_soc_component *component =
  1001. snd_soc_kcontrol_component(kcontrol);
  1002. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1003. ucontrol->value.integer.value[0] = wsa884x->pbr_enable;
  1004. return 0;
  1005. }
  1006. static int wsa884x_set_pbr(struct snd_kcontrol *kcontrol,
  1007. struct snd_ctl_elem_value *ucontrol)
  1008. {
  1009. struct snd_soc_component *component =
  1010. snd_soc_kcontrol_component(kcontrol);
  1011. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1012. int value = ucontrol->value.integer.value[0];
  1013. dev_dbg(component->dev, "%s: PBR enable current %d, new %d\n",
  1014. __func__, wsa884x->pbr_enable, value);
  1015. wsa884x->pbr_enable = value;
  1016. return 0;
  1017. }
  1018. static int wsa884x_get_cps(struct snd_kcontrol *kcontrol,
  1019. struct snd_ctl_elem_value *ucontrol)
  1020. {
  1021. struct snd_soc_component *component =
  1022. snd_soc_kcontrol_component(kcontrol);
  1023. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1024. ucontrol->value.integer.value[0] = wsa884x->cps_enable;
  1025. return 0;
  1026. }
  1027. static int wsa884x_set_cps(struct snd_kcontrol *kcontrol,
  1028. struct snd_ctl_elem_value *ucontrol)
  1029. {
  1030. struct snd_soc_component *component =
  1031. snd_soc_kcontrol_component(kcontrol);
  1032. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1033. int value = ucontrol->value.integer.value[0];
  1034. dev_dbg(component->dev, "%s: CPS enable current %d, new %d\n",
  1035. __func__, wsa884x->cps_enable, value);
  1036. wsa884x->cps_enable = value;
  1037. return 0;
  1038. }
  1039. static const struct snd_kcontrol_new wsa884x_snd_controls[] = {
  1040. SOC_ENUM_EXT("WSA PA Gain", wsa_pa_gain_enum,
  1041. wsa_pa_gain_get, wsa_pa_gain_put),
  1042. SOC_SINGLE_EXT("WSA Temp", SND_SOC_NOPM, 0, UINT_MAX, 0,
  1043. wsa_get_temp, NULL),
  1044. SOC_SINGLE_EXT("WSA Get DevNum", SND_SOC_NOPM, 0, UINT_MAX, 0,
  1045. wsa884x_get_dev_num, NULL),
  1046. SOC_SINGLE_EXT("WSA MODE", SND_SOC_NOPM, 0, 1, 0,
  1047. wsa_dev_mode_get, wsa_dev_mode_put),
  1048. SOC_SINGLE_EXT("COMP Switch", SND_SOC_NOPM, 0, 1, 0,
  1049. wsa884x_get_compander, wsa884x_set_compander),
  1050. SOC_SINGLE_EXT("VISENSE Switch", SND_SOC_NOPM, 0, 1, 0,
  1051. wsa884x_get_visense, wsa884x_set_visense),
  1052. SOC_SINGLE_EXT("PBR Switch", SND_SOC_NOPM, 0, 1, 0,
  1053. wsa884x_get_pbr, wsa884x_set_pbr),
  1054. SOC_SINGLE_EXT("CPS Switch", SND_SOC_NOPM, 0, 1, 0,
  1055. wsa884x_get_cps, wsa884x_set_cps),
  1056. };
  1057. static const struct snd_kcontrol_new swr_dac_port[] = {
  1058. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1059. };
  1060. static int wsa884x_set_port(struct snd_soc_component *component, int port_idx,
  1061. u8 *port_id, u8 *num_ch, u8 *ch_mask, u32 *ch_rate,
  1062. u8 *port_type)
  1063. {
  1064. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1065. *port_id = wsa884x->port[port_idx].port_id;
  1066. *num_ch = wsa884x->port[port_idx].num_ch;
  1067. *ch_mask = wsa884x->port[port_idx].ch_mask;
  1068. *ch_rate = wsa884x->port[port_idx].ch_rate;
  1069. *port_type = wsa884x->port[port_idx].port_type;
  1070. return 0;
  1071. }
  1072. static int wsa884x_enable_swr_dac_port(struct snd_soc_dapm_widget *w,
  1073. struct snd_kcontrol *kcontrol, int event)
  1074. {
  1075. struct snd_soc_component *component =
  1076. snd_soc_dapm_to_component(w->dapm);
  1077. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1078. u8 port_id[WSA884X_MAX_SWR_PORTS];
  1079. u8 num_ch[WSA884X_MAX_SWR_PORTS];
  1080. u8 ch_mask[WSA884X_MAX_SWR_PORTS];
  1081. u32 ch_rate[WSA884X_MAX_SWR_PORTS];
  1082. u8 port_type[WSA884X_MAX_SWR_PORTS];
  1083. u8 num_port = 0;
  1084. dev_dbg(component->dev, "%s: event %d name %s\n", __func__,
  1085. event, w->name);
  1086. if (wsa884x == NULL)
  1087. return -EINVAL;
  1088. switch (event) {
  1089. case SND_SOC_DAPM_PRE_PMU:
  1090. wsa884x_set_port(component, SWR_DAC_PORT,
  1091. &port_id[num_port], &num_ch[num_port],
  1092. &ch_mask[num_port], &ch_rate[num_port],
  1093. &port_type[num_port]);
  1094. if (wsa884x->dev_mode == RECEIVER)
  1095. ch_rate[num_port] = SWR_CLK_RATE_4P8MHZ;
  1096. ++num_port;
  1097. if (wsa884x->comp_enable) {
  1098. wsa884x_set_port(component, SWR_COMP_PORT,
  1099. &port_id[num_port], &num_ch[num_port],
  1100. &ch_mask[num_port], &ch_rate[num_port],
  1101. &port_type[num_port]);
  1102. ++num_port;
  1103. }
  1104. if (wsa884x->pbr_enable) {
  1105. wsa884x_set_port(component, SWR_PBR_PORT,
  1106. &port_id[num_port], &num_ch[num_port],
  1107. &ch_mask[num_port], &ch_rate[num_port],
  1108. &port_type[num_port]);
  1109. ++num_port;
  1110. }
  1111. if (wsa884x->visense_enable) {
  1112. wsa884x_set_port(component, SWR_VISENSE_PORT,
  1113. &port_id[num_port], &num_ch[num_port],
  1114. &ch_mask[num_port], &ch_rate[num_port],
  1115. &port_type[num_port]);
  1116. ++num_port;
  1117. }
  1118. if (wsa884x->cps_enable) {
  1119. wsa884x_set_port(component, SWR_CPS_PORT,
  1120. &port_id[num_port], &num_ch[num_port],
  1121. &ch_mask[num_port], &ch_rate[num_port],
  1122. &port_type[num_port]);
  1123. ++num_port;
  1124. }
  1125. swr_connect_port(wsa884x->swr_slave, &port_id[0], num_port,
  1126. &ch_mask[0], &ch_rate[0], &num_ch[0],
  1127. &port_type[0]);
  1128. break;
  1129. case SND_SOC_DAPM_POST_PMU:
  1130. set_bit(SPKR_STATUS, &wsa884x->status_mask);
  1131. break;
  1132. case SND_SOC_DAPM_PRE_PMD:
  1133. wsa884x_set_port(component, SWR_DAC_PORT,
  1134. &port_id[num_port], &num_ch[num_port],
  1135. &ch_mask[num_port], &ch_rate[num_port],
  1136. &port_type[num_port]);
  1137. ++num_port;
  1138. if (wsa884x->comp_enable) {
  1139. wsa884x_set_port(component, SWR_COMP_PORT,
  1140. &port_id[num_port], &num_ch[num_port],
  1141. &ch_mask[num_port], &ch_rate[num_port],
  1142. &port_type[num_port]);
  1143. ++num_port;
  1144. }
  1145. if (wsa884x->pbr_enable) {
  1146. wsa884x_set_port(component, SWR_PBR_PORT,
  1147. &port_id[num_port], &num_ch[num_port],
  1148. &ch_mask[num_port], &ch_rate[num_port],
  1149. &port_type[num_port]);
  1150. ++num_port;
  1151. }
  1152. if (wsa884x->visense_enable) {
  1153. wsa884x_set_port(component, SWR_VISENSE_PORT,
  1154. &port_id[num_port], &num_ch[num_port],
  1155. &ch_mask[num_port], &ch_rate[num_port],
  1156. &port_type[num_port]);
  1157. ++num_port;
  1158. }
  1159. if (wsa884x->cps_enable) {
  1160. wsa884x_set_port(component, SWR_CPS_PORT,
  1161. &port_id[num_port], &num_ch[num_port],
  1162. &ch_mask[num_port], &ch_rate[num_port],
  1163. &port_type[num_port]);
  1164. ++num_port;
  1165. }
  1166. swr_disconnect_port(wsa884x->swr_slave, &port_id[0], num_port,
  1167. &ch_mask[0], &port_type[0]);
  1168. break;
  1169. case SND_SOC_DAPM_POST_PMD:
  1170. if (swr_set_device_group(wsa884x->swr_slave, SWR_GROUP_NONE))
  1171. dev_err_ratelimited(component->dev,
  1172. "%s: set num ch failed\n", __func__);
  1173. swr_slvdev_datapath_control(wsa884x->swr_slave,
  1174. wsa884x->swr_slave->dev_num,
  1175. false);
  1176. break;
  1177. default:
  1178. break;
  1179. }
  1180. return 0;
  1181. }
  1182. static int wsa884x_spkr_event(struct snd_soc_dapm_widget *w,
  1183. struct snd_kcontrol *kcontrol, int event)
  1184. {
  1185. struct snd_soc_component *component =
  1186. snd_soc_dapm_to_component(w->dapm);
  1187. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1188. dev_dbg(component->dev, "%s: %s %d\n", __func__, w->name, event);
  1189. switch (event) {
  1190. case SND_SOC_DAPM_POST_PMU:
  1191. swr_slvdev_datapath_control(wsa884x->swr_slave,
  1192. wsa884x->swr_slave->dev_num,
  1193. true);
  1194. wsa884x_set_gain_parameters(component);
  1195. if (wsa884x->dev_mode == SPEAKER) {
  1196. snd_soc_component_update_bits(component,
  1197. REG_FIELD_VALUE(DRE_CTL_0, PROG_DELAY, 0x0F));
  1198. } else {
  1199. snd_soc_component_update_bits(component,
  1200. REG_FIELD_VALUE(DRE_CTL_0, PROG_DELAY, 0x03));
  1201. snd_soc_component_update_bits(component,
  1202. REG_FIELD_VALUE(CDC_PATH_MODE, RXD_MODE, 0x01));
  1203. snd_soc_component_update_bits(component,
  1204. REG_FIELD_VALUE(PWM_CLK_CTL,
  1205. PWM_CLK_FREQ_SEL, 0x01));
  1206. }
  1207. if (wsa884x->pbr_enable) {
  1208. snd_soc_component_update_bits(component,
  1209. REG_FIELD_VALUE(CURRENT_LIMIT,
  1210. CURRENT_LIMIT_OVRD_EN, 0x00));
  1211. switch (wsa884x->bat_cfg) {
  1212. case CONFIG_1S:
  1213. snd_soc_component_update_bits(component,
  1214. REG_FIELD_VALUE(CURRENT_LIMIT,
  1215. CURRENT_LIMIT, 0x15));
  1216. break;
  1217. case CONFIG_2S:
  1218. snd_soc_component_update_bits(component,
  1219. REG_FIELD_VALUE(CURRENT_LIMIT,
  1220. CURRENT_LIMIT, 0x11));
  1221. break;
  1222. case CONFIG_3S:
  1223. snd_soc_component_update_bits(component,
  1224. REG_FIELD_VALUE(CURRENT_LIMIT,
  1225. CURRENT_LIMIT, 0x0D));
  1226. break;
  1227. }
  1228. } else {
  1229. snd_soc_component_update_bits(component,
  1230. REG_FIELD_VALUE(CURRENT_LIMIT,
  1231. CURRENT_LIMIT_OVRD_EN, 0x01));
  1232. if (wsa884x->system_gain >= G_12_DB)
  1233. snd_soc_component_update_bits(component,
  1234. REG_FIELD_VALUE(CURRENT_LIMIT,
  1235. CURRENT_LIMIT, 0x15));
  1236. else
  1237. snd_soc_component_update_bits(component,
  1238. REG_FIELD_VALUE(CURRENT_LIMIT,
  1239. CURRENT_LIMIT, 0x09));
  1240. }
  1241. /* Force remove group */
  1242. swr_remove_from_group(wsa884x->swr_slave,
  1243. wsa884x->swr_slave->dev_num);
  1244. if (test_bit(SPKR_ADIE_LB, &wsa884x->status_mask) &&
  1245. !wsa884x->pa_mute)
  1246. snd_soc_component_update_bits(component,
  1247. REG_FIELD_VALUE(PA_FSM_EN, GLOBAL_PA_EN, 0x01));
  1248. break;
  1249. case SND_SOC_DAPM_PRE_PMD:
  1250. snd_soc_component_update_bits(component,
  1251. REG_FIELD_VALUE(PA_FSM_EN, GLOBAL_PA_EN, 0x00));
  1252. snd_soc_component_update_bits(component,
  1253. REG_FIELD_VALUE(PDM_WD_CTL, PDM_WD_EN, 0x00));
  1254. clear_bit(SPKR_STATUS, &wsa884x->status_mask);
  1255. clear_bit(SPKR_ADIE_LB, &wsa884x->status_mask);
  1256. wsa884x->pa_mute = 0;
  1257. break;
  1258. }
  1259. return 0;
  1260. }
  1261. static const struct snd_soc_dapm_widget wsa884x_dapm_widgets[] = {
  1262. SND_SOC_DAPM_INPUT("IN"),
  1263. SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0, swr_dac_port,
  1264. ARRAY_SIZE(swr_dac_port), wsa884x_enable_swr_dac_port,
  1265. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1266. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1267. SND_SOC_DAPM_SPK("SPKR", wsa884x_spkr_event),
  1268. };
  1269. static const struct snd_soc_dapm_route wsa884x_audio_map[] = {
  1270. {"SWR DAC_Port", "Switch", "IN"},
  1271. {"SPKR", NULL, "SWR DAC_Port"},
  1272. };
  1273. int wsa884x_set_channel_map(struct snd_soc_component *component, u8 *port,
  1274. u8 num_port, unsigned int *ch_mask,
  1275. unsigned int *ch_rate, u8 *port_type)
  1276. {
  1277. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1278. int i;
  1279. if (!port || !ch_mask || !ch_rate ||
  1280. (num_port > WSA884X_MAX_SWR_PORTS)) {
  1281. dev_err_ratelimited(component->dev,
  1282. "%s: Invalid port=%pK, ch_mask=%pK, ch_rate=%pK\n",
  1283. __func__, port, ch_mask, ch_rate);
  1284. return -EINVAL;
  1285. }
  1286. for (i = 0; i < num_port; i++) {
  1287. wsa884x->port[i].port_id = port[i];
  1288. wsa884x->port[i].ch_mask = ch_mask[i];
  1289. wsa884x->port[i].ch_rate = ch_rate[i];
  1290. wsa884x->port[i].num_ch = __sw_hweight8(ch_mask[i]);
  1291. if (port_type)
  1292. wsa884x->port[i].port_type = port_type[i];
  1293. }
  1294. return 0;
  1295. }
  1296. EXPORT_SYMBOL(wsa884x_set_channel_map);
  1297. static void wsa884x_codec_init(struct snd_soc_component *component)
  1298. {
  1299. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1300. int i;
  1301. if (!wsa884x)
  1302. return;
  1303. for (i = 0; i < ARRAY_SIZE(reg_init); i++)
  1304. snd_soc_component_update_bits(component, reg_init[i].reg,
  1305. reg_init[i].mask, reg_init[i].val);
  1306. wsa_noise_gate_write(component, wsa884x->noise_gate_mode);
  1307. }
  1308. static int32_t wsa884x_temp_reg_read(struct snd_soc_component *component,
  1309. struct wsa_temp_register *wsa_temp_reg)
  1310. {
  1311. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1312. if (!wsa884x) {
  1313. dev_err_ratelimited(component->dev, "%s: wsa884x is NULL\n", __func__);
  1314. return -EINVAL;
  1315. }
  1316. mutex_lock(&wsa884x->res_lock);
  1317. snd_soc_component_update_bits(component,
  1318. REG_FIELD_VALUE(PA_FSM_BYP0, DC_CAL_EN, 0x01));
  1319. snd_soc_component_update_bits(component,
  1320. REG_FIELD_VALUE(PA_FSM_BYP0, BG_EN, 0x01));
  1321. snd_soc_component_update_bits(component,
  1322. REG_FIELD_VALUE(PA_FSM_BYP0, CLK_WD_EN, 0x01));
  1323. snd_soc_component_update_bits(component,
  1324. REG_FIELD_VALUE(PA_FSM_BYP0, TSADC_EN, 0x01));
  1325. snd_soc_component_update_bits(component,
  1326. REG_FIELD_VALUE(PA_FSM_BYP0, D_UNMUTE, 0x01));
  1327. snd_soc_component_update_bits(component,
  1328. REG_FIELD_VALUE(PA_FSM_BYP0, SPKR_PROT_EN, 0x01));
  1329. snd_soc_component_update_bits(component,
  1330. REG_FIELD_VALUE(TADC_VALUE_CTL, TEMP_VALUE_RD_EN, 0x00));
  1331. wsa_temp_reg->dmeas_msb = snd_soc_component_read(component,
  1332. WSA884X_TEMP_DIN_MSB);
  1333. wsa_temp_reg->dmeas_lsb = snd_soc_component_read(component,
  1334. WSA884X_TEMP_DIN_LSB);
  1335. snd_soc_component_update_bits(component,
  1336. REG_FIELD_VALUE(TADC_VALUE_CTL, TEMP_VALUE_RD_EN, 0x01));
  1337. wsa_temp_reg->d1_msb = snd_soc_component_read(component,
  1338. WSA884X_OTP_REG_1);
  1339. wsa_temp_reg->d1_lsb = snd_soc_component_read(component,
  1340. WSA884X_OTP_REG_2);
  1341. wsa_temp_reg->d2_msb = snd_soc_component_read(component,
  1342. WSA884X_OTP_REG_3);
  1343. wsa_temp_reg->d2_lsb = snd_soc_component_read(component,
  1344. WSA884X_OTP_REG_4);
  1345. snd_soc_component_update_bits(component,
  1346. WSA884X_PA_FSM_BYP0, 0xE7, 0x00);
  1347. mutex_unlock(&wsa884x->res_lock);
  1348. return 0;
  1349. }
  1350. static int wsa884x_get_temperature(struct snd_soc_component *component,
  1351. int *temp)
  1352. {
  1353. struct wsa_temp_register reg;
  1354. int dmeas, d1, d2;
  1355. int ret = 0;
  1356. int temp_val = 0;
  1357. int t1 = T1_TEMP;
  1358. int t2 = T2_TEMP;
  1359. u8 retry = WSA884X_TEMP_RETRY;
  1360. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1361. if (!wsa884x)
  1362. return -EINVAL;
  1363. do {
  1364. ret = wsa884x_temp_reg_read(component, &reg);
  1365. if (ret) {
  1366. pr_err_ratelimited("%s: temp read failed: %d, current temp: %d\n",
  1367. __func__, ret, wsa884x->curr_temp);
  1368. if (temp)
  1369. *temp = wsa884x->curr_temp;
  1370. return 0;
  1371. }
  1372. /*
  1373. * Temperature register values are expected to be in the
  1374. * following range.
  1375. * d1_msb = 68 - 92 and d1_lsb = 0, 64, 128, 192
  1376. * d2_msb = 185 -218 and d2_lsb = 0, 64, 128, 192
  1377. */
  1378. if ((reg.d1_msb < 68 || reg.d1_msb > 92) ||
  1379. (!(reg.d1_lsb == 0 || reg.d1_lsb == 64 || reg.d1_lsb == 128 ||
  1380. reg.d1_lsb == 192)) ||
  1381. (reg.d2_msb < 185 || reg.d2_msb > 218) ||
  1382. (!(reg.d2_lsb == 0 || reg.d2_lsb == 64 || reg.d2_lsb == 128 ||
  1383. reg.d2_lsb == 192))) {
  1384. printk_ratelimited("%s: Temperature registers[%d %d %d %d] are out of range\n",
  1385. __func__, reg.d1_msb, reg.d1_lsb, reg.d2_msb,
  1386. reg.d2_lsb);
  1387. }
  1388. dmeas = ((reg.dmeas_msb << 0x8) | reg.dmeas_lsb) >> 0x6;
  1389. d1 = ((reg.d1_msb << 0x8) | reg.d1_lsb) >> 0x6;
  1390. d2 = ((reg.d2_msb << 0x8) | reg.d2_lsb) >> 0x6;
  1391. if (d1 == d2)
  1392. temp_val = TEMP_INVALID;
  1393. else
  1394. temp_val = t1 + (((dmeas - d1) * (t2 - t1))/(d2 - d1));
  1395. if (temp_val <= LOW_TEMP_THRESHOLD ||
  1396. temp_val >= HIGH_TEMP_THRESHOLD) {
  1397. pr_debug("%s: T0: %d is out of range[%d, %d]\n", __func__,
  1398. temp_val, LOW_TEMP_THRESHOLD, HIGH_TEMP_THRESHOLD);
  1399. if (retry--)
  1400. msleep(10);
  1401. } else {
  1402. break;
  1403. }
  1404. } while (retry);
  1405. wsa884x->curr_temp = temp_val;
  1406. if (temp)
  1407. *temp = temp_val;
  1408. pr_debug("%s: t0 measured: %d dmeas = %d, d1 = %d, d2 = %d\n",
  1409. __func__, temp_val, dmeas, d1, d2);
  1410. return ret;
  1411. }
  1412. static int wsa884x_codec_probe(struct snd_soc_component *component)
  1413. {
  1414. char w_name[MAX_NAME_LEN];
  1415. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1416. struct swr_device *dev;
  1417. int variant = 0, version = 0;
  1418. struct snd_soc_dapm_context *dapm =
  1419. snd_soc_component_get_dapm(component);
  1420. if (!wsa884x)
  1421. return -EINVAL;
  1422. if (!component->name_prefix)
  1423. return -EINVAL;
  1424. snd_soc_component_init_regmap(component, wsa884x->regmap);
  1425. dev = wsa884x->swr_slave;
  1426. wsa884x->component = component;
  1427. variant = (snd_soc_component_read(component, WSA884X_OTP_REG_0)
  1428. & FIELD_MASK(OTP_REG_0, WSA884X_ID));
  1429. wsa884x->variant = variant;
  1430. version = (snd_soc_component_read(component, WSA884X_CHIP_ID0)
  1431. & FIELD_MASK(CHIP_ID0, BYTE_0));
  1432. wsa884x->version = version;
  1433. wsa884x->comp_offset = COMP_OFFSET2;
  1434. wsa884x_codec_init(component);
  1435. wsa884x->global_pa_cnt = 0;
  1436. memset(w_name, 0, sizeof(w_name));
  1437. strlcpy(w_name, wsa884x->dai_driver->playback.stream_name,
  1438. sizeof(w_name));
  1439. snd_soc_dapm_ignore_suspend(dapm, w_name);
  1440. memset(w_name, 0, sizeof(w_name));
  1441. strlcpy(w_name, "IN", sizeof(w_name));
  1442. snd_soc_dapm_ignore_suspend(dapm, w_name);
  1443. memset(w_name, 0, sizeof(w_name));
  1444. strlcpy(w_name, "SWR DAC_Port", sizeof(w_name));
  1445. snd_soc_dapm_ignore_suspend(dapm, w_name);
  1446. memset(w_name, 0, sizeof(w_name));
  1447. strlcpy(w_name, "SPKR", sizeof(w_name));
  1448. snd_soc_dapm_ignore_suspend(dapm, w_name);
  1449. snd_soc_dapm_sync(dapm);
  1450. return 0;
  1451. }
  1452. static void wsa884x_codec_remove(struct snd_soc_component *component)
  1453. {
  1454. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1455. if (!wsa884x)
  1456. return;
  1457. snd_soc_component_exit_regmap(component);
  1458. return;
  1459. }
  1460. static int wsa884x_soc_codec_suspend(struct snd_soc_component *component)
  1461. {
  1462. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1463. if (!wsa884x)
  1464. return 0;
  1465. wsa884x->dapm_bias_off = true;
  1466. return 0;
  1467. }
  1468. static int wsa884x_soc_codec_resume(struct snd_soc_component *component)
  1469. {
  1470. struct wsa884x_priv *wsa884x = snd_soc_component_get_drvdata(component);
  1471. if (!wsa884x)
  1472. return 0;
  1473. wsa884x->dapm_bias_off = false;
  1474. return 0;
  1475. }
  1476. static const struct snd_soc_component_driver soc_codec_dev_wsa884x_wsa = {
  1477. .name = "",
  1478. .probe = wsa884x_codec_probe,
  1479. .remove = wsa884x_codec_remove,
  1480. .controls = wsa884x_snd_controls,
  1481. .num_controls = ARRAY_SIZE(wsa884x_snd_controls),
  1482. .dapm_widgets = wsa884x_dapm_widgets,
  1483. .num_dapm_widgets = ARRAY_SIZE(wsa884x_dapm_widgets),
  1484. .dapm_routes = wsa884x_audio_map,
  1485. .num_dapm_routes = ARRAY_SIZE(wsa884x_audio_map),
  1486. .suspend = wsa884x_soc_codec_suspend,
  1487. .resume = wsa884x_soc_codec_resume,
  1488. };
  1489. static int wsa884x_gpio_ctrl(struct wsa884x_priv *wsa884x, bool enable)
  1490. {
  1491. int ret = 0;
  1492. if (enable)
  1493. ret = msm_cdc_pinctrl_select_active_state(
  1494. wsa884x->wsa_rst_np);
  1495. else
  1496. ret = msm_cdc_pinctrl_select_sleep_state(
  1497. wsa884x->wsa_rst_np);
  1498. if (ret != 0)
  1499. dev_err_ratelimited(wsa884x->dev,
  1500. "%s: Failed to turn state %d; ret=%d\n",
  1501. __func__, enable, ret);
  1502. return ret;
  1503. }
  1504. static int wsa884x_swr_up(struct wsa884x_priv *wsa884x)
  1505. {
  1506. int ret;
  1507. ret = wsa884x_gpio_ctrl(wsa884x, true);
  1508. if (ret)
  1509. dev_err_ratelimited(wsa884x->dev, "%s: Failed to enable gpio\n", __func__);
  1510. return ret;
  1511. }
  1512. static int wsa884x_swr_down(struct wsa884x_priv *wsa884x)
  1513. {
  1514. int ret;
  1515. ret = wsa884x_gpio_ctrl(wsa884x, false);
  1516. if (ret)
  1517. dev_err_ratelimited(wsa884x->dev, "%s: Failed to disable gpio\n", __func__);
  1518. return ret;
  1519. }
  1520. static int wsa884x_swr_reset(struct wsa884x_priv *wsa884x)
  1521. {
  1522. u8 retry = WSA884X_NUM_RETRY;
  1523. u8 devnum = 0;
  1524. struct swr_device *pdev;
  1525. pdev = wsa884x->swr_slave;
  1526. while (swr_get_logical_dev_num(pdev, pdev->addr, &devnum) && retry--) {
  1527. /* Retry after 1 msec delay */
  1528. usleep_range(1000, 1100);
  1529. }
  1530. pdev->dev_num = devnum;
  1531. wsa884x_regcache_sync(wsa884x);
  1532. return 0;
  1533. }
  1534. static int wsa884x_event_notify(struct notifier_block *nb,
  1535. unsigned long val, void *ptr)
  1536. {
  1537. u16 event = (val & 0xffff);
  1538. struct wsa884x_priv *wsa884x = container_of(nb, struct wsa884x_priv,
  1539. parent_nblock);
  1540. if (!wsa884x)
  1541. return -EINVAL;
  1542. switch (event) {
  1543. case BOLERO_SLV_EVT_PA_OFF_PRE_SSR:
  1544. if (test_bit(SPKR_STATUS, &wsa884x->status_mask))
  1545. snd_soc_component_update_bits(wsa884x->component,
  1546. REG_FIELD_VALUE(PA_FSM_EN, GLOBAL_PA_EN, 0x00));
  1547. wsa884x_swr_down(wsa884x);
  1548. break;
  1549. case BOLERO_SLV_EVT_SSR_UP:
  1550. wsa884x_swr_up(wsa884x);
  1551. /* Add delay to allow enumerate */
  1552. usleep_range(20000, 20010);
  1553. wsa884x_swr_reset(wsa884x);
  1554. dev_err(wsa884x->dev, "%s: BOLERO_SLV_EVT_SSR_UP Called", __func__);
  1555. swr_init_port_params(wsa884x->swr_slave, WSA884X_MAX_SWR_PORTS,
  1556. wsa884x->swr_wsa_port_params);
  1557. break;
  1558. case BOLERO_SLV_EVT_PA_ON_POST_FSCLK:
  1559. if (test_bit(SPKR_STATUS, &wsa884x->status_mask)) {
  1560. snd_soc_component_update_bits(wsa884x->component,
  1561. REG_FIELD_VALUE(PDM_WD_CTL, PDM_WD_EN, 0x01));
  1562. snd_soc_component_update_bits(wsa884x->component,
  1563. REG_FIELD_VALUE(PA_FSM_EN, GLOBAL_PA_EN, 0x01));
  1564. }
  1565. break;
  1566. case BOLERO_SLV_EVT_PA_ON_POST_FSCLK_ADIE_LB:
  1567. if (test_bit(SPKR_STATUS, &wsa884x->status_mask))
  1568. set_bit(SPKR_ADIE_LB, &wsa884x->status_mask);
  1569. break;
  1570. default:
  1571. dev_dbg(wsa884x->dev, "%s: unknown event %d\n",
  1572. __func__, event);
  1573. break;
  1574. }
  1575. return 0;
  1576. }
  1577. static int wsa884x_parse_port_params(struct device *dev, char *prop)
  1578. {
  1579. u32 *dt_array, map_size, max_uc;
  1580. int ret = 0;
  1581. u32 cnt = 0;
  1582. u32 i, j;
  1583. struct swr_port_params (*map)[SWR_UC_MAX][WSA884X_MAX_SWR_PORTS];
  1584. struct swr_dev_frame_config (*map_uc)[SWR_UC_MAX];
  1585. struct wsa884x_priv *wsa884x = dev_get_drvdata(dev);
  1586. map = &wsa884x->wsa_port_params;
  1587. map_uc = &wsa884x->swr_wsa_port_params;
  1588. if (!of_find_property(dev->of_node, prop,
  1589. &map_size)) {
  1590. dev_err(dev, "missing port mapping prop %s\n", prop);
  1591. ret = -EINVAL;
  1592. goto err_port_map;
  1593. }
  1594. max_uc = map_size / (WSA884X_MAX_SWR_PORTS * SWR_PORT_PARAMS * sizeof(u32));
  1595. if (max_uc != SWR_UC_MAX) {
  1596. dev_err(dev, "%s: port params not provided for all usecases\n",
  1597. __func__);
  1598. ret = -EINVAL;
  1599. goto err_port_map;
  1600. }
  1601. dt_array = kzalloc(map_size, GFP_KERNEL);
  1602. if (!dt_array) {
  1603. ret = -ENOMEM;
  1604. goto err_port_map;
  1605. }
  1606. ret = of_property_read_u32_array(dev->of_node, prop, dt_array,
  1607. WSA884X_MAX_SWR_PORTS * SWR_PORT_PARAMS * max_uc);
  1608. if (ret) {
  1609. dev_err(dev, "%s: Failed to read port mapping from prop %s\n",
  1610. __func__, prop);
  1611. goto err_pdata_fail;
  1612. }
  1613. for (i = 0; i < max_uc; i++) {
  1614. for (j = 0; j < WSA884X_MAX_SWR_PORTS; j++) {
  1615. cnt = (i * WSA884X_MAX_SWR_PORTS + j) * SWR_PORT_PARAMS;
  1616. (*map)[i][j].offset1 = dt_array[cnt];
  1617. (*map)[i][j].lane_ctrl = dt_array[cnt + 1];
  1618. }
  1619. (*map_uc)[i].pp = &(*map)[i][0];
  1620. }
  1621. kfree(dt_array);
  1622. return 0;
  1623. err_pdata_fail:
  1624. kfree(dt_array);
  1625. err_port_map:
  1626. return ret;
  1627. }
  1628. static int wsa884x_enable_supplies(struct device *dev,
  1629. struct wsa884x_priv *priv)
  1630. {
  1631. int ret = 0;
  1632. /* Parse power supplies */
  1633. msm_cdc_get_power_supplies(dev, &priv->regulator,
  1634. &priv->num_supplies);
  1635. if (!priv->regulator || (priv->num_supplies <= 0)) {
  1636. dev_err(dev, "%s: no power supplies defined\n", __func__);
  1637. return -EINVAL;
  1638. }
  1639. ret = msm_cdc_init_supplies(dev, &priv->supplies,
  1640. priv->regulator, priv->num_supplies);
  1641. if (!priv->supplies) {
  1642. dev_err(dev, "%s: Cannot init wsa supplies\n",
  1643. __func__);
  1644. return ret;
  1645. }
  1646. ret = msm_cdc_enable_static_supplies(dev, priv->supplies,
  1647. priv->regulator,
  1648. priv->num_supplies);
  1649. if (ret)
  1650. dev_err(dev, "%s: wsa static supply enable failed!\n",
  1651. __func__);
  1652. return ret;
  1653. }
  1654. static struct snd_soc_dai_driver wsa_dai[] = {
  1655. {
  1656. .name = "",
  1657. .playback = {
  1658. .stream_name = "",
  1659. .rates = WSA884X_RATES | WSA884X_FRAC_RATES,
  1660. .formats = WSA884X_FORMATS,
  1661. .rate_max = 192000,
  1662. .rate_min = 8000,
  1663. .channels_min = 1,
  1664. .channels_max = 2,
  1665. },
  1666. },
  1667. };
  1668. static int wsa884x_swr_probe(struct swr_device *pdev)
  1669. {
  1670. int ret = 0;
  1671. struct wsa884x_priv *wsa884x;
  1672. u8 devnum = 0;
  1673. bool pin_state_current = false;
  1674. struct wsa_ctrl_platform_data *plat_data = NULL;
  1675. struct snd_soc_component *component;
  1676. u32 noise_gate_mode;
  1677. char buffer[MAX_NAME_LEN];
  1678. int dev_index = 0;
  1679. struct regmap_irq_chip *wsa884x_sub_regmap_irq_chip = NULL;
  1680. u8 wo0_val;
  1681. int sys_gain_size, sys_gain_length;
  1682. int wsa_dev_index;
  1683. wsa884x = devm_kzalloc(&pdev->dev, sizeof(struct wsa884x_priv),
  1684. GFP_KERNEL);
  1685. if (!wsa884x)
  1686. return -ENOMEM;
  1687. wsa884x_sub_regmap_irq_chip = devm_kzalloc(&pdev->dev, sizeof(struct regmap_irq_chip),
  1688. GFP_KERNEL);
  1689. if (!wsa884x_sub_regmap_irq_chip)
  1690. return -ENOMEM;
  1691. memcpy(wsa884x_sub_regmap_irq_chip, &wsa884x_regmap_irq_chip,
  1692. sizeof(struct regmap_irq_chip));
  1693. ret = wsa884x_enable_supplies(&pdev->dev, wsa884x);
  1694. if (ret) {
  1695. ret = -EPROBE_DEFER;
  1696. goto err;
  1697. }
  1698. wsa884x->wsa_rst_np = of_parse_phandle(pdev->dev.of_node,
  1699. "qcom,spkr-sd-n-node", 0);
  1700. if (!wsa884x->wsa_rst_np) {
  1701. dev_dbg(&pdev->dev, "%s: pinctrl not defined\n", __func__);
  1702. goto err_supply;
  1703. }
  1704. swr_set_dev_data(pdev, wsa884x);
  1705. wsa884x->swr_slave = pdev;
  1706. wsa884x->dev = &pdev->dev;
  1707. pin_state_current = msm_cdc_pinctrl_get_state(wsa884x->wsa_rst_np);
  1708. wsa884x_gpio_ctrl(wsa884x, true);
  1709. /*
  1710. * Add 5msec delay to provide sufficient time for
  1711. * soundwire auto enumeration of slave devices as
  1712. * per HW requirement.
  1713. */
  1714. usleep_range(5000, 5010);
  1715. ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
  1716. if (ret) {
  1717. dev_dbg(&pdev->dev,
  1718. "%s get devnum %d for dev addr %lx failed\n",
  1719. __func__, devnum, pdev->addr);
  1720. ret = -EPROBE_DEFER;
  1721. goto err_supply;
  1722. }
  1723. pdev->dev_num = devnum;
  1724. wsa884x->regmap = devm_regmap_init_swr(pdev,
  1725. &wsa884x_regmap_config);
  1726. if (IS_ERR(wsa884x->regmap)) {
  1727. ret = PTR_ERR(wsa884x->regmap);
  1728. dev_err(&pdev->dev, "%s: regmap_init failed %d\n",
  1729. __func__, ret);
  1730. goto dev_err;
  1731. }
  1732. devm_regmap_qti_debugfs_register(&pdev->dev, wsa884x->regmap);
  1733. wsa884x_sub_regmap_irq_chip->irq_drv_data = wsa884x;
  1734. wsa884x->irq_info.wcd_regmap_irq_chip = wsa884x_sub_regmap_irq_chip;
  1735. wsa884x->irq_info.codec_name = "WSA884X";
  1736. wsa884x->irq_info.regmap = wsa884x->regmap;
  1737. wsa884x->irq_info.dev = &pdev->dev;
  1738. ret = wcd_irq_init(&wsa884x->irq_info, &wsa884x->virq);
  1739. if (ret) {
  1740. dev_err(wsa884x->dev, "%s: IRQ init failed: %d\n",
  1741. __func__, ret);
  1742. goto dev_err;
  1743. }
  1744. wsa884x->swr_slave->slave_irq = wsa884x->virq;
  1745. wcd_request_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_SAF2WAR,
  1746. "WSA SAF2WAR", wsa884x_saf2war_handle_irq, wsa884x);
  1747. wcd_request_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_WAR2SAF,
  1748. "WSA WAR2SAF", wsa884x_war2saf_handle_irq, wsa884x);
  1749. wcd_request_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_DISABLE,
  1750. "WSA OTP", wsa884x_otp_handle_irq, wsa884x);
  1751. wcd_request_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_OCP,
  1752. "WSA OCP", wsa884x_ocp_handle_irq, wsa884x);
  1753. wcd_request_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_CLIP,
  1754. "WSA CLIP", wsa884x_clip_handle_irq, wsa884x);
  1755. wcd_disable_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_CLIP);
  1756. wcd_request_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_PDM_WD,
  1757. "WSA PDM WD", wsa884x_pdm_wd_handle_irq, wsa884x);
  1758. wcd_request_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_CLK_WD,
  1759. "WSA CLK WD", wsa884x_clk_wd_handle_irq, wsa884x);
  1760. wcd_request_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_INTR_PIN,
  1761. "WSA EXT INT", wsa884x_ext_int_handle_irq, wsa884x);
  1762. wcd_disable_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_INTR_PIN);
  1763. /* Under Voltage Lock out (UVLO) interrupt handle */
  1764. wcd_request_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_UVLO,
  1765. "WSA UVLO", wsa884x_uvlo_handle_irq, wsa884x);
  1766. wcd_request_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_PA_ON_ERR,
  1767. "WSA PA ERR", wsa884x_pa_on_err_handle_irq, wsa884x);
  1768. wsa884x->driver = devm_kzalloc(&pdev->dev,
  1769. sizeof(struct snd_soc_component_driver), GFP_KERNEL);
  1770. if (!wsa884x->driver) {
  1771. ret = -ENOMEM;
  1772. goto err_irq;
  1773. }
  1774. memcpy(wsa884x->driver, &soc_codec_dev_wsa884x_wsa,
  1775. sizeof(struct snd_soc_component_driver));
  1776. wsa884x->dai_driver = devm_kzalloc(&pdev->dev,
  1777. sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
  1778. if (!wsa884x->dai_driver) {
  1779. ret = -ENOMEM;
  1780. goto err_mem;
  1781. }
  1782. memcpy(wsa884x->dai_driver, wsa_dai, sizeof(struct snd_soc_dai_driver));
  1783. /* Get last digit from HEX format */
  1784. dev_index = (int)((char)(pdev->addr & 0xF));
  1785. dev_index += 1;
  1786. if (of_device_is_compatible(pdev->dev.of_node, "qcom,wsa884x_2"))
  1787. dev_index += 2;
  1788. snprintf(buffer, sizeof(buffer), "wsa-codec.%d", dev_index);
  1789. wsa884x->driver->name = kstrndup(buffer, strlen(buffer), GFP_KERNEL);
  1790. snprintf(buffer, sizeof(buffer), "wsa_rx%d", dev_index);
  1791. wsa884x->dai_driver->name =
  1792. kstrndup(buffer, strlen(buffer), GFP_KERNEL);
  1793. snprintf(buffer, sizeof(buffer), "WSA884X_AIF%d Playback", dev_index);
  1794. wsa884x->dai_driver->playback.stream_name =
  1795. kstrndup(buffer, strlen(buffer), GFP_KERNEL);
  1796. /* Number of DAI's used is 1 */
  1797. ret = snd_soc_register_component(&pdev->dev,
  1798. wsa884x->driver, wsa884x->dai_driver, 1);
  1799. component = snd_soc_lookup_component(&pdev->dev, wsa884x->driver->name);
  1800. if (!component) {
  1801. dev_err(&pdev->dev, "%s: component is NULL\n", __func__);
  1802. ret = -EINVAL;
  1803. goto err_mem;
  1804. }
  1805. wsa884x->parent_np = of_parse_phandle(pdev->dev.of_node,
  1806. "qcom,bolero-handle", 0);
  1807. if (!wsa884x->parent_np)
  1808. wsa884x->parent_np = of_parse_phandle(pdev->dev.of_node,
  1809. "qcom,lpass-cdc-handle", 0);
  1810. if (wsa884x->parent_np) {
  1811. wsa884x->parent_dev =
  1812. of_find_device_by_node(wsa884x->parent_np);
  1813. if (wsa884x->parent_dev) {
  1814. plat_data = dev_get_platdata(&wsa884x->parent_dev->dev);
  1815. if (plat_data) {
  1816. wsa884x->parent_nblock.notifier_call =
  1817. wsa884x_event_notify;
  1818. if (plat_data->register_notifier)
  1819. plat_data->register_notifier(
  1820. plat_data->handle,
  1821. &wsa884x->parent_nblock,
  1822. true);
  1823. wsa884x->register_notifier =
  1824. plat_data->register_notifier;
  1825. wsa884x->handle = plat_data->handle;
  1826. } else {
  1827. dev_err(&pdev->dev, "%s: plat data not found\n",
  1828. __func__);
  1829. }
  1830. } else {
  1831. dev_err(&pdev->dev, "%s: parent dev not found\n",
  1832. __func__);
  1833. }
  1834. } else {
  1835. dev_info(&pdev->dev, "%s: parent node not found\n", __func__);
  1836. }
  1837. /* Start in speaker mode by default */
  1838. wsa884x->dev_mode = SPEAKER;
  1839. wsa884x->dev_index = dev_index;
  1840. /* wsa_dev_index is macro_agnostic index */
  1841. wsa_dev_index = (wsa884x->dev_index - 1) % 2;
  1842. wsa884x->macro_np = of_parse_phandle(pdev->dev.of_node,
  1843. "qcom,wsa-macro-handle", 0);
  1844. if (wsa884x->macro_np) {
  1845. wsa884x->macro_dev =
  1846. of_find_device_by_node(wsa884x->macro_np);
  1847. if (wsa884x->macro_dev) {
  1848. ret = of_property_read_u32_index(
  1849. wsa884x->macro_dev->dev.of_node,
  1850. "qcom,wsa-rloads",
  1851. wsa_dev_index,
  1852. &wsa884x->rload);
  1853. if (ret) {
  1854. dev_err(&pdev->dev,
  1855. "%s: Failed to read wsa rloads\n",
  1856. __func__);
  1857. goto err_mem;
  1858. }
  1859. ret = of_property_read_u32_index(
  1860. wsa884x->macro_dev->dev.of_node,
  1861. "qcom,wsa-bat-cfgs",
  1862. wsa_dev_index,
  1863. &wsa884x->bat_cfg);
  1864. if (ret) {
  1865. dev_err(&pdev->dev,
  1866. "%s: Failed to read wsa bat cfgs\n",
  1867. __func__);
  1868. goto err_mem;
  1869. }
  1870. ret = of_property_read_u32(wsa884x->macro_dev->dev.of_node,
  1871. "qcom,noise-gate-mode", &noise_gate_mode);
  1872. if (ret) {
  1873. dev_info(&pdev->dev,
  1874. "%s: Failed to read wsa noise gate mode\n",
  1875. __func__);
  1876. wsa884x->noise_gate_mode = IDLE_DETECT;
  1877. } else {
  1878. if (IDLE_DETECT <= noise_gate_mode && noise_gate_mode <= NG3)
  1879. wsa884x->noise_gate_mode = noise_gate_mode;
  1880. else
  1881. wsa884x->noise_gate_mode = IDLE_DETECT;
  1882. }
  1883. if (!of_find_property(wsa884x->macro_dev->dev.of_node,
  1884. "qcom,wsa-system-gains", &sys_gain_size)) {
  1885. dev_err(&pdev->dev,
  1886. "%s: missing wsa-system-gains\n",
  1887. __func__);
  1888. goto err_mem;
  1889. }
  1890. sys_gain_length = sys_gain_size / (2 * sizeof(u32));
  1891. ret = of_property_read_u32_array(
  1892. wsa884x->macro_dev->dev.of_node,
  1893. "qcom,wsa-system-gains", wsa884x->sys_gains,
  1894. sys_gain_length);
  1895. if (ret) {
  1896. dev_err(&pdev->dev,
  1897. "%s: Failed to read wsa system gains\n",
  1898. __func__);
  1899. goto err_mem;
  1900. }
  1901. wsa884x->system_gain = wsa884x->sys_gains[
  1902. wsa884x->dev_mode + wsa_dev_index * 2];
  1903. } else {
  1904. dev_err(&pdev->dev, "%s: parent dev not found\n",
  1905. __func__);
  1906. goto err_mem;
  1907. }
  1908. } else {
  1909. dev_err(&pdev->dev, "%s: parent node not found\n", __func__);
  1910. goto err_mem;
  1911. }
  1912. dev_dbg(component->dev,
  1913. "%s: Bat_cfg: 0x%x rload: 0x%x, sys_gain: 0x%x\n", __func__,
  1914. wsa884x->bat_cfg, wsa884x->rload, wsa884x->system_gain);
  1915. ret = wsa884x_validate_dt_configuration_params(component, wsa884x->rload,
  1916. wsa884x->bat_cfg, wsa884x->system_gain);
  1917. if (ret) {
  1918. dev_err(&pdev->dev,
  1919. "%s: invalid dt parameter: Bat_cfg: 0x%x rload: 0x%x, sys_gain: 0x%x\n",
  1920. __func__, wsa884x->bat_cfg, wsa884x->rload, wsa884x->system_gain);
  1921. ret = -EINVAL;
  1922. goto err_mem;
  1923. }
  1924. /* Assume that compander is enabled by default unless it is haptics sku */
  1925. if (wsa884x->variant == WSA8845H)
  1926. wsa884x->comp_enable = false;
  1927. else
  1928. wsa884x->comp_enable = true;
  1929. wsa884x_set_gain_parameters(component);
  1930. wsa884x_set_pbr_parameters(component);
  1931. /* Must write WO registers in a single write */
  1932. wo0_val = (0xC | (wsa884x->pa_aux_gain << 0x02) | !wsa884x->dev_mode);
  1933. snd_soc_component_write(component, WSA884X_ANA_WO_CTL_0, wo0_val);
  1934. snd_soc_component_write(component, WSA884X_ANA_WO_CTL_1, 0x0);
  1935. if (wsa884x->rload == WSA_4_OHMS || wsa884x->rload == WSA_6_OHMS)
  1936. snd_soc_component_update_bits(component,
  1937. REG_FIELD_VALUE(OCP_CTL, OCP_CURR_LIMIT, 0x07));
  1938. if (wsa884x->dev_mode == SPEAKER) {
  1939. snd_soc_component_update_bits(component,
  1940. REG_FIELD_VALUE(DRE_CTL_0, PROG_DELAY, 0x0F));
  1941. } else {
  1942. snd_soc_component_update_bits(component,
  1943. REG_FIELD_VALUE(DRE_CTL_0, PROG_DELAY, 0x03));
  1944. snd_soc_component_update_bits(component,
  1945. REG_FIELD_VALUE(CDC_PATH_MODE, RXD_MODE, 0x01));
  1946. snd_soc_component_update_bits(component,
  1947. REG_FIELD_VALUE(PWM_CLK_CTL,
  1948. PWM_CLK_FREQ_SEL, 0x01));
  1949. }
  1950. if (wsa884x->bat_cfg != CONFIG_1S && wsa884x->bat_cfg != EXT_1S)
  1951. snd_soc_component_update_bits(component,
  1952. REG_FIELD_VALUE(TOP_CTRL1,
  1953. OCP_LOWVBAT_ITH_SEL_EN, 0x00));
  1954. ret = wsa884x_parse_port_params(&pdev->dev, "qcom,swr-wsa-port-params");
  1955. if (ret) {
  1956. dev_err(&pdev->dev, "Failed to read port params\n");
  1957. goto err;
  1958. }
  1959. swr_init_port_params(wsa884x->swr_slave, WSA884X_MAX_SWR_PORTS,
  1960. wsa884x->swr_wsa_port_params);
  1961. mutex_init(&wsa884x->res_lock);
  1962. #ifdef CONFIG_DEBUG_FS
  1963. if (!wsa884x->debugfs_dent) {
  1964. wsa884x->debugfs_dent = debugfs_create_dir(
  1965. dev_name(&pdev->dev), 0);
  1966. if (!IS_ERR(wsa884x->debugfs_dent)) {
  1967. wsa884x->debugfs_peek =
  1968. debugfs_create_file("swrslave_peek",
  1969. S_IFREG | 0444,
  1970. wsa884x->debugfs_dent,
  1971. (void *) pdev,
  1972. &codec_debug_read_ops);
  1973. wsa884x->debugfs_poke =
  1974. debugfs_create_file("swrslave_poke",
  1975. S_IFREG | 0444,
  1976. wsa884x->debugfs_dent,
  1977. (void *) pdev,
  1978. &codec_debug_write_ops);
  1979. wsa884x->debugfs_reg_dump =
  1980. debugfs_create_file(
  1981. "swrslave_reg_dump",
  1982. S_IFREG | 0444,
  1983. wsa884x->debugfs_dent,
  1984. (void *) pdev,
  1985. &codec_debug_dump_ops);
  1986. }
  1987. }
  1988. #endif
  1989. return 0;
  1990. err_mem:
  1991. if (wsa884x->dai_driver) {
  1992. kfree(wsa884x->dai_driver->name);
  1993. kfree(wsa884x->dai_driver->playback.stream_name);
  1994. kfree(wsa884x->dai_driver);
  1995. }
  1996. if (wsa884x->driver) {
  1997. kfree(wsa884x->driver->name);
  1998. kfree(wsa884x->driver);
  1999. }
  2000. err_irq:
  2001. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_SAF2WAR, NULL);
  2002. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_WAR2SAF, NULL);
  2003. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_DISABLE, NULL);
  2004. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_OCP, NULL);
  2005. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_CLIP, NULL);
  2006. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_PDM_WD, NULL);
  2007. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_CLK_WD, NULL);
  2008. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_INTR_PIN, NULL);
  2009. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_UVLO, NULL);
  2010. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_PA_ON_ERR, NULL);
  2011. wcd_irq_exit(&wsa884x->irq_info, wsa884x->virq);
  2012. dev_err:
  2013. if (pin_state_current == false)
  2014. wsa884x_gpio_ctrl(wsa884x, false);
  2015. swr_remove_device(pdev);
  2016. err_supply:
  2017. msm_cdc_release_supplies(&pdev->dev, wsa884x->supplies,
  2018. wsa884x->regulator,
  2019. wsa884x->num_supplies);
  2020. err:
  2021. swr_set_dev_data(pdev, NULL);
  2022. return ret;
  2023. }
  2024. static int wsa884x_swr_remove(struct swr_device *pdev)
  2025. {
  2026. struct wsa884x_priv *wsa884x;
  2027. wsa884x = swr_get_dev_data(pdev);
  2028. if (!wsa884x) {
  2029. dev_err(&pdev->dev, "%s: wsa884x is NULL\n", __func__);
  2030. return -EINVAL;
  2031. }
  2032. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_SAF2WAR, NULL);
  2033. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_WAR2SAF, NULL);
  2034. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_DISABLE, NULL);
  2035. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_OCP, NULL);
  2036. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_CLIP, NULL);
  2037. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_PDM_WD, NULL);
  2038. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_CLK_WD, NULL);
  2039. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_INTR_PIN, NULL);
  2040. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_UVLO, NULL);
  2041. wcd_free_irq(&wsa884x->irq_info, WSA884X_IRQ_INT_PA_ON_ERR, NULL);
  2042. if (wsa884x->register_notifier)
  2043. wsa884x->register_notifier(wsa884x->handle,
  2044. &wsa884x->parent_nblock, false);
  2045. #ifdef CONFIG_DEBUG_FS
  2046. debugfs_remove_recursive(wsa884x->debugfs_dent);
  2047. wsa884x->debugfs_dent = NULL;
  2048. #endif
  2049. mutex_destroy(&wsa884x->res_lock);
  2050. snd_soc_unregister_component(&pdev->dev);
  2051. if (wsa884x->dai_driver) {
  2052. kfree(wsa884x->dai_driver->name);
  2053. kfree(wsa884x->dai_driver->playback.stream_name);
  2054. kfree(wsa884x->dai_driver);
  2055. }
  2056. if (wsa884x->driver) {
  2057. kfree(wsa884x->driver->name);
  2058. kfree(wsa884x->driver);
  2059. }
  2060. msm_cdc_release_supplies(&pdev->dev, wsa884x->supplies,
  2061. wsa884x->regulator,
  2062. wsa884x->num_supplies);
  2063. swr_set_dev_data(pdev, NULL);
  2064. return 0;
  2065. }
  2066. #ifdef CONFIG_PM_SLEEP
  2067. static int wsa884x_swr_suspend(struct device *dev)
  2068. {
  2069. struct wsa884x_priv *wsa884x = swr_get_dev_data(to_swr_device(dev));
  2070. if (!wsa884x) {
  2071. dev_err_ratelimited(dev, "%s: wsa884x private data is NULL\n", __func__);
  2072. return -EINVAL;
  2073. }
  2074. dev_dbg(dev, "%s: system suspend\n", __func__);
  2075. if (wsa884x->dapm_bias_off ||
  2076. (snd_soc_component_get_bias_level(wsa884x->component) ==
  2077. SND_SOC_BIAS_OFF)) {
  2078. msm_cdc_set_supplies_lpm_mode(dev, wsa884x->supplies,
  2079. wsa884x->regulator,
  2080. wsa884x->num_supplies,
  2081. true);
  2082. set_bit(WSA_SUPPLIES_LPM_MODE, &wsa884x->status_mask);
  2083. }
  2084. return 0;
  2085. }
  2086. static int wsa884x_swr_resume(struct device *dev)
  2087. {
  2088. struct wsa884x_priv *wsa884x = swr_get_dev_data(to_swr_device(dev));
  2089. if (!wsa884x) {
  2090. dev_err(dev, "%s: wsa884x private data is NULL\n", __func__);
  2091. return -EINVAL;
  2092. }
  2093. if (test_bit(WSA_SUPPLIES_LPM_MODE, &wsa884x->status_mask)) {
  2094. msm_cdc_set_supplies_lpm_mode(dev, wsa884x->supplies,
  2095. wsa884x->regulator,
  2096. wsa884x->num_supplies,
  2097. false);
  2098. clear_bit(WSA_SUPPLIES_LPM_MODE, &wsa884x->status_mask);
  2099. }
  2100. dev_dbg(dev, "%s: system resume\n", __func__);
  2101. return 0;
  2102. }
  2103. #endif /* CONFIG_PM_SLEEP */
  2104. static const struct dev_pm_ops wsa884x_swr_pm_ops = {
  2105. .suspend_late = wsa884x_swr_suspend,
  2106. .resume_early = wsa884x_swr_resume,
  2107. };
  2108. static const struct swr_device_id wsa884x_swr_id[] = {
  2109. {"wsa884x", 0},
  2110. {"wsa884x_2", 0},
  2111. {}
  2112. };
  2113. static const struct of_device_id wsa884x_swr_dt_match[] = {
  2114. {
  2115. .compatible = "qcom,wsa884x",
  2116. },
  2117. {
  2118. .compatible = "qcom,wsa884x_2",
  2119. },
  2120. {}
  2121. };
  2122. static struct swr_driver wsa884x_swr_driver = {
  2123. .driver = {
  2124. .name = "wsa884x",
  2125. .owner = THIS_MODULE,
  2126. .pm = &wsa884x_swr_pm_ops,
  2127. .of_match_table = wsa884x_swr_dt_match,
  2128. },
  2129. .probe = wsa884x_swr_probe,
  2130. .remove = wsa884x_swr_remove,
  2131. .id_table = wsa884x_swr_id,
  2132. };
  2133. static int __init wsa884x_swr_init(void)
  2134. {
  2135. return swr_driver_register(&wsa884x_swr_driver);
  2136. }
  2137. static void __exit wsa884x_swr_exit(void)
  2138. {
  2139. swr_driver_unregister(&wsa884x_swr_driver);
  2140. }
  2141. module_init(wsa884x_swr_init);
  2142. module_exit(wsa884x_swr_exit);
  2143. MODULE_DESCRIPTION("WSA884x codec driver");
  2144. MODULE_LICENSE("GPL v2");