hwmon: (lm95245) Use new hwmon registration API
Simplify code and reduce code size by using the new hwmon registration API. Other changes: - Convert to use regmap, and drop local caching. This avoids reading registers unnecessarily, and uses regmap for caching of non-volatile registers. - Add support for temp2_max, temp2_max_alarm, temp2_max_hyst, and temp2_offset. - Order include files alphabetically - Drop FSF address - Check errors from register read and write functions and report to userspace. - Accept negative hysteresis values. While unlikely, a maximum limit _can_ be set to a value smaller than 31 degrees C, which makes negative hysteresis values possible. Signed-off-by: Guenter Roeck <linux@roeck-us.net>
This commit is contained in:
Родитель
3e9046281b
Коммит
c0a4b9ec1b
|
@ -15,22 +15,16 @@
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software
|
|
||||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#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/err.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/hwmon.h>
|
||||||
|
#include <linux/i2c.h>
|
||||||
|
#include <linux/module.h>
|
||||||
#include <linux/mutex.h>
|
#include <linux/mutex.h>
|
||||||
#include <linux/sysfs.h>
|
#include <linux/regmap.h>
|
||||||
|
#include <linux/slab.h>
|
||||||
|
|
||||||
static const unsigned short normal_i2c[] = {
|
static const unsigned short normal_i2c[] = {
|
||||||
0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END };
|
0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END };
|
||||||
|
@ -89,6 +83,7 @@ static const unsigned short normal_i2c[] = {
|
||||||
#define RATE_CR1000 0x02
|
#define RATE_CR1000 0x02
|
||||||
#define RATE_CR2500 0x03
|
#define RATE_CR2500 0x03
|
||||||
|
|
||||||
|
#define STATUS1_ROS 0x10
|
||||||
#define STATUS1_DIODE_FAULT 0x04
|
#define STATUS1_DIODE_FAULT 0x04
|
||||||
#define STATUS1_RTCRIT 0x02
|
#define STATUS1_RTCRIT 0x02
|
||||||
#define STATUS1_LOC 0x01
|
#define STATUS1_LOC 0x01
|
||||||
|
@ -112,14 +107,9 @@ static const u8 lm95245_reg_address[] = {
|
||||||
|
|
||||||
/* Client data (each client gets its own) */
|
/* Client data (each client gets its own) */
|
||||||
struct lm95245_data {
|
struct lm95245_data {
|
||||||
struct i2c_client *client;
|
struct regmap *regmap;
|
||||||
struct mutex update_lock;
|
struct mutex update_lock;
|
||||||
unsigned long last_updated; /* in jiffies */
|
int interval; /* in msecs */
|
||||||
unsigned long interval; /* in msecs */
|
|
||||||
bool valid; /* zero until following fields are valid */
|
|
||||||
/* registers values */
|
|
||||||
u8 regs[ARRAY_SIZE(lm95245_reg_address)];
|
|
||||||
u8 config1, config2;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Conversions */
|
/* Conversions */
|
||||||
|
@ -135,60 +125,36 @@ static int temp_from_reg_signed(u8 val_h, u8 val_l)
|
||||||
return temp_from_reg_unsigned(val_h, val_l);
|
return temp_from_reg_unsigned(val_h, val_l);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct lm95245_data *lm95245_update_device(struct device *dev)
|
static int lm95245_read_conversion_rate(struct lm95245_data *data)
|
||||||
{
|
{
|
||||||
struct lm95245_data *data = dev_get_drvdata(dev);
|
unsigned int rate;
|
||||||
struct i2c_client *client = data->client;
|
int ret;
|
||||||
|
|
||||||
mutex_lock(&data->update_lock);
|
ret = regmap_read(data->regmap, LM95245_REG_RW_CONVERS_RATE, &rate);
|
||||||
|
if (ret < 0)
|
||||||
if (time_after(jiffies, data->last_updated
|
return ret;
|
||||||
+ msecs_to_jiffies(data->interval)) || !data->valid) {
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(lm95245_reg_address); i++)
|
|
||||||
data->regs[i]
|
|
||||||
= i2c_smbus_read_byte_data(client,
|
|
||||||
lm95245_reg_address[i]);
|
|
||||||
data->last_updated = jiffies;
|
|
||||||
data->valid = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
mutex_unlock(&data->update_lock);
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned long lm95245_read_conversion_rate(struct i2c_client *client)
|
|
||||||
{
|
|
||||||
int rate;
|
|
||||||
unsigned long interval;
|
|
||||||
|
|
||||||
rate = i2c_smbus_read_byte_data(client, LM95245_REG_RW_CONVERS_RATE);
|
|
||||||
|
|
||||||
switch (rate) {
|
switch (rate) {
|
||||||
case RATE_CR0063:
|
case RATE_CR0063:
|
||||||
interval = 63;
|
data->interval = 63;
|
||||||
break;
|
break;
|
||||||
case RATE_CR0364:
|
case RATE_CR0364:
|
||||||
interval = 364;
|
data->interval = 364;
|
||||||
break;
|
break;
|
||||||
case RATE_CR1000:
|
case RATE_CR1000:
|
||||||
interval = 1000;
|
data->interval = 1000;
|
||||||
break;
|
break;
|
||||||
case RATE_CR2500:
|
case RATE_CR2500:
|
||||||
default:
|
default:
|
||||||
interval = 2500;
|
data->interval = 2500;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
return interval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned long lm95245_set_conversion_rate(struct i2c_client *client,
|
static int lm95245_set_conversion_rate(struct lm95245_data *data, long interval)
|
||||||
unsigned long interval)
|
|
||||||
{
|
{
|
||||||
int rate;
|
int ret, rate;
|
||||||
|
|
||||||
if (interval <= 63) {
|
if (interval <= 63) {
|
||||||
interval = 63;
|
interval = 63;
|
||||||
|
@ -204,220 +170,288 @@ static unsigned long lm95245_set_conversion_rate(struct i2c_client *client,
|
||||||
rate = RATE_CR2500;
|
rate = RATE_CR2500;
|
||||||
}
|
}
|
||||||
|
|
||||||
i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONVERS_RATE, rate);
|
ret = regmap_write(data->regmap, LM95245_REG_RW_CONVERS_RATE, rate);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
|
||||||
return interval;
|
data->interval = interval;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sysfs stuff */
|
static int lm95245_read_temp(struct device *dev, u32 attr, int channel,
|
||||||
static ssize_t show_input(struct device *dev, struct device_attribute *attr,
|
long *val)
|
||||||
char *buf)
|
|
||||||
{
|
|
||||||
struct lm95245_data *data = lm95245_update_device(dev);
|
|
||||||
int temp;
|
|
||||||
int index = to_sensor_dev_attr(attr)->index;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Index 0 (Local temp) is always signed
|
|
||||||
* Index 2 (Remote temp) has both signed and unsigned data
|
|
||||||
* use signed calculation for remote if signed bit is set
|
|
||||||
*/
|
|
||||||
if (index == 0 || data->regs[index] & 0x80)
|
|
||||||
temp = temp_from_reg_signed(data->regs[index],
|
|
||||||
data->regs[index + 1]);
|
|
||||||
else
|
|
||||||
temp = temp_from_reg_unsigned(data->regs[index + 2],
|
|
||||||
data->regs[index + 3]);
|
|
||||||
|
|
||||||
return snprintf(buf, PAGE_SIZE - 1, "%d\n", temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static ssize_t show_limit(struct device *dev, struct device_attribute *attr,
|
|
||||||
char *buf)
|
|
||||||
{
|
|
||||||
struct lm95245_data *data = lm95245_update_device(dev);
|
|
||||||
int index = to_sensor_dev_attr(attr)->index;
|
|
||||||
|
|
||||||
return snprintf(buf, PAGE_SIZE - 1, "%d\n",
|
|
||||||
data->regs[index] * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
static ssize_t set_limit(struct device *dev, struct device_attribute *attr,
|
|
||||||
const char *buf, size_t count)
|
|
||||||
{
|
{
|
||||||
struct lm95245_data *data = dev_get_drvdata(dev);
|
struct lm95245_data *data = dev_get_drvdata(dev);
|
||||||
int index = to_sensor_dev_attr(attr)->index;
|
struct regmap *regmap = data->regmap;
|
||||||
struct i2c_client *client = data->client;
|
int ret, regl, regh, regvall, regvalh;
|
||||||
unsigned long val;
|
|
||||||
|
|
||||||
if (kstrtoul(buf, 10, &val) < 0)
|
switch (attr) {
|
||||||
return -EINVAL;
|
case hwmon_temp_input:
|
||||||
|
regl = channel ? LM95245_REG_R_REMOTE_TEMPL_S :
|
||||||
val /= 1000;
|
LM95245_REG_R_LOCAL_TEMPL_S;
|
||||||
|
regh = channel ? LM95245_REG_R_REMOTE_TEMPH_S :
|
||||||
val = clamp_val(val, 0, (index == 6 ? 127 : 255));
|
LM95245_REG_R_LOCAL_TEMPH_S;
|
||||||
|
ret = regmap_read(regmap, regl, ®vall);
|
||||||
mutex_lock(&data->update_lock);
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
data->valid = 0;
|
ret = regmap_read(regmap, regh, ®valh);
|
||||||
|
if (ret < 0)
|
||||||
i2c_smbus_write_byte_data(client, lm95245_reg_address[index], val);
|
return ret;
|
||||||
|
/*
|
||||||
mutex_unlock(&data->update_lock);
|
* Local temp is always signed.
|
||||||
|
* Remote temp has both signed and unsigned data.
|
||||||
return count;
|
* Use signed calculation for remote if signed bit is set
|
||||||
|
* or if reported temperature is below signed limit.
|
||||||
|
*/
|
||||||
|
if (!channel || (regvalh & 0x80) || regvalh < 0x7f) {
|
||||||
|
*val = temp_from_reg_signed(regvalh, regvall);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPL_U,
|
||||||
|
®vall);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPH_U,
|
||||||
|
®valh);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
*val = temp_from_reg_unsigned(regvalh, regvall);
|
||||||
|
return 0;
|
||||||
|
case hwmon_temp_max:
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT,
|
||||||
|
®valh);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
*val = regvalh * 1000;
|
||||||
|
return 0;
|
||||||
|
case hwmon_temp_crit:
|
||||||
|
regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
|
||||||
|
LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
|
||||||
|
ret = regmap_read(regmap, regh, ®valh);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
*val = regvalh * 1000;
|
||||||
|
return 0;
|
||||||
|
case hwmon_temp_max_hyst:
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT,
|
||||||
|
®valh);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
|
||||||
|
®vall);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
*val = (regvalh - regvall) * 1000;
|
||||||
|
return 0;
|
||||||
|
case hwmon_temp_crit_hyst:
|
||||||
|
regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
|
||||||
|
LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
|
||||||
|
ret = regmap_read(regmap, regh, ®valh);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
|
||||||
|
®vall);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
*val = (regvalh - regvall) * 1000;
|
||||||
|
return 0;
|
||||||
|
case hwmon_temp_type:
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_RW_CONFIG2, ®valh);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
*val = (regvalh & CFG2_REMOTE_TT) ? 1 : 2;
|
||||||
|
return 0;
|
||||||
|
case hwmon_temp_offset:
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFL,
|
||||||
|
®vall);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFH,
|
||||||
|
®valh);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
*val = temp_from_reg_signed(regvalh, regvall);
|
||||||
|
return 0;
|
||||||
|
case hwmon_temp_max_alarm:
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_R_STATUS1, ®valh);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
*val = !!(regvalh & STATUS1_ROS);
|
||||||
|
return 0;
|
||||||
|
case hwmon_temp_crit_alarm:
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_R_STATUS1, ®valh);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
*val = !!(regvalh & (channel ? STATUS1_RTCRIT : STATUS1_LOC));
|
||||||
|
return 0;
|
||||||
|
case hwmon_temp_fault:
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_R_STATUS1, ®valh);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
*val = !!(regvalh & STATUS1_DIODE_FAULT);
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
return -EOPNOTSUPP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t show_crit_hyst(struct device *dev, struct device_attribute *attr,
|
static int lm95245_write_temp(struct device *dev, u32 attr, int channel,
|
||||||
char *buf)
|
long val)
|
||||||
{
|
|
||||||
struct lm95245_data *data = lm95245_update_device(dev);
|
|
||||||
int index = to_sensor_dev_attr(attr)->index;
|
|
||||||
int hyst = data->regs[index] - data->regs[8];
|
|
||||||
|
|
||||||
return snprintf(buf, PAGE_SIZE - 1, "%d\n", hyst * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
static ssize_t set_crit_hyst(struct device *dev, struct device_attribute *attr,
|
|
||||||
const char *buf, size_t count)
|
|
||||||
{
|
{
|
||||||
struct lm95245_data *data = dev_get_drvdata(dev);
|
struct lm95245_data *data = dev_get_drvdata(dev);
|
||||||
int index = to_sensor_dev_attr(attr)->index;
|
struct regmap *regmap = data->regmap;
|
||||||
struct i2c_client *client = data->client;
|
unsigned int regval;
|
||||||
unsigned long val;
|
int ret, reg;
|
||||||
int hyst, limit;
|
|
||||||
|
|
||||||
if (kstrtoul(buf, 10, &val) < 0)
|
switch (attr) {
|
||||||
return -EINVAL;
|
case hwmon_temp_max:
|
||||||
|
val = clamp_val(val / 1000, 0, 255);
|
||||||
mutex_lock(&data->update_lock);
|
ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT, val);
|
||||||
|
return ret;
|
||||||
limit = i2c_smbus_read_byte_data(client, lm95245_reg_address[index]);
|
case hwmon_temp_crit:
|
||||||
hyst = limit - val / 1000;
|
reg = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT :
|
||||||
hyst = clamp_val(hyst, 0, 31);
|
LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT;
|
||||||
data->regs[8] = hyst;
|
val = clamp_val(val / 1000, 0, channel ? 255 : 127);
|
||||||
|
ret = regmap_write(regmap, reg, val);
|
||||||
/* shared crit hysteresis */
|
return ret;
|
||||||
i2c_smbus_write_byte_data(client, LM95245_REG_RW_COMMON_HYSTERESIS,
|
case hwmon_temp_crit_hyst:
|
||||||
hyst);
|
mutex_lock(&data->update_lock);
|
||||||
|
ret = regmap_read(regmap, LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT,
|
||||||
mutex_unlock(&data->update_lock);
|
®val);
|
||||||
|
if (ret < 0) {
|
||||||
return count;
|
mutex_unlock(&data->update_lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
/* Clamp to reasonable range to prevent overflow */
|
||||||
|
val = clamp_val(val, -1000000, 1000000);
|
||||||
|
val = regval - val / 1000;
|
||||||
|
val = clamp_val(val, 0, 31);
|
||||||
|
ret = regmap_write(regmap, LM95245_REG_RW_COMMON_HYSTERESIS,
|
||||||
|
val);
|
||||||
|
mutex_unlock(&data->update_lock);
|
||||||
|
return ret;
|
||||||
|
case hwmon_temp_offset:
|
||||||
|
val = clamp_val(val, -128000, 127875);
|
||||||
|
val = val * 256 / 1000;
|
||||||
|
mutex_lock(&data->update_lock);
|
||||||
|
ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFL,
|
||||||
|
val & 0xe0);
|
||||||
|
if (ret < 0) {
|
||||||
|
mutex_unlock(&data->update_lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFH,
|
||||||
|
(val >> 8) & 0xff);
|
||||||
|
mutex_unlock(&data->update_lock);
|
||||||
|
return ret;
|
||||||
|
case hwmon_temp_type:
|
||||||
|
if (val != 1 && val != 2)
|
||||||
|
return -EINVAL;
|
||||||
|
ret = regmap_update_bits(regmap, LM95245_REG_RW_CONFIG2,
|
||||||
|
CFG2_REMOTE_TT,
|
||||||
|
val == 1 ? CFG2_REMOTE_TT : 0);
|
||||||
|
return ret;
|
||||||
|
default:
|
||||||
|
return -EOPNOTSUPP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t show_type(struct device *dev, struct device_attribute *attr,
|
static int lm95245_read_chip(struct device *dev, u32 attr, int channel,
|
||||||
char *buf)
|
long *val)
|
||||||
{
|
{
|
||||||
struct lm95245_data *data = dev_get_drvdata(dev);
|
struct lm95245_data *data = dev_get_drvdata(dev);
|
||||||
|
|
||||||
return snprintf(buf, PAGE_SIZE - 1,
|
switch (attr) {
|
||||||
data->config2 & CFG2_REMOTE_TT ? "1\n" : "2\n");
|
case hwmon_chip_update_interval:
|
||||||
|
*val = data->interval;
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
return -EOPNOTSUPP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t set_type(struct device *dev, struct device_attribute *attr,
|
static int lm95245_write_chip(struct device *dev, u32 attr, int channel,
|
||||||
const char *buf, size_t count)
|
long val)
|
||||||
{
|
{
|
||||||
struct lm95245_data *data = dev_get_drvdata(dev);
|
struct lm95245_data *data = dev_get_drvdata(dev);
|
||||||
struct i2c_client *client = data->client;
|
int ret;
|
||||||
unsigned long val;
|
|
||||||
|
|
||||||
if (kstrtoul(buf, 10, &val) < 0)
|
switch (attr) {
|
||||||
return -EINVAL;
|
case hwmon_chip_update_interval:
|
||||||
if (val != 1 && val != 2)
|
mutex_lock(&data->update_lock);
|
||||||
return -EINVAL;
|
ret = lm95245_set_conversion_rate(data, val);
|
||||||
|
mutex_unlock(&data->update_lock);
|
||||||
mutex_lock(&data->update_lock);
|
return ret;
|
||||||
|
default:
|
||||||
if (val == 1)
|
return -EOPNOTSUPP;
|
||||||
data->config2 |= CFG2_REMOTE_TT;
|
}
|
||||||
else
|
|
||||||
data->config2 &= ~CFG2_REMOTE_TT;
|
|
||||||
|
|
||||||
data->valid = 0;
|
|
||||||
|
|
||||||
i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG2,
|
|
||||||
data->config2);
|
|
||||||
|
|
||||||
mutex_unlock(&data->update_lock);
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
|
static int lm95245_read(struct device *dev, enum hwmon_sensor_types type,
|
||||||
char *buf)
|
u32 attr, int channel, long *val)
|
||||||
{
|
{
|
||||||
struct lm95245_data *data = lm95245_update_device(dev);
|
switch (type) {
|
||||||
int index = to_sensor_dev_attr(attr)->index;
|
case hwmon_chip:
|
||||||
|
return lm95245_read_chip(dev, attr, channel, val);
|
||||||
return snprintf(buf, PAGE_SIZE - 1, "%d\n",
|
case hwmon_temp:
|
||||||
!!(data->regs[9] & index));
|
return lm95245_read_temp(dev, attr, channel, val);
|
||||||
|
default:
|
||||||
|
return -EOPNOTSUPP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
|
static int lm95245_write(struct device *dev, enum hwmon_sensor_types type,
|
||||||
char *buf)
|
u32 attr, int channel, long val)
|
||||||
{
|
{
|
||||||
struct lm95245_data *data = lm95245_update_device(dev);
|
switch (type) {
|
||||||
|
case hwmon_chip:
|
||||||
return snprintf(buf, PAGE_SIZE - 1, "%lu\n", data->interval);
|
return lm95245_write_chip(dev, attr, channel, val);
|
||||||
|
case hwmon_temp:
|
||||||
|
return lm95245_write_temp(dev, attr, channel, val);
|
||||||
|
default:
|
||||||
|
return -EOPNOTSUPP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
|
static umode_t lm95245_temp_is_visible(const void *data, u32 attr, int channel)
|
||||||
const char *buf, size_t count)
|
|
||||||
{
|
{
|
||||||
struct lm95245_data *data = dev_get_drvdata(dev);
|
switch (attr) {
|
||||||
struct i2c_client *client = data->client;
|
case hwmon_temp_input:
|
||||||
unsigned long val;
|
case hwmon_temp_max_alarm:
|
||||||
|
case hwmon_temp_max_hyst:
|
||||||
if (kstrtoul(buf, 10, &val) < 0)
|
case hwmon_temp_crit_alarm:
|
||||||
return -EINVAL;
|
case hwmon_temp_fault:
|
||||||
|
return S_IRUGO;
|
||||||
mutex_lock(&data->update_lock);
|
case hwmon_temp_type:
|
||||||
|
case hwmon_temp_max:
|
||||||
data->interval = lm95245_set_conversion_rate(client, val);
|
case hwmon_temp_crit:
|
||||||
|
case hwmon_temp_offset:
|
||||||
mutex_unlock(&data->update_lock);
|
return S_IRUGO | S_IWUSR;
|
||||||
|
case hwmon_temp_crit_hyst:
|
||||||
return count;
|
return (channel == 0) ? S_IRUGO | S_IWUSR : S_IRUGO;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
|
static umode_t lm95245_is_visible(const void *data,
|
||||||
static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_limit,
|
enum hwmon_sensor_types type,
|
||||||
set_limit, 6);
|
u32 attr, int channel)
|
||||||
static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_crit_hyst,
|
{
|
||||||
set_crit_hyst, 6);
|
switch (type) {
|
||||||
static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
|
case hwmon_chip:
|
||||||
STATUS1_LOC);
|
switch (attr) {
|
||||||
|
case hwmon_chip_update_interval:
|
||||||
static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2);
|
return S_IRUGO | S_IWUSR;
|
||||||
static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_limit,
|
default:
|
||||||
set_limit, 7);
|
return 0;
|
||||||
static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_crit_hyst, NULL, 7);
|
}
|
||||||
static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
|
case hwmon_temp:
|
||||||
STATUS1_RTCRIT);
|
return lm95245_temp_is_visible(data, attr, channel);
|
||||||
static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type,
|
default:
|
||||||
set_type, 0);
|
return 0;
|
||||||
static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL,
|
}
|
||||||
STATUS1_DIODE_FAULT);
|
}
|
||||||
|
|
||||||
static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
|
|
||||||
set_interval);
|
|
||||||
|
|
||||||
static struct attribute *lm95245_attrs[] = {
|
|
||||||
&sensor_dev_attr_temp1_input.dev_attr.attr,
|
|
||||||
&sensor_dev_attr_temp1_crit.dev_attr.attr,
|
|
||||||
&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
|
|
||||||
&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
|
|
||||||
&sensor_dev_attr_temp2_input.dev_attr.attr,
|
|
||||||
&sensor_dev_attr_temp2_crit.dev_attr.attr,
|
|
||||||
&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
|
|
||||||
&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
|
|
||||||
&sensor_dev_attr_temp2_type.dev_attr.attr,
|
|
||||||
&sensor_dev_attr_temp2_fault.dev_attr.attr,
|
|
||||||
&dev_attr_update_interval.attr,
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
ATTRIBUTE_GROUPS(lm95245);
|
|
||||||
|
|
||||||
/* Return 0 if detection is successful, -ENODEV otherwise */
|
/* Return 0 if detection is successful, -ENODEV otherwise */
|
||||||
static int lm95245_detect(struct i2c_client *new_client,
|
static int lm95245_detect(struct i2c_client *new_client,
|
||||||
|
@ -453,44 +487,130 @@ static int lm95245_detect(struct i2c_client *new_client,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void lm95245_init_client(struct i2c_client *client,
|
static int lm95245_init_client(struct lm95245_data *data)
|
||||||
struct lm95245_data *data)
|
|
||||||
{
|
{
|
||||||
data->interval = lm95245_read_conversion_rate(client);
|
int ret;
|
||||||
|
|
||||||
data->config1 = i2c_smbus_read_byte_data(client,
|
ret = lm95245_read_conversion_rate(data);
|
||||||
LM95245_REG_RW_CONFIG1);
|
if (ret < 0)
|
||||||
data->config2 = i2c_smbus_read_byte_data(client,
|
return ret;
|
||||||
LM95245_REG_RW_CONFIG2);
|
|
||||||
|
|
||||||
if (data->config1 & CFG_STOP) {
|
return regmap_update_bits(data->regmap, LM95245_REG_RW_CONFIG1,
|
||||||
/* Clear the standby bit */
|
CFG_STOP, 0);
|
||||||
data->config1 &= ~CFG_STOP;
|
}
|
||||||
i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG1,
|
|
||||||
data->config1);
|
static bool lm95245_is_writeable_reg(struct device *dev, unsigned int reg)
|
||||||
|
{
|
||||||
|
switch (reg) {
|
||||||
|
case LM95245_REG_RW_CONFIG1:
|
||||||
|
case LM95245_REG_RW_CONVERS_RATE:
|
||||||
|
case LM95245_REG_W_ONE_SHOT:
|
||||||
|
case LM95245_REG_RW_CONFIG2:
|
||||||
|
case LM95245_REG_RW_REMOTE_OFFH:
|
||||||
|
case LM95245_REG_RW_REMOTE_OFFL:
|
||||||
|
case LM95245_REG_RW_REMOTE_OS_LIMIT:
|
||||||
|
case LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT:
|
||||||
|
case LM95245_REG_RW_REMOTE_TCRIT_LIMIT:
|
||||||
|
case LM95245_REG_RW_COMMON_HYSTERESIS:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool lm95245_is_volatile_reg(struct device *dev, unsigned int reg)
|
||||||
|
{
|
||||||
|
switch (reg) {
|
||||||
|
case LM95245_REG_R_STATUS1:
|
||||||
|
case LM95245_REG_R_STATUS2:
|
||||||
|
case LM95245_REG_R_LOCAL_TEMPH_S:
|
||||||
|
case LM95245_REG_R_LOCAL_TEMPL_S:
|
||||||
|
case LM95245_REG_R_REMOTE_TEMPH_S:
|
||||||
|
case LM95245_REG_R_REMOTE_TEMPL_S:
|
||||||
|
case LM95245_REG_R_REMOTE_TEMPH_U:
|
||||||
|
case LM95245_REG_R_REMOTE_TEMPL_U:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct regmap_config lm95245_regmap_config = {
|
||||||
|
.reg_bits = 8,
|
||||||
|
.val_bits = 8,
|
||||||
|
.writeable_reg = lm95245_is_writeable_reg,
|
||||||
|
.volatile_reg = lm95245_is_volatile_reg,
|
||||||
|
.cache_type = REGCACHE_RBTREE,
|
||||||
|
.use_single_rw = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const u32 lm95245_chip_config[] = {
|
||||||
|
HWMON_C_UPDATE_INTERVAL,
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct hwmon_channel_info lm95245_chip = {
|
||||||
|
.type = hwmon_chip,
|
||||||
|
.config = lm95245_chip_config,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const u32 lm95245_temp_config[] = {
|
||||||
|
HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_CRIT_ALARM,
|
||||||
|
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST | HWMON_T_CRIT |
|
||||||
|
HWMON_T_CRIT_HYST | HWMON_T_FAULT | HWMON_T_MAX_ALARM |
|
||||||
|
HWMON_T_CRIT_ALARM | HWMON_T_TYPE | HWMON_T_OFFSET,
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct hwmon_channel_info lm95245_temp = {
|
||||||
|
.type = hwmon_temp,
|
||||||
|
.config = lm95245_temp_config,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct hwmon_channel_info *lm95245_info[] = {
|
||||||
|
&lm95245_chip,
|
||||||
|
&lm95245_temp,
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct hwmon_ops lm95245_hwmon_ops = {
|
||||||
|
.is_visible = lm95245_is_visible,
|
||||||
|
.read = lm95245_read,
|
||||||
|
.write = lm95245_write,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct hwmon_chip_info lm95245_chip_info = {
|
||||||
|
.ops = &lm95245_hwmon_ops,
|
||||||
|
.info = lm95245_info,
|
||||||
|
};
|
||||||
|
|
||||||
static int lm95245_probe(struct i2c_client *client,
|
static int lm95245_probe(struct i2c_client *client,
|
||||||
const struct i2c_device_id *id)
|
const struct i2c_device_id *id)
|
||||||
{
|
{
|
||||||
struct device *dev = &client->dev;
|
struct device *dev = &client->dev;
|
||||||
struct lm95245_data *data;
|
struct lm95245_data *data;
|
||||||
struct device *hwmon_dev;
|
struct device *hwmon_dev;
|
||||||
|
int ret;
|
||||||
|
|
||||||
data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL);
|
data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL);
|
||||||
if (!data)
|
if (!data)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
data->client = client;
|
data->regmap = devm_regmap_init_i2c(client, &lm95245_regmap_config);
|
||||||
|
if (IS_ERR(data->regmap))
|
||||||
|
return PTR_ERR(data->regmap);
|
||||||
|
|
||||||
mutex_init(&data->update_lock);
|
mutex_init(&data->update_lock);
|
||||||
|
|
||||||
/* Initialize the LM95245 chip */
|
/* Initialize the LM95245 chip */
|
||||||
lm95245_init_client(client, data);
|
ret = lm95245_init_client(data);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
|
||||||
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
|
hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
|
||||||
data,
|
data,
|
||||||
lm95245_groups);
|
&lm95245_chip_info,
|
||||||
|
NULL);
|
||||||
return PTR_ERR_OR_ZERO(hwmon_dev);
|
return PTR_ERR_OR_ZERO(hwmon_dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче