1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hardware monitoring driver for ZL6100 and compatibles
5 * Copyright (c) 2011 Ericsson AB.
6 * Copyright (c) 2012 Guenter Roeck
9 #include <linux/bitops.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/ktime.h>
17 #include <linux/delay.h>
20 enum chips { zl2004, zl2005, zl2006, zl2008, zl2105, zl2106, zl6100, zl6105,
21 zl8802, zl9101, zl9117, zls1003, zls4009 };
25 ktime_t access; /* chip access time */
26 int delay; /* Delay between chip accesses in uS */
27 struct pmbus_driver_info info;
30 #define to_zl6100_data(x) container_of(x, struct zl6100_data, info)
32 #define ZL6100_MFR_CONFIG 0xd0
33 #define ZL6100_DEVICE_ID 0xe4
35 #define ZL6100_MFR_XTEMP_ENABLE BIT(7)
37 #define ZL8802_MFR_USER_GLOBAL_CONFIG 0xe9
38 #define ZL8802_MFR_TMON_ENABLE BIT(12)
39 #define ZL8802_MFR_USER_CONFIG 0xd1
40 #define ZL8802_MFR_XTEMP_ENABLE_2 BIT(1)
41 #define ZL8802_MFR_DDC_CONFIG 0xd3
42 #define ZL8802_MFR_PHASES_MASK 0x0007
44 #define MFR_VMON_OV_FAULT_LIMIT 0xf5
45 #define MFR_VMON_UV_FAULT_LIMIT 0xf6
46 #define MFR_READ_VMON 0xf7
48 #define VMON_UV_WARNING BIT(5)
49 #define VMON_OV_WARNING BIT(4)
50 #define VMON_UV_FAULT BIT(1)
51 #define VMON_OV_FAULT BIT(0)
53 #define ZL6100_WAIT_TIME 1000 /* uS */
55 static ushort delay = ZL6100_WAIT_TIME;
56 module_param(delay, ushort, 0644);
57 MODULE_PARM_DESC(delay, "Delay between chip accesses in uS");
59 /* Convert linear sensor value to milli-units */
60 static long zl6100_l2d(s16 l)
67 mantissa = ((s16)((l & 0x7ff) << 5)) >> 5;
71 /* scale result to milli-units */
82 #define MAX_MANTISSA (1023 * 1000)
83 #define MIN_MANTISSA (511 * 1000)
85 static u16 zl6100_d2l(long val)
87 s16 exponent = 0, mantissa;
88 bool negative = false;
99 /* Reduce large mantissa until it fits into 10 bit */
100 while (val >= MAX_MANTISSA && exponent < 15) {
104 /* Increase small mantissa to improve precision */
105 while (val < MIN_MANTISSA && exponent > -15) {
110 /* Convert mantissa from milli-units to units */
111 mantissa = DIV_ROUND_CLOSEST(val, 1000);
113 /* Ensure that resulting number is within range */
114 if (mantissa > 0x3ff)
119 mantissa = -mantissa;
121 /* Convert to 5 bit exponent, 11 bit mantissa */
122 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
125 /* Some chips need a delay between accesses */
126 static inline void zl6100_wait(const struct zl6100_data *data)
129 s64 delta = ktime_us_delta(ktime_get(), data->access);
130 if (delta < data->delay)
131 udelay(data->delay - delta);
135 static int zl6100_read_word_data(struct i2c_client *client, int page,
138 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
139 struct zl6100_data *data = to_zl6100_data(info);
142 if (page >= info->pages)
145 if (data->id == zl2005) {
147 * Limit register detection is not reliable on ZL2005.
148 * Make sure registers are not erroneously detected.
151 case PMBUS_VOUT_OV_WARN_LIMIT:
152 case PMBUS_VOUT_UV_WARN_LIMIT:
153 case PMBUS_IOUT_OC_WARN_LIMIT:
159 case PMBUS_VIRT_READ_VMON:
160 vreg = MFR_READ_VMON;
162 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
163 case PMBUS_VIRT_VMON_OV_FAULT_LIMIT:
164 vreg = MFR_VMON_OV_FAULT_LIMIT;
166 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
167 case PMBUS_VIRT_VMON_UV_FAULT_LIMIT:
168 vreg = MFR_VMON_UV_FAULT_LIMIT;
171 if (reg >= PMBUS_VIRT_BASE)
178 ret = pmbus_read_word_data(client, page, phase, vreg);
179 data->access = ktime_get();
184 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
185 ret = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(ret) * 9, 10));
187 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
188 ret = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(ret) * 11, 10));
195 static int zl6100_read_byte_data(struct i2c_client *client, int page, int reg)
197 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
198 struct zl6100_data *data = to_zl6100_data(info);
201 if (page >= info->pages)
207 case PMBUS_VIRT_STATUS_VMON:
208 ret = pmbus_read_byte_data(client, 0,
209 PMBUS_STATUS_MFR_SPECIFIC);
214 if (ret & VMON_UV_WARNING)
215 status |= PB_VOLTAGE_UV_WARNING;
216 if (ret & VMON_OV_WARNING)
217 status |= PB_VOLTAGE_OV_WARNING;
218 if (ret & VMON_UV_FAULT)
219 status |= PB_VOLTAGE_UV_FAULT;
220 if (ret & VMON_OV_FAULT)
221 status |= PB_VOLTAGE_OV_FAULT;
225 ret = pmbus_read_byte_data(client, page, reg);
228 data->access = ktime_get();
233 static int zl6100_write_word_data(struct i2c_client *client, int page, int reg,
236 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
237 struct zl6100_data *data = to_zl6100_data(info);
240 if (page >= info->pages)
244 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
245 word = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(word) * 10, 9));
246 vreg = MFR_VMON_OV_FAULT_LIMIT;
247 pmbus_clear_cache(client);
249 case PMBUS_VIRT_VMON_OV_FAULT_LIMIT:
250 vreg = MFR_VMON_OV_FAULT_LIMIT;
251 pmbus_clear_cache(client);
253 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
254 word = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(word) * 10, 11));
255 vreg = MFR_VMON_UV_FAULT_LIMIT;
256 pmbus_clear_cache(client);
258 case PMBUS_VIRT_VMON_UV_FAULT_LIMIT:
259 vreg = MFR_VMON_UV_FAULT_LIMIT;
260 pmbus_clear_cache(client);
263 if (reg >= PMBUS_VIRT_BASE)
269 ret = pmbus_write_word_data(client, page, vreg, word);
270 data->access = ktime_get();
275 static int zl6100_write_byte(struct i2c_client *client, int page, u8 value)
277 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
278 struct zl6100_data *data = to_zl6100_data(info);
281 if (page >= info->pages)
285 ret = pmbus_write_byte(client, page, value);
286 data->access = ktime_get();
291 static const struct i2c_device_id zl6100_id[] = {
312 {"zls1003", zls1003},
313 {"zls4009", zls4009},
316 MODULE_DEVICE_TABLE(i2c, zl6100_id);
318 static int zl6100_probe(struct i2c_client *client)
321 struct zl6100_data *data;
322 struct pmbus_driver_info *info;
323 u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
324 const struct i2c_device_id *mid;
326 if (!i2c_check_functionality(client->adapter,
327 I2C_FUNC_SMBUS_READ_WORD_DATA
328 | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
331 ret = i2c_smbus_read_block_data(client, ZL6100_DEVICE_ID,
334 dev_err(&client->dev, "Failed to read device ID\n");
337 device_id[ret] = '\0';
338 dev_info(&client->dev, "Device ID %s\n", device_id);
341 for (mid = zl6100_id; mid->name[0]; mid++) {
342 if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
346 dev_err(&client->dev, "Unsupported device\n");
349 if (strcmp(client->name, mid->name) != 0)
350 dev_notice(&client->dev,
351 "Device mismatch: Configured %s, detected %s\n",
352 client->name, mid->name);
354 data = devm_kzalloc(&client->dev, sizeof(struct zl6100_data),
359 data->id = mid->driver_data;
362 * According to information from the chip vendor, all currently
363 * supported chips are known to require a wait time between I2C
369 * Since there was a direct I2C device access above, wait before
370 * accessing the chip again.
372 data->access = ktime_get();
378 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
379 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
380 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
381 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
384 * ZL2004, ZL8802, ZL9101M, ZL9117M and ZLS4009 support monitoring
385 * an extra voltage (VMON for ZL2004, ZL8802 and ZLS4009,
386 * VDRV for ZL9101M and ZL9117M). Report it as vmon.
388 if (data->id == zl2004 || data->id == zl8802 || data->id == zl9101 ||
389 data->id == zl9117 || data->id == zls4009)
390 info->func[0] |= PMBUS_HAVE_VMON | PMBUS_HAVE_STATUS_VMON;
393 * ZL8802 has two outputs that can be used either independently or in
394 * a current sharing configuration. The driver uses the DDC_CONFIG
395 * register to check if the module is running with independent or
396 * shared outputs. If the module is in shared output mode, only one
397 * output voltage will be reported.
399 if (data->id == zl8802) {
401 info->func[0] |= PMBUS_HAVE_IIN;
403 ret = i2c_smbus_read_word_data(client, ZL8802_MFR_DDC_CONFIG);
407 data->access = ktime_get();
410 if (ret & ZL8802_MFR_PHASES_MASK)
411 info->func[1] |= PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
413 info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
414 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
416 for (i = 0; i < 2; i++) {
417 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
421 data->access = ktime_get();
424 ret = i2c_smbus_read_word_data(client, ZL8802_MFR_USER_CONFIG);
428 if (ret & ZL8802_MFR_XTEMP_ENABLE_2)
429 info->func[i] |= PMBUS_HAVE_TEMP2;
431 data->access = ktime_get();
434 ret = i2c_smbus_read_word_data(client, ZL8802_MFR_USER_GLOBAL_CONFIG);
438 if (ret & ZL8802_MFR_TMON_ENABLE)
439 info->func[0] |= PMBUS_HAVE_TEMP3;
441 ret = i2c_smbus_read_word_data(client, ZL6100_MFR_CONFIG);
445 if (ret & ZL6100_MFR_XTEMP_ENABLE)
446 info->func[0] |= PMBUS_HAVE_TEMP2;
449 data->access = ktime_get();
452 info->read_word_data = zl6100_read_word_data;
453 info->read_byte_data = zl6100_read_byte_data;
454 info->write_word_data = zl6100_write_word_data;
455 info->write_byte = zl6100_write_byte;
457 return pmbus_do_probe(client, info);
460 static struct i2c_driver zl6100_driver = {
464 .probe_new = zl6100_probe,
465 .id_table = zl6100_id,
468 module_i2c_driver(zl6100_driver);
470 MODULE_AUTHOR("Guenter Roeck");
471 MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
472 MODULE_LICENSE("GPL");
473 MODULE_IMPORT_NS(PMBUS);