wsa881x.c 42 KB

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