blob: 5f0490b8f6cb819c72ddee9469e19d232d874afc [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Philipp Zabel9a74ccd2015-02-13 20:18:52 +01002/*
3 * Copyright (C) 2014 Philipp Zabel, Pengutronix
4 *
Philipp Zabel9a74ccd2015-02-13 20:18:52 +01005 * PWM (mis)used as clock output
6 */
7#include <linux/clk-provider.h>
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/of.h>
11#include <linux/platform_device.h>
12#include <linux/pwm.h>
13
14struct clk_pwm {
15 struct clk_hw hw;
16 struct pwm_device *pwm;
17 u32 fixed_rate;
18};
19
20static inline struct clk_pwm *to_clk_pwm(struct clk_hw *hw)
21{
22 return container_of(hw, struct clk_pwm, hw);
23}
24
25static int clk_pwm_prepare(struct clk_hw *hw)
26{
27 struct clk_pwm *clk_pwm = to_clk_pwm(hw);
28
29 return pwm_enable(clk_pwm->pwm);
30}
31
32static void clk_pwm_unprepare(struct clk_hw *hw)
33{
34 struct clk_pwm *clk_pwm = to_clk_pwm(hw);
35
36 pwm_disable(clk_pwm->pwm);
37}
38
39static unsigned long clk_pwm_recalc_rate(struct clk_hw *hw,
40 unsigned long parent_rate)
41{
42 struct clk_pwm *clk_pwm = to_clk_pwm(hw);
43
44 return clk_pwm->fixed_rate;
45}
46
47static const struct clk_ops clk_pwm_ops = {
48 .prepare = clk_pwm_prepare,
49 .unprepare = clk_pwm_unprepare,
50 .recalc_rate = clk_pwm_recalc_rate,
51};
52
53static int clk_pwm_probe(struct platform_device *pdev)
54{
55 struct device_node *node = pdev->dev.of_node;
56 struct clk_init_data init;
57 struct clk_pwm *clk_pwm;
58 struct pwm_device *pwm;
Boris Brezillondd0b38b2016-04-14 21:17:23 +020059 struct pwm_args pargs;
Philipp Zabel9a74ccd2015-02-13 20:18:52 +010060 const char *clk_name;
Philipp Zabel9a74ccd2015-02-13 20:18:52 +010061 int ret;
62
63 clk_pwm = devm_kzalloc(&pdev->dev, sizeof(*clk_pwm), GFP_KERNEL);
64 if (!clk_pwm)
65 return -ENOMEM;
66
67 pwm = devm_pwm_get(&pdev->dev, NULL);
68 if (IS_ERR(pwm))
69 return PTR_ERR(pwm);
70
Boris Brezillondd0b38b2016-04-14 21:17:23 +020071 pwm_get_args(pwm, &pargs);
72 if (!pargs.period) {
Philipp Zabel9a74ccd2015-02-13 20:18:52 +010073 dev_err(&pdev->dev, "invalid PWM period\n");
74 return -EINVAL;
75 }
76
77 if (of_property_read_u32(node, "clock-frequency", &clk_pwm->fixed_rate))
Boris Brezillondd0b38b2016-04-14 21:17:23 +020078 clk_pwm->fixed_rate = NSEC_PER_SEC / pargs.period;
Philipp Zabel9a74ccd2015-02-13 20:18:52 +010079
Boris Brezillondd0b38b2016-04-14 21:17:23 +020080 if (pargs.period != NSEC_PER_SEC / clk_pwm->fixed_rate &&
81 pargs.period != DIV_ROUND_UP(NSEC_PER_SEC, clk_pwm->fixed_rate)) {
Philipp Zabel9a74ccd2015-02-13 20:18:52 +010082 dev_err(&pdev->dev,
83 "clock-frequency does not match PWM period\n");
84 return -EINVAL;
85 }
86
Boris Brezillondd0b38b2016-04-14 21:17:23 +020087 /*
88 * FIXME: pwm_apply_args() should be removed when switching to the
89 * atomic PWM API.
90 */
91 pwm_apply_args(pwm);
92 ret = pwm_config(pwm, (pargs.period + 1) >> 1, pargs.period);
Philipp Zabel9a74ccd2015-02-13 20:18:52 +010093 if (ret < 0)
94 return ret;
95
96 clk_name = node->name;
97 of_property_read_string(node, "clock-output-names", &clk_name);
98
99 init.name = clk_name;
100 init.ops = &clk_pwm_ops;
Stephen Boyd90b6c5c2019-04-25 10:57:37 -0700101 init.flags = 0;
Philipp Zabel9a74ccd2015-02-13 20:18:52 +0100102 init.num_parents = 0;
103
104 clk_pwm->pwm = pwm;
105 clk_pwm->hw.init = &init;
Stephen Boyd4cf915d2016-06-01 16:15:22 -0700106 ret = devm_clk_hw_register(&pdev->dev, &clk_pwm->hw);
107 if (ret)
108 return ret;
Philipp Zabel9a74ccd2015-02-13 20:18:52 +0100109
Stephen Boyd4cf915d2016-06-01 16:15:22 -0700110 return of_clk_add_hw_provider(node, of_clk_hw_simple_get, &clk_pwm->hw);
Philipp Zabel9a74ccd2015-02-13 20:18:52 +0100111}
112
113static int clk_pwm_remove(struct platform_device *pdev)
114{
115 of_clk_del_provider(pdev->dev.of_node);
116
117 return 0;
118}
119
120static const struct of_device_id clk_pwm_dt_ids[] = {
121 { .compatible = "pwm-clock" },
122 { }
123};
124MODULE_DEVICE_TABLE(of, clk_pwm_dt_ids);
125
126static struct platform_driver clk_pwm_driver = {
127 .probe = clk_pwm_probe,
128 .remove = clk_pwm_remove,
129 .driver = {
130 .name = "pwm-clock",
131 .of_match_table = of_match_ptr(clk_pwm_dt_ids),
132 },
133};
134
135module_platform_driver(clk_pwm_driver);
136
137MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
138MODULE_DESCRIPTION("PWM clock driver");
139MODULE_LICENSE("GPL");