2 * DRA7 ATL (Audio Tracking Logic) clock driver
4 * Copyright (C) 2013 Texas Instruments, Inc.
6 * Peter Ujfalusi <peter.ujfalusi@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/clk.h>
20 #include <linux/clk-provider.h>
21 #include <linux/slab.h>
24 #include <linux/of_address.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
28 #define DRA7_ATL_INSTANCES 4
30 #define DRA7_ATL_PPMR_REG(id) (0x200 + (id * 0x80))
31 #define DRA7_ATL_BBSR_REG(id) (0x204 + (id * 0x80))
32 #define DRA7_ATL_ATLCR_REG(id) (0x208 + (id * 0x80))
33 #define DRA7_ATL_SWEN_REG(id) (0x210 + (id * 0x80))
34 #define DRA7_ATL_BWSMUX_REG(id) (0x214 + (id * 0x80))
35 #define DRA7_ATL_AWSMUX_REG(id) (0x218 + (id * 0x80))
36 #define DRA7_ATL_PCLKMUX_REG(id) (0x21c + (id * 0x80))
38 #define DRA7_ATL_SWEN BIT(0)
39 #define DRA7_ATL_DIVIDER_MASK (0x1f)
40 #define DRA7_ATL_PCLKMUX BIT(0)
41 struct dra7_atl_clock_info
;
43 struct dra7_atl_desc
{
46 struct dra7_atl_clock_info
*cinfo
;
49 bool probed
; /* the driver for the IP has been loaded */
50 bool valid
; /* configured */
52 u32 bws
; /* Baseband Word Select Mux */
53 u32 aws
; /* Audio Word Select Mux */
54 u32 divider
; /* Cached divider value */
57 struct dra7_atl_clock_info
{
61 struct dra7_atl_desc
*cdesc
;
64 #define to_atl_desc(_hw) container_of(_hw, struct dra7_atl_desc, hw)
66 static inline void atl_write(struct dra7_atl_clock_info
*cinfo
, u32 reg
,
69 __raw_writel(val
, cinfo
->iobase
+ reg
);
72 static inline int atl_read(struct dra7_atl_clock_info
*cinfo
, u32 reg
)
74 return __raw_readl(cinfo
->iobase
+ reg
);
77 static int atl_clk_enable(struct clk_hw
*hw
)
79 struct dra7_atl_desc
*cdesc
= to_atl_desc(hw
);
84 if (unlikely(!cdesc
->valid
))
85 dev_warn(cdesc
->cinfo
->dev
, "atl%d has not been configured\n",
87 pm_runtime_get_sync(cdesc
->cinfo
->dev
);
89 atl_write(cdesc
->cinfo
, DRA7_ATL_ATLCR_REG(cdesc
->id
),
91 atl_write(cdesc
->cinfo
, DRA7_ATL_SWEN_REG(cdesc
->id
), DRA7_ATL_SWEN
);
94 cdesc
->enabled
= true;
99 static void atl_clk_disable(struct clk_hw
*hw
)
101 struct dra7_atl_desc
*cdesc
= to_atl_desc(hw
);
106 atl_write(cdesc
->cinfo
, DRA7_ATL_SWEN_REG(cdesc
->id
), 0);
107 pm_runtime_put_sync(cdesc
->cinfo
->dev
);
110 cdesc
->enabled
= false;
113 static int atl_clk_is_enabled(struct clk_hw
*hw
)
115 struct dra7_atl_desc
*cdesc
= to_atl_desc(hw
);
117 return cdesc
->enabled
;
120 static unsigned long atl_clk_recalc_rate(struct clk_hw
*hw
,
121 unsigned long parent_rate
)
123 struct dra7_atl_desc
*cdesc
= to_atl_desc(hw
);
125 return parent_rate
/ cdesc
->divider
;
128 static long atl_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
129 unsigned long *parent_rate
)
133 divider
= (*parent_rate
+ rate
/ 2) / rate
;
134 if (divider
> DRA7_ATL_DIVIDER_MASK
+ 1)
135 divider
= DRA7_ATL_DIVIDER_MASK
+ 1;
137 return *parent_rate
/ divider
;
140 static int atl_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
141 unsigned long parent_rate
)
143 struct dra7_atl_desc
*cdesc
;
149 cdesc
= to_atl_desc(hw
);
150 divider
= ((parent_rate
+ rate
/ 2) / rate
) - 1;
151 if (divider
> DRA7_ATL_DIVIDER_MASK
)
152 divider
= DRA7_ATL_DIVIDER_MASK
;
154 cdesc
->divider
= divider
+ 1;
159 static const struct clk_ops atl_clk_ops
= {
160 .enable
= atl_clk_enable
,
161 .disable
= atl_clk_disable
,
162 .is_enabled
= atl_clk_is_enabled
,
163 .recalc_rate
= atl_clk_recalc_rate
,
164 .round_rate
= atl_clk_round_rate
,
165 .set_rate
= atl_clk_set_rate
,
168 static void __init
of_dra7_atl_clock_setup(struct device_node
*node
)
170 struct dra7_atl_desc
*clk_hw
= NULL
;
171 struct clk_init_data init
= { NULL
};
172 const char **parent_names
= NULL
;
175 clk_hw
= kzalloc(sizeof(*clk_hw
), GFP_KERNEL
);
177 pr_err("%s: could not allocate dra7_atl_desc\n", __func__
);
181 clk_hw
->hw
.init
= &init
;
183 init
.name
= node
->name
;
184 init
.ops
= &atl_clk_ops
;
185 init
.flags
= CLK_IGNORE_UNUSED
;
186 init
.num_parents
= of_clk_get_parent_count(node
);
188 if (init
.num_parents
!= 1) {
189 pr_err("%s: atl clock %s must have 1 parent\n", __func__
,
194 parent_names
= kzalloc(sizeof(char *), GFP_KERNEL
);
199 parent_names
[0] = of_clk_get_parent_name(node
, 0);
201 init
.parent_names
= parent_names
;
203 clk
= clk_register(NULL
, &clk_hw
->hw
);
206 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
214 CLK_OF_DECLARE(dra7_atl_clock
, "ti,dra7-atl-clock", of_dra7_atl_clock_setup
);
216 static int of_dra7_atl_clk_probe(struct platform_device
*pdev
)
218 struct device_node
*node
= pdev
->dev
.of_node
;
219 struct dra7_atl_clock_info
*cinfo
;
226 cinfo
= devm_kzalloc(&pdev
->dev
, sizeof(*cinfo
), GFP_KERNEL
);
230 cinfo
->iobase
= of_iomap(node
, 0);
231 cinfo
->dev
= &pdev
->dev
;
232 pm_runtime_enable(cinfo
->dev
);
233 pm_runtime_irq_safe(cinfo
->dev
);
235 pm_runtime_get_sync(cinfo
->dev
);
236 atl_write(cinfo
, DRA7_ATL_PCLKMUX_REG(0), DRA7_ATL_PCLKMUX
);
238 for (i
= 0; i
< DRA7_ATL_INSTANCES
; i
++) {
239 struct device_node
*cfg_node
;
241 struct dra7_atl_desc
*cdesc
;
242 struct of_phandle_args clkspec
;
246 rc
= of_parse_phandle_with_args(node
, "ti,provided-clocks",
250 pr_err("%s: failed to lookup atl clock %d\n", __func__
,
255 clk
= of_clk_get_from_provider(&clkspec
);
257 pr_err("%s: failed to get atl clock %d from provider\n",
262 cdesc
= to_atl_desc(__clk_get_hw(clk
));
263 cdesc
->cinfo
= cinfo
;
266 /* Get configuration for the ATL instances */
267 snprintf(prop
, sizeof(prop
), "atl%u", i
);
269 cfg_node
= of_find_node_by_name(node
, prop
);
271 ret
= of_property_read_u32(cfg_node
, "bws",
273 ret
|= of_property_read_u32(cfg_node
, "aws",
277 atl_write(cinfo
, DRA7_ATL_BWSMUX_REG(i
),
279 atl_write(cinfo
, DRA7_ATL_AWSMUX_REG(i
),
282 of_node_put(cfg_node
);
285 cdesc
->probed
= true;
287 * Enable the clock if it has been asked prior to loading the
291 atl_clk_enable(__clk_get_hw(clk
));
293 pm_runtime_put_sync(cinfo
->dev
);
298 static int of_dra7_atl_clk_remove(struct platform_device
*pdev
)
300 pm_runtime_disable(&pdev
->dev
);
305 static const struct of_device_id of_dra7_atl_clk_match_tbl
[] = {
306 { .compatible
= "ti,dra7-atl", },
309 MODULE_DEVICE_TABLE(of
, of_dra7_atl_clk_match_tbl
);
311 static struct platform_driver dra7_atl_clk_driver
= {
314 .of_match_table
= of_dra7_atl_clk_match_tbl
,
316 .probe
= of_dra7_atl_clk_probe
,
317 .remove
= of_dra7_atl_clk_remove
,
320 module_platform_driver(dra7_atl_clk_driver
);
322 MODULE_DESCRIPTION("Clock driver for DRA7 Audio Tracking Logic");
323 MODULE_ALIAS("platform:dra7-atl-clock");
324 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
325 MODULE_LICENSE("GPL v2");