142560c6da8a9fc102a4bcad49f96f03443319c7
[openwrt/staging/ldir.git] / target / linux / bcm27xx / patches-5.4 / 950-0771-w1_therm-optimizing-temperature-read-timings.patch
1 From 23769ad113407ceb21a4285a8194c7d788149269 Mon Sep 17 00:00:00 2001
2 From: Akira Shimahara <akira215corp@gmail.com>
3 Date: Mon, 11 May 2020 22:37:42 +0200
4 Subject: [PATCH] w1_therm: optimizing temperature read timings
5
6 commit 67b392f7b8edfa6f427fecd98722acab34c1c99f upstream.
7
8 Optimizing temperature reading by reducing waiting conversion time
9 according to device resolution settings, as per device specification.
10 This is device dependent as not all the devices supports resolution
11 setting, so it has been added in device family structures.
12
13 The process to read the temperature on the device has been adapted in a
14 new function 'convert_t()', which replace the former 'read_therm()', is
15 introduce to deal with this timing. Strong pull up is also applied during
16 the required time, according to device power status needs and
17 'strong_pullup' module parameter.
18
19 'temperature_from_RAM()' function is introduced to get the correct
20 temperature computation (device dependent) from device RAM data.
21
22 An new sysfs entry has been added to ouptut only temperature. The old
23 entry w1_slave has been kept for compatibility, without changing its
24 output format.
25
26 Updating Documentation/ABI/testing/sysfs-driver-w1_therm accordingly.
27
28 Signed-off-by: Akira Shimahara <akira215corp@gmail.com>
29 Link: https://lore.kernel.org/r/20200511203742.411039-1-akira215corp@gmail.com
30 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
31 ---
32 .../ABI/testing/sysfs-driver-w1_therm | 12 +
33 drivers/w1/slaves/w1_therm.c | 286 +++++++++++-------
34 2 files changed, 197 insertions(+), 101 deletions(-)
35
36 --- a/Documentation/ABI/testing/sysfs-driver-w1_therm
37 +++ b/Documentation/ABI/testing/sysfs-driver-w1_therm
38 @@ -41,6 +41,18 @@ Users: any user space application which
39 w1_term device
40
41
42 +What: /sys/bus/w1/devices/.../temperature
43 +Date: May 2020
44 +Contact: Akira Shimahara <akira215corp@gmail.com>
45 +Description:
46 + (RO) return the temperature in 1/1000 degC.
47 + Note that the conversion duration depend on the resolution (if
48 + device support this feature). It takes 94ms in 9bits
49 + resolution, 750ms for 12bits.
50 +Users: any user space application which wants to communicate with
51 + w1_term device
52 +
53 +
54 What: /sys/bus/w1/devices/.../w1_slave
55 Date: May 2020
56 Contact: Akira Shimahara <akira215corp@gmail.com>
57 --- a/drivers/w1/slaves/w1_therm.c
58 +++ b/drivers/w1/slaves/w1_therm.c
59 @@ -93,6 +93,7 @@ module_param_named(strong_pullup, w1_str
60 * @reserved: not used here
61 * @f: pointer to the device binding structure
62 * @convert: pointer to the device conversion function
63 + * @get_conversion_time: pointer to the device conversion time function
64 * @set_resolution: pointer to the device set_resolution function
65 * @get_resolution: pointer to the device get_resolution function
66 */
67 @@ -101,6 +102,7 @@ struct w1_therm_family_converter {
68 u16 reserved;
69 struct w1_family *f;
70 int (*convert)(u8 rom[9]);
71 + int (*get_conversion_time)(struct w1_slave *sl);
72 int (*set_resolution)(struct w1_slave *sl, int val);
73 int (*get_resolution)(struct w1_slave *sl);
74 };
75 @@ -154,6 +156,15 @@ struct therm_info {
76 static int reset_select_slave(struct w1_slave *sl);
77
78 /**
79 + * convert_t() - Query the device for temperature conversion and read
80 + * @sl: pointer to the slave to read
81 + * @info: pointer to a structure to store the read results
82 + *
83 + * Return: 0 if success, -kernel error code otherwise
84 + */
85 +static int convert_t(struct w1_slave *sl, struct therm_info *info);
86 +
87 +/**
88 * read_scratchpad() - read the data in device RAM
89 * @sl: pointer to the slave to read
90 * @info: pointer to a structure to store the read results
91 @@ -213,6 +224,9 @@ static ssize_t w1_slave_store(struct dev
92 static ssize_t w1_seq_show(struct device *device,
93 struct device_attribute *attr, char *buf);
94
95 +static ssize_t temperature_show(struct device *device,
96 + struct device_attribute *attr, char *buf);
97 +
98 static ssize_t ext_power_show(struct device *device,
99 struct device_attribute *attr, char *buf);
100
101 @@ -229,6 +243,7 @@ static ssize_t eeprom_store(struct devic
102
103 static DEVICE_ATTR_RW(w1_slave);
104 static DEVICE_ATTR_RO(w1_seq);
105 +static DEVICE_ATTR_RO(temperature);
106 static DEVICE_ATTR_RO(ext_power);
107 static DEVICE_ATTR_RW(resolution);
108 static DEVICE_ATTR_WO(eeprom);
109 @@ -259,6 +274,7 @@ static void w1_therm_remove_slave(struct
110
111 static struct attribute *w1_therm_attrs[] = {
112 &dev_attr_w1_slave.attr,
113 + &dev_attr_temperature.attr,
114 &dev_attr_ext_power.attr,
115 &dev_attr_resolution.attr,
116 &dev_attr_eeprom.attr,
117 @@ -267,6 +283,7 @@ static struct attribute *w1_therm_attrs[
118
119 static struct attribute *w1_ds18s20_attrs[] = {
120 &dev_attr_w1_slave.attr,
121 + &dev_attr_temperature.attr,
122 &dev_attr_ext_power.attr,
123 &dev_attr_eeprom.attr,
124 NULL,
125 @@ -275,6 +292,7 @@ static struct attribute *w1_ds18s20_attr
126 static struct attribute *w1_ds28ea00_attrs[] = {
127 &dev_attr_w1_slave.attr,
128 &dev_attr_w1_seq.attr,
129 + &dev_attr_temperature.attr,
130 &dev_attr_ext_power.attr,
131 &dev_attr_resolution.attr,
132 &dev_attr_eeprom.attr,
133 @@ -389,6 +407,37 @@ static struct w1_family w1_therm_family_
134
135 /* Device dependent func */
136
137 +static inline int w1_DS18B20_convert_time(struct w1_slave *sl)
138 +{
139 + int ret;
140 +
141 + if (!sl->family_data)
142 + return -ENODEV; /* device unknown */
143 +
144 + /* return time in ms for conversion operation */
145 + switch (SLAVE_RESOLUTION(sl)) {
146 + case 9:
147 + ret = 95;
148 + break;
149 + case 10:
150 + ret = 190;
151 + break;
152 + case 11:
153 + ret = 375;
154 + break;
155 + case 12:
156 + default:
157 + ret = 750;
158 + }
159 + return ret;
160 +}
161 +
162 +static inline int w1_DS18S20_convert_time(struct w1_slave *sl)
163 +{
164 + (void)(sl);
165 + return 750; /* always 750ms for DS18S20 */
166 +}
167 +
168 static inline int w1_DS18B20_write_data(struct w1_slave *sl,
169 const u8 *data)
170 {
171 @@ -480,8 +529,10 @@ static inline int w1_DS18S20_convert_tem
172 {
173 int t, h;
174
175 - if (!rom[7])
176 + if (!rom[7]) {
177 + pr_debug("%s: Invalid argument for conversion\n", __func__);
178 return 0;
179 + }
180
181 if (rom[1] == 0)
182 t = ((s32)rom[0] >> 1)*1000;
183 @@ -500,34 +551,39 @@ static inline int w1_DS18S20_convert_tem
184
185 static struct w1_therm_family_converter w1_therm_families[] = {
186 {
187 - .f = &w1_therm_family_DS18S20,
188 - .convert = w1_DS18S20_convert_temp,
189 - .set_resolution = NULL, /* no config register */
190 - .get_resolution = NULL, /* no config register */
191 + .f = &w1_therm_family_DS18S20,
192 + .convert = w1_DS18S20_convert_temp,
193 + .get_conversion_time = w1_DS18S20_convert_time,
194 + .set_resolution = NULL, /* no config register */
195 + .get_resolution = NULL, /* no config register */
196 },
197 {
198 - .f = &w1_therm_family_DS1822,
199 - .convert = w1_DS18B20_convert_temp,
200 - .set_resolution = w1_DS18B20_set_resolution,
201 - .get_resolution = w1_DS18B20_get_resolution,
202 + .f = &w1_therm_family_DS1822,
203 + .convert = w1_DS18B20_convert_temp,
204 + .get_conversion_time = w1_DS18B20_convert_time,
205 + .set_resolution = w1_DS18B20_set_resolution,
206 + .get_resolution = w1_DS18B20_get_resolution,
207 },
208 {
209 - .f = &w1_therm_family_DS18B20,
210 - .convert = w1_DS18B20_convert_temp,
211 - .set_resolution = w1_DS18B20_set_resolution,
212 - .get_resolution = w1_DS18B20_get_resolution,
213 + .f = &w1_therm_family_DS18B20,
214 + .convert = w1_DS18B20_convert_temp,
215 + .get_conversion_time = w1_DS18B20_convert_time,
216 + .set_resolution = w1_DS18B20_set_resolution,
217 + .get_resolution = w1_DS18B20_get_resolution,
218 },
219 {
220 - .f = &w1_therm_family_DS28EA00,
221 - .convert = w1_DS18B20_convert_temp,
222 - .set_resolution = w1_DS18B20_set_resolution,
223 - .get_resolution = w1_DS18B20_get_resolution,
224 + .f = &w1_therm_family_DS28EA00,
225 + .convert = w1_DS18B20_convert_temp,
226 + .get_conversion_time = w1_DS18B20_convert_time,
227 + .set_resolution = w1_DS18B20_set_resolution,
228 + .get_resolution = w1_DS18B20_get_resolution,
229 },
230 {
231 - .f = &w1_therm_family_DS1825,
232 - .convert = w1_DS18B20_convert_temp,
233 - .set_resolution = w1_DS18B20_set_resolution,
234 - .get_resolution = w1_DS18B20_get_resolution,
235 + .f = &w1_therm_family_DS1825,
236 + .convert = w1_DS18B20_convert_temp,
237 + .get_conversion_time = w1_DS18B20_convert_time,
238 + .set_resolution = w1_DS18B20_set_resolution,
239 + .get_resolution = w1_DS18B20_get_resolution,
240 }
241 };
242
243 @@ -582,24 +638,44 @@ static inline bool bus_mutex_lock(struct
244 }
245
246 /**
247 - * w1_convert_temp() - temperature conversion binding function
248 - * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
249 - * @fid: device family id
250 + * conversion_time() - get the Tconv for the slave
251 + * @sl: device to get the conversion time
252 *
253 - * The function call the temperature computation function according to
254 - * device family.
255 + * On device supporting resolution settings, conversion time depend
256 + * on the resolution setting. This helper function get the slave timing,
257 + * depending on its current setting.
258 *
259 - * Return: value in millidegrees Celsius.
260 + * Return: conversion time in ms, negative values are kernel error code
261 */
262 -static inline int w1_convert_temp(u8 rom[9], u8 fid)
263 +static inline int conversion_time(struct w1_slave *sl)
264 {
265 - int i;
266 + if (SLAVE_SPECIFIC_FUNC(sl))
267 + return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl);
268
269 - for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
270 - if (w1_therm_families[i].f->fid == fid)
271 - return w1_therm_families[i].convert(rom);
272 + dev_info(&sl->dev,
273 + "%s: Device not supported by the driver\n", __func__);
274
275 - return 0;
276 + return -ENODEV; /* No device family */
277 +}
278 +
279 +/**
280 + * temperature_from_RAM() - Convert the read info to temperature
281 + * @sl: device that sent the RAM data
282 + * @rom: read value on the slave device RAM
283 + *
284 + * Device dependent, the function bind the correct computation method.
285 + *
286 + * Return: temperature in 1/1000degC, 0 on error.
287 + */
288 +static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9])
289 +{
290 + if (SLAVE_SPECIFIC_FUNC(sl))
291 + return SLAVE_SPECIFIC_FUNC(sl)->convert(rom);
292 +
293 + dev_info(&sl->dev,
294 + "%s: Device not supported by the driver\n", __func__);
295 +
296 + return 0; /* No device family */
297 }
298
299 /* Interface Functions */
300 @@ -679,96 +755,74 @@ static int reset_select_slave(struct w1_
301 return 0;
302 }
303
304 -static ssize_t read_therm(struct device *device,
305 - struct w1_slave *sl, struct therm_info *info)
306 +static int convert_t(struct w1_slave *sl, struct therm_info *info)
307 {
308 - struct w1_master *dev = sl->master;
309 - u8 external_power;
310 - int ret, max_trying = 10;
311 - u8 *family_data = sl->family_data;
312 + struct w1_master *dev_master = sl->master;
313 + int max_trying = W1_THERM_MAX_TRY;
314 + int t_conv;
315 + int ret = -ENODEV;
316 + bool strong_pullup;
317
318 - if (!family_data) {
319 - ret = -ENODEV;
320 + if (!sl->family_data)
321 goto error;
322 - }
323
324 - /* prevent the slave from going away in sleep */
325 - atomic_inc(THERM_REFCNT(family_data));
326 + strong_pullup = (w1_strong_pullup == 2 ||
327 + (!SLAVE_POWERMODE(sl) &&
328 + w1_strong_pullup));
329
330 - ret = mutex_lock_interruptible(&dev->bus_mutex);
331 - if (ret != 0)
332 - goto dec_refcnt;
333 + /* get conversion duration device and id dependent */
334 + t_conv = conversion_time(sl);
335
336 memset(info->rom, 0, sizeof(info->rom));
337
338 - while (max_trying--) {
339 + /* prevent the slave from going away in sleep */
340 + atomic_inc(THERM_REFCNT(sl->family_data));
341 +
342 + if (!bus_mutex_lock(&dev_master->bus_mutex)) {
343 + ret = -EAGAIN; /* Didn't acquire the mutex */
344 + goto dec_refcnt;
345 + }
346 +
347 + while (max_trying-- && ret) { /* ret should be 0 */
348
349 info->verdict = 0;
350 info->crc = 0;
351 -
352 + /* safe version to select slave */
353 if (!reset_select_slave(sl)) {
354 - int count = 0;
355 - unsigned int tm = 750;
356 unsigned long sleep_rem;
357
358 - w1_write_8(dev, W1_READ_PSUPPLY);
359 - external_power = w1_read_8(dev);
360 -
361 - if (reset_select_slave(sl))
362 - continue;
363 -
364 /* 750ms strong pullup (or delay) after the convert */
365 - if (w1_strong_pullup == 2 ||
366 - (!external_power && w1_strong_pullup))
367 - w1_next_pullup(dev, tm);
368 -
369 - w1_write_8(dev, W1_CONVERT_TEMP);
370 + if (strong_pullup)
371 + w1_next_pullup(dev_master, t_conv);
372
373 - if (external_power) {
374 - mutex_unlock(&dev->bus_mutex);
375 + w1_write_8(dev_master, W1_CONVERT_TEMP);
376
377 - sleep_rem = msleep_interruptible(tm);
378 + if (strong_pullup) { /*some device need pullup */
379 + sleep_rem = msleep_interruptible(t_conv);
380 if (sleep_rem != 0) {
381 ret = -EINTR;
382 - goto dec_refcnt;
383 + goto mt_unlock;
384 }
385 + mutex_unlock(&dev_master->bus_mutex);
386 + } else { /*no device need pullup */
387 + mutex_unlock(&dev_master->bus_mutex);
388
389 - ret = mutex_lock_interruptible(&dev->bus_mutex);
390 - if (ret != 0)
391 - goto dec_refcnt;
392 - } else if (!w1_strong_pullup) {
393 - sleep_rem = msleep_interruptible(tm);
394 + sleep_rem = msleep_interruptible(t_conv);
395 if (sleep_rem != 0) {
396 ret = -EINTR;
397 - goto mt_unlock;
398 + goto dec_refcnt;
399 }
400 }
401 -
402 - if (!reset_select_slave(sl)) {
403 -
404 - w1_write_8(dev, W1_READ_SCRATCHPAD);
405 - count = w1_read_block(dev, info->rom, 9);
406 - if (count != 9) {
407 - dev_warn(device, "w1_read_block() "
408 - "returned %u instead of 9.\n",
409 - count);
410 - }
411 -
412 - info->crc = w1_calc_crc8(info->rom, 8);
413 -
414 - if (info->rom[8] == info->crc)
415 - info->verdict = 1;
416 - }
417 + ret = read_scratchpad(sl, info);
418 + goto dec_refcnt;
419 }
420
421 - if (info->verdict)
422 - break;
423 }
424
425 mt_unlock:
426 - mutex_unlock(&dev->bus_mutex);
427 + mutex_unlock(&dev_master->bus_mutex);
428 dec_refcnt:
429 - atomic_dec(THERM_REFCNT(family_data));
430 + atomic_dec(THERM_REFCNT(sl->family_data));
431 error:
432 return ret;
433 }
434 @@ -1000,27 +1054,33 @@ static ssize_t w1_slave_show(struct devi
435 u8 *family_data = sl->family_data;
436 int ret, i;
437 ssize_t c = PAGE_SIZE;
438 - u8 fid = sl->family->fid;
439
440 - ret = read_therm(device, sl, &info);
441 - if (ret)
442 - return ret;
443 + ret = convert_t(sl, &info);
444 +
445 + if (ret < 0) {
446 + dev_dbg(device,
447 + "%s: Temperature data may be corrupted. err=%d\n",
448 + __func__, ret);
449 + return 0;
450 + }
451
452 for (i = 0; i < 9; ++i)
453 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
454 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
455 info.crc, (info.verdict) ? "YES" : "NO");
456 +
457 if (info.verdict)
458 memcpy(family_data, info.rom, sizeof(info.rom));
459 else
460 - dev_warn(device, "Read failed CRC check\n");
461 + dev_warn(device, "%s:Read failed CRC check\n", __func__);
462
463 for (i = 0; i < 9; ++i)
464 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
465 ((u8 *)family_data)[i]);
466
467 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
468 - w1_convert_temp(info.rom, fid));
469 + temperature_from_RAM(sl, info.rom));
470 +
471 ret = PAGE_SIZE - c;
472 return ret;
473 }
474 @@ -1063,6 +1123,31 @@ static ssize_t w1_slave_store(struct dev
475 return size; /* always return size to avoid infinite calling */
476 }
477
478 +static ssize_t temperature_show(struct device *device,
479 + struct device_attribute *attr, char *buf)
480 +{
481 + struct w1_slave *sl = dev_to_w1_slave(device);
482 + struct therm_info info;
483 + int ret = 0;
484 +
485 + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
486 + dev_info(device,
487 + "%s: Device not supported by the driver\n", __func__);
488 + return 0; /* No device family */
489 + }
490 +
491 + ret = convert_t(sl, &info);
492 +
493 + if (ret < 0) {
494 + dev_dbg(device,
495 + "%s: Temperature data may be corrupted. err=%d\n",
496 + __func__, ret);
497 + return 0;
498 + }
499 +
500 + return sprintf(buf, "%d\n", temperature_from_RAM(sl, info.rom));
501 +}
502 +
503 static ssize_t ext_power_show(struct device *device,
504 struct device_attribute *attr, char *buf)
505 {
506 @@ -1172,12 +1257,11 @@ static int w1_read_temp(struct device *d
507 {
508 struct w1_slave *sl = dev_get_drvdata(device);
509 struct therm_info info;
510 - u8 fid = sl->family->fid;
511 int ret;
512
513 switch (attr) {
514 case hwmon_temp_input:
515 - ret = read_therm(device, sl, &info);
516 + ret = convert_t(sl, &info);
517 if (ret)
518 return ret;
519
520 @@ -1186,7 +1270,7 @@ static int w1_read_temp(struct device *d
521 return ret;
522 }
523
524 - *val = w1_convert_temp(info.rom, fid);
525 + *val = temperature_from_RAM(sl, info.rom);
526 ret = 0;
527 break;
528 default: