gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / pwm / pwm-jz4740.c
blob3cd5c054ad9aff82c7f8b7465419d9682fbce753
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
4 * JZ4740 platform PWM support
6 * Limitations:
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>
24 #define NUM_PWM 8
26 struct jz4740_pwm_chip {
27 struct pwm_chip chip;
28 struct regmap *map;
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,
37 unsigned int channel)
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",
44 &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);
52 struct clk *clk;
53 char name[16];
54 int err;
56 if (!jz4740_pwm_can_use_chn(jz, pwm->hwpwm))
57 return -EBUSY;
59 snprintf(name, sizeof(name), "timer%u", pwm->hwpwm);
61 clk = clk_get(chip->dev, name);
62 if (IS_ERR(clk)) {
63 if (PTR_ERR(clk) != -EPROBE_DEFER)
64 dev_err(chip->dev, "Failed to get clock: %pe", clk);
66 return PTR_ERR(clk);
69 err = clk_prepare_enable(clk);
70 if (err < 0) {
71 clk_put(clk);
72 return err;
75 pwm_set_chip_data(pwm, clk);
77 return 0;
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);
85 clk_put(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);
96 /* Start counter */
97 regmap_write(jz->map, TCU_REG_TESR, BIT(pwm->hwpwm));
99 return 0;
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),
119 TCU_TCSR_PWM_EN, 0);
121 /* Stop counter */
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;
132 long rate;
133 int err;
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);
153 if (rate < 0) {
154 dev_err(chip->dev, "Unable to round rate: %ld", rate);
155 return 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);
166 duty = period - tmp;
168 if (duty >= period)
169 duty = period - 1;
171 jz4740_pwm_disable(chip, pwm);
173 err = clk_set_rate(clk, rate);
174 if (err) {
175 dev_err(chip->dev, "Unable to set rate: %d", err);
176 return err;
179 /* Reset counter to 0 */
180 regmap_write(jz4740->map, TCU_REG_TCNTc(pwm->hwpwm), 0);
182 /* Set duty */
183 regmap_write(jz4740->map, TCU_REG_TDHRc(pwm->hwpwm), duty);
185 /* Set period */
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);
192 /* Set polarity */
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);
197 break;
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);
202 break;
205 if (state->enabled)
206 jz4740_pwm_enable(chip, pwm);
208 return 0;
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);
224 if (!jz4740)
225 return -ENOMEM;
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);
252 #ifdef CONFIG_OF
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);
258 #endif
260 static struct platform_driver jz4740_pwm_driver = {
261 .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");