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