summaryrefstree
path: root/kernel/drivers/ccic/usbpd-core.c
blob: 61dcd5e7793876c48f744eaf0319cd870c6a6b3f (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
/*
 * usbpd-core.c  --  usbpd control IC framework.
 *
 *  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.
 *
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/suspend.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/of.h>
#include <linux/ccic/core.h>
#include <linux/module.h>

static struct class usbpd_class;

static struct dentry *debugfs_root;

#if defined(CONFIG_IFCONN_NOTIFIER)
static int usbpd_handle_ifconn_notification(struct notifier_block *nb,
				unsigned long action, void *data)
{
	struct ifconn_notifier_template *template = data;
	struct usbpd_dev *udev =
			container_of(nb, struct usbpd_dev, nb);
	struct pdic_notifier_data *pd_noti = &udev->pd_noti;
	ifconn_notifier_id_t notifier_id = template->id;
	int select_pdo_num = 0, ret = 0;

	pr_info("%s, event : %d\n", __func__, template->event);
	if (notifier_id == IFCONN_NOTIFY_ID_SELECT_PDO) {
		select_pdo_num = template->event;
		if (pd_noti->sink_status.selected_pdo_num == select_pdo_num || select_pdo_num == 0)
			return 0;
		else if (select_pdo_num > pd_noti->sink_status.available_pdo_num)
			pd_noti->sink_status.selected_pdo_num = pd_noti->sink_status.available_pdo_num;
		else if (select_pdo_num < 1)
			pd_noti->sink_status.selected_pdo_num = 1;
		else
			pd_noti->sink_status.selected_pdo_num = select_pdo_num;

		ret = udev->desc->ops->usbpd_select_pdo(udev, pd_noti->sink_status.selected_pdo_num);
	}

	return ret;
}
#endif

static void usbpd_init_pd_info(struct usbpd_desc *usbpd_desc)
{
	struct usbpd_info *pd_info = &usbpd_desc->pd_info;

	pd_info->is_host = 0;
	pd_info->is_client = 0;
	pd_info->is_pr_swap = 0;
	pd_info->power_role = 0;
	pd_info->data_role = 0;
	pd_info->request_power_number = 0;
	pd_info->pd_charger_attached = false;
	pd_info->plug_attached = false;
	pd_info->dp_is_connect = false;
	pd_info->dp_hs_connect = false;
	pd_info->is_sent_pin_configuration = false;
	pd_info->is_src = false;
	pd_info->is_samsung_accessory_enter_mode = false;
}

static struct usbpd_init_data *usbpd_get_init_data(const char *name)
{
	struct device_node *np;
	struct usbpd_init_data *init_data = NULL;
	int ret = 0;

	np = of_find_node_by_name(NULL, "usbpd-manager");
	if (np == NULL) {
		pr_err("%s np NULL\n", __func__);
		return ERR_PTR(-EINVAL);
	}

	init_data = kzalloc(sizeof(struct usbpd_init_data), GFP_KERNEL);
	if (!init_data)
		return ERR_PTR(-ENOMEM); /* Out of memory? */

	ret = of_property_read_u32(np, "pdic,max_voltage",
			&init_data->max_charging_volt);
	if (ret < 0)
		pr_err("%s error reading max_charging_volt %d\n",
				__func__, init_data->max_charging_volt);

	ret = of_property_read_u32(np, "pdic,max_power",
			&init_data->max_power);
	if (ret < 0)
		pr_err("%s error reading max_power %d\n",
				__func__, init_data->max_power);

	ret = of_property_read_u32(np, "pdic,op_power",
			&init_data->op_power);
	if (ret < 0)
		pr_err("%s error reading op_power %d\n",
				__func__, init_data->op_power);

	ret = of_property_read_u32(np, "pdic,max_current",
			&init_data->max_current);
	if (ret < 0)
		pr_err("%s error reading max_current %d\n",
				__func__, init_data->max_current);

	ret = of_property_read_u32(np, "pdic,min_current",
			&init_data->min_current);
	if (ret < 0)
		pr_err("%s error reading min_current %d\n",
				__func__, init_data->min_current);

	init_data->giveback = of_property_read_bool(np,
					     "pdic,giveback");
	init_data->usb_com_capable = of_property_read_bool(np,
					     "pdic,usb_com_capable");
	init_data->no_usb_suspend = of_property_read_bool(np,
					     "pdic,no_usb_suspend");

	/* source capability */
	ret = of_property_read_u32(np, "source,max_voltage",
			&init_data->source_max_volt);
	ret = of_property_read_u32(np, "source,min_voltage",
			&init_data->source_min_volt);
	ret = of_property_read_u32(np, "source,max_power",
			&init_data->source_max_power);

	/* sink capability */
	ret = of_property_read_u32(np, "sink,capable_max_voltage",
			&init_data->sink_cap_max_volt);
	if (ret < 0) {
		init_data->sink_cap_max_volt = 5000;
		pr_err("%s error reading sink_cap_max_volt %d\n",
				__func__, init_data->sink_cap_max_volt);
	}

	return init_data;
}

