Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Alexander Stein <alexander.stein@systec-electronic.com> |
| 3 | * |
Guenter Roeck | 162a8df | 2014-04-22 08:48:57 -0700 | [diff] [blame] | 4 | * The LM95245 is a sensor chip made by TI / National Semiconductor. |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 5 | * It reports up to two temperatures (its own plus an external one). |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 6 | * |
| 7 | * This driver is based on lm95241.c |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 20 | #include <linux/err.h> |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | #include <linux/hwmon.h> |
| 23 | #include <linux/i2c.h> |
| 24 | #include <linux/module.h> |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 26 | #include <linux/regmap.h> |
| 27 | #include <linux/slab.h> |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 28 | |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 29 | static const unsigned short normal_i2c[] = { |
| 30 | 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END }; |
| 31 | |
| 32 | /* LM95245 registers */ |
| 33 | /* general registers */ |
| 34 | #define LM95245_REG_RW_CONFIG1 0x03 |
| 35 | #define LM95245_REG_RW_CONVERS_RATE 0x04 |
| 36 | #define LM95245_REG_W_ONE_SHOT 0x0F |
| 37 | |
| 38 | /* diode configuration */ |
| 39 | #define LM95245_REG_RW_CONFIG2 0xBF |
| 40 | #define LM95245_REG_RW_REMOTE_OFFH 0x11 |
| 41 | #define LM95245_REG_RW_REMOTE_OFFL 0x12 |
| 42 | |
| 43 | /* status registers */ |
| 44 | #define LM95245_REG_R_STATUS1 0x02 |
| 45 | #define LM95245_REG_R_STATUS2 0x33 |
| 46 | |
| 47 | /* limit registers */ |
| 48 | #define LM95245_REG_RW_REMOTE_OS_LIMIT 0x07 |
| 49 | #define LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT 0x20 |
| 50 | #define LM95245_REG_RW_REMOTE_TCRIT_LIMIT 0x19 |
| 51 | #define LM95245_REG_RW_COMMON_HYSTERESIS 0x21 |
| 52 | |
| 53 | /* temperature signed */ |
| 54 | #define LM95245_REG_R_LOCAL_TEMPH_S 0x00 |
| 55 | #define LM95245_REG_R_LOCAL_TEMPL_S 0x30 |
| 56 | #define LM95245_REG_R_REMOTE_TEMPH_S 0x01 |
| 57 | #define LM95245_REG_R_REMOTE_TEMPL_S 0x10 |
| 58 | /* temperature unsigned */ |
| 59 | #define LM95245_REG_R_REMOTE_TEMPH_U 0x31 |
| 60 | #define LM95245_REG_R_REMOTE_TEMPL_U 0x32 |
| 61 | |
| 62 | /* id registers */ |
| 63 | #define LM95245_REG_R_MAN_ID 0xFE |
| 64 | #define LM95245_REG_R_CHIP_ID 0xFF |
| 65 | |
| 66 | /* LM95245 specific bitfields */ |
| 67 | #define CFG_STOP 0x40 |
| 68 | #define CFG_REMOTE_TCRIT_MASK 0x10 |
| 69 | #define CFG_REMOTE_OS_MASK 0x08 |
| 70 | #define CFG_LOCAL_TCRIT_MASK 0x04 |
| 71 | #define CFG_LOCAL_OS_MASK 0x02 |
| 72 | |
| 73 | #define CFG2_OS_A0 0x40 |
| 74 | #define CFG2_DIODE_FAULT_OS 0x20 |
| 75 | #define CFG2_DIODE_FAULT_TCRIT 0x10 |
| 76 | #define CFG2_REMOTE_TT 0x08 |
| 77 | #define CFG2_REMOTE_FILTER_DIS 0x00 |
| 78 | #define CFG2_REMOTE_FILTER_EN 0x06 |
| 79 | |
| 80 | /* conversation rate in ms */ |
| 81 | #define RATE_CR0063 0x00 |
| 82 | #define RATE_CR0364 0x01 |
| 83 | #define RATE_CR1000 0x02 |
| 84 | #define RATE_CR2500 0x03 |
| 85 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 86 | #define STATUS1_ROS 0x10 |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 87 | #define STATUS1_DIODE_FAULT 0x04 |
| 88 | #define STATUS1_RTCRIT 0x02 |
| 89 | #define STATUS1_LOC 0x01 |
| 90 | |
| 91 | #define MANUFACTURER_ID 0x01 |
Guenter Roeck | 162a8df | 2014-04-22 08:48:57 -0700 | [diff] [blame] | 92 | #define LM95235_REVISION 0xB1 |
| 93 | #define LM95245_REVISION 0xB3 |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 94 | |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 95 | /* Client data (each client gets its own) */ |
| 96 | struct lm95245_data { |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 97 | struct regmap *regmap; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 98 | struct mutex update_lock; |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 99 | int interval; /* in msecs */ |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /* Conversions */ |
| 103 | static int temp_from_reg_unsigned(u8 val_h, u8 val_l) |
| 104 | { |
| 105 | return val_h * 1000 + val_l * 1000 / 256; |
| 106 | } |
| 107 | |
| 108 | static int temp_from_reg_signed(u8 val_h, u8 val_l) |
| 109 | { |
| 110 | if (val_h & 0x80) |
| 111 | return (val_h - 0x100) * 1000; |
| 112 | return temp_from_reg_unsigned(val_h, val_l); |
| 113 | } |
| 114 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 115 | static int lm95245_read_conversion_rate(struct lm95245_data *data) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 116 | { |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 117 | unsigned int rate; |
| 118 | int ret; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 119 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 120 | ret = regmap_read(data->regmap, LM95245_REG_RW_CONVERS_RATE, &rate); |
| 121 | if (ret < 0) |
| 122 | return ret; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 123 | |
| 124 | switch (rate) { |
| 125 | case RATE_CR0063: |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 126 | data->interval = 63; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 127 | break; |
| 128 | case RATE_CR0364: |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 129 | data->interval = 364; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 130 | break; |
| 131 | case RATE_CR1000: |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 132 | data->interval = 1000; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 133 | break; |
| 134 | case RATE_CR2500: |
| 135 | default: |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 136 | data->interval = 2500; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 137 | break; |
| 138 | } |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 139 | return 0; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 142 | static int lm95245_set_conversion_rate(struct lm95245_data *data, long interval) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 143 | { |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 144 | int ret, rate; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 145 | |
| 146 | if (interval <= 63) { |
| 147 | interval = 63; |
| 148 | rate = RATE_CR0063; |
| 149 | } else if (interval <= 364) { |
| 150 | interval = 364; |
| 151 | rate = RATE_CR0364; |
| 152 | } else if (interval <= 1000) { |
| 153 | interval = 1000; |
| 154 | rate = RATE_CR1000; |
| 155 | } else { |
| 156 | interval = 2500; |
| 157 | rate = RATE_CR2500; |
| 158 | } |
| 159 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 160 | ret = regmap_write(data->regmap, LM95245_REG_RW_CONVERS_RATE, rate); |
| 161 | if (ret < 0) |
| 162 | return ret; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 163 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 164 | data->interval = interval; |
| 165 | return 0; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 168 | static int lm95245_read_temp(struct device *dev, u32 attr, int channel, |
| 169 | long *val) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 170 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 171 | struct lm95245_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 172 | struct regmap *regmap = data->regmap; |
| 173 | int ret, regl, regh, regvall, regvalh; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 174 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 175 | switch (attr) { |
| 176 | case hwmon_temp_input: |
| 177 | regl = channel ? LM95245_REG_R_REMOTE_TEMPL_S : |
| 178 | LM95245_REG_R_LOCAL_TEMPL_S; |
| 179 | regh = channel ? LM95245_REG_R_REMOTE_TEMPH_S : |
| 180 | LM95245_REG_R_LOCAL_TEMPH_S; |
| 181 | ret = regmap_read(regmap, regl, ®vall); |
| 182 | if (ret < 0) |
| 183 | return ret; |
| 184 | ret = regmap_read(regmap, regh, ®valh); |
| 185 | if (ret < 0) |
| 186 | return ret; |
| 187 | /* |
| 188 | * Local temp is always signed. |
| 189 | * Remote temp has both signed and unsigned data. |
| 190 | * Use signed calculation for remote if signed bit is set |
| 191 | * or if reported temperature is below signed limit. |
| 192 | */ |
| 193 | if (!channel || (regvalh & 0x80) || regvalh < 0x7f) { |
| 194 | *val = temp_from_reg_signed(regvalh, regvall); |
| 195 | return 0; |
| 196 | } |
| 197 | ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPL_U, |
| 198 | ®vall); |
| 199 | if (ret < 0) |
| 200 | return ret; |
| 201 | ret = regmap_read(regmap, LM95245_REG_R_REMOTE_TEMPH_U, |
| 202 | ®valh); |
| 203 | if (ret < 0) |
| 204 | return ret; |
| 205 | *val = temp_from_reg_unsigned(regvalh, regvall); |
| 206 | return 0; |
| 207 | case hwmon_temp_max: |
| 208 | ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT, |
| 209 | ®valh); |
| 210 | if (ret < 0) |
| 211 | return ret; |
| 212 | *val = regvalh * 1000; |
| 213 | return 0; |
| 214 | case hwmon_temp_crit: |
| 215 | regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT : |
| 216 | LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT; |
| 217 | ret = regmap_read(regmap, regh, ®valh); |
| 218 | if (ret < 0) |
| 219 | return ret; |
| 220 | *val = regvalh * 1000; |
| 221 | return 0; |
| 222 | case hwmon_temp_max_hyst: |
| 223 | ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT, |
| 224 | ®valh); |
| 225 | if (ret < 0) |
| 226 | return ret; |
| 227 | ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS, |
| 228 | ®vall); |
| 229 | if (ret < 0) |
| 230 | return ret; |
| 231 | *val = (regvalh - regvall) * 1000; |
| 232 | return 0; |
| 233 | case hwmon_temp_crit_hyst: |
| 234 | regh = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT : |
| 235 | LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT; |
| 236 | ret = regmap_read(regmap, regh, ®valh); |
| 237 | if (ret < 0) |
| 238 | return ret; |
| 239 | ret = regmap_read(regmap, LM95245_REG_RW_COMMON_HYSTERESIS, |
| 240 | ®vall); |
| 241 | if (ret < 0) |
| 242 | return ret; |
| 243 | *val = (regvalh - regvall) * 1000; |
| 244 | return 0; |
| 245 | case hwmon_temp_type: |
| 246 | ret = regmap_read(regmap, LM95245_REG_RW_CONFIG2, ®valh); |
| 247 | if (ret < 0) |
| 248 | return ret; |
| 249 | *val = (regvalh & CFG2_REMOTE_TT) ? 1 : 2; |
| 250 | return 0; |
| 251 | case hwmon_temp_offset: |
| 252 | ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFL, |
| 253 | ®vall); |
| 254 | if (ret < 0) |
| 255 | return ret; |
| 256 | ret = regmap_read(regmap, LM95245_REG_RW_REMOTE_OFFH, |
| 257 | ®valh); |
| 258 | if (ret < 0) |
| 259 | return ret; |
| 260 | *val = temp_from_reg_signed(regvalh, regvall); |
| 261 | return 0; |
| 262 | case hwmon_temp_max_alarm: |
| 263 | ret = regmap_read(regmap, LM95245_REG_R_STATUS1, ®valh); |
| 264 | if (ret < 0) |
| 265 | return ret; |
| 266 | *val = !!(regvalh & STATUS1_ROS); |
| 267 | return 0; |
| 268 | case hwmon_temp_crit_alarm: |
| 269 | ret = regmap_read(regmap, LM95245_REG_R_STATUS1, ®valh); |
| 270 | if (ret < 0) |
| 271 | return ret; |
| 272 | *val = !!(regvalh & (channel ? STATUS1_RTCRIT : STATUS1_LOC)); |
| 273 | return 0; |
| 274 | case hwmon_temp_fault: |
| 275 | ret = regmap_read(regmap, LM95245_REG_R_STATUS1, ®valh); |
| 276 | if (ret < 0) |
| 277 | return ret; |
| 278 | *val = !!(regvalh & STATUS1_DIODE_FAULT); |
| 279 | return 0; |
| 280 | default: |
| 281 | return -EOPNOTSUPP; |
| 282 | } |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 285 | static int lm95245_write_temp(struct device *dev, u32 attr, int channel, |
| 286 | long val) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 287 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 288 | struct lm95245_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 289 | struct regmap *regmap = data->regmap; |
| 290 | unsigned int regval; |
| 291 | int ret, reg; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 292 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 293 | switch (attr) { |
| 294 | case hwmon_temp_max: |
| 295 | val = clamp_val(val / 1000, 0, 255); |
| 296 | ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OS_LIMIT, val); |
| 297 | return ret; |
| 298 | case hwmon_temp_crit: |
| 299 | reg = channel ? LM95245_REG_RW_REMOTE_TCRIT_LIMIT : |
| 300 | LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT; |
| 301 | val = clamp_val(val / 1000, 0, channel ? 255 : 127); |
| 302 | ret = regmap_write(regmap, reg, val); |
| 303 | return ret; |
| 304 | case hwmon_temp_crit_hyst: |
| 305 | mutex_lock(&data->update_lock); |
| 306 | ret = regmap_read(regmap, LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT, |
| 307 | ®val); |
| 308 | if (ret < 0) { |
| 309 | mutex_unlock(&data->update_lock); |
| 310 | return ret; |
| 311 | } |
| 312 | /* Clamp to reasonable range to prevent overflow */ |
| 313 | val = clamp_val(val, -1000000, 1000000); |
| 314 | val = regval - val / 1000; |
| 315 | val = clamp_val(val, 0, 31); |
| 316 | ret = regmap_write(regmap, LM95245_REG_RW_COMMON_HYSTERESIS, |
| 317 | val); |
| 318 | mutex_unlock(&data->update_lock); |
| 319 | return ret; |
| 320 | case hwmon_temp_offset: |
| 321 | val = clamp_val(val, -128000, 127875); |
| 322 | val = val * 256 / 1000; |
| 323 | mutex_lock(&data->update_lock); |
| 324 | ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFL, |
| 325 | val & 0xe0); |
| 326 | if (ret < 0) { |
| 327 | mutex_unlock(&data->update_lock); |
| 328 | return ret; |
| 329 | } |
| 330 | ret = regmap_write(regmap, LM95245_REG_RW_REMOTE_OFFH, |
| 331 | (val >> 8) & 0xff); |
| 332 | mutex_unlock(&data->update_lock); |
| 333 | return ret; |
| 334 | case hwmon_temp_type: |
| 335 | if (val != 1 && val != 2) |
| 336 | return -EINVAL; |
| 337 | ret = regmap_update_bits(regmap, LM95245_REG_RW_CONFIG2, |
| 338 | CFG2_REMOTE_TT, |
| 339 | val == 1 ? CFG2_REMOTE_TT : 0); |
| 340 | return ret; |
| 341 | default: |
| 342 | return -EOPNOTSUPP; |
| 343 | } |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 346 | static int lm95245_read_chip(struct device *dev, u32 attr, int channel, |
| 347 | long *val) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 348 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 349 | struct lm95245_data *data = dev_get_drvdata(dev); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 350 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 351 | switch (attr) { |
| 352 | case hwmon_chip_update_interval: |
| 353 | *val = data->interval; |
| 354 | return 0; |
| 355 | default: |
| 356 | return -EOPNOTSUPP; |
| 357 | } |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 360 | static int lm95245_write_chip(struct device *dev, u32 attr, int channel, |
| 361 | long val) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 362 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 363 | struct lm95245_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 364 | int ret; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 365 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 366 | switch (attr) { |
| 367 | case hwmon_chip_update_interval: |
| 368 | mutex_lock(&data->update_lock); |
| 369 | ret = lm95245_set_conversion_rate(data, val); |
| 370 | mutex_unlock(&data->update_lock); |
| 371 | return ret; |
| 372 | default: |
| 373 | return -EOPNOTSUPP; |
| 374 | } |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 377 | static int lm95245_read(struct device *dev, enum hwmon_sensor_types type, |
| 378 | u32 attr, int channel, long *val) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 379 | { |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 380 | switch (type) { |
| 381 | case hwmon_chip: |
| 382 | return lm95245_read_chip(dev, attr, channel, val); |
| 383 | case hwmon_temp: |
| 384 | return lm95245_read_temp(dev, attr, channel, val); |
| 385 | default: |
| 386 | return -EOPNOTSUPP; |
| 387 | } |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 390 | static int lm95245_write(struct device *dev, enum hwmon_sensor_types type, |
| 391 | u32 attr, int channel, long val) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 392 | { |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 393 | switch (type) { |
| 394 | case hwmon_chip: |
| 395 | return lm95245_write_chip(dev, attr, channel, val); |
| 396 | case hwmon_temp: |
| 397 | return lm95245_write_temp(dev, attr, channel, val); |
| 398 | default: |
| 399 | return -EOPNOTSUPP; |
| 400 | } |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 403 | static umode_t lm95245_temp_is_visible(const void *data, u32 attr, int channel) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 404 | { |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 405 | switch (attr) { |
| 406 | case hwmon_temp_input: |
| 407 | case hwmon_temp_max_alarm: |
| 408 | case hwmon_temp_max_hyst: |
| 409 | case hwmon_temp_crit_alarm: |
| 410 | case hwmon_temp_fault: |
Guenter Roeck | 5a2d18c | 2018-12-10 14:02:14 -0800 | [diff] [blame] | 411 | return 0444; |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 412 | case hwmon_temp_type: |
| 413 | case hwmon_temp_max: |
| 414 | case hwmon_temp_crit: |
| 415 | case hwmon_temp_offset: |
Guenter Roeck | 5a2d18c | 2018-12-10 14:02:14 -0800 | [diff] [blame] | 416 | return 0644; |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 417 | case hwmon_temp_crit_hyst: |
Guenter Roeck | 5a2d18c | 2018-12-10 14:02:14 -0800 | [diff] [blame] | 418 | return (channel == 0) ? 0644 : 0444; |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 419 | default: |
| 420 | return 0; |
| 421 | } |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 424 | static umode_t lm95245_is_visible(const void *data, |
| 425 | enum hwmon_sensor_types type, |
| 426 | u32 attr, int channel) |
| 427 | { |
| 428 | switch (type) { |
| 429 | case hwmon_chip: |
| 430 | switch (attr) { |
| 431 | case hwmon_chip_update_interval: |
Guenter Roeck | 5a2d18c | 2018-12-10 14:02:14 -0800 | [diff] [blame] | 432 | return 0644; |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 433 | default: |
| 434 | return 0; |
| 435 | } |
| 436 | case hwmon_temp: |
| 437 | return lm95245_temp_is_visible(data, attr, channel); |
| 438 | default: |
| 439 | return 0; |
| 440 | } |
| 441 | } |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 442 | |
| 443 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 444 | static int lm95245_detect(struct i2c_client *new_client, |
| 445 | struct i2c_board_info *info) |
| 446 | { |
| 447 | struct i2c_adapter *adapter = new_client->adapter; |
Guenter Roeck | 162a8df | 2014-04-22 08:48:57 -0700 | [diff] [blame] | 448 | int address = new_client->addr; |
| 449 | const char *name; |
| 450 | int rev, id; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 451 | |
| 452 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 453 | return -ENODEV; |
| 454 | |
Guenter Roeck | 162a8df | 2014-04-22 08:48:57 -0700 | [diff] [blame] | 455 | id = i2c_smbus_read_byte_data(new_client, LM95245_REG_R_MAN_ID); |
| 456 | if (id != MANUFACTURER_ID) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 457 | return -ENODEV; |
| 458 | |
Guenter Roeck | 162a8df | 2014-04-22 08:48:57 -0700 | [diff] [blame] | 459 | rev = i2c_smbus_read_byte_data(new_client, LM95245_REG_R_CHIP_ID); |
| 460 | switch (rev) { |
| 461 | case LM95235_REVISION: |
| 462 | if (address != 0x18 && address != 0x29 && address != 0x4c) |
| 463 | return -ENODEV; |
| 464 | name = "lm95235"; |
| 465 | break; |
| 466 | case LM95245_REVISION: |
| 467 | name = "lm95245"; |
| 468 | break; |
| 469 | default: |
| 470 | return -ENODEV; |
| 471 | } |
| 472 | |
| 473 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 477 | static int lm95245_init_client(struct lm95245_data *data) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 478 | { |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 479 | int ret; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 480 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 481 | ret = lm95245_read_conversion_rate(data); |
| 482 | if (ret < 0) |
| 483 | return ret; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 484 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 485 | return regmap_update_bits(data->regmap, LM95245_REG_RW_CONFIG1, |
| 486 | CFG_STOP, 0); |
| 487 | } |
| 488 | |
| 489 | static bool lm95245_is_writeable_reg(struct device *dev, unsigned int reg) |
| 490 | { |
| 491 | switch (reg) { |
| 492 | case LM95245_REG_RW_CONFIG1: |
| 493 | case LM95245_REG_RW_CONVERS_RATE: |
| 494 | case LM95245_REG_W_ONE_SHOT: |
| 495 | case LM95245_REG_RW_CONFIG2: |
| 496 | case LM95245_REG_RW_REMOTE_OFFH: |
| 497 | case LM95245_REG_RW_REMOTE_OFFL: |
| 498 | case LM95245_REG_RW_REMOTE_OS_LIMIT: |
| 499 | case LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT: |
| 500 | case LM95245_REG_RW_REMOTE_TCRIT_LIMIT: |
| 501 | case LM95245_REG_RW_COMMON_HYSTERESIS: |
| 502 | return true; |
| 503 | default: |
| 504 | return false; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 508 | static bool lm95245_is_volatile_reg(struct device *dev, unsigned int reg) |
| 509 | { |
| 510 | switch (reg) { |
| 511 | case LM95245_REG_R_STATUS1: |
| 512 | case LM95245_REG_R_STATUS2: |
| 513 | case LM95245_REG_R_LOCAL_TEMPH_S: |
| 514 | case LM95245_REG_R_LOCAL_TEMPL_S: |
| 515 | case LM95245_REG_R_REMOTE_TEMPH_S: |
| 516 | case LM95245_REG_R_REMOTE_TEMPL_S: |
| 517 | case LM95245_REG_R_REMOTE_TEMPH_U: |
| 518 | case LM95245_REG_R_REMOTE_TEMPL_U: |
| 519 | return true; |
| 520 | default: |
| 521 | return false; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | static const struct regmap_config lm95245_regmap_config = { |
| 526 | .reg_bits = 8, |
| 527 | .val_bits = 8, |
| 528 | .writeable_reg = lm95245_is_writeable_reg, |
| 529 | .volatile_reg = lm95245_is_volatile_reg, |
| 530 | .cache_type = REGCACHE_RBTREE, |
David Frey | 1c96a2f | 2018-09-01 09:50:41 -0700 | [diff] [blame] | 531 | .use_single_read = true, |
| 532 | .use_single_write = true, |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 533 | }; |
| 534 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 535 | static const struct hwmon_channel_info *lm95245_info[] = { |
Guenter Roeck | 7aea006 | 2019-03-31 10:53:48 -0700 | [diff] [blame] | 536 | HWMON_CHANNEL_INFO(chip, |
| 537 | HWMON_C_UPDATE_INTERVAL), |
| 538 | HWMON_CHANNEL_INFO(temp, |
| 539 | HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_HYST | |
| 540 | HWMON_T_CRIT_ALARM, |
| 541 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST | |
| 542 | HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_FAULT | |
| 543 | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | |
| 544 | HWMON_T_TYPE | HWMON_T_OFFSET), |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 545 | NULL |
| 546 | }; |
| 547 | |
| 548 | static const struct hwmon_ops lm95245_hwmon_ops = { |
| 549 | .is_visible = lm95245_is_visible, |
| 550 | .read = lm95245_read, |
| 551 | .write = lm95245_write, |
| 552 | }; |
| 553 | |
| 554 | static const struct hwmon_chip_info lm95245_chip_info = { |
| 555 | .ops = &lm95245_hwmon_ops, |
| 556 | .info = lm95245_info, |
| 557 | }; |
| 558 | |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 559 | static int lm95245_probe(struct i2c_client *client, |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 560 | const struct i2c_device_id *id) |
| 561 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 562 | struct device *dev = &client->dev; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 563 | struct lm95245_data *data; |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 564 | struct device *hwmon_dev; |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 565 | int ret; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 566 | |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 567 | data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL); |
Guenter Roeck | a8dd946 | 2012-06-02 09:58:12 -0700 | [diff] [blame] | 568 | if (!data) |
| 569 | return -ENOMEM; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 570 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 571 | data->regmap = devm_regmap_init_i2c(client, &lm95245_regmap_config); |
| 572 | if (IS_ERR(data->regmap)) |
| 573 | return PTR_ERR(data->regmap); |
| 574 | |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 575 | mutex_init(&data->update_lock); |
| 576 | |
| 577 | /* Initialize the LM95245 chip */ |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 578 | ret = lm95245_init_client(data); |
| 579 | if (ret < 0) |
| 580 | return ret; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 581 | |
Guenter Roeck | c0a4b9e | 2016-07-05 19:10:52 -0700 | [diff] [blame] | 582 | hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, |
| 583 | data, |
| 584 | &lm95245_chip_info, |
| 585 | NULL); |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 586 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /* Driver data (common to all clients) */ |
| 590 | static const struct i2c_device_id lm95245_id[] = { |
Guenter Roeck | 162a8df | 2014-04-22 08:48:57 -0700 | [diff] [blame] | 591 | { "lm95235", 0 }, |
| 592 | { "lm95245", 0 }, |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 593 | { } |
| 594 | }; |
| 595 | MODULE_DEVICE_TABLE(i2c, lm95245_id); |
| 596 | |
Guenter Roeck | 0e09e9f | 2019-04-04 07:52:05 -0700 | [diff] [blame] | 597 | static const struct of_device_id __maybe_unused lm95245_of_match[] = { |
Javier Martinez Canillas | 5ada705 | 2017-02-24 10:13:05 -0300 | [diff] [blame] | 598 | { .compatible = "national,lm95235" }, |
| 599 | { .compatible = "national,lm95245" }, |
| 600 | { }, |
| 601 | }; |
| 602 | MODULE_DEVICE_TABLE(of, lm95245_of_match); |
| 603 | |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 604 | static struct i2c_driver lm95245_driver = { |
| 605 | .class = I2C_CLASS_HWMON, |
| 606 | .driver = { |
Guenter Roeck | 162a8df | 2014-04-22 08:48:57 -0700 | [diff] [blame] | 607 | .name = "lm95245", |
Javier Martinez Canillas | 5ada705 | 2017-02-24 10:13:05 -0300 | [diff] [blame] | 608 | .of_match_table = of_match_ptr(lm95245_of_match), |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 609 | }, |
| 610 | .probe = lm95245_probe, |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 611 | .id_table = lm95245_id, |
| 612 | .detect = lm95245_detect, |
| 613 | .address_list = normal_i2c, |
| 614 | }; |
| 615 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 616 | module_i2c_driver(lm95245_driver); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 617 | |
| 618 | MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>"); |
Guenter Roeck | 162a8df | 2014-04-22 08:48:57 -0700 | [diff] [blame] | 619 | MODULE_DESCRIPTION("LM95235/LM95245 sensor driver"); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 620 | MODULE_LICENSE("GPL"); |