swr-haptics.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. return rc;
  273. }
  274. swr_slvdev_datapath_control(swr_hap->swr_slave,
  275. swr_hap->swr_slave->dev_num, true);
  276. /* trigger SWR play */
  277. val = SWR_PLAY_BIT | SWR_PLAY_SRC_VAL_SWR;
  278. rc = regmap_write(swr_hap->regmap, SWR_PLAY_REG, val);
  279. if (rc) {
  280. dev_err_ratelimited(swr_hap->dev, "%s: Enable SWR_PLAY failed, rc=%d\n",
  281. __func__, rc);
  282. swr_slvdev_datapath_control(swr_hap->swr_slave,
  283. swr_hap->swr_slave->dev_num, false);
  284. swr_hap_disable_hpwr_vreg(swr_hap);
  285. return rc;
  286. }
  287. break;
  288. case SND_SOC_DAPM_PRE_PMD:
  289. /* stop SWR play */
  290. val = SWR_PLAY_SRC_VAL_SWR;
  291. rc = regmap_write(swr_hap->regmap, SWR_PLAY_REG, val);
  292. if (rc) {
  293. dev_err_ratelimited(swr_hap->dev, "%s: Enable SWR_PLAY failed, rc=%d\n",
  294. __func__, rc);
  295. return rc;
  296. }
  297. rc = swr_hap_disable_hpwr_vreg(swr_hap);
  298. if (rc < 0) {
  299. dev_err_ratelimited(swr_hap->dev, "%s: Disable hpwr_vreg failed, rc=%d\n",
  300. __func__, rc);
  301. return rc;
  302. }
  303. break;
  304. case SND_SOC_DAPM_POST_PMD:
  305. swr_disconnect_port(swr_hap->swr_slave, &port_id, num_port,
  306. &ch_mask, &port_type);
  307. swr_slvdev_datapath_control(swr_hap->swr_slave,
  308. swr_hap->swr_slave->dev_num, false);
  309. swr_device_wakeup_unvote(swr_hap->swr_slave);
  310. break;
  311. default:
  312. break;
  313. }
  314. return 0;
  315. }
  316. static int haptics_vmax_get(struct snd_kcontrol *kcontrol,
  317. struct snd_ctl_elem_value *ucontrol)
  318. {
  319. struct snd_soc_component *component =
  320. snd_soc_kcontrol_component(kcontrol);
  321. struct swr_haptics_dev *swr_hap =
  322. snd_soc_component_get_drvdata(component);
  323. pr_debug("%s: vmax %u\n", __func__, swr_hap->vmax);
  324. ucontrol->value.integer.value[0] = swr_hap->vmax;
  325. return 0;
  326. }
  327. static int haptics_vmax_put(struct snd_kcontrol *kcontrol,
  328. struct snd_ctl_elem_value *ucontrol)
  329. {
  330. struct snd_soc_component *component =
  331. snd_soc_kcontrol_component(kcontrol);
  332. struct swr_haptics_dev *swr_hap =
  333. snd_soc_component_get_drvdata(component);
  334. swr_hap->vmax = ucontrol->value.integer.value[0];
  335. pr_debug("%s: vmax %u\n", __func__, swr_hap->vmax);
  336. return 0;
  337. }
  338. static const struct snd_kcontrol_new haptics_snd_controls[] = {
  339. SOC_SINGLE_EXT("Haptics Amplitude Step", SND_SOC_NOPM, 0, 100, 0,
  340. haptics_vmax_get, haptics_vmax_put),
  341. };
  342. static const struct snd_soc_dapm_widget haptics_comp_dapm_widgets[] = {
  343. SND_SOC_DAPM_INPUT("HAP_IN"),
  344. SND_SOC_DAPM_MIXER_E("SWR DAC_Port", SND_SOC_NOPM, 0, 0,
  345. hap_swr_dac_port, ARRAY_SIZE(hap_swr_dac_port),
  346. hap_enable_swr_dac_port,
  347. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  348. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  349. SND_SOC_DAPM_SPK("HAP_OUT", NULL),
  350. };
  351. static const struct snd_soc_dapm_route haptics_comp_dapm_route[] = {
  352. {"SWR DAC_Port", "Switch", "HAP_IN"},
  353. {"HAP_OUT", NULL, "SWR DAC_Port"},
  354. };
  355. static int haptics_comp_probe(struct snd_soc_component *component)
  356. {
  357. struct snd_soc_dapm_context *dapm;
  358. struct swr_haptics_dev *swr_hap =
  359. snd_soc_component_get_drvdata(component);
  360. if (!swr_hap) {
  361. pr_err("%s: get swr_haptics_dev failed\n", __func__);
  362. return -EINVAL;
  363. }
  364. snd_soc_component_init_regmap(component, swr_hap->regmap);
  365. dapm = snd_soc_component_get_dapm(component);
  366. if (dapm && dapm->component) {
  367. snd_soc_dapm_ignore_suspend(dapm, "HAP_IN");
  368. snd_soc_dapm_ignore_suspend(dapm, "HAP_OUT");
  369. }
  370. return 0;
  371. }
  372. static void haptics_comp_remove(struct snd_soc_component *component)
  373. {
  374. }
  375. static const struct snd_soc_component_driver swr_haptics_component = {
  376. .name = "swr-haptics",
  377. .probe = haptics_comp_probe,
  378. .remove = haptics_comp_remove,
  379. .controls = haptics_snd_controls,
  380. .num_controls = ARRAY_SIZE(haptics_snd_controls),
  381. .dapm_widgets = haptics_comp_dapm_widgets,
  382. .num_dapm_widgets = ARRAY_SIZE(haptics_comp_dapm_widgets),
  383. .dapm_routes = haptics_comp_dapm_route,
  384. .num_dapm_routes = ARRAY_SIZE(haptics_comp_dapm_route),
  385. };
  386. static struct snd_soc_dai_driver haptics_dai[] = {
  387. {
  388. .name = "swr_haptics",
  389. .playback = {
  390. .stream_name = "HAPTICS_AIF Playback",
  391. .rates = HAPTICS_RATES,
  392. .formats = HAPTICS_FORMATS,
  393. .rate_max = 192000,
  394. .rate_min = 8000,
  395. .channels_min = 1,
  396. .channels_max = 1,
  397. },
  398. },
  399. };
  400. static int swr_haptics_parse_port_mapping(struct swr_device *sdev)
  401. {
  402. struct swr_haptics_dev *swr_hap = swr_get_dev_data(sdev);
  403. u32 port_cfg[NUM_SWR_PORT_DT_PARAMS];
  404. int rc;
  405. if (!swr_hap) {
  406. dev_err(&sdev->dev, "%s: get swr_haptics_dev failed\n",
  407. __func__);
  408. return -EINVAL;
  409. }
  410. rc = of_property_read_u32_array(sdev->dev.of_node, "qcom,rx_swr_ch_map",
  411. port_cfg, NUM_SWR_PORT_DT_PARAMS);
  412. if (rc < 0) {
  413. dev_err(swr_hap->dev, "%s: Get qcom,rx_swr_ch_map failed, rc=%d\n",
  414. __func__, rc);
  415. return -EINVAL;
  416. }
  417. swr_hap->port.port_id = (u8) port_cfg[PORT_ID_DT_IDX];
  418. swr_hap->port.num_ch = (u8) port_cfg[NUM_CH_DT_IDX];
  419. swr_hap->port.ch_mask = (u8) port_cfg[CH_MASK_DT_IDX];
  420. swr_hap->port.ch_rate = port_cfg[CH_RATE_DT_IDX];
  421. swr_hap->port.port_type = (u8) port_cfg[PORT_TYPE_DT_IDX];
  422. dev_dbg(swr_hap->dev, "%s: port_id = %d, ch_mask = %d, ch_rate = %d, num_ch = %d, port_type = %d\n",
  423. __func__, swr_hap->port.port_id,
  424. swr_hap->port.ch_mask, swr_hap->port.ch_rate,
  425. swr_hap->port.num_ch, swr_hap->port.port_type);
  426. return 0;
  427. }
  428. #define MAX_HAPTICS_VMAX_MV 10000
  429. #define VMAX_STEP_MV 50
  430. static int hboost_notifier(struct notifier_block *nb, unsigned long event, void *val)
  431. {
  432. struct swr_haptics_dev *swr_hap = container_of(nb, struct swr_haptics_dev, hboost_nb);
  433. u32 vmax_mv;
  434. switch (event) {
  435. case VMAX_CLAMP:
  436. vmax_mv = *(u32 *)val;
  437. if (vmax_mv > MAX_HAPTICS_VMAX_MV) {
  438. dev_err_ratelimited(swr_hap->dev, "%s: voted Vmax (%u mv) is higher than maximum (%u mv)\n",
  439. __func__, vmax_mv, MAX_HAPTICS_VMAX_MV);
  440. return -EINVAL;
  441. }
  442. dev_dbg(swr_hap->dev, "%s: Vmax is clamped at %u mv to support hBoost concurrency\n",
  443. __func__, vmax_mv);
  444. swr_hap->clamped_vmax = vmax_mv / VMAX_STEP_MV;
  445. break;
  446. default:
  447. break;
  448. }
  449. return 0;
  450. }
  451. static int swr_haptics_probe(struct swr_device *sdev)
  452. {
  453. struct swr_haptics_dev *swr_hap;
  454. struct device_node *node = sdev->dev.of_node;
  455. int rc;
  456. u8 devnum;
  457. u32 pmic_type;
  458. int retry = 5;
  459. swr_hap = devm_kzalloc(&sdev->dev,
  460. sizeof(struct swr_haptics_dev), GFP_KERNEL);
  461. if (!swr_hap)
  462. return -ENOMEM;
  463. /* VMAX default to 5V */
  464. swr_hap->vmax = 100;
  465. swr_hap->swr_slave = sdev;
  466. swr_hap->dev = &sdev->dev;
  467. pmic_type = (uintptr_t)of_device_get_match_data(swr_hap->dev);
  468. if (pmic_type == PM8350B)
  469. swr_hap->flags |= HAP_SSR_RECOVERY;
  470. swr_set_dev_data(sdev, swr_hap);
  471. rc = swr_haptics_parse_port_mapping(sdev);
  472. if (rc < 0) {
  473. dev_err(swr_hap->dev, "%s: failed to parse swr port mapping, rc=%d\n",
  474. __func__, rc);
  475. goto clean;
  476. }
  477. swr_hap->slave_vdd = devm_regulator_get(swr_hap->dev, "swr-slave");
  478. if (IS_ERR(swr_hap->slave_vdd)) {
  479. rc = PTR_ERR(swr_hap->slave_vdd);
  480. if (rc != -EPROBE_DEFER)
  481. dev_err(swr_hap->dev, "%s: get swr-slave-supply failed, rc=%d\n",
  482. __func__, rc);
  483. goto clean;
  484. }
  485. if (of_find_property(node, "qcom,hpwr-supply", NULL)) {
  486. swr_hap->hpwr_vreg = devm_regulator_get(swr_hap->dev,
  487. "qcom,hpwr");
  488. if (IS_ERR(swr_hap->hpwr_vreg)) {
  489. rc = PTR_ERR(swr_hap->hpwr_vreg);
  490. if (rc != -EPROBE_DEFER)
  491. dev_err(swr_hap->dev, "%s: Get qcom,hpwr-supply failed, rc=%d\n",
  492. __func__, rc);
  493. goto clean;
  494. }
  495. rc = of_property_read_u32(node, "qcom,hpwr-voltage-mv",
  496. &swr_hap->hpwr_voltage_mv);
  497. if (rc < 0) {
  498. dev_err(swr_hap->dev, "%s: Failed to read qcom,hpwr-voltage-mv, rc=%d\n",
  499. __func__, rc);
  500. goto clean;
  501. }
  502. }
  503. rc = swr_haptics_slave_enable(swr_hap);
  504. if (rc < 0) {
  505. dev_err(swr_hap->dev, "%s: enable swr-slave-vdd failed, rc=%d\n",
  506. __func__, rc);
  507. goto clean;
  508. }
  509. do {
  510. /* Add delay for soundwire enumeration */
  511. usleep_range(500, 510);
  512. rc = swr_get_logical_dev_num(sdev, sdev->addr, &devnum);
  513. } while (rc && --retry);
  514. if (rc) {
  515. dev_err(swr_hap->dev, "%s: failed to get devnum for swr-haptics, rc=%d\n",
  516. __func__, rc);
  517. rc = -EPROBE_DEFER;
  518. goto dev_err;
  519. }
  520. sdev->dev_num = devnum;
  521. swr_hap->regmap = devm_regmap_init_swr(sdev, &swr_hap_regmap_config);
  522. if (IS_ERR(swr_hap->regmap)) {
  523. rc = PTR_ERR(swr_hap->regmap);
  524. dev_err(swr_hap->dev, "%s: init regmap failed, rc=%d\n",
  525. __func__, rc);
  526. goto dev_err;
  527. }
  528. rc = snd_soc_register_component(&sdev->dev,
  529. &swr_haptics_component, haptics_dai, ARRAY_SIZE(haptics_dai));
  530. if (rc) {
  531. dev_err(swr_hap->dev, "%s: register swr_haptics component failed, rc=%d\n",
  532. __func__, rc);
  533. goto dev_err;
  534. }
  535. swr_hap->hboost_nb.notifier_call = hboost_notifier;
  536. register_hboost_event_notifier(&swr_hap->hboost_nb);
  537. return 0;
  538. dev_err:
  539. swr_haptics_slave_disable(swr_hap);
  540. swr_remove_device(sdev);
  541. clean:
  542. swr_set_dev_data(sdev, NULL);
  543. return rc;
  544. }
  545. static int swr_haptics_remove(struct swr_device *sdev)
  546. {
  547. struct swr_haptics_dev *swr_hap;
  548. int rc = 0;
  549. swr_hap = swr_get_dev_data(sdev);
  550. if (!swr_hap) {
  551. dev_err(&sdev->dev, "%s: no data for swr_hap\n", __func__);
  552. rc = -ENODEV;
  553. goto clean;
  554. }
  555. unregister_hboost_event_notifier(&swr_hap->hboost_nb);
  556. rc = swr_haptics_slave_disable(swr_hap);
  557. if (rc < 0) {
  558. dev_err(swr_hap->dev, "%s: disable swr-slave failed, rc=%d\n",
  559. __func__, rc);
  560. goto clean;
  561. }
  562. clean:
  563. snd_soc_unregister_component(&sdev->dev);
  564. swr_set_dev_data(sdev, NULL);
  565. return rc;
  566. }
  567. static int swr_haptics_device_up(struct swr_device *sdev)
  568. {
  569. struct swr_haptics_dev *swr_hap;
  570. swr_hap = swr_get_dev_data(sdev);
  571. if (!swr_hap) {
  572. dev_err_ratelimited(&sdev->dev, "%s: no data for swr_hap\n", __func__);
  573. return -ENODEV;
  574. }
  575. if (swr_hap->flags & HAP_SSR_RECOVERY)
  576. swr_hap->ssr_recovery = true;
  577. /* Take SWR slave out of reset */
  578. return swr_haptics_slave_enable(swr_hap);
  579. }
  580. static int swr_haptics_device_down(struct swr_device *sdev)
  581. {
  582. struct swr_haptics_dev *swr_hap = swr_get_dev_data(sdev);
  583. int rc;
  584. if (!swr_hap) {
  585. dev_err_ratelimited(&sdev->dev, "%s: no data for swr_hap\n", __func__);
  586. return -ENODEV;
  587. }
  588. /* Disable HAP_PWR regulator */
  589. rc = swr_hap_disable_hpwr_vreg(swr_hap);
  590. if (rc < 0) {
  591. dev_err_ratelimited(swr_hap->dev, "Disable hpwr_vreg failed, rc=%d\n",
  592. rc);
  593. return rc;
  594. }
  595. /* Put SWR slave into reset */
  596. return swr_haptics_slave_disable(swr_hap);
  597. }
  598. static int swr_haptics_suspend(struct device *dev)
  599. {
  600. struct swr_haptics_dev *swr_hap;
  601. int rc = 0;
  602. swr_hap = swr_get_dev_data(to_swr_device(dev));
  603. if (!swr_hap) {
  604. dev_err_ratelimited(dev, "%s: no data for swr_hap\n", __func__);
  605. return -ENODEV;
  606. }
  607. return rc;
  608. }
  609. static int swr_haptics_resume(struct device *dev)
  610. {
  611. struct swr_haptics_dev *swr_hap;
  612. int rc = 0;
  613. swr_hap = swr_get_dev_data(to_swr_device(dev));
  614. if (!swr_hap) {
  615. dev_err_ratelimited(dev, "%s: no data for swr_hap\n", __func__);
  616. return -ENODEV;
  617. }
  618. return rc;
  619. }
  620. static const struct of_device_id swr_haptics_match_table[] = {
  621. {
  622. .compatible = "qcom,swr-haptics",
  623. .data = NULL,
  624. },
  625. {
  626. .compatible = "qcom,pm8350b-swr-haptics",
  627. .data = (void *)PM8350B,
  628. },
  629. {
  630. .compatible = "qcom,pm8550b-swr-haptics",
  631. .data = (void *)PM8550B,
  632. },
  633. { },
  634. };
  635. static const struct swr_device_id swr_haptics_id[] = {
  636. {"swr-haptics", 0},
  637. {"pm8350b-swr-haptics", 0},
  638. {"pm8550b-swr-haptics", 0},
  639. {},
  640. };
  641. static const struct dev_pm_ops swr_haptics_pm_ops = {
  642. .suspend = swr_haptics_suspend,
  643. .resume = swr_haptics_resume,
  644. };
  645. static struct swr_driver swr_haptics_driver = {
  646. .driver = {
  647. .name = "swr-haptics",
  648. .owner = THIS_MODULE,
  649. .pm = &swr_haptics_pm_ops,
  650. .of_match_table = swr_haptics_match_table,
  651. },
  652. .probe = swr_haptics_probe,
  653. .remove = swr_haptics_remove,
  654. .id_table = swr_haptics_id,
  655. .device_up = swr_haptics_device_up,
  656. .device_down = swr_haptics_device_down,
  657. };
  658. static int __init swr_haptics_init(void)
  659. {
  660. return swr_driver_register(&swr_haptics_driver);
  661. }
  662. static void __exit swr_haptics_exit(void)
  663. {
  664. swr_driver_unregister(&swr_haptics_driver);
  665. }
  666. module_init(swr_haptics_init);
  667. module_exit(swr_haptics_exit);
  668. MODULE_DESCRIPTION("SWR haptics driver");
  669. MODULE_LICENSE("GPL v2");