linux/drivers/hwmon/ad7418.c
<<
>>
Prefs
   1/*
   2 * An hwmon driver for the Analog Devices AD7416/17/18
   3 * Copyright (C) 2006-07 Tower Technologies
   4 *
   5 * Author: Alessandro Zummo <a.zummo@towertech.it>
   6 *
   7 * Based on lm75.c
   8 * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License,
  12 * as published by the Free Software Foundation - version 2.
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/jiffies.h>
  17#include <linux/i2c.h>
  18#include <linux/hwmon.h>
  19#include <linux/hwmon-sysfs.h>
  20#include <linux/err.h>
  21#include <linux/mutex.h>
  22#include <linux/delay.h>
  23#include <linux/slab.h>
  24
  25#include "lm75.h"
  26
  27#define DRV_VERSION "0.4"
  28
  29enum chips { ad7416, ad7417, ad7418 };
  30
  31/* AD7418 registers */
  32#define AD7418_REG_TEMP_IN      0x00
  33#define AD7418_REG_CONF         0x01
  34#define AD7418_REG_TEMP_HYST    0x02
  35#define AD7418_REG_TEMP_OS      0x03
  36#define AD7418_REG_ADC          0x04
  37#define AD7418_REG_CONF2        0x05
  38
  39#define AD7418_REG_ADC_CH(x)    ((x) << 5)
  40#define AD7418_CH_TEMP          AD7418_REG_ADC_CH(0)
  41
  42static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
  43                                        AD7418_REG_TEMP_HYST,
  44                                        AD7418_REG_TEMP_OS };
  45
  46struct ad7418_data {
  47        struct i2c_client       *client;
  48        enum chips              type;
  49        struct mutex            lock;
  50        int                     adc_max;        /* number of ADC channels */
  51        char                    valid;
  52        unsigned long           last_updated;   /* In jiffies */
  53        s16                     temp[3];        /* Register values */
  54        u16                     in[4];
  55};
  56
  57static struct ad7418_data *ad7418_update_device(struct device *dev)
  58{
  59        struct ad7418_data *data = dev_get_drvdata(dev);
  60        struct i2c_client *client = data->client;
  61
  62        mutex_lock(&data->lock);
  63
  64        if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  65                || !data->valid) {
  66                u8 cfg;
  67                int i, ch;
  68
  69                /* read config register and clear channel bits */
  70                cfg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
  71                cfg &= 0x1F;
  72
  73                i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
  74                                                cfg | AD7418_CH_TEMP);
  75                udelay(30);
  76
  77                for (i = 0; i < 3; i++) {
  78                        data->temp[i] =
  79                                i2c_smbus_read_word_swapped(client,
  80                                                AD7418_REG_TEMP[i]);
  81                }
  82
  83                for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
  84                        i2c_smbus_write_byte_data(client,
  85                                        AD7418_REG_CONF,
  86                                        cfg | AD7418_REG_ADC_CH(ch));
  87
  88                        udelay(15);
  89                        data->in[data->adc_max - 1 - i] =
  90                                i2c_smbus_read_word_swapped(client,
  91                                                AD7418_REG_ADC);
  92                }
  93
  94                /* restore old configuration value */
  95                i2c_smbus_write_word_swapped(client, AD7418_REG_CONF, cfg);
  96
  97                data->last_updated = jiffies;
  98                data->valid = 1;
  99        }
 100
 101        mutex_unlock(&data->lock);
 102
 103        return data;
 104}
 105
 106static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
 107                         char *buf)
 108{
 109        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 110        struct ad7418_data *data = ad7418_update_device(dev);
 111        return sprintf(buf, "%d\n",
 112                LM75_TEMP_FROM_REG(data->temp[attr->index]));
 113}
 114
 115static ssize_t adc_show(struct device *dev, struct device_attribute *devattr,
 116                        char *buf)
 117{
 118        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 119        struct ad7418_data *data = ad7418_update_device(dev);
 120
 121        return sprintf(buf, "%d\n",
 122                ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
 123}
 124
 125static ssize_t temp_store(struct device *dev,
 126                          struct device_attribute *devattr, const char *buf,
 127                          size_t count)
 128{
 129        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 130        struct ad7418_data *data = dev_get_drvdata(dev);
 131        struct i2c_client *client = data->client;
 132        long temp;
 133        int ret = kstrtol(buf, 10, &temp);
 134
 135        if (ret < 0)
 136                return ret;
 137
 138        mutex_lock(&data->lock);
 139        data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
 140        i2c_smbus_write_word_swapped(client,
 141                                     AD7418_REG_TEMP[attr->index],
 142                                     data->temp[attr->index]);
 143        mutex_unlock(&data->lock);
 144        return count;
 145}
 146
 147static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
 148static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp, 1);
 149static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
 150
 151static SENSOR_DEVICE_ATTR_RO(in1_input, adc, 0);
 152static SENSOR_DEVICE_ATTR_RO(in2_input, adc, 1);
 153static SENSOR_DEVICE_ATTR_RO(in3_input, adc, 2);
 154static SENSOR_DEVICE_ATTR_RO(in4_input, adc, 3);
 155
 156static struct attribute *ad7416_attrs[] = {
 157        &sensor_dev_attr_temp1_max.dev_attr.attr,
 158        &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
 159        &sensor_dev_attr_temp1_input.dev_attr.attr,
 160        NULL
 161};
 162ATTRIBUTE_GROUPS(ad7416);
 163
 164static struct attribute *ad7417_attrs[] = {
 165        &sensor_dev_attr_temp1_max.dev_attr.attr,
 166        &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
 167        &sensor_dev_attr_temp1_input.dev_attr.attr,
 168        &sensor_dev_attr_in1_input.dev_attr.attr,
 169        &sensor_dev_attr_in2_input.dev_attr.attr,
 170        &sensor_dev_attr_in3_input.dev_attr.attr,
 171        &sensor_dev_attr_in4_input.dev_attr.attr,
 172        NULL
 173};
 174ATTRIBUTE_GROUPS(ad7417);
 175
 176static struct attribute *ad7418_attrs[] = {
 177        &sensor_dev_attr_temp1_max.dev_attr.attr,
 178        &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
 179        &sensor_dev_attr_temp1_input.dev_attr.attr,
 180        &sensor_dev_attr_in1_input.dev_attr.attr,
 181        NULL
 182};
 183ATTRIBUTE_GROUPS(ad7418);
 184
 185static void ad7418_init_client(struct i2c_client *client)
 186{
 187        struct ad7418_data *data = i2c_get_clientdata(client);
 188
 189        int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
 190        if (reg < 0) {
 191                dev_err(&client->dev, "cannot read configuration register\n");
 192        } else {
 193                dev_info(&client->dev, "configuring for mode 1\n");
 194                i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
 195
 196                if (data->type == ad7417 || data->type == ad7418)
 197                        i2c_smbus_write_byte_data(client,
 198                                                AD7418_REG_CONF2, 0x00);
 199        }
 200}
 201
 202static int ad7418_probe(struct i2c_client *client,
 203                         const struct i2c_device_id *id)
 204{
 205        struct device *dev = &client->dev;
 206        struct i2c_adapter *adapter = client->adapter;
 207        struct ad7418_data *data;
 208        struct device *hwmon_dev;
 209        const struct attribute_group **attr_groups = NULL;
 210
 211        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
 212                                        I2C_FUNC_SMBUS_WORD_DATA))
 213                return -EOPNOTSUPP;
 214
 215        data = devm_kzalloc(dev, sizeof(struct ad7418_data), GFP_KERNEL);
 216        if (!data)
 217                return -ENOMEM;
 218
 219        i2c_set_clientdata(client, data);
 220
 221        mutex_init(&data->lock);
 222        data->client = client;
 223        data->type = id->driver_data;
 224
 225        switch (data->type) {
 226        case ad7416:
 227                data->adc_max = 0;
 228                attr_groups = ad7416_groups;
 229                break;
 230
 231        case ad7417:
 232                data->adc_max = 4;
 233                attr_groups = ad7417_groups;
 234                break;
 235
 236        case ad7418:
 237                data->adc_max = 1;
 238                attr_groups = ad7418_groups;
 239                break;
 240        }
 241
 242        dev_info(dev, "%s chip found\n", client->name);
 243
 244        /* Initialize the AD7418 chip */
 245        ad7418_init_client(client);
 246
 247        hwmon_dev = devm_hwmon_device_register_with_groups(dev,
 248                                                           client->name,
 249                                                           data, attr_groups);
 250        return PTR_ERR_OR_ZERO(hwmon_dev);
 251}
 252
 253static const struct i2c_device_id ad7418_id[] = {
 254        { "ad7416", ad7416 },
 255        { "ad7417", ad7417 },
 256        { "ad7418", ad7418 },
 257        { }
 258};
 259MODULE_DEVICE_TABLE(i2c, ad7418_id);
 260
 261static struct i2c_driver ad7418_driver = {
 262        .driver = {
 263                .name   = "ad7418",
 264        },
 265        .probe          = ad7418_probe,
 266        .id_table       = ad7418_id,
 267};
 268
 269module_i2c_driver(ad7418_driver);
 270
 271MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
 272MODULE_DESCRIPTION("AD7416/17/18 driver");
 273MODULE_LICENSE("GPL");
 274MODULE_VERSION(DRV_VERSION);
 275