Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Atmel maXTouch Touchscreen Controller Driver |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2010 Atmel Corporation |
| 6 | * Copyright (C) 2010 Ulf Samuelsson (ulf@atmel.com) |
| 7 | * Copyright (C) 2009 Raphael Derosso Pereira <raphaelpereira@gmail.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * |
| 25 | * Driver for Atmel maXTouch family of touch controllers. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/input.h> |
| 32 | #include <linux/debugfs.h> |
| 33 | #include <linux/cdev.h> |
| 34 | #include <linux/mutex.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/pm_runtime.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 37 | #include <linux/module.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 38 | |
| 39 | #include <asm/uaccess.h> |
| 40 | |
| 41 | #include <linux/atmel_maxtouch.h> |
| 42 | |
| 43 | #if defined(CONFIG_HAS_EARLYSUSPEND) |
| 44 | #include <linux/earlysuspend.h> |
| 45 | |
| 46 | /* Early-suspend level */ |
| 47 | #define MXT_SUSPEND_LEVEL 1 |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | #define DRIVER_VERSION "0.91a_mod" |
| 52 | |
| 53 | static int debug = DEBUG_INFO; |
| 54 | static int comms = 0; |
| 55 | module_param(debug, int, 0644); |
| 56 | module_param(comms, int, 0644); |
| 57 | |
| 58 | MODULE_PARM_DESC(debug, "Activate debugging output"); |
| 59 | MODULE_PARM_DESC(comms, "Select communications mode"); |
| 60 | |
| 61 | #define T7_DATA_SIZE 3 |
| 62 | |
| 63 | /* Device Info descriptor */ |
| 64 | /* Parsed from maXTouch "Id information" inside device */ |
| 65 | struct mxt_device_info { |
| 66 | u8 family_id; |
| 67 | u8 variant_id; |
| 68 | u8 major; |
| 69 | u8 minor; |
| 70 | u8 build; |
| 71 | u8 num_objs; |
| 72 | u8 x_size; |
| 73 | u8 y_size; |
| 74 | char family_name[16]; /* Family name */ |
| 75 | char variant_name[16]; /* Variant name */ |
| 76 | u16 num_nodes; /* Number of sensor nodes */ |
| 77 | }; |
| 78 | |
| 79 | /* object descriptor table, parsed from maXTouch "object table" */ |
| 80 | struct mxt_object { |
| 81 | u16 chip_addr; |
| 82 | u8 type; |
| 83 | u8 size; |
| 84 | u8 instances; |
| 85 | u8 num_report_ids; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | /* Mapping from report id to object type and instance */ |
| 90 | struct report_id_map { |
| 91 | u8 object; |
| 92 | u8 instance; |
| 93 | /* |
| 94 | * This is the first report ID belonging to object. It enables us to |
| 95 | * find out easily the touch number: each touch has different report |
| 96 | * ID (which are assigned to touches in increasing order). By |
| 97 | * subtracting the first report ID from current, we get the touch |
| 98 | * number. |
| 99 | */ |
| 100 | u8 first_rid; |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | /* Driver datastructure */ |
| 105 | struct mxt_data { |
| 106 | struct i2c_client *client; |
| 107 | struct input_dev *input; |
| 108 | char phys_name[32]; |
| 109 | int irq; |
| 110 | |
| 111 | u16 last_read_addr; |
| 112 | bool new_msgs; |
| 113 | u8 *last_message; |
| 114 | |
| 115 | int valid_irq_counter; |
| 116 | int invalid_irq_counter; |
| 117 | int irq_counter; |
| 118 | int message_counter; |
| 119 | int read_fail_counter; |
| 120 | |
| 121 | |
| 122 | int bytes_to_read; |
| 123 | |
| 124 | struct delayed_work dwork; |
| 125 | u8 xpos_format; |
| 126 | u8 ypos_format; |
| 127 | |
| 128 | u8 numtouch; |
| 129 | |
| 130 | struct mxt_device_info device_info; |
| 131 | |
| 132 | u32 info_block_crc; |
| 133 | u32 configuration_crc; |
| 134 | u16 report_id_count; |
| 135 | struct report_id_map *rid_map; |
| 136 | struct mxt_object *object_table; |
| 137 | |
| 138 | u16 msg_proc_addr; |
| 139 | u8 message_size; |
| 140 | |
| 141 | u16 min_x_val; |
| 142 | u16 min_y_val; |
| 143 | u16 max_x_val; |
| 144 | u16 max_y_val; |
| 145 | |
| 146 | int (*init_hw)(struct i2c_client *client); |
| 147 | int (*exit_hw)(struct i2c_client *client); |
| 148 | int (*power_on)(bool on); |
| 149 | u8 (*valid_interrupt)(void); |
| 150 | u8 (*read_chg)(void); |
| 151 | |
| 152 | /* debugfs variables */ |
| 153 | struct dentry *debug_dir; |
| 154 | int current_debug_datap; |
| 155 | |
| 156 | struct mutex debug_mutex; |
| 157 | u16 *debug_data; |
| 158 | |
| 159 | /* Character device variables */ |
| 160 | struct cdev cdev; |
| 161 | struct cdev cdev_messages; /* 2nd Char dev for messages */ |
| 162 | dev_t dev_num; |
| 163 | struct class *mxt_class; |
| 164 | |
| 165 | |
| 166 | u16 address_pointer; |
| 167 | bool valid_ap; |
| 168 | |
| 169 | /* Message buffer & pointers */ |
| 170 | char *messages; |
| 171 | int msg_buffer_startp, msg_buffer_endp; |
| 172 | /* Put only non-touch messages to buffer if this is set */ |
| 173 | char nontouch_msg_only; |
| 174 | struct mutex msg_mutex; |
| 175 | #if defined(CONFIG_HAS_EARLYSUSPEND) |
| 176 | struct early_suspend early_suspend; |
| 177 | #endif |
| 178 | u8 t7_data[T7_DATA_SIZE]; |
| 179 | bool is_suspended; |
| 180 | }; |
| 181 | /*default value, enough to read versioning*/ |
| 182 | #define CONFIG_DATA_SIZE 6 |
| 183 | static u16 t38_size = CONFIG_DATA_SIZE; |
| 184 | static int mxt_read_block(struct i2c_client *client, u16 addr, u16 length, |
| 185 | u8 *value); |
| 186 | static int mxt_write_byte(struct i2c_client *client, u16 addr, u8 value); |
| 187 | static int mxt_write_block(struct i2c_client *client, u16 addr, u16 length, |
| 188 | u8 *value); |
| 189 | static u8 mxt_valid_interrupt_dummy(void) |
| 190 | { |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | #define I2C_RETRY_COUNT 5 |
| 195 | #define I2C_PAYLOAD_SIZE 254 |
| 196 | |
| 197 | /* Returns the start address of object in mXT memory. */ |
| 198 | #define MXT_BASE_ADDR(object_type, mxt) \ |
| 199 | get_object_address(object_type, 0, mxt->object_table, \ |
| 200 | mxt->device_info.num_objs) |
| 201 | |
| 202 | /* Maps a report ID to an object type (object type number). */ |
| 203 | #define REPORT_ID_TO_OBJECT(rid, mxt) \ |
| 204 | (((rid) == 0xff) ? 0 : mxt->rid_map[rid].object) |
| 205 | |
| 206 | /* Maps a report ID to an object type (string). */ |
| 207 | #define REPORT_ID_TO_OBJECT_NAME(rid, mxt) \ |
| 208 | object_type_name[REPORT_ID_TO_OBJECT(rid, mxt)] |
| 209 | |
| 210 | /* Returns non-zero if given object is a touch object */ |
| 211 | #define IS_TOUCH_OBJECT(object) \ |
| 212 | ((object == MXT_TOUCH_MULTITOUCHSCREEN_T9) || \ |
| 213 | (object == MXT_TOUCH_KEYARRAY_T15) || \ |
| 214 | (object == MXT_TOUCH_PROXIMITY_T23) || \ |
| 215 | (object == MXT_TOUCH_SINGLETOUCHSCREEN_T10) || \ |
| 216 | (object == MXT_TOUCH_XSLIDER_T11) || \ |
| 217 | (object == MXT_TOUCH_YSLIDER_T12) || \ |
| 218 | (object == MXT_TOUCH_XWHEEL_T13) || \ |
| 219 | (object == MXT_TOUCH_YWHEEL_T14) || \ |
| 220 | (object == MXT_TOUCH_KEYSET_T31) || \ |
| 221 | (object == MXT_TOUCH_XSLIDERSET_T32) ? 1 : 0) |
| 222 | |
| 223 | #define mxt_debug(level, ...) \ |
| 224 | do { \ |
| 225 | if (debug >= (level)) \ |
| 226 | pr_debug(__VA_ARGS__); \ |
| 227 | } while (0) |
| 228 | |
| 229 | |
| 230 | /* |
| 231 | * Check whether we have multi-touch enabled kernel; if not, report just the |
| 232 | * first touch (on mXT224, the maximum is 10 simultaneous touches). |
| 233 | * Because just the 1st one is reported, it might seem that the screen is not |
| 234 | * responding to touch if the first touch is removed while the screen is being |
| 235 | * touched by another finger, so beware. |
| 236 | * |
| 237 | */ |
| 238 | |
| 239 | #ifdef ABS_MT_TRACKING_ID |
| 240 | static inline void report_mt(int touch_number, int size, int x, int y, struct |
| 241 | mxt_data *mxt) { |
| 242 | input_report_abs(mxt->input, ABS_MT_TRACKING_ID, touch_number); |
| 243 | input_report_abs(mxt->input, ABS_MT_TOUCH_MAJOR, size); |
| 244 | input_report_abs(mxt->input, ABS_MT_POSITION_X, x); |
| 245 | input_report_abs(mxt->input, ABS_MT_POSITION_Y, y); |
| 246 | input_mt_sync(mxt->input); |
| 247 | } |
| 248 | #else |
| 249 | static inline void report_mt(int touch_number, int size, int x, int y, struct |
| 250 | mxt_data *mxt) { |
| 251 | if (touch_number == 0) { |
| 252 | input_report_abs(mxt->input, ABS_TOOL_WIDTH, size); |
| 253 | input_report_abs(mxt->input, ABS_X, x); |
| 254 | input_report_abs(mxt->input, ABS_Y, y); |
| 255 | } |
| 256 | } |
| 257 | #endif |
| 258 | |
| 259 | |
| 260 | static inline void report_gesture(int data, struct mxt_data *mxt) |
| 261 | { |
| 262 | input_event(mxt->input, EV_MSC, MSC_GESTURE, data); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | static const u8 *object_type_name[] = { |
| 267 | [0] = "Reserved", |
| 268 | [5] = "GEN_MESSAGEPROCESSOR_T5", |
| 269 | [6] = "GEN_COMMANDPROCESSOR_T6", |
| 270 | [7] = "GEN_POWERCONFIG_T7", |
| 271 | [8] = "GEN_ACQUIRECONFIG_T8", |
| 272 | [9] = "TOUCH_MULTITOUCHSCREEN_T9", |
| 273 | [15] = "TOUCH_KEYARRAY_T15", |
| 274 | [17] = "SPT_COMMSCONFIG_T18", |
| 275 | [19] = "SPT_GPIOPWM_T19", |
| 276 | [20] = "PROCI_GRIPFACESUPPRESSION_T20", |
| 277 | [22] = "PROCG_NOISESUPPRESSION_T22", |
| 278 | [23] = "TOUCH_PROXIMITY_T23", |
| 279 | [24] = "PROCI_ONETOUCHGESTUREPROCESSOR_T24", |
| 280 | [25] = "SPT_SELFTEST_T25", |
| 281 | [27] = "PROCI_TWOTOUCHGESTUREPROCESSOR_T27", |
| 282 | [28] = "SPT_CTECONFIG_T28", |
| 283 | [37] = "DEBUG_DIAGNOSTICS_T37", |
| 284 | [38] = "SPT_USER_DATA_T38", |
| 285 | [40] = "PROCI_GRIPSUPPRESSION_T40", |
| 286 | [41] = "PROCI_PALMSUPPRESSION_T41", |
| 287 | [42] = "PROCI_FACESUPPRESSION_T42", |
| 288 | [43] = "SPT_DIGITIZER_T43", |
| 289 | [44] = "SPT_MESSAGECOUNT_T44", |
| 290 | }; |
| 291 | |
| 292 | |
| 293 | static u16 get_object_address(uint8_t object_type, |
| 294 | uint8_t instance, |
| 295 | struct mxt_object *object_table, |
| 296 | int max_objs); |
| 297 | |
| 298 | int mxt_write_ap(struct mxt_data *mxt, u16 ap); |
| 299 | |
| 300 | static int mxt_read_block_wo_addr(struct i2c_client *client, |
| 301 | u16 length, |
| 302 | u8 *value); |
| 303 | |
| 304 | ssize_t debug_data_read(struct mxt_data *mxt, char *buf, size_t count, |
| 305 | loff_t *ppos, u8 debug_command){ |
| 306 | int i; |
| 307 | u16 *data; |
| 308 | u16 diagnostics_reg; |
| 309 | int offset = 0; |
| 310 | int size; |
| 311 | int read_size; |
| 312 | int error; |
| 313 | char *buf_start; |
| 314 | u16 debug_data_addr; |
| 315 | u16 page_address; |
| 316 | u8 page; |
| 317 | u8 debug_command_reg; |
| 318 | |
| 319 | data = mxt->debug_data; |
| 320 | if (data == NULL) |
| 321 | return -EIO; |
| 322 | |
| 323 | /* If first read after open, read all data to buffer. */ |
| 324 | if (mxt->current_debug_datap == 0){ |
| 325 | |
| 326 | diagnostics_reg = MXT_BASE_ADDR(MXT_GEN_COMMANDPROCESSOR_T6, |
| 327 | mxt) + |
| 328 | MXT_ADR_T6_DIAGNOSTIC; |
| 329 | if (count > (mxt->device_info.num_nodes * 2)) |
| 330 | count = mxt->device_info.num_nodes; |
| 331 | |
| 332 | debug_data_addr = MXT_BASE_ADDR(MXT_DEBUG_DIAGNOSTIC_T37, mxt)+ |
| 333 | MXT_ADR_T37_DATA; |
| 334 | page_address = MXT_BASE_ADDR(MXT_DEBUG_DIAGNOSTIC_T37, mxt) + |
| 335 | MXT_ADR_T37_PAGE; |
| 336 | error = mxt_read_block(mxt->client, page_address, 1, &page); |
| 337 | if (error < 0) |
| 338 | return error; |
| 339 | mxt_debug(DEBUG_TRACE, "debug data page = %d\n", page); |
| 340 | while (page != 0) { |
| 341 | error = mxt_write_byte(mxt->client, |
| 342 | diagnostics_reg, |
| 343 | MXT_CMD_T6_PAGE_DOWN); |
| 344 | if (error < 0) |
| 345 | return error; |
| 346 | /* Wait for command to be handled; when it has, the |
| 347 | register will be cleared. */ |
| 348 | debug_command_reg = 1; |
| 349 | while (debug_command_reg != 0) { |
| 350 | error = mxt_read_block(mxt->client, |
| 351 | diagnostics_reg, 1, |
| 352 | &debug_command_reg); |
| 353 | if (error < 0) |
| 354 | return error; |
| 355 | mxt_debug(DEBUG_TRACE, |
| 356 | "Waiting for debug diag command " |
| 357 | "to propagate...\n"); |
| 358 | |
| 359 | } |
| 360 | error = mxt_read_block(mxt->client, page_address, 1, |
| 361 | &page); |
| 362 | if (error < 0) |
| 363 | return error; |
| 364 | mxt_debug(DEBUG_TRACE, "debug data page = %d\n", page); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Lock mutex to prevent writing some unwanted data to debug |
| 369 | * command register. User can still write through the char |
| 370 | * device interface though. TODO: fix? |
| 371 | */ |
| 372 | |
| 373 | mutex_lock(&mxt->debug_mutex); |
| 374 | /* Configure Debug Diagnostics object to show deltas/refs */ |
| 375 | error = mxt_write_byte(mxt->client, diagnostics_reg, |
| 376 | debug_command); |
| 377 | |
| 378 | /* Wait for command to be handled; when it has, the |
| 379 | * register will be cleared. */ |
| 380 | debug_command_reg = 1; |
| 381 | while (debug_command_reg != 0) { |
| 382 | error = mxt_read_block(mxt->client, |
| 383 | diagnostics_reg, 1, |
| 384 | &debug_command_reg); |
| 385 | if (error < 0) |
| 386 | return error; |
| 387 | mxt_debug(DEBUG_TRACE, "Waiting for debug diag command " |
| 388 | "to propagate...\n"); |
| 389 | |
| 390 | } |
| 391 | |
| 392 | if (error < 0) { |
| 393 | printk (KERN_WARNING |
| 394 | "Error writing to maXTouch device!\n"); |
| 395 | return error; |
| 396 | } |
| 397 | |
| 398 | size = mxt->device_info.num_nodes * sizeof(u16); |
| 399 | |
| 400 | while (size > 0) { |
| 401 | read_size = size > 128 ? 128 : size; |
| 402 | mxt_debug(DEBUG_TRACE, |
| 403 | "Debug data read loop, reading %d bytes...\n", |
| 404 | read_size); |
| 405 | error = mxt_read_block(mxt->client, |
| 406 | debug_data_addr, |
| 407 | read_size, |
| 408 | (u8 *) &data[offset]); |
| 409 | if (error < 0) { |
| 410 | printk(KERN_WARNING |
| 411 | "Error reading debug data\n"); |
| 412 | goto error; |
| 413 | } |
| 414 | offset += read_size/2; |
| 415 | size -= read_size; |
| 416 | |
| 417 | /* Select next page */ |
| 418 | error = mxt_write_byte(mxt->client, diagnostics_reg, |
| 419 | MXT_CMD_T6_PAGE_UP); |
| 420 | if (error < 0) { |
| 421 | printk(KERN_WARNING |
| 422 | "Error writing to maXTouch device!\n"); |
| 423 | goto error; |
| 424 | } |
| 425 | } |
| 426 | mutex_unlock(&mxt->debug_mutex); |
| 427 | } |
| 428 | |
| 429 | buf_start = buf; |
| 430 | i = mxt->current_debug_datap; |
| 431 | |
| 432 | while (((buf- buf_start) < (count - 6)) && |
| 433 | (i < mxt->device_info.num_nodes)){ |
| 434 | |
| 435 | mxt->current_debug_datap++; |
| 436 | if (debug_command == MXT_CMD_T6_REFERENCES_MODE) |
| 437 | buf += sprintf(buf, "%d: %5d\n", i, |
| 438 | (u16) le16_to_cpu(data[i])); |
| 439 | else if (debug_command == MXT_CMD_T6_DELTAS_MODE) |
| 440 | buf += sprintf(buf, "%d: %5d\n", i, |
| 441 | (s16) le16_to_cpu(data[i])); |
| 442 | i++; |
| 443 | } |
| 444 | |
| 445 | return (buf - buf_start); |
| 446 | error: |
| 447 | mutex_unlock(&mxt->debug_mutex); |
| 448 | return error; |
| 449 | } |
| 450 | |
| 451 | ssize_t deltas_read(struct file *file, char *buf, size_t count, loff_t *ppos) |
| 452 | { |
| 453 | return debug_data_read(file->private_data, buf, count, ppos, |
| 454 | MXT_CMD_T6_DELTAS_MODE); |
| 455 | } |
| 456 | |
| 457 | ssize_t refs_read(struct file *file, char *buf, size_t count, |
| 458 | loff_t *ppos) |
| 459 | { |
| 460 | return debug_data_read(file->private_data, buf, count, ppos, |
| 461 | MXT_CMD_T6_REFERENCES_MODE); |
| 462 | } |
| 463 | |
| 464 | int debug_data_open(struct inode *inode, struct file *file) |
| 465 | { |
| 466 | struct mxt_data *mxt; |
| 467 | int i; |
| 468 | mxt = inode->i_private; |
| 469 | if (mxt == NULL) |
| 470 | return -EIO; |
| 471 | mxt->current_debug_datap = 0; |
| 472 | mxt->debug_data = kmalloc(mxt->device_info.num_nodes * sizeof(u16), |
| 473 | GFP_KERNEL); |
| 474 | if (mxt->debug_data == NULL) |
| 475 | return -ENOMEM; |
| 476 | |
| 477 | |
| 478 | for (i = 0; i < mxt->device_info.num_nodes; i++) |
| 479 | mxt->debug_data[i] = 7777; |
| 480 | |
| 481 | |
| 482 | file->private_data = mxt; |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | int debug_data_release(struct inode *inode, struct file *file) |
| 487 | { |
| 488 | struct mxt_data *mxt; |
| 489 | mxt = file->private_data; |
| 490 | kfree(mxt->debug_data); |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static struct file_operations delta_fops = { |
| 495 | .owner = THIS_MODULE, |
| 496 | .open = debug_data_open, |
| 497 | .release = debug_data_release, |
| 498 | .read = deltas_read, |
| 499 | }; |
| 500 | |
| 501 | static struct file_operations refs_fops = { |
| 502 | .owner = THIS_MODULE, |
| 503 | .open = debug_data_open, |
| 504 | .release = debug_data_release, |
| 505 | .read = refs_read, |
| 506 | }; |
| 507 | |
| 508 | |
| 509 | int mxt_memory_open(struct inode *inode, struct file *file) |
| 510 | { |
| 511 | struct mxt_data *mxt; |
| 512 | mxt = container_of(inode->i_cdev, struct mxt_data, cdev); |
| 513 | if (mxt == NULL) |
| 514 | return -EIO; |
| 515 | file->private_data = mxt; |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | int mxt_message_open(struct inode *inode, struct file *file) |
| 520 | { |
| 521 | struct mxt_data *mxt; |
| 522 | mxt = container_of(inode->i_cdev, struct mxt_data, cdev_messages); |
| 523 | if (mxt == NULL) |
| 524 | return -EIO; |
| 525 | file->private_data = mxt; |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | ssize_t mxt_memory_read(struct file *file, char *buf, size_t count, |
| 531 | loff_t *ppos) |
| 532 | { |
| 533 | int i; |
| 534 | struct mxt_data *mxt; |
| 535 | |
| 536 | mxt = file->private_data; |
| 537 | if (mxt->valid_ap){ |
| 538 | mxt_debug(DEBUG_TRACE, "Reading %d bytes from current ap\n", |
| 539 | (int) count); |
| 540 | i = mxt_read_block_wo_addr(mxt->client, count, (u8 *) buf); |
| 541 | } else { |
| 542 | mxt_debug(DEBUG_TRACE, "Address pointer changed since set;" |
| 543 | "writing AP (%d) before reading %d bytes", |
| 544 | mxt->address_pointer, (int) count); |
| 545 | i = mxt_read_block(mxt->client, mxt->address_pointer, count, |
| 546 | buf); |
| 547 | } |
| 548 | |
| 549 | return i; |
| 550 | } |
| 551 | |
| 552 | ssize_t mxt_memory_write(struct file *file, const char *buf, size_t count, |
| 553 | loff_t *ppos) |
| 554 | { |
| 555 | int i; |
| 556 | int whole_blocks; |
| 557 | int last_block_size; |
| 558 | struct mxt_data *mxt; |
| 559 | u16 address; |
| 560 | |
| 561 | mxt = file->private_data; |
| 562 | address = mxt->address_pointer; |
| 563 | |
| 564 | mxt_debug(DEBUG_TRACE, "mxt_memory_write entered\n"); |
| 565 | whole_blocks = count / I2C_PAYLOAD_SIZE; |
| 566 | last_block_size = count % I2C_PAYLOAD_SIZE; |
| 567 | |
| 568 | for (i = 0; i < whole_blocks; i++) { |
| 569 | mxt_debug(DEBUG_TRACE, "About to write to %d...", |
| 570 | address); |
| 571 | mxt_write_block(mxt->client, address, I2C_PAYLOAD_SIZE, |
| 572 | (u8 *) buf); |
| 573 | address += I2C_PAYLOAD_SIZE; |
| 574 | buf += I2C_PAYLOAD_SIZE; |
| 575 | } |
| 576 | |
| 577 | mxt_write_block(mxt->client, address, last_block_size, (u8 *) buf); |
| 578 | |
| 579 | return count; |
| 580 | } |
| 581 | |
| 582 | static long mxt_ioctl(struct file *file, |
| 583 | unsigned int cmd, unsigned long arg) |
| 584 | { |
| 585 | int retval; |
| 586 | struct mxt_data *mxt; |
| 587 | |
| 588 | retval = 0; |
| 589 | mxt = file->private_data; |
| 590 | |
| 591 | switch (cmd) { |
| 592 | case MXT_SET_ADDRESS_IOCTL: |
| 593 | retval = mxt_write_ap(mxt, (u16) arg); |
| 594 | if (retval >= 0) { |
| 595 | mxt->address_pointer = (u16) arg; |
| 596 | mxt->valid_ap = 1; |
| 597 | } |
| 598 | break; |
| 599 | case MXT_RESET_IOCTL: |
| 600 | retval = mxt_write_byte(mxt->client, |
| 601 | MXT_BASE_ADDR(MXT_GEN_COMMANDPROCESSOR_T6, mxt) + |
| 602 | MXT_ADR_T6_RESET, |
| 603 | 1); |
| 604 | break; |
| 605 | case MXT_CALIBRATE_IOCTL: |
| 606 | retval = mxt_write_byte(mxt->client, |
| 607 | MXT_BASE_ADDR(MXT_GEN_COMMANDPROCESSOR_T6, mxt) + |
| 608 | MXT_ADR_T6_CALIBRATE, |
| 609 | 1); |
| 610 | |
| 611 | break; |
| 612 | case MXT_BACKUP_IOCTL: |
| 613 | retval = mxt_write_byte(mxt->client, |
| 614 | MXT_BASE_ADDR(MXT_GEN_COMMANDPROCESSOR_T6, mxt) + |
| 615 | MXT_ADR_T6_BACKUPNV, |
| 616 | MXT_CMD_T6_BACKUP); |
| 617 | break; |
| 618 | case MXT_NONTOUCH_MSG_IOCTL: |
| 619 | mxt->nontouch_msg_only = 1; |
| 620 | break; |
| 621 | case MXT_ALL_MSG_IOCTL: |
| 622 | mxt->nontouch_msg_only = 0; |
| 623 | break; |
| 624 | default: |
| 625 | return -EIO; |
| 626 | } |
| 627 | |
| 628 | return retval; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Copies messages from buffer to user space. |
| 633 | * |
| 634 | * NOTE: if less than (mxt->message_size * 5 + 1) bytes requested, |
| 635 | * this will return 0! |
| 636 | * |
| 637 | */ |
| 638 | ssize_t mxt_message_read(struct file *file, char *buf, size_t count, |
| 639 | loff_t *ppos) |
| 640 | { |
| 641 | int i; |
| 642 | struct mxt_data *mxt; |
| 643 | char *buf_start; |
| 644 | |
| 645 | mxt = file->private_data; |
| 646 | if (mxt == NULL) |
| 647 | return -EIO; |
| 648 | buf_start = buf; |
| 649 | |
| 650 | mutex_lock(&mxt->msg_mutex); |
| 651 | /* Copy messages until buffer empty, or 'count' bytes written */ |
| 652 | while ((mxt->msg_buffer_startp != mxt->msg_buffer_endp) && |
| 653 | ((buf - buf_start) < (count - (5 * mxt->message_size) - 1))){ |
| 654 | |
| 655 | for (i = 0; i < mxt->message_size; i++){ |
| 656 | buf += sprintf(buf, "[%2X] ", |
| 657 | *(mxt->messages + mxt->msg_buffer_endp * |
| 658 | mxt->message_size + i)); |
| 659 | } |
| 660 | buf += sprintf(buf, "\n"); |
| 661 | if (mxt->msg_buffer_endp < MXT_MESSAGE_BUFFER_SIZE) |
| 662 | mxt->msg_buffer_endp++; |
| 663 | else |
| 664 | mxt->msg_buffer_endp = 0; |
| 665 | } |
| 666 | mutex_unlock(&mxt->msg_mutex); |
| 667 | return (buf - buf_start); |
| 668 | } |
| 669 | |
| 670 | static struct file_operations mxt_message_fops = { |
| 671 | .owner = THIS_MODULE, |
| 672 | .open = mxt_message_open, |
| 673 | .read = mxt_message_read, |
| 674 | }; |
| 675 | |
| 676 | static struct file_operations mxt_memory_fops = { |
| 677 | .owner = THIS_MODULE, |
| 678 | .open = mxt_memory_open, |
| 679 | .read = mxt_memory_read, |
| 680 | .write = mxt_memory_write, |
| 681 | .unlocked_ioctl = mxt_ioctl, |
| 682 | }; |
| 683 | |
| 684 | |
| 685 | /* Writes the address pointer (to set up following reads). */ |
| 686 | |
| 687 | int mxt_write_ap(struct mxt_data *mxt, u16 ap) |
| 688 | { |
| 689 | struct i2c_client *client; |
| 690 | __le16 le_ap = cpu_to_le16(ap); |
| 691 | client = mxt->client; |
| 692 | if (mxt != NULL) |
| 693 | mxt->last_read_addr = -1; |
| 694 | if (i2c_master_send(client, (u8 *) &le_ap, 2) == 2) { |
| 695 | mxt_debug(DEBUG_TRACE, "Address pointer set to %d\n", ap); |
| 696 | return 0; |
| 697 | } else { |
| 698 | mxt_debug(DEBUG_INFO, "Error writing address pointer!\n"); |
| 699 | return -EIO; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | |
| 704 | |
| 705 | /* Calculates the 24-bit CRC sum. */ |
| 706 | static u32 CRC_24(u32 crc, u8 byte1, u8 byte2) |
| 707 | { |
| 708 | static const u32 crcpoly = 0x80001B; |
| 709 | u32 result; |
| 710 | u32 data_word; |
| 711 | |
| 712 | data_word = ((((u16) byte2) << 8u) | byte1); |
| 713 | result = ((crc << 1u) ^ data_word); |
| 714 | if (result & 0x1000000) |
| 715 | result ^= crcpoly; |
| 716 | return result; |
| 717 | } |
| 718 | |
| 719 | /* Returns object address in mXT chip, or zero if object is not found */ |
| 720 | static u16 get_object_address(uint8_t object_type, |
| 721 | uint8_t instance, |
| 722 | struct mxt_object *object_table, |
| 723 | int max_objs) |
| 724 | { |
| 725 | uint8_t object_table_index = 0; |
| 726 | uint8_t address_found = 0; |
| 727 | uint16_t address = 0; |
| 728 | struct mxt_object *obj; |
| 729 | |
| 730 | while ((object_table_index < max_objs) && !address_found) { |
| 731 | obj = &object_table[object_table_index]; |
| 732 | if (obj->type == object_type) { |
| 733 | address_found = 1; |
| 734 | /* Are there enough instances defined in the FW? */ |
| 735 | if (obj->instances >= instance) { |
| 736 | address = obj->chip_addr + |
| 737 | (obj->size + 1) * instance; |
| 738 | } else { |
| 739 | return 0; |
| 740 | } |
| 741 | } |
| 742 | object_table_index++; |
| 743 | } |
| 744 | return address; |
| 745 | } |
| 746 | |
| 747 | |
| 748 | /* |
| 749 | * Reads a block of bytes from given address from mXT chip. If we are |
| 750 | * reading from message window, and previous read was from message window, |
| 751 | * there's no need to write the address pointer: the mXT chip will |
| 752 | * automatically set the address pointer back to message window start. |
| 753 | */ |
| 754 | |
| 755 | static int mxt_read_block(struct i2c_client *client, |
| 756 | u16 addr, |
| 757 | u16 length, |
| 758 | u8 *value) |
| 759 | { |
| 760 | struct i2c_adapter *adapter = client->adapter; |
| 761 | struct i2c_msg msg[2]; |
| 762 | __le16 le_addr; |
| 763 | struct mxt_data *mxt; |
| 764 | |
| 765 | mxt = i2c_get_clientdata(client); |
| 766 | |
| 767 | if (mxt != NULL) { |
| 768 | if ((mxt->last_read_addr == addr) && |
| 769 | (addr == mxt->msg_proc_addr)) { |
| 770 | if (i2c_master_recv(client, value, length) == length) |
| 771 | return length; |
| 772 | else |
| 773 | return -EIO; |
| 774 | } else { |
| 775 | mxt->last_read_addr = addr; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | mxt_debug(DEBUG_TRACE, "Writing address pointer & reading %d bytes " |
| 780 | "in on i2c transaction...\n", length); |
| 781 | |
| 782 | le_addr = cpu_to_le16(addr); |
| 783 | msg[0].addr = client->addr; |
| 784 | msg[0].flags = 0x00; |
| 785 | msg[0].len = 2; |
| 786 | msg[0].buf = (u8 *) &le_addr; |
| 787 | |
| 788 | msg[1].addr = client->addr; |
| 789 | msg[1].flags = I2C_M_RD; |
| 790 | msg[1].len = length; |
| 791 | msg[1].buf = (u8 *) value; |
| 792 | if (i2c_transfer(adapter, msg, 2) == 2) |
| 793 | return length; |
| 794 | else |
| 795 | return -EIO; |
| 796 | |
| 797 | } |
| 798 | |
| 799 | /* Reads a block of bytes from current address from mXT chip. */ |
| 800 | |
| 801 | static int mxt_read_block_wo_addr(struct i2c_client *client, |
| 802 | u16 length, |
| 803 | u8 *value) |
| 804 | { |
| 805 | |
| 806 | |
| 807 | if (i2c_master_recv(client, value, length) == length) { |
| 808 | mxt_debug(DEBUG_TRACE, "I2C block read ok\n"); |
| 809 | return length; |
| 810 | } else { |
| 811 | mxt_debug(DEBUG_INFO, "I2C block read failed\n"); |
| 812 | return -EIO; |
| 813 | } |
| 814 | |
| 815 | } |
| 816 | |
| 817 | |
| 818 | /* Writes one byte to given address in mXT chip. */ |
| 819 | |
| 820 | static int mxt_write_byte(struct i2c_client *client, u16 addr, u8 value) |
| 821 | { |
| 822 | struct { |
| 823 | __le16 le_addr; |
| 824 | u8 data; |
| 825 | |
| 826 | } i2c_byte_transfer; |
| 827 | |
| 828 | struct mxt_data *mxt; |
| 829 | |
| 830 | mxt = i2c_get_clientdata(client); |
| 831 | if (mxt != NULL) |
| 832 | mxt->last_read_addr = -1; |
| 833 | i2c_byte_transfer.le_addr = cpu_to_le16(addr); |
| 834 | i2c_byte_transfer.data = value; |
| 835 | if (i2c_master_send(client, (u8 *) &i2c_byte_transfer, 3) == 3) |
| 836 | return 0; |
| 837 | else |
| 838 | return -EIO; |
| 839 | } |
| 840 | |
| 841 | |
| 842 | /* Writes a block of bytes (max 256) to given address in mXT chip. */ |
| 843 | static int mxt_write_block(struct i2c_client *client, |
| 844 | u16 addr, |
| 845 | u16 length, |
| 846 | u8 *value) |
| 847 | { |
| 848 | int i; |
| 849 | struct { |
| 850 | __le16 le_addr; |
| 851 | u8 data[256]; |
| 852 | |
| 853 | } i2c_block_transfer; |
| 854 | |
| 855 | struct mxt_data *mxt; |
| 856 | |
| 857 | mxt_debug(DEBUG_TRACE, "Writing %d bytes to %d...", length, addr); |
| 858 | if (length > 256) |
| 859 | return -EINVAL; |
| 860 | mxt = i2c_get_clientdata(client); |
| 861 | if (mxt != NULL) |
| 862 | mxt->last_read_addr = -1; |
| 863 | for (i = 0; i < length; i++) |
| 864 | i2c_block_transfer.data[i] = *value++; |
| 865 | i2c_block_transfer.le_addr = cpu_to_le16(addr); |
| 866 | i = i2c_master_send(client, (u8 *) &i2c_block_transfer, length + 2); |
| 867 | if (i == (length + 2)) |
| 868 | return length; |
| 869 | else |
| 870 | return -EIO; |
| 871 | } |
| 872 | |
| 873 | /* Calculates the CRC value for mXT infoblock. */ |
| 874 | int calculate_infoblock_crc(u32 *crc_result, u8 *data, int crc_area_size) |
| 875 | { |
| 876 | u32 crc = 0; |
| 877 | int i; |
| 878 | |
| 879 | for (i = 0; i < (crc_area_size - 1); i = i + 2) |
| 880 | crc = CRC_24(crc, *(data + i), *(data + i + 1)); |
| 881 | /* If uneven size, pad with zero */ |
| 882 | if (crc_area_size & 0x0001) |
| 883 | crc = CRC_24(crc, *(data + i), 0); |
| 884 | /* Return only 24 bits of CRC. */ |
| 885 | *crc_result = (crc & 0x00FFFFFF); |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | /* Processes a touchscreen message. */ |
| 891 | void process_T9_message(u8 *message, struct mxt_data *mxt, int last_touch) |
| 892 | { |
| 893 | |
| 894 | struct input_dev *input; |
| 895 | u8 status; |
| 896 | u16 xpos = 0xFFFF; |
| 897 | u16 ypos = 0xFFFF; |
| 898 | u8 touch_size = 255; |
| 899 | u8 touch_number; |
| 900 | u8 amplitude; |
| 901 | u8 report_id; |
| 902 | |
| 903 | static int stored_size[10]; |
| 904 | static int stored_x[10]; |
| 905 | static int stored_y[10]; |
| 906 | int i; |
| 907 | int active_touches = 0; |
| 908 | /* |
| 909 | * If the 'last_touch' flag is set, we have received all the touch |
| 910 | * messages |
| 911 | * there are available in this cycle, so send the events for touches |
| 912 | * that are |
| 913 | * active. |
| 914 | */ |
| 915 | if (last_touch){ |
| 916 | /* TODO: For compatibility with single-touch systems, send ABS_X & |
| 917 | * ABS_Y */ |
| 918 | /* |
| 919 | if (stored_size[0]){ |
| 920 | input_report_abs(mxt->input, ABS_X, stored_x[0]); |
| 921 | input_report_abs(mxt->input, ABS_Y, stored_y[0]); |
| 922 | }*/ |
| 923 | |
| 924 | |
| 925 | for (i = 0; i < 10; i++){ |
| 926 | if (stored_size[i]){ |
| 927 | active_touches++; |
| 928 | input_report_abs(mxt->input, |
| 929 | ABS_MT_TRACKING_ID, |
| 930 | i); |
| 931 | input_report_abs(mxt->input, |
| 932 | ABS_MT_TOUCH_MAJOR, |
| 933 | stored_size[i]); |
| 934 | input_report_abs(mxt->input, |
| 935 | ABS_MT_POSITION_X, |
| 936 | stored_x[i]); |
| 937 | input_report_abs(mxt->input, |
| 938 | ABS_MT_POSITION_Y, |
| 939 | stored_y[i]); |
| 940 | input_mt_sync(mxt->input); |
| 941 | } |
| 942 | } |
Mohan Pallaka | a9df12a | 2011-11-18 16:34:24 +0530 | [diff] [blame] | 943 | input_report_key(mxt->input, BTN_TOUCH, !!active_touches); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 944 | if (active_touches == 0) |
| 945 | input_mt_sync(mxt->input); |
| 946 | input_sync(mxt->input); |
| 947 | |
| 948 | }else{ |
| 949 | |
| 950 | input = mxt->input; |
| 951 | status = message[MXT_MSG_T9_STATUS]; |
| 952 | report_id = message[0]; |
| 953 | |
| 954 | if (status & MXT_MSGB_T9_SUPPRESS) { |
| 955 | /* Touch has been suppressed by grip/face */ |
| 956 | /* detection */ |
| 957 | mxt_debug(DEBUG_TRACE, "SUPRESS"); |
| 958 | } else { |
| 959 | /* Put together the 10-/12-bit coordinate values. */ |
| 960 | xpos = message[MXT_MSG_T9_XPOSMSB] * 16 + |
| 961 | ((message[MXT_MSG_T9_XYPOSLSB] >> 4) & 0xF); |
| 962 | ypos = message[MXT_MSG_T9_YPOSMSB] * 16 + |
| 963 | ((message[MXT_MSG_T9_XYPOSLSB] >> 0) & 0xF); |
| 964 | |
| 965 | if (mxt->max_x_val < 1024) |
| 966 | xpos >>= 2; |
| 967 | if (mxt->max_y_val < 1024) |
| 968 | ypos >>= 2; |
| 969 | |
| 970 | touch_number = message[MXT_MSG_REPORTID] - |
| 971 | mxt->rid_map[report_id].first_rid; |
| 972 | |
| 973 | stored_x[touch_number] = xpos; |
| 974 | stored_y[touch_number] = ypos; |
| 975 | |
| 976 | if (status & MXT_MSGB_T9_DETECT) { |
| 977 | /* |
| 978 | * mXT224 reports the number of touched nodes, |
| 979 | * so the exact value for touch ellipse major |
| 980 | * axis length in nodes would be 2*sqrt(touch_size/pi) |
| 981 | * (assuming round touch shape), which would then need |
| 982 | * to be scaled using information about how many sensor |
| 983 | * lines we do have. So this is very much simplified, |
| 984 | * but sufficient for most if not all apps? |
| 985 | */ |
| 986 | touch_size = message[MXT_MSG_T9_TCHAREA]; |
| 987 | touch_size = touch_size >> 2; |
| 988 | if (!touch_size) |
| 989 | touch_size = 1; |
| 990 | /* |
| 991 | * report_mt(touch_number, touch_size, xpos, ypos, mxt); |
| 992 | */ |
| 993 | |
| 994 | stored_size[touch_number] = touch_size; |
| 995 | |
| 996 | if (status & MXT_MSGB_T9_AMP) |
| 997 | /* Amplitude of touch has changed */ |
| 998 | amplitude = message[MXT_MSG_T9_TCHAMPLITUDE]; |
| 999 | } |
| 1000 | |
| 1001 | if (status & MXT_MSGB_T9_RELEASE) { |
| 1002 | /* The previously reported touch has been removed.*/ |
| 1003 | /* report_mt(touch_number, 0, xpos, ypos, mxt); */ |
| 1004 | stored_size[touch_number] = 0; |
| 1005 | } |
| 1006 | |
| 1007 | /* input_sync(input); */ |
| 1008 | } |
| 1009 | |
| 1010 | if (status & MXT_MSGB_T9_SUPPRESS) { |
| 1011 | mxt_debug(DEBUG_TRACE, "SUPRESS"); |
| 1012 | } else { |
| 1013 | if (status & MXT_MSGB_T9_DETECT) { |
| 1014 | mxt_debug(DEBUG_TRACE, "DETECT:%s%s%s%s", |
| 1015 | ((status & MXT_MSGB_T9_PRESS) ? " PRESS" : ""), |
| 1016 | ((status & MXT_MSGB_T9_MOVE) ? " MOVE" : ""), |
| 1017 | ((status & MXT_MSGB_T9_AMP) ? " AMP" : ""), |
| 1018 | ((status & MXT_MSGB_T9_VECTOR) ? " VECT" : "")); |
| 1019 | |
| 1020 | } else if (status & MXT_MSGB_T9_RELEASE) { |
| 1021 | mxt_debug(DEBUG_TRACE, "RELEASE"); |
| 1022 | } |
| 1023 | } |
| 1024 | mxt_debug(DEBUG_TRACE, "X=%d, Y=%d, TOUCHSIZE=%d", |
| 1025 | xpos, ypos, touch_size); |
| 1026 | } |
| 1027 | return; |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | int process_message(u8 *message, u8 object, struct mxt_data *mxt) |
| 1032 | { |
| 1033 | struct i2c_client *client; |
| 1034 | u8 status; |
| 1035 | u16 xpos = 0xFFFF; |
| 1036 | u16 ypos = 0xFFFF; |
| 1037 | u8 event; |
| 1038 | u8 direction; |
| 1039 | u16 distance; |
| 1040 | u8 length; |
| 1041 | u8 report_id; |
| 1042 | static u8 error_cond = 0; |
| 1043 | |
| 1044 | client = mxt->client; |
| 1045 | length = mxt->message_size; |
| 1046 | report_id = message[0]; |
| 1047 | |
| 1048 | if ((mxt->nontouch_msg_only == 0) || |
| 1049 | (!IS_TOUCH_OBJECT(object))){ |
| 1050 | mutex_lock(&mxt->msg_mutex); |
| 1051 | /* Copy the message to buffer */ |
| 1052 | if (mxt->msg_buffer_startp < MXT_MESSAGE_BUFFER_SIZE) { |
| 1053 | mxt->msg_buffer_startp++; |
| 1054 | } else { |
| 1055 | mxt->msg_buffer_startp = 0; |
| 1056 | } |
| 1057 | |
| 1058 | if (mxt->msg_buffer_startp == mxt->msg_buffer_endp) { |
| 1059 | mxt_debug(DEBUG_TRACE, |
| 1060 | "Message buf full, discarding last entry.\n"); |
| 1061 | if (mxt->msg_buffer_endp < MXT_MESSAGE_BUFFER_SIZE) { |
| 1062 | mxt->msg_buffer_endp++; |
| 1063 | } else { |
| 1064 | mxt->msg_buffer_endp = 0; |
| 1065 | } |
| 1066 | } |
| 1067 | memcpy((mxt->messages + mxt->msg_buffer_startp * length), |
| 1068 | message, |
| 1069 | length); |
| 1070 | mutex_unlock(&mxt->msg_mutex); |
| 1071 | } |
| 1072 | |
| 1073 | switch (object) { |
| 1074 | case MXT_GEN_COMMANDPROCESSOR_T6: |
| 1075 | status = message[1]; |
| 1076 | |
| 1077 | if (status & MXT_MSGB_T6_COMSERR) { |
| 1078 | if ((!error_cond) & MXT_MSGB_T6_COMSERR){ |
| 1079 | dev_err(&client->dev, |
| 1080 | "maXTouch checksum error\n"); |
| 1081 | error_cond |= MXT_MSGB_T6_COMSERR; |
| 1082 | } |
| 1083 | } |
| 1084 | if (status & MXT_MSGB_T6_CFGERR) { |
| 1085 | /* |
| 1086 | * Configuration error. A proper configuration |
| 1087 | * needs to be written to chip and backed up. |
| 1088 | */ |
| 1089 | if ((!error_cond) & MXT_MSGB_T6_CFGERR){ |
| 1090 | dev_err(&client->dev, |
| 1091 | "maXTouch configuration error\n"); |
| 1092 | error_cond |= MXT_MSGB_T6_CFGERR; |
| 1093 | } |
| 1094 | } |
| 1095 | if (status & MXT_MSGB_T6_CAL) { |
| 1096 | /* Calibration in action, no need to react */ |
| 1097 | dev_dbg(&client->dev, |
| 1098 | "maXTouch calibration in progress\n"); |
| 1099 | } |
| 1100 | if (status & MXT_MSGB_T6_SIGERR) { |
| 1101 | /* |
| 1102 | * Signal acquisition error, something is seriously |
| 1103 | * wrong, not much we can in the driver to correct |
| 1104 | * this |
| 1105 | */ |
| 1106 | if ((!error_cond) & MXT_MSGB_T6_SIGERR){ |
| 1107 | dev_err(&client->dev, |
| 1108 | "maXTouch acquisition error\n"); |
| 1109 | error_cond |= MXT_MSGB_T6_SIGERR; |
| 1110 | } |
| 1111 | } |
| 1112 | if (status & MXT_MSGB_T6_OFL) { |
| 1113 | /* |
| 1114 | * Cycle overflow, the acquisition interval is too |
| 1115 | * short. |
| 1116 | */ |
| 1117 | dev_err(&client->dev, |
| 1118 | "maXTouch cycle overflow\n"); |
| 1119 | } |
| 1120 | if (status & MXT_MSGB_T6_RESET) { |
| 1121 | /* Chip has reseted, no need to react. */ |
| 1122 | dev_dbg(&client->dev, |
| 1123 | "maXTouch chip reset\n"); |
| 1124 | } |
| 1125 | if (status == 0) { |
| 1126 | /* Chip status back to normal. */ |
| 1127 | dev_dbg(&client->dev, |
| 1128 | "maXTouch status normal\n"); |
| 1129 | error_cond = 0; |
| 1130 | } |
| 1131 | break; |
| 1132 | |
| 1133 | case MXT_TOUCH_MULTITOUCHSCREEN_T9: |
| 1134 | process_T9_message(message, mxt, 0); |
| 1135 | break; |
| 1136 | |
| 1137 | case MXT_SPT_GPIOPWM_T19: |
| 1138 | if (debug >= DEBUG_TRACE) |
| 1139 | dev_info(&client->dev, |
| 1140 | "Receiving GPIO message\n"); |
| 1141 | break; |
| 1142 | |
| 1143 | case MXT_PROCI_GRIPFACESUPPRESSION_T20: |
| 1144 | if (debug >= DEBUG_TRACE) |
| 1145 | dev_info(&client->dev, |
| 1146 | "Receiving face suppression msg\n"); |
| 1147 | break; |
| 1148 | |
| 1149 | case MXT_PROCG_NOISESUPPRESSION_T22: |
| 1150 | if (debug >= DEBUG_TRACE) |
| 1151 | dev_info(&client->dev, |
| 1152 | "Receiving noise suppression msg\n"); |
| 1153 | status = message[MXT_MSG_T22_STATUS]; |
| 1154 | if (status & MXT_MSGB_T22_FHCHG) { |
| 1155 | if (debug >= DEBUG_TRACE) |
| 1156 | dev_info(&client->dev, |
| 1157 | "maXTouch: Freq changed\n"); |
| 1158 | } |
| 1159 | if (status & MXT_MSGB_T22_GCAFERR) { |
| 1160 | if (debug >= DEBUG_TRACE) |
| 1161 | dev_info(&client->dev, |
| 1162 | "maXTouch: High noise " |
| 1163 | "level\n"); |
| 1164 | } |
| 1165 | if (status & MXT_MSGB_T22_FHERR) { |
| 1166 | if (debug >= DEBUG_TRACE) |
| 1167 | dev_info(&client->dev, |
| 1168 | "maXTouch: Freq changed - " |
| 1169 | "Noise level too high\n"); |
| 1170 | } |
| 1171 | break; |
| 1172 | |
| 1173 | case MXT_PROCI_ONETOUCHGESTUREPROCESSOR_T24: |
| 1174 | if (debug >= DEBUG_TRACE) |
| 1175 | dev_info(&client->dev, |
| 1176 | "Receiving one-touch gesture msg\n"); |
| 1177 | |
| 1178 | event = message[MXT_MSG_T24_STATUS] & 0x0F; |
| 1179 | xpos = message[MXT_MSG_T24_XPOSMSB] * 16 + |
| 1180 | ((message[MXT_MSG_T24_XYPOSLSB] >> 4) & 0x0F); |
| 1181 | ypos = message[MXT_MSG_T24_YPOSMSB] * 16 + |
| 1182 | ((message[MXT_MSG_T24_XYPOSLSB] >> 0) & 0x0F); |
| 1183 | if (mxt->max_x_val < 1024) |
| 1184 | xpos >>= 2; |
| 1185 | if (mxt->max_y_val < 1024) |
| 1186 | ypos >>= 2; |
| 1187 | direction = message[MXT_MSG_T24_DIR]; |
| 1188 | distance = message[MXT_MSG_T24_DIST] + |
| 1189 | (message[MXT_MSG_T24_DIST + 1] << 16); |
| 1190 | |
| 1191 | report_gesture((event << 24) | (direction << 16) | distance, |
| 1192 | mxt); |
| 1193 | report_gesture((xpos << 16) | ypos, mxt); |
| 1194 | |
| 1195 | break; |
| 1196 | |
| 1197 | case MXT_SPT_SELFTEST_T25: |
| 1198 | if (debug >= DEBUG_TRACE) |
| 1199 | dev_info(&client->dev, |
| 1200 | "Receiving Self-Test msg\n"); |
| 1201 | |
| 1202 | if (message[MXT_MSG_T25_STATUS] == MXT_MSGR_T25_OK) { |
| 1203 | if (debug >= DEBUG_TRACE) |
| 1204 | dev_info(&client->dev, |
| 1205 | "maXTouch: Self-Test OK\n"); |
| 1206 | |
| 1207 | } else { |
| 1208 | dev_err(&client->dev, |
| 1209 | "maXTouch: Self-Test Failed [%02x]:" |
| 1210 | "{%02x,%02x,%02x,%02x,%02x}\n", |
| 1211 | message[MXT_MSG_T25_STATUS], |
| 1212 | message[MXT_MSG_T25_STATUS + 0], |
| 1213 | message[MXT_MSG_T25_STATUS + 1], |
| 1214 | message[MXT_MSG_T25_STATUS + 2], |
| 1215 | message[MXT_MSG_T25_STATUS + 3], |
| 1216 | message[MXT_MSG_T25_STATUS + 4] |
| 1217 | ); |
| 1218 | } |
| 1219 | break; |
| 1220 | |
| 1221 | case MXT_PROCI_TWOTOUCHGESTUREPROCESSOR_T27: |
| 1222 | if (debug >= DEBUG_TRACE) |
| 1223 | dev_info(&client->dev, |
| 1224 | "Receiving 2-touch gesture message\n"); |
| 1225 | |
| 1226 | event = message[MXT_MSG_T27_STATUS] & 0xF0; |
| 1227 | xpos = message[MXT_MSG_T27_XPOSMSB] * 16 + |
| 1228 | ((message[MXT_MSG_T27_XYPOSLSB] >> 4) & 0x0F); |
| 1229 | ypos = message[MXT_MSG_T27_YPOSMSB] * 16 + |
| 1230 | ((message[MXT_MSG_T27_XYPOSLSB] >> 0) & 0x0F); |
| 1231 | if (mxt->max_x_val < 1024) |
| 1232 | xpos >>= 2; |
| 1233 | if (mxt->max_y_val < 1024) |
| 1234 | ypos >>= 2; |
| 1235 | |
| 1236 | direction = message[MXT_MSG_T27_ANGLE]; |
| 1237 | distance = message[MXT_MSG_T27_SEPARATION] + |
| 1238 | (message[MXT_MSG_T27_SEPARATION + 1] << 16); |
| 1239 | |
| 1240 | report_gesture((event << 24) | (direction << 16) | distance, |
| 1241 | mxt); |
| 1242 | report_gesture((xpos << 16) | ypos, mxt); |
| 1243 | |
| 1244 | |
| 1245 | break; |
| 1246 | |
| 1247 | case MXT_SPT_CTECONFIG_T28: |
| 1248 | if (debug >= DEBUG_TRACE) |
| 1249 | dev_info(&client->dev, |
| 1250 | "Receiving CTE message...\n"); |
| 1251 | status = message[MXT_MSG_T28_STATUS]; |
| 1252 | if (status & MXT_MSGB_T28_CHKERR) |
| 1253 | dev_err(&client->dev, |
| 1254 | "maXTouch: Power-Up CRC failure\n"); |
| 1255 | |
| 1256 | break; |
| 1257 | default: |
| 1258 | if (debug >= DEBUG_TRACE) |
| 1259 | dev_info(&client->dev, |
| 1260 | "maXTouch: Unknown message!\n"); |
| 1261 | |
| 1262 | break; |
| 1263 | } |
| 1264 | |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | |
| 1269 | /* |
| 1270 | * Processes messages when the interrupt line (CHG) is asserted. Keeps |
| 1271 | * reading messages until a message with report ID 0xFF is received, |
| 1272 | * which indicates that there is no more new messages. |
| 1273 | * |
| 1274 | */ |
| 1275 | |
| 1276 | static void mxt_worker(struct work_struct *work) |
| 1277 | { |
| 1278 | struct mxt_data *mxt; |
| 1279 | struct i2c_client *client; |
| 1280 | |
| 1281 | u8 *message; |
| 1282 | u16 message_length; |
| 1283 | u16 message_addr; |
| 1284 | u8 report_id; |
| 1285 | u8 object; |
| 1286 | int error; |
| 1287 | int i; |
| 1288 | char *message_string; |
| 1289 | char *message_start; |
| 1290 | |
| 1291 | message = NULL; |
| 1292 | mxt = container_of(work, struct mxt_data, dwork.work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1293 | client = mxt->client; |
| 1294 | message_addr = mxt->msg_proc_addr; |
| 1295 | message_length = mxt->message_size; |
| 1296 | |
| 1297 | if (message_length < 256) { |
| 1298 | message = kmalloc(message_length, GFP_KERNEL); |
| 1299 | if (message == NULL) { |
| 1300 | dev_err(&client->dev, "Error allocating memory\n"); |
Anirudh Ghayal | 9ffded2 | 2011-09-20 09:00:00 +0530 | [diff] [blame] | 1301 | goto fail_worker; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1302 | } |
| 1303 | } else { |
| 1304 | dev_err(&client->dev, |
| 1305 | "Message length larger than 256 bytes not supported\n"); |
Anirudh Ghayal | 9ffded2 | 2011-09-20 09:00:00 +0530 | [diff] [blame] | 1306 | goto fail_worker; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Anirudh Ghayal | 9ffded2 | 2011-09-20 09:00:00 +0530 | [diff] [blame] | 1309 | mxt_debug(DEBUG_TRACE, "maXTouch worker active:\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1310 | |
| 1311 | do { |
| 1312 | /* Read next message, reread on failure. */ |
| 1313 | /* TODO: message length, CRC included? */ |
| 1314 | mxt->message_counter++; |
| 1315 | for (i = 1; i < I2C_RETRY_COUNT; i++) { |
| 1316 | error = mxt_read_block(client, |
| 1317 | message_addr, |
| 1318 | message_length - 1, |
| 1319 | message); |
| 1320 | if (error >= 0) |
| 1321 | break; |
| 1322 | mxt->read_fail_counter++; |
| 1323 | dev_err(&client->dev, |
| 1324 | "Failure reading maxTouch device\n"); |
| 1325 | } |
| 1326 | if (error < 0) { |
| 1327 | kfree(message); |
Anirudh Ghayal | 9ffded2 | 2011-09-20 09:00:00 +0530 | [diff] [blame] | 1328 | goto fail_worker; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | if (mxt->address_pointer != message_addr) |
| 1332 | mxt->valid_ap = 0; |
| 1333 | report_id = message[0]; |
| 1334 | |
| 1335 | if (debug >= DEBUG_RAW) { |
| 1336 | mxt_debug(DEBUG_RAW, "%s message [msg count: %08x]:", |
| 1337 | REPORT_ID_TO_OBJECT_NAME(report_id, mxt), |
| 1338 | mxt->message_counter |
| 1339 | ); |
| 1340 | /* 5 characters per one byte */ |
| 1341 | message_string = kmalloc(message_length * 5, |
| 1342 | GFP_KERNEL); |
| 1343 | if (message_string == NULL) { |
| 1344 | dev_err(&client->dev, |
| 1345 | "Error allocating memory\n"); |
| 1346 | kfree(message); |
Anirudh Ghayal | 9ffded2 | 2011-09-20 09:00:00 +0530 | [diff] [blame] | 1347 | goto fail_worker; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1348 | } |
| 1349 | message_start = message_string; |
| 1350 | for (i = 0; i < message_length; i++) { |
| 1351 | message_string += |
| 1352 | sprintf(message_string, |
| 1353 | "0x%02X ", message[i]); |
| 1354 | } |
| 1355 | mxt_debug(DEBUG_RAW, "%s", message_start); |
| 1356 | kfree(message_start); |
| 1357 | } |
| 1358 | |
| 1359 | if ((report_id != MXT_END_OF_MESSAGES) && (report_id != 0)) { |
| 1360 | memcpy(mxt->last_message, message, message_length); |
| 1361 | mxt->new_msgs = 1; |
| 1362 | smp_wmb(); |
| 1363 | /* Get type of object and process the message */ |
| 1364 | object = mxt->rid_map[report_id].object; |
| 1365 | process_message(message, object, mxt); |
| 1366 | } |
| 1367 | |
| 1368 | mxt_debug(DEBUG_TRACE, "chgline: %d\n", mxt->read_chg()); |
| 1369 | } while (comms ? (mxt->read_chg() == 0) : |
| 1370 | ((report_id != MXT_END_OF_MESSAGES) && (report_id != 0))); |
| 1371 | |
| 1372 | /* All messages processed, send the events) */ |
| 1373 | process_T9_message(NULL, mxt, 1); |
| 1374 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1375 | kfree(message); |
Anirudh Ghayal | 9ffded2 | 2011-09-20 09:00:00 +0530 | [diff] [blame] | 1376 | |
| 1377 | fail_worker: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1378 | /* Make sure we just didn't miss a interrupt. */ |
| 1379 | if (mxt->read_chg() == 0){ |
| 1380 | schedule_delayed_work(&mxt->dwork, 0); |
Anirudh Ghayal | 61d88f7 | 2011-10-17 11:28:02 +0530 | [diff] [blame] | 1381 | } else |
| 1382 | enable_irq(mxt->irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | |
| 1386 | /* |
| 1387 | * The maXTouch device will signal the host about a new message by asserting |
| 1388 | * the CHG line. This ISR schedules a worker routine to read the message when |
| 1389 | * that happens. |
| 1390 | */ |
| 1391 | |
| 1392 | static irqreturn_t mxt_irq_handler(int irq, void *_mxt) |
| 1393 | { |
| 1394 | struct mxt_data *mxt = _mxt; |
| 1395 | |
| 1396 | mxt->irq_counter++; |
| 1397 | if (mxt->valid_interrupt()) { |
| 1398 | /* Send the signal only if falling edge generated the irq. */ |
Anirudh Ghayal | 9ffded2 | 2011-09-20 09:00:00 +0530 | [diff] [blame] | 1399 | disable_irq_nosync(mxt->irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1400 | schedule_delayed_work(&mxt->dwork, 0); |
| 1401 | mxt->valid_irq_counter++; |
| 1402 | } else { |
| 1403 | mxt->invalid_irq_counter++; |
| 1404 | return IRQ_NONE; |
| 1405 | } |
| 1406 | |
| 1407 | return IRQ_HANDLED; |
| 1408 | } |
| 1409 | |
| 1410 | |
| 1411 | |
| 1412 | /******************************************************************************/ |
| 1413 | /* Initialization of driver */ |
| 1414 | /******************************************************************************/ |
| 1415 | |
| 1416 | static int __devinit mxt_identify(struct i2c_client *client, |
| 1417 | struct mxt_data *mxt, |
| 1418 | u8 *id_block_data) |
| 1419 | { |
| 1420 | u8 buf[MXT_ID_BLOCK_SIZE]; |
| 1421 | int error; |
| 1422 | int identified; |
| 1423 | |
| 1424 | identified = 0; |
| 1425 | |
| 1426 | /* Read Device info to check if chip is valid */ |
| 1427 | error = mxt_read_block(client, MXT_ADDR_INFO_BLOCK, MXT_ID_BLOCK_SIZE, |
| 1428 | (u8 *) buf); |
| 1429 | |
| 1430 | if (error < 0) { |
| 1431 | mxt->read_fail_counter++; |
| 1432 | dev_err(&client->dev, "Failure accessing maXTouch device\n"); |
| 1433 | return -EIO; |
| 1434 | } |
| 1435 | |
| 1436 | memcpy(id_block_data, buf, MXT_ID_BLOCK_SIZE); |
| 1437 | |
| 1438 | mxt->device_info.family_id = buf[0]; |
| 1439 | mxt->device_info.variant_id = buf[1]; |
| 1440 | mxt->device_info.major = ((buf[2] >> 4) & 0x0F); |
| 1441 | mxt->device_info.minor = (buf[2] & 0x0F); |
| 1442 | mxt->device_info.build = buf[3]; |
| 1443 | mxt->device_info.x_size = buf[4]; |
| 1444 | mxt->device_info.y_size = buf[5]; |
| 1445 | mxt->device_info.num_objs = buf[6]; |
| 1446 | mxt->device_info.num_nodes = mxt->device_info.x_size * |
| 1447 | mxt->device_info.y_size; |
| 1448 | |
| 1449 | /* |
| 1450 | * Check Family & Variant Info; warn if not recognized but |
| 1451 | * still continue. |
| 1452 | */ |
| 1453 | |
| 1454 | /* MXT224 */ |
| 1455 | if (mxt->device_info.family_id == MXT224_FAMILYID) { |
| 1456 | strcpy(mxt->device_info.family_name, "mXT224"); |
| 1457 | |
| 1458 | if (mxt->device_info.variant_id == MXT224_CAL_VARIANTID) { |
| 1459 | strcpy(mxt->device_info.variant_name, "Calibrated"); |
| 1460 | } else if (mxt->device_info.variant_id == |
| 1461 | MXT224_UNCAL_VARIANTID) { |
| 1462 | strcpy(mxt->device_info.variant_name, "Uncalibrated"); |
| 1463 | } else { |
| 1464 | dev_err(&client->dev, |
| 1465 | "Warning: maXTouch Variant ID [%d] not " |
| 1466 | "supported\n", |
| 1467 | mxt->device_info.variant_id); |
| 1468 | strcpy(mxt->device_info.variant_name, "UNKNOWN"); |
| 1469 | /* identified = -ENXIO; */ |
| 1470 | } |
| 1471 | |
| 1472 | /* MXT1386 */ |
| 1473 | } else if (mxt->device_info.family_id == MXT1386_FAMILYID) { |
| 1474 | strcpy(mxt->device_info.family_name, "mXT1386"); |
| 1475 | |
| 1476 | if (mxt->device_info.variant_id == MXT1386_CAL_VARIANTID) { |
| 1477 | strcpy(mxt->device_info.variant_name, "Calibrated"); |
| 1478 | } else { |
| 1479 | dev_err(&client->dev, |
| 1480 | "Warning: maXTouch Variant ID [%d] not " |
| 1481 | "supported\n", |
| 1482 | mxt->device_info.variant_id); |
| 1483 | strcpy(mxt->device_info.variant_name, "UNKNOWN"); |
| 1484 | /* identified = -ENXIO; */ |
| 1485 | } |
| 1486 | /* Unknown family ID! */ |
| 1487 | } else { |
| 1488 | dev_err(&client->dev, |
| 1489 | "Warning: maXTouch Family ID [%d] not supported\n", |
| 1490 | mxt->device_info.family_id); |
| 1491 | strcpy(mxt->device_info.family_name, "UNKNOWN"); |
| 1492 | strcpy(mxt->device_info.variant_name, "UNKNOWN"); |
| 1493 | /* identified = -ENXIO; */ |
| 1494 | } |
| 1495 | |
| 1496 | dev_info( |
| 1497 | &client->dev, |
| 1498 | "Atmel maXTouch (Family %s (%X), Variant %s (%X)) Firmware " |
| 1499 | "version [%d.%d] Build %d\n", |
| 1500 | mxt->device_info.family_name, |
| 1501 | mxt->device_info.family_id, |
| 1502 | mxt->device_info.variant_name, |
| 1503 | mxt->device_info.variant_id, |
| 1504 | mxt->device_info.major, |
| 1505 | mxt->device_info.minor, |
| 1506 | mxt->device_info.build |
| 1507 | ); |
| 1508 | dev_dbg( |
| 1509 | &client->dev, |
| 1510 | "Atmel maXTouch Configuration " |
| 1511 | "[X: %d] x [Y: %d]\n", |
| 1512 | mxt->device_info.x_size, |
| 1513 | mxt->device_info.y_size |
| 1514 | ); |
| 1515 | return identified; |
| 1516 | } |
| 1517 | |
| 1518 | /* |
| 1519 | * Reads the object table from maXTouch chip to get object data like |
| 1520 | * address, size, report id. For Info Block CRC calculation, already read |
| 1521 | * id data is passed to this function too (Info Block consists of the ID |
| 1522 | * block and object table). |
| 1523 | * |
| 1524 | */ |
| 1525 | static int __devinit mxt_read_object_table(struct i2c_client *client, |
| 1526 | struct mxt_data *mxt, |
| 1527 | u8 *raw_id_data) |
| 1528 | { |
| 1529 | u16 report_id_count; |
| 1530 | u8 buf[MXT_OBJECT_TABLE_ELEMENT_SIZE]; |
| 1531 | u8 *raw_ib_data; |
| 1532 | u8 object_type; |
| 1533 | u16 object_address; |
| 1534 | u16 object_size; |
| 1535 | u8 object_instances; |
| 1536 | u8 object_report_ids; |
| 1537 | u16 object_info_address; |
| 1538 | u32 crc; |
| 1539 | u32 calculated_crc; |
| 1540 | int i; |
| 1541 | int error; |
| 1542 | |
| 1543 | u8 object_instance; |
| 1544 | u8 object_report_id; |
| 1545 | u8 report_id; |
| 1546 | int first_report_id; |
| 1547 | int ib_pointer; |
| 1548 | struct mxt_object *object_table; |
| 1549 | |
| 1550 | mxt_debug(DEBUG_TRACE, "maXTouch driver reading configuration\n"); |
| 1551 | |
| 1552 | object_table = kzalloc(sizeof(struct mxt_object) * |
| 1553 | mxt->device_info.num_objs, |
| 1554 | GFP_KERNEL); |
| 1555 | if (object_table == NULL) { |
| 1556 | printk(KERN_WARNING "maXTouch: Memory allocation failed!\n"); |
| 1557 | error = -ENOMEM; |
| 1558 | goto err_object_table_alloc; |
| 1559 | } |
| 1560 | |
| 1561 | raw_ib_data = kmalloc(MXT_OBJECT_TABLE_ELEMENT_SIZE * |
| 1562 | mxt->device_info.num_objs + MXT_ID_BLOCK_SIZE, |
| 1563 | GFP_KERNEL); |
| 1564 | if (raw_ib_data == NULL) { |
| 1565 | printk(KERN_WARNING "maXTouch: Memory allocation failed!\n"); |
| 1566 | error = -ENOMEM; |
| 1567 | goto err_ib_alloc; |
| 1568 | } |
| 1569 | |
| 1570 | /* Copy the ID data for CRC calculation. */ |
| 1571 | memcpy(raw_ib_data, raw_id_data, MXT_ID_BLOCK_SIZE); |
| 1572 | ib_pointer = MXT_ID_BLOCK_SIZE; |
| 1573 | |
| 1574 | mxt->object_table = object_table; |
| 1575 | |
| 1576 | mxt_debug(DEBUG_TRACE, "maXTouch driver Memory allocated\n"); |
| 1577 | |
| 1578 | object_info_address = MXT_ADDR_OBJECT_TABLE; |
| 1579 | |
| 1580 | report_id_count = 0; |
| 1581 | for (i = 0; i < mxt->device_info.num_objs; i++) { |
| 1582 | mxt_debug(DEBUG_TRACE, "Reading maXTouch at [0x%04x]: ", |
| 1583 | object_info_address); |
| 1584 | |
| 1585 | error = mxt_read_block(client, object_info_address, |
| 1586 | MXT_OBJECT_TABLE_ELEMENT_SIZE, buf); |
| 1587 | |
| 1588 | if (error < 0) { |
| 1589 | mxt->read_fail_counter++; |
| 1590 | dev_err(&client->dev, |
| 1591 | "maXTouch Object %d could not be read\n", i); |
| 1592 | error = -EIO; |
| 1593 | goto err_object_read; |
| 1594 | } |
| 1595 | |
| 1596 | memcpy(raw_ib_data + ib_pointer, buf, |
| 1597 | MXT_OBJECT_TABLE_ELEMENT_SIZE); |
| 1598 | ib_pointer += MXT_OBJECT_TABLE_ELEMENT_SIZE; |
| 1599 | |
| 1600 | object_type = buf[0]; |
| 1601 | object_address = (buf[2] << 8) + buf[1]; |
| 1602 | object_size = buf[3] + 1; |
| 1603 | object_instances = buf[4] + 1; |
| 1604 | object_report_ids = buf[5]; |
| 1605 | mxt_debug(DEBUG_TRACE, "Type=%03d, Address=0x%04x, " |
| 1606 | "Size=0x%02x, %d instances, %d report id's\n", |
| 1607 | object_type, |
| 1608 | object_address, |
| 1609 | object_size, |
| 1610 | object_instances, |
| 1611 | object_report_ids |
| 1612 | ); |
| 1613 | |
| 1614 | if (object_type == 38) |
| 1615 | t38_size = object_size; |
| 1616 | /* TODO: check whether object is known and supported? */ |
| 1617 | |
| 1618 | /* Save frequently needed info. */ |
| 1619 | if (object_type == MXT_GEN_MESSAGEPROCESSOR_T5) { |
| 1620 | mxt->msg_proc_addr = object_address; |
| 1621 | mxt->message_size = object_size; |
| 1622 | } |
| 1623 | |
| 1624 | object_table[i].type = object_type; |
| 1625 | object_table[i].chip_addr = object_address; |
| 1626 | object_table[i].size = object_size; |
| 1627 | object_table[i].instances = object_instances; |
| 1628 | object_table[i].num_report_ids = object_report_ids; |
| 1629 | report_id_count += object_instances * object_report_ids; |
| 1630 | |
| 1631 | object_info_address += MXT_OBJECT_TABLE_ELEMENT_SIZE; |
| 1632 | } |
| 1633 | |
| 1634 | mxt->rid_map = |
| 1635 | kzalloc(sizeof(struct report_id_map) * (report_id_count + 1), |
| 1636 | /* allocate for report_id 0, even if not used */ |
| 1637 | GFP_KERNEL); |
| 1638 | if (mxt->rid_map == NULL) { |
| 1639 | printk(KERN_WARNING "maXTouch: Can't allocate memory!\n"); |
| 1640 | error = -ENOMEM; |
| 1641 | goto err_rid_map_alloc; |
| 1642 | } |
| 1643 | |
| 1644 | mxt->messages = kzalloc(mxt->message_size * MXT_MESSAGE_BUFFER_SIZE, |
| 1645 | GFP_KERNEL); |
| 1646 | if (mxt->messages == NULL) { |
| 1647 | printk(KERN_WARNING "maXTouch: Can't allocate memory!\n"); |
| 1648 | error = -ENOMEM; |
| 1649 | goto err_msg_alloc; |
| 1650 | } |
| 1651 | |
| 1652 | mxt->last_message = kzalloc(mxt->message_size, GFP_KERNEL); |
| 1653 | if (mxt->last_message == NULL) { |
| 1654 | printk(KERN_WARNING "maXTouch: Can't allocate memory!\n"); |
| 1655 | error = -ENOMEM; |
| 1656 | goto err_msg_alloc; |
| 1657 | } |
| 1658 | |
| 1659 | mxt->report_id_count = report_id_count; |
| 1660 | if (report_id_count > 254) { /* 0 & 255 are reserved */ |
| 1661 | dev_err(&client->dev, |
| 1662 | "Too many maXTouch report id's [%d]\n", |
| 1663 | report_id_count); |
| 1664 | error = -ENXIO; |
| 1665 | goto err_max_rid; |
| 1666 | } |
| 1667 | |
| 1668 | /* Create a mapping from report id to object type */ |
| 1669 | report_id = 1; /* Start from 1, 0 is reserved. */ |
| 1670 | |
| 1671 | /* Create table associating report id's with objects & instances */ |
| 1672 | for (i = 0; i < mxt->device_info.num_objs; i++) { |
| 1673 | for (object_instance = 0; |
| 1674 | object_instance < object_table[i].instances; |
| 1675 | object_instance++){ |
| 1676 | first_report_id = report_id; |
| 1677 | for (object_report_id = 0; |
| 1678 | object_report_id < object_table[i].num_report_ids; |
| 1679 | object_report_id++) { |
| 1680 | mxt->rid_map[report_id].object = |
| 1681 | object_table[i].type; |
| 1682 | mxt->rid_map[report_id].instance = |
| 1683 | object_instance; |
| 1684 | mxt->rid_map[report_id].first_rid = |
| 1685 | first_report_id; |
| 1686 | report_id++; |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | /* Read 3 byte CRC */ |
| 1692 | error = mxt_read_block(client, object_info_address, 3, buf); |
| 1693 | if (error < 0) { |
| 1694 | mxt->read_fail_counter++; |
| 1695 | dev_err(&client->dev, "Error reading CRC\n"); |
| 1696 | } |
| 1697 | |
| 1698 | crc = (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 1699 | |
| 1700 | if (calculate_infoblock_crc(&calculated_crc, raw_ib_data, |
| 1701 | ib_pointer)) { |
| 1702 | printk(KERN_WARNING "Error while calculating CRC!\n"); |
| 1703 | calculated_crc = 0; |
| 1704 | } |
| 1705 | kfree(raw_ib_data); |
| 1706 | |
| 1707 | mxt_debug(DEBUG_TRACE, "\nReported info block CRC = 0x%6X\n", crc); |
| 1708 | mxt_debug(DEBUG_TRACE, "Calculated info block CRC = 0x%6X\n\n", |
| 1709 | calculated_crc); |
| 1710 | |
| 1711 | if (crc == calculated_crc) { |
| 1712 | mxt->info_block_crc = crc; |
| 1713 | } else { |
| 1714 | mxt->info_block_crc = 0; |
| 1715 | printk(KERN_ALERT "maXTouch: Info block CRC invalid!\n"); |
| 1716 | } |
| 1717 | |
| 1718 | if (debug >= DEBUG_VERBOSE) { |
| 1719 | |
| 1720 | dev_info(&client->dev, "maXTouch: %d Objects\n", |
| 1721 | mxt->device_info.num_objs); |
| 1722 | |
| 1723 | for (i = 0; i < mxt->device_info.num_objs; i++) { |
| 1724 | dev_info(&client->dev, "Type:\t\t\t[%d]: %s\n", |
| 1725 | object_table[i].type, |
| 1726 | object_type_name[object_table[i].type]); |
| 1727 | dev_info(&client->dev, "\tAddress:\t0x%04X\n", |
| 1728 | object_table[i].chip_addr); |
| 1729 | dev_info(&client->dev, "\tSize:\t\t%d Bytes\n", |
| 1730 | object_table[i].size); |
| 1731 | dev_info(&client->dev, "\tInstances:\t%d\n", |
| 1732 | object_table[i].instances); |
| 1733 | dev_info(&client->dev, "\tReport Id's:\t%d\n", |
| 1734 | object_table[i].num_report_ids); |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | return 0; |
| 1739 | |
| 1740 | |
| 1741 | err_max_rid: |
| 1742 | kfree(mxt->last_message); |
| 1743 | err_msg_alloc: |
| 1744 | kfree(mxt->rid_map); |
| 1745 | err_rid_map_alloc: |
| 1746 | err_object_read: |
| 1747 | kfree(raw_ib_data); |
| 1748 | err_ib_alloc: |
| 1749 | kfree(object_table); |
| 1750 | err_object_table_alloc: |
| 1751 | return error; |
| 1752 | } |
| 1753 | |
| 1754 | #if defined(CONFIG_PM) |
| 1755 | static int mxt_suspend(struct device *dev) |
| 1756 | { |
| 1757 | struct mxt_data *mxt = dev_get_drvdata(dev); |
| 1758 | int error, i; |
| 1759 | u8 t7_deepsl_data[T7_DATA_SIZE]; |
| 1760 | u16 t7_addr; |
| 1761 | |
| 1762 | if (device_may_wakeup(dev)) { |
| 1763 | enable_irq_wake(mxt->irq); |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
| 1767 | disable_irq(mxt->irq); |
| 1768 | |
| 1769 | flush_delayed_work_sync(&mxt->dwork); |
| 1770 | |
| 1771 | for (i = 0; i < T7_DATA_SIZE; i++) |
| 1772 | t7_deepsl_data[i] = 0; |
| 1773 | |
| 1774 | t7_addr = MXT_BASE_ADDR(MXT_GEN_POWERCONFIG_T7, mxt); |
| 1775 | /* save current power state values */ |
| 1776 | error = mxt_read_block(mxt->client, t7_addr, |
| 1777 | ARRAY_SIZE(mxt->t7_data), mxt->t7_data); |
| 1778 | if (error < 0) |
| 1779 | goto err_enable_irq; |
| 1780 | |
| 1781 | /* configure deep sleep mode */ |
| 1782 | error = mxt_write_block(mxt->client, t7_addr, |
| 1783 | ARRAY_SIZE(t7_deepsl_data), t7_deepsl_data); |
| 1784 | if (error < 0) |
| 1785 | goto err_enable_irq; |
| 1786 | |
| 1787 | /* power off the device */ |
| 1788 | if (mxt->power_on) { |
| 1789 | error = mxt->power_on(false); |
| 1790 | if (error) { |
| 1791 | dev_err(dev, "power off failed"); |
| 1792 | goto err_write_block; |
| 1793 | } |
| 1794 | } |
| 1795 | mxt->is_suspended = true; |
| 1796 | return 0; |
| 1797 | |
| 1798 | err_write_block: |
| 1799 | mxt_write_block(mxt->client, t7_addr, |
| 1800 | ARRAY_SIZE(mxt->t7_data), mxt->t7_data); |
| 1801 | err_enable_irq: |
| 1802 | enable_irq(mxt->irq); |
| 1803 | return error; |
| 1804 | } |
| 1805 | |
| 1806 | static int mxt_resume(struct device *dev) |
| 1807 | { |
| 1808 | struct mxt_data *mxt = dev_get_drvdata(dev); |
| 1809 | int error; |
| 1810 | u16 t7_addr; |
| 1811 | |
| 1812 | if (device_may_wakeup(dev)) { |
| 1813 | disable_irq_wake(mxt->irq); |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
| 1817 | if (!mxt->is_suspended) |
| 1818 | return 0; |
| 1819 | |
| 1820 | /* power on the device */ |
| 1821 | if (mxt->power_on) { |
| 1822 | error = mxt->power_on(true); |
| 1823 | if (error) { |
| 1824 | dev_err(dev, "power on failed"); |
| 1825 | return error; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | t7_addr = MXT_BASE_ADDR(MXT_GEN_POWERCONFIG_T7, mxt); |
| 1830 | /* restore the old power state values */ |
| 1831 | error = mxt_write_block(mxt->client, t7_addr, |
| 1832 | ARRAY_SIZE(mxt->t7_data), mxt->t7_data); |
| 1833 | if (error < 0) |
| 1834 | goto err_write_block; |
| 1835 | |
Anirudh Ghayal | 4e66bb7 | 2012-01-02 17:04:32 +0530 | [diff] [blame] | 1836 | /* Make sure we just didn't miss a interrupt. */ |
| 1837 | if (mxt->read_chg() == 0) |
| 1838 | schedule_delayed_work(&mxt->dwork, 0); |
| 1839 | else |
| 1840 | enable_irq(mxt->irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1841 | |
| 1842 | mxt->is_suspended = false; |
| 1843 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1844 | return 0; |
| 1845 | |
| 1846 | err_write_block: |
| 1847 | if (mxt->power_on) |
| 1848 | mxt->power_on(false); |
| 1849 | return error; |
| 1850 | } |
| 1851 | |
| 1852 | #if defined(CONFIG_HAS_EARLYSUSPEND) |
| 1853 | static void mxt_early_suspend(struct early_suspend *h) |
| 1854 | { |
| 1855 | struct mxt_data *mxt = container_of(h, struct mxt_data, early_suspend); |
| 1856 | |
| 1857 | mxt_suspend(&mxt->client->dev); |
| 1858 | } |
| 1859 | |
| 1860 | static void mxt_late_resume(struct early_suspend *h) |
| 1861 | { |
| 1862 | struct mxt_data *mxt = container_of(h, struct mxt_data, early_suspend); |
| 1863 | |
| 1864 | mxt_resume(&mxt->client->dev); |
| 1865 | } |
| 1866 | #endif |
| 1867 | |
| 1868 | static const struct dev_pm_ops mxt_pm_ops = { |
| 1869 | #ifndef CONFIG_HAS_EARLYSUSPEND |
| 1870 | .suspend = mxt_suspend, |
| 1871 | .resume = mxt_resume, |
| 1872 | #endif |
| 1873 | }; |
| 1874 | #endif |
| 1875 | |
| 1876 | static int __devinit mxt_probe(struct i2c_client *client, |
| 1877 | const struct i2c_device_id *id) |
| 1878 | { |
| 1879 | struct mxt_data *mxt; |
Mohan Pallaka | 4e9a94e | 2011-11-23 16:34:21 +0530 | [diff] [blame] | 1880 | struct maxtouch_platform_data *pdata; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1881 | struct input_dev *input; |
| 1882 | u8 *id_data; |
| 1883 | u8 *t38_data; |
| 1884 | u16 t38_addr; |
| 1885 | int error; |
| 1886 | |
| 1887 | mxt_debug(DEBUG_INFO, "mXT224: mxt_probe\n"); |
| 1888 | |
| 1889 | if (client == NULL) { |
| 1890 | pr_debug("maXTouch: client == NULL\n"); |
| 1891 | return -EINVAL; |
| 1892 | } else if (client->adapter == NULL) { |
| 1893 | pr_debug("maXTouch: client->adapter == NULL\n"); |
| 1894 | return -EINVAL; |
| 1895 | } else if (&client->dev == NULL) { |
| 1896 | pr_debug("maXTouch: client->dev == NULL\n"); |
| 1897 | return -EINVAL; |
| 1898 | } else if (&client->adapter->dev == NULL) { |
| 1899 | pr_debug("maXTouch: client->adapter->dev == NULL\n"); |
| 1900 | return -EINVAL; |
| 1901 | } else if (id == NULL) { |
| 1902 | pr_debug("maXTouch: id == NULL\n"); |
| 1903 | return -EINVAL; |
| 1904 | } |
| 1905 | |
| 1906 | /* Enable runtime PM ops, start in ACTIVE mode */ |
| 1907 | error = pm_runtime_set_active(&client->dev); |
| 1908 | if (error < 0) |
| 1909 | dev_dbg(&client->dev, "unable to set runtime pm state\n"); |
| 1910 | pm_runtime_enable(&client->dev); |
| 1911 | |
| 1912 | mxt_debug(DEBUG_INFO, "maXTouch driver v. %s\n", DRIVER_VERSION); |
| 1913 | mxt_debug(DEBUG_INFO, "\t \"%s\"\n", client->name); |
| 1914 | mxt_debug(DEBUG_INFO, "\taddr:\t0x%04x\n", client->addr); |
| 1915 | mxt_debug(DEBUG_INFO, "\tirq:\t%d\n", client->irq); |
| 1916 | mxt_debug(DEBUG_INFO, "\tflags:\t0x%04x\n", client->flags); |
| 1917 | mxt_debug(DEBUG_INFO, "\tadapter:\"%s\"\n", client->adapter->name); |
| 1918 | mxt_debug(DEBUG_INFO, "\tdevice:\t\"%s\"\n", client->dev.init_name); |
| 1919 | |
| 1920 | mxt_debug(DEBUG_TRACE, "maXTouch driver functionality OK\n"); |
| 1921 | |
| 1922 | /* Allocate structure - we need it to identify device */ |
| 1923 | mxt = kzalloc(sizeof(struct mxt_data), GFP_KERNEL); |
| 1924 | if (mxt == NULL) { |
| 1925 | dev_err(&client->dev, "insufficient memory\n"); |
| 1926 | error = -ENOMEM; |
| 1927 | goto err_mxt_alloc; |
| 1928 | } |
| 1929 | |
| 1930 | id_data = kmalloc(MXT_ID_BLOCK_SIZE, GFP_KERNEL); |
| 1931 | if (id_data == NULL) { |
| 1932 | dev_err(&client->dev, "insufficient memory\n"); |
| 1933 | error = -ENOMEM; |
| 1934 | goto err_id_alloc; |
| 1935 | } |
| 1936 | |
| 1937 | input = input_allocate_device(); |
| 1938 | if (!input) { |
| 1939 | dev_err(&client->dev, "error allocating input device\n"); |
| 1940 | error = -ENOMEM; |
| 1941 | goto err_input_dev_alloc; |
| 1942 | } |
| 1943 | |
| 1944 | /* Initialize Platform data */ |
| 1945 | |
| 1946 | pdata = client->dev.platform_data; |
| 1947 | if (pdata == NULL) { |
| 1948 | dev_err(&client->dev, "platform data is required!\n"); |
| 1949 | error = -EINVAL; |
| 1950 | goto err_pdata; |
| 1951 | } |
| 1952 | if (debug >= DEBUG_TRACE) |
| 1953 | printk(KERN_INFO "Platform OK: pdata = 0x%08x\n", |
| 1954 | (unsigned int) pdata); |
| 1955 | |
| 1956 | mxt->is_suspended = false; |
| 1957 | mxt->read_fail_counter = 0; |
| 1958 | mxt->message_counter = 0; |
| 1959 | |
| 1960 | if (pdata->min_x) |
| 1961 | mxt->min_x_val = pdata->min_x; |
| 1962 | else |
| 1963 | mxt->min_x_val = 0; |
| 1964 | |
| 1965 | if (pdata->min_y) |
| 1966 | mxt->min_y_val = pdata->min_y; |
| 1967 | else |
| 1968 | mxt->min_y_val = 0; |
| 1969 | |
| 1970 | mxt->max_x_val = pdata->max_x; |
| 1971 | mxt->max_y_val = pdata->max_y; |
| 1972 | |
| 1973 | /* Get data that is defined in board specific code. */ |
| 1974 | mxt->init_hw = pdata->init_platform_hw; |
| 1975 | mxt->exit_hw = pdata->exit_platform_hw; |
| 1976 | mxt->power_on = pdata->power_on; |
| 1977 | mxt->read_chg = pdata->read_chg; |
| 1978 | |
| 1979 | if (pdata->valid_interrupt != NULL) |
| 1980 | mxt->valid_interrupt = pdata->valid_interrupt; |
| 1981 | else |
| 1982 | mxt->valid_interrupt = mxt_valid_interrupt_dummy; |
| 1983 | |
| 1984 | if (mxt->init_hw) { |
| 1985 | error = mxt->init_hw(client); |
| 1986 | if (error) { |
| 1987 | dev_err(&client->dev, "hw init failed"); |
| 1988 | goto err_init_hw; |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | /* power on the device */ |
| 1993 | if (mxt->power_on) { |
| 1994 | error = mxt->power_on(true); |
| 1995 | if (error) { |
| 1996 | dev_err(&client->dev, "power on failed"); |
| 1997 | goto err_pwr_on; |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | if (debug >= DEBUG_TRACE) |
| 2002 | printk(KERN_INFO "maXTouch driver identifying chip\n"); |
| 2003 | |
| 2004 | if (mxt_identify(client, mxt, id_data) < 0) { |
| 2005 | dev_err(&client->dev, "Chip could not be identified\n"); |
| 2006 | error = -ENODEV; |
| 2007 | goto err_identify; |
| 2008 | } |
| 2009 | /* Chip is valid and active. */ |
| 2010 | if (debug >= DEBUG_TRACE) |
| 2011 | printk(KERN_INFO "maXTouch driver allocating input device\n"); |
| 2012 | |
| 2013 | mxt->client = client; |
| 2014 | mxt->input = input; |
| 2015 | |
| 2016 | INIT_DELAYED_WORK(&mxt->dwork, mxt_worker); |
| 2017 | mutex_init(&mxt->debug_mutex); |
| 2018 | mutex_init(&mxt->msg_mutex); |
| 2019 | mxt_debug(DEBUG_TRACE, "maXTouch driver creating device name\n"); |
| 2020 | |
| 2021 | snprintf( |
| 2022 | mxt->phys_name, |
| 2023 | sizeof(mxt->phys_name), |
| 2024 | "%s/input0", |
| 2025 | dev_name(&client->dev) |
| 2026 | ); |
| 2027 | input->name = "Atmel maXTouch Touchscreen controller"; |
| 2028 | input->phys = mxt->phys_name; |
| 2029 | input->id.bustype = BUS_I2C; |
| 2030 | input->dev.parent = &client->dev; |
| 2031 | |
| 2032 | mxt_debug(DEBUG_INFO, "maXTouch name: \"%s\"\n", input->name); |
| 2033 | mxt_debug(DEBUG_INFO, "maXTouch phys: \"%s\"\n", input->phys); |
| 2034 | mxt_debug(DEBUG_INFO, "maXTouch driver setting abs parameters\n"); |
| 2035 | |
| 2036 | set_bit(BTN_TOUCH, input->keybit); |
Amy Maloche | 5520db2 | 2012-06-20 11:07:37 -0700 | [diff] [blame] | 2037 | set_bit(INPUT_PROP_DIRECT, input->propbit); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2038 | |
| 2039 | /* Single touch */ |
| 2040 | input_set_abs_params(input, ABS_X, mxt->min_x_val, |
| 2041 | mxt->max_x_val, 0, 0); |
| 2042 | input_set_abs_params(input, ABS_Y, mxt->min_y_val, |
| 2043 | mxt->max_y_val, 0, 0); |
| 2044 | input_set_abs_params(input, ABS_PRESSURE, 0, MXT_MAX_REPORTED_PRESSURE, |
| 2045 | 0, 0); |
| 2046 | input_set_abs_params(input, ABS_TOOL_WIDTH, 0, MXT_MAX_REPORTED_WIDTH, |
| 2047 | 0, 0); |
| 2048 | |
| 2049 | /* Multitouch */ |
| 2050 | input_set_abs_params(input, ABS_MT_POSITION_X, mxt->min_x_val, |
| 2051 | mxt->max_x_val, 0, 0); |
| 2052 | input_set_abs_params(input, ABS_MT_POSITION_Y, mxt->min_y_val, |
| 2053 | mxt->max_y_val, 0, 0); |
| 2054 | input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, MXT_MAX_TOUCH_SIZE, |
| 2055 | 0, 0); |
| 2056 | input_set_abs_params(input, ABS_MT_TRACKING_ID, 0, MXT_MAX_NUM_TOUCHES, |
| 2057 | 0, 0); |
| 2058 | |
| 2059 | __set_bit(EV_ABS, input->evbit); |
| 2060 | __set_bit(EV_SYN, input->evbit); |
| 2061 | __set_bit(EV_KEY, input->evbit); |
| 2062 | __set_bit(EV_MSC, input->evbit); |
| 2063 | input->mscbit[0] = BIT_MASK(MSC_GESTURE); |
| 2064 | |
| 2065 | mxt_debug(DEBUG_TRACE, "maXTouch driver setting client data\n"); |
| 2066 | i2c_set_clientdata(client, mxt); |
| 2067 | mxt_debug(DEBUG_TRACE, "maXTouch driver setting drv data\n"); |
| 2068 | input_set_drvdata(input, mxt); |
| 2069 | mxt_debug(DEBUG_TRACE, "maXTouch driver input register device\n"); |
| 2070 | error = input_register_device(mxt->input); |
| 2071 | if (error < 0) { |
| 2072 | dev_err(&client->dev, |
| 2073 | "Failed to register input device\n"); |
| 2074 | goto err_register_device; |
| 2075 | } |
| 2076 | |
| 2077 | error = mxt_read_object_table(client, mxt, id_data); |
| 2078 | if (error < 0) |
| 2079 | goto err_read_ot; |
| 2080 | |
| 2081 | |
| 2082 | /* Create debugfs entries. */ |
| 2083 | mxt->debug_dir = debugfs_create_dir("maXTouch", NULL); |
| 2084 | if (mxt->debug_dir == ERR_PTR(-ENODEV)) { |
| 2085 | /* debugfs is not enabled. */ |
| 2086 | printk(KERN_WARNING "debugfs not enabled in kernel\n"); |
| 2087 | } else if (mxt->debug_dir == NULL) { |
| 2088 | printk(KERN_WARNING "error creating debugfs dir\n"); |
| 2089 | } else { |
| 2090 | mxt_debug(DEBUG_TRACE, "created \"maXTouch\" debugfs dir\n"); |
| 2091 | |
| 2092 | debugfs_create_file("deltas", S_IRUSR, mxt->debug_dir, mxt, |
| 2093 | &delta_fops); |
| 2094 | debugfs_create_file("refs", S_IRUSR, mxt->debug_dir, mxt, |
| 2095 | &refs_fops); |
| 2096 | } |
| 2097 | |
| 2098 | /* Create character device nodes for reading & writing registers */ |
| 2099 | mxt->mxt_class = class_create(THIS_MODULE, "maXTouch_memory"); |
| 2100 | if (IS_ERR(mxt->mxt_class)){ |
| 2101 | printk(KERN_WARNING "class create failed! exiting..."); |
| 2102 | goto err_class_create; |
| 2103 | |
| 2104 | } |
| 2105 | /* 2 numbers; one for memory and one for messages */ |
| 2106 | error = alloc_chrdev_region(&mxt->dev_num, 0, 2, |
| 2107 | "maXTouch_memory"); |
| 2108 | mxt_debug(DEBUG_VERBOSE, |
| 2109 | "device number %d allocated!\n", MAJOR(mxt->dev_num)); |
| 2110 | if (error){ |
| 2111 | printk(KERN_WARNING "Error registering device\n"); |
| 2112 | } |
| 2113 | cdev_init(&mxt->cdev, &mxt_memory_fops); |
| 2114 | cdev_init(&mxt->cdev_messages, &mxt_message_fops); |
| 2115 | |
| 2116 | mxt_debug(DEBUG_VERBOSE, "cdev initialized\n"); |
| 2117 | mxt->cdev.owner = THIS_MODULE; |
| 2118 | mxt->cdev_messages.owner = THIS_MODULE; |
| 2119 | |
| 2120 | error = cdev_add(&mxt->cdev, mxt->dev_num, 1); |
| 2121 | if (error){ |
| 2122 | printk(KERN_WARNING "Bad cdev\n"); |
| 2123 | } |
| 2124 | |
| 2125 | error = cdev_add(&mxt->cdev_messages, mxt->dev_num + 1, 1); |
| 2126 | if (error){ |
| 2127 | printk(KERN_WARNING "Bad cdev\n"); |
| 2128 | } |
| 2129 | |
| 2130 | mxt_debug(DEBUG_VERBOSE, "cdev added\n"); |
| 2131 | |
| 2132 | device_create(mxt->mxt_class, NULL, MKDEV(MAJOR(mxt->dev_num), 0), NULL, |
| 2133 | "maXTouch"); |
| 2134 | |
| 2135 | device_create(mxt->mxt_class, NULL, MKDEV(MAJOR(mxt->dev_num), 1), NULL, |
| 2136 | "maXTouch_messages"); |
| 2137 | |
| 2138 | mxt->msg_buffer_startp = 0; |
| 2139 | mxt->msg_buffer_endp = 0; |
| 2140 | |
| 2141 | /* Allocate the interrupt */ |
| 2142 | mxt_debug(DEBUG_TRACE, "maXTouch driver allocating interrupt...\n"); |
| 2143 | mxt->irq = client->irq; |
| 2144 | mxt->valid_irq_counter = 0; |
| 2145 | mxt->invalid_irq_counter = 0; |
| 2146 | mxt->irq_counter = 0; |
| 2147 | if (mxt->irq) { |
| 2148 | /* Try to request IRQ with falling edge first. This is |
| 2149 | * not always supported. If it fails, try with any edge. */ |
| 2150 | error = request_irq(mxt->irq, |
| 2151 | mxt_irq_handler, |
| 2152 | IRQF_TRIGGER_FALLING, |
| 2153 | client->dev.driver->name, |
| 2154 | mxt); |
| 2155 | if (error < 0) { |
| 2156 | /* TODO: why only 0 works on STK1000? */ |
| 2157 | error = request_irq(mxt->irq, |
| 2158 | mxt_irq_handler, |
| 2159 | 0, |
| 2160 | client->dev.driver->name, |
| 2161 | mxt); |
| 2162 | } |
| 2163 | |
| 2164 | if (error < 0) { |
| 2165 | dev_err(&client->dev, |
| 2166 | "failed to allocate irq %d\n", mxt->irq); |
| 2167 | goto err_irq; |
| 2168 | } |
| 2169 | } |
| 2170 | |
| 2171 | if (debug > DEBUG_INFO) |
| 2172 | dev_info(&client->dev, "touchscreen, irq %d\n", mxt->irq); |
| 2173 | |
| 2174 | t38_data = kmalloc(t38_size*sizeof(u8), GFP_KERNEL); |
| 2175 | |
| 2176 | if (t38_data == NULL) { |
| 2177 | dev_err(&client->dev, "insufficient memory\n"); |
| 2178 | error = -ENOMEM; |
| 2179 | goto err_t38; |
| 2180 | } |
| 2181 | |
| 2182 | t38_addr = MXT_BASE_ADDR(MXT_USER_INFO_T38, mxt); |
| 2183 | mxt_read_block(client, t38_addr, t38_size, t38_data); |
| 2184 | dev_info(&client->dev, "VERSION:%02x.%02x.%02x, DATE: %d/%d/%d\n", |
| 2185 | t38_data[0], t38_data[1], t38_data[2], |
| 2186 | t38_data[3], t38_data[4], t38_data[5]); |
| 2187 | |
| 2188 | /* Schedule a worker routine to read any messages that might have |
| 2189 | * been sent before interrupts were enabled. */ |
| 2190 | cancel_delayed_work(&mxt->dwork); |
Anirudh Ghayal | 9ffded2 | 2011-09-20 09:00:00 +0530 | [diff] [blame] | 2191 | disable_irq(mxt->irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2192 | schedule_delayed_work(&mxt->dwork, 0); |
| 2193 | kfree(t38_data); |
| 2194 | kfree(id_data); |
| 2195 | |
| 2196 | device_init_wakeup(&client->dev, pdata->wakeup); |
| 2197 | #if defined(CONFIG_HAS_EARLYSUSPEND) |
| 2198 | mxt->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + |
| 2199 | MXT_SUSPEND_LEVEL; |
| 2200 | mxt->early_suspend.suspend = mxt_early_suspend; |
| 2201 | mxt->early_suspend.resume = mxt_late_resume; |
| 2202 | register_early_suspend(&mxt->early_suspend); |
| 2203 | #endif |
| 2204 | |
| 2205 | return 0; |
| 2206 | |
| 2207 | err_t38: |
| 2208 | free_irq(mxt->irq, mxt); |
| 2209 | err_irq: |
| 2210 | kfree(mxt->rid_map); |
| 2211 | kfree(mxt->object_table); |
| 2212 | kfree(mxt->last_message); |
| 2213 | err_class_create: |
| 2214 | if (mxt->debug_dir) |
| 2215 | debugfs_remove(mxt->debug_dir); |
| 2216 | kfree(mxt->last_message); |
| 2217 | kfree(mxt->rid_map); |
| 2218 | kfree(mxt->object_table); |
| 2219 | err_read_ot: |
| 2220 | input_unregister_device(mxt->input); |
| 2221 | mxt->input = NULL; |
| 2222 | err_register_device: |
| 2223 | mutex_destroy(&mxt->debug_mutex); |
| 2224 | mutex_destroy(&mxt->msg_mutex); |
| 2225 | err_identify: |
| 2226 | if (mxt->power_on) |
| 2227 | mxt->power_on(false); |
| 2228 | err_pwr_on: |
| 2229 | if (mxt->exit_hw != NULL) |
| 2230 | mxt->exit_hw(client); |
| 2231 | err_init_hw: |
| 2232 | err_pdata: |
| 2233 | input_free_device(input); |
| 2234 | err_input_dev_alloc: |
| 2235 | kfree(id_data); |
| 2236 | err_id_alloc: |
| 2237 | kfree(mxt); |
| 2238 | err_mxt_alloc: |
| 2239 | pm_runtime_set_suspended(&client->dev); |
| 2240 | pm_runtime_disable(&client->dev); |
| 2241 | return error; |
| 2242 | } |
| 2243 | |
| 2244 | static int __devexit mxt_remove(struct i2c_client *client) |
| 2245 | { |
| 2246 | struct mxt_data *mxt; |
| 2247 | |
| 2248 | pm_runtime_set_suspended(&client->dev); |
| 2249 | pm_runtime_disable(&client->dev); |
| 2250 | |
| 2251 | mxt = i2c_get_clientdata(client); |
| 2252 | |
| 2253 | /* Remove debug dir entries */ |
| 2254 | debugfs_remove_recursive(mxt->debug_dir); |
| 2255 | |
| 2256 | device_init_wakeup(&client->dev, 0); |
| 2257 | #if defined(CONFIG_HAS_EARLYSUSPEND) |
| 2258 | unregister_early_suspend(&mxt->early_suspend); |
| 2259 | #endif |
| 2260 | |
| 2261 | if (mxt != NULL) { |
| 2262 | if (mxt->power_on) |
| 2263 | mxt->power_on(false); |
| 2264 | |
| 2265 | if (mxt->exit_hw != NULL) |
| 2266 | mxt->exit_hw(client); |
| 2267 | |
| 2268 | if (mxt->irq) { |
| 2269 | free_irq(mxt->irq, mxt); |
| 2270 | } |
| 2271 | |
| 2272 | unregister_chrdev_region(mxt->dev_num, 2); |
| 2273 | device_destroy(mxt->mxt_class, MKDEV(MAJOR(mxt->dev_num), 0)); |
| 2274 | device_destroy(mxt->mxt_class, MKDEV(MAJOR(mxt->dev_num), 1)); |
| 2275 | cdev_del(&mxt->cdev); |
| 2276 | cdev_del(&mxt->cdev_messages); |
| 2277 | cancel_delayed_work_sync(&mxt->dwork); |
| 2278 | input_unregister_device(mxt->input); |
| 2279 | class_destroy(mxt->mxt_class); |
| 2280 | debugfs_remove(mxt->debug_dir); |
| 2281 | |
| 2282 | kfree(mxt->rid_map); |
| 2283 | kfree(mxt->object_table); |
| 2284 | kfree(mxt->last_message); |
| 2285 | } |
| 2286 | kfree(mxt); |
| 2287 | |
| 2288 | i2c_set_clientdata(client, NULL); |
| 2289 | if (debug >= DEBUG_TRACE) |
| 2290 | dev_info(&client->dev, "Touchscreen unregistered\n"); |
| 2291 | |
| 2292 | return 0; |
| 2293 | } |
| 2294 | |
| 2295 | static const struct i2c_device_id mxt_idtable[] = { |
| 2296 | {"maXTouch", 0,}, |
| 2297 | { } |
| 2298 | }; |
| 2299 | |
| 2300 | MODULE_DEVICE_TABLE(i2c, mxt_idtable); |
| 2301 | |
| 2302 | static struct i2c_driver mxt_driver = { |
| 2303 | .driver = { |
| 2304 | .name = "maXTouch", |
| 2305 | .owner = THIS_MODULE, |
| 2306 | #if defined(CONFIG_PM) |
| 2307 | .pm = &mxt_pm_ops, |
| 2308 | #endif |
| 2309 | }, |
| 2310 | |
| 2311 | .id_table = mxt_idtable, |
| 2312 | .probe = mxt_probe, |
| 2313 | .remove = __devexit_p(mxt_remove), |
| 2314 | }; |
| 2315 | |
| 2316 | static int __init mxt_init(void) |
| 2317 | { |
| 2318 | int err; |
| 2319 | err = i2c_add_driver(&mxt_driver); |
| 2320 | if (err) { |
| 2321 | printk(KERN_WARNING "Adding maXTouch driver failed " |
| 2322 | "(errno = %d)\n", err); |
| 2323 | } else { |
| 2324 | mxt_debug(DEBUG_TRACE, "Successfully added driver %s\n", |
| 2325 | mxt_driver.driver.name); |
| 2326 | } |
| 2327 | return err; |
| 2328 | } |
| 2329 | |
| 2330 | static void __exit mxt_cleanup(void) |
| 2331 | { |
| 2332 | i2c_del_driver(&mxt_driver); |
| 2333 | } |
| 2334 | |
| 2335 | |
| 2336 | module_init(mxt_init); |
| 2337 | module_exit(mxt_cleanup); |
| 2338 | |
| 2339 | MODULE_AUTHOR("Iiro Valkonen"); |
| 2340 | MODULE_DESCRIPTION("Driver for Atmel maXTouch Touchscreen Controller"); |
| 2341 | MODULE_LICENSE("GPL"); |