Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: elants_i2c - wire up regulator support

Elan touchscreen controllers use two power supplies, vcc33 and vccio,
and we need to enable them before trying to access the device. On X86
firmware usually does this, but on ARM it is usually left to the kernel.

Signed-off-by: Dmitry Torokhov <dtor@chromium.org>
Reviewed-by: Benson Leung <bleung@chromium.org>
Reviewed-by: Scott Liu <scott.liu@emc.com.tw>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

Dmitry Torokhov and committed by
Dmitry Torokhov
afe10358 00159f19

+165 -22
+3
Documentation/devicetree/bindings/input/elants_i2c.txt
··· 13 13 - pinctrl-names: should be "default" (see pinctrl binding [1]). 14 14 - pinctrl-0: a phandle pointing to the pin settings for the device (see 15 15 pinctrl binding [1]). 16 + - reset-gpios: reset gpio the chip is connected to. 17 + - vcc33-supply: a phandle for the regulator supplying 3.3V power. 18 + - vccio-supply: a phandle for the regulator supplying IO power. 16 19 17 20 [0]: Documentation/devicetree/bindings/interrupt-controller/interrupts.txt 18 21 [1]: Documentation/devicetree/bindings/pinctrl/pinctrl-bindings.txt
+162 -22
drivers/input/touchscreen/elants_i2c.c
··· 38 38 #include <linux/input/mt.h> 39 39 #include <linux/acpi.h> 40 40 #include <linux/of.h> 41 + #include <linux/gpio/consumer.h> 42 + #include <linux/regulator/consumer.h> 41 43 #include <asm/unaligned.h> 42 44 43 45 /* Device, Driver information */ ··· 104 102 /* calibration timeout definition */ 105 103 #define ELAN_CALI_TIMEOUT_MSEC 10000 106 104 105 + #define ELAN_POWERON_DELAY_USEC 500 106 + #define ELAN_RESET_DELAY_MSEC 20 107 + 107 108 enum elants_state { 108 109 ELAN_STATE_NORMAL, 109 110 ELAN_WAIT_QUEUE_HEADER, ··· 122 117 struct elants_data { 123 118 struct i2c_client *client; 124 119 struct input_dev *input; 120 + 121 + struct regulator *vcc33; 122 + struct regulator *vccio; 123 + struct gpio_desc *reset_gpio; 125 124 126 125 u16 fw_version; 127 126 u8 test_version; ··· 150 141 u8 buf[MAX_PACKET_SIZE]; 151 142 152 143 bool wake_irq_enabled; 144 + bool keep_power_in_suspend; 153 145 }; 154 146 155 147 static int elants_i2c_send(struct i2c_client *client, ··· 1068 1058 sysfs_remove_group(&ts->client->dev.kobj, &elants_attribute_group); 1069 1059 } 1070 1060 1061 + static int elants_i2c_power_on(struct elants_data *ts) 1062 + { 1063 + int error; 1064 + 1065 + /* 1066 + * If we do not have reset gpio assume platform firmware 1067 + * controls regulators and does power them on for us. 1068 + */ 1069 + if (IS_ERR_OR_NULL(ts->reset_gpio)) 1070 + return 0; 1071 + 1072 + gpiod_set_value_cansleep(ts->reset_gpio, 1); 1073 + 1074 + error = regulator_enable(ts->vcc33); 1075 + if (error) { 1076 + dev_err(&ts->client->dev, 1077 + "failed to enable vcc33 regulator: %d\n", 1078 + error); 1079 + goto release_reset_gpio; 1080 + } 1081 + 1082 + error = regulator_enable(ts->vccio); 1083 + if (error) { 1084 + dev_err(&ts->client->dev, 1085 + "failed to enable vccio regulator: %d\n", 1086 + error); 1087 + regulator_disable(ts->vcc33); 1088 + goto release_reset_gpio; 1089 + } 1090 + 1091 + /* 1092 + * We need to wait a bit after powering on controller before 1093 + * we are allowed to release reset GPIO. 1094 + */ 1095 + udelay(ELAN_POWERON_DELAY_USEC); 1096 + 1097 + release_reset_gpio: 1098 + gpiod_set_value_cansleep(ts->reset_gpio, 0); 1099 + if (error) 1100 + return error; 1101 + 1102 + msleep(ELAN_RESET_DELAY_MSEC); 1103 + 1104 + return 0; 1105 + } 1106 + 1107 + static void elants_i2c_power_off(void *_data) 1108 + { 1109 + struct elants_data *ts = _data; 1110 + 1111 + if (!IS_ERR_OR_NULL(ts->reset_gpio)) { 1112 + /* 1113 + * Activate reset gpio to prevent leakage through the 1114 + * pin once we shut off power to the controller. 1115 + */ 1116 + gpiod_set_value_cansleep(ts->reset_gpio, 1); 1117 + regulator_disable(ts->vccio); 1118 + regulator_disable(ts->vcc33); 1119 + } 1120 + } 1121 + 1071 1122 static int elants_i2c_probe(struct i2c_client *client, 1072 1123 const struct i2c_device_id *id) 1073 1124 { ··· 1143 1072 return -ENXIO; 1144 1073 } 1145 1074 1146 - /* Make sure there is something at this address */ 1147 - if (i2c_smbus_xfer(client->adapter, client->addr, 0, 1148 - I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) { 1149 - dev_err(&client->dev, "nothing at this address\n"); 1150 - return -ENXIO; 1151 - } 1152 - 1153 1075 ts = devm_kzalloc(&client->dev, sizeof(struct elants_data), GFP_KERNEL); 1154 1076 if (!ts) 1155 1077 return -ENOMEM; ··· 1152 1088 1153 1089 ts->client = client; 1154 1090 i2c_set_clientdata(client, ts); 1091 + 1092 + ts->vcc33 = devm_regulator_get(&client->dev, "vcc33"); 1093 + if (IS_ERR(ts->vcc33)) { 1094 + error = PTR_ERR(ts->vcc33); 1095 + if (error != -EPROBE_DEFER) 1096 + dev_err(&client->dev, 1097 + "Failed to get 'vcc33' regulator: %d\n", 1098 + error); 1099 + return error; 1100 + } 1101 + 1102 + ts->vccio = devm_regulator_get(&client->dev, "vccio"); 1103 + if (IS_ERR(ts->vccio)) { 1104 + error = PTR_ERR(ts->vccio); 1105 + if (error != -EPROBE_DEFER) 1106 + dev_err(&client->dev, 1107 + "Failed to get 'vccio' regulator: %d\n", 1108 + error); 1109 + return error; 1110 + } 1111 + 1112 + ts->reset_gpio = devm_gpiod_get(&client->dev, "reset"); 1113 + if (IS_ERR(ts->reset_gpio)) { 1114 + error = PTR_ERR(ts->reset_gpio); 1115 + 1116 + if (error == -EPROBE_DEFER) 1117 + return error; 1118 + 1119 + if (error != -ENOENT && error != -ENOSYS) { 1120 + dev_err(&client->dev, 1121 + "failed to get reset gpio: %d\n", 1122 + error); 1123 + return error; 1124 + } 1125 + 1126 + ts->keep_power_in_suspend = true; 1127 + } else { 1128 + error = gpiod_direction_output(ts->reset_gpio, 0); 1129 + if (error) { 1130 + dev_err(&client->dev, 1131 + "failed to configure reset gpio as output: %d\n", 1132 + error); 1133 + return error; 1134 + } 1135 + } 1136 + 1137 + error = elants_i2c_power_on(ts); 1138 + if (error) 1139 + return error; 1140 + 1141 + error = devm_add_action(&client->dev, elants_i2c_power_off, ts); 1142 + if (error) { 1143 + dev_err(&client->dev, 1144 + "failed to install power off action: %d\n", error); 1145 + elants_i2c_power_off(ts); 1146 + return error; 1147 + } 1148 + 1149 + /* Make sure there is something at this address */ 1150 + if (i2c_smbus_xfer(client->adapter, client->addr, 0, 1151 + I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) { 1152 + dev_err(&client->dev, "nothing at this address\n"); 1153 + return -ENXIO; 1154 + } 1155 1155 1156 1156 error = elants_i2c_initialize(ts); 1157 1157 if (error) { ··· 1324 1196 1325 1197 disable_irq(client->irq); 1326 1198 1327 - for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { 1328 - error = elants_i2c_send(client, set_sleep_cmd, 1329 - sizeof(set_sleep_cmd)); 1330 - if (!error) 1331 - break; 1199 + if (device_may_wakeup(dev) || ts->keep_power_in_suspend) { 1200 + for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { 1201 + error = elants_i2c_send(client, set_sleep_cmd, 1202 + sizeof(set_sleep_cmd)); 1203 + if (!error) 1204 + break; 1332 1205 1333 - dev_err(&client->dev, "suspend command failed: %d\n", error); 1206 + dev_err(&client->dev, 1207 + "suspend command failed: %d\n", error); 1208 + } 1209 + 1210 + if (device_may_wakeup(dev)) 1211 + ts->wake_irq_enabled = 1212 + (enable_irq_wake(client->irq) == 0); 1213 + } else { 1214 + elants_i2c_power_off(ts); 1334 1215 } 1335 - 1336 - if (device_may_wakeup(dev)) 1337 - ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0); 1338 1216 1339 1217 return 0; 1340 1218 } ··· 1356 1222 if (device_may_wakeup(dev) && ts->wake_irq_enabled) 1357 1223 disable_irq_wake(client->irq); 1358 1224 1359 - for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { 1360 - error = elants_i2c_send(client, set_active_cmd, 1361 - sizeof(set_active_cmd)); 1362 - if (!error) 1363 - break; 1225 + if (ts->keep_power_in_suspend) { 1226 + for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { 1227 + error = elants_i2c_send(client, set_active_cmd, 1228 + sizeof(set_active_cmd)); 1229 + if (!error) 1230 + break; 1364 1231 1365 - dev_err(&client->dev, "resume command failed: %d\n", error); 1232 + dev_err(&client->dev, 1233 + "resume command failed: %d\n", error); 1234 + } 1235 + } else { 1236 + elants_i2c_power_on(ts); 1237 + elants_i2c_initialize(ts); 1366 1238 } 1367 1239 1368 1240 ts->state = ELAN_STATE_NORMAL;