wsa883x.c 52 KB

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