2 * Hardware monitoring driver for LM25056 / LM25066 / LM5064 / LM5066
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 { lm25056, lm25066, lm5064, lm5066 };
32 #define LM25066_READ_VAUX 0xd0
33 #define LM25066_MFR_READ_IIN 0xd1
34 #define LM25066_MFR_READ_PIN 0xd2
35 #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
36 #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
37 #define LM25066_READ_PIN_PEAK 0xd5
38 #define LM25066_CLEAR_PIN_PEAK 0xd6
39 #define LM25066_DEVICE_SETUP 0xd9
40 #define LM25066_READ_AVG_VIN 0xdc
41 #define LM25066_READ_AVG_VOUT 0xdd
42 #define LM25066_READ_AVG_IIN 0xde
43 #define LM25066_READ_AVG_PIN 0xdf
45 #define LM25066_DEV_SETUP_CL (1 << 4) /* Current limit */
49 #define LM25056_VAUX_OV_WARN_LIMIT 0xe3
50 #define LM25056_VAUX_UV_WARN_LIMIT 0xe4
52 #define LM25056_MFR_STS_VAUX_OV_WARN (1 << 1)
53 #define LM25056_MFR_STS_VAUX_UV_WARN (1 << 0)
59 #define PSC_CURRENT_IN_L (PSC_NUM_CLASSES)
60 #define PSC_POWER_L (PSC_NUM_CLASSES + 1)
62 static struct __coeff lm25066_coeff[4][PSC_NUM_CLASSES + 2] = {
72 [PSC_CURRENT_IN_L] = {
103 [PSC_CURRENT_IN_L] = {
115 [PSC_TEMPERATURE] = {
124 [PSC_VOLTAGE_OUT] = {
132 [PSC_CURRENT_IN_L] = {
144 [PSC_TEMPERATURE] = {
153 [PSC_VOLTAGE_OUT] = {
161 [PSC_CURRENT_IN_L] = {
173 [PSC_TEMPERATURE] = {
179 struct lm25066_data {
181 struct pmbus_driver_info info;
184 #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
186 static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
188 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
189 const struct lm25066_data *data = to_lm25066_data(info);
193 case PMBUS_VIRT_READ_VMON:
194 ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
197 /* Adjust returned value to match VIN coefficients */
200 /* VIN: 6.14 mV VAUX: 293 uV LSB */
201 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
204 /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
205 ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
208 /* VIN: 4.53 mV VAUX: 700 uV LSB */
209 ret = DIV_ROUND_CLOSEST(ret * 70, 453);
212 /* VIN: 2.18 mV VAUX: 725 uV LSB */
213 ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
218 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
221 ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
223 case PMBUS_IIN_OC_WARN_LIMIT:
224 ret = pmbus_read_word_data(client, 0,
225 LM25066_MFR_IIN_OC_WARN_LIMIT);
227 case PMBUS_PIN_OP_WARN_LIMIT:
228 ret = pmbus_read_word_data(client, 0,
229 LM25066_MFR_PIN_OP_WARN_LIMIT);
231 case PMBUS_VIRT_READ_VIN_AVG:
232 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
234 case PMBUS_VIRT_READ_VOUT_AVG:
235 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
237 case PMBUS_VIRT_READ_IIN_AVG:
238 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
240 case PMBUS_VIRT_READ_PIN_AVG:
241 ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
243 case PMBUS_VIRT_READ_PIN_MAX:
244 ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
246 case PMBUS_VIRT_RESET_PIN_HISTORY:
256 static int lm25056_read_word_data(struct i2c_client *client, int page, int reg)
261 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
262 ret = pmbus_read_word_data(client, 0,
263 LM25056_VAUX_UV_WARN_LIMIT);
266 /* Adjust returned value to match VIN coefficients */
267 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
269 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
270 ret = pmbus_read_word_data(client, 0,
271 LM25056_VAUX_OV_WARN_LIMIT);
274 /* Adjust returned value to match VIN coefficients */
275 ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
278 ret = lm25066_read_word_data(client, page, reg);
284 static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
289 case PMBUS_VIRT_STATUS_VMON:
290 ret = pmbus_read_byte_data(client, 0,
291 PMBUS_STATUS_MFR_SPECIFIC);
295 if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
296 s |= PB_VOLTAGE_UV_WARNING;
297 if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
298 s |= PB_VOLTAGE_OV_WARNING;
308 static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
314 case PMBUS_VOUT_UV_WARN_LIMIT:
315 case PMBUS_OT_FAULT_LIMIT:
316 case PMBUS_OT_WARN_LIMIT:
317 case PMBUS_VIN_UV_WARN_LIMIT:
318 case PMBUS_VIN_OV_WARN_LIMIT:
319 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
320 ret = pmbus_write_word_data(client, 0, reg, word);
321 pmbus_clear_cache(client);
323 case PMBUS_IIN_OC_WARN_LIMIT:
324 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
325 ret = pmbus_write_word_data(client, 0,
326 LM25066_MFR_IIN_OC_WARN_LIMIT,
328 pmbus_clear_cache(client);
330 case PMBUS_PIN_OP_WARN_LIMIT:
331 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
332 ret = pmbus_write_word_data(client, 0,
333 LM25066_MFR_PIN_OP_WARN_LIMIT,
335 pmbus_clear_cache(client);
337 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
338 /* Adjust from VIN coefficients (for LM25056) */
339 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
340 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
341 ret = pmbus_write_word_data(client, 0,
342 LM25056_VAUX_UV_WARN_LIMIT, word);
343 pmbus_clear_cache(client);
345 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
346 /* Adjust from VIN coefficients (for LM25056) */
347 word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
348 word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
349 ret = pmbus_write_word_data(client, 0,
350 LM25056_VAUX_OV_WARN_LIMIT, word);
351 pmbus_clear_cache(client);
353 case PMBUS_VIRT_RESET_PIN_HISTORY:
354 ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
363 static int lm25066_probe(struct i2c_client *client,
364 const struct i2c_device_id *id)
367 struct lm25066_data *data;
368 struct pmbus_driver_info *info;
369 struct __coeff *coeff;
371 if (!i2c_check_functionality(client->adapter,
372 I2C_FUNC_SMBUS_READ_BYTE_DATA))
375 data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
380 config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
384 data->id = id->driver_data;
388 info->format[PSC_VOLTAGE_IN] = direct;
389 info->format[PSC_VOLTAGE_OUT] = direct;
390 info->format[PSC_CURRENT_IN] = direct;
391 info->format[PSC_TEMPERATURE] = direct;
392 info->format[PSC_POWER] = direct;
394 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
395 | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
396 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
398 if (data->id == lm25056) {
399 info->func[0] |= PMBUS_HAVE_STATUS_VMON;
400 info->read_word_data = lm25056_read_word_data;
401 info->read_byte_data = lm25056_read_byte_data;
403 info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
404 info->read_word_data = lm25066_read_word_data;
406 info->write_word_data = lm25066_write_word_data;
408 coeff = &lm25066_coeff[data->id][0];
409 info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
410 info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
411 info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
412 info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
413 info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
414 info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
415 info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
416 info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
417 info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
418 info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
419 info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
420 info->b[PSC_POWER] = coeff[PSC_POWER].b;
421 info->R[PSC_POWER] = coeff[PSC_POWER].R;
422 if (config & LM25066_DEV_SETUP_CL) {
423 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
424 info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
426 info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
427 info->m[PSC_POWER] = coeff[PSC_POWER].m;
430 return pmbus_do_probe(client, id, info);
433 static const struct i2c_device_id lm25066_id[] = {
434 {"lm25056", lm25056},
435 {"lm25066", lm25066},
441 MODULE_DEVICE_TABLE(i2c, lm25066_id);
443 /* This is the driver that will be inserted */
444 static struct i2c_driver lm25066_driver = {
448 .probe = lm25066_probe,
449 .remove = pmbus_do_remove,
450 .id_table = lm25066_id,
453 module_i2c_driver(lm25066_driver);
455 MODULE_AUTHOR("Guenter Roeck");
456 MODULE_DESCRIPTION("PMBus driver for LM25056/LM25066/LM5064/LM5066");
457 MODULE_LICENSE("GPL");