Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 2 | /* |
Guenter Roeck | b6707b7 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 3 | * Copyright (c) 2011 David George <david.george@ska.ac.za> |
| 4 | * |
| 5 | * based on adm1021.c |
| 6 | * some credit to Christoph Scheurer, but largely a rewrite |
Guenter Roeck | b6707b7 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 7 | */ |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/jiffies.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/hwmon.h> |
| 15 | #include <linux/hwmon-sysfs.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/mutex.h> |
| 18 | |
| 19 | /* Addresses to scan */ |
Axel Lin | 28681d7 | 2014-07-18 16:02:38 +0800 | [diff] [blame] | 20 | static const unsigned short max1668_addr_list[] = { |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 21 | 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; |
| 22 | |
| 23 | /* max1668 registers */ |
| 24 | |
| 25 | #define MAX1668_REG_TEMP(nr) (nr) |
| 26 | #define MAX1668_REG_STAT1 0x05 |
| 27 | #define MAX1668_REG_STAT2 0x06 |
| 28 | #define MAX1668_REG_MAN_ID 0xfe |
| 29 | #define MAX1668_REG_DEV_ID 0xff |
| 30 | |
| 31 | /* limits */ |
| 32 | |
| 33 | /* write high limits */ |
| 34 | #define MAX1668_REG_LIMH_WR(nr) (0x13 + 2 * (nr)) |
| 35 | /* write low limits */ |
| 36 | #define MAX1668_REG_LIML_WR(nr) (0x14 + 2 * (nr)) |
| 37 | /* read high limits */ |
| 38 | #define MAX1668_REG_LIMH_RD(nr) (0x08 + 2 * (nr)) |
| 39 | /* read low limits */ |
| 40 | #define MAX1668_REG_LIML_RD(nr) (0x09 + 2 * (nr)) |
| 41 | |
| 42 | /* manufacturer and device ID Constants */ |
| 43 | #define MAN_ID_MAXIM 0x4d |
| 44 | #define DEV_ID_MAX1668 0x3 |
| 45 | #define DEV_ID_MAX1805 0x5 |
| 46 | #define DEV_ID_MAX1989 0xb |
| 47 | |
| 48 | /* read only mode module parameter */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 49 | static bool read_only; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 50 | module_param(read_only, bool, 0); |
| 51 | MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); |
| 52 | |
| 53 | enum chips { max1668, max1805, max1989 }; |
| 54 | |
| 55 | struct max1668_data { |
Guenter Roeck | 5230551 | 2014-02-15 17:50:48 -0800 | [diff] [blame] | 56 | struct i2c_client *client; |
| 57 | const struct attribute_group *groups[3]; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 58 | enum chips type; |
| 59 | |
| 60 | struct mutex update_lock; |
Paul Fertser | 952a11c | 2021-09-24 22:52:02 +0300 | [diff] [blame] | 61 | bool valid; /* true if following fields are valid */ |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 62 | unsigned long last_updated; /* In jiffies */ |
| 63 | |
| 64 | /* 1x local and 4x remote */ |
| 65 | s8 temp_max[5]; |
| 66 | s8 temp_min[5]; |
| 67 | s8 temp[5]; |
| 68 | u16 alarms; |
| 69 | }; |
| 70 | |
| 71 | static struct max1668_data *max1668_update_device(struct device *dev) |
| 72 | { |
Guenter Roeck | 5230551 | 2014-02-15 17:50:48 -0800 | [diff] [blame] | 73 | struct max1668_data *data = dev_get_drvdata(dev); |
| 74 | struct i2c_client *client = data->client; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 75 | struct max1668_data *ret = data; |
| 76 | s32 val; |
| 77 | int i; |
| 78 | |
| 79 | mutex_lock(&data->update_lock); |
| 80 | |
| 81 | if (data->valid && !time_after(jiffies, |
| 82 | data->last_updated + HZ + HZ / 2)) |
| 83 | goto abort; |
| 84 | |
| 85 | for (i = 0; i < 5; i++) { |
| 86 | val = i2c_smbus_read_byte_data(client, MAX1668_REG_TEMP(i)); |
| 87 | if (unlikely(val < 0)) { |
| 88 | ret = ERR_PTR(val); |
| 89 | goto abort; |
| 90 | } |
| 91 | data->temp[i] = (s8) val; |
| 92 | |
| 93 | val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIMH_RD(i)); |
| 94 | if (unlikely(val < 0)) { |
| 95 | ret = ERR_PTR(val); |
| 96 | goto abort; |
| 97 | } |
| 98 | data->temp_max[i] = (s8) val; |
| 99 | |
| 100 | val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIML_RD(i)); |
| 101 | if (unlikely(val < 0)) { |
| 102 | ret = ERR_PTR(val); |
| 103 | goto abort; |
| 104 | } |
| 105 | data->temp_min[i] = (s8) val; |
| 106 | } |
| 107 | |
| 108 | val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT1); |
| 109 | if (unlikely(val < 0)) { |
| 110 | ret = ERR_PTR(val); |
| 111 | goto abort; |
| 112 | } |
Guenter Roeck | dabaa0d | 2011-06-09 18:05:16 -0700 | [diff] [blame] | 113 | data->alarms = val << 8; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 114 | |
| 115 | val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT2); |
| 116 | if (unlikely(val < 0)) { |
| 117 | ret = ERR_PTR(val); |
| 118 | goto abort; |
| 119 | } |
Guenter Roeck | dabaa0d | 2011-06-09 18:05:16 -0700 | [diff] [blame] | 120 | data->alarms |= val; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 121 | |
| 122 | data->last_updated = jiffies; |
Paul Fertser | 952a11c | 2021-09-24 22:52:02 +0300 | [diff] [blame] | 123 | data->valid = true; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 124 | abort: |
| 125 | mutex_unlock(&data->update_lock); |
| 126 | |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static ssize_t show_temp(struct device *dev, |
| 131 | struct device_attribute *devattr, char *buf) |
| 132 | { |
| 133 | int index = to_sensor_dev_attr(devattr)->index; |
| 134 | struct max1668_data *data = max1668_update_device(dev); |
| 135 | |
| 136 | if (IS_ERR(data)) |
| 137 | return PTR_ERR(data); |
| 138 | |
| 139 | return sprintf(buf, "%d\n", data->temp[index] * 1000); |
| 140 | } |
| 141 | |
| 142 | static ssize_t show_temp_max(struct device *dev, |
| 143 | struct device_attribute *devattr, char *buf) |
| 144 | { |
| 145 | int index = to_sensor_dev_attr(devattr)->index; |
| 146 | struct max1668_data *data = max1668_update_device(dev); |
| 147 | |
| 148 | if (IS_ERR(data)) |
| 149 | return PTR_ERR(data); |
| 150 | |
| 151 | return sprintf(buf, "%d\n", data->temp_max[index] * 1000); |
| 152 | } |
| 153 | |
| 154 | static ssize_t show_temp_min(struct device *dev, |
| 155 | struct device_attribute *devattr, char *buf) |
| 156 | { |
| 157 | int index = to_sensor_dev_attr(devattr)->index; |
| 158 | struct max1668_data *data = max1668_update_device(dev); |
| 159 | |
| 160 | if (IS_ERR(data)) |
| 161 | return PTR_ERR(data); |
| 162 | |
| 163 | return sprintf(buf, "%d\n", data->temp_min[index] * 1000); |
| 164 | } |
| 165 | |
| 166 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 167 | char *buf) |
| 168 | { |
| 169 | int index = to_sensor_dev_attr(attr)->index; |
| 170 | struct max1668_data *data = max1668_update_device(dev); |
| 171 | |
| 172 | if (IS_ERR(data)) |
| 173 | return PTR_ERR(data); |
| 174 | |
| 175 | return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1); |
| 176 | } |
| 177 | |
Guenter Roeck | dabaa0d | 2011-06-09 18:05:16 -0700 | [diff] [blame] | 178 | static ssize_t show_fault(struct device *dev, |
| 179 | struct device_attribute *devattr, char *buf) |
| 180 | { |
| 181 | int index = to_sensor_dev_attr(devattr)->index; |
| 182 | struct max1668_data *data = max1668_update_device(dev); |
| 183 | |
| 184 | if (IS_ERR(data)) |
| 185 | return PTR_ERR(data); |
| 186 | |
| 187 | return sprintf(buf, "%u\n", |
| 188 | (data->alarms & (1 << 12)) && data->temp[index] == 127); |
| 189 | } |
| 190 | |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 191 | static ssize_t set_temp_max(struct device *dev, |
| 192 | struct device_attribute *devattr, |
| 193 | const char *buf, size_t count) |
| 194 | { |
| 195 | int index = to_sensor_dev_attr(devattr)->index; |
Guenter Roeck | 5230551 | 2014-02-15 17:50:48 -0800 | [diff] [blame] | 196 | struct max1668_data *data = dev_get_drvdata(dev); |
| 197 | struct i2c_client *client = data->client; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 198 | long temp; |
| 199 | int ret; |
| 200 | |
| 201 | ret = kstrtol(buf, 10, &temp); |
| 202 | if (ret < 0) |
| 203 | return ret; |
| 204 | |
| 205 | mutex_lock(&data->update_lock); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 206 | data->temp_max[index] = clamp_val(temp/1000, -128, 127); |
Guenter Roeck | c5a7066 | 2014-02-15 17:59:05 -0800 | [diff] [blame] | 207 | ret = i2c_smbus_write_byte_data(client, |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 208 | MAX1668_REG_LIMH_WR(index), |
Guenter Roeck | c5a7066 | 2014-02-15 17:59:05 -0800 | [diff] [blame] | 209 | data->temp_max[index]); |
| 210 | if (ret < 0) |
| 211 | count = ret; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 212 | mutex_unlock(&data->update_lock); |
| 213 | |
| 214 | return count; |
| 215 | } |
| 216 | |
| 217 | static ssize_t set_temp_min(struct device *dev, |
| 218 | struct device_attribute *devattr, |
| 219 | const char *buf, size_t count) |
| 220 | { |
| 221 | int index = to_sensor_dev_attr(devattr)->index; |
Guenter Roeck | 5230551 | 2014-02-15 17:50:48 -0800 | [diff] [blame] | 222 | struct max1668_data *data = dev_get_drvdata(dev); |
| 223 | struct i2c_client *client = data->client; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 224 | long temp; |
| 225 | int ret; |
| 226 | |
| 227 | ret = kstrtol(buf, 10, &temp); |
| 228 | if (ret < 0) |
| 229 | return ret; |
| 230 | |
| 231 | mutex_lock(&data->update_lock); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 232 | data->temp_min[index] = clamp_val(temp/1000, -128, 127); |
Guenter Roeck | c5a7066 | 2014-02-15 17:59:05 -0800 | [diff] [blame] | 233 | ret = i2c_smbus_write_byte_data(client, |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 234 | MAX1668_REG_LIML_WR(index), |
Guenter Roeck | c5a7066 | 2014-02-15 17:59:05 -0800 | [diff] [blame] | 235 | data->temp_min[index]); |
| 236 | if (ret < 0) |
| 237 | count = ret; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 238 | mutex_unlock(&data->update_lock); |
| 239 | |
| 240 | return count; |
| 241 | } |
| 242 | |
| 243 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
| 244 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, |
| 245 | set_temp_max, 0); |
| 246 | static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min, |
| 247 | set_temp_min, 0); |
| 248 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); |
| 249 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, |
| 250 | set_temp_max, 1); |
| 251 | static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min, |
| 252 | set_temp_min, 1); |
| 253 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); |
| 254 | static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, |
| 255 | set_temp_max, 2); |
| 256 | static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min, |
| 257 | set_temp_min, 2); |
| 258 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); |
| 259 | static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, |
| 260 | set_temp_max, 3); |
| 261 | static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min, |
| 262 | set_temp_min, 3); |
| 263 | static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4); |
| 264 | static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, |
| 265 | set_temp_max, 4); |
| 266 | static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min, |
| 267 | set_temp_min, 4); |
| 268 | |
| 269 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14); |
| 270 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13); |
| 271 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 272 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 273 | static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 274 | static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 275 | static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 276 | static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 277 | static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 278 | static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 279 | |
Guenter Roeck | dabaa0d | 2011-06-09 18:05:16 -0700 | [diff] [blame] | 280 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1); |
| 281 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2); |
| 282 | static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3); |
| 283 | static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_fault, NULL, 4); |
| 284 | |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 285 | /* Attributes common to MAX1668, MAX1989 and MAX1805 */ |
| 286 | static struct attribute *max1668_attribute_common[] = { |
| 287 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 288 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 289 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 290 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 291 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 292 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 293 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 294 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 295 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 296 | |
| 297 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 298 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 299 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 300 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 301 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
| 302 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, |
Guenter Roeck | dabaa0d | 2011-06-09 18:05:16 -0700 | [diff] [blame] | 303 | |
| 304 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
| 305 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 306 | NULL |
| 307 | }; |
| 308 | |
| 309 | /* Attributes not present on MAX1805 */ |
| 310 | static struct attribute *max1668_attribute_unique[] = { |
| 311 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 312 | &sensor_dev_attr_temp4_min.dev_attr.attr, |
| 313 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 314 | &sensor_dev_attr_temp5_max.dev_attr.attr, |
| 315 | &sensor_dev_attr_temp5_min.dev_attr.attr, |
| 316 | &sensor_dev_attr_temp5_input.dev_attr.attr, |
| 317 | |
| 318 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, |
| 319 | &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, |
| 320 | &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, |
| 321 | &sensor_dev_attr_temp5_min_alarm.dev_attr.attr, |
Guenter Roeck | dabaa0d | 2011-06-09 18:05:16 -0700 | [diff] [blame] | 322 | |
| 323 | &sensor_dev_attr_temp4_fault.dev_attr.attr, |
| 324 | &sensor_dev_attr_temp5_fault.dev_attr.attr, |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 325 | NULL |
| 326 | }; |
| 327 | |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 328 | static umode_t max1668_attribute_mode(struct kobject *kobj, |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 329 | struct attribute *attr, int index) |
| 330 | { |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 331 | umode_t ret = S_IRUGO; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 332 | if (read_only) |
| 333 | return ret; |
| 334 | if (attr == &sensor_dev_attr_temp1_max.dev_attr.attr || |
| 335 | attr == &sensor_dev_attr_temp2_max.dev_attr.attr || |
| 336 | attr == &sensor_dev_attr_temp3_max.dev_attr.attr || |
| 337 | attr == &sensor_dev_attr_temp4_max.dev_attr.attr || |
| 338 | attr == &sensor_dev_attr_temp5_max.dev_attr.attr || |
| 339 | attr == &sensor_dev_attr_temp1_min.dev_attr.attr || |
| 340 | attr == &sensor_dev_attr_temp2_min.dev_attr.attr || |
| 341 | attr == &sensor_dev_attr_temp3_min.dev_attr.attr || |
| 342 | attr == &sensor_dev_attr_temp4_min.dev_attr.attr || |
| 343 | attr == &sensor_dev_attr_temp5_min.dev_attr.attr) |
| 344 | ret |= S_IWUSR; |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | static const struct attribute_group max1668_group_common = { |
| 349 | .attrs = max1668_attribute_common, |
| 350 | .is_visible = max1668_attribute_mode |
| 351 | }; |
| 352 | |
| 353 | static const struct attribute_group max1668_group_unique = { |
| 354 | .attrs = max1668_attribute_unique, |
| 355 | .is_visible = max1668_attribute_mode |
| 356 | }; |
| 357 | |
| 358 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 359 | static int max1668_detect(struct i2c_client *client, |
| 360 | struct i2c_board_info *info) |
| 361 | { |
| 362 | struct i2c_adapter *adapter = client->adapter; |
| 363 | const char *type_name; |
| 364 | int man_id, dev_id; |
| 365 | |
| 366 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 367 | return -ENODEV; |
| 368 | |
| 369 | /* Check for unsupported part */ |
| 370 | man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID); |
| 371 | if (man_id != MAN_ID_MAXIM) |
| 372 | return -ENODEV; |
| 373 | |
| 374 | dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID); |
| 375 | if (dev_id < 0) |
| 376 | return -ENODEV; |
| 377 | |
| 378 | type_name = NULL; |
| 379 | if (dev_id == DEV_ID_MAX1668) |
| 380 | type_name = "max1668"; |
| 381 | else if (dev_id == DEV_ID_MAX1805) |
| 382 | type_name = "max1805"; |
| 383 | else if (dev_id == DEV_ID_MAX1989) |
| 384 | type_name = "max1989"; |
| 385 | |
| 386 | if (!type_name) |
| 387 | return -ENODEV; |
| 388 | |
| 389 | strlcpy(info->type, type_name, I2C_NAME_SIZE); |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 394 | static const struct i2c_device_id max1668_id[]; |
| 395 | |
| 396 | static int max1668_probe(struct i2c_client *client) |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 397 | { |
| 398 | struct i2c_adapter *adapter = client->adapter; |
Guenter Roeck | 5230551 | 2014-02-15 17:50:48 -0800 | [diff] [blame] | 399 | struct device *dev = &client->dev; |
| 400 | struct device *hwmon_dev; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 401 | struct max1668_data *data; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 402 | |
| 403 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 404 | return -ENODEV; |
| 405 | |
Guenter Roeck | 5230551 | 2014-02-15 17:50:48 -0800 | [diff] [blame] | 406 | data = devm_kzalloc(dev, sizeof(struct max1668_data), GFP_KERNEL); |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 407 | if (!data) |
| 408 | return -ENOMEM; |
| 409 | |
Guenter Roeck | 5230551 | 2014-02-15 17:50:48 -0800 | [diff] [blame] | 410 | data->client = client; |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 411 | data->type = i2c_match_id(max1668_id, client)->driver_data; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 412 | mutex_init(&data->update_lock); |
| 413 | |
Guenter Roeck | 5230551 | 2014-02-15 17:50:48 -0800 | [diff] [blame] | 414 | /* sysfs hooks */ |
| 415 | data->groups[0] = &max1668_group_common; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 416 | if (data->type == max1668 || data->type == max1989) |
Guenter Roeck | 5230551 | 2014-02-15 17:50:48 -0800 | [diff] [blame] | 417 | data->groups[1] = &max1668_group_unique; |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 418 | |
Guenter Roeck | 5230551 | 2014-02-15 17:50:48 -0800 | [diff] [blame] | 419 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 420 | data, data->groups); |
| 421 | return PTR_ERR_OR_ZERO(hwmon_dev); |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static const struct i2c_device_id max1668_id[] = { |
| 425 | { "max1668", max1668 }, |
| 426 | { "max1805", max1805 }, |
| 427 | { "max1989", max1989 }, |
| 428 | { } |
| 429 | }; |
| 430 | MODULE_DEVICE_TABLE(i2c, max1668_id); |
| 431 | |
| 432 | /* This is the driver that will be inserted */ |
| 433 | static struct i2c_driver max1668_driver = { |
| 434 | .class = I2C_CLASS_HWMON, |
| 435 | .driver = { |
| 436 | .name = "max1668", |
| 437 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 438 | .probe_new = max1668_probe, |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 439 | .id_table = max1668_id, |
| 440 | .detect = max1668_detect, |
| 441 | .address_list = max1668_addr_list, |
| 442 | }; |
| 443 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 444 | module_i2c_driver(max1668_driver); |
David George | 731b4ca | 2011-06-02 08:43:45 -0700 | [diff] [blame] | 445 | |
| 446 | MODULE_AUTHOR("David George <david.george@ska.ac.za>"); |
| 447 | MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver"); |
| 448 | MODULE_LICENSE("GPL"); |