// SPDX-License-Identifier: GPL-2.0-or-later
/*
* w1_therm.c
*
* Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
*/
#include <asm/types.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/hwmon.h>
#include <linux/string.h>
#include <linux/w1.h>
#define W1_THERM_DS18S20 0x10
#define W1_THERM_DS1822 0x22
#define W1_THERM_DS18B20 0x28
#define W1_THERM_DS1825 0x3B
#define W1_THERM_DS28EA00 0x42
/*
* Allow the strong pullup to be disabled, but default to enabled.
* If it was disabled a parasite powered device might not get the require
* current to do a temperature conversion. If it is enabled parasite powered
* devices have a better chance of getting the current required.
* In case the parasite power-detection is not working (seems to be the case
* for some DS18S20) the strong pullup can also be forced, regardless of the
* power state of the devices.
*
* Summary of options:
* - strong_pullup = 0 Disable strong pullup completely
* - strong_pullup = 1 Enable automatic strong pullup detection
* - strong_pullup = 2 Force strong pullup
*/
static int w1_strong_pullup = 1;
module_param_named(strong_pullup, w1_strong_pullup, int, 0);
/* This command should be in public header w1.h but is not */
#define W1_RECALL_EEPROM 0xB8
/* Nb of try for an operation */
#define W1_THERM_MAX_TRY 5
/* ms delay to retry bus mutex */
#define W1_THERM_RETRY_DELAY 20
/* delay in ms to write in EEPROM */
#define W1_THERM_EEPROM_WRITE_DELAY 10
#define EEPROM_CMD_WRITE "save" /* cmd for write eeprom sysfs */
#define EEPROM_CMD_READ "restore" /* cmd for read eeprom sysfs */
/* Helpers Macros */
/*
* return a pointer on the slave w1_therm_family_converter struct:
* always test family data existence before using this macro
*/
#define SLAVE_SPECIFIC_FUNC(sl) \
(((struct w1_therm_family_data *)(sl->family_data))->specific_functions)
/*
* return the power mode of the sl slave : 1-ext, 0-parasite, <0 unknown
* always test family data existence before using this macro
*/
#define SLAVE_POWERMODE(sl) \
(((struct w1_therm_family_data *)(sl->family_data))->external_powered)
/*
* return the resolution in bit of the sl slave : <0 unknown
* always test family data existence before using this macro
*/
#define SLAVE_RESOLUTION(sl) \
(((struct w1_therm_family_data *)(sl->family_data))->resolution)
/* return the address of the refcnt in the family data */
#define THERM_REFCNT(family_data) \
(&((struct w1_therm_family_data *)family_data)->refcnt)
/* Structs definition */
/**
* struct w1_therm_family_converter - bind device specific functions
* @broken: flag for non-registred families
* @reserved: not used here
* @f: pointer to the device binding structure
* @convert: pointer to the device conversion function
* @set_resolution: pointer to the device set_resolution function
* @get_resolution: pointer to the device get_resolution function
*/
struct w1_therm_family_converter {
u8 broken;
u16 reserved;
struct w1_family *f;
int (*convert)(u8 rom[9]);
int (*set_resolution)(struct w1_slave *sl, int val);
int (*get_resolution)(struct w1_slave *sl);
};
/**
* struct w1_therm_family_data - device data
* @rom: ROM device id (64bit Lasered ROM code + 1 CRC byte)
* @refcnt: ref count
* @external_powered: 1 device powered externally,
* 0 device parasite powered,
* -x error or undefined
* @resolution: current device resolution
* @specific_functions: pointer to struct of device specific function
*/
struct w1_therm_family_data {
uint8_t rom[9];
atomic_t refcnt;
int external_powered;
int resolution;
struct w1_therm_family_converter *specific_functions;
};
/**
* struct therm_info - store temperature reading
* @rom: read device data (8 data bytes + 1 CRC byte)
* @crc: computed crc from rom
* @verdict: 1 crc checked, 0 crc not matching
*/
struct therm_info {
u8 rom[9];
u8 crc;
u8 verdict;
};
/* Hardware Functions declaration */
/**
* reset_select_slave() - reset and select a slave
* @sl: the slave to select
*
* Resets the bus and select the slave by sending a ROM MATCH cmd
* w1_reset_select_slave() from w1_io.c could not be used here because
* it sent a SKIP ROM command if only one device is on the line.
* At the beginning of the such process, sl->master->slave_count