2 * Driver for Linear Technology LTC4151 High Voltage I2C Current
5 * Copyright (C) 2011 AppearTV AS
9 * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot
11 * Copyright (C) 2010 Ericsson AB.
13 * Datasheet: http://www.linear.com/docs/Datasheet/4151fc.pdf
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/err.h>
35 #include <linux/slab.h>
36 #include <linux/i2c.h>
37 #include <linux/hwmon.h>
38 #include <linux/hwmon-sysfs.h>
41 #define LTC4151_SENSE_H 0x00
42 #define LTC4151_SENSE_L 0x01
43 #define LTC4151_VIN_H 0x02
44 #define LTC4151_VIN_L 0x03
45 #define LTC4151_ADIN_H 0x04
46 #define LTC4151_ADIN_L 0x05
49 struct device
*hwmon_dev
;
51 struct mutex update_lock
;
53 unsigned long last_updated
; /* in jiffies */
59 static struct ltc4151_data
*ltc4151_update_device(struct device
*dev
)
61 struct i2c_client
*client
= to_i2c_client(dev
);
62 struct ltc4151_data
*data
= i2c_get_clientdata(client
);
63 struct ltc4151_data
*ret
= data
;
65 mutex_lock(&data
->update_lock
);
68 * The chip's A/D updates 6 times per second
69 * (Conversion Rate 6 - 9 Hz)
71 if (time_after(jiffies
, data
->last_updated
+ HZ
/ 6) || !data
->valid
) {
74 dev_dbg(&client
->dev
, "Starting ltc4151 update\n");
76 /* Read all registers */
77 for (i
= 0; i
< ARRAY_SIZE(data
->regs
); i
++) {
80 val
= i2c_smbus_read_byte_data(client
, i
);
81 if (unlikely(val
< 0)) {
83 "Failed to read ADC value: error %d\n",
90 data
->last_updated
= jiffies
;
94 mutex_unlock(&data
->update_lock
);
98 /* Return the voltage from the given register in mV */
99 static int ltc4151_get_value(struct ltc4151_data
*data
, u8 reg
)
103 val
= (data
->regs
[reg
] << 4) + (data
->regs
[reg
+ 1] >> 4);
107 /* 500uV resolution. Convert to mV. */
108 val
= val
* 500 / 1000;
110 case LTC4151_SENSE_H
:
112 * 20uV resolution. Convert to current as measured with
113 * an 1 mOhm sense resistor, in mA.
118 /* 25 mV per increment */
122 /* If we get here, the developer messed up */
131 static ssize_t
ltc4151_show_value(struct device
*dev
,
132 struct device_attribute
*da
, char *buf
)
134 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
135 struct ltc4151_data
*data
= ltc4151_update_device(dev
);
139 return PTR_ERR(data
);
141 value
= ltc4151_get_value(data
, attr
->index
);
142 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
148 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, \
149 ltc4151_show_value
, NULL
, LTC4151_VIN_H
);
150 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, \
151 ltc4151_show_value
, NULL
, LTC4151_ADIN_H
);
153 /* Currents (via sense resistor) */
154 static SENSOR_DEVICE_ATTR(curr1_input
, S_IRUGO
, \
155 ltc4151_show_value
, NULL
, LTC4151_SENSE_H
);
157 /* Finally, construct an array of pointers to members of the above objects,
158 * as required for sysfs_create_group()
160 static struct attribute
*ltc4151_attributes
[] = {
161 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
162 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
164 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
169 static const struct attribute_group ltc4151_group
= {
170 .attrs
= ltc4151_attributes
,
173 static int ltc4151_probe(struct i2c_client
*client
,
174 const struct i2c_device_id
*id
)
176 struct i2c_adapter
*adapter
= client
->adapter
;
177 struct ltc4151_data
*data
;
180 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
183 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
189 i2c_set_clientdata(client
, data
);
190 mutex_init(&data
->update_lock
);
192 /* Register sysfs hooks */
193 ret
= sysfs_create_group(&client
->dev
.kobj
, <c4151_group
);
195 goto out_sysfs_create_group
;
197 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
198 if (IS_ERR(data
->hwmon_dev
)) {
199 ret
= PTR_ERR(data
->hwmon_dev
);
200 goto out_hwmon_device_register
;
205 out_hwmon_device_register
:
206 sysfs_remove_group(&client
->dev
.kobj
, <c4151_group
);
207 out_sysfs_create_group
:
213 static int ltc4151_remove(struct i2c_client
*client
)
215 struct ltc4151_data
*data
= i2c_get_clientdata(client
);
217 hwmon_device_unregister(data
->hwmon_dev
);
218 sysfs_remove_group(&client
->dev
.kobj
, <c4151_group
);
225 static const struct i2c_device_id ltc4151_id
[] = {
229 MODULE_DEVICE_TABLE(i2c
, ltc4151_id
);
231 /* This is the driver that will be inserted */
232 static struct i2c_driver ltc4151_driver
= {
236 .probe
= ltc4151_probe
,
237 .remove
= ltc4151_remove
,
238 .id_table
= ltc4151_id
,
241 static int __init
ltc4151_init(void)
243 return i2c_add_driver(<c4151_driver
);
246 static void __exit
ltc4151_exit(void)
248 i2c_del_driver(<c4151_driver
);
251 MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
252 MODULE_DESCRIPTION("LTC4151 driver");
253 MODULE_LICENSE("GPL");
255 module_init(ltc4151_init
);
256 module_exit(ltc4151_exit
);