Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Hardware monitoring driver for MAX127.
0004  *
0005  * Copyright (c) 2020 Facebook Inc.
0006  */
0007 
0008 #include <linux/err.h>
0009 #include <linux/hwmon.h>
0010 #include <linux/i2c.h>
0011 #include <linux/init.h>
0012 #include <linux/module.h>
0013 
0014 /*
0015  * MAX127 Control Byte. Refer to MAX127 datasheet, Table 1 "Control-Byte
0016  * Format" for details.
0017  */
0018 #define MAX127_CTRL_START   BIT(7)
0019 #define MAX127_CTRL_SEL_SHIFT   4
0020 #define MAX127_CTRL_RNG     BIT(3)
0021 #define MAX127_CTRL_BIP     BIT(2)
0022 #define MAX127_CTRL_PD1     BIT(1)
0023 #define MAX127_CTRL_PD0     BIT(0)
0024 
0025 #define MAX127_NUM_CHANNELS 8
0026 #define MAX127_SET_CHANNEL(ch)  (((ch) & 7) << MAX127_CTRL_SEL_SHIFT)
0027 
0028 /*
0029  * MAX127 channel input ranges. Refer to MAX127 datasheet, Table 3 "Range
0030  * and Polarity Selection" for details.
0031  */
0032 #define MAX127_FULL_RANGE   10000   /* 10V */
0033 #define MAX127_HALF_RANGE   5000    /* 5V */
0034 
0035 /*
0036  * MAX127 returns 2 bytes at read:
0037  *   - the first byte contains data[11:4].
0038  *   - the second byte contains data[3:0] (MSB) and 4 dummy 0s (LSB).
0039  * Refer to MAX127 datasheet, "Read a Conversion (Read Cycle)" section
0040  * for details.
0041  */
0042 #define MAX127_DATA_LEN     2
0043 #define MAX127_DATA_SHIFT   4
0044 
0045 #define MAX127_SIGN_BIT     BIT(11)
0046 
0047 struct max127_data {
0048     struct mutex lock;
0049     struct i2c_client *client;
0050     u8 ctrl_byte[MAX127_NUM_CHANNELS];
0051 };
0052 
0053 static int max127_select_channel(struct i2c_client *client, u8 ctrl_byte)
0054 {
0055     int status;
0056     struct i2c_msg msg = {
0057         .addr = client->addr,
0058         .flags = 0,
0059         .len = sizeof(ctrl_byte),
0060         .buf = &ctrl_byte,
0061     };
0062 
0063     status = i2c_transfer(client->adapter, &msg, 1);
0064     if (status < 0)
0065         return status;
0066     if (status != 1)
0067         return -EIO;
0068 
0069     return 0;
0070 }
0071 
0072 static int max127_read_channel(struct i2c_client *client, long *val)
0073 {
0074     int status;
0075     u8 i2c_data[MAX127_DATA_LEN];
0076     struct i2c_msg msg = {
0077         .addr = client->addr,
0078         .flags = I2C_M_RD,
0079         .len = sizeof(i2c_data),
0080         .buf = i2c_data,
0081     };
0082 
0083     status = i2c_transfer(client->adapter, &msg, 1);
0084     if (status < 0)
0085         return status;
0086     if (status != 1)
0087         return -EIO;
0088 
0089     *val = (i2c_data[1] >> MAX127_DATA_SHIFT) |
0090         ((u16)i2c_data[0] << MAX127_DATA_SHIFT);
0091     return 0;
0092 }
0093 
0094 static long max127_process_raw(u8 ctrl_byte, long raw)
0095 {
0096     long scale, weight;
0097 
0098     /*
0099      * MAX127's data coding is binary in unipolar mode with 1 LSB =
0100      * (Full-Scale/4096) and two’s complement binary in bipolar mode
0101      * with 1 LSB = [(2 x |FS|)/4096].
0102      * Refer to MAX127 datasheet, "Transfer Function" section for
0103      * details.
0104      */
0105     scale = (ctrl_byte & MAX127_CTRL_RNG) ? MAX127_FULL_RANGE :
0106                         MAX127_HALF_RANGE;
0107     if (ctrl_byte & MAX127_CTRL_BIP) {
0108         weight = (raw & MAX127_SIGN_BIT);
0109         raw &= ~MAX127_SIGN_BIT;
0110         raw -= weight;
0111         raw *= 2;
0112     }
0113 
0114     return raw * scale / 4096;
0115 }
0116 
0117 static int max127_read_input(struct max127_data *data, int channel, long *val)
0118 {
0119     long raw;
0120     int status;
0121     struct i2c_client *client = data->client;
0122     u8 ctrl_byte = data->ctrl_byte[channel];
0123 
0124     mutex_lock(&data->lock);
0125 
0126     status = max127_select_channel(client, ctrl_byte);
0127     if (status)
0128         goto exit;
0129 
0130     status = max127_read_channel(client, &raw);
0131     if (status)
0132         goto exit;
0133 
0134     *val = max127_process_raw(ctrl_byte, raw);
0135 
0136 exit:
0137     mutex_unlock(&data->lock);
0138     return status;
0139 }
0140 
0141 static int max127_read_min(struct max127_data *data, int channel, long *val)
0142 {
0143     u8 rng_bip = (data->ctrl_byte[channel] >> 2) & 3;
0144     static const int min_input_map[4] = {
0145         0,          /* RNG=0, BIP=0 */
0146         -MAX127_HALF_RANGE, /* RNG=0, BIP=1 */
0147         0,          /* RNG=1, BIP=0 */
0148         -MAX127_FULL_RANGE, /* RNG=1, BIP=1 */
0149     };
0150 
0151     *val = min_input_map[rng_bip];
0152     return 0;
0153 }
0154 
0155 static int max127_read_max(struct max127_data *data, int channel, long *val)
0156 {
0157     u8 rng_bip = (data->ctrl_byte[channel] >> 2) & 3;
0158     static const int max_input_map[4] = {
0159         MAX127_HALF_RANGE,  /* RNG=0, BIP=0 */
0160         MAX127_HALF_RANGE,  /* RNG=0, BIP=1 */
0161         MAX127_FULL_RANGE,  /* RNG=1, BIP=0 */
0162         MAX127_FULL_RANGE,  /* RNG=1, BIP=1 */
0163     };
0164 
0165     *val = max_input_map[rng_bip];
0166     return 0;
0167 }
0168 
0169 static int max127_write_min(struct max127_data *data, int channel, long val)
0170 {
0171     u8 ctrl;
0172 
0173     mutex_lock(&data->lock);
0174 
0175     ctrl = data->ctrl_byte[channel];
0176     if (val <= -MAX127_FULL_RANGE) {
0177         ctrl |= (MAX127_CTRL_RNG | MAX127_CTRL_BIP);
0178     } else if (val < 0) {
0179         ctrl |= MAX127_CTRL_BIP;
0180         ctrl &= ~MAX127_CTRL_RNG;
0181     } else {
0182         ctrl &= ~MAX127_CTRL_BIP;
0183     }
0184     data->ctrl_byte[channel] = ctrl;
0185 
0186     mutex_unlock(&data->lock);
0187 
0188     return 0;
0189 }
0190 
0191 static int max127_write_max(struct max127_data *data, int channel, long val)
0192 {
0193     mutex_lock(&data->lock);
0194 
0195     if (val >= MAX127_FULL_RANGE)
0196         data->ctrl_byte[channel] |= MAX127_CTRL_RNG;
0197     else
0198         data->ctrl_byte[channel] &= ~MAX127_CTRL_RNG;
0199 
0200     mutex_unlock(&data->lock);
0201 
0202     return 0;
0203 }
0204 
0205 static umode_t max127_is_visible(const void *_data,
0206                  enum hwmon_sensor_types type,
0207                  u32 attr, int channel)
0208 {
0209     if (type == hwmon_in) {
0210         switch (attr) {
0211         case hwmon_in_input:
0212             return 0444;
0213 
0214         case hwmon_in_min:
0215         case hwmon_in_max:
0216             return 0644;
0217 
0218         default:
0219             break;
0220         }
0221     }
0222 
0223     return 0;
0224 }
0225 
0226 static int max127_read(struct device *dev, enum hwmon_sensor_types type,
0227             u32 attr, int channel, long *val)
0228 {
0229     int status;
0230     struct max127_data *data = dev_get_drvdata(dev);
0231 
0232     if (type != hwmon_in)
0233         return -EOPNOTSUPP;
0234 
0235     switch (attr) {
0236     case hwmon_in_input:
0237         status = max127_read_input(data, channel, val);
0238         break;
0239 
0240     case hwmon_in_min:
0241         status = max127_read_min(data, channel, val);
0242         break;
0243 
0244     case hwmon_in_max:
0245         status = max127_read_max(data, channel, val);
0246         break;
0247 
0248     default:
0249         status = -EOPNOTSUPP;
0250         break;
0251     }
0252 
0253     return status;
0254 }
0255 
0256 static int max127_write(struct device *dev, enum hwmon_sensor_types type,
0257             u32 attr, int channel, long val)
0258 {
0259     int status;
0260     struct max127_data *data = dev_get_drvdata(dev);
0261 
0262     if (type != hwmon_in)
0263         return -EOPNOTSUPP;
0264 
0265     switch (attr) {
0266     case hwmon_in_min:
0267         status = max127_write_min(data, channel, val);
0268         break;
0269 
0270     case hwmon_in_max:
0271         status = max127_write_max(data, channel, val);
0272         break;
0273 
0274     default:
0275         status = -EOPNOTSUPP;
0276         break;
0277     }
0278 
0279     return status;
0280 }
0281 
0282 static const struct hwmon_ops max127_hwmon_ops = {
0283     .is_visible = max127_is_visible,
0284     .read = max127_read,
0285     .write = max127_write,
0286 };
0287 
0288 static const struct hwmon_channel_info *max127_info[] = {
0289     HWMON_CHANNEL_INFO(in,
0290                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
0291                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
0292                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
0293                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
0294                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
0295                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
0296                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
0297                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX),
0298     NULL,
0299 };
0300 
0301 static const struct hwmon_chip_info max127_chip_info = {
0302     .ops = &max127_hwmon_ops,
0303     .info = max127_info,
0304 };
0305 
0306 static int max127_probe(struct i2c_client *client,
0307             const struct i2c_device_id *id)
0308 {
0309     int i;
0310     struct device *hwmon_dev;
0311     struct max127_data *data;
0312     struct device *dev = &client->dev;
0313 
0314     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0315     if (!data)
0316         return -ENOMEM;
0317 
0318     data->client = client;
0319     mutex_init(&data->lock);
0320     for (i = 0; i < ARRAY_SIZE(data->ctrl_byte); i++)
0321         data->ctrl_byte[i] = (MAX127_CTRL_START |
0322                       MAX127_SET_CHANNEL(i));
0323 
0324     hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
0325                              data,
0326                              &max127_chip_info,
0327                              NULL);
0328 
0329     return PTR_ERR_OR_ZERO(hwmon_dev);
0330 }
0331 
0332 static const struct i2c_device_id max127_id[] = {
0333     { "max127", 0 },
0334     { }
0335 };
0336 MODULE_DEVICE_TABLE(i2c, max127_id);
0337 
0338 static struct i2c_driver max127_driver = {
0339     .class      = I2C_CLASS_HWMON,
0340     .driver = {
0341         .name   = "max127",
0342     },
0343     .probe      = max127_probe,
0344     .id_table   = max127_id,
0345 };
0346 
0347 module_i2c_driver(max127_driver);
0348 
0349 MODULE_LICENSE("GPL");
0350 MODULE_AUTHOR("Mike Choi <mikechoi@fb.com>");
0351 MODULE_AUTHOR("Tao Ren <rentao.bupt@gmail.com>");
0352 MODULE_DESCRIPTION("MAX127 Hardware Monitoring driver");