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 struct pmbus_driver_info info
;
28 #define to_zl6100_data(x) container_of(x, struct zl6100_data, info)
30 #define ZL6100_MFR_CONFIG 0xd0
31 #define ZL6100_DEVICE_ID 0xe4
33 #define ZL6100_MFR_XTEMP_ENABLE BIT(7)
35 #define ZL8802_MFR_USER_GLOBAL_CONFIG 0xe9
36 #define ZL8802_MFR_TMON_ENABLE BIT(12)
37 #define ZL8802_MFR_USER_CONFIG 0xd1
38 #define ZL8802_MFR_XTEMP_ENABLE_2 BIT(1)
39 #define ZL8802_MFR_DDC_CONFIG 0xd3
40 #define ZL8802_MFR_PHASES_MASK 0x0007
42 #define MFR_VMON_OV_FAULT_LIMIT 0xf5
43 #define MFR_VMON_UV_FAULT_LIMIT 0xf6
44 #define MFR_READ_VMON 0xf7
46 #define VMON_UV_WARNING BIT(5)
47 #define VMON_OV_WARNING BIT(4)
48 #define VMON_UV_FAULT BIT(1)
49 #define VMON_OV_FAULT BIT(0)
51 #define ZL6100_WAIT_TIME 1000 /* uS */
53 static ushort delay
= ZL6100_WAIT_TIME
;
54 module_param(delay
, ushort
, 0644);
55 MODULE_PARM_DESC(delay
, "Delay between chip accesses in uS");
57 /* Convert linear sensor value to milli-units */
58 static long zl6100_l2d(s16 l
)
65 mantissa
= ((s16
)((l
& 0x7ff) << 5)) >> 5;
69 /* scale result to milli-units */
80 #define MAX_MANTISSA (1023 * 1000)
81 #define MIN_MANTISSA (511 * 1000)
83 static u16
zl6100_d2l(long val
)
85 s16 exponent
= 0, mantissa
;
86 bool negative
= false;
97 /* Reduce large mantissa until it fits into 10 bit */
98 while (val
>= MAX_MANTISSA
&& exponent
< 15) {
102 /* Increase small mantissa to improve precision */
103 while (val
< MIN_MANTISSA
&& exponent
> -15) {
108 /* Convert mantissa from milli-units to units */
109 mantissa
= DIV_ROUND_CLOSEST(val
, 1000);
111 /* Ensure that resulting number is within range */
112 if (mantissa
> 0x3ff)
117 mantissa
= -mantissa
;
119 /* Convert to 5 bit exponent, 11 bit mantissa */
120 return (mantissa
& 0x7ff) | ((exponent
<< 11) & 0xf800);
123 static int zl6100_read_word_data(struct i2c_client
*client
, int page
,
126 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
127 struct zl6100_data
*data
= to_zl6100_data(info
);
130 if (page
>= info
->pages
)
133 if (data
->id
== zl2005
) {
135 * Limit register detection is not reliable on ZL2005.
136 * Make sure registers are not erroneously detected.
139 case PMBUS_VOUT_OV_WARN_LIMIT
:
140 case PMBUS_VOUT_UV_WARN_LIMIT
:
141 case PMBUS_IOUT_OC_WARN_LIMIT
:
147 case PMBUS_VIRT_READ_VMON
:
148 vreg
= MFR_READ_VMON
;
150 case PMBUS_VIRT_VMON_OV_WARN_LIMIT
:
151 case PMBUS_VIRT_VMON_OV_FAULT_LIMIT
:
152 vreg
= MFR_VMON_OV_FAULT_LIMIT
;
154 case PMBUS_VIRT_VMON_UV_WARN_LIMIT
:
155 case PMBUS_VIRT_VMON_UV_FAULT_LIMIT
:
156 vreg
= MFR_VMON_UV_FAULT_LIMIT
;
159 if (reg
>= PMBUS_VIRT_BASE
)
165 ret
= pmbus_read_word_data(client
, page
, phase
, vreg
);
170 case PMBUS_VIRT_VMON_OV_WARN_LIMIT
:
171 ret
= zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(ret
) * 9, 10));
173 case PMBUS_VIRT_VMON_UV_WARN_LIMIT
:
174 ret
= zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(ret
) * 11, 10));
181 static int zl6100_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
183 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
186 if (page
>= info
->pages
)
190 case PMBUS_VIRT_STATUS_VMON
:
191 ret
= pmbus_read_byte_data(client
, 0,
192 PMBUS_STATUS_MFR_SPECIFIC
);
197 if (ret
& VMON_UV_WARNING
)
198 status
|= PB_VOLTAGE_UV_WARNING
;
199 if (ret
& VMON_OV_WARNING
)
200 status
|= PB_VOLTAGE_OV_WARNING
;
201 if (ret
& VMON_UV_FAULT
)
202 status
|= PB_VOLTAGE_UV_FAULT
;
203 if (ret
& VMON_OV_FAULT
)
204 status
|= PB_VOLTAGE_OV_FAULT
;
208 ret
= pmbus_read_byte_data(client
, page
, reg
);
215 static int zl6100_write_word_data(struct i2c_client
*client
, int page
, int reg
,
218 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
221 if (page
>= info
->pages
)
225 case PMBUS_VIRT_VMON_OV_WARN_LIMIT
:
226 word
= zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(word
) * 10, 9));
227 vreg
= MFR_VMON_OV_FAULT_LIMIT
;
228 pmbus_clear_cache(client
);
230 case PMBUS_VIRT_VMON_OV_FAULT_LIMIT
:
231 vreg
= MFR_VMON_OV_FAULT_LIMIT
;
232 pmbus_clear_cache(client
);
234 case PMBUS_VIRT_VMON_UV_WARN_LIMIT
:
235 word
= zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(word
) * 10, 11));
236 vreg
= MFR_VMON_UV_FAULT_LIMIT
;
237 pmbus_clear_cache(client
);
239 case PMBUS_VIRT_VMON_UV_FAULT_LIMIT
:
240 vreg
= MFR_VMON_UV_FAULT_LIMIT
;
241 pmbus_clear_cache(client
);
244 if (reg
>= PMBUS_VIRT_BASE
)
249 return pmbus_write_word_data(client
, page
, vreg
, word
);
252 static const struct i2c_device_id zl6100_id
[] = {
273 {"zls1003", zls1003
},
274 {"zls4009", zls4009
},
277 MODULE_DEVICE_TABLE(i2c
, zl6100_id
);
279 static int zl6100_probe(struct i2c_client
*client
)
282 struct zl6100_data
*data
;
283 struct pmbus_driver_info
*info
;
284 u8 device_id
[I2C_SMBUS_BLOCK_MAX
+ 1];
285 const struct i2c_device_id
*mid
;
287 if (!i2c_check_functionality(client
->adapter
,
288 I2C_FUNC_SMBUS_READ_WORD_DATA
289 | I2C_FUNC_SMBUS_READ_BLOCK_DATA
))
292 ret
= i2c_smbus_read_block_data(client
, ZL6100_DEVICE_ID
,
295 dev_err(&client
->dev
, "Failed to read device ID\n");
298 device_id
[ret
] = '\0';
299 dev_info(&client
->dev
, "Device ID %s\n", device_id
);
302 for (mid
= zl6100_id
; mid
->name
[0]; mid
++) {
303 if (!strncasecmp(mid
->name
, device_id
, strlen(mid
->name
)))
307 dev_err(&client
->dev
, "Unsupported device\n");
310 if (strcmp(client
->name
, mid
->name
) != 0)
311 dev_notice(&client
->dev
,
312 "Device mismatch: Configured %s, detected %s\n",
313 client
->name
, mid
->name
);
315 data
= devm_kzalloc(&client
->dev
, sizeof(struct zl6100_data
),
320 data
->id
= mid
->driver_data
;
323 * According to information from the chip vendor, all currently
324 * supported chips are known to require a wait time between I2C
332 info
->func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_STATUS_INPUT
333 | PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
334 | PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
335 | PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
;
338 * ZL2004, ZL8802, ZL9101M, ZL9117M and ZLS4009 support monitoring
339 * an extra voltage (VMON for ZL2004, ZL8802 and ZLS4009,
340 * VDRV for ZL9101M and ZL9117M). Report it as vmon.
342 if (data
->id
== zl2004
|| data
->id
== zl8802
|| data
->id
== zl9101
||
343 data
->id
== zl9117
|| data
->id
== zls4009
)
344 info
->func
[0] |= PMBUS_HAVE_VMON
| PMBUS_HAVE_STATUS_VMON
;
347 * ZL8802 has two outputs that can be used either independently or in
348 * a current sharing configuration. The driver uses the DDC_CONFIG
349 * register to check if the module is running with independent or
350 * shared outputs. If the module is in shared output mode, only one
351 * output voltage will be reported.
353 if (data
->id
== zl8802
) {
355 info
->func
[0] |= PMBUS_HAVE_IIN
;
357 ret
= i2c_smbus_read_word_data(client
, ZL8802_MFR_DDC_CONFIG
);
363 if (ret
& ZL8802_MFR_PHASES_MASK
)
364 info
->func
[1] |= PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
;
366 info
->func
[1] = PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
367 | PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
;
369 for (i
= 0; i
< 2; i
++) {
370 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, i
);
376 ret
= i2c_smbus_read_word_data(client
, ZL8802_MFR_USER_CONFIG
);
380 if (ret
& ZL8802_MFR_XTEMP_ENABLE_2
)
381 info
->func
[i
] |= PMBUS_HAVE_TEMP2
;
385 ret
= i2c_smbus_read_word_data(client
, ZL8802_MFR_USER_GLOBAL_CONFIG
);
389 if (ret
& ZL8802_MFR_TMON_ENABLE
)
390 info
->func
[0] |= PMBUS_HAVE_TEMP3
;
392 ret
= i2c_smbus_read_word_data(client
, ZL6100_MFR_CONFIG
);
396 if (ret
& ZL6100_MFR_XTEMP_ENABLE
)
397 info
->func
[0] |= PMBUS_HAVE_TEMP2
;
402 info
->access_delay
= delay
;
403 info
->read_word_data
= zl6100_read_word_data
;
404 info
->read_byte_data
= zl6100_read_byte_data
;
405 info
->write_word_data
= zl6100_write_word_data
;
407 return pmbus_do_probe(client
, info
);
410 static struct i2c_driver zl6100_driver
= {
414 .probe
= zl6100_probe
,
415 .id_table
= zl6100_id
,
418 module_i2c_driver(zl6100_driver
);
420 MODULE_AUTHOR("Guenter Roeck");
421 MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
422 MODULE_LICENSE("GPL");
423 MODULE_IMPORT_NS(PMBUS
);