blob: c514d39760cb182efba76ca3f25294628123c8c4 [file] [log] [blame]
Gabriel FERNANDEZ94885fa2014-02-27 16:24:14 +01001/*
2 * clkgen-mux.c: ST GEN-MUX Clock driver
3 *
4 * Copyright (C) 2014 STMicroelectronics (R&D) Limited
5 *
6 * Authors: Stephen Gallimore <stephen.gallimore@st.com>
7 * Pankaj Dev <pankaj.dev@st.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 */
15
16#include <linux/slab.h>
17#include <linux/of_address.h>
Stephen Boydd5f728a2015-06-19 15:00:46 -070018#include <linux/clk.h>
Gabriel FERNANDEZ94885fa2014-02-27 16:24:14 +010019#include <linux/clk-provider.h>
Gabriel Fernandez46a57af2015-10-07 11:08:57 +020020#include "clkgen.h"
Gabriel FERNANDEZ94885fa2014-02-27 16:24:14 +010021
Gabriel FERNANDEZ94885fa2014-02-27 16:24:14 +010022static const char ** __init clkgen_mux_get_parents(struct device_node *np,
23 int *num_parents)
24{
25 const char **parents;
Stephen Boydcaeb0572016-02-19 17:43:30 -080026 unsigned int nparents;
Gabriel FERNANDEZ94885fa2014-02-27 16:24:14 +010027
Geert Uytterhoeven0a652392015-05-29 11:25:46 +020028 nparents = of_clk_get_parent_count(np);
Stephen Boydcaeb0572016-02-19 17:43:30 -080029 if (WARN_ON(!nparents))
Gabriel FERNANDEZ94885fa2014-02-27 16:24:14 +010030 return ERR_PTR(-EINVAL);
31
Stephen Boyd86665d22015-07-07 18:30:05 -070032 parents = kcalloc(nparents, sizeof(const char *), GFP_KERNEL);
Gabriel FERNANDEZ94885fa2014-02-27 16:24:14 +010033 if (!parents)
34 return ERR_PTR(-ENOMEM);
35
Dinh Nguyen0b4e7f02015-07-06 22:59:04 -050036 *num_parents = of_clk_parent_fill(np, parents, nparents);
Gabriel FERNANDEZ94885fa2014-02-27 16:24:14 +010037 return parents;
38}
39
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010040struct clkgen_mux_data {
41 u32 offset;
42 u8 shift;
43 u8 width;
44 spinlock_t *lock;
45 unsigned long clk_flags;
46 u8 mux_flags;
47};
48
Gabriel FERNANDEZ13e6f2d2014-07-15 17:20:23 +020049static struct clkgen_mux_data stih407_a9_mux_data = {
50 .offset = 0x1a4,
Gabriel Fernandez3be6d8c2015-06-23 16:09:25 +020051 .shift = 0,
Gabriel FERNANDEZ13e6f2d2014-07-15 17:20:23 +020052 .width = 2,
Gabriel Fernandez46a57af2015-10-07 11:08:57 +020053 .lock = &clkgen_a9_lock,
Gabriel FERNANDEZ13e6f2d2014-07-15 17:20:23 +020054};
Gabriel FERNANDEZab35dc12014-02-27 16:24:19 +010055
Gabriel Fernandez880d54f2016-08-29 14:26:54 +020056static void __init st_of_clkgen_mux_setup(struct device_node *np,
57 struct clkgen_mux_data *data)
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010058{
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010059 struct clk *clk;
60 void __iomem *reg;
61 const char **parents;
Gabriel Fernandez7df404c2016-08-29 14:26:53 +020062 int num_parents = 0;
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010063
64 reg = of_iomap(np, 0);
65 if (!reg) {
66 pr_err("%s: Failed to get base address\n", __func__);
67 return;
68 }
69
70 parents = clkgen_mux_get_parents(np, &num_parents);
71 if (IS_ERR(parents)) {
72 pr_err("%s: Failed to get parents (%ld)\n",
73 __func__, PTR_ERR(parents));
Stephen Boyd86665d22015-07-07 18:30:05 -070074 goto err_parents;
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010075 }
76
77 clk = clk_register_mux(NULL, np->name, parents, num_parents,
78 data->clk_flags | CLK_SET_RATE_PARENT,
79 reg + data->offset,
80 data->shift, data->width, data->mux_flags,
81 data->lock);
82 if (IS_ERR(clk))
83 goto err;
84
85 pr_debug("%s: parent %s rate %u\n",
86 __clk_get_name(clk),
87 __clk_get_name(clk_get_parent(clk)),
88 (unsigned int)clk_get_rate(clk));
89
Stephen Boyd86665d22015-07-07 18:30:05 -070090 kfree(parents);
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010091 of_clk_add_provider(np, of_clk_src_simple_get, clk);
Stephen Boyd86665d22015-07-07 18:30:05 -070092 return;
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010093
94err:
95 kfree(parents);
Stephen Boyd86665d22015-07-07 18:30:05 -070096err_parents:
97 iounmap(reg);
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010098}
Gabriel Fernandez880d54f2016-08-29 14:26:54 +020099
100static void __init st_of_clkgen_a9_mux_setup(struct device_node *np)
101{
102 st_of_clkgen_mux_setup(np, &stih407_a9_mux_data);
103}
104CLK_OF_DECLARE(clkgen_a9mux, "st,stih407-clkgen-a9-mux",
105 st_of_clkgen_a9_mux_setup);