Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Pixcir I2C touchscreen controllers. |
| 3 | * |
| 4 | * Copyright (C) 2010-2011 Pixcir, Inc. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/input.h> |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 26 | #include <linux/input/mt.h> |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 27 | #include <linux/input/pixcir_ts.h> |
Roger Quadros | 0dfc8d4 | 2014-05-18 22:46:43 -0700 | [diff] [blame] | 28 | #include <linux/gpio.h> |
Roger Quadros | a405459 | 2014-07-28 10:05:39 -0700 | [diff] [blame^] | 29 | #include <linux/of.h> |
| 30 | #include <linux/of_gpio.h> |
| 31 | #include <linux/of_device.h> |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 32 | |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 33 | #define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */ |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 34 | |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 35 | struct pixcir_i2c_ts_data { |
| 36 | struct i2c_client *client; |
| 37 | struct input_dev *input; |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 38 | const struct pixcir_ts_platform_data *pdata; |
Roger Quadros | 3b36fbb | 2014-05-18 22:44:35 -0700 | [diff] [blame] | 39 | bool running; |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 40 | int max_fingers; /* Max fingers supported in this instance */ |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 43 | struct pixcir_touch { |
| 44 | int x; |
| 45 | int y; |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 46 | int id; |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | struct pixcir_report_data { |
| 50 | int num_touches; |
| 51 | struct pixcir_touch touches[PIXCIR_MAX_SLOTS]; |
| 52 | }; |
| 53 | |
| 54 | static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata, |
| 55 | struct pixcir_report_data *report) |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 56 | { |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 57 | u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5]; |
| 58 | u8 wrbuf[1] = { 0 }; |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 59 | u8 *bufptr; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 60 | u8 touch; |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 61 | int ret, i; |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 62 | int readsize; |
| 63 | const struct pixcir_i2c_chip_data *chip = &tsdata->pdata->chip; |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 64 | |
| 65 | memset(report, 0, sizeof(struct pixcir_report_data)); |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 66 | |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 67 | i = chip->has_hw_ids ? 1 : 0; |
| 68 | readsize = 2 + tsdata->max_fingers * (4 + i); |
| 69 | if (readsize > sizeof(rdbuf)) |
| 70 | readsize = sizeof(rdbuf); |
| 71 | |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 72 | ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf)); |
| 73 | if (ret != sizeof(wrbuf)) { |
| 74 | dev_err(&tsdata->client->dev, |
| 75 | "%s: i2c_master_send failed(), ret=%d\n", |
| 76 | __func__, ret); |
| 77 | return; |
| 78 | } |
| 79 | |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 80 | ret = i2c_master_recv(tsdata->client, rdbuf, readsize); |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 81 | if (ret != sizeof(rdbuf)) { |
| 82 | dev_err(&tsdata->client->dev, |
| 83 | "%s: i2c_master_recv failed(), ret=%d\n", |
| 84 | __func__, ret); |
| 85 | return; |
| 86 | } |
| 87 | |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 88 | touch = rdbuf[0] & 0x7; |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 89 | if (touch > tsdata->max_fingers) |
| 90 | touch = tsdata->max_fingers; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 91 | |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 92 | report->num_touches = touch; |
| 93 | bufptr = &rdbuf[2]; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 94 | |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 95 | for (i = 0; i < touch; i++) { |
| 96 | report->touches[i].x = (bufptr[1] << 8) | bufptr[0]; |
| 97 | report->touches[i].y = (bufptr[3] << 8) | bufptr[2]; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 98 | |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 99 | if (chip->has_hw_ids) { |
| 100 | report->touches[i].id = bufptr[4]; |
| 101 | bufptr = bufptr + 5; |
| 102 | } else { |
| 103 | bufptr = bufptr + 4; |
| 104 | } |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts, |
| 109 | struct pixcir_report_data *report) |
| 110 | { |
| 111 | struct input_mt_pos pos[PIXCIR_MAX_SLOTS]; |
| 112 | int slots[PIXCIR_MAX_SLOTS]; |
| 113 | struct pixcir_touch *touch; |
| 114 | int n, i, slot; |
| 115 | struct device *dev = &ts->client->dev; |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 116 | const struct pixcir_i2c_chip_data *chip = &ts->pdata->chip; |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 117 | |
| 118 | n = report->num_touches; |
| 119 | if (n > PIXCIR_MAX_SLOTS) |
| 120 | n = PIXCIR_MAX_SLOTS; |
| 121 | |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 122 | if (!chip->has_hw_ids) { |
| 123 | for (i = 0; i < n; i++) { |
| 124 | touch = &report->touches[i]; |
| 125 | pos[i].x = touch->x; |
| 126 | pos[i].y = touch->y; |
| 127 | } |
| 128 | |
| 129 | input_mt_assign_slots(ts->input, slots, pos, n); |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 132 | for (i = 0; i < n; i++) { |
| 133 | touch = &report->touches[i]; |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 134 | |
| 135 | if (chip->has_hw_ids) { |
| 136 | slot = input_mt_get_slot_by_key(ts->input, touch->id); |
| 137 | if (slot < 0) { |
| 138 | dev_dbg(dev, "no free slot for id 0x%x\n", |
| 139 | touch->id); |
| 140 | continue; |
| 141 | } |
| 142 | } else { |
| 143 | slot = slots[i]; |
| 144 | } |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 145 | |
| 146 | input_mt_slot(ts->input, slot); |
| 147 | input_mt_report_slot_state(ts->input, |
| 148 | MT_TOOL_FINGER, true); |
| 149 | |
| 150 | input_event(ts->input, EV_ABS, ABS_MT_POSITION_X, touch->x); |
| 151 | input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y, touch->y); |
| 152 | |
| 153 | dev_dbg(dev, "%d: slot %d, x %d, y %d\n", |
| 154 | i, slot, touch->x, touch->y); |
| 155 | } |
| 156 | |
| 157 | input_mt_sync_frame(ts->input); |
| 158 | input_sync(ts->input); |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static irqreturn_t pixcir_ts_isr(int irq, void *dev_id) |
| 162 | { |
| 163 | struct pixcir_i2c_ts_data *tsdata = dev_id; |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 164 | const struct pixcir_ts_platform_data *pdata = tsdata->pdata; |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 165 | struct pixcir_report_data report; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 166 | |
Roger Quadros | 3b36fbb | 2014-05-18 22:44:35 -0700 | [diff] [blame] | 167 | while (tsdata->running) { |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 168 | /* parse packet */ |
| 169 | pixcir_ts_parse(tsdata, &report); |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 170 | |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 171 | /* report it */ |
| 172 | pixcir_ts_report(tsdata, &report); |
| 173 | |
| 174 | if (gpio_get_value(pdata->gpio_attb)) { |
| 175 | if (report.num_touches) { |
| 176 | /* |
| 177 | * Last report with no finger up? |
| 178 | * Do it now then. |
| 179 | */ |
| 180 | input_mt_sync_frame(tsdata->input); |
| 181 | input_sync(tsdata->input); |
| 182 | } |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 183 | break; |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 184 | } |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 185 | |
| 186 | msleep(20); |
| 187 | } |
| 188 | |
| 189 | return IRQ_HANDLED; |
| 190 | } |
| 191 | |
Roger Quadros | 3b36fbb | 2014-05-18 22:44:35 -0700 | [diff] [blame] | 192 | static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts, |
| 193 | enum pixcir_power_mode mode) |
| 194 | { |
| 195 | struct device *dev = &ts->client->dev; |
| 196 | int ret; |
| 197 | |
| 198 | ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE); |
| 199 | if (ret < 0) { |
| 200 | dev_err(dev, "%s: can't read reg 0x%x : %d\n", |
| 201 | __func__, PIXCIR_REG_POWER_MODE, ret); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | ret &= ~PIXCIR_POWER_MODE_MASK; |
| 206 | ret |= mode; |
| 207 | |
| 208 | /* Always AUTO_IDLE */ |
| 209 | ret |= PIXCIR_POWER_ALLOW_IDLE; |
| 210 | |
| 211 | ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret); |
| 212 | if (ret < 0) { |
| 213 | dev_err(dev, "%s: can't write reg 0x%x : %d\n", |
| 214 | __func__, PIXCIR_REG_POWER_MODE, ret); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Set the interrupt mode for the device i.e. ATTB line behaviour |
| 223 | * |
| 224 | * @polarity : 1 for active high, 0 for active low. |
| 225 | */ |
| 226 | static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts, |
| 227 | enum pixcir_int_mode mode, bool polarity) |
| 228 | { |
| 229 | struct device *dev = &ts->client->dev; |
| 230 | int ret; |
| 231 | |
| 232 | ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE); |
| 233 | if (ret < 0) { |
| 234 | dev_err(dev, "%s: can't read reg 0x%x : %d\n", |
| 235 | __func__, PIXCIR_REG_INT_MODE, ret); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | ret &= ~PIXCIR_INT_MODE_MASK; |
| 240 | ret |= mode; |
| 241 | |
| 242 | if (polarity) |
| 243 | ret |= PIXCIR_INT_POL_HIGH; |
| 244 | else |
| 245 | ret &= ~PIXCIR_INT_POL_HIGH; |
| 246 | |
| 247 | ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret); |
| 248 | if (ret < 0) { |
| 249 | dev_err(dev, "%s: can't write reg 0x%x : %d\n", |
| 250 | __func__, PIXCIR_REG_INT_MODE, ret); |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Enable/disable interrupt generation |
| 259 | */ |
| 260 | static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable) |
| 261 | { |
| 262 | struct device *dev = &ts->client->dev; |
| 263 | int ret; |
| 264 | |
| 265 | ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE); |
| 266 | if (ret < 0) { |
| 267 | dev_err(dev, "%s: can't read reg 0x%x : %d\n", |
| 268 | __func__, PIXCIR_REG_INT_MODE, ret); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | if (enable) |
| 273 | ret |= PIXCIR_INT_ENABLE; |
| 274 | else |
| 275 | ret &= ~PIXCIR_INT_ENABLE; |
| 276 | |
| 277 | ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret); |
| 278 | if (ret < 0) { |
| 279 | dev_err(dev, "%s: can't write reg 0x%x : %d\n", |
| 280 | __func__, PIXCIR_REG_INT_MODE, ret); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int pixcir_start(struct pixcir_i2c_ts_data *ts) |
| 288 | { |
| 289 | struct device *dev = &ts->client->dev; |
| 290 | int error; |
| 291 | |
| 292 | /* LEVEL_TOUCH interrupt with active low polarity */ |
| 293 | error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0); |
| 294 | if (error) { |
| 295 | dev_err(dev, "Failed to set interrupt mode: %d\n", error); |
| 296 | return error; |
| 297 | } |
| 298 | |
| 299 | ts->running = true; |
| 300 | mb(); /* Update status before IRQ can fire */ |
| 301 | |
| 302 | /* enable interrupt generation */ |
| 303 | error = pixcir_int_enable(ts, true); |
| 304 | if (error) { |
| 305 | dev_err(dev, "Failed to enable interrupt generation: %d\n", |
| 306 | error); |
| 307 | return error; |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int pixcir_stop(struct pixcir_i2c_ts_data *ts) |
| 314 | { |
| 315 | int error; |
| 316 | |
| 317 | /* Disable interrupt generation */ |
| 318 | error = pixcir_int_enable(ts, false); |
| 319 | if (error) { |
| 320 | dev_err(&ts->client->dev, |
| 321 | "Failed to disable interrupt generation: %d\n", |
| 322 | error); |
| 323 | return error; |
| 324 | } |
| 325 | |
| 326 | /* Exit ISR if running, no more report parsing */ |
| 327 | ts->running = false; |
| 328 | mb(); /* update status before we synchronize irq */ |
| 329 | |
| 330 | /* Wait till running ISR is complete */ |
| 331 | synchronize_irq(ts->client->irq); |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static int pixcir_input_open(struct input_dev *dev) |
| 337 | { |
| 338 | struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev); |
| 339 | |
| 340 | return pixcir_start(ts); |
| 341 | } |
| 342 | |
| 343 | static void pixcir_input_close(struct input_dev *dev) |
| 344 | { |
| 345 | struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev); |
| 346 | |
| 347 | pixcir_stop(ts); |
| 348 | } |
| 349 | |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 350 | #ifdef CONFIG_PM_SLEEP |
| 351 | static int pixcir_i2c_ts_suspend(struct device *dev) |
| 352 | { |
| 353 | struct i2c_client *client = to_i2c_client(dev); |
Roger Quadros | 7cdcb8d | 2014-05-18 22:49:20 -0700 | [diff] [blame] | 354 | struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client); |
| 355 | struct input_dev *input = ts->input; |
| 356 | int ret = 0; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 357 | |
Roger Quadros | 7cdcb8d | 2014-05-18 22:49:20 -0700 | [diff] [blame] | 358 | mutex_lock(&input->mutex); |
| 359 | |
| 360 | if (device_may_wakeup(&client->dev)) { |
| 361 | if (!input->users) { |
| 362 | ret = pixcir_start(ts); |
| 363 | if (ret) { |
| 364 | dev_err(dev, "Failed to start\n"); |
| 365 | goto unlock; |
| 366 | } |
| 367 | } |
| 368 | |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 369 | enable_irq_wake(client->irq); |
Roger Quadros | 7cdcb8d | 2014-05-18 22:49:20 -0700 | [diff] [blame] | 370 | } else if (input->users) { |
| 371 | ret = pixcir_stop(ts); |
| 372 | } |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 373 | |
Roger Quadros | 7cdcb8d | 2014-05-18 22:49:20 -0700 | [diff] [blame] | 374 | unlock: |
| 375 | mutex_unlock(&input->mutex); |
| 376 | |
| 377 | return ret; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static int pixcir_i2c_ts_resume(struct device *dev) |
| 381 | { |
| 382 | struct i2c_client *client = to_i2c_client(dev); |
Roger Quadros | 7cdcb8d | 2014-05-18 22:49:20 -0700 | [diff] [blame] | 383 | struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client); |
| 384 | struct input_dev *input = ts->input; |
| 385 | int ret = 0; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 386 | |
Roger Quadros | 7cdcb8d | 2014-05-18 22:49:20 -0700 | [diff] [blame] | 387 | mutex_lock(&input->mutex); |
| 388 | |
| 389 | if (device_may_wakeup(&client->dev)) { |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 390 | disable_irq_wake(client->irq); |
| 391 | |
Roger Quadros | 7cdcb8d | 2014-05-18 22:49:20 -0700 | [diff] [blame] | 392 | if (!input->users) { |
| 393 | ret = pixcir_stop(ts); |
| 394 | if (ret) { |
| 395 | dev_err(dev, "Failed to stop\n"); |
| 396 | goto unlock; |
| 397 | } |
| 398 | } |
| 399 | } else if (input->users) { |
| 400 | ret = pixcir_start(ts); |
| 401 | } |
| 402 | |
| 403 | unlock: |
| 404 | mutex_unlock(&input->mutex); |
| 405 | |
| 406 | return ret; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 407 | } |
| 408 | #endif |
| 409 | |
| 410 | static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops, |
| 411 | pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume); |
| 412 | |
Roger Quadros | a405459 | 2014-07-28 10:05:39 -0700 | [diff] [blame^] | 413 | #ifdef CONFIG_OF |
| 414 | static const struct of_device_id pixcir_of_match[]; |
| 415 | |
| 416 | static struct pixcir_ts_platform_data *pixcir_parse_dt(struct device *dev) |
| 417 | { |
| 418 | struct pixcir_ts_platform_data *pdata; |
| 419 | struct device_node *np = dev->of_node; |
| 420 | const struct of_device_id *match; |
| 421 | |
| 422 | match = of_match_device(of_match_ptr(pixcir_of_match), dev); |
| 423 | if (!match) |
| 424 | return ERR_PTR(-EINVAL); |
| 425 | |
| 426 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 427 | if (!pdata) |
| 428 | return ERR_PTR(-ENOMEM); |
| 429 | |
| 430 | pdata->chip = *(const struct pixcir_i2c_chip_data *)match->data; |
| 431 | |
| 432 | pdata->gpio_attb = of_get_named_gpio(np, "attb-gpio", 0); |
| 433 | /* gpio_attb validity is checked in probe */ |
| 434 | |
| 435 | if (of_property_read_u32(np, "touchscreen-size-x", &pdata->x_max)) { |
| 436 | dev_err(dev, "Failed to get touchscreen-size-x property\n"); |
| 437 | return ERR_PTR(-EINVAL); |
| 438 | } |
| 439 | pdata->x_max -= 1; |
| 440 | |
| 441 | if (of_property_read_u32(np, "touchscreen-size-y", &pdata->y_max)) { |
| 442 | dev_err(dev, "Failed to get touchscreen-size-y property\n"); |
| 443 | return ERR_PTR(-EINVAL); |
| 444 | } |
| 445 | pdata->y_max -= 1; |
| 446 | |
| 447 | dev_dbg(dev, "%s: x %d, y %d, gpio %d\n", __func__, |
| 448 | pdata->x_max + 1, pdata->y_max + 1, pdata->gpio_attb); |
| 449 | |
| 450 | return pdata; |
| 451 | } |
| 452 | #else |
| 453 | static struct pixcir_ts_platform_data *pixcir_parse_dt(struct device *dev) |
| 454 | { |
| 455 | return ERR_PTR(-EINVAL); |
| 456 | } |
| 457 | #endif |
| 458 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 459 | static int pixcir_i2c_ts_probe(struct i2c_client *client, |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 460 | const struct i2c_device_id *id) |
| 461 | { |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 462 | const struct pixcir_ts_platform_data *pdata = |
| 463 | dev_get_platdata(&client->dev); |
Roger Quadros | e9d4718 | 2014-05-18 22:43:42 -0700 | [diff] [blame] | 464 | struct device *dev = &client->dev; |
Roger Quadros | a405459 | 2014-07-28 10:05:39 -0700 | [diff] [blame^] | 465 | struct device_node *np = dev->of_node; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 466 | struct pixcir_i2c_ts_data *tsdata; |
| 467 | struct input_dev *input; |
| 468 | int error; |
| 469 | |
Roger Quadros | a405459 | 2014-07-28 10:05:39 -0700 | [diff] [blame^] | 470 | if (np && !pdata) { |
| 471 | pdata = pixcir_parse_dt(dev); |
| 472 | if (IS_ERR(pdata)) |
| 473 | return PTR_ERR(pdata); |
| 474 | } |
| 475 | |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 476 | if (!pdata) { |
| 477 | dev_err(&client->dev, "platform data not defined\n"); |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | |
Roger Quadros | 0dfc8d4 | 2014-05-18 22:46:43 -0700 | [diff] [blame] | 481 | if (!gpio_is_valid(pdata->gpio_attb)) { |
| 482 | dev_err(dev, "Invalid gpio_attb in pdata\n"); |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 486 | if (!pdata->chip.max_fingers) { |
| 487 | dev_err(dev, "Invalid max_fingers in pdata\n"); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
Roger Quadros | e9d4718 | 2014-05-18 22:43:42 -0700 | [diff] [blame] | 491 | tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL); |
| 492 | if (!tsdata) |
| 493 | return -ENOMEM; |
| 494 | |
| 495 | input = devm_input_allocate_device(dev); |
| 496 | if (!input) { |
| 497 | dev_err(dev, "Failed to allocate input device\n"); |
| 498 | return -ENOMEM; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | tsdata->client = client; |
| 502 | tsdata->input = input; |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 503 | tsdata->pdata = pdata; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 504 | |
| 505 | input->name = client->name; |
| 506 | input->id.bustype = BUS_I2C; |
Roger Quadros | 3b36fbb | 2014-05-18 22:44:35 -0700 | [diff] [blame] | 507 | input->open = pixcir_input_open; |
| 508 | input->close = pixcir_input_close; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 509 | input->dev.parent = &client->dev; |
| 510 | |
| 511 | __set_bit(EV_KEY, input->evbit); |
| 512 | __set_bit(EV_ABS, input->evbit); |
| 513 | __set_bit(BTN_TOUCH, input->keybit); |
| 514 | input_set_abs_params(input, ABS_X, 0, pdata->x_max, 0, 0); |
| 515 | input_set_abs_params(input, ABS_Y, 0, pdata->y_max, 0, 0); |
| 516 | input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0); |
| 517 | input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0); |
| 518 | |
Roger Quadros | 36874c7 | 2014-07-28 10:01:07 -0700 | [diff] [blame] | 519 | tsdata->max_fingers = tsdata->pdata->chip.max_fingers; |
| 520 | if (tsdata->max_fingers > PIXCIR_MAX_SLOTS) { |
| 521 | tsdata->max_fingers = PIXCIR_MAX_SLOTS; |
| 522 | dev_info(dev, "Limiting maximum fingers to %d\n", |
| 523 | tsdata->max_fingers); |
| 524 | } |
| 525 | |
| 526 | error = input_mt_init_slots(input, tsdata->max_fingers, |
Roger Quadros | 62e65b7 | 2014-07-28 09:58:54 -0700 | [diff] [blame] | 527 | INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); |
| 528 | if (error) { |
| 529 | dev_err(dev, "Error initializing Multi-Touch slots\n"); |
| 530 | return error; |
| 531 | } |
| 532 | |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 533 | input_set_drvdata(input, tsdata); |
| 534 | |
Roger Quadros | 0dfc8d4 | 2014-05-18 22:46:43 -0700 | [diff] [blame] | 535 | error = devm_gpio_request_one(dev, pdata->gpio_attb, |
| 536 | GPIOF_DIR_IN, "pixcir_i2c_attb"); |
| 537 | if (error) { |
| 538 | dev_err(dev, "Failed to request ATTB gpio\n"); |
| 539 | return error; |
| 540 | } |
| 541 | |
Roger Quadros | e9d4718 | 2014-05-18 22:43:42 -0700 | [diff] [blame] | 542 | error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr, |
| 543 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 544 | client->name, tsdata); |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 545 | if (error) { |
Roger Quadros | e9d4718 | 2014-05-18 22:43:42 -0700 | [diff] [blame] | 546 | dev_err(dev, "failed to request irq %d\n", client->irq); |
| 547 | return error; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Roger Quadros | 3b36fbb | 2014-05-18 22:44:35 -0700 | [diff] [blame] | 550 | /* Always be in IDLE mode to save power, device supports auto wake */ |
| 551 | error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE); |
| 552 | if (error) { |
| 553 | dev_err(dev, "Failed to set IDLE mode\n"); |
| 554 | return error; |
| 555 | } |
| 556 | |
| 557 | /* Stop device till opened */ |
| 558 | error = pixcir_stop(tsdata); |
| 559 | if (error) |
| 560 | return error; |
| 561 | |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 562 | error = input_register_device(input); |
| 563 | if (error) |
Roger Quadros | e9d4718 | 2014-05-18 22:43:42 -0700 | [diff] [blame] | 564 | return error; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 565 | |
Roger Quadros | 7cdcb8d | 2014-05-18 22:49:20 -0700 | [diff] [blame] | 566 | i2c_set_clientdata(client, tsdata); |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 567 | device_init_wakeup(&client->dev, 1); |
| 568 | |
| 569 | return 0; |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 570 | } |
| 571 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 572 | static int pixcir_i2c_ts_remove(struct i2c_client *client) |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 573 | { |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 574 | device_init_wakeup(&client->dev, 0); |
| 575 | |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static const struct i2c_device_id pixcir_i2c_ts_id[] = { |
| 580 | { "pixcir_ts", 0 }, |
Roger Quadros | a405459 | 2014-07-28 10:05:39 -0700 | [diff] [blame^] | 581 | { "pixcir_tangoc", 0 }, |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 582 | { } |
| 583 | }; |
| 584 | MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id); |
| 585 | |
Roger Quadros | a405459 | 2014-07-28 10:05:39 -0700 | [diff] [blame^] | 586 | #ifdef CONFIG_OF |
| 587 | static const struct pixcir_i2c_chip_data pixcir_ts_data = { |
| 588 | .max_fingers = 2, |
| 589 | /* no hw id support */ |
| 590 | }; |
| 591 | |
| 592 | static const struct pixcir_i2c_chip_data pixcir_tangoc_data = { |
| 593 | .max_fingers = 5, |
| 594 | .has_hw_ids = true, |
| 595 | }; |
| 596 | |
| 597 | static const struct of_device_id pixcir_of_match[] = { |
| 598 | { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data }, |
| 599 | { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data }, |
| 600 | { } |
| 601 | }; |
| 602 | MODULE_DEVICE_TABLE(of, pixcir_of_match); |
| 603 | #endif |
| 604 | |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 605 | static struct i2c_driver pixcir_i2c_ts_driver = { |
| 606 | .driver = { |
| 607 | .owner = THIS_MODULE, |
| 608 | .name = "pixcir_ts", |
| 609 | .pm = &pixcir_dev_pm_ops, |
Roger Quadros | a405459 | 2014-07-28 10:05:39 -0700 | [diff] [blame^] | 610 | .of_match_table = of_match_ptr(pixcir_of_match), |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 611 | }, |
| 612 | .probe = pixcir_i2c_ts_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 613 | .remove = pixcir_i2c_ts_remove, |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 614 | .id_table = pixcir_i2c_ts_id, |
| 615 | }; |
| 616 | |
Dmitry Torokhov | 4a53383 | 2012-03-16 23:05:44 -0700 | [diff] [blame] | 617 | module_i2c_driver(pixcir_i2c_ts_driver); |
Jianchun Bian | 36a281e | 2011-12-30 15:16:21 -0800 | [diff] [blame] | 618 | |
| 619 | MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>"); |
| 620 | MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver"); |
| 621 | MODULE_LICENSE("GPL"); |