1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
4 * JZ4740 platform PWM support
7 * - The .apply callback doesn't complete the currently running period before
8 * reconfiguring the hardware.
9 * - Each period starts with the inactive part.
12 #include <linux/clk.h>
13 #include <linux/err.h>
14 #include <linux/gpio.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/ingenic-tcu.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/pwm.h>
22 #include <linux/regmap.h>
26 struct jz4740_pwm_chip
{
31 static inline struct jz4740_pwm_chip
*to_jz4740(struct pwm_chip
*chip
)
33 return container_of(chip
, struct jz4740_pwm_chip
, chip
);
36 static bool jz4740_pwm_can_use_chn(struct jz4740_pwm_chip
*jz
,
39 /* Enable all TCU channels for PWM use by default except channels 0/1 */
40 u32 pwm_channels_mask
= GENMASK(NUM_PWM
- 1, 2);
42 device_property_read_u32(jz
->chip
.dev
->parent
,
43 "ingenic,pwm-channels-mask",
46 return !!(pwm_channels_mask
& BIT(channel
));
49 static int jz4740_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
51 struct jz4740_pwm_chip
*jz
= to_jz4740(chip
);
56 if (!jz4740_pwm_can_use_chn(jz
, pwm
->hwpwm
))
59 snprintf(name
, sizeof(name
), "timer%u", pwm
->hwpwm
);
61 clk
= clk_get(chip
->dev
, name
);
63 if (PTR_ERR(clk
) != -EPROBE_DEFER
)
64 dev_err(chip
->dev
, "Failed to get clock: %pe", clk
);
69 err
= clk_prepare_enable(clk
);
75 pwm_set_chip_data(pwm
, clk
);
80 static void jz4740_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
82 struct clk
*clk
= pwm_get_chip_data(pwm
);
84 clk_disable_unprepare(clk
);
88 static int jz4740_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
90 struct jz4740_pwm_chip
*jz
= to_jz4740(chip
);
92 /* Enable PWM output */
93 regmap_update_bits(jz
->map
, TCU_REG_TCSRc(pwm
->hwpwm
),
94 TCU_TCSR_PWM_EN
, TCU_TCSR_PWM_EN
);
97 regmap_write(jz
->map
, TCU_REG_TESR
, BIT(pwm
->hwpwm
));
102 static void jz4740_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
104 struct jz4740_pwm_chip
*jz
= to_jz4740(chip
);
107 * Set duty > period. This trick allows the TCU channels in TCU2 mode to
108 * properly return to their init level.
110 regmap_write(jz
->map
, TCU_REG_TDHRc(pwm
->hwpwm
), 0xffff);
111 regmap_write(jz
->map
, TCU_REG_TDFRc(pwm
->hwpwm
), 0x0);
114 * Disable PWM output.
115 * In TCU2 mode (channel 1/2 on JZ4750+), this must be done before the
116 * counter is stopped, while in TCU1 mode the order does not matter.
118 regmap_update_bits(jz
->map
, TCU_REG_TCSRc(pwm
->hwpwm
),
122 regmap_write(jz
->map
, TCU_REG_TECR
, BIT(pwm
->hwpwm
));
125 static int jz4740_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
126 const struct pwm_state
*state
)
128 struct jz4740_pwm_chip
*jz4740
= to_jz4740(pwm
->chip
);
129 unsigned long long tmp
= 0xffffull
* NSEC_PER_SEC
;
130 struct clk
*clk
= pwm_get_chip_data(pwm
);
131 unsigned long period
, duty
;
136 * Limit the clock to a maximum rate that still gives us a period value
137 * which fits in 16 bits.
139 do_div(tmp
, state
->period
);
142 * /!\ IMPORTANT NOTE:
143 * -------------------
144 * This code relies on the fact that clk_round_rate() will always round
145 * down, which is not a valid assumption given by the clk API, but only
146 * happens to be true with the clk drivers used for Ingenic SoCs.
148 * Right now, there is no alternative as the clk API does not have a
149 * round-down function (and won't have one for a while), but if it ever
150 * comes to light, a round-down function should be used instead.
152 rate
= clk_round_rate(clk
, tmp
);
154 dev_err(chip
->dev
, "Unable to round rate: %ld", rate
);
158 /* Calculate period value */
159 tmp
= (unsigned long long)rate
* state
->period
;
160 do_div(tmp
, NSEC_PER_SEC
);
161 period
= (unsigned long)tmp
;
163 /* Calculate duty value */
164 tmp
= (unsigned long long)period
* state
->duty_cycle
;
165 do_div(tmp
, state
->period
);
171 jz4740_pwm_disable(chip
, pwm
);
173 err
= clk_set_rate(clk
, rate
);
175 dev_err(chip
->dev
, "Unable to set rate: %d", err
);
179 /* Reset counter to 0 */
180 regmap_write(jz4740
->map
, TCU_REG_TCNTc(pwm
->hwpwm
), 0);
183 regmap_write(jz4740
->map
, TCU_REG_TDHRc(pwm
->hwpwm
), duty
);
186 regmap_write(jz4740
->map
, TCU_REG_TDFRc(pwm
->hwpwm
), period
);
188 /* Set abrupt shutdown */
189 regmap_update_bits(jz4740
->map
, TCU_REG_TCSRc(pwm
->hwpwm
),
190 TCU_TCSR_PWM_SD
, TCU_TCSR_PWM_SD
);
193 switch (state
->polarity
) {
194 case PWM_POLARITY_NORMAL
:
195 regmap_update_bits(jz4740
->map
, TCU_REG_TCSRc(pwm
->hwpwm
),
196 TCU_TCSR_PWM_INITL_HIGH
, 0);
198 case PWM_POLARITY_INVERSED
:
199 regmap_update_bits(jz4740
->map
, TCU_REG_TCSRc(pwm
->hwpwm
),
200 TCU_TCSR_PWM_INITL_HIGH
,
201 TCU_TCSR_PWM_INITL_HIGH
);
206 jz4740_pwm_enable(chip
, pwm
);
211 static const struct pwm_ops jz4740_pwm_ops
= {
212 .request
= jz4740_pwm_request
,
213 .free
= jz4740_pwm_free
,
214 .apply
= jz4740_pwm_apply
,
215 .owner
= THIS_MODULE
,
218 static int jz4740_pwm_probe(struct platform_device
*pdev
)
220 struct device
*dev
= &pdev
->dev
;
221 struct jz4740_pwm_chip
*jz4740
;
223 jz4740
= devm_kzalloc(dev
, sizeof(*jz4740
), GFP_KERNEL
);
227 jz4740
->map
= device_node_to_regmap(dev
->parent
->of_node
);
228 if (IS_ERR(jz4740
->map
)) {
229 dev_err(dev
, "regmap not found: %ld\n", PTR_ERR(jz4740
->map
));
230 return PTR_ERR(jz4740
->map
);
233 jz4740
->chip
.dev
= dev
;
234 jz4740
->chip
.ops
= &jz4740_pwm_ops
;
235 jz4740
->chip
.npwm
= NUM_PWM
;
236 jz4740
->chip
.base
= -1;
237 jz4740
->chip
.of_xlate
= of_pwm_xlate_with_flags
;
238 jz4740
->chip
.of_pwm_n_cells
= 3;
240 platform_set_drvdata(pdev
, jz4740
);
242 return pwmchip_add(&jz4740
->chip
);
245 static int jz4740_pwm_remove(struct platform_device
*pdev
)
247 struct jz4740_pwm_chip
*jz4740
= platform_get_drvdata(pdev
);
249 return pwmchip_remove(&jz4740
->chip
);
253 static const struct of_device_id jz4740_pwm_dt_ids
[] = {
254 { .compatible
= "ingenic,jz4740-pwm", },
257 MODULE_DEVICE_TABLE(of
, jz4740_pwm_dt_ids
);
260 static struct platform_driver jz4740_pwm_driver
= {
262 .name
= "jz4740-pwm",
263 .of_match_table
= of_match_ptr(jz4740_pwm_dt_ids
),
265 .probe
= jz4740_pwm_probe
,
266 .remove
= jz4740_pwm_remove
,
268 module_platform_driver(jz4740_pwm_driver
);
270 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
271 MODULE_DESCRIPTION("Ingenic JZ4740 PWM driver");
272 MODULE_ALIAS("platform:jz4740-pwm");
273 MODULE_LICENSE("GPL");