2525#define GRTS_MAX_CHANNELS 4
2626
2727enum grts_ch_type {
28- GRTS_CH_NONE = 0 ,
2928 GRTS_CH_X ,
3029 GRTS_CH_Y ,
3130 GRTS_CH_PRESSURE ,
3231 GRTS_CH_Z1 ,
3332 GRTS_CH_Z2 ,
33+ GRTS_CH_MAX = GRTS_CH_Z2 + 1
3434};
3535
3636/**
@@ -42,7 +42,7 @@ enum grts_ch_type {
4242 * @iio_cb: iio_callback buffer for the data
4343 * @input: the input device structure that we register
4444 * @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
4646 */
4747struct grts_state {
4848 u32 x_plate_ohms ;
@@ -52,37 +52,25 @@ struct grts_state {
5252 struct iio_cb_buffer * iio_cb ;
5353 struct input_dev * input ;
5454 struct touchscreen_properties prop ;
55- u8 ch [ GRTS_MAX_CHANNELS ];
55+ u8 ch_map [ GRTS_CH_MAX ];
5656};
5757
5858static int grts_cb (const void * data , void * private )
5959{
6060 const u16 * touch_info = data ;
6161 struct grts_state * st = private ;
62- unsigned int x , y , press = 0 , z1 = 0 , z2 ;
63- unsigned int Rt , i ;
64-
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- }
62+ unsigned int x , y , press ;
63+
64+ x = touch_info [st -> ch_map [GRTS_CH_X ]];
65+ y = touch_info [st -> ch_map [GRTS_CH_Y ]];
66+
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 ;
8473
85- if (z1 ) {
8674 Rt = z2 ;
8775 Rt -= z1 ;
8876 Rt *= st -> x_plate_ohms ;
@@ -142,60 +130,59 @@ static void grts_disable(void *data)
142130 iio_channel_release_all_cb (data );
143131}
144132
145- static int grts_get_properties (struct grts_state * st , struct device * dev )
133+ static int grts_map_channel (struct grts_state * st , struct device * dev ,
134+ enum grts_ch_type type , const char * name ,
135+ bool optional )
146136{
147- int idx , error ;
148-
149- idx = device_property_match_string (dev , "io-channel-names" , "x" );
150- if (idx < 0 )
151- return idx ;
152-
153- if (idx >= ARRAY_SIZE (st -> ch ))
137+ int idx ;
138+
139+ idx = device_property_match_string (dev , "io-channel-names" , name );
140+ if (idx < 0 ) {
141+ if (!optional )
142+ return idx ;
143+ idx = GRTS_MAX_CHANNELS ;
144+ } else if (idx >= GRTS_MAX_CHANNELS ) {
154145 return - EOVERFLOW ;
146+ }
155147
156- st -> ch [idx ] = GRTS_CH_X ;
148+ st -> ch_map [type ] = idx ;
149+ return 0 ;
150+ }
157151
158- idx = device_property_match_string ( dev , "io-channel-names" , "y" );
159- if ( idx < 0 )
160- return idx ;
152+ static int grts_get_properties ( struct grts_state * st , struct device * dev )
153+ {
154+ int error ;
161155
162- if (idx >= ARRAY_SIZE (st -> ch ))
163- return - EOVERFLOW ;
156+ error = grts_map_channel (st , dev , GRTS_CH_X , "x" , false);
157+ if (error )
158+ return error ;
164159
165- st -> ch [idx ] = GRTS_CH_Y ;
160+ error = grts_map_channel (st , dev , GRTS_CH_Y , "y" , false);
161+ if (error )
162+ return error ;
166163
167164 /* pressure is optional */
168- idx = device_property_match_string (dev , "io-channel-names" , "pressure" );
169- if (idx >= 0 ) {
170- if (idx >= ARRAY_SIZE (st -> ch ))
171- return - EOVERFLOW ;
165+ error = grts_map_channel (st , dev , GRTS_CH_PRESSURE , "pressure" , true);
166+ if (error )
167+ return error ;
172168
173- st -> ch [ idx ] = GRTS_CH_PRESSURE ;
169+ if ( st -> ch_map [ GRTS_CH_PRESSURE ] < GRTS_MAX_CHANNELS ) {
174170 st -> pressure = true;
175-
176171 return 0 ;
177172 }
178173
179174 /* if no pressure is defined, try optional z1 + z2 */
180- idx = device_property_match_string (dev , "io-channel-names" , "z1" );
181- if (idx < 0 )
182- return 0 ;
183-
184- if (idx >= ARRAY_SIZE (st -> ch ))
185- return - EOVERFLOW ;
175+ error = grts_map_channel (st , dev , GRTS_CH_Z1 , "z1" , true);
176+ if (error )
177+ return error ;
186178
187- st -> ch [idx ] = GRTS_CH_Z1 ;
179+ if (st -> ch_map [GRTS_CH_Z1 ] >= GRTS_MAX_CHANNELS )
180+ return 0 ;
188181
189182 /* if z1 is provided z2 is not optional */
190- idx = device_property_match_string (dev , "io-channel-names" , "z2" );
191- if (idx < 0 )
192- return idx ;
193-
194- if (idx >= ARRAY_SIZE (st -> ch ))
195- return - EOVERFLOW ;
196-
197- st -> ch [idx ] = GRTS_CH_Z2 ;
198- st -> pressure = true;
183+ error = grts_map_channel (st , dev , GRTS_CH_Z2 , "z2" , true);
184+ if (error )
185+ return error ;
199186
200187 error = device_property_read_u32 (dev ,
201188 "touchscreen-x-plate-ohms" ,
@@ -205,6 +192,7 @@ static int grts_get_properties(struct grts_state *st, struct device *dev)
205192 return error ;
206193 }
207194
195+ st -> pressure = true;
208196 return 0 ;
209197}
210198
0 commit comments