summaryrefstree
path: root/kernel/drivers/battery/common/sec_pd.c
blob: b6d575d5d510e18c519a7040ca43ac11e2e97aa7 (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
/*
 * Copyrights (C) 2017 Samsung Electronics, Inc.
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/battery/sec_pd.h>

static SEC_PD_SINK_STATUS *g_psink_status;

int sec_pd_select_pdo(int num)
{
	if (!g_psink_status) {
		pr_err("%s: g_psink_status is NULL\n", __func__);
		return -1;
	}

	if (!g_psink_status->fp_sec_pd_select_pdo) {
		pr_err("%s: not exist\n", __func__);
		return -1;
	}

	g_psink_status->fp_sec_pd_select_pdo(num);

	return 0;
}
EXPORT_SYMBOL(sec_pd_select_pdo);

int sec_pd_select_pps(int num, int ppsVol, int ppsCur)
{
	if (!g_psink_status) {
		pr_err("%s: g_psink_status is NULL\n", __func__);
		return -1;
	}

	if (!g_psink_status->fp_sec_pd_select_pps) {
		pr_err("%s: not exist\n", __func__);
		return -1;
	}

		return g_psink_status->fp_sec_pd_select_pps(num, ppsVol, ppsCur);
}
EXPORT_SYMBOL(sec_pd_select_pps);

int sec_pd_get_apdo_max_power(unsigned int *pdo_pos, unsigned int *taMaxVol, unsigned int *taMaxCur, unsigned int *taMaxPwr)
{
	int i;
	int ret = 0;
	int max_current = 0, max_voltage = 0, max_power = 0;

	if (!g_psink_status) {
		pr_err("%s: g_psink_status is NULL\n", __func__);
		return -1;
	}

	if (!g_psink_status->has_apdo) {
		pr_info("%s: pd don't have apdo\n", __func__);
		return -1;
	}

	/* First, get TA maximum power from the fixed PDO */
	for (i = 1; i <= g_psink_status->available_pdo_num; i++) {
		if (!(g_psink_status->power_list[i].apdo)) {
			max_voltage = g_psink_status->power_list[i].max_voltage;
			max_current = g_psink_status->power_list[i].max_current;
			max_power = (max_voltage * max_current > max_power) ? (max_voltage * max_current) : max_power;
			*taMaxPwr = max_power;	/* uW */
		}
	}

	if (*pdo_pos == 0) {
		/* Get the proper PDO */
		for (i = 1; i <= g_psink_status->available_pdo_num; i++) {
			if (g_psink_status->power_list[i].apdo) {
				if (g_psink_status->power_list[i].max_voltage >= *taMaxVol) {
					*pdo_pos = i;
					*taMaxVol = g_psink_status->power_list[i].max_voltage;
					*taMaxCur = g_psink_status->power_list[i].max_current;
					break;
				}
			}
			if (*pdo_pos)
				break;
		}

		if (*pdo_pos == 0) {
			pr_info("mv (%d) and ma (%d) out of range of APDO\n",
				*taMaxVol, *taMaxCur);
			ret = -EINVAL;
		}
	} else {
		/* If we already have pdo object position, we don't need to search max current */
		ret = -ENOTSUPP;
	}

	pr_info("%s : *pdo_pos(%d), *taMaxVol(%d), *maxCur(%d), *maxPwr(%d)\n",
		__func__, *pdo_pos, *taMaxVol, *taMaxCur, *taMaxPwr);

	return ret;
}
EXPORT_SYMBOL(sec_pd_get_apdo_max_power);

void sec_pd_init_data(SEC_PD_SINK_STATUS* psink_status)
{
	g_psink_status = psink_status;
	if (g_psink_status)
		pr_info("%s: done.\n", __func__);
	else
		pr_err("%s: g_psink_status is NULL\n", __func__);
}
EXPORT_SYMBOL(sec_pd_init_data);

int sec_pd_register_chg_info_cb(void *cb)
{
	if (!g_psink_status) {
		pr_err("%s: g_psink_status is NULL\n", __func__);
		return -1;
	}
	g_psink_status->fp_sec_pd_ext_cb = cb;

	return 0;
}
EXPORT_SYMBOL(sec_pd_register_chg_info_cb);

void sec_pd_get_vid_pid(unsigned short *vid, unsigned short *pid, unsigned int *xid)
{
	if (!g_psink_status) {
		pr_err("%s: g_psink_status is NULL\n", __func__);
		return;
	}
	*vid = g_psink_status->vid;
	*pid = g_psink_status->pid;
	*xid = g_psink_status->xid;
}
EXPORT_SYMBOL(sec_pd_get_vid_pid);

static int __init sec_pd_init(void)
{
	pr_info("%s: \n", __func__);
	return 0;
}

module_init(sec_pd_init);
MODULE_DESCRIPTION("Samsung PD control");
MODULE_AUTHOR("Samsung Electronics");
MODULE_LICENSE("GPL");