2  *  Fujitsu Lifebook Application Panel button drive
 
   4  *  Copyright (C) 2007 Stephen Hemminger <shemminger@linux-foundation.org>
 
   5  *  Copyright (C) 2001-2003 Jochen Eisinger <jochen@penguin-breeder.org>
 
   7  * This program is free software; you can redistribute it and/or modify it
 
   8  * under the terms of the GNU General Public License version 2 as published by
 
   9  * the Free Software Foundation.
 
  11  * Many Fujitsu Lifebook laptops have a small panel of buttons that are
 
  12  * accessible via the i2c/smbus interface. This driver polls those
 
  13  * buttons and generates input events.
 
  15  * For more details see:
 
  16  *      http://apanel.sourceforge.net/tech.php
 
  19 #include <linux/kernel.h>
 
  20 #include <linux/module.h>
 
  21 #include <linux/ioport.h>
 
  23 #include <linux/input-polldev.h>
 
  24 #include <linux/i2c.h>
 
  25 #include <linux/workqueue.h>
 
  26 #include <linux/leds.h>
 
  28 #define APANEL_NAME     "Fujitsu Application Panel"
 
  29 #define APANEL_VERSION  "1.3.1"
 
  30 #define APANEL          "apanel"
 
  32 /* How often we poll keys - msecs */
 
  33 #define POLL_INTERVAL_DEFAULT   1000
 
  35 /* Magic constants in BIOS that tell about buttons */
 
  38         APANEL_DEV_APPBTN = 1,
 
  53 /* Result of BIOS snooping/probing -- what features are supported */
 
  54 static enum apanel_chip device_chip[APANEL_DEV_MAX];
 
  56 #define MAX_PANEL_KEYS  12
 
  59         struct input_polled_dev *ipdev;
 
  60         struct i2c_client *client;
 
  61         unsigned short keymap[MAX_PANEL_KEYS];
 
  64         struct work_struct led_work;
 
  65         struct led_classdev mail_led;
 
  69 static int apanel_probe(struct i2c_client *, const struct i2c_device_id *);
 
  71 static void report_key(struct input_dev *input, unsigned keycode)
 
  73         pr_debug(APANEL ": report key %#x\n", keycode);
 
  74         input_report_key(input, keycode, 1);
 
  77         input_report_key(input, keycode, 0);
 
  81 /* Poll for key changes
 
  83  * Read Application keys via SMI
 
  84  *  A (0x4), B (0x8), Internet (0x2), Email (0x1).
 
  87  * Forward (0x100), Rewind (0x200), Stop (0x400), Pause (0x800)
 
  89 static void apanel_poll(struct input_polled_dev *ipdev)
 
  91         struct apanel *ap = ipdev->private;
 
  92         struct input_dev *idev = ipdev->input;
 
  93         u8 cmd = device_chip[APANEL_DEV_APPBTN] == CHIP_OZ992C ? 0 : 8;
 
  97         data = i2c_smbus_read_word_data(ap->client, cmd);
 
  99                 return; /* ignore errors (due to ACPI??) */
 
 101         /* write back to clear latch */
 
 102         i2c_smbus_write_word_data(ap->client, cmd, 0);
 
 107         dev_dbg(&idev->dev, APANEL ": data %#x\n", data);
 
 108         for (i = 0; i < idev->keycodemax; i++)
 
 109                 if ((1u << i) & data)
 
 110                         report_key(idev, ap->keymap[i]);
 
 113 /* Track state changes of LED */
 
 114 static void led_update(struct work_struct *work)
 
 116         struct apanel *ap = container_of(work, struct apanel, led_work);
 
 118         i2c_smbus_write_word_data(ap->client, 0x10, ap->led_bits);
 
 121 static void mail_led_set(struct led_classdev *led,
 
 122                          enum led_brightness value)
 
 124         struct apanel *ap = container_of(led, struct apanel, mail_led);
 
 126         if (value != LED_OFF)
 
 127                 ap->led_bits |= 0x8000;
 
 129                 ap->led_bits &= ~0x8000;
 
 131         schedule_work(&ap->led_work);
 
 134 static int apanel_remove(struct i2c_client *client)
 
 136         struct apanel *ap = i2c_get_clientdata(client);
 
 138         if (device_chip[APANEL_DEV_LED] != CHIP_NONE)
 
 139                 led_classdev_unregister(&ap->mail_led);
 
 141         input_unregister_polled_device(ap->ipdev);
 
 142         input_free_polled_device(ap->ipdev);
 
 147 static void apanel_shutdown(struct i2c_client *client)
 
 149         apanel_remove(client);
 
 152 static struct i2c_device_id apanel_id[] = {
 
 153         { "fujitsu_apanel", 0 },
 
 156 MODULE_DEVICE_TABLE(i2c, apanel_id);
 
 158 static struct i2c_driver apanel_driver = {
 
 162         .probe          = &apanel_probe,
 
 163         .remove         = &apanel_remove,
 
 164         .shutdown       = &apanel_shutdown,
 
 165         .id_table       = apanel_id,
 
 168 static struct apanel apanel = {
 
 178                 [11] = KEY_PLAYPAUSE,
 
 183                 .brightness_set = mail_led_set,
 
 187 /* NB: Only one panel on the i2c. */
 
 188 static int apanel_probe(struct i2c_client *client,
 
 189                         const struct i2c_device_id *id)
 
 192         struct input_polled_dev *ipdev;
 
 193         struct input_dev *idev;
 
 194         u8 cmd = device_chip[APANEL_DEV_APPBTN] == CHIP_OZ992C ? 0 : 8;
 
 195         int i, err = -ENOMEM;
 
 199         ipdev = input_allocate_polled_device();
 
 206         i2c_set_clientdata(client, ap);
 
 208         err = i2c_smbus_write_word_data(client, cmd, 0);
 
 210                 dev_warn(&client->dev, APANEL ": smbus write error %d\n",
 
 215         ipdev->poll = apanel_poll;
 
 216         ipdev->poll_interval = POLL_INTERVAL_DEFAULT;
 
 220         idev->name = APANEL_NAME " buttons";
 
 221         idev->phys = "apanel/input0";
 
 222         idev->id.bustype = BUS_HOST;
 
 223         idev->dev.parent = &client->dev;
 
 225         set_bit(EV_KEY, idev->evbit);
 
 227         idev->keycode = ap->keymap;
 
 228         idev->keycodesize = sizeof(ap->keymap[0]);
 
 229         idev->keycodemax = (device_chip[APANEL_DEV_CDBTN] != CHIP_NONE) ? 12 : 4;
 
 231         for (i = 0; i < idev->keycodemax; i++)
 
 233                         set_bit(ap->keymap[i], idev->keybit);
 
 235         err = input_register_polled_device(ipdev);
 
 239         INIT_WORK(&ap->led_work, led_update);
 
 240         if (device_chip[APANEL_DEV_LED] != CHIP_NONE) {
 
 241                 err = led_classdev_register(&client->dev, &ap->mail_led);
 
 248         input_unregister_polled_device(ipdev);
 
 250         input_free_polled_device(ipdev);
 
 255 /* Scan the system ROM for the signature "FJKEYINF" */
 
 256 static __init const void __iomem *bios_signature(const void __iomem *bios)
 
 259         const unsigned char signature[] = "FJKEYINF";
 
 261         for (offset = 0; offset < 0x10000; offset += 0x10) {
 
 262                 if (check_signature(bios + offset, signature,
 
 263                                     sizeof(signature)-1))
 
 264                         return bios + offset;
 
 266         pr_notice(APANEL ": Fujitsu BIOS signature '%s' not found...\n",
 
 271 static int __init apanel_init(void)
 
 274         const void __iomem *p;
 
 276         unsigned char i2c_addr;
 
 279         bios = ioremap(0xF0000, 0x10000); /* Can't fail */
 
 281         p = bios_signature(bios);
 
 287         /* just use the first address */
 
 289         i2c_addr = readb(p + 3) >> 1;
 
 291         for ( ; (devno = readb(p)) & 0x7f; p += 4) {
 
 292                 unsigned char method, slave, chip;
 
 294                 method = readb(p + 1);
 
 296                 slave = readb(p + 3) >> 1;
 
 298                 if (slave != i2c_addr) {
 
 299                         pr_notice(APANEL ": only one SMBus slave "
 
 300                                   "address supported, skiping device...\n");
 
 304                 /* translate alternative device numbers */
 
 307                         devno = APANEL_DEV_APPBTN;
 
 310                         devno = APANEL_DEV_LED;
 
 314                 if (devno >= APANEL_DEV_MAX)
 
 315                         pr_notice(APANEL ": unknown device %u found\n", devno);
 
 316                 else if (device_chip[devno] != CHIP_NONE)
 
 317                         pr_warning(APANEL ": duplicate entry for devno %u\n", devno);
 
 319                 else if (method != 1 && method != 2 && method != 4) {
 
 320                         pr_notice(APANEL ": unknown method %u for devno %u\n",
 
 323                         device_chip[devno] = (enum apanel_chip) chip;
 
 330                 pr_info(APANEL ": no input devices reported by BIOS\n");
 
 334         return i2c_add_driver(&apanel_driver);
 
 336 module_init(apanel_init);
 
 338 static void __exit apanel_cleanup(void)
 
 340         i2c_del_driver(&apanel_driver);
 
 342 module_exit(apanel_cleanup);
 
 344 MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
 
 345 MODULE_DESCRIPTION(APANEL_NAME " driver");
 
 346 MODULE_LICENSE("GPL");
 
 347 MODULE_VERSION(APANEL_VERSION);
 
 349 MODULE_ALIAS("dmi:*:svnFUJITSU:pnLifeBook*:pvr*:rvnFUJITSU:*");
 
 350 MODULE_ALIAS("dmi:*:svnFUJITSU:pnLifebook*:pvr*:rvnFUJITSU:*");