drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / input / keyboard / clps711x-keypad.c
blob4c1a3e611edd7490c5401ee4e39cc16f9fb4459b
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Cirrus Logic CLPS711X Keypad driver
5 * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
6 */
8 #include <linux/input.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/platform_device.h>
13 #include <linux/property.h>
14 #include <linux/regmap.h>
15 #include <linux/sched.h>
16 #include <linux/input/matrix_keypad.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/mfd/syscon/clps711x.h>
20 #define CLPS711X_KEYPAD_COL_COUNT 8
22 struct clps711x_gpio_data {
23 struct gpio_desc *desc;
24 DECLARE_BITMAP(last_state, CLPS711X_KEYPAD_COL_COUNT);
27 struct clps711x_keypad_data {
28 struct regmap *syscon;
29 int row_count;
30 unsigned int row_shift;
31 struct clps711x_gpio_data *gpio_data;
34 static void clps711x_keypad_poll(struct input_dev *input)
36 const unsigned short *keycodes = input->keycode;
37 struct clps711x_keypad_data *priv = input_get_drvdata(input);
38 bool sync = false;
39 int col, row;
41 for (col = 0; col < CLPS711X_KEYPAD_COL_COUNT; col++) {
42 /* Assert column */
43 regmap_update_bits(priv->syscon, SYSCON_OFFSET,
44 SYSCON1_KBDSCAN_MASK,
45 SYSCON1_KBDSCAN(8 + col));
47 /* Scan rows */
48 for (row = 0; row < priv->row_count; row++) {
49 struct clps711x_gpio_data *data = &priv->gpio_data[row];
50 bool state, state1;
52 /* Read twice for protection against fluctuations */
53 do {
54 state = gpiod_get_value_cansleep(data->desc);
55 cond_resched();
56 state1 = gpiod_get_value_cansleep(data->desc);
57 } while (state != state1);
59 if (test_bit(col, data->last_state) != state) {
60 int code = MATRIX_SCAN_CODE(row, col,
61 priv->row_shift);
63 if (state) {
64 set_bit(col, data->last_state);
65 input_event(input,
66 EV_MSC, MSC_SCAN, code);
67 } else {
68 clear_bit(col, data->last_state);
71 if (keycodes[code])
72 input_report_key(input,
73 keycodes[code], state);
74 sync = true;
78 /* Set all columns to low */
79 regmap_update_bits(priv->syscon, SYSCON_OFFSET,
80 SYSCON1_KBDSCAN_MASK, SYSCON1_KBDSCAN(1));
83 if (sync)
84 input_sync(input);
87 static int clps711x_keypad_probe(struct platform_device *pdev)
89 struct clps711x_keypad_data *priv;
90 struct device *dev = &pdev->dev;
91 struct input_dev *input;
92 u32 poll_interval;
93 int i, err;
95 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
96 if (!priv)
97 return -ENOMEM;
99 priv->syscon = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
100 if (IS_ERR(priv->syscon))
101 return PTR_ERR(priv->syscon);
103 priv->row_count = gpiod_count(dev, "row");
104 if (priv->row_count < 1)
105 return -EINVAL;
107 priv->gpio_data = devm_kcalloc(dev,
108 priv->row_count, sizeof(*priv->gpio_data),
109 GFP_KERNEL);
110 if (!priv->gpio_data)
111 return -ENOMEM;
113 priv->row_shift = get_count_order(CLPS711X_KEYPAD_COL_COUNT);
115 for (i = 0; i < priv->row_count; i++) {
116 struct clps711x_gpio_data *data = &priv->gpio_data[i];
118 data->desc = devm_gpiod_get_index(dev, "row", i, GPIOD_IN);
119 if (IS_ERR(data->desc))
120 return PTR_ERR(data->desc);
123 err = device_property_read_u32(dev, "poll-interval", &poll_interval);
124 if (err)
125 return err;
127 input = devm_input_allocate_device(dev);
128 if (!input)
129 return -ENOMEM;
131 input_set_drvdata(input, priv);
133 input->name = pdev->name;
134 input->dev.parent = dev;
135 input->id.bustype = BUS_HOST;
136 input->id.vendor = 0x0001;
137 input->id.product = 0x0001;
138 input->id.version = 0x0100;
140 err = matrix_keypad_build_keymap(NULL, NULL, priv->row_count,
141 CLPS711X_KEYPAD_COL_COUNT,
142 NULL, input);
143 if (err)
144 return err;
146 input_set_capability(input, EV_MSC, MSC_SCAN);
147 if (device_property_read_bool(dev, "autorepeat"))
148 __set_bit(EV_REP, input->evbit);
150 /* Set all columns to low */
151 regmap_update_bits(priv->syscon, SYSCON_OFFSET, SYSCON1_KBDSCAN_MASK,
152 SYSCON1_KBDSCAN(1));
155 err = input_setup_polling(input, clps711x_keypad_poll);
156 if (err)
157 return err;
159 input_set_poll_interval(input, poll_interval);
161 err = input_register_device(input);
162 if (err)
163 return err;
165 return 0;
168 static const struct of_device_id clps711x_keypad_of_match[] = {
169 { .compatible = "cirrus,ep7209-keypad", },
172 MODULE_DEVICE_TABLE(of, clps711x_keypad_of_match);
174 static struct platform_driver clps711x_keypad_driver = {
175 .driver = {
176 .name = "clps711x-keypad",
177 .of_match_table = clps711x_keypad_of_match,
179 .probe = clps711x_keypad_probe,
181 module_platform_driver(clps711x_keypad_driver);
183 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
184 MODULE_DESCRIPTION("Cirrus Logic CLPS711X Keypad driver");
185 MODULE_LICENSE("GPL");