13e5580c4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2df922703SMike Looijmans /* 3df922703SMike Looijmans * Driver for Linear Technology LTC2990 power monitor 4df922703SMike Looijmans * 5df922703SMike Looijmans * Copyright (C) 2014 Topic Embedded Products 6df922703SMike Looijmans * Author: Mike Looijmans <mike.looijmans@topic.nl> 7df922703SMike Looijmans */ 8df922703SMike Looijmans 9a6282d17STom Levens #include <linux/bitops.h> 10df922703SMike Looijmans #include <linux/err.h> 11df922703SMike Looijmans #include <linux/hwmon.h> 12df922703SMike Looijmans #include <linux/hwmon-sysfs.h> 13df922703SMike Looijmans #include <linux/i2c.h> 14df922703SMike Looijmans #include <linux/kernel.h> 15df922703SMike Looijmans #include <linux/module.h> 16*06a1c69cSMax Staudt #include <linux/property.h> 17df922703SMike Looijmans 18df922703SMike Looijmans #define LTC2990_STATUS 0x00 19df922703SMike Looijmans #define LTC2990_CONTROL 0x01 20df922703SMike Looijmans #define LTC2990_TRIGGER 0x02 21df922703SMike Looijmans #define LTC2990_TINT_MSB 0x04 22df922703SMike Looijmans #define LTC2990_V1_MSB 0x06 23df922703SMike Looijmans #define LTC2990_V2_MSB 0x08 24df922703SMike Looijmans #define LTC2990_V3_MSB 0x0A 25df922703SMike Looijmans #define LTC2990_V4_MSB 0x0C 26df922703SMike Looijmans #define LTC2990_VCC_MSB 0x0E 27df922703SMike Looijmans 285d9ca430STom Levens #define LTC2990_IN0 BIT(0) 295d9ca430STom Levens #define LTC2990_IN1 BIT(1) 305d9ca430STom Levens #define LTC2990_IN2 BIT(2) 315d9ca430STom Levens #define LTC2990_IN3 BIT(3) 325d9ca430STom Levens #define LTC2990_IN4 BIT(4) 335d9ca430STom Levens #define LTC2990_CURR1 BIT(5) 345d9ca430STom Levens #define LTC2990_CURR2 BIT(6) 355d9ca430STom Levens #define LTC2990_TEMP1 BIT(7) 365d9ca430STom Levens #define LTC2990_TEMP2 BIT(8) 375d9ca430STom Levens #define LTC2990_TEMP3 BIT(9) 385d9ca430STom Levens #define LTC2990_NONE 0 395d9ca430STom Levens #define LTC2990_ALL GENMASK(9, 0) 405d9ca430STom Levens 415d9ca430STom Levens #define LTC2990_MODE0_SHIFT 0 425d9ca430STom Levens #define LTC2990_MODE0_MASK GENMASK(2, 0) 435d9ca430STom Levens #define LTC2990_MODE1_SHIFT 3 445d9ca430STom Levens #define LTC2990_MODE1_MASK GENMASK(1, 0) 455d9ca430STom Levens 465d9ca430STom Levens /* Enabled measurements for mode bits 2..0 */ 475d9ca430STom Levens static const int ltc2990_attrs_ena_0[] = { 485d9ca430STom Levens LTC2990_IN1 | LTC2990_IN2 | LTC2990_TEMP3, 495d9ca430STom Levens LTC2990_CURR1 | LTC2990_TEMP3, 505d9ca430STom Levens LTC2990_CURR1 | LTC2990_IN3 | LTC2990_IN4, 515d9ca430STom Levens LTC2990_TEMP2 | LTC2990_IN3 | LTC2990_IN4, 525d9ca430STom Levens LTC2990_TEMP2 | LTC2990_CURR2, 535d9ca430STom Levens LTC2990_TEMP2 | LTC2990_TEMP3, 545d9ca430STom Levens LTC2990_CURR1 | LTC2990_CURR2, 555d9ca430STom Levens LTC2990_IN1 | LTC2990_IN2 | LTC2990_IN3 | LTC2990_IN4 565d9ca430STom Levens }; 575d9ca430STom Levens 585d9ca430STom Levens /* Enabled measurements for mode bits 4..3 */ 595d9ca430STom Levens static const int ltc2990_attrs_ena_1[] = { 605d9ca430STom Levens LTC2990_NONE, 615d9ca430STom Levens LTC2990_TEMP2 | LTC2990_IN1 | LTC2990_CURR1, 625d9ca430STom Levens LTC2990_TEMP3 | LTC2990_IN3 | LTC2990_CURR2, 635d9ca430STom Levens LTC2990_ALL 645d9ca430STom Levens }; 655d9ca430STom Levens 665d9ca430STom Levens struct ltc2990_data { 675d9ca430STom Levens struct i2c_client *i2c; 685d9ca430STom Levens u32 mode[2]; 695d9ca430STom Levens }; 70df922703SMike Looijmans 71df922703SMike Looijmans /* Return the converted value from the given register in uV or mC */ 725d9ca430STom Levens static int ltc2990_get_value(struct i2c_client *i2c, int index, int *result) 73df922703SMike Looijmans { 74df922703SMike Looijmans int val; 755d9ca430STom Levens u8 reg; 765d9ca430STom Levens 775d9ca430STom Levens switch (index) { 785d9ca430STom Levens case LTC2990_IN0: 795d9ca430STom Levens reg = LTC2990_VCC_MSB; 805d9ca430STom Levens break; 815d9ca430STom Levens case LTC2990_IN1: 825d9ca430STom Levens case LTC2990_CURR1: 835d9ca430STom Levens case LTC2990_TEMP2: 845d9ca430STom Levens reg = LTC2990_V1_MSB; 855d9ca430STom Levens break; 865d9ca430STom Levens case LTC2990_IN2: 875d9ca430STom Levens reg = LTC2990_V2_MSB; 885d9ca430STom Levens break; 895d9ca430STom Levens case LTC2990_IN3: 905d9ca430STom Levens case LTC2990_CURR2: 915d9ca430STom Levens case LTC2990_TEMP3: 925d9ca430STom Levens reg = LTC2990_V3_MSB; 935d9ca430STom Levens break; 945d9ca430STom Levens case LTC2990_IN4: 955d9ca430STom Levens reg = LTC2990_V4_MSB; 965d9ca430STom Levens break; 975d9ca430STom Levens case LTC2990_TEMP1: 985d9ca430STom Levens reg = LTC2990_TINT_MSB; 995d9ca430STom Levens break; 1005d9ca430STom Levens default: 1015d9ca430STom Levens return -EINVAL; 1025d9ca430STom Levens } 103df922703SMike Looijmans 104df922703SMike Looijmans val = i2c_smbus_read_word_swapped(i2c, reg); 105df922703SMike Looijmans if (unlikely(val < 0)) 106df922703SMike Looijmans return val; 107df922703SMike Looijmans 1085d9ca430STom Levens switch (index) { 1095d9ca430STom Levens case LTC2990_TEMP1: 1105d9ca430STom Levens case LTC2990_TEMP2: 1115d9ca430STom Levens case LTC2990_TEMP3: 1125d9ca430STom Levens /* temp, 0.0625 degrees/LSB */ 113a6282d17STom Levens *result = sign_extend32(val, 12) * 1000 / 16; 114df922703SMike Looijmans break; 1155d9ca430STom Levens case LTC2990_CURR1: 1165d9ca430STom Levens case LTC2990_CURR2: 1175d9ca430STom Levens /* Vx-Vy, 19.42uV/LSB */ 118a6282d17STom Levens *result = sign_extend32(val, 14) * 1942 / 100; 119df922703SMike Looijmans break; 1205d9ca430STom Levens case LTC2990_IN0: 1215d9ca430STom Levens /* Vcc, 305.18uV/LSB, 2.5V offset */ 122a6282d17STom Levens *result = sign_extend32(val, 14) * 30518 / (100 * 1000) + 2500; 123df922703SMike Looijmans break; 1245d9ca430STom Levens case LTC2990_IN1: 1255d9ca430STom Levens case LTC2990_IN2: 1265d9ca430STom Levens case LTC2990_IN3: 1275d9ca430STom Levens case LTC2990_IN4: 1285d9ca430STom Levens /* Vx, 305.18uV/LSB */ 1295d9ca430STom Levens *result = sign_extend32(val, 14) * 30518 / (100 * 1000); 1305d9ca430STom Levens break; 131df922703SMike Looijmans default: 132df922703SMike Looijmans return -EINVAL; /* won't happen, keep compiler happy */ 133df922703SMike Looijmans } 134df922703SMike Looijmans 135df922703SMike Looijmans return 0; 136df922703SMike Looijmans } 137df922703SMike Looijmans 138bbc7a6b5SGuenter Roeck static ssize_t ltc2990_value_show(struct device *dev, 139df922703SMike Looijmans struct device_attribute *da, char *buf) 140df922703SMike Looijmans { 141df922703SMike Looijmans struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 1425d9ca430STom Levens struct ltc2990_data *data = dev_get_drvdata(dev); 143df922703SMike Looijmans int value; 144df922703SMike Looijmans int ret; 145df922703SMike Looijmans 1465d9ca430STom Levens ret = ltc2990_get_value(data->i2c, attr->index, &value); 147df922703SMike Looijmans if (unlikely(ret < 0)) 148df922703SMike Looijmans return ret; 149df922703SMike Looijmans 150df922703SMike Looijmans return snprintf(buf, PAGE_SIZE, "%d\n", value); 151df922703SMike Looijmans } 152df922703SMike Looijmans 1535d9ca430STom Levens static umode_t ltc2990_attrs_visible(struct kobject *kobj, 1545d9ca430STom Levens struct attribute *a, int n) 1555d9ca430STom Levens { 1565d9ca430STom Levens struct device *dev = container_of(kobj, struct device, kobj); 1575d9ca430STom Levens struct ltc2990_data *data = dev_get_drvdata(dev); 1585d9ca430STom Levens struct device_attribute *da = 1595d9ca430STom Levens container_of(a, struct device_attribute, attr); 1605d9ca430STom Levens struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 1615d9ca430STom Levens 1625d9ca430STom Levens int attrs_mask = LTC2990_IN0 | LTC2990_TEMP1 | 1635d9ca430STom Levens (ltc2990_attrs_ena_0[data->mode[0]] & 1645d9ca430STom Levens ltc2990_attrs_ena_1[data->mode[1]]); 1655d9ca430STom Levens 1665d9ca430STom Levens if (attr->index & attrs_mask) 1675d9ca430STom Levens return a->mode; 1685d9ca430STom Levens 1695d9ca430STom Levens return 0; 1705d9ca430STom Levens } 1715d9ca430STom Levens 172bbc7a6b5SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, ltc2990_value, LTC2990_TEMP1); 173bbc7a6b5SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_input, ltc2990_value, LTC2990_TEMP2); 174bbc7a6b5SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_input, ltc2990_value, LTC2990_TEMP3); 175bbc7a6b5SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc2990_value, LTC2990_CURR1); 176bbc7a6b5SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(curr2_input, ltc2990_value, LTC2990_CURR2); 177bbc7a6b5SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in0_input, ltc2990_value, LTC2990_IN0); 178bbc7a6b5SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_input, ltc2990_value, LTC2990_IN1); 179bbc7a6b5SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_input, ltc2990_value, LTC2990_IN2); 180bbc7a6b5SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in3_input, ltc2990_value, LTC2990_IN3); 181bbc7a6b5SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in4_input, ltc2990_value, LTC2990_IN4); 182df922703SMike Looijmans 183df922703SMike Looijmans static struct attribute *ltc2990_attrs[] = { 184df922703SMike Looijmans &sensor_dev_attr_temp1_input.dev_attr.attr, 1855d9ca430STom Levens &sensor_dev_attr_temp2_input.dev_attr.attr, 1865d9ca430STom Levens &sensor_dev_attr_temp3_input.dev_attr.attr, 187df922703SMike Looijmans &sensor_dev_attr_curr1_input.dev_attr.attr, 188df922703SMike Looijmans &sensor_dev_attr_curr2_input.dev_attr.attr, 189df922703SMike Looijmans &sensor_dev_attr_in0_input.dev_attr.attr, 1905d9ca430STom Levens &sensor_dev_attr_in1_input.dev_attr.attr, 1915d9ca430STom Levens &sensor_dev_attr_in2_input.dev_attr.attr, 1925d9ca430STom Levens &sensor_dev_attr_in3_input.dev_attr.attr, 1935d9ca430STom Levens &sensor_dev_attr_in4_input.dev_attr.attr, 194df922703SMike Looijmans NULL, 195df922703SMike Looijmans }; 1965d9ca430STom Levens 1975d9ca430STom Levens static const struct attribute_group ltc2990_group = { 1985d9ca430STom Levens .attrs = ltc2990_attrs, 1995d9ca430STom Levens .is_visible = ltc2990_attrs_visible, 2005d9ca430STom Levens }; 2015d9ca430STom Levens __ATTRIBUTE_GROUPS(ltc2990); 202df922703SMike Looijmans 203df922703SMike Looijmans static int ltc2990_i2c_probe(struct i2c_client *i2c, 204df922703SMike Looijmans const struct i2c_device_id *id) 205df922703SMike Looijmans { 206df922703SMike Looijmans int ret; 207df922703SMike Looijmans struct device *hwmon_dev; 2085d9ca430STom Levens struct ltc2990_data *data; 209df922703SMike Looijmans 210df922703SMike Looijmans if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA | 211df922703SMike Looijmans I2C_FUNC_SMBUS_WORD_DATA)) 212df922703SMike Looijmans return -ENODEV; 213df922703SMike Looijmans 2145d9ca430STom Levens data = devm_kzalloc(&i2c->dev, sizeof(struct ltc2990_data), GFP_KERNEL); 2155d9ca430STom Levens if (unlikely(!data)) 2165d9ca430STom Levens return -ENOMEM; 2175d9ca430STom Levens 2185d9ca430STom Levens data->i2c = i2c; 2195d9ca430STom Levens 220*06a1c69cSMax Staudt if (dev_fwnode(&i2c->dev)) { 221*06a1c69cSMax Staudt ret = device_property_read_u32_array(&i2c->dev, 222*06a1c69cSMax Staudt "lltc,meas-mode", 2235d9ca430STom Levens data->mode, 2); 2245d9ca430STom Levens if (ret < 0) 2255d9ca430STom Levens return ret; 2265d9ca430STom Levens 2275d9ca430STom Levens if (data->mode[0] & ~LTC2990_MODE0_MASK || 2285d9ca430STom Levens data->mode[1] & ~LTC2990_MODE1_MASK) 2295d9ca430STom Levens return -EINVAL; 2305d9ca430STom Levens } else { 2315d9ca430STom Levens ret = i2c_smbus_read_byte_data(i2c, LTC2990_CONTROL); 2325d9ca430STom Levens if (ret < 0) 2335d9ca430STom Levens return ret; 2345d9ca430STom Levens 2355d9ca430STom Levens data->mode[0] = ret >> LTC2990_MODE0_SHIFT & LTC2990_MODE0_MASK; 2365d9ca430STom Levens data->mode[1] = ret >> LTC2990_MODE1_SHIFT & LTC2990_MODE1_MASK; 2375d9ca430STom Levens } 2385d9ca430STom Levens 2395d9ca430STom Levens /* Setup continuous mode */ 240df922703SMike Looijmans ret = i2c_smbus_write_byte_data(i2c, LTC2990_CONTROL, 2415d9ca430STom Levens data->mode[0] << LTC2990_MODE0_SHIFT | 2425d9ca430STom Levens data->mode[1] << LTC2990_MODE1_SHIFT); 243df922703SMike Looijmans if (ret < 0) { 244df922703SMike Looijmans dev_err(&i2c->dev, "Error: Failed to set control mode.\n"); 245df922703SMike Looijmans return ret; 246df922703SMike Looijmans } 247df922703SMike Looijmans /* Trigger once to start continuous conversion */ 248df922703SMike Looijmans ret = i2c_smbus_write_byte_data(i2c, LTC2990_TRIGGER, 1); 249df922703SMike Looijmans if (ret < 0) { 250df922703SMike Looijmans dev_err(&i2c->dev, "Error: Failed to start acquisition.\n"); 251df922703SMike Looijmans return ret; 252df922703SMike Looijmans } 253df922703SMike Looijmans 254df922703SMike Looijmans hwmon_dev = devm_hwmon_device_register_with_groups(&i2c->dev, 255df922703SMike Looijmans i2c->name, 2565d9ca430STom Levens data, 257df922703SMike Looijmans ltc2990_groups); 258df922703SMike Looijmans 259df922703SMike Looijmans return PTR_ERR_OR_ZERO(hwmon_dev); 260df922703SMike Looijmans } 261df922703SMike Looijmans 262df922703SMike Looijmans static const struct i2c_device_id ltc2990_i2c_id[] = { 263df922703SMike Looijmans { "ltc2990", 0 }, 264df922703SMike Looijmans {} 265df922703SMike Looijmans }; 266df922703SMike Looijmans MODULE_DEVICE_TABLE(i2c, ltc2990_i2c_id); 267df922703SMike Looijmans 268df922703SMike Looijmans static struct i2c_driver ltc2990_i2c_driver = { 269df922703SMike Looijmans .driver = { 270df922703SMike Looijmans .name = "ltc2990", 271df922703SMike Looijmans }, 272df922703SMike Looijmans .probe = ltc2990_i2c_probe, 273df922703SMike Looijmans .id_table = ltc2990_i2c_id, 274df922703SMike Looijmans }; 275df922703SMike Looijmans 276df922703SMike Looijmans module_i2c_driver(ltc2990_i2c_driver); 277df922703SMike Looijmans 278df922703SMike Looijmans MODULE_DESCRIPTION("LTC2990 Sensor Driver"); 279df922703SMike Looijmans MODULE_AUTHOR("Topic Embedded Products"); 280df922703SMike Looijmans MODULE_LICENSE("GPL v2"); 281