Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 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 |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Tom Levens | a6282d1 | 2017-07-03 06:28:58 +0200 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 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> |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 17 | #include <linux/of.h> |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 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 | |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 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 | }; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 71 | |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 72 | /* Return the converted value from the given register in uV or mC */ |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 73 | static int ltc2990_get_value(struct i2c_client *i2c, int index, int *result) |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 74 | { |
| 75 | int val; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 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 | } |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 104 | |
| 105 | val = i2c_smbus_read_word_swapped(i2c, reg); |
| 106 | if (unlikely(val < 0)) |
| 107 | return val; |
| 108 | |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 109 | switch (index) { |
| 110 | case LTC2990_TEMP1: |
| 111 | case LTC2990_TEMP2: |
| 112 | case LTC2990_TEMP3: |
| 113 | /* temp, 0.0625 degrees/LSB */ |
Tom Levens | a6282d1 | 2017-07-03 06:28:58 +0200 | [diff] [blame] | 114 | *result = sign_extend32(val, 12) * 1000 / 16; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 115 | break; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 116 | case LTC2990_CURR1: |
| 117 | case LTC2990_CURR2: |
| 118 | /* Vx-Vy, 19.42uV/LSB */ |
Tom Levens | a6282d1 | 2017-07-03 06:28:58 +0200 | [diff] [blame] | 119 | *result = sign_extend32(val, 14) * 1942 / 100; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 120 | break; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 121 | case LTC2990_IN0: |
| 122 | /* Vcc, 305.18uV/LSB, 2.5V offset */ |
Tom Levens | a6282d1 | 2017-07-03 06:28:58 +0200 | [diff] [blame] | 123 | *result = sign_extend32(val, 14) * 30518 / (100 * 1000) + 2500; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 124 | break; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 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; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 132 | default: |
| 133 | return -EINVAL; /* won't happen, keep compiler happy */ |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static ssize_t ltc2990_show_value(struct device *dev, |
| 140 | struct device_attribute *da, char *buf) |
| 141 | { |
| 142 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 143 | struct ltc2990_data *data = dev_get_drvdata(dev); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 144 | int value; |
| 145 | int ret; |
| 146 | |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 147 | ret = ltc2990_get_value(data->i2c, attr->index, &value); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 148 | if (unlikely(ret < 0)) |
| 149 | return ret; |
| 150 | |
| 151 | return snprintf(buf, PAGE_SIZE, "%d\n", value); |
| 152 | } |
| 153 | |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 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 | |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 173 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ltc2990_show_value, NULL, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 174 | LTC2990_TEMP1); |
| 175 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, ltc2990_show_value, NULL, |
| 176 | LTC2990_TEMP2); |
| 177 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, ltc2990_show_value, NULL, |
| 178 | LTC2990_TEMP3); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 179 | static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc2990_show_value, NULL, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 180 | LTC2990_CURR1); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 181 | static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO, ltc2990_show_value, NULL, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 182 | LTC2990_CURR2); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 183 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ltc2990_show_value, NULL, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 184 | LTC2990_IN0); |
| 185 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc2990_show_value, NULL, |
| 186 | LTC2990_IN1); |
| 187 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc2990_show_value, NULL, |
| 188 | LTC2990_IN2); |
| 189 | static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ltc2990_show_value, NULL, |
| 190 | LTC2990_IN3); |
| 191 | static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ltc2990_show_value, NULL, |
| 192 | LTC2990_IN4); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 193 | |
| 194 | static struct attribute *ltc2990_attrs[] = { |
| 195 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 196 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 197 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 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, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 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, |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 205 | NULL, |
| 206 | }; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 207 | |
| 208 | static const struct attribute_group ltc2990_group = { |
| 209 | .attrs = ltc2990_attrs, |
| 210 | .is_visible = ltc2990_attrs_visible, |
| 211 | }; |
| 212 | __ATTRIBUTE_GROUPS(ltc2990); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 213 | |
| 214 | static int ltc2990_i2c_probe(struct i2c_client *i2c, |
| 215 | const struct i2c_device_id *id) |
| 216 | { |
| 217 | int ret; |
| 218 | struct device *hwmon_dev; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 219 | struct ltc2990_data *data; |
| 220 | struct device_node *of_node = i2c->dev.of_node; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 221 | |
| 222 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 223 | I2C_FUNC_SMBUS_WORD_DATA)) |
| 224 | return -ENODEV; |
| 225 | |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 226 | data = devm_kzalloc(&i2c->dev, sizeof(struct ltc2990_data), GFP_KERNEL); |
| 227 | if (unlikely(!data)) |
| 228 | return -ENOMEM; |
| 229 | |
| 230 | data->i2c = i2c; |
| 231 | |
| 232 | if (of_node) { |
| 233 | ret = of_property_read_u32_array(of_node, "lltc,meas-mode", |
| 234 | data->mode, 2); |
| 235 | if (ret < 0) |
| 236 | return ret; |
| 237 | |
| 238 | if (data->mode[0] & ~LTC2990_MODE0_MASK || |
| 239 | data->mode[1] & ~LTC2990_MODE1_MASK) |
| 240 | return -EINVAL; |
| 241 | } else { |
| 242 | ret = i2c_smbus_read_byte_data(i2c, LTC2990_CONTROL); |
| 243 | if (ret < 0) |
| 244 | return ret; |
| 245 | |
| 246 | data->mode[0] = ret >> LTC2990_MODE0_SHIFT & LTC2990_MODE0_MASK; |
| 247 | data->mode[1] = ret >> LTC2990_MODE1_SHIFT & LTC2990_MODE1_MASK; |
| 248 | } |
| 249 | |
| 250 | /* Setup continuous mode */ |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 251 | ret = i2c_smbus_write_byte_data(i2c, LTC2990_CONTROL, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 252 | data->mode[0] << LTC2990_MODE0_SHIFT | |
| 253 | data->mode[1] << LTC2990_MODE1_SHIFT); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 254 | if (ret < 0) { |
| 255 | dev_err(&i2c->dev, "Error: Failed to set control mode.\n"); |
| 256 | return ret; |
| 257 | } |
| 258 | /* Trigger once to start continuous conversion */ |
| 259 | ret = i2c_smbus_write_byte_data(i2c, LTC2990_TRIGGER, 1); |
| 260 | if (ret < 0) { |
| 261 | dev_err(&i2c->dev, "Error: Failed to start acquisition.\n"); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | hwmon_dev = devm_hwmon_device_register_with_groups(&i2c->dev, |
| 266 | i2c->name, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame^] | 267 | data, |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 268 | ltc2990_groups); |
| 269 | |
| 270 | return PTR_ERR_OR_ZERO(hwmon_dev); |
| 271 | } |
| 272 | |
| 273 | static const struct i2c_device_id ltc2990_i2c_id[] = { |
| 274 | { "ltc2990", 0 }, |
| 275 | {} |
| 276 | }; |
| 277 | MODULE_DEVICE_TABLE(i2c, ltc2990_i2c_id); |
| 278 | |
| 279 | static struct i2c_driver ltc2990_i2c_driver = { |
| 280 | .driver = { |
| 281 | .name = "ltc2990", |
| 282 | }, |
| 283 | .probe = ltc2990_i2c_probe, |
| 284 | .id_table = ltc2990_i2c_id, |
| 285 | }; |
| 286 | |
| 287 | module_i2c_driver(ltc2990_i2c_driver); |
| 288 | |
| 289 | MODULE_DESCRIPTION("LTC2990 Sensor Driver"); |
| 290 | MODULE_AUTHOR("Topic Embedded Products"); |
| 291 | MODULE_LICENSE("GPL v2"); |