blob: 560a8b9abf9312bd921311bcf88d121727750c83 [file] [log] [blame]
Boris BREZILLON80eded62014-05-07 18:02:15 +02001/*
2 * drivers/clk/at91/clk-slow.c
3 *
4 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 */
12
13#include <linux/clk-provider.h>
14#include <linux/clkdev.h>
15#include <linux/clk/at91_pmc.h>
Boris BREZILLON80eded62014-05-07 18:02:15 +020016#include <linux/of.h>
Boris Brezillon1bdf0232014-09-07 08:14:29 +020017#include <linux/mfd/syscon.h>
18#include <linux/regmap.h>
Boris BREZILLON80eded62014-05-07 18:02:15 +020019
20#include "pmc.h"
Boris BREZILLON80eded62014-05-07 18:02:15 +020021
22struct clk_sam9260_slow {
23 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020024 struct regmap *regmap;
Boris BREZILLON80eded62014-05-07 18:02:15 +020025};
26
27#define to_clk_sam9260_slow(hw) container_of(hw, struct clk_sam9260_slow, hw)
28
Boris BREZILLON80eded62014-05-07 18:02:15 +020029static u8 clk_sam9260_slow_get_parent(struct clk_hw *hw)
30{
31 struct clk_sam9260_slow *slowck = to_clk_sam9260_slow(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +020032 unsigned int status;
Boris BREZILLON80eded62014-05-07 18:02:15 +020033
Boris Brezillon1bdf0232014-09-07 08:14:29 +020034 regmap_read(slowck->regmap, AT91_PMC_SR, &status);
35
36 return status & AT91_PMC_OSCSEL ? 1 : 0;
Boris BREZILLON80eded62014-05-07 18:02:15 +020037}
38
39static const struct clk_ops sam9260_slow_ops = {
40 .get_parent = clk_sam9260_slow_get_parent,
41};
42
Stephen Boydf5644f12016-06-01 14:31:22 -070043static struct clk_hw * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +020044at91_clk_register_sam9260_slow(struct regmap *regmap,
Boris BREZILLON80eded62014-05-07 18:02:15 +020045 const char *name,
46 const char **parent_names,
47 int num_parents)
48{
49 struct clk_sam9260_slow *slowck;
Stephen Boydf5644f12016-06-01 14:31:22 -070050 struct clk_hw *hw;
Boris BREZILLON80eded62014-05-07 18:02:15 +020051 struct clk_init_data init;
Stephen Boydf5644f12016-06-01 14:31:22 -070052 int ret;
Boris BREZILLON80eded62014-05-07 18:02:15 +020053
Boris Brezillon1bdf0232014-09-07 08:14:29 +020054 if (!name)
Boris BREZILLON80eded62014-05-07 18:02:15 +020055 return ERR_PTR(-EINVAL);
56
57 if (!parent_names || !num_parents)
58 return ERR_PTR(-EINVAL);
59
60 slowck = kzalloc(sizeof(*slowck), GFP_KERNEL);
61 if (!slowck)
62 return ERR_PTR(-ENOMEM);
63
64 init.name = name;
65 init.ops = &sam9260_slow_ops;
66 init.parent_names = parent_names;
67 init.num_parents = num_parents;
68 init.flags = 0;
69
70 slowck->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020071 slowck->regmap = regmap;
Boris BREZILLON80eded62014-05-07 18:02:15 +020072
Stephen Boydf5644f12016-06-01 14:31:22 -070073 hw = &slowck->hw;
74 ret = clk_hw_register(NULL, &slowck->hw);
75 if (ret) {
Boris BREZILLON80eded62014-05-07 18:02:15 +020076 kfree(slowck);
Stephen Boydf5644f12016-06-01 14:31:22 -070077 hw = ERR_PTR(ret);
78 }
Boris BREZILLON80eded62014-05-07 18:02:15 +020079
Stephen Boydf5644f12016-06-01 14:31:22 -070080 return hw;
Boris BREZILLON80eded62014-05-07 18:02:15 +020081}
82
Boris Brezillon1bdf0232014-09-07 08:14:29 +020083static void __init of_at91sam9260_clk_slow_setup(struct device_node *np)
Boris BREZILLON80eded62014-05-07 18:02:15 +020084{
Stephen Boydf5644f12016-06-01 14:31:22 -070085 struct clk_hw *hw;
Boris BREZILLON80eded62014-05-07 18:02:15 +020086 const char *parent_names[2];
Stephen Boyd8c1b1e52016-02-19 17:29:17 -080087 unsigned int num_parents;
Boris BREZILLON80eded62014-05-07 18:02:15 +020088 const char *name = np->name;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020089 struct regmap *regmap;
Boris BREZILLON80eded62014-05-07 18:02:15 +020090
Geert Uytterhoeven51a43be2015-05-29 11:25:45 +020091 num_parents = of_clk_get_parent_count(np);
Boris BREZILLONe8531ac2014-09-02 17:27:51 +020092 if (num_parents != 2)
Boris BREZILLON80eded62014-05-07 18:02:15 +020093 return;
94
Dinh Nguyenf0557fb2015-07-06 22:59:01 -050095 of_clk_parent_fill(np, parent_names, num_parents);
Boris Brezillon1bdf0232014-09-07 08:14:29 +020096 regmap = syscon_node_to_regmap(of_get_parent(np));
97 if (IS_ERR(regmap))
98 return;
Boris BREZILLON80eded62014-05-07 18:02:15 +020099
100 of_property_read_string(np, "clock-output-names", &name);
101
Stephen Boydf5644f12016-06-01 14:31:22 -0700102 hw = at91_clk_register_sam9260_slow(regmap, name, parent_names,
Boris BREZILLON80eded62014-05-07 18:02:15 +0200103 num_parents);
Stephen Boydf5644f12016-06-01 14:31:22 -0700104 if (IS_ERR(hw))
Boris BREZILLON80eded62014-05-07 18:02:15 +0200105 return;
106
Stephen Boydf5644f12016-06-01 14:31:22 -0700107 of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
Boris BREZILLON80eded62014-05-07 18:02:15 +0200108}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200109
110CLK_OF_DECLARE(at91sam9260_clk_slow, "atmel,at91sam9260-clk-slow",
111 of_at91sam9260_clk_slow_setup);