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