Skip to content

Commit c269351

Browse files
pcercueithierryreding
authored andcommitted
pwm: jz4740: Obtain regmap from parent node
The TCU registers are shared between a handful of drivers, accessing them through the same regmap. While this driver is devicetree-compatible, it is never (as of now) probed from devicetree, so this change does not introduce a ABI problem with current devicetree files. Signed-off-by: Paul Cercueil <[email protected]> Tested-by: Mathieu Malaterre <[email protected]> Tested-by: Artur Rojek <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 485b56f commit c269351

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

drivers/pwm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ config PWM_JZ4740
236236
tristate "Ingenic JZ47xx PWM support"
237237
depends on MACH_INGENIC
238238
depends on COMMON_CLK
239+
select MFD_SYSCON
239240
help
240241
Generic PWM framework driver for Ingenic JZ47xx based
241242
machines.

drivers/pwm/pwm-jz4740.c

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@
1313
#include <linux/err.h>
1414
#include <linux/gpio.h>
1515
#include <linux/kernel.h>
16+
#include <linux/mfd/ingenic-tcu.h>
17+
#include <linux/mfd/syscon.h>
1618
#include <linux/module.h>
1719
#include <linux/of_device.h>
1820
#include <linux/platform_device.h>
1921
#include <linux/pwm.h>
20-
21-
#include <asm/mach-jz4740/timer.h>
22+
#include <linux/regmap.h>
2223

2324
#define NUM_PWM 8
2425

2526
struct jz4740_pwm_chip {
2627
struct pwm_chip chip;
28+
struct regmap *map;
2729
};
2830

2931
static inline struct jz4740_pwm_chip *to_jz4740(struct pwm_chip *chip)
@@ -76,36 +78,39 @@ static void jz4740_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
7678

7779
static int jz4740_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
7880
{
79-
uint32_t ctrl = jz4740_timer_get_ctrl(pwm->pwm);
81+
struct jz4740_pwm_chip *jz = to_jz4740(chip);
82+
83+
/* Enable PWM output */
84+
regmap_update_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm),
85+
TCU_TCSR_PWM_EN, TCU_TCSR_PWM_EN);
8086

81-
ctrl |= JZ_TIMER_CTRL_PWM_ENABLE;
82-
jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
83-
jz4740_timer_enable(pwm->hwpwm);
87+
/* Start counter */
88+
regmap_write(jz->map, TCU_REG_TESR, BIT(pwm->hwpwm));
8489

8590
return 0;
8691
}
8792

8893
static void jz4740_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
8994
{
90-
uint32_t ctrl = jz4740_timer_get_ctrl(pwm->hwpwm);
95+
struct jz4740_pwm_chip *jz = to_jz4740(chip);
9196

9297
/*
9398
* Set duty > period. This trick allows the TCU channels in TCU2 mode to
9499
* properly return to their init level.
95100
*/
96-
jz4740_timer_set_duty(pwm->hwpwm, 0xffff);
97-
jz4740_timer_set_period(pwm->hwpwm, 0x0);
101+
regmap_write(jz->map, TCU_REG_TDHRc(pwm->hwpwm), 0xffff);
102+
regmap_write(jz->map, TCU_REG_TDFRc(pwm->hwpwm), 0x0);
98103

99104
/*
100105
* Disable PWM output.
101106
* In TCU2 mode (channel 1/2 on JZ4750+), this must be done before the
102107
* counter is stopped, while in TCU1 mode the order does not matter.
103108
*/
104-
ctrl &= ~JZ_TIMER_CTRL_PWM_ENABLE;
105-
jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
109+
regmap_update_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm),
110+
TCU_TCSR_PWM_EN, 0);
106111

107112
/* Stop counter */
108-
jz4740_timer_disable(pwm->hwpwm);
113+
regmap_write(jz->map, TCU_REG_TECR, BIT(pwm->hwpwm));
109114
}
110115

111116
static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
@@ -115,7 +120,6 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
115120
unsigned long long tmp = 0xffffull * NSEC_PER_SEC;
116121
struct clk *clk = pwm_get_chip_data(pwm);
117122
unsigned long period, duty;
118-
uint16_t ctrl;
119123
long rate;
120124
int err;
121125

@@ -163,24 +167,32 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
163167
return err;
164168
}
165169

166-
jz4740_timer_set_count(pwm->hwpwm, 0);
167-
jz4740_timer_set_duty(pwm->hwpwm, duty);
168-
jz4740_timer_set_period(pwm->hwpwm, period);
170+
/* Reset counter to 0 */
171+
regmap_write(jz4740->map, TCU_REG_TCNTc(pwm->hwpwm), 0);
172+
173+
/* Set duty */
174+
regmap_write(jz4740->map, TCU_REG_TDHRc(pwm->hwpwm), duty);
169175

170-
ctrl = jz4740_timer_get_ctrl(pwm->hwpwm);
171-
ctrl |= JZ_TIMER_CTRL_PWM_ABBRUPT_SHUTDOWN;
176+
/* Set period */
177+
regmap_write(jz4740->map, TCU_REG_TDFRc(pwm->hwpwm), period);
172178

179+
/* Set abrupt shutdown */
180+
regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm),
181+
TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD);
182+
183+
/* Set polarity */
173184
switch (state->polarity) {
174185
case PWM_POLARITY_NORMAL:
175-
ctrl &= ~JZ_TIMER_CTRL_PWM_ACTIVE_LOW;
186+
regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm),
187+
TCU_TCSR_PWM_INITL_HIGH, 0);
176188
break;
177189
case PWM_POLARITY_INVERSED:
178-
ctrl |= JZ_TIMER_CTRL_PWM_ACTIVE_LOW;
190+
regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm),
191+
TCU_TCSR_PWM_INITL_HIGH,
192+
TCU_TCSR_PWM_INITL_HIGH);
179193
break;
180194
}
181195

182-
jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
183-
184196
if (state->enabled)
185197
jz4740_pwm_enable(chip, pwm);
186198

@@ -196,13 +208,20 @@ static const struct pwm_ops jz4740_pwm_ops = {
196208

197209
static int jz4740_pwm_probe(struct platform_device *pdev)
198210
{
211+
struct device *dev = &pdev->dev;
199212
struct jz4740_pwm_chip *jz4740;
200213

201-
jz4740 = devm_kzalloc(&pdev->dev, sizeof(*jz4740), GFP_KERNEL);
214+
jz4740 = devm_kzalloc(dev, sizeof(*jz4740), GFP_KERNEL);
202215
if (!jz4740)
203216
return -ENOMEM;
204217

205-
jz4740->chip.dev = &pdev->dev;
218+
jz4740->map = device_node_to_regmap(dev->parent->of_node);
219+
if (IS_ERR(jz4740->map)) {
220+
dev_err(dev, "regmap not found: %ld\n", PTR_ERR(jz4740->map));
221+
return PTR_ERR(jz4740->map);
222+
}
223+
224+
jz4740->chip.dev = dev;
206225
jz4740->chip.ops = &jz4740_pwm_ops;
207226
jz4740->chip.npwm = NUM_PWM;
208227
jz4740->chip.base = -1;

0 commit comments

Comments
 (0)