aboutsummaryrefslogtreecommitdiff
path: root/drivers/bus/ti-sysc.c
blob: 778c0654f6a3bae3f324a1f8c20115ae32ce9748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// SPDX-License-Identifier: GPL-2.0+
/*
 * Texas Instruments sysc interconnect target driver
 *
 * Copyright (C) 2020 Dario Binacchi <dariobin@libero.it>
 */

#include <common.h>
#include <clk.h>
#include <dm.h>
#include <dm/device_compat.h>

enum ti_sysc_clocks {
	TI_SYSC_FCK,
	TI_SYSC_ICK,
	TI_SYSC_MAX_CLOCKS,
};

static const char *const clock_names[] = {"fck", "ick"};

struct ti_sysc_priv {
	int clocks_count;
	struct clk clocks[TI_SYSC_MAX_CLOCKS];
};

static const struct udevice_id ti_sysc_ids[] = {
	{.compatible = "ti,sysc-omap2"},
	{.compatible = "ti,sysc-omap4"},
	{.compatible = "ti,sysc-omap4-simple"},
	{.compatible = "ti,sysc-omap3430-sr"},
	{.compatible = "ti,sysc-omap3630-sr"},
	{.compatible = "ti,sysc-omap4-sr"},
	{.compatible = "ti,sysc-omap3-sham"},
	{.compatible = "ti,sysc-omap-aes"},
	{.compatible = "ti,sysc-mcasp"},
	{.compatible = "ti,sysc-usb-host-fs"},
	{}
};

static int ti_sysc_get_one_clock(struct udevice *dev, enum ti_sysc_clocks index)
{
	struct ti_sysc_priv *priv = dev_get_priv(dev);
	const char *name;
	int err;

	switch (index) {
	case TI_SYSC_FCK:
		break;
	case TI_SYSC_ICK:
		break;
	default:
		return -EINVAL;
	}

	name = clock_names[index];

	err = clk_get_by_name(dev, name, &priv->clocks[index]);
	if (err) {
		if (err == -ENODATA)
			return 0;

		dev_err(dev, "failed to get %s clock\n", name);
		return err;
	}

	return 0;
}

static int ti_sysc_put_clocks(struct udevice *dev)
{
	struct ti_sysc_priv *priv = dev_get_priv(dev);
	int err;

	err = clk_release_all(priv->clocks, priv->clocks_count);
	if (err)
		dev_err(dev, "failed to release all clocks\n");

	return err;
}

static int ti_sysc_get_clocks(struct udevice *dev)
{
	struct ti_sysc_priv *priv = dev_get_priv(dev);
	int i, err;

	for (i = 0; i < TI_SYSC_MAX_CLOCKS; i++) {
		err = ti_sysc_get_one_clock(dev, i);
		if (!err)
			priv->clocks_count++;
		else if (err != -ENOENT)
			return err;
	}

	return 0;
}

static int ti_sysc_child_post_remove(struct udevice *dev)
{
	struct ti_sysc_priv *priv = dev_get_priv(dev->parent);
	int i, err;

	for (i = 0; i < priv->clocks_count; i++) {
		err = clk_disable(&priv->clocks[i]);
		if (err) {
			dev_err(dev->parent, "failed to disable %s clock\n",
				clock_names[i]);
			return err;
		}
	}

	return 0;
}

static int ti_sysc_child_pre_probe(struct udevice *dev)
{
	struct ti_sysc_priv *priv = dev_get_priv(dev->parent);
	int i, err;

	for (i = 0; i < priv->clocks_count; i++) {
		err = clk_enable(&priv->clocks[i]);
		if (err) {
			dev_err(dev->parent, "failed to enable %s clock\n",
				clock_names[i]);
			return err;
		}
	}

	return 0;
}

static int ti_sysc_remove(struct udevice *dev)
{
	return ti_sysc_put_clocks(dev);
}

static int ti_sysc_probe(struct udevice *dev)
{
	int err;

	err = ti_sysc_get_clocks(dev);
	if (err)
		goto clocks_err;

	return 0;

clocks_err:
	ti_sysc_put_clocks(dev);
	return err;
}

U_BOOT_DRIVER(ti_sysc) = {
	.name = "ti_sysc",
	.id = UCLASS_SIMPLE_BUS,
	.of_match = ti_sysc_ids,
	.probe = ti_sysc_probe,
	.remove = ti_sysc_remove,
	.child_pre_probe = ti_sysc_child_pre_probe,
	.child_post_remove = ti_sysc_child_post_remove,
	.priv_auto = sizeof(struct ti_sysc_priv)
};