Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License version 2 as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * Copyright (C) 2012 ARM Limited |
| 12 | */ |
| 13 | |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 14 | #include <linux/clkdev.h> |
| 15 | #include <linux/clk-provider.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/vexpress.h> |
| 21 | |
| 22 | struct vexpress_osc { |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 23 | struct regmap *reg; |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 24 | struct clk_hw hw; |
| 25 | unsigned long rate_min; |
| 26 | unsigned long rate_max; |
| 27 | }; |
| 28 | |
| 29 | #define to_vexpress_osc(osc) container_of(osc, struct vexpress_osc, hw) |
| 30 | |
| 31 | static unsigned long vexpress_osc_recalc_rate(struct clk_hw *hw, |
| 32 | unsigned long parent_rate) |
| 33 | { |
| 34 | struct vexpress_osc *osc = to_vexpress_osc(hw); |
| 35 | u32 rate; |
| 36 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 37 | regmap_read(osc->reg, 0, &rate); |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 38 | |
| 39 | return rate; |
| 40 | } |
| 41 | |
| 42 | static long vexpress_osc_round_rate(struct clk_hw *hw, unsigned long rate, |
| 43 | unsigned long *parent_rate) |
| 44 | { |
| 45 | struct vexpress_osc *osc = to_vexpress_osc(hw); |
| 46 | |
| 47 | if (WARN_ON(osc->rate_min && rate < osc->rate_min)) |
| 48 | rate = osc->rate_min; |
| 49 | |
| 50 | if (WARN_ON(osc->rate_max && rate > osc->rate_max)) |
| 51 | rate = osc->rate_max; |
| 52 | |
| 53 | return rate; |
| 54 | } |
| 55 | |
| 56 | static int vexpress_osc_set_rate(struct clk_hw *hw, unsigned long rate, |
| 57 | unsigned long parent_rate) |
| 58 | { |
| 59 | struct vexpress_osc *osc = to_vexpress_osc(hw); |
| 60 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 61 | return regmap_write(osc->reg, 0, rate); |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static struct clk_ops vexpress_osc_ops = { |
| 65 | .recalc_rate = vexpress_osc_recalc_rate, |
| 66 | .round_rate = vexpress_osc_round_rate, |
| 67 | .set_rate = vexpress_osc_set_rate, |
| 68 | }; |
| 69 | |
| 70 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 71 | static int vexpress_osc_probe(struct platform_device *pdev) |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 72 | { |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 73 | struct clk_lookup *cl = pdev->dev.platform_data; /* Non-DT lookup */ |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 74 | struct clk_init_data init; |
| 75 | struct vexpress_osc *osc; |
| 76 | struct clk *clk; |
| 77 | u32 range[2]; |
| 78 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 79 | osc = devm_kzalloc(&pdev->dev, sizeof(*osc), GFP_KERNEL); |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 80 | if (!osc) |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 81 | return -ENOMEM; |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 82 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 83 | osc->reg = devm_regmap_init_vexpress_config(&pdev->dev); |
| 84 | if (IS_ERR(osc->reg)) |
| 85 | return PTR_ERR(osc->reg); |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 86 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 87 | if (of_property_read_u32_array(pdev->dev.of_node, "freq-range", range, |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 88 | ARRAY_SIZE(range)) == 0) { |
| 89 | osc->rate_min = range[0]; |
| 90 | osc->rate_max = range[1]; |
| 91 | } |
| 92 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 93 | if (of_property_read_string(pdev->dev.of_node, "clock-output-names", |
| 94 | &init.name) != 0) |
| 95 | init.name = dev_name(&pdev->dev); |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 96 | |
| 97 | init.ops = &vexpress_osc_ops; |
| 98 | init.flags = CLK_IS_ROOT; |
| 99 | init.num_parents = 0; |
| 100 | |
| 101 | osc->hw.init = &init; |
| 102 | |
| 103 | clk = clk_register(NULL, &osc->hw); |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 104 | if (IS_ERR(clk)) |
| 105 | return PTR_ERR(clk); |
| 106 | |
| 107 | of_clk_add_provider(pdev->dev.of_node, of_clk_src_simple_get, clk); |
| 108 | |
| 109 | /* Only happens for non-DT cases */ |
| 110 | if (cl) { |
| 111 | cl->clk = clk; |
| 112 | clkdev_add(cl); |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 115 | dev_dbg(&pdev->dev, "Registered clock '%s'\n", init.name); |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 116 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 117 | return 0; |
Pawel Moll | ed27ff1 | 2012-09-18 15:17:47 +0100 | [diff] [blame] | 118 | } |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 119 | |
| 120 | static struct of_device_id vexpress_osc_of_match[] = { |
| 121 | { .compatible = "arm,vexpress-osc", }, |
| 122 | {} |
| 123 | }; |
| 124 | |
| 125 | static struct platform_driver vexpress_osc_driver = { |
| 126 | .driver = { |
| 127 | .name = "vexpress-osc", |
| 128 | .of_match_table = vexpress_osc_of_match, |
| 129 | }, |
| 130 | .probe = vexpress_osc_probe, |
| 131 | }; |
| 132 | |
| 133 | static int __init vexpress_osc_init(void) |
| 134 | { |
| 135 | return platform_driver_register(&vexpress_osc_driver); |
| 136 | } |
| 137 | core_initcall(vexpress_osc_init); |