2 * Synaptics DSX touchscreen driver
4 * Copyright (C) 2012 Synaptics Incorporated
6 * Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com>
7 * Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/delay.h>
25 #include <linux/input.h>
26 #include <linux/gpio.h>
27 #include <linux/uaccess.h>
28 #include <linux/cdev.h>
29 #include "synaptics_dsx.h"
30 #include "synaptics_dsx_i2c.h"
32 #define CHAR_DEVICE_NAME "rmi"
33 #define DEVICE_CLASS_NAME "rmidev"
34 #define SYSFS_FOLDER_NAME "rmidev"
36 #define REG_ADDR_LIMIT 0xFFFF
38 static ssize_t
rmidev_sysfs_data_show(struct file
*data_file
,
39 struct kobject
*kobj
, struct bin_attribute
*attributes
,
40 char *buf
, loff_t pos
, size_t count
);
42 static ssize_t
rmidev_sysfs_data_store(struct file
*data_file
,
43 struct kobject
*kobj
, struct bin_attribute
*attributes
,
44 char *buf
, loff_t pos
, size_t count
);
46 static ssize_t
rmidev_sysfs_open_store(struct device
*dev
,
47 struct device_attribute
*attr
, const char *buf
, size_t count
);
49 static ssize_t
rmidev_sysfs_release_store(struct device
*dev
,
50 struct device_attribute
*attr
, const char *buf
, size_t count
);
52 static ssize_t
rmidev_sysfs_attn_state_show(struct device
*dev
,
53 struct device_attribute
*attr
, char *buf
);
55 struct rmidev_handle
{
58 struct synaptics_rmi4_data
*rmi4_data
;
59 struct synaptics_rmi4_access_ptr
*fn_ptr
;
60 struct kobject
*sysfs_dir
;
68 struct class *device_class
;
69 struct mutex file_mutex
;
70 struct rmidev_handle
*rmi_dev
;
73 static struct bin_attribute attr_data
= {
76 .mode
= (S_IRUGO
| S_IWUGO
),
79 .read
= rmidev_sysfs_data_show
,
80 .write
= rmidev_sysfs_data_store
,
83 static struct device_attribute attrs
[] = {
85 synaptics_rmi4_show_error
,
86 rmidev_sysfs_open_store
),
87 __ATTR(release
, S_IWUGO
,
88 synaptics_rmi4_show_error
,
89 rmidev_sysfs_release_store
),
90 __ATTR(attn_state
, S_IRUGO
,
91 rmidev_sysfs_attn_state_show
,
92 synaptics_rmi4_store_error
),
95 static int rmidev_major_num
;
97 static struct class *rmidev_device_class
;
99 static struct rmidev_handle
*rmidev
;
101 DECLARE_COMPLETION(rmidev_remove_complete
);
103 static irqreturn_t
rmidev_sysfs_irq(int irq
, void *data
)
105 struct synaptics_rmi4_data
*rmi4_data
= data
;
107 sysfs_notify(&rmi4_data
->input_dev
->dev
.kobj
,
108 SYSFS_FOLDER_NAME
, "attn_state");
113 static int rmidev_sysfs_irq_enable(struct synaptics_rmi4_data
*rmi4_data
,
117 unsigned char intr_status
[MAX_INTR_REGISTERS
];
118 unsigned long irq_flags
= IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
;
121 if (rmidev
->irq_enabled
)
124 /* Clear interrupts first */
125 retval
= rmidev
->fn_ptr
->read(rmi4_data
,
126 rmi4_data
->f01_data_base_addr
+ 1,
128 rmi4_data
->num_of_intr_regs
);
132 retval
= request_threaded_irq(rmi4_data
->irq
, NULL
,
133 rmidev_sysfs_irq
, irq_flags
,
134 "synaptics_dsx_rmidev", rmi4_data
);
136 dev_err(&rmi4_data
->i2c_client
->dev
,
137 "%s: Failed to create irq thread\n",
142 rmidev
->irq_enabled
= true;
144 if (rmidev
->irq_enabled
) {
145 disable_irq(rmi4_data
->irq
);
146 free_irq(rmi4_data
->irq
, rmi4_data
);
147 rmidev
->irq_enabled
= false;
154 static ssize_t
rmidev_sysfs_data_show(struct file
*data_file
,
155 struct kobject
*kobj
, struct bin_attribute
*attributes
,
156 char *buf
, loff_t pos
, size_t count
)
159 unsigned int length
= (unsigned int)count
;
160 unsigned short address
= (unsigned short)pos
;
162 if (length
> (REG_ADDR_LIMIT
- address
)) {
163 dev_err(&rmidev
->rmi4_data
->i2c_client
->dev
,
164 "%s: Out of register map limit\n",
170 retval
= rmidev
->fn_ptr
->read(rmidev
->rmi4_data
,
172 (unsigned char *)buf
,
175 dev_err(&rmidev
->rmi4_data
->i2c_client
->dev
,
176 "%s: Failed to read data\n",
187 static ssize_t
rmidev_sysfs_data_store(struct file
*data_file
,
188 struct kobject
*kobj
, struct bin_attribute
*attributes
,
189 char *buf
, loff_t pos
, size_t count
)
192 unsigned int length
= (unsigned int)count
;
193 unsigned short address
= (unsigned short)pos
;
195 if (length
> (REG_ADDR_LIMIT
- address
)) {
196 dev_err(&rmidev
->rmi4_data
->i2c_client
->dev
,
197 "%s: Out of register map limit\n",
203 retval
= rmidev
->fn_ptr
->write(rmidev
->rmi4_data
,
205 (unsigned char *)buf
,
208 dev_err(&rmidev
->rmi4_data
->i2c_client
->dev
,
209 "%s: Failed to write data\n",
220 static ssize_t
rmidev_sysfs_open_store(struct device
*dev
,
221 struct device_attribute
*attr
, const char *buf
, size_t count
)
224 struct synaptics_rmi4_data
*rmi4_data
= rmidev
->rmi4_data
;
226 if (sscanf(buf
, "%u", &input
) != 1)
232 rmidev
->fn_ptr
->enable(rmi4_data
, false);
233 rmidev_sysfs_irq_enable(rmi4_data
, true);
235 dev_dbg(&rmi4_data
->i2c_client
->dev
,
236 "%s: Attention interrupt disabled\n",
242 static ssize_t
rmidev_sysfs_release_store(struct device
*dev
,
243 struct device_attribute
*attr
, const char *buf
, size_t count
)
246 struct synaptics_rmi4_data
*rmi4_data
= rmidev
->rmi4_data
;
248 if (sscanf(buf
, "%u", &input
) != 1)
254 rmi4_data
->reset_device(rmi4_data
);
256 rmidev_sysfs_irq_enable(rmi4_data
, false);
257 rmidev
->fn_ptr
->enable(rmi4_data
, true);
259 dev_dbg(&rmi4_data
->i2c_client
->dev
,
260 "%s: Attention interrupt enabled\n",
266 static ssize_t
rmidev_sysfs_attn_state_show(struct device
*dev
,
267 struct device_attribute
*attr
, char *buf
)
270 const struct synaptics_dsx_platform_data
*platform_data
=
271 rmidev
->rmi4_data
->board
;
273 attn_state
= gpio_get_value(platform_data
->irq_gpio
);
275 return snprintf(buf
, PAGE_SIZE
, "%u\n", attn_state
);
279 * rmidev_llseek - used to set up register address
281 * @filp: file structure for seek
283 * if whence == SEEK_SET,
284 * high 16 bits: page address
285 * low 16 bits: register address
286 * if whence == SEEK_CUR,
287 * offset from current position
288 * if whence == SEEK_END,
289 * offset from end position (0xFFFF)
290 * @whence: SEEK_SET, SEEK_CUR, or SEEK_END
292 static loff_t
rmidev_llseek(struct file
*filp
, loff_t off
, int whence
)
295 struct rmidev_data
*dev_data
= filp
->private_data
;
297 if (IS_ERR(dev_data
)) {
298 pr_err("%s: Pointer of char device data is invalid", __func__
);
302 mutex_lock(&(dev_data
->file_mutex
));
309 newpos
= filp
->f_pos
+ off
;
312 newpos
= REG_ADDR_LIMIT
+ off
;
319 if (newpos
< 0 || newpos
> REG_ADDR_LIMIT
) {
320 dev_err(&rmidev
->rmi4_data
->i2c_client
->dev
,
321 "%s: New position 0x%04x is invalid\n",
322 __func__
, (unsigned int)newpos
);
327 filp
->f_pos
= newpos
;
330 mutex_unlock(&(dev_data
->file_mutex
));
336 * rmidev_read: - use to read data from rmi device
338 * @filp: file structure for read
339 * @buf: user space buffer pointer
340 * @count: number of bytes to read
341 * @f_pos: offset (starting register address)
343 static ssize_t
rmidev_read(struct file
*filp
, char __user
*buf
,
344 size_t count
, loff_t
*f_pos
)
347 unsigned char tmpbuf
[count
+ 1];
348 struct rmidev_data
*dev_data
= filp
->private_data
;
350 if (IS_ERR(dev_data
)) {
351 pr_err("%s: Pointer of char device data is invalid", __func__
);
358 if (count
> (REG_ADDR_LIMIT
- *f_pos
))
359 count
= REG_ADDR_LIMIT
- *f_pos
;
361 mutex_lock(&(dev_data
->file_mutex
));
363 retval
= rmidev
->fn_ptr
->read(rmidev
->rmi4_data
,
370 if (copy_to_user(buf
, tmpbuf
, count
))
376 mutex_unlock(&(dev_data
->file_mutex
));
382 * rmidev_write: - used to write data to rmi device
384 * @filep: file structure for write
385 * @buf: user space buffer pointer
386 * @count: number of bytes to write
387 * @f_pos: offset (starting register address)
389 static ssize_t
rmidev_write(struct file
*filp
, const char __user
*buf
,
390 size_t count
, loff_t
*f_pos
)
393 unsigned char tmpbuf
[count
+ 1];
394 struct rmidev_data
*dev_data
= filp
->private_data
;
396 if (IS_ERR(dev_data
)) {
397 pr_err("%s: Pointer of char device data is invalid", __func__
);
404 if (count
> (REG_ADDR_LIMIT
- *f_pos
))
405 count
= REG_ADDR_LIMIT
- *f_pos
;
407 if (copy_from_user(tmpbuf
, buf
, count
))
410 mutex_lock(&(dev_data
->file_mutex
));
412 retval
= rmidev
->fn_ptr
->write(rmidev
->rmi4_data
,
419 mutex_unlock(&(dev_data
->file_mutex
));
425 * rmidev_open: enable access to rmi device
426 * @inp: inode struture
427 * @filp: file structure
429 static int rmidev_open(struct inode
*inp
, struct file
*filp
)
432 struct rmidev_data
*dev_data
=
433 container_of(inp
->i_cdev
, struct rmidev_data
, main_dev
);
438 filp
->private_data
= dev_data
;
440 mutex_lock(&(dev_data
->file_mutex
));
442 rmidev
->fn_ptr
->enable(rmidev
->rmi4_data
, false);
443 dev_dbg(&rmidev
->rmi4_data
->i2c_client
->dev
,
444 "%s: Attention interrupt disabled\n",
447 if (dev_data
->ref_count
< 1)
448 dev_data
->ref_count
++;
452 mutex_unlock(&(dev_data
->file_mutex
));
458 * rmidev_release: - release access to rmi device
459 * @inp: inode structure
460 * @filp: file structure
462 static int rmidev_release(struct inode
*inp
, struct file
*filp
)
464 struct synaptics_rmi4_data
*rmi4_data
= rmidev
->rmi4_data
;
465 struct rmidev_data
*dev_data
=
466 container_of(inp
->i_cdev
, struct rmidev_data
, main_dev
);
471 rmi4_data
->reset_device(rmi4_data
);
473 mutex_lock(&(dev_data
->file_mutex
));
475 dev_data
->ref_count
--;
476 if (dev_data
->ref_count
< 0)
477 dev_data
->ref_count
= 0;
479 rmidev
->fn_ptr
->enable(rmi4_data
, true);
480 dev_dbg(&rmi4_data
->i2c_client
->dev
,
481 "%s: Attention interrupt enabled\n",
484 mutex_unlock(&(dev_data
->file_mutex
));
489 static const struct file_operations rmidev_fops
= {
490 .owner
= THIS_MODULE
,
491 .llseek
= rmidev_llseek
,
493 .write
= rmidev_write
,
495 .release
= rmidev_release
,
498 static void rmidev_device_cleanup(struct rmidev_data
*dev_data
)
503 devno
= dev_data
->main_dev
.dev
;
505 if (dev_data
->device_class
)
506 device_destroy(dev_data
->device_class
, devno
);
508 cdev_del(&dev_data
->main_dev
);
510 unregister_chrdev_region(devno
, 1);
512 dev_dbg(&rmidev
->rmi4_data
->i2c_client
->dev
,
513 "%s: rmidev device removed\n",
520 static char *rmi_char_devnode(struct device
*dev
, mode_t
*mode
)
525 *mode
= (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
527 return kasprintf(GFP_KERNEL
, "rmi/%s", dev_name(dev
));
530 static int rmidev_create_device_class(void)
532 rmidev_device_class
= class_create(THIS_MODULE
, DEVICE_CLASS_NAME
);
534 if (IS_ERR(rmidev_device_class
)) {
535 pr_err("%s: Failed to create /dev/%s\n",
536 __func__
, CHAR_DEVICE_NAME
);
540 rmidev_device_class
->devnode
= rmi_char_devnode
;
545 static int rmidev_init_device(struct synaptics_rmi4_data
*rmi4_data
)
549 unsigned char attr_count
;
550 struct rmidev_data
*dev_data
;
551 struct device
*device_ptr
;
553 rmidev
= kzalloc(sizeof(*rmidev
), GFP_KERNEL
);
555 dev_err(&rmi4_data
->i2c_client
->dev
,
556 "%s: Failed to alloc mem for rmidev\n",
562 rmidev
->fn_ptr
= kzalloc(sizeof(*(rmidev
->fn_ptr
)), GFP_KERNEL
);
564 dev_err(&rmi4_data
->i2c_client
->dev
,
565 "%s: Failed to alloc mem for fn_ptr\n",
571 rmidev
->fn_ptr
->read
= rmi4_data
->i2c_read
;
572 rmidev
->fn_ptr
->write
= rmi4_data
->i2c_write
;
573 rmidev
->fn_ptr
->enable
= rmi4_data
->irq_enable
;
574 rmidev
->rmi4_data
= rmi4_data
;
576 retval
= rmidev_create_device_class();
578 dev_err(&rmi4_data
->i2c_client
->dev
,
579 "%s: Failed to create device class\n",
581 goto err_device_class
;
584 if (rmidev_major_num
) {
585 dev_no
= MKDEV(rmidev_major_num
, DEV_NUMBER
);
586 retval
= register_chrdev_region(dev_no
, 1, CHAR_DEVICE_NAME
);
588 retval
= alloc_chrdev_region(&dev_no
, 0, 1, CHAR_DEVICE_NAME
);
590 dev_err(&rmi4_data
->i2c_client
->dev
,
591 "%s: Failed to allocate char device region\n",
593 goto err_device_region
;
596 rmidev_major_num
= MAJOR(dev_no
);
597 dev_dbg(&rmi4_data
->i2c_client
->dev
,
598 "%s: Major number of rmidev = %d\n",
599 __func__
, rmidev_major_num
);
602 dev_data
= kzalloc(sizeof(*dev_data
), GFP_KERNEL
);
604 dev_err(&rmi4_data
->i2c_client
->dev
,
605 "%s: Failed to alloc mem for dev_data\n",
611 mutex_init(&dev_data
->file_mutex
);
612 dev_data
->rmi_dev
= rmidev
;
613 rmidev
->data
= dev_data
;
615 cdev_init(&dev_data
->main_dev
, &rmidev_fops
);
617 retval
= cdev_add(&dev_data
->main_dev
, dev_no
, 1);
619 dev_err(&rmi4_data
->i2c_client
->dev
,
620 "%s: Failed to add rmi char device\n",
622 goto err_char_device
;
625 dev_set_name(&rmidev
->dev
, "rmidev%d", MINOR(dev_no
));
626 dev_data
->device_class
= rmidev_device_class
;
628 device_ptr
= device_create(dev_data
->device_class
, NULL
, dev_no
,
629 NULL
, CHAR_DEVICE_NAME
"%d", MINOR(dev_no
));
630 if (IS_ERR(device_ptr
)) {
631 dev_err(&rmi4_data
->i2c_client
->dev
,
632 "%s: Failed to create rmi char device\n",
635 goto err_char_device
;
638 retval
= gpio_export(rmi4_data
->board
->irq_gpio
, false);
640 dev_err(&rmi4_data
->i2c_client
->dev
,
641 "%s: Failed to export attention gpio\n",
644 retval
= gpio_export_link(&(rmi4_data
->input_dev
->dev
),
645 "attn", rmi4_data
->board
->irq_gpio
);
647 dev_err(&rmi4_data
->input_dev
->dev
,
648 "%s Failed to create gpio symlink\n",
651 dev_dbg(&rmi4_data
->input_dev
->dev
,
652 "%s: Exported attention gpio %d\n",
653 __func__
, rmi4_data
->board
->irq_gpio
);
657 rmidev
->sysfs_dir
= kobject_create_and_add(SYSFS_FOLDER_NAME
,
658 &rmi4_data
->input_dev
->dev
.kobj
);
659 if (!rmidev
->sysfs_dir
) {
660 dev_err(&rmi4_data
->i2c_client
->dev
,
661 "%s: Failed to create sysfs directory\n",
667 retval
= sysfs_create_bin_file(rmidev
->sysfs_dir
,
670 dev_err(&rmi4_data
->i2c_client
->dev
,
671 "%s: Failed to create sysfs bin file\n",
676 for (attr_count
= 0; attr_count
< ARRAY_SIZE(attrs
); attr_count
++) {
677 retval
= sysfs_create_file(rmidev
->sysfs_dir
,
678 &attrs
[attr_count
].attr
);
680 dev_err(&rmi4_data
->input_dev
->dev
,
681 "%s: Failed to create sysfs attributes\n",
684 goto err_sysfs_attrs
;
691 for (attr_count
--; attr_count
>= 0; attr_count
--)
692 sysfs_remove_file(rmidev
->sysfs_dir
, &attrs
[attr_count
].attr
);
694 sysfs_remove_bin_file(rmidev
->sysfs_dir
, &attr_data
);
697 kobject_put(rmidev
->sysfs_dir
);
701 rmidev_device_cleanup(dev_data
);
705 unregister_chrdev_region(dev_no
, 1);
708 class_destroy(rmidev_device_class
);
711 kfree(rmidev
->fn_ptr
);
721 static void rmidev_remove_device(struct synaptics_rmi4_data
*rmi4_data
)
723 unsigned char attr_count
;
724 struct rmidev_data
*dev_data
;
729 for (attr_count
= 0; attr_count
< ARRAY_SIZE(attrs
); attr_count
++)
730 sysfs_remove_file(rmidev
->sysfs_dir
, &attrs
[attr_count
].attr
);
732 sysfs_remove_bin_file(rmidev
->sysfs_dir
, &attr_data
);
734 kobject_put(rmidev
->sysfs_dir
);
736 dev_data
= rmidev
->data
;
738 rmidev_device_cleanup(dev_data
);
742 unregister_chrdev_region(rmidev
->dev_no
, 1);
744 class_destroy(rmidev_device_class
);
746 kfree(rmidev
->fn_ptr
);
751 complete(&rmidev_remove_complete
);
756 static struct synaptics_rmi4_exp_fn rmidev_module
= {
758 .init
= rmidev_init_device
,
759 .remove
= rmidev_remove_device
,
762 .early_suspend
= NULL
,
769 static int __init
rmidev_module_init(void)
771 synaptics_rmi4_new_function(&rmidev_module
, true);
776 static void __exit
rmidev_module_exit(void)
778 synaptics_rmi4_new_function(&rmidev_module
, false);
780 wait_for_completion(&rmidev_remove_complete
);
785 module_init(rmidev_module_init
);
786 module_exit(rmidev_module_exit
);
788 MODULE_AUTHOR("Synaptics, Inc.");
789 MODULE_DESCRIPTION("Synaptics DSX RMI Dev Module");
790 MODULE_LICENSE("GPL v2");