1bd3cba00SDouglas Anderson // SPDX-License-Identifier: GPL-2.0 2bd3cba00SDouglas Anderson /* 3bd3cba00SDouglas Anderson * Driver for Elan touchscreens that use the i2c-hid protocol. 4bd3cba00SDouglas Anderson * 5bd3cba00SDouglas Anderson * Copyright 2020 Google LLC 6bd3cba00SDouglas Anderson */ 7bd3cba00SDouglas Anderson 8bd3cba00SDouglas Anderson #include <linux/delay.h> 9bd3cba00SDouglas Anderson #include <linux/device.h> 10bd3cba00SDouglas Anderson #include <linux/gpio/consumer.h> 11bd3cba00SDouglas Anderson #include <linux/i2c.h> 12bd3cba00SDouglas Anderson #include <linux/kernel.h> 13bd3cba00SDouglas Anderson #include <linux/module.h> 14bd3cba00SDouglas Anderson #include <linux/of.h> 15bd3cba00SDouglas Anderson #include <linux/pm.h> 16bd3cba00SDouglas Anderson #include <linux/regulator/consumer.h> 17bd3cba00SDouglas Anderson 18bd3cba00SDouglas Anderson #include "i2c-hid.h" 19bd3cba00SDouglas Anderson 20bd3cba00SDouglas Anderson struct elan_i2c_hid_chip_data { 21f2f43bf1SCong Yang unsigned int post_gpio_reset_on_delay_ms; 22f2f43bf1SCong Yang unsigned int post_gpio_reset_off_delay_ms; 23bd3cba00SDouglas Anderson unsigned int post_power_delay_ms; 24bd3cba00SDouglas Anderson u16 hid_descriptor_address; 25f2f43bf1SCong Yang const char *main_supply_name; 26bd3cba00SDouglas Anderson }; 27bd3cba00SDouglas Anderson 28bd3cba00SDouglas Anderson struct i2c_hid_of_elan { 29bd3cba00SDouglas Anderson struct i2chid_ops ops; 30bd3cba00SDouglas Anderson 31bd3cba00SDouglas Anderson struct regulator *vcc33; 32bd3cba00SDouglas Anderson struct regulator *vccio; 33bd3cba00SDouglas Anderson struct gpio_desc *reset_gpio; 34*6d458d0dSJohan Hovold bool no_reset_on_power_off; 35bd3cba00SDouglas Anderson const struct elan_i2c_hid_chip_data *chip_data; 36bd3cba00SDouglas Anderson }; 37bd3cba00SDouglas Anderson 38bd3cba00SDouglas Anderson static int elan_i2c_hid_power_up(struct i2chid_ops *ops) 39bd3cba00SDouglas Anderson { 40bd3cba00SDouglas Anderson struct i2c_hid_of_elan *ihid_elan = 41bd3cba00SDouglas Anderson container_of(ops, struct i2c_hid_of_elan, ops); 42bd3cba00SDouglas Anderson int ret; 43bd3cba00SDouglas Anderson 44*6d458d0dSJohan Hovold gpiod_set_value_cansleep(ihid_elan->reset_gpio, 1); 45*6d458d0dSJohan Hovold 46f2f43bf1SCong Yang if (ihid_elan->vcc33) { 47bd3cba00SDouglas Anderson ret = regulator_enable(ihid_elan->vcc33); 48bd3cba00SDouglas Anderson if (ret) 49*6d458d0dSJohan Hovold goto err_deassert_reset; 50f2f43bf1SCong Yang } 51bd3cba00SDouglas Anderson 52bd3cba00SDouglas Anderson ret = regulator_enable(ihid_elan->vccio); 53*6d458d0dSJohan Hovold if (ret) 54*6d458d0dSJohan Hovold goto err_disable_vcc33; 55bd3cba00SDouglas Anderson 56bd3cba00SDouglas Anderson if (ihid_elan->chip_data->post_power_delay_ms) 57bd3cba00SDouglas Anderson msleep(ihid_elan->chip_data->post_power_delay_ms); 58bd3cba00SDouglas Anderson 59bd3cba00SDouglas Anderson gpiod_set_value_cansleep(ihid_elan->reset_gpio, 0); 60f2f43bf1SCong Yang if (ihid_elan->chip_data->post_gpio_reset_on_delay_ms) 61f2f43bf1SCong Yang msleep(ihid_elan->chip_data->post_gpio_reset_on_delay_ms); 62bd3cba00SDouglas Anderson 63bd3cba00SDouglas Anderson return 0; 64*6d458d0dSJohan Hovold 65*6d458d0dSJohan Hovold err_disable_vcc33: 66*6d458d0dSJohan Hovold if (ihid_elan->vcc33) 67*6d458d0dSJohan Hovold regulator_disable(ihid_elan->vcc33); 68*6d458d0dSJohan Hovold err_deassert_reset: 69*6d458d0dSJohan Hovold if (ihid_elan->no_reset_on_power_off) 70*6d458d0dSJohan Hovold gpiod_set_value_cansleep(ihid_elan->reset_gpio, 0); 71*6d458d0dSJohan Hovold 72*6d458d0dSJohan Hovold return ret; 73bd3cba00SDouglas Anderson } 74bd3cba00SDouglas Anderson 75bd3cba00SDouglas Anderson static void elan_i2c_hid_power_down(struct i2chid_ops *ops) 76bd3cba00SDouglas Anderson { 77bd3cba00SDouglas Anderson struct i2c_hid_of_elan *ihid_elan = 78bd3cba00SDouglas Anderson container_of(ops, struct i2c_hid_of_elan, ops); 79bd3cba00SDouglas Anderson 80*6d458d0dSJohan Hovold /* 81*6d458d0dSJohan Hovold * Do not assert reset when the hardware allows for it to remain 82*6d458d0dSJohan Hovold * deasserted regardless of the state of the (shared) power supply to 83*6d458d0dSJohan Hovold * avoid wasting power when the supply is left on. 84*6d458d0dSJohan Hovold */ 85*6d458d0dSJohan Hovold if (!ihid_elan->no_reset_on_power_off) 86bd3cba00SDouglas Anderson gpiod_set_value_cansleep(ihid_elan->reset_gpio, 1); 87*6d458d0dSJohan Hovold 88f2f43bf1SCong Yang if (ihid_elan->chip_data->post_gpio_reset_off_delay_ms) 89f2f43bf1SCong Yang msleep(ihid_elan->chip_data->post_gpio_reset_off_delay_ms); 90f2f43bf1SCong Yang 91bd3cba00SDouglas Anderson regulator_disable(ihid_elan->vccio); 92f2f43bf1SCong Yang if (ihid_elan->vcc33) 93bd3cba00SDouglas Anderson regulator_disable(ihid_elan->vcc33); 94bd3cba00SDouglas Anderson } 95bd3cba00SDouglas Anderson 96baf34f3bSStephen Kitt static int i2c_hid_of_elan_probe(struct i2c_client *client) 97bd3cba00SDouglas Anderson { 98bd3cba00SDouglas Anderson struct i2c_hid_of_elan *ihid_elan; 99*6d458d0dSJohan Hovold int ret; 100bd3cba00SDouglas Anderson 101bd3cba00SDouglas Anderson ihid_elan = devm_kzalloc(&client->dev, sizeof(*ihid_elan), GFP_KERNEL); 102bd3cba00SDouglas Anderson if (!ihid_elan) 103bd3cba00SDouglas Anderson return -ENOMEM; 104bd3cba00SDouglas Anderson 105bd3cba00SDouglas Anderson ihid_elan->ops.power_up = elan_i2c_hid_power_up; 106bd3cba00SDouglas Anderson ihid_elan->ops.power_down = elan_i2c_hid_power_down; 107bd3cba00SDouglas Anderson 108bd3cba00SDouglas Anderson /* Start out with reset asserted */ 109bd3cba00SDouglas Anderson ihid_elan->reset_gpio = 110bd3cba00SDouglas Anderson devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH); 111bd3cba00SDouglas Anderson if (IS_ERR(ihid_elan->reset_gpio)) 112bd3cba00SDouglas Anderson return PTR_ERR(ihid_elan->reset_gpio); 113bd3cba00SDouglas Anderson 114*6d458d0dSJohan Hovold ihid_elan->no_reset_on_power_off = of_property_read_bool(client->dev.of_node, 115*6d458d0dSJohan Hovold "no-reset-on-power-off"); 116*6d458d0dSJohan Hovold 117bd3cba00SDouglas Anderson ihid_elan->vccio = devm_regulator_get(&client->dev, "vccio"); 118*6d458d0dSJohan Hovold if (IS_ERR(ihid_elan->vccio)) { 119*6d458d0dSJohan Hovold ret = PTR_ERR(ihid_elan->vccio); 120*6d458d0dSJohan Hovold goto err_deassert_reset; 121*6d458d0dSJohan Hovold } 122bd3cba00SDouglas Anderson 123f2f43bf1SCong Yang ihid_elan->chip_data = device_get_match_data(&client->dev); 124f2f43bf1SCong Yang 125f2f43bf1SCong Yang if (ihid_elan->chip_data->main_supply_name) { 126f2f43bf1SCong Yang ihid_elan->vcc33 = devm_regulator_get(&client->dev, 127f2f43bf1SCong Yang ihid_elan->chip_data->main_supply_name); 128*6d458d0dSJohan Hovold if (IS_ERR(ihid_elan->vcc33)) { 129*6d458d0dSJohan Hovold ret = PTR_ERR(ihid_elan->vcc33); 130*6d458d0dSJohan Hovold goto err_deassert_reset; 131*6d458d0dSJohan Hovold } 132f2f43bf1SCong Yang } 133bd3cba00SDouglas Anderson 134*6d458d0dSJohan Hovold ret = i2c_hid_core_probe(client, &ihid_elan->ops, 135bd3cba00SDouglas Anderson ihid_elan->chip_data->hid_descriptor_address, 0); 136*6d458d0dSJohan Hovold if (ret) 137*6d458d0dSJohan Hovold goto err_deassert_reset; 138*6d458d0dSJohan Hovold 139*6d458d0dSJohan Hovold return 0; 140*6d458d0dSJohan Hovold 141*6d458d0dSJohan Hovold err_deassert_reset: 142*6d458d0dSJohan Hovold if (ihid_elan->no_reset_on_power_off) 143*6d458d0dSJohan Hovold gpiod_set_value_cansleep(ihid_elan->reset_gpio, 0); 144*6d458d0dSJohan Hovold 145*6d458d0dSJohan Hovold return ret; 146bd3cba00SDouglas Anderson } 147bd3cba00SDouglas Anderson 148bd3cba00SDouglas Anderson static const struct elan_i2c_hid_chip_data elan_ekth6915_chip_data = { 149bd3cba00SDouglas Anderson .post_power_delay_ms = 1, 150f2f43bf1SCong Yang .post_gpio_reset_on_delay_ms = 300, 151bd3cba00SDouglas Anderson .hid_descriptor_address = 0x0001, 152f2f43bf1SCong Yang .main_supply_name = "vcc33", 153f2f43bf1SCong Yang }; 154f2f43bf1SCong Yang 155f2f43bf1SCong Yang static const struct elan_i2c_hid_chip_data ilitek_ili9882t_chip_data = { 156f2f43bf1SCong Yang .post_power_delay_ms = 1, 157f2f43bf1SCong Yang .post_gpio_reset_on_delay_ms = 200, 158f2f43bf1SCong Yang .post_gpio_reset_off_delay_ms = 65, 159f2f43bf1SCong Yang .hid_descriptor_address = 0x0001, 160f2f43bf1SCong Yang /* 161f2f43bf1SCong Yang * this touchscreen is tightly integrated with the panel and assumes 162f2f43bf1SCong Yang * that the relevant power rails (other than the IO rail) have already 163f2f43bf1SCong Yang * been turned on by the panel driver because we're a panel follower. 164f2f43bf1SCong Yang */ 165f2f43bf1SCong Yang .main_supply_name = NULL, 166bd3cba00SDouglas Anderson }; 167bd3cba00SDouglas Anderson 168bd3cba00SDouglas Anderson static const struct of_device_id elan_i2c_hid_of_match[] = { 169bd3cba00SDouglas Anderson { .compatible = "elan,ekth6915", .data = &elan_ekth6915_chip_data }, 170f2f43bf1SCong Yang { .compatible = "ilitek,ili9882t", .data = &ilitek_ili9882t_chip_data }, 171bd3cba00SDouglas Anderson { } 172bd3cba00SDouglas Anderson }; 173bd3cba00SDouglas Anderson MODULE_DEVICE_TABLE(of, elan_i2c_hid_of_match); 174bd3cba00SDouglas Anderson 175bd3cba00SDouglas Anderson static struct i2c_driver elan_i2c_hid_ts_driver = { 176bd3cba00SDouglas Anderson .driver = { 177bd3cba00SDouglas Anderson .name = "i2c_hid_of_elan", 178bd3cba00SDouglas Anderson .pm = &i2c_hid_core_pm, 179bd3cba00SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS, 180bd3cba00SDouglas Anderson .of_match_table = of_match_ptr(elan_i2c_hid_of_match), 181bd3cba00SDouglas Anderson }, 182e4b88075SUwe Kleine-König .probe = i2c_hid_of_elan_probe, 183bd3cba00SDouglas Anderson .remove = i2c_hid_core_remove, 184bd3cba00SDouglas Anderson .shutdown = i2c_hid_core_shutdown, 185bd3cba00SDouglas Anderson }; 186bd3cba00SDouglas Anderson module_i2c_driver(elan_i2c_hid_ts_driver); 187bd3cba00SDouglas Anderson 188bd3cba00SDouglas Anderson MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>"); 189bd3cba00SDouglas Anderson MODULE_DESCRIPTION("Elan i2c-hid touchscreen driver"); 190bd3cba00SDouglas Anderson MODULE_LICENSE("GPL"); 191