1 // SPDX-License-Identifier: GPL-2.0+
3 * Hardware monitoring driver for MAX127.
5 * Copyright (c) 2020 Facebook Inc.
9 #include <linux/hwmon.h>
10 #include <linux/i2c.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
15 * MAX127 Control Byte. Refer to MAX127 datasheet, Table 1 "Control-Byte
16 * Format" for details.
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)
25 #define MAX127_NUM_CHANNELS 8
26 #define MAX127_SET_CHANNEL(ch) (((ch) & 7) << MAX127_CTRL_SEL_SHIFT)
29 * MAX127 channel input ranges. Refer to MAX127 datasheet, Table 3 "Range
30 * and Polarity Selection" for details.
32 #define MAX127_FULL_RANGE 10000 /* 10V */
33 #define MAX127_HALF_RANGE 5000 /* 5V */
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
42 #define MAX127_DATA_LEN 2
43 #define MAX127_DATA_SHIFT 4
45 #define MAX127_SIGN_BIT BIT(11)
49 struct i2c_client
*client
;
50 u8 ctrl_byte
[MAX127_NUM_CHANNELS
];
53 static int max127_select_channel(struct i2c_client
*client
, u8 ctrl_byte
)
56 struct i2c_msg msg
= {
59 .len
= sizeof(ctrl_byte
),
63 status
= i2c_transfer(client
->adapter
, &msg
, 1);
72 static int max127_read_channel(struct i2c_client
*client
, long *val
)
75 u8 i2c_data
[MAX127_DATA_LEN
];
76 struct i2c_msg msg
= {
79 .len
= sizeof(i2c_data
),
83 status
= i2c_transfer(client
->adapter
, &msg
, 1);
89 *val
= (i2c_data
[1] >> MAX127_DATA_SHIFT
) |
90 ((u16
)i2c_data
[0] << MAX127_DATA_SHIFT
);
94 static long max127_process_raw(u8 ctrl_byte
, long raw
)
99 * MAX127's data coding is binary in unipolar mode with 1 LSB =
100 * (Full-Scale/4096) and two’s complement binary in bipolar mode
101 * with 1 LSB = [(2 x |FS|)/4096].
102 * Refer to MAX127 datasheet, "Transfer Function" section for
105 scale
= (ctrl_byte
& MAX127_CTRL_RNG
) ? MAX127_FULL_RANGE
:
107 if (ctrl_byte
& MAX127_CTRL_BIP
) {
108 weight
= (raw
& MAX127_SIGN_BIT
);
109 raw
&= ~MAX127_SIGN_BIT
;
114 return raw
* scale
/ 4096;
117 static int max127_read_input(struct max127_data
*data
, int channel
, long *val
)
121 struct i2c_client
*client
= data
->client
;
122 u8 ctrl_byte
= data
->ctrl_byte
[channel
];
124 mutex_lock(&data
->lock
);
126 status
= max127_select_channel(client
, ctrl_byte
);
130 status
= max127_read_channel(client
, &raw
);
134 *val
= max127_process_raw(ctrl_byte
, raw
);
137 mutex_unlock(&data
->lock
);
141 static int max127_read_min(struct max127_data
*data
, int channel
, long *val
)
143 u8 rng_bip
= (data
->ctrl_byte
[channel
] >> 2) & 3;
144 static const int min_input_map
[4] = {
145 0, /* RNG=0, BIP=0 */
146 -MAX127_HALF_RANGE
, /* RNG=0, BIP=1 */
147 0, /* RNG=1, BIP=0 */
148 -MAX127_FULL_RANGE
, /* RNG=1, BIP=1 */
151 *val
= min_input_map
[rng_bip
];
155 static int max127_read_max(struct max127_data
*data
, int channel
, long *val
)
157 u8 rng_bip
= (data
->ctrl_byte
[channel
] >> 2) & 3;
158 static const int max_input_map
[4] = {
159 MAX127_HALF_RANGE
, /* RNG=0, BIP=0 */
160 MAX127_HALF_RANGE
, /* RNG=0, BIP=1 */
161 MAX127_FULL_RANGE
, /* RNG=1, BIP=0 */
162 MAX127_FULL_RANGE
, /* RNG=1, BIP=1 */
165 *val
= max_input_map
[rng_bip
];
169 static int max127_write_min(struct max127_data
*data
, int channel
, long val
)
173 mutex_lock(&data
->lock
);
175 ctrl
= data
->ctrl_byte
[channel
];
176 if (val
<= -MAX127_FULL_RANGE
) {
177 ctrl
|= (MAX127_CTRL_RNG
| MAX127_CTRL_BIP
);
178 } else if (val
< 0) {
179 ctrl
|= MAX127_CTRL_BIP
;
180 ctrl
&= ~MAX127_CTRL_RNG
;
182 ctrl
&= ~MAX127_CTRL_BIP
;
184 data
->ctrl_byte
[channel
] = ctrl
;
186 mutex_unlock(&data
->lock
);
191 static int max127_write_max(struct max127_data
*data
, int channel
, long val
)
193 mutex_lock(&data
->lock
);
195 if (val
>= MAX127_FULL_RANGE
)
196 data
->ctrl_byte
[channel
] |= MAX127_CTRL_RNG
;
198 data
->ctrl_byte
[channel
] &= ~MAX127_CTRL_RNG
;
200 mutex_unlock(&data
->lock
);
205 static umode_t
max127_is_visible(const void *_data
,
206 enum hwmon_sensor_types type
,
207 u32 attr
, int channel
)
209 if (type
== hwmon_in
) {
226 static int max127_read(struct device
*dev
, enum hwmon_sensor_types type
,
227 u32 attr
, int channel
, long *val
)
230 struct max127_data
*data
= dev_get_drvdata(dev
);
232 if (type
!= hwmon_in
)
237 status
= max127_read_input(data
, channel
, val
);
241 status
= max127_read_min(data
, channel
, val
);
245 status
= max127_read_max(data
, channel
, val
);
249 status
= -EOPNOTSUPP
;
256 static int max127_write(struct device
*dev
, enum hwmon_sensor_types type
,
257 u32 attr
, int channel
, long val
)
260 struct max127_data
*data
= dev_get_drvdata(dev
);
262 if (type
!= hwmon_in
)
267 status
= max127_write_min(data
, channel
, val
);
271 status
= max127_write_max(data
, channel
, val
);
275 status
= -EOPNOTSUPP
;
282 static const struct hwmon_ops max127_hwmon_ops
= {
283 .is_visible
= max127_is_visible
,
285 .write
= max127_write
,
288 static const struct hwmon_channel_info
*max127_info
[] = {
289 HWMON_CHANNEL_INFO(in
,
290 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
,
291 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
,
292 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
,
293 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
,
294 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
,
295 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
,
296 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
,
297 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
),
301 static const struct hwmon_chip_info max127_chip_info
= {
302 .ops
= &max127_hwmon_ops
,
306 static int max127_probe(struct i2c_client
*client
,
307 const struct i2c_device_id
*id
)
310 struct device
*hwmon_dev
;
311 struct max127_data
*data
;
312 struct device
*dev
= &client
->dev
;
314 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
318 data
->client
= client
;
319 mutex_init(&data
->lock
);
320 for (i
= 0; i
< ARRAY_SIZE(data
->ctrl_byte
); i
++)
321 data
->ctrl_byte
[i
] = (MAX127_CTRL_START
|
322 MAX127_SET_CHANNEL(i
));
324 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
329 return PTR_ERR_OR_ZERO(hwmon_dev
);
332 static const struct i2c_device_id max127_id
[] = {
336 MODULE_DEVICE_TABLE(i2c
, max127_id
);
338 static struct i2c_driver max127_driver
= {
339 .class = I2C_CLASS_HWMON
,
343 .probe
= max127_probe
,
344 .id_table
= max127_id
,
347 module_i2c_driver(max127_driver
);
349 MODULE_LICENSE("GPL");
350 MODULE_AUTHOR("Mike Choi <mikechoi@fb.com>");
351 MODULE_AUTHOR("Tao Ren <rentao.bupt@gmail.com>");
352 MODULE_DESCRIPTION("MAX127 Hardware Monitoring driver");