drivers/input/misc/pwm-beeper.c
Source file repositories/reference/linux-study-clean/drivers/input/misc/pwm-beeper.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/input/misc/pwm-beeper.c- Extension
.c- Size
- 5816 bytes
- Lines
- 254
- Domain
- Driver Families
- Bucket
- drivers/input
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/input.hlinux/regulator/consumer.hlinux/module.hlinux/kernel.hlinux/of.hlinux/platform_device.hlinux/property.hlinux/pwm.hlinux/slab.hlinux/workqueue.h
Detected Declarations
struct pwm_beeperfunction pwm_beeper_onfunction pwm_beeper_offfunction pwm_beeper_workfunction pwm_beeper_eventfunction pwm_beeper_stopfunction pwm_beeper_closefunction pwm_beeper_probefunction pwm_beeper_suspendfunction pwm_beeper_resumefunction scoped_guard
Annotated Snippet
struct pwm_beeper {
struct input_dev *input;
struct pwm_device *pwm;
struct regulator *amplifier;
struct work_struct work;
unsigned long period;
unsigned int bell_frequency;
bool suspended;
bool amplifier_on;
};
#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
static int pwm_beeper_on(struct pwm_beeper *beeper, unsigned long period)
{
struct pwm_state state;
int error;
pwm_get_state(beeper->pwm, &state);
state.enabled = true;
state.period = period;
pwm_set_relative_duty_cycle(&state, 50, 100);
error = pwm_apply_might_sleep(beeper->pwm, &state);
if (error)
return error;
if (!beeper->amplifier_on) {
error = regulator_enable(beeper->amplifier);
if (error) {
pwm_disable(beeper->pwm);
return error;
}
beeper->amplifier_on = true;
}
return 0;
}
static void pwm_beeper_off(struct pwm_beeper *beeper)
{
if (beeper->amplifier_on) {
regulator_disable(beeper->amplifier);
beeper->amplifier_on = false;
}
pwm_disable(beeper->pwm);
}
static void pwm_beeper_work(struct work_struct *work)
{
struct pwm_beeper *beeper = container_of(work, struct pwm_beeper, work);
unsigned long period = READ_ONCE(beeper->period);
if (period)
pwm_beeper_on(beeper, period);
else
pwm_beeper_off(beeper);
}
static int pwm_beeper_event(struct input_dev *input,
unsigned int type, unsigned int code, int value)
{
struct pwm_beeper *beeper = input_get_drvdata(input);
if (type != EV_SND || value < 0)
return -EINVAL;
switch (code) {
case SND_BELL:
value = value ? beeper->bell_frequency : 0;
break;
case SND_TONE:
break;
default:
return -EINVAL;
}
if (value == 0)
beeper->period = 0;
else
beeper->period = HZ_TO_NANOSECONDS(value);
if (!beeper->suspended)
schedule_work(&beeper->work);
return 0;
}
Annotation
- Immediate include surface: `linux/input.h`, `linux/regulator/consumer.h`, `linux/module.h`, `linux/kernel.h`, `linux/of.h`, `linux/platform_device.h`, `linux/property.h`, `linux/pwm.h`.
- Detected declarations: `struct pwm_beeper`, `function pwm_beeper_on`, `function pwm_beeper_off`, `function pwm_beeper_work`, `function pwm_beeper_event`, `function pwm_beeper_stop`, `function pwm_beeper_close`, `function pwm_beeper_probe`, `function pwm_beeper_suspend`, `function pwm_beeper_resume`.
- Atlas domain: Driver Families / drivers/input.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.