summaryrefslogtreecommitdiff
path: root/ina260_hwmon.c
blob: 67ad1e7724f94c2ad4191f9d56f951aa9dbe7b9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Driver for Texas Instruments INA260 power monitor chip
 * Datasheet: https://www.ti.com/lit/gpn/INA260
 *
 * Copyright (C) 2023 GUEGAN Loic <loic.guegan@mailbox.org>
 */

#include "linux/module.h"
#include "linux/uaccess.h"
#include "linux/i2c.h"
#include "linux/kobject.h"
#include "linux/slab.h"
#include "linux/kernel.h"
#include <linux/sysfs.h>
#include <linux/hwmon.h>
#include <linux/regmap.h>

// INA260 registers
#define INA260_REG_CONFIGURATION 0x00
#define INA260_REG_CURRENT 0x01
#define INA260_REG_VOLTAGE 0x02
#define INA260_REG_POWER 0x03
#define INA260_REG_MASKENABLE 0x06
#define INA260_REG_ALERTLIMIT 0x07
#define INA260_REG_MANUFACTURER 0xFE
#define INA260_REG_DIE 0xFF

static struct regmap_config ina260_regmap_config = {
    .max_register = INA260_REG_DIE,
	.reg_bits = 8,
	.val_bits = 16,
};

#define INA260_REG_SHOW(_attr,_reg) \
static ssize_t _attr##_show(struct device *dev, struct device_attribute *attr, char *buf) \
{ \
    unsigned int rvalue; \
    int err; \
    struct client_data *cdata=dev_get_drvdata(dev); \
    err = regmap_read(cdata->regmap, (_reg), &rvalue); \
    if(err>0) \
        return err; \
    return sprintf(buf, "%x\n", rvalue); \
}

#define INA260_REG_STORE(_attr,_reg) \
static ssize_t _attr##_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
{ \
    int uvalue, err; \
    struct client_data *cdata=dev_get_drvdata(dev); \
    if(kstrtoint(buf, 0,&uvalue)) \
        return -EINVAL; \
    err = regmap_write(cdata->regmap, (_reg), uvalue); \
    if(err>0) \
        return err; \
    return count; \
}

/**
 * @brief This macro is not available on old kernel especially the one
 * used by Beaglebones Single Board Computers
 */
#define HWMON_CHANNEL_INFO(stype, ...)	\
	(&(struct hwmon_channel_info) {	\
		.type = hwmon_##stype,	\
		.config = (u32 []) {	\
			__VA_ARGS__, 0	\
		}			\
	})

/**
 * @brief Embedded user data
 */
struct client_data {
    struct i2c_client *client;
    struct regmap *regmap;
};

static int ina260_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
		       u32 attr, int channel, long *val)
{
    int rvalue, reg, err, rem;
    struct client_data *cdata=dev_get_drvdata(dev);
    switch(type){
        case hwmon_power:
            reg=INA260_REG_POWER;
            break;
        case hwmon_curr:
            reg=INA260_REG_CURRENT;
            break;
        case hwmon_in:
            reg=INA260_REG_VOLTAGE;
            break;
        default:
            return -EOPNOTSUPP;
    }
    err=regmap_read(cdata->regmap, reg, &rvalue);
    if(err<0){
        return err;
    } else if(type == hwmon_power) {
        *val=10*rvalue*1000;
    } else{
        *val=div_u64_rem(rvalue*25,100,&rem)+rvalue+div_u64(rem,10);
    }
    return 0;
}

static int ina260_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
		       u32 attr, int channel, long val)
{
    return -EOPNOTSUPP;
}

INA260_REG_SHOW(configuration,INA260_REG_CONFIGURATION)
INA260_REG_SHOW(curr,INA260_REG_CURRENT)
INA260_REG_SHOW(bus_voltage,INA260_REG_VOLTAGE)
INA260_REG_SHOW(power,INA260_REG_POWER)
INA260_REG_SHOW(mask_enable,INA260_REG_MASKENABLE)
INA260_REG_SHOW(alert_limit,INA260_REG_ALERTLIMIT)
INA260_REG_SHOW(manufacturer_id,INA260_REG_MANUFACTURER)
INA260_REG_SHOW(die_id,INA260_REG_DIE)

