1 /* 2 * emc1403.c - SMSC Thermal Driver 3 * 4 * Copyright (C) 2008 Intel Corp 5 * 6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; version 2 of the License. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 21 * 22 * TODO 23 * - cache alarm and critical limit registers 24 * - add emc1404 support 25 */ 26 27 #include <linux/module.h> 28 #include <linux/init.h> 29 #include <linux/slab.h> 30 #include <linux/i2c.h> 31 #include <linux/hwmon.h> 32 #include <linux/hwmon-sysfs.h> 33 #include <linux/err.h> 34 #include <linux/sysfs.h> 35 #include <linux/mutex.h> 36 37 #define THERMAL_PID_REG 0xfd 38 #define THERMAL_SMSC_ID_REG 0xfe 39 #define THERMAL_REVISION_REG 0xff 40 41 struct thermal_data { 42 struct device *hwmon_dev; 43 struct mutex mutex; 44 /* 45 * Cache the hyst value so we don't keep re-reading it. In theory 46 * we could cache it forever as nobody else should be writing it. 47 */ 48 u8 cached_hyst; 49 unsigned long hyst_valid; 50 }; 51 52 static ssize_t show_temp(struct device *dev, 53 struct device_attribute *attr, char *buf) 54 { 55 struct i2c_client *client = to_i2c_client(dev); 56 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 57 int retval = i2c_smbus_read_byte_data(client, sda->index); 58 59 if (retval < 0) 60 return retval; 61 return sprintf(buf, "%d000\n", retval); 62 } 63 64 static ssize_t show_bit(struct device *dev, 65 struct device_attribute *attr, char *buf) 66 { 67 struct i2c_client *client = to_i2c_client(dev); 68 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); 69 int retval = i2c_smbus_read_byte_data(client, sda->nr); 70 71 if (retval < 0) 72 return retval; 73 retval &= sda->index; 74 return sprintf(buf, "%d\n", retval ? 1 : 0); 75 } 76 77 static ssize_t store_temp(struct device *dev, 78 struct device_attribute *attr, const char *buf, size_t count) 79 { 80 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 81 struct i2c_client *client = to_i2c_client(dev); 82 unsigned long val; 83 int retval; 84 85 if (kstrtoul(buf, 10, &val)) 86 return -EINVAL; 87 retval = i2c_smbus_write_byte_data(client, sda->index, 88 DIV_ROUND_CLOSEST(val, 1000)); 89 if (retval < 0) 90 return retval; 91 return count; 92 } 93 94 static ssize_t store_bit(struct device *dev, 95 struct device_attribute *attr, const char *buf, size_t count) 96 { 97 struct i2c_client *client = to_i2c_client(dev); 98 struct thermal_data *data = i2c_get_clientdata(client); 99 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); 100 unsigned long val; 101 int retval; 102 103 if (kstrtoul(buf, 10, &val)) 104 return -EINVAL; 105 106 mutex_lock(&data->mutex); 107 retval = i2c_smbus_read_byte_data(client, sda->nr); 108 if (retval < 0) 109 goto fail; 110 111 retval &= ~sda->index; 112 if (val) 113 retval |= sda->index; 114 115 retval = i2c_smbus_write_byte_data(client, sda->index, retval); 116 if (retval == 0) 117 retval = count; 118 fail: 119 mutex_unlock(&data->mutex); 120 return retval; 121 } 122 123 static ssize_t show_hyst(struct device *dev, 124 struct device_attribute *attr, char *buf) 125 { 126 struct i2c_client *client = to_i2c_client(dev); 127 struct thermal_data *data = i2c_get_clientdata(client); 128 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 129 int retval; 130 int hyst; 131 132 retval = i2c_smbus_read_byte_data(client, sda->index); 133 if (retval < 0) 134 return retval; 135 136 if (time_after(jiffies, data->hyst_valid)) { 137 hyst = i2c_smbus_read_byte_data(client, 0x21); 138 if (hyst < 0) 139 return retval; 140 data->cached_hyst = hyst; 141 data->hyst_valid = jiffies + HZ; 142 } 143 return sprintf(buf, "%d000\n", retval - data->cached_hyst); 144 } 145 146 static ssize_t store_hyst(struct device *dev, 147 struct device_attribute *attr, const char *buf, size_t count) 148 { 149 struct i2c_client *client = to_i2c_client(dev); 150 struct thermal_data *data = i2c_get_clientdata(client); 151 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 152 int retval; 153 int hyst; 154 unsigned long val; 155 156 if (kstrtoul(buf, 10, &val)) 157 return -EINVAL; 158 159 mutex_lock(&data->mutex); 160 retval = i2c_smbus_read_byte_data(client, sda->index); 161 if (retval < 0) 162 goto fail; 163 164 hyst = val - retval * 1000; 165 hyst = DIV_ROUND_CLOSEST(hyst, 1000); 166 if (hyst < 0 || hyst > 255) { 167 retval = -ERANGE; 168 goto fail; 169 } 170 171 retval = i2c_smbus_write_byte_data(client, 0x21, hyst); 172 if (retval == 0) { 173 retval = count; 174 data->cached_hyst = hyst; 175 data->hyst_valid = jiffies + HZ; 176 } 177 fail: 178 mutex_unlock(&data->mutex); 179 return retval; 180 } 181 182 /* 183 * Sensors. We pass the actual i2c register to the methods. 184 */ 185 186 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, 187 show_temp, store_temp, 0x06); 188 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, 189 show_temp, store_temp, 0x05); 190 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, 191 show_temp, store_temp, 0x20); 192 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0x00); 193 static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, 194 show_bit, NULL, 0x36, 0x01); 195 static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, 196 show_bit, NULL, 0x35, 0x01); 197 static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, 198 show_bit, NULL, 0x37, 0x01); 199 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR, 200 show_hyst, store_hyst, 0x20); 201 202 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, 203 show_temp, store_temp, 0x08); 204 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, 205 show_temp, store_temp, 0x07); 206 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, 207 show_temp, store_temp, 0x19); 208 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0x01); 209 static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, 210 show_bit, NULL, 0x36, 0x02); 211 static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, 212 show_bit, NULL, 0x35, 0x02); 213 static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, 214 show_bit, NULL, 0x37, 0x02); 215 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO | S_IWUSR, 216 show_hyst, store_hyst, 0x19); 217 218 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, 219 show_temp, store_temp, 0x16); 220 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, 221 show_temp, store_temp, 0x15); 222 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO | S_IWUSR, 223 show_temp, store_temp, 0x1A); 224 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0x23); 225 static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, 226 show_bit, NULL, 0x36, 0x04); 227 static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, 228 show_bit, NULL, 0x35, 0x04); 229 static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, 230 show_bit, NULL, 0x37, 0x04); 231 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO | S_IWUSR, 232 show_hyst, store_hyst, 0x1A); 233 234 static SENSOR_DEVICE_ATTR_2(power_state, S_IRUGO | S_IWUSR, 235 show_bit, store_bit, 0x03, 0x40); 236 237 static struct attribute *mid_att_thermal[] = { 238 &sensor_dev_attr_temp1_min.dev_attr.attr, 239 &sensor_dev_attr_temp1_max.dev_attr.attr, 240 &sensor_dev_attr_temp1_crit.dev_attr.attr, 241 &sensor_dev_attr_temp1_input.dev_attr.attr, 242 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, 243 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 244 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 245 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, 246 &sensor_dev_attr_temp2_min.dev_attr.attr, 247 &sensor_dev_attr_temp2_max.dev_attr.attr, 248 &sensor_dev_attr_temp2_crit.dev_attr.attr, 249 &sensor_dev_attr_temp2_input.dev_attr.attr, 250 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, 251 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, 252 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, 253 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, 254 &sensor_dev_attr_temp3_min.dev_attr.attr, 255 &sensor_dev_attr_temp3_max.dev_attr.attr, 256 &sensor_dev_attr_temp3_crit.dev_attr.attr, 257 &sensor_dev_attr_temp3_input.dev_attr.attr, 258 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, 259 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, 260 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, 261 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, 262 &sensor_dev_attr_power_state.dev_attr.attr, 263 NULL 264 }; 265 266 static const struct attribute_group m_thermal_gr = { 267 .attrs = mid_att_thermal 268 }; 269 270 static int emc1403_detect(struct i2c_client *client, 271 struct i2c_board_info *info) 272 { 273 int id; 274 /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */ 275 276 id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG); 277 if (id != 0x5d) 278 return -ENODEV; 279 280 id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG); 281 switch (id) { 282 case 0x21: 283 strlcpy(info->type, "emc1403", I2C_NAME_SIZE); 284 break; 285 case 0x23: 286 strlcpy(info->type, "emc1423", I2C_NAME_SIZE); 287 break; 288 /* 289 * Note: 0x25 is the 1404 which is very similar and this 290 * driver could be extended 291 */ 292 default: 293 return -ENODEV; 294 } 295 296 id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG); 297 if (id != 0x01) 298 return -ENODEV; 299 300 return 0; 301 } 302 303 static int emc1403_probe(struct i2c_client *client, 304 const struct i2c_device_id *id) 305 { 306 int res; 307 struct thermal_data *data; 308 309 data = kzalloc(sizeof(struct thermal_data), GFP_KERNEL); 310 if (data == NULL) { 311 dev_warn(&client->dev, "out of memory"); 312 return -ENOMEM; 313 } 314 315 i2c_set_clientdata(client, data); 316 mutex_init(&data->mutex); 317 data->hyst_valid = jiffies - 1; /* Expired */ 318 319 res = sysfs_create_group(&client->dev.kobj, &m_thermal_gr); 320 if (res) { 321 dev_warn(&client->dev, "create group failed\n"); 322 goto thermal_error1; 323 } 324 data->hwmon_dev = hwmon_device_register(&client->dev); 325 if (IS_ERR(data->hwmon_dev)) { 326 res = PTR_ERR(data->hwmon_dev); 327 dev_warn(&client->dev, "register hwmon dev failed\n"); 328 goto thermal_error2; 329 } 330 dev_info(&client->dev, "EMC1403 Thermal chip found\n"); 331 return res; 332 333 thermal_error2: 334 sysfs_remove_group(&client->dev.kobj, &m_thermal_gr); 335 thermal_error1: 336 kfree(data); 337 return res; 338 } 339 340 static int emc1403_remove(struct i2c_client *client) 341 { 342 struct thermal_data *data = i2c_get_clientdata(client); 343 344 hwmon_device_unregister(data->hwmon_dev); 345 sysfs_remove_group(&client->dev.kobj, &m_thermal_gr); 346 kfree(data); 347 return 0; 348 } 349 350 static const unsigned short emc1403_address_list[] = { 351 0x18, 0x29, 0x4c, 0x4d, I2C_CLIENT_END 352 }; 353 354 static const struct i2c_device_id emc1403_idtable[] = { 355 { "emc1403", 0 }, 356 { "emc1423", 0 }, 357 { } 358 }; 359 MODULE_DEVICE_TABLE(i2c, emc1403_idtable); 360 361 static struct i2c_driver sensor_emc1403 = { 362 .class = I2C_CLASS_HWMON, 363 .driver = { 364 .name = "emc1403", 365 }, 366 .detect = emc1403_detect, 367 .probe = emc1403_probe, 368 .remove = emc1403_remove, 369 .id_table = emc1403_idtable, 370 .address_list = emc1403_address_list, 371 }; 372 373 module_i2c_driver(sensor_emc1403); 374 375 MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com"); 376 MODULE_DESCRIPTION("emc1403 Thermal Driver"); 377 MODULE_LICENSE("GPL v2"); 378