apanel.c 8.7 KB

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