pxa27x_keypad.c 23 KB

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