Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / rmi4 / rmi_f3a.c
blob0e8baed84dbb5a0e4dc2821ba0eefeecaf9057d2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2012-2020 Synaptics Incorporated
4 */
6 #include <linux/kernel.h>
7 #include <linux/rmi.h>
8 #include <linux/input.h>
9 #include <linux/slab.h>
10 #include "rmi_driver.h"
12 #define RMI_F3A_MAX_GPIO_COUNT 128
13 #define RMI_F3A_MAX_REG_SIZE DIV_ROUND_UP(RMI_F3A_MAX_GPIO_COUNT, 8)
15 /* Defs for Query 0 */
16 #define RMI_F3A_GPIO_COUNT 0x7F
18 #define RMI_F3A_DATA_REGS_MAX_SIZE RMI_F3A_MAX_REG_SIZE
20 #define TRACKSTICK_RANGE_START 3
21 #define TRACKSTICK_RANGE_END 6
23 struct f3a_data {
24 /* Query Data */
25 u8 gpio_count;
27 u8 register_count;
29 u8 data_regs[RMI_F3A_DATA_REGS_MAX_SIZE];
30 u16 *gpio_key_map;
32 struct input_dev *input;
34 struct rmi_function *f03;
35 bool trackstick_buttons;
38 static void rmi_f3a_report_button(struct rmi_function *fn,
39 struct f3a_data *f3a, unsigned int button)
41 u16 key_code = f3a->gpio_key_map[button];
42 bool key_down = !(f3a->data_regs[0] & BIT(button));
44 if (f3a->trackstick_buttons &&
45 button >= TRACKSTICK_RANGE_START &&
46 button <= TRACKSTICK_RANGE_END) {
47 rmi_f03_overwrite_button(f3a->f03, key_code, key_down);
48 } else {
49 rmi_dbg(RMI_DEBUG_FN, &fn->dev,
50 "%s: call input report key (0x%04x) value (0x%02x)",
51 __func__, key_code, key_down);
52 input_report_key(f3a->input, key_code, key_down);
56 static irqreturn_t rmi_f3a_attention(int irq, void *ctx)
58 struct rmi_function *fn = ctx;
59 struct f3a_data *f3a = dev_get_drvdata(&fn->dev);
60 struct rmi_driver_data *drvdata = dev_get_drvdata(&fn->rmi_dev->dev);
61 int error;
62 int i;
64 if (drvdata->attn_data.data) {
65 if (drvdata->attn_data.size < f3a->register_count) {
66 dev_warn(&fn->dev,
67 "F3A interrupted, but data is missing\n");
68 return IRQ_HANDLED;
70 memcpy(f3a->data_regs, drvdata->attn_data.data,
71 f3a->register_count);
72 drvdata->attn_data.data += f3a->register_count;
73 drvdata->attn_data.size -= f3a->register_count;
74 } else {
75 error = rmi_read_block(fn->rmi_dev, fn->fd.data_base_addr,
76 f3a->data_regs, f3a->register_count);
77 if (error) {
78 dev_err(&fn->dev,
79 "%s: Failed to read F3a data registers: %d\n",
80 __func__, error);
81 return IRQ_RETVAL(error);
85 for (i = 0; i < f3a->gpio_count; i++)
86 if (f3a->gpio_key_map[i] != KEY_RESERVED)
87 rmi_f3a_report_button(fn, f3a, i);
88 if (f3a->trackstick_buttons)
89 rmi_f03_commit_buttons(f3a->f03);
91 return IRQ_HANDLED;
94 static int rmi_f3a_config(struct rmi_function *fn)
96 struct f3a_data *f3a = dev_get_drvdata(&fn->dev);
97 struct rmi_driver *drv = fn->rmi_dev->driver;
98 const struct rmi_device_platform_data *pdata =
99 rmi_get_platform_data(fn->rmi_dev);
101 if (!f3a)
102 return 0;
104 if (pdata->gpio_data.trackstick_buttons) {
105 /* Try [re-]establish link to F03. */
106 f3a->f03 = rmi_find_function(fn->rmi_dev, 0x03);
107 f3a->trackstick_buttons = f3a->f03 != NULL;
110 drv->set_irq_bits(fn->rmi_dev, fn->irq_mask);
112 return 0;
115 static bool rmi_f3a_is_valid_button(int button, struct f3a_data *f3a,
116 u8 *query1_regs, u8 *ctrl1_regs)
118 /* gpio exist && direction input */
119 return (query1_regs[0] & BIT(button)) && !(ctrl1_regs[0] & BIT(button));
122 static int rmi_f3a_map_gpios(struct rmi_function *fn, struct f3a_data *f3a,
123 u8 *query1_regs, u8 *ctrl1_regs)
125 const struct rmi_device_platform_data *pdata =
126 rmi_get_platform_data(fn->rmi_dev);
127 struct input_dev *input = f3a->input;
128 unsigned int button = BTN_LEFT;
129 unsigned int trackstick_button = BTN_LEFT;
130 bool button_mapped = false;
131 int i;
132 int button_count = min_t(u8, f3a->gpio_count, TRACKSTICK_RANGE_END);
134 f3a->gpio_key_map = devm_kcalloc(&fn->dev,
135 button_count,
136 sizeof(f3a->gpio_key_map[0]),
137 GFP_KERNEL);
138 if (!f3a->gpio_key_map) {
139 dev_err(&fn->dev, "Failed to allocate gpio map memory.\n");
140 return -ENOMEM;
143 for (i = 0; i < button_count; i++) {
144 if (!rmi_f3a_is_valid_button(i, f3a, query1_regs, ctrl1_regs))
145 continue;
147 if (pdata->gpio_data.trackstick_buttons &&
148 i >= TRACKSTICK_RANGE_START &&
149 i < TRACKSTICK_RANGE_END) {
150 f3a->gpio_key_map[i] = trackstick_button++;
151 } else if (!pdata->gpio_data.buttonpad || !button_mapped) {
152 f3a->gpio_key_map[i] = button;
153 input_set_capability(input, EV_KEY, button++);
154 button_mapped = true;
157 input->keycode = f3a->gpio_key_map;
158 input->keycodesize = sizeof(f3a->gpio_key_map[0]);
159 input->keycodemax = f3a->gpio_count;
161 if (pdata->gpio_data.buttonpad || (button - BTN_LEFT == 1))
162 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
164 return 0;
167 static int rmi_f3a_initialize(struct rmi_function *fn, struct f3a_data *f3a)
169 u8 query1[RMI_F3A_MAX_REG_SIZE];
170 u8 ctrl1[RMI_F3A_MAX_REG_SIZE];
171 u8 buf;
172 int error;
174 error = rmi_read(fn->rmi_dev, fn->fd.query_base_addr, &buf);
175 if (error < 0) {
176 dev_err(&fn->dev, "Failed to read general info register: %d\n",
177 error);
178 return -ENODEV;
181 f3a->gpio_count = buf & RMI_F3A_GPIO_COUNT;
182 f3a->register_count = DIV_ROUND_UP(f3a->gpio_count, 8);
184 /* Query1 -> gpio exist */
185 error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr + 1,
186 query1, f3a->register_count);
187 if (error) {
188 dev_err(&fn->dev, "Failed to read query1 register\n");
189 return error;
192 /* Ctrl1 -> gpio direction */
193 error = rmi_read_block(fn->rmi_dev, fn->fd.control_base_addr + 1,
194 ctrl1, f3a->register_count);
195 if (error) {
196 dev_err(&fn->dev, "Failed to read control1 register\n");
197 return error;
200 error = rmi_f3a_map_gpios(fn, f3a, query1, ctrl1);
201 if (error)
202 return error;
204 return 0;
207 static int rmi_f3a_probe(struct rmi_function *fn)
209 struct rmi_device *rmi_dev = fn->rmi_dev;
210 struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
211 struct f3a_data *f3a;
212 int error;
214 if (!drv_data->input) {
215 dev_info(&fn->dev, "F3A: no input device found, ignoring\n");
216 return -ENXIO;
219 f3a = devm_kzalloc(&fn->dev, sizeof(*f3a), GFP_KERNEL);
220 if (!f3a)
221 return -ENOMEM;
223 f3a->input = drv_data->input;
225 error = rmi_f3a_initialize(fn, f3a);
226 if (error)
227 return error;
229 dev_set_drvdata(&fn->dev, f3a);
230 return 0;
233 struct rmi_function_handler rmi_f3a_handler = {
234 .driver = {
235 .name = "rmi4_f3a",
237 .func = 0x3a,
238 .probe = rmi_f3a_probe,
239 .config = rmi_f3a_config,
240 .attention = rmi_f3a_attention,