0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/module.h>
0016 #include <linux/init.h>
0017 #include <linux/slab.h>
0018 #include <linux/jiffies.h>
0019 #include <linux/i2c.h>
0020 #include <linux/hwmon.h>
0021 #include <linux/hwmon-sysfs.h>
0022 #include <linux/err.h>
0023 #include <linux/mutex.h>
0024 #include <linux/sysfs.h>
0025
0026 static const unsigned short normal_i2c[] = {
0027 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
0028
0029
0030
0031
0032
0033 #define MAX1619_REG_R_MAN_ID 0xFE
0034 #define MAX1619_REG_R_CHIP_ID 0xFF
0035 #define MAX1619_REG_R_CONFIG 0x03
0036 #define MAX1619_REG_W_CONFIG 0x09
0037 #define MAX1619_REG_R_CONVRATE 0x04
0038 #define MAX1619_REG_W_CONVRATE 0x0A
0039 #define MAX1619_REG_R_STATUS 0x02
0040 #define MAX1619_REG_R_LOCAL_TEMP 0x00
0041 #define MAX1619_REG_R_REMOTE_TEMP 0x01
0042 #define MAX1619_REG_R_REMOTE_HIGH 0x07
0043 #define MAX1619_REG_W_REMOTE_HIGH 0x0D
0044 #define MAX1619_REG_R_REMOTE_LOW 0x08
0045 #define MAX1619_REG_W_REMOTE_LOW 0x0E
0046 #define MAX1619_REG_R_REMOTE_CRIT 0x10
0047 #define MAX1619_REG_W_REMOTE_CRIT 0x12
0048 #define MAX1619_REG_R_TCRIT_HYST 0x11
0049 #define MAX1619_REG_W_TCRIT_HYST 0x13
0050
0051
0052
0053
0054
0055 static int temp_from_reg(int val)
0056 {
0057 return (val & 0x80 ? val-0x100 : val) * 1000;
0058 }
0059
0060 static int temp_to_reg(int val)
0061 {
0062 return (val < 0 ? val+0x100*1000 : val) / 1000;
0063 }
0064
0065 enum temp_index {
0066 t_input1 = 0,
0067 t_input2,
0068 t_low2,
0069 t_high2,
0070 t_crit2,
0071 t_hyst2,
0072 t_num_regs
0073 };
0074
0075
0076
0077
0078
0079 struct max1619_data {
0080 struct i2c_client *client;
0081 struct mutex update_lock;
0082 bool valid;
0083 unsigned long last_updated;
0084
0085
0086 u8 temp[t_num_regs];
0087 u8 alarms;
0088 };
0089
0090 static const u8 regs_read[t_num_regs] = {
0091 [t_input1] = MAX1619_REG_R_LOCAL_TEMP,
0092 [t_input2] = MAX1619_REG_R_REMOTE_TEMP,
0093 [t_low2] = MAX1619_REG_R_REMOTE_LOW,
0094 [t_high2] = MAX1619_REG_R_REMOTE_HIGH,
0095 [t_crit2] = MAX1619_REG_R_REMOTE_CRIT,
0096 [t_hyst2] = MAX1619_REG_R_TCRIT_HYST,
0097 };
0098
0099 static const u8 regs_write[t_num_regs] = {
0100 [t_low2] = MAX1619_REG_W_REMOTE_LOW,
0101 [t_high2] = MAX1619_REG_W_REMOTE_HIGH,
0102 [t_crit2] = MAX1619_REG_W_REMOTE_CRIT,
0103 [t_hyst2] = MAX1619_REG_W_TCRIT_HYST,
0104 };
0105
0106 static struct max1619_data *max1619_update_device(struct device *dev)
0107 {
0108 struct max1619_data *data = dev_get_drvdata(dev);
0109 struct i2c_client *client = data->client;
0110 int config, i;
0111
0112 mutex_lock(&data->update_lock);
0113
0114 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
0115 dev_dbg(&client->dev, "Updating max1619 data.\n");
0116 for (i = 0; i < t_num_regs; i++)
0117 data->temp[i] = i2c_smbus_read_byte_data(client,
0118 regs_read[i]);
0119 data->alarms = i2c_smbus_read_byte_data(client,
0120 MAX1619_REG_R_STATUS);
0121
0122 config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
0123 if (!(config & 0x20))
0124 data->alarms ^= 0x02;
0125
0126 data->last_updated = jiffies;
0127 data->valid = true;
0128 }
0129
0130 mutex_unlock(&data->update_lock);
0131
0132 return data;
0133 }
0134
0135
0136
0137
0138
0139 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
0140 char *buf)
0141 {
0142 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0143 struct max1619_data *data = max1619_update_device(dev);
0144
0145 return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
0146 }
0147
0148 static ssize_t temp_store(struct device *dev,
0149 struct device_attribute *devattr, const char *buf,
0150 size_t count)
0151 {
0152 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0153 struct max1619_data *data = dev_get_drvdata(dev);
0154 struct i2c_client *client = data->client;
0155 long val;
0156 int err = kstrtol(buf, 10, &val);
0157 if (err)
0158 return err;
0159
0160 mutex_lock(&data->update_lock);
0161 data->temp[attr->index] = temp_to_reg(val);
0162 i2c_smbus_write_byte_data(client, regs_write[attr->index],
0163 data->temp[attr->index]);
0164 mutex_unlock(&data->update_lock);
0165 return count;
0166 }
0167
0168 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
0169 char *buf)
0170 {
0171 struct max1619_data *data = max1619_update_device(dev);
0172 return sprintf(buf, "%d\n", data->alarms);
0173 }
0174
0175 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
0176 char *buf)
0177 {
0178 int bitnr = to_sensor_dev_attr(attr)->index;
0179 struct max1619_data *data = max1619_update_device(dev);
0180 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
0181 }
0182
0183 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input1);
0184 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, t_input2);
0185 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, t_low2);
0186 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, t_high2);
0187 static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, t_crit2);
0188 static SENSOR_DEVICE_ATTR_RW(temp2_crit_hyst, temp, t_hyst2);
0189
0190 static DEVICE_ATTR_RO(alarms);
0191 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 1);
0192 static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2);
0193 static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, alarm, 3);
0194 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4);
0195
0196 static struct attribute *max1619_attrs[] = {
0197 &sensor_dev_attr_temp1_input.dev_attr.attr,
0198 &sensor_dev_attr_temp2_input.dev_attr.attr,
0199 &sensor_dev_attr_temp2_min.dev_attr.attr,
0200 &sensor_dev_attr_temp2_max.dev_attr.attr,
0201 &sensor_dev_attr_temp2_crit.dev_attr.attr,
0202 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
0203
0204 &dev_attr_alarms.attr,
0205 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
0206 &sensor_dev_attr_temp2_fault.dev_attr.attr,
0207 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
0208 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
0209 NULL
0210 };
0211 ATTRIBUTE_GROUPS(max1619);
0212
0213
0214 static int max1619_detect(struct i2c_client *client,
0215 struct i2c_board_info *info)
0216 {
0217 struct i2c_adapter *adapter = client->adapter;
0218 u8 reg_config, reg_convrate, reg_status, man_id, chip_id;
0219
0220 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0221 return -ENODEV;
0222
0223
0224 reg_config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
0225 reg_convrate = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONVRATE);
0226 reg_status = i2c_smbus_read_byte_data(client, MAX1619_REG_R_STATUS);
0227 if ((reg_config & 0x03) != 0x00
0228 || reg_convrate > 0x07 || (reg_status & 0x61) != 0x00) {
0229 dev_dbg(&adapter->dev, "MAX1619 detection failed at 0x%02x\n",
0230 client->addr);
0231 return -ENODEV;
0232 }
0233
0234
0235 man_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_MAN_ID);
0236 chip_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CHIP_ID);
0237 if (man_id != 0x4D || chip_id != 0x04) {
0238 dev_info(&adapter->dev,
0239 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X).\n",
0240 man_id, chip_id);
0241 return -ENODEV;
0242 }
0243
0244 strlcpy(info->type, "max1619", I2C_NAME_SIZE);
0245
0246 return 0;
0247 }
0248
0249 static void max1619_init_client(struct i2c_client *client)
0250 {
0251 u8 config;
0252
0253
0254
0255
0256 i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
0257 5);
0258 config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
0259 if (config & 0x40)
0260 i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
0261 config & 0xBF);
0262 }
0263
0264 static int max1619_probe(struct i2c_client *new_client)
0265 {
0266 struct max1619_data *data;
0267 struct device *hwmon_dev;
0268
0269 data = devm_kzalloc(&new_client->dev, sizeof(struct max1619_data),
0270 GFP_KERNEL);
0271 if (!data)
0272 return -ENOMEM;
0273
0274 data->client = new_client;
0275 mutex_init(&data->update_lock);
0276
0277
0278 max1619_init_client(new_client);
0279
0280 hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
0281 new_client->name,
0282 data,
0283 max1619_groups);
0284 return PTR_ERR_OR_ZERO(hwmon_dev);
0285 }
0286
0287 static const struct i2c_device_id max1619_id[] = {
0288 { "max1619", 0 },
0289 { }
0290 };
0291 MODULE_DEVICE_TABLE(i2c, max1619_id);
0292
0293 #ifdef CONFIG_OF
0294 static const struct of_device_id max1619_of_match[] = {
0295 { .compatible = "maxim,max1619", },
0296 {},
0297 };
0298
0299 MODULE_DEVICE_TABLE(of, max1619_of_match);
0300 #endif
0301
0302 static struct i2c_driver max1619_driver = {
0303 .class = I2C_CLASS_HWMON,
0304 .driver = {
0305 .name = "max1619",
0306 .of_match_table = of_match_ptr(max1619_of_match),
0307 },
0308 .probe_new = max1619_probe,
0309 .id_table = max1619_id,
0310 .detect = max1619_detect,
0311 .address_list = normal_i2c,
0312 };
0313
0314 module_i2c_driver(max1619_driver);
0315
0316 MODULE_AUTHOR("Oleksij Rempel <bug-track@fisher-privat.net>, Jean Delvare <jdelvare@suse.de>");
0317 MODULE_DESCRIPTION("MAX1619 sensor driver");
0318 MODULE_LICENSE("GPL");