1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * DRA7 ATL (Audio Tracking Logic) clock driver
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2013 Texas Instruments, Inc.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Peter Ujfalusi <peter.ujfalusi@ti.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
9*4882a593Smuzhiyun * it under the terms of the GNU General Public License version 2 as
10*4882a593Smuzhiyun * published by the Free Software Foundation.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13*4882a593Smuzhiyun * kind, whether express or implied; without even the implied warranty
14*4882a593Smuzhiyun * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*4882a593Smuzhiyun * GNU General Public License for more details.
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/init.h>
19*4882a593Smuzhiyun #include <linux/clk.h>
20*4882a593Smuzhiyun #include <linux/clk-provider.h>
21*4882a593Smuzhiyun #include <linux/slab.h>
22*4882a593Smuzhiyun #include <linux/io.h>
23*4882a593Smuzhiyun #include <linux/of.h>
24*4882a593Smuzhiyun #include <linux/of_address.h>
25*4882a593Smuzhiyun #include <linux/platform_device.h>
26*4882a593Smuzhiyun #include <linux/pm_runtime.h>
27*4882a593Smuzhiyun #include <linux/clk/ti.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "clock.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define DRA7_ATL_INSTANCES 4
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define DRA7_ATL_PPMR_REG(id) (0x200 + (id * 0x80))
34*4882a593Smuzhiyun #define DRA7_ATL_BBSR_REG(id) (0x204 + (id * 0x80))
35*4882a593Smuzhiyun #define DRA7_ATL_ATLCR_REG(id) (0x208 + (id * 0x80))
36*4882a593Smuzhiyun #define DRA7_ATL_SWEN_REG(id) (0x210 + (id * 0x80))
37*4882a593Smuzhiyun #define DRA7_ATL_BWSMUX_REG(id) (0x214 + (id * 0x80))
38*4882a593Smuzhiyun #define DRA7_ATL_AWSMUX_REG(id) (0x218 + (id * 0x80))
39*4882a593Smuzhiyun #define DRA7_ATL_PCLKMUX_REG(id) (0x21c + (id * 0x80))
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #define DRA7_ATL_SWEN BIT(0)
42*4882a593Smuzhiyun #define DRA7_ATL_DIVIDER_MASK (0x1f)
43*4882a593Smuzhiyun #define DRA7_ATL_PCLKMUX BIT(0)
44*4882a593Smuzhiyun struct dra7_atl_clock_info;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun struct dra7_atl_desc {
47*4882a593Smuzhiyun struct clk *clk;
48*4882a593Smuzhiyun struct clk_hw hw;
49*4882a593Smuzhiyun struct dra7_atl_clock_info *cinfo;
50*4882a593Smuzhiyun int id;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun bool probed; /* the driver for the IP has been loaded */
53*4882a593Smuzhiyun bool valid; /* configured */
54*4882a593Smuzhiyun bool enabled;
55*4882a593Smuzhiyun u32 bws; /* Baseband Word Select Mux */
56*4882a593Smuzhiyun u32 aws; /* Audio Word Select Mux */
57*4882a593Smuzhiyun u32 divider; /* Cached divider value */
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun struct dra7_atl_clock_info {
61*4882a593Smuzhiyun struct device *dev;
62*4882a593Smuzhiyun void __iomem *iobase;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun struct dra7_atl_desc *cdesc;
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #define to_atl_desc(_hw) container_of(_hw, struct dra7_atl_desc, hw)
68*4882a593Smuzhiyun
atl_write(struct dra7_atl_clock_info * cinfo,u32 reg,u32 val)69*4882a593Smuzhiyun static inline void atl_write(struct dra7_atl_clock_info *cinfo, u32 reg,
70*4882a593Smuzhiyun u32 val)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun __raw_writel(val, cinfo->iobase + reg);
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun
atl_read(struct dra7_atl_clock_info * cinfo,u32 reg)75*4882a593Smuzhiyun static inline int atl_read(struct dra7_atl_clock_info *cinfo, u32 reg)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun return __raw_readl(cinfo->iobase + reg);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
atl_clk_enable(struct clk_hw * hw)80*4882a593Smuzhiyun static int atl_clk_enable(struct clk_hw *hw)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun struct dra7_atl_desc *cdesc = to_atl_desc(hw);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun if (!cdesc->probed)
85*4882a593Smuzhiyun goto out;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun if (unlikely(!cdesc->valid))
88*4882a593Smuzhiyun dev_warn(cdesc->cinfo->dev, "atl%d has not been configured\n",
89*4882a593Smuzhiyun cdesc->id);
90*4882a593Smuzhiyun pm_runtime_get_sync(cdesc->cinfo->dev);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun atl_write(cdesc->cinfo, DRA7_ATL_ATLCR_REG(cdesc->id),
93*4882a593Smuzhiyun cdesc->divider - 1);
94*4882a593Smuzhiyun atl_write(cdesc->cinfo, DRA7_ATL_SWEN_REG(cdesc->id), DRA7_ATL_SWEN);
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun out:
97*4882a593Smuzhiyun cdesc->enabled = true;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun return 0;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
atl_clk_disable(struct clk_hw * hw)102*4882a593Smuzhiyun static void atl_clk_disable(struct clk_hw *hw)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun struct dra7_atl_desc *cdesc = to_atl_desc(hw);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun if (!cdesc->probed)
107*4882a593Smuzhiyun goto out;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun atl_write(cdesc->cinfo, DRA7_ATL_SWEN_REG(cdesc->id), 0);
110*4882a593Smuzhiyun pm_runtime_put_sync(cdesc->cinfo->dev);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun out:
113*4882a593Smuzhiyun cdesc->enabled = false;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
atl_clk_is_enabled(struct clk_hw * hw)116*4882a593Smuzhiyun static int atl_clk_is_enabled(struct clk_hw *hw)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun struct dra7_atl_desc *cdesc = to_atl_desc(hw);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun return cdesc->enabled;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
atl_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)123*4882a593Smuzhiyun static unsigned long atl_clk_recalc_rate(struct clk_hw *hw,
124*4882a593Smuzhiyun unsigned long parent_rate)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun struct dra7_atl_desc *cdesc = to_atl_desc(hw);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun return parent_rate / cdesc->divider;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
atl_clk_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)131*4882a593Smuzhiyun static long atl_clk_round_rate(struct clk_hw *hw, unsigned long rate,
132*4882a593Smuzhiyun unsigned long *parent_rate)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun unsigned divider;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun divider = (*parent_rate + rate / 2) / rate;
137*4882a593Smuzhiyun if (divider > DRA7_ATL_DIVIDER_MASK + 1)
138*4882a593Smuzhiyun divider = DRA7_ATL_DIVIDER_MASK + 1;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun return *parent_rate / divider;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
atl_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)143*4882a593Smuzhiyun static int atl_clk_set_rate(struct clk_hw *hw, unsigned long rate,
144*4882a593Smuzhiyun unsigned long parent_rate)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun struct dra7_atl_desc *cdesc;
147*4882a593Smuzhiyun u32 divider;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun if (!hw || !rate)
150*4882a593Smuzhiyun return -EINVAL;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun cdesc = to_atl_desc(hw);
153*4882a593Smuzhiyun divider = ((parent_rate + rate / 2) / rate) - 1;
154*4882a593Smuzhiyun if (divider > DRA7_ATL_DIVIDER_MASK)
155*4882a593Smuzhiyun divider = DRA7_ATL_DIVIDER_MASK;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun cdesc->divider = divider + 1;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun return 0;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun static const struct clk_ops atl_clk_ops = {
163*4882a593Smuzhiyun .enable = atl_clk_enable,
164*4882a593Smuzhiyun .disable = atl_clk_disable,
165*4882a593Smuzhiyun .is_enabled = atl_clk_is_enabled,
166*4882a593Smuzhiyun .recalc_rate = atl_clk_recalc_rate,
167*4882a593Smuzhiyun .round_rate = atl_clk_round_rate,
168*4882a593Smuzhiyun .set_rate = atl_clk_set_rate,
169*4882a593Smuzhiyun };
170*4882a593Smuzhiyun
of_dra7_atl_clock_setup(struct device_node * node)171*4882a593Smuzhiyun static void __init of_dra7_atl_clock_setup(struct device_node *node)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun struct dra7_atl_desc *clk_hw = NULL;
174*4882a593Smuzhiyun struct clk_init_data init = { NULL };
175*4882a593Smuzhiyun const char **parent_names = NULL;
176*4882a593Smuzhiyun struct clk *clk;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
179*4882a593Smuzhiyun if (!clk_hw) {
180*4882a593Smuzhiyun pr_err("%s: could not allocate dra7_atl_desc\n", __func__);
181*4882a593Smuzhiyun return;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun clk_hw->hw.init = &init;
185*4882a593Smuzhiyun clk_hw->divider = 1;
186*4882a593Smuzhiyun init.name = node->name;
187*4882a593Smuzhiyun init.ops = &atl_clk_ops;
188*4882a593Smuzhiyun init.flags = CLK_IGNORE_UNUSED;
189*4882a593Smuzhiyun init.num_parents = of_clk_get_parent_count(node);
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun if (init.num_parents != 1) {
192*4882a593Smuzhiyun pr_err("%s: atl clock %pOFn must have 1 parent\n", __func__,
193*4882a593Smuzhiyun node);
194*4882a593Smuzhiyun goto cleanup;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun parent_names = kzalloc(sizeof(char *), GFP_KERNEL);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun if (!parent_names)
200*4882a593Smuzhiyun goto cleanup;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun parent_names[0] = of_clk_get_parent_name(node, 0);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun init.parent_names = parent_names;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun clk = ti_clk_register(NULL, &clk_hw->hw, node->name);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun if (!IS_ERR(clk)) {
209*4882a593Smuzhiyun of_clk_add_provider(node, of_clk_src_simple_get, clk);
210*4882a593Smuzhiyun kfree(parent_names);
211*4882a593Smuzhiyun return;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun cleanup:
214*4882a593Smuzhiyun kfree(parent_names);
215*4882a593Smuzhiyun kfree(clk_hw);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun CLK_OF_DECLARE(dra7_atl_clock, "ti,dra7-atl-clock", of_dra7_atl_clock_setup);
218*4882a593Smuzhiyun
of_dra7_atl_clk_probe(struct platform_device * pdev)219*4882a593Smuzhiyun static int of_dra7_atl_clk_probe(struct platform_device *pdev)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun struct device_node *node = pdev->dev.of_node;
222*4882a593Smuzhiyun struct dra7_atl_clock_info *cinfo;
223*4882a593Smuzhiyun int i;
224*4882a593Smuzhiyun int ret = 0;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun if (!node)
227*4882a593Smuzhiyun return -ENODEV;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun cinfo = devm_kzalloc(&pdev->dev, sizeof(*cinfo), GFP_KERNEL);
230*4882a593Smuzhiyun if (!cinfo)
231*4882a593Smuzhiyun return -ENOMEM;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun cinfo->iobase = of_iomap(node, 0);
234*4882a593Smuzhiyun cinfo->dev = &pdev->dev;
235*4882a593Smuzhiyun pm_runtime_enable(cinfo->dev);
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun pm_runtime_get_sync(cinfo->dev);
238*4882a593Smuzhiyun atl_write(cinfo, DRA7_ATL_PCLKMUX_REG(0), DRA7_ATL_PCLKMUX);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun for (i = 0; i < DRA7_ATL_INSTANCES; i++) {
241*4882a593Smuzhiyun struct device_node *cfg_node;
242*4882a593Smuzhiyun char prop[5];
243*4882a593Smuzhiyun struct dra7_atl_desc *cdesc;
244*4882a593Smuzhiyun struct of_phandle_args clkspec;
245*4882a593Smuzhiyun struct clk *clk;
246*4882a593Smuzhiyun int rc;
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun rc = of_parse_phandle_with_args(node, "ti,provided-clocks",
249*4882a593Smuzhiyun NULL, i, &clkspec);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun if (rc) {
252*4882a593Smuzhiyun pr_err("%s: failed to lookup atl clock %d\n", __func__,
253*4882a593Smuzhiyun i);
254*4882a593Smuzhiyun ret = -EINVAL;
255*4882a593Smuzhiyun goto pm_put;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun clk = of_clk_get_from_provider(&clkspec);
259*4882a593Smuzhiyun if (IS_ERR(clk)) {
260*4882a593Smuzhiyun pr_err("%s: failed to get atl clock %d from provider\n",
261*4882a593Smuzhiyun __func__, i);
262*4882a593Smuzhiyun ret = PTR_ERR(clk);
263*4882a593Smuzhiyun goto pm_put;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun cdesc = to_atl_desc(__clk_get_hw(clk));
267*4882a593Smuzhiyun cdesc->cinfo = cinfo;
268*4882a593Smuzhiyun cdesc->id = i;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /* Get configuration for the ATL instances */
271*4882a593Smuzhiyun snprintf(prop, sizeof(prop), "atl%u", i);
272*4882a593Smuzhiyun cfg_node = of_get_child_by_name(node, prop);
273*4882a593Smuzhiyun if (cfg_node) {
274*4882a593Smuzhiyun ret = of_property_read_u32(cfg_node, "bws",
275*4882a593Smuzhiyun &cdesc->bws);
276*4882a593Smuzhiyun ret |= of_property_read_u32(cfg_node, "aws",
277*4882a593Smuzhiyun &cdesc->aws);
278*4882a593Smuzhiyun if (!ret) {
279*4882a593Smuzhiyun cdesc->valid = true;
280*4882a593Smuzhiyun atl_write(cinfo, DRA7_ATL_BWSMUX_REG(i),
281*4882a593Smuzhiyun cdesc->bws);
282*4882a593Smuzhiyun atl_write(cinfo, DRA7_ATL_AWSMUX_REG(i),
283*4882a593Smuzhiyun cdesc->aws);
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun of_node_put(cfg_node);
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun cdesc->probed = true;
289*4882a593Smuzhiyun /*
290*4882a593Smuzhiyun * Enable the clock if it has been asked prior to loading the
291*4882a593Smuzhiyun * hw driver
292*4882a593Smuzhiyun */
293*4882a593Smuzhiyun if (cdesc->enabled)
294*4882a593Smuzhiyun atl_clk_enable(__clk_get_hw(clk));
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun pm_put:
298*4882a593Smuzhiyun pm_runtime_put_sync(cinfo->dev);
299*4882a593Smuzhiyun return ret;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun static const struct of_device_id of_dra7_atl_clk_match_tbl[] = {
303*4882a593Smuzhiyun { .compatible = "ti,dra7-atl", },
304*4882a593Smuzhiyun {},
305*4882a593Smuzhiyun };
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun static struct platform_driver dra7_atl_clk_driver = {
308*4882a593Smuzhiyun .driver = {
309*4882a593Smuzhiyun .name = "dra7-atl",
310*4882a593Smuzhiyun .suppress_bind_attrs = true,
311*4882a593Smuzhiyun .of_match_table = of_dra7_atl_clk_match_tbl,
312*4882a593Smuzhiyun },
313*4882a593Smuzhiyun .probe = of_dra7_atl_clk_probe,
314*4882a593Smuzhiyun };
315*4882a593Smuzhiyun builtin_platform_driver(dra7_atl_clk_driver);
316