aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/touchscreen/rmi4/rmi_driver.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/touchscreen/rmi4/rmi_driver.c')
-rw-r--r--drivers/input/touchscreen/rmi4/rmi_driver.c1354
1 files changed, 1354 insertions, 0 deletions
diff --git a/drivers/input/touchscreen/rmi4/rmi_driver.c b/drivers/input/touchscreen/rmi4/rmi_driver.c
new file mode 100644
index 00000000000..6aeb3b93b2c
--- /dev/null
+++ b/drivers/input/touchscreen/rmi4/rmi_driver.c
@@ -0,0 +1,1354 @@
1/*
2 * Copyright (c) 2011 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This driver adds support for generic RMI4 devices from Synpatics. It
6 * implements the mandatory f01 RMI register and depends on the presence of
7 * other required RMI functions.
8 *
9 * The RMI4 specification can be found here (URL split after files/ for
10 * style reasons):
11 * http://www.synaptics.com/sites/default/files/
12 * 511-000136-01-Rev-E-RMI4%20Intrfacing%20Guide.pdf
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/kernel.h>
30#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/module.h>
33#include <linux/device.h>
34#include <linux/slab.h>
35#include <linux/list.h>
36#include <linux/pm.h>
37#include <linux/gpio.h>
38#include <linux/workqueue.h>
39#include <linux/rmi.h>
40#include "rmi_driver.h"
41
42#define DELAY_DEBUG 0
43#define REGISTER_DEBUG 0
44
45#define PDT_END_SCAN_LOCATION 0x0005
46#define PDT_PROPERTIES_LOCATION 0x00EF
47#define BSR_LOCATION 0x00FE
48#define HAS_BSR_MASK 0x20
49#define HAS_NONSTANDARD_PDT_MASK 0x40
50#define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff)
51#define RMI4_MAX_PAGE 0xff
52#define RMI4_PAGE_SIZE 0x100
53
54#define RMI_DEVICE_RESET_CMD 0x01
55#define INITIAL_RESET_WAIT_MS 20
56
57#ifdef CONFIG_HAS_EARLYSUSPEND
58static void rmi_driver_early_suspend(struct early_suspend *h);
59static void rmi_driver_late_resume(struct early_suspend *h);
60#endif
61
62
63/* sysfs files for attributes for driver values. */
64static ssize_t rmi_driver_hasbsr_show(struct device *dev,
65 struct device_attribute *attr, char *buf);
66
67static ssize_t rmi_driver_bsr_show(struct device *dev,
68 struct device_attribute *attr, char *buf);
69
70static ssize_t rmi_driver_bsr_store(struct device *dev,
71 struct device_attribute *attr,
72 const char *buf, size_t count);
73
74static ssize_t rmi_driver_enabled_show(struct device *dev,
75 struct device_attribute *attr,
76 char *buf);
77
78static ssize_t rmi_driver_enabled_store(struct device *dev,
79 struct device_attribute *attr,
80 const char *buf, size_t count);
81
82static ssize_t rmi_driver_phys_show(struct device *dev,
83 struct device_attribute *attr,
84 char *buf);
85
86static ssize_t rmi_driver_version_show(struct device *dev,
87 struct device_attribute *attr,
88 char *buf);
89
90#if REGISTER_DEBUG
91static ssize_t rmi_driver_reg_store(struct device *dev,
92 struct device_attribute *attr,
93 const char *buf, size_t count);
94#endif
95
96#if DELAY_DEBUG
97static ssize_t rmi_delay_show(struct device *dev,
98 struct device_attribute *attr,
99 char *buf);
100
101static ssize_t rmi_delay_store(struct device *dev,
102 struct device_attribute *attr,
103 const char *buf, size_t count);
104#endif
105
106static struct device_attribute attrs[] = {
107 __ATTR(hasbsr, RMI_RO_ATTR,
108 rmi_driver_hasbsr_show, rmi_store_error),
109 __ATTR(bsr, RMI_RW_ATTR,
110 rmi_driver_bsr_show, rmi_driver_bsr_store),
111 __ATTR(enabled, RMI_RW_ATTR,
112 rmi_driver_enabled_show, rmi_driver_enabled_store),
113 __ATTR(phys, RMI_RO_ATTR,
114 rmi_driver_phys_show, rmi_store_error),
115#if REGISTER_DEBUG
116 __ATTR(reg, RMI_WO_ATTR,
117 rmi_show_error, rmi_driver_reg_store),
118#endif
119#if DELAY_DEBUG
120 __ATTR(delay, RMI_RW_ATTR,
121 rmi_delay_show, rmi_delay_store),
122#endif
123 __ATTR(version, RMI_RO_ATTR,
124 rmi_driver_version_show, rmi_store_error),
125};
126
127
128/*
129** ONLY needed for POLLING mode of the driver
130*/
131struct rmi_device *polled_synaptics_rmi_device = NULL;
132EXPORT_SYMBOL(polled_synaptics_rmi_device);
133
134/* Useful helper functions for u8* */
135
136void u8_set_bit(u8 *target, int pos)
137{
138 target[pos/8] |= 1<<pos%8;
139}
140
141void u8_clear_bit(u8 *target, int pos)
142{
143 target[pos/8] &= ~(1<<pos%8);
144}
145
146bool u8_is_set(u8 *target, int pos)
147{
148 return target[pos/8] & 1<<pos%8;
149}
150
151bool u8_is_any_set(u8 *target, int size)
152{
153 int i;
154 for (i = 0; i < size; i++) {
155 if (target[i])
156 return true;
157 }
158 return false;
159}
160
161void u8_or(u8 *dest, u8 *target1, u8 *target2, int size)
162{
163 int i;
164 for (i = 0; i < size; i++)
165 dest[i] = target1[i] | target2[i];
166}
167
168void u8_and(u8 *dest, u8 *target1, u8 *target2, int size)
169{
170 int i;
171 for (i = 0; i < size; i++)
172 dest[i] = target1[i] & target2[i];
173}
174
175/* Helper fn to convert a byte array representing a short in the RMI
176 * endian-ness to a short in the native processor's specific endianness.
177 * We don't use ntohs/htons here because, well, we're not dealing with
178 * a pair of shorts. And casting dest to short* wouldn't work, because
179 * that would imply knowing the byte order of short in the first place.
180 */
181void batohs(unsigned short *dest, unsigned char *src)
182{
183 *dest = src[1] * 0x100 + src[0];
184}
185
186/* Helper function to convert a short (in host processor endianess) to
187 * a byte array in the RMI endianess for shorts. See above comment for
188 * why we dont us htons or something like that.
189 */
190void hstoba(unsigned char *dest, unsigned short src)
191{
192 dest[0] = src % 0x100;
193 dest[1] = src / 0x100;
194}
195
196static bool has_bsr(struct rmi_driver_data *data)
197{
198 return (data->pdt_props & HAS_BSR_MASK) != 0;
199}
200
201/* Utility routine to set bits in a register. */
202int rmi_set_bits(struct rmi_device *rmi_dev, unsigned short address,
203 unsigned char bits)
204{
205 unsigned char reg_contents;
206 int retval;
207
208 retval = rmi_read_block(rmi_dev, address, &reg_contents, 1);
209 if (retval)
210 return retval;
211 reg_contents = reg_contents | bits;
212 retval = rmi_write_block(rmi_dev, address, &reg_contents, 1);
213 if (retval == 1)
214 return 0;
215 else if (retval == 0)
216 return -EIO;
217 return retval;
218}
219EXPORT_SYMBOL(rmi_set_bits);
220
221/* Utility routine to clear bits in a register. */
222int rmi_clear_bits(struct rmi_device *rmi_dev, unsigned short address,
223 unsigned char bits)
224{
225 unsigned char reg_contents;
226 int retval;
227
228 retval = rmi_read_block(rmi_dev, address, &reg_contents, 1);
229 if (retval)
230 return retval;
231 reg_contents = reg_contents & ~bits;
232 retval = rmi_write_block(rmi_dev, address, &reg_contents, 1);
233 if (retval == 1)
234 return 0;
235 else if (retval == 0)
236 return -EIO;
237 return retval;
238}
239EXPORT_SYMBOL(rmi_clear_bits);
240
241static void rmi_free_function_list(struct rmi_device *rmi_dev)
242{
243 struct rmi_function_container *entry, *n;
244 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
245
246 list_for_each_entry_safe(entry, n, &data->rmi_functions.list, list) {
247 kfree(entry->irq_mask);
248 list_del(&entry->list);
249 }
250}
251
252static int init_one_function(struct rmi_device *rmi_dev,
253 struct rmi_function_container *fc)
254{
255 int retval;
256
257 dev_info(&rmi_dev->dev, "Initializing F%02X.\n",
258 fc->fd.function_number);
259 dev_dbg(&rmi_dev->dev, "Initializing F%02X.\n",
260 fc->fd.function_number);
261
262 if (!fc->fh) {
263 struct rmi_function_handler *fh =
264 rmi_get_function_handler(fc->fd.function_number);
265 if (!fh) {
266 dev_dbg(&rmi_dev->dev, "No handler for F%02X.\n",
267 fc->fd.function_number);
268 return 0;
269 }
270 fc->fh = fh;
271 }
272
273 if (!fc->fh->init)
274 return 0;
275
276 dev_set_name(&(fc->dev), "fn%02x", fc->fd.function_number);
277
278 fc->dev.parent = &rmi_dev->dev;
279
280 retval = device_register(&fc->dev);
281 if (retval) {
282 dev_err(&rmi_dev->dev, "Failed device_register for F%02X.\n",
283 fc->fd.function_number);
284 return retval;
285 }
286
287 retval = fc->fh->init(fc);
288 if (retval < 0) {
289 dev_err(&rmi_dev->dev, "Failed to initialize function F%02x\n",
290 fc->fd.function_number);
291 goto error_exit;
292 }
293
294 return 0;
295
296error_exit:
297 device_unregister(&fc->dev);
298 return retval;
299}
300
301static void rmi_driver_fh_add(struct rmi_device *rmi_dev,
302 struct rmi_function_handler *fh)
303{
304 struct rmi_function_container *entry;
305 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
306
307 if (fh->func == 0x01)
308 data->f01_container->fh = fh;
309 else {
310 list_for_each_entry(entry, &data->rmi_functions.list, list)
311 if (entry->fd.function_number == fh->func) {
312 entry->fh = fh;
313 init_one_function(rmi_dev, entry);
314 }
315 }
316
317}
318
319static void rmi_driver_fh_remove(struct rmi_device *rmi_dev,
320 struct rmi_function_handler *fh)
321{
322 struct rmi_function_container *entry;
323 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
324
325 list_for_each_entry(entry, &data->rmi_functions.list, list)
326 if (entry->fd.function_number == fh->func) {
327 if (fh->remove)
328 fh->remove(entry);
329
330 entry->fh = NULL;
331 }
332}
333
334static void construct_mask(u8 *mask, int num, int pos)
335{
336 int i;
337
338 for (i = 0; i < num; i++)
339 u8_set_bit(mask, pos+i);
340}
341
342static int process_interrupt_requests(struct rmi_device *rmi_dev)
343{
344 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
345 struct device *dev = &rmi_dev->dev;
346 struct rmi_function_container *entry;
347 u8 irq_status[data->num_of_irq_regs];
348 u8 irq_bits[data->num_of_irq_regs];
349 int error;
350
351 error = rmi_read_block(rmi_dev,
352 data->f01_container->fd.data_base_addr + 1,
353 irq_status, data->num_of_irq_regs);
354 if (error < 0) {
355 dev_err(dev, "%s: failed to read irqs.", __func__);
356 return error;
357 }
358 /* Device control (F01) is handled before anything else. */
359 u8_and(irq_bits, irq_status, data->f01_container->irq_mask,
360 data->num_of_irq_regs);
361 if (u8_is_any_set(irq_bits, data->num_of_irq_regs))
362 data->f01_container->fh->attention(
363 data->f01_container, irq_bits);
364
365 //dev_info(dev, " irq_status = 0x%2x data->current_irq_mask = 0x%2x data->num_of_irq_regs = %d\n",
366 // irq_status[0], data->current_irq_mask[0], data->num_of_irq_regs );
367
368
369 u8_and(irq_status, irq_status, data->current_irq_mask,
370 data->num_of_irq_regs);
371
372 /* At this point, irq_status has all bits that are set in the
373 * interrupt status register and are enabled.
374 */
375
376 list_for_each_entry(entry, &data->rmi_functions.list, list){
377 if (entry->irq_mask && entry->fh && entry->fh->attention) {
378
379 u8_and(irq_bits, irq_status, entry->irq_mask,
380 data->num_of_irq_regs);
381 if (u8_is_any_set(irq_bits, data->num_of_irq_regs)) {
382 error = entry->fh->attention(entry, irq_bits);
383 if (error < 0)
384 dev_err(dev, "%s: f%.2x"
385 " attention handler failed:"
386 " %d\n", __func__,
387 entry->fh->func, error);
388 }
389 }
390 }
391 return 0;
392}
393
394
395static int rmi_driver_irq_handler(struct rmi_device *rmi_dev, int irq)
396{
397 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
398
399 /* Can get called before the driver is fully ready to deal with
400 * interrupts.
401 */
402 if (!data || !data->f01_container || !data->f01_container->fh) {
403 dev_warn(&rmi_dev->dev,
404 "Not ready to handle interrupts yet!\n");
405 return 0;
406 }
407
408 return process_interrupt_requests(rmi_dev);
409}
410
411/*
412 * Construct a function's IRQ mask. This should
413 * be called once and stored.
414 */
415static u8 *rmi_driver_irq_get_mask(struct rmi_device *rmi_dev,
416 struct rmi_function_container *fc) {
417 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
418
419 /* TODO: Where should this be freed? */
420 u8 *irq_mask = kzalloc(sizeof(u8) * data->num_of_irq_regs, GFP_KERNEL);
421 if (irq_mask)
422 construct_mask(irq_mask, fc->num_of_irqs, fc->irq_pos);
423
424 return irq_mask;
425}
426
427/*
428 * This pair of functions allows functions like function 54 to request to have
429 * other interupts disabled until the restore function is called. Only one store
430 * happens at a time.
431 */
432static int rmi_driver_irq_save(struct rmi_device *rmi_dev, u8 * new_ints)
433{
434 int retval = 0;
435 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
436 struct device *dev = &rmi_dev->dev;
437
438 mutex_lock(&data->irq_mutex);
439 if (!data->irq_stored) {
440 /* Save current enabled interupts */
441 retval = rmi_read_block(rmi_dev,
442 data->f01_container->fd.control_base_addr+1,
443 data->irq_mask_store, data->num_of_irq_regs);
444 if (retval < 0) {
445 dev_err(dev, "%s: Failed to read enabled interrupts!",
446 __func__);
447 goto error_unlock;
448 }
449 /*
450 * Disable every interupt except for function 54
451 * TODO:Will also want to not disable function 1-like functions.
452 * No need to take care of this now, since there's no good way
453 * to identify them.
454 */
455 retval = rmi_write_block(rmi_dev,
456 data->f01_container->fd.control_base_addr+1,
457 new_ints, data->num_of_irq_regs);
458 if (retval < 0) {
459 dev_err(dev, "%s: Failed to change enabled interrupts!",
460 __func__);
461 goto error_unlock;
462 }
463 memcpy(data->current_irq_mask, new_ints,
464 data->num_of_irq_regs * sizeof(u8));
465 data->irq_stored = true;
466 } else {
467 retval = -ENOSPC; /* No space to store IRQs.*/
468 dev_err(dev, "%s: Attempted to save values when"
469 " already stored!", __func__);
470 }
471
472error_unlock:
473 mutex_unlock(&data->irq_mutex);
474 return retval;
475}
476
477static int rmi_driver_irq_restore(struct rmi_device *rmi_dev)
478{
479 int retval = 0;
480 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
481 struct device *dev = &rmi_dev->dev;
482 mutex_lock(&data->irq_mutex);
483
484 if (data->irq_stored) {
485 retval = rmi_write_block(rmi_dev,
486 data->f01_container->fd.control_base_addr+1,
487 data->irq_mask_store, data->num_of_irq_regs);
488 if (retval < 0) {
489 dev_err(dev, "%s: Failed to write enabled interupts!",
490 __func__);
491 goto error_unlock;
492 }
493 memcpy(data->current_irq_mask, data->irq_mask_store,
494 data->num_of_irq_regs * sizeof(u8));
495 data->irq_stored = false;
496 } else {
497 retval = -EINVAL;
498 dev_err(dev, "%s: Attempted to restore values when not stored!",
499 __func__);
500 }
501
502error_unlock:
503 mutex_unlock(&data->irq_mutex);
504 return retval;
505}
506
507static int rmi_driver_fn_01_specific(struct rmi_device *rmi_dev,
508 struct pdt_entry *pdt_ptr,
509 int *current_irq_count,
510 u16 page_start)
511{
512 struct rmi_driver_data *data = NULL;
513 struct rmi_function_container *fc = NULL;
514 int retval = 0;
515 struct device *dev = &rmi_dev->dev;
516 struct rmi_function_handler *fh =
517 rmi_get_function_handler(0x01);
518
519 data = rmi_get_driverdata(rmi_dev);
520
521 dev_info(dev, "%s: Found F01, initializing.\n", __func__);
522 if (!fh)
523 dev_dbg(dev, "%s: No function handler for F01?!", __func__);
524
525 fc = kzalloc(sizeof(struct rmi_function_container), GFP_KERNEL);
526 if (!fc) {
527 retval = -ENOMEM;
528 return retval;
529 }
530
531 copy_pdt_entry_to_fd(pdt_ptr, &fc->fd, page_start);
532 fc->num_of_irqs = pdt_ptr->interrupt_source_count;
533 fc->irq_pos = *current_irq_count;
534
535 *current_irq_count += fc->num_of_irqs;
536
537 fc->rmi_dev = rmi_dev;
538 fc->dev.parent = &fc->rmi_dev->dev;
539 fc->fh = fh;
540
541 dev_set_name(&(fc->dev), "fn%02x", fc->fd.function_number);
542
543 retval = device_register(&fc->dev);
544 if (retval) {
545 dev_err(dev, "%s: Failed device_register for F01.\n", __func__);
546 goto error_free_data;
547 }
548
549 data->f01_container = fc;
550
551 return retval;
552
553error_free_data:
554 kfree(fc);
555 return retval;
556}
557
558/*
559 * Scan the PDT for F01 so we can force a reset before anything else
560 * is done. This forces the sensor into a known state, and also
561 * forces application of any pending updates from reflashing the
562 * firmware or configuration. We have to do this before actually
563 * building the PDT because the reflash might cause various registers
564 * to move around.
565 */
566static int do_initial_reset(struct rmi_device* rmi_dev)
567{
568 struct pdt_entry pdt_entry;
569 int page;
570 struct device *dev = &rmi_dev->dev;
571 bool done = false;
572 int i;
573 int retval;
574
575 pr_info("in function ____%s____ \n", __func__);
576
577 polled_synaptics_rmi_device = rmi_dev;
578
579 for (page = 0; (page <= RMI4_MAX_PAGE) && !done; page++) {
580
581 u16 page_start = RMI4_PAGE_SIZE * page;
582 u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
583 u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
584 pr_info(" reading page = %d\n", page );
585 done = true;
586 for (i = pdt_start; i >= pdt_end; i -= sizeof(pdt_entry)) {
587
588 pr_info(" reading PDT entry %3d (block %3d)\n",
589 i%sizeof(pdt_entry), i);
590
591 retval = rmi_read_block(rmi_dev, i, (u8 *)&pdt_entry,
592 sizeof(pdt_entry));
593 if (retval != sizeof(pdt_entry)) {
594 dev_err(dev, "Read PDT entry at 0x%04x"
595 "failed, code = %d.\n", i, retval);
596 return retval;
597 }
598
599 if (RMI4_END_OF_PDT(pdt_entry.function_number))
600 break;
601 done = false;
602
603 if (pdt_entry.function_number == 0x01) {
604 u16 cmd_addr = page_start +
605 pdt_entry.command_base_addr;
606 u8 cmd_buf = RMI_DEVICE_RESET_CMD;
607 retval = rmi_write_block(rmi_dev, cmd_addr,
608 &cmd_buf, 1);
609 if (retval < 0) {
610 dev_err(dev, "Initial reset failed. "
611 "Code = %d.\n", retval);
612 return retval;
613 }
614 mdelay(INITIAL_RESET_WAIT_MS);
615 return 0;
616 }
617 }
618 }
619
620 dev_warn(dev, "WARNING: Failed to find F01 for initial reset.\n");
621 return -ENODEV;
622}
623
624
625static int rmi_scan_pdt(struct rmi_device *rmi_dev)
626{
627 struct rmi_driver_data *data;
628 struct rmi_function_container *fc;
629 struct pdt_entry pdt_entry;
630 int page;
631 struct device *dev = &rmi_dev->dev;
632 int irq_count = 0;
633 bool done = false;
634 int i;
635 int retval;
636 pr_info("in function ____%s____ \n", __func__);
637 pr_info(" doing initial reset \n");
638
639 retval = do_initial_reset(rmi_dev);
640 pr_info(" back in %s \n", __func__);
641
642 if (retval)
643 dev_err(dev, "WARNING: Initial reset failed! Soldiering on.\n");
644
645 data = rmi_get_driverdata(rmi_dev);
646
647 INIT_LIST_HEAD(&data->rmi_functions.list);
648
649 /* parse the PDT */
650 for (page = 0; (page <= RMI4_MAX_PAGE) && !done; page++) {
651 u16 page_start = RMI4_PAGE_SIZE * page;
652 u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
653 u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
654
655 done = true;
656 for (i = pdt_start; i >= pdt_end; i -= sizeof(pdt_entry)) {
657 retval = rmi_read_block(rmi_dev, i, (u8 *)&pdt_entry,
658 sizeof(pdt_entry));
659 if (retval != sizeof(pdt_entry)) {
660 dev_err(dev, "Read PDT entry at 0x%04x"
661 "failed.\n", i);
662 goto error_exit;
663 }
664
665 if (RMI4_END_OF_PDT(pdt_entry.function_number))
666 break;
667
668 dev_dbg(dev, "%s: Found F%.2X on page 0x%02X\n",
669 __func__, pdt_entry.function_number, page);
670 done = false;
671
672 /*
673 * F01 is handled by rmi_driver. Hopefully we will get
674 * rid of the special treatment of f01 at some point
675 * in time.
676 */
677 if (pdt_entry.function_number == 0x01) {
678 retval = rmi_driver_fn_01_specific(rmi_dev,
679 &pdt_entry, &irq_count,
680 page_start);
681 if (retval)
682 goto error_exit;
683 continue;
684 }
685
686 fc = kzalloc(sizeof(struct rmi_function_container),
687 GFP_KERNEL);
688 if (!fc) {
689 dev_err(dev, "Failed to allocate function "
690 "container for F%02X.\n",
691 pdt_entry.function_number);
692 retval = -ENOMEM;
693 goto error_exit;
694 }
695
696 copy_pdt_entry_to_fd(&pdt_entry, &fc->fd, page_start);
697
698 fc->rmi_dev = rmi_dev;
699 fc->num_of_irqs = pdt_entry.interrupt_source_count;
700 fc->irq_pos = irq_count;
701 irq_count += fc->num_of_irqs;
702
703 retval = init_one_function(rmi_dev, fc);
704 if (retval < 0) {
705 dev_err(dev, "Failed to initialize F%.2x\n",
706 pdt_entry.function_number);
707 kfree(fc);
708 goto error_exit;
709 }
710
711 list_add_tail(&fc->list, &data->rmi_functions.list);
712 }
713 }
714 data->num_of_irq_regs = (irq_count + 7) / 8;
715 dev_dbg(dev, "%s: Done with PDT scan.\n", __func__);
716 return 0;
717
718error_exit:
719 return retval;
720}
721
722
723#ifdef SYNAPTICS_SENSOR_POLL
724void synaptics_sensor_poller(unsigned long data){
725 pr_info("in function ____%s____ , rmi_device= 0x%8x \n", __func__, polled_synaptics_rmi_device);
726 // msleep(10000);
727 for (;;) {
728 msleep(100);
729 rmi_driver_irq_handler(polled_synaptics_rmi_device, 0);
730 }
731
732 return;
733}
734
735struct workqueue_struct *synaptics_rmi_polling_queue = NULL;
736struct delayed_work synaptics_rmi_polling_work;
737
738#endif
739
740
741static int rmi_driver_probe(struct rmi_device *rmi_dev)
742{
743 struct rmi_driver_data *data;
744 struct rmi_function_container *fc;
745 struct rmi_device_platform_data *pdata;
746 int error = 0;
747 struct device *dev = &rmi_dev->dev;
748 int attr_count = 0;
749
750 dev_dbg(dev, "%s: Starting probe.\n", __func__);
751
752 pdata = to_rmi_platform_data(rmi_dev);
753
754 data = kzalloc(sizeof(struct rmi_driver_data), GFP_KERNEL);
755 if (!data) {
756 dev_err(dev, "%s: Failed to allocate driver data.\n", __func__);
757 return -ENOMEM;
758 }
759
760 rmi_set_driverdata(rmi_dev, data);
761
762 error = rmi_scan_pdt(rmi_dev);
763 if (error) {
764 dev_err(dev, "PDT scan failed with code %d.\n", error);
765 goto err_free_data;
766 }
767
768 if (!data->f01_container) {
769 dev_err(dev, "missing f01 function!\n");
770 error = -EINVAL;
771 goto err_free_data;
772 }
773
774 data->f01_container->irq_mask = kzalloc(
775 sizeof(u8) * data->num_of_irq_regs, GFP_KERNEL);
776 if (!data->f01_container->irq_mask) {
777 dev_err(dev, "Failed to allocate F01 IRQ mask.\n");
778 error = -ENOMEM;
779 goto err_free_data;
780 }
781 construct_mask(data->f01_container->irq_mask,
782 data->f01_container->num_of_irqs,
783 data->f01_container->irq_pos);
784 list_for_each_entry(fc, &data->rmi_functions.list, list)
785 fc->irq_mask = rmi_driver_irq_get_mask(rmi_dev, fc);
786
787 error = rmi_driver_f01_init(rmi_dev);
788 if (error < 0) {
789 dev_err(dev, "Failed to initialize F01.\n");
790 goto err_free_data;
791 }
792
793 error = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION,
794 (char *) &data->pdt_props);
795 if (error < 0) {
796 /* we'll print out a warning and continue since
797 * failure to get the PDT properties is not a cause to fail
798 */
799 dev_warn(dev, "Could not read PDT properties from 0x%04x. "
800 "Assuming 0x00.\n", PDT_PROPERTIES_LOCATION);
801 }
802
803 dev_dbg(dev, "%s: Creating sysfs files.", __func__);
804 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
805 error = device_create_file(dev, &attrs[attr_count]);
806 if (error < 0) {
807 dev_err(dev, "%s: Failed to create sysfs file %s.\n",
808 __func__, attrs[attr_count].attr.name);
809 goto err_free_data;
810 }
811 }
812
813 __mutex_init(&data->irq_mutex, "irq_mutex", &data->irq_key);
814 data->current_irq_mask = kzalloc(sizeof(u8) * data->num_of_irq_regs,
815 GFP_KERNEL);
816 if (!data->current_irq_mask) {
817 dev_err(dev, "Failed to allocate current_irq_mask.\n");
818 error = -ENOMEM;
819 goto err_free_data;
820 }
821 error = rmi_read_block(rmi_dev,
822 data->f01_container->fd.control_base_addr+1,
823 data->current_irq_mask, data->num_of_irq_regs);
824 if (error < 0) {
825 dev_err(dev, "%s: Failed to read current IRQ mask.\n",
826 __func__);
827 goto err_free_data;
828 }
829 data->irq_mask_store = kzalloc(sizeof(u8) * data->num_of_irq_regs,
830 GFP_KERNEL);
831 if (!data->irq_mask_store) {
832 dev_err(dev, "Failed to allocate mask store.\n");
833 error = -ENOMEM;
834 goto err_free_data;
835 }
836
837#if defined(CONFIG_RMI4_DEV)
838 if (rmi_char_dev_register(rmi_dev->phys))
839 pr_err("%s: error register char device", __func__);
840#endif /*CONFIG_RMI4_DEV*/
841
842#ifdef CONFIG_PM
843 data->pm_data = pdata->pm_data;
844 data->pre_suspend = pdata->pre_suspend;
845 data->post_resume = pdata->post_resume;
846
847 mutex_init(&data->suspend_mutex);
848
849#ifdef CONFIG_HAS_EARLYSUSPEND
850 rmi_dev->early_suspend_handler.level =
851 EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
852 rmi_dev->early_suspend_handler.suspend = rmi_driver_early_suspend;
853 rmi_dev->early_suspend_handler.resume = rmi_driver_late_resume;
854 register_early_suspend(&rmi_dev->early_suspend_handler);
855#endif /* CONFIG_HAS_EARLYSUSPEND */
856#endif /* CONFIG_PM */
857 data->enabled = true;
858
859 dev_info(dev, "connected RMI device manufacturer: %s product: %s\n",
860 data->manufacturer_id == 1 ? "synaptics" : "unknown",
861 data->product_id);
862
863#ifdef SYNAPTICS_SENSOR_POLL
864 synaptics_rmi_polling_queue = create_singlethread_workqueue("rmi_poll_work");
865 INIT_DELAYED_WORK_DEFERRABLE(&synaptics_rmi_polling_work, synaptics_sensor_poller);
866 pr_info("%s: setting up POLLING mode, rmi_device= 0x%8x \n", __func__, polled_synaptics_rmi_device);
867 queue_delayed_work(synaptics_rmi_polling_queue, &synaptics_rmi_polling_work, 1000);
868#endif
869 return 0;
870
871 err_free_data:
872 rmi_free_function_list(rmi_dev);
873 for (attr_count--; attr_count >= 0; attr_count--)
874 device_remove_file(dev, &attrs[attr_count]);
875 kfree(data->f01_container->irq_mask);
876 kfree(data->irq_mask_store);
877 kfree(data->current_irq_mask);
878 kfree(data);
879 return error;
880}
881
882#ifdef CONFIG_PM
883static int rmi_driver_suspend(struct device *dev)
884{
885 struct rmi_device *rmi_dev;
886 struct rmi_driver_data *data;
887 struct rmi_function_container *entry;
888 int retval = 0;
889
890 rmi_dev = to_rmi_device(dev);
891 data = rmi_get_driverdata(rmi_dev);
892
893 mutex_lock(&data->suspend_mutex);
894 if (data->suspended)
895 goto exit;
896
897 if (data->pre_suspend) {
898 retval = data->pre_suspend(data->pm_data);
899 if (retval)
900 goto exit;
901 }
902
903 list_for_each_entry(entry, &data->rmi_functions.list, list)
904 if (entry->fh && entry->fh->suspend) {
905 retval = entry->fh->suspend(entry);
906 if (retval < 0)
907 goto exit;
908 }
909
910 if (data->f01_container && data->f01_container->fh
911 && data->f01_container->fh->suspend) {
912 retval = data->f01_container->fh->suspend(data->f01_container);
913 if (retval < 0)
914 goto exit;
915 }
916 data->suspended = true;
917
918exit:
919 mutex_unlock(&data->suspend_mutex);
920 return retval;
921}
922
923static int rmi_driver_resume(struct device *dev)
924{
925 struct rmi_device *rmi_dev;
926 struct rmi_driver_data *data;
927 struct rmi_function_container *entry;
928 int retval = 0;
929
930 rmi_dev = to_rmi_device(dev);
931 data = rmi_get_driverdata(rmi_dev);
932
933 mutex_lock(&data->suspend_mutex);
934 if (!data->suspended)
935 goto exit;
936
937 if (data->f01_container && data->f01_container->fh
938 && data->f01_container->fh->resume) {
939 retval = data->f01_container->fh->resume(data->f01_container);
940 if (retval < 0)
941 goto exit;
942 }
943
944 list_for_each_entry(entry, &data->rmi_functions.list, list)
945 if (entry->fh && entry->fh->resume) {
946 retval = entry->fh->resume(entry);
947 if (retval < 0)
948 goto exit;
949 }
950
951 if (data->post_resume) {
952 retval = data->post_resume(data->pm_data);
953 if (retval)
954 goto exit;
955 }
956
957 data->suspended = false;
958
959exit:
960 mutex_unlock(&data->suspend_mutex);
961 return retval;
962}
963
964#ifdef CONFIG_HAS_EARLYSUSPEND
965static void rmi_driver_early_suspend(struct early_suspend *h)
966{
967 struct rmi_device *rmi_dev =
968 container_of(h, struct rmi_device, early_suspend_handler);
969
970 dev_dbg(&rmi_dev->dev, "Early suspend.\n");
971 rmi_driver_suspend(&rmi_dev->dev);
972}
973
974static void rmi_driver_late_resume(struct early_suspend *h)
975{
976 struct rmi_device *rmi_dev =
977 container_of(h, struct rmi_device, early_suspend_handler);
978
979 dev_dbg(&rmi_dev->dev, "Late resume.\n");
980 rmi_driver_resume(&rmi_dev->dev);
981}
982#endif /* CONFIG_HAS_EARLYSUSPEND */
983#endif /* CONFIG_PM */
984
985static int __devexit rmi_driver_remove(struct rmi_device *rmi_dev)
986{
987 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
988 struct rmi_function_container *entry;
989 int i;
990
991 list_for_each_entry(entry, &data->rmi_functions.list, list)
992 if (entry->fh && entry->fh->remove)
993 entry->fh->remove(entry);
994
995 rmi_free_function_list(rmi_dev);
996 for (i = 0; i < ARRAY_SIZE(attrs); i++)
997 device_remove_file(&rmi_dev->dev, &attrs[i]);
998 kfree(data->f01_container->irq_mask);
999 kfree(data->irq_mask_store);
1000 kfree(data->current_irq_mask);
1001 kfree(data);
1002
1003 return 0;
1004}
1005
1006#ifdef UNIVERSAL_DEV_PM_OPS
1007static UNIVERSAL_DEV_PM_OPS(rmi_driver_pm, rmi_driver_suspend,
1008 rmi_driver_resume, NULL);
1009#endif
1010
1011static struct rmi_driver sensor_driver = {
1012 .driver = {
1013 .owner = THIS_MODULE,
1014 // .name = "rmi-generic",
1015 .name = "rmi_generic",
1016#ifdef UNIVERSAL_DEV_PM_OPS
1017 .pm = &rmi_driver_pm,
1018#endif
1019 },
1020 .probe = rmi_driver_probe,
1021 .irq_handler = rmi_driver_irq_handler,
1022 .fh_add = rmi_driver_fh_add,
1023 .fh_remove = rmi_driver_fh_remove,
1024 .get_func_irq_mask = rmi_driver_irq_get_mask,
1025 .store_irq_mask = rmi_driver_irq_save,
1026 .restore_irq_mask = rmi_driver_irq_restore,
1027 .remove = __devexit_p(rmi_driver_remove)
1028};
1029
1030/* Utility routine to handle writes to read-only attributes. Hopefully
1031 * this will never happen, but if the user does something stupid, we don't
1032 * want to accept it quietly (which is what can happen if you just put NULL
1033 * for the attribute's store function).
1034 */
1035ssize_t rmi_store_error(struct device *dev,
1036 struct device_attribute *attr,
1037 const char *buf, size_t count)
1038{
1039 dev_warn(dev,
1040 "RMI4 WARNING: Attempt to write %d characters to read-only "
1041 "attribute %s.", count, attr->attr.name);
1042 return -EPERM;
1043}
1044
1045/* Utility routine to handle reads of write-only attributes. Hopefully
1046 * this will never happen, but if the user does something stupid, we don't
1047 * want to accept it quietly (which is what can happen if you just put NULL
1048 * for the attribute's show function).
1049 */
1050ssize_t rmi_show_error(struct device *dev,
1051 struct device_attribute *attr,
1052 char *buf)
1053{
1054 dev_warn(dev,
1055 "RMI4 WARNING: Attempt to read from write-only attribute %s.",
1056 attr->attr.name);
1057 return -EPERM;
1058}
1059
1060/* sysfs show and store fns for driver attributes */
1061static ssize_t rmi_driver_hasbsr_show(struct device *dev,
1062 struct device_attribute *attr,
1063 char *buf)
1064{
1065 struct rmi_device *rmi_dev;
1066 struct rmi_driver_data *data;
1067 rmi_dev = to_rmi_device(dev);
1068 data = rmi_get_driverdata(rmi_dev);
1069
1070 return snprintf(buf, PAGE_SIZE, "%u\n", has_bsr(data));
1071}
1072
1073static ssize_t rmi_driver_bsr_show(struct device *dev,
1074 struct device_attribute *attr, char *buf)
1075{
1076 struct rmi_device *rmi_dev;
1077 struct rmi_driver_data *data;
1078 rmi_dev = to_rmi_device(dev);
1079 data = rmi_get_driverdata(rmi_dev);
1080
1081 return snprintf(buf, PAGE_SIZE, "%u\n", data->bsr);
1082}
1083
1084static ssize_t rmi_driver_bsr_store(struct device *dev,
1085 struct device_attribute *attr,
1086 const char *buf, size_t count)
1087{
1088 int retval;
1089 unsigned long val;
1090 struct rmi_device *rmi_dev;
1091 struct rmi_driver_data *data;
1092
1093 rmi_dev = to_rmi_device(dev);
1094 data = rmi_get_driverdata(rmi_dev);
1095
1096 /* need to convert the string data to an actual value */
1097 retval = strict_strtoul(buf, 10, &val);
1098 if (retval < 0) {
1099 dev_err(dev, "Invalid value '%s' written to BSR.\n", buf);
1100 return -EINVAL;
1101 }
1102
1103 retval = rmi_write(rmi_dev, BSR_LOCATION, (unsigned char)val);
1104 if (retval) {
1105 dev_err(dev, "%s : failed to write bsr %u to 0x%x\n",
1106 __func__, (unsigned int)val, BSR_LOCATION);
1107 return retval;
1108 }
1109
1110 data->bsr = val;
1111
1112 return count;
1113}
1114
1115static void disable_sensor(struct rmi_device *rmi_dev)
1116{
1117 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
1118
1119 rmi_dev->phys->disable_device(rmi_dev->phys);
1120
1121 data->enabled = false;
1122}
1123
1124static int enable_sensor(struct rmi_device *rmi_dev)
1125{
1126 struct rmi_driver_data *data = rmi_get_driverdata(rmi_dev);
1127 int retval = 0;
1128 pr_info("in function ____%s____ \n", __func__);
1129 retval = rmi_dev->phys->enable_device(rmi_dev->phys);
1130 /* non-zero means error occurred */
1131 if (retval)
1132 return retval;
1133
1134 data->enabled = true;
1135
1136 return 0;
1137}
1138
1139static ssize_t rmi_driver_enabled_show(struct device *dev,
1140 struct device_attribute *attr, char *buf)
1141{
1142 struct rmi_device *rmi_dev;
1143 struct rmi_driver_data *data;
1144
1145 rmi_dev = to_rmi_device(dev);
1146 data = rmi_get_driverdata(rmi_dev);
1147
1148 return snprintf(buf, PAGE_SIZE, "%u\n", data->enabled);
1149}
1150
1151static ssize_t rmi_driver_enabled_store(struct device *dev,
1152 struct device_attribute *attr,
1153 const char *buf, size_t count)
1154{
1155 int retval;
1156 int new_value;
1157 struct rmi_device *rmi_dev;
1158 struct rmi_driver_data *data;
1159
1160 rmi_dev = to_rmi_device(dev);
1161 data = rmi_get_driverdata(rmi_dev);
1162
1163 if (sysfs_streq(buf, "0"))
1164 new_value = false;
1165 else if (sysfs_streq(buf, "1"))
1166 new_value = true;
1167 else
1168 return -EINVAL;
1169
1170 if (new_value) {
1171 retval = enable_sensor(rmi_dev);
1172 if (retval) {
1173 dev_err(dev, "Failed to enable sensor, code=%d.\n",
1174 retval);
1175 return -EIO;
1176 }
1177 } else {
1178 disable_sensor(rmi_dev);
1179 }
1180
1181 return count;
1182}
1183
1184#if REGISTER_DEBUG
1185static ssize_t rmi_driver_reg_store(struct device *dev,
1186 struct device_attribute *attr,
1187 const char *buf, size_t count)
1188{
1189 int retval;
1190 unsigned int address;
1191 unsigned int bytes;
1192 struct rmi_device *rmi_dev;
1193 struct rmi_driver_data *data;
1194 u8 readbuf[128];
1195 unsigned char outbuf[512];
1196 unsigned char *bufptr = outbuf;
1197 int i;
1198
1199 rmi_dev = to_rmi_device(dev);
1200 data = rmi_get_driverdata(rmi_dev);
1201
1202 retval = sscanf(buf, "%x %u", &address, &bytes);
1203 if (retval != 2) {
1204 dev_err(dev, "Invalid input (code %d) for reg store: %s",
1205 retval, buf);
1206 return -EINVAL;
1207 }
1208 if (address < 0 || address > 0xFFFF) {
1209 dev_err(dev, "Invalid address for reg store '%#06x'.\n",
1210 address);
1211 return -EINVAL;
1212 }
1213 if (bytes < 0 || bytes >= sizeof(readbuf) || address+bytes > 65535) {
1214 dev_err(dev, "Invalid byte count for reg store '%d'.\n",
1215 bytes);
1216 return -EINVAL;
1217 }
1218
1219 retval = rmi_read_block(rmi_dev, address, readbuf, bytes);
1220 if (retval != bytes) {
1221 dev_err(dev, "Failed to read %d registers at %#06x, code %d.\n",
1222 bytes, address, retval);
1223 return retval;
1224 }
1225
1226 dev_info(dev, "Reading %d bytes from %#06x.\n", bytes, address);
1227 for (i = 0; i < bytes; i++) {
1228 retval = snprintf(bufptr, 4, "%02X ", readbuf[i]);
1229 if (retval < 0) {
1230 dev_err(dev, "Failed to format string. Code: %d",
1231 retval);
1232 return retval;
1233 }
1234 bufptr += retval;
1235 }
1236 dev_info(dev, "%s\n", outbuf);
1237
1238 return count;
1239}
1240#endif
1241
1242#if DELAY_DEBUG
1243static ssize_t rmi_delay_store(struct device *dev,
1244 struct device_attribute *attr,
1245 const char *buf, size_t count)
1246{
1247 int retval;
1248 struct rmi_device *rmi_dev;
1249 struct rmi_device_platform_data *pdata;
1250 unsigned int new_read_delay;
1251 unsigned int new_write_delay;
1252 unsigned int new_block_delay;
1253 unsigned int new_pre_delay;
1254 unsigned int new_post_delay;
1255
1256 retval = sscanf(buf, "%u %u %u %u %u", &new_read_delay,
1257 &new_write_delay, &new_block_delay,
1258 &new_pre_delay, &new_post_delay);
1259 if (retval != 5) {
1260 dev_err(dev, "Incorrect number of values provided for delay.");
1261 return -EINVAL;
1262 }
1263 if (new_read_delay < 0) {
1264 dev_err(dev, "Byte delay must be positive microseconds.\n");
1265 return -EINVAL;
1266 }
1267 if (new_write_delay < 0) {
1268 dev_err(dev, "Write delay must be positive microseconds.\n");
1269 return -EINVAL;
1270 }
1271 if (new_block_delay < 0) {
1272 dev_err(dev, "Block delay must be positive microseconds.\n");
1273 return -EINVAL;
1274 }
1275 if (new_pre_delay < 0) {
1276 dev_err(dev,
1277 "Pre-transfer delay must be positive microseconds.\n");
1278 return -EINVAL;
1279 }
1280 if (new_post_delay < 0) {
1281 dev_err(dev,
1282 "Post-transfer delay must be positive microseconds.\n");
1283 return -EINVAL;
1284 }
1285
1286 rmi_dev = to_rmi_device(dev);
1287 pdata = rmi_dev->phys->dev->platform_data;
1288
1289 dev_info(dev, "Setting delays to %u %u %u %u %u.\n", new_read_delay,
1290 new_write_delay, new_block_delay, new_pre_delay,
1291 new_post_delay);
1292 pdata->spi_data.read_delay_us = new_read_delay;
1293 pdata->spi_data.write_delay_us = new_write_delay;
1294 pdata->spi_data.block_delay_us = new_block_delay;
1295 pdata->spi_data.pre_delay_us = new_pre_delay;
1296 pdata->spi_data.post_delay_us = new_post_delay;
1297
1298 return count;
1299}
1300
1301static ssize_t rmi_delay_show(struct device *dev,
1302 struct device_attribute *attr, char *buf)
1303{
1304 struct rmi_device *rmi_dev;
1305 struct rmi_device_platform_data *pdata;
1306
1307 rmi_dev = to_rmi_device(dev);
1308 pdata = rmi_dev->phys->dev->platform_data;
1309
1310 return snprintf(buf, PAGE_SIZE, "%d %d %d %d %d\n",
1311 pdata->spi_data.read_delay_us, pdata->spi_data.write_delay_us,
1312 pdata->spi_data.block_delay_us,
1313 pdata->spi_data.pre_delay_us, pdata->spi_data.post_delay_us);
1314}
1315#endif
1316
1317static ssize_t rmi_driver_phys_show(struct device *dev,
1318 struct device_attribute *attr, char *buf)
1319{
1320 struct rmi_device *rmi_dev;
1321 struct rmi_phys_info *info;
1322
1323 rmi_dev = to_rmi_device(dev);
1324 info = &rmi_dev->phys->info;
1325
1326 return snprintf(buf, PAGE_SIZE, "%-5s %ld %ld %ld %ld %ld %ld %ld\n",
1327 info->proto ? info->proto : "unk",
1328 info->tx_count, info->tx_bytes, info->tx_errs,
1329 info->rx_count, info->rx_bytes, info->rx_errs,
1330 info->attn_count);
1331}
1332
1333static ssize_t rmi_driver_version_show(struct device *dev,
1334 struct device_attribute *attr, char *buf)
1335{
1336 return snprintf(buf, PAGE_SIZE, "%s\n",
1337 RMI_DRIVER_VERSION_STRING);
1338}
1339
1340static int __init rmi_driver_init(void)
1341{
1342 return rmi_register_driver(&sensor_driver);
1343}
1344
1345static void __exit rmi_driver_exit(void)
1346{
1347 rmi_unregister_driver(&sensor_driver);
1348}
1349
1350module_init(rmi_driver_init);
1351module_exit(rmi_driver_exit);
1352
1353MODULE_AUTHOR("Eric Andersson <eric.andersson@unixphere.com>");
1354MODULE_DESCRIPTION("RMI generic driver");