wsa881x.c 46 KB

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