wsa881x.c 41 KB

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