wsa883x.c 50 KB

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