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