1104862Sru// SPDX-License-Identifier: GPL-2.0-or-later
2104862Sru/*
3104862Sru * Copyright (C) 2016 Socionext Inc.
4104862Sru *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5114402Sru */
6114402Sru
7114402Sru#include <linux/clk-provider.h>
8114402Sru#include <linux/init.h>
9114402Sru#include <linux/mfd/syscon.h>
10114402Sru#include <linux/of.h>
11114402Sru#include <linux/platform_device.h>
12114402Sru
13114402Sru#include "clk-uniphier.h"
14114402Sru
15114402Srustatic struct clk_hw *uniphier_clk_register(struct device *dev,
16114402Sru					    struct regmap *regmap,
17114402Sru					const struct uniphier_clk_data *data)
18114402Sru{
19114402Sru	switch (data->type) {
20114402Sru	case UNIPHIER_CLK_TYPE_CPUGEAR:
21114402Sru		return uniphier_clk_register_cpugear(dev, regmap, data->name,
22114402Sru						     &data->data.cpugear);
23114402Sru	case UNIPHIER_CLK_TYPE_FIXED_FACTOR:
24114402Sru		return uniphier_clk_register_fixed_factor(dev, data->name,
25114402Sru							  &data->data.factor);
26114402Sru	case UNIPHIER_CLK_TYPE_FIXED_RATE:
27114402Sru		return uniphier_clk_register_fixed_rate(dev, data->name,
28114402Sru							&data->data.rate);
29114402Sru	case UNIPHIER_CLK_TYPE_GATE:
30114402Sru		return uniphier_clk_register_gate(dev, regmap, data->name,
31114402Sru						  &data->data.gate);
32114402Sru	case UNIPHIER_CLK_TYPE_MUX:
33114402Sru		return uniphier_clk_register_mux(dev, regmap, data->name,
34114402Sru						 &data->data.mux);
35114402Sru	default:
36114402Sru		dev_err(dev, "unsupported clock type\n");
37114402Sru		return ERR_PTR(-EINVAL);
38114402Sru	}
39114402Sru}
40114402Sru
41114402Srustatic int uniphier_clk_probe(struct platform_device *pdev)
42114402Sru{
43114402Sru	struct device *dev = &pdev->dev;
44114402Sru	struct clk_hw_onecell_data *hw_data;
45114402Sru	const struct uniphier_clk_data *p, *data;
46114402Sru	struct regmap *regmap;
47114402Sru	struct device_node *parent;
48114402Sru	int clk_num = 0;
49114402Sru
50114402Sru	data = of_device_get_match_data(dev);
51114402Sru	if (WARN_ON(!data))
52114402Sru		return -EINVAL;
53114402Sru
54114402Sru	parent = of_get_parent(dev->of_node); /* parent should be syscon node */
55114402Sru	regmap = syscon_node_to_regmap(parent);
56114402Sru	of_node_put(parent);
57114402Sru	if (IS_ERR(regmap)) {
58114402Sru		dev_err(dev, "failed to get regmap (error %ld)\n",
59114402Sru			PTR_ERR(regmap));
60114402Sru		return PTR_ERR(regmap);
61114402Sru	}
62114402Sru
63114402Sru	for (p = data; p->name; p++)
64114402Sru		clk_num = max(clk_num, p->idx + 1);
65114402Sru
66114402Sru	hw_data = devm_kzalloc(dev, struct_size(hw_data, hws, clk_num),
67114402Sru			GFP_KERNEL);
68114402Sru	if (!hw_data)
69114402Sru		return -ENOMEM;
70114402Sru
71114402Sru	hw_data->num = clk_num;
72114402Sru
73114402Sru	/* avoid returning NULL for unused idx */
74114402Sru	while (--clk_num >= 0)
75114402Sru		hw_data->hws[clk_num] = ERR_PTR(-EINVAL);
76104862Sru
77104862Sru	for (p = data; p->name; p++) {
78114402Sru		struct clk_hw *hw;
79114402Sru
80114402Sru		dev_dbg(dev, "register %s (index=%d)\n", p->name, p->idx);
81114402Sru		hw = uniphier_clk_register(dev, regmap, p);
82114402Sru		if (WARN(IS_ERR(hw), "failed to register %s", p->name))
83114402Sru			continue;
84114402Sru
85114402Sru		if (p->idx >= 0)
86114402Sru			hw_data->hws[p->idx] = hw;
87114402Sru	}
88114402Sru
89114402Sru	return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
90114402Sru					   hw_data);
91114402Sru}
92114402Sru
93114402Srustatic const struct of_device_id uniphier_clk_match[] = {
94114402Sru	/* System clock */
95114402Sru	{
96114402Sru		.compatible = "socionext,uniphier-ld4-clock",
97114402Sru		.data = uniphier_ld4_sys_clk_data,
98114402Sru	},
99114402Sru	{
100114402Sru		.compatible = "socionext,uniphier-pro4-clock",
101104862Sru		.data = uniphier_pro4_sys_clk_data,
102	},
103	{
104		.compatible = "socionext,uniphier-sld8-clock",
105		.data = uniphier_sld8_sys_clk_data,
106	},
107	{
108		.compatible = "socionext,uniphier-pro5-clock",
109		.data = uniphier_pro5_sys_clk_data,
110	},
111	{
112		.compatible = "socionext,uniphier-pxs2-clock",
113		.data = uniphier_pxs2_sys_clk_data,
114	},
115	{
116		.compatible = "socionext,uniphier-ld11-clock",
117		.data = uniphier_ld11_sys_clk_data,
118	},
119	{
120		.compatible = "socionext,uniphier-ld20-clock",
121		.data = uniphier_ld20_sys_clk_data,
122	},
123	{
124		.compatible = "socionext,uniphier-pxs3-clock",
125		.data = uniphier_pxs3_sys_clk_data,
126	},
127	{
128		.compatible = "socionext,uniphier-nx1-clock",
129		.data = uniphier_nx1_sys_clk_data,
130	},
131	/* Media I/O clock, SD clock */
132	{
133		.compatible = "socionext,uniphier-ld4-mio-clock",
134		.data = uniphier_ld4_mio_clk_data,
135	},
136	{
137		.compatible = "socionext,uniphier-pro4-mio-clock",
138		.data = uniphier_ld4_mio_clk_data,
139	},
140	{
141		.compatible = "socionext,uniphier-sld8-mio-clock",
142		.data = uniphier_ld4_mio_clk_data,
143	},
144	{
145		.compatible = "socionext,uniphier-pro5-sd-clock",
146		.data = uniphier_pro5_sd_clk_data,
147	},
148	{
149		.compatible = "socionext,uniphier-pxs2-sd-clock",
150		.data = uniphier_pro5_sd_clk_data,
151	},
152	{
153		.compatible = "socionext,uniphier-ld11-mio-clock",
154		.data = uniphier_ld4_mio_clk_data,
155	},
156	{
157		.compatible = "socionext,uniphier-ld20-sd-clock",
158		.data = uniphier_pro5_sd_clk_data,
159	},
160	{
161		.compatible = "socionext,uniphier-pxs3-sd-clock",
162		.data = uniphier_pro5_sd_clk_data,
163	},
164	{
165		.compatible = "socionext,uniphier-nx1-sd-clock",
166		.data = uniphier_pro5_sd_clk_data,
167	},
168	/* Peripheral clock */
169	{
170		.compatible = "socionext,uniphier-ld4-peri-clock",
171		.data = uniphier_ld4_peri_clk_data,
172	},
173	{
174		.compatible = "socionext,uniphier-pro4-peri-clock",
175		.data = uniphier_pro4_peri_clk_data,
176	},
177	{
178		.compatible = "socionext,uniphier-sld8-peri-clock",
179		.data = uniphier_ld4_peri_clk_data,
180	},
181	{
182		.compatible = "socionext,uniphier-pro5-peri-clock",
183		.data = uniphier_pro4_peri_clk_data,
184	},
185	{
186		.compatible = "socionext,uniphier-pxs2-peri-clock",
187		.data = uniphier_pro4_peri_clk_data,
188	},
189	{
190		.compatible = "socionext,uniphier-ld11-peri-clock",
191		.data = uniphier_pro4_peri_clk_data,
192	},
193	{
194		.compatible = "socionext,uniphier-ld20-peri-clock",
195		.data = uniphier_pro4_peri_clk_data,
196	},
197	{
198		.compatible = "socionext,uniphier-pxs3-peri-clock",
199		.data = uniphier_pro4_peri_clk_data,
200	},
201	{
202		.compatible = "socionext,uniphier-nx1-peri-clock",
203		.data = uniphier_pro4_peri_clk_data,
204	},
205	/* SoC-glue clock */
206	{
207		.compatible = "socionext,uniphier-pro4-sg-clock",
208		.data = uniphier_pro4_sg_clk_data,
209	},
210	{ /* sentinel */ }
211};
212
213static struct platform_driver uniphier_clk_driver = {
214	.probe = uniphier_clk_probe,
215	.driver = {
216		.name = "uniphier-clk",
217		.of_match_table = uniphier_clk_match,
218	},
219};
220builtin_platform_driver(uniphier_clk_driver);
221