swr-haptics.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/regmap.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <linux/slab.h>
  12. #include <soc/soundwire.h>
  13. #include <sound/soc.h>
  14. #include <sound/soc-dapm.h>
  15. #define HAPTICS_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  16. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |\
  17. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000 |\
  18. SNDRV_PCM_RATE_384000)
  19. #define HAPTICS_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  20. SNDRV_PCM_FMTBIT_S24_LE |\
  21. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  22. /* SWR register definition */
  23. #define SWR_HAP_ACCESS_BASE 0x3000
  24. #define FIFO_WR_READY_REG (SWR_HAP_ACCESS_BASE + 0x8)
  25. #define NUM_PAT_SMPL_REG (SWR_HAP_ACCESS_BASE + 0x9)
  26. #define SWR_WR_ACCESS_REG (SWR_HAP_ACCESS_BASE + 0xa)
  27. #define CAL_TLRA_STATUS_MSB_REG (SWR_HAP_ACCESS_BASE + 0xb)
  28. #define CAL_TLRA_STATUS_LSB_REG (SWR_HAP_ACCESS_BASE + 0xc)
  29. #define AUTO_RES_CAL_DONE_REG (SWR_HAP_ACCESS_BASE + 0xd)
  30. #define SWR_READ_DATA_REG (SWR_HAP_ACCESS_BASE + 0x80)
  31. #define SWR_PLAY_REG (SWR_HAP_ACCESS_BASE + 0x81)
  32. #define SWR_VMAX_REG (SWR_HAP_ACCESS_BASE + 0x82)
  33. #define SWR_PLAY_BIT BIT(7)
  34. #define SWR_BRAKE_EN_BIT BIT(3)
  35. #define SWR_PLAY_SRC_MASK GENMASK(2, 0)
  36. #define SWR_PLAY_SRC_VAL_SWR 4
  37. #define SWR_HAP_REG_MAX (SWR_HAP_ACCESS_BASE + 0xff)
  38. static struct reg_default swr_hap_reg_defaults[] = {
  39. {FIFO_WR_READY_REG, 1},
  40. {NUM_PAT_SMPL_REG, 8},
  41. {SWR_WR_ACCESS_REG, 1},
  42. {CAL_TLRA_STATUS_MSB_REG, 0},
  43. {CAL_TLRA_STATUS_LSB_REG, 0},
  44. {AUTO_RES_CAL_DONE_REG, 0},
  45. {SWR_READ_DATA_REG, 0},
  46. {SWR_PLAY_REG, 4},
  47. {SWR_VMAX_REG, 0},
  48. };
  49. enum {
  50. PORT_ID_DT_IDX,
  51. NUM_CH_DT_IDX,
  52. CH_MASK_DT_IDX,
  53. CH_RATE_DT_IDX,
  54. PORT_TYPE_DT_IDX,
  55. NUM_SWR_PORT_DT_PARAMS,
  56. };
  57. struct swr_port {
  58. u8 port_id;
  59. u8 ch_mask;
  60. u32 ch_rate;
  61. u8 num_ch;
  62. u8 port_type;
  63. };
  64. struct swr_haptics_dev {
  65. struct device *dev;
  66. struct swr_device *swr_slave;
  67. struct snd_soc_component *component;
  68. struct regmap *regmap;
  69. struct swr_port port;
  70. struct regulator *vdd;
  71. };
  72. static bool swr_hap_volatile_register(struct device *dev, unsigned int reg)
  73. {
  74. switch (reg) {
  75. case SWR_READ_DATA_REG:
  76. case SWR_PLAY_REG:
  77. case SWR_VMAX_REG:
  78. return 1;
  79. default:
  80. return 0;
  81. }
  82. }
  83. static bool swr_hap_readable_register(struct device *dev, unsigned int reg)
  84. {
  85. if (reg <= SWR_HAP_ACCESS_BASE)
  86. return 0;
  87. return 1;
  88. }
  89. static bool swr_hap_writeable_register(struct device *dev, unsigned int reg)
  90. {
  91. if (reg <= SWR_HAP_ACCESS_BASE)
  92. return 0;
  93. switch (reg) {
  94. case FIFO_WR_READY_REG:
  95. case NUM_PAT_SMPL_REG:
  96. case SWR_WR_ACCESS_REG:
  97. case CAL_TLRA_STATUS_MSB_REG:
  98. case CAL_TLRA_STATUS_LSB_REG:
  99. case AUTO_RES_CAL_DONE_REG:
  100. case SWR_READ_DATA_REG:
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. struct regmap_config swr_hap_regmap_config = {
  106. .reg_bits = 16,
  107. .val_bits = 8,
  108. .cache_type = REGCACHE_RBTREE,
  109. .reg_defaults = swr_hap_reg_defaults,
  110. .num_reg_defaults = ARRAY_SIZE(swr_hap_reg_defaults),
  111. .max_register = SWR_HAP_REG_MAX,
  112. .volatile_reg = swr_hap_volatile_register,
  113. .readable_reg = swr_hap_readable_register,
  114. .writeable_reg = swr_hap_writeable_register,
  115. .reg_format_endian = REGMAP_ENDIAN_NATIVE,
  116. .val_format_endian = REGMAP_ENDIAN_NATIVE,
  117. .can_multi_write = true,
  118. };
  119. static const struct snd_kcontrol_new hap_swr_dac_port[] = {
  120. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  121. };
  122. static int hap_enable_swr_dac_port(struct snd_soc_dapm_widget *w,
  123. struct snd_kcontrol *kcontrol, int event)
  124. {
  125. struct snd_soc_component *swr_hap_comp =
  126. snd_soc_dapm_to_component(w->dapm);
  127. struct swr_haptics_dev *swr_hap;
  128. u8 port_id, ch_mask, num_ch, port_type, num_port;
  129. u32 ch_rate;
  130. unsigned int val;
  131. int rc;
  132. if (!swr_hap_comp) {
  133. pr_err("%s: swr_hap_component is NULL\n", __func__);
  134. return -EINVAL;
  135. }
  136. swr_hap = snd_soc_component_get_drvdata(swr_hap_comp);
  137. if (!swr_hap) {
  138. pr_err("%s: get swr_haptics_dev failed\n", __func__);
  139. return -ENODEV;
  140. }
  141. dev_dbg(swr_hap->dev, "%s: %s event %d\n", __func__, w->name, event);
  142. num_port = 1;
  143. port_id = swr_hap->port.port_id;
  144. ch_mask = swr_hap->port.ch_mask;
  145. ch_rate = swr_hap->port.ch_rate;
  146. num_ch = swr_hap->port.num_ch;
  147. port_type = swr_hap->port.port_type;
  148. switch (event) {
  149. case SND_SOC_DAPM_PRE_PMU:
  150. swr_device_wakeup_vote(swr_hap->swr_slave);
  151. swr_connect_port(swr_hap->swr_slave, &port_id, num_port,
  152. &ch_mask, &ch_rate, &num_ch, &port_type);
  153. break;
  154. case SND_SOC_DAPM_POST_PMU:
  155. swr_slvdev_datapath_control(swr_hap->swr_slave,
  156. swr_hap->swr_slave->dev_num, true);
  157. /* trigger SWR play */
  158. val = SWR_PLAY_BIT | SWR_PLAY_SRC_VAL_SWR;
  159. rc = regmap_write(swr_hap->regmap, SWR_PLAY_REG, val);
  160. if (rc) {
  161. dev_err(swr_hap->dev, "%s: Enable SWR_PLAY failed, rc=%d\n",
  162. __func__, rc);
  163. return rc;
  164. }
  165. break;
  166. case SND_SOC_DAPM_PRE_PMD:
  167. /* stop SWR play */
  168. val = 0;
  169. rc = regmap_write(swr_hap->regmap, SWR_PLAY_REG, val);
  170. if (rc) {
  171. dev_err(swr_hap->dev, "%s: Enable SWR_PLAY failed, rc=%d\n",
  172. __func__, rc);
  173. return rc;
  174. }
  175. break;
  176. case SND_SOC_DAPM_POST_PMD:
  177. swr_disconnect_port(swr_hap->swr_slave, &port_id, num_port,
  178. &ch_mask, &port_type);
  179. swr_slvdev_datapath_control(swr_hap->swr_slave,
  180. swr_hap->swr_slave->dev_num, false);
  181. swr_device_wakeup_unvote(swr_hap->swr_slave);
  182. break;
  183. default:
  184. break;
  185. }
  186. return 0;
  187. }
  188. static const struct snd_soc_dapm_widget haptics_comp_dapm_widgets[] = {
  189. SND_SOC_DAPM_INPUT("HAP_IN"),
  190. SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0,
  191. hap_swr_dac_port, ARRAY_SIZE(hap_swr_dac_port),
  192. hap_enable_swr_dac_port,
  193. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  194. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  195. SND_SOC_DAPM_SPK("HAP_OUT", NULL),
  196. };
  197. static const struct snd_soc_dapm_route haptics_comp_dapm_route[] = {
  198. {"SWR DAC_Port", "Switch", "HAP_IN"},
  199. {"HAP_OUT", NULL, "SWR DAC_Port"},
  200. };
  201. static int haptics_comp_probe(struct snd_soc_component *component)
  202. {
  203. struct snd_soc_dapm_context *dapm;
  204. struct swr_haptics_dev *swr_hap =
  205. snd_soc_component_get_drvdata(component);
  206. if (!swr_hap) {
  207. pr_err("%s: get swr_haptics_dev failed\n", __func__);
  208. return -EINVAL;
  209. }
  210. snd_soc_component_init_regmap(component, swr_hap->regmap);
  211. dapm = snd_soc_component_get_dapm(component);
  212. if (dapm && dapm->component) {
  213. snd_soc_dapm_ignore_suspend(dapm, "HAP_IN");
  214. snd_soc_dapm_ignore_suspend(dapm, "HAP_OUT");
  215. }
  216. return 0;
  217. }
  218. static void haptics_comp_remove(struct snd_soc_component *component)
  219. {
  220. }
  221. static const struct snd_soc_component_driver swr_haptics_component = {
  222. .name = "swr-haptics",
  223. .probe = haptics_comp_probe,
  224. .remove = haptics_comp_remove,
  225. .dapm_widgets = haptics_comp_dapm_widgets,
  226. .num_dapm_widgets = ARRAY_SIZE(haptics_comp_dapm_widgets),
  227. .dapm_routes = haptics_comp_dapm_route,
  228. .num_dapm_routes = ARRAY_SIZE(haptics_comp_dapm_route),
  229. };
  230. static struct snd_soc_dai_driver haptics_dai[] = {
  231. {
  232. .name = "swr_haptics",
  233. .playback = {
  234. .stream_name = "HAPTICS_AIF Playback",
  235. .rates = HAPTICS_RATES,
  236. .formats = HAPTICS_FORMATS,
  237. .rate_max = 192000,
  238. .rate_min = 8000,
  239. .channels_min = 1,
  240. .channels_max = 1,
  241. },
  242. },
  243. };
  244. static int swr_haptics_parse_port_mapping(struct swr_device *sdev)
  245. {
  246. struct swr_haptics_dev *swr_hap = swr_get_dev_data(sdev);
  247. u32 port_cfg[NUM_SWR_PORT_DT_PARAMS];
  248. int rc;
  249. if (!swr_hap) {
  250. dev_err(&sdev->dev, "%s: get swr_haptics_dev failed\n",
  251. __func__);
  252. return -EINVAL;
  253. }
  254. rc = of_property_read_u32_array(sdev->dev.of_node, "qcom,rx_swr_ch_map",
  255. port_cfg, NUM_SWR_PORT_DT_PARAMS);
  256. if (rc < 0) {
  257. dev_err(swr_hap->dev, "%s: Get qcom,rx_swr_ch_map failed, rc=%d\n",
  258. __func__, rc);
  259. return -EINVAL;
  260. }
  261. swr_hap->port.port_id = (u8) port_cfg[PORT_ID_DT_IDX];
  262. swr_hap->port.num_ch = (u8) port_cfg[NUM_CH_DT_IDX];
  263. swr_hap->port.ch_mask = (u8) port_cfg[CH_MASK_DT_IDX];
  264. swr_hap->port.ch_rate = port_cfg[CH_RATE_DT_IDX];
  265. swr_hap->port.port_type = (u8) port_cfg[PORT_TYPE_DT_IDX];
  266. dev_dbg(swr_hap->dev, "%s: port_id = %d, ch_mask = %d, ch_rate = %d, num_ch = %d, port_type = %d\n",
  267. __func__, swr_hap->port.port_id,
  268. swr_hap->port.ch_mask, swr_hap->port.ch_rate,
  269. swr_hap->port.num_ch, swr_hap->port.port_type);
  270. return 0;
  271. }
  272. static int swr_haptics_probe(struct swr_device *sdev)
  273. {
  274. struct swr_haptics_dev *swr_hap;
  275. int rc;
  276. u8 devnum;
  277. swr_hap = devm_kzalloc(&sdev->dev,
  278. sizeof(struct swr_haptics_dev), GFP_KERNEL);
  279. if (!swr_hap)
  280. return -ENOMEM;
  281. swr_hap->swr_slave = sdev;
  282. swr_hap->dev = &sdev->dev;
  283. swr_set_dev_data(sdev, swr_hap);
  284. rc = swr_haptics_parse_port_mapping(sdev);
  285. if (rc < 0) {
  286. dev_err(swr_hap->dev, "%s: failed to parse swr port mapping, rc=%d\n",
  287. __func__, rc);
  288. goto clean;
  289. }
  290. swr_hap->vdd = devm_regulator_get(swr_hap->dev, "swr-slave");
  291. if (IS_ERR(swr_hap->vdd)) {
  292. rc = PTR_ERR(swr_hap->vdd);
  293. if (rc != -EPROBE_DEFER)
  294. dev_err(swr_hap->dev, "%s: get swr-slave-supply failed, rc=%d\n",
  295. __func__, rc);
  296. goto clean;
  297. }
  298. rc = regulator_enable(swr_hap->vdd);
  299. if (rc < 0) {
  300. dev_err(swr_hap->dev, "%s: enable swr-slave-vdd failed, rc=%d\n",
  301. __func__, rc);
  302. goto clean;
  303. }
  304. rc = swr_get_logical_dev_num(sdev, sdev->addr, &devnum);
  305. if (rc) {
  306. dev_err(swr_hap->dev, "%s: failed to get devnum for swr-haptics, rc=%d\n",
  307. __func__, rc);
  308. rc = -EPROBE_DEFER;
  309. goto clean;
  310. }
  311. sdev->dev_num = devnum;
  312. swr_hap->regmap = devm_regmap_init_swr(sdev, &swr_hap_regmap_config);
  313. if (IS_ERR(swr_hap->regmap)) {
  314. rc = PTR_ERR(swr_hap->regmap);
  315. dev_err(swr_hap->dev, "%s: init regmap failed, rc=%d\n",
  316. __func__, rc);
  317. goto dev_err;
  318. }
  319. rc = snd_soc_register_component(&sdev->dev,
  320. &swr_haptics_component, haptics_dai, ARRAY_SIZE(haptics_dai));
  321. if (rc) {
  322. dev_err(swr_hap->dev, "%s: register swr_haptics component failed, rc=%d\n",
  323. __func__, rc);
  324. goto dev_err;
  325. }
  326. return 0;
  327. dev_err:
  328. regulator_disable(swr_hap->vdd);
  329. swr_remove_device(sdev);
  330. clean:
  331. swr_set_dev_data(sdev, NULL);
  332. return rc;
  333. }
  334. static int swr_haptics_remove(struct swr_device *sdev)
  335. {
  336. struct swr_haptics_dev *swr_hap;
  337. int rc = 0;
  338. swr_hap = swr_get_dev_data(sdev);
  339. if (!swr_hap) {
  340. dev_err(&sdev->dev, "%s: no data for swr_hap\n", __func__);
  341. rc = -ENODEV;
  342. goto clean;
  343. }
  344. rc = regulator_disable(swr_hap->vdd);
  345. if (rc < 0) {
  346. dev_err(swr_hap->dev, "%s: disable swr-slave failed, rc=%d\n",
  347. __func__, rc);
  348. goto clean;
  349. }
  350. clean:
  351. snd_soc_unregister_component(&sdev->dev);
  352. swr_set_dev_data(sdev, NULL);
  353. return rc;
  354. }
  355. static int swr_haptics_device_up(struct swr_device *sdev)
  356. {
  357. struct swr_haptics_dev *swr_hap;
  358. int rc;
  359. swr_hap = swr_get_dev_data(sdev);
  360. if (!swr_hap) {
  361. dev_err(&sdev->dev, "%s: no data for swr_hap\n", __func__);
  362. return -ENODEV;
  363. }
  364. /* Take SWR slave out of reset */
  365. rc = regulator_enable(swr_hap->vdd);
  366. if (rc < 0) {
  367. dev_err(swr_hap->dev, "%s: enable swr-slave failed, rc=%d\n",
  368. __func__, rc);
  369. return rc;
  370. }
  371. return 0;
  372. }
  373. static int swr_haptics_device_down(struct swr_device *sdev)
  374. {
  375. struct swr_haptics_dev *swr_hap = swr_get_dev_data(sdev);
  376. int rc;
  377. if (!swr_hap) {
  378. dev_err(&sdev->dev, "%s: no data for swr_hap\n", __func__);
  379. return -ENODEV;
  380. }
  381. /* Put SWR slave into reset */
  382. rc = regulator_disable(swr_hap->vdd);
  383. if (rc < 0) {
  384. dev_err(swr_hap->dev, "%s: disable swr-slave failed, rc=%d\n",
  385. __func__, rc);
  386. return rc;
  387. }
  388. return 0;
  389. }
  390. static int swr_haptics_suspend(struct device *dev)
  391. {
  392. struct swr_haptics_dev *swr_hap;
  393. int rc;
  394. swr_hap = swr_get_dev_data(to_swr_device(dev));
  395. if (!swr_hap) {
  396. dev_err(dev, "%s: no data for swr_hap\n", __func__);
  397. return -ENODEV;
  398. }
  399. /* Put SWR slave into reset */
  400. rc = regulator_disable(swr_hap->vdd);
  401. if (rc < 0) {
  402. dev_err(swr_hap->dev, "%s: disable swr-slave failed, rc=%d\n",
  403. __func__, rc);
  404. return rc;
  405. }
  406. return 0;
  407. }
  408. static int swr_haptics_resume(struct device *dev)
  409. {
  410. struct swr_haptics_dev *swr_hap;
  411. int rc;
  412. swr_hap = swr_get_dev_data(to_swr_device(dev));
  413. if (!swr_hap) {
  414. dev_err(dev, "%s: no data for swr_hap\n", __func__);
  415. return -ENODEV;
  416. }
  417. /* Take SWR slave out of reset */
  418. rc = regulator_enable(swr_hap->vdd);
  419. if (rc < 0) {
  420. dev_err(swr_hap->dev, "%s: enable swr-slave failed, rc=%d\n",
  421. __func__, rc);
  422. return rc;
  423. }
  424. return 0;
  425. }
  426. static const struct of_device_id swr_haptics_match_table[] = {
  427. { .compatible = "qcom,swr-haptics", },
  428. { .compatible = "qcom,pm8350b-swr-haptics", },
  429. { },
  430. };
  431. static const struct swr_device_id swr_haptics_id[] = {
  432. {"swr-haptics", 0},
  433. {"pm8350b-swr-haptics", 0},
  434. {},
  435. };
  436. static const struct dev_pm_ops swr_haptics_pm_ops = {
  437. .suspend = swr_haptics_suspend,
  438. .resume = swr_haptics_resume,
  439. };
  440. static struct swr_driver swr_haptics_driver = {
  441. .driver = {
  442. .name = "swr-haptics",
  443. .owner = THIS_MODULE,
  444. .pm = &swr_haptics_pm_ops,
  445. .of_match_table = swr_haptics_match_table,
  446. },
  447. .probe = swr_haptics_probe,
  448. .remove = swr_haptics_remove,
  449. .id_table = swr_haptics_id,
  450. .device_up = swr_haptics_device_up,
  451. .device_down = swr_haptics_device_down,
  452. };
  453. static int __init swr_haptics_init(void)
  454. {
  455. return swr_driver_register(&swr_haptics_driver);
  456. }
  457. static void __exit swr_haptics_exit(void)
  458. {
  459. swr_driver_unregister(&swr_haptics_driver);
  460. }
  461. module_init(swr_haptics_init);
  462. module_exit(swr_haptics_exit);
  463. MODULE_DESCRIPTION("SWR haptics driver");
  464. MODULE_LICENSE("GPL v2");