iqs62x-keys.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Azoteq IQS620A/621/622/624/625 Keys and Switches
  4. *
  5. * Copyright (C) 2019 Jeff LaBundy <jeff@labundy.com>
  6. */
  7. #include <linux/device.h>
  8. #include <linux/input.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mfd/iqs62x.h>
  11. #include <linux/module.h>
  12. #include <linux/notifier.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/regmap.h>
  16. #include <linux/slab.h>
  17. enum {
  18. IQS62X_SW_HALL_N,
  19. IQS62X_SW_HALL_S,
  20. };
  21. static const char * const iqs62x_switch_names[] = {
  22. [IQS62X_SW_HALL_N] = "hall-switch-north",
  23. [IQS62X_SW_HALL_S] = "hall-switch-south",
  24. };
  25. struct iqs62x_switch_desc {
  26. enum iqs62x_event_flag flag;
  27. unsigned int code;
  28. bool enabled;
  29. };
  30. struct iqs62x_keys_private {
  31. struct iqs62x_core *iqs62x;
  32. struct input_dev *input;
  33. struct notifier_block notifier;
  34. struct iqs62x_switch_desc switches[ARRAY_SIZE(iqs62x_switch_names)];
  35. unsigned int keycode[IQS62X_NUM_KEYS];
  36. unsigned int keycodemax;
  37. u8 interval;
  38. };
  39. static int iqs62x_keys_parse_prop(struct platform_device *pdev,
  40. struct iqs62x_keys_private *iqs62x_keys)
  41. {
  42. struct fwnode_handle *child;
  43. unsigned int val;
  44. int ret, i;
  45. ret = device_property_count_u32(&pdev->dev, "linux,keycodes");
  46. if (ret > IQS62X_NUM_KEYS) {
  47. dev_err(&pdev->dev, "Too many keycodes present\n");
  48. return -EINVAL;
  49. } else if (ret < 0) {
  50. dev_err(&pdev->dev, "Failed to count keycodes: %d\n", ret);
  51. return ret;
  52. }
  53. iqs62x_keys->keycodemax = ret;
  54. ret = device_property_read_u32_array(&pdev->dev, "linux,keycodes",
  55. iqs62x_keys->keycode,
  56. iqs62x_keys->keycodemax);
  57. if (ret) {
  58. dev_err(&pdev->dev, "Failed to read keycodes: %d\n", ret);
  59. return ret;
  60. }
  61. for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++) {
  62. child = device_get_named_child_node(&pdev->dev,
  63. iqs62x_switch_names[i]);
  64. if (!child)
  65. continue;
  66. ret = fwnode_property_read_u32(child, "linux,code", &val);
  67. if (ret) {
  68. dev_err(&pdev->dev, "Failed to read switch code: %d\n",
  69. ret);
  70. return ret;
  71. }
  72. iqs62x_keys->switches[i].code = val;
  73. iqs62x_keys->switches[i].enabled = true;
  74. if (fwnode_property_present(child, "azoteq,use-prox"))
  75. iqs62x_keys->switches[i].flag = (i == IQS62X_SW_HALL_N ?
  76. IQS62X_EVENT_HALL_N_P :
  77. IQS62X_EVENT_HALL_S_P);
  78. else
  79. iqs62x_keys->switches[i].flag = (i == IQS62X_SW_HALL_N ?
  80. IQS62X_EVENT_HALL_N_T :
  81. IQS62X_EVENT_HALL_S_T);
  82. }
  83. return 0;
  84. }
  85. static int iqs62x_keys_init(struct iqs62x_keys_private *iqs62x_keys)
  86. {
  87. struct iqs62x_core *iqs62x = iqs62x_keys->iqs62x;
  88. enum iqs62x_event_flag flag;
  89. unsigned int event_reg, val;
  90. unsigned int event_mask = 0;
  91. int ret, i;
  92. switch (iqs62x->dev_desc->prod_num) {
  93. case IQS620_PROD_NUM:
  94. case IQS621_PROD_NUM:
  95. case IQS622_PROD_NUM:
  96. event_reg = IQS620_GLBL_EVENT_MASK;
  97. /*
  98. * Discreet button, hysteresis and SAR UI flags represent keys
  99. * and are unmasked if mapped to a valid keycode.
  100. */
  101. for (i = 0; i < iqs62x_keys->keycodemax; i++) {
  102. if (iqs62x_keys->keycode[i] == KEY_RESERVED)
  103. continue;
  104. if (iqs62x_events[i].reg == IQS62X_EVENT_PROX)
  105. event_mask |= iqs62x->dev_desc->prox_mask;
  106. else if (iqs62x_events[i].reg == IQS62X_EVENT_HYST)
  107. event_mask |= (iqs62x->dev_desc->hyst_mask |
  108. iqs62x->dev_desc->sar_mask);
  109. }
  110. ret = regmap_read(iqs62x->regmap, iqs62x->dev_desc->hall_flags,
  111. &val);
  112. if (ret)
  113. return ret;
  114. /*
  115. * Hall UI flags represent switches and are unmasked if their
  116. * corresponding child nodes are present.
  117. */
  118. for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++) {
  119. if (!(iqs62x_keys->switches[i].enabled))
  120. continue;
  121. flag = iqs62x_keys->switches[i].flag;
  122. if (iqs62x_events[flag].reg != IQS62X_EVENT_HALL)
  123. continue;
  124. event_mask |= iqs62x->dev_desc->hall_mask;
  125. input_report_switch(iqs62x_keys->input,
  126. iqs62x_keys->switches[i].code,
  127. (val & iqs62x_events[flag].mask) ==
  128. iqs62x_events[flag].val);
  129. }
  130. input_sync(iqs62x_keys->input);
  131. break;
  132. case IQS624_PROD_NUM:
  133. event_reg = IQS624_HALL_UI;
  134. /*
  135. * Interval change events represent keys and are unmasked if
  136. * either wheel movement flag is mapped to a valid keycode.
  137. */
  138. if (iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_UP] != KEY_RESERVED)
  139. event_mask |= IQS624_HALL_UI_INT_EVENT;
  140. if (iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_DN] != KEY_RESERVED)
  141. event_mask |= IQS624_HALL_UI_INT_EVENT;
  142. ret = regmap_read(iqs62x->regmap, iqs62x->dev_desc->interval,
  143. &val);
  144. if (ret)
  145. return ret;
  146. iqs62x_keys->interval = val;
  147. break;
  148. default:
  149. return 0;
  150. }
  151. return regmap_update_bits(iqs62x->regmap, event_reg, event_mask, 0);
  152. }
  153. static int iqs62x_keys_notifier(struct notifier_block *notifier,
  154. unsigned long event_flags, void *context)
  155. {
  156. struct iqs62x_event_data *event_data = context;
  157. struct iqs62x_keys_private *iqs62x_keys;
  158. int ret, i;
  159. iqs62x_keys = container_of(notifier, struct iqs62x_keys_private,
  160. notifier);
  161. if (event_flags & BIT(IQS62X_EVENT_SYS_RESET)) {
  162. ret = iqs62x_keys_init(iqs62x_keys);
  163. if (ret) {
  164. dev_err(iqs62x_keys->input->dev.parent,
  165. "Failed to re-initialize device: %d\n", ret);
  166. return NOTIFY_BAD;
  167. }
  168. return NOTIFY_OK;
  169. }
  170. for (i = 0; i < iqs62x_keys->keycodemax; i++) {
  171. if (iqs62x_events[i].reg == IQS62X_EVENT_WHEEL &&
  172. event_data->interval == iqs62x_keys->interval)
  173. continue;
  174. input_report_key(iqs62x_keys->input, iqs62x_keys->keycode[i],
  175. event_flags & BIT(i));
  176. }
  177. for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++)
  178. if (iqs62x_keys->switches[i].enabled)
  179. input_report_switch(iqs62x_keys->input,
  180. iqs62x_keys->switches[i].code,
  181. event_flags &
  182. BIT(iqs62x_keys->switches[i].flag));
  183. input_sync(iqs62x_keys->input);
  184. if (event_data->interval == iqs62x_keys->interval)
  185. return NOTIFY_OK;
  186. /*
  187. * Each frame contains at most one wheel event (up or down), in which
  188. * case a complementary release cycle is emulated.
  189. */
  190. if (event_flags & BIT(IQS62X_EVENT_WHEEL_UP)) {
  191. input_report_key(iqs62x_keys->input,
  192. iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_UP],
  193. 0);
  194. input_sync(iqs62x_keys->input);
  195. } else if (event_flags & BIT(IQS62X_EVENT_WHEEL_DN)) {
  196. input_report_key(iqs62x_keys->input,
  197. iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_DN],
  198. 0);
  199. input_sync(iqs62x_keys->input);
  200. }
  201. iqs62x_keys->interval = event_data->interval;
  202. return NOTIFY_OK;
  203. }
  204. static int iqs62x_keys_probe(struct platform_device *pdev)
  205. {
  206. struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
  207. struct iqs62x_keys_private *iqs62x_keys;
  208. struct input_dev *input;
  209. int ret, i;
  210. iqs62x_keys = devm_kzalloc(&pdev->dev, sizeof(*iqs62x_keys),
  211. GFP_KERNEL);
  212. if (!iqs62x_keys)
  213. return -ENOMEM;
  214. platform_set_drvdata(pdev, iqs62x_keys);
  215. ret = iqs62x_keys_parse_prop(pdev, iqs62x_keys);
  216. if (ret)
  217. return ret;
  218. input = devm_input_allocate_device(&pdev->dev);
  219. if (!input)
  220. return -ENOMEM;
  221. input->keycodemax = iqs62x_keys->keycodemax;
  222. input->keycode = iqs62x_keys->keycode;
  223. input->keycodesize = sizeof(*iqs62x_keys->keycode);
  224. input->name = iqs62x->dev_desc->dev_name;
  225. input->id.bustype = BUS_I2C;
  226. for (i = 0; i < iqs62x_keys->keycodemax; i++)
  227. if (iqs62x_keys->keycode[i] != KEY_RESERVED)
  228. input_set_capability(input, EV_KEY,
  229. iqs62x_keys->keycode[i]);
  230. for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++)
  231. if (iqs62x_keys->switches[i].enabled)
  232. input_set_capability(input, EV_SW,
  233. iqs62x_keys->switches[i].code);
  234. iqs62x_keys->iqs62x = iqs62x;
  235. iqs62x_keys->input = input;
  236. ret = iqs62x_keys_init(iqs62x_keys);
  237. if (ret) {
  238. dev_err(&pdev->dev, "Failed to initialize device: %d\n", ret);
  239. return ret;
  240. }
  241. ret = input_register_device(iqs62x_keys->input);
  242. if (ret) {
  243. dev_err(&pdev->dev, "Failed to register device: %d\n", ret);
  244. return ret;
  245. }
  246. iqs62x_keys->notifier.notifier_call = iqs62x_keys_notifier;
  247. ret = blocking_notifier_chain_register(&iqs62x_keys->iqs62x->nh,
  248. &iqs62x_keys->notifier);
  249. if (ret)
  250. dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
  251. return ret;
  252. }
  253. static int iqs62x_keys_remove(struct platform_device *pdev)
  254. {
  255. struct iqs62x_keys_private *iqs62x_keys = platform_get_drvdata(pdev);
  256. int ret;
  257. ret = blocking_notifier_chain_unregister(&iqs62x_keys->iqs62x->nh,
  258. &iqs62x_keys->notifier);
  259. if (ret)
  260. dev_err(&pdev->dev, "Failed to unregister notifier: %d\n", ret);
  261. return ret;
  262. }
  263. static struct platform_driver iqs62x_keys_platform_driver = {
  264. .driver = {
  265. .name = "iqs62x-keys",
  266. },
  267. .probe = iqs62x_keys_probe,
  268. .remove = iqs62x_keys_remove,
  269. };
  270. module_platform_driver(iqs62x_keys_platform_driver);
  271. MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
  272. MODULE_DESCRIPTION("Azoteq IQS620A/621/622/624/625 Keys and Switches");
  273. MODULE_LICENSE("GPL");
  274. MODULE_ALIAS("platform:iqs62x-keys");