max7360-rotary.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2025 Bootlin
  4. *
  5. * Author: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/device/devres.h>
  9. #include <linux/dev_printk.h>
  10. #include <linux/init.h>
  11. #include <linux/input.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/mfd/max7360.h>
  14. #include <linux/property.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_wakeirq.h>
  17. #include <linux/regmap.h>
  18. #include <linux/types.h>
  19. #define MAX7360_ROTARY_DEFAULT_STEPS 24
  20. struct max7360_rotary {
  21. struct input_dev *input;
  22. struct regmap *regmap;
  23. unsigned int debounce_ms;
  24. unsigned int pos;
  25. u32 steps;
  26. u32 axis;
  27. bool relative_axis;
  28. bool rollover;
  29. };
  30. static void max7360_rotary_report_event(struct max7360_rotary *max7360_rotary, int steps)
  31. {
  32. if (max7360_rotary->relative_axis) {
  33. input_report_rel(max7360_rotary->input, max7360_rotary->axis, steps);
  34. } else {
  35. int pos = max7360_rotary->pos;
  36. int maxval = max7360_rotary->steps;
  37. /*
  38. * Add steps to the position.
  39. * Make sure added steps are always in ]-maxval; maxval[
  40. * interval, so (pos + maxval) is always >= 0.
  41. * Then set back pos to the [0; maxval[ interval.
  42. */
  43. pos += steps % maxval;
  44. if (max7360_rotary->rollover)
  45. pos = (pos + maxval) % maxval;
  46. else
  47. pos = clamp(pos, 0, maxval - 1);
  48. max7360_rotary->pos = pos;
  49. input_report_abs(max7360_rotary->input, max7360_rotary->axis, max7360_rotary->pos);
  50. }
  51. input_sync(max7360_rotary->input);
  52. }
  53. static irqreturn_t max7360_rotary_irq(int irq, void *data)
  54. {
  55. struct max7360_rotary *max7360_rotary = data;
  56. struct device *dev = max7360_rotary->input->dev.parent;
  57. unsigned int val;
  58. int error;
  59. error = regmap_read(max7360_rotary->regmap, MAX7360_REG_RTR_CNT, &val);
  60. if (error < 0) {
  61. dev_err(dev, "Failed to read rotary counter\n");
  62. return IRQ_NONE;
  63. }
  64. if (val == 0)
  65. return IRQ_NONE;
  66. max7360_rotary_report_event(max7360_rotary, sign_extend32(val, 7));
  67. return IRQ_HANDLED;
  68. }
  69. static int max7360_rotary_hw_init(struct max7360_rotary *max7360_rotary)
  70. {
  71. struct device *dev = max7360_rotary->input->dev.parent;
  72. int val;
  73. int error;
  74. val = FIELD_PREP(MAX7360_ROT_DEBOUNCE, max7360_rotary->debounce_ms) |
  75. FIELD_PREP(MAX7360_ROT_INTCNT, 1) | MAX7360_ROT_INTCNT_DLY;
  76. error = regmap_write(max7360_rotary->regmap, MAX7360_REG_RTRCFG, val);
  77. if (error)
  78. dev_err(dev, "Failed to set max7360 rotary encoder configuration\n");
  79. return error;
  80. }
  81. static int max7360_rotary_probe(struct platform_device *pdev)
  82. {
  83. struct max7360_rotary *max7360_rotary;
  84. struct device *dev = &pdev->dev;
  85. struct input_dev *input;
  86. struct regmap *regmap;
  87. int irq;
  88. int error;
  89. regmap = dev_get_regmap(dev->parent, NULL);
  90. if (!regmap)
  91. return dev_err_probe(dev, -ENODEV, "Could not get parent regmap\n");
  92. irq = fwnode_irq_get_byname(dev_fwnode(dev->parent), "inti");
  93. if (irq < 0)
  94. return dev_err_probe(dev, irq, "Failed to get IRQ\n");
  95. max7360_rotary = devm_kzalloc(dev, sizeof(*max7360_rotary), GFP_KERNEL);
  96. if (!max7360_rotary)
  97. return -ENOMEM;
  98. max7360_rotary->regmap = regmap;
  99. device_property_read_u32(dev->parent, "linux,axis", &max7360_rotary->axis);
  100. max7360_rotary->rollover = device_property_read_bool(dev->parent,
  101. "rotary-encoder,rollover");
  102. max7360_rotary->relative_axis =
  103. device_property_read_bool(dev->parent, "rotary-encoder,relative-axis");
  104. error = device_property_read_u32(dev->parent, "rotary-encoder,steps",
  105. &max7360_rotary->steps);
  106. if (error)
  107. max7360_rotary->steps = MAX7360_ROTARY_DEFAULT_STEPS;
  108. device_property_read_u32(dev->parent, "rotary-debounce-delay-ms",
  109. &max7360_rotary->debounce_ms);
  110. if (max7360_rotary->debounce_ms > MAX7360_ROT_DEBOUNCE_MAX)
  111. return dev_err_probe(dev, -EINVAL, "Invalid debounce timing: %u\n",
  112. max7360_rotary->debounce_ms);
  113. input = devm_input_allocate_device(dev);
  114. if (!input)
  115. return -ENOMEM;
  116. max7360_rotary->input = input;
  117. input->id.bustype = BUS_I2C;
  118. input->name = pdev->name;
  119. if (max7360_rotary->relative_axis)
  120. input_set_capability(input, EV_REL, max7360_rotary->axis);
  121. else
  122. input_set_abs_params(input, max7360_rotary->axis, 0, max7360_rotary->steps, 0, 1);
  123. error = devm_request_threaded_irq(dev, irq, NULL, max7360_rotary_irq,
  124. IRQF_ONESHOT | IRQF_SHARED,
  125. "max7360-rotary", max7360_rotary);
  126. if (error)
  127. return dev_err_probe(dev, error, "Failed to register interrupt\n");
  128. error = input_register_device(input);
  129. if (error)
  130. return dev_err_probe(dev, error, "Could not register input device\n");
  131. error = max7360_rotary_hw_init(max7360_rotary);
  132. if (error)
  133. return dev_err_probe(dev, error, "Failed to initialize max7360 rotary\n");
  134. device_init_wakeup(dev, true);
  135. error = dev_pm_set_wake_irq(dev, irq);
  136. if (error)
  137. dev_warn(dev, "Failed to set up wakeup irq: %d\n", error);
  138. return 0;
  139. }
  140. static void max7360_rotary_remove(struct platform_device *pdev)
  141. {
  142. dev_pm_clear_wake_irq(&pdev->dev);
  143. device_init_wakeup(&pdev->dev, false);
  144. }
  145. static struct platform_driver max7360_rotary_driver = {
  146. .driver = {
  147. .name = "max7360-rotary",
  148. },
  149. .probe = max7360_rotary_probe,
  150. .remove = max7360_rotary_remove,
  151. };
  152. module_platform_driver(max7360_rotary_driver);
  153. MODULE_DESCRIPTION("MAX7360 Rotary driver");
  154. MODULE_AUTHOR("Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>");
  155. MODULE_LICENSE("GPL");