1 // SPDX-License-Identifier: GPL-2.0-only
3 * DRA7 ATL (Audio Tracking Logic) clock driver
5 * Copyright (C) 2013 Texas Instruments, Inc.
7 * Peter Ujfalusi <peter.ujfalusi@ti.com>
10 #include <linux/init.h>
11 #include <linux/clk.h>
12 #include <linux/clk-provider.h>
13 #include <linux/slab.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/clk/ti.h>
23 #define DRA7_ATL_INSTANCES 4
25 #define DRA7_ATL_PPMR_REG(id) (0x200 + (id * 0x80))
26 #define DRA7_ATL_BBSR_REG(id) (0x204 + (id * 0x80))
27 #define DRA7_ATL_ATLCR_REG(id) (0x208 + (id * 0x80))
28 #define DRA7_ATL_SWEN_REG(id) (0x210 + (id * 0x80))
29 #define DRA7_ATL_BWSMUX_REG(id) (0x214 + (id * 0x80))
30 #define DRA7_ATL_AWSMUX_REG(id) (0x218 + (id * 0x80))
31 #define DRA7_ATL_PCLKMUX_REG(id) (0x21c + (id * 0x80))
33 #define DRA7_ATL_SWEN BIT(0)
34 #define DRA7_ATL_DIVIDER_MASK (0x1f)
35 #define DRA7_ATL_PCLKMUX BIT(0)
36 struct dra7_atl_clock_info
;
38 struct dra7_atl_desc
{
41 struct dra7_atl_clock_info
*cinfo
;
44 bool probed
; /* the driver for the IP has been loaded */
45 bool valid
; /* configured */
47 u32 bws
; /* Baseband Word Select Mux */
48 u32 aws
; /* Audio Word Select Mux */
49 u32 divider
; /* Cached divider value */
52 struct dra7_atl_clock_info
{
56 struct dra7_atl_desc
*cdesc
;
59 #define to_atl_desc(_hw) container_of(_hw, struct dra7_atl_desc, hw)
61 static inline void atl_write(struct dra7_atl_clock_info
*cinfo
, u32 reg
,
64 __raw_writel(val
, cinfo
->iobase
+ reg
);
67 static inline int atl_read(struct dra7_atl_clock_info
*cinfo
, u32 reg
)
69 return __raw_readl(cinfo
->iobase
+ reg
);
72 static int atl_clk_enable(struct clk_hw
*hw
)
74 struct dra7_atl_desc
*cdesc
= to_atl_desc(hw
);
79 if (unlikely(!cdesc
->valid
))
80 dev_warn(cdesc
->cinfo
->dev
, "atl%d has not been configured\n",
82 pm_runtime_get_sync(cdesc
->cinfo
->dev
);
84 atl_write(cdesc
->cinfo
, DRA7_ATL_ATLCR_REG(cdesc
->id
),
86 atl_write(cdesc
->cinfo
, DRA7_ATL_SWEN_REG(cdesc
->id
), DRA7_ATL_SWEN
);
89 cdesc
->enabled
= true;
94 static void atl_clk_disable(struct clk_hw
*hw
)
96 struct dra7_atl_desc
*cdesc
= to_atl_desc(hw
);
101 atl_write(cdesc
->cinfo
, DRA7_ATL_SWEN_REG(cdesc
->id
), 0);
102 pm_runtime_put_sync(cdesc
->cinfo
->dev
);
105 cdesc
->enabled
= false;
108 static int atl_clk_is_enabled(struct clk_hw
*hw
)
110 struct dra7_atl_desc
*cdesc
= to_atl_desc(hw
);
112 return cdesc
->enabled
;
115 static unsigned long atl_clk_recalc_rate(struct clk_hw
*hw
,
116 unsigned long parent_rate
)
118 struct dra7_atl_desc
*cdesc
= to_atl_desc(hw
);
120 return parent_rate
/ cdesc
->divider
;
123 static long atl_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
124 unsigned long *parent_rate
)
128 divider
= (*parent_rate
+ rate
/ 2) / rate
;
129 if (divider
> DRA7_ATL_DIVIDER_MASK
+ 1)
130 divider
= DRA7_ATL_DIVIDER_MASK
+ 1;
132 return *parent_rate
/ divider
;
135 static int atl_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
136 unsigned long parent_rate
)
138 struct dra7_atl_desc
*cdesc
;
144 cdesc
= to_atl_desc(hw
);
145 divider
= ((parent_rate
+ rate
/ 2) / rate
) - 1;
146 if (divider
> DRA7_ATL_DIVIDER_MASK
)
147 divider
= DRA7_ATL_DIVIDER_MASK
;
149 cdesc
->divider
= divider
+ 1;
154 static const struct clk_ops atl_clk_ops
= {
155 .enable
= atl_clk_enable
,
156 .disable
= atl_clk_disable
,
157 .is_enabled
= atl_clk_is_enabled
,
158 .recalc_rate
= atl_clk_recalc_rate
,
159 .round_rate
= atl_clk_round_rate
,
160 .set_rate
= atl_clk_set_rate
,
163 static void __init
of_dra7_atl_clock_setup(struct device_node
*node
)
165 struct dra7_atl_desc
*clk_hw
= NULL
;
166 struct clk_parent_data pdata
= { .index
= 0 };
167 struct clk_init_data init
= { NULL
};
171 clk_hw
= kzalloc(sizeof(*clk_hw
), GFP_KERNEL
);
173 pr_err("%s: could not allocate dra7_atl_desc\n", __func__
);
177 clk_hw
->hw
.init
= &init
;
179 name
= ti_dt_clk_name(node
);
181 init
.ops
= &atl_clk_ops
;
182 init
.flags
= CLK_IGNORE_UNUSED
;
183 init
.num_parents
= of_clk_get_parent_count(node
);
185 if (init
.num_parents
!= 1) {
186 pr_err("%s: atl clock %pOFn must have 1 parent\n", __func__
,
191 init
.parent_data
= &pdata
;
192 clk
= of_ti_clk_register(node
, &clk_hw
->hw
, name
);
195 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
201 CLK_OF_DECLARE(dra7_atl_clock
, "ti,dra7-atl-clock", of_dra7_atl_clock_setup
);
203 static int of_dra7_atl_clk_probe(struct platform_device
*pdev
)
205 struct device_node
*node
= pdev
->dev
.of_node
;
206 struct dra7_atl_clock_info
*cinfo
;
213 cinfo
= devm_kzalloc(&pdev
->dev
, sizeof(*cinfo
), GFP_KERNEL
);
217 cinfo
->iobase
= of_iomap(node
, 0);
218 cinfo
->dev
= &pdev
->dev
;
219 pm_runtime_enable(cinfo
->dev
);
221 pm_runtime_get_sync(cinfo
->dev
);
222 atl_write(cinfo
, DRA7_ATL_PCLKMUX_REG(0), DRA7_ATL_PCLKMUX
);
224 for (i
= 0; i
< DRA7_ATL_INSTANCES
; i
++) {
225 struct device_node
*cfg_node
;
227 struct dra7_atl_desc
*cdesc
;
228 struct of_phandle_args clkspec
;
232 rc
= of_parse_phandle_with_args(node
, "ti,provided-clocks",
236 pr_err("%s: failed to lookup atl clock %d\n", __func__
,
242 clk
= of_clk_get_from_provider(&clkspec
);
243 of_node_put(clkspec
.np
);
245 pr_err("%s: failed to get atl clock %d from provider\n",
251 cdesc
= to_atl_desc(__clk_get_hw(clk
));
252 cdesc
->cinfo
= cinfo
;
255 /* Get configuration for the ATL instances */
256 snprintf(prop
, sizeof(prop
), "atl%u", i
);
257 cfg_node
= of_get_child_by_name(node
, prop
);
259 ret
= of_property_read_u32(cfg_node
, "bws",
261 ret
|= of_property_read_u32(cfg_node
, "aws",
265 atl_write(cinfo
, DRA7_ATL_BWSMUX_REG(i
),
267 atl_write(cinfo
, DRA7_ATL_AWSMUX_REG(i
),
270 of_node_put(cfg_node
);
273 cdesc
->probed
= true;
275 * Enable the clock if it has been asked prior to loading the
279 atl_clk_enable(__clk_get_hw(clk
));
283 pm_runtime_put_sync(cinfo
->dev
);
287 static const struct of_device_id of_dra7_atl_clk_match_tbl
[] = {
288 { .compatible
= "ti,dra7-atl", },
292 static struct platform_driver dra7_atl_clk_driver
= {
295 .suppress_bind_attrs
= true,
296 .of_match_table
= of_dra7_atl_clk_match_tbl
,
298 .probe
= of_dra7_atl_clk_probe
,
300 builtin_platform_driver(dra7_atl_clk_driver
);