apanel.c 7.9 KB

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