Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * LM73 Sensor driver |
| 3 | * Based on LM75 |
| 4 | * |
| 5 | * Copyright (C) 2007, CenoSYS (www.cenosys.com). |
| 6 | * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu). |
| 7 | * |
| 8 | * Guillaume Ligneul <guillaume.ligneul@gmail.com> |
| 9 | * Adrien Demarez <adrien.demarez@bolloretelecom.eu> |
| 10 | * Jeremy Laine <jeremy.laine@bolloretelecom.eu> |
Chris Verges | 2bf9233 | 2013-01-05 01:41:20 -0800 | [diff] [blame] | 11 | * Chris Verges <kg4ysn@gmail.com> |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 12 | * |
| 13 | * This software program is licensed subject to the GNU General Public License |
| 14 | * (GPL).Version 2,June 1991, available at |
| 15 | * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 20 | #include <linux/i2c.h> |
| 21 | #include <linux/hwmon.h> |
| 22 | #include <linux/hwmon-sysfs.h> |
| 23 | #include <linux/err.h> |
| 24 | |
| 25 | |
| 26 | /* Addresses scanned */ |
| 27 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, |
| 28 | 0x4d, 0x4e, I2C_CLIENT_END }; |
| 29 | |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 30 | /* LM73 registers */ |
| 31 | #define LM73_REG_INPUT 0x00 |
| 32 | #define LM73_REG_CONF 0x01 |
| 33 | #define LM73_REG_MAX 0x02 |
| 34 | #define LM73_REG_MIN 0x03 |
| 35 | #define LM73_REG_CTRL 0x04 |
| 36 | #define LM73_REG_ID 0x07 |
| 37 | |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 38 | #define LM73_ID 0x9001 /* 0x0190, byte-swapped */ |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 39 | #define DRVNAME "lm73" |
Guenter Roeck | 91bba68 | 2013-01-10 21:16:47 -0800 | [diff] [blame] | 40 | #define LM73_TEMP_MIN (-256000 / 250) |
| 41 | #define LM73_TEMP_MAX (255750 / 250) |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 42 | |
Chris Verges | 8c14d12 | 2013-01-05 01:41:19 -0800 | [diff] [blame] | 43 | #define LM73_CTRL_RES_SHIFT 5 |
| 44 | #define LM73_CTRL_RES_MASK (BIT(5) | BIT(6)) |
| 45 | #define LM73_CTRL_TO_MASK BIT(7) |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 46 | |
Chris Verges | 2bf9233 | 2013-01-05 01:41:20 -0800 | [diff] [blame] | 47 | #define LM73_CTRL_HI_SHIFT 2 |
| 48 | #define LM73_CTRL_LO_SHIFT 1 |
| 49 | |
Chris Verges | 8c14d12 | 2013-01-05 01:41:19 -0800 | [diff] [blame] | 50 | static const unsigned short lm73_convrates[] = { |
| 51 | 14, /* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */ |
| 52 | 28, /* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */ |
| 53 | 56, /* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */ |
| 54 | 112, /* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */ |
| 55 | }; |
| 56 | |
| 57 | struct lm73_data { |
| 58 | struct device *hwmon_dev; |
| 59 | struct mutex lock; |
| 60 | u8 ctrl; /* control register value */ |
| 61 | }; |
| 62 | |
| 63 | /*-----------------------------------------------------------------------*/ |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 64 | |
| 65 | static ssize_t set_temp(struct device *dev, struct device_attribute *da, |
| 66 | const char *buf, size_t count) |
| 67 | { |
| 68 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 69 | struct i2c_client *client = to_i2c_client(dev); |
| 70 | long temp; |
| 71 | short value; |
Chris Verges | 0602934 | 2012-12-21 01:58:34 -0800 | [diff] [blame] | 72 | s32 err; |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 73 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 74 | int status = kstrtol(buf, 10, &temp); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 75 | if (status < 0) |
| 76 | return status; |
| 77 | |
| 78 | /* Write value */ |
Guenter Roeck | 91bba68 | 2013-01-10 21:16:47 -0800 | [diff] [blame] | 79 | value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5; |
Chris Verges | 0602934 | 2012-12-21 01:58:34 -0800 | [diff] [blame] | 80 | err = i2c_smbus_write_word_swapped(client, attr->index, value); |
| 81 | return (err < 0) ? err : count; |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, |
| 85 | char *buf) |
| 86 | { |
| 87 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 88 | struct i2c_client *client = to_i2c_client(dev); |
Chris Verges | 0602934 | 2012-12-21 01:58:34 -0800 | [diff] [blame] | 89 | int temp; |
| 90 | |
| 91 | s32 err = i2c_smbus_read_word_swapped(client, attr->index); |
| 92 | if (err < 0) |
| 93 | return err; |
| 94 | |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 95 | /* use integer division instead of equivalent right shift to |
| 96 | guarantee arithmetic shift and preserve the sign */ |
Chris Verges | 0602934 | 2012-12-21 01:58:34 -0800 | [diff] [blame] | 97 | temp = (((s16) err) * 250) / 32; |
| 98 | return scnprintf(buf, PAGE_SIZE, "%d\n", temp); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Chris Verges | 8c14d12 | 2013-01-05 01:41:19 -0800 | [diff] [blame] | 101 | static ssize_t set_convrate(struct device *dev, struct device_attribute *da, |
| 102 | const char *buf, size_t count) |
| 103 | { |
| 104 | struct i2c_client *client = to_i2c_client(dev); |
| 105 | struct lm73_data *data = i2c_get_clientdata(client); |
| 106 | unsigned long convrate; |
| 107 | s32 err; |
| 108 | int res = 0; |
| 109 | |
| 110 | err = kstrtoul(buf, 10, &convrate); |
| 111 | if (err < 0) |
| 112 | return err; |
| 113 | |
| 114 | /* |
| 115 | * Convert the desired conversion rate into register bits. |
| 116 | * res is already initialized, and everything past the second-to-last |
| 117 | * value in the array is treated as belonging to the last value |
| 118 | * in the array. |
| 119 | */ |
| 120 | while (res < (ARRAY_SIZE(lm73_convrates) - 1) && |
| 121 | convrate > lm73_convrates[res]) |
| 122 | res++; |
| 123 | |
| 124 | mutex_lock(&data->lock); |
| 125 | data->ctrl &= LM73_CTRL_TO_MASK; |
| 126 | data->ctrl |= res << LM73_CTRL_RES_SHIFT; |
| 127 | err = i2c_smbus_write_byte_data(client, LM73_REG_CTRL, data->ctrl); |
| 128 | mutex_unlock(&data->lock); |
| 129 | |
| 130 | if (err < 0) |
| 131 | return err; |
| 132 | |
| 133 | return count; |
| 134 | } |
| 135 | |
| 136 | static ssize_t show_convrate(struct device *dev, struct device_attribute *da, |
| 137 | char *buf) |
| 138 | { |
| 139 | struct i2c_client *client = to_i2c_client(dev); |
| 140 | struct lm73_data *data = i2c_get_clientdata(client); |
| 141 | int res; |
| 142 | |
| 143 | res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT; |
| 144 | return scnprintf(buf, PAGE_SIZE, "%hu\n", lm73_convrates[res]); |
| 145 | } |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 146 | |
Chris Verges | 2bf9233 | 2013-01-05 01:41:20 -0800 | [diff] [blame] | 147 | static ssize_t show_maxmin_alarm(struct device *dev, |
| 148 | struct device_attribute *da, char *buf) |
| 149 | { |
| 150 | struct i2c_client *client = to_i2c_client(dev); |
| 151 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 152 | struct lm73_data *data = i2c_get_clientdata(client); |
| 153 | s32 ctrl; |
| 154 | |
| 155 | mutex_lock(&data->lock); |
| 156 | ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL); |
| 157 | if (ctrl < 0) |
| 158 | goto abort; |
| 159 | data->ctrl = ctrl; |
| 160 | mutex_unlock(&data->lock); |
| 161 | |
| 162 | return scnprintf(buf, PAGE_SIZE, "%d\n", (ctrl >> attr->index) & 1); |
| 163 | |
| 164 | abort: |
| 165 | mutex_unlock(&data->lock); |
| 166 | return ctrl; |
| 167 | } |
| 168 | |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 169 | /*-----------------------------------------------------------------------*/ |
| 170 | |
| 171 | /* sysfs attributes for hwmon */ |
| 172 | |
| 173 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 174 | show_temp, set_temp, LM73_REG_MAX); |
| 175 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, |
| 176 | show_temp, set_temp, LM73_REG_MIN); |
| 177 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, |
| 178 | show_temp, NULL, LM73_REG_INPUT); |
Chris Verges | 8c14d12 | 2013-01-05 01:41:19 -0800 | [diff] [blame] | 179 | static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, |
| 180 | show_convrate, set_convrate, 0); |
Chris Verges | 2bf9233 | 2013-01-05 01:41:20 -0800 | [diff] [blame] | 181 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, |
| 182 | show_maxmin_alarm, NULL, LM73_CTRL_HI_SHIFT); |
| 183 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, |
| 184 | show_maxmin_alarm, NULL, LM73_CTRL_LO_SHIFT); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 185 | |
| 186 | static struct attribute *lm73_attributes[] = { |
| 187 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 188 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 189 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
Chris Verges | 8c14d12 | 2013-01-05 01:41:19 -0800 | [diff] [blame] | 190 | &sensor_dev_attr_update_interval.dev_attr.attr, |
Chris Verges | 2bf9233 | 2013-01-05 01:41:20 -0800 | [diff] [blame] | 191 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 192 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 193 | NULL |
| 194 | }; |
| 195 | |
| 196 | static const struct attribute_group lm73_group = { |
| 197 | .attrs = lm73_attributes, |
| 198 | }; |
| 199 | |
| 200 | /*-----------------------------------------------------------------------*/ |
| 201 | |
| 202 | /* device probe and removal */ |
| 203 | |
| 204 | static int |
| 205 | lm73_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 206 | { |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 207 | int status; |
Chris Verges | 8c14d12 | 2013-01-05 01:41:19 -0800 | [diff] [blame] | 208 | struct lm73_data *data; |
| 209 | int ctrl; |
| 210 | |
| 211 | data = devm_kzalloc(&client->dev, sizeof(struct lm73_data), |
| 212 | GFP_KERNEL); |
| 213 | if (!data) |
| 214 | return -ENOMEM; |
| 215 | |
| 216 | i2c_set_clientdata(client, data); |
| 217 | mutex_init(&data->lock); |
| 218 | |
| 219 | ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL); |
| 220 | if (ctrl < 0) |
| 221 | return ctrl; |
| 222 | data->ctrl = ctrl; |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 223 | |
| 224 | /* Register sysfs hooks */ |
| 225 | status = sysfs_create_group(&client->dev.kobj, &lm73_group); |
| 226 | if (status) |
| 227 | return status; |
| 228 | |
Chris Verges | 8c14d12 | 2013-01-05 01:41:19 -0800 | [diff] [blame] | 229 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 230 | if (IS_ERR(data->hwmon_dev)) { |
| 231 | status = PTR_ERR(data->hwmon_dev); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 232 | goto exit_remove; |
| 233 | } |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 234 | |
| 235 | dev_info(&client->dev, "%s: sensor '%s'\n", |
Chris Verges | 8c14d12 | 2013-01-05 01:41:19 -0800 | [diff] [blame] | 236 | dev_name(data->hwmon_dev), client->name); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 237 | |
| 238 | return 0; |
| 239 | |
| 240 | exit_remove: |
| 241 | sysfs_remove_group(&client->dev.kobj, &lm73_group); |
| 242 | return status; |
| 243 | } |
| 244 | |
| 245 | static int lm73_remove(struct i2c_client *client) |
| 246 | { |
Chris Verges | 8c14d12 | 2013-01-05 01:41:19 -0800 | [diff] [blame] | 247 | struct lm73_data *data = i2c_get_clientdata(client); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 248 | |
Chris Verges | 8c14d12 | 2013-01-05 01:41:19 -0800 | [diff] [blame] | 249 | hwmon_device_unregister(data->hwmon_dev); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 250 | sysfs_remove_group(&client->dev.kobj, &lm73_group); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static const struct i2c_device_id lm73_ids[] = { |
Jean Delvare | 1f86df4 | 2009-12-14 21:17:26 +0100 | [diff] [blame] | 255 | { "lm73", 0 }, |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 256 | { /* LIST END */ } |
| 257 | }; |
| 258 | MODULE_DEVICE_TABLE(i2c, lm73_ids); |
| 259 | |
| 260 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 261 | static int lm73_detect(struct i2c_client *new_client, |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 262 | struct i2c_board_info *info) |
| 263 | { |
| 264 | struct i2c_adapter *adapter = new_client->adapter; |
Jean Delvare | 24d6e2a | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 265 | int id, ctrl, conf; |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 266 | |
| 267 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 268 | I2C_FUNC_SMBUS_WORD_DATA)) |
| 269 | return -ENODEV; |
| 270 | |
Jean Delvare | 24d6e2a | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 271 | /* |
| 272 | * Do as much detection as possible with byte reads first, as word |
| 273 | * reads can confuse other devices. |
| 274 | */ |
| 275 | ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL); |
| 276 | if (ctrl < 0 || (ctrl & 0x10)) |
| 277 | return -ENODEV; |
| 278 | |
| 279 | conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF); |
| 280 | if (conf < 0 || (conf & 0x0c)) |
| 281 | return -ENODEV; |
| 282 | |
| 283 | id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID); |
| 284 | if (id < 0 || id != (LM73_ID & 0xff)) |
| 285 | return -ENODEV; |
| 286 | |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 287 | /* Check device ID */ |
| 288 | id = i2c_smbus_read_word_data(new_client, LM73_REG_ID); |
Jean Delvare | 24d6e2a | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 289 | if (id < 0 || id != LM73_ID) |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 290 | return -ENODEV; |
| 291 | |
| 292 | strlcpy(info->type, "lm73", I2C_NAME_SIZE); |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static struct i2c_driver lm73_driver = { |
| 298 | .class = I2C_CLASS_HWMON, |
| 299 | .driver = { |
| 300 | .name = "lm73", |
| 301 | }, |
| 302 | .probe = lm73_probe, |
| 303 | .remove = lm73_remove, |
| 304 | .id_table = lm73_ids, |
| 305 | .detect = lm73_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 306 | .address_list = normal_i2c, |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 307 | }; |
| 308 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 309 | module_i2c_driver(lm73_driver); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 310 | |
| 311 | MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>"); |
| 312 | MODULE_DESCRIPTION("LM73 driver"); |
| 313 | MODULE_LICENSE("GPL"); |