wsa883x.c 49 KB

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