2 * Hardware monitoring driver for LTC2974, LTC2978, LTC3880, and LTC3883
4 * Copyright (c) 2011 Ericsson AB.
5 * Copyright (c) 2013 Guenter Roeck
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/err.h>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
30 enum chips
{ ltc2974
, ltc2978
, ltc3880
, ltc3883
};
32 /* Common for all chips */
33 #define LTC2978_MFR_VOUT_PEAK 0xdd
34 #define LTC2978_MFR_VIN_PEAK 0xde
35 #define LTC2978_MFR_TEMPERATURE_PEAK 0xdf
36 #define LTC2978_MFR_SPECIAL_ID 0xe7
38 /* LTC2974 and LTC2978 */
39 #define LTC2978_MFR_VOUT_MIN 0xfb
40 #define LTC2978_MFR_VIN_MIN 0xfc
41 #define LTC2978_MFR_TEMPERATURE_MIN 0xfd
44 #define LTC2974_MFR_IOUT_PEAK 0xd7
45 #define LTC2974_MFR_IOUT_MIN 0xd8
47 /* LTC3880 and LTC3883 */
48 #define LTC3880_MFR_IOUT_PEAK 0xd7
49 #define LTC3880_MFR_CLEAR_PEAKS 0xe3
50 #define LTC3880_MFR_TEMPERATURE2_PEAK 0xf4
53 #define LTC3883_MFR_IIN_PEAK 0xe1
55 #define LTC2974_ID 0x0212
56 #define LTC2978_ID_REV1 0x0121
57 #define LTC2978_ID_REV2 0x0122
58 #define LTC3880_ID 0x4000
59 #define LTC3880_ID_MASK 0xff00
60 #define LTC3883_ID 0x4300
61 #define LTC3883_ID_MASK 0xff00
63 #define LTC2974_NUM_PAGES 4
64 #define LTC2978_NUM_PAGES 8
65 #define LTC3880_NUM_PAGES 2
66 #define LTC3883_NUM_PAGES 1
69 * LTC2978 clears peak data whenever the CLEAR_FAULTS command is executed, which
70 * happens pretty much each time chip data is updated. Raw peak data therefore
71 * does not provide much value. To be able to provide useful peak data, keep an
72 * internal cache of measured peak data, which is only cleared if an explicit
73 * "clear peak" command is executed for the sensor in question.
79 u16 temp_min
[LTC2974_NUM_PAGES
], temp_max
[LTC2974_NUM_PAGES
];
80 u16 vout_min
[LTC2978_NUM_PAGES
], vout_max
[LTC2978_NUM_PAGES
];
81 u16 iout_min
[LTC2974_NUM_PAGES
], iout_max
[LTC2974_NUM_PAGES
];
84 struct pmbus_driver_info info
;
87 #define to_ltc2978_data(x) container_of(x, struct ltc2978_data, info)
89 static inline int lin11_to_val(int data
)
91 s16 e
= ((s16
)data
) >> 11;
92 s32 m
= (((s16
)(data
<< 5)) >> 5);
95 * mantissa is 10 bit + sign, exponent adds up to 15 bit.
96 * Add 6 bit to exponent for maximum accuracy (10 + 15 + 6 = 31).
99 return (e
< 0 ? m
>> -e
: m
<< e
);
102 static int ltc2978_read_word_data_common(struct i2c_client
*client
, int page
,
105 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
106 struct ltc2978_data
*data
= to_ltc2978_data(info
);
110 case PMBUS_VIRT_READ_VIN_MAX
:
111 ret
= pmbus_read_word_data(client
, page
, LTC2978_MFR_VIN_PEAK
);
113 if (lin11_to_val(ret
) > lin11_to_val(data
->vin_max
))
118 case PMBUS_VIRT_READ_VOUT_MAX
:
119 ret
= pmbus_read_word_data(client
, page
, LTC2978_MFR_VOUT_PEAK
);
122 * VOUT is 16 bit unsigned with fixed exponent,
123 * so we can compare it directly
125 if (ret
> data
->vout_max
[page
])
126 data
->vout_max
[page
] = ret
;
127 ret
= data
->vout_max
[page
];
130 case PMBUS_VIRT_READ_TEMP_MAX
:
131 ret
= pmbus_read_word_data(client
, page
,
132 LTC2978_MFR_TEMPERATURE_PEAK
);
134 if (lin11_to_val(ret
)
135 > lin11_to_val(data
->temp_max
[page
]))
136 data
->temp_max
[page
] = ret
;
137 ret
= data
->temp_max
[page
];
140 case PMBUS_VIRT_RESET_VOUT_HISTORY
:
141 case PMBUS_VIRT_RESET_VIN_HISTORY
:
142 case PMBUS_VIRT_RESET_TEMP_HISTORY
:
152 static int ltc2978_read_word_data(struct i2c_client
*client
, int page
, int reg
)
154 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
155 struct ltc2978_data
*data
= to_ltc2978_data(info
);
159 case PMBUS_VIRT_READ_VIN_MIN
:
160 ret
= pmbus_read_word_data(client
, page
, LTC2978_MFR_VIN_MIN
);
162 if (lin11_to_val(ret
) < lin11_to_val(data
->vin_min
))
167 case PMBUS_VIRT_READ_VOUT_MIN
:
168 ret
= pmbus_read_word_data(client
, page
, LTC2978_MFR_VOUT_MIN
);
171 * VOUT_MIN is known to not be supported on some lots
172 * of LTC2978 revision 1, and will return the maximum
173 * possible voltage if read. If VOUT_MAX is valid and
174 * lower than the reading of VOUT_MIN, use it instead.
176 if (data
->vout_max
[page
] && ret
> data
->vout_max
[page
])
177 ret
= data
->vout_max
[page
];
178 if (ret
< data
->vout_min
[page
])
179 data
->vout_min
[page
] = ret
;
180 ret
= data
->vout_min
[page
];
183 case PMBUS_VIRT_READ_TEMP_MIN
:
184 ret
= pmbus_read_word_data(client
, page
,
185 LTC2978_MFR_TEMPERATURE_MIN
);
187 if (lin11_to_val(ret
)
188 < lin11_to_val(data
->temp_min
[page
]))
189 data
->temp_min
[page
] = ret
;
190 ret
= data
->temp_min
[page
];
193 case PMBUS_VIRT_READ_IOUT_MAX
:
194 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
195 case PMBUS_VIRT_READ_TEMP2_MAX
:
196 case PMBUS_VIRT_RESET_TEMP2_HISTORY
:
200 ret
= ltc2978_read_word_data_common(client
, page
, reg
);
206 static int ltc2974_read_word_data(struct i2c_client
*client
, int page
, int reg
)
208 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
209 struct ltc2978_data
*data
= to_ltc2978_data(info
);
213 case PMBUS_VIRT_READ_IOUT_MAX
:
214 ret
= pmbus_read_word_data(client
, page
, LTC2974_MFR_IOUT_PEAK
);
216 if (lin11_to_val(ret
)
217 > lin11_to_val(data
->iout_max
[page
]))
218 data
->iout_max
[page
] = ret
;
219 ret
= data
->iout_max
[page
];
222 case PMBUS_VIRT_READ_IOUT_MIN
:
223 ret
= pmbus_read_word_data(client
, page
, LTC2974_MFR_IOUT_MIN
);
225 if (lin11_to_val(ret
)
226 < lin11_to_val(data
->iout_min
[page
]))
227 data
->iout_min
[page
] = ret
;
228 ret
= data
->iout_min
[page
];
231 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
235 ret
= ltc2978_read_word_data(client
, page
, reg
);
241 static int ltc3880_read_word_data(struct i2c_client
*client
, int page
, int reg
)
243 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
244 struct ltc2978_data
*data
= to_ltc2978_data(info
);
248 case PMBUS_VIRT_READ_IOUT_MAX
:
249 ret
= pmbus_read_word_data(client
, page
, LTC3880_MFR_IOUT_PEAK
);
251 if (lin11_to_val(ret
)
252 > lin11_to_val(data
->iout_max
[page
]))
253 data
->iout_max
[page
] = ret
;
254 ret
= data
->iout_max
[page
];
257 case PMBUS_VIRT_READ_TEMP2_MAX
:
258 ret
= pmbus_read_word_data(client
, page
,
259 LTC3880_MFR_TEMPERATURE2_PEAK
);
261 if (lin11_to_val(ret
) > lin11_to_val(data
->temp2_max
))
262 data
->temp2_max
= ret
;
263 ret
= data
->temp2_max
;
266 case PMBUS_VIRT_READ_VIN_MIN
:
267 case PMBUS_VIRT_READ_VOUT_MIN
:
268 case PMBUS_VIRT_READ_TEMP_MIN
:
271 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
272 case PMBUS_VIRT_RESET_TEMP2_HISTORY
:
276 ret
= ltc2978_read_word_data_common(client
, page
, reg
);
282 static int ltc3883_read_word_data(struct i2c_client
*client
, int page
, int reg
)
284 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
285 struct ltc2978_data
*data
= to_ltc2978_data(info
);
289 case PMBUS_VIRT_READ_IIN_MAX
:
290 ret
= pmbus_read_word_data(client
, page
, LTC3883_MFR_IIN_PEAK
);
292 if (lin11_to_val(ret
)
293 > lin11_to_val(data
->iin_max
))
298 case PMBUS_VIRT_RESET_IIN_HISTORY
:
302 ret
= ltc3880_read_word_data(client
, page
, reg
);
308 static int ltc2978_clear_peaks(struct i2c_client
*client
, int page
,
313 if (id
== ltc3880
|| id
== ltc3883
)
314 ret
= pmbus_write_byte(client
, 0, LTC3880_MFR_CLEAR_PEAKS
);
316 ret
= pmbus_write_byte(client
, page
, PMBUS_CLEAR_FAULTS
);
321 static int ltc2978_write_word_data(struct i2c_client
*client
, int page
,
324 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
325 struct ltc2978_data
*data
= to_ltc2978_data(info
);
329 case PMBUS_VIRT_RESET_IIN_HISTORY
:
330 data
->iin_max
= 0x7c00;
331 ret
= ltc2978_clear_peaks(client
, page
, data
->id
);
333 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
334 data
->iout_max
[page
] = 0x7c00;
335 data
->iout_min
[page
] = 0xfbff;
336 ret
= ltc2978_clear_peaks(client
, page
, data
->id
);
338 case PMBUS_VIRT_RESET_TEMP2_HISTORY
:
339 data
->temp2_max
= 0x7c00;
340 ret
= ltc2978_clear_peaks(client
, page
, data
->id
);
342 case PMBUS_VIRT_RESET_VOUT_HISTORY
:
343 data
->vout_min
[page
] = 0xffff;
344 data
->vout_max
[page
] = 0;
345 ret
= ltc2978_clear_peaks(client
, page
, data
->id
);
347 case PMBUS_VIRT_RESET_VIN_HISTORY
:
348 data
->vin_min
= 0x7bff;
349 data
->vin_max
= 0x7c00;
350 ret
= ltc2978_clear_peaks(client
, page
, data
->id
);
352 case PMBUS_VIRT_RESET_TEMP_HISTORY
:
353 data
->temp_min
[page
] = 0x7bff;
354 data
->temp_max
[page
] = 0x7c00;
355 ret
= ltc2978_clear_peaks(client
, page
, data
->id
);
364 static const struct i2c_device_id ltc2978_id
[] = {
365 {"ltc2974", ltc2974
},
366 {"ltc2978", ltc2978
},
367 {"ltc3880", ltc3880
},
368 {"ltc3883", ltc3883
},
371 MODULE_DEVICE_TABLE(i2c
, ltc2978_id
);
373 static int ltc2978_probe(struct i2c_client
*client
,
374 const struct i2c_device_id
*id
)
377 struct ltc2978_data
*data
;
378 struct pmbus_driver_info
*info
;
380 if (!i2c_check_functionality(client
->adapter
,
381 I2C_FUNC_SMBUS_READ_WORD_DATA
))
384 data
= devm_kzalloc(&client
->dev
, sizeof(struct ltc2978_data
),
389 chip_id
= i2c_smbus_read_word_data(client
, LTC2978_MFR_SPECIAL_ID
);
393 if (chip_id
== LTC2974_ID
) {
395 } else if (chip_id
== LTC2978_ID_REV1
|| chip_id
== LTC2978_ID_REV2
) {
397 } else if ((chip_id
& LTC3880_ID_MASK
) == LTC3880_ID
) {
399 } else if ((chip_id
& LTC3883_ID_MASK
) == LTC3883_ID
) {
402 dev_err(&client
->dev
, "Unsupported chip ID 0x%x\n", chip_id
);
405 if (data
->id
!= id
->driver_data
)
406 dev_warn(&client
->dev
,
407 "Device mismatch: Configured %s, detected %s\n",
409 ltc2978_id
[data
->id
].name
);
412 info
->write_word_data
= ltc2978_write_word_data
;
414 data
->vin_min
= 0x7bff;
415 data
->vin_max
= 0x7c00;
416 for (i
= 0; i
< ARRAY_SIZE(data
->vout_min
); i
++)
417 data
->vout_min
[i
] = 0xffff;
418 for (i
= 0; i
< ARRAY_SIZE(data
->iout_min
); i
++)
419 data
->iout_min
[i
] = 0xfbff;
420 for (i
= 0; i
< ARRAY_SIZE(data
->iout_max
); i
++)
421 data
->iout_max
[i
] = 0x7c00;
422 for (i
= 0; i
< ARRAY_SIZE(data
->temp_min
); i
++)
423 data
->temp_min
[i
] = 0x7bff;
424 for (i
= 0; i
< ARRAY_SIZE(data
->temp_max
); i
++)
425 data
->temp_max
[i
] = 0x7c00;
426 data
->temp2_max
= 0x7c00;
430 info
->read_word_data
= ltc2974_read_word_data
;
431 info
->pages
= LTC2974_NUM_PAGES
;
432 info
->func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_STATUS_INPUT
434 for (i
= 0; i
< info
->pages
; i
++) {
435 info
->func
[i
] |= PMBUS_HAVE_VOUT
436 | PMBUS_HAVE_STATUS_VOUT
| PMBUS_HAVE_POUT
437 | PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
438 | PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
;
442 info
->read_word_data
= ltc2978_read_word_data
;
443 info
->pages
= LTC2978_NUM_PAGES
;
444 info
->func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_STATUS_INPUT
445 | PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
446 | PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
;
447 for (i
= 1; i
< LTC2978_NUM_PAGES
; i
++) {
448 info
->func
[i
] = PMBUS_HAVE_VOUT
449 | PMBUS_HAVE_STATUS_VOUT
;
453 info
->read_word_data
= ltc3880_read_word_data
;
454 info
->pages
= LTC3880_NUM_PAGES
;
455 info
->func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_IIN
456 | PMBUS_HAVE_STATUS_INPUT
457 | PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
458 | PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
459 | PMBUS_HAVE_POUT
| PMBUS_HAVE_TEMP
460 | PMBUS_HAVE_TEMP2
| PMBUS_HAVE_STATUS_TEMP
;
461 info
->func
[1] = PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
462 | PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
464 | PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
;
467 info
->read_word_data
= ltc3883_read_word_data
;
468 info
->pages
= LTC3883_NUM_PAGES
;
469 info
->func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_IIN
470 | PMBUS_HAVE_STATUS_INPUT
471 | PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
472 | PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
473 | PMBUS_HAVE_PIN
| PMBUS_HAVE_POUT
| PMBUS_HAVE_TEMP
474 | PMBUS_HAVE_TEMP2
| PMBUS_HAVE_STATUS_TEMP
;
479 return pmbus_do_probe(client
, id
, info
);
482 /* This is the driver that will be inserted */
483 static struct i2c_driver ltc2978_driver
= {
487 .probe
= ltc2978_probe
,
488 .remove
= pmbus_do_remove
,
489 .id_table
= ltc2978_id
,
492 module_i2c_driver(ltc2978_driver
);
494 MODULE_AUTHOR("Guenter Roeck");
495 MODULE_DESCRIPTION("PMBus driver for LTC2974, LTC2978, LTC3880, and LTC3883");
496 MODULE_LICENSE("GPL");