Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | // SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2019 Spreadtrum Communications Inc. */ #include <linux/clk.h> #include <linux/err.h> #include <linux/io.h> #include <linux/math64.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/pwm.h> #define SPRD_PWM_PRESCALE 0x0 #define SPRD_PWM_MOD 0x4 #define SPRD_PWM_DUTY 0x8 #define SPRD_PWM_ENABLE 0x18 #define SPRD_PWM_MOD_MAX GENMASK(7, 0) #define SPRD_PWM_DUTY_MSK GENMASK(15, 0) #define SPRD_PWM_PRESCALE_MSK GENMASK(7, 0) #define SPRD_PWM_ENABLE_BIT BIT(0) #define SPRD_PWM_CHN_NUM 4 #define SPRD_PWM_REGS_SHIFT 5 #define SPRD_PWM_CHN_CLKS_NUM 2 #define SPRD_PWM_CHN_OUTPUT_CLK 1 struct sprd_pwm_chn { struct clk_bulk_data clks[SPRD_PWM_CHN_CLKS_NUM]; u32 clk_rate; }; struct sprd_pwm_chip { void __iomem *base; struct device *dev; struct pwm_chip chip; int num_pwms; struct sprd_pwm_chn chn[SPRD_PWM_CHN_NUM]; }; /* * The list of clocks required by PWM channels, and each channel has 2 clocks: * enable clock and pwm clock. */ static const char * const sprd_pwm_clks[] = { "enable0", "pwm0", "enable1", "pwm1", "enable2", "pwm2", "enable3", "pwm3", }; static u32 sprd_pwm_read(struct sprd_pwm_chip *spc, u32 hwid, u32 reg) { u32 offset = reg + (hwid << SPRD_PWM_REGS_SHIFT); return readl_relaxed(spc->base + offset); } static void sprd_pwm_write(struct sprd_pwm_chip *spc, u32 hwid, u32 reg, u32 val) { u32 offset = reg + (hwid << SPRD_PWM_REGS_SHIFT); writel_relaxed(val, spc->base + offset); } static void sprd_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_state *state) { struct sprd_pwm_chip *spc = container_of(chip, struct sprd_pwm_chip, chip); struct sprd_pwm_chn *chn = &spc->chn[pwm->hwpwm]; u32 val, duty, prescale; u64 tmp; int ret; /* * The clocks to PWM channel has to be enabled first before * reading to the registers. */ ret = clk_bulk_prepare_enable(SPRD_PWM_CHN_CLKS_NUM, chn->clks); if (ret) { dev_err(spc->dev, "failed to enable pwm%u clocks\n", pwm->hwpwm); return; } val = sprd_pwm_read(spc, pwm->hwpwm, SPRD_PWM_ENABLE); if (val & SPRD_PWM_ENABLE_BIT) state->enabled = true; else state->enabled = false; /* * The hardware provides a counter that is feed by the source clock. * The period length is (PRESCALE + 1) * MOD counter steps. * The duty cycle length is (PRESCALE + 1) * DUTY counter steps. * Thus the period_ns and duty_ns calculation formula should be: * period_ns = NSEC_PER_SEC * (prescale + 1) * mod / clk_rate * duty_ns = NSEC_PER_SEC * (prescale + 1) * duty / clk_rate */ val = sprd_pwm_read(spc, pwm->hwpwm, SPRD_PWM_PRESCALE); prescale = val & SPRD_PWM_PRESCALE_MSK; tmp = (prescale + 1) * NSEC_PER_SEC * SPRD_PWM_MOD_MAX; state->period = DIV_ROUND_CLOSEST_ULL(tmp, chn->clk_rate); val = sprd_pwm_read(spc, pwm->hwpwm, SPRD_PWM_DUTY); duty = val & SPRD_PWM_DUTY_MSK; tmp = (prescale + 1) * NSEC_PER_SEC * duty; state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, chn->clk_rate); state->polarity = PWM_POLARITY_NORMAL; /* Disable PWM clocks if the PWM channel is not in enable state. */ if (!state->enabled) clk_bulk_disable_unprepare(SPRD_PWM_CHN_CLKS_NUM, chn->clks); } static int sprd_pwm_config(struct sprd_pwm_chip *spc, struct pwm_device *pwm, int duty_ns, int period_ns) { struct sprd_pwm_chn *chn = &spc->chn[pwm->hwpwm]; u32 prescale, duty; u64 tmp; /* * The hardware provides a counter that is feed by the source clock. * The period length is (PRESCALE + 1) * MOD counter steps. * The duty cycle length is (PRESCALE + 1) * DUTY counter steps. * * To keep the maths simple we're always using MOD = SPRD_PWM_MOD_MAX. * The value for PRESCALE is selected such that the resulting period * gets the maximal length not bigger than the requested one with the * given settings (MOD = SPRD_PWM_MOD_MAX and input clock). */ duty = duty_ns * SPRD_PWM_MOD_MAX / period_ns; tmp = (u64)chn->clk_rate * period_ns; do_div(tmp, NSEC_PER_SEC); prescale = DIV_ROUND_CLOSEST_ULL(tmp, SPRD_PWM_MOD_MAX) - 1; if (prescale > SPRD_PWM_PRESCALE_MSK) prescale = SPRD_PWM_PRESCALE_MSK; /* * Note: Writing DUTY triggers the hardware to actually apply the * values written to MOD and DUTY to the output, so must keep writing * DUTY last. * * The hardware can ensures that current running period is completed * before changing a new configuration to avoid mixed settings. */ sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_PRESCALE, prescale); sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_MOD, SPRD_PWM_MOD_MAX); sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_DUTY, duty); return 0; } static int sprd_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_state *state) { struct sprd_pwm_chip *spc = container_of(chip, struct sprd_pwm_chip, chip); struct sprd_pwm_chn *chn = &spc->chn[pwm->hwpwm]; struct pwm_state *cstate = &pwm->state; int ret; if (state->polarity != PWM_POLARITY_NORMAL) return -EINVAL; if (state->enabled) { if (!cstate->enabled) { /* * The clocks to PWM channel has to be enabled first * before writing to the registers. */ ret = clk_bulk_prepare_enable(SPRD_PWM_CHN_CLKS_NUM, chn->clks); if (ret) { dev_err(spc->dev, "failed to enable pwm%u clocks\n", pwm->hwpwm); return ret; } } ret = sprd_pwm_config(spc, pwm, state->duty_cycle, state->period); if (ret) return ret; sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_ENABLE, 1); } else if (cstate->enabled) { /* * Note: After setting SPRD_PWM_ENABLE to zero, the controller * will not wait for current period to be completed, instead it * will stop the PWM channel immediately. */ sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_ENABLE, 0); clk_bulk_disable_unprepare(SPRD_PWM_CHN_CLKS_NUM, chn->clks); } return 0; } static const struct pwm_ops sprd_pwm_ops = { .apply = sprd_pwm_apply, .get_state = sprd_pwm_get_state, .owner = THIS_MODULE, }; static int sprd_pwm_clk_init(struct sprd_pwm_chip *spc) { struct clk *clk_pwm; int ret, i; for (i = 0; i < SPRD_PWM_CHN_NUM; i++) { struct sprd_pwm_chn *chn = &spc->chn[i]; int j; for (j = 0; j < SPRD_PWM_CHN_CLKS_NUM; ++j) chn->clks[j].id = sprd_pwm_clks[i * SPRD_PWM_CHN_CLKS_NUM + j]; ret = devm_clk_bulk_get(spc->dev, SPRD_PWM_CHN_CLKS_NUM, chn->clks); if (ret) { if (ret == -ENOENT) break; return dev_err_probe(spc->dev, ret, "failed to get channel clocks\n"); } clk_pwm = chn->clks[SPRD_PWM_CHN_OUTPUT_CLK].clk; chn->clk_rate = clk_get_rate(clk_pwm); } if (!i) { dev_err(spc->dev, "no available PWM channels\n"); return -ENODEV; } spc->num_pwms = i; return 0; } static int sprd_pwm_probe(struct platform_device *pdev) { struct sprd_pwm_chip *spc; int ret; spc = devm_kzalloc(&pdev->dev, sizeof(*spc), GFP_KERNEL); if (!spc) return -ENOMEM; spc->base = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(spc->base)) return PTR_ERR(spc->base); spc->dev = &pdev->dev; platform_set_drvdata(pdev, spc); ret = sprd_pwm_clk_init(spc); if (ret) return ret; spc->chip.dev = &pdev->dev; spc->chip.ops = &sprd_pwm_ops; spc->chip.npwm = spc->num_pwms; ret = pwmchip_add(&spc->chip); if (ret) dev_err(&pdev->dev, "failed to add PWM chip\n"); return ret; } static int sprd_pwm_remove(struct platform_device *pdev) { struct sprd_pwm_chip *spc = platform_get_drvdata(pdev); pwmchip_remove(&spc->chip); return 0; } static const struct of_device_id sprd_pwm_of_match[] = { { .compatible = "sprd,ums512-pwm", }, { }, }; MODULE_DEVICE_TABLE(of, sprd_pwm_of_match); static struct platform_driver sprd_pwm_driver = { .driver = { .name = "sprd-pwm", .of_match_table = sprd_pwm_of_match, }, .probe = sprd_pwm_probe, .remove = sprd_pwm_remove, }; module_platform_driver(sprd_pwm_driver); MODULE_DESCRIPTION("Spreadtrum PWM Driver"); MODULE_LICENSE("GPL v2"); |