From 055f624e1eb9cae28211a7e1e140111d29228e13 Mon Sep 17 00:00:00 2001 From: Linus Walleij Date: Thu, 19 Oct 2017 16:21:05 -0700 Subject: Input: gpio_mouse - add DT bindings This adds DT bindings for simple mice attached to GPIO lines. As the properties are very general and pertains to all mice I can think of, we use very generic names for the 4-7 GPIO lines, "up", "down" etc. Acked-by: Hans-Christian Noren Egtvedt Signed-off-by: Linus Walleij Acked-by: Rob Herring Signed-off-by: Dmitry Torokhov --- .../devicetree/bindings/input/gpio-mouse.txt | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/gpio-mouse.txt (limited to 'Documentation') diff --git a/Documentation/devicetree/bindings/input/gpio-mouse.txt b/Documentation/devicetree/bindings/input/gpio-mouse.txt new file mode 100644 index 000000000000..519510a11af9 --- /dev/null +++ b/Documentation/devicetree/bindings/input/gpio-mouse.txt @@ -0,0 +1,32 @@ +Device-Tree bindings for GPIO attached mice + +This simply uses standard GPIO handles to define a simple mouse connected +to 5-7 GPIO lines. + +Required properties: + - compatible: must be "gpio-mouse" + - scan-interval-ms: The scanning interval in milliseconds + - up-gpios: GPIO line phandle to the line indicating "up" + - down-gpios: GPIO line phandle to the line indicating "down" + - left-gpios: GPIO line phandle to the line indicating "left" + - right-gpios: GPIO line phandle to the line indicating "right" + +Optional properties: + - button-left-gpios: GPIO line handle to the left mouse button + - button-middle-gpios: GPIO line handle to the middle mouse button + - button-right-gpios: GPIO line handle to the right mouse button +Example: + +#include + +gpio-mouse { + compatible = "gpio-mouse"; + scan-interval-ms = <50>; + up-gpios = <&gpio0 0 GPIO_ACTIVE_LOW>; + down-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>; + left-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>; + right-gpios = <&gpio0 3 GPIO_ACTIVE_LOW>; + button-left-gpios = <&gpio0 4 GPIO_ACTIVE_LOW>; + button-middle-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>; + button-right-gpios = <&gpio0 6 GPIO_ACTIVE_LOW>; +}; -- cgit v1.2.3 From 25309004c0e761986c9dc23526a03928a85188c0 Mon Sep 17 00:00:00 2001 From: Marcin Niestroj Date: Tue, 24 Oct 2017 11:06:41 -0700 Subject: Input: goodix - support gt1151 touchpanel Support was added based on Goodix GitHub repo [1]. There are two major differences between gt1151 and currently supported devices (gt9x): * CONFIG_DATA register has 0x8050 address instead of 0x8047, * config data checksum has 16-bit width instead of 8-bit. Also update goodix_i2c_test() function, so it reads ID register (which has the same address for all devices) instead of CONFIG_DATA (because its address is known only after reading ID of the device). [1] https://github.com/goodix/gt1x_driver_generic Signed-off-by: Marcin Niestroj Acked-by: Rob Herring Signed-off-by: Dmitry Torokhov --- .../bindings/input/touchscreen/goodix.txt | 3 +- drivers/input/touchscreen/goodix.c | 125 ++++++++++++++++----- 2 files changed, 100 insertions(+), 28 deletions(-) (limited to 'Documentation') diff --git a/Documentation/devicetree/bindings/input/touchscreen/goodix.txt b/Documentation/devicetree/bindings/input/touchscreen/goodix.txt index c98757a69110..0c369d8ebcab 100644 --- a/Documentation/devicetree/bindings/input/touchscreen/goodix.txt +++ b/Documentation/devicetree/bindings/input/touchscreen/goodix.txt @@ -2,7 +2,8 @@ Device tree bindings for Goodix GT9xx series touchscreen controller Required properties: - - compatible : Should be "goodix,gt911" + - compatible : Should be "goodix,gt1151" + or "goodix,gt911" or "goodix,gt9110" or "goodix,gt912" or "goodix,gt927" diff --git a/drivers/input/touchscreen/goodix.c b/drivers/input/touchscreen/goodix.c index b3bbad7d2282..69d0b8cbc71f 100644 --- a/drivers/input/touchscreen/goodix.c +++ b/drivers/input/touchscreen/goodix.c @@ -31,9 +31,18 @@ #include #include +struct goodix_ts_data; + +struct goodix_chip_data { + u16 config_addr; + int config_len; + int (*check_config)(struct goodix_ts_data *, const struct firmware *); +}; + struct goodix_ts_data { struct i2c_client *client; struct input_dev *input_dev; + const struct goodix_chip_data *chip; int abs_x_max; int abs_y_max; bool swapped_x_y; @@ -41,7 +50,6 @@ struct goodix_ts_data { bool inverted_y; unsigned int max_touch_num; unsigned int int_trigger_type; - int cfg_len; struct gpio_desc *gpiod_int; struct gpio_desc *gpiod_rst; u16 id; @@ -69,7 +77,8 @@ struct goodix_ts_data { #define GOODIX_CMD_SCREEN_OFF 0x05 #define GOODIX_READ_COOR_ADDR 0x814E -#define GOODIX_REG_CONFIG_DATA 0x8047 +#define GOODIX_GT1X_REG_CONFIG_DATA 0x8050 +#define GOODIX_GT9X_REG_CONFIG_DATA 0x8047 #define GOODIX_REG_ID 0x8140 #define GOODIX_BUFFER_STATUS_READY BIT(7) @@ -79,6 +88,35 @@ struct goodix_ts_data { #define MAX_CONTACTS_LOC 5 #define TRIGGER_LOC 6 +static int goodix_check_cfg_8(struct goodix_ts_data *ts, + const struct firmware *cfg); +static int goodix_check_cfg_16(struct goodix_ts_data *ts, + const struct firmware *cfg); + +static const struct goodix_chip_data gt1x_chip_data = { + .config_addr = GOODIX_GT1X_REG_CONFIG_DATA, + .config_len = GOODIX_CONFIG_MAX_LENGTH, + .check_config = goodix_check_cfg_16, +}; + +static const struct goodix_chip_data gt911_chip_data = { + .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, + .config_len = GOODIX_CONFIG_911_LENGTH, + .check_config = goodix_check_cfg_8, +}; + +static const struct goodix_chip_data gt967_chip_data = { + .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, + .config_len = GOODIX_CONFIG_967_LENGTH, + .check_config = goodix_check_cfg_8, +}; + +static const struct goodix_chip_data gt9x_chip_data = { + .config_addr = GOODIX_GT9X_REG_CONFIG_DATA, + .config_len = GOODIX_CONFIG_MAX_LENGTH, + .check_config = goodix_check_cfg_8, +}; + static const unsigned long goodix_irq_flags[] = { IRQ_TYPE_EDGE_RISING, IRQ_TYPE_EDGE_FALLING, @@ -177,22 +215,25 @@ static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value) return goodix_i2c_write(client, reg, &value, sizeof(value)); } -static int goodix_get_cfg_len(u16 id) +static const struct goodix_chip_data *goodix_get_chip_data(u16 id) { switch (id) { + case 1151: + return >1x_chip_data; + case 911: case 9271: case 9110: case 927: case 928: - return GOODIX_CONFIG_911_LENGTH; + return >911_chip_data; case 912: case 967: - return GOODIX_CONFIG_967_LENGTH; + return >967_chip_data; default: - return GOODIX_CONFIG_MAX_LENGTH; + return >9x_chip_data; } } @@ -332,25 +373,12 @@ static int goodix_request_irq(struct goodix_ts_data *ts) ts->irq_flags, ts->client->name, ts); } -/** - * goodix_check_cfg - Checks if config fw is valid - * - * @ts: goodix_ts_data pointer - * @cfg: firmware config data - */ -static int goodix_check_cfg(struct goodix_ts_data *ts, - const struct firmware *cfg) +static int goodix_check_cfg_8(struct goodix_ts_data *ts, + const struct firmware *cfg) { - int i, raw_cfg_len; + int i, raw_cfg_len = cfg->size - 2; u8 check_sum = 0; - if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) { - dev_err(&ts->client->dev, - "The length of the config fw is not correct"); - return -EINVAL; - } - - raw_cfg_len = cfg->size - 2; for (i = 0; i < raw_cfg_len; i++) check_sum += cfg->data[i]; check_sum = (~check_sum) + 1; @@ -369,6 +397,48 @@ static int goodix_check_cfg(struct goodix_ts_data *ts, return 0; } +static int goodix_check_cfg_16(struct goodix_ts_data *ts, + const struct firmware *cfg) +{ + int i, raw_cfg_len = cfg->size - 3; + u16 check_sum = 0; + + for (i = 0; i < raw_cfg_len; i += 2) + check_sum += get_unaligned_be16(&cfg->data[i]); + check_sum = (~check_sum) + 1; + if (check_sum != get_unaligned_be16(&cfg->data[raw_cfg_len])) { + dev_err(&ts->client->dev, + "The checksum of the config fw is not correct"); + return -EINVAL; + } + + if (cfg->data[raw_cfg_len + 2] != 1) { + dev_err(&ts->client->dev, + "Config fw must have Config_Fresh register set"); + return -EINVAL; + } + + return 0; +} + +/** + * goodix_check_cfg - Checks if config fw is valid + * + * @ts: goodix_ts_data pointer + * @cfg: firmware config data + */ +static int goodix_check_cfg(struct goodix_ts_data *ts, + const struct firmware *cfg) +{ + if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) { + dev_err(&ts->client->dev, + "The length of the config fw is not correct"); + return -EINVAL; + } + + return ts->chip->check_config(ts, cfg); +} + /** * goodix_send_cfg - Write fw config to device * @@ -384,7 +454,7 @@ static int goodix_send_cfg(struct goodix_ts_data *ts, if (error) return error; - error = goodix_i2c_write(ts->client, GOODIX_REG_CONFIG_DATA, cfg->data, + error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg->data, cfg->size); if (error) { dev_err(&ts->client->dev, "Failed to write config data: %d", @@ -511,8 +581,8 @@ static void goodix_read_config(struct goodix_ts_data *ts) u8 config[GOODIX_CONFIG_MAX_LENGTH]; int error; - error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA, - config, ts->cfg_len); + error = goodix_i2c_read(ts->client, ts->chip->config_addr, + config, ts->chip->config_len); if (error) { dev_warn(&ts->client->dev, "Error reading config (%d), using defaults\n", @@ -592,7 +662,7 @@ static int goodix_i2c_test(struct i2c_client *client) u8 test; while (retry++ < 2) { - error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA, + error = goodix_i2c_read(client, GOODIX_REG_ID, &test, 1); if (!error) return 0; @@ -762,7 +832,7 @@ static int goodix_ts_probe(struct i2c_client *client, return error; } - ts->cfg_len = goodix_get_cfg_len(ts->id); + ts->chip = goodix_get_chip_data(ts->id); if (ts->gpiod_int && ts->gpiod_rst) { /* update device config */ @@ -891,6 +961,7 @@ MODULE_DEVICE_TABLE(acpi, goodix_acpi_match); #ifdef CONFIG_OF static const struct of_device_id goodix_of_match[] = { + { .compatible = "goodix,gt1151" }, { .compatible = "goodix,gt911" }, { .compatible = "goodix,gt9110" }, { .compatible = "goodix,gt912" }, -- cgit v1.2.3 From 7e577a17f2eefeef32f1106ebf91e7cd143ba654 Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Sat, 14 Oct 2017 10:10:53 -0700 Subject: Input: add I2C attached EETI EXC3000 multi touch driver The 3000 series have a new protocol which allows to report up to 5 points in a single 66 byte frame. One must always read in 66 byte frames. To support up to 10 points, two consecutive frames need to be read: The first frame says how many points until sync. The second frame must say zero points or both frames must be discarded. To be able to work with the higher 400KHz I2C bus rate, one must successfully send a special package prior _each_ read or the controller will refuse to cooperate. This is a minimal implementation based on egalax_i2c.c (which can be found on the internet) and egalax_ts.c but without the vendor interface and no power management support. Signed-off-by: Ahmet Inan Acked-by: Rob Herring Signed-off-by: Dmitry Torokhov --- .../bindings/input/touchscreen/exc3000.txt | 27 +++ drivers/input/touchscreen/Kconfig | 10 + drivers/input/touchscreen/Makefile | 1 + drivers/input/touchscreen/exc3000.c | 223 +++++++++++++++++++++ 4 files changed, 261 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/touchscreen/exc3000.txt create mode 100644 drivers/input/touchscreen/exc3000.c (limited to 'Documentation') diff --git a/Documentation/devicetree/bindings/input/touchscreen/exc3000.txt b/Documentation/devicetree/bindings/input/touchscreen/exc3000.txt new file mode 100644 index 000000000000..1dcff4a43eaa --- /dev/null +++ b/Documentation/devicetree/bindings/input/touchscreen/exc3000.txt @@ -0,0 +1,27 @@ +* EETI EXC3000 Multiple Touch Controller + +Required properties: +- compatible: must be "eeti,exc3000" +- reg: i2c slave address +- interrupt-parent: the phandle for the interrupt controller +- interrupts: touch controller interrupt +- touchscreen-size-x: See touchscreen.txt +- touchscreen-size-y: See touchscreen.txt + +Optional properties: +- touchscreen-inverted-x: See touchscreen.txt +- touchscreen-inverted-y: See touchscreen.txt +- touchscreen-swapped-x-y: See touchscreen.txt + +Example: + + touchscreen@2a { + compatible = "eeti,exc3000"; + reg = <0x2a>; + interrupt-parent = <&gpio1>; + interrupts = <9 IRQ_TYPE_LEVEL_LOW>; + touchscreen-size-x = <4096>; + touchscreen-size-y = <4096>; + touchscreen-inverted-x; + touchscreen-swapped-x-y; + }; diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig index 16cadc149bbc..26a66bc7b2df 100644 --- a/drivers/input/touchscreen/Kconfig +++ b/drivers/input/touchscreen/Kconfig @@ -316,6 +316,16 @@ config TOUCHSCREEN_EGALAX_SERIAL To compile this driver as a module, choose M here: the module will be called egalax_ts_serial. +config TOUCHSCREEN_EXC3000 + tristate "EETI EXC3000 multi-touch panel support" + depends on I2C + help + Say Y here to enable support for I2C connected EETI + EXC3000 multi-touch panels. + + To compile this driver as a module, choose M here: the + module will be called exc3000. + config TOUCHSCREEN_FUJITSU tristate "Fujitsu serial touchscreen" select SERIO diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile index 6badce87037b..a129df6b2300 100644 --- a/drivers/input/touchscreen/Makefile +++ b/drivers/input/touchscreen/Makefile @@ -37,6 +37,7 @@ obj-$(CONFIG_TOUCHSCREEN_ELAN) += elants_i2c.o obj-$(CONFIG_TOUCHSCREEN_ELO) += elo.o obj-$(CONFIG_TOUCHSCREEN_EGALAX) += egalax_ts.o obj-$(CONFIG_TOUCHSCREEN_EGALAX_SERIAL) += egalax_ts_serial.o +obj-$(CONFIG_TOUCHSCREEN_EXC3000) += exc3000.o obj-$(CONFIG_TOUCHSCREEN_FUJITSU) += fujitsu_ts.o obj-$(CONFIG_TOUCHSCREEN_GOODIX) += goodix.o obj-$(CONFIG_TOUCHSCREEN_ILI210X) += ili210x.o diff --git a/drivers/input/touchscreen/exc3000.c b/drivers/input/touchscreen/exc3000.c new file mode 100644 index 000000000000..37437a53cd1a --- /dev/null +++ b/drivers/input/touchscreen/exc3000.c @@ -0,0 +1,223 @@ +/* + * Driver for I2C connected EETI EXC3000 multiple touch controller + * + * Copyright (C) 2017 Ahmet Inan + * + * minimal implementation based on egalax_ts.c and egalax_i2c.c + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define EXC3000_NUM_SLOTS 10 +#define EXC3000_SLOTS_PER_FRAME 5 +#define EXC3000_LEN_FRAME 66 +#define EXC3000_LEN_POINT 10 +#define EXC3000_MT_EVENT 6 +#define EXC3000_TIMEOUT_MS 100 + +struct exc3000_data { + struct i2c_client *client; + struct input_dev *input; + struct touchscreen_properties prop; + struct timer_list timer; + u8 buf[2 * EXC3000_LEN_FRAME]; +}; + +static void exc3000_report_slots(struct input_dev *input, + struct touchscreen_properties *prop, + const u8 *buf, int num) +{ + for (; num--; buf += EXC3000_LEN_POINT) { + if (buf[0] & BIT(0)) { + input_mt_slot(input, buf[1]); + input_mt_report_slot_state(input, MT_TOOL_FINGER, true); + touchscreen_report_pos(input, prop, + get_unaligned_le16(buf + 2), + get_unaligned_le16(buf + 4), + true); + } + } +} + +static void exc3000_timer(struct timer_list *t) +{ + struct exc3000_data *data = from_timer(data, t, timer); + + input_mt_sync_frame(data->input); + input_sync(data->input); +} + +static int exc3000_read_frame(struct i2c_client *client, u8 *buf) +{ + int ret; + + ret = i2c_master_send(client, "'", 2); + if (ret < 0) + return ret; + + if (ret != 2) + return -EIO; + + ret = i2c_master_recv(client, buf, EXC3000_LEN_FRAME); + if (ret < 0) + return ret; + + if (ret != EXC3000_LEN_FRAME) + return -EIO; + + if (get_unaligned_le16(buf) != EXC3000_LEN_FRAME || + buf[2] != EXC3000_MT_EVENT) + return -EINVAL; + + return 0; +} + +static int exc3000_read_data(struct i2c_client *client, + u8 *buf, int *n_slots) +{ + int error; + + error = exc3000_read_frame(client, buf); + if (error) + return error; + + *n_slots = buf[3]; + if (!*n_slots || *n_slots > EXC3000_NUM_SLOTS) + return -EINVAL; + + if (*n_slots > EXC3000_SLOTS_PER_FRAME) { + /* Read 2nd frame to get the rest of the contacts. */ + error = exc3000_read_frame(client, buf + EXC3000_LEN_FRAME); + if (error) + return error; + + /* 2nd chunk must have number of contacts set to 0. */ + if (buf[EXC3000_LEN_FRAME + 3] != 0) + return -EINVAL; + } + + return 0; +} + +static irqreturn_t exc3000_interrupt(int irq, void *dev_id) +{ + struct exc3000_data *data = dev_id; + struct input_dev *input = data->input; + u8 *buf = data->buf; + int slots, total_slots; + int error; + + error = exc3000_read_data(data->client, buf, &total_slots); + if (error) { + /* Schedule a timer to release "stuck" contacts */ + mod_timer(&data->timer, + jiffies + msecs_to_jiffies(EXC3000_TIMEOUT_MS)); + goto out; + } + + /* + * We read full state successfully, no contacts will be "stuck". + */ + del_timer_sync(&data->timer); + + while (total_slots > 0) { + slots = min(total_slots, EXC3000_SLOTS_PER_FRAME); + exc3000_report_slots(input, &data->prop, buf + 4, slots); + total_slots -= slots; + buf += EXC3000_LEN_FRAME; + } + + input_mt_sync_frame(input); + input_sync(input); + +out: + return IRQ_HANDLED; +} + +static int exc3000_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct exc3000_data *data; + struct input_dev *input; + int error; + + data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->client = client; + timer_setup(&data->timer, exc3000_timer, 0); + + input = devm_input_allocate_device(&client->dev); + if (!input) + return -ENOMEM; + + data->input = input; + + input->name = "EETI EXC3000 Touch Screen"; + input->id.bustype = BUS_I2C; + + input_set_abs_params(input, ABS_MT_POSITION_X, 0, 4095, 0, 0); + input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 4095, 0, 0); + touchscreen_parse_properties(input, true, &data->prop); + + error = input_mt_init_slots(input, EXC3000_NUM_SLOTS, + INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); + if (error) + return error; + + error = input_register_device(input); + if (error) + return error; + + error = devm_request_threaded_irq(&client->dev, client->irq, + NULL, exc3000_interrupt, IRQF_ONESHOT, + client->name, data); + if (error) + return error; + + return 0; +} + +static const struct i2c_device_id exc3000_id[] = { + { "exc3000", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, exc3000_id); + +#ifdef CONFIG_OF +static const struct of_device_id exc3000_of_match[] = { + { .compatible = "eeti,exc3000" }, + { } +}; +MODULE_DEVICE_TABLE(of, exc3000_of_match); +#endif + +static struct i2c_driver exc3000_driver = { + .driver = { + .name = "exc3000", + .of_match_table = of_match_ptr(exc3000_of_match), + }, + .id_table = exc3000_id, + .probe = exc3000_probe, +}; + +module_i2c_driver(exc3000_driver); + +MODULE_AUTHOR("Ahmet Inan "); +MODULE_DESCRIPTION("I2C connected EETI EXC3000 multiple touch controller driver"); +MODULE_LICENSE("GPL v2"); -- cgit v1.2.3 From 842ff286166e8512450573f6b6eb5e04e626a07f Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Wed, 25 Oct 2017 14:57:04 -0700 Subject: Input: add support for HiDeep touchscreen The HiDeep touchscreen device is a capacitive multi-touch controller mainly for multi-touch supported devices use. It use I2C interface for communication to IC and provide axis X, Y, Z locations for ten finger touch through input event interface to userspace. It support the Crimson and the Lime two type IC. They are different the number of channel supported and FW size. But the working protocol is same. Signed-off-by: Anthony Kim Acked-by: Rob Herring Signed-off-by: Dmitry Torokhov --- .../bindings/input/touchscreen/hideep.txt | 42 + .../devicetree/bindings/vendor-prefixes.txt | 1 + drivers/input/touchscreen/Kconfig | 11 + drivers/input/touchscreen/Makefile | 1 + drivers/input/touchscreen/hideep.c | 1120 ++++++++++++++++++++ 5 files changed, 1175 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/touchscreen/hideep.txt create mode 100644 drivers/input/touchscreen/hideep.c (limited to 'Documentation') diff --git a/Documentation/devicetree/bindings/input/touchscreen/hideep.txt b/Documentation/devicetree/bindings/input/touchscreen/hideep.txt new file mode 100644 index 000000000000..121d9b7c79a2 --- /dev/null +++ b/Documentation/devicetree/bindings/input/touchscreen/hideep.txt @@ -0,0 +1,42 @@ +* HiDeep Finger and Stylus touchscreen controller + +Required properties: +- compatible : must be "hideep,hideep-ts" +- reg : I2C slave address, (e.g. 0x6C). +- interrupt-parent : Interrupt controller to which the chip is connected. +- interrupts : Interrupt to which the chip is connected. + +Optional properties: +- vdd-supply : It is the controller supply for controlling + main voltage(3.3V) through the regulator. +- vid-supply : It is the controller supply for controlling + IO voltage(1.8V) through the regulator. +- reset-gpios : Define for reset gpio pin. + It is to use for reset IC. +- touchscreen-size-x : X axis size of touchscreen +- touchscreen-size-y : Y axis size of touchscreen +- linux,keycodes : Specifies an array of numeric keycode values to + be used for reporting button presses. The array can + contain up to 3 entries. + +Example: + +#include "dt-bindings/input/input.h" + +i2c@00000000 { + + /* ... */ + + touchscreen@6c { + compatible = "hideep,hideep-ts"; + reg = <0x6c>; + interrupt-parent = <&gpx1>; + interrupts = <2 IRQ_TYPE_LEVEL_LOW>; + vdd-supply = <&ldo15_reg>"; + vid-supply = <&ldo18_reg>; + reset-gpios = <&gpx1 5 0>; + touchscreen-size-x = <1080>; + touchscreen-size-y = <1920>; + linux,keycodes = , , ; + }; +}; diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt b/Documentation/devicetree/bindings/vendor-prefixes.txt index 1afd298eddd7..138895e99508 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.txt +++ b/Documentation/devicetree/bindings/vendor-prefixes.txt @@ -137,6 +137,7 @@ gw Gateworks Corporation hannstar HannStar Display Corporation haoyu Haoyu Microelectronic Co. Ltd. hardkernel Hardkernel Co., Ltd +hideep HiDeep Inc. himax Himax Technologies, Inc. hisilicon Hisilicon Limited. hit Hitachi Ltd. diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig index 26a66bc7b2df..dbe33fc3a286 100644 --- a/drivers/input/touchscreen/Kconfig +++ b/drivers/input/touchscreen/Kconfig @@ -354,6 +354,17 @@ config TOUCHSCREEN_GOODIX To compile this driver as a module, choose M here: the module will be called goodix. +config TOUCHSCREEN_HIDEEP + tristate "HiDeep Touch IC" + depends on I2C + help + Say Y here if you have a touchscreen using HiDeep. + + If unsure, say N. + + To compile this driver as a moudle, choose M here : the + module will be called hideep_ts. + config TOUCHSCREEN_ILI210X tristate "Ilitek ILI210X based touchscreen" depends on I2C diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile index 2fa458088e3b..d9a7024c8cd9 100644 --- a/drivers/input/touchscreen/Makefile +++ b/drivers/input/touchscreen/Makefile @@ -41,6 +41,7 @@ obj-$(CONFIG_TOUCHSCREEN_EGALAX_SERIAL) += egalax_ts_serial.o obj-$(CONFIG_TOUCHSCREEN_EXC3000) += exc3000.o obj-$(CONFIG_TOUCHSCREEN_FUJITSU) += fujitsu_ts.o obj-$(CONFIG_TOUCHSCREEN_GOODIX) += goodix.o +obj-$(CONFIG_TOUCHSCREEN_HIDEEP) += hideep.o obj-$(CONFIG_TOUCHSCREEN_ILI210X) += ili210x.o obj-$(CONFIG_TOUCHSCREEN_IMX6UL_TSC) += imx6ul_tsc.o obj-$(CONFIG_TOUCHSCREEN_INEXIO) += inexio.o diff --git a/drivers/input/touchscreen/hideep.c b/drivers/input/touchscreen/hideep.c new file mode 100644 index 000000000000..fc080a7c2e1f --- /dev/null +++ b/drivers/input/touchscreen/hideep.c @@ -0,0 +1,1120 @@ +/* + * Copyright (C) 2012-2017 Hideep, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 + * as published by the Free Software Foudation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define HIDEEP_TS_NAME "HiDeep Touchscreen" +#define HIDEEP_I2C_NAME "hideep_ts" + +#define HIDEEP_MT_MAX 10 +#define HIDEEP_KEY_MAX 3 + +/* count(2) + touch data(100) + key data(6) */ +#define HIDEEP_MAX_EVENT 108UL + +#define HIDEEP_TOUCH_EVENT_INDEX 2 +#define HIDEEP_KEY_EVENT_INDEX 102 + +/* Touch & key event */ +#define HIDEEP_EVENT_ADDR 0x240 + +/* command list */ +#define HIDEEP_RESET_CMD 0x9800 + +/* event bit */ +#define HIDEEP_MT_RELEASED BIT(4) +#define HIDEEP_KEY_PRESSED BIT(7) +#define HIDEEP_KEY_FIRST_PRESSED BIT(8) +#define HIDEEP_KEY_PRESSED_MASK (HIDEEP_KEY_PRESSED | \ + HIDEEP_KEY_FIRST_PRESSED) + +#define HIDEEP_KEY_IDX_MASK 0x0f + +/* For NVM */ +#define HIDEEP_YRAM_BASE 0x40000000 +#define HIDEEP_PERIPHERAL_BASE 0x50000000 +#define HIDEEP_ESI_BASE (HIDEEP_PERIPHERAL_BASE + 0x00000000) +#define HIDEEP_FLASH_BASE (HIDEEP_PERIPHERAL_BASE + 0x01000000) +#define HIDEEP_SYSCON_BASE (HIDEEP_PERIPHERAL_BASE + 0x02000000) + +#define HIDEEP_SYSCON_MOD_CON (HIDEEP_SYSCON_BASE + 0x0000) +#define HIDEEP_SYSCON_SPC_CON (HIDEEP_SYSCON_BASE + 0x0004) +#define HIDEEP_SYSCON_CLK_CON (HIDEEP_SYSCON_BASE + 0x0008) +#define HIDEEP_SYSCON_CLK_ENA (HIDEEP_SYSCON_BASE + 0x000C) +#define HIDEEP_SYSCON_RST_CON (HIDEEP_SYSCON_BASE + 0x0010) +#define HIDEEP_SYSCON_WDT_CON (HIDEEP_SYSCON_BASE + 0x0014) +#define HIDEEP_SYSCON_WDT_CNT (HIDEEP_SYSCON_BASE + 0x0018) +#define HIDEEP_SYSCON_PWR_CON (HIDEEP_SYSCON_BASE + 0x0020) +#define HIDEEP_SYSCON_PGM_ID (HIDEEP_SYSCON_BASE + 0x00F4) + +#define HIDEEP_FLASH_CON (HIDEEP_FLASH_BASE + 0x0000) +#define HIDEEP_FLASH_STA (HIDEEP_FLASH_BASE + 0x0004) +#define HIDEEP_FLASH_CFG (HIDEEP_FLASH_BASE + 0x0008) +#define HIDEEP_FLASH_TIM (HIDEEP_FLASH_BASE + 0x000C) +#define HIDEEP_FLASH_CACHE_CFG (HIDEEP_FLASH_BASE + 0x0010) +#define HIDEEP_FLASH_PIO_SIG (HIDEEP_FLASH_BASE + 0x400000) + +#define HIDEEP_ESI_TX_INVALID (HIDEEP_ESI_BASE + 0x0008) + +#define HIDEEP_PERASE 0x00040000 +#define HIDEEP_WRONLY 0x00100000 + +#define HIDEEP_NVM_MASK_OFS 0x0000000C +#define HIDEEP_NVM_DEFAULT_PAGE 0 +#define HIDEEP_NVM_SFR_WPAGE 1 +#define HIDEEP_NVM_SFR_RPAGE 2 + +#define HIDEEP_PIO_SIG 0x00400000 +#define HIDEEP_PROT_MODE 0x03400000 + +#define HIDEEP_NVM_PAGE_SIZE 128 + +#define HIDEEP_DWZ_INFO 0x000002C0 + +struct hideep_event { + __le16 x; + __le16 y; + __le16 z; + u8 w; + u8 flag; + u8 type; + u8 index; +}; + +struct dwz_info { + __be32 code_start; + u8 code_crc[12]; + + __be32 c_code_start; + __be16 gen_ver; + __be16 c_code_len; + + __be32 vr_start; + __be16 rsv0; + __be16 vr_len; + + __be32 ft_start; + __be16 vr_version; + __be16 ft_len; + + __be16 core_ver; + __be16 boot_ver; + + __be16 release_ver; + __be16 custom_ver; + + u8 factory_id; + u8 panel_type; + u8 model_name[6]; + + __be16 extra_option; + __be16 product_code; + + __be16 vendor_id; + __be16 product_id; +}; + +struct pgm_packet { + struct { + u8 unused[3]; + u8 len; + __be32 addr; + } header; + __be32 payload[HIDEEP_NVM_PAGE_SIZE / sizeof(__be32)]; +}; + +#define HIDEEP_XFER_BUF_SIZE sizeof(struct pgm_packet) + +struct hideep_ts { + struct i2c_client *client; + struct input_dev *input_dev; + struct regmap *reg; + + struct touchscreen_properties prop; + + struct gpio_desc *reset_gpio; + + struct regulator *vcc_vdd; + struct regulator *vcc_vid; + + struct mutex dev_mutex; + + u32 tch_count; + u32 lpm_count; + + /* + * Data buffer to read packet from the device (contacts and key + * states). We align it on double-word boundary to keep word-sized + * fields in contact data and double-word-sized fields in program + * packet aligned. + */ + u8 xfer_buf[HIDEEP_XFER_BUF_SIZE] __aligned(4); + + int key_num; + u32 key_codes[HIDEEP_KEY_MAX]; + + struct dwz_info dwz_info; + + unsigned int fw_size; + u32 nvm_mask; +}; + +static int hideep_pgm_w_mem(struct hideep_ts *ts, u32 addr, + const __be32 *data, size_t count) +{ + struct pgm_packet *packet = (void *)ts->xfer_buf; + size_t len = count * sizeof(*data); + struct i2c_msg msg = { + .addr = ts->client->addr, + .len = len + sizeof(packet->header.len) + + sizeof(packet->header.addr), + .buf = &packet->header.len, + }; + int ret; + + if (len > HIDEEP_NVM_PAGE_SIZE) + return -EINVAL; + + packet->header.len = 0x80 | (count - 1); + packet->header.addr = cpu_to_be32(addr); + memcpy(packet->payload, data, len); + + ret = i2c_transfer(ts->client->adapter, &msg, 1); + if (ret != 1) + return ret < 0 ? ret : -EIO; + + return 0; +} + +static int hideep_pgm_r_mem(struct hideep_ts *ts, u32 addr, + __be32 *data, size_t count) +{ + struct pgm_packet *packet = (void *)ts->xfer_buf; + size_t len = count * sizeof(*data); + struct i2c_msg msg[] = { + { + .addr = ts->client->addr, + .len = sizeof(packet->header.len) + + sizeof(packet->header.addr), + .buf = &packet->header.len, + }, + { + .addr = ts->client->addr, + .flags = I2C_M_RD, + .len = len, + .buf = (u8 *)data, + }, + }; + int ret; + + if (len > HIDEEP_NVM_PAGE_SIZE) + return -EINVAL; + + packet->header.len = count - 1; + packet->header.addr = cpu_to_be32(addr); + + ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); + if (ret != ARRAY_SIZE(msg)) + return ret < 0 ? ret : -EIO; + + return 0; +} + +static int hideep_pgm_r_reg(struct hideep_ts *ts, u32 addr, u32 *val) +{ + __be32 data; + int error; + + error = hideep_pgm_r_mem(ts, addr, &data, 1); + if (error) { + dev_err(&ts->client->dev, + "read of register %#08x failed: %d\n", + addr, error); + return error; + } + + *val = be32_to_cpu(data); + return 0; +} + +static int hideep_pgm_w_reg(struct hideep_ts *ts, u32 addr, u32 val) +{ + __be32 data = cpu_to_be32(val); + int error; + + error = hideep_pgm_w_mem(ts, addr, &data, 1); + if (error) { + dev_err(&ts->client->dev, + "write to register %#08x (%#08x) failed: %d\n", + addr, val, error); + return error; + } + + return 0; +} + +#define SW_RESET_IN_PGM(clk) \ +{ \ + hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CNT, (clk)); \ + hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CON, 0x03); \ + hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CON, 0x01); \ +} + +#define SET_FLASH_PIO(ce) \ + hideep_pgm_w_reg(ts, HIDEEP_FLASH_CON, \ + 0x01 | ((ce) << 1)) + +#define SET_PIO_SIG(x, y) \ + hideep_pgm_w_reg(ts, HIDEEP_FLASH_PIO_SIG + (x), (y)) + +#define SET_FLASH_HWCONTROL() \ + hideep_pgm_w_reg(ts, HIDEEP_FLASH_CON, 0x00) + +#define NVM_W_SFR(x, y) \ +{ \ + SET_FLASH_PIO(1); \ + SET_PIO_SIG(x, y); \ + SET_FLASH_PIO(0); \ +} + +static void hideep_pgm_set(struct hideep_ts *ts) +{ + hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CON, 0x00); + hideep_pgm_w_reg(ts, HIDEEP_SYSCON_SPC_CON, 0x00); + hideep_pgm_w_reg(ts, HIDEEP_SYSCON_CLK_ENA, 0xFF); + hideep_pgm_w_reg(ts, HIDEEP_SYSCON_CLK_CON, 0x01); + hideep_pgm_w_reg(ts, HIDEEP_SYSCON_PWR_CON, 0x01); + hideep_pgm_w_reg(ts, HIDEEP_FLASH_TIM, 0x03); + hideep_pgm_w_reg(ts, HIDEEP_FLASH_CACHE_CFG, 0x00); +} + +static int hideep_pgm_get_pattern(struct hideep_ts *ts, u32 *pattern) +{ + u16 p1 = 0xAF39; + u16 p2 = 0xDF9D; + int error; + + error = regmap_bulk_write(ts->reg, p1, &p2, 1); + if (error) { + dev_err(&ts->client->dev, + "%s: regmap_bulk_write() failed with %d\n", + __func__, error); + return error; + } + + usleep_range(1000, 1100); + + /* flush invalid Tx load register */ + error = hideep_pgm_w_reg(ts, HIDEEP_ESI_TX_INVALID, 0x01); + if (error) + return error; + + error = hideep_pgm_r_reg(ts, HIDEEP_SYSCON_PGM_ID, pattern); + if (error) + return error; + + return 0; +} + +static int hideep_enter_pgm(struct hideep_ts *ts) +{ + int retry_count = 10; + u32 pattern; + int error; + + while (retry_count--) { + error = hideep_pgm_get_pattern(ts, &pattern); + if (error) { + dev_err(&ts->client->dev, + "hideep_pgm_get_pattern failed: %d\n", error); + } else if (pattern != 0x39AF9DDF) { + dev_err(&ts->client->dev, "%s: bad pattern: %#08x\n", + __func__, pattern); + } else { + dev_dbg(&ts->client->dev, "found magic code"); + + hideep_pgm_set(ts); + usleep_range(1000, 1100); + + return 0; + } + } + + dev_err(&ts->client->dev, "failed to enter pgm mode\n"); + SW_RESET_IN_PGM(1000); + return -EIO; +} + +static void hideep_nvm_unlock(struct hideep_ts *ts) +{ + u32 unmask_code; + + hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_SFR_RPAGE); + hideep_pgm_r_reg(ts, 0x0000000C, &unmask_code); + hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_DEFAULT_PAGE); + + /* make it unprotected code */ + unmask_code &= ~HIDEEP_PROT_MODE; + + /* compare unmask code */ + if (unmask_code != ts->nvm_mask) + dev_warn(&ts->client->dev, + "read mask code different %#08x vs %#08x", + unmask_code, ts->nvm_mask); + + hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_SFR_WPAGE); + SET_FLASH_PIO(0); + + NVM_W_SFR(HIDEEP_NVM_MASK_OFS, ts->nvm_mask); + SET_FLASH_HWCONTROL(); + hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_DEFAULT_PAGE); +} + +static int hideep_check_status(struct hideep_ts *ts) +{ + int time_out = 100; + int status; + int error; + + while (time_out--) { + error = hideep_pgm_r_reg(ts, HIDEEP_FLASH_STA, &status); + if (!error && status) + return 0; + + usleep_range(1000, 1100); + } + + return -ETIMEDOUT; +} + +static int hideep_program_page(struct hideep_ts *ts, u32 addr, + const __be32 *ucode, size_t xfer_count) +{ + u32 val; + int error; + + error = hideep_check_status(ts); + if (error) + return -EBUSY; + + addr &= ~(HIDEEP_NVM_PAGE_SIZE - 1); + + SET_FLASH_PIO(0); + SET_FLASH_PIO(1); + + /* erase page */ + SET_PIO_SIG(HIDEEP_PERASE | addr, 0xFFFFFFFF); + + SET_FLASH_PIO(0); + + error = hideep_check_status(ts); + if (error) + return -EBUSY; + + /* write page */ + SET_FLASH_PIO(1); + + val = be32_to_cpu(ucode[0]); + SET_PIO_SIG(HIDEEP_WRONLY | addr, val); + + hideep_pgm_w_mem(ts, HIDEEP_FLASH_PIO_SIG | HIDEEP_WRONLY, + ucode, xfer_count); + + val = be32_to_cpu(ucode[xfer_count - 1]); + SET_PIO_SIG(124, val); + + SET_FLASH_PIO(0); + + usleep_range(1000, 1100); + + error = hideep_check_status(ts); + if (error) + return -EBUSY; + + SET_FLASH_HWCONTROL(); + + return 0; +} + +static int hideep_program_nvm(struct hideep_ts *ts, + const __be32 *ucode, size_t ucode_len) +{ + struct pgm_packet *packet_r = (void *)ts->xfer_buf; + __be32 *current_ucode = packet_r->payload; + size_t xfer_len; + size_t xfer_count; + u32 addr = 0; + int error; + + hideep_nvm_unlock(ts); + + while (ucode_len > 0) { + xfer_len = min_t(size_t, ucode_len, HIDEEP_NVM_PAGE_SIZE); + xfer_count = xfer_len / sizeof(*ucode); + + error = hideep_pgm_r_mem(ts, 0x00000000 + addr, + current_ucode, xfer_count); + if (error) { + dev_err(&ts->client->dev, + "%s: failed to read page at offset %#08x: %d\n", + __func__, addr, error); + return error; + } + + /* See if the page needs updating */ + if (memcmp(ucode, current_ucode, xfer_len)) { + error = hideep_program_page(ts, addr, + ucode, xfer_count); + if (error) { + dev_err(&ts->client->dev, + "%s: iwrite failure @%#08x: %d\n", + __func__, addr, error); + return error; + } + + usleep_range(1000, 1100); + } + + ucode += xfer_count; + addr += xfer_len; + ucode_len -= xfer_len; + } + + return 0; +} + +static int hideep_verify_nvm(struct hideep_ts *ts, + const __be32 *ucode, size_t ucode_len) +{ + struct pgm_packet *packet_r = (void *)ts->xfer_buf; + __be32 *current_ucode = packet_r->payload; + size_t xfer_len; + size_t xfer_count; + u32 addr = 0; + int i; + int error; + + while (ucode_len > 0) { + xfer_len = min_t(size_t, ucode_len, HIDEEP_NVM_PAGE_SIZE); + xfer_count = xfer_len / sizeof(*ucode); + + error = hideep_pgm_r_mem(ts, 0x00000000 + addr, + current_ucode, xfer_count); + if (error) { + dev_err(&ts->client->dev, + "%s: failed to read page at offset %#08x: %d\n", + __func__, addr, error); + return error; + } + + if (memcmp(ucode, current_ucode, xfer_len)) { + const u8 *ucode_bytes = (const u8 *)ucode; + const u8 *current_bytes = (const u8 *)current_ucode; + + for (i = 0; i < xfer_len; i++) + if (ucode_bytes[i] != current_bytes[i]) + dev_err(&ts->client->dev, + "%s: mismatch @%#08x: (%#02x vs %#02x)\n", + __func__, addr + i, + ucode_bytes[i], + current_bytes[i]); + + return -EIO; + } + + ucode += xfer_count; + addr += xfer_len; + ucode_len -= xfer_len; + } + + return 0; +} + +static int hideep_load_dwz(struct hideep_ts *ts) +{ + u16 product_code; + int error; + + error = hideep_enter_pgm(ts); + if (error) + return error; + + msleep(50); + + error = hideep_pgm_r_mem(ts, HIDEEP_DWZ_INFO, + (void *)&ts->dwz_info, + sizeof(ts->dwz_info) / sizeof(__be32)); + + SW_RESET_IN_PGM(10); + msleep(50); + + if (error) { + dev_err(&ts->client->dev, + "failed to fetch DWZ data: %d\n", error); + return error; + } + + product_code = be16_to_cpu(ts->dwz_info.product_code); + + switch (product_code & 0xF0) { + case 0x40: + dev_dbg(&ts->client->dev, "used crimson IC"); + ts->fw_size = 1024 * 48; + ts->nvm_mask = 0x00310000; + break; + case 0x60: + dev_dbg(&ts->client->dev, "used lime IC"); + ts->fw_size = 1024 * 64; + ts->nvm_mask = 0x0030027B; + break; + default: + dev_err(&ts->client->dev, "product code is wrong: %#04x", + product_code); + return -EINVAL; + } + + dev_dbg(&ts->client->dev, "firmware release version: %#04x", + be16_to_cpu(ts->dwz_info.release_ver)); + + return 0; +} + +static int hideep_flash_firmware(struct hideep_ts *ts, + const __be32 *ucode, size_t ucode_len) +{ + int retry_cnt = 3; + int error; + + while (retry_cnt--) { + error = hideep_program_nvm(ts, ucode, ucode_len); + if (!error) { + error = hideep_verify_nvm(ts, ucode, ucode_len); + if (!error) + return 0; + } + } + + return error; +} + +static int hideep_update_firmware(struct hideep_ts *ts, + const __be32 *ucode, size_t ucode_len) +{ + int error, error2; + + dev_dbg(&ts->client->dev, "starting firmware update"); + + /* enter program mode */ + error = hideep_enter_pgm(ts); + if (error) + return error; + + error = hideep_flash_firmware(ts, ucode, ucode_len); + if (error) + dev_err(&ts->client->dev, + "firmware update failed: %d\n", error); + else + dev_dbg(&ts->client->dev, "firmware updated successfully\n"); + + SW_RESET_IN_PGM(1000); + + error2 = hideep_load_dwz(ts); + if (error2) + dev_err(&ts->client->dev, + "failed to load dwz after firmware update: %d\n", + error2); + + return error ?: error2; +} + +static int hideep_power_on(struct hideep_ts *ts) +{ + int error = 0; + + error = regulator_enable(ts->vcc_vdd); + if (error) + dev_err(&ts->client->dev, + "failed to enable 'vdd' regulator: %d", error); + + usleep_range(999, 1000); + + error = regulator_enable(ts->vcc_vid); + if (error) + dev_err(&ts->client->dev, + "failed to enable 'vcc_vid' regulator: %d", + error); + + msleep(30); + + if (ts->reset_gpio) { + gpiod_set_value_cansleep(ts->reset_gpio, 0); + } else { + error = regmap_write(ts->reg, HIDEEP_RESET_CMD, 0x01); + if (error) + dev_err(&ts->client->dev, + "failed to send 'reset' command: %d\n", error); + } + + msleep(50); + + return error; +} + +static void hideep_power_off(void *data) +{ + struct hideep_ts *ts = data; + + if (ts->reset_gpio) + gpiod_set_value(ts->reset_gpio, 1); + + regulator_disable(ts->vcc_vid); + regulator_disable(ts->vcc_vdd); +} + +#define __GET_MT_TOOL_TYPE(type) ((type) == 0x01 ? MT_TOOL_FINGER : MT_TOOL_PEN) + +static void hideep_report_slot(struct input_dev *input, + const struct hideep_event *event) +{ + input_mt_slot(input, event->index & 0x0f); + input_mt_report_slot_state(input, + __GET_MT_TOOL_TYPE(event->type), + !(event->flag & HIDEEP_MT_RELEASED)); + if (!(event->flag & HIDEEP_MT_RELEASED)) { + input_report_abs(input, ABS_MT_POSITION_X, + le16_to_cpup(&event->x)); + input_report_abs(input, ABS_MT_POSITION_Y, + le16_to_cpup(&event->y)); + input_report_abs(input, ABS_MT_PRESSURE, + le16_to_cpup(&event->z)); + input_report_abs(input, ABS_MT_TOUCH_MAJOR, event->w); + } +} + +static void hideep_parse_and_report(struct hideep_ts *ts) +{ + const struct hideep_event *events = + (void *)&ts->xfer_buf[HIDEEP_TOUCH_EVENT_INDEX]; + const u8 *keys = &ts->xfer_buf[HIDEEP_KEY_EVENT_INDEX]; + int touch_count = ts->xfer_buf[0]; + int key_count = ts->xfer_buf[1] & 0x0f; + int lpm_count = ts->xfer_buf[1] & 0xf0; + int i; + + /* get touch event count */ + dev_dbg(&ts->client->dev, "mt = %d, key = %d, lpm = %02x", + touch_count, key_count, lpm_count); + + touch_count = min(touch_count, HIDEEP_MT_MAX); + for (i = 0; i < touch_count; i++) + hideep_report_slot(ts->input_dev, events + i); + + key_count = min(key_count, HIDEEP_KEY_MAX); + for (i = 0; i < key_count; i++) { + u8 key_data = keys[i * 2]; + + input_report_key(ts->input_dev, + ts->key_codes[key_data & HIDEEP_KEY_IDX_MASK], + key_data & HIDEEP_KEY_PRESSED_MASK); + } + + input_mt_sync_frame(ts->input_dev); + input_sync(ts->input_dev); +} + +static irqreturn_t hideep_irq(int irq, void *handle) +{ + struct hideep_ts *ts = handle; + int error; + + BUILD_BUG_ON(HIDEEP_MAX_EVENT > HIDEEP_XFER_BUF_SIZE); + + error = regmap_bulk_read(ts->reg, HIDEEP_EVENT_ADDR, + ts->xfer_buf, HIDEEP_MAX_EVENT / 2); + if (error) { + dev_err(&ts->client->dev, "failed to read events: %d\n", error); + goto out; + } + + hideep_parse_and_report(ts); + +out: + return IRQ_HANDLED; +} + +static int hideep_get_axis_info(struct hideep_ts *ts) +{ + __le16 val[2]; + int error; + + error = regmap_bulk_read(ts->reg, 0x28, val, ARRAY_SIZE(val)); + if (error) + return error; + + ts->prop.max_x = le16_to_cpup(val); + ts->prop.max_y = le16_to_cpup(val + 1); + + dev_dbg(&ts->client->dev, "X: %d, Y: %d", + ts->prop.max_x, ts->prop.max_y); + + return 0; +} + +static int hideep_init_input(struct hideep_ts *ts) +{ + struct device *dev = &ts->client->dev; + int i; + int error; + + ts->input_dev = devm_input_allocate_device(dev); + if (!ts->input_dev) { + dev_err(dev, "failed to allocate input device\n"); + return -ENOMEM; + } + + ts->input_dev->name = HIDEEP_TS_NAME; + ts->input_dev->id.bustype = BUS_I2C; + input_set_drvdata(ts->input_dev, ts); + + input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X); + input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y); + input_set_abs_params(ts->input_dev, ABS_MT_PRESSURE, 0, 65535, 0, 0); + input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); + input_set_abs_params(ts->input_dev, ABS_MT_TOOL_TYPE, + 0, MT_TOOL_MAX, 0, 0); + touchscreen_parse_properties(ts->input_dev, true, &ts->prop); + + if (ts->prop.max_x == 0 || ts->prop.max_y == 0) { + error = hideep_get_axis_info(ts); + if (error) + return error; + } + + error = input_mt_init_slots(ts->input_dev, HIDEEP_MT_MAX, + INPUT_MT_DIRECT); + if (error) + return error; + + ts->key_num = device_property_read_u32_array(dev, "linux,keycodes", + NULL, 0); + if (ts->key_num > HIDEEP_KEY_MAX) { + dev_err(dev, "too many keys defined: %d\n", + ts->key_num); + return -EINVAL; + } + + if (ts->key_num <= 0) { + dev_dbg(dev, + "missing or malformed 'linux,keycodes' property\n"); + } else { + error = device_property_read_u32_array(dev, "linux,keycodes", + ts->key_codes, + ts->key_num); + if (error) { + dev_dbg(dev, "failed to read keymap: %d", error); + return error; + } + + if (ts->key_num) { + ts->input_dev->keycode = ts->key_codes; + ts->input_dev->keycodesize = sizeof(ts->key_codes[0]); + ts->input_dev->keycodemax = ts->key_num; + + for (i = 0; i < ts->key_num; i++) + input_set_capability(ts->input_dev, EV_KEY, + ts->key_codes[i]); + } + } + + error = input_register_device(ts->input_dev); + if (error) { + dev_err(dev, "failed to register input device: %d", error); + return error; + } + + return 0; +} + +static ssize_t hideep_update_fw(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct hideep_ts *ts = i2c_get_clientdata(client); + const struct firmware *fw_entry; + char *fw_name; + int mode; + int error; + + error = kstrtoint(buf, 0, &mode); + if (error) + return error; + + fw_name = kasprintf(GFP_KERNEL, "hideep_ts_%04x.bin", + be16_to_cpu(ts->dwz_info.product_id)); + if (!fw_name) + return -ENOMEM; + + error = request_firmware(&fw_entry, fw_name, dev); + if (error) { + dev_err(dev, "failed to request firmware %s: %d", + fw_name, error); + goto out_free_fw_name; + } + + if (fw_entry->size % sizeof(__be32)) { + dev_err(dev, "invalid firmware size %zu\n", fw_entry->size); + error = -EINVAL; + goto out_release_fw; + } + + if (fw_entry->size > ts->fw_size) { + dev_err(dev, "fw size (%zu) is too big (memory size %d)\n", + fw_entry->size, ts->fw_size); + error = -EFBIG; + goto out_release_fw; + } + + mutex_lock(&ts->dev_mutex); + disable_irq(client->irq); + + error = hideep_update_firmware(ts, (const __be32 *)fw_entry->data, + fw_entry->size); + + enable_irq(client->irq); + mutex_unlock(&ts->dev_mutex); + +out_release_fw: + release_firmware(fw_entry); +out_free_fw_name: + kfree(fw_name); + + return error ?: count; +} + +static ssize_t hideep_fw_version_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct i2c_client *client = to_i2c_client(dev); + struct hideep_ts *ts = i2c_get_clientdata(client); + ssize_t len; + + mutex_lock(&ts->dev_mutex); + len = scnprintf(buf, PAGE_SIZE, "%04x\n", + be16_to_cpu(ts->dwz_info.release_ver)); + mutex_unlock(&ts->dev_mutex); + + return len; +} + +static ssize_t hideep_product_id_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct i2c_client *client = to_i2c_client(dev); + struct hideep_ts *ts = i2c_get_clientdata(client); + ssize_t len; + + mutex_lock(&ts->dev_mutex); + len = scnprintf(buf, PAGE_SIZE, "%04x\n", + be16_to_cpu(ts->dwz_info.product_id)); + mutex_unlock(&ts->dev_mutex); + + return len; +} + +static DEVICE_ATTR(version, 0664, hideep_fw_version_show, NULL); +static DEVICE_ATTR(product_id, 0664, hideep_product_id_show, NULL); +static DEVICE_ATTR(update_fw, 0664, NULL, hideep_update_fw); + +static struct attribute *hideep_ts_sysfs_entries[] = { + &dev_attr_version.attr, + &dev_attr_product_id.attr, + &dev_attr_update_fw.attr, + NULL, +}; + +static const struct attribute_group hideep_ts_attr_group = { + .attrs = hideep_ts_sysfs_entries, +}; + +static int __maybe_unused hideep_suspend(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct hideep_ts *ts = i2c_get_clientdata(client); + + disable_irq(client->irq); + hideep_power_off(ts); + + return 0; +} + +static int __maybe_unused hideep_resume(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct hideep_ts *ts = i2c_get_clientdata(client); + int error; + + error = hideep_power_on(ts); + if (error) { + dev_err(&client->dev, "power on failed"); + return error; + } + + enable_irq(client->irq); + + return 0; +} + +static SIMPLE_DEV_PM_OPS(hideep_pm_ops, hideep_suspend, hideep_resume); + +static const struct regmap_config hideep_regmap_config = { + .reg_bits = 16, + .reg_format_endian = REGMAP_ENDIAN_LITTLE, + .val_bits = 16, + .val_format_endian = REGMAP_ENDIAN_LITTLE, + .max_register = 0xffff, +}; + +static int hideep_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct hideep_ts *ts; + int error; + + /* check i2c bus */ + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { + dev_err(&client->dev, "check i2c device error"); + return -ENODEV; + } + + if (client->irq <= 0) { + dev_err(&client->dev, "missing irq: %d\n", client->irq); + return -EINVAL; + } + + ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); + if (!ts) + return -ENOMEM; + + ts->client = client; + i2c_set_clientdata(client, ts); + mutex_init(&ts->dev_mutex); + + ts->reg = devm_regmap_init_i2c(client, &hideep_regmap_config); + if (IS_ERR(ts->reg)) { + error = PTR_ERR(ts->reg); + dev_err(&client->dev, + "failed to initialize regmap: %d\n", error); + return error; + } + + ts->vcc_vdd = devm_regulator_get(&client->dev, "vdd"); + if (IS_ERR(ts->vcc_vdd)) + return PTR_ERR(ts->vcc_vdd); + + ts->vcc_vid = devm_regulator_get(&client->dev, "vid"); + if (IS_ERR(ts->vcc_vid)) + return PTR_ERR(ts->vcc_vid); + + ts->reset_gpio = devm_gpiod_get_optional(&client->dev, + "reset", GPIOD_OUT_HIGH); + if (IS_ERR(ts->reset_gpio)) + return PTR_ERR(ts->reset_gpio); + + error = hideep_power_on(ts); + if (error) { + dev_err(&client->dev, "power on failed: %d\n", error); + return error; + } + + error = devm_add_action_or_reset(&client->dev, hideep_power_off, ts); + if (error) + return error; + + error = hideep_load_dwz(ts); + if (error) { + dev_err(&client->dev, "failed to load dwz: %d", error); + return error; + } + + error = hideep_init_input(ts); + if (error) + return error; + + error = devm_request_threaded_irq(&client->dev, client->irq, + NULL, hideep_irq, IRQF_ONESHOT, + client->name, ts); + if (error) { + dev_err(&client->dev, "failed to request irq %d: %d\n", + client->irq, error); + return error; + } + + error = devm_device_add_group(&client->dev, &hideep_ts_attr_group); + if (error) { + dev_err(&client->dev, + "failed to add sysfs attributes: %d\n", error); + return error; + } + + return 0; +} + +static const struct i2c_device_id hideep_i2c_id[] = { + { HIDEEP_I2C_NAME, 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, hideep_i2c_id); + +#ifdef CONFIG_ACPI +static const struct acpi_device_id hideep_acpi_id[] = { + { "HIDP0001", 0 }, + { } +}; +MODULE_DEVICE_TABLE(acpi, hideep_acpi_id); +#endif + +#ifdef CONFIG_OF +static const struct of_device_id hideep_match_table[] = { + { .compatible = "hideep,hideep-ts" }, + { } +}; +MODULE_DEVICE_TABLE(of, hideep_match_table); +#endif + +static struct i2c_driver hideep_driver = { + .driver = { + .name = HIDEEP_I2C_NAME, + .of_match_table = of_match_ptr(hideep_match_table), + .acpi_match_table = ACPI_PTR(hideep_acpi_id), + .pm = &hideep_pm_ops, + }, + .id_table = hideep_i2c_id, + .probe = hideep_probe, +}; + +module_i2c_driver(hideep_driver); + +MODULE_DESCRIPTION("Driver for HiDeep Touchscreen Controller"); +MODULE_AUTHOR("anthony.kim@hideep.com"); +MODULE_LICENSE("GPL v2"); -- cgit v1.2.3 From 0145a7141e597e14c60f7561add76bea874768b2 Mon Sep 17 00:00:00 2001 From: Andi Shyti Date: Thu, 9 Nov 2017 23:10:06 -0800 Subject: Input: add support for the Samsung S6SY761 touchscreen The S6SY761 touchscreen is a capicitive multi-touch controller for mobile use. It's connected with i2c at the address 0x48. This commit provides a basic version of the driver which can handle only initialization, touch events and power states. The controller is controlled by a firmware which, in the version I currently have, doesn't provide all the possible functionalities mentioned in the datasheet. Signed-off-by: Andi Shyti Acked-by: Rob Herring Signed-off-by: Dmitry Torokhov --- .../bindings/input/touchscreen/samsung,s6sy761.txt | 34 ++ drivers/input/touchscreen/Kconfig | 11 + drivers/input/touchscreen/Makefile | 1 + drivers/input/touchscreen/s6sy761.c | 559 +++++++++++++++++++++ 4 files changed, 605 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/touchscreen/samsung,s6sy761.txt create mode 100644 drivers/input/touchscreen/s6sy761.c (limited to 'Documentation') diff --git a/Documentation/devicetree/bindings/input/touchscreen/samsung,s6sy761.txt b/Documentation/devicetree/bindings/input/touchscreen/samsung,s6sy761.txt new file mode 100644 index 000000000000..d9b7c2ff611e --- /dev/null +++ b/Documentation/devicetree/bindings/input/touchscreen/samsung,s6sy761.txt @@ -0,0 +1,34 @@ +* Samsung S6SY761 touchscreen controller + +Required properties: +- compatible : must be "samsung,s6sy761" +- reg : I2C slave address, (e.g. 0x48) +- interrupt-parent : the phandle to the interrupt controller which provides + the interrupt +- interrupts : interrupt specification +- avdd-supply : analogic power supply +- vdd-supply : power supply + +Optional properties: +- touchscreen-size-x : see touchscreen.txt. This property is embedded in the + device. If defined it forces a different x resolution. +- touchscreen-size-y : see touchscreen.txt. This property is embedded in the + device. If defined it forces a different y resolution. + +Example: + +i2c@00000000 { + + /* ... */ + + touchscreen@48 { + compatible = "samsung,s6sy761"; + reg = <0x48>; + interrupt-parent = <&gpa1>; + interrupts = <1 IRQ_TYPE_NONE>; + avdd-supply = <&ldo30_reg>; + vdd-supply = <&ldo31_reg>; + touchscreen-size-x = <4096>; + touchscreen-size-y = <4096>; + }; +}; diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig index dbe33fc3a286..0cfdb7cb610e 100644 --- a/drivers/input/touchscreen/Kconfig +++ b/drivers/input/touchscreen/Kconfig @@ -404,6 +404,17 @@ config TOUCHSCREEN_S3C2410 To compile this driver as a module, choose M here: the module will be called s3c2410_ts. +config TOUCHSCREEN_S6SY761 + tristate "Samsung S6SY761 Touchscreen driver" + depends on I2C + help + Say Y if you have the Samsung S6SY761 driver + + If unsure, say N + + To compile this driver as module, choose M here: the + module will be called s6sy761. + config TOUCHSCREEN_GUNZE tristate "Gunze AHL-51S touchscreen" select SERIO diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile index d9a7024c8cd9..d2a2b3b7af27 100644 --- a/drivers/input/touchscreen/Makefile +++ b/drivers/input/touchscreen/Makefile @@ -67,6 +67,7 @@ obj-$(CONFIG_TOUCHSCREEN_PENMOUNT) += penmount.o obj-$(CONFIG_TOUCHSCREEN_PIXCIR) += pixcir_i2c_ts.o obj-$(CONFIG_TOUCHSCREEN_RM_TS) += raydium_i2c_ts.o obj-$(CONFIG_TOUCHSCREEN_S3C2410) += s3c2410_ts.o +obj-$(CONFIG_TOUCHSCREEN_S6SY761) += s6sy761.o obj-$(CONFIG_TOUCHSCREEN_SILEAD) += silead.o obj-$(CONFIG_TOUCHSCREEN_SIS_I2C) += sis_i2c.o obj-$(CONFIG_TOUCHSCREEN_ST1232) += st1232.o diff --git a/drivers/input/touchscreen/s6sy761.c b/drivers/input/touchscreen/s6sy761.c new file mode 100644 index 000000000000..26b1cb8a88ec --- /dev/null +++ b/drivers/input/touchscreen/s6sy761.c @@ -0,0 +1,559 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * Author: Andi Shyti + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * Samsung S6SY761 Touchscreen device driver + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* commands */ +#define S6SY761_SENSE_ON 0x10 +#define S6SY761_SENSE_OFF 0x11 +#define S6SY761_TOUCH_FUNCTION 0x30 /* R/W for get/set */ +#define S6SY761_FIRMWARE_INTEGRITY 0x21 +#define S6SY761_PANEL_INFO 0x23 +#define S6SY761_DEVICE_ID 0x52 +#define S6SY761_BOOT_STATUS 0x55 +#define S6SY761_READ_ONE_EVENT 0x60 +#define S6SY761_READ_ALL_EVENT 0x61 +#define S6SY761_CLEAR_EVENT_STACK 0x62 +#define S6SY761_APPLICATION_MODE 0xe4 + +/* events */ +#define S6SY761_EVENT_INFO 0x02 +#define S6SY761_EVENT_VENDOR_INFO 0x07 + +/* info */ +#define S6SY761_INFO_BOOT_COMPLETE 0x00 + +/* firmware status */ +#define S6SY761_FW_OK 0x80 + +/* + * the functionalities are put as a reference + * as in the device I am using none of them + * works therefore not used in this driver yet. + */ +/* touchscreen functionalities */ +#define S6SY761_MASK_TOUCH BIT(0) +#define S6SY761_MASK_HOVER BIT(1) +#define S6SY761_MASK_COVER BIT(2) +#define S6SY761_MASK_GLOVE BIT(3) +#define S6SY761_MASK_STYLUS BIT(4) +#define S6SY761_MASK_PALM BIT(5) +#define S6SY761_MASK_WET BIT(6) +#define S6SY761_MASK_PROXIMITY BIT(7) + +/* boot status (BS) */ +#define S6SY761_BS_BOOT_LOADER 0x10 +#define S6SY761_BS_APPLICATION 0x20 + +/* event id */ +#define S6SY761_EVENT_ID_COORDINATE 0x00 +#define S6SY761_EVENT_ID_STATUS 0x01 + +/* event register masks */ +#define S6SY761_MASK_TOUCH_STATE 0xc0 /* byte 0 */ +#define S6SY761_MASK_TID 0x3c +#define S6SY761_MASK_EID 0x03 +#define S6SY761_MASK_X 0xf0 /* byte 3 */ +#define S6SY761_MASK_Y 0x0f +#define S6SY761_MASK_Z 0x3f /* byte 6 */ +#define S6SY761_MASK_LEFT_EVENTS 0x3f /* byte 7 */ +#define S6SY761_MASK_TOUCH_TYPE 0xc0 /* MSB in byte 6, LSB in byte 7 */ + +/* event touch state values */ +#define S6SY761_TS_NONE 0x00 +#define S6SY761_TS_PRESS 0x01 +#define S6SY761_TS_MOVE 0x02 +#define S6SY761_TS_RELEASE 0x03 + +/* application modes */ +#define S6SY761_APP_NORMAL 0x0 +#define S6SY761_APP_LOW_POWER 0x1 +#define S6SY761_APP_TEST 0x2 +#define S6SY761_APP_FLASH 0x3 +#define S6SY761_APP_SLEEP 0x4 + +#define S6SY761_EVENT_SIZE 8 +#define S6SY761_EVENT_COUNT 32 +#define S6SY761_DEVID_SIZE 3 +#define S6SY761_PANEL_ID_SIZE 11 +#define S6SY761_TS_STATUS_SIZE 5 +#define S6SY761_MAX_FINGERS 10 + +#define S6SY761_DEV_NAME "s6sy761" + +enum s6sy761_regulators { + S6SY761_REGULATOR_VDD, + S6SY761_REGULATOR_AVDD, +}; + +struct s6sy761_data { + struct i2c_client *client; + struct regulator_bulk_data regulators[2]; + struct input_dev *input; + struct touchscreen_properties prop; + + u8 data[S6SY761_EVENT_SIZE * S6SY761_EVENT_COUNT]; + + u16 devid; + u8 tx_channel; +}; + +/* + * We can't simply use i2c_smbus_read_i2c_block_data because we + * need to read more than 255 bytes + */ +static int s6sy761_read_events(struct s6sy761_data *sdata, u16 n_events) +{ + u8 cmd = S6SY761_READ_ALL_EVENT; + struct i2c_msg msgs[2] = { + { + .addr = sdata->client->addr, + .len = 1, + .buf = &cmd, + }, + { + .addr = sdata->client->addr, + .flags = I2C_M_RD, + .len = (n_events * S6SY761_EVENT_SIZE), + .buf = sdata->data + S6SY761_EVENT_SIZE, + }, + }; + int ret; + + ret = i2c_transfer(sdata->client->adapter, msgs, ARRAY_SIZE(msgs)); + if (ret < 0) + return ret; + + return ret == ARRAY_SIZE(msgs) ? 0 : -EIO; +} + +static void s6sy761_report_coordinates(struct s6sy761_data *sdata, + u8 *event, u8 tid) +{ + u8 major = event[4]; + u8 minor = event[5]; + u8 z = event[6] & S6SY761_MASK_Z; + u16 x = (event[1] << 3) | ((event[3] & S6SY761_MASK_X) >> 4); + u16 y = (event[2] << 3) | (event[3] & S6SY761_MASK_Y); + + input_mt_slot(sdata->input, tid); + + input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, true); + input_report_abs(sdata->input, ABS_MT_POSITION_X, x); + input_report_abs(sdata->input, ABS_MT_POSITION_Y, y); + input_report_abs(sdata->input, ABS_MT_TOUCH_MAJOR, major); + input_report_abs(sdata->input, ABS_MT_TOUCH_MINOR, minor); + input_report_abs(sdata->input, ABS_MT_PRESSURE, z); + + input_sync(sdata->input); +} + +static void s6sy761_report_release(struct s6sy761_data *sdata, + u8 *event, u8 tid) +{ + input_mt_slot(sdata->input, tid); + input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, false); + + input_sync(sdata->input); +} + +static void s6sy761_handle_coordinates(struct s6sy761_data *sdata, u8 *event) +{ + u8 tid; + u8 touch_state; + + if (unlikely(!(event[0] & S6SY761_MASK_TID))) + return; + + tid = ((event[0] & S6SY761_MASK_TID) >> 2) - 1; + touch_state = (event[0] & S6SY761_MASK_TOUCH_STATE) >> 6; + + switch (touch_state) { + + case S6SY761_TS_NONE: + break; + case S6SY761_TS_RELEASE: + s6sy761_report_release(sdata, event, tid); + break; + case S6SY761_TS_PRESS: + case S6SY761_TS_MOVE: + s6sy761_report_coordinates(sdata, event, tid); + break; + } +} + +static void s6sy761_handle_events(struct s6sy761_data *sdata, u8 n_events) +{ + int i; + + for (i = 0; i < n_events; i++) { + u8 *event = &sdata->data[i * S6SY761_EVENT_SIZE]; + u8 event_id = event[0] & S6SY761_MASK_EID; + + if (!event[0]) + return; + + switch (event_id) { + + case S6SY761_EVENT_ID_COORDINATE: + s6sy761_handle_coordinates(sdata, event); + break; + + case S6SY761_EVENT_ID_STATUS: + break; + + default: + break; + } + } +} + +static irqreturn_t s6sy761_irq_handler(int irq, void *dev) +{ + struct s6sy761_data *sdata = dev; + int ret; + u8 n_events; + + ret = i2c_smbus_read_i2c_block_data(sdata->client, + S6SY761_READ_ONE_EVENT, + S6SY761_EVENT_SIZE, + sdata->data); + if (ret < 0) { + dev_err(&sdata->client->dev, "failed to read events\n"); + return IRQ_HANDLED; + } + + if (!sdata->data[0]) + return IRQ_HANDLED; + + n_events = sdata->data[7] & S6SY761_MASK_LEFT_EVENTS; + if (unlikely(n_events > S6SY761_EVENT_COUNT - 1)) + return IRQ_HANDLED; + + if (n_events) { + ret = s6sy761_read_events(sdata, n_events); + if (ret < 0) { + dev_err(&sdata->client->dev, "failed to read events\n"); + return IRQ_HANDLED; + } + } + + s6sy761_handle_events(sdata, n_events + 1); + + return IRQ_HANDLED; +} + +static int s6sy761_input_open(struct input_dev *dev) +{ + struct s6sy761_data *sdata = input_get_drvdata(dev); + + return i2c_smbus_write_byte(sdata->client, S6SY761_SENSE_ON); +} + +static void s6sy761_input_close(struct input_dev *dev) +{ + struct s6sy761_data *sdata = input_get_drvdata(dev); + int ret; + + ret = i2c_smbus_write_byte(sdata->client, S6SY761_SENSE_OFF); + if (ret) + dev_err(&sdata->client->dev, "failed to turn off sensing\n"); +} + +static ssize_t s6sy761_sysfs_devid(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct s6sy761_data *sdata = dev_get_drvdata(dev); + + return sprintf(buf, "%#x\n", sdata->devid); +} + +static DEVICE_ATTR(devid, 0444, s6sy761_sysfs_devid, NULL); + +static struct attribute *s6sy761_sysfs_attrs[] = { + &dev_attr_devid.attr, + NULL +}; + +static struct attribute_group s6sy761_attribute_group = { + .attrs = s6sy761_sysfs_attrs +}; + +static int s6sy761_power_on(struct s6sy761_data *sdata) +{ + u8 buffer[S6SY761_EVENT_SIZE]; + u8 event; + int ret; + + ret = regulator_bulk_enable(ARRAY_SIZE(sdata->regulators), + sdata->regulators); + if (ret) + return ret; + + msleep(140); + + /* double check whether the touch is functional */ + ret = i2c_smbus_read_i2c_block_data(sdata->client, + S6SY761_READ_ONE_EVENT, + S6SY761_EVENT_SIZE, + buffer); + if (ret < 0) + return ret; + + event = (buffer[0] >> 2) & 0xf; + + if ((event != S6SY761_EVENT_INFO && + event != S6SY761_EVENT_VENDOR_INFO) || + buffer[1] != S6SY761_INFO_BOOT_COMPLETE) { + return -ENODEV; + } + + ret = i2c_smbus_read_byte_data(sdata->client, S6SY761_BOOT_STATUS); + if (ret < 0) + return ret; + + /* for some reasons the device might be stuck in the bootloader */ + if (ret != S6SY761_BS_APPLICATION) + return -ENODEV; + + /* enable touch functionality */ + ret = i2c_smbus_write_word_data(sdata->client, + S6SY761_TOUCH_FUNCTION, + S6SY761_MASK_TOUCH); + if (ret) + return ret; + + return 0; +} + +static int s6sy761_hw_init(struct s6sy761_data *sdata, + unsigned int *max_x, unsigned int *max_y) +{ + u8 buffer[S6SY761_PANEL_ID_SIZE]; /* larger read size */ + int ret; + + ret = s6sy761_power_on(sdata); + if (ret) + return ret; + + ret = i2c_smbus_read_i2c_block_data(sdata->client, + S6SY761_DEVICE_ID, + S6SY761_DEVID_SIZE, + buffer); + if (ret < 0) + return ret; + + sdata->devid = get_unaligned_be16(buffer + 1); + + ret = i2c_smbus_read_i2c_block_data(sdata->client, + S6SY761_PANEL_INFO, + S6SY761_PANEL_ID_SIZE, + buffer); + if (ret < 0) + return ret; + + *max_x = get_unaligned_be16(buffer); + *max_y = get_unaligned_be16(buffer + 2); + + /* if no tx channels defined, at least keep one */ + sdata->tx_channel = max_t(u8, buffer[8], 1); + + ret = i2c_smbus_read_byte_data(sdata->client, + S6SY761_FIRMWARE_INTEGRITY); + if (ret < 0) + return ret; + else if (ret != S6SY761_FW_OK) + return -ENODEV; + + return 0; +} + +static void s6sy761_power_off(void *data) +{ + struct s6sy761_data *sdata = data; + + disable_irq(sdata->client->irq); + regulator_bulk_disable(ARRAY_SIZE(sdata->regulators), + sdata->regulators); +} + +static int s6sy761_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct s6sy761_data *sdata; + unsigned int max_x, max_y; + int err; + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | + I2C_FUNC_SMBUS_BYTE_DATA | + I2C_FUNC_SMBUS_I2C_BLOCK)) + return -ENODEV; + + sdata = devm_kzalloc(&client->dev, sizeof(*sdata), GFP_KERNEL); + if (!sdata) + return -ENOMEM; + + i2c_set_clientdata(client, sdata); + sdata->client = client; + + sdata->regulators[S6SY761_REGULATOR_VDD].supply = "vdd"; + sdata->regulators[S6SY761_REGULATOR_AVDD].supply = "avdd"; + err = devm_regulator_bulk_get(&client->dev, + ARRAY_SIZE(sdata->regulators), + sdata->regulators); + if (err) + return err; + + err = devm_add_action_or_reset(&client->dev, s6sy761_power_off, sdata); + if (err) + return err; + + err = s6sy761_hw_init(sdata, &max_x, &max_y); + if (err) + return err; + + sdata->input = devm_input_allocate_device(&client->dev); + if (!sdata->input) + return -ENOMEM; + + sdata->input->name = S6SY761_DEV_NAME; + sdata->input->id.bustype = BUS_I2C; + sdata->input->open = s6sy761_input_open; + sdata->input->close = s6sy761_input_close; + + input_set_abs_params(sdata->input, ABS_MT_POSITION_X, 0, max_x, 0, 0); + input_set_abs_params(sdata->input, ABS_MT_POSITION_Y, 0, max_y, 0, 0); + input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); + input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0); + input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); + input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0); + input_set_abs_params(sdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0); + + touchscreen_parse_properties(sdata->input, true, &sdata->prop); + + if (!input_abs_get_max(sdata->input, ABS_X) || + !input_abs_get_max(sdata->input, ABS_Y)) { + dev_warn(&client->dev, "the axis have not been set\n"); + } + + err = input_mt_init_slots(sdata->input, sdata->tx_channel, + INPUT_MT_DIRECT); + if (err) + return err; + + input_set_drvdata(sdata->input, sdata); + + err = input_register_device(sdata->input); + if (err) + return err; + + err = devm_request_threaded_irq(&client->dev, client->irq, NULL, + s6sy761_irq_handler, + IRQF_TRIGGER_LOW | IRQF_ONESHOT, + "s6sy761_irq", sdata); + if (err) + return err; + + err = devm_device_add_group(&client->dev, &s6sy761_attribute_group); + if (err) + return err; + + pm_runtime_enable(&client->dev); + + return 0; +} + +static int s6sy761_remove(struct i2c_client *client) +{ + pm_runtime_disable(&client->dev); + + return 0; +} + +static int __maybe_unused s6sy761_runtime_suspend(struct device *dev) +{ + struct s6sy761_data *sdata = dev_get_drvdata(dev); + + return i2c_smbus_write_byte_data(sdata->client, + S6SY761_APPLICATION_MODE, S6SY761_APP_SLEEP); +} + +static int __maybe_unused s6sy761_runtime_resume(struct device *dev) +{ + struct s6sy761_data *sdata = dev_get_drvdata(dev); + + return i2c_smbus_write_byte_data(sdata->client, + S6SY761_APPLICATION_MODE, S6SY761_APP_NORMAL); +} + +static int __maybe_unused s6sy761_suspend(struct device *dev) +{ + struct s6sy761_data *sdata = dev_get_drvdata(dev); + + s6sy761_power_off(sdata); + + return 0; +} + +static int __maybe_unused s6sy761_resume(struct device *dev) +{ + struct s6sy761_data *sdata = dev_get_drvdata(dev); + + enable_irq(sdata->client->irq); + + return s6sy761_power_on(sdata); +} + +static const struct dev_pm_ops s6sy761_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(s6sy761_suspend, s6sy761_resume) + SET_RUNTIME_PM_OPS(s6sy761_runtime_suspend, + s6sy761_runtime_resume, NULL) +}; + +#ifdef CONFIG_OF +static const struct of_device_id s6sy761_of_match[] = { + { .compatible = "samsung,s6sy761", }, + { }, +}; +MODULE_DEVICE_TABLE(of, s6sy761_of_match); +#endif + +static const struct i2c_device_id s6sy761_id[] = { + { "s6sy761", 0 }, + { }, +}; +MODULE_DEVICE_TABLE(i2c, s6sy761_id); + +static struct i2c_driver s6sy761_driver = { + .driver = { + .name = S6SY761_DEV_NAME, + .of_match_table = of_match_ptr(s6sy761_of_match), + .pm = &s6sy761_pm_ops, + }, + .probe = s6sy761_probe, + .remove = s6sy761_remove, + .id_table = s6sy761_id, +}; + +module_i2c_driver(s6sy761_driver); + +MODULE_AUTHOR("Andi Shyti "); +MODULE_DESCRIPTION("Samsung S6SY761 Touch Screen"); +MODULE_LICENSE("GPL v2"); -- cgit v1.2.3