blob: f0b9b37bde5854275f045bbe71f38b89f4afcd6c [file] [log] [blame]
Michael Hennerichfba65fe2009-10-10 13:54:02 -04001/*
2 * Keypad driver for Analog Devices ADP5520 MFD PMICs
3 *
4 * Copyright 2009 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
Michael Hennerichfba65fe2009-10-10 13:54:02 -040011#include <linux/platform_device.h>
12#include <linux/input.h>
13#include <linux/mfd/adp5520.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Himangi Saraogiec62c7a2014-05-22 10:40:42 -070015#include <linux/device.h>
Michael Hennerichfba65fe2009-10-10 13:54:02 -040016
17struct adp5520_keys {
18 struct input_dev *input;
19 struct notifier_block notifier;
20 struct device *master;
21 unsigned short keycode[ADP5520_KEYMAPSIZE];
22};
23
24static void adp5520_keys_report_event(struct adp5520_keys *dev,
25 unsigned short keymask, int value)
26{
27 int i;
28
29 for (i = 0; i < ADP5520_MAXKEYS; i++)
30 if (keymask & (1 << i))
31 input_report_key(dev->input, dev->keycode[i], value);
32
33 input_sync(dev->input);
34}
35
36static int adp5520_keys_notifier(struct notifier_block *nb,
37 unsigned long event, void *data)
38{
39 struct adp5520_keys *dev;
40 uint8_t reg_val_lo, reg_val_hi;
41 unsigned short keymask;
42
43 dev = container_of(nb, struct adp5520_keys, notifier);
44
45 if (event & ADP5520_KP_INT) {
46 adp5520_read(dev->master, ADP5520_KP_INT_STAT_1, &reg_val_lo);
47 adp5520_read(dev->master, ADP5520_KP_INT_STAT_2, &reg_val_hi);
48
49 keymask = (reg_val_hi << 8) | reg_val_lo;
50 /* Read twice to clear */
51 adp5520_read(dev->master, ADP5520_KP_INT_STAT_1, &reg_val_lo);
52 adp5520_read(dev->master, ADP5520_KP_INT_STAT_2, &reg_val_hi);
53 keymask |= (reg_val_hi << 8) | reg_val_lo;
54 adp5520_keys_report_event(dev, keymask, 1);
55 }
56
57 if (event & ADP5520_KR_INT) {
58 adp5520_read(dev->master, ADP5520_KR_INT_STAT_1, &reg_val_lo);
59 adp5520_read(dev->master, ADP5520_KR_INT_STAT_2, &reg_val_hi);
60
61 keymask = (reg_val_hi << 8) | reg_val_lo;
62 /* Read twice to clear */
63 adp5520_read(dev->master, ADP5520_KR_INT_STAT_1, &reg_val_lo);
64 adp5520_read(dev->master, ADP5520_KR_INT_STAT_2, &reg_val_hi);
65 keymask |= (reg_val_hi << 8) | reg_val_lo;
66 adp5520_keys_report_event(dev, keymask, 0);
67 }
68
69 return 0;
70}
71
Bill Pemberton5298cc42012-11-23 21:38:25 -080072static int adp5520_keys_probe(struct platform_device *pdev)
Michael Hennerichfba65fe2009-10-10 13:54:02 -040073{
Jingoo Hanc838cb32013-12-05 19:21:10 -080074 struct adp5520_keys_platform_data *pdata = dev_get_platdata(&pdev->dev);
Michael Hennerichfba65fe2009-10-10 13:54:02 -040075 struct input_dev *input;
76 struct adp5520_keys *dev;
77 int ret, i;
78 unsigned char en_mask, ctl_mask = 0;
79
80 if (pdev->id != ID_ADP5520) {
81 dev_err(&pdev->dev, "only ADP5520 supports Keypad\n");
82 return -EINVAL;
83 }
84
Himangi Saraogiec62c7a2014-05-22 10:40:42 -070085 if (!pdata) {
Michael Hennerichfba65fe2009-10-10 13:54:02 -040086 dev_err(&pdev->dev, "missing platform data\n");
87 return -EINVAL;
88 }
89
90 if (!(pdata->rows_en_mask && pdata->cols_en_mask))
91 return -EINVAL;
92
Himangi Saraogiec62c7a2014-05-22 10:40:42 -070093 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
94 if (!dev) {
Michael Hennerichfba65fe2009-10-10 13:54:02 -040095 dev_err(&pdev->dev, "failed to alloc memory\n");
96 return -ENOMEM;
97 }
98
Himangi Saraogiec62c7a2014-05-22 10:40:42 -070099 input = devm_input_allocate_device(&pdev->dev);
100 if (!input)
101 return -ENOMEM;
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400102
103 dev->master = pdev->dev.parent;
104 dev->input = input;
105
106 input->name = pdev->name;
107 input->phys = "adp5520-keys/input0";
108 input->dev.parent = &pdev->dev;
109
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400110 input->id.bustype = BUS_I2C;
111 input->id.vendor = 0x0001;
112 input->id.product = 0x5520;
113 input->id.version = 0x0001;
114
115 input->keycodesize = sizeof(dev->keycode[0]);
116 input->keycodemax = pdata->keymapsize;
117 input->keycode = dev->keycode;
118
119 memcpy(dev->keycode, pdata->keymap,
120 pdata->keymapsize * input->keycodesize);
121
122 /* setup input device */
123 __set_bit(EV_KEY, input->evbit);
124
125 if (pdata->repeat)
126 __set_bit(EV_REP, input->evbit);
127
128 for (i = 0; i < input->keycodemax; i++)
129 __set_bit(dev->keycode[i], input->keybit);
130 __clear_bit(KEY_RESERVED, input->keybit);
131
132 ret = input_register_device(input);
133 if (ret) {
134 dev_err(&pdev->dev, "unable to register input device\n");
Himangi Saraogiec62c7a2014-05-22 10:40:42 -0700135 return ret;
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400136 }
137
138 en_mask = pdata->rows_en_mask | pdata->cols_en_mask;
139
140 ret = adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_1, en_mask);
141
142 if (en_mask & ADP5520_COL_C3)
143 ctl_mask |= ADP5520_C3_MODE;
144
145 if (en_mask & ADP5520_ROW_R3)
146 ctl_mask |= ADP5520_R3_MODE;
147
148 if (ctl_mask)
149 ret |= adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
150 ctl_mask);
151
152 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
153 pdata->rows_en_mask);
154
155 if (ret) {
156 dev_err(&pdev->dev, "failed to write\n");
Himangi Saraogiec62c7a2014-05-22 10:40:42 -0700157 return -EIO;
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400158 }
159
160 dev->notifier.notifier_call = adp5520_keys_notifier;
161 ret = adp5520_register_notifier(dev->master, &dev->notifier,
162 ADP5520_KP_IEN | ADP5520_KR_IEN);
163 if (ret) {
164 dev_err(&pdev->dev, "failed to register notifier\n");
Himangi Saraogiec62c7a2014-05-22 10:40:42 -0700165 return ret;
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400166 }
167
168 platform_set_drvdata(pdev, dev);
169 return 0;
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400170}
171
Bill Pembertone2619cf2012-11-23 21:50:47 -0800172static int adp5520_keys_remove(struct platform_device *pdev)
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400173{
174 struct adp5520_keys *dev = platform_get_drvdata(pdev);
175
176 adp5520_unregister_notifier(dev->master, &dev->notifier,
177 ADP5520_KP_IEN | ADP5520_KR_IEN);
178
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400179 return 0;
180}
181
182static struct platform_driver adp5520_keys_driver = {
183 .driver = {
184 .name = "adp5520-keys",
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400185 },
186 .probe = adp5520_keys_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800187 .remove = adp5520_keys_remove,
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400188};
JJ Ding5146c842011-11-29 11:08:39 -0800189module_platform_driver(adp5520_keys_driver);
Michael Hennerichfba65fe2009-10-10 13:54:02 -0400190
191MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
192MODULE_DESCRIPTION("Keys ADP5520 Driver");
193MODULE_LICENSE("GPL");
194MODULE_ALIAS("platform:adp5520-keys");