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