wsa881x.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, 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. };
  113. struct wsa_ctrl_platform_data {
  114. void *handle;
  115. int (*update_wsa_event)(void *handle, u16 event, u32 data);
  116. int (*register_notifier)(void *handle,
  117. struct notifier_block *nblock,
  118. bool enable);
  119. };
  120. #define SWR_SLV_MAX_REG_ADDR 0x390
  121. #define SWR_SLV_START_REG_ADDR 0x40
  122. #define SWR_SLV_MAX_BUF_LEN 25
  123. #define BYTES_PER_LINE 12
  124. #define SWR_SLV_RD_BUF_LEN 8
  125. #define SWR_SLV_WR_BUF_LEN 32
  126. #define SWR_SLV_MAX_DEVICES 2
  127. #define WSA881X_VERSION_ENTRY_SIZE 27
  128. #define WSA881X_OCP_CTL_TIMER_SEC 2
  129. #define WSA881X_OCP_CTL_TEMP_CELSIUS 25
  130. #define WSA881X_OCP_CTL_POLL_TIMER_SEC 60
  131. static int wsa881x_ocp_poll_timer_sec = WSA881X_OCP_CTL_POLL_TIMER_SEC;
  132. module_param(wsa881x_ocp_poll_timer_sec, int, 0664);
  133. MODULE_PARM_DESC(wsa881x_ocp_poll_timer_sec, "timer for ocp ctl polling");
  134. static struct wsa881x_priv *dbgwsa881x;
  135. static struct dentry *debugfs_wsa881x_dent;
  136. static struct dentry *debugfs_peek;
  137. static struct dentry *debugfs_poke;
  138. static struct dentry *debugfs_reg_dump;
  139. static unsigned int read_data;
  140. static unsigned int devnum;
  141. static int32_t wsa881x_resource_acquire(struct snd_soc_component *component,
  142. bool enable);
  143. static const char * const wsa_pa_gain_text[] = {
  144. "G_18_DB", "G_16P5_DB", "G_15_DB", "G_13P5_DB", "G_12_DB", "G_10P5_DB",
  145. "G_9_DB", "G_7P5_DB", "G_6_DB", "G_4P5_DB", "G_3_DB", "G_1P5_DB",
  146. "G_0_DB"
  147. };
  148. static const struct soc_enum wsa_pa_gain_enum =
  149. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_pa_gain_text), wsa_pa_gain_text);
  150. static int wsa_pa_gain_get(struct snd_kcontrol *kcontrol,
  151. struct snd_ctl_elem_value *ucontrol)
  152. {
  153. struct snd_soc_component *component =
  154. snd_soc_kcontrol_component(kcontrol);
  155. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  156. ucontrol->value.integer.value[0] = wsa881x->pa_gain;
  157. dev_dbg(component->dev, "%s: PA gain = 0x%x\n", __func__,
  158. wsa881x->pa_gain);
  159. return 0;
  160. }
  161. static int wsa_pa_gain_put(struct snd_kcontrol *kcontrol,
  162. struct snd_ctl_elem_value *ucontrol)
  163. {
  164. struct snd_soc_component *component =
  165. snd_soc_kcontrol_component(kcontrol);
  166. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  167. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  168. __func__, ucontrol->value.integer.value[0]);
  169. wsa881x->pa_gain = ucontrol->value.integer.value[0];
  170. return 0;
  171. }
  172. static int wsa881x_get_mute(struct snd_kcontrol *kcontrol,
  173. struct snd_ctl_elem_value *ucontrol)
  174. {
  175. struct snd_soc_component *component =
  176. snd_soc_kcontrol_component(kcontrol);
  177. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  178. ucontrol->value.integer.value[0] = wsa881x->pa_mute;
  179. return 0;
  180. }
  181. static int wsa881x_set_mute(struct snd_kcontrol *kcontrol,
  182. struct snd_ctl_elem_value *ucontrol)
  183. {
  184. struct snd_soc_component *component =
  185. snd_soc_kcontrol_component(kcontrol);
  186. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  187. int value = ucontrol->value.integer.value[0];
  188. dev_dbg(component->dev, "%s: mute current %d, new %d\n",
  189. __func__, wsa881x->pa_mute, value);
  190. if (value)
  191. snd_soc_component_update_bits(component, WSA881X_SPKR_DRV_EN,
  192. 0x80, 0x00);
  193. wsa881x->pa_mute = value;
  194. return 0;
  195. }
  196. static int wsa881x_get_t0_init(struct snd_kcontrol *kcontrol,
  197. struct snd_ctl_elem_value *ucontrol)
  198. {
  199. struct snd_soc_component *component =
  200. snd_soc_kcontrol_component(kcontrol);
  201. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  202. struct wsa881x_tz_priv *pdata = &wsa881x->tz_pdata;
  203. ucontrol->value.integer.value[0] = pdata->t0_init;
  204. dev_dbg(component->dev, "%s: t0 init %d\n", __func__, pdata->t0_init);
  205. return 0;
  206. }
  207. static int wsa881x_set_t0_init(struct snd_kcontrol *kcontrol,
  208. struct snd_ctl_elem_value *ucontrol)
  209. {
  210. struct snd_soc_component *component =
  211. snd_soc_kcontrol_component(kcontrol);
  212. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  213. struct wsa881x_tz_priv *pdata = &wsa881x->tz_pdata;
  214. pdata->t0_init = ucontrol->value.integer.value[0];
  215. dev_dbg(component->dev, "%s: t0 init %d\n", __func__, pdata->t0_init);
  216. return 0;
  217. }
  218. static const struct snd_kcontrol_new wsa_snd_controls[] = {
  219. SOC_ENUM_EXT("WSA PA Gain", wsa_pa_gain_enum,
  220. wsa_pa_gain_get, wsa_pa_gain_put),
  221. SOC_SINGLE_EXT("WSA PA Mute", SND_SOC_NOPM, 0, 1, 0,
  222. wsa881x_get_mute, wsa881x_set_mute),
  223. SOC_SINGLE_EXT("WSA T0 Init", SND_SOC_NOPM, 0, 1, 0,
  224. wsa881x_get_t0_init, wsa881x_set_t0_init),
  225. };
  226. static int codec_debug_open(struct inode *inode, struct file *file)
  227. {
  228. file->private_data = inode->i_private;
  229. return 0;
  230. }
  231. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  232. {
  233. char *token;
  234. int base, cnt;
  235. token = strsep(&buf, " ");
  236. for (cnt = 0; cnt < num_of_par; cnt++) {
  237. if (token) {
  238. if ((token[1] == 'x') || (token[1] == 'X'))
  239. base = 16;
  240. else
  241. base = 10;
  242. if (kstrtou32(token, base, &param1[cnt]) != 0)
  243. return -EINVAL;
  244. token = strsep(&buf, " ");
  245. } else
  246. return -EINVAL;
  247. }
  248. return 0;
  249. }
  250. static ssize_t wsa881x_codec_version_read(struct snd_info_entry *entry,
  251. void *file_private_data, struct file *file,
  252. char __user *buf, size_t count, loff_t pos)
  253. {
  254. struct wsa881x_priv *wsa881x;
  255. char buffer[WSA881X_VERSION_ENTRY_SIZE];
  256. int len;
  257. wsa881x = (struct wsa881x_priv *) entry->private_data;
  258. if (!wsa881x) {
  259. pr_err("%s: wsa881x priv is null\n", __func__);
  260. return -EINVAL;
  261. }
  262. len = snprintf(buffer, sizeof(buffer), "WSA881X-SOUNDWIRE_2_0\n");
  263. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  264. }
  265. static struct snd_info_entry_ops wsa881x_codec_info_ops = {
  266. .read = wsa881x_codec_version_read,
  267. };
  268. /*
  269. * wsa881x_codec_info_create_codec_entry - creates wsa881x module
  270. * @codec_root: The parent directory
  271. * @component: Codec instance
  272. *
  273. * Creates wsa881x module and version entry under the given
  274. * parent directory.
  275. *
  276. * Return: 0 on success or negative error code on failure.
  277. */
  278. int wsa881x_codec_info_create_codec_entry(struct snd_info_entry *codec_root,
  279. struct snd_soc_component *component)
  280. {
  281. struct snd_info_entry *version_entry;
  282. struct wsa881x_priv *wsa881x;
  283. struct snd_soc_card *card;
  284. char name[80];
  285. if (!codec_root || !component)
  286. return -EINVAL;
  287. wsa881x = snd_soc_component_get_drvdata(component);
  288. card = component->card;
  289. snprintf(name, sizeof(name), "%s.%x", "wsa881x",
  290. (u32)wsa881x->swr_slave->addr);
  291. wsa881x->entry = snd_info_create_subdir(codec_root->module,
  292. (const char *)name,
  293. codec_root);
  294. if (!wsa881x->entry) {
  295. dev_dbg(component->dev, "%s: failed to create wsa881x entry\n",
  296. __func__);
  297. return -ENOMEM;
  298. }
  299. version_entry = snd_info_create_card_entry(card->snd_card,
  300. "version",
  301. wsa881x->entry);
  302. if (!version_entry) {
  303. dev_dbg(component->dev, "%s: failed to create wsa881x version entry\n",
  304. __func__);
  305. return -ENOMEM;
  306. }
  307. version_entry->private_data = wsa881x;
  308. version_entry->size = WSA881X_VERSION_ENTRY_SIZE;
  309. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  310. version_entry->c.ops = &wsa881x_codec_info_ops;
  311. if (snd_info_register(version_entry) < 0) {
  312. snd_info_free_entry(version_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. component = wsa881x->component;
  853. wsa881x_get_temp(wsa881x->tz_pdata.tz_dev, &temp_val);
  854. dev_dbg(component->dev, " temp = %d\n", temp_val);
  855. if (temp_val <= WSA881X_OCP_CTL_TEMP_CELSIUS)
  856. snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
  857. 0xC0, 0x00);
  858. else
  859. snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
  860. 0xC0, 0xC0);
  861. schedule_delayed_work(&wsa881x->ocp_ctl_work,
  862. msecs_to_jiffies(wsa881x_ocp_poll_timer_sec * 1000));
  863. }
  864. static int wsa881x_spkr_pa_event(struct snd_soc_dapm_widget *w,
  865. struct snd_kcontrol *kcontrol, int event)
  866. {
  867. struct snd_soc_component *component =
  868. snd_soc_dapm_to_component(w->dapm);
  869. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  870. int min_gain, max_gain;
  871. dev_dbg(component->dev, "%s: %s %d\n", __func__, w->name, event);
  872. switch (event) {
  873. case SND_SOC_DAPM_PRE_PMU:
  874. snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
  875. 0xC0, 0x80);
  876. regmap_multi_reg_write(wsa881x->regmap,
  877. wsa881x_pre_pmu_pa_2_0,
  878. ARRAY_SIZE(wsa881x_pre_pmu_pa_2_0));
  879. swr_slvdev_datapath_control(wsa881x->swr_slave,
  880. wsa881x->swr_slave->dev_num,
  881. true);
  882. /* Set register mode if compander is not enabled */
  883. if (!wsa881x->comp_enable)
  884. snd_soc_component_update_bits(component,
  885. WSA881X_SPKR_DRV_GAIN,
  886. 0x08, 0x08);
  887. else
  888. snd_soc_component_update_bits(component,
  889. WSA881X_SPKR_DRV_GAIN,
  890. 0x08, 0x00);
  891. break;
  892. case SND_SOC_DAPM_POST_PMU:
  893. if (!wsa881x->bolero_dev)
  894. snd_soc_component_update_bits(component,
  895. WSA881X_SPKR_DRV_EN,
  896. 0x80, 0x80);
  897. if (!wsa881x->comp_enable) {
  898. max_gain = wsa881x->pa_gain;
  899. /*
  900. * Gain has to set incrementally in 4 steps
  901. * as per HW sequence
  902. */
  903. if (max_gain > G_4P5DB)
  904. min_gain = G_0DB;
  905. else
  906. min_gain = max_gain + 3;
  907. /*
  908. * 1ms delay is needed before change in gain
  909. * as per HW requirement.
  910. */
  911. usleep_range(1000, 1010);
  912. wsa881x_ramp_pa_gain(component, min_gain, max_gain,
  913. 1000);
  914. }
  915. if (wsa881x->visense_enable) {
  916. wsa881x_visense_txfe_ctrl(component, ENABLE,
  917. 0x00, 0x03, 0x01);
  918. snd_soc_component_update_bits(component,
  919. WSA881X_ADC_EN_SEL_IBAIS,
  920. 0x07, 0x01);
  921. wsa881x_visense_adc_ctrl(component, ENABLE);
  922. }
  923. schedule_delayed_work(&wsa881x->ocp_ctl_work,
  924. msecs_to_jiffies(WSA881X_OCP_CTL_TIMER_SEC * 1000));
  925. /* Force remove group */
  926. swr_remove_from_group(wsa881x->swr_slave,
  927. wsa881x->swr_slave->dev_num);
  928. break;
  929. case SND_SOC_DAPM_POST_PMD:
  930. snd_soc_component_update_bits(component,
  931. WSA881X_SPKR_DRV_EN,
  932. 0x80, 0x00);
  933. if (wsa881x->visense_enable) {
  934. wsa881x_visense_adc_ctrl(component, DISABLE);
  935. wsa881x_visense_txfe_ctrl(component, DISABLE,
  936. 0x00, 0x01, 0x01);
  937. }
  938. cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
  939. snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
  940. 0xC0, 0xC0);
  941. break;
  942. }
  943. return 0;
  944. }
  945. static const struct snd_soc_dapm_widget wsa881x_dapm_widgets[] = {
  946. SND_SOC_DAPM_INPUT("IN"),
  947. SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0, swr_dac_port,
  948. ARRAY_SIZE(swr_dac_port), wsa881x_enable_swr_dac_port,
  949. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  950. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  951. SND_SOC_DAPM_DAC_E("RDAC", NULL, WSA881X_SPKR_DAC_CTL, 7, 0,
  952. wsa881x_rdac_event,
  953. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  954. SND_SOC_DAPM_PGA_E("SPKR PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
  955. wsa881x_spkr_pa_event, SND_SOC_DAPM_PRE_PMU |
  956. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  957. SND_SOC_DAPM_OUTPUT("SPKR"),
  958. };
  959. static const struct snd_soc_dapm_route wsa881x_audio_map[] = {
  960. {"SWR DAC_Port", "Switch", "IN"},
  961. {"RDAC", NULL, "SWR DAC_Port"},
  962. {"SPKR PGA", NULL, "RDAC"},
  963. {"SPKR", NULL, "SPKR PGA"},
  964. };
  965. int wsa881x_set_channel_map(struct snd_soc_component *component, u8 *port,
  966. u8 num_port, unsigned int *ch_mask,
  967. unsigned int *ch_rate, u8 *port_type)
  968. {
  969. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  970. int i;
  971. if (!port || !ch_mask || !ch_rate ||
  972. (num_port > WSA881X_MAX_SWR_PORTS)) {
  973. dev_err(component->dev,
  974. "%s: Invalid port=%pK, ch_mask=%pK, ch_rate=%pK\n",
  975. __func__, port, ch_mask, ch_rate);
  976. return -EINVAL;
  977. }
  978. for (i = 0; i < num_port; i++) {
  979. wsa881x->port[i].port_id = port[i];
  980. wsa881x->port[i].ch_mask = ch_mask[i];
  981. wsa881x->port[i].ch_rate = ch_rate[i];
  982. wsa881x->port[i].num_ch = __sw_hweight8(ch_mask[i]);
  983. if (port_type)
  984. wsa881x->port[i].port_type = port_type[i];
  985. }
  986. return 0;
  987. }
  988. EXPORT_SYMBOL(wsa881x_set_channel_map);
  989. static void wsa881x_init(struct snd_soc_component *component)
  990. {
  991. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  992. wsa881x->version =
  993. snd_soc_component_read32(component, WSA881X_CHIP_ID1);
  994. wsa881x_regmap_defaults(wsa881x->regmap, wsa881x->version);
  995. /* Enable software reset output from soundwire slave */
  996. snd_soc_component_update_bits(component, WSA881X_SWR_RESET_EN,
  997. 0x07, 0x07);
  998. /* Bring out of analog reset */
  999. snd_soc_component_update_bits(component, WSA881X_CDC_RST_CTL,
  1000. 0x02, 0x02);
  1001. /* Bring out of digital reset */
  1002. snd_soc_component_update_bits(component, WSA881X_CDC_RST_CTL,
  1003. 0x01, 0x01);
  1004. snd_soc_component_update_bits(component, WSA881X_CLOCK_CONFIG,
  1005. 0x10, 0x10);
  1006. snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
  1007. 0x02, 0x02);
  1008. snd_soc_component_update_bits(component, WSA881X_SPKR_MISC_CTL1,
  1009. 0xC0, 0x80);
  1010. snd_soc_component_update_bits(component, WSA881X_SPKR_MISC_CTL1,
  1011. 0x06, 0x06);
  1012. snd_soc_component_update_bits(component, WSA881X_SPKR_BIAS_INT,
  1013. 0xFF, 0x00);
  1014. snd_soc_component_update_bits(component, WSA881X_SPKR_PA_INT,
  1015. 0xF0, 0x40);
  1016. snd_soc_component_update_bits(component, WSA881X_SPKR_PA_INT,
  1017. 0x0E, 0x0E);
  1018. snd_soc_component_update_bits(component, WSA881X_BOOST_LOOP_STABILITY,
  1019. 0x03, 0x03);
  1020. snd_soc_component_update_bits(component, WSA881X_BOOST_MISC2_CTL,
  1021. 0xFF, 0x14);
  1022. snd_soc_component_update_bits(component, WSA881X_BOOST_START_CTL,
  1023. 0x80, 0x80);
  1024. snd_soc_component_update_bits(component, WSA881X_BOOST_START_CTL,
  1025. 0x03, 0x00);
  1026. snd_soc_component_update_bits(component,
  1027. WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
  1028. 0x0C, 0x04);
  1029. snd_soc_component_update_bits(component,
  1030. WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
  1031. 0x03, 0x00);
  1032. if (snd_soc_component_read32(component, WSA881X_OTP_REG_0))
  1033. snd_soc_component_update_bits(component,
  1034. WSA881X_BOOST_PRESET_OUT1,
  1035. 0xF0, 0x70);
  1036. snd_soc_component_update_bits(component, WSA881X_BOOST_PRESET_OUT2,
  1037. 0xF0, 0x30);
  1038. snd_soc_component_update_bits(component, WSA881X_SPKR_DRV_EN,
  1039. 0x08, 0x08);
  1040. snd_soc_component_update_bits(component, WSA881X_BOOST_CURRENT_LIMIT,
  1041. 0x0F, 0x08);
  1042. snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
  1043. 0x30, 0x30);
  1044. snd_soc_component_update_bits(component, WSA881X_SPKR_OCP_CTL,
  1045. 0x0C, 0x00);
  1046. snd_soc_component_update_bits(component, WSA881X_OTP_REG_28,
  1047. 0x3F, 0x3A);
  1048. snd_soc_component_update_bits(component, WSA881X_BONGO_RESRV_REG1,
  1049. 0xFF, 0xB2);
  1050. snd_soc_component_update_bits(component, WSA881X_BONGO_RESRV_REG2,
  1051. 0xFF, 0x05);
  1052. }
  1053. static int32_t wsa881x_resource_acquire(struct snd_soc_component *component,
  1054. bool enable)
  1055. {
  1056. wsa881x_clk_ctrl(component, enable);
  1057. wsa881x_bandgap_ctrl(component, enable);
  1058. return 0;
  1059. }
  1060. static int32_t wsa881x_temp_reg_read(struct snd_soc_component *component,
  1061. struct wsa_temp_register *wsa_temp_reg)
  1062. {
  1063. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  1064. struct swr_device *dev;
  1065. u8 retry = WSA881X_NUM_RETRY;
  1066. u8 devnum = 0;
  1067. if (!wsa881x) {
  1068. dev_err(component->dev, "%s: wsa881x is NULL\n", __func__);
  1069. return -EINVAL;
  1070. }
  1071. dev = wsa881x->swr_slave;
  1072. if (dev && (wsa881x->state == WSA881X_DEV_DOWN)) {
  1073. while (swr_get_logical_dev_num(dev, dev->addr, &devnum) &&
  1074. retry--) {
  1075. /* Retry after 1 msec delay */
  1076. usleep_range(1000, 1100);
  1077. }
  1078. if (retry == 0) {
  1079. dev_err(component->dev,
  1080. "%s get devnum %d for dev addr %lx failed\n",
  1081. __func__, devnum, dev->addr);
  1082. return -EINVAL;
  1083. }
  1084. }
  1085. wsa881x_regcache_sync(wsa881x);
  1086. mutex_lock(&wsa881x->temp_lock);
  1087. wsa881x_resource_acquire(component, ENABLE);
  1088. snd_soc_component_update_bits(component, WSA881X_TADC_VALUE_CTL,
  1089. 0x01, 0x00);
  1090. wsa_temp_reg->dmeas_msb = snd_soc_component_read32(
  1091. component, WSA881X_TEMP_MSB);
  1092. wsa_temp_reg->dmeas_lsb = snd_soc_component_read32(
  1093. component, WSA881X_TEMP_LSB);
  1094. snd_soc_component_update_bits(component, WSA881X_TADC_VALUE_CTL,
  1095. 0x01, 0x01);
  1096. wsa_temp_reg->d1_msb = snd_soc_component_read32(
  1097. component, WSA881X_OTP_REG_1);
  1098. wsa_temp_reg->d1_lsb = snd_soc_component_read32(
  1099. component, WSA881X_OTP_REG_2);
  1100. wsa_temp_reg->d2_msb = snd_soc_component_read32(
  1101. component, WSA881X_OTP_REG_3);
  1102. wsa_temp_reg->d2_lsb = snd_soc_component_read32(
  1103. component, WSA881X_OTP_REG_4);
  1104. wsa881x_resource_acquire(component, DISABLE);
  1105. mutex_unlock(&wsa881x->temp_lock);
  1106. return 0;
  1107. }
  1108. static int wsa881x_probe(struct snd_soc_component *component)
  1109. {
  1110. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  1111. struct swr_device *dev;
  1112. if (!wsa881x)
  1113. return -EINVAL;
  1114. snd_soc_component_init_regmap(component, wsa881x->regmap);
  1115. dev = wsa881x->swr_slave;
  1116. wsa881x->component = component;
  1117. mutex_init(&wsa881x->bg_lock);
  1118. wsa881x_init(component);
  1119. snprintf(wsa881x->tz_pdata.name, sizeof(wsa881x->tz_pdata.name),
  1120. "%s.%x", "wsatz", (u8)dev->addr);
  1121. wsa881x->bg_cnt = 0;
  1122. wsa881x->clk_cnt = 0;
  1123. wsa881x->tz_pdata.component = component;
  1124. wsa881x->tz_pdata.wsa_temp_reg_read = wsa881x_temp_reg_read;
  1125. wsa881x_init_thermal(&wsa881x->tz_pdata);
  1126. snd_soc_add_component_controls(component, wsa_snd_controls,
  1127. ARRAY_SIZE(wsa_snd_controls));
  1128. INIT_DELAYED_WORK(&wsa881x->ocp_ctl_work, wsa881x_ocp_ctl_work);
  1129. return 0;
  1130. }
  1131. static void wsa881x_remove(struct snd_soc_component *component)
  1132. {
  1133. struct wsa881x_priv *wsa881x = snd_soc_component_get_drvdata(component);
  1134. if (wsa881x->tz_pdata.tz_dev)
  1135. wsa881x_deinit_thermal(wsa881x->tz_pdata.tz_dev);
  1136. mutex_destroy(&wsa881x->bg_lock);
  1137. return;
  1138. }
  1139. static const struct snd_soc_component_driver soc_codec_dev_wsa881x = {
  1140. .name = DRV_NAME,
  1141. .probe = wsa881x_probe,
  1142. .remove = wsa881x_remove,
  1143. .controls = wsa881x_snd_controls,
  1144. .num_controls = ARRAY_SIZE(wsa881x_snd_controls),
  1145. .dapm_widgets = wsa881x_dapm_widgets,
  1146. .num_dapm_widgets = ARRAY_SIZE(wsa881x_dapm_widgets),
  1147. .dapm_routes = wsa881x_audio_map,
  1148. .num_dapm_routes = ARRAY_SIZE(wsa881x_audio_map),
  1149. };
  1150. static int wsa881x_gpio_ctrl(struct wsa881x_priv *wsa881x, bool enable)
  1151. {
  1152. int ret = 0;
  1153. if (wsa881x->pd_gpio < 0) {
  1154. dev_err(wsa881x->dev, "%s: gpio is not valid %d\n",
  1155. __func__, wsa881x->pd_gpio);
  1156. return -EINVAL;
  1157. }
  1158. if (wsa881x->wsa_rst_np) {
  1159. if (enable)
  1160. ret = msm_cdc_pinctrl_select_active_state(
  1161. wsa881x->wsa_rst_np);
  1162. else
  1163. ret = msm_cdc_pinctrl_select_sleep_state(
  1164. wsa881x->wsa_rst_np);
  1165. if (ret != 0)
  1166. dev_err(wsa881x->dev,
  1167. "%s: Failed to turn state %d; ret=%d\n",
  1168. __func__, enable, ret);
  1169. } else {
  1170. if (gpio_is_valid(wsa881x->pd_gpio))
  1171. gpio_direction_output(wsa881x->pd_gpio, enable);
  1172. }
  1173. return ret;
  1174. }
  1175. static int wsa881x_gpio_init(struct swr_device *pdev)
  1176. {
  1177. int ret = 0;
  1178. struct wsa881x_priv *wsa881x;
  1179. wsa881x = swr_get_dev_data(pdev);
  1180. if (!wsa881x) {
  1181. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1182. return -EINVAL;
  1183. }
  1184. dev_dbg(&pdev->dev, "%s: gpio %d request with name %s\n",
  1185. __func__, wsa881x->pd_gpio, dev_name(&pdev->dev));
  1186. ret = gpio_request(wsa881x->pd_gpio, dev_name(&pdev->dev));
  1187. if (ret) {
  1188. if (ret == -EBUSY) {
  1189. /* GPIO was already requested */
  1190. dev_dbg(&pdev->dev,
  1191. "%s: gpio %d is already set to high\n",
  1192. __func__, wsa881x->pd_gpio);
  1193. ret = 0;
  1194. } else {
  1195. dev_err(&pdev->dev, "%s: Failed to request gpio %d, err: %d\n",
  1196. __func__, wsa881x->pd_gpio, ret);
  1197. }
  1198. }
  1199. return ret;
  1200. }
  1201. static int wsa881x_event_notify(struct notifier_block *nb,
  1202. unsigned long val, void *ptr)
  1203. {
  1204. u16 event = (val & 0xffff);
  1205. struct wsa881x_priv *wsa881x = container_of(nb, struct wsa881x_priv,
  1206. bolero_nblock);
  1207. if (!wsa881x)
  1208. return -EINVAL;
  1209. switch (event) {
  1210. case BOLERO_WSA_EVT_PA_OFF_PRE_SSR:
  1211. snd_soc_component_update_bits(wsa881x->component,
  1212. WSA881X_SPKR_DRV_GAIN,
  1213. 0xF0, 0xC0);
  1214. snd_soc_component_update_bits(wsa881x->component,
  1215. WSA881X_SPKR_DRV_EN,
  1216. 0x80, 0x00);
  1217. break;
  1218. case BOLERO_WSA_EVT_PA_ON_POST_FSCLK:
  1219. if ((snd_soc_component_read32(wsa881x->component,
  1220. WSA881X_SPKR_DAC_CTL) & 0x80) == 0x80)
  1221. snd_soc_component_update_bits(wsa881x->component,
  1222. WSA881X_SPKR_DRV_EN,
  1223. 0x80, 0x80);
  1224. default:
  1225. break;
  1226. }
  1227. return 0;
  1228. }
  1229. static int wsa881x_swr_probe(struct swr_device *pdev)
  1230. {
  1231. int ret = 0;
  1232. struct wsa881x_priv *wsa881x;
  1233. u8 devnum = 0;
  1234. bool pin_state_current = false;
  1235. struct wsa_ctrl_platform_data *plat_data = NULL;
  1236. wsa881x = devm_kzalloc(&pdev->dev, sizeof(struct wsa881x_priv),
  1237. GFP_KERNEL);
  1238. if (!wsa881x)
  1239. return -ENOMEM;
  1240. wsa881x->wsa_rst_np = of_parse_phandle(pdev->dev.of_node,
  1241. "qcom,spkr-sd-n-node", 0);
  1242. if (!wsa881x->wsa_rst_np) {
  1243. dev_dbg(&pdev->dev, "%s: Not using pinctrl, fallback to gpio\n",
  1244. __func__);
  1245. wsa881x->pd_gpio = of_get_named_gpio(pdev->dev.of_node,
  1246. "qcom,spkr-sd-n-gpio", 0);
  1247. if (wsa881x->pd_gpio < 0) {
  1248. dev_err(&pdev->dev, "%s: %s property is not found %d\n",
  1249. __func__, "qcom,spkr-sd-n-gpio",
  1250. wsa881x->pd_gpio);
  1251. goto err;
  1252. }
  1253. dev_dbg(&pdev->dev, "%s: reset gpio %d\n", __func__,
  1254. wsa881x->pd_gpio);
  1255. }
  1256. swr_set_dev_data(pdev, wsa881x);
  1257. wsa881x->swr_slave = pdev;
  1258. if (!wsa881x->wsa_rst_np) {
  1259. ret = wsa881x_gpio_init(pdev);
  1260. if (ret)
  1261. goto err;
  1262. }
  1263. if (wsa881x->wsa_rst_np)
  1264. pin_state_current = msm_cdc_pinctrl_get_state(
  1265. wsa881x->wsa_rst_np);
  1266. wsa881x_gpio_ctrl(wsa881x, true);
  1267. wsa881x->state = WSA881X_DEV_UP;
  1268. if (!debugfs_wsa881x_dent) {
  1269. dbgwsa881x = wsa881x;
  1270. debugfs_wsa881x_dent = debugfs_create_dir(
  1271. "wsa881x_swr_slave", 0);
  1272. if (!IS_ERR(debugfs_wsa881x_dent)) {
  1273. debugfs_peek = debugfs_create_file("swrslave_peek",
  1274. S_IFREG | 0444, debugfs_wsa881x_dent,
  1275. (void *) "swrslave_peek",
  1276. &codec_debug_ops);
  1277. debugfs_poke = debugfs_create_file("swrslave_poke",
  1278. S_IFREG | 0444, debugfs_wsa881x_dent,
  1279. (void *) "swrslave_poke",
  1280. &codec_debug_ops);
  1281. debugfs_reg_dump = debugfs_create_file(
  1282. "swrslave_reg_dump",
  1283. S_IFREG | 0444,
  1284. debugfs_wsa881x_dent,
  1285. (void *) "swrslave_reg_dump",
  1286. &codec_debug_ops);
  1287. }
  1288. }
  1289. /*
  1290. * Add 5msec delay to provide sufficient time for
  1291. * soundwire auto enumeration of slave devices as
  1292. * as per HW requirement.
  1293. */
  1294. usleep_range(5000, 5010);
  1295. ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
  1296. if (ret) {
  1297. dev_dbg(&pdev->dev,
  1298. "%s get devnum %d for dev addr %lx failed\n",
  1299. __func__, devnum, pdev->addr);
  1300. goto dev_err;
  1301. }
  1302. pdev->dev_num = devnum;
  1303. wsa881x->regmap = devm_regmap_init_swr(pdev,
  1304. &wsa881x_regmap_config);
  1305. if (IS_ERR(wsa881x->regmap)) {
  1306. ret = PTR_ERR(wsa881x->regmap);
  1307. dev_err(&pdev->dev, "%s: regmap_init failed %d\n",
  1308. __func__, ret);
  1309. goto dev_err;
  1310. }
  1311. ret = snd_soc_register_component(&pdev->dev, &soc_codec_dev_wsa881x,
  1312. NULL, 0);
  1313. if (ret) {
  1314. dev_err(&pdev->dev, "%s: Codec registration failed\n",
  1315. __func__);
  1316. goto dev_err;
  1317. }
  1318. wsa881x->bolero_np = of_parse_phandle(pdev->dev.of_node,
  1319. "qcom,bolero-handle", 0);
  1320. if (wsa881x->bolero_np) {
  1321. wsa881x->bolero_dev =
  1322. of_find_device_by_node(wsa881x->bolero_np);
  1323. if (wsa881x->bolero_dev) {
  1324. plat_data = dev_get_platdata(&wsa881x->bolero_dev->dev);
  1325. if (plat_data) {
  1326. wsa881x->bolero_nblock.notifier_call =
  1327. wsa881x_event_notify;
  1328. if (plat_data->register_notifier)
  1329. plat_data->register_notifier(
  1330. plat_data->handle,
  1331. &wsa881x->bolero_nblock,
  1332. true);
  1333. wsa881x->register_notifier =
  1334. plat_data->register_notifier;
  1335. wsa881x->handle = plat_data->handle;
  1336. } else {
  1337. dev_err(&pdev->dev, "%s: plat data not found\n",
  1338. __func__);
  1339. }
  1340. } else {
  1341. dev_err(&pdev->dev, "%s: bolero dev not found\n",
  1342. __func__);
  1343. }
  1344. } else {
  1345. dev_info(&pdev->dev, "%s: bolero node not found\n", __func__);
  1346. }
  1347. mutex_init(&wsa881x->res_lock);
  1348. mutex_init(&wsa881x->temp_lock);
  1349. return 0;
  1350. dev_err:
  1351. if (pin_state_current == false)
  1352. wsa881x_gpio_ctrl(wsa881x, false);
  1353. swr_remove_device(pdev);
  1354. err:
  1355. return ret;
  1356. }
  1357. static int wsa881x_swr_remove(struct swr_device *pdev)
  1358. {
  1359. struct wsa881x_priv *wsa881x;
  1360. wsa881x = swr_get_dev_data(pdev);
  1361. if (!wsa881x) {
  1362. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1363. return -EINVAL;
  1364. }
  1365. if (wsa881x->register_notifier)
  1366. wsa881x->register_notifier(wsa881x->handle,
  1367. &wsa881x->bolero_nblock, false);
  1368. debugfs_remove_recursive(debugfs_wsa881x_dent);
  1369. debugfs_wsa881x_dent = NULL;
  1370. mutex_destroy(&wsa881x->res_lock);
  1371. mutex_destroy(&wsa881x->temp_lock);
  1372. snd_soc_unregister_component(&pdev->dev);
  1373. if (wsa881x->pd_gpio)
  1374. gpio_free(wsa881x->pd_gpio);
  1375. swr_set_dev_data(pdev, NULL);
  1376. return 0;
  1377. }
  1378. static int wsa881x_swr_up(struct swr_device *pdev)
  1379. {
  1380. int ret;
  1381. struct wsa881x_priv *wsa881x;
  1382. wsa881x = swr_get_dev_data(pdev);
  1383. if (!wsa881x) {
  1384. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1385. return -EINVAL;
  1386. }
  1387. ret = wsa881x_gpio_ctrl(wsa881x, true);
  1388. if (ret)
  1389. dev_err(&pdev->dev, "%s: Failed to enable gpio\n", __func__);
  1390. else
  1391. wsa881x->state = WSA881X_DEV_UP;
  1392. return ret;
  1393. }
  1394. static int wsa881x_swr_down(struct swr_device *pdev)
  1395. {
  1396. struct wsa881x_priv *wsa881x;
  1397. int ret;
  1398. wsa881x = swr_get_dev_data(pdev);
  1399. if (!wsa881x) {
  1400. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1401. return -EINVAL;
  1402. }
  1403. if (delayed_work_pending(&wsa881x->ocp_ctl_work))
  1404. cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
  1405. ret = wsa881x_gpio_ctrl(wsa881x, false);
  1406. if (ret)
  1407. dev_err(&pdev->dev, "%s: Failed to disable gpio\n", __func__);
  1408. else
  1409. wsa881x->state = WSA881X_DEV_DOWN;
  1410. return ret;
  1411. }
  1412. static int wsa881x_swr_reset(struct swr_device *pdev)
  1413. {
  1414. struct wsa881x_priv *wsa881x;
  1415. u8 retry = WSA881X_NUM_RETRY;
  1416. u8 devnum = 0;
  1417. wsa881x = swr_get_dev_data(pdev);
  1418. if (!wsa881x) {
  1419. dev_err(&pdev->dev, "%s: wsa881x is NULL\n", __func__);
  1420. return -EINVAL;
  1421. }
  1422. if (wsa881x->state == WSA881X_DEV_READY) {
  1423. dev_dbg(&pdev->dev, "%s: device already active\n", __func__);
  1424. return 0;
  1425. }
  1426. wsa881x->bg_cnt = 0;
  1427. wsa881x->clk_cnt = 0;
  1428. while (swr_get_logical_dev_num(pdev, pdev->addr, &devnum) && retry--) {
  1429. /* Retry after 1 msec delay */
  1430. usleep_range(1000, 1100);
  1431. }
  1432. pdev->dev_num = devnum;
  1433. wsa881x_regcache_sync(wsa881x);
  1434. return 0;
  1435. }
  1436. #ifdef CONFIG_PM_SLEEP
  1437. static int wsa881x_swr_suspend(struct device *dev)
  1438. {
  1439. dev_dbg(dev, "%s: system suspend\n", __func__);
  1440. return 0;
  1441. }
  1442. static int wsa881x_swr_resume(struct device *dev)
  1443. {
  1444. struct wsa881x_priv *wsa881x = swr_get_dev_data(to_swr_device(dev));
  1445. if (!wsa881x) {
  1446. dev_err(dev, "%s: wsa881x private data is NULL\n", __func__);
  1447. return -EINVAL;
  1448. }
  1449. dev_dbg(dev, "%s: system resume\n", __func__);
  1450. return 0;
  1451. }
  1452. #endif /* CONFIG_PM_SLEEP */
  1453. static const struct dev_pm_ops wsa881x_swr_pm_ops = {
  1454. SET_SYSTEM_SLEEP_PM_OPS(wsa881x_swr_suspend, wsa881x_swr_resume)
  1455. };
  1456. static const struct swr_device_id wsa881x_swr_id[] = {
  1457. {"wsa881x", 0},
  1458. {}
  1459. };
  1460. static const struct of_device_id wsa881x_swr_dt_match[] = {
  1461. {
  1462. .compatible = "qcom,wsa881x",
  1463. },
  1464. {}
  1465. };
  1466. static struct swr_driver wsa881x_codec_driver = {
  1467. .driver = {
  1468. .name = "wsa881x",
  1469. .owner = THIS_MODULE,
  1470. .pm = &wsa881x_swr_pm_ops,
  1471. .of_match_table = wsa881x_swr_dt_match,
  1472. },
  1473. .probe = wsa881x_swr_probe,
  1474. .remove = wsa881x_swr_remove,
  1475. .id_table = wsa881x_swr_id,
  1476. .device_up = wsa881x_swr_up,
  1477. .device_down = wsa881x_swr_down,
  1478. .reset_device = wsa881x_swr_reset,
  1479. };
  1480. static int __init wsa881x_codec_init(void)
  1481. {
  1482. return swr_driver_register(&wsa881x_codec_driver);
  1483. }
  1484. static void __exit wsa881x_codec_exit(void)
  1485. {
  1486. swr_driver_unregister(&wsa881x_codec_driver);
  1487. }
  1488. module_init(wsa881x_codec_init);
  1489. module_exit(wsa881x_codec_exit);
  1490. MODULE_DESCRIPTION("WSA881x Codec driver");
  1491. MODULE_LICENSE("GPL v2");