summaryrefslogtreecommitdiff
path: root/ina260.c
blob: 1903e68cc117558e674aca553b072e59741fe8db (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "linux/module.h" // to access basic module functions (module_init etc..)
#include "linux/uaccess.h" // copy_from_user() etc...
#include "linux/i2c.h"
#include "linux/kobject.h"
#include "linux/slab.h" // kzalloc()
#include "linux/kernel.h" // Kernel utility e.g kstrtol
#include <linux/sysfs.h>

#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

#define INA260_IS_ATTR(_name) (strcmp(attr->attr.name, #_name) == 0)

static int ina260_avgs[8]={
    1,4,16,64,128,256,512,1024
};

static char ina260_modes[8][42]={
    "Power-Down (or Shutdown)","Shunt Current, Triggered",
    "Bus Voltage, Triggered","Shunt Current and Bus Voltage, Triggered", 
    "Power-Down (or Shutdown)","Shunt Current, Continuous","Bus Voltage, Continuous",
    "Shunt Current and Bus Voltage, Continuous"
};

static char ina260_ishcts_vbusct[8][9]={
    "140 µs", "204 µs","332 µs", "588 µs","1.1 ms", "2.116 ms","4.156 ms","8.244 ms"
};


static struct kobject* ina260_kobj;

struct client_data {
    struct kobject kobj;
    struct i2c_client *client;
    unsigned char reg; // Client selected register
};
static const struct i2c_device_id ina260_ids[] = {
    { "ina260", 0 },
    { }
};
MODULE_DEVICE_TABLE(i2c,ina260_ids); // Allow i2c subsystem to probe

static int ina260_read_register(struct client_data* cdata, unsigned char reg, int *value){
    unsigned char bytes[2];
    if(cdata->reg == reg){
        //printk("Cache\n");
        if(i2c_master_recv(cdata->client,bytes,2)<0)
            return 1;
    } else {
        //printk("No cache\n");
        if(i2c_master_send(cdata->client,&reg,1)<0)
            return 1;
        cdata->reg = reg;
        if(i2c_master_recv(cdata->client,bytes,2)<0)
            return 1;
    }
    *value=(bytes[0]<<8) | bytes[1];
    return 0;
}

static int ina260_write_register(struct client_data* cdata, unsigned char reg, int value){
    unsigned char data[3];
    data[0]=reg;
    data[1]=(value>>8) & 0xFF; // MSB
    data[2]=value & 0xFF;   // LSB
    if(i2c_master_send(cdata->client,data,3)<0)
        return 1;
    return 0;
}

static ssize_t attr_show(struct kobject *_kobj, 
                      struct kobj_attribute *attr,
                     char *buf)
{
    int value;
    struct client_data *data=container_of(_kobj,struct client_data,kobj);
    unsigned char reg=INA260_REG_POWER;

    if(!INA260_IS_ATTR(power)){
        if (INA260_IS_ATTR(current)){
            reg=INA260_REG_CURRENT;
        } else if(INA260_IS_ATTR(bus_voltage)) {
            reg=INA260_REG_VOLTAGE;
        } else if(INA260_IS_ATTR(mask_enable)) {
            reg=INA260_REG_MASKENABLE;
        } else if(INA260_IS_ATTR(alert_limit)) {
            reg=INA260_REG_ALERTLIMIT;
        } else if(INA260_IS_ATTR(configuration)) {
            reg=INA260_REG_CONFIGURATION;
        } else if(INA260_IS_ATTR(manufacturer_id)) {
            reg=INA260_REG_MANUFACTURER;
        } else {
            reg=INA260_REG_DIE;
        }
    }

    if(ina260_read_register(data,reg,&value))
        return -1;

    return sprintf(buf, "%x\n", value);
}
 
 
static ssize_t attr_store(struct kobject *_kobj, 
                       struct kobj_attribute *attr,
               const char *buf, size_t count)
{
    struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
    int data;
    unsigned char reg=INA260_REG_CONFIGURATION;
    if(kstrtoint(buf, 10,&data))
        return -EINVAL;

    if (INA260_IS_ATTR(mask_enable)){
        reg=INA260_REG_MASKENABLE;
    } else if (INA260_IS_ATTR(alert_limit)){
        reg=INA260_REG_ALERTLIMIT;
    }

    if(ina260_write_register(cdata, reg, data))
        return -1;

    return count;
}

static ssize_t attr_field_show(struct kobject *_kobj, 
                      struct kobj_attribute *attr,
                     char *buf)
{
    struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
    int value;
    if(INA260_IS_ATTR(avg)){
        if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&value))
            return -1;
        return sprintf(buf, "%d\n", ina260_avgs[(value>>9)&0x7]);
    } else if (INA260_IS_ATTR(mode)){
        if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&value))
            return -1;
        return sprintf(buf, "%s\n", ina260_modes[value&0x7]);
    } else if (INA260_IS_ATTR(ishct)){
        if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&value))
            return -1;
        return sprintf(buf, "%s\n", ina260_ishcts_vbusct[(value>>3)&0x7]);
    } else if (INA260_IS_ATTR(vbusct)){
        if(ina260_read_register(cdata, INA260_REG_CONFIGURATION,&value))
            return -1;
        return sprintf(buf, "%s\n", ina260_ishcts_vbusct[(value>>6)&0x7]);
    } else if (INA260_IS_ATTR(aff)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", (value>>4)&0x1);
    } else if (INA260_IS_ATTR(cvrf)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", (value>>3)&0x1);
    } else if (INA260_IS_ATTR(ocl)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", (value>>15)&0x1);
    }else if (INA260_IS_ATTR(ucl)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", (value>>14)&0x1);
    }else if (INA260_IS_ATTR(bol)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", (value>>13)&0x1);
    }else if (INA260_IS_ATTR(bul)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", (value>>12)&0x1);
    }else if (INA260_IS_ATTR(pol)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", (value>>11)&0x1);
    }else if (INA260_IS_ATTR(cnvr)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", (value>>10)&0x1);
    }else if (INA260_IS_ATTR(ovf)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", (value>>2)&0x1);
    }else if (INA260_IS_ATTR(apol)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", (value>>1)&0x1);
    }else if (INA260_IS_ATTR(len)){
        if(ina260_read_register(cdata, INA260_REG_MASKENABLE,&value))
            return -1;
        return sprintf(buf, "%d\n", value&0x1);
    }else if (INA260_IS_ATTR(did)){
        if(ina260_read_register(cdata, INA260_REG_DIE,&value))
            return -1;
        return sprintf(buf, "0x%x\n", (value>>4));
    }else if (INA260_IS_ATTR(rid)){
        if(ina260_read_register(cdata, INA260_REG_DIE,&value))
            return -1;
        return sprintf(buf, "0x%x\n", value&0x7);
    }else if (INA260_IS_ATTR(reset)){
        return sprintf(buf, "0\n");
    }
    return 0;
}

