Merge tag 'i2c-for-6.9-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[linux-block.git] / sound / soc / codecs / lpass-macro-common.c
CommitLineData
9e3d83c5
SRM
1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright (c) 2022, The Linux Foundation. All rights reserved.
3
4#include <linux/export.h>
5#include <linux/module.h>
6#include <linux/init.h>
340d79a1 7#include <linux/of.h>
9e3d83c5
SRM
8#include <linux/platform_device.h>
9#include <linux/pm_domain.h>
10#include <linux/pm_runtime.h>
11
12#include "lpass-macro-common.h"
13
14struct lpass_macro *lpass_macro_pds_init(struct device *dev)
15{
16 struct lpass_macro *l_pds;
17 int ret;
18
1e108e60 19 if (!of_property_present(dev->of_node, "power-domains"))
9e3d83c5
SRM
20 return NULL;
21
22 l_pds = devm_kzalloc(dev, sizeof(*l_pds), GFP_KERNEL);
23 if (!l_pds)
24 return ERR_PTR(-ENOMEM);
25
26 l_pds->macro_pd = dev_pm_domain_attach_by_name(dev, "macro");
1a8ee4cf 27 if (IS_ERR_OR_NULL(l_pds->macro_pd)) {
81e7b165 28 ret = l_pds->macro_pd ? PTR_ERR(l_pds->macro_pd) : -ENODATA;
9e3d83c5
SRM
29 goto macro_err;
30 }
31
1a8ee4cf
SRM
32 ret = pm_runtime_resume_and_get(l_pds->macro_pd);
33 if (ret < 0)
34 goto macro_sync_err;
35
9e3d83c5 36 l_pds->dcodec_pd = dev_pm_domain_attach_by_name(dev, "dcodec");
1a8ee4cf 37 if (IS_ERR_OR_NULL(l_pds->dcodec_pd)) {
81e7b165 38 ret = l_pds->dcodec_pd ? PTR_ERR(l_pds->dcodec_pd) : -ENODATA;
9e3d83c5 39 goto dcodec_err;
1a8ee4cf 40 }
9e3d83c5 41
1a8ee4cf
SRM
42 ret = pm_runtime_resume_and_get(l_pds->dcodec_pd);
43 if (ret < 0)
9e3d83c5 44 goto dcodec_sync_err;
9e3d83c5
SRM
45 return l_pds;
46
47dcodec_sync_err:
48 dev_pm_domain_detach(l_pds->dcodec_pd, false);
49dcodec_err:
50 pm_runtime_put(l_pds->macro_pd);
1a8ee4cf 51macro_sync_err:
9e3d83c5 52 dev_pm_domain_detach(l_pds->macro_pd, false);
1a8ee4cf 53macro_err:
9e3d83c5
SRM
54 return ERR_PTR(ret);
55}
56EXPORT_SYMBOL_GPL(lpass_macro_pds_init);
57
58void lpass_macro_pds_exit(struct lpass_macro *pds)
59{
1a8ee4cf
SRM
60 if (pds) {
61 pm_runtime_put(pds->macro_pd);
62 dev_pm_domain_detach(pds->macro_pd, false);
63 pm_runtime_put(pds->dcodec_pd);
64 dev_pm_domain_detach(pds->dcodec_pd, false);
65 }
9e3d83c5
SRM
66}
67EXPORT_SYMBOL_GPL(lpass_macro_pds_exit);
68
69MODULE_DESCRIPTION("Common macro driver");
70MODULE_LICENSE("GPL");