wsa883x.c 48 KB

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