Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'pwm/for-6.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm

Pull pwm updates from Thierry Reding:
"This contains a bunch of cleanups and simplifications across the
board, as well as a number of small fixes.

Perhaps the most notable change here is the addition of an API that
allows PWMs to be used in atomic contexts, which is useful when time-
critical operations are involved, such as using a PWM to generate IR
signals.

Finally, I have decided to step down as PWM subsystem maintainer. Due
to other responsibilities I have lately not been able to find the time
that the subsystem deserves and Uwe, who has been helping out a lot
for the past few years and has many things planned for the future, has
kindly volunteered to take over. I have no doubt that he will be a
suitable replacement"

* tag 'pwm/for-6.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm: (44 commits)
MAINTAINERS: pwm: Thierry steps down, Uwe takes over
pwm: linux/pwm.h: fix Excess kernel-doc description warning
pwm: Add pwm_apply_state() compatibility stub
pwm: cros-ec: Drop documentation for dropped struct member
pwm: Drop two unused API functions
pwm: lpc18xx-sct: Don't modify the cached period of other PWM outputs
pwm: meson: Simplify using dev_err_probe()
pwm: stmpe: Silence duplicate error messages
pwm: Reduce number of pointer dereferences in pwm_device_request()
pwm: crc: Use consistent variable naming for driver data
pwm: omap-dmtimer: Drop locking
dt-bindings: pwm: ti,pwm-omap-dmtimer: Update binding for yaml
media: pwm-ir-tx: Trigger edges from hrtimer interrupt context
pwm: bcm2835: Allow PWM driver to be used in atomic context
pwm: Make it possible to apply PWM changes in atomic context
pwm: renesas: Remove unused include
pwm: Replace ENOTSUPP with EOPNOTSUPP
pwm: Rename pwm_apply_state() to pwm_apply_might_sleep()
pwm: Stop referencing pwm->chip
pwm: Update kernel doc for struct pwm_chip
...

