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

Input: resistive-adc-touch - rework mapping of channels

Instead of iterating over channels establish and use channel map to
retrieve data. As a side effect this will silence "uninitialized variable"
warnings.

Tested-by: Oleksij Rempel <o.rempel@pengutronix.de>
Link: https://lore.kernel.org/r/YLXR2brkc4H54xtK@google.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+52 -64
+52 -64
drivers/input/touchscreen/resistive-adc-touch.c
··· 25 25 #define GRTS_MAX_CHANNELS 4 26 26 27 27 enum grts_ch_type { 28 - GRTS_CH_NONE = 0, 29 28 GRTS_CH_X, 30 29 GRTS_CH_Y, 31 30 GRTS_CH_PRESSURE, 32 31 GRTS_CH_Z1, 33 32 GRTS_CH_Z2, 33 + GRTS_CH_MAX = GRTS_CH_Z2 + 1 34 34 }; 35 35 36 36 /** ··· 42 42 * @iio_cb: iio_callback buffer for the data 43 43 * @input: the input device structure that we register 44 44 * @prop: touchscreen properties struct 45 - * @ch: channels that are defined for the touchscreen 45 + * @ch_map: map of channels that are defined for the touchscreen 46 46 */ 47 47 struct grts_state { 48 48 u32 x_plate_ohms; ··· 52 52 struct iio_cb_buffer *iio_cb; 53 53 struct input_dev *input; 54 54 struct touchscreen_properties prop; 55 - u8 ch[GRTS_MAX_CHANNELS]; 55 + u8 ch_map[GRTS_CH_MAX]; 56 56 }; 57 57 58 58 static int grts_cb(const void *data, void *private) 59 59 { 60 60 const u16 *touch_info = data; 61 61 struct grts_state *st = private; 62 - unsigned int x, y, press = 0, z1 = 0, z2; 63 - unsigned int Rt, i; 62 + unsigned int x, y, press; 64 63 65 - for (i = 0; i < ARRAY_SIZE(st->ch) && st->ch[i] != GRTS_CH_NONE; i++) { 66 - switch (st->ch[i]) { 67 - case GRTS_CH_X: 68 - x = touch_info[i]; 69 - break; 70 - case GRTS_CH_Y: 71 - y = touch_info[i]; 72 - break; 73 - case GRTS_CH_PRESSURE: 74 - press = touch_info[i]; 75 - break; 76 - case GRTS_CH_Z1: 77 - z1 = touch_info[i]; 78 - break; 79 - case GRTS_CH_Z2: 80 - z2 = touch_info[i]; 81 - break; 82 - } 83 - } 64 + x = touch_info[st->ch_map[GRTS_CH_X]]; 65 + y = touch_info[st->ch_map[GRTS_CH_Y]]; 84 66 85 - if (z1) { 67 + if (st->ch_map[GRTS_CH_PRESSURE] < GRTS_MAX_CHANNELS) { 68 + press = touch_info[st->ch_map[GRTS_CH_PRESSURE]]; 69 + } else if (st->ch_map[GRTS_CH_Z1] < GRTS_MAX_CHANNELS) { 70 + unsigned int z1 = touch_info[st->ch_map[GRTS_CH_Z1]]; 71 + unsigned int z2 = touch_info[st->ch_map[GRTS_CH_Z2]]; 72 + unsigned int Rt; 73 + 86 74 Rt = z2; 87 75 Rt -= z1; 88 76 Rt *= st->x_plate_ohms; ··· 130 142 iio_channel_release_all_cb(data); 131 143 } 132 144 145 + static int grts_map_channel(struct grts_state *st, struct device *dev, 146 + enum grts_ch_type type, const char *name, 147 + bool optional) 148 + { 149 + int idx; 150 + 151 + idx = device_property_match_string(dev, "io-channel-names", name); 152 + if (idx < 0) { 153 + if (!optional) 154 + return idx; 155 + idx = GRTS_MAX_CHANNELS; 156 + } else if (idx >= GRTS_MAX_CHANNELS) { 157 + return -EOVERFLOW; 158 + } 159 + 160 + st->ch_map[type] = idx; 161 + return 0; 162 + } 163 + 133 164 static int grts_get_properties(struct grts_state *st, struct device *dev) 134 165 { 135 - int idx, error; 166 + int error; 136 167 137 - idx = device_property_match_string(dev, "io-channel-names", "x"); 138 - if (idx < 0) 139 - return idx; 168 + error = grts_map_channel(st, dev, GRTS_CH_X, "x", false); 169 + if (error) 170 + return error; 140 171 141 - if (idx >= ARRAY_SIZE(st->ch)) 142 - return -EOVERFLOW; 143 - 144 - st->ch[idx] = GRTS_CH_X; 145 - 146 - idx = device_property_match_string(dev, "io-channel-names", "y"); 147 - if (idx < 0) 148 - return idx; 149 - 150 - if (idx >= ARRAY_SIZE(st->ch)) 151 - return -EOVERFLOW; 152 - 153 - st->ch[idx] = GRTS_CH_Y; 172 + error = grts_map_channel(st, dev, GRTS_CH_Y, "y", false); 173 + if (error) 174 + return error; 154 175 155 176 /* pressure is optional */ 156 - idx = device_property_match_string(dev, "io-channel-names", "pressure"); 157 - if (idx >= 0) { 158 - if (idx >= ARRAY_SIZE(st->ch)) 159 - return -EOVERFLOW; 177 + error = grts_map_channel(st, dev, GRTS_CH_PRESSURE, "pressure", true); 178 + if (error) 179 + return error; 160 180 161 - st->ch[idx] = GRTS_CH_PRESSURE; 181 + if (st->ch_map[GRTS_CH_PRESSURE] < GRTS_MAX_CHANNELS) { 162 182 st->pressure = true; 163 - 164 183 return 0; 165 184 } 166 185 167 186 /* if no pressure is defined, try optional z1 + z2 */ 168 - idx = device_property_match_string(dev, "io-channel-names", "z1"); 169 - if (idx < 0) 187 + error = grts_map_channel(st, dev, GRTS_CH_Z1, "z1", true); 188 + if (error) 189 + return error; 190 + 191 + if (st->ch_map[GRTS_CH_Z1] >= GRTS_MAX_CHANNELS) 170 192 return 0; 171 193 172 - if (idx >= ARRAY_SIZE(st->ch)) 173 - return -EOVERFLOW; 174 - 175 - st->ch[idx] = GRTS_CH_Z1; 176 - 177 194 /* if z1 is provided z2 is not optional */ 178 - idx = device_property_match_string(dev, "io-channel-names", "z2"); 179 - if (idx < 0) 180 - return idx; 181 - 182 - if (idx >= ARRAY_SIZE(st->ch)) 183 - return -EOVERFLOW; 184 - 185 - st->ch[idx] = GRTS_CH_Z2; 186 - st->pressure = true; 195 + error = grts_map_channel(st, dev, GRTS_CH_Z2, "z2", true); 196 + if (error) 197 + return error; 187 198 188 199 error = device_property_read_u32(dev, 189 200 "touchscreen-x-plate-ohms", ··· 192 205 return error; 193 206 } 194 207 208 + st->pressure = true; 195 209 return 0; 196 210 } 197 211