static int ina260_write_field3(struct client_data *cdata, unsigned char reg, unsigned char n, int value3bits){
    int mask, value;
    if(!(value3bits>=0 && value3bits <8))
        return -EINVAL;
    // Fetch register value:
    if(ina260_read_register(cdata,reg,&value))
        return 1;
    // Write bits:
    mask=~(0x7 << n);
    value &= mask; // clear bits
    value |= value3bits << n;
    // Write register value:
    if(ina260_write_register(cdata,reg,value))
        return 1;
    return 0;
}

static int ina260_write_field1(struct client_data *cdata, unsigned char reg, unsigned char n, int bit){
    int mask, value;
    if(!(bit==0 || bit == 1))
        return -EINVAL;
    // Fetch register value:
    if(ina260_read_register(cdata,reg,&value))
        return 1;
    // Set bit:
    mask=~(1<< n);
    value &= mask; // clear bit
    value |= bit << n;
    // Write register value
    if(ina260_write_register(cdata,reg,value))
        return 1;
    return 0;
}

static ssize_t attr_field_store(struct kobject *_kobj, 
                       struct kobj_attribute *attr,
               const char *buf, size_t count)
{
    struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
    int data=0, ret=count;
    unsigned char reg=INA260_REG_CONFIGURATION;
    // Extract user supplied value
    if(kstrtoint(buf, 10,&data))
        return -EINVAL;
    // Store:
    if(INA260_IS_ATTR(reset) && data!=0){
        ret=ina260_write_register(cdata, reg, 0xFFFF);
    } else if(INA260_IS_ATTR(avg)){
      ret=ina260_write_field3(cdata,reg,9,data);
    } else if(INA260_IS_ATTR(mode)){
      ret=ina260_write_field3(cdata,reg,0,data);
    } else if(INA260_IS_ATTR(ishct)){
      ret=ina260_write_field3(cdata,reg,3,data);
    } else if(INA260_IS_ATTR(vbusct)){
      ret=ina260_write_field3(cdata,reg,6,data);
    } else if(INA260_IS_ATTR(ocl)){
      ret=ina260_write_field1(cdata,reg,15,data);
    } else if(INA260_IS_ATTR(ucl)){
      ret=ina260_write_field1(cdata,reg,14,data);
    } else if(INA260_IS_ATTR(bol)){
      ret=ina260_write_field1(cdata,reg,13,data);
    } else if(INA260_IS_ATTR(bul)){
      ret=ina260_write_field1(cdata,reg,12,data);
    } else if(INA260_IS_ATTR(pol)){
      ret=ina260_write_field1(cdata,reg,11,data);
    } else if(INA260_IS_ATTR(cnvr)){
      ret=ina260_write_field1(cdata,reg,10,data);
    } else if(INA260_IS_ATTR(apol)){
      ret=ina260_write_field1(cdata,reg,1,data);
    } else if(INA260_IS_ATTR(len)){
      ret=ina260_write_field1(cdata,reg,0,data);
    }
    return ret ? ret: count;
}


