xref: /openbmc/linux/drivers/input/misc/apanel.c (revision c4e78957)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Fujitsu Lifebook Application Panel button drive
4  *
5  *  Copyright (C) 2007 Stephen Hemminger <shemminger@linux-foundation.org>
6  *  Copyright (C) 2001-2003 Jochen Eisinger <jochen@penguin-breeder.org>
7  *
8  * Many Fujitsu Lifebook laptops have a small panel of buttons that are
9  * accessible via the i2c/smbus interface. This driver polls those
10  * buttons and generates input events.
11  *
12  * For more details see:
13  *	http://apanel.sourceforge.net/tech.php
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/ioport.h>
19 #include <linux/io.h>
20 #include <linux/input.h>
21 #include <linux/i2c.h>
22 #include <linux/leds.h>
23 
24 #define APANEL_NAME	"Fujitsu Application Panel"
25 #define APANEL		"apanel"
26 
27 /* How often we poll keys - msecs */
28 #define POLL_INTERVAL_DEFAULT	1000
29 
30 /* Magic constants in BIOS that tell about buttons */
31 enum apanel_devid {
32 	APANEL_DEV_NONE	  = 0,
33 	APANEL_DEV_APPBTN = 1,
34 	APANEL_DEV_CDBTN  = 2,
35 	APANEL_DEV_LCD	  = 3,
36 	APANEL_DEV_LED	  = 4,
37 
38 	APANEL_DEV_MAX,
39 };
40 
41 enum apanel_chip {
42 	CHIP_NONE    = 0,
43 	CHIP_OZ992C  = 1,
44 	CHIP_OZ163T  = 2,
45 	CHIP_OZ711M3 = 4,
46 };
47 
48 /* Result of BIOS snooping/probing -- what features are supported */
49 static enum apanel_chip device_chip[APANEL_DEV_MAX];
50 
51 #define MAX_PANEL_KEYS	12
52 
53 struct apanel {
54 	struct input_dev *idev;
55 	struct i2c_client *client;
56 	unsigned short keymap[MAX_PANEL_KEYS];
57 	u16 nkeys;
58 	struct led_classdev mail_led;
59 };
60 
61 static const unsigned short apanel_keymap[MAX_PANEL_KEYS] = {
62 	[0] = KEY_MAIL,
63 	[1] = KEY_WWW,
64 	[2] = KEY_PROG2,
65 	[3] = KEY_PROG1,
66 
67 	[8] = KEY_FORWARD,
68 	[9] = KEY_REWIND,
69 	[10] = KEY_STOPCD,
70 	[11] = KEY_PLAYPAUSE,
71 };
72 
73 static void report_key(struct input_dev *input, unsigned keycode)
74 {
75 	dev_dbg(input->dev.parent, "report key %#x\n", keycode);
76 	input_report_key(input, keycode, 1);
77 	input_sync(input);
78 
79 	input_report_key(input, keycode, 0);
80 	input_sync(input);
81 }
82 
83 /* Poll for key changes
84  *
85  * Read Application keys via SMI
86  *  A (0x4), B (0x8), Internet (0x2), Email (0x1).
87  *
88  * CD keys:
89  * Forward (0x100), Rewind (0x200), Stop (0x400), Pause (0x800)
90  */
91 static void apanel_poll(struct input_dev *idev)
92 {
93 	struct apanel *ap = input_get_drvdata(idev);
94 	u8 cmd = device_chip[APANEL_DEV_APPBTN] == CHIP_OZ992C ? 0 : 8;
95 	s32 data;
96 	int i;
97 
98 	data = i2c_smbus_read_word_data(ap->client, cmd);
99 	if (data < 0)
100 		return;	/* ignore errors (due to ACPI??) */
101 
102 	/* write back to clear latch */
103 	i2c_smbus_write_word_data(ap->client, cmd, 0);
104 
105 	if (!data)
106 		return;
107 
108 	dev_dbg(&idev->dev, APANEL ": data %#x\n", data);
109 	for (i = 0; i < idev->keycodemax; i++)
110 		if ((1u << i) & data)
111 			report_key(idev, ap->keymap[i]);
112 }
113 
114 static int mail_led_set(struct led_classdev *led,
115 			 enum led_brightness value)
116 {
117 	struct apanel *ap = container_of(led, struct apanel, mail_led);
118 	u16 led_bits = value != LED_OFF ? 0x8000 : 0x0000;
119 
120 	return i2c_smbus_write_word_data(ap->client, 0x10, led_bits);
121 }
122 
123 static int apanel_probe(struct i2c_client *client,
124 			const struct i2c_device_id *id)
125 {
126 	struct apanel *ap;
127 	struct input_dev *idev;
128 	u8 cmd = device_chip[APANEL_DEV_APPBTN] == CHIP_OZ992C ? 0 : 8;
129 	int i, err;
130 
131 	ap = devm_kzalloc(&client->dev, sizeof(*ap), GFP_KERNEL);
132 	if (!ap)
133 		return -ENOMEM;
134 
135 	idev = devm_input_allocate_device(&client->dev);
136 	if (!idev)
137 		return -ENOMEM;
138 
139 	ap->idev = idev;
140 	ap->client = client;
141 
142 	i2c_set_clientdata(client, ap);
143 
144 	err = i2c_smbus_write_word_data(client, cmd, 0);
145 	if (err) {
146 		dev_warn(&client->dev, "smbus write error %d\n", err);
147 		return err;
148 	}
149 
150 	input_set_drvdata(idev, ap);
151 
152 	idev->name = APANEL_NAME " buttons";
153 	idev->phys = "apanel/input0";
154 	idev->id.bustype = BUS_HOST;
155 
156 	memcpy(ap->keymap, apanel_keymap, sizeof(apanel_keymap));
157 	idev->keycode = ap->keymap;
158 	idev->keycodesize = sizeof(ap->keymap[0]);
159 	idev->keycodemax = (device_chip[APANEL_DEV_CDBTN] != CHIP_NONE) ? 12 : 4;
160 
161 	set_bit(EV_KEY, idev->evbit);
162 	for (i = 0; i < idev->keycodemax; i++)
163 		if (ap->keymap[i])
164 			set_bit(ap->keymap[i], idev->keybit);
165 
166 	err = input_setup_polling(idev, apanel_poll);
167 	if (err)
168 		return err;
169 
170 	input_set_poll_interval(idev, POLL_INTERVAL_DEFAULT);
171 
172 	err = input_register_device(idev);
173 	if (err)
174 		return err;
175 
176 	if (device_chip[APANEL_DEV_LED] != CHIP_NONE) {
177 		ap->mail_led.name = "mail:blue";
178 		ap->mail_led.brightness_set_blocking = mail_led_set;
179 		err = devm_led_classdev_register(&client->dev, &ap->mail_led);
180 		if (err)
181 			return err;
182 	}
183 
184 	return 0;
185 }
186 
187 static void apanel_shutdown(struct i2c_client *client)
188 {
189 	struct apanel *ap = i2c_get_clientdata(client);
190 
191 	if (device_chip[APANEL_DEV_LED] != CHIP_NONE)
192 		led_set_brightness(&ap->mail_led, LED_OFF);
193 }
194 
195 static const struct i2c_device_id apanel_id[] = {
196 	{ "fujitsu_apanel", 0 },
197 	{ }
198 };
199 MODULE_DEVICE_TABLE(i2c, apanel_id);
200 
201 static struct i2c_driver apanel_driver = {
202 	.driver = {
203 		.name = APANEL,
204 	},
205 	.probe		= apanel_probe,
206 	.shutdown	= apanel_shutdown,
207 	.id_table	= apanel_id,
208 };
209 
210 /* Scan the system ROM for the signature "FJKEYINF" */
211 static __init const void __iomem *bios_signature(const void __iomem *bios)
212 {
213 	ssize_t offset;
214 	const unsigned char signature[] = "FJKEYINF";
215 
216 	for (offset = 0; offset < 0x10000; offset += 0x10) {
217 		if (check_signature(bios + offset, signature,
218 				    sizeof(signature)-1))
219 			return bios + offset;
220 	}
221 	pr_notice(APANEL ": Fujitsu BIOS signature '%s' not found...\n",
222 		  signature);
223 	return NULL;
224 }
225 
226 static int __init apanel_init(void)
227 {
228 	void __iomem *bios;
229 	const void __iomem *p;
230 	u8 devno;
231 	unsigned char i2c_addr;
232 	int found = 0;
233 
234 	bios = ioremap(0xF0000, 0x10000); /* Can't fail */
235 
236 	p = bios_signature(bios);
237 	if (!p) {
238 		iounmap(bios);
239 		return -ENODEV;
240 	}
241 
242 	/* just use the first address */
243 	p += 8;
244 	i2c_addr = readb(p + 3) >> 1;
245 
246 	for ( ; (devno = readb(p)) & 0x7f; p += 4) {
247 		unsigned char method, slave, chip;
248 
249 		method = readb(p + 1);
250 		chip = readb(p + 2);
251 		slave = readb(p + 3) >> 1;
252 
253 		if (slave != i2c_addr) {
254 			pr_notice(APANEL ": only one SMBus slave "
255 				  "address supported, skipping device...\n");
256 			continue;
257 		}
258 
259 		/* translate alternative device numbers */
260 		switch (devno) {
261 		case 6:
262 			devno = APANEL_DEV_APPBTN;
263 			break;
264 		case 7:
265 			devno = APANEL_DEV_LED;
266 			break;
267 		}
268 
269 		if (devno >= APANEL_DEV_MAX)
270 			pr_notice(APANEL ": unknown device %u found\n", devno);
271 		else if (device_chip[devno] != CHIP_NONE)
272 			pr_warn(APANEL ": duplicate entry for devno %u\n",
273 				devno);
274 
275 		else if (method != 1 && method != 2 && method != 4) {
276 			pr_notice(APANEL ": unknown method %u for devno %u\n",
277 				  method, devno);
278 		} else {
279 			device_chip[devno] = (enum apanel_chip) chip;
280 			++found;
281 		}
282 	}
283 	iounmap(bios);
284 
285 	if (found == 0) {
286 		pr_info(APANEL ": no input devices reported by BIOS\n");
287 		return -EIO;
288 	}
289 
290 	return i2c_add_driver(&apanel_driver);
291 }
292 module_init(apanel_init);
293 
294 static void __exit apanel_cleanup(void)
295 {
296 	i2c_del_driver(&apanel_driver);
297 }
298 module_exit(apanel_cleanup);
299 
300 MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
301 MODULE_DESCRIPTION(APANEL_NAME " driver");
302 MODULE_LICENSE("GPL");
303 
304 MODULE_ALIAS("dmi:*:svnFUJITSU:pnLifeBook*:pvr*:rvnFUJITSU:*");
305 MODULE_ALIAS("dmi:*:svnFUJITSU:pnLifebook*:pvr*:rvnFUJITSU:*");
306