Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Synaptics RMI4 touchscreen driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Synaptics Incorporated |
| 5 | * |
| 6 | * Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com> |
| 7 | * Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com> |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 8 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/input.h> |
| 28 | #include <linux/gpio.h> |
| 29 | #include <linux/regulator/consumer.h> |
| 30 | #include <linux/input/synaptics_dsx.h> |
| 31 | #include "synaptics_i2c_rmi4.h" |
| 32 | #ifdef KERNEL_ABOVE_2_6_38 |
| 33 | #include <linux/input/mt.h> |
| 34 | #endif |
| 35 | |
| 36 | #define DRIVER_NAME "synaptics_rmi4_i2c" |
| 37 | #define INPUT_PHYS_NAME "synaptics_rmi4_i2c/input0" |
| 38 | |
| 39 | #ifdef KERNEL_ABOVE_2_6_38 |
| 40 | #define TYPE_B_PROTOCOL |
| 41 | #endif |
| 42 | |
| 43 | #define NO_0D_WHILE_2D |
| 44 | /* |
| 45 | #define REPORT_2D_Z |
| 46 | */ |
| 47 | #define REPORT_2D_W |
| 48 | |
| 49 | #define RPT_TYPE (1 << 0) |
| 50 | #define RPT_X_LSB (1 << 1) |
| 51 | #define RPT_X_MSB (1 << 2) |
| 52 | #define RPT_Y_LSB (1 << 3) |
| 53 | #define RPT_Y_MSB (1 << 4) |
| 54 | #define RPT_Z (1 << 5) |
| 55 | #define RPT_WX (1 << 6) |
| 56 | #define RPT_WY (1 << 7) |
| 57 | #define RPT_DEFAULT (RPT_TYPE | RPT_X_LSB | RPT_X_MSB | RPT_Y_LSB | RPT_Y_MSB) |
| 58 | |
| 59 | #define EXP_FN_DET_INTERVAL 1000 /* ms */ |
| 60 | #define POLLING_PERIOD 1 /* ms */ |
| 61 | #define SYN_I2C_RETRY_TIMES 10 |
| 62 | #define MAX_ABS_MT_TOUCH_MAJOR 15 |
| 63 | |
| 64 | #define F01_STD_QUERY_LEN 21 |
| 65 | #define F01_BUID_ID_OFFSET 18 |
| 66 | #define F11_STD_QUERY_LEN 9 |
| 67 | #define F11_STD_CTRL_LEN 10 |
| 68 | #define F11_STD_DATA_LEN 12 |
| 69 | |
| 70 | #define NORMAL_OPERATION (0 << 0) |
| 71 | #define SENSOR_SLEEP (1 << 0) |
| 72 | #define NO_SLEEP_OFF (0 << 3) |
| 73 | #define NO_SLEEP_ON (1 << 3) |
| 74 | |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 75 | #define RMI4_VTG_MIN_UV 2700000 |
| 76 | #define RMI4_VTG_MAX_UV 3300000 |
| 77 | #define RMI4_ACTIVE_LOAD_UA 15000 |
| 78 | #define RMI4_LPM_LOAD_UA 10 |
| 79 | |
| 80 | #define RMI4_I2C_VTG_MIN_UV 1800000 |
| 81 | #define RMI4_I2C_VTG_MAX_UV 1800000 |
| 82 | #define RMI4_I2C_LOAD_UA 10000 |
| 83 | #define RMI4_I2C_LPM_LOAD_UA 10 |
| 84 | |
Amy Maloche | 946da66 | 2013-01-18 16:27:11 -0800 | [diff] [blame] | 85 | #define RMI4_GPIO_SLEEP_LOW_US 10000 |
| 86 | #define RMI4_GPIO_WAIT_HIGH_MS 25 |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 87 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 88 | static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data, |
| 89 | unsigned short addr, unsigned char *data, |
| 90 | unsigned short length); |
| 91 | |
| 92 | static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data, |
| 93 | unsigned short addr, unsigned char *data, |
| 94 | unsigned short length); |
| 95 | |
| 96 | static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data); |
| 97 | |
| 98 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 99 | static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev, |
| 100 | struct device_attribute *attr, char *buf); |
| 101 | |
| 102 | static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev, |
| 103 | struct device_attribute *attr, const char *buf, size_t count); |
| 104 | |
| 105 | static void synaptics_rmi4_early_suspend(struct early_suspend *h); |
| 106 | |
| 107 | static void synaptics_rmi4_late_resume(struct early_suspend *h); |
| 108 | |
| 109 | static int synaptics_rmi4_suspend(struct device *dev); |
| 110 | |
| 111 | static int synaptics_rmi4_resume(struct device *dev); |
| 112 | #endif |
| 113 | |
| 114 | static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev, |
| 115 | struct device_attribute *attr, const char *buf, size_t count); |
| 116 | |
| 117 | static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev, |
| 118 | struct device_attribute *attr, char *buf); |
| 119 | |
| 120 | static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev, |
| 121 | struct device_attribute *attr, char *buf); |
| 122 | |
| 123 | static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev, |
| 124 | struct device_attribute *attr, char *buf); |
| 125 | |
| 126 | static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev, |
| 127 | struct device_attribute *attr, char *buf); |
| 128 | |
| 129 | static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev, |
| 130 | struct device_attribute *attr, const char *buf, size_t count); |
| 131 | |
| 132 | struct synaptics_rmi4_f01_device_status { |
| 133 | union { |
| 134 | struct { |
| 135 | unsigned char status_code:4; |
| 136 | unsigned char reserved:2; |
| 137 | unsigned char flash_prog:1; |
| 138 | unsigned char unconfigured:1; |
| 139 | } __packed; |
| 140 | unsigned char data[1]; |
| 141 | }; |
| 142 | }; |
| 143 | |
| 144 | struct synaptics_rmi4_f1a_query { |
| 145 | union { |
| 146 | struct { |
| 147 | unsigned char max_button_count:3; |
| 148 | unsigned char reserved:5; |
| 149 | unsigned char has_general_control:1; |
| 150 | unsigned char has_interrupt_enable:1; |
| 151 | unsigned char has_multibutton_select:1; |
| 152 | unsigned char has_tx_rx_map:1; |
| 153 | unsigned char has_perbutton_threshold:1; |
| 154 | unsigned char has_release_threshold:1; |
| 155 | unsigned char has_strongestbtn_hysteresis:1; |
| 156 | unsigned char has_filter_strength:1; |
| 157 | } __packed; |
| 158 | unsigned char data[2]; |
| 159 | }; |
| 160 | }; |
| 161 | |
| 162 | struct synaptics_rmi4_f1a_control_0 { |
| 163 | union { |
| 164 | struct { |
| 165 | unsigned char multibutton_report:2; |
| 166 | unsigned char filter_mode:2; |
| 167 | unsigned char reserved:4; |
| 168 | } __packed; |
| 169 | unsigned char data[1]; |
| 170 | }; |
| 171 | }; |
| 172 | |
| 173 | struct synaptics_rmi4_f1a_control_3_4 { |
| 174 | unsigned char transmitterbutton; |
| 175 | unsigned char receiverbutton; |
| 176 | }; |
| 177 | |
| 178 | struct synaptics_rmi4_f1a_control { |
| 179 | struct synaptics_rmi4_f1a_control_0 general_control; |
| 180 | unsigned char *button_int_enable; |
| 181 | unsigned char *multi_button; |
| 182 | struct synaptics_rmi4_f1a_control_3_4 *electrode_map; |
| 183 | unsigned char *button_threshold; |
| 184 | unsigned char button_release_threshold; |
| 185 | unsigned char strongest_button_hysteresis; |
| 186 | unsigned char filter_strength; |
| 187 | }; |
| 188 | |
| 189 | struct synaptics_rmi4_f1a_handle { |
| 190 | int button_bitmask_size; |
| 191 | unsigned char button_count; |
| 192 | unsigned char valid_button_count; |
| 193 | unsigned char *button_data_buffer; |
| 194 | unsigned char *button_map; |
| 195 | struct synaptics_rmi4_f1a_query button_query; |
| 196 | struct synaptics_rmi4_f1a_control button_control; |
| 197 | }; |
| 198 | |
| 199 | struct synaptics_rmi4_exp_fn { |
| 200 | enum exp_fn fn_type; |
| 201 | bool inserted; |
| 202 | int (*func_init)(struct synaptics_rmi4_data *rmi4_data); |
| 203 | void (*func_remove)(struct synaptics_rmi4_data *rmi4_data); |
| 204 | void (*func_attn)(struct synaptics_rmi4_data *rmi4_data, |
| 205 | unsigned char intr_mask); |
| 206 | struct list_head link; |
| 207 | }; |
| 208 | |
| 209 | static struct device_attribute attrs[] = { |
| 210 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 211 | __ATTR(full_pm_cycle, (S_IRUGO | S_IWUGO), |
| 212 | synaptics_rmi4_full_pm_cycle_show, |
| 213 | synaptics_rmi4_full_pm_cycle_store), |
| 214 | #endif |
| 215 | __ATTR(reset, S_IWUGO, |
| 216 | synaptics_rmi4_show_error, |
| 217 | synaptics_rmi4_f01_reset_store), |
| 218 | __ATTR(productinfo, S_IRUGO, |
| 219 | synaptics_rmi4_f01_productinfo_show, |
| 220 | synaptics_rmi4_store_error), |
| 221 | __ATTR(buildid, S_IRUGO, |
| 222 | synaptics_rmi4_f01_buildid_show, |
| 223 | synaptics_rmi4_store_error), |
| 224 | __ATTR(flashprog, S_IRUGO, |
| 225 | synaptics_rmi4_f01_flashprog_show, |
| 226 | synaptics_rmi4_store_error), |
| 227 | __ATTR(0dbutton, (S_IRUGO | S_IWUGO), |
| 228 | synaptics_rmi4_0dbutton_show, |
| 229 | synaptics_rmi4_0dbutton_store), |
| 230 | }; |
| 231 | |
| 232 | static bool exp_fn_inited; |
| 233 | static struct mutex exp_fn_list_mutex; |
| 234 | static struct list_head exp_fn_list; |
| 235 | |
| 236 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 237 | static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev, |
| 238 | struct device_attribute *attr, char *buf) |
| 239 | { |
| 240 | struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); |
| 241 | |
| 242 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 243 | rmi4_data->full_pm_cycle); |
| 244 | } |
| 245 | |
| 246 | static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev, |
| 247 | struct device_attribute *attr, const char *buf, size_t count) |
| 248 | { |
| 249 | unsigned int input; |
| 250 | struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); |
| 251 | |
| 252 | if (sscanf(buf, "%u", &input) != 1) |
| 253 | return -EINVAL; |
| 254 | |
| 255 | rmi4_data->full_pm_cycle = input > 0 ? 1 : 0; |
| 256 | |
| 257 | return count; |
| 258 | } |
| 259 | #endif |
| 260 | |
| 261 | static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev, |
| 262 | struct device_attribute *attr, const char *buf, size_t count) |
| 263 | { |
| 264 | int retval; |
| 265 | unsigned int reset; |
| 266 | struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); |
| 267 | |
| 268 | if (sscanf(buf, "%u", &reset) != 1) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | if (reset != 1) |
| 272 | return -EINVAL; |
| 273 | |
| 274 | retval = synaptics_rmi4_reset_device(rmi4_data); |
| 275 | if (retval < 0) { |
| 276 | dev_err(dev, |
| 277 | "%s: Failed to issue reset command, error = %d\n", |
| 278 | __func__, retval); |
| 279 | return retval; |
| 280 | } |
| 281 | |
| 282 | return count; |
| 283 | } |
| 284 | |
| 285 | static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev, |
| 286 | struct device_attribute *attr, char *buf) |
| 287 | { |
| 288 | struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); |
| 289 | |
| 290 | return snprintf(buf, PAGE_SIZE, "0x%02x 0x%02x\n", |
| 291 | (rmi4_data->rmi4_mod_info.product_info[0]), |
| 292 | (rmi4_data->rmi4_mod_info.product_info[1])); |
| 293 | } |
| 294 | |
| 295 | static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev, |
| 296 | struct device_attribute *attr, char *buf) |
| 297 | { |
| 298 | unsigned int build_id; |
| 299 | struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); |
| 300 | struct synaptics_rmi4_device_info *rmi; |
| 301 | |
| 302 | rmi = &(rmi4_data->rmi4_mod_info); |
| 303 | |
| 304 | build_id = (unsigned int)rmi->build_id[0] + |
| 305 | (unsigned int)rmi->build_id[1] * 0x100 + |
| 306 | (unsigned int)rmi->build_id[2] * 0x10000; |
| 307 | |
| 308 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 309 | build_id); |
| 310 | } |
| 311 | |
| 312 | static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev, |
| 313 | struct device_attribute *attr, char *buf) |
| 314 | { |
| 315 | int retval; |
| 316 | struct synaptics_rmi4_f01_device_status device_status; |
| 317 | struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); |
| 318 | |
| 319 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 320 | rmi4_data->f01_data_base_addr, |
| 321 | device_status.data, |
| 322 | sizeof(device_status.data)); |
| 323 | if (retval < 0) { |
| 324 | dev_err(dev, |
| 325 | "%s: Failed to read device status, error = %d\n", |
| 326 | __func__, retval); |
| 327 | return retval; |
| 328 | } |
| 329 | |
| 330 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 331 | device_status.flash_prog); |
| 332 | } |
| 333 | |
| 334 | static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev, |
| 335 | struct device_attribute *attr, char *buf) |
| 336 | { |
| 337 | struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); |
| 338 | |
| 339 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 340 | rmi4_data->button_0d_enabled); |
| 341 | } |
| 342 | |
| 343 | static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev, |
| 344 | struct device_attribute *attr, const char *buf, size_t count) |
| 345 | { |
| 346 | int retval; |
| 347 | unsigned int input; |
| 348 | unsigned char ii; |
| 349 | unsigned char intr_enable; |
| 350 | struct synaptics_rmi4_fn *fhandler; |
| 351 | struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); |
| 352 | struct synaptics_rmi4_device_info *rmi; |
| 353 | |
| 354 | rmi = &(rmi4_data->rmi4_mod_info); |
| 355 | |
| 356 | if (sscanf(buf, "%u", &input) != 1) |
| 357 | return -EINVAL; |
| 358 | |
| 359 | input = input > 0 ? 1 : 0; |
| 360 | |
| 361 | if (rmi4_data->button_0d_enabled == input) |
| 362 | return count; |
| 363 | |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 364 | if (!list_empty(&rmi->support_fn_list)) { |
| 365 | list_for_each_entry(fhandler, &rmi->support_fn_list, link) { |
| 366 | if (fhandler->fn_number == SYNAPTICS_RMI4_F1A) { |
| 367 | ii = fhandler->intr_reg_num; |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 368 | |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 369 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 370 | rmi4_data->f01_ctrl_base_addr + |
| 371 | 1 + ii, |
| 372 | &intr_enable, |
| 373 | sizeof(intr_enable)); |
| 374 | if (retval < 0) |
| 375 | return retval; |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 376 | |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 377 | if (input == 1) |
| 378 | intr_enable |= fhandler->intr_mask; |
| 379 | else |
| 380 | intr_enable &= ~fhandler->intr_mask; |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 381 | |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 382 | retval = synaptics_rmi4_i2c_write(rmi4_data, |
| 383 | rmi4_data->f01_ctrl_base_addr + |
| 384 | 1 + ii, |
| 385 | &intr_enable, |
| 386 | sizeof(intr_enable)); |
| 387 | if (retval < 0) |
| 388 | return retval; |
| 389 | } |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | rmi4_data->button_0d_enabled = input; |
| 394 | |
| 395 | return count; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * synaptics_rmi4_set_page() |
| 400 | * |
| 401 | * Called by synaptics_rmi4_i2c_read() and synaptics_rmi4_i2c_write(). |
| 402 | * |
| 403 | * This function writes to the page select register to switch to the |
| 404 | * assigned page. |
| 405 | */ |
| 406 | static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *rmi4_data, |
| 407 | unsigned int address) |
| 408 | { |
| 409 | int retval = 0; |
| 410 | unsigned char retry; |
| 411 | unsigned char buf[PAGE_SELECT_LEN]; |
| 412 | unsigned char page; |
| 413 | struct i2c_client *i2c = rmi4_data->i2c_client; |
| 414 | |
| 415 | page = ((address >> 8) & MASK_8BIT); |
| 416 | if (page != rmi4_data->current_page) { |
| 417 | buf[0] = MASK_8BIT; |
| 418 | buf[1] = page; |
| 419 | for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) { |
| 420 | retval = i2c_master_send(i2c, buf, PAGE_SELECT_LEN); |
| 421 | if (retval != PAGE_SELECT_LEN) { |
| 422 | dev_err(&i2c->dev, |
| 423 | "%s: I2C retry %d\n", |
| 424 | __func__, retry + 1); |
| 425 | msleep(20); |
| 426 | } else { |
| 427 | rmi4_data->current_page = page; |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | } else |
| 432 | return PAGE_SELECT_LEN; |
| 433 | return (retval == PAGE_SELECT_LEN) ? retval : -EIO; |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * synaptics_rmi4_i2c_read() |
| 438 | * |
| 439 | * Called by various functions in this driver, and also exported to |
| 440 | * other expansion Function modules such as rmi_dev. |
| 441 | * |
| 442 | * This function reads data of an arbitrary length from the sensor, |
| 443 | * starting from an assigned register address of the sensor, via I2C |
| 444 | * with a retry mechanism. |
| 445 | */ |
| 446 | static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data, |
| 447 | unsigned short addr, unsigned char *data, unsigned short length) |
| 448 | { |
| 449 | int retval; |
| 450 | unsigned char retry; |
| 451 | unsigned char buf; |
| 452 | struct i2c_msg msg[] = { |
| 453 | { |
| 454 | .addr = rmi4_data->i2c_client->addr, |
| 455 | .flags = 0, |
| 456 | .len = 1, |
| 457 | .buf = &buf, |
| 458 | }, |
| 459 | { |
| 460 | .addr = rmi4_data->i2c_client->addr, |
| 461 | .flags = I2C_M_RD, |
| 462 | .len = length, |
| 463 | .buf = data, |
| 464 | }, |
| 465 | }; |
| 466 | |
| 467 | buf = addr & MASK_8BIT; |
| 468 | |
| 469 | mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex)); |
| 470 | |
| 471 | retval = synaptics_rmi4_set_page(rmi4_data, addr); |
| 472 | if (retval != PAGE_SELECT_LEN) |
| 473 | goto exit; |
| 474 | |
| 475 | for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) { |
| 476 | if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 2) == 2) { |
| 477 | retval = length; |
| 478 | break; |
| 479 | } |
| 480 | dev_err(&rmi4_data->i2c_client->dev, |
| 481 | "%s: I2C retry %d\n", |
| 482 | __func__, retry + 1); |
| 483 | msleep(20); |
| 484 | } |
| 485 | |
| 486 | if (retry == SYN_I2C_RETRY_TIMES) { |
| 487 | dev_err(&rmi4_data->i2c_client->dev, |
| 488 | "%s: I2C read over retry limit\n", |
| 489 | __func__); |
| 490 | retval = -EIO; |
| 491 | } |
| 492 | |
| 493 | exit: |
| 494 | mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex)); |
| 495 | |
| 496 | return retval; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * synaptics_rmi4_i2c_write() |
| 501 | * |
| 502 | * Called by various functions in this driver, and also exported to |
| 503 | * other expansion Function modules such as rmi_dev. |
| 504 | * |
| 505 | * This function writes data of an arbitrary length to the sensor, |
| 506 | * starting from an assigned register address of the sensor, via I2C with |
| 507 | * a retry mechanism. |
| 508 | */ |
| 509 | static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data, |
| 510 | unsigned short addr, unsigned char *data, unsigned short length) |
| 511 | { |
| 512 | int retval; |
| 513 | unsigned char retry; |
| 514 | unsigned char buf[length + 1]; |
| 515 | struct i2c_msg msg[] = { |
| 516 | { |
| 517 | .addr = rmi4_data->i2c_client->addr, |
| 518 | .flags = 0, |
| 519 | .len = length + 1, |
| 520 | .buf = buf, |
| 521 | } |
| 522 | }; |
| 523 | |
| 524 | mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex)); |
| 525 | |
| 526 | retval = synaptics_rmi4_set_page(rmi4_data, addr); |
| 527 | if (retval != PAGE_SELECT_LEN) |
| 528 | goto exit; |
| 529 | |
| 530 | buf[0] = addr & MASK_8BIT; |
| 531 | memcpy(&buf[1], &data[0], length); |
| 532 | |
| 533 | for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) { |
| 534 | if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 1) == 1) { |
| 535 | retval = length; |
| 536 | break; |
| 537 | } |
| 538 | dev_err(&rmi4_data->i2c_client->dev, |
| 539 | "%s: I2C retry %d\n", |
| 540 | __func__, retry + 1); |
| 541 | msleep(20); |
| 542 | } |
| 543 | |
| 544 | if (retry == SYN_I2C_RETRY_TIMES) { |
| 545 | dev_err(&rmi4_data->i2c_client->dev, |
| 546 | "%s: I2C write over retry limit\n", |
| 547 | __func__); |
| 548 | retval = -EIO; |
| 549 | } |
| 550 | |
| 551 | exit: |
| 552 | mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex)); |
| 553 | |
| 554 | return retval; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * synaptics_rmi4_f11_abs_report() |
| 559 | * |
| 560 | * Called by synaptics_rmi4_report_touch() when valid Function $11 |
| 561 | * finger data has been detected. |
| 562 | * |
| 563 | * This function reads the Function $11 data registers, determines the |
| 564 | * status of each finger supported by the Function, processes any |
| 565 | * necessary coordinate manipulation, reports the finger data to |
| 566 | * the input subsystem, and returns the number of fingers detected. |
| 567 | */ |
| 568 | static int synaptics_rmi4_f11_abs_report(struct synaptics_rmi4_data *rmi4_data, |
| 569 | struct synaptics_rmi4_fn *fhandler) |
| 570 | { |
| 571 | int retval; |
| 572 | unsigned char touch_count = 0; /* number of touch points */ |
| 573 | unsigned char reg_index; |
| 574 | unsigned char finger; |
| 575 | unsigned char fingers_supported; |
| 576 | unsigned char num_of_finger_status_regs; |
| 577 | unsigned char finger_shift; |
| 578 | unsigned char finger_status; |
| 579 | unsigned char data_reg_blk_size; |
| 580 | unsigned char finger_status_reg[3]; |
| 581 | unsigned char data[F11_STD_DATA_LEN]; |
| 582 | unsigned short data_addr; |
| 583 | unsigned short data_offset; |
| 584 | int x; |
| 585 | int y; |
| 586 | int wx; |
| 587 | int wy; |
| 588 | |
| 589 | /* |
| 590 | * The number of finger status registers is determined by the |
| 591 | * maximum number of fingers supported - 2 bits per finger. So |
| 592 | * the number of finger status registers to read is: |
| 593 | * register_count = ceil(max_num_of_fingers / 4) |
| 594 | */ |
| 595 | fingers_supported = fhandler->num_of_data_points; |
| 596 | num_of_finger_status_regs = (fingers_supported + 3) / 4; |
| 597 | data_addr = fhandler->full_addr.data_base; |
| 598 | data_reg_blk_size = fhandler->size_of_data_register_block; |
| 599 | |
| 600 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 601 | data_addr, |
| 602 | finger_status_reg, |
| 603 | num_of_finger_status_regs); |
| 604 | if (retval < 0) |
| 605 | return 0; |
| 606 | |
| 607 | for (finger = 0; finger < fingers_supported; finger++) { |
| 608 | reg_index = finger / 4; |
| 609 | finger_shift = (finger % 4) * 2; |
| 610 | finger_status = (finger_status_reg[reg_index] >> finger_shift) |
| 611 | & MASK_2BIT; |
| 612 | |
| 613 | /* |
| 614 | * Each 2-bit finger status field represents the following: |
| 615 | * 00 = finger not present |
| 616 | * 01 = finger present and data accurate |
| 617 | * 10 = finger present but data may be inaccurate |
| 618 | * 11 = reserved |
| 619 | */ |
| 620 | #ifdef TYPE_B_PROTOCOL |
| 621 | input_mt_slot(rmi4_data->input_dev, finger); |
| 622 | input_mt_report_slot_state(rmi4_data->input_dev, |
| 623 | MT_TOOL_FINGER, finger_status != 0); |
| 624 | #endif |
| 625 | |
| 626 | if (finger_status) { |
| 627 | data_offset = data_addr + |
| 628 | num_of_finger_status_regs + |
| 629 | (finger * data_reg_blk_size); |
| 630 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 631 | data_offset, |
| 632 | data, |
| 633 | data_reg_blk_size); |
| 634 | if (retval < 0) |
| 635 | return 0; |
| 636 | |
| 637 | x = (data[0] << 4) | (data[2] & MASK_4BIT); |
| 638 | y = (data[1] << 4) | ((data[2] >> 4) & MASK_4BIT); |
| 639 | wx = (data[3] & MASK_4BIT); |
| 640 | wy = (data[3] >> 4) & MASK_4BIT; |
| 641 | |
| 642 | if (rmi4_data->board->x_flip) |
| 643 | x = rmi4_data->sensor_max_x - x; |
| 644 | if (rmi4_data->board->y_flip) |
| 645 | y = rmi4_data->sensor_max_y - y; |
| 646 | |
| 647 | dev_dbg(&rmi4_data->i2c_client->dev, |
| 648 | "%s: Finger %d:\n" |
| 649 | "status = 0x%02x\n" |
| 650 | "x = %d\n" |
| 651 | "y = %d\n" |
| 652 | "wx = %d\n" |
| 653 | "wy = %d\n", |
| 654 | __func__, finger, |
| 655 | finger_status, |
| 656 | x, y, wx, wy); |
| 657 | |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 658 | input_report_key(rmi4_data->input_dev, |
| 659 | BTN_TOUCH, 1); |
| 660 | input_report_key(rmi4_data->input_dev, |
| 661 | BTN_TOOL_FINGER, 1); |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 662 | input_report_abs(rmi4_data->input_dev, |
| 663 | ABS_MT_POSITION_X, x); |
| 664 | input_report_abs(rmi4_data->input_dev, |
| 665 | ABS_MT_POSITION_Y, y); |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 666 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 667 | #ifdef REPORT_2D_W |
| 668 | input_report_abs(rmi4_data->input_dev, |
| 669 | ABS_MT_TOUCH_MAJOR, max(wx, wy)); |
| 670 | input_report_abs(rmi4_data->input_dev, |
| 671 | ABS_MT_TOUCH_MINOR, min(wx, wy)); |
| 672 | #endif |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 673 | #ifndef TYPE_B_PROTOCOL |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 674 | input_mt_sync(rmi4_data->input_dev); |
| 675 | #endif |
| 676 | touch_count++; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | #ifndef TYPE_B_PROTOCOL |
| 681 | if (!touch_count) |
| 682 | input_mt_sync(rmi4_data->input_dev); |
| 683 | #else |
| 684 | /* sync after groups of events */ |
| 685 | #ifdef KERNEL_ABOVE_3_7 |
| 686 | input_mt_sync_frame(rmi4_data->input_dev); |
| 687 | #endif |
| 688 | #endif |
| 689 | |
| 690 | input_sync(rmi4_data->input_dev); |
| 691 | |
| 692 | return touch_count; |
| 693 | } |
| 694 | |
| 695 | static void synaptics_rmi4_f1a_report(struct synaptics_rmi4_data *rmi4_data, |
| 696 | struct synaptics_rmi4_fn *fhandler) |
| 697 | { |
| 698 | int retval; |
| 699 | unsigned char button; |
| 700 | unsigned char index; |
| 701 | unsigned char shift; |
| 702 | unsigned char status; |
| 703 | unsigned char *data; |
| 704 | unsigned short data_addr = fhandler->full_addr.data_base; |
| 705 | struct synaptics_rmi4_f1a_handle *f1a = fhandler->data; |
| 706 | static unsigned char do_once = 1; |
| 707 | static bool current_status[MAX_NUMBER_OF_BUTTONS]; |
| 708 | #ifdef NO_0D_WHILE_2D |
| 709 | static bool before_2d_status[MAX_NUMBER_OF_BUTTONS]; |
| 710 | static bool while_2d_status[MAX_NUMBER_OF_BUTTONS]; |
| 711 | #endif |
| 712 | |
| 713 | if (do_once) { |
| 714 | memset(current_status, 0, sizeof(current_status)); |
| 715 | #ifdef NO_0D_WHILE_2D |
| 716 | memset(before_2d_status, 0, sizeof(before_2d_status)); |
| 717 | memset(while_2d_status, 0, sizeof(while_2d_status)); |
| 718 | #endif |
| 719 | do_once = 0; |
| 720 | } |
| 721 | |
| 722 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 723 | data_addr, |
| 724 | f1a->button_data_buffer, |
| 725 | f1a->button_bitmask_size); |
| 726 | if (retval < 0) { |
| 727 | dev_err(&rmi4_data->i2c_client->dev, |
| 728 | "%s: Failed to read button data registers\n", |
| 729 | __func__); |
| 730 | return; |
| 731 | } |
| 732 | |
| 733 | data = f1a->button_data_buffer; |
| 734 | |
| 735 | for (button = 0; button < f1a->valid_button_count; button++) { |
| 736 | index = button / 8; |
| 737 | shift = button % 8; |
| 738 | status = ((data[index] >> shift) & MASK_1BIT); |
| 739 | |
| 740 | if (current_status[button] == status) |
| 741 | continue; |
| 742 | else |
| 743 | current_status[button] = status; |
| 744 | |
| 745 | dev_dbg(&rmi4_data->i2c_client->dev, |
| 746 | "%s: Button %d (code %d) ->%d\n", |
| 747 | __func__, button, |
| 748 | f1a->button_map[button], |
| 749 | status); |
| 750 | #ifdef NO_0D_WHILE_2D |
| 751 | if (rmi4_data->fingers_on_2d == false) { |
| 752 | if (status == 1) { |
| 753 | before_2d_status[button] = 1; |
| 754 | } else { |
| 755 | if (while_2d_status[button] == 1) { |
| 756 | while_2d_status[button] = 0; |
| 757 | continue; |
| 758 | } else { |
| 759 | before_2d_status[button] = 0; |
| 760 | } |
| 761 | } |
| 762 | input_report_key(rmi4_data->input_dev, |
| 763 | f1a->button_map[button], |
| 764 | status); |
| 765 | } else { |
| 766 | if (before_2d_status[button] == 1) { |
| 767 | before_2d_status[button] = 0; |
| 768 | input_report_key(rmi4_data->input_dev, |
| 769 | f1a->button_map[button], |
| 770 | status); |
| 771 | } else { |
| 772 | if (status == 1) |
| 773 | while_2d_status[button] = 1; |
| 774 | else |
| 775 | while_2d_status[button] = 0; |
| 776 | } |
| 777 | } |
| 778 | #else |
| 779 | input_report_key(rmi4_data->input_dev, |
| 780 | f1a->button_map[button], |
| 781 | status); |
| 782 | #endif |
| 783 | } |
| 784 | |
| 785 | input_sync(rmi4_data->input_dev); |
| 786 | |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * synaptics_rmi4_report_touch() |
| 792 | * |
| 793 | * Called by synaptics_rmi4_sensor_report(). |
| 794 | * |
| 795 | * This function calls the appropriate finger data reporting function |
| 796 | * based on the function handler it receives and returns the number of |
| 797 | * fingers detected. |
| 798 | */ |
| 799 | static void synaptics_rmi4_report_touch(struct synaptics_rmi4_data *rmi4_data, |
| 800 | struct synaptics_rmi4_fn *fhandler, |
| 801 | unsigned char *touch_count) |
| 802 | { |
| 803 | unsigned char touch_count_2d; |
| 804 | |
| 805 | dev_dbg(&rmi4_data->i2c_client->dev, |
| 806 | "%s: Function %02x reporting\n", |
| 807 | __func__, fhandler->fn_number); |
| 808 | |
| 809 | switch (fhandler->fn_number) { |
| 810 | case SYNAPTICS_RMI4_F11: |
| 811 | touch_count_2d = synaptics_rmi4_f11_abs_report(rmi4_data, |
| 812 | fhandler); |
| 813 | |
| 814 | *touch_count += touch_count_2d; |
| 815 | |
| 816 | if (touch_count_2d) |
| 817 | rmi4_data->fingers_on_2d = true; |
| 818 | else |
| 819 | rmi4_data->fingers_on_2d = false; |
| 820 | break; |
| 821 | |
| 822 | case SYNAPTICS_RMI4_F1A: |
| 823 | synaptics_rmi4_f1a_report(rmi4_data, fhandler); |
| 824 | break; |
| 825 | |
| 826 | default: |
| 827 | break; |
| 828 | } |
| 829 | |
| 830 | return; |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * synaptics_rmi4_sensor_report() |
| 835 | * |
| 836 | * Called by synaptics_rmi4_irq(). |
| 837 | * |
| 838 | * This function determines the interrupt source(s) from the sensor |
| 839 | * and calls synaptics_rmi4_report_touch() with the appropriate |
| 840 | * function handler for each function with valid data inputs. |
| 841 | */ |
| 842 | static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *rmi4_data) |
| 843 | { |
| 844 | int retval; |
| 845 | unsigned char touch_count = 0; |
| 846 | unsigned char intr[MAX_INTR_REGISTERS]; |
| 847 | struct synaptics_rmi4_fn *fhandler; |
| 848 | struct synaptics_rmi4_exp_fn *exp_fhandler; |
| 849 | struct synaptics_rmi4_device_info *rmi; |
| 850 | |
| 851 | rmi = &(rmi4_data->rmi4_mod_info); |
| 852 | |
| 853 | /* |
| 854 | * Get interrupt status information from F01 Data1 register to |
| 855 | * determine the source(s) that are flagging the interrupt. |
| 856 | */ |
| 857 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 858 | rmi4_data->f01_data_base_addr + 1, |
| 859 | intr, |
| 860 | rmi4_data->num_of_intr_regs); |
| 861 | if (retval < 0) |
| 862 | return retval; |
| 863 | |
| 864 | /* |
| 865 | * Traverse the function handler list and service the source(s) |
| 866 | * of the interrupt accordingly. |
| 867 | */ |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 868 | if (!list_empty(&rmi->support_fn_list)) { |
| 869 | list_for_each_entry(fhandler, &rmi->support_fn_list, link) { |
| 870 | if (fhandler->num_of_data_sources) { |
| 871 | if (fhandler->intr_mask & |
| 872 | intr[fhandler->intr_reg_num]) { |
| 873 | synaptics_rmi4_report_touch(rmi4_data, |
| 874 | fhandler, &touch_count); |
| 875 | } |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | mutex_lock(&exp_fn_list_mutex); |
| 881 | if (!list_empty(&exp_fn_list)) { |
| 882 | list_for_each_entry(exp_fhandler, &exp_fn_list, link) { |
| 883 | if (exp_fhandler->inserted && |
| 884 | (exp_fhandler->func_attn != NULL)) |
| 885 | exp_fhandler->func_attn(rmi4_data, intr[0]); |
| 886 | } |
| 887 | } |
| 888 | mutex_unlock(&exp_fn_list_mutex); |
| 889 | |
| 890 | return touch_count; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * synaptics_rmi4_irq() |
| 895 | * |
| 896 | * Called by the kernel when an interrupt occurs (when the sensor |
| 897 | * asserts the attention irq). |
| 898 | * |
| 899 | * This function is the ISR thread and handles the acquisition |
| 900 | * and the reporting of finger data when the presence of fingers |
| 901 | * is detected. |
| 902 | */ |
| 903 | static irqreturn_t synaptics_rmi4_irq(int irq, void *data) |
| 904 | { |
| 905 | struct synaptics_rmi4_data *rmi4_data = data; |
| 906 | |
| 907 | synaptics_rmi4_sensor_report(rmi4_data); |
| 908 | |
| 909 | return IRQ_HANDLED; |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * synaptics_rmi4_irq_enable() |
| 914 | * |
| 915 | * Called by synaptics_rmi4_probe() and the power management functions |
| 916 | * in this driver and also exported to other expansion Function modules |
| 917 | * such as rmi_dev. |
| 918 | * |
| 919 | * This function handles the enabling and disabling of the attention |
| 920 | * irq including the setting up of the ISR thread. |
| 921 | */ |
| 922 | static int synaptics_rmi4_irq_enable(struct synaptics_rmi4_data *rmi4_data, |
| 923 | bool enable) |
| 924 | { |
| 925 | int retval = 0; |
| 926 | unsigned char intr_status; |
| 927 | const struct synaptics_rmi4_platform_data *platform_data = |
| 928 | rmi4_data->i2c_client->dev.platform_data; |
| 929 | |
| 930 | if (enable) { |
| 931 | if (rmi4_data->irq_enabled) |
| 932 | return retval; |
| 933 | |
| 934 | /* Clear interrupts first */ |
| 935 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 936 | rmi4_data->f01_data_base_addr + 1, |
| 937 | &intr_status, |
| 938 | rmi4_data->num_of_intr_regs); |
| 939 | if (retval < 0) |
| 940 | return retval; |
| 941 | |
| 942 | retval = request_threaded_irq(rmi4_data->irq, NULL, |
| 943 | synaptics_rmi4_irq, platform_data->irq_flags, |
| 944 | DRIVER_NAME, rmi4_data); |
| 945 | if (retval < 0) { |
| 946 | dev_err(&rmi4_data->i2c_client->dev, |
| 947 | "%s: Failed to create irq thread\n", |
| 948 | __func__); |
| 949 | return retval; |
| 950 | } |
| 951 | |
| 952 | rmi4_data->irq_enabled = true; |
| 953 | } else { |
| 954 | if (rmi4_data->irq_enabled) { |
| 955 | disable_irq(rmi4_data->irq); |
| 956 | free_irq(rmi4_data->irq, rmi4_data); |
| 957 | rmi4_data->irq_enabled = false; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | return retval; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * synaptics_rmi4_f11_init() |
| 966 | * |
| 967 | * Called by synaptics_rmi4_query_device(). |
| 968 | * |
| 969 | * This funtion parses information from the Function 11 registers |
| 970 | * and determines the number of fingers supported, x and y data ranges, |
| 971 | * offset to the associated interrupt status register, interrupt bit |
| 972 | * mask, and gathers finger data acquisition capabilities from the query |
| 973 | * registers. |
| 974 | */ |
| 975 | static int synaptics_rmi4_f11_init(struct synaptics_rmi4_data *rmi4_data, |
| 976 | struct synaptics_rmi4_fn *fhandler, |
| 977 | struct synaptics_rmi4_fn_desc *fd, |
| 978 | unsigned int intr_count) |
| 979 | { |
| 980 | int retval; |
| 981 | unsigned char ii; |
| 982 | unsigned char intr_offset; |
| 983 | unsigned char abs_data_size; |
| 984 | unsigned char abs_data_blk_size; |
| 985 | unsigned char query[F11_STD_QUERY_LEN]; |
| 986 | unsigned char control[F11_STD_CTRL_LEN]; |
| 987 | |
| 988 | fhandler->fn_number = fd->fn_number; |
| 989 | fhandler->num_of_data_sources = fd->intr_src_count; |
| 990 | |
| 991 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 992 | fhandler->full_addr.query_base, |
| 993 | query, |
| 994 | sizeof(query)); |
| 995 | if (retval < 0) |
| 996 | return retval; |
| 997 | |
| 998 | /* Maximum number of fingers supported */ |
| 999 | if ((query[1] & MASK_3BIT) <= 4) |
| 1000 | fhandler->num_of_data_points = (query[1] & MASK_3BIT) + 1; |
| 1001 | else if ((query[1] & MASK_3BIT) == 5) |
| 1002 | fhandler->num_of_data_points = 10; |
| 1003 | |
| 1004 | rmi4_data->num_of_fingers = fhandler->num_of_data_points; |
| 1005 | |
| 1006 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 1007 | fhandler->full_addr.ctrl_base, |
| 1008 | control, |
| 1009 | sizeof(control)); |
| 1010 | if (retval < 0) |
| 1011 | return retval; |
| 1012 | |
| 1013 | /* Maximum x and y */ |
| 1014 | rmi4_data->sensor_max_x = ((control[6] & MASK_8BIT) << 0) | |
| 1015 | ((control[7] & MASK_4BIT) << 8); |
| 1016 | rmi4_data->sensor_max_y = ((control[8] & MASK_8BIT) << 0) | |
| 1017 | ((control[9] & MASK_4BIT) << 8); |
| 1018 | dev_dbg(&rmi4_data->i2c_client->dev, |
| 1019 | "%s: Function %02x max x = %d max y = %d\n", |
| 1020 | __func__, fhandler->fn_number, |
| 1021 | rmi4_data->sensor_max_x, |
| 1022 | rmi4_data->sensor_max_y); |
| 1023 | |
| 1024 | fhandler->intr_reg_num = (intr_count + 7) / 8; |
| 1025 | if (fhandler->intr_reg_num != 0) |
| 1026 | fhandler->intr_reg_num -= 1; |
| 1027 | |
| 1028 | /* Set an enable bit for each data source */ |
| 1029 | intr_offset = intr_count % 8; |
| 1030 | fhandler->intr_mask = 0; |
| 1031 | for (ii = intr_offset; |
| 1032 | ii < ((fd->intr_src_count & MASK_3BIT) + |
| 1033 | intr_offset); |
| 1034 | ii++) |
| 1035 | fhandler->intr_mask |= 1 << ii; |
| 1036 | |
| 1037 | abs_data_size = query[5] & MASK_2BIT; |
| 1038 | abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0)); |
| 1039 | fhandler->size_of_data_register_block = abs_data_blk_size; |
| 1040 | |
| 1041 | return retval; |
| 1042 | } |
| 1043 | |
| 1044 | static int synaptics_rmi4_f1a_alloc_mem(struct synaptics_rmi4_data *rmi4_data, |
| 1045 | struct synaptics_rmi4_fn *fhandler) |
| 1046 | { |
| 1047 | int retval; |
| 1048 | struct synaptics_rmi4_f1a_handle *f1a; |
| 1049 | |
| 1050 | f1a = kzalloc(sizeof(*f1a), GFP_KERNEL); |
| 1051 | if (!f1a) { |
| 1052 | dev_err(&rmi4_data->i2c_client->dev, |
| 1053 | "%s: Failed to alloc mem for function handle\n", |
| 1054 | __func__); |
| 1055 | return -ENOMEM; |
| 1056 | } |
| 1057 | |
| 1058 | fhandler->data = (void *)f1a; |
| 1059 | |
| 1060 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 1061 | fhandler->full_addr.query_base, |
| 1062 | f1a->button_query.data, |
| 1063 | sizeof(f1a->button_query.data)); |
| 1064 | if (retval < 0) { |
| 1065 | dev_err(&rmi4_data->i2c_client->dev, |
| 1066 | "%s: Failed to read query registers\n", |
| 1067 | __func__); |
| 1068 | return retval; |
| 1069 | } |
| 1070 | |
| 1071 | f1a->button_count = f1a->button_query.max_button_count + 1; |
| 1072 | f1a->button_bitmask_size = (f1a->button_count + 7) / 8; |
| 1073 | |
| 1074 | f1a->button_data_buffer = kcalloc(f1a->button_bitmask_size, |
| 1075 | sizeof(*(f1a->button_data_buffer)), GFP_KERNEL); |
| 1076 | if (!f1a->button_data_buffer) { |
| 1077 | dev_err(&rmi4_data->i2c_client->dev, |
| 1078 | "%s: Failed to alloc mem for data buffer\n", |
| 1079 | __func__); |
| 1080 | return -ENOMEM; |
| 1081 | } |
| 1082 | |
| 1083 | f1a->button_map = kcalloc(f1a->button_count, |
| 1084 | sizeof(*(f1a->button_map)), GFP_KERNEL); |
| 1085 | if (!f1a->button_map) { |
| 1086 | dev_err(&rmi4_data->i2c_client->dev, |
| 1087 | "%s: Failed to alloc mem for button map\n", |
| 1088 | __func__); |
| 1089 | return -ENOMEM; |
| 1090 | } |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | static int synaptics_rmi4_capacitance_button_map( |
| 1096 | struct synaptics_rmi4_data *rmi4_data, |
| 1097 | struct synaptics_rmi4_fn *fhandler) |
| 1098 | { |
| 1099 | unsigned char ii; |
| 1100 | struct synaptics_rmi4_f1a_handle *f1a = fhandler->data; |
| 1101 | const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board; |
| 1102 | |
| 1103 | if (!pdata->capacitance_button_map) { |
| 1104 | dev_err(&rmi4_data->i2c_client->dev, |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1105 | "%s: capacitance_button_map is" \ |
| 1106 | "NULL in board file\n", |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1107 | __func__); |
| 1108 | return -ENODEV; |
| 1109 | } else if (!pdata->capacitance_button_map->map) { |
| 1110 | dev_err(&rmi4_data->i2c_client->dev, |
| 1111 | "%s: Button map is missing in board file\n", |
| 1112 | __func__); |
| 1113 | return -ENODEV; |
| 1114 | } else { |
| 1115 | if (pdata->capacitance_button_map->nbuttons != |
| 1116 | f1a->button_count) { |
| 1117 | f1a->valid_button_count = min(f1a->button_count, |
| 1118 | pdata->capacitance_button_map->nbuttons); |
| 1119 | } else { |
| 1120 | f1a->valid_button_count = f1a->button_count; |
| 1121 | } |
| 1122 | |
| 1123 | for (ii = 0; ii < f1a->valid_button_count; ii++) |
| 1124 | f1a->button_map[ii] = |
| 1125 | pdata->capacitance_button_map->map[ii]; |
| 1126 | } |
| 1127 | |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | static void synaptics_rmi4_f1a_kfree(struct synaptics_rmi4_fn *fhandler) |
| 1132 | { |
| 1133 | struct synaptics_rmi4_f1a_handle *f1a = fhandler->data; |
| 1134 | |
| 1135 | if (f1a) { |
| 1136 | kfree(f1a->button_data_buffer); |
| 1137 | kfree(f1a->button_map); |
| 1138 | kfree(f1a); |
| 1139 | fhandler->data = NULL; |
| 1140 | } |
| 1141 | |
| 1142 | return; |
| 1143 | } |
| 1144 | |
| 1145 | static int synaptics_rmi4_f1a_init(struct synaptics_rmi4_data *rmi4_data, |
| 1146 | struct synaptics_rmi4_fn *fhandler, |
| 1147 | struct synaptics_rmi4_fn_desc *fd, |
| 1148 | unsigned int intr_count) |
| 1149 | { |
| 1150 | int retval; |
| 1151 | unsigned char ii; |
| 1152 | unsigned short intr_offset; |
| 1153 | |
| 1154 | fhandler->fn_number = fd->fn_number; |
| 1155 | fhandler->num_of_data_sources = fd->intr_src_count; |
| 1156 | |
| 1157 | fhandler->intr_reg_num = (intr_count + 7) / 8; |
| 1158 | if (fhandler->intr_reg_num != 0) |
| 1159 | fhandler->intr_reg_num -= 1; |
| 1160 | |
| 1161 | /* Set an enable bit for each data source */ |
| 1162 | intr_offset = intr_count % 8; |
| 1163 | fhandler->intr_mask = 0; |
| 1164 | for (ii = intr_offset; |
| 1165 | ii < ((fd->intr_src_count & MASK_3BIT) + |
| 1166 | intr_offset); |
| 1167 | ii++) |
| 1168 | fhandler->intr_mask |= 1 << ii; |
| 1169 | |
| 1170 | retval = synaptics_rmi4_f1a_alloc_mem(rmi4_data, fhandler); |
| 1171 | if (retval < 0) |
| 1172 | goto error_exit; |
| 1173 | |
| 1174 | retval = synaptics_rmi4_capacitance_button_map(rmi4_data, fhandler); |
| 1175 | if (retval < 0) |
| 1176 | goto error_exit; |
| 1177 | |
| 1178 | rmi4_data->button_0d_enabled = 1; |
| 1179 | |
| 1180 | return 0; |
| 1181 | |
| 1182 | error_exit: |
| 1183 | synaptics_rmi4_f1a_kfree(fhandler); |
| 1184 | |
| 1185 | return retval; |
| 1186 | } |
| 1187 | |
| 1188 | static int synaptics_rmi4_alloc_fh(struct synaptics_rmi4_fn **fhandler, |
| 1189 | struct synaptics_rmi4_fn_desc *rmi_fd, int page_number) |
| 1190 | { |
| 1191 | *fhandler = kmalloc(sizeof(**fhandler), GFP_KERNEL); |
| 1192 | if (!(*fhandler)) |
| 1193 | return -ENOMEM; |
| 1194 | |
| 1195 | (*fhandler)->full_addr.data_base = |
| 1196 | (rmi_fd->data_base_addr | |
| 1197 | (page_number << 8)); |
| 1198 | (*fhandler)->full_addr.ctrl_base = |
| 1199 | (rmi_fd->ctrl_base_addr | |
| 1200 | (page_number << 8)); |
| 1201 | (*fhandler)->full_addr.cmd_base = |
| 1202 | (rmi_fd->cmd_base_addr | |
| 1203 | (page_number << 8)); |
| 1204 | (*fhandler)->full_addr.query_base = |
| 1205 | (rmi_fd->query_base_addr | |
| 1206 | (page_number << 8)); |
| 1207 | |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1211 | |
| 1212 | /** |
| 1213 | * synaptics_rmi4_query_device_info() |
| 1214 | * |
| 1215 | * Called by synaptics_rmi4_query_device(). |
| 1216 | * |
| 1217 | */ |
| 1218 | static int synaptics_rmi4_query_device_info( |
| 1219 | struct synaptics_rmi4_data *rmi4_data) |
| 1220 | { |
| 1221 | int retval; |
| 1222 | unsigned char f01_query[F01_STD_QUERY_LEN]; |
| 1223 | struct synaptics_rmi4_device_info *rmi = &(rmi4_data->rmi4_mod_info); |
| 1224 | |
| 1225 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 1226 | rmi4_data->f01_query_base_addr, |
| 1227 | f01_query, |
| 1228 | sizeof(f01_query)); |
| 1229 | if (retval < 0) |
| 1230 | return retval; |
| 1231 | |
| 1232 | /* RMI Version 4.0 currently supported */ |
| 1233 | rmi->version_major = 4; |
| 1234 | rmi->version_minor = 0; |
| 1235 | |
| 1236 | rmi->manufacturer_id = f01_query[0]; |
| 1237 | rmi->product_props = f01_query[1]; |
| 1238 | rmi->product_info[0] = f01_query[2] & MASK_7BIT; |
| 1239 | rmi->product_info[1] = f01_query[3] & MASK_7BIT; |
| 1240 | rmi->date_code[0] = f01_query[4] & MASK_5BIT; |
| 1241 | rmi->date_code[1] = f01_query[5] & MASK_4BIT; |
| 1242 | rmi->date_code[2] = f01_query[6] & MASK_5BIT; |
| 1243 | rmi->tester_id = ((f01_query[7] & MASK_7BIT) << 8) | |
| 1244 | (f01_query[8] & MASK_7BIT); |
| 1245 | rmi->serial_number = ((f01_query[9] & MASK_7BIT) << 8) | |
| 1246 | (f01_query[10] & MASK_7BIT); |
| 1247 | memcpy(rmi->product_id_string, &f01_query[11], 10); |
| 1248 | |
| 1249 | if (rmi->manufacturer_id != 1) { |
| 1250 | dev_err(&rmi4_data->i2c_client->dev, |
| 1251 | "%s: Non-Synaptics device found, manufacturer ID = %d\n", |
| 1252 | __func__, rmi->manufacturer_id); |
| 1253 | } |
| 1254 | |
| 1255 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 1256 | rmi4_data->f01_query_base_addr + F01_BUID_ID_OFFSET, |
| 1257 | rmi->build_id, |
| 1258 | sizeof(rmi->build_id)); |
| 1259 | if (retval < 0) { |
| 1260 | dev_err(&rmi4_data->i2c_client->dev, |
| 1261 | "%s: Failed to read firmware build id (code %d)\n", |
| 1262 | __func__, retval); |
| 1263 | return retval; |
| 1264 | } |
| 1265 | return retval; |
| 1266 | } |
| 1267 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1268 | /** |
| 1269 | * synaptics_rmi4_query_device() |
| 1270 | * |
| 1271 | * Called by synaptics_rmi4_probe(). |
| 1272 | * |
| 1273 | * This funtion scans the page description table, records the offsets |
| 1274 | * to the register types of Function $01, sets up the function handlers |
| 1275 | * for Function $11 and Function $12, determines the number of interrupt |
| 1276 | * sources from the sensor, adds valid Functions with data inputs to the |
| 1277 | * Function linked list, parses information from the query registers of |
| 1278 | * Function $01, and enables the interrupt sources from the valid Functions |
| 1279 | * with data inputs. |
| 1280 | */ |
| 1281 | static int synaptics_rmi4_query_device(struct synaptics_rmi4_data *rmi4_data) |
| 1282 | { |
| 1283 | int retval; |
| 1284 | unsigned char ii; |
| 1285 | unsigned char page_number; |
| 1286 | unsigned char intr_count = 0; |
| 1287 | unsigned char data_sources = 0; |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1288 | unsigned short pdt_entry_addr; |
| 1289 | unsigned short intr_addr; |
| 1290 | struct synaptics_rmi4_f01_device_status status; |
| 1291 | struct synaptics_rmi4_fn_desc rmi_fd; |
| 1292 | struct synaptics_rmi4_fn *fhandler; |
| 1293 | struct synaptics_rmi4_device_info *rmi; |
| 1294 | |
| 1295 | rmi = &(rmi4_data->rmi4_mod_info); |
| 1296 | |
| 1297 | INIT_LIST_HEAD(&rmi->support_fn_list); |
| 1298 | |
| 1299 | /* Scan the page description tables of the pages to service */ |
| 1300 | for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) { |
| 1301 | for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END; |
| 1302 | pdt_entry_addr -= PDT_ENTRY_SIZE) { |
| 1303 | pdt_entry_addr |= (page_number << 8); |
| 1304 | |
| 1305 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 1306 | pdt_entry_addr, |
| 1307 | (unsigned char *)&rmi_fd, |
| 1308 | sizeof(rmi_fd)); |
| 1309 | if (retval < 0) |
| 1310 | return retval; |
| 1311 | |
| 1312 | fhandler = NULL; |
| 1313 | |
| 1314 | if (rmi_fd.fn_number == 0) { |
| 1315 | dev_dbg(&rmi4_data->i2c_client->dev, |
| 1316 | "%s: Reached end of PDT\n", |
| 1317 | __func__); |
| 1318 | break; |
| 1319 | } |
| 1320 | |
| 1321 | dev_dbg(&rmi4_data->i2c_client->dev, |
| 1322 | "%s: F%02x found (page %d)\n", |
| 1323 | __func__, rmi_fd.fn_number, |
| 1324 | page_number); |
| 1325 | |
| 1326 | switch (rmi_fd.fn_number) { |
| 1327 | case SYNAPTICS_RMI4_F01: |
| 1328 | rmi4_data->f01_query_base_addr = |
| 1329 | rmi_fd.query_base_addr; |
| 1330 | rmi4_data->f01_ctrl_base_addr = |
| 1331 | rmi_fd.ctrl_base_addr; |
| 1332 | rmi4_data->f01_data_base_addr = |
| 1333 | rmi_fd.data_base_addr; |
| 1334 | rmi4_data->f01_cmd_base_addr = |
| 1335 | rmi_fd.cmd_base_addr; |
| 1336 | |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1337 | retval = |
| 1338 | synaptics_rmi4_query_device_info(rmi4_data); |
| 1339 | if (retval < 0) |
| 1340 | return retval; |
| 1341 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1342 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 1343 | rmi4_data->f01_data_base_addr, |
| 1344 | status.data, |
| 1345 | sizeof(status.data)); |
| 1346 | if (retval < 0) |
| 1347 | return retval; |
| 1348 | |
| 1349 | if (status.flash_prog == 1) { |
| 1350 | pr_notice("%s: In flash prog mode, status = 0x%02x\n", |
| 1351 | __func__, |
| 1352 | status.status_code); |
| 1353 | goto flash_prog_mode; |
| 1354 | } |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1355 | break; |
| 1356 | |
| 1357 | case SYNAPTICS_RMI4_F34: |
| 1358 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 1359 | rmi_fd.ctrl_base_addr, |
| 1360 | rmi->config_id, |
| 1361 | sizeof(rmi->config_id)); |
| 1362 | if (retval < 0) |
| 1363 | return retval; |
| 1364 | break; |
| 1365 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1366 | case SYNAPTICS_RMI4_F11: |
| 1367 | if (rmi_fd.intr_src_count == 0) |
| 1368 | break; |
| 1369 | |
| 1370 | retval = synaptics_rmi4_alloc_fh(&fhandler, |
| 1371 | &rmi_fd, page_number); |
| 1372 | if (retval < 0) { |
| 1373 | dev_err(&rmi4_data->i2c_client->dev, |
| 1374 | "%s: Failed to alloc for F%d\n", |
| 1375 | __func__, |
| 1376 | rmi_fd.fn_number); |
| 1377 | return retval; |
| 1378 | } |
| 1379 | |
| 1380 | retval = synaptics_rmi4_f11_init(rmi4_data, |
| 1381 | fhandler, &rmi_fd, intr_count); |
| 1382 | if (retval < 0) |
| 1383 | return retval; |
| 1384 | break; |
| 1385 | |
| 1386 | case SYNAPTICS_RMI4_F1A: |
| 1387 | if (rmi_fd.intr_src_count == 0) |
| 1388 | break; |
| 1389 | |
| 1390 | retval = synaptics_rmi4_alloc_fh(&fhandler, |
| 1391 | &rmi_fd, page_number); |
| 1392 | if (retval < 0) { |
| 1393 | dev_err(&rmi4_data->i2c_client->dev, |
| 1394 | "%s: Failed to alloc for F%d\n", |
| 1395 | __func__, |
| 1396 | rmi_fd.fn_number); |
| 1397 | return retval; |
| 1398 | } |
| 1399 | |
| 1400 | retval = synaptics_rmi4_f1a_init(rmi4_data, |
| 1401 | fhandler, &rmi_fd, intr_count); |
| 1402 | if (retval < 0) |
| 1403 | return retval; |
| 1404 | break; |
| 1405 | } |
| 1406 | |
| 1407 | /* Accumulate the interrupt count */ |
| 1408 | intr_count += (rmi_fd.intr_src_count & MASK_3BIT); |
| 1409 | |
| 1410 | if (fhandler && rmi_fd.intr_src_count) { |
| 1411 | list_add_tail(&fhandler->link, |
| 1412 | &rmi->support_fn_list); |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | flash_prog_mode: |
| 1418 | rmi4_data->num_of_intr_regs = (intr_count + 7) / 8; |
| 1419 | dev_dbg(&rmi4_data->i2c_client->dev, |
| 1420 | "%s: Number of interrupt registers = %d\n", |
| 1421 | __func__, rmi4_data->num_of_intr_regs); |
| 1422 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1423 | memset(rmi4_data->intr_mask, 0x00, sizeof(rmi4_data->intr_mask)); |
| 1424 | |
| 1425 | /* |
| 1426 | * Map out the interrupt bit masks for the interrupt sources |
| 1427 | * from the registered function handlers. |
| 1428 | */ |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1429 | if (!list_empty(&rmi->support_fn_list)) { |
| 1430 | list_for_each_entry(fhandler, &rmi->support_fn_list, link) |
| 1431 | data_sources += fhandler->num_of_data_sources; |
| 1432 | } |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1433 | if (data_sources) { |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1434 | if (!list_empty(&rmi->support_fn_list)) { |
| 1435 | list_for_each_entry(fhandler, |
| 1436 | &rmi->support_fn_list, link) { |
| 1437 | if (fhandler->num_of_data_sources) { |
| 1438 | rmi4_data->intr_mask[fhandler->intr_reg_num] |= |
| 1439 | fhandler->intr_mask; |
| 1440 | } |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /* Enable the interrupt sources */ |
| 1446 | for (ii = 0; ii < rmi4_data->num_of_intr_regs; ii++) { |
| 1447 | if (rmi4_data->intr_mask[ii] != 0x00) { |
| 1448 | dev_dbg(&rmi4_data->i2c_client->dev, |
| 1449 | "%s: Interrupt enable mask %d = 0x%02x\n", |
| 1450 | __func__, ii, rmi4_data->intr_mask[ii]); |
| 1451 | intr_addr = rmi4_data->f01_ctrl_base_addr + 1 + ii; |
| 1452 | retval = synaptics_rmi4_i2c_write(rmi4_data, |
| 1453 | intr_addr, |
| 1454 | &(rmi4_data->intr_mask[ii]), |
| 1455 | sizeof(rmi4_data->intr_mask[ii])); |
| 1456 | if (retval < 0) |
| 1457 | return retval; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data) |
| 1465 | { |
| 1466 | int retval; |
| 1467 | unsigned char command = 0x01; |
| 1468 | struct synaptics_rmi4_fn *fhandler; |
| 1469 | struct synaptics_rmi4_device_info *rmi; |
| 1470 | |
| 1471 | rmi = &(rmi4_data->rmi4_mod_info); |
| 1472 | |
| 1473 | retval = synaptics_rmi4_i2c_write(rmi4_data, |
| 1474 | rmi4_data->f01_cmd_base_addr, |
| 1475 | &command, |
| 1476 | sizeof(command)); |
| 1477 | if (retval < 0) { |
| 1478 | dev_err(&rmi4_data->i2c_client->dev, |
| 1479 | "%s: Failed to issue reset command, error = %d\n", |
| 1480 | __func__, retval); |
| 1481 | return retval; |
| 1482 | } |
| 1483 | |
| 1484 | msleep(100); |
| 1485 | |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1486 | if (!list_empty(&rmi->support_fn_list)) { |
| 1487 | list_for_each_entry(fhandler, &rmi->support_fn_list, link) { |
| 1488 | if (fhandler->fn_number == SYNAPTICS_RMI4_F1A) |
| 1489 | synaptics_rmi4_f1a_kfree(fhandler); |
| 1490 | else |
| 1491 | kfree(fhandler->data); |
| 1492 | kfree(fhandler); |
| 1493 | } |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | retval = synaptics_rmi4_query_device(rmi4_data); |
| 1497 | if (retval < 0) { |
| 1498 | dev_err(&rmi4_data->i2c_client->dev, |
| 1499 | "%s: Failed to query device\n", |
| 1500 | __func__); |
| 1501 | return retval; |
| 1502 | } |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | /** |
| 1508 | * synaptics_rmi4_detection_work() |
| 1509 | * |
| 1510 | * Called by the kernel at the scheduled time. |
| 1511 | * |
| 1512 | * This function is a self-rearming work thread that checks for the |
| 1513 | * insertion and removal of other expansion Function modules such as |
| 1514 | * rmi_dev and calls their initialization and removal callback functions |
| 1515 | * accordingly. |
| 1516 | */ |
| 1517 | static void synaptics_rmi4_detection_work(struct work_struct *work) |
| 1518 | { |
| 1519 | struct synaptics_rmi4_exp_fn *exp_fhandler, *next_list_entry; |
| 1520 | struct synaptics_rmi4_data *rmi4_data = |
| 1521 | container_of(work, struct synaptics_rmi4_data, |
| 1522 | det_work.work); |
| 1523 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1524 | mutex_lock(&exp_fn_list_mutex); |
| 1525 | if (!list_empty(&exp_fn_list)) { |
| 1526 | list_for_each_entry_safe(exp_fhandler, |
| 1527 | next_list_entry, |
| 1528 | &exp_fn_list, |
| 1529 | link) { |
| 1530 | if ((exp_fhandler->func_init != NULL) && |
| 1531 | (exp_fhandler->inserted == false)) { |
| 1532 | exp_fhandler->func_init(rmi4_data); |
| 1533 | exp_fhandler->inserted = true; |
| 1534 | } else if ((exp_fhandler->func_init == NULL) && |
| 1535 | (exp_fhandler->inserted == true)) { |
| 1536 | exp_fhandler->func_remove(rmi4_data); |
| 1537 | list_del(&exp_fhandler->link); |
| 1538 | kfree(exp_fhandler); |
| 1539 | } |
| 1540 | } |
| 1541 | } |
| 1542 | mutex_unlock(&exp_fn_list_mutex); |
| 1543 | |
| 1544 | return; |
| 1545 | } |
| 1546 | |
| 1547 | /** |
| 1548 | * synaptics_rmi4_new_function() |
| 1549 | * |
| 1550 | * Called by other expansion Function modules in their module init and |
| 1551 | * module exit functions. |
| 1552 | * |
| 1553 | * This function is used by other expansion Function modules such as |
| 1554 | * rmi_dev to register themselves with the driver by providing their |
| 1555 | * initialization and removal callback function pointers so that they |
| 1556 | * can be inserted or removed dynamically at module init and exit times, |
| 1557 | * respectively. |
| 1558 | */ |
| 1559 | void synaptics_rmi4_new_function(enum exp_fn fn_type, bool insert, |
| 1560 | int (*func_init)(struct synaptics_rmi4_data *rmi4_data), |
| 1561 | void (*func_remove)(struct synaptics_rmi4_data *rmi4_data), |
| 1562 | void (*func_attn)(struct synaptics_rmi4_data *rmi4_data, |
| 1563 | unsigned char intr_mask)) |
| 1564 | { |
| 1565 | struct synaptics_rmi4_exp_fn *exp_fhandler; |
| 1566 | |
| 1567 | if (!exp_fn_inited) { |
| 1568 | mutex_init(&exp_fn_list_mutex); |
| 1569 | INIT_LIST_HEAD(&exp_fn_list); |
| 1570 | exp_fn_inited = 1; |
| 1571 | } |
| 1572 | |
| 1573 | mutex_lock(&exp_fn_list_mutex); |
| 1574 | if (insert) { |
| 1575 | exp_fhandler = kzalloc(sizeof(*exp_fhandler), GFP_KERNEL); |
| 1576 | if (!exp_fhandler) { |
| 1577 | pr_err("%s: Failed to alloc mem for expansion function\n", |
| 1578 | __func__); |
| 1579 | goto exit; |
| 1580 | } |
| 1581 | exp_fhandler->fn_type = fn_type; |
| 1582 | exp_fhandler->func_init = func_init; |
| 1583 | exp_fhandler->func_attn = func_attn; |
| 1584 | exp_fhandler->func_remove = func_remove; |
| 1585 | exp_fhandler->inserted = false; |
| 1586 | list_add_tail(&exp_fhandler->link, &exp_fn_list); |
| 1587 | } else { |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1588 | if (!list_empty(&exp_fn_list)) { |
| 1589 | list_for_each_entry(exp_fhandler, &exp_fn_list, link) { |
| 1590 | if (exp_fhandler->func_init == func_init) { |
| 1591 | exp_fhandler->inserted = false; |
| 1592 | exp_fhandler->func_init = NULL; |
| 1593 | exp_fhandler->func_attn = NULL; |
| 1594 | goto exit; |
| 1595 | } |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | exit: |
| 1601 | mutex_unlock(&exp_fn_list_mutex); |
| 1602 | |
| 1603 | return; |
| 1604 | } |
| 1605 | EXPORT_SYMBOL(synaptics_rmi4_new_function); |
| 1606 | |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 1607 | |
| 1608 | static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA) |
| 1609 | { |
| 1610 | return (regulator_count_voltages(reg) > 0) ? |
| 1611 | regulator_set_optimum_mode(reg, load_uA) : 0; |
| 1612 | } |
| 1613 | |
| 1614 | static int synaptics_rmi4_regulator_configure(struct synaptics_rmi4_data |
| 1615 | *rmi4_data, bool on) |
| 1616 | { |
| 1617 | int retval; |
| 1618 | |
| 1619 | if (on == false) |
| 1620 | goto hw_shutdown; |
| 1621 | |
| 1622 | if (rmi4_data->board->regulator_en) { |
| 1623 | rmi4_data->vdd = regulator_get(&rmi4_data->i2c_client->dev, |
| 1624 | "vdd"); |
| 1625 | if (IS_ERR(rmi4_data->vdd)) { |
| 1626 | dev_err(&rmi4_data->i2c_client->dev, |
| 1627 | "%s: Failed to get vdd regulator\n", |
| 1628 | __func__); |
| 1629 | return PTR_ERR(rmi4_data->vdd); |
| 1630 | } |
| 1631 | |
| 1632 | if (regulator_count_voltages(rmi4_data->vdd) > 0) { |
| 1633 | retval = regulator_set_voltage(rmi4_data->vdd, |
| 1634 | RMI4_VTG_MIN_UV, RMI4_VTG_MAX_UV); |
| 1635 | if (retval) { |
| 1636 | dev_err(&rmi4_data->i2c_client->dev, |
| 1637 | "regulator set_vtg failed retval=%d\n", |
| 1638 | retval); |
| 1639 | goto err_set_vtg_vdd; |
| 1640 | } |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | if (rmi4_data->board->i2c_pull_up) { |
| 1645 | rmi4_data->vcc_i2c = regulator_get(&rmi4_data->i2c_client->dev, |
| 1646 | "vcc_i2c"); |
| 1647 | if (IS_ERR(rmi4_data->vcc_i2c)) { |
| 1648 | dev_err(&rmi4_data->i2c_client->dev, |
| 1649 | "%s: Failed to get i2c regulator\n", |
| 1650 | __func__); |
| 1651 | retval = PTR_ERR(rmi4_data->vcc_i2c); |
| 1652 | goto err_get_vtg_i2c; |
| 1653 | } |
| 1654 | |
| 1655 | if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0) { |
| 1656 | retval = regulator_set_voltage(rmi4_data->vcc_i2c, |
| 1657 | RMI4_I2C_VTG_MIN_UV, RMI4_I2C_VTG_MAX_UV); |
| 1658 | if (retval) { |
| 1659 | dev_err(&rmi4_data->i2c_client->dev, |
| 1660 | "reg set i2c vtg failed retval=%d\n", |
| 1661 | retval); |
| 1662 | goto err_set_vtg_i2c; |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | err_set_vtg_i2c: |
| 1668 | if (rmi4_data->board->i2c_pull_up) |
| 1669 | regulator_put(rmi4_data->vcc_i2c); |
| 1670 | err_get_vtg_i2c: |
| 1671 | if (rmi4_data->board->regulator_en) |
| 1672 | if (regulator_count_voltages(rmi4_data->vdd) > 0) |
| 1673 | regulator_set_voltage(rmi4_data->vdd, 0, |
| 1674 | RMI4_VTG_MAX_UV); |
| 1675 | err_set_vtg_vdd: |
| 1676 | if (rmi4_data->board->regulator_en) |
| 1677 | regulator_put(rmi4_data->vdd); |
| 1678 | return retval; |
| 1679 | |
| 1680 | hw_shutdown: |
| 1681 | if (rmi4_data->board->regulator_en) { |
| 1682 | if (regulator_count_voltages(rmi4_data->vdd) > 0) |
| 1683 | regulator_set_voltage(rmi4_data->vdd, 0, |
| 1684 | RMI4_VTG_MAX_UV); |
| 1685 | regulator_put(rmi4_data->vdd); |
| 1686 | } |
| 1687 | if (rmi4_data->board->i2c_pull_up) { |
| 1688 | if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0) |
| 1689 | regulator_set_voltage(rmi4_data->vcc_i2c, 0, |
| 1690 | RMI4_I2C_VTG_MAX_UV); |
| 1691 | regulator_put(rmi4_data->vcc_i2c); |
| 1692 | } |
| 1693 | return 0; |
| 1694 | }; |
| 1695 | |
| 1696 | static int synaptics_rmi4_power_on(struct synaptics_rmi4_data *rmi4_data, |
| 1697 | bool on) { |
| 1698 | int retval; |
| 1699 | |
| 1700 | if (on == false) |
| 1701 | goto power_off; |
| 1702 | |
| 1703 | if (rmi4_data->board->regulator_en) { |
| 1704 | retval = reg_set_optimum_mode_check(rmi4_data->vdd, |
| 1705 | RMI4_ACTIVE_LOAD_UA); |
| 1706 | if (retval < 0) { |
| 1707 | dev_err(&rmi4_data->i2c_client->dev, |
| 1708 | "Regulator vdd set_opt failed rc=%d\n", |
| 1709 | retval); |
| 1710 | return retval; |
| 1711 | } |
| 1712 | |
| 1713 | retval = regulator_enable(rmi4_data->vdd); |
| 1714 | if (retval) { |
| 1715 | dev_err(&rmi4_data->i2c_client->dev, |
| 1716 | "Regulator vdd enable failed rc=%d\n", |
| 1717 | retval); |
| 1718 | goto error_reg_en_vdd; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | if (rmi4_data->board->i2c_pull_up) { |
| 1723 | retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c, |
| 1724 | RMI4_I2C_LOAD_UA); |
| 1725 | if (retval < 0) { |
| 1726 | dev_err(&rmi4_data->i2c_client->dev, |
| 1727 | "Regulator vcc_i2c set_opt failed rc=%d\n", |
| 1728 | retval); |
| 1729 | goto error_reg_opt_i2c; |
| 1730 | } |
| 1731 | |
| 1732 | retval = regulator_enable(rmi4_data->vcc_i2c); |
| 1733 | if (retval) { |
| 1734 | dev_err(&rmi4_data->i2c_client->dev, |
| 1735 | "Regulator vcc_i2c enable failed rc=%d\n", |
| 1736 | retval); |
| 1737 | goto error_reg_en_vcc_i2c; |
| 1738 | } |
| 1739 | } |
| 1740 | return 0; |
| 1741 | |
| 1742 | error_reg_en_vcc_i2c: |
| 1743 | if (rmi4_data->board->i2c_pull_up) |
| 1744 | reg_set_optimum_mode_check(rmi4_data->vdd, 0); |
| 1745 | error_reg_opt_i2c: |
| 1746 | if (rmi4_data->board->regulator_en) |
| 1747 | regulator_disable(rmi4_data->vdd); |
| 1748 | error_reg_en_vdd: |
| 1749 | if (rmi4_data->board->regulator_en) |
| 1750 | reg_set_optimum_mode_check(rmi4_data->vdd, 0); |
| 1751 | return retval; |
| 1752 | |
| 1753 | power_off: |
| 1754 | if (rmi4_data->board->regulator_en) { |
| 1755 | reg_set_optimum_mode_check(rmi4_data->vdd, 0); |
| 1756 | regulator_disable(rmi4_data->vdd); |
| 1757 | } |
| 1758 | if (rmi4_data->board->i2c_pull_up) { |
| 1759 | reg_set_optimum_mode_check(rmi4_data->vcc_i2c, 0); |
| 1760 | regulator_disable(rmi4_data->vcc_i2c); |
| 1761 | } |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1765 | /** |
| 1766 | * synaptics_rmi4_probe() |
| 1767 | * |
| 1768 | * Called by the kernel when an association with an I2C device of the |
| 1769 | * same name is made (after doing i2c_add_driver). |
| 1770 | * |
| 1771 | * This funtion allocates and initializes the resources for the driver |
| 1772 | * as an input driver, turns on the power to the sensor, queries the |
| 1773 | * sensor for its supported Functions and characteristics, registers |
| 1774 | * the driver to the input subsystem, sets up the interrupt, handles |
| 1775 | * the registration of the early_suspend and late_resume functions, |
| 1776 | * and creates a work queue for detection of other expansion Function |
| 1777 | * modules. |
| 1778 | */ |
| 1779 | static int __devinit synaptics_rmi4_probe(struct i2c_client *client, |
| 1780 | const struct i2c_device_id *dev_id) |
| 1781 | { |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 1782 | int retval = 0; |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1783 | unsigned char ii; |
| 1784 | unsigned char attr_count; |
| 1785 | struct synaptics_rmi4_f1a_handle *f1a; |
| 1786 | struct synaptics_rmi4_fn *fhandler; |
| 1787 | struct synaptics_rmi4_data *rmi4_data; |
| 1788 | struct synaptics_rmi4_device_info *rmi; |
| 1789 | const struct synaptics_rmi4_platform_data *platform_data = |
| 1790 | client->dev.platform_data; |
| 1791 | |
| 1792 | if (!i2c_check_functionality(client->adapter, |
| 1793 | I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 1794 | dev_err(&client->dev, |
| 1795 | "%s: SMBus byte data not supported\n", |
| 1796 | __func__); |
| 1797 | return -EIO; |
| 1798 | } |
| 1799 | |
| 1800 | if (!platform_data) { |
| 1801 | dev_err(&client->dev, |
| 1802 | "%s: No platform data found\n", |
| 1803 | __func__); |
| 1804 | return -EINVAL; |
| 1805 | } |
| 1806 | |
| 1807 | rmi4_data = kzalloc(sizeof(*rmi4_data) * 2, GFP_KERNEL); |
| 1808 | if (!rmi4_data) { |
| 1809 | dev_err(&client->dev, |
| 1810 | "%s: Failed to alloc mem for rmi4_data\n", |
| 1811 | __func__); |
| 1812 | return -ENOMEM; |
| 1813 | } |
| 1814 | |
| 1815 | rmi = &(rmi4_data->rmi4_mod_info); |
| 1816 | |
| 1817 | rmi4_data->input_dev = input_allocate_device(); |
| 1818 | if (rmi4_data->input_dev == NULL) { |
| 1819 | dev_err(&client->dev, |
| 1820 | "%s: Failed to allocate input device\n", |
| 1821 | __func__); |
| 1822 | retval = -ENOMEM; |
| 1823 | goto err_input_device; |
| 1824 | } |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1825 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1826 | rmi4_data->i2c_client = client; |
| 1827 | rmi4_data->current_page = MASK_8BIT; |
| 1828 | rmi4_data->board = platform_data; |
| 1829 | rmi4_data->touch_stopped = false; |
| 1830 | rmi4_data->sensor_sleep = false; |
| 1831 | rmi4_data->irq_enabled = false; |
| 1832 | |
| 1833 | rmi4_data->i2c_read = synaptics_rmi4_i2c_read; |
| 1834 | rmi4_data->i2c_write = synaptics_rmi4_i2c_write; |
| 1835 | rmi4_data->irq_enable = synaptics_rmi4_irq_enable; |
| 1836 | rmi4_data->reset_device = synaptics_rmi4_reset_device; |
| 1837 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1838 | rmi4_data->input_dev->name = DRIVER_NAME; |
| 1839 | rmi4_data->input_dev->phys = INPUT_PHYS_NAME; |
| 1840 | rmi4_data->input_dev->id.bustype = BUS_I2C; |
Alexandra Chin | 5d2999d | 2013-02-22 15:09:29 -0800 | [diff] [blame] | 1841 | rmi4_data->input_dev->id.product = SYNAPTICS_DSX_DRIVER_PRODUCT; |
| 1842 | rmi4_data->input_dev->id.version = SYNAPTICS_DSX_DRIVER_VERSION; |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1843 | rmi4_data->input_dev->dev.parent = &client->dev; |
| 1844 | input_set_drvdata(rmi4_data->input_dev, rmi4_data); |
| 1845 | |
| 1846 | set_bit(EV_SYN, rmi4_data->input_dev->evbit); |
| 1847 | set_bit(EV_KEY, rmi4_data->input_dev->evbit); |
| 1848 | set_bit(EV_ABS, rmi4_data->input_dev->evbit); |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1849 | set_bit(BTN_TOUCH, rmi4_data->input_dev->keybit); |
| 1850 | set_bit(BTN_TOOL_FINGER, rmi4_data->input_dev->keybit); |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1851 | |
| 1852 | #ifdef INPUT_PROP_DIRECT |
| 1853 | set_bit(INPUT_PROP_DIRECT, rmi4_data->input_dev->propbit); |
| 1854 | #endif |
| 1855 | |
| 1856 | input_set_abs_params(rmi4_data->input_dev, |
| 1857 | ABS_MT_POSITION_X, 0, |
| 1858 | rmi4_data->sensor_max_x, 0, 0); |
| 1859 | input_set_abs_params(rmi4_data->input_dev, |
| 1860 | ABS_MT_POSITION_Y, 0, |
| 1861 | rmi4_data->sensor_max_y, 0, 0); |
| 1862 | #ifdef REPORT_2D_W |
| 1863 | input_set_abs_params(rmi4_data->input_dev, |
| 1864 | ABS_MT_TOUCH_MAJOR, 0, |
| 1865 | MAX_ABS_MT_TOUCH_MAJOR, 0, 0); |
| 1866 | #endif |
| 1867 | |
| 1868 | #ifdef TYPE_B_PROTOCOL |
| 1869 | input_mt_init_slots(rmi4_data->input_dev, |
| 1870 | rmi4_data->num_of_fingers); |
| 1871 | #endif |
| 1872 | |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 1873 | retval = synaptics_rmi4_regulator_configure(rmi4_data, true); |
| 1874 | if (retval < 0) { |
| 1875 | dev_err(&client->dev, "Failed to configure regulators\n"); |
Amy Maloche | 946da66 | 2013-01-18 16:27:11 -0800 | [diff] [blame] | 1876 | goto err_reg_configure; |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | retval = synaptics_rmi4_power_on(rmi4_data, true); |
| 1880 | if (retval < 0) { |
| 1881 | dev_err(&client->dev, "Failed to power on\n"); |
Amy Maloche | 946da66 | 2013-01-18 16:27:11 -0800 | [diff] [blame] | 1882 | goto err_power_device; |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 1883 | } |
| 1884 | |
Amy Maloche | 946da66 | 2013-01-18 16:27:11 -0800 | [diff] [blame] | 1885 | if (gpio_is_valid(platform_data->irq_gpio)) { |
| 1886 | /* configure touchscreen irq gpio */ |
| 1887 | retval = gpio_request(platform_data->irq_gpio, "rmi4_irq_gpio"); |
| 1888 | if (retval) { |
| 1889 | dev_err(&client->dev, "unable to request gpio [%d]\n", |
| 1890 | platform_data->irq_gpio); |
| 1891 | goto err_query_device; |
| 1892 | } |
| 1893 | retval = gpio_direction_input(platform_data->irq_gpio); |
| 1894 | if (retval) { |
| 1895 | dev_err(&client->dev, |
| 1896 | "unable to set direction for gpio [%d]\n", |
| 1897 | platform_data->irq_gpio); |
| 1898 | goto err_irq_gpio_req; |
| 1899 | } |
| 1900 | } else { |
| 1901 | dev_err(&client->dev, "irq gpio not provided\n"); |
| 1902 | goto err_query_device; |
| 1903 | } |
| 1904 | |
| 1905 | if (gpio_is_valid(platform_data->reset_gpio)) { |
| 1906 | /* configure touchscreen reset out gpio */ |
| 1907 | retval = gpio_request(platform_data->reset_gpio, |
| 1908 | "rmi4_reset_gpio"); |
| 1909 | if (retval) { |
| 1910 | dev_err(&client->dev, "unable to request gpio [%d]\n", |
| 1911 | platform_data->reset_gpio); |
| 1912 | goto err_irq_gpio_req; |
| 1913 | } |
| 1914 | |
| 1915 | retval = gpio_direction_output(platform_data->reset_gpio, 1); |
| 1916 | if (retval) { |
| 1917 | dev_err(&client->dev, |
| 1918 | "unable to set direction for gpio [%d]\n", |
| 1919 | platform_data->reset_gpio); |
| 1920 | goto err_reset_gpio_req; |
| 1921 | } |
| 1922 | |
| 1923 | gpio_set_value(platform_data->reset_gpio, 0); |
| 1924 | usleep(RMI4_GPIO_SLEEP_LOW_US); |
| 1925 | gpio_set_value(platform_data->reset_gpio, 1); |
| 1926 | msleep(RMI4_GPIO_WAIT_HIGH_MS); |
| 1927 | } |
| 1928 | |
| 1929 | |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 1930 | init_waitqueue_head(&rmi4_data->wait); |
| 1931 | mutex_init(&(rmi4_data->rmi4_io_ctrl_mutex)); |
| 1932 | |
| 1933 | retval = synaptics_rmi4_query_device(rmi4_data); |
| 1934 | if (retval < 0) { |
| 1935 | dev_err(&client->dev, |
| 1936 | "%s: Failed to query device\n", |
| 1937 | __func__); |
Amy Maloche | 946da66 | 2013-01-18 16:27:11 -0800 | [diff] [blame] | 1938 | goto err_reset_gpio_req; |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | i2c_set_clientdata(client, rmi4_data); |
| 1942 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1943 | f1a = NULL; |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 1944 | if (!list_empty(&rmi->support_fn_list)) { |
| 1945 | list_for_each_entry(fhandler, &rmi->support_fn_list, link) { |
| 1946 | if (fhandler->fn_number == SYNAPTICS_RMI4_F1A) |
| 1947 | f1a = fhandler->data; |
| 1948 | } |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | if (f1a) { |
| 1952 | for (ii = 0; ii < f1a->valid_button_count; ii++) { |
| 1953 | set_bit(f1a->button_map[ii], |
| 1954 | rmi4_data->input_dev->keybit); |
| 1955 | input_set_capability(rmi4_data->input_dev, |
| 1956 | EV_KEY, f1a->button_map[ii]); |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | retval = input_register_device(rmi4_data->input_dev); |
| 1961 | if (retval) { |
| 1962 | dev_err(&client->dev, |
| 1963 | "%s: Failed to register input device\n", |
| 1964 | __func__); |
| 1965 | goto err_register_input; |
| 1966 | } |
| 1967 | |
| 1968 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 1969 | rmi4_data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; |
| 1970 | rmi4_data->early_suspend.suspend = synaptics_rmi4_early_suspend; |
| 1971 | rmi4_data->early_suspend.resume = synaptics_rmi4_late_resume; |
| 1972 | register_early_suspend(&rmi4_data->early_suspend); |
| 1973 | #endif |
| 1974 | |
| 1975 | if (!exp_fn_inited) { |
| 1976 | mutex_init(&exp_fn_list_mutex); |
| 1977 | INIT_LIST_HEAD(&exp_fn_list); |
| 1978 | exp_fn_inited = 1; |
| 1979 | } |
| 1980 | |
| 1981 | rmi4_data->det_workqueue = |
| 1982 | create_singlethread_workqueue("rmi_det_workqueue"); |
| 1983 | INIT_DELAYED_WORK(&rmi4_data->det_work, |
| 1984 | synaptics_rmi4_detection_work); |
| 1985 | queue_delayed_work(rmi4_data->det_workqueue, |
| 1986 | &rmi4_data->det_work, |
| 1987 | msecs_to_jiffies(EXP_FN_DET_INTERVAL)); |
| 1988 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 1989 | rmi4_data->irq = gpio_to_irq(platform_data->irq_gpio); |
| 1990 | |
| 1991 | retval = synaptics_rmi4_irq_enable(rmi4_data, true); |
| 1992 | if (retval < 0) { |
| 1993 | dev_err(&client->dev, |
| 1994 | "%s: Failed to enable attention interrupt\n", |
| 1995 | __func__); |
| 1996 | goto err_enable_irq; |
| 1997 | } |
| 1998 | |
| 1999 | for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) { |
| 2000 | retval = sysfs_create_file(&rmi4_data->input_dev->dev.kobj, |
| 2001 | &attrs[attr_count].attr); |
| 2002 | if (retval < 0) { |
| 2003 | dev_err(&client->dev, |
| 2004 | "%s: Failed to create sysfs attributes\n", |
| 2005 | __func__); |
| 2006 | goto err_sysfs; |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | return retval; |
| 2011 | |
| 2012 | err_sysfs: |
| 2013 | for (attr_count--; attr_count >= 0; attr_count--) { |
| 2014 | sysfs_remove_file(&rmi4_data->input_dev->dev.kobj, |
| 2015 | &attrs[attr_count].attr); |
| 2016 | } |
| 2017 | |
| 2018 | err_enable_irq: |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2019 | input_unregister_device(rmi4_data->input_dev); |
| 2020 | |
| 2021 | err_register_input: |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 2022 | if (!list_empty(&rmi->support_fn_list)) { |
| 2023 | list_for_each_entry(fhandler, &rmi->support_fn_list, link) { |
| 2024 | if (fhandler->fn_number == SYNAPTICS_RMI4_F1A) |
| 2025 | synaptics_rmi4_f1a_kfree(fhandler); |
| 2026 | else |
| 2027 | kfree(fhandler->data); |
| 2028 | kfree(fhandler); |
| 2029 | } |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2030 | } |
Amy Maloche | 946da66 | 2013-01-18 16:27:11 -0800 | [diff] [blame] | 2031 | err_reset_gpio_req: |
| 2032 | if (gpio_is_valid(platform_data->reset_gpio)) |
| 2033 | gpio_free(platform_data->reset_gpio); |
| 2034 | err_irq_gpio_req: |
| 2035 | if (gpio_is_valid(platform_data->irq_gpio)) |
| 2036 | gpio_free(platform_data->irq_gpio); |
| 2037 | err_query_device: |
| 2038 | synaptics_rmi4_power_on(rmi4_data, false); |
| 2039 | err_power_device: |
| 2040 | synaptics_rmi4_regulator_configure(rmi4_data, false); |
| 2041 | err_reg_configure: |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2042 | input_free_device(rmi4_data->input_dev); |
| 2043 | rmi4_data->input_dev = NULL; |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2044 | err_input_device: |
| 2045 | kfree(rmi4_data); |
| 2046 | |
| 2047 | return retval; |
| 2048 | } |
| 2049 | |
| 2050 | /** |
| 2051 | * synaptics_rmi4_remove() |
| 2052 | * |
| 2053 | * Called by the kernel when the association with an I2C device of the |
| 2054 | * same name is broken (when the driver is unloaded). |
| 2055 | * |
| 2056 | * This funtion terminates the work queue, stops sensor data acquisition, |
| 2057 | * frees the interrupt, unregisters the driver from the input subsystem, |
| 2058 | * turns off the power to the sensor, and frees other allocated resources. |
| 2059 | */ |
| 2060 | static int __devexit synaptics_rmi4_remove(struct i2c_client *client) |
| 2061 | { |
| 2062 | unsigned char attr_count; |
| 2063 | struct synaptics_rmi4_fn *fhandler; |
| 2064 | struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client); |
| 2065 | struct synaptics_rmi4_device_info *rmi; |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2066 | |
| 2067 | rmi = &(rmi4_data->rmi4_mod_info); |
| 2068 | |
| 2069 | cancel_delayed_work_sync(&rmi4_data->det_work); |
| 2070 | flush_workqueue(rmi4_data->det_workqueue); |
| 2071 | destroy_workqueue(rmi4_data->det_workqueue); |
| 2072 | |
| 2073 | rmi4_data->touch_stopped = true; |
| 2074 | wake_up(&rmi4_data->wait); |
| 2075 | |
| 2076 | synaptics_rmi4_irq_enable(rmi4_data, false); |
| 2077 | |
| 2078 | for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) { |
| 2079 | sysfs_remove_file(&rmi4_data->input_dev->dev.kobj, |
| 2080 | &attrs[attr_count].attr); |
| 2081 | } |
| 2082 | |
| 2083 | input_unregister_device(rmi4_data->input_dev); |
| 2084 | |
Alexandra Chin | d5591a6 | 2013-02-07 12:59:15 -0800 | [diff] [blame] | 2085 | if (!list_empty(&rmi->support_fn_list)) { |
| 2086 | list_for_each_entry(fhandler, &rmi->support_fn_list, link) { |
| 2087 | if (fhandler->fn_number == SYNAPTICS_RMI4_F1A) |
| 2088 | synaptics_rmi4_f1a_kfree(fhandler); |
| 2089 | else |
| 2090 | kfree(fhandler->data); |
| 2091 | kfree(fhandler); |
| 2092 | } |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2093 | } |
Amy Maloche | 946da66 | 2013-01-18 16:27:11 -0800 | [diff] [blame] | 2094 | |
| 2095 | if (gpio_is_valid(rmi4_data->board->reset_gpio)) |
| 2096 | gpio_free(rmi4_data->board->reset_gpio); |
| 2097 | if (gpio_is_valid(rmi4_data->board->irq_gpio)) |
| 2098 | gpio_free(rmi4_data->board->irq_gpio); |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2099 | |
Amy Maloche | 1a53b61 | 2013-01-18 15:25:15 -0800 | [diff] [blame] | 2100 | synaptics_rmi4_power_on(rmi4_data, false); |
| 2101 | synaptics_rmi4_regulator_configure(rmi4_data, false); |
| 2102 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2103 | kfree(rmi4_data); |
| 2104 | |
| 2105 | return 0; |
| 2106 | } |
| 2107 | |
| 2108 | #ifdef CONFIG_PM |
| 2109 | /** |
| 2110 | * synaptics_rmi4_sensor_sleep() |
| 2111 | * |
| 2112 | * Called by synaptics_rmi4_early_suspend() and synaptics_rmi4_suspend(). |
| 2113 | * |
| 2114 | * This function stops finger data acquisition and puts the sensor to sleep. |
| 2115 | */ |
| 2116 | static void synaptics_rmi4_sensor_sleep(struct synaptics_rmi4_data *rmi4_data) |
| 2117 | { |
| 2118 | int retval; |
| 2119 | unsigned char device_ctrl; |
| 2120 | |
| 2121 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 2122 | rmi4_data->f01_ctrl_base_addr, |
| 2123 | &device_ctrl, |
| 2124 | sizeof(device_ctrl)); |
| 2125 | if (retval < 0) { |
| 2126 | dev_err(&(rmi4_data->input_dev->dev), |
| 2127 | "%s: Failed to enter sleep mode\n", |
| 2128 | __func__); |
| 2129 | rmi4_data->sensor_sleep = false; |
| 2130 | return; |
| 2131 | } |
| 2132 | |
| 2133 | device_ctrl = (device_ctrl & ~MASK_3BIT); |
| 2134 | device_ctrl = (device_ctrl | NO_SLEEP_OFF | SENSOR_SLEEP); |
| 2135 | |
| 2136 | retval = synaptics_rmi4_i2c_write(rmi4_data, |
| 2137 | rmi4_data->f01_ctrl_base_addr, |
| 2138 | &device_ctrl, |
| 2139 | sizeof(device_ctrl)); |
| 2140 | if (retval < 0) { |
| 2141 | dev_err(&(rmi4_data->input_dev->dev), |
| 2142 | "%s: Failed to enter sleep mode\n", |
| 2143 | __func__); |
| 2144 | rmi4_data->sensor_sleep = false; |
| 2145 | return; |
| 2146 | } else { |
| 2147 | rmi4_data->sensor_sleep = true; |
| 2148 | } |
| 2149 | |
| 2150 | return; |
| 2151 | } |
| 2152 | |
| 2153 | /** |
| 2154 | * synaptics_rmi4_sensor_wake() |
| 2155 | * |
| 2156 | * Called by synaptics_rmi4_resume() and synaptics_rmi4_late_resume(). |
| 2157 | * |
| 2158 | * This function wakes the sensor from sleep. |
| 2159 | */ |
| 2160 | static void synaptics_rmi4_sensor_wake(struct synaptics_rmi4_data *rmi4_data) |
| 2161 | { |
| 2162 | int retval; |
| 2163 | unsigned char device_ctrl; |
| 2164 | |
| 2165 | retval = synaptics_rmi4_i2c_read(rmi4_data, |
| 2166 | rmi4_data->f01_ctrl_base_addr, |
| 2167 | &device_ctrl, |
| 2168 | sizeof(device_ctrl)); |
| 2169 | if (retval < 0) { |
| 2170 | dev_err(&(rmi4_data->input_dev->dev), |
| 2171 | "%s: Failed to wake from sleep mode\n", |
| 2172 | __func__); |
| 2173 | rmi4_data->sensor_sleep = true; |
| 2174 | return; |
| 2175 | } |
| 2176 | |
| 2177 | device_ctrl = (device_ctrl & ~MASK_3BIT); |
| 2178 | device_ctrl = (device_ctrl | NO_SLEEP_OFF | NORMAL_OPERATION); |
| 2179 | |
| 2180 | retval = synaptics_rmi4_i2c_write(rmi4_data, |
| 2181 | rmi4_data->f01_ctrl_base_addr, |
| 2182 | &device_ctrl, |
| 2183 | sizeof(device_ctrl)); |
| 2184 | if (retval < 0) { |
| 2185 | dev_err(&(rmi4_data->input_dev->dev), |
| 2186 | "%s: Failed to wake from sleep mode\n", |
| 2187 | __func__); |
| 2188 | rmi4_data->sensor_sleep = true; |
| 2189 | return; |
| 2190 | } else { |
| 2191 | rmi4_data->sensor_sleep = false; |
| 2192 | } |
| 2193 | |
| 2194 | return; |
| 2195 | } |
| 2196 | |
| 2197 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 2198 | /** |
| 2199 | * synaptics_rmi4_early_suspend() |
| 2200 | * |
| 2201 | * Called by the kernel during the early suspend phase when the system |
| 2202 | * enters suspend. |
| 2203 | * |
| 2204 | * This function calls synaptics_rmi4_sensor_sleep() to stop finger |
| 2205 | * data acquisition and put the sensor to sleep. |
| 2206 | */ |
| 2207 | static void synaptics_rmi4_early_suspend(struct early_suspend *h) |
| 2208 | { |
| 2209 | struct synaptics_rmi4_data *rmi4_data = |
| 2210 | container_of(h, struct synaptics_rmi4_data, |
| 2211 | early_suspend); |
| 2212 | |
| 2213 | rmi4_data->touch_stopped = true; |
| 2214 | wake_up(&rmi4_data->wait); |
| 2215 | synaptics_rmi4_irq_enable(rmi4_data, false); |
| 2216 | synaptics_rmi4_sensor_sleep(rmi4_data); |
| 2217 | |
| 2218 | if (rmi4_data->full_pm_cycle) |
| 2219 | synaptics_rmi4_suspend(&(rmi4_data->input_dev->dev)); |
| 2220 | |
| 2221 | return; |
| 2222 | } |
| 2223 | |
| 2224 | /** |
| 2225 | * synaptics_rmi4_late_resume() |
| 2226 | * |
| 2227 | * Called by the kernel during the late resume phase when the system |
| 2228 | * wakes up from suspend. |
| 2229 | * |
| 2230 | * This function goes through the sensor wake process if the system wakes |
| 2231 | * up from early suspend (without going into suspend). |
| 2232 | */ |
| 2233 | static void synaptics_rmi4_late_resume(struct early_suspend *h) |
| 2234 | { |
| 2235 | struct synaptics_rmi4_data *rmi4_data = |
| 2236 | container_of(h, struct synaptics_rmi4_data, |
| 2237 | early_suspend); |
| 2238 | |
| 2239 | if (rmi4_data->full_pm_cycle) |
| 2240 | synaptics_rmi4_resume(&(rmi4_data->input_dev->dev)); |
| 2241 | |
| 2242 | if (rmi4_data->sensor_sleep == true) { |
| 2243 | synaptics_rmi4_sensor_wake(rmi4_data); |
| 2244 | rmi4_data->touch_stopped = false; |
| 2245 | synaptics_rmi4_irq_enable(rmi4_data, true); |
| 2246 | } |
| 2247 | |
| 2248 | return; |
| 2249 | } |
| 2250 | #endif |
| 2251 | |
| 2252 | /** |
| 2253 | * synaptics_rmi4_suspend() |
| 2254 | * |
| 2255 | * Called by the kernel during the suspend phase when the system |
| 2256 | * enters suspend. |
| 2257 | * |
| 2258 | * This function stops finger data acquisition and puts the sensor to |
| 2259 | * sleep (if not already done so during the early suspend phase), |
| 2260 | * disables the interrupt, and turns off the power to the sensor. |
| 2261 | */ |
| 2262 | static int synaptics_rmi4_suspend(struct device *dev) |
| 2263 | { |
| 2264 | struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2265 | |
| 2266 | if (!rmi4_data->sensor_sleep) { |
| 2267 | rmi4_data->touch_stopped = true; |
| 2268 | wake_up(&rmi4_data->wait); |
| 2269 | synaptics_rmi4_irq_enable(rmi4_data, false); |
| 2270 | synaptics_rmi4_sensor_sleep(rmi4_data); |
| 2271 | } |
| 2272 | |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2273 | return 0; |
| 2274 | } |
| 2275 | |
| 2276 | /** |
| 2277 | * synaptics_rmi4_resume() |
| 2278 | * |
| 2279 | * Called by the kernel during the resume phase when the system |
| 2280 | * wakes up from suspend. |
| 2281 | * |
| 2282 | * This function turns on the power to the sensor, wakes the sensor |
| 2283 | * from sleep, enables the interrupt, and starts finger data |
| 2284 | * acquisition. |
| 2285 | */ |
| 2286 | static int synaptics_rmi4_resume(struct device *dev) |
| 2287 | { |
| 2288 | struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev); |
Alexandra Chin | 669d27c | 2012-12-24 15:42:30 +0800 | [diff] [blame] | 2289 | |
| 2290 | synaptics_rmi4_sensor_wake(rmi4_data); |
| 2291 | rmi4_data->touch_stopped = false; |
| 2292 | synaptics_rmi4_irq_enable(rmi4_data, true); |
| 2293 | |
| 2294 | return 0; |
| 2295 | } |
| 2296 | |
| 2297 | static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = { |
| 2298 | .suspend = synaptics_rmi4_suspend, |
| 2299 | .resume = synaptics_rmi4_resume, |
| 2300 | }; |
| 2301 | #endif |
| 2302 | |
| 2303 | static const struct i2c_device_id synaptics_rmi4_id_table[] = { |
| 2304 | {DRIVER_NAME, 0}, |
| 2305 | {}, |
| 2306 | }; |
| 2307 | MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table); |
| 2308 | |
| 2309 | static struct i2c_driver synaptics_rmi4_driver = { |
| 2310 | .driver = { |
| 2311 | .name = DRIVER_NAME, |
| 2312 | .owner = THIS_MODULE, |
| 2313 | #ifdef CONFIG_PM |
| 2314 | .pm = &synaptics_rmi4_dev_pm_ops, |
| 2315 | #endif |
| 2316 | }, |
| 2317 | .probe = synaptics_rmi4_probe, |
| 2318 | .remove = __devexit_p(synaptics_rmi4_remove), |
| 2319 | .id_table = synaptics_rmi4_id_table, |
| 2320 | }; |
| 2321 | |
| 2322 | /** |
| 2323 | * synaptics_rmi4_init() |
| 2324 | * |
| 2325 | * Called by the kernel during do_initcalls (if built-in) |
| 2326 | * or when the driver is loaded (if a module). |
| 2327 | * |
| 2328 | * This function registers the driver to the I2C subsystem. |
| 2329 | * |
| 2330 | */ |
| 2331 | static int __init synaptics_rmi4_init(void) |
| 2332 | { |
| 2333 | return i2c_add_driver(&synaptics_rmi4_driver); |
| 2334 | } |
| 2335 | |
| 2336 | /** |
| 2337 | * synaptics_rmi4_exit() |
| 2338 | * |
| 2339 | * Called by the kernel when the driver is unloaded. |
| 2340 | * |
| 2341 | * This funtion unregisters the driver from the I2C subsystem. |
| 2342 | * |
| 2343 | */ |
| 2344 | static void __exit synaptics_rmi4_exit(void) |
| 2345 | { |
| 2346 | i2c_del_driver(&synaptics_rmi4_driver); |
| 2347 | } |
| 2348 | |
| 2349 | module_init(synaptics_rmi4_init); |
| 2350 | module_exit(synaptics_rmi4_exit); |
| 2351 | |
| 2352 | MODULE_AUTHOR("Synaptics, Inc."); |
| 2353 | MODULE_DESCRIPTION("Synaptics RMI4 I2C Touch Driver"); |
| 2354 | MODULE_LICENSE("GPL v2"); |