Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 2 | /* |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 3 | * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com> |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 4 | * |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 5 | * The LM95241 is a sensor chip made by National Semiconductors. |
| 6 | * It reports up to three temperatures (its own plus up to two external ones). |
| 7 | * Complete datasheet can be obtained from National's website at: |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 8 | * http://www.national.com/ds.cgi/LM/LM95241.pdf |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Guenter Roeck | e8172a9 | 2016-07-04 08:01:04 -0700 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Guenter Roeck | 4b2ea08 | 2016-07-04 06:48:18 -0700 | [diff] [blame] | 12 | #include <linux/err.h> |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 13 | #include <linux/i2c.h> |
Guenter Roeck | 4b2ea08 | 2016-07-04 06:48:18 -0700 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/jiffies.h> |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 16 | #include <linux/hwmon.h> |
Guenter Roeck | 4b2ea08 | 2016-07-04 06:48:18 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Guenter Roeck | 4b2ea08 | 2016-07-04 06:48:18 -0700 | [diff] [blame] | 19 | #include <linux/slab.h> |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 20 | |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 21 | #define DEVNAME "lm95241" |
| 22 | |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 23 | static const unsigned short normal_i2c[] = { |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 24 | 0x19, 0x2a, 0x2b, I2C_CLIENT_END }; |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 25 | |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 26 | /* LM95241 registers */ |
| 27 | #define LM95241_REG_R_MAN_ID 0xFE |
| 28 | #define LM95241_REG_R_CHIP_ID 0xFF |
| 29 | #define LM95241_REG_R_STATUS 0x02 |
| 30 | #define LM95241_REG_RW_CONFIG 0x03 |
| 31 | #define LM95241_REG_RW_REM_FILTER 0x06 |
| 32 | #define LM95241_REG_RW_TRUTHERM 0x07 |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 33 | #define LM95241_REG_W_ONE_SHOT 0x0F |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 34 | #define LM95241_REG_R_LOCAL_TEMPH 0x10 |
| 35 | #define LM95241_REG_R_REMOTE1_TEMPH 0x11 |
| 36 | #define LM95241_REG_R_REMOTE2_TEMPH 0x12 |
| 37 | #define LM95241_REG_R_LOCAL_TEMPL 0x20 |
| 38 | #define LM95241_REG_R_REMOTE1_TEMPL 0x21 |
| 39 | #define LM95241_REG_R_REMOTE2_TEMPL 0x22 |
| 40 | #define LM95241_REG_RW_REMOTE_MODEL 0x30 |
| 41 | |
| 42 | /* LM95241 specific bitfields */ |
Guenter Roeck | e8172a9 | 2016-07-04 08:01:04 -0700 | [diff] [blame] | 43 | #define CFG_STOP BIT(6) |
| 44 | #define CFG_CR0076 0x00 |
| 45 | #define CFG_CR0182 BIT(4) |
| 46 | #define CFG_CR1000 BIT(5) |
| 47 | #define CFG_CR2700 (BIT(4) | BIT(5)) |
| 48 | #define CFG_CRMASK (BIT(4) | BIT(5)) |
| 49 | #define R1MS_MASK BIT(0) |
| 50 | #define R2MS_MASK BIT(2) |
| 51 | #define R1DF_MASK BIT(1) |
| 52 | #define R2DF_MASK BIT(2) |
| 53 | #define R1FE_MASK BIT(0) |
| 54 | #define R2FE_MASK BIT(2) |
| 55 | #define R1DM BIT(0) |
| 56 | #define R2DM BIT(1) |
| 57 | #define TT1_SHIFT 0 |
| 58 | #define TT2_SHIFT 4 |
| 59 | #define TT_OFF 0 |
| 60 | #define TT_ON 1 |
| 61 | #define TT_MASK 7 |
Guenter Roeck | 8c1d041 | 2011-07-05 13:31:48 -0700 | [diff] [blame] | 62 | #define NATSEMI_MAN_ID 0x01 |
| 63 | #define LM95231_CHIP_ID 0xA1 |
| 64 | #define LM95241_CHIP_ID 0xA4 |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 65 | |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 66 | static const u8 lm95241_reg_address[] = { |
| 67 | LM95241_REG_R_LOCAL_TEMPH, |
| 68 | LM95241_REG_R_LOCAL_TEMPL, |
| 69 | LM95241_REG_R_REMOTE1_TEMPH, |
| 70 | LM95241_REG_R_REMOTE1_TEMPL, |
| 71 | LM95241_REG_R_REMOTE2_TEMPH, |
| 72 | LM95241_REG_R_REMOTE2_TEMPL |
| 73 | }; |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 74 | |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 75 | /* Client data (each client gets its own) */ |
| 76 | struct lm95241_data { |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 77 | struct i2c_client *client; |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 78 | struct mutex update_lock; |
Guenter Roeck | f48ccb2 | 2016-07-03 21:46:05 -0700 | [diff] [blame] | 79 | unsigned long last_updated; /* in jiffies */ |
| 80 | unsigned long interval; /* in milli-seconds */ |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 81 | char valid; /* zero until following fields are valid */ |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 82 | /* registers values */ |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 83 | u8 temp[ARRAY_SIZE(lm95241_reg_address)]; |
Guenter Roeck | 090a7f8 | 2016-07-04 06:46:31 -0700 | [diff] [blame] | 84 | u8 status, config, model, trutherm; |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 87 | /* Conversions */ |
Guenter Roeck | 0c2a40e | 2011-06-30 02:09:37 -0700 | [diff] [blame] | 88 | static int temp_from_reg_signed(u8 val_h, u8 val_l) |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 89 | { |
Guenter Roeck | 0c2a40e | 2011-06-30 02:09:37 -0700 | [diff] [blame] | 90 | s16 val_hl = (val_h << 8) | val_l; |
| 91 | return val_hl * 1000 / 256; |
| 92 | } |
| 93 | |
| 94 | static int temp_from_reg_unsigned(u8 val_h, u8 val_l) |
| 95 | { |
| 96 | u16 val_hl = (val_h << 8) | val_l; |
| 97 | return val_hl * 1000 / 256; |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 98 | } |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 99 | |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 100 | static struct lm95241_data *lm95241_update_device(struct device *dev) |
| 101 | { |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 102 | struct lm95241_data *data = dev_get_drvdata(dev); |
| 103 | struct i2c_client *client = data->client; |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 104 | |
| 105 | mutex_lock(&data->update_lock); |
| 106 | |
Guenter Roeck | f48ccb2 | 2016-07-03 21:46:05 -0700 | [diff] [blame] | 107 | if (time_after(jiffies, data->last_updated |
| 108 | + msecs_to_jiffies(data->interval)) || |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 109 | !data->valid) { |
| 110 | int i; |
| 111 | |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 112 | dev_dbg(dev, "Updating lm95241 data.\n"); |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 113 | for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++) |
| 114 | data->temp[i] |
| 115 | = i2c_smbus_read_byte_data(client, |
| 116 | lm95241_reg_address[i]); |
Guenter Roeck | 090a7f8 | 2016-07-04 06:46:31 -0700 | [diff] [blame] | 117 | |
| 118 | data->status = i2c_smbus_read_byte_data(client, |
| 119 | LM95241_REG_R_STATUS); |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 120 | data->last_updated = jiffies; |
| 121 | data->valid = 1; |
| 122 | } |
| 123 | |
| 124 | mutex_unlock(&data->update_lock); |
| 125 | |
| 126 | return data; |
| 127 | } |
| 128 | |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 129 | static int lm95241_read_chip(struct device *dev, u32 attr, int channel, |
| 130 | long *val) |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 131 | { |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 132 | struct lm95241_data *data = dev_get_drvdata(dev); |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 133 | |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 134 | switch (attr) { |
| 135 | case hwmon_chip_update_interval: |
| 136 | *val = data->interval; |
| 137 | return 0; |
| 138 | default: |
| 139 | return -EOPNOTSUPP; |
| 140 | } |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 143 | static int lm95241_read_temp(struct device *dev, u32 attr, int channel, |
| 144 | long *val) |
| 145 | { |
| 146 | struct lm95241_data *data = lm95241_update_device(dev); |
| 147 | |
| 148 | switch (attr) { |
| 149 | case hwmon_temp_input: |
| 150 | if (!channel || (data->config & BIT(channel - 1))) |
| 151 | *val = temp_from_reg_signed(data->temp[channel * 2], |
| 152 | data->temp[channel * 2 + 1]); |
| 153 | else |
| 154 | *val = temp_from_reg_unsigned(data->temp[channel * 2], |
| 155 | data->temp[channel * 2 + 1]); |
| 156 | return 0; |
| 157 | case hwmon_temp_min: |
| 158 | if (channel == 1) |
| 159 | *val = (data->config & R1DF_MASK) ? -128000 : 0; |
| 160 | else |
| 161 | *val = (data->config & R2DF_MASK) ? -128000 : 0; |
| 162 | return 0; |
| 163 | case hwmon_temp_max: |
| 164 | if (channel == 1) |
| 165 | *val = (data->config & R1DF_MASK) ? 127875 : 255875; |
| 166 | else |
| 167 | *val = (data->config & R2DF_MASK) ? 127875 : 255875; |
| 168 | return 0; |
| 169 | case hwmon_temp_type: |
| 170 | if (channel == 1) |
| 171 | *val = (data->model & R1MS_MASK) ? 1 : 2; |
| 172 | else |
| 173 | *val = (data->model & R2MS_MASK) ? 1 : 2; |
| 174 | return 0; |
| 175 | case hwmon_temp_fault: |
| 176 | if (channel == 1) |
| 177 | *val = !!(data->status & R1DM); |
| 178 | else |
| 179 | *val = !!(data->status & R2DM); |
| 180 | return 0; |
| 181 | default: |
| 182 | return -EOPNOTSUPP; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static int lm95241_read(struct device *dev, enum hwmon_sensor_types type, |
| 187 | u32 attr, int channel, long *val) |
| 188 | { |
| 189 | switch (type) { |
| 190 | case hwmon_chip: |
| 191 | return lm95241_read_chip(dev, attr, channel, val); |
| 192 | case hwmon_temp: |
| 193 | return lm95241_read_temp(dev, attr, channel, val); |
| 194 | default: |
| 195 | return -EOPNOTSUPP; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static int lm95241_write_chip(struct device *dev, u32 attr, int channel, |
| 200 | long val) |
| 201 | { |
| 202 | struct lm95241_data *data = dev_get_drvdata(dev); |
| 203 | int convrate; |
| 204 | u8 config; |
| 205 | int ret; |
| 206 | |
| 207 | mutex_lock(&data->update_lock); |
| 208 | |
| 209 | switch (attr) { |
| 210 | case hwmon_chip_update_interval: |
| 211 | config = data->config & ~CFG_CRMASK; |
| 212 | if (val < 130) { |
| 213 | convrate = 76; |
| 214 | config |= CFG_CR0076; |
| 215 | } else if (val < 590) { |
| 216 | convrate = 182; |
| 217 | config |= CFG_CR0182; |
| 218 | } else if (val < 1850) { |
| 219 | convrate = 1000; |
| 220 | config |= CFG_CR1000; |
| 221 | } else { |
| 222 | convrate = 2700; |
| 223 | config |= CFG_CR2700; |
| 224 | } |
| 225 | data->interval = convrate; |
| 226 | data->config = config; |
| 227 | ret = i2c_smbus_write_byte_data(data->client, |
| 228 | LM95241_REG_RW_CONFIG, config); |
| 229 | break; |
| 230 | default: |
| 231 | ret = -EOPNOTSUPP; |
| 232 | break; |
| 233 | } |
| 234 | mutex_unlock(&data->update_lock); |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static int lm95241_write_temp(struct device *dev, u32 attr, int channel, |
| 239 | long val) |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 240 | { |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 241 | struct lm95241_data *data = dev_get_drvdata(dev); |
| 242 | struct i2c_client *client = data->client; |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 243 | int ret; |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 244 | |
| 245 | mutex_lock(&data->update_lock); |
| 246 | |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 247 | switch (attr) { |
| 248 | case hwmon_temp_min: |
| 249 | if (channel == 1) { |
| 250 | if (val < 0) |
| 251 | data->config |= R1DF_MASK; |
| 252 | else |
| 253 | data->config &= ~R1DF_MASK; |
| 254 | } else { |
| 255 | if (val < 0) |
| 256 | data->config |= R2DF_MASK; |
| 257 | else |
| 258 | data->config &= ~R2DF_MASK; |
| 259 | } |
| 260 | data->valid = 0; |
| 261 | ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, |
| 262 | data->config); |
| 263 | break; |
| 264 | case hwmon_temp_max: |
| 265 | if (channel == 1) { |
| 266 | if (val <= 127875) |
| 267 | data->config |= R1DF_MASK; |
| 268 | else |
| 269 | data->config &= ~R1DF_MASK; |
| 270 | } else { |
| 271 | if (val <= 127875) |
| 272 | data->config |= R2DF_MASK; |
| 273 | else |
| 274 | data->config &= ~R2DF_MASK; |
| 275 | } |
| 276 | data->valid = 0; |
| 277 | ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, |
| 278 | data->config); |
| 279 | break; |
| 280 | case hwmon_temp_type: |
| 281 | if (val != 1 && val != 2) { |
| 282 | ret = -EINVAL; |
| 283 | break; |
| 284 | } |
| 285 | if (channel == 1) { |
| 286 | data->trutherm &= ~(TT_MASK << TT1_SHIFT); |
| 287 | if (val == 1) { |
| 288 | data->model |= R1MS_MASK; |
| 289 | data->trutherm |= (TT_ON << TT1_SHIFT); |
| 290 | } else { |
| 291 | data->model &= ~R1MS_MASK; |
| 292 | data->trutherm |= (TT_OFF << TT1_SHIFT); |
| 293 | } |
| 294 | } else { |
| 295 | data->trutherm &= ~(TT_MASK << TT2_SHIFT); |
| 296 | if (val == 1) { |
| 297 | data->model |= R2MS_MASK; |
| 298 | data->trutherm |= (TT_ON << TT2_SHIFT); |
| 299 | } else { |
| 300 | data->model &= ~R2MS_MASK; |
| 301 | data->trutherm |= (TT_OFF << TT2_SHIFT); |
| 302 | } |
| 303 | } |
| 304 | ret = i2c_smbus_write_byte_data(client, |
| 305 | LM95241_REG_RW_REMOTE_MODEL, |
| 306 | data->model); |
| 307 | if (ret < 0) |
| 308 | break; |
| 309 | ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, |
| 310 | data->trutherm); |
| 311 | break; |
| 312 | default: |
| 313 | ret = -EOPNOTSUPP; |
| 314 | break; |
Guenter Roeck | f48ccb2 | 2016-07-03 21:46:05 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Guenter Roeck | f48ccb2 | 2016-07-03 21:46:05 -0700 | [diff] [blame] | 317 | mutex_unlock(&data->update_lock); |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 318 | |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 319 | return ret; |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 322 | static int lm95241_write(struct device *dev, enum hwmon_sensor_types type, |
| 323 | u32 attr, int channel, long val) |
| 324 | { |
| 325 | switch (type) { |
| 326 | case hwmon_chip: |
| 327 | return lm95241_write_chip(dev, attr, channel, val); |
| 328 | case hwmon_temp: |
| 329 | return lm95241_write_temp(dev, attr, channel, val); |
| 330 | default: |
| 331 | return -EOPNOTSUPP; |
| 332 | } |
| 333 | } |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 334 | |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 335 | static umode_t lm95241_is_visible(const void *data, |
| 336 | enum hwmon_sensor_types type, |
| 337 | u32 attr, int channel) |
| 338 | { |
| 339 | switch (type) { |
| 340 | case hwmon_chip: |
| 341 | switch (attr) { |
| 342 | case hwmon_chip_update_interval: |
Guenter Roeck | 5c06d56 | 2018-12-10 14:02:13 -0800 | [diff] [blame] | 343 | return 0644; |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 344 | } |
| 345 | break; |
| 346 | case hwmon_temp: |
| 347 | switch (attr) { |
| 348 | case hwmon_temp_input: |
Guenter Roeck | 5c06d56 | 2018-12-10 14:02:13 -0800 | [diff] [blame] | 349 | return 0444; |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 350 | case hwmon_temp_fault: |
Guenter Roeck | 5c06d56 | 2018-12-10 14:02:13 -0800 | [diff] [blame] | 351 | return 0444; |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 352 | case hwmon_temp_min: |
| 353 | case hwmon_temp_max: |
| 354 | case hwmon_temp_type: |
Guenter Roeck | 5c06d56 | 2018-12-10 14:02:13 -0800 | [diff] [blame] | 355 | return 0644; |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 356 | } |
| 357 | break; |
| 358 | default: |
| 359 | break; |
| 360 | } |
| 361 | return 0; |
| 362 | } |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 363 | |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 364 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 365 | static int lm95241_detect(struct i2c_client *new_client, |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 366 | struct i2c_board_info *info) |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 367 | { |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 368 | struct i2c_adapter *adapter = new_client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 369 | const char *name; |
Guenter Roeck | 8c1d041 | 2011-07-05 13:31:48 -0700 | [diff] [blame] | 370 | int mfg_id, chip_id; |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 371 | |
| 372 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 373 | return -ENODEV; |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 374 | |
Guenter Roeck | 8c1d041 | 2011-07-05 13:31:48 -0700 | [diff] [blame] | 375 | mfg_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID); |
| 376 | if (mfg_id != NATSEMI_MAN_ID) |
| 377 | return -ENODEV; |
| 378 | |
| 379 | chip_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID); |
| 380 | switch (chip_id) { |
| 381 | case LM95231_CHIP_ID: |
| 382 | name = "lm95231"; |
| 383 | break; |
| 384 | case LM95241_CHIP_ID: |
| 385 | name = "lm95241"; |
| 386 | break; |
| 387 | default: |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 388 | return -ENODEV; |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 391 | /* Fill the i2c board info */ |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 392 | strlcpy(info->type, name, I2C_NAME_SIZE); |
| 393 | return 0; |
| 394 | } |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 395 | |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 396 | static void lm95241_init_client(struct i2c_client *client, |
| 397 | struct lm95241_data *data) |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 398 | { |
Guenter Roeck | f48ccb2 | 2016-07-03 21:46:05 -0700 | [diff] [blame] | 399 | data->interval = 1000; |
| 400 | data->config = CFG_CR1000; |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 401 | data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT); |
| 402 | |
| 403 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config); |
| 404 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER, |
| 405 | R1FE_MASK | R2FE_MASK); |
| 406 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, |
| 407 | data->trutherm); |
| 408 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL, |
| 409 | data->model); |
| 410 | } |
| 411 | |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 412 | static const struct hwmon_channel_info *lm95241_info[] = { |
Guenter Roeck | f5011f9 | 2019-03-31 10:53:48 -0700 | [diff] [blame] | 413 | HWMON_CHANNEL_INFO(chip, |
| 414 | HWMON_C_UPDATE_INTERVAL), |
| 415 | HWMON_CHANNEL_INFO(temp, |
| 416 | HWMON_T_INPUT, |
| 417 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | |
| 418 | HWMON_T_TYPE | HWMON_T_FAULT, |
| 419 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | |
| 420 | HWMON_T_TYPE | HWMON_T_FAULT), |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 421 | NULL |
| 422 | }; |
| 423 | |
| 424 | static const struct hwmon_ops lm95241_hwmon_ops = { |
| 425 | .is_visible = lm95241_is_visible, |
| 426 | .read = lm95241_read, |
| 427 | .write = lm95241_write, |
| 428 | }; |
| 429 | |
| 430 | static const struct hwmon_chip_info lm95241_chip_info = { |
| 431 | .ops = &lm95241_hwmon_ops, |
| 432 | .info = lm95241_info, |
| 433 | }; |
| 434 | |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 435 | static int lm95241_probe(struct i2c_client *client, |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 436 | const struct i2c_device_id *id) |
| 437 | { |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 438 | struct device *dev = &client->dev; |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 439 | struct lm95241_data *data; |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 440 | struct device *hwmon_dev; |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 441 | |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 442 | data = devm_kzalloc(dev, sizeof(struct lm95241_data), GFP_KERNEL); |
Guenter Roeck | 1487bf7 | 2012-06-02 09:58:11 -0700 | [diff] [blame] | 443 | if (!data) |
| 444 | return -ENOMEM; |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 445 | |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 446 | data->client = client; |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 447 | mutex_init(&data->update_lock); |
| 448 | |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 449 | /* Initialize the LM95241 chip */ |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 450 | lm95241_init_client(client, data); |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 451 | |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 452 | hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 453 | data, |
Guenter Roeck | 3e90462 | 2016-07-04 19:58:11 -0700 | [diff] [blame] | 454 | &lm95241_chip_info, |
| 455 | NULL); |
Guenter Roeck | f809621 | 2014-01-20 09:25:50 -0800 | [diff] [blame] | 456 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 459 | /* Driver data (common to all clients) */ |
| 460 | static const struct i2c_device_id lm95241_id[] = { |
Guenter Roeck | 8c1d041 | 2011-07-05 13:31:48 -0700 | [diff] [blame] | 461 | { "lm95231", 0 }, |
| 462 | { "lm95241", 0 }, |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 463 | { } |
| 464 | }; |
| 465 | MODULE_DEVICE_TABLE(i2c, lm95241_id); |
| 466 | |
| 467 | static struct i2c_driver lm95241_driver = { |
| 468 | .class = I2C_CLASS_HWMON, |
| 469 | .driver = { |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 470 | .name = DEVNAME, |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 471 | }, |
| 472 | .probe = lm95241_probe, |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 473 | .id_table = lm95241_id, |
| 474 | .detect = lm95241_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 475 | .address_list = normal_i2c, |
Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 476 | }; |
| 477 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 478 | module_i2c_driver(lm95241_driver); |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 479 | |
Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 480 | MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>"); |
Axel Lin | 0a8784e | 2016-09-13 15:27:48 +0800 | [diff] [blame] | 481 | MODULE_DESCRIPTION("LM95231/LM95241 sensor driver"); |
Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 482 | MODULE_LICENSE("GPL"); |