1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * lm75.h - Part of lm_sensors, Linux kernel modules for hardware monitoring 4*4882a593Smuzhiyun * Copyright (c) 2003 Mark M. Hoffman <mhoffman@lightlink.com> 5*4882a593Smuzhiyun */ 6*4882a593Smuzhiyun 7*4882a593Smuzhiyun /* 8*4882a593Smuzhiyun * This file contains common code for encoding/decoding LM75 type 9*4882a593Smuzhiyun * temperature readings, which are emulated by many of the chips 10*4882a593Smuzhiyun * we support. As the user is unlikely to load more than one driver 11*4882a593Smuzhiyun * which contains this code, we don't worry about the wasted space. 12*4882a593Smuzhiyun */ 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun #include <linux/kernel.h> 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun /* straight from the datasheet */ 17*4882a593Smuzhiyun #define LM75_TEMP_MIN (-55000) 18*4882a593Smuzhiyun #define LM75_TEMP_MAX 125000 19*4882a593Smuzhiyun #define LM75_SHUTDOWN 0x01 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun /* 22*4882a593Smuzhiyun * TEMP: 0.001C/bit (-55C to +125C) 23*4882a593Smuzhiyun * REG: (0.5C/bit, two's complement) << 7 24*4882a593Smuzhiyun */ LM75_TEMP_TO_REG(long temp)25*4882a593Smuzhiyunstatic inline u16 LM75_TEMP_TO_REG(long temp) 26*4882a593Smuzhiyun { 27*4882a593Smuzhiyun int ntemp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX); 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun ntemp += (ntemp < 0 ? -250 : 250); 30*4882a593Smuzhiyun return (u16)((ntemp / 500) << 7); 31*4882a593Smuzhiyun } 32*4882a593Smuzhiyun LM75_TEMP_FROM_REG(u16 reg)33*4882a593Smuzhiyunstatic inline int LM75_TEMP_FROM_REG(u16 reg) 34*4882a593Smuzhiyun { 35*4882a593Smuzhiyun /* 36*4882a593Smuzhiyun * use integer division instead of equivalent right shift to 37*4882a593Smuzhiyun * guarantee arithmetic shift and preserve the sign 38*4882a593Smuzhiyun */ 39*4882a593Smuzhiyun return ((s16)reg / 128) * 500; 40*4882a593Smuzhiyun } 41