Thomas Gleixner | 3e5580c | 2019-05-29 07:12:27 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Driver for Linear Technology LTC2990 power monitor |
| 4 | * |
| 5 | * Copyright (C) 2014 Topic Embedded Products |
| 6 | * Author: Mike Looijmans <mike.looijmans@topic.nl> |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Tom Levens | a6282d1 | 2017-07-03 06:28:58 +0200 | [diff] [blame] | 9 | #include <linux/bitops.h> |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 10 | #include <linux/err.h> |
| 11 | #include <linux/hwmon.h> |
| 12 | #include <linux/hwmon-sysfs.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
Max Staudt | 06a1c69 | 2019-08-19 14:16:17 +0200 | [diff] [blame] | 16 | #include <linux/property.h> |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 17 | |
| 18 | #define LTC2990_STATUS 0x00 |
| 19 | #define LTC2990_CONTROL 0x01 |
| 20 | #define LTC2990_TRIGGER 0x02 |
| 21 | #define LTC2990_TINT_MSB 0x04 |
| 22 | #define LTC2990_V1_MSB 0x06 |
| 23 | #define LTC2990_V2_MSB 0x08 |
| 24 | #define LTC2990_V3_MSB 0x0A |
| 25 | #define LTC2990_V4_MSB 0x0C |
| 26 | #define LTC2990_VCC_MSB 0x0E |
| 27 | |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 28 | #define LTC2990_IN0 BIT(0) |
| 29 | #define LTC2990_IN1 BIT(1) |
| 30 | #define LTC2990_IN2 BIT(2) |
| 31 | #define LTC2990_IN3 BIT(3) |
| 32 | #define LTC2990_IN4 BIT(4) |
| 33 | #define LTC2990_CURR1 BIT(5) |
| 34 | #define LTC2990_CURR2 BIT(6) |
| 35 | #define LTC2990_TEMP1 BIT(7) |
| 36 | #define LTC2990_TEMP2 BIT(8) |
| 37 | #define LTC2990_TEMP3 BIT(9) |
| 38 | #define LTC2990_NONE 0 |
| 39 | #define LTC2990_ALL GENMASK(9, 0) |
| 40 | |
| 41 | #define LTC2990_MODE0_SHIFT 0 |
| 42 | #define LTC2990_MODE0_MASK GENMASK(2, 0) |
| 43 | #define LTC2990_MODE1_SHIFT 3 |
| 44 | #define LTC2990_MODE1_MASK GENMASK(1, 0) |
| 45 | |
| 46 | /* Enabled measurements for mode bits 2..0 */ |
| 47 | static const int ltc2990_attrs_ena_0[] = { |
| 48 | LTC2990_IN1 | LTC2990_IN2 | LTC2990_TEMP3, |
| 49 | LTC2990_CURR1 | LTC2990_TEMP3, |
| 50 | LTC2990_CURR1 | LTC2990_IN3 | LTC2990_IN4, |
| 51 | LTC2990_TEMP2 | LTC2990_IN3 | LTC2990_IN4, |
| 52 | LTC2990_TEMP2 | LTC2990_CURR2, |
| 53 | LTC2990_TEMP2 | LTC2990_TEMP3, |
| 54 | LTC2990_CURR1 | LTC2990_CURR2, |
| 55 | LTC2990_IN1 | LTC2990_IN2 | LTC2990_IN3 | LTC2990_IN4 |
| 56 | }; |
| 57 | |
| 58 | /* Enabled measurements for mode bits 4..3 */ |
| 59 | static const int ltc2990_attrs_ena_1[] = { |
| 60 | LTC2990_NONE, |
| 61 | LTC2990_TEMP2 | LTC2990_IN1 | LTC2990_CURR1, |
| 62 | LTC2990_TEMP3 | LTC2990_IN3 | LTC2990_CURR2, |
| 63 | LTC2990_ALL |
| 64 | }; |
| 65 | |
| 66 | struct ltc2990_data { |
| 67 | struct i2c_client *i2c; |
| 68 | u32 mode[2]; |
| 69 | }; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 70 | |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 71 | /* Return the converted value from the given register in uV or mC */ |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 72 | 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] | 73 | { |
| 74 | int val; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 75 | u8 reg; |
| 76 | |
| 77 | switch (index) { |
| 78 | case LTC2990_IN0: |
| 79 | reg = LTC2990_VCC_MSB; |
| 80 | break; |
| 81 | case LTC2990_IN1: |
| 82 | case LTC2990_CURR1: |
| 83 | case LTC2990_TEMP2: |
| 84 | reg = LTC2990_V1_MSB; |
| 85 | break; |
| 86 | case LTC2990_IN2: |
| 87 | reg = LTC2990_V2_MSB; |
| 88 | break; |
| 89 | case LTC2990_IN3: |
| 90 | case LTC2990_CURR2: |
| 91 | case LTC2990_TEMP3: |
| 92 | reg = LTC2990_V3_MSB; |
| 93 | break; |
| 94 | case LTC2990_IN4: |
| 95 | reg = LTC2990_V4_MSB; |
| 96 | break; |
| 97 | case LTC2990_TEMP1: |
| 98 | reg = LTC2990_TINT_MSB; |
| 99 | break; |
| 100 | default: |
| 101 | return -EINVAL; |
| 102 | } |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 103 | |
| 104 | val = i2c_smbus_read_word_swapped(i2c, reg); |
| 105 | if (unlikely(val < 0)) |
| 106 | return val; |
| 107 | |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 108 | switch (index) { |
| 109 | case LTC2990_TEMP1: |
| 110 | case LTC2990_TEMP2: |
| 111 | case LTC2990_TEMP3: |
| 112 | /* temp, 0.0625 degrees/LSB */ |
Tom Levens | a6282d1 | 2017-07-03 06:28:58 +0200 | [diff] [blame] | 113 | *result = sign_extend32(val, 12) * 1000 / 16; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 114 | break; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 115 | case LTC2990_CURR1: |
| 116 | case LTC2990_CURR2: |
| 117 | /* Vx-Vy, 19.42uV/LSB */ |
Tom Levens | a6282d1 | 2017-07-03 06:28:58 +0200 | [diff] [blame] | 118 | *result = sign_extend32(val, 14) * 1942 / 100; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 119 | break; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 120 | case LTC2990_IN0: |
| 121 | /* Vcc, 305.18uV/LSB, 2.5V offset */ |
Tom Levens | a6282d1 | 2017-07-03 06:28:58 +0200 | [diff] [blame] | 122 | *result = sign_extend32(val, 14) * 30518 / (100 * 1000) + 2500; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 123 | break; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 124 | case LTC2990_IN1: |
| 125 | case LTC2990_IN2: |
| 126 | case LTC2990_IN3: |
| 127 | case LTC2990_IN4: |
| 128 | /* Vx, 305.18uV/LSB */ |
| 129 | *result = sign_extend32(val, 14) * 30518 / (100 * 1000); |
| 130 | break; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 131 | default: |
| 132 | return -EINVAL; /* won't happen, keep compiler happy */ |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Guenter Roeck | bbc7a6b | 2018-12-10 14:02:14 -0800 | [diff] [blame] | 138 | static ssize_t ltc2990_value_show(struct device *dev, |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 139 | struct device_attribute *da, char *buf) |
| 140 | { |
| 141 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 142 | struct ltc2990_data *data = dev_get_drvdata(dev); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 143 | int value; |
| 144 | int ret; |
| 145 | |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 146 | ret = ltc2990_get_value(data->i2c, attr->index, &value); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 147 | if (unlikely(ret < 0)) |
| 148 | return ret; |
| 149 | |
| 150 | return snprintf(buf, PAGE_SIZE, "%d\n", value); |
| 151 | } |
| 152 | |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 153 | static umode_t ltc2990_attrs_visible(struct kobject *kobj, |
| 154 | struct attribute *a, int n) |
| 155 | { |
| 156 | struct device *dev = container_of(kobj, struct device, kobj); |
| 157 | struct ltc2990_data *data = dev_get_drvdata(dev); |
| 158 | struct device_attribute *da = |
| 159 | container_of(a, struct device_attribute, attr); |
| 160 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 161 | |
| 162 | int attrs_mask = LTC2990_IN0 | LTC2990_TEMP1 | |
| 163 | (ltc2990_attrs_ena_0[data->mode[0]] & |
| 164 | ltc2990_attrs_ena_1[data->mode[1]]); |
| 165 | |
| 166 | if (attr->index & attrs_mask) |
| 167 | return a->mode; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Guenter Roeck | bbc7a6b | 2018-12-10 14:02:14 -0800 | [diff] [blame] | 172 | static SENSOR_DEVICE_ATTR_RO(temp1_input, ltc2990_value, LTC2990_TEMP1); |
| 173 | static SENSOR_DEVICE_ATTR_RO(temp2_input, ltc2990_value, LTC2990_TEMP2); |
| 174 | static SENSOR_DEVICE_ATTR_RO(temp3_input, ltc2990_value, LTC2990_TEMP3); |
| 175 | static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc2990_value, LTC2990_CURR1); |
| 176 | static SENSOR_DEVICE_ATTR_RO(curr2_input, ltc2990_value, LTC2990_CURR2); |
| 177 | static SENSOR_DEVICE_ATTR_RO(in0_input, ltc2990_value, LTC2990_IN0); |
| 178 | static SENSOR_DEVICE_ATTR_RO(in1_input, ltc2990_value, LTC2990_IN1); |
| 179 | static SENSOR_DEVICE_ATTR_RO(in2_input, ltc2990_value, LTC2990_IN2); |
| 180 | static SENSOR_DEVICE_ATTR_RO(in3_input, ltc2990_value, LTC2990_IN3); |
| 181 | static SENSOR_DEVICE_ATTR_RO(in4_input, ltc2990_value, LTC2990_IN4); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 182 | |
| 183 | static struct attribute *ltc2990_attrs[] = { |
| 184 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 185 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 186 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 187 | &sensor_dev_attr_curr1_input.dev_attr.attr, |
| 188 | &sensor_dev_attr_curr2_input.dev_attr.attr, |
| 189 | &sensor_dev_attr_in0_input.dev_attr.attr, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 190 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 191 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 192 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 193 | &sensor_dev_attr_in4_input.dev_attr.attr, |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 194 | NULL, |
| 195 | }; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 196 | |
| 197 | static const struct attribute_group ltc2990_group = { |
| 198 | .attrs = ltc2990_attrs, |
| 199 | .is_visible = ltc2990_attrs_visible, |
| 200 | }; |
| 201 | __ATTRIBUTE_GROUPS(ltc2990); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 202 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 203 | static int ltc2990_i2c_probe(struct i2c_client *i2c) |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 204 | { |
| 205 | int ret; |
| 206 | struct device *hwmon_dev; |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 207 | struct ltc2990_data *data; |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 208 | |
| 209 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 210 | I2C_FUNC_SMBUS_WORD_DATA)) |
| 211 | return -ENODEV; |
| 212 | |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 213 | data = devm_kzalloc(&i2c->dev, sizeof(struct ltc2990_data), GFP_KERNEL); |
| 214 | if (unlikely(!data)) |
| 215 | return -ENOMEM; |
| 216 | |
| 217 | data->i2c = i2c; |
| 218 | |
Max Staudt | 06a1c69 | 2019-08-19 14:16:17 +0200 | [diff] [blame] | 219 | if (dev_fwnode(&i2c->dev)) { |
| 220 | ret = device_property_read_u32_array(&i2c->dev, |
| 221 | "lltc,meas-mode", |
| 222 | data->mode, 2); |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 223 | if (ret < 0) |
| 224 | return ret; |
| 225 | |
| 226 | if (data->mode[0] & ~LTC2990_MODE0_MASK || |
| 227 | data->mode[1] & ~LTC2990_MODE1_MASK) |
| 228 | return -EINVAL; |
| 229 | } else { |
| 230 | ret = i2c_smbus_read_byte_data(i2c, LTC2990_CONTROL); |
| 231 | if (ret < 0) |
| 232 | return ret; |
| 233 | |
| 234 | data->mode[0] = ret >> LTC2990_MODE0_SHIFT & LTC2990_MODE0_MASK; |
| 235 | data->mode[1] = ret >> LTC2990_MODE1_SHIFT & LTC2990_MODE1_MASK; |
| 236 | } |
| 237 | |
| 238 | /* Setup continuous mode */ |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 239 | ret = i2c_smbus_write_byte_data(i2c, LTC2990_CONTROL, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 240 | data->mode[0] << LTC2990_MODE0_SHIFT | |
| 241 | data->mode[1] << LTC2990_MODE1_SHIFT); |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 242 | if (ret < 0) { |
| 243 | dev_err(&i2c->dev, "Error: Failed to set control mode.\n"); |
| 244 | return ret; |
| 245 | } |
| 246 | /* Trigger once to start continuous conversion */ |
| 247 | ret = i2c_smbus_write_byte_data(i2c, LTC2990_TRIGGER, 1); |
| 248 | if (ret < 0) { |
| 249 | dev_err(&i2c->dev, "Error: Failed to start acquisition.\n"); |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | hwmon_dev = devm_hwmon_device_register_with_groups(&i2c->dev, |
| 254 | i2c->name, |
Tom Levens | 5d9ca43 | 2017-07-03 06:29:00 +0200 | [diff] [blame] | 255 | data, |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 256 | ltc2990_groups); |
| 257 | |
| 258 | return PTR_ERR_OR_ZERO(hwmon_dev); |
| 259 | } |
| 260 | |
| 261 | static const struct i2c_device_id ltc2990_i2c_id[] = { |
| 262 | { "ltc2990", 0 }, |
| 263 | {} |
| 264 | }; |
| 265 | MODULE_DEVICE_TABLE(i2c, ltc2990_i2c_id); |
| 266 | |
| 267 | static struct i2c_driver ltc2990_i2c_driver = { |
| 268 | .driver = { |
| 269 | .name = "ltc2990", |
| 270 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 271 | .probe_new = ltc2990_i2c_probe, |
Mike Looijmans | df92270 | 2016-01-15 10:54:59 +0100 | [diff] [blame] | 272 | .id_table = ltc2990_i2c_id, |
| 273 | }; |
| 274 | |
| 275 | module_i2c_driver(ltc2990_i2c_driver); |
| 276 | |
| 277 | MODULE_DESCRIPTION("LTC2990 Sensor Driver"); |
| 278 | MODULE_AUTHOR("Topic Embedded Products"); |
| 279 | MODULE_LICENSE("GPL v2"); |