Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mcs5000_ts.c - Touchscreen driver for MELFAS MCS-5000 controller |
| 3 | * |
| 4 | * Copyright (C) 2009 Samsung Electronics Co.Ltd |
| 5 | * Author: Joonyoung Shim <jy0922.shim@samsung.com> |
| 6 | * |
| 7 | * Based on wm97xx-core.c |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 17 | #include <linux/i2c.h> |
Joonyoung Shim | 312e8e8 | 2010-07-04 01:21:25 -0700 | [diff] [blame] | 18 | #include <linux/i2c/mcs.h> |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/input.h> |
| 21 | #include <linux/irq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 23 | |
| 24 | /* Registers */ |
| 25 | #define MCS5000_TS_STATUS 0x00 |
| 26 | #define STATUS_OFFSET 0 |
| 27 | #define STATUS_NO (0 << STATUS_OFFSET) |
| 28 | #define STATUS_INIT (1 << STATUS_OFFSET) |
| 29 | #define STATUS_SENSING (2 << STATUS_OFFSET) |
| 30 | #define STATUS_COORD (3 << STATUS_OFFSET) |
| 31 | #define STATUS_GESTURE (4 << STATUS_OFFSET) |
| 32 | #define ERROR_OFFSET 4 |
| 33 | #define ERROR_NO (0 << ERROR_OFFSET) |
| 34 | #define ERROR_POWER_ON_RESET (1 << ERROR_OFFSET) |
| 35 | #define ERROR_INT_RESET (2 << ERROR_OFFSET) |
| 36 | #define ERROR_EXT_RESET (3 << ERROR_OFFSET) |
| 37 | #define ERROR_INVALID_REG_ADDRESS (8 << ERROR_OFFSET) |
| 38 | #define ERROR_INVALID_REG_VALUE (9 << ERROR_OFFSET) |
| 39 | |
| 40 | #define MCS5000_TS_OP_MODE 0x01 |
| 41 | #define RESET_OFFSET 0 |
| 42 | #define RESET_NO (0 << RESET_OFFSET) |
| 43 | #define RESET_EXT_SOFT (1 << RESET_OFFSET) |
| 44 | #define OP_MODE_OFFSET 1 |
| 45 | #define OP_MODE_SLEEP (0 << OP_MODE_OFFSET) |
| 46 | #define OP_MODE_ACTIVE (1 << OP_MODE_OFFSET) |
| 47 | #define GESTURE_OFFSET 4 |
| 48 | #define GESTURE_DISABLE (0 << GESTURE_OFFSET) |
| 49 | #define GESTURE_ENABLE (1 << GESTURE_OFFSET) |
| 50 | #define PROXIMITY_OFFSET 5 |
| 51 | #define PROXIMITY_DISABLE (0 << PROXIMITY_OFFSET) |
| 52 | #define PROXIMITY_ENABLE (1 << PROXIMITY_OFFSET) |
| 53 | #define SCAN_MODE_OFFSET 6 |
| 54 | #define SCAN_MODE_INTERRUPT (0 << SCAN_MODE_OFFSET) |
| 55 | #define SCAN_MODE_POLLING (1 << SCAN_MODE_OFFSET) |
| 56 | #define REPORT_RATE_OFFSET 7 |
| 57 | #define REPORT_RATE_40 (0 << REPORT_RATE_OFFSET) |
| 58 | #define REPORT_RATE_80 (1 << REPORT_RATE_OFFSET) |
| 59 | |
| 60 | #define MCS5000_TS_SENS_CTL 0x02 |
| 61 | #define MCS5000_TS_FILTER_CTL 0x03 |
| 62 | #define PRI_FILTER_OFFSET 0 |
| 63 | #define SEC_FILTER_OFFSET 4 |
| 64 | |
| 65 | #define MCS5000_TS_X_SIZE_UPPER 0x08 |
| 66 | #define MCS5000_TS_X_SIZE_LOWER 0x09 |
| 67 | #define MCS5000_TS_Y_SIZE_UPPER 0x0A |
| 68 | #define MCS5000_TS_Y_SIZE_LOWER 0x0B |
| 69 | |
| 70 | #define MCS5000_TS_INPUT_INFO 0x10 |
| 71 | #define INPUT_TYPE_OFFSET 0 |
| 72 | #define INPUT_TYPE_NONTOUCH (0 << INPUT_TYPE_OFFSET) |
| 73 | #define INPUT_TYPE_SINGLE (1 << INPUT_TYPE_OFFSET) |
| 74 | #define INPUT_TYPE_DUAL (2 << INPUT_TYPE_OFFSET) |
| 75 | #define INPUT_TYPE_PALM (3 << INPUT_TYPE_OFFSET) |
| 76 | #define INPUT_TYPE_PROXIMITY (7 << INPUT_TYPE_OFFSET) |
| 77 | #define GESTURE_CODE_OFFSET 3 |
| 78 | #define GESTURE_CODE_NO (0 << GESTURE_CODE_OFFSET) |
| 79 | |
| 80 | #define MCS5000_TS_X_POS_UPPER 0x11 |
| 81 | #define MCS5000_TS_X_POS_LOWER 0x12 |
| 82 | #define MCS5000_TS_Y_POS_UPPER 0x13 |
| 83 | #define MCS5000_TS_Y_POS_LOWER 0x14 |
| 84 | #define MCS5000_TS_Z_POS 0x15 |
| 85 | #define MCS5000_TS_WIDTH 0x16 |
| 86 | #define MCS5000_TS_GESTURE_VAL 0x17 |
| 87 | #define MCS5000_TS_MODULE_REV 0x20 |
| 88 | #define MCS5000_TS_FIRMWARE_VER 0x21 |
| 89 | |
| 90 | /* Touchscreen absolute values */ |
| 91 | #define MCS5000_MAX_XC 0x3ff |
| 92 | #define MCS5000_MAX_YC 0x3ff |
| 93 | |
| 94 | enum mcs5000_ts_read_offset { |
| 95 | READ_INPUT_INFO, |
| 96 | READ_X_POS_UPPER, |
| 97 | READ_X_POS_LOWER, |
| 98 | READ_Y_POS_UPPER, |
| 99 | READ_Y_POS_LOWER, |
| 100 | READ_BLOCK_SIZE, |
| 101 | }; |
| 102 | |
| 103 | /* Each client has this additional data */ |
| 104 | struct mcs5000_ts_data { |
| 105 | struct i2c_client *client; |
| 106 | struct input_dev *input_dev; |
Joonyoung Shim | 312e8e8 | 2010-07-04 01:21:25 -0700 | [diff] [blame] | 107 | const struct mcs_platform_data *platform_data; |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | static irqreturn_t mcs5000_ts_interrupt(int irq, void *dev_id) |
| 111 | { |
| 112 | struct mcs5000_ts_data *data = dev_id; |
| 113 | struct i2c_client *client = data->client; |
| 114 | u8 buffer[READ_BLOCK_SIZE]; |
| 115 | int err; |
| 116 | int x; |
| 117 | int y; |
| 118 | |
| 119 | err = i2c_smbus_read_i2c_block_data(client, MCS5000_TS_INPUT_INFO, |
| 120 | READ_BLOCK_SIZE, buffer); |
| 121 | if (err < 0) { |
| 122 | dev_err(&client->dev, "%s, err[%d]\n", __func__, err); |
| 123 | goto out; |
| 124 | } |
| 125 | |
| 126 | switch (buffer[READ_INPUT_INFO]) { |
| 127 | case INPUT_TYPE_NONTOUCH: |
| 128 | input_report_key(data->input_dev, BTN_TOUCH, 0); |
| 129 | input_sync(data->input_dev); |
| 130 | break; |
| 131 | |
| 132 | case INPUT_TYPE_SINGLE: |
| 133 | x = (buffer[READ_X_POS_UPPER] << 8) | buffer[READ_X_POS_LOWER]; |
| 134 | y = (buffer[READ_Y_POS_UPPER] << 8) | buffer[READ_Y_POS_LOWER]; |
| 135 | |
| 136 | input_report_key(data->input_dev, BTN_TOUCH, 1); |
| 137 | input_report_abs(data->input_dev, ABS_X, x); |
| 138 | input_report_abs(data->input_dev, ABS_Y, y); |
| 139 | input_sync(data->input_dev); |
| 140 | break; |
| 141 | |
| 142 | case INPUT_TYPE_DUAL: |
| 143 | /* TODO */ |
| 144 | break; |
| 145 | |
| 146 | case INPUT_TYPE_PALM: |
| 147 | /* TODO */ |
| 148 | break; |
| 149 | |
| 150 | case INPUT_TYPE_PROXIMITY: |
| 151 | /* TODO */ |
| 152 | break; |
| 153 | |
| 154 | default: |
| 155 | dev_err(&client->dev, "Unknown ts input type %d\n", |
| 156 | buffer[READ_INPUT_INFO]); |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | out: |
| 161 | return IRQ_HANDLED; |
| 162 | } |
| 163 | |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 164 | static void mcs5000_ts_phys_init(struct mcs5000_ts_data *data, |
| 165 | const struct mcs_platform_data *platform_data) |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 166 | { |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 167 | struct i2c_client *client = data->client; |
| 168 | |
| 169 | /* Touch reset & sleep mode */ |
| 170 | i2c_smbus_write_byte_data(client, MCS5000_TS_OP_MODE, |
| 171 | RESET_EXT_SOFT | OP_MODE_SLEEP); |
| 172 | |
| 173 | /* Touch size */ |
| 174 | i2c_smbus_write_byte_data(client, MCS5000_TS_X_SIZE_UPPER, |
| 175 | platform_data->x_size >> 8); |
| 176 | i2c_smbus_write_byte_data(client, MCS5000_TS_X_SIZE_LOWER, |
| 177 | platform_data->x_size & 0xff); |
| 178 | i2c_smbus_write_byte_data(client, MCS5000_TS_Y_SIZE_UPPER, |
| 179 | platform_data->y_size >> 8); |
| 180 | i2c_smbus_write_byte_data(client, MCS5000_TS_Y_SIZE_LOWER, |
| 181 | platform_data->y_size & 0xff); |
| 182 | |
| 183 | /* Touch active mode & 80 report rate */ |
| 184 | i2c_smbus_write_byte_data(data->client, MCS5000_TS_OP_MODE, |
| 185 | OP_MODE_ACTIVE | REPORT_RATE_80); |
| 186 | } |
| 187 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 188 | static int mcs5000_ts_probe(struct i2c_client *client, |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 189 | const struct i2c_device_id *id) |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 190 | { |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 191 | const struct mcs_platform_data *pdata; |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 192 | struct mcs5000_ts_data *data; |
| 193 | struct input_dev *input_dev; |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 194 | int error; |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 195 | |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 196 | pdata = dev_get_platdata(&client->dev); |
| 197 | if (!pdata) |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 198 | return -EINVAL; |
| 199 | |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 200 | data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); |
| 201 | if (!data) { |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 202 | dev_err(&client->dev, "Failed to allocate memory\n"); |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 203 | return -ENOMEM; |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | data->client = client; |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 207 | |
| 208 | input_dev = devm_input_allocate_device(&client->dev); |
| 209 | if (!input_dev) { |
| 210 | dev_err(&client->dev, "Failed to allocate input device\n"); |
| 211 | return -ENOMEM; |
| 212 | } |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 213 | |
Beomho Seo | 21d128a | 2014-05-27 10:22:12 -0700 | [diff] [blame] | 214 | input_dev->name = "MELFAS MCS-5000 Touchscreen"; |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 215 | input_dev->id.bustype = BUS_I2C; |
| 216 | input_dev->dev.parent = &client->dev; |
| 217 | |
| 218 | __set_bit(EV_ABS, input_dev->evbit); |
| 219 | __set_bit(EV_KEY, input_dev->evbit); |
| 220 | __set_bit(BTN_TOUCH, input_dev->keybit); |
| 221 | input_set_abs_params(input_dev, ABS_X, 0, MCS5000_MAX_XC, 0, 0); |
| 222 | input_set_abs_params(input_dev, ABS_Y, 0, MCS5000_MAX_YC, 0, 0); |
| 223 | |
| 224 | input_set_drvdata(input_dev, data); |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 225 | data->input_dev = input_dev; |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 226 | |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 227 | if (pdata->cfg_pin) |
| 228 | pdata->cfg_pin(); |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 229 | |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 230 | error = devm_request_threaded_irq(&client->dev, client->irq, |
| 231 | NULL, mcs5000_ts_interrupt, |
| 232 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 233 | "mcs5000_ts", data); |
| 234 | if (error) { |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 235 | dev_err(&client->dev, "Failed to register interrupt\n"); |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 236 | return error; |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 239 | error = input_register_device(data->input_dev); |
| 240 | if (error) { |
| 241 | dev_err(&client->dev, "Failed to register input device\n"); |
| 242 | return error; |
| 243 | } |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 244 | |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 245 | mcs5000_ts_phys_init(data, pdata); |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 246 | i2c_set_clientdata(client, data); |
| 247 | |
| 248 | return 0; |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Fabio Estevam | 9634c15 | 2014-07-30 11:34:49 -0700 | [diff] [blame] | 251 | static int __maybe_unused mcs5000_ts_suspend(struct device *dev) |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 252 | { |
Mark Brown | 92b672e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 253 | struct i2c_client *client = to_i2c_client(dev); |
| 254 | |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 255 | /* Touch sleep mode */ |
| 256 | i2c_smbus_write_byte_data(client, MCS5000_TS_OP_MODE, OP_MODE_SLEEP); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Fabio Estevam | 9634c15 | 2014-07-30 11:34:49 -0700 | [diff] [blame] | 261 | static int __maybe_unused mcs5000_ts_resume(struct device *dev) |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 262 | { |
Mark Brown | 92b672e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 263 | struct i2c_client *client = to_i2c_client(dev); |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 264 | struct mcs5000_ts_data *data = i2c_get_clientdata(client); |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 265 | const struct mcs_platform_data *pdata = dev_get_platdata(dev); |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 266 | |
Beomho Seo | f5189d0 | 2014-05-26 13:36:13 -0700 | [diff] [blame] | 267 | mcs5000_ts_phys_init(data, pdata); |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |
Mark Brown | 92b672e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 271 | |
| 272 | static SIMPLE_DEV_PM_OPS(mcs5000_ts_pm, mcs5000_ts_suspend, mcs5000_ts_resume); |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 273 | |
| 274 | static const struct i2c_device_id mcs5000_ts_id[] = { |
| 275 | { "mcs5000_ts", 0 }, |
| 276 | { } |
| 277 | }; |
| 278 | MODULE_DEVICE_TABLE(i2c, mcs5000_ts_id); |
| 279 | |
| 280 | static struct i2c_driver mcs5000_ts_driver = { |
| 281 | .probe = mcs5000_ts_probe, |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 282 | .driver = { |
| 283 | .name = "mcs5000_ts", |
Mark Brown | 92b672e | 2011-01-06 23:01:03 -0800 | [diff] [blame] | 284 | .pm = &mcs5000_ts_pm, |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 285 | }, |
| 286 | .id_table = mcs5000_ts_id, |
| 287 | }; |
| 288 | |
Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 289 | module_i2c_driver(mcs5000_ts_driver); |
Joonyoung Shim | 38e783b | 2009-09-17 22:35:45 -0700 | [diff] [blame] | 290 | |
| 291 | /* Module information */ |
| 292 | MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); |
| 293 | MODULE_DESCRIPTION("Touchscreen driver for MELFAS MCS-5000 controller"); |
| 294 | MODULE_LICENSE("GPL"); |