wsa883x.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/device.h>
  10. #include <linux/printk.h>
  11. #include <linux/bitops.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/delay.h>
  15. #include <linux/kernel.h>
  16. #include <linux/gpio.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/regmap.h>
  20. #include <linux/debugfs.h>
  21. #include <soc/soundwire.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/tlv.h>
  27. #include <asoc/msm-cdc-pinctrl.h>
  28. #include <asoc/msm-cdc-supply.h>
  29. #include "wsa883x.h"
  30. #include "internal.h"
  31. #include "asoc/bolero-slave-internal.h"
  32. #include <linux/qti-regmap-debugfs.h>
  33. #define T1_TEMP -10
  34. #define T2_TEMP 150
  35. #define LOW_TEMP_THRESHOLD 5
  36. #define HIGH_TEMP_THRESHOLD 45
  37. #define TEMP_INVALID 0xFFFF
  38. #define WSA883X_TEMP_RETRY 3
  39. #define MAX_NAME_LEN 40
  40. #define WSA883X_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  41. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |\
  42. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000 |\
  43. SNDRV_PCM_RATE_384000)
  44. /* Fractional Rates */
  45. #define WSA883X_FRAC_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |\
  46. SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_352800)
  47. #define WSA883X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  48. SNDRV_PCM_FMTBIT_S24_LE |\
  49. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  50. enum {
  51. WSA_4OHMS =4,
  52. WSA_8OHMS = 8,
  53. WSA_16OHMS = 16,
  54. WSA_32OHMS = 32,
  55. };
  56. struct wsa_temp_register {
  57. u8 d1_msb;
  58. u8 d1_lsb;
  59. u8 d2_msb;
  60. u8 d2_lsb;
  61. u8 dmeas_msb;
  62. u8 dmeas_lsb;
  63. };
  64. enum {
  65. COMP_OFFSET0,
  66. COMP_OFFSET1,
  67. COMP_OFFSET2,
  68. COMP_OFFSET3,
  69. COMP_OFFSET4,
  70. };
  71. struct wsa_reg_mask_val {
  72. u16 reg;
  73. u8 mask;
  74. u8 val;
  75. };
  76. static const struct wsa_reg_mask_val reg_init[] = {
  77. {WSA883X_PA_FSM_BYP, 0x01, 0x00},
  78. {WSA883X_ISENSE2, 0xE0, 0x40},
  79. {WSA883X_ADC_6, 0x02, 0x02},
  80. {WSA883X_CDC_SPK_DSM_A2_0, 0xFF, 0x0A},
  81. {WSA883X_CDC_SPK_DSM_A2_1, 0x0F, 0x08},
  82. {WSA883X_CDC_SPK_DSM_A3_0, 0xFF, 0xF3},
  83. {WSA883X_CDC_SPK_DSM_A3_1, 0x07, 0x07},
  84. {WSA883X_CDC_SPK_DSM_A4_0, 0xFF, 0x79},
  85. {WSA883X_CDC_SPK_DSM_A4_1, 0x03, 0x02},
  86. {WSA883X_CDC_SPK_DSM_A5_0, 0xFF, 0x0B},
  87. {WSA883X_CDC_SPK_DSM_A5_1, 0x03, 0x02},
  88. {WSA883X_CDC_SPK_DSM_A6_0, 0xFF, 0x8A},
  89. {WSA883X_CDC_SPK_DSM_A7_0, 0xFF, 0x9B},
  90. {WSA883X_CDC_SPK_DSM_C_0, 0xFF, 0x68},
  91. {WSA883X_CDC_SPK_DSM_C_1, 0xFF, 0x54},
  92. {WSA883X_CDC_SPK_DSM_C_2, 0xFF, 0xF2},
  93. {WSA883X_CDC_SPK_DSM_C_3, 0x3F, 0x20},
  94. {WSA883X_CDC_SPK_DSM_R1, 0xFF, 0x83},
  95. {WSA883X_CDC_SPK_DSM_R2, 0xFF, 0x7F},
  96. {WSA883X_CDC_SPK_DSM_R3, 0xFF, 0x9D},
  97. {WSA883X_CDC_SPK_DSM_R4, 0xFF, 0x82},
  98. {WSA883X_CDC_SPK_DSM_R5, 0xFF, 0x8B},
  99. {WSA883X_CDC_SPK_DSM_R6, 0xFF, 0x9B},
  100. {WSA883X_CDC_SPK_DSM_R7, 0xFF, 0x3F},
  101. {WSA883X_VBAT_SNS, 0x60, 0x20},
  102. {WSA883X_DRE_CTL_0, 0xF0, 0x90},
  103. {WSA883X_DRE_IDLE_DET_CTL, 0x10, 0x00},
  104. {WSA883X_CURRENT_LIMIT, 0x78, 0x40},
  105. {WSA883X_DRE_CTL_0, 0x07, 0x02},
  106. {WSA883X_VAGC_TIME, 0x0F, 0x0F},
  107. {WSA883X_VAGC_ATTN_LVL_1_2, 0x77, 0x00},
  108. {WSA883X_VAGC_ATTN_LVL_3, 0x07, 0x01},
  109. {WSA883X_VAGC_CTL, 0x01, 0x01},
  110. {WSA883X_TAGC_CTL, 0x0E, 0x0A},
  111. {WSA883X_TAGC_TIME, 0x0C, 0x0C},
  112. {WSA883X_TAGC_E2E_GAIN, 0x1F, 0x02},
  113. {WSA883X_TEMP_CONFIG0, 0x07, 0x02},
  114. {WSA883X_TEMP_CONFIG1, 0x07, 0x02},
  115. {WSA883X_OTP_REG_1, 0xFF, 0x49},
  116. {WSA883X_OTP_REG_2, 0xC0, 0x80},
  117. {WSA883X_OTP_REG_3, 0xFF, 0xC9},
  118. {WSA883X_OTP_REG_4, 0xC0, 0x40},
  119. {WSA883X_TAGC_CTL, 0x01, 0x01},
  120. {WSA883X_ADC_2, 0x40, 0x00},
  121. {WSA883X_ADC_7, 0x04, 0x04},
  122. {WSA883X_ADC_7, 0x02, 0x02},
  123. {WSA883X_CKWD_CTL_0, 0x60, 0x00},
  124. {WSA883X_DRE_CTL_1, 0x3E, 0x20},
  125. {WSA883X_CKWD_CTL_1, 0x1F, 0x1B},
  126. {WSA883X_GMAMP_SUP1, 0x60, 0x60},
  127. };
  128. static int wsa883x_handle_post_irq(void *data);
  129. static int wsa883x_get_temperature(struct snd_soc_component *component,
  130. int *temp);
  131. enum {
  132. WSA8830 = 0,
  133. WSA8835,
  134. WSA8832,
  135. WSA8835_V2 = 5,
  136. };
  137. enum {
  138. SPKR_STATUS = 0,
  139. WSA_SUPPLIES_LPM_MODE,
  140. SPKR_ADIE_LB,
  141. };
  142. enum {
  143. WSA883X_IRQ_INT_SAF2WAR = 0,
  144. WSA883X_IRQ_INT_WAR2SAF,
  145. WSA883X_IRQ_INT_DISABLE,
  146. WSA883X_IRQ_INT_OCP,
  147. WSA883X_IRQ_INT_CLIP,
  148. WSA883X_IRQ_INT_PDM_WD,
  149. WSA883X_IRQ_INT_CLK_WD,
  150. WSA883X_IRQ_INT_INTR_PIN,
  151. WSA883X_IRQ_INT_UVLO,
  152. WSA883X_IRQ_INT_PA_ON_ERR,
  153. WSA883X_NUM_IRQS,
  154. };
  155. static const struct regmap_irq wsa883x_irqs[WSA883X_NUM_IRQS] = {
  156. REGMAP_IRQ_REG(WSA883X_IRQ_INT_SAF2WAR, 0, 0x01),
  157. REGMAP_IRQ_REG(WSA883X_IRQ_INT_WAR2SAF, 0, 0x02),
  158. REGMAP_IRQ_REG(WSA883X_IRQ_INT_DISABLE, 0, 0x04),
  159. REGMAP_IRQ_REG(WSA883X_IRQ_INT_OCP, 0, 0x08),
  160. REGMAP_IRQ_REG(WSA883X_IRQ_INT_CLIP, 0, 0x10),
  161. REGMAP_IRQ_REG(WSA883X_IRQ_INT_PDM_WD, 0, 0x20),
  162. REGMAP_IRQ_REG(WSA883X_IRQ_INT_CLK_WD, 0, 0x40),
  163. REGMAP_IRQ_REG(WSA883X_IRQ_INT_INTR_PIN, 0, 0x80),
  164. REGMAP_IRQ_REG(WSA883X_IRQ_INT_UVLO, 1, 0x01),
  165. REGMAP_IRQ_REG(WSA883X_IRQ_INT_PA_ON_ERR, 1, 0x02),
  166. };
  167. static struct regmap_irq_chip wsa883x_regmap_irq_chip = {
  168. .name = "wsa883x",
  169. .irqs = wsa883x_irqs,
  170. .num_irqs = ARRAY_SIZE(wsa883x_irqs),
  171. .num_regs = 2,
  172. .status_base = WSA883X_INTR_STATUS0,
  173. .mask_base = WSA883X_INTR_MASK0,
  174. .type_base = WSA883X_INTR_LEVEL0,
  175. .ack_base = WSA883X_INTR_CLEAR0,
  176. .use_ack = 1,
  177. .runtime_pm = false,
  178. .handle_post_irq = wsa883x_handle_post_irq,
  179. .irq_drv_data = NULL,
  180. };
  181. static int wsa883x_handle_post_irq(void *data)
  182. {
  183. struct wsa883x_priv *wsa883x = data;
  184. u32 sts1 = 0, sts2 = 0;
  185. regmap_read(wsa883x->regmap, WSA883X_INTR_STATUS0, &sts1);
  186. regmap_read(wsa883x->regmap, WSA883X_INTR_STATUS1, &sts2);
  187. wsa883x->swr_slave->slave_irq_pending =
  188. ((sts1 || sts2) ? true : false);
  189. return IRQ_HANDLED;
  190. }
  191. #ifdef CONFIG_DEBUG_FS
  192. static int codec_debug_open(struct inode *inode, struct file *file)
  193. {
  194. file->private_data = inode->i_private;
  195. return 0;
  196. }
  197. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  198. {
  199. char *token;
  200. int base, cnt;
  201. token = strsep(&buf, " ");
  202. for (cnt = 0; cnt < num_of_par; cnt++) {
  203. if (token) {
  204. if ((token[1] == 'x') || (token[1] == 'X'))
  205. base = 16;
  206. else
  207. base = 10;
  208. if (kstrtou32(token, base, &param1[cnt]) != 0)
  209. return -EINVAL;
  210. token = strsep(&buf, " ");
  211. } else {
  212. return -EINVAL;
  213. }
  214. }
  215. return 0;
  216. }
  217. static bool is_swr_slave_reg_readable(int reg)
  218. {
  219. int ret = true;
  220. if (((reg > 0x46) && (reg < 0x4A)) ||
  221. ((reg > 0x4A) && (reg < 0x50)) ||
  222. ((reg > 0x55) && (reg < 0xD0)) ||
  223. ((reg > 0xD0) && (reg < 0xE0)) ||
  224. ((reg > 0xE0) && (reg < 0xF0)) ||
  225. ((reg > 0xF0) && (reg < 0x100)) ||
  226. ((reg > 0x105) && (reg < 0x120)) ||
  227. ((reg > 0x205) && (reg < 0x220)) ||
  228. ((reg > 0x305) && (reg < 0x320)) ||
  229. ((reg > 0x405) && (reg < 0x420)) ||
  230. ((reg > 0x128) && (reg < 0x130)) ||
  231. ((reg > 0x228) && (reg < 0x230)) ||
  232. ((reg > 0x328) && (reg < 0x330)) ||
  233. ((reg > 0x428) && (reg < 0x430)) ||
  234. ((reg > 0x138) && (reg < 0x205)) ||
  235. ((reg > 0x238) && (reg < 0x305)) ||
  236. ((reg > 0x338) && (reg < 0x405)) ||
  237. ((reg > 0x405) && (reg < 0xF00)) ||
  238. ((reg > 0xF05) && (reg < 0xF20)) ||
  239. ((reg > 0xF25) && (reg < 0xF30)) ||
  240. ((reg > 0xF35) && (reg < 0x2000)))
  241. ret = false;
  242. return ret;
  243. }
  244. static ssize_t swr_slave_reg_show(struct swr_device *pdev, char __user *ubuf,
  245. size_t count, loff_t *ppos)
  246. {
  247. int i, reg_val, len;
  248. ssize_t total = 0;
  249. char tmp_buf[SWR_SLV_MAX_BUF_LEN];
  250. if (!ubuf || !ppos)
  251. return 0;
  252. for (i = (((int) *ppos/BYTES_PER_LINE) + SWR_SLV_START_REG_ADDR);
  253. i <= SWR_SLV_MAX_REG_ADDR; i++) {
  254. if (!is_swr_slave_reg_readable(i))
  255. continue;
  256. swr_read(pdev, pdev->dev_num, i, &reg_val, 1);
  257. len = snprintf(tmp_buf, sizeof(tmp_buf), "0x%.3x: 0x%.2x\n", i,
  258. (reg_val & 0xFF));
  259. if (len < 0) {
  260. pr_err("%s: fail to fill the buffer\n", __func__);
  261. total = -EFAULT;
  262. goto copy_err;
  263. }
  264. if ((total + len) >= count - 1)
  265. break;
  266. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  267. pr_err("%s: fail to copy reg dump\n", __func__);
  268. total = -EFAULT;
  269. goto copy_err;
  270. }
  271. total += len;
  272. *ppos += len;
  273. }
  274. copy_err:
  275. *ppos = SWR_SLV_MAX_REG_ADDR * BYTES_PER_LINE;
  276. return total;
  277. }
  278. static ssize_t codec_debug_dump(struct file *file, char __user *ubuf,
  279. size_t count, loff_t *ppos)
  280. {
  281. struct swr_device *pdev;
  282. if (!count || !file || !ppos || !ubuf)
  283. return -EINVAL;
  284. pdev = file->private_data;
  285. if (!pdev)
  286. return -EINVAL;
  287. if (*ppos < 0)
  288. return -EINVAL;
  289. return swr_slave_reg_show(pdev, ubuf, count, ppos);
  290. }
  291. static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
  292. size_t count, loff_t *ppos)
  293. {
  294. char lbuf[SWR_SLV_RD_BUF_LEN];
  295. struct swr_device *pdev = NULL;
  296. struct wsa883x_priv *wsa883x = NULL;
  297. if (!count || !file || !ppos || !ubuf)
  298. return -EINVAL;
  299. pdev = file->private_data;
  300. if (!pdev)
  301. return -EINVAL;
  302. wsa883x = swr_get_dev_data(pdev);
  303. if (!wsa883x)
  304. return -EINVAL;
  305. if (*ppos < 0)
  306. return -EINVAL;
  307. snprintf(lbuf, sizeof(lbuf), "0x%x\n",
  308. (wsa883x->read_data & 0xFF));
  309. return simple_read_from_buffer(ubuf, count, ppos, lbuf,
  310. strnlen(lbuf, 7));
  311. }
  312. static ssize_t codec_debug_peek_write(struct file *file,
  313. const char __user *ubuf, size_t cnt, loff_t *ppos)
  314. {
  315. char lbuf[SWR_SLV_WR_BUF_LEN];
  316. int rc = 0;
  317. u32 param[5];
  318. struct swr_device *pdev = NULL;
  319. struct wsa883x_priv *wsa883x = NULL;
  320. if (!cnt || !file || !ppos || !ubuf)
  321. return -EINVAL;
  322. pdev = file->private_data;
  323. if (!pdev)
  324. return -EINVAL;
  325. wsa883x = swr_get_dev_data(pdev);
  326. if (!wsa883x)
  327. return -EINVAL;
  328. if (*ppos < 0)
  329. return -EINVAL;
  330. if (cnt > sizeof(lbuf) - 1)
  331. return -EINVAL;
  332. rc = copy_from_user(lbuf, ubuf, cnt);
  333. if (rc)
  334. return -EFAULT;
  335. lbuf[cnt] = '\0';
  336. rc = get_parameters(lbuf, param, 1);
  337. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) && (rc == 0)))
  338. return -EINVAL;
  339. swr_read(pdev, pdev->dev_num, param[0], &wsa883x->read_data, 1);
  340. if (rc == 0)
  341. rc = cnt;
  342. else
  343. pr_err("%s: rc = %d\n", __func__, rc);
  344. return rc;
  345. }
  346. static ssize_t codec_debug_write(struct file *file,
  347. const char __user *ubuf, size_t cnt, loff_t *ppos)
  348. {
  349. char lbuf[SWR_SLV_WR_BUF_LEN];
  350. int rc = 0;
  351. u32 param[5];
  352. struct swr_device *pdev;
  353. if (!file || !ppos || !ubuf)
  354. return -EINVAL;
  355. pdev = file->private_data;
  356. if (!pdev)
  357. return -EINVAL;
  358. if (cnt > sizeof(lbuf) - 1)
  359. return -EINVAL;
  360. rc = copy_from_user(lbuf, ubuf, cnt);
  361. if (rc)
  362. return -EFAULT;
  363. lbuf[cnt] = '\0';
  364. rc = get_parameters(lbuf, param, 2);
  365. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) &&
  366. (param[1] <= 0xFF) && (rc == 0)))
  367. return -EINVAL;
  368. swr_write(pdev, pdev->dev_num, param[0], &param[1]);
  369. if (rc == 0)
  370. rc = cnt;
  371. else
  372. pr_err("%s: rc = %d\n", __func__, rc);
  373. return rc;
  374. }
  375. static const struct file_operations codec_debug_write_ops = {
  376. .open = codec_debug_open,
  377. .write = codec_debug_write,
  378. };
  379. static const struct file_operations codec_debug_read_ops = {
  380. .open = codec_debug_open,
  381. .read = codec_debug_read,
  382. .write = codec_debug_peek_write,
  383. };
  384. static const struct file_operations codec_debug_dump_ops = {
  385. .open = codec_debug_open,
  386. .read = codec_debug_dump,
  387. };
  388. #endif
  389. static void wsa883x_regcache_sync(struct wsa883x_priv *wsa883x)
  390. {
  391. mutex_lock(&wsa883x->res_lock);
  392. regcache_mark_dirty(wsa883x->regmap);
  393. regcache_sync(wsa883x->regmap);
  394. mutex_unlock(&wsa883x->res_lock);
  395. }
  396. static irqreturn_t wsa883x_saf2war_handle_irq(int irq, void *data)
  397. {
  398. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  399. __func__, irq);
  400. return IRQ_HANDLED;
  401. }
  402. static irqreturn_t wsa883x_war2saf_handle_irq(int irq, void *data)
  403. {
  404. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  405. __func__, irq);
  406. return IRQ_HANDLED;
  407. }
  408. static irqreturn_t wsa883x_otp_handle_irq(int irq, void *data)
  409. {
  410. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  411. __func__, irq);
  412. return IRQ_HANDLED;
  413. }
  414. static irqreturn_t wsa883x_ocp_handle_irq(int irq, void *data)
  415. {
  416. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  417. __func__, irq);
  418. return IRQ_HANDLED;
  419. }
  420. static irqreturn_t wsa883x_clip_handle_irq(int irq, void *data)
  421. {
  422. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  423. __func__, irq);
  424. return IRQ_HANDLED;
  425. }
  426. static irqreturn_t wsa883x_pdm_wd_handle_irq(int irq, void *data)
  427. {
  428. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  429. __func__, irq);
  430. return IRQ_HANDLED;
  431. }
  432. static irqreturn_t wsa883x_clk_wd_handle_irq(int irq, void *data)
  433. {
  434. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  435. __func__, irq);
  436. return IRQ_HANDLED;
  437. }
  438. static irqreturn_t wsa883x_ext_int_handle_irq(int irq, void *data)
  439. {
  440. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  441. __func__, irq);
  442. return IRQ_HANDLED;
  443. }
  444. static irqreturn_t wsa883x_uvlo_handle_irq(int irq, void *data)
  445. {
  446. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  447. __func__, irq);
  448. return IRQ_HANDLED;
  449. }
  450. static irqreturn_t wsa883x_pa_on_err_handle_irq(int irq, void *data)
  451. {
  452. u8 pa_fsm_sta = 0, pa_fsm_err = 0;
  453. struct wsa883x_priv *wsa883x = data;
  454. struct snd_soc_component *component = NULL;
  455. if (!wsa883x)
  456. return IRQ_NONE;
  457. component = wsa883x->component;
  458. if (!component)
  459. return IRQ_NONE;
  460. pa_fsm_sta = (snd_soc_component_read(component, WSA883X_PA_FSM_STA)
  461. & 0x70);
  462. if (pa_fsm_sta)
  463. pa_fsm_err = snd_soc_component_read(component,
  464. WSA883X_PA_FSM_ERR_COND);
  465. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  466. __func__, irq);
  467. snd_soc_component_update_bits(component, WSA883X_PA_FSM_CTL,
  468. 0x10, 0x00);
  469. snd_soc_component_update_bits(component, WSA883X_PA_FSM_CTL,
  470. 0x10, 0x10);
  471. snd_soc_component_update_bits(component, WSA883X_PA_FSM_CTL,
  472. 0x10, 0x00);
  473. return IRQ_HANDLED;
  474. }
  475. static const char * const wsa_dev_mode_text[] = {
  476. "speaker", "receiver", "ultrasound"
  477. };
  478. enum {
  479. SPEAKER,
  480. RECEIVER,
  481. ULTRASOUND,
  482. };
  483. static const struct soc_enum wsa_dev_mode_enum =
  484. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_dev_mode_text), wsa_dev_mode_text);
  485. static int wsa_dev_mode_get(struct snd_kcontrol *kcontrol,
  486. struct snd_ctl_elem_value *ucontrol)
  487. {
  488. struct snd_soc_component *component =
  489. snd_soc_kcontrol_component(kcontrol);
  490. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  491. ucontrol->value.integer.value[0] = wsa883x->dev_mode;
  492. dev_dbg(component->dev, "%s: mode = 0x%x\n", __func__,
  493. wsa883x->dev_mode);
  494. return 0;
  495. }
  496. static int wsa_dev_mode_put(struct snd_kcontrol *kcontrol,
  497. struct snd_ctl_elem_value *ucontrol)
  498. {
  499. struct snd_soc_component *component =
  500. snd_soc_kcontrol_component(kcontrol);
  501. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  502. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  503. __func__, ucontrol->value.integer.value[0]);
  504. wsa883x->dev_mode = ucontrol->value.integer.value[0];
  505. return 0;
  506. }
  507. static const char * const wsa_pa_gain_text[] = {
  508. "G_21_DB", "G_19P5_DB", "G_18_DB", "G_16P5_DB", "G_15_DB", "G_13P5_DB",
  509. "G_12_DB", "G_10P5_DB", "G_9_DB", "G_7P5_DB", "G_6_DB", "G_4P5_DB", "G_3_DB", "G_1P5_DB",
  510. "G_0_DB", "N_1P5_DB", "N_3_DB"
  511. };
  512. static const struct soc_enum wsa_pa_gain_enum =
  513. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_pa_gain_text), wsa_pa_gain_text);
  514. static int wsa_pa_gain_get(struct snd_kcontrol *kcontrol,
  515. struct snd_ctl_elem_value *ucontrol)
  516. {
  517. struct snd_soc_component *component =
  518. snd_soc_kcontrol_component(kcontrol);
  519. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  520. ucontrol->value.integer.value[0] = wsa883x->pa_gain;
  521. dev_dbg(component->dev, "%s: PA gain = 0x%x\n", __func__,
  522. wsa883x->pa_gain);
  523. return 0;
  524. }
  525. static int wsa_pa_gain_put(struct snd_kcontrol *kcontrol,
  526. struct snd_ctl_elem_value *ucontrol)
  527. {
  528. struct snd_soc_component *component =
  529. snd_soc_kcontrol_component(kcontrol);
  530. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  531. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  532. __func__, ucontrol->value.integer.value[0]);
  533. wsa883x->pa_gain = ucontrol->value.integer.value[0];
  534. if (!wsa883x->comp_enable)
  535. snd_soc_component_update_bits(component,
  536. WSA883X_DRE_CTL_1,
  537. 0x3E, wsa883x->pa_gain << 1);
  538. return 0;
  539. }
  540. static int wsa883x_get_mute(struct snd_kcontrol *kcontrol,
  541. struct snd_ctl_elem_value *ucontrol)
  542. {
  543. struct snd_soc_component *component =
  544. snd_soc_kcontrol_component(kcontrol);
  545. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  546. ucontrol->value.integer.value[0] = wsa883x->pa_mute;
  547. return 0;
  548. }
  549. static int wsa883x_set_mute(struct snd_kcontrol *kcontrol,
  550. struct snd_ctl_elem_value *ucontrol)
  551. {
  552. struct snd_soc_component *component =
  553. snd_soc_kcontrol_component(kcontrol);
  554. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  555. int value = ucontrol->value.integer.value[0];
  556. dev_dbg(component->dev, "%s: mute current %d, new %d\n",
  557. __func__, wsa883x->pa_mute, value);
  558. wsa883x->pa_mute = value;
  559. return 0;
  560. }
  561. static int wsa_get_temp(struct snd_kcontrol *kcontrol,
  562. struct snd_ctl_elem_value *ucontrol)
  563. {
  564. struct snd_soc_component *component =
  565. snd_soc_kcontrol_component(kcontrol);
  566. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  567. int temp = 0;
  568. if (test_bit(SPKR_STATUS, &wsa883x->status_mask))
  569. temp = wsa883x->curr_temp;
  570. else
  571. wsa883x_get_temperature(component, &temp);
  572. ucontrol->value.integer.value[0] = temp;
  573. return 0;
  574. }
  575. static ssize_t wsa883x_codec_version_read(struct snd_info_entry *entry,
  576. void *file_private_data, struct file *file,
  577. char __user *buf, size_t count, loff_t pos)
  578. {
  579. struct wsa883x_priv *wsa883x;
  580. char buffer[WSA883X_VERSION_ENTRY_SIZE];
  581. int len = 0;
  582. wsa883x = (struct wsa883x_priv *) entry->private_data;
  583. if (!wsa883x) {
  584. pr_err("%s: wsa883x priv is null\n", __func__);
  585. return -EINVAL;
  586. }
  587. switch (wsa883x->version) {
  588. case WSA883X_VERSION_1_0:
  589. len = snprintf(buffer, sizeof(buffer), "WSA883X_1_0\n");
  590. break;
  591. case WSA883X_VERSION_1_1:
  592. len = snprintf(buffer, sizeof(buffer), "WSA883X_1_1\n");
  593. break;
  594. default:
  595. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  596. break;
  597. }
  598. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  599. }
  600. static struct snd_info_entry_ops wsa883x_codec_info_ops = {
  601. .read = wsa883x_codec_version_read,
  602. };
  603. static ssize_t wsa883x_variant_read(struct snd_info_entry *entry,
  604. void *file_private_data,
  605. struct file *file,
  606. char __user *buf, size_t count,
  607. loff_t pos)
  608. {
  609. struct wsa883x_priv *wsa883x;
  610. char buffer[WSA883X_VARIANT_ENTRY_SIZE];
  611. int len = 0;
  612. wsa883x = (struct wsa883x_priv *) entry->private_data;
  613. if (!wsa883x) {
  614. pr_err("%s: wsa883x priv is null\n", __func__);
  615. return -EINVAL;
  616. }
  617. switch (wsa883x->variant) {
  618. case WSA8830:
  619. len = snprintf(buffer, sizeof(buffer), "WSA8830\n");
  620. break;
  621. case WSA8835:
  622. case WSA8835_V2:
  623. len = snprintf(buffer, sizeof(buffer), "WSA8835\n");
  624. break;
  625. case WSA8832:
  626. len = snprintf(buffer, sizeof(buffer), "WSA8832\n");
  627. break;
  628. default:
  629. len = snprintf(buffer, sizeof(buffer), "UNDEFINED\n");
  630. break;
  631. }
  632. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  633. }
  634. static struct snd_info_entry_ops wsa883x_variant_ops = {
  635. .read = wsa883x_variant_read,
  636. };
  637. /*
  638. * wsa883x_codec_info_create_codec_entry - creates wsa883x module
  639. * @codec_root: The parent directory
  640. * @component: Codec instance
  641. *
  642. * Creates wsa883x module and version entry under the given
  643. * parent directory.
  644. *
  645. * Return: 0 on success or negative error code on failure.
  646. */
  647. int wsa883x_codec_info_create_codec_entry(struct snd_info_entry *codec_root,
  648. struct snd_soc_component *component)
  649. {
  650. struct snd_info_entry *version_entry;
  651. struct snd_info_entry *variant_entry;
  652. struct wsa883x_priv *wsa883x;
  653. struct snd_soc_card *card;
  654. char name[80];
  655. if (!codec_root || !component)
  656. return -EINVAL;
  657. wsa883x = snd_soc_component_get_drvdata(component);
  658. if (wsa883x->entry) {
  659. dev_dbg(wsa883x->dev,
  660. "%s:wsa883x module already created\n", __func__);
  661. return 0;
  662. }
  663. card = component->card;
  664. snprintf(name, sizeof(name), "%s.%llx", "wsa883x",
  665. wsa883x->swr_slave->addr);
  666. wsa883x->entry = snd_info_create_module_entry(codec_root->module,
  667. (const char *)name,
  668. codec_root);
  669. if (!wsa883x->entry) {
  670. dev_dbg(component->dev, "%s: failed to create wsa883x entry\n",
  671. __func__);
  672. return -ENOMEM;
  673. }
  674. wsa883x->entry->mode = S_IFDIR | 0555;
  675. if (snd_info_register(wsa883x->entry) < 0) {
  676. snd_info_free_entry(wsa883x->entry);
  677. return -ENOMEM;
  678. }
  679. version_entry = snd_info_create_card_entry(card->snd_card,
  680. "version",
  681. wsa883x->entry);
  682. if (!version_entry) {
  683. dev_dbg(component->dev, "%s: failed to create wsa883x version entry\n",
  684. __func__);
  685. snd_info_free_entry(wsa883x->entry);
  686. return -ENOMEM;
  687. }
  688. version_entry->private_data = wsa883x;
  689. version_entry->size = WSA883X_VERSION_ENTRY_SIZE;
  690. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  691. version_entry->c.ops = &wsa883x_codec_info_ops;
  692. if (snd_info_register(version_entry) < 0) {
  693. snd_info_free_entry(version_entry);
  694. snd_info_free_entry(wsa883x->entry);
  695. return -ENOMEM;
  696. }
  697. wsa883x->version_entry = version_entry;
  698. variant_entry = snd_info_create_card_entry(card->snd_card,
  699. "variant",
  700. wsa883x->entry);
  701. if (!variant_entry) {
  702. dev_dbg(component->dev,
  703. "%s: failed to create wsa883x variant entry\n",
  704. __func__);
  705. snd_info_free_entry(version_entry);
  706. snd_info_free_entry(wsa883x->entry);
  707. return -ENOMEM;
  708. }
  709. variant_entry->private_data = wsa883x;
  710. variant_entry->size = WSA883X_VARIANT_ENTRY_SIZE;
  711. variant_entry->content = SNDRV_INFO_CONTENT_DATA;
  712. variant_entry->c.ops = &wsa883x_variant_ops;
  713. if (snd_info_register(variant_entry) < 0) {
  714. snd_info_free_entry(variant_entry);
  715. snd_info_free_entry(version_entry);
  716. snd_info_free_entry(wsa883x->entry);
  717. return -ENOMEM;
  718. }
  719. wsa883x->variant_entry = variant_entry;
  720. return 0;
  721. }
  722. EXPORT_SYMBOL(wsa883x_codec_info_create_codec_entry);
  723. /*
  724. * wsa883x_codec_get_dev_num - returns swr device number
  725. * @component: Codec instance
  726. *
  727. * Return: swr device number on success or negative error
  728. * code on failure.
  729. */
  730. int wsa883x_codec_get_dev_num(struct snd_soc_component *component)
  731. {
  732. struct wsa883x_priv *wsa883x;
  733. if (!component)
  734. return -EINVAL;
  735. wsa883x = snd_soc_component_get_drvdata(component);
  736. if (!wsa883x) {
  737. pr_err("%s: wsa883x component is NULL\n", __func__);
  738. return -EINVAL;
  739. }
  740. return wsa883x->swr_slave->dev_num;
  741. }
  742. EXPORT_SYMBOL(wsa883x_codec_get_dev_num);
  743. static int wsa883x_get_dev_num(struct snd_kcontrol *kcontrol,
  744. struct snd_ctl_elem_value *ucontrol)
  745. {
  746. struct snd_soc_component *component =
  747. snd_soc_kcontrol_component(kcontrol);
  748. struct wsa883x_priv *wsa883x;
  749. if (!component)
  750. return -EINVAL;
  751. wsa883x = snd_soc_component_get_drvdata(component);
  752. if (!wsa883x) {
  753. pr_err("%s: wsa883x component is NULL\n", __func__);
  754. return -EINVAL;
  755. }
  756. ucontrol->value.integer.value[0] = wsa883x->swr_slave->dev_num;
  757. return 0;
  758. }
  759. static int wsa883x_get_compander(struct snd_kcontrol *kcontrol,
  760. struct snd_ctl_elem_value *ucontrol)
  761. {
  762. struct snd_soc_component *component =
  763. snd_soc_kcontrol_component(kcontrol);
  764. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  765. ucontrol->value.integer.value[0] = wsa883x->comp_enable;
  766. return 0;
  767. }
  768. static int wsa883x_set_compander(struct snd_kcontrol *kcontrol,
  769. struct snd_ctl_elem_value *ucontrol)
  770. {
  771. struct snd_soc_component *component =
  772. snd_soc_kcontrol_component(kcontrol);
  773. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  774. int value = ucontrol->value.integer.value[0];
  775. dev_dbg(component->dev, "%s: Compander enable current %d, new %d\n",
  776. __func__, wsa883x->comp_enable, value);
  777. wsa883x->comp_enable = value;
  778. return 0;
  779. }
  780. static int wsa883x_get_comp_offset(struct snd_kcontrol *kcontrol,
  781. struct snd_ctl_elem_value *ucontrol)
  782. {
  783. struct snd_soc_component *component =
  784. snd_soc_kcontrol_component(kcontrol);
  785. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  786. ucontrol->value.integer.value[0] = wsa883x->comp_offset;
  787. return 0;
  788. }
  789. static int wsa883x_set_comp_offset(struct snd_kcontrol *kcontrol,
  790. struct snd_ctl_elem_value *ucontrol)
  791. {
  792. struct snd_soc_component *component =
  793. snd_soc_kcontrol_component(kcontrol);
  794. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  795. int value = ucontrol->value.integer.value[0];
  796. dev_dbg(component->dev, "%s: comp_offset %d\n",
  797. __func__, wsa883x->comp_offset);
  798. wsa883x->comp_offset = value;
  799. return 0;
  800. }
  801. static int wsa883x_get_visense(struct snd_kcontrol *kcontrol,
  802. struct snd_ctl_elem_value *ucontrol)
  803. {
  804. struct snd_soc_component *component =
  805. snd_soc_kcontrol_component(kcontrol);
  806. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  807. ucontrol->value.integer.value[0] = wsa883x->visense_enable;
  808. return 0;
  809. }
  810. static int wsa883x_set_visense(struct snd_kcontrol *kcontrol,
  811. struct snd_ctl_elem_value *ucontrol)
  812. {
  813. struct snd_soc_component *component =
  814. snd_soc_kcontrol_component(kcontrol);
  815. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  816. int value = ucontrol->value.integer.value[0];
  817. dev_dbg(component->dev, "%s: VIsense enable current %d, new %d\n",
  818. __func__, wsa883x->visense_enable, value);
  819. wsa883x->visense_enable = value;
  820. return 0;
  821. }
  822. static int wsa883x_get_ext_vdd_spk(struct snd_kcontrol *kcontrol,
  823. struct snd_ctl_elem_value *ucontrol)
  824. {
  825. struct snd_soc_component *component =
  826. snd_soc_kcontrol_component(kcontrol);
  827. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  828. ucontrol->value.integer.value[0] = wsa883x->ext_vdd_spk;
  829. return 0;
  830. }
  831. static int wsa883x_put_ext_vdd_spk(struct snd_kcontrol *kcontrol,
  832. struct snd_ctl_elem_value *ucontrol)
  833. {
  834. struct snd_soc_component *component =
  835. snd_soc_kcontrol_component(kcontrol);
  836. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  837. int value = ucontrol->value.integer.value[0];
  838. dev_dbg(component->dev, "%s: Ext VDD SPK enable current %d, new %d\n",
  839. __func__, wsa883x->ext_vdd_spk, value);
  840. wsa883x->ext_vdd_spk = value;
  841. return 0;
  842. }
  843. static const struct snd_kcontrol_new wsa883x_snd_controls[] = {
  844. SOC_ENUM_EXT("WSA PA Gain", wsa_pa_gain_enum,
  845. wsa_pa_gain_get, wsa_pa_gain_put),
  846. SOC_SINGLE_EXT("WSA PA Mute", SND_SOC_NOPM, 0, 1, 0,
  847. wsa883x_get_mute, wsa883x_set_mute),
  848. SOC_SINGLE_EXT("WSA Temp", SND_SOC_NOPM, 0, UINT_MAX, 0,
  849. wsa_get_temp, NULL),
  850. SOC_SINGLE_EXT("WSA Get DevNum", SND_SOC_NOPM, 0, UINT_MAX, 0,
  851. wsa883x_get_dev_num, NULL),
  852. SOC_ENUM_EXT("WSA MODE", wsa_dev_mode_enum,
  853. wsa_dev_mode_get, wsa_dev_mode_put),
  854. SOC_SINGLE_EXT("COMP Offset", SND_SOC_NOPM, 0, 4, 0,
  855. wsa883x_get_comp_offset, wsa883x_set_comp_offset),
  856. SOC_SINGLE_EXT("COMP Switch", SND_SOC_NOPM, 0, 1, 0,
  857. wsa883x_get_compander, wsa883x_set_compander),
  858. SOC_SINGLE_EXT("VISENSE Switch", SND_SOC_NOPM, 0, 1, 0,
  859. wsa883x_get_visense, wsa883x_set_visense),
  860. SOC_SINGLE_EXT("External VDD_SPK", SND_SOC_NOPM, 0, 1, 0,
  861. wsa883x_get_ext_vdd_spk, wsa883x_put_ext_vdd_spk),
  862. };
  863. static const struct snd_kcontrol_new swr_dac_port[] = {
  864. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  865. };
  866. static int wsa883x_set_port(struct snd_soc_component *component, int port_idx,
  867. u8 *port_id, u8 *num_ch, u8 *ch_mask, u32 *ch_rate,
  868. u8 *port_type)
  869. {
  870. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  871. *port_id = wsa883x->port[port_idx].port_id;
  872. *num_ch = wsa883x->port[port_idx].num_ch;
  873. *ch_mask = wsa883x->port[port_idx].ch_mask;
  874. *ch_rate = wsa883x->port[port_idx].ch_rate;
  875. *port_type = wsa883x->port[port_idx].port_type;
  876. return 0;
  877. }
  878. static int wsa883x_enable_swr_dac_port(struct snd_soc_dapm_widget *w,
  879. struct snd_kcontrol *kcontrol, int event)
  880. {
  881. struct snd_soc_component *component =
  882. snd_soc_dapm_to_component(w->dapm);
  883. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  884. u8 port_id[WSA883X_MAX_SWR_PORTS];
  885. u8 num_ch[WSA883X_MAX_SWR_PORTS];
  886. u8 ch_mask[WSA883X_MAX_SWR_PORTS];
  887. u32 ch_rate[WSA883X_MAX_SWR_PORTS];
  888. u8 port_type[WSA883X_MAX_SWR_PORTS];
  889. u8 num_port = 0;
  890. dev_dbg(component->dev, "%s: event %d name %s\n", __func__,
  891. event, w->name);
  892. if (wsa883x == NULL)
  893. return -EINVAL;
  894. switch (event) {
  895. case SND_SOC_DAPM_PRE_PMU:
  896. wsa883x_set_port(component, SWR_DAC_PORT,
  897. &port_id[num_port], &num_ch[num_port],
  898. &ch_mask[num_port], &ch_rate[num_port],
  899. &port_type[num_port]);
  900. if (wsa883x->dev_mode == RECEIVER)
  901. ch_rate[num_port] = SWR_CLK_RATE_4P8MHZ;
  902. ++num_port;
  903. if (wsa883x->comp_enable) {
  904. wsa883x_set_port(component, SWR_COMP_PORT,
  905. &port_id[num_port], &num_ch[num_port],
  906. &ch_mask[num_port], &ch_rate[num_port],
  907. &port_type[num_port]);
  908. ++num_port;
  909. }
  910. if (wsa883x->visense_enable) {
  911. wsa883x_set_port(component, SWR_VISENSE_PORT,
  912. &port_id[num_port], &num_ch[num_port],
  913. &ch_mask[num_port], &ch_rate[num_port],
  914. &port_type[num_port]);
  915. ++num_port;
  916. }
  917. swr_connect_port(wsa883x->swr_slave, &port_id[0], num_port,
  918. &ch_mask[0], &ch_rate[0], &num_ch[0],
  919. &port_type[0]);
  920. break;
  921. case SND_SOC_DAPM_POST_PMU:
  922. set_bit(SPKR_STATUS, &wsa883x->status_mask);
  923. break;
  924. case SND_SOC_DAPM_PRE_PMD:
  925. wsa883x_set_port(component, SWR_DAC_PORT,
  926. &port_id[num_port], &num_ch[num_port],
  927. &ch_mask[num_port], &ch_rate[num_port],
  928. &port_type[num_port]);
  929. ++num_port;
  930. if (wsa883x->comp_enable) {
  931. wsa883x_set_port(component, SWR_COMP_PORT,
  932. &port_id[num_port], &num_ch[num_port],
  933. &ch_mask[num_port], &ch_rate[num_port],
  934. &port_type[num_port]);
  935. ++num_port;
  936. }
  937. if (wsa883x->visense_enable) {
  938. wsa883x_set_port(component, SWR_VISENSE_PORT,
  939. &port_id[num_port], &num_ch[num_port],
  940. &ch_mask[num_port], &ch_rate[num_port],
  941. &port_type[num_port]);
  942. ++num_port;
  943. }
  944. swr_disconnect_port(wsa883x->swr_slave, &port_id[0], num_port,
  945. &ch_mask[0], &port_type[0]);
  946. break;
  947. case SND_SOC_DAPM_POST_PMD:
  948. if (swr_set_device_group(wsa883x->swr_slave, SWR_GROUP_NONE))
  949. dev_err(component->dev,
  950. "%s: set num ch failed\n", __func__);
  951. swr_slvdev_datapath_control(wsa883x->swr_slave,
  952. wsa883x->swr_slave->dev_num,
  953. false);
  954. break;
  955. default:
  956. break;
  957. }
  958. return 0;
  959. }
  960. static int wsa883x_spkr_event(struct snd_soc_dapm_widget *w,
  961. struct snd_kcontrol *kcontrol, int event)
  962. {
  963. struct snd_soc_component *component =
  964. snd_soc_dapm_to_component(w->dapm);
  965. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  966. dev_dbg(component->dev, "%s: %s %d\n", __func__, w->name, event);
  967. switch (event) {
  968. case SND_SOC_DAPM_POST_PMU:
  969. if (wsa883x->dev_mode == RECEIVER) {
  970. snd_soc_component_update_bits(component,
  971. WSA883X_CDC_PATH_MODE,
  972. 0x02, 0x02);
  973. snd_soc_component_update_bits(component,
  974. WSA883X_SPKR_PWM_CLK_CTL,
  975. 0x08, 0x08);
  976. snd_soc_component_update_bits(component,
  977. WSA883X_DRE_CTL_0,
  978. 0xF0, 0x00);
  979. } else if (wsa883x->dev_mode == SPEAKER) {
  980. snd_soc_component_update_bits(component,
  981. WSA883X_CDC_PATH_MODE,
  982. 0x02, 0x00);
  983. snd_soc_component_update_bits(component,
  984. WSA883X_SPKR_PWM_CLK_CTL,
  985. 0x08, 0x00);
  986. snd_soc_component_update_bits(component,
  987. WSA883X_DRE_CTL_0,
  988. 0xF0, 0x90);
  989. }
  990. swr_slvdev_datapath_control(wsa883x->swr_slave,
  991. wsa883x->swr_slave->dev_num,
  992. true);
  993. /* Added delay as per HW sequence */
  994. usleep_range(250, 300);
  995. snd_soc_component_update_bits(component,
  996. WSA883X_DRE_CTL_1,
  997. 0x01, 0x01);
  998. /* Added delay as per HW sequence */
  999. usleep_range(250, 300);
  1000. /* Force remove group */
  1001. swr_remove_from_group(wsa883x->swr_slave,
  1002. wsa883x->swr_slave->dev_num);
  1003. if (wsa883x->comp_enable)
  1004. snd_soc_component_update_bits(component,
  1005. WSA883X_DRE_CTL_0,
  1006. 0x07,
  1007. wsa883x->comp_offset);
  1008. wcd_enable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PA_ON_ERR);
  1009. wcd_enable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_UVLO);
  1010. wcd_enable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_SAF2WAR);
  1011. wcd_enable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_WAR2SAF);
  1012. wcd_enable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_DISABLE);
  1013. wcd_enable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_OCP);
  1014. wcd_enable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLK_WD);
  1015. snd_soc_component_update_bits(component,
  1016. WSA883X_VBAT_ADC_FLT_CTL,
  1017. 0x0E, 0x06);
  1018. snd_soc_component_update_bits(component,
  1019. WSA883X_VBAT_ADC_FLT_CTL,
  1020. 0x01, 0x01);
  1021. if (test_bit(SPKR_ADIE_LB, &wsa883x->status_mask))
  1022. snd_soc_component_update_bits(component,
  1023. WSA883X_PA_FSM_CTL, 0x01, 0x01);
  1024. break;
  1025. case SND_SOC_DAPM_PRE_PMD:
  1026. if (!test_bit(SPKR_ADIE_LB, &wsa883x->status_mask))
  1027. wcd_disable_irq(&wsa883x->irq_info,
  1028. WSA883X_IRQ_INT_PDM_WD);
  1029. snd_soc_component_update_bits(component,
  1030. WSA883X_VBAT_ADC_FLT_CTL,
  1031. 0x01, 0x00);
  1032. snd_soc_component_update_bits(component,
  1033. WSA883X_VBAT_ADC_FLT_CTL,
  1034. 0x0E, 0x00);
  1035. snd_soc_component_update_bits(component, WSA883X_PA_FSM_CTL,
  1036. 0x01, 0x00);
  1037. snd_soc_component_update_bits(component, WSA883X_PA_FSM_CTL,
  1038. 0x10, 0x00);
  1039. snd_soc_component_update_bits(component, WSA883X_PA_FSM_CTL,
  1040. 0x10, 0x10);
  1041. snd_soc_component_update_bits(component, WSA883X_PA_FSM_CTL,
  1042. 0x10, 0x00);
  1043. snd_soc_component_update_bits(component, WSA883X_PDM_WD_CTL,
  1044. 0x01, 0x00);
  1045. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLK_WD);
  1046. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_OCP);
  1047. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_DISABLE);
  1048. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_WAR2SAF);
  1049. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_SAF2WAR);
  1050. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_UVLO);
  1051. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PA_ON_ERR);
  1052. clear_bit(SPKR_STATUS, &wsa883x->status_mask);
  1053. clear_bit(SPKR_ADIE_LB, &wsa883x->status_mask);
  1054. break;
  1055. }
  1056. return 0;
  1057. }
  1058. static const struct snd_soc_dapm_widget wsa883x_dapm_widgets[] = {
  1059. SND_SOC_DAPM_INPUT("IN"),
  1060. SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0, swr_dac_port,
  1061. ARRAY_SIZE(swr_dac_port), wsa883x_enable_swr_dac_port,
  1062. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1063. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1064. SND_SOC_DAPM_SPK("SPKR", wsa883x_spkr_event),
  1065. };
  1066. static const struct snd_soc_dapm_route wsa883x_audio_map[] = {
  1067. {"SWR DAC_Port", "Switch", "IN"},
  1068. {"SPKR", NULL, "SWR DAC_Port"},
  1069. };
  1070. int wsa883x_set_channel_map(struct snd_soc_component *component, u8 *port,
  1071. u8 num_port, unsigned int *ch_mask,
  1072. unsigned int *ch_rate, u8 *port_type)
  1073. {
  1074. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  1075. int i;
  1076. if (!port || !ch_mask || !ch_rate ||
  1077. (num_port > WSA883X_MAX_SWR_PORTS)) {
  1078. dev_err(component->dev,
  1079. "%s: Invalid port=%pK, ch_mask=%pK, ch_rate=%pK\n",
  1080. __func__, port, ch_mask, ch_rate);
  1081. return -EINVAL;
  1082. }
  1083. for (i = 0; i < num_port; i++) {
  1084. wsa883x->port[i].port_id = port[i];
  1085. wsa883x->port[i].ch_mask = ch_mask[i];
  1086. wsa883x->port[i].ch_rate = ch_rate[i];
  1087. wsa883x->port[i].num_ch = __sw_hweight8(ch_mask[i]);
  1088. if (port_type)
  1089. wsa883x->port[i].port_type = port_type[i];
  1090. }
  1091. return 0;
  1092. }
  1093. EXPORT_SYMBOL(wsa883x_set_channel_map);
  1094. static void wsa883x_codec_init(struct snd_soc_component *component)
  1095. {
  1096. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  1097. int i;
  1098. if (!wsa883x)
  1099. return;
  1100. for (i = 0; i < ARRAY_SIZE(reg_init); i++)
  1101. snd_soc_component_update_bits(component, reg_init[i].reg,
  1102. reg_init[i].mask, reg_init[i].val);
  1103. if (wsa883x->variant == WSA8830 || wsa883x->variant == WSA8832) {
  1104. snd_soc_component_update_bits(component, WSA883X_DRE_CTL_0,
  1105. 0x07, 0x03);
  1106. wsa883x->comp_offset = COMP_OFFSET3;
  1107. }
  1108. }
  1109. static int32_t wsa883x_temp_reg_read(struct snd_soc_component *component,
  1110. struct wsa_temp_register *wsa_temp_reg)
  1111. {
  1112. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  1113. if (!wsa883x) {
  1114. dev_err(component->dev, "%s: wsa883x is NULL\n", __func__);
  1115. return -EINVAL;
  1116. }
  1117. mutex_lock(&wsa883x->res_lock);
  1118. snd_soc_component_update_bits(component, WSA883X_PA_FSM_BYP,
  1119. 0x01, 0x01);
  1120. snd_soc_component_update_bits(component, WSA883X_PA_FSM_BYP,
  1121. 0x04, 0x04);
  1122. snd_soc_component_update_bits(component, WSA883X_PA_FSM_BYP,
  1123. 0x02, 0x02);
  1124. snd_soc_component_update_bits(component, WSA883X_PA_FSM_BYP,
  1125. 0x80, 0x80);
  1126. snd_soc_component_update_bits(component, WSA883X_PA_FSM_BYP,
  1127. 0x20, 0x20);
  1128. snd_soc_component_update_bits(component, WSA883X_PA_FSM_BYP,
  1129. 0x40, 0x40);
  1130. snd_soc_component_update_bits(component, WSA883X_TADC_VALUE_CTL,
  1131. 0x01, 0x00);
  1132. wsa_temp_reg->dmeas_msb = snd_soc_component_read(
  1133. component, WSA883X_TEMP_MSB);
  1134. wsa_temp_reg->dmeas_lsb = snd_soc_component_read(
  1135. component, WSA883X_TEMP_LSB);
  1136. snd_soc_component_update_bits(component, WSA883X_TADC_VALUE_CTL,
  1137. 0x01, 0x01);
  1138. wsa_temp_reg->d1_msb = snd_soc_component_read(
  1139. component, WSA883X_OTP_REG_1);
  1140. wsa_temp_reg->d1_lsb = snd_soc_component_read(
  1141. component, WSA883X_OTP_REG_2);
  1142. wsa_temp_reg->d2_msb = snd_soc_component_read(
  1143. component, WSA883X_OTP_REG_3);
  1144. wsa_temp_reg->d2_lsb = snd_soc_component_read(
  1145. component, WSA883X_OTP_REG_4);
  1146. snd_soc_component_update_bits(component, WSA883X_PA_FSM_BYP,
  1147. 0xE7, 0x00);
  1148. mutex_unlock(&wsa883x->res_lock);
  1149. return 0;
  1150. }
  1151. static int wsa883x_get_temperature(struct snd_soc_component *component,
  1152. int *temp)
  1153. {
  1154. struct wsa_temp_register reg;
  1155. int dmeas, d1, d2;
  1156. int ret = 0;
  1157. int temp_val = 0;
  1158. int t1 = T1_TEMP;
  1159. int t2 = T2_TEMP;
  1160. u8 retry = WSA883X_TEMP_RETRY;
  1161. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  1162. if (!wsa883x)
  1163. return -EINVAL;
  1164. do {
  1165. ret = wsa883x_temp_reg_read(component, &reg);
  1166. if (ret) {
  1167. pr_err("%s: temp read failed: %d, current temp: %d\n",
  1168. __func__, ret, wsa883x->curr_temp);
  1169. if (temp)
  1170. *temp = wsa883x->curr_temp;
  1171. return 0;
  1172. }
  1173. /*
  1174. * Temperature register values are expected to be in the
  1175. * following range.
  1176. * d1_msb = 68 - 92 and d1_lsb = 0, 64, 128, 192
  1177. * d2_msb = 185 -218 and d2_lsb = 0, 64, 128, 192
  1178. */
  1179. if ((reg.d1_msb < 68 || reg.d1_msb > 92) ||
  1180. (!(reg.d1_lsb == 0 || reg.d1_lsb == 64 || reg.d1_lsb == 128 ||
  1181. reg.d1_lsb == 192)) ||
  1182. (reg.d2_msb < 185 || reg.d2_msb > 218) ||
  1183. (!(reg.d2_lsb == 0 || reg.d2_lsb == 64 || reg.d2_lsb == 128 ||
  1184. reg.d2_lsb == 192))) {
  1185. printk_ratelimited("%s: Temperature registers[%d %d %d %d] are out of range\n",
  1186. __func__, reg.d1_msb, reg.d1_lsb, reg.d2_msb,
  1187. reg.d2_lsb);
  1188. }
  1189. dmeas = ((reg.dmeas_msb << 0x8) | reg.dmeas_lsb) >> 0x6;
  1190. d1 = ((reg.d1_msb << 0x8) | reg.d1_lsb) >> 0x6;
  1191. d2 = ((reg.d2_msb << 0x8) | reg.d2_lsb) >> 0x6;
  1192. if (d1 == d2)
  1193. temp_val = TEMP_INVALID;
  1194. else
  1195. temp_val = t1 + (((dmeas - d1) * (t2 - t1))/(d2 - d1));
  1196. if (temp_val <= LOW_TEMP_THRESHOLD ||
  1197. temp_val >= HIGH_TEMP_THRESHOLD) {
  1198. pr_debug("%s: T0: %d is out of range[%d, %d]\n", __func__,
  1199. temp_val, LOW_TEMP_THRESHOLD, HIGH_TEMP_THRESHOLD);
  1200. if (retry--)
  1201. msleep(10);
  1202. } else {
  1203. break;
  1204. }
  1205. } while (retry);
  1206. wsa883x->curr_temp = temp_val;
  1207. if (temp)
  1208. *temp = temp_val;
  1209. pr_debug("%s: t0 measured: %d dmeas = %d, d1 = %d, d2 = %d\n",
  1210. __func__, temp_val, dmeas, d1, d2);
  1211. return ret;
  1212. }
  1213. static int wsa883x_codec_probe(struct snd_soc_component *component)
  1214. {
  1215. char w_name[MAX_NAME_LEN];
  1216. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  1217. struct swr_device *dev;
  1218. int variant = 0, version = 0;
  1219. struct snd_soc_dapm_context *dapm =
  1220. snd_soc_component_get_dapm(component);
  1221. if (!wsa883x)
  1222. return -EINVAL;
  1223. if (!component->name_prefix)
  1224. return -EINVAL;
  1225. snd_soc_component_init_regmap(component, wsa883x->regmap);
  1226. dev = wsa883x->swr_slave;
  1227. wsa883x->component = component;
  1228. variant = (snd_soc_component_read(component, WSA883X_OTP_REG_0)
  1229. & 0x0F);
  1230. wsa883x->variant = variant;
  1231. version = (snd_soc_component_read(component, WSA883X_CHIP_ID0)
  1232. & 0xFF);
  1233. wsa883x->version = version;
  1234. wsa883x->comp_offset = COMP_OFFSET2;
  1235. wsa883x_codec_init(component);
  1236. wsa883x->global_pa_cnt = 0;
  1237. memset(w_name, 0, sizeof(w_name));
  1238. strlcpy(w_name, wsa883x->dai_driver->playback.stream_name,
  1239. sizeof(w_name));
  1240. snd_soc_dapm_ignore_suspend(dapm, w_name);
  1241. memset(w_name, 0, sizeof(w_name));
  1242. strlcpy(w_name, "IN", sizeof(w_name));
  1243. snd_soc_dapm_ignore_suspend(dapm, w_name);
  1244. memset(w_name, 0, sizeof(w_name));
  1245. strlcpy(w_name, "SWR DAC_Port", sizeof(w_name));
  1246. snd_soc_dapm_ignore_suspend(dapm, w_name);
  1247. memset(w_name, 0, sizeof(w_name));
  1248. strlcpy(w_name, "SPKR", sizeof(w_name));
  1249. snd_soc_dapm_ignore_suspend(dapm, w_name);
  1250. snd_soc_dapm_sync(dapm);
  1251. return 0;
  1252. }
  1253. static void wsa883x_codec_remove(struct snd_soc_component *component)
  1254. {
  1255. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  1256. if (!wsa883x)
  1257. return;
  1258. snd_soc_component_exit_regmap(component);
  1259. return;
  1260. }
  1261. static int wsa883x_soc_codec_suspend(struct snd_soc_component *component)
  1262. {
  1263. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  1264. if (!wsa883x)
  1265. return 0;
  1266. wsa883x->dapm_bias_off = true;
  1267. return 0;
  1268. }
  1269. static int wsa883x_soc_codec_resume(struct snd_soc_component *component)
  1270. {
  1271. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  1272. if (!wsa883x)
  1273. return 0;
  1274. wsa883x->dapm_bias_off = false;
  1275. return 0;
  1276. }
  1277. static const struct snd_soc_component_driver soc_codec_dev_wsa883x_wsa = {
  1278. .name = "",
  1279. .probe = wsa883x_codec_probe,
  1280. .remove = wsa883x_codec_remove,
  1281. .controls = wsa883x_snd_controls,
  1282. .num_controls = ARRAY_SIZE(wsa883x_snd_controls),
  1283. .dapm_widgets = wsa883x_dapm_widgets,
  1284. .num_dapm_widgets = ARRAY_SIZE(wsa883x_dapm_widgets),
  1285. .dapm_routes = wsa883x_audio_map,
  1286. .num_dapm_routes = ARRAY_SIZE(wsa883x_audio_map),
  1287. .suspend = wsa883x_soc_codec_suspend,
  1288. .resume = wsa883x_soc_codec_resume,
  1289. };
  1290. static int wsa883x_gpio_ctrl(struct wsa883x_priv *wsa883x, bool enable)
  1291. {
  1292. int ret = 0;
  1293. if (enable)
  1294. ret = msm_cdc_pinctrl_select_active_state(
  1295. wsa883x->wsa_rst_np);
  1296. else
  1297. ret = msm_cdc_pinctrl_select_sleep_state(
  1298. wsa883x->wsa_rst_np);
  1299. if (ret != 0)
  1300. dev_err(wsa883x->dev,
  1301. "%s: Failed to turn state %d; ret=%d\n",
  1302. __func__, enable, ret);
  1303. return ret;
  1304. }
  1305. static int wsa883x_swr_up(struct wsa883x_priv *wsa883x)
  1306. {
  1307. int ret;
  1308. ret = wsa883x_gpio_ctrl(wsa883x, true);
  1309. if (ret)
  1310. dev_err(wsa883x->dev, "%s: Failed to enable gpio\n", __func__);
  1311. return ret;
  1312. }
  1313. static int wsa883x_swr_down(struct wsa883x_priv *wsa883x)
  1314. {
  1315. int ret;
  1316. ret = wsa883x_gpio_ctrl(wsa883x, false);
  1317. if (ret)
  1318. dev_err(wsa883x->dev, "%s: Failed to disable gpio\n", __func__);
  1319. return ret;
  1320. }
  1321. static int wsa883x_swr_reset(struct wsa883x_priv *wsa883x)
  1322. {
  1323. u8 retry = WSA883X_NUM_RETRY;
  1324. u8 devnum = 0;
  1325. struct swr_device *pdev;
  1326. pdev = wsa883x->swr_slave;
  1327. while (swr_get_logical_dev_num(pdev, pdev->addr, &devnum) && retry--) {
  1328. /* Retry after 1 msec delay */
  1329. usleep_range(1000, 1100);
  1330. }
  1331. pdev->dev_num = devnum;
  1332. wsa883x_regcache_sync(wsa883x);
  1333. return 0;
  1334. }
  1335. static int wsa883x_event_notify(struct notifier_block *nb,
  1336. unsigned long val, void *ptr)
  1337. {
  1338. u16 event = (val & 0xffff);
  1339. struct wsa883x_priv *wsa883x = container_of(nb, struct wsa883x_priv,
  1340. parent_nblock);
  1341. if (!wsa883x)
  1342. return -EINVAL;
  1343. switch (event) {
  1344. case BOLERO_SLV_EVT_PA_OFF_PRE_SSR:
  1345. if (test_bit(SPKR_STATUS, &wsa883x->status_mask))
  1346. snd_soc_component_update_bits(wsa883x->component,
  1347. WSA883X_PA_FSM_CTL,
  1348. 0x01, 0x00);
  1349. wsa883x_swr_down(wsa883x);
  1350. break;
  1351. case BOLERO_SLV_EVT_SSR_UP:
  1352. wsa883x_swr_up(wsa883x);
  1353. /* Add delay to allow enumerate */
  1354. usleep_range(20000, 20010);
  1355. wsa883x_swr_reset(wsa883x);
  1356. break;
  1357. case BOLERO_SLV_EVT_PA_ON_POST_FSCLK:
  1358. if (test_bit(SPKR_STATUS, &wsa883x->status_mask)) {
  1359. snd_soc_component_update_bits(wsa883x->component,
  1360. WSA883X_PDM_WD_CTL,
  1361. 0x01, 0x01);
  1362. snd_soc_component_update_bits(wsa883x->component,
  1363. WSA883X_PA_FSM_CTL,
  1364. 0x01, 0x01);
  1365. wcd_enable_irq(&wsa883x->irq_info,
  1366. WSA883X_IRQ_INT_PDM_WD);
  1367. /* Added delay as per HW sequence */
  1368. usleep_range(3000, 3100);
  1369. if (wsa883x->comp_enable) {
  1370. snd_soc_component_update_bits(wsa883x->component,
  1371. WSA883X_DRE_CTL_1,
  1372. 0x01, 0x00);
  1373. /* Added delay as per HW sequence */
  1374. usleep_range(5000, 5050);
  1375. }
  1376. }
  1377. break;
  1378. case BOLERO_SLV_EVT_PA_ON_POST_FSCLK_ADIE_LB:
  1379. if (test_bit(SPKR_STATUS, &wsa883x->status_mask))
  1380. set_bit(SPKR_ADIE_LB, &wsa883x->status_mask);
  1381. break;
  1382. default:
  1383. dev_dbg(wsa883x->dev, "%s: unknown event %d\n",
  1384. __func__, event);
  1385. break;
  1386. }
  1387. return 0;
  1388. }
  1389. static int wsa883x_enable_supplies(struct device * dev,
  1390. struct wsa883x_priv *priv)
  1391. {
  1392. int ret = 0;
  1393. /* Parse power supplies */
  1394. msm_cdc_get_power_supplies(dev, &priv->regulator,
  1395. &priv->num_supplies);
  1396. if (!priv->regulator || (priv->num_supplies <= 0)) {
  1397. dev_err(dev, "%s: no power supplies defined\n", __func__);
  1398. return -EINVAL;
  1399. }
  1400. ret = msm_cdc_init_supplies(dev, &priv->supplies,
  1401. priv->regulator, priv->num_supplies);
  1402. if (!priv->supplies) {
  1403. dev_err(dev, "%s: Cannot init wsa supplies\n",
  1404. __func__);
  1405. return ret;
  1406. }
  1407. ret = msm_cdc_enable_static_supplies(dev, priv->supplies,
  1408. priv->regulator,
  1409. priv->num_supplies);
  1410. if (ret)
  1411. dev_err(dev, "%s: wsa static supply enable failed!\n",
  1412. __func__);
  1413. return ret;
  1414. }
  1415. static struct snd_soc_dai_driver wsa_dai[] = {
  1416. {
  1417. .name = "",
  1418. .playback = {
  1419. .stream_name = "",
  1420. .rates = WSA883X_RATES | WSA883X_FRAC_RATES,
  1421. .formats = WSA883X_FORMATS,
  1422. .rate_max = 192000,
  1423. .rate_min = 8000,
  1424. .channels_min = 1,
  1425. .channels_max = 2,
  1426. },
  1427. },
  1428. };
  1429. static int wsa883x_swr_probe(struct swr_device *pdev)
  1430. {
  1431. int ret = 0, i = 0;
  1432. struct wsa883x_priv *wsa883x;
  1433. u8 devnum = 0;
  1434. bool pin_state_current = false;
  1435. struct wsa_ctrl_platform_data *plat_data = NULL;
  1436. struct snd_soc_component *component;
  1437. char buffer[MAX_NAME_LEN];
  1438. int dev_index = 0;
  1439. struct regmap_irq_chip *wsa883x_sub_regmap_irq_chip = NULL;
  1440. wsa883x = devm_kzalloc(&pdev->dev, sizeof(struct wsa883x_priv),
  1441. GFP_KERNEL);
  1442. if (!wsa883x)
  1443. return -ENOMEM;
  1444. wsa883x_sub_regmap_irq_chip = devm_kzalloc(&pdev->dev, sizeof(struct regmap_irq_chip),
  1445. GFP_KERNEL);
  1446. if (!wsa883x_sub_regmap_irq_chip)
  1447. return -ENOMEM;
  1448. memcpy(wsa883x_sub_regmap_irq_chip, &wsa883x_regmap_irq_chip,
  1449. sizeof(struct regmap_irq_chip));
  1450. ret = wsa883x_enable_supplies(&pdev->dev, wsa883x);
  1451. if (ret) {
  1452. ret = -EPROBE_DEFER;
  1453. goto err;
  1454. }
  1455. wsa883x->wsa_rst_np = of_parse_phandle(pdev->dev.of_node,
  1456. "qcom,spkr-sd-n-node", 0);
  1457. if (!wsa883x->wsa_rst_np) {
  1458. dev_dbg(&pdev->dev, "%s: pinctrl not defined\n", __func__);
  1459. goto err_supply;
  1460. }
  1461. swr_set_dev_data(pdev, wsa883x);
  1462. wsa883x->swr_slave = pdev;
  1463. wsa883x->dev = &pdev->dev;
  1464. pin_state_current = msm_cdc_pinctrl_get_state(wsa883x->wsa_rst_np);
  1465. wsa883x_gpio_ctrl(wsa883x, true);
  1466. /*
  1467. * Add 5msec delay to provide sufficient time for
  1468. * soundwire auto enumeration of slave devices as
  1469. * as per HW requirement.
  1470. */
  1471. usleep_range(5000, 5010);
  1472. ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
  1473. if (ret) {
  1474. dev_dbg(&pdev->dev,
  1475. "%s get devnum %d for dev addr %lx failed\n",
  1476. __func__, devnum, pdev->addr);
  1477. ret = -EPROBE_DEFER;
  1478. goto err_supply;
  1479. }
  1480. pdev->dev_num = devnum;
  1481. wsa883x->regmap = devm_regmap_init_swr(pdev,
  1482. &wsa883x_regmap_config);
  1483. if (IS_ERR(wsa883x->regmap)) {
  1484. ret = PTR_ERR(wsa883x->regmap);
  1485. dev_err(&pdev->dev, "%s: regmap_init failed %d\n",
  1486. __func__, ret);
  1487. goto dev_err;
  1488. }
  1489. devm_regmap_qti_debugfs_register(&pdev->dev, wsa883x->regmap);
  1490. /* Set all interrupts as edge triggered */
  1491. for (i = 0; i < wsa883x_sub_regmap_irq_chip->num_regs; i++)
  1492. regmap_write(wsa883x->regmap, (WSA883X_INTR_LEVEL0 + i), 0);
  1493. wsa883x_sub_regmap_irq_chip->irq_drv_data = wsa883x;
  1494. wsa883x->irq_info.wcd_regmap_irq_chip = wsa883x_sub_regmap_irq_chip;
  1495. wsa883x->irq_info.codec_name = "WSA883X";
  1496. wsa883x->irq_info.regmap = wsa883x->regmap;
  1497. wsa883x->irq_info.dev = &pdev->dev;
  1498. ret = wcd_irq_init(&wsa883x->irq_info, &wsa883x->virq);
  1499. if (ret) {
  1500. dev_err(wsa883x->dev, "%s: IRQ init failed: %d\n",
  1501. __func__, ret);
  1502. goto dev_err;
  1503. }
  1504. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_SAF2WAR,
  1505. "WSA SAF2WAR", wsa883x_saf2war_handle_irq, wsa883x);
  1506. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_SAF2WAR);
  1507. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_WAR2SAF,
  1508. "WSA WAR2SAF", wsa883x_war2saf_handle_irq, wsa883x);
  1509. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_WAR2SAF);
  1510. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_DISABLE,
  1511. "WSA OTP", wsa883x_otp_handle_irq, wsa883x);
  1512. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_DISABLE);
  1513. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_OCP,
  1514. "WSA OCP", wsa883x_ocp_handle_irq, wsa883x);
  1515. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_OCP);
  1516. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLIP,
  1517. "WSA CLIP", wsa883x_clip_handle_irq, wsa883x);
  1518. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLIP);
  1519. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PDM_WD,
  1520. "WSA PDM WD", wsa883x_pdm_wd_handle_irq, wsa883x);
  1521. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PDM_WD);
  1522. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLK_WD,
  1523. "WSA CLK WD", wsa883x_clk_wd_handle_irq, wsa883x);
  1524. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLK_WD);
  1525. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_INTR_PIN,
  1526. "WSA EXT INT", wsa883x_ext_int_handle_irq, wsa883x);
  1527. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_INTR_PIN);
  1528. /* Under Voltage Lock out (UVLO) interrupt handle */
  1529. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_UVLO,
  1530. "WSA UVLO", wsa883x_uvlo_handle_irq, wsa883x);
  1531. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_UVLO);
  1532. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PA_ON_ERR,
  1533. "WSA PA ERR", wsa883x_pa_on_err_handle_irq, wsa883x);
  1534. wcd_disable_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PA_ON_ERR);
  1535. wsa883x->driver = devm_kzalloc(&pdev->dev,
  1536. sizeof(struct snd_soc_component_driver), GFP_KERNEL);
  1537. if (!wsa883x->driver) {
  1538. ret = -ENOMEM;
  1539. goto err_irq;
  1540. }
  1541. memcpy(wsa883x->driver, &soc_codec_dev_wsa883x_wsa,
  1542. sizeof(struct snd_soc_component_driver));
  1543. wsa883x->dai_driver = devm_kzalloc(&pdev->dev,
  1544. sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
  1545. if (!wsa883x->dai_driver) {
  1546. ret = -ENOMEM;
  1547. goto err_mem;
  1548. }
  1549. memcpy(wsa883x->dai_driver, wsa_dai, sizeof(struct snd_soc_dai_driver));
  1550. /* Get last digit from HEX format */
  1551. dev_index = (int)((char)(pdev->addr & 0xF));
  1552. if (of_device_is_compatible(pdev->dev.of_node, "qcom,wsa883x_2"))
  1553. dev_index += 2;
  1554. snprintf(buffer, sizeof(buffer), "wsa-codec.%d", dev_index);
  1555. wsa883x->driver->name = kstrndup(buffer, strlen(buffer), GFP_KERNEL);
  1556. snprintf(buffer, sizeof(buffer), "wsa_rx%d", dev_index);
  1557. wsa883x->dai_driver->name =
  1558. kstrndup(buffer, strlen(buffer), GFP_KERNEL);
  1559. snprintf(buffer, sizeof(buffer), "WSA883X_AIF%d Playback", dev_index);
  1560. wsa883x->dai_driver->playback.stream_name =
  1561. kstrndup(buffer, strlen(buffer), GFP_KERNEL);
  1562. /* Number of DAI's used is 1 */
  1563. ret = snd_soc_register_component(&pdev->dev,
  1564. wsa883x->driver, wsa883x->dai_driver, 1);
  1565. component = snd_soc_lookup_component(&pdev->dev, wsa883x->driver->name);
  1566. if (!component) {
  1567. dev_err(&pdev->dev, "%s: component is NULL \n", __func__);
  1568. ret = -EINVAL;
  1569. goto err_mem;
  1570. }
  1571. wsa883x->parent_np = of_parse_phandle(pdev->dev.of_node,
  1572. "qcom,bolero-handle", 0);
  1573. if (!wsa883x->parent_np)
  1574. wsa883x->parent_np = of_parse_phandle(pdev->dev.of_node,
  1575. "qcom,lpass-cdc-handle", 0);
  1576. if (wsa883x->parent_np) {
  1577. wsa883x->parent_dev =
  1578. of_find_device_by_node(wsa883x->parent_np);
  1579. if (wsa883x->parent_dev) {
  1580. plat_data = dev_get_platdata(&wsa883x->parent_dev->dev);
  1581. if (plat_data) {
  1582. wsa883x->parent_nblock.notifier_call =
  1583. wsa883x_event_notify;
  1584. if (plat_data->register_notifier)
  1585. plat_data->register_notifier(
  1586. plat_data->handle,
  1587. &wsa883x->parent_nblock,
  1588. true);
  1589. wsa883x->register_notifier =
  1590. plat_data->register_notifier;
  1591. wsa883x->handle = plat_data->handle;
  1592. } else {
  1593. dev_err(&pdev->dev, "%s: plat data not found\n",
  1594. __func__);
  1595. }
  1596. } else {
  1597. dev_err(&pdev->dev, "%s: parent dev not found\n",
  1598. __func__);
  1599. }
  1600. } else {
  1601. dev_info(&pdev->dev, "%s: parent node not found\n", __func__);
  1602. }
  1603. mutex_init(&wsa883x->res_lock);
  1604. #ifdef CONFIG_DEBUG_FS
  1605. if (!wsa883x->debugfs_dent) {
  1606. wsa883x->debugfs_dent = debugfs_create_dir(
  1607. dev_name(&pdev->dev), 0);
  1608. if (!IS_ERR(wsa883x->debugfs_dent)) {
  1609. wsa883x->debugfs_peek =
  1610. debugfs_create_file("swrslave_peek",
  1611. S_IFREG | 0444,
  1612. wsa883x->debugfs_dent,
  1613. (void *) pdev,
  1614. &codec_debug_read_ops);
  1615. wsa883x->debugfs_poke =
  1616. debugfs_create_file("swrslave_poke",
  1617. S_IFREG | 0444,
  1618. wsa883x->debugfs_dent,
  1619. (void *) pdev,
  1620. &codec_debug_write_ops);
  1621. wsa883x->debugfs_reg_dump =
  1622. debugfs_create_file(
  1623. "swrslave_reg_dump",
  1624. S_IFREG | 0444,
  1625. wsa883x->debugfs_dent,
  1626. (void *) pdev,
  1627. &codec_debug_dump_ops);
  1628. }
  1629. }
  1630. #endif
  1631. return 0;
  1632. err_mem:
  1633. if (wsa883x->dai_driver) {
  1634. kfree(wsa883x->dai_driver->name);
  1635. kfree(wsa883x->dai_driver->playback.stream_name);
  1636. kfree(wsa883x->dai_driver);
  1637. }
  1638. if (wsa883x->driver) {
  1639. kfree(wsa883x->driver->name);
  1640. kfree(wsa883x->driver);
  1641. }
  1642. err_irq:
  1643. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_SAF2WAR, NULL);
  1644. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_WAR2SAF, NULL);
  1645. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_DISABLE, NULL);
  1646. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_OCP, NULL);
  1647. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLIP, NULL);
  1648. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PDM_WD, NULL);
  1649. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLK_WD, NULL);
  1650. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_INTR_PIN, NULL);
  1651. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_UVLO, NULL);
  1652. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PA_ON_ERR, NULL);
  1653. wcd_irq_exit(&wsa883x->irq_info, wsa883x->virq);
  1654. dev_err:
  1655. if (pin_state_current == false)
  1656. wsa883x_gpio_ctrl(wsa883x, false);
  1657. swr_remove_device(pdev);
  1658. err_supply:
  1659. msm_cdc_release_supplies(&pdev->dev, wsa883x->supplies,
  1660. wsa883x->regulator,
  1661. wsa883x->num_supplies);
  1662. err:
  1663. swr_set_dev_data(pdev, NULL);
  1664. return ret;
  1665. }
  1666. static int wsa883x_swr_remove(struct swr_device *pdev)
  1667. {
  1668. struct wsa883x_priv *wsa883x;
  1669. wsa883x = swr_get_dev_data(pdev);
  1670. if (!wsa883x) {
  1671. dev_err(&pdev->dev, "%s: wsa883x is NULL\n", __func__);
  1672. return -EINVAL;
  1673. }
  1674. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_SAF2WAR, NULL);
  1675. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_WAR2SAF, NULL);
  1676. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_DISABLE, NULL);
  1677. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_OCP, NULL);
  1678. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLIP, NULL);
  1679. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PDM_WD, NULL);
  1680. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLK_WD, NULL);
  1681. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_INTR_PIN, NULL);
  1682. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_UVLO, NULL);
  1683. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PA_ON_ERR, NULL);
  1684. if (wsa883x->register_notifier)
  1685. wsa883x->register_notifier(wsa883x->handle,
  1686. &wsa883x->parent_nblock, false);
  1687. #ifdef CONFIG_DEBUG_FS
  1688. debugfs_remove_recursive(wsa883x->debugfs_dent);
  1689. wsa883x->debugfs_dent = NULL;
  1690. #endif
  1691. mutex_destroy(&wsa883x->res_lock);
  1692. snd_soc_unregister_component(&pdev->dev);
  1693. if (wsa883x->dai_driver) {
  1694. kfree(wsa883x->dai_driver->name);
  1695. kfree(wsa883x->dai_driver->playback.stream_name);
  1696. kfree(wsa883x->dai_driver);
  1697. }
  1698. if (wsa883x->driver) {
  1699. kfree(wsa883x->driver->name);
  1700. kfree(wsa883x->driver);
  1701. }
  1702. msm_cdc_release_supplies(&pdev->dev, wsa883x->supplies,
  1703. wsa883x->regulator,
  1704. wsa883x->num_supplies);
  1705. swr_set_dev_data(pdev, NULL);
  1706. return 0;
  1707. }
  1708. #ifdef CONFIG_PM_SLEEP
  1709. static int wsa883x_swr_suspend(struct device *dev)
  1710. {
  1711. struct wsa883x_priv *wsa883x = swr_get_dev_data(to_swr_device(dev));
  1712. if (!wsa883x) {
  1713. dev_err(dev, "%s: wsa883x private data is NULL\n", __func__);
  1714. return -EINVAL;
  1715. }
  1716. dev_dbg(dev, "%s: system suspend\n", __func__);
  1717. if (wsa883x->dapm_bias_off) {
  1718. msm_cdc_set_supplies_lpm_mode(dev, wsa883x->supplies,
  1719. wsa883x->regulator,
  1720. wsa883x->num_supplies,
  1721. true);
  1722. set_bit(WSA_SUPPLIES_LPM_MODE, &wsa883x->status_mask);
  1723. }
  1724. return 0;
  1725. }
  1726. static int wsa883x_swr_resume(struct device *dev)
  1727. {
  1728. struct wsa883x_priv *wsa883x = swr_get_dev_data(to_swr_device(dev));
  1729. if (!wsa883x) {
  1730. dev_err(dev, "%s: wsa883x private data is NULL\n", __func__);
  1731. return -EINVAL;
  1732. }
  1733. if (test_bit(WSA_SUPPLIES_LPM_MODE, &wsa883x->status_mask)) {
  1734. msm_cdc_set_supplies_lpm_mode(dev, wsa883x->supplies,
  1735. wsa883x->regulator,
  1736. wsa883x->num_supplies,
  1737. false);
  1738. clear_bit(WSA_SUPPLIES_LPM_MODE, &wsa883x->status_mask);
  1739. }
  1740. dev_dbg(dev, "%s: system resume\n", __func__);
  1741. return 0;
  1742. }
  1743. #endif /* CONFIG_PM_SLEEP */
  1744. static const struct dev_pm_ops wsa883x_swr_pm_ops = {
  1745. .suspend_late = wsa883x_swr_suspend,
  1746. .resume_early = wsa883x_swr_resume,
  1747. };
  1748. static const struct swr_device_id wsa883x_swr_id[] = {
  1749. {"wsa883x", 0},
  1750. {"wsa883x_2", 0},
  1751. {}
  1752. };
  1753. static const struct of_device_id wsa883x_swr_dt_match[] = {
  1754. {
  1755. .compatible = "qcom,wsa883x",
  1756. },
  1757. {
  1758. .compatible = "qcom,wsa883x_2",
  1759. },
  1760. {}
  1761. };
  1762. static struct swr_driver wsa883x_swr_driver = {
  1763. .driver = {
  1764. .name = "wsa883x",
  1765. .owner = THIS_MODULE,
  1766. .pm = &wsa883x_swr_pm_ops,
  1767. .of_match_table = wsa883x_swr_dt_match,
  1768. },
  1769. .probe = wsa883x_swr_probe,
  1770. .remove = wsa883x_swr_remove,
  1771. .id_table = wsa883x_swr_id,
  1772. };
  1773. static int __init wsa883x_swr_init(void)
  1774. {
  1775. return swr_driver_register(&wsa883x_swr_driver);
  1776. }
  1777. static void __exit wsa883x_swr_exit(void)
  1778. {
  1779. swr_driver_unregister(&wsa883x_swr_driver);
  1780. }
  1781. module_init(wsa883x_swr_init);
  1782. module_exit(wsa883x_swr_exit);
  1783. MODULE_DESCRIPTION("WSA883x codec driver");
  1784. MODULE_LICENSE("GPL v2");