| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Driver for |
| 4 |
* Maxim MAX16065/MAX16066 12-Channel/8-Channel, Flash-Configurable |
| 5 |
* System Managers with Nonvolatile Fault Registers |
| 6 |
* Maxim MAX16067/MAX16068 6-Channel, Flash-Configurable System Managers |
| 7 |
* with Nonvolatile Fault Registers |
| 8 |
* Maxim MAX16070/MAX16071 12-Channel/8-Channel, Flash-Configurable System |
| 9 |
* Monitors with Nonvolatile Fault Registers |
| 10 |
* |
| 11 |
* Copyright (C) 2011 Ericsson AB. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <linux/kernel.h> |
| 15 |
#include <linux/module.h> |
| 16 |
#include <linux/init.h> |
| 17 |
#include <linux/err.h> |
| 18 |
#include <linux/slab.h> |
| 19 |
#include <linux/i2c.h> |
| 20 |
#include <linux/hwmon.h> |
| 21 |
#include <linux/hwmon-sysfs.h> |
| 22 |
#include <linux/jiffies.h> |
| 23 |
|
| 24 |
enum chips { max16065, max16066, max16067, max16068, max16070, max16071 }; |
| 25 |
|
| 26 |
/* |
| 27 |
* Registers |
| 28 |
*/ |
| 29 |
#define MAX16065_ADC(x) ((x) * 2) |
| 30 |
|
| 31 |
#define MAX16065_CURR_SENSE 0x18 |
| 32 |
#define MAX16065_CSP_ADC 0x19 |
| 33 |
#define MAX16065_FAULT(x) (0x1b + (x)) |
| 34 |
#define MAX16065_SCALE(x) (0x43 + (x)) |
| 35 |
#define MAX16065_CURR_CONTROL 0x47 |
| 36 |
#define MAX16065_LIMIT(l, x) (0x48 + (l) + (x) * 3) /* |
| 37 |
* l: limit |
| 38 |
* 0: min/max |
| 39 |
* 1: crit |
| 40 |
* 2: lcrit |
| 41 |
* x: ADC index |
| 42 |
*/ |
| 43 |
|
| 44 |
#define MAX16065_SW_ENABLE 0x73 |
| 45 |
|
| 46 |
#define MAX16065_WARNING_OV (1 << 3) /* Set if secondary threshold is OV |
| 47 |
warning */ |
| 48 |
|
| 49 |
#define MAX16065_CURR_ENABLE (1 << 0) |
| 50 |
|
| 51 |
#define MAX16065_NUM_LIMIT 3 |
| 52 |
#define MAX16065_NUM_ADC 12 /* maximum number of ADC channels */ |
| 53 |
|
| 54 |
static const int max16065_num_adc[] = { |
| 55 |
[max16065] = 12, |
| 56 |
[max16066] = 8, |
| 57 |
[max16067] = 6, |
| 58 |
[max16068] = 6, |
| 59 |
[max16070] = 12, |
| 60 |
[max16071] = 8, |
| 61 |
}; |
| 62 |
|
| 63 |
static const bool max16065_have_secondary[] = { |
| 64 |
[max16065] = true, |
| 65 |
[max16066] = true, |
| 66 |
[max16067] = false, |
| 67 |
[max16068] = false, |
| 68 |
[max16070] = true, |
| 69 |
[max16071] = true, |
| 70 |
}; |
| 71 |
|
| 72 |
static const bool max16065_have_current[] = { |
| 73 |
[max16065] = true, |
| 74 |
[max16066] = true, |
| 75 |
[max16067] = false, |
| 76 |
[max16068] = false, |
| 77 |
[max16070] = true, |
| 78 |
[max16071] = true, |
| 79 |
}; |
| 80 |
|
| 81 |
struct max16065_data { |
| 82 |
enum chips chip; |
| 83 |
struct i2c_client *client; |
| 84 |
const struct attribute_group *groups[4]; |
| 85 |
struct mutex update_lock; |
| 86 |
bool valid; |
| 87 |
unsigned long last_updated; /* in jiffies */ |
| 88 |
int num_adc; |
| 89 |
bool have_current; |
| 90 |
int curr_gain; |
| 91 |
/* limits are in mV */ |
| 92 |
int limit[MAX16065_NUM_LIMIT][MAX16065_NUM_ADC]; |
| 93 |
int range[MAX16065_NUM_ADC + 1];/* voltage range */ |
| 94 |
int adc[MAX16065_NUM_ADC + 1]; /* adc values (raw) including csp_adc */ |
| 95 |
int curr_sense; |
| 96 |
int fault[2]; |
| 97 |
}; |
| 98 |
|
| 99 |
static const int max16065_adc_range[] = { 5560, 2780, 1390, 0 }; |
| 100 |
static const int max16065_csp_adc_range[] = { 7000, 14000 }; |
| 101 |
|
| 102 |
/* ADC registers have 10 bit resolution. */ |
| 103 |
static inline int ADC_TO_MV(int adc, int range) |
| 104 |
{ |
| 105 |
return (adc * range) / 1024; |
| 106 |
} |
| 107 |
|
| 108 |
/* |
| 109 |
* Limit registers have 8 bit resolution and match upper 8 bits of ADC |
| 110 |
* registers. |
| 111 |
*/ |
| 112 |
static inline int LIMIT_TO_MV(int limit, int range) |
| 113 |
{ |
| 114 |
return limit * range / 256; |
| 115 |
} |
| 116 |
|
| 117 |
static inline int MV_TO_LIMIT(unsigned long mv, int range) |
| 118 |
{ |
| 119 |
mv = clamp_val(mv, 0, ULONG_MAX / 256); |
| 120 |
return DIV_ROUND_CLOSEST(clamp_val(mv * 256, 0, range * 255), range); |
| 121 |
} |
| 122 |
|
| 123 |
static inline int ADC_TO_CURR(int adc, int gain) |
| 124 |
{ |
| 125 |
return adc * 1400000 / (gain * 255); |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
* max16065_read_adc() |
| 130 |
* |
| 131 |
* Read 16 bit value from <reg>, <reg+1>. |
| 132 |
* Upper 8 bits are in <reg>, lower 2 bits are in bits 7:6 of <reg+1>. |
| 133 |
*/ |
| 134 |
static int max16065_read_adc(struct i2c_client *client, int reg) |
| 135 |
{ |
| 136 |
int rv; |
| 137 |
|
| 138 |
rv = i2c_smbus_read_word_swapped(client, reg); |
| 139 |
if (unlikely(rv < 0)) |
| 140 |
return rv; |
| 141 |
return rv >> 6; |
| 142 |
} |
| 143 |
|
| 144 |
static struct max16065_data *max16065_update_device(struct device *dev) |
| 145 |
{ |
| 146 |
struct max16065_data *data = dev_get_drvdata(dev); |
| 147 |
struct i2c_client *client = data->client; |
| 148 |
|
| 149 |
mutex_lock(&data->update_lock); |
| 150 |
if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 151 |
int i; |
| 152 |
|
| 153 |
for (i = 0; i < data->num_adc; i++) |
| 154 |
WRITE_ONCE(data->adc[i], |
| 155 |
max16065_read_adc(client, MAX16065_ADC(i))); |
| 156 |
|
| 157 |
if (data->have_current) { |
| 158 |
WRITE_ONCE(data->adc[MAX16065_NUM_ADC], |
| 159 |
max16065_read_adc(client, MAX16065_CSP_ADC)); |
| 160 |
WRITE_ONCE(data->curr_sense, |
| 161 |
i2c_smbus_read_byte_data(client, MAX16065_CURR_SENSE)); |
| 162 |
} |
| 163 |
|
| 164 |
for (i = 0; i < 2; i++) |
| 165 |
WRITE_ONCE(data->fault[i], |
| 166 |
i2c_smbus_read_byte_data(client, MAX16065_FAULT(i))); |
| 167 |
|
| 168 |
/* |
| 169 |
* MAX16067 and MAX16068 have separate undervoltage and |
| 170 |
* overvoltage alarm bits. Squash them together. |
| 171 |
*/ |
| 172 |
if (data->chip == max16067 || data->chip == max16068) |
| 173 |
WRITE_ONCE(data->fault[0], |
| 174 |
data->fault[0] | data->fault[1]); |
| 175 |
|
| 176 |
data->last_updated = jiffies; |
| 177 |
data->valid = true; |
| 178 |
} |
| 179 |
mutex_unlock(&data->update_lock); |
| 180 |
return data; |
| 181 |
} |
| 182 |
|
| 183 |
static ssize_t max16065_alarm_show(struct device *dev, |
| 184 |
struct device_attribute *da, char *buf) |
| 185 |
{ |
| 186 |
struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da); |
| 187 |
struct max16065_data *data = max16065_update_device(dev); |
| 188 |
int val = READ_ONCE(data->fault[attr2->nr]); |
| 189 |
|
| 190 |
if (val < 0) |
| 191 |
return val; |
| 192 |
|
| 193 |
val &= (1 << attr2->index); |
| 194 |
if (val) |
| 195 |
i2c_smbus_write_byte_data(data->client, |
| 196 |
MAX16065_FAULT(attr2->nr), val); |
| 197 |
|
| 198 |
return sysfs_emit(buf, "%d\n", !!val); |
| 199 |
} |
| 200 |
|
| 201 |
static ssize_t max16065_input_show(struct device *dev, |
| 202 |
struct device_attribute *da, char *buf) |
| 203 |
{ |
| 204 |
struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 205 |
struct max16065_data *data = max16065_update_device(dev); |
| 206 |
int adc = READ_ONCE(data->adc[attr->index]); |
| 207 |
|
| 208 |
if (unlikely(adc < 0)) |
| 209 |
return adc; |
| 210 |
|
| 211 |
return sysfs_emit(buf, "%d\n", |
| 212 |
ADC_TO_MV(adc, data->range[attr->index])); |
| 213 |
} |
| 214 |
|
| 215 |
static ssize_t max16065_current_show(struct device *dev, |
| 216 |
struct device_attribute *da, char *buf) |
| 217 |
{ |
| 218 |
struct max16065_data *data = max16065_update_device(dev); |
| 219 |
int curr_sense = READ_ONCE(data->curr_sense); |
| 220 |
|
| 221 |
if (unlikely(curr_sense < 0)) |
| 222 |
return curr_sense; |
| 223 |
|
| 224 |
return sysfs_emit(buf, "%d\n", |
| 225 |
ADC_TO_CURR(curr_sense, data->curr_gain)); |
| 226 |
} |
| 227 |
|
| 228 |
static ssize_t max16065_limit_store(struct device *dev, |
| 229 |
struct device_attribute *da, |
| 230 |
const char *buf, size_t count) |
| 231 |
{ |
| 232 |
struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da); |
| 233 |
struct max16065_data *data = dev_get_drvdata(dev); |
| 234 |
unsigned long val; |
| 235 |
int err; |
| 236 |
int limit; |
| 237 |
|
| 238 |
err = kstrtoul(buf, 10, &val); |
| 239 |
if (unlikely(err < 0)) |
| 240 |
return err; |
| 241 |
|
| 242 |
limit = MV_TO_LIMIT(val, data->range[attr2->index]); |
| 243 |
|
| 244 |
mutex_lock(&data->update_lock); |
| 245 |
data->limit[attr2->nr][attr2->index] |
| 246 |
= LIMIT_TO_MV(limit, data->range[attr2->index]); |
| 247 |
i2c_smbus_write_byte_data(data->client, |
| 248 |
MAX16065_LIMIT(attr2->nr, attr2->index), |
| 249 |
limit); |
| 250 |
mutex_unlock(&data->update_lock); |
| 251 |
|
| 252 |
return count; |
| 253 |
} |
| 254 |
|
| 255 |
static ssize_t max16065_limit_show(struct device *dev, |
| 256 |
struct device_attribute *da, char *buf) |
| 257 |
{ |
| 258 |
struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da); |
| 259 |
struct max16065_data *data = dev_get_drvdata(dev); |
| 260 |
|
| 261 |
return sysfs_emit(buf, "%d\n", |
| 262 |
data->limit[attr2->nr][attr2->index]); |
| 263 |
} |
| 264 |
|
| 265 |
/* Construct a sensor_device_attribute structure for each register */ |
| 266 |
|
| 267 |
/* Input voltages */ |
| 268 |
static SENSOR_DEVICE_ATTR_RO(in0_input, max16065_input, 0); |
| 269 |
static SENSOR_DEVICE_ATTR_RO(in1_input, max16065_input, 1); |
| 270 |
static SENSOR_DEVICE_ATTR_RO(in2_input, max16065_input, 2); |
| 271 |
static SENSOR_DEVICE_ATTR_RO(in3_input, max16065_input, 3); |
| 272 |
static SENSOR_DEVICE_ATTR_RO(in4_input, max16065_input, 4); |
| 273 |
static SENSOR_DEVICE_ATTR_RO(in5_input, max16065_input, 5); |
| 274 |
static SENSOR_DEVICE_ATTR_RO(in6_input, max16065_input, 6); |
| 275 |
static SENSOR_DEVICE_ATTR_RO(in7_input, max16065_input, 7); |
| 276 |
static SENSOR_DEVICE_ATTR_RO(in8_input, max16065_input, 8); |
| 277 |
static SENSOR_DEVICE_ATTR_RO(in9_input, max16065_input, 9); |
| 278 |
static SENSOR_DEVICE_ATTR_RO(in10_input, max16065_input, 10); |
| 279 |
static SENSOR_DEVICE_ATTR_RO(in11_input, max16065_input, 11); |
| 280 |
static SENSOR_DEVICE_ATTR_RO(in12_input, max16065_input, 12); |
| 281 |
|
| 282 |
/* Input voltages lcrit */ |
| 283 |
static SENSOR_DEVICE_ATTR_2_RW(in0_lcrit, max16065_limit, 2, 0); |
| 284 |
static SENSOR_DEVICE_ATTR_2_RW(in1_lcrit, max16065_limit, 2, 1); |
| 285 |
static SENSOR_DEVICE_ATTR_2_RW(in2_lcrit, max16065_limit, 2, 2); |
| 286 |
static SENSOR_DEVICE_ATTR_2_RW(in3_lcrit, max16065_limit, 2, 3); |
| 287 |
static SENSOR_DEVICE_ATTR_2_RW(in4_lcrit, max16065_limit, 2, 4); |
| 288 |
static SENSOR_DEVICE_ATTR_2_RW(in5_lcrit, max16065_limit, 2, 5); |
| 289 |
static SENSOR_DEVICE_ATTR_2_RW(in6_lcrit, max16065_limit, 2, 6); |
| 290 |
static SENSOR_DEVICE_ATTR_2_RW(in7_lcrit, max16065_limit, 2, 7); |
| 291 |
static SENSOR_DEVICE_ATTR_2_RW(in8_lcrit, max16065_limit, 2, 8); |
| 292 |
static SENSOR_DEVICE_ATTR_2_RW(in9_lcrit, max16065_limit, 2, 9); |
| 293 |
static SENSOR_DEVICE_ATTR_2_RW(in10_lcrit, max16065_limit, 2, 10); |
| 294 |
static SENSOR_DEVICE_ATTR_2_RW(in11_lcrit, max16065_limit, 2, 11); |
| 295 |
|
| 296 |
/* Input voltages crit */ |
| 297 |
static SENSOR_DEVICE_ATTR_2_RW(in0_crit, max16065_limit, 1, 0); |
| 298 |
static SENSOR_DEVICE_ATTR_2_RW(in1_crit, max16065_limit, 1, 1); |
| 299 |
static SENSOR_DEVICE_ATTR_2_RW(in2_crit, max16065_limit, 1, 2); |
| 300 |
static SENSOR_DEVICE_ATTR_2_RW(in3_crit, max16065_limit, 1, 3); |
| 301 |
static SENSOR_DEVICE_ATTR_2_RW(in4_crit, max16065_limit, 1, 4); |
| 302 |
static SENSOR_DEVICE_ATTR_2_RW(in5_crit, max16065_limit, 1, 5); |
| 303 |
static SENSOR_DEVICE_ATTR_2_RW(in6_crit, max16065_limit, 1, 6); |
| 304 |
static SENSOR_DEVICE_ATTR_2_RW(in7_crit, max16065_limit, 1, 7); |
| 305 |
static SENSOR_DEVICE_ATTR_2_RW(in8_crit, max16065_limit, 1, 8); |
| 306 |
static SENSOR_DEVICE_ATTR_2_RW(in9_crit, max16065_limit, 1, 9); |
| 307 |
static SENSOR_DEVICE_ATTR_2_RW(in10_crit, max16065_limit, 1, 10); |
| 308 |
static SENSOR_DEVICE_ATTR_2_RW(in11_crit, max16065_limit, 1, 11); |
| 309 |
|
| 310 |
/* Input voltages min */ |
| 311 |
static SENSOR_DEVICE_ATTR_2_RW(in0_min, max16065_limit, 0, 0); |
| 312 |
static SENSOR_DEVICE_ATTR_2_RW(in1_min, max16065_limit, 0, 1); |
| 313 |
static SENSOR_DEVICE_ATTR_2_RW(in2_min, max16065_limit, 0, 2); |
| 314 |
static SENSOR_DEVICE_ATTR_2_RW(in3_min, max16065_limit, 0, 3); |
| 315 |
static SENSOR_DEVICE_ATTR_2_RW(in4_min, max16065_limit, 0, 4); |
| 316 |
static SENSOR_DEVICE_ATTR_2_RW(in5_min, max16065_limit, 0, 5); |
| 317 |
static SENSOR_DEVICE_ATTR_2_RW(in6_min, max16065_limit, 0, 6); |
| 318 |
static SENSOR_DEVICE_ATTR_2_RW(in7_min, max16065_limit, 0, 7); |
| 319 |
static SENSOR_DEVICE_ATTR_2_RW(in8_min, max16065_limit, 0, 8); |
| 320 |
static SENSOR_DEVICE_ATTR_2_RW(in9_min, max16065_limit, 0, 9); |
| 321 |
static SENSOR_DEVICE_ATTR_2_RW(in10_min, max16065_limit, 0, 10); |
| 322 |
static SENSOR_DEVICE_ATTR_2_RW(in11_min, max16065_limit, 0, 11); |
| 323 |
|
| 324 |
/* Input voltages max */ |
| 325 |
static SENSOR_DEVICE_ATTR_2_RW(in0_max, max16065_limit, 0, 0); |
| 326 |
static SENSOR_DEVICE_ATTR_2_RW(in1_max, max16065_limit, 0, 1); |
| 327 |
static SENSOR_DEVICE_ATTR_2_RW(in2_max, max16065_limit, 0, 2); |
| 328 |
static SENSOR_DEVICE_ATTR_2_RW(in3_max, max16065_limit, 0, 3); |
| 329 |
static SENSOR_DEVICE_ATTR_2_RW(in4_max, max16065_limit, 0, 4); |
| 330 |
static SENSOR_DEVICE_ATTR_2_RW(in5_max, max16065_limit, 0, 5); |
| 331 |
static SENSOR_DEVICE_ATTR_2_RW(in6_max, max16065_limit, 0, 6); |
| 332 |
static SENSOR_DEVICE_ATTR_2_RW(in7_max, max16065_limit, 0, 7); |
| 333 |
static SENSOR_DEVICE_ATTR_2_RW(in8_max, max16065_limit, 0, 8); |
| 334 |
static SENSOR_DEVICE_ATTR_2_RW(in9_max, max16065_limit, 0, 9); |
| 335 |
static SENSOR_DEVICE_ATTR_2_RW(in10_max, max16065_limit, 0, 10); |
| 336 |
static SENSOR_DEVICE_ATTR_2_RW(in11_max, max16065_limit, 0, 11); |
| 337 |
|
| 338 |
/* alarms */ |
| 339 |
static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, max16065_alarm, 0, 0); |
| 340 |
static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, max16065_alarm, 0, 1); |
| 341 |
static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, max16065_alarm, 0, 2); |
| 342 |
static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, max16065_alarm, 0, 3); |
| 343 |
static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, max16065_alarm, 0, 4); |
| 344 |
static SENSOR_DEVICE_ATTR_2_RO(in5_alarm, max16065_alarm, 0, 5); |
| 345 |
static SENSOR_DEVICE_ATTR_2_RO(in6_alarm, max16065_alarm, 0, 6); |
| 346 |
static SENSOR_DEVICE_ATTR_2_RO(in7_alarm, max16065_alarm, 0, 7); |
| 347 |
static SENSOR_DEVICE_ATTR_2_RO(in8_alarm, max16065_alarm, 1, 0); |
| 348 |
static SENSOR_DEVICE_ATTR_2_RO(in9_alarm, max16065_alarm, 1, 1); |
| 349 |
static SENSOR_DEVICE_ATTR_2_RO(in10_alarm, max16065_alarm, 1, 2); |
| 350 |
static SENSOR_DEVICE_ATTR_2_RO(in11_alarm, max16065_alarm, 1, 3); |
| 351 |
|
| 352 |
/* Current and alarm */ |
| 353 |
static SENSOR_DEVICE_ATTR_RO(curr1_input, max16065_current, 0); |
| 354 |
static SENSOR_DEVICE_ATTR_2_RO(curr1_alarm, max16065_alarm, 1, 4); |
| 355 |
|
| 356 |
/* |
| 357 |
* Finally, construct an array of pointers to members of the above objects, |
| 358 |
* as required for sysfs_create_group() |
| 359 |
*/ |
| 360 |
static struct attribute *max16065_basic_attributes[] = { |
| 361 |
&sensor_dev_attr_in0_input.dev_attr.attr, |
| 362 |
&sensor_dev_attr_in0_lcrit.dev_attr.attr, |
| 363 |
&sensor_dev_attr_in0_crit.dev_attr.attr, |
| 364 |
&sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 365 |
|
| 366 |
&sensor_dev_attr_in1_input.dev_attr.attr, |
| 367 |
&sensor_dev_attr_in1_lcrit.dev_attr.attr, |
| 368 |
&sensor_dev_attr_in1_crit.dev_attr.attr, |
| 369 |
&sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 370 |
|
| 371 |
&sensor_dev_attr_in2_input.dev_attr.attr, |
| 372 |
&sensor_dev_attr_in2_lcrit.dev_attr.attr, |
| 373 |
&sensor_dev_attr_in2_crit.dev_attr.attr, |
| 374 |
&sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 375 |
|
| 376 |
&sensor_dev_attr_in3_input.dev_attr.attr, |
| 377 |
&sensor_dev_attr_in3_lcrit.dev_attr.attr, |
| 378 |
&sensor_dev_attr_in3_crit.dev_attr.attr, |
| 379 |
&sensor_dev_attr_in3_alarm.dev_attr.attr, |
| 380 |
|
| 381 |
&sensor_dev_attr_in4_input.dev_attr.attr, |
| 382 |
&sensor_dev_attr_in4_lcrit.dev_attr.attr, |
| 383 |
&sensor_dev_attr_in4_crit.dev_attr.attr, |
| 384 |
&sensor_dev_attr_in4_alarm.dev_attr.attr, |
| 385 |
|
| 386 |
&sensor_dev_attr_in5_input.dev_attr.attr, |
| 387 |
&sensor_dev_attr_in5_lcrit.dev_attr.attr, |
| 388 |
&sensor_dev_attr_in5_crit.dev_attr.attr, |
| 389 |
&sensor_dev_attr_in5_alarm.dev_attr.attr, |
| 390 |
|
| 391 |
&sensor_dev_attr_in6_input.dev_attr.attr, |
| 392 |
&sensor_dev_attr_in6_lcrit.dev_attr.attr, |
| 393 |
&sensor_dev_attr_in6_crit.dev_attr.attr, |
| 394 |
&sensor_dev_attr_in6_alarm.dev_attr.attr, |
| 395 |
|
| 396 |
&sensor_dev_attr_in7_input.dev_attr.attr, |
| 397 |
&sensor_dev_attr_in7_lcrit.dev_attr.attr, |
| 398 |
&sensor_dev_attr_in7_crit.dev_attr.attr, |
| 399 |
&sensor_dev_attr_in7_alarm.dev_attr.attr, |
| 400 |
|
| 401 |
&sensor_dev_attr_in8_input.dev_attr.attr, |
| 402 |
&sensor_dev_attr_in8_lcrit.dev_attr.attr, |
| 403 |
&sensor_dev_attr_in8_crit.dev_attr.attr, |
| 404 |
&sensor_dev_attr_in8_alarm.dev_attr.attr, |
| 405 |
|
| 406 |
&sensor_dev_attr_in9_input.dev_attr.attr, |
| 407 |
&sensor_dev_attr_in9_lcrit.dev_attr.attr, |
| 408 |
&sensor_dev_attr_in9_crit.dev_attr.attr, |
| 409 |
&sensor_dev_attr_in9_alarm.dev_attr.attr, |
| 410 |
|
| 411 |
&sensor_dev_attr_in10_input.dev_attr.attr, |
| 412 |
&sensor_dev_attr_in10_lcrit.dev_attr.attr, |
| 413 |
&sensor_dev_attr_in10_crit.dev_attr.attr, |
| 414 |
&sensor_dev_attr_in10_alarm.dev_attr.attr, |
| 415 |
|
| 416 |
&sensor_dev_attr_in11_input.dev_attr.attr, |
| 417 |
&sensor_dev_attr_in11_lcrit.dev_attr.attr, |
| 418 |
&sensor_dev_attr_in11_crit.dev_attr.attr, |
| 419 |
&sensor_dev_attr_in11_alarm.dev_attr.attr, |
| 420 |
|
| 421 |
NULL |
| 422 |
}; |
| 423 |
|
| 424 |
static struct attribute *max16065_current_attributes[] = { |
| 425 |
&sensor_dev_attr_in12_input.dev_attr.attr, |
| 426 |
&sensor_dev_attr_curr1_input.dev_attr.attr, |
| 427 |
&sensor_dev_attr_curr1_alarm.dev_attr.attr, |
| 428 |
NULL |
| 429 |
}; |
| 430 |
|
| 431 |
static struct attribute *max16065_min_attributes[] = { |
| 432 |
&sensor_dev_attr_in0_min.dev_attr.attr, |
| 433 |
&sensor_dev_attr_in1_min.dev_attr.attr, |
| 434 |
&sensor_dev_attr_in2_min.dev_attr.attr, |
| 435 |
&sensor_dev_attr_in3_min.dev_attr.attr, |
| 436 |
&sensor_dev_attr_in4_min.dev_attr.attr, |
| 437 |
&sensor_dev_attr_in5_min.dev_attr.attr, |
| 438 |
&sensor_dev_attr_in6_min.dev_attr.attr, |
| 439 |
&sensor_dev_attr_in7_min.dev_attr.attr, |
| 440 |
&sensor_dev_attr_in8_min.dev_attr.attr, |
| 441 |
&sensor_dev_attr_in9_min.dev_attr.attr, |
| 442 |
&sensor_dev_attr_in10_min.dev_attr.attr, |
| 443 |
&sensor_dev_attr_in11_min.dev_attr.attr, |
| 444 |
NULL |
| 445 |
}; |
| 446 |
|
| 447 |
static struct attribute *max16065_max_attributes[] = { |
| 448 |
&sensor_dev_attr_in0_max.dev_attr.attr, |
| 449 |
&sensor_dev_attr_in1_max.dev_attr.attr, |
| 450 |
&sensor_dev_attr_in2_max.dev_attr.attr, |
| 451 |
&sensor_dev_attr_in3_max.dev_attr.attr, |
| 452 |
&sensor_dev_attr_in4_max.dev_attr.attr, |
| 453 |
&sensor_dev_attr_in5_max.dev_attr.attr, |
| 454 |
&sensor_dev_attr_in6_max.dev_attr.attr, |
| 455 |
&sensor_dev_attr_in7_max.dev_attr.attr, |
| 456 |
&sensor_dev_attr_in8_max.dev_attr.attr, |
| 457 |
&sensor_dev_attr_in9_max.dev_attr.attr, |
| 458 |
&sensor_dev_attr_in10_max.dev_attr.attr, |
| 459 |
&sensor_dev_attr_in11_max.dev_attr.attr, |
| 460 |
NULL |
| 461 |
}; |
| 462 |
|
| 463 |
static umode_t max16065_basic_is_visible(struct kobject *kobj, |
| 464 |
struct attribute *a, int n) |
| 465 |
{ |
| 466 |
struct device *dev = kobj_to_dev(kobj); |
| 467 |
struct max16065_data *data = dev_get_drvdata(dev); |
| 468 |
int index = n / 4; |
| 469 |
|
| 470 |
if (index >= data->num_adc || !data->range[index]) |
| 471 |
return 0; |
| 472 |
return a->mode; |
| 473 |
} |
| 474 |
|
| 475 |
static umode_t max16065_secondary_is_visible(struct kobject *kobj, |
| 476 |
struct attribute *a, int index) |
| 477 |
{ |
| 478 |
struct device *dev = kobj_to_dev(kobj); |
| 479 |
struct max16065_data *data = dev_get_drvdata(dev); |
| 480 |
|
| 481 |
if (index >= data->num_adc) |
| 482 |
return 0; |
| 483 |
return a->mode; |
| 484 |
} |
| 485 |
|
| 486 |
static const struct attribute_group max16065_basic_group = { |
| 487 |
.attrs = max16065_basic_attributes, |
| 488 |
.is_visible = max16065_basic_is_visible, |
| 489 |
}; |
| 490 |
|
| 491 |
static const struct attribute_group max16065_current_group = { |
| 492 |
.attrs = max16065_current_attributes, |
| 493 |
}; |
| 494 |
|
| 495 |
static const struct attribute_group max16065_min_group = { |
| 496 |
.attrs = max16065_min_attributes, |
| 497 |
.is_visible = max16065_secondary_is_visible, |
| 498 |
}; |
| 499 |
|
| 500 |
static const struct attribute_group max16065_max_group = { |
| 501 |
.attrs = max16065_max_attributes, |
| 502 |
.is_visible = max16065_secondary_is_visible, |
| 503 |
}; |
| 504 |
|
| 505 |
static int max16065_probe(struct i2c_client *client) |
| 506 |
{ |
| 507 |
struct i2c_adapter *adapter = client->adapter; |
| 508 |
struct max16065_data *data; |
| 509 |
struct device *dev = &client->dev; |
| 510 |
struct device *hwmon_dev; |
| 511 |
int i, j, val; |
| 512 |
bool have_secondary; /* true if chip has secondary limits */ |
| 513 |
bool secondary_is_max = false; /* secondary limits reflect max */ |
| 514 |
int groups = 0; |
| 515 |
enum chips chip = (uintptr_t)i2c_get_match_data(client); |
| 516 |
|
| 517 |
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
| 518 |
| I2C_FUNC_SMBUS_READ_WORD_DATA)) |
| 519 |
return -ENODEV; |
| 520 |
|
| 521 |
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 522 |
if (unlikely(!data)) |
| 523 |
return -ENOMEM; |
| 524 |
|
| 525 |
data->chip = chip; |
| 526 |
data->client = client; |
| 527 |
mutex_init(&data->update_lock); |
| 528 |
|
| 529 |
data->num_adc = max16065_num_adc[chip]; |
| 530 |
data->have_current = max16065_have_current[chip]; |
| 531 |
have_secondary = max16065_have_secondary[chip]; |
| 532 |
|
| 533 |
if (have_secondary) { |
| 534 |
val = i2c_smbus_read_byte_data(client, MAX16065_SW_ENABLE); |
| 535 |
if (unlikely(val < 0)) |
| 536 |
return val; |
| 537 |
secondary_is_max = val & MAX16065_WARNING_OV; |
| 538 |
} |
| 539 |
|
| 540 |
/* Read scale registers, convert to range */ |
| 541 |
for (i = 0; i < DIV_ROUND_UP(data->num_adc, 4); i++) { |
| 542 |
val = i2c_smbus_read_byte_data(client, MAX16065_SCALE(i)); |
| 543 |
if (unlikely(val < 0)) |
| 544 |
return val; |
| 545 |
for (j = 0; j < 4 && i * 4 + j < data->num_adc; j++) { |
| 546 |
data->range[i * 4 + j] = |
| 547 |
max16065_adc_range[(val >> (j * 2)) & 0x3]; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
/* Read limits */ |
| 552 |
for (i = 0; i < MAX16065_NUM_LIMIT; i++) { |
| 553 |
if (i == 0 && !have_secondary) |
| 554 |
continue; |
| 555 |
|
| 556 |
for (j = 0; j < data->num_adc; j++) { |
| 557 |
val = i2c_smbus_read_byte_data(client, |
| 558 |
MAX16065_LIMIT(i, j)); |
| 559 |
if (unlikely(val < 0)) |
| 560 |
return val; |
| 561 |
data->limit[i][j] = LIMIT_TO_MV(val, data->range[j]); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/* sysfs hooks */ |
| 566 |
data->groups[groups++] = &max16065_basic_group; |
| 567 |
if (have_secondary) |
| 568 |
data->groups[groups++] = secondary_is_max ? |
| 569 |
&max16065_max_group : &max16065_min_group; |
| 570 |
|
| 571 |
if (data->have_current) { |
| 572 |
val = i2c_smbus_read_byte_data(client, MAX16065_CURR_CONTROL); |
| 573 |
if (unlikely(val < 0)) |
| 574 |
return val; |
| 575 |
if (val & MAX16065_CURR_ENABLE) { |
| 576 |
/* |
| 577 |
* Current gain is 6, 12, 24, 48 based on values in |
| 578 |
* bit 2,3. |
| 579 |
*/ |
| 580 |
data->curr_gain = 6 << ((val >> 2) & 0x03); |
| 581 |
data->range[MAX16065_NUM_ADC] |
| 582 |
= max16065_csp_adc_range[(val >> 1) & 0x01]; |
| 583 |
data->groups[groups++] = &max16065_current_group; |
| 584 |
} else { |
| 585 |
data->have_current = false; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 590 |
data, data->groups); |
| 591 |
return PTR_ERR_OR_ZERO(hwmon_dev); |
| 592 |
} |
| 593 |
|
| 594 |
static const struct i2c_device_id max16065_id[] = { |
| 595 |
{ "max16065", max16065 }, |
| 596 |
{ "max16066", max16066 }, |
| 597 |
{ "max16067", max16067 }, |
| 598 |
{ "max16068", max16068 }, |
| 599 |
{ "max16070", max16070 }, |
| 600 |
{ "max16071", max16071 }, |
| 601 |
{ } |
| 602 |
}; |
| 603 |
|
| 604 |
MODULE_DEVICE_TABLE(i2c, max16065_id); |
| 605 |
|
| 606 |
/* This is the driver that will be inserted */ |
| 607 |
static struct i2c_driver max16065_driver = { |
| 608 |
.driver = { |
| 609 |
.name = "max16065", |
| 610 |
}, |
| 611 |
.probe = max16065_probe, |
| 612 |
.id_table = max16065_id, |
| 613 |
}; |
| 614 |
|
| 615 |
module_i2c_driver(max16065_driver); |
| 616 |
|
| 617 |
MODULE_AUTHOR("Guenter Roeck <[email protected]>"); |
| 618 |
MODULE_DESCRIPTION("MAX16065 driver"); |
| 619 |
MODULE_LICENSE("GPL"); |