Kuninori Morimoto | 4aeba64 | 2018-11-12 11:30:06 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 2 | /* |
| 3 | * ST1232 Touchscreen Controller Driver |
| 4 | * |
| 5 | * Copyright (C) 2010 Renesas Solutions Corp. |
| 6 | * Tony SIM <chinyeow.sim.xt@renesas.com> |
| 7 | * |
| 8 | * Using code from: |
| 9 | * - android.git.kernel.org: projects/kernel/common.git: synaptics_i2c_rmi.c |
| 10 | * Copyright (C) 2007 Google, Inc. |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/delay.h> |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 14 | #include <linux/gpio.h> |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 15 | #include <linux/i2c.h> |
| 16 | #include <linux/input.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/module.h> |
Sachin Kamat | 4d6e482 | 2013-10-06 00:56:40 -0700 | [diff] [blame] | 19 | #include <linux/of.h> |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 20 | #include <linux/of_gpio.h> |
Rafael J. Wysocki | 9ee27ff | 2011-12-23 01:24:25 +0100 | [diff] [blame] | 21 | #include <linux/pm_qos.h> |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/types.h> |
| 24 | |
| 25 | #define ST1232_TS_NAME "st1232-ts" |
| 26 | |
| 27 | #define MIN_X 0x00 |
| 28 | #define MIN_Y 0x00 |
| 29 | #define MAX_X 0x31f /* (800 - 1) */ |
| 30 | #define MAX_Y 0x1df /* (480 - 1) */ |
| 31 | #define MAX_AREA 0xff |
| 32 | #define MAX_FINGERS 2 |
| 33 | |
| 34 | struct st1232_ts_finger { |
| 35 | u16 x; |
| 36 | u16 y; |
| 37 | u8 t; |
| 38 | bool is_valid; |
| 39 | }; |
| 40 | |
| 41 | struct st1232_ts_data { |
| 42 | struct i2c_client *client; |
| 43 | struct input_dev *input_dev; |
| 44 | struct st1232_ts_finger finger[MAX_FINGERS]; |
Rafael J. Wysocki | 9ee27ff | 2011-12-23 01:24:25 +0100 | [diff] [blame] | 45 | struct dev_pm_qos_request low_latency_req; |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 46 | int reset_gpio; |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static int st1232_ts_read_data(struct st1232_ts_data *ts) |
| 50 | { |
| 51 | struct st1232_ts_finger *finger = ts->finger; |
| 52 | struct i2c_client *client = ts->client; |
| 53 | struct i2c_msg msg[2]; |
| 54 | int error; |
| 55 | u8 start_reg; |
| 56 | u8 buf[10]; |
| 57 | |
| 58 | /* read touchscreen data from ST1232 */ |
| 59 | msg[0].addr = client->addr; |
| 60 | msg[0].flags = 0; |
| 61 | msg[0].len = 1; |
| 62 | msg[0].buf = &start_reg; |
| 63 | start_reg = 0x10; |
| 64 | |
| 65 | msg[1].addr = ts->client->addr; |
| 66 | msg[1].flags = I2C_M_RD; |
| 67 | msg[1].len = sizeof(buf); |
| 68 | msg[1].buf = buf; |
| 69 | |
| 70 | error = i2c_transfer(client->adapter, msg, 2); |
| 71 | if (error < 0) |
| 72 | return error; |
| 73 | |
| 74 | /* get "valid" bits */ |
| 75 | finger[0].is_valid = buf[2] >> 7; |
| 76 | finger[1].is_valid = buf[5] >> 7; |
| 77 | |
| 78 | /* get xy coordinate */ |
| 79 | if (finger[0].is_valid) { |
| 80 | finger[0].x = ((buf[2] & 0x0070) << 4) | buf[3]; |
| 81 | finger[0].y = ((buf[2] & 0x0007) << 8) | buf[4]; |
| 82 | finger[0].t = buf[8]; |
| 83 | } |
| 84 | |
| 85 | if (finger[1].is_valid) { |
| 86 | finger[1].x = ((buf[5] & 0x0070) << 4) | buf[6]; |
| 87 | finger[1].y = ((buf[5] & 0x0007) << 8) | buf[7]; |
| 88 | finger[1].t = buf[9]; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id) |
| 95 | { |
| 96 | struct st1232_ts_data *ts = dev_id; |
| 97 | struct st1232_ts_finger *finger = ts->finger; |
| 98 | struct input_dev *input_dev = ts->input_dev; |
| 99 | int count = 0; |
| 100 | int i, ret; |
| 101 | |
| 102 | ret = st1232_ts_read_data(ts); |
| 103 | if (ret < 0) |
| 104 | goto end; |
| 105 | |
| 106 | /* multi touch protocol */ |
| 107 | for (i = 0; i < MAX_FINGERS; i++) { |
| 108 | if (!finger[i].is_valid) |
| 109 | continue; |
| 110 | |
| 111 | input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, finger[i].t); |
| 112 | input_report_abs(input_dev, ABS_MT_POSITION_X, finger[i].x); |
| 113 | input_report_abs(input_dev, ABS_MT_POSITION_Y, finger[i].y); |
| 114 | input_mt_sync(input_dev); |
| 115 | count++; |
| 116 | } |
| 117 | |
| 118 | /* SYN_MT_REPORT only if no contact */ |
Rafael J. Wysocki | 9ee27ff | 2011-12-23 01:24:25 +0100 | [diff] [blame] | 119 | if (!count) { |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 120 | input_mt_sync(input_dev); |
Rafael J. Wysocki | 9ee27ff | 2011-12-23 01:24:25 +0100 | [diff] [blame] | 121 | if (ts->low_latency_req.dev) { |
| 122 | dev_pm_qos_remove_request(&ts->low_latency_req); |
| 123 | ts->low_latency_req.dev = NULL; |
| 124 | } |
| 125 | } else if (!ts->low_latency_req.dev) { |
| 126 | /* First contact, request 100 us latency. */ |
| 127 | dev_pm_qos_add_ancestor_request(&ts->client->dev, |
Rafael J. Wysocki | 71d821f | 2014-02-11 00:36:00 +0100 | [diff] [blame] | 128 | &ts->low_latency_req, |
| 129 | DEV_PM_QOS_RESUME_LATENCY, 100); |
Rafael J. Wysocki | 9ee27ff | 2011-12-23 01:24:25 +0100 | [diff] [blame] | 130 | } |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 131 | |
| 132 | /* SYN_REPORT */ |
| 133 | input_sync(input_dev); |
| 134 | |
| 135 | end: |
| 136 | return IRQ_HANDLED; |
| 137 | } |
| 138 | |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 139 | static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron) |
| 140 | { |
| 141 | if (gpio_is_valid(ts->reset_gpio)) |
| 142 | gpio_direction_output(ts->reset_gpio, poweron); |
| 143 | } |
| 144 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 145 | static int st1232_ts_probe(struct i2c_client *client, |
Geert Uytterhoeven | 4a1a57d | 2017-10-31 09:47:28 -0700 | [diff] [blame] | 146 | const struct i2c_device_id *id) |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 147 | { |
| 148 | struct st1232_ts_data *ts; |
| 149 | struct input_dev *input_dev; |
| 150 | int error; |
| 151 | |
| 152 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 153 | dev_err(&client->dev, "need I2C_FUNC_I2C\n"); |
| 154 | return -EIO; |
| 155 | } |
| 156 | |
| 157 | if (!client->irq) { |
| 158 | dev_err(&client->dev, "no IRQ?\n"); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | |
Laurent Pinchart | 95b24d2 | 2013-04-15 09:23:00 -0700 | [diff] [blame] | 162 | ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); |
| 163 | if (!ts) |
| 164 | return -ENOMEM; |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 165 | |
Laurent Pinchart | 95b24d2 | 2013-04-15 09:23:00 -0700 | [diff] [blame] | 166 | input_dev = devm_input_allocate_device(&client->dev); |
| 167 | if (!input_dev) |
| 168 | return -ENOMEM; |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 169 | |
| 170 | ts->client = client; |
| 171 | ts->input_dev = input_dev; |
| 172 | |
Geert Uytterhoeven | 4a1a57d | 2017-10-31 09:47:28 -0700 | [diff] [blame] | 173 | ts->reset_gpio = of_get_gpio(client->dev.of_node, 0); |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 174 | if (gpio_is_valid(ts->reset_gpio)) { |
| 175 | error = devm_gpio_request(&client->dev, ts->reset_gpio, NULL); |
| 176 | if (error) { |
| 177 | dev_err(&client->dev, |
| 178 | "Unable to request GPIO pin %d.\n", |
| 179 | ts->reset_gpio); |
| 180 | return error; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | st1232_ts_power(ts, true); |
| 185 | |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 186 | input_dev->name = "st1232-touchscreen"; |
| 187 | input_dev->id.bustype = BUS_I2C; |
| 188 | input_dev->dev.parent = &client->dev; |
| 189 | |
Martin Kepplinger | 20bbb31 | 2018-10-05 11:44:45 -0700 | [diff] [blame] | 190 | __set_bit(INPUT_PROP_DIRECT, input_dev->propbit); |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 191 | __set_bit(EV_SYN, input_dev->evbit); |
| 192 | __set_bit(EV_KEY, input_dev->evbit); |
| 193 | __set_bit(EV_ABS, input_dev->evbit); |
| 194 | |
| 195 | input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, MAX_AREA, 0, 0); |
| 196 | input_set_abs_params(input_dev, ABS_MT_POSITION_X, MIN_X, MAX_X, 0, 0); |
| 197 | input_set_abs_params(input_dev, ABS_MT_POSITION_Y, MIN_Y, MAX_Y, 0, 0); |
| 198 | |
Laurent Pinchart | 95b24d2 | 2013-04-15 09:23:00 -0700 | [diff] [blame] | 199 | error = devm_request_threaded_irq(&client->dev, client->irq, |
| 200 | NULL, st1232_ts_irq_handler, |
| 201 | IRQF_ONESHOT, |
| 202 | client->name, ts); |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 203 | if (error) { |
| 204 | dev_err(&client->dev, "Failed to register interrupt\n"); |
Laurent Pinchart | 95b24d2 | 2013-04-15 09:23:00 -0700 | [diff] [blame] | 205 | return error; |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | error = input_register_device(ts->input_dev); |
| 209 | if (error) { |
| 210 | dev_err(&client->dev, "Unable to register %s input device\n", |
| 211 | input_dev->name); |
Laurent Pinchart | 95b24d2 | 2013-04-15 09:23:00 -0700 | [diff] [blame] | 212 | return error; |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | i2c_set_clientdata(client, ts); |
| 216 | device_init_wakeup(&client->dev, 1); |
| 217 | |
| 218 | return 0; |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 221 | static int st1232_ts_remove(struct i2c_client *client) |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 222 | { |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 223 | struct st1232_ts_data *ts = i2c_get_clientdata(client); |
| 224 | |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 225 | st1232_ts_power(ts, false); |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 230 | static int __maybe_unused st1232_ts_suspend(struct device *dev) |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 231 | { |
| 232 | struct i2c_client *client = to_i2c_client(dev); |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 233 | struct st1232_ts_data *ts = i2c_get_clientdata(client); |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 234 | |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 235 | if (device_may_wakeup(&client->dev)) { |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 236 | enable_irq_wake(client->irq); |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 237 | } else { |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 238 | disable_irq(client->irq); |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 239 | st1232_ts_power(ts, false); |
| 240 | } |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 245 | static int __maybe_unused st1232_ts_resume(struct device *dev) |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 246 | { |
| 247 | struct i2c_client *client = to_i2c_client(dev); |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 248 | struct st1232_ts_data *ts = i2c_get_clientdata(client); |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 249 | |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 250 | if (device_may_wakeup(&client->dev)) { |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 251 | disable_irq_wake(client->irq); |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 252 | } else { |
| 253 | st1232_ts_power(ts, true); |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 254 | enable_irq(client->irq); |
Bastian Hecht | e6a9081 | 2013-04-15 09:31:00 -0700 | [diff] [blame] | 255 | } |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Dmitry Torokhov | b357140 | 2012-04-03 11:30:28 -0700 | [diff] [blame] | 260 | static SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops, |
| 261 | st1232_ts_suspend, st1232_ts_resume); |
| 262 | |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 263 | static const struct i2c_device_id st1232_ts_id[] = { |
| 264 | { ST1232_TS_NAME, 0 }, |
| 265 | { } |
| 266 | }; |
| 267 | MODULE_DEVICE_TABLE(i2c, st1232_ts_id); |
| 268 | |
Bill Pemberton | 78f50c2 | 2012-11-23 21:31:00 -0800 | [diff] [blame] | 269 | static const struct of_device_id st1232_ts_dt_ids[] = { |
Magnus Damm | e6293d2 | 2012-04-03 11:30:28 -0700 | [diff] [blame] | 270 | { .compatible = "sitronix,st1232", }, |
| 271 | { } |
| 272 | }; |
| 273 | MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids); |
Magnus Damm | e6293d2 | 2012-04-03 11:30:28 -0700 | [diff] [blame] | 274 | |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 275 | static struct i2c_driver st1232_ts_driver = { |
| 276 | .probe = st1232_ts_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 277 | .remove = st1232_ts_remove, |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 278 | .id_table = st1232_ts_id, |
| 279 | .driver = { |
| 280 | .name = ST1232_TS_NAME, |
Geert Uytterhoeven | 4a1a57d | 2017-10-31 09:47:28 -0700 | [diff] [blame] | 281 | .of_match_table = st1232_ts_dt_ids, |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 282 | .pm = &st1232_ts_pm_ops, |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 283 | }, |
| 284 | }; |
| 285 | |
Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 286 | module_i2c_driver(st1232_ts_driver); |
Tony SIM | 56a8bd6 | 2010-12-15 23:39:25 -0800 | [diff] [blame] | 287 | |
| 288 | MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>"); |
| 289 | MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver"); |
Kuninori Morimoto | 4aeba64 | 2018-11-12 11:30:06 -0800 | [diff] [blame] | 290 | MODULE_LICENSE("GPL v2"); |