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 
17 /**
18  * struct ti_sci_power_domain_data - pm domain controller information structure
19  * @sci: TI SCI handle used for communication with system controller
20  */
21 struct ti_sci_power_domain_data {
22 	const struct ti_sci_handle *sci;
23 };
24 
25 static int ti_sci_power_domain_probe(struct udevice *dev)
26 {
27 	struct ti_sci_power_domain_data *data = dev_get_priv(dev);
28 
29 	debug("%s(dev=%p)\n", __func__, dev);
30 
31 	if (!data)
32 		return -ENOMEM;
33 
34 	/* Store handle for communication with the system controller */
35 	data->sci = ti_sci_get_handle(dev);
36 	if (IS_ERR(data->sci))
37 		return PTR_ERR(data->sci);
38 
39 	return 0;
40 }
41 
42 static int ti_sci_power_domain_request(struct power_domain *pd)
43 {
44 	debug("%s(pd=%p)\n", __func__, pd);
45 	return 0;
46 }
47 
48 static int ti_sci_power_domain_free(struct power_domain *pd)
49 {
50 	debug("%s(pd=%p)\n", __func__, pd);
51 	return 0;
52 }
53 
54 static int ti_sci_power_domain_on(struct power_domain *pd)
55 {
56 	struct ti_sci_power_domain_data *data = dev_get_priv(pd->dev);
57 	const struct ti_sci_handle *sci = data->sci;
58 	const struct ti_sci_dev_ops *dops = &sci->ops.dev_ops;
59 	int ret;
60 
61 	debug("%s(pd=%p)\n", __func__, pd);
62 
63 	ret = dops->get_device(sci, pd->id);
64 	if (ret)
65 		dev_err(power_domain->dev, "%s: get_device failed (%d)\n",
66 			__func__, ret);
67 
68 	return ret;
69 }
70 
71 static int ti_sci_power_domain_off(struct power_domain *pd)
72 {
73 	struct ti_sci_power_domain_data *data = dev_get_priv(pd->dev);
74 	const struct ti_sci_handle *sci = data->sci;
75 	const struct ti_sci_dev_ops *dops = &sci->ops.dev_ops;
76 	int ret;
77 
78 	debug("%s(pd=%p)\n", __func__, pd);
79 
80 	ret = dops->put_device(sci, pd->id);
81 	if (ret)
82 		dev_err(power_domain->dev, "%s: put_device failed (%d)\n",
83 			__func__, ret);
84 
85 	return ret;
86 }
87 
88 static const struct udevice_id ti_sci_power_domain_of_match[] = {
89 	{ .compatible = "ti,sci-pm-domain" },
90 	{ /* sentinel */ }
91 };
92 
93 static struct power_domain_ops ti_sci_power_domain_ops = {
94 	.request = ti_sci_power_domain_request,
95 	.free = ti_sci_power_domain_free,
96 	.on = ti_sci_power_domain_on,
97 	.off = ti_sci_power_domain_off,
98 };
99 
100 U_BOOT_DRIVER(ti_sci_pm_domains) = {
101 	.name = "ti-sci-pm-domains",
102 	.id = UCLASS_POWER_DOMAIN,
103 	.of_match = ti_sci_power_domain_of_match,
104 	.probe = ti_sci_power_domain_probe,
105 	.priv_auto_alloc_size = sizeof(struct ti_sci_power_domain_data),
106 	.ops = &ti_sci_power_domain_ops,
107 };
108