Kalhan Trisal | dac6831 | 2010-05-27 19:58:56 +0200 | [diff] [blame] | 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 | /* Cache the hyst value so we don't keep re-reading it. In theory |
| 45 | we could cache it forever as nobody else should be writing it. */ |
| 46 | u8 cached_hyst; |
| 47 | unsigned long hyst_valid; |
| 48 | }; |
| 49 | |
| 50 | static ssize_t show_temp(struct device *dev, |
| 51 | struct device_attribute *attr, char *buf) |
| 52 | { |
| 53 | struct i2c_client *client = to_i2c_client(dev); |
| 54 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
| 55 | int retval = i2c_smbus_read_byte_data(client, sda->index); |
| 56 | |
| 57 | if (retval < 0) |
| 58 | return retval; |
| 59 | return sprintf(buf, "%d000\n", retval); |
| 60 | } |
| 61 | |
| 62 | static ssize_t show_bit(struct device *dev, |
| 63 | struct device_attribute *attr, char *buf) |
| 64 | { |
| 65 | struct i2c_client *client = to_i2c_client(dev); |
| 66 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
| 67 | int retval = i2c_smbus_read_byte_data(client, sda->nr); |
| 68 | |
| 69 | if (retval < 0) |
| 70 | return retval; |
| 71 | retval &= sda->index; |
| 72 | return sprintf(buf, "%d\n", retval ? 1 : 0); |
| 73 | } |
| 74 | |
| 75 | static ssize_t store_temp(struct device *dev, |
| 76 | struct device_attribute *attr, const char *buf, size_t count) |
| 77 | { |
| 78 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
| 79 | struct i2c_client *client = to_i2c_client(dev); |
| 80 | unsigned long val; |
| 81 | int retval; |
| 82 | |
| 83 | if (strict_strtoul(buf, 10, &val)) |
| 84 | return -EINVAL; |
| 85 | retval = i2c_smbus_write_byte_data(client, sda->index, |
| 86 | DIV_ROUND_CLOSEST(val, 1000)); |
| 87 | if (retval < 0) |
| 88 | return retval; |
| 89 | return count; |
| 90 | } |
| 91 | |
| 92 | static ssize_t show_hyst(struct device *dev, |
| 93 | struct device_attribute *attr, char *buf) |
| 94 | { |
| 95 | struct i2c_client *client = to_i2c_client(dev); |
| 96 | struct thermal_data *data = i2c_get_clientdata(client); |
| 97 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
| 98 | int retval; |
| 99 | int hyst; |
| 100 | |
| 101 | retval = i2c_smbus_read_byte_data(client, sda->index); |
| 102 | if (retval < 0) |
| 103 | return retval; |
| 104 | |
| 105 | if (time_after(jiffies, data->hyst_valid)) { |
| 106 | hyst = i2c_smbus_read_byte_data(client, 0x21); |
| 107 | if (hyst < 0) |
| 108 | return retval; |
| 109 | data->cached_hyst = hyst; |
| 110 | data->hyst_valid = jiffies + HZ; |
| 111 | } |
| 112 | return sprintf(buf, "%d000\n", retval - data->cached_hyst); |
| 113 | } |
| 114 | |
| 115 | static ssize_t store_hyst(struct device *dev, |
| 116 | struct device_attribute *attr, const char *buf, size_t count) |
| 117 | { |
| 118 | struct i2c_client *client = to_i2c_client(dev); |
| 119 | struct thermal_data *data = i2c_get_clientdata(client); |
| 120 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
| 121 | int retval; |
| 122 | int hyst; |
| 123 | unsigned long val; |
| 124 | |
| 125 | if (strict_strtoul(buf, 10, &val)) |
| 126 | return -EINVAL; |
| 127 | |
| 128 | mutex_lock(&data->mutex); |
| 129 | retval = i2c_smbus_read_byte_data(client, sda->index); |
| 130 | if (retval < 0) |
| 131 | goto fail; |
| 132 | |
| 133 | hyst = val - retval * 1000; |
| 134 | hyst = DIV_ROUND_CLOSEST(hyst, 1000); |
| 135 | if (hyst < 0 || hyst > 255) { |
| 136 | retval = -ERANGE; |
| 137 | goto fail; |
| 138 | } |
| 139 | |
| 140 | retval = i2c_smbus_write_byte_data(client, 0x21, hyst); |
| 141 | if (retval == 0) { |
| 142 | retval = count; |
| 143 | data->cached_hyst = hyst; |
| 144 | data->hyst_valid = jiffies + HZ; |
| 145 | } |
| 146 | fail: |
| 147 | mutex_unlock(&data->mutex); |
| 148 | return retval; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Sensors. We pass the actual i2c register to the methods. |
| 153 | */ |
| 154 | |
| 155 | static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, |
| 156 | show_temp, store_temp, 0x06); |
| 157 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, |
| 158 | show_temp, store_temp, 0x05); |
| 159 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, |
| 160 | show_temp, store_temp, 0x20); |
| 161 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0x00); |
| 162 | static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, |
| 163 | show_bit, NULL, 0x36, 0x01); |
| 164 | static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, |
| 165 | show_bit, NULL, 0x35, 0x01); |
| 166 | static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, |
| 167 | show_bit, NULL, 0x37, 0x01); |
| 168 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR, |
| 169 | show_hyst, store_hyst, 0x20); |
| 170 | |
| 171 | static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, |
| 172 | show_temp, store_temp, 0x08); |
| 173 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, |
| 174 | show_temp, store_temp, 0x07); |
| 175 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, |
| 176 | show_temp, store_temp, 0x19); |
| 177 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0x01); |
| 178 | static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, |
| 179 | show_bit, NULL, 0x36, 0x02); |
| 180 | static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, |
| 181 | show_bit, NULL, 0x35, 0x02); |
| 182 | static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, |
| 183 | show_bit, NULL, 0x37, 0x02); |
| 184 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO | S_IWUSR, |
| 185 | show_hyst, store_hyst, 0x19); |
| 186 | |
| 187 | static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, |
| 188 | show_temp, store_temp, 0x16); |
| 189 | static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, |
| 190 | show_temp, store_temp, 0x15); |
| 191 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO | S_IWUSR, |
| 192 | show_temp, store_temp, 0x1A); |
| 193 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0x23); |
| 194 | static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, |
| 195 | show_bit, NULL, 0x36, 0x04); |
| 196 | static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, |
| 197 | show_bit, NULL, 0x35, 0x04); |
| 198 | static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, |
| 199 | show_bit, NULL, 0x37, 0x04); |
| 200 | static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO | S_IWUSR, |
| 201 | show_hyst, store_hyst, 0x1A); |
| 202 | |
| 203 | static struct attribute *mid_att_thermal[] = { |
| 204 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 205 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 206 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 207 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 208 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 209 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 210 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 211 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
| 212 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 213 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 214 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 215 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 216 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 217 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 218 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
| 219 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 220 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 221 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 222 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 223 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 224 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, |
| 225 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
| 226 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, |
| 227 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, |
| 228 | NULL |
| 229 | }; |
| 230 | |
| 231 | static const struct attribute_group m_thermal_gr = { |
| 232 | .attrs = mid_att_thermal |
| 233 | }; |
| 234 | |
| 235 | static int emc1403_detect(struct i2c_client *client, |
| 236 | struct i2c_board_info *info) |
| 237 | { |
| 238 | int id; |
| 239 | /* Check if thermal chip is SMSC and EMC1403 */ |
| 240 | |
| 241 | id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG); |
| 242 | if (id != 0x5d) |
| 243 | return -ENODEV; |
| 244 | |
| 245 | /* Note: 0x25 is the 1404 which is very similar and this |
| 246 | driver could be extended */ |
| 247 | id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG); |
| 248 | if (id != 0x21) |
| 249 | return -ENODEV; |
| 250 | |
| 251 | id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG); |
| 252 | if (id != 0x01) |
| 253 | return -ENODEV; |
| 254 | |
| 255 | strlcpy(info->type, "emc1403", I2C_NAME_SIZE); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int emc1403_probe(struct i2c_client *client, |
| 260 | const struct i2c_device_id *id) |
| 261 | { |
| 262 | int res; |
| 263 | struct thermal_data *data; |
| 264 | |
| 265 | data = kzalloc(sizeof(struct thermal_data), GFP_KERNEL); |
| 266 | if (data == NULL) { |
| 267 | dev_warn(&client->dev, "out of memory"); |
| 268 | return -ENOMEM; |
| 269 | } |
| 270 | |
| 271 | i2c_set_clientdata(client, data); |
| 272 | mutex_init(&data->mutex); |
| 273 | data->hyst_valid = jiffies - 1; /* Expired */ |
| 274 | |
| 275 | res = sysfs_create_group(&client->dev.kobj, &m_thermal_gr); |
| 276 | if (res) { |
| 277 | dev_warn(&client->dev, "create group failed\n"); |
| 278 | hwmon_device_unregister(data->hwmon_dev); |
| 279 | goto thermal_error1; |
| 280 | } |
| 281 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 282 | if (IS_ERR(data->hwmon_dev)) { |
| 283 | res = PTR_ERR(data->hwmon_dev); |
| 284 | dev_warn(&client->dev, "register hwmon dev failed\n"); |
| 285 | goto thermal_error2; |
| 286 | } |
| 287 | dev_info(&client->dev, "EMC1403 Thermal chip found\n"); |
| 288 | return res; |
| 289 | |
| 290 | thermal_error2: |
| 291 | sysfs_remove_group(&client->dev.kobj, &m_thermal_gr); |
| 292 | thermal_error1: |
| 293 | kfree(data); |
| 294 | return res; |
| 295 | } |
| 296 | |
| 297 | static int emc1403_remove(struct i2c_client *client) |
| 298 | { |
| 299 | struct thermal_data *data = i2c_get_clientdata(client); |
| 300 | |
| 301 | hwmon_device_unregister(data->hwmon_dev); |
| 302 | sysfs_remove_group(&client->dev.kobj, &m_thermal_gr); |
| 303 | kfree(data); |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static const unsigned short emc1403_address_list[] = { |
| 308 | 0x18, 0x2a, 0x4c, 0x4d, I2C_CLIENT_END |
| 309 | }; |
| 310 | |
| 311 | static const struct i2c_device_id emc1403_idtable[] = { |
| 312 | { "emc1403", 0 }, |
| 313 | { } |
| 314 | }; |
| 315 | MODULE_DEVICE_TABLE(i2c, emc1403_idtable); |
| 316 | |
| 317 | static struct i2c_driver sensor_emc1403 = { |
| 318 | .class = I2C_CLASS_HWMON, |
| 319 | .driver = { |
| 320 | .name = "emc1403", |
| 321 | }, |
| 322 | .detect = emc1403_detect, |
| 323 | .probe = emc1403_probe, |
| 324 | .remove = emc1403_remove, |
| 325 | .id_table = emc1403_idtable, |
| 326 | .address_list = emc1403_address_list, |
| 327 | }; |
| 328 | |
| 329 | static int __init sensor_emc1403_init(void) |
| 330 | { |
| 331 | return i2c_add_driver(&sensor_emc1403); |
| 332 | } |
| 333 | |
| 334 | static void __exit sensor_emc1403_exit(void) |
| 335 | { |
| 336 | i2c_del_driver(&sensor_emc1403); |
| 337 | } |
| 338 | |
| 339 | module_init(sensor_emc1403_init); |
| 340 | module_exit(sensor_emc1403_exit); |
| 341 | |
| 342 | MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com"); |
| 343 | MODULE_DESCRIPTION("emc1403 Thermal Driver"); |
| 344 | MODULE_LICENSE("GPL v2"); |