wsa881x.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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 int wsa881x_set_boost_level(struct snd_kcontrol *kcontrol,
  580. struct snd_ctl_elem_value *ucontrol)
  581. {
  582. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  583. u8 wsa_boost_level = 0;
  584. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  585. __func__, ucontrol->value.integer.value[0]);
  586. wsa_boost_level = ucontrol->value.integer.value[0];
  587. snd_soc_update_bits(codec, WSA881X_BOOST_PRESET_OUT1,
  588. 0xff, wsa_boost_level);
  589. return 0;
  590. }
  591. static int wsa881x_get_boost_level(struct snd_kcontrol *kcontrol,
  592. struct snd_ctl_elem_value *ucontrol)
  593. {
  594. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  595. u8 wsa_boost_level = 0;
  596. wsa_boost_level = snd_soc_read(codec, WSA881X_BOOST_PRESET_OUT1);
  597. ucontrol->value.integer.value[0] = wsa_boost_level;
  598. dev_dbg(codec->dev, "%s: boost level = 0x%x\n", __func__,
  599. wsa_boost_level);
  600. return 0;
  601. }
  602. static const struct snd_kcontrol_new wsa881x_snd_controls[] = {
  603. SOC_SINGLE_EXT("COMP Switch", SND_SOC_NOPM, 0, 1, 0,
  604. wsa881x_get_compander, wsa881x_set_compander),
  605. SOC_SINGLE_EXT("BOOST Switch", SND_SOC_NOPM, 0, 1, 0,
  606. wsa881x_get_boost, wsa881x_set_boost),
  607. SOC_SINGLE_EXT("VISENSE Switch", SND_SOC_NOPM, 0, 1, 0,
  608. wsa881x_get_visense, wsa881x_set_visense),
  609. SOC_SINGLE_EXT("Boost Level", SND_SOC_NOPM, 0, 0xff, 0,
  610. wsa881x_get_boost_level, wsa881x_set_boost_level),
  611. };
  612. static const struct snd_kcontrol_new swr_dac_port[] = {
  613. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  614. };
  615. static int wsa881x_set_port(struct snd_soc_codec *codec, int port_idx,
  616. u8 *port_id, u8 *num_ch, u8 *ch_mask, u32 *ch_rate)
  617. {
  618. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  619. *port_id = wsa881x->port[port_idx].port_id;
  620. *num_ch = wsa881x->port[port_idx].num_ch;
  621. *ch_mask = wsa881x->port[port_idx].ch_mask;
  622. *ch_rate = wsa881x->port[port_idx].ch_rate;
  623. return 0;
  624. }
  625. static int wsa881x_enable_swr_dac_port(struct snd_soc_dapm_widget *w,
  626. struct snd_kcontrol *kcontrol, int event)
  627. {
  628. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  629. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  630. u8 port_id[WSA881X_MAX_SWR_PORTS];
  631. u8 num_ch[WSA881X_MAX_SWR_PORTS];
  632. u8 ch_mask[WSA881X_MAX_SWR_PORTS];
  633. u32 ch_rate[WSA881X_MAX_SWR_PORTS];
  634. u8 num_port = 0;
  635. dev_dbg(codec->dev, "%s: event %d name %s\n", __func__,
  636. event, w->name);
  637. if (wsa881x == NULL)
  638. return -EINVAL;
  639. switch (event) {
  640. case SND_SOC_DAPM_PRE_PMU:
  641. wsa881x_set_port(codec, SWR_DAC_PORT,
  642. &port_id[num_port], &num_ch[num_port],
  643. &ch_mask[num_port], &ch_rate[num_port]);
  644. ++num_port;
  645. if (wsa881x->comp_enable) {
  646. wsa881x_set_port(codec, SWR_COMP_PORT,
  647. &port_id[num_port], &num_ch[num_port],
  648. &ch_mask[num_port], &ch_rate[num_port]);
  649. ++num_port;
  650. }
  651. if (wsa881x->boost_enable) {
  652. wsa881x_set_port(codec, SWR_BOOST_PORT,
  653. &port_id[num_port], &num_ch[num_port],
  654. &ch_mask[num_port], &ch_rate[num_port]);
  655. ++num_port;
  656. }
  657. if (wsa881x->visense_enable) {
  658. wsa881x_set_port(codec, SWR_VISENSE_PORT,
  659. &port_id[num_port], &num_ch[num_port],
  660. &ch_mask[num_port], &ch_rate[num_port]);
  661. ++num_port;
  662. }
  663. swr_connect_port(wsa881x->swr_slave, &port_id[0], num_port,
  664. &ch_mask[0], &ch_rate[0], &num_ch[0]);
  665. break;
  666. case SND_SOC_DAPM_POST_PMU:
  667. break;
  668. case SND_SOC_DAPM_PRE_PMD:
  669. break;
  670. case SND_SOC_DAPM_POST_PMD:
  671. port_id[num_port] = wsa881x->port[SWR_DAC_PORT].port_id;
  672. ++num_port;
  673. if (wsa881x->comp_enable) {
  674. port_id[num_port] =
  675. wsa881x->port[SWR_COMP_PORT].port_id;
  676. ++num_port;
  677. }
  678. if (wsa881x->boost_enable) {
  679. port_id[num_port] =
  680. wsa881x->port[SWR_BOOST_PORT].port_id;
  681. ++num_port;
  682. }
  683. if (wsa881x->visense_enable) {
  684. port_id[num_port] =
  685. wsa881x->port[SWR_VISENSE_PORT].port_id;
  686. ++num_port;
  687. }
  688. swr_disconnect_port(wsa881x->swr_slave, &port_id[0], num_port);
  689. break;
  690. default:
  691. break;
  692. }
  693. return 0;
  694. }
  695. static int wsa881x_rdac_event(struct snd_soc_dapm_widget *w,
  696. struct snd_kcontrol *kcontrol, int event)
  697. {
  698. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  699. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  700. dev_dbg(codec->dev, "%s: %s %d boost %d visense %d\n", __func__,
  701. w->name, event, wsa881x->boost_enable,
  702. wsa881x->visense_enable);
  703. switch (event) {
  704. case SND_SOC_DAPM_PRE_PMU:
  705. wsa881x_resource_acquire(codec, ENABLE);
  706. wsa881x_boost_ctrl(codec, ENABLE);
  707. break;
  708. case SND_SOC_DAPM_POST_PMD:
  709. swr_slvdev_datapath_control(wsa881x->swr_slave,
  710. wsa881x->swr_slave->dev_num,
  711. false);
  712. wsa881x_boost_ctrl(codec, DISABLE);
  713. wsa881x_resource_acquire(codec, DISABLE);
  714. break;
  715. }
  716. return 0;
  717. }
  718. static int wsa881x_ramp_pa_gain(struct snd_soc_codec *codec,
  719. int min_gain, int max_gain, int udelay)
  720. {
  721. int val;
  722. for (val = min_gain; max_gain <= val; val--) {
  723. snd_soc_update_bits(codec, WSA881X_SPKR_DRV_GAIN,
  724. 0xF0, val << 4);
  725. /*
  726. * 1ms delay is needed for every step change in gain as per
  727. * HW requirement.
  728. */
  729. usleep_range(udelay, udelay+10);
  730. }
  731. return 0;
  732. }
  733. static void wsa881x_ocp_ctl_work(struct work_struct *work)
  734. {
  735. struct wsa881x_priv *wsa881x;
  736. struct delayed_work *dwork;
  737. struct snd_soc_codec *codec;
  738. int temp_val;
  739. dwork = to_delayed_work(work);
  740. wsa881x = container_of(dwork, struct wsa881x_priv, ocp_ctl_work);
  741. codec = wsa881x->codec;
  742. wsa881x_get_temp(wsa881x->tz_pdata.tz_dev, &temp_val);
  743. dev_dbg(codec->dev, " temp = %d\n", temp_val);
  744. if (temp_val <= WSA881X_OCP_CTL_TEMP_CELSIUS)
  745. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0x00);
  746. else
  747. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0xC0);
  748. schedule_delayed_work(&wsa881x->ocp_ctl_work,
  749. msecs_to_jiffies(wsa881x_ocp_poll_timer_sec * 1000));
  750. }
  751. static int wsa881x_spkr_pa_event(struct snd_soc_dapm_widget *w,
  752. struct snd_kcontrol *kcontrol, int event)
  753. {
  754. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  755. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  756. int min_gain, max_gain;
  757. dev_dbg(codec->dev, "%s: %s %d\n", __func__, w->name, event);
  758. switch (event) {
  759. case SND_SOC_DAPM_PRE_PMU:
  760. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0x80);
  761. regmap_multi_reg_write(wsa881x->regmap,
  762. wsa881x_pre_pmu_pa_2_0,
  763. ARRAY_SIZE(wsa881x_pre_pmu_pa_2_0));
  764. swr_slvdev_datapath_control(wsa881x->swr_slave,
  765. wsa881x->swr_slave->dev_num,
  766. true);
  767. /* Set register mode if compander is not enabled */
  768. if (!wsa881x->comp_enable)
  769. snd_soc_update_bits(codec, WSA881X_SPKR_DRV_GAIN,
  770. 0x08, 0x08);
  771. else
  772. snd_soc_update_bits(codec, WSA881X_SPKR_DRV_GAIN,
  773. 0x08, 0x00);
  774. break;
  775. case SND_SOC_DAPM_POST_PMU:
  776. if (!wsa881x->comp_enable) {
  777. max_gain = wsa881x->pa_gain;
  778. /*
  779. * Gain has to set incrementally in 4 steps
  780. * as per HW sequence
  781. */
  782. if (max_gain > G_4P5DB)
  783. min_gain = G_0DB;
  784. else
  785. min_gain = max_gain + 3;
  786. /*
  787. * 1ms delay is needed before change in gain
  788. * as per HW requirement.
  789. */
  790. usleep_range(1000, 1010);
  791. wsa881x_ramp_pa_gain(codec, min_gain, max_gain, 1000);
  792. }
  793. if (wsa881x->visense_enable) {
  794. wsa881x_visense_txfe_ctrl(codec, ENABLE,
  795. 0x00, 0x03, 0x01);
  796. snd_soc_update_bits(codec, WSA881X_ADC_EN_SEL_IBAIS,
  797. 0x07, 0x01);
  798. wsa881x_visense_adc_ctrl(codec, ENABLE);
  799. }
  800. schedule_delayed_work(&wsa881x->ocp_ctl_work,
  801. msecs_to_jiffies(WSA881X_OCP_CTL_TIMER_SEC * 1000));
  802. /* Force remove group */
  803. swr_remove_from_group(wsa881x->swr_slave,
  804. wsa881x->swr_slave->dev_num);
  805. break;
  806. case SND_SOC_DAPM_POST_PMD:
  807. if (wsa881x->visense_enable) {
  808. wsa881x_visense_adc_ctrl(codec, DISABLE);
  809. wsa881x_visense_txfe_ctrl(codec, DISABLE,
  810. 0x00, 0x01, 0x01);
  811. }
  812. cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
  813. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0xC0, 0xC0);
  814. break;
  815. }
  816. return 0;
  817. }
  818. static const struct snd_soc_dapm_widget wsa881x_dapm_widgets[] = {
  819. SND_SOC_DAPM_INPUT("IN"),
  820. SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0, swr_dac_port,
  821. ARRAY_SIZE(swr_dac_port), wsa881x_enable_swr_dac_port,
  822. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  823. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  824. SND_SOC_DAPM_DAC_E("RDAC", NULL, WSA881X_SPKR_DAC_CTL, 7, 0,
  825. wsa881x_rdac_event,
  826. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  827. SND_SOC_DAPM_PGA_E("SPKR PGA", WSA881X_SPKR_DRV_EN, 7, 0, NULL, 0,
  828. wsa881x_spkr_pa_event, SND_SOC_DAPM_PRE_PMU |
  829. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  830. SND_SOC_DAPM_OUTPUT("SPKR"),
  831. };
  832. static const struct snd_soc_dapm_route wsa881x_audio_map[] = {
  833. {"SWR DAC_Port", "Switch", "IN"},
  834. {"RDAC", NULL, "SWR DAC_Port"},
  835. {"SPKR PGA", NULL, "RDAC"},
  836. {"SPKR", NULL, "SPKR PGA"},
  837. };
  838. int wsa881x_set_channel_map(struct snd_soc_codec *codec, u8 *port, u8 num_port,
  839. unsigned int *ch_mask, unsigned int *ch_rate)
  840. {
  841. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  842. int i;
  843. if (!port || !ch_mask || !ch_rate ||
  844. (num_port > WSA881X_MAX_SWR_PORTS)) {
  845. dev_err(codec->dev,
  846. "%s: Invalid port=%pK, ch_mask=%pK, ch_rate=%pK\n",
  847. __func__, port, ch_mask, ch_rate);
  848. return -EINVAL;
  849. }
  850. for (i = 0; i < num_port; i++) {
  851. wsa881x->port[i].port_id = port[i];
  852. wsa881x->port[i].ch_mask = ch_mask[i];
  853. wsa881x->port[i].ch_rate = ch_rate[i];
  854. wsa881x->port[i].num_ch = __sw_hweight8(ch_mask[i]);
  855. }
  856. return 0;
  857. }
  858. EXPORT_SYMBOL(wsa881x_set_channel_map);
  859. static void wsa881x_init(struct snd_soc_codec *codec)
  860. {
  861. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  862. wsa881x->version = snd_soc_read(codec, WSA881X_CHIP_ID1);
  863. wsa881x_regmap_defaults(wsa881x->regmap, wsa881x->version);
  864. /* Enable software reset output from soundwire slave */
  865. snd_soc_update_bits(codec, WSA881X_SWR_RESET_EN, 0x07, 0x07);
  866. /* Bring out of analog reset */
  867. snd_soc_update_bits(codec, WSA881X_CDC_RST_CTL, 0x02, 0x02);
  868. /* Bring out of digital reset */
  869. snd_soc_update_bits(codec, WSA881X_CDC_RST_CTL, 0x01, 0x01);
  870. snd_soc_update_bits(codec, WSA881X_CLOCK_CONFIG, 0x10, 0x10);
  871. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0x02, 0x02);
  872. snd_soc_update_bits(codec, WSA881X_SPKR_MISC_CTL1, 0xC0, 0x80);
  873. snd_soc_update_bits(codec, WSA881X_SPKR_MISC_CTL1, 0x06, 0x06);
  874. snd_soc_update_bits(codec, WSA881X_SPKR_BIAS_INT, 0xFF, 0x00);
  875. snd_soc_update_bits(codec, WSA881X_SPKR_PA_INT, 0xF0, 0x40);
  876. snd_soc_update_bits(codec, WSA881X_SPKR_PA_INT, 0x0E, 0x0E);
  877. snd_soc_update_bits(codec, WSA881X_BOOST_LOOP_STABILITY,
  878. 0x03, 0x03);
  879. snd_soc_update_bits(codec, WSA881X_BOOST_MISC2_CTL, 0xFF, 0x14);
  880. snd_soc_update_bits(codec, WSA881X_BOOST_START_CTL, 0x80, 0x80);
  881. snd_soc_update_bits(codec, WSA881X_BOOST_START_CTL, 0x03, 0x00);
  882. snd_soc_update_bits(codec, WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
  883. 0x0C, 0x04);
  884. snd_soc_update_bits(codec, WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
  885. 0x03, 0x00);
  886. if (snd_soc_read(codec, WSA881X_OTP_REG_0))
  887. snd_soc_update_bits(codec, WSA881X_BOOST_PRESET_OUT1,
  888. 0xF0, 0x70);
  889. snd_soc_update_bits(codec, WSA881X_BOOST_PRESET_OUT2,
  890. 0xF0, 0x30);
  891. snd_soc_update_bits(codec, WSA881X_SPKR_DRV_EN, 0x08, 0x08);
  892. snd_soc_update_bits(codec, WSA881X_BOOST_CURRENT_LIMIT,
  893. 0x0F, 0x08);
  894. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0x30, 0x30);
  895. snd_soc_update_bits(codec, WSA881X_SPKR_OCP_CTL, 0x0C, 0x00);
  896. snd_soc_update_bits(codec, WSA881X_OTP_REG_28, 0x3F, 0x3A);
  897. snd_soc_update_bits(codec, WSA881X_BONGO_RESRV_REG1,
  898. 0xFF, 0xB2);
  899. snd_soc_update_bits(codec, WSA881X_BONGO_RESRV_REG2,
  900. 0xFF, 0x05);
  901. }
  902. static int32_t wsa881x_resource_acquire(struct snd_soc_codec *codec,
  903. bool enable)
  904. {
  905. wsa881x_clk_ctrl(codec, enable);
  906. wsa881x_bandgap_ctrl(codec, enable);
  907. return 0;
  908. }
  909. static int32_t wsa881x_temp_reg_read(struct snd_soc_codec *codec,
  910. struct wsa_temp_register *wsa_temp_reg)
  911. {
  912. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  913. struct swr_device *dev;
  914. u8 retry = WSA881X_NUM_RETRY;
  915. u8 devnum = 0;
  916. if (!wsa881x) {
  917. dev_err(codec->dev, "%s: wsa881x is NULL\n", __func__);
  918. return -EINVAL;
  919. }
  920. dev = wsa881x->swr_slave;
  921. if (dev && (wsa881x->state == WSA881X_DEV_DOWN)) {
  922. while (swr_get_logical_dev_num(dev, dev->addr, &devnum) &&
  923. retry--) {
  924. /* Retry after 1 msec delay */
  925. usleep_range(1000, 1100);
  926. }
  927. if (retry == 0) {
  928. dev_err(codec->dev,
  929. "%s get devnum %d for dev addr %lx failed\n",
  930. __func__, devnum, dev->addr);
  931. return -EINVAL;
  932. }
  933. }
  934. mutex_lock(&wsa881x->res_lock);
  935. if (!wsa881x->clk_cnt) {
  936. regcache_mark_dirty(wsa881x->regmap);
  937. regcache_sync(wsa881x->regmap);
  938. }
  939. mutex_unlock(&wsa881x->res_lock);
  940. wsa881x_resource_acquire(codec, ENABLE);
  941. snd_soc_update_bits(codec, WSA881X_TADC_VALUE_CTL, 0x01, 0x00);
  942. wsa_temp_reg->dmeas_msb = snd_soc_read(codec, WSA881X_TEMP_MSB);
  943. wsa_temp_reg->dmeas_lsb = snd_soc_read(codec, WSA881X_TEMP_LSB);
  944. snd_soc_update_bits(codec, WSA881X_TADC_VALUE_CTL, 0x01, 0x01);
  945. wsa_temp_reg->d1_msb = snd_soc_read(codec, WSA881X_OTP_REG_1);
  946. wsa_temp_reg->d1_lsb = snd_soc_read(codec, WSA881X_OTP_REG_2);
  947. wsa_temp_reg->d2_msb = snd_soc_read(codec, WSA881X_OTP_REG_3);
  948. wsa_temp_reg->d2_lsb = snd_soc_read(codec, WSA881X_OTP_REG_4);
  949. wsa881x_resource_acquire(codec, DISABLE);
  950. return 0;
  951. }
  952. static int wsa881x_probe(struct snd_soc_codec *codec)
  953. {
  954. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  955. struct swr_device *dev;
  956. if (!wsa881x)
  957. return -EINVAL;
  958. dev = wsa881x->swr_slave;
  959. wsa881x->codec = codec;
  960. mutex_init(&wsa881x->bg_lock);
  961. mutex_init(&wsa881x->res_lock);
  962. wsa881x_init(codec);
  963. snprintf(wsa881x->tz_pdata.name, sizeof(wsa881x->tz_pdata.name),
  964. "%s.%x", "wsatz", (u8)dev->addr);
  965. wsa881x->bg_cnt = 0;
  966. wsa881x->clk_cnt = 0;
  967. wsa881x->tz_pdata.codec = codec;
  968. wsa881x->tz_pdata.wsa_temp_reg_read = wsa881x_temp_reg_read;
  969. wsa881x_init_thermal(&wsa881x->tz_pdata);
  970. snd_soc_add_codec_controls(codec, wsa_snd_controls,
  971. ARRAY_SIZE(wsa_snd_controls));
  972. INIT_DELAYED_WORK(&wsa881x->ocp_ctl_work, wsa881x_ocp_ctl_work);
  973. return 0;
  974. }
  975. static int wsa881x_remove(struct snd_soc_codec *codec)
  976. {
  977. struct wsa881x_priv *wsa881x = snd_soc_codec_get_drvdata(codec);
  978. if (wsa881x->tz_pdata.tz_dev)
  979. wsa881x_deinit_thermal(wsa881x->tz_pdata.tz_dev);
  980. mutex_destroy(&wsa881x->bg_lock);
  981. mutex_destroy(&wsa881x->res_lock);
  982. return 0;
  983. }
  984. static struct regmap *wsa881x_get_regmap(struct device *dev)
  985. {
  986. struct wsa881x_priv *control = swr_get_dev_data(to_swr_device(dev));
  987. if (!control)
  988. return NULL;
  989. return control->regmap;
  990. }
  991. static struct snd_soc_codec_driver soc_codec_dev_wsa881x = {
  992. .probe = wsa881x_probe,
  993. .remove = wsa881x_remove,
  994. .get_regmap = wsa881x_get_regmap,
  995. .component_driver = {
  996. .controls = wsa881x_snd_controls,
  997. .num_controls = ARRAY_SIZE(wsa881x_snd_controls),
  998. .dapm_widgets = wsa881x_dapm_widgets,
  999. .num_dapm_widgets = ARRAY_SIZE(wsa881x_dapm_widgets),
  1000. .dapm_routes = wsa881x_audio_map,
  1001. .num_dapm_routes = ARRAY_SIZE(wsa881x_audio_map),
  1002. },
  1003. };
  1004. static int wsa881x_gpio_ctrl(struct wsa881x_priv *wsa881x, bool enable)
  1005. {
  1006. int ret = 0;
  1007. if (wsa881x->pd_gpio < 0) {
  1008. dev_err(wsa881x->dev, "%s: gpio is not valid %d\n",
  1009. __func__, wsa881x->pd_gpio);
  1010. return -EINVAL;
  1011. }
  1012. if (wsa881x->wsa_rst_np) {
  1013. if (enable)
  1014. ret = msm_cdc_pinctrl_select_active_state(
  1015. wsa881x->wsa_rst_np);
  1016. else
  1017. ret = msm_cdc_pinctrl_select_sleep_state(
  1018. wsa881x->wsa_rst_np);
  1019. if (ret != 0)
  1020. dev_err(wsa881x->dev,
  1021. "%s: Failed to turn state %d; ret=%d\n",
  1022. __func__, enable, ret);
  1023. } else {
  1024. if (gpio_is_valid(wsa881x->pd_gpio))
  1025. gpio_direction_output(wsa881x->pd_gpio, enable);
  1026. }
  1027. return ret;
  1028. }
  1029. static int wsa881x_gpio_init(struct swr_device *pdev)
  1030. {
  1031. int ret = 0;
  1032. struct wsa881x_priv *wsa881x;
  1033. wsa881x = swr_get_dev_data(pdev);
  1034. if (!wsa881x) {
  1035. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1036. return -EINVAL;
  1037. }
  1038. dev_dbg(&pdev->dev, "%s: gpio %d request with name %s\n",
  1039. __func__, wsa881x->pd_gpio, dev_name(&pdev->dev));
  1040. ret = gpio_request(wsa881x->pd_gpio, dev_name(&pdev->dev));
  1041. if (ret) {
  1042. if (ret == -EBUSY) {
  1043. /* GPIO was already requested */
  1044. dev_dbg(&pdev->dev,
  1045. "%s: gpio %d is already set to high\n",
  1046. __func__, wsa881x->pd_gpio);
  1047. ret = 0;
  1048. } else {
  1049. dev_err(&pdev->dev, "%s: Failed to request gpio %d, err: %d\n",
  1050. __func__, wsa881x->pd_gpio, ret);
  1051. }
  1052. }
  1053. return ret;
  1054. }
  1055. static int wsa881x_swr_probe(struct swr_device *pdev)
  1056. {
  1057. int ret = 0;
  1058. struct wsa881x_priv *wsa881x;
  1059. u8 devnum = 0;
  1060. bool pin_state_current = false;
  1061. wsa881x = devm_kzalloc(&pdev->dev, sizeof(struct wsa881x_priv),
  1062. GFP_KERNEL);
  1063. if (!wsa881x)
  1064. return -ENOMEM;
  1065. wsa881x->wsa_rst_np = of_parse_phandle(pdev->dev.of_node,
  1066. "qcom,spkr-sd-n-node", 0);
  1067. if (!wsa881x->wsa_rst_np) {
  1068. dev_dbg(&pdev->dev, "%s: Not using pinctrl, fallback to gpio\n",
  1069. __func__);
  1070. wsa881x->pd_gpio = of_get_named_gpio(pdev->dev.of_node,
  1071. "qcom,spkr-sd-n-gpio", 0);
  1072. if (wsa881x->pd_gpio < 0) {
  1073. dev_err(&pdev->dev, "%s: %s property is not found %d\n",
  1074. __func__, "qcom,spkr-sd-n-gpio",
  1075. wsa881x->pd_gpio);
  1076. goto err;
  1077. }
  1078. dev_dbg(&pdev->dev, "%s: reset gpio %d\n", __func__,
  1079. wsa881x->pd_gpio);
  1080. }
  1081. swr_set_dev_data(pdev, wsa881x);
  1082. wsa881x->swr_slave = pdev;
  1083. if (!wsa881x->wsa_rst_np) {
  1084. ret = wsa881x_gpio_init(pdev);
  1085. if (ret)
  1086. goto err;
  1087. }
  1088. if (wsa881x->wsa_rst_np)
  1089. pin_state_current = msm_cdc_pinctrl_get_state(
  1090. wsa881x->wsa_rst_np);
  1091. wsa881x_gpio_ctrl(wsa881x, true);
  1092. wsa881x->state = WSA881X_DEV_UP;
  1093. if (!debugfs_wsa881x_dent) {
  1094. dbgwsa881x = wsa881x;
  1095. debugfs_wsa881x_dent = debugfs_create_dir(
  1096. "wsa881x_swr_slave", 0);
  1097. if (!IS_ERR(debugfs_wsa881x_dent)) {
  1098. debugfs_peek = debugfs_create_file("swrslave_peek",
  1099. S_IFREG | 0444, debugfs_wsa881x_dent,
  1100. (void *) "swrslave_peek",
  1101. &codec_debug_ops);
  1102. debugfs_poke = debugfs_create_file("swrslave_poke",
  1103. S_IFREG | 0444, debugfs_wsa881x_dent,
  1104. (void *) "swrslave_poke",
  1105. &codec_debug_ops);
  1106. debugfs_reg_dump = debugfs_create_file(
  1107. "swrslave_reg_dump",
  1108. S_IFREG | 0444,
  1109. debugfs_wsa881x_dent,
  1110. (void *) "swrslave_reg_dump",
  1111. &codec_debug_ops);
  1112. }
  1113. }
  1114. /*
  1115. * Add 5msec delay to provide sufficient time for
  1116. * soundwire auto enumeration of slave devices as
  1117. * as per HW requirement.
  1118. */
  1119. usleep_range(5000, 5010);
  1120. ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
  1121. if (ret) {
  1122. dev_dbg(&pdev->dev,
  1123. "%s get devnum %d for dev addr %lx failed\n",
  1124. __func__, devnum, pdev->addr);
  1125. goto dev_err;
  1126. }
  1127. pdev->dev_num = devnum;
  1128. wsa881x->regmap = devm_regmap_init_swr(pdev,
  1129. &wsa881x_regmap_config);
  1130. if (IS_ERR(wsa881x->regmap)) {
  1131. ret = PTR_ERR(wsa881x->regmap);
  1132. dev_err(&pdev->dev, "%s: regmap_init failed %d\n",
  1133. __func__, ret);
  1134. goto dev_err;
  1135. }
  1136. ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wsa881x,
  1137. NULL, 0);
  1138. if (ret) {
  1139. dev_err(&pdev->dev, "%s: Codec registration failed\n",
  1140. __func__);
  1141. goto dev_err;
  1142. }
  1143. return 0;
  1144. dev_err:
  1145. if (pin_state_current == false)
  1146. wsa881x_gpio_ctrl(wsa881x, false);
  1147. swr_remove_device(pdev);
  1148. err:
  1149. return ret;
  1150. }
  1151. static int wsa881x_swr_remove(struct swr_device *pdev)
  1152. {
  1153. struct wsa881x_priv *wsa881x;
  1154. wsa881x = swr_get_dev_data(pdev);
  1155. if (!wsa881x) {
  1156. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1157. return -EINVAL;
  1158. }
  1159. debugfs_remove_recursive(debugfs_wsa881x_dent);
  1160. debugfs_wsa881x_dent = NULL;
  1161. snd_soc_unregister_codec(&pdev->dev);
  1162. if (wsa881x->pd_gpio)
  1163. gpio_free(wsa881x->pd_gpio);
  1164. swr_set_dev_data(pdev, NULL);
  1165. return 0;
  1166. }
  1167. static int wsa881x_swr_up(struct swr_device *pdev)
  1168. {
  1169. int ret;
  1170. struct wsa881x_priv *wsa881x;
  1171. wsa881x = swr_get_dev_data(pdev);
  1172. if (!wsa881x) {
  1173. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1174. return -EINVAL;
  1175. }
  1176. ret = wsa881x_gpio_ctrl(wsa881x, true);
  1177. if (ret)
  1178. dev_err(&pdev->dev, "%s: Failed to enable gpio\n", __func__);
  1179. else
  1180. wsa881x->state = WSA881X_DEV_UP;
  1181. return ret;
  1182. }
  1183. static int wsa881x_swr_down(struct swr_device *pdev)
  1184. {
  1185. struct wsa881x_priv *wsa881x;
  1186. int ret;
  1187. wsa881x = swr_get_dev_data(pdev);
  1188. if (!wsa881x) {
  1189. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1190. return -EINVAL;
  1191. }
  1192. if (delayed_work_pending(&wsa881x->ocp_ctl_work))
  1193. cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
  1194. ret = wsa881x_gpio_ctrl(wsa881x, false);
  1195. if (ret)
  1196. dev_err(&pdev->dev, "%s: Failed to disable gpio\n", __func__);
  1197. else
  1198. wsa881x->state = WSA881X_DEV_DOWN;
  1199. return ret;
  1200. }
  1201. static int wsa881x_swr_reset(struct swr_device *pdev)
  1202. {
  1203. struct wsa881x_priv *wsa881x;
  1204. u8 retry = WSA881X_NUM_RETRY;
  1205. u8 devnum = 0;
  1206. wsa881x = swr_get_dev_data(pdev);
  1207. if (!wsa881x) {
  1208. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1209. return -EINVAL;
  1210. }
  1211. wsa881x->bg_cnt = 0;
  1212. wsa881x->clk_cnt = 0;
  1213. while (swr_get_logical_dev_num(pdev, pdev->addr, &devnum) && retry--) {
  1214. /* Retry after 1 msec delay */
  1215. usleep_range(1000, 1100);
  1216. }
  1217. pdev->dev_num = devnum;
  1218. regcache_mark_dirty(wsa881x->regmap);
  1219. regcache_sync(wsa881x->regmap);
  1220. return 0;
  1221. }
  1222. #ifdef CONFIG_PM_SLEEP
  1223. static int wsa881x_swr_suspend(struct device *dev)
  1224. {
  1225. dev_dbg(dev, "%s: system suspend\n", __func__);
  1226. return 0;
  1227. }
  1228. static int wsa881x_swr_resume(struct device *dev)
  1229. {
  1230. struct wsa881x_priv *wsa881x = swr_get_dev_data(to_swr_device(dev));
  1231. if (!wsa881x) {
  1232. dev_err(dev, "%s: wsa881x private data is NULL\n", __func__);
  1233. return -EINVAL;
  1234. }
  1235. dev_dbg(dev, "%s: system resume\n", __func__);
  1236. return 0;
  1237. }
  1238. #endif /* CONFIG_PM_SLEEP */
  1239. static const struct dev_pm_ops wsa881x_swr_pm_ops = {
  1240. SET_SYSTEM_SLEEP_PM_OPS(wsa881x_swr_suspend, wsa881x_swr_resume)
  1241. };
  1242. static const struct swr_device_id wsa881x_swr_id[] = {
  1243. {"wsa881x", 0},
  1244. {}
  1245. };
  1246. static const struct of_device_id wsa881x_swr_dt_match[] = {
  1247. {
  1248. .compatible = "qcom,wsa881x",
  1249. },
  1250. {}
  1251. };
  1252. static struct swr_driver wsa881x_codec_driver = {
  1253. .driver = {
  1254. .name = "wsa881x",
  1255. .owner = THIS_MODULE,
  1256. .pm = &wsa881x_swr_pm_ops,
  1257. .of_match_table = wsa881x_swr_dt_match,
  1258. },
  1259. .probe = wsa881x_swr_probe,
  1260. .remove = wsa881x_swr_remove,
  1261. .id_table = wsa881x_swr_id,
  1262. .device_up = wsa881x_swr_up,
  1263. .device_down = wsa881x_swr_down,
  1264. .reset_device = wsa881x_swr_reset,
  1265. };
  1266. static int __init wsa881x_codec_init(void)
  1267. {
  1268. return swr_driver_register(&wsa881x_codec_driver);
  1269. }
  1270. static void __exit wsa881x_codec_exit(void)
  1271. {
  1272. swr_driver_unregister(&wsa881x_codec_driver);
  1273. }
  1274. module_init(wsa881x_codec_init);
  1275. module_exit(wsa881x_codec_exit);
  1276. MODULE_DESCRIPTION("WSA881x Codec driver");
  1277. MODULE_LICENSE("GPL v2");