wsa884x.c 69 KB

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