// SPDX-License-Identifier: GPL-2.0-or-later /* * w1_therm.c * * Copyright (c) 2004 Evgeniy Polyakov <[email protected]> */ #include <asm/types.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/sched.h> #include <linux/device.h> #include <linux/types.h> #include <linux/slab.h> #include <linux/delay.h> #include <linux/hwmon.h> #include <linux/string.h> #include <linux/jiffies.h> #include <linux/w1.h> #define W1_THERM_DS18S20 … #define W1_THERM_DS1822 … #define W1_THERM_DS18B20 … #define W1_THERM_DS1825 … #define W1_THERM_DS28EA00 … /* * Allow the strong pullup to be disabled, but default to enabled. * If it was disabled a parasite powered device might not get the require * current to do a temperature conversion. If it is enabled parasite powered * devices have a better chance of getting the current required. * In case the parasite power-detection is not working (seems to be the case * for some DS18S20) the strong pullup can also be forced, regardless of the * power state of the devices. * * Summary of options: * - strong_pullup = 0 Disable strong pullup completely * - strong_pullup = 1 Enable automatic strong pullup detection * - strong_pullup = 2 Force strong pullup */ static int w1_strong_pullup = …; module_param_named(strong_pullup, w1_strong_pullup, int, 0); /* Counter for devices supporting bulk reading */ static u16 bulk_read_device_counter; /* =0 as per C standard */ /* This command should be in public header w1.h but is not */ #define W1_RECALL_EEPROM … /* Nb of try for an operation */ #define W1_THERM_MAX_TRY … /* ms delay to retry bus mutex */ #define W1_THERM_RETRY_DELAY … /* delay in ms to write in EEPROM */ #define W1_THERM_EEPROM_WRITE_DELAY … #define EEPROM_CMD_WRITE … #define EEPROM_CMD_READ … #define BULK_TRIGGER_CMD … #define MIN_TEMP … #define MAX_TEMP … /* Allowed values for sysfs conv_time attribute */ #define CONV_TIME_DEFAULT … #define CONV_TIME_MEASURE … /* Bits in sysfs "features" value */ #define W1_THERM_CHECK_RESULT … #define W1_THERM_POLL_COMPLETION … #define W1_THERM_FEATURES_MASK … /* Poll period in milliseconds. Should be less then a shortest operation on the device */ #define W1_POLL_PERIOD … #define W1_POLL_CONVERT_TEMP … #define W1_POLL_RECALL_EEPROM … /* Masks for resolution functions, work with all devices */ /* Bit mask for config register for all devices, bits 7,6,5 */ #define W1_THERM_RESOLUTION_MASK … /* Bit offset of resolution in config register for all devices */ #define W1_THERM_RESOLUTION_SHIFT … /* Bit offset of resolution in config register for all devices */ #define W1_THERM_RESOLUTION_SHIFT … /* Add this to bit value to get resolution */ #define W1_THERM_RESOLUTION_MIN … /* Maximum allowed value */ #define W1_THERM_RESOLUTION_MAX … /* Helpers Macros */ /* * return a pointer on the slave w1_therm_family_converter struct: * always test family data existence before using this macro */ #define SLAVE_SPECIFIC_FUNC(sl) … /* * return the power mode of the sl slave : 1-ext, 0-parasite, <0 unknown * always test family data existence before using this macro */ #define SLAVE_POWERMODE(sl) … /* * return the resolution in bit of the sl slave : <0 unknown * always test family data existence before using this macro */ #define SLAVE_RESOLUTION(sl) … /* * return the conv_time_override of the sl slave * always test family data existence before using this macro */ #define SLAVE_CONV_TIME_OVERRIDE(sl) … /* * return the features of the sl slave * always test family data existence before using this macro */ #define SLAVE_FEATURES(sl) … /* * return whether or not a converT command has been issued to the slave * * 0: no bulk read is pending * * -1: conversion is in progress * * 1: conversion done, result to be read */ #define SLAVE_CONVERT_TRIGGERED(sl) … /* return the address of the refcnt in the family data */ #define THERM_REFCNT(family_data) … /* Structs definition */ /** * struct w1_therm_family_converter - bind device specific functions * @broken: flag for non-registred families * @reserved: not used here * @f: pointer to the device binding structure * @convert: pointer to the device conversion function * @get_conversion_time: pointer to the device conversion time function * @set_resolution: pointer to the device set_resolution function * @get_resolution: pointer to the device get_resolution function * @write_data: pointer to the device writing function (2 or 3 bytes) * @bulk_read: true if device family support bulk read, false otherwise */ struct w1_therm_family_converter { … }; /** * struct w1_therm_family_data - device data * @rom: ROM device id (64bit Lasered ROM code + 1 CRC byte) * @refcnt: ref count * @external_powered: 1 device powered externally, * 0 device parasite powered, * -x error or undefined * @resolution: current device resolution * @convert_triggered: conversion state of the device * @conv_time_override: user selected conversion time or CONV_TIME_DEFAULT * @features: bit mask - enable temperature validity check, poll for completion * @specific_functions: pointer to struct of device specific function */ struct w1_therm_family_data { … }; /** * struct therm_info - store temperature reading * @rom: read device data (8 data bytes + 1 CRC byte) * @crc: computed crc from rom * @verdict: 1 crc checked, 0 crc not matching */ struct therm_info { … }; /* Hardware Functions declaration */ /** * reset_select_slave() - reset and select a slave * @sl: the slave to select * * Resets the bus and select the slave by sending a ROM MATCH cmd * w1_reset_select_slave() from w1_io.c could not be used here because * it sent a SKIP ROM command if only one device is on the line. * At the beginning of the such process, sl->master->slave_count is 1 even if * more devices are on the line, causing collision on the line. * * Context: The w1 master lock must be held. * * Return: 0 if success, negative kernel error code otherwise. */ static int reset_select_slave(struct w1_slave *sl); /** * convert_t() - Query the device for temperature conversion and read * @sl: pointer to the slave to read * @info: pointer to a structure to store the read results * * Return: 0 if success, -kernel error code otherwise */ static int convert_t(struct w1_slave *sl, struct therm_info *info); /** * read_scratchpad() - read the data in device RAM * @sl: pointer to the slave to read * @info: pointer to a structure to store the read results * * Return: 0 if success, -kernel error code otherwise */ static int read_scratchpad(struct w1_slave *sl, struct therm_info *info); /** * write_scratchpad() - write nb_bytes in the device RAM * @sl: pointer to the slave to write in * @data: pointer to an array of 3 bytes, as 3 bytes MUST be written * @nb_bytes: number of bytes to be written (2 for DS18S20, 3 otherwise) * * Return: 0 if success, -kernel error code otherwise */ static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes); /** * copy_scratchpad() - Copy the content of scratchpad in device EEPROM * @sl: slave involved * * Return: 0 if success, -kernel error code otherwise */ static int copy_scratchpad(struct w1_slave *sl); /** * recall_eeprom() - Restore EEPROM data to device RAM * @sl: slave involved * * Return: 0 if success, -kernel error code otherwise */ static int recall_eeprom(struct w1_slave *sl); /** * read_powermode() - Query the power mode of the slave * @sl: slave to retrieve the power mode * * Ask the device to get its power mode (external or parasite) * and store the power status in the &struct w1_therm_family_data. * * Return: * * 0 parasite powered device * * 1 externally powered device * * <0 kernel error code */ static int read_powermode(struct w1_slave *sl); /** * trigger_bulk_read() - function to trigger a bulk read on the bus * @dev_master: the device master of the bus * * Send a SKIP ROM follow by a CONVERT T command on the bus. * It also set the status flag in each slave &struct w1_therm_family_data * to signal that a conversion is in progress. * * Return: 0 if success, -kernel error code otherwise */ static int trigger_bulk_read(struct w1_master *dev_master); /* Sysfs interface declaration */ static ssize_t w1_slave_show(struct device *device, struct device_attribute *attr, char *buf); static ssize_t w1_slave_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size); static ssize_t w1_seq_show(struct device *device, struct device_attribute *attr, char *buf); static ssize_t temperature_show(struct device *device, struct device_attribute *attr, char *buf); static ssize_t ext_power_show(struct device *device, struct device_attribute *attr, char *buf); static ssize_t resolution_show(struct device *device, struct device_attribute *attr, char *buf); static ssize_t resolution_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size); static ssize_t eeprom_cmd_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size); static ssize_t alarms_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size); static ssize_t alarms_show(struct device *device, struct device_attribute *attr, char *buf); static ssize_t therm_bulk_read_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size); static ssize_t therm_bulk_read_show(struct device *device, struct device_attribute *attr, char *buf); static ssize_t conv_time_show(struct device *device, struct device_attribute *attr, char *buf); static ssize_t conv_time_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size); static ssize_t features_show(struct device *device, struct device_attribute *attr, char *buf); static ssize_t features_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size); /* Attributes declarations */ static DEVICE_ATTR_RW(w1_slave); static DEVICE_ATTR_RO(w1_seq); static DEVICE_ATTR_RO(temperature); static DEVICE_ATTR_RO(ext_power); static DEVICE_ATTR_RW(resolution); static DEVICE_ATTR_WO(eeprom_cmd); static DEVICE_ATTR_RW(alarms); static DEVICE_ATTR_RW(conv_time); static DEVICE_ATTR_RW(features); static DEVICE_ATTR_RW(therm_bulk_read); /* attribut at master level */ /* Interface Functions declaration */ /** * w1_therm_add_slave() - Called when a new slave is discovered * @sl: slave just discovered by the master. * * Called by the master when the slave is discovered on the bus. Used to * initialize slave state before the beginning of any communication. * * Return: 0 - If success, negative kernel code otherwise */ static int w1_therm_add_slave(struct w1_slave *sl); /** * w1_therm_remove_slave() - Called when a slave is removed * @sl: slave to be removed. * * Called by the master when the slave is considered not to be on the bus * anymore. Used to free memory. */ static void w1_therm_remove_slave(struct w1_slave *sl); /* Family attributes */ static struct attribute *w1_therm_attrs[] = …; static struct attribute *w1_ds18s20_attrs[] = …; static struct attribute *w1_ds28ea00_attrs[] = …; /* Attribute groups */ ATTRIBUTE_GROUPS(…); ATTRIBUTE_GROUPS(…); ATTRIBUTE_GROUPS(…); #if IS_REACHABLE(CONFIG_HWMON) static int w1_read_temp(struct device *dev, u32 attr, int channel, long *val); static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type, u32 attr, int channel) { … } static int w1_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { … } static const u32 w1_temp_config[] = …; static const struct hwmon_channel_info w1_temp = …; static const struct hwmon_channel_info * const w1_info[] = …; static const struct hwmon_ops w1_hwmon_ops = …; static const struct hwmon_chip_info w1_chip_info = …; #define W1_CHIPINFO … #else #define W1_CHIPINFO … #endif /* Family operations */ static const struct w1_family_ops w1_therm_fops = …; static const struct w1_family_ops w1_ds18s20_fops = …; static const struct w1_family_ops w1_ds28ea00_fops = …; /* Family binding operations struct */ static struct w1_family w1_therm_family_DS18S20 = …; static struct w1_family w1_therm_family_DS18B20 = …; static struct w1_family w1_therm_family_DS1822 = …; static struct w1_family w1_therm_family_DS28EA00 = …; static struct w1_family w1_therm_family_DS1825 = …; /* Device dependent func */ static inline int w1_DS18B20_convert_time(struct w1_slave *sl) { … } static inline int w1_DS18S20_convert_time(struct w1_slave *sl) { … } static inline int w1_DS1825_convert_time(struct w1_slave *sl) { … } static inline int w1_DS18B20_write_data(struct w1_slave *sl, const u8 *data) { … } static inline int w1_DS18S20_write_data(struct w1_slave *sl, const u8 *data) { … } static inline int w1_DS18B20_set_resolution(struct w1_slave *sl, int val) { … } static inline int w1_DS18B20_get_resolution(struct w1_slave *sl) { … } /** * w1_DS18B20_convert_temp() - temperature computation for DS18B20 * @rom: data read from device RAM (8 data bytes + 1 CRC byte) * * Can be called for any DS18B20 compliant device. * * Return: value in millidegrees Celsius. */ static inline int w1_DS18B20_convert_temp(u8 rom[9]) { … } /** * w1_DS18S20_convert_temp() - temperature computation for DS18S20 * @rom: data read from device RAM (8 data bytes + 1 CRC byte) * * Can be called for any DS18S20 compliant device. * * Return: value in millidegrees Celsius. */ static inline int w1_DS18S20_convert_temp(u8 rom[9]) { … } /** * w1_DS1825_convert_temp() - temperature computation for DS1825 * @rom: data read from device RAM (8 data bytes + 1 CRC byte) * * Can be called for any DS1825 compliant device. * Is used by MAX31850, too * * Return: value in millidegrees Celsius. */ static inline int w1_DS1825_convert_temp(u8 rom[9]) { … } /* Device capability description */ /* GX20MH01 device shares family number and structure with DS18B20 */ static struct w1_therm_family_converter w1_therm_families[] = …; /* Helpers Functions */ /** * device_family() - Retrieve a pointer on &struct w1_therm_family_converter * @sl: slave to retrieve the device specific structure * * Return: pointer to the slaves's family converter, NULL if not known */ static struct w1_therm_family_converter *device_family(struct w1_slave *sl) { … } /** * bus_mutex_lock() - Acquire the mutex * @lock: w1 bus mutex to acquire * * It try to acquire the mutex W1_THERM_MAX_TRY times and wait * W1_THERM_RETRY_DELAY between 2 attempts. * * Return: true is mutex is acquired and lock, false otherwise */ static inline bool bus_mutex_lock(struct mutex *lock) { … } /** * check_family_data() - Check if family data and specific functions are present * @sl: W1 device data * * Return: 0 - OK, negative value - error */ static int check_family_data(struct w1_slave *sl) { … } /** * bulk_read_support() - check if slave support bulk read * @sl: device to check the ability * * Return: true if bulk read is supported, false if not or error */ static inline bool bulk_read_support(struct w1_slave *sl) { … } /** * conversion_time() - get the Tconv for the slave * @sl: device to get the conversion time * * On device supporting resolution settings, conversion time depend * on the resolution setting. This helper function get the slave timing, * depending on its current setting. * * Return: conversion time in ms, negative values are kernel error code */ static inline int conversion_time(struct w1_slave *sl) { … } /** * temperature_from_RAM() - Convert the read info to temperature * @sl: device that sent the RAM data * @rom: read value on the slave device RAM * * Device dependent, the function bind the correct computation method. * * Return: temperature in 1/1000degC, 0 on error. */ static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9]) { … } /** * int_to_short() - Safe casting of int to short * * @i: integer to be converted to short * * Device register use 1 byte to store signed integer. * This helper function convert the int in a signed short, * using the min/max values that device can measure as limits. * min/max values are defined by macro. * * Return: a short in the range of min/max value */ static inline s8 int_to_short(int i) { … } /* Interface Functions */ static int w1_therm_add_slave(struct w1_slave *sl) { … } static void w1_therm_remove_slave(struct w1_slave *sl) { … } /* Hardware Functions */ /* Safe version of reset_select_slave - avoid using the one in w_io.c */ static int reset_select_slave(struct w1_slave *sl) { … } /** * w1_poll_completion - Poll for operation completion, with timeout * @dev_master: the device master of the bus * @tout_ms: timeout in milliseconds * * The device is answering 0's while an operation is in progress and 1's after it completes * Timeout may happen if the previous command was not recognised due to a line noise * * Return: 0 - OK, negative error - timeout */ static int w1_poll_completion(struct w1_master *dev_master, int tout_ms) { … } static int convert_t(struct w1_slave *sl, struct therm_info *info) { … } static int conv_time_measure(struct w1_slave *sl, int *conv_time) { … } static int read_scratchpad(struct w1_slave *sl, struct therm_info *info) { … } static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes) { … } static int copy_scratchpad(struct w1_slave *sl) { … } static int recall_eeprom(struct w1_slave *sl) { … } static int read_powermode(struct w1_slave *sl) { … } static int trigger_bulk_read(struct w1_master *dev_master) { … } /* Sysfs Interface definition */ static ssize_t w1_slave_show(struct device *device, struct device_attribute *attr, char *buf) { … } static ssize_t w1_slave_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size) { … } static ssize_t temperature_show(struct device *device, struct device_attribute *attr, char *buf) { … } static ssize_t ext_power_show(struct device *device, struct device_attribute *attr, char *buf) { … } static ssize_t resolution_show(struct device *device, struct device_attribute *attr, char *buf) { … } static ssize_t resolution_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size) { … } static ssize_t eeprom_cmd_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size) { … } static ssize_t alarms_show(struct device *device, struct device_attribute *attr, char *buf) { … } static ssize_t alarms_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size) { … } static ssize_t therm_bulk_read_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size) { … } static ssize_t therm_bulk_read_show(struct device *device, struct device_attribute *attr, char *buf) { … } static ssize_t conv_time_show(struct device *device, struct device_attribute *attr, char *buf) { … } static ssize_t conv_time_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size) { … } static ssize_t features_show(struct device *device, struct device_attribute *attr, char *buf) { … } static ssize_t features_store(struct device *device, struct device_attribute *attr, const char *buf, size_t size) { … } #if IS_REACHABLE(CONFIG_HWMON) static int w1_read_temp(struct device *device, u32 attr, int channel, long *val) { … } #endif #define W1_42_CHAIN … #define W1_42_CHAIN_OFF … #define W1_42_CHAIN_OFF_INV … #define W1_42_CHAIN_ON … #define W1_42_CHAIN_ON_INV … #define W1_42_CHAIN_DONE … #define W1_42_CHAIN_DONE_INV … #define W1_42_COND_READ … #define W1_42_SUCCESS_CONFIRM_BYTE … #define W1_42_FINISHED_BYTE … static ssize_t w1_seq_show(struct device *device, struct device_attribute *attr, char *buf) { … } static int __init w1_therm_init(void) { … } static void __exit w1_therm_fini(void) { … } module_init(…) …; module_exit(w1_therm_fini); MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …; MODULE_ALIAS(…) …; MODULE_ALIAS(…) …; MODULE_ALIAS(…) …; MODULE_ALIAS(…) …; MODULE_ALIAS(…) …;