2 * Hardware monitoring driver for ZL6100 and compatibles
4 * Copyright (c) 2011 Ericsson AB.
5 * Copyright (c) 2012 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/bitops.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/ktime.h>
30 #include <linux/delay.h>
33 enum chips
{ zl2004
, zl2005
, zl2006
, zl2008
, zl2105
, zl2106
, zl6100
, zl6105
,
38 ktime_t access
; /* chip access time */
39 int delay
; /* Delay between chip accesses in uS */
40 struct pmbus_driver_info info
;
43 #define to_zl6100_data(x) container_of(x, struct zl6100_data, info)
45 #define ZL6100_MFR_CONFIG 0xd0
46 #define ZL6100_DEVICE_ID 0xe4
48 #define ZL6100_MFR_XTEMP_ENABLE BIT(7)
50 #define MFR_VMON_OV_FAULT_LIMIT 0xf5
51 #define MFR_VMON_UV_FAULT_LIMIT 0xf6
52 #define MFR_READ_VMON 0xf7
54 #define VMON_UV_WARNING BIT(5)
55 #define VMON_OV_WARNING BIT(4)
56 #define VMON_UV_FAULT BIT(1)
57 #define VMON_OV_FAULT BIT(0)
59 #define ZL6100_WAIT_TIME 1000 /* uS */
61 static ushort delay
= ZL6100_WAIT_TIME
;
62 module_param(delay
, ushort
, 0644);
63 MODULE_PARM_DESC(delay
, "Delay between chip accesses in uS");
65 /* Convert linear sensor value to milli-units */
66 static long zl6100_l2d(s16 l
)
73 mantissa
= ((s16
)((l
& 0x7ff) << 5)) >> 5;
77 /* scale result to milli-units */
88 #define MAX_MANTISSA (1023 * 1000)
89 #define MIN_MANTISSA (511 * 1000)
91 static u16
zl6100_d2l(long val
)
93 s16 exponent
= 0, mantissa
;
94 bool negative
= false;
105 /* Reduce large mantissa until it fits into 10 bit */
106 while (val
>= MAX_MANTISSA
&& exponent
< 15) {
110 /* Increase small mantissa to improve precision */
111 while (val
< MIN_MANTISSA
&& exponent
> -15) {
116 /* Convert mantissa from milli-units to units */
117 mantissa
= DIV_ROUND_CLOSEST(val
, 1000);
119 /* Ensure that resulting number is within range */
120 if (mantissa
> 0x3ff)
125 mantissa
= -mantissa
;
127 /* Convert to 5 bit exponent, 11 bit mantissa */
128 return (mantissa
& 0x7ff) | ((exponent
<< 11) & 0xf800);
131 /* Some chips need a delay between accesses */
132 static inline void zl6100_wait(const struct zl6100_data
*data
)
135 s64 delta
= ktime_us_delta(ktime_get(), data
->access
);
136 if (delta
< data
->delay
)
137 udelay(data
->delay
- delta
);
141 static int zl6100_read_word_data(struct i2c_client
*client
, int page
, int reg
)
143 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
144 struct zl6100_data
*data
= to_zl6100_data(info
);
150 if (data
->id
== zl2005
) {
152 * Limit register detection is not reliable on ZL2005.
153 * Make sure registers are not erroneously detected.
156 case PMBUS_VOUT_OV_WARN_LIMIT
:
157 case PMBUS_VOUT_UV_WARN_LIMIT
:
158 case PMBUS_IOUT_OC_WARN_LIMIT
:
164 case PMBUS_VIRT_READ_VMON
:
165 vreg
= MFR_READ_VMON
;
167 case PMBUS_VIRT_VMON_OV_WARN_LIMIT
:
168 case PMBUS_VIRT_VMON_OV_FAULT_LIMIT
:
169 vreg
= MFR_VMON_OV_FAULT_LIMIT
;
171 case PMBUS_VIRT_VMON_UV_WARN_LIMIT
:
172 case PMBUS_VIRT_VMON_UV_FAULT_LIMIT
:
173 vreg
= MFR_VMON_UV_FAULT_LIMIT
;
176 if (reg
>= PMBUS_VIRT_BASE
)
183 ret
= pmbus_read_word_data(client
, page
, vreg
);
184 data
->access
= ktime_get();
189 case PMBUS_VIRT_VMON_OV_WARN_LIMIT
:
190 ret
= zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(ret
) * 9, 10));
192 case PMBUS_VIRT_VMON_UV_WARN_LIMIT
:
193 ret
= zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(ret
) * 11, 10));
200 static int zl6100_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
202 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
203 struct zl6100_data
*data
= to_zl6100_data(info
);
212 case PMBUS_VIRT_STATUS_VMON
:
213 ret
= pmbus_read_byte_data(client
, 0,
214 PMBUS_STATUS_MFR_SPECIFIC
);
219 if (ret
& VMON_UV_WARNING
)
220 status
|= PB_VOLTAGE_UV_WARNING
;
221 if (ret
& VMON_OV_WARNING
)
222 status
|= PB_VOLTAGE_OV_WARNING
;
223 if (ret
& VMON_UV_FAULT
)
224 status
|= PB_VOLTAGE_UV_FAULT
;
225 if (ret
& VMON_OV_FAULT
)
226 status
|= PB_VOLTAGE_OV_FAULT
;
230 ret
= pmbus_read_byte_data(client
, page
, reg
);
233 data
->access
= ktime_get();
238 static int zl6100_write_word_data(struct i2c_client
*client
, int page
, int reg
,
241 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
242 struct zl6100_data
*data
= to_zl6100_data(info
);
249 case PMBUS_VIRT_VMON_OV_WARN_LIMIT
:
250 word
= zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(word
) * 10, 9));
251 vreg
= MFR_VMON_OV_FAULT_LIMIT
;
252 pmbus_clear_cache(client
);
254 case PMBUS_VIRT_VMON_OV_FAULT_LIMIT
:
255 vreg
= MFR_VMON_OV_FAULT_LIMIT
;
256 pmbus_clear_cache(client
);
258 case PMBUS_VIRT_VMON_UV_WARN_LIMIT
:
259 word
= zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(word
) * 10, 11));
260 vreg
= MFR_VMON_UV_FAULT_LIMIT
;
261 pmbus_clear_cache(client
);
263 case PMBUS_VIRT_VMON_UV_FAULT_LIMIT
:
264 vreg
= MFR_VMON_UV_FAULT_LIMIT
;
265 pmbus_clear_cache(client
);
268 if (reg
>= PMBUS_VIRT_BASE
)
274 ret
= pmbus_write_word_data(client
, page
, vreg
, word
);
275 data
->access
= ktime_get();
280 static int zl6100_write_byte(struct i2c_client
*client
, int page
, u8 value
)
282 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
283 struct zl6100_data
*data
= to_zl6100_data(info
);
290 ret
= pmbus_write_byte(client
, page
, value
);
291 data
->access
= ktime_get();
296 static const struct i2c_device_id zl6100_id
[] = {
314 MODULE_DEVICE_TABLE(i2c
, zl6100_id
);
316 static int zl6100_probe(struct i2c_client
*client
,
317 const struct i2c_device_id
*id
)
320 struct zl6100_data
*data
;
321 struct pmbus_driver_info
*info
;
322 u8 device_id
[I2C_SMBUS_BLOCK_MAX
+ 1];
323 const struct i2c_device_id
*mid
;
325 if (!i2c_check_functionality(client
->adapter
,
326 I2C_FUNC_SMBUS_READ_WORD_DATA
327 | I2C_FUNC_SMBUS_READ_BLOCK_DATA
))
330 ret
= i2c_smbus_read_block_data(client
, ZL6100_DEVICE_ID
,
333 dev_err(&client
->dev
, "Failed to read device ID\n");
336 device_id
[ret
] = '\0';
337 dev_info(&client
->dev
, "Device ID %s\n", device_id
);
340 for (mid
= zl6100_id
; mid
->name
[0]; mid
++) {
341 if (!strncasecmp(mid
->name
, device_id
, strlen(mid
->name
)))
345 dev_err(&client
->dev
, "Unsupported device\n");
348 if (id
->driver_data
!= mid
->driver_data
)
349 dev_notice(&client
->dev
,
350 "Device mismatch: Configured %s, detected %s\n",
351 id
->name
, mid
->name
);
353 data
= devm_kzalloc(&client
->dev
, sizeof(struct zl6100_data
),
358 data
->id
= mid
->driver_data
;
361 * According to information from the chip vendor, all currently
362 * supported chips are known to require a wait time between I2C
368 * Since there was a direct I2C device access above, wait before
369 * accessing the chip again.
371 data
->access
= ktime_get();
377 info
->func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_STATUS_INPUT
378 | PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
379 | PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
380 | PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
;
383 * ZL2004, ZL9101M, and ZL9117M support monitoring an extra voltage
384 * (VMON for ZL2004, VDRV for ZL9101M and ZL9117M). Report it as vmon.
386 if (data
->id
== zl2004
|| data
->id
== zl9101
|| data
->id
== zl9117
)
387 info
->func
[0] |= PMBUS_HAVE_VMON
| PMBUS_HAVE_STATUS_VMON
;
389 ret
= i2c_smbus_read_word_data(client
, ZL6100_MFR_CONFIG
);
393 if (ret
& ZL6100_MFR_XTEMP_ENABLE
)
394 info
->func
[0] |= PMBUS_HAVE_TEMP2
;
396 data
->access
= ktime_get();
399 info
->read_word_data
= zl6100_read_word_data
;
400 info
->read_byte_data
= zl6100_read_byte_data
;
401 info
->write_word_data
= zl6100_write_word_data
;
402 info
->write_byte
= zl6100_write_byte
;
404 return pmbus_do_probe(client
, mid
, info
);
407 static struct i2c_driver zl6100_driver
= {
411 .probe
= zl6100_probe
,
412 .remove
= pmbus_do_remove
,
413 .id_table
= zl6100_id
,
416 module_i2c_driver(zl6100_driver
);
418 MODULE_AUTHOR("Guenter Roeck");
419 MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
420 MODULE_LICENSE("GPL");