1 /* 2 * Touchkey driver for MELFAS MCS5000/5080 controller 3 * 4 * Copyright (C) 2010 Samsung Electronics Co.Ltd 5 * Author: HeungJun Kim <riverful.kim@samsung.com> 6 * Author: Joonyoung Shim <jy0922.shim@samsung.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/module.h> 15 #include <linux/i2c.h> 16 #include <linux/interrupt.h> 17 #include <linux/input.h> 18 #include <linux/irq.h> 19 #include <linux/slab.h> 20 #include <linux/platform_data/mcs.h> 21 #include <linux/pm.h> 22 23 /* MCS5000 Touchkey */ 24 #define MCS5000_TOUCHKEY_STATUS 0x04 25 #define MCS5000_TOUCHKEY_STATUS_PRESS 7 26 #define MCS5000_TOUCHKEY_FW 0x0a 27 #define MCS5000_TOUCHKEY_BASE_VAL 0x61 28 29 /* MCS5080 Touchkey */ 30 #define MCS5080_TOUCHKEY_STATUS 0x00 31 #define MCS5080_TOUCHKEY_STATUS_PRESS 3 32 #define MCS5080_TOUCHKEY_FW 0x01 33 #define MCS5080_TOUCHKEY_BASE_VAL 0x1 34 35 enum mcs_touchkey_type { 36 MCS5000_TOUCHKEY, 37 MCS5080_TOUCHKEY, 38 }; 39 40 struct mcs_touchkey_chip { 41 unsigned int status_reg; 42 unsigned int pressbit; 43 unsigned int press_invert; 44 unsigned int baseval; 45 }; 46 47 struct mcs_touchkey_data { 48 void (*poweron)(bool); 49 50 struct i2c_client *client; 51 struct input_dev *input_dev; 52 struct mcs_touchkey_chip chip; 53 unsigned int key_code; 54 unsigned int key_val; 55 unsigned short keycodes[]; 56 }; 57 58 static irqreturn_t mcs_touchkey_interrupt(int irq, void *dev_id) 59 { 60 struct mcs_touchkey_data *data = dev_id; 61 struct mcs_touchkey_chip *chip = &data->chip; 62 struct i2c_client *client = data->client; 63 struct input_dev *input = data->input_dev; 64 unsigned int key_val; 65 unsigned int pressed; 66 int val; 67 68 val = i2c_smbus_read_byte_data(client, chip->status_reg); 69 if (val < 0) { 70 dev_err(&client->dev, "i2c read error [%d]\n", val); 71 goto out; 72 } 73 74 pressed = (val & (1 << chip->pressbit)) >> chip->pressbit; 75 if (chip->press_invert) 76 pressed ^= chip->press_invert; 77 78 /* key_val is 0 when released, so we should use key_val of press. */ 79 if (pressed) { 80 key_val = val & (0xff >> (8 - chip->pressbit)); 81 if (!key_val) 82 goto out; 83 key_val -= chip->baseval; 84 data->key_code = data->keycodes[key_val]; 85 data->key_val = key_val; 86 } 87 88 input_event(input, EV_MSC, MSC_SCAN, data->key_val); 89 input_report_key(input, data->key_code, pressed); 90 input_sync(input); 91 92 dev_dbg(&client->dev, "key %d %d %s\n", data->key_val, data->key_code, 93 pressed ? "pressed" : "released"); 94 95 out: 96 return IRQ_HANDLED; 97 } 98 99 static int mcs_touchkey_probe(struct i2c_client *client, 100 const struct i2c_device_id *id) 101 { 102 const struct mcs_platform_data *pdata; 103 struct mcs_touchkey_data *data; 104 struct input_dev *input_dev; 105 unsigned int fw_reg; 106 int fw_ver; 107 int error; 108 int i; 109 110 pdata = dev_get_platdata(&client->dev); 111 if (!pdata) { 112 dev_err(&client->dev, "no platform data defined\n"); 113 return -EINVAL; 114 } 115 116 data = kzalloc(struct_size(data, keycodes, pdata->key_maxval + 1), 117 GFP_KERNEL); 118 input_dev = input_allocate_device(); 119 if (!data || !input_dev) { 120 dev_err(&client->dev, "Failed to allocate memory\n"); 121 error = -ENOMEM; 122 goto err_free_mem; 123 } 124 125 data->client = client; 126 data->input_dev = input_dev; 127 128 if (id->driver_data == MCS5000_TOUCHKEY) { 129 data->chip.status_reg = MCS5000_TOUCHKEY_STATUS; 130 data->chip.pressbit = MCS5000_TOUCHKEY_STATUS_PRESS; 131 data->chip.baseval = MCS5000_TOUCHKEY_BASE_VAL; 132 fw_reg = MCS5000_TOUCHKEY_FW; 133 } else { 134 data->chip.status_reg = MCS5080_TOUCHKEY_STATUS; 135 data->chip.pressbit = MCS5080_TOUCHKEY_STATUS_PRESS; 136 data->chip.press_invert = 1; 137 data->chip.baseval = MCS5080_TOUCHKEY_BASE_VAL; 138 fw_reg = MCS5080_TOUCHKEY_FW; 139 } 140 141 fw_ver = i2c_smbus_read_byte_data(client, fw_reg); 142 if (fw_ver < 0) { 143 error = fw_ver; 144 dev_err(&client->dev, "i2c read error[%d]\n", error); 145 goto err_free_mem; 146 } 147 dev_info(&client->dev, "Firmware version: %d\n", fw_ver); 148 149 input_dev->name = "MELFAS MCS Touchkey"; 150 input_dev->id.bustype = BUS_I2C; 151 input_dev->dev.parent = &client->dev; 152 input_dev->evbit[0] = BIT_MASK(EV_KEY); 153 if (!pdata->no_autorepeat) 154 input_dev->evbit[0] |= BIT_MASK(EV_REP); 155 input_dev->keycode = data->keycodes; 156 input_dev->keycodesize = sizeof(data->keycodes[0]); 157 input_dev->keycodemax = pdata->key_maxval + 1; 158 159 for (i = 0; i < pdata->keymap_size; i++) { 160 unsigned int val = MCS_KEY_VAL(pdata->keymap[i]); 161 unsigned int code = MCS_KEY_CODE(pdata->keymap[i]); 162 163 data->keycodes[val] = code; 164 __set_bit(code, input_dev->keybit); 165 } 166 167 input_set_capability(input_dev, EV_MSC, MSC_SCAN); 168 input_set_drvdata(input_dev, data); 169 170 if (pdata->cfg_pin) 171 pdata->cfg_pin(); 172 173 if (pdata->poweron) { 174 data->poweron = pdata->poweron; 175 data->poweron(true); 176 } 177 178 error = request_threaded_irq(client->irq, NULL, mcs_touchkey_interrupt, 179 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 180 client->dev.driver->name, data); 181 if (error) { 182 dev_err(&client->dev, "Failed to register interrupt\n"); 183 goto err_free_mem; 184 } 185 186 error = input_register_device(input_dev); 187 if (error) 188 goto err_free_irq; 189 190 i2c_set_clientdata(client, data); 191 return 0; 192 193 err_free_irq: 194 free_irq(client->irq, data); 195 err_free_mem: 196 input_free_device(input_dev); 197 kfree(data); 198 return error; 199 } 200 201 static int mcs_touchkey_remove(struct i2c_client *client) 202 { 203 struct mcs_touchkey_data *data = i2c_get_clientdata(client); 204 205 free_irq(client->irq, data); 206 if (data->poweron) 207 data->poweron(false); 208 input_unregister_device(data->input_dev); 209 kfree(data); 210 211 return 0; 212 } 213 214 static void mcs_touchkey_shutdown(struct i2c_client *client) 215 { 216 struct mcs_touchkey_data *data = i2c_get_clientdata(client); 217 218 if (data->poweron) 219 data->poweron(false); 220 } 221 222 #ifdef CONFIG_PM_SLEEP 223 static int mcs_touchkey_suspend(struct device *dev) 224 { 225 struct mcs_touchkey_data *data = dev_get_drvdata(dev); 226 struct i2c_client *client = data->client; 227 228 /* Disable the work */ 229 disable_irq(client->irq); 230 231 /* Finally turn off the power */ 232 if (data->poweron) 233 data->poweron(false); 234 235 return 0; 236 } 237 238 static int mcs_touchkey_resume(struct device *dev) 239 { 240 struct mcs_touchkey_data *data = dev_get_drvdata(dev); 241 struct i2c_client *client = data->client; 242 243 /* Enable the device first */ 244 if (data->poweron) 245 data->poweron(true); 246 247 /* Enable irq again */ 248 enable_irq(client->irq); 249 250 return 0; 251 } 252 #endif 253 254 static SIMPLE_DEV_PM_OPS(mcs_touchkey_pm_ops, 255 mcs_touchkey_suspend, mcs_touchkey_resume); 256 257 static const struct i2c_device_id mcs_touchkey_id[] = { 258 { "mcs5000_touchkey", MCS5000_TOUCHKEY }, 259 { "mcs5080_touchkey", MCS5080_TOUCHKEY }, 260 { } 261 }; 262 MODULE_DEVICE_TABLE(i2c, mcs_touchkey_id); 263 264 static struct i2c_driver mcs_touchkey_driver = { 265 .driver = { 266 .name = "mcs_touchkey", 267 .pm = &mcs_touchkey_pm_ops, 268 }, 269 .probe = mcs_touchkey_probe, 270 .remove = mcs_touchkey_remove, 271 .shutdown = mcs_touchkey_shutdown, 272 .id_table = mcs_touchkey_id, 273 }; 274 275 module_i2c_driver(mcs_touchkey_driver); 276 277 /* Module information */ 278 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); 279 MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>"); 280 MODULE_DESCRIPTION("Touchkey driver for MELFAS MCS5000/5080 controller"); 281 MODULE_LICENSE("GPL"); 282