Thomas Gleixner | a912e80 | 2019-05-27 08:55:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de> |
| 4 | * JZ4740 platform PWM support |
Uwe Kleine-König | 3b442c6 | 2019-07-30 14:32:29 +0200 | [diff] [blame] | 5 | * |
| 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. |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/clk.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/gpio.h> |
| 15 | #include <linux/kernel.h> |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 16 | #include <linux/mfd/ingenic-tcu.h> |
| 17 | #include <linux/mfd/syscon.h> |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 18 | #include <linux/module.h> |
Paul Cercueil | cc20173 | 2018-01-06 17:58:42 +0100 | [diff] [blame] | 19 | #include <linux/of_device.h> |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/pwm.h> |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 22 | #include <linux/regmap.h> |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 23 | |
| 24 | #define NUM_PWM 8 |
| 25 | |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 26 | struct jz4740_pwm_chip { |
| 27 | struct pwm_chip chip; |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 28 | struct regmap *map; |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static inline struct jz4740_pwm_chip *to_jz4740(struct pwm_chip *chip) |
| 32 | { |
| 33 | return container_of(chip, struct jz4740_pwm_chip, chip); |
| 34 | } |
| 35 | |
Paul Cercueil | a2005fc | 2020-03-23 15:24:21 +0100 | [diff] [blame] | 36 | static bool jz4740_pwm_can_use_chn(struct jz4740_pwm_chip *jz, |
| 37 | unsigned int channel) |
| 38 | { |
| 39 | /* Enable all TCU channels for PWM use by default except channels 0/1 */ |
| 40 | u32 pwm_channels_mask = GENMASK(NUM_PWM - 1, 2); |
| 41 | |
| 42 | device_property_read_u32(jz->chip.dev->parent, |
| 43 | "ingenic,pwm-channels-mask", |
| 44 | &pwm_channels_mask); |
| 45 | |
| 46 | return !!(pwm_channels_mask & BIT(channel)); |
| 47 | } |
| 48 | |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 49 | static int jz4740_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) |
| 50 | { |
Paul Cercueil | ce1f9ce | 2020-03-23 15:24:18 +0100 | [diff] [blame] | 51 | struct jz4740_pwm_chip *jz = to_jz4740(chip); |
| 52 | struct clk *clk; |
| 53 | char name[16]; |
| 54 | int err; |
| 55 | |
Paul Cercueil | a2005fc | 2020-03-23 15:24:21 +0100 | [diff] [blame] | 56 | if (!jz4740_pwm_can_use_chn(jz, pwm->hwpwm)) |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 57 | return -EBUSY; |
| 58 | |
Paul Cercueil | ce1f9ce | 2020-03-23 15:24:18 +0100 | [diff] [blame] | 59 | snprintf(name, sizeof(name), "timer%u", pwm->hwpwm); |
| 60 | |
| 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); |
| 65 | |
| 66 | return PTR_ERR(clk); |
| 67 | } |
| 68 | |
| 69 | err = clk_prepare_enable(clk); |
| 70 | if (err < 0) { |
| 71 | clk_put(clk); |
| 72 | return err; |
| 73 | } |
| 74 | |
| 75 | pwm_set_chip_data(pwm, clk); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static void jz4740_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) |
| 81 | { |
Paul Cercueil | ce1f9ce | 2020-03-23 15:24:18 +0100 | [diff] [blame] | 82 | struct clk *clk = pwm_get_chip_data(pwm); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 83 | |
Paul Cercueil | ce1f9ce | 2020-03-23 15:24:18 +0100 | [diff] [blame] | 84 | clk_disable_unprepare(clk); |
| 85 | clk_put(clk); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static int jz4740_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 89 | { |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 90 | struct jz4740_pwm_chip *jz = to_jz4740(chip); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 91 | |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 92 | /* Enable PWM output */ |
| 93 | regmap_update_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm), |
| 94 | TCU_TCSR_PWM_EN, TCU_TCSR_PWM_EN); |
| 95 | |
| 96 | /* Start counter */ |
| 97 | regmap_write(jz->map, TCU_REG_TESR, BIT(pwm->hwpwm)); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static void jz4740_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 103 | { |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 104 | struct jz4740_pwm_chip *jz = to_jz4740(chip); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 105 | |
Paul Cercueil | 6580fd1 | 2019-06-07 17:44:09 +0200 | [diff] [blame] | 106 | /* |
| 107 | * Set duty > period. This trick allows the TCU channels in TCU2 mode to |
| 108 | * properly return to their init level. |
| 109 | */ |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 110 | regmap_write(jz->map, TCU_REG_TDHRc(pwm->hwpwm), 0xffff); |
| 111 | regmap_write(jz->map, TCU_REG_TDFRc(pwm->hwpwm), 0x0); |
Paul Cercueil | 6580fd1 | 2019-06-07 17:44:09 +0200 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * Disable PWM output. |
Maarten ter Huurne | df56b17 | 2018-01-06 17:58:40 +0100 | [diff] [blame] | 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. |
| 117 | */ |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 118 | regmap_update_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm), |
| 119 | TCU_TCSR_PWM_EN, 0); |
Maarten ter Huurne | df56b17 | 2018-01-06 17:58:40 +0100 | [diff] [blame] | 120 | |
| 121 | /* Stop counter */ |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 122 | regmap_write(jz->map, TCU_REG_TECR, BIT(pwm->hwpwm)); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Paul Cercueil | 1ac99c5 | 2019-06-07 17:44:07 +0200 | [diff] [blame] | 125 | static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
Uwe Kleine-König | 71523d1 | 2019-08-24 17:37:07 +0200 | [diff] [blame] | 126 | const struct pwm_state *state) |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 127 | { |
| 128 | struct jz4740_pwm_chip *jz4740 = to_jz4740(pwm->chip); |
Paul Cercueil | 485b56f | 2020-03-23 15:24:19 +0100 | [diff] [blame] | 129 | unsigned long long tmp = 0xffffull * NSEC_PER_SEC; |
| 130 | struct clk *clk = pwm_get_chip_data(pwm); |
| 131 | unsigned long period, duty; |
Paul Cercueil | 485b56f | 2020-03-23 15:24:19 +0100 | [diff] [blame] | 132 | long rate; |
Paul Cercueil | ce1f9ce | 2020-03-23 15:24:18 +0100 | [diff] [blame] | 133 | int err; |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 134 | |
Paul Cercueil | 485b56f | 2020-03-23 15:24:19 +0100 | [diff] [blame] | 135 | /* |
| 136 | * Limit the clock to a maximum rate that still gives us a period value |
| 137 | * which fits in 16 bits. |
| 138 | */ |
| 139 | do_div(tmp, state->period); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 140 | |
Paul Cercueil | 485b56f | 2020-03-23 15:24:19 +0100 | [diff] [blame] | 141 | /* |
| 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. |
| 147 | * |
| 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. |
| 151 | */ |
| 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; |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Paul Cercueil | 485b56f | 2020-03-23 15:24:19 +0100 | [diff] [blame] | 158 | /* Calculate period value */ |
| 159 | tmp = (unsigned long long)rate * state->period; |
| 160 | do_div(tmp, NSEC_PER_SEC); |
| 161 | period = (unsigned long)tmp; |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 162 | |
Paul Cercueil | 485b56f | 2020-03-23 15:24:19 +0100 | [diff] [blame] | 163 | /* Calculate duty value */ |
Paul Cercueil | 1ac99c5 | 2019-06-07 17:44:07 +0200 | [diff] [blame] | 164 | tmp = (unsigned long long)period * state->duty_cycle; |
| 165 | do_div(tmp, state->period); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 166 | duty = period - tmp; |
| 167 | |
| 168 | if (duty >= period) |
| 169 | duty = period - 1; |
| 170 | |
Paul Cercueil | 1ac99c5 | 2019-06-07 17:44:07 +0200 | [diff] [blame] | 171 | jz4740_pwm_disable(chip, pwm); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 172 | |
Paul Cercueil | ce1f9ce | 2020-03-23 15:24:18 +0100 | [diff] [blame] | 173 | err = clk_set_rate(clk, rate); |
| 174 | if (err) { |
| 175 | dev_err(chip->dev, "Unable to set rate: %d", err); |
| 176 | return err; |
| 177 | } |
| 178 | |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 179 | /* Reset counter to 0 */ |
| 180 | regmap_write(jz4740->map, TCU_REG_TCNTc(pwm->hwpwm), 0); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 181 | |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 182 | /* Set duty */ |
| 183 | regmap_write(jz4740->map, TCU_REG_TDHRc(pwm->hwpwm), duty); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 184 | |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 185 | /* Set period */ |
| 186 | regmap_write(jz4740->map, TCU_REG_TDFRc(pwm->hwpwm), period); |
| 187 | |
| 188 | /* Set abrupt shutdown */ |
| 189 | regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), |
| 190 | TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD); |
| 191 | |
| 192 | /* Set polarity */ |
Paul Cercueil | 1ac99c5 | 2019-06-07 17:44:07 +0200 | [diff] [blame] | 193 | switch (state->polarity) { |
Paul Cercueil | 174dcc8 | 2018-01-06 17:58:41 +0100 | [diff] [blame] | 194 | case PWM_POLARITY_NORMAL: |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 195 | regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), |
| 196 | TCU_TCSR_PWM_INITL_HIGH, 0); |
Paul Cercueil | 174dcc8 | 2018-01-06 17:58:41 +0100 | [diff] [blame] | 197 | break; |
| 198 | case PWM_POLARITY_INVERSED: |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 199 | regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), |
| 200 | TCU_TCSR_PWM_INITL_HIGH, |
| 201 | TCU_TCSR_PWM_INITL_HIGH); |
Paul Cercueil | 174dcc8 | 2018-01-06 17:58:41 +0100 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | |
Paul Cercueil | 1ac99c5 | 2019-06-07 17:44:07 +0200 | [diff] [blame] | 205 | if (state->enabled) |
| 206 | jz4740_pwm_enable(chip, pwm); |
| 207 | |
Paul Cercueil | 174dcc8 | 2018-01-06 17:58:41 +0100 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 211 | static const struct pwm_ops jz4740_pwm_ops = { |
| 212 | .request = jz4740_pwm_request, |
| 213 | .free = jz4740_pwm_free, |
Paul Cercueil | 1ac99c5 | 2019-06-07 17:44:07 +0200 | [diff] [blame] | 214 | .apply = jz4740_pwm_apply, |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 215 | .owner = THIS_MODULE, |
| 216 | }; |
| 217 | |
Bill Pemberton | 3e9fe83 | 2012-11-19 13:23:14 -0500 | [diff] [blame] | 218 | static int jz4740_pwm_probe(struct platform_device *pdev) |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 219 | { |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 220 | struct device *dev = &pdev->dev; |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 221 | struct jz4740_pwm_chip *jz4740; |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 222 | |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 223 | jz4740 = devm_kzalloc(dev, sizeof(*jz4740), GFP_KERNEL); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 224 | if (!jz4740) |
| 225 | return -ENOMEM; |
| 226 | |
Paul Cercueil | c269351 | 2020-03-23 15:24:20 +0100 | [diff] [blame] | 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); |
| 231 | } |
| 232 | |
| 233 | jz4740->chip.dev = dev; |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 234 | jz4740->chip.ops = &jz4740_pwm_ops; |
| 235 | jz4740->chip.npwm = NUM_PWM; |
| 236 | jz4740->chip.base = -1; |
Paul Cercueil | cc20173 | 2018-01-06 17:58:42 +0100 | [diff] [blame] | 237 | jz4740->chip.of_xlate = of_pwm_xlate_with_flags; |
| 238 | jz4740->chip.of_pwm_n_cells = 3; |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 239 | |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 240 | platform_set_drvdata(pdev, jz4740); |
| 241 | |
Lars-Peter Clausen | 0dc1135 | 2013-12-07 18:13:16 +0100 | [diff] [blame] | 242 | return pwmchip_add(&jz4740->chip); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Bill Pemberton | 77f3791 | 2012-11-19 13:26:09 -0500 | [diff] [blame] | 245 | static int jz4740_pwm_remove(struct platform_device *pdev) |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 246 | { |
| 247 | struct jz4740_pwm_chip *jz4740 = platform_get_drvdata(pdev); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 248 | |
Lars-Peter Clausen | 0dc1135 | 2013-12-07 18:13:16 +0100 | [diff] [blame] | 249 | return pwmchip_remove(&jz4740->chip); |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Paul Cercueil | cc20173 | 2018-01-06 17:58:42 +0100 | [diff] [blame] | 252 | #ifdef CONFIG_OF |
| 253 | static const struct of_device_id jz4740_pwm_dt_ids[] = { |
| 254 | { .compatible = "ingenic,jz4740-pwm", }, |
Paul Cercueil | cc20173 | 2018-01-06 17:58:42 +0100 | [diff] [blame] | 255 | {}, |
| 256 | }; |
| 257 | MODULE_DEVICE_TABLE(of, jz4740_pwm_dt_ids); |
| 258 | #endif |
| 259 | |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 260 | static struct platform_driver jz4740_pwm_driver = { |
| 261 | .driver = { |
| 262 | .name = "jz4740-pwm", |
Paul Cercueil | cc20173 | 2018-01-06 17:58:42 +0100 | [diff] [blame] | 263 | .of_match_table = of_match_ptr(jz4740_pwm_dt_ids), |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 264 | }, |
| 265 | .probe = jz4740_pwm_probe, |
Bill Pemberton | fd10911 | 2012-11-19 13:21:28 -0500 | [diff] [blame] | 266 | .remove = jz4740_pwm_remove, |
Thierry Reding | f6b8a57 | 2012-08-22 10:01:24 +0200 | [diff] [blame] | 267 | }; |
| 268 | module_platform_driver(jz4740_pwm_driver); |
| 269 | |
| 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"); |