2 * Driver for Linear Technology LTC2990 power monitor
4 * Copyright (C) 2014 Topic Embedded Products
5 * Author: Mike Looijmans <mike.looijmans@topic.nl>
10 #include <linux/bitops.h>
11 #include <linux/err.h>
12 #include <linux/hwmon.h>
13 #include <linux/hwmon-sysfs.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
19 #define LTC2990_STATUS 0x00
20 #define LTC2990_CONTROL 0x01
21 #define LTC2990_TRIGGER 0x02
22 #define LTC2990_TINT_MSB 0x04
23 #define LTC2990_V1_MSB 0x06
24 #define LTC2990_V2_MSB 0x08
25 #define LTC2990_V3_MSB 0x0A
26 #define LTC2990_V4_MSB 0x0C
27 #define LTC2990_VCC_MSB 0x0E
29 #define LTC2990_IN0 BIT(0)
30 #define LTC2990_IN1 BIT(1)
31 #define LTC2990_IN2 BIT(2)
32 #define LTC2990_IN3 BIT(3)
33 #define LTC2990_IN4 BIT(4)
34 #define LTC2990_CURR1 BIT(5)
35 #define LTC2990_CURR2 BIT(6)
36 #define LTC2990_TEMP1 BIT(7)
37 #define LTC2990_TEMP2 BIT(8)
38 #define LTC2990_TEMP3 BIT(9)
39 #define LTC2990_NONE 0
40 #define LTC2990_ALL GENMASK(9, 0)
42 #define LTC2990_MODE0_SHIFT 0
43 #define LTC2990_MODE0_MASK GENMASK(2, 0)
44 #define LTC2990_MODE1_SHIFT 3
45 #define LTC2990_MODE1_MASK GENMASK(1, 0)
47 /* Enabled measurements for mode bits 2..0 */
48 static const int ltc2990_attrs_ena_0
[] = {
49 LTC2990_IN1
| LTC2990_IN2
| LTC2990_TEMP3
,
50 LTC2990_CURR1
| LTC2990_TEMP3
,
51 LTC2990_CURR1
| LTC2990_IN3
| LTC2990_IN4
,
52 LTC2990_TEMP2
| LTC2990_IN3
| LTC2990_IN4
,
53 LTC2990_TEMP2
| LTC2990_CURR2
,
54 LTC2990_TEMP2
| LTC2990_TEMP3
,
55 LTC2990_CURR1
| LTC2990_CURR2
,
56 LTC2990_IN1
| LTC2990_IN2
| LTC2990_IN3
| LTC2990_IN4
59 /* Enabled measurements for mode bits 4..3 */
60 static const int ltc2990_attrs_ena_1
[] = {
62 LTC2990_TEMP2
| LTC2990_IN1
| LTC2990_CURR1
,
63 LTC2990_TEMP3
| LTC2990_IN3
| LTC2990_CURR2
,
68 struct i2c_client
*i2c
;
72 /* Return the converted value from the given register in uV or mC */
73 static int ltc2990_get_value(struct i2c_client
*i2c
, int index
, int *result
)
80 reg
= LTC2990_VCC_MSB
;
99 reg
= LTC2990_TINT_MSB
;
105 val
= i2c_smbus_read_word_swapped(i2c
, reg
);
106 if (unlikely(val
< 0))
113 /* temp, 0.0625 degrees/LSB */
114 *result
= sign_extend32(val
, 12) * 1000 / 16;
118 /* Vx-Vy, 19.42uV/LSB */
119 *result
= sign_extend32(val
, 14) * 1942 / 100;
122 /* Vcc, 305.18uV/LSB, 2.5V offset */
123 *result
= sign_extend32(val
, 14) * 30518 / (100 * 1000) + 2500;
129 /* Vx, 305.18uV/LSB */
130 *result
= sign_extend32(val
, 14) * 30518 / (100 * 1000);
133 return -EINVAL
; /* won't happen, keep compiler happy */
139 static ssize_t
ltc2990_show_value(struct device
*dev
,
140 struct device_attribute
*da
, char *buf
)
142 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
143 struct ltc2990_data
*data
= dev_get_drvdata(dev
);
147 ret
= ltc2990_get_value(data
->i2c
, attr
->index
, &value
);
148 if (unlikely(ret
< 0))
151 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
154 static umode_t
ltc2990_attrs_visible(struct kobject
*kobj
,
155 struct attribute
*a
, int n
)
157 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
158 struct ltc2990_data
*data
= dev_get_drvdata(dev
);
159 struct device_attribute
*da
=
160 container_of(a
, struct device_attribute
, attr
);
161 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
163 int attrs_mask
= LTC2990_IN0
| LTC2990_TEMP1
|
164 (ltc2990_attrs_ena_0
[data
->mode
[0]] &
165 ltc2990_attrs_ena_1
[data
->mode
[1]]);
167 if (attr
->index
& attrs_mask
)
173 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, ltc2990_show_value
, NULL
,
175 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, ltc2990_show_value
, NULL
,
177 static SENSOR_DEVICE_ATTR(temp3_input
, S_IRUGO
, ltc2990_show_value
, NULL
,
179 static SENSOR_DEVICE_ATTR(curr1_input
, S_IRUGO
, ltc2990_show_value
, NULL
,
181 static SENSOR_DEVICE_ATTR(curr2_input
, S_IRUGO
, ltc2990_show_value
, NULL
,
183 static SENSOR_DEVICE_ATTR(in0_input
, S_IRUGO
, ltc2990_show_value
, NULL
,
185 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, ltc2990_show_value
, NULL
,
187 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, ltc2990_show_value
, NULL
,
189 static SENSOR_DEVICE_ATTR(in3_input
, S_IRUGO
, ltc2990_show_value
, NULL
,
191 static SENSOR_DEVICE_ATTR(in4_input
, S_IRUGO
, ltc2990_show_value
, NULL
,
194 static struct attribute
*ltc2990_attrs
[] = {
195 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
196 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
197 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
198 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
199 &sensor_dev_attr_curr2_input
.dev_attr
.attr
,
200 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
201 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
202 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
203 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
204 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
208 static const struct attribute_group ltc2990_group
= {
209 .attrs
= ltc2990_attrs
,
210 .is_visible
= ltc2990_attrs_visible
,
212 __ATTRIBUTE_GROUPS(ltc2990
);
214 static int ltc2990_i2c_probe(struct i2c_client
*i2c
,
215 const struct i2c_device_id
*id
)
218 struct device
*hwmon_dev
;
219 struct ltc2990_data
*data
;
220 struct device_node
*of_node
= i2c
->dev
.of_node
;
222 if (!i2c_check_functionality(i2c
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
|
223 I2C_FUNC_SMBUS_WORD_DATA
))
226 data
= devm_kzalloc(&i2c
->dev
, sizeof(struct ltc2990_data
), GFP_KERNEL
);
233 ret
= of_property_read_u32_array(of_node
, "lltc,meas-mode",
238 if (data
->mode
[0] & ~LTC2990_MODE0_MASK
||
239 data
->mode
[1] & ~LTC2990_MODE1_MASK
)
242 ret
= i2c_smbus_read_byte_data(i2c
, LTC2990_CONTROL
);
246 data
->mode
[0] = ret
>> LTC2990_MODE0_SHIFT
& LTC2990_MODE0_MASK
;
247 data
->mode
[1] = ret
>> LTC2990_MODE1_SHIFT
& LTC2990_MODE1_MASK
;
250 /* Setup continuous mode */
251 ret
= i2c_smbus_write_byte_data(i2c
, LTC2990_CONTROL
,
252 data
->mode
[0] << LTC2990_MODE0_SHIFT
|
253 data
->mode
[1] << LTC2990_MODE1_SHIFT
);
255 dev_err(&i2c
->dev
, "Error: Failed to set control mode.\n");
258 /* Trigger once to start continuous conversion */
259 ret
= i2c_smbus_write_byte_data(i2c
, LTC2990_TRIGGER
, 1);
261 dev_err(&i2c
->dev
, "Error: Failed to start acquisition.\n");
265 hwmon_dev
= devm_hwmon_device_register_with_groups(&i2c
->dev
,
270 return PTR_ERR_OR_ZERO(hwmon_dev
);
273 static const struct i2c_device_id ltc2990_i2c_id
[] = {
277 MODULE_DEVICE_TABLE(i2c
, ltc2990_i2c_id
);
279 static struct i2c_driver ltc2990_i2c_driver
= {
283 .probe
= ltc2990_i2c_probe
,
284 .id_table
= ltc2990_i2c_id
,
287 module_i2c_driver(ltc2990_i2c_driver
);
289 MODULE_DESCRIPTION("LTC2990 Sensor Driver");
290 MODULE_AUTHOR("Topic Embedded Products");
291 MODULE_LICENSE("GPL v2");