1*80503b23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2fba65fe0SMichael Hennerich /*
3fba65fe0SMichael Hennerich * Keypad driver for Analog Devices ADP5520 MFD PMICs
4fba65fe0SMichael Hennerich *
5fba65fe0SMichael Hennerich * Copyright 2009 Analog Devices Inc.
6fba65fe0SMichael Hennerich */
7fba65fe0SMichael Hennerich
8fba65fe0SMichael Hennerich #include <linux/module.h>
9fba65fe0SMichael Hennerich #include <linux/kernel.h>
10fba65fe0SMichael Hennerich #include <linux/platform_device.h>
11fba65fe0SMichael Hennerich #include <linux/input.h>
12fba65fe0SMichael Hennerich #include <linux/mfd/adp5520.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
14ec62c7a8SHimangi Saraogi #include <linux/device.h>
15fba65fe0SMichael Hennerich
16fba65fe0SMichael Hennerich struct adp5520_keys {
17fba65fe0SMichael Hennerich struct input_dev *input;
18fba65fe0SMichael Hennerich struct notifier_block notifier;
19fba65fe0SMichael Hennerich struct device *master;
20fba65fe0SMichael Hennerich unsigned short keycode[ADP5520_KEYMAPSIZE];
21fba65fe0SMichael Hennerich };
22fba65fe0SMichael Hennerich
adp5520_keys_report_event(struct adp5520_keys * dev,unsigned short keymask,int value)23fba65fe0SMichael Hennerich static void adp5520_keys_report_event(struct adp5520_keys *dev,
24fba65fe0SMichael Hennerich unsigned short keymask, int value)
25fba65fe0SMichael Hennerich {
26fba65fe0SMichael Hennerich int i;
27fba65fe0SMichael Hennerich
28fba65fe0SMichael Hennerich for (i = 0; i < ADP5520_MAXKEYS; i++)
29fba65fe0SMichael Hennerich if (keymask & (1 << i))
30fba65fe0SMichael Hennerich input_report_key(dev->input, dev->keycode[i], value);
31fba65fe0SMichael Hennerich
32fba65fe0SMichael Hennerich input_sync(dev->input);
33fba65fe0SMichael Hennerich }
34fba65fe0SMichael Hennerich
adp5520_keys_notifier(struct notifier_block * nb,unsigned long event,void * data)35fba65fe0SMichael Hennerich static int adp5520_keys_notifier(struct notifier_block *nb,
36fba65fe0SMichael Hennerich unsigned long event, void *data)
37fba65fe0SMichael Hennerich {
38fba65fe0SMichael Hennerich struct adp5520_keys *dev;
39fba65fe0SMichael Hennerich uint8_t reg_val_lo, reg_val_hi;
40fba65fe0SMichael Hennerich unsigned short keymask;
41fba65fe0SMichael Hennerich
42fba65fe0SMichael Hennerich dev = container_of(nb, struct adp5520_keys, notifier);
43fba65fe0SMichael Hennerich
44fba65fe0SMichael Hennerich if (event & ADP5520_KP_INT) {
45fba65fe0SMichael Hennerich adp5520_read(dev->master, ADP5520_KP_INT_STAT_1, ®_val_lo);
46fba65fe0SMichael Hennerich adp5520_read(dev->master, ADP5520_KP_INT_STAT_2, ®_val_hi);
47fba65fe0SMichael Hennerich
48fba65fe0SMichael Hennerich keymask = (reg_val_hi << 8) | reg_val_lo;
49fba65fe0SMichael Hennerich /* Read twice to clear */
50fba65fe0SMichael Hennerich adp5520_read(dev->master, ADP5520_KP_INT_STAT_1, ®_val_lo);
51fba65fe0SMichael Hennerich adp5520_read(dev->master, ADP5520_KP_INT_STAT_2, ®_val_hi);
52fba65fe0SMichael Hennerich keymask |= (reg_val_hi << 8) | reg_val_lo;
53fba65fe0SMichael Hennerich adp5520_keys_report_event(dev, keymask, 1);
54fba65fe0SMichael Hennerich }
55fba65fe0SMichael Hennerich
56fba65fe0SMichael Hennerich if (event & ADP5520_KR_INT) {
57fba65fe0SMichael Hennerich adp5520_read(dev->master, ADP5520_KR_INT_STAT_1, ®_val_lo);
58fba65fe0SMichael Hennerich adp5520_read(dev->master, ADP5520_KR_INT_STAT_2, ®_val_hi);
59fba65fe0SMichael Hennerich
60fba65fe0SMichael Hennerich keymask = (reg_val_hi << 8) | reg_val_lo;
61fba65fe0SMichael Hennerich /* Read twice to clear */
62fba65fe0SMichael Hennerich adp5520_read(dev->master, ADP5520_KR_INT_STAT_1, ®_val_lo);
63fba65fe0SMichael Hennerich adp5520_read(dev->master, ADP5520_KR_INT_STAT_2, ®_val_hi);
64fba65fe0SMichael Hennerich keymask |= (reg_val_hi << 8) | reg_val_lo;
65fba65fe0SMichael Hennerich adp5520_keys_report_event(dev, keymask, 0);
66fba65fe0SMichael Hennerich }
67fba65fe0SMichael Hennerich
68fba65fe0SMichael Hennerich return 0;
69fba65fe0SMichael Hennerich }
70fba65fe0SMichael Hennerich
adp5520_keys_probe(struct platform_device * pdev)715298cc4cSBill Pemberton static int adp5520_keys_probe(struct platform_device *pdev)
72fba65fe0SMichael Hennerich {
73c838cb3dSJingoo Han struct adp5520_keys_platform_data *pdata = dev_get_platdata(&pdev->dev);
74fba65fe0SMichael Hennerich struct input_dev *input;
75fba65fe0SMichael Hennerich struct adp5520_keys *dev;
76fba65fe0SMichael Hennerich int ret, i;
77fba65fe0SMichael Hennerich unsigned char en_mask, ctl_mask = 0;
78fba65fe0SMichael Hennerich
79fba65fe0SMichael Hennerich if (pdev->id != ID_ADP5520) {
80fba65fe0SMichael Hennerich dev_err(&pdev->dev, "only ADP5520 supports Keypad\n");
81fba65fe0SMichael Hennerich return -EINVAL;
82fba65fe0SMichael Hennerich }
83fba65fe0SMichael Hennerich
84ec62c7a8SHimangi Saraogi if (!pdata) {
85fba65fe0SMichael Hennerich dev_err(&pdev->dev, "missing platform data\n");
86fba65fe0SMichael Hennerich return -EINVAL;
87fba65fe0SMichael Hennerich }
88fba65fe0SMichael Hennerich
89fba65fe0SMichael Hennerich if (!(pdata->rows_en_mask && pdata->cols_en_mask))
90fba65fe0SMichael Hennerich return -EINVAL;
91fba65fe0SMichael Hennerich
92ec62c7a8SHimangi Saraogi dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
93ec62c7a8SHimangi Saraogi if (!dev) {
94fba65fe0SMichael Hennerich dev_err(&pdev->dev, "failed to alloc memory\n");
95fba65fe0SMichael Hennerich return -ENOMEM;
96fba65fe0SMichael Hennerich }
97fba65fe0SMichael Hennerich
98ec62c7a8SHimangi Saraogi input = devm_input_allocate_device(&pdev->dev);
99ec62c7a8SHimangi Saraogi if (!input)
100ec62c7a8SHimangi Saraogi return -ENOMEM;
101fba65fe0SMichael Hennerich
102fba65fe0SMichael Hennerich dev->master = pdev->dev.parent;
103fba65fe0SMichael Hennerich dev->input = input;
104fba65fe0SMichael Hennerich
105fba65fe0SMichael Hennerich input->name = pdev->name;
106fba65fe0SMichael Hennerich input->phys = "adp5520-keys/input0";
107fba65fe0SMichael Hennerich input->dev.parent = &pdev->dev;
108fba65fe0SMichael Hennerich
109fba65fe0SMichael Hennerich input->id.bustype = BUS_I2C;
110fba65fe0SMichael Hennerich input->id.vendor = 0x0001;
111fba65fe0SMichael Hennerich input->id.product = 0x5520;
112fba65fe0SMichael Hennerich input->id.version = 0x0001;
113fba65fe0SMichael Hennerich
114fba65fe0SMichael Hennerich input->keycodesize = sizeof(dev->keycode[0]);
115fba65fe0SMichael Hennerich input->keycodemax = pdata->keymapsize;
116fba65fe0SMichael Hennerich input->keycode = dev->keycode;
117fba65fe0SMichael Hennerich
118fba65fe0SMichael Hennerich memcpy(dev->keycode, pdata->keymap,
119fba65fe0SMichael Hennerich pdata->keymapsize * input->keycodesize);
120fba65fe0SMichael Hennerich
121fba65fe0SMichael Hennerich /* setup input device */
122fba65fe0SMichael Hennerich __set_bit(EV_KEY, input->evbit);
123fba65fe0SMichael Hennerich
124fba65fe0SMichael Hennerich if (pdata->repeat)
125fba65fe0SMichael Hennerich __set_bit(EV_REP, input->evbit);
126fba65fe0SMichael Hennerich
127fba65fe0SMichael Hennerich for (i = 0; i < input->keycodemax; i++)
128fba65fe0SMichael Hennerich __set_bit(dev->keycode[i], input->keybit);
129fba65fe0SMichael Hennerich __clear_bit(KEY_RESERVED, input->keybit);
130fba65fe0SMichael Hennerich
131fba65fe0SMichael Hennerich ret = input_register_device(input);
132fba65fe0SMichael Hennerich if (ret) {
133fba65fe0SMichael Hennerich dev_err(&pdev->dev, "unable to register input device\n");
134ec62c7a8SHimangi Saraogi return ret;
135fba65fe0SMichael Hennerich }
136fba65fe0SMichael Hennerich
137fba65fe0SMichael Hennerich en_mask = pdata->rows_en_mask | pdata->cols_en_mask;
138fba65fe0SMichael Hennerich
139fba65fe0SMichael Hennerich ret = adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_1, en_mask);
140fba65fe0SMichael Hennerich
141fba65fe0SMichael Hennerich if (en_mask & ADP5520_COL_C3)
142fba65fe0SMichael Hennerich ctl_mask |= ADP5520_C3_MODE;
143fba65fe0SMichael Hennerich
144fba65fe0SMichael Hennerich if (en_mask & ADP5520_ROW_R3)
145fba65fe0SMichael Hennerich ctl_mask |= ADP5520_R3_MODE;
146fba65fe0SMichael Hennerich
147fba65fe0SMichael Hennerich if (ctl_mask)
148fba65fe0SMichael Hennerich ret |= adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
149fba65fe0SMichael Hennerich ctl_mask);
150fba65fe0SMichael Hennerich
151fba65fe0SMichael Hennerich ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
152fba65fe0SMichael Hennerich pdata->rows_en_mask);
153fba65fe0SMichael Hennerich
154fba65fe0SMichael Hennerich if (ret) {
155fba65fe0SMichael Hennerich dev_err(&pdev->dev, "failed to write\n");
156ec62c7a8SHimangi Saraogi return -EIO;
157fba65fe0SMichael Hennerich }
158fba65fe0SMichael Hennerich
159fba65fe0SMichael Hennerich dev->notifier.notifier_call = adp5520_keys_notifier;
160fba65fe0SMichael Hennerich ret = adp5520_register_notifier(dev->master, &dev->notifier,
161fba65fe0SMichael Hennerich ADP5520_KP_IEN | ADP5520_KR_IEN);
162fba65fe0SMichael Hennerich if (ret) {
163fba65fe0SMichael Hennerich dev_err(&pdev->dev, "failed to register notifier\n");
164ec62c7a8SHimangi Saraogi return ret;
165fba65fe0SMichael Hennerich }
166fba65fe0SMichael Hennerich
167fba65fe0SMichael Hennerich platform_set_drvdata(pdev, dev);
168fba65fe0SMichael Hennerich return 0;
169fba65fe0SMichael Hennerich }
170fba65fe0SMichael Hennerich
adp5520_keys_remove(struct platform_device * pdev)171e2619cf7SBill Pemberton static int adp5520_keys_remove(struct platform_device *pdev)
172fba65fe0SMichael Hennerich {
173fba65fe0SMichael Hennerich struct adp5520_keys *dev = platform_get_drvdata(pdev);
174fba65fe0SMichael Hennerich
175fba65fe0SMichael Hennerich adp5520_unregister_notifier(dev->master, &dev->notifier,
176fba65fe0SMichael Hennerich ADP5520_KP_IEN | ADP5520_KR_IEN);
177fba65fe0SMichael Hennerich
178fba65fe0SMichael Hennerich return 0;
179fba65fe0SMichael Hennerich }
180fba65fe0SMichael Hennerich
181fba65fe0SMichael Hennerich static struct platform_driver adp5520_keys_driver = {
182fba65fe0SMichael Hennerich .driver = {
183fba65fe0SMichael Hennerich .name = "adp5520-keys",
184fba65fe0SMichael Hennerich },
185fba65fe0SMichael Hennerich .probe = adp5520_keys_probe,
1861cb0aa88SBill Pemberton .remove = adp5520_keys_remove,
187fba65fe0SMichael Hennerich };
1885146c84fSJJ Ding module_platform_driver(adp5520_keys_driver);
189fba65fe0SMichael Hennerich
190fba65fe0SMichael Hennerich MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
191fba65fe0SMichael Hennerich MODULE_DESCRIPTION("Keys ADP5520 Driver");
192fba65fe0SMichael Hennerich MODULE_LICENSE("GPL");
193fba65fe0SMichael Hennerich MODULE_ALIAS("platform:adp5520-keys");
194