blob: 020569a499f20faa03a63fff95847ed8be42c273 [file] [log] [blame]
Ashish Jangamf0c5f652012-03-04 08:40:58 -08001/*
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
23struct da9052_onkey {
24 struct da9052 *da9052;
25 struct input_dev *input;
26 struct delayed_work work;
Ashish Jangamf0c5f652012-03-04 08:40:58 -080027};
28
29static 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
56static 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
64static 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 Pemberton5298cc42012-11-23 21:38:25 -080073static int da9052_onkey_probe(struct platform_device *pdev)
Ashish Jangamf0c5f652012-03-04 08:40:58 -080074{
75 struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent);
76 struct da9052_onkey *onkey;
77 struct input_dev *input_dev;
Ashish Jangamf0c5f652012-03-04 08:40:58 -080078 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 Jangamf0c5f652012-03-04 08:40:58 -080085 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 Juhl0e3d0f32012-04-11 20:55:18 -070089 error = -ENOMEM;
90 goto err_free_mem;
Ashish Jangamf0c5f652012-03-04 08:40:58 -080091 }
92
93 onkey->input = input_dev;
94 onkey->da9052 = da9052;
Ashish Jangamf0c5f652012-03-04 08:40:58 -080095 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 Estevam21eed072012-10-04 00:15:07 -0300104 error = da9052_request_irq(onkey->da9052, DA9052_IRQ_NONKEY, "ONKEY",
105 da9052_onkey_irq, onkey);
Ashish Jangamf0c5f652012-03-04 08:40:58 -0800106 if (error < 0) {
107 dev_err(onkey->da9052->dev,
Fabio Estevam21eed072012-10-04 00:15:07 -0300108 "Failed to register ONKEY IRQ: %d\n", error);
Ashish Jangamf0c5f652012-03-04 08:40:58 -0800109 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
122err_free_irq:
Fabio Estevam21eed072012-10-04 00:15:07 -0300123 da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey);
Ashish Jangamf0c5f652012-03-04 08:40:58 -0800124 cancel_delayed_work_sync(&onkey->work);
125err_free_mem:
126 input_free_device(input_dev);
127 kfree(onkey);
128
129 return error;
130}
131
Bill Pembertone2619cf2012-11-23 21:50:47 -0800132static int da9052_onkey_remove(struct platform_device *pdev)
Ashish Jangamf0c5f652012-03-04 08:40:58 -0800133{
134 struct da9052_onkey *onkey = platform_get_drvdata(pdev);
135
Fabio Estevam21eed072012-10-04 00:15:07 -0300136 da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey);
Ashish Jangamf0c5f652012-03-04 08:40:58 -0800137 cancel_delayed_work_sync(&onkey->work);
138
139 input_unregister_device(onkey->input);
140 kfree(onkey);
141
142 return 0;
143}
144
145static struct platform_driver da9052_onkey_driver = {
146 .probe = da9052_onkey_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800147 .remove = da9052_onkey_remove,
Ashish Jangamf0c5f652012-03-04 08:40:58 -0800148 .driver = {
149 .name = "da9052-onkey",
150 .owner = THIS_MODULE,
151 },
152};
153module_platform_driver(da9052_onkey_driver);
154
155MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
156MODULE_DESCRIPTION("Onkey driver for DA9052");
157MODULE_LICENSE("GPL");
158MODULE_ALIAS("platform:da9052-onkey");