0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/init.h>
0014 #include <linux/slab.h>
0015 #include <linux/i2c.h>
0016 #include <linux/hwmon.h>
0017 #include <linux/hwmon-sysfs.h>
0018 #include <linux/err.h>
0019 #include <linux/sysfs.h>
0020 #include <linux/mutex.h>
0021 #include <linux/regmap.h>
0022
0023 #define THERMAL_PID_REG 0xfd
0024 #define THERMAL_SMSC_ID_REG 0xfe
0025 #define THERMAL_REVISION_REG 0xff
0026
0027 enum emc1403_chip { emc1402, emc1403, emc1404 };
0028
0029 struct thermal_data {
0030 struct regmap *regmap;
0031 struct mutex mutex;
0032 const struct attribute_group *groups[4];
0033 };
0034
0035 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
0036 char *buf)
0037 {
0038 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
0039 struct thermal_data *data = dev_get_drvdata(dev);
0040 unsigned int val;
0041 int retval;
0042
0043 retval = regmap_read(data->regmap, sda->index, &val);
0044 if (retval < 0)
0045 return retval;
0046 return sprintf(buf, "%d000\n", val);
0047 }
0048
0049 static ssize_t bit_show(struct device *dev, struct device_attribute *attr,
0050 char *buf)
0051 {
0052 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
0053 struct thermal_data *data = dev_get_drvdata(dev);
0054 unsigned int val;
0055 int retval;
0056
0057 retval = regmap_read(data->regmap, sda->nr, &val);
0058 if (retval < 0)
0059 return retval;
0060 return sprintf(buf, "%d\n", !!(val & sda->index));
0061 }
0062
0063 static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
0064 const char *buf, size_t count)
0065 {
0066 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
0067 struct thermal_data *data = dev_get_drvdata(dev);
0068 unsigned long val;
0069 int retval;
0070
0071 if (kstrtoul(buf, 10, &val))
0072 return -EINVAL;
0073 retval = regmap_write(data->regmap, sda->index,
0074 DIV_ROUND_CLOSEST(val, 1000));
0075 if (retval < 0)
0076 return retval;
0077 return count;
0078 }
0079
0080 static ssize_t bit_store(struct device *dev, struct device_attribute *attr,
0081 const char *buf, size_t count)
0082 {
0083 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
0084 struct thermal_data *data = dev_get_drvdata(dev);
0085 unsigned long val;
0086 int retval;
0087
0088 if (kstrtoul(buf, 10, &val))
0089 return -EINVAL;
0090
0091 retval = regmap_update_bits(data->regmap, sda->nr, sda->index,
0092 val ? sda->index : 0);
0093 if (retval < 0)
0094 return retval;
0095 return count;
0096 }
0097
0098 static ssize_t show_hyst_common(struct device *dev,
0099 struct device_attribute *attr, char *buf,
0100 bool is_min)
0101 {
0102 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
0103 struct thermal_data *data = dev_get_drvdata(dev);
0104 struct regmap *regmap = data->regmap;
0105 unsigned int limit;
0106 unsigned int hyst;
0107 int retval;
0108
0109 retval = regmap_read(regmap, sda->index, &limit);
0110 if (retval < 0)
0111 return retval;
0112
0113 retval = regmap_read(regmap, 0x21, &hyst);
0114 if (retval < 0)
0115 return retval;
0116
0117 return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst);
0118 }
0119
0120 static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
0121 char *buf)
0122 {
0123 return show_hyst_common(dev, attr, buf, false);
0124 }
0125
0126 static ssize_t min_hyst_show(struct device *dev,
0127 struct device_attribute *attr, char *buf)
0128 {
0129 return show_hyst_common(dev, attr, buf, true);
0130 }
0131
0132 static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
0133 const char *buf, size_t count)
0134 {
0135 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
0136 struct thermal_data *data = dev_get_drvdata(dev);
0137 struct regmap *regmap = data->regmap;
0138 unsigned int limit;
0139 int retval;
0140 int hyst;
0141 unsigned long val;
0142
0143 if (kstrtoul(buf, 10, &val))
0144 return -EINVAL;
0145
0146 mutex_lock(&data->mutex);
0147 retval = regmap_read(regmap, sda->index, &limit);
0148 if (retval < 0)
0149 goto fail;
0150
0151 hyst = limit * 1000 - val;
0152 hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255);
0153 retval = regmap_write(regmap, 0x21, hyst);
0154 if (retval == 0)
0155 retval = count;
0156 fail:
0157 mutex_unlock(&data->mutex);
0158 return retval;
0159 }
0160
0161
0162
0163
0164
0165 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 0x06);
0166 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 0x05);
0167 static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, 0x20);
0168 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0x00);
0169 static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, bit, 0x36, 0x01);
0170 static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, bit, 0x35, 0x01);
0171 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, bit, 0x37, 0x01);
0172 static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, min_hyst, 0x06);
0173 static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, hyst, 0x05);
0174 static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0x20);
0175
0176 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, 0x08);
0177 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 0x07);
0178 static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, 0x19);
0179 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 0x01);
0180 static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, bit, 0x1b, 0x02);
0181 static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, bit, 0x36, 0x02);
0182 static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, bit, 0x35, 0x02);
0183 static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, bit, 0x37, 0x02);
0184 static SENSOR_DEVICE_ATTR_RO(temp2_min_hyst, min_hyst, 0x08);
0185 static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, hyst, 0x07);
0186 static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, hyst, 0x19);
0187
0188 static SENSOR_DEVICE_ATTR_RW(temp3_min, temp, 0x16);
0189 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 0x15);
0190 static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 0x1A);
0191 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 0x23);
0192 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, bit, 0x1b, 0x04);
0193 static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, bit, 0x36, 0x04);
0194 static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, bit, 0x35, 0x04);
0195 static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, bit, 0x37, 0x04);
0196 static SENSOR_DEVICE_ATTR_RO(temp3_min_hyst, min_hyst, 0x16);
0197 static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, hyst, 0x15);
0198 static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, hyst, 0x1A);
0199
0200 static SENSOR_DEVICE_ATTR_RW(temp4_min, temp, 0x2D);
0201 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 0x2C);
0202 static SENSOR_DEVICE_ATTR_RW(temp4_crit, temp, 0x30);
0203 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 0x2A);
0204 static SENSOR_DEVICE_ATTR_2_RO(temp4_fault, bit, 0x1b, 0x08);
0205 static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, bit, 0x36, 0x08);
0206 static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, bit, 0x35, 0x08);
0207 static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, bit, 0x37, 0x08);
0208 static SENSOR_DEVICE_ATTR_RO(temp4_min_hyst, min_hyst, 0x2D);
0209 static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, hyst, 0x2C);
0210 static SENSOR_DEVICE_ATTR_RO(temp4_crit_hyst, hyst, 0x30);
0211
0212 static SENSOR_DEVICE_ATTR_2_RW(power_state, bit, 0x03, 0x40);
0213
0214 static struct attribute *emc1402_attrs[] = {
0215 &sensor_dev_attr_temp1_min.dev_attr.attr,
0216 &sensor_dev_attr_temp1_max.dev_attr.attr,
0217 &sensor_dev_attr_temp1_crit.dev_attr.attr,
0218 &sensor_dev_attr_temp1_input.dev_attr.attr,
0219 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
0220 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
0221 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
0222
0223 &sensor_dev_attr_temp2_min.dev_attr.attr,
0224 &sensor_dev_attr_temp2_max.dev_attr.attr,
0225 &sensor_dev_attr_temp2_crit.dev_attr.attr,
0226 &sensor_dev_attr_temp2_input.dev_attr.attr,
0227 &sensor_dev_attr_temp2_min_hyst.dev_attr.attr,
0228 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
0229 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
0230
0231 &sensor_dev_attr_power_state.dev_attr.attr,
0232 NULL
0233 };
0234
0235 static const struct attribute_group emc1402_group = {
0236 .attrs = emc1402_attrs,
0237 };
0238
0239 static struct attribute *emc1403_attrs[] = {
0240 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
0241 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
0242 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
0243
0244 &sensor_dev_attr_temp2_fault.dev_attr.attr,
0245 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
0246 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
0247 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
0248
0249 &sensor_dev_attr_temp3_min.dev_attr.attr,
0250 &sensor_dev_attr_temp3_max.dev_attr.attr,
0251 &sensor_dev_attr_temp3_crit.dev_attr.attr,
0252 &sensor_dev_attr_temp3_input.dev_attr.attr,
0253 &sensor_dev_attr_temp3_fault.dev_attr.attr,
0254 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
0255 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
0256 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
0257 &sensor_dev_attr_temp3_min_hyst.dev_attr.attr,
0258 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
0259 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
0260 NULL
0261 };
0262
0263 static const struct attribute_group emc1403_group = {
0264 .attrs = emc1403_attrs,
0265 };
0266
0267 static struct attribute *emc1404_attrs[] = {
0268 &sensor_dev_attr_temp4_min.dev_attr.attr,
0269 &sensor_dev_attr_temp4_max.dev_attr.attr,
0270 &sensor_dev_attr_temp4_crit.dev_attr.attr,
0271 &sensor_dev_attr_temp4_input.dev_attr.attr,
0272 &sensor_dev_attr_temp4_fault.dev_attr.attr,
0273 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
0274 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
0275 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
0276 &sensor_dev_attr_temp4_min_hyst.dev_attr.attr,
0277 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
0278 &sensor_dev_attr_temp4_crit_hyst.dev_attr.attr,
0279 NULL
0280 };
0281
0282 static const struct attribute_group emc1404_group = {
0283 .attrs = emc1404_attrs,
0284 };
0285
0286
0287
0288
0289
0290
0291
0292
0293 static struct sensor_device_attribute_2 emc1402_alarms[] = {
0294 SENSOR_ATTR_2_RO(temp1_min_alarm, bit, 0x02, 0x20),
0295 SENSOR_ATTR_2_RO(temp1_max_alarm, bit, 0x02, 0x40),
0296 SENSOR_ATTR_2_RO(temp1_crit_alarm, bit, 0x02, 0x01),
0297
0298 SENSOR_ATTR_2_RO(temp2_fault, bit, 0x02, 0x04),
0299 SENSOR_ATTR_2_RO(temp2_min_alarm, bit, 0x02, 0x08),
0300 SENSOR_ATTR_2_RO(temp2_max_alarm, bit, 0x02, 0x10),
0301 SENSOR_ATTR_2_RO(temp2_crit_alarm, bit, 0x02, 0x02),
0302 };
0303
0304 static struct attribute *emc1402_alarm_attrs[] = {
0305 &emc1402_alarms[0].dev_attr.attr,
0306 &emc1402_alarms[1].dev_attr.attr,
0307 &emc1402_alarms[2].dev_attr.attr,
0308 &emc1402_alarms[3].dev_attr.attr,
0309 &emc1402_alarms[4].dev_attr.attr,
0310 &emc1402_alarms[5].dev_attr.attr,
0311 &emc1402_alarms[6].dev_attr.attr,
0312 NULL,
0313 };
0314
0315 static const struct attribute_group emc1402_alarm_group = {
0316 .attrs = emc1402_alarm_attrs,
0317 };
0318
0319 static int emc1403_detect(struct i2c_client *client,
0320 struct i2c_board_info *info)
0321 {
0322 int id;
0323
0324
0325 id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
0326 if (id != 0x5d)
0327 return -ENODEV;
0328
0329 id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
0330 switch (id) {
0331 case 0x20:
0332 strlcpy(info->type, "emc1402", I2C_NAME_SIZE);
0333 break;
0334 case 0x21:
0335 strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
0336 break;
0337 case 0x22:
0338 strlcpy(info->type, "emc1422", I2C_NAME_SIZE);
0339 break;
0340 case 0x23:
0341 strlcpy(info->type, "emc1423", I2C_NAME_SIZE);
0342 break;
0343 case 0x25:
0344 strlcpy(info->type, "emc1404", I2C_NAME_SIZE);
0345 break;
0346 case 0x27:
0347 strlcpy(info->type, "emc1424", I2C_NAME_SIZE);
0348 break;
0349 default:
0350 return -ENODEV;
0351 }
0352
0353 id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
0354 if (id < 0x01 || id > 0x04)
0355 return -ENODEV;
0356
0357 return 0;
0358 }
0359
0360 static bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg)
0361 {
0362 switch (reg) {
0363 case 0x00:
0364 case 0x01:
0365 case 0x02:
0366 case 0x10:
0367 case 0x1b:
0368 case 0x23:
0369 case 0x24:
0370 case 0x29:
0371 case 0x2a:
0372 case 0x2b:
0373 case 0x35:
0374 case 0x36:
0375 case 0x37:
0376 return true;
0377 default:
0378 return false;
0379 }
0380 }
0381
0382 static const struct regmap_config emc1403_regmap_config = {
0383 .reg_bits = 8,
0384 .val_bits = 8,
0385 .cache_type = REGCACHE_RBTREE,
0386 .volatile_reg = emc1403_regmap_is_volatile,
0387 };
0388
0389 static const struct i2c_device_id emc1403_idtable[];
0390
0391 static int emc1403_probe(struct i2c_client *client)
0392 {
0393 struct thermal_data *data;
0394 struct device *hwmon_dev;
0395 const struct i2c_device_id *id = i2c_match_id(emc1403_idtable, client);
0396
0397 data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
0398 GFP_KERNEL);
0399 if (data == NULL)
0400 return -ENOMEM;
0401
0402 data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
0403 if (IS_ERR(data->regmap))
0404 return PTR_ERR(data->regmap);
0405
0406 mutex_init(&data->mutex);
0407
0408 switch (id->driver_data) {
0409 case emc1404:
0410 data->groups[2] = &emc1404_group;
0411 fallthrough;
0412 case emc1403:
0413 data->groups[1] = &emc1403_group;
0414 fallthrough;
0415 case emc1402:
0416 data->groups[0] = &emc1402_group;
0417 }
0418
0419 if (id->driver_data == emc1402)
0420 data->groups[1] = &emc1402_alarm_group;
0421
0422 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
0423 client->name, data,
0424 data->groups);
0425 if (IS_ERR(hwmon_dev))
0426 return PTR_ERR(hwmon_dev);
0427
0428 dev_info(&client->dev, "%s Thermal chip found\n", id->name);
0429 return 0;
0430 }
0431
0432 static const unsigned short emc1403_address_list[] = {
0433 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
0434 };
0435
0436
0437 static const struct i2c_device_id emc1403_idtable[] = {
0438 { "emc1402", emc1402 },
0439 { "emc1403", emc1403 },
0440 { "emc1404", emc1404 },
0441 { "emc1412", emc1402 },
0442 { "emc1413", emc1403 },
0443 { "emc1414", emc1404 },
0444 { "emc1422", emc1402 },
0445 { "emc1423", emc1403 },
0446 { "emc1424", emc1404 },
0447 { }
0448 };
0449 MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
0450
0451 static struct i2c_driver sensor_emc1403 = {
0452 .class = I2C_CLASS_HWMON,
0453 .driver = {
0454 .name = "emc1403",
0455 },
0456 .detect = emc1403_detect,
0457 .probe_new = emc1403_probe,
0458 .id_table = emc1403_idtable,
0459 .address_list = emc1403_address_list,
0460 };
0461
0462 module_i2c_driver(sensor_emc1403);
0463
0464 MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
0465 MODULE_DESCRIPTION("emc1403 Thermal Driver");
0466 MODULE_LICENSE("GPL v2");