| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * w1_therm.c |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net> |
|---|
| 5 | | - * |
|---|
| 6 | | - * |
|---|
| 7 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 8 | | - * it under the therms of the GNU General Public License as published by |
|---|
| 9 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | | - * (at your option) any later version. |
|---|
| 11 | | - * |
|---|
| 12 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 13 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | | - * GNU General Public License for more details. |
|---|
| 16 | | - * |
|---|
| 17 | | - * You should have received a copy of the GNU General Public License |
|---|
| 18 | | - * along with this program; if not, write to the Free Software |
|---|
| 19 | | - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | 6 | */ |
|---|
| 21 | 7 | |
|---|
| 22 | 8 | #include <asm/types.h> |
|---|
| .. | .. |
|---|
| 30 | 16 | #include <linux/slab.h> |
|---|
| 31 | 17 | #include <linux/delay.h> |
|---|
| 32 | 18 | #include <linux/hwmon.h> |
|---|
| 19 | +#include <linux/string.h> |
|---|
| 20 | +#include <linux/jiffies.h> |
|---|
| 33 | 21 | |
|---|
| 34 | 22 | #include <linux/w1.h> |
|---|
| 35 | 23 | |
|---|
| .. | .. |
|---|
| 39 | 27 | #define W1_THERM_DS1825 0x3B |
|---|
| 40 | 28 | #define W1_THERM_DS28EA00 0x42 |
|---|
| 41 | 29 | |
|---|
| 42 | | -/* Allow the strong pullup to be disabled, but default to enabled. |
|---|
| 30 | +/* |
|---|
| 31 | + * Allow the strong pullup to be disabled, but default to enabled. |
|---|
| 43 | 32 | * If it was disabled a parasite powered device might not get the require |
|---|
| 44 | 33 | * current to do a temperature conversion. If it is enabled parasite powered |
|---|
| 45 | 34 | * devices have a better chance of getting the current required. |
|---|
| .. | .. |
|---|
| 55 | 44 | static int w1_strong_pullup = 1; |
|---|
| 56 | 45 | module_param_named(strong_pullup, w1_strong_pullup, int, 0); |
|---|
| 57 | 46 | |
|---|
| 47 | +/* Counter for devices supporting bulk reading */ |
|---|
| 48 | +static u16 bulk_read_device_counter; /* =0 as per C standard */ |
|---|
| 49 | + |
|---|
| 50 | +/* This command should be in public header w1.h but is not */ |
|---|
| 51 | +#define W1_RECALL_EEPROM 0xB8 |
|---|
| 52 | + |
|---|
| 53 | +/* Nb of try for an operation */ |
|---|
| 54 | +#define W1_THERM_MAX_TRY 5 |
|---|
| 55 | + |
|---|
| 56 | +/* ms delay to retry bus mutex */ |
|---|
| 57 | +#define W1_THERM_RETRY_DELAY 20 |
|---|
| 58 | + |
|---|
| 59 | +/* delay in ms to write in EEPROM */ |
|---|
| 60 | +#define W1_THERM_EEPROM_WRITE_DELAY 10 |
|---|
| 61 | + |
|---|
| 62 | +#define EEPROM_CMD_WRITE "save" /* cmd for write eeprom sysfs */ |
|---|
| 63 | +#define EEPROM_CMD_READ "restore" /* cmd for read eeprom sysfs */ |
|---|
| 64 | +#define BULK_TRIGGER_CMD "trigger" /* cmd to trigger a bulk read */ |
|---|
| 65 | + |
|---|
| 66 | +#define MIN_TEMP -55 /* min temperature that can be mesured */ |
|---|
| 67 | +#define MAX_TEMP 125 /* max temperature that can be mesured */ |
|---|
| 68 | + |
|---|
| 69 | +/* Allowed values for sysfs conv_time attribute */ |
|---|
| 70 | +#define CONV_TIME_DEFAULT 0 |
|---|
| 71 | +#define CONV_TIME_MEASURE 1 |
|---|
| 72 | + |
|---|
| 73 | +/* Bits in sysfs "features" value */ |
|---|
| 74 | +#define W1_THERM_CHECK_RESULT 1 /* Enable conversion success check */ |
|---|
| 75 | +#define W1_THERM_POLL_COMPLETION 2 /* Poll for conversion completion */ |
|---|
| 76 | +#define W1_THERM_FEATURES_MASK 3 /* All values mask */ |
|---|
| 77 | + |
|---|
| 78 | +/* Poll period in milliseconds. Should be less then a shortest operation on the device */ |
|---|
| 79 | +#define W1_POLL_PERIOD 32 |
|---|
| 80 | +#define W1_POLL_CONVERT_TEMP 2000 /* Timeout for W1_CONVERT_TEMP, ms */ |
|---|
| 81 | +#define W1_POLL_RECALL_EEPROM 500 /* Timeout for W1_RECALL_EEPROM, ms*/ |
|---|
| 82 | + |
|---|
| 83 | +/* Masks for resolution functions, work with all devices */ |
|---|
| 84 | +/* Bit mask for config register for all devices, bits 7,6,5 */ |
|---|
| 85 | +#define W1_THERM_RESOLUTION_MASK 0xE0 |
|---|
| 86 | +/* Bit offset of resolution in config register for all devices */ |
|---|
| 87 | +#define W1_THERM_RESOLUTION_SHIFT 5 |
|---|
| 88 | +/* Bit offset of resolution in config register for all devices */ |
|---|
| 89 | +#define W1_THERM_RESOLUTION_SHIFT 5 |
|---|
| 90 | +/* Add this to bit value to get resolution */ |
|---|
| 91 | +#define W1_THERM_RESOLUTION_MIN 9 |
|---|
| 92 | +/* Maximum allowed value */ |
|---|
| 93 | +#define W1_THERM_RESOLUTION_MAX 14 |
|---|
| 94 | + |
|---|
| 95 | +/* Helpers Macros */ |
|---|
| 96 | + |
|---|
| 97 | +/* |
|---|
| 98 | + * return a pointer on the slave w1_therm_family_converter struct: |
|---|
| 99 | + * always test family data existence before using this macro |
|---|
| 100 | + */ |
|---|
| 101 | +#define SLAVE_SPECIFIC_FUNC(sl) \ |
|---|
| 102 | + (((struct w1_therm_family_data *)(sl->family_data))->specific_functions) |
|---|
| 103 | + |
|---|
| 104 | +/* |
|---|
| 105 | + * return the power mode of the sl slave : 1-ext, 0-parasite, <0 unknown |
|---|
| 106 | + * always test family data existence before using this macro |
|---|
| 107 | + */ |
|---|
| 108 | +#define SLAVE_POWERMODE(sl) \ |
|---|
| 109 | + (((struct w1_therm_family_data *)(sl->family_data))->external_powered) |
|---|
| 110 | + |
|---|
| 111 | +/* |
|---|
| 112 | + * return the resolution in bit of the sl slave : <0 unknown |
|---|
| 113 | + * always test family data existence before using this macro |
|---|
| 114 | + */ |
|---|
| 115 | +#define SLAVE_RESOLUTION(sl) \ |
|---|
| 116 | + (((struct w1_therm_family_data *)(sl->family_data))->resolution) |
|---|
| 117 | + |
|---|
| 118 | +/* |
|---|
| 119 | + * return the conv_time_override of the sl slave |
|---|
| 120 | + * always test family data existence before using this macro |
|---|
| 121 | + */ |
|---|
| 122 | + #define SLAVE_CONV_TIME_OVERRIDE(sl) \ |
|---|
| 123 | + (((struct w1_therm_family_data *)(sl->family_data))->conv_time_override) |
|---|
| 124 | + |
|---|
| 125 | +/* |
|---|
| 126 | + * return the features of the sl slave |
|---|
| 127 | + * always test family data existence before using this macro |
|---|
| 128 | + */ |
|---|
| 129 | + #define SLAVE_FEATURES(sl) \ |
|---|
| 130 | + (((struct w1_therm_family_data *)(sl->family_data))->features) |
|---|
| 131 | + |
|---|
| 132 | +/* |
|---|
| 133 | + * return whether or not a converT command has been issued to the slave |
|---|
| 134 | + * * 0: no bulk read is pending |
|---|
| 135 | + * * -1: conversion is in progress |
|---|
| 136 | + * * 1: conversion done, result to be read |
|---|
| 137 | + */ |
|---|
| 138 | +#define SLAVE_CONVERT_TRIGGERED(sl) \ |
|---|
| 139 | + (((struct w1_therm_family_data *)(sl->family_data))->convert_triggered) |
|---|
| 140 | + |
|---|
| 141 | +/* return the address of the refcnt in the family data */ |
|---|
| 142 | +#define THERM_REFCNT(family_data) \ |
|---|
| 143 | + (&((struct w1_therm_family_data *)family_data)->refcnt) |
|---|
| 144 | + |
|---|
| 145 | +/* Structs definition */ |
|---|
| 146 | + |
|---|
| 147 | +/** |
|---|
| 148 | + * struct w1_therm_family_converter - bind device specific functions |
|---|
| 149 | + * @broken: flag for non-registred families |
|---|
| 150 | + * @reserved: not used here |
|---|
| 151 | + * @f: pointer to the device binding structure |
|---|
| 152 | + * @convert: pointer to the device conversion function |
|---|
| 153 | + * @get_conversion_time: pointer to the device conversion time function |
|---|
| 154 | + * @set_resolution: pointer to the device set_resolution function |
|---|
| 155 | + * @get_resolution: pointer to the device get_resolution function |
|---|
| 156 | + * @write_data: pointer to the device writing function (2 or 3 bytes) |
|---|
| 157 | + * @bulk_read: true if device family support bulk read, false otherwise |
|---|
| 158 | + */ |
|---|
| 159 | +struct w1_therm_family_converter { |
|---|
| 160 | + u8 broken; |
|---|
| 161 | + u16 reserved; |
|---|
| 162 | + struct w1_family *f; |
|---|
| 163 | + int (*convert)(u8 rom[9]); |
|---|
| 164 | + int (*get_conversion_time)(struct w1_slave *sl); |
|---|
| 165 | + int (*set_resolution)(struct w1_slave *sl, int val); |
|---|
| 166 | + int (*get_resolution)(struct w1_slave *sl); |
|---|
| 167 | + int (*write_data)(struct w1_slave *sl, const u8 *data); |
|---|
| 168 | + bool bulk_read; |
|---|
| 169 | +}; |
|---|
| 170 | + |
|---|
| 171 | +/** |
|---|
| 172 | + * struct w1_therm_family_data - device data |
|---|
| 173 | + * @rom: ROM device id (64bit Lasered ROM code + 1 CRC byte) |
|---|
| 174 | + * @refcnt: ref count |
|---|
| 175 | + * @external_powered: 1 device powered externally, |
|---|
| 176 | + * 0 device parasite powered, |
|---|
| 177 | + * -x error or undefined |
|---|
| 178 | + * @resolution: current device resolution |
|---|
| 179 | + * @convert_triggered: conversion state of the device |
|---|
| 180 | + * @conv_time_override: user selected conversion time or CONV_TIME_DEFAULT |
|---|
| 181 | + * @features: bit mask - enable temperature validity check, poll for completion |
|---|
| 182 | + * @specific_functions: pointer to struct of device specific function |
|---|
| 183 | + */ |
|---|
| 58 | 184 | struct w1_therm_family_data { |
|---|
| 59 | 185 | uint8_t rom[9]; |
|---|
| 60 | 186 | atomic_t refcnt; |
|---|
| 187 | + int external_powered; |
|---|
| 188 | + int resolution; |
|---|
| 189 | + int convert_triggered; |
|---|
| 190 | + int conv_time_override; |
|---|
| 191 | + unsigned int features; |
|---|
| 192 | + struct w1_therm_family_converter *specific_functions; |
|---|
| 61 | 193 | }; |
|---|
| 62 | 194 | |
|---|
| 195 | +/** |
|---|
| 196 | + * struct therm_info - store temperature reading |
|---|
| 197 | + * @rom: read device data (8 data bytes + 1 CRC byte) |
|---|
| 198 | + * @crc: computed crc from rom |
|---|
| 199 | + * @verdict: 1 crc checked, 0 crc not matching |
|---|
| 200 | + */ |
|---|
| 63 | 201 | struct therm_info { |
|---|
| 64 | 202 | u8 rom[9]; |
|---|
| 65 | 203 | u8 crc; |
|---|
| 66 | 204 | u8 verdict; |
|---|
| 67 | 205 | }; |
|---|
| 68 | 206 | |
|---|
| 69 | | -/* return the address of the refcnt in the family data */ |
|---|
| 70 | | -#define THERM_REFCNT(family_data) \ |
|---|
| 71 | | - (&((struct w1_therm_family_data *)family_data)->refcnt) |
|---|
| 207 | +/* Hardware Functions declaration */ |
|---|
| 72 | 208 | |
|---|
| 73 | | -static int w1_therm_add_slave(struct w1_slave *sl) |
|---|
| 74 | | -{ |
|---|
| 75 | | - sl->family_data = kzalloc(sizeof(struct w1_therm_family_data), |
|---|
| 76 | | - GFP_KERNEL); |
|---|
| 77 | | - if (!sl->family_data) |
|---|
| 78 | | - return -ENOMEM; |
|---|
| 79 | | - atomic_set(THERM_REFCNT(sl->family_data), 1); |
|---|
| 80 | | - return 0; |
|---|
| 81 | | -} |
|---|
| 209 | +/** |
|---|
| 210 | + * reset_select_slave() - reset and select a slave |
|---|
| 211 | + * @sl: the slave to select |
|---|
| 212 | + * |
|---|
| 213 | + * Resets the bus and select the slave by sending a ROM MATCH cmd |
|---|
| 214 | + * w1_reset_select_slave() from w1_io.c could not be used here because |
|---|
| 215 | + * it sent a SKIP ROM command if only one device is on the line. |
|---|
| 216 | + * At the beginning of the such process, sl->master->slave_count is 1 even if |
|---|
| 217 | + * more devices are on the line, causing collision on the line. |
|---|
| 218 | + * |
|---|
| 219 | + * Context: The w1 master lock must be held. |
|---|
| 220 | + * |
|---|
| 221 | + * Return: 0 if success, negative kernel error code otherwise. |
|---|
| 222 | + */ |
|---|
| 223 | +static int reset_select_slave(struct w1_slave *sl); |
|---|
| 82 | 224 | |
|---|
| 83 | | -static void w1_therm_remove_slave(struct w1_slave *sl) |
|---|
| 84 | | -{ |
|---|
| 85 | | - int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data)); |
|---|
| 225 | +/** |
|---|
| 226 | + * convert_t() - Query the device for temperature conversion and read |
|---|
| 227 | + * @sl: pointer to the slave to read |
|---|
| 228 | + * @info: pointer to a structure to store the read results |
|---|
| 229 | + * |
|---|
| 230 | + * Return: 0 if success, -kernel error code otherwise |
|---|
| 231 | + */ |
|---|
| 232 | +static int convert_t(struct w1_slave *sl, struct therm_info *info); |
|---|
| 86 | 233 | |
|---|
| 87 | | - while (refcnt) { |
|---|
| 88 | | - msleep(1000); |
|---|
| 89 | | - refcnt = atomic_read(THERM_REFCNT(sl->family_data)); |
|---|
| 90 | | - } |
|---|
| 91 | | - kfree(sl->family_data); |
|---|
| 92 | | - sl->family_data = NULL; |
|---|
| 93 | | -} |
|---|
| 234 | +/** |
|---|
| 235 | + * read_scratchpad() - read the data in device RAM |
|---|
| 236 | + * @sl: pointer to the slave to read |
|---|
| 237 | + * @info: pointer to a structure to store the read results |
|---|
| 238 | + * |
|---|
| 239 | + * Return: 0 if success, -kernel error code otherwise |
|---|
| 240 | + */ |
|---|
| 241 | +static int read_scratchpad(struct w1_slave *sl, struct therm_info *info); |
|---|
| 242 | + |
|---|
| 243 | +/** |
|---|
| 244 | + * write_scratchpad() - write nb_bytes in the device RAM |
|---|
| 245 | + * @sl: pointer to the slave to write in |
|---|
| 246 | + * @data: pointer to an array of 3 bytes, as 3 bytes MUST be written |
|---|
| 247 | + * @nb_bytes: number of bytes to be written (2 for DS18S20, 3 otherwise) |
|---|
| 248 | + * |
|---|
| 249 | + * Return: 0 if success, -kernel error code otherwise |
|---|
| 250 | + */ |
|---|
| 251 | +static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes); |
|---|
| 252 | + |
|---|
| 253 | +/** |
|---|
| 254 | + * copy_scratchpad() - Copy the content of scratchpad in device EEPROM |
|---|
| 255 | + * @sl: slave involved |
|---|
| 256 | + * |
|---|
| 257 | + * Return: 0 if success, -kernel error code otherwise |
|---|
| 258 | + */ |
|---|
| 259 | +static int copy_scratchpad(struct w1_slave *sl); |
|---|
| 260 | + |
|---|
| 261 | +/** |
|---|
| 262 | + * recall_eeprom() - Restore EEPROM data to device RAM |
|---|
| 263 | + * @sl: slave involved |
|---|
| 264 | + * |
|---|
| 265 | + * Return: 0 if success, -kernel error code otherwise |
|---|
| 266 | + */ |
|---|
| 267 | +static int recall_eeprom(struct w1_slave *sl); |
|---|
| 268 | + |
|---|
| 269 | +/** |
|---|
| 270 | + * read_powermode() - Query the power mode of the slave |
|---|
| 271 | + * @sl: slave to retrieve the power mode |
|---|
| 272 | + * |
|---|
| 273 | + * Ask the device to get its power mode (external or parasite) |
|---|
| 274 | + * and store the power status in the &struct w1_therm_family_data. |
|---|
| 275 | + * |
|---|
| 276 | + * Return: |
|---|
| 277 | + * * 0 parasite powered device |
|---|
| 278 | + * * 1 externally powered device |
|---|
| 279 | + * * <0 kernel error code |
|---|
| 280 | + */ |
|---|
| 281 | +static int read_powermode(struct w1_slave *sl); |
|---|
| 282 | + |
|---|
| 283 | +/** |
|---|
| 284 | + * trigger_bulk_read() - function to trigger a bulk read on the bus |
|---|
| 285 | + * @dev_master: the device master of the bus |
|---|
| 286 | + * |
|---|
| 287 | + * Send a SKIP ROM follow by a CONVERT T commmand on the bus. |
|---|
| 288 | + * It also set the status flag in each slave &struct w1_therm_family_data |
|---|
| 289 | + * to signal that a conversion is in progress. |
|---|
| 290 | + * |
|---|
| 291 | + * Return: 0 if success, -kernel error code otherwise |
|---|
| 292 | + */ |
|---|
| 293 | +static int trigger_bulk_read(struct w1_master *dev_master); |
|---|
| 294 | + |
|---|
| 295 | +/* Sysfs interface declaration */ |
|---|
| 94 | 296 | |
|---|
| 95 | 297 | static ssize_t w1_slave_show(struct device *device, |
|---|
| 96 | 298 | struct device_attribute *attr, char *buf); |
|---|
| .. | .. |
|---|
| 101 | 303 | static ssize_t w1_seq_show(struct device *device, |
|---|
| 102 | 304 | struct device_attribute *attr, char *buf); |
|---|
| 103 | 305 | |
|---|
| 306 | +static ssize_t temperature_show(struct device *device, |
|---|
| 307 | + struct device_attribute *attr, char *buf); |
|---|
| 308 | + |
|---|
| 309 | +static ssize_t ext_power_show(struct device *device, |
|---|
| 310 | + struct device_attribute *attr, char *buf); |
|---|
| 311 | + |
|---|
| 312 | +static ssize_t resolution_show(struct device *device, |
|---|
| 313 | + struct device_attribute *attr, char *buf); |
|---|
| 314 | + |
|---|
| 315 | +static ssize_t resolution_store(struct device *device, |
|---|
| 316 | + struct device_attribute *attr, const char *buf, size_t size); |
|---|
| 317 | + |
|---|
| 318 | +static ssize_t eeprom_store(struct device *device, |
|---|
| 319 | + struct device_attribute *attr, const char *buf, size_t size); |
|---|
| 320 | + |
|---|
| 321 | +static ssize_t alarms_store(struct device *device, |
|---|
| 322 | + struct device_attribute *attr, const char *buf, size_t size); |
|---|
| 323 | + |
|---|
| 324 | +static ssize_t alarms_show(struct device *device, |
|---|
| 325 | + struct device_attribute *attr, char *buf); |
|---|
| 326 | + |
|---|
| 327 | +static ssize_t therm_bulk_read_store(struct device *device, |
|---|
| 328 | + struct device_attribute *attr, const char *buf, size_t size); |
|---|
| 329 | + |
|---|
| 330 | +static ssize_t therm_bulk_read_show(struct device *device, |
|---|
| 331 | + struct device_attribute *attr, char *buf); |
|---|
| 332 | + |
|---|
| 333 | +static ssize_t conv_time_show(struct device *device, |
|---|
| 334 | + struct device_attribute *attr, char *buf); |
|---|
| 335 | + |
|---|
| 336 | +static ssize_t conv_time_store(struct device *device, |
|---|
| 337 | + struct device_attribute *attr, const char *buf, |
|---|
| 338 | + size_t size); |
|---|
| 339 | + |
|---|
| 340 | +static ssize_t features_show(struct device *device, |
|---|
| 341 | + struct device_attribute *attr, char *buf); |
|---|
| 342 | + |
|---|
| 343 | +static ssize_t features_store(struct device *device, |
|---|
| 344 | + struct device_attribute *attr, const char *buf, |
|---|
| 345 | + size_t size); |
|---|
| 346 | +/* Attributes declarations */ |
|---|
| 347 | + |
|---|
| 104 | 348 | static DEVICE_ATTR_RW(w1_slave); |
|---|
| 105 | 349 | static DEVICE_ATTR_RO(w1_seq); |
|---|
| 350 | +static DEVICE_ATTR_RO(temperature); |
|---|
| 351 | +static DEVICE_ATTR_RO(ext_power); |
|---|
| 352 | +static DEVICE_ATTR_RW(resolution); |
|---|
| 353 | +static DEVICE_ATTR_WO(eeprom); |
|---|
| 354 | +static DEVICE_ATTR_RW(alarms); |
|---|
| 355 | +static DEVICE_ATTR_RW(conv_time); |
|---|
| 356 | +static DEVICE_ATTR_RW(features); |
|---|
| 357 | + |
|---|
| 358 | +static DEVICE_ATTR_RW(therm_bulk_read); /* attribut at master level */ |
|---|
| 359 | + |
|---|
| 360 | +/* Interface Functions declaration */ |
|---|
| 361 | + |
|---|
| 362 | +/** |
|---|
| 363 | + * w1_therm_add_slave() - Called when a new slave is discovered |
|---|
| 364 | + * @sl: slave just discovered by the master. |
|---|
| 365 | + * |
|---|
| 366 | + * Called by the master when the slave is discovered on the bus. Used to |
|---|
| 367 | + * initialize slave state before the beginning of any communication. |
|---|
| 368 | + * |
|---|
| 369 | + * Return: 0 - If success, negative kernel code otherwise |
|---|
| 370 | + */ |
|---|
| 371 | +static int w1_therm_add_slave(struct w1_slave *sl); |
|---|
| 372 | + |
|---|
| 373 | +/** |
|---|
| 374 | + * w1_therm_remove_slave() - Called when a slave is removed |
|---|
| 375 | + * @sl: slave to be removed. |
|---|
| 376 | + * |
|---|
| 377 | + * Called by the master when the slave is considered not to be on the bus |
|---|
| 378 | + * anymore. Used to free memory. |
|---|
| 379 | + */ |
|---|
| 380 | +static void w1_therm_remove_slave(struct w1_slave *sl); |
|---|
| 381 | + |
|---|
| 382 | +/* Family attributes */ |
|---|
| 106 | 383 | |
|---|
| 107 | 384 | static struct attribute *w1_therm_attrs[] = { |
|---|
| 108 | 385 | &dev_attr_w1_slave.attr, |
|---|
| 386 | + &dev_attr_temperature.attr, |
|---|
| 387 | + &dev_attr_ext_power.attr, |
|---|
| 388 | + &dev_attr_resolution.attr, |
|---|
| 389 | + &dev_attr_eeprom.attr, |
|---|
| 390 | + &dev_attr_alarms.attr, |
|---|
| 391 | + &dev_attr_conv_time.attr, |
|---|
| 392 | + &dev_attr_features.attr, |
|---|
| 393 | + NULL, |
|---|
| 394 | +}; |
|---|
| 395 | + |
|---|
| 396 | +static struct attribute *w1_ds18s20_attrs[] = { |
|---|
| 397 | + &dev_attr_w1_slave.attr, |
|---|
| 398 | + &dev_attr_temperature.attr, |
|---|
| 399 | + &dev_attr_ext_power.attr, |
|---|
| 400 | + &dev_attr_eeprom.attr, |
|---|
| 401 | + &dev_attr_alarms.attr, |
|---|
| 402 | + &dev_attr_conv_time.attr, |
|---|
| 403 | + &dev_attr_features.attr, |
|---|
| 109 | 404 | NULL, |
|---|
| 110 | 405 | }; |
|---|
| 111 | 406 | |
|---|
| 112 | 407 | static struct attribute *w1_ds28ea00_attrs[] = { |
|---|
| 113 | 408 | &dev_attr_w1_slave.attr, |
|---|
| 114 | 409 | &dev_attr_w1_seq.attr, |
|---|
| 410 | + &dev_attr_temperature.attr, |
|---|
| 411 | + &dev_attr_ext_power.attr, |
|---|
| 412 | + &dev_attr_resolution.attr, |
|---|
| 413 | + &dev_attr_eeprom.attr, |
|---|
| 414 | + &dev_attr_alarms.attr, |
|---|
| 415 | + &dev_attr_conv_time.attr, |
|---|
| 416 | + &dev_attr_features.attr, |
|---|
| 115 | 417 | NULL, |
|---|
| 116 | 418 | }; |
|---|
| 117 | 419 | |
|---|
| 420 | +/* Attribute groups */ |
|---|
| 421 | + |
|---|
| 118 | 422 | ATTRIBUTE_GROUPS(w1_therm); |
|---|
| 423 | +ATTRIBUTE_GROUPS(w1_ds18s20); |
|---|
| 119 | 424 | ATTRIBUTE_GROUPS(w1_ds28ea00); |
|---|
| 120 | 425 | |
|---|
| 121 | 426 | #if IS_REACHABLE(CONFIG_HWMON) |
|---|
| .. | .. |
|---|
| 168 | 473 | #define W1_CHIPINFO NULL |
|---|
| 169 | 474 | #endif |
|---|
| 170 | 475 | |
|---|
| 171 | | -static struct w1_family_ops w1_therm_fops = { |
|---|
| 476 | +/* Family operations */ |
|---|
| 477 | + |
|---|
| 478 | +static const struct w1_family_ops w1_therm_fops = { |
|---|
| 172 | 479 | .add_slave = w1_therm_add_slave, |
|---|
| 173 | 480 | .remove_slave = w1_therm_remove_slave, |
|---|
| 174 | 481 | .groups = w1_therm_groups, |
|---|
| 175 | 482 | .chip_info = W1_CHIPINFO, |
|---|
| 176 | 483 | }; |
|---|
| 177 | 484 | |
|---|
| 178 | | -static struct w1_family_ops w1_ds28ea00_fops = { |
|---|
| 485 | +static const struct w1_family_ops w1_ds18s20_fops = { |
|---|
| 486 | + .add_slave = w1_therm_add_slave, |
|---|
| 487 | + .remove_slave = w1_therm_remove_slave, |
|---|
| 488 | + .groups = w1_ds18s20_groups, |
|---|
| 489 | + .chip_info = W1_CHIPINFO, |
|---|
| 490 | +}; |
|---|
| 491 | + |
|---|
| 492 | +static const struct w1_family_ops w1_ds28ea00_fops = { |
|---|
| 179 | 493 | .add_slave = w1_therm_add_slave, |
|---|
| 180 | 494 | .remove_slave = w1_therm_remove_slave, |
|---|
| 181 | 495 | .groups = w1_ds28ea00_groups, |
|---|
| 182 | 496 | .chip_info = W1_CHIPINFO, |
|---|
| 183 | 497 | }; |
|---|
| 184 | 498 | |
|---|
| 499 | +/* Family binding operations struct */ |
|---|
| 500 | + |
|---|
| 185 | 501 | static struct w1_family w1_therm_family_DS18S20 = { |
|---|
| 186 | 502 | .fid = W1_THERM_DS18S20, |
|---|
| 187 | | - .fops = &w1_therm_fops, |
|---|
| 503 | + .fops = &w1_ds18s20_fops, |
|---|
| 188 | 504 | }; |
|---|
| 189 | 505 | |
|---|
| 190 | 506 | static struct w1_family w1_therm_family_DS18B20 = { |
|---|
| .. | .. |
|---|
| 207 | 523 | .fops = &w1_therm_fops, |
|---|
| 208 | 524 | }; |
|---|
| 209 | 525 | |
|---|
| 210 | | -struct w1_therm_family_converter { |
|---|
| 211 | | - u8 broken; |
|---|
| 212 | | - u16 reserved; |
|---|
| 213 | | - struct w1_family *f; |
|---|
| 214 | | - int (*convert)(u8 rom[9]); |
|---|
| 215 | | - int (*precision)(struct device *device, int val); |
|---|
| 216 | | - int (*eeprom)(struct device *device); |
|---|
| 217 | | -}; |
|---|
| 526 | +/* Device dependent func */ |
|---|
| 218 | 527 | |
|---|
| 219 | | -/* write configuration to eeprom */ |
|---|
| 220 | | -static inline int w1_therm_eeprom(struct device *device); |
|---|
| 221 | | - |
|---|
| 222 | | -/* Set precision for conversion */ |
|---|
| 223 | | -static inline int w1_DS18B20_precision(struct device *device, int val); |
|---|
| 224 | | -static inline int w1_DS18S20_precision(struct device *device, int val); |
|---|
| 225 | | - |
|---|
| 226 | | -/* The return value is millidegrees Centigrade. */ |
|---|
| 227 | | -static inline int w1_DS18B20_convert_temp(u8 rom[9]); |
|---|
| 228 | | -static inline int w1_DS18S20_convert_temp(u8 rom[9]); |
|---|
| 229 | | - |
|---|
| 230 | | -static struct w1_therm_family_converter w1_therm_families[] = { |
|---|
| 231 | | - { |
|---|
| 232 | | - .f = &w1_therm_family_DS18S20, |
|---|
| 233 | | - .convert = w1_DS18S20_convert_temp, |
|---|
| 234 | | - .precision = w1_DS18S20_precision, |
|---|
| 235 | | - .eeprom = w1_therm_eeprom |
|---|
| 236 | | - }, |
|---|
| 237 | | - { |
|---|
| 238 | | - .f = &w1_therm_family_DS1822, |
|---|
| 239 | | - .convert = w1_DS18B20_convert_temp, |
|---|
| 240 | | - .precision = w1_DS18S20_precision, |
|---|
| 241 | | - .eeprom = w1_therm_eeprom |
|---|
| 242 | | - }, |
|---|
| 243 | | - { |
|---|
| 244 | | - .f = &w1_therm_family_DS18B20, |
|---|
| 245 | | - .convert = w1_DS18B20_convert_temp, |
|---|
| 246 | | - .precision = w1_DS18B20_precision, |
|---|
| 247 | | - .eeprom = w1_therm_eeprom |
|---|
| 248 | | - }, |
|---|
| 249 | | - { |
|---|
| 250 | | - .f = &w1_therm_family_DS28EA00, |
|---|
| 251 | | - .convert = w1_DS18B20_convert_temp, |
|---|
| 252 | | - .precision = w1_DS18S20_precision, |
|---|
| 253 | | - .eeprom = w1_therm_eeprom |
|---|
| 254 | | - }, |
|---|
| 255 | | - { |
|---|
| 256 | | - .f = &w1_therm_family_DS1825, |
|---|
| 257 | | - .convert = w1_DS18B20_convert_temp, |
|---|
| 258 | | - .precision = w1_DS18S20_precision, |
|---|
| 259 | | - .eeprom = w1_therm_eeprom |
|---|
| 260 | | - } |
|---|
| 261 | | -}; |
|---|
| 262 | | - |
|---|
| 263 | | -static inline int w1_therm_eeprom(struct device *device) |
|---|
| 528 | +static inline int w1_DS18B20_convert_time(struct w1_slave *sl) |
|---|
| 264 | 529 | { |
|---|
| 265 | | - struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 266 | | - struct w1_master *dev = sl->master; |
|---|
| 267 | | - u8 rom[9], external_power; |
|---|
| 268 | | - int ret, max_trying = 10; |
|---|
| 269 | | - u8 *family_data = sl->family_data; |
|---|
| 530 | + int ret; |
|---|
| 270 | 531 | |
|---|
| 271 | | - if (!sl->family_data) { |
|---|
| 272 | | - ret = -ENODEV; |
|---|
| 273 | | - goto error; |
|---|
| 274 | | - } |
|---|
| 532 | + if (!sl->family_data) |
|---|
| 533 | + return -ENODEV; /* device unknown */ |
|---|
| 275 | 534 | |
|---|
| 276 | | - /* prevent the slave from going away in sleep */ |
|---|
| 277 | | - atomic_inc(THERM_REFCNT(family_data)); |
|---|
| 535 | + if (SLAVE_CONV_TIME_OVERRIDE(sl) != CONV_TIME_DEFAULT) |
|---|
| 536 | + return SLAVE_CONV_TIME_OVERRIDE(sl); |
|---|
| 278 | 537 | |
|---|
| 279 | | - ret = mutex_lock_interruptible(&dev->bus_mutex); |
|---|
| 280 | | - if (ret != 0) |
|---|
| 281 | | - goto dec_refcnt; |
|---|
| 282 | | - |
|---|
| 283 | | - memset(rom, 0, sizeof(rom)); |
|---|
| 284 | | - |
|---|
| 285 | | - while (max_trying--) { |
|---|
| 286 | | - if (!w1_reset_select_slave(sl)) { |
|---|
| 287 | | - unsigned int tm = 10; |
|---|
| 288 | | - unsigned long sleep_rem; |
|---|
| 289 | | - |
|---|
| 290 | | - /* check if in parasite mode */ |
|---|
| 291 | | - w1_write_8(dev, W1_READ_PSUPPLY); |
|---|
| 292 | | - external_power = w1_read_8(dev); |
|---|
| 293 | | - |
|---|
| 294 | | - if (w1_reset_select_slave(sl)) |
|---|
| 295 | | - continue; |
|---|
| 296 | | - |
|---|
| 297 | | - /* 10ms strong pullup/delay after the copy command */ |
|---|
| 298 | | - if (w1_strong_pullup == 2 || |
|---|
| 299 | | - (!external_power && w1_strong_pullup)) |
|---|
| 300 | | - w1_next_pullup(dev, tm); |
|---|
| 301 | | - |
|---|
| 302 | | - w1_write_8(dev, W1_COPY_SCRATCHPAD); |
|---|
| 303 | | - |
|---|
| 304 | | - if (external_power) { |
|---|
| 305 | | - mutex_unlock(&dev->bus_mutex); |
|---|
| 306 | | - |
|---|
| 307 | | - sleep_rem = msleep_interruptible(tm); |
|---|
| 308 | | - if (sleep_rem != 0) { |
|---|
| 309 | | - ret = -EINTR; |
|---|
| 310 | | - goto dec_refcnt; |
|---|
| 311 | | - } |
|---|
| 312 | | - |
|---|
| 313 | | - ret = mutex_lock_interruptible(&dev->bus_mutex); |
|---|
| 314 | | - if (ret != 0) |
|---|
| 315 | | - goto dec_refcnt; |
|---|
| 316 | | - } else if (!w1_strong_pullup) { |
|---|
| 317 | | - sleep_rem = msleep_interruptible(tm); |
|---|
| 318 | | - if (sleep_rem != 0) { |
|---|
| 319 | | - ret = -EINTR; |
|---|
| 320 | | - goto mt_unlock; |
|---|
| 321 | | - } |
|---|
| 322 | | - } |
|---|
| 323 | | - |
|---|
| 324 | | - break; |
|---|
| 325 | | - } |
|---|
| 326 | | - } |
|---|
| 327 | | - |
|---|
| 328 | | -mt_unlock: |
|---|
| 329 | | - mutex_unlock(&dev->bus_mutex); |
|---|
| 330 | | -dec_refcnt: |
|---|
| 331 | | - atomic_dec(THERM_REFCNT(family_data)); |
|---|
| 332 | | -error: |
|---|
| 333 | | - return ret; |
|---|
| 334 | | -} |
|---|
| 335 | | - |
|---|
| 336 | | -/* DS18S20 does not feature configuration register */ |
|---|
| 337 | | -static inline int w1_DS18S20_precision(struct device *device, int val) |
|---|
| 338 | | -{ |
|---|
| 339 | | - return 0; |
|---|
| 340 | | -} |
|---|
| 341 | | - |
|---|
| 342 | | -static inline int w1_DS18B20_precision(struct device *device, int val) |
|---|
| 343 | | -{ |
|---|
| 344 | | - struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 345 | | - struct w1_master *dev = sl->master; |
|---|
| 346 | | - u8 rom[9], crc; |
|---|
| 347 | | - int ret, max_trying = 10; |
|---|
| 348 | | - u8 *family_data = sl->family_data; |
|---|
| 349 | | - uint8_t precision_bits; |
|---|
| 350 | | - uint8_t mask = 0x60; |
|---|
| 351 | | - |
|---|
| 352 | | - if (val > 12 || val < 9) { |
|---|
| 353 | | - pr_warn("Unsupported precision\n"); |
|---|
| 354 | | - ret = -EINVAL; |
|---|
| 355 | | - goto error; |
|---|
| 356 | | - } |
|---|
| 357 | | - |
|---|
| 358 | | - if (!sl->family_data) { |
|---|
| 359 | | - ret = -ENODEV; |
|---|
| 360 | | - goto error; |
|---|
| 361 | | - } |
|---|
| 362 | | - |
|---|
| 363 | | - /* prevent the slave from going away in sleep */ |
|---|
| 364 | | - atomic_inc(THERM_REFCNT(family_data)); |
|---|
| 365 | | - |
|---|
| 366 | | - ret = mutex_lock_interruptible(&dev->bus_mutex); |
|---|
| 367 | | - if (ret != 0) |
|---|
| 368 | | - goto dec_refcnt; |
|---|
| 369 | | - |
|---|
| 370 | | - memset(rom, 0, sizeof(rom)); |
|---|
| 371 | | - |
|---|
| 372 | | - /* translate precision to bitmask (see datasheet page 9) */ |
|---|
| 373 | | - switch (val) { |
|---|
| 538 | + /* Return the conversion time, depending on resolution, |
|---|
| 539 | + * select maximum conversion time among all compatible devices |
|---|
| 540 | + */ |
|---|
| 541 | + switch (SLAVE_RESOLUTION(sl)) { |
|---|
| 374 | 542 | case 9: |
|---|
| 375 | | - precision_bits = 0x00; |
|---|
| 543 | + ret = 95; |
|---|
| 376 | 544 | break; |
|---|
| 377 | 545 | case 10: |
|---|
| 378 | | - precision_bits = 0x20; |
|---|
| 546 | + ret = 190; |
|---|
| 379 | 547 | break; |
|---|
| 380 | 548 | case 11: |
|---|
| 381 | | - precision_bits = 0x40; |
|---|
| 549 | + ret = 375; |
|---|
| 382 | 550 | break; |
|---|
| 383 | 551 | case 12: |
|---|
| 384 | | - default: |
|---|
| 385 | | - precision_bits = 0x60; |
|---|
| 552 | + ret = 750; |
|---|
| 386 | 553 | break; |
|---|
| 554 | + case 13: |
|---|
| 555 | + ret = 850; /* GX20MH01 only. Datasheet says 500ms, but that's not enough. */ |
|---|
| 556 | + break; |
|---|
| 557 | + case 14: |
|---|
| 558 | + ret = 1600; /* GX20MH01 only. Datasheet says 1000ms - not enough */ |
|---|
| 559 | + break; |
|---|
| 560 | + default: |
|---|
| 561 | + ret = 750; |
|---|
| 387 | 562 | } |
|---|
| 388 | | - |
|---|
| 389 | | - while (max_trying--) { |
|---|
| 390 | | - crc = 0; |
|---|
| 391 | | - |
|---|
| 392 | | - if (!w1_reset_select_slave(sl)) { |
|---|
| 393 | | - int count = 0; |
|---|
| 394 | | - |
|---|
| 395 | | - /* read values to only alter precision bits */ |
|---|
| 396 | | - w1_write_8(dev, W1_READ_SCRATCHPAD); |
|---|
| 397 | | - count = w1_read_block(dev, rom, 9); |
|---|
| 398 | | - if (count != 9) |
|---|
| 399 | | - dev_warn(device, "w1_read_block() returned %u instead of 9.\n", count); |
|---|
| 400 | | - |
|---|
| 401 | | - crc = w1_calc_crc8(rom, 8); |
|---|
| 402 | | - if (rom[8] == crc) { |
|---|
| 403 | | - rom[4] = (rom[4] & ~mask) | (precision_bits & mask); |
|---|
| 404 | | - |
|---|
| 405 | | - if (!w1_reset_select_slave(sl)) { |
|---|
| 406 | | - w1_write_8(dev, W1_WRITE_SCRATCHPAD); |
|---|
| 407 | | - w1_write_8(dev, rom[2]); |
|---|
| 408 | | - w1_write_8(dev, rom[3]); |
|---|
| 409 | | - w1_write_8(dev, rom[4]); |
|---|
| 410 | | - |
|---|
| 411 | | - break; |
|---|
| 412 | | - } |
|---|
| 413 | | - } |
|---|
| 414 | | - } |
|---|
| 415 | | - } |
|---|
| 416 | | - |
|---|
| 417 | | - mutex_unlock(&dev->bus_mutex); |
|---|
| 418 | | -dec_refcnt: |
|---|
| 419 | | - atomic_dec(THERM_REFCNT(family_data)); |
|---|
| 420 | | -error: |
|---|
| 421 | 563 | return ret; |
|---|
| 422 | 564 | } |
|---|
| 423 | 565 | |
|---|
| 424 | | -static inline int w1_DS18B20_convert_temp(u8 rom[9]) |
|---|
| 566 | +static inline int w1_DS18S20_convert_time(struct w1_slave *sl) |
|---|
| 425 | 567 | { |
|---|
| 426 | | - s16 t = le16_to_cpup((__le16 *)rom); |
|---|
| 568 | + if (!sl->family_data) |
|---|
| 569 | + return -ENODEV; /* device unknown */ |
|---|
| 427 | 570 | |
|---|
| 428 | | - return t*1000/16; |
|---|
| 571 | + if (SLAVE_CONV_TIME_OVERRIDE(sl) == CONV_TIME_DEFAULT) |
|---|
| 572 | + return 750; /* default for DS18S20 */ |
|---|
| 573 | + else |
|---|
| 574 | + return SLAVE_CONV_TIME_OVERRIDE(sl); |
|---|
| 429 | 575 | } |
|---|
| 430 | 576 | |
|---|
| 577 | +static inline int w1_DS18B20_write_data(struct w1_slave *sl, |
|---|
| 578 | + const u8 *data) |
|---|
| 579 | +{ |
|---|
| 580 | + return write_scratchpad(sl, data, 3); |
|---|
| 581 | +} |
|---|
| 582 | + |
|---|
| 583 | +static inline int w1_DS18S20_write_data(struct w1_slave *sl, |
|---|
| 584 | + const u8 *data) |
|---|
| 585 | +{ |
|---|
| 586 | + /* No config register */ |
|---|
| 587 | + return write_scratchpad(sl, data, 2); |
|---|
| 588 | +} |
|---|
| 589 | + |
|---|
| 590 | +static inline int w1_DS18B20_set_resolution(struct w1_slave *sl, int val) |
|---|
| 591 | +{ |
|---|
| 592 | + int ret; |
|---|
| 593 | + struct therm_info info, info2; |
|---|
| 594 | + |
|---|
| 595 | + /* DS18B20 resolution is 9 to 12 bits */ |
|---|
| 596 | + /* GX20MH01 resolution is 9 to 14 bits */ |
|---|
| 597 | + if (val < W1_THERM_RESOLUTION_MIN || val > W1_THERM_RESOLUTION_MAX) |
|---|
| 598 | + return -EINVAL; |
|---|
| 599 | + |
|---|
| 600 | + /* Calc bit value from resolution */ |
|---|
| 601 | + val = (val - W1_THERM_RESOLUTION_MIN) << W1_THERM_RESOLUTION_SHIFT; |
|---|
| 602 | + |
|---|
| 603 | + /* |
|---|
| 604 | + * Read the scratchpad to change only the required bits |
|---|
| 605 | + * (bit5 & bit 6 from byte 4) |
|---|
| 606 | + */ |
|---|
| 607 | + ret = read_scratchpad(sl, &info); |
|---|
| 608 | + |
|---|
| 609 | + if (ret) |
|---|
| 610 | + return ret; |
|---|
| 611 | + |
|---|
| 612 | + |
|---|
| 613 | + info.rom[4] &= ~W1_THERM_RESOLUTION_MASK; |
|---|
| 614 | + info.rom[4] |= val; |
|---|
| 615 | + |
|---|
| 616 | + /* Write data in the device RAM */ |
|---|
| 617 | + ret = w1_DS18B20_write_data(sl, info.rom + 2); |
|---|
| 618 | + if (ret) |
|---|
| 619 | + return ret; |
|---|
| 620 | + |
|---|
| 621 | + /* Have to read back the resolution to verify an actual value |
|---|
| 622 | + * GX20MH01 and DS18B20 are indistinguishable by family number, but resolutions differ |
|---|
| 623 | + * Some DS18B20 clones don't support resolution change |
|---|
| 624 | + */ |
|---|
| 625 | + ret = read_scratchpad(sl, &info2); |
|---|
| 626 | + if (ret) |
|---|
| 627 | + /* Scratchpad read fail */ |
|---|
| 628 | + return ret; |
|---|
| 629 | + |
|---|
| 630 | + if ((info2.rom[4] & W1_THERM_RESOLUTION_MASK) == (info.rom[4] & W1_THERM_RESOLUTION_MASK)) |
|---|
| 631 | + return 0; |
|---|
| 632 | + |
|---|
| 633 | + /* Resolution verify error */ |
|---|
| 634 | + return -EIO; |
|---|
| 635 | +} |
|---|
| 636 | + |
|---|
| 637 | +static inline int w1_DS18B20_get_resolution(struct w1_slave *sl) |
|---|
| 638 | +{ |
|---|
| 639 | + int ret; |
|---|
| 640 | + int resolution; |
|---|
| 641 | + struct therm_info info; |
|---|
| 642 | + |
|---|
| 643 | + ret = read_scratchpad(sl, &info); |
|---|
| 644 | + |
|---|
| 645 | + if (ret) |
|---|
| 646 | + return ret; |
|---|
| 647 | + |
|---|
| 648 | + resolution = ((info.rom[4] & W1_THERM_RESOLUTION_MASK) >> W1_THERM_RESOLUTION_SHIFT) |
|---|
| 649 | + + W1_THERM_RESOLUTION_MIN; |
|---|
| 650 | + /* GX20MH01 has one special case: |
|---|
| 651 | + * >=14 means 14 bits when getting resolution from bit value. |
|---|
| 652 | + * Other devices have no more then 12 bits. |
|---|
| 653 | + */ |
|---|
| 654 | + if (resolution > W1_THERM_RESOLUTION_MAX) |
|---|
| 655 | + resolution = W1_THERM_RESOLUTION_MAX; |
|---|
| 656 | + |
|---|
| 657 | + return resolution; |
|---|
| 658 | +} |
|---|
| 659 | + |
|---|
| 660 | +/** |
|---|
| 661 | + * w1_DS18B20_convert_temp() - temperature computation for DS18B20 |
|---|
| 662 | + * @rom: data read from device RAM (8 data bytes + 1 CRC byte) |
|---|
| 663 | + * |
|---|
| 664 | + * Can be called for any DS18B20 compliant device. |
|---|
| 665 | + * |
|---|
| 666 | + * Return: value in millidegrees Celsius. |
|---|
| 667 | + */ |
|---|
| 668 | +static inline int w1_DS18B20_convert_temp(u8 rom[9]) |
|---|
| 669 | +{ |
|---|
| 670 | + u16 bv; |
|---|
| 671 | + s16 t; |
|---|
| 672 | + |
|---|
| 673 | + /* Signed 16-bit value to unsigned, cpu order */ |
|---|
| 674 | + bv = le16_to_cpup((__le16 *)rom); |
|---|
| 675 | + |
|---|
| 676 | + /* Config register bit R2 = 1 - GX20MH01 in 13 or 14 bit resolution mode */ |
|---|
| 677 | + if (rom[4] & 0x80) { |
|---|
| 678 | + /* Insert two temperature bits from config register */ |
|---|
| 679 | + /* Avoid arithmetic shift of signed value */ |
|---|
| 680 | + bv = (bv << 2) | (rom[4] & 3); |
|---|
| 681 | + t = (s16) bv; /* Degrees, lowest bit is 2^-6 */ |
|---|
| 682 | + return (int)t * 1000 / 64; /* Sign-extend to int; millidegrees */ |
|---|
| 683 | + } |
|---|
| 684 | + t = (s16)bv; /* Degrees, lowest bit is 2^-4 */ |
|---|
| 685 | + return (int)t * 1000 / 16; /* Sign-extend to int; millidegrees */ |
|---|
| 686 | +} |
|---|
| 687 | + |
|---|
| 688 | +/** |
|---|
| 689 | + * w1_DS18S20_convert_temp() - temperature computation for DS18S20 |
|---|
| 690 | + * @rom: data read from device RAM (8 data bytes + 1 CRC byte) |
|---|
| 691 | + * |
|---|
| 692 | + * Can be called for any DS18S20 compliant device. |
|---|
| 693 | + * |
|---|
| 694 | + * Return: value in millidegrees Celsius. |
|---|
| 695 | + */ |
|---|
| 431 | 696 | static inline int w1_DS18S20_convert_temp(u8 rom[9]) |
|---|
| 432 | 697 | { |
|---|
| 433 | 698 | int t, h; |
|---|
| 434 | 699 | |
|---|
| 435 | | - if (!rom[7]) |
|---|
| 700 | + if (!rom[7]) { |
|---|
| 701 | + pr_debug("%s: Invalid argument for conversion\n", __func__); |
|---|
| 436 | 702 | return 0; |
|---|
| 703 | + } |
|---|
| 437 | 704 | |
|---|
| 438 | 705 | if (rom[1] == 0) |
|---|
| 439 | 706 | t = ((s32)rom[0] >> 1)*1000; |
|---|
| .. | .. |
|---|
| 448 | 715 | return t; |
|---|
| 449 | 716 | } |
|---|
| 450 | 717 | |
|---|
| 451 | | -static inline int w1_convert_temp(u8 rom[9], u8 fid) |
|---|
| 718 | +/* Device capability description */ |
|---|
| 719 | +/* GX20MH01 device shares family number and structure with DS18B20 */ |
|---|
| 720 | + |
|---|
| 721 | +static struct w1_therm_family_converter w1_therm_families[] = { |
|---|
| 722 | + { |
|---|
| 723 | + .f = &w1_therm_family_DS18S20, |
|---|
| 724 | + .convert = w1_DS18S20_convert_temp, |
|---|
| 725 | + .get_conversion_time = w1_DS18S20_convert_time, |
|---|
| 726 | + .set_resolution = NULL, /* no config register */ |
|---|
| 727 | + .get_resolution = NULL, /* no config register */ |
|---|
| 728 | + .write_data = w1_DS18S20_write_data, |
|---|
| 729 | + .bulk_read = true |
|---|
| 730 | + }, |
|---|
| 731 | + { |
|---|
| 732 | + .f = &w1_therm_family_DS1822, |
|---|
| 733 | + .convert = w1_DS18B20_convert_temp, |
|---|
| 734 | + .get_conversion_time = w1_DS18B20_convert_time, |
|---|
| 735 | + .set_resolution = w1_DS18B20_set_resolution, |
|---|
| 736 | + .get_resolution = w1_DS18B20_get_resolution, |
|---|
| 737 | + .write_data = w1_DS18B20_write_data, |
|---|
| 738 | + .bulk_read = true |
|---|
| 739 | + }, |
|---|
| 740 | + { |
|---|
| 741 | + /* Also used for GX20MH01 */ |
|---|
| 742 | + .f = &w1_therm_family_DS18B20, |
|---|
| 743 | + .convert = w1_DS18B20_convert_temp, |
|---|
| 744 | + .get_conversion_time = w1_DS18B20_convert_time, |
|---|
| 745 | + .set_resolution = w1_DS18B20_set_resolution, |
|---|
| 746 | + .get_resolution = w1_DS18B20_get_resolution, |
|---|
| 747 | + .write_data = w1_DS18B20_write_data, |
|---|
| 748 | + .bulk_read = true |
|---|
| 749 | + }, |
|---|
| 750 | + { |
|---|
| 751 | + .f = &w1_therm_family_DS28EA00, |
|---|
| 752 | + .convert = w1_DS18B20_convert_temp, |
|---|
| 753 | + .get_conversion_time = w1_DS18B20_convert_time, |
|---|
| 754 | + .set_resolution = w1_DS18B20_set_resolution, |
|---|
| 755 | + .get_resolution = w1_DS18B20_get_resolution, |
|---|
| 756 | + .write_data = w1_DS18B20_write_data, |
|---|
| 757 | + .bulk_read = false |
|---|
| 758 | + }, |
|---|
| 759 | + { |
|---|
| 760 | + .f = &w1_therm_family_DS1825, |
|---|
| 761 | + .convert = w1_DS18B20_convert_temp, |
|---|
| 762 | + .get_conversion_time = w1_DS18B20_convert_time, |
|---|
| 763 | + .set_resolution = w1_DS18B20_set_resolution, |
|---|
| 764 | + .get_resolution = w1_DS18B20_get_resolution, |
|---|
| 765 | + .write_data = w1_DS18B20_write_data, |
|---|
| 766 | + .bulk_read = true |
|---|
| 767 | + } |
|---|
| 768 | +}; |
|---|
| 769 | + |
|---|
| 770 | +/* Helpers Functions */ |
|---|
| 771 | + |
|---|
| 772 | +/** |
|---|
| 773 | + * device_family() - Retrieve a pointer on &struct w1_therm_family_converter |
|---|
| 774 | + * @sl: slave to retrieve the device specific structure |
|---|
| 775 | + * |
|---|
| 776 | + * Return: pointer to the slaves's family converter, NULL if not known |
|---|
| 777 | + */ |
|---|
| 778 | +static struct w1_therm_family_converter *device_family(struct w1_slave *sl) |
|---|
| 452 | 779 | { |
|---|
| 780 | + struct w1_therm_family_converter *ret = NULL; |
|---|
| 453 | 781 | int i; |
|---|
| 454 | 782 | |
|---|
| 455 | | - for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) |
|---|
| 456 | | - if (w1_therm_families[i].f->fid == fid) |
|---|
| 457 | | - return w1_therm_families[i].convert(rom); |
|---|
| 783 | + for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) { |
|---|
| 784 | + if (w1_therm_families[i].f->fid == sl->family->fid) { |
|---|
| 785 | + ret = &w1_therm_families[i]; |
|---|
| 786 | + break; |
|---|
| 787 | + } |
|---|
| 788 | + } |
|---|
| 789 | + return ret; |
|---|
| 790 | +} |
|---|
| 791 | + |
|---|
| 792 | +/** |
|---|
| 793 | + * bus_mutex_lock() - Acquire the mutex |
|---|
| 794 | + * @lock: w1 bus mutex to acquire |
|---|
| 795 | + * |
|---|
| 796 | + * It try to acquire the mutex W1_THERM_MAX_TRY times and wait |
|---|
| 797 | + * W1_THERM_RETRY_DELAY between 2 attempts. |
|---|
| 798 | + * |
|---|
| 799 | + * Return: true is mutex is acquired and lock, false otherwise |
|---|
| 800 | + */ |
|---|
| 801 | +static inline bool bus_mutex_lock(struct mutex *lock) |
|---|
| 802 | +{ |
|---|
| 803 | + int max_trying = W1_THERM_MAX_TRY; |
|---|
| 804 | + |
|---|
| 805 | + /* try to acquire the mutex, if not, sleep retry_delay before retry) */ |
|---|
| 806 | + while (mutex_lock_interruptible(lock) != 0 && max_trying > 0) { |
|---|
| 807 | + unsigned long sleep_rem; |
|---|
| 808 | + |
|---|
| 809 | + sleep_rem = msleep_interruptible(W1_THERM_RETRY_DELAY); |
|---|
| 810 | + if (!sleep_rem) |
|---|
| 811 | + max_trying--; |
|---|
| 812 | + } |
|---|
| 813 | + |
|---|
| 814 | + if (!max_trying) |
|---|
| 815 | + return false; /* Didn't acquire the bus mutex */ |
|---|
| 816 | + |
|---|
| 817 | + return true; |
|---|
| 818 | +} |
|---|
| 819 | + |
|---|
| 820 | +/** |
|---|
| 821 | + * check_family_data() - Check if family data and specific functions are present |
|---|
| 822 | + * @sl: W1 device data |
|---|
| 823 | + * |
|---|
| 824 | + * Return: 0 - OK, negative value - error |
|---|
| 825 | + */ |
|---|
| 826 | +static int check_family_data(struct w1_slave *sl) |
|---|
| 827 | +{ |
|---|
| 828 | + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { |
|---|
| 829 | + dev_info(&sl->dev, |
|---|
| 830 | + "%s: Device is not supported by the driver\n", __func__); |
|---|
| 831 | + return -EINVAL; /* No device family */ |
|---|
| 832 | + } |
|---|
| 833 | + return 0; |
|---|
| 834 | +} |
|---|
| 835 | + |
|---|
| 836 | +/** |
|---|
| 837 | + * support_bulk_read() - check if slave support bulk read |
|---|
| 838 | + * @sl: device to check the ability |
|---|
| 839 | + * |
|---|
| 840 | + * Return: true if bulk read is supported, false if not or error |
|---|
| 841 | + */ |
|---|
| 842 | +static inline bool bulk_read_support(struct w1_slave *sl) |
|---|
| 843 | +{ |
|---|
| 844 | + if (SLAVE_SPECIFIC_FUNC(sl)) |
|---|
| 845 | + return SLAVE_SPECIFIC_FUNC(sl)->bulk_read; |
|---|
| 846 | + |
|---|
| 847 | + dev_info(&sl->dev, |
|---|
| 848 | + "%s: Device not supported by the driver\n", __func__); |
|---|
| 849 | + |
|---|
| 850 | + return false; /* No device family */ |
|---|
| 851 | +} |
|---|
| 852 | + |
|---|
| 853 | +/** |
|---|
| 854 | + * conversion_time() - get the Tconv for the slave |
|---|
| 855 | + * @sl: device to get the conversion time |
|---|
| 856 | + * |
|---|
| 857 | + * On device supporting resolution settings, conversion time depend |
|---|
| 858 | + * on the resolution setting. This helper function get the slave timing, |
|---|
| 859 | + * depending on its current setting. |
|---|
| 860 | + * |
|---|
| 861 | + * Return: conversion time in ms, negative values are kernel error code |
|---|
| 862 | + */ |
|---|
| 863 | +static inline int conversion_time(struct w1_slave *sl) |
|---|
| 864 | +{ |
|---|
| 865 | + if (SLAVE_SPECIFIC_FUNC(sl)) |
|---|
| 866 | + return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl); |
|---|
| 867 | + |
|---|
| 868 | + dev_info(&sl->dev, |
|---|
| 869 | + "%s: Device not supported by the driver\n", __func__); |
|---|
| 870 | + |
|---|
| 871 | + return -ENODEV; /* No device family */ |
|---|
| 872 | +} |
|---|
| 873 | + |
|---|
| 874 | +/** |
|---|
| 875 | + * temperature_from_RAM() - Convert the read info to temperature |
|---|
| 876 | + * @sl: device that sent the RAM data |
|---|
| 877 | + * @rom: read value on the slave device RAM |
|---|
| 878 | + * |
|---|
| 879 | + * Device dependent, the function bind the correct computation method. |
|---|
| 880 | + * |
|---|
| 881 | + * Return: temperature in 1/1000degC, 0 on error. |
|---|
| 882 | + */ |
|---|
| 883 | +static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9]) |
|---|
| 884 | +{ |
|---|
| 885 | + if (SLAVE_SPECIFIC_FUNC(sl)) |
|---|
| 886 | + return SLAVE_SPECIFIC_FUNC(sl)->convert(rom); |
|---|
| 887 | + |
|---|
| 888 | + dev_info(&sl->dev, |
|---|
| 889 | + "%s: Device not supported by the driver\n", __func__); |
|---|
| 890 | + |
|---|
| 891 | + return 0; /* No device family */ |
|---|
| 892 | +} |
|---|
| 893 | + |
|---|
| 894 | +/** |
|---|
| 895 | + * int_to_short() - Safe casting of int to short |
|---|
| 896 | + * |
|---|
| 897 | + * @i: integer to be converted to short |
|---|
| 898 | + * |
|---|
| 899 | + * Device register use 1 byte to store signed integer. |
|---|
| 900 | + * This helper function convert the int in a signed short, |
|---|
| 901 | + * using the min/max values that device can measure as limits. |
|---|
| 902 | + * min/max values are defined by macro. |
|---|
| 903 | + * |
|---|
| 904 | + * Return: a short in the range of min/max value |
|---|
| 905 | + */ |
|---|
| 906 | +static inline s8 int_to_short(int i) |
|---|
| 907 | +{ |
|---|
| 908 | + /* Prepare to cast to short by eliminating out of range values */ |
|---|
| 909 | + i = i > MAX_TEMP ? MAX_TEMP : i; |
|---|
| 910 | + i = i < MIN_TEMP ? MIN_TEMP : i; |
|---|
| 911 | + return (s8) i; |
|---|
| 912 | +} |
|---|
| 913 | + |
|---|
| 914 | +/* Interface Functions */ |
|---|
| 915 | + |
|---|
| 916 | +static int w1_therm_add_slave(struct w1_slave *sl) |
|---|
| 917 | +{ |
|---|
| 918 | + struct w1_therm_family_converter *sl_family_conv; |
|---|
| 919 | + |
|---|
| 920 | + /* Allocate memory */ |
|---|
| 921 | + sl->family_data = kzalloc(sizeof(struct w1_therm_family_data), |
|---|
| 922 | + GFP_KERNEL); |
|---|
| 923 | + if (!sl->family_data) |
|---|
| 924 | + return -ENOMEM; |
|---|
| 925 | + |
|---|
| 926 | + atomic_set(THERM_REFCNT(sl->family_data), 1); |
|---|
| 927 | + |
|---|
| 928 | + /* Get a pointer to the device specific function struct */ |
|---|
| 929 | + sl_family_conv = device_family(sl); |
|---|
| 930 | + if (!sl_family_conv) { |
|---|
| 931 | + kfree(sl->family_data); |
|---|
| 932 | + return -ENODEV; |
|---|
| 933 | + } |
|---|
| 934 | + /* save this pointer to the device structure */ |
|---|
| 935 | + SLAVE_SPECIFIC_FUNC(sl) = sl_family_conv; |
|---|
| 936 | + |
|---|
| 937 | + if (bulk_read_support(sl)) { |
|---|
| 938 | + /* |
|---|
| 939 | + * add the sys entry to trigger bulk_read |
|---|
| 940 | + * at master level only the 1st time |
|---|
| 941 | + */ |
|---|
| 942 | + if (!bulk_read_device_counter) { |
|---|
| 943 | + int err = device_create_file(&sl->master->dev, |
|---|
| 944 | + &dev_attr_therm_bulk_read); |
|---|
| 945 | + |
|---|
| 946 | + if (err) |
|---|
| 947 | + dev_warn(&sl->dev, |
|---|
| 948 | + "%s: Device has been added, but bulk read is unavailable. err=%d\n", |
|---|
| 949 | + __func__, err); |
|---|
| 950 | + } |
|---|
| 951 | + /* Increment the counter */ |
|---|
| 952 | + bulk_read_device_counter++; |
|---|
| 953 | + } |
|---|
| 954 | + |
|---|
| 955 | + /* Getting the power mode of the device {external, parasite} */ |
|---|
| 956 | + SLAVE_POWERMODE(sl) = read_powermode(sl); |
|---|
| 957 | + |
|---|
| 958 | + if (SLAVE_POWERMODE(sl) < 0) { |
|---|
| 959 | + /* no error returned as device has been added */ |
|---|
| 960 | + dev_warn(&sl->dev, |
|---|
| 961 | + "%s: Device has been added, but power_mode may be corrupted. err=%d\n", |
|---|
| 962 | + __func__, SLAVE_POWERMODE(sl)); |
|---|
| 963 | + } |
|---|
| 964 | + |
|---|
| 965 | + /* Getting the resolution of the device */ |
|---|
| 966 | + if (SLAVE_SPECIFIC_FUNC(sl)->get_resolution) { |
|---|
| 967 | + SLAVE_RESOLUTION(sl) = |
|---|
| 968 | + SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl); |
|---|
| 969 | + if (SLAVE_RESOLUTION(sl) < 0) { |
|---|
| 970 | + /* no error returned as device has been added */ |
|---|
| 971 | + dev_warn(&sl->dev, |
|---|
| 972 | + "%s:Device has been added, but resolution may be corrupted. err=%d\n", |
|---|
| 973 | + __func__, SLAVE_RESOLUTION(sl)); |
|---|
| 974 | + } |
|---|
| 975 | + } |
|---|
| 976 | + |
|---|
| 977 | + /* Finally initialize convert_triggered flag */ |
|---|
| 978 | + SLAVE_CONVERT_TRIGGERED(sl) = 0; |
|---|
| 458 | 979 | |
|---|
| 459 | 980 | return 0; |
|---|
| 460 | 981 | } |
|---|
| 461 | 982 | |
|---|
| 462 | | -static ssize_t w1_slave_store(struct device *device, |
|---|
| 463 | | - struct device_attribute *attr, const char *buf, |
|---|
| 464 | | - size_t size) |
|---|
| 983 | +static void w1_therm_remove_slave(struct w1_slave *sl) |
|---|
| 465 | 984 | { |
|---|
| 466 | | - int val, ret; |
|---|
| 467 | | - struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 468 | | - int i; |
|---|
| 985 | + int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data)); |
|---|
| 469 | 986 | |
|---|
| 470 | | - ret = kstrtoint(buf, 0, &val); |
|---|
| 471 | | - if (ret) |
|---|
| 472 | | - return ret; |
|---|
| 473 | | - |
|---|
| 474 | | - for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) { |
|---|
| 475 | | - if (w1_therm_families[i].f->fid == sl->family->fid) { |
|---|
| 476 | | - /* zero value indicates to write current configuration to eeprom */ |
|---|
| 477 | | - if (val == 0) |
|---|
| 478 | | - ret = w1_therm_families[i].eeprom(device); |
|---|
| 479 | | - else |
|---|
| 480 | | - ret = w1_therm_families[i].precision(device, val); |
|---|
| 481 | | - break; |
|---|
| 482 | | - } |
|---|
| 987 | + if (bulk_read_support(sl)) { |
|---|
| 988 | + bulk_read_device_counter--; |
|---|
| 989 | + /* Delete the entry if no more device support the feature */ |
|---|
| 990 | + if (!bulk_read_device_counter) |
|---|
| 991 | + device_remove_file(&sl->master->dev, |
|---|
| 992 | + &dev_attr_therm_bulk_read); |
|---|
| 483 | 993 | } |
|---|
| 484 | | - return ret ? : size; |
|---|
| 994 | + |
|---|
| 995 | + while (refcnt) { |
|---|
| 996 | + msleep(1000); |
|---|
| 997 | + refcnt = atomic_read(THERM_REFCNT(sl->family_data)); |
|---|
| 998 | + } |
|---|
| 999 | + kfree(sl->family_data); |
|---|
| 1000 | + sl->family_data = NULL; |
|---|
| 485 | 1001 | } |
|---|
| 486 | 1002 | |
|---|
| 487 | | -static ssize_t read_therm(struct device *device, |
|---|
| 488 | | - struct w1_slave *sl, struct therm_info *info) |
|---|
| 489 | | -{ |
|---|
| 490 | | - struct w1_master *dev = sl->master; |
|---|
| 491 | | - u8 external_power; |
|---|
| 492 | | - int ret, max_trying = 10; |
|---|
| 493 | | - u8 *family_data = sl->family_data; |
|---|
| 1003 | +/* Hardware Functions */ |
|---|
| 494 | 1004 | |
|---|
| 495 | | - if (!family_data) { |
|---|
| 496 | | - ret = -ENODEV; |
|---|
| 1005 | +/* Safe version of reset_select_slave - avoid using the one in w_io.c */ |
|---|
| 1006 | +static int reset_select_slave(struct w1_slave *sl) |
|---|
| 1007 | +{ |
|---|
| 1008 | + u8 match[9] = { W1_MATCH_ROM, }; |
|---|
| 1009 | + u64 rn = le64_to_cpu(*((u64 *)&sl->reg_num)); |
|---|
| 1010 | + |
|---|
| 1011 | + if (w1_reset_bus(sl->master)) |
|---|
| 1012 | + return -ENODEV; |
|---|
| 1013 | + |
|---|
| 1014 | + memcpy(&match[1], &rn, 8); |
|---|
| 1015 | + w1_write_block(sl->master, match, 9); |
|---|
| 1016 | + |
|---|
| 1017 | + return 0; |
|---|
| 1018 | +} |
|---|
| 1019 | + |
|---|
| 1020 | +/** |
|---|
| 1021 | + * w1_poll_completion - Poll for operation completion, with timeout |
|---|
| 1022 | + * @dev_master: the device master of the bus |
|---|
| 1023 | + * @tout_ms: timeout in milliseconds |
|---|
| 1024 | + * |
|---|
| 1025 | + * The device is answering 0's while an operation is in progress and 1's after it completes |
|---|
| 1026 | + * Timeout may happen if the previous command was not recognised due to a line noise |
|---|
| 1027 | + * |
|---|
| 1028 | + * Return: 0 - OK, negative error - timeout |
|---|
| 1029 | + */ |
|---|
| 1030 | +static int w1_poll_completion(struct w1_master *dev_master, int tout_ms) |
|---|
| 1031 | +{ |
|---|
| 1032 | + int i; |
|---|
| 1033 | + |
|---|
| 1034 | + for (i = 0; i < tout_ms/W1_POLL_PERIOD; i++) { |
|---|
| 1035 | + /* Delay is before poll, for device to recognize a command */ |
|---|
| 1036 | + msleep(W1_POLL_PERIOD); |
|---|
| 1037 | + |
|---|
| 1038 | + /* Compare all 8 bits to mitigate a noise on the bus */ |
|---|
| 1039 | + if (w1_read_8(dev_master) == 0xFF) |
|---|
| 1040 | + break; |
|---|
| 1041 | + } |
|---|
| 1042 | + if (i == tout_ms/W1_POLL_PERIOD) |
|---|
| 1043 | + return -EIO; |
|---|
| 1044 | + |
|---|
| 1045 | + return 0; |
|---|
| 1046 | +} |
|---|
| 1047 | + |
|---|
| 1048 | +static int convert_t(struct w1_slave *sl, struct therm_info *info) |
|---|
| 1049 | +{ |
|---|
| 1050 | + struct w1_master *dev_master = sl->master; |
|---|
| 1051 | + int max_trying = W1_THERM_MAX_TRY; |
|---|
| 1052 | + int t_conv; |
|---|
| 1053 | + int ret = -ENODEV; |
|---|
| 1054 | + bool strong_pullup; |
|---|
| 1055 | + |
|---|
| 1056 | + if (!sl->family_data) |
|---|
| 497 | 1057 | goto error; |
|---|
| 1058 | + |
|---|
| 1059 | + strong_pullup = (w1_strong_pullup == 2 || |
|---|
| 1060 | + (!SLAVE_POWERMODE(sl) && |
|---|
| 1061 | + w1_strong_pullup)); |
|---|
| 1062 | + |
|---|
| 1063 | + if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) { |
|---|
| 1064 | + dev_warn(&sl->dev, |
|---|
| 1065 | + "%s: Disabling W1_THERM_POLL_COMPLETION in parasite power mode.\n", |
|---|
| 1066 | + __func__); |
|---|
| 1067 | + SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION; |
|---|
| 498 | 1068 | } |
|---|
| 499 | 1069 | |
|---|
| 500 | | - /* prevent the slave from going away in sleep */ |
|---|
| 501 | | - atomic_inc(THERM_REFCNT(family_data)); |
|---|
| 502 | | - |
|---|
| 503 | | - ret = mutex_lock_interruptible(&dev->bus_mutex); |
|---|
| 504 | | - if (ret != 0) |
|---|
| 505 | | - goto dec_refcnt; |
|---|
| 1070 | + /* get conversion duration device and id dependent */ |
|---|
| 1071 | + t_conv = conversion_time(sl); |
|---|
| 506 | 1072 | |
|---|
| 507 | 1073 | memset(info->rom, 0, sizeof(info->rom)); |
|---|
| 508 | 1074 | |
|---|
| 509 | | - while (max_trying--) { |
|---|
| 1075 | + /* prevent the slave from going away in sleep */ |
|---|
| 1076 | + atomic_inc(THERM_REFCNT(sl->family_data)); |
|---|
| 1077 | + |
|---|
| 1078 | + if (!bus_mutex_lock(&dev_master->bus_mutex)) { |
|---|
| 1079 | + ret = -EAGAIN; /* Didn't acquire the mutex */ |
|---|
| 1080 | + goto dec_refcnt; |
|---|
| 1081 | + } |
|---|
| 1082 | + |
|---|
| 1083 | + while (max_trying-- && ret) { /* ret should be 0 */ |
|---|
| 510 | 1084 | |
|---|
| 511 | 1085 | info->verdict = 0; |
|---|
| 512 | 1086 | info->crc = 0; |
|---|
| 513 | | - |
|---|
| 514 | | - if (!w1_reset_select_slave(sl)) { |
|---|
| 515 | | - int count = 0; |
|---|
| 516 | | - unsigned int tm = 750; |
|---|
| 1087 | + /* safe version to select slave */ |
|---|
| 1088 | + if (!reset_select_slave(sl)) { |
|---|
| 517 | 1089 | unsigned long sleep_rem; |
|---|
| 518 | 1090 | |
|---|
| 519 | | - w1_write_8(dev, W1_READ_PSUPPLY); |
|---|
| 520 | | - external_power = w1_read_8(dev); |
|---|
| 521 | | - |
|---|
| 522 | | - if (w1_reset_select_slave(sl)) |
|---|
| 523 | | - continue; |
|---|
| 524 | | - |
|---|
| 525 | 1091 | /* 750ms strong pullup (or delay) after the convert */ |
|---|
| 526 | | - if (w1_strong_pullup == 2 || |
|---|
| 527 | | - (!external_power && w1_strong_pullup)) |
|---|
| 528 | | - w1_next_pullup(dev, tm); |
|---|
| 1092 | + if (strong_pullup) |
|---|
| 1093 | + w1_next_pullup(dev_master, t_conv); |
|---|
| 529 | 1094 | |
|---|
| 530 | | - w1_write_8(dev, W1_CONVERT_TEMP); |
|---|
| 1095 | + w1_write_8(dev_master, W1_CONVERT_TEMP); |
|---|
| 531 | 1096 | |
|---|
| 532 | | - if (external_power) { |
|---|
| 533 | | - mutex_unlock(&dev->bus_mutex); |
|---|
| 534 | | - |
|---|
| 535 | | - sleep_rem = msleep_interruptible(tm); |
|---|
| 1097 | + if (SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) { |
|---|
| 1098 | + ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP); |
|---|
| 1099 | + if (ret) { |
|---|
| 1100 | + dev_dbg(&sl->dev, "%s: Timeout\n", __func__); |
|---|
| 1101 | + goto mt_unlock; |
|---|
| 1102 | + } |
|---|
| 1103 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1104 | + } else if (!strong_pullup) { /*no device need pullup */ |
|---|
| 1105 | + sleep_rem = msleep_interruptible(t_conv); |
|---|
| 1106 | + if (sleep_rem != 0) { |
|---|
| 1107 | + ret = -EINTR; |
|---|
| 1108 | + goto mt_unlock; |
|---|
| 1109 | + } |
|---|
| 1110 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1111 | + } else { /*some device need pullup */ |
|---|
| 1112 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1113 | + sleep_rem = msleep_interruptible(t_conv); |
|---|
| 536 | 1114 | if (sleep_rem != 0) { |
|---|
| 537 | 1115 | ret = -EINTR; |
|---|
| 538 | 1116 | goto dec_refcnt; |
|---|
| 539 | 1117 | } |
|---|
| 1118 | + } |
|---|
| 1119 | + ret = read_scratchpad(sl, info); |
|---|
| 540 | 1120 | |
|---|
| 541 | | - ret = mutex_lock_interruptible(&dev->bus_mutex); |
|---|
| 542 | | - if (ret != 0) |
|---|
| 543 | | - goto dec_refcnt; |
|---|
| 544 | | - } else if (!w1_strong_pullup) { |
|---|
| 545 | | - sleep_rem = msleep_interruptible(tm); |
|---|
| 1121 | + /* If enabled, check for conversion success */ |
|---|
| 1122 | + if ((SLAVE_FEATURES(sl) & W1_THERM_CHECK_RESULT) && |
|---|
| 1123 | + (info->rom[6] == 0xC) && |
|---|
| 1124 | + ((info->rom[1] == 0x5 && info->rom[0] == 0x50) || |
|---|
| 1125 | + (info->rom[1] == 0x7 && info->rom[0] == 0xFF)) |
|---|
| 1126 | + ) { |
|---|
| 1127 | + /* Invalid reading (scratchpad byte 6 = 0xC) |
|---|
| 1128 | + * due to insufficient conversion time |
|---|
| 1129 | + * or power failure. |
|---|
| 1130 | + */ |
|---|
| 1131 | + ret = -EIO; |
|---|
| 1132 | + } |
|---|
| 1133 | + |
|---|
| 1134 | + goto dec_refcnt; |
|---|
| 1135 | + } |
|---|
| 1136 | + |
|---|
| 1137 | + } |
|---|
| 1138 | + |
|---|
| 1139 | +mt_unlock: |
|---|
| 1140 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1141 | +dec_refcnt: |
|---|
| 1142 | + atomic_dec(THERM_REFCNT(sl->family_data)); |
|---|
| 1143 | +error: |
|---|
| 1144 | + return ret; |
|---|
| 1145 | +} |
|---|
| 1146 | + |
|---|
| 1147 | +static int conv_time_measure(struct w1_slave *sl, int *conv_time) |
|---|
| 1148 | +{ |
|---|
| 1149 | + struct therm_info inf, |
|---|
| 1150 | + *info = &inf; |
|---|
| 1151 | + struct w1_master *dev_master = sl->master; |
|---|
| 1152 | + int max_trying = W1_THERM_MAX_TRY; |
|---|
| 1153 | + int ret = -ENODEV; |
|---|
| 1154 | + bool strong_pullup; |
|---|
| 1155 | + |
|---|
| 1156 | + if (!sl->family_data) |
|---|
| 1157 | + goto error; |
|---|
| 1158 | + |
|---|
| 1159 | + strong_pullup = (w1_strong_pullup == 2 || |
|---|
| 1160 | + (!SLAVE_POWERMODE(sl) && |
|---|
| 1161 | + w1_strong_pullup)); |
|---|
| 1162 | + |
|---|
| 1163 | + if (strong_pullup) { |
|---|
| 1164 | + pr_info("%s: Measure with strong_pullup is not supported.\n", __func__); |
|---|
| 1165 | + return -EINVAL; |
|---|
| 1166 | + } |
|---|
| 1167 | + |
|---|
| 1168 | + memset(info->rom, 0, sizeof(info->rom)); |
|---|
| 1169 | + |
|---|
| 1170 | + /* prevent the slave from going away in sleep */ |
|---|
| 1171 | + atomic_inc(THERM_REFCNT(sl->family_data)); |
|---|
| 1172 | + |
|---|
| 1173 | + if (!bus_mutex_lock(&dev_master->bus_mutex)) { |
|---|
| 1174 | + ret = -EAGAIN; /* Didn't acquire the mutex */ |
|---|
| 1175 | + goto dec_refcnt; |
|---|
| 1176 | + } |
|---|
| 1177 | + |
|---|
| 1178 | + while (max_trying-- && ret) { /* ret should be 0 */ |
|---|
| 1179 | + info->verdict = 0; |
|---|
| 1180 | + info->crc = 0; |
|---|
| 1181 | + /* safe version to select slave */ |
|---|
| 1182 | + if (!reset_select_slave(sl)) { |
|---|
| 1183 | + int j_start, j_end; |
|---|
| 1184 | + |
|---|
| 1185 | + /*no device need pullup */ |
|---|
| 1186 | + w1_write_8(dev_master, W1_CONVERT_TEMP); |
|---|
| 1187 | + |
|---|
| 1188 | + j_start = jiffies; |
|---|
| 1189 | + ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP); |
|---|
| 1190 | + if (ret) { |
|---|
| 1191 | + dev_dbg(&sl->dev, "%s: Timeout\n", __func__); |
|---|
| 1192 | + goto mt_unlock; |
|---|
| 1193 | + } |
|---|
| 1194 | + j_end = jiffies; |
|---|
| 1195 | + /* 1.2x increase for variation and changes over temperature range */ |
|---|
| 1196 | + *conv_time = jiffies_to_msecs(j_end-j_start)*12/10; |
|---|
| 1197 | + pr_debug("W1 Measure complete, conv_time = %d, HZ=%d.\n", |
|---|
| 1198 | + *conv_time, HZ); |
|---|
| 1199 | + if (*conv_time <= CONV_TIME_MEASURE) { |
|---|
| 1200 | + ret = -EIO; |
|---|
| 1201 | + goto mt_unlock; |
|---|
| 1202 | + } |
|---|
| 1203 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1204 | + ret = read_scratchpad(sl, info); |
|---|
| 1205 | + goto dec_refcnt; |
|---|
| 1206 | + } |
|---|
| 1207 | + |
|---|
| 1208 | + } |
|---|
| 1209 | +mt_unlock: |
|---|
| 1210 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1211 | +dec_refcnt: |
|---|
| 1212 | + atomic_dec(THERM_REFCNT(sl->family_data)); |
|---|
| 1213 | +error: |
|---|
| 1214 | + return ret; |
|---|
| 1215 | +} |
|---|
| 1216 | + |
|---|
| 1217 | +static int read_scratchpad(struct w1_slave *sl, struct therm_info *info) |
|---|
| 1218 | +{ |
|---|
| 1219 | + struct w1_master *dev_master = sl->master; |
|---|
| 1220 | + int max_trying = W1_THERM_MAX_TRY; |
|---|
| 1221 | + int ret = -ENODEV; |
|---|
| 1222 | + |
|---|
| 1223 | + info->verdict = 0; |
|---|
| 1224 | + |
|---|
| 1225 | + if (!sl->family_data) |
|---|
| 1226 | + goto error; |
|---|
| 1227 | + |
|---|
| 1228 | + memset(info->rom, 0, sizeof(info->rom)); |
|---|
| 1229 | + |
|---|
| 1230 | + /* prevent the slave from going away in sleep */ |
|---|
| 1231 | + atomic_inc(THERM_REFCNT(sl->family_data)); |
|---|
| 1232 | + |
|---|
| 1233 | + if (!bus_mutex_lock(&dev_master->bus_mutex)) { |
|---|
| 1234 | + ret = -EAGAIN; /* Didn't acquire the mutex */ |
|---|
| 1235 | + goto dec_refcnt; |
|---|
| 1236 | + } |
|---|
| 1237 | + |
|---|
| 1238 | + while (max_trying-- && ret) { /* ret should be 0 */ |
|---|
| 1239 | + /* safe version to select slave */ |
|---|
| 1240 | + if (!reset_select_slave(sl)) { |
|---|
| 1241 | + u8 nb_bytes_read; |
|---|
| 1242 | + |
|---|
| 1243 | + w1_write_8(dev_master, W1_READ_SCRATCHPAD); |
|---|
| 1244 | + |
|---|
| 1245 | + nb_bytes_read = w1_read_block(dev_master, info->rom, 9); |
|---|
| 1246 | + if (nb_bytes_read != 9) { |
|---|
| 1247 | + dev_warn(&sl->dev, |
|---|
| 1248 | + "w1_read_block(): returned %u instead of 9.\n", |
|---|
| 1249 | + nb_bytes_read); |
|---|
| 1250 | + ret = -EIO; |
|---|
| 1251 | + } |
|---|
| 1252 | + |
|---|
| 1253 | + info->crc = w1_calc_crc8(info->rom, 8); |
|---|
| 1254 | + |
|---|
| 1255 | + if (info->rom[8] == info->crc) { |
|---|
| 1256 | + info->verdict = 1; |
|---|
| 1257 | + ret = 0; |
|---|
| 1258 | + } else |
|---|
| 1259 | + ret = -EIO; /* CRC not checked */ |
|---|
| 1260 | + } |
|---|
| 1261 | + |
|---|
| 1262 | + } |
|---|
| 1263 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1264 | + |
|---|
| 1265 | +dec_refcnt: |
|---|
| 1266 | + atomic_dec(THERM_REFCNT(sl->family_data)); |
|---|
| 1267 | +error: |
|---|
| 1268 | + return ret; |
|---|
| 1269 | +} |
|---|
| 1270 | + |
|---|
| 1271 | +static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes) |
|---|
| 1272 | +{ |
|---|
| 1273 | + struct w1_master *dev_master = sl->master; |
|---|
| 1274 | + int max_trying = W1_THERM_MAX_TRY; |
|---|
| 1275 | + int ret = -ENODEV; |
|---|
| 1276 | + |
|---|
| 1277 | + if (!sl->family_data) |
|---|
| 1278 | + goto error; |
|---|
| 1279 | + |
|---|
| 1280 | + /* prevent the slave from going away in sleep */ |
|---|
| 1281 | + atomic_inc(THERM_REFCNT(sl->family_data)); |
|---|
| 1282 | + |
|---|
| 1283 | + if (!bus_mutex_lock(&dev_master->bus_mutex)) { |
|---|
| 1284 | + ret = -EAGAIN; /* Didn't acquire the mutex */ |
|---|
| 1285 | + goto dec_refcnt; |
|---|
| 1286 | + } |
|---|
| 1287 | + |
|---|
| 1288 | + while (max_trying-- && ret) { /* ret should be 0 */ |
|---|
| 1289 | + /* safe version to select slave */ |
|---|
| 1290 | + if (!reset_select_slave(sl)) { |
|---|
| 1291 | + w1_write_8(dev_master, W1_WRITE_SCRATCHPAD); |
|---|
| 1292 | + w1_write_block(dev_master, data, nb_bytes); |
|---|
| 1293 | + ret = 0; |
|---|
| 1294 | + } |
|---|
| 1295 | + } |
|---|
| 1296 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1297 | + |
|---|
| 1298 | +dec_refcnt: |
|---|
| 1299 | + atomic_dec(THERM_REFCNT(sl->family_data)); |
|---|
| 1300 | +error: |
|---|
| 1301 | + return ret; |
|---|
| 1302 | +} |
|---|
| 1303 | + |
|---|
| 1304 | +static int copy_scratchpad(struct w1_slave *sl) |
|---|
| 1305 | +{ |
|---|
| 1306 | + struct w1_master *dev_master = sl->master; |
|---|
| 1307 | + int max_trying = W1_THERM_MAX_TRY; |
|---|
| 1308 | + int t_write, ret = -ENODEV; |
|---|
| 1309 | + bool strong_pullup; |
|---|
| 1310 | + |
|---|
| 1311 | + if (!sl->family_data) |
|---|
| 1312 | + goto error; |
|---|
| 1313 | + |
|---|
| 1314 | + t_write = W1_THERM_EEPROM_WRITE_DELAY; |
|---|
| 1315 | + strong_pullup = (w1_strong_pullup == 2 || |
|---|
| 1316 | + (!SLAVE_POWERMODE(sl) && |
|---|
| 1317 | + w1_strong_pullup)); |
|---|
| 1318 | + |
|---|
| 1319 | + /* prevent the slave from going away in sleep */ |
|---|
| 1320 | + atomic_inc(THERM_REFCNT(sl->family_data)); |
|---|
| 1321 | + |
|---|
| 1322 | + if (!bus_mutex_lock(&dev_master->bus_mutex)) { |
|---|
| 1323 | + ret = -EAGAIN; /* Didn't acquire the mutex */ |
|---|
| 1324 | + goto dec_refcnt; |
|---|
| 1325 | + } |
|---|
| 1326 | + |
|---|
| 1327 | + while (max_trying-- && ret) { /* ret should be 0 */ |
|---|
| 1328 | + /* safe version to select slave */ |
|---|
| 1329 | + if (!reset_select_slave(sl)) { |
|---|
| 1330 | + unsigned long sleep_rem; |
|---|
| 1331 | + |
|---|
| 1332 | + /* 10ms strong pullup (or delay) after the convert */ |
|---|
| 1333 | + if (strong_pullup) |
|---|
| 1334 | + w1_next_pullup(dev_master, t_write); |
|---|
| 1335 | + |
|---|
| 1336 | + w1_write_8(dev_master, W1_COPY_SCRATCHPAD); |
|---|
| 1337 | + |
|---|
| 1338 | + if (strong_pullup) { |
|---|
| 1339 | + sleep_rem = msleep_interruptible(t_write); |
|---|
| 546 | 1340 | if (sleep_rem != 0) { |
|---|
| 547 | 1341 | ret = -EINTR; |
|---|
| 548 | 1342 | goto mt_unlock; |
|---|
| 549 | 1343 | } |
|---|
| 550 | 1344 | } |
|---|
| 551 | | - |
|---|
| 552 | | - if (!w1_reset_select_slave(sl)) { |
|---|
| 553 | | - |
|---|
| 554 | | - w1_write_8(dev, W1_READ_SCRATCHPAD); |
|---|
| 555 | | - count = w1_read_block(dev, info->rom, 9); |
|---|
| 556 | | - if (count != 9) { |
|---|
| 557 | | - dev_warn(device, "w1_read_block() " |
|---|
| 558 | | - "returned %u instead of 9.\n", |
|---|
| 559 | | - count); |
|---|
| 560 | | - } |
|---|
| 561 | | - |
|---|
| 562 | | - info->crc = w1_calc_crc8(info->rom, 8); |
|---|
| 563 | | - |
|---|
| 564 | | - if (info->rom[8] == info->crc) |
|---|
| 565 | | - info->verdict = 1; |
|---|
| 566 | | - } |
|---|
| 1345 | + ret = 0; |
|---|
| 567 | 1346 | } |
|---|
| 568 | 1347 | |
|---|
| 569 | | - if (info->verdict) |
|---|
| 570 | | - break; |
|---|
| 571 | 1348 | } |
|---|
| 572 | 1349 | |
|---|
| 573 | 1350 | mt_unlock: |
|---|
| 574 | | - mutex_unlock(&dev->bus_mutex); |
|---|
| 1351 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 575 | 1352 | dec_refcnt: |
|---|
| 576 | | - atomic_dec(THERM_REFCNT(family_data)); |
|---|
| 1353 | + atomic_dec(THERM_REFCNT(sl->family_data)); |
|---|
| 577 | 1354 | error: |
|---|
| 578 | 1355 | return ret; |
|---|
| 579 | 1356 | } |
|---|
| 1357 | + |
|---|
| 1358 | +static int recall_eeprom(struct w1_slave *sl) |
|---|
| 1359 | +{ |
|---|
| 1360 | + struct w1_master *dev_master = sl->master; |
|---|
| 1361 | + int max_trying = W1_THERM_MAX_TRY; |
|---|
| 1362 | + int ret = -ENODEV; |
|---|
| 1363 | + |
|---|
| 1364 | + if (!sl->family_data) |
|---|
| 1365 | + goto error; |
|---|
| 1366 | + |
|---|
| 1367 | + /* prevent the slave from going away in sleep */ |
|---|
| 1368 | + atomic_inc(THERM_REFCNT(sl->family_data)); |
|---|
| 1369 | + |
|---|
| 1370 | + if (!bus_mutex_lock(&dev_master->bus_mutex)) { |
|---|
| 1371 | + ret = -EAGAIN; /* Didn't acquire the mutex */ |
|---|
| 1372 | + goto dec_refcnt; |
|---|
| 1373 | + } |
|---|
| 1374 | + |
|---|
| 1375 | + while (max_trying-- && ret) { /* ret should be 0 */ |
|---|
| 1376 | + /* safe version to select slave */ |
|---|
| 1377 | + if (!reset_select_slave(sl)) { |
|---|
| 1378 | + |
|---|
| 1379 | + w1_write_8(dev_master, W1_RECALL_EEPROM); |
|---|
| 1380 | + ret = w1_poll_completion(dev_master, W1_POLL_RECALL_EEPROM); |
|---|
| 1381 | + } |
|---|
| 1382 | + |
|---|
| 1383 | + } |
|---|
| 1384 | + |
|---|
| 1385 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1386 | + |
|---|
| 1387 | +dec_refcnt: |
|---|
| 1388 | + atomic_dec(THERM_REFCNT(sl->family_data)); |
|---|
| 1389 | +error: |
|---|
| 1390 | + return ret; |
|---|
| 1391 | +} |
|---|
| 1392 | + |
|---|
| 1393 | +static int read_powermode(struct w1_slave *sl) |
|---|
| 1394 | +{ |
|---|
| 1395 | + struct w1_master *dev_master = sl->master; |
|---|
| 1396 | + int max_trying = W1_THERM_MAX_TRY; |
|---|
| 1397 | + int ret = -ENODEV; |
|---|
| 1398 | + |
|---|
| 1399 | + if (!sl->family_data) |
|---|
| 1400 | + goto error; |
|---|
| 1401 | + |
|---|
| 1402 | + /* prevent the slave from going away in sleep */ |
|---|
| 1403 | + atomic_inc(THERM_REFCNT(sl->family_data)); |
|---|
| 1404 | + |
|---|
| 1405 | + if (!bus_mutex_lock(&dev_master->bus_mutex)) { |
|---|
| 1406 | + ret = -EAGAIN; /* Didn't acquire the mutex */ |
|---|
| 1407 | + goto dec_refcnt; |
|---|
| 1408 | + } |
|---|
| 1409 | + |
|---|
| 1410 | + while ((max_trying--) && (ret < 0)) { |
|---|
| 1411 | + /* safe version to select slave */ |
|---|
| 1412 | + if (!reset_select_slave(sl)) { |
|---|
| 1413 | + w1_write_8(dev_master, W1_READ_PSUPPLY); |
|---|
| 1414 | + /* |
|---|
| 1415 | + * Emit a read time slot and read only one bit, |
|---|
| 1416 | + * 1 is externally powered, |
|---|
| 1417 | + * 0 is parasite powered |
|---|
| 1418 | + */ |
|---|
| 1419 | + ret = w1_touch_bit(dev_master, 1); |
|---|
| 1420 | + /* ret should be either 1 either 0 */ |
|---|
| 1421 | + } |
|---|
| 1422 | + } |
|---|
| 1423 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1424 | + |
|---|
| 1425 | +dec_refcnt: |
|---|
| 1426 | + atomic_dec(THERM_REFCNT(sl->family_data)); |
|---|
| 1427 | +error: |
|---|
| 1428 | + return ret; |
|---|
| 1429 | +} |
|---|
| 1430 | + |
|---|
| 1431 | +static int trigger_bulk_read(struct w1_master *dev_master) |
|---|
| 1432 | +{ |
|---|
| 1433 | + struct w1_slave *sl = NULL; /* used to iterate through slaves */ |
|---|
| 1434 | + int max_trying = W1_THERM_MAX_TRY; |
|---|
| 1435 | + int t_conv = 0; |
|---|
| 1436 | + int ret = -ENODEV; |
|---|
| 1437 | + bool strong_pullup = false; |
|---|
| 1438 | + |
|---|
| 1439 | + /* |
|---|
| 1440 | + * Check whether there are parasite powered device on the bus, |
|---|
| 1441 | + * and compute duration of conversion for these devices |
|---|
| 1442 | + * so we can apply a strong pullup if required |
|---|
| 1443 | + */ |
|---|
| 1444 | + list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) { |
|---|
| 1445 | + if (!sl->family_data) |
|---|
| 1446 | + goto error; |
|---|
| 1447 | + if (bulk_read_support(sl)) { |
|---|
| 1448 | + int t_cur = conversion_time(sl); |
|---|
| 1449 | + |
|---|
| 1450 | + t_conv = t_cur > t_conv ? t_cur : t_conv; |
|---|
| 1451 | + strong_pullup = strong_pullup || |
|---|
| 1452 | + (w1_strong_pullup == 2 || |
|---|
| 1453 | + (!SLAVE_POWERMODE(sl) && |
|---|
| 1454 | + w1_strong_pullup)); |
|---|
| 1455 | + } |
|---|
| 1456 | + } |
|---|
| 1457 | + |
|---|
| 1458 | + /* |
|---|
| 1459 | + * t_conv is the max conversion time required on the bus |
|---|
| 1460 | + * If its 0, no device support the bulk read feature |
|---|
| 1461 | + */ |
|---|
| 1462 | + if (!t_conv) |
|---|
| 1463 | + goto error; |
|---|
| 1464 | + |
|---|
| 1465 | + if (!bus_mutex_lock(&dev_master->bus_mutex)) { |
|---|
| 1466 | + ret = -EAGAIN; /* Didn't acquire the mutex */ |
|---|
| 1467 | + goto error; |
|---|
| 1468 | + } |
|---|
| 1469 | + |
|---|
| 1470 | + while ((max_trying--) && (ret < 0)) { /* ret should be either 0 */ |
|---|
| 1471 | + |
|---|
| 1472 | + if (!w1_reset_bus(dev_master)) { /* Just reset the bus */ |
|---|
| 1473 | + unsigned long sleep_rem; |
|---|
| 1474 | + |
|---|
| 1475 | + w1_write_8(dev_master, W1_SKIP_ROM); |
|---|
| 1476 | + |
|---|
| 1477 | + if (strong_pullup) /* Apply pullup if required */ |
|---|
| 1478 | + w1_next_pullup(dev_master, t_conv); |
|---|
| 1479 | + |
|---|
| 1480 | + w1_write_8(dev_master, W1_CONVERT_TEMP); |
|---|
| 1481 | + |
|---|
| 1482 | + /* set a flag to instruct that converT pending */ |
|---|
| 1483 | + list_for_each_entry(sl, |
|---|
| 1484 | + &dev_master->slist, w1_slave_entry) { |
|---|
| 1485 | + if (bulk_read_support(sl)) |
|---|
| 1486 | + SLAVE_CONVERT_TRIGGERED(sl) = -1; |
|---|
| 1487 | + } |
|---|
| 1488 | + |
|---|
| 1489 | + if (strong_pullup) { /* some device need pullup */ |
|---|
| 1490 | + sleep_rem = msleep_interruptible(t_conv); |
|---|
| 1491 | + if (sleep_rem != 0) { |
|---|
| 1492 | + ret = -EINTR; |
|---|
| 1493 | + goto mt_unlock; |
|---|
| 1494 | + } |
|---|
| 1495 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1496 | + } else { |
|---|
| 1497 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1498 | + sleep_rem = msleep_interruptible(t_conv); |
|---|
| 1499 | + if (sleep_rem != 0) { |
|---|
| 1500 | + ret = -EINTR; |
|---|
| 1501 | + goto set_flag; |
|---|
| 1502 | + } |
|---|
| 1503 | + } |
|---|
| 1504 | + ret = 0; |
|---|
| 1505 | + goto set_flag; |
|---|
| 1506 | + } |
|---|
| 1507 | + } |
|---|
| 1508 | + |
|---|
| 1509 | +mt_unlock: |
|---|
| 1510 | + mutex_unlock(&dev_master->bus_mutex); |
|---|
| 1511 | +set_flag: |
|---|
| 1512 | + /* set a flag to register convsersion is done */ |
|---|
| 1513 | + list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) { |
|---|
| 1514 | + if (bulk_read_support(sl)) |
|---|
| 1515 | + SLAVE_CONVERT_TRIGGERED(sl) = 1; |
|---|
| 1516 | + } |
|---|
| 1517 | +error: |
|---|
| 1518 | + return ret; |
|---|
| 1519 | +} |
|---|
| 1520 | + |
|---|
| 1521 | +/* Sysfs Interface definition */ |
|---|
| 580 | 1522 | |
|---|
| 581 | 1523 | static ssize_t w1_slave_show(struct device *device, |
|---|
| 582 | 1524 | struct device_attribute *attr, char *buf) |
|---|
| .. | .. |
|---|
| 586 | 1528 | u8 *family_data = sl->family_data; |
|---|
| 587 | 1529 | int ret, i; |
|---|
| 588 | 1530 | ssize_t c = PAGE_SIZE; |
|---|
| 589 | | - u8 fid = sl->family->fid; |
|---|
| 590 | 1531 | |
|---|
| 591 | | - ret = read_therm(device, sl, &info); |
|---|
| 592 | | - if (ret) |
|---|
| 593 | | - return ret; |
|---|
| 1532 | + if (bulk_read_support(sl)) { |
|---|
| 1533 | + if (SLAVE_CONVERT_TRIGGERED(sl) < 0) { |
|---|
| 1534 | + dev_dbg(device, |
|---|
| 1535 | + "%s: Conversion in progress, retry later\n", |
|---|
| 1536 | + __func__); |
|---|
| 1537 | + return 0; |
|---|
| 1538 | + } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) { |
|---|
| 1539 | + /* A bulk read has been issued, read the device RAM */ |
|---|
| 1540 | + ret = read_scratchpad(sl, &info); |
|---|
| 1541 | + SLAVE_CONVERT_TRIGGERED(sl) = 0; |
|---|
| 1542 | + } else |
|---|
| 1543 | + ret = convert_t(sl, &info); |
|---|
| 1544 | + } else |
|---|
| 1545 | + ret = convert_t(sl, &info); |
|---|
| 1546 | + |
|---|
| 1547 | + if (ret < 0) { |
|---|
| 1548 | + dev_dbg(device, |
|---|
| 1549 | + "%s: Temperature data may be corrupted. err=%d\n", |
|---|
| 1550 | + __func__, ret); |
|---|
| 1551 | + return 0; |
|---|
| 1552 | + } |
|---|
| 594 | 1553 | |
|---|
| 595 | 1554 | for (i = 0; i < 9; ++i) |
|---|
| 596 | 1555 | c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]); |
|---|
| 597 | 1556 | c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n", |
|---|
| 598 | 1557 | info.crc, (info.verdict) ? "YES" : "NO"); |
|---|
| 1558 | + |
|---|
| 599 | 1559 | if (info.verdict) |
|---|
| 600 | 1560 | memcpy(family_data, info.rom, sizeof(info.rom)); |
|---|
| 601 | 1561 | else |
|---|
| 602 | | - dev_warn(device, "Read failed CRC check\n"); |
|---|
| 1562 | + dev_warn(device, "%s:Read failed CRC check\n", __func__); |
|---|
| 603 | 1563 | |
|---|
| 604 | 1564 | for (i = 0; i < 9; ++i) |
|---|
| 605 | 1565 | c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", |
|---|
| 606 | 1566 | ((u8 *)family_data)[i]); |
|---|
| 607 | 1567 | |
|---|
| 608 | 1568 | c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n", |
|---|
| 609 | | - w1_convert_temp(info.rom, fid)); |
|---|
| 1569 | + temperature_from_RAM(sl, info.rom)); |
|---|
| 1570 | + |
|---|
| 610 | 1571 | ret = PAGE_SIZE - c; |
|---|
| 611 | 1572 | return ret; |
|---|
| 1573 | +} |
|---|
| 1574 | + |
|---|
| 1575 | +static ssize_t w1_slave_store(struct device *device, |
|---|
| 1576 | + struct device_attribute *attr, const char *buf, |
|---|
| 1577 | + size_t size) |
|---|
| 1578 | +{ |
|---|
| 1579 | + int val, ret = 0; |
|---|
| 1580 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1581 | + |
|---|
| 1582 | + ret = kstrtoint(buf, 10, &val); /* converting user entry to int */ |
|---|
| 1583 | + |
|---|
| 1584 | + if (ret) { /* conversion error */ |
|---|
| 1585 | + dev_info(device, |
|---|
| 1586 | + "%s: conversion error. err= %d\n", __func__, ret); |
|---|
| 1587 | + return size; /* return size to avoid call back again */ |
|---|
| 1588 | + } |
|---|
| 1589 | + |
|---|
| 1590 | + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { |
|---|
| 1591 | + dev_info(device, |
|---|
| 1592 | + "%s: Device not supported by the driver\n", __func__); |
|---|
| 1593 | + return size; /* No device family */ |
|---|
| 1594 | + } |
|---|
| 1595 | + |
|---|
| 1596 | + if (val == 0) /* val=0 : trigger a EEPROM save */ |
|---|
| 1597 | + ret = copy_scratchpad(sl); |
|---|
| 1598 | + else { |
|---|
| 1599 | + if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution) |
|---|
| 1600 | + ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val); |
|---|
| 1601 | + } |
|---|
| 1602 | + |
|---|
| 1603 | + if (ret) { |
|---|
| 1604 | + dev_warn(device, "%s: Set resolution - error %d\n", __func__, ret); |
|---|
| 1605 | + /* Propagate error to userspace */ |
|---|
| 1606 | + return ret; |
|---|
| 1607 | + } |
|---|
| 1608 | + SLAVE_RESOLUTION(sl) = val; |
|---|
| 1609 | + /* Reset the conversion time to default - it depends on resolution */ |
|---|
| 1610 | + SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT; |
|---|
| 1611 | + |
|---|
| 1612 | + return size; /* always return size to avoid infinite calling */ |
|---|
| 1613 | +} |
|---|
| 1614 | + |
|---|
| 1615 | +static ssize_t temperature_show(struct device *device, |
|---|
| 1616 | + struct device_attribute *attr, char *buf) |
|---|
| 1617 | +{ |
|---|
| 1618 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1619 | + struct therm_info info; |
|---|
| 1620 | + int ret = 0; |
|---|
| 1621 | + |
|---|
| 1622 | + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { |
|---|
| 1623 | + dev_info(device, |
|---|
| 1624 | + "%s: Device not supported by the driver\n", __func__); |
|---|
| 1625 | + return 0; /* No device family */ |
|---|
| 1626 | + } |
|---|
| 1627 | + |
|---|
| 1628 | + if (bulk_read_support(sl)) { |
|---|
| 1629 | + if (SLAVE_CONVERT_TRIGGERED(sl) < 0) { |
|---|
| 1630 | + dev_dbg(device, |
|---|
| 1631 | + "%s: Conversion in progress, retry later\n", |
|---|
| 1632 | + __func__); |
|---|
| 1633 | + return 0; |
|---|
| 1634 | + } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) { |
|---|
| 1635 | + /* A bulk read has been issued, read the device RAM */ |
|---|
| 1636 | + ret = read_scratchpad(sl, &info); |
|---|
| 1637 | + SLAVE_CONVERT_TRIGGERED(sl) = 0; |
|---|
| 1638 | + } else |
|---|
| 1639 | + ret = convert_t(sl, &info); |
|---|
| 1640 | + } else |
|---|
| 1641 | + ret = convert_t(sl, &info); |
|---|
| 1642 | + |
|---|
| 1643 | + if (ret < 0) { |
|---|
| 1644 | + dev_dbg(device, |
|---|
| 1645 | + "%s: Temperature data may be corrupted. err=%d\n", |
|---|
| 1646 | + __func__, ret); |
|---|
| 1647 | + return 0; |
|---|
| 1648 | + } |
|---|
| 1649 | + |
|---|
| 1650 | + return sprintf(buf, "%d\n", temperature_from_RAM(sl, info.rom)); |
|---|
| 1651 | +} |
|---|
| 1652 | + |
|---|
| 1653 | +static ssize_t ext_power_show(struct device *device, |
|---|
| 1654 | + struct device_attribute *attr, char *buf) |
|---|
| 1655 | +{ |
|---|
| 1656 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1657 | + |
|---|
| 1658 | + if (!sl->family_data) { |
|---|
| 1659 | + dev_info(device, |
|---|
| 1660 | + "%s: Device not supported by the driver\n", __func__); |
|---|
| 1661 | + return 0; /* No device family */ |
|---|
| 1662 | + } |
|---|
| 1663 | + |
|---|
| 1664 | + /* Getting the power mode of the device {external, parasite} */ |
|---|
| 1665 | + SLAVE_POWERMODE(sl) = read_powermode(sl); |
|---|
| 1666 | + |
|---|
| 1667 | + if (SLAVE_POWERMODE(sl) < 0) { |
|---|
| 1668 | + dev_dbg(device, |
|---|
| 1669 | + "%s: Power_mode may be corrupted. err=%d\n", |
|---|
| 1670 | + __func__, SLAVE_POWERMODE(sl)); |
|---|
| 1671 | + } |
|---|
| 1672 | + return sprintf(buf, "%d\n", SLAVE_POWERMODE(sl)); |
|---|
| 1673 | +} |
|---|
| 1674 | + |
|---|
| 1675 | +static ssize_t resolution_show(struct device *device, |
|---|
| 1676 | + struct device_attribute *attr, char *buf) |
|---|
| 1677 | +{ |
|---|
| 1678 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1679 | + |
|---|
| 1680 | + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { |
|---|
| 1681 | + dev_info(device, |
|---|
| 1682 | + "%s: Device not supported by the driver\n", __func__); |
|---|
| 1683 | + return 0; /* No device family */ |
|---|
| 1684 | + } |
|---|
| 1685 | + |
|---|
| 1686 | + /* get the correct function depending on the device */ |
|---|
| 1687 | + SLAVE_RESOLUTION(sl) = SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl); |
|---|
| 1688 | + if (SLAVE_RESOLUTION(sl) < 0) { |
|---|
| 1689 | + dev_dbg(device, |
|---|
| 1690 | + "%s: Resolution may be corrupted. err=%d\n", |
|---|
| 1691 | + __func__, SLAVE_RESOLUTION(sl)); |
|---|
| 1692 | + } |
|---|
| 1693 | + |
|---|
| 1694 | + return sprintf(buf, "%d\n", SLAVE_RESOLUTION(sl)); |
|---|
| 1695 | +} |
|---|
| 1696 | + |
|---|
| 1697 | +static ssize_t resolution_store(struct device *device, |
|---|
| 1698 | + struct device_attribute *attr, const char *buf, size_t size) |
|---|
| 1699 | +{ |
|---|
| 1700 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1701 | + int val; |
|---|
| 1702 | + int ret = 0; |
|---|
| 1703 | + |
|---|
| 1704 | + ret = kstrtoint(buf, 10, &val); /* converting user entry to int */ |
|---|
| 1705 | + |
|---|
| 1706 | + if (ret) { /* conversion error */ |
|---|
| 1707 | + dev_info(device, |
|---|
| 1708 | + "%s: conversion error. err= %d\n", __func__, ret); |
|---|
| 1709 | + return size; /* return size to avoid call back again */ |
|---|
| 1710 | + } |
|---|
| 1711 | + |
|---|
| 1712 | + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { |
|---|
| 1713 | + dev_info(device, |
|---|
| 1714 | + "%s: Device not supported by the driver\n", __func__); |
|---|
| 1715 | + return size; /* No device family */ |
|---|
| 1716 | + } |
|---|
| 1717 | + |
|---|
| 1718 | + /* |
|---|
| 1719 | + * Don't deal with the val enterd by user, |
|---|
| 1720 | + * only device knows what is correct or not |
|---|
| 1721 | + */ |
|---|
| 1722 | + |
|---|
| 1723 | + /* get the correct function depending on the device */ |
|---|
| 1724 | + ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val); |
|---|
| 1725 | + |
|---|
| 1726 | + if (ret) |
|---|
| 1727 | + return ret; |
|---|
| 1728 | + |
|---|
| 1729 | + SLAVE_RESOLUTION(sl) = val; |
|---|
| 1730 | + /* Reset the conversion time to default because it depends on resolution */ |
|---|
| 1731 | + SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT; |
|---|
| 1732 | + |
|---|
| 1733 | + return size; |
|---|
| 1734 | +} |
|---|
| 1735 | + |
|---|
| 1736 | +static ssize_t eeprom_store(struct device *device, |
|---|
| 1737 | + struct device_attribute *attr, const char *buf, size_t size) |
|---|
| 1738 | +{ |
|---|
| 1739 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1740 | + int ret = -EINVAL; /* Invalid argument */ |
|---|
| 1741 | + |
|---|
| 1742 | + if (size == sizeof(EEPROM_CMD_WRITE)) { |
|---|
| 1743 | + if (!strncmp(buf, EEPROM_CMD_WRITE, sizeof(EEPROM_CMD_WRITE)-1)) |
|---|
| 1744 | + ret = copy_scratchpad(sl); |
|---|
| 1745 | + } else if (size == sizeof(EEPROM_CMD_READ)) { |
|---|
| 1746 | + if (!strncmp(buf, EEPROM_CMD_READ, sizeof(EEPROM_CMD_READ)-1)) |
|---|
| 1747 | + ret = recall_eeprom(sl); |
|---|
| 1748 | + } |
|---|
| 1749 | + |
|---|
| 1750 | + if (ret) |
|---|
| 1751 | + dev_info(device, "%s: error in process %d\n", __func__, ret); |
|---|
| 1752 | + |
|---|
| 1753 | + return size; |
|---|
| 1754 | +} |
|---|
| 1755 | + |
|---|
| 1756 | +static ssize_t alarms_show(struct device *device, |
|---|
| 1757 | + struct device_attribute *attr, char *buf) |
|---|
| 1758 | +{ |
|---|
| 1759 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1760 | + int ret; |
|---|
| 1761 | + s8 th = 0, tl = 0; |
|---|
| 1762 | + struct therm_info scratchpad; |
|---|
| 1763 | + |
|---|
| 1764 | + ret = read_scratchpad(sl, &scratchpad); |
|---|
| 1765 | + |
|---|
| 1766 | + if (!ret) { |
|---|
| 1767 | + th = scratchpad.rom[2]; /* TH is byte 2 */ |
|---|
| 1768 | + tl = scratchpad.rom[3]; /* TL is byte 3 */ |
|---|
| 1769 | + } else { |
|---|
| 1770 | + dev_info(device, |
|---|
| 1771 | + "%s: error reading alarms register %d\n", |
|---|
| 1772 | + __func__, ret); |
|---|
| 1773 | + } |
|---|
| 1774 | + |
|---|
| 1775 | + return sprintf(buf, "%hd %hd\n", tl, th); |
|---|
| 1776 | +} |
|---|
| 1777 | + |
|---|
| 1778 | +static ssize_t alarms_store(struct device *device, |
|---|
| 1779 | + struct device_attribute *attr, const char *buf, size_t size) |
|---|
| 1780 | +{ |
|---|
| 1781 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1782 | + struct therm_info info; |
|---|
| 1783 | + u8 new_config_register[3]; /* array of data to be written */ |
|---|
| 1784 | + int temp, ret; |
|---|
| 1785 | + char *token = NULL; |
|---|
| 1786 | + s8 tl, th, tt; /* 1 byte per value + temp ring order */ |
|---|
| 1787 | + char *p_args, *orig; |
|---|
| 1788 | + |
|---|
| 1789 | + p_args = orig = kmalloc(size, GFP_KERNEL); |
|---|
| 1790 | + /* Safe string copys as buf is const */ |
|---|
| 1791 | + if (!p_args) { |
|---|
| 1792 | + dev_warn(device, |
|---|
| 1793 | + "%s: error unable to allocate memory %d\n", |
|---|
| 1794 | + __func__, -ENOMEM); |
|---|
| 1795 | + return size; |
|---|
| 1796 | + } |
|---|
| 1797 | + strcpy(p_args, buf); |
|---|
| 1798 | + |
|---|
| 1799 | + /* Split string using space char */ |
|---|
| 1800 | + token = strsep(&p_args, " "); |
|---|
| 1801 | + |
|---|
| 1802 | + if (!token) { |
|---|
| 1803 | + dev_info(device, |
|---|
| 1804 | + "%s: error parsing args %d\n", __func__, -EINVAL); |
|---|
| 1805 | + goto free_m; |
|---|
| 1806 | + } |
|---|
| 1807 | + |
|---|
| 1808 | + /* Convert 1st entry to int */ |
|---|
| 1809 | + ret = kstrtoint (token, 10, &temp); |
|---|
| 1810 | + if (ret) { |
|---|
| 1811 | + dev_info(device, |
|---|
| 1812 | + "%s: error parsing args %d\n", __func__, ret); |
|---|
| 1813 | + goto free_m; |
|---|
| 1814 | + } |
|---|
| 1815 | + |
|---|
| 1816 | + tl = int_to_short(temp); |
|---|
| 1817 | + |
|---|
| 1818 | + /* Split string using space char */ |
|---|
| 1819 | + token = strsep(&p_args, " "); |
|---|
| 1820 | + if (!token) { |
|---|
| 1821 | + dev_info(device, |
|---|
| 1822 | + "%s: error parsing args %d\n", __func__, -EINVAL); |
|---|
| 1823 | + goto free_m; |
|---|
| 1824 | + } |
|---|
| 1825 | + /* Convert 2nd entry to int */ |
|---|
| 1826 | + ret = kstrtoint (token, 10, &temp); |
|---|
| 1827 | + if (ret) { |
|---|
| 1828 | + dev_info(device, |
|---|
| 1829 | + "%s: error parsing args %d\n", __func__, ret); |
|---|
| 1830 | + goto free_m; |
|---|
| 1831 | + } |
|---|
| 1832 | + |
|---|
| 1833 | + /* Prepare to cast to short by eliminating out of range values */ |
|---|
| 1834 | + th = int_to_short(temp); |
|---|
| 1835 | + |
|---|
| 1836 | + /* Reorder if required th and tl */ |
|---|
| 1837 | + if (tl > th) { |
|---|
| 1838 | + tt = tl; tl = th; th = tt; |
|---|
| 1839 | + } |
|---|
| 1840 | + |
|---|
| 1841 | + /* |
|---|
| 1842 | + * Read the scratchpad to change only the required bits |
|---|
| 1843 | + * (th : byte 2 - tl: byte 3) |
|---|
| 1844 | + */ |
|---|
| 1845 | + ret = read_scratchpad(sl, &info); |
|---|
| 1846 | + if (!ret) { |
|---|
| 1847 | + new_config_register[0] = th; /* Byte 2 */ |
|---|
| 1848 | + new_config_register[1] = tl; /* Byte 3 */ |
|---|
| 1849 | + new_config_register[2] = info.rom[4];/* Byte 4 */ |
|---|
| 1850 | + } else { |
|---|
| 1851 | + dev_info(device, |
|---|
| 1852 | + "%s: error reading from the slave device %d\n", |
|---|
| 1853 | + __func__, ret); |
|---|
| 1854 | + goto free_m; |
|---|
| 1855 | + } |
|---|
| 1856 | + |
|---|
| 1857 | + /* Write data in the device RAM */ |
|---|
| 1858 | + if (!SLAVE_SPECIFIC_FUNC(sl)) { |
|---|
| 1859 | + dev_info(device, |
|---|
| 1860 | + "%s: Device not supported by the driver %d\n", |
|---|
| 1861 | + __func__, -ENODEV); |
|---|
| 1862 | + goto free_m; |
|---|
| 1863 | + } |
|---|
| 1864 | + |
|---|
| 1865 | + ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register); |
|---|
| 1866 | + if (ret) |
|---|
| 1867 | + dev_info(device, |
|---|
| 1868 | + "%s: error writing to the slave device %d\n", |
|---|
| 1869 | + __func__, ret); |
|---|
| 1870 | + |
|---|
| 1871 | +free_m: |
|---|
| 1872 | + /* free allocated memory */ |
|---|
| 1873 | + kfree(orig); |
|---|
| 1874 | + |
|---|
| 1875 | + return size; |
|---|
| 1876 | +} |
|---|
| 1877 | + |
|---|
| 1878 | +static ssize_t therm_bulk_read_store(struct device *device, |
|---|
| 1879 | + struct device_attribute *attr, const char *buf, size_t size) |
|---|
| 1880 | +{ |
|---|
| 1881 | + struct w1_master *dev_master = dev_to_w1_master(device); |
|---|
| 1882 | + int ret = -EINVAL; /* Invalid argument */ |
|---|
| 1883 | + |
|---|
| 1884 | + if (size == sizeof(BULK_TRIGGER_CMD)) |
|---|
| 1885 | + if (!strncmp(buf, BULK_TRIGGER_CMD, |
|---|
| 1886 | + sizeof(BULK_TRIGGER_CMD)-1)) |
|---|
| 1887 | + ret = trigger_bulk_read(dev_master); |
|---|
| 1888 | + |
|---|
| 1889 | + if (ret) |
|---|
| 1890 | + dev_info(device, |
|---|
| 1891 | + "%s: unable to trigger a bulk read on the bus. err=%d\n", |
|---|
| 1892 | + __func__, ret); |
|---|
| 1893 | + |
|---|
| 1894 | + return size; |
|---|
| 1895 | +} |
|---|
| 1896 | + |
|---|
| 1897 | +static ssize_t therm_bulk_read_show(struct device *device, |
|---|
| 1898 | + struct device_attribute *attr, char *buf) |
|---|
| 1899 | +{ |
|---|
| 1900 | + struct w1_master *dev_master = dev_to_w1_master(device); |
|---|
| 1901 | + struct w1_slave *sl = NULL; |
|---|
| 1902 | + int ret = 0; |
|---|
| 1903 | + |
|---|
| 1904 | + list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) { |
|---|
| 1905 | + if (sl->family_data) { |
|---|
| 1906 | + if (bulk_read_support(sl)) { |
|---|
| 1907 | + if (SLAVE_CONVERT_TRIGGERED(sl) == -1) { |
|---|
| 1908 | + ret = -1; |
|---|
| 1909 | + goto show_result; |
|---|
| 1910 | + } |
|---|
| 1911 | + if (SLAVE_CONVERT_TRIGGERED(sl) == 1) |
|---|
| 1912 | + /* continue to check other slaves */ |
|---|
| 1913 | + ret = 1; |
|---|
| 1914 | + } |
|---|
| 1915 | + } |
|---|
| 1916 | + } |
|---|
| 1917 | +show_result: |
|---|
| 1918 | + return sprintf(buf, "%d\n", ret); |
|---|
| 1919 | +} |
|---|
| 1920 | + |
|---|
| 1921 | +static ssize_t conv_time_show(struct device *device, |
|---|
| 1922 | + struct device_attribute *attr, char *buf) |
|---|
| 1923 | +{ |
|---|
| 1924 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1925 | + |
|---|
| 1926 | + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { |
|---|
| 1927 | + dev_info(device, |
|---|
| 1928 | + "%s: Device is not supported by the driver\n", __func__); |
|---|
| 1929 | + return 0; /* No device family */ |
|---|
| 1930 | + } |
|---|
| 1931 | + return sprintf(buf, "%d\n", conversion_time(sl)); |
|---|
| 1932 | +} |
|---|
| 1933 | + |
|---|
| 1934 | +static ssize_t conv_time_store(struct device *device, |
|---|
| 1935 | + struct device_attribute *attr, const char *buf, size_t size) |
|---|
| 1936 | +{ |
|---|
| 1937 | + int val, ret = 0; |
|---|
| 1938 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1939 | + |
|---|
| 1940 | + if (kstrtoint(buf, 10, &val)) /* converting user entry to int */ |
|---|
| 1941 | + return -EINVAL; |
|---|
| 1942 | + |
|---|
| 1943 | + if (check_family_data(sl)) |
|---|
| 1944 | + return -ENODEV; |
|---|
| 1945 | + |
|---|
| 1946 | + if (val != CONV_TIME_MEASURE) { |
|---|
| 1947 | + if (val >= CONV_TIME_DEFAULT) |
|---|
| 1948 | + SLAVE_CONV_TIME_OVERRIDE(sl) = val; |
|---|
| 1949 | + else |
|---|
| 1950 | + return -EINVAL; |
|---|
| 1951 | + |
|---|
| 1952 | + } else { |
|---|
| 1953 | + int conv_time; |
|---|
| 1954 | + |
|---|
| 1955 | + ret = conv_time_measure(sl, &conv_time); |
|---|
| 1956 | + if (ret) |
|---|
| 1957 | + return -EIO; |
|---|
| 1958 | + SLAVE_CONV_TIME_OVERRIDE(sl) = conv_time; |
|---|
| 1959 | + } |
|---|
| 1960 | + return size; |
|---|
| 1961 | +} |
|---|
| 1962 | + |
|---|
| 1963 | +static ssize_t features_show(struct device *device, |
|---|
| 1964 | + struct device_attribute *attr, char *buf) |
|---|
| 1965 | +{ |
|---|
| 1966 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1967 | + |
|---|
| 1968 | + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { |
|---|
| 1969 | + dev_info(device, |
|---|
| 1970 | + "%s: Device not supported by the driver\n", __func__); |
|---|
| 1971 | + return 0; /* No device family */ |
|---|
| 1972 | + } |
|---|
| 1973 | + return sprintf(buf, "%u\n", SLAVE_FEATURES(sl)); |
|---|
| 1974 | +} |
|---|
| 1975 | + |
|---|
| 1976 | +static ssize_t features_store(struct device *device, |
|---|
| 1977 | + struct device_attribute *attr, const char *buf, size_t size) |
|---|
| 1978 | +{ |
|---|
| 1979 | + int val, ret = 0; |
|---|
| 1980 | + bool strong_pullup; |
|---|
| 1981 | + struct w1_slave *sl = dev_to_w1_slave(device); |
|---|
| 1982 | + |
|---|
| 1983 | + ret = kstrtouint(buf, 10, &val); /* converting user entry to int */ |
|---|
| 1984 | + if (ret) |
|---|
| 1985 | + return -EINVAL; /* invalid number */ |
|---|
| 1986 | + |
|---|
| 1987 | + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { |
|---|
| 1988 | + dev_info(device, "%s: Device not supported by the driver\n", __func__); |
|---|
| 1989 | + return -ENODEV; |
|---|
| 1990 | + } |
|---|
| 1991 | + |
|---|
| 1992 | + if ((val & W1_THERM_FEATURES_MASK) != val) |
|---|
| 1993 | + return -EINVAL; |
|---|
| 1994 | + |
|---|
| 1995 | + SLAVE_FEATURES(sl) = val; |
|---|
| 1996 | + |
|---|
| 1997 | + strong_pullup = (w1_strong_pullup == 2 || |
|---|
| 1998 | + (!SLAVE_POWERMODE(sl) && |
|---|
| 1999 | + w1_strong_pullup)); |
|---|
| 2000 | + |
|---|
| 2001 | + if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) { |
|---|
| 2002 | + dev_warn(&sl->dev, |
|---|
| 2003 | + "%s: W1_THERM_POLL_COMPLETION disabled in parasite power mode.\n", |
|---|
| 2004 | + __func__); |
|---|
| 2005 | + SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION; |
|---|
| 2006 | + } |
|---|
| 2007 | + |
|---|
| 2008 | + return size; |
|---|
| 612 | 2009 | } |
|---|
| 613 | 2010 | |
|---|
| 614 | 2011 | #if IS_REACHABLE(CONFIG_HWMON) |
|---|
| .. | .. |
|---|
| 617 | 2014 | { |
|---|
| 618 | 2015 | struct w1_slave *sl = dev_get_drvdata(device); |
|---|
| 619 | 2016 | struct therm_info info; |
|---|
| 620 | | - u8 fid = sl->family->fid; |
|---|
| 621 | 2017 | int ret; |
|---|
| 622 | 2018 | |
|---|
| 623 | 2019 | switch (attr) { |
|---|
| 624 | 2020 | case hwmon_temp_input: |
|---|
| 625 | | - ret = read_therm(device, sl, &info); |
|---|
| 2021 | + ret = convert_t(sl, &info); |
|---|
| 626 | 2022 | if (ret) |
|---|
| 627 | 2023 | return ret; |
|---|
| 628 | 2024 | |
|---|
| .. | .. |
|---|
| 631 | 2027 | return ret; |
|---|
| 632 | 2028 | } |
|---|
| 633 | 2029 | |
|---|
| 634 | | - *val = w1_convert_temp(info.rom, fid); |
|---|
| 2030 | + *val = temperature_from_RAM(sl, info.rom); |
|---|
| 635 | 2031 | ret = 0; |
|---|
| 636 | 2032 | break; |
|---|
| 637 | 2033 | default: |
|---|
| .. | .. |
|---|
| 680 | 2076 | if (ack != W1_42_SUCCESS_CONFIRM_BYTE) |
|---|
| 681 | 2077 | goto error; |
|---|
| 682 | 2078 | |
|---|
| 683 | | - /* In case the bus fails to send 0xFF, limit*/ |
|---|
| 2079 | + /* In case the bus fails to send 0xFF, limit */ |
|---|
| 684 | 2080 | for (i = 0; i <= 64; i++) { |
|---|
| 685 | 2081 | if (w1_reset_bus(sl->master)) |
|---|
| 686 | 2082 | goto error; |
|---|
| .. | .. |
|---|
| 693 | 2089 | if (sl->reg_num.id == reg_num->id) |
|---|
| 694 | 2090 | seq = i; |
|---|
| 695 | 2091 | |
|---|
| 2092 | + if (w1_reset_bus(sl->master)) |
|---|
| 2093 | + goto error; |
|---|
| 2094 | + |
|---|
| 2095 | + /* Put the device into chain DONE state */ |
|---|
| 2096 | + w1_write_8(sl->master, W1_MATCH_ROM); |
|---|
| 2097 | + w1_write_block(sl->master, (u8 *)&rn, 8); |
|---|
| 696 | 2098 | w1_write_8(sl->master, W1_42_CHAIN); |
|---|
| 697 | 2099 | w1_write_8(sl->master, W1_42_CHAIN_DONE); |
|---|
| 698 | 2100 | w1_write_8(sl->master, W1_42_CHAIN_DONE_INV); |
|---|
| 699 | | - w1_read_block(sl->master, &ack, sizeof(ack)); |
|---|
| 700 | 2101 | |
|---|
| 701 | 2102 | /* check for acknowledgment */ |
|---|
| 702 | 2103 | ack = w1_read_8(sl->master); |
|---|
| 703 | 2104 | if (ack != W1_42_SUCCESS_CONFIRM_BYTE) |
|---|
| 704 | 2105 | goto error; |
|---|
| 705 | | - |
|---|
| 706 | 2106 | } |
|---|
| 707 | 2107 | |
|---|
| 708 | 2108 | /* Exit from CHAIN state */ |
|---|