swr-haptics.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/device.h>
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/regmap.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <linux/slab.h>
  14. #include <soc/soundwire.h>
  15. #include <sound/soc.h>
  16. #include <sound/soc-dapm.h>
  17. #include <linux/soc/qcom/battery_charger.h>
  18. #define HAPTICS_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  19. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |\
  20. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000 |\
  21. SNDRV_PCM_RATE_384000)
  22. #define HAPTICS_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  23. SNDRV_PCM_FMTBIT_S24_LE |\
  24. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  25. /* SWR register definition */
  26. #define SWR_HAP_ACCESS_BASE 0x3000
  27. #define FIFO_WR_READY_REG (SWR_HAP_ACCESS_BASE + 0x8)
  28. #define NUM_PAT_SMPL_REG (SWR_HAP_ACCESS_BASE + 0x9)
  29. #define SWR_WR_ACCESS_REG (SWR_HAP_ACCESS_BASE + 0xa)
  30. #define CAL_TLRA_STATUS_MSB_REG (SWR_HAP_ACCESS_BASE + 0xb)
  31. #define CAL_TLRA_STATUS_LSB_REG (SWR_HAP_ACCESS_BASE + 0xc)
  32. #define AUTO_RES_CAL_DONE_REG (SWR_HAP_ACCESS_BASE + 0xd)
  33. #define SWR_READ_DATA_REG (SWR_HAP_ACCESS_BASE + 0x80)
  34. #define SWR_PLAY_REG (SWR_HAP_ACCESS_BASE + 0x81)
  35. #define SWR_VMAX_REG (SWR_HAP_ACCESS_BASE + 0x82)
  36. #define SWR_PLAY_BIT BIT(7)
  37. #define SWR_BRAKE_EN_BIT BIT(3)
  38. #define SWR_PLAY_SRC_MASK GENMASK(2, 0)
  39. #define SWR_PLAY_SRC_VAL_SWR 4
  40. #define SWR_HAP_REG_MAX (SWR_HAP_ACCESS_BASE + 0xff)
  41. enum pmic_type {
  42. PM8350B = 1,
  43. PM8550B = 2,
  44. };
  45. enum {
  46. HAP_SSR_RECOVERY = BIT(0),
  47. };
  48. static struct reg_default swr_hap_reg_defaults[] = {
  49. {FIFO_WR_READY_REG, 1},
  50. {NUM_PAT_SMPL_REG, 8},
  51. {SWR_WR_ACCESS_REG, 1},
  52. {CAL_TLRA_STATUS_MSB_REG, 0},
  53. {CAL_TLRA_STATUS_LSB_REG, 0},
  54. {AUTO_RES_CAL_DONE_REG, 0},
  55. {SWR_READ_DATA_REG, 0},
  56. {SWR_PLAY_REG, 4},
  57. {SWR_VMAX_REG, 0},
  58. };
  59. enum {
  60. PORT_ID_DT_IDX,
  61. NUM_CH_DT_IDX,
  62. CH_MASK_DT_IDX,
  63. CH_RATE_DT_IDX,
  64. PORT_TYPE_DT_IDX,
  65. NUM_SWR_PORT_DT_PARAMS,
  66. };
  67. struct swr_port {
  68. u8 port_id;
  69. u8 ch_mask;
  70. u32 ch_rate;
  71. u8 num_ch;
  72. u8 port_type;
  73. };
  74. struct swr_haptics_dev {
  75. struct device *dev;
  76. struct swr_device *swr_slave;
  77. struct snd_soc_component *component;
  78. struct regmap *regmap;
  79. struct swr_port port;
  80. struct regulator *slave_vdd;
  81. struct regulator *hpwr_vreg;
  82. struct notifier_block hboost_nb;
  83. u32 hpwr_voltage_mv;
  84. bool slave_enabled;
  85. bool hpwr_vreg_enabled;
  86. bool ssr_recovery;
  87. u8 vmax;
  88. u8 clamped_vmax;
  89. u8 flags;
  90. };
  91. static bool swr_hap_volatile_register(struct device *dev, unsigned int reg)
  92. {
  93. switch (reg) {
  94. case SWR_READ_DATA_REG:
  95. case SWR_PLAY_REG:
  96. case SWR_VMAX_REG:
  97. return 1;
  98. default:
  99. return 0;
  100. }
  101. }
  102. static bool swr_hap_readable_register(struct device *dev, unsigned int reg)
  103. {
  104. if (reg <= SWR_HAP_ACCESS_BASE)
  105. return 0;
  106. return 1;
  107. }
  108. static bool swr_hap_writeable_register(struct device *dev, unsigned int reg)
  109. {
  110. if (reg <= SWR_HAP_ACCESS_BASE)
  111. return 0;
  112. switch (reg) {
  113. case FIFO_WR_READY_REG:
  114. case NUM_PAT_SMPL_REG:
  115. case SWR_WR_ACCESS_REG:
  116. case CAL_TLRA_STATUS_MSB_REG:
  117. case CAL_TLRA_STATUS_LSB_REG:
  118. case AUTO_RES_CAL_DONE_REG:
  119. case SWR_READ_DATA_REG:
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. static int swr_hap_enable_hpwr_vreg(struct swr_haptics_dev *swr_hap)
  125. {
  126. int rc;
  127. if (swr_hap->hpwr_vreg == NULL || swr_hap->hpwr_vreg_enabled)
  128. return 0;
  129. rc = regulator_set_voltage(swr_hap->hpwr_vreg,
  130. swr_hap->hpwr_voltage_mv * 1000, INT_MAX);
  131. if (rc < 0) {
  132. dev_err_ratelimited(swr_hap->dev, "%s: Set hpwr voltage failed, rc=%d\n",
  133. __func__, rc);
  134. return rc;
  135. }
  136. rc = regulator_enable(swr_hap->hpwr_vreg);
  137. if (rc < 0) {
  138. dev_err_ratelimited(swr_hap->dev, "%s: Enable hpwr failed, rc=%d\n",
  139. __func__, rc);
  140. regulator_set_voltage(swr_hap->hpwr_vreg, 0, INT_MAX);
  141. return rc;
  142. }
  143. dev_dbg(swr_hap->dev, "%s: enabled hpwr_regulator\n", __func__);
  144. swr_hap->hpwr_vreg_enabled = true;
  145. return 0;
  146. }
  147. static int swr_hap_disable_hpwr_vreg(struct swr_haptics_dev *swr_hap)
  148. {
  149. int rc;
  150. if (swr_hap->hpwr_vreg == NULL || !swr_hap->hpwr_vreg_enabled)
  151. return 0;
  152. rc = regulator_disable(swr_hap->hpwr_vreg);
  153. if (rc < 0) {
  154. dev_err_ratelimited(swr_hap->dev, "%s: Disable hpwr failed, rc=%d\n",
  155. __func__, rc);
  156. return rc;
  157. }
  158. rc = regulator_set_voltage(swr_hap->hpwr_vreg, 0, INT_MAX);
  159. if (rc < 0) {
  160. dev_err_ratelimited(swr_hap->dev, "%s: Set hpwr voltage failed, rc=%d\n",
  161. __func__, rc);
  162. return rc;
  163. }
  164. dev_dbg(swr_hap->dev, "%s: disabled hpwr_regulator\n", __func__);
  165. swr_hap->hpwr_vreg_enabled = false;
  166. return 0;
  167. }
  168. static int swr_haptics_slave_enable(struct swr_haptics_dev *swr_hap)
  169. {
  170. int rc;
  171. if (swr_hap->slave_enabled)
  172. return 0;
  173. rc = regulator_enable(swr_hap->slave_vdd);
  174. if (rc < 0) {
  175. dev_err_ratelimited(swr_hap->dev, "%s: enable swr-slave-vdd failed, rc=%d\n",
  176. __func__, rc);
  177. return rc;
  178. }
  179. dev_dbg(swr_hap->dev, "%s: enable swr-slave-vdd success\n", __func__);
  180. swr_hap->slave_enabled = true;
  181. return 0;
  182. }
  183. static int swr_haptics_slave_disable(struct swr_haptics_dev *swr_hap)
  184. {
  185. int rc;
  186. if (!swr_hap->slave_enabled)
  187. return 0;
  188. rc = regulator_disable(swr_hap->slave_vdd);
  189. if (rc < 0) {
  190. dev_err_ratelimited(swr_hap->dev, "%s: disable swr-slave-vdd failed, rc=%d\n",
  191. __func__, rc);
  192. return rc;
  193. }
  194. dev_dbg(swr_hap->dev, "%s: disable swr-slave-vdd success\n", __func__);
  195. swr_hap->slave_enabled = false;
  196. return 0;
  197. }
  198. struct regmap_config swr_hap_regmap_config = {
  199. .reg_bits = 16,
  200. .val_bits = 8,
  201. .cache_type = REGCACHE_RBTREE,
  202. .reg_defaults = swr_hap_reg_defaults,
  203. .num_reg_defaults = ARRAY_SIZE(swr_hap_reg_defaults),
  204. .max_register = SWR_HAP_REG_MAX,
  205. .volatile_reg = swr_hap_volatile_register,
  206. .readable_reg = swr_hap_readable_register,
  207. .writeable_reg = swr_hap_writeable_register,
  208. .reg_format_endian = REGMAP_ENDIAN_NATIVE,
  209. .val_format_endian = REGMAP_ENDIAN_NATIVE,
  210. .can_multi_write = true,
  211. };
  212. static const struct snd_kcontrol_new hap_swr_dac_port[] = {
  213. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  214. };
  215. static int hap_enable_swr_dac_port(struct snd_soc_dapm_widget *w,
  216. struct snd_kcontrol *kcontrol, int event)
  217. {
  218. struct snd_soc_component *swr_hap_comp =
  219. snd_soc_dapm_to_component(w->dapm);
  220. struct swr_haptics_dev *swr_hap;
  221. u8 port_id, ch_mask, num_ch, port_type, num_port;
  222. u8 vmax;
  223. u32 ch_rate;
  224. unsigned int val;
  225. int rc;
  226. if (!swr_hap_comp) {
  227. pr_err("%s: swr_hap_component is NULL\n", __func__);
  228. return -EINVAL;
  229. }
  230. swr_hap = snd_soc_component_get_drvdata(swr_hap_comp);
  231. if (!swr_hap) {
  232. pr_err("%s: get swr_haptics_dev failed\n", __func__);
  233. return -ENODEV;
  234. }
  235. dev_dbg(swr_hap->dev, "%s: %s event %d\n", __func__, w->name, event);
  236. num_port = 1;
  237. port_id = swr_hap->port.port_id;
  238. ch_mask = swr_hap->port.ch_mask;
  239. ch_rate = swr_hap->port.ch_rate;
  240. num_ch = swr_hap->port.num_ch;
  241. port_type = swr_hap->port.port_type;
  242. switch (event) {
  243. case SND_SOC_DAPM_PRE_PMU:
  244. /* If SSR ever happened, toggle swr-slave-vdd for HW recovery */
  245. if ((swr_hap->flags & HAP_SSR_RECOVERY)
  246. && swr_hap->ssr_recovery) {
  247. swr_haptics_slave_disable(swr_hap);
  248. swr_haptics_slave_enable(swr_hap);
  249. swr_hap->ssr_recovery = false;
  250. }
  251. vmax = swr_hap->vmax;
  252. if ((swr_hap->clamped_vmax != 0) && (swr_hap->vmax > swr_hap->clamped_vmax))
  253. vmax = swr_hap->clamped_vmax;
  254. rc = regmap_write(swr_hap->regmap, SWR_VMAX_REG, vmax);
  255. if (rc) {
  256. dev_err_ratelimited(swr_hap->dev, "%s: SWR_VMAX update failed, rc=%d\n",
  257. __func__, rc);
  258. return rc;
  259. }
  260. regmap_read(swr_hap->regmap, SWR_VMAX_REG, &val);
  261. regmap_read(swr_hap->regmap, SWR_READ_DATA_REG, &val);
  262. dev_dbg(swr_hap->dev, "%s: swr_vmax is set to 0x%x\n", __func__, val);
  263. swr_device_wakeup_vote(swr_hap->swr_slave);
  264. swr_connect_port(swr_hap->swr_slave, &port_id, num_port,
  265. &ch_mask, &ch_rate, &num_ch, &port_type);
  266. break;
  267. case SND_SOC_DAPM_POST_PMU:
  268. rc = swr_hap_enable_hpwr_vreg(swr_hap);
  269. if (rc < 0) {
  270. dev_err_ratelimited(swr_hap->dev, "%s: Enable hpwr_vreg failed, rc=%d\n",
  271. __func__, rc);
  272. swr_device_wakeup_unvote(swr_hap->swr_slave);
  273. return rc;
  274. }
  275. swr_slvdev_datapath_control(swr_hap->swr_slave,
  276. swr_hap->swr_slave->dev_num, true);
  277. /* trigger SWR play */
  278. val = SWR_PLAY_BIT | SWR_PLAY_SRC_VAL_SWR;
  279. rc = regmap_write(swr_hap->regmap, SWR_PLAY_REG, val);
  280. if (rc) {
  281. dev_err_ratelimited(swr_hap->dev, "%s: Enable SWR_PLAY failed, rc=%d\n",
  282. __func__, rc);
  283. swr_slvdev_datapath_control(swr_hap->swr_slave,
  284. swr_hap->swr_slave->dev_num, false);
  285. swr_hap_disable_hpwr_vreg(swr_hap);
  286. swr_device_wakeup_unvote(swr_hap->swr_slave);
  287. return rc;
  288. }
  289. swr_device_wakeup_unvote(swr_hap->swr_slave);
  290. break;
  291. case SND_SOC_DAPM_PRE_PMD:
  292. swr_device_wakeup_vote(swr_hap->swr_slave);
  293. /* stop SWR play */
  294. val = SWR_PLAY_SRC_VAL_SWR;
  295. rc = regmap_write(swr_hap->regmap, SWR_PLAY_REG, val);
  296. if (rc) {
  297. dev_err_ratelimited(swr_hap->dev, "%s: Enable SWR_PLAY failed, rc=%d\n",
  298. __func__, rc);
  299. swr_device_wakeup_unvote(swr_hap->swr_slave);
  300. return rc;
  301. }
  302. rc = swr_hap_disable_hpwr_vreg(swr_hap);
  303. if (rc < 0) {
  304. dev_err_ratelimited(swr_hap->dev, "%s: Disable hpwr_vreg failed, rc=%d\n",
  305. __func__, rc);
  306. swr_device_wakeup_unvote(swr_hap->swr_slave);
  307. return rc;
  308. }
  309. break;
  310. case SND_SOC_DAPM_POST_PMD:
  311. swr_disconnect_port(swr_hap->swr_slave, &port_id, num_port,
  312. &ch_mask, &port_type);
  313. swr_slvdev_datapath_control(swr_hap->swr_slave,
  314. swr_hap->swr_slave->dev_num, false);
  315. swr_device_wakeup_unvote(swr_hap->swr_slave);
  316. break;
  317. default:
  318. break;
  319. }
  320. return 0;
  321. }
  322. static int haptics_vmax_get(struct snd_kcontrol *kcontrol,
  323. struct snd_ctl_elem_value *ucontrol)
  324. {
  325. struct snd_soc_component *component =
  326. snd_soc_kcontrol_component(kcontrol);
  327. struct swr_haptics_dev *swr_hap =
  328. snd_soc_component_get_drvdata(component);
  329. pr_debug("%s: vmax %u\n", __func__, swr_hap->vmax);
  330. ucontrol->value.integer.value[0] = swr_hap->vmax;
  331. return 0;
  332. }
  333. static int haptics_vmax_put(struct snd_kcontrol *kcontrol,
  334. struct snd_ctl_elem_value *ucontrol)
  335. {
  336. struct snd_soc_component *component =
  337. snd_soc_kcontrol_component(kcontrol);
  338. struct swr_haptics_dev *swr_hap =
  339. snd_soc_component_get_drvdata(component);
  340. swr_hap->vmax = ucontrol->value.integer.value[0];
  341. pr_debug("%s: vmax %u\n", __func__, swr_hap->vmax);
  342. return 0;
  343. }
  344. static const struct snd_kcontrol_new haptics_snd_controls[] = {
  345. SOC_SINGLE_EXT("Haptics Amplitude Step", SND_SOC_NOPM, 0, 100, 0,
  346. haptics_vmax_get, haptics_vmax_put),
  347. };
  348. static const struct snd_soc_dapm_widget haptics_comp_dapm_widgets[] = {
  349. SND_SOC_DAPM_INPUT("HAP_IN"),
  350. SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0,
  351. hap_swr_dac_port, ARRAY_SIZE(hap_swr_dac_port),
  352. hap_enable_swr_dac_port,
  353. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  354. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  355. SND_SOC_DAPM_SPK("HAP_OUT", NULL),
  356. };
  357. static const struct snd_soc_dapm_route haptics_comp_dapm_route[] = {
  358. {"SWR DAC_Port", "Switch", "HAP_IN"},
  359. {"HAP_OUT", NULL, "SWR DAC_Port"},
  360. };
  361. static int haptics_comp_probe(struct snd_soc_component *component)
  362. {
  363. struct snd_soc_dapm_context *dapm;
  364. struct swr_haptics_dev *swr_hap =
  365. snd_soc_component_get_drvdata(component);
  366. if (!swr_hap) {
  367. pr_err("%s: get swr_haptics_dev failed\n", __func__);
  368. return -EINVAL;
  369. }
  370. snd_soc_component_init_regmap(component, swr_hap->regmap);
  371. dapm = snd_soc_component_get_dapm(component);
  372. if (dapm && dapm->component) {
  373. snd_soc_dapm_ignore_suspend(dapm, "HAP_IN");
  374. snd_soc_dapm_ignore_suspend(dapm, "HAP_OUT");
  375. }
  376. return 0;
  377. }
  378. static void haptics_comp_remove(struct snd_soc_component *component)
  379. {
  380. }
  381. static const struct snd_soc_component_driver swr_haptics_component = {
  382. .name = "swr-haptics",
  383. .probe = haptics_comp_probe,
  384. .remove = haptics_comp_remove,
  385. .controls = haptics_snd_controls,
  386. .num_controls = ARRAY_SIZE(haptics_snd_controls),
  387. .dapm_widgets = haptics_comp_dapm_widgets,
  388. .num_dapm_widgets = ARRAY_SIZE(haptics_comp_dapm_widgets),
  389. .dapm_routes = haptics_comp_dapm_route,
  390. .num_dapm_routes = ARRAY_SIZE(haptics_comp_dapm_route),
  391. };
  392. static struct snd_soc_dai_driver haptics_dai[] = {
  393. {
  394. .name = "swr_haptics",
  395. .playback = {
  396. .stream_name = "HAPTICS_AIF Playback",
  397. .rates = HAPTICS_RATES,
  398. .formats = HAPTICS_FORMATS,
  399. .rate_max = 192000,
  400. .rate_min = 8000,
  401. .channels_min = 1,
  402. .channels_max = 1,
  403. },
  404. },
  405. };
  406. static int swr_haptics_parse_port_mapping(struct swr_device *sdev)
  407. {
  408. struct swr_haptics_dev *swr_hap = swr_get_dev_data(sdev);
  409. u32 port_cfg[NUM_SWR_PORT_DT_PARAMS];
  410. int rc;
  411. if (!swr_hap) {
  412. dev_err(&sdev->dev, "%s: get swr_haptics_dev failed\n",
  413. __func__);
  414. return -EINVAL;
  415. }
  416. rc = of_property_read_u32_array(sdev->dev.of_node, "qcom,rx_swr_ch_map",
  417. port_cfg, NUM_SWR_PORT_DT_PARAMS);
  418. if (rc < 0) {
  419. dev_err(swr_hap->dev, "%s: Get qcom,rx_swr_ch_map failed, rc=%d\n",
  420. __func__, rc);
  421. return -EINVAL;
  422. }
  423. swr_hap->port.port_id = (u8) port_cfg[PORT_ID_DT_IDX];
  424. swr_hap->port.num_ch = (u8) port_cfg[NUM_CH_DT_IDX];
  425. swr_hap->port.ch_mask = (u8) port_cfg[CH_MASK_DT_IDX];
  426. swr_hap->port.ch_rate = port_cfg[CH_RATE_DT_IDX];
  427. swr_hap->port.port_type = (u8) port_cfg[PORT_TYPE_DT_IDX];
  428. dev_dbg(swr_hap->dev, "%s: port_id = %d, ch_mask = %d, ch_rate = %d, num_ch = %d, port_type = %d\n",
  429. __func__, swr_hap->port.port_id,
  430. swr_hap->port.ch_mask, swr_hap->port.ch_rate,
  431. swr_hap->port.num_ch, swr_hap->port.port_type);
  432. return 0;
  433. }
  434. #define MAX_HAPTICS_VMAX_MV 10000
  435. #define VMAX_STEP_MV 50
  436. static int hboost_notifier(struct notifier_block *nb, unsigned long event, void *val)
  437. {
  438. struct swr_haptics_dev *swr_hap = container_of(nb, struct swr_haptics_dev, hboost_nb);
  439. u32 vmax_mv;
  440. switch (event) {
  441. case VMAX_CLAMP:
  442. vmax_mv = *(u32 *)val;
  443. if (vmax_mv > MAX_HAPTICS_VMAX_MV) {
  444. dev_err_ratelimited(swr_hap->dev, "%s: voted Vmax (%u mv) is higher than maximum (%u mv)\n",
  445. __func__, vmax_mv, MAX_HAPTICS_VMAX_MV);
  446. return -EINVAL;
  447. }
  448. dev_dbg(swr_hap->dev, "%s: Vmax is clamped at %u mv to support hBoost concurrency\n",
  449. __func__, vmax_mv);
  450. swr_hap->clamped_vmax = vmax_mv / VMAX_STEP_MV;
  451. break;
  452. default:
  453. break;
  454. }
  455. return 0;
  456. }
  457. static int swr_haptics_probe(struct swr_device *sdev)
  458. {
  459. struct swr_haptics_dev *swr_hap;
  460. struct device_node *node = sdev->dev.of_node;
  461. int rc;
  462. u8 devnum;
  463. u32 pmic_type;
  464. int retry = 5;
  465. swr_hap = devm_kzalloc(&sdev->dev,
  466. sizeof(struct swr_haptics_dev), GFP_KERNEL);
  467. if (!swr_hap)
  468. return -ENOMEM;
  469. /* VMAX default to 5V */
  470. swr_hap->vmax = 100;
  471. swr_hap->swr_slave = sdev;
  472. swr_hap->dev = &sdev->dev;
  473. pmic_type = (uintptr_t)of_device_get_match_data(swr_hap->dev);
  474. if (pmic_type == PM8350B)
  475. swr_hap->flags |= HAP_SSR_RECOVERY;
  476. swr_set_dev_data(sdev, swr_hap);
  477. rc = swr_haptics_parse_port_mapping(sdev);
  478. if (rc < 0) {
  479. dev_err(swr_hap->dev, "%s: failed to parse swr port mapping, rc=%d\n",
  480. __func__, rc);
  481. goto clean;
  482. }
  483. swr_hap->slave_vdd = devm_regulator_get(swr_hap->dev, "swr-slave");
  484. if (IS_ERR(swr_hap->slave_vdd)) {
  485. rc = PTR_ERR(swr_hap->slave_vdd);
  486. if (rc != -EPROBE_DEFER)
  487. dev_err(swr_hap->dev, "%s: get swr-slave-supply failed, rc=%d\n",
  488. __func__, rc);
  489. goto clean;
  490. }
  491. if (of_find_property(node, "qcom,hpwr-supply", NULL)) {
  492. swr_hap->hpwr_vreg = devm_regulator_get(swr_hap->dev,
  493. "qcom,hpwr");
  494. if (IS_ERR(swr_hap->hpwr_vreg)) {
  495. rc = PTR_ERR(swr_hap->hpwr_vreg);
  496. if (rc != -EPROBE_DEFER)
  497. dev_err(swr_hap->dev, "%s: Get qcom,hpwr-supply failed, rc=%d\n",
  498. __func__, rc);
  499. goto clean;
  500. }
  501. rc = of_property_read_u32(node, "qcom,hpwr-voltage-mv",
  502. &swr_hap->hpwr_voltage_mv);
  503. if (rc < 0) {
  504. dev_err(swr_hap->dev, "%s: Failed to read qcom,hpwr-voltage-mv, rc=%d\n",
  505. __func__, rc);
  506. goto clean;
  507. }
  508. }
  509. rc = swr_haptics_slave_enable(swr_hap);
  510. if (rc < 0) {
  511. dev_err(swr_hap->dev, "%s: enable swr-slave-vdd failed, rc=%d\n",
  512. __func__, rc);
  513. goto clean;
  514. }
  515. do {
  516. /* Add delay for soundwire enumeration */
  517. usleep_range(500, 510);
  518. rc = swr_get_logical_dev_num(sdev, sdev->addr, &devnum);
  519. } while (rc && --retry);
  520. if (rc) {
  521. dev_err(swr_hap->dev, "%s: failed to get devnum for swr-haptics, rc=%d\n",
  522. __func__, rc);
  523. rc = -EPROBE_DEFER;
  524. goto dev_err;
  525. }
  526. sdev->dev_num = devnum;
  527. swr_hap->regmap = devm_regmap_init_swr(sdev, &swr_hap_regmap_config);
  528. if (IS_ERR(swr_hap->regmap)) {
  529. rc = PTR_ERR(swr_hap->regmap);
  530. dev_err(swr_hap->dev, "%s: init regmap failed, rc=%d\n",
  531. __func__, rc);
  532. goto dev_err;
  533. }
  534. rc = snd_soc_register_component(&sdev->dev,
  535. &swr_haptics_component, haptics_dai, ARRAY_SIZE(haptics_dai));
  536. if (rc) {
  537. dev_err(swr_hap->dev, "%s: register swr_haptics component failed, rc=%d\n",
  538. __func__, rc);
  539. goto dev_err;
  540. }
  541. swr_hap->hboost_nb.notifier_call = hboost_notifier;
  542. register_hboost_event_notifier(&swr_hap->hboost_nb);
  543. return 0;
  544. dev_err:
  545. swr_haptics_slave_disable(swr_hap);
  546. swr_remove_device(sdev);
  547. clean:
  548. swr_set_dev_data(sdev, NULL);
  549. return rc;
  550. }
  551. static int swr_haptics_remove(struct swr_device *sdev)
  552. {
  553. struct swr_haptics_dev *swr_hap;
  554. int rc = 0;
  555. swr_hap = swr_get_dev_data(sdev);
  556. if (!swr_hap) {
  557. dev_err(&sdev->dev, "%s: no data for swr_hap\n", __func__);
  558. rc = -ENODEV;
  559. goto clean;
  560. }
  561. unregister_hboost_event_notifier(&swr_hap->hboost_nb);
  562. rc = swr_haptics_slave_disable(swr_hap);
  563. if (rc < 0) {
  564. dev_err(swr_hap->dev, "%s: disable swr-slave failed, rc=%d\n",
  565. __func__, rc);
  566. goto clean;
  567. }
  568. clean:
  569. snd_soc_unregister_component(&sdev->dev);
  570. swr_set_dev_data(sdev, NULL);
  571. return rc;
  572. }
  573. static int swr_haptics_device_up(struct swr_device *sdev)
  574. {
  575. struct swr_haptics_dev *swr_hap;
  576. swr_hap = swr_get_dev_data(sdev);
  577. if (!swr_hap) {
  578. dev_err_ratelimited(&sdev->dev, "%s: no data for swr_hap\n", __func__);
  579. return -ENODEV;
  580. }
  581. if (swr_hap->flags & HAP_SSR_RECOVERY)
  582. swr_hap->ssr_recovery = true;
  583. /* Take SWR slave out of reset */
  584. return swr_haptics_slave_enable(swr_hap);
  585. }
  586. static int swr_haptics_device_down(struct swr_device *sdev)
  587. {
  588. struct swr_haptics_dev *swr_hap = swr_get_dev_data(sdev);
  589. int rc;
  590. if (!swr_hap) {
  591. dev_err_ratelimited(&sdev->dev, "%s: no data for swr_hap\n", __func__);
  592. return -ENODEV;
  593. }
  594. /* Disable HAP_PWR regulator */
  595. rc = swr_hap_disable_hpwr_vreg(swr_hap);
  596. if (rc < 0) {
  597. dev_err_ratelimited(swr_hap->dev, "Disable hpwr_vreg failed, rc=%d\n",
  598. rc);
  599. return rc;
  600. }
  601. /* Put SWR slave into reset */
  602. return swr_haptics_slave_disable(swr_hap);
  603. }
  604. static int swr_haptics_suspend(struct device *dev)
  605. {
  606. struct swr_haptics_dev *swr_hap;
  607. int rc = 0;
  608. swr_hap = swr_get_dev_data(to_swr_device(dev));
  609. if (!swr_hap) {
  610. dev_err_ratelimited(dev, "%s: no data for swr_hap\n", __func__);
  611. return -ENODEV;
  612. }
  613. return rc;
  614. }
  615. static int swr_haptics_resume(struct device *dev)
  616. {
  617. struct swr_haptics_dev *swr_hap;
  618. int rc = 0;
  619. swr_hap = swr_get_dev_data(to_swr_device(dev));
  620. if (!swr_hap) {
  621. dev_err_ratelimited(dev, "%s: no data for swr_hap\n", __func__);
  622. return -ENODEV;
  623. }
  624. return rc;
  625. }
  626. static const struct of_device_id swr_haptics_match_table[] = {
  627. {
  628. .compatible = "qcom,swr-haptics",
  629. .data = NULL,
  630. },
  631. {
  632. .compatible = "qcom,pm8350b-swr-haptics",
  633. .data = (void *)PM8350B,
  634. },
  635. {
  636. .compatible = "qcom,pm8550b-swr-haptics",
  637. .data = (void *)PM8550B,
  638. },
  639. { },
  640. };
  641. static const struct swr_device_id swr_haptics_id[] = {
  642. {"swr-haptics", 0},
  643. {"pm8350b-swr-haptics", 0},
  644. {"pm8550b-swr-haptics", 0},
  645. {},
  646. };
  647. static const struct dev_pm_ops swr_haptics_pm_ops = {
  648. .suspend = swr_haptics_suspend,
  649. .resume = swr_haptics_resume,
  650. };
  651. static struct swr_driver swr_haptics_driver = {
  652. .driver = {
  653. .name = "swr-haptics",
  654. .owner = THIS_MODULE,
  655. .pm = &swr_haptics_pm_ops,
  656. .of_match_table = swr_haptics_match_table,
  657. },
  658. .probe = swr_haptics_probe,
  659. .remove = swr_haptics_remove,
  660. .id_table = swr_haptics_id,
  661. .device_up = swr_haptics_device_up,
  662. .device_down = swr_haptics_device_down,
  663. };
  664. static int __init swr_haptics_init(void)
  665. {
  666. return swr_driver_register(&swr_haptics_driver);
  667. }
  668. static void __exit swr_haptics_exit(void)
  669. {
  670. swr_driver_unregister(&swr_haptics_driver);
  671. }
  672. module_init(swr_haptics_init);
  673. module_exit(swr_haptics_exit);
  674. MODULE_DESCRIPTION("SWR haptics driver");
  675. MODULE_LICENSE("GPL v2");