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; 27 unsigned int irq; 28 }; 29 30 static void da9052_onkey_query(struct da9052_onkey *onkey) 31 { 32 int key_stat; 33 34 key_stat = da9052_reg_read(onkey->da9052, DA9052_EVENT_B_REG); 35 if (key_stat < 0) { 36 dev_err(onkey->da9052->dev, 37 "Failed to read onkey event %d\n", key_stat); 38 } else { 39 /* 40 * Since interrupt for deassertion of ONKEY pin is not 41 * generated, onkey event state determines the onkey 42 * button state. 43 */ 44 key_stat &= DA9052_EVENTB_ENONKEY; 45 input_report_key(onkey->input, KEY_POWER, key_stat); 46 input_sync(onkey->input); 47 } 48 49 /* 50 * Interrupt is generated only when the ONKEY pin is asserted. 51 * Hence the deassertion of the pin is simulated through work queue. 52 */ 53 if (key_stat) 54 schedule_delayed_work(&onkey->work, msecs_to_jiffies(50)); 55 } 56 57 static void da9052_onkey_work(struct work_struct *work) 58 { 59 struct da9052_onkey *onkey = container_of(work, struct da9052_onkey, 60 work.work); 61 62 da9052_onkey_query(onkey); 63 } 64 65 static irqreturn_t da9052_onkey_irq(int irq, void *data) 66 { 67 struct da9052_onkey *onkey = data; 68 69 da9052_onkey_query(onkey); 70 71 return IRQ_HANDLED; 72 } 73 74 static int __devinit da9052_onkey_probe(struct platform_device *pdev) 75 { 76 struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent); 77 struct da9052_onkey *onkey; 78 struct input_dev *input_dev; 79 int irq; 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 87 irq = platform_get_irq_byname(pdev, "ONKEY"); 88 if (irq < 0) { 89 dev_err(&pdev->dev, 90 "Failed to get an IRQ for input device, %d\n", irq); 91 return -EINVAL; 92 } 93 94 onkey = kzalloc(sizeof(*onkey), GFP_KERNEL); 95 input_dev = input_allocate_device(); 96 if (!onkey || !input_dev) { 97 dev_err(&pdev->dev, "Failed to allocate memory\n"); 98 return -ENOMEM; 99 } 100 101 onkey->input = input_dev; 102 onkey->da9052 = da9052; 103 onkey->irq = irq; 104 INIT_DELAYED_WORK(&onkey->work, da9052_onkey_work); 105 106 input_dev->name = "da9052-onkey"; 107 input_dev->phys = "da9052-onkey/input0"; 108 input_dev->dev.parent = &pdev->dev; 109 110 input_dev->evbit[0] = BIT_MASK(EV_KEY); 111 __set_bit(KEY_POWER, input_dev->keybit); 112 113 error = request_threaded_irq(onkey->irq, NULL, da9052_onkey_irq, 114 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 115 "ONKEY", onkey); 116 if (error < 0) { 117 dev_err(onkey->da9052->dev, 118 "Failed to register ONKEY IRQ %d, error = %d\n", 119 onkey->irq, error); 120 goto err_free_mem; 121 } 122 123 error = input_register_device(onkey->input); 124 if (error) { 125 dev_err(&pdev->dev, "Unable to register input device, %d\n", 126 error); 127 goto err_free_irq; 128 } 129 130 platform_set_drvdata(pdev, onkey); 131 return 0; 132 133 err_free_irq: 134 free_irq(onkey->irq, onkey); 135 cancel_delayed_work_sync(&onkey->work); 136 err_free_mem: 137 input_free_device(input_dev); 138 kfree(onkey); 139 140 return error; 141 } 142 143 static int __devexit da9052_onkey_remove(struct platform_device *pdev) 144 { 145 struct da9052_onkey *onkey = platform_get_drvdata(pdev); 146 147 free_irq(onkey->irq, onkey); 148 cancel_delayed_work_sync(&onkey->work); 149 150 input_unregister_device(onkey->input); 151 kfree(onkey); 152 153 return 0; 154 } 155 156 static struct platform_driver da9052_onkey_driver = { 157 .probe = da9052_onkey_probe, 158 .remove = __devexit_p(da9052_onkey_remove), 159 .driver = { 160 .name = "da9052-onkey", 161 .owner = THIS_MODULE, 162 }, 163 }; 164 module_platform_driver(da9052_onkey_driver); 165 166 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); 167 MODULE_DESCRIPTION("Onkey driver for DA9052"); 168 MODULE_LICENSE("GPL"); 169 MODULE_ALIAS("platform:da9052-onkey"); 170