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/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>
28 #include <linux/ktime.h>
29 #include <linux/delay.h>
32 enum chips { zl2004, zl2005, zl2006, zl2008, zl2105, zl2106, zl6100, zl6105,
37 ktime_t access; /* chip access time */
38 int delay; /* Delay between chip accesses in uS */
39 struct pmbus_driver_info info;
42 #define to_zl6100_data(x) container_of(x, struct zl6100_data, info)
44 #define ZL6100_MFR_CONFIG 0xd0
45 #define ZL6100_DEVICE_ID 0xe4
47 #define ZL6100_MFR_XTEMP_ENABLE (1 << 7)
49 #define MFR_VMON_OV_FAULT_LIMIT 0xf5
50 #define MFR_VMON_UV_FAULT_LIMIT 0xf6
51 #define MFR_READ_VMON 0xf7
53 #define VMON_UV_WARNING (1 << 5)
54 #define VMON_OV_WARNING (1 << 4)
55 #define VMON_UV_FAULT (1 << 1)
56 #define VMON_OV_FAULT (1 << 0)
58 #define ZL6100_WAIT_TIME 1000 /* uS */
60 static ushort delay = ZL6100_WAIT_TIME;
61 module_param(delay, ushort, 0644);
62 MODULE_PARM_DESC(delay, "Delay between chip accesses in uS");
64 /* Convert linear sensor value to milli-units */
65 static long zl6100_l2d(s16 l)
72 mantissa = ((s16)((l & 0x7ff) << 5)) >> 5;
76 /* scale result to milli-units */
87 #define MAX_MANTISSA (1023 * 1000)
88 #define MIN_MANTISSA (511 * 1000)
90 static u16 zl6100_d2l(long val)
92 s16 exponent = 0, mantissa;
93 bool negative = false;
104 /* Reduce large mantissa until it fits into 10 bit */
105 while (val >= MAX_MANTISSA && exponent < 15) {
109 /* Increase small mantissa to improve precision */
110 while (val < MIN_MANTISSA && exponent > -15) {
115 /* Convert mantissa from milli-units to units */
116 mantissa = DIV_ROUND_CLOSEST(val, 1000);
118 /* Ensure that resulting number is within range */
119 if (mantissa > 0x3ff)
124 mantissa = -mantissa;
126 /* Convert to 5 bit exponent, 11 bit mantissa */
127 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
130 /* Some chips need a delay between accesses */
131 static inline void zl6100_wait(const struct zl6100_data *data)
134 s64 delta = ktime_us_delta(ktime_get(), data->access);
135 if (delta < data->delay)
136 udelay(data->delay - delta);
140 static int zl6100_read_word_data(struct i2c_client *client, int page, int reg)
142 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
143 struct zl6100_data *data = to_zl6100_data(info);
149 if (data->id == zl2005) {
151 * Limit register detection is not reliable on ZL2005.
152 * Make sure registers are not erroneously detected.
155 case PMBUS_VOUT_OV_WARN_LIMIT:
156 case PMBUS_VOUT_UV_WARN_LIMIT:
157 case PMBUS_IOUT_OC_WARN_LIMIT:
163 case PMBUS_VIRT_READ_VMON:
164 vreg = MFR_READ_VMON;
166 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
167 case PMBUS_VIRT_VMON_OV_FAULT_LIMIT:
168 vreg = MFR_VMON_OV_FAULT_LIMIT;
170 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
171 case PMBUS_VIRT_VMON_UV_FAULT_LIMIT:
172 vreg = MFR_VMON_UV_FAULT_LIMIT;
175 if (reg >= PMBUS_VIRT_BASE)
182 ret = pmbus_read_word_data(client, page, vreg);
183 data->access = ktime_get();
188 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
189 ret = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(ret) * 9, 10));
191 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
192 ret = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(ret) * 11, 10));
199 static int zl6100_read_byte_data(struct i2c_client *client, int page, int reg)
201 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
202 struct zl6100_data *data = to_zl6100_data(info);
211 case PMBUS_VIRT_STATUS_VMON:
212 ret = pmbus_read_byte_data(client, 0,
213 PMBUS_STATUS_MFR_SPECIFIC);
218 if (ret & VMON_UV_WARNING)
219 status |= PB_VOLTAGE_UV_WARNING;
220 if (ret & VMON_OV_WARNING)
221 status |= PB_VOLTAGE_OV_WARNING;
222 if (ret & VMON_UV_FAULT)
223 status |= PB_VOLTAGE_UV_FAULT;
224 if (ret & VMON_OV_FAULT)
225 status |= PB_VOLTAGE_OV_FAULT;
229 ret = pmbus_read_byte_data(client, page, reg);
232 data->access = ktime_get();
237 static int zl6100_write_word_data(struct i2c_client *client, int page, int reg,
240 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
241 struct zl6100_data *data = to_zl6100_data(info);
248 case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
249 word = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(word) * 10, 9));
250 vreg = MFR_VMON_OV_FAULT_LIMIT;
251 pmbus_clear_cache(client);
253 case PMBUS_VIRT_VMON_OV_FAULT_LIMIT:
254 vreg = MFR_VMON_OV_FAULT_LIMIT;
255 pmbus_clear_cache(client);
257 case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
258 word = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(word) * 10, 11));
259 vreg = MFR_VMON_UV_FAULT_LIMIT;
260 pmbus_clear_cache(client);
262 case PMBUS_VIRT_VMON_UV_FAULT_LIMIT:
263 vreg = MFR_VMON_UV_FAULT_LIMIT;
264 pmbus_clear_cache(client);
267 if (reg >= PMBUS_VIRT_BASE)
273 ret = pmbus_write_word_data(client, page, vreg, word);
274 data->access = ktime_get();
279 static int zl6100_write_byte(struct i2c_client *client, int page, u8 value)
281 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
282 struct zl6100_data *data = to_zl6100_data(info);
289 ret = pmbus_write_byte(client, page, value);
290 data->access = ktime_get();
295 static const struct i2c_device_id zl6100_id[] = {
313 MODULE_DEVICE_TABLE(i2c, zl6100_id);
315 static int zl6100_probe(struct i2c_client *client,
316 const struct i2c_device_id *id)
319 struct zl6100_data *data;
320 struct pmbus_driver_info *info;
321 u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
322 const struct i2c_device_id *mid;
324 if (!i2c_check_functionality(client->adapter,
325 I2C_FUNC_SMBUS_READ_WORD_DATA
326 | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
329 ret = i2c_smbus_read_block_data(client, ZL6100_DEVICE_ID,
332 dev_err(&client->dev, "Failed to read device ID\n");
335 device_id[ret] = '\0';
336 dev_info(&client->dev, "Device ID %s\n", device_id);
339 for (mid = zl6100_id; mid->name[0]; mid++) {
340 if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
344 dev_err(&client->dev, "Unsupported device\n");
347 if (id->driver_data != mid->driver_data)
348 dev_notice(&client->dev,
349 "Device mismatch: Configured %s, detected %s\n",
350 id->name, mid->name);
352 data = devm_kzalloc(&client->dev, sizeof(struct zl6100_data),
357 data->id = mid->driver_data;
360 * According to information from the chip vendor, all currently
361 * supported chips are known to require a wait time between I2C
367 * Since there was a direct I2C device access above, wait before
368 * accessing the chip again.
370 data->access = ktime_get();
376 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
377 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
378 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
379 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
382 * ZL2004, ZL9101M, and ZL9117M support monitoring an extra voltage
383 * (VMON for ZL2004, VDRV for ZL9101M and ZL9117M). Report it as vmon.
385 if (data->id == zl2004 || data->id == zl9101 || data->id == zl9117)
386 info->func[0] |= PMBUS_HAVE_VMON | PMBUS_HAVE_STATUS_VMON;
388 ret = i2c_smbus_read_word_data(client, ZL6100_MFR_CONFIG);
392 if (ret & ZL6100_MFR_XTEMP_ENABLE)
393 info->func[0] |= PMBUS_HAVE_TEMP2;
395 data->access = ktime_get();
398 info->read_word_data = zl6100_read_word_data;
399 info->read_byte_data = zl6100_read_byte_data;
400 info->write_word_data = zl6100_write_word_data;
401 info->write_byte = zl6100_write_byte;
403 return pmbus_do_probe(client, mid, info);
406 static struct i2c_driver zl6100_driver = {
410 .probe = zl6100_probe,
411 .remove = pmbus_do_remove,
412 .id_table = zl6100_id,
415 module_i2c_driver(zl6100_driver);
417 MODULE_AUTHOR("Guenter Roeck");
418 MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
419 MODULE_LICENSE("GPL");