// SPDX-License-Identifier: GPL-2.0-or-later
/*
* lm75.c - Part of lm_sensors, Linux kernel modules for hardware
* monitoring
* Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/err.h>
#include <linux/of.h>
#include <linux/regmap.h>
#include <linux/util_macros.h>
#include <linux/regulator/consumer.h>
#include "lm75.h"
/*
* This driver handles the LM75 and compatible digital temperature sensors.
*/
enum lm75_type { /* keep sorted in alphabetical order */
adt75,
as6200,
at30ts74,
ds1775,
ds75,
ds7505,
g751,
lm75,
lm75a,
lm75b,
max6625,
max6626,
max31725,
mcp980x,
pct2075,
stds75,
stlm75,
tcn75,
tmp100,
tmp101,
tmp105,
tmp112,
tmp175,
tmp275,
tmp75,
tmp75b,
tmp75c,
tmp1075,
};
/**
* struct lm75_params - lm75 configuration parameters.
* @config_reg_16bits: Configure register size is 2 bytes.
* @set_mask: Bits to set in configuration register when configuring
* the chip.
* @clr_mask: Bits to clear in configuration register when configuring
* the chip.
* @default_resolution: Default number of bits to represent the temperature
* value.
* @resolution_limits: Limit register resolution. Optional. Should be set if
* the resolution of limit registers does not match the
* resolution of the temperature register.
* @resolutions: List of resolutions associated with sample times.
* Optional. Should be set if num_sample_times is larger
* than 1, and if the resolution changes with sample times.
* If set, number of entries must match num_sample_times.
* @default_sample_time:Sample time to be set by default.
* @num_sample_times: Number of possible sample times to be set. Optional.
* Should be set if the number of sample times is larger
* than one.
* @sample_times: All the possible sample times to be set. Mandatory if
* num_sample_times is larger than 1. If set, number of
* entries must match num_sample_times.
* @alarm: Alarm bit is supported.
*/
struct lm75_params {
bool config_reg_16bits;
u16 set_mask;
u16 clr_mask;
u8 default_resolution;
u8 resolution_limits;
const u8 *resolutions;
unsigned int default_sample_time;
u8 num_sample_times;
const unsigned int *sample_times;
bool alarm;
};
/* Addresses scanned */
static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
/* The LM75 registers */
#define LM75_REG_TEMP 0x00
#define LM75_REG_CONF 0x01
#define LM75_REG_HYST 0x02
#define LM75_REG_MAX 0x03
#define PCT2075_REG_IDLE 0x04
/* Each client has this additional data */
struct lm75_data {
struct i2c_client *client;
struct regmap *regmap;
struct regulator *vs;
u16 orig_conf;
u16 current_conf;
u8 resolution; /* In bits, 9 to 16 */
unsigned int sample_time; /* In ms */
enum lm75_type kind;
const struct lm75_params *params;
};
/*-----------------------------------------------------------------------*/
static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 };
#define LM75_SAMPLE_CLEAR_MASK (3 << 5)
/* The structure below stores the configuration values of the supported devices.
* In case of being supported multiple configurations, the default one must
* always be the first element of the array
*/
static const struct lm75_params device_params[] = {
[adt75] = {
.clr_mask = 1 << 5, /* not one-shot mode */
.default_resolution = 12,
.default_sample_time = MSEC_PER_SEC / 10,
},
[as6200] = {
.config_reg_16bits = true,
.set_mask = 0x94C0, /* 8 sample/s, 4 CF, positive polarity */
.default_resolution = 12,
.default_sample_time = 125,
.num_sample_times = 4,
.sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
.alarm = true,
},
[at30ts74] = {
.set_mask = 3 << 5, /* 12-bit mode*/
.default_resolution = 12,
.default_sample_time = 200,
.num_sample_times = 4,
.sample_times = (unsigned int []){ 25, 50, 100, 200 },
.resolutions = (u8 []) {9, 10, 11, 12 },
},
[ds1775] = {
.clr_mask = 3 << 5,
.set_mask = 2 << 5, /* 11-bit mode */
.default_resolution = 11,
.default_sample_time = 500,
.num_sample_times = 4,
.sample_times = (unsigned int []){ 125, 250, 500, 1000 },
.resolutions = (u8 []) {9, 10, 11, 12 },
},
[ds75] = {
.clr_mask = 3 << 5,
.set_mask = 2 << 5, /* 11-bit mode */
.default_resolution = 11,
.default_sample_time = 600,
.num_sample_times = 4,
.sample_times = (unsigned int []){ 150, 300, 600, 1200 },
.resolutions = (u8 []) {9, 10, 11, 12 },
},
[stds75] = {
.clr_mask = 3 << 5,
.set_mask = 2 << 5, /* 11-bit mode */
.default_resolution = 11,
.default_sample_time = 600,
.num_sample_times = 4,
.sample_times = (unsigned int []){ 150, 300, 600, 1200 },
.resolutions = (u8 []) {9, 10, 11, 12 },
},
[stlm75] = {
.default_resolution = 9,
.default_sample_time = MSEC_PER_SEC / 6,
},
[ds7505] = {
.set_mask = 3 << 5, /* 12-bit mode*/
.default_resolution = 12,
.default_sample_time = 200,
.num_sample_times = 4,
.sample_times = (unsigned int []){ 25, 50, 100