diff options
author | Ira Snyder <iws@ovro.caltech.edu> | 2009-01-07 10:37:32 -0500 |
---|---|---|
committer | Jean Delvare <khali@linux-fr.org> | 2009-01-07 10:37:32 -0500 |
commit | 6e34b187bc216fc632769fb8b906d3a29ccd8f14 (patch) | |
tree | b0b05a1019c1ca884261e22c647416c6e82c4b06 /drivers/hwmon | |
parent | 2f650631b3710622666367474b5475ff81ba486e (diff) |
hwmon: Add LTC4245 driver
Add Linux support for the Linear Technology LTC4245 Multiple Supply Hot
Swap controller I2C monitoring interface.
Signed-off-by: Ira W. Snyder <iws@ovro.caltech.edu>
Acked-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Jean Delvare <khali@linux-fr.org>
Diffstat (limited to 'drivers/hwmon')
-rw-r--r-- | drivers/hwmon/Kconfig | 11 | ||||
-rw-r--r-- | drivers/hwmon/Makefile | 1 | ||||
-rw-r--r-- | drivers/hwmon/ltc4245.c | 567 |
3 files changed, 579 insertions, 0 deletions
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index cc611e4b7896..1ef1205b4e8c 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig | |||
@@ -549,6 +549,17 @@ config SENSORS_LM93 | |||
549 | This driver can also be built as a module. If so, the module | 549 | This driver can also be built as a module. If so, the module |
550 | will be called lm93. | 550 | will be called lm93. |
551 | 551 | ||
552 | config SENSORS_LTC4245 | ||
553 | tristate "Linear Technology LTC4245" | ||
554 | depends on I2C && EXPERIMENTAL | ||
555 | default n | ||
556 | help | ||
557 | If you say yes here you get support for Linear Technology LTC4245 | ||
558 | Multiple Supply Hot Swap Controller I2C interface. | ||
559 | |||
560 | This driver can also be built as a module. If so, the module will | ||
561 | be called ltc4245. | ||
562 | |||
552 | config SENSORS_MAX1111 | 563 | config SENSORS_MAX1111 |
553 | tristate "Maxim MAX1111 Multichannel, Serial 8-bit ADC chip" | 564 | tristate "Maxim MAX1111 Multichannel, Serial 8-bit ADC chip" |
554 | depends on SPI_MASTER | 565 | depends on SPI_MASTER |
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index 58fc5be5355d..8fd124eff646 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile | |||
@@ -62,6 +62,7 @@ obj-$(CONFIG_SENSORS_LM87) += lm87.o | |||
62 | obj-$(CONFIG_SENSORS_LM90) += lm90.o | 62 | obj-$(CONFIG_SENSORS_LM90) += lm90.o |
63 | obj-$(CONFIG_SENSORS_LM92) += lm92.o | 63 | obj-$(CONFIG_SENSORS_LM92) += lm92.o |
64 | obj-$(CONFIG_SENSORS_LM93) += lm93.o | 64 | obj-$(CONFIG_SENSORS_LM93) += lm93.o |
65 | obj-$(CONFIG_SENSORS_LTC4245) += ltc4245.o | ||
65 | obj-$(CONFIG_SENSORS_MAX1111) += max1111.o | 66 | obj-$(CONFIG_SENSORS_MAX1111) += max1111.o |
66 | obj-$(CONFIG_SENSORS_MAX1619) += max1619.o | 67 | obj-$(CONFIG_SENSORS_MAX1619) += max1619.o |
67 | obj-$(CONFIG_SENSORS_MAX6650) += max6650.o | 68 | obj-$(CONFIG_SENSORS_MAX6650) += max6650.o |
diff --git a/drivers/hwmon/ltc4245.c b/drivers/hwmon/ltc4245.c new file mode 100644 index 000000000000..034b2c515848 --- /dev/null +++ b/drivers/hwmon/ltc4245.c | |||
@@ -0,0 +1,567 @@ | |||
1 | /* | ||
2 | * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller | ||
3 | * | ||
4 | * Copyright (C) 2008 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 | * This driver is based on the ds1621 and ina209 drivers. | ||
11 | * | ||
12 | * Datasheet: | ||
13 | * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1140,P19392,D13517 | ||
14 | */ | ||
15 | |||
16 | #include <linux/kernel.h> | ||
17 | #include <linux/module.h> | ||
18 | #include <linux/init.h> | ||
19 | #include <linux/err.h> | ||
20 | #include <linux/slab.h> | ||
21 | #include <linux/i2c.h> | ||
22 | #include <linux/hwmon.h> | ||
23 | #include <linux/hwmon-sysfs.h> | ||
24 | |||
25 | /* Valid addresses are 0x20 - 0x3f | ||
26 | * | ||
27 | * For now, we do not probe, since some of these addresses | ||
28 | * are known to be unfriendly to probing */ | ||
29 | static const unsigned short normal_i2c[] = { I2C_CLIENT_END }; | ||
30 | |||
31 | /* Insmod parameters */ | ||
32 | I2C_CLIENT_INSMOD_1(ltc4245); | ||
33 | |||
34 | /* Here are names of the chip's registers (a.k.a. commands) */ | ||
35 | enum ltc4245_cmd { | ||
36 | LTC4245_STATUS = 0x00, /* readonly */ | ||
37 | LTC4245_ALERT = 0x01, | ||
38 | LTC4245_CONTROL = 0x02, | ||
39 | LTC4245_ON = 0x03, | ||
40 | LTC4245_FAULT1 = 0x04, | ||
41 | LTC4245_FAULT2 = 0x05, | ||
42 | LTC4245_GPIO = 0x06, | ||
43 | LTC4245_ADCADR = 0x07, | ||
44 | |||
45 | LTC4245_12VIN = 0x10, | ||
46 | LTC4245_12VSENSE = 0x11, | ||
47 | LTC4245_12VOUT = 0x12, | ||
48 | LTC4245_5VIN = 0x13, | ||
49 | LTC4245_5VSENSE = 0x14, | ||
50 | LTC4245_5VOUT = 0x15, | ||
51 | LTC4245_3VIN = 0x16, | ||
52 | LTC4245_3VSENSE = 0x17, | ||
53 | LTC4245_3VOUT = 0x18, | ||
54 | LTC4245_VEEIN = 0x19, | ||
55 | LTC4245_VEESENSE = 0x1a, | ||
56 | LTC4245_VEEOUT = 0x1b, | ||
57 | LTC4245_GPIOADC1 = 0x1c, | ||
58 | LTC4245_GPIOADC2 = 0x1d, | ||
59 | LTC4245_GPIOADC3 = 0x1e, | ||
60 | }; | ||
61 | |||
62 | struct ltc4245_data { | ||
63 | struct device *hwmon_dev; | ||
64 | |||
65 | struct mutex update_lock; | ||
66 | bool valid; | ||
67 | unsigned long last_updated; /* in jiffies */ | ||
68 | |||
69 | /* Control registers */ | ||
70 | u8 cregs[0x08]; | ||
71 | |||
72 | /* Voltage registers */ | ||
73 | u8 vregs[0x0f]; | ||
74 | }; | ||
75 | |||
76 | static struct ltc4245_data *ltc4245_update_device(struct device *dev) | ||
77 | { | ||
78 | struct i2c_client *client = to_i2c_client(dev); | ||
79 | struct ltc4245_data *data = i2c_get_clientdata(client); | ||
80 | s32 val; | ||
81 | int i; | ||
82 | |||
83 | mutex_lock(&data->update_lock); | ||
84 | |||
85 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | ||
86 | |||
87 | dev_dbg(&client->dev, "Starting ltc4245 update\n"); | ||
88 | |||
89 | /* Read control registers -- 0x00 to 0x07 */ | ||
90 | for (i = 0; i < ARRAY_SIZE(data->cregs); i++) { | ||
91 | val = i2c_smbus_read_byte_data(client, i); | ||
92 | if (unlikely(val < 0)) | ||
93 | data->cregs[i] = 0; | ||
94 | else | ||
95 | data->cregs[i] = val; | ||
96 | } | ||
97 | |||
98 | /* Read voltage registers -- 0x10 to 0x1f */ | ||
99 | for (i = 0; i < ARRAY_SIZE(data->vregs); i++) { | ||
100 | val = i2c_smbus_read_byte_data(client, i+0x10); | ||
101 | if (unlikely(val < 0)) | ||
102 | data->vregs[i] = 0; | ||
103 | else | ||
104 | data->vregs[i] = val; | ||
105 | } | ||
106 | |||
107 | data->last_updated = jiffies; | ||
108 | data->valid = 1; | ||
109 | } | ||
110 | |||
111 | mutex_unlock(&data->update_lock); | ||
112 | |||
113 | return data; | ||
114 | } | ||
115 | |||
116 | /* Return the voltage from the given register in millivolts */ | ||
117 | static int ltc4245_get_voltage(struct device *dev, u8 reg) | ||
118 | { | ||
119 | struct ltc4245_data *data = ltc4245_update_device(dev); | ||
120 | const u8 regval = data->vregs[reg - 0x10]; | ||
121 | u32 voltage = 0; | ||
122 | |||
123 | switch (reg) { | ||
124 | case LTC4245_12VIN: | ||
125 | case LTC4245_12VOUT: | ||
126 | voltage = regval * 55; | ||
127 | break; | ||
128 | case LTC4245_5VIN: | ||
129 | case LTC4245_5VOUT: | ||
130 | voltage = regval * 22; | ||
131 | break; | ||
132 | case LTC4245_3VIN: | ||
133 | case LTC4245_3VOUT: | ||
134 | voltage = regval * 15; | ||
135 | break; | ||
136 | case LTC4245_VEEIN: | ||
137 | case LTC4245_VEEOUT: | ||
138 | voltage = regval * -55; | ||
139 | break; | ||
140 | case LTC4245_GPIOADC1: | ||
141 | case LTC4245_GPIOADC2: | ||
142 | case LTC4245_GPIOADC3: | ||
143 | voltage = regval * 10; | ||
144 | break; | ||
145 | default: | ||
146 | /* If we get here, the developer messed up */ | ||
147 | WARN_ON_ONCE(1); | ||
148 | break; | ||
149 | } | ||
150 | |||
151 | return voltage; | ||
152 | } | ||
153 | |||
154 | /* Return the current in the given sense register in milliAmperes */ | ||
155 | static unsigned int ltc4245_get_current(struct device *dev, u8 reg) | ||
156 | { | ||
157 | struct ltc4245_data *data = ltc4245_update_device(dev); | ||
158 | const u8 regval = data->vregs[reg - 0x10]; | ||
159 | unsigned int voltage; | ||
160 | unsigned int curr; | ||
161 | |||
162 | /* The strange looking conversions that follow are fixed-point | ||
163 | * math, since we cannot do floating point in the kernel. | ||
164 | * | ||
165 | * Step 1: convert sense register to microVolts | ||
166 | * Step 2: convert voltage to milliAmperes | ||
167 | * | ||
168 | * If you play around with the V=IR equation, you come up with | ||
169 | * the following: X uV / Y mOhm == Z mA | ||
170 | * | ||
171 | * With the resistors that are fractions of a milliOhm, we multiply | ||
172 | * the voltage and resistance by 10, to shift the decimal point. | ||
173 | * Now we can use the normal division operator again. | ||
174 | */ | ||
175 | |||
176 | switch (reg) { | ||
177 | case LTC4245_12VSENSE: | ||
178 | voltage = regval * 250; /* voltage in uV */ | ||
179 | curr = voltage / 50; /* sense resistor 50 mOhm */ | ||
180 | break; | ||
181 | case LTC4245_5VSENSE: | ||
182 | voltage = regval * 125; /* voltage in uV */ | ||
183 | curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */ | ||
184 | break; | ||
185 | case LTC4245_3VSENSE: | ||
186 | voltage = regval * 125; /* voltage in uV */ | ||
187 | curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */ | ||
188 | break; | ||
189 | case LTC4245_VEESENSE: | ||
190 | voltage = regval * 250; /* voltage in uV */ | ||
191 | curr = voltage / 100; /* sense resistor 100 mOhm */ | ||
192 | break; | ||
193 | default: | ||
194 | /* If we get here, the developer messed up */ | ||
195 | WARN_ON_ONCE(1); | ||
196 | curr = 0; | ||
197 | break; | ||
198 | } | ||
199 | |||
200 | return curr; | ||
201 | } | ||
202 | |||
203 | static ssize_t ltc4245_show_voltage(struct device *dev, | ||
204 | struct device_attribute *da, | ||
205 | char *buf) | ||
206 | { | ||
207 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | ||
208 | const int voltage = ltc4245_get_voltage(dev, attr->index); | ||
209 | |||
210 | return snprintf(buf, PAGE_SIZE, "%d\n", voltage); | ||
211 | } | ||
212 | |||
213 | static ssize_t ltc4245_show_current(struct device *dev, | ||
214 | struct device_attribute *da, | ||
215 | char *buf) | ||
216 | { | ||
217 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | ||
218 | const unsigned int curr = ltc4245_get_current(dev, attr->index); | ||
219 | |||
220 | return snprintf(buf, PAGE_SIZE, "%u\n", curr); | ||
221 | } | ||
222 | |||
223 | static ssize_t ltc4245_show_power(struct device *dev, | ||
224 | struct device_attribute *da, | ||
225 | char *buf) | ||
226 | { | ||
227 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | ||
228 | const unsigned int curr = ltc4245_get_current(dev, attr->index); | ||
229 | const int output_voltage = ltc4245_get_voltage(dev, attr->index+1); | ||
230 | |||
231 | /* current in mA * voltage in mV == power in uW */ | ||
232 | const unsigned int power = abs(output_voltage * curr); | ||
233 | |||
234 | return snprintf(buf, PAGE_SIZE, "%u\n", power); | ||
235 | } | ||
236 | |||
237 | static ssize_t ltc4245_show_alarm(struct device *dev, | ||
238 | struct device_attribute *da, | ||
239 | char *buf) | ||
240 | { | ||
241 | struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da); | ||
242 | struct ltc4245_data *data = ltc4245_update_device(dev); | ||
243 | const u8 reg = data->cregs[attr->index]; | ||
244 | const u32 mask = attr->nr; | ||
245 | |||
246 | return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0); | ||
247 | } | ||
248 | |||
249 | /* These macros are used below in constructing device attribute objects | ||
250 | * for use with sysfs_create_group() to make a sysfs device file | ||
251 | * for each register. | ||
252 | */ | ||
253 | |||
254 | #define LTC4245_VOLTAGE(name, ltc4245_cmd_idx) \ | ||
255 | static SENSOR_DEVICE_ATTR(name, S_IRUGO, \ | ||
256 | ltc4245_show_voltage, NULL, ltc4245_cmd_idx) | ||
257 | |||
258 | #define LTC4245_CURRENT(name, ltc4245_cmd_idx) \ | ||
259 | static SENSOR_DEVICE_ATTR(name, S_IRUGO, \ | ||
260 | ltc4245_show_current, NULL, ltc4245_cmd_idx) | ||
261 | |||
262 | #define LTC4245_POWER(name, ltc4245_cmd_idx) \ | ||
263 | static SENSOR_DEVICE_ATTR(name, S_IRUGO, \ | ||
264 | ltc4245_show_power, NULL, ltc4245_cmd_idx) | ||
265 | |||
266 | #define LTC4245_ALARM(name, mask, reg) \ | ||
267 | static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \ | ||
268 | ltc4245_show_alarm, NULL, (mask), reg) | ||
269 | |||
270 | /* Construct a sensor_device_attribute structure for each register */ | ||
271 | |||
272 | /* Input voltages */ | ||
273 | LTC4245_VOLTAGE(in1_input, LTC4245_12VIN); | ||
274 | LTC4245_VOLTAGE(in2_input, LTC4245_5VIN); | ||
275 | LTC4245_VOLTAGE(in3_input, LTC4245_3VIN); | ||
276 | LTC4245_VOLTAGE(in4_input, LTC4245_VEEIN); | ||
277 | |||
278 | /* Input undervoltage alarms */ | ||
279 | LTC4245_ALARM(in1_min_alarm, (1 << 0), LTC4245_FAULT1); | ||
280 | LTC4245_ALARM(in2_min_alarm, (1 << 1), LTC4245_FAULT1); | ||
281 | LTC4245_ALARM(in3_min_alarm, (1 << 2), LTC4245_FAULT1); | ||
282 | LTC4245_ALARM(in4_min_alarm, (1 << 3), LTC4245_FAULT1); | ||
283 | |||
284 | /* Currents (via sense resistor) */ | ||
285 | LTC4245_CURRENT(curr1_input, LTC4245_12VSENSE); | ||
286 | LTC4245_CURRENT(curr2_input, LTC4245_5VSENSE); | ||
287 | LTC4245_CURRENT(curr3_input, LTC4245_3VSENSE); | ||
288 | LTC4245_CURRENT(curr4_input, LTC4245_VEESENSE); | ||
289 | |||
290 | /* Overcurrent alarms */ | ||
291 | LTC4245_ALARM(curr1_max_alarm, (1 << 4), LTC4245_FAULT1); | ||
292 | LTC4245_ALARM(curr2_max_alarm, (1 << 5), LTC4245_FAULT1); | ||
293 | LTC4245_ALARM(curr3_max_alarm, (1 << 6), LTC4245_FAULT1); | ||
294 | LTC4245_ALARM(curr4_max_alarm, (1 << 7), LTC4245_FAULT1); | ||
295 | |||
296 | /* Output voltages */ | ||
297 | LTC4245_VOLTAGE(in5_input, LTC4245_12VOUT); | ||
298 | LTC4245_VOLTAGE(in6_input, LTC4245_5VOUT); | ||
299 | LTC4245_VOLTAGE(in7_input, LTC4245_3VOUT); | ||
300 | LTC4245_VOLTAGE(in8_input, LTC4245_VEEOUT); | ||
301 | |||
302 | /* Power Bad alarms */ | ||
303 | LTC4245_ALARM(in5_min_alarm, (1 << 0), LTC4245_FAULT2); | ||
304 | LTC4245_ALARM(in6_min_alarm, (1 << 1), LTC4245_FAULT2); | ||
305 | LTC4245_ALARM(in7_min_alarm, (1 << 2), LTC4245_FAULT2); | ||
306 | LTC4245_ALARM(in8_min_alarm, (1 << 3), LTC4245_FAULT2); | ||
307 | |||
308 | /* GPIO voltages */ | ||
309 | LTC4245_VOLTAGE(in9_input, LTC4245_GPIOADC1); | ||
310 | LTC4245_VOLTAGE(in10_input, LTC4245_GPIOADC2); | ||
311 | LTC4245_VOLTAGE(in11_input, LTC4245_GPIOADC3); | ||
312 | |||
313 | /* Power Consumption (virtual) */ | ||
314 | LTC4245_POWER(power1_input, LTC4245_12VSENSE); | ||
315 | LTC4245_POWER(power2_input, LTC4245_5VSENSE); | ||
316 | LTC4245_POWER(power3_input, LTC4245_3VSENSE); | ||
317 | LTC4245_POWER(power4_input, LTC4245_VEESENSE); | ||
318 | |||
319 | /* Finally, construct an array of pointers to members of the above objects, | ||
320 | * as required for sysfs_create_group() | ||
321 | */ | ||
322 | static struct attribute *ltc4245_attributes[] = { | ||
323 | &sensor_dev_attr_in1_input.dev_attr.attr, | ||
324 | &sensor_dev_attr_in2_input.dev_attr.attr, | ||
325 | &sensor_dev_attr_in3_input.dev_attr.attr, | ||
326 | &sensor_dev_attr_in4_input.dev_attr.attr, | ||
327 | |||
328 | &sensor_dev_attr_in1_min_alarm.dev_attr.attr, | ||
329 | &sensor_dev_attr_in2_min_alarm.dev_attr.attr, | ||
330 | &sensor_dev_attr_in3_min_alarm.dev_attr.attr, | ||
331 | &sensor_dev_attr_in4_min_alarm.dev_attr.attr, | ||
332 | |||
333 | &sensor_dev_attr_curr1_input.dev_attr.attr, | ||
334 | &sensor_dev_attr_curr2_input.dev_attr.attr, | ||
335 | &sensor_dev_attr_curr3_input.dev_attr.attr, | ||
336 | &sensor_dev_attr_curr4_input.dev_attr.attr, | ||
337 | |||
338 | &sensor_dev_attr_curr1_max_alarm.dev_attr.attr, | ||
339 | &sensor_dev_attr_curr2_max_alarm.dev_attr.attr, | ||
340 | &sensor_dev_attr_curr3_max_alarm.dev_attr.attr, | ||
341 | &sensor_dev_attr_curr4_max_alarm.dev_attr.attr, | ||
342 | |||
343 | &sensor_dev_attr_in5_input.dev_attr.attr, | ||
344 | &sensor_dev_attr_in6_input.dev_attr.attr, | ||
345 | &sensor_dev_attr_in7_input.dev_attr.attr, | ||
346 | &sensor_dev_attr_in8_input.dev_attr.attr, | ||
347 | |||
348 | &sensor_dev_attr_in5_min_alarm.dev_attr.attr, | ||
349 | &sensor_dev_attr_in6_min_alarm.dev_attr.attr, | ||
350 | &sensor_dev_attr_in7_min_alarm.dev_attr.attr, | ||
351 | &sensor_dev_attr_in8_min_alarm.dev_attr.attr, | ||
352 | |||
353 | &sensor_dev_attr_in9_input.dev_attr.attr, | ||
354 | &sensor_dev_attr_in10_input.dev_attr.attr, | ||
355 | &sensor_dev_attr_in11_input.dev_attr.attr, | ||
356 | |||
357 | &sensor_dev_attr_power1_input.dev_attr.attr, | ||
358 | &sensor_dev_attr_power2_input.dev_attr.attr, | ||
359 | &sensor_dev_attr_power3_input.dev_attr.attr, | ||
360 | &sensor_dev_attr_power4_input.dev_attr.attr, | ||
361 | |||
362 | NULL, | ||
363 | }; | ||
364 | |||
365 | static const struct attribute_group ltc4245_group = { | ||
366 | .attrs = ltc4245_attributes, | ||
367 | }; | ||
368 | |||
369 | static int ltc4245_probe(struct i2c_client *client, | ||
370 | const struct i2c_device_id *id) | ||
371 | { | ||
372 | struct ltc4245_data *data; | ||
373 | int ret; | ||
374 | |||
375 | data = kzalloc(sizeof(*data), GFP_KERNEL); | ||
376 | if (!data) { | ||
377 | ret = -ENOMEM; | ||
378 | goto out_kzalloc; | ||
379 | } | ||
380 | |||
381 | i2c_set_clientdata(client, data); | ||
382 | mutex_init(&data->update_lock); | ||
383 | |||
384 | /* Initialize the LTC4245 chip */ | ||
385 | /* TODO */ | ||
386 | |||
387 | /* Register sysfs hooks */ | ||
388 | ret = sysfs_create_group(&client->dev.kobj, <c4245_group); | ||
389 | if (ret) | ||
390 | goto out_sysfs_create_group; | ||
391 | |||
392 | data->hwmon_dev = hwmon_device_register(&client->dev); | ||
393 | if (IS_ERR(data->hwmon_dev)) { | ||
394 | ret = PTR_ERR(data->hwmon_dev); | ||
395 | goto out_hwmon_device_register; | ||
396 | } | ||
397 | |||
398 | return 0; | ||
399 | |||
400 | out_hwmon_device_register: | ||
401 | sysfs_remove_group(&client->dev.kobj, <c4245_group); | ||
402 | out_sysfs_create_group: | ||
403 | kfree(data); | ||
404 | out_kzalloc: | ||
405 | return ret; | ||
406 | } | ||
407 | |||
408 | static int ltc4245_remove(struct i2c_client *client) | ||
409 | { | ||
410 | struct ltc4245_data *data = i2c_get_clientdata(client); | ||
411 | |||
412 | hwmon_device_unregister(data->hwmon_dev); | ||
413 | sysfs_remove_group(&client->dev.kobj, <c4245_group); | ||
414 | |||
415 | kfree(data); | ||
416 | |||
417 | return 0; | ||
418 | } | ||
419 | |||
420 | /* Check that some bits in a control register appear at all possible | ||
421 | * locations without changing value | ||
422 | * | ||
423 | * @client: the i2c client to use | ||
424 | * @reg: the register to read | ||
425 | * @bits: the bits to check (0xff checks all bits, | ||
426 | * 0x03 checks only the last two bits) | ||
427 | * | ||
428 | * return -ERRNO if the register read failed | ||
429 | * return -ENODEV if the register value doesn't stay constant at all | ||
430 | * possible addresses | ||
431 | * | ||
432 | * return 0 for success | ||
433 | */ | ||
434 | static int ltc4245_check_control_reg(struct i2c_client *client, u8 reg, u8 bits) | ||
435 | { | ||
436 | int i; | ||
437 | s32 v, voff1, voff2; | ||
438 | |||
439 | /* Read register and check for error */ | ||
440 | v = i2c_smbus_read_byte_data(client, reg); | ||
441 | if (v < 0) | ||
442 | return v; | ||
443 | |||
444 | v &= bits; | ||
445 | |||
446 | for (i = 0x00; i < 0xff; i += 0x20) { | ||
447 | |||
448 | voff1 = i2c_smbus_read_byte_data(client, reg + i); | ||
449 | if (voff1 < 0) | ||
450 | return voff1; | ||
451 | |||
452 | voff2 = i2c_smbus_read_byte_data(client, reg + i + 0x08); | ||
453 | if (voff2 < 0) | ||
454 | return voff2; | ||
455 | |||
456 | voff1 &= bits; | ||
457 | voff2 &= bits; | ||
458 | |||
459 | if (v != voff1 || v != voff2) | ||
460 | return -ENODEV; | ||
461 | } | ||
462 | |||
463 | return 0; | ||
464 | } | ||
465 | |||
466 | static int ltc4245_detect(struct i2c_client *client, | ||
467 | int kind, | ||
468 | struct i2c_board_info *info) | ||
469 | { | ||
470 | struct i2c_adapter *adapter = client->adapter; | ||
471 | |||
472 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | ||
473 | return -ENODEV; | ||
474 | |||
475 | if (kind < 0) { /* probed detection - check the chip type */ | ||
476 | s32 v; /* 8 bits from the chip, or -ERRNO */ | ||
477 | |||
478 | /* Chip registers 0x00-0x07 are control registers | ||
479 | * Chip registers 0x10-0x1f are data registers | ||
480 | * | ||
481 | * Address bits b7-b5 are ignored. This makes the chip "repeat" | ||
482 | * in steps of 0x20. Any control registers should appear with | ||
483 | * the same values across all duplicated addresses. | ||
484 | * | ||
485 | * Register 0x02 bit b2 is reserved, expect 0 | ||
486 | * Register 0x07 bits b7 to b4 are reserved, expect 0 | ||
487 | * | ||
488 | * Registers 0x01, 0x02 are control registers and should not | ||
489 | * change on their own. | ||
490 | * | ||
491 | * Register 0x06 bits b6 and b7 are control bits, and should | ||
492 | * not change on their own. | ||
493 | * | ||
494 | * Register 0x07 bits b3 to b0 are control bits, and should | ||
495 | * not change on their own. | ||
496 | */ | ||
497 | |||
498 | /* read register 0x02 reserved bit, expect 0 */ | ||
499 | v = i2c_smbus_read_byte_data(client, LTC4245_CONTROL); | ||
500 | if (v < 0 || (v & 0x04) != 0) | ||
501 | return -ENODEV; | ||
502 | |||
503 | /* read register 0x07 reserved bits, expect 0 */ | ||
504 | v = i2c_smbus_read_byte_data(client, LTC4245_ADCADR); | ||
505 | if (v < 0 || (v & 0xf0) != 0) | ||
506 | return -ENODEV; | ||
507 | |||
508 | /* check that the alert register appears at all locations */ | ||
509 | if (ltc4245_check_control_reg(client, LTC4245_ALERT, 0xff)) | ||
510 | return -ENODEV; | ||
511 | |||
512 | /* check that the control register appears at all locations */ | ||
513 | if (ltc4245_check_control_reg(client, LTC4245_CONTROL, 0xff)) | ||
514 | return -ENODEV; | ||
515 | |||
516 | /* check that register 0x06 bits b6 and b7 stay constant */ | ||
517 | if (ltc4245_check_control_reg(client, LTC4245_GPIO, 0xc0)) | ||
518 | return -ENODEV; | ||
519 | |||
520 | /* check that register 0x07 bits b3-b0 stay constant */ | ||
521 | if (ltc4245_check_control_reg(client, LTC4245_ADCADR, 0x0f)) | ||
522 | return -ENODEV; | ||
523 | } | ||
524 | |||
525 | strlcpy(info->type, "ltc4245", I2C_NAME_SIZE); | ||
526 | dev_info(&adapter->dev, "ltc4245 %s at address 0x%02x\n", | ||
527 | kind < 0 ? "probed" : "forced", | ||
528 | client->addr); | ||
529 | |||
530 | return 0; | ||
531 | } | ||
532 | |||
533 | static const struct i2c_device_id ltc4245_id[] = { | ||
534 | { "ltc4245", ltc4245 }, | ||
535 | { } | ||
536 | }; | ||
537 | MODULE_DEVICE_TABLE(i2c, ltc4245_id); | ||
538 | |||
539 | /* This is the driver that will be inserted */ | ||
540 | static struct i2c_driver ltc4245_driver = { | ||
541 | .class = I2C_CLASS_HWMON, | ||
542 | .driver = { | ||
543 | .name = "ltc4245", | ||
544 | }, | ||
545 | .probe = ltc4245_probe, | ||
546 | .remove = ltc4245_remove, | ||
547 | .id_table = ltc4245_id, | ||
548 | .detect = ltc4245_detect, | ||
549 | .address_data = &addr_data, | ||
550 | }; | ||
551 | |||
552 | static int __init ltc4245_init(void) | ||
553 | { | ||
554 | return i2c_add_driver(<c4245_driver); | ||
555 | } | ||
556 | |||
557 | static void __exit ltc4245_exit(void) | ||
558 | { | ||
559 | i2c_del_driver(<c4245_driver); | ||
560 | } | ||
561 | |||
562 | MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>"); | ||
563 | MODULE_DESCRIPTION("LTC4245 driver"); | ||
564 | MODULE_LICENSE("GPL"); | ||
565 | |||
566 | module_init(ltc4245_init); | ||
567 | module_exit(ltc4245_exit); | ||