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