Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 24 | |
| 25 | #include "lm75.h" |
| 26 | |
Jean Delvare | 369932f | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 27 | #define DRV_VERSION "0.4" |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 28 | |
Jean Delvare | 369932f | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 29 | enum chips { ad7416, ad7417, ad7418 }; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 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 | |
| 42 | static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN, |
| 43 | AD7418_REG_TEMP_HYST, |
| 44 | AD7418_REG_TEMP_OS }; |
| 45 | |
| 46 | struct ad7418_data { |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 47 | struct i2c_client *client; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 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 | |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 57 | static struct ad7418_data *ad7418_update_device(struct device *dev) |
| 58 | { |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 59 | struct ad7418_data *data = dev_get_drvdata(dev); |
| 60 | struct i2c_client *client = data->client; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 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++) { |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 78 | data->temp[i] = |
| 79 | i2c_smbus_read_word_swapped(client, |
| 80 | AD7418_REG_TEMP[i]); |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 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] = |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 90 | i2c_smbus_read_word_swapped(client, |
| 91 | AD7418_REG_ADC); |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* restore old configuration value */ |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 95 | i2c_smbus_write_word_swapped(client, AD7418_REG_CONF, cfg); |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 96 | |
| 97 | data->last_updated = jiffies; |
| 98 | data->valid = 1; |
| 99 | } |
| 100 | |
| 101 | mutex_unlock(&data->lock); |
| 102 | |
| 103 | return data; |
| 104 | } |
| 105 | |
| 106 | static ssize_t show_temp(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 | |
| 115 | static ssize_t show_adc(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 | |
| 125 | static ssize_t set_temp(struct device *dev, struct device_attribute *devattr, |
| 126 | const char *buf, size_t count) |
| 127 | { |
| 128 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 129 | struct ad7418_data *data = dev_get_drvdata(dev); |
| 130 | struct i2c_client *client = data->client; |
Frans Meulenbroeks | e91aef22 | 2012-01-08 19:34:17 +0100 | [diff] [blame] | 131 | long temp; |
| 132 | int ret = kstrtol(buf, 10, &temp); |
| 133 | |
| 134 | if (ret < 0) |
| 135 | return ret; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 136 | |
| 137 | mutex_lock(&data->lock); |
| 138 | data->temp[attr->index] = LM75_TEMP_TO_REG(temp); |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 139 | i2c_smbus_write_word_swapped(client, |
| 140 | AD7418_REG_TEMP[attr->index], |
| 141 | data->temp[attr->index]); |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 142 | mutex_unlock(&data->lock); |
| 143 | return count; |
| 144 | } |
| 145 | |
| 146 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
| 147 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, |
| 148 | show_temp, set_temp, 1); |
| 149 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 150 | show_temp, set_temp, 2); |
| 151 | |
| 152 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_adc, NULL, 0); |
| 153 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 1); |
| 154 | static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2); |
| 155 | static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3); |
| 156 | |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 157 | static struct attribute *ad7416_attrs[] = { |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 158 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 159 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
| 160 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 161 | NULL |
| 162 | }; |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 163 | ATTRIBUTE_GROUPS(ad7416); |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 164 | |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 165 | static struct attribute *ad7417_attrs[] = { |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 166 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 167 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
| 168 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 169 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 170 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 171 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 172 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 173 | NULL |
| 174 | }; |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 175 | ATTRIBUTE_GROUPS(ad7417); |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 176 | |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 177 | static struct attribute *ad7418_attrs[] = { |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 178 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 179 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
| 180 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 181 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 182 | NULL |
| 183 | }; |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 184 | ATTRIBUTE_GROUPS(ad7418); |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 185 | |
Axel Lin | 991763a | 2014-07-01 22:39:08 +0800 | [diff] [blame] | 186 | static void ad7418_init_client(struct i2c_client *client) |
| 187 | { |
| 188 | struct ad7418_data *data = i2c_get_clientdata(client); |
| 189 | |
| 190 | int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF); |
| 191 | if (reg < 0) { |
| 192 | dev_err(&client->dev, "cannot read configuration register\n"); |
| 193 | } else { |
| 194 | dev_info(&client->dev, "configuring for mode 1\n"); |
| 195 | i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe); |
| 196 | |
| 197 | if (data->type == ad7417 || data->type == ad7418) |
| 198 | i2c_smbus_write_byte_data(client, |
| 199 | AD7418_REG_CONF2, 0x00); |
| 200 | } |
| 201 | } |
| 202 | |
Jean Delvare | 369932f | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 203 | static int ad7418_probe(struct i2c_client *client, |
| 204 | const struct i2c_device_id *id) |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 205 | { |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 206 | struct device *dev = &client->dev; |
Jean Delvare | 369932f | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 207 | struct i2c_adapter *adapter = client->adapter; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 208 | struct ad7418_data *data; |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 209 | struct device *hwmon_dev; |
| 210 | const struct attribute_group **attr_groups = NULL; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 211 | |
| 212 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
Guenter Roeck | d528292 | 2012-06-02 09:57:58 -0700 | [diff] [blame] | 213 | I2C_FUNC_SMBUS_WORD_DATA)) |
| 214 | return -EOPNOTSUPP; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 215 | |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 216 | data = devm_kzalloc(dev, sizeof(struct ad7418_data), GFP_KERNEL); |
Guenter Roeck | d528292 | 2012-06-02 09:57:58 -0700 | [diff] [blame] | 217 | if (!data) |
| 218 | return -ENOMEM; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 219 | |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 220 | i2c_set_clientdata(client, data); |
| 221 | |
| 222 | mutex_init(&data->lock); |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 223 | data->client = client; |
Jean Delvare | 369932f | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 224 | data->type = id->driver_data; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 225 | |
| 226 | switch (data->type) { |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 227 | case ad7416: |
| 228 | data->adc_max = 0; |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 229 | attr_groups = ad7416_groups; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 230 | break; |
| 231 | |
| 232 | case ad7417: |
| 233 | data->adc_max = 4; |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 234 | attr_groups = ad7417_groups; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 235 | break; |
| 236 | |
| 237 | case ad7418: |
| 238 | data->adc_max = 1; |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 239 | attr_groups = ad7418_groups; |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 243 | dev_info(dev, "%s chip found\n", client->name); |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 244 | |
| 245 | /* Initialize the AD7418 chip */ |
| 246 | ad7418_init_client(client); |
| 247 | |
Axel Lin | 337076f | 2014-07-01 22:40:29 +0800 | [diff] [blame] | 248 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, |
| 249 | client->name, |
| 250 | data, attr_groups); |
| 251 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Axel Lin | 991763a | 2014-07-01 22:39:08 +0800 | [diff] [blame] | 254 | static const struct i2c_device_id ad7418_id[] = { |
| 255 | { "ad7416", ad7416 }, |
| 256 | { "ad7417", ad7417 }, |
| 257 | { "ad7418", ad7418 }, |
| 258 | { } |
| 259 | }; |
| 260 | MODULE_DEVICE_TABLE(i2c, ad7418_id); |
| 261 | |
| 262 | static struct i2c_driver ad7418_driver = { |
| 263 | .driver = { |
| 264 | .name = "ad7418", |
| 265 | }, |
| 266 | .probe = ad7418_probe, |
Axel Lin | 991763a | 2014-07-01 22:39:08 +0800 | [diff] [blame] | 267 | .id_table = ad7418_id, |
| 268 | }; |
| 269 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 270 | module_i2c_driver(ad7418_driver); |
Alessandro Zummo | 2d8dd65 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 271 | |
| 272 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 273 | MODULE_DESCRIPTION("AD7416/17/18 driver"); |
| 274 | MODULE_LICENSE("GPL"); |
| 275 | MODULE_VERSION(DRV_VERSION); |