apanel.c 8.6 KB

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