wsa883x.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, 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/regmap.h>
  19. #include <linux/debugfs.h>
  20. #include <soc/soundwire.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dapm.h>
  25. #include <sound/tlv.h>
  26. #include <asoc/msm-cdc-pinctrl.h>
  27. #include "wsa883x.h"
  28. #include "internal.h"
  29. #define T1_TEMP -10
  30. #define T2_TEMP 150
  31. #define LOW_TEMP_THRESHOLD 5
  32. #define HIGH_TEMP_THRESHOLD 45
  33. #define TEMP_INVALID 0xFFFF
  34. #define WSA883X_TEMP_RETRY 3
  35. enum {
  36. WSA_4OHMS =4,
  37. WSA_8OHMS = 8,
  38. WSA_16OHMS = 16,
  39. WSA_32OHMS = 32,
  40. };
  41. struct wsa_temp_register {
  42. u8 d1_msb;
  43. u8 d1_lsb;
  44. u8 d2_msb;
  45. u8 d2_lsb;
  46. u8 dmeas_msb;
  47. u8 dmeas_lsb;
  48. };
  49. static int wsa883x_get_temperature(struct snd_soc_component *component,
  50. int *temp);
  51. enum {
  52. WSA8830 = 0,
  53. WSA8835,
  54. };
  55. enum {
  56. WSA883X_IRQ_INT_SAF2WAR = 0,
  57. WSA883X_IRQ_INT_WAR2SAF,
  58. WSA883X_IRQ_INT_DISABLE,
  59. WSA883X_IRQ_INT_OCP,
  60. WSA883X_IRQ_INT_CLIP,
  61. WSA883X_IRQ_INT_PDM_WD,
  62. WSA883X_IRQ_INT_CLK_WD,
  63. WSA883X_IRQ_INT_INTR_PIN,
  64. WSA883X_IRQ_INT_UVLO,
  65. WSA883X_IRQ_INT_PA_ON_ERR,
  66. };
  67. static const struct regmap_irq wsa883x_irqs[WSA883X_NUM_IRQS] = {
  68. REGMAP_IRQ_REG(WSA883X_IRQ_INT_SAF2WAR, 0, 0x01),
  69. REGMAP_IRQ_REG(WSA883X_IRQ_INT_WAR2SAF, 0, 0x02),
  70. REGMAP_IRQ_REG(WSA883X_IRQ_INT_DISABLE, 0, 0x04),
  71. REGMAP_IRQ_REG(WSA883X_IRQ_INT_OCP, 0, 0x08),
  72. REGMAP_IRQ_REG(WSA883X_IRQ_INT_CLIP, 0, 0x10),
  73. REGMAP_IRQ_REG(WSA883X_IRQ_INT_PDM_WD, 0, 0x20),
  74. REGMAP_IRQ_REG(WSA883X_IRQ_INT_CLK_WD, 0, 0x40),
  75. REGMAP_IRQ_REG(WSA883X_IRQ_INT_INTR_PIN, 0, 0x80),
  76. REGMAP_IRQ_REG(WSA883X_IRQ_INT_UVLO, 1, 0x01),
  77. REGMAP_IRQ_REG(WSA883X_IRQ_INT_PA_ON_ERR, 1, 0x02),
  78. };
  79. static struct regmap_irq_chip wsa883x_regmap_irq_chip = {
  80. .name = "wsa883x",
  81. .irqs = wsa883x_irqs,
  82. .num_irqs = ARRAY_SIZE(wsa883x_irqs),
  83. .num_regs = 2,
  84. .status_base = WSA883X_INTR_STATUS0,
  85. .mask_base = WSA883X_INTR_MASK0,
  86. .type_base = WSA883X_INTR_LEVEL0,
  87. .ack_base = WSA883X_INTR_CLEAR0,
  88. .use_ack = 1,
  89. .runtime_pm = false,
  90. .irq_drv_data = NULL,
  91. };
  92. #ifdef CONFIG_DEBUG_FS
  93. static int codec_debug_open(struct inode *inode, struct file *file)
  94. {
  95. file->private_data = inode->i_private;
  96. return 0;
  97. }
  98. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  99. {
  100. char *token;
  101. int base, cnt;
  102. token = strsep(&buf, " ");
  103. for (cnt = 0; cnt < num_of_par; cnt++) {
  104. if (token) {
  105. if ((token[1] == 'x') || (token[1] == 'X'))
  106. base = 16;
  107. else
  108. base = 10;
  109. if (kstrtou32(token, base, &param1[cnt]) != 0)
  110. return -EINVAL;
  111. token = strsep(&buf, " ");
  112. } else {
  113. return -EINVAL;
  114. }
  115. }
  116. return 0;
  117. }
  118. static bool is_swr_slave_reg_readable(int reg)
  119. {
  120. int ret = true;
  121. if (((reg > 0x46) && (reg < 0x4A)) ||
  122. ((reg > 0x4A) && (reg < 0x50)) ||
  123. ((reg > 0x55) && (reg < 0xD0)) ||
  124. ((reg > 0xD0) && (reg < 0xE0)) ||
  125. ((reg > 0xE0) && (reg < 0xF0)) ||
  126. ((reg > 0xF0) && (reg < 0x100)) ||
  127. ((reg > 0x105) && (reg < 0x120)) ||
  128. ((reg > 0x205) && (reg < 0x220)) ||
  129. ((reg > 0x305) && (reg < 0x320)) ||
  130. ((reg > 0x405) && (reg < 0x420)) ||
  131. ((reg > 0x128) && (reg < 0x130)) ||
  132. ((reg > 0x228) && (reg < 0x230)) ||
  133. ((reg > 0x328) && (reg < 0x330)) ||
  134. ((reg > 0x428) && (reg < 0x430)) ||
  135. ((reg > 0x138) && (reg < 0x205)) ||
  136. ((reg > 0x238) && (reg < 0x305)) ||
  137. ((reg > 0x338) && (reg < 0x405)) ||
  138. ((reg > 0x405) && (reg < 0xF00)) ||
  139. ((reg > 0xF05) && (reg < 0xF20)) ||
  140. ((reg > 0xF25) && (reg < 0xF30)) ||
  141. ((reg > 0xF35) && (reg < 0x2000)))
  142. ret = false;
  143. return ret;
  144. }
  145. static ssize_t swr_slave_reg_show(struct swr_device *pdev, char __user *ubuf,
  146. size_t count, loff_t *ppos)
  147. {
  148. int i, reg_val, len;
  149. ssize_t total = 0;
  150. char tmp_buf[SWR_SLV_MAX_BUF_LEN];
  151. if (!ubuf || !ppos)
  152. return 0;
  153. for (i = (((int) *ppos/BYTES_PER_LINE) + SWR_SLV_START_REG_ADDR);
  154. i <= SWR_SLV_MAX_REG_ADDR; i++) {
  155. if (!is_swr_slave_reg_readable(i))
  156. continue;
  157. swr_read(pdev, pdev->dev_num, i, &reg_val, 1);
  158. len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i,
  159. (reg_val & 0xFF));
  160. if ((total + len) >= count - 1)
  161. break;
  162. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  163. pr_err("%s: fail to copy reg dump\n", __func__);
  164. total = -EFAULT;
  165. goto copy_err;
  166. }
  167. total += len;
  168. *ppos += len;
  169. }
  170. copy_err:
  171. *ppos = SWR_SLV_MAX_REG_ADDR * BYTES_PER_LINE;
  172. return total;
  173. }
  174. static ssize_t codec_debug_dump(struct file *file, char __user *ubuf,
  175. size_t count, loff_t *ppos)
  176. {
  177. struct swr_device *pdev;
  178. if (!count || !file || !ppos || !ubuf)
  179. return -EINVAL;
  180. pdev = file->private_data;
  181. if (!pdev)
  182. return -EINVAL;
  183. if (*ppos < 0)
  184. return -EINVAL;
  185. return swr_slave_reg_show(pdev, ubuf, count, ppos);
  186. }
  187. static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
  188. size_t count, loff_t *ppos)
  189. {
  190. char lbuf[SWR_SLV_RD_BUF_LEN];
  191. struct swr_device *pdev = NULL;
  192. struct wsa883x_priv *wsa883x = NULL;
  193. if (!count || !file || !ppos || !ubuf)
  194. return -EINVAL;
  195. pdev = file->private_data;
  196. if (!pdev)
  197. return -EINVAL;
  198. wsa883x = swr_get_dev_data(pdev);
  199. if (!wsa883x)
  200. return -EINVAL;
  201. if (*ppos < 0)
  202. return -EINVAL;
  203. snprintf(lbuf, sizeof(lbuf), "0x%x\n",
  204. (wsa883x->read_data & 0xFF));
  205. return simple_read_from_buffer(ubuf, count, ppos, lbuf,
  206. strnlen(lbuf, 7));
  207. }
  208. static ssize_t codec_debug_peek_write(struct file *file,
  209. const char __user *ubuf, size_t cnt, loff_t *ppos)
  210. {
  211. char lbuf[SWR_SLV_WR_BUF_LEN];
  212. int rc = 0;
  213. u32 param[5];
  214. struct swr_device *pdev = NULL;
  215. struct wsa883x_priv *wsa883x = NULL;
  216. if (!cnt || !file || !ppos || !ubuf)
  217. return -EINVAL;
  218. pdev = file->private_data;
  219. if (!pdev)
  220. return -EINVAL;
  221. wsa883x = swr_get_dev_data(pdev);
  222. if (!wsa883x)
  223. return -EINVAL;
  224. if (*ppos < 0)
  225. return -EINVAL;
  226. if (cnt > sizeof(lbuf) - 1)
  227. return -EINVAL;
  228. rc = copy_from_user(lbuf, ubuf, cnt);
  229. if (rc)
  230. return -EFAULT;
  231. lbuf[cnt] = '\0';
  232. rc = get_parameters(lbuf, param, 1);
  233. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) && (rc == 0)))
  234. return -EINVAL;
  235. swr_read(pdev, pdev->dev_num, param[0], &wsa883x->read_data, 1);
  236. if (rc == 0)
  237. rc = cnt;
  238. else
  239. pr_err("%s: rc = %d\n", __func__, rc);
  240. return rc;
  241. }
  242. static ssize_t codec_debug_write(struct file *file,
  243. const char __user *ubuf, size_t cnt, loff_t *ppos)
  244. {
  245. char lbuf[SWR_SLV_WR_BUF_LEN];
  246. int rc = 0;
  247. u32 param[5];
  248. struct swr_device *pdev;
  249. if (!file || !ppos || !ubuf)
  250. return -EINVAL;
  251. pdev = file->private_data;
  252. if (!pdev)
  253. return -EINVAL;
  254. if (cnt > sizeof(lbuf) - 1)
  255. return -EINVAL;
  256. rc = copy_from_user(lbuf, ubuf, cnt);
  257. if (rc)
  258. return -EFAULT;
  259. lbuf[cnt] = '\0';
  260. rc = get_parameters(lbuf, param, 2);
  261. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) &&
  262. (param[1] <= 0xFF) && (rc == 0)))
  263. return -EINVAL;
  264. swr_write(pdev, pdev->dev_num, param[0], &param[1]);
  265. if (rc == 0)
  266. rc = cnt;
  267. else
  268. pr_err("%s: rc = %d\n", __func__, rc);
  269. return rc;
  270. }
  271. static const struct file_operations codec_debug_write_ops = {
  272. .open = codec_debug_open,
  273. .write = codec_debug_write,
  274. };
  275. static const struct file_operations codec_debug_read_ops = {
  276. .open = codec_debug_open,
  277. .read = codec_debug_read,
  278. .write = codec_debug_peek_write,
  279. };
  280. static const struct file_operations codec_debug_dump_ops = {
  281. .open = codec_debug_open,
  282. .read = codec_debug_dump,
  283. };
  284. #endif
  285. static irqreturn_t wsa883x_saf2war_handle_irq(int irq, void *data)
  286. {
  287. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  288. __func__, irq);
  289. return IRQ_HANDLED;
  290. }
  291. static irqreturn_t wsa883x_war2saf_handle_irq(int irq, void *data)
  292. {
  293. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  294. __func__, irq);
  295. return IRQ_HANDLED;
  296. }
  297. static irqreturn_t wsa883x_otp_handle_irq(int irq, void *data)
  298. {
  299. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  300. __func__, irq);
  301. return IRQ_HANDLED;
  302. }
  303. static irqreturn_t wsa883x_ocp_handle_irq(int irq, void *data)
  304. {
  305. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  306. __func__, irq);
  307. return IRQ_HANDLED;
  308. }
  309. static irqreturn_t wsa883x_clip_handle_irq(int irq, void *data)
  310. {
  311. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  312. __func__, irq);
  313. return IRQ_HANDLED;
  314. }
  315. static irqreturn_t wsa883x_pdm_wd_handle_irq(int irq, void *data)
  316. {
  317. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  318. __func__, irq);
  319. return IRQ_HANDLED;
  320. }
  321. static irqreturn_t wsa883x_clk_wd_handle_irq(int irq, void *data)
  322. {
  323. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  324. __func__, irq);
  325. return IRQ_HANDLED;
  326. }
  327. static irqreturn_t wsa883x_ext_int_handle_irq(int irq, void *data)
  328. {
  329. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  330. __func__, irq);
  331. return IRQ_HANDLED;
  332. }
  333. static irqreturn_t wsa883x_uvlo_handle_irq(int irq, void *data)
  334. {
  335. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  336. __func__, irq);
  337. return IRQ_HANDLED;
  338. }
  339. static irqreturn_t wsa883x_pa_on_err_handle_irq(int irq, void *data)
  340. {
  341. pr_err_ratelimited("%s: interrupt for irq =%d triggered\n",
  342. __func__, irq);
  343. return IRQ_HANDLED;
  344. }
  345. static const char * const wsa_dev_mode_text[] = {
  346. "speaker", "receiver", "ultrasound"
  347. };
  348. static const struct soc_enum wsa_dev_mode_enum =
  349. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_dev_mode_text), wsa_dev_mode_text);
  350. static int wsa_dev_mode_get(struct snd_kcontrol *kcontrol,
  351. struct snd_ctl_elem_value *ucontrol)
  352. {
  353. struct snd_soc_component *component =
  354. snd_soc_kcontrol_component(kcontrol);
  355. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  356. ucontrol->value.integer.value[0] = wsa883x->dev_mode;
  357. dev_dbg(component->dev, "%s: mode = 0x%x\n", __func__,
  358. wsa883x->dev_mode);
  359. return 0;
  360. }
  361. static int wsa_dev_mode_put(struct snd_kcontrol *kcontrol,
  362. struct snd_ctl_elem_value *ucontrol)
  363. {
  364. struct snd_soc_component *component =
  365. snd_soc_kcontrol_component(kcontrol);
  366. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  367. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  368. __func__, ucontrol->value.integer.value[0]);
  369. wsa883x->dev_mode = ucontrol->value.integer.value[0];
  370. return 0;
  371. }
  372. static const char * const wsa_pa_gain_text[] = {
  373. "G_18_DB", "G_16P5_DB", "G_15_DB", "G_13P5_DB", "G_12_DB", "G_10P5_DB",
  374. "G_9_DB", "G_7P5_DB", "G_6_DB", "G_4P5_DB", "G_3_DB", "G_1P5_DB",
  375. "G_0_DB"
  376. };
  377. static const struct soc_enum wsa_pa_gain_enum =
  378. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_pa_gain_text), wsa_pa_gain_text);
  379. static int wsa_pa_gain_get(struct snd_kcontrol *kcontrol,
  380. struct snd_ctl_elem_value *ucontrol)
  381. {
  382. struct snd_soc_component *component =
  383. snd_soc_kcontrol_component(kcontrol);
  384. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  385. ucontrol->value.integer.value[0] = wsa883x->pa_gain;
  386. dev_dbg(component->dev, "%s: PA gain = 0x%x\n", __func__,
  387. wsa883x->pa_gain);
  388. return 0;
  389. }
  390. static int wsa_pa_gain_put(struct snd_kcontrol *kcontrol,
  391. struct snd_ctl_elem_value *ucontrol)
  392. {
  393. struct snd_soc_component *component =
  394. snd_soc_kcontrol_component(kcontrol);
  395. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  396. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  397. __func__, ucontrol->value.integer.value[0]);
  398. wsa883x->pa_gain = ucontrol->value.integer.value[0];
  399. return 0;
  400. }
  401. static int wsa883x_get_mute(struct snd_kcontrol *kcontrol,
  402. struct snd_ctl_elem_value *ucontrol)
  403. {
  404. struct snd_soc_component *component =
  405. snd_soc_kcontrol_component(kcontrol);
  406. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  407. ucontrol->value.integer.value[0] = wsa883x->pa_mute;
  408. return 0;
  409. }
  410. static int wsa883x_set_mute(struct snd_kcontrol *kcontrol,
  411. struct snd_ctl_elem_value *ucontrol)
  412. {
  413. struct snd_soc_component *component =
  414. snd_soc_kcontrol_component(kcontrol);
  415. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  416. int value = ucontrol->value.integer.value[0];
  417. dev_dbg(component->dev, "%s: mute current %d, new %d\n",
  418. __func__, wsa883x->pa_mute, value);
  419. wsa883x->pa_mute = value;
  420. return 0;
  421. }
  422. static int wsa_get_temp(struct snd_kcontrol *kcontrol,
  423. struct snd_ctl_elem_value *ucontrol)
  424. {
  425. struct snd_soc_component *component =
  426. snd_soc_kcontrol_component(kcontrol);
  427. int temp = 0;
  428. wsa883x_get_temperature(component, &temp);
  429. ucontrol->value.integer.value[0] = temp;
  430. return 0;
  431. }
  432. static ssize_t wsa883x_codec_version_read(struct snd_info_entry *entry,
  433. void *file_private_data, struct file *file,
  434. char __user *buf, size_t count, loff_t pos)
  435. {
  436. struct wsa883x_priv *wsa883x;
  437. char buffer[WSA883X_VERSION_ENTRY_SIZE];
  438. int len = 0;
  439. wsa883x = (struct wsa883x_priv *) entry->private_data;
  440. if (!wsa883x) {
  441. pr_err("%s: wsa883x priv is null\n", __func__);
  442. return -EINVAL;
  443. }
  444. len = snprintf(buffer, sizeof(buffer), "WSA883X-SOUNDWIRE_1_0\n");
  445. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  446. }
  447. static struct snd_info_entry_ops wsa883x_codec_info_ops = {
  448. .read = wsa883x_codec_version_read,
  449. };
  450. /*
  451. * wsa883x_codec_info_create_codec_entry - creates wsa883x module
  452. * @codec_root: The parent directory
  453. * @component: Codec instance
  454. *
  455. * Creates wsa883x module and version entry under the given
  456. * parent directory.
  457. *
  458. * Return: 0 on success or negative error code on failure.
  459. */
  460. int wsa883x_codec_info_create_codec_entry(struct snd_info_entry *codec_root,
  461. struct snd_soc_component *component)
  462. {
  463. struct snd_info_entry *version_entry;
  464. struct wsa883x_priv *wsa883x;
  465. struct snd_soc_card *card;
  466. char name[80];
  467. if (!codec_root || !component)
  468. return -EINVAL;
  469. wsa883x = snd_soc_component_get_drvdata(component);
  470. card = component->card;
  471. snprintf(name, sizeof(name), "%s.%x", "wsa883x",
  472. (u32)wsa883x->swr_slave->addr);
  473. wsa883x->entry = snd_info_create_subdir(codec_root->module,
  474. (const char *)name,
  475. codec_root);
  476. if (!wsa883x->entry) {
  477. dev_dbg(component->dev, "%s: failed to create wsa883x entry\n",
  478. __func__);
  479. return -ENOMEM;
  480. }
  481. version_entry = snd_info_create_card_entry(card->snd_card,
  482. "version",
  483. wsa883x->entry);
  484. if (!version_entry) {
  485. dev_dbg(component->dev, "%s: failed to create wsa883x version entry\n",
  486. __func__);
  487. return -ENOMEM;
  488. }
  489. version_entry->private_data = wsa883x;
  490. version_entry->size = WSA883X_VERSION_ENTRY_SIZE;
  491. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  492. version_entry->c.ops = &wsa883x_codec_info_ops;
  493. if (snd_info_register(version_entry) < 0) {
  494. snd_info_free_entry(version_entry);
  495. return -ENOMEM;
  496. }
  497. wsa883x->version_entry = version_entry;
  498. return 0;
  499. }
  500. EXPORT_SYMBOL(wsa883x_codec_info_create_codec_entry);
  501. static void wsa883x_regcache_sync(struct wsa883x_priv *wsa883x)
  502. {
  503. mutex_lock(&wsa883x->res_lock);
  504. regcache_mark_dirty(wsa883x->regmap);
  505. regcache_sync(wsa883x->regmap);
  506. mutex_unlock(&wsa883x->res_lock);
  507. }
  508. static int wsa883x_get_compander(struct snd_kcontrol *kcontrol,
  509. struct snd_ctl_elem_value *ucontrol)
  510. {
  511. struct snd_soc_component *component =
  512. snd_soc_kcontrol_component(kcontrol);
  513. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  514. ucontrol->value.integer.value[0] = wsa883x->comp_enable;
  515. return 0;
  516. }
  517. static int wsa883x_set_compander(struct snd_kcontrol *kcontrol,
  518. struct snd_ctl_elem_value *ucontrol)
  519. {
  520. struct snd_soc_component *component =
  521. snd_soc_kcontrol_component(kcontrol);
  522. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  523. int value = ucontrol->value.integer.value[0];
  524. dev_dbg(component->dev, "%s: Compander enable current %d, new %d\n",
  525. __func__, wsa883x->comp_enable, value);
  526. wsa883x->comp_enable = value;
  527. return 0;
  528. }
  529. static int wsa883x_get_visense(struct snd_kcontrol *kcontrol,
  530. struct snd_ctl_elem_value *ucontrol)
  531. {
  532. struct snd_soc_component *component =
  533. snd_soc_kcontrol_component(kcontrol);
  534. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  535. ucontrol->value.integer.value[0] = wsa883x->visense_enable;
  536. return 0;
  537. }
  538. static int wsa883x_set_visense(struct snd_kcontrol *kcontrol,
  539. struct snd_ctl_elem_value *ucontrol)
  540. {
  541. struct snd_soc_component *component =
  542. snd_soc_kcontrol_component(kcontrol);
  543. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  544. int value = ucontrol->value.integer.value[0];
  545. dev_dbg(component->dev, "%s: VIsense enable current %d, new %d\n",
  546. __func__, wsa883x->visense_enable, value);
  547. wsa883x->visense_enable = value;
  548. return 0;
  549. }
  550. static int wsa883x_get_ext_vdd_spk(struct snd_kcontrol *kcontrol,
  551. struct snd_ctl_elem_value *ucontrol)
  552. {
  553. struct snd_soc_component *component =
  554. snd_soc_kcontrol_component(kcontrol);
  555. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  556. ucontrol->value.integer.value[0] = wsa883x->ext_vdd_spk;
  557. return 0;
  558. }
  559. static int wsa883x_put_ext_vdd_spk(struct snd_kcontrol *kcontrol,
  560. struct snd_ctl_elem_value *ucontrol)
  561. {
  562. struct snd_soc_component *component =
  563. snd_soc_kcontrol_component(kcontrol);
  564. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  565. int value = ucontrol->value.integer.value[0];
  566. dev_dbg(component->dev, "%s: Ext VDD SPK enable current %d, new %d\n",
  567. __func__, wsa883x->ext_vdd_spk, value);
  568. wsa883x->ext_vdd_spk = value;
  569. return 0;
  570. }
  571. static const struct snd_kcontrol_new wsa883x_snd_controls[] = {
  572. SOC_ENUM_EXT("WSA PA Gain", wsa_pa_gain_enum,
  573. wsa_pa_gain_get, wsa_pa_gain_put),
  574. SOC_SINGLE_EXT("WSA PA Mute", SND_SOC_NOPM, 0, 1, 0,
  575. wsa883x_get_mute, wsa883x_set_mute),
  576. SOC_SINGLE_EXT("WSA Temp", SND_SOC_NOPM, 0, 1, 0,
  577. wsa_get_temp, NULL),
  578. SOC_ENUM_EXT("WSA MODE", wsa_dev_mode_enum,
  579. wsa_dev_mode_get, wsa_dev_mode_put),
  580. SOC_SINGLE_EXT("COMP Switch", SND_SOC_NOPM, 0, 1, 0,
  581. wsa883x_get_compander, wsa883x_set_compander),
  582. SOC_SINGLE_EXT("VISENSE Switch", SND_SOC_NOPM, 0, 1, 0,
  583. wsa883x_get_visense, wsa883x_set_visense),
  584. SOC_SINGLE_EXT("External VDD_SPK", SND_SOC_NOPM, 0, 1, 0,
  585. wsa883x_get_ext_vdd_spk, wsa883x_put_ext_vdd_spk),
  586. };
  587. static const struct snd_kcontrol_new swr_dac_port[] = {
  588. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  589. };
  590. static int wsa883x_set_port(struct snd_soc_component *component, int port_idx,
  591. u8 *port_id, u8 *num_ch, u8 *ch_mask, u32 *ch_rate,
  592. u8 *port_type)
  593. {
  594. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  595. *port_id = wsa883x->port[port_idx].port_id;
  596. *num_ch = wsa883x->port[port_idx].num_ch;
  597. *ch_mask = wsa883x->port[port_idx].ch_mask;
  598. *ch_rate = wsa883x->port[port_idx].ch_rate;
  599. *port_type = wsa883x->port[port_idx].port_type;
  600. return 0;
  601. }
  602. static int wsa883x_enable_swr_dac_port(struct snd_soc_dapm_widget *w,
  603. struct snd_kcontrol *kcontrol, int event)
  604. {
  605. struct snd_soc_component *component =
  606. snd_soc_dapm_to_component(w->dapm);
  607. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  608. u8 port_id[WSA883X_MAX_SWR_PORTS];
  609. u8 num_ch[WSA883X_MAX_SWR_PORTS];
  610. u8 ch_mask[WSA883X_MAX_SWR_PORTS];
  611. u32 ch_rate[WSA883X_MAX_SWR_PORTS];
  612. u8 port_type[WSA883X_MAX_SWR_PORTS];
  613. u8 num_port = 0;
  614. dev_dbg(component->dev, "%s: event %d name %s\n", __func__,
  615. event, w->name);
  616. if (wsa883x == NULL)
  617. return -EINVAL;
  618. switch (event) {
  619. case SND_SOC_DAPM_PRE_PMU:
  620. wsa883x_set_port(component, SWR_DAC_PORT,
  621. &port_id[num_port], &num_ch[num_port],
  622. &ch_mask[num_port], &ch_rate[num_port],
  623. &port_type[num_port]);
  624. ++num_port;
  625. if (wsa883x->comp_enable) {
  626. wsa883x_set_port(component, SWR_COMP_PORT,
  627. &port_id[num_port], &num_ch[num_port],
  628. &ch_mask[num_port], &ch_rate[num_port],
  629. &port_type[num_port]);
  630. ++num_port;
  631. }
  632. if (wsa883x->visense_enable) {
  633. wsa883x_set_port(component, SWR_VISENSE_PORT,
  634. &port_id[num_port], &num_ch[num_port],
  635. &ch_mask[num_port], &ch_rate[num_port],
  636. &port_type[num_port]);
  637. ++num_port;
  638. }
  639. swr_connect_port(wsa883x->swr_slave, &port_id[0], num_port,
  640. &ch_mask[0], &ch_rate[0], &num_ch[0],
  641. &port_type[0]);
  642. break;
  643. case SND_SOC_DAPM_POST_PMU:
  644. swr_slvdev_datapath_control(wsa883x->swr_slave,
  645. wsa883x->swr_slave->dev_num,
  646. true);
  647. break;
  648. case SND_SOC_DAPM_PRE_PMD:
  649. swr_slvdev_datapath_control(wsa883x->swr_slave,
  650. wsa883x->swr_slave->dev_num,
  651. false);
  652. break;
  653. case SND_SOC_DAPM_POST_PMD:
  654. wsa883x_set_port(component, SWR_DAC_PORT,
  655. &port_id[num_port], &num_ch[num_port],
  656. &ch_mask[num_port], &ch_rate[num_port],
  657. &port_type[num_port]);
  658. ++num_port;
  659. if (wsa883x->comp_enable) {
  660. wsa883x_set_port(component, SWR_COMP_PORT,
  661. &port_id[num_port], &num_ch[num_port],
  662. &ch_mask[num_port], &ch_rate[num_port],
  663. &port_type[num_port]);
  664. ++num_port;
  665. }
  666. if (wsa883x->visense_enable) {
  667. wsa883x_set_port(component, SWR_VISENSE_PORT,
  668. &port_id[num_port], &num_ch[num_port],
  669. &ch_mask[num_port], &ch_rate[num_port],
  670. &port_type[num_port]);
  671. ++num_port;
  672. }
  673. swr_disconnect_port(wsa883x->swr_slave, &port_id[0], num_port,
  674. &ch_mask[0], &port_type[0]);
  675. break;
  676. default:
  677. break;
  678. }
  679. return 0;
  680. }
  681. static int wsa883x_spkr_event(struct snd_soc_dapm_widget *w,
  682. struct snd_kcontrol *kcontrol, int event)
  683. {
  684. struct snd_soc_component *component =
  685. snd_soc_dapm_to_component(w->dapm);
  686. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  687. int min_gain, max_gain;
  688. dev_dbg(component->dev, "%s: %s %d\n", __func__, w->name, event);
  689. switch (event) {
  690. case SND_SOC_DAPM_PRE_PMU:
  691. /* TODO Vote for Global PA */
  692. break;
  693. case SND_SOC_DAPM_POST_PMU:
  694. /* Force remove group */
  695. swr_remove_from_group(wsa883x->swr_slave,
  696. wsa883x->swr_slave->dev_num);
  697. break;
  698. case SND_SOC_DAPM_POST_PMD:
  699. /* TODO Unvote for Global PA */
  700. break;
  701. }
  702. return 0;
  703. }
  704. static const struct snd_soc_dapm_widget wsa883x_dapm_widgets[] = {
  705. SND_SOC_DAPM_INPUT("IN"),
  706. SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0, swr_dac_port,
  707. ARRAY_SIZE(swr_dac_port), wsa883x_enable_swr_dac_port,
  708. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  709. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  710. SND_SOC_DAPM_SPK("SPKR", wsa883x_spkr_event),
  711. };
  712. static const struct snd_soc_dapm_route wsa883x_audio_map[] = {
  713. {"SWR DAC_Port", "Switch", "IN"},
  714. {"SPKR", NULL, "SWR DAC_Port"},
  715. };
  716. int wsa883x_set_channel_map(struct snd_soc_component *component, u8 *port,
  717. u8 num_port, unsigned int *ch_mask,
  718. unsigned int *ch_rate, u8 *port_type)
  719. {
  720. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  721. int i;
  722. if (!port || !ch_mask || !ch_rate ||
  723. (num_port > WSA883X_MAX_SWR_PORTS)) {
  724. dev_err(component->dev,
  725. "%s: Invalid port=%pK, ch_mask=%pK, ch_rate=%pK\n",
  726. __func__, port, ch_mask, ch_rate);
  727. return -EINVAL;
  728. }
  729. for (i = 0; i < num_port; i++) {
  730. wsa883x->port[i].port_id = port[i];
  731. wsa883x->port[i].ch_mask = ch_mask[i];
  732. wsa883x->port[i].ch_rate = ch_rate[i];
  733. wsa883x->port[i].num_ch = __sw_hweight8(ch_mask[i]);
  734. if (port_type)
  735. wsa883x->port[i].port_type = port_type[i];
  736. }
  737. return 0;
  738. }
  739. EXPORT_SYMBOL(wsa883x_set_channel_map);
  740. static void wsa883x_codec_init(struct snd_soc_component *component)
  741. {
  742. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  743. if (!wsa883x)
  744. return;
  745. }
  746. static int32_t wsa883x_temp_reg_read(struct snd_soc_component *component,
  747. struct wsa_temp_register *wsa_temp_reg)
  748. {
  749. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  750. if (!wsa883x) {
  751. dev_err(component->dev, "%s: wsa883x is NULL\n", __func__);
  752. return -EINVAL;
  753. }
  754. mutex_lock(&wsa883x->res_lock);
  755. /* TODO Vote for global PA */
  756. snd_soc_component_update_bits(component, WSA883X_TADC_VALUE_CTL,
  757. 0x01, 0x00);
  758. wsa_temp_reg->dmeas_msb = snd_soc_component_read32(
  759. component, WSA883X_TEMP_MSB);
  760. wsa_temp_reg->dmeas_lsb = snd_soc_component_read32(
  761. component, WSA883X_TEMP_LSB);
  762. snd_soc_component_update_bits(component, WSA883X_TADC_VALUE_CTL,
  763. 0x01, 0x01);
  764. wsa_temp_reg->d1_msb = snd_soc_component_read32(
  765. component, WSA883X_OTP_REG_1);
  766. wsa_temp_reg->d1_lsb = snd_soc_component_read32(
  767. component, WSA883X_OTP_REG_2);
  768. wsa_temp_reg->d2_msb = snd_soc_component_read32(
  769. component, WSA883X_OTP_REG_3);
  770. wsa_temp_reg->d2_lsb = snd_soc_component_read32(
  771. component, WSA883X_OTP_REG_4);
  772. /* TODO Unvote for global PA */
  773. mutex_unlock(&wsa883x->res_lock);
  774. return 0;
  775. }
  776. static int wsa883x_get_temperature(struct snd_soc_component *component,
  777. int *temp)
  778. {
  779. struct snd_soc_component *component;
  780. struct wsa_temp_register reg;
  781. int dmeas, d1, d2;
  782. int ret = 0;
  783. int temp_val = 0;
  784. int t1 = T1_TEMP;
  785. int t2 = T2_TEMP;
  786. u8 retry = WSA883X_TEMP_RETRY;
  787. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  788. if (!wsa883x)
  789. return -EINVAL;
  790. do {
  791. ret = wsa883x_temp_reg_read(component, &reg)
  792. if (ret) {
  793. pr_err("%s: temp read failed: %d, current temp: %d\n",
  794. __func__, ret, wsa883x->curr_temp);
  795. if (temp)
  796. *temp = wsa883x->curr_temp;
  797. return 0;
  798. }
  799. /*
  800. * Temperature register values are expected to be in the
  801. * following range.
  802. * d1_msb = 68 - 92 and d1_lsb = 0, 64, 128, 192
  803. * d2_msb = 185 -218 and d2_lsb = 0, 64, 128, 192
  804. */
  805. if ((reg.d1_msb < 68 || reg.d1_msb > 92) ||
  806. (!(reg.d1_lsb == 0 || reg.d1_lsb == 64 || reg.d1_lsb == 128 ||
  807. reg.d1_lsb == 192)) ||
  808. (reg.d2_msb < 185 || reg.d2_msb > 218) ||
  809. (!(reg.d2_lsb == 0 || reg.d2_lsb == 64 || reg.d2_lsb == 128 ||
  810. reg.d2_lsb == 192))) {
  811. printk_ratelimited("%s: Temperature registers[%d %d %d %d] are out of range\n",
  812. __func__, reg.d1_msb, reg.d1_lsb, reg.d2_msb,
  813. reg.d2_lsb);
  814. }
  815. dmeas = ((reg.dmeas_msb << 0x8) | reg.dmeas_lsb) >> 0x6;
  816. d1 = ((reg.d1_msb << 0x8) | reg.d1_lsb) >> 0x6;
  817. d2 = ((reg.d2_msb << 0x8) | reg.d2_lsb) >> 0x6;
  818. if (d1 == d2)
  819. temp_val = TEMP_INVALID;
  820. else
  821. temp_val = t1 + (((dmeas - d1) * (t2 - t1))/(d2 - d1));
  822. if (temp_val <= LOW_TEMP_THRESHOLD ||
  823. temp_val >= HIGH_TEMP_THRESHOLD) {
  824. pr_debug("%s: T0: %d is out of range[%d, %d]\n", __func__,
  825. temp_val, LOW_TEMP_THRESHOLD, HIGH_TEMP_THRESHOLD);
  826. if (retry--)
  827. msleep(10);
  828. } else {
  829. break;
  830. }
  831. } while (retry);
  832. wsa883x->curr_temp = temp_val;
  833. if (temp)
  834. *temp = temp_val;
  835. pr_debug("%s: t0 measured: %d dmeas = %d, d1 = %d, d2 = %d\n",
  836. __func__, temp_val, dmeas, d1, d2);
  837. return ret;
  838. }
  839. static int wsa883x_codec_probe(struct snd_soc_component *component)
  840. {
  841. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  842. struct swr_device *dev;
  843. if (!wsa883x)
  844. return -EINVAL;
  845. snd_soc_component_init_regmap(component, wsa883x->regmap);
  846. dev = wsa883x->swr_slave;
  847. wsa883x->component = component;
  848. wsa883x_codec_init(component);
  849. wsa883x->global_pa_cnt = 0;
  850. return 0;
  851. }
  852. static void wsa883x_codec_remove(struct snd_soc_component *component)
  853. {
  854. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  855. if (!wsa883x)
  856. return;
  857. snd_soc_component_exit_regmap(component);
  858. return;
  859. }
  860. static const struct snd_soc_component_driver soc_codec_dev_wsa883x = {
  861. .name = DRV_NAME,
  862. .probe = wsa883x_codec_probe,
  863. .remove = wsa883x_codec_remove,
  864. .controls = wsa883x_snd_controls,
  865. .num_controls = ARRAY_SIZE(wsa883x_snd_controls),
  866. .dapm_widgets = wsa883x_dapm_widgets,
  867. .num_dapm_widgets = ARRAY_SIZE(wsa883x_dapm_widgets),
  868. .dapm_routes = wsa883x_audio_map,
  869. .num_dapm_routes = ARRAY_SIZE(wsa883x_audio_map),
  870. };
  871. static int wsa883x_gpio_ctrl(struct wsa883x_priv *wsa883x, bool enable)
  872. {
  873. int ret = 0;
  874. if (enable)
  875. ret = msm_cdc_pinctrl_select_active_state(
  876. wsa883x->wsa_rst_np);
  877. else
  878. ret = msm_cdc_pinctrl_select_sleep_state(
  879. wsa883x->wsa_rst_np);
  880. if (ret != 0)
  881. dev_err(wsa883x->dev,
  882. "%s: Failed to turn state %d; ret=%d\n",
  883. __func__, enable, ret);
  884. return ret;
  885. }
  886. static int wsa883x_swr_probe(struct swr_device *pdev)
  887. {
  888. int ret = 0;
  889. struct wsa883x_priv *wsa883x;
  890. u8 devnum = 0;
  891. bool pin_state_current = false;
  892. wsa883x = devm_kzalloc(&pdev->dev, sizeof(struct wsa883x_priv),
  893. GFP_KERNEL);
  894. if (!wsa883x)
  895. return -ENOMEM;
  896. wsa883x->wsa_rst_np = of_parse_phandle(pdev->dev.of_node,
  897. "qcom,spkr-sd-n-node", 0);
  898. if (!wsa883x->wsa_rst_np) {
  899. dev_dbg(&pdev->dev, "%s: pinctrl not defined\n", __func__);
  900. goto err;
  901. }
  902. swr_set_dev_data(pdev, wsa883x);
  903. wsa883x->swr_slave = pdev;
  904. pin_state_current = msm_cdc_pinctrl_get_state(wsa883x->wsa_rst_np);
  905. wsa883x_gpio_ctrl(wsa883x, true);
  906. /*
  907. * Add 5msec delay to provide sufficient time for
  908. * soundwire auto enumeration of slave devices as
  909. * as per HW requirement.
  910. */
  911. usleep_range(5000, 5010);
  912. ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
  913. if (ret) {
  914. dev_dbg(&pdev->dev,
  915. "%s get devnum %d for dev addr %lx failed\n",
  916. __func__, devnum, pdev->addr);
  917. goto dev_err;
  918. }
  919. pdev->dev_num = devnum;
  920. wsa883x->regmap = devm_regmap_init_swr(pdev,
  921. &wsa883x_regmap_config);
  922. if (IS_ERR(wsa883x->regmap)) {
  923. ret = PTR_ERR(wsa883x->regmap);
  924. dev_err(&pdev->dev, "%s: regmap_init failed %d\n",
  925. __func__, ret);
  926. goto dev_err;
  927. }
  928. /* Set all interrupts as edge triggered */
  929. for (i = 0; i < wsa883x_regmap_irq_chip.num_regs; i++)
  930. regmap_write(wsa883x->regmap, (WSA883X_INTR_LEVEL0 + i), 0);
  931. wsa883x_regmap_irq_chip.irq_drv_data = wsa883x;
  932. wsa883x->irq_info.wcd_regmap_irq_chip = &wsa883x_regmap_irq_chip;
  933. wsa883x->irq_info.codec_name = "WSA883X";
  934. wsa883x->irq_info.regmap = wsa883x->regmap;
  935. wsa883x->irq_info.dev = dev;
  936. ret = wcd_irq_init(&wsa883x->irq_info, &wsa883x->virq);
  937. if (ret) {
  938. dev_err(wsa883x->dev, "%s: IRQ init failed: %d\n",
  939. __func__, ret);
  940. goto dev_err;
  941. }
  942. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_SAF2WAR,
  943. "WSA SAF2WAR", wsa883x_saf2war_handle_irq, NULL);
  944. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_WAR2SAF,
  945. "WSA WAR2SAF", wsa883x_war2saf_handle_irq, NULL);
  946. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_DISABLE,
  947. "WSA OTP", wsa883x_otp_handle_irq, NULL);
  948. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_OCP,
  949. "WSA OCP", wsa883x_ocp_handle_irq, NULL);
  950. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLIP,
  951. "WSA CLIP", wsa883x_clip_handle_irq, NULL);
  952. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PDM_WD,
  953. "WSA PDM WD", wsa883x_pdm_wd_handle_irq, NULL);
  954. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLK_WD,
  955. "WSA CLK WD", wsa883x_clk_wd_handle_irq, NULL);
  956. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_INTR_PIN,
  957. "WSA EXT INT", wsa883x_ext_int_handle_irq, NULL);
  958. /* Under Voltage Lock out (UVLO) interrupt handle */
  959. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_UVLO,
  960. "WSA UVLO", wsa883x_uvlo_handle_irq, NULL);
  961. wcd_request_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PA_ON_ERR,
  962. "WSA PA ERR", wsa883x_pa_on_err_handle_irq, NULL);
  963. ret = snd_soc_register_component(&pdev->dev, &soc_codec_dev_wsa883x,
  964. NULL, 0);
  965. if (ret) {
  966. dev_err(&pdev->dev, "%s: Codec registration failed\n",
  967. __func__);
  968. goto err_irq;
  969. }
  970. mutex_init(&wsa883x->res_lock);
  971. #ifdef CONFIG_DEBUG_FS
  972. if (!wsa883x->debugfs_dent) {
  973. wsa883x->debugfs_dent = debugfs_create_dir(
  974. dev_name(&pdev->dev), 0);
  975. if (!IS_ERR(wsa883x->debugfs_dent)) {
  976. wsa883x->debugfs_peek =
  977. debugfs_create_file("swrslave_peek",
  978. S_IFREG | 0444,
  979. wsa883x->debugfs_dent,
  980. (void *) pdev,
  981. &codec_debug_read_ops);
  982. wsa883x->debugfs_poke =
  983. debugfs_create_file("swrslave_poke",
  984. S_IFREG | 0444,
  985. wsa883x->debugfs_dent,
  986. (void *) pdev,
  987. &codec_debug_write_ops);
  988. wsa883x->debugfs_reg_dump =
  989. debugfs_create_file(
  990. "swrslave_reg_dump",
  991. S_IFREG | 0444,
  992. wsa883x->debugfs_dent,
  993. (void *) pdev,
  994. &codec_debug_dump_ops);
  995. }
  996. }
  997. #endif
  998. return 0;
  999. err_irq:
  1000. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_SAF2WAR, NULL);
  1001. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_WAR2SAF, NULL);
  1002. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_DISABLE, NULL);
  1003. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_OCP, NULL);
  1004. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLIP, NULL);
  1005. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PDM_WD, NULL);
  1006. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLK_WD, NULL);
  1007. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_INTR_PIN, NULL);
  1008. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_UVLO, NULL);
  1009. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PA_ON_ERR, NULL);
  1010. wcd_irq_exit(&wsa883x->irq_info, wsa883x->virq);
  1011. dev_err:
  1012. if (pin_state_current == false)
  1013. wsa883x_gpio_ctrl(wsa883x, false);
  1014. swr_remove_device(pdev);
  1015. err:
  1016. return ret;
  1017. }
  1018. static int wsa883x_swr_remove(struct swr_device *pdev)
  1019. {
  1020. struct wsa883x_priv *wsa883x;
  1021. wsa883x = swr_get_dev_data(pdev);
  1022. if (!wsa883x) {
  1023. dev_err(&pdev->dev, "%s: wsa883x is NULL\n", __func__);
  1024. return -EINVAL;
  1025. }
  1026. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_SAF2WAR, NULL);
  1027. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_WAR2SAF, NULL);
  1028. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_DISABLE, NULL);
  1029. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_OCP, NULL);
  1030. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLIP, NULL);
  1031. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PDM_WD, NULL);
  1032. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_CLK_WD, NULL);
  1033. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_INTR_PIN, NULL);
  1034. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_UVLO, NULL);
  1035. wcd_free_irq(&wsa883x->irq_info, WSA883X_IRQ_INT_PA_ON_ERR, NULL);
  1036. #ifdef CONFIG_DEBUG_FS
  1037. debugfs_remove_recursive(wsa883x->debugfs_dent);
  1038. wsa883x->debugfs_dent = NULL;
  1039. #endif
  1040. mutex_destroy(&wsa883x->res_lock);
  1041. snd_soc_unregister_component(&pdev->dev);
  1042. swr_set_dev_data(pdev, NULL);
  1043. return 0;
  1044. }
  1045. #ifdef CONFIG_PM_SLEEP
  1046. static int wsa883x_swr_suspend(struct device *dev)
  1047. {
  1048. dev_dbg(dev, "%s: system suspend\n", __func__);
  1049. return 0;
  1050. }
  1051. static int wsa883x_swr_resume(struct device *dev)
  1052. {
  1053. struct wsa883x_priv *wsa883x = swr_get_dev_data(to_swr_device(dev));
  1054. if (!wsa883x) {
  1055. dev_err(dev, "%s: wsa883x private data is NULL\n", __func__);
  1056. return -EINVAL;
  1057. }
  1058. dev_dbg(dev, "%s: system resume\n", __func__);
  1059. return 0;
  1060. }
  1061. #endif /* CONFIG_PM_SLEEP */
  1062. static const struct dev_pm_ops wsa883x_swr_pm_ops = {
  1063. SET_SYSTEM_SLEEP_PM_OPS(wsa883x_swr_suspend, wsa883x_swr_resume)
  1064. };
  1065. static const struct swr_device_id wsa883x_swr_id[] = {
  1066. {"wsa883x", 0},
  1067. {}
  1068. };
  1069. static const struct of_device_id wsa883x_swr_dt_match[] = {
  1070. {
  1071. .compatible = "qcom,wsa883x",
  1072. },
  1073. {}
  1074. };
  1075. static struct swr_driver wsa883x_swr_driver = {
  1076. .driver = {
  1077. .name = "wsa883x",
  1078. .owner = THIS_MODULE,
  1079. .pm = &wsa883x_swr_pm_ops,
  1080. .of_match_table = wsa883x_swr_dt_match,
  1081. },
  1082. .probe = wsa883x_swr_probe,
  1083. .remove = wsa883x_swr_remove,
  1084. .id_table = wsa883x_swr_id,
  1085. };
  1086. static int __init wsa883x_swr_init(void)
  1087. {
  1088. return swr_driver_register(&wsa883x_swr_driver);
  1089. }
  1090. static void __exit wsa883x_swr_exit(void)
  1091. {
  1092. swr_driver_unregister(&wsa883x_swr_driver);
  1093. }
  1094. module_init(wsa883x_swr_init);
  1095. module_exit(wsa883x_swr_exit);
  1096. MODULE_DESCRIPTION("WSA883x codec driver");
  1097. MODULE_LICENSE("GPL v2");