Lines Matching +full:slave +full:- +full:kernel

1 // SPDX-License-Identifier: GPL-2.0-or-later
10 #include <linux/kernel.h>
35 * In case the parasite power-detection is not working (seems to be the case
40 * - strong_pullup = 0 Disable strong pullup completely
41 * - strong_pullup = 1 Enable automatic strong pullup detection
42 * - strong_pullup = 2 Force strong pullup
66 #define MIN_TEMP -55 /* min temperature that can be measured */
98 * return a pointer on the slave w1_therm_family_converter struct:
102 (((struct w1_therm_family_data *)(sl->family_data))->specific_functions)
105 * return the power mode of the sl slave : 1-ext, 0-parasite, <0 unknown
109 (((struct w1_therm_family_data *)(sl->family_data))->external_powered)
112 * return the resolution in bit of the sl slave : <0 unknown
116 (((struct w1_therm_family_data *)(sl->family_data))->resolution)
119 * return the conv_time_override of the sl slave
123 (((struct w1_therm_family_data *)(sl->family_data))->conv_time_override)
126 * return the features of the sl slave
130 (((struct w1_therm_family_data *)(sl->family_data))->features)
133 * return whether or not a converT command has been issued to the slave
135 * * -1: conversion is in progress
139 (((struct w1_therm_family_data *)(sl->family_data))->convert_triggered)
143 (&((struct w1_therm_family_data *)family_data)->refcnt)
148 * struct w1_therm_family_converter - bind device specific functions
149 * @broken: flag for non-registred families
172 * struct w1_therm_family_data - device data
177 * -x error or undefined
181 * @features: bit mask - enable temperature validity check, poll for completion
196 * struct therm_info - store temperature reading
210 * reset_select_slave() - reset and select a slave
211 * @sl: the slave to select
213 * Resets the bus and select the slave by sending a ROM MATCH cmd
216 * At the beginning of the such process, sl->master->slave_count is 1 even if
221 * Return: 0 if success, negative kernel error code otherwise.
226 * convert_t() - Query the device for temperature conversion and read
227 * @sl: pointer to the slave to read
230 * Return: 0 if success, -kernel error code otherwise
235 * read_scratchpad() - read the data in device RAM
236 * @sl: pointer to the slave to read
239 * Return: 0 if success, -kernel error code otherwise
244 * write_scratchpad() - write nb_bytes in the device RAM
245 * @sl: pointer to the slave to write in
249 * Return: 0 if success, -kernel error code otherwise
254 * copy_scratchpad() - Copy the content of scratchpad in device EEPROM
255 * @sl: slave involved
257 * Return: 0 if success, -kernel error code otherwise
262 * recall_eeprom() - Restore EEPROM data to device RAM
263 * @sl: slave involved
265 * Return: 0 if success, -kernel error code otherwise
270 * read_powermode() - Query the power mode of the slave
271 * @sl: slave to retrieve the power mode
279 * * <0 kernel error code
284 * trigger_bulk_read() - function to trigger a bulk read on the bus
288 * It also set the status flag in each slave &struct w1_therm_family_data
291 * Return: 0 if success, -kernel error code otherwise
363 * w1_therm_add_slave() - Called when a new slave is discovered
364 * @sl: slave just discovered by the master.
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.
369 * Return: 0 - If success, negative kernel code otherwise
374 * w1_therm_remove_slave() - Called when a slave is removed
375 * @sl: slave to be removed.
377 * Called by the master when the slave is considered not to be on the bus
443 return -EOPNOTSUPP;
522 if (!sl->family_data)
523 return -ENODEV; /* device unknown */
548 ret = 1600; /* GX20MH01 only. Datasheet says 1000ms - not enough */
558 if (!sl->family_data)
559 return -ENODEV; /* device unknown */
571 if (!sl->family_data)
572 return -ENODEV; /* device unknown */
624 return -EINVAL;
627 val = (val - W1_THERM_RESOLUTION_MIN) << W1_THERM_RESOLUTION_SHIFT;
660 return -EIO;
688 * w1_DS18B20_convert_temp() - temperature computation for DS18B20
700 /* Signed 16-bit value to unsigned, cpu order */
703 /* Config register bit R2 = 1 - GX20MH01 in 13 or 14 bit resolution mode */
708 t = (s16) bv; /* Degrees, lowest bit is 2^-6 */
709 return (int)t * 1000 / 64; /* Sign-extend to int; millidegrees */
711 t = (s16)bv; /* Degrees, lowest bit is 2^-4 */
712 return (int)t * 1000 / 16; /* Sign-extend to int; millidegrees */
716 * w1_DS18S20_convert_temp() - temperature computation for DS18S20
735 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
737 t -= 250;
738 h = 1000*((s32)rom[7] - (s32)rom[6]);
746 * w1_DS1825_convert_temp() - temperature computation for DS1825
760 /* Signed 16-bit value to unsigned, cpu order */
763 /* Config register bit 7 = 1 - MA31850 found, 14 bit resolution */
767 bv = (bv & 0xFFFC); /* Degrees, lowest 4 bits are 2^-1, 2^-2 and 2 zero bits */
769 t = (s16)bv; /* Degrees, lowest bit is 2^-4 */
770 return (int)t * 1000 / 16; /* Sign-extend to int; millidegrees */
829 * device_family() - Retrieve a pointer on &struct w1_therm_family_converter
830 * @sl: slave to retrieve the device specific structure
840 if (w1_therm_families[i].f->fid == sl->family->fid) {
849 * bus_mutex_lock() - Acquire the mutex
867 max_trying--;
877 * check_family_data() - Check if family data and specific functions are present
880 * Return: 0 - OK, negative value - error
884 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
885 dev_info(&sl->dev,
887 return -EINVAL; /* No device family */
893 * bulk_read_support() - check if slave support bulk read
901 return SLAVE_SPECIFIC_FUNC(sl)->bulk_read;
903 dev_info(&sl->dev,
910 * conversion_time() - get the Tconv for the slave
914 * on the resolution setting. This helper function get the slave timing,
917 * Return: conversion time in ms, negative values are kernel error code
922 return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl);
924 dev_info(&sl->dev,
927 return -ENODEV; /* No device family */
931 * temperature_from_RAM() - Convert the read info to temperature
933 * @rom: read value on the slave device RAM
942 return SLAVE_SPECIFIC_FUNC(sl)->convert(rom);
944 dev_info(&sl->dev,
951 * int_to_short() - Safe casting of int to short
976 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
978 if (!sl->family_data)
979 return -ENOMEM;
981 atomic_set(THERM_REFCNT(sl->family_data), 1);
986 kfree(sl->family_data);
987 return -ENODEV;
998 int err = device_create_file(&sl->master->dev,
1002 dev_warn(&sl->dev,
1015 dev_warn(&sl->dev,
1021 if (SLAVE_SPECIFIC_FUNC(sl)->get_resolution) {
1023 SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
1026 dev_warn(&sl->dev,
1040 int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
1043 bulk_read_device_counter--;
1046 device_remove_file(&sl->master->dev,
1052 refcnt = atomic_read(THERM_REFCNT(sl->family_data));
1054 kfree(sl->family_data);
1055 sl->family_data = NULL;
1060 /* Safe version of reset_select_slave - avoid using the one in w_io.c */
1064 u64 rn = le64_to_cpu(*((u64 *)&sl->reg_num));
1066 if (w1_reset_bus(sl->master))
1067 return -ENODEV;
1070 w1_write_block(sl->master, match, 9);
1076 * w1_poll_completion - Poll for operation completion, with timeout
1083 * Return: 0 - OK, negative error - timeout
1098 return -EIO;
1105 struct w1_master *dev_master = sl->master;
1108 int ret = -ENODEV;
1111 if (!sl->family_data)
1119 dev_warn(&sl->dev,
1128 memset(info->rom, 0, sizeof(info->rom));
1130 /* prevent the slave from going away in sleep */
1131 atomic_inc(THERM_REFCNT(sl->family_data));
1133 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1134 ret = -EAGAIN; /* Didn't acquire the mutex */
1138 while (max_trying-- && ret) { /* ret should be 0 */
1140 info->verdict = 0;
1141 info->crc = 0;
1142 /* safe version to select slave */
1155 dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
1158 mutex_unlock(&dev_master->bus_mutex);
1162 ret = -EINTR;
1165 mutex_unlock(&dev_master->bus_mutex);
1167 mutex_unlock(&dev_master->bus_mutex);
1170 ret = -EINTR;
1178 (info->rom[6] == 0xC) &&
1179 ((info->rom[1] == 0x5 && info->rom[0] == 0x50) ||
1180 (info->rom[1] == 0x7 && info->rom[0] == 0xFF))
1186 ret = -EIO;
1195 mutex_unlock(&dev_master->bus_mutex);
1197 atomic_dec(THERM_REFCNT(sl->family_data));
1206 struct w1_master *dev_master = sl->master;
1208 int ret = -ENODEV;
1211 if (!sl->family_data)
1220 return -EINVAL;
1223 memset(info->rom, 0, sizeof(info->rom));
1225 /* prevent the slave from going away in sleep */
1226 atomic_inc(THERM_REFCNT(sl->family_data));
1228 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1229 ret = -EAGAIN; /* Didn't acquire the mutex */
1233 while (max_trying-- && ret) { /* ret should be 0 */
1234 info->verdict = 0;
1235 info->crc = 0;
1236 /* safe version to select slave */
1246 dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
1251 *conv_time = jiffies_to_msecs(j_end-j_start)*12/10;
1255 ret = -EIO;
1258 mutex_unlock(&dev_master->bus_mutex);
1265 mutex_unlock(&dev_master->bus_mutex);
1267 atomic_dec(THERM_REFCNT(sl->family_data));
1274 struct w1_master *dev_master = sl->master;
1276 int ret = -ENODEV;
1278 info->verdict = 0;
1280 if (!sl->family_data)
1283 memset(info->rom, 0, sizeof(info->rom));
1285 /* prevent the slave from going away in sleep */
1286 atomic_inc(THERM_REFCNT(sl->family_data));
1288 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1289 ret = -EAGAIN; /* Didn't acquire the mutex */
1293 while (max_trying-- && ret) { /* ret should be 0 */
1294 /* safe version to select slave */
1300 nb_bytes_read = w1_read_block(dev_master, info->rom, 9);
1302 dev_warn(&sl->dev,
1305 ret = -EIO;
1308 info->crc = w1_calc_crc8(info->rom, 8);
1310 if (info->rom[8] == info->crc) {
1311 info->verdict = 1;
1314 ret = -EIO; /* CRC not checked */
1318 mutex_unlock(&dev_master->bus_mutex);
1321 atomic_dec(THERM_REFCNT(sl->family_data));
1328 struct w1_master *dev_master = sl->master;
1330 int ret = -ENODEV;
1332 if (!sl->family_data)
1335 /* prevent the slave from going away in sleep */
1336 atomic_inc(THERM_REFCNT(sl->family_data));
1338 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1339 ret = -EAGAIN; /* Didn't acquire the mutex */
1343 while (max_trying-- && ret) { /* ret should be 0 */
1344 /* safe version to select slave */
1351 mutex_unlock(&dev_master->bus_mutex);
1354 atomic_dec(THERM_REFCNT(sl->family_data));
1361 struct w1_master *dev_master = sl->master;
1363 int t_write, ret = -ENODEV;
1366 if (!sl->family_data)
1374 /* prevent the slave from going away in sleep */
1375 atomic_inc(THERM_REFCNT(sl->family_data));
1377 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1378 ret = -EAGAIN; /* Didn't acquire the mutex */
1382 while (max_trying-- && ret) { /* ret should be 0 */
1383 /* safe version to select slave */
1396 ret = -EINTR;
1406 mutex_unlock(&dev_master->bus_mutex);
1408 atomic_dec(THERM_REFCNT(sl->family_data));
1415 struct w1_master *dev_master = sl->master;
1417 int ret = -ENODEV;
1419 if (!sl->family_data)
1422 /* prevent the slave from going away in sleep */
1423 atomic_inc(THERM_REFCNT(sl->family_data));
1425 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1426 ret = -EAGAIN; /* Didn't acquire the mutex */
1430 while (max_trying-- && ret) { /* ret should be 0 */
1431 /* safe version to select slave */
1440 mutex_unlock(&dev_master->bus_mutex);
1443 atomic_dec(THERM_REFCNT(sl->family_data));
1450 struct w1_master *dev_master = sl->master;
1452 int ret = -ENODEV;
1454 if (!sl->family_data)
1457 /* prevent the slave from going away in sleep */
1458 atomic_inc(THERM_REFCNT(sl->family_data));
1460 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1461 ret = -EAGAIN; /* Didn't acquire the mutex */
1465 while ((max_trying--) && (ret < 0)) {
1466 /* safe version to select slave */
1478 mutex_unlock(&dev_master->bus_mutex);
1481 atomic_dec(THERM_REFCNT(sl->family_data));
1491 int ret = -ENODEV;
1499 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1500 if (!sl->family_data)
1520 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1521 ret = -EAGAIN; /* Didn't acquire the mutex */
1525 while ((max_trying--) && (ret < 0)) { /* ret should be either 0 */
1539 &dev_master->slist, w1_slave_entry) {
1541 SLAVE_CONVERT_TRIGGERED(sl) = -1;
1547 ret = -EINTR;
1550 mutex_unlock(&dev_master->bus_mutex);
1552 mutex_unlock(&dev_master->bus_mutex);
1555 ret = -EINTR;
1565 mutex_unlock(&dev_master->bus_mutex);
1568 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1583 u8 *family_data = sl->family_data;
1610 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
1611 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
1620 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
1623 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
1626 ret = PAGE_SIZE - c;
1645 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1654 if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution)
1655 ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
1659 dev_warn(device, "%s: Set resolution - error %d\n", __func__, ret);
1664 /* Reset the conversion time to default - it depends on resolution */
1677 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1713 if (!sl->family_data) {
1735 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1742 SLAVE_RESOLUTION(sl) = SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
1767 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1779 ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
1795 int ret = -EINVAL; /* Invalid argument */
1798 if (!strncmp(buf, EEPROM_CMD_WRITE, sizeof(EEPROM_CMD_WRITE)-1))
1801 if (!strncmp(buf, EEPROM_CMD_READ, sizeof(EEPROM_CMD_READ)-1))
1849 __func__, -ENOMEM);
1859 "%s: error parsing args %d\n", __func__, -EINVAL);
1877 "%s: error parsing args %d\n", __func__, -EINVAL);
1897 * (th : byte 2 - tl: byte 3)
1906 "%s: error reading from the slave device %d\n",
1915 __func__, -ENODEV);
1919 ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register);
1922 "%s: error writing to the slave device %d\n",
1936 int ret = -EINVAL; /* Invalid argument */
1940 sizeof(BULK_TRIGGER_CMD)-1))
1958 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1959 if (sl->family_data) {
1961 if (SLAVE_CONVERT_TRIGGERED(sl) == -1) {
1962 ret = -1;
1980 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1995 return -EINVAL;
1998 return -ENODEV;
2004 return -EINVAL;
2011 return -EIO;
2022 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
2039 return -EINVAL; /* invalid number */
2041 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
2043 return -ENODEV;
2047 return -EINVAL;
2056 dev_warn(&sl->dev,
2080 ret = -EIO;
2088 ret = -EOPNOTSUPP;
2117 mutex_lock(&sl->master->bus_mutex);
2119 if (w1_reset_bus(sl->master))
2121 w1_write_8(sl->master, W1_SKIP_ROM);
2122 w1_write_8(sl->master, W1_42_CHAIN);
2123 w1_write_8(sl->master, W1_42_CHAIN_ON);
2124 w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
2125 msleep(sl->master->pullup_duration);
2128 ack = w1_read_8(sl->master);
2134 if (w1_reset_bus(sl->master))
2137 w1_write_8(sl->master, W1_42_COND_READ);
2138 w1_read_block(sl->master, (u8 *)&rn, 8);
2140 if (reg_num->family == W1_42_FINISHED_BYTE)
2142 if (sl->reg_num.id == reg_num->id)
2145 if (w1_reset_bus(sl->master))
2149 w1_write_8(sl->master, W1_MATCH_ROM);
2150 w1_write_block(sl->master, (u8 *)&rn, 8);
2151 w1_write_8(sl->master, W1_42_CHAIN);
2152 w1_write_8(sl->master, W1_42_CHAIN_DONE);
2153 w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
2156 ack = w1_read_8(sl->master);
2162 if (w1_reset_bus(sl->master))
2164 w1_write_8(sl->master, W1_SKIP_ROM);
2165 w1_write_8(sl->master, W1_42_CHAIN);
2166 w1_write_8(sl->master, W1_42_CHAIN_OFF);
2167 w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
2170 ack = w1_read_8(sl->master);
2173 mutex_unlock(&sl->master->bus_mutex);
2175 c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
2176 return PAGE_SIZE - c;
2178 mutex_unlock(&sl->master->bus_mutex);
2179 return -EIO;
2208 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
2210 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
2211 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
2212 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
2213 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
2214 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));