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