blob: c45a75e65c869ea48cb4ecde71937571e3309393 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
H Hartley Sweetena2308692013-10-15 18:40:03 -07002/*
3 * PWM framework driver for Cirrus Logic EP93xx
4 *
5 * Copyright (c) 2009 Matthieu Crapet <mcrapet@gmail.com>
6 * Copyright (c) 2009, 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
7 *
8 * EP9301/02 have only one channel:
9 * platform device ep93xx-pwm.1 - PWMOUT1 (EGPIO14)
10 *
11 * EP9307 has only one channel:
12 * platform device ep93xx-pwm.0 - PWMOUT
13 *
14 * EP9312/15 have two channels:
15 * platform device ep93xx-pwm.0 - PWMOUT
16 * platform device ep93xx-pwm.1 - PWMOUT1 (EGPIO14)
H Hartley Sweetena2308692013-10-15 18:40:03 -070017 */
18
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <linux/clk.h>
23#include <linux/err.h>
24#include <linux/io.h>
25#include <linux/pwm.h>
26
27#include <asm/div64.h>
28
Arnd Bergmann67e38f52019-04-15 22:17:11 +020029#include <linux/soc/cirrus/ep93xx.h> /* for ep93xx_pwm_{acquire,release}_gpio() */
H Hartley Sweetena2308692013-10-15 18:40:03 -070030
31#define EP93XX_PWMx_TERM_COUNT 0x00
32#define EP93XX_PWMx_DUTY_CYCLE 0x04
33#define EP93XX_PWMx_ENABLE 0x08
34#define EP93XX_PWMx_INVERT 0x0c
35
36struct ep93xx_pwm {
37 void __iomem *base;
38 struct clk *clk;
39 struct pwm_chip chip;
40};
41
42static inline struct ep93xx_pwm *to_ep93xx_pwm(struct pwm_chip *chip)
43{
44 return container_of(chip, struct ep93xx_pwm, chip);
45}
46
47static int ep93xx_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
48{
49 struct platform_device *pdev = to_platform_device(chip->dev);
50
51 return ep93xx_pwm_acquire_gpio(pdev);
52}
53
54static void ep93xx_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
55{
56 struct platform_device *pdev = to_platform_device(chip->dev);
57
58 ep93xx_pwm_release_gpio(pdev);
59}
60
Uwe Kleine-König6d453742021-06-23 16:02:39 +020061static int ep93xx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
62 const struct pwm_state *state)
63{
64 int ret;
Uwe Kleine-König72cce472021-06-23 16:02:40 +020065 struct ep93xx_pwm *ep93xx_pwm = to_ep93xx_pwm(chip);
Uwe Kleine-König6d453742021-06-23 16:02:39 +020066 bool enabled = state->enabled;
Uwe Kleine-Königf4a8e312021-07-01 10:27:55 +020067 void __iomem *base = ep93xx_pwm->base;
68 unsigned long long c;
69 unsigned long period_cycles;
70 unsigned long duty_cycles;
71 unsigned long term;
Uwe Kleine-König6d453742021-06-23 16:02:39 +020072
73 if (state->polarity != pwm->state.polarity) {
74 if (enabled) {
Uwe Kleine-König72cce472021-06-23 16:02:40 +020075 writew(0x0, ep93xx_pwm->base + EP93XX_PWMx_ENABLE);
Alexander Sverdlinb235f8a2021-06-14 01:30:41 +020076 clk_disable_unprepare(ep93xx_pwm->clk);
Uwe Kleine-König6d453742021-06-23 16:02:39 +020077 enabled = false;
78 }
79
Uwe Kleine-König72cce472021-06-23 16:02:40 +020080 /*
81 * The clock needs to be enabled to access the PWM registers.
82 * Polarity can only be changed when the PWM is disabled.
83 */
Alexander Sverdlinb235f8a2021-06-14 01:30:41 +020084 ret = clk_prepare_enable(ep93xx_pwm->clk);
Uwe Kleine-König6d453742021-06-23 16:02:39 +020085 if (ret)
86 return ret;
Uwe Kleine-König72cce472021-06-23 16:02:40 +020087
88 if (state->polarity == PWM_POLARITY_INVERSED)
89 writew(0x1, ep93xx_pwm->base + EP93XX_PWMx_INVERT);
90 else
91 writew(0x0, ep93xx_pwm->base + EP93XX_PWMx_INVERT);
92
Alexander Sverdlinb235f8a2021-06-14 01:30:41 +020093 clk_disable_unprepare(ep93xx_pwm->clk);
Uwe Kleine-König6d453742021-06-23 16:02:39 +020094 }
95
96 if (!state->enabled) {
Uwe Kleine-König72cce472021-06-23 16:02:40 +020097 if (enabled) {
98 writew(0x0, ep93xx_pwm->base + EP93XX_PWMx_ENABLE);
Alexander Sverdlinb235f8a2021-06-14 01:30:41 +020099 clk_disable_unprepare(ep93xx_pwm->clk);
Uwe Kleine-König72cce472021-06-23 16:02:40 +0200100 }
Uwe Kleine-König6d453742021-06-23 16:02:39 +0200101
102 return 0;
103 }
104
Uwe Kleine-Königf4a8e312021-07-01 10:27:55 +0200105 /*
106 * The clock needs to be enabled to access the PWM registers.
107 * Configuration can be changed at any time.
108 */
109 if (!pwm_is_enabled(pwm)) {
110 ret = clk_prepare_enable(ep93xx_pwm->clk);
Uwe Kleine-König6d453742021-06-23 16:02:39 +0200111 if (ret)
112 return ret;
113 }
114
Uwe Kleine-Königf4a8e312021-07-01 10:27:55 +0200115 c = clk_get_rate(ep93xx_pwm->clk);
116 c *= state->period;
117 do_div(c, 1000000000);
118 period_cycles = c;
119
120 c = period_cycles;
121 c *= state->duty_cycle;
122 do_div(c, state->period);
123 duty_cycles = c;
124
125 if (period_cycles < 0x10000 && duty_cycles < 0x10000) {
126 term = readw(base + EP93XX_PWMx_TERM_COUNT);
127
128 /* Order is important if PWM is running */
129 if (period_cycles > term) {
130 writew(period_cycles, base + EP93XX_PWMx_TERM_COUNT);
131 writew(duty_cycles, base + EP93XX_PWMx_DUTY_CYCLE);
132 } else {
133 writew(duty_cycles, base + EP93XX_PWMx_DUTY_CYCLE);
134 writew(period_cycles, base + EP93XX_PWMx_TERM_COUNT);
135 }
136 ret = 0;
137 } else {
138 ret = -EINVAL;
139 }
140
141 if (!pwm_is_enabled(pwm))
142 clk_disable_unprepare(ep93xx_pwm->clk);
143
144 if (ret)
145 return ret;
146
Uwe Kleine-König72cce472021-06-23 16:02:40 +0200147 if (!enabled) {
Alexander Sverdlinb235f8a2021-06-14 01:30:41 +0200148 ret = clk_prepare_enable(ep93xx_pwm->clk);
Uwe Kleine-König72cce472021-06-23 16:02:40 +0200149 if (ret)
150 return ret;
151
152 writew(0x1, ep93xx_pwm->base + EP93XX_PWMx_ENABLE);
153 }
Uwe Kleine-König6d453742021-06-23 16:02:39 +0200154
155 return 0;
156}
157
H Hartley Sweetena2308692013-10-15 18:40:03 -0700158static const struct pwm_ops ep93xx_pwm_ops = {
159 .request = ep93xx_pwm_request,
160 .free = ep93xx_pwm_free,
Uwe Kleine-König6d453742021-06-23 16:02:39 +0200161 .apply = ep93xx_pwm_apply,
H Hartley Sweetena2308692013-10-15 18:40:03 -0700162 .owner = THIS_MODULE,
163};
164
165static int ep93xx_pwm_probe(struct platform_device *pdev)
166{
167 struct ep93xx_pwm *ep93xx_pwm;
H Hartley Sweetena2308692013-10-15 18:40:03 -0700168 int ret;
169
170 ep93xx_pwm = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_pwm), GFP_KERNEL);
171 if (!ep93xx_pwm)
172 return -ENOMEM;
173
Yangtao Lifc0155f2019-12-29 08:05:43 +0000174 ep93xx_pwm->base = devm_platform_ioremap_resource(pdev, 0);
H Hartley Sweetena2308692013-10-15 18:40:03 -0700175 if (IS_ERR(ep93xx_pwm->base))
176 return PTR_ERR(ep93xx_pwm->base);
177
178 ep93xx_pwm->clk = devm_clk_get(&pdev->dev, "pwm_clk");
179 if (IS_ERR(ep93xx_pwm->clk))
180 return PTR_ERR(ep93xx_pwm->clk);
181
182 ep93xx_pwm->chip.dev = &pdev->dev;
183 ep93xx_pwm->chip.ops = &ep93xx_pwm_ops;
H Hartley Sweetena2308692013-10-15 18:40:03 -0700184 ep93xx_pwm->chip.npwm = 1;
185
Uwe Kleine-Königa0b336a2021-07-07 18:28:02 +0200186 ret = devm_pwmchip_add(&pdev->dev, &ep93xx_pwm->chip);
H Hartley Sweetena2308692013-10-15 18:40:03 -0700187 if (ret < 0)
188 return ret;
189
H Hartley Sweetena2308692013-10-15 18:40:03 -0700190 return 0;
191}
192
H Hartley Sweetena2308692013-10-15 18:40:03 -0700193static struct platform_driver ep93xx_pwm_driver = {
194 .driver = {
195 .name = "ep93xx-pwm",
196 },
197 .probe = ep93xx_pwm_probe,
H Hartley Sweetena2308692013-10-15 18:40:03 -0700198};
199module_platform_driver(ep93xx_pwm_driver);
200
201MODULE_DESCRIPTION("Cirrus Logic EP93xx PWM driver");
Jingoo Han5eabf822013-12-19 13:32:45 +0900202MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>");
203MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
H Hartley Sweetena2308692013-10-15 18:40:03 -0700204MODULE_ALIAS("platform:ep93xx-pwm");
205MODULE_LICENSE("GPL");