pxa27x_keypad.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. * linux/drivers/input/keyboard/pxa27x_keypad.c
  3. *
  4. * Driver for the pxa27x matrix keyboard controller.
  5. *
  6. * Created: Feb 22, 2007
  7. * Author: Rodolfo Giometti <giometti@linux.it>
  8. *
  9. * Based on a previous implementations by Kevin O'Connor
  10. * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and
  11. * on some suggestions by Nicolas Pitre <nico@fluxnic.net>.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/input.h>
  22. #include <linux/device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <linux/input/matrix_keypad.h>
  27. #include <linux/slab.h>
  28. #include <asm/mach/arch.h>
  29. #include <asm/mach/map.h>
  30. #include <mach/hardware.h>
  31. #include <linux/platform_data/keypad-pxa27x.h>
  32. /*
  33. * Keypad Controller registers
  34. */
  35. #define KPC 0x0000 /* Keypad Control register */
  36. #define KPDK 0x0008 /* Keypad Direct Key register */
  37. #define KPREC 0x0010 /* Keypad Rotary Encoder register */
  38. #define KPMK 0x0018 /* Keypad Matrix Key register */
  39. #define KPAS 0x0020 /* Keypad Automatic Scan register */
  40. /* Keypad Automatic Scan Multiple Key Presser register 0-3 */
  41. #define KPASMKP0 0x0028
  42. #define KPASMKP1 0x0030
  43. #define KPASMKP2 0x0038
  44. #define KPASMKP3 0x0040
  45. #define KPKDI 0x0048
  46. /* bit definitions */
  47. #define KPC_MKRN(n) ((((n) - 1) & 0x7) << 26) /* matrix key row number */
  48. #define KPC_MKCN(n) ((((n) - 1) & 0x7) << 23) /* matrix key column number */
  49. #define KPC_DKN(n) ((((n) - 1) & 0x7) << 6) /* direct key number */
  50. #define KPC_AS (0x1 << 30) /* Automatic Scan bit */
  51. #define KPC_ASACT (0x1 << 29) /* Automatic Scan on Activity */
  52. #define KPC_MI (0x1 << 22) /* Matrix interrupt bit */
  53. #define KPC_IMKP (0x1 << 21) /* Ignore Multiple Key Press */
  54. #define KPC_MS(n) (0x1 << (13 + (n))) /* Matrix scan line 'n' */
  55. #define KPC_MS_ALL (0xff << 13)
  56. #define KPC_ME (0x1 << 12) /* Matrix Keypad Enable */
  57. #define KPC_MIE (0x1 << 11) /* Matrix Interrupt Enable */
  58. #define KPC_DK_DEB_SEL (0x1 << 9) /* Direct Keypad Debounce Select */
  59. #define KPC_DI (0x1 << 5) /* Direct key interrupt bit */
  60. #define KPC_RE_ZERO_DEB (0x1 << 4) /* Rotary Encoder Zero Debounce */
  61. #define KPC_REE1 (0x1 << 3) /* Rotary Encoder1 Enable */
  62. #define KPC_REE0 (0x1 << 2) /* Rotary Encoder0 Enable */
  63. #define KPC_DE (0x1 << 1) /* Direct Keypad Enable */
  64. #define KPC_DIE (0x1 << 0) /* Direct Keypad interrupt Enable */
  65. #define KPDK_DKP (0x1 << 31)
  66. #define KPDK_DK(n) ((n) & 0xff)
  67. #define KPREC_OF1 (0x1 << 31)
  68. #define kPREC_UF1 (0x1 << 30)
  69. #define KPREC_OF0 (0x1 << 15)
  70. #define KPREC_UF0 (0x1 << 14)
  71. #define KPREC_RECOUNT0(n) ((n) & 0xff)
  72. #define KPREC_RECOUNT1(n) (((n) >> 16) & 0xff)
  73. #define KPMK_MKP (0x1 << 31)
  74. #define KPAS_SO (0x1 << 31)
  75. #define KPASMKPx_SO (0x1 << 31)
  76. #define KPAS_MUKP(n) (((n) >> 26) & 0x1f)
  77. #define KPAS_RP(n) (((n) >> 4) & 0xf)
  78. #define KPAS_CP(n) ((n) & 0xf)
  79. #define KPASMKP_MKC_MASK (0xff)
  80. #define keypad_readl(off) __raw_readl(keypad->mmio_base + (off))
  81. #define keypad_writel(off, v) __raw_writel((v), keypad->mmio_base + (off))
  82. #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
  83. #define MAX_KEYPAD_KEYS (MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM)
  84. struct pxa27x_keypad {
  85. const struct pxa27x_keypad_platform_data *pdata;
  86. struct clk *clk;
  87. struct input_dev *input_dev;
  88. void __iomem *mmio_base;
  89. int irq;
  90. unsigned short keycodes[MAX_KEYPAD_KEYS];
  91. int rotary_rel_code[2];
  92. /* state row bits of each column scan */
  93. uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
  94. uint32_t direct_key_state;
  95. unsigned int direct_key_mask;
  96. };
  97. #ifdef CONFIG_OF
  98. static int pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad *keypad,
  99. struct pxa27x_keypad_platform_data *pdata)
  100. {
  101. struct input_dev *input_dev = keypad->input_dev;
  102. struct device *dev = input_dev->dev.parent;
  103. u32 rows, cols;
  104. int error;
  105. error = matrix_keypad_parse_of_params(dev, &rows, &cols);
  106. if (error)
  107. return error;
  108. if (rows > MAX_MATRIX_KEY_ROWS || cols > MAX_MATRIX_KEY_COLS) {
  109. dev_err(dev, "rows or cols exceeds maximum value\n");
  110. return -EINVAL;
  111. }
  112. pdata->matrix_key_rows = rows;
  113. pdata->matrix_key_cols = cols;
  114. error = matrix_keypad_build_keymap(NULL, NULL,
  115. pdata->matrix_key_rows,
  116. pdata->matrix_key_cols,
  117. keypad->keycodes, input_dev);
  118. if (error)
  119. return error;
  120. return 0;
  121. }
  122. static int pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad *keypad,
  123. struct pxa27x_keypad_platform_data *pdata)
  124. {
  125. struct input_dev *input_dev = keypad->input_dev;
  126. struct device *dev = input_dev->dev.parent;
  127. struct device_node *np = dev->of_node;
  128. const __be16 *prop;
  129. unsigned short code;
  130. unsigned int proplen, size;
  131. int i;
  132. int error;
  133. error = of_property_read_u32(np, "marvell,direct-key-count",
  134. &pdata->direct_key_num);
  135. if (error) {
  136. /*
  137. * If do not have marvel,direct-key-count defined,
  138. * it means direct key is not supported.
  139. */
  140. return error == -EINVAL ? 0 : error;
  141. }
  142. error = of_property_read_u32(np, "marvell,direct-key-mask",
  143. &pdata->direct_key_mask);
  144. if (error) {
  145. if (error != -EINVAL)
  146. return error;
  147. /*
  148. * If marvell,direct-key-mask is not defined, driver will use
  149. * default value. Default value is set when configure the keypad.
  150. */
  151. pdata->direct_key_mask = 0;
  152. }
  153. pdata->direct_key_low_active = of_property_read_bool(np,
  154. "marvell,direct-key-low-active");
  155. prop = of_get_property(np, "marvell,direct-key-map", &proplen);
  156. if (!prop)
  157. return -EINVAL;
  158. if (proplen % sizeof(u16))
  159. return -EINVAL;
  160. size = proplen / sizeof(u16);
  161. /* Only MAX_DIRECT_KEY_NUM is accepted.*/
  162. if (size > MAX_DIRECT_KEY_NUM)
  163. return -EINVAL;
  164. for (i = 0; i < size; i++) {
  165. code = be16_to_cpup(prop + i);
  166. keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = code;
  167. __set_bit(code, input_dev->keybit);
  168. }
  169. return 0;
  170. }
  171. static int pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad *keypad,
  172. struct pxa27x_keypad_platform_data *pdata)
  173. {
  174. const __be32 *prop;
  175. int i, relkey_ret;
  176. unsigned int code, proplen;
  177. const char *rotaryname[2] = {
  178. "marvell,rotary0", "marvell,rotary1"};
  179. const char relkeyname[] = {"marvell,rotary-rel-key"};
  180. struct input_dev *input_dev = keypad->input_dev;
  181. struct device *dev = input_dev->dev.parent;
  182. struct device_node *np = dev->of_node;
  183. relkey_ret = of_property_read_u32(np, relkeyname, &code);
  184. /* if can read correct rotary key-code, we do not need this. */
  185. if (relkey_ret == 0) {
  186. unsigned short relcode;
  187. /* rotary0 taks lower half, rotary1 taks upper half. */
  188. relcode = code & 0xffff;
  189. pdata->rotary0_rel_code = (code & 0xffff);
  190. __set_bit(relcode, input_dev->relbit);
  191. relcode = code >> 16;
  192. pdata->rotary1_rel_code = relcode;
  193. __set_bit(relcode, input_dev->relbit);
  194. }
  195. for (i = 0; i < 2; i++) {
  196. prop = of_get_property(np, rotaryname[i], &proplen);
  197. /*
  198. * If the prop is not set, it means keypad does not need
  199. * initialize the rotaryX.
  200. */
  201. if (!prop)
  202. continue;
  203. code = be32_to_cpup(prop);
  204. /*
  205. * Not all up/down key code are valid.
  206. * Now we depends on direct-rel-code.
  207. */
  208. if ((!(code & 0xffff) || !(code >> 16)) && relkey_ret) {
  209. return relkey_ret;
  210. } else {
  211. unsigned int n = MAX_MATRIX_KEY_NUM + (i << 1);
  212. unsigned short keycode;
  213. keycode = code & 0xffff;
  214. keypad->keycodes[n] = keycode;
  215. __set_bit(keycode, input_dev->keybit);
  216. keycode = code >> 16;
  217. keypad->keycodes[n + 1] = keycode;
  218. __set_bit(keycode, input_dev->keybit);
  219. if (i == 0)
  220. pdata->rotary0_rel_code = -1;
  221. else
  222. pdata->rotary1_rel_code = -1;
  223. }
  224. if (i == 0)
  225. pdata->enable_rotary0 = 1;
  226. else
  227. pdata->enable_rotary1 = 1;
  228. }
  229. keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
  230. keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
  231. return 0;
  232. }
  233. static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
  234. {
  235. struct input_dev *input_dev = keypad->input_dev;
  236. struct device *dev = input_dev->dev.parent;
  237. struct device_node *np = dev->of_node;
  238. struct pxa27x_keypad_platform_data *pdata;
  239. int error;
  240. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  241. if (!pdata) {
  242. dev_err(dev, "failed to allocate memory for pdata\n");
  243. return -ENOMEM;
  244. }
  245. error = pxa27x_keypad_matrix_key_parse_dt(keypad, pdata);
  246. if (error) {
  247. dev_err(dev, "failed to parse matrix key\n");
  248. return error;
  249. }
  250. error = pxa27x_keypad_direct_key_parse_dt(keypad, pdata);
  251. if (error) {
  252. dev_err(dev, "failed to parse direct key\n");
  253. return error;
  254. }
  255. error = pxa27x_keypad_rotary_parse_dt(keypad, pdata);
  256. if (error) {
  257. dev_err(dev, "failed to parse rotary key\n");
  258. return error;
  259. }
  260. error = of_property_read_u32(np, "marvell,debounce-interval",
  261. &pdata->debounce_interval);
  262. if (error) {
  263. dev_err(dev, "failed to parse debpunce-interval\n");
  264. return error;
  265. }
  266. /*
  267. * The keycodes may not only includes matrix key but also the direct
  268. * key or rotary key.
  269. */
  270. input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
  271. keypad->pdata = pdata;
  272. return 0;
  273. }
  274. #else
  275. static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
  276. {
  277. dev_info(keypad->input_dev->dev.parent, "missing platform data\n");
  278. return -EINVAL;
  279. }
  280. #endif
  281. static int pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
  282. {
  283. const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  284. struct input_dev *input_dev = keypad->input_dev;
  285. const struct matrix_keymap_data *keymap_data =
  286. pdata ? pdata->matrix_keymap_data : NULL;
  287. unsigned short keycode;
  288. int i;
  289. int error;
  290. error = matrix_keypad_build_keymap(keymap_data, NULL,
  291. pdata->matrix_key_rows,
  292. pdata->matrix_key_cols,
  293. keypad->keycodes, input_dev);
  294. if (error)
  295. return error;
  296. /*
  297. * The keycodes may not only include matrix keys but also the direct
  298. * or rotary keys.
  299. */
  300. input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
  301. /* For direct keys. */
  302. for (i = 0; i < pdata->direct_key_num; i++) {
  303. keycode = pdata->direct_key_map[i];
  304. keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode;
  305. __set_bit(keycode, input_dev->keybit);
  306. }
  307. if (pdata->enable_rotary0) {
  308. if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
  309. keycode = pdata->rotary0_up_key;
  310. keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode;
  311. __set_bit(keycode, input_dev->keybit);
  312. keycode = pdata->rotary0_down_key;
  313. keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode;
  314. __set_bit(keycode, input_dev->keybit);
  315. keypad->rotary_rel_code[0] = -1;
  316. } else {
  317. keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
  318. __set_bit(pdata->rotary0_rel_code, input_dev->relbit);
  319. }
  320. }
  321. if (pdata->enable_rotary1) {
  322. if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
  323. keycode = pdata->rotary1_up_key;
  324. keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode;
  325. __set_bit(keycode, input_dev->keybit);
  326. keycode = pdata->rotary1_down_key;
  327. keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode;
  328. __set_bit(keycode, input_dev->keybit);
  329. keypad->rotary_rel_code[1] = -1;
  330. } else {
  331. keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
  332. __set_bit(pdata->rotary1_rel_code, input_dev->relbit);
  333. }
  334. }
  335. __clear_bit(KEY_RESERVED, input_dev->keybit);
  336. return 0;
  337. }
  338. static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
  339. {
  340. const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  341. struct input_dev *input_dev = keypad->input_dev;
  342. int row, col, num_keys_pressed = 0;
  343. uint32_t new_state[MAX_MATRIX_KEY_COLS];
  344. uint32_t kpas = keypad_readl(KPAS);
  345. num_keys_pressed = KPAS_MUKP(kpas);
  346. memset(new_state, 0, sizeof(new_state));
  347. if (num_keys_pressed == 0)
  348. goto scan;
  349. if (num_keys_pressed == 1) {
  350. col = KPAS_CP(kpas);
  351. row = KPAS_RP(kpas);
  352. /* if invalid row/col, treat as no key pressed */
  353. if (col >= pdata->matrix_key_cols ||
  354. row >= pdata->matrix_key_rows)
  355. goto scan;
  356. new_state[col] = (1 << row);
  357. goto scan;
  358. }
  359. if (num_keys_pressed > 1) {
  360. uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
  361. uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
  362. uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
  363. uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
  364. new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
  365. new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
  366. new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
  367. new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
  368. new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
  369. new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
  370. new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
  371. new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
  372. }
  373. scan:
  374. for (col = 0; col < pdata->matrix_key_cols; col++) {
  375. uint32_t bits_changed;
  376. int code;
  377. bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
  378. if (bits_changed == 0)
  379. continue;
  380. for (row = 0; row < pdata->matrix_key_rows; row++) {
  381. if ((bits_changed & (1 << row)) == 0)
  382. continue;
  383. code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
  384. input_event(input_dev, EV_MSC, MSC_SCAN, code);
  385. input_report_key(input_dev, keypad->keycodes[code],
  386. new_state[col] & (1 << row));
  387. }
  388. }
  389. input_sync(input_dev);
  390. memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
  391. }
  392. #define DEFAULT_KPREC (0x007f007f)
  393. static inline int rotary_delta(uint32_t kprec)
  394. {
  395. if (kprec & KPREC_OF0)
  396. return (kprec & 0xff) + 0x7f;
  397. else if (kprec & KPREC_UF0)
  398. return (kprec & 0xff) - 0x7f - 0xff;
  399. else
  400. return (kprec & 0xff) - 0x7f;
  401. }
  402. static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
  403. {
  404. struct input_dev *dev = keypad->input_dev;
  405. if (delta == 0)
  406. return;
  407. if (keypad->rotary_rel_code[r] == -1) {
  408. int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1);
  409. unsigned char keycode = keypad->keycodes[code];
  410. /* simulate a press-n-release */
  411. input_event(dev, EV_MSC, MSC_SCAN, code);
  412. input_report_key(dev, keycode, 1);
  413. input_sync(dev);
  414. input_event(dev, EV_MSC, MSC_SCAN, code);
  415. input_report_key(dev, keycode, 0);
  416. input_sync(dev);
  417. } else {
  418. input_report_rel(dev, keypad->rotary_rel_code[r], delta);
  419. input_sync(dev);
  420. }
  421. }
  422. static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
  423. {
  424. const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  425. uint32_t kprec;
  426. /* read and reset to default count value */
  427. kprec = keypad_readl(KPREC);
  428. keypad_writel(KPREC, DEFAULT_KPREC);
  429. if (pdata->enable_rotary0)
  430. report_rotary_event(keypad, 0, rotary_delta(kprec));
  431. if (pdata->enable_rotary1)
  432. report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
  433. }
  434. static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
  435. {
  436. const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  437. struct input_dev *input_dev = keypad->input_dev;
  438. unsigned int new_state;
  439. uint32_t kpdk, bits_changed;
  440. int i;
  441. kpdk = keypad_readl(KPDK);
  442. if (pdata->enable_rotary0 || pdata->enable_rotary1)
  443. pxa27x_keypad_scan_rotary(keypad);
  444. /*
  445. * The KPDR_DK only output the key pin level, so it relates to board,
  446. * and low level may be active.
  447. */
  448. if (pdata->direct_key_low_active)
  449. new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask;
  450. else
  451. new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
  452. bits_changed = keypad->direct_key_state ^ new_state;
  453. if (bits_changed == 0)
  454. return;
  455. for (i = 0; i < pdata->direct_key_num; i++) {
  456. if (bits_changed & (1 << i)) {
  457. int code = MAX_MATRIX_KEY_NUM + i;
  458. input_event(input_dev, EV_MSC, MSC_SCAN, code);
  459. input_report_key(input_dev, keypad->keycodes[code],
  460. new_state & (1 << i));
  461. }
  462. }
  463. input_sync(input_dev);
  464. keypad->direct_key_state = new_state;
  465. }
  466. static void clear_wakeup_event(struct pxa27x_keypad *keypad)
  467. {
  468. const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  469. if (pdata->clear_wakeup_event)
  470. (pdata->clear_wakeup_event)();
  471. }
  472. static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
  473. {
  474. struct pxa27x_keypad *keypad = dev_id;
  475. unsigned long kpc = keypad_readl(KPC);
  476. clear_wakeup_event(keypad);
  477. if (kpc & KPC_DI)
  478. pxa27x_keypad_scan_direct(keypad);
  479. if (kpc & KPC_MI)
  480. pxa27x_keypad_scan_matrix(keypad);
  481. return IRQ_HANDLED;
  482. }
  483. static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
  484. {
  485. const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  486. unsigned int mask = 0, direct_key_num = 0;
  487. unsigned long kpc = 0;
  488. /* clear pending interrupt bit */
  489. keypad_readl(KPC);
  490. /* enable matrix keys with automatic scan */
  491. if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
  492. kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
  493. kpc |= KPC_MKRN(pdata->matrix_key_rows) |
  494. KPC_MKCN(pdata->matrix_key_cols);
  495. }
  496. /* enable rotary key, debounce interval same as direct keys */
  497. if (pdata->enable_rotary0) {
  498. mask |= 0x03;
  499. direct_key_num = 2;
  500. kpc |= KPC_REE0;
  501. }
  502. if (pdata->enable_rotary1) {
  503. mask |= 0x0c;
  504. direct_key_num = 4;
  505. kpc |= KPC_REE1;
  506. }
  507. if (pdata->direct_key_num > direct_key_num)
  508. direct_key_num = pdata->direct_key_num;
  509. /*
  510. * Direct keys usage may not start from KP_DKIN0, check the platfrom
  511. * mask data to config the specific.
  512. */
  513. if (pdata->direct_key_mask)
  514. keypad->direct_key_mask = pdata->direct_key_mask;
  515. else
  516. keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask;
  517. /* enable direct key */
  518. if (direct_key_num)
  519. kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
  520. keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
  521. keypad_writel(KPREC, DEFAULT_KPREC);
  522. keypad_writel(KPKDI, pdata->debounce_interval);
  523. }
  524. static int pxa27x_keypad_open(struct input_dev *dev)
  525. {
  526. struct pxa27x_keypad *keypad = input_get_drvdata(dev);
  527. /* Enable unit clock */
  528. clk_prepare_enable(keypad->clk);
  529. pxa27x_keypad_config(keypad);
  530. return 0;
  531. }
  532. static void pxa27x_keypad_close(struct input_dev *dev)
  533. {
  534. struct pxa27x_keypad *keypad = input_get_drvdata(dev);
  535. /* Disable clock unit */
  536. clk_disable_unprepare(keypad->clk);
  537. }
  538. #ifdef CONFIG_PM_SLEEP
  539. static int pxa27x_keypad_suspend(struct device *dev)
  540. {
  541. struct platform_device *pdev = to_platform_device(dev);
  542. struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
  543. /*
  544. * If the keypad is used a wake up source, clock can not be disabled.
  545. * Or it can not detect the key pressing.
  546. */
  547. if (device_may_wakeup(&pdev->dev))
  548. enable_irq_wake(keypad->irq);
  549. else
  550. clk_disable_unprepare(keypad->clk);
  551. return 0;
  552. }
  553. static int pxa27x_keypad_resume(struct device *dev)
  554. {
  555. struct platform_device *pdev = to_platform_device(dev);
  556. struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
  557. struct input_dev *input_dev = keypad->input_dev;
  558. /*
  559. * If the keypad is used as wake up source, the clock is not turned
  560. * off. So do not need configure it again.
  561. */
  562. if (device_may_wakeup(&pdev->dev)) {
  563. disable_irq_wake(keypad->irq);
  564. } else {
  565. mutex_lock(&input_dev->mutex);
  566. if (input_dev->users) {
  567. /* Enable unit clock */
  568. clk_prepare_enable(keypad->clk);
  569. pxa27x_keypad_config(keypad);
  570. }
  571. mutex_unlock(&input_dev->mutex);
  572. }
  573. return 0;
  574. }
  575. #endif
  576. static SIMPLE_DEV_PM_OPS(pxa27x_keypad_pm_ops,
  577. pxa27x_keypad_suspend, pxa27x_keypad_resume);
  578. static int pxa27x_keypad_probe(struct platform_device *pdev)
  579. {
  580. const struct pxa27x_keypad_platform_data *pdata =
  581. dev_get_platdata(&pdev->dev);
  582. struct device_node *np = pdev->dev.of_node;
  583. struct pxa27x_keypad *keypad;
  584. struct input_dev *input_dev;
  585. struct resource *res;
  586. int irq, error;
  587. /* Driver need build keycode from device tree or pdata */
  588. if (!np && !pdata)
  589. return -EINVAL;
  590. irq = platform_get_irq(pdev, 0);
  591. if (irq < 0) {
  592. dev_err(&pdev->dev, "failed to get keypad irq\n");
  593. return -ENXIO;
  594. }
  595. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  596. if (res == NULL) {
  597. dev_err(&pdev->dev, "failed to get I/O memory\n");
  598. return -ENXIO;
  599. }
  600. keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
  601. input_dev = input_allocate_device();
  602. if (!keypad || !input_dev) {
  603. dev_err(&pdev->dev, "failed to allocate memory\n");
  604. error = -ENOMEM;
  605. goto failed_free;
  606. }
  607. keypad->pdata = pdata;
  608. keypad->input_dev = input_dev;
  609. keypad->irq = irq;
  610. res = request_mem_region(res->start, resource_size(res), pdev->name);
  611. if (res == NULL) {
  612. dev_err(&pdev->dev, "failed to request I/O memory\n");
  613. error = -EBUSY;
  614. goto failed_free;
  615. }
  616. keypad->mmio_base = ioremap(res->start, resource_size(res));
  617. if (keypad->mmio_base == NULL) {
  618. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  619. error = -ENXIO;
  620. goto failed_free_mem;
  621. }
  622. keypad->clk = clk_get(&pdev->dev, NULL);
  623. if (IS_ERR(keypad->clk)) {
  624. dev_err(&pdev->dev, "failed to get keypad clock\n");
  625. error = PTR_ERR(keypad->clk);
  626. goto failed_free_io;
  627. }
  628. input_dev->name = pdev->name;
  629. input_dev->id.bustype = BUS_HOST;
  630. input_dev->open = pxa27x_keypad_open;
  631. input_dev->close = pxa27x_keypad_close;
  632. input_dev->dev.parent = &pdev->dev;
  633. input_dev->keycode = keypad->keycodes;
  634. input_dev->keycodesize = sizeof(keypad->keycodes[0]);
  635. input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
  636. input_set_drvdata(input_dev, keypad);
  637. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  638. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  639. if (pdata)
  640. error = pxa27x_keypad_build_keycode(keypad);
  641. else
  642. error = pxa27x_keypad_build_keycode_from_dt(keypad);
  643. if (error) {
  644. dev_err(&pdev->dev, "failed to build keycode\n");
  645. goto failed_put_clk;
  646. }
  647. if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) ||
  648. (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) {
  649. input_dev->evbit[0] |= BIT_MASK(EV_REL);
  650. }
  651. error = request_irq(irq, pxa27x_keypad_irq_handler, 0,
  652. pdev->name, keypad);
  653. if (error) {
  654. dev_err(&pdev->dev, "failed to request IRQ\n");
  655. goto failed_put_clk;
  656. }
  657. /* Register the input device */
  658. error = input_register_device(input_dev);
  659. if (error) {
  660. dev_err(&pdev->dev, "failed to register input device\n");
  661. goto failed_free_irq;
  662. }
  663. platform_set_drvdata(pdev, keypad);
  664. device_init_wakeup(&pdev->dev, 1);
  665. return 0;
  666. failed_free_irq:
  667. free_irq(irq, keypad);
  668. failed_put_clk:
  669. clk_put(keypad->clk);
  670. failed_free_io:
  671. iounmap(keypad->mmio_base);
  672. failed_free_mem:
  673. release_mem_region(res->start, resource_size(res));
  674. failed_free:
  675. input_free_device(input_dev);
  676. kfree(keypad);
  677. return error;
  678. }
  679. static int pxa27x_keypad_remove(struct platform_device *pdev)
  680. {
  681. struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
  682. struct resource *res;
  683. free_irq(keypad->irq, keypad);
  684. clk_put(keypad->clk);
  685. input_unregister_device(keypad->input_dev);
  686. iounmap(keypad->mmio_base);
  687. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  688. release_mem_region(res->start, resource_size(res));
  689. kfree(keypad);
  690. return 0;
  691. }
  692. /* work with hotplug and coldplug */
  693. MODULE_ALIAS("platform:pxa27x-keypad");
  694. #ifdef CONFIG_OF
  695. static const struct of_device_id pxa27x_keypad_dt_match[] = {
  696. { .compatible = "marvell,pxa27x-keypad" },
  697. {},
  698. };
  699. MODULE_DEVICE_TABLE(of, pxa27x_keypad_dt_match);
  700. #endif
  701. static struct platform_driver pxa27x_keypad_driver = {
  702. .probe = pxa27x_keypad_probe,
  703. .remove = pxa27x_keypad_remove,
  704. .driver = {
  705. .name = "pxa27x-keypad",
  706. .of_match_table = of_match_ptr(pxa27x_keypad_dt_match),
  707. .owner = THIS_MODULE,
  708. .pm = &pxa27x_keypad_pm_ops,
  709. },
  710. };
  711. module_platform_driver(pxa27x_keypad_driver);
  712. MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
  713. MODULE_LICENSE("GPL");