Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 1 | /** |
| 2 | * wm831x-on.c - WM831X ON pin driver |
| 3 | * |
| 4 | * Copyright (C) 2009 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General |
| 7 | * Public License. See the file "COPYING" in the main directory of this |
| 8 | * archive for more details. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 23 | #include <linux/kernel.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/input.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/workqueue.h> |
| 29 | #include <linux/mfd/wm831x/core.h> |
| 30 | |
| 31 | struct wm831x_on { |
| 32 | struct input_dev *dev; |
| 33 | struct delayed_work work; |
| 34 | struct wm831x *wm831x; |
| 35 | }; |
| 36 | |
| 37 | /* |
| 38 | * The chip gives us an interrupt when the ON pin is asserted but we |
| 39 | * then need to poll to see when the pin is deasserted. |
| 40 | */ |
| 41 | static void wm831x_poll_on(struct work_struct *work) |
| 42 | { |
| 43 | struct wm831x_on *wm831x_on = container_of(work, struct wm831x_on, |
| 44 | work.work); |
| 45 | struct wm831x *wm831x = wm831x_on->wm831x; |
| 46 | int poll, ret; |
| 47 | |
| 48 | ret = wm831x_reg_read(wm831x, WM831X_ON_PIN_CONTROL); |
| 49 | if (ret >= 0) { |
| 50 | poll = !(ret & WM831X_ON_PIN_STS); |
| 51 | |
| 52 | input_report_key(wm831x_on->dev, KEY_POWER, poll); |
| 53 | input_sync(wm831x_on->dev); |
| 54 | } else { |
| 55 | dev_err(wm831x->dev, "Failed to read ON status: %d\n", ret); |
| 56 | poll = 1; |
| 57 | } |
| 58 | |
| 59 | if (poll) |
| 60 | schedule_delayed_work(&wm831x_on->work, 100); |
| 61 | } |
| 62 | |
| 63 | static irqreturn_t wm831x_on_irq(int irq, void *data) |
| 64 | { |
| 65 | struct wm831x_on *wm831x_on = data; |
| 66 | |
| 67 | schedule_delayed_work(&wm831x_on->work, 0); |
| 68 | |
| 69 | return IRQ_HANDLED; |
| 70 | } |
| 71 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 72 | static int wm831x_on_probe(struct platform_device *pdev) |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 73 | { |
| 74 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
| 75 | struct wm831x_on *wm831x_on; |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 76 | int irq = wm831x_irq(wm831x, platform_get_irq(pdev, 0)); |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 77 | int ret; |
| 78 | |
Mark Brown | dae6ba4 | 2012-10-11 00:42:34 -0700 | [diff] [blame] | 79 | wm831x_on = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_on), |
| 80 | GFP_KERNEL); |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 81 | if (!wm831x_on) { |
| 82 | dev_err(&pdev->dev, "Can't allocate data\n"); |
| 83 | return -ENOMEM; |
| 84 | } |
| 85 | |
| 86 | wm831x_on->wm831x = wm831x; |
| 87 | INIT_DELAYED_WORK(&wm831x_on->work, wm831x_poll_on); |
| 88 | |
Mark Brown | 5d3caa4 | 2012-12-24 09:45:22 -0800 | [diff] [blame] | 89 | wm831x_on->dev = devm_input_allocate_device(&pdev->dev); |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 90 | if (!wm831x_on->dev) { |
| 91 | dev_err(&pdev->dev, "Can't allocate input dev\n"); |
| 92 | ret = -ENOMEM; |
| 93 | goto err; |
| 94 | } |
| 95 | |
| 96 | wm831x_on->dev->evbit[0] = BIT_MASK(EV_KEY); |
| 97 | wm831x_on->dev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER); |
| 98 | wm831x_on->dev->name = "wm831x_on"; |
| 99 | wm831x_on->dev->phys = "wm831x_on/input0"; |
| 100 | wm831x_on->dev->dev.parent = &pdev->dev; |
| 101 | |
Mark Brown | afadb8e | 2010-03-10 23:41:33 -0800 | [diff] [blame] | 102 | ret = request_threaded_irq(irq, NULL, wm831x_on_irq, |
| 103 | IRQF_TRIGGER_RISING, "wm831x_on", |
| 104 | wm831x_on); |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 105 | if (ret < 0) { |
| 106 | dev_err(&pdev->dev, "Unable to request IRQ: %d\n", ret); |
| 107 | goto err_input_dev; |
| 108 | } |
| 109 | ret = input_register_device(wm831x_on->dev); |
| 110 | if (ret) { |
| 111 | dev_dbg(&pdev->dev, "Can't register input device: %d\n", ret); |
| 112 | goto err_irq; |
| 113 | } |
| 114 | |
| 115 | platform_set_drvdata(pdev, wm831x_on); |
| 116 | |
| 117 | return 0; |
| 118 | |
| 119 | err_irq: |
Mark Brown | afadb8e | 2010-03-10 23:41:33 -0800 | [diff] [blame] | 120 | free_irq(irq, wm831x_on); |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 121 | err_input_dev: |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 122 | err: |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 123 | return ret; |
| 124 | } |
| 125 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 126 | static int wm831x_on_remove(struct platform_device *pdev) |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 127 | { |
| 128 | struct wm831x_on *wm831x_on = platform_get_drvdata(pdev); |
| 129 | int irq = platform_get_irq(pdev, 0); |
| 130 | |
Mark Brown | afadb8e | 2010-03-10 23:41:33 -0800 | [diff] [blame] | 131 | free_irq(irq, wm831x_on); |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 132 | cancel_delayed_work_sync(&wm831x_on->work); |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static struct platform_driver wm831x_on_driver = { |
| 138 | .probe = wm831x_on_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 139 | .remove = wm831x_on_remove, |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 140 | .driver = { |
| 141 | .name = "wm831x-on", |
| 142 | .owner = THIS_MODULE, |
| 143 | }, |
| 144 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 145 | module_platform_driver(wm831x_on_driver); |
Mark Brown | 0c73b99 | 2009-09-15 12:07:12 +0200 | [diff] [blame] | 146 | |
| 147 | MODULE_ALIAS("platform:wm831x-on"); |
| 148 | MODULE_DESCRIPTION("WM831x ON pin"); |
| 149 | MODULE_LICENSE("GPL"); |
| 150 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 151 | |