2 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
3 * Copyright 2017 Linaro Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/clk.h>
11 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pwm.h>
17 #include <linux/slab.h>
19 #define ZX_PWM_MODE 0x0
20 #define ZX_PWM_CLKDIV_SHIFT 2
21 #define ZX_PWM_CLKDIV_MASK GENMASK(11, 2)
22 #define ZX_PWM_CLKDIV(x) (((x) << ZX_PWM_CLKDIV_SHIFT) & \
24 #define ZX_PWM_POLAR BIT(1)
25 #define ZX_PWM_EN BIT(0)
26 #define ZX_PWM_PERIOD 0x4
27 #define ZX_PWM_DUTY 0x8
29 #define ZX_PWM_CLKDIV_MAX 1023
30 #define ZX_PWM_PERIOD_MAX 65535
39 static inline struct zx_pwm_chip
*to_zx_pwm_chip(struct pwm_chip
*chip
)
41 return container_of(chip
, struct zx_pwm_chip
, chip
);
44 static inline u32
zx_pwm_readl(struct zx_pwm_chip
*zpc
, unsigned int hwpwm
,
47 return readl(zpc
->base
+ (hwpwm
+ 1) * 0x10 + offset
);
50 static inline void zx_pwm_writel(struct zx_pwm_chip
*zpc
, unsigned int hwpwm
,
51 unsigned int offset
, u32 value
)
53 writel(value
, zpc
->base
+ (hwpwm
+ 1) * 0x10 + offset
);
56 static void zx_pwm_set_mask(struct zx_pwm_chip
*zpc
, unsigned int hwpwm
,
57 unsigned int offset
, u32 mask
, u32 value
)
61 data
= zx_pwm_readl(zpc
, hwpwm
, offset
);
64 zx_pwm_writel(zpc
, hwpwm
, offset
, data
);
67 static void zx_pwm_get_state(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
68 struct pwm_state
*state
)
70 struct zx_pwm_chip
*zpc
= to_zx_pwm_chip(chip
);
76 value
= zx_pwm_readl(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
);
78 if (value
& ZX_PWM_POLAR
)
79 state
->polarity
= PWM_POLARITY_NORMAL
;
81 state
->polarity
= PWM_POLARITY_INVERSED
;
83 if (value
& ZX_PWM_EN
)
84 state
->enabled
= true;
86 state
->enabled
= false;
88 div
= (value
& ZX_PWM_CLKDIV_MASK
) >> ZX_PWM_CLKDIV_SHIFT
;
89 rate
= clk_get_rate(zpc
->wclk
);
91 tmp
= zx_pwm_readl(zpc
, pwm
->hwpwm
, ZX_PWM_PERIOD
);
92 tmp
*= div
* NSEC_PER_SEC
;
93 state
->period
= DIV_ROUND_CLOSEST_ULL(tmp
, rate
);
95 tmp
= zx_pwm_readl(zpc
, pwm
->hwpwm
, ZX_PWM_DUTY
);
96 tmp
*= div
* NSEC_PER_SEC
;
97 state
->duty_cycle
= DIV_ROUND_CLOSEST_ULL(tmp
, rate
);
100 static int zx_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
101 unsigned int duty_ns
, unsigned int period_ns
)
103 struct zx_pwm_chip
*zpc
= to_zx_pwm_chip(chip
);
104 unsigned int period_cycles
, duty_cycles
;
105 unsigned long long c
;
106 unsigned int div
= 1;
109 /* Find out the best divider */
110 rate
= clk_get_rate(zpc
->wclk
);
115 do_div(c
, NSEC_PER_SEC
);
117 if (c
< ZX_PWM_PERIOD_MAX
)
122 if (div
> ZX_PWM_CLKDIV_MAX
)
126 /* Calculate duty cycles */
129 do_div(c
, period_ns
);
133 * If the PWM is being enabled, we have to temporarily disable it
134 * before configuring the registers.
136 if (pwm_is_enabled(pwm
))
137 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
, ZX_PWM_EN
, 0);
139 /* Set up registers */
140 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
, ZX_PWM_CLKDIV_MASK
,
142 zx_pwm_writel(zpc
, pwm
->hwpwm
, ZX_PWM_PERIOD
, period_cycles
);
143 zx_pwm_writel(zpc
, pwm
->hwpwm
, ZX_PWM_DUTY
, duty_cycles
);
145 /* Re-enable the PWM if needed */
146 if (pwm_is_enabled(pwm
))
147 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
,
148 ZX_PWM_EN
, ZX_PWM_EN
);
153 static int zx_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
154 struct pwm_state
*state
)
156 struct zx_pwm_chip
*zpc
= to_zx_pwm_chip(chip
);
157 struct pwm_state cstate
;
160 pwm_get_state(pwm
, &cstate
);
162 if (state
->polarity
!= cstate
.polarity
)
163 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
, ZX_PWM_POLAR
,
164 (state
->polarity
== PWM_POLARITY_INVERSED
) ?
167 if (state
->period
!= cstate
.period
||
168 state
->duty_cycle
!= cstate
.duty_cycle
) {
169 ret
= zx_pwm_config(chip
, pwm
, state
->duty_cycle
,
175 if (state
->enabled
!= cstate
.enabled
) {
176 if (state
->enabled
) {
177 ret
= clk_prepare_enable(zpc
->wclk
);
181 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
,
182 ZX_PWM_EN
, ZX_PWM_EN
);
184 zx_pwm_set_mask(zpc
, pwm
->hwpwm
, ZX_PWM_MODE
,
186 clk_disable_unprepare(zpc
->wclk
);
193 static const struct pwm_ops zx_pwm_ops
= {
194 .apply
= zx_pwm_apply
,
195 .get_state
= zx_pwm_get_state
,
196 .owner
= THIS_MODULE
,
199 static int zx_pwm_probe(struct platform_device
*pdev
)
201 struct zx_pwm_chip
*zpc
;
202 struct resource
*res
;
206 zpc
= devm_kzalloc(&pdev
->dev
, sizeof(*zpc
), GFP_KERNEL
);
210 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
211 zpc
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
212 if (IS_ERR(zpc
->base
))
213 return PTR_ERR(zpc
->base
);
215 zpc
->pclk
= devm_clk_get(&pdev
->dev
, "pclk");
216 if (IS_ERR(zpc
->pclk
))
217 return PTR_ERR(zpc
->pclk
);
219 zpc
->wclk
= devm_clk_get(&pdev
->dev
, "wclk");
220 if (IS_ERR(zpc
->wclk
))
221 return PTR_ERR(zpc
->wclk
);
223 ret
= clk_prepare_enable(zpc
->pclk
);
227 zpc
->chip
.dev
= &pdev
->dev
;
228 zpc
->chip
.ops
= &zx_pwm_ops
;
231 zpc
->chip
.of_xlate
= of_pwm_xlate_with_flags
;
232 zpc
->chip
.of_pwm_n_cells
= 3;
235 * PWM devices may be enabled by firmware, and let's disable all of
236 * them initially to save power.
238 for (i
= 0; i
< zpc
->chip
.npwm
; i
++)
239 zx_pwm_set_mask(zpc
, i
, ZX_PWM_MODE
, ZX_PWM_EN
, 0);
241 ret
= pwmchip_add(&zpc
->chip
);
243 dev_err(&pdev
->dev
, "failed to add PWM chip: %d\n", ret
);
247 platform_set_drvdata(pdev
, zpc
);
252 static int zx_pwm_remove(struct platform_device
*pdev
)
254 struct zx_pwm_chip
*zpc
= platform_get_drvdata(pdev
);
257 ret
= pwmchip_remove(&zpc
->chip
);
258 clk_disable_unprepare(zpc
->pclk
);
263 static const struct of_device_id zx_pwm_dt_ids
[] = {
264 { .compatible
= "zte,zx296718-pwm", },
267 MODULE_DEVICE_TABLE(of
, zx_pwm_dt_ids
);
269 static struct platform_driver zx_pwm_driver
= {
272 .of_match_table
= zx_pwm_dt_ids
,
274 .probe
= zx_pwm_probe
,
275 .remove
= zx_pwm_remove
,
277 module_platform_driver(zx_pwm_driver
);
279 MODULE_ALIAS("platform:zx-pwm");
280 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
281 MODULE_DESCRIPTION("ZTE ZX PWM Driver");
282 MODULE_LICENSE("GPL v2");