wsa883x.c 54 KB

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