xref: /openbmc/linux/drivers/hwmon/ltc2990.c (revision ae213c44)
1 /*
2  * Driver for Linear Technology LTC2990 power monitor
3  *
4  * Copyright (C) 2014 Topic Embedded Products
5  * Author: Mike Looijmans <mike.looijmans@topic.nl>
6  *
7  * License: GPLv2
8  */
9 
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>
17 #include <linux/of.h>
18 
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
28 
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)
41 
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)
46 
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
57 };
58 
59 /* Enabled measurements for mode bits 4..3 */
60 static const int ltc2990_attrs_ena_1[] = {
61 	LTC2990_NONE,
62 	LTC2990_TEMP2 | LTC2990_IN1 | LTC2990_CURR1,
63 	LTC2990_TEMP3 | LTC2990_IN3 | LTC2990_CURR2,
64 	LTC2990_ALL
65 };
66 
67 struct ltc2990_data {
68 	struct i2c_client *i2c;
69 	u32 mode[2];
70 };
71 
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)
74 {
75 	int val;
76 	u8 reg;
77 
78 	switch (index) {
79 	case LTC2990_IN0:
80 		reg = LTC2990_VCC_MSB;
81 		break;
82 	case LTC2990_IN1:
83 	case LTC2990_CURR1:
84 	case LTC2990_TEMP2:
85 		reg = LTC2990_V1_MSB;
86 		break;
87 	case LTC2990_IN2:
88 		reg = LTC2990_V2_MSB;
89 		break;
90 	case LTC2990_IN3:
91 	case LTC2990_CURR2:
92 	case LTC2990_TEMP3:
93 		reg = LTC2990_V3_MSB;
94 		break;
95 	case LTC2990_IN4:
96 		reg = LTC2990_V4_MSB;
97 		break;
98 	case LTC2990_TEMP1:
99 		reg = LTC2990_TINT_MSB;
100 		break;
101 	default:
102 		return -EINVAL;
103 	}
104 
105 	val = i2c_smbus_read_word_swapped(i2c, reg);
106 	if (unlikely(val < 0))
107 		return val;
108 
109 	switch (index) {
110 	case LTC2990_TEMP1:
111 	case LTC2990_TEMP2:
112 	case LTC2990_TEMP3:
113 		/* temp, 0.0625 degrees/LSB */
114 		*result = sign_extend32(val, 12) * 1000 / 16;
115 		break;
116 	case LTC2990_CURR1:
117 	case LTC2990_CURR2:
118 		 /* Vx-Vy, 19.42uV/LSB */
119 		*result = sign_extend32(val, 14) * 1942 / 100;
120 		break;
121 	case LTC2990_IN0:
122 		/* Vcc, 305.18uV/LSB, 2.5V offset */
123 		*result = sign_extend32(val, 14) * 30518 / (100 * 1000) + 2500;
124 		break;
125 	case LTC2990_IN1:
126 	case LTC2990_IN2:
127 	case LTC2990_IN3:
128 	case LTC2990_IN4:
129 		/* Vx, 305.18uV/LSB */
130 		*result = sign_extend32(val, 14) * 30518 / (100 * 1000);
131 		break;
132 	default:
133 		return -EINVAL; /* won't happen, keep compiler happy */
134 	}
135 
136 	return 0;
137 }
138 
139 static ssize_t ltc2990_value_show(struct device *dev,
140 				  struct device_attribute *da, char *buf)
141 {
142 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
143 	struct ltc2990_data *data = dev_get_drvdata(dev);
144 	int value;
145 	int ret;
146 
147 	ret = ltc2990_get_value(data->i2c, attr->index, &value);
148 	if (unlikely(ret < 0))
149 		return ret;
150 
151 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
152 }
153 
154 static umode_t ltc2990_attrs_visible(struct kobject *kobj,
155 				     struct attribute *a, int n)
156 {
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);
162 
163 	int attrs_mask = LTC2990_IN0 | LTC2990_TEMP1 |
164 			 (ltc2990_attrs_ena_0[data->mode[0]] &
165 			  ltc2990_attrs_ena_1[data->mode[1]]);
166 
167 	if (attr->index & attrs_mask)
168 		return a->mode;
169 
170 	return 0;
171 }
172 
173 static SENSOR_DEVICE_ATTR_RO(temp1_input, ltc2990_value, LTC2990_TEMP1);
174 static SENSOR_DEVICE_ATTR_RO(temp2_input, ltc2990_value, LTC2990_TEMP2);
175 static SENSOR_DEVICE_ATTR_RO(temp3_input, ltc2990_value, LTC2990_TEMP3);
176 static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc2990_value, LTC2990_CURR1);
177 static SENSOR_DEVICE_ATTR_RO(curr2_input, ltc2990_value, LTC2990_CURR2);
178 static SENSOR_DEVICE_ATTR_RO(in0_input, ltc2990_value, LTC2990_IN0);
179 static SENSOR_DEVICE_ATTR_RO(in1_input, ltc2990_value, LTC2990_IN1);
180 static SENSOR_DEVICE_ATTR_RO(in2_input, ltc2990_value, LTC2990_IN2);
181 static SENSOR_DEVICE_ATTR_RO(in3_input, ltc2990_value, LTC2990_IN3);
182 static SENSOR_DEVICE_ATTR_RO(in4_input, ltc2990_value, LTC2990_IN4);
183 
184 static struct attribute *ltc2990_attrs[] = {
185 	&sensor_dev_attr_temp1_input.dev_attr.attr,
186 	&sensor_dev_attr_temp2_input.dev_attr.attr,
187 	&sensor_dev_attr_temp3_input.dev_attr.attr,
188 	&sensor_dev_attr_curr1_input.dev_attr.attr,
189 	&sensor_dev_attr_curr2_input.dev_attr.attr,
190 	&sensor_dev_attr_in0_input.dev_attr.attr,
191 	&sensor_dev_attr_in1_input.dev_attr.attr,
192 	&sensor_dev_attr_in2_input.dev_attr.attr,
193 	&sensor_dev_attr_in3_input.dev_attr.attr,
194 	&sensor_dev_attr_in4_input.dev_attr.attr,
195 	NULL,
196 };
197 
198 static const struct attribute_group ltc2990_group = {
199 	.attrs = ltc2990_attrs,
200 	.is_visible = ltc2990_attrs_visible,
201 };
202 __ATTRIBUTE_GROUPS(ltc2990);
203 
204 static int ltc2990_i2c_probe(struct i2c_client *i2c,
205 			     const struct i2c_device_id *id)
206 {
207 	int ret;
208 	struct device *hwmon_dev;
209 	struct ltc2990_data *data;
210 	struct device_node *of_node = i2c->dev.of_node;
211 
212 	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
213 				     I2C_FUNC_SMBUS_WORD_DATA))
214 		return -ENODEV;
215 
216 	data = devm_kzalloc(&i2c->dev, sizeof(struct ltc2990_data), GFP_KERNEL);
217 	if (unlikely(!data))
218 		return -ENOMEM;
219 
220 	data->i2c = i2c;
221 
222 	if (of_node) {
223 		ret = of_property_read_u32_array(of_node, "lltc,meas-mode",
224 						 data->mode, 2);
225 		if (ret < 0)
226 			return ret;
227 
228 		if (data->mode[0] & ~LTC2990_MODE0_MASK ||
229 		    data->mode[1] & ~LTC2990_MODE1_MASK)
230 			return -EINVAL;
231 	} else {
232 		ret = i2c_smbus_read_byte_data(i2c, LTC2990_CONTROL);
233 		if (ret < 0)
234 			return ret;
235 
236 		data->mode[0] = ret >> LTC2990_MODE0_SHIFT & LTC2990_MODE0_MASK;
237 		data->mode[1] = ret >> LTC2990_MODE1_SHIFT & LTC2990_MODE1_MASK;
238 	}
239 
240 	/* Setup continuous mode */
241 	ret = i2c_smbus_write_byte_data(i2c, LTC2990_CONTROL,
242 					data->mode[0] << LTC2990_MODE0_SHIFT |
243 					data->mode[1] << LTC2990_MODE1_SHIFT);
244 	if (ret < 0) {
245 		dev_err(&i2c->dev, "Error: Failed to set control mode.\n");
246 		return ret;
247 	}
248 	/* Trigger once to start continuous conversion */
249 	ret = i2c_smbus_write_byte_data(i2c, LTC2990_TRIGGER, 1);
250 	if (ret < 0) {
251 		dev_err(&i2c->dev, "Error: Failed to start acquisition.\n");
252 		return ret;
253 	}
254 
255 	hwmon_dev = devm_hwmon_device_register_with_groups(&i2c->dev,
256 							   i2c->name,
257 							   data,
258 							   ltc2990_groups);
259 
260 	return PTR_ERR_OR_ZERO(hwmon_dev);
261 }
262 
263 static const struct i2c_device_id ltc2990_i2c_id[] = {
264 	{ "ltc2990", 0 },
265 	{}
266 };
267 MODULE_DEVICE_TABLE(i2c, ltc2990_i2c_id);
268 
269 static struct i2c_driver ltc2990_i2c_driver = {
270 	.driver = {
271 		.name = "ltc2990",
272 	},
273 	.probe    = ltc2990_i2c_probe,
274 	.id_table = ltc2990_i2c_id,
275 };
276 
277 module_i2c_driver(ltc2990_i2c_driver);
278 
279 MODULE_DESCRIPTION("LTC2990 Sensor Driver");
280 MODULE_AUTHOR("Topic Embedded Products");
281 MODULE_LICENSE("GPL v2");
282