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