wsa883x.c 54 KB

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