wsa881x.c 43 KB

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