kernel: update 3.14 to 3.14.18
[openwrt.git] / target / linux / sunxi / patches-3.14 / 171-input-add-temp-sensor-support.patch
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
5
6 The sun4i resisitive touchscreen controller also comes with a built-in
7 temperature sensor. This commit adds support for it.
8
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.
13
14 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
15 ---
16  .../bindings/input/touchscreen/sun4i.txt           |   5 +
17  drivers/input/touchscreen/sun4i-ts.c               | 140 ++++++++++++++++-----
18  2 files changed, 114 insertions(+), 31 deletions(-)
19
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
25  
26 +Optional properties:
27 + - allwinner,ts-attached: boolean indicating that an actual touchscreen is
28 +                         attached to the controller
29 +
30  Example:
31  
32         rtp: rtp@01c25000 {
33                 compatible = "allwinner,sun4i-ts";
34                 reg = <0x01c25000 0x100>;
35                 interrupts = <29>;
36 +               allwinner,ts-attached;
37         };
38 --- a/drivers/input/touchscreen/sun4i-ts.c
39 +++ b/drivers/input/touchscreen/sun4i-ts.c
40 @@ -3,6 +3,9 @@
41   *
42   * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
43   *
44 + * The hwmon parts are based on work by Corentin LABBE which is:
45 + * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
46 + *
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
50 @@ -30,6 +33,7 @@
51   */
52  
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 {
59         void __iomem *base;
60         unsigned int irq;
61         bool ignore_fifo_data;
62 +       int temp_data;
63  };
64  
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)
67  {
68 -       struct sun4i_ts_data *ts = dev_id;
69 -       u32 reg_val, x, y;
70 -
71 -       reg_val  = readl(ts->base + TP_INT_FIFOS);
72 +       u32 x, y;
73  
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);
79         }
80 +}
81 +
82 +static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
83 +{
84 +       struct sun4i_ts_data *ts = dev_id;
85 +       u32 reg_val;
86 +
87 +       reg_val  = readl(ts->base + TP_INT_FIFOS);
88 +
89 +       if (reg_val & TEMP_DATA_PENDING)
90 +               ts->temp_data = readl(ts->base + TEMP_DATA);
91 +
92 +       if (ts->input)
93 +               sun4i_ts_irq_handle_input(ts, reg_val);
94  
95         writel(reg_val, ts->base + TP_INT_FIFOS);
96  
97 @@ -149,9 +165,9 @@ static int sun4i_ts_open(struct input_de
98  {
99         struct sun4i_ts_data *ts = input_get_drvdata(dev);
100  
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);
107  
108         return 0;
109  }
110 @@ -160,15 +176,48 @@ static void sun4i_ts_close(struct input_
111  {
112         struct sun4i_ts_data *ts = input_get_drvdata(dev);
113  
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);
118 +}
119 +
120 +static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
121 +                        char *buf)
122 +{
123 +       struct sun4i_ts_data *ts = dev_get_drvdata(dev);
124 +
125 +       /* No temp_data until the first irq */
126 +       if (ts->temp_data == -1)
127 +               return -EAGAIN;
128 +
129 +       return sprintf(buf, "%d\n", (ts->temp_data - 1447) * 100);
130 +}
131 +
132 +static ssize_t show_temp_label(struct device *dev,
133 +                             struct device_attribute *devattr, char *buf)
134 +{
135 +       return sprintf(buf, "SoC temperature\n");
136  }
137  
138 +static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
139 +static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
140 +
141 +static struct attribute *sun4i_ts_attrs[] = {
142 +       &dev_attr_temp1_input.attr,
143 +       &dev_attr_temp1_label.attr,
144 +       NULL
145 +};
146 +ATTRIBUTE_GROUPS(sun4i_ts);
147 +
148  static int sun4i_ts_probe(struct platform_device *pdev)
149  {
150         struct sun4i_ts_data *ts;
151         struct device *dev = &pdev->dev;
152 +       struct device_node *np = dev->of_node;
153 +       struct device *hwmon;
154         int ret;
155 +       bool ts_attached;
156 +
157 +       ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
158  
159         ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
160         if (!ts)
161 @@ -176,24 +225,27 @@ static int sun4i_ts_probe(struct platfor
162  
163         ts->dev = dev;
164         ts->ignore_fifo_data = true;
165 +       ts->temp_data = -1;
166  
167 -       ts->input = devm_input_allocate_device(dev);
168 -       if (!ts->input)
169 -               return -ENOMEM;
170 -
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);
184 +       if (ts_attached) {
185 +               ts->input = devm_input_allocate_device(dev);
186 +               if (!ts->input)
187 +                       return -ENOMEM;
188 +
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);
202 +       }
203  
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);
209  
210 -       ret = input_register_device(ts->input);
211 -       if (ret)
212 -               return ret;
213 +       hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
214 +                                                      ts, sun4i_ts_groups);
215 +       if (IS_ERR(hwmon))
216 +               return PTR_ERR(hwmon);
217 +
218 +       writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
219 +
220 +       if (ts_attached) {
221 +               ret = input_register_device(ts->input);
222 +               if (ret) {
223 +                       writel(0, ts->base + TP_INT_FIFOC);
224 +                       return ret;
225 +               }
226 +       }
227  
228         platform_set_drvdata(pdev, ts);
229         return 0;
230  }
231  
232 +static int sun4i_ts_remove(struct platform_device *pdev)
233 +{
234 +       struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
235 +
236 +       /* Explicit unregister to avoid open/close changing the imask later */
237 +       if (ts->input)
238 +               input_unregister_device(ts->input);
239 +
240 +       /* Deactivate all IRQs */
241 +       writel(0, ts->base + TP_INT_FIFOC);
242 +
243 +       return 0;
244 +}
245 +
246  static const struct of_device_id sun4i_ts_of_match[] = {
247         { .compatible = "allwinner,sun4i-ts", },
248         { /* sentinel */ }
249 @@ -253,6 +330,7 @@ static struct platform_driver sun4i_ts_d
250                 .of_match_table = of_match_ptr(sun4i_ts_of_match),
251         },
252         .probe  = sun4i_ts_probe,
253 +       .remove = sun4i_ts_remove,
254  };
255  
256  module_platform_driver(sun4i_ts_driver);