Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 1 | /* |
| 2 | * ds620.c - Support for temperature sensor and thermostat DS620 |
| 3 | * |
| 4 | * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de> |
| 5 | * |
| 6 | * based on ds1621.c by Christian W. Zuckschwerdt <zany@triq.net> |
| 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; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/jiffies.h> |
| 27 | #include <linux/i2c.h> |
| 28 | #include <linux/hwmon.h> |
| 29 | #include <linux/hwmon-sysfs.h> |
| 30 | #include <linux/err.h> |
| 31 | #include <linux/mutex.h> |
| 32 | #include <linux/sysfs.h> |
| 33 | #include <linux/i2c/ds620.h> |
| 34 | |
| 35 | /* |
| 36 | * Many DS620 constants specified below |
| 37 | * 15 14 13 12 11 10 09 08 |
| 38 | * |Done|NVB |THF |TLF |R1 |R0 |AUTOC|1SHOT| |
| 39 | * |
| 40 | * 07 06 05 04 03 02 01 00 |
| 41 | * |PO2 |PO1 |A2 |A1 |A0 | | | | |
| 42 | */ |
| 43 | #define DS620_REG_CONFIG_DONE 0x8000 |
| 44 | #define DS620_REG_CONFIG_NVB 0x4000 |
| 45 | #define DS620_REG_CONFIG_THF 0x2000 |
| 46 | #define DS620_REG_CONFIG_TLF 0x1000 |
| 47 | #define DS620_REG_CONFIG_R1 0x0800 |
| 48 | #define DS620_REG_CONFIG_R0 0x0400 |
| 49 | #define DS620_REG_CONFIG_AUTOC 0x0200 |
| 50 | #define DS620_REG_CONFIG_1SHOT 0x0100 |
| 51 | #define DS620_REG_CONFIG_PO2 0x0080 |
| 52 | #define DS620_REG_CONFIG_PO1 0x0040 |
| 53 | #define DS620_REG_CONFIG_A2 0x0020 |
| 54 | #define DS620_REG_CONFIG_A1 0x0010 |
| 55 | #define DS620_REG_CONFIG_A0 0x0008 |
| 56 | |
| 57 | /* The DS620 registers */ |
| 58 | static const u8 DS620_REG_TEMP[3] = { |
| 59 | 0xAA, /* input, word, RO */ |
| 60 | 0xA2, /* min, word, RW */ |
| 61 | 0xA0, /* max, word, RW */ |
| 62 | }; |
| 63 | |
| 64 | #define DS620_REG_CONF 0xAC /* word, RW */ |
| 65 | #define DS620_COM_START 0x51 /* no data */ |
| 66 | #define DS620_COM_STOP 0x22 /* no data */ |
| 67 | |
| 68 | /* Each client has this additional data */ |
| 69 | struct ds620_data { |
| 70 | struct device *hwmon_dev; |
| 71 | struct mutex update_lock; |
| 72 | char valid; /* !=0 if following fields are valid */ |
| 73 | unsigned long last_updated; /* In jiffies */ |
| 74 | |
Roland Stigge | cc41d58 | 2011-09-21 13:06:11 -0400 | [diff] [blame] | 75 | s16 temp[3]; /* Register values, word */ |
Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 78 | static void ds620_init_client(struct i2c_client *client) |
| 79 | { |
| 80 | struct ds620_platform_data *ds620_info = client->dev.platform_data; |
| 81 | u16 conf, new_conf; |
| 82 | |
| 83 | new_conf = conf = |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 84 | i2c_smbus_read_word_swapped(client, DS620_REG_CONF); |
Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 85 | |
| 86 | /* switch to continuous conversion mode */ |
| 87 | new_conf &= ~DS620_REG_CONFIG_1SHOT; |
| 88 | /* already high at power-on, but don't trust the BIOS! */ |
| 89 | new_conf |= DS620_REG_CONFIG_PO2; |
| 90 | /* thermostat mode according to platform data */ |
| 91 | if (ds620_info && ds620_info->pomode == 1) |
| 92 | new_conf &= ~DS620_REG_CONFIG_PO1; /* PO_LOW */ |
| 93 | else if (ds620_info && ds620_info->pomode == 2) |
| 94 | new_conf |= DS620_REG_CONFIG_PO1; /* PO_HIGH */ |
| 95 | else |
| 96 | new_conf &= ~DS620_REG_CONFIG_PO2; /* always low */ |
| 97 | /* with highest precision */ |
| 98 | new_conf |= DS620_REG_CONFIG_R1 | DS620_REG_CONFIG_R0; |
| 99 | |
| 100 | if (conf != new_conf) |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 101 | i2c_smbus_write_word_swapped(client, DS620_REG_CONF, new_conf); |
Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 102 | |
| 103 | /* start conversion */ |
| 104 | i2c_smbus_write_byte(client, DS620_COM_START); |
| 105 | } |
| 106 | |
| 107 | static struct ds620_data *ds620_update_client(struct device *dev) |
| 108 | { |
| 109 | struct i2c_client *client = to_i2c_client(dev); |
| 110 | struct ds620_data *data = i2c_get_clientdata(client); |
| 111 | struct ds620_data *ret = data; |
| 112 | |
| 113 | mutex_lock(&data->update_lock); |
| 114 | |
| 115 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 116 | || !data->valid) { |
| 117 | int i; |
| 118 | int res; |
| 119 | |
| 120 | dev_dbg(&client->dev, "Starting ds620 update\n"); |
| 121 | |
| 122 | for (i = 0; i < ARRAY_SIZE(data->temp); i++) { |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 123 | res = i2c_smbus_read_word_swapped(client, |
| 124 | DS620_REG_TEMP[i]); |
Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 125 | if (res < 0) { |
| 126 | ret = ERR_PTR(res); |
| 127 | goto abort; |
| 128 | } |
| 129 | |
| 130 | data->temp[i] = res; |
| 131 | } |
| 132 | |
| 133 | data->last_updated = jiffies; |
| 134 | data->valid = 1; |
| 135 | } |
| 136 | abort: |
| 137 | mutex_unlock(&data->update_lock); |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, |
| 143 | char *buf) |
| 144 | { |
| 145 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 146 | struct ds620_data *data = ds620_update_client(dev); |
| 147 | |
| 148 | if (IS_ERR(data)) |
| 149 | return PTR_ERR(data); |
| 150 | |
| 151 | return sprintf(buf, "%d\n", ((data->temp[attr->index] / 8) * 625) / 10); |
| 152 | } |
| 153 | |
| 154 | static ssize_t set_temp(struct device *dev, struct device_attribute *da, |
| 155 | const char *buf, size_t count) |
| 156 | { |
| 157 | int res; |
| 158 | long val; |
| 159 | |
| 160 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 161 | struct i2c_client *client = to_i2c_client(dev); |
| 162 | struct ds620_data *data = i2c_get_clientdata(client); |
| 163 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 164 | res = kstrtol(buf, 10, &val); |
Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 165 | |
| 166 | if (res) |
| 167 | return res; |
| 168 | |
| 169 | val = (val * 10 / 625) * 8; |
| 170 | |
| 171 | mutex_lock(&data->update_lock); |
| 172 | data->temp[attr->index] = val; |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 173 | i2c_smbus_write_word_swapped(client, DS620_REG_TEMP[attr->index], |
| 174 | data->temp[attr->index]); |
Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 175 | mutex_unlock(&data->update_lock); |
| 176 | return count; |
| 177 | } |
| 178 | |
| 179 | static ssize_t show_alarm(struct device *dev, struct device_attribute *da, |
| 180 | char *buf) |
| 181 | { |
| 182 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 183 | struct ds620_data *data = ds620_update_client(dev); |
| 184 | struct i2c_client *client = to_i2c_client(dev); |
| 185 | u16 conf, new_conf; |
| 186 | int res; |
| 187 | |
| 188 | if (IS_ERR(data)) |
| 189 | return PTR_ERR(data); |
| 190 | |
| 191 | /* reset alarms if necessary */ |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 192 | res = i2c_smbus_read_word_swapped(client, DS620_REG_CONF); |
Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 193 | if (res < 0) |
| 194 | return res; |
| 195 | |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 196 | new_conf = conf = res; |
Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 197 | new_conf &= ~attr->index; |
| 198 | if (conf != new_conf) { |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 199 | res = i2c_smbus_write_word_swapped(client, DS620_REG_CONF, |
| 200 | new_conf); |
Roland Stigge | 6099469 | 2011-01-09 09:31:39 -0500 | [diff] [blame] | 201 | if (res < 0) |
| 202 | return res; |
| 203 | } |
| 204 | |
| 205 | return sprintf(buf, "%d\n", !!(conf & attr->index)); |
| 206 | } |
| 207 | |
| 208 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
| 209 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp, 1); |
| 210 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, 2); |
| 211 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, |
| 212 | DS620_REG_CONFIG_TLF); |
| 213 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, |
| 214 | DS620_REG_CONFIG_THF); |
| 215 | |
| 216 | static struct attribute *ds620_attributes[] = { |
| 217 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 218 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 219 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 220 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 221 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 222 | NULL |
| 223 | }; |
| 224 | |
| 225 | static const struct attribute_group ds620_group = { |
| 226 | .attrs = ds620_attributes, |
| 227 | }; |
| 228 | |
| 229 | static int ds620_probe(struct i2c_client *client, |
| 230 | const struct i2c_device_id *id) |
| 231 | { |
| 232 | struct ds620_data *data; |
| 233 | int err; |
| 234 | |
| 235 | data = kzalloc(sizeof(struct ds620_data), GFP_KERNEL); |
| 236 | if (!data) { |
| 237 | err = -ENOMEM; |
| 238 | goto exit; |
| 239 | } |
| 240 | |
| 241 | i2c_set_clientdata(client, data); |
| 242 | mutex_init(&data->update_lock); |
| 243 | |
| 244 | /* Initialize the DS620 chip */ |
| 245 | ds620_init_client(client); |
| 246 | |
| 247 | /* Register sysfs hooks */ |
| 248 | err = sysfs_create_group(&client->dev.kobj, &ds620_group); |
| 249 | if (err) |
| 250 | goto exit_free; |
| 251 | |
| 252 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 253 | if (IS_ERR(data->hwmon_dev)) { |
| 254 | err = PTR_ERR(data->hwmon_dev); |
| 255 | goto exit_remove_files; |
| 256 | } |
| 257 | |
| 258 | dev_info(&client->dev, "temperature sensor found\n"); |
| 259 | |
| 260 | return 0; |
| 261 | |
| 262 | exit_remove_files: |
| 263 | sysfs_remove_group(&client->dev.kobj, &ds620_group); |
| 264 | exit_free: |
| 265 | kfree(data); |
| 266 | exit: |
| 267 | return err; |
| 268 | } |
| 269 | |
| 270 | static int ds620_remove(struct i2c_client *client) |
| 271 | { |
| 272 | struct ds620_data *data = i2c_get_clientdata(client); |
| 273 | |
| 274 | hwmon_device_unregister(data->hwmon_dev); |
| 275 | sysfs_remove_group(&client->dev.kobj, &ds620_group); |
| 276 | |
| 277 | kfree(data); |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static const struct i2c_device_id ds620_id[] = { |
| 283 | {"ds620", 0}, |
| 284 | {} |
| 285 | }; |
| 286 | |
| 287 | MODULE_DEVICE_TABLE(i2c, ds620_id); |
| 288 | |
| 289 | /* This is the driver that will be inserted */ |
| 290 | static struct i2c_driver ds620_driver = { |
| 291 | .class = I2C_CLASS_HWMON, |
| 292 | .driver = { |
| 293 | .name = "ds620", |
| 294 | }, |
| 295 | .probe = ds620_probe, |
| 296 | .remove = ds620_remove, |
| 297 | .id_table = ds620_id, |
| 298 | }; |
| 299 | |
| 300 | static int __init ds620_init(void) |
| 301 | { |
| 302 | return i2c_add_driver(&ds620_driver); |
| 303 | } |
| 304 | |
| 305 | static void __exit ds620_exit(void) |
| 306 | { |
| 307 | i2c_del_driver(&ds620_driver); |
| 308 | } |
| 309 | |
| 310 | MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); |
| 311 | MODULE_DESCRIPTION("DS620 driver"); |
| 312 | MODULE_LICENSE("GPL"); |
| 313 | |
| 314 | module_init(ds620_init); |
| 315 | module_exit(ds620_exit); |