// ----- Registers -----
static struct kobj_attribute configuration_attribute =
    __ATTR(configuration, 0664, attr_show, attr_store);
static struct kobj_attribute current_attribute = {
	.attr = {.name = "current", // current is a defined macro so need to do it manually
		 .mode = VERIFY_OCTAL_PERMISSIONS(0444) },
	.show	= attr_show,
	.store	= attr_store,
};
static struct kobj_attribute bus_voltage_attribute =
    __ATTR(bus_voltage, 0444, attr_show, attr_store);
static struct kobj_attribute power_attribute =
    __ATTR(power, 0444, attr_show, attr_store);
static struct kobj_attribute mask_enable_attribute =
    __ATTR(mask_enable, 0664, attr_show, attr_store);
static struct kobj_attribute alert_limit_attribute =
    __ATTR(alert_limit, 0664, attr_show, attr_store);
static struct kobj_attribute manufacturer_id_attribute =
    __ATTR(manufacturer_id, 0664, attr_show, attr_store);
static struct kobj_attribute die_id_attribute=
    __ATTR(die_id, 0664, attr_show, attr_store);
static struct attribute *registers_attrs[] = {
    &configuration_attribute.attr,
    &current_attribute.attr,
    &bus_voltage_attribute.attr,
    &power_attribute.attr,
    &mask_enable_attribute.attr,
    &alert_limit_attribute.attr,
    &manufacturer_id_attribute.attr,
    &die_id_attribute.attr,
    NULL,   
};
static const struct attribute_group registers_group = {
	.attrs = registers_attrs,
    .name = "registers"
};

