blob: 0a9f498b97b15173ce2b890c17ed89c3dc57734d [file] [log] [blame]
Andreas Dannenberg24c6e912018-08-27 15:57:45 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments System Control Interface (TI SCI) power domain driver
4 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -05005 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
Andreas Dannenberg24c6e912018-08-27 15:57:45 +05306 * Andreas Dannenberg <dannenberg@ti.com>
7 *
8 * Loosely based on Linux kernel ti_sci_pm_domains.c...
9 */
10
Andreas Dannenberg24c6e912018-08-27 15:57:45 +053011#include <dm.h>
12#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Andreas Dannenberg24c6e912018-08-27 15:57:45 +053015#include <power-domain-uclass.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <linux/err.h>
Andreas Dannenberg24c6e912018-08-27 15:57:45 +053018#include <linux/soc/ti/ti_sci_protocol.h>
Lokesh Vutlae34f7a82019-06-07 19:24:46 +053019#include <dt-bindings/soc/ti,sci_pm_domain.h>
Andreas Dannenberg24c6e912018-08-27 15:57:45 +053020
21/**
22 * struct ti_sci_power_domain_data - pm domain controller information structure
23 * @sci: TI SCI handle used for communication with system controller
24 */
25struct ti_sci_power_domain_data {
26 const struct ti_sci_handle *sci;
27};
28
29static int ti_sci_power_domain_probe(struct udevice *dev)
30{
31 struct ti_sci_power_domain_data *data = dev_get_priv(dev);
32
33 debug("%s(dev=%p)\n", __func__, dev);
34
35 if (!data)
36 return -ENOMEM;
37
38 /* Store handle for communication with the system controller */
39 data->sci = ti_sci_get_handle(dev);
40 if (IS_ERR(data->sci))
41 return PTR_ERR(data->sci);
42
43 return 0;
44}
45
Andreas Dannenberg24c6e912018-08-27 15:57:45 +053046static int ti_sci_power_domain_on(struct power_domain *pd)
47{
48 struct ti_sci_power_domain_data *data = dev_get_priv(pd->dev);
49 const struct ti_sci_handle *sci = data->sci;
50 const struct ti_sci_dev_ops *dops = &sci->ops.dev_ops;
Lokesh Vutlae34f7a82019-06-07 19:24:46 +053051 u8 flags = (uintptr_t)pd->priv;
Andreas Dannenberg24c6e912018-08-27 15:57:45 +053052 int ret;
53
54 debug("%s(pd=%p)\n", __func__, pd);
55
Lokesh Vutlae34f7a82019-06-07 19:24:46 +053056 if (flags & TI_SCI_PD_EXCLUSIVE)
57 ret = dops->get_device_exclusive(sci, pd->id);
58 else
59 ret = dops->get_device(sci, pd->id);
60
Andreas Dannenberg24c6e912018-08-27 15:57:45 +053061 if (ret)
Nishanth Menon0401fb62019-08-01 19:08:24 +053062 dev_err(pd->dev, "%s: get_device(%lu) failed (%d)\n",
63 __func__, pd->id, ret);
Andreas Dannenberg24c6e912018-08-27 15:57:45 +053064
65 return ret;
66}
67
68static int ti_sci_power_domain_off(struct power_domain *pd)
69{
70 struct ti_sci_power_domain_data *data = dev_get_priv(pd->dev);
71 const struct ti_sci_handle *sci = data->sci;
72 const struct ti_sci_dev_ops *dops = &sci->ops.dev_ops;
73 int ret;
74
75 debug("%s(pd=%p)\n", __func__, pd);
76
77 ret = dops->put_device(sci, pd->id);
78 if (ret)
Nishanth Menon0401fb62019-08-01 19:08:24 +053079 dev_err(pd->dev, "%s: put_device(%lu) failed (%d)\n",
80 __func__, pd->id, ret);
Andreas Dannenberg24c6e912018-08-27 15:57:45 +053081
82 return ret;
83}
84
Lokesh Vutlae34f7a82019-06-07 19:24:46 +053085static int ti_sci_power_domain_of_xlate(struct power_domain *pd,
86 struct ofnode_phandle_args *args)
87{
88 u8 flags;
89
90 debug("%s(power_domain=%p)\n", __func__, pd);
91
92 if (args->args_count < 1) {
93 debug("Invalid args_count: %d\n", args->args_count);
94 return -EINVAL;
95 }
96
97 pd->id = args->args[0];
98 /* By default request for device exclusive */
99 flags = TI_SCI_PD_EXCLUSIVE;
100 if (args->args_count == 2)
101 flags = args->args[1] & TI_SCI_PD_EXCLUSIVE;
102 pd->priv = (void *)(uintptr_t)flags;
103
104 return 0;
105}
106
Andreas Dannenberg24c6e912018-08-27 15:57:45 +0530107static const struct udevice_id ti_sci_power_domain_of_match[] = {
108 { .compatible = "ti,sci-pm-domain" },
109 { /* sentinel */ }
110};
111
112static struct power_domain_ops ti_sci_power_domain_ops = {
Andreas Dannenberg24c6e912018-08-27 15:57:45 +0530113 .on = ti_sci_power_domain_on,
114 .off = ti_sci_power_domain_off,
Lokesh Vutlae34f7a82019-06-07 19:24:46 +0530115 .of_xlate = ti_sci_power_domain_of_xlate,
Andreas Dannenberg24c6e912018-08-27 15:57:45 +0530116};
117
118U_BOOT_DRIVER(ti_sci_pm_domains) = {
119 .name = "ti-sci-pm-domains",
120 .id = UCLASS_POWER_DOMAIN,
121 .of_match = ti_sci_power_domain_of_match,
122 .probe = ti_sci_power_domain_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700123 .priv_auto = sizeof(struct ti_sci_power_domain_data),
Andreas Dannenberg24c6e912018-08-27 15:57:45 +0530124 .ops = &ti_sci_power_domain_ops,
125};