/**
 * usbpd_register - register usbpd
 * @usbpd_desc: usbpd to register
 * @cfg: runtime configuration for usbpd
 *
 * Called by usbpd drivers to register a usbpd.
 * Returns a valid pointer to struct usbpd_dev on success
 * or an ERR_PTR() on error.
 */
struct usbpd_dev *
usbpd_register(struct device *dev, struct usbpd_desc *usbpd_desc, void *_data)
{
	struct usbpd_init_data *init_data;
	struct usbpd_dev *udev;
	int ret = 0;

	if (usbpd_desc->name == NULL || usbpd_desc->ops == NULL)
		return ERR_PTR(-EINVAL);

	udev = kzalloc(sizeof(struct usbpd_dev), GFP_KERNEL);
	if (udev == NULL)
		return ERR_PTR(-ENOMEM);

#if defined(CONFIG_IFCONN_NOTIFIER)
	ret = ifconn_notifier_register(&udev->nb,
				usbpd_handle_ifconn_notification,
				IFCONN_NOTIFY_CCIC,
				IFCONN_NOTIFY_MANAGER);
	if (ret < 0) {
		pr_err("%s notifier register failed.\n", __func__);
	}
#endif

	init_data = usbpd_get_init_data(usbpd_desc->name);
	if (IS_ERR(init_data)) {
		pr_err("%s, init_data init error!\n", __func__);
		init_data = NULL;
	}
	/* Todo abnormal state
	if (!init_data || ret < 0) {
		init_data = config->init_data;
		udev->dev.of_node = of_node_get(config->of_node);
	}
	*/
	mutex_init(&udev->mutex);
	udev->desc = usbpd_desc;
	udev->drv_data = _data;

	udev->dev.parent = dev;
	dev_set_name(&udev->dev, usbpd_desc->name);

	usbpd_init_pd_info(udev->desc);


	/* set usbpd constraints */
	if (init_data)
		udev->init_data = init_data;

	/* Todo for sysfs */
	ret = device_register(&udev->dev);

	if (ret != 0)
		goto clean;

	mdelay(10);

#if defined(CONFIG_DUAL_ROLE_USB_INTF)
	dual_role_init(udev);
#endif

	return udev;

clean:
	kfree(udev);
	return ERR_PTR(ret);
}
EXPORT_SYMBOL_GPL(usbpd_register);

/**
 * usbpd_unregister - unregister usbpd
 * @udev: usbpd to unregister
 *
 * Called by usbpd drivers to unregister a usbpd.
 */
void usbpd_unregister(struct usbpd_dev *udev)
{
	if (udev == NULL)
		return;

	device_unregister(&udev->dev);
}
EXPORT_SYMBOL_GPL(usbpd_unregister);

/**
 * udev_get_drvdata - get udev usbpd driver data
 * @udev: usbpd
 *
 * Get udev usbpd driver private data. This call can be used in the
 * usbpd driver context.
 */
void *udev_get_drvdata(struct usbpd_dev *udev)
{
	return udev->drv_data;
}
EXPORT_SYMBOL_GPL(udev_get_drvdata);

static int __init usbpd_init(void)
{
	int ret;

	ret = class_register(&usbpd_class);

	debugfs_root = debugfs_create_dir("usbpd", NULL);
	if (!debugfs_root)
		pr_warn("usbpd: Failed to create debugfs directory\n");

	return ret;
}

/* init early to allow our consumers to complete system booting */
core_initcall(usbpd_init);