power: domain: ti_sci_power_domains: Add support for exclusive and shared access
[platform/kernel/u-boot.git] / drivers / power / domain / ti-sci-power-domain.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Texas Instruments System Control Interface (TI SCI) power domain driver
4  *
5  * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
6  *      Andreas Dannenberg <dannenberg@ti.com>
7  *
8  * Loosely based on Linux kernel ti_sci_pm_domains.c...
9  */
10
11 #include <common.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <power-domain-uclass.h>
15 #include <linux/soc/ti/ti_sci_protocol.h>
16 #include <dt-bindings/soc/ti,sci_pm_domain.h>
17
18 /**
19  * struct ti_sci_power_domain_data - pm domain controller information structure
20  * @sci: TI SCI handle used for communication with system controller
21  */
22 struct ti_sci_power_domain_data {
23         const struct ti_sci_handle *sci;
24 };
25
26 static int ti_sci_power_domain_probe(struct udevice *dev)
27 {
28         struct ti_sci_power_domain_data *data = dev_get_priv(dev);
29
30         debug("%s(dev=%p)\n", __func__, dev);
31
32         if (!data)
33                 return -ENOMEM;
34
35         /* Store handle for communication with the system controller */
36         data->sci = ti_sci_get_handle(dev);
37         if (IS_ERR(data->sci))
38                 return PTR_ERR(data->sci);
39
40         return 0;
41 }
42
43 static int ti_sci_power_domain_request(struct power_domain *pd)
44 {
45         debug("%s(pd=%p)\n", __func__, pd);
46         return 0;
47 }
48
49 static int ti_sci_power_domain_free(struct power_domain *pd)
50 {
51         debug("%s(pd=%p)\n", __func__, pd);
52         return 0;
53 }
54
55 static int ti_sci_power_domain_on(struct power_domain *pd)
56 {
57         struct ti_sci_power_domain_data *data = dev_get_priv(pd->dev);
58         const struct ti_sci_handle *sci = data->sci;
59         const struct ti_sci_dev_ops *dops = &sci->ops.dev_ops;
60         u8 flags = (uintptr_t)pd->priv;
61         int ret;
62
63         debug("%s(pd=%p)\n", __func__, pd);
64
65         if (flags & TI_SCI_PD_EXCLUSIVE)
66                 ret = dops->get_device_exclusive(sci, pd->id);
67         else
68                 ret = dops->get_device(sci, pd->id);
69
70         if (ret)
71                 dev_err(power_domain->dev, "%s: get_device failed (%d)\n",
72                         __func__, ret);
73
74         return ret;
75 }
76
77 static int ti_sci_power_domain_off(struct power_domain *pd)
78 {
79         struct ti_sci_power_domain_data *data = dev_get_priv(pd->dev);
80         const struct ti_sci_handle *sci = data->sci;
81         const struct ti_sci_dev_ops *dops = &sci->ops.dev_ops;
82         int ret;
83
84         debug("%s(pd=%p)\n", __func__, pd);
85
86         ret = dops->put_device(sci, pd->id);
87         if (ret)
88                 dev_err(power_domain->dev, "%s: put_device failed (%d)\n",
89                         __func__, ret);
90
91         return ret;
92 }
93
94 static int ti_sci_power_domain_of_xlate(struct power_domain *pd,
95                                         struct ofnode_phandle_args *args)
96 {
97         u8 flags;
98
99         debug("%s(power_domain=%p)\n", __func__, pd);
100
101         if (args->args_count < 1) {
102                 debug("Invalid args_count: %d\n", args->args_count);
103                 return -EINVAL;
104         }
105
106         pd->id = args->args[0];
107         /* By default request for device exclusive */
108         flags = TI_SCI_PD_EXCLUSIVE;
109         if (args->args_count == 2)
110                 flags = args->args[1] & TI_SCI_PD_EXCLUSIVE;
111         pd->priv = (void *)(uintptr_t)flags;
112
113         return 0;
114 }
115
116 static const struct udevice_id ti_sci_power_domain_of_match[] = {
117         { .compatible = "ti,sci-pm-domain" },
118         { /* sentinel */ }
119 };
120
121 static struct power_domain_ops ti_sci_power_domain_ops = {
122         .request = ti_sci_power_domain_request,
123         .free = ti_sci_power_domain_free,
124         .on = ti_sci_power_domain_on,
125         .off = ti_sci_power_domain_off,
126         .of_xlate = ti_sci_power_domain_of_xlate,
127 };
128
129 U_BOOT_DRIVER(ti_sci_pm_domains) = {
130         .name = "ti-sci-pm-domains",
131         .id = UCLASS_POWER_DOMAIN,
132         .of_match = ti_sci_power_domain_of_match,
133         .probe = ti_sci_power_domain_probe,
134         .priv_auto_alloc_size = sizeof(struct ti_sci_power_domain_data),
135         .ops = &ti_sci_power_domain_ops,
136 };