1 // SPDX-License-Identifier: GPL-2.0-only
3 * Fujitsu Lifebook Application Panel button drive
5 * Copyright (C) 2007 Stephen Hemminger <shemminger@linux-foundation.org>
6 * Copyright (C) 2001-2003 Jochen Eisinger <jochen@penguin-breeder.org>
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.
12 * For more details see:
13 * http://apanel.sourceforge.net/tech.php
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/ioport.h>
20 #include <linux/input-polldev.h>
21 #include <linux/i2c.h>
22 #include <linux/leds.h>
24 #define APANEL_NAME "Fujitsu Application Panel"
25 #define APANEL "apanel"
27 /* How often we poll keys - msecs */
28 #define POLL_INTERVAL_DEFAULT 1000
30 /* Magic constants in BIOS that tell about buttons */
33 APANEL_DEV_APPBTN = 1,
48 /* Result of BIOS snooping/probing -- what features are supported */
49 static enum apanel_chip device_chip[APANEL_DEV_MAX];
51 #define MAX_PANEL_KEYS 12
54 struct input_polled_dev *ipdev;
55 struct i2c_client *client;
56 unsigned short keymap[MAX_PANEL_KEYS];
58 struct led_classdev mail_led;
62 static int apanel_probe(struct i2c_client *, const struct i2c_device_id *);
64 static void report_key(struct input_dev *input, unsigned keycode)
66 pr_debug(APANEL ": report key %#x\n", keycode);
67 input_report_key(input, keycode, 1);
70 input_report_key(input, keycode, 0);
74 /* Poll for key changes
76 * Read Application keys via SMI
77 * A (0x4), B (0x8), Internet (0x2), Email (0x1).
80 * Forward (0x100), Rewind (0x200), Stop (0x400), Pause (0x800)
82 static void apanel_poll(struct input_polled_dev *ipdev)
84 struct apanel *ap = ipdev->private;
85 struct input_dev *idev = ipdev->input;
86 u8 cmd = device_chip[APANEL_DEV_APPBTN] == CHIP_OZ992C ? 0 : 8;
90 data = i2c_smbus_read_word_data(ap->client, cmd);
92 return; /* ignore errors (due to ACPI??) */
94 /* write back to clear latch */
95 i2c_smbus_write_word_data(ap->client, cmd, 0);
100 dev_dbg(&idev->dev, APANEL ": data %#x\n", data);
101 for (i = 0; i < idev->keycodemax; i++)
102 if ((1u << i) & data)
103 report_key(idev, ap->keymap[i]);
106 static int mail_led_set(struct led_classdev *led,
107 enum led_brightness value)
109 struct apanel *ap = container_of(led, struct apanel, mail_led);
110 u16 led_bits = value != LED_OFF ? 0x8000 : 0x0000;
112 return i2c_smbus_write_word_data(ap->client, 0x10, led_bits);
115 static int apanel_remove(struct i2c_client *client)
117 struct apanel *ap = i2c_get_clientdata(client);
119 if (device_chip[APANEL_DEV_LED] != CHIP_NONE)
120 led_classdev_unregister(&ap->mail_led);
122 input_unregister_polled_device(ap->ipdev);
123 input_free_polled_device(ap->ipdev);
128 static void apanel_shutdown(struct i2c_client *client)
130 apanel_remove(client);
133 static const struct i2c_device_id apanel_id[] = {
134 { "fujitsu_apanel", 0 },
137 MODULE_DEVICE_TABLE(i2c, apanel_id);
139 static struct i2c_driver apanel_driver = {
143 .probe = &apanel_probe,
144 .remove = &apanel_remove,
145 .shutdown = &apanel_shutdown,
146 .id_table = apanel_id,
149 static struct apanel apanel = {
159 [11] = KEY_PLAYPAUSE,
164 .brightness_set_blocking = mail_led_set,
168 /* NB: Only one panel on the i2c. */
169 static int apanel_probe(struct i2c_client *client,
170 const struct i2c_device_id *id)
173 struct input_polled_dev *ipdev;
174 struct input_dev *idev;
175 u8 cmd = device_chip[APANEL_DEV_APPBTN] == CHIP_OZ992C ? 0 : 8;
176 int i, err = -ENOMEM;
180 ipdev = input_allocate_polled_device();
187 i2c_set_clientdata(client, ap);
189 err = i2c_smbus_write_word_data(client, cmd, 0);
191 dev_warn(&client->dev, APANEL ": smbus write error %d\n",
196 ipdev->poll = apanel_poll;
197 ipdev->poll_interval = POLL_INTERVAL_DEFAULT;
201 idev->name = APANEL_NAME " buttons";
202 idev->phys = "apanel/input0";
203 idev->id.bustype = BUS_HOST;
204 idev->dev.parent = &client->dev;
206 set_bit(EV_KEY, idev->evbit);
208 idev->keycode = ap->keymap;
209 idev->keycodesize = sizeof(ap->keymap[0]);
210 idev->keycodemax = (device_chip[APANEL_DEV_CDBTN] != CHIP_NONE) ? 12 : 4;
212 for (i = 0; i < idev->keycodemax; i++)
214 set_bit(ap->keymap[i], idev->keybit);
216 err = input_register_polled_device(ipdev);
220 if (device_chip[APANEL_DEV_LED] != CHIP_NONE) {
221 err = led_classdev_register(&client->dev, &ap->mail_led);
228 input_unregister_polled_device(ipdev);
230 input_free_polled_device(ipdev);
235 /* Scan the system ROM for the signature "FJKEYINF" */
236 static __init const void __iomem *bios_signature(const void __iomem *bios)
239 const unsigned char signature[] = "FJKEYINF";
241 for (offset = 0; offset < 0x10000; offset += 0x10) {
242 if (check_signature(bios + offset, signature,
243 sizeof(signature)-1))
244 return bios + offset;
246 pr_notice(APANEL ": Fujitsu BIOS signature '%s' not found...\n",
251 static int __init apanel_init(void)
254 const void __iomem *p;
256 unsigned char i2c_addr;
259 bios = ioremap(0xF0000, 0x10000); /* Can't fail */
261 p = bios_signature(bios);
267 /* just use the first address */
269 i2c_addr = readb(p + 3) >> 1;
271 for ( ; (devno = readb(p)) & 0x7f; p += 4) {
272 unsigned char method, slave, chip;
274 method = readb(p + 1);
276 slave = readb(p + 3) >> 1;
278 if (slave != i2c_addr) {
279 pr_notice(APANEL ": only one SMBus slave "
280 "address supported, skipping device...\n");
284 /* translate alternative device numbers */
287 devno = APANEL_DEV_APPBTN;
290 devno = APANEL_DEV_LED;
294 if (devno >= APANEL_DEV_MAX)
295 pr_notice(APANEL ": unknown device %u found\n", devno);
296 else if (device_chip[devno] != CHIP_NONE)
297 pr_warn(APANEL ": duplicate entry for devno %u\n",
300 else if (method != 1 && method != 2 && method != 4) {
301 pr_notice(APANEL ": unknown method %u for devno %u\n",
304 device_chip[devno] = (enum apanel_chip) chip;
311 pr_info(APANEL ": no input devices reported by BIOS\n");
315 return i2c_add_driver(&apanel_driver);
317 module_init(apanel_init);
319 static void __exit apanel_cleanup(void)
321 i2c_del_driver(&apanel_driver);
323 module_exit(apanel_cleanup);
325 MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
326 MODULE_DESCRIPTION(APANEL_NAME " driver");
327 MODULE_LICENSE("GPL");
329 MODULE_ALIAS("dmi:*:svnFUJITSU:pnLifeBook*:pvr*:rvnFUJITSU:*");
330 MODULE_ALIAS("dmi:*:svnFUJITSU:pnLifebook*:pvr*:rvnFUJITSU:*");