1 /* 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> 11 #include <linux/init.h> 12 #include <linux/platform_device.h> 13 #include <linux/input.h> 14 #include <linux/mfd/adp5520.h> 15 #include <linux/slab.h> 16 17 struct adp5520_keys { 18 struct input_dev *input; 19 struct notifier_block notifier; 20 struct device *master; 21 unsigned short keycode[ADP5520_KEYMAPSIZE]; 22 }; 23 24 static 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 36 static 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, ®_val_lo); 47 adp5520_read(dev->master, ADP5520_KP_INT_STAT_2, ®_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, ®_val_lo); 52 adp5520_read(dev->master, ADP5520_KP_INT_STAT_2, ®_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, ®_val_lo); 59 adp5520_read(dev->master, ADP5520_KR_INT_STAT_2, ®_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, ®_val_lo); 64 adp5520_read(dev->master, ADP5520_KR_INT_STAT_2, ®_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 72 static int adp5520_keys_probe(struct platform_device *pdev) 73 { 74 struct adp5520_keys_platform_data *pdata = pdev->dev.platform_data; 75 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 85 if (pdata == NULL) { 86 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 93 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 94 if (dev == NULL) { 95 dev_err(&pdev->dev, "failed to alloc memory\n"); 96 return -ENOMEM; 97 } 98 99 input = input_allocate_device(); 100 if (!input) { 101 ret = -ENOMEM; 102 goto err; 103 } 104 105 dev->master = pdev->dev.parent; 106 dev->input = input; 107 108 input->name = pdev->name; 109 input->phys = "adp5520-keys/input0"; 110 input->dev.parent = &pdev->dev; 111 112 input_set_drvdata(input, dev); 113 114 input->id.bustype = BUS_I2C; 115 input->id.vendor = 0x0001; 116 input->id.product = 0x5520; 117 input->id.version = 0x0001; 118 119 input->keycodesize = sizeof(dev->keycode[0]); 120 input->keycodemax = pdata->keymapsize; 121 input->keycode = dev->keycode; 122 123 memcpy(dev->keycode, pdata->keymap, 124 pdata->keymapsize * input->keycodesize); 125 126 /* setup input device */ 127 __set_bit(EV_KEY, input->evbit); 128 129 if (pdata->repeat) 130 __set_bit(EV_REP, input->evbit); 131 132 for (i = 0; i < input->keycodemax; i++) 133 __set_bit(dev->keycode[i], input->keybit); 134 __clear_bit(KEY_RESERVED, input->keybit); 135 136 ret = input_register_device(input); 137 if (ret) { 138 dev_err(&pdev->dev, "unable to register input device\n"); 139 goto err; 140 } 141 142 en_mask = pdata->rows_en_mask | pdata->cols_en_mask; 143 144 ret = adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_1, en_mask); 145 146 if (en_mask & ADP5520_COL_C3) 147 ctl_mask |= ADP5520_C3_MODE; 148 149 if (en_mask & ADP5520_ROW_R3) 150 ctl_mask |= ADP5520_R3_MODE; 151 152 if (ctl_mask) 153 ret |= adp5520_set_bits(dev->master, ADP5520_LED_CONTROL, 154 ctl_mask); 155 156 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP, 157 pdata->rows_en_mask); 158 159 if (ret) { 160 dev_err(&pdev->dev, "failed to write\n"); 161 ret = -EIO; 162 goto err1; 163 } 164 165 dev->notifier.notifier_call = adp5520_keys_notifier; 166 ret = adp5520_register_notifier(dev->master, &dev->notifier, 167 ADP5520_KP_IEN | ADP5520_KR_IEN); 168 if (ret) { 169 dev_err(&pdev->dev, "failed to register notifier\n"); 170 goto err1; 171 } 172 173 platform_set_drvdata(pdev, dev); 174 return 0; 175 176 err1: 177 input_unregister_device(input); 178 input = NULL; 179 err: 180 input_free_device(input); 181 kfree(dev); 182 return ret; 183 } 184 185 static int adp5520_keys_remove(struct platform_device *pdev) 186 { 187 struct adp5520_keys *dev = platform_get_drvdata(pdev); 188 189 adp5520_unregister_notifier(dev->master, &dev->notifier, 190 ADP5520_KP_IEN | ADP5520_KR_IEN); 191 192 input_unregister_device(dev->input); 193 kfree(dev); 194 return 0; 195 } 196 197 static struct platform_driver adp5520_keys_driver = { 198 .driver = { 199 .name = "adp5520-keys", 200 .owner = THIS_MODULE, 201 }, 202 .probe = adp5520_keys_probe, 203 .remove = adp5520_keys_remove, 204 }; 205 module_platform_driver(adp5520_keys_driver); 206 207 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); 208 MODULE_DESCRIPTION("Keys ADP5520 Driver"); 209 MODULE_LICENSE("GPL"); 210 MODULE_ALIAS("platform:adp5520-keys"); 211