wsa884x.c 67 KB

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