2019-05-28 16:57:07 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-08-06 02:31:46 +00:00
|
|
|
/*
|
|
|
|
* NXP LPC18xx State Configurable Timer - Pulse Width Modulator driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Ariel D'Alessandro <ariel@vanguardiasur.com>
|
|
|
|
*
|
|
|
|
* Notes
|
|
|
|
* =====
|
|
|
|
* NXP LPC18xx provides a State Configurable Timer (SCT) which can be configured
|
|
|
|
* as a Pulse Width Modulator.
|
|
|
|
*
|
|
|
|
* SCT supports 16 outputs, 16 events and 16 registers. Each event will be
|
|
|
|
* triggered when its related register matches the SCT counter value, and it
|
|
|
|
* will set or clear a selected output.
|
|
|
|
*
|
|
|
|
* One of the events is preselected to generate the period, thus the maximum
|
|
|
|
* number of simultaneous channels is limited to 15. Notice that period is
|
|
|
|
* global to all the channels, thus PWM driver will refuse setting different
|
|
|
|
* values to it, unless there's only one channel requested.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
2023-07-14 17:48:50 +00:00
|
|
|
#include <linux/mod_devicetable.h>
|
2015-08-06 02:31:46 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/pwm.h>
|
|
|
|
|
|
|
|
/* LPC18xx SCT registers */
|
|
|
|
#define LPC18XX_PWM_CONFIG 0x000
|
|
|
|
#define LPC18XX_PWM_CONFIG_UNIFY BIT(0)
|
|
|
|
#define LPC18XX_PWM_CONFIG_NORELOAD BIT(7)
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_CTRL 0x004
|
|
|
|
#define LPC18XX_PWM_CTRL_HALT BIT(2)
|
|
|
|
#define LPC18XX_PWM_BIDIR BIT(4)
|
|
|
|
#define LPC18XX_PWM_PRE_SHIFT 5
|
|
|
|
#define LPC18XX_PWM_PRE_MASK (0xff << LPC18XX_PWM_PRE_SHIFT)
|
|
|
|
#define LPC18XX_PWM_PRE(x) (x << LPC18XX_PWM_PRE_SHIFT)
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_LIMIT 0x008
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_RES_BASE 0x058
|
|
|
|
#define LPC18XX_PWM_RES_SHIFT(_ch) (_ch * 2)
|
|
|
|
#define LPC18XX_PWM_RES(_ch, _action) (_action << LPC18XX_PWM_RES_SHIFT(_ch))
|
|
|
|
#define LPC18XX_PWM_RES_MASK(_ch) (0x3 << LPC18XX_PWM_RES_SHIFT(_ch))
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_MATCH_BASE 0x100
|
|
|
|
#define LPC18XX_PWM_MATCH(_ch) (LPC18XX_PWM_MATCH_BASE + _ch * 4)
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_MATCHREL_BASE 0x200
|
|
|
|
#define LPC18XX_PWM_MATCHREL(_ch) (LPC18XX_PWM_MATCHREL_BASE + _ch * 4)
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_EVSTATEMSK_BASE 0x300
|
|
|
|
#define LPC18XX_PWM_EVSTATEMSK(_ch) (LPC18XX_PWM_EVSTATEMSK_BASE + _ch * 8)
|
|
|
|
#define LPC18XX_PWM_EVSTATEMSK_ALL 0xffffffff
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_EVCTRL_BASE 0x304
|
|
|
|
#define LPC18XX_PWM_EVCTRL(_ev) (LPC18XX_PWM_EVCTRL_BASE + _ev * 8)
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_EVCTRL_MATCH(_ch) _ch
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_EVCTRL_COMB_SHIFT 12
|
|
|
|
#define LPC18XX_PWM_EVCTRL_COMB_MATCH (0x1 << LPC18XX_PWM_EVCTRL_COMB_SHIFT)
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_OUTPUTSET_BASE 0x500
|
|
|
|
#define LPC18XX_PWM_OUTPUTSET(_ch) (LPC18XX_PWM_OUTPUTSET_BASE + _ch * 8)
|
|
|
|
|
|
|
|
#define LPC18XX_PWM_OUTPUTCL_BASE 0x504
|
|
|
|
#define LPC18XX_PWM_OUTPUTCL(_ch) (LPC18XX_PWM_OUTPUTCL_BASE + _ch * 8)
|
|
|
|
|
|
|
|
/* LPC18xx SCT unified counter */
|
|
|
|
#define LPC18XX_PWM_TIMER_MAX 0xffffffff
|
|
|
|
|
|
|
|
/* LPC18xx SCT events */
|
|
|
|
#define LPC18XX_PWM_EVENT_PERIOD 0
|
|
|
|
#define LPC18XX_PWM_EVENT_MAX 16
|
|
|
|
|
2021-11-10 08:49:49 +00:00
|
|
|
#define LPC18XX_NUM_PWMS 16
|
|
|
|
|
2015-08-06 02:31:46 +00:00
|
|
|
/* SCT conflict resolution */
|
|
|
|
enum lpc18xx_pwm_res_action {
|
|
|
|
LPC18XX_PWM_RES_NONE,
|
|
|
|
LPC18XX_PWM_RES_SET,
|
|
|
|
LPC18XX_PWM_RES_CLEAR,
|
|
|
|
LPC18XX_PWM_RES_TOGGLE,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct lpc18xx_pwm_data {
|
|
|
|
unsigned int duty_event;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct lpc18xx_pwm_chip {
|
|
|
|
void __iomem *base;
|
|
|
|
struct clk *pwm_clk;
|
|
|
|
unsigned long clk_rate;
|
|
|
|
unsigned int period_ns;
|
|
|
|
unsigned int min_period_ns;
|
2022-07-12 16:15:19 +00:00
|
|
|
u64 max_period_ns;
|
2015-08-06 02:31:46 +00:00
|
|
|
unsigned int period_event;
|
|
|
|
unsigned long event_map;
|
|
|
|
struct mutex res_lock;
|
|
|
|
struct mutex period_lock;
|
2021-11-10 08:49:49 +00:00
|
|
|
struct lpc18xx_pwm_data channeldata[LPC18XX_NUM_PWMS];
|
2015-08-06 02:31:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct lpc18xx_pwm_chip *
|
|
|
|
to_lpc18xx_pwm_chip(struct pwm_chip *chip)
|
|
|
|
{
|
2024-02-14 09:31:50 +00:00
|
|
|
return pwmchip_get_drvdata(chip);
|
2015-08-06 02:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void lpc18xx_pwm_writel(struct lpc18xx_pwm_chip *lpc18xx_pwm,
|
|
|
|
u32 reg, u32 val)
|
|
|
|
{
|
|
|
|
writel(val, lpc18xx_pwm->base + reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 lpc18xx_pwm_readl(struct lpc18xx_pwm_chip *lpc18xx_pwm,
|
|
|
|
u32 reg)
|
|
|
|
{
|
|
|
|
return readl(lpc18xx_pwm->base + reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lpc18xx_pwm_set_conflict_res(struct lpc18xx_pwm_chip *lpc18xx_pwm,
|
|
|
|
struct pwm_device *pwm,
|
|
|
|
enum lpc18xx_pwm_res_action action)
|
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
mutex_lock(&lpc18xx_pwm->res_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simultaneous set and clear may happen on an output, that is the case
|
|
|
|
* when duty_ns == period_ns. LPC18xx SCT allows to set a conflict
|
|
|
|
* resolution action to be taken in such a case.
|
|
|
|
*/
|
|
|
|
val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_RES_BASE);
|
|
|
|
val &= ~LPC18XX_PWM_RES_MASK(pwm->hwpwm);
|
|
|
|
val |= LPC18XX_PWM_RES(pwm->hwpwm, action);
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_RES_BASE, val);
|
|
|
|
|
|
|
|
mutex_unlock(&lpc18xx_pwm->res_lock);
|
|
|
|
}
|
|
|
|
|
2022-07-12 16:15:19 +00:00
|
|
|
static void lpc18xx_pwm_config_period(struct pwm_chip *chip, u64 period_ns)
|
2015-08-06 02:31:46 +00:00
|
|
|
{
|
|
|
|
struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
|
2022-07-12 16:15:19 +00:00
|
|
|
u32 val;
|
2015-08-06 02:31:46 +00:00
|
|
|
|
2022-07-12 16:15:19 +00:00
|
|
|
/*
|
|
|
|
* With clk_rate < NSEC_PER_SEC this cannot overflow.
|
|
|
|
* With period_ns < max_period_ns this also fits into an u32.
|
|
|
|
* As period_ns >= min_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, lpc18xx_pwm->clk_rate);
|
|
|
|
* we have val >= 1.
|
|
|
|
*/
|
|
|
|
val = mul_u64_u64_div_u64(period_ns, lpc18xx_pwm->clk_rate, NSEC_PER_SEC);
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm,
|
|
|
|
LPC18XX_PWM_MATCH(lpc18xx_pwm->period_event),
|
2022-07-12 16:15:19 +00:00
|
|
|
val - 1);
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm,
|
|
|
|
LPC18XX_PWM_MATCHREL(lpc18xx_pwm->period_event),
|
2022-07-12 16:15:19 +00:00
|
|
|
val - 1);
|
2015-08-06 02:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void lpc18xx_pwm_config_duty(struct pwm_chip *chip,
|
2022-07-12 16:15:19 +00:00
|
|
|
struct pwm_device *pwm, u64 duty_ns)
|
2015-08-06 02:31:46 +00:00
|
|
|
{
|
|
|
|
struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
|
pwm: lpc18xx-sct: Simplify driver by not using pwm_[gs]et_chip_data()
The per-channel data is available directly in the driver data struct. So
use it without making use of pwm_[gs]et_chip_data().
The relevant change introduced by this patch to lpc18xx_pwm_disable() at
the assembler level (for an arm lpc18xx_defconfig build) is:
push {r3, r4, r5, lr}
mov r4, r0
mov r0, r1
mov r5, r1
bl 0 <pwm_get_chip_data>
ldr r3, [r0, #0]
changes to
ldr r3, [r1, #8]
push {r4, lr}
add.w r3, r0, r3, lsl #2
ldr r3, [r3, #92] ; 0x5c
So this reduces stack usage, has an improved runtime behavior because of
better pipeline usage, doesn't branch to an external function and the
generated code is a bit smaller occupying less memory.
The codesize of lpc18xx_pwm_probe() is reduced by 32 bytes.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2021-11-10 08:49:50 +00:00
|
|
|
struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
|
2022-07-12 16:15:19 +00:00
|
|
|
u32 val;
|
2015-08-06 02:31:46 +00:00
|
|
|
|
2022-07-12 16:15:19 +00:00
|
|
|
/*
|
2022-12-02 18:35:07 +00:00
|
|
|
* With clk_rate <= NSEC_PER_SEC this cannot overflow.
|
2022-07-12 16:15:19 +00:00
|
|
|
* With duty_ns <= period_ns < max_period_ns this also fits into an u32.
|
|
|
|
*/
|
|
|
|
val = mul_u64_u64_div_u64(duty_ns, lpc18xx_pwm->clk_rate, NSEC_PER_SEC);
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm,
|
|
|
|
LPC18XX_PWM_MATCH(lpc18xx_data->duty_event),
|
2022-07-12 16:15:19 +00:00
|
|
|
val);
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm,
|
|
|
|
LPC18XX_PWM_MATCHREL(lpc18xx_data->duty_event),
|
2022-07-12 16:15:19 +00:00
|
|
|
val);
|
2015-08-06 02:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lpc18xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
|
|
|
|
int duty_ns, int period_ns)
|
|
|
|
{
|
|
|
|
struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
|
2023-12-09 23:00:46 +00:00
|
|
|
int requested_events;
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
if (period_ns < lpc18xx_pwm->min_period_ns ||
|
|
|
|
period_ns > lpc18xx_pwm->max_period_ns) {
|
2024-02-14 09:31:48 +00:00
|
|
|
dev_err(pwmchip_parent(chip), "period %d not in range\n", period_ns);
|
2015-08-06 02:31:46 +00:00
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&lpc18xx_pwm->period_lock);
|
|
|
|
|
|
|
|
requested_events = bitmap_weight(&lpc18xx_pwm->event_map,
|
|
|
|
LPC18XX_PWM_EVENT_MAX);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The PWM supports only a single period for all PWM channels.
|
|
|
|
* Once the period is set, it can only be changed if no more than one
|
|
|
|
* channel is requested at that moment.
|
|
|
|
*/
|
|
|
|
if (requested_events > 2 && lpc18xx_pwm->period_ns != period_ns &&
|
|
|
|
lpc18xx_pwm->period_ns) {
|
2024-02-14 09:31:48 +00:00
|
|
|
dev_err(pwmchip_parent(chip), "conflicting period requested for PWM %u\n",
|
2015-08-06 02:31:46 +00:00
|
|
|
pwm->hwpwm);
|
|
|
|
mutex_unlock(&lpc18xx_pwm->period_lock);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((requested_events <= 2 && lpc18xx_pwm->period_ns != period_ns) ||
|
|
|
|
!lpc18xx_pwm->period_ns) {
|
|
|
|
lpc18xx_pwm->period_ns = period_ns;
|
|
|
|
lpc18xx_pwm_config_period(chip, period_ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&lpc18xx_pwm->period_lock);
|
|
|
|
|
|
|
|
lpc18xx_pwm_config_duty(chip, pwm, duty_ns);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:40:32 +00:00
|
|
|
static int lpc18xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm, enum pwm_polarity polarity)
|
2015-08-06 02:31:46 +00:00
|
|
|
{
|
|
|
|
struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
|
pwm: lpc18xx-sct: Simplify driver by not using pwm_[gs]et_chip_data()
The per-channel data is available directly in the driver data struct. So
use it without making use of pwm_[gs]et_chip_data().
The relevant change introduced by this patch to lpc18xx_pwm_disable() at
the assembler level (for an arm lpc18xx_defconfig build) is:
push {r3, r4, r5, lr}
mov r4, r0
mov r0, r1
mov r5, r1
bl 0 <pwm_get_chip_data>
ldr r3, [r0, #0]
changes to
ldr r3, [r1, #8]
push {r4, lr}
add.w r3, r0, r3, lsl #2
ldr r3, [r3, #92] ; 0x5c
So this reduces stack usage, has an improved runtime behavior because of
better pipeline usage, doesn't branch to an external function and the
generated code is a bit smaller occupying less memory.
The codesize of lpc18xx_pwm_probe() is reduced by 32 bytes.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2021-11-10 08:49:50 +00:00
|
|
|
struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
|
2015-08-06 02:31:46 +00:00
|
|
|
enum lpc18xx_pwm_res_action res_action;
|
|
|
|
unsigned int set_event, clear_event;
|
|
|
|
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm,
|
|
|
|
LPC18XX_PWM_EVCTRL(lpc18xx_data->duty_event),
|
|
|
|
LPC18XX_PWM_EVCTRL_MATCH(lpc18xx_data->duty_event) |
|
|
|
|
LPC18XX_PWM_EVCTRL_COMB_MATCH);
|
|
|
|
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm,
|
|
|
|
LPC18XX_PWM_EVSTATEMSK(lpc18xx_data->duty_event),
|
|
|
|
LPC18XX_PWM_EVSTATEMSK_ALL);
|
|
|
|
|
2022-05-08 09:40:32 +00:00
|
|
|
if (polarity == PWM_POLARITY_NORMAL) {
|
2015-08-06 02:31:46 +00:00
|
|
|
set_event = lpc18xx_pwm->period_event;
|
|
|
|
clear_event = lpc18xx_data->duty_event;
|
|
|
|
res_action = LPC18XX_PWM_RES_SET;
|
|
|
|
} else {
|
|
|
|
set_event = lpc18xx_data->duty_event;
|
|
|
|
clear_event = lpc18xx_pwm->period_event;
|
|
|
|
res_action = LPC18XX_PWM_RES_CLEAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTSET(pwm->hwpwm),
|
|
|
|
BIT(set_event));
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTCL(pwm->hwpwm),
|
|
|
|
BIT(clear_event));
|
|
|
|
lpc18xx_pwm_set_conflict_res(lpc18xx_pwm, pwm, res_action);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lpc18xx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
|
pwm: lpc18xx-sct: Simplify driver by not using pwm_[gs]et_chip_data()
The per-channel data is available directly in the driver data struct. So
use it without making use of pwm_[gs]et_chip_data().
The relevant change introduced by this patch to lpc18xx_pwm_disable() at
the assembler level (for an arm lpc18xx_defconfig build) is:
push {r3, r4, r5, lr}
mov r4, r0
mov r0, r1
mov r5, r1
bl 0 <pwm_get_chip_data>
ldr r3, [r0, #0]
changes to
ldr r3, [r1, #8]
push {r4, lr}
add.w r3, r0, r3, lsl #2
ldr r3, [r3, #92] ; 0x5c
So this reduces stack usage, has an improved runtime behavior because of
better pipeline usage, doesn't branch to an external function and the
generated code is a bit smaller occupying less memory.
The codesize of lpc18xx_pwm_probe() is reduced by 32 bytes.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2021-11-10 08:49:50 +00:00
|
|
|
struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm,
|
|
|
|
LPC18XX_PWM_EVCTRL(lpc18xx_data->duty_event), 0);
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTSET(pwm->hwpwm), 0);
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTCL(pwm->hwpwm), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lpc18xx_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
|
pwm: lpc18xx-sct: Simplify driver by not using pwm_[gs]et_chip_data()
The per-channel data is available directly in the driver data struct. So
use it without making use of pwm_[gs]et_chip_data().
The relevant change introduced by this patch to lpc18xx_pwm_disable() at
the assembler level (for an arm lpc18xx_defconfig build) is:
push {r3, r4, r5, lr}
mov r4, r0
mov r0, r1
mov r5, r1
bl 0 <pwm_get_chip_data>
ldr r3, [r0, #0]
changes to
ldr r3, [r1, #8]
push {r4, lr}
add.w r3, r0, r3, lsl #2
ldr r3, [r3, #92] ; 0x5c
So this reduces stack usage, has an improved runtime behavior because of
better pipeline usage, doesn't branch to an external function and the
generated code is a bit smaller occupying less memory.
The codesize of lpc18xx_pwm_probe() is reduced by 32 bytes.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2021-11-10 08:49:50 +00:00
|
|
|
struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
|
2015-08-06 02:31:46 +00:00
|
|
|
unsigned long event;
|
|
|
|
|
|
|
|
event = find_first_zero_bit(&lpc18xx_pwm->event_map,
|
|
|
|
LPC18XX_PWM_EVENT_MAX);
|
|
|
|
|
|
|
|
if (event >= LPC18XX_PWM_EVENT_MAX) {
|
2024-02-14 09:31:48 +00:00
|
|
|
dev_err(pwmchip_parent(chip),
|
2015-08-06 02:31:46 +00:00
|
|
|
"maximum number of simultaneous channels reached\n");
|
|
|
|
return -EBUSY;
|
2021-02-03 02:50:05 +00:00
|
|
|
}
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
set_bit(event, &lpc18xx_pwm->event_map);
|
|
|
|
lpc18xx_data->duty_event = event;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lpc18xx_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
|
pwm: lpc18xx-sct: Simplify driver by not using pwm_[gs]et_chip_data()
The per-channel data is available directly in the driver data struct. So
use it without making use of pwm_[gs]et_chip_data().
The relevant change introduced by this patch to lpc18xx_pwm_disable() at
the assembler level (for an arm lpc18xx_defconfig build) is:
push {r3, r4, r5, lr}
mov r4, r0
mov r0, r1
mov r5, r1
bl 0 <pwm_get_chip_data>
ldr r3, [r0, #0]
changes to
ldr r3, [r1, #8]
push {r4, lr}
add.w r3, r0, r3, lsl #2
ldr r3, [r3, #92] ; 0x5c
So this reduces stack usage, has an improved runtime behavior because of
better pipeline usage, doesn't branch to an external function and the
generated code is a bit smaller occupying less memory.
The codesize of lpc18xx_pwm_probe() is reduced by 32 bytes.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2021-11-10 08:49:50 +00:00
|
|
|
struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
clear_bit(lpc18xx_data->duty_event, &lpc18xx_pwm->event_map);
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:40:32 +00:00
|
|
|
static int lpc18xx_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 && pwm->state.enabled) {
|
|
|
|
lpc18xx_pwm_disable(chip, pwm);
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state->enabled) {
|
|
|
|
if (enabled)
|
|
|
|
lpc18xx_pwm_disable(chip, pwm);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-01 10:22:53 +00:00
|
|
|
err = lpc18xx_pwm_config(chip, pwm, state->duty_cycle, state->period);
|
2022-05-08 09:40:32 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!enabled)
|
|
|
|
err = lpc18xx_pwm_enable(chip, pwm, state->polarity);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2015-08-06 02:31:46 +00:00
|
|
|
static const struct pwm_ops lpc18xx_pwm_ops = {
|
2022-05-08 09:40:32 +00:00
|
|
|
.apply = lpc18xx_pwm_apply,
|
2015-08-06 02:31:46 +00:00
|
|
|
.request = lpc18xx_pwm_request,
|
|
|
|
.free = lpc18xx_pwm_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct of_device_id lpc18xx_pwm_of_match[] = {
|
|
|
|
{ .compatible = "nxp,lpc1850-sct-pwm" },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, lpc18xx_pwm_of_match);
|
|
|
|
|
|
|
|
static int lpc18xx_pwm_probe(struct platform_device *pdev)
|
|
|
|
{
|
2024-02-14 09:31:49 +00:00
|
|
|
struct pwm_chip *chip;
|
2015-08-06 02:31:46 +00:00
|
|
|
struct lpc18xx_pwm_chip *lpc18xx_pwm;
|
pwm: lpc18xx-sct: Simplify driver by not using pwm_[gs]et_chip_data()
The per-channel data is available directly in the driver data struct. So
use it without making use of pwm_[gs]et_chip_data().
The relevant change introduced by this patch to lpc18xx_pwm_disable() at
the assembler level (for an arm lpc18xx_defconfig build) is:
push {r3, r4, r5, lr}
mov r4, r0
mov r0, r1
mov r5, r1
bl 0 <pwm_get_chip_data>
ldr r3, [r0, #0]
changes to
ldr r3, [r1, #8]
push {r4, lr}
add.w r3, r0, r3, lsl #2
ldr r3, [r3, #92] ; 0x5c
So this reduces stack usage, has an improved runtime behavior because of
better pipeline usage, doesn't branch to an external function and the
generated code is a bit smaller occupying less memory.
The codesize of lpc18xx_pwm_probe() is reduced by 32 bytes.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2021-11-10 08:49:50 +00:00
|
|
|
int ret;
|
2015-08-06 02:31:46 +00:00
|
|
|
u64 val;
|
|
|
|
|
2024-02-14 09:31:50 +00:00
|
|
|
chip = devm_pwmchip_alloc(&pdev->dev, LPC18XX_NUM_PWMS, sizeof(*lpc18xx_pwm));
|
|
|
|
if (IS_ERR(chip))
|
|
|
|
return PTR_ERR(chip);
|
|
|
|
lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
|
2015-08-06 02:31:46 +00:00
|
|
|
|
2019-12-29 08:06:07 +00:00
|
|
|
lpc18xx_pwm->base = devm_platform_ioremap_resource(pdev, 0);
|
2015-08-06 02:31:46 +00:00
|
|
|
if (IS_ERR(lpc18xx_pwm->base))
|
|
|
|
return PTR_ERR(lpc18xx_pwm->base);
|
|
|
|
|
2023-07-18 14:41:28 +00:00
|
|
|
lpc18xx_pwm->pwm_clk = devm_clk_get_enabled(&pdev->dev, "pwm");
|
2022-07-12 16:15:18 +00:00
|
|
|
if (IS_ERR(lpc18xx_pwm->pwm_clk))
|
|
|
|
return dev_err_probe(&pdev->dev, PTR_ERR(lpc18xx_pwm->pwm_clk),
|
|
|
|
"failed to get pwm clock\n");
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
lpc18xx_pwm->clk_rate = clk_get_rate(lpc18xx_pwm->pwm_clk);
|
2023-07-18 14:41:28 +00:00
|
|
|
if (!lpc18xx_pwm->clk_rate)
|
|
|
|
return dev_err_probe(&pdev->dev,
|
|
|
|
-EINVAL, "pwm clock has no frequency\n");
|
2015-08-06 02:31:46 +00:00
|
|
|
|
2022-07-12 16:15:19 +00:00
|
|
|
/*
|
|
|
|
* If clkrate is too fast, the calculations in .apply() might overflow.
|
|
|
|
*/
|
2023-07-18 14:41:28 +00:00
|
|
|
if (lpc18xx_pwm->clk_rate > NSEC_PER_SEC)
|
|
|
|
return dev_err_probe(&pdev->dev, -EINVAL, "pwm clock to fast\n");
|
2022-07-12 16:15:19 +00:00
|
|
|
|
2015-08-06 02:31:46 +00:00
|
|
|
mutex_init(&lpc18xx_pwm->res_lock);
|
|
|
|
mutex_init(&lpc18xx_pwm->period_lock);
|
|
|
|
|
2022-07-12 16:15:19 +00:00
|
|
|
lpc18xx_pwm->max_period_ns =
|
|
|
|
mul_u64_u64_div_u64(NSEC_PER_SEC, LPC18XX_PWM_TIMER_MAX, lpc18xx_pwm->clk_rate);
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
lpc18xx_pwm->min_period_ns = DIV_ROUND_UP(NSEC_PER_SEC,
|
|
|
|
lpc18xx_pwm->clk_rate);
|
|
|
|
|
2024-02-14 09:31:49 +00:00
|
|
|
chip->ops = &lpc18xx_pwm_ops;
|
2015-08-06 02:31:46 +00:00
|
|
|
|
|
|
|
/* SCT counter must be in unify (32 bit) mode */
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CONFIG,
|
|
|
|
LPC18XX_PWM_CONFIG_UNIFY);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Everytime the timer counter reaches the period value, the related
|
|
|
|
* event will be triggered and the counter reset to 0.
|
|
|
|
*/
|
|
|
|
set_bit(LPC18XX_PWM_EVENT_PERIOD, &lpc18xx_pwm->event_map);
|
|
|
|
lpc18xx_pwm->period_event = LPC18XX_PWM_EVENT_PERIOD;
|
|
|
|
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm,
|
|
|
|
LPC18XX_PWM_EVSTATEMSK(lpc18xx_pwm->period_event),
|
|
|
|
LPC18XX_PWM_EVSTATEMSK_ALL);
|
|
|
|
|
|
|
|
val = LPC18XX_PWM_EVCTRL_MATCH(lpc18xx_pwm->period_event) |
|
|
|
|
LPC18XX_PWM_EVCTRL_COMB_MATCH;
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm,
|
|
|
|
LPC18XX_PWM_EVCTRL(lpc18xx_pwm->period_event), val);
|
|
|
|
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_LIMIT,
|
|
|
|
BIT(lpc18xx_pwm->period_event));
|
|
|
|
|
|
|
|
val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_CTRL);
|
|
|
|
val &= ~LPC18XX_PWM_BIDIR;
|
|
|
|
val &= ~LPC18XX_PWM_CTRL_HALT;
|
|
|
|
val &= ~LPC18XX_PWM_PRE_MASK;
|
|
|
|
val |= LPC18XX_PWM_PRE(0);
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL, val);
|
|
|
|
|
2024-02-14 09:31:49 +00:00
|
|
|
ret = pwmchip_add(chip);
|
2023-07-18 14:41:28 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return dev_err_probe(&pdev->dev, ret, "pwmchip_add failed\n");
|
2021-11-10 08:49:48 +00:00
|
|
|
|
2024-02-14 09:31:49 +00:00
|
|
|
platform_set_drvdata(pdev, chip);
|
2021-11-10 08:49:48 +00:00
|
|
|
|
2015-08-06 02:31:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-03 18:54:28 +00:00
|
|
|
static void lpc18xx_pwm_remove(struct platform_device *pdev)
|
2015-08-06 02:31:46 +00:00
|
|
|
{
|
2024-02-14 09:31:49 +00:00
|
|
|
struct pwm_chip *chip = platform_get_drvdata(pdev);
|
|
|
|
struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
|
2015-08-06 02:31:46 +00:00
|
|
|
u32 val;
|
|
|
|
|
2024-02-14 09:31:49 +00:00
|
|
|
pwmchip_remove(chip);
|
2021-03-27 21:24:28 +00:00
|
|
|
|
2015-08-06 02:31:46 +00:00
|
|
|
val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_CTRL);
|
|
|
|
lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL,
|
|
|
|
val | LPC18XX_PWM_CTRL_HALT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver lpc18xx_pwm_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "lpc18xx-sct-pwm",
|
|
|
|
.of_match_table = lpc18xx_pwm_of_match,
|
|
|
|
},
|
|
|
|
.probe = lpc18xx_pwm_probe,
|
2023-03-03 18:54:28 +00:00
|
|
|
.remove_new = lpc18xx_pwm_remove,
|
2015-08-06 02:31:46 +00:00
|
|
|
};
|
|
|
|
module_platform_driver(lpc18xx_pwm_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Ariel D'Alessandro <ariel@vanguardiasur.com.ar>");
|
|
|
|
MODULE_DESCRIPTION("NXP LPC18xx PWM driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|