blob: 681b5b7b3c3b5d2b9de7316e1a217d2b2eaf164a [file] [log] [blame]
Ira Snyder6e34b182009-01-07 16:37:32 +01001/*
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>
Jean Delvaredcd8f392012-10-10 15:25:56 +020024#include <linux/jiffies.h>
Ira W. Snyder5950ec82010-08-14 21:08:49 +020025#include <linux/i2c/ltc4245.h>
Ira Snyder6e34b182009-01-07 16:37:32 +010026
Ira Snyder6e34b182009-01-07 16:37:32 +010027/* Here are names of the chip's registers (a.k.a. commands) */
28enum ltc4245_cmd {
29 LTC4245_STATUS = 0x00, /* readonly */
30 LTC4245_ALERT = 0x01,
31 LTC4245_CONTROL = 0x02,
32 LTC4245_ON = 0x03,
33 LTC4245_FAULT1 = 0x04,
34 LTC4245_FAULT2 = 0x05,
35 LTC4245_GPIO = 0x06,
36 LTC4245_ADCADR = 0x07,
37
38 LTC4245_12VIN = 0x10,
39 LTC4245_12VSENSE = 0x11,
40 LTC4245_12VOUT = 0x12,
41 LTC4245_5VIN = 0x13,
42 LTC4245_5VSENSE = 0x14,
43 LTC4245_5VOUT = 0x15,
44 LTC4245_3VIN = 0x16,
45 LTC4245_3VSENSE = 0x17,
46 LTC4245_3VOUT = 0x18,
47 LTC4245_VEEIN = 0x19,
48 LTC4245_VEESENSE = 0x1a,
49 LTC4245_VEEOUT = 0x1b,
Ira W. Snyderdf16dd52010-05-27 19:59:02 +020050 LTC4245_GPIOADC = 0x1c,
Ira Snyder6e34b182009-01-07 16:37:32 +010051};
52
53struct ltc4245_data {
Guenter Roeck9a85d532013-07-06 09:46:38 -070054 struct i2c_client *client;
55
56 const struct attribute_group *groups[3];
Ira Snyder6e34b182009-01-07 16:37:32 +010057
58 struct mutex update_lock;
59 bool valid;
60 unsigned long last_updated; /* in jiffies */
61
62 /* Control registers */
63 u8 cregs[0x08];
64
65 /* Voltage registers */
Ira W. Snyderdf16dd52010-05-27 19:59:02 +020066 u8 vregs[0x0d];
Ira W. Snyder5950ec82010-08-14 21:08:49 +020067
68 /* GPIO ADC registers */
69 bool use_extra_gpios;
70 int gpios[3];
Ira Snyder6e34b182009-01-07 16:37:32 +010071};
72
Ira W. Snyder5950ec82010-08-14 21:08:49 +020073/*
74 * Update the readings from the GPIO pins. If the driver has been configured to
75 * sample all GPIO's as analog voltages, a round-robin sampling method is used.
76 * Otherwise, only the configured GPIO pin is sampled.
77 *
78 * LOCKING: must hold data->update_lock
79 */
80static void ltc4245_update_gpios(struct device *dev)
81{
Guenter Roeck9a85d532013-07-06 09:46:38 -070082 struct ltc4245_data *data = dev_get_drvdata(dev);
83 struct i2c_client *client = data->client;
Ira W. Snyder5950ec82010-08-14 21:08:49 +020084 u8 gpio_curr, gpio_next, gpio_reg;
85 int i;
86
87 /* no extra gpio support, we're basically done */
88 if (!data->use_extra_gpios) {
89 data->gpios[0] = data->vregs[LTC4245_GPIOADC - 0x10];
90 return;
91 }
92
93 /*
94 * If the last reading was too long ago, then we mark all old GPIO
95 * readings as stale by setting them to -EAGAIN
96 */
97 if (time_after(jiffies, data->last_updated + 5 * HZ)) {
Ira W. Snyder5950ec82010-08-14 21:08:49 +020098 for (i = 0; i < ARRAY_SIZE(data->gpios); i++)
99 data->gpios[i] = -EAGAIN;
100 }
101
102 /*
103 * Get the current GPIO pin
104 *
105 * The datasheet calls these GPIO[1-3], but we'll calculate the zero
106 * based array index instead, and call them GPIO[0-2]. This is much
107 * easier to think about.
108 */
109 gpio_curr = (data->cregs[LTC4245_GPIO] & 0xc0) >> 6;
110 if (gpio_curr > 0)
111 gpio_curr -= 1;
112
113 /* Read the GPIO voltage from the GPIOADC register */
114 data->gpios[gpio_curr] = data->vregs[LTC4245_GPIOADC - 0x10];
115
116 /* Find the next GPIO pin to read */
117 gpio_next = (gpio_curr + 1) % ARRAY_SIZE(data->gpios);
118
119 /*
120 * Calculate the correct setting for the GPIO register so it will
121 * sample the next GPIO pin
122 */
123 gpio_reg = (data->cregs[LTC4245_GPIO] & 0x3f) | ((gpio_next + 1) << 6);
124
125 /* Update the GPIO register */
126 i2c_smbus_write_byte_data(client, LTC4245_GPIO, gpio_reg);
127
128 /* Update saved data */
129 data->cregs[LTC4245_GPIO] = gpio_reg;
130}
131
Ira Snyder6e34b182009-01-07 16:37:32 +0100132static struct ltc4245_data *ltc4245_update_device(struct device *dev)
133{
Guenter Roeck9a85d532013-07-06 09:46:38 -0700134 struct ltc4245_data *data = dev_get_drvdata(dev);
135 struct i2c_client *client = data->client;
Ira Snyder6e34b182009-01-07 16:37:32 +0100136 s32 val;
137 int i;
138
139 mutex_lock(&data->update_lock);
140
141 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
142
Ira Snyder6e34b182009-01-07 16:37:32 +0100143 /* Read control registers -- 0x00 to 0x07 */
144 for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
145 val = i2c_smbus_read_byte_data(client, i);
146 if (unlikely(val < 0))
147 data->cregs[i] = 0;
148 else
149 data->cregs[i] = val;
150 }
151
Ira W. Snyderdf16dd52010-05-27 19:59:02 +0200152 /* Read voltage registers -- 0x10 to 0x1c */
Ira Snyder6e34b182009-01-07 16:37:32 +0100153 for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
154 val = i2c_smbus_read_byte_data(client, i+0x10);
155 if (unlikely(val < 0))
156 data->vregs[i] = 0;
157 else
158 data->vregs[i] = val;
159 }
160
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200161 /* Update GPIO readings */
162 ltc4245_update_gpios(dev);
163
Ira Snyder6e34b182009-01-07 16:37:32 +0100164 data->last_updated = jiffies;
165 data->valid = 1;
166 }
167
168 mutex_unlock(&data->update_lock);
169
170 return data;
171}
172
173/* Return the voltage from the given register in millivolts */
174static int ltc4245_get_voltage(struct device *dev, u8 reg)
175{
176 struct ltc4245_data *data = ltc4245_update_device(dev);
177 const u8 regval = data->vregs[reg - 0x10];
178 u32 voltage = 0;
179
180 switch (reg) {
181 case LTC4245_12VIN:
182 case LTC4245_12VOUT:
183 voltage = regval * 55;
184 break;
185 case LTC4245_5VIN:
186 case LTC4245_5VOUT:
187 voltage = regval * 22;
188 break;
189 case LTC4245_3VIN:
190 case LTC4245_3VOUT:
191 voltage = regval * 15;
192 break;
193 case LTC4245_VEEIN:
194 case LTC4245_VEEOUT:
195 voltage = regval * -55;
196 break;
Ira W. Snyderdf16dd52010-05-27 19:59:02 +0200197 case LTC4245_GPIOADC:
Ira Snyder6e34b182009-01-07 16:37:32 +0100198 voltage = regval * 10;
199 break;
200 default:
201 /* If we get here, the developer messed up */
202 WARN_ON_ONCE(1);
203 break;
204 }
205
206 return voltage;
207}
208
209/* Return the current in the given sense register in milliAmperes */
210static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
211{
212 struct ltc4245_data *data = ltc4245_update_device(dev);
213 const u8 regval = data->vregs[reg - 0x10];
214 unsigned int voltage;
215 unsigned int curr;
216
Guenter Roeck430b4fc2012-01-19 11:02:22 -0800217 /*
218 * The strange looking conversions that follow are fixed-point
Ira Snyder6e34b182009-01-07 16:37:32 +0100219 * math, since we cannot do floating point in the kernel.
220 *
221 * Step 1: convert sense register to microVolts
222 * Step 2: convert voltage to milliAmperes
223 *
224 * If you play around with the V=IR equation, you come up with
225 * the following: X uV / Y mOhm == Z mA
226 *
227 * With the resistors that are fractions of a milliOhm, we multiply
228 * the voltage and resistance by 10, to shift the decimal point.
229 * Now we can use the normal division operator again.
230 */
231
232 switch (reg) {
233 case LTC4245_12VSENSE:
234 voltage = regval * 250; /* voltage in uV */
235 curr = voltage / 50; /* sense resistor 50 mOhm */
236 break;
237 case LTC4245_5VSENSE:
238 voltage = regval * 125; /* voltage in uV */
239 curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
240 break;
241 case LTC4245_3VSENSE:
242 voltage = regval * 125; /* voltage in uV */
243 curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
244 break;
245 case LTC4245_VEESENSE:
246 voltage = regval * 250; /* voltage in uV */
247 curr = voltage / 100; /* sense resistor 100 mOhm */
248 break;
249 default:
250 /* If we get here, the developer messed up */
251 WARN_ON_ONCE(1);
252 curr = 0;
253 break;
254 }
255
256 return curr;
257}
258
259static ssize_t ltc4245_show_voltage(struct device *dev,
260 struct device_attribute *da,
261 char *buf)
262{
263 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
264 const int voltage = ltc4245_get_voltage(dev, attr->index);
265
266 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
267}
268
269static ssize_t ltc4245_show_current(struct device *dev,
270 struct device_attribute *da,
271 char *buf)
272{
273 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
274 const unsigned int curr = ltc4245_get_current(dev, attr->index);
275
276 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
277}
278
279static ssize_t ltc4245_show_power(struct device *dev,
280 struct device_attribute *da,
281 char *buf)
282{
283 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
284 const unsigned int curr = ltc4245_get_current(dev, attr->index);
285 const int output_voltage = ltc4245_get_voltage(dev, attr->index+1);
286
287 /* current in mA * voltage in mV == power in uW */
288 const unsigned int power = abs(output_voltage * curr);
289
290 return snprintf(buf, PAGE_SIZE, "%u\n", power);
291}
292
293static ssize_t ltc4245_show_alarm(struct device *dev,
294 struct device_attribute *da,
295 char *buf)
296{
297 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
298 struct ltc4245_data *data = ltc4245_update_device(dev);
299 const u8 reg = data->cregs[attr->index];
300 const u32 mask = attr->nr;
301
302 return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
303}
304
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200305static ssize_t ltc4245_show_gpio(struct device *dev,
306 struct device_attribute *da,
307 char *buf)
308{
309 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
310 struct ltc4245_data *data = ltc4245_update_device(dev);
311 int val = data->gpios[attr->index];
312
313 /* handle stale GPIO's */
314 if (val < 0)
315 return val;
316
317 /* Convert to millivolts and print */
318 return snprintf(buf, PAGE_SIZE, "%u\n", val * 10);
319}
320
Ira Snyder6e34b182009-01-07 16:37:32 +0100321/* Construct a sensor_device_attribute structure for each register */
322
323/* Input voltages */
Guenter Roeckf5390382013-01-10 10:48:34 -0800324static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4245_show_voltage, NULL,
325 LTC4245_12VIN);
326static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4245_show_voltage, NULL,
327 LTC4245_5VIN);
328static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ltc4245_show_voltage, NULL,
329 LTC4245_3VIN);
330static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ltc4245_show_voltage, NULL,
331 LTC4245_VEEIN);
Ira Snyder6e34b182009-01-07 16:37:32 +0100332
333/* Input undervoltage alarms */
Guenter Roeckf5390382013-01-10 10:48:34 -0800334static SENSOR_DEVICE_ATTR_2(in1_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
335 1 << 0, LTC4245_FAULT1);
336static SENSOR_DEVICE_ATTR_2(in2_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
337 1 << 1, LTC4245_FAULT1);
338static SENSOR_DEVICE_ATTR_2(in3_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
339 1 << 2, LTC4245_FAULT1);
340static SENSOR_DEVICE_ATTR_2(in4_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
341 1 << 3, LTC4245_FAULT1);
Ira Snyder6e34b182009-01-07 16:37:32 +0100342
343/* Currents (via sense resistor) */
Guenter Roeckf5390382013-01-10 10:48:34 -0800344static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4245_show_current, NULL,
345 LTC4245_12VSENSE);
346static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO, ltc4245_show_current, NULL,
347 LTC4245_5VSENSE);
348static SENSOR_DEVICE_ATTR(curr3_input, S_IRUGO, ltc4245_show_current, NULL,
349 LTC4245_3VSENSE);
350static SENSOR_DEVICE_ATTR(curr4_input, S_IRUGO, ltc4245_show_current, NULL,
351 LTC4245_VEESENSE);
Ira Snyder6e34b182009-01-07 16:37:32 +0100352
353/* Overcurrent alarms */
Guenter Roeckf5390382013-01-10 10:48:34 -0800354static SENSOR_DEVICE_ATTR_2(curr1_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
355 1 << 4, LTC4245_FAULT1);
356static SENSOR_DEVICE_ATTR_2(curr2_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
357 1 << 5, LTC4245_FAULT1);
358static SENSOR_DEVICE_ATTR_2(curr3_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
359 1 << 6, LTC4245_FAULT1);
360static SENSOR_DEVICE_ATTR_2(curr4_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
361 1 << 7, LTC4245_FAULT1);
Ira Snyder6e34b182009-01-07 16:37:32 +0100362
363/* Output voltages */
Guenter Roeckf5390382013-01-10 10:48:34 -0800364static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, ltc4245_show_voltage, NULL,
365 LTC4245_12VOUT);
366static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ltc4245_show_voltage, NULL,
367 LTC4245_5VOUT);
368static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, ltc4245_show_voltage, NULL,
369 LTC4245_3VOUT);
370static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, ltc4245_show_voltage, NULL,
371 LTC4245_VEEOUT);
Ira Snyder6e34b182009-01-07 16:37:32 +0100372
373/* Power Bad alarms */
Guenter Roeckf5390382013-01-10 10:48:34 -0800374static SENSOR_DEVICE_ATTR_2(in5_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
375 1 << 0, LTC4245_FAULT2);
376static SENSOR_DEVICE_ATTR_2(in6_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
377 1 << 1, LTC4245_FAULT2);
378static SENSOR_DEVICE_ATTR_2(in7_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
379 1 << 2, LTC4245_FAULT2);
380static SENSOR_DEVICE_ATTR_2(in8_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
381 1 << 3, LTC4245_FAULT2);
Ira Snyder6e34b182009-01-07 16:37:32 +0100382
383/* GPIO voltages */
Guenter Roeckf5390382013-01-10 10:48:34 -0800384static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, ltc4245_show_gpio, NULL, 0);
385static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, ltc4245_show_gpio, NULL, 1);
386static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, ltc4245_show_gpio, NULL, 2);
Ira Snyder6e34b182009-01-07 16:37:32 +0100387
388/* Power Consumption (virtual) */
Guenter Roeckf5390382013-01-10 10:48:34 -0800389static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ltc4245_show_power, NULL,
390 LTC4245_12VSENSE);
391static SENSOR_DEVICE_ATTR(power2_input, S_IRUGO, ltc4245_show_power, NULL,
392 LTC4245_5VSENSE);
393static SENSOR_DEVICE_ATTR(power3_input, S_IRUGO, ltc4245_show_power, NULL,
394 LTC4245_3VSENSE);
395static SENSOR_DEVICE_ATTR(power4_input, S_IRUGO, ltc4245_show_power, NULL,
396 LTC4245_VEESENSE);
Ira Snyder6e34b182009-01-07 16:37:32 +0100397
Guenter Roeck430b4fc2012-01-19 11:02:22 -0800398/*
399 * Finally, construct an array of pointers to members of the above objects,
Ira Snyder6e34b182009-01-07 16:37:32 +0100400 * as required for sysfs_create_group()
401 */
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200402static struct attribute *ltc4245_std_attributes[] = {
Ira Snyder6e34b182009-01-07 16:37:32 +0100403 &sensor_dev_attr_in1_input.dev_attr.attr,
404 &sensor_dev_attr_in2_input.dev_attr.attr,
405 &sensor_dev_attr_in3_input.dev_attr.attr,
406 &sensor_dev_attr_in4_input.dev_attr.attr,
407
408 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
409 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
410 &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
411 &sensor_dev_attr_in4_min_alarm.dev_attr.attr,
412
413 &sensor_dev_attr_curr1_input.dev_attr.attr,
414 &sensor_dev_attr_curr2_input.dev_attr.attr,
415 &sensor_dev_attr_curr3_input.dev_attr.attr,
416 &sensor_dev_attr_curr4_input.dev_attr.attr,
417
418 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
419 &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
420 &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
421 &sensor_dev_attr_curr4_max_alarm.dev_attr.attr,
422
423 &sensor_dev_attr_in5_input.dev_attr.attr,
424 &sensor_dev_attr_in6_input.dev_attr.attr,
425 &sensor_dev_attr_in7_input.dev_attr.attr,
426 &sensor_dev_attr_in8_input.dev_attr.attr,
427
428 &sensor_dev_attr_in5_min_alarm.dev_attr.attr,
429 &sensor_dev_attr_in6_min_alarm.dev_attr.attr,
430 &sensor_dev_attr_in7_min_alarm.dev_attr.attr,
431 &sensor_dev_attr_in8_min_alarm.dev_attr.attr,
432
433 &sensor_dev_attr_in9_input.dev_attr.attr,
Ira Snyder6e34b182009-01-07 16:37:32 +0100434
435 &sensor_dev_attr_power1_input.dev_attr.attr,
436 &sensor_dev_attr_power2_input.dev_attr.attr,
437 &sensor_dev_attr_power3_input.dev_attr.attr,
438 &sensor_dev_attr_power4_input.dev_attr.attr,
439
440 NULL,
441};
442
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200443static struct attribute *ltc4245_gpio_attributes[] = {
444 &sensor_dev_attr_in10_input.dev_attr.attr,
445 &sensor_dev_attr_in11_input.dev_attr.attr,
446 NULL,
Ira Snyder6e34b182009-01-07 16:37:32 +0100447};
448
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200449static const struct attribute_group ltc4245_std_group = {
450 .attrs = ltc4245_std_attributes,
451};
452
453static const struct attribute_group ltc4245_gpio_group = {
454 .attrs = ltc4245_gpio_attributes,
455};
456
Guenter Roeck9a85d532013-07-06 09:46:38 -0700457static void ltc4245_sysfs_add_groups(struct ltc4245_data *data)
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200458{
Guenter Roeck9a85d532013-07-06 09:46:38 -0700459 /* standard sysfs attributes */
460 data->groups[0] = &ltc4245_std_group;
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200461
462 /* if we're using the extra gpio support, register it's attributes */
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200463 if (data->use_extra_gpios)
Guenter Roeck9a85d532013-07-06 09:46:38 -0700464 data->groups[1] = &ltc4245_gpio_group;
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200465}
466
467static bool ltc4245_use_extra_gpios(struct i2c_client *client)
468{
469 struct ltc4245_platform_data *pdata = dev_get_platdata(&client->dev);
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200470 struct device_node *np = client->dev.of_node;
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200471
472 /* prefer platform data */
473 if (pdata)
474 return pdata->use_extra_gpios;
475
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200476 /* fallback on OF */
477 if (of_find_property(np, "ltc4245,use-extra-gpios", NULL))
478 return true;
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200479
480 return false;
481}
482
Ira Snyder6e34b182009-01-07 16:37:32 +0100483static int ltc4245_probe(struct i2c_client *client,
484 const struct i2c_device_id *id)
485{
Jean Delvare2d2a7cf2009-10-04 22:53:42 +0200486 struct i2c_adapter *adapter = client->adapter;
Ira Snyder6e34b182009-01-07 16:37:32 +0100487 struct ltc4245_data *data;
Guenter Roeck9a85d532013-07-06 09:46:38 -0700488 struct device *hwmon_dev;
Ira Snyder6e34b182009-01-07 16:37:32 +0100489
Jean Delvare2d2a7cf2009-10-04 22:53:42 +0200490 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
491 return -ENODEV;
492
Guenter Roeckeffb8ab2012-06-02 10:35:52 -0700493 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
494 if (!data)
495 return -ENOMEM;
Ira Snyder6e34b182009-01-07 16:37:32 +0100496
Guenter Roeck9a85d532013-07-06 09:46:38 -0700497 data->client = client;
Ira Snyder6e34b182009-01-07 16:37:32 +0100498 mutex_init(&data->update_lock);
Ira W. Snyder5950ec82010-08-14 21:08:49 +0200499 data->use_extra_gpios = ltc4245_use_extra_gpios(client);
Ira Snyder6e34b182009-01-07 16:37:32 +0100500
501 /* Initialize the LTC4245 chip */
Ira W. Snyder58f055e2009-09-23 22:59:44 +0200502 i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
503 i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
Ira Snyder6e34b182009-01-07 16:37:32 +0100504
Guenter Roeck9a85d532013-07-06 09:46:38 -0700505 /* Add sysfs hooks */
506 ltc4245_sysfs_add_groups(data);
Ira Snyder6e34b182009-01-07 16:37:32 +0100507
Guenter Roeck94c42cf2014-02-02 14:12:40 -0800508 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
509 client->name, data,
510 data->groups);
511 return PTR_ERR_OR_ZERO(hwmon_dev);
Ira Snyder6e34b182009-01-07 16:37:32 +0100512}
513
Ira Snyder6e34b182009-01-07 16:37:32 +0100514static const struct i2c_device_id ltc4245_id[] = {
Jean Delvare2d2a7cf2009-10-04 22:53:42 +0200515 { "ltc4245", 0 },
Ira Snyder6e34b182009-01-07 16:37:32 +0100516 { }
517};
518MODULE_DEVICE_TABLE(i2c, ltc4245_id);
519
520/* This is the driver that will be inserted */
521static struct i2c_driver ltc4245_driver = {
Ira Snyder6e34b182009-01-07 16:37:32 +0100522 .driver = {
523 .name = "ltc4245",
524 },
525 .probe = ltc4245_probe,
Ira Snyder6e34b182009-01-07 16:37:32 +0100526 .id_table = ltc4245_id,
Ira Snyder6e34b182009-01-07 16:37:32 +0100527};
528
Axel Linf0967ee2012-01-20 15:38:18 +0800529module_i2c_driver(ltc4245_driver);
Ira Snyder6e34b182009-01-07 16:37:32 +0100530
531MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
532MODULE_DESCRIPTION("LTC4245 driver");
533MODULE_LICENSE("GPL");