wsa881x.c 46 KB

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