f2fs: fix to recover inode's i_flags during POR
[linux/fpc-iii.git] / drivers / input / rmi4 / rmi_f03.c
blobaaa1edc9552254609c1e2ba00008b48bf80f3a85
1 /*
2 * Copyright (C) 2015-2016 Red Hat
3 * Copyright (C) 2015 Lyude Paul <thatslyude@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/serio.h>
13 #include <linux/notifier.h>
14 #include "rmi_driver.h"
16 #define RMI_F03_RX_DATA_OFB 0x01
17 #define RMI_F03_OB_SIZE 2
19 #define RMI_F03_OB_OFFSET 2
20 #define RMI_F03_OB_DATA_OFFSET 1
21 #define RMI_F03_OB_FLAG_TIMEOUT BIT(6)
22 #define RMI_F03_OB_FLAG_PARITY BIT(7)
24 #define RMI_F03_DEVICE_COUNT 0x07
25 #define RMI_F03_BYTES_PER_DEVICE 0x07
26 #define RMI_F03_BYTES_PER_DEVICE_SHIFT 4
27 #define RMI_F03_QUEUE_LENGTH 0x0F
29 #define PSMOUSE_OOB_EXTRA_BTNS 0x01
31 struct f03_data {
32 struct rmi_function *fn;
34 struct serio *serio;
35 bool serio_registered;
37 unsigned int overwrite_buttons;
39 u8 device_count;
40 u8 rx_queue_length;
43 int rmi_f03_overwrite_button(struct rmi_function *fn, unsigned int button,
44 int value)
46 struct f03_data *f03 = dev_get_drvdata(&fn->dev);
47 unsigned int bit;
49 if (button < BTN_LEFT || button > BTN_MIDDLE)
50 return -EINVAL;
52 bit = BIT(button - BTN_LEFT);
54 if (value)
55 f03->overwrite_buttons |= bit;
56 else
57 f03->overwrite_buttons &= ~bit;
59 return 0;
62 void rmi_f03_commit_buttons(struct rmi_function *fn)
64 struct f03_data *f03 = dev_get_drvdata(&fn->dev);
65 struct serio *serio = f03->serio;
67 serio_pause_rx(serio);
68 if (serio->drv) {
69 serio->drv->interrupt(serio, PSMOUSE_OOB_EXTRA_BTNS,
70 SERIO_OOB_DATA);
71 serio->drv->interrupt(serio, f03->overwrite_buttons,
72 SERIO_OOB_DATA);
74 serio_continue_rx(serio);
77 static int rmi_f03_pt_write(struct serio *id, unsigned char val)
79 struct f03_data *f03 = id->port_data;
80 int error;
82 rmi_dbg(RMI_DEBUG_FN, &f03->fn->dev,
83 "%s: Wrote %.2hhx to PS/2 passthrough address",
84 __func__, val);
86 error = rmi_write(f03->fn->rmi_dev, f03->fn->fd.data_base_addr, val);
87 if (error) {
88 dev_err(&f03->fn->dev,
89 "%s: Failed to write to F03 TX register (%d).\n",
90 __func__, error);
91 return error;
94 return 0;
97 static int rmi_f03_initialize(struct f03_data *f03)
99 struct rmi_function *fn = f03->fn;
100 struct device *dev = &fn->dev;
101 int error;
102 u8 bytes_per_device;
103 u8 query1;
104 u8 query2[RMI_F03_DEVICE_COUNT * RMI_F03_BYTES_PER_DEVICE];
105 size_t query2_len;
107 error = rmi_read(fn->rmi_dev, fn->fd.query_base_addr, &query1);
108 if (error) {
109 dev_err(dev, "Failed to read query register (%d).\n", error);
110 return error;
113 f03->device_count = query1 & RMI_F03_DEVICE_COUNT;
114 bytes_per_device = (query1 >> RMI_F03_BYTES_PER_DEVICE_SHIFT) &
115 RMI_F03_BYTES_PER_DEVICE;
117 query2_len = f03->device_count * bytes_per_device;
120 * The first generation of image sensors don't have a second part to
121 * their f03 query, as such we have to set some of these values manually
123 if (query2_len < 1) {
124 f03->device_count = 1;
125 f03->rx_queue_length = 7;
126 } else {
127 error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr + 1,
128 query2, query2_len);
129 if (error) {
130 dev_err(dev,
131 "Failed to read second set of query registers (%d).\n",
132 error);
133 return error;
136 f03->rx_queue_length = query2[0] & RMI_F03_QUEUE_LENGTH;
139 return 0;
142 static int rmi_f03_pt_open(struct serio *serio)
144 struct f03_data *f03 = serio->port_data;
145 struct rmi_function *fn = f03->fn;
146 const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
147 const u16 data_addr = fn->fd.data_base_addr + RMI_F03_OB_OFFSET;
148 u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
149 int error;
152 * Consume any pending data. Some devices like to spam with
153 * 0xaa 0x00 announcements which may confuse us as we try to
154 * probe the device.
156 error = rmi_read_block(fn->rmi_dev, data_addr, &obs, ob_len);
157 if (!error)
158 rmi_dbg(RMI_DEBUG_FN, &fn->dev,
159 "%s: Consumed %*ph (%d) from PS2 guest\n",
160 __func__, ob_len, obs, ob_len);
162 return fn->rmi_dev->driver->set_irq_bits(fn->rmi_dev, fn->irq_mask);
165 static void rmi_f03_pt_close(struct serio *serio)
167 struct f03_data *f03 = serio->port_data;
168 struct rmi_function *fn = f03->fn;
170 fn->rmi_dev->driver->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
173 static int rmi_f03_register_pt(struct f03_data *f03)
175 struct serio *serio;
177 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
178 if (!serio)
179 return -ENOMEM;
181 serio->id.type = SERIO_PS_PSTHRU;
182 serio->write = rmi_f03_pt_write;
183 serio->open = rmi_f03_pt_open;
184 serio->close = rmi_f03_pt_close;
185 serio->port_data = f03;
187 strlcpy(serio->name, "RMI4 PS/2 pass-through", sizeof(serio->name));
188 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0",
189 dev_name(&f03->fn->dev));
190 serio->dev.parent = &f03->fn->dev;
192 f03->serio = serio;
194 printk(KERN_INFO "serio: %s port at %s\n",
195 serio->name, dev_name(&f03->fn->dev));
196 serio_register_port(serio);
198 return 0;
201 static int rmi_f03_probe(struct rmi_function *fn)
203 struct device *dev = &fn->dev;
204 struct f03_data *f03;
205 int error;
207 f03 = devm_kzalloc(dev, sizeof(struct f03_data), GFP_KERNEL);
208 if (!f03)
209 return -ENOMEM;
211 f03->fn = fn;
213 error = rmi_f03_initialize(f03);
214 if (error < 0)
215 return error;
217 if (f03->device_count != 1)
218 dev_warn(dev, "found %d devices on PS/2 passthrough",
219 f03->device_count);
221 dev_set_drvdata(dev, f03);
222 return 0;
225 static int rmi_f03_config(struct rmi_function *fn)
227 struct f03_data *f03 = dev_get_drvdata(&fn->dev);
228 int error;
230 if (!f03->serio_registered) {
231 error = rmi_f03_register_pt(f03);
232 if (error)
233 return error;
235 f03->serio_registered = true;
236 } else {
238 * We must be re-configuring the sensor, just enable
239 * interrupts for this function.
241 fn->rmi_dev->driver->set_irq_bits(fn->rmi_dev, fn->irq_mask);
244 return 0;
247 static irqreturn_t rmi_f03_attention(int irq, void *ctx)
249 struct rmi_function *fn = ctx;
250 struct rmi_device *rmi_dev = fn->rmi_dev;
251 struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
252 struct f03_data *f03 = dev_get_drvdata(&fn->dev);
253 const u16 data_addr = fn->fd.data_base_addr + RMI_F03_OB_OFFSET;
254 const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
255 u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
256 u8 ob_status;
257 u8 ob_data;
258 unsigned int serio_flags;
259 int i;
260 int error;
262 if (drvdata->attn_data.data) {
263 /* First grab the data passed by the transport device */
264 if (drvdata->attn_data.size < ob_len) {
265 dev_warn(&fn->dev, "F03 interrupted, but data is missing!\n");
266 return IRQ_HANDLED;
269 memcpy(obs, drvdata->attn_data.data, ob_len);
271 drvdata->attn_data.data += ob_len;
272 drvdata->attn_data.size -= ob_len;
273 } else {
274 /* Grab all of the data registers, and check them for data */
275 error = rmi_read_block(fn->rmi_dev, data_addr, &obs, ob_len);
276 if (error) {
277 dev_err(&fn->dev,
278 "%s: Failed to read F03 output buffers: %d\n",
279 __func__, error);
280 serio_interrupt(f03->serio, 0, SERIO_TIMEOUT);
281 return IRQ_RETVAL(error);
285 for (i = 0; i < ob_len; i += RMI_F03_OB_SIZE) {
286 ob_status = obs[i];
287 ob_data = obs[i + RMI_F03_OB_DATA_OFFSET];
288 serio_flags = 0;
290 if (!(ob_status & RMI_F03_RX_DATA_OFB))
291 continue;
293 if (ob_status & RMI_F03_OB_FLAG_TIMEOUT)
294 serio_flags |= SERIO_TIMEOUT;
295 if (ob_status & RMI_F03_OB_FLAG_PARITY)
296 serio_flags |= SERIO_PARITY;
298 rmi_dbg(RMI_DEBUG_FN, &fn->dev,
299 "%s: Received %.2hhx from PS2 guest T: %c P: %c\n",
300 __func__, ob_data,
301 serio_flags & SERIO_TIMEOUT ? 'Y' : 'N',
302 serio_flags & SERIO_PARITY ? 'Y' : 'N');
304 serio_interrupt(f03->serio, ob_data, serio_flags);
307 return IRQ_HANDLED;
310 static void rmi_f03_remove(struct rmi_function *fn)
312 struct f03_data *f03 = dev_get_drvdata(&fn->dev);
314 if (f03->serio_registered)
315 serio_unregister_port(f03->serio);
318 struct rmi_function_handler rmi_f03_handler = {
319 .driver = {
320 .name = "rmi4_f03",
322 .func = 0x03,
323 .probe = rmi_f03_probe,
324 .config = rmi_f03_config,
325 .attention = rmi_f03_attention,
326 .remove = rmi_f03_remove,
329 MODULE_AUTHOR("Lyude Paul <thatslyude@gmail.com>");
330 MODULE_DESCRIPTION("RMI F03 module");
331 MODULE_LICENSE("GPL");