Changes in 4.9.221 ext4: fix extent_status fragmentation for plain files net: ipv4: emulate READ_ONCE() on ->hdrincl bit-field in raw_sendmsg() net: ipv4: avoid unused variable warning for sysctl drm/msm: Use the correct dma_sync calls harder crypto: mxs-dcp - make symbols 'sha1_null_hash' and 'sha256_null_hash' static vti4: removed duplicate log message. watchdog: reset last_hw_keepalive time at start scsi: lpfc: Fix kasan slab-out-of-bounds error in lpfc_unreg_login ceph: return ceph_mdsc_do_request() errors from __get_parent() ceph: don't skip updating wanted caps when cap is stale pwm: rcar: Fix late Runtime PM enablement scsi: iscsi: Report unbind session event when the target has been removed ASoC: Intel: atom: Take the drv->lock mutex before calling sst_send_slot_map() kernel/gcov/fs.c: gcov_seq_next() should increase position index ipc/util.c: sysvipc_find_ipc() should increase position index s390/cio: avoid duplicated 'ADD' uevents pwm: renesas-tpu: Fix late Runtime PM enablement pwm: bcm2835: Dynamically allocate base PCI/ASPM: Allow re-enabling Clock PM ipv6: fix restrict IPV6_ADDRFORM operation macsec: avoid to set wrong mtu macvlan: fix null dereference in macvlan_device_event() net: netrom: Fix potential nr_neigh refcnt leak in nr_add_node net/x25: Fix x25_neigh refcnt leak when receiving frame tcp: cache line align MAX_TCP_HEADER team: fix hang in team_mode_get() net: dsa: b53: Fix ARL register definitions xfrm: Always set XFRM_TRANSFORMED in xfrm{4,6}_output_finish ALSA: hda: Remove ASUS ROG Zenith from the blacklist iio: xilinx-xadc: Fix ADC-B powerdown iio: xilinx-xadc: Fix clearing interrupt when enabling trigger iio: xilinx-xadc: Fix sequencer configuration for aux channels in simultaneous mode fs/namespace.c: fix mountpoint reference counter race USB: sisusbvga: Change port variable from signed to unsigned USB: Add USB_QUIRK_DELAY_CTRL_MSG and USB_QUIRK_DELAY_INIT for Corsair K70 RGB RAPIDFIRE USB: core: Fix free-while-in-use bug in the USB S-Glibrary USB: hub: Fix handling of connect changes during sleep overflow.h: Add arithmetic shift helper vmalloc: fix remap_vmalloc_range() bounds checks ALSA: usx2y: Fix potential NULL dereference ALSA: usb-audio: Fix usb audio refcnt leak when getting spdif ALSA: usb-audio: Filter out unsupported sample rates on Focusrite devices tpm/tpm_tis: Free IRQ if probing fails KVM: Check validity of resolved slot when searching memslots KVM: VMX: Enable machine check support for 32bit targets tty: hvc: fix buffer overflow during hvc_alloc(). tty: rocket, avoid OOB access usb-storage: Add unusual_devs entry for JMicron JMS566 audit: check the length of userspace generated audit records ASoC: dapm: fixup dapm kcontrol widget ARM: imx: provide v7_cpu_resume() only on ARM_CPU_SUSPEND=y staging: comedi: dt2815: fix writing hi byte of analog output staging: comedi: Fix comedi_device refcnt leak in comedi_open staging: vt6656: Fix drivers TBTT timing counter. staging: vt6656: Power save stop wake_up_count wrap around. UAS: no use logging any details in case of ENODEV UAS: fix deadlock in error handling and PM flushing work usb: f_fs: Clear OS Extended descriptor counts to zero in ffs_data_reset() remoteproc: Fix wrong rvring index computation fuse: fix possibly missed wake-up after abort mtd: cfi: fix deadloop in cfi_cmdset_0002.c do_write_buffer usb: gadget: udc: bdc: Remove unnecessary NULL checks in bdc_req_complete nfsd: memory corruption in nfsd4_lock() net/cxgb4: Check the return from t4_query_params properly perf/core: fix parent pid/tid in task exit events bpf, x86: Fix encoding for lower 8-bit registers in BPF_STX BPF_B xfs: fix partially uninitialized structure in xfs_reflink_remap_extent scsi: target: fix PR IN / READ FULL STATUS for FC objtool: Fix CONFIG_UBSAN_TRAP unreachable warnings objtool: Support Clang non-section symbols in ORC dump xen/xenbus: ensure xenbus_map_ring_valloc() returns proper grant status ext4: convert BUG_ON's to WARN_ON's in mballoc.c hwmon: (jc42) Fix name to have no illegal characters ext4: avoid declaring fs inconsistent due to invalid file handles ext4: protect journal inode's blocks using block_validity ext4: don't perform block validity checks on the journal inode ext4: fix block validity checks for journal inodes using indirect blocks ext4: unsigned int compared against zero ext4: check for non-zero journal inum in ext4_calculate_overhead propagate_one(): mnt_set_mountpoint() needs mount_lock Linux 4.9.221 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com> Change-Id: Ica420bdbb6c18fff9d0e6abffc180beefff0c5b9
283 lines
6.8 KiB
C
283 lines
6.8 KiB
C
/*
|
|
* R-Car PWM Timer driver
|
|
*
|
|
* Copyright (C) 2015 Renesas Electronics Corporation
|
|
*
|
|
* This is free software; you can redistribute it and/or modify
|
|
* it under the terms of version 2 of the GNU General Public License as
|
|
* published by the Free Software Foundation.
|
|
*/
|
|
|
|
#include <linux/clk.h>
|
|
#include <linux/err.h>
|
|
#include <linux/io.h>
|
|
#include <linux/module.h>
|
|
#include <linux/of.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/pm_runtime.h>
|
|
#include <linux/pwm.h>
|
|
#include <linux/slab.h>
|
|
|
|
#define RCAR_PWM_MAX_DIVISION 24
|
|
#define RCAR_PWM_MAX_CYCLE 1023
|
|
|
|
#define RCAR_PWMCR 0x00
|
|
#define RCAR_PWMCR_CC0_MASK 0x000f0000
|
|
#define RCAR_PWMCR_CC0_SHIFT 16
|
|
#define RCAR_PWMCR_CCMD BIT(15)
|
|
#define RCAR_PWMCR_SYNC BIT(11)
|
|
#define RCAR_PWMCR_SS0 BIT(4)
|
|
#define RCAR_PWMCR_EN0 BIT(0)
|
|
|
|
#define RCAR_PWMCNT 0x04
|
|
#define RCAR_PWMCNT_CYC0_MASK 0x03ff0000
|
|
#define RCAR_PWMCNT_CYC0_SHIFT 16
|
|
#define RCAR_PWMCNT_PH0_MASK 0x000003ff
|
|
#define RCAR_PWMCNT_PH0_SHIFT 0
|
|
|
|
struct rcar_pwm_chip {
|
|
struct pwm_chip chip;
|
|
void __iomem *base;
|
|
struct clk *clk;
|
|
};
|
|
|
|
static inline struct rcar_pwm_chip *to_rcar_pwm_chip(struct pwm_chip *chip)
|
|
{
|
|
return container_of(chip, struct rcar_pwm_chip, chip);
|
|
}
|
|
|
|
static void rcar_pwm_write(struct rcar_pwm_chip *rp, u32 data,
|
|
unsigned int offset)
|
|
{
|
|
writel(data, rp->base + offset);
|
|
}
|
|
|
|
static u32 rcar_pwm_read(struct rcar_pwm_chip *rp, unsigned int offset)
|
|
{
|
|
return readl(rp->base + offset);
|
|
}
|
|
|
|
static void rcar_pwm_update(struct rcar_pwm_chip *rp, u32 mask, u32 data,
|
|
unsigned int offset)
|
|
{
|
|
u32 value;
|
|
|
|
value = rcar_pwm_read(rp, offset);
|
|
value &= ~mask;
|
|
value |= data & mask;
|
|
rcar_pwm_write(rp, value, offset);
|
|
}
|
|
|
|
static int rcar_pwm_get_clock_division(struct rcar_pwm_chip *rp, int period_ns)
|
|
{
|
|
unsigned long clk_rate = clk_get_rate(rp->clk);
|
|
unsigned long long max; /* max cycle / nanoseconds */
|
|
unsigned int div;
|
|
|
|
if (clk_rate == 0)
|
|
return -EINVAL;
|
|
|
|
for (div = 0; div <= RCAR_PWM_MAX_DIVISION; div++) {
|
|
max = (unsigned long long)NSEC_PER_SEC * RCAR_PWM_MAX_CYCLE *
|
|
(1 << div);
|
|
do_div(max, clk_rate);
|
|
if (period_ns <= max)
|
|
break;
|
|
}
|
|
|
|
return (div <= RCAR_PWM_MAX_DIVISION) ? div : -ERANGE;
|
|
}
|
|
|
|
static void rcar_pwm_set_clock_control(struct rcar_pwm_chip *rp,
|
|
unsigned int div)
|
|
{
|
|
u32 value;
|
|
|
|
value = rcar_pwm_read(rp, RCAR_PWMCR);
|
|
value &= ~(RCAR_PWMCR_CCMD | RCAR_PWMCR_CC0_MASK);
|
|
|
|
if (div & 1)
|
|
value |= RCAR_PWMCR_CCMD;
|
|
|
|
div >>= 1;
|
|
|
|
value |= div << RCAR_PWMCR_CC0_SHIFT;
|
|
rcar_pwm_write(rp, value, RCAR_PWMCR);
|
|
}
|
|
|
|
static int rcar_pwm_set_counter(struct rcar_pwm_chip *rp, int div, int duty_ns,
|
|
int period_ns)
|
|
{
|
|
unsigned long long one_cycle, tmp; /* 0.01 nanoseconds */
|
|
unsigned long clk_rate = clk_get_rate(rp->clk);
|
|
u32 cyc, ph;
|
|
|
|
one_cycle = (unsigned long long)NSEC_PER_SEC * 100ULL * (1 << div);
|
|
do_div(one_cycle, clk_rate);
|
|
|
|
tmp = period_ns * 100ULL;
|
|
do_div(tmp, one_cycle);
|
|
cyc = (tmp << RCAR_PWMCNT_CYC0_SHIFT) & RCAR_PWMCNT_CYC0_MASK;
|
|
|
|
tmp = duty_ns * 100ULL;
|
|
do_div(tmp, one_cycle);
|
|
ph = tmp & RCAR_PWMCNT_PH0_MASK;
|
|
|
|
/* Avoid prohibited setting */
|
|
if (cyc == 0 || ph == 0)
|
|
return -EINVAL;
|
|
|
|
rcar_pwm_write(rp, cyc | ph, RCAR_PWMCNT);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int rcar_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
{
|
|
struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
|
|
|
|
return clk_prepare_enable(rp->clk);
|
|
}
|
|
|
|
static void rcar_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
{
|
|
struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
|
|
|
|
clk_disable_unprepare(rp->clk);
|
|
}
|
|
|
|
static int rcar_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
|
|
int duty_ns, int period_ns)
|
|
{
|
|
struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
|
|
int div, ret;
|
|
|
|
div = rcar_pwm_get_clock_division(rp, period_ns);
|
|
if (div < 0)
|
|
return div;
|
|
|
|
/*
|
|
* Let the core driver set pwm->period if disabled and duty_ns == 0.
|
|
* But, this driver should prevent to set the new duty_ns if current
|
|
* duty_cycle is not set
|
|
*/
|
|
if (!pwm_is_enabled(pwm) && !duty_ns && !pwm->state.duty_cycle)
|
|
return 0;
|
|
|
|
rcar_pwm_update(rp, RCAR_PWMCR_SYNC, RCAR_PWMCR_SYNC, RCAR_PWMCR);
|
|
|
|
ret = rcar_pwm_set_counter(rp, div, duty_ns, period_ns);
|
|
if (!ret)
|
|
rcar_pwm_set_clock_control(rp, div);
|
|
|
|
/* The SYNC should be set to 0 even if rcar_pwm_set_counter failed */
|
|
rcar_pwm_update(rp, RCAR_PWMCR_SYNC, 0, RCAR_PWMCR);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int rcar_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
{
|
|
struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
|
|
u32 value;
|
|
|
|
/* Don't enable the PWM device if CYC0 or PH0 is 0 */
|
|
value = rcar_pwm_read(rp, RCAR_PWMCNT);
|
|
if ((value & RCAR_PWMCNT_CYC0_MASK) == 0 ||
|
|
(value & RCAR_PWMCNT_PH0_MASK) == 0)
|
|
return -EINVAL;
|
|
|
|
rcar_pwm_update(rp, RCAR_PWMCR_EN0, RCAR_PWMCR_EN0, RCAR_PWMCR);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void rcar_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
{
|
|
struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
|
|
|
|
rcar_pwm_update(rp, RCAR_PWMCR_EN0, 0, RCAR_PWMCR);
|
|
}
|
|
|
|
static const struct pwm_ops rcar_pwm_ops = {
|
|
.request = rcar_pwm_request,
|
|
.free = rcar_pwm_free,
|
|
.config = rcar_pwm_config,
|
|
.enable = rcar_pwm_enable,
|
|
.disable = rcar_pwm_disable,
|
|
.owner = THIS_MODULE,
|
|
};
|
|
|
|
static int rcar_pwm_probe(struct platform_device *pdev)
|
|
{
|
|
struct rcar_pwm_chip *rcar_pwm;
|
|
struct resource *res;
|
|
int ret;
|
|
|
|
rcar_pwm = devm_kzalloc(&pdev->dev, sizeof(*rcar_pwm), GFP_KERNEL);
|
|
if (rcar_pwm == NULL)
|
|
return -ENOMEM;
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
rcar_pwm->base = devm_ioremap_resource(&pdev->dev, res);
|
|
if (IS_ERR(rcar_pwm->base))
|
|
return PTR_ERR(rcar_pwm->base);
|
|
|
|
rcar_pwm->clk = devm_clk_get(&pdev->dev, NULL);
|
|
if (IS_ERR(rcar_pwm->clk)) {
|
|
dev_err(&pdev->dev, "cannot get clock\n");
|
|
return PTR_ERR(rcar_pwm->clk);
|
|
}
|
|
|
|
platform_set_drvdata(pdev, rcar_pwm);
|
|
|
|
rcar_pwm->chip.dev = &pdev->dev;
|
|
rcar_pwm->chip.ops = &rcar_pwm_ops;
|
|
rcar_pwm->chip.base = -1;
|
|
rcar_pwm->chip.npwm = 1;
|
|
|
|
pm_runtime_enable(&pdev->dev);
|
|
|
|
ret = pwmchip_add(&rcar_pwm->chip);
|
|
if (ret < 0) {
|
|
dev_err(&pdev->dev, "failed to register PWM chip: %d\n", ret);
|
|
pm_runtime_disable(&pdev->dev);
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int rcar_pwm_remove(struct platform_device *pdev)
|
|
{
|
|
struct rcar_pwm_chip *rcar_pwm = platform_get_drvdata(pdev);
|
|
int ret;
|
|
|
|
ret = pwmchip_remove(&rcar_pwm->chip);
|
|
|
|
pm_runtime_disable(&pdev->dev);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static const struct of_device_id rcar_pwm_of_table[] = {
|
|
{ .compatible = "renesas,pwm-rcar", },
|
|
{ },
|
|
};
|
|
MODULE_DEVICE_TABLE(of, rcar_pwm_of_table);
|
|
|
|
static struct platform_driver rcar_pwm_driver = {
|
|
.probe = rcar_pwm_probe,
|
|
.remove = rcar_pwm_remove,
|
|
.driver = {
|
|
.name = "pwm-rcar",
|
|
.of_match_table = of_match_ptr(rcar_pwm_of_table),
|
|
}
|
|
};
|
|
module_platform_driver(rcar_pwm_driver);
|
|
|
|
MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
|
|
MODULE_DESCRIPTION("Renesas PWM Timer Driver");
|
|
MODULE_LICENSE("GPL v2");
|
|
MODULE_ALIAS("platform:pwm-rcar");
|