wsa881x.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. /*
  2. * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/device.h>
  18. #include <linux/printk.h>
  19. #include <linux/bitops.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/gpio.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/regmap.h>
  27. #include <linux/debugfs.h>
  28. #include <soc/soundwire.h>
  29. #include <sound/pcm.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/soc.h>
  32. #include <sound/soc-dapm.h>
  33. #include <sound/tlv.h>
  34. #include "msm-cdc-pinctrl.h"
  35. #include "wsa881x.h"
  36. #include "wsa881x-temp-sensor.h"
  37. #define WSA881X_NUM_RETRY 5
  38. enum {
  39. G_18DB = 0,
  40. G_16P5DB,
  41. G_15DB,
  42. G_13P5DB,
  43. G_12DB,
  44. G_10P5DB,
  45. G_9DB,
  46. G_7P5DB,
  47. G_6DB,
  48. G_4P5DB,
  49. G_3DB,
  50. G_1P5DB,
  51. G_0DB,
  52. };
  53. enum {
  54. DISABLE = 0,
  55. ENABLE,
  56. };
  57. enum {
  58. SWR_DAC_PORT,
  59. SWR_COMP_PORT,
  60. SWR_BOOST_PORT,
  61. SWR_VISENSE_PORT,
  62. };
  63. struct swr_port {
  64. u8 port_id;
  65. u8 ch_mask;
  66. u32 ch_rate;
  67. u8 num_ch;
  68. };
  69. enum {
  70. WSA881X_DEV_DOWN,
  71. WSA881X_DEV_UP,
  72. };
  73. /*
  74. * Private data Structure for wsa881x. All parameters related to
  75. * WSA881X codec needs to be defined here.
  76. */
  77. struct wsa881x_priv {
  78. struct regmap *regmap;
  79. struct device *dev;
  80. struct swr_device *swr_slave;
  81. struct snd_soc_codec *codec;
  82. bool comp_enable;
  83. bool boost_enable;
  84. bool visense_enable;
  85. u8 pa_gain;
  86. struct swr_port port[WSA881X_MAX_SWR_PORTS];
  87. int pd_gpio;
  88. struct wsa881x_tz_priv tz_pdata;
  89. int bg_cnt;
  90. int clk_cnt;
  91. int version;
  92. struct mutex bg_lock;
  93. struct mutex res_lock;
  94. struct snd_info_entry *entry;
  95. struct snd_info_entry *version_entry;
  96. int state;
  97. struct delayed_work ocp_ctl_work;
  98. struct device_node *wsa_rst_np;
  99. int pa_mute;
  100. };
  101. #define SWR_SLV_MAX_REG_ADDR 0x390
  102. #define SWR_SLV_START_REG_ADDR 0x40
  103. #define SWR_SLV_MAX_BUF_LEN 20
  104. #define BYTES_PER_LINE 12
  105. #define SWR_SLV_RD_BUF_LEN 8
  106. #define SWR_SLV_WR_BUF_LEN 32
  107. #define SWR_SLV_MAX_DEVICES 2
  108. #define WSA881X_VERSION_ENTRY_SIZE 27
  109. #define WSA881X_OCP_CTL_TIMER_SEC 2
  110. #define WSA881X_OCP_CTL_TEMP_CELSIUS 25
  111. #define WSA881X_OCP_CTL_POLL_TIMER_SEC 60
  112. static int wsa881x_ocp_poll_timer_sec = WSA881X_OCP_CTL_POLL_TIMER_SEC;
  113. module_param(wsa881x_ocp_poll_timer_sec, int, 0664);
  114. MODULE_PARM_DESC(wsa881x_ocp_poll_timer_sec, "timer for ocp ctl polling");
  115. static struct wsa881x_priv *dbgwsa881x;
  116. static struct dentry *debugfs_wsa881x_dent;
  117. static struct dentry *debugfs_peek;
  118. static struct dentry *debugfs_poke;
  119. static struct dentry *debugfs_reg_dump;
  120. static unsigned int read_data;
  121. static unsigned int devnum;
  122. static int32_t wsa881x_resource_acquire(struct snd_soc_codec *codec,
  123. bool enable);
  124. static const char * const wsa_pa_gain_text[] = {
  125. "G_18_DB", "G_16P5_DB", "G_15_DB", "G_13P5_DB", "G_12_DB", "G_10P5_DB",
  126. "G_9_DB", "G_7P5_DB", "G_6_DB", "G_4P5_DB", "G_3_DB", "G_1P5_DB",
  127. "G_0_DB"
  128. };
  129. static const struct soc_enum wsa_pa_gain_enum =
  130. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_pa_gain_text), wsa_pa_gain_text);
  131. static int wsa_pa_gain_get(struct snd_kcontrol *kcontrol,
  132. struct snd_ctl_elem_value *ucontrol)
  133. {
  134. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  135. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  136. ucontrol->value.integer.value[0] = wsa881x->pa_gain;
  137. dev_dbg(codec->dev, "%s: PA gain = 0x%x\n", __func__, wsa881x->pa_gain);
  138. return 0;
  139. }
  140. static int wsa_pa_gain_put(struct snd_kcontrol *kcontrol,
  141. struct snd_ctl_elem_value *ucontrol)
  142. {
  143. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  144. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  145. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  146. __func__, ucontrol->value.integer.value[0]);
  147. wsa881x->pa_gain = ucontrol->value.integer.value[0];
  148. return 0;
  149. }
  150. static int wsa881x_get_mute(struct snd_kcontrol *kcontrol,
  151. struct snd_ctl_elem_value *ucontrol)
  152. {
  153. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  154. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  155. ucontrol->value.integer.value[0] = wsa881x->pa_mute;
  156. return 0;
  157. }
  158. static int wsa881x_set_mute(struct snd_kcontrol *kcontrol,
  159. struct snd_ctl_elem_value *ucontrol)
  160. {
  161. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  162. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  163. int value = ucontrol->value.integer.value[0];
  164. dev_dbg(codec->dev, "%s: mute current %d, new %d\n",
  165. __func__, wsa881x->pa_mute, value);
  166. if (value)
  167. snd_soc_update_bits(codec, WSA881X_SPKR_DRV_EN, 0x80, 0x00);
  168. wsa881x->pa_mute = value;
  169. return 0;
  170. }
  171. static const struct snd_kcontrol_new wsa_snd_controls[] = {
  172. SOC_ENUM_EXT("WSA PA Gain", wsa_pa_gain_enum,
  173. wsa_pa_gain_get, wsa_pa_gain_put),
  174. SOC_SINGLE_EXT("WSA PA Mute", SND_SOC_NOPM, 0, 1, 0,
  175. wsa881x_get_mute, wsa881x_set_mute),
  176. };
  177. static int codec_debug_open(struct inode *inode, struct file *file)
  178. {
  179. file->private_data = inode->i_private;
  180. return 0;
  181. }
  182. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  183. {
  184. char *token;
  185. int base, cnt;
  186. token = strsep(&buf, " ");
  187. for (cnt = 0; cnt < num_of_par; cnt++) {
  188. if (token) {
  189. if ((token[1] == 'x') || (token[1] == 'X'))
  190. base = 16;
  191. else
  192. base = 10;
  193. if (kstrtou32(token, base, &param1[cnt]) != 0)
  194. return -EINVAL;
  195. token = strsep(&buf, " ");
  196. } else
  197. return -EINVAL;
  198. }
  199. return 0;
  200. }
  201. static ssize_t wsa881x_codec_version_read(struct snd_info_entry *entry,
  202. void *file_private_data, struct file *file,
  203. char __user *buf, size_t count, loff_t pos)
  204. {
  205. struct wsa881x_priv *wsa881x;
  206. char buffer[WSA881X_VERSION_ENTRY_SIZE];
  207. int len;
  208. wsa881x = (struct wsa881x_priv *) entry->private_data;
  209. if (!wsa881x) {
  210. pr_err("%s: wsa881x priv is null\n", __func__);
  211. return -EINVAL;
  212. }
  213. len = snprintf(buffer, sizeof(buffer), "WSA881X-SOUNDWIRE_2_0\n");
  214. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  215. }
  216. static struct snd_info_entry_ops wsa881x_codec_info_ops = {
  217. .read = wsa881x_codec_version_read,
  218. };
  219. /*
  220. * wsa881x_codec_info_create_codec_entry - creates wsa881x module
  221. * @codec_root: The parent directory
  222. * @codec: Codec instance
  223. *
  224. * Creates wsa881x module and version entry under the given
  225. * parent directory.
  226. *
  227. * Return: 0 on success or negative error code on failure.
  228. */
  229. int wsa881x_codec_info_create_codec_entry(struct snd_info_entry *codec_root,
  230. struct snd_soc_codec *codec)
  231. {
  232. struct snd_info_entry *version_entry;
  233. struct wsa881x_priv *wsa881x;
  234. struct snd_soc_card *card;
  235. char name[80];
  236. if (!codec_root || !codec)
  237. return -EINVAL;
  238. wsa881x = snd_soc_codec_get_drvdata(codec);
  239. card = codec->component.card;
  240. snprintf(name, sizeof(name), "%s.%x", "wsa881x",
  241. (u32)wsa881x->swr_slave->addr);
  242. wsa881x->entry = snd_info_create_subdir(codec_root->module,
  243. (const char *)name,
  244. codec_root);
  245. if (!wsa881x->entry) {
  246. dev_dbg(codec->dev, "%s: failed to create wsa881x entry\n",
  247. __func__);
  248. return -ENOMEM;
  249. }
  250. version_entry = snd_info_create_card_entry(card->snd_card,
  251. "version",
  252. wsa881x->entry);
  253. if (!version_entry) {
  254. dev_dbg(codec->dev, "%s: failed to create wsa881x version entry\n",
  255. __func__);
  256. return -ENOMEM;
  257. }
  258. version_entry->private_data = wsa881x;
  259. version_entry->size = WSA881X_VERSION_ENTRY_SIZE;
  260. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  261. version_entry->c.ops = &wsa881x_codec_info_ops;
  262. if (snd_info_register(version_entry) < 0) {
  263. snd_info_free_entry(version_entry);
  264. return -ENOMEM;
  265. }
  266. wsa881x->version_entry = version_entry;
  267. return 0;
  268. }
  269. EXPORT_SYMBOL(wsa881x_codec_info_create_codec_entry);
  270. static bool is_swr_slv_reg_readable(int reg)
  271. {
  272. bool ret = true;
  273. if (((reg > 0x46) && (reg < 0x4A)) ||
  274. ((reg > 0x4A) && (reg < 0x50)) ||
  275. ((reg > 0x55) && (reg < 0xE0)) ||
  276. ((reg > 0xE0) && (reg < 0xF0)) ||
  277. ((reg > 0xF0) && (reg < 0x100)) ||
  278. ((reg > 0x105) && (reg < 0x120)) ||
  279. ((reg > 0x128) && (reg < 0x130)) ||
  280. ((reg > 0x138) && (reg < 0x200)) ||
  281. ((reg > 0x205) && (reg < 0x220)) ||
  282. ((reg > 0x228) && (reg < 0x230)) ||
  283. ((reg > 0x238) && (reg < 0x300)) ||
  284. ((reg > 0x305) && (reg < 0x320)) ||
  285. ((reg > 0x328) && (reg < 0x330)) ||
  286. ((reg > 0x338) && (reg < 0x400)) ||
  287. ((reg > 0x405) && (reg < 0x420)))
  288. ret = false;
  289. return ret;
  290. }
  291. static ssize_t wsa881x_swrslave_reg_show(char __user *ubuf, size_t count,
  292. loff_t *ppos)
  293. {
  294. int i, reg_val, len;
  295. ssize_t total = 0;
  296. char tmp_buf[SWR_SLV_MAX_BUF_LEN];
  297. if (!ubuf || !ppos || (devnum == 0))
  298. return 0;
  299. for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_SLV_START_REG_ADDR);
  300. i <= SWR_SLV_MAX_REG_ADDR; i++) {
  301. if (!is_swr_slv_reg_readable(i))
  302. continue;
  303. swr_read(dbgwsa881x->swr_slave, devnum,
  304. i, &reg_val, 1);
  305. len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i,
  306. (reg_val & 0xFF));
  307. if ((total + len) >= count - 1)
  308. break;
  309. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  310. pr_err("%s: fail to copy reg dump\n", __func__);
  311. total = -EFAULT;
  312. goto copy_err;
  313. }
  314. *ppos += len;
  315. total += len;
  316. }
  317. copy_err:
  318. return total;
  319. }
  320. static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
  321. size_t count, loff_t *ppos)
  322. {
  323. char lbuf[SWR_SLV_RD_BUF_LEN];
  324. char *access_str;
  325. ssize_t ret_cnt;
  326. if (!count || !file || !ppos || !ubuf)
  327. return -EINVAL;
  328. access_str = file->private_data;
  329. if (*ppos < 0)
  330. return -EINVAL;
  331. if (!strcmp(access_str, "swrslave_peek")) {
  332. snprintf(lbuf, sizeof(lbuf), "0x%x\n", (read_data & 0xFF));
  333. ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
  334. strnlen(lbuf, 7));
  335. } else if (!strcmp(access_str, "swrslave_reg_dump")) {
  336. ret_cnt = wsa881x_swrslave_reg_show(ubuf, count, ppos);
  337. } else {
  338. pr_err("%s: %s not permitted to read\n", __func__, access_str);
  339. ret_cnt = -EPERM;
  340. }
  341. return ret_cnt;
  342. }
  343. static ssize_t codec_debug_write(struct file *filp,
  344. const char __user *ubuf, size_t cnt, loff_t *ppos)
  345. {
  346. char lbuf[SWR_SLV_WR_BUF_LEN];
  347. int rc;
  348. u32 param[5];
  349. char *access_str;
  350. if (!filp || !ppos || !ubuf)
  351. return -EINVAL;
  352. access_str = filp->private_data;
  353. if (cnt > sizeof(lbuf) - 1)
  354. return -EINVAL;
  355. rc = copy_from_user(lbuf, ubuf, cnt);
  356. if (rc)
  357. return -EFAULT;
  358. lbuf[cnt] = '\0';
  359. if (!strcmp(access_str, "swrslave_poke")) {
  360. /* write */
  361. rc = get_parameters(lbuf, param, 3);
  362. if ((param[0] <= SWR_SLV_MAX_REG_ADDR) && (param[1] <= 0xFF) &&
  363. (rc == 0))
  364. swr_write(dbgwsa881x->swr_slave, param[2],
  365. param[0], &param[1]);
  366. else
  367. rc = -EINVAL;
  368. } else if (!strcmp(access_str, "swrslave_peek")) {
  369. /* read */
  370. rc = get_parameters(lbuf, param, 2);
  371. if ((param[0] <= SWR_SLV_MAX_REG_ADDR) && (rc == 0))
  372. swr_read(dbgwsa881x->swr_slave, param[1],
  373. param[0], &read_data, 1);
  374. else
  375. rc = -EINVAL;
  376. } else if (!strcmp(access_str, "swrslave_reg_dump")) {
  377. /* reg dump */
  378. rc = get_parameters(lbuf, param, 1);
  379. if ((rc == 0) && (param[0] > 0) &&
  380. (param[0] <= SWR_SLV_MAX_DEVICES))
  381. devnum = param[0];
  382. else
  383. rc = -EINVAL;
  384. }
  385. if (rc == 0)
  386. rc = cnt;
  387. else
  388. pr_err("%s: rc = %d\n", __func__, rc);
  389. return rc;
  390. }
  391. static const struct file_operations codec_debug_ops = {
  392. .open = codec_debug_open,
  393. .write = codec_debug_write,
  394. .read = codec_debug_read,
  395. };
  396. static const struct reg_sequence wsa881x_pre_pmu_pa[] = {
  397. {WSA881X_SPKR_DRV_GAIN, 0x41, 0},
  398. {WSA881X_SPKR_MISC_CTL1, 0x01, 0},
  399. {WSA881X_ADC_EN_DET_TEST_I, 0x01, 0},
  400. {WSA881X_ADC_EN_MODU_V, 0x02, 0},
  401. {WSA881X_ADC_EN_DET_TEST_V, 0x10, 0},
  402. {WSA881X_SPKR_PWRSTG_DBG, 0xA0, 0},
  403. };
  404. static const struct reg_sequence wsa881x_pre_pmu_pa_2_0[] = {
  405. {WSA881X_SPKR_DRV_GAIN, 0x41, 0},
  406. {WSA881X_SPKR_MISC_CTL1, 0x87, 0},
  407. };
  408. static const struct reg_sequence wsa881x_post_pmu_pa[] = {
  409. {WSA881X_SPKR_PWRSTG_DBG, 0x00, 0},
  410. {WSA881X_ADC_EN_DET_TEST_V, 0x00, 0},
  411. {WSA881X_ADC_EN_MODU_V, 0x00, 0},
  412. {WSA881X_ADC_EN_DET_TEST_I, 0x00, 0},
  413. };
  414. static const struct reg_sequence wsa881x_vi_txfe_en[] = {
  415. {WSA881X_SPKR_PROT_FE_VSENSE_VCM, 0x85, 0},
  416. {WSA881X_SPKR_PROT_ATEST2, 0x0A, 0},
  417. {WSA881X_SPKR_PROT_FE_GAIN, 0xCF, 0},
  418. };
  419. static const struct reg_sequence wsa881x_vi_txfe_en_2_0[] = {
  420. {WSA881X_SPKR_PROT_FE_VSENSE_VCM, 0x85, 0},
  421. {WSA881X_SPKR_PROT_ATEST2, 0x0A, 0},
  422. {WSA881X_SPKR_PROT_FE_GAIN, 0x47, 0},
  423. };
  424. static int wsa881x_boost_ctrl(struct snd_soc_codec *codec, bool enable)
  425. {
  426. dev_dbg(codec->dev, "%s: enable:%d\n", __func__, enable);
  427. if (enable)
  428. snd_soc_update_bits(codec, WSA881X_BOOST_EN_CTL, 0x80, 0x80);
  429. else
  430. snd_soc_update_bits(codec, WSA881X_BOOST_EN_CTL, 0x80, 0x00);
  431. /*
  432. * 1.5ms sleep is needed after boost enable/disable as per
  433. * HW requirement
  434. */
  435. usleep_range(1500, 1510);
  436. return 0;
  437. }
  438. static int wsa881x_visense_txfe_ctrl(struct snd_soc_codec *codec, bool enable,
  439. u8 isense1_gain, u8 isense2_gain,
  440. u8 vsense_gain)
  441. {
  442. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  443. dev_dbg(codec->dev,
  444. "%s: enable:%d, isense1 gain: %d, isense2 gain: %d, vsense_gain %d\n",
  445. __func__, enable, isense1_gain, isense2_gain, vsense_gain);
  446. if (enable) {
  447. regmap_multi_reg_write(wsa881x->regmap,
  448. wsa881x_vi_txfe_en_2_0,
  449. ARRAY_SIZE(wsa881x_vi_txfe_en_2_0));
  450. } else {
  451. snd_soc_update_bits(codec, WSA881X_SPKR_PROT_FE_VSENSE_VCM,
  452. 0x08, 0x08);
  453. /*
  454. * 200us sleep is needed after visense txfe disable as per
  455. * HW requirement.
  456. */
  457. usleep_range(200, 210);
  458. snd_soc_update_bits(codec, WSA881X_SPKR_PROT_FE_GAIN,
  459. 0x01, 0x00);
  460. }
  461. return 0;
  462. }
  463. static int wsa881x_visense_adc_ctrl(struct snd_soc_codec *codec, bool enable)
  464. {
  465. dev_dbg(codec->dev, "%s: enable:%d\n", __func__, enable);
  466. snd_soc_update_bits(codec, WSA881X_ADC_EN_MODU_V, (0x01 << 7),
  467. (enable << 7));
  468. snd_soc_update_bits(codec, WSA881X_ADC_EN_MODU_I, (0x01 << 7),
  469. (enable << 7));
  470. return 0;
  471. }
  472. static void wsa881x_bandgap_ctrl(struct snd_soc_codec *codec, bool enable)
  473. {
  474. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  475. dev_dbg(codec->dev, "%s: enable:%d, bg_count:%d\n", __func__,
  476. enable, wsa881x->bg_cnt);
  477. mutex_lock(&wsa881x->bg_lock);
  478. if (enable) {
  479. ++wsa881x->bg_cnt;
  480. if (wsa881x->bg_cnt == 1) {
  481. snd_soc_update_bits(codec, WSA881X_TEMP_OP,
  482. 0x08, 0x08);
  483. /* 400usec sleep is needed as per HW requirement */
  484. usleep_range(400, 410);
  485. snd_soc_update_bits(codec, WSA881X_TEMP_OP,
  486. 0x04, 0x04);
  487. }
  488. } else {
  489. --wsa881x->bg_cnt;
  490. if (wsa881x->bg_cnt <= 0) {
  491. WARN_ON(wsa881x->bg_cnt < 0);
  492. wsa881x->bg_cnt = 0;
  493. snd_soc_update_bits(codec, WSA881X_TEMP_OP, 0x04, 0x00);
  494. snd_soc_update_bits(codec, WSA881X_TEMP_OP, 0x08, 0x00);
  495. }
  496. }
  497. mutex_unlock(&wsa881x->bg_lock);
  498. }
  499. static void wsa881x_clk_ctrl(struct snd_soc_codec *codec, bool enable)
  500. {
  501. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  502. dev_dbg(codec->dev, "%s: enable:%d, clk_count:%d\n", __func__,
  503. enable, wsa881x->clk_cnt);
  504. mutex_lock(&wsa881x->res_lock);
  505. if (enable) {
  506. ++wsa881x->clk_cnt;
  507. if (wsa881x->clk_cnt == 1) {
  508. snd_soc_write(codec, WSA881X_CDC_DIG_CLK_CTL, 0x01);
  509. snd_soc_write(codec, WSA881X_CDC_ANA_CLK_CTL, 0x01);
  510. }
  511. } else {
  512. --wsa881x->clk_cnt;
  513. if (wsa881x->clk_cnt <= 0) {
  514. WARN_ON(wsa881x->clk_cnt < 0);
  515. wsa881x->clk_cnt = 0;
  516. snd_soc_write(codec, WSA881X_CDC_DIG_CLK_CTL, 0x00);
  517. snd_soc_write(codec, WSA881X_CDC_ANA_CLK_CTL, 0x00);
  518. }
  519. }
  520. mutex_unlock(&wsa881x->res_lock);
  521. }
  522. static int wsa881x_get_compander(struct snd_kcontrol *kcontrol,
  523. struct snd_ctl_elem_value *ucontrol)
  524. {
  525. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  526. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  527. ucontrol->value.integer.value[0] = wsa881x->comp_enable;
  528. return 0;
  529. }
  530. static int wsa881x_set_compander(struct snd_kcontrol *kcontrol,
  531. struct snd_ctl_elem_value *ucontrol)
  532. {
  533. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  534. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  535. int value = ucontrol->value.integer.value[0];
  536. dev_dbg(codec->dev, "%s: Compander enable current %d, new %d\n",
  537. __func__, wsa881x->comp_enable, value);
  538. wsa881x->comp_enable = value;
  539. return 0;
  540. }
  541. static int wsa881x_get_boost(struct snd_kcontrol *kcontrol,
  542. struct snd_ctl_elem_value *ucontrol)
  543. {
  544. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  545. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  546. ucontrol->value.integer.value[0] = wsa881x->boost_enable;
  547. return 0;
  548. }
  549. static int wsa881x_set_boost(struct snd_kcontrol *kcontrol,
  550. struct snd_ctl_elem_value *ucontrol)
  551. {
  552. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  553. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  554. int value = ucontrol->value.integer.value[0];
  555. dev_dbg(codec->dev, "%s: Boost enable current %d, new %d\n",
  556. __func__, wsa881x->boost_enable, value);
  557. wsa881x->boost_enable = value;
  558. return 0;
  559. }
  560. static int wsa881x_get_visense(struct snd_kcontrol *kcontrol,
  561. struct snd_ctl_elem_value *ucontrol)
  562. {
  563. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  564. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  565. ucontrol->value.integer.value[0] = wsa881x->visense_enable;
  566. return 0;
  567. }
  568. static int wsa881x_set_visense(struct snd_kcontrol *kcontrol,
  569. struct snd_ctl_elem_value *ucontrol)
  570. {
  571. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  572. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  573. int value = ucontrol->value.integer.value[0];
  574. dev_dbg(codec->dev, "%s: VIsense enable current %d, new %d\n",
  575. __func__, wsa881x->visense_enable, value);
  576. wsa881x->visense_enable = value;
  577. return 0;
  578. }
  579. static const struct snd_kcontrol_new wsa881x_snd_controls[] = {
  580. SOC_SINGLE_EXT("COMP Switch", SND_SOC_NOPM, 0, 1, 0,
  581. wsa881x_get_compander, wsa881x_set_compander),
  582. SOC_SINGLE_EXT("BOOST Switch", SND_SOC_NOPM, 0, 1, 0,
  583. wsa881x_get_boost, wsa881x_set_boost),
  584. SOC_SINGLE_EXT("VISENSE Switch", SND_SOC_NOPM, 0, 1, 0,
  585. wsa881x_get_visense, wsa881x_set_visense),
  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 wsa881x_set_port(struct snd_soc_codec *codec, int port_idx,
  591. u8 *port_id, u8 *num_ch, u8 *ch_mask, u32 *ch_rate)
  592. {
  593. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  594. *port_id = wsa881x->port[port_idx].port_id;
  595. *num_ch = wsa881x->port[port_idx].num_ch;
  596. *ch_mask = wsa881x->port[port_idx].ch_mask;
  597. *ch_rate = wsa881x->port[port_idx].ch_rate;
  598. return 0;
  599. }
  600. static int wsa881x_enable_swr_dac_port(struct snd_soc_dapm_widget *w,
  601. struct snd_kcontrol *kcontrol, int event)
  602. {
  603. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  604. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  605. u8 port_id[WSA881X_MAX_SWR_PORTS];
  606. u8 num_ch[WSA881X_MAX_SWR_PORTS];
  607. u8 ch_mask[WSA881X_MAX_SWR_PORTS];
  608. u32 ch_rate[WSA881X_MAX_SWR_PORTS];
  609. u8 num_port = 0;
  610. dev_dbg(codec->dev, "%s: event %d name %s\n", __func__,
  611. event, w->name);
  612. if (wsa881x == NULL)
  613. return -EINVAL;
  614. switch (event) {
  615. case SND_SOC_DAPM_PRE_PMU:
  616. wsa881x_set_port(codec, SWR_DAC_PORT,
  617. &port_id[num_port], &num_ch[num_port],
  618. &ch_mask[num_port], &ch_rate[num_port]);
  619. ++num_port;
  620. if (wsa881x->comp_enable) {
  621. wsa881x_set_port(codec, SWR_COMP_PORT,
  622. &port_id[num_port], &num_ch[num_port],
  623. &ch_mask[num_port], &ch_rate[num_port]);
  624. ++num_port;
  625. }
  626. if (wsa881x->boost_enable) {
  627. wsa881x_set_port(codec, SWR_BOOST_PORT,
  628. &port_id[num_port], &num_ch[num_port],
  629. &ch_mask[num_port], &ch_rate[num_port]);
  630. ++num_port;
  631. }
  632. if (wsa881x->visense_enable) {
  633. wsa881x_set_port(codec, SWR_VISENSE_PORT,
  634. &port_id[num_port], &num_ch[num_port],
  635. &ch_mask[num_port], &ch_rate[num_port]);
  636. ++num_port;
  637. }
  638. swr_connect_port(wsa881x->swr_slave, &port_id[0], num_port,
  639. &ch_mask[0], &ch_rate[0], &num_ch[0]);
  640. break;
  641. case SND_SOC_DAPM_POST_PMU:
  642. break;
  643. case SND_SOC_DAPM_PRE_PMD:
  644. break;
  645. case SND_SOC_DAPM_POST_PMD:
  646. port_id[num_port] = wsa881x->port[SWR_DAC_PORT].port_id;
  647. ++num_port;
  648. if (wsa881x->comp_enable) {
  649. port_id[num_port] =
  650. wsa881x->port[SWR_COMP_PORT].port_id;
  651. ++num_port;
  652. }
  653. if (wsa881x->boost_enable) {
  654. port_id[num_port] =
  655. wsa881x->port[SWR_BOOST_PORT].port_id;
  656. ++num_port;
  657. }
  658. if (wsa881x->visense_enable) {
  659. port_id[num_port] =
  660. wsa881x->port[SWR_VISENSE_PORT].port_id;
  661. ++num_port;
  662. }
  663. swr_disconnect_port(wsa881x->swr_slave, &port_id[0], num_port);
  664. break;
  665. default:
  666. break;
  667. }
  668. return 0;
  669. }
  670. static int wsa881x_rdac_event(struct snd_soc_dapm_widget *w,
  671. struct snd_kcontrol *kcontrol, int event)
  672. {
  673. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  674. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  675. dev_dbg(codec->dev, "%s: %s %d boost %d visense %d\n", __func__,
  676. w->name, event, wsa881x->boost_enable,
  677. wsa881x->visense_enable);
  678. switch (event) {
  679. case SND_SOC_DAPM_PRE_PMU:
  680. wsa881x_resource_acquire(codec, ENABLE);
  681. wsa881x_boost_ctrl(codec, ENABLE);
  682. break;
  683. case SND_SOC_DAPM_POST_PMD:
  684. swr_slvdev_datapath_control(wsa881x->swr_slave,
  685. wsa881x->swr_slave->dev_num,
  686. false);
  687. wsa881x_boost_ctrl(codec, DISABLE);
  688. wsa881x_resource_acquire(codec, DISABLE);
  689. break;
  690. }
  691. return 0;
  692. }
  693. static int wsa881x_ramp_pa_gain(struct snd_soc_codec *codec,
  694. int min_gain, int max_gain, int udelay)
  695. {
  696. int val;
  697. for (val = min_gain; max_gain <= val; val--) {
  698. snd_soc_update_bits(codec, WSA881X_SPKR_DRV_GAIN,
  699. 0xF0, val << 4);
  700. /*
  701. * 1ms delay is needed for every step change in gain as per
  702. * HW requirement.
  703. */
  704. usleep_range(udelay, udelay+10);
  705. }
  706. return 0;
  707. }
  708. static void wsa881x_ocp_ctl_work(struct work_struct *work)
  709. {
  710. struct wsa881x_priv *wsa881x;
  711. struct delayed_work *dwork;
  712. struct snd_soc_codec *codec;
  713. int temp_val;
  714. dwork = to_delayed_work(work);
  715. wsa881x = container_of(dwork, struct wsa881x_priv, ocp_ctl_work);
  716. codec = wsa881x->codec;
  717. wsa881x_get_temp(wsa881x->tz_pdata.tz_dev, &temp_val);
  718. dev_dbg(codec->dev, " temp = %d\n", temp_val);
  719. if (temp_val <= WSA881X_OCP_CTL_TEMP_CELSIUS)
  720. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0x00);
  721. else
  722. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0xC0);
  723. schedule_delayed_work(&wsa881x->ocp_ctl_work,
  724. msecs_to_jiffies(wsa881x_ocp_poll_timer_sec * 1000));
  725. }
  726. static int wsa881x_spkr_pa_event(struct snd_soc_dapm_widget *w,
  727. struct snd_kcontrol *kcontrol, int event)
  728. {
  729. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  730. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  731. int min_gain, max_gain;
  732. dev_dbg(codec->dev, "%s: %s %d\n", __func__, w->name, event);
  733. switch (event) {
  734. case SND_SOC_DAPM_PRE_PMU:
  735. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0x80);
  736. regmap_multi_reg_write(wsa881x->regmap,
  737. wsa881x_pre_pmu_pa_2_0,
  738. ARRAY_SIZE(wsa881x_pre_pmu_pa_2_0));
  739. swr_slvdev_datapath_control(wsa881x->swr_slave,
  740. wsa881x->swr_slave->dev_num,
  741. true);
  742. /* Set register mode if compander is not enabled */
  743. if (!wsa881x->comp_enable)
  744. snd_soc_update_bits(codec, WSA881X_SPKR_DRV_GAIN,
  745. 0x08, 0x08);
  746. else
  747. snd_soc_update_bits(codec, WSA881X_SPKR_DRV_GAIN,
  748. 0x08, 0x00);
  749. break;
  750. case SND_SOC_DAPM_POST_PMU:
  751. if (!wsa881x->comp_enable) {
  752. max_gain = wsa881x->pa_gain;
  753. /*
  754. * Gain has to set incrementally in 4 steps
  755. * as per HW sequence
  756. */
  757. if (max_gain > G_4P5DB)
  758. min_gain = G_0DB;
  759. else
  760. min_gain = max_gain + 3;
  761. /*
  762. * 1ms delay is needed before change in gain
  763. * as per HW requirement.
  764. */
  765. usleep_range(1000, 1010);
  766. wsa881x_ramp_pa_gain(codec, min_gain, max_gain, 1000);
  767. }
  768. if (wsa881x->visense_enable) {
  769. wsa881x_visense_txfe_ctrl(codec, ENABLE,
  770. 0x00, 0x03, 0x01);
  771. snd_soc_update_bits(codec, WSA881X_ADC_EN_SEL_IBAIS,
  772. 0x07, 0x01);
  773. wsa881x_visense_adc_ctrl(codec, ENABLE);
  774. }
  775. schedule_delayed_work(&wsa881x->ocp_ctl_work,
  776. msecs_to_jiffies(WSA881X_OCP_CTL_TIMER_SEC * 1000));
  777. /* Force remove group */
  778. swr_remove_from_group(wsa881x->swr_slave,
  779. wsa881x->swr_slave->dev_num);
  780. break;
  781. case SND_SOC_DAPM_POST_PMD:
  782. if (wsa881x->visense_enable) {
  783. wsa881x_visense_adc_ctrl(codec, DISABLE);
  784. wsa881x_visense_txfe_ctrl(codec, DISABLE,
  785. 0x00, 0x01, 0x01);
  786. }
  787. cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
  788. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0xC0);
  789. break;
  790. }
  791. return 0;
  792. }
  793. static const struct snd_soc_dapm_widget wsa881x_dapm_widgets[] = {
  794. SND_SOC_DAPM_INPUT("IN"),
  795. SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0, swr_dac_port,
  796. ARRAY_SIZE(swr_dac_port), wsa881x_enable_swr_dac_port,
  797. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  798. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  799. SND_SOC_DAPM_DAC_E("RDAC", NULL, WSA881X_SPKR_DAC_CTL, 7, 0,
  800. wsa881x_rdac_event,
  801. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  802. SND_SOC_DAPM_PGA_E("SPKR PGA", WSA881X_SPKR_DRV_EN, 7, 0, NULL, 0,
  803. wsa881x_spkr_pa_event, SND_SOC_DAPM_PRE_PMU |
  804. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  805. SND_SOC_DAPM_OUTPUT("SPKR"),
  806. };
  807. static const struct snd_soc_dapm_route wsa881x_audio_map[] = {
  808. {"SWR DAC_Port", "Switch", "IN"},
  809. {"RDAC", NULL, "SWR DAC_Port"},
  810. {"SPKR PGA", NULL, "RDAC"},
  811. {"SPKR", NULL, "SPKR PGA"},
  812. };
  813. int wsa881x_set_channel_map(struct snd_soc_codec *codec, u8 *port, u8 num_port,
  814. unsigned int *ch_mask, unsigned int *ch_rate)
  815. {
  816. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  817. int i;
  818. if (!port || !ch_mask || !ch_rate ||
  819. (num_port > WSA881X_MAX_SWR_PORTS)) {
  820. dev_err(codec->dev,
  821. "%s: Invalid port=%pK, ch_mask=%pK, ch_rate=%pK\n",
  822. __func__, port, ch_mask, ch_rate);
  823. return -EINVAL;
  824. }
  825. for (i = 0; i < num_port; i++) {
  826. wsa881x->port[i].port_id = port[i];
  827. wsa881x->port[i].ch_mask = ch_mask[i];
  828. wsa881x->port[i].ch_rate = ch_rate[i];
  829. wsa881x->port[i].num_ch = __sw_hweight8(ch_mask[i]);
  830. }
  831. return 0;
  832. }
  833. EXPORT_SYMBOL(wsa881x_set_channel_map);
  834. static void wsa881x_init(struct snd_soc_codec *codec)
  835. {
  836. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  837. wsa881x->version = snd_soc_read(codec, WSA881X_CHIP_ID1);
  838. wsa881x_regmap_defaults(wsa881x->regmap, wsa881x->version);
  839. /* Enable software reset output from soundwire slave */
  840. snd_soc_update_bits(codec, WSA881X_SWR_RESET_EN, 0x07, 0x07);
  841. /* Bring out of analog reset */
  842. snd_soc_update_bits(codec, WSA881X_CDC_RST_CTL, 0x02, 0x02);
  843. /* Bring out of digital reset */
  844. snd_soc_update_bits(codec, WSA881X_CDC_RST_CTL, 0x01, 0x01);
  845. snd_soc_update_bits(codec, WSA881X_CLOCK_CONFIG, 0x10, 0x10);
  846. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0x02, 0x02);
  847. snd_soc_update_bits(codec, WSA881X_SPKR_MISC_CTL1, 0xC0, 0x80);
  848. snd_soc_update_bits(codec, WSA881X_SPKR_MISC_CTL1, 0x06, 0x06);
  849. snd_soc_update_bits(codec, WSA881X_SPKR_BIAS_INT, 0xFF, 0x00);
  850. snd_soc_update_bits(codec, WSA881X_SPKR_PA_INT, 0xF0, 0x40);
  851. snd_soc_update_bits(codec, WSA881X_SPKR_PA_INT, 0x0E, 0x0E);
  852. snd_soc_update_bits(codec, WSA881X_BOOST_LOOP_STABILITY,
  853. 0x03, 0x03);
  854. snd_soc_update_bits(codec, WSA881X_BOOST_MISC2_CTL, 0xFF, 0x14);
  855. snd_soc_update_bits(codec, WSA881X_BOOST_START_CTL, 0x80, 0x80);
  856. snd_soc_update_bits(codec, WSA881X_BOOST_START_CTL, 0x03, 0x00);
  857. snd_soc_update_bits(codec, WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
  858. 0x0C, 0x04);
  859. snd_soc_update_bits(codec, WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
  860. 0x03, 0x00);
  861. if (snd_soc_read(codec, WSA881X_OTP_REG_0))
  862. snd_soc_update_bits(codec, WSA881X_BOOST_PRESET_OUT1,
  863. 0xF0, 0x70);
  864. snd_soc_update_bits(codec, WSA881X_BOOST_PRESET_OUT2,
  865. 0xF0, 0x30);
  866. snd_soc_update_bits(codec, WSA881X_SPKR_DRV_EN, 0x08, 0x08);
  867. snd_soc_update_bits(codec, WSA881X_BOOST_CURRENT_LIMIT,
  868. 0x0F, 0x08);
  869. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0x30, 0x30);
  870. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0x0C, 0x00);
  871. snd_soc_update_bits(codec, WSA881X_OTP_REG_28, 0x3F, 0x3A);
  872. snd_soc_update_bits(codec, WSA881X_BONGO_RESRV_REG1,
  873. 0xFF, 0xB2);
  874. snd_soc_update_bits(codec, WSA881X_BONGO_RESRV_REG2,
  875. 0xFF, 0x05);
  876. }
  877. static int32_t wsa881x_resource_acquire(struct snd_soc_codec *codec,
  878. bool enable)
  879. {
  880. wsa881x_clk_ctrl(codec, enable);
  881. wsa881x_bandgap_ctrl(codec, enable);
  882. return 0;
  883. }
  884. static int32_t wsa881x_temp_reg_read(struct snd_soc_codec *codec,
  885. struct wsa_temp_register *wsa_temp_reg)
  886. {
  887. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  888. struct swr_device *dev;
  889. u8 retry = WSA881X_NUM_RETRY;
  890. u8 devnum = 0;
  891. if (!wsa881x) {
  892. dev_err(codec->dev, "%s: wsa881x is NULL\n", __func__);
  893. return -EINVAL;
  894. }
  895. dev = wsa881x->swr_slave;
  896. if (dev && (wsa881x->state == WSA881X_DEV_DOWN)) {
  897. while (swr_get_logical_dev_num(dev, dev->addr, &devnum) &&
  898. retry--) {
  899. /* Retry after 1 msec delay */
  900. usleep_range(1000, 1100);
  901. }
  902. if (retry == 0) {
  903. dev_err(codec->dev,
  904. "%s get devnum %d for dev addr %lx failed\n",
  905. __func__, devnum, dev->addr);
  906. return -EINVAL;
  907. }
  908. }
  909. mutex_lock(&wsa881x->res_lock);
  910. if (!wsa881x->clk_cnt) {
  911. regcache_mark_dirty(wsa881x->regmap);
  912. regcache_sync(wsa881x->regmap);
  913. }
  914. mutex_unlock(&wsa881x->res_lock);
  915. wsa881x_resource_acquire(codec, ENABLE);
  916. snd_soc_update_bits(codec, WSA881X_TADC_VALUE_CTL, 0x01, 0x00);
  917. wsa_temp_reg->dmeas_msb = snd_soc_read(codec, WSA881X_TEMP_MSB);
  918. wsa_temp_reg->dmeas_lsb = snd_soc_read(codec, WSA881X_TEMP_LSB);
  919. snd_soc_update_bits(codec, WSA881X_TADC_VALUE_CTL, 0x01, 0x01);
  920. wsa_temp_reg->d1_msb = snd_soc_read(codec, WSA881X_OTP_REG_1);
  921. wsa_temp_reg->d1_lsb = snd_soc_read(codec, WSA881X_OTP_REG_2);
  922. wsa_temp_reg->d2_msb = snd_soc_read(codec, WSA881X_OTP_REG_3);
  923. wsa_temp_reg->d2_lsb = snd_soc_read(codec, WSA881X_OTP_REG_4);
  924. wsa881x_resource_acquire(codec, DISABLE);
  925. return 0;
  926. }
  927. static int wsa881x_probe(struct snd_soc_codec *codec)
  928. {
  929. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  930. struct swr_device *dev;
  931. if (!wsa881x)
  932. return -EINVAL;
  933. dev = wsa881x->swr_slave;
  934. wsa881x->codec = codec;
  935. mutex_init(&wsa881x->bg_lock);
  936. mutex_init(&wsa881x->res_lock);
  937. wsa881x_init(codec);
  938. snprintf(wsa881x->tz_pdata.name, sizeof(wsa881x->tz_pdata.name),
  939. "%s.%x", "wsatz", (u8)dev->addr);
  940. wsa881x->bg_cnt = 0;
  941. wsa881x->clk_cnt = 0;
  942. wsa881x->tz_pdata.codec = codec;
  943. wsa881x->tz_pdata.wsa_temp_reg_read = wsa881x_temp_reg_read;
  944. wsa881x_init_thermal(&wsa881x->tz_pdata);
  945. snd_soc_add_codec_controls(codec, wsa_snd_controls,
  946. ARRAY_SIZE(wsa_snd_controls));
  947. INIT_DELAYED_WORK(&wsa881x->ocp_ctl_work, wsa881x_ocp_ctl_work);
  948. return 0;
  949. }
  950. static int wsa881x_remove(struct snd_soc_codec *codec)
  951. {
  952. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  953. if (wsa881x->tz_pdata.tz_dev)
  954. wsa881x_deinit_thermal(wsa881x->tz_pdata.tz_dev);
  955. mutex_destroy(&wsa881x->bg_lock);
  956. mutex_destroy(&wsa881x->res_lock);
  957. return 0;
  958. }
  959. static struct regmap *wsa881x_get_regmap(struct device *dev)
  960. {
  961. struct wsa881x_priv *control = swr_get_dev_data(to_swr_device(dev));
  962. if (!control)
  963. return NULL;
  964. return control->regmap;
  965. }
  966. static struct snd_soc_codec_driver soc_codec_dev_wsa881x = {
  967. .probe = wsa881x_probe,
  968. .remove = wsa881x_remove,
  969. .get_regmap = wsa881x_get_regmap,
  970. .component_driver = {
  971. .controls = wsa881x_snd_controls,
  972. .num_controls = ARRAY_SIZE(wsa881x_snd_controls),
  973. .dapm_widgets = wsa881x_dapm_widgets,
  974. .num_dapm_widgets = ARRAY_SIZE(wsa881x_dapm_widgets),
  975. .dapm_routes = wsa881x_audio_map,
  976. .num_dapm_routes = ARRAY_SIZE(wsa881x_audio_map),
  977. },
  978. };
  979. static int wsa881x_gpio_ctrl(struct wsa881x_priv *wsa881x, bool enable)
  980. {
  981. int ret = 0;
  982. if (wsa881x->pd_gpio < 0) {
  983. dev_err(wsa881x->dev, "%s: gpio is not valid %d\n",
  984. __func__, wsa881x->pd_gpio);
  985. return -EINVAL;
  986. }
  987. if (wsa881x->wsa_rst_np) {
  988. if (enable)
  989. ret = msm_cdc_pinctrl_select_active_state(
  990. wsa881x->wsa_rst_np);
  991. else
  992. ret = msm_cdc_pinctrl_select_sleep_state(
  993. wsa881x->wsa_rst_np);
  994. if (ret != 0)
  995. dev_err(wsa881x->dev,
  996. "%s: Failed to turn state %d; ret=%d\n",
  997. __func__, enable, ret);
  998. } else {
  999. if (gpio_is_valid(wsa881x->pd_gpio))
  1000. gpio_direction_output(wsa881x->pd_gpio, enable);
  1001. }
  1002. return ret;
  1003. }
  1004. static int wsa881x_gpio_init(struct swr_device *pdev)
  1005. {
  1006. int ret = 0;
  1007. struct wsa881x_priv *wsa881x;
  1008. wsa881x = swr_get_dev_data(pdev);
  1009. if (!wsa881x) {
  1010. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1011. return -EINVAL;
  1012. }
  1013. dev_dbg(&pdev->dev, "%s: gpio %d request with name %s\n",
  1014. __func__, wsa881x->pd_gpio, dev_name(&pdev->dev));
  1015. ret = gpio_request(wsa881x->pd_gpio, dev_name(&pdev->dev));
  1016. if (ret) {
  1017. if (ret == -EBUSY) {
  1018. /* GPIO was already requested */
  1019. dev_dbg(&pdev->dev,
  1020. "%s: gpio %d is already set to high\n",
  1021. __func__, wsa881x->pd_gpio);
  1022. ret = 0;
  1023. } else {
  1024. dev_err(&pdev->dev, "%s: Failed to request gpio %d, err: %d\n",
  1025. __func__, wsa881x->pd_gpio, ret);
  1026. }
  1027. }
  1028. return ret;
  1029. }
  1030. static int wsa881x_swr_probe(struct swr_device *pdev)
  1031. {
  1032. int ret = 0;
  1033. struct wsa881x_priv *wsa881x;
  1034. u8 devnum = 0;
  1035. bool pin_state_current = false;
  1036. wsa881x = devm_kzalloc(&pdev->dev, sizeof(struct wsa881x_priv),
  1037. GFP_KERNEL);
  1038. if (!wsa881x)
  1039. return -ENOMEM;
  1040. wsa881x->wsa_rst_np = of_parse_phandle(pdev->dev.of_node,
  1041. "qcom,spkr-sd-n-node", 0);
  1042. if (!wsa881x->wsa_rst_np) {
  1043. dev_dbg(&pdev->dev, "%s: Not using pinctrl, fallback to gpio\n",
  1044. __func__);
  1045. wsa881x->pd_gpio = of_get_named_gpio(pdev->dev.of_node,
  1046. "qcom,spkr-sd-n-gpio", 0);
  1047. if (wsa881x->pd_gpio < 0) {
  1048. dev_err(&pdev->dev, "%s: %s property is not found %d\n",
  1049. __func__, "qcom,spkr-sd-n-gpio",
  1050. wsa881x->pd_gpio);
  1051. goto err;
  1052. }
  1053. dev_dbg(&pdev->dev, "%s: reset gpio %d\n", __func__,
  1054. wsa881x->pd_gpio);
  1055. }
  1056. swr_set_dev_data(pdev, wsa881x);
  1057. wsa881x->swr_slave = pdev;
  1058. if (!wsa881x->wsa_rst_np) {
  1059. ret = wsa881x_gpio_init(pdev);
  1060. if (ret)
  1061. goto err;
  1062. }
  1063. if (wsa881x->wsa_rst_np)
  1064. pin_state_current = msm_cdc_pinctrl_get_state(
  1065. wsa881x->wsa_rst_np);
  1066. wsa881x_gpio_ctrl(wsa881x, true);
  1067. wsa881x->state = WSA881X_DEV_UP;
  1068. if (!debugfs_wsa881x_dent) {
  1069. dbgwsa881x = wsa881x;
  1070. debugfs_wsa881x_dent = debugfs_create_dir(
  1071. "wsa881x_swr_slave", 0);
  1072. if (!IS_ERR(debugfs_wsa881x_dent)) {
  1073. debugfs_peek = debugfs_create_file("swrslave_peek",
  1074. S_IFREG | 0444, debugfs_wsa881x_dent,
  1075. (void *) "swrslave_peek",
  1076. &codec_debug_ops);
  1077. debugfs_poke = debugfs_create_file("swrslave_poke",
  1078. S_IFREG | 0444, debugfs_wsa881x_dent,
  1079. (void *) "swrslave_poke",
  1080. &codec_debug_ops);
  1081. debugfs_reg_dump = debugfs_create_file(
  1082. "swrslave_reg_dump",
  1083. S_IFREG | 0444,
  1084. debugfs_wsa881x_dent,
  1085. (void *) "swrslave_reg_dump",
  1086. &codec_debug_ops);
  1087. }
  1088. }
  1089. /*
  1090. * Add 5msec delay to provide sufficient time for
  1091. * soundwire auto enumeration of slave devices as
  1092. * as per HW requirement.
  1093. */
  1094. usleep_range(5000, 5010);
  1095. ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
  1096. if (ret) {
  1097. dev_dbg(&pdev->dev,
  1098. "%s get devnum %d for dev addr %lx failed\n",
  1099. __func__, devnum, pdev->addr);
  1100. goto dev_err;
  1101. }
  1102. pdev->dev_num = devnum;
  1103. wsa881x->regmap = devm_regmap_init_swr(pdev,
  1104. &wsa881x_regmap_config);
  1105. if (IS_ERR(wsa881x->regmap)) {
  1106. ret = PTR_ERR(wsa881x->regmap);
  1107. dev_err(&pdev->dev, "%s: regmap_init failed %d\n",
  1108. __func__, ret);
  1109. goto dev_err;
  1110. }
  1111. ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wsa881x,
  1112. NULL, 0);
  1113. if (ret) {
  1114. dev_err(&pdev->dev, "%s: Codec registration failed\n",
  1115. __func__);
  1116. goto dev_err;
  1117. }
  1118. return 0;
  1119. dev_err:
  1120. if (pin_state_current == false)
  1121. wsa881x_gpio_ctrl(wsa881x, false);
  1122. swr_remove_device(pdev);
  1123. err:
  1124. return ret;
  1125. }
  1126. static int wsa881x_swr_remove(struct swr_device *pdev)
  1127. {
  1128. struct wsa881x_priv *wsa881x;
  1129. wsa881x = swr_get_dev_data(pdev);
  1130. if (!wsa881x) {
  1131. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1132. return -EINVAL;
  1133. }
  1134. debugfs_remove_recursive(debugfs_wsa881x_dent);
  1135. debugfs_wsa881x_dent = NULL;
  1136. snd_soc_unregister_codec(&pdev->dev);
  1137. if (wsa881x->pd_gpio)
  1138. gpio_free(wsa881x->pd_gpio);
  1139. swr_set_dev_data(pdev, NULL);
  1140. return 0;
  1141. }
  1142. static int wsa881x_swr_up(struct swr_device *pdev)
  1143. {
  1144. int ret;
  1145. struct wsa881x_priv *wsa881x;
  1146. wsa881x = swr_get_dev_data(pdev);
  1147. if (!wsa881x) {
  1148. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1149. return -EINVAL;
  1150. }
  1151. ret = wsa881x_gpio_ctrl(wsa881x, true);
  1152. if (ret)
  1153. dev_err(&pdev->dev, "%s: Failed to enable gpio\n", __func__);
  1154. else
  1155. wsa881x->state = WSA881X_DEV_UP;
  1156. return ret;
  1157. }
  1158. static int wsa881x_swr_down(struct swr_device *pdev)
  1159. {
  1160. struct wsa881x_priv *wsa881x;
  1161. int ret;
  1162. wsa881x = swr_get_dev_data(pdev);
  1163. if (!wsa881x) {
  1164. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1165. return -EINVAL;
  1166. }
  1167. if (delayed_work_pending(&wsa881x->ocp_ctl_work))
  1168. cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
  1169. ret = wsa881x_gpio_ctrl(wsa881x, false);
  1170. if (ret)
  1171. dev_err(&pdev->dev, "%s: Failed to disable gpio\n", __func__);
  1172. else
  1173. wsa881x->state = WSA881X_DEV_DOWN;
  1174. return ret;
  1175. }
  1176. static int wsa881x_swr_reset(struct swr_device *pdev)
  1177. {
  1178. struct wsa881x_priv *wsa881x;
  1179. u8 retry = WSA881X_NUM_RETRY;
  1180. u8 devnum = 0;
  1181. wsa881x = swr_get_dev_data(pdev);
  1182. if (!wsa881x) {
  1183. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1184. return -EINVAL;
  1185. }
  1186. wsa881x->bg_cnt = 0;
  1187. wsa881x->clk_cnt = 0;
  1188. while (swr_get_logical_dev_num(pdev, pdev->addr, &devnum) && retry--) {
  1189. /* Retry after 1 msec delay */
  1190. usleep_range(1000, 1100);
  1191. }
  1192. pdev->dev_num = devnum;
  1193. regcache_mark_dirty(wsa881x->regmap);
  1194. regcache_sync(wsa881x->regmap);
  1195. return 0;
  1196. }
  1197. #ifdef CONFIG_PM_SLEEP
  1198. static int wsa881x_swr_suspend(struct device *dev)
  1199. {
  1200. dev_dbg(dev, "%s: system suspend\n", __func__);
  1201. return 0;
  1202. }
  1203. static int wsa881x_swr_resume(struct device *dev)
  1204. {
  1205. struct wsa881x_priv *wsa881x = swr_get_dev_data(to_swr_device(dev));
  1206. if (!wsa881x) {
  1207. dev_err(dev, "%s: wsa881x private data is NULL\n", __func__);
  1208. return -EINVAL;
  1209. }
  1210. dev_dbg(dev, "%s: system resume\n", __func__);
  1211. return 0;
  1212. }
  1213. #endif /* CONFIG_PM_SLEEP */
  1214. static const struct dev_pm_ops wsa881x_swr_pm_ops = {
  1215. SET_SYSTEM_SLEEP_PM_OPS(wsa881x_swr_suspend, wsa881x_swr_resume)
  1216. };
  1217. static const struct swr_device_id wsa881x_swr_id[] = {
  1218. {"wsa881x", 0},
  1219. {}
  1220. };
  1221. static const struct of_device_id wsa881x_swr_dt_match[] = {
  1222. {
  1223. .compatible = "qcom,wsa881x",
  1224. },
  1225. {}
  1226. };
  1227. static struct swr_driver wsa881x_codec_driver = {
  1228. .driver = {
  1229. .name = "wsa881x",
  1230. .owner = THIS_MODULE,
  1231. .pm = &wsa881x_swr_pm_ops,
  1232. .of_match_table = wsa881x_swr_dt_match,
  1233. },
  1234. .probe = wsa881x_swr_probe,
  1235. .remove = wsa881x_swr_remove,
  1236. .id_table = wsa881x_swr_id,
  1237. .device_up = wsa881x_swr_up,
  1238. .device_down = wsa881x_swr_down,
  1239. .reset_device = wsa881x_swr_reset,
  1240. };
  1241. static int __init wsa881x_codec_init(void)
  1242. {
  1243. return swr_driver_register(&wsa881x_codec_driver);
  1244. }
  1245. static void __exit wsa881x_codec_exit(void)
  1246. {
  1247. swr_driver_unregister(&wsa881x_codec_driver);
  1248. }
  1249. module_init(wsa881x_codec_init);
  1250. module_exit(wsa881x_codec_exit);
  1251. MODULE_DESCRIPTION("WSA881x Codec driver");
  1252. MODULE_LICENSE("GPL v2");