Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 2 | /* |
Axel Lin | 261995dd | 2012-07-01 08:29:28 +0800 | [diff] [blame] | 3 | * drivers/pwm/pwm-vt8500.c |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 4 | * |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 5 | * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz> |
| 6 | * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com> |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/pwm.h> |
| 16 | #include <linux/delay.h> |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 17 | #include <linux/clk.h> |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 18 | |
| 19 | #include <asm/div64.h> |
| 20 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 21 | #include <linux/of.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/of_address.h> |
| 24 | |
| 25 | /* |
| 26 | * SoC architecture allocates register space for 4 PWMs but only |
| 27 | * 2 are currently implemented. |
| 28 | */ |
| 29 | #define VT8500_NR_PWMS 2 |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 30 | |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 31 | #define REG_CTRL(pwm) (((pwm) << 4) + 0x00) |
| 32 | #define REG_SCALAR(pwm) (((pwm) << 4) + 0x04) |
| 33 | #define REG_PERIOD(pwm) (((pwm) << 4) + 0x08) |
| 34 | #define REG_DUTY(pwm) (((pwm) << 4) + 0x0C) |
| 35 | #define REG_STATUS 0x40 |
| 36 | |
| 37 | #define CTRL_ENABLE BIT(0) |
| 38 | #define CTRL_INVERT BIT(1) |
| 39 | #define CTRL_AUTOLOAD BIT(2) |
| 40 | #define CTRL_STOP_IMM BIT(3) |
| 41 | #define CTRL_LOAD_PRESCALE BIT(4) |
| 42 | #define CTRL_LOAD_PERIOD BIT(5) |
| 43 | |
| 44 | #define STATUS_CTRL_UPDATE BIT(0) |
| 45 | #define STATUS_SCALAR_UPDATE BIT(1) |
| 46 | #define STATUS_PERIOD_UPDATE BIT(2) |
| 47 | #define STATUS_DUTY_UPDATE BIT(3) |
| 48 | #define STATUS_ALL_UPDATE 0x0F |
| 49 | |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 50 | struct vt8500_chip { |
| 51 | struct pwm_chip chip; |
| 52 | void __iomem *base; |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 53 | struct clk *clk; |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | #define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip) |
| 57 | |
| 58 | #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t) |
Uwe Kleine-König | e9d866d | 2021-11-02 10:28:04 +0100 | [diff] [blame] | 59 | static inline void vt8500_pwm_busy_wait(struct vt8500_chip *vt8500, int nr, u8 bitmask) |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 60 | { |
| 61 | int loops = msecs_to_loops(10); |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 62 | u32 mask = bitmask << (nr << 8); |
| 63 | |
| 64 | while ((readl(vt8500->base + REG_STATUS) & mask) && --loops) |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 65 | cpu_relax(); |
| 66 | |
| 67 | if (unlikely(!loops)) |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 68 | dev_warn(vt8500->chip.dev, "Waiting for status bits 0x%x to clear timed out\n", |
| 69 | mask); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, |
| 73 | int duty_ns, int period_ns) |
| 74 | { |
| 75 | struct vt8500_chip *vt8500 = to_vt8500_chip(chip); |
| 76 | unsigned long long c; |
| 77 | unsigned long period_cycles, prescale, pv, dc; |
Tony Prisk | 422470a | 2012-11-20 06:44:46 +1300 | [diff] [blame] | 78 | int err; |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 79 | u32 val; |
Tony Prisk | 422470a | 2012-11-20 06:44:46 +1300 | [diff] [blame] | 80 | |
| 81 | err = clk_enable(vt8500->clk); |
| 82 | if (err < 0) { |
| 83 | dev_err(chip->dev, "failed to enable clock\n"); |
| 84 | return err; |
| 85 | } |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 86 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 87 | c = clk_get_rate(vt8500->clk); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 88 | c = c * period_ns; |
| 89 | do_div(c, 1000000000); |
| 90 | period_cycles = c; |
| 91 | |
| 92 | if (period_cycles < 1) |
| 93 | period_cycles = 1; |
| 94 | prescale = (period_cycles - 1) / 4096; |
| 95 | pv = period_cycles / (prescale + 1) - 1; |
| 96 | if (pv > 4095) |
| 97 | pv = 4095; |
| 98 | |
Tony Prisk | 422470a | 2012-11-20 06:44:46 +1300 | [diff] [blame] | 99 | if (prescale > 1023) { |
| 100 | clk_disable(vt8500->clk); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 101 | return -EINVAL; |
Tony Prisk | 422470a | 2012-11-20 06:44:46 +1300 | [diff] [blame] | 102 | } |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 103 | |
| 104 | c = (unsigned long long)pv * duty_ns; |
| 105 | do_div(c, period_ns); |
| 106 | dc = c; |
| 107 | |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 108 | writel(prescale, vt8500->base + REG_SCALAR(pwm->hwpwm)); |
Uwe Kleine-König | e9d866d | 2021-11-02 10:28:04 +0100 | [diff] [blame] | 109 | vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_SCALAR_UPDATE); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 110 | |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 111 | writel(pv, vt8500->base + REG_PERIOD(pwm->hwpwm)); |
Uwe Kleine-König | e9d866d | 2021-11-02 10:28:04 +0100 | [diff] [blame] | 112 | vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_PERIOD_UPDATE); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 113 | |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 114 | writel(dc, vt8500->base + REG_DUTY(pwm->hwpwm)); |
Uwe Kleine-König | e9d866d | 2021-11-02 10:28:04 +0100 | [diff] [blame] | 115 | vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_DUTY_UPDATE); |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 116 | |
| 117 | val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); |
| 118 | val |= CTRL_AUTOLOAD; |
| 119 | writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); |
Uwe Kleine-König | e9d866d | 2021-11-02 10:28:04 +0100 | [diff] [blame] | 120 | vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 121 | |
Tony Prisk | 422470a | 2012-11-20 06:44:46 +1300 | [diff] [blame] | 122 | clk_disable(vt8500->clk); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 127 | { |
| 128 | struct vt8500_chip *vt8500 = to_vt8500_chip(chip); |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 129 | int err; |
| 130 | u32 val; |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 131 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 132 | err = clk_enable(vt8500->clk); |
Tony Prisk | 2f9569f | 2012-11-20 06:44:45 +1300 | [diff] [blame] | 133 | if (err < 0) { |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 134 | dev_err(chip->dev, "failed to enable clock\n"); |
| 135 | return err; |
Tony Prisk | 422470a | 2012-11-20 06:44:46 +1300 | [diff] [blame] | 136 | } |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 137 | |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 138 | val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); |
| 139 | val |= CTRL_ENABLE; |
| 140 | writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); |
Uwe Kleine-König | e9d866d | 2021-11-02 10:28:04 +0100 | [diff] [blame] | 141 | vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 142 | |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 147 | { |
| 148 | struct vt8500_chip *vt8500 = to_vt8500_chip(chip); |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 149 | u32 val; |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 150 | |
Tony Prisk | 8ab432c | 2013-01-03 08:44:15 +1300 | [diff] [blame] | 151 | val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); |
| 152 | val &= ~CTRL_ENABLE; |
| 153 | writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); |
Uwe Kleine-König | e9d866d | 2021-11-02 10:28:04 +0100 | [diff] [blame] | 154 | vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 155 | |
| 156 | clk_disable(vt8500->clk); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Tony Prisk | 3ccb1c1 | 2013-01-03 08:44:16 +1300 | [diff] [blame] | 159 | static int vt8500_pwm_set_polarity(struct pwm_chip *chip, |
| 160 | struct pwm_device *pwm, |
| 161 | enum pwm_polarity polarity) |
| 162 | { |
| 163 | struct vt8500_chip *vt8500 = to_vt8500_chip(chip); |
| 164 | u32 val; |
| 165 | |
| 166 | val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); |
| 167 | |
| 168 | if (polarity == PWM_POLARITY_INVERSED) |
| 169 | val |= CTRL_INVERT; |
| 170 | else |
| 171 | val &= ~CTRL_INVERT; |
| 172 | |
| 173 | writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); |
Uwe Kleine-König | e9d866d | 2021-11-02 10:28:04 +0100 | [diff] [blame] | 174 | vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); |
Tony Prisk | 3ccb1c1 | 2013-01-03 08:44:16 +1300 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Bhumika Goyal | b2ec9ef | 2017-01-10 23:42:06 +0530 | [diff] [blame] | 179 | static const struct pwm_ops vt8500_pwm_ops = { |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 180 | .enable = vt8500_pwm_enable, |
| 181 | .disable = vt8500_pwm_disable, |
| 182 | .config = vt8500_pwm_config, |
Tony Prisk | 3ccb1c1 | 2013-01-03 08:44:16 +1300 | [diff] [blame] | 183 | .set_polarity = vt8500_pwm_set_polarity, |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 184 | .owner = THIS_MODULE, |
| 185 | }; |
| 186 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 187 | static const struct of_device_id vt8500_pwm_dt_ids[] = { |
| 188 | { .compatible = "via,vt8500-pwm", }, |
| 189 | { /* Sentinel */ } |
| 190 | }; |
| 191 | MODULE_DEVICE_TABLE(of, vt8500_pwm_dt_ids); |
| 192 | |
| 193 | static int vt8500_pwm_probe(struct platform_device *pdev) |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 194 | { |
| 195 | struct vt8500_chip *chip; |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 196 | struct device_node *np = pdev->dev.of_node; |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 197 | int ret; |
| 198 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 199 | if (!np) { |
| 200 | dev_err(&pdev->dev, "invalid devicetree node\n"); |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | |
Axel Lin | 261995dd | 2012-07-01 08:29:28 +0800 | [diff] [blame] | 204 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
Jingoo Han | b9f8740 | 2014-04-23 18:42:10 +0900 | [diff] [blame] | 205 | if (chip == NULL) |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 206 | return -ENOMEM; |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 207 | |
| 208 | chip->chip.dev = &pdev->dev; |
| 209 | chip->chip.ops = &vt8500_pwm_ops; |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 210 | chip->chip.npwm = VT8500_NR_PWMS; |
| 211 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 212 | chip->clk = devm_clk_get(&pdev->dev, NULL); |
| 213 | if (IS_ERR(chip->clk)) { |
| 214 | dev_err(&pdev->dev, "clock source not specified\n"); |
| 215 | return PTR_ERR(chip->clk); |
| 216 | } |
| 217 | |
Yangtao Li | 4906bf5 | 2019-12-29 08:06:02 +0000 | [diff] [blame] | 218 | chip->base = devm_platform_ioremap_resource(pdev, 0); |
Thierry Reding | 6d4294d | 2013-01-21 11:09:16 +0100 | [diff] [blame] | 219 | if (IS_ERR(chip->base)) |
| 220 | return PTR_ERR(chip->base); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 221 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 222 | ret = clk_prepare(chip->clk); |
| 223 | if (ret < 0) { |
| 224 | dev_err(&pdev->dev, "failed to prepare clock\n"); |
Axel Lin | 261995dd | 2012-07-01 08:29:28 +0800 | [diff] [blame] | 225 | return ret; |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | ret = pwmchip_add(&chip->chip); |
| 229 | if (ret < 0) { |
| 230 | dev_err(&pdev->dev, "failed to add PWM chip\n"); |
Arvind Yadav | 0bd24f9 | 2017-05-05 13:30:14 +0530 | [diff] [blame] | 231 | clk_unprepare(chip->clk); |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 232 | return ret; |
| 233 | } |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 234 | |
| 235 | platform_set_drvdata(pdev, chip); |
| 236 | return ret; |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 239 | static int vt8500_pwm_remove(struct platform_device *pdev) |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 240 | { |
Uwe Kleine-König | fb2cb3b | 2021-06-21 15:04:57 +0200 | [diff] [blame] | 241 | struct vt8500_chip *chip = platform_get_drvdata(pdev); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 242 | |
Uwe Kleine-König | 868f13b | 2021-06-21 15:04:58 +0200 | [diff] [blame] | 243 | pwmchip_remove(&chip->chip); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 244 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 245 | clk_unprepare(chip->clk); |
| 246 | |
Uwe Kleine-König | 868f13b | 2021-06-21 15:04:58 +0200 | [diff] [blame] | 247 | return 0; |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 250 | static struct platform_driver vt8500_pwm_driver = { |
| 251 | .probe = vt8500_pwm_probe, |
| 252 | .remove = vt8500_pwm_remove, |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 253 | .driver = { |
| 254 | .name = "vt8500-pwm", |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 255 | .of_match_table = vt8500_pwm_dt_ids, |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 256 | }, |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 257 | }; |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 258 | module_platform_driver(vt8500_pwm_driver); |
Sascha Hauer | a245cce | 2012-03-15 10:04:37 +0100 | [diff] [blame] | 259 | |
Tony Prisk | 63e1ed2 | 2012-10-27 14:49:57 +1300 | [diff] [blame] | 260 | MODULE_DESCRIPTION("VT8500 PWM Driver"); |
| 261 | MODULE_AUTHOR("Tony Prisk <linux@prisktech.co.nz>"); |
| 262 | MODULE_LICENSE("GPL v2"); |