1 /*
2 * TI SCI Generic Power Domain Driver
3 *
4 * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
5 * J Keerthy <j-keerthy@ti.com>
6 * Dave Gerlach <d-gerlach@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18 #include <linux/err.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/of.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_domain.h>
24 #include <linux/slab.h>
25 #include <linux/soc/ti/ti_sci_protocol.h>
26
27 /**
28 * struct ti_sci_genpd_dev_data: holds data needed for every device attached
29 * to this genpd
30 * @idx: index of the device that identifies it with the system
31 * control processor.
32 */
33 struct ti_sci_genpd_dev_data {
34 int idx;
35 };
36
37 /**
38 * struct ti_sci_pm_domain: TI specific data needed for power domain
39 * @ti_sci: handle to TI SCI protocol driver that provides ops to
40 * communicate with system control processor.
41 * @dev: pointer to dev for the driver for devm allocs
42 * @pd: generic_pm_domain for use with the genpd framework
43 */
44 struct ti_sci_pm_domain {
45 const struct ti_sci_handle *ti_sci;
46 struct device *dev;
47 struct generic_pm_domain pd;
48 };
49
50 #define genpd_to_ti_sci_pd(gpd) container_of(gpd, struct ti_sci_pm_domain, pd)
51
52 /**
53 * ti_sci_dev_id(): get prepopulated ti_sci id from struct dev
54 * @dev: pointer to device associated with this genpd
55 *
56 * Returns device_id stored from ti,sci_id property
57 */
ti_sci_dev_id(struct device * dev)58 static int ti_sci_dev_id(struct device *dev)
59 {
60 struct generic_pm_domain_data *genpd_data = dev_gpd_data(dev);
61 struct ti_sci_genpd_dev_data *sci_dev_data = genpd_data->data;
62
63 return sci_dev_data->idx;
64 }
65
66 /**
67 * ti_sci_dev_to_sci_handle(): get pointer to ti_sci_handle
68 * @dev: pointer to device associated with this genpd
69 *
70 * Returns ti_sci_handle to be used to communicate with system
71 * control processor.
72 */
ti_sci_dev_to_sci_handle(struct device * dev)73 static const struct ti_sci_handle *ti_sci_dev_to_sci_handle(struct device *dev)
74 {
75 struct generic_pm_domain *pd = pd_to_genpd(dev->pm_domain);
76 struct ti_sci_pm_domain *ti_sci_genpd = genpd_to_ti_sci_pd(pd);
77
78 return ti_sci_genpd->ti_sci;
79 }
80
81 /**
82 * ti_sci_dev_start(): genpd device start hook called to turn device on
83 * @dev: pointer to device associated with this genpd to be powered on
84 */
ti_sci_dev_start(struct device * dev)85 static int ti_sci_dev_start(struct device *dev)
86 {
87 const struct ti_sci_handle *ti_sci = ti_sci_dev_to_sci_handle(dev);
88 int idx = ti_sci_dev_id(dev);
89
90 return ti_sci->ops.dev_ops.get_device(ti_sci, idx);
91 }
92
93 /**
94 * ti_sci_dev_stop(): genpd device stop hook called to turn device off
95 * @dev: pointer to device associated with this genpd to be powered off
96 */
ti_sci_dev_stop(struct device * dev)97 static int ti_sci_dev_stop(struct device *dev)
98 {
99 const struct ti_sci_handle *ti_sci = ti_sci_dev_to_sci_handle(dev);
100 int idx = ti_sci_dev_id(dev);
101
102 return ti_sci->ops.dev_ops.put_device(ti_sci, idx);
103 }
104
ti_sci_pd_attach_dev(struct generic_pm_domain * domain,struct device * dev)105 static int ti_sci_pd_attach_dev(struct generic_pm_domain *domain,
106 struct device *dev)
107 {
108 struct device_node *np = dev->of_node;
109 struct of_phandle_args pd_args;
110 struct ti_sci_pm_domain *ti_sci_genpd = genpd_to_ti_sci_pd(domain);
111 const struct ti_sci_handle *ti_sci = ti_sci_genpd->ti_sci;
112 struct ti_sci_genpd_dev_data *sci_dev_data;
113 struct generic_pm_domain_data *genpd_data;
114 int idx, ret = 0;
115
116 ret = of_parse_phandle_with_args(np, "power-domains",
117 "#power-domain-cells", 0, &pd_args);
118 if (ret < 0)
119 return ret;
120
121 if (pd_args.args_count != 1)
122 return -EINVAL;
123
124 idx = pd_args.args[0];
125
126 /*
127 * Check the validity of the requested idx, if the index is not valid
128 * the PMMC will return a NAK here and we will not allocate it.
129 */
130 ret = ti_sci->ops.dev_ops.is_valid(ti_sci, idx);
131 if (ret)
132 return -EINVAL;
133
134 sci_dev_data = kzalloc(sizeof(*sci_dev_data), GFP_KERNEL);
135 if (!sci_dev_data)
136 return -ENOMEM;
137
138 sci_dev_data->idx = idx;
139
140 genpd_data = dev_gpd_data(dev);
141 genpd_data->data = sci_dev_data;
142
143 return 0;
144 }
145
ti_sci_pd_detach_dev(struct generic_pm_domain * domain,struct device * dev)146 static void ti_sci_pd_detach_dev(struct generic_pm_domain *domain,
147 struct device *dev)
148 {
149 struct generic_pm_domain_data *genpd_data = dev_gpd_data(dev);
150 struct ti_sci_genpd_dev_data *sci_dev_data = genpd_data->data;
151
152 kfree(sci_dev_data);
153 genpd_data->data = NULL;
154 }
155
156 static const struct of_device_id ti_sci_pm_domain_matches[] = {
157 { .compatible = "ti,sci-pm-domain", },
158 { },
159 };
160 MODULE_DEVICE_TABLE(of, ti_sci_pm_domain_matches);
161
ti_sci_pm_domain_probe(struct platform_device * pdev)162 static int ti_sci_pm_domain_probe(struct platform_device *pdev)
163 {
164 struct device *dev = &pdev->dev;
165 struct device_node *np = dev->of_node;
166 struct ti_sci_pm_domain *ti_sci_pd;
167 int ret;
168
169 ti_sci_pd = devm_kzalloc(dev, sizeof(*ti_sci_pd), GFP_KERNEL);
170 if (!ti_sci_pd)
171 return -ENOMEM;
172
173 ti_sci_pd->ti_sci = devm_ti_sci_get_handle(dev);
174 if (IS_ERR(ti_sci_pd->ti_sci))
175 return PTR_ERR(ti_sci_pd->ti_sci);
176
177 ti_sci_pd->dev = dev;
178
179 ti_sci_pd->pd.name = "ti_sci_pd";
180
181 ti_sci_pd->pd.attach_dev = ti_sci_pd_attach_dev;
182 ti_sci_pd->pd.detach_dev = ti_sci_pd_detach_dev;
183
184 ti_sci_pd->pd.dev_ops.start = ti_sci_dev_start;
185 ti_sci_pd->pd.dev_ops.stop = ti_sci_dev_stop;
186
187 pm_genpd_init(&ti_sci_pd->pd, NULL, true);
188
189 ret = of_genpd_add_provider_simple(np, &ti_sci_pd->pd);
190
191 return ret;
192 }
193
194 static struct platform_driver ti_sci_pm_domains_driver = {
195 .probe = ti_sci_pm_domain_probe,
196 .driver = {
197 .name = "ti_sci_pm_domains",
198 .of_match_table = ti_sci_pm_domain_matches,
199 },
200 };
201 module_platform_driver(ti_sci_pm_domains_driver);
202 MODULE_LICENSE("GPL v2");
203 MODULE_DESCRIPTION("TI System Control Interface (SCI) Power Domain driver");
204 MODULE_AUTHOR("Dave Gerlach");
205