Lines Matching +full:diff +full:- +full:channels

1 // SPDX-License-Identifier: GPL-2.0-or-later
13 * There are 2 channels, currently this driver only supports channel 0 since
62 /* struct lradc_variant - Describe sun4i-a10-lradc-keys hardware variant
111 u32 i, ints, val, voltage, diff, keycode = 0, closest = 0xffffffff; in sun4i_lradc_irq() local
113 ints = readl(lradc->base + LRADC_INTS); in sun4i_lradc_irq()
121 input_report_key(lradc->input, lradc->chan0_keycode, 0); in sun4i_lradc_irq()
122 lradc->chan0_keycode = 0; in sun4i_lradc_irq()
125 if ((ints & CHAN0_KEYDOWN_IRQ) && lradc->chan0_keycode == 0) { in sun4i_lradc_irq()
126 val = readl(lradc->base + LRADC_DATA0) & 0x3f; in sun4i_lradc_irq()
127 voltage = val * lradc->vref / 63; in sun4i_lradc_irq()
129 for (i = 0; i < lradc->chan0_map_count; i++) { in sun4i_lradc_irq()
130 diff = abs(lradc->chan0_map[i].voltage - voltage); in sun4i_lradc_irq()
131 if (diff < closest) { in sun4i_lradc_irq()
132 closest = diff; in sun4i_lradc_irq()
133 keycode = lradc->chan0_map[i].keycode; in sun4i_lradc_irq()
137 lradc->chan0_keycode = keycode; in sun4i_lradc_irq()
138 input_report_key(lradc->input, lradc->chan0_keycode, 1); in sun4i_lradc_irq()
141 input_sync(lradc->input); in sun4i_lradc_irq()
143 writel(ints, lradc->base + LRADC_INTS); in sun4i_lradc_irq()
153 error = regulator_enable(lradc->vref_supply); in sun4i_lradc_open()
157 error = reset_control_deassert(lradc->reset); in sun4i_lradc_open()
161 error = clk_prepare_enable(lradc->clk); in sun4i_lradc_open()
165 lradc->vref = regulator_get_voltage(lradc->vref_supply) * in sun4i_lradc_open()
166 lradc->variant->divisor_numerator / in sun4i_lradc_open()
167 lradc->variant->divisor_denominator; in sun4i_lradc_open()
173 SAMPLE_RATE(0) | ENABLE(1), lradc->base + LRADC_CTRL); in sun4i_lradc_open()
175 writel(CHAN0_KEYUP_IRQ | CHAN0_KEYDOWN_IRQ, lradc->base + LRADC_INTC); in sun4i_lradc_open()
180 reset_control_assert(lradc->reset); in sun4i_lradc_open()
182 regulator_disable(lradc->vref_supply); in sun4i_lradc_open()
193 SAMPLE_RATE(2), lradc->base + LRADC_CTRL); in sun4i_lradc_close()
194 writel(0, lradc->base + LRADC_INTC); in sun4i_lradc_close()
196 clk_disable_unprepare(lradc->clk); in sun4i_lradc_close()
197 reset_control_assert(lradc->reset); in sun4i_lradc_close()
198 regulator_disable(lradc->vref_supply); in sun4i_lradc_close()
208 np = dev->of_node; in sun4i_lradc_load_dt_keymap()
210 return -EINVAL; in sun4i_lradc_load_dt_keymap()
212 lradc->chan0_map_count = of_get_child_count(np); in sun4i_lradc_load_dt_keymap()
213 if (lradc->chan0_map_count == 0) { in sun4i_lradc_load_dt_keymap()
215 return -EINVAL; in sun4i_lradc_load_dt_keymap()
218 lradc->chan0_map = devm_kmalloc_array(dev, lradc->chan0_map_count, in sun4i_lradc_load_dt_keymap()
221 if (!lradc->chan0_map) in sun4i_lradc_load_dt_keymap()
222 return -ENOMEM; in sun4i_lradc_load_dt_keymap()
226 struct sun4i_lradc_keymap *map = &lradc->chan0_map[i]; in sun4i_lradc_load_dt_keymap()
233 return -EINVAL; in sun4i_lradc_load_dt_keymap()
236 error = of_property_read_u32(pp, "voltage", &map->voltage); in sun4i_lradc_load_dt_keymap()
240 return -EINVAL; in sun4i_lradc_load_dt_keymap()
243 error = of_property_read_u32(pp, "linux,code", &map->keycode); in sun4i_lradc_load_dt_keymap()
247 return -EINVAL; in sun4i_lradc_load_dt_keymap()
259 struct device *dev = &pdev->dev; in sun4i_lradc_probe()
264 return -ENOMEM; in sun4i_lradc_probe()
270 lradc->variant = of_device_get_match_data(&pdev->dev); in sun4i_lradc_probe()
271 if (!lradc->variant) { in sun4i_lradc_probe()
272 dev_err(&pdev->dev, "Missing sun4i-a10-lradc-keys variant\n"); in sun4i_lradc_probe()
273 return -EINVAL; in sun4i_lradc_probe()
276 if (lradc->variant->has_clock_reset) { in sun4i_lradc_probe()
277 lradc->clk = devm_clk_get(dev, NULL); in sun4i_lradc_probe()
278 if (IS_ERR(lradc->clk)) in sun4i_lradc_probe()
279 return PTR_ERR(lradc->clk); in sun4i_lradc_probe()
281 lradc->reset = devm_reset_control_get_exclusive(dev, NULL); in sun4i_lradc_probe()
282 if (IS_ERR(lradc->reset)) in sun4i_lradc_probe()
283 return PTR_ERR(lradc->reset); in sun4i_lradc_probe()
286 lradc->vref_supply = devm_regulator_get(dev, "vref"); in sun4i_lradc_probe()
287 if (IS_ERR(lradc->vref_supply)) in sun4i_lradc_probe()
288 return PTR_ERR(lradc->vref_supply); in sun4i_lradc_probe()
290 lradc->dev = dev; in sun4i_lradc_probe()
291 lradc->input = devm_input_allocate_device(dev); in sun4i_lradc_probe()
292 if (!lradc->input) in sun4i_lradc_probe()
293 return -ENOMEM; in sun4i_lradc_probe()
295 lradc->input->name = pdev->name; in sun4i_lradc_probe()
296 lradc->input->phys = "sun4i_lradc/input0"; in sun4i_lradc_probe()
297 lradc->input->open = sun4i_lradc_open; in sun4i_lradc_probe()
298 lradc->input->close = sun4i_lradc_close; in sun4i_lradc_probe()
299 lradc->input->id.bustype = BUS_HOST; in sun4i_lradc_probe()
300 lradc->input->id.vendor = 0x0001; in sun4i_lradc_probe()
301 lradc->input->id.product = 0x0001; in sun4i_lradc_probe()
302 lradc->input->id.version = 0x0100; in sun4i_lradc_probe()
304 __set_bit(EV_KEY, lradc->input->evbit); in sun4i_lradc_probe()
305 for (i = 0; i < lradc->chan0_map_count; i++) in sun4i_lradc_probe()
306 __set_bit(lradc->chan0_map[i].keycode, lradc->input->keybit); in sun4i_lradc_probe()
308 input_set_drvdata(lradc->input, lradc); in sun4i_lradc_probe()
310 lradc->base = devm_ioremap_resource(dev, in sun4i_lradc_probe()
312 if (IS_ERR(lradc->base)) in sun4i_lradc_probe()
313 return PTR_ERR(lradc->base); in sun4i_lradc_probe()
320 "sun4i-a10-lradc-keys", lradc); in sun4i_lradc_probe()
324 error = input_register_device(lradc->input); in sun4i_lradc_probe()
328 if (device_property_read_bool(dev, "wakeup-source")) { in sun4i_lradc_probe()
342 { .compatible = "allwinner,sun4i-a10-lradc-keys",
344 { .compatible = "allwinner,sun8i-a83t-r-lradc",
346 { .compatible = "allwinner,sun50i-r329-lradc",
354 .name = "sun4i-a10-lradc-keys",