blob: b803ada5e3c97a09969d13d6733fc4f33e211752 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Michal Orzel8cbf2172020-04-30 16:05:34 +02003 * lm75.h - Part of lm_sensors, Linux kernel modules for hardware monitoring
4 * Copyright (c) 2003 Mark M. Hoffman <mhoffman@lightlink.com>
5 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
7/*
Michal Orzel8cbf2172020-04-30 16:05:34 +02008 * This file contains common code for encoding/decoding LM75 type
9 * temperature readings, which are emulated by many of the chips
10 * we support. As the user is unlikely to load more than one driver
11 * which contains this code, we don't worry about the wasted space.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Christophe JAILLET39397ba2022-06-03 22:51:46 +020014#include <linux/minmax.h>
15#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/* straight from the datasheet */
18#define LM75_TEMP_MIN (-55000)
19#define LM75_TEMP_MAX 125000
Shubhrajyoti Datta99145182010-08-14 21:08:50 +020020#define LM75_SHUTDOWN 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Michal Orzel8cbf2172020-04-30 16:05:34 +020022/*
23 * TEMP: 0.001C/bit (-55C to +125C)
24 * REG: (0.5C/bit, two's complement) << 7
25 */
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +020026static inline u16 LM75_TEMP_TO_REG(long temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Guenter Roeck2a844c12013-01-09 08:09:34 -080028 int ntemp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
Michal Orzel8cbf2172020-04-30 16:05:34 +020029
Frans Meulenbroeksa01a6842012-01-04 23:16:39 +010030 ntemp += (ntemp < 0 ? -250 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return (u16)((ntemp / 500) << 7);
32}
33
34static inline int LM75_TEMP_FROM_REG(u16 reg)
35{
Michal Orzel8cbf2172020-04-30 16:05:34 +020036 /*
37 * use integer division instead of equivalent right shift to
38 * guarantee arithmetic shift and preserve the sign
39 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return ((s16)reg / 128) * 500;
41}