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