1 From d8b5553dbf60e519d565dbd83327b08865e960e2 Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Fri, 27 Dec 2013 15:25:28 +0100
4 Subject: [PATCH] input: sun4i-ts: Add support for temperature sensor
6 The sun4i resisitive touchscreen controller also comes with a built-in
7 temperature sensor. This commit adds support for it.
9 This commit also introduces a new "ts-attached" device-tree property,
10 when this is not set, the input part of the driver won't register. This way
11 the internal temperature sensor can be used to measure the SoC temperature
12 independent of there actually being a touchscreen attached to the controller.
14 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
16 .../bindings/input/touchscreen/sun4i.txt | 5 +
17 drivers/input/touchscreen/sun4i-ts.c | 140 ++++++++++++++++-----
18 2 files changed, 114 insertions(+), 31 deletions(-)
20 --- a/Documentation/devicetree/bindings/input/touchscreen/sun4i.txt
21 +++ b/Documentation/devicetree/bindings/input/touchscreen/sun4i.txt
22 @@ -6,10 +6,15 @@ Required properties:
23 - reg: mmio address range of the chip
24 - interrupts: interrupt to which the chip is connected
27 + - allwinner,ts-attached: boolean indicating that an actual touchscreen is
28 + attached to the controller
33 compatible = "allwinner,sun4i-ts";
34 reg = <0x01c25000 0x100>;
36 + allwinner,ts-attached;
38 --- a/drivers/input/touchscreen/sun4i-ts.c
39 +++ b/drivers/input/touchscreen/sun4i-ts.c
42 * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
44 + * The hwmon parts are based on work by Corentin LABBE which is:
45 + * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
47 * This program is free software; you can redistribute it and/or modify
48 * it under the terms of the GNU General Public License as published by
49 * the Free Software Foundation; either version 2 of the License, or
53 #include <linux/err.h>
54 +#include <linux/hwmon.h>
55 #include <linux/init.h>
56 #include <linux/input.h>
57 #include <linux/interrupt.h>
58 @@ -106,14 +110,12 @@ struct sun4i_ts_data {
61 bool ignore_fifo_data;
65 -static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
66 +static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
68 - struct sun4i_ts_data *ts = dev_id;
71 - reg_val = readl(ts->base + TP_INT_FIFOS);
74 if (reg_val & FIFO_DATA_PENDING) {
75 x = readl(ts->base + TP_DATA);
76 @@ -139,6 +141,20 @@ static irqreturn_t sun4i_ts_irq(int irq,
77 input_report_key(ts->input, BTN_TOUCH, 0);
78 input_sync(ts->input);
82 +static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
84 + struct sun4i_ts_data *ts = dev_id;
87 + reg_val = readl(ts->base + TP_INT_FIFOS);
89 + if (reg_val & TEMP_DATA_PENDING)
90 + ts->temp_data = readl(ts->base + TEMP_DATA);
93 + sun4i_ts_irq_handle_input(ts, reg_val);
95 writel(reg_val, ts->base + TP_INT_FIFOS);
97 @@ -149,9 +165,9 @@ static int sun4i_ts_open(struct input_de
99 struct sun4i_ts_data *ts = input_get_drvdata(dev);
101 - /* Flush, set trig level to 1, enable data and up irqs */
102 - writel(DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) | TP_UP_IRQ_EN(1),
103 - ts->base + TP_INT_FIFOC);
104 + /* Flush, set trig level to 1, enable temp, data and up irqs */
105 + writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
106 + TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
110 @@ -160,15 +176,48 @@ static void sun4i_ts_close(struct input_
112 struct sun4i_ts_data *ts = input_get_drvdata(dev);
114 - /* Deactivate all IRQs */
115 - writel(0, ts->base + TP_INT_FIFOC);
116 + /* Deactivate all input IRQs */
117 + writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
120 +static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
123 + struct sun4i_ts_data *ts = dev_get_drvdata(dev);
125 + /* No temp_data until the first irq */
126 + if (ts->temp_data == -1)
129 + return sprintf(buf, "%d\n", (ts->temp_data - 1447) * 100);
132 +static ssize_t show_temp_label(struct device *dev,
133 + struct device_attribute *devattr, char *buf)
135 + return sprintf(buf, "SoC temperature\n");
138 +static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
139 +static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
141 +static struct attribute *sun4i_ts_attrs[] = {
142 + &dev_attr_temp1_input.attr,
143 + &dev_attr_temp1_label.attr,
146 +ATTRIBUTE_GROUPS(sun4i_ts);
148 static int sun4i_ts_probe(struct platform_device *pdev)
150 struct sun4i_ts_data *ts;
151 struct device *dev = &pdev->dev;
152 + struct device_node *np = dev->of_node;
153 + struct device *hwmon;
157 + ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
159 ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
161 @@ -176,24 +225,27 @@ static int sun4i_ts_probe(struct platfor
164 ts->ignore_fifo_data = true;
165 + ts->temp_data = -1;
167 - ts->input = devm_input_allocate_device(dev);
171 - ts->input->name = pdev->name;
172 - ts->input->phys = "sun4i_ts/input0";
173 - ts->input->open = sun4i_ts_open;
174 - ts->input->close = sun4i_ts_close;
175 - ts->input->id.bustype = BUS_HOST;
176 - ts->input->id.vendor = 0x0001;
177 - ts->input->id.product = 0x0001;
178 - ts->input->id.version = 0x0100;
179 - ts->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
180 - set_bit(BTN_TOUCH, ts->input->keybit);
181 - input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
182 - input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
183 - input_set_drvdata(ts->input, ts);
185 + ts->input = devm_input_allocate_device(dev);
189 + ts->input->name = pdev->name;
190 + ts->input->phys = "sun4i_ts/input0";
191 + ts->input->open = sun4i_ts_open;
192 + ts->input->close = sun4i_ts_close;
193 + ts->input->id.bustype = BUS_HOST;
194 + ts->input->id.vendor = 0x0001;
195 + ts->input->id.product = 0x0001;
196 + ts->input->id.version = 0x0100;
197 + ts->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
198 + set_bit(BTN_TOUCH, ts->input->keybit);
199 + input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
200 + input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
201 + input_set_drvdata(ts->input, ts);
204 ts->base = devm_ioremap_resource(dev,
205 platform_get_resource(pdev, IORESOURCE_MEM, 0));
206 @@ -232,14 +284,39 @@ static int sun4i_ts_probe(struct platfor
207 writel(STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1) | TP_MODE_EN(1),
208 ts->base + TP_CTRL1);
210 - ret = input_register_device(ts->input);
213 + hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
214 + ts, sun4i_ts_groups);
216 + return PTR_ERR(hwmon);
218 + writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
221 + ret = input_register_device(ts->input);
223 + writel(0, ts->base + TP_INT_FIFOC);
228 platform_set_drvdata(pdev, ts);
232 +static int sun4i_ts_remove(struct platform_device *pdev)
234 + struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
236 + /* Explicit unregister to avoid open/close changing the imask later */
238 + input_unregister_device(ts->input);
240 + /* Deactivate all IRQs */
241 + writel(0, ts->base + TP_INT_FIFOC);
246 static const struct of_device_id sun4i_ts_of_match[] = {
247 { .compatible = "allwinner,sun4i-ts", },
249 @@ -253,6 +330,7 @@ static struct platform_driver sun4i_ts_d
250 .of_match_table = of_match_ptr(sun4i_ts_of_match),
252 .probe = sun4i_ts_probe,
253 + .remove = sun4i_ts_remove,
256 module_platform_driver(sun4i_ts_driver);