wsa883x.c 48 KB

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