1 /* 2 * Copyright (c) 2012-2016 Synaptics Incorporated 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 as published by 6 * the Free Software Foundation. 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/rmi.h> 11 #include <linux/input.h> 12 #include <linux/slab.h> 13 #include "rmi_driver.h" 14 15 #define RMI_F30_QUERY_SIZE 2 16 17 /* Defs for Query 0 */ 18 #define RMI_F30_EXTENDED_PATTERNS 0x01 19 #define RMI_F30_HAS_MAPPABLE_BUTTONS BIT(1) 20 #define RMI_F30_HAS_LED BIT(2) 21 #define RMI_F30_HAS_GPIO BIT(3) 22 #define RMI_F30_HAS_HAPTIC BIT(4) 23 #define RMI_F30_HAS_GPIO_DRV_CTL BIT(5) 24 #define RMI_F30_HAS_MECH_MOUSE_BTNS BIT(6) 25 26 /* Defs for Query 1 */ 27 #define RMI_F30_GPIO_LED_COUNT 0x1F 28 29 /* Defs for Control Registers */ 30 #define RMI_F30_CTRL_1_GPIO_DEBOUNCE 0x01 31 #define RMI_F30_CTRL_1_HALT BIT(4) 32 #define RMI_F30_CTRL_1_HALTED BIT(5) 33 #define RMI_F30_CTRL_10_NUM_MECH_MOUSE_BTNS 0x03 34 35 #define RMI_F30_CTRL_MAX_REGS 32 36 #define RMI_F30_CTRL_MAX_BYTES DIV_ROUND_UP(RMI_F30_CTRL_MAX_REGS, 8) 37 #define RMI_F30_CTRL_MAX_REG_BLOCKS 11 38 39 #define RMI_F30_CTRL_REGS_MAX_SIZE (RMI_F30_CTRL_MAX_BYTES \ 40 + 1 \ 41 + RMI_F30_CTRL_MAX_BYTES \ 42 + RMI_F30_CTRL_MAX_BYTES \ 43 + RMI_F30_CTRL_MAX_BYTES \ 44 + 6 \ 45 + RMI_F30_CTRL_MAX_REGS \ 46 + RMI_F30_CTRL_MAX_REGS \ 47 + RMI_F30_CTRL_MAX_BYTES \ 48 + 1 \ 49 + 1) 50 51 #define TRACKSTICK_RANGE_START 3 52 #define TRACKSTICK_RANGE_END 6 53 54 struct rmi_f30_ctrl_data { 55 int address; 56 int length; 57 u8 *regs; 58 }; 59 60 struct f30_data { 61 /* Query Data */ 62 bool has_extended_pattern; 63 bool has_mappable_buttons; 64 bool has_led; 65 bool has_gpio; 66 bool has_haptic; 67 bool has_gpio_driver_control; 68 bool has_mech_mouse_btns; 69 u8 gpioled_count; 70 71 u8 register_count; 72 73 /* Control Register Data */ 74 struct rmi_f30_ctrl_data ctrl[RMI_F30_CTRL_MAX_REG_BLOCKS]; 75 u8 ctrl_regs[RMI_F30_CTRL_REGS_MAX_SIZE]; 76 u32 ctrl_regs_size; 77 78 u8 data_regs[RMI_F30_CTRL_MAX_BYTES]; 79 u16 *gpioled_key_map; 80 81 struct input_dev *input; 82 83 struct rmi_function *f03; 84 bool trackstick_buttons; 85 }; 86 87 static int rmi_f30_read_control_parameters(struct rmi_function *fn, 88 struct f30_data *f30) 89 { 90 int error; 91 92 error = rmi_read_block(fn->rmi_dev, fn->fd.control_base_addr, 93 f30->ctrl_regs, f30->ctrl_regs_size); 94 if (error) { 95 dev_err(&fn->dev, 96 "%s: Could not read control registers at 0x%x: %d\n", 97 __func__, fn->fd.control_base_addr, error); 98 return error; 99 } 100 101 return 0; 102 } 103 104 static void rmi_f30_report_button(struct rmi_function *fn, 105 struct f30_data *f30, unsigned int button) 106 { 107 unsigned int reg_num = button >> 3; 108 unsigned int bit_num = button & 0x07; 109 u16 key_code = f30->gpioled_key_map[button]; 110 bool key_down = !(f30->data_regs[reg_num] & BIT(bit_num)); 111 112 if (f30->trackstick_buttons && 113 button >= TRACKSTICK_RANGE_START && 114 button <= TRACKSTICK_RANGE_END) { 115 rmi_f03_overwrite_button(f30->f03, key_code, key_down); 116 } else { 117 rmi_dbg(RMI_DEBUG_FN, &fn->dev, 118 "%s: call input report key (0x%04x) value (0x%02x)", 119 __func__, key_code, key_down); 120 121 input_report_key(f30->input, key_code, key_down); 122 } 123 } 124 125 static irqreturn_t rmi_f30_attention(int irq, void *ctx) 126 { 127 struct rmi_function *fn = ctx; 128 struct f30_data *f30 = dev_get_drvdata(&fn->dev); 129 struct rmi_driver_data *drvdata = dev_get_drvdata(&fn->rmi_dev->dev); 130 int error; 131 int i; 132 133 /* Read the gpi led data. */ 134 if (drvdata->attn_data.data) { 135 if (drvdata->attn_data.size < f30->register_count) { 136 dev_warn(&fn->dev, 137 "F30 interrupted, but data is missing\n"); 138 return IRQ_HANDLED; 139 } 140 memcpy(f30->data_regs, drvdata->attn_data.data, 141 f30->register_count); 142 drvdata->attn_data.data += f30->register_count; 143 drvdata->attn_data.size -= f30->register_count; 144 } else { 145 error = rmi_read_block(fn->rmi_dev, fn->fd.data_base_addr, 146 f30->data_regs, f30->register_count); 147 if (error) { 148 dev_err(&fn->dev, 149 "%s: Failed to read F30 data registers: %d\n", 150 __func__, error); 151 return IRQ_RETVAL(error); 152 } 153 } 154 155 if (f30->has_gpio) { 156 for (i = 0; i < f30->gpioled_count; i++) 157 if (f30->gpioled_key_map[i] != KEY_RESERVED) 158 rmi_f30_report_button(fn, f30, i); 159 if (f30->trackstick_buttons) 160 rmi_f03_commit_buttons(f30->f03); 161 } 162 163 return IRQ_HANDLED; 164 } 165 166 static int rmi_f30_config(struct rmi_function *fn) 167 { 168 struct f30_data *f30 = dev_get_drvdata(&fn->dev); 169 struct rmi_driver *drv = fn->rmi_dev->driver; 170 const struct rmi_device_platform_data *pdata = 171 rmi_get_platform_data(fn->rmi_dev); 172 int error; 173 174 /* can happen if f30_data.disable is set */ 175 if (!f30) 176 return 0; 177 178 if (pdata->f30_data.trackstick_buttons) { 179 /* Try [re-]establish link to F03. */ 180 f30->f03 = rmi_find_function(fn->rmi_dev, 0x03); 181 f30->trackstick_buttons = f30->f03 != NULL; 182 } 183 184 if (pdata->f30_data.disable) { 185 drv->clear_irq_bits(fn->rmi_dev, fn->irq_mask); 186 } else { 187 /* Write Control Register values back to device */ 188 error = rmi_write_block(fn->rmi_dev, fn->fd.control_base_addr, 189 f30->ctrl_regs, f30->ctrl_regs_size); 190 if (error) { 191 dev_err(&fn->dev, 192 "%s: Could not write control registers at 0x%x: %d\n", 193 __func__, fn->fd.control_base_addr, error); 194 return error; 195 } 196 197 drv->set_irq_bits(fn->rmi_dev, fn->irq_mask); 198 } 199 200 return 0; 201 } 202 203 static void rmi_f30_set_ctrl_data(struct rmi_f30_ctrl_data *ctrl, 204 int *ctrl_addr, int len, u8 **reg) 205 { 206 ctrl->address = *ctrl_addr; 207 ctrl->length = len; 208 ctrl->regs = *reg; 209 *ctrl_addr += len; 210 *reg += len; 211 } 212 213 static bool rmi_f30_is_valid_button(int button, struct rmi_f30_ctrl_data *ctrl) 214 { 215 int byte_position = button >> 3; 216 int bit_position = button & 0x07; 217 218 /* 219 * ctrl2 -> dir == 0 -> input mode 220 * ctrl3 -> data == 1 -> actual button 221 */ 222 return !(ctrl[2].regs[byte_position] & BIT(bit_position)) && 223 (ctrl[3].regs[byte_position] & BIT(bit_position)); 224 } 225 226 static int rmi_f30_map_gpios(struct rmi_function *fn, 227 struct f30_data *f30) 228 { 229 const struct rmi_device_platform_data *pdata = 230 rmi_get_platform_data(fn->rmi_dev); 231 struct input_dev *input = f30->input; 232 unsigned int button = BTN_LEFT; 233 unsigned int trackstick_button = BTN_LEFT; 234 bool button_mapped = false; 235 int i; 236 int button_count = min_t(u8, f30->gpioled_count, TRACKSTICK_RANGE_END); 237 238 f30->gpioled_key_map = devm_kcalloc(&fn->dev, 239 button_count, 240 sizeof(f30->gpioled_key_map[0]), 241 GFP_KERNEL); 242 if (!f30->gpioled_key_map) { 243 dev_err(&fn->dev, "Failed to allocate gpioled map memory.\n"); 244 return -ENOMEM; 245 } 246 247 for (i = 0; i < button_count; i++) { 248 if (!rmi_f30_is_valid_button(i, f30->ctrl)) 249 continue; 250 251 if (pdata->f30_data.trackstick_buttons && 252 i >= TRACKSTICK_RANGE_START && i < TRACKSTICK_RANGE_END) { 253 f30->gpioled_key_map[i] = trackstick_button++; 254 } else if (!pdata->f30_data.buttonpad || !button_mapped) { 255 f30->gpioled_key_map[i] = button; 256 input_set_capability(input, EV_KEY, button++); 257 button_mapped = true; 258 } 259 } 260 261 input->keycode = f30->gpioled_key_map; 262 input->keycodesize = sizeof(f30->gpioled_key_map[0]); 263 input->keycodemax = f30->gpioled_count; 264 265 /* 266 * Buttonpad could be also inferred from f30->has_mech_mouse_btns, 267 * but I am not sure, so use only the pdata info and the number of 268 * mapped buttons. 269 */ 270 if (pdata->f30_data.buttonpad || (button - BTN_LEFT == 1)) 271 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); 272 273 return 0; 274 } 275 276 static int rmi_f30_initialize(struct rmi_function *fn, struct f30_data *f30) 277 { 278 u8 *ctrl_reg = f30->ctrl_regs; 279 int control_address = fn->fd.control_base_addr; 280 u8 buf[RMI_F30_QUERY_SIZE]; 281 int error; 282 283 error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr, 284 buf, RMI_F30_QUERY_SIZE); 285 if (error) { 286 dev_err(&fn->dev, "Failed to read query register\n"); 287 return error; 288 } 289 290 f30->has_extended_pattern = buf[0] & RMI_F30_EXTENDED_PATTERNS; 291 f30->has_mappable_buttons = buf[0] & RMI_F30_HAS_MAPPABLE_BUTTONS; 292 f30->has_led = buf[0] & RMI_F30_HAS_LED; 293 f30->has_gpio = buf[0] & RMI_F30_HAS_GPIO; 294 f30->has_haptic = buf[0] & RMI_F30_HAS_HAPTIC; 295 f30->has_gpio_driver_control = buf[0] & RMI_F30_HAS_GPIO_DRV_CTL; 296 f30->has_mech_mouse_btns = buf[0] & RMI_F30_HAS_MECH_MOUSE_BTNS; 297 f30->gpioled_count = buf[1] & RMI_F30_GPIO_LED_COUNT; 298 299 f30->register_count = DIV_ROUND_UP(f30->gpioled_count, 8); 300 301 if (f30->has_gpio && f30->has_led) 302 rmi_f30_set_ctrl_data(&f30->ctrl[0], &control_address, 303 f30->register_count, &ctrl_reg); 304 305 rmi_f30_set_ctrl_data(&f30->ctrl[1], &control_address, 306 sizeof(u8), &ctrl_reg); 307 308 if (f30->has_gpio) { 309 rmi_f30_set_ctrl_data(&f30->ctrl[2], &control_address, 310 f30->register_count, &ctrl_reg); 311 312 rmi_f30_set_ctrl_data(&f30->ctrl[3], &control_address, 313 f30->register_count, &ctrl_reg); 314 } 315 316 if (f30->has_led) { 317 rmi_f30_set_ctrl_data(&f30->ctrl[4], &control_address, 318 f30->register_count, &ctrl_reg); 319 320 rmi_f30_set_ctrl_data(&f30->ctrl[5], &control_address, 321 f30->has_extended_pattern ? 6 : 2, 322 &ctrl_reg); 323 } 324 325 if (f30->has_led || f30->has_gpio_driver_control) { 326 /* control 6 uses a byte per gpio/led */ 327 rmi_f30_set_ctrl_data(&f30->ctrl[6], &control_address, 328 f30->gpioled_count, &ctrl_reg); 329 } 330 331 if (f30->has_mappable_buttons) { 332 /* control 7 uses a byte per gpio/led */ 333 rmi_f30_set_ctrl_data(&f30->ctrl[7], &control_address, 334 f30->gpioled_count, &ctrl_reg); 335 } 336 337 if (f30->has_haptic) { 338 rmi_f30_set_ctrl_data(&f30->ctrl[8], &control_address, 339 f30->register_count, &ctrl_reg); 340 341 rmi_f30_set_ctrl_data(&f30->ctrl[9], &control_address, 342 sizeof(u8), &ctrl_reg); 343 } 344 345 if (f30->has_mech_mouse_btns) 346 rmi_f30_set_ctrl_data(&f30->ctrl[10], &control_address, 347 sizeof(u8), &ctrl_reg); 348 349 f30->ctrl_regs_size = ctrl_reg - 350 f30->ctrl_regs ?: RMI_F30_CTRL_REGS_MAX_SIZE; 351 352 error = rmi_f30_read_control_parameters(fn, f30); 353 if (error) { 354 dev_err(&fn->dev, 355 "Failed to initialize F30 control params: %d\n", 356 error); 357 return error; 358 } 359 360 if (f30->has_gpio) { 361 error = rmi_f30_map_gpios(fn, f30); 362 if (error) 363 return error; 364 } 365 366 return 0; 367 } 368 369 static int rmi_f30_probe(struct rmi_function *fn) 370 { 371 struct rmi_device *rmi_dev = fn->rmi_dev; 372 const struct rmi_device_platform_data *pdata = 373 rmi_get_platform_data(rmi_dev); 374 struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev); 375 struct f30_data *f30; 376 int error; 377 378 if (pdata->f30_data.disable) 379 return 0; 380 381 if (!drv_data->input) { 382 dev_info(&fn->dev, "F30: no input device found, ignoring\n"); 383 return -ENXIO; 384 } 385 386 f30 = devm_kzalloc(&fn->dev, sizeof(*f30), GFP_KERNEL); 387 if (!f30) 388 return -ENOMEM; 389 390 f30->input = drv_data->input; 391 392 error = rmi_f30_initialize(fn, f30); 393 if (error) 394 return error; 395 396 dev_set_drvdata(&fn->dev, f30); 397 return 0; 398 } 399 400 struct rmi_function_handler rmi_f30_handler = { 401 .driver = { 402 .name = "rmi4_f30", 403 }, 404 .func = 0x30, 405 .probe = rmi_f30_probe, 406 .config = rmi_f30_config, 407 .attention = rmi_f30_attention, 408 }; 409