Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Allwinner sun4i low res adc attached tablet keys driver
0004  *
0005  * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
0006  */
0007 
0008 /*
0009  * Allwinnner sunxi SoCs have a lradc which is specifically designed to have
0010  * various (tablet) keys (ie home, back, search, etc). attached to it using
0011  * a resistor network. This driver is for the keys on such boards.
0012  *
0013  * There are 2 channels, currently this driver only supports channel 0 since
0014  * there are no boards known to use channel 1.
0015  */
0016 
0017 #include <linux/clk.h>
0018 #include <linux/err.h>
0019 #include <linux/init.h>
0020 #include <linux/input.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/io.h>
0023 #include <linux/module.h>
0024 #include <linux/of_platform.h>
0025 #include <linux/platform_device.h>
0026 #include <linux/pm_wakeirq.h>
0027 #include <linux/pm_wakeup.h>
0028 #include <linux/regulator/consumer.h>
0029 #include <linux/reset.h>
0030 #include <linux/slab.h>
0031 
0032 #define LRADC_CTRL      0x00
0033 #define LRADC_INTC      0x04
0034 #define LRADC_INTS      0x08
0035 #define LRADC_DATA0     0x0c
0036 #define LRADC_DATA1     0x10
0037 
0038 /* LRADC_CTRL bits */
0039 #define FIRST_CONVERT_DLY(x)    ((x) << 24) /* 8 bits */
0040 #define CHAN_SELECT(x)      ((x) << 22) /* 2 bits */
0041 #define CONTINUE_TIME_SEL(x)    ((x) << 16) /* 4 bits */
0042 #define KEY_MODE_SEL(x)     ((x) << 12) /* 2 bits */
0043 #define LEVELA_B_CNT(x)     ((x) << 8)  /* 4 bits */
0044 #define HOLD_KEY_EN(x)      ((x) << 7)
0045 #define HOLD_EN(x)      ((x) << 6)
0046 #define LEVELB_VOL(x)       ((x) << 4)  /* 2 bits */
0047 #define SAMPLE_RATE(x)      ((x) << 2)  /* 2 bits */
0048 #define ENABLE(x)       ((x) << 0)
0049 
0050 /* LRADC_INTC and LRADC_INTS bits */
0051 #define CHAN1_KEYUP_IRQ     BIT(12)
0052 #define CHAN1_ALRDY_HOLD_IRQ    BIT(11)
0053 #define CHAN1_HOLD_IRQ      BIT(10)
0054 #define CHAN1_KEYDOWN_IRQ   BIT(9)
0055 #define CHAN1_DATA_IRQ      BIT(8)
0056 #define CHAN0_KEYUP_IRQ     BIT(4)
0057 #define CHAN0_ALRDY_HOLD_IRQ    BIT(3)
0058 #define CHAN0_HOLD_IRQ      BIT(2)
0059 #define CHAN0_KEYDOWN_IRQ   BIT(1)
0060 #define CHAN0_DATA_IRQ      BIT(0)
0061 
0062 /* struct lradc_variant - Describe sun4i-a10-lradc-keys hardware variant
0063  * @divisor_numerator:      The numerator of lradc Vref internally divisor
0064  * @divisor_denominator:    The denominator of lradc Vref internally divisor
0065  * @has_clock_reset:        If the binding requires a clock and reset
0066  */
0067 struct lradc_variant {
0068     u8 divisor_numerator;
0069     u8 divisor_denominator;
0070     bool has_clock_reset;
0071 };
0072 
0073 static const struct lradc_variant lradc_variant_a10 = {
0074     .divisor_numerator = 2,
0075     .divisor_denominator = 3
0076 };
0077 
0078 static const struct lradc_variant r_lradc_variant_a83t = {
0079     .divisor_numerator = 3,
0080     .divisor_denominator = 4
0081 };
0082 
0083 static const struct lradc_variant lradc_variant_r329 = {
0084     .divisor_numerator = 3,
0085     .divisor_denominator = 4,
0086     .has_clock_reset = true,
0087 };
0088 
0089 struct sun4i_lradc_keymap {
0090     u32 voltage;
0091     u32 keycode;
0092 };
0093 
0094 struct sun4i_lradc_data {
0095     struct device *dev;
0096     struct input_dev *input;
0097     void __iomem *base;
0098     struct clk *clk;
0099     struct reset_control *reset;
0100     struct regulator *vref_supply;
0101     struct sun4i_lradc_keymap *chan0_map;
0102     const struct lradc_variant *variant;
0103     u32 chan0_map_count;
0104     u32 chan0_keycode;
0105     u32 vref;
0106 };
0107 
0108 static irqreturn_t sun4i_lradc_irq(int irq, void *dev_id)
0109 {
0110     struct sun4i_lradc_data *lradc = dev_id;
0111     u32 i, ints, val, voltage, diff, keycode = 0, closest = 0xffffffff;
0112 
0113     ints  = readl(lradc->base + LRADC_INTS);
0114 
0115     /*
0116      * lradc supports only one keypress at a time, release does not give
0117      * any info as to which key was released, so we cache the keycode.
0118      */
0119 
0120     if (ints & CHAN0_KEYUP_IRQ) {
0121         input_report_key(lradc->input, lradc->chan0_keycode, 0);
0122         lradc->chan0_keycode = 0;
0123     }
0124 
0125     if ((ints & CHAN0_KEYDOWN_IRQ) && lradc->chan0_keycode == 0) {
0126         val = readl(lradc->base + LRADC_DATA0) & 0x3f;
0127         voltage = val * lradc->vref / 63;
0128 
0129         for (i = 0; i < lradc->chan0_map_count; i++) {
0130             diff = abs(lradc->chan0_map[i].voltage - voltage);
0131             if (diff < closest) {
0132                 closest = diff;
0133                 keycode = lradc->chan0_map[i].keycode;
0134             }
0135         }
0136 
0137         lradc->chan0_keycode = keycode;
0138         input_report_key(lradc->input, lradc->chan0_keycode, 1);
0139     }
0140 
0141     input_sync(lradc->input);
0142 
0143     writel(ints, lradc->base + LRADC_INTS);
0144 
0145     return IRQ_HANDLED;
0146 }
0147 
0148 static int sun4i_lradc_open(struct input_dev *dev)
0149 {
0150     struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
0151     int error;
0152 
0153     error = regulator_enable(lradc->vref_supply);
0154     if (error)
0155         return error;
0156 
0157     error = reset_control_deassert(lradc->reset);
0158     if (error)
0159         goto err_disable_reg;
0160 
0161     error = clk_prepare_enable(lradc->clk);
0162     if (error)
0163         goto err_assert_reset;
0164 
0165     lradc->vref = regulator_get_voltage(lradc->vref_supply) *
0166               lradc->variant->divisor_numerator /
0167               lradc->variant->divisor_denominator;
0168     /*
0169      * Set sample time to 4 ms / 250 Hz. Wait 2 * 4 ms for key to
0170      * stabilize on press, wait (1 + 1) * 4 ms for key release
0171      */
0172     writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
0173         SAMPLE_RATE(0) | ENABLE(1), lradc->base + LRADC_CTRL);
0174 
0175     writel(CHAN0_KEYUP_IRQ | CHAN0_KEYDOWN_IRQ, lradc->base + LRADC_INTC);
0176 
0177     return 0;
0178 
0179 err_assert_reset:
0180     reset_control_assert(lradc->reset);
0181 err_disable_reg:
0182     regulator_disable(lradc->vref_supply);
0183 
0184     return error;
0185 }
0186 
0187 static void sun4i_lradc_close(struct input_dev *dev)
0188 {
0189     struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
0190 
0191     /* Disable lradc, leave other settings unchanged */
0192     writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
0193         SAMPLE_RATE(2), lradc->base + LRADC_CTRL);
0194     writel(0, lradc->base + LRADC_INTC);
0195 
0196     clk_disable_unprepare(lradc->clk);
0197     reset_control_assert(lradc->reset);
0198     regulator_disable(lradc->vref_supply);
0199 }
0200 
0201 static int sun4i_lradc_load_dt_keymap(struct device *dev,
0202                       struct sun4i_lradc_data *lradc)
0203 {
0204     struct device_node *np, *pp;
0205     int i;
0206     int error;
0207 
0208     np = dev->of_node;
0209     if (!np)
0210         return -EINVAL;
0211 
0212     lradc->chan0_map_count = of_get_child_count(np);
0213     if (lradc->chan0_map_count == 0) {
0214         dev_err(dev, "keymap is missing in device tree\n");
0215         return -EINVAL;
0216     }
0217 
0218     lradc->chan0_map = devm_kmalloc_array(dev, lradc->chan0_map_count,
0219                           sizeof(struct sun4i_lradc_keymap),
0220                           GFP_KERNEL);
0221     if (!lradc->chan0_map)
0222         return -ENOMEM;
0223 
0224     i = 0;
0225     for_each_child_of_node(np, pp) {
0226         struct sun4i_lradc_keymap *map = &lradc->chan0_map[i];
0227         u32 channel;
0228 
0229         error = of_property_read_u32(pp, "channel", &channel);
0230         if (error || channel != 0) {
0231             dev_err(dev, "%pOFn: Inval channel prop\n", pp);
0232             of_node_put(pp);
0233             return -EINVAL;
0234         }
0235 
0236         error = of_property_read_u32(pp, "voltage", &map->voltage);
0237         if (error) {
0238             dev_err(dev, "%pOFn: Inval voltage prop\n", pp);
0239             of_node_put(pp);
0240             return -EINVAL;
0241         }
0242 
0243         error = of_property_read_u32(pp, "linux,code", &map->keycode);
0244         if (error) {
0245             dev_err(dev, "%pOFn: Inval linux,code prop\n", pp);
0246             of_node_put(pp);
0247             return -EINVAL;
0248         }
0249 
0250         i++;
0251     }
0252 
0253     return 0;
0254 }
0255 
0256 static int sun4i_lradc_probe(struct platform_device *pdev)
0257 {
0258     struct sun4i_lradc_data *lradc;
0259     struct device *dev = &pdev->dev;
0260     int error, i, irq;
0261 
0262     lradc = devm_kzalloc(dev, sizeof(struct sun4i_lradc_data), GFP_KERNEL);
0263     if (!lradc)
0264         return -ENOMEM;
0265 
0266     error = sun4i_lradc_load_dt_keymap(dev, lradc);
0267     if (error)
0268         return error;
0269 
0270     lradc->variant = of_device_get_match_data(&pdev->dev);
0271     if (!lradc->variant) {
0272         dev_err(&pdev->dev, "Missing sun4i-a10-lradc-keys variant\n");
0273         return -EINVAL;
0274     }
0275 
0276     if (lradc->variant->has_clock_reset) {
0277         lradc->clk = devm_clk_get(dev, NULL);
0278         if (IS_ERR(lradc->clk))
0279             return PTR_ERR(lradc->clk);
0280 
0281         lradc->reset = devm_reset_control_get_exclusive(dev, NULL);
0282         if (IS_ERR(lradc->reset))
0283             return PTR_ERR(lradc->reset);
0284     }
0285 
0286     lradc->vref_supply = devm_regulator_get(dev, "vref");
0287     if (IS_ERR(lradc->vref_supply))
0288         return PTR_ERR(lradc->vref_supply);
0289 
0290     lradc->dev = dev;
0291     lradc->input = devm_input_allocate_device(dev);
0292     if (!lradc->input)
0293         return -ENOMEM;
0294 
0295     lradc->input->name = pdev->name;
0296     lradc->input->phys = "sun4i_lradc/input0";
0297     lradc->input->open = sun4i_lradc_open;
0298     lradc->input->close = sun4i_lradc_close;
0299     lradc->input->id.bustype = BUS_HOST;
0300     lradc->input->id.vendor = 0x0001;
0301     lradc->input->id.product = 0x0001;
0302     lradc->input->id.version = 0x0100;
0303 
0304     __set_bit(EV_KEY, lradc->input->evbit);
0305     for (i = 0; i < lradc->chan0_map_count; i++)
0306         __set_bit(lradc->chan0_map[i].keycode, lradc->input->keybit);
0307 
0308     input_set_drvdata(lradc->input, lradc);
0309 
0310     lradc->base = devm_ioremap_resource(dev,
0311                   platform_get_resource(pdev, IORESOURCE_MEM, 0));
0312     if (IS_ERR(lradc->base))
0313         return PTR_ERR(lradc->base);
0314 
0315     irq = platform_get_irq(pdev, 0);
0316     if (irq < 0)
0317         return irq;
0318 
0319     error = devm_request_irq(dev, irq, sun4i_lradc_irq, 0,
0320                  "sun4i-a10-lradc-keys", lradc);
0321     if (error)
0322         return error;
0323 
0324     error = input_register_device(lradc->input);
0325     if (error)
0326         return error;
0327 
0328     if (device_property_read_bool(dev, "wakeup-source")) {
0329         error = dev_pm_set_wake_irq(dev, irq);
0330         if (error)
0331             dev_warn(dev,
0332                  "Failed to set IRQ %d as a wake IRQ: %d\n",
0333                  irq, error);
0334         else
0335             device_set_wakeup_capable(dev, true);
0336     }
0337 
0338     return 0;
0339 }
0340 
0341 static const struct of_device_id sun4i_lradc_of_match[] = {
0342     { .compatible = "allwinner,sun4i-a10-lradc-keys",
0343         .data = &lradc_variant_a10 },
0344     { .compatible = "allwinner,sun8i-a83t-r-lradc",
0345         .data = &r_lradc_variant_a83t },
0346     { .compatible = "allwinner,sun50i-r329-lradc",
0347         .data = &lradc_variant_r329 },
0348     { /* sentinel */ }
0349 };
0350 MODULE_DEVICE_TABLE(of, sun4i_lradc_of_match);
0351 
0352 static struct platform_driver sun4i_lradc_driver = {
0353     .driver = {
0354         .name   = "sun4i-a10-lradc-keys",
0355         .of_match_table = of_match_ptr(sun4i_lradc_of_match),
0356     },
0357     .probe  = sun4i_lradc_probe,
0358 };
0359 
0360 module_platform_driver(sun4i_lradc_driver);
0361 
0362 MODULE_DESCRIPTION("Allwinner sun4i low res adc attached tablet keys driver");
0363 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0364 MODULE_LICENSE("GPL");