Changes in 4.9.285 ocfs2: drop acl cache for directories too usb: gadget: r8a66597: fix a loop in set_feature() usb: musb: tusb6010: uninitialized data in tusb_fifo_write_unaligned() cifs: fix incorrect check for null pointer in header_assemble xen/x86: fix PV trap handling on secondary processors usb-storage: Add quirk for ScanLogic SL11R-IDE older than 2.6c USB: serial: cp210x: add ID for GW Instek GDM-834x Digital Multimeter staging: greybus: uart: fix tty use after free USB: serial: mos7840: remove duplicated 0xac24 device ID USB: serial: option: add Telit LN920 compositions USB: serial: option: remove duplicate USB device ID USB: serial: option: add device id for Foxconn T99W265 mcb: fix error handling in mcb_alloc_bus() serial: mvebu-uart: fix driver's tx_empty callback net: hso: fix muxed tty registration net/mlx4_en: Don't allow aRFS for encapsulated packets scsi: iscsi: Adjust iface sysfs attr detection blktrace: Fix uaf in blk_trace access after removing by sysfs net: stmmac: allow CSR clock of 300MHz m68k: Double cast io functions to unsigned long compiler.h: Introduce absolute_pointer macro net: i825xx: Use absolute_pointer for memcpy from fixed memory location sparc: avoid stringop-overread errors qnx4: avoid stringop-overread errors parisc: Use absolute_pointer() to define PAGE0 arm64: Mark __stack_chk_guard as __ro_after_init alpha: Declare virt_to_phys and virt_to_bus parameter as pointer to volatile net: 6pack: Fix tx timeout and slot time spi: Fix tegra20 build with CONFIG_PM=n arm64: dts: marvell: armada-37xx: Extend PCIe MEM space qnx4: work around gcc false positive warning bug tty: Fix out-of-bound vmalloc access in imageblit cpufreq: schedutil: Use kobject release() method to free sugov_tunables cpufreq: schedutil: Destroy mutex before kobject_put() frees the memory mac80211: fix use-after-free in CCMP/GCMP RX ipvs: check that ip_vs_conn_tab_bits is between 8 and 20 mac80211: limit injected vht mcs/nss in ieee80211_parse_tx_radiotap hwmon: (tmp421) fix rounding for negative values e100: fix length calculation in e100_get_regs_len e100: fix buffer overrun in e100_get_regs ipack: ipoctal: fix stack information leak ipack: ipoctal: fix tty registration race ipack: ipoctal: fix tty-registration error handling ipack: ipoctal: fix missing allocation-failure check ipack: ipoctal: fix module reference leak ext4: fix potential infinite loop in ext4_dx_readdir() net: udp: annotate data race around udp_sk(sk)->corkflag EDAC/synopsys: Fix wrong value type assignment for edac_mode ARM: 9077/1: PLT: Move struct plt_entries definition to header ARM: 9078/1: Add warn suppress parameter to arm_gen_branch_link() ARM: 9079/1: ftrace: Add MODULE_PLTS support ARM: 9098/1: ftrace: MODULE_PLT: Fix build problem without DYNAMIC_FTRACE arm64: Extend workaround for erratum 1024718 to all versions of Cortex-A55 HID: betop: fix slab-out-of-bounds Write in betop_probe netfilter: ipset: Fix oversized kvmalloc() calls HID: usbhid: free raw_report buffers in usbhid_stop cred: allow get_cred() and put_cred() to be given NULL. Linux 4.9.285 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com> Change-Id: I55fa39c89ba10e7092db6001c6feee0d792a401f
308 lines
7.3 KiB
C
308 lines
7.3 KiB
C
/* tmp421.c
|
|
*
|
|
* Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
|
|
* Preliminary support by:
|
|
* Melvin Rook, Raymond Ng
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*/
|
|
|
|
/*
|
|
* Driver for the Texas Instruments TMP421 SMBus temperature sensor IC.
|
|
* Supported models: TMP421, TMP422, TMP423, TMP441, TMP442
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/init.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/jiffies.h>
|
|
#include <linux/i2c.h>
|
|
#include <linux/hwmon.h>
|
|
#include <linux/hwmon-sysfs.h>
|
|
#include <linux/err.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/sysfs.h>
|
|
|
|
/* Addresses to scan */
|
|
static const unsigned short normal_i2c[] = { 0x2a, 0x4c, 0x4d, 0x4e, 0x4f,
|
|
I2C_CLIENT_END };
|
|
|
|
enum chips { tmp421, tmp422, tmp423, tmp441, tmp442 };
|
|
|
|
/* The TMP421 registers */
|
|
#define TMP421_STATUS_REG 0x08
|
|
#define TMP421_CONFIG_REG_1 0x09
|
|
#define TMP421_CONVERSION_RATE_REG 0x0B
|
|
#define TMP421_MANUFACTURER_ID_REG 0xFE
|
|
#define TMP421_DEVICE_ID_REG 0xFF
|
|
|
|
static const u8 TMP421_TEMP_MSB[4] = { 0x00, 0x01, 0x02, 0x03 };
|
|
static const u8 TMP421_TEMP_LSB[4] = { 0x10, 0x11, 0x12, 0x13 };
|
|
|
|
/* Flags */
|
|
#define TMP421_CONFIG_SHUTDOWN 0x40
|
|
#define TMP421_CONFIG_RANGE 0x04
|
|
|
|
/* Manufacturer / Device ID's */
|
|
#define TMP421_MANUFACTURER_ID 0x55
|
|
#define TMP421_DEVICE_ID 0x21
|
|
#define TMP422_DEVICE_ID 0x22
|
|
#define TMP423_DEVICE_ID 0x23
|
|
#define TMP441_DEVICE_ID 0x41
|
|
#define TMP442_DEVICE_ID 0x42
|
|
|
|
static const struct i2c_device_id tmp421_id[] = {
|
|
{ "tmp421", 2 },
|
|
{ "tmp422", 3 },
|
|
{ "tmp423", 4 },
|
|
{ "tmp441", 2 },
|
|
{ "tmp442", 3 },
|
|
{ }
|
|
};
|
|
MODULE_DEVICE_TABLE(i2c, tmp421_id);
|
|
|
|
struct tmp421_data {
|
|
struct i2c_client *client;
|
|
struct mutex update_lock;
|
|
u32 temp_config[5];
|
|
struct hwmon_channel_info temp_info;
|
|
const struct hwmon_channel_info *info[2];
|
|
struct hwmon_chip_info chip;
|
|
char valid;
|
|
unsigned long last_updated;
|
|
int channels;
|
|
u8 config;
|
|
s16 temp[4];
|
|
};
|
|
|
|
static int temp_from_raw(u16 reg, bool extended)
|
|
{
|
|
/* Mask out status bits */
|
|
int temp = reg & ~0xf;
|
|
|
|
if (extended)
|
|
temp = temp - 64 * 256;
|
|
else
|
|
temp = (s16)temp;
|
|
|
|
return DIV_ROUND_CLOSEST(temp * 1000, 256);
|
|
}
|
|
|
|
static struct tmp421_data *tmp421_update_device(struct device *dev)
|
|
{
|
|
struct tmp421_data *data = dev_get_drvdata(dev);
|
|
struct i2c_client *client = data->client;
|
|
int i;
|
|
|
|
mutex_lock(&data->update_lock);
|
|
|
|
if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
|
|
data->config = i2c_smbus_read_byte_data(client,
|
|
TMP421_CONFIG_REG_1);
|
|
|
|
for (i = 0; i < data->channels; i++) {
|
|
data->temp[i] = i2c_smbus_read_byte_data(client,
|
|
TMP421_TEMP_MSB[i]) << 8;
|
|
data->temp[i] |= i2c_smbus_read_byte_data(client,
|
|
TMP421_TEMP_LSB[i]);
|
|
}
|
|
data->last_updated = jiffies;
|
|
data->valid = 1;
|
|
}
|
|
|
|
mutex_unlock(&data->update_lock);
|
|
|
|
return data;
|
|
}
|
|
|
|
static int tmp421_read(struct device *dev, enum hwmon_sensor_types type,
|
|
u32 attr, int channel, long *val)
|
|
{
|
|
struct tmp421_data *tmp421 = tmp421_update_device(dev);
|
|
|
|
switch (attr) {
|
|
case hwmon_temp_input:
|
|
*val = temp_from_raw(tmp421->temp[channel],
|
|
tmp421->config & TMP421_CONFIG_RANGE);
|
|
return 0;
|
|
case hwmon_temp_fault:
|
|
/*
|
|
* The OPEN bit signals a fault. This is bit 0 of the temperature
|
|
* register (low byte).
|
|
*/
|
|
*val = tmp421->temp[channel] & 0x01;
|
|
return 0;
|
|
default:
|
|
return -EOPNOTSUPP;
|
|
}
|
|
|
|
}
|
|
|
|
static umode_t tmp421_is_visible(const void *data, enum hwmon_sensor_types type,
|
|
u32 attr, int channel)
|
|
{
|
|
switch (attr) {
|
|
case hwmon_temp_fault:
|
|
if (channel == 0)
|
|
return 0;
|
|
return S_IRUGO;
|
|
case hwmon_temp_input:
|
|
return S_IRUGO;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
static int tmp421_init_client(struct i2c_client *client)
|
|
{
|
|
int config, config_orig;
|
|
|
|
/* Set the conversion rate to 2 Hz */
|
|
i2c_smbus_write_byte_data(client, TMP421_CONVERSION_RATE_REG, 0x05);
|
|
|
|
/* Start conversions (disable shutdown if necessary) */
|
|
config = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
|
|
if (config < 0) {
|
|
dev_err(&client->dev,
|
|
"Could not read configuration register (%d)\n", config);
|
|
return config;
|
|
}
|
|
|
|
config_orig = config;
|
|
config &= ~TMP421_CONFIG_SHUTDOWN;
|
|
|
|
if (config != config_orig) {
|
|
dev_info(&client->dev, "Enable monitoring chip\n");
|
|
i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_1, config);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int tmp421_detect(struct i2c_client *client,
|
|
struct i2c_board_info *info)
|
|
{
|
|
enum chips kind;
|
|
struct i2c_adapter *adapter = client->adapter;
|
|
const char * const names[] = { "TMP421", "TMP422", "TMP423",
|
|
"TMP441", "TMP442" };
|
|
int addr = client->addr;
|
|
u8 reg;
|
|
|
|
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
|
|
return -ENODEV;
|
|
|
|
reg = i2c_smbus_read_byte_data(client, TMP421_MANUFACTURER_ID_REG);
|
|
if (reg != TMP421_MANUFACTURER_ID)
|
|
return -ENODEV;
|
|
|
|
reg = i2c_smbus_read_byte_data(client, TMP421_CONVERSION_RATE_REG);
|
|
if (reg & 0xf8)
|
|
return -ENODEV;
|
|
|
|
reg = i2c_smbus_read_byte_data(client, TMP421_STATUS_REG);
|
|
if (reg & 0x7f)
|
|
return -ENODEV;
|
|
|
|
reg = i2c_smbus_read_byte_data(client, TMP421_DEVICE_ID_REG);
|
|
switch (reg) {
|
|
case TMP421_DEVICE_ID:
|
|
kind = tmp421;
|
|
break;
|
|
case TMP422_DEVICE_ID:
|
|
if (addr == 0x2a)
|
|
return -ENODEV;
|
|
kind = tmp422;
|
|
break;
|
|
case TMP423_DEVICE_ID:
|
|
if (addr != 0x4c && addr != 0x4d)
|
|
return -ENODEV;
|
|
kind = tmp423;
|
|
break;
|
|
case TMP441_DEVICE_ID:
|
|
kind = tmp441;
|
|
break;
|
|
case TMP442_DEVICE_ID:
|
|
if (addr != 0x4c && addr != 0x4d)
|
|
return -ENODEV;
|
|
kind = tmp442;
|
|
break;
|
|
default:
|
|
return -ENODEV;
|
|
}
|
|
|
|
strlcpy(info->type, tmp421_id[kind].name, I2C_NAME_SIZE);
|
|
dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n",
|
|
names[kind], client->addr);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct hwmon_ops tmp421_ops = {
|
|
.is_visible = tmp421_is_visible,
|
|
.read = tmp421_read,
|
|
};
|
|
|
|
static int tmp421_probe(struct i2c_client *client,
|
|
const struct i2c_device_id *id)
|
|
{
|
|
struct device *dev = &client->dev;
|
|
struct device *hwmon_dev;
|
|
struct tmp421_data *data;
|
|
int i, err;
|
|
|
|
data = devm_kzalloc(dev, sizeof(struct tmp421_data), GFP_KERNEL);
|
|
if (!data)
|
|
return -ENOMEM;
|
|
|
|
mutex_init(&data->update_lock);
|
|
data->channels = id->driver_data;
|
|
data->client = client;
|
|
|
|
err = tmp421_init_client(client);
|
|
if (err)
|
|
return err;
|
|
|
|
for (i = 0; i < data->channels; i++)
|
|
data->temp_config[i] = HWMON_T_INPUT | HWMON_T_FAULT;
|
|
|
|
data->chip.ops = &tmp421_ops;
|
|
data->chip.info = data->info;
|
|
|
|
data->info[0] = &data->temp_info;
|
|
|
|
data->temp_info.type = hwmon_temp;
|
|
data->temp_info.config = data->temp_config;
|
|
|
|
hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
|
|
data,
|
|
&data->chip,
|
|
NULL);
|
|
return PTR_ERR_OR_ZERO(hwmon_dev);
|
|
}
|
|
|
|
static struct i2c_driver tmp421_driver = {
|
|
.class = I2C_CLASS_HWMON,
|
|
.driver = {
|
|
.name = "tmp421",
|
|
},
|
|
.probe = tmp421_probe,
|
|
.id_table = tmp421_id,
|
|
.detect = tmp421_detect,
|
|
.address_list = normal_i2c,
|
|
};
|
|
|
|
module_i2c_driver(tmp421_driver);
|
|
|
|
MODULE_AUTHOR("Andre Prendel <andre.prendel@gmx.de>");
|
|
MODULE_DESCRIPTION("Texas Instruments TMP421/422/423/441/442 temperature sensor driver");
|
|
MODULE_LICENSE("GPL");
|