wsa883x.c 52 KB

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