• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Google, Inc.
3  * Copyright (C) 2012 Intel, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/types.h>
20 #include <linux/input.h>
21 #include <linux/kernel.h>
22 #include <linux/platform_device.h>
23 #include <linux/irq.h>
24 #include <linux/io.h>
25 #include <linux/acpi.h>
26 
27 enum {
28 	REG_READ        = 0x00,
29 	REG_SET_PAGE    = 0x00,
30 	REG_LEN         = 0x04,
31 	REG_DATA        = 0x08,
32 
33 	PAGE_NAME       = 0x00000,
34 	PAGE_EVBITS     = 0x10000,
35 	PAGE_ABSDATA    = 0x20000 | EV_ABS,
36 };
37 
38 struct event_dev {
39 	struct input_dev *input;
40 	int irq;
41 	void __iomem *addr;
42 	char name[0];
43 };
44 
rotary_interrupt(int irq,void * dev_id)45 static irqreturn_t rotary_interrupt(int irq, void *dev_id)
46 {
47 	struct event_dev *edev = dev_id;
48 	unsigned type, code, value;
49 
50 	type = __raw_readl(edev->addr + REG_READ);
51 	code = __raw_readl(edev->addr + REG_READ);
52 	value = __raw_readl(edev->addr + REG_READ);
53 
54 	input_event(edev->input, type, code, value);
55 	return IRQ_HANDLED;
56 }
57 
rotary_import_bits(struct event_dev * edev,unsigned long bits[],unsigned type,size_t count)58 static void rotary_import_bits(struct event_dev *edev,
59 			unsigned long bits[], unsigned type, size_t count)
60 {
61 	void __iomem *addr = edev->addr;
62 	int i, j;
63 	size_t size;
64 	uint8_t val;
65 
66 	__raw_writel(PAGE_EVBITS | type, addr + REG_SET_PAGE);
67 
68 	size = __raw_readl(addr + REG_LEN) * 8;
69 	if (size < count)
70 		count = size;
71 
72 	addr += REG_DATA;
73 	for (i = 0; i < count; i += 8) {
74 		val = __raw_readb(addr++);
75 		for (j = 0; j < 8; j++)
76 			if (val & 1 << j)
77 				set_bit(i + j, bits);
78 	}
79 }
80 
rotary_import_abs_params(struct event_dev * edev)81 static void rotary_import_abs_params(struct event_dev *edev)
82 {
83 	struct input_dev *input_dev = edev->input;
84 	void __iomem *addr = edev->addr;
85 	u32 val[4];
86 	int count;
87 	int i, j;
88 
89 	__raw_writel(PAGE_ABSDATA, addr + REG_SET_PAGE);
90 
91 	count = __raw_readl(addr + REG_LEN) / sizeof(val);
92 	if (count > ABS_MAX)
93 		count = ABS_MAX;
94 
95 	for (i = 0; i < count; i++) {
96 		if (!test_bit(i, input_dev->absbit))
97 			continue;
98 
99 		for (j = 0; j < ARRAY_SIZE(val); j++) {
100 			int offset = (i * ARRAY_SIZE(val) + j) * sizeof(u32);
101 			val[j] = __raw_readl(edev->addr + REG_DATA + offset);
102 		}
103 
104 		input_set_abs_params(input_dev, i,
105 				     val[0], val[1], val[2], val[3]);
106 	}
107 }
108 
rotary_probe(struct platform_device * pdev)109 static int rotary_probe(struct platform_device *pdev)
110 {
111 	struct input_dev *input_dev;
112 	struct event_dev *edev;
113 	struct resource *res;
114 	unsigned keymapnamelen;
115 	void __iomem *addr;
116 	int irq;
117 	int i;
118 	int error;
119 
120 	irq = platform_get_irq(pdev, 0);
121 	if (irq < 0)
122 		return -EINVAL;
123 
124 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
125 	if (!res)
126 		return -EINVAL;
127 
128 	addr = devm_ioremap(&pdev->dev, res->start, 4096);
129 	if (!addr)
130 		return -ENOMEM;
131 
132 	__raw_writel(PAGE_NAME, addr + REG_SET_PAGE);
133 	keymapnamelen = __raw_readl(addr + REG_LEN);
134 
135 	edev = devm_kzalloc(&pdev->dev,
136 			    sizeof(struct event_dev) + keymapnamelen + 1,
137 			    GFP_KERNEL);
138 	if (!edev)
139 		return -ENOMEM;
140 
141 	input_dev = devm_input_allocate_device(&pdev->dev);
142 	if (!input_dev)
143 		return -ENOMEM;
144 
145 	edev->input = input_dev;
146 	edev->addr = addr;
147 	edev->irq = irq;
148 
149 	for (i = 0; i < keymapnamelen; i++)
150 		edev->name[i] = __raw_readb(edev->addr + REG_DATA + i);
151 
152 	pr_debug("rotary_probe() keymap=%s\n", edev->name);
153 
154 	input_dev->name = edev->name;
155 	input_dev->id.bustype = BUS_HOST;
156 	rotary_import_bits(edev, input_dev->evbit, EV_SYN, EV_MAX);
157 	rotary_import_bits(edev, input_dev->relbit, EV_REL, REL_MAX);
158 	rotary_import_bits(edev, input_dev->absbit, EV_ABS, ABS_MAX);
159 
160 	rotary_import_abs_params(edev);
161 
162 	error = devm_request_irq(&pdev->dev, edev->irq, rotary_interrupt, 0,
163 				 "goldfish-rotary", edev);
164 	if (error)
165 		return error;
166 
167 	error = input_register_device(input_dev);
168 	if (error)
169 		return error;
170 
171 	return 0;
172 }
173 
174 static const struct of_device_id goldfish_rotary_of_match[] = {
175 	{ .compatible = "generic,goldfish-rotary", },
176 	{},
177 };
178 MODULE_DEVICE_TABLE(of, goldfish_rotary_of_match);
179 
180 static const struct acpi_device_id goldfish_rotary_acpi_match[] = {
181 	{ "GFSH0008", 0 },
182 	{ },
183 };
184 MODULE_DEVICE_TABLE(acpi, goldfish_rotary_acpi_match);
185 
186 static struct platform_driver rotary_driver = {
187 	.probe	= rotary_probe,
188 	.driver	= {
189 		.owner	= THIS_MODULE,
190 		.name	= "goldfish_rotary",
191 		.of_match_table = goldfish_rotary_of_match,
192 		.acpi_match_table = ACPI_PTR(goldfish_rotary_acpi_match),
193 	},
194 };
195 
196 module_platform_driver(rotary_driver);
197 
198 MODULE_AUTHOR("Nimrod Gileadi");
199 MODULE_DESCRIPTION("Goldfish Rotary Encoder Device");
200 MODULE_LICENSE("GPL");
201