Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ON pin driver for Dialog DA9052 PMICs |
| 3 | * |
| 4 | * Copyright(c) 2012 Dialog Semiconductor Ltd. |
| 5 | * |
| 6 | * Author: David Dajun Chen <dchen@diasemi.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/input.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/workqueue.h> |
| 19 | |
| 20 | #include <linux/mfd/da9052/da9052.h> |
| 21 | #include <linux/mfd/da9052/reg.h> |
| 22 | |
| 23 | struct da9052_onkey { |
| 24 | struct da9052 *da9052; |
| 25 | struct input_dev *input; |
| 26 | struct delayed_work work; |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | static void da9052_onkey_query(struct da9052_onkey *onkey) |
| 30 | { |
| 31 | int key_stat; |
| 32 | |
| 33 | key_stat = da9052_reg_read(onkey->da9052, DA9052_EVENT_B_REG); |
| 34 | if (key_stat < 0) { |
| 35 | dev_err(onkey->da9052->dev, |
| 36 | "Failed to read onkey event %d\n", key_stat); |
| 37 | } else { |
| 38 | /* |
| 39 | * Since interrupt for deassertion of ONKEY pin is not |
| 40 | * generated, onkey event state determines the onkey |
| 41 | * button state. |
| 42 | */ |
| 43 | key_stat &= DA9052_EVENTB_ENONKEY; |
| 44 | input_report_key(onkey->input, KEY_POWER, key_stat); |
| 45 | input_sync(onkey->input); |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * Interrupt is generated only when the ONKEY pin is asserted. |
| 50 | * Hence the deassertion of the pin is simulated through work queue. |
| 51 | */ |
| 52 | if (key_stat) |
| 53 | schedule_delayed_work(&onkey->work, msecs_to_jiffies(50)); |
| 54 | } |
| 55 | |
| 56 | static void da9052_onkey_work(struct work_struct *work) |
| 57 | { |
| 58 | struct da9052_onkey *onkey = container_of(work, struct da9052_onkey, |
| 59 | work.work); |
| 60 | |
| 61 | da9052_onkey_query(onkey); |
| 62 | } |
| 63 | |
| 64 | static irqreturn_t da9052_onkey_irq(int irq, void *data) |
| 65 | { |
| 66 | struct da9052_onkey *onkey = data; |
| 67 | |
| 68 | da9052_onkey_query(onkey); |
| 69 | |
| 70 | return IRQ_HANDLED; |
| 71 | } |
| 72 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 73 | static int da9052_onkey_probe(struct platform_device *pdev) |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 74 | { |
| 75 | struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent); |
| 76 | struct da9052_onkey *onkey; |
| 77 | struct input_dev *input_dev; |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 78 | int error; |
| 79 | |
| 80 | if (!da9052) { |
| 81 | dev_err(&pdev->dev, "Failed to get the driver's data\n"); |
| 82 | return -EINVAL; |
| 83 | } |
| 84 | |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 85 | onkey = kzalloc(sizeof(*onkey), GFP_KERNEL); |
| 86 | input_dev = input_allocate_device(); |
| 87 | if (!onkey || !input_dev) { |
| 88 | dev_err(&pdev->dev, "Failed to allocate memory\n"); |
Jesper Juhl | 0e3d0f3 | 2012-04-11 20:55:18 -0700 | [diff] [blame] | 89 | error = -ENOMEM; |
| 90 | goto err_free_mem; |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | onkey->input = input_dev; |
| 94 | onkey->da9052 = da9052; |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 95 | INIT_DELAYED_WORK(&onkey->work, da9052_onkey_work); |
| 96 | |
| 97 | input_dev->name = "da9052-onkey"; |
| 98 | input_dev->phys = "da9052-onkey/input0"; |
| 99 | input_dev->dev.parent = &pdev->dev; |
| 100 | |
| 101 | input_dev->evbit[0] = BIT_MASK(EV_KEY); |
| 102 | __set_bit(KEY_POWER, input_dev->keybit); |
| 103 | |
Fabio Estevam | 21eed07 | 2012-10-04 00:15:07 -0300 | [diff] [blame] | 104 | error = da9052_request_irq(onkey->da9052, DA9052_IRQ_NONKEY, "ONKEY", |
| 105 | da9052_onkey_irq, onkey); |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 106 | if (error < 0) { |
| 107 | dev_err(onkey->da9052->dev, |
Fabio Estevam | 21eed07 | 2012-10-04 00:15:07 -0300 | [diff] [blame] | 108 | "Failed to register ONKEY IRQ: %d\n", error); |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 109 | goto err_free_mem; |
| 110 | } |
| 111 | |
| 112 | error = input_register_device(onkey->input); |
| 113 | if (error) { |
| 114 | dev_err(&pdev->dev, "Unable to register input device, %d\n", |
| 115 | error); |
| 116 | goto err_free_irq; |
| 117 | } |
| 118 | |
| 119 | platform_set_drvdata(pdev, onkey); |
| 120 | return 0; |
| 121 | |
| 122 | err_free_irq: |
Fabio Estevam | 21eed07 | 2012-10-04 00:15:07 -0300 | [diff] [blame] | 123 | da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey); |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 124 | cancel_delayed_work_sync(&onkey->work); |
| 125 | err_free_mem: |
| 126 | input_free_device(input_dev); |
| 127 | kfree(onkey); |
| 128 | |
| 129 | return error; |
| 130 | } |
| 131 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 132 | static int da9052_onkey_remove(struct platform_device *pdev) |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 133 | { |
| 134 | struct da9052_onkey *onkey = platform_get_drvdata(pdev); |
| 135 | |
Fabio Estevam | 21eed07 | 2012-10-04 00:15:07 -0300 | [diff] [blame] | 136 | da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey); |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 137 | cancel_delayed_work_sync(&onkey->work); |
| 138 | |
| 139 | input_unregister_device(onkey->input); |
| 140 | kfree(onkey); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static struct platform_driver da9052_onkey_driver = { |
| 146 | .probe = da9052_onkey_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 147 | .remove = da9052_onkey_remove, |
Ashish Jangam | f0c5f65 | 2012-03-04 08:40:58 -0800 | [diff] [blame] | 148 | .driver = { |
| 149 | .name = "da9052-onkey", |
| 150 | .owner = THIS_MODULE, |
| 151 | }, |
| 152 | }; |
| 153 | module_platform_driver(da9052_onkey_driver); |
| 154 | |
| 155 | MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); |
| 156 | MODULE_DESCRIPTION("Onkey driver for DA9052"); |
| 157 | MODULE_LICENSE("GPL"); |
| 158 | MODULE_ALIAS("platform:da9052-onkey"); |