INA260_REG_STORE(configuration,INA260_REG_CONFIGURATION)
INA260_REG_STORE(curr,INA260_REG_CURRENT)
INA260_REG_STORE(bus_voltage,INA260_REG_VOLTAGE)
INA260_REG_STORE(power,INA260_REG_POWER)
INA260_REG_STORE(mask_enable,INA260_REG_MASKENABLE)
INA260_REG_STORE(alert_limit,INA260_REG_ALERTLIMIT)

static umode_t ina260_hwmon_is_visible(const void *drvdata,
				 enum hwmon_sensor_types type,
				 u32 attr, int channel){
	switch (type) {
	case hwmon_in:
		switch (attr) {
		case hwmon_in_input:
			return 0444;
		default:
			return 0;
		}
	case hwmon_curr:
		switch (attr) {
		case hwmon_curr_input:
			return 0444;
		default:
			return 0;
		}
	case hwmon_power:
		switch (attr) {
		case hwmon_power_input:
			return 0444;
		default:
			return 0;
		}
	default:
		return 0;
	}
}
static const struct hwmon_channel_info *  ina260_hwmon_info[] = {
    HWMON_CHANNEL_INFO(in,HWMON_I_INPUT),
	HWMON_CHANNEL_INFO(power,HWMON_P_INPUT),
    HWMON_CHANNEL_INFO(curr,HWMON_C_INPUT),
	NULL
};
static const struct hwmon_ops ina260_hwmon_ops = {
    .is_visible = ina260_hwmon_is_visible,
	.read = ina260_hwmon_read,
	.write = ina260_hwmon_write,
};

static const struct hwmon_chip_info ina260_chip_info = {
	.ops = &ina260_hwmon_ops,
	.info = ina260_hwmon_info,
};
// ----- Registers -----
static DEVICE_ATTR_RW(configuration);
static DEVICE_ATTR_RW(curr);
static DEVICE_ATTR_RW(bus_voltage);
static DEVICE_ATTR_RW(power);
static DEVICE_ATTR_RW(mask_enable);
static DEVICE_ATTR_RW(alert_limit);
static DEVICE_ATTR_RO(manufacturer_id);
static DEVICE_ATTR_RO(die_id);
static struct attribute *registers_attrs[] = {
    &dev_attr_configuration.attr,
    &dev_attr_curr.attr,
    &dev_attr_bus_voltage.attr,
    &dev_attr_power.attr,
    &dev_attr_mask_enable.attr,
    &dev_attr_alert_limit.attr,
    &dev_attr_manufacturer_id.attr,
    &dev_attr_die_id.attr,
    NULL,   
};
static const struct attribute_group registers_group = {
	.attrs = registers_attrs,
    .name = "registers"
};

const struct attribute_group *extra_groups[] = {
    &registers_group,
    NULL
};

static int ina260_probe_new(struct i2c_client *client){
    struct client_data *p;
    struct device *hwmon_dev;

    // Initialize client data:
    printk("Adding ina260 [bus=%d address=0x%02x]\n",client->adapter->nr,client->addr);
    p=kzalloc(sizeof(struct client_data),GFP_KERNEL);
    p->client=client;
	p->regmap = devm_regmap_init_i2c(client, &ina260_regmap_config);

    hwmon_dev=hwmon_device_register_with_info(&client->dev,client->name,p,
    &ina260_chip_info,extra_groups);
	if (IS_ERR(hwmon_dev))
		return PTR_ERR(hwmon_dev);
    return 0;
}

static int ina260_remove(struct i2c_client *client){
    struct client_data *p=i2c_get_clientdata(client);
    kfree(p);
    hwmon_device_unregister(&client->dev);
    printk("Removing ina260 [bus=%d address=0x%02x]\n",client->adapter->nr,client->addr);
    return 0;
}

static const struct i2c_device_id ina260_ids[] = {
    { "ina260", 0 },
    { }
};
MODULE_DEVICE_TABLE(i2c,ina260_ids);

static struct i2c_driver ina260_driver = {
    .class = I2C_CLASS_HWMON,
	.driver = {
		.name = "ina260"
	},
	.probe_new = ina260_probe_new,
	.remove = ina260_remove,
	.id_table = ina260_ids
};

static int __init ina260_init(void){
    i2c_add_driver(&ina260_driver);
    return 0;
}

static void __exit ina260_exit(void){
    i2c_del_driver(&ina260_driver);
}

module_init(ina260_init);
module_exit(ina260_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Loïc Guegan");
MODULE_DESCRIPTION("INA260 Texas Instruments");
MODULE_VERSION("1.0");