Loading...
1/*
2 * Driver for Linear Technology LTC4215 I2C Hot Swap Controller
3 *
4 * Copyright (C) 2009 Ira W. Snyder <iws@ovro.caltech.edu>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * Datasheet:
11 * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1163,P17572,D12697
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.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
23/* Here are names of the chip's registers (a.k.a. commands) */
24enum ltc4215_cmd {
25 LTC4215_CONTROL = 0x00, /* rw */
26 LTC4215_ALERT = 0x01, /* rw */
27 LTC4215_STATUS = 0x02, /* ro */
28 LTC4215_FAULT = 0x03, /* rw */
29 LTC4215_SENSE = 0x04, /* rw */
30 LTC4215_SOURCE = 0x05, /* rw */
31 LTC4215_ADIN = 0x06, /* rw */
32};
33
34struct ltc4215_data {
35 struct device *hwmon_dev;
36
37 struct mutex update_lock;
38 bool valid;
39 unsigned long last_updated; /* in jiffies */
40
41 /* Registers */
42 u8 regs[7];
43};
44
45static struct ltc4215_data *ltc4215_update_device(struct device *dev)
46{
47 struct i2c_client *client = to_i2c_client(dev);
48 struct ltc4215_data *data = i2c_get_clientdata(client);
49 s32 val;
50 int i;
51
52 mutex_lock(&data->update_lock);
53
54 /* The chip's A/D updates 10 times per second */
55 if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
56
57 dev_dbg(&client->dev, "Starting ltc4215 update\n");
58
59 /* Read all registers */
60 for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
61 val = i2c_smbus_read_byte_data(client, i);
62 if (unlikely(val < 0))
63 data->regs[i] = 0;
64 else
65 data->regs[i] = val;
66 }
67
68 data->last_updated = jiffies;
69 data->valid = 1;
70 }
71
72 mutex_unlock(&data->update_lock);
73
74 return data;
75}
76
77/* Return the voltage from the given register in millivolts */
78static int ltc4215_get_voltage(struct device *dev, u8 reg)
79{
80 struct ltc4215_data *data = ltc4215_update_device(dev);
81 const u8 regval = data->regs[reg];
82 u32 voltage = 0;
83
84 switch (reg) {
85 case LTC4215_SENSE:
86 /* 151 uV per increment */
87 voltage = regval * 151 / 1000;
88 break;
89 case LTC4215_SOURCE:
90 /* 60.5 mV per increment */
91 voltage = regval * 605 / 10;
92 break;
93 case LTC4215_ADIN:
94 /* The ADIN input is divided by 12.5, and has 4.82 mV
95 * per increment, so we have the additional multiply */
96 voltage = regval * 482 * 125 / 1000;
97 break;
98 default:
99 /* If we get here, the developer messed up */
100 WARN_ON_ONCE(1);
101 break;
102 }
103
104 return voltage;
105}
106
107/* Return the current from the sense resistor in mA */
108static unsigned int ltc4215_get_current(struct device *dev)
109{
110 struct ltc4215_data *data = ltc4215_update_device(dev);
111
112 /* The strange looking conversions that follow are fixed-point
113 * math, since we cannot do floating point in the kernel.
114 *
115 * Step 1: convert sense register to microVolts
116 * Step 2: convert voltage to milliAmperes
117 *
118 * If you play around with the V=IR equation, you come up with
119 * the following: X uV / Y mOhm == Z mA
120 *
121 * With the resistors that are fractions of a milliOhm, we multiply
122 * the voltage and resistance by 10, to shift the decimal point.
123 * Now we can use the normal division operator again.
124 */
125
126 /* Calculate voltage in microVolts (151 uV per increment) */
127 const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
128
129 /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
130 const unsigned int curr = voltage / 4;
131
132 return curr;
133}
134
135static ssize_t ltc4215_show_voltage(struct device *dev,
136 struct device_attribute *da,
137 char *buf)
138{
139 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
140 const int voltage = ltc4215_get_voltage(dev, attr->index);
141
142 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
143}
144
145static ssize_t ltc4215_show_current(struct device *dev,
146 struct device_attribute *da,
147 char *buf)
148{
149 const unsigned int curr = ltc4215_get_current(dev);
150
151 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
152}
153
154static ssize_t ltc4215_show_power(struct device *dev,
155 struct device_attribute *da,
156 char *buf)
157{
158 const unsigned int curr = ltc4215_get_current(dev);
159 const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
160
161 /* current in mA * voltage in mV == power in uW */
162 const unsigned int power = abs(output_voltage * curr);
163
164 return snprintf(buf, PAGE_SIZE, "%u\n", power);
165}
166
167static ssize_t ltc4215_show_alarm(struct device *dev,
168 struct device_attribute *da,
169 char *buf)
170{
171 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
172 struct ltc4215_data *data = ltc4215_update_device(dev);
173 const u8 reg = data->regs[attr->index];
174 const u32 mask = attr->nr;
175
176 return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
177}
178
179/* These macros are used below in constructing device attribute objects
180 * for use with sysfs_create_group() to make a sysfs device file
181 * for each register.
182 */
183
184#define LTC4215_VOLTAGE(name, ltc4215_cmd_idx) \
185 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
186 ltc4215_show_voltage, NULL, ltc4215_cmd_idx)
187
188#define LTC4215_CURRENT(name) \
189 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
190 ltc4215_show_current, NULL, 0);
191
192#define LTC4215_POWER(name) \
193 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
194 ltc4215_show_power, NULL, 0);
195
196#define LTC4215_ALARM(name, mask, reg) \
197 static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
198 ltc4215_show_alarm, NULL, (mask), reg)
199
200/* Construct a sensor_device_attribute structure for each register */
201
202/* Current */
203LTC4215_CURRENT(curr1_input);
204LTC4215_ALARM(curr1_max_alarm, (1 << 2), LTC4215_STATUS);
205
206/* Power (virtual) */
207LTC4215_POWER(power1_input);
208
209/* Input Voltage */
210LTC4215_VOLTAGE(in1_input, LTC4215_ADIN);
211LTC4215_ALARM(in1_max_alarm, (1 << 0), LTC4215_STATUS);
212LTC4215_ALARM(in1_min_alarm, (1 << 1), LTC4215_STATUS);
213
214/* Output Voltage */
215LTC4215_VOLTAGE(in2_input, LTC4215_SOURCE);
216LTC4215_ALARM(in2_min_alarm, (1 << 3), LTC4215_STATUS);
217
218/* Finally, construct an array of pointers to members of the above objects,
219 * as required for sysfs_create_group()
220 */
221static struct attribute *ltc4215_attributes[] = {
222 &sensor_dev_attr_curr1_input.dev_attr.attr,
223 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
224
225 &sensor_dev_attr_power1_input.dev_attr.attr,
226
227 &sensor_dev_attr_in1_input.dev_attr.attr,
228 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
229 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
230
231 &sensor_dev_attr_in2_input.dev_attr.attr,
232 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
233
234 NULL,
235};
236
237static const struct attribute_group ltc4215_group = {
238 .attrs = ltc4215_attributes,
239};
240
241static int ltc4215_probe(struct i2c_client *client,
242 const struct i2c_device_id *id)
243{
244 struct i2c_adapter *adapter = client->adapter;
245 struct ltc4215_data *data;
246 int ret;
247
248 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
249 return -ENODEV;
250
251 data = kzalloc(sizeof(*data), GFP_KERNEL);
252 if (!data) {
253 ret = -ENOMEM;
254 goto out_kzalloc;
255 }
256
257 i2c_set_clientdata(client, data);
258 mutex_init(&data->update_lock);
259
260 /* Initialize the LTC4215 chip */
261 i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
262
263 /* Register sysfs hooks */
264 ret = sysfs_create_group(&client->dev.kobj, <c4215_group);
265 if (ret)
266 goto out_sysfs_create_group;
267
268 data->hwmon_dev = hwmon_device_register(&client->dev);
269 if (IS_ERR(data->hwmon_dev)) {
270 ret = PTR_ERR(data->hwmon_dev);
271 goto out_hwmon_device_register;
272 }
273
274 return 0;
275
276out_hwmon_device_register:
277 sysfs_remove_group(&client->dev.kobj, <c4215_group);
278out_sysfs_create_group:
279 kfree(data);
280out_kzalloc:
281 return ret;
282}
283
284static int ltc4215_remove(struct i2c_client *client)
285{
286 struct ltc4215_data *data = i2c_get_clientdata(client);
287
288 hwmon_device_unregister(data->hwmon_dev);
289 sysfs_remove_group(&client->dev.kobj, <c4215_group);
290
291 kfree(data);
292
293 return 0;
294}
295
296static const struct i2c_device_id ltc4215_id[] = {
297 { "ltc4215", 0 },
298 { }
299};
300MODULE_DEVICE_TABLE(i2c, ltc4215_id);
301
302/* This is the driver that will be inserted */
303static struct i2c_driver ltc4215_driver = {
304 .driver = {
305 .name = "ltc4215",
306 },
307 .probe = ltc4215_probe,
308 .remove = ltc4215_remove,
309 .id_table = ltc4215_id,
310};
311
312static int __init ltc4215_init(void)
313{
314 return i2c_add_driver(<c4215_driver);
315}
316
317static void __exit ltc4215_exit(void)
318{
319 i2c_del_driver(<c4215_driver);
320}
321
322MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
323MODULE_DESCRIPTION("LTC4215 driver");
324MODULE_LICENSE("GPL");
325
326module_init(ltc4215_init);
327module_exit(ltc4215_exit);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Driver for Linear Technology LTC4215 I2C Hot Swap Controller
4 *
5 * Copyright (C) 2009 Ira W. Snyder <iws@ovro.caltech.edu>
6 *
7 * Datasheet:
8 * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1163,P17572,D12697
9 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/i2c.h>
17#include <linux/hwmon.h>
18#include <linux/hwmon-sysfs.h>
19#include <linux/jiffies.h>
20
21/* Here are names of the chip's registers (a.k.a. commands) */
22enum ltc4215_cmd {
23 LTC4215_CONTROL = 0x00, /* rw */
24 LTC4215_ALERT = 0x01, /* rw */
25 LTC4215_STATUS = 0x02, /* ro */
26 LTC4215_FAULT = 0x03, /* rw */
27 LTC4215_SENSE = 0x04, /* rw */
28 LTC4215_SOURCE = 0x05, /* rw */
29 LTC4215_ADIN = 0x06, /* rw */
30};
31
32struct ltc4215_data {
33 struct i2c_client *client;
34
35 struct mutex update_lock;
36 bool valid;
37 unsigned long last_updated; /* in jiffies */
38
39 /* Registers */
40 u8 regs[7];
41};
42
43static struct ltc4215_data *ltc4215_update_device(struct device *dev)
44{
45 struct ltc4215_data *data = dev_get_drvdata(dev);
46 struct i2c_client *client = data->client;
47 s32 val;
48 int i;
49
50 mutex_lock(&data->update_lock);
51
52 /* The chip's A/D updates 10 times per second */
53 if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
54
55 dev_dbg(&client->dev, "Starting ltc4215 update\n");
56
57 /* Read all registers */
58 for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
59 val = i2c_smbus_read_byte_data(client, i);
60 if (unlikely(val < 0))
61 data->regs[i] = 0;
62 else
63 data->regs[i] = val;
64 }
65
66 data->last_updated = jiffies;
67 data->valid = true;
68 }
69
70 mutex_unlock(&data->update_lock);
71
72 return data;
73}
74
75/* Return the voltage from the given register in millivolts */
76static int ltc4215_get_voltage(struct device *dev, u8 reg)
77{
78 struct ltc4215_data *data = ltc4215_update_device(dev);
79 const u8 regval = data->regs[reg];
80 u32 voltage = 0;
81
82 switch (reg) {
83 case LTC4215_SENSE:
84 /* 151 uV per increment */
85 voltage = regval * 151 / 1000;
86 break;
87 case LTC4215_SOURCE:
88 /* 60.5 mV per increment */
89 voltage = regval * 605 / 10;
90 break;
91 case LTC4215_ADIN:
92 /*
93 * The ADIN input is divided by 12.5, and has 4.82 mV
94 * per increment, so we have the additional multiply
95 */
96 voltage = regval * 482 * 125 / 1000;
97 break;
98 default:
99 /* If we get here, the developer messed up */
100 WARN_ON_ONCE(1);
101 break;
102 }
103
104 return voltage;
105}
106
107/* Return the current from the sense resistor in mA */
108static unsigned int ltc4215_get_current(struct device *dev)
109{
110 struct ltc4215_data *data = ltc4215_update_device(dev);
111
112 /*
113 * The strange looking conversions that follow are fixed-point
114 * math, since we cannot do floating point in the kernel.
115 *
116 * Step 1: convert sense register to microVolts
117 * Step 2: convert voltage to milliAmperes
118 *
119 * If you play around with the V=IR equation, you come up with
120 * the following: X uV / Y mOhm == Z mA
121 *
122 * With the resistors that are fractions of a milliOhm, we multiply
123 * the voltage and resistance by 10, to shift the decimal point.
124 * Now we can use the normal division operator again.
125 */
126
127 /* Calculate voltage in microVolts (151 uV per increment) */
128 const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
129
130 /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
131 const unsigned int curr = voltage / 4;
132
133 return curr;
134}
135
136static ssize_t ltc4215_voltage_show(struct device *dev,
137 struct device_attribute *da, char *buf)
138{
139 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
140 const int voltage = ltc4215_get_voltage(dev, attr->index);
141
142 return sysfs_emit(buf, "%d\n", voltage);
143}
144
145static ssize_t ltc4215_current_show(struct device *dev,
146 struct device_attribute *da, char *buf)
147{
148 const unsigned int curr = ltc4215_get_current(dev);
149
150 return sysfs_emit(buf, "%u\n", curr);
151}
152
153static ssize_t ltc4215_power_show(struct device *dev,
154 struct device_attribute *da, char *buf)
155{
156 const unsigned int curr = ltc4215_get_current(dev);
157 const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
158
159 /* current in mA * voltage in mV == power in uW */
160 const unsigned int power = abs(output_voltage * curr);
161
162 return sysfs_emit(buf, "%u\n", power);
163}
164
165static ssize_t ltc4215_alarm_show(struct device *dev,
166 struct device_attribute *da, char *buf)
167{
168 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
169 struct ltc4215_data *data = ltc4215_update_device(dev);
170 const u8 reg = data->regs[LTC4215_STATUS];
171 const u32 mask = attr->index;
172
173 return sysfs_emit(buf, "%u\n", !!(reg & mask));
174}
175
176/*
177 * These macros are used below in constructing device attribute objects
178 * for use with sysfs_create_group() to make a sysfs device file
179 * for each register.
180 */
181
182/* Construct a sensor_device_attribute structure for each register */
183
184/* Current */
185static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4215_current, 0);
186static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4215_alarm, 1 << 2);
187
188/* Power (virtual) */
189static SENSOR_DEVICE_ATTR_RO(power1_input, ltc4215_power, 0);
190
191/* Input Voltage */
192static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4215_voltage, LTC4215_ADIN);
193static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4215_alarm, 1 << 0);
194static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4215_alarm, 1 << 1);
195
196/* Output Voltage */
197static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4215_voltage, LTC4215_SOURCE);
198static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc4215_alarm, 1 << 3);
199
200/*
201 * Finally, construct an array of pointers to members of the above objects,
202 * as required for sysfs_create_group()
203 */
204static struct attribute *ltc4215_attrs[] = {
205 &sensor_dev_attr_curr1_input.dev_attr.attr,
206 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
207
208 &sensor_dev_attr_power1_input.dev_attr.attr,
209
210 &sensor_dev_attr_in1_input.dev_attr.attr,
211 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
212 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
213
214 &sensor_dev_attr_in2_input.dev_attr.attr,
215 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
216
217 NULL,
218};
219ATTRIBUTE_GROUPS(ltc4215);
220
221static int ltc4215_probe(struct i2c_client *client)
222{
223 struct i2c_adapter *adapter = client->adapter;
224 struct device *dev = &client->dev;
225 struct ltc4215_data *data;
226 struct device *hwmon_dev;
227
228 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
229 return -ENODEV;
230
231 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
232 if (!data)
233 return -ENOMEM;
234
235 data->client = client;
236 mutex_init(&data->update_lock);
237
238 /* Initialize the LTC4215 chip */
239 i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
240
241 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
242 data,
243 ltc4215_groups);
244 return PTR_ERR_OR_ZERO(hwmon_dev);
245}
246
247static const struct i2c_device_id ltc4215_id[] = {
248 { "ltc4215", 0 },
249 { }
250};
251MODULE_DEVICE_TABLE(i2c, ltc4215_id);
252
253/* This is the driver that will be inserted */
254static struct i2c_driver ltc4215_driver = {
255 .driver = {
256 .name = "ltc4215",
257 },
258 .probe = ltc4215_probe,
259 .id_table = ltc4215_id,
260};
261
262module_i2c_driver(ltc4215_driver);
263
264MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
265MODULE_DESCRIPTION("LTC4215 driver");
266MODULE_LICENSE("GPL");