pxa27x_keypad.c 22 KB

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