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