Lines Matching +full:pwm +full:- +full:active +full:- +full:state
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
15 #include <linux/mfd/ingenic-tcu.h>
20 #include <linux/pwm.h>
40 /* Enable all TCU channels for PWM use by default except channels 0/1 */ in jz4740_pwm_can_use_chn()
41 u32 pwm_channels_mask = GENMASK(jz->chip.npwm - 1, 2); in jz4740_pwm_can_use_chn()
43 device_property_read_u32(jz->chip.dev->parent, in jz4740_pwm_can_use_chn()
44 "ingenic,pwm-channels-mask", in jz4740_pwm_can_use_chn()
50 static int jz4740_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) in jz4740_pwm_request() argument
57 if (!jz4740_pwm_can_use_chn(jz, pwm->hwpwm)) in jz4740_pwm_request()
58 return -EBUSY; in jz4740_pwm_request()
60 snprintf(name, sizeof(name), "timer%u", pwm->hwpwm); in jz4740_pwm_request()
62 clk = clk_get(chip->dev, name); in jz4740_pwm_request()
64 return dev_err_probe(chip->dev, PTR_ERR(clk), in jz4740_pwm_request()
73 pwm_set_chip_data(pwm, clk); in jz4740_pwm_request()
78 static void jz4740_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) in jz4740_pwm_free() argument
80 struct clk *clk = pwm_get_chip_data(pwm); in jz4740_pwm_free()
86 static int jz4740_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) in jz4740_pwm_enable() argument
90 /* Enable PWM output */ in jz4740_pwm_enable()
91 regmap_update_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm), in jz4740_pwm_enable()
95 regmap_write(jz->map, TCU_REG_TESR, BIT(pwm->hwpwm)); in jz4740_pwm_enable()
100 static void jz4740_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) in jz4740_pwm_disable() argument
108 regmap_write(jz->map, TCU_REG_TDHRc(pwm->hwpwm), 0xffff); in jz4740_pwm_disable()
109 regmap_write(jz->map, TCU_REG_TDFRc(pwm->hwpwm), 0x0); in jz4740_pwm_disable()
112 * Disable PWM output. in jz4740_pwm_disable()
116 regmap_update_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm), in jz4740_pwm_disable()
120 regmap_write(jz->map, TCU_REG_TECR, BIT(pwm->hwpwm)); in jz4740_pwm_disable()
123 static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in jz4740_pwm_apply() argument
124 const struct pwm_state *state) in jz4740_pwm_apply() argument
126 struct jz4740_pwm_chip *jz4740 = to_jz4740(pwm->chip); in jz4740_pwm_apply()
128 struct clk *clk = pwm_get_chip_data(pwm); in jz4740_pwm_apply()
137 do_div(tmp, state->period); in jz4740_pwm_apply()
141 * ------------------- in jz4740_pwm_apply()
147 * round-down function (and won't have one for a while), but if it ever in jz4740_pwm_apply()
148 * comes to light, a round-down function should be used instead. in jz4740_pwm_apply()
152 dev_err(chip->dev, "Unable to round rate: %ld", rate); in jz4740_pwm_apply()
157 tmp = (unsigned long long)rate * state->period; in jz4740_pwm_apply()
162 tmp = (unsigned long long)rate * state->duty_cycle; in jz4740_pwm_apply()
167 duty = period - 1; in jz4740_pwm_apply()
169 jz4740_pwm_disable(chip, pwm); in jz4740_pwm_apply()
173 dev_err(chip->dev, "Unable to set rate: %d", err); in jz4740_pwm_apply()
178 regmap_write(jz4740->map, TCU_REG_TCNTc(pwm->hwpwm), 0); in jz4740_pwm_apply()
181 regmap_write(jz4740->map, TCU_REG_TDHRc(pwm->hwpwm), duty); in jz4740_pwm_apply()
184 regmap_write(jz4740->map, TCU_REG_TDFRc(pwm->hwpwm), period); in jz4740_pwm_apply()
187 regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), in jz4740_pwm_apply()
193 * The PWM starts in inactive state until the internal timer reaches the in jz4740_pwm_apply()
194 * duty value, then becomes active until the timer reaches the period in jz4740_pwm_apply()
195 * value. In theory, we should then use (period - duty) as the real duty in jz4740_pwm_apply()
196 * value, as a high duty value would otherwise result in the PWM pin in jz4740_pwm_apply()
199 * Here, we don't do that, and instead invert the polarity of the PWM in jz4740_pwm_apply()
200 * when it is active. This trick makes the PWM start with its active in jz4740_pwm_apply()
201 * state instead of its inactive state. in jz4740_pwm_apply()
203 if ((state->polarity == PWM_POLARITY_NORMAL) ^ state->enabled) in jz4740_pwm_apply()
204 regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), in jz4740_pwm_apply()
207 regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), in jz4740_pwm_apply()
211 if (state->enabled) in jz4740_pwm_apply()
212 jz4740_pwm_enable(chip, pwm); in jz4740_pwm_apply()
226 struct device *dev = &pdev->dev; in jz4740_pwm_probe()
232 return -EINVAL; in jz4740_pwm_probe()
236 return -ENOMEM; in jz4740_pwm_probe()
238 jz4740->map = device_node_to_regmap(dev->parent->of_node); in jz4740_pwm_probe()
239 if (IS_ERR(jz4740->map)) { in jz4740_pwm_probe()
240 dev_err(dev, "regmap not found: %ld\n", PTR_ERR(jz4740->map)); in jz4740_pwm_probe()
241 return PTR_ERR(jz4740->map); in jz4740_pwm_probe()
244 jz4740->chip.dev = dev; in jz4740_pwm_probe()
245 jz4740->chip.ops = &jz4740_pwm_ops; in jz4740_pwm_probe()
246 jz4740->chip.npwm = info->num_pwms; in jz4740_pwm_probe()
248 return devm_pwmchip_add(dev, &jz4740->chip); in jz4740_pwm_probe()
265 { .compatible = "ingenic,jz4740-pwm", .data = &jz4740_soc_info },
266 { .compatible = "ingenic,jz4725b-pwm", .data = &jz4725b_soc_info },
267 { .compatible = "ingenic,x1000-pwm", .data = &x1000_soc_info },
275 .name = "jz4740-pwm",
282 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
283 MODULE_DESCRIPTION("Ingenic JZ4740 PWM driver");
284 MODULE_ALIAS("platform:jz4740-pwm");