Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Support for synaptics touchscreen. |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2007 Google, Inc. |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 5 | * Author: Arve Hjønnevåg <arve@android.com> |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
Pavel Machek | 58c6d6d | 2009-08-05 00:00:10 +0200 | [diff] [blame] | 16 | * http://www.synaptics.com/sites/default/files/511_000099_01F.pdf |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/delay.h> |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 21 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 22 | #include <linux/earlysuspend.h> |
| 23 | #endif |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 24 | #include <linux/hrtimer.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/input.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/io.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include "synaptics_i2c_rmi.h" |
| 31 | |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 32 | static struct workqueue_struct *synaptics_wq; |
| 33 | |
| 34 | struct synaptics_ts_data { |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 35 | u16 addr; |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 36 | struct i2c_client *client; |
| 37 | struct input_dev *input_dev; |
| 38 | int use_irq; |
| 39 | struct hrtimer timer; |
| 40 | struct work_struct work; |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 41 | u16 max[2]; |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 42 | int snap_state[2][2]; |
| 43 | int snap_down_on[2]; |
| 44 | int snap_down_off[2]; |
| 45 | int snap_up_on[2]; |
| 46 | int snap_up_off[2]; |
| 47 | int snap_down[2]; |
| 48 | int snap_up[2]; |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 49 | u32 flags; |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 50 | int (*power)(int on); |
| 51 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 52 | struct early_suspend early_suspend; |
| 53 | #endif |
| 54 | }; |
| 55 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 56 | static int i2c_set(struct synaptics_ts_data *ts, u8 reg, u8 val, char *msg) |
| 57 | { |
| 58 | int ret = i2c_smbus_write_byte_data(ts->client, reg, val); |
| 59 | if (ret < 0) |
| 60 | pr_err("i2c_smbus_write_byte_data failed (%s)\n", msg); |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | static int i2c_read(struct synaptics_ts_data *ts, u8 reg, char *msg) |
| 65 | { |
| 66 | int ret = i2c_smbus_read_byte_data(ts->client, reg); |
| 67 | if (ret < 0) |
| 68 | pr_err("i2c_smbus_read_byte_data failed (%s)\n", msg); |
| 69 | return ret; |
| 70 | } |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 71 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 72 | static void synaptics_ts_early_suspend(struct early_suspend *h); |
| 73 | static void synaptics_ts_late_resume(struct early_suspend *h); |
| 74 | #endif |
| 75 | |
| 76 | static int synaptics_init_panel(struct synaptics_ts_data *ts) |
| 77 | { |
| 78 | int ret; |
| 79 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 80 | ret = i2c_set(ts, 0xff, 0x10, "set page select"); |
| 81 | if (ret == 0) |
| 82 | ret = i2c_set(ts, 0x41, 0x04, "set No Clip Z"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 83 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 84 | ret = i2c_set(ts, 0xff, 0x04, "fallback page select"); |
| 85 | ret = i2c_set(ts, 0xf0, 0x81, "select 80 reports per second"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 86 | return ret; |
| 87 | } |
| 88 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 89 | static void decode_report(struct synaptics_ts_data *ts, u8 *buf) |
| 90 | { |
Pavel Machek | 58c6d6d | 2009-08-05 00:00:10 +0200 | [diff] [blame] | 91 | /* |
| 92 | * This sensor sends two 6-byte absolute finger reports, an optional |
| 93 | * 2-byte relative report followed by a status byte. This function |
| 94 | * reads the two finger reports and transforms the coordinates |
| 95 | * according the platform data so they can be aligned with the lcd |
| 96 | * behind the touchscreen. Typically we flip the y-axis since the |
| 97 | * sensor uses the bottom left corner as the origin, but if the sensor |
| 98 | * is mounted upside down the platform data will request that the |
| 99 | * x-axis should be flipped instead. The snap to inactive edge border |
| 100 | * are used to allow tapping the edges of the screen on the G1. The |
| 101 | * active area of the touchscreen is smaller than the lcd. When the |
| 102 | * finger gets close the edge of the screen we snap it to the |
| 103 | * edge. This allows ui elements at the edge of the screen to be hit, |
| 104 | * and it prevents hitting ui elements that are not at the edge of the |
| 105 | * screen when the finger is touching the edge. |
| 106 | */ |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 107 | int pos[2][2]; |
| 108 | int f, a; |
| 109 | int base = 2; |
| 110 | int z = buf[1]; |
| 111 | int w = buf[0] >> 4; |
| 112 | int finger = buf[0] & 7; |
| 113 | int finger2_pressed; |
| 114 | |
| 115 | for (f = 0; f < 2; f++) { |
| 116 | u32 flip_flag = SYNAPTICS_FLIP_X; |
| 117 | for (a = 0; a < 2; a++) { |
| 118 | int p = buf[base + 1]; |
| 119 | p |= (u16)(buf[base] & 0x1f) << 8; |
| 120 | if (ts->flags & flip_flag) |
| 121 | p = ts->max[a] - p; |
| 122 | if (ts->flags & SYNAPTICS_SNAP_TO_INACTIVE_EDGE) { |
| 123 | if (ts->snap_state[f][a]) { |
| 124 | if (p <= ts->snap_down_off[a]) |
| 125 | p = ts->snap_down[a]; |
| 126 | else if (p >= ts->snap_up_off[a]) |
| 127 | p = ts->snap_up[a]; |
| 128 | else |
| 129 | ts->snap_state[f][a] = 0; |
| 130 | } else { |
| 131 | if (p <= ts->snap_down_on[a]) { |
| 132 | p = ts->snap_down[a]; |
| 133 | ts->snap_state[f][a] = 1; |
| 134 | } else if (p >= ts->snap_up_on[a]) { |
| 135 | p = ts->snap_up[a]; |
| 136 | ts->snap_state[f][a] = 1; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | pos[f][a] = p; |
| 141 | base += 2; |
| 142 | flip_flag <<= 1; |
| 143 | } |
| 144 | base += 2; |
| 145 | if (ts->flags & SYNAPTICS_SWAP_XY) |
| 146 | swap(pos[f][0], pos[f][1]); |
| 147 | } |
| 148 | if (z) { |
| 149 | input_report_abs(ts->input_dev, ABS_X, pos[0][0]); |
| 150 | input_report_abs(ts->input_dev, ABS_Y, pos[0][1]); |
| 151 | } |
| 152 | input_report_abs(ts->input_dev, ABS_PRESSURE, z); |
| 153 | input_report_abs(ts->input_dev, ABS_TOOL_WIDTH, w); |
| 154 | input_report_key(ts->input_dev, BTN_TOUCH, finger); |
| 155 | finger2_pressed = finger > 1 && finger != 7; |
| 156 | input_report_key(ts->input_dev, BTN_2, finger2_pressed); |
| 157 | if (finger2_pressed) { |
| 158 | input_report_abs(ts->input_dev, ABS_HAT0X, pos[1][0]); |
| 159 | input_report_abs(ts->input_dev, ABS_HAT0Y, pos[1][1]); |
| 160 | } |
| 161 | input_sync(ts->input_dev); |
| 162 | } |
| 163 | |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 164 | static void synaptics_ts_work_func(struct work_struct *work) |
| 165 | { |
| 166 | int i; |
| 167 | int ret; |
| 168 | int bad_data = 0; |
| 169 | struct i2c_msg msg[2]; |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 170 | u8 start_reg = 0; |
| 171 | u8 buf[15]; |
| 172 | struct synaptics_ts_data *ts = |
| 173 | container_of(work, struct synaptics_ts_data, work); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 174 | |
| 175 | msg[0].addr = ts->client->addr; |
| 176 | msg[0].flags = 0; |
| 177 | msg[0].len = 1; |
| 178 | msg[0].buf = &start_reg; |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 179 | msg[1].addr = ts->client->addr; |
| 180 | msg[1].flags = I2C_M_RD; |
| 181 | msg[1].len = sizeof(buf); |
| 182 | msg[1].buf = buf; |
| 183 | |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 184 | for (i = 0; i < ((ts->use_irq && !bad_data) ? 1 : 10); i++) { |
| 185 | ret = i2c_transfer(ts->client->adapter, msg, 2); |
| 186 | if (ret < 0) { |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 187 | pr_err("ts_work: i2c_transfer failed\n"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 188 | bad_data = 1; |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 189 | continue; |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 190 | } |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 191 | if ((buf[14] & 0xc0) != 0x40) { |
| 192 | pr_warning("synaptics_ts_work_func:" |
| 193 | " bad read %x %x %x %x %x %x %x %x %x" |
| 194 | " %x %x %x %x %x %x, ret %d\n", |
| 195 | buf[0], buf[1], buf[2], buf[3], |
| 196 | buf[4], buf[5], buf[6], buf[7], |
| 197 | buf[8], buf[9], buf[10], buf[11], |
| 198 | buf[12], buf[13], buf[14], ret); |
| 199 | if (bad_data) |
| 200 | synaptics_init_panel(ts); |
| 201 | bad_data = 1; |
| 202 | continue; |
| 203 | } |
| 204 | bad_data = 0; |
| 205 | if ((buf[14] & 1) == 0) |
| 206 | break; |
| 207 | |
| 208 | decode_report(ts, buf); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 209 | } |
| 210 | if (ts->use_irq) |
| 211 | enable_irq(ts->client->irq); |
| 212 | } |
| 213 | |
| 214 | static enum hrtimer_restart synaptics_ts_timer_func(struct hrtimer *timer) |
| 215 | { |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 216 | struct synaptics_ts_data *ts = |
| 217 | container_of(timer, struct synaptics_ts_data, timer); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 218 | |
| 219 | queue_work(synaptics_wq, &ts->work); |
| 220 | |
| 221 | hrtimer_start(&ts->timer, ktime_set(0, 12500000), HRTIMER_MODE_REL); |
| 222 | return HRTIMER_NORESTART; |
| 223 | } |
| 224 | |
| 225 | static irqreturn_t synaptics_ts_irq_handler(int irq, void *dev_id) |
| 226 | { |
| 227 | struct synaptics_ts_data *ts = dev_id; |
| 228 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 229 | disable_irq_nosync(ts->client->irq); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 230 | queue_work(synaptics_wq, &ts->work); |
| 231 | return IRQ_HANDLED; |
| 232 | } |
| 233 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 234 | static int detect(struct synaptics_ts_data *ts, u32 *panel_version) |
| 235 | { |
| 236 | int ret; |
| 237 | int retry = 10; |
| 238 | |
| 239 | ret = i2c_set(ts, 0xf4, 0x01, "reset device"); |
| 240 | |
| 241 | while (retry-- > 0) { |
| 242 | ret = i2c_smbus_read_byte_data(ts->client, 0xe4); |
| 243 | if (ret >= 0) |
| 244 | break; |
| 245 | msleep(100); |
| 246 | } |
| 247 | if (ret < 0) { |
| 248 | pr_err("i2c_smbus_read_byte_data failed\n"); |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | *panel_version = ret << 8; |
| 253 | ret = i2c_read(ts, 0xe5, "product minor"); |
| 254 | if (ret < 0) |
| 255 | return ret; |
| 256 | *panel_version |= ret; |
| 257 | |
| 258 | ret = i2c_read(ts, 0xe3, "property"); |
| 259 | if (ret < 0) |
| 260 | return ret; |
| 261 | |
| 262 | pr_info("synaptics: version %x, product property %x\n", |
| 263 | *panel_version, ret); |
| 264 | return 0; |
| 265 | } |
| 266 | |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 267 | static void compute_areas(struct synaptics_ts_data *ts, |
| 268 | struct synaptics_i2c_rmi_platform_data *pdata, |
| 269 | u16 max_x, u16 max_y) |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 270 | { |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 271 | int inactive_area_left; |
| 272 | int inactive_area_right; |
| 273 | int inactive_area_top; |
| 274 | int inactive_area_bottom; |
| 275 | int snap_left_on; |
| 276 | int snap_left_off; |
| 277 | int snap_right_on; |
| 278 | int snap_right_off; |
| 279 | int snap_top_on; |
| 280 | int snap_top_off; |
| 281 | int snap_bottom_on; |
| 282 | int snap_bottom_off; |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 283 | int fuzz_x; |
| 284 | int fuzz_y; |
| 285 | int fuzz_p; |
| 286 | int fuzz_w; |
| 287 | int swapped = !!(ts->flags & SYNAPTICS_SWAP_XY); |
| 288 | |
| 289 | inactive_area_left = pdata->inactive_left; |
| 290 | inactive_area_right = pdata->inactive_right; |
| 291 | inactive_area_top = pdata->inactive_top; |
| 292 | inactive_area_bottom = pdata->inactive_bottom; |
| 293 | snap_left_on = pdata->snap_left_on; |
| 294 | snap_left_off = pdata->snap_left_off; |
| 295 | snap_right_on = pdata->snap_right_on; |
| 296 | snap_right_off = pdata->snap_right_off; |
| 297 | snap_top_on = pdata->snap_top_on; |
| 298 | snap_top_off = pdata->snap_top_off; |
| 299 | snap_bottom_on = pdata->snap_bottom_on; |
| 300 | snap_bottom_off = pdata->snap_bottom_off; |
| 301 | fuzz_x = pdata->fuzz_x; |
| 302 | fuzz_y = pdata->fuzz_y; |
| 303 | fuzz_p = pdata->fuzz_p; |
| 304 | fuzz_w = pdata->fuzz_w; |
| 305 | |
| 306 | inactive_area_left = inactive_area_left * max_x / 0x10000; |
| 307 | inactive_area_right = inactive_area_right * max_x / 0x10000; |
| 308 | inactive_area_top = inactive_area_top * max_y / 0x10000; |
| 309 | inactive_area_bottom = inactive_area_bottom * max_y / 0x10000; |
| 310 | snap_left_on = snap_left_on * max_x / 0x10000; |
| 311 | snap_left_off = snap_left_off * max_x / 0x10000; |
| 312 | snap_right_on = snap_right_on * max_x / 0x10000; |
| 313 | snap_right_off = snap_right_off * max_x / 0x10000; |
| 314 | snap_top_on = snap_top_on * max_y / 0x10000; |
| 315 | snap_top_off = snap_top_off * max_y / 0x10000; |
| 316 | snap_bottom_on = snap_bottom_on * max_y / 0x10000; |
| 317 | snap_bottom_off = snap_bottom_off * max_y / 0x10000; |
| 318 | fuzz_x = fuzz_x * max_x / 0x10000; |
| 319 | fuzz_y = fuzz_y * max_y / 0x10000; |
| 320 | |
| 321 | |
| 322 | ts->snap_down[swapped] = -inactive_area_left; |
| 323 | ts->snap_up[swapped] = max_x + inactive_area_right; |
| 324 | ts->snap_down[!swapped] = -inactive_area_top; |
| 325 | ts->snap_up[!swapped] = max_y + inactive_area_bottom; |
| 326 | ts->snap_down_on[swapped] = snap_left_on; |
| 327 | ts->snap_down_off[swapped] = snap_left_off; |
| 328 | ts->snap_up_on[swapped] = max_x - snap_right_on; |
| 329 | ts->snap_up_off[swapped] = max_x - snap_right_off; |
| 330 | ts->snap_down_on[!swapped] = snap_top_on; |
| 331 | ts->snap_down_off[!swapped] = snap_top_off; |
| 332 | ts->snap_up_on[!swapped] = max_y - snap_bottom_on; |
| 333 | ts->snap_up_off[!swapped] = max_y - snap_bottom_off; |
| 334 | pr_info("synaptics_ts_probe: max_x %d, max_y %d\n", max_x, max_y); |
| 335 | pr_info("synaptics_ts_probe: inactive_x %d %d, inactive_y %d %d\n", |
| 336 | inactive_area_left, inactive_area_right, |
| 337 | inactive_area_top, inactive_area_bottom); |
| 338 | pr_info("synaptics_ts_probe: snap_x %d-%d %d-%d, snap_y %d-%d %d-%d\n", |
| 339 | snap_left_on, snap_left_off, snap_right_on, snap_right_off, |
| 340 | snap_top_on, snap_top_off, snap_bottom_on, snap_bottom_off); |
| 341 | |
| 342 | input_set_abs_params(ts->input_dev, ABS_X, |
| 343 | -inactive_area_left, max_x + inactive_area_right, |
| 344 | fuzz_x, 0); |
| 345 | input_set_abs_params(ts->input_dev, ABS_Y, |
| 346 | -inactive_area_top, max_y + inactive_area_bottom, |
| 347 | fuzz_y, 0); |
| 348 | input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 255, fuzz_p, 0); |
| 349 | input_set_abs_params(ts->input_dev, ABS_TOOL_WIDTH, 0, 15, fuzz_w, 0); |
| 350 | input_set_abs_params(ts->input_dev, ABS_HAT0X, -inactive_area_left, |
| 351 | max_x + inactive_area_right, fuzz_x, 0); |
| 352 | input_set_abs_params(ts->input_dev, ABS_HAT0Y, -inactive_area_top, |
| 353 | max_y + inactive_area_bottom, fuzz_y, 0); |
| 354 | } |
| 355 | |
| 356 | static struct synaptics_i2c_rmi_platform_data fake_pdata; |
| 357 | |
| 358 | static int __devinit synaptics_ts_probe( |
| 359 | struct i2c_client *client, const struct i2c_device_id *id) |
| 360 | { |
| 361 | struct synaptics_ts_data *ts; |
| 362 | u8 buf0[4]; |
| 363 | u8 buf1[8]; |
| 364 | struct i2c_msg msg[2]; |
| 365 | int ret = 0; |
| 366 | struct synaptics_i2c_rmi_platform_data *pdata; |
| 367 | u32 panel_version = 0; |
| 368 | u16 max_x, max_y; |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 369 | |
| 370 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 371 | pr_err("synaptics_ts_probe: need I2C_FUNC_I2C\n"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 372 | ret = -ENODEV; |
| 373 | goto err_check_functionality_failed; |
| 374 | } |
| 375 | |
Pavel Machek | c60adf3 | 2009-08-08 15:40:50 +0200 | [diff] [blame^] | 376 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) { |
| 377 | pr_err("synaptics_ts_probe: need I2C_FUNC_SMBUS_WORD_DATA\n"); |
| 378 | ret = -ENODEV; |
| 379 | goto err_check_functionality_failed; |
| 380 | } |
| 381 | |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 382 | ts = kzalloc(sizeof(*ts), GFP_KERNEL); |
| 383 | if (ts == NULL) { |
| 384 | ret = -ENOMEM; |
| 385 | goto err_alloc_data_failed; |
| 386 | } |
| 387 | INIT_WORK(&ts->work, synaptics_ts_work_func); |
| 388 | ts->client = client; |
| 389 | i2c_set_clientdata(client, ts); |
| 390 | pdata = client->dev.platform_data; |
| 391 | if (pdata) |
| 392 | ts->power = pdata->power; |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 393 | else |
| 394 | pdata = &fake_pdata; |
| 395 | |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 396 | if (ts->power) { |
| 397 | ret = ts->power(1); |
| 398 | if (ret < 0) { |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 399 | pr_err("synaptics_ts_probe power on failed\n"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 400 | goto err_power_failed; |
| 401 | } |
| 402 | } |
| 403 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 404 | ret = detect(ts, &panel_version); |
| 405 | if (ret) |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 406 | goto err_detect_failed; |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 407 | |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 408 | while (pdata->version > panel_version) |
| 409 | pdata++; |
| 410 | ts->flags = pdata->flags; |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 411 | |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 412 | ret = i2c_read(ts, 0xf0, "device control"); |
| 413 | if (ret < 0) |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 414 | goto err_detect_failed; |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 415 | pr_info("synaptics: device control %x\n", ret); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 416 | |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 417 | ret = i2c_read(ts, 0xf1, "interrupt enable"); |
| 418 | if (ret < 0) |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 419 | goto err_detect_failed; |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 420 | pr_info("synaptics_ts_probe: interrupt enable %x\n", ret); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 421 | |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 422 | ret = i2c_set(ts, 0xf1, 0, "disable interrupt"); |
| 423 | if (ret < 0) |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 424 | goto err_detect_failed; |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 425 | |
| 426 | msg[0].addr = ts->client->addr; |
| 427 | msg[0].flags = 0; |
| 428 | msg[0].len = 1; |
| 429 | msg[0].buf = buf0; |
| 430 | buf0[0] = 0xe0; |
| 431 | msg[1].addr = ts->client->addr; |
| 432 | msg[1].flags = I2C_M_RD; |
| 433 | msg[1].len = 8; |
| 434 | msg[1].buf = buf1; |
| 435 | ret = i2c_transfer(ts->client->adapter, msg, 2); |
| 436 | if (ret < 0) { |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 437 | pr_err("i2c_transfer failed\n"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 438 | goto err_detect_failed; |
| 439 | } |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 440 | pr_info("synaptics_ts_probe: 0xe0: %x %x %x %x %x %x %x %x\n", |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 441 | buf1[0], buf1[1], buf1[2], buf1[3], |
| 442 | buf1[4], buf1[5], buf1[6], buf1[7]); |
| 443 | |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 444 | ret = i2c_set(ts, 0xff, 0x10, "page select = 0x10"); |
| 445 | if (ret < 0) |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 446 | goto err_detect_failed; |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 447 | |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 448 | ret = i2c_smbus_read_word_data(ts->client, 0x04); |
| 449 | if (ret < 0) { |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 450 | pr_err("i2c_smbus_read_word_data failed\n"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 451 | goto err_detect_failed; |
| 452 | } |
| 453 | ts->max[0] = max_x = (ret >> 8 & 0xff) | ((ret & 0x1f) << 8); |
| 454 | ret = i2c_smbus_read_word_data(ts->client, 0x06); |
| 455 | if (ret < 0) { |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 456 | pr_err("i2c_smbus_read_word_data failed\n"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 457 | goto err_detect_failed; |
| 458 | } |
| 459 | ts->max[1] = max_y = (ret >> 8 & 0xff) | ((ret & 0x1f) << 8); |
| 460 | if (ts->flags & SYNAPTICS_SWAP_XY) |
| 461 | swap(max_x, max_y); |
| 462 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 463 | /* will also switch back to page 0x04 */ |
| 464 | ret = synaptics_init_panel(ts); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 465 | if (ret < 0) { |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 466 | pr_err("synaptics_init_panel failed\n"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 467 | goto err_detect_failed; |
| 468 | } |
| 469 | |
| 470 | ts->input_dev = input_allocate_device(); |
| 471 | if (ts->input_dev == NULL) { |
| 472 | ret = -ENOMEM; |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 473 | pr_err("synaptics: Failed to allocate input device\n"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 474 | goto err_input_dev_alloc_failed; |
| 475 | } |
| 476 | ts->input_dev->name = "synaptics-rmi-touchscreen"; |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 477 | ts->input_dev->phys = "msm/input0"; |
| 478 | ts->input_dev->id.bustype = BUS_I2C; |
| 479 | |
| 480 | __set_bit(EV_SYN, ts->input_dev->evbit); |
| 481 | __set_bit(EV_KEY, ts->input_dev->evbit); |
| 482 | __set_bit(BTN_TOUCH, ts->input_dev->keybit); |
| 483 | __set_bit(BTN_2, ts->input_dev->keybit); |
| 484 | __set_bit(EV_ABS, ts->input_dev->evbit); |
| 485 | |
| 486 | compute_areas(ts, pdata, max_x, max_y); |
| 487 | |
| 488 | |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 489 | ret = input_register_device(ts->input_dev); |
| 490 | if (ret) { |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 491 | pr_err("synaptics: Unable to register %s input device\n", |
| 492 | ts->input_dev->name); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 493 | goto err_input_register_device_failed; |
| 494 | } |
| 495 | if (client->irq) { |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 496 | ret = request_irq(client->irq, synaptics_ts_irq_handler, |
| 497 | 0, client->name, ts); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 498 | if (ret == 0) { |
Pavel Machek | 4191934 | 2009-07-21 12:04:26 +0200 | [diff] [blame] | 499 | ret = i2c_set(ts, 0xf1, 0x01, "enable abs int"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 500 | if (ret) |
| 501 | free_irq(client->irq, ts); |
| 502 | } |
| 503 | if (ret == 0) |
| 504 | ts->use_irq = 1; |
| 505 | else |
| 506 | dev_err(&client->dev, "request_irq failed\n"); |
| 507 | } |
| 508 | if (!ts->use_irq) { |
| 509 | hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 510 | ts->timer.function = synaptics_ts_timer_func; |
| 511 | hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL); |
| 512 | } |
| 513 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 514 | ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; |
| 515 | ts->early_suspend.suspend = synaptics_ts_early_suspend; |
| 516 | ts->early_suspend.resume = synaptics_ts_late_resume; |
| 517 | register_early_suspend(&ts->early_suspend); |
| 518 | #endif |
| 519 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 520 | pr_info("synaptics: Start touchscreen %s in %s mode\n", |
| 521 | ts->input_dev->name, ts->use_irq ? "interrupt" : "polling"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 522 | |
| 523 | return 0; |
| 524 | |
| 525 | err_input_register_device_failed: |
| 526 | input_free_device(ts->input_dev); |
| 527 | |
| 528 | err_input_dev_alloc_failed: |
| 529 | err_detect_failed: |
| 530 | err_power_failed: |
| 531 | kfree(ts); |
| 532 | err_alloc_data_failed: |
| 533 | err_check_functionality_failed: |
| 534 | return ret; |
| 535 | } |
| 536 | |
| 537 | static int synaptics_ts_remove(struct i2c_client *client) |
| 538 | { |
| 539 | struct synaptics_ts_data *ts = i2c_get_clientdata(client); |
| 540 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 541 | unregister_early_suspend(&ts->early_suspend); |
| 542 | #endif |
| 543 | if (ts->use_irq) |
| 544 | free_irq(client->irq, ts); |
| 545 | else |
| 546 | hrtimer_cancel(&ts->timer); |
| 547 | input_unregister_device(ts->input_dev); |
| 548 | kfree(ts); |
| 549 | return 0; |
| 550 | } |
| 551 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 552 | #ifdef CONFIG_PM |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 553 | static int synaptics_ts_suspend(struct i2c_client *client, pm_message_t mesg) |
| 554 | { |
| 555 | int ret; |
| 556 | struct synaptics_ts_data *ts = i2c_get_clientdata(client); |
| 557 | |
| 558 | if (ts->use_irq) |
| 559 | disable_irq(client->irq); |
| 560 | else |
| 561 | hrtimer_cancel(&ts->timer); |
| 562 | ret = cancel_work_sync(&ts->work); |
| 563 | if (ret && ts->use_irq) /* if work was pending disable-count is now 2 */ |
| 564 | enable_irq(client->irq); |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 565 | i2c_set(ts, 0xf1, 0, "disable interrupt"); |
| 566 | i2c_set(ts, 0xf0, 0x86, "deep sleep"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 567 | |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 568 | if (ts->power) { |
| 569 | ret = ts->power(0); |
| 570 | if (ret < 0) |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 571 | pr_err("synaptics_ts_suspend power off failed\n"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 572 | } |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int synaptics_ts_resume(struct i2c_client *client) |
| 577 | { |
| 578 | int ret; |
| 579 | struct synaptics_ts_data *ts = i2c_get_clientdata(client); |
| 580 | |
| 581 | if (ts->power) { |
| 582 | ret = ts->power(1); |
| 583 | if (ret < 0) |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 584 | pr_err("synaptics_ts_resume power on failed\n"); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | synaptics_init_panel(ts); |
| 588 | |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 589 | if (ts->use_irq) { |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 590 | enable_irq(client->irq); |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 591 | i2c_set(ts, 0xf1, 0x01, "enable abs int"); |
| 592 | } else |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 593 | hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL); |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 599 | static void synaptics_ts_early_suspend(struct early_suspend *h) |
| 600 | { |
| 601 | struct synaptics_ts_data *ts; |
| 602 | ts = container_of(h, struct synaptics_ts_data, early_suspend); |
| 603 | synaptics_ts_suspend(ts->client, PMSG_SUSPEND); |
| 604 | } |
| 605 | |
| 606 | static void synaptics_ts_late_resume(struct early_suspend *h) |
| 607 | { |
| 608 | struct synaptics_ts_data *ts; |
| 609 | ts = container_of(h, struct synaptics_ts_data, early_suspend); |
| 610 | synaptics_ts_resume(ts->client); |
| 611 | } |
| 612 | #endif |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 613 | #else |
| 614 | #define synaptics_ts_suspend NULL |
| 615 | #define synaptics_ts_resume NULL |
| 616 | #endif |
| 617 | |
| 618 | |
Arve Hjønnevåg | 2e4d2af | 2009-07-20 12:53:40 +0200 | [diff] [blame] | 619 | |
| 620 | static const struct i2c_device_id synaptics_ts_id[] = { |
| 621 | { SYNAPTICS_I2C_RMI_NAME, 0 }, |
| 622 | { } |
| 623 | }; |
| 624 | |
| 625 | static struct i2c_driver synaptics_ts_driver = { |
| 626 | .probe = synaptics_ts_probe, |
| 627 | .remove = synaptics_ts_remove, |
| 628 | #ifndef CONFIG_HAS_EARLYSUSPEND |
| 629 | .suspend = synaptics_ts_suspend, |
| 630 | .resume = synaptics_ts_resume, |
| 631 | #endif |
| 632 | .id_table = synaptics_ts_id, |
| 633 | .driver = { |
| 634 | .name = SYNAPTICS_I2C_RMI_NAME, |
| 635 | }, |
| 636 | }; |
| 637 | |
| 638 | static int __devinit synaptics_ts_init(void) |
| 639 | { |
| 640 | synaptics_wq = create_singlethread_workqueue("synaptics_wq"); |
| 641 | if (!synaptics_wq) |
| 642 | return -ENOMEM; |
| 643 | return i2c_add_driver(&synaptics_ts_driver); |
| 644 | } |
| 645 | |
| 646 | static void __exit synaptics_ts_exit(void) |
| 647 | { |
| 648 | i2c_del_driver(&synaptics_ts_driver); |
| 649 | if (synaptics_wq) |
| 650 | destroy_workqueue(synaptics_wq); |
| 651 | } |
| 652 | |
| 653 | module_init(synaptics_ts_init); |
| 654 | module_exit(synaptics_ts_exit); |
| 655 | |
| 656 | MODULE_DESCRIPTION("Synaptics Touchscreen Driver"); |
| 657 | MODULE_LICENSE("GPL"); |
Pavel Machek | 261314a | 2009-07-20 23:59:11 +0200 | [diff] [blame] | 658 | MODULE_AUTHOR("Arve Hjønnevåg <arve@android.com>"); |