150cc4cafSJan Kotas // SPDX-License-Identifier: GPL-2.0
250cc4cafSJan Kotas
350cc4cafSJan Kotas /*
450cc4cafSJan Kotas * Memory Mapped IO Fixed clock driver
550cc4cafSJan Kotas *
650cc4cafSJan Kotas * Copyright (C) 2018 Cadence Design Systems, Inc.
750cc4cafSJan Kotas *
850cc4cafSJan Kotas * Authors:
950cc4cafSJan Kotas * Jan Kotas <jank@cadence.com>
1050cc4cafSJan Kotas */
1150cc4cafSJan Kotas
1250cc4cafSJan Kotas #include <linux/clk-provider.h>
1362e59c4eSStephen Boyd #include <linux/io.h>
1450cc4cafSJan Kotas #include <linux/module.h>
1562e59c4eSStephen Boyd #include <linux/of_address.h>
1650cc4cafSJan Kotas #include <linux/platform_device.h>
1750cc4cafSJan Kotas
fixed_mmio_clk_setup(struct device_node * node)1850cc4cafSJan Kotas static struct clk_hw *fixed_mmio_clk_setup(struct device_node *node)
1950cc4cafSJan Kotas {
2050cc4cafSJan Kotas struct clk_hw *clk;
2150cc4cafSJan Kotas const char *clk_name = node->name;
2250cc4cafSJan Kotas void __iomem *base;
2350cc4cafSJan Kotas u32 freq;
2450cc4cafSJan Kotas int ret;
2550cc4cafSJan Kotas
2650cc4cafSJan Kotas base = of_iomap(node, 0);
2750cc4cafSJan Kotas if (!base) {
2850cc4cafSJan Kotas pr_err("%pOFn: failed to map address\n", node);
2950cc4cafSJan Kotas return ERR_PTR(-EIO);
3050cc4cafSJan Kotas }
3150cc4cafSJan Kotas
3250cc4cafSJan Kotas freq = readl(base);
3350cc4cafSJan Kotas iounmap(base);
3450cc4cafSJan Kotas of_property_read_string(node, "clock-output-names", &clk_name);
3550cc4cafSJan Kotas
3650cc4cafSJan Kotas clk = clk_hw_register_fixed_rate(NULL, clk_name, NULL, 0, freq);
3750cc4cafSJan Kotas if (IS_ERR(clk)) {
3850cc4cafSJan Kotas pr_err("%pOFn: failed to register fixed rate clock\n", node);
3950cc4cafSJan Kotas return clk;
4050cc4cafSJan Kotas }
4150cc4cafSJan Kotas
4250cc4cafSJan Kotas ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, clk);
4350cc4cafSJan Kotas if (ret) {
4450cc4cafSJan Kotas pr_err("%pOFn: failed to add clock provider\n", node);
4550cc4cafSJan Kotas clk_hw_unregister(clk);
4650cc4cafSJan Kotas clk = ERR_PTR(ret);
4750cc4cafSJan Kotas }
4850cc4cafSJan Kotas
4950cc4cafSJan Kotas return clk;
5050cc4cafSJan Kotas }
5150cc4cafSJan Kotas
of_fixed_mmio_clk_setup(struct device_node * node)5250cc4cafSJan Kotas static void __init of_fixed_mmio_clk_setup(struct device_node *node)
5350cc4cafSJan Kotas {
5450cc4cafSJan Kotas fixed_mmio_clk_setup(node);
5550cc4cafSJan Kotas }
5650cc4cafSJan Kotas CLK_OF_DECLARE(fixed_mmio_clk, "fixed-mmio-clock", of_fixed_mmio_clk_setup);
5750cc4cafSJan Kotas
58e28aa9b0SLee Jones /*
5950cc4cafSJan Kotas * This is not executed when of_fixed_mmio_clk_setup succeeded.
6050cc4cafSJan Kotas */
of_fixed_mmio_clk_probe(struct platform_device * pdev)6150cc4cafSJan Kotas static int of_fixed_mmio_clk_probe(struct platform_device *pdev)
6250cc4cafSJan Kotas {
6350cc4cafSJan Kotas struct clk_hw *clk;
6450cc4cafSJan Kotas
6550cc4cafSJan Kotas clk = fixed_mmio_clk_setup(pdev->dev.of_node);
6650cc4cafSJan Kotas if (IS_ERR(clk))
6750cc4cafSJan Kotas return PTR_ERR(clk);
6850cc4cafSJan Kotas
6950cc4cafSJan Kotas platform_set_drvdata(pdev, clk);
7050cc4cafSJan Kotas
7150cc4cafSJan Kotas return 0;
7250cc4cafSJan Kotas }
7350cc4cafSJan Kotas
of_fixed_mmio_clk_remove(struct platform_device * pdev)74*6f149b65SUwe Kleine-König static void of_fixed_mmio_clk_remove(struct platform_device *pdev)
7550cc4cafSJan Kotas {
7650cc4cafSJan Kotas struct clk_hw *clk = platform_get_drvdata(pdev);
7750cc4cafSJan Kotas
7850cc4cafSJan Kotas of_clk_del_provider(pdev->dev.of_node);
7950cc4cafSJan Kotas clk_hw_unregister_fixed_rate(clk);
8050cc4cafSJan Kotas }
8150cc4cafSJan Kotas
8250cc4cafSJan Kotas static const struct of_device_id of_fixed_mmio_clk_ids[] = {
8350cc4cafSJan Kotas { .compatible = "fixed-mmio-clock" },
8450cc4cafSJan Kotas { }
8550cc4cafSJan Kotas };
8650cc4cafSJan Kotas MODULE_DEVICE_TABLE(of, of_fixed_mmio_clk_ids);
8750cc4cafSJan Kotas
8850cc4cafSJan Kotas static struct platform_driver of_fixed_mmio_clk_driver = {
8950cc4cafSJan Kotas .driver = {
9050cc4cafSJan Kotas .name = "of_fixed_mmio_clk",
9150cc4cafSJan Kotas .of_match_table = of_fixed_mmio_clk_ids,
9250cc4cafSJan Kotas },
9350cc4cafSJan Kotas .probe = of_fixed_mmio_clk_probe,
94*6f149b65SUwe Kleine-König .remove_new = of_fixed_mmio_clk_remove,
9550cc4cafSJan Kotas };
9650cc4cafSJan Kotas module_platform_driver(of_fixed_mmio_clk_driver);
9750cc4cafSJan Kotas
9850cc4cafSJan Kotas MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
9950cc4cafSJan Kotas MODULE_DESCRIPTION("Memory Mapped IO Fixed clock driver");
10050cc4cafSJan Kotas