summaryrefstree
path: root/kernel/drivers/ccic/ccic_core.c
blob: 6f1e0cacd9c96bafb5a2da9348b3e1b60775e7fc (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
/*
 * driver/../ccic_core.c - CCIC Core implementation
 *
 * Copyright (C) 2017 Samsung Electronics
 *
 * Author:Wookwang Lee. <wookwang.lee@samsung.com>,
 * Author:Guneet Singh Khurana  <gs.khurana@samsung.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; 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <linux/device.h>
#include <linux/module.h>
#include <linux/sec_sysfs.h>
#include <linux/ccic/ccic_core.h>
#include <linux/ccic/ccic_sysfs.h>
#include <linux/power_supply.h>
#include <linux/of.h>
#include <linux/regulator/consumer.h>

#ifndef CONFIG_SWITCH
#error "ERROR: CONFIG_SWITCH is not set."
#endif /* CONFIG_SWITCH */

#include <linux/switch.h>

struct device *ccic_device;
static struct switch_dev switch_dock = {
	.name = "ccic_dock",
};

int ccic_register_switch_device(int mode)
{
	int ret = 0;

	if (mode) {
		ret = switch_dev_register(&switch_dock);
		if (ret < 0) {
			pr_err("%s: Failed to register dock switch(%d)\n",
			       __func__, ret);
			return -ENODEV;
		}
	} else {
		switch_dev_unregister(&switch_dock);
	}
	return 0;
}

void ccic_send_dock_intent(int type)
{
	pr_info("%s: CCIC dock type(%d)", __func__, type);
	switch_set_state(&switch_dock, type);
}

void ccic_send_dock_uevent(u32 vid, u32 pid, int state)
{
	char switch_string[32];
	char pd_ids_string[32];
	char *envp[3] = { switch_string, pd_ids_string, NULL };

	pr_info("%s: CCIC dock : USBPD_IPS=%04x:%04x SWITCH_STATE=%d\n",
			__func__,
			le16_to_cpu(vid),
			le16_to_cpu(pid),
			state);

	if (IS_ERR(ccic_device)) {
		pr_err("%s CCIC ERROR: Failed to send a dock uevent!\n",
				__func__);
		return;
	}

	snprintf(switch_string, 32, "SWITCH_STATE=%d", state);
	snprintf(pd_ids_string, 32, "USBPD_IDS=%04x:%04x",
			le16_to_cpu(vid),
			le16_to_cpu(pid));
	kobject_uevent_env(&ccic_device->kobj, KOBJ_CHANGE, envp);
}

int ccic_core_register_chip(pccic_data_t pccic_data)
{
	int ret = 0;

	pr_info("%s\n", __func__);
	if (IS_ERR(ccic_device)) {
		pr_err("%s ccic_device is not present try again\n", __func__);
		ret = -ENODEV;
		goto out;
	}

	dev_set_drvdata(ccic_device, pccic_data);

	/* create sysfs group */
	ret = sysfs_create_group(&ccic_device->kobj, &ccic_sysfs_group);
	if (ret)
		pr_err("%s: ccic sysfs fail, ret %d", __func__, ret);
out:
	return ret;
}

void ccic_core_unregister_chip(void)
{
	pr_info("%s\n", __func__);
	if (IS_ERR(ccic_device)) {
		pr_err("%s ccic_device is not present try again\n", __func__);
		return;
	}
	sysfs_remove_group(&ccic_device->kobj, &ccic_sysfs_group);
	dev_set_drvdata(ccic_device, NULL);
}

int ccic_core_init(void)
{
	int ret = 0;

	pr_info("%s\n", __func__);

	ccic_device = sec_device_create(NULL, "ccic");
	if (IS_ERR(ccic_device)) {
		pr_err("%s Failed to create device(switch)!\n", __func__);
		ret = -ENODEV;
		goto out;
	}
	dev_set_drvdata(ccic_device, NULL);
	ccic_sysfs_init_attrs();
out:
	return ret;
}
void *ccic_core_get_drvdata(void)
{
	pccic_data_t pccic_data = NULL;

	pr_info("%s\n", __func__);
	if (!ccic_device) {
		pr_err("%s ccic_device is not present try again\n", __func__);
		return NULL;
	}
	pccic_data = dev_get_drvdata(ccic_device);
	if (!pccic_data) {
		pr_err("%s drv data is null in ccic device\n", __func__);
		return NULL;
	}
	return (pccic_data->drv_data);
}