0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/module.h>
0014 #include <linux/i2c.h>
0015 #include <linux/irq.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/input.h>
0018 #include <linux/delay.h>
0019 #include <linux/workqueue.h>
0020 #include <linux/slab.h>
0021 #include <linux/pm.h>
0022
0023 #define DRIVER_NAME "synaptics_i2c"
0024
0025 #define PRODUCT_ID_LENGTH 15
0026 #define REGISTER_LENGTH 8
0027
0028
0029
0030
0031
0032 #define SOFT_RESET_DELAY_US 3000
0033
0034 #define HARD_RESET_DELAY_MS 500
0035
0036
0037 #define PAGE_SEL_REG 0xff
0038 #define DEVICE_STATUS_REG 0x09
0039
0040
0041 #define DEV_CONTROL_REG 0x0000
0042 #define INTERRUPT_EN_REG 0x0001
0043 #define ERR_STAT_REG 0x0002
0044 #define INT_REQ_STAT_REG 0x0003
0045 #define DEV_COMMAND_REG 0x0004
0046
0047 #define RMI_PROT_VER_REG 0x0200
0048 #define MANUFACT_ID_REG 0x0201
0049 #define PHYS_INT_VER_REG 0x0202
0050 #define PROD_PROPERTY_REG 0x0203
0051 #define INFO_QUERY_REG0 0x0204
0052 #define INFO_QUERY_REG1 (INFO_QUERY_REG0 + 1)
0053 #define INFO_QUERY_REG2 (INFO_QUERY_REG0 + 2)
0054 #define INFO_QUERY_REG3 (INFO_QUERY_REG0 + 3)
0055
0056 #define PRODUCT_ID_REG0 0x0210
0057 #define PRODUCT_ID_REG1 (PRODUCT_ID_REG0 + 1)
0058 #define PRODUCT_ID_REG2 (PRODUCT_ID_REG0 + 2)
0059 #define PRODUCT_ID_REG3 (PRODUCT_ID_REG0 + 3)
0060 #define PRODUCT_ID_REG4 (PRODUCT_ID_REG0 + 4)
0061 #define PRODUCT_ID_REG5 (PRODUCT_ID_REG0 + 5)
0062 #define PRODUCT_ID_REG6 (PRODUCT_ID_REG0 + 6)
0063 #define PRODUCT_ID_REG7 (PRODUCT_ID_REG0 + 7)
0064 #define PRODUCT_ID_REG8 (PRODUCT_ID_REG0 + 8)
0065 #define PRODUCT_ID_REG9 (PRODUCT_ID_REG0 + 9)
0066 #define PRODUCT_ID_REG10 (PRODUCT_ID_REG0 + 10)
0067 #define PRODUCT_ID_REG11 (PRODUCT_ID_REG0 + 11)
0068 #define PRODUCT_ID_REG12 (PRODUCT_ID_REG0 + 12)
0069 #define PRODUCT_ID_REG13 (PRODUCT_ID_REG0 + 13)
0070 #define PRODUCT_ID_REG14 (PRODUCT_ID_REG0 + 14)
0071 #define PRODUCT_ID_REG15 (PRODUCT_ID_REG0 + 15)
0072
0073 #define DATA_REG0 0x0400
0074 #define ABS_PRESSURE_REG 0x0401
0075 #define ABS_MSB_X_REG 0x0402
0076 #define ABS_LSB_X_REG (ABS_MSB_X_REG + 1)
0077 #define ABS_MSB_Y_REG 0x0404
0078 #define ABS_LSB_Y_REG (ABS_MSB_Y_REG + 1)
0079 #define REL_X_REG 0x0406
0080 #define REL_Y_REG 0x0407
0081
0082 #define DEV_QUERY_REG0 0x1000
0083 #define DEV_QUERY_REG1 (DEV_QUERY_REG0 + 1)
0084 #define DEV_QUERY_REG2 (DEV_QUERY_REG0 + 2)
0085 #define DEV_QUERY_REG3 (DEV_QUERY_REG0 + 3)
0086 #define DEV_QUERY_REG4 (DEV_QUERY_REG0 + 4)
0087 #define DEV_QUERY_REG5 (DEV_QUERY_REG0 + 5)
0088 #define DEV_QUERY_REG6 (DEV_QUERY_REG0 + 6)
0089 #define DEV_QUERY_REG7 (DEV_QUERY_REG0 + 7)
0090 #define DEV_QUERY_REG8 (DEV_QUERY_REG0 + 8)
0091
0092 #define GENERAL_2D_CONTROL_REG 0x1041
0093 #define SENSOR_SENSITIVITY_REG 0x1044
0094 #define SENS_MAX_POS_MSB_REG 0x1046
0095 #define SENS_MAX_POS_LSB_REG (SENS_MAX_POS_UPPER_REG + 1)
0096
0097
0098
0099 #define REPORT_RATE_1ST_BIT 6
0100
0101
0102 #define F10_ABS_INT_ENA 0
0103 #define F10_REL_INT_ENA 1
0104 #define F20_INT_ENA 2
0105
0106
0107 #define F10_ABS_INT_REQ 0
0108 #define F10_REL_INT_REQ 1
0109 #define F20_INT_REQ 2
0110
0111 #define STAT_CONFIGURED 6
0112 #define STAT_ERROR 7
0113
0114
0115 #define RESET_COMMAND 0x01
0116 #define REZERO_COMMAND 0x02
0117
0118
0119 #define GESTURE 3
0120
0121
0122
0123 #define HAS_PALM_DETECT 1
0124 #define HAS_MULTI_FING 2
0125 #define HAS_SCROLLER 4
0126 #define HAS_2D_SCROLL 5
0127
0128
0129 #define NO_DECELERATION 1
0130 #define REDUCE_REPORTING 3
0131 #define NO_FILTER 5
0132
0133
0134
0135 #define REPORT_RATE_MSK 0xc0
0136 #define SLEEP_MODE_MSK 0x07
0137
0138
0139 #define FULL_AWAKE 0x0
0140 #define NORMAL_OP 0x1
0141 #define LOW_PWR_OP 0x2
0142 #define VERY_LOW_PWR_OP 0x3
0143 #define SENS_SLEEP 0x4
0144 #define SLEEP_MOD 0x5
0145 #define DEEP_SLEEP 0x6
0146 #define HIBERNATE 0x7
0147
0148
0149
0150 #define INT_ENA_REQ_MSK 0x07
0151 #define INT_ENA_ABS_MSK 0x01
0152 #define INT_ENA_REL_MSK 0x02
0153 #define INT_ENA_F20_MSK 0x04
0154
0155
0156 #define CONFIGURED_MSK 0x40
0157 #define ERROR_MSK 0x80
0158
0159
0160 #define FINGER_WIDTH_MSK 0xf0
0161 #define GESTURE_MSK 0x08
0162 #define SENSOR_STATUS_MSK 0x07
0163
0164
0165
0166
0167
0168
0169 #define MSB_POSITION_MSK 0x1f
0170
0171
0172
0173
0174 #define NUM_EXTRA_POS_MSK 0x07
0175
0176
0177 #define THREAD_IRQ_SLEEP_SECS 2
0178 #define THREAD_IRQ_SLEEP_MSECS (THREAD_IRQ_SLEEP_SECS * MSEC_PER_SEC)
0179
0180
0181
0182
0183
0184 #define NO_DATA_THRES (MSEC_PER_SEC)
0185 #define NO_DATA_SLEEP_MSECS (MSEC_PER_SEC / 4)
0186
0187
0188 static bool no_decel = true;
0189 module_param(no_decel, bool, 0644);
0190 MODULE_PARM_DESC(no_decel, "No Deceleration. Default = 1 (on)");
0191
0192
0193 static bool reduce_report;
0194 module_param(reduce_report, bool, 0644);
0195 MODULE_PARM_DESC(reduce_report, "Reduced Reporting. Default = 0 (off)");
0196
0197
0198 static bool no_filter;
0199 module_param(no_filter, bool, 0644);
0200 MODULE_PARM_DESC(no_filter, "No Filter. Default = 0 (off)");
0201
0202
0203
0204
0205
0206
0207
0208 static bool polling_req;
0209 module_param(polling_req, bool, 0444);
0210 MODULE_PARM_DESC(polling_req, "Request Polling. Default = 0 (use irq)");
0211
0212
0213 static int scan_rate = 80;
0214 module_param(scan_rate, int, 0644);
0215 MODULE_PARM_DESC(scan_rate, "Polling rate in times/sec. Default = 80");
0216
0217
0218 struct synaptics_i2c {
0219 struct i2c_client *client;
0220 struct input_dev *input;
0221 struct delayed_work dwork;
0222 int no_data_count;
0223 int no_decel_param;
0224 int reduce_report_param;
0225 int no_filter_param;
0226 int scan_rate_param;
0227 int scan_ms;
0228 };
0229
0230 static inline void set_scan_rate(struct synaptics_i2c *touch, int scan_rate)
0231 {
0232 touch->scan_ms = MSEC_PER_SEC / scan_rate;
0233 touch->scan_rate_param = scan_rate;
0234 }
0235
0236
0237
0238
0239
0240
0241 static s32 synaptics_i2c_reg_get(struct i2c_client *client, u16 reg)
0242 {
0243 int ret;
0244
0245 ret = i2c_smbus_write_byte_data(client, PAGE_SEL_REG, reg >> 8);
0246 if (ret == 0)
0247 ret = i2c_smbus_read_byte_data(client, reg & 0xff);
0248
0249 return ret;
0250 }
0251
0252 static s32 synaptics_i2c_reg_set(struct i2c_client *client, u16 reg, u8 val)
0253 {
0254 int ret;
0255
0256 ret = i2c_smbus_write_byte_data(client, PAGE_SEL_REG, reg >> 8);
0257 if (ret == 0)
0258 ret = i2c_smbus_write_byte_data(client, reg & 0xff, val);
0259
0260 return ret;
0261 }
0262
0263 static s32 synaptics_i2c_word_get(struct i2c_client *client, u16 reg)
0264 {
0265 int ret;
0266
0267 ret = i2c_smbus_write_byte_data(client, PAGE_SEL_REG, reg >> 8);
0268 if (ret == 0)
0269 ret = i2c_smbus_read_word_data(client, reg & 0xff);
0270
0271 return ret;
0272 }
0273
0274 static int synaptics_i2c_config(struct i2c_client *client)
0275 {
0276 int ret, control;
0277 u8 int_en;
0278
0279
0280 ret = synaptics_i2c_reg_set(client, DEV_CONTROL_REG, 0xc1);
0281 if (ret)
0282 return ret;
0283
0284
0285 int_en = (polling_req) ? 0 : INT_ENA_ABS_MSK | INT_ENA_REL_MSK;
0286 ret = synaptics_i2c_reg_set(client, INTERRUPT_EN_REG, int_en);
0287 if (ret)
0288 return ret;
0289
0290 control = synaptics_i2c_reg_get(client, GENERAL_2D_CONTROL_REG);
0291
0292 control |= no_decel ? 1 << NO_DECELERATION : 0;
0293
0294 control |= reduce_report ? 1 << REDUCE_REPORTING : 0;
0295
0296 control |= no_filter ? 1 << NO_FILTER : 0;
0297 ret = synaptics_i2c_reg_set(client, GENERAL_2D_CONTROL_REG, control);
0298 if (ret)
0299 return ret;
0300
0301 return 0;
0302 }
0303
0304 static int synaptics_i2c_reset_config(struct i2c_client *client)
0305 {
0306 int ret;
0307
0308
0309 ret = synaptics_i2c_reg_set(client, DEV_COMMAND_REG, RESET_COMMAND);
0310 if (ret) {
0311 dev_err(&client->dev, "Unable to reset device\n");
0312 } else {
0313 usleep_range(SOFT_RESET_DELAY_US, SOFT_RESET_DELAY_US + 100);
0314 ret = synaptics_i2c_config(client);
0315 if (ret)
0316 dev_err(&client->dev, "Unable to config device\n");
0317 }
0318
0319 return ret;
0320 }
0321
0322 static int synaptics_i2c_check_error(struct i2c_client *client)
0323 {
0324 int status, ret = 0;
0325
0326 status = i2c_smbus_read_byte_data(client, DEVICE_STATUS_REG) &
0327 (CONFIGURED_MSK | ERROR_MSK);
0328
0329 if (status != CONFIGURED_MSK)
0330 ret = synaptics_i2c_reset_config(client);
0331
0332 return ret;
0333 }
0334
0335 static bool synaptics_i2c_get_input(struct synaptics_i2c *touch)
0336 {
0337 struct input_dev *input = touch->input;
0338 int xy_delta, gesture;
0339 s32 data;
0340 s8 x_delta, y_delta;
0341
0342
0343 if (synaptics_i2c_check_error(touch->client))
0344 return false;
0345
0346
0347 data = synaptics_i2c_reg_get(touch->client, DATA_REG0);
0348 gesture = (data >> GESTURE) & 0x1;
0349
0350
0351
0352
0353
0354 xy_delta = synaptics_i2c_word_get(touch->client, REL_X_REG) & 0xffff;
0355
0356
0357 x_delta = xy_delta & 0xff;
0358 y_delta = (xy_delta >> REGISTER_LENGTH) & 0xff;
0359
0360
0361 input_report_key(input, BTN_LEFT, gesture);
0362
0363
0364 input_report_rel(input, REL_X, x_delta);
0365 input_report_rel(input, REL_Y, -y_delta);
0366 input_sync(input);
0367
0368 return xy_delta || gesture;
0369 }
0370
0371 static irqreturn_t synaptics_i2c_irq(int irq, void *dev_id)
0372 {
0373 struct synaptics_i2c *touch = dev_id;
0374
0375 mod_delayed_work(system_wq, &touch->dwork, 0);
0376
0377 return IRQ_HANDLED;
0378 }
0379
0380 static void synaptics_i2c_check_params(struct synaptics_i2c *touch)
0381 {
0382 bool reset = false;
0383
0384 if (scan_rate != touch->scan_rate_param)
0385 set_scan_rate(touch, scan_rate);
0386
0387 if (no_decel != touch->no_decel_param) {
0388 touch->no_decel_param = no_decel;
0389 reset = true;
0390 }
0391
0392 if (no_filter != touch->no_filter_param) {
0393 touch->no_filter_param = no_filter;
0394 reset = true;
0395 }
0396
0397 if (reduce_report != touch->reduce_report_param) {
0398 touch->reduce_report_param = reduce_report;
0399 reset = true;
0400 }
0401
0402 if (reset)
0403 synaptics_i2c_reset_config(touch->client);
0404 }
0405
0406
0407 static unsigned long synaptics_i2c_adjust_delay(struct synaptics_i2c *touch,
0408 bool have_data)
0409 {
0410 unsigned long delay, nodata_count_thres;
0411
0412 if (polling_req) {
0413 delay = touch->scan_ms;
0414 if (have_data) {
0415 touch->no_data_count = 0;
0416 } else {
0417 nodata_count_thres = NO_DATA_THRES / touch->scan_ms;
0418 if (touch->no_data_count < nodata_count_thres)
0419 touch->no_data_count++;
0420 else
0421 delay = NO_DATA_SLEEP_MSECS;
0422 }
0423 return msecs_to_jiffies(delay);
0424 } else {
0425 delay = msecs_to_jiffies(THREAD_IRQ_SLEEP_MSECS);
0426 return round_jiffies_relative(delay);
0427 }
0428 }
0429
0430
0431 static void synaptics_i2c_work_handler(struct work_struct *work)
0432 {
0433 bool have_data;
0434 struct synaptics_i2c *touch =
0435 container_of(work, struct synaptics_i2c, dwork.work);
0436 unsigned long delay;
0437
0438 synaptics_i2c_check_params(touch);
0439
0440 have_data = synaptics_i2c_get_input(touch);
0441 delay = synaptics_i2c_adjust_delay(touch, have_data);
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451 mod_delayed_work(system_wq, &touch->dwork, delay);
0452 }
0453
0454 static int synaptics_i2c_open(struct input_dev *input)
0455 {
0456 struct synaptics_i2c *touch = input_get_drvdata(input);
0457 int ret;
0458
0459 ret = synaptics_i2c_reset_config(touch->client);
0460 if (ret)
0461 return ret;
0462
0463 if (polling_req)
0464 mod_delayed_work(system_wq, &touch->dwork,
0465 msecs_to_jiffies(NO_DATA_SLEEP_MSECS));
0466
0467 return 0;
0468 }
0469
0470 static void synaptics_i2c_close(struct input_dev *input)
0471 {
0472 struct synaptics_i2c *touch = input_get_drvdata(input);
0473
0474 if (!polling_req)
0475 synaptics_i2c_reg_set(touch->client, INTERRUPT_EN_REG, 0);
0476
0477 cancel_delayed_work_sync(&touch->dwork);
0478
0479
0480 synaptics_i2c_reg_set(touch->client, DEV_CONTROL_REG, DEEP_SLEEP);
0481 }
0482
0483 static void synaptics_i2c_set_input_params(struct synaptics_i2c *touch)
0484 {
0485 struct input_dev *input = touch->input;
0486
0487 input->name = touch->client->name;
0488 input->phys = touch->client->adapter->name;
0489 input->id.bustype = BUS_I2C;
0490 input->id.version = synaptics_i2c_word_get(touch->client,
0491 INFO_QUERY_REG0);
0492 input->dev.parent = &touch->client->dev;
0493 input->open = synaptics_i2c_open;
0494 input->close = synaptics_i2c_close;
0495 input_set_drvdata(input, touch);
0496
0497
0498 __set_bit(EV_REL, input->evbit);
0499 __set_bit(REL_X, input->relbit);
0500 __set_bit(REL_Y, input->relbit);
0501
0502
0503 __set_bit(EV_KEY, input->evbit);
0504 __set_bit(BTN_LEFT, input->keybit);
0505 }
0506
0507 static struct synaptics_i2c *synaptics_i2c_touch_create(struct i2c_client *client)
0508 {
0509 struct synaptics_i2c *touch;
0510
0511 touch = kzalloc(sizeof(struct synaptics_i2c), GFP_KERNEL);
0512 if (!touch)
0513 return NULL;
0514
0515 touch->client = client;
0516 touch->no_decel_param = no_decel;
0517 touch->scan_rate_param = scan_rate;
0518 set_scan_rate(touch, scan_rate);
0519 INIT_DELAYED_WORK(&touch->dwork, synaptics_i2c_work_handler);
0520
0521 return touch;
0522 }
0523
0524 static int synaptics_i2c_probe(struct i2c_client *client,
0525 const struct i2c_device_id *dev_id)
0526 {
0527 int ret;
0528 struct synaptics_i2c *touch;
0529
0530 touch = synaptics_i2c_touch_create(client);
0531 if (!touch)
0532 return -ENOMEM;
0533
0534 ret = synaptics_i2c_reset_config(client);
0535 if (ret)
0536 goto err_mem_free;
0537
0538 if (client->irq < 1)
0539 polling_req = true;
0540
0541 touch->input = input_allocate_device();
0542 if (!touch->input) {
0543 ret = -ENOMEM;
0544 goto err_mem_free;
0545 }
0546
0547 synaptics_i2c_set_input_params(touch);
0548
0549 if (!polling_req) {
0550 dev_dbg(&touch->client->dev,
0551 "Requesting IRQ: %d\n", touch->client->irq);
0552
0553 ret = request_irq(touch->client->irq, synaptics_i2c_irq,
0554 IRQ_TYPE_EDGE_FALLING,
0555 DRIVER_NAME, touch);
0556 if (ret) {
0557 dev_warn(&touch->client->dev,
0558 "IRQ request failed: %d, "
0559 "falling back to polling\n", ret);
0560 polling_req = true;
0561 synaptics_i2c_reg_set(touch->client,
0562 INTERRUPT_EN_REG, 0);
0563 }
0564 }
0565
0566 if (polling_req)
0567 dev_dbg(&touch->client->dev,
0568 "Using polling at rate: %d times/sec\n", scan_rate);
0569
0570
0571 ret = input_register_device(touch->input);
0572 if (ret) {
0573 dev_err(&client->dev,
0574 "Input device register failed: %d\n", ret);
0575 goto err_input_free;
0576 }
0577
0578 i2c_set_clientdata(client, touch);
0579
0580 return 0;
0581
0582 err_input_free:
0583 input_free_device(touch->input);
0584 err_mem_free:
0585 kfree(touch);
0586
0587 return ret;
0588 }
0589
0590 static int synaptics_i2c_remove(struct i2c_client *client)
0591 {
0592 struct synaptics_i2c *touch = i2c_get_clientdata(client);
0593
0594 if (!polling_req)
0595 free_irq(client->irq, touch);
0596
0597 input_unregister_device(touch->input);
0598 kfree(touch);
0599
0600 return 0;
0601 }
0602
0603 static int __maybe_unused synaptics_i2c_suspend(struct device *dev)
0604 {
0605 struct i2c_client *client = to_i2c_client(dev);
0606 struct synaptics_i2c *touch = i2c_get_clientdata(client);
0607
0608 cancel_delayed_work_sync(&touch->dwork);
0609
0610
0611 synaptics_i2c_reg_set(touch->client, DEV_CONTROL_REG, DEEP_SLEEP);
0612
0613 return 0;
0614 }
0615
0616 static int __maybe_unused synaptics_i2c_resume(struct device *dev)
0617 {
0618 int ret;
0619 struct i2c_client *client = to_i2c_client(dev);
0620 struct synaptics_i2c *touch = i2c_get_clientdata(client);
0621
0622 ret = synaptics_i2c_reset_config(client);
0623 if (ret)
0624 return ret;
0625
0626 mod_delayed_work(system_wq, &touch->dwork,
0627 msecs_to_jiffies(NO_DATA_SLEEP_MSECS));
0628
0629 return 0;
0630 }
0631
0632 static SIMPLE_DEV_PM_OPS(synaptics_i2c_pm, synaptics_i2c_suspend,
0633 synaptics_i2c_resume);
0634
0635 static const struct i2c_device_id synaptics_i2c_id_table[] = {
0636 { "synaptics_i2c", 0 },
0637 { },
0638 };
0639 MODULE_DEVICE_TABLE(i2c, synaptics_i2c_id_table);
0640
0641 #ifdef CONFIG_OF
0642 static const struct of_device_id synaptics_i2c_of_match[] = {
0643 { .compatible = "synaptics,synaptics_i2c", },
0644 { },
0645 };
0646 MODULE_DEVICE_TABLE(of, synaptics_i2c_of_match);
0647 #endif
0648
0649 static struct i2c_driver synaptics_i2c_driver = {
0650 .driver = {
0651 .name = DRIVER_NAME,
0652 .of_match_table = of_match_ptr(synaptics_i2c_of_match),
0653 .pm = &synaptics_i2c_pm,
0654 },
0655
0656 .probe = synaptics_i2c_probe,
0657 .remove = synaptics_i2c_remove,
0658
0659 .id_table = synaptics_i2c_id_table,
0660 };
0661
0662 module_i2c_driver(synaptics_i2c_driver);
0663
0664 MODULE_DESCRIPTION("Synaptics I2C touchpad driver");
0665 MODULE_AUTHOR("Mike Rapoport, Igor Grinberg, Compulab");
0666 MODULE_LICENSE("GPL");
0667