brcm2708: add linux 4.1 support
[openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0119-Merge-pull-request-1059-from-pelwell-rpi-4.0.y.patch
1 From 0dd4dae3071d135836946ab1b990061c0899e9b5 Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <pelwell@users.noreply.github.com>
3 Date: Mon, 13 Jul 2015 13:25:31 +0100
4 Subject: [PATCH 119/121] Merge pull request #1059 from pelwell/rpi-4.0.y
5
6 w1_therm: Back-port locking improvements from 4.2-rc1
7 ---
8  Documentation/ABI/stable/sysfs-driver-w1_ds28ea00 |   6 +
9  Documentation/w1/slaves/w1_therm                  |  11 +-
10  drivers/w1/slaves/w1_therm.c                      | 162 ++++++++++++++++++++--
11  3 files changed, 163 insertions(+), 16 deletions(-)
12  create mode 100644 Documentation/ABI/stable/sysfs-driver-w1_ds28ea00
13
14 --- /dev/null
15 +++ b/Documentation/ABI/stable/sysfs-driver-w1_ds28ea00
16 @@ -0,0 +1,6 @@
17 +What:          /sys/bus/w1/devices/.../w1_seq
18 +Date:          Apr 2015
19 +Contact:       Matt Campbell <mattrcampbell@gmail.com>
20 +Description:   Support for the DS28EA00 chain sequence function
21 +               see Documentation/w1/slaves/w1_therm for detailed information
22 +Users:         any user space application which wants to communicate with DS28EA00
23 --- a/Documentation/w1/slaves/w1_therm
24 +++ b/Documentation/w1/slaves/w1_therm
25 @@ -11,12 +11,14 @@ Author: Evgeniy Polyakov <johnpol@2ka.mi
26  Description
27  -----------
28  
29 -w1_therm provides basic temperature conversion for ds18*20 devices.
30 +w1_therm provides basic temperature conversion for ds18*20 devices, and the
31 +ds28ea00 device.
32  supported family codes:
33  W1_THERM_DS18S20       0x10
34  W1_THERM_DS1822                0x22
35  W1_THERM_DS18B20       0x28
36  W1_THERM_DS1825                0x3B
37 +W1_THERM_DS28EA00      0x42
38  
39  Support is provided through the sysfs w1_slave file.  Each open and
40  read sequence will initiate a temperature conversion then provide two
41 @@ -48,3 +50,10 @@ resistor).  The DS18b20 temperature sens
42  maximum current draw of 1.5mA and that a 5k pullup resistor is not
43  sufficient.  The strong pullup is designed to provide the additional
44  current required.
45 +
46 +The DS28EA00 provides an additional two pins for implementing a sequence
47 +detection algorithm.  This feature allows you to determine the physical
48 +location of the chip in the 1-wire bus without needing pre-existing
49 +knowledge of the bus ordering.  Support is provided through the sysfs
50 +w1_seq file.  The file will contain a single line with an integer value
51 +representing the device index in the bus starting at 0.
52 --- a/drivers/w1/slaves/w1_therm.c
53 +++ b/drivers/w1/slaves/w1_therm.c
54 @@ -59,16 +59,32 @@ MODULE_ALIAS("w1-family-" __stringify(W1
55  static int w1_strong_pullup = 1;
56  module_param_named(strong_pullup, w1_strong_pullup, int, 0);
57  
58 +struct w1_therm_family_data {
59 +       uint8_t rom[9];
60 +       atomic_t refcnt;
61 +};
62 +
63 +/* return the address of the refcnt in the family data */
64 +#define THERM_REFCNT(family_data) \
65 +       (&((struct w1_therm_family_data*)family_data)->refcnt)
66 +
67  static int w1_therm_add_slave(struct w1_slave *sl)
68  {
69 -       sl->family_data = kzalloc(9, GFP_KERNEL);
70 +       sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
71 +               GFP_KERNEL);
72         if (!sl->family_data)
73                 return -ENOMEM;
74 +       atomic_set(THERM_REFCNT(sl->family_data), 1);
75         return 0;
76  }
77  
78  static void w1_therm_remove_slave(struct w1_slave *sl)
79  {
80 +       int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
81 +       while(refcnt) {
82 +               msleep(1000);
83 +               refcnt = atomic_read(THERM_REFCNT(sl->family_data));
84 +       }
85         kfree(sl->family_data);
86         sl->family_data = NULL;
87  }
88 @@ -76,13 +92,24 @@ static void w1_therm_remove_slave(struct
89  static ssize_t w1_slave_show(struct device *device,
90         struct device_attribute *attr, char *buf);
91  
92 +static ssize_t w1_seq_show(struct device *device,
93 +       struct device_attribute *attr, char *buf);
94 +
95  static DEVICE_ATTR_RO(w1_slave);
96 +static DEVICE_ATTR_RO(w1_seq);
97  
98  static struct attribute *w1_therm_attrs[] = {
99         &dev_attr_w1_slave.attr,
100         NULL,
101  };
102 +
103 +static struct attribute *w1_ds28ea00_attrs[] = {
104 +       &dev_attr_w1_slave.attr,
105 +       &dev_attr_w1_seq.attr,
106 +       NULL,
107 +};
108  ATTRIBUTE_GROUPS(w1_therm);
109 +ATTRIBUTE_GROUPS(w1_ds28ea00);
110  
111  static struct w1_family_ops w1_therm_fops = {
112         .add_slave      = w1_therm_add_slave,
113 @@ -90,6 +117,12 @@ static struct w1_family_ops w1_therm_fop
114         .groups         = w1_therm_groups,
115  };
116  
117 +static struct w1_family_ops w1_ds28ea00_fops = {
118 +       .add_slave      = w1_therm_add_slave,
119 +       .remove_slave   = w1_therm_remove_slave,
120 +       .groups         = w1_ds28ea00_groups,
121 +};
122 +
123  static struct w1_family w1_therm_family_DS18S20 = {
124         .fid = W1_THERM_DS18S20,
125         .fops = &w1_therm_fops,
126 @@ -107,7 +140,7 @@ static struct w1_family w1_therm_family_
127  
128  static struct w1_family w1_therm_family_DS28EA00 = {
129         .fid = W1_THERM_DS28EA00,
130 -       .fops = &w1_therm_fops,
131 +       .fops = &w1_ds28ea00_fops,
132  };
133  
134  static struct w1_family w1_therm_family_DS1825 = {
135 @@ -194,13 +227,22 @@ static ssize_t w1_slave_show(struct devi
136         struct w1_slave *sl = dev_to_w1_slave(device);
137         struct w1_master *dev = sl->master;
138         u8 rom[9], crc, verdict, external_power;
139 -       int i, max_trying = 10;
140 +       int i, ret, max_trying = 10;
141         ssize_t c = PAGE_SIZE;
142 +       u8 *family_data = sl->family_data;
143  
144 -       i = mutex_lock_interruptible(&dev->bus_mutex);
145 -       if (i != 0)
146 -               return i;
147 +       ret = mutex_lock_interruptible(&dev->bus_mutex);
148 +       if (ret != 0)
149 +               goto post_unlock;
150 +
151 +       if(!sl->family_data)
152 +       {
153 +               ret = -ENODEV;
154 +               goto pre_unlock;
155 +       }
156  
157 +       /* prevent the slave from going away in sleep */
158 +       atomic_inc(THERM_REFCNT(family_data));
159         memset(rom, 0, sizeof(rom));
160  
161         while (max_trying--) {
162 @@ -230,17 +272,19 @@ static ssize_t w1_slave_show(struct devi
163                                 mutex_unlock(&dev->bus_mutex);
164  
165                                 sleep_rem = msleep_interruptible(tm);
166 -                               if (sleep_rem != 0)
167 -                                       return -EINTR;
168 +                               if (sleep_rem != 0) {
169 +                                       ret = -EINTR;
170 +                                       goto post_unlock;
171 +                               }
172  
173 -                               i = mutex_lock_interruptible(&dev->bus_mutex);
174 -                               if (i != 0)
175 -                                       return i;
176 +                               ret = mutex_lock_interruptible(&dev->bus_mutex);
177 +                               if (ret != 0)
178 +                                       goto post_unlock;
179                         } else if (!w1_strong_pullup) {
180                                 sleep_rem = msleep_interruptible(tm);
181                                 if (sleep_rem != 0) {
182 -                                       mutex_unlock(&dev->bus_mutex);
183 -                                       return -EINTR;
184 +                                       ret = -EINTR;
185 +                                       goto pre_unlock;
186                                 }
187                         }
188  
189 @@ -269,19 +313,107 @@ static ssize_t w1_slave_show(struct devi
190         c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
191                            crc, (verdict) ? "YES" : "NO");
192         if (verdict)
193 -               memcpy(sl->family_data, rom, sizeof(rom));
194 +               memcpy(family_data, rom, sizeof(rom));
195         else
196                 dev_warn(device, "Read failed CRC check\n");
197  
198         for (i = 0; i < 9; ++i)
199                 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
200 -                             ((u8 *)sl->family_data)[i]);
201 +                             ((u8 *)family_data)[i]);
202  
203         c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
204                 w1_convert_temp(rom, sl->family->fid));
205 +       ret = PAGE_SIZE - c;
206 +
207 +pre_unlock:
208         mutex_unlock(&dev->bus_mutex);
209  
210 +post_unlock:
211 +       atomic_dec(THERM_REFCNT(family_data));
212 +       return ret;
213 +}
214 +
215 +#define W1_42_CHAIN    0x99
216 +#define W1_42_CHAIN_OFF        0x3C
217 +#define W1_42_CHAIN_OFF_INV    0xC3
218 +#define W1_42_CHAIN_ON 0x5A
219 +#define W1_42_CHAIN_ON_INV     0xA5
220 +#define W1_42_CHAIN_DONE 0x96
221 +#define W1_42_CHAIN_DONE_INV 0x69
222 +#define W1_42_COND_READ        0x0F
223 +#define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
224 +#define W1_42_FINISHED_BYTE 0xFF
225 +static ssize_t w1_seq_show(struct device *device,
226 +       struct device_attribute *attr, char *buf)
227 +{
228 +       struct w1_slave *sl = dev_to_w1_slave(device);
229 +       ssize_t c = PAGE_SIZE;
230 +       int rv;
231 +       int i;
232 +       u8 ack;
233 +       u64 rn;
234 +       struct w1_reg_num *reg_num;
235 +       int seq = 0;
236 +
237 +       mutex_lock(&sl->master->bus_mutex);
238 +       /* Place all devices in CHAIN state */
239 +       if (w1_reset_bus(sl->master))
240 +               goto error;
241 +       w1_write_8(sl->master, W1_SKIP_ROM);
242 +       w1_write_8(sl->master, W1_42_CHAIN);
243 +       w1_write_8(sl->master, W1_42_CHAIN_ON);
244 +       w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
245 +       msleep(sl->master->pullup_duration);
246 +
247 +       /* check for acknowledgment */
248 +       ack = w1_read_8(sl->master);
249 +       if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
250 +               goto error;
251 +
252 +       /* In case the bus fails to send 0xFF, limit*/
253 +       for (i = 0; i <= 64; i++) {
254 +               if (w1_reset_bus(sl->master))
255 +                       goto error;
256 +
257 +               w1_write_8(sl->master, W1_42_COND_READ);
258 +               rv = w1_read_block(sl->master, (u8 *)&rn, 8);
259 +               reg_num = (struct w1_reg_num *) &rn;
260 +               if (reg_num->family == W1_42_FINISHED_BYTE)
261 +                       break;
262 +               if (sl->reg_num.id == reg_num->id)
263 +                       seq = i;
264 +
265 +               w1_write_8(sl->master, W1_42_CHAIN);
266 +               w1_write_8(sl->master, W1_42_CHAIN_DONE);
267 +               w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
268 +               w1_read_block(sl->master, &ack, sizeof(ack));
269 +
270 +               /* check for acknowledgment */
271 +               ack = w1_read_8(sl->master);
272 +               if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
273 +                       goto error;
274 +
275 +       }
276 +
277 +       /* Exit from CHAIN state */
278 +       if (w1_reset_bus(sl->master))
279 +               goto error;
280 +       w1_write_8(sl->master, W1_SKIP_ROM);
281 +       w1_write_8(sl->master, W1_42_CHAIN);
282 +       w1_write_8(sl->master, W1_42_CHAIN_OFF);
283 +       w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
284 +
285 +       /* check for acknowledgment */
286 +       ack = w1_read_8(sl->master);
287 +       if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
288 +               goto error;
289 +       mutex_unlock(&sl->master->bus_mutex);
290 +
291 +       c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
292         return PAGE_SIZE - c;
293 +error:
294 +       mutex_unlock(&sl->master->bus_mutex);
295 +       return -EIO;
296  }
297  
298  static int __init w1_therm_init(void)