aboutsummaryrefslogtreecommitdiff
path: root/drivers/platform/x86/intel_fg_helper.c
blob: 87c5876b04c63af2dd8da42c799b1d915664918a (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
/*
 * intel_fg_helper.c : Intel fuel gauging config helper
 *
 * (C) Copyright 2014 Intel Corporation
 * Author: Gopal, Saranya <saranya.gopal@intel.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 */

#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/atomic.h>

#define MAX_DATA_SIZE	256

static atomic_t fopen_count;
static bool is_fg_data_avail;
static void *fg_data;
static int num_bytes;

int (*intel_restore_data)(void *data, int len);
int (*intel_store_data)(void *data, int len);

/**
 * intel_fg_get_data - get FG config data
 * @data - config data output pointer
 * @len - length of config data
 */
int intel_fg_get_data(void *data, int len)
{
	if (!is_fg_data_avail)
		return -ENODATA;
	if (num_bytes < len)
		return -EINVAL;
	memcpy(data, fg_data, len);
	return 0;
}
EXPORT_SYMBOL(intel_fg_get_data);

/**
 * intel_fg_set_data - set FG config data
 * This function will set the config data to be stored
 * when the device file content is saved.
 * @data - config data input pointer
 * @len - length of the config data
 */
void intel_fg_set_data(void *data, int len)
{
	memcpy(fg_data, data, len);
}
EXPORT_SYMBOL(intel_fg_set_data);

/**
 * intel_fg_set_restore_fn - set the pointer to restore function.
 * The restore function set by this function will be called once FG
 * config data is written to the device file.
 * @restore_fn - pointer to restore function.
 */
void intel_fg_set_restore_fn(int (*restore_fn)(void *data, int len))
{
	intel_restore_data = restore_fn;
}
EXPORT_SYMBOL(intel_fg_set_restore_fn);

static int intel_fg_dev_file_open(struct inode *inode, struct file *file)
{
	if (atomic_read(&fopen_count))
		return -EBUSY;
	atomic_inc(&fopen_count);
	return 0;
}

/**
 * intel_fg_set_store_fn - set the pointer to store function
 * The store function set by this function will be called when
 * FG config data is read from the device file.
 * @store_fn - pointer to store function.
 */
void intel_fg_set_store_fn(int (*store_fn)(void *data, int len))
{
	intel_store_data = store_fn;
}
EXPORT_SYMBOL(intel_fg_set_store_fn);

static int intel_fg_dev_file_close(struct inode *inode, struct file *file)
{
	atomic_dec(&fopen_count);
	return 0;
}

static ssize_t intel_fg_dev_file_read(struct file *f, char __user *buf,
					size_t len, loff_t *off)
{
	if (!is_fg_data_avail)
		return -ENODATA;
	if (intel_store_data && !intel_store_data(fg_data, num_bytes))
		pr_info("FG data is updated");
	if (!copy_to_user(buf, fg_data, len))
		return len;
	return -EINVAL;
}

static ssize_t intel_fg_dev_file_write(struct file *f, const char __user *buf,
					size_t len, loff_t *off)
{
	if (copy_from_user(fg_data, buf, len))
		return -EINVAL;
	if (intel_restore_data && !intel_restore_data(fg_data, len))
		pr_info("FG data restored successfully");
	is_fg_data_avail = true;
	num_bytes = len;
	return len;
}

static const struct file_operations intel_fg_helper_fops = {
	.owner = THIS_MODULE,
	.open = &intel_fg_dev_file_open,
	.release = &intel_fg_dev_file_close,
	.read = &intel_fg_dev_file_read,
	.write = &intel_fg_dev_file_write,
};

static struct miscdevice fg_helper = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "intel_fg",
	.fops = &intel_fg_helper_fops,
};

static int __init fg_helper_init(void)
{
	fg_data = kzalloc(MAX_DATA_SIZE, GFP_KERNEL);
	intel_store_data = NULL;
	intel_restore_data = NULL;
	return misc_register(&fg_helper);
}
fs_initcall(fg_helper_init);

static void __exit fg_helper_exit(void)
{
	misc_deregister(&fg_helper);
}
module_exit(fg_helper_exit);

MODULE_AUTHOR("Saranya Gopal <saranya.gopal@intel.com>");
MODULE_DESCRIPTION("Fuel gauge config helper");
MODULE_LICENSE("GPL");