Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 2 | * lm77.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
| 4 | * |
| 5 | * Copyright (c) 2004 Andras BALI <drewie@freemail.hu> |
| 6 | * |
| 7 | * Heavily based on lm75.c by Frodo Looijaard <frodol@dds.nl>. The LM77 |
| 8 | * is a temperature sensor and thermal window comparator with 0.5 deg |
| 9 | * resolution made by National Semiconductor. Complete datasheet can be |
| 10 | * obtained at their site: |
| 11 | * http://www.national.com/pf/LM/LM77.html |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 22 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/jiffies.h> |
| 28 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 29 | #include <linux/hwmon.h> |
Jean Delvare | 1f52af0 | 2008-01-03 23:35:33 +0100 | [diff] [blame] | 30 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 31 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 35 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, |
| 36 | I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* The LM77 registers */ |
| 39 | #define LM77_REG_TEMP 0x00 |
| 40 | #define LM77_REG_CONF 0x01 |
| 41 | #define LM77_REG_TEMP_HYST 0x02 |
| 42 | #define LM77_REG_TEMP_CRIT 0x03 |
| 43 | #define LM77_REG_TEMP_MIN 0x04 |
| 44 | #define LM77_REG_TEMP_MAX 0x05 |
| 45 | |
| 46 | /* Each client has this additional data */ |
| 47 | struct lm77_data { |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 48 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 49 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | char valid; |
| 51 | unsigned long last_updated; /* In jiffies */ |
| 52 | int temp_input; /* Temperatures */ |
| 53 | int temp_crit; |
| 54 | int temp_min; |
| 55 | int temp_max; |
| 56 | int temp_hyst; |
| 57 | u8 alarms; |
| 58 | }; |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* straight from the datasheet */ |
| 61 | #define LM77_TEMP_MIN (-55000) |
| 62 | #define LM77_TEMP_MAX 125000 |
| 63 | |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 64 | /* |
| 65 | * In the temperature registers, the low 3 bits are not part of the |
| 66 | * temperature values; they are the status bits. |
| 67 | */ |
Jean Delvare | 413b645 | 2006-01-09 22:43:08 +0100 | [diff] [blame] | 68 | static inline s16 LM77_TEMP_TO_REG(int temp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 70 | int ntemp = clamp_val(temp, LM77_TEMP_MIN, LM77_TEMP_MAX); |
Jean Delvare | 413b645 | 2006-01-09 22:43:08 +0100 | [diff] [blame] | 71 | return (ntemp / 500) * 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Jean Delvare | 413b645 | 2006-01-09 22:43:08 +0100 | [diff] [blame] | 74 | static inline int LM77_TEMP_FROM_REG(s16 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Jean Delvare | 413b645 | 2006-01-09 22:43:08 +0100 | [diff] [blame] | 76 | return (reg / 8) * 500; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Guenter Roeck | 9f9edcd | 2014-04-12 08:45:20 -0700 | [diff] [blame] | 79 | /* |
| 80 | * All registers are word-sized, except for the configuration register. |
| 81 | * The LM77 uses the high-byte first convention. |
| 82 | */ |
| 83 | static u16 lm77_read_value(struct i2c_client *client, u8 reg) |
| 84 | { |
| 85 | if (reg == LM77_REG_CONF) |
| 86 | return i2c_smbus_read_byte_data(client, reg); |
| 87 | else |
| 88 | return i2c_smbus_read_word_swapped(client, reg); |
| 89 | } |
| 90 | |
| 91 | static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value) |
| 92 | { |
| 93 | if (reg == LM77_REG_CONF) |
| 94 | return i2c_smbus_write_byte_data(client, reg, value); |
| 95 | else |
| 96 | return i2c_smbus_write_word_swapped(client, reg, value); |
| 97 | } |
| 98 | |
| 99 | static struct lm77_data *lm77_update_device(struct device *dev) |
| 100 | { |
| 101 | struct i2c_client *client = to_i2c_client(dev); |
| 102 | struct lm77_data *data = i2c_get_clientdata(client); |
| 103 | |
| 104 | mutex_lock(&data->update_lock); |
| 105 | |
| 106 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 107 | || !data->valid) { |
| 108 | dev_dbg(&client->dev, "Starting lm77 update\n"); |
| 109 | data->temp_input = |
| 110 | LM77_TEMP_FROM_REG(lm77_read_value(client, |
| 111 | LM77_REG_TEMP)); |
| 112 | data->temp_hyst = |
| 113 | LM77_TEMP_FROM_REG(lm77_read_value(client, |
| 114 | LM77_REG_TEMP_HYST)); |
| 115 | data->temp_crit = |
| 116 | LM77_TEMP_FROM_REG(lm77_read_value(client, |
| 117 | LM77_REG_TEMP_CRIT)); |
| 118 | data->temp_min = |
| 119 | LM77_TEMP_FROM_REG(lm77_read_value(client, |
| 120 | LM77_REG_TEMP_MIN)); |
| 121 | data->temp_max = |
| 122 | LM77_TEMP_FROM_REG(lm77_read_value(client, |
| 123 | LM77_REG_TEMP_MAX)); |
| 124 | data->alarms = |
| 125 | lm77_read_value(client, LM77_REG_TEMP) & 0x0007; |
| 126 | data->last_updated = jiffies; |
| 127 | data->valid = 1; |
| 128 | } |
| 129 | |
| 130 | mutex_unlock(&data->update_lock); |
| 131 | |
| 132 | return data; |
| 133 | } |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | /* sysfs stuff */ |
| 136 | |
| 137 | /* read routines for temperature limits */ |
| 138 | #define show(value) \ |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 139 | static ssize_t show_##value(struct device *dev, \ |
| 140 | struct device_attribute *attr, \ |
| 141 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { \ |
| 143 | struct lm77_data *data = lm77_update_device(dev); \ |
| 144 | return sprintf(buf, "%d\n", data->value); \ |
| 145 | } |
| 146 | |
| 147 | show(temp_input); |
| 148 | show(temp_crit); |
| 149 | show(temp_min); |
| 150 | show(temp_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | /* read routines for hysteresis values */ |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 153 | static ssize_t show_temp_crit_hyst(struct device *dev, |
| 154 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
| 156 | struct lm77_data *data = lm77_update_device(dev); |
| 157 | return sprintf(buf, "%d\n", data->temp_crit - data->temp_hyst); |
| 158 | } |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 159 | static ssize_t show_temp_min_hyst(struct device *dev, |
| 160 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | struct lm77_data *data = lm77_update_device(dev); |
| 163 | return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst); |
| 164 | } |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 165 | static ssize_t show_temp_max_hyst(struct device *dev, |
| 166 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
| 168 | struct lm77_data *data = lm77_update_device(dev); |
| 169 | return sprintf(buf, "%d\n", data->temp_max - data->temp_hyst); |
| 170 | } |
| 171 | |
| 172 | /* write routines */ |
| 173 | #define set(value, reg) \ |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 174 | static ssize_t set_##value(struct device *dev, struct device_attribute *attr, \ |
| 175 | const char *buf, size_t count) \ |
| 176 | { \ |
| 177 | struct i2c_client *client = to_i2c_client(dev); \ |
| 178 | struct lm77_data *data = i2c_get_clientdata(client); \ |
| 179 | long val; \ |
| 180 | int err = kstrtol(buf, 10, &val); \ |
| 181 | if (err) \ |
| 182 | return err; \ |
| 183 | \ |
| 184 | mutex_lock(&data->update_lock); \ |
| 185 | data->value = val; \ |
| 186 | lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value)); \ |
| 187 | mutex_unlock(&data->update_lock); \ |
| 188 | return count; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | set(temp_min, LM77_REG_TEMP_MIN); |
| 192 | set(temp_max, LM77_REG_TEMP_MAX); |
| 193 | |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 194 | /* |
| 195 | * hysteresis is stored as a relative value on the chip, so it has to be |
| 196 | * converted first |
| 197 | */ |
| 198 | static ssize_t set_temp_crit_hyst(struct device *dev, |
| 199 | struct device_attribute *attr, |
| 200 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
| 202 | struct i2c_client *client = to_i2c_client(dev); |
| 203 | struct lm77_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 204 | unsigned long val; |
| 205 | int err; |
| 206 | |
| 207 | err = kstrtoul(buf, 10, &val); |
| 208 | if (err) |
| 209 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 211 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | data->temp_hyst = data->temp_crit - val; |
| 213 | lm77_write_value(client, LM77_REG_TEMP_HYST, |
| 214 | LM77_TEMP_TO_REG(data->temp_hyst)); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 215 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return count; |
| 217 | } |
| 218 | |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 219 | static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr, |
| 220 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | struct i2c_client *client = to_i2c_client(dev); |
| 223 | struct lm77_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 224 | unsigned long val; |
| 225 | int err; |
| 226 | |
| 227 | err = kstrtoul(buf, 10, &val); |
| 228 | if (err) |
| 229 | return err; |
| 230 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 231 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | data->temp_crit = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | lm77_write_value(client, LM77_REG_TEMP_CRIT, |
| 234 | LM77_TEMP_TO_REG(data->temp_crit)); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 235 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return count; |
| 237 | } |
| 238 | |
Jean Delvare | 1f52af0 | 2008-01-03 23:35:33 +0100 | [diff] [blame] | 239 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 240 | char *buf) |
| 241 | { |
| 242 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 243 | struct lm77_data *data = lm77_update_device(dev); |
| 244 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 245 | } |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | static DEVICE_ATTR(temp1_input, S_IRUGO, |
| 248 | show_temp_input, NULL); |
| 249 | static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, |
| 250 | show_temp_crit, set_temp_crit); |
| 251 | static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, |
| 252 | show_temp_min, set_temp_min); |
| 253 | static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 254 | show_temp_max, set_temp_max); |
| 255 | |
| 256 | static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, |
| 257 | show_temp_crit_hyst, set_temp_crit_hyst); |
| 258 | static DEVICE_ATTR(temp1_min_hyst, S_IRUGO, |
| 259 | show_temp_min_hyst, NULL); |
| 260 | static DEVICE_ATTR(temp1_max_hyst, S_IRUGO, |
| 261 | show_temp_max_hyst, NULL); |
| 262 | |
Jean Delvare | 1f52af0 | 2008-01-03 23:35:33 +0100 | [diff] [blame] | 263 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 264 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 265 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Mark M. Hoffman | 0501a381 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 267 | static struct attribute *lm77_attributes[] = { |
| 268 | &dev_attr_temp1_input.attr, |
| 269 | &dev_attr_temp1_crit.attr, |
| 270 | &dev_attr_temp1_min.attr, |
| 271 | &dev_attr_temp1_max.attr, |
| 272 | &dev_attr_temp1_crit_hyst.attr, |
| 273 | &dev_attr_temp1_min_hyst.attr, |
| 274 | &dev_attr_temp1_max_hyst.attr, |
Jean Delvare | 1f52af0 | 2008-01-03 23:35:33 +0100 | [diff] [blame] | 275 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 276 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 277 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a381 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 278 | NULL |
| 279 | }; |
| 280 | |
| 281 | static const struct attribute_group lm77_group = { |
| 282 | .attrs = lm77_attributes, |
| 283 | }; |
| 284 | |
Jean Delvare | a189dd6 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 285 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 286 | static int lm77_detect(struct i2c_client *client, struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 288 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 747d9be | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 289 | int i, cur, conf, hyst, crit, min, max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 292 | I2C_FUNC_SMBUS_WORD_DATA)) |
Jean Delvare | a189dd6 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 293 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 295 | /* |
| 296 | * Here comes the remaining detection. Since the LM77 has no |
| 297 | * register dedicated to identification, we have to rely on the |
| 298 | * following tricks: |
| 299 | * |
| 300 | * 1. the high 4 bits represent the sign and thus they should |
| 301 | * always be the same |
| 302 | * 2. the high 3 bits are unused in the configuration register |
| 303 | * 3. addresses 0x06 and 0x07 return the last read value |
| 304 | * 4. registers cycling over 8-address boundaries |
| 305 | * |
| 306 | * Word-sized registers are high-byte first. |
| 307 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Jean Delvare | 747d9be | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 309 | /* addresses cycling */ |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 310 | cur = i2c_smbus_read_word_data(client, 0); |
| 311 | conf = i2c_smbus_read_byte_data(client, 1); |
| 312 | hyst = i2c_smbus_read_word_data(client, 2); |
| 313 | crit = i2c_smbus_read_word_data(client, 3); |
| 314 | min = i2c_smbus_read_word_data(client, 4); |
| 315 | max = i2c_smbus_read_word_data(client, 5); |
Jean Delvare | 747d9be | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 316 | for (i = 8; i <= 0xff; i += 8) { |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 317 | if (i2c_smbus_read_byte_data(client, i + 1) != conf |
| 318 | || i2c_smbus_read_word_data(client, i + 2) != hyst |
| 319 | || i2c_smbus_read_word_data(client, i + 3) != crit |
| 320 | || i2c_smbus_read_word_data(client, i + 4) != min |
| 321 | || i2c_smbus_read_word_data(client, i + 5) != max) |
Jean Delvare | a189dd6 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 322 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Jean Delvare | 747d9be | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 325 | /* sign bits */ |
| 326 | if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0) |
| 327 | || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0) |
| 328 | || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0) |
| 329 | || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0) |
| 330 | || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0)) |
| 331 | return -ENODEV; |
| 332 | |
| 333 | /* unused bits */ |
| 334 | if (conf & 0xe0) |
| 335 | return -ENODEV; |
| 336 | |
| 337 | /* 0x06 and 0x07 return the last read value */ |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 338 | cur = i2c_smbus_read_word_data(client, 0); |
| 339 | if (i2c_smbus_read_word_data(client, 6) != cur |
| 340 | || i2c_smbus_read_word_data(client, 7) != cur) |
Jean Delvare | 747d9be | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 341 | return -ENODEV; |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 342 | hyst = i2c_smbus_read_word_data(client, 2); |
| 343 | if (i2c_smbus_read_word_data(client, 6) != hyst |
| 344 | || i2c_smbus_read_word_data(client, 7) != hyst) |
Jean Delvare | 747d9be | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 345 | return -ENODEV; |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 346 | min = i2c_smbus_read_word_data(client, 4); |
| 347 | if (i2c_smbus_read_word_data(client, 6) != min |
| 348 | || i2c_smbus_read_word_data(client, 7) != min) |
Jean Delvare | 747d9be | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 349 | return -ENODEV; |
| 350 | |
Jean Delvare | a189dd6 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 351 | strlcpy(info->type, "lm77", I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Jean Delvare | a189dd6 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
Guenter Roeck | 9f9edcd | 2014-04-12 08:45:20 -0700 | [diff] [blame] | 356 | static void lm77_init_client(struct i2c_client *client) |
| 357 | { |
| 358 | /* Initialize the LM77 chip - turn off shutdown mode */ |
| 359 | int conf = lm77_read_value(client, LM77_REG_CONF); |
| 360 | if (conf & 1) |
| 361 | lm77_write_value(client, LM77_REG_CONF, conf & 0xfe); |
| 362 | } |
| 363 | |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 364 | static int lm77_probe(struct i2c_client *client, const struct i2c_device_id *id) |
Jean Delvare | a189dd6 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 365 | { |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 366 | struct device *dev = &client->dev; |
Jean Delvare | a189dd6 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 367 | struct lm77_data *data; |
| 368 | int err; |
| 369 | |
Guenter Roeck | 52714c0 | 2012-06-16 18:24:02 -0700 | [diff] [blame] | 370 | data = devm_kzalloc(dev, sizeof(struct lm77_data), GFP_KERNEL); |
| 371 | if (!data) |
| 372 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 374 | i2c_set_clientdata(client, data); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 375 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | /* Initialize the LM77 chip */ |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 378 | lm77_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | /* Register sysfs hooks */ |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 381 | err = sysfs_create_group(&dev->kobj, &lm77_group); |
Guenter Roeck | 02fe2fd | 2012-01-14 13:42:20 -0800 | [diff] [blame] | 382 | if (err) |
Guenter Roeck | 52714c0 | 2012-06-16 18:24:02 -0700 | [diff] [blame] | 383 | return err; |
Mark M. Hoffman | 0501a381 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 384 | |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 385 | data->hwmon_dev = hwmon_device_register(dev); |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 386 | if (IS_ERR(data->hwmon_dev)) { |
| 387 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | 0501a381 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 388 | goto exit_remove; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | return 0; |
| 392 | |
Mark M. Hoffman | 0501a381 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 393 | exit_remove: |
Guenter Roeck | efd2d11 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 394 | sysfs_remove_group(&dev->kobj, &lm77_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return err; |
| 396 | } |
| 397 | |
Jean Delvare | a189dd6 | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 398 | static int lm77_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 400 | struct lm77_data *data = i2c_get_clientdata(client); |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 401 | hwmon_device_unregister(data->hwmon_dev); |
Mark M. Hoffman | 0501a381 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 402 | sysfs_remove_group(&client->dev.kobj, &lm77_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
Guenter Roeck | 9f9edcd | 2014-04-12 08:45:20 -0700 | [diff] [blame] | 406 | static const struct i2c_device_id lm77_id[] = { |
| 407 | { "lm77", 0 }, |
| 408 | { } |
| 409 | }; |
| 410 | MODULE_DEVICE_TABLE(i2c, lm77_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Guenter Roeck | 9f9edcd | 2014-04-12 08:45:20 -0700 | [diff] [blame] | 412 | /* This is the driver that will be inserted */ |
| 413 | static struct i2c_driver lm77_driver = { |
| 414 | .class = I2C_CLASS_HWMON, |
| 415 | .driver = { |
| 416 | .name = "lm77", |
| 417 | }, |
| 418 | .probe = lm77_probe, |
| 419 | .remove = lm77_remove, |
| 420 | .id_table = lm77_id, |
| 421 | .detect = lm77_detect, |
| 422 | .address_list = normal_i2c, |
| 423 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 425 | module_i2c_driver(lm77_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>"); |
| 428 | MODULE_DESCRIPTION("LM77 driver"); |
| 429 | MODULE_LICENSE("GPL"); |