wsa883x.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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/regmap.h>
  19. #include <linux/debugfs.h>
  20. #include <soc/soundwire.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dapm.h>
  25. #include <sound/tlv.h>
  26. #include <asoc/msm-cdc-pinctrl.h>
  27. #include "wsa883x.h"
  28. #include "internal.h"
  29. #define T1_TEMP -10
  30. #define T2_TEMP 150
  31. #define LOW_TEMP_THRESHOLD 5
  32. #define HIGH_TEMP_THRESHOLD 45
  33. #define TEMP_INVALID 0xFFFF
  34. #define WSA883X_TEMP_RETRY 3
  35. enum {
  36. WSA_4OHMS =4,
  37. WSA_8OHMS = 8,
  38. WSA_16OHMS = 16,
  39. WSA_32OHMS = 32,
  40. };
  41. struct wsa_temp_register {
  42. u8 d1_msb;
  43. u8 d1_lsb;
  44. u8 d2_msb;
  45. u8 d2_lsb;
  46. u8 dmeas_msb;
  47. u8 dmeas_lsb;
  48. };
  49. static int wsa883x_get_temperature(struct snd_soc_component *component,
  50. int *temp);
  51. #ifdef CONFIG_DEBUG_FS
  52. static int codec_debug_open(struct inode *inode, struct file *file)
  53. {
  54. file->private_data = inode->i_private;
  55. return 0;
  56. }
  57. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  58. {
  59. char *token;
  60. int base, cnt;
  61. token = strsep(&buf, " ");
  62. for (cnt = 0; cnt < num_of_par; cnt++) {
  63. if (token) {
  64. if ((token[1] == 'x') || (token[1] == 'X'))
  65. base = 16;
  66. else
  67. base = 10;
  68. if (kstrtou32(token, base, &param1[cnt]) != 0)
  69. return -EINVAL;
  70. token = strsep(&buf, " ");
  71. } else {
  72. return -EINVAL;
  73. }
  74. }
  75. return 0;
  76. }
  77. static bool is_swr_slave_reg_readable(int reg)
  78. {
  79. int ret = true;
  80. if (((reg > 0x46) && (reg < 0x4A)) ||
  81. ((reg > 0x4A) && (reg < 0x50)) ||
  82. ((reg > 0x55) && (reg < 0xD0)) ||
  83. ((reg > 0xD0) && (reg < 0xE0)) ||
  84. ((reg > 0xE0) && (reg < 0xF0)) ||
  85. ((reg > 0xF0) && (reg < 0x100)) ||
  86. ((reg > 0x105) && (reg < 0x120)) ||
  87. ((reg > 0x205) && (reg < 0x220)) ||
  88. ((reg > 0x305) && (reg < 0x320)) ||
  89. ((reg > 0x405) && (reg < 0x420)) ||
  90. ((reg > 0x128) && (reg < 0x130)) ||
  91. ((reg > 0x228) && (reg < 0x230)) ||
  92. ((reg > 0x328) && (reg < 0x330)) ||
  93. ((reg > 0x428) && (reg < 0x430)) ||
  94. ((reg > 0x138) && (reg < 0x205)) ||
  95. ((reg > 0x238) && (reg < 0x305)) ||
  96. ((reg > 0x338) && (reg < 0x405)) ||
  97. ((reg > 0x405) && (reg < 0xF00)) ||
  98. ((reg > 0xF05) && (reg < 0xF20)) ||
  99. ((reg > 0xF25) && (reg < 0xF30)) ||
  100. ((reg > 0xF35) && (reg < 0x2000)))
  101. ret = false;
  102. return ret;
  103. }
  104. static ssize_t swr_slave_reg_show(struct swr_device *pdev, char __user *ubuf,
  105. size_t count, loff_t *ppos)
  106. {
  107. int i, reg_val, len;
  108. ssize_t total = 0;
  109. char tmp_buf[SWR_SLV_MAX_BUF_LEN];
  110. if (!ubuf || !ppos)
  111. return 0;
  112. for (i = (((int) *ppos/BYTES_PER_LINE) + SWR_SLV_START_REG_ADDR);
  113. i <= SWR_SLV_MAX_REG_ADDR; i++) {
  114. if (!is_swr_slave_reg_readable(i))
  115. continue;
  116. swr_read(pdev, pdev->dev_num, i, &reg_val, 1);
  117. len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i,
  118. (reg_val & 0xFF));
  119. if ((total + len) >= count - 1)
  120. break;
  121. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  122. pr_err("%s: fail to copy reg dump\n", __func__);
  123. total = -EFAULT;
  124. goto copy_err;
  125. }
  126. total += len;
  127. *ppos += len;
  128. }
  129. copy_err:
  130. *ppos = SWR_SLV_MAX_REG_ADDR * BYTES_PER_LINE;
  131. return total;
  132. }
  133. static ssize_t codec_debug_dump(struct file *file, char __user *ubuf,
  134. size_t count, loff_t *ppos)
  135. {
  136. struct swr_device *pdev;
  137. if (!count || !file || !ppos || !ubuf)
  138. return -EINVAL;
  139. pdev = file->private_data;
  140. if (!pdev)
  141. return -EINVAL;
  142. if (*ppos < 0)
  143. return -EINVAL;
  144. return swr_slave_reg_show(pdev, ubuf, count, ppos);
  145. }
  146. static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
  147. size_t count, loff_t *ppos)
  148. {
  149. char lbuf[SWR_SLV_RD_BUF_LEN];
  150. struct swr_device *pdev = NULL;
  151. struct wsa883x_priv *wsa883x = NULL;
  152. if (!count || !file || !ppos || !ubuf)
  153. return -EINVAL;
  154. pdev = file->private_data;
  155. if (!pdev)
  156. return -EINVAL;
  157. wsa883x = swr_get_dev_data(pdev);
  158. if (!wsa883x)
  159. return -EINVAL;
  160. if (*ppos < 0)
  161. return -EINVAL;
  162. snprintf(lbuf, sizeof(lbuf), "0x%x\n",
  163. (wsa883x->read_data & 0xFF));
  164. return simple_read_from_buffer(ubuf, count, ppos, lbuf,
  165. strnlen(lbuf, 7));
  166. }
  167. static ssize_t codec_debug_peek_write(struct file *file,
  168. const char __user *ubuf, size_t cnt, loff_t *ppos)
  169. {
  170. char lbuf[SWR_SLV_WR_BUF_LEN];
  171. int rc = 0;
  172. u32 param[5];
  173. struct swr_device *pdev = NULL;
  174. struct wsa883x_priv *wsa883x = NULL;
  175. if (!cnt || !file || !ppos || !ubuf)
  176. return -EINVAL;
  177. pdev = file->private_data;
  178. if (!pdev)
  179. return -EINVAL;
  180. wsa883x = swr_get_dev_data(pdev);
  181. if (!wsa883x)
  182. return -EINVAL;
  183. if (*ppos < 0)
  184. return -EINVAL;
  185. if (cnt > sizeof(lbuf) - 1)
  186. return -EINVAL;
  187. rc = copy_from_user(lbuf, ubuf, cnt);
  188. if (rc)
  189. return -EFAULT;
  190. lbuf[cnt] = '\0';
  191. rc = get_parameters(lbuf, param, 1);
  192. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) && (rc == 0)))
  193. return -EINVAL;
  194. swr_read(pdev, pdev->dev_num, param[0], &wsa883x->read_data, 1);
  195. if (rc == 0)
  196. rc = cnt;
  197. else
  198. pr_err("%s: rc = %d\n", __func__, rc);
  199. return rc;
  200. }
  201. static ssize_t codec_debug_write(struct file *file,
  202. const char __user *ubuf, size_t cnt, loff_t *ppos)
  203. {
  204. char lbuf[SWR_SLV_WR_BUF_LEN];
  205. int rc = 0;
  206. u32 param[5];
  207. struct swr_device *pdev;
  208. if (!file || !ppos || !ubuf)
  209. return -EINVAL;
  210. pdev = file->private_data;
  211. if (!pdev)
  212. return -EINVAL;
  213. if (cnt > sizeof(lbuf) - 1)
  214. return -EINVAL;
  215. rc = copy_from_user(lbuf, ubuf, cnt);
  216. if (rc)
  217. return -EFAULT;
  218. lbuf[cnt] = '\0';
  219. rc = get_parameters(lbuf, param, 2);
  220. if (!((param[0] <= SWR_SLV_MAX_REG_ADDR) &&
  221. (param[1] <= 0xFF) && (rc == 0)))
  222. return -EINVAL;
  223. swr_write(pdev, pdev->dev_num, param[0], &param[1]);
  224. if (rc == 0)
  225. rc = cnt;
  226. else
  227. pr_err("%s: rc = %d\n", __func__, rc);
  228. return rc;
  229. }
  230. static const struct file_operations codec_debug_write_ops = {
  231. .open = codec_debug_open,
  232. .write = codec_debug_write,
  233. };
  234. static const struct file_operations codec_debug_read_ops = {
  235. .open = codec_debug_open,
  236. .read = codec_debug_read,
  237. .write = codec_debug_peek_write,
  238. };
  239. static const struct file_operations codec_debug_dump_ops = {
  240. .open = codec_debug_open,
  241. .read = codec_debug_dump,
  242. };
  243. #endif
  244. static const char * const wsa_dev_mode_text[] = {
  245. "speaker", "receiver", "ultrasound"
  246. };
  247. static const struct soc_enum wsa_dev_mode_enum =
  248. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_dev_mode_text), wsa_dev_mode_text);
  249. static int wsa_dev_mode_get(struct snd_kcontrol *kcontrol,
  250. struct snd_ctl_elem_value *ucontrol)
  251. {
  252. struct snd_soc_component *component =
  253. snd_soc_kcontrol_component(kcontrol);
  254. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  255. ucontrol->value.integer.value[0] = wsa883x->dev_mode;
  256. dev_dbg(component->dev, "%s: mode = 0x%x\n", __func__,
  257. wsa883x->dev_mode);
  258. return 0;
  259. }
  260. static int wsa_dev_mode_put(struct snd_kcontrol *kcontrol,
  261. struct snd_ctl_elem_value *ucontrol)
  262. {
  263. struct snd_soc_component *component =
  264. snd_soc_kcontrol_component(kcontrol);
  265. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  266. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  267. __func__, ucontrol->value.integer.value[0]);
  268. wsa883x->dev_mode = ucontrol->value.integer.value[0];
  269. return 0;
  270. }
  271. static const char * const wsa_pa_gain_text[] = {
  272. "G_18_DB", "G_16P5_DB", "G_15_DB", "G_13P5_DB", "G_12_DB", "G_10P5_DB",
  273. "G_9_DB", "G_7P5_DB", "G_6_DB", "G_4P5_DB", "G_3_DB", "G_1P5_DB",
  274. "G_0_DB"
  275. };
  276. static const struct soc_enum wsa_pa_gain_enum =
  277. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa_pa_gain_text), wsa_pa_gain_text);
  278. static int wsa_pa_gain_get(struct snd_kcontrol *kcontrol,
  279. struct snd_ctl_elem_value *ucontrol)
  280. {
  281. struct snd_soc_component *component =
  282. snd_soc_kcontrol_component(kcontrol);
  283. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  284. ucontrol->value.integer.value[0] = wsa883x->pa_gain;
  285. dev_dbg(component->dev, "%s: PA gain = 0x%x\n", __func__,
  286. wsa883x->pa_gain);
  287. return 0;
  288. }
  289. static int wsa_pa_gain_put(struct snd_kcontrol *kcontrol,
  290. struct snd_ctl_elem_value *ucontrol)
  291. {
  292. struct snd_soc_component *component =
  293. snd_soc_kcontrol_component(kcontrol);
  294. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  295. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  296. __func__, ucontrol->value.integer.value[0]);
  297. wsa883x->pa_gain = ucontrol->value.integer.value[0];
  298. return 0;
  299. }
  300. static int wsa883x_get_mute(struct snd_kcontrol *kcontrol,
  301. struct snd_ctl_elem_value *ucontrol)
  302. {
  303. struct snd_soc_component *component =
  304. snd_soc_kcontrol_component(kcontrol);
  305. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  306. ucontrol->value.integer.value[0] = wsa883x->pa_mute;
  307. return 0;
  308. }
  309. static int wsa883x_set_mute(struct snd_kcontrol *kcontrol,
  310. struct snd_ctl_elem_value *ucontrol)
  311. {
  312. struct snd_soc_component *component =
  313. snd_soc_kcontrol_component(kcontrol);
  314. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  315. int value = ucontrol->value.integer.value[0];
  316. dev_dbg(component->dev, "%s: mute current %d, new %d\n",
  317. __func__, wsa883x->pa_mute, value);
  318. wsa883x->pa_mute = value;
  319. return 0;
  320. }
  321. static int wsa_get_temp(struct snd_kcontrol *kcontrol,
  322. struct snd_ctl_elem_value *ucontrol)
  323. {
  324. struct snd_soc_component *component =
  325. snd_soc_kcontrol_component(kcontrol);
  326. int temp = 0;
  327. wsa883x_get_temperature(component, &temp);
  328. ucontrol->value.integer.value[0] = temp;
  329. return 0;
  330. }
  331. static ssize_t wsa883x_codec_version_read(struct snd_info_entry *entry,
  332. void *file_private_data, struct file *file,
  333. char __user *buf, size_t count, loff_t pos)
  334. {
  335. struct wsa883x_priv *wsa883x;
  336. char buffer[WSA883X_VERSION_ENTRY_SIZE];
  337. int len = 0;
  338. wsa883x = (struct wsa883x_priv *) entry->private_data;
  339. if (!wsa883x) {
  340. pr_err("%s: wsa883x priv is null\n", __func__);
  341. return -EINVAL;
  342. }
  343. len = snprintf(buffer, sizeof(buffer), "WSA883X-SOUNDWIRE_1_0\n");
  344. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  345. }
  346. static struct snd_info_entry_ops wsa883x_codec_info_ops = {
  347. .read = wsa883x_codec_version_read,
  348. };
  349. /*
  350. * wsa883x_codec_info_create_codec_entry - creates wsa883x module
  351. * @codec_root: The parent directory
  352. * @component: Codec instance
  353. *
  354. * Creates wsa883x module and version entry under the given
  355. * parent directory.
  356. *
  357. * Return: 0 on success or negative error code on failure.
  358. */
  359. int wsa883x_codec_info_create_codec_entry(struct snd_info_entry *codec_root,
  360. struct snd_soc_component *component)
  361. {
  362. struct snd_info_entry *version_entry;
  363. struct wsa883x_priv *wsa883x;
  364. struct snd_soc_card *card;
  365. char name[80];
  366. if (!codec_root || !component)
  367. return -EINVAL;
  368. wsa883x = snd_soc_component_get_drvdata(component);
  369. card = component->card;
  370. snprintf(name, sizeof(name), "%s.%x", "wsa883x",
  371. (u32)wsa883x->swr_slave->addr);
  372. wsa883x->entry = snd_info_create_subdir(codec_root->module,
  373. (const char *)name,
  374. codec_root);
  375. if (!wsa883x->entry) {
  376. dev_dbg(component->dev, "%s: failed to create wsa883x entry\n",
  377. __func__);
  378. return -ENOMEM;
  379. }
  380. version_entry = snd_info_create_card_entry(card->snd_card,
  381. "version",
  382. wsa883x->entry);
  383. if (!version_entry) {
  384. dev_dbg(component->dev, "%s: failed to create wsa883x version entry\n",
  385. __func__);
  386. return -ENOMEM;
  387. }
  388. version_entry->private_data = wsa883x;
  389. version_entry->size = WSA883X_VERSION_ENTRY_SIZE;
  390. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  391. version_entry->c.ops = &wsa883x_codec_info_ops;
  392. if (snd_info_register(version_entry) < 0) {
  393. snd_info_free_entry(version_entry);
  394. return -ENOMEM;
  395. }
  396. wsa883x->version_entry = version_entry;
  397. return 0;
  398. }
  399. EXPORT_SYMBOL(wsa883x_codec_info_create_codec_entry);
  400. static void wsa883x_regcache_sync(struct wsa883x_priv *wsa883x)
  401. {
  402. mutex_lock(&wsa883x->res_lock);
  403. regcache_mark_dirty(wsa883x->regmap);
  404. regcache_sync(wsa883x->regmap);
  405. mutex_unlock(&wsa883x->res_lock);
  406. }
  407. static int wsa883x_get_compander(struct snd_kcontrol *kcontrol,
  408. struct snd_ctl_elem_value *ucontrol)
  409. {
  410. struct snd_soc_component *component =
  411. snd_soc_kcontrol_component(kcontrol);
  412. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  413. ucontrol->value.integer.value[0] = wsa883x->comp_enable;
  414. return 0;
  415. }
  416. static int wsa883x_set_compander(struct snd_kcontrol *kcontrol,
  417. struct snd_ctl_elem_value *ucontrol)
  418. {
  419. struct snd_soc_component *component =
  420. snd_soc_kcontrol_component(kcontrol);
  421. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  422. int value = ucontrol->value.integer.value[0];
  423. dev_dbg(component->dev, "%s: Compander enable current %d, new %d\n",
  424. __func__, wsa883x->comp_enable, value);
  425. wsa883x->comp_enable = value;
  426. return 0;
  427. }
  428. static int wsa883x_get_visense(struct snd_kcontrol *kcontrol,
  429. struct snd_ctl_elem_value *ucontrol)
  430. {
  431. struct snd_soc_component *component =
  432. snd_soc_kcontrol_component(kcontrol);
  433. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  434. ucontrol->value.integer.value[0] = wsa883x->visense_enable;
  435. return 0;
  436. }
  437. static int wsa883x_set_visense(struct snd_kcontrol *kcontrol,
  438. struct snd_ctl_elem_value *ucontrol)
  439. {
  440. struct snd_soc_component *component =
  441. snd_soc_kcontrol_component(kcontrol);
  442. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  443. int value = ucontrol->value.integer.value[0];
  444. dev_dbg(component->dev, "%s: VIsense enable current %d, new %d\n",
  445. __func__, wsa883x->visense_enable, value);
  446. wsa883x->visense_enable = value;
  447. return 0;
  448. }
  449. static int wsa883x_get_ext_vdd_spk(struct snd_kcontrol *kcontrol,
  450. struct snd_ctl_elem_value *ucontrol)
  451. {
  452. struct snd_soc_component *component =
  453. snd_soc_kcontrol_component(kcontrol);
  454. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  455. ucontrol->value.integer.value[0] = wsa883x->ext_vdd_spk;
  456. return 0;
  457. }
  458. static int wsa883x_put_ext_vdd_spk(struct snd_kcontrol *kcontrol,
  459. struct snd_ctl_elem_value *ucontrol)
  460. {
  461. struct snd_soc_component *component =
  462. snd_soc_kcontrol_component(kcontrol);
  463. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  464. int value = ucontrol->value.integer.value[0];
  465. dev_dbg(component->dev, "%s: Ext VDD SPK enable current %d, new %d\n",
  466. __func__, wsa883x->ext_vdd_spk, value);
  467. wsa883x->ext_vdd_spk = value;
  468. return 0;
  469. }
  470. static const struct snd_kcontrol_new wsa883x_snd_controls[] = {
  471. SOC_ENUM_EXT("WSA PA Gain", wsa_pa_gain_enum,
  472. wsa_pa_gain_get, wsa_pa_gain_put),
  473. SOC_SINGLE_EXT("WSA PA Mute", SND_SOC_NOPM, 0, 1, 0,
  474. wsa883x_get_mute, wsa883x_set_mute),
  475. SOC_SINGLE_EXT("WSA Temp", SND_SOC_NOPM, 0, 1, 0,
  476. wsa_get_temp, NULL),
  477. SOC_ENUM_EXT("WSA MODE", wsa_dev_mode_enum,
  478. wsa_dev_mode_get, wsa_dev_mode_put),
  479. SOC_SINGLE_EXT("COMP Switch", SND_SOC_NOPM, 0, 1, 0,
  480. wsa883x_get_compander, wsa883x_set_compander),
  481. SOC_SINGLE_EXT("VISENSE Switch", SND_SOC_NOPM, 0, 1, 0,
  482. wsa883x_get_visense, wsa883x_set_visense),
  483. SOC_SINGLE_EXT("External VDD_SPK", SND_SOC_NOPM, 0, 1, 0,
  484. wsa883x_get_ext_vdd_spk, wsa883x_put_ext_vdd_spk),
  485. };
  486. static const struct snd_kcontrol_new swr_dac_port[] = {
  487. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  488. };
  489. static int wsa883x_set_port(struct snd_soc_component *component, int port_idx,
  490. u8 *port_id, u8 *num_ch, u8 *ch_mask, u32 *ch_rate,
  491. u8 *port_type)
  492. {
  493. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  494. *port_id = wsa883x->port[port_idx].port_id;
  495. *num_ch = wsa883x->port[port_idx].num_ch;
  496. *ch_mask = wsa883x->port[port_idx].ch_mask;
  497. *ch_rate = wsa883x->port[port_idx].ch_rate;
  498. *port_type = wsa883x->port[port_idx].port_type;
  499. return 0;
  500. }
  501. static int wsa883x_enable_swr_dac_port(struct snd_soc_dapm_widget *w,
  502. struct snd_kcontrol *kcontrol, int event)
  503. {
  504. struct snd_soc_component *component =
  505. snd_soc_dapm_to_component(w->dapm);
  506. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  507. u8 port_id[WSA883X_MAX_SWR_PORTS];
  508. u8 num_ch[WSA883X_MAX_SWR_PORTS];
  509. u8 ch_mask[WSA883X_MAX_SWR_PORTS];
  510. u32 ch_rate[WSA883X_MAX_SWR_PORTS];
  511. u8 port_type[WSA883X_MAX_SWR_PORTS];
  512. u8 num_port = 0;
  513. dev_dbg(component->dev, "%s: event %d name %s\n", __func__,
  514. event, w->name);
  515. if (wsa883x == NULL)
  516. return -EINVAL;
  517. switch (event) {
  518. case SND_SOC_DAPM_PRE_PMU:
  519. wsa883x_set_port(component, SWR_DAC_PORT,
  520. &port_id[num_port], &num_ch[num_port],
  521. &ch_mask[num_port], &ch_rate[num_port],
  522. &port_type[num_port]);
  523. ++num_port;
  524. if (wsa883x->comp_enable) {
  525. wsa883x_set_port(component, SWR_COMP_PORT,
  526. &port_id[num_port], &num_ch[num_port],
  527. &ch_mask[num_port], &ch_rate[num_port],
  528. &port_type[num_port]);
  529. ++num_port;
  530. }
  531. if (wsa883x->visense_enable) {
  532. wsa883x_set_port(component, SWR_VISENSE_PORT,
  533. &port_id[num_port], &num_ch[num_port],
  534. &ch_mask[num_port], &ch_rate[num_port],
  535. &port_type[num_port]);
  536. ++num_port;
  537. }
  538. swr_connect_port(wsa883x->swr_slave, &port_id[0], num_port,
  539. &ch_mask[0], &ch_rate[0], &num_ch[0],
  540. &port_type[0]);
  541. break;
  542. case SND_SOC_DAPM_POST_PMU:
  543. swr_slvdev_datapath_control(wsa883x->swr_slave,
  544. wsa883x->swr_slave->dev_num,
  545. true);
  546. break;
  547. case SND_SOC_DAPM_PRE_PMD:
  548. swr_slvdev_datapath_control(wsa883x->swr_slave,
  549. wsa883x->swr_slave->dev_num,
  550. false);
  551. break;
  552. case SND_SOC_DAPM_POST_PMD:
  553. wsa883x_set_port(component, SWR_DAC_PORT,
  554. &port_id[num_port], &num_ch[num_port],
  555. &ch_mask[num_port], &ch_rate[num_port],
  556. &port_type[num_port]);
  557. ++num_port;
  558. if (wsa883x->comp_enable) {
  559. wsa883x_set_port(component, SWR_COMP_PORT,
  560. &port_id[num_port], &num_ch[num_port],
  561. &ch_mask[num_port], &ch_rate[num_port],
  562. &port_type[num_port]);
  563. ++num_port;
  564. }
  565. if (wsa883x->visense_enable) {
  566. wsa883x_set_port(component, SWR_VISENSE_PORT,
  567. &port_id[num_port], &num_ch[num_port],
  568. &ch_mask[num_port], &ch_rate[num_port],
  569. &port_type[num_port]);
  570. ++num_port;
  571. }
  572. swr_disconnect_port(wsa883x->swr_slave, &port_id[0], num_port,
  573. &ch_mask[0], &port_type[0]);
  574. break;
  575. default:
  576. break;
  577. }
  578. return 0;
  579. }
  580. static int wsa883x_spkr_event(struct snd_soc_dapm_widget *w,
  581. struct snd_kcontrol *kcontrol, int event)
  582. {
  583. struct snd_soc_component *component =
  584. snd_soc_dapm_to_component(w->dapm);
  585. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  586. int min_gain, max_gain;
  587. dev_dbg(component->dev, "%s: %s %d\n", __func__, w->name, event);
  588. switch (event) {
  589. case SND_SOC_DAPM_PRE_PMU:
  590. /* TODO Vote for Global PA */
  591. break;
  592. case SND_SOC_DAPM_POST_PMU:
  593. /* Force remove group */
  594. swr_remove_from_group(wsa883x->swr_slave,
  595. wsa883x->swr_slave->dev_num);
  596. break;
  597. case SND_SOC_DAPM_POST_PMD:
  598. /* TODO Unvote for Global PA */
  599. break;
  600. }
  601. return 0;
  602. }
  603. static const struct snd_soc_dapm_widget wsa883x_dapm_widgets[] = {
  604. SND_SOC_DAPM_INPUT("IN"),
  605. SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0, swr_dac_port,
  606. ARRAY_SIZE(swr_dac_port), wsa883x_enable_swr_dac_port,
  607. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  608. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  609. SND_SOC_DAPM_SPK("SPKR", wsa883x_spkr_event),
  610. };
  611. static const struct snd_soc_dapm_route wsa883x_audio_map[] = {
  612. {"SWR DAC_Port", "Switch", "IN"},
  613. {"SPKR", NULL, "SWR DAC_Port"},
  614. };
  615. int wsa883x_set_channel_map(struct snd_soc_component *component, u8 *port,
  616. u8 num_port, unsigned int *ch_mask,
  617. unsigned int *ch_rate, u8 *port_type)
  618. {
  619. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  620. int i;
  621. if (!port || !ch_mask || !ch_rate ||
  622. (num_port > WSA883X_MAX_SWR_PORTS)) {
  623. dev_err(component->dev,
  624. "%s: Invalid port=%pK, ch_mask=%pK, ch_rate=%pK\n",
  625. __func__, port, ch_mask, ch_rate);
  626. return -EINVAL;
  627. }
  628. for (i = 0; i < num_port; i++) {
  629. wsa883x->port[i].port_id = port[i];
  630. wsa883x->port[i].ch_mask = ch_mask[i];
  631. wsa883x->port[i].ch_rate = ch_rate[i];
  632. wsa883x->port[i].num_ch = __sw_hweight8(ch_mask[i]);
  633. if (port_type)
  634. wsa883x->port[i].port_type = port_type[i];
  635. }
  636. return 0;
  637. }
  638. EXPORT_SYMBOL(wsa883x_set_channel_map);
  639. static void wsa883x_codec_init(struct snd_soc_component *component)
  640. {
  641. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  642. if (!wsa883x)
  643. return;
  644. }
  645. static int32_t wsa883x_temp_reg_read(struct snd_soc_component *component,
  646. struct wsa_temp_register *wsa_temp_reg)
  647. {
  648. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  649. if (!wsa883x) {
  650. dev_err(component->dev, "%s: wsa883x is NULL\n", __func__);
  651. return -EINVAL;
  652. }
  653. mutex_lock(&wsa883x->res_lock);
  654. /* TODO Vote for global PA */
  655. snd_soc_component_update_bits(component, WSA883X_TADC_VALUE_CTL,
  656. 0x01, 0x00);
  657. wsa_temp_reg->dmeas_msb = snd_soc_component_read32(
  658. component, WSA883X_TEMP_MSB);
  659. wsa_temp_reg->dmeas_lsb = snd_soc_component_read32(
  660. component, WSA883X_TEMP_LSB);
  661. snd_soc_component_update_bits(component, WSA883X_TADC_VALUE_CTL,
  662. 0x01, 0x01);
  663. wsa_temp_reg->d1_msb = snd_soc_component_read32(
  664. component, WSA883X_OTP_REG_1);
  665. wsa_temp_reg->d1_lsb = snd_soc_component_read32(
  666. component, WSA883X_OTP_REG_2);
  667. wsa_temp_reg->d2_msb = snd_soc_component_read32(
  668. component, WSA883X_OTP_REG_3);
  669. wsa_temp_reg->d2_lsb = snd_soc_component_read32(
  670. component, WSA883X_OTP_REG_4);
  671. /* TODO Unvote for global PA */
  672. mutex_unlock(&wsa883x->res_lock);
  673. return 0;
  674. }
  675. static int wsa883x_get_temperature(struct snd_soc_component *component,
  676. int *temp)
  677. {
  678. struct snd_soc_component *component;
  679. struct wsa_temp_register reg;
  680. int dmeas, d1, d2;
  681. int ret = 0;
  682. int temp_val = 0;
  683. int t1 = T1_TEMP;
  684. int t2 = T2_TEMP;
  685. u8 retry = WSA883X_TEMP_RETRY;
  686. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  687. if (!wsa883x)
  688. return -EINVAL;
  689. do {
  690. ret = wsa883x_temp_reg_read(component, &reg)
  691. if (ret) {
  692. pr_err("%s: temp read failed: %d, current temp: %d\n",
  693. __func__, ret, wsa883x->curr_temp);
  694. if (temp)
  695. *temp = wsa883x->curr_temp;
  696. return 0;
  697. }
  698. /*
  699. * Temperature register values are expected to be in the
  700. * following range.
  701. * d1_msb = 68 - 92 and d1_lsb = 0, 64, 128, 192
  702. * d2_msb = 185 -218 and d2_lsb = 0, 64, 128, 192
  703. */
  704. if ((reg.d1_msb < 68 || reg.d1_msb > 92) ||
  705. (!(reg.d1_lsb == 0 || reg.d1_lsb == 64 || reg.d1_lsb == 128 ||
  706. reg.d1_lsb == 192)) ||
  707. (reg.d2_msb < 185 || reg.d2_msb > 218) ||
  708. (!(reg.d2_lsb == 0 || reg.d2_lsb == 64 || reg.d2_lsb == 128 ||
  709. reg.d2_lsb == 192))) {
  710. printk_ratelimited("%s: Temperature registers[%d %d %d %d] are out of range\n",
  711. __func__, reg.d1_msb, reg.d1_lsb, reg.d2_msb,
  712. reg.d2_lsb);
  713. }
  714. dmeas = ((reg.dmeas_msb << 0x8) | reg.dmeas_lsb) >> 0x6;
  715. d1 = ((reg.d1_msb << 0x8) | reg.d1_lsb) >> 0x6;
  716. d2 = ((reg.d2_msb << 0x8) | reg.d2_lsb) >> 0x6;
  717. if (d1 == d2)
  718. temp_val = TEMP_INVALID;
  719. else
  720. temp_val = t1 + (((dmeas - d1) * (t2 - t1))/(d2 - d1));
  721. if (temp_val <= LOW_TEMP_THRESHOLD ||
  722. temp_val >= HIGH_TEMP_THRESHOLD) {
  723. pr_debug("%s: T0: %d is out of range[%d, %d]\n", __func__,
  724. temp_val, LOW_TEMP_THRESHOLD, HIGH_TEMP_THRESHOLD);
  725. if (retry--)
  726. msleep(10);
  727. } else {
  728. break;
  729. }
  730. } while (retry);
  731. wsa883x->curr_temp = temp_val;
  732. if (temp)
  733. *temp = temp_val;
  734. pr_debug("%s: t0 measured: %d dmeas = %d, d1 = %d, d2 = %d\n",
  735. __func__, temp_val, dmeas, d1, d2);
  736. return ret;
  737. }
  738. static int wsa883x_codec_probe(struct snd_soc_component *component)
  739. {
  740. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  741. struct swr_device *dev;
  742. if (!wsa883x)
  743. return -EINVAL;
  744. snd_soc_component_init_regmap(component, wsa883x->regmap);
  745. dev = wsa883x->swr_slave;
  746. wsa883x->component = component;
  747. wsa883x_codec_init(component);
  748. wsa883x->global_pa_cnt = 0;
  749. return 0;
  750. }
  751. static void wsa883x_codec_remove(struct snd_soc_component *component)
  752. {
  753. struct wsa883x_priv *wsa883x = snd_soc_component_get_drvdata(component);
  754. if (!wsa883x)
  755. return;
  756. snd_soc_component_exit_regmap(component);
  757. return;
  758. }
  759. static const struct snd_soc_component_driver soc_codec_dev_wsa883x = {
  760. .name = DRV_NAME,
  761. .probe = wsa883x_codec_probe,
  762. .remove = wsa883x_codec_remove,
  763. .controls = wsa883x_snd_controls,
  764. .num_controls = ARRAY_SIZE(wsa883x_snd_controls),
  765. .dapm_widgets = wsa883x_dapm_widgets,
  766. .num_dapm_widgets = ARRAY_SIZE(wsa883x_dapm_widgets),
  767. .dapm_routes = wsa883x_audio_map,
  768. .num_dapm_routes = ARRAY_SIZE(wsa883x_audio_map),
  769. };
  770. static int wsa883x_gpio_ctrl(struct wsa883x_priv *wsa883x, bool enable)
  771. {
  772. int ret = 0;
  773. if (enable)
  774. ret = msm_cdc_pinctrl_select_active_state(
  775. wsa883x->wsa_rst_np);
  776. else
  777. ret = msm_cdc_pinctrl_select_sleep_state(
  778. wsa883x->wsa_rst_np);
  779. if (ret != 0)
  780. dev_err(wsa883x->dev,
  781. "%s: Failed to turn state %d; ret=%d\n",
  782. __func__, enable, ret);
  783. return ret;
  784. }
  785. static int wsa883x_swr_probe(struct swr_device *pdev)
  786. {
  787. int ret = 0;
  788. struct wsa883x_priv *wsa883x;
  789. u8 devnum = 0;
  790. bool pin_state_current = false;
  791. wsa883x = devm_kzalloc(&pdev->dev, sizeof(struct wsa883x_priv),
  792. GFP_KERNEL);
  793. if (!wsa883x)
  794. return -ENOMEM;
  795. wsa883x->wsa_rst_np = of_parse_phandle(pdev->dev.of_node,
  796. "qcom,spkr-sd-n-node", 0);
  797. if (!wsa883x->wsa_rst_np) {
  798. dev_dbg(&pdev->dev, "%s: pinctrl not defined\n", __func__);
  799. goto err;
  800. }
  801. swr_set_dev_data(pdev, wsa883x);
  802. wsa883x->swr_slave = pdev;
  803. pin_state_current = msm_cdc_pinctrl_get_state(wsa883x->wsa_rst_np);
  804. wsa883x_gpio_ctrl(wsa883x, true);
  805. /*
  806. * Add 5msec delay to provide sufficient time for
  807. * soundwire auto enumeration of slave devices as
  808. * as per HW requirement.
  809. */
  810. usleep_range(5000, 5010);
  811. ret = swr_get_logical_dev_num(pdev, pdev->addr, &devnum);
  812. if (ret) {
  813. dev_dbg(&pdev->dev,
  814. "%s get devnum %d for dev addr %lx failed\n",
  815. __func__, devnum, pdev->addr);
  816. goto dev_err;
  817. }
  818. pdev->dev_num = devnum;
  819. wsa883x->regmap = devm_regmap_init_swr(pdev,
  820. &wsa883x_regmap_config);
  821. if (IS_ERR(wsa883x->regmap)) {
  822. ret = PTR_ERR(wsa883x->regmap);
  823. dev_err(&pdev->dev, "%s: regmap_init failed %d\n",
  824. __func__, ret);
  825. goto dev_err;
  826. }
  827. ret = snd_soc_register_component(&pdev->dev, &soc_codec_dev_wsa883x,
  828. NULL, 0);
  829. if (ret) {
  830. dev_err(&pdev->dev, "%s: Codec registration failed\n",
  831. __func__);
  832. goto dev_err;
  833. }
  834. mutex_init(&wsa883x->res_lock);
  835. #ifdef CONFIG_DEBUG_FS
  836. if (!wcd938x->debugfs_dent) {
  837. wcd938x->debugfs_dent = debugfs_create_dir(
  838. dev_name(&pdev->dev), 0);
  839. if (!IS_ERR(wcd938x->debugfs_dent)) {
  840. wcd938x->debugfs_peek =
  841. debugfs_create_file("swrslave_peek",
  842. S_IFREG | 0444,
  843. wcd938x->debugfs_dent,
  844. (void *) pdev,
  845. &codec_debug_read_ops);
  846. wcd938x->debugfs_poke =
  847. debugfs_create_file("swrslave_poke",
  848. S_IFREG | 0444,
  849. wcd938x->debugfs_dent,
  850. (void *) pdev,
  851. &codec_debug_write_ops);
  852. wcd938x->debugfs_reg_dump =
  853. debugfs_create_file(
  854. "swrslave_reg_dump",
  855. S_IFREG | 0444,
  856. wcd938x->debugfs_dent,
  857. (void *) pdev,
  858. &codec_debug_dump_ops);
  859. }
  860. }
  861. #endif
  862. return 0;
  863. dev_err:
  864. if (pin_state_current == false)
  865. wsa883x_gpio_ctrl(wsa883x, false);
  866. swr_remove_device(pdev);
  867. err:
  868. return ret;
  869. }
  870. static int wsa883x_swr_remove(struct swr_device *pdev)
  871. {
  872. struct wsa883x_priv *wsa883x;
  873. wsa883x = swr_get_dev_data(pdev);
  874. if (!wsa883x) {
  875. dev_err(&pdev->dev, "%s: wsa883x is NULL\n", __func__);
  876. return -EINVAL;
  877. }
  878. #ifdef CONFIG_DEBUG_FS
  879. debugfs_remove_recursive(wsa883x->debugfs_dent);
  880. wsa883x->debugfs_dent = NULL;
  881. #endif
  882. mutex_destroy(&wsa883x->res_lock);
  883. snd_soc_unregister_component(&pdev->dev);
  884. swr_set_dev_data(pdev, NULL);
  885. return 0;
  886. }
  887. #ifdef CONFIG_PM_SLEEP
  888. static int wsa883x_swr_suspend(struct device *dev)
  889. {
  890. dev_dbg(dev, "%s: system suspend\n", __func__);
  891. return 0;
  892. }
  893. static int wsa883x_swr_resume(struct device *dev)
  894. {
  895. struct wsa883x_priv *wsa883x = swr_get_dev_data(to_swr_device(dev));
  896. if (!wsa883x) {
  897. dev_err(dev, "%s: wsa883x private data is NULL\n", __func__);
  898. return -EINVAL;
  899. }
  900. dev_dbg(dev, "%s: system resume\n", __func__);
  901. return 0;
  902. }
  903. #endif /* CONFIG_PM_SLEEP */
  904. static const struct dev_pm_ops wsa883x_swr_pm_ops = {
  905. SET_SYSTEM_SLEEP_PM_OPS(wsa883x_swr_suspend, wsa883x_swr_resume)
  906. };
  907. static const struct swr_device_id wsa883x_swr_id[] = {
  908. {"wsa883x", 0},
  909. {}
  910. };
  911. static const struct of_device_id wsa883x_swr_dt_match[] = {
  912. {
  913. .compatible = "qcom,wsa883x",
  914. },
  915. {}
  916. };
  917. static struct swr_driver wsa883x_swr_driver = {
  918. .driver = {
  919. .name = "wsa883x",
  920. .owner = THIS_MODULE,
  921. .pm = &wsa883x_swr_pm_ops,
  922. .of_match_table = wsa883x_swr_dt_match,
  923. },
  924. .probe = wsa883x_swr_probe,
  925. .remove = wsa883x_swr_remove,
  926. .id_table = wsa883x_swr_id,
  927. };
  928. static int __init wsa883x_swr_init(void)
  929. {
  930. return swr_driver_register(&wsa883x_swr_driver);
  931. }
  932. static void __exit wsa883x_swr_exit(void)
  933. {
  934. swr_driver_unregister(&wsa883x_swr_driver);
  935. }
  936. module_init(wsa883x_swr_init);
  937. module_exit(wsa883x_swr_exit);
  938. MODULE_DESCRIPTION("WSA883x codec driver");
  939. MODULE_LICENSE("GPL v2");