pxa27x_keypad.c 22 KB

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