cregit-Linux how code gets into the kernel

Release 4.11 drivers/input/misc/pwm-beeper.c

/*
 *  Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
 *  PWM beeper driver
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under  the terms of the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the License, or (at your
 *  option) any later version.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/input.h>
#include <linux/regulator/consumer.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <linux/slab.h>
#include <linux/workqueue.h>


struct pwm_beeper {
	
struct input_dev *input;
	
struct pwm_device *pwm;
	
struct regulator *amplifier;
	
struct work_struct work;
	
unsigned long period;
	
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_state(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; }

Contributors

PersonTokensPropCommitsCommitProp
David Lechner6051.72%133.33%
Dmitry Torokhov3126.72%133.33%
Manfred Schlaegl2521.55%133.33%
Total116100.00%3100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
David Lechner3076.92%150.00%
Manfred Schlaegl923.08%150.00%
Total39100.00%2100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Manfred Schlaegl3055.56%150.00%
David Lechner2444.44%150.00%
Total54100.00%2100.00%


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 ? 1000 : 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; }

Contributors

PersonTokensPropCommitsCommitProp
Lars-Peter Clausen9684.96%133.33%
Manfred Schlaegl108.85%133.33%
Dmitry Torokhov76.19%133.33%
Total113100.00%3100.00%


static void pwm_beeper_stop(struct pwm_beeper *beeper) { cancel_work_sync(&beeper->work); pwm_beeper_off(beeper); }

Contributors

PersonTokensPropCommitsCommitProp
Manfred Schlaegl1666.67%133.33%
Lars-Peter Clausen729.17%133.33%
David Lechner14.17%133.33%
Total24100.00%3100.00%


static void pwm_beeper_close(struct input_dev *input) { struct pwm_beeper *beeper = input_get_drvdata(input); pwm_beeper_stop(beeper); }

Contributors

PersonTokensPropCommitsCommitProp
Manfred Schlaegl2596.15%150.00%
Lars-Peter Clausen13.85%150.00%
Total26100.00%2100.00%


static int pwm_beeper_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; struct pwm_beeper *beeper; struct pwm_state state; int error; beeper = devm_kzalloc(dev, sizeof(*beeper), GFP_KERNEL); if (!beeper) return -ENOMEM; beeper->pwm = devm_pwm_get(dev, NULL); if (IS_ERR(beeper->pwm)) { error = PTR_ERR(beeper->pwm); if (error != -EPROBE_DEFER) dev_err(dev, "Failed to request PWM device: %d\n", error); return error; } /* Sync up PWM state and ensure it is off. */ pwm_init_state(beeper->pwm, &state); state.enabled = false; error = pwm_apply_state(beeper->pwm, &state); if (error) { dev_err(dev, "failed to apply initial PWM state: %d\n", error); return error; } beeper->amplifier = devm_regulator_get(dev, "amp"); if (IS_ERR(beeper->amplifier)) { error = PTR_ERR(beeper->amplifier); if (error != -EPROBE_DEFER) dev_err(dev, "Failed to get 'amp' regulator: %d\n", error); return error; } INIT_WORK(&beeper->work, pwm_beeper_work); beeper->input = devm_input_allocate_device(dev); if (!beeper->input) { dev_err(dev, "Failed to allocate input device\n"); return -ENOMEM; } beeper->input->name = "pwm-beeper"; beeper->input->phys = "pwm/input0"; beeper->input->id.bustype = BUS_HOST; beeper->input->id.vendor = 0x001f; beeper->input->id.product = 0x0001; beeper->input->id.version = 0x0100; input_set_capability(beeper->input, EV_SND, SND_TONE); input_set_capability(beeper->input, EV_SND, SND_BELL); beeper->input->event = pwm_beeper_event; beeper->input->close = pwm_beeper_close; input_set_drvdata(beeper->input, beeper); error = input_register_device(beeper->input); if (error) { dev_err(dev, "Failed to register input device: %d\n", error); return error; } platform_set_drvdata(pdev, beeper); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Lars-Peter Clausen20553.52%111.11%
Dmitry Torokhov8121.15%333.33%
David Lechner5714.88%222.22%
Manfred Schlaegl184.70%111.11%
Sascha Hauer164.18%111.11%
Boris Brezillon61.57%111.11%
Total383100.00%9100.00%


static int __maybe_unused pwm_beeper_suspend(struct device *dev) { struct pwm_beeper *beeper = dev_get_drvdata(dev); /* * Spinlock is taken here is not to protect write to * beeper->suspended, but to ensure that pwm_beeper_event * does not re-submit work once flag is set. */ spin_lock_irq(&beeper->input->event_lock); beeper->suspended = true; spin_unlock_irq(&beeper->input->event_lock); pwm_beeper_stop(beeper); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Lars-Peter Clausen2849.12%125.00%
Dmitry Torokhov2747.37%125.00%
Jingoo Han11.75%125.00%
Manfred Schlaegl11.75%125.00%
Total57100.00%4100.00%


static int __maybe_unused pwm_beeper_resume(struct device *dev) { struct pwm_beeper *beeper = dev_get_drvdata(dev); spin_lock_irq(&beeper->input->event_lock); beeper->suspended = false; spin_unlock_irq(&beeper->input->event_lock); /* Let worker figure out if we should resume beeping */ schedule_work(&beeper->work); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Lars-Peter Clausen3050.00%133.33%
Dmitry Torokhov2948.33%133.33%
Jingoo Han11.67%133.33%
Total60100.00%3100.00%

static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops, pwm_beeper_suspend, pwm_beeper_resume); #ifdef CONFIG_OF static const struct of_device_id pwm_beeper_match[] = { { .compatible = "pwm-beeper", }, { }, }; MODULE_DEVICE_TABLE(of, pwm_beeper_match); #endif static struct platform_driver pwm_beeper_driver = { .probe = pwm_beeper_probe, .driver = { .name = "pwm-beeper", .pm = &pwm_beeper_pm_ops, .of_match_table = of_match_ptr(pwm_beeper_match), }, }; module_platform_driver(pwm_beeper_driver); MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); MODULE_DESCRIPTION("PWM beeper driver"); MODULE_LICENSE("GPL"); MODULE_ALIAS("platform:pwm-beeper");

Overall Contributors

PersonTokensPropCommitsCommitProp
Lars-Peter Clausen47245.30%16.67%
David Lechner18317.56%213.33%
Dmitry Torokhov18017.27%533.33%
Manfred Schlaegl14113.53%16.67%
Sascha Hauer454.32%16.67%
Luis de Bethencourt70.67%16.67%
Boris Brezillon60.58%16.67%
Jingoo Han40.38%16.67%
Sachin Kamat30.29%16.67%
JJ Ding10.10%16.67%
Total1042100.00%15100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.