blob: 6fdb1abcd4c8174c151d29a9d7b69c3fcf655799 [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
Fabian Frederickf3755732015-03-31 20:50:42 +020056static const struct of_device_id mux_of_match[] = {
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010057 {
Gabriel FERNANDEZ13e6f2d2014-07-15 17:20:23 +020058 .compatible = "st,stih407-clkgen-a9-mux",
59 .data = &stih407_a9_mux_data,
60 },
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010061 {}
62};
Stephen Boyd8e6dd772015-05-01 12:45:53 -070063static void __init st_of_clkgen_mux_setup(struct device_node *np)
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010064{
65 const struct of_device_id *match;
66 struct clk *clk;
67 void __iomem *reg;
68 const char **parents;
Gabriel Fernandez7df404c2016-08-29 14:26:53 +020069 int num_parents = 0;
Stephen Boyd86665d22015-07-07 18:30:05 -070070 const struct clkgen_mux_data *data;
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010071
72 match = of_match_node(mux_of_match, np);
73 if (!match) {
74 pr_err("%s: No matching data\n", __func__);
75 return;
76 }
77
Stephen Boyd86665d22015-07-07 18:30:05 -070078 data = match->data;
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010079
80 reg = of_iomap(np, 0);
81 if (!reg) {
82 pr_err("%s: Failed to get base address\n", __func__);
83 return;
84 }
85
86 parents = clkgen_mux_get_parents(np, &num_parents);
87 if (IS_ERR(parents)) {
88 pr_err("%s: Failed to get parents (%ld)\n",
89 __func__, PTR_ERR(parents));
Stephen Boyd86665d22015-07-07 18:30:05 -070090 goto err_parents;
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +010091 }
92
93 clk = clk_register_mux(NULL, np->name, parents, num_parents,
94 data->clk_flags | CLK_SET_RATE_PARENT,
95 reg + data->offset,
96 data->shift, data->width, data->mux_flags,
97 data->lock);
98 if (IS_ERR(clk))
99 goto err;
100
101 pr_debug("%s: parent %s rate %u\n",
102 __clk_get_name(clk),
103 __clk_get_name(clk_get_parent(clk)),
104 (unsigned int)clk_get_rate(clk));
105
Stephen Boyd86665d22015-07-07 18:30:05 -0700106 kfree(parents);
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +0100107 of_clk_add_provider(np, of_clk_src_simple_get, clk);
Stephen Boyd86665d22015-07-07 18:30:05 -0700108 return;
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +0100109
110err:
111 kfree(parents);
Stephen Boyd86665d22015-07-07 18:30:05 -0700112err_parents:
113 iounmap(reg);
Gabriel FERNANDEZ44993d32014-02-27 16:24:16 +0100114}
115CLK_OF_DECLARE(clkgen_mux, "st,clkgen-mux", st_of_clkgen_mux_setup);