blob: f61506c530891e23a32633d361aa648cabd79a2d [file] [log] [blame]
Neil Armstrongf8c11f72016-08-18 12:08:46 +02001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright (c) 2016 BayLibre, SAS.
8 * Author: Neil Armstrong <narmstrong@baylibre.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 * The full GNU General Public License is included in this distribution
22 * in the file called COPYING.
23 *
24 * BSD LICENSE
25 *
26 * Copyright (c) 2016 BayLibre, SAS.
27 * Author: Neil Armstrong <narmstrong@baylibre.com>
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55#include <linux/clk-provider.h>
56#include <linux/of_address.h>
57#include <linux/platform_device.h>
58#include <linux/reset-controller.h>
Neil Armstrongffb13e32017-08-01 13:56:57 +020059#include <linux/mfd/syscon.h>
60#include <linux/regmap.h>
Neil Armstrongf8c11f72016-08-18 12:08:46 +020061#include <linux/init.h>
62#include <dt-bindings/clock/gxbb-aoclkc.h>
63#include <dt-bindings/reset/gxbb-aoclkc.h>
Neil Armstrongffb13e32017-08-01 13:56:57 +020064#include "gxbb-aoclk.h"
Neil Armstrongf8c11f72016-08-18 12:08:46 +020065
66static DEFINE_SPINLOCK(gxbb_aoclk_lock);
67
68struct gxbb_aoclk_reset_controller {
69 struct reset_controller_dev reset;
70 unsigned int *data;
Neil Armstrongffb13e32017-08-01 13:56:57 +020071 struct regmap *regmap;
Neil Armstrongf8c11f72016-08-18 12:08:46 +020072};
73
74static int gxbb_aoclk_do_reset(struct reset_controller_dev *rcdev,
75 unsigned long id)
76{
77 struct gxbb_aoclk_reset_controller *reset =
78 container_of(rcdev, struct gxbb_aoclk_reset_controller, reset);
79
Neil Armstrongffb13e32017-08-01 13:56:57 +020080 return regmap_write(reset->regmap, AO_RTI_GEN_CNTL_REG0,
81 BIT(reset->data[id]));
Neil Armstrongf8c11f72016-08-18 12:08:46 +020082}
83
84static const struct reset_control_ops gxbb_aoclk_reset_ops = {
85 .reset = gxbb_aoclk_do_reset,
86};
87
88#define GXBB_AO_GATE(_name, _bit) \
Neil Armstrongffb13e32017-08-01 13:56:57 +020089static struct aoclk_gate_regmap _name##_ao = { \
Neil Armstrongf8c11f72016-08-18 12:08:46 +020090 .bit_idx = (_bit), \
91 .lock = &gxbb_aoclk_lock, \
92 .hw.init = &(struct clk_init_data) { \
93 .name = #_name "_ao", \
Neil Armstrongffb13e32017-08-01 13:56:57 +020094 .ops = &meson_aoclk_gate_regmap_ops, \
Neil Armstrongf8c11f72016-08-18 12:08:46 +020095 .parent_names = (const char *[]){ "clk81" }, \
96 .num_parents = 1, \
97 .flags = (CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED), \
98 }, \
99}
100
101GXBB_AO_GATE(remote, 0);
102GXBB_AO_GATE(i2c_master, 1);
103GXBB_AO_GATE(i2c_slave, 2);
104GXBB_AO_GATE(uart1, 3);
105GXBB_AO_GATE(uart2, 5);
106GXBB_AO_GATE(ir_blaster, 6);
107
108static unsigned int gxbb_aoclk_reset[] = {
109 [RESET_AO_REMOTE] = 16,
110 [RESET_AO_I2C_MASTER] = 18,
111 [RESET_AO_I2C_SLAVE] = 19,
112 [RESET_AO_UART1] = 17,
113 [RESET_AO_UART2] = 22,
114 [RESET_AO_IR_BLASTER] = 23,
115};
116
Neil Armstrongffb13e32017-08-01 13:56:57 +0200117static struct aoclk_gate_regmap *gxbb_aoclk_gate[] = {
Neil Armstrongf8c11f72016-08-18 12:08:46 +0200118 [CLKID_AO_REMOTE] = &remote_ao,
119 [CLKID_AO_I2C_MASTER] = &i2c_master_ao,
120 [CLKID_AO_I2C_SLAVE] = &i2c_slave_ao,
121 [CLKID_AO_UART1] = &uart1_ao,
122 [CLKID_AO_UART2] = &uart2_ao,
123 [CLKID_AO_IR_BLASTER] = &ir_blaster_ao,
124};
125
126static struct clk_hw_onecell_data gxbb_aoclk_onecell_data = {
127 .hws = {
128 [CLKID_AO_REMOTE] = &remote_ao.hw,
129 [CLKID_AO_I2C_MASTER] = &i2c_master_ao.hw,
130 [CLKID_AO_I2C_SLAVE] = &i2c_slave_ao.hw,
131 [CLKID_AO_UART1] = &uart1_ao.hw,
132 [CLKID_AO_UART2] = &uart2_ao.hw,
133 [CLKID_AO_IR_BLASTER] = &ir_blaster_ao.hw,
134 },
135 .num = ARRAY_SIZE(gxbb_aoclk_gate),
136};
137
138static int gxbb_aoclkc_probe(struct platform_device *pdev)
139{
Neil Armstrongf8c11f72016-08-18 12:08:46 +0200140 struct gxbb_aoclk_reset_controller *rstc;
Neil Armstrongffb13e32017-08-01 13:56:57 +0200141 struct device *dev = &pdev->dev;
142 struct regmap *regmap;
143 int ret, clkid;
Neil Armstrongf8c11f72016-08-18 12:08:46 +0200144
Wei Yongjun718cc4b2016-08-22 16:08:21 +0000145 rstc = devm_kzalloc(dev, sizeof(*rstc), GFP_KERNEL);
Neil Armstrongf8c11f72016-08-18 12:08:46 +0200146 if (!rstc)
147 return -ENOMEM;
148
Neil Armstrongffb13e32017-08-01 13:56:57 +0200149 regmap = syscon_node_to_regmap(of_get_parent(dev->of_node));
150 if (IS_ERR(regmap)) {
151 dev_err(dev, "failed to get regmap\n");
152 return -ENODEV;
153 }
Neil Armstrongf8c11f72016-08-18 12:08:46 +0200154
155 /* Reset Controller */
Neil Armstrongffb13e32017-08-01 13:56:57 +0200156 rstc->regmap = regmap;
Neil Armstrongf8c11f72016-08-18 12:08:46 +0200157 rstc->data = gxbb_aoclk_reset;
158 rstc->reset.ops = &gxbb_aoclk_reset_ops;
159 rstc->reset.nr_resets = ARRAY_SIZE(gxbb_aoclk_reset);
160 rstc->reset.of_node = dev->of_node;
161 ret = devm_reset_controller_register(dev, &rstc->reset);
162
163 /*
Neil Armstrongffb13e32017-08-01 13:56:57 +0200164 * Populate regmap and register all clks
Neil Armstrongf8c11f72016-08-18 12:08:46 +0200165 */
Neil Armstrongffb13e32017-08-01 13:56:57 +0200166 for (clkid = 0; clkid < ARRAY_SIZE(gxbb_aoclk_gate); clkid++) {
167 gxbb_aoclk_gate[clkid]->regmap = regmap;
Neil Armstrongf8c11f72016-08-18 12:08:46 +0200168
169 ret = devm_clk_hw_register(dev,
170 gxbb_aoclk_onecell_data.hws[clkid]);
171 if (ret)
172 return ret;
173 }
174
175 return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
176 &gxbb_aoclk_onecell_data);
177}
178
179static const struct of_device_id gxbb_aoclkc_match_table[] = {
Neil Armstrongffb13e32017-08-01 13:56:57 +0200180 { .compatible = "amlogic,meson-gx-aoclkc" },
Neil Armstrongf8c11f72016-08-18 12:08:46 +0200181 { }
182};
183
184static struct platform_driver gxbb_aoclkc_driver = {
185 .probe = gxbb_aoclkc_probe,
186 .driver = {
187 .name = "gxbb-aoclkc",
188 .of_match_table = gxbb_aoclkc_match_table,
189 },
190};
191builtin_platform_driver(gxbb_aoclkc_driver);