126822652SMichael Srba // SPDX-License-Identifier: GPL-2.0-only 226822652SMichael Srba 326822652SMichael Srba #include <linux/delay.h> 426822652SMichael Srba #include <linux/i2c.h> 526822652SMichael Srba #include <linux/input.h> 626822652SMichael Srba #include <linux/input/mt.h> 726822652SMichael Srba #include <linux/input/touchscreen.h> 826822652SMichael Srba #include <linux/interrupt.h> 926822652SMichael Srba #include <linux/irq.h> 1026822652SMichael Srba #include <linux/kernel.h> 1126822652SMichael Srba #include <linux/module.h> 1226822652SMichael Srba #include <linux/of.h> 1326822652SMichael Srba #include <linux/regulator/consumer.h> 1426822652SMichael Srba #include <linux/slab.h> 1526822652SMichael Srba 1626822652SMichael Srba /* Register Map */ 1726822652SMichael Srba 1826822652SMichael Srba #define BT541_SWRESET_CMD 0x0000 1926822652SMichael Srba #define BT541_WAKEUP_CMD 0x0001 2026822652SMichael Srba 2126822652SMichael Srba #define BT541_IDLE_CMD 0x0004 2226822652SMichael Srba #define BT541_SLEEP_CMD 0x0005 2326822652SMichael Srba 2426822652SMichael Srba #define BT541_CLEAR_INT_STATUS_CMD 0x0003 2526822652SMichael Srba #define BT541_CALIBRATE_CMD 0x0006 2626822652SMichael Srba #define BT541_SAVE_STATUS_CMD 0x0007 2726822652SMichael Srba #define BT541_SAVE_CALIBRATION_CMD 0x0008 2826822652SMichael Srba #define BT541_RECALL_FACTORY_CMD 0x000f 2926822652SMichael Srba 3026822652SMichael Srba #define BT541_THRESHOLD 0x0020 3126822652SMichael Srba 3226822652SMichael Srba #define BT541_LARGE_PALM_REJECT_AREA_TH 0x003F 3326822652SMichael Srba 3426822652SMichael Srba #define BT541_DEBUG_REG 0x0115 /* 0~7 */ 3526822652SMichael Srba 3626822652SMichael Srba #define BT541_TOUCH_MODE 0x0010 3726822652SMichael Srba #define BT541_CHIP_REVISION 0x0011 3826822652SMichael Srba #define BT541_FIRMWARE_VERSION 0x0012 3926822652SMichael Srba 4026822652SMichael Srba #define ZINITIX_USB_DETECT 0x116 4126822652SMichael Srba 4226822652SMichael Srba #define BT541_MINOR_FW_VERSION 0x0121 4326822652SMichael Srba 4426822652SMichael Srba #define BT541_VENDOR_ID 0x001C 4526822652SMichael Srba #define BT541_HW_ID 0x0014 4626822652SMichael Srba 4726822652SMichael Srba #define BT541_DATA_VERSION_REG 0x0013 4826822652SMichael Srba #define BT541_SUPPORTED_FINGER_NUM 0x0015 4926822652SMichael Srba #define BT541_EEPROM_INFO 0x0018 5026822652SMichael Srba #define BT541_INITIAL_TOUCH_MODE 0x0019 5126822652SMichael Srba 5226822652SMichael Srba #define BT541_TOTAL_NUMBER_OF_X 0x0060 5326822652SMichael Srba #define BT541_TOTAL_NUMBER_OF_Y 0x0061 5426822652SMichael Srba 5526822652SMichael Srba #define BT541_DELAY_RAW_FOR_HOST 0x007f 5626822652SMichael Srba 5726822652SMichael Srba #define BT541_BUTTON_SUPPORTED_NUM 0x00B0 5826822652SMichael Srba #define BT541_BUTTON_SENSITIVITY 0x00B2 5926822652SMichael Srba #define BT541_DUMMY_BUTTON_SENSITIVITY 0X00C8 6026822652SMichael Srba 6126822652SMichael Srba #define BT541_X_RESOLUTION 0x00C0 6226822652SMichael Srba #define BT541_Y_RESOLUTION 0x00C1 6326822652SMichael Srba 6426822652SMichael Srba #define BT541_POINT_STATUS_REG 0x0080 6526822652SMichael Srba #define BT541_ICON_STATUS_REG 0x00AA 6626822652SMichael Srba 6726822652SMichael Srba #define BT541_POINT_COORD_REG (BT541_POINT_STATUS_REG + 2) 6826822652SMichael Srba 6926822652SMichael Srba #define BT541_AFE_FREQUENCY 0x0100 7026822652SMichael Srba #define BT541_DND_N_COUNT 0x0122 7126822652SMichael Srba #define BT541_DND_U_COUNT 0x0135 7226822652SMichael Srba 7326822652SMichael Srba #define BT541_RAWDATA_REG 0x0200 7426822652SMichael Srba 7526822652SMichael Srba #define BT541_EEPROM_INFO_REG 0x0018 7626822652SMichael Srba 7726822652SMichael Srba #define BT541_INT_ENABLE_FLAG 0x00f0 7826822652SMichael Srba #define BT541_PERIODICAL_INTERRUPT_INTERVAL 0x00f1 7926822652SMichael Srba 8026822652SMichael Srba #define BT541_BTN_WIDTH 0x016d 8126822652SMichael Srba 8226822652SMichael Srba #define BT541_CHECKSUM_RESULT 0x012c 8326822652SMichael Srba 8426822652SMichael Srba #define BT541_INIT_FLASH 0x01d0 8526822652SMichael Srba #define BT541_WRITE_FLASH 0x01d1 8626822652SMichael Srba #define BT541_READ_FLASH 0x01d2 8726822652SMichael Srba 8826822652SMichael Srba #define ZINITIX_INTERNAL_FLAG_02 0x011e 8926822652SMichael Srba #define ZINITIX_INTERNAL_FLAG_03 0x011f 9026822652SMichael Srba 9126822652SMichael Srba #define ZINITIX_I2C_CHECKSUM_WCNT 0x016a 9226822652SMichael Srba #define ZINITIX_I2C_CHECKSUM_RESULT 0x016c 9326822652SMichael Srba 9426822652SMichael Srba /* Interrupt & status register flags */ 9526822652SMichael Srba 9626822652SMichael Srba #define BIT_PT_CNT_CHANGE BIT(0) 9726822652SMichael Srba #define BIT_DOWN BIT(1) 9826822652SMichael Srba #define BIT_MOVE BIT(2) 9926822652SMichael Srba #define BIT_UP BIT(3) 10026822652SMichael Srba #define BIT_PALM BIT(4) 10126822652SMichael Srba #define BIT_PALM_REJECT BIT(5) 10226822652SMichael Srba #define BIT_RESERVED_0 BIT(6) 10326822652SMichael Srba #define BIT_RESERVED_1 BIT(7) 10426822652SMichael Srba #define BIT_WEIGHT_CHANGE BIT(8) 10526822652SMichael Srba #define BIT_PT_NO_CHANGE BIT(9) 10626822652SMichael Srba #define BIT_REJECT BIT(10) 10726822652SMichael Srba #define BIT_PT_EXIST BIT(11) 10826822652SMichael Srba #define BIT_RESERVED_2 BIT(12) 10926822652SMichael Srba #define BIT_ERROR BIT(13) 11026822652SMichael Srba #define BIT_DEBUG BIT(14) 11126822652SMichael Srba #define BIT_ICON_EVENT BIT(15) 11226822652SMichael Srba 11326822652SMichael Srba #define SUB_BIT_EXIST BIT(0) 11426822652SMichael Srba #define SUB_BIT_DOWN BIT(1) 11526822652SMichael Srba #define SUB_BIT_MOVE BIT(2) 11626822652SMichael Srba #define SUB_BIT_UP BIT(3) 11726822652SMichael Srba #define SUB_BIT_UPDATE BIT(4) 11826822652SMichael Srba #define SUB_BIT_WAIT BIT(5) 11926822652SMichael Srba 12026822652SMichael Srba #define DEFAULT_TOUCH_POINT_MODE 2 12126822652SMichael Srba #define MAX_SUPPORTED_FINGER_NUM 5 12226822652SMichael Srba 12326822652SMichael Srba #define CHIP_ON_DELAY 15 // ms 12426822652SMichael Srba #define FIRMWARE_ON_DELAY 40 // ms 12526822652SMichael Srba 12626822652SMichael Srba struct point_coord { 12726822652SMichael Srba __le16 x; 12826822652SMichael Srba __le16 y; 12926822652SMichael Srba u8 width; 13026822652SMichael Srba u8 sub_status; 13126822652SMichael Srba // currently unused, but needed as padding: 13226822652SMichael Srba u8 minor_width; 13326822652SMichael Srba u8 angle; 13426822652SMichael Srba }; 13526822652SMichael Srba 13626822652SMichael Srba struct touch_event { 13726822652SMichael Srba __le16 status; 138*e941dc13SLinus Walleij u8 finger_mask; 13926822652SMichael Srba u8 time_stamp; 14026822652SMichael Srba struct point_coord point_coord[MAX_SUPPORTED_FINGER_NUM]; 14126822652SMichael Srba }; 14226822652SMichael Srba 14326822652SMichael Srba struct bt541_ts_data { 14426822652SMichael Srba struct i2c_client *client; 14526822652SMichael Srba struct input_dev *input_dev; 14626822652SMichael Srba struct touchscreen_properties prop; 14726822652SMichael Srba struct regulator_bulk_data supplies[2]; 14826822652SMichael Srba u32 zinitix_mode; 14926822652SMichael Srba }; 15026822652SMichael Srba 15126822652SMichael Srba static int zinitix_read_data(struct i2c_client *client, 15226822652SMichael Srba u16 reg, void *values, size_t length) 15326822652SMichael Srba { 15426822652SMichael Srba __le16 reg_le = cpu_to_le16(reg); 15526822652SMichael Srba int ret; 15626822652SMichael Srba 15726822652SMichael Srba /* A single i2c_transfer() transaction does not work here. */ 15826822652SMichael Srba ret = i2c_master_send(client, (u8 *)®_le, sizeof(reg_le)); 15926822652SMichael Srba if (ret != sizeof(reg_le)) 16026822652SMichael Srba return ret < 0 ? ret : -EIO; 16126822652SMichael Srba 16226822652SMichael Srba ret = i2c_master_recv(client, (u8 *)values, length); 16326822652SMichael Srba if (ret != length) 164a374c19fSYang Li return ret < 0 ? ret : -EIO; 16526822652SMichael Srba 16626822652SMichael Srba return 0; 16726822652SMichael Srba } 16826822652SMichael Srba 16926822652SMichael Srba static int zinitix_write_u16(struct i2c_client *client, u16 reg, u16 value) 17026822652SMichael Srba { 17126822652SMichael Srba __le16 packet[2] = {cpu_to_le16(reg), cpu_to_le16(value)}; 17226822652SMichael Srba int ret; 17326822652SMichael Srba 17426822652SMichael Srba ret = i2c_master_send(client, (u8 *)packet, sizeof(packet)); 17526822652SMichael Srba if (ret != sizeof(packet)) 17626822652SMichael Srba return ret < 0 ? ret : -EIO; 17726822652SMichael Srba 17826822652SMichael Srba return 0; 17926822652SMichael Srba } 18026822652SMichael Srba 18126822652SMichael Srba static int zinitix_write_cmd(struct i2c_client *client, u16 reg) 18226822652SMichael Srba { 18326822652SMichael Srba __le16 reg_le = cpu_to_le16(reg); 18426822652SMichael Srba int ret; 18526822652SMichael Srba 18626822652SMichael Srba ret = i2c_master_send(client, (u8 *)®_le, sizeof(reg_le)); 18726822652SMichael Srba if (ret != sizeof(reg_le)) 18826822652SMichael Srba return ret < 0 ? ret : -EIO; 18926822652SMichael Srba 19026822652SMichael Srba return 0; 19126822652SMichael Srba } 19226822652SMichael Srba 193836f308cSDmitry Torokhov static int zinitix_init_touch(struct bt541_ts_data *bt541) 19426822652SMichael Srba { 19526822652SMichael Srba struct i2c_client *client = bt541->client; 19626822652SMichael Srba int i; 19726822652SMichael Srba int error; 19826822652SMichael Srba 19926822652SMichael Srba error = zinitix_write_cmd(client, BT541_SWRESET_CMD); 20026822652SMichael Srba if (error) { 20126822652SMichael Srba dev_err(&client->dev, "Failed to write reset command\n"); 20226822652SMichael Srba return error; 20326822652SMichael Srba } 20426822652SMichael Srba 20526822652SMichael Srba error = zinitix_write_u16(client, BT541_INT_ENABLE_FLAG, 0x0); 20626822652SMichael Srba if (error) { 20726822652SMichael Srba dev_err(&client->dev, 20826822652SMichael Srba "Failed to reset interrupt enable flag\n"); 20926822652SMichael Srba return error; 21026822652SMichael Srba } 21126822652SMichael Srba 21226822652SMichael Srba /* initialize */ 21326822652SMichael Srba error = zinitix_write_u16(client, BT541_X_RESOLUTION, 21426822652SMichael Srba bt541->prop.max_x); 21526822652SMichael Srba if (error) 21626822652SMichael Srba return error; 21726822652SMichael Srba 21826822652SMichael Srba error = zinitix_write_u16(client, BT541_Y_RESOLUTION, 21926822652SMichael Srba bt541->prop.max_y); 22026822652SMichael Srba if (error) 22126822652SMichael Srba return error; 22226822652SMichael Srba 22326822652SMichael Srba error = zinitix_write_u16(client, BT541_SUPPORTED_FINGER_NUM, 22426822652SMichael Srba MAX_SUPPORTED_FINGER_NUM); 22526822652SMichael Srba if (error) 22626822652SMichael Srba return error; 22726822652SMichael Srba 22826822652SMichael Srba error = zinitix_write_u16(client, BT541_INITIAL_TOUCH_MODE, 22926822652SMichael Srba bt541->zinitix_mode); 23026822652SMichael Srba if (error) 23126822652SMichael Srba return error; 23226822652SMichael Srba 23326822652SMichael Srba error = zinitix_write_u16(client, BT541_TOUCH_MODE, 23426822652SMichael Srba bt541->zinitix_mode); 23526822652SMichael Srba if (error) 23626822652SMichael Srba return error; 23726822652SMichael Srba 23826822652SMichael Srba error = zinitix_write_u16(client, BT541_INT_ENABLE_FLAG, 23926822652SMichael Srba BIT_PT_CNT_CHANGE | BIT_DOWN | BIT_MOVE | 24026822652SMichael Srba BIT_UP); 24126822652SMichael Srba if (error) 24226822652SMichael Srba return error; 24326822652SMichael Srba 24426822652SMichael Srba /* clear queue */ 24526822652SMichael Srba for (i = 0; i < 10; i++) { 24626822652SMichael Srba zinitix_write_cmd(client, BT541_CLEAR_INT_STATUS_CMD); 24726822652SMichael Srba udelay(10); 24826822652SMichael Srba } 24926822652SMichael Srba 25026822652SMichael Srba return 0; 25126822652SMichael Srba } 25226822652SMichael Srba 25326822652SMichael Srba static int zinitix_init_regulators(struct bt541_ts_data *bt541) 25426822652SMichael Srba { 255c54be0e3SLinus Walleij struct device *dev = &bt541->client->dev; 25626822652SMichael Srba int error; 25726822652SMichael Srba 258c54be0e3SLinus Walleij /* 259c54be0e3SLinus Walleij * Some older device trees have erroneous names for the regulators, 260c54be0e3SLinus Walleij * so check if "vddo" is present and in that case use these names. 261c54be0e3SLinus Walleij * Else use the proper supply names on the component. 262c54be0e3SLinus Walleij */ 263c54be0e3SLinus Walleij if (of_find_property(dev->of_node, "vddo-supply", NULL)) { 26426822652SMichael Srba bt541->supplies[0].supply = "vdd"; 26526822652SMichael Srba bt541->supplies[1].supply = "vddo"; 266c54be0e3SLinus Walleij } else { 267c54be0e3SLinus Walleij /* Else use the proper supply names */ 268c54be0e3SLinus Walleij bt541->supplies[0].supply = "vcca"; 269c54be0e3SLinus Walleij bt541->supplies[1].supply = "vdd"; 270c54be0e3SLinus Walleij } 271c54be0e3SLinus Walleij error = devm_regulator_bulk_get(dev, 27226822652SMichael Srba ARRAY_SIZE(bt541->supplies), 27326822652SMichael Srba bt541->supplies); 27426822652SMichael Srba if (error < 0) { 275c54be0e3SLinus Walleij dev_err(dev, "Failed to get regulators: %d\n", error); 27626822652SMichael Srba return error; 27726822652SMichael Srba } 27826822652SMichael Srba 27926822652SMichael Srba return 0; 28026822652SMichael Srba } 28126822652SMichael Srba 28226822652SMichael Srba static int zinitix_send_power_on_sequence(struct bt541_ts_data *bt541) 28326822652SMichael Srba { 28426822652SMichael Srba int error; 28526822652SMichael Srba struct i2c_client *client = bt541->client; 28626822652SMichael Srba 28726822652SMichael Srba error = zinitix_write_u16(client, 0xc000, 0x0001); 28826822652SMichael Srba if (error) { 28926822652SMichael Srba dev_err(&client->dev, 29026822652SMichael Srba "Failed to send power sequence(vendor cmd enable)\n"); 29126822652SMichael Srba return error; 29226822652SMichael Srba } 29326822652SMichael Srba udelay(10); 29426822652SMichael Srba 29526822652SMichael Srba error = zinitix_write_cmd(client, 0xc004); 29626822652SMichael Srba if (error) { 29726822652SMichael Srba dev_err(&client->dev, 29826822652SMichael Srba "Failed to send power sequence (intn clear)\n"); 29926822652SMichael Srba return error; 30026822652SMichael Srba } 30126822652SMichael Srba udelay(10); 30226822652SMichael Srba 30326822652SMichael Srba error = zinitix_write_u16(client, 0xc002, 0x0001); 30426822652SMichael Srba if (error) { 30526822652SMichael Srba dev_err(&client->dev, 30626822652SMichael Srba "Failed to send power sequence (nvm init)\n"); 30726822652SMichael Srba return error; 30826822652SMichael Srba } 30926822652SMichael Srba mdelay(2); 31026822652SMichael Srba 31126822652SMichael Srba error = zinitix_write_u16(client, 0xc001, 0x0001); 31226822652SMichael Srba if (error) { 31326822652SMichael Srba dev_err(&client->dev, 31426822652SMichael Srba "Failed to send power sequence (program start)\n"); 31526822652SMichael Srba return error; 31626822652SMichael Srba } 31726822652SMichael Srba msleep(FIRMWARE_ON_DELAY); 31826822652SMichael Srba 31926822652SMichael Srba return 0; 32026822652SMichael Srba } 32126822652SMichael Srba 32226822652SMichael Srba static void zinitix_report_finger(struct bt541_ts_data *bt541, int slot, 32326822652SMichael Srba const struct point_coord *p) 32426822652SMichael Srba { 325*e941dc13SLinus Walleij u16 x, y; 326*e941dc13SLinus Walleij 327*e941dc13SLinus Walleij if (unlikely(!(p->sub_status & 328*e941dc13SLinus Walleij (SUB_BIT_UP | SUB_BIT_DOWN | SUB_BIT_MOVE)))) { 329*e941dc13SLinus Walleij dev_dbg(&bt541->client->dev, "unknown finger event %#02x\n", 330*e941dc13SLinus Walleij p->sub_status); 331*e941dc13SLinus Walleij return; 332*e941dc13SLinus Walleij } 333*e941dc13SLinus Walleij 334*e941dc13SLinus Walleij x = le16_to_cpu(p->x); 335*e941dc13SLinus Walleij y = le16_to_cpu(p->y); 336*e941dc13SLinus Walleij 33726822652SMichael Srba input_mt_slot(bt541->input_dev, slot); 338*e941dc13SLinus Walleij if (input_mt_report_slot_state(bt541->input_dev, MT_TOOL_FINGER, 339*e941dc13SLinus Walleij !(p->sub_status & SUB_BIT_UP))) { 340*e941dc13SLinus Walleij touchscreen_report_pos(bt541->input_dev, 341*e941dc13SLinus Walleij &bt541->prop, x, y, true); 342*e941dc13SLinus Walleij input_report_abs(bt541->input_dev, 343*e941dc13SLinus Walleij ABS_MT_TOUCH_MAJOR, p->width); 344*e941dc13SLinus Walleij dev_dbg(&bt541->client->dev, "finger %d %s (%u, %u)\n", 345*e941dc13SLinus Walleij slot, p->sub_status & SUB_BIT_DOWN ? "down" : "move", 346*e941dc13SLinus Walleij x, y); 347*e941dc13SLinus Walleij } else { 348*e941dc13SLinus Walleij dev_dbg(&bt541->client->dev, "finger %d up (%u, %u)\n", 349*e941dc13SLinus Walleij slot, x, y); 350*e941dc13SLinus Walleij } 35126822652SMichael Srba } 35226822652SMichael Srba 35326822652SMichael Srba static irqreturn_t zinitix_ts_irq_handler(int irq, void *bt541_handler) 35426822652SMichael Srba { 35526822652SMichael Srba struct bt541_ts_data *bt541 = bt541_handler; 35626822652SMichael Srba struct i2c_client *client = bt541->client; 35726822652SMichael Srba struct touch_event touch_event; 358*e941dc13SLinus Walleij unsigned long finger_mask; 35926822652SMichael Srba int error; 36026822652SMichael Srba int i; 36126822652SMichael Srba 36226822652SMichael Srba memset(&touch_event, 0, sizeof(struct touch_event)); 36326822652SMichael Srba 36426822652SMichael Srba error = zinitix_read_data(bt541->client, BT541_POINT_STATUS_REG, 36526822652SMichael Srba &touch_event, sizeof(struct touch_event)); 36626822652SMichael Srba if (error) { 36726822652SMichael Srba dev_err(&client->dev, "Failed to read in touchpoint struct\n"); 36826822652SMichael Srba goto out; 36926822652SMichael Srba } 37026822652SMichael Srba 371*e941dc13SLinus Walleij finger_mask = touch_event.finger_mask; 372*e941dc13SLinus Walleij for_each_set_bit(i, &finger_mask, MAX_SUPPORTED_FINGER_NUM) { 373*e941dc13SLinus Walleij const struct point_coord *p = &touch_event.point_coord[i]; 374*e941dc13SLinus Walleij 375*e941dc13SLinus Walleij /* Only process contacts that are actually reported */ 376*e941dc13SLinus Walleij if (p->sub_status & SUB_BIT_EXIST) 377*e941dc13SLinus Walleij zinitix_report_finger(bt541, i, p); 378*e941dc13SLinus Walleij } 37926822652SMichael Srba 38026822652SMichael Srba input_mt_sync_frame(bt541->input_dev); 38126822652SMichael Srba input_sync(bt541->input_dev); 38226822652SMichael Srba 38326822652SMichael Srba out: 38426822652SMichael Srba zinitix_write_cmd(bt541->client, BT541_CLEAR_INT_STATUS_CMD); 38526822652SMichael Srba return IRQ_HANDLED; 38626822652SMichael Srba } 38726822652SMichael Srba 38826822652SMichael Srba static int zinitix_start(struct bt541_ts_data *bt541) 38926822652SMichael Srba { 39026822652SMichael Srba int error; 39126822652SMichael Srba 39226822652SMichael Srba error = regulator_bulk_enable(ARRAY_SIZE(bt541->supplies), 39326822652SMichael Srba bt541->supplies); 39426822652SMichael Srba if (error) { 39526822652SMichael Srba dev_err(&bt541->client->dev, 39626822652SMichael Srba "Failed to enable regulators: %d\n", error); 39726822652SMichael Srba return error; 39826822652SMichael Srba } 39926822652SMichael Srba 40026822652SMichael Srba msleep(CHIP_ON_DELAY); 40126822652SMichael Srba 40226822652SMichael Srba error = zinitix_send_power_on_sequence(bt541); 40326822652SMichael Srba if (error) { 40426822652SMichael Srba dev_err(&bt541->client->dev, 40526822652SMichael Srba "Error while sending power-on sequence: %d\n", error); 40626822652SMichael Srba return error; 40726822652SMichael Srba } 40826822652SMichael Srba 40926822652SMichael Srba error = zinitix_init_touch(bt541); 41026822652SMichael Srba if (error) { 41126822652SMichael Srba dev_err(&bt541->client->dev, 41226822652SMichael Srba "Error while configuring touch IC\n"); 41326822652SMichael Srba return error; 41426822652SMichael Srba } 41526822652SMichael Srba 41626822652SMichael Srba enable_irq(bt541->client->irq); 41726822652SMichael Srba 41826822652SMichael Srba return 0; 41926822652SMichael Srba } 42026822652SMichael Srba 42126822652SMichael Srba static int zinitix_stop(struct bt541_ts_data *bt541) 42226822652SMichael Srba { 42326822652SMichael Srba int error; 42426822652SMichael Srba 42526822652SMichael Srba disable_irq(bt541->client->irq); 42626822652SMichael Srba 42726822652SMichael Srba error = regulator_bulk_disable(ARRAY_SIZE(bt541->supplies), 42826822652SMichael Srba bt541->supplies); 42926822652SMichael Srba if (error) { 43026822652SMichael Srba dev_err(&bt541->client->dev, 43126822652SMichael Srba "Failed to disable regulators: %d\n", error); 43226822652SMichael Srba return error; 43326822652SMichael Srba } 43426822652SMichael Srba 43526822652SMichael Srba return 0; 43626822652SMichael Srba } 43726822652SMichael Srba 43826822652SMichael Srba static int zinitix_input_open(struct input_dev *dev) 43926822652SMichael Srba { 44026822652SMichael Srba struct bt541_ts_data *bt541 = input_get_drvdata(dev); 44126822652SMichael Srba 44226822652SMichael Srba return zinitix_start(bt541); 44326822652SMichael Srba } 44426822652SMichael Srba 44526822652SMichael Srba static void zinitix_input_close(struct input_dev *dev) 44626822652SMichael Srba { 44726822652SMichael Srba struct bt541_ts_data *bt541 = input_get_drvdata(dev); 44826822652SMichael Srba 44926822652SMichael Srba zinitix_stop(bt541); 45026822652SMichael Srba } 45126822652SMichael Srba 45226822652SMichael Srba static int zinitix_init_input_dev(struct bt541_ts_data *bt541) 45326822652SMichael Srba { 45426822652SMichael Srba struct input_dev *input_dev; 45526822652SMichael Srba int error; 45626822652SMichael Srba 45726822652SMichael Srba input_dev = devm_input_allocate_device(&bt541->client->dev); 45826822652SMichael Srba if (!input_dev) { 45926822652SMichael Srba dev_err(&bt541->client->dev, 46026822652SMichael Srba "Failed to allocate input device."); 46126822652SMichael Srba return -ENOMEM; 46226822652SMichael Srba } 46326822652SMichael Srba 46426822652SMichael Srba input_set_drvdata(input_dev, bt541); 46526822652SMichael Srba bt541->input_dev = input_dev; 46626822652SMichael Srba 46726822652SMichael Srba input_dev->name = "Zinitix Capacitive TouchScreen"; 46826822652SMichael Srba input_dev->phys = "input/ts"; 46926822652SMichael Srba input_dev->id.bustype = BUS_I2C; 47026822652SMichael Srba input_dev->open = zinitix_input_open; 47126822652SMichael Srba input_dev->close = zinitix_input_close; 47226822652SMichael Srba 47326822652SMichael Srba input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X); 47426822652SMichael Srba input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y); 47526822652SMichael Srba input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0); 47626822652SMichael Srba input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 47726822652SMichael Srba 47826822652SMichael Srba touchscreen_parse_properties(input_dev, true, &bt541->prop); 47926822652SMichael Srba if (!bt541->prop.max_x || !bt541->prop.max_y) { 48026822652SMichael Srba dev_err(&bt541->client->dev, 48126822652SMichael Srba "Touchscreen-size-x and/or touchscreen-size-y not set in dts\n"); 48226822652SMichael Srba return -EINVAL; 48326822652SMichael Srba } 48426822652SMichael Srba 48526822652SMichael Srba error = input_mt_init_slots(input_dev, MAX_SUPPORTED_FINGER_NUM, 48626822652SMichael Srba INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 48726822652SMichael Srba if (error) { 48826822652SMichael Srba dev_err(&bt541->client->dev, 48926822652SMichael Srba "Failed to initialize MT slots: %d", error); 49026822652SMichael Srba return error; 49126822652SMichael Srba } 49226822652SMichael Srba 49326822652SMichael Srba error = input_register_device(input_dev); 49426822652SMichael Srba if (error) { 49526822652SMichael Srba dev_err(&bt541->client->dev, 49626822652SMichael Srba "Failed to register input device: %d", error); 49726822652SMichael Srba return error; 49826822652SMichael Srba } 49926822652SMichael Srba 50026822652SMichael Srba return 0; 50126822652SMichael Srba } 50226822652SMichael Srba 50326822652SMichael Srba static int zinitix_ts_probe(struct i2c_client *client) 50426822652SMichael Srba { 50526822652SMichael Srba struct bt541_ts_data *bt541; 50626822652SMichael Srba int error; 50726822652SMichael Srba 50826822652SMichael Srba if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 50926822652SMichael Srba dev_err(&client->dev, 51026822652SMichael Srba "Failed to assert adapter's support for plain I2C.\n"); 51126822652SMichael Srba return -ENXIO; 51226822652SMichael Srba } 51326822652SMichael Srba 51426822652SMichael Srba bt541 = devm_kzalloc(&client->dev, sizeof(*bt541), GFP_KERNEL); 51526822652SMichael Srba if (!bt541) 51626822652SMichael Srba return -ENOMEM; 51726822652SMichael Srba 51826822652SMichael Srba bt541->client = client; 51926822652SMichael Srba i2c_set_clientdata(client, bt541); 52026822652SMichael Srba 52126822652SMichael Srba error = zinitix_init_regulators(bt541); 52226822652SMichael Srba if (error) { 52326822652SMichael Srba dev_err(&client->dev, 52426822652SMichael Srba "Failed to initialize regulators: %d\n", error); 52526822652SMichael Srba return error; 52626822652SMichael Srba } 52726822652SMichael Srba 528cf73ed89SNikita Travkin error = devm_request_threaded_irq(&client->dev, client->irq, 529cf73ed89SNikita Travkin NULL, zinitix_ts_irq_handler, 530cf73ed89SNikita Travkin IRQF_ONESHOT | IRQF_NO_AUTOEN, 531cf73ed89SNikita Travkin client->name, bt541); 532cf73ed89SNikita Travkin if (error) { 533cf73ed89SNikita Travkin dev_err(&client->dev, "Failed to request IRQ: %d\n", error); 534cf73ed89SNikita Travkin return error; 535cf73ed89SNikita Travkin } 536cf73ed89SNikita Travkin 53726822652SMichael Srba error = zinitix_init_input_dev(bt541); 53826822652SMichael Srba if (error) { 53926822652SMichael Srba dev_err(&client->dev, 54026822652SMichael Srba "Failed to initialize input device: %d\n", error); 54126822652SMichael Srba return error; 54226822652SMichael Srba } 54326822652SMichael Srba 54426822652SMichael Srba error = device_property_read_u32(&client->dev, "zinitix,mode", 54526822652SMichael Srba &bt541->zinitix_mode); 54626822652SMichael Srba if (error < 0) { 54726822652SMichael Srba /* fall back to mode 2 */ 54826822652SMichael Srba bt541->zinitix_mode = DEFAULT_TOUCH_POINT_MODE; 54926822652SMichael Srba } 55026822652SMichael Srba 55126822652SMichael Srba if (bt541->zinitix_mode != 2) { 55226822652SMichael Srba /* 55326822652SMichael Srba * If there are devices that don't support mode 2, support 55426822652SMichael Srba * for other modes (0, 1) will be needed. 55526822652SMichael Srba */ 55626822652SMichael Srba dev_err(&client->dev, 55726822652SMichael Srba "Malformed zinitix,mode property, must be 2 (supplied: %d)\n", 55826822652SMichael Srba bt541->zinitix_mode); 55926822652SMichael Srba return -EINVAL; 56026822652SMichael Srba } 56126822652SMichael Srba 56226822652SMichael Srba return 0; 56326822652SMichael Srba } 56426822652SMichael Srba 56526822652SMichael Srba static int __maybe_unused zinitix_suspend(struct device *dev) 56626822652SMichael Srba { 56726822652SMichael Srba struct i2c_client *client = to_i2c_client(dev); 56826822652SMichael Srba struct bt541_ts_data *bt541 = i2c_get_clientdata(client); 56926822652SMichael Srba 57026822652SMichael Srba mutex_lock(&bt541->input_dev->mutex); 57126822652SMichael Srba 572d69f0a43SAndrzej Pietrasiewicz if (input_device_enabled(bt541->input_dev)) 57326822652SMichael Srba zinitix_stop(bt541); 57426822652SMichael Srba 57526822652SMichael Srba mutex_unlock(&bt541->input_dev->mutex); 57626822652SMichael Srba 57726822652SMichael Srba return 0; 57826822652SMichael Srba } 57926822652SMichael Srba 58026822652SMichael Srba static int __maybe_unused zinitix_resume(struct device *dev) 58126822652SMichael Srba { 58226822652SMichael Srba struct i2c_client *client = to_i2c_client(dev); 58326822652SMichael Srba struct bt541_ts_data *bt541 = i2c_get_clientdata(client); 58426822652SMichael Srba int ret = 0; 58526822652SMichael Srba 58626822652SMichael Srba mutex_lock(&bt541->input_dev->mutex); 58726822652SMichael Srba 588d69f0a43SAndrzej Pietrasiewicz if (input_device_enabled(bt541->input_dev)) 58926822652SMichael Srba ret = zinitix_start(bt541); 59026822652SMichael Srba 59126822652SMichael Srba mutex_unlock(&bt541->input_dev->mutex); 59226822652SMichael Srba 59326822652SMichael Srba return ret; 59426822652SMichael Srba } 59526822652SMichael Srba 59626822652SMichael Srba static SIMPLE_DEV_PM_OPS(zinitix_pm_ops, zinitix_suspend, zinitix_resume); 59726822652SMichael Srba 59826822652SMichael Srba #ifdef CONFIG_OF 59926822652SMichael Srba static const struct of_device_id zinitix_of_match[] = { 600de8aa31aSLinus Walleij { .compatible = "zinitix,bt402" }, 601de8aa31aSLinus Walleij { .compatible = "zinitix,bt403" }, 602de8aa31aSLinus Walleij { .compatible = "zinitix,bt404" }, 603de8aa31aSLinus Walleij { .compatible = "zinitix,bt412" }, 604de8aa31aSLinus Walleij { .compatible = "zinitix,bt413" }, 605de8aa31aSLinus Walleij { .compatible = "zinitix,bt431" }, 606de8aa31aSLinus Walleij { .compatible = "zinitix,bt432" }, 607de8aa31aSLinus Walleij { .compatible = "zinitix,bt531" }, 6089df136b5SNikita Travkin { .compatible = "zinitix,bt532" }, 609de8aa31aSLinus Walleij { .compatible = "zinitix,bt538" }, 61026822652SMichael Srba { .compatible = "zinitix,bt541" }, 611de8aa31aSLinus Walleij { .compatible = "zinitix,bt548" }, 612de8aa31aSLinus Walleij { .compatible = "zinitix,bt554" }, 613de8aa31aSLinus Walleij { .compatible = "zinitix,at100" }, 61426822652SMichael Srba { } 61526822652SMichael Srba }; 61626822652SMichael Srba MODULE_DEVICE_TABLE(of, zinitix_of_match); 61726822652SMichael Srba #endif 61826822652SMichael Srba 61926822652SMichael Srba static struct i2c_driver zinitix_ts_driver = { 62026822652SMichael Srba .probe_new = zinitix_ts_probe, 62126822652SMichael Srba .driver = { 62226822652SMichael Srba .name = "Zinitix-TS", 62326822652SMichael Srba .pm = &zinitix_pm_ops, 62426822652SMichael Srba .of_match_table = of_match_ptr(zinitix_of_match), 62526822652SMichael Srba }, 62626822652SMichael Srba }; 62726822652SMichael Srba module_i2c_driver(zinitix_ts_driver); 62826822652SMichael Srba 62926822652SMichael Srba MODULE_AUTHOR("Michael Srba <[email protected]>"); 63026822652SMichael Srba MODULE_DESCRIPTION("Zinitix touchscreen driver"); 63126822652SMichael Srba MODULE_LICENSE("GPL v2"); 632