+517 -365
-1
Documentation/devicetree/bindings/pwm/mediatek,pwm-disp.yaml
··· 8 8 9 9 maintainers: 10 10 - Jitao Shi <jitao.shi@mediatek.com> 11 - - Xinlei Lee <xinlei.lee@mediatek.com> 12 11 13 12 allOf: 14 13 - $ref: pwm.yaml#
-22
Documentation/devicetree/bindings/pwm/pwm-omap-dmtimer.txt
··· 1 - * OMAP PWM for dual-mode timers 2 - 3 - Required properties: 4 - - compatible: Shall contain "ti,omap-dmtimer-pwm". 5 - - ti,timers: phandle to PWM capable OMAP timer. See timer/ti,timer-dm.yaml for info 6 - about these timers. 7 - - #pwm-cells: Should be 3. See pwm.yaml in this directory for a description of 8 - the cells format. 9 - 10 - Optional properties: 11 - - ti,prescaler: Should be a value between 0 and 7, see the timers datasheet 12 - - ti,clock-source: Set dmtimer parent clock, values between 0 and 2: 13 - - 0x00 - high-frequency system clock (timer_sys_ck) 14 - - 0x01 - 32-kHz always-on clock (timer_32k_ck) 15 - - 0x02 - external clock (timer_ext_ck, OMAP2 only) 16 - 17 - Example: 18 - pwm9: dmtimer-pwm@9 { 19 - compatible = "ti,omap-dmtimer-pwm"; 20 - ti,timers = <&timer9>; 21 - #pwm-cells = <3>; 22 - };
+59
Documentation/devicetree/bindings/pwm/ti,omap-dmtimer-pwm.yaml
··· 1 + # SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/pwm/ti,omap-dmtimer-pwm.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: TI dual mode timer PWM controller 8 + 9 + maintainers: 10 + - Tony Lindgren <tony@atomide.com> 11 + 12 + description: 13 + TI dual mode timer instances have an IO pin for PWM capability 14 + 15 + allOf: 16 + - $ref: pwm.yaml# 17 + 18 + properties: 19 + compatible: 20 + const: ti,omap-dmtimer-pwm 21 + 22 + "#pwm-cells": 23 + const: 3 24 + 25 + ti,timers: 26 + description: Timer instance phandle for the PWM 27 + $ref: /schemas/types.yaml#/definitions/phandle 28 + 29 + ti,prescaler: 30 + description: | 31 + Legacy clock prescaler for timer. The timer counter is prescaled 32 + with 2^n where n is the prescaler. 33 + $ref: /schemas/types.yaml#/definitions/uint32 34 + enum: [ 0, 1, 2, 3, 4, 5, 6, 7 ] 35 + deprecated: true 36 + 37 + ti,clock-source: 38 + description: | 39 + Legacy clock for timer, please use assigned-clocks instead. 40 + 0x00 - high-frequency system clock (timer_sys_ck) 41 + 0x01 - 32-kHz always-on clock (timer_32k_ck) 42 + 0x02 - external clock (timer_ext_ck, OMAP2 only) 43 + $ref: /schemas/types.yaml#/definitions/uint32 44 + enum: [ 0, 1, 2 ] 45 + deprecated: true 46 + 47 + required: 48 + - compatible 49 + - ti,timers 50 + 51 + unevaluatedProperties: false 52 + 53 + examples: 54 + - | 55 + pwm9: pwm { 56 + compatible = "ti,omap-dmtimer-pwm"; 57 + ti,timers = <&timer9>; 58 + #pwm-cells = <3>; 59 + };
+13 -4
Documentation/driver-api/pwm.rst
··· 41 41 42 42 After being requested, a PWM has to be configured using:: 43 43 44 - int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state); 44 + int pwm_apply_might_sleep(struct pwm_device *pwm, struct pwm_state *state); 45 45 46 46 This API controls both the PWM period/duty_cycle config and the 47 47 enable/disable state. 48 + 49 + PWM devices can be used from atomic context, if the PWM does not sleep. You 50 + can check if this the case with:: 51 + 52 + bool pwm_might_sleep(struct pwm_device *pwm); 53 + 54 + If false, the PWM can also be configured from atomic context with:: 55 + 56 + int pwm_apply_atomic(struct pwm_device *pwm, struct pwm_state *state); 48 57 49 58 As a consumer, don't rely on the output's state for a disabled PWM. If it's 50 59 easily possible, drivers are supposed to emit the inactive state, but some ··· 66 57 EMI by phase shifting the individual channels of a chip. 67 58 68 59 The pwm_config(), pwm_enable() and pwm_disable() functions are just wrappers 69 - around pwm_apply_state() and should not be used if the user wants to change 60 + around pwm_apply_might_sleep() and should not be used if the user wants to change 70 61 several parameter at once. For example, if you see pwm_config() and 71 62 pwm_{enable,disable}() calls in the same function, this probably means you 72 - should switch to pwm_apply_state(). 63 + should switch to pwm_apply_might_sleep(). 73 64 74 65 The PWM user API also allows one to query the PWM state that was passed to the 75 - last invocation of pwm_apply_state() using pwm_get_state(). Note this is 66 + last invocation of pwm_apply_might_sleep() using pwm_get_state(). Note this is 76 67 different to what the driver has actually implemented if the request cannot be 77 68 satisfied exactly with the hardware in use. There is currently no way for 78 69 consumers to get the actually implemented settings.
+3 -4
MAINTAINERS
··· 17645 17645 F: drivers/media/rc/pwm-ir-tx.c 17646 17646 17647 17647 PWM SUBSYSTEM 17648 - M: Thierry Reding <thierry.reding@gmail.com> 17649 - R: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> 17648 + M: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> 17650 17649 L: linux-pwm@vger.kernel.org 17651 17650 S: Maintained 17652 17651 Q: https://patchwork.ozlabs.org/project/linux-pwm/list/ 17653 - T: git git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm.git 17652 + T: git https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git 17654 17653 F: Documentation/devicetree/bindings/gpio/gpio-mvebu.yaml 17655 17654 F: Documentation/devicetree/bindings/pwm/ 17656 17655 F: Documentation/driver-api/pwm.rst ··· 17659 17660 F: include/dt-bindings/pwm/ 17660 17661 F: include/linux/pwm.h 17661 17662 F: include/linux/pwm_backlight.h 17662 - K: pwm_(config|apply_state|ops) 17663 + K: pwm_(config|apply_might_sleep|apply_atomic|ops) 17663 17664 17664 17665 PXA GPIO DRIVER 17665 17666 M: Robert Jarzmik <robert.jarzmik@free.fr>
+3 -3
drivers/gpu/drm/i915/display/intel_backlight.c
··· 275 275 struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel; 276 276 277 277 pwm_set_relative_duty_cycle(&panel->backlight.pwm_state, level, 100); 278 - pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state); 278 + pwm_apply_might_sleep(panel->backlight.pwm, &panel->backlight.pwm_state); 279 279 } 280 280 281 281 static void ··· 428 428 intel_backlight_set_pwm_level(old_conn_state, level); 429 429 430 430 panel->backlight.pwm_state.enabled = false; 431 - pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state); 431 + pwm_apply_might_sleep(panel->backlight.pwm, &panel->backlight.pwm_state); 432 432 } 433 433 434 434 void intel_backlight_disable(const struct drm_connector_state *old_conn_state) ··· 750 750 751 751 pwm_set_relative_duty_cycle(&panel->backlight.pwm_state, level, 100); 752 752 panel->backlight.pwm_state.enabled = true; 753 - pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state); 753 + pwm_apply_might_sleep(panel->backlight.pwm, &panel->backlight.pwm_state); 754 754 } 755 755 756 756 static void __intel_backlight_enable(const struct intel_crtc_state *crtc_state,
+1 -1
drivers/gpu/drm/solomon/ssd130x.c
··· 319 319 320 320 pwm_init_state(ssd130x->pwm, &pwmstate); 321 321 pwm_set_relative_duty_cycle(&pwmstate, 50, 100); 322 - pwm_apply_state(ssd130x->pwm, &pwmstate); 322 + pwm_apply_might_sleep(ssd130x->pwm, &pwmstate); 323 323 324 324 /* Enable the PWM */ 325 325 pwm_enable(ssd130x->pwm);
+4 -4
drivers/hwmon/pwm-fan.c
··· 151 151 } 152 152 153 153 state->enabled = true; 154 - ret = pwm_apply_state(ctx->pwm, state); 154 + ret = pwm_apply_might_sleep(ctx->pwm, state); 155 155 if (ret) { 156 156 dev_err(ctx->dev, "failed to enable PWM\n"); 157 157 goto disable_regulator; ··· 181 181 182 182 state->enabled = false; 183 183 state->duty_cycle = 0; 184 - ret = pwm_apply_state(ctx->pwm, state); 184 + ret = pwm_apply_might_sleep(ctx->pwm, state); 185 185 if (ret) { 186 186 dev_err(ctx->dev, "failed to disable PWM\n"); 187 187 return ret; ··· 207 207 208 208 period = state->period; 209 209 state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); 210 - ret = pwm_apply_state(ctx->pwm, state); 210 + ret = pwm_apply_might_sleep(ctx->pwm, state); 211 211 if (ret) 212 212 return ret; 213 213 ret = pwm_fan_power_on(ctx); ··· 278 278 state, 279 279 &enable_regulator); 280 280 281 - pwm_apply_state(ctx->pwm, state); 281 + pwm_apply_might_sleep(ctx->pwm, state); 282 282 pwm_fan_switch_power(ctx, enable_regulator); 283 283 pwm_fan_update_state(ctx, 0); 284 284 }
+2 -2
drivers/input/misc/da7280.c
··· 352 352 state.duty_cycle = period_mag_multi; 353 353 } 354 354 355 - error = pwm_apply_state(haptics->pwm_dev, &state); 355 + error = pwm_apply_might_sleep(haptics->pwm_dev, &state); 356 356 if (error) 357 357 dev_err(haptics->dev, "Failed to apply pwm state: %d\n", error); 358 358 ··· 1175 1175 /* Sync up PWM state and ensure it is off. */ 1176 1176 pwm_init_state(haptics->pwm_dev, &state); 1177 1177 state.enabled = false; 1178 - error = pwm_apply_state(haptics->pwm_dev, &state); 1178 + error = pwm_apply_might_sleep(haptics->pwm_dev, &state); 1179 1179 if (error) { 1180 1180 dev_err(dev, "Failed to apply PWM state: %d\n", error); 1181 1181 return error;
+2 -2
drivers/input/misc/pwm-beeper.c
··· 39 39 state.period = period; 40 40 pwm_set_relative_duty_cycle(&state, 50, 100); 41 41 42 - error = pwm_apply_state(beeper->pwm, &state); 42 + error = pwm_apply_might_sleep(beeper->pwm, &state); 43 43 if (error) 44 44 return error; 45 45 ··· 138 138 /* Sync up PWM state and ensure it is off. */ 139 139 pwm_init_state(beeper->pwm, &state); 140 140 state.enabled = false; 141 - error = pwm_apply_state(beeper->pwm, &state); 141 + error = pwm_apply_might_sleep(beeper->pwm, &state); 142 142 if (error) { 143 143 dev_err(dev, "failed to apply initial PWM state: %d\n", 144 144 error);
+4 -4
drivers/input/misc/pwm-vibra.c
··· 56 56 pwm_set_relative_duty_cycle(&state, vibrator->level, 0xffff); 57 57 state.enabled = true; 58 58 59 - err = pwm_apply_state(vibrator->pwm, &state); 59 + err = pwm_apply_might_sleep(vibrator->pwm, &state); 60 60 if (err) { 61 61 dev_err(pdev, "failed to apply pwm state: %d\n", err); 62 62 return err; ··· 67 67 state.duty_cycle = vibrator->direction_duty_cycle; 68 68 state.enabled = true; 69 69 70 - err = pwm_apply_state(vibrator->pwm_dir, &state); 70 + err = pwm_apply_might_sleep(vibrator->pwm_dir, &state); 71 71 if (err) { 72 72 dev_err(pdev, "failed to apply dir-pwm state: %d\n", err); 73 73 pwm_disable(vibrator->pwm); ··· 160 160 /* Sync up PWM state and ensure it is off. */ 161 161 pwm_init_state(vibrator->pwm, &state); 162 162 state.enabled = false; 163 - err = pwm_apply_state(vibrator->pwm, &state); 163 + err = pwm_apply_might_sleep(vibrator->pwm, &state); 164 164 if (err) { 165 165 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n", 166 166 err); ··· 174 174 /* Sync up PWM state and ensure it is off. */ 175 175 pwm_init_state(vibrator->pwm_dir, &state); 176 176 state.enabled = false; 177 - err = pwm_apply_state(vibrator->pwm_dir, &state); 177 + err = pwm_apply_might_sleep(vibrator->pwm_dir, &state); 178 178 if (err) { 179 179 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n", 180 180 err);
+1 -1
drivers/leds/leds-pwm.c
··· 54 54 55 55 led_dat->pwmstate.duty_cycle = duty; 56 56 led_dat->pwmstate.enabled = true; 57 - return pwm_apply_state(led_dat->pwm, &led_dat->pwmstate); 57 + return pwm_apply_might_sleep(led_dat->pwm, &led_dat->pwmstate); 58 58 } 59 59 60 60 __attribute__((nonnull))
+2 -2
drivers/leds/rgb/leds-pwm-multicolor.c
··· 51 51 52 52 priv->leds[i].state.duty_cycle = duty; 53 53 priv->leds[i].state.enabled = duty > 0; 54 - ret = pwm_apply_state(priv->leds[i].pwm, 55 - &priv->leds[i].state); 54 + ret = pwm_apply_might_sleep(priv->leds[i].pwm, 55 + &priv->leds[i].state); 56 56 if (ret) 57 57 break; 58 58 }
+80 -7
drivers/media/rc/pwm-ir-tx.c
··· 10 10 #include <linux/slab.h> 11 11 #include <linux/of.h> 12 12 #include <linux/platform_device.h> 13 + #include <linux/hrtimer.h> 14 + #include <linux/completion.h> 13 15 #include <media/rc-core.h> 14 16 15 17 #define DRIVER_NAME "pwm-ir-tx" ··· 19 17 20 18 struct pwm_ir { 21 19 struct pwm_device *pwm; 22 - unsigned int carrier; 23 - unsigned int duty_cycle; 20 + struct hrtimer timer; 21 + struct completion tx_done; 22 + struct pwm_state *state; 23 + u32 carrier; 24 + u32 duty_cycle; 25 + const unsigned int *txbuf; 26 + unsigned int txbuf_len; 27 + unsigned int txbuf_index; 24 28 }; 25 29 26 30 static const struct of_device_id pwm_ir_of_match[] = { ··· 57 49 return 0; 58 50 } 59 51 60 - static int pwm_ir_tx(struct rc_dev *dev, unsigned int *txbuf, 61 - unsigned int count) 52 + static int pwm_ir_tx_sleep(struct rc_dev *dev, unsigned int *txbuf, 53 + unsigned int count) 62 54 { 63 55 struct pwm_ir *pwm_ir = dev->priv; 64 56 struct pwm_device *pwm = pwm_ir->pwm; ··· 76 68 77 69 for (i = 0; i < count; i++) { 78 70 state.enabled = !(i % 2); 79 - pwm_apply_state(pwm, &state); 71 + pwm_apply_might_sleep(pwm, &state); 80 72 81 73 edge = ktime_add_us(edge, txbuf[i]); 82 74 delta = ktime_us_delta(edge, ktime_get()); ··· 85 77 } 86 78 87 79 state.enabled = false; 88 - pwm_apply_state(pwm, &state); 80 + pwm_apply_might_sleep(pwm, &state); 89 81 90 82 return count; 83 + } 84 + 85 + static int pwm_ir_tx_atomic(struct rc_dev *dev, unsigned int *txbuf, 86 + unsigned int count) 87 + { 88 + struct pwm_ir *pwm_ir = dev->priv; 89 + struct pwm_device *pwm = pwm_ir->pwm; 90 + struct pwm_state state; 91 + 92 + pwm_init_state(pwm, &state); 93 + 94 + state.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, pwm_ir->carrier); 95 + pwm_set_relative_duty_cycle(&state, pwm_ir->duty_cycle, 100); 96 + 97 + pwm_ir->txbuf = txbuf; 98 + pwm_ir->txbuf_len = count; 99 + pwm_ir->txbuf_index = 0; 100 + pwm_ir->state = &state; 101 + 102 + hrtimer_start(&pwm_ir->timer, 0, HRTIMER_MODE_REL); 103 + 104 + wait_for_completion(&pwm_ir->tx_done); 105 + 106 + return count; 107 + } 108 + 109 + static enum hrtimer_restart pwm_ir_timer(struct hrtimer *timer) 110 + { 111 + struct pwm_ir *pwm_ir = container_of(timer, struct pwm_ir, timer); 112 + ktime_t now; 113 + 114 + /* 115 + * If we happen to hit an odd latency spike, loop through the 116 + * pulses until we catch up. 117 + */ 118 + do { 119 + u64 ns; 120 + 121 + pwm_ir->state->enabled = !(pwm_ir->txbuf_index % 2); 122 + pwm_apply_atomic(pwm_ir->pwm, pwm_ir->state); 123 + 124 + if (pwm_ir->txbuf_index >= pwm_ir->txbuf_len) { 125 + complete(&pwm_ir->tx_done); 126 + 127 + return HRTIMER_NORESTART; 128 + } 129 + 130 + ns = US_TO_NS(pwm_ir->txbuf[pwm_ir->txbuf_index]); 131 + hrtimer_add_expires_ns(timer, ns); 132 + 133 + pwm_ir->txbuf_index++; 134 + 135 + now = timer->base->get_time(); 136 + } while (hrtimer_get_expires_tv64(timer) < now); 137 + 138 + return HRTIMER_RESTART; 91 139 } 92 140 93 141 static int pwm_ir_probe(struct platform_device *pdev) ··· 167 103 if (!rcdev) 168 104 return -ENOMEM; 169 105 106 + if (pwm_might_sleep(pwm_ir->pwm)) { 107 + dev_info(&pdev->dev, "TX will not be accurate as PWM device might sleep\n"); 108 + rcdev->tx_ir = pwm_ir_tx_sleep; 109 + } else { 110 + init_completion(&pwm_ir->tx_done); 111 + hrtimer_init(&pwm_ir->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 112 + pwm_ir->timer.function = pwm_ir_timer; 113 + rcdev->tx_ir = pwm_ir_tx_atomic; 114 + } 115 + 170 116 rcdev->priv = pwm_ir; 171 117 rcdev->driver_name = DRIVER_NAME; 172 118 rcdev->device_name = DEVICE_NAME; 173 - rcdev->tx_ir = pwm_ir_tx; 174 119 rcdev->s_tx_duty_cycle = pwm_ir_set_duty_cycle; 175 120 rcdev->s_tx_carrier = pwm_ir_set_carrier; 176 121
+1 -1
drivers/platform/x86/lenovo-yogabook.c
··· 435 435 .enabled = level, 436 436 }; 437 437 438 - pwm_apply_state(data->kbd_bl_pwm, &state); 438 + pwm_apply_might_sleep(data->kbd_bl_pwm, &state); 439 439 gpiod_set_value(data->kbd_bl_led_enable, level ? 1 : 0); 440 440 return 0; 441 441 }
+94 -70
drivers/pwm/core.c
··· 8 8 9 9 #include <linux/acpi.h> 10 10 #include <linux/module.h> 11 + #include <linux/idr.h> 11 12 #include <linux/of.h> 12 13 #include <linux/pwm.h> 13 14 #include <linux/list.h> ··· 24 23 #define CREATE_TRACE_POINTS 25 24 #include <trace/events/pwm.h> 26 25 27 - #define MAX_PWMS 1024 28 - 29 26 static DEFINE_MUTEX(pwm_lookup_lock); 30 27 static LIST_HEAD(pwm_lookup_list); 31 28 32 - /* protects access to pwm_chips and allocated_pwms */ 29 + /* protects access to pwm_chips */ 33 30 static DEFINE_MUTEX(pwm_lock); 34 31 35 - static LIST_HEAD(pwm_chips); 36 - static DECLARE_BITMAP(allocated_pwms, MAX_PWMS); 37 - 38 - /* Called with pwm_lock held */ 39 - static int alloc_pwms(unsigned int count) 40 - { 41 - unsigned int start; 42 - 43 - start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, 0, 44 - count, 0); 45 - 46 - if (start + count > MAX_PWMS) 47 - return -ENOSPC; 48 - 49 - bitmap_set(allocated_pwms, start, count); 50 - 51 - return start; 52 - } 53 - 54 - /* Called with pwm_lock held */ 55 - static void free_pwms(struct pwm_chip *chip) 56 - { 57 - bitmap_clear(allocated_pwms, chip->base, chip->npwm); 58 - 59 - kfree(chip->pwms); 60 - chip->pwms = NULL; 61 - } 32 + static DEFINE_IDR(pwm_chips); 62 33 63 34 static struct pwm_chip *pwmchip_find_by_name(const char *name) 64 35 { 65 36 struct pwm_chip *chip; 37 + unsigned long id, tmp; 66 38 67 39 if (!name) 68 40 return NULL; 69 41 70 42 mutex_lock(&pwm_lock); 71 43 72 - list_for_each_entry(chip, &pwm_chips, list) { 44 + idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) { 73 45 const char *chip_name = dev_name(chip->dev); 74 46 75 47 if (chip_name && strcmp(chip_name, name) == 0) { ··· 59 85 static int pwm_device_request(struct pwm_device *pwm, const char *label) 60 86 { 61 87 int err; 88 + struct pwm_chip *chip = pwm->chip; 89 + const struct pwm_ops *ops = chip->ops; 62 90 63 91 if (test_bit(PWMF_REQUESTED, &pwm->flags)) 64 92 return -EBUSY; 65 93 66 - if (!try_module_get(pwm->chip->owner)) 94 + if (!try_module_get(chip->owner)) 67 95 return -ENODEV; 68 96 69 - if (pwm->chip->ops->request) { 70 - err = pwm->chip->ops->request(pwm->chip, pwm); 97 + if (ops->request) { 98 + err = ops->request(chip, pwm); 71 99 if (err) { 72 - module_put(pwm->chip->owner); 100 + module_put(chip->owner); 73 101 return err; 74 102 } 75 103 } 76 104 77 - if (pwm->chip->ops->get_state) { 105 + if (ops->get_state) { 78 106 /* 79 107 * Zero-initialize state because most drivers are unaware of 80 108 * .usage_power. The other members of state are supposed to be ··· 86 110 */ 87 111 struct pwm_state state = { 0, }; 88 112 89 - err = pwm->chip->ops->get_state(pwm->chip, pwm, &state); 113 + err = ops->get_state(chip, pwm, &state); 90 114 trace_pwm_get(pwm, &state, err); 91 115 92 116 if (!err) ··· 210 234 */ 211 235 int __pwmchip_add(struct pwm_chip *chip, struct module *owner) 212 236 { 213 - struct pwm_device *pwm; 214 237 unsigned int i; 215 238 int ret; 216 239 ··· 221 246 222 247 chip->owner = owner; 223 248 224 - chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); 249 + chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL); 225 250 if (!chip->pwms) 226 251 return -ENOMEM; 227 252 228 253 mutex_lock(&pwm_lock); 229 254 230 - ret = alloc_pwms(chip->npwm); 255 + ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL); 231 256 if (ret < 0) { 232 257 mutex_unlock(&pwm_lock); 233 258 kfree(chip->pwms); 234 259 return ret; 235 260 } 236 261 237 - chip->base = ret; 262 + chip->id = ret; 238 263 239 264 for (i = 0; i < chip->npwm; i++) { 240 - pwm = &chip->pwms[i]; 265 + struct pwm_device *pwm = &chip->pwms[i]; 241 266 242 267 pwm->chip = chip; 243 - pwm->pwm = chip->base + i; 244 268 pwm->hwpwm = i; 245 269 } 246 - 247 - list_add(&chip->list, &pwm_chips); 248 270 249 271 mutex_unlock(&pwm_lock); 250 272 ··· 269 297 270 298 mutex_lock(&pwm_lock); 271 299 272 - list_del_init(&chip->list); 273 - 274 - free_pwms(chip); 300 + idr_remove(&pwm_chips, chip->id); 275 301 276 302 mutex_unlock(&pwm_lock); 303 + 304 + kfree(chip->pwms); 277 305 } 278 306 EXPORT_SYMBOL_GPL(pwmchip_remove); 279 307 ··· 328 356 } 329 357 EXPORT_SYMBOL_GPL(pwm_request_from_chip); 330 358 331 - static void pwm_apply_state_debug(struct pwm_device *pwm, 332 - const struct pwm_state *state) 359 + static void pwm_apply_debug(struct pwm_device *pwm, 360 + const struct pwm_state *state) 333 361 { 334 362 struct pwm_state *last = &pwm->last; 335 363 struct pwm_chip *chip = pwm->chip; ··· 435 463 } 436 464 437 465 /** 438 - * pwm_apply_state() - atomically apply a new state to a PWM device 466 + * __pwm_apply() - atomically apply a new state to a PWM device 439 467 * @pwm: PWM device 440 468 * @state: new state to apply 441 469 */ 442 - int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) 470 + static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state) 443 471 { 444 472 struct pwm_chip *chip; 445 473 int err; 446 - 447 - /* 448 - * Some lowlevel driver's implementations of .apply() make use of 449 - * mutexes, also with some drivers only returning when the new 450 - * configuration is active calling pwm_apply_state() from atomic context 451 - * is a bad idea. So make it explicit that calling this function might 452 - * sleep. 453 - */ 454 - might_sleep(); 455 474 456 475 if (!pwm || !state || !state->period || 457 476 state->duty_cycle > state->period) ··· 468 505 * only do this after pwm->state was applied as some 469 506 * implementations of .get_state depend on this 470 507 */ 471 - pwm_apply_state_debug(pwm, state); 508 + pwm_apply_debug(pwm, state); 472 509 473 510 return 0; 474 511 } 475 - EXPORT_SYMBOL_GPL(pwm_apply_state); 512 + 513 + /** 514 + * pwm_apply_might_sleep() - atomically apply a new state to a PWM device 515 + * Cannot be used in atomic context. 516 + * @pwm: PWM device 517 + * @state: new state to apply 518 + */ 519 + int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) 520 + { 521 + int err; 522 + 523 + /* 524 + * Some lowlevel driver's implementations of .apply() make use of 525 + * mutexes, also with some drivers only returning when the new 526 + * configuration is active calling pwm_apply_might_sleep() from atomic context 527 + * is a bad idea. So make it explicit that calling this function might 528 + * sleep. 529 + */ 530 + might_sleep(); 531 + 532 + if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) { 533 + /* 534 + * Catch any drivers that have been marked as atomic but 535 + * that will sleep anyway. 536 + */ 537 + non_block_start(); 538 + err = __pwm_apply(pwm, state); 539 + non_block_end(); 540 + } else { 541 + err = __pwm_apply(pwm, state); 542 + } 543 + 544 + return err; 545 + } 546 + EXPORT_SYMBOL_GPL(pwm_apply_might_sleep); 547 + 548 + /** 549 + * pwm_apply_atomic() - apply a new state to a PWM device from atomic context 550 + * Not all PWM devices support this function, check with pwm_might_sleep(). 551 + * @pwm: PWM device 552 + * @state: new state to apply 553 + */ 554 + int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state) 555 + { 556 + WARN_ONCE(!pwm->chip->atomic, 557 + "sleeping PWM driver used in atomic context\n"); 558 + 559 + return __pwm_apply(pwm, state); 560 + } 561 + EXPORT_SYMBOL_GPL(pwm_apply_atomic); 476 562 477 563 /** 478 564 * pwm_capture() - capture and report a PWM signal ··· 579 567 state.period = pargs.period; 580 568 state.polarity = pargs.polarity; 581 569 582 - return pwm_apply_state(pwm, &state); 570 + return pwm_apply_might_sleep(pwm, &state); 583 571 } 584 572 585 573 /* ··· 602 590 state.duty_cycle = state.period - state.duty_cycle; 603 591 } 604 592 605 - return pwm_apply_state(pwm, &state); 593 + return pwm_apply_might_sleep(pwm, &state); 606 594 } 607 595 EXPORT_SYMBOL_GPL(pwm_adjust_config); 608 596 609 597 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) 610 598 { 611 599 struct pwm_chip *chip; 600 + unsigned long id, tmp; 612 601 613 602 mutex_lock(&pwm_lock); 614 603 615 - list_for_each_entry(chip, &pwm_chips, list) 604 + idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) 616 605 if (chip->dev && device_match_fwnode(chip->dev, fwnode)) { 617 606 mutex_unlock(&pwm_lock); 618 607 return chip; ··· 1071 1058 1072 1059 static void *pwm_seq_start(struct seq_file *s, loff_t *pos) 1073 1060 { 1061 + unsigned long id = *pos; 1062 + void *ret; 1063 + 1074 1064 mutex_lock(&pwm_lock); 1075 1065 s->private = ""; 1076 1066 1077 - return seq_list_start(&pwm_chips, *pos); 1067 + ret = idr_get_next_ul(&pwm_chips, &id); 1068 + *pos = id; 1069 + return ret; 1078 1070 } 1079 1071 1080 1072 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) 1081 1073 { 1074 + unsigned long id = *pos + 1; 1075 + void *ret; 1076 + 1082 1077 s->private = "\n"; 1083 1078 1084 - return seq_list_next(v, &pwm_chips, pos); 1079 + ret = idr_get_next_ul(&pwm_chips, &id); 1080 + *pos = id; 1081 + return ret; 1085 1082 } 1086 1083 1087 1084 static void pwm_seq_stop(struct seq_file *s, void *v) ··· 1101 1078 1102 1079 static int pwm_seq_show(struct seq_file *s, void *v) 1103 1080 { 1104 - struct pwm_chip *chip = list_entry(v, struct pwm_chip, list); 1081 + struct pwm_chip *chip = v; 1105 1082 1106 - seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, 1083 + seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", 1084 + (char *)s->private, chip->id, 1107 1085 chip->dev->bus ? chip->dev->bus->name : "no-bus", 1108 1086 dev_name(chip->dev), chip->npwm, 1109 1087 (chip->npwm != 1) ? "s" : "");
+3 -5
drivers/pwm/pwm-atmel-hlcdc.c
··· 180 180 .div1_clk_erratum = true, 181 181 }; 182 182 183 - #ifdef CONFIG_PM_SLEEP 184 183 static int atmel_hlcdc_pwm_suspend(struct device *dev) 185 184 { 186 185 struct atmel_hlcdc_pwm *atmel = dev_get_drvdata(dev); ··· 209 210 return atmel_hlcdc_pwm_apply(&atmel->chip, &atmel->chip.pwms[0], 210 211 &state); 211 212 } 212 - #endif 213 213 214 - static SIMPLE_DEV_PM_OPS(atmel_hlcdc_pwm_pm_ops, 215 - atmel_hlcdc_pwm_suspend, atmel_hlcdc_pwm_resume); 214 + static DEFINE_SIMPLE_DEV_PM_OPS(atmel_hlcdc_pwm_pm_ops, 215 + atmel_hlcdc_pwm_suspend, atmel_hlcdc_pwm_resume); 216 216 217 217 static const struct of_device_id atmel_hlcdc_dt_ids[] = { 218 218 { ··· 295 297 .driver = { 296 298 .name = "atmel-hlcdc-pwm", 297 299 .of_match_table = atmel_hlcdc_pwm_dt_ids, 298 - .pm = &atmel_hlcdc_pwm_pm_ops, 300 + .pm = pm_ptr(&atmel_hlcdc_pwm_pm_ops), 299 301 }, 300 302 .probe = atmel_hlcdc_pwm_probe, 301 303 .remove_new = atmel_hlcdc_pwm_remove,
+3 -5
drivers/pwm/pwm-atmel-tcb.c
··· 489 489 }; 490 490 MODULE_DEVICE_TABLE(of, atmel_tcb_pwm_dt_ids); 491 491 492 - #ifdef CONFIG_PM_SLEEP 493 492 static int atmel_tcb_pwm_suspend(struct device *dev) 494 493 { 495 494 struct atmel_tcb_pwm_chip *tcbpwm = dev_get_drvdata(dev); ··· 521 522 522 523 return 0; 523 524 } 524 - #endif 525 525 526 - static SIMPLE_DEV_PM_OPS(atmel_tcb_pwm_pm_ops, atmel_tcb_pwm_suspend, 527 - atmel_tcb_pwm_resume); 526 + static DEFINE_SIMPLE_DEV_PM_OPS(atmel_tcb_pwm_pm_ops, atmel_tcb_pwm_suspend, 527 + atmel_tcb_pwm_resume); 528 528 529 529 static struct platform_driver atmel_tcb_pwm_driver = { 530 530 .driver = { 531 531 .name = "atmel-tcb-pwm", 532 532 .of_match_table = atmel_tcb_pwm_dt_ids, 533 - .pm = &atmel_tcb_pwm_pm_ops, 533 + .pm = pm_ptr(&atmel_tcb_pwm_pm_ops), 534 534 }, 535 535 .probe = atmel_tcb_pwm_probe, 536 536 .remove_new = atmel_tcb_pwm_remove,
+1 -1
drivers/pwm/pwm-bcm-kona.c
··· 260 260 return err; 261 261 } 262 262 263 - err = kona_pwmc_config(pwm->chip, pwm, state->duty_cycle, state->period); 263 + err = kona_pwmc_config(chip, pwm, state->duty_cycle, state->period); 264 264 if (err && !pwm->state.enabled) 265 265 clk_disable_unprepare(kp->clk); 266 266
+29 -9
drivers/pwm/pwm-bcm2835.c
··· 28 28 struct device *dev; 29 29 void __iomem *base; 30 30 struct clk *clk; 31 + unsigned long rate; 31 32 }; 32 33 33 34 static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip) ··· 64 63 { 65 64 66 65 struct bcm2835_pwm *pc = to_bcm2835_pwm(chip); 67 - unsigned long rate = clk_get_rate(pc->clk); 68 66 unsigned long long period_cycles; 69 67 u64 max_period; 70 68 71 69 u32 val; 72 - 73 - if (!rate) { 74 - dev_err(pc->dev, "failed to get clock rate\n"); 75 - return -EINVAL; 76 - } 77 70 78 71 /* 79 72 * period_cycles must be a 32 bit value, so period * rate / NSEC_PER_SEC ··· 83 88 * <=> period < ((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate 84 89 * <=> period <= ceil((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate) - 1 85 90 */ 86 - max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, rate) - 1; 91 + max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, pc->rate) - 1; 87 92 88 93 if (state->period > max_period) 89 94 return -EINVAL; 90 95 91 96 /* set period */ 92 - period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * rate, NSEC_PER_SEC); 97 + period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * pc->rate, NSEC_PER_SEC); 93 98 94 99 /* don't accept a period that is too small */ 95 100 if (period_cycles < PERIOD_MIN) ··· 98 103 writel(period_cycles, pc->base + PERIOD(pwm->hwpwm)); 99 104 100 105 /* set duty cycle */ 101 - val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * rate, NSEC_PER_SEC); 106 + val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * pc->rate, NSEC_PER_SEC); 102 107 writel(val, pc->base + DUTY(pwm->hwpwm)); 103 108 104 109 /* set polarity */ ··· 126 131 .apply = bcm2835_pwm_apply, 127 132 }; 128 133 134 + static void devm_clk_rate_exclusive_put(void *data) 135 + { 136 + struct clk *clk = data; 137 + 138 + clk_rate_exclusive_put(clk); 139 + } 140 + 129 141 static int bcm2835_pwm_probe(struct platform_device *pdev) 130 142 { 131 143 struct bcm2835_pwm *pc; ··· 153 151 return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk), 154 152 "clock not found\n"); 155 153 154 + ret = clk_rate_exclusive_get(pc->clk); 155 + if (ret) 156 + return dev_err_probe(&pdev->dev, ret, 157 + "fail to get exclusive rate\n"); 158 + 159 + ret = devm_add_action_or_reset(&pdev->dev, devm_clk_rate_exclusive_put, 160 + pc->clk); 161 + if (ret) { 162 + clk_rate_exclusive_put(pc->clk); 163 + return ret; 164 + } 165 + 166 + pc->rate = clk_get_rate(pc->clk); 167 + if (!pc->rate) 168 + return dev_err_probe(&pdev->dev, -EINVAL, 169 + "failed to get clock rate\n"); 170 + 156 171 pc->chip.dev = &pdev->dev; 157 172 pc->chip.ops = &bcm2835_pwm_ops; 173 + pc->chip.atomic = true; 158 174 pc->chip.npwm = 2; 159 175 160 176 platform_set_drvdata(pdev, pc);
+3 -5
drivers/pwm/pwm-berlin.c
··· 226 226 return 0; 227 227 } 228 228 229 - #ifdef CONFIG_PM_SLEEP 230 229 static int berlin_pwm_suspend(struct device *dev) 231 230 { 232 231 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev); ··· 266 267 267 268 return 0; 268 269 } 269 - #endif 270 270 271 - static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend, 272 - berlin_pwm_resume); 271 + static DEFINE_SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend, 272 + berlin_pwm_resume); 273 273 274 274 static struct platform_driver berlin_pwm_driver = { 275 275 .probe = berlin_pwm_probe, 276 276 .driver = { 277 277 .name = "berlin-pwm", 278 278 .of_match_table = berlin_pwm_match, 279 - .pm = &berlin_pwm_pm_ops, 279 + .pm = pm_ptr(&berlin_pwm_pm_ops), 280 280 }, 281 281 }; 282 282 module_platform_driver(berlin_pwm_driver);
+3 -5
drivers/pwm/pwm-brcmstb.c
··· 259 259 return 0; 260 260 } 261 261 262 - #ifdef CONFIG_PM_SLEEP 263 262 static int brcmstb_pwm_suspend(struct device *dev) 264 263 { 265 264 struct brcmstb_pwm *p = dev_get_drvdata(dev); ··· 274 275 275 276 return clk_prepare_enable(p->clk); 276 277 } 277 - #endif 278 278 279 - static SIMPLE_DEV_PM_OPS(brcmstb_pwm_pm_ops, brcmstb_pwm_suspend, 280 - brcmstb_pwm_resume); 279 + static DEFINE_SIMPLE_DEV_PM_OPS(brcmstb_pwm_pm_ops, brcmstb_pwm_suspend, 280 + brcmstb_pwm_resume); 281 281 282 282 static struct platform_driver brcmstb_pwm_driver = { 283 283 .probe = brcmstb_pwm_probe, 284 284 .driver = { 285 285 .name = "pwm-brcmstb", 286 286 .of_match_table = brcmstb_pwm_of_match, 287 - .pm = &brcmstb_pwm_pm_ops, 287 + .pm = pm_ptr(&brcmstb_pwm_pm_ops), 288 288 }, 289 289 }; 290 290 module_platform_driver(brcmstb_pwm_driver);
+8 -8
drivers/pwm/pwm-crc.c
··· 160 160 161 161 static int crystalcove_pwm_probe(struct platform_device *pdev) 162 162 { 163 - struct crystalcove_pwm *pwm; 163 + struct crystalcove_pwm *crc_pwm; 164 164 struct device *dev = pdev->dev.parent; 165 165 struct intel_soc_pmic *pmic = dev_get_drvdata(dev); 166 166 167 - pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); 168 - if (!pwm) 167 + crc_pwm = devm_kzalloc(&pdev->dev, sizeof(*crc_pwm), GFP_KERNEL); 168 + if (!crc_pwm) 169 169 return -ENOMEM; 170 170 171 - pwm->chip.dev = &pdev->dev; 172 - pwm->chip.ops = &crc_pwm_ops; 173 - pwm->chip.npwm = 1; 171 + crc_pwm->chip.dev = &pdev->dev; 172 + crc_pwm->chip.ops = &crc_pwm_ops; 173 + crc_pwm->chip.npwm = 1; 174 174 175 175 /* get the PMIC regmap */ 176 - pwm->regmap = pmic->regmap; 176 + crc_pwm->regmap = pmic->regmap; 177 177 178 - return devm_pwmchip_add(&pdev->dev, &pwm->chip); 178 + return devm_pwmchip_add(&pdev->dev, &crc_pwm->chip); 179 179 } 180 180 181 181 static struct platform_driver crystalcove_pwm_driver = {
-2
drivers/pwm/pwm-cros-ec.c
··· 18 18 /** 19 19 * struct cros_ec_pwm_device - Driver data for EC PWM 20 20 * 21 - * @dev: Device node 22 21 * @ec: Pointer to EC device 23 22 * @chip: PWM controller chip 24 23 * @use_pwm_type: Use PWM types instead of generic channels 25 24 * @channel: array with per-channel data 26 25 */ 27 26 struct cros_ec_pwm_device { 28 - struct device *dev; 29 27 struct cros_ec_device *ec; 30 28 struct pwm_chip chip; 31 29 bool use_pwm_type;
+2 -4
drivers/pwm/pwm-dwc.c
··· 71 71 pm_runtime_get_noresume(&pci->dev); 72 72 } 73 73 74 - #ifdef CONFIG_PM_SLEEP 75 74 static int dwc_pwm_suspend(struct device *dev) 76 75 { 77 76 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev); ··· 105 106 106 107 return 0; 107 108 } 108 - #endif 109 109 110 - static SIMPLE_DEV_PM_OPS(dwc_pwm_pm_ops, dwc_pwm_suspend, dwc_pwm_resume); 110 + static DEFINE_SIMPLE_DEV_PM_OPS(dwc_pwm_pm_ops, dwc_pwm_suspend, dwc_pwm_resume); 111 111 112 112 static const struct pci_device_id dwc_pwm_id_table[] = { 113 113 { PCI_VDEVICE(INTEL, 0x4bb7) }, /* Elkhart Lake */ ··· 120 122 .remove = dwc_pwm_remove, 121 123 .id_table = dwc_pwm_id_table, 122 124 .driver = { 123 - .pm = &dwc_pwm_pm_ops, 125 + .pm = pm_ptr(&dwc_pwm_pm_ops), 124 126 }, 125 127 }; 126 128
+3 -7
drivers/pwm/pwm-img.c
··· 13 13 #include <linux/mfd/syscon.h> 14 14 #include <linux/module.h> 15 15 #include <linux/of.h> 16 - #include <linux/of_device.h> 17 16 #include <linux/platform_device.h> 18 17 #include <linux/pm_runtime.h> 18 + #include <linux/property.h> 19 19 #include <linux/pwm.h> 20 20 #include <linux/regmap.h> 21 21 #include <linux/slab.h> ··· 196 196 return 0; 197 197 } 198 198 199 - err = img_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); 199 + err = img_pwm_config(chip, pwm, state->duty_cycle, state->period); 200 200 if (err) 201 201 return err; 202 202 ··· 260 260 u64 val; 261 261 unsigned long clk_rate; 262 262 struct img_pwm_chip *imgchip; 263 - const struct of_device_id *of_dev_id; 264 263 265 264 imgchip = devm_kzalloc(&pdev->dev, sizeof(*imgchip), GFP_KERNEL); 266 265 if (!imgchip) ··· 271 272 if (IS_ERR(imgchip->base)) 272 273 return PTR_ERR(imgchip->base); 273 274 274 - of_dev_id = of_match_device(img_pwm_of_match, &pdev->dev); 275 - if (!of_dev_id) 276 - return -ENODEV; 277 - imgchip->data = of_dev_id->data; 275 + imgchip->data = device_get_match_data(&pdev->dev); 278 276 279 277 imgchip->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, 280 278 "img,cr-periph");
+5 -5
drivers/pwm/pwm-imx-tpm.c
··· 371 371 return 0; 372 372 } 373 373 374 - static int __maybe_unused pwm_imx_tpm_suspend(struct device *dev) 374 + static int pwm_imx_tpm_suspend(struct device *dev) 375 375 { 376 376 struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev); 377 377 ··· 390 390 return 0; 391 391 } 392 392 393 - static int __maybe_unused pwm_imx_tpm_resume(struct device *dev) 393 + static int pwm_imx_tpm_resume(struct device *dev) 394 394 { 395 395 struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev); 396 396 int ret = 0; ··· 402 402 return ret; 403 403 } 404 404 405 - static SIMPLE_DEV_PM_OPS(imx_tpm_pwm_pm, 406 - pwm_imx_tpm_suspend, pwm_imx_tpm_resume); 405 + static DEFINE_SIMPLE_DEV_PM_OPS(imx_tpm_pwm_pm, 406 + pwm_imx_tpm_suspend, pwm_imx_tpm_resume); 407 407 408 408 static const struct of_device_id imx_tpm_pwm_dt_ids[] = { 409 409 { .compatible = "fsl,imx7ulp-pwm", }, ··· 415 415 .driver = { 416 416 .name = "imx7ulp-tpm-pwm", 417 417 .of_match_table = imx_tpm_pwm_dt_ids, 418 - .pm = &imx_tpm_pwm_pm, 418 + .pm = pm_ptr(&imx_tpm_pwm_pm), 419 419 }, 420 420 .probe = pwm_imx_tpm_probe, 421 421 };
+3 -3
drivers/pwm/pwm-jz4740.c
··· 123 123 static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 124 124 const struct pwm_state *state) 125 125 { 126 - struct jz4740_pwm_chip *jz = to_jz4740(pwm->chip); 126 + struct jz4740_pwm_chip *jz = to_jz4740(chip); 127 127 unsigned long long tmp = 0xffffull * NSEC_PER_SEC; 128 128 struct clk *clk = jz->clk[pwm->hwpwm]; 129 129 unsigned long period, duty; ··· 149 149 */ 150 150 rate = clk_round_rate(clk, tmp); 151 151 if (rate < 0) { 152 - dev_err(chip->dev, "Unable to round rate: %ld", rate); 152 + dev_err(chip->dev, "Unable to round rate: %ld\n", rate); 153 153 return rate; 154 154 } 155 155 ··· 170 170 171 171 err = clk_set_rate(clk, rate); 172 172 if (err) { 173 - dev_err(chip->dev, "Unable to set rate: %d", err); 173 + dev_err(chip->dev, "Unable to set rate: %d\n", err); 174 174 return err; 175 175 } 176 176
+2 -4
drivers/pwm/pwm-lpc18xx-sct.c
··· 194 194 int duty_ns, int period_ns) 195 195 { 196 196 struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip); 197 - int requested_events, i; 197 + int requested_events; 198 198 199 199 if (period_ns < lpc18xx_pwm->min_period_ns || 200 200 period_ns > lpc18xx_pwm->max_period_ns) { ··· 223 223 if ((requested_events <= 2 && lpc18xx_pwm->period_ns != period_ns) || 224 224 !lpc18xx_pwm->period_ns) { 225 225 lpc18xx_pwm->period_ns = period_ns; 226 - for (i = 0; i < chip->npwm; i++) 227 - pwm_set_period(&chip->pwms[i], period_ns); 228 226 lpc18xx_pwm_config_period(chip, period_ns); 229 227 } 230 228 ··· 326 328 return 0; 327 329 } 328 330 329 - err = lpc18xx_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); 331 + err = lpc18xx_pwm_config(chip, pwm, state->duty_cycle, state->period); 330 332 if (err) 331 333 return err; 332 334
+1 -1
drivers/pwm/pwm-lpc32xx.c
··· 103 103 return 0; 104 104 } 105 105 106 - err = lpc32xx_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); 106 + err = lpc32xx_pwm_config(chip, pwm, state->duty_cycle, state->period); 107 107 if (err) 108 108 return err; 109 109
+1 -1
drivers/pwm/pwm-mediatek.c
··· 217 217 return 0; 218 218 } 219 219 220 - err = pwm_mediatek_config(pwm->chip, pwm, state->duty_cycle, state->period); 220 + err = pwm_mediatek_config(chip, pwm, state->duty_cycle, state->period); 221 221 if (err) 222 222 return err; 223 223
+14 -21
drivers/pwm/pwm-meson.c
··· 468 468 channel->mux.hw.init = &init; 469 469 470 470 err = devm_clk_hw_register(dev, &channel->mux.hw); 471 - if (err) { 472 - dev_err(dev, "failed to register %s: %d\n", name, err); 473 - return err; 474 - } 471 + if (err) 472 + return dev_err_probe(dev, err, 473 + "failed to register %s\n", name); 475 474 476 475 snprintf(name, sizeof(name), "%s#div%u", dev_name(dev), i); 477 476 ··· 490 491 channel->div.lock = &meson->lock; 491 492 492 493 err = devm_clk_hw_register(dev, &channel->div.hw); 493 - if (err) { 494 - dev_err(dev, "failed to register %s: %d\n", name, err); 495 - return err; 496 - } 494 + if (err) 495 + return dev_err_probe(dev, err, 496 + "failed to register %s\n", name); 497 497 498 498 snprintf(name, sizeof(name), "%s#gate%u", dev_name(dev), i); 499 499 ··· 511 513 channel->gate.lock = &meson->lock; 512 514 513 515 err = devm_clk_hw_register(dev, &channel->gate.hw); 514 - if (err) { 515 - dev_err(dev, "failed to register %s: %d\n", name, err); 516 - return err; 517 - } 516 + if (err) 517 + return dev_err_probe(dev, err, "failed to register %s\n", name); 518 518 519 519 channel->clk = devm_clk_hw_get_clk(dev, &channel->gate.hw, NULL); 520 - if (IS_ERR(channel->clk)) { 521 - err = PTR_ERR(channel->clk); 522 - dev_err(dev, "failed to register %s: %d\n", name, err); 523 - return err; 524 - } 520 + if (IS_ERR(channel->clk)) 521 + return dev_err_probe(dev, PTR_ERR(channel->clk), 522 + "failed to register %s\n", name); 525 523 } 526 524 527 525 return 0; ··· 548 554 return err; 549 555 550 556 err = devm_pwmchip_add(&pdev->dev, &meson->chip); 551 - if (err < 0) { 552 - dev_err(&pdev->dev, "failed to register PWM chip: %d\n", err); 553 - return err; 554 - } 557 + if (err < 0) 558 + return dev_err_probe(&pdev->dev, err, 559 + "failed to register PWM chip\n"); 555 560 556 561 return 0; 557 562 }
+4 -16
drivers/pwm/pwm-omap-dmtimer.c
··· 37 37 #include <linux/err.h> 38 38 #include <linux/kernel.h> 39 39 #include <linux/module.h> 40 - #include <linux/mutex.h> 41 40 #include <linux/of.h> 42 41 #include <linux/of_platform.h> 43 42 #include <clocksource/timer-ti-dm.h> ··· 54 55 * struct pwm_omap_dmtimer_chip - Structure representing a pwm chip 55 56 * corresponding to omap dmtimer. 56 57 * @chip: PWM chip structure representing PWM controller 57 - * @mutex: Mutex to protect pwm apply state 58 58 * @dm_timer: Pointer to omap dm timer. 59 59 * @pdata: Pointer to omap dm timer ops. 60 60 * @dm_timer_pdev: Pointer to omap dm timer platform device ··· 61 63 struct pwm_omap_dmtimer_chip { 62 64 struct pwm_chip chip; 63 65 /* Mutex to protect pwm apply state */ 64 - struct mutex mutex; 65 66 struct omap_dm_timer *dm_timer; 66 67 const struct omap_dm_timer_ops *pdata; 67 68 struct platform_device *dm_timer_pdev; ··· 274 277 const struct pwm_state *state) 275 278 { 276 279 struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip); 277 - int ret = 0; 278 - 279 - mutex_lock(&omap->mutex); 280 + int ret; 280 281 281 282 if (pwm_omap_dmtimer_is_enabled(omap) && !state->enabled) { 282 283 omap->pdata->stop(omap->dm_timer); 283 - goto unlock_mutex; 284 + return 0; 284 285 } 285 286 286 287 if (pwm_omap_dmtimer_polarity(omap) != state->polarity) ··· 287 292 ret = pwm_omap_dmtimer_config(chip, pwm, state->duty_cycle, 288 293 state->period); 289 294 if (ret) 290 - goto unlock_mutex; 295 + return ret; 291 296 292 297 if (!pwm_omap_dmtimer_is_enabled(omap) && state->enabled) { 293 298 omap->pdata->set_pwm(omap->dm_timer, ··· 298 303 pwm_omap_dmtimer_start(omap); 299 304 } 300 305 301 - unlock_mutex: 302 - mutex_unlock(&omap->mutex); 303 - 304 - return ret; 306 + return 0; 305 307 } 306 308 307 309 static const struct pwm_ops pwm_omap_dmtimer_ops = { ··· 396 404 omap->chip.ops = &pwm_omap_dmtimer_ops; 397 405 omap->chip.npwm = 1; 398 406 399 - mutex_init(&omap->mutex); 400 - 401 407 ret = pwmchip_add(&omap->chip); 402 408 if (ret < 0) { 403 409 dev_err(&pdev->dev, "failed to register PWM\n"); ··· 442 452 omap->pdata->free(omap->dm_timer); 443 453 444 454 put_device(&omap->dm_timer_pdev->dev); 445 - 446 - mutex_destroy(&omap->mutex); 447 455 } 448 456 449 457 static const struct of_device_id pwm_omap_dmtimer_of_match[] = {
+1 -2
drivers/pwm/pwm-renesas-tpu.c
··· 11 11 #include <linux/init.h> 12 12 #include <linux/ioport.h> 13 13 #include <linux/module.h> 14 - #include <linux/mutex.h> 15 14 #include <linux/of.h> 16 15 #include <linux/platform_device.h> 17 16 #include <linux/pm_runtime.h> ··· 415 416 return 0; 416 417 } 417 418 418 - err = tpu_pwm_config(pwm->chip, pwm, 419 + err = tpu_pwm_config(chip, pwm, 419 420 state->duty_cycle, state->period, enabled); 420 421 if (err) 421 422 return err;
+2 -7
drivers/pwm/pwm-rockchip.c
··· 10 10 #include <linux/io.h> 11 11 #include <linux/module.h> 12 12 #include <linux/of.h> 13 - #include <linux/of_device.h> 14 13 #include <linux/platform_device.h> 14 + #include <linux/property.h> 15 15 #include <linux/pwm.h> 16 16 #include <linux/time.h> 17 17 ··· 296 296 297 297 static int rockchip_pwm_probe(struct platform_device *pdev) 298 298 { 299 - const struct of_device_id *id; 300 299 struct rockchip_pwm_chip *pc; 301 300 u32 enable_conf, ctrl; 302 301 bool enabled; 303 302 int ret, count; 304 - 305 - id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev); 306 - if (!id) 307 - return -EINVAL; 308 303 309 304 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); 310 305 if (!pc) ··· 339 344 340 345 platform_set_drvdata(pdev, pc); 341 346 342 - pc->data = id->data; 347 + pc->data = device_get_match_data(&pdev->dev); 343 348 pc->chip.dev = &pdev->dev; 344 349 pc->chip.ops = &rockchip_pwm_ops; 345 350 pc->chip.npwm = 1;
+2 -4
drivers/pwm/pwm-samsung.c
··· 620 620 clk_disable_unprepare(our_chip->base_clk); 621 621 } 622 622 623 - #ifdef CONFIG_PM_SLEEP 624 623 static int pwm_samsung_resume(struct device *dev) 625 624 { 626 625 struct samsung_pwm_chip *our_chip = dev_get_drvdata(dev); ··· 652 653 653 654 return 0; 654 655 } 655 - #endif 656 656 657 - static SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume); 657 + static DEFINE_SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume); 658 658 659 659 static struct platform_driver pwm_samsung_driver = { 660 660 .driver = { 661 661 .name = "samsung-pwm", 662 - .pm = &pwm_samsung_pm_ops, 662 + .pm = pm_ptr(&pwm_samsung_pm_ops), 663 663 .of_match_table = of_match_ptr(samsung_pwm_matches), 664 664 }, 665 665 .probe = pwm_samsung_probe,
+1 -1
drivers/pwm/pwm-sti.c
··· 407 407 return 0; 408 408 } 409 409 410 - err = sti_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); 410 + err = sti_pwm_config(chip, pwm, state->duty_cycle, state->period); 411 411 if (err) 412 412 return err; 413 413
+5 -5
drivers/pwm/pwm-stm32-lp.c
··· 218 218 return 0; 219 219 } 220 220 221 - static int __maybe_unused stm32_pwm_lp_suspend(struct device *dev) 221 + static int stm32_pwm_lp_suspend(struct device *dev) 222 222 { 223 223 struct stm32_pwm_lp *priv = dev_get_drvdata(dev); 224 224 struct pwm_state state; ··· 233 233 return pinctrl_pm_select_sleep_state(dev); 234 234 } 235 235 236 - static int __maybe_unused stm32_pwm_lp_resume(struct device *dev) 236 + static int stm32_pwm_lp_resume(struct device *dev) 237 237 { 238 238 return pinctrl_pm_select_default_state(dev); 239 239 } 240 240 241 - static SIMPLE_DEV_PM_OPS(stm32_pwm_lp_pm_ops, stm32_pwm_lp_suspend, 242 - stm32_pwm_lp_resume); 241 + static DEFINE_SIMPLE_DEV_PM_OPS(stm32_pwm_lp_pm_ops, stm32_pwm_lp_suspend, 242 + stm32_pwm_lp_resume); 243 243 244 244 static const struct of_device_id stm32_pwm_lp_of_match[] = { 245 245 { .compatible = "st,stm32-pwm-lp", }, ··· 252 252 .driver = { 253 253 .name = "stm32-pwm-lp", 254 254 .of_match_table = stm32_pwm_lp_of_match, 255 - .pm = &stm32_pwm_lp_pm_ops, 255 + .pm = pm_ptr(&stm32_pwm_lp_pm_ops), 256 256 }, 257 257 }; 258 258 module_platform_driver(stm32_pwm_lp_driver);
+65 -41
drivers/pwm/pwm-stm32.c
··· 52 52 return ccer & TIM_CCER_CCXE; 53 53 } 54 54 55 - static int write_ccrx(struct stm32_pwm *dev, int ch, u32 value) 56 - { 57 - switch (ch) { 58 - case 0: 59 - return regmap_write(dev->regmap, TIM_CCR1, value); 60 - case 1: 61 - return regmap_write(dev->regmap, TIM_CCR2, value); 62 - case 2: 63 - return regmap_write(dev->regmap, TIM_CCR3, value); 64 - case 3: 65 - return regmap_write(dev->regmap, TIM_CCR4, value); 66 - } 67 - return -EINVAL; 68 - } 69 - 70 55 #define TIM_CCER_CC12P (TIM_CCER_CC1P | TIM_CCER_CC2P) 71 56 #define TIM_CCER_CC12E (TIM_CCER_CC1E | TIM_CCER_CC2E) 72 57 #define TIM_CCER_CC34P (TIM_CCER_CC3P | TIM_CCER_CC4P) ··· 308 323 return ret; 309 324 } 310 325 311 - static int stm32_pwm_config(struct stm32_pwm *priv, int ch, 326 + static int stm32_pwm_config(struct stm32_pwm *priv, unsigned int ch, 312 327 int duty_ns, int period_ns) 313 328 { 314 329 unsigned long long prd, div, dty; ··· 354 369 dty = prd * duty_ns; 355 370 do_div(dty, period_ns); 356 371 357 - write_ccrx(priv, ch, dty); 372 + regmap_write(priv->regmap, TIM_CCR1 + 4 * ch, dty); 358 373 359 374 /* Configure output mode */ 360 375 shift = (ch & 0x1) * CCMR_CHANNEL_SHIFT; ··· 371 386 return 0; 372 387 } 373 388 374 - static int stm32_pwm_set_polarity(struct stm32_pwm *priv, int ch, 389 + static int stm32_pwm_set_polarity(struct stm32_pwm *priv, unsigned int ch, 375 390 enum pwm_polarity polarity) 376 391 { 377 392 u32 mask; ··· 386 401 return 0; 387 402 } 388 403 389 - static int stm32_pwm_enable(struct stm32_pwm *priv, int ch) 404 + static int stm32_pwm_enable(struct stm32_pwm *priv, unsigned int ch) 390 405 { 391 406 u32 mask; 392 407 int ret; ··· 411 426 return 0; 412 427 } 413 428 414 - static void stm32_pwm_disable(struct stm32_pwm *priv, int ch) 429 + static void stm32_pwm_disable(struct stm32_pwm *priv, unsigned int ch) 415 430 { 416 431 u32 mask; 417 432 ··· 471 486 return ret; 472 487 } 473 488 489 + static int stm32_pwm_get_state(struct pwm_chip *chip, 490 + struct pwm_device *pwm, struct pwm_state *state) 491 + { 492 + struct stm32_pwm *priv = to_stm32_pwm_dev(chip); 493 + int ch = pwm->hwpwm; 494 + unsigned long rate; 495 + u32 ccer, psc, arr, ccr; 496 + u64 dty, prd; 497 + int ret; 498 + 499 + mutex_lock(&priv->lock); 500 + 501 + ret = regmap_read(priv->regmap, TIM_CCER, &ccer); 502 + if (ret) 503 + goto out; 504 + 505 + state->enabled = ccer & (TIM_CCER_CC1E << (ch * 4)); 506 + state->polarity = (ccer & (TIM_CCER_CC1P << (ch * 4))) ? 507 + PWM_POLARITY_INVERSED : PWM_POLARITY_NORMAL; 508 + ret = regmap_read(priv->regmap, TIM_PSC, &psc); 509 + if (ret) 510 + goto out; 511 + ret = regmap_read(priv->regmap, TIM_ARR, &arr); 512 + if (ret) 513 + goto out; 514 + ret = regmap_read(priv->regmap, TIM_CCR1 + 4 * ch, &ccr); 515 + if (ret) 516 + goto out; 517 + 518 + rate = clk_get_rate(priv->clk); 519 + 520 + prd = (u64)NSEC_PER_SEC * (psc + 1) * (arr + 1); 521 + state->period = DIV_ROUND_UP_ULL(prd, rate); 522 + dty = (u64)NSEC_PER_SEC * (psc + 1) * ccr; 523 + state->duty_cycle = DIV_ROUND_UP_ULL(dty, rate); 524 + 525 + out: 526 + mutex_unlock(&priv->lock); 527 + return ret; 528 + } 529 + 474 530 static const struct pwm_ops stm32pwm_ops = { 475 531 .apply = stm32_pwm_apply_locked, 532 + .get_state = stm32_pwm_get_state, 476 533 .capture = IS_ENABLED(CONFIG_DMA_ENGINE) ? stm32_pwm_capture : NULL, 477 534 }; 478 535 ··· 605 578 priv->have_complementary_output = (ccer != 0); 606 579 } 607 580 608 - static int stm32_pwm_detect_channels(struct stm32_pwm *priv) 581 + static unsigned int stm32_pwm_detect_channels(struct stm32_pwm *priv, 582 + unsigned int *num_enabled) 609 583 { 610 - u32 ccer; 611 - int npwm = 0; 584 + u32 ccer, ccer_backup; 612 585 613 586 /* 614 587 * If channels enable bits don't exist writing 1 will have no 615 588 * effect so we can detect and count them. 616 589 */ 590 + regmap_read(priv->regmap, TIM_CCER, &ccer_backup); 617 591 regmap_set_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE); 618 592 regmap_read(priv->regmap, TIM_CCER, &ccer); 619 - regmap_clear_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE); 593 + regmap_write(priv->regmap, TIM_CCER, ccer_backup); 620 594 621 - if (ccer & TIM_CCER_CC1E) 622 - npwm++; 595 + *num_enabled = hweight32(ccer_backup & TIM_CCER_CCXE); 623 596 624 - if (ccer & TIM_CCER_CC2E) 625 - npwm++; 626 - 627 - if (ccer & TIM_CCER_CC3E) 628 - npwm++; 629 - 630 - if (ccer & TIM_CCER_CC4E) 631 - npwm++; 632 - 633 - return npwm; 597 + return hweight32(ccer & TIM_CCER_CCXE); 634 598 } 635 599 636 600 static int stm32_pwm_probe(struct platform_device *pdev) ··· 630 612 struct device_node *np = dev->of_node; 631 613 struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent); 632 614 struct stm32_pwm *priv; 615 + unsigned int num_enabled; 616 + unsigned int i; 633 617 int ret; 634 618 635 619 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); ··· 654 634 655 635 priv->chip.dev = dev; 656 636 priv->chip.ops = &stm32pwm_ops; 657 - priv->chip.npwm = stm32_pwm_detect_channels(priv); 637 + priv->chip.npwm = stm32_pwm_detect_channels(priv, &num_enabled); 638 + 639 + /* Initialize clock refcount to number of enabled PWM channels. */ 640 + for (i = 0; i < num_enabled; i++) 641 + clk_enable(priv->clk); 658 642 659 643 ret = devm_pwmchip_add(dev, &priv->chip); 660 644 if (ret < 0) ··· 669 645 return 0; 670 646 } 671 647 672 - static int __maybe_unused stm32_pwm_suspend(struct device *dev) 648 + static int stm32_pwm_suspend(struct device *dev) 673 649 { 674 650 struct stm32_pwm *priv = dev_get_drvdata(dev); 675 651 unsigned int i; ··· 690 666 return pinctrl_pm_select_sleep_state(dev); 691 667 } 692 668 693 - static int __maybe_unused stm32_pwm_resume(struct device *dev) 669 + static int stm32_pwm_resume(struct device *dev) 694 670 { 695 671 struct stm32_pwm *priv = dev_get_drvdata(dev); 696 672 int ret; ··· 703 679 return stm32_pwm_apply_breakinputs(priv); 704 680 } 705 681 706 - static SIMPLE_DEV_PM_OPS(stm32_pwm_pm_ops, stm32_pwm_suspend, stm32_pwm_resume); 682 + static DEFINE_SIMPLE_DEV_PM_OPS(stm32_pwm_pm_ops, stm32_pwm_suspend, stm32_pwm_resume); 707 683 708 684 static const struct of_device_id stm32_pwm_of_match[] = { 709 685 { .compatible = "st,stm32-pwm", }, ··· 716 692 .driver = { 717 693 .name = "stm32-pwm", 718 694 .of_match_table = stm32_pwm_of_match, 719 - .pm = &stm32_pwm_pm_ops, 695 + .pm = pm_ptr(&stm32_pwm_pm_ops), 720 696 }, 721 697 }; 722 698 module_platform_driver(stm32_pwm_driver);
+7 -7
drivers/pwm/pwm-stmpe.c
··· 44 44 45 45 ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS); 46 46 if (ret < 0) { 47 - dev_err(chip->dev, "error reading PWM#%u control\n", 47 + dev_dbg(chip->dev, "error reading PWM#%u control\n", 48 48 pwm->hwpwm); 49 49 return ret; 50 50 } ··· 53 53 54 54 ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value); 55 55 if (ret) { 56 - dev_err(chip->dev, "error writing PWM#%u control\n", 56 + dev_dbg(chip->dev, "error writing PWM#%u control\n", 57 57 pwm->hwpwm); 58 58 return ret; 59 59 } ··· 70 70 71 71 ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS); 72 72 if (ret < 0) { 73 - dev_err(chip->dev, "error reading PWM#%u control\n", 73 + dev_dbg(chip->dev, "error reading PWM#%u control\n", 74 74 pwm->hwpwm); 75 75 return ret; 76 76 } ··· 79 79 80 80 ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value); 81 81 if (ret) 82 - dev_err(chip->dev, "error writing PWM#%u control\n", 82 + dev_dbg(chip->dev, "error writing PWM#%u control\n", 83 83 pwm->hwpwm); 84 84 return ret; 85 85 } ··· 233 233 234 234 ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value); 235 235 if (ret) { 236 - dev_err(chip->dev, "error writing register %02x: %d\n", 236 + dev_dbg(chip->dev, "error writing register %02x: %d\n", 237 237 offset, ret); 238 238 return ret; 239 239 } ··· 242 242 243 243 ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value); 244 244 if (ret) { 245 - dev_err(chip->dev, "error writing register %02x: %d\n", 245 + dev_dbg(chip->dev, "error writing register %02x: %d\n", 246 246 offset, ret); 247 247 return ret; 248 248 } ··· 275 275 return 0; 276 276 } 277 277 278 - err = stmpe_24xx_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); 278 + err = stmpe_24xx_pwm_config(chip, pwm, state->duty_cycle, state->period); 279 279 if (err) 280 280 return err; 281 281
+1 -1
drivers/pwm/pwm-tegra.c
··· 256 256 return 0; 257 257 } 258 258 259 - err = tegra_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); 259 + err = tegra_pwm_config(chip, pwm, state->duty_cycle, state->period); 260 260 if (err) 261 261 return err; 262 262
+2 -4
drivers/pwm/pwm-tiecap.c
··· 269 269 pm_runtime_disable(&pdev->dev); 270 270 } 271 271 272 - #ifdef CONFIG_PM_SLEEP 273 272 static void ecap_pwm_save_context(struct ecap_pwm_chip *pc) 274 273 { 275 274 pm_runtime_get_sync(pc->chip.dev); ··· 311 312 ecap_pwm_restore_context(pc); 312 313 return 0; 313 314 } 314 - #endif 315 315 316 - static SIMPLE_DEV_PM_OPS(ecap_pwm_pm_ops, ecap_pwm_suspend, ecap_pwm_resume); 316 + static DEFINE_SIMPLE_DEV_PM_OPS(ecap_pwm_pm_ops, ecap_pwm_suspend, ecap_pwm_resume); 317 317 318 318 static struct platform_driver ecap_pwm_driver = { 319 319 .driver = { 320 320 .name = "ecap", 321 321 .of_match_table = ecap_of_match, 322 - .pm = &ecap_pwm_pm_ops, 322 + .pm = pm_ptr(&ecap_pwm_pm_ops), 323 323 }, 324 324 .probe = ecap_pwm_probe, 325 325 .remove_new = ecap_pwm_remove,
+3 -5
drivers/pwm/pwm-tiehrpwm.c
··· 521 521 pm_runtime_disable(&pdev->dev); 522 522 } 523 523 524 - #ifdef CONFIG_PM_SLEEP 525 524 static void ehrpwm_pwm_save_context(struct ehrpwm_pwm_chip *pc) 526 525 { 527 526 pm_runtime_get_sync(pc->chip.dev); ··· 588 589 589 590 return 0; 590 591 } 591 - #endif 592 592 593 - static SIMPLE_DEV_PM_OPS(ehrpwm_pwm_pm_ops, ehrpwm_pwm_suspend, 594 - ehrpwm_pwm_resume); 593 + static DEFINE_SIMPLE_DEV_PM_OPS(ehrpwm_pwm_pm_ops, ehrpwm_pwm_suspend, 594 + ehrpwm_pwm_resume); 595 595 596 596 static struct platform_driver ehrpwm_pwm_driver = { 597 597 .driver = { 598 598 .name = "ehrpwm", 599 599 .of_match_table = ehrpwm_of_match, 600 - .pm = &ehrpwm_pwm_pm_ops, 600 + .pm = pm_ptr(&ehrpwm_pwm_pm_ops), 601 601 }, 602 602 .probe = ehrpwm_pwm_probe, 603 603 .remove_new = ehrpwm_pwm_remove,
+3 -3
drivers/pwm/pwm-twl-led.c
··· 172 172 * We cannot skip calling ->config even if state->period == 173 173 * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle 174 174 * because we might have exited early in the last call to 175 - * pwm_apply_state because of !state->enabled and so the two values in 175 + * pwm_apply_might_sleep because of !state->enabled and so the two values in 176 176 * pwm->state might not be configured in hardware. 177 177 */ 178 - ret = twl4030_pwmled_config(pwm->chip, pwm, 178 + ret = twl4030_pwmled_config(chip, pwm, 179 179 state->duty_cycle, state->period); 180 180 if (ret) 181 181 return ret; ··· 275 275 return 0; 276 276 } 277 277 278 - err = twl6030_pwmled_config(pwm->chip, pwm, 278 + err = twl6030_pwmled_config(chip, pwm, 279 279 state->duty_cycle, state->period); 280 280 if (err) 281 281 return err;
+2 -2
drivers/pwm/pwm-twl.c
··· 294 294 return 0; 295 295 } 296 296 297 - err = twl_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); 297 + err = twl_pwm_config(chip, pwm, state->duty_cycle, state->period); 298 298 if (err) 299 299 return err; 300 300 ··· 319 319 return 0; 320 320 } 321 321 322 - err = twl_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); 322 + err = twl_pwm_config(chip, pwm, state->duty_cycle, state->period); 323 323 if (err) 324 324 return err; 325 325
+2 -2
drivers/pwm/pwm-vt8500.c
··· 206 206 * We cannot skip calling ->config even if state->period == 207 207 * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle 208 208 * because we might have exited early in the last call to 209 - * pwm_apply_state because of !state->enabled and so the two values in 209 + * pwm_apply_might_sleep because of !state->enabled and so the two values in 210 210 * pwm->state might not be configured in hardware. 211 211 */ 212 - err = vt8500_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); 212 + err = vt8500_pwm_config(chip, pwm, state->duty_cycle, state->period); 213 213 if (err) 214 214 return err; 215 215
+6 -6
drivers/pwm/sysfs.c
··· 62 62 mutex_lock(&export->lock); 63 63 pwm_get_state(pwm, &state); 64 64 state.period = val; 65 - ret = pwm_apply_state(pwm, &state); 65 + ret = pwm_apply_might_sleep(pwm, &state); 66 66 mutex_unlock(&export->lock); 67 67 68 68 return ret ? : size; ··· 97 97 mutex_lock(&export->lock); 98 98 pwm_get_state(pwm, &state); 99 99 state.duty_cycle = val; 100 - ret = pwm_apply_state(pwm, &state); 100 + ret = pwm_apply_might_sleep(pwm, &state); 101 101 mutex_unlock(&export->lock); 102 102 103 103 return ret ? : size; ··· 144 144 goto unlock; 145 145 } 146 146 147 - ret = pwm_apply_state(pwm, &state); 147 + ret = pwm_apply_might_sleep(pwm, &state); 148 148 149 149 unlock: 150 150 mutex_unlock(&export->lock); ··· 194 194 mutex_lock(&export->lock); 195 195 pwm_get_state(pwm, &state); 196 196 state.polarity = polarity; 197 - ret = pwm_apply_state(pwm, &state); 197 + ret = pwm_apply_might_sleep(pwm, &state); 198 198 mutex_unlock(&export->lock); 199 199 200 200 return ret ? : size; ··· 401 401 struct pwm_device *pwm, 402 402 struct pwm_state *state) 403 403 { 404 - int ret = pwm_apply_state(pwm, state); 404 + int ret = pwm_apply_might_sleep(pwm, state); 405 405 406 406 /* release lock taken in pwm_class_get_state */ 407 407 mutex_unlock(&export->lock); ··· 510 510 * the kernel it's just not exported. 511 511 */ 512 512 parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip, 513 - "pwmchip%d", chip->base); 513 + "pwmchip%d", chip->id); 514 514 if (IS_ERR(parent)) { 515 515 dev_warn(chip->dev, 516 516 "device_create failed for pwm_chip sysfs export\n");
+2 -2
drivers/regulator/pwm-regulator.c
··· 90 90 pwm_set_relative_duty_cycle(&pstate, 91 91 drvdata->duty_cycle_table[selector].dutycycle, 100); 92 92 93 - ret = pwm_apply_state(drvdata->pwm, &pstate); 93 + ret = pwm_apply_might_sleep(drvdata->pwm, &pstate); 94 94 if (ret) { 95 95 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret); 96 96 return ret; ··· 216 216 217 217 pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit); 218 218 219 - ret = pwm_apply_state(drvdata->pwm, &pstate); 219 + ret = pwm_apply_might_sleep(drvdata->pwm, &pstate); 220 220 if (ret) { 221 221 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret); 222 222 return ret;
+1 -1
drivers/video/backlight/lm3630a_bl.c
··· 180 180 181 181 pchip->pwmd_state.enabled = pchip->pwmd_state.duty_cycle ? true : false; 182 182 183 - return pwm_apply_state(pchip->pwmd, &pchip->pwmd_state); 183 + return pwm_apply_might_sleep(pchip->pwmd, &pchip->pwmd_state); 184 184 } 185 185 186 186 /* update and get brightness */
+1 -1
drivers/video/backlight/lp855x_bl.c
··· 234 234 state.duty_cycle = div_u64(br * state.period, max_br); 235 235 state.enabled = state.duty_cycle; 236 236 237 - return pwm_apply_state(lp->pwm, &state); 237 + return pwm_apply_might_sleep(lp->pwm, &state); 238 238 } 239 239 240 240 static int lp855x_bl_update_status(struct backlight_device *bl)
+6 -6
drivers/video/backlight/pwm_bl.c
··· 103 103 pwm_get_state(pb->pwm, &state); 104 104 state.duty_cycle = compute_duty_cycle(pb, brightness, &state); 105 105 state.enabled = true; 106 - pwm_apply_state(pb->pwm, &state); 106 + pwm_apply_might_sleep(pb->pwm, &state); 107 107 108 108 pwm_backlight_power_on(pb); 109 109 } else { ··· 120 120 * inactive output. 121 121 */ 122 122 state.enabled = !pb->power_supply && !pb->enable_gpio; 123 - pwm_apply_state(pb->pwm, &state); 123 + pwm_apply_might_sleep(pb->pwm, &state); 124 124 } 125 125 126 126 if (pb->notify_after) ··· 528 528 if (!state.period && (data->pwm_period_ns > 0)) 529 529 state.period = data->pwm_period_ns; 530 530 531 - ret = pwm_apply_state(pb->pwm, &state); 531 + ret = pwm_apply_might_sleep(pb->pwm, &state); 532 532 if (ret) { 533 533 dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n", 534 534 ret); ··· 633 633 pwm_get_state(pb->pwm, &state); 634 634 state.duty_cycle = 0; 635 635 state.enabled = false; 636 - pwm_apply_state(pb->pwm, &state); 636 + pwm_apply_might_sleep(pb->pwm, &state); 637 637 638 638 if (pb->exit) 639 639 pb->exit(&pdev->dev); ··· 649 649 pwm_get_state(pb->pwm, &state); 650 650 state.duty_cycle = 0; 651 651 state.enabled = false; 652 - pwm_apply_state(pb->pwm, &state); 652 + pwm_apply_might_sleep(pb->pwm, &state); 653 653 } 654 654 655 655 #ifdef CONFIG_PM_SLEEP ··· 673 673 pwm_get_state(pb->pwm, &state); 674 674 state.duty_cycle = 0; 675 675 state.enabled = false; 676 - pwm_apply_state(pb->pwm, &state); 676 + pwm_apply_might_sleep(pb->pwm, &state); 677 677 678 678 if (pb->notify_after) 679 679 pb->notify_after(pb->dev, 0);
+1 -1
drivers/video/fbdev/ssd1307fb.c
··· 347 347 348 348 pwm_init_state(par->pwm, &pwmstate); 349 349 pwm_set_relative_duty_cycle(&pwmstate, 50, 100); 350 - pwm_apply_state(par->pwm, &pwmstate); 350 + pwm_apply_might_sleep(par->pwm, &pwmstate); 351 351 352 352 /* Enable the PWM */ 353 353 pwm_enable(par->pwm);
+50 -34
include/linux/pwm.h
··· 69 69 * @label: name of the PWM device 70 70 * @flags: flags associated with the PWM device 71 71 * @hwpwm: per-chip relative index of the PWM device 72 - * @pwm: global index of the PWM device 73 72 * @chip: PWM chip providing this PWM device 74 73 * @args: PWM arguments 75 74 * @state: last applied state ··· 78 79 const char *label; 79 80 unsigned long flags; 80 81 unsigned int hwpwm; 81 - unsigned int pwm; 82 82 struct pwm_chip *chip; 83 83 84 84 struct pwm_args args; ··· 91 93 * @state: state to fill with the current PWM state 92 94 * 93 95 * The returned PWM state represents the state that was applied by a previous call to 94 - * pwm_apply_state(). Drivers may have to slightly tweak that state before programming it to 95 - * hardware. If pwm_apply_state() was never called, this returns either the current hardware 96 + * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to 97 + * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware 96 98 * state (if supported) or the default settings. 97 99 */ 98 100 static inline void pwm_get_state(const struct pwm_device *pwm, ··· 110 112 return state.enabled; 111 113 } 112 114 113 - static inline void pwm_set_period(struct pwm_device *pwm, u64 period) 114 - { 115 - if (pwm) 116 - pwm->state.period = period; 117 - } 118 - 119 115 static inline u64 pwm_get_period(const struct pwm_device *pwm) 120 116 { 121 117 struct pwm_state state; ··· 117 125 pwm_get_state(pwm, &state); 118 126 119 127 return state.period; 120 - } 121 - 122 - static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty) 123 - { 124 - if (pwm) 125 - pwm->state.duty_cycle = duty; 126 128 } 127 129 128 130 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) ··· 144 158 } 145 159 146 160 /** 147 - * pwm_init_state() - prepare a new state to be applied with pwm_apply_state() 161 + * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep() 148 162 * @pwm: PWM device 149 163 * @state: state to fill with the prepared PWM state 150 164 * 151 165 * This functions prepares a state that can later be tweaked and applied 152 - * to the PWM device with pwm_apply_state(). This is a convenient function 166 + * to the PWM device with pwm_apply_might_sleep(). This is a convenient function 153 167 * that first retrieves the current PWM state and the replaces the period 154 168 * and polarity fields with the reference values defined in pwm->args. 155 169 * Once the function returns, you can adjust the ->enabled and ->duty_cycle 156 - * fields according to your needs before calling pwm_apply_state(). 170 + * fields according to your needs before calling pwm_apply_might_sleep(). 157 171 * 158 172 * ->duty_cycle is initially set to zero to avoid cases where the current 159 173 * ->duty_cycle value exceed the pwm_args->period one, which would trigger 160 - * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle 174 + * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle 161 175 * first. 162 176 */ 163 177 static inline void pwm_init_state(const struct pwm_device *pwm, ··· 213 227 * 214 228 * pwm_init_state(pwm, &state); 215 229 * pwm_set_relative_duty_cycle(&state, 50, 100); 216 - * pwm_apply_state(pwm, &state); 230 + * pwm_apply_might_sleep(pwm, &state); 217 231 * 218 232 * This functions returns -EINVAL if @duty_cycle and/or @scale are 219 233 * inconsistent (@scale == 0 or @duty_cycle > @scale). ··· 268 282 * @dev: device providing the PWMs 269 283 * @ops: callbacks for this PWM controller 270 284 * @owner: module providing this chip 271 - * @base: number of first PWM controlled by this chip 285 + * @id: unique number of this PWM chip 272 286 * @npwm: number of PWMs controlled by this chip 273 287 * @of_xlate: request a PWM device given a device tree PWM specifier 274 288 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier 275 - * @list: list node for internal use 289 + * @atomic: can the driver's ->apply() be called in atomic context 276 290 * @pwms: array of PWM devices allocated by the framework 277 291 */ 278 292 struct pwm_chip { 279 293 struct device *dev; 280 294 const struct pwm_ops *ops; 281 295 struct module *owner; 282 - int base; 296 + unsigned int id; 283 297 unsigned int npwm; 284 298 285 299 struct pwm_device * (*of_xlate)(struct pwm_chip *chip, 286 300 const struct of_phandle_args *args); 287 301 unsigned int of_pwm_n_cells; 302 + bool atomic; 288 303 289 304 /* only used internally by the PWM framework */ 290 - struct list_head list; 291 305 struct pwm_device *pwms; 292 306 }; 293 307 294 308 #if IS_ENABLED(CONFIG_PWM) 295 309 /* PWM user APIs */ 296 - int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state); 310 + int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state); 311 + int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state); 297 312 int pwm_adjust_config(struct pwm_device *pwm); 298 313 299 314 /** ··· 322 335 323 336 state.duty_cycle = duty_ns; 324 337 state.period = period_ns; 325 - return pwm_apply_state(pwm, &state); 338 + return pwm_apply_might_sleep(pwm, &state); 326 339 } 327 340 328 341 /** ··· 343 356 return 0; 344 357 345 358 state.enabled = true; 346 - return pwm_apply_state(pwm, &state); 359 + return pwm_apply_might_sleep(pwm, &state); 347 360 } 348 361 349 362 /** ··· 362 375 return; 363 376 364 377 state.enabled = false; 365 - pwm_apply_state(pwm, &state); 378 + pwm_apply_might_sleep(pwm, &state); 379 + } 380 + 381 + /** 382 + * pwm_might_sleep() - is pwm_apply_atomic() supported? 383 + * @pwm: PWM device 384 + * 385 + * Returns: false if pwm_apply_atomic() can be called from atomic context. 386 + */ 387 + static inline bool pwm_might_sleep(struct pwm_device *pwm) 388 + { 389 + return !pwm->chip->atomic; 366 390 } 367 391 368 392 /* PWM provider APIs */ ··· 404 406 struct fwnode_handle *fwnode, 405 407 const char *con_id); 406 408 #else 407 - static inline int pwm_apply_state(struct pwm_device *pwm, 408 - const struct pwm_state *state) 409 + static inline bool pwm_might_sleep(struct pwm_device *pwm) 410 + { 411 + return true; 412 + } 413 + 414 + static inline int pwm_apply_might_sleep(struct pwm_device *pwm, 415 + const struct pwm_state *state) 409 416 { 410 417 might_sleep(); 411 - return -ENOTSUPP; 418 + return -EOPNOTSUPP; 419 + } 420 + 421 + static inline int pwm_apply_atomic(struct pwm_device *pwm, 422 + const struct pwm_state *state) 423 + { 424 + return -EOPNOTSUPP; 412 425 } 413 426 414 427 static inline int pwm_adjust_config(struct pwm_device *pwm) 415 428 { 416 - return -ENOTSUPP; 429 + return -EOPNOTSUPP; 417 430 } 418 431 419 432 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, ··· 533 524 state.period = pwm->args.period; 534 525 state.usage_power = false; 535 526 536 - pwm_apply_state(pwm, &state); 527 + pwm_apply_might_sleep(pwm, &state); 528 + } 529 + 530 + /* only for backwards-compatibility, new code should not use this */ 531 + static inline int pwm_apply_state(struct pwm_device *pwm, 532 + const struct pwm_state *state) 533 + { 534 + return pwm_apply_might_sleep(pwm, state); 537 535 } 538 536 539 537 struct pwm_lookup {