max7359_keypad.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * max7359_keypad.c - MAX7359 Key Switch Controller Driver
  3. *
  4. * Copyright (C) 2009 Samsung Electronics
  5. * Kim Kyuwon <q1.kim@samsung.com>
  6. *
  7. * Based on pxa27x_keypad.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Datasheet: http://www.maxim-ic.com/quick_view2.cfm/qv_pk/5456
  14. */
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/slab.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/pm.h>
  20. #include <linux/input.h>
  21. #include <linux/input/matrix_keypad.h>
  22. #define MAX7359_MAX_KEY_ROWS 8
  23. #define MAX7359_MAX_KEY_COLS 8
  24. #define MAX7359_MAX_KEY_NUM (MAX7359_MAX_KEY_ROWS * MAX7359_MAX_KEY_COLS)
  25. #define MAX7359_ROW_SHIFT 3
  26. /*
  27. * MAX7359 registers
  28. */
  29. #define MAX7359_REG_KEYFIFO 0x00
  30. #define MAX7359_REG_CONFIG 0x01
  31. #define MAX7359_REG_DEBOUNCE 0x02
  32. #define MAX7359_REG_INTERRUPT 0x03
  33. #define MAX7359_REG_PORTS 0x04
  34. #define MAX7359_REG_KEYREP 0x05
  35. #define MAX7359_REG_SLEEP 0x06
  36. /*
  37. * Configuration register bits
  38. */
  39. #define MAX7359_CFG_SLEEP (1 << 7)
  40. #define MAX7359_CFG_INTERRUPT (1 << 5)
  41. #define MAX7359_CFG_KEY_RELEASE (1 << 3)
  42. #define MAX7359_CFG_WAKEUP (1 << 1)
  43. #define MAX7359_CFG_TIMEOUT (1 << 0)
  44. /*
  45. * Autosleep register values (ms)
  46. */
  47. #define MAX7359_AUTOSLEEP_8192 0x01
  48. #define MAX7359_AUTOSLEEP_4096 0x02
  49. #define MAX7359_AUTOSLEEP_2048 0x03
  50. #define MAX7359_AUTOSLEEP_1024 0x04
  51. #define MAX7359_AUTOSLEEP_512 0x05
  52. #define MAX7359_AUTOSLEEP_256 0x06
  53. struct max7359_keypad {
  54. /* matrix key code map */
  55. unsigned short keycodes[MAX7359_MAX_KEY_NUM];
  56. struct input_dev *input_dev;
  57. struct i2c_client *client;
  58. };
  59. static int max7359_write_reg(struct i2c_client *client, u8 reg, u8 val)
  60. {
  61. int ret = i2c_smbus_write_byte_data(client, reg, val);
  62. if (ret < 0)
  63. dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
  64. __func__, reg, val, ret);
  65. return ret;
  66. }
  67. static int max7359_read_reg(struct i2c_client *client, int reg)
  68. {
  69. int ret = i2c_smbus_read_byte_data(client, reg);
  70. if (ret < 0)
  71. dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
  72. __func__, reg, ret);
  73. return ret;
  74. }
  75. /* runs in an IRQ thread -- can (and will!) sleep */
  76. static irqreturn_t max7359_interrupt(int irq, void *dev_id)
  77. {
  78. struct max7359_keypad *keypad = dev_id;
  79. struct input_dev *input_dev = keypad->input_dev;
  80. int val, row, col, release, code;
  81. val = max7359_read_reg(keypad->client, MAX7359_REG_KEYFIFO);
  82. row = val & 0x7;
  83. col = (val >> 3) & 0x7;
  84. release = val & 0x40;
  85. code = MATRIX_SCAN_CODE(row, col, MAX7359_ROW_SHIFT);
  86. dev_dbg(&keypad->client->dev,
  87. "key[%d:%d] %s\n", row, col, release ? "release" : "press");
  88. input_event(input_dev, EV_MSC, MSC_SCAN, code);
  89. input_report_key(input_dev, keypad->keycodes[code], !release);
  90. input_sync(input_dev);
  91. return IRQ_HANDLED;
  92. }
  93. /*
  94. * Let MAX7359 fall into a deep sleep:
  95. * If no keys are pressed, enter sleep mode for 8192 ms. And if any
  96. * key is pressed, the MAX7359 returns to normal operating mode.
  97. */
  98. static inline void max7359_fall_deepsleep(struct i2c_client *client)
  99. {
  100. max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_8192);
  101. }
  102. /*
  103. * Let MAX7359 take a catnap:
  104. * Autosleep just for 256 ms.
  105. */
  106. static inline void max7359_take_catnap(struct i2c_client *client)
  107. {
  108. max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_256);
  109. }
  110. static int max7359_open(struct input_dev *dev)
  111. {
  112. struct max7359_keypad *keypad = input_get_drvdata(dev);
  113. max7359_take_catnap(keypad->client);
  114. return 0;
  115. }
  116. static void max7359_close(struct input_dev *dev)
  117. {
  118. struct max7359_keypad *keypad = input_get_drvdata(dev);
  119. max7359_fall_deepsleep(keypad->client);
  120. }
  121. static void max7359_initialize(struct i2c_client *client)
  122. {
  123. max7359_write_reg(client, MAX7359_REG_CONFIG,
  124. MAX7359_CFG_KEY_RELEASE | /* Key release enable */
  125. MAX7359_CFG_WAKEUP); /* Key press wakeup enable */
  126. /* Full key-scan functionality */
  127. max7359_write_reg(client, MAX7359_REG_DEBOUNCE, 0x1F);
  128. /* nINT asserts every debounce cycles */
  129. max7359_write_reg(client, MAX7359_REG_INTERRUPT, 0x01);
  130. max7359_fall_deepsleep(client);
  131. }
  132. static int max7359_probe(struct i2c_client *client,
  133. const struct i2c_device_id *id)
  134. {
  135. const struct matrix_keymap_data *keymap_data =
  136. dev_get_platdata(&client->dev);
  137. struct max7359_keypad *keypad;
  138. struct input_dev *input_dev;
  139. int ret;
  140. int error;
  141. if (!client->irq) {
  142. dev_err(&client->dev, "The irq number should not be zero\n");
  143. return -EINVAL;
  144. }
  145. /* Detect MAX7359: The initial Keys FIFO value is '0x3F' */
  146. ret = max7359_read_reg(client, MAX7359_REG_KEYFIFO);
  147. if (ret < 0) {
  148. dev_err(&client->dev, "failed to detect device\n");
  149. return -ENODEV;
  150. }
  151. dev_dbg(&client->dev, "keys FIFO is 0x%02x\n", ret);
  152. keypad = devm_kzalloc(&client->dev, sizeof(struct max7359_keypad),
  153. GFP_KERNEL);
  154. if (!keypad) {
  155. dev_err(&client->dev, "failed to allocate memory\n");
  156. return -ENOMEM;
  157. }
  158. input_dev = devm_input_allocate_device(&client->dev);
  159. if (!input_dev) {
  160. dev_err(&client->dev, "failed to allocate input device\n");
  161. return -ENOMEM;
  162. }
  163. keypad->client = client;
  164. keypad->input_dev = input_dev;
  165. input_dev->name = client->name;
  166. input_dev->id.bustype = BUS_I2C;
  167. input_dev->open = max7359_open;
  168. input_dev->close = max7359_close;
  169. input_dev->dev.parent = &client->dev;
  170. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  171. input_dev->keycodesize = sizeof(keypad->keycodes[0]);
  172. input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
  173. input_dev->keycode = keypad->keycodes;
  174. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  175. input_set_drvdata(input_dev, keypad);
  176. error = matrix_keypad_build_keymap(keymap_data, NULL,
  177. MAX7359_MAX_KEY_ROWS,
  178. MAX7359_MAX_KEY_COLS,
  179. keypad->keycodes,
  180. input_dev);
  181. if (error) {
  182. dev_err(&client->dev, "failed to build keymap\n");
  183. return error;
  184. }
  185. error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  186. max7359_interrupt,
  187. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  188. client->name, keypad);
  189. if (error) {
  190. dev_err(&client->dev, "failed to register interrupt\n");
  191. return error;
  192. }
  193. /* Register the input device */
  194. error = input_register_device(input_dev);
  195. if (error) {
  196. dev_err(&client->dev, "failed to register input device\n");
  197. return error;
  198. }
  199. /* Initialize MAX7359 */
  200. max7359_initialize(client);
  201. i2c_set_clientdata(client, keypad);
  202. device_init_wakeup(&client->dev, 1);
  203. return 0;
  204. }
  205. #ifdef CONFIG_PM_SLEEP
  206. static int max7359_suspend(struct device *dev)
  207. {
  208. struct i2c_client *client = to_i2c_client(dev);
  209. max7359_fall_deepsleep(client);
  210. if (device_may_wakeup(&client->dev))
  211. enable_irq_wake(client->irq);
  212. return 0;
  213. }
  214. static int max7359_resume(struct device *dev)
  215. {
  216. struct i2c_client *client = to_i2c_client(dev);
  217. if (device_may_wakeup(&client->dev))
  218. disable_irq_wake(client->irq);
  219. /* Restore the default setting */
  220. max7359_take_catnap(client);
  221. return 0;
  222. }
  223. #endif
  224. static SIMPLE_DEV_PM_OPS(max7359_pm, max7359_suspend, max7359_resume);
  225. static const struct i2c_device_id max7359_ids[] = {
  226. { "max7359", 0 },
  227. { }
  228. };
  229. MODULE_DEVICE_TABLE(i2c, max7359_ids);
  230. static struct i2c_driver max7359_i2c_driver = {
  231. .driver = {
  232. .name = "max7359",
  233. .pm = &max7359_pm,
  234. },
  235. .probe = max7359_probe,
  236. .id_table = max7359_ids,
  237. };
  238. module_i2c_driver(max7359_i2c_driver);
  239. MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>");
  240. MODULE_DESCRIPTION("MAX7359 Key Switch Controller Driver");
  241. MODULE_LICENSE("GPL v2");