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 <[email protected]>
  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_platform.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_wakeirq.h>
  25. #include <linux/pm_wakeup.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/reset.h>
  28. #include <linux/slab.h>
  29. #define LRADC_CTRL 0x00
  30. #define LRADC_INTC 0x04
  31. #define LRADC_INTS 0x08
  32. #define LRADC_DATA0 0x0c
  33. #define LRADC_DATA1 0x10
  34. /* LRADC_CTRL bits */
  35. #define FIRST_CONVERT_DLY(x) ((x) << 24) /* 8 bits */
  36. #define CHAN_SELECT(x) ((x) << 22) /* 2 bits */
  37. #define CONTINUE_TIME_SEL(x) ((x) << 16) /* 4 bits */
  38. #define KEY_MODE_SEL(x) ((x) << 12) /* 2 bits */
  39. #define LEVELA_B_CNT(x) ((x) << 8) /* 4 bits */
  40. #define HOLD_KEY_EN(x) ((x) << 7)
  41. #define HOLD_EN(x) ((x) << 6)
  42. #define LEVELB_VOL(x) ((x) << 4) /* 2 bits */
  43. #define SAMPLE_RATE(x) ((x) << 2) /* 2 bits */
  44. #define ENABLE(x) ((x) << 0)
  45. /* LRADC_INTC and LRADC_INTS bits */
  46. #define CHAN1_KEYUP_IRQ BIT(12)
  47. #define CHAN1_ALRDY_HOLD_IRQ BIT(11)
  48. #define CHAN1_HOLD_IRQ BIT(10)
  49. #define CHAN1_KEYDOWN_IRQ BIT(9)
  50. #define CHAN1_DATA_IRQ BIT(8)
  51. #define CHAN0_KEYUP_IRQ BIT(4)
  52. #define CHAN0_ALRDY_HOLD_IRQ BIT(3)
  53. #define CHAN0_HOLD_IRQ BIT(2)
  54. #define CHAN0_KEYDOWN_IRQ BIT(1)
  55. #define CHAN0_DATA_IRQ BIT(0)
  56. /* struct lradc_variant - Describe sun4i-a10-lradc-keys hardware variant
  57. * @divisor_numerator: The numerator of lradc Vref internally divisor
  58. * @divisor_denominator: The denominator of lradc Vref internally divisor
  59. * @has_clock_reset: If the binding requires a clock and reset
  60. */
  61. struct lradc_variant {
  62. u8 divisor_numerator;
  63. u8 divisor_denominator;
  64. bool has_clock_reset;
  65. };
  66. static const struct lradc_variant lradc_variant_a10 = {
  67. .divisor_numerator = 2,
  68. .divisor_denominator = 3
  69. };
  70. static const struct lradc_variant r_lradc_variant_a83t = {
  71. .divisor_numerator = 3,
  72. .divisor_denominator = 4
  73. };
  74. static const struct lradc_variant lradc_variant_r329 = {
  75. .divisor_numerator = 3,
  76. .divisor_denominator = 4,
  77. .has_clock_reset = true,
  78. };
  79. struct sun4i_lradc_keymap {
  80. u32 voltage;
  81. u32 keycode;
  82. };
  83. struct sun4i_lradc_data {
  84. struct device *dev;
  85. struct input_dev *input;
  86. void __iomem *base;
  87. struct clk *clk;
  88. struct reset_control *reset;
  89. struct regulator *vref_supply;
  90. struct sun4i_lradc_keymap *chan0_map;
  91. const struct lradc_variant *variant;
  92. u32 chan0_map_count;
  93. u32 chan0_keycode;
  94. u32 vref;
  95. };
  96. static irqreturn_t sun4i_lradc_irq(int irq, void *dev_id)
  97. {
  98. struct sun4i_lradc_data *lradc = dev_id;
  99. u32 i, ints, val, voltage, diff, keycode = 0, closest = 0xffffffff;
  100. ints = readl(lradc->base + LRADC_INTS);
  101. /*
  102. * lradc supports only one keypress at a time, release does not give
  103. * any info as to which key was released, so we cache the keycode.
  104. */
  105. if (ints & CHAN0_KEYUP_IRQ) {
  106. input_report_key(lradc->input, lradc->chan0_keycode, 0);
  107. lradc->chan0_keycode = 0;
  108. }
  109. if ((ints & CHAN0_KEYDOWN_IRQ) && lradc->chan0_keycode == 0) {
  110. val = readl(lradc->base + LRADC_DATA0) & 0x3f;
  111. voltage = val * lradc->vref / 63;
  112. for (i = 0; i < lradc->chan0_map_count; i++) {
  113. diff = abs(lradc->chan0_map[i].voltage - voltage);
  114. if (diff < closest) {
  115. closest = diff;
  116. keycode = lradc->chan0_map[i].keycode;
  117. }
  118. }
  119. lradc->chan0_keycode = keycode;
  120. input_report_key(lradc->input, lradc->chan0_keycode, 1);
  121. }
  122. input_sync(lradc->input);
  123. writel(ints, lradc->base + LRADC_INTS);
  124. return IRQ_HANDLED;
  125. }
  126. static int sun4i_lradc_open(struct input_dev *dev)
  127. {
  128. struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
  129. int error;
  130. error = regulator_enable(lradc->vref_supply);
  131. if (error)
  132. return error;
  133. error = reset_control_deassert(lradc->reset);
  134. if (error)
  135. goto err_disable_reg;
  136. error = clk_prepare_enable(lradc->clk);
  137. if (error)
  138. goto err_assert_reset;
  139. lradc->vref = regulator_get_voltage(lradc->vref_supply) *
  140. lradc->variant->divisor_numerator /
  141. lradc->variant->divisor_denominator;
  142. /*
  143. * Set sample time to 4 ms / 250 Hz. Wait 2 * 4 ms for key to
  144. * stabilize on press, wait (1 + 1) * 4 ms for key release
  145. */
  146. writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
  147. SAMPLE_RATE(0) | ENABLE(1), lradc->base + LRADC_CTRL);
  148. writel(CHAN0_KEYUP_IRQ | CHAN0_KEYDOWN_IRQ, lradc->base + LRADC_INTC);
  149. return 0;
  150. err_assert_reset:
  151. reset_control_assert(lradc->reset);
  152. err_disable_reg:
  153. regulator_disable(lradc->vref_supply);
  154. return error;
  155. }
  156. static void sun4i_lradc_close(struct input_dev *dev)
  157. {
  158. struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
  159. /* Disable lradc, leave other settings unchanged */
  160. writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
  161. SAMPLE_RATE(2), lradc->base + LRADC_CTRL);
  162. writel(0, lradc->base + LRADC_INTC);
  163. clk_disable_unprepare(lradc->clk);
  164. reset_control_assert(lradc->reset);
  165. regulator_disable(lradc->vref_supply);
  166. }
  167. static int sun4i_lradc_load_dt_keymap(struct device *dev,
  168. struct sun4i_lradc_data *lradc)
  169. {
  170. struct device_node *np, *pp;
  171. int i;
  172. int error;
  173. np = dev->of_node;
  174. if (!np)
  175. return -EINVAL;
  176. lradc->chan0_map_count = of_get_child_count(np);
  177. if (lradc->chan0_map_count == 0) {
  178. dev_err(dev, "keymap is missing in device tree\n");
  179. return -EINVAL;
  180. }
  181. lradc->chan0_map = devm_kmalloc_array(dev, lradc->chan0_map_count,
  182. sizeof(struct sun4i_lradc_keymap),
  183. GFP_KERNEL);
  184. if (!lradc->chan0_map)
  185. return -ENOMEM;
  186. i = 0;
  187. for_each_child_of_node(np, pp) {
  188. struct sun4i_lradc_keymap *map = &lradc->chan0_map[i];
  189. u32 channel;
  190. error = of_property_read_u32(pp, "channel", &channel);
  191. if (error || channel != 0) {
  192. dev_err(dev, "%pOFn: Inval channel prop\n", pp);
  193. of_node_put(pp);
  194. return -EINVAL;
  195. }
  196. error = of_property_read_u32(pp, "voltage", &map->voltage);
  197. if (error) {
  198. dev_err(dev, "%pOFn: Inval voltage prop\n", pp);
  199. of_node_put(pp);
  200. return -EINVAL;
  201. }
  202. error = of_property_read_u32(pp, "linux,code", &map->keycode);
  203. if (error) {
  204. dev_err(dev, "%pOFn: Inval linux,code prop\n", pp);
  205. of_node_put(pp);
  206. return -EINVAL;
  207. }
  208. i++;
  209. }
  210. return 0;
  211. }
  212. static int sun4i_lradc_probe(struct platform_device *pdev)
  213. {
  214. struct sun4i_lradc_data *lradc;
  215. struct device *dev = &pdev->dev;
  216. int error, i, irq;
  217. lradc = devm_kzalloc(dev, sizeof(struct sun4i_lradc_data), GFP_KERNEL);
  218. if (!lradc)
  219. return -ENOMEM;
  220. error = sun4i_lradc_load_dt_keymap(dev, lradc);
  221. if (error)
  222. return error;
  223. lradc->variant = of_device_get_match_data(&pdev->dev);
  224. if (!lradc->variant) {
  225. dev_err(&pdev->dev, "Missing sun4i-a10-lradc-keys variant\n");
  226. return -EINVAL;
  227. }
  228. if (lradc->variant->has_clock_reset) {
  229. lradc->clk = devm_clk_get(dev, NULL);
  230. if (IS_ERR(lradc->clk))
  231. return PTR_ERR(lradc->clk);
  232. lradc->reset = devm_reset_control_get_exclusive(dev, NULL);
  233. if (IS_ERR(lradc->reset))
  234. return PTR_ERR(lradc->reset);
  235. }
  236. lradc->vref_supply = devm_regulator_get(dev, "vref");
  237. if (IS_ERR(lradc->vref_supply))
  238. return PTR_ERR(lradc->vref_supply);
  239. lradc->dev = dev;
  240. lradc->input = devm_input_allocate_device(dev);
  241. if (!lradc->input)
  242. return -ENOMEM;
  243. lradc->input->name = pdev->name;
  244. lradc->input->phys = "sun4i_lradc/input0";
  245. lradc->input->open = sun4i_lradc_open;
  246. lradc->input->close = sun4i_lradc_close;
  247. lradc->input->id.bustype = BUS_HOST;
  248. lradc->input->id.vendor = 0x0001;
  249. lradc->input->id.product = 0x0001;
  250. lradc->input->id.version = 0x0100;
  251. __set_bit(EV_KEY, lradc->input->evbit);
  252. for (i = 0; i < lradc->chan0_map_count; i++)
  253. __set_bit(lradc->chan0_map[i].keycode, lradc->input->keybit);
  254. input_set_drvdata(lradc->input, lradc);
  255. lradc->base = devm_ioremap_resource(dev,
  256. platform_get_resource(pdev, IORESOURCE_MEM, 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 <[email protected]>");
  300. MODULE_LICENSE("GPL");