1 From feffc2efed02adda00c6c5480292db012663e6e8 Mon Sep 17 00:00:00 2001
2 From: Akira Shimahara <akira215corp@gmail.com>
3 Date: Mon, 11 May 2020 22:37:08 +0200
4 Subject: [PATCH] w1_therm: adding resolution sysfs entry
6 commit 308bdb94de0c1abe7eac5193f58638b8aeaddf4b upstream.
8 Adding resolution sysfs entry (RW) to get or set the device resolution
9 Write values are managed as follow:
10 * '9..12': resolution to set in bit
11 * Anything else: do nothing
13 * '9..12': device resolution in bit
14 * '-xx': xx is kernel error when reading the resolution
16 Only supported devices will show the sysfs entry. A new family has been
17 created for DS18S20 devices as they do not implement resolution feature.
19 The resolution of each device is check when the device is
20 discover by the bus master, in 'w1_therm_add_slave(struct w1_slave *)'.
21 The status is stored in the device structure w1_therm_family_data so
22 that the driver always knows the resolution of each device, which could
23 be used later to determine the required conversion duration (resolution
26 The resolution is re evaluate each time a user read or write the sysfs
29 To avoid looping through the w1_therm_families at run time, the pointer
30 'specific_functions' is set up to the correct 'w1_therm_family_converter'
31 when the slave is added (which mean when it is discovered by the master).
32 This initialization is done by a helper function
33 'device_family(struct w1_slave *sl)', and a dedicated macro
34 'SLAVE_SPECIFIC_FUNC(sl)' allow the access to the specific function of the
37 'read_scratchpad' and 'write_scratchpad' are the hardware functions to
38 access the device RAM, as per protocol specification.
40 It cancel the former 'precision' functions, which was only set and never
41 read (so not stored in the device struct).
43 Updating Documentation/ABI/testing/sysfs-driver-w1_therm accordingly.
45 Signed-off-by: Akira Shimahara <akira215corp@gmail.com>
46 Link: https://lore.kernel.org/r/20200511203708.410649-1-akira215corp@gmail.com
47 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
49 .../ABI/testing/sysfs-driver-w1_therm | 17 +
50 drivers/w1/slaves/w1_therm.c | 442 ++++++++++++++----
51 2 files changed, 361 insertions(+), 98 deletions(-)
53 --- a/Documentation/ABI/testing/sysfs-driver-w1_therm
54 +++ b/Documentation/ABI/testing/sysfs-driver-w1_therm
55 @@ -10,6 +10,23 @@ Users: any user space application which
59 +What: /sys/bus/w1/devices/.../resolution
61 +Contact: Akira Shimahara <akira215corp@gmail.com>
63 + (RW) get or set the device resolution (on supported devices,
64 + if not, this entry is not present). Note that the resolution
65 + will be changed only in device RAM, so it will be cleared when
66 + power is lost. Trigger a 'save' to EEPROM command to keep
67 + values after power-on. Read or write are :
68 + * '9..12': device resolution in bit
69 + or resolution to set in bit
70 + * '-xx': xx is kernel error when reading the resolution
71 + * Anything else: do nothing
72 +Users: any user space application which wants to communicate with
76 What: /sys/bus/w1/devices/.../w1_slave
78 Contact: Akira Shimahara <akira215corp@gmail.com>
79 --- a/drivers/w1/slaves/w1_therm.c
80 +++ b/drivers/w1/slaves/w1_therm.c
81 @@ -52,12 +52,26 @@ module_param_named(strong_pullup, w1_str
85 + * return a pointer on the slave w1_therm_family_converter struct:
86 + * always test family data existence before using this macro
88 +#define SLAVE_SPECIFIC_FUNC(sl) \
89 + (((struct w1_therm_family_data *)(sl->family_data))->specific_functions)
92 * return the power mode of the sl slave : 1-ext, 0-parasite, <0 unknown
93 * always test family data existence before using this macro
95 #define SLAVE_POWERMODE(sl) \
96 (((struct w1_therm_family_data *)(sl->family_data))->external_powered)
99 + * return the resolution in bit of the sl slave : <0 unknown
100 + * always test family data existence before using this macro
102 +#define SLAVE_RESOLUTION(sl) \
103 + (((struct w1_therm_family_data *)(sl->family_data))->resolution)
105 /* return the address of the refcnt in the family data */
106 #define THERM_REFCNT(family_data) \
107 (&((struct w1_therm_family_data *)family_data)->refcnt)
108 @@ -70,7 +84,8 @@ module_param_named(strong_pullup, w1_str
109 * @reserved: not used here
110 * @f: pointer to the device binding structure
111 * @convert: pointer to the device conversion function
112 - * @precision: pointer to the device precision function
113 + * @set_resolution: pointer to the device set_resolution function
114 + * @get_resolution: pointer to the device get_resolution function
115 * @eeprom: pointer to eeprom function
117 struct w1_therm_family_converter {
118 @@ -78,7 +93,8 @@ struct w1_therm_family_converter {
121 int (*convert)(u8 rom[9]);
122 - int (*precision)(struct device *device, int val);
123 + int (*set_resolution)(struct w1_slave *sl, int val);
124 + int (*get_resolution)(struct w1_slave *sl);
125 int (*eeprom)(struct device *device);
128 @@ -89,11 +105,15 @@ struct w1_therm_family_converter {
129 * @external_powered: 1 device powered externally,
130 * 0 device parasite powered,
131 * -x error or undefined
132 + * @resolution: current device resolution
133 + * @specific_functions: pointer to struct of device specific function
135 struct w1_therm_family_data {
138 int external_powered;
140 + struct w1_therm_family_converter *specific_functions;
144 @@ -127,6 +147,25 @@ struct therm_info {
145 static int reset_select_slave(struct w1_slave *sl);
148 + * read_scratchpad() - read the data in device RAM
149 + * @sl: pointer to the slave to read
150 + * @info: pointer to a structure to store the read results
152 + * Return: 0 if success, -kernel error code otherwise
154 +static int read_scratchpad(struct w1_slave *sl, struct therm_info *info);
157 + * write_scratchpad() - write nb_bytes in the device RAM
158 + * @sl: pointer to the slave to write in
159 + * @data: pointer to an array of 3 bytes, as 3 bytes MUST be written
160 + * @nb_bytes: number of bytes to be written (2 for DS18S20, 3 otherwise)
162 + * Return: 0 if success, -kernel error code otherwise
164 +static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes);
167 * read_powermode() - Query the power mode of the slave
168 * @sl: slave to retrieve the power mode
170 @@ -154,11 +193,18 @@ static ssize_t w1_seq_show(struct device
171 static ssize_t ext_power_show(struct device *device,
172 struct device_attribute *attr, char *buf);
174 +static ssize_t resolution_show(struct device *device,
175 + struct device_attribute *attr, char *buf);
177 +static ssize_t resolution_store(struct device *device,
178 + struct device_attribute *attr, const char *buf, size_t size);
180 /* Attributes declarations */
182 static DEVICE_ATTR_RW(w1_slave);
183 static DEVICE_ATTR_RO(w1_seq);
184 static DEVICE_ATTR_RO(ext_power);
185 +static DEVICE_ATTR_RW(resolution);
187 /* Interface Functions declaration */
189 @@ -187,6 +233,13 @@ static void w1_therm_remove_slave(struct
190 static struct attribute *w1_therm_attrs[] = {
191 &dev_attr_w1_slave.attr,
192 &dev_attr_ext_power.attr,
193 + &dev_attr_resolution.attr,
197 +static struct attribute *w1_ds18s20_attrs[] = {
198 + &dev_attr_w1_slave.attr,
199 + &dev_attr_ext_power.attr,
203 @@ -194,12 +247,14 @@ static struct attribute *w1_ds28ea00_att
204 &dev_attr_w1_slave.attr,
205 &dev_attr_w1_seq.attr,
206 &dev_attr_ext_power.attr,
207 + &dev_attr_resolution.attr,
211 /* Attribute groups */
213 ATTRIBUTE_GROUPS(w1_therm);
214 +ATTRIBUTE_GROUPS(w1_ds18s20);
215 ATTRIBUTE_GROUPS(w1_ds28ea00);
217 #if IS_REACHABLE(CONFIG_HWMON)
218 @@ -261,6 +316,13 @@ static struct w1_family_ops w1_therm_fop
219 .chip_info = W1_CHIPINFO,
222 +static struct w1_family_ops w1_ds18s20_fops = {
223 + .add_slave = w1_therm_add_slave,
224 + .remove_slave = w1_therm_remove_slave,
225 + .groups = w1_ds18s20_groups,
226 + .chip_info = W1_CHIPINFO,
229 static struct w1_family_ops w1_ds28ea00_fops = {
230 .add_slave = w1_therm_add_slave,
231 .remove_slave = w1_therm_remove_slave,
232 @@ -272,7 +334,7 @@ static struct w1_family_ops w1_ds28ea00_
234 static struct w1_family w1_therm_family_DS18S20 = {
235 .fid = W1_THERM_DS18S20,
236 - .fops = &w1_therm_fops,
237 + .fops = &w1_ds18s20_fops,
240 static struct w1_family w1_therm_family_DS18B20 = {
241 @@ -300,92 +362,67 @@ static struct w1_family w1_therm_family_
242 /* write configuration to eeprom */
243 static inline int w1_therm_eeprom(struct device *device);
245 -/* DS18S20 does not feature configuration register */
246 -static inline int w1_DS18S20_precision(struct device *device, int val)
247 +static inline int w1_DS18B20_write_data(struct w1_slave *sl,
251 + return write_scratchpad(sl, data, 3);
254 -/* Set precision for conversion */
255 -static inline int w1_DS18B20_precision(struct device *device, int val)
256 +static inline int w1_DS18S20_write_data(struct w1_slave *sl,
259 - struct w1_slave *sl = dev_to_w1_slave(device);
260 - struct w1_master *dev = sl->master;
262 - int ret, max_trying = 10;
263 - u8 *family_data = sl->family_data;
264 - uint8_t precision_bits;
265 - uint8_t mask = 0x60;
267 - if (val > 12 || val < 9) {
268 - pr_warn("Unsupported precision\n");
273 - if (!sl->family_data) {
278 - /* prevent the slave from going away in sleep */
279 - atomic_inc(THERM_REFCNT(family_data));
280 + /* No config register */
281 + return write_scratchpad(sl, data, 2);
284 - ret = mutex_lock_interruptible(&dev->bus_mutex);
287 +static inline int w1_DS18B20_set_resolution(struct w1_slave *sl, int val)
290 + u8 new_config_register[3]; /* array of data to be written */
291 + struct therm_info info;
293 - memset(rom, 0, sizeof(rom));
294 + /* resolution of DS18B20 is in the range [9..12] bits */
295 + if (val < 9 || val > 12)
298 + val -= 9; /* soustract 9 the lowest resolution in bit */
299 + val = (val << 5); /* shift to position bit 5 & bit 6 */
302 + * Read the scratchpad to change only the required bits
303 + * (bit5 & bit 6 from byte 4)
305 + ret = read_scratchpad(sl, &info);
307 + new_config_register[0] = info.rom[2];
308 + new_config_register[1] = info.rom[3];
309 + /* config register is byte 4 & mask 0b10011111*/
310 + new_config_register[2] = (info.rom[4] & 0x9F) |
315 - /* translate precision to bitmask (see datasheet page 9) */
318 - precision_bits = 0x00;
321 - precision_bits = 0x20;
324 - precision_bits = 0x40;
328 - precision_bits = 0x60;
331 + /* Write data in the device RAM */
332 + ret = w1_DS18B20_write_data(sl, new_config_register);
334 - while (max_trying--) {
339 - if (!reset_select_slave(sl)) {
341 +static inline int w1_DS18B20_get_resolution(struct w1_slave *sl)
344 + u8 config_register;
345 + struct therm_info info;
347 - /* read values to only alter precision bits */
348 - w1_write_8(dev, W1_READ_SCRATCHPAD);
349 - count = w1_read_block(dev, rom, 9);
351 - dev_warn(device, "w1_read_block() returned %u instead of 9.\n", count);
353 - crc = w1_calc_crc8(rom, 8);
354 - if (rom[8] == crc) {
355 - rom[4] = (rom[4] & ~mask) | (precision_bits & mask);
357 - if (!reset_select_slave(sl)) {
358 - w1_write_8(dev, W1_WRITE_SCRATCHPAD);
359 - w1_write_8(dev, rom[2]);
360 - w1_write_8(dev, rom[3]);
361 - w1_write_8(dev, rom[4]);
362 + ret = read_scratchpad(sl, &info);
369 + config_register = info.rom[4]; /* config register is byte 4 */
370 + config_register &= 0x60; /* 0b01100000 keep only bit 5 & 6 */
371 + config_register = (config_register >> 5); /* shift */
372 + config_register += 9; /* add 9 the lowest resolution in bit */
373 + ret = (int) config_register;
376 - mutex_unlock(&dev->bus_mutex);
378 - atomic_dec(THERM_REFCNT(family_data));
383 @@ -438,31 +475,36 @@ static struct w1_therm_family_converter
385 .f = &w1_therm_family_DS18S20,
386 .convert = w1_DS18S20_convert_temp,
387 - .precision = w1_DS18S20_precision,
388 + .set_resolution = NULL, /* no config register */
389 + .get_resolution = NULL, /* no config register */
390 .eeprom = w1_therm_eeprom
393 .f = &w1_therm_family_DS1822,
394 .convert = w1_DS18B20_convert_temp,
395 - .precision = w1_DS18S20_precision,
396 + .set_resolution = w1_DS18B20_set_resolution,
397 + .get_resolution = w1_DS18B20_get_resolution,
398 .eeprom = w1_therm_eeprom
401 .f = &w1_therm_family_DS18B20,
402 .convert = w1_DS18B20_convert_temp,
403 - .precision = w1_DS18B20_precision,
404 + .set_resolution = w1_DS18B20_set_resolution,
405 + .get_resolution = w1_DS18B20_get_resolution,
406 .eeprom = w1_therm_eeprom
409 .f = &w1_therm_family_DS28EA00,
410 .convert = w1_DS18B20_convert_temp,
411 - .precision = w1_DS18S20_precision,
412 + .set_resolution = w1_DS18B20_set_resolution,
413 + .get_resolution = w1_DS18B20_get_resolution,
414 .eeprom = w1_therm_eeprom
417 .f = &w1_therm_family_DS1825,
418 .convert = w1_DS18B20_convert_temp,
419 - .precision = w1_DS18S20_precision,
420 + .set_resolution = w1_DS18B20_set_resolution,
421 + .get_resolution = w1_DS18B20_get_resolution,
422 .eeprom = w1_therm_eeprom
425 @@ -470,6 +512,26 @@ static struct w1_therm_family_converter
426 /* Helpers Functions */
429 + * device_family() - Retrieve a pointer on &struct w1_therm_family_converter
430 + * @sl: slave to retrieve the device specific structure
432 + * Return: pointer to the slaves's family converter, NULL if not known
434 +static struct w1_therm_family_converter *device_family(struct w1_slave *sl)
436 + struct w1_therm_family_converter *ret = NULL;
439 + for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
440 + if (w1_therm_families[i].f->fid == sl->family->fid) {
441 + ret = &w1_therm_families[i];
449 * bus_mutex_lock() - Acquire the mutex
450 * @lock: w1 bus mutex to acquire
452 @@ -522,6 +584,9 @@ static inline int w1_convert_temp(u8 rom
454 static int w1_therm_add_slave(struct w1_slave *sl)
456 + struct w1_therm_family_converter *sl_family_conv;
458 + /* Allocate memory */
459 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
461 if (!sl->family_data)
462 @@ -529,6 +594,15 @@ static int w1_therm_add_slave(struct w1_
464 atomic_set(THERM_REFCNT(sl->family_data), 1);
466 + /* Get a pointer to the device specific function struct */
467 + sl_family_conv = device_family(sl);
468 + if (!sl_family_conv) {
469 + kfree(sl->family_data);
472 + /* save this pointer to the device structure */
473 + SLAVE_SPECIFIC_FUNC(sl) = sl_family_conv;
475 /* Getting the power mode of the device {external, parasite} */
476 SLAVE_POWERMODE(sl) = read_powermode(sl);
478 @@ -539,6 +613,18 @@ static int w1_therm_add_slave(struct w1_
479 __func__, SLAVE_POWERMODE(sl));
482 + /* Getting the resolution of the device */
483 + if (SLAVE_SPECIFIC_FUNC(sl)->get_resolution) {
484 + SLAVE_RESOLUTION(sl) =
485 + SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
486 + if (SLAVE_RESOLUTION(sl) < 0) {
487 + /* no error returned as device has been added */
489 + "%s:Device has been added, but resolution may be corrupted. err=%d\n",
490 + __func__, SLAVE_RESOLUTION(sl));
497 @@ -665,6 +751,93 @@ error:
501 +static int read_scratchpad(struct w1_slave *sl, struct therm_info *info)
503 + struct w1_master *dev_master = sl->master;
504 + int max_trying = W1_THERM_MAX_TRY;
509 + if (!sl->family_data)
512 + memset(info->rom, 0, sizeof(info->rom));
514 + /* prevent the slave from going away in sleep */
515 + atomic_inc(THERM_REFCNT(sl->family_data));
517 + if (!bus_mutex_lock(&dev_master->bus_mutex)) {
518 + ret = -EAGAIN; /* Didn't acquire the mutex */
522 + while (max_trying-- && ret) { /* ret should be 0 */
523 + /* safe version to select slave */
524 + if (!reset_select_slave(sl)) {
527 + w1_write_8(dev_master, W1_READ_SCRATCHPAD);
529 + nb_bytes_read = w1_read_block(dev_master, info->rom, 9);
530 + if (nb_bytes_read != 9) {
532 + "w1_read_block(): returned %u instead of 9.\n",
537 + info->crc = w1_calc_crc8(info->rom, 8);
539 + if (info->rom[8] == info->crc) {
543 + ret = -EIO; /* CRC not checked */
547 + mutex_unlock(&dev_master->bus_mutex);
550 + atomic_dec(THERM_REFCNT(sl->family_data));
555 +static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes)
557 + struct w1_master *dev_master = sl->master;
558 + int max_trying = W1_THERM_MAX_TRY;
561 + if (!sl->family_data)
564 + /* prevent the slave from going away in sleep */
565 + atomic_inc(THERM_REFCNT(sl->family_data));
567 + if (!bus_mutex_lock(&dev_master->bus_mutex)) {
568 + ret = -EAGAIN; /* Didn't acquire the mutex */
572 + while (max_trying-- && ret) { /* ret should be 0 */
573 + /* safe version to select slave */
574 + if (!reset_select_slave(sl)) {
575 + w1_write_8(dev_master, W1_WRITE_SCRATCHPAD);
576 + w1_write_block(dev_master, data, nb_bytes);
580 + mutex_unlock(&dev_master->bus_mutex);
583 + atomic_dec(THERM_REFCNT(sl->family_data));
588 static inline int w1_therm_eeprom(struct device *device)
590 struct w1_slave *sl = dev_to_w1_slave(device);
591 @@ -815,26 +988,38 @@ static ssize_t w1_slave_store(struct dev
592 struct device_attribute *attr, const char *buf,
597 struct w1_slave *sl = dev_to_w1_slave(device);
600 - ret = kstrtoint(buf, 0, &val);
603 + ret = kstrtoint(buf, 10, &val); /* converting user entry to int */
605 - for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
606 - if (w1_therm_families[i].f->fid == sl->family->fid) {
607 - /* zero value indicates to write current configuration to eeprom */
609 - ret = w1_therm_families[i].eeprom(device);
611 - ret = w1_therm_families[i].precision(device,
615 + if (ret) { /* conversion error */
617 + "%s: conversion error. err= %d\n", __func__, ret);
618 + return size; /* return size to avoid call back again */
621 + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
623 + "%s: Device not supported by the driver\n", __func__);
624 + return size; /* No device family */
627 + if (val == 0) /* val=0 : trigger a EEPROM save */
628 + ret = SLAVE_SPECIFIC_FUNC(sl)->eeprom(device);
630 + if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution)
631 + ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
633 - return ret ? : size;
637 + "%s: writing error %d\n", __func__, ret);
638 + /* return size to avoid call back again */
640 + SLAVE_RESOLUTION(sl) = val;
642 + return size; /* always return size to avoid infinite calling */
645 static ssize_t ext_power_show(struct device *device,
646 @@ -859,6 +1044,67 @@ static ssize_t ext_power_show(struct dev
647 return sprintf(buf, "%d\n", SLAVE_POWERMODE(sl));
650 +static ssize_t resolution_show(struct device *device,
651 + struct device_attribute *attr, char *buf)
653 + struct w1_slave *sl = dev_to_w1_slave(device);
655 + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
657 + "%s: Device not supported by the driver\n", __func__);
658 + return 0; /* No device family */
661 + /* get the correct function depending on the device */
662 + SLAVE_RESOLUTION(sl) = SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
663 + if (SLAVE_RESOLUTION(sl) < 0) {
665 + "%s: Resolution may be corrupted. err=%d\n",
666 + __func__, SLAVE_RESOLUTION(sl));
669 + return sprintf(buf, "%d\n", SLAVE_RESOLUTION(sl));
672 +static ssize_t resolution_store(struct device *device,
673 + struct device_attribute *attr, const char *buf, size_t size)
675 + struct w1_slave *sl = dev_to_w1_slave(device);
679 + ret = kstrtoint(buf, 10, &val); /* converting user entry to int */
681 + if (ret) { /* conversion error */
683 + "%s: conversion error. err= %d\n", __func__, ret);
684 + return size; /* return size to avoid call back again */
687 + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
689 + "%s: Device not supported by the driver\n", __func__);
690 + return size; /* No device family */
694 + * Don't deal with the val enterd by user,
695 + * only device knows what is correct or not
698 + /* get the correct function depending on the device */
699 + ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
703 + "%s: writing error %d\n", __func__, ret);
704 + /* return size to avoid call back again */
706 + SLAVE_RESOLUTION(sl) = val;
711 #if IS_REACHABLE(CONFIG_HWMON)
712 static int w1_read_temp(struct device *device, u32 attr, int channel,