Lines Matching full:encoder

11  * See file:Documentation/input/devices/rotary-encoder.rst for more information
26 #define DRV_NAME "rotary-encoder"
56 static unsigned int rotary_encoder_get_state(struct rotary_encoder *encoder) in rotary_encoder_get_state() argument
61 for (i = 0; i < encoder->gpios->ndescs; ++i) { in rotary_encoder_get_state()
62 int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]); in rotary_encoder_get_state()
65 if (encoder->encoding == ROTENC_GRAY && ret & 1) in rotary_encoder_get_state()
74 static void rotary_encoder_report_event(struct rotary_encoder *encoder) in rotary_encoder_report_event() argument
76 if (encoder->relative_axis) { in rotary_encoder_report_event()
77 input_report_rel(encoder->input, in rotary_encoder_report_event()
78 encoder->axis, encoder->dir); in rotary_encoder_report_event()
80 unsigned int pos = encoder->pos; in rotary_encoder_report_event()
82 if (encoder->dir < 0) { in rotary_encoder_report_event()
84 if (encoder->rollover) in rotary_encoder_report_event()
85 pos += encoder->steps; in rotary_encoder_report_event()
90 if (encoder->rollover || pos < encoder->steps) in rotary_encoder_report_event()
94 if (encoder->rollover) in rotary_encoder_report_event()
95 pos %= encoder->steps; in rotary_encoder_report_event()
97 encoder->pos = pos; in rotary_encoder_report_event()
98 input_report_abs(encoder->input, encoder->axis, encoder->pos); in rotary_encoder_report_event()
101 input_sync(encoder->input); in rotary_encoder_report_event()
106 struct rotary_encoder *encoder = dev_id; in rotary_encoder_irq() local
109 mutex_lock(&encoder->access_mutex); in rotary_encoder_irq()
111 state = rotary_encoder_get_state(encoder); in rotary_encoder_irq()
115 if (encoder->armed) { in rotary_encoder_irq()
116 rotary_encoder_report_event(encoder); in rotary_encoder_irq()
117 encoder->armed = false; in rotary_encoder_irq()
123 if (encoder->armed) in rotary_encoder_irq()
124 encoder->dir = 2 - state; in rotary_encoder_irq()
128 encoder->armed = true; in rotary_encoder_irq()
132 mutex_unlock(&encoder->access_mutex); in rotary_encoder_irq()
139 struct rotary_encoder *encoder = dev_id; in rotary_encoder_half_period_irq() local
142 mutex_lock(&encoder->access_mutex); in rotary_encoder_half_period_irq()
144 state = rotary_encoder_get_state(encoder); in rotary_encoder_half_period_irq()
147 encoder->dir = ((encoder->last_stable - state + 1) % 4) - 1; in rotary_encoder_half_period_irq()
149 if (state != encoder->last_stable) { in rotary_encoder_half_period_irq()
150 rotary_encoder_report_event(encoder); in rotary_encoder_half_period_irq()
151 encoder->last_stable = state; in rotary_encoder_half_period_irq()
155 mutex_unlock(&encoder->access_mutex); in rotary_encoder_half_period_irq()
162 struct rotary_encoder *encoder = dev_id; in rotary_encoder_quarter_period_irq() local
165 mutex_lock(&encoder->access_mutex); in rotary_encoder_quarter_period_irq()
167 state = rotary_encoder_get_state(encoder); in rotary_encoder_quarter_period_irq()
169 if ((encoder->last_stable + 1) % 4 == state) in rotary_encoder_quarter_period_irq()
170 encoder->dir = 1; in rotary_encoder_quarter_period_irq()
171 else if (encoder->last_stable == (state + 1) % 4) in rotary_encoder_quarter_period_irq()
172 encoder->dir = -1; in rotary_encoder_quarter_period_irq()
176 rotary_encoder_report_event(encoder); in rotary_encoder_quarter_period_irq()
179 encoder->last_stable = state; in rotary_encoder_quarter_period_irq()
180 mutex_unlock(&encoder->access_mutex); in rotary_encoder_quarter_period_irq()
188 struct rotary_encoder *encoder; in rotary_encoder_probe() local
195 encoder = devm_kzalloc(dev, sizeof(struct rotary_encoder), GFP_KERNEL); in rotary_encoder_probe()
196 if (!encoder) in rotary_encoder_probe()
199 mutex_init(&encoder->access_mutex); in rotary_encoder_probe()
201 device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps); in rotary_encoder_probe()
203 err = device_property_read_u32(dev, "rotary-encoder,steps-per-period", in rotary_encoder_probe()
214 "rotary-encoder,half-period") ? 2 : 1; in rotary_encoder_probe()
217 encoder->rollover = in rotary_encoder_probe()
218 device_property_read_bool(dev, "rotary-encoder,rollover"); in rotary_encoder_probe()
220 if (!device_property_present(dev, "rotary-encoder,encoding") || in rotary_encoder_probe()
221 !device_property_match_string(dev, "rotary-encoder,encoding", in rotary_encoder_probe()
224 encoder->encoding = ROTENC_GRAY; in rotary_encoder_probe()
225 } else if (!device_property_match_string(dev, "rotary-encoder,encoding", in rotary_encoder_probe()
228 encoder->encoding = ROTENC_BINARY; in rotary_encoder_probe()
234 device_property_read_u32(dev, "linux,axis", &encoder->axis); in rotary_encoder_probe()
235 encoder->relative_axis = in rotary_encoder_probe()
236 device_property_read_bool(dev, "rotary-encoder,relative-axis"); in rotary_encoder_probe()
238 encoder->gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN); in rotary_encoder_probe()
239 if (IS_ERR(encoder->gpios)) in rotary_encoder_probe()
240 return dev_err_probe(dev, PTR_ERR(encoder->gpios), "unable to get gpios\n"); in rotary_encoder_probe()
241 if (encoder->gpios->ndescs < 2) { in rotary_encoder_probe()
250 encoder->input = input; in rotary_encoder_probe()
255 if (encoder->relative_axis) in rotary_encoder_probe()
256 input_set_capability(input, EV_REL, encoder->axis); in rotary_encoder_probe()
259 encoder->axis, 0, encoder->steps, 0, 1); in rotary_encoder_probe()
261 switch (steps_per_period >> (encoder->gpios->ndescs - 2)) { in rotary_encoder_probe()
264 encoder->last_stable = rotary_encoder_get_state(encoder); in rotary_encoder_probe()
268 encoder->last_stable = rotary_encoder_get_state(encoder); in rotary_encoder_probe()
279 encoder->irq = in rotary_encoder_probe()
281 encoder->gpios->ndescs, sizeof(*encoder->irq), in rotary_encoder_probe()
283 if (!encoder->irq) in rotary_encoder_probe()
286 for (i = 0; i < encoder->gpios->ndescs; ++i) { in rotary_encoder_probe()
287 encoder->irq[i] = gpiod_to_irq(encoder->gpios->desc[i]); in rotary_encoder_probe()
289 err = devm_request_threaded_irq(dev, encoder->irq[i], in rotary_encoder_probe()
293 DRV_NAME, encoder); in rotary_encoder_probe()
296 encoder->irq[i], i); in rotary_encoder_probe()
310 platform_set_drvdata(pdev, encoder); in rotary_encoder_probe()
317 struct rotary_encoder *encoder = dev_get_drvdata(dev); in rotary_encoder_suspend() local
321 for (i = 0; i < encoder->gpios->ndescs; ++i) in rotary_encoder_suspend()
322 enable_irq_wake(encoder->irq[i]); in rotary_encoder_suspend()
330 struct rotary_encoder *encoder = dev_get_drvdata(dev); in rotary_encoder_resume() local
334 for (i = 0; i < encoder->gpios->ndescs; ++i) in rotary_encoder_resume()
335 disable_irq_wake(encoder->irq[i]); in rotary_encoder_resume()
346 { .compatible = "rotary-encoder", },
363 MODULE_DESCRIPTION("GPIO rotary encoder driver");