sun4i-lradc-keys.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Allwinner sun4i low res adc attached tablet keys driver
  4. *
  5. * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
  6. */
  7. /*
  8. * Allwinnner sunxi SoCs have a lradc which is specifically designed to have
  9. * various (tablet) keys (ie home, back, search, etc). attached to it using
  10. * a resistor network. This driver is for the keys on such boards.
  11. *
  12. * There are 2 channels, currently this driver only supports channel 0 since
  13. * there are no boards known to use channel 1.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/input.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_wakeirq.h>
  25. #include <linux/pm_wakeup.h>
  26. #include <linux/property.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/reset.h>
  29. #include <linux/slab.h>
  30. #define LRADC_CTRL 0x00
  31. #define LRADC_INTC 0x04
  32. #define LRADC_INTS 0x08
  33. #define LRADC_DATA0 0x0c
  34. #define LRADC_DATA1 0x10
  35. /* LRADC_CTRL bits */
  36. #define FIRST_CONVERT_DLY(x) ((x) << 24) /* 8 bits */
  37. #define CHAN_SELECT(x) ((x) << 22) /* 2 bits */
  38. #define CONTINUE_TIME_SEL(x) ((x) << 16) /* 4 bits */
  39. #define KEY_MODE_SEL(x) ((x) << 12) /* 2 bits */
  40. #define LEVELA_B_CNT(x) ((x) << 8) /* 4 bits */
  41. #define HOLD_KEY_EN(x) ((x) << 7)
  42. #define HOLD_EN(x) ((x) << 6)
  43. #define LEVELB_VOL(x) ((x) << 4) /* 2 bits */
  44. #define SAMPLE_RATE(x) ((x) << 2) /* 2 bits */
  45. #define ENABLE(x) ((x) << 0)
  46. /* LRADC_INTC and LRADC_INTS bits */
  47. #define CHAN1_KEYUP_IRQ BIT(12)
  48. #define CHAN1_ALRDY_HOLD_IRQ BIT(11)
  49. #define CHAN1_HOLD_IRQ BIT(10)
  50. #define CHAN1_KEYDOWN_IRQ BIT(9)
  51. #define CHAN1_DATA_IRQ BIT(8)
  52. #define CHAN0_KEYUP_IRQ BIT(4)
  53. #define CHAN0_ALRDY_HOLD_IRQ BIT(3)
  54. #define CHAN0_HOLD_IRQ BIT(2)
  55. #define CHAN0_KEYDOWN_IRQ BIT(1)
  56. #define CHAN0_DATA_IRQ BIT(0)
  57. /* struct lradc_variant - Describe sun4i-a10-lradc-keys hardware variant
  58. * @divisor_numerator: The numerator of lradc Vref internally divisor
  59. * @divisor_denominator: The denominator of lradc Vref internally divisor
  60. * @has_clock_reset: If the binding requires a clock and reset
  61. */
  62. struct lradc_variant {
  63. u8 divisor_numerator;
  64. u8 divisor_denominator;
  65. bool has_clock_reset;
  66. };
  67. static const struct lradc_variant lradc_variant_a10 = {
  68. .divisor_numerator = 2,
  69. .divisor_denominator = 3
  70. };
  71. static const struct lradc_variant r_lradc_variant_a83t = {
  72. .divisor_numerator = 3,
  73. .divisor_denominator = 4
  74. };
  75. static const struct lradc_variant lradc_variant_r329 = {
  76. .divisor_numerator = 3,
  77. .divisor_denominator = 4,
  78. .has_clock_reset = true,
  79. };
  80. struct sun4i_lradc_keymap {
  81. u32 voltage;
  82. u32 keycode;
  83. };
  84. struct sun4i_lradc_data {
  85. struct device *dev;
  86. struct input_dev *input;
  87. void __iomem *base;
  88. struct clk *clk;
  89. struct reset_control *reset;
  90. struct regulator *vref_supply;
  91. struct sun4i_lradc_keymap *chan0_map;
  92. const struct lradc_variant *variant;
  93. u32 chan0_map_count;
  94. u32 chan0_keycode;
  95. u32 vref;
  96. };
  97. static irqreturn_t sun4i_lradc_irq(int irq, void *dev_id)
  98. {
  99. struct sun4i_lradc_data *lradc = dev_id;
  100. u32 i, ints, val, voltage, diff, keycode = 0, closest = 0xffffffff;
  101. ints = readl(lradc->base + LRADC_INTS);
  102. /*
  103. * lradc supports only one keypress at a time, release does not give
  104. * any info as to which key was released, so we cache the keycode.
  105. */
  106. if (ints & CHAN0_KEYUP_IRQ) {
  107. input_report_key(lradc->input, lradc->chan0_keycode, 0);
  108. lradc->chan0_keycode = 0;
  109. }
  110. if ((ints & CHAN0_KEYDOWN_IRQ) && lradc->chan0_keycode == 0) {
  111. val = readl(lradc->base + LRADC_DATA0) & 0x3f;
  112. voltage = val * lradc->vref / 63;
  113. for (i = 0; i < lradc->chan0_map_count; i++) {
  114. diff = abs(lradc->chan0_map[i].voltage - voltage);
  115. if (diff < closest) {
  116. closest = diff;
  117. keycode = lradc->chan0_map[i].keycode;
  118. }
  119. }
  120. lradc->chan0_keycode = keycode;
  121. input_report_key(lradc->input, lradc->chan0_keycode, 1);
  122. }
  123. input_sync(lradc->input);
  124. writel(ints, lradc->base + LRADC_INTS);
  125. return IRQ_HANDLED;
  126. }
  127. static int sun4i_lradc_open(struct input_dev *dev)
  128. {
  129. struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
  130. int error;
  131. error = regulator_enable(lradc->vref_supply);
  132. if (error)
  133. return error;
  134. error = reset_control_deassert(lradc->reset);
  135. if (error)
  136. goto err_disable_reg;
  137. error = clk_prepare_enable(lradc->clk);
  138. if (error)
  139. goto err_assert_reset;
  140. lradc->vref = regulator_get_voltage(lradc->vref_supply) *
  141. lradc->variant->divisor_numerator /
  142. lradc->variant->divisor_denominator;
  143. /*
  144. * Set sample time to 4 ms / 250 Hz. Wait 2 * 4 ms for key to
  145. * stabilize on press, wait (1 + 1) * 4 ms for key release
  146. */
  147. writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
  148. SAMPLE_RATE(0) | ENABLE(1), lradc->base + LRADC_CTRL);
  149. writel(CHAN0_KEYUP_IRQ | CHAN0_KEYDOWN_IRQ, lradc->base + LRADC_INTC);
  150. return 0;
  151. err_assert_reset:
  152. reset_control_assert(lradc->reset);
  153. err_disable_reg:
  154. regulator_disable(lradc->vref_supply);
  155. return error;
  156. }
  157. static void sun4i_lradc_close(struct input_dev *dev)
  158. {
  159. struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
  160. /* Disable lradc, leave other settings unchanged */
  161. writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
  162. SAMPLE_RATE(2), lradc->base + LRADC_CTRL);
  163. writel(0, lradc->base + LRADC_INTC);
  164. clk_disable_unprepare(lradc->clk);
  165. reset_control_assert(lradc->reset);
  166. regulator_disable(lradc->vref_supply);
  167. }
  168. static int sun4i_lradc_load_dt_keymap(struct device *dev,
  169. struct sun4i_lradc_data *lradc)
  170. {
  171. struct device_node *np, *pp;
  172. int i;
  173. int error;
  174. np = dev->of_node;
  175. if (!np)
  176. return -EINVAL;
  177. lradc->chan0_map_count = of_get_child_count(np);
  178. if (lradc->chan0_map_count == 0) {
  179. dev_err(dev, "keymap is missing in device tree\n");
  180. return -EINVAL;
  181. }
  182. lradc->chan0_map = devm_kmalloc_array(dev, lradc->chan0_map_count,
  183. sizeof(struct sun4i_lradc_keymap),
  184. GFP_KERNEL);
  185. if (!lradc->chan0_map)
  186. return -ENOMEM;
  187. i = 0;
  188. for_each_child_of_node(np, pp) {
  189. struct sun4i_lradc_keymap *map = &lradc->chan0_map[i];
  190. u32 channel;
  191. error = of_property_read_u32(pp, "channel", &channel);
  192. if (error || channel != 0) {
  193. dev_err(dev, "%pOFn: Inval channel prop\n", pp);
  194. of_node_put(pp);
  195. return -EINVAL;
  196. }
  197. error = of_property_read_u32(pp, "voltage", &map->voltage);
  198. if (error) {
  199. dev_err(dev, "%pOFn: Inval voltage prop\n", pp);
  200. of_node_put(pp);
  201. return -EINVAL;
  202. }
  203. error = of_property_read_u32(pp, "linux,code", &map->keycode);
  204. if (error) {
  205. dev_err(dev, "%pOFn: Inval linux,code prop\n", pp);
  206. of_node_put(pp);
  207. return -EINVAL;
  208. }
  209. i++;
  210. }
  211. return 0;
  212. }
  213. static int sun4i_lradc_probe(struct platform_device *pdev)
  214. {
  215. struct sun4i_lradc_data *lradc;
  216. struct device *dev = &pdev->dev;
  217. int error, i, irq;
  218. lradc = devm_kzalloc(dev, sizeof(struct sun4i_lradc_data), GFP_KERNEL);
  219. if (!lradc)
  220. return -ENOMEM;
  221. error = sun4i_lradc_load_dt_keymap(dev, lradc);
  222. if (error)
  223. return error;
  224. lradc->variant = of_device_get_match_data(&pdev->dev);
  225. if (!lradc->variant) {
  226. dev_err(&pdev->dev, "Missing sun4i-a10-lradc-keys variant\n");
  227. return -EINVAL;
  228. }
  229. if (lradc->variant->has_clock_reset) {
  230. lradc->clk = devm_clk_get(dev, NULL);
  231. if (IS_ERR(lradc->clk))
  232. return PTR_ERR(lradc->clk);
  233. lradc->reset = devm_reset_control_get_exclusive(dev, NULL);
  234. if (IS_ERR(lradc->reset))
  235. return PTR_ERR(lradc->reset);
  236. }
  237. lradc->vref_supply = devm_regulator_get(dev, "vref");
  238. if (IS_ERR(lradc->vref_supply))
  239. return PTR_ERR(lradc->vref_supply);
  240. lradc->dev = dev;
  241. lradc->input = devm_input_allocate_device(dev);
  242. if (!lradc->input)
  243. return -ENOMEM;
  244. lradc->input->name = pdev->name;
  245. lradc->input->phys = "sun4i_lradc/input0";
  246. lradc->input->open = sun4i_lradc_open;
  247. lradc->input->close = sun4i_lradc_close;
  248. lradc->input->id.bustype = BUS_HOST;
  249. lradc->input->id.vendor = 0x0001;
  250. lradc->input->id.product = 0x0001;
  251. lradc->input->id.version = 0x0100;
  252. __set_bit(EV_KEY, lradc->input->evbit);
  253. for (i = 0; i < lradc->chan0_map_count; i++)
  254. __set_bit(lradc->chan0_map[i].keycode, lradc->input->keybit);
  255. input_set_drvdata(lradc->input, lradc);
  256. lradc->base = devm_platform_ioremap_resource(pdev, 0);
  257. if (IS_ERR(lradc->base))
  258. return PTR_ERR(lradc->base);
  259. irq = platform_get_irq(pdev, 0);
  260. if (irq < 0)
  261. return irq;
  262. error = devm_request_irq(dev, irq, sun4i_lradc_irq, 0,
  263. "sun4i-a10-lradc-keys", lradc);
  264. if (error)
  265. return error;
  266. error = input_register_device(lradc->input);
  267. if (error)
  268. return error;
  269. if (device_property_read_bool(dev, "wakeup-source")) {
  270. error = dev_pm_set_wake_irq(dev, irq);
  271. if (error)
  272. dev_warn(dev,
  273. "Failed to set IRQ %d as a wake IRQ: %d\n",
  274. irq, error);
  275. else
  276. device_set_wakeup_capable(dev, true);
  277. }
  278. return 0;
  279. }
  280. static const struct of_device_id sun4i_lradc_of_match[] = {
  281. { .compatible = "allwinner,sun4i-a10-lradc-keys",
  282. .data = &lradc_variant_a10 },
  283. { .compatible = "allwinner,sun8i-a83t-r-lradc",
  284. .data = &r_lradc_variant_a83t },
  285. { .compatible = "allwinner,sun50i-r329-lradc",
  286. .data = &lradc_variant_r329 },
  287. { /* sentinel */ }
  288. };
  289. MODULE_DEVICE_TABLE(of, sun4i_lradc_of_match);
  290. static struct platform_driver sun4i_lradc_driver = {
  291. .driver = {
  292. .name = "sun4i-a10-lradc-keys",
  293. .of_match_table = of_match_ptr(sun4i_lradc_of_match),
  294. },
  295. .probe = sun4i_lradc_probe,
  296. };
  297. module_platform_driver(sun4i_lradc_driver);
  298. MODULE_DESCRIPTION("Allwinner sun4i low res adc attached tablet keys driver");
  299. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  300. MODULE_LICENSE("GPL");