Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
80943bb
Documentation
LICENSES
arch
block
certs
crypto
drivers
accel
accessibility
acpi
amba
android
ata
atm
auxdisplay
base
bcma
block
bluetooth
bus
cache
cdrom
cdx
char
clk
clocksource
comedi
connector
counter
cpufreq
cpuidle
crypto
cxl
dax
dca
devfreq
dio
dma-buf
dma
dpll
edac
eisa
extcon
firewire
firmware
fpga
fsi
gnss
gpio
gpu
greybus
hid
hsi
hte
hv
hwmon
hwspinlock
hwtracing
i2c
i3c
idle
iio
infiniband
input
interconnect
iommu
ipack
irqchip
isdn
leds
macintosh
mailbox
mcb
md
media
memory
memstick
message
mfd
misc
mmc
most
mtd
mux
net
nfc
ntb
nubus
nvdimm
nvme
nvmem
of
opp
parisc
parport
pci
pcmcia
peci
perf
phy
pinctrl
platform
pmdomain
pnp
power
powercap
pps
ps3
ptp
pwm
Kconfig
Makefile
core.c
pwm-ab8500.c
pwm-apple.c
pwm-atmel-hlcdc.c
pwm-atmel-tcb.c
pwm-atmel.c
pwm-bcm-iproc.c
pwm-bcm-kona.c
pwm-bcm2835.c
pwm-berlin.c
pwm-brcmstb.c
pwm-clk.c
pwm-clps711x.c
pwm-crc.c
pwm-cros-ec.c
pwm-dwc-core.c
pwm-dwc.c
pwm-dwc.h
pwm-ep93xx.c
pwm-fsl-ftm.c
pwm-hibvt.c
pwm-img.c
pwm-imx-tpm.c
pwm-imx1.c
pwm-imx27.c
pwm-intel-lgm.c
pwm-iqs620a.c
pwm-jz4740.c
pwm-keembay.c
pwm-lp3943.c
pwm-lpc18xx-sct.c
pwm-lpc32xx.c
pwm-lpss-pci.c
pwm-lpss-platform.c
pwm-lpss.c
pwm-lpss.h
pwm-mediatek.c
pwm-meson.c
pwm-microchip-core.c
pwm-mtk-disp.c
pwm-mxs.c
pwm-ntxec.c
pwm-omap-dmtimer.c
pwm-pca9685.c
pwm-pxa.c
pwm-raspberrypi-poe.c
pwm-rcar.c
pwm-renesas-tpu.c
pwm-rockchip.c
pwm-rz-mtu3.c
pwm-samsung.c
pwm-sifive.c
pwm-sl28cpld.c
pwm-spear.c
pwm-sprd.c
pwm-sti.c
pwm-stm32-lp.c
pwm-stm32.c
pwm-stmpe.c
pwm-sun4i.c
pwm-sunplus.c
pwm-tegra.c
pwm-tiecap.c
pwm-tiehrpwm.c
pwm-twl-led.c
pwm-twl.c
pwm-visconti.c
pwm-vt8500.c
pwm-xilinx.c
sysfs.c
rapidio
ras
regulator
remoteproc
reset
rpmsg
rtc
s390
sbus
scsi
sh
siox
slimbus
soc
soundwire
spi
spmi
ssb
staging
target
tc
tee
thermal
thunderbolt
tty
ufs
uio
usb
vdpa
vfio
vhost
video
virt
virtio
w1
watchdog
xen
zorro
Kconfig
Makefile
fs
include
init
io_uring
ipc
kernel
lib
mm
net
rust
samples
scripts
security
sound
tools
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
.rustfmt.toml
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
drivers
/
pwm
/
pwm-vt8500.c
Copy path
Blame
Blame
Latest commit
History
History
275 lines (219 loc) · 6.76 KB
Breadcrumbs
linux
/
drivers
/
pwm
/
pwm-vt8500.c
Top
File metadata and controls
Code
Blame
275 lines (219 loc) · 6.76 KB
Raw
// SPDX-License-Identifier: GPL-2.0-only /* * drivers/pwm/pwm-vt8500.c * * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz> * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com> */ #include <linux/mod_devicetable.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/platform_device.h> #include <linux/slab.h> #include <linux/err.h> #include <linux/io.h> #include <linux/pwm.h> #include <linux/delay.h> #include <linux/clk.h> #include <asm/div64.h> /* * SoC architecture allocates register space for 4 PWMs but only * 2 are currently implemented. */ #define VT8500_NR_PWMS 2 #define REG_CTRL(pwm) (((pwm) << 4) + 0x00) #define REG_SCALAR(pwm) (((pwm) << 4) + 0x04) #define REG_PERIOD(pwm) (((pwm) << 4) + 0x08) #define REG_DUTY(pwm) (((pwm) << 4) + 0x0C) #define REG_STATUS 0x40 #define CTRL_ENABLE BIT(0) #define CTRL_INVERT BIT(1) #define CTRL_AUTOLOAD BIT(2) #define CTRL_STOP_IMM BIT(3) #define CTRL_LOAD_PRESCALE BIT(4) #define CTRL_LOAD_PERIOD BIT(5) #define STATUS_CTRL_UPDATE BIT(0) #define STATUS_SCALAR_UPDATE BIT(1) #define STATUS_PERIOD_UPDATE BIT(2) #define STATUS_DUTY_UPDATE BIT(3) #define STATUS_ALL_UPDATE 0x0F struct vt8500_chip { struct pwm_chip chip; void __iomem *base; struct clk *clk; }; #define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip) #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t) static inline void vt8500_pwm_busy_wait(struct vt8500_chip *vt8500, int nr, u8 bitmask) { int loops = msecs_to_loops(10); u32 mask = bitmask << (nr << 8); while ((readl(vt8500->base + REG_STATUS) & mask) && --loops) cpu_relax(); if (unlikely(!loops)) dev_warn(vt8500->chip.dev, "Waiting for status bits 0x%x to clear timed out\n", mask); } static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, u64 duty_ns, u64 period_ns) { struct vt8500_chip *vt8500 = to_vt8500_chip(chip); unsigned long long c; unsigned long period_cycles, prescale, pv, dc; int err; u32 val; err = clk_enable(vt8500->clk); if (err < 0) { dev_err(chip->dev, "failed to enable clock\n"); return err; } c = clk_get_rate(vt8500->clk); c = c * period_ns; do_div(c, 1000000000); period_cycles = c; if (period_cycles < 1) period_cycles = 1; prescale = (period_cycles - 1) / 4096; pv = period_cycles / (prescale + 1) - 1; if (pv > 4095) pv = 4095; if (prescale > 1023) { clk_disable(vt8500->clk); return -EINVAL; } c = (unsigned long long)pv * duty_ns; dc = div64_u64(c, period_ns); writel(prescale, vt8500->base + REG_SCALAR(pwm->hwpwm)); vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_SCALAR_UPDATE); writel(pv, vt8500->base + REG_PERIOD(pwm->hwpwm)); vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_PERIOD_UPDATE); writel(dc, vt8500->base + REG_DUTY(pwm->hwpwm)); vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_DUTY_UPDATE); val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); val |= CTRL_AUTOLOAD; writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); clk_disable(vt8500->clk); return 0; } static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) { struct vt8500_chip *vt8500 = to_vt8500_chip(chip); int err; u32 val; err = clk_enable(vt8500->clk); if (err < 0) { dev_err(chip->dev, "failed to enable clock\n"); return err; } val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); val |= CTRL_ENABLE; writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); return 0; } static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) { struct vt8500_chip *vt8500 = to_vt8500_chip(chip); u32 val; val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); val &= ~CTRL_ENABLE; writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); clk_disable(vt8500->clk); } static int vt8500_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm, enum pwm_polarity polarity) { struct vt8500_chip *vt8500 = to_vt8500_chip(chip); u32 val; val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); if (polarity == PWM_POLARITY_INVERSED) val |= CTRL_INVERT; else val &= ~CTRL_INVERT; writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); return 0; } static int vt8500_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_state *state) { int err; bool enabled = pwm->state.enabled; if (state->polarity != pwm->state.polarity) { /* * Changing the polarity of a running PWM is only allowed when * the PWM driver implements ->apply(). */ if (enabled) { vt8500_pwm_disable(chip, pwm); enabled = false; } err = vt8500_pwm_set_polarity(chip, pwm, state->polarity); if (err) return err; } if (!state->enabled) { if (enabled) vt8500_pwm_disable(chip, pwm); return 0; } /* * We cannot skip calling ->config even if state->period == * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle * because we might have exited early in the last call to * pwm_apply_state because of !state->enabled and so the two values in * pwm->state might not be configured in hardware. */ err = vt8500_pwm_config(chip, pwm, state->duty_cycle, state->period); if (err) return err; if (!enabled) err = vt8500_pwm_enable(chip, pwm); return err; } static const struct pwm_ops vt8500_pwm_ops = { .apply = vt8500_pwm_apply, }; static const struct of_device_id vt8500_pwm_dt_ids[] = { { .compatible = "via,vt8500-pwm", }, { /* Sentinel */ } }; MODULE_DEVICE_TABLE(of, vt8500_pwm_dt_ids); static int vt8500_pwm_probe(struct platform_device *pdev) { struct vt8500_chip *vt8500; struct device_node *np = pdev->dev.of_node; int ret; if (!np) return dev_err_probe(&pdev->dev, -EINVAL, "invalid devicetree node\n"); vt8500 = devm_kzalloc(&pdev->dev, sizeof(*vt8500), GFP_KERNEL); if (vt8500 == NULL) return -ENOMEM; vt8500->chip.dev = &pdev->dev; vt8500->chip.ops = &vt8500_pwm_ops; vt8500->chip.npwm = VT8500_NR_PWMS; vt8500->clk = devm_clk_get_prepared(&pdev->dev, NULL); if (IS_ERR(vt8500->clk)) return dev_err_probe(&pdev->dev, PTR_ERR(vt8500->clk), "clock source not specified\n"); vt8500->base = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(vt8500->base)) return PTR_ERR(vt8500->base); ret = devm_pwmchip_add(&pdev->dev, &vt8500->chip); if (ret < 0) return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n"); return 0; } static struct platform_driver vt8500_pwm_driver = { .probe = vt8500_pwm_probe, .driver = { .name = "vt8500-pwm", .of_match_table = vt8500_pwm_dt_ids, }, }; module_platform_driver(vt8500_pwm_driver); MODULE_DESCRIPTION("VT8500 PWM Driver"); MODULE_AUTHOR("Tony Prisk <linux@prisktech.co.nz>"); MODULE_LICENSE("GPL v2");
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
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
You can’t perform that action at this time.