123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #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>
- #include <linux/i2c/ds620.h>
- #define DS620_REG_CONFIG_DONE 0x8000
- #define DS620_REG_CONFIG_NVB 0x4000
- #define DS620_REG_CONFIG_THF 0x2000
- #define DS620_REG_CONFIG_TLF 0x1000
- #define DS620_REG_CONFIG_R1 0x0800
- #define DS620_REG_CONFIG_R0 0x0400
- #define DS620_REG_CONFIG_AUTOC 0x0200
- #define DS620_REG_CONFIG_1SHOT 0x0100
- #define DS620_REG_CONFIG_PO2 0x0080
- #define DS620_REG_CONFIG_PO1 0x0040
- #define DS620_REG_CONFIG_A2 0x0020
- #define DS620_REG_CONFIG_A1 0x0010
- #define DS620_REG_CONFIG_A0 0x0008
- static const u8 DS620_REG_TEMP[3] = {
- 0xAA,
- 0xA2,
- 0xA0,
- };
- #define DS620_REG_CONF 0xAC
- #define DS620_COM_START 0x51
- #define DS620_COM_STOP 0x22
- struct ds620_data {
- struct i2c_client *client;
- struct mutex update_lock;
- char valid;
- unsigned long last_updated;
- s16 temp[3];
- };
- static void ds620_init_client(struct i2c_client *client)
- {
- struct ds620_platform_data *ds620_info = dev_get_platdata(&client->dev);
- u16 conf, new_conf;
- new_conf = conf =
- i2c_smbus_read_word_swapped(client, DS620_REG_CONF);
-
- new_conf &= ~DS620_REG_CONFIG_1SHOT;
-
- new_conf |= DS620_REG_CONFIG_PO2;
-
- if (ds620_info && ds620_info->pomode == 1)
- new_conf &= ~DS620_REG_CONFIG_PO1;
- else if (ds620_info && ds620_info->pomode == 2)
- new_conf |= DS620_REG_CONFIG_PO1;
- else
- new_conf &= ~DS620_REG_CONFIG_PO2;
-
- new_conf |= DS620_REG_CONFIG_R1 | DS620_REG_CONFIG_R0;
- if (conf != new_conf)
- i2c_smbus_write_word_swapped(client, DS620_REG_CONF, new_conf);
-
- i2c_smbus_write_byte(client, DS620_COM_START);
- }
- static struct ds620_data *ds620_update_client(struct device *dev)
- {
- struct ds620_data *data = dev_get_drvdata(dev);
- struct i2c_client *client = data->client;
- struct ds620_data *ret = data;
- mutex_lock(&data->update_lock);
- if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
- || !data->valid) {
- int i;
- int res;
- dev_dbg(&client->dev, "Starting ds620 update\n");
- for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
- res = i2c_smbus_read_word_swapped(client,
- DS620_REG_TEMP[i]);
- if (res < 0) {
- ret = ERR_PTR(res);
- goto abort;
- }
- data->temp[i] = res;
- }
- data->last_updated = jiffies;
- data->valid = 1;
- }
- abort:
- mutex_unlock(&data->update_lock);
- return ret;
- }
- static ssize_t show_temp(struct device *dev, struct device_attribute *da,
- char *buf)
- {
- struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
- struct ds620_data *data = ds620_update_client(dev);
- if (IS_ERR(data))
- return PTR_ERR(data);
- return sprintf(buf, "%d\n", ((data->temp[attr->index] / 8) * 625) / 10);
- }
- static ssize_t set_temp(struct device *dev, struct device_attribute *da,
- const char *buf, size_t count)
- {
- int res;
- long val;
- struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
- struct ds620_data *data = dev_get_drvdata(dev);
- struct i2c_client *client = data->client;
- res = kstrtol(buf, 10, &val);
- if (res)
- return res;
- val = (clamp_val(val, -128000, 128000) * 10 / 625) * 8;
- mutex_lock(&data->update_lock);
- data->temp[attr->index] = val;
- i2c_smbus_write_word_swapped(client, DS620_REG_TEMP[attr->index],
- data->temp[attr->index]);
- mutex_unlock(&data->update_lock);
- return count;
- }
- static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
- char *buf)
- {
- struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
- struct ds620_data *data = ds620_update_client(dev);
- struct i2c_client *client;
- u16 conf, new_conf;
- int res;
- if (IS_ERR(data))
- return PTR_ERR(data);
- client = data->client;
-
- res = i2c_smbus_read_word_swapped(client, DS620_REG_CONF);
- if (res < 0)
- return res;
- new_conf = conf = res;
- new_conf &= ~attr->index;
- if (conf != new_conf) {
- res = i2c_smbus_write_word_swapped(client, DS620_REG_CONF,
- new_conf);
- if (res < 0)
- return res;
- }
- return sprintf(buf, "%d\n", !!(conf & attr->index));
- }
- static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
- static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp, 1);
- static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, 2);
- static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
- DS620_REG_CONFIG_TLF);
- static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
- DS620_REG_CONFIG_THF);
- static struct attribute *ds620_attrs[] = {
- &sensor_dev_attr_temp1_input.dev_attr.attr,
- &sensor_dev_attr_temp1_min.dev_attr.attr,
- &sensor_dev_attr_temp1_max.dev_attr.attr,
- &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
- &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
- NULL
- };
- ATTRIBUTE_GROUPS(ds620);
- static int ds620_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- struct device *dev = &client->dev;
- struct device *hwmon_dev;
- struct ds620_data *data;
- data = devm_kzalloc(dev, sizeof(struct ds620_data), GFP_KERNEL);
- if (!data)
- return -ENOMEM;
- data->client = client;
- mutex_init(&data->update_lock);
-
- ds620_init_client(client);
- hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
- data, ds620_groups);
- return PTR_ERR_OR_ZERO(hwmon_dev);
- }
- static const struct i2c_device_id ds620_id[] = {
- {"ds620", 0},
- {}
- };
- MODULE_DEVICE_TABLE(i2c, ds620_id);
- static struct i2c_driver ds620_driver = {
- .class = I2C_CLASS_HWMON,
- .driver = {
- .name = "ds620",
- },
- .probe = ds620_probe,
- .id_table = ds620_id,
- };
- module_i2c_driver(ds620_driver);
- MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
- MODULE_DESCRIPTION("DS620 driver");
- MODULE_LICENSE("GPL");
|