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