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