Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de> |
| 3 | * PWM beeper driver |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by the |
| 7 | * Free Software Foundation; either version 2 of the License, or (at your |
| 8 | * option) any later version. |
| 9 | * |
| 10 | * You should have received a copy of the GNU General Public License along |
| 11 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 12 | * 675 Mass Ave, Cambridge, MA 02139, USA. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/input.h> |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 17 | #include <linux/regulator/consumer.h> |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/kernel.h> |
Sachin Kamat | 9dbe4c3 | 2013-10-06 00:56:09 -0700 | [diff] [blame] | 20 | #include <linux/of.h> |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/pwm.h> |
| 23 | #include <linux/slab.h> |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 24 | #include <linux/workqueue.h> |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 25 | |
| 26 | struct pwm_beeper { |
| 27 | struct input_dev *input; |
| 28 | struct pwm_device *pwm; |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 29 | struct regulator *amplifier; |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 30 | struct work_struct work; |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 31 | unsigned long period; |
Dmitry Torokhov | e9728f0 | 2017-01-19 11:13:37 -0800 | [diff] [blame] | 32 | bool suspended; |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 33 | bool amplifier_on; |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x)) |
| 37 | |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 38 | static int pwm_beeper_on(struct pwm_beeper *beeper, unsigned long period) |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 39 | { |
Dmitry Torokhov | 2de8b41 | 2017-01-19 13:52:49 -0800 | [diff] [blame^] | 40 | struct pwm_state state; |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 41 | int error; |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 42 | |
Dmitry Torokhov | 2de8b41 | 2017-01-19 13:52:49 -0800 | [diff] [blame^] | 43 | pwm_get_state(beeper->pwm, &state); |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 44 | |
Dmitry Torokhov | 2de8b41 | 2017-01-19 13:52:49 -0800 | [diff] [blame^] | 45 | state.enabled = true; |
| 46 | state.period = period; |
| 47 | pwm_set_relative_duty_cycle(&state, 50, 100); |
| 48 | |
| 49 | error = pwm_apply_state(beeper->pwm, &state); |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 50 | if (error) |
| 51 | return error; |
| 52 | |
| 53 | if (!beeper->amplifier_on) { |
| 54 | error = regulator_enable(beeper->amplifier); |
| 55 | if (error) { |
| 56 | pwm_disable(beeper->pwm); |
| 57 | return error; |
| 58 | } |
| 59 | |
| 60 | beeper->amplifier_on = true; |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static void pwm_beeper_off(struct pwm_beeper *beeper) |
| 67 | { |
| 68 | if (beeper->amplifier_on) { |
| 69 | regulator_disable(beeper->amplifier); |
| 70 | beeper->amplifier_on = false; |
| 71 | } |
| 72 | |
| 73 | pwm_disable(beeper->pwm); |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void pwm_beeper_work(struct work_struct *work) |
| 77 | { |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 78 | struct pwm_beeper *beeper = container_of(work, struct pwm_beeper, work); |
| 79 | unsigned long period = READ_ONCE(beeper->period); |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 80 | |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 81 | if (period) |
| 82 | pwm_beeper_on(beeper, period); |
| 83 | else |
| 84 | pwm_beeper_off(beeper); |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 87 | static int pwm_beeper_event(struct input_dev *input, |
| 88 | unsigned int type, unsigned int code, int value) |
| 89 | { |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 90 | struct pwm_beeper *beeper = input_get_drvdata(input); |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 91 | |
| 92 | if (type != EV_SND || value < 0) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | switch (code) { |
| 96 | case SND_BELL: |
| 97 | value = value ? 1000 : 0; |
| 98 | break; |
| 99 | case SND_TONE: |
| 100 | break; |
| 101 | default: |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 105 | if (value == 0) |
| 106 | beeper->period = 0; |
| 107 | else |
| 108 | beeper->period = HZ_TO_NANOSECONDS(value); |
| 109 | |
Dmitry Torokhov | e9728f0 | 2017-01-19 11:13:37 -0800 | [diff] [blame] | 110 | if (!beeper->suspended) |
| 111 | schedule_work(&beeper->work); |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 116 | static void pwm_beeper_stop(struct pwm_beeper *beeper) |
| 117 | { |
| 118 | cancel_work_sync(&beeper->work); |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 119 | pwm_beeper_off(beeper); |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void pwm_beeper_close(struct input_dev *input) |
| 123 | { |
| 124 | struct pwm_beeper *beeper = input_get_drvdata(input); |
| 125 | |
| 126 | pwm_beeper_stop(beeper); |
| 127 | } |
| 128 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 129 | static int pwm_beeper_probe(struct platform_device *pdev) |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 130 | { |
Dmitry Torokhov | bcf4b04 | 2017-01-06 09:50:14 -0800 | [diff] [blame] | 131 | struct device *dev = &pdev->dev; |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 132 | struct pwm_beeper *beeper; |
Dmitry Torokhov | 2de8b41 | 2017-01-19 13:52:49 -0800 | [diff] [blame^] | 133 | struct pwm_state state; |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 134 | int error; |
| 135 | |
Dmitry Torokhov | bcf4b04 | 2017-01-06 09:50:14 -0800 | [diff] [blame] | 136 | beeper = devm_kzalloc(dev, sizeof(*beeper), GFP_KERNEL); |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 137 | if (!beeper) |
| 138 | return -ENOMEM; |
| 139 | |
Dmitry Torokhov | bcf4b04 | 2017-01-06 09:50:14 -0800 | [diff] [blame] | 140 | beeper->pwm = devm_pwm_get(dev, NULL); |
Sascha Hauer | d56a289 | 2012-11-03 12:05:52 -0700 | [diff] [blame] | 141 | if (IS_ERR(beeper->pwm)) { |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 142 | error = PTR_ERR(beeper->pwm); |
David Lechner | 6248188 | 2017-01-06 10:35:16 -0800 | [diff] [blame] | 143 | if (error != -EPROBE_DEFER) |
| 144 | dev_err(dev, "Failed to request PWM device: %d\n", |
| 145 | error); |
Dmitry Torokhov | bcf4b04 | 2017-01-06 09:50:14 -0800 | [diff] [blame] | 146 | return error; |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Dmitry Torokhov | 2de8b41 | 2017-01-19 13:52:49 -0800 | [diff] [blame^] | 149 | /* Sync up PWM state and ensure it is off. */ |
| 150 | pwm_init_state(beeper->pwm, &state); |
| 151 | state.enabled = false; |
| 152 | error = pwm_apply_state(beeper->pwm, &state); |
| 153 | if (error) { |
| 154 | dev_err(dev, "failed to apply initial PWM state: %d\n", |
| 155 | error); |
| 156 | return error; |
| 157 | } |
Boris Brezillon | cfae56f | 2016-04-14 21:17:34 +0200 | [diff] [blame] | 158 | |
David Lechner | 9e54924 | 2017-01-15 17:09:43 -0800 | [diff] [blame] | 159 | beeper->amplifier = devm_regulator_get(dev, "amp"); |
| 160 | if (IS_ERR(beeper->amplifier)) { |
| 161 | error = PTR_ERR(beeper->amplifier); |
| 162 | if (error != -EPROBE_DEFER) |
| 163 | dev_err(dev, "Failed to get 'amp' regulator: %d\n", |
| 164 | error); |
| 165 | return error; |
| 166 | } |
| 167 | |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 168 | INIT_WORK(&beeper->work, pwm_beeper_work); |
| 169 | |
Dmitry Torokhov | bcf4b04 | 2017-01-06 09:50:14 -0800 | [diff] [blame] | 170 | beeper->input = devm_input_allocate_device(dev); |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 171 | if (!beeper->input) { |
Dmitry Torokhov | bcf4b04 | 2017-01-06 09:50:14 -0800 | [diff] [blame] | 172 | dev_err(dev, "Failed to allocate input device\n"); |
| 173 | return -ENOMEM; |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 174 | } |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 175 | |
| 176 | beeper->input->name = "pwm-beeper"; |
| 177 | beeper->input->phys = "pwm/input0"; |
| 178 | beeper->input->id.bustype = BUS_HOST; |
| 179 | beeper->input->id.vendor = 0x001f; |
| 180 | beeper->input->id.product = 0x0001; |
| 181 | beeper->input->id.version = 0x0100; |
| 182 | |
Dmitry Torokhov | 48a55d7 | 2017-01-06 09:54:14 -0800 | [diff] [blame] | 183 | input_set_capability(beeper->input, EV_SND, SND_TONE); |
| 184 | input_set_capability(beeper->input, EV_SND, SND_BELL); |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 185 | |
| 186 | beeper->input->event = pwm_beeper_event; |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 187 | beeper->input->close = pwm_beeper_close; |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 188 | |
| 189 | input_set_drvdata(beeper->input, beeper); |
| 190 | |
| 191 | error = input_register_device(beeper->input); |
| 192 | if (error) { |
Dmitry Torokhov | bcf4b04 | 2017-01-06 09:50:14 -0800 | [diff] [blame] | 193 | dev_err(dev, "Failed to register input device: %d\n", error); |
| 194 | return error; |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | platform_set_drvdata(pdev, beeper); |
| 198 | |
| 199 | return 0; |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Jingoo Han | 97a652a | 2014-11-02 00:02:46 -0700 | [diff] [blame] | 202 | static int __maybe_unused pwm_beeper_suspend(struct device *dev) |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 203 | { |
| 204 | struct pwm_beeper *beeper = dev_get_drvdata(dev); |
| 205 | |
Dmitry Torokhov | e9728f0 | 2017-01-19 11:13:37 -0800 | [diff] [blame] | 206 | /* |
| 207 | * Spinlock is taken here is not to protect write to |
| 208 | * beeper->suspended, but to ensure that pwm_beeper_event |
| 209 | * does not re-submit work once flag is set. |
| 210 | */ |
| 211 | spin_lock_irq(&beeper->input->event_lock); |
| 212 | beeper->suspended = true; |
| 213 | spin_unlock_irq(&beeper->input->event_lock); |
| 214 | |
Manfred Schlaegl | f49cf3b | 2016-05-27 16:36:36 -0700 | [diff] [blame] | 215 | pwm_beeper_stop(beeper); |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Jingoo Han | 97a652a | 2014-11-02 00:02:46 -0700 | [diff] [blame] | 220 | static int __maybe_unused pwm_beeper_resume(struct device *dev) |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 221 | { |
| 222 | struct pwm_beeper *beeper = dev_get_drvdata(dev); |
| 223 | |
Dmitry Torokhov | e9728f0 | 2017-01-19 11:13:37 -0800 | [diff] [blame] | 224 | spin_lock_irq(&beeper->input->event_lock); |
| 225 | beeper->suspended = false; |
| 226 | spin_unlock_irq(&beeper->input->event_lock); |
| 227 | |
| 228 | /* Let worker figure out if we should resume beeping */ |
| 229 | schedule_work(&beeper->work); |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops, |
| 235 | pwm_beeper_suspend, pwm_beeper_resume); |
| 236 | |
Sascha Hauer | d56a289 | 2012-11-03 12:05:52 -0700 | [diff] [blame] | 237 | #ifdef CONFIG_OF |
| 238 | static const struct of_device_id pwm_beeper_match[] = { |
| 239 | { .compatible = "pwm-beeper", }, |
| 240 | { }, |
| 241 | }; |
Luis de Bethencourt | 544edf56 | 2015-09-03 10:49:46 -0700 | [diff] [blame] | 242 | MODULE_DEVICE_TABLE(of, pwm_beeper_match); |
Sascha Hauer | d56a289 | 2012-11-03 12:05:52 -0700 | [diff] [blame] | 243 | #endif |
| 244 | |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 245 | static struct platform_driver pwm_beeper_driver = { |
| 246 | .probe = pwm_beeper_probe, |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 247 | .driver = { |
| 248 | .name = "pwm-beeper", |
Dmitry Torokhov | ceb5b6c | 2015-02-19 16:22:31 -0800 | [diff] [blame] | 249 | .pm = &pwm_beeper_pm_ops, |
Sascha Hauer | d56a289 | 2012-11-03 12:05:52 -0700 | [diff] [blame] | 250 | .of_match_table = of_match_ptr(pwm_beeper_match), |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 251 | }, |
| 252 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 253 | module_platform_driver(pwm_beeper_driver); |
Lars-Peter Clausen | e22739d | 2010-07-14 00:25:21 -0700 | [diff] [blame] | 254 | |
| 255 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 256 | MODULE_DESCRIPTION("PWM beeper driver"); |
| 257 | MODULE_LICENSE("GPL"); |
| 258 | MODULE_ALIAS("platform:pwm-beeper"); |