1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
5 * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
7 * This driver is based on the ds1621 and ina209 drivers.
10 * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1140,P19392,D13517
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/bitops.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h>
22 #include <linux/jiffies.h>
23 #include <linux/platform_data/ltc4245.h>
25 /* Here are names of the chip's registers (a.k.a. commands) */
27 LTC4245_STATUS = 0x00, /* readonly */
29 LTC4245_CONTROL = 0x02,
31 LTC4245_FAULT1 = 0x04,
32 LTC4245_FAULT2 = 0x05,
34 LTC4245_ADCADR = 0x07,
37 LTC4245_12VSENSE = 0x11,
38 LTC4245_12VOUT = 0x12,
40 LTC4245_5VSENSE = 0x14,
43 LTC4245_3VSENSE = 0x17,
46 LTC4245_VEESENSE = 0x1a,
47 LTC4245_VEEOUT = 0x1b,
48 LTC4245_GPIOADC = 0x1c,
52 struct i2c_client *client;
54 struct mutex update_lock;
56 unsigned long last_updated; /* in jiffies */
58 /* Control registers */
61 /* Voltage registers */
64 /* GPIO ADC registers */
70 * Update the readings from the GPIO pins. If the driver has been configured to
71 * sample all GPIO's as analog voltages, a round-robin sampling method is used.
72 * Otherwise, only the configured GPIO pin is sampled.
74 * LOCKING: must hold data->update_lock
76 static void ltc4245_update_gpios(struct device *dev)
78 struct ltc4245_data *data = dev_get_drvdata(dev);
79 struct i2c_client *client = data->client;
80 u8 gpio_curr, gpio_next, gpio_reg;
83 /* no extra gpio support, we're basically done */
84 if (!data->use_extra_gpios) {
85 data->gpios[0] = data->vregs[LTC4245_GPIOADC - 0x10];
90 * If the last reading was too long ago, then we mark all old GPIO
91 * readings as stale by setting them to -EAGAIN
93 if (time_after(jiffies, data->last_updated + 5 * HZ)) {
94 for (i = 0; i < ARRAY_SIZE(data->gpios); i++)
95 data->gpios[i] = -EAGAIN;
99 * Get the current GPIO pin
101 * The datasheet calls these GPIO[1-3], but we'll calculate the zero
102 * based array index instead, and call them GPIO[0-2]. This is much
103 * easier to think about.
105 gpio_curr = (data->cregs[LTC4245_GPIO] & 0xc0) >> 6;
109 /* Read the GPIO voltage from the GPIOADC register */
110 data->gpios[gpio_curr] = data->vregs[LTC4245_GPIOADC - 0x10];
112 /* Find the next GPIO pin to read */
113 gpio_next = (gpio_curr + 1) % ARRAY_SIZE(data->gpios);
116 * Calculate the correct setting for the GPIO register so it will
117 * sample the next GPIO pin
119 gpio_reg = (data->cregs[LTC4245_GPIO] & 0x3f) | ((gpio_next + 1) << 6);
121 /* Update the GPIO register */
122 i2c_smbus_write_byte_data(client, LTC4245_GPIO, gpio_reg);
124 /* Update saved data */
125 data->cregs[LTC4245_GPIO] = gpio_reg;
128 static struct ltc4245_data *ltc4245_update_device(struct device *dev)
130 struct ltc4245_data *data = dev_get_drvdata(dev);
131 struct i2c_client *client = data->client;
135 mutex_lock(&data->update_lock);
137 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
139 /* Read control registers -- 0x00 to 0x07 */
140 for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
141 val = i2c_smbus_read_byte_data(client, i);
142 if (unlikely(val < 0))
145 data->cregs[i] = val;
148 /* Read voltage registers -- 0x10 to 0x1c */
149 for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
150 val = i2c_smbus_read_byte_data(client, i+0x10);
151 if (unlikely(val < 0))
154 data->vregs[i] = val;
157 /* Update GPIO readings */
158 ltc4245_update_gpios(dev);
160 data->last_updated = jiffies;
164 mutex_unlock(&data->update_lock);
169 /* Return the voltage from the given register in millivolts */
170 static int ltc4245_get_voltage(struct device *dev, u8 reg)
172 struct ltc4245_data *data = ltc4245_update_device(dev);
173 const u8 regval = data->vregs[reg - 0x10];
179 voltage = regval * 55;
183 voltage = regval * 22;
187 voltage = regval * 15;
191 voltage = regval * -55;
193 case LTC4245_GPIOADC:
194 voltage = regval * 10;
197 /* If we get here, the developer messed up */
205 /* Return the current in the given sense register in milliAmperes */
206 static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
208 struct ltc4245_data *data = ltc4245_update_device(dev);
209 const u8 regval = data->vregs[reg - 0x10];
210 unsigned int voltage;
214 * The strange looking conversions that follow are fixed-point
215 * math, since we cannot do floating point in the kernel.
217 * Step 1: convert sense register to microVolts
218 * Step 2: convert voltage to milliAmperes
220 * If you play around with the V=IR equation, you come up with
221 * the following: X uV / Y mOhm == Z mA
223 * With the resistors that are fractions of a milliOhm, we multiply
224 * the voltage and resistance by 10, to shift the decimal point.
225 * Now we can use the normal division operator again.
229 case LTC4245_12VSENSE:
230 voltage = regval * 250; /* voltage in uV */
231 curr = voltage / 50; /* sense resistor 50 mOhm */
233 case LTC4245_5VSENSE:
234 voltage = regval * 125; /* voltage in uV */
235 curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
237 case LTC4245_3VSENSE:
238 voltage = regval * 125; /* voltage in uV */
239 curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
241 case LTC4245_VEESENSE:
242 voltage = regval * 250; /* voltage in uV */
243 curr = voltage / 100; /* sense resistor 100 mOhm */
246 /* If we get here, the developer messed up */
255 /* Map from voltage channel index to voltage register */
257 static const s8 ltc4245_in_regs[] = {
258 LTC4245_12VIN, LTC4245_5VIN, LTC4245_3VIN, LTC4245_VEEIN,
259 LTC4245_12VOUT, LTC4245_5VOUT, LTC4245_3VOUT, LTC4245_VEEOUT,
262 /* Map from current channel index to current register */
264 static const s8 ltc4245_curr_regs[] = {
265 LTC4245_12VSENSE, LTC4245_5VSENSE, LTC4245_3VSENSE, LTC4245_VEESENSE,
268 static int ltc4245_read_curr(struct device *dev, u32 attr, int channel,
271 struct ltc4245_data *data = ltc4245_update_device(dev);
274 case hwmon_curr_input:
275 *val = ltc4245_get_current(dev, ltc4245_curr_regs[channel]);
277 case hwmon_curr_max_alarm:
278 *val = !!(data->cregs[LTC4245_FAULT1] & BIT(channel + 4));
285 static int ltc4245_read_in(struct device *dev, u32 attr, int channel, long *val)
287 struct ltc4245_data *data = ltc4245_update_device(dev);
292 *val = ltc4245_get_voltage(dev,
293 ltc4245_in_regs[channel]);
295 int regval = data->gpios[channel - 8];
302 case hwmon_in_min_alarm:
304 *val = !!(data->cregs[LTC4245_FAULT1] & BIT(channel));
306 *val = !!(data->cregs[LTC4245_FAULT2] &
314 static int ltc4245_read_power(struct device *dev, u32 attr, int channel,
321 case hwmon_power_input:
322 (void)ltc4245_update_device(dev);
323 curr = ltc4245_get_current(dev, ltc4245_curr_regs[channel]);
324 voltage = ltc4245_get_voltage(dev, ltc4245_in_regs[channel]);
325 *val = abs(curr * voltage);
332 static int ltc4245_read(struct device *dev, enum hwmon_sensor_types type,
333 u32 attr, int channel, long *val)
338 return ltc4245_read_curr(dev, attr, channel, val);
340 return ltc4245_read_power(dev, attr, channel, val);
342 return ltc4245_read_in(dev, attr, channel - 1, val);
348 static umode_t ltc4245_is_visible(const void *_data,
349 enum hwmon_sensor_types type,
350 u32 attr, int channel)
352 const struct ltc4245_data *data = _data;
360 if (channel > 9 && !data->use_extra_gpios)
363 case hwmon_in_min_alarm:
372 case hwmon_curr_input:
373 case hwmon_curr_max_alarm:
380 case hwmon_power_input:
390 static const struct hwmon_channel_info * const ltc4245_info[] = {
391 HWMON_CHANNEL_INFO(in,
393 HWMON_I_INPUT | HWMON_I_MIN_ALARM,
394 HWMON_I_INPUT | HWMON_I_MIN_ALARM,
395 HWMON_I_INPUT | HWMON_I_MIN_ALARM,
396 HWMON_I_INPUT | HWMON_I_MIN_ALARM,
397 HWMON_I_INPUT | HWMON_I_MIN_ALARM,
398 HWMON_I_INPUT | HWMON_I_MIN_ALARM,
399 HWMON_I_INPUT | HWMON_I_MIN_ALARM,
400 HWMON_I_INPUT | HWMON_I_MIN_ALARM,
404 HWMON_CHANNEL_INFO(curr,
405 HWMON_C_INPUT | HWMON_C_MAX_ALARM,
406 HWMON_C_INPUT | HWMON_C_MAX_ALARM,
407 HWMON_C_INPUT | HWMON_C_MAX_ALARM,
408 HWMON_C_INPUT | HWMON_C_MAX_ALARM),
409 HWMON_CHANNEL_INFO(power,
417 static const struct hwmon_ops ltc4245_hwmon_ops = {
418 .is_visible = ltc4245_is_visible,
419 .read = ltc4245_read,
422 static const struct hwmon_chip_info ltc4245_chip_info = {
423 .ops = <c4245_hwmon_ops,
424 .info = ltc4245_info,
427 static bool ltc4245_use_extra_gpios(struct i2c_client *client)
429 struct ltc4245_platform_data *pdata = dev_get_platdata(&client->dev);
430 struct device_node *np = client->dev.of_node;
432 /* prefer platform data */
434 return pdata->use_extra_gpios;
437 if (of_property_read_bool(np, "ltc4245,use-extra-gpios"))
443 static int ltc4245_probe(struct i2c_client *client)
445 struct i2c_adapter *adapter = client->adapter;
446 struct ltc4245_data *data;
447 struct device *hwmon_dev;
449 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
452 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
456 data->client = client;
457 mutex_init(&data->update_lock);
458 data->use_extra_gpios = ltc4245_use_extra_gpios(client);
460 /* Initialize the LTC4245 chip */
461 i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
462 i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
464 hwmon_dev = devm_hwmon_device_register_with_info(&client->dev,
468 return PTR_ERR_OR_ZERO(hwmon_dev);
471 static const struct i2c_device_id ltc4245_id[] = {
475 MODULE_DEVICE_TABLE(i2c, ltc4245_id);
477 /* This is the driver that will be inserted */
478 static struct i2c_driver ltc4245_driver = {
482 .probe = ltc4245_probe,
483 .id_table = ltc4245_id,
486 module_i2c_driver(ltc4245_driver);
488 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
489 MODULE_DESCRIPTION("LTC4245 driver");
490 MODULE_LICENSE("GPL");