1*8efd1014STao Ren // SPDX-License-Identifier: GPL-2.0+ 2*8efd1014STao Ren /* 3*8efd1014STao Ren * Hardware monitoring driver for MAX127. 4*8efd1014STao Ren * 5*8efd1014STao Ren * Copyright (c) 2020 Facebook Inc. 6*8efd1014STao Ren */ 7*8efd1014STao Ren 8*8efd1014STao Ren #include <linux/err.h> 9*8efd1014STao Ren #include <linux/hwmon.h> 10*8efd1014STao Ren #include <linux/i2c.h> 11*8efd1014STao Ren #include <linux/init.h> 12*8efd1014STao Ren #include <linux/module.h> 13*8efd1014STao Ren 14*8efd1014STao Ren /* 15*8efd1014STao Ren * MAX127 Control Byte. Refer to MAX127 datasheet, Table 1 "Control-Byte 16*8efd1014STao Ren * Format" for details. 17*8efd1014STao Ren */ 18*8efd1014STao Ren #define MAX127_CTRL_START BIT(7) 19*8efd1014STao Ren #define MAX127_CTRL_SEL_SHIFT 4 20*8efd1014STao Ren #define MAX127_CTRL_RNG BIT(3) 21*8efd1014STao Ren #define MAX127_CTRL_BIP BIT(2) 22*8efd1014STao Ren #define MAX127_CTRL_PD1 BIT(1) 23*8efd1014STao Ren #define MAX127_CTRL_PD0 BIT(0) 24*8efd1014STao Ren 25*8efd1014STao Ren #define MAX127_NUM_CHANNELS 8 26*8efd1014STao Ren #define MAX127_SET_CHANNEL(ch) (((ch) & 7) << MAX127_CTRL_SEL_SHIFT) 27*8efd1014STao Ren 28*8efd1014STao Ren /* 29*8efd1014STao Ren * MAX127 channel input ranges. Refer to MAX127 datasheet, Table 3 "Range 30*8efd1014STao Ren * and Polarity Selection" for details. 31*8efd1014STao Ren */ 32*8efd1014STao Ren #define MAX127_FULL_RANGE 10000 /* 10V */ 33*8efd1014STao Ren #define MAX127_HALF_RANGE 5000 /* 5V */ 34*8efd1014STao Ren 35*8efd1014STao Ren /* 36*8efd1014STao Ren * MAX127 returns 2 bytes at read: 37*8efd1014STao Ren * - the first byte contains data[11:4]. 38*8efd1014STao Ren * - the second byte contains data[3:0] (MSB) and 4 dummy 0s (LSB). 39*8efd1014STao Ren * Refer to MAX127 datasheet, "Read a Conversion (Read Cycle)" section 40*8efd1014STao Ren * for details. 41*8efd1014STao Ren */ 42*8efd1014STao Ren #define MAX127_DATA_LEN 2 43*8efd1014STao Ren #define MAX127_DATA_SHIFT 4 44*8efd1014STao Ren 45*8efd1014STao Ren #define MAX127_SIGN_BIT BIT(11) 46*8efd1014STao Ren 47*8efd1014STao Ren struct max127_data { 48*8efd1014STao Ren struct mutex lock; 49*8efd1014STao Ren struct i2c_client *client; 50*8efd1014STao Ren u8 ctrl_byte[MAX127_NUM_CHANNELS]; 51*8efd1014STao Ren }; 52*8efd1014STao Ren 53*8efd1014STao Ren static int max127_select_channel(struct i2c_client *client, u8 ctrl_byte) 54*8efd1014STao Ren { 55*8efd1014STao Ren int status; 56*8efd1014STao Ren struct i2c_msg msg = { 57*8efd1014STao Ren .addr = client->addr, 58*8efd1014STao Ren .flags = 0, 59*8efd1014STao Ren .len = sizeof(ctrl_byte), 60*8efd1014STao Ren .buf = &ctrl_byte, 61*8efd1014STao Ren }; 62*8efd1014STao Ren 63*8efd1014STao Ren status = i2c_transfer(client->adapter, &msg, 1); 64*8efd1014STao Ren if (status < 0) 65*8efd1014STao Ren return status; 66*8efd1014STao Ren if (status != 1) 67*8efd1014STao Ren return -EIO; 68*8efd1014STao Ren 69*8efd1014STao Ren return 0; 70*8efd1014STao Ren } 71*8efd1014STao Ren 72*8efd1014STao Ren static int max127_read_channel(struct i2c_client *client, long *val) 73*8efd1014STao Ren { 74*8efd1014STao Ren int status; 75*8efd1014STao Ren u8 i2c_data[MAX127_DATA_LEN]; 76*8efd1014STao Ren struct i2c_msg msg = { 77*8efd1014STao Ren .addr = client->addr, 78*8efd1014STao Ren .flags = I2C_M_RD, 79*8efd1014STao Ren .len = sizeof(i2c_data), 80*8efd1014STao Ren .buf = i2c_data, 81*8efd1014STao Ren }; 82*8efd1014STao Ren 83*8efd1014STao Ren status = i2c_transfer(client->adapter, &msg, 1); 84*8efd1014STao Ren if (status < 0) 85*8efd1014STao Ren return status; 86*8efd1014STao Ren if (status != 1) 87*8efd1014STao Ren return -EIO; 88*8efd1014STao Ren 89*8efd1014STao Ren *val = (i2c_data[1] >> MAX127_DATA_SHIFT) | 90*8efd1014STao Ren ((u16)i2c_data[0] << MAX127_DATA_SHIFT); 91*8efd1014STao Ren return 0; 92*8efd1014STao Ren } 93*8efd1014STao Ren 94*8efd1014STao Ren static long max127_process_raw(u8 ctrl_byte, long raw) 95*8efd1014STao Ren { 96*8efd1014STao Ren long scale, weight; 97*8efd1014STao Ren 98*8efd1014STao Ren /* 99*8efd1014STao Ren * MAX127's data coding is binary in unipolar mode with 1 LSB = 100*8efd1014STao Ren * (Full-Scale/4096) and two’s complement binary in bipolar mode 101*8efd1014STao Ren * with 1 LSB = [(2 x |FS|)/4096]. 102*8efd1014STao Ren * Refer to MAX127 datasheet, "Transfer Function" section for 103*8efd1014STao Ren * details. 104*8efd1014STao Ren */ 105*8efd1014STao Ren scale = (ctrl_byte & MAX127_CTRL_RNG) ? MAX127_FULL_RANGE : 106*8efd1014STao Ren MAX127_HALF_RANGE; 107*8efd1014STao Ren if (ctrl_byte & MAX127_CTRL_BIP) { 108*8efd1014STao Ren weight = (raw & MAX127_SIGN_BIT); 109*8efd1014STao Ren raw &= ~MAX127_SIGN_BIT; 110*8efd1014STao Ren raw -= weight; 111*8efd1014STao Ren raw *= 2; 112*8efd1014STao Ren } 113*8efd1014STao Ren 114*8efd1014STao Ren return raw * scale / 4096; 115*8efd1014STao Ren } 116*8efd1014STao Ren 117*8efd1014STao Ren static int max127_read_input(struct max127_data *data, int channel, long *val) 118*8efd1014STao Ren { 119*8efd1014STao Ren long raw; 120*8efd1014STao Ren int status; 121*8efd1014STao Ren struct i2c_client *client = data->client; 122*8efd1014STao Ren u8 ctrl_byte = data->ctrl_byte[channel]; 123*8efd1014STao Ren 124*8efd1014STao Ren mutex_lock(&data->lock); 125*8efd1014STao Ren 126*8efd1014STao Ren status = max127_select_channel(client, ctrl_byte); 127*8efd1014STao Ren if (status) 128*8efd1014STao Ren goto exit; 129*8efd1014STao Ren 130*8efd1014STao Ren status = max127_read_channel(client, &raw); 131*8efd1014STao Ren if (status) 132*8efd1014STao Ren goto exit; 133*8efd1014STao Ren 134*8efd1014STao Ren *val = max127_process_raw(ctrl_byte, raw); 135*8efd1014STao Ren 136*8efd1014STao Ren exit: 137*8efd1014STao Ren mutex_unlock(&data->lock); 138*8efd1014STao Ren return status; 139*8efd1014STao Ren } 140*8efd1014STao Ren 141*8efd1014STao Ren static int max127_read_min(struct max127_data *data, int channel, long *val) 142*8efd1014STao Ren { 143*8efd1014STao Ren u8 rng_bip = (data->ctrl_byte[channel] >> 2) & 3; 144*8efd1014STao Ren static const int min_input_map[4] = { 145*8efd1014STao Ren 0, /* RNG=0, BIP=0 */ 146*8efd1014STao Ren -MAX127_HALF_RANGE, /* RNG=0, BIP=1 */ 147*8efd1014STao Ren 0, /* RNG=1, BIP=0 */ 148*8efd1014STao Ren -MAX127_FULL_RANGE, /* RNG=1, BIP=1 */ 149*8efd1014STao Ren }; 150*8efd1014STao Ren 151*8efd1014STao Ren *val = min_input_map[rng_bip]; 152*8efd1014STao Ren return 0; 153*8efd1014STao Ren } 154*8efd1014STao Ren 155*8efd1014STao Ren static int max127_read_max(struct max127_data *data, int channel, long *val) 156*8efd1014STao Ren { 157*8efd1014STao Ren u8 rng_bip = (data->ctrl_byte[channel] >> 2) & 3; 158*8efd1014STao Ren static const int max_input_map[4] = { 159*8efd1014STao Ren MAX127_HALF_RANGE, /* RNG=0, BIP=0 */ 160*8efd1014STao Ren MAX127_HALF_RANGE, /* RNG=0, BIP=1 */ 161*8efd1014STao Ren MAX127_FULL_RANGE, /* RNG=1, BIP=0 */ 162*8efd1014STao Ren MAX127_FULL_RANGE, /* RNG=1, BIP=1 */ 163*8efd1014STao Ren }; 164*8efd1014STao Ren 165*8efd1014STao Ren *val = max_input_map[rng_bip]; 166*8efd1014STao Ren return 0; 167*8efd1014STao Ren } 168*8efd1014STao Ren 169*8efd1014STao Ren static int max127_write_min(struct max127_data *data, int channel, long val) 170*8efd1014STao Ren { 171*8efd1014STao Ren u8 ctrl; 172*8efd1014STao Ren 173*8efd1014STao Ren mutex_lock(&data->lock); 174*8efd1014STao Ren 175*8efd1014STao Ren ctrl = data->ctrl_byte[channel]; 176*8efd1014STao Ren if (val <= -MAX127_FULL_RANGE) { 177*8efd1014STao Ren ctrl |= (MAX127_CTRL_RNG | MAX127_CTRL_BIP); 178*8efd1014STao Ren } else if (val < 0) { 179*8efd1014STao Ren ctrl |= MAX127_CTRL_BIP; 180*8efd1014STao Ren ctrl &= ~MAX127_CTRL_RNG; 181*8efd1014STao Ren } else { 182*8efd1014STao Ren ctrl &= ~MAX127_CTRL_BIP; 183*8efd1014STao Ren } 184*8efd1014STao Ren data->ctrl_byte[channel] = ctrl; 185*8efd1014STao Ren 186*8efd1014STao Ren mutex_unlock(&data->lock); 187*8efd1014STao Ren 188*8efd1014STao Ren return 0; 189*8efd1014STao Ren } 190*8efd1014STao Ren 191*8efd1014STao Ren static int max127_write_max(struct max127_data *data, int channel, long val) 192*8efd1014STao Ren { 193*8efd1014STao Ren mutex_lock(&data->lock); 194*8efd1014STao Ren 195*8efd1014STao Ren if (val >= MAX127_FULL_RANGE) 196*8efd1014STao Ren data->ctrl_byte[channel] |= MAX127_CTRL_RNG; 197*8efd1014STao Ren else 198*8efd1014STao Ren data->ctrl_byte[channel] &= ~MAX127_CTRL_RNG; 199*8efd1014STao Ren 200*8efd1014STao Ren mutex_unlock(&data->lock); 201*8efd1014STao Ren 202*8efd1014STao Ren return 0; 203*8efd1014STao Ren } 204*8efd1014STao Ren 205*8efd1014STao Ren static umode_t max127_is_visible(const void *_data, 206*8efd1014STao Ren enum hwmon_sensor_types type, 207*8efd1014STao Ren u32 attr, int channel) 208*8efd1014STao Ren { 209*8efd1014STao Ren if (type == hwmon_in) { 210*8efd1014STao Ren switch (attr) { 211*8efd1014STao Ren case hwmon_in_input: 212*8efd1014STao Ren return 0444; 213*8efd1014STao Ren 214*8efd1014STao Ren case hwmon_in_min: 215*8efd1014STao Ren case hwmon_in_max: 216*8efd1014STao Ren return 0644; 217*8efd1014STao Ren 218*8efd1014STao Ren default: 219*8efd1014STao Ren break; 220*8efd1014STao Ren } 221*8efd1014STao Ren } 222*8efd1014STao Ren 223*8efd1014STao Ren return 0; 224*8efd1014STao Ren } 225*8efd1014STao Ren 226*8efd1014STao Ren static int max127_read(struct device *dev, enum hwmon_sensor_types type, 227*8efd1014STao Ren u32 attr, int channel, long *val) 228*8efd1014STao Ren { 229*8efd1014STao Ren int status; 230*8efd1014STao Ren struct max127_data *data = dev_get_drvdata(dev); 231*8efd1014STao Ren 232*8efd1014STao Ren if (type != hwmon_in) 233*8efd1014STao Ren return -EOPNOTSUPP; 234*8efd1014STao Ren 235*8efd1014STao Ren switch (attr) { 236*8efd1014STao Ren case hwmon_in_input: 237*8efd1014STao Ren status = max127_read_input(data, channel, val); 238*8efd1014STao Ren break; 239*8efd1014STao Ren 240*8efd1014STao Ren case hwmon_in_min: 241*8efd1014STao Ren status = max127_read_min(data, channel, val); 242*8efd1014STao Ren break; 243*8efd1014STao Ren 244*8efd1014STao Ren case hwmon_in_max: 245*8efd1014STao Ren status = max127_read_max(data, channel, val); 246*8efd1014STao Ren break; 247*8efd1014STao Ren 248*8efd1014STao Ren default: 249*8efd1014STao Ren status = -EOPNOTSUPP; 250*8efd1014STao Ren break; 251*8efd1014STao Ren } 252*8efd1014STao Ren 253*8efd1014STao Ren return status; 254*8efd1014STao Ren } 255*8efd1014STao Ren 256*8efd1014STao Ren static int max127_write(struct device *dev, enum hwmon_sensor_types type, 257*8efd1014STao Ren u32 attr, int channel, long val) 258*8efd1014STao Ren { 259*8efd1014STao Ren int status; 260*8efd1014STao Ren struct max127_data *data = dev_get_drvdata(dev); 261*8efd1014STao Ren 262*8efd1014STao Ren if (type != hwmon_in) 263*8efd1014STao Ren return -EOPNOTSUPP; 264*8efd1014STao Ren 265*8efd1014STao Ren switch (attr) { 266*8efd1014STao Ren case hwmon_in_min: 267*8efd1014STao Ren status = max127_write_min(data, channel, val); 268*8efd1014STao Ren break; 269*8efd1014STao Ren 270*8efd1014STao Ren case hwmon_in_max: 271*8efd1014STao Ren status = max127_write_max(data, channel, val); 272*8efd1014STao Ren break; 273*8efd1014STao Ren 274*8efd1014STao Ren default: 275*8efd1014STao Ren status = -EOPNOTSUPP; 276*8efd1014STao Ren break; 277*8efd1014STao Ren } 278*8efd1014STao Ren 279*8efd1014STao Ren return status; 280*8efd1014STao Ren } 281*8efd1014STao Ren 282*8efd1014STao Ren static const struct hwmon_ops max127_hwmon_ops = { 283*8efd1014STao Ren .is_visible = max127_is_visible, 284*8efd1014STao Ren .read = max127_read, 285*8efd1014STao Ren .write = max127_write, 286*8efd1014STao Ren }; 287*8efd1014STao Ren 288*8efd1014STao Ren static const struct hwmon_channel_info *max127_info[] = { 289*8efd1014STao Ren HWMON_CHANNEL_INFO(in, 290*8efd1014STao Ren HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX, 291*8efd1014STao Ren HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX, 292*8efd1014STao Ren HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX, 293*8efd1014STao Ren HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX, 294*8efd1014STao Ren HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX, 295*8efd1014STao Ren HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX, 296*8efd1014STao Ren HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX, 297*8efd1014STao Ren HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX), 298*8efd1014STao Ren NULL, 299*8efd1014STao Ren }; 300*8efd1014STao Ren 301*8efd1014STao Ren static const struct hwmon_chip_info max127_chip_info = { 302*8efd1014STao Ren .ops = &max127_hwmon_ops, 303*8efd1014STao Ren .info = max127_info, 304*8efd1014STao Ren }; 305*8efd1014STao Ren 306*8efd1014STao Ren static int max127_probe(struct i2c_client *client, 307*8efd1014STao Ren const struct i2c_device_id *id) 308*8efd1014STao Ren { 309*8efd1014STao Ren int i; 310*8efd1014STao Ren struct device *hwmon_dev; 311*8efd1014STao Ren struct max127_data *data; 312*8efd1014STao Ren struct device *dev = &client->dev; 313*8efd1014STao Ren 314*8efd1014STao Ren data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 315*8efd1014STao Ren if (!data) 316*8efd1014STao Ren return -ENOMEM; 317*8efd1014STao Ren 318*8efd1014STao Ren data->client = client; 319*8efd1014STao Ren mutex_init(&data->lock); 320*8efd1014STao Ren for (i = 0; i < ARRAY_SIZE(data->ctrl_byte); i++) 321*8efd1014STao Ren data->ctrl_byte[i] = (MAX127_CTRL_START | 322*8efd1014STao Ren MAX127_SET_CHANNEL(i)); 323*8efd1014STao Ren 324*8efd1014STao Ren hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, 325*8efd1014STao Ren data, 326*8efd1014STao Ren &max127_chip_info, 327*8efd1014STao Ren NULL); 328*8efd1014STao Ren 329*8efd1014STao Ren return PTR_ERR_OR_ZERO(hwmon_dev); 330*8efd1014STao Ren } 331*8efd1014STao Ren 332*8efd1014STao Ren static const struct i2c_device_id max127_id[] = { 333*8efd1014STao Ren { "max127", 0 }, 334*8efd1014STao Ren { } 335*8efd1014STao Ren }; 336*8efd1014STao Ren MODULE_DEVICE_TABLE(i2c, max127_id); 337*8efd1014STao Ren 338*8efd1014STao Ren static struct i2c_driver max127_driver = { 339*8efd1014STao Ren .class = I2C_CLASS_HWMON, 340*8efd1014STao Ren .driver = { 341*8efd1014STao Ren .name = "max127", 342*8efd1014STao Ren }, 343*8efd1014STao Ren .probe = max127_probe, 344*8efd1014STao Ren .id_table = max127_id, 345*8efd1014STao Ren }; 346*8efd1014STao Ren 347*8efd1014STao Ren module_i2c_driver(max127_driver); 348*8efd1014STao Ren 349*8efd1014STao Ren MODULE_LICENSE("GPL"); 350*8efd1014STao Ren MODULE_AUTHOR("Mike Choi <mikechoi@fb.com>"); 351*8efd1014STao Ren MODULE_AUTHOR("Tao Ren <rentao.bupt@gmail.com>"); 352*8efd1014STao Ren MODULE_DESCRIPTION("MAX127 Hardware Monitoring driver"); 353