apanel.c 7.8 KB

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