Lines Matching +full:int +full:- +full:gpios
1 // SPDX-License-Identifier: GPL-2.0-only
11 * See file:Documentation/input/devices/rotary-encoder.rst for more information
26 #define DRV_NAME "rotary-encoder"
44 unsigned int pos;
46 struct gpio_descs *gpios; member
48 unsigned int *irq;
51 signed char dir; /* 1 - clockwise, -1 - CCW */
53 unsigned int last_stable;
56 static unsigned int rotary_encoder_get_state(struct rotary_encoder *encoder) in rotary_encoder_get_state()
58 int i; in rotary_encoder_get_state()
59 unsigned int ret = 0; in rotary_encoder_get_state()
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()
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()
83 /* turning counter-clockwise */ in rotary_encoder_report_event()
84 if (encoder->rollover) in rotary_encoder_report_event()
85 pos += encoder->steps; in rotary_encoder_report_event()
87 pos--; 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()
104 static irqreturn_t rotary_encoder_irq(int irq, void *dev_id) in rotary_encoder_irq()
107 unsigned int state; in rotary_encoder_irq()
109 mutex_lock(&encoder->access_mutex); in rotary_encoder_irq()
115 if (encoder->armed) { 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()
137 static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id) in rotary_encoder_half_period_irq()
140 unsigned int state; in rotary_encoder_half_period_irq()
142 mutex_lock(&encoder->access_mutex); 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()
151 encoder->last_stable = state; in rotary_encoder_half_period_irq()
155 mutex_unlock(&encoder->access_mutex); in rotary_encoder_half_period_irq()
160 static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id) in rotary_encoder_quarter_period_irq()
163 unsigned int state; in rotary_encoder_quarter_period_irq()
165 mutex_lock(&encoder->access_mutex); 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()
179 encoder->last_stable = state; in rotary_encoder_quarter_period_irq()
180 mutex_unlock(&encoder->access_mutex); in rotary_encoder_quarter_period_irq()
185 static int rotary_encoder_probe(struct platform_device *pdev) in rotary_encoder_probe()
187 struct device *dev = &pdev->dev; in rotary_encoder_probe()
192 unsigned int i; in rotary_encoder_probe()
193 int err; in rotary_encoder_probe()
197 return -ENOMEM; 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()
207 * The 'half-period' property has been deprecated, you must in rotary_encoder_probe()
208 * use 'steps-per-period' and set an appropriate value, but 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()
231 return -EINVAL; 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 err = PTR_ERR(encoder->gpios); in rotary_encoder_probe()
241 if (err != -EPROBE_DEFER) in rotary_encoder_probe()
242 dev_err(dev, "unable to get gpios: %d\n", err); in rotary_encoder_probe()
245 if (encoder->gpios->ndescs < 2) { in rotary_encoder_probe()
246 dev_err(dev, "not enough gpios found\n"); in rotary_encoder_probe()
247 return -EINVAL; in rotary_encoder_probe()
252 return -ENOMEM; in rotary_encoder_probe()
254 encoder->input = input; in rotary_encoder_probe()
256 input->name = pdev->name; in rotary_encoder_probe()
257 input->id.bustype = BUS_HOST; in rotary_encoder_probe()
258 input->dev.parent = dev; in rotary_encoder_probe()
260 if (encoder->relative_axis) in rotary_encoder_probe()
261 input_set_capability(input, EV_REL, encoder->axis); in rotary_encoder_probe()
264 encoder->axis, 0, encoder->steps, 0, 1); in rotary_encoder_probe()
266 switch (steps_per_period >> (encoder->gpios->ndescs - 2)) { in rotary_encoder_probe()
269 encoder->last_stable = rotary_encoder_get_state(encoder); in rotary_encoder_probe()
273 encoder->last_stable = rotary_encoder_get_state(encoder); in rotary_encoder_probe()
279 dev_err(dev, "'%d' is not a valid steps-per-period value\n", in rotary_encoder_probe()
281 return -EINVAL; in rotary_encoder_probe()
284 encoder->irq = in rotary_encoder_probe()
286 encoder->gpios->ndescs, sizeof(*encoder->irq), in rotary_encoder_probe()
288 if (!encoder->irq) in rotary_encoder_probe()
289 return -ENOMEM; in rotary_encoder_probe()
291 for (i = 0; i < encoder->gpios->ndescs; ++i) { in rotary_encoder_probe()
292 encoder->irq[i] = gpiod_to_irq(encoder->gpios->desc[i]); in rotary_encoder_probe()
294 err = devm_request_threaded_irq(dev, encoder->irq[i], in rotary_encoder_probe()
301 encoder->irq[i], i); in rotary_encoder_probe()
313 device_property_read_bool(dev, "wakeup-source")); in rotary_encoder_probe()
320 static int __maybe_unused rotary_encoder_suspend(struct device *dev) in rotary_encoder_suspend()
323 unsigned int i; in rotary_encoder_suspend()
326 for (i = 0; i < encoder->gpios->ndescs; ++i) in rotary_encoder_suspend()
327 enable_irq_wake(encoder->irq[i]); in rotary_encoder_suspend()
333 static int __maybe_unused rotary_encoder_resume(struct device *dev) in rotary_encoder_resume()
336 unsigned int i; in rotary_encoder_resume()
339 for (i = 0; i < encoder->gpios->ndescs; ++i) in rotary_encoder_resume()
340 disable_irq_wake(encoder->irq[i]); in rotary_encoder_resume()
351 { .compatible = "rotary-encoder", },