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