1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
4 * Copyright 2017 Linaro Ltd.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/pwm.h>
14 #include <linux/slab.h>
16 #define ZX_PWM_MODE 0x0
17 #define ZX_PWM_CLKDIV_SHIFT 2
18 #define ZX_PWM_CLKDIV_MASK GENMASK(11, 2)
19 #define ZX_PWM_CLKDIV(x) (((x) << ZX_PWM_CLKDIV_SHIFT) & \
21 #define ZX_PWM_POLAR BIT(1)
22 #define ZX_PWM_EN BIT(0)
23 #define ZX_PWM_PERIOD 0x4
24 #define ZX_PWM_DUTY 0x8
26 #define ZX_PWM_CLKDIV_MAX 1023
27 #define ZX_PWM_PERIOD_MAX 65535
36 static inline struct zx_pwm_chip
*to_zx_pwm_chip(struct pwm_chip
*chip
)
38 return container_of(chip
, struct zx_pwm_chip
, chip
);
41 static inline u32
zx_pwm_readl(struct zx_pwm_chip
*zpc
, unsigned int hwpwm
,
44 return readl(zpc
->base
+ (hwpwm
+ 1) * 0x10 + offset
);
47 static inline void zx_pwm_writel(struct zx_pwm_chip
*zpc
, unsigned int hwpwm
,
48 unsigned int offset
, u32 value
)
50 writel(value
, zpc
->base
+ (hwpwm
+ 1) * 0x10 + offset
);
53 static void zx_pwm_set_mask(struct zx_pwm_chip
*zpc
, unsigned int hwpwm
,
54 unsigned int offset
, u32 mask
, u32 value
)
58 data
= zx_pwm_readl(zpc
, hwpwm
, offset
);
61 zx_pwm_writel(zpc
, hwpwm
, offset
, data
);
64 static void zx_pwm_get_state(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
65 struct pwm_state
*state
)
67 struct zx_pwm_chip
*zpc
= to_zx_pwm_chip(chip
);
73 value
= zx_pwm_readl(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
);
75 if (value
& ZX_PWM_POLAR
)
76 state
->polarity
= PWM_POLARITY_NORMAL
;
78 state
->polarity
= PWM_POLARITY_INVERSED
;
80 if (value
& ZX_PWM_EN
)
81 state
->enabled
= true;
83 state
->enabled
= false;
85 div
= (value
& ZX_PWM_CLKDIV_MASK
) >> ZX_PWM_CLKDIV_SHIFT
;
86 rate
= clk_get_rate(zpc
->wclk
);
88 tmp
= zx_pwm_readl(zpc
, pwm
->hwpwm
, ZX_PWM_PERIOD
);
89 tmp
*= div
* NSEC_PER_SEC
;
90 state
->period
= DIV_ROUND_CLOSEST_ULL(tmp
, rate
);
92 tmp
= zx_pwm_readl(zpc
, pwm
->hwpwm
, ZX_PWM_DUTY
);
93 tmp
*= div
* NSEC_PER_SEC
;
94 state
->duty_cycle
= DIV_ROUND_CLOSEST_ULL(tmp
, rate
);
97 static int zx_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
98 unsigned int duty_ns
, unsigned int period_ns
)
100 struct zx_pwm_chip
*zpc
= to_zx_pwm_chip(chip
);
101 unsigned int period_cycles
, duty_cycles
;
102 unsigned long long c
;
103 unsigned int div
= 1;
106 /* Find out the best divider */
107 rate
= clk_get_rate(zpc
->wclk
);
112 do_div(c
, NSEC_PER_SEC
);
114 if (c
< ZX_PWM_PERIOD_MAX
)
119 if (div
> ZX_PWM_CLKDIV_MAX
)
123 /* Calculate duty cycles */
126 do_div(c
, period_ns
);
130 * If the PWM is being enabled, we have to temporarily disable it
131 * before configuring the registers.
133 if (pwm_is_enabled(pwm
))
134 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
, ZX_PWM_EN
, 0);
136 /* Set up registers */
137 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
, ZX_PWM_CLKDIV_MASK
,
139 zx_pwm_writel(zpc
, pwm
->hwpwm
, ZX_PWM_PERIOD
, period_cycles
);
140 zx_pwm_writel(zpc
, pwm
->hwpwm
, ZX_PWM_DUTY
, duty_cycles
);
142 /* Re-enable the PWM if needed */
143 if (pwm_is_enabled(pwm
))
144 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
,
145 ZX_PWM_EN
, ZX_PWM_EN
);
150 static int zx_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
151 struct pwm_state
*state
)
153 struct zx_pwm_chip
*zpc
= to_zx_pwm_chip(chip
);
154 struct pwm_state cstate
;
157 pwm_get_state(pwm
, &cstate
);
159 if (state
->polarity
!= cstate
.polarity
)
160 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
, ZX_PWM_POLAR
,
161 (state
->polarity
== PWM_POLARITY_INVERSED
) ?
164 if (state
->period
!= cstate
.period
||
165 state
->duty_cycle
!= cstate
.duty_cycle
) {
166 ret
= zx_pwm_config(chip
, pwm
, state
->duty_cycle
,
172 if (state
->enabled
!= cstate
.enabled
) {
173 if (state
->enabled
) {
174 ret
= clk_prepare_enable(zpc
->wclk
);
178 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
,
179 ZX_PWM_EN
, ZX_PWM_EN
);
181 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
,
183 clk_disable_unprepare(zpc
->wclk
);
190 static const struct pwm_ops zx_pwm_ops
= {
191 .apply
= zx_pwm_apply
,
192 .get_state
= zx_pwm_get_state
,
193 .owner
= THIS_MODULE
,
196 static int zx_pwm_probe(struct platform_device
*pdev
)
198 struct zx_pwm_chip
*zpc
;
199 struct resource
*res
;
203 zpc
= devm_kzalloc(&pdev
->dev
, sizeof(*zpc
), GFP_KERNEL
);
207 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
208 zpc
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
209 if (IS_ERR(zpc
->base
))
210 return PTR_ERR(zpc
->base
);
212 zpc
->pclk
= devm_clk_get(&pdev
->dev
, "pclk");
213 if (IS_ERR(zpc
->pclk
))
214 return PTR_ERR(zpc
->pclk
);
216 zpc
->wclk
= devm_clk_get(&pdev
->dev
, "wclk");
217 if (IS_ERR(zpc
->wclk
))
218 return PTR_ERR(zpc
->wclk
);
220 ret
= clk_prepare_enable(zpc
->pclk
);
224 zpc
->chip
.dev
= &pdev
->dev
;
225 zpc
->chip
.ops
= &zx_pwm_ops
;
228 zpc
->chip
.of_xlate
= of_pwm_xlate_with_flags
;
229 zpc
->chip
.of_pwm_n_cells
= 3;
232 * PWM devices may be enabled by firmware, and let's disable all of
233 * them initially to save power.
235 for (i
= 0; i
< zpc
->chip
.npwm
; i
++)
236 zx_pwm_set_mask(zpc
, i
, ZX_PWM_MODE
, ZX_PWM_EN
, 0);
238 ret
= pwmchip_add(&zpc
->chip
);
240 dev_err(&pdev
->dev
, "failed to add PWM chip: %d\n", ret
);
244 platform_set_drvdata(pdev
, zpc
);
249 static int zx_pwm_remove(struct platform_device
*pdev
)
251 struct zx_pwm_chip
*zpc
= platform_get_drvdata(pdev
);
254 ret
= pwmchip_remove(&zpc
->chip
);
255 clk_disable_unprepare(zpc
->pclk
);
260 static const struct of_device_id zx_pwm_dt_ids
[] = {
261 { .compatible
= "zte,zx296718-pwm", },
264 MODULE_DEVICE_TABLE(of
, zx_pwm_dt_ids
);
266 static struct platform_driver zx_pwm_driver
= {
269 .of_match_table
= zx_pwm_dt_ids
,
271 .probe
= zx_pwm_probe
,
272 .remove
= zx_pwm_remove
,
274 module_platform_driver(zx_pwm_driver
);
276 MODULE_ALIAS("platform:zx-pwm");
277 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
278 MODULE_DESCRIPTION("ZTE ZX PWM Driver");
279 MODULE_LICENSE("GPL v2");