clps711x-keypad.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Cirrus Logic CLPS711X Keypad driver
  3. *
  4. * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/input.h>
  12. #include <linux/input-polldev.h>
  13. #include <linux/module.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/sched.h>
  18. #include <linux/input/matrix_keypad.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/mfd/syscon/clps711x.h>
  21. #define CLPS711X_KEYPAD_COL_COUNT 8
  22. struct clps711x_gpio_data {
  23. struct gpio_desc *desc;
  24. DECLARE_BITMAP(last_state, CLPS711X_KEYPAD_COL_COUNT);
  25. };
  26. struct clps711x_keypad_data {
  27. struct regmap *syscon;
  28. int row_count;
  29. unsigned int row_shift;
  30. struct clps711x_gpio_data *gpio_data;
  31. };
  32. static void clps711x_keypad_poll(struct input_polled_dev *dev)
  33. {
  34. const unsigned short *keycodes = dev->input->keycode;
  35. struct clps711x_keypad_data *priv = dev->private;
  36. bool sync = false;
  37. int col, row;
  38. for (col = 0; col < CLPS711X_KEYPAD_COL_COUNT; col++) {
  39. /* Assert column */
  40. regmap_update_bits(priv->syscon, SYSCON_OFFSET,
  41. SYSCON1_KBDSCAN_MASK,
  42. SYSCON1_KBDSCAN(8 + col));
  43. /* Scan rows */
  44. for (row = 0; row < priv->row_count; row++) {
  45. struct clps711x_gpio_data *data = &priv->gpio_data[row];
  46. bool state, state1;
  47. /* Read twice for protection against fluctuations */
  48. do {
  49. state = gpiod_get_value_cansleep(data->desc);
  50. cond_resched();
  51. state1 = gpiod_get_value_cansleep(data->desc);
  52. } while (state != state1);
  53. if (test_bit(col, data->last_state) != state) {
  54. int code = MATRIX_SCAN_CODE(row, col,
  55. priv->row_shift);
  56. if (state) {
  57. set_bit(col, data->last_state);
  58. input_event(dev->input, EV_MSC,
  59. MSC_SCAN, code);
  60. } else {
  61. clear_bit(col, data->last_state);
  62. }
  63. if (keycodes[code])
  64. input_report_key(dev->input,
  65. keycodes[code], state);
  66. sync = true;
  67. }
  68. }
  69. /* Set all columns to low */
  70. regmap_update_bits(priv->syscon, SYSCON_OFFSET,
  71. SYSCON1_KBDSCAN_MASK, SYSCON1_KBDSCAN(1));
  72. }
  73. if (sync)
  74. input_sync(dev->input);
  75. }
  76. static int clps711x_keypad_probe(struct platform_device *pdev)
  77. {
  78. struct clps711x_keypad_data *priv;
  79. struct device *dev = &pdev->dev;
  80. struct device_node *np = dev->of_node;
  81. struct input_polled_dev *poll_dev;
  82. u32 poll_interval;
  83. int i, err;
  84. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  85. if (!priv)
  86. return -ENOMEM;
  87. priv->syscon =
  88. syscon_regmap_lookup_by_compatible("cirrus,clps711x-syscon1");
  89. if (IS_ERR(priv->syscon))
  90. return PTR_ERR(priv->syscon);
  91. priv->row_count = of_gpio_named_count(np, "row-gpios");
  92. if (priv->row_count < 1)
  93. return -EINVAL;
  94. priv->gpio_data = devm_kzalloc(dev,
  95. sizeof(*priv->gpio_data) * priv->row_count,
  96. GFP_KERNEL);
  97. if (!priv->gpio_data)
  98. return -ENOMEM;
  99. priv->row_shift = get_count_order(CLPS711X_KEYPAD_COL_COUNT);
  100. for (i = 0; i < priv->row_count; i++) {
  101. struct clps711x_gpio_data *data = &priv->gpio_data[i];
  102. data->desc = devm_gpiod_get_index(dev, "row", i, GPIOD_IN);
  103. if (IS_ERR(data->desc))
  104. return PTR_ERR(data->desc);
  105. }
  106. err = of_property_read_u32(np, "poll-interval", &poll_interval);
  107. if (err)
  108. return err;
  109. poll_dev = input_allocate_polled_device();
  110. if (!poll_dev)
  111. return -ENOMEM;
  112. poll_dev->private = priv;
  113. poll_dev->poll = clps711x_keypad_poll;
  114. poll_dev->poll_interval = poll_interval;
  115. poll_dev->input->name = pdev->name;
  116. poll_dev->input->dev.parent = dev;
  117. poll_dev->input->id.bustype = BUS_HOST;
  118. poll_dev->input->id.vendor = 0x0001;
  119. poll_dev->input->id.product = 0x0001;
  120. poll_dev->input->id.version = 0x0100;
  121. err = matrix_keypad_build_keymap(NULL, NULL, priv->row_count,
  122. CLPS711X_KEYPAD_COL_COUNT,
  123. NULL, poll_dev->input);
  124. if (err)
  125. goto out_err;
  126. input_set_capability(poll_dev->input, EV_MSC, MSC_SCAN);
  127. if (of_property_read_bool(np, "autorepeat"))
  128. __set_bit(EV_REP, poll_dev->input->evbit);
  129. platform_set_drvdata(pdev, poll_dev);
  130. /* Set all columns to low */
  131. regmap_update_bits(priv->syscon, SYSCON_OFFSET, SYSCON1_KBDSCAN_MASK,
  132. SYSCON1_KBDSCAN(1));
  133. err = input_register_polled_device(poll_dev);
  134. if (err)
  135. goto out_err;
  136. return 0;
  137. out_err:
  138. input_free_polled_device(poll_dev);
  139. return err;
  140. }
  141. static int clps711x_keypad_remove(struct platform_device *pdev)
  142. {
  143. struct input_polled_dev *poll_dev = platform_get_drvdata(pdev);
  144. input_unregister_polled_device(poll_dev);
  145. input_free_polled_device(poll_dev);
  146. return 0;
  147. }
  148. static const struct of_device_id clps711x_keypad_of_match[] = {
  149. { .compatible = "cirrus,clps711x-keypad", },
  150. { }
  151. };
  152. MODULE_DEVICE_TABLE(of, clps711x_keypad_of_match);
  153. static struct platform_driver clps711x_keypad_driver = {
  154. .driver = {
  155. .name = "clps711x-keypad",
  156. .of_match_table = clps711x_keypad_of_match,
  157. },
  158. .probe = clps711x_keypad_probe,
  159. .remove = clps711x_keypad_remove,
  160. };
  161. module_platform_driver(clps711x_keypad_driver);
  162. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  163. MODULE_DESCRIPTION("Cirrus Logic CLPS711X Keypad driver");
  164. MODULE_LICENSE("GPL");