174ba9207SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 28d5d45fbSJean Delvare /* 38cbf2172SMichal Orzel * lm75.h - Part of lm_sensors, Linux kernel modules for hardware monitoring 48cbf2172SMichal Orzel * Copyright (c) 2003 Mark M. Hoffman <mhoffman@lightlink.com> 58d5d45fbSJean Delvare */ 68d5d45fbSJean Delvare 78d5d45fbSJean Delvare /* 88cbf2172SMichal Orzel * This file contains common code for encoding/decoding LM75 type 98cbf2172SMichal Orzel * temperature readings, which are emulated by many of the chips 108cbf2172SMichal Orzel * we support. As the user is unlikely to load more than one driver 118cbf2172SMichal Orzel * which contains this code, we don't worry about the wasted space. 128d5d45fbSJean Delvare */ 138d5d45fbSJean Delvare 14*39397ba8SChristophe JAILLET #include <linux/minmax.h> 15*39397ba8SChristophe JAILLET #include <linux/types.h> 168d5d45fbSJean Delvare 178d5d45fbSJean Delvare /* straight from the datasheet */ 188d5d45fbSJean Delvare #define LM75_TEMP_MIN (-55000) 198d5d45fbSJean Delvare #define LM75_TEMP_MAX 125000 209914518eSShubhrajyoti Datta #define LM75_SHUTDOWN 0x01 218d5d45fbSJean Delvare 228cbf2172SMichal Orzel /* 238cbf2172SMichal Orzel * TEMP: 0.001C/bit (-55C to +125C) 248cbf2172SMichal Orzel * REG: (0.5C/bit, two's complement) << 7 258cbf2172SMichal Orzel */ LM75_TEMP_TO_REG(long temp)265bfedac0SChristian Hohnstaedtstatic inline u16 LM75_TEMP_TO_REG(long temp) 278d5d45fbSJean Delvare { 282a844c14SGuenter Roeck int ntemp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX); 298cbf2172SMichal Orzel 308d5d45fbSJean Delvare ntemp += (ntemp < 0 ? -250 : 250); 318d5d45fbSJean Delvare return (u16)((ntemp / 500) << 7); 328d5d45fbSJean Delvare } 338d5d45fbSJean Delvare LM75_TEMP_FROM_REG(u16 reg)348d5d45fbSJean Delvarestatic inline int LM75_TEMP_FROM_REG(u16 reg) 358d5d45fbSJean Delvare { 368cbf2172SMichal Orzel /* 378cbf2172SMichal Orzel * use integer division instead of equivalent right shift to 388cbf2172SMichal Orzel * guarantee arithmetic shift and preserve the sign 398cbf2172SMichal Orzel */ 408d5d45fbSJean Delvare return ((s16)reg / 128) * 500; 418d5d45fbSJean Delvare } 42