// ----- Fields -----
static struct kobj_attribute reset_field_attribute =
    __ATTR(reset, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute avg_field_attribute =
    __ATTR(avg, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute mode_field_attribute =
    __ATTR(mode, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute ishct_field_attribute =
    __ATTR(ishct, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute vbusct_field_attribute =
    __ATTR(vbusct, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute aff_field_attribute =
    __ATTR(aff, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute cvrf_field_attribute =
    __ATTR(cvrf, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute ocl_field_attribute =
    __ATTR(ocl, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute ucl_field_attribute =
    __ATTR(ucl, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute bol_field_attribute =
    __ATTR(bol, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute bul_field_attribute =
    __ATTR(bul, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute pol_field_attribute =
    __ATTR(pol, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute cnvr_field_attribute =
    __ATTR(cnvr, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute ovf_field_attribute =
    __ATTR(ovf, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute apol_field_attribute =
    __ATTR(apol, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute len_field_attribute =
    __ATTR(len, 0664, attr_field_show, attr_field_store);
static struct kobj_attribute did_field_attribute =
    __ATTR(did, 0444, attr_field_show, attr_field_store);
static struct kobj_attribute rid_field_attribute =
    __ATTR(rid, 0444, attr_field_show, attr_field_store); 
static struct attribute *fields_attrs[] = {
    &reset_field_attribute.attr,
    &avg_field_attribute.attr,
    &mode_field_attribute.attr,
    &ishct_field_attribute.attr,
    &vbusct_field_attribute.attr,
    &aff_field_attribute.attr,
    &cvrf_field_attribute.attr,
    &ocl_field_attribute.attr,
    &ucl_field_attribute.attr,
    &bol_field_attribute.attr,
    &bul_field_attribute.attr,
    &pol_field_attribute.attr,
    &cnvr_field_attribute.attr,
    &ovf_field_attribute.attr,
    &apol_field_attribute.attr,
    &len_field_attribute.attr,
    &did_field_attribute.attr,
    &rid_field_attribute.attr,
    NULL,   
};
static const struct attribute_group fields_group = {
	.attrs = fields_attrs,
};


static struct kobj_type ina260_ktype = {
	.sysfs_ops	= &kobj_sysfs_ops,
};

static int ina260_probe_register(struct i2c_client *client, unsigned char reg, int value){
    unsigned char bytes[2];
    if(i2c_master_send(client,&reg,1)<0)
        return 1;
    if(i2c_master_recv(client,bytes,2)<0)
        return 1;
    return ((bytes[0]<<8) | bytes[1])!=value;
}

static int ina260_probe_new(struct i2c_client *client){
    struct client_data *p;
    if(ina260_probe_register(client,INA260_REG_MANUFACTURER,0x5449) ||
       ina260_probe_register(client,INA260_REG_DIE,0x2270)){
        printk("INA260 chip probe failed on i2c bus %d\n",client->adapter->nr);
        return 1;
    }
    printk("New INA260 chip addr=0x%x detected on i2c bus %d\n",client->addr,client->adapter->nr);
    p=kzalloc(sizeof(struct client_data),GFP_KERNEL);
    kobject_init(&p->kobj,&ina260_ktype);
    if(kobject_add(&p->kobj,ina260_kobj,client->dev.kobj.name)){
        kobject_put(&p->kobj);
        kfree(p);
        return 1;
    }
    p->client=client;
    p->reg=INA260_REG_DIE; // Last reg to be used
    i2c_set_clientdata(client,p);
    if(sysfs_create_group(&p->kobj,&registers_group)||
       sysfs_create_group(&p->kobj,&fields_group)){
        kobject_put(&p->kobj);
        kfree(p);
        return 1;
    }
    return 0;
}

static void ina260_remove(struct i2c_client *client){
    struct client_data *p=i2c_get_clientdata(client);
    kobject_put(&p->kobj);
    kfree(p);
    printk("INA260 addr=0x%x remove from i2c bus %d\n",client->addr,client->adapter->nr);
}

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

static int __init ina260_init(void){
    ina260_kobj = kobject_create_and_add("ina260",kernel_kobj);
    i2c_add_driver(&ina260_driver);
    return 0;
}

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

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");