18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * w1_therm.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <asm/types.h> 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/moduleparam.h> 138c2ecf20Sopenharmony_ci#include <linux/sched.h> 148c2ecf20Sopenharmony_ci#include <linux/device.h> 158c2ecf20Sopenharmony_ci#include <linux/types.h> 168c2ecf20Sopenharmony_ci#include <linux/slab.h> 178c2ecf20Sopenharmony_ci#include <linux/delay.h> 188c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 198c2ecf20Sopenharmony_ci#include <linux/string.h> 208c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <linux/w1.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define W1_THERM_DS18S20 0x10 258c2ecf20Sopenharmony_ci#define W1_THERM_DS1822 0x22 268c2ecf20Sopenharmony_ci#define W1_THERM_DS18B20 0x28 278c2ecf20Sopenharmony_ci#define W1_THERM_DS1825 0x3B 288c2ecf20Sopenharmony_ci#define W1_THERM_DS28EA00 0x42 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* 318c2ecf20Sopenharmony_ci * Allow the strong pullup to be disabled, but default to enabled. 328c2ecf20Sopenharmony_ci * If it was disabled a parasite powered device might not get the require 338c2ecf20Sopenharmony_ci * current to do a temperature conversion. If it is enabled parasite powered 348c2ecf20Sopenharmony_ci * devices have a better chance of getting the current required. 358c2ecf20Sopenharmony_ci * In case the parasite power-detection is not working (seems to be the case 368c2ecf20Sopenharmony_ci * for some DS18S20) the strong pullup can also be forced, regardless of the 378c2ecf20Sopenharmony_ci * power state of the devices. 388c2ecf20Sopenharmony_ci * 398c2ecf20Sopenharmony_ci * Summary of options: 408c2ecf20Sopenharmony_ci * - strong_pullup = 0 Disable strong pullup completely 418c2ecf20Sopenharmony_ci * - strong_pullup = 1 Enable automatic strong pullup detection 428c2ecf20Sopenharmony_ci * - strong_pullup = 2 Force strong pullup 438c2ecf20Sopenharmony_ci */ 448c2ecf20Sopenharmony_cistatic int w1_strong_pullup = 1; 458c2ecf20Sopenharmony_cimodule_param_named(strong_pullup, w1_strong_pullup, int, 0); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci/* Counter for devices supporting bulk reading */ 488c2ecf20Sopenharmony_cistatic u16 bulk_read_device_counter; /* =0 as per C standard */ 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* This command should be in public header w1.h but is not */ 518c2ecf20Sopenharmony_ci#define W1_RECALL_EEPROM 0xB8 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci/* Nb of try for an operation */ 548c2ecf20Sopenharmony_ci#define W1_THERM_MAX_TRY 5 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* ms delay to retry bus mutex */ 578c2ecf20Sopenharmony_ci#define W1_THERM_RETRY_DELAY 20 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci/* delay in ms to write in EEPROM */ 608c2ecf20Sopenharmony_ci#define W1_THERM_EEPROM_WRITE_DELAY 10 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci#define EEPROM_CMD_WRITE "save" /* cmd for write eeprom sysfs */ 638c2ecf20Sopenharmony_ci#define EEPROM_CMD_READ "restore" /* cmd for read eeprom sysfs */ 648c2ecf20Sopenharmony_ci#define BULK_TRIGGER_CMD "trigger" /* cmd to trigger a bulk read */ 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci#define MIN_TEMP -55 /* min temperature that can be mesured */ 678c2ecf20Sopenharmony_ci#define MAX_TEMP 125 /* max temperature that can be mesured */ 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/* Allowed values for sysfs conv_time attribute */ 708c2ecf20Sopenharmony_ci#define CONV_TIME_DEFAULT 0 718c2ecf20Sopenharmony_ci#define CONV_TIME_MEASURE 1 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/* Bits in sysfs "features" value */ 748c2ecf20Sopenharmony_ci#define W1_THERM_CHECK_RESULT 1 /* Enable conversion success check */ 758c2ecf20Sopenharmony_ci#define W1_THERM_POLL_COMPLETION 2 /* Poll for conversion completion */ 768c2ecf20Sopenharmony_ci#define W1_THERM_FEATURES_MASK 3 /* All values mask */ 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/* Poll period in milliseconds. Should be less then a shortest operation on the device */ 798c2ecf20Sopenharmony_ci#define W1_POLL_PERIOD 32 808c2ecf20Sopenharmony_ci#define W1_POLL_CONVERT_TEMP 2000 /* Timeout for W1_CONVERT_TEMP, ms */ 818c2ecf20Sopenharmony_ci#define W1_POLL_RECALL_EEPROM 500 /* Timeout for W1_RECALL_EEPROM, ms*/ 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci/* Masks for resolution functions, work with all devices */ 848c2ecf20Sopenharmony_ci/* Bit mask for config register for all devices, bits 7,6,5 */ 858c2ecf20Sopenharmony_ci#define W1_THERM_RESOLUTION_MASK 0xE0 868c2ecf20Sopenharmony_ci/* Bit offset of resolution in config register for all devices */ 878c2ecf20Sopenharmony_ci#define W1_THERM_RESOLUTION_SHIFT 5 888c2ecf20Sopenharmony_ci/* Bit offset of resolution in config register for all devices */ 898c2ecf20Sopenharmony_ci#define W1_THERM_RESOLUTION_SHIFT 5 908c2ecf20Sopenharmony_ci/* Add this to bit value to get resolution */ 918c2ecf20Sopenharmony_ci#define W1_THERM_RESOLUTION_MIN 9 928c2ecf20Sopenharmony_ci/* Maximum allowed value */ 938c2ecf20Sopenharmony_ci#define W1_THERM_RESOLUTION_MAX 14 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci/* Helpers Macros */ 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/* 988c2ecf20Sopenharmony_ci * return a pointer on the slave w1_therm_family_converter struct: 998c2ecf20Sopenharmony_ci * always test family data existence before using this macro 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_ci#define SLAVE_SPECIFIC_FUNC(sl) \ 1028c2ecf20Sopenharmony_ci (((struct w1_therm_family_data *)(sl->family_data))->specific_functions) 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci/* 1058c2ecf20Sopenharmony_ci * return the power mode of the sl slave : 1-ext, 0-parasite, <0 unknown 1068c2ecf20Sopenharmony_ci * always test family data existence before using this macro 1078c2ecf20Sopenharmony_ci */ 1088c2ecf20Sopenharmony_ci#define SLAVE_POWERMODE(sl) \ 1098c2ecf20Sopenharmony_ci (((struct w1_therm_family_data *)(sl->family_data))->external_powered) 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci/* 1128c2ecf20Sopenharmony_ci * return the resolution in bit of the sl slave : <0 unknown 1138c2ecf20Sopenharmony_ci * always test family data existence before using this macro 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_ci#define SLAVE_RESOLUTION(sl) \ 1168c2ecf20Sopenharmony_ci (((struct w1_therm_family_data *)(sl->family_data))->resolution) 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci/* 1198c2ecf20Sopenharmony_ci * return the conv_time_override of the sl slave 1208c2ecf20Sopenharmony_ci * always test family data existence before using this macro 1218c2ecf20Sopenharmony_ci */ 1228c2ecf20Sopenharmony_ci #define SLAVE_CONV_TIME_OVERRIDE(sl) \ 1238c2ecf20Sopenharmony_ci (((struct w1_therm_family_data *)(sl->family_data))->conv_time_override) 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci/* 1268c2ecf20Sopenharmony_ci * return the features of the sl slave 1278c2ecf20Sopenharmony_ci * always test family data existence before using this macro 1288c2ecf20Sopenharmony_ci */ 1298c2ecf20Sopenharmony_ci #define SLAVE_FEATURES(sl) \ 1308c2ecf20Sopenharmony_ci (((struct w1_therm_family_data *)(sl->family_data))->features) 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci/* 1338c2ecf20Sopenharmony_ci * return whether or not a converT command has been issued to the slave 1348c2ecf20Sopenharmony_ci * * 0: no bulk read is pending 1358c2ecf20Sopenharmony_ci * * -1: conversion is in progress 1368c2ecf20Sopenharmony_ci * * 1: conversion done, result to be read 1378c2ecf20Sopenharmony_ci */ 1388c2ecf20Sopenharmony_ci#define SLAVE_CONVERT_TRIGGERED(sl) \ 1398c2ecf20Sopenharmony_ci (((struct w1_therm_family_data *)(sl->family_data))->convert_triggered) 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci/* return the address of the refcnt in the family data */ 1428c2ecf20Sopenharmony_ci#define THERM_REFCNT(family_data) \ 1438c2ecf20Sopenharmony_ci (&((struct w1_therm_family_data *)family_data)->refcnt) 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci/* Structs definition */ 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci/** 1488c2ecf20Sopenharmony_ci * struct w1_therm_family_converter - bind device specific functions 1498c2ecf20Sopenharmony_ci * @broken: flag for non-registred families 1508c2ecf20Sopenharmony_ci * @reserved: not used here 1518c2ecf20Sopenharmony_ci * @f: pointer to the device binding structure 1528c2ecf20Sopenharmony_ci * @convert: pointer to the device conversion function 1538c2ecf20Sopenharmony_ci * @get_conversion_time: pointer to the device conversion time function 1548c2ecf20Sopenharmony_ci * @set_resolution: pointer to the device set_resolution function 1558c2ecf20Sopenharmony_ci * @get_resolution: pointer to the device get_resolution function 1568c2ecf20Sopenharmony_ci * @write_data: pointer to the device writing function (2 or 3 bytes) 1578c2ecf20Sopenharmony_ci * @bulk_read: true if device family support bulk read, false otherwise 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_cistruct w1_therm_family_converter { 1608c2ecf20Sopenharmony_ci u8 broken; 1618c2ecf20Sopenharmony_ci u16 reserved; 1628c2ecf20Sopenharmony_ci struct w1_family *f; 1638c2ecf20Sopenharmony_ci int (*convert)(u8 rom[9]); 1648c2ecf20Sopenharmony_ci int (*get_conversion_time)(struct w1_slave *sl); 1658c2ecf20Sopenharmony_ci int (*set_resolution)(struct w1_slave *sl, int val); 1668c2ecf20Sopenharmony_ci int (*get_resolution)(struct w1_slave *sl); 1678c2ecf20Sopenharmony_ci int (*write_data)(struct w1_slave *sl, const u8 *data); 1688c2ecf20Sopenharmony_ci bool bulk_read; 1698c2ecf20Sopenharmony_ci}; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci/** 1728c2ecf20Sopenharmony_ci * struct w1_therm_family_data - device data 1738c2ecf20Sopenharmony_ci * @rom: ROM device id (64bit Lasered ROM code + 1 CRC byte) 1748c2ecf20Sopenharmony_ci * @refcnt: ref count 1758c2ecf20Sopenharmony_ci * @external_powered: 1 device powered externally, 1768c2ecf20Sopenharmony_ci * 0 device parasite powered, 1778c2ecf20Sopenharmony_ci * -x error or undefined 1788c2ecf20Sopenharmony_ci * @resolution: current device resolution 1798c2ecf20Sopenharmony_ci * @convert_triggered: conversion state of the device 1808c2ecf20Sopenharmony_ci * @conv_time_override: user selected conversion time or CONV_TIME_DEFAULT 1818c2ecf20Sopenharmony_ci * @features: bit mask - enable temperature validity check, poll for completion 1828c2ecf20Sopenharmony_ci * @specific_functions: pointer to struct of device specific function 1838c2ecf20Sopenharmony_ci */ 1848c2ecf20Sopenharmony_cistruct w1_therm_family_data { 1858c2ecf20Sopenharmony_ci uint8_t rom[9]; 1868c2ecf20Sopenharmony_ci atomic_t refcnt; 1878c2ecf20Sopenharmony_ci int external_powered; 1888c2ecf20Sopenharmony_ci int resolution; 1898c2ecf20Sopenharmony_ci int convert_triggered; 1908c2ecf20Sopenharmony_ci int conv_time_override; 1918c2ecf20Sopenharmony_ci unsigned int features; 1928c2ecf20Sopenharmony_ci struct w1_therm_family_converter *specific_functions; 1938c2ecf20Sopenharmony_ci}; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci/** 1968c2ecf20Sopenharmony_ci * struct therm_info - store temperature reading 1978c2ecf20Sopenharmony_ci * @rom: read device data (8 data bytes + 1 CRC byte) 1988c2ecf20Sopenharmony_ci * @crc: computed crc from rom 1998c2ecf20Sopenharmony_ci * @verdict: 1 crc checked, 0 crc not matching 2008c2ecf20Sopenharmony_ci */ 2018c2ecf20Sopenharmony_cistruct therm_info { 2028c2ecf20Sopenharmony_ci u8 rom[9]; 2038c2ecf20Sopenharmony_ci u8 crc; 2048c2ecf20Sopenharmony_ci u8 verdict; 2058c2ecf20Sopenharmony_ci}; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci/* Hardware Functions declaration */ 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci/** 2108c2ecf20Sopenharmony_ci * reset_select_slave() - reset and select a slave 2118c2ecf20Sopenharmony_ci * @sl: the slave to select 2128c2ecf20Sopenharmony_ci * 2138c2ecf20Sopenharmony_ci * Resets the bus and select the slave by sending a ROM MATCH cmd 2148c2ecf20Sopenharmony_ci * w1_reset_select_slave() from w1_io.c could not be used here because 2158c2ecf20Sopenharmony_ci * it sent a SKIP ROM command if only one device is on the line. 2168c2ecf20Sopenharmony_ci * At the beginning of the such process, sl->master->slave_count is 1 even if 2178c2ecf20Sopenharmony_ci * more devices are on the line, causing collision on the line. 2188c2ecf20Sopenharmony_ci * 2198c2ecf20Sopenharmony_ci * Context: The w1 master lock must be held. 2208c2ecf20Sopenharmony_ci * 2218c2ecf20Sopenharmony_ci * Return: 0 if success, negative kernel error code otherwise. 2228c2ecf20Sopenharmony_ci */ 2238c2ecf20Sopenharmony_cistatic int reset_select_slave(struct w1_slave *sl); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci/** 2268c2ecf20Sopenharmony_ci * convert_t() - Query the device for temperature conversion and read 2278c2ecf20Sopenharmony_ci * @sl: pointer to the slave to read 2288c2ecf20Sopenharmony_ci * @info: pointer to a structure to store the read results 2298c2ecf20Sopenharmony_ci * 2308c2ecf20Sopenharmony_ci * Return: 0 if success, -kernel error code otherwise 2318c2ecf20Sopenharmony_ci */ 2328c2ecf20Sopenharmony_cistatic int convert_t(struct w1_slave *sl, struct therm_info *info); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci/** 2358c2ecf20Sopenharmony_ci * read_scratchpad() - read the data in device RAM 2368c2ecf20Sopenharmony_ci * @sl: pointer to the slave to read 2378c2ecf20Sopenharmony_ci * @info: pointer to a structure to store the read results 2388c2ecf20Sopenharmony_ci * 2398c2ecf20Sopenharmony_ci * Return: 0 if success, -kernel error code otherwise 2408c2ecf20Sopenharmony_ci */ 2418c2ecf20Sopenharmony_cistatic int read_scratchpad(struct w1_slave *sl, struct therm_info *info); 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci/** 2448c2ecf20Sopenharmony_ci * write_scratchpad() - write nb_bytes in the device RAM 2458c2ecf20Sopenharmony_ci * @sl: pointer to the slave to write in 2468c2ecf20Sopenharmony_ci * @data: pointer to an array of 3 bytes, as 3 bytes MUST be written 2478c2ecf20Sopenharmony_ci * @nb_bytes: number of bytes to be written (2 for DS18S20, 3 otherwise) 2488c2ecf20Sopenharmony_ci * 2498c2ecf20Sopenharmony_ci * Return: 0 if success, -kernel error code otherwise 2508c2ecf20Sopenharmony_ci */ 2518c2ecf20Sopenharmony_cistatic int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes); 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci/** 2548c2ecf20Sopenharmony_ci * copy_scratchpad() - Copy the content of scratchpad in device EEPROM 2558c2ecf20Sopenharmony_ci * @sl: slave involved 2568c2ecf20Sopenharmony_ci * 2578c2ecf20Sopenharmony_ci * Return: 0 if success, -kernel error code otherwise 2588c2ecf20Sopenharmony_ci */ 2598c2ecf20Sopenharmony_cistatic int copy_scratchpad(struct w1_slave *sl); 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci/** 2628c2ecf20Sopenharmony_ci * recall_eeprom() - Restore EEPROM data to device RAM 2638c2ecf20Sopenharmony_ci * @sl: slave involved 2648c2ecf20Sopenharmony_ci * 2658c2ecf20Sopenharmony_ci * Return: 0 if success, -kernel error code otherwise 2668c2ecf20Sopenharmony_ci */ 2678c2ecf20Sopenharmony_cistatic int recall_eeprom(struct w1_slave *sl); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci/** 2708c2ecf20Sopenharmony_ci * read_powermode() - Query the power mode of the slave 2718c2ecf20Sopenharmony_ci * @sl: slave to retrieve the power mode 2728c2ecf20Sopenharmony_ci * 2738c2ecf20Sopenharmony_ci * Ask the device to get its power mode (external or parasite) 2748c2ecf20Sopenharmony_ci * and store the power status in the &struct w1_therm_family_data. 2758c2ecf20Sopenharmony_ci * 2768c2ecf20Sopenharmony_ci * Return: 2778c2ecf20Sopenharmony_ci * * 0 parasite powered device 2788c2ecf20Sopenharmony_ci * * 1 externally powered device 2798c2ecf20Sopenharmony_ci * * <0 kernel error code 2808c2ecf20Sopenharmony_ci */ 2818c2ecf20Sopenharmony_cistatic int read_powermode(struct w1_slave *sl); 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci/** 2848c2ecf20Sopenharmony_ci * trigger_bulk_read() - function to trigger a bulk read on the bus 2858c2ecf20Sopenharmony_ci * @dev_master: the device master of the bus 2868c2ecf20Sopenharmony_ci * 2878c2ecf20Sopenharmony_ci * Send a SKIP ROM follow by a CONVERT T commmand on the bus. 2888c2ecf20Sopenharmony_ci * It also set the status flag in each slave &struct w1_therm_family_data 2898c2ecf20Sopenharmony_ci * to signal that a conversion is in progress. 2908c2ecf20Sopenharmony_ci * 2918c2ecf20Sopenharmony_ci * Return: 0 if success, -kernel error code otherwise 2928c2ecf20Sopenharmony_ci */ 2938c2ecf20Sopenharmony_cistatic int trigger_bulk_read(struct w1_master *dev_master); 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci/* Sysfs interface declaration */ 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_cistatic ssize_t w1_slave_show(struct device *device, 2988c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_cistatic ssize_t w1_slave_store(struct device *device, 3018c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_cistatic ssize_t w1_seq_show(struct device *device, 3048c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf); 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_cistatic ssize_t temperature_show(struct device *device, 3078c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf); 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_cistatic ssize_t ext_power_show(struct device *device, 3108c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf); 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_cistatic ssize_t resolution_show(struct device *device, 3138c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf); 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_cistatic ssize_t resolution_store(struct device *device, 3168c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size); 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_cistatic ssize_t eeprom_store(struct device *device, 3198c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size); 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_cistatic ssize_t alarms_store(struct device *device, 3228c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *device, 3258c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf); 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_cistatic ssize_t therm_bulk_read_store(struct device *device, 3288c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size); 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_cistatic ssize_t therm_bulk_read_show(struct device *device, 3318c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf); 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_cistatic ssize_t conv_time_show(struct device *device, 3348c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf); 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_cistatic ssize_t conv_time_store(struct device *device, 3378c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 3388c2ecf20Sopenharmony_ci size_t size); 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_cistatic ssize_t features_show(struct device *device, 3418c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf); 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_cistatic ssize_t features_store(struct device *device, 3448c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 3458c2ecf20Sopenharmony_ci size_t size); 3468c2ecf20Sopenharmony_ci/* Attributes declarations */ 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(w1_slave); 3498c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(w1_seq); 3508c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(temperature); 3518c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(ext_power); 3528c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(resolution); 3538c2ecf20Sopenharmony_cistatic DEVICE_ATTR_WO(eeprom); 3548c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(alarms); 3558c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(conv_time); 3568c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(features); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(therm_bulk_read); /* attribut at master level */ 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci/* Interface Functions declaration */ 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci/** 3638c2ecf20Sopenharmony_ci * w1_therm_add_slave() - Called when a new slave is discovered 3648c2ecf20Sopenharmony_ci * @sl: slave just discovered by the master. 3658c2ecf20Sopenharmony_ci * 3668c2ecf20Sopenharmony_ci * Called by the master when the slave is discovered on the bus. Used to 3678c2ecf20Sopenharmony_ci * initialize slave state before the beginning of any communication. 3688c2ecf20Sopenharmony_ci * 3698c2ecf20Sopenharmony_ci * Return: 0 - If success, negative kernel code otherwise 3708c2ecf20Sopenharmony_ci */ 3718c2ecf20Sopenharmony_cistatic int w1_therm_add_slave(struct w1_slave *sl); 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci/** 3748c2ecf20Sopenharmony_ci * w1_therm_remove_slave() - Called when a slave is removed 3758c2ecf20Sopenharmony_ci * @sl: slave to be removed. 3768c2ecf20Sopenharmony_ci * 3778c2ecf20Sopenharmony_ci * Called by the master when the slave is considered not to be on the bus 3788c2ecf20Sopenharmony_ci * anymore. Used to free memory. 3798c2ecf20Sopenharmony_ci */ 3808c2ecf20Sopenharmony_cistatic void w1_therm_remove_slave(struct w1_slave *sl); 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci/* Family attributes */ 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_cistatic struct attribute *w1_therm_attrs[] = { 3858c2ecf20Sopenharmony_ci &dev_attr_w1_slave.attr, 3868c2ecf20Sopenharmony_ci &dev_attr_temperature.attr, 3878c2ecf20Sopenharmony_ci &dev_attr_ext_power.attr, 3888c2ecf20Sopenharmony_ci &dev_attr_resolution.attr, 3898c2ecf20Sopenharmony_ci &dev_attr_eeprom.attr, 3908c2ecf20Sopenharmony_ci &dev_attr_alarms.attr, 3918c2ecf20Sopenharmony_ci &dev_attr_conv_time.attr, 3928c2ecf20Sopenharmony_ci &dev_attr_features.attr, 3938c2ecf20Sopenharmony_ci NULL, 3948c2ecf20Sopenharmony_ci}; 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_cistatic struct attribute *w1_ds18s20_attrs[] = { 3978c2ecf20Sopenharmony_ci &dev_attr_w1_slave.attr, 3988c2ecf20Sopenharmony_ci &dev_attr_temperature.attr, 3998c2ecf20Sopenharmony_ci &dev_attr_ext_power.attr, 4008c2ecf20Sopenharmony_ci &dev_attr_eeprom.attr, 4018c2ecf20Sopenharmony_ci &dev_attr_alarms.attr, 4028c2ecf20Sopenharmony_ci &dev_attr_conv_time.attr, 4038c2ecf20Sopenharmony_ci &dev_attr_features.attr, 4048c2ecf20Sopenharmony_ci NULL, 4058c2ecf20Sopenharmony_ci}; 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_cistatic struct attribute *w1_ds28ea00_attrs[] = { 4088c2ecf20Sopenharmony_ci &dev_attr_w1_slave.attr, 4098c2ecf20Sopenharmony_ci &dev_attr_w1_seq.attr, 4108c2ecf20Sopenharmony_ci &dev_attr_temperature.attr, 4118c2ecf20Sopenharmony_ci &dev_attr_ext_power.attr, 4128c2ecf20Sopenharmony_ci &dev_attr_resolution.attr, 4138c2ecf20Sopenharmony_ci &dev_attr_eeprom.attr, 4148c2ecf20Sopenharmony_ci &dev_attr_alarms.attr, 4158c2ecf20Sopenharmony_ci &dev_attr_conv_time.attr, 4168c2ecf20Sopenharmony_ci &dev_attr_features.attr, 4178c2ecf20Sopenharmony_ci NULL, 4188c2ecf20Sopenharmony_ci}; 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci/* Attribute groups */ 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(w1_therm); 4238c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(w1_ds18s20); 4248c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(w1_ds28ea00); 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci#if IS_REACHABLE(CONFIG_HWMON) 4278c2ecf20Sopenharmony_cistatic int w1_read_temp(struct device *dev, u32 attr, int channel, 4288c2ecf20Sopenharmony_ci long *val); 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_cistatic umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type, 4318c2ecf20Sopenharmony_ci u32 attr, int channel) 4328c2ecf20Sopenharmony_ci{ 4338c2ecf20Sopenharmony_ci return attr == hwmon_temp_input ? 0444 : 0; 4348c2ecf20Sopenharmony_ci} 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_cistatic int w1_read(struct device *dev, enum hwmon_sensor_types type, 4378c2ecf20Sopenharmony_ci u32 attr, int channel, long *val) 4388c2ecf20Sopenharmony_ci{ 4398c2ecf20Sopenharmony_ci switch (type) { 4408c2ecf20Sopenharmony_ci case hwmon_temp: 4418c2ecf20Sopenharmony_ci return w1_read_temp(dev, attr, channel, val); 4428c2ecf20Sopenharmony_ci default: 4438c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 4448c2ecf20Sopenharmony_ci } 4458c2ecf20Sopenharmony_ci} 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_cistatic const u32 w1_temp_config[] = { 4488c2ecf20Sopenharmony_ci HWMON_T_INPUT, 4498c2ecf20Sopenharmony_ci 0 4508c2ecf20Sopenharmony_ci}; 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_cistatic const struct hwmon_channel_info w1_temp = { 4538c2ecf20Sopenharmony_ci .type = hwmon_temp, 4548c2ecf20Sopenharmony_ci .config = w1_temp_config, 4558c2ecf20Sopenharmony_ci}; 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_cistatic const struct hwmon_channel_info *w1_info[] = { 4588c2ecf20Sopenharmony_ci &w1_temp, 4598c2ecf20Sopenharmony_ci NULL 4608c2ecf20Sopenharmony_ci}; 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_cistatic const struct hwmon_ops w1_hwmon_ops = { 4638c2ecf20Sopenharmony_ci .is_visible = w1_is_visible, 4648c2ecf20Sopenharmony_ci .read = w1_read, 4658c2ecf20Sopenharmony_ci}; 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_cistatic const struct hwmon_chip_info w1_chip_info = { 4688c2ecf20Sopenharmony_ci .ops = &w1_hwmon_ops, 4698c2ecf20Sopenharmony_ci .info = w1_info, 4708c2ecf20Sopenharmony_ci}; 4718c2ecf20Sopenharmony_ci#define W1_CHIPINFO (&w1_chip_info) 4728c2ecf20Sopenharmony_ci#else 4738c2ecf20Sopenharmony_ci#define W1_CHIPINFO NULL 4748c2ecf20Sopenharmony_ci#endif 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci/* Family operations */ 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_cistatic const struct w1_family_ops w1_therm_fops = { 4798c2ecf20Sopenharmony_ci .add_slave = w1_therm_add_slave, 4808c2ecf20Sopenharmony_ci .remove_slave = w1_therm_remove_slave, 4818c2ecf20Sopenharmony_ci .groups = w1_therm_groups, 4828c2ecf20Sopenharmony_ci .chip_info = W1_CHIPINFO, 4838c2ecf20Sopenharmony_ci}; 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_cistatic const struct w1_family_ops w1_ds18s20_fops = { 4868c2ecf20Sopenharmony_ci .add_slave = w1_therm_add_slave, 4878c2ecf20Sopenharmony_ci .remove_slave = w1_therm_remove_slave, 4888c2ecf20Sopenharmony_ci .groups = w1_ds18s20_groups, 4898c2ecf20Sopenharmony_ci .chip_info = W1_CHIPINFO, 4908c2ecf20Sopenharmony_ci}; 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_cistatic const struct w1_family_ops w1_ds28ea00_fops = { 4938c2ecf20Sopenharmony_ci .add_slave = w1_therm_add_slave, 4948c2ecf20Sopenharmony_ci .remove_slave = w1_therm_remove_slave, 4958c2ecf20Sopenharmony_ci .groups = w1_ds28ea00_groups, 4968c2ecf20Sopenharmony_ci .chip_info = W1_CHIPINFO, 4978c2ecf20Sopenharmony_ci}; 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci/* Family binding operations struct */ 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_cistatic struct w1_family w1_therm_family_DS18S20 = { 5028c2ecf20Sopenharmony_ci .fid = W1_THERM_DS18S20, 5038c2ecf20Sopenharmony_ci .fops = &w1_ds18s20_fops, 5048c2ecf20Sopenharmony_ci}; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_cistatic struct w1_family w1_therm_family_DS18B20 = { 5078c2ecf20Sopenharmony_ci .fid = W1_THERM_DS18B20, 5088c2ecf20Sopenharmony_ci .fops = &w1_therm_fops, 5098c2ecf20Sopenharmony_ci}; 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_cistatic struct w1_family w1_therm_family_DS1822 = { 5128c2ecf20Sopenharmony_ci .fid = W1_THERM_DS1822, 5138c2ecf20Sopenharmony_ci .fops = &w1_therm_fops, 5148c2ecf20Sopenharmony_ci}; 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_cistatic struct w1_family w1_therm_family_DS28EA00 = { 5178c2ecf20Sopenharmony_ci .fid = W1_THERM_DS28EA00, 5188c2ecf20Sopenharmony_ci .fops = &w1_ds28ea00_fops, 5198c2ecf20Sopenharmony_ci}; 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_cistatic struct w1_family w1_therm_family_DS1825 = { 5228c2ecf20Sopenharmony_ci .fid = W1_THERM_DS1825, 5238c2ecf20Sopenharmony_ci .fops = &w1_therm_fops, 5248c2ecf20Sopenharmony_ci}; 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci/* Device dependent func */ 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_cistatic inline int w1_DS18B20_convert_time(struct w1_slave *sl) 5298c2ecf20Sopenharmony_ci{ 5308c2ecf20Sopenharmony_ci int ret; 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci if (!sl->family_data) 5338c2ecf20Sopenharmony_ci return -ENODEV; /* device unknown */ 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci if (SLAVE_CONV_TIME_OVERRIDE(sl) != CONV_TIME_DEFAULT) 5368c2ecf20Sopenharmony_ci return SLAVE_CONV_TIME_OVERRIDE(sl); 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci /* Return the conversion time, depending on resolution, 5398c2ecf20Sopenharmony_ci * select maximum conversion time among all compatible devices 5408c2ecf20Sopenharmony_ci */ 5418c2ecf20Sopenharmony_ci switch (SLAVE_RESOLUTION(sl)) { 5428c2ecf20Sopenharmony_ci case 9: 5438c2ecf20Sopenharmony_ci ret = 95; 5448c2ecf20Sopenharmony_ci break; 5458c2ecf20Sopenharmony_ci case 10: 5468c2ecf20Sopenharmony_ci ret = 190; 5478c2ecf20Sopenharmony_ci break; 5488c2ecf20Sopenharmony_ci case 11: 5498c2ecf20Sopenharmony_ci ret = 375; 5508c2ecf20Sopenharmony_ci break; 5518c2ecf20Sopenharmony_ci case 12: 5528c2ecf20Sopenharmony_ci ret = 750; 5538c2ecf20Sopenharmony_ci break; 5548c2ecf20Sopenharmony_ci case 13: 5558c2ecf20Sopenharmony_ci ret = 850; /* GX20MH01 only. Datasheet says 500ms, but that's not enough. */ 5568c2ecf20Sopenharmony_ci break; 5578c2ecf20Sopenharmony_ci case 14: 5588c2ecf20Sopenharmony_ci ret = 1600; /* GX20MH01 only. Datasheet says 1000ms - not enough */ 5598c2ecf20Sopenharmony_ci break; 5608c2ecf20Sopenharmony_ci default: 5618c2ecf20Sopenharmony_ci ret = 750; 5628c2ecf20Sopenharmony_ci } 5638c2ecf20Sopenharmony_ci return ret; 5648c2ecf20Sopenharmony_ci} 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_cistatic inline int w1_DS18S20_convert_time(struct w1_slave *sl) 5678c2ecf20Sopenharmony_ci{ 5688c2ecf20Sopenharmony_ci if (!sl->family_data) 5698c2ecf20Sopenharmony_ci return -ENODEV; /* device unknown */ 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci if (SLAVE_CONV_TIME_OVERRIDE(sl) == CONV_TIME_DEFAULT) 5728c2ecf20Sopenharmony_ci return 750; /* default for DS18S20 */ 5738c2ecf20Sopenharmony_ci else 5748c2ecf20Sopenharmony_ci return SLAVE_CONV_TIME_OVERRIDE(sl); 5758c2ecf20Sopenharmony_ci} 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_cistatic inline int w1_DS18B20_write_data(struct w1_slave *sl, 5788c2ecf20Sopenharmony_ci const u8 *data) 5798c2ecf20Sopenharmony_ci{ 5808c2ecf20Sopenharmony_ci return write_scratchpad(sl, data, 3); 5818c2ecf20Sopenharmony_ci} 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_cistatic inline int w1_DS18S20_write_data(struct w1_slave *sl, 5848c2ecf20Sopenharmony_ci const u8 *data) 5858c2ecf20Sopenharmony_ci{ 5868c2ecf20Sopenharmony_ci /* No config register */ 5878c2ecf20Sopenharmony_ci return write_scratchpad(sl, data, 2); 5888c2ecf20Sopenharmony_ci} 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_cistatic inline int w1_DS18B20_set_resolution(struct w1_slave *sl, int val) 5918c2ecf20Sopenharmony_ci{ 5928c2ecf20Sopenharmony_ci int ret; 5938c2ecf20Sopenharmony_ci struct therm_info info, info2; 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci /* DS18B20 resolution is 9 to 12 bits */ 5968c2ecf20Sopenharmony_ci /* GX20MH01 resolution is 9 to 14 bits */ 5978c2ecf20Sopenharmony_ci if (val < W1_THERM_RESOLUTION_MIN || val > W1_THERM_RESOLUTION_MAX) 5988c2ecf20Sopenharmony_ci return -EINVAL; 5998c2ecf20Sopenharmony_ci 6008c2ecf20Sopenharmony_ci /* Calc bit value from resolution */ 6018c2ecf20Sopenharmony_ci val = (val - W1_THERM_RESOLUTION_MIN) << W1_THERM_RESOLUTION_SHIFT; 6028c2ecf20Sopenharmony_ci 6038c2ecf20Sopenharmony_ci /* 6048c2ecf20Sopenharmony_ci * Read the scratchpad to change only the required bits 6058c2ecf20Sopenharmony_ci * (bit5 & bit 6 from byte 4) 6068c2ecf20Sopenharmony_ci */ 6078c2ecf20Sopenharmony_ci ret = read_scratchpad(sl, &info); 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci if (ret) 6108c2ecf20Sopenharmony_ci return ret; 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci info.rom[4] &= ~W1_THERM_RESOLUTION_MASK; 6148c2ecf20Sopenharmony_ci info.rom[4] |= val; 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_ci /* Write data in the device RAM */ 6178c2ecf20Sopenharmony_ci ret = w1_DS18B20_write_data(sl, info.rom + 2); 6188c2ecf20Sopenharmony_ci if (ret) 6198c2ecf20Sopenharmony_ci return ret; 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_ci /* Have to read back the resolution to verify an actual value 6228c2ecf20Sopenharmony_ci * GX20MH01 and DS18B20 are indistinguishable by family number, but resolutions differ 6238c2ecf20Sopenharmony_ci * Some DS18B20 clones don't support resolution change 6248c2ecf20Sopenharmony_ci */ 6258c2ecf20Sopenharmony_ci ret = read_scratchpad(sl, &info2); 6268c2ecf20Sopenharmony_ci if (ret) 6278c2ecf20Sopenharmony_ci /* Scratchpad read fail */ 6288c2ecf20Sopenharmony_ci return ret; 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_ci if ((info2.rom[4] & W1_THERM_RESOLUTION_MASK) == (info.rom[4] & W1_THERM_RESOLUTION_MASK)) 6318c2ecf20Sopenharmony_ci return 0; 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_ci /* Resolution verify error */ 6348c2ecf20Sopenharmony_ci return -EIO; 6358c2ecf20Sopenharmony_ci} 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_cistatic inline int w1_DS18B20_get_resolution(struct w1_slave *sl) 6388c2ecf20Sopenharmony_ci{ 6398c2ecf20Sopenharmony_ci int ret; 6408c2ecf20Sopenharmony_ci int resolution; 6418c2ecf20Sopenharmony_ci struct therm_info info; 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci ret = read_scratchpad(sl, &info); 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci if (ret) 6468c2ecf20Sopenharmony_ci return ret; 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci resolution = ((info.rom[4] & W1_THERM_RESOLUTION_MASK) >> W1_THERM_RESOLUTION_SHIFT) 6498c2ecf20Sopenharmony_ci + W1_THERM_RESOLUTION_MIN; 6508c2ecf20Sopenharmony_ci /* GX20MH01 has one special case: 6518c2ecf20Sopenharmony_ci * >=14 means 14 bits when getting resolution from bit value. 6528c2ecf20Sopenharmony_ci * Other devices have no more then 12 bits. 6538c2ecf20Sopenharmony_ci */ 6548c2ecf20Sopenharmony_ci if (resolution > W1_THERM_RESOLUTION_MAX) 6558c2ecf20Sopenharmony_ci resolution = W1_THERM_RESOLUTION_MAX; 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci return resolution; 6588c2ecf20Sopenharmony_ci} 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_ci/** 6618c2ecf20Sopenharmony_ci * w1_DS18B20_convert_temp() - temperature computation for DS18B20 6628c2ecf20Sopenharmony_ci * @rom: data read from device RAM (8 data bytes + 1 CRC byte) 6638c2ecf20Sopenharmony_ci * 6648c2ecf20Sopenharmony_ci * Can be called for any DS18B20 compliant device. 6658c2ecf20Sopenharmony_ci * 6668c2ecf20Sopenharmony_ci * Return: value in millidegrees Celsius. 6678c2ecf20Sopenharmony_ci */ 6688c2ecf20Sopenharmony_cistatic inline int w1_DS18B20_convert_temp(u8 rom[9]) 6698c2ecf20Sopenharmony_ci{ 6708c2ecf20Sopenharmony_ci u16 bv; 6718c2ecf20Sopenharmony_ci s16 t; 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci /* Signed 16-bit value to unsigned, cpu order */ 6748c2ecf20Sopenharmony_ci bv = le16_to_cpup((__le16 *)rom); 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_ci /* Config register bit R2 = 1 - GX20MH01 in 13 or 14 bit resolution mode */ 6778c2ecf20Sopenharmony_ci if (rom[4] & 0x80) { 6788c2ecf20Sopenharmony_ci /* Insert two temperature bits from config register */ 6798c2ecf20Sopenharmony_ci /* Avoid arithmetic shift of signed value */ 6808c2ecf20Sopenharmony_ci bv = (bv << 2) | (rom[4] & 3); 6818c2ecf20Sopenharmony_ci t = (s16) bv; /* Degrees, lowest bit is 2^-6 */ 6828c2ecf20Sopenharmony_ci return (int)t * 1000 / 64; /* Sign-extend to int; millidegrees */ 6838c2ecf20Sopenharmony_ci } 6848c2ecf20Sopenharmony_ci t = (s16)bv; /* Degrees, lowest bit is 2^-4 */ 6858c2ecf20Sopenharmony_ci return (int)t * 1000 / 16; /* Sign-extend to int; millidegrees */ 6868c2ecf20Sopenharmony_ci} 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci/** 6898c2ecf20Sopenharmony_ci * w1_DS18S20_convert_temp() - temperature computation for DS18S20 6908c2ecf20Sopenharmony_ci * @rom: data read from device RAM (8 data bytes + 1 CRC byte) 6918c2ecf20Sopenharmony_ci * 6928c2ecf20Sopenharmony_ci * Can be called for any DS18S20 compliant device. 6938c2ecf20Sopenharmony_ci * 6948c2ecf20Sopenharmony_ci * Return: value in millidegrees Celsius. 6958c2ecf20Sopenharmony_ci */ 6968c2ecf20Sopenharmony_cistatic inline int w1_DS18S20_convert_temp(u8 rom[9]) 6978c2ecf20Sopenharmony_ci{ 6988c2ecf20Sopenharmony_ci int t, h; 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_ci if (!rom[7]) { 7018c2ecf20Sopenharmony_ci pr_debug("%s: Invalid argument for conversion\n", __func__); 7028c2ecf20Sopenharmony_ci return 0; 7038c2ecf20Sopenharmony_ci } 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci if (rom[1] == 0) 7068c2ecf20Sopenharmony_ci t = ((s32)rom[0] >> 1)*1000; 7078c2ecf20Sopenharmony_ci else 7088c2ecf20Sopenharmony_ci t = 1000*(-1*(s32)(0x100-rom[0]) >> 1); 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci t -= 250; 7118c2ecf20Sopenharmony_ci h = 1000*((s32)rom[7] - (s32)rom[6]); 7128c2ecf20Sopenharmony_ci h /= (s32)rom[7]; 7138c2ecf20Sopenharmony_ci t += h; 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ci return t; 7168c2ecf20Sopenharmony_ci} 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci/* Device capability description */ 7198c2ecf20Sopenharmony_ci/* GX20MH01 device shares family number and structure with DS18B20 */ 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_cistatic struct w1_therm_family_converter w1_therm_families[] = { 7228c2ecf20Sopenharmony_ci { 7238c2ecf20Sopenharmony_ci .f = &w1_therm_family_DS18S20, 7248c2ecf20Sopenharmony_ci .convert = w1_DS18S20_convert_temp, 7258c2ecf20Sopenharmony_ci .get_conversion_time = w1_DS18S20_convert_time, 7268c2ecf20Sopenharmony_ci .set_resolution = NULL, /* no config register */ 7278c2ecf20Sopenharmony_ci .get_resolution = NULL, /* no config register */ 7288c2ecf20Sopenharmony_ci .write_data = w1_DS18S20_write_data, 7298c2ecf20Sopenharmony_ci .bulk_read = true 7308c2ecf20Sopenharmony_ci }, 7318c2ecf20Sopenharmony_ci { 7328c2ecf20Sopenharmony_ci .f = &w1_therm_family_DS1822, 7338c2ecf20Sopenharmony_ci .convert = w1_DS18B20_convert_temp, 7348c2ecf20Sopenharmony_ci .get_conversion_time = w1_DS18B20_convert_time, 7358c2ecf20Sopenharmony_ci .set_resolution = w1_DS18B20_set_resolution, 7368c2ecf20Sopenharmony_ci .get_resolution = w1_DS18B20_get_resolution, 7378c2ecf20Sopenharmony_ci .write_data = w1_DS18B20_write_data, 7388c2ecf20Sopenharmony_ci .bulk_read = true 7398c2ecf20Sopenharmony_ci }, 7408c2ecf20Sopenharmony_ci { 7418c2ecf20Sopenharmony_ci /* Also used for GX20MH01 */ 7428c2ecf20Sopenharmony_ci .f = &w1_therm_family_DS18B20, 7438c2ecf20Sopenharmony_ci .convert = w1_DS18B20_convert_temp, 7448c2ecf20Sopenharmony_ci .get_conversion_time = w1_DS18B20_convert_time, 7458c2ecf20Sopenharmony_ci .set_resolution = w1_DS18B20_set_resolution, 7468c2ecf20Sopenharmony_ci .get_resolution = w1_DS18B20_get_resolution, 7478c2ecf20Sopenharmony_ci .write_data = w1_DS18B20_write_data, 7488c2ecf20Sopenharmony_ci .bulk_read = true 7498c2ecf20Sopenharmony_ci }, 7508c2ecf20Sopenharmony_ci { 7518c2ecf20Sopenharmony_ci .f = &w1_therm_family_DS28EA00, 7528c2ecf20Sopenharmony_ci .convert = w1_DS18B20_convert_temp, 7538c2ecf20Sopenharmony_ci .get_conversion_time = w1_DS18B20_convert_time, 7548c2ecf20Sopenharmony_ci .set_resolution = w1_DS18B20_set_resolution, 7558c2ecf20Sopenharmony_ci .get_resolution = w1_DS18B20_get_resolution, 7568c2ecf20Sopenharmony_ci .write_data = w1_DS18B20_write_data, 7578c2ecf20Sopenharmony_ci .bulk_read = false 7588c2ecf20Sopenharmony_ci }, 7598c2ecf20Sopenharmony_ci { 7608c2ecf20Sopenharmony_ci .f = &w1_therm_family_DS1825, 7618c2ecf20Sopenharmony_ci .convert = w1_DS18B20_convert_temp, 7628c2ecf20Sopenharmony_ci .get_conversion_time = w1_DS18B20_convert_time, 7638c2ecf20Sopenharmony_ci .set_resolution = w1_DS18B20_set_resolution, 7648c2ecf20Sopenharmony_ci .get_resolution = w1_DS18B20_get_resolution, 7658c2ecf20Sopenharmony_ci .write_data = w1_DS18B20_write_data, 7668c2ecf20Sopenharmony_ci .bulk_read = true 7678c2ecf20Sopenharmony_ci } 7688c2ecf20Sopenharmony_ci}; 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci/* Helpers Functions */ 7718c2ecf20Sopenharmony_ci 7728c2ecf20Sopenharmony_ci/** 7738c2ecf20Sopenharmony_ci * device_family() - Retrieve a pointer on &struct w1_therm_family_converter 7748c2ecf20Sopenharmony_ci * @sl: slave to retrieve the device specific structure 7758c2ecf20Sopenharmony_ci * 7768c2ecf20Sopenharmony_ci * Return: pointer to the slaves's family converter, NULL if not known 7778c2ecf20Sopenharmony_ci */ 7788c2ecf20Sopenharmony_cistatic struct w1_therm_family_converter *device_family(struct w1_slave *sl) 7798c2ecf20Sopenharmony_ci{ 7808c2ecf20Sopenharmony_ci struct w1_therm_family_converter *ret = NULL; 7818c2ecf20Sopenharmony_ci int i; 7828c2ecf20Sopenharmony_ci 7838c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) { 7848c2ecf20Sopenharmony_ci if (w1_therm_families[i].f->fid == sl->family->fid) { 7858c2ecf20Sopenharmony_ci ret = &w1_therm_families[i]; 7868c2ecf20Sopenharmony_ci break; 7878c2ecf20Sopenharmony_ci } 7888c2ecf20Sopenharmony_ci } 7898c2ecf20Sopenharmony_ci return ret; 7908c2ecf20Sopenharmony_ci} 7918c2ecf20Sopenharmony_ci 7928c2ecf20Sopenharmony_ci/** 7938c2ecf20Sopenharmony_ci * bus_mutex_lock() - Acquire the mutex 7948c2ecf20Sopenharmony_ci * @lock: w1 bus mutex to acquire 7958c2ecf20Sopenharmony_ci * 7968c2ecf20Sopenharmony_ci * It try to acquire the mutex W1_THERM_MAX_TRY times and wait 7978c2ecf20Sopenharmony_ci * W1_THERM_RETRY_DELAY between 2 attempts. 7988c2ecf20Sopenharmony_ci * 7998c2ecf20Sopenharmony_ci * Return: true is mutex is acquired and lock, false otherwise 8008c2ecf20Sopenharmony_ci */ 8018c2ecf20Sopenharmony_cistatic inline bool bus_mutex_lock(struct mutex *lock) 8028c2ecf20Sopenharmony_ci{ 8038c2ecf20Sopenharmony_ci int max_trying = W1_THERM_MAX_TRY; 8048c2ecf20Sopenharmony_ci 8058c2ecf20Sopenharmony_ci /* try to acquire the mutex, if not, sleep retry_delay before retry) */ 8068c2ecf20Sopenharmony_ci while (mutex_lock_interruptible(lock) != 0 && max_trying > 0) { 8078c2ecf20Sopenharmony_ci unsigned long sleep_rem; 8088c2ecf20Sopenharmony_ci 8098c2ecf20Sopenharmony_ci sleep_rem = msleep_interruptible(W1_THERM_RETRY_DELAY); 8108c2ecf20Sopenharmony_ci if (!sleep_rem) 8118c2ecf20Sopenharmony_ci max_trying--; 8128c2ecf20Sopenharmony_ci } 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_ci if (!max_trying) 8158c2ecf20Sopenharmony_ci return false; /* Didn't acquire the bus mutex */ 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_ci return true; 8188c2ecf20Sopenharmony_ci} 8198c2ecf20Sopenharmony_ci 8208c2ecf20Sopenharmony_ci/** 8218c2ecf20Sopenharmony_ci * check_family_data() - Check if family data and specific functions are present 8228c2ecf20Sopenharmony_ci * @sl: W1 device data 8238c2ecf20Sopenharmony_ci * 8248c2ecf20Sopenharmony_ci * Return: 0 - OK, negative value - error 8258c2ecf20Sopenharmony_ci */ 8268c2ecf20Sopenharmony_cistatic int check_family_data(struct w1_slave *sl) 8278c2ecf20Sopenharmony_ci{ 8288c2ecf20Sopenharmony_ci if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 8298c2ecf20Sopenharmony_ci dev_info(&sl->dev, 8308c2ecf20Sopenharmony_ci "%s: Device is not supported by the driver\n", __func__); 8318c2ecf20Sopenharmony_ci return -EINVAL; /* No device family */ 8328c2ecf20Sopenharmony_ci } 8338c2ecf20Sopenharmony_ci return 0; 8348c2ecf20Sopenharmony_ci} 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci/** 8378c2ecf20Sopenharmony_ci * support_bulk_read() - check if slave support bulk read 8388c2ecf20Sopenharmony_ci * @sl: device to check the ability 8398c2ecf20Sopenharmony_ci * 8408c2ecf20Sopenharmony_ci * Return: true if bulk read is supported, false if not or error 8418c2ecf20Sopenharmony_ci */ 8428c2ecf20Sopenharmony_cistatic inline bool bulk_read_support(struct w1_slave *sl) 8438c2ecf20Sopenharmony_ci{ 8448c2ecf20Sopenharmony_ci if (SLAVE_SPECIFIC_FUNC(sl)) 8458c2ecf20Sopenharmony_ci return SLAVE_SPECIFIC_FUNC(sl)->bulk_read; 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci dev_info(&sl->dev, 8488c2ecf20Sopenharmony_ci "%s: Device not supported by the driver\n", __func__); 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci return false; /* No device family */ 8518c2ecf20Sopenharmony_ci} 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci/** 8548c2ecf20Sopenharmony_ci * conversion_time() - get the Tconv for the slave 8558c2ecf20Sopenharmony_ci * @sl: device to get the conversion time 8568c2ecf20Sopenharmony_ci * 8578c2ecf20Sopenharmony_ci * On device supporting resolution settings, conversion time depend 8588c2ecf20Sopenharmony_ci * on the resolution setting. This helper function get the slave timing, 8598c2ecf20Sopenharmony_ci * depending on its current setting. 8608c2ecf20Sopenharmony_ci * 8618c2ecf20Sopenharmony_ci * Return: conversion time in ms, negative values are kernel error code 8628c2ecf20Sopenharmony_ci */ 8638c2ecf20Sopenharmony_cistatic inline int conversion_time(struct w1_slave *sl) 8648c2ecf20Sopenharmony_ci{ 8658c2ecf20Sopenharmony_ci if (SLAVE_SPECIFIC_FUNC(sl)) 8668c2ecf20Sopenharmony_ci return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl); 8678c2ecf20Sopenharmony_ci 8688c2ecf20Sopenharmony_ci dev_info(&sl->dev, 8698c2ecf20Sopenharmony_ci "%s: Device not supported by the driver\n", __func__); 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_ci return -ENODEV; /* No device family */ 8728c2ecf20Sopenharmony_ci} 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci/** 8758c2ecf20Sopenharmony_ci * temperature_from_RAM() - Convert the read info to temperature 8768c2ecf20Sopenharmony_ci * @sl: device that sent the RAM data 8778c2ecf20Sopenharmony_ci * @rom: read value on the slave device RAM 8788c2ecf20Sopenharmony_ci * 8798c2ecf20Sopenharmony_ci * Device dependent, the function bind the correct computation method. 8808c2ecf20Sopenharmony_ci * 8818c2ecf20Sopenharmony_ci * Return: temperature in 1/1000degC, 0 on error. 8828c2ecf20Sopenharmony_ci */ 8838c2ecf20Sopenharmony_cistatic inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9]) 8848c2ecf20Sopenharmony_ci{ 8858c2ecf20Sopenharmony_ci if (SLAVE_SPECIFIC_FUNC(sl)) 8868c2ecf20Sopenharmony_ci return SLAVE_SPECIFIC_FUNC(sl)->convert(rom); 8878c2ecf20Sopenharmony_ci 8888c2ecf20Sopenharmony_ci dev_info(&sl->dev, 8898c2ecf20Sopenharmony_ci "%s: Device not supported by the driver\n", __func__); 8908c2ecf20Sopenharmony_ci 8918c2ecf20Sopenharmony_ci return 0; /* No device family */ 8928c2ecf20Sopenharmony_ci} 8938c2ecf20Sopenharmony_ci 8948c2ecf20Sopenharmony_ci/** 8958c2ecf20Sopenharmony_ci * int_to_short() - Safe casting of int to short 8968c2ecf20Sopenharmony_ci * 8978c2ecf20Sopenharmony_ci * @i: integer to be converted to short 8988c2ecf20Sopenharmony_ci * 8998c2ecf20Sopenharmony_ci * Device register use 1 byte to store signed integer. 9008c2ecf20Sopenharmony_ci * This helper function convert the int in a signed short, 9018c2ecf20Sopenharmony_ci * using the min/max values that device can measure as limits. 9028c2ecf20Sopenharmony_ci * min/max values are defined by macro. 9038c2ecf20Sopenharmony_ci * 9048c2ecf20Sopenharmony_ci * Return: a short in the range of min/max value 9058c2ecf20Sopenharmony_ci */ 9068c2ecf20Sopenharmony_cistatic inline s8 int_to_short(int i) 9078c2ecf20Sopenharmony_ci{ 9088c2ecf20Sopenharmony_ci /* Prepare to cast to short by eliminating out of range values */ 9098c2ecf20Sopenharmony_ci i = i > MAX_TEMP ? MAX_TEMP : i; 9108c2ecf20Sopenharmony_ci i = i < MIN_TEMP ? MIN_TEMP : i; 9118c2ecf20Sopenharmony_ci return (s8) i; 9128c2ecf20Sopenharmony_ci} 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci/* Interface Functions */ 9158c2ecf20Sopenharmony_ci 9168c2ecf20Sopenharmony_cistatic int w1_therm_add_slave(struct w1_slave *sl) 9178c2ecf20Sopenharmony_ci{ 9188c2ecf20Sopenharmony_ci struct w1_therm_family_converter *sl_family_conv; 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_ci /* Allocate memory */ 9218c2ecf20Sopenharmony_ci sl->family_data = kzalloc(sizeof(struct w1_therm_family_data), 9228c2ecf20Sopenharmony_ci GFP_KERNEL); 9238c2ecf20Sopenharmony_ci if (!sl->family_data) 9248c2ecf20Sopenharmony_ci return -ENOMEM; 9258c2ecf20Sopenharmony_ci 9268c2ecf20Sopenharmony_ci atomic_set(THERM_REFCNT(sl->family_data), 1); 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ci /* Get a pointer to the device specific function struct */ 9298c2ecf20Sopenharmony_ci sl_family_conv = device_family(sl); 9308c2ecf20Sopenharmony_ci if (!sl_family_conv) { 9318c2ecf20Sopenharmony_ci kfree(sl->family_data); 9328c2ecf20Sopenharmony_ci return -ENODEV; 9338c2ecf20Sopenharmony_ci } 9348c2ecf20Sopenharmony_ci /* save this pointer to the device structure */ 9358c2ecf20Sopenharmony_ci SLAVE_SPECIFIC_FUNC(sl) = sl_family_conv; 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci if (bulk_read_support(sl)) { 9388c2ecf20Sopenharmony_ci /* 9398c2ecf20Sopenharmony_ci * add the sys entry to trigger bulk_read 9408c2ecf20Sopenharmony_ci * at master level only the 1st time 9418c2ecf20Sopenharmony_ci */ 9428c2ecf20Sopenharmony_ci if (!bulk_read_device_counter) { 9438c2ecf20Sopenharmony_ci int err = device_create_file(&sl->master->dev, 9448c2ecf20Sopenharmony_ci &dev_attr_therm_bulk_read); 9458c2ecf20Sopenharmony_ci 9468c2ecf20Sopenharmony_ci if (err) 9478c2ecf20Sopenharmony_ci dev_warn(&sl->dev, 9488c2ecf20Sopenharmony_ci "%s: Device has been added, but bulk read is unavailable. err=%d\n", 9498c2ecf20Sopenharmony_ci __func__, err); 9508c2ecf20Sopenharmony_ci } 9518c2ecf20Sopenharmony_ci /* Increment the counter */ 9528c2ecf20Sopenharmony_ci bulk_read_device_counter++; 9538c2ecf20Sopenharmony_ci } 9548c2ecf20Sopenharmony_ci 9558c2ecf20Sopenharmony_ci /* Getting the power mode of the device {external, parasite} */ 9568c2ecf20Sopenharmony_ci SLAVE_POWERMODE(sl) = read_powermode(sl); 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_ci if (SLAVE_POWERMODE(sl) < 0) { 9598c2ecf20Sopenharmony_ci /* no error returned as device has been added */ 9608c2ecf20Sopenharmony_ci dev_warn(&sl->dev, 9618c2ecf20Sopenharmony_ci "%s: Device has been added, but power_mode may be corrupted. err=%d\n", 9628c2ecf20Sopenharmony_ci __func__, SLAVE_POWERMODE(sl)); 9638c2ecf20Sopenharmony_ci } 9648c2ecf20Sopenharmony_ci 9658c2ecf20Sopenharmony_ci /* Getting the resolution of the device */ 9668c2ecf20Sopenharmony_ci if (SLAVE_SPECIFIC_FUNC(sl)->get_resolution) { 9678c2ecf20Sopenharmony_ci SLAVE_RESOLUTION(sl) = 9688c2ecf20Sopenharmony_ci SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl); 9698c2ecf20Sopenharmony_ci if (SLAVE_RESOLUTION(sl) < 0) { 9708c2ecf20Sopenharmony_ci /* no error returned as device has been added */ 9718c2ecf20Sopenharmony_ci dev_warn(&sl->dev, 9728c2ecf20Sopenharmony_ci "%s:Device has been added, but resolution may be corrupted. err=%d\n", 9738c2ecf20Sopenharmony_ci __func__, SLAVE_RESOLUTION(sl)); 9748c2ecf20Sopenharmony_ci } 9758c2ecf20Sopenharmony_ci } 9768c2ecf20Sopenharmony_ci 9778c2ecf20Sopenharmony_ci /* Finally initialize convert_triggered flag */ 9788c2ecf20Sopenharmony_ci SLAVE_CONVERT_TRIGGERED(sl) = 0; 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_ci return 0; 9818c2ecf20Sopenharmony_ci} 9828c2ecf20Sopenharmony_ci 9838c2ecf20Sopenharmony_cistatic void w1_therm_remove_slave(struct w1_slave *sl) 9848c2ecf20Sopenharmony_ci{ 9858c2ecf20Sopenharmony_ci int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data)); 9868c2ecf20Sopenharmony_ci 9878c2ecf20Sopenharmony_ci if (bulk_read_support(sl)) { 9888c2ecf20Sopenharmony_ci bulk_read_device_counter--; 9898c2ecf20Sopenharmony_ci /* Delete the entry if no more device support the feature */ 9908c2ecf20Sopenharmony_ci if (!bulk_read_device_counter) 9918c2ecf20Sopenharmony_ci device_remove_file(&sl->master->dev, 9928c2ecf20Sopenharmony_ci &dev_attr_therm_bulk_read); 9938c2ecf20Sopenharmony_ci } 9948c2ecf20Sopenharmony_ci 9958c2ecf20Sopenharmony_ci while (refcnt) { 9968c2ecf20Sopenharmony_ci msleep(1000); 9978c2ecf20Sopenharmony_ci refcnt = atomic_read(THERM_REFCNT(sl->family_data)); 9988c2ecf20Sopenharmony_ci } 9998c2ecf20Sopenharmony_ci kfree(sl->family_data); 10008c2ecf20Sopenharmony_ci sl->family_data = NULL; 10018c2ecf20Sopenharmony_ci} 10028c2ecf20Sopenharmony_ci 10038c2ecf20Sopenharmony_ci/* Hardware Functions */ 10048c2ecf20Sopenharmony_ci 10058c2ecf20Sopenharmony_ci/* Safe version of reset_select_slave - avoid using the one in w_io.c */ 10068c2ecf20Sopenharmony_cistatic int reset_select_slave(struct w1_slave *sl) 10078c2ecf20Sopenharmony_ci{ 10088c2ecf20Sopenharmony_ci u8 match[9] = { W1_MATCH_ROM, }; 10098c2ecf20Sopenharmony_ci u64 rn = le64_to_cpu(*((u64 *)&sl->reg_num)); 10108c2ecf20Sopenharmony_ci 10118c2ecf20Sopenharmony_ci if (w1_reset_bus(sl->master)) 10128c2ecf20Sopenharmony_ci return -ENODEV; 10138c2ecf20Sopenharmony_ci 10148c2ecf20Sopenharmony_ci memcpy(&match[1], &rn, 8); 10158c2ecf20Sopenharmony_ci w1_write_block(sl->master, match, 9); 10168c2ecf20Sopenharmony_ci 10178c2ecf20Sopenharmony_ci return 0; 10188c2ecf20Sopenharmony_ci} 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_ci/** 10218c2ecf20Sopenharmony_ci * w1_poll_completion - Poll for operation completion, with timeout 10228c2ecf20Sopenharmony_ci * @dev_master: the device master of the bus 10238c2ecf20Sopenharmony_ci * @tout_ms: timeout in milliseconds 10248c2ecf20Sopenharmony_ci * 10258c2ecf20Sopenharmony_ci * The device is answering 0's while an operation is in progress and 1's after it completes 10268c2ecf20Sopenharmony_ci * Timeout may happen if the previous command was not recognised due to a line noise 10278c2ecf20Sopenharmony_ci * 10288c2ecf20Sopenharmony_ci * Return: 0 - OK, negative error - timeout 10298c2ecf20Sopenharmony_ci */ 10308c2ecf20Sopenharmony_cistatic int w1_poll_completion(struct w1_master *dev_master, int tout_ms) 10318c2ecf20Sopenharmony_ci{ 10328c2ecf20Sopenharmony_ci int i; 10338c2ecf20Sopenharmony_ci 10348c2ecf20Sopenharmony_ci for (i = 0; i < tout_ms/W1_POLL_PERIOD; i++) { 10358c2ecf20Sopenharmony_ci /* Delay is before poll, for device to recognize a command */ 10368c2ecf20Sopenharmony_ci msleep(W1_POLL_PERIOD); 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci /* Compare all 8 bits to mitigate a noise on the bus */ 10398c2ecf20Sopenharmony_ci if (w1_read_8(dev_master) == 0xFF) 10408c2ecf20Sopenharmony_ci break; 10418c2ecf20Sopenharmony_ci } 10428c2ecf20Sopenharmony_ci if (i == tout_ms/W1_POLL_PERIOD) 10438c2ecf20Sopenharmony_ci return -EIO; 10448c2ecf20Sopenharmony_ci 10458c2ecf20Sopenharmony_ci return 0; 10468c2ecf20Sopenharmony_ci} 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_cistatic int convert_t(struct w1_slave *sl, struct therm_info *info) 10498c2ecf20Sopenharmony_ci{ 10508c2ecf20Sopenharmony_ci struct w1_master *dev_master = sl->master; 10518c2ecf20Sopenharmony_ci int max_trying = W1_THERM_MAX_TRY; 10528c2ecf20Sopenharmony_ci int t_conv; 10538c2ecf20Sopenharmony_ci int ret = -ENODEV; 10548c2ecf20Sopenharmony_ci bool strong_pullup; 10558c2ecf20Sopenharmony_ci 10568c2ecf20Sopenharmony_ci if (!sl->family_data) 10578c2ecf20Sopenharmony_ci goto error; 10588c2ecf20Sopenharmony_ci 10598c2ecf20Sopenharmony_ci strong_pullup = (w1_strong_pullup == 2 || 10608c2ecf20Sopenharmony_ci (!SLAVE_POWERMODE(sl) && 10618c2ecf20Sopenharmony_ci w1_strong_pullup)); 10628c2ecf20Sopenharmony_ci 10638c2ecf20Sopenharmony_ci if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) { 10648c2ecf20Sopenharmony_ci dev_warn(&sl->dev, 10658c2ecf20Sopenharmony_ci "%s: Disabling W1_THERM_POLL_COMPLETION in parasite power mode.\n", 10668c2ecf20Sopenharmony_ci __func__); 10678c2ecf20Sopenharmony_ci SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION; 10688c2ecf20Sopenharmony_ci } 10698c2ecf20Sopenharmony_ci 10708c2ecf20Sopenharmony_ci /* get conversion duration device and id dependent */ 10718c2ecf20Sopenharmony_ci t_conv = conversion_time(sl); 10728c2ecf20Sopenharmony_ci 10738c2ecf20Sopenharmony_ci memset(info->rom, 0, sizeof(info->rom)); 10748c2ecf20Sopenharmony_ci 10758c2ecf20Sopenharmony_ci /* prevent the slave from going away in sleep */ 10768c2ecf20Sopenharmony_ci atomic_inc(THERM_REFCNT(sl->family_data)); 10778c2ecf20Sopenharmony_ci 10788c2ecf20Sopenharmony_ci if (!bus_mutex_lock(&dev_master->bus_mutex)) { 10798c2ecf20Sopenharmony_ci ret = -EAGAIN; /* Didn't acquire the mutex */ 10808c2ecf20Sopenharmony_ci goto dec_refcnt; 10818c2ecf20Sopenharmony_ci } 10828c2ecf20Sopenharmony_ci 10838c2ecf20Sopenharmony_ci while (max_trying-- && ret) { /* ret should be 0 */ 10848c2ecf20Sopenharmony_ci 10858c2ecf20Sopenharmony_ci info->verdict = 0; 10868c2ecf20Sopenharmony_ci info->crc = 0; 10878c2ecf20Sopenharmony_ci /* safe version to select slave */ 10888c2ecf20Sopenharmony_ci if (!reset_select_slave(sl)) { 10898c2ecf20Sopenharmony_ci unsigned long sleep_rem; 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_ci /* 750ms strong pullup (or delay) after the convert */ 10928c2ecf20Sopenharmony_ci if (strong_pullup) 10938c2ecf20Sopenharmony_ci w1_next_pullup(dev_master, t_conv); 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ci w1_write_8(dev_master, W1_CONVERT_TEMP); 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_ci if (SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) { 10988c2ecf20Sopenharmony_ci ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP); 10998c2ecf20Sopenharmony_ci if (ret) { 11008c2ecf20Sopenharmony_ci dev_dbg(&sl->dev, "%s: Timeout\n", __func__); 11018c2ecf20Sopenharmony_ci goto mt_unlock; 11028c2ecf20Sopenharmony_ci } 11038c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 11048c2ecf20Sopenharmony_ci } else if (!strong_pullup) { /*no device need pullup */ 11058c2ecf20Sopenharmony_ci sleep_rem = msleep_interruptible(t_conv); 11068c2ecf20Sopenharmony_ci if (sleep_rem != 0) { 11078c2ecf20Sopenharmony_ci ret = -EINTR; 11088c2ecf20Sopenharmony_ci goto mt_unlock; 11098c2ecf20Sopenharmony_ci } 11108c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 11118c2ecf20Sopenharmony_ci } else { /*some device need pullup */ 11128c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 11138c2ecf20Sopenharmony_ci sleep_rem = msleep_interruptible(t_conv); 11148c2ecf20Sopenharmony_ci if (sleep_rem != 0) { 11158c2ecf20Sopenharmony_ci ret = -EINTR; 11168c2ecf20Sopenharmony_ci goto dec_refcnt; 11178c2ecf20Sopenharmony_ci } 11188c2ecf20Sopenharmony_ci } 11198c2ecf20Sopenharmony_ci ret = read_scratchpad(sl, info); 11208c2ecf20Sopenharmony_ci 11218c2ecf20Sopenharmony_ci /* If enabled, check for conversion success */ 11228c2ecf20Sopenharmony_ci if ((SLAVE_FEATURES(sl) & W1_THERM_CHECK_RESULT) && 11238c2ecf20Sopenharmony_ci (info->rom[6] == 0xC) && 11248c2ecf20Sopenharmony_ci ((info->rom[1] == 0x5 && info->rom[0] == 0x50) || 11258c2ecf20Sopenharmony_ci (info->rom[1] == 0x7 && info->rom[0] == 0xFF)) 11268c2ecf20Sopenharmony_ci ) { 11278c2ecf20Sopenharmony_ci /* Invalid reading (scratchpad byte 6 = 0xC) 11288c2ecf20Sopenharmony_ci * due to insufficient conversion time 11298c2ecf20Sopenharmony_ci * or power failure. 11308c2ecf20Sopenharmony_ci */ 11318c2ecf20Sopenharmony_ci ret = -EIO; 11328c2ecf20Sopenharmony_ci } 11338c2ecf20Sopenharmony_ci 11348c2ecf20Sopenharmony_ci goto dec_refcnt; 11358c2ecf20Sopenharmony_ci } 11368c2ecf20Sopenharmony_ci 11378c2ecf20Sopenharmony_ci } 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_cimt_unlock: 11408c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 11418c2ecf20Sopenharmony_cidec_refcnt: 11428c2ecf20Sopenharmony_ci atomic_dec(THERM_REFCNT(sl->family_data)); 11438c2ecf20Sopenharmony_cierror: 11448c2ecf20Sopenharmony_ci return ret; 11458c2ecf20Sopenharmony_ci} 11468c2ecf20Sopenharmony_ci 11478c2ecf20Sopenharmony_cistatic int conv_time_measure(struct w1_slave *sl, int *conv_time) 11488c2ecf20Sopenharmony_ci{ 11498c2ecf20Sopenharmony_ci struct therm_info inf, 11508c2ecf20Sopenharmony_ci *info = &inf; 11518c2ecf20Sopenharmony_ci struct w1_master *dev_master = sl->master; 11528c2ecf20Sopenharmony_ci int max_trying = W1_THERM_MAX_TRY; 11538c2ecf20Sopenharmony_ci int ret = -ENODEV; 11548c2ecf20Sopenharmony_ci bool strong_pullup; 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci if (!sl->family_data) 11578c2ecf20Sopenharmony_ci goto error; 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_ci strong_pullup = (w1_strong_pullup == 2 || 11608c2ecf20Sopenharmony_ci (!SLAVE_POWERMODE(sl) && 11618c2ecf20Sopenharmony_ci w1_strong_pullup)); 11628c2ecf20Sopenharmony_ci 11638c2ecf20Sopenharmony_ci if (strong_pullup) { 11648c2ecf20Sopenharmony_ci pr_info("%s: Measure with strong_pullup is not supported.\n", __func__); 11658c2ecf20Sopenharmony_ci return -EINVAL; 11668c2ecf20Sopenharmony_ci } 11678c2ecf20Sopenharmony_ci 11688c2ecf20Sopenharmony_ci memset(info->rom, 0, sizeof(info->rom)); 11698c2ecf20Sopenharmony_ci 11708c2ecf20Sopenharmony_ci /* prevent the slave from going away in sleep */ 11718c2ecf20Sopenharmony_ci atomic_inc(THERM_REFCNT(sl->family_data)); 11728c2ecf20Sopenharmony_ci 11738c2ecf20Sopenharmony_ci if (!bus_mutex_lock(&dev_master->bus_mutex)) { 11748c2ecf20Sopenharmony_ci ret = -EAGAIN; /* Didn't acquire the mutex */ 11758c2ecf20Sopenharmony_ci goto dec_refcnt; 11768c2ecf20Sopenharmony_ci } 11778c2ecf20Sopenharmony_ci 11788c2ecf20Sopenharmony_ci while (max_trying-- && ret) { /* ret should be 0 */ 11798c2ecf20Sopenharmony_ci info->verdict = 0; 11808c2ecf20Sopenharmony_ci info->crc = 0; 11818c2ecf20Sopenharmony_ci /* safe version to select slave */ 11828c2ecf20Sopenharmony_ci if (!reset_select_slave(sl)) { 11838c2ecf20Sopenharmony_ci int j_start, j_end; 11848c2ecf20Sopenharmony_ci 11858c2ecf20Sopenharmony_ci /*no device need pullup */ 11868c2ecf20Sopenharmony_ci w1_write_8(dev_master, W1_CONVERT_TEMP); 11878c2ecf20Sopenharmony_ci 11888c2ecf20Sopenharmony_ci j_start = jiffies; 11898c2ecf20Sopenharmony_ci ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP); 11908c2ecf20Sopenharmony_ci if (ret) { 11918c2ecf20Sopenharmony_ci dev_dbg(&sl->dev, "%s: Timeout\n", __func__); 11928c2ecf20Sopenharmony_ci goto mt_unlock; 11938c2ecf20Sopenharmony_ci } 11948c2ecf20Sopenharmony_ci j_end = jiffies; 11958c2ecf20Sopenharmony_ci /* 1.2x increase for variation and changes over temperature range */ 11968c2ecf20Sopenharmony_ci *conv_time = jiffies_to_msecs(j_end-j_start)*12/10; 11978c2ecf20Sopenharmony_ci pr_debug("W1 Measure complete, conv_time = %d, HZ=%d.\n", 11988c2ecf20Sopenharmony_ci *conv_time, HZ); 11998c2ecf20Sopenharmony_ci if (*conv_time <= CONV_TIME_MEASURE) { 12008c2ecf20Sopenharmony_ci ret = -EIO; 12018c2ecf20Sopenharmony_ci goto mt_unlock; 12028c2ecf20Sopenharmony_ci } 12038c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 12048c2ecf20Sopenharmony_ci ret = read_scratchpad(sl, info); 12058c2ecf20Sopenharmony_ci goto dec_refcnt; 12068c2ecf20Sopenharmony_ci } 12078c2ecf20Sopenharmony_ci 12088c2ecf20Sopenharmony_ci } 12098c2ecf20Sopenharmony_cimt_unlock: 12108c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 12118c2ecf20Sopenharmony_cidec_refcnt: 12128c2ecf20Sopenharmony_ci atomic_dec(THERM_REFCNT(sl->family_data)); 12138c2ecf20Sopenharmony_cierror: 12148c2ecf20Sopenharmony_ci return ret; 12158c2ecf20Sopenharmony_ci} 12168c2ecf20Sopenharmony_ci 12178c2ecf20Sopenharmony_cistatic int read_scratchpad(struct w1_slave *sl, struct therm_info *info) 12188c2ecf20Sopenharmony_ci{ 12198c2ecf20Sopenharmony_ci struct w1_master *dev_master = sl->master; 12208c2ecf20Sopenharmony_ci int max_trying = W1_THERM_MAX_TRY; 12218c2ecf20Sopenharmony_ci int ret = -ENODEV; 12228c2ecf20Sopenharmony_ci 12238c2ecf20Sopenharmony_ci info->verdict = 0; 12248c2ecf20Sopenharmony_ci 12258c2ecf20Sopenharmony_ci if (!sl->family_data) 12268c2ecf20Sopenharmony_ci goto error; 12278c2ecf20Sopenharmony_ci 12288c2ecf20Sopenharmony_ci memset(info->rom, 0, sizeof(info->rom)); 12298c2ecf20Sopenharmony_ci 12308c2ecf20Sopenharmony_ci /* prevent the slave from going away in sleep */ 12318c2ecf20Sopenharmony_ci atomic_inc(THERM_REFCNT(sl->family_data)); 12328c2ecf20Sopenharmony_ci 12338c2ecf20Sopenharmony_ci if (!bus_mutex_lock(&dev_master->bus_mutex)) { 12348c2ecf20Sopenharmony_ci ret = -EAGAIN; /* Didn't acquire the mutex */ 12358c2ecf20Sopenharmony_ci goto dec_refcnt; 12368c2ecf20Sopenharmony_ci } 12378c2ecf20Sopenharmony_ci 12388c2ecf20Sopenharmony_ci while (max_trying-- && ret) { /* ret should be 0 */ 12398c2ecf20Sopenharmony_ci /* safe version to select slave */ 12408c2ecf20Sopenharmony_ci if (!reset_select_slave(sl)) { 12418c2ecf20Sopenharmony_ci u8 nb_bytes_read; 12428c2ecf20Sopenharmony_ci 12438c2ecf20Sopenharmony_ci w1_write_8(dev_master, W1_READ_SCRATCHPAD); 12448c2ecf20Sopenharmony_ci 12458c2ecf20Sopenharmony_ci nb_bytes_read = w1_read_block(dev_master, info->rom, 9); 12468c2ecf20Sopenharmony_ci if (nb_bytes_read != 9) { 12478c2ecf20Sopenharmony_ci dev_warn(&sl->dev, 12488c2ecf20Sopenharmony_ci "w1_read_block(): returned %u instead of 9.\n", 12498c2ecf20Sopenharmony_ci nb_bytes_read); 12508c2ecf20Sopenharmony_ci ret = -EIO; 12518c2ecf20Sopenharmony_ci } 12528c2ecf20Sopenharmony_ci 12538c2ecf20Sopenharmony_ci info->crc = w1_calc_crc8(info->rom, 8); 12548c2ecf20Sopenharmony_ci 12558c2ecf20Sopenharmony_ci if (info->rom[8] == info->crc) { 12568c2ecf20Sopenharmony_ci info->verdict = 1; 12578c2ecf20Sopenharmony_ci ret = 0; 12588c2ecf20Sopenharmony_ci } else 12598c2ecf20Sopenharmony_ci ret = -EIO; /* CRC not checked */ 12608c2ecf20Sopenharmony_ci } 12618c2ecf20Sopenharmony_ci 12628c2ecf20Sopenharmony_ci } 12638c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 12648c2ecf20Sopenharmony_ci 12658c2ecf20Sopenharmony_cidec_refcnt: 12668c2ecf20Sopenharmony_ci atomic_dec(THERM_REFCNT(sl->family_data)); 12678c2ecf20Sopenharmony_cierror: 12688c2ecf20Sopenharmony_ci return ret; 12698c2ecf20Sopenharmony_ci} 12708c2ecf20Sopenharmony_ci 12718c2ecf20Sopenharmony_cistatic int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes) 12728c2ecf20Sopenharmony_ci{ 12738c2ecf20Sopenharmony_ci struct w1_master *dev_master = sl->master; 12748c2ecf20Sopenharmony_ci int max_trying = W1_THERM_MAX_TRY; 12758c2ecf20Sopenharmony_ci int ret = -ENODEV; 12768c2ecf20Sopenharmony_ci 12778c2ecf20Sopenharmony_ci if (!sl->family_data) 12788c2ecf20Sopenharmony_ci goto error; 12798c2ecf20Sopenharmony_ci 12808c2ecf20Sopenharmony_ci /* prevent the slave from going away in sleep */ 12818c2ecf20Sopenharmony_ci atomic_inc(THERM_REFCNT(sl->family_data)); 12828c2ecf20Sopenharmony_ci 12838c2ecf20Sopenharmony_ci if (!bus_mutex_lock(&dev_master->bus_mutex)) { 12848c2ecf20Sopenharmony_ci ret = -EAGAIN; /* Didn't acquire the mutex */ 12858c2ecf20Sopenharmony_ci goto dec_refcnt; 12868c2ecf20Sopenharmony_ci } 12878c2ecf20Sopenharmony_ci 12888c2ecf20Sopenharmony_ci while (max_trying-- && ret) { /* ret should be 0 */ 12898c2ecf20Sopenharmony_ci /* safe version to select slave */ 12908c2ecf20Sopenharmony_ci if (!reset_select_slave(sl)) { 12918c2ecf20Sopenharmony_ci w1_write_8(dev_master, W1_WRITE_SCRATCHPAD); 12928c2ecf20Sopenharmony_ci w1_write_block(dev_master, data, nb_bytes); 12938c2ecf20Sopenharmony_ci ret = 0; 12948c2ecf20Sopenharmony_ci } 12958c2ecf20Sopenharmony_ci } 12968c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 12978c2ecf20Sopenharmony_ci 12988c2ecf20Sopenharmony_cidec_refcnt: 12998c2ecf20Sopenharmony_ci atomic_dec(THERM_REFCNT(sl->family_data)); 13008c2ecf20Sopenharmony_cierror: 13018c2ecf20Sopenharmony_ci return ret; 13028c2ecf20Sopenharmony_ci} 13038c2ecf20Sopenharmony_ci 13048c2ecf20Sopenharmony_cistatic int copy_scratchpad(struct w1_slave *sl) 13058c2ecf20Sopenharmony_ci{ 13068c2ecf20Sopenharmony_ci struct w1_master *dev_master = sl->master; 13078c2ecf20Sopenharmony_ci int max_trying = W1_THERM_MAX_TRY; 13088c2ecf20Sopenharmony_ci int t_write, ret = -ENODEV; 13098c2ecf20Sopenharmony_ci bool strong_pullup; 13108c2ecf20Sopenharmony_ci 13118c2ecf20Sopenharmony_ci if (!sl->family_data) 13128c2ecf20Sopenharmony_ci goto error; 13138c2ecf20Sopenharmony_ci 13148c2ecf20Sopenharmony_ci t_write = W1_THERM_EEPROM_WRITE_DELAY; 13158c2ecf20Sopenharmony_ci strong_pullup = (w1_strong_pullup == 2 || 13168c2ecf20Sopenharmony_ci (!SLAVE_POWERMODE(sl) && 13178c2ecf20Sopenharmony_ci w1_strong_pullup)); 13188c2ecf20Sopenharmony_ci 13198c2ecf20Sopenharmony_ci /* prevent the slave from going away in sleep */ 13208c2ecf20Sopenharmony_ci atomic_inc(THERM_REFCNT(sl->family_data)); 13218c2ecf20Sopenharmony_ci 13228c2ecf20Sopenharmony_ci if (!bus_mutex_lock(&dev_master->bus_mutex)) { 13238c2ecf20Sopenharmony_ci ret = -EAGAIN; /* Didn't acquire the mutex */ 13248c2ecf20Sopenharmony_ci goto dec_refcnt; 13258c2ecf20Sopenharmony_ci } 13268c2ecf20Sopenharmony_ci 13278c2ecf20Sopenharmony_ci while (max_trying-- && ret) { /* ret should be 0 */ 13288c2ecf20Sopenharmony_ci /* safe version to select slave */ 13298c2ecf20Sopenharmony_ci if (!reset_select_slave(sl)) { 13308c2ecf20Sopenharmony_ci unsigned long sleep_rem; 13318c2ecf20Sopenharmony_ci 13328c2ecf20Sopenharmony_ci /* 10ms strong pullup (or delay) after the convert */ 13338c2ecf20Sopenharmony_ci if (strong_pullup) 13348c2ecf20Sopenharmony_ci w1_next_pullup(dev_master, t_write); 13358c2ecf20Sopenharmony_ci 13368c2ecf20Sopenharmony_ci w1_write_8(dev_master, W1_COPY_SCRATCHPAD); 13378c2ecf20Sopenharmony_ci 13388c2ecf20Sopenharmony_ci if (strong_pullup) { 13398c2ecf20Sopenharmony_ci sleep_rem = msleep_interruptible(t_write); 13408c2ecf20Sopenharmony_ci if (sleep_rem != 0) { 13418c2ecf20Sopenharmony_ci ret = -EINTR; 13428c2ecf20Sopenharmony_ci goto mt_unlock; 13438c2ecf20Sopenharmony_ci } 13448c2ecf20Sopenharmony_ci } 13458c2ecf20Sopenharmony_ci ret = 0; 13468c2ecf20Sopenharmony_ci } 13478c2ecf20Sopenharmony_ci 13488c2ecf20Sopenharmony_ci } 13498c2ecf20Sopenharmony_ci 13508c2ecf20Sopenharmony_cimt_unlock: 13518c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 13528c2ecf20Sopenharmony_cidec_refcnt: 13538c2ecf20Sopenharmony_ci atomic_dec(THERM_REFCNT(sl->family_data)); 13548c2ecf20Sopenharmony_cierror: 13558c2ecf20Sopenharmony_ci return ret; 13568c2ecf20Sopenharmony_ci} 13578c2ecf20Sopenharmony_ci 13588c2ecf20Sopenharmony_cistatic int recall_eeprom(struct w1_slave *sl) 13598c2ecf20Sopenharmony_ci{ 13608c2ecf20Sopenharmony_ci struct w1_master *dev_master = sl->master; 13618c2ecf20Sopenharmony_ci int max_trying = W1_THERM_MAX_TRY; 13628c2ecf20Sopenharmony_ci int ret = -ENODEV; 13638c2ecf20Sopenharmony_ci 13648c2ecf20Sopenharmony_ci if (!sl->family_data) 13658c2ecf20Sopenharmony_ci goto error; 13668c2ecf20Sopenharmony_ci 13678c2ecf20Sopenharmony_ci /* prevent the slave from going away in sleep */ 13688c2ecf20Sopenharmony_ci atomic_inc(THERM_REFCNT(sl->family_data)); 13698c2ecf20Sopenharmony_ci 13708c2ecf20Sopenharmony_ci if (!bus_mutex_lock(&dev_master->bus_mutex)) { 13718c2ecf20Sopenharmony_ci ret = -EAGAIN; /* Didn't acquire the mutex */ 13728c2ecf20Sopenharmony_ci goto dec_refcnt; 13738c2ecf20Sopenharmony_ci } 13748c2ecf20Sopenharmony_ci 13758c2ecf20Sopenharmony_ci while (max_trying-- && ret) { /* ret should be 0 */ 13768c2ecf20Sopenharmony_ci /* safe version to select slave */ 13778c2ecf20Sopenharmony_ci if (!reset_select_slave(sl)) { 13788c2ecf20Sopenharmony_ci 13798c2ecf20Sopenharmony_ci w1_write_8(dev_master, W1_RECALL_EEPROM); 13808c2ecf20Sopenharmony_ci ret = w1_poll_completion(dev_master, W1_POLL_RECALL_EEPROM); 13818c2ecf20Sopenharmony_ci } 13828c2ecf20Sopenharmony_ci 13838c2ecf20Sopenharmony_ci } 13848c2ecf20Sopenharmony_ci 13858c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 13868c2ecf20Sopenharmony_ci 13878c2ecf20Sopenharmony_cidec_refcnt: 13888c2ecf20Sopenharmony_ci atomic_dec(THERM_REFCNT(sl->family_data)); 13898c2ecf20Sopenharmony_cierror: 13908c2ecf20Sopenharmony_ci return ret; 13918c2ecf20Sopenharmony_ci} 13928c2ecf20Sopenharmony_ci 13938c2ecf20Sopenharmony_cistatic int read_powermode(struct w1_slave *sl) 13948c2ecf20Sopenharmony_ci{ 13958c2ecf20Sopenharmony_ci struct w1_master *dev_master = sl->master; 13968c2ecf20Sopenharmony_ci int max_trying = W1_THERM_MAX_TRY; 13978c2ecf20Sopenharmony_ci int ret = -ENODEV; 13988c2ecf20Sopenharmony_ci 13998c2ecf20Sopenharmony_ci if (!sl->family_data) 14008c2ecf20Sopenharmony_ci goto error; 14018c2ecf20Sopenharmony_ci 14028c2ecf20Sopenharmony_ci /* prevent the slave from going away in sleep */ 14038c2ecf20Sopenharmony_ci atomic_inc(THERM_REFCNT(sl->family_data)); 14048c2ecf20Sopenharmony_ci 14058c2ecf20Sopenharmony_ci if (!bus_mutex_lock(&dev_master->bus_mutex)) { 14068c2ecf20Sopenharmony_ci ret = -EAGAIN; /* Didn't acquire the mutex */ 14078c2ecf20Sopenharmony_ci goto dec_refcnt; 14088c2ecf20Sopenharmony_ci } 14098c2ecf20Sopenharmony_ci 14108c2ecf20Sopenharmony_ci while ((max_trying--) && (ret < 0)) { 14118c2ecf20Sopenharmony_ci /* safe version to select slave */ 14128c2ecf20Sopenharmony_ci if (!reset_select_slave(sl)) { 14138c2ecf20Sopenharmony_ci w1_write_8(dev_master, W1_READ_PSUPPLY); 14148c2ecf20Sopenharmony_ci /* 14158c2ecf20Sopenharmony_ci * Emit a read time slot and read only one bit, 14168c2ecf20Sopenharmony_ci * 1 is externally powered, 14178c2ecf20Sopenharmony_ci * 0 is parasite powered 14188c2ecf20Sopenharmony_ci */ 14198c2ecf20Sopenharmony_ci ret = w1_touch_bit(dev_master, 1); 14208c2ecf20Sopenharmony_ci /* ret should be either 1 either 0 */ 14218c2ecf20Sopenharmony_ci } 14228c2ecf20Sopenharmony_ci } 14238c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 14248c2ecf20Sopenharmony_ci 14258c2ecf20Sopenharmony_cidec_refcnt: 14268c2ecf20Sopenharmony_ci atomic_dec(THERM_REFCNT(sl->family_data)); 14278c2ecf20Sopenharmony_cierror: 14288c2ecf20Sopenharmony_ci return ret; 14298c2ecf20Sopenharmony_ci} 14308c2ecf20Sopenharmony_ci 14318c2ecf20Sopenharmony_cistatic int trigger_bulk_read(struct w1_master *dev_master) 14328c2ecf20Sopenharmony_ci{ 14338c2ecf20Sopenharmony_ci struct w1_slave *sl = NULL; /* used to iterate through slaves */ 14348c2ecf20Sopenharmony_ci int max_trying = W1_THERM_MAX_TRY; 14358c2ecf20Sopenharmony_ci int t_conv = 0; 14368c2ecf20Sopenharmony_ci int ret = -ENODEV; 14378c2ecf20Sopenharmony_ci bool strong_pullup = false; 14388c2ecf20Sopenharmony_ci 14398c2ecf20Sopenharmony_ci /* 14408c2ecf20Sopenharmony_ci * Check whether there are parasite powered device on the bus, 14418c2ecf20Sopenharmony_ci * and compute duration of conversion for these devices 14428c2ecf20Sopenharmony_ci * so we can apply a strong pullup if required 14438c2ecf20Sopenharmony_ci */ 14448c2ecf20Sopenharmony_ci list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) { 14458c2ecf20Sopenharmony_ci if (!sl->family_data) 14468c2ecf20Sopenharmony_ci goto error; 14478c2ecf20Sopenharmony_ci if (bulk_read_support(sl)) { 14488c2ecf20Sopenharmony_ci int t_cur = conversion_time(sl); 14498c2ecf20Sopenharmony_ci 14508c2ecf20Sopenharmony_ci t_conv = t_cur > t_conv ? t_cur : t_conv; 14518c2ecf20Sopenharmony_ci strong_pullup = strong_pullup || 14528c2ecf20Sopenharmony_ci (w1_strong_pullup == 2 || 14538c2ecf20Sopenharmony_ci (!SLAVE_POWERMODE(sl) && 14548c2ecf20Sopenharmony_ci w1_strong_pullup)); 14558c2ecf20Sopenharmony_ci } 14568c2ecf20Sopenharmony_ci } 14578c2ecf20Sopenharmony_ci 14588c2ecf20Sopenharmony_ci /* 14598c2ecf20Sopenharmony_ci * t_conv is the max conversion time required on the bus 14608c2ecf20Sopenharmony_ci * If its 0, no device support the bulk read feature 14618c2ecf20Sopenharmony_ci */ 14628c2ecf20Sopenharmony_ci if (!t_conv) 14638c2ecf20Sopenharmony_ci goto error; 14648c2ecf20Sopenharmony_ci 14658c2ecf20Sopenharmony_ci if (!bus_mutex_lock(&dev_master->bus_mutex)) { 14668c2ecf20Sopenharmony_ci ret = -EAGAIN; /* Didn't acquire the mutex */ 14678c2ecf20Sopenharmony_ci goto error; 14688c2ecf20Sopenharmony_ci } 14698c2ecf20Sopenharmony_ci 14708c2ecf20Sopenharmony_ci while ((max_trying--) && (ret < 0)) { /* ret should be either 0 */ 14718c2ecf20Sopenharmony_ci 14728c2ecf20Sopenharmony_ci if (!w1_reset_bus(dev_master)) { /* Just reset the bus */ 14738c2ecf20Sopenharmony_ci unsigned long sleep_rem; 14748c2ecf20Sopenharmony_ci 14758c2ecf20Sopenharmony_ci w1_write_8(dev_master, W1_SKIP_ROM); 14768c2ecf20Sopenharmony_ci 14778c2ecf20Sopenharmony_ci if (strong_pullup) /* Apply pullup if required */ 14788c2ecf20Sopenharmony_ci w1_next_pullup(dev_master, t_conv); 14798c2ecf20Sopenharmony_ci 14808c2ecf20Sopenharmony_ci w1_write_8(dev_master, W1_CONVERT_TEMP); 14818c2ecf20Sopenharmony_ci 14828c2ecf20Sopenharmony_ci /* set a flag to instruct that converT pending */ 14838c2ecf20Sopenharmony_ci list_for_each_entry(sl, 14848c2ecf20Sopenharmony_ci &dev_master->slist, w1_slave_entry) { 14858c2ecf20Sopenharmony_ci if (bulk_read_support(sl)) 14868c2ecf20Sopenharmony_ci SLAVE_CONVERT_TRIGGERED(sl) = -1; 14878c2ecf20Sopenharmony_ci } 14888c2ecf20Sopenharmony_ci 14898c2ecf20Sopenharmony_ci if (strong_pullup) { /* some device need pullup */ 14908c2ecf20Sopenharmony_ci sleep_rem = msleep_interruptible(t_conv); 14918c2ecf20Sopenharmony_ci if (sleep_rem != 0) { 14928c2ecf20Sopenharmony_ci ret = -EINTR; 14938c2ecf20Sopenharmony_ci goto mt_unlock; 14948c2ecf20Sopenharmony_ci } 14958c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 14968c2ecf20Sopenharmony_ci } else { 14978c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 14988c2ecf20Sopenharmony_ci sleep_rem = msleep_interruptible(t_conv); 14998c2ecf20Sopenharmony_ci if (sleep_rem != 0) { 15008c2ecf20Sopenharmony_ci ret = -EINTR; 15018c2ecf20Sopenharmony_ci goto set_flag; 15028c2ecf20Sopenharmony_ci } 15038c2ecf20Sopenharmony_ci } 15048c2ecf20Sopenharmony_ci ret = 0; 15058c2ecf20Sopenharmony_ci goto set_flag; 15068c2ecf20Sopenharmony_ci } 15078c2ecf20Sopenharmony_ci } 15088c2ecf20Sopenharmony_ci 15098c2ecf20Sopenharmony_cimt_unlock: 15108c2ecf20Sopenharmony_ci mutex_unlock(&dev_master->bus_mutex); 15118c2ecf20Sopenharmony_ciset_flag: 15128c2ecf20Sopenharmony_ci /* set a flag to register convsersion is done */ 15138c2ecf20Sopenharmony_ci list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) { 15148c2ecf20Sopenharmony_ci if (bulk_read_support(sl)) 15158c2ecf20Sopenharmony_ci SLAVE_CONVERT_TRIGGERED(sl) = 1; 15168c2ecf20Sopenharmony_ci } 15178c2ecf20Sopenharmony_cierror: 15188c2ecf20Sopenharmony_ci return ret; 15198c2ecf20Sopenharmony_ci} 15208c2ecf20Sopenharmony_ci 15218c2ecf20Sopenharmony_ci/* Sysfs Interface definition */ 15228c2ecf20Sopenharmony_ci 15238c2ecf20Sopenharmony_cistatic ssize_t w1_slave_show(struct device *device, 15248c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 15258c2ecf20Sopenharmony_ci{ 15268c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 15278c2ecf20Sopenharmony_ci struct therm_info info; 15288c2ecf20Sopenharmony_ci u8 *family_data = sl->family_data; 15298c2ecf20Sopenharmony_ci int ret, i; 15308c2ecf20Sopenharmony_ci ssize_t c = PAGE_SIZE; 15318c2ecf20Sopenharmony_ci 15328c2ecf20Sopenharmony_ci if (bulk_read_support(sl)) { 15338c2ecf20Sopenharmony_ci if (SLAVE_CONVERT_TRIGGERED(sl) < 0) { 15348c2ecf20Sopenharmony_ci dev_dbg(device, 15358c2ecf20Sopenharmony_ci "%s: Conversion in progress, retry later\n", 15368c2ecf20Sopenharmony_ci __func__); 15378c2ecf20Sopenharmony_ci return 0; 15388c2ecf20Sopenharmony_ci } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) { 15398c2ecf20Sopenharmony_ci /* A bulk read has been issued, read the device RAM */ 15408c2ecf20Sopenharmony_ci ret = read_scratchpad(sl, &info); 15418c2ecf20Sopenharmony_ci SLAVE_CONVERT_TRIGGERED(sl) = 0; 15428c2ecf20Sopenharmony_ci } else 15438c2ecf20Sopenharmony_ci ret = convert_t(sl, &info); 15448c2ecf20Sopenharmony_ci } else 15458c2ecf20Sopenharmony_ci ret = convert_t(sl, &info); 15468c2ecf20Sopenharmony_ci 15478c2ecf20Sopenharmony_ci if (ret < 0) { 15488c2ecf20Sopenharmony_ci dev_dbg(device, 15498c2ecf20Sopenharmony_ci "%s: Temperature data may be corrupted. err=%d\n", 15508c2ecf20Sopenharmony_ci __func__, ret); 15518c2ecf20Sopenharmony_ci return 0; 15528c2ecf20Sopenharmony_ci } 15538c2ecf20Sopenharmony_ci 15548c2ecf20Sopenharmony_ci for (i = 0; i < 9; ++i) 15558c2ecf20Sopenharmony_ci c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]); 15568c2ecf20Sopenharmony_ci c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n", 15578c2ecf20Sopenharmony_ci info.crc, (info.verdict) ? "YES" : "NO"); 15588c2ecf20Sopenharmony_ci 15598c2ecf20Sopenharmony_ci if (info.verdict) 15608c2ecf20Sopenharmony_ci memcpy(family_data, info.rom, sizeof(info.rom)); 15618c2ecf20Sopenharmony_ci else 15628c2ecf20Sopenharmony_ci dev_warn(device, "%s:Read failed CRC check\n", __func__); 15638c2ecf20Sopenharmony_ci 15648c2ecf20Sopenharmony_ci for (i = 0; i < 9; ++i) 15658c2ecf20Sopenharmony_ci c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", 15668c2ecf20Sopenharmony_ci ((u8 *)family_data)[i]); 15678c2ecf20Sopenharmony_ci 15688c2ecf20Sopenharmony_ci c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n", 15698c2ecf20Sopenharmony_ci temperature_from_RAM(sl, info.rom)); 15708c2ecf20Sopenharmony_ci 15718c2ecf20Sopenharmony_ci ret = PAGE_SIZE - c; 15728c2ecf20Sopenharmony_ci return ret; 15738c2ecf20Sopenharmony_ci} 15748c2ecf20Sopenharmony_ci 15758c2ecf20Sopenharmony_cistatic ssize_t w1_slave_store(struct device *device, 15768c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 15778c2ecf20Sopenharmony_ci size_t size) 15788c2ecf20Sopenharmony_ci{ 15798c2ecf20Sopenharmony_ci int val, ret = 0; 15808c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 15818c2ecf20Sopenharmony_ci 15828c2ecf20Sopenharmony_ci ret = kstrtoint(buf, 10, &val); /* converting user entry to int */ 15838c2ecf20Sopenharmony_ci 15848c2ecf20Sopenharmony_ci if (ret) { /* conversion error */ 15858c2ecf20Sopenharmony_ci dev_info(device, 15868c2ecf20Sopenharmony_ci "%s: conversion error. err= %d\n", __func__, ret); 15878c2ecf20Sopenharmony_ci return size; /* return size to avoid call back again */ 15888c2ecf20Sopenharmony_ci } 15898c2ecf20Sopenharmony_ci 15908c2ecf20Sopenharmony_ci if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 15918c2ecf20Sopenharmony_ci dev_info(device, 15928c2ecf20Sopenharmony_ci "%s: Device not supported by the driver\n", __func__); 15938c2ecf20Sopenharmony_ci return size; /* No device family */ 15948c2ecf20Sopenharmony_ci } 15958c2ecf20Sopenharmony_ci 15968c2ecf20Sopenharmony_ci if (val == 0) /* val=0 : trigger a EEPROM save */ 15978c2ecf20Sopenharmony_ci ret = copy_scratchpad(sl); 15988c2ecf20Sopenharmony_ci else { 15998c2ecf20Sopenharmony_ci if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution) 16008c2ecf20Sopenharmony_ci ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val); 16018c2ecf20Sopenharmony_ci } 16028c2ecf20Sopenharmony_ci 16038c2ecf20Sopenharmony_ci if (ret) { 16048c2ecf20Sopenharmony_ci dev_warn(device, "%s: Set resolution - error %d\n", __func__, ret); 16058c2ecf20Sopenharmony_ci /* Propagate error to userspace */ 16068c2ecf20Sopenharmony_ci return ret; 16078c2ecf20Sopenharmony_ci } 16088c2ecf20Sopenharmony_ci SLAVE_RESOLUTION(sl) = val; 16098c2ecf20Sopenharmony_ci /* Reset the conversion time to default - it depends on resolution */ 16108c2ecf20Sopenharmony_ci SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT; 16118c2ecf20Sopenharmony_ci 16128c2ecf20Sopenharmony_ci return size; /* always return size to avoid infinite calling */ 16138c2ecf20Sopenharmony_ci} 16148c2ecf20Sopenharmony_ci 16158c2ecf20Sopenharmony_cistatic ssize_t temperature_show(struct device *device, 16168c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 16178c2ecf20Sopenharmony_ci{ 16188c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 16198c2ecf20Sopenharmony_ci struct therm_info info; 16208c2ecf20Sopenharmony_ci int ret = 0; 16218c2ecf20Sopenharmony_ci 16228c2ecf20Sopenharmony_ci if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 16238c2ecf20Sopenharmony_ci dev_info(device, 16248c2ecf20Sopenharmony_ci "%s: Device not supported by the driver\n", __func__); 16258c2ecf20Sopenharmony_ci return 0; /* No device family */ 16268c2ecf20Sopenharmony_ci } 16278c2ecf20Sopenharmony_ci 16288c2ecf20Sopenharmony_ci if (bulk_read_support(sl)) { 16298c2ecf20Sopenharmony_ci if (SLAVE_CONVERT_TRIGGERED(sl) < 0) { 16308c2ecf20Sopenharmony_ci dev_dbg(device, 16318c2ecf20Sopenharmony_ci "%s: Conversion in progress, retry later\n", 16328c2ecf20Sopenharmony_ci __func__); 16338c2ecf20Sopenharmony_ci return 0; 16348c2ecf20Sopenharmony_ci } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) { 16358c2ecf20Sopenharmony_ci /* A bulk read has been issued, read the device RAM */ 16368c2ecf20Sopenharmony_ci ret = read_scratchpad(sl, &info); 16378c2ecf20Sopenharmony_ci SLAVE_CONVERT_TRIGGERED(sl) = 0; 16388c2ecf20Sopenharmony_ci } else 16398c2ecf20Sopenharmony_ci ret = convert_t(sl, &info); 16408c2ecf20Sopenharmony_ci } else 16418c2ecf20Sopenharmony_ci ret = convert_t(sl, &info); 16428c2ecf20Sopenharmony_ci 16438c2ecf20Sopenharmony_ci if (ret < 0) { 16448c2ecf20Sopenharmony_ci dev_dbg(device, 16458c2ecf20Sopenharmony_ci "%s: Temperature data may be corrupted. err=%d\n", 16468c2ecf20Sopenharmony_ci __func__, ret); 16478c2ecf20Sopenharmony_ci return 0; 16488c2ecf20Sopenharmony_ci } 16498c2ecf20Sopenharmony_ci 16508c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", temperature_from_RAM(sl, info.rom)); 16518c2ecf20Sopenharmony_ci} 16528c2ecf20Sopenharmony_ci 16538c2ecf20Sopenharmony_cistatic ssize_t ext_power_show(struct device *device, 16548c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 16558c2ecf20Sopenharmony_ci{ 16568c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 16578c2ecf20Sopenharmony_ci 16588c2ecf20Sopenharmony_ci if (!sl->family_data) { 16598c2ecf20Sopenharmony_ci dev_info(device, 16608c2ecf20Sopenharmony_ci "%s: Device not supported by the driver\n", __func__); 16618c2ecf20Sopenharmony_ci return 0; /* No device family */ 16628c2ecf20Sopenharmony_ci } 16638c2ecf20Sopenharmony_ci 16648c2ecf20Sopenharmony_ci /* Getting the power mode of the device {external, parasite} */ 16658c2ecf20Sopenharmony_ci SLAVE_POWERMODE(sl) = read_powermode(sl); 16668c2ecf20Sopenharmony_ci 16678c2ecf20Sopenharmony_ci if (SLAVE_POWERMODE(sl) < 0) { 16688c2ecf20Sopenharmony_ci dev_dbg(device, 16698c2ecf20Sopenharmony_ci "%s: Power_mode may be corrupted. err=%d\n", 16708c2ecf20Sopenharmony_ci __func__, SLAVE_POWERMODE(sl)); 16718c2ecf20Sopenharmony_ci } 16728c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", SLAVE_POWERMODE(sl)); 16738c2ecf20Sopenharmony_ci} 16748c2ecf20Sopenharmony_ci 16758c2ecf20Sopenharmony_cistatic ssize_t resolution_show(struct device *device, 16768c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 16778c2ecf20Sopenharmony_ci{ 16788c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 16798c2ecf20Sopenharmony_ci 16808c2ecf20Sopenharmony_ci if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 16818c2ecf20Sopenharmony_ci dev_info(device, 16828c2ecf20Sopenharmony_ci "%s: Device not supported by the driver\n", __func__); 16838c2ecf20Sopenharmony_ci return 0; /* No device family */ 16848c2ecf20Sopenharmony_ci } 16858c2ecf20Sopenharmony_ci 16868c2ecf20Sopenharmony_ci /* get the correct function depending on the device */ 16878c2ecf20Sopenharmony_ci SLAVE_RESOLUTION(sl) = SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl); 16888c2ecf20Sopenharmony_ci if (SLAVE_RESOLUTION(sl) < 0) { 16898c2ecf20Sopenharmony_ci dev_dbg(device, 16908c2ecf20Sopenharmony_ci "%s: Resolution may be corrupted. err=%d\n", 16918c2ecf20Sopenharmony_ci __func__, SLAVE_RESOLUTION(sl)); 16928c2ecf20Sopenharmony_ci } 16938c2ecf20Sopenharmony_ci 16948c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", SLAVE_RESOLUTION(sl)); 16958c2ecf20Sopenharmony_ci} 16968c2ecf20Sopenharmony_ci 16978c2ecf20Sopenharmony_cistatic ssize_t resolution_store(struct device *device, 16988c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 16998c2ecf20Sopenharmony_ci{ 17008c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 17018c2ecf20Sopenharmony_ci int val; 17028c2ecf20Sopenharmony_ci int ret = 0; 17038c2ecf20Sopenharmony_ci 17048c2ecf20Sopenharmony_ci ret = kstrtoint(buf, 10, &val); /* converting user entry to int */ 17058c2ecf20Sopenharmony_ci 17068c2ecf20Sopenharmony_ci if (ret) { /* conversion error */ 17078c2ecf20Sopenharmony_ci dev_info(device, 17088c2ecf20Sopenharmony_ci "%s: conversion error. err= %d\n", __func__, ret); 17098c2ecf20Sopenharmony_ci return size; /* return size to avoid call back again */ 17108c2ecf20Sopenharmony_ci } 17118c2ecf20Sopenharmony_ci 17128c2ecf20Sopenharmony_ci if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 17138c2ecf20Sopenharmony_ci dev_info(device, 17148c2ecf20Sopenharmony_ci "%s: Device not supported by the driver\n", __func__); 17158c2ecf20Sopenharmony_ci return size; /* No device family */ 17168c2ecf20Sopenharmony_ci } 17178c2ecf20Sopenharmony_ci 17188c2ecf20Sopenharmony_ci /* 17198c2ecf20Sopenharmony_ci * Don't deal with the val enterd by user, 17208c2ecf20Sopenharmony_ci * only device knows what is correct or not 17218c2ecf20Sopenharmony_ci */ 17228c2ecf20Sopenharmony_ci 17238c2ecf20Sopenharmony_ci /* get the correct function depending on the device */ 17248c2ecf20Sopenharmony_ci ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val); 17258c2ecf20Sopenharmony_ci 17268c2ecf20Sopenharmony_ci if (ret) 17278c2ecf20Sopenharmony_ci return ret; 17288c2ecf20Sopenharmony_ci 17298c2ecf20Sopenharmony_ci SLAVE_RESOLUTION(sl) = val; 17308c2ecf20Sopenharmony_ci /* Reset the conversion time to default because it depends on resolution */ 17318c2ecf20Sopenharmony_ci SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT; 17328c2ecf20Sopenharmony_ci 17338c2ecf20Sopenharmony_ci return size; 17348c2ecf20Sopenharmony_ci} 17358c2ecf20Sopenharmony_ci 17368c2ecf20Sopenharmony_cistatic ssize_t eeprom_store(struct device *device, 17378c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 17388c2ecf20Sopenharmony_ci{ 17398c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 17408c2ecf20Sopenharmony_ci int ret = -EINVAL; /* Invalid argument */ 17418c2ecf20Sopenharmony_ci 17428c2ecf20Sopenharmony_ci if (size == sizeof(EEPROM_CMD_WRITE)) { 17438c2ecf20Sopenharmony_ci if (!strncmp(buf, EEPROM_CMD_WRITE, sizeof(EEPROM_CMD_WRITE)-1)) 17448c2ecf20Sopenharmony_ci ret = copy_scratchpad(sl); 17458c2ecf20Sopenharmony_ci } else if (size == sizeof(EEPROM_CMD_READ)) { 17468c2ecf20Sopenharmony_ci if (!strncmp(buf, EEPROM_CMD_READ, sizeof(EEPROM_CMD_READ)-1)) 17478c2ecf20Sopenharmony_ci ret = recall_eeprom(sl); 17488c2ecf20Sopenharmony_ci } 17498c2ecf20Sopenharmony_ci 17508c2ecf20Sopenharmony_ci if (ret) 17518c2ecf20Sopenharmony_ci dev_info(device, "%s: error in process %d\n", __func__, ret); 17528c2ecf20Sopenharmony_ci 17538c2ecf20Sopenharmony_ci return size; 17548c2ecf20Sopenharmony_ci} 17558c2ecf20Sopenharmony_ci 17568c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *device, 17578c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 17588c2ecf20Sopenharmony_ci{ 17598c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 17608c2ecf20Sopenharmony_ci int ret; 17618c2ecf20Sopenharmony_ci s8 th = 0, tl = 0; 17628c2ecf20Sopenharmony_ci struct therm_info scratchpad; 17638c2ecf20Sopenharmony_ci 17648c2ecf20Sopenharmony_ci ret = read_scratchpad(sl, &scratchpad); 17658c2ecf20Sopenharmony_ci 17668c2ecf20Sopenharmony_ci if (!ret) { 17678c2ecf20Sopenharmony_ci th = scratchpad.rom[2]; /* TH is byte 2 */ 17688c2ecf20Sopenharmony_ci tl = scratchpad.rom[3]; /* TL is byte 3 */ 17698c2ecf20Sopenharmony_ci } else { 17708c2ecf20Sopenharmony_ci dev_info(device, 17718c2ecf20Sopenharmony_ci "%s: error reading alarms register %d\n", 17728c2ecf20Sopenharmony_ci __func__, ret); 17738c2ecf20Sopenharmony_ci } 17748c2ecf20Sopenharmony_ci 17758c2ecf20Sopenharmony_ci return sprintf(buf, "%hd %hd\n", tl, th); 17768c2ecf20Sopenharmony_ci} 17778c2ecf20Sopenharmony_ci 17788c2ecf20Sopenharmony_cistatic ssize_t alarms_store(struct device *device, 17798c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 17808c2ecf20Sopenharmony_ci{ 17818c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 17828c2ecf20Sopenharmony_ci struct therm_info info; 17838c2ecf20Sopenharmony_ci u8 new_config_register[3]; /* array of data to be written */ 17848c2ecf20Sopenharmony_ci int temp, ret; 17858c2ecf20Sopenharmony_ci char *token = NULL; 17868c2ecf20Sopenharmony_ci s8 tl, th, tt; /* 1 byte per value + temp ring order */ 17878c2ecf20Sopenharmony_ci char *p_args, *orig; 17888c2ecf20Sopenharmony_ci 17898c2ecf20Sopenharmony_ci p_args = orig = kmalloc(size, GFP_KERNEL); 17908c2ecf20Sopenharmony_ci /* Safe string copys as buf is const */ 17918c2ecf20Sopenharmony_ci if (!p_args) { 17928c2ecf20Sopenharmony_ci dev_warn(device, 17938c2ecf20Sopenharmony_ci "%s: error unable to allocate memory %d\n", 17948c2ecf20Sopenharmony_ci __func__, -ENOMEM); 17958c2ecf20Sopenharmony_ci return size; 17968c2ecf20Sopenharmony_ci } 17978c2ecf20Sopenharmony_ci strcpy(p_args, buf); 17988c2ecf20Sopenharmony_ci 17998c2ecf20Sopenharmony_ci /* Split string using space char */ 18008c2ecf20Sopenharmony_ci token = strsep(&p_args, " "); 18018c2ecf20Sopenharmony_ci 18028c2ecf20Sopenharmony_ci if (!token) { 18038c2ecf20Sopenharmony_ci dev_info(device, 18048c2ecf20Sopenharmony_ci "%s: error parsing args %d\n", __func__, -EINVAL); 18058c2ecf20Sopenharmony_ci goto free_m; 18068c2ecf20Sopenharmony_ci } 18078c2ecf20Sopenharmony_ci 18088c2ecf20Sopenharmony_ci /* Convert 1st entry to int */ 18098c2ecf20Sopenharmony_ci ret = kstrtoint (token, 10, &temp); 18108c2ecf20Sopenharmony_ci if (ret) { 18118c2ecf20Sopenharmony_ci dev_info(device, 18128c2ecf20Sopenharmony_ci "%s: error parsing args %d\n", __func__, ret); 18138c2ecf20Sopenharmony_ci goto free_m; 18148c2ecf20Sopenharmony_ci } 18158c2ecf20Sopenharmony_ci 18168c2ecf20Sopenharmony_ci tl = int_to_short(temp); 18178c2ecf20Sopenharmony_ci 18188c2ecf20Sopenharmony_ci /* Split string using space char */ 18198c2ecf20Sopenharmony_ci token = strsep(&p_args, " "); 18208c2ecf20Sopenharmony_ci if (!token) { 18218c2ecf20Sopenharmony_ci dev_info(device, 18228c2ecf20Sopenharmony_ci "%s: error parsing args %d\n", __func__, -EINVAL); 18238c2ecf20Sopenharmony_ci goto free_m; 18248c2ecf20Sopenharmony_ci } 18258c2ecf20Sopenharmony_ci /* Convert 2nd entry to int */ 18268c2ecf20Sopenharmony_ci ret = kstrtoint (token, 10, &temp); 18278c2ecf20Sopenharmony_ci if (ret) { 18288c2ecf20Sopenharmony_ci dev_info(device, 18298c2ecf20Sopenharmony_ci "%s: error parsing args %d\n", __func__, ret); 18308c2ecf20Sopenharmony_ci goto free_m; 18318c2ecf20Sopenharmony_ci } 18328c2ecf20Sopenharmony_ci 18338c2ecf20Sopenharmony_ci /* Prepare to cast to short by eliminating out of range values */ 18348c2ecf20Sopenharmony_ci th = int_to_short(temp); 18358c2ecf20Sopenharmony_ci 18368c2ecf20Sopenharmony_ci /* Reorder if required th and tl */ 18378c2ecf20Sopenharmony_ci if (tl > th) { 18388c2ecf20Sopenharmony_ci tt = tl; tl = th; th = tt; 18398c2ecf20Sopenharmony_ci } 18408c2ecf20Sopenharmony_ci 18418c2ecf20Sopenharmony_ci /* 18428c2ecf20Sopenharmony_ci * Read the scratchpad to change only the required bits 18438c2ecf20Sopenharmony_ci * (th : byte 2 - tl: byte 3) 18448c2ecf20Sopenharmony_ci */ 18458c2ecf20Sopenharmony_ci ret = read_scratchpad(sl, &info); 18468c2ecf20Sopenharmony_ci if (!ret) { 18478c2ecf20Sopenharmony_ci new_config_register[0] = th; /* Byte 2 */ 18488c2ecf20Sopenharmony_ci new_config_register[1] = tl; /* Byte 3 */ 18498c2ecf20Sopenharmony_ci new_config_register[2] = info.rom[4];/* Byte 4 */ 18508c2ecf20Sopenharmony_ci } else { 18518c2ecf20Sopenharmony_ci dev_info(device, 18528c2ecf20Sopenharmony_ci "%s: error reading from the slave device %d\n", 18538c2ecf20Sopenharmony_ci __func__, ret); 18548c2ecf20Sopenharmony_ci goto free_m; 18558c2ecf20Sopenharmony_ci } 18568c2ecf20Sopenharmony_ci 18578c2ecf20Sopenharmony_ci /* Write data in the device RAM */ 18588c2ecf20Sopenharmony_ci if (!SLAVE_SPECIFIC_FUNC(sl)) { 18598c2ecf20Sopenharmony_ci dev_info(device, 18608c2ecf20Sopenharmony_ci "%s: Device not supported by the driver %d\n", 18618c2ecf20Sopenharmony_ci __func__, -ENODEV); 18628c2ecf20Sopenharmony_ci goto free_m; 18638c2ecf20Sopenharmony_ci } 18648c2ecf20Sopenharmony_ci 18658c2ecf20Sopenharmony_ci ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register); 18668c2ecf20Sopenharmony_ci if (ret) 18678c2ecf20Sopenharmony_ci dev_info(device, 18688c2ecf20Sopenharmony_ci "%s: error writing to the slave device %d\n", 18698c2ecf20Sopenharmony_ci __func__, ret); 18708c2ecf20Sopenharmony_ci 18718c2ecf20Sopenharmony_cifree_m: 18728c2ecf20Sopenharmony_ci /* free allocated memory */ 18738c2ecf20Sopenharmony_ci kfree(orig); 18748c2ecf20Sopenharmony_ci 18758c2ecf20Sopenharmony_ci return size; 18768c2ecf20Sopenharmony_ci} 18778c2ecf20Sopenharmony_ci 18788c2ecf20Sopenharmony_cistatic ssize_t therm_bulk_read_store(struct device *device, 18798c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 18808c2ecf20Sopenharmony_ci{ 18818c2ecf20Sopenharmony_ci struct w1_master *dev_master = dev_to_w1_master(device); 18828c2ecf20Sopenharmony_ci int ret = -EINVAL; /* Invalid argument */ 18838c2ecf20Sopenharmony_ci 18848c2ecf20Sopenharmony_ci if (size == sizeof(BULK_TRIGGER_CMD)) 18858c2ecf20Sopenharmony_ci if (!strncmp(buf, BULK_TRIGGER_CMD, 18868c2ecf20Sopenharmony_ci sizeof(BULK_TRIGGER_CMD)-1)) 18878c2ecf20Sopenharmony_ci ret = trigger_bulk_read(dev_master); 18888c2ecf20Sopenharmony_ci 18898c2ecf20Sopenharmony_ci if (ret) 18908c2ecf20Sopenharmony_ci dev_info(device, 18918c2ecf20Sopenharmony_ci "%s: unable to trigger a bulk read on the bus. err=%d\n", 18928c2ecf20Sopenharmony_ci __func__, ret); 18938c2ecf20Sopenharmony_ci 18948c2ecf20Sopenharmony_ci return size; 18958c2ecf20Sopenharmony_ci} 18968c2ecf20Sopenharmony_ci 18978c2ecf20Sopenharmony_cistatic ssize_t therm_bulk_read_show(struct device *device, 18988c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 18998c2ecf20Sopenharmony_ci{ 19008c2ecf20Sopenharmony_ci struct w1_master *dev_master = dev_to_w1_master(device); 19018c2ecf20Sopenharmony_ci struct w1_slave *sl = NULL; 19028c2ecf20Sopenharmony_ci int ret = 0; 19038c2ecf20Sopenharmony_ci 19048c2ecf20Sopenharmony_ci list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) { 19058c2ecf20Sopenharmony_ci if (sl->family_data) { 19068c2ecf20Sopenharmony_ci if (bulk_read_support(sl)) { 19078c2ecf20Sopenharmony_ci if (SLAVE_CONVERT_TRIGGERED(sl) == -1) { 19088c2ecf20Sopenharmony_ci ret = -1; 19098c2ecf20Sopenharmony_ci goto show_result; 19108c2ecf20Sopenharmony_ci } 19118c2ecf20Sopenharmony_ci if (SLAVE_CONVERT_TRIGGERED(sl) == 1) 19128c2ecf20Sopenharmony_ci /* continue to check other slaves */ 19138c2ecf20Sopenharmony_ci ret = 1; 19148c2ecf20Sopenharmony_ci } 19158c2ecf20Sopenharmony_ci } 19168c2ecf20Sopenharmony_ci } 19178c2ecf20Sopenharmony_cishow_result: 19188c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", ret); 19198c2ecf20Sopenharmony_ci} 19208c2ecf20Sopenharmony_ci 19218c2ecf20Sopenharmony_cistatic ssize_t conv_time_show(struct device *device, 19228c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 19238c2ecf20Sopenharmony_ci{ 19248c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 19258c2ecf20Sopenharmony_ci 19268c2ecf20Sopenharmony_ci if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 19278c2ecf20Sopenharmony_ci dev_info(device, 19288c2ecf20Sopenharmony_ci "%s: Device is not supported by the driver\n", __func__); 19298c2ecf20Sopenharmony_ci return 0; /* No device family */ 19308c2ecf20Sopenharmony_ci } 19318c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", conversion_time(sl)); 19328c2ecf20Sopenharmony_ci} 19338c2ecf20Sopenharmony_ci 19348c2ecf20Sopenharmony_cistatic ssize_t conv_time_store(struct device *device, 19358c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 19368c2ecf20Sopenharmony_ci{ 19378c2ecf20Sopenharmony_ci int val, ret = 0; 19388c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 19398c2ecf20Sopenharmony_ci 19408c2ecf20Sopenharmony_ci if (kstrtoint(buf, 10, &val)) /* converting user entry to int */ 19418c2ecf20Sopenharmony_ci return -EINVAL; 19428c2ecf20Sopenharmony_ci 19438c2ecf20Sopenharmony_ci if (check_family_data(sl)) 19448c2ecf20Sopenharmony_ci return -ENODEV; 19458c2ecf20Sopenharmony_ci 19468c2ecf20Sopenharmony_ci if (val != CONV_TIME_MEASURE) { 19478c2ecf20Sopenharmony_ci if (val >= CONV_TIME_DEFAULT) 19488c2ecf20Sopenharmony_ci SLAVE_CONV_TIME_OVERRIDE(sl) = val; 19498c2ecf20Sopenharmony_ci else 19508c2ecf20Sopenharmony_ci return -EINVAL; 19518c2ecf20Sopenharmony_ci 19528c2ecf20Sopenharmony_ci } else { 19538c2ecf20Sopenharmony_ci int conv_time; 19548c2ecf20Sopenharmony_ci 19558c2ecf20Sopenharmony_ci ret = conv_time_measure(sl, &conv_time); 19568c2ecf20Sopenharmony_ci if (ret) 19578c2ecf20Sopenharmony_ci return -EIO; 19588c2ecf20Sopenharmony_ci SLAVE_CONV_TIME_OVERRIDE(sl) = conv_time; 19598c2ecf20Sopenharmony_ci } 19608c2ecf20Sopenharmony_ci return size; 19618c2ecf20Sopenharmony_ci} 19628c2ecf20Sopenharmony_ci 19638c2ecf20Sopenharmony_cistatic ssize_t features_show(struct device *device, 19648c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 19658c2ecf20Sopenharmony_ci{ 19668c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 19678c2ecf20Sopenharmony_ci 19688c2ecf20Sopenharmony_ci if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 19698c2ecf20Sopenharmony_ci dev_info(device, 19708c2ecf20Sopenharmony_ci "%s: Device not supported by the driver\n", __func__); 19718c2ecf20Sopenharmony_ci return 0; /* No device family */ 19728c2ecf20Sopenharmony_ci } 19738c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", SLAVE_FEATURES(sl)); 19748c2ecf20Sopenharmony_ci} 19758c2ecf20Sopenharmony_ci 19768c2ecf20Sopenharmony_cistatic ssize_t features_store(struct device *device, 19778c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 19788c2ecf20Sopenharmony_ci{ 19798c2ecf20Sopenharmony_ci int val, ret = 0; 19808c2ecf20Sopenharmony_ci bool strong_pullup; 19818c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 19828c2ecf20Sopenharmony_ci 19838c2ecf20Sopenharmony_ci ret = kstrtouint(buf, 10, &val); /* converting user entry to int */ 19848c2ecf20Sopenharmony_ci if (ret) 19858c2ecf20Sopenharmony_ci return -EINVAL; /* invalid number */ 19868c2ecf20Sopenharmony_ci 19878c2ecf20Sopenharmony_ci if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 19888c2ecf20Sopenharmony_ci dev_info(device, "%s: Device not supported by the driver\n", __func__); 19898c2ecf20Sopenharmony_ci return -ENODEV; 19908c2ecf20Sopenharmony_ci } 19918c2ecf20Sopenharmony_ci 19928c2ecf20Sopenharmony_ci if ((val & W1_THERM_FEATURES_MASK) != val) 19938c2ecf20Sopenharmony_ci return -EINVAL; 19948c2ecf20Sopenharmony_ci 19958c2ecf20Sopenharmony_ci SLAVE_FEATURES(sl) = val; 19968c2ecf20Sopenharmony_ci 19978c2ecf20Sopenharmony_ci strong_pullup = (w1_strong_pullup == 2 || 19988c2ecf20Sopenharmony_ci (!SLAVE_POWERMODE(sl) && 19998c2ecf20Sopenharmony_ci w1_strong_pullup)); 20008c2ecf20Sopenharmony_ci 20018c2ecf20Sopenharmony_ci if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) { 20028c2ecf20Sopenharmony_ci dev_warn(&sl->dev, 20038c2ecf20Sopenharmony_ci "%s: W1_THERM_POLL_COMPLETION disabled in parasite power mode.\n", 20048c2ecf20Sopenharmony_ci __func__); 20058c2ecf20Sopenharmony_ci SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION; 20068c2ecf20Sopenharmony_ci } 20078c2ecf20Sopenharmony_ci 20088c2ecf20Sopenharmony_ci return size; 20098c2ecf20Sopenharmony_ci} 20108c2ecf20Sopenharmony_ci 20118c2ecf20Sopenharmony_ci#if IS_REACHABLE(CONFIG_HWMON) 20128c2ecf20Sopenharmony_cistatic int w1_read_temp(struct device *device, u32 attr, int channel, 20138c2ecf20Sopenharmony_ci long *val) 20148c2ecf20Sopenharmony_ci{ 20158c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_get_drvdata(device); 20168c2ecf20Sopenharmony_ci struct therm_info info; 20178c2ecf20Sopenharmony_ci int ret; 20188c2ecf20Sopenharmony_ci 20198c2ecf20Sopenharmony_ci switch (attr) { 20208c2ecf20Sopenharmony_ci case hwmon_temp_input: 20218c2ecf20Sopenharmony_ci ret = convert_t(sl, &info); 20228c2ecf20Sopenharmony_ci if (ret) 20238c2ecf20Sopenharmony_ci return ret; 20248c2ecf20Sopenharmony_ci 20258c2ecf20Sopenharmony_ci if (!info.verdict) { 20268c2ecf20Sopenharmony_ci ret = -EIO; 20278c2ecf20Sopenharmony_ci return ret; 20288c2ecf20Sopenharmony_ci } 20298c2ecf20Sopenharmony_ci 20308c2ecf20Sopenharmony_ci *val = temperature_from_RAM(sl, info.rom); 20318c2ecf20Sopenharmony_ci ret = 0; 20328c2ecf20Sopenharmony_ci break; 20338c2ecf20Sopenharmony_ci default: 20348c2ecf20Sopenharmony_ci ret = -EOPNOTSUPP; 20358c2ecf20Sopenharmony_ci break; 20368c2ecf20Sopenharmony_ci } 20378c2ecf20Sopenharmony_ci 20388c2ecf20Sopenharmony_ci return ret; 20398c2ecf20Sopenharmony_ci} 20408c2ecf20Sopenharmony_ci#endif 20418c2ecf20Sopenharmony_ci 20428c2ecf20Sopenharmony_ci#define W1_42_CHAIN 0x99 20438c2ecf20Sopenharmony_ci#define W1_42_CHAIN_OFF 0x3C 20448c2ecf20Sopenharmony_ci#define W1_42_CHAIN_OFF_INV 0xC3 20458c2ecf20Sopenharmony_ci#define W1_42_CHAIN_ON 0x5A 20468c2ecf20Sopenharmony_ci#define W1_42_CHAIN_ON_INV 0xA5 20478c2ecf20Sopenharmony_ci#define W1_42_CHAIN_DONE 0x96 20488c2ecf20Sopenharmony_ci#define W1_42_CHAIN_DONE_INV 0x69 20498c2ecf20Sopenharmony_ci#define W1_42_COND_READ 0x0F 20508c2ecf20Sopenharmony_ci#define W1_42_SUCCESS_CONFIRM_BYTE 0xAA 20518c2ecf20Sopenharmony_ci#define W1_42_FINISHED_BYTE 0xFF 20528c2ecf20Sopenharmony_cistatic ssize_t w1_seq_show(struct device *device, 20538c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 20548c2ecf20Sopenharmony_ci{ 20558c2ecf20Sopenharmony_ci struct w1_slave *sl = dev_to_w1_slave(device); 20568c2ecf20Sopenharmony_ci ssize_t c = PAGE_SIZE; 20578c2ecf20Sopenharmony_ci int rv; 20588c2ecf20Sopenharmony_ci int i; 20598c2ecf20Sopenharmony_ci u8 ack; 20608c2ecf20Sopenharmony_ci u64 rn; 20618c2ecf20Sopenharmony_ci struct w1_reg_num *reg_num; 20628c2ecf20Sopenharmony_ci int seq = 0; 20638c2ecf20Sopenharmony_ci 20648c2ecf20Sopenharmony_ci mutex_lock(&sl->master->bus_mutex); 20658c2ecf20Sopenharmony_ci /* Place all devices in CHAIN state */ 20668c2ecf20Sopenharmony_ci if (w1_reset_bus(sl->master)) 20678c2ecf20Sopenharmony_ci goto error; 20688c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_SKIP_ROM); 20698c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_42_CHAIN); 20708c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_42_CHAIN_ON); 20718c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_42_CHAIN_ON_INV); 20728c2ecf20Sopenharmony_ci msleep(sl->master->pullup_duration); 20738c2ecf20Sopenharmony_ci 20748c2ecf20Sopenharmony_ci /* check for acknowledgment */ 20758c2ecf20Sopenharmony_ci ack = w1_read_8(sl->master); 20768c2ecf20Sopenharmony_ci if (ack != W1_42_SUCCESS_CONFIRM_BYTE) 20778c2ecf20Sopenharmony_ci goto error; 20788c2ecf20Sopenharmony_ci 20798c2ecf20Sopenharmony_ci /* In case the bus fails to send 0xFF, limit */ 20808c2ecf20Sopenharmony_ci for (i = 0; i <= 64; i++) { 20818c2ecf20Sopenharmony_ci if (w1_reset_bus(sl->master)) 20828c2ecf20Sopenharmony_ci goto error; 20838c2ecf20Sopenharmony_ci 20848c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_42_COND_READ); 20858c2ecf20Sopenharmony_ci rv = w1_read_block(sl->master, (u8 *)&rn, 8); 20868c2ecf20Sopenharmony_ci reg_num = (struct w1_reg_num *) &rn; 20878c2ecf20Sopenharmony_ci if (reg_num->family == W1_42_FINISHED_BYTE) 20888c2ecf20Sopenharmony_ci break; 20898c2ecf20Sopenharmony_ci if (sl->reg_num.id == reg_num->id) 20908c2ecf20Sopenharmony_ci seq = i; 20918c2ecf20Sopenharmony_ci 20928c2ecf20Sopenharmony_ci if (w1_reset_bus(sl->master)) 20938c2ecf20Sopenharmony_ci goto error; 20948c2ecf20Sopenharmony_ci 20958c2ecf20Sopenharmony_ci /* Put the device into chain DONE state */ 20968c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_MATCH_ROM); 20978c2ecf20Sopenharmony_ci w1_write_block(sl->master, (u8 *)&rn, 8); 20988c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_42_CHAIN); 20998c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_42_CHAIN_DONE); 21008c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_42_CHAIN_DONE_INV); 21018c2ecf20Sopenharmony_ci 21028c2ecf20Sopenharmony_ci /* check for acknowledgment */ 21038c2ecf20Sopenharmony_ci ack = w1_read_8(sl->master); 21048c2ecf20Sopenharmony_ci if (ack != W1_42_SUCCESS_CONFIRM_BYTE) 21058c2ecf20Sopenharmony_ci goto error; 21068c2ecf20Sopenharmony_ci } 21078c2ecf20Sopenharmony_ci 21088c2ecf20Sopenharmony_ci /* Exit from CHAIN state */ 21098c2ecf20Sopenharmony_ci if (w1_reset_bus(sl->master)) 21108c2ecf20Sopenharmony_ci goto error; 21118c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_SKIP_ROM); 21128c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_42_CHAIN); 21138c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_42_CHAIN_OFF); 21148c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_42_CHAIN_OFF_INV); 21158c2ecf20Sopenharmony_ci 21168c2ecf20Sopenharmony_ci /* check for acknowledgment */ 21178c2ecf20Sopenharmony_ci ack = w1_read_8(sl->master); 21188c2ecf20Sopenharmony_ci if (ack != W1_42_SUCCESS_CONFIRM_BYTE) 21198c2ecf20Sopenharmony_ci goto error; 21208c2ecf20Sopenharmony_ci mutex_unlock(&sl->master->bus_mutex); 21218c2ecf20Sopenharmony_ci 21228c2ecf20Sopenharmony_ci c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq); 21238c2ecf20Sopenharmony_ci return PAGE_SIZE - c; 21248c2ecf20Sopenharmony_cierror: 21258c2ecf20Sopenharmony_ci mutex_unlock(&sl->master->bus_mutex); 21268c2ecf20Sopenharmony_ci return -EIO; 21278c2ecf20Sopenharmony_ci} 21288c2ecf20Sopenharmony_ci 21298c2ecf20Sopenharmony_cistatic int __init w1_therm_init(void) 21308c2ecf20Sopenharmony_ci{ 21318c2ecf20Sopenharmony_ci int err, i; 21328c2ecf20Sopenharmony_ci 21338c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) { 21348c2ecf20Sopenharmony_ci err = w1_register_family(w1_therm_families[i].f); 21358c2ecf20Sopenharmony_ci if (err) 21368c2ecf20Sopenharmony_ci w1_therm_families[i].broken = 1; 21378c2ecf20Sopenharmony_ci } 21388c2ecf20Sopenharmony_ci 21398c2ecf20Sopenharmony_ci return 0; 21408c2ecf20Sopenharmony_ci} 21418c2ecf20Sopenharmony_ci 21428c2ecf20Sopenharmony_cistatic void __exit w1_therm_fini(void) 21438c2ecf20Sopenharmony_ci{ 21448c2ecf20Sopenharmony_ci int i; 21458c2ecf20Sopenharmony_ci 21468c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) 21478c2ecf20Sopenharmony_ci if (!w1_therm_families[i].broken) 21488c2ecf20Sopenharmony_ci w1_unregister_family(w1_therm_families[i].f); 21498c2ecf20Sopenharmony_ci} 21508c2ecf20Sopenharmony_ci 21518c2ecf20Sopenharmony_cimodule_init(w1_therm_init); 21528c2ecf20Sopenharmony_cimodule_exit(w1_therm_fini); 21538c2ecf20Sopenharmony_ci 21548c2ecf20Sopenharmony_ciMODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); 21558c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family."); 21568c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 21578c2ecf20Sopenharmony_ciMODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20)); 21588c2ecf20Sopenharmony_ciMODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822)); 21598c2ecf20Sopenharmony_ciMODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20)); 21608c2ecf20Sopenharmony_ciMODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825)); 21618c2ecf20Sopenharmony_ciMODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00)); 2162