summaryrefstree
path: root/kernel/drivers/sti/abc/abc_hub_cond.c
blob: 8af50a033215ec3f1a8c4fa73bfc34a61f8db9dd (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
/* abc_hub_cond.c
 *
 * Abnormal Behavior Catcher Hub Driver Sub Module Cond(Detecting Connection)
 *
 * Copyright (C) 2017 Samsung Electronics
 *
 * 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/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/err.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/suspend.h>
#include <linux/of_gpio.h>
#include <linux/sec_class.h>
#include <linux/sti/abc_hub.h>

#define SEC_CONN_PRINT(format, ...) \
	pr_info("[ABC_COND] " format, ##__VA_ARGS__)
#define ABCEVENT_CONN_MAX_DEV_STRING 120

/* This value is used for checking gpio irq is enabled or not. */
static int detect_conn_enabled;

/**
 * Parse the device tree and get gpio number, irq type.
 * And then request gpios.
 */
int parse_cond_data(struct device *dev,
		    struct abc_hub_platform_data *pdata,
		    struct device_node *np)
{
	int i;
	int gpio_num = 0;
#if IS_ENABLED(CONFIG_ARCH_QCOM)
	struct pinctrl *conn_pinctrl;
	struct pinctrl *pm_conn_pinctrl;
#endif

	SEC_CONN_PRINT("ABC_HUB_COND DT Parsing start.");

	pdata->cond.gpio_cnt = of_gpio_named_count(np, "sec,det_conn_gpios");
	SEC_CONN_PRINT("gpio cnt = : %d\n", pdata->cond.gpio_cnt);

	/* if support gpio cnt less than 1 set value to 0 */
	if (pdata->cond.gpio_cnt <= 0) {
		SEC_CONN_PRINT("of_gpio_named_count = 0.\n");
		pdata->cond.gpio_cnt = 0;
	}

	pdata->cond.gpio_total_cnt = pdata->cond.gpio_cnt;

#if IS_ENABLED(CONFIG_ARCH_QCOM)
	/* Setting pinctrl state to NO PULL */
	conn_pinctrl = devm_pinctrl_get_select(dev, "det_ap_connect");
	if (IS_ERR_OR_NULL(conn_pinctrl))
		SEC_CONN_PRINT("setting no pull failed.\n");
#endif
	for (i = 0; i < pdata->cond.gpio_cnt; i++) {
		/*Get connector name*/
		of_property_read_string_index(np, "sec,det_conn_name", i,
					      &pdata->cond.name[i]);

		/*Get connector gpio number*/
		gpio_num = of_get_named_gpio(np, "sec,det_conn_gpios", i);
		pdata->cond.irq_gpio[i] = gpio_num;
		SEC_CONN_PRINT("index[%d] : gpio number / name: %d / %s\n",
			       i, gpio_num, pdata->cond.name[i]);

		if (!gpio_is_valid(gpio_num)) {
			dev_err(dev, "%s: Failed to get irq gpio\n", __func__);
			return -EINVAL;
		}
		/*Filling the irq_num from this gpio.*/
		pdata->cond.irq_num[i] = gpio_to_irq(gpio_num);
		pdata->cond.irq_type[i] = IRQ_TYPE_EDGE_BOTH;
		SEC_CONN_PRINT("i = [%d], gpio level [%d] = %d\n",
			       i, gpio_num, gpio_get_value(gpio_num));
		SEC_CONN_PRINT("gpio irq gpio = [%d], irq = [%d]\n",
			       gpio_num, pdata->cond.irq_type[i]);
	}

#if IS_ENABLED(CONFIG_ARCH_QCOM)
	/* Get pm_gpio */
	pdata->cond.gpio_pm_cnt = of_gpio_named_count(np,
						      "sec,det_pm_conn_gpios");
	SEC_CONN_PRINT("pm gpio cnt = : %d\n", pdata->cond.gpio_pm_cnt);

	if (pdata->cond.gpio_pm_cnt <= 0) {
		SEC_CONN_PRINT("pm of_gpio_named_count = 0.\n");
		return 0;
	}

	/* Setting pinctrl state to NO PULL */
	pm_conn_pinctrl = devm_pinctrl_get_select(dev, "det_pm_connect");
	if (IS_ERR_OR_NULL(pm_conn_pinctrl)) {
		SEC_CONN_PRINT("pm setting no pull failed.\n");
		return -ENODEV;
	}

	pdata->cond.gpio_total_cnt = pdata->cond.gpio_total_cnt +
				     pdata->cond.gpio_pm_cnt;
	SEC_CONN_PRINT("gpio_total_count = %d", pdata->cond.gpio_total_cnt);

	for (i = pdata->cond.gpio_cnt; i < pdata->cond.gpio_total_cnt; i++) {
		/*Get connector name*/
		of_property_read_string_index(np, "sec,det_pm_conn_name",
					      i - pdata->cond.gpio_cnt,
					      &pdata->cond.name[i]);

		/* Get connector gpio number */
		gpio_num = of_get_named_gpio(np, "sec,det_pm_conn_gpios",
					     i - pdata->cond.gpio_cnt);
		pdata->cond.irq_gpio[i] = gpio_num;
		SEC_CONN_PRINT("index[%d] : pm gpio number / name: %d / %s\n",
			       i, gpio_num, pdata->cond.name[i]);
		if (!gpio_is_valid(gpio_num)) {
			SEC_CONN_PRINT("pm gpio[%d] invalid\n",
				       i - pdata->cond.gpio_cnt);
			return -EINVAL;
		}
		/*Filling the irq_num from this gpio.*/
		pdata->cond.irq_num[i] = gpio_to_irq(gpio_num);
		pdata->cond.irq_type[i] = IRQ_TYPE_EDGE_BOTH;
		SEC_CONN_PRINT("i = [%d], gpio level [%d] = %d\n",
			       i, gpio_num, gpio_get_value(gpio_num));
		SEC_CONN_PRINT("gpio irq gpio = [%d], irq = [%d]\n",
			       gpio_num, pdata->cond.irq_type[i]);
	}
#endif
	SEC_CONN_PRINT("ABC_HUB_COND DT Parsing done.");

	return 0;
}

/**
 * Send an ABC_event about given gpio pin number.
 */
void send_ABC_event_by_num(int num, struct abc_hub_info *pinfo, int level)
{
	char ABC_event_dev_str[ABCEVENT_CONN_MAX_DEV_STRING];

	/*Send ABC Event Data*/
	if (level == 1) {
		snprintf(ABC_event_dev_str, ABCEVENT_CONN_MAX_DEV_STRING,
			 "MODULE=cond@ERROR=%s", pinfo->pdata->cond.name[num]);
		abc_hub_send_event(ABC_event_dev_str);

		SEC_CONN_PRINT("send ABC pin[%d]:CONNECTOR_NAME=%s,TYPE=%d\n",
			       num, pinfo->pdata->cond.name[num], level);
	} else if (level == 0)
		SEC_CONN_PRINT("send ABC pin[%d]:CONNECTOR_NAME=%s,TYPE=%d\n",
			       num, pinfo->pdata->cond.name[num], level);
}

/**
 * Check and send ABC_event from irq handler.
 */
void check_and_send_ABC_event_irq(int irq, struct abc_hub_info *pinfo,
				  int type)
{
	int i;

	/*Send ABCEvent Data*/
	for (i = 0; i < pinfo->pdata->cond.gpio_total_cnt; i++) {
		if (irq != pinfo->pdata->cond.irq_num[i])
			continue;
		/* if edge interrupt occurs, check level one more.*/
		/* and if it still level high send ABC_event.*/
		usleep_range(1 * 1000, 1 * 1000);
		if (gpio_get_value(pinfo->pdata->cond.irq_gpio[i]))
			send_ABC_event_by_num(i, pinfo, 1);

		SEC_CONN_PRINT("%s status changed.\n",
			       pinfo->pdata->cond.name[i]);
	}
}

/**
 * This is IRQ handler function which is
 * called when the connector pin state change.
 */
static irqreturn_t detect_conn_interrupt_handler(int irq, void *handle)
{
	int type;
	struct abc_hub_info *pinfo = handle;

	if (detect_conn_enabled != 0) {
		SEC_CONN_PRINT("%s\n", __func__);

		type = irq_get_trigger_type(irq);
		check_and_send_ABC_event_irq(irq, pinfo, type);
	}

	return IRQ_HANDLED;
}

/**
 * Enable all gpio pin IRQs which is from Device Tree.
 */
int abc_detect_conn_irq_enable(struct abc_hub_info *pinfo, bool enable,
			       int pin)
{
	int ret = 0;
	int i;

	if (!enable) {
		for (i = 0; i < pinfo->pdata->cond.gpio_total_cnt; i++) {
			if (pinfo->pdata->cond.irq_enabled[i]) {
				disable_irq(pinfo->pdata->cond.irq_num[i]);
				free_irq(pinfo->pdata->cond.irq_num[i], pinfo);
			}
		}
		detect_conn_enabled = 0;
		return ret;
	}

	if (pin >= pinfo->pdata->cond.gpio_total_cnt)
		return ret;

	ret = request_threaded_irq(pinfo->pdata->cond.irq_num[pin], NULL,
				   detect_conn_interrupt_handler,
				   pinfo->pdata->cond.irq_type[pin] |
				   IRQF_ONESHOT,
				   pinfo->pdata->cond.name[pin], pinfo);

	if (ret) {
		SEC_CONN_PRINT("%s: Failed to request threaded irq %d.\n",
			       __func__, ret);

		return ret;
	}

	SEC_CONN_PRINT("%s: Succeeded to request threaded irq %d:\n",
		       __func__, ret);
	SEC_CONN_PRINT("irq_num[%d], type[%x],name[%s].\n",
		       pinfo->pdata->cond.irq_num[pin],
		       pinfo->pdata->cond.irq_type[pin],
		       pinfo->pdata->cond.name[pin]);

	pinfo->pdata->cond.irq_enabled[pin] = true;
	return ret;
}

/**
 * Enable abc hub cond driver.
 * Firstly, check gpio level for all pins and send abc event if value is true.
 * Secondly, enable edge irq so that it can send abc event while drivers is on.
 */
void abc_hub_cond_enable(struct device *dev, int enable)
{
	/* common sequence */
	struct abc_hub_info *pinfo = dev_get_drvdata(dev);
	struct abc_hub_platform_data *pdata = pinfo->pdata;
	int ret;
	int i;

	pdata->cond.enabled = enable;

	/* implement enable sequence */
	if (enable != ABC_HUB_ENABLED) {
		SEC_CONN_PRINT("ABC_HUB_COND_DISANABLED.");
		/*Disable interrupt.*/
		ret = abc_detect_conn_irq_enable(pinfo, false, 0);
		return;
	}
	SEC_CONN_PRINT("ABC_HUB_COND_ENABLED.");
	for (i = 0; i < pdata->cond.gpio_total_cnt; i++) {
		if ((detect_conn_enabled & (1 << i)))
			continue;
		detect_conn_enabled |= (1 << i);

		SEC_CONN_PRINT("gpio level [%d] = %d\n",
			       pdata->cond.irq_gpio[i],
			       gpio_get_value(pdata->cond.irq_gpio[i]));
		/*get level value of the gpio pin.*/
		/*if there's gpio low pin, send ABC_event*/
		if (gpio_get_value(pdata->cond.irq_gpio[i]))
			send_ABC_event_by_num(i, pinfo, 1);
		else
			send_ABC_event_by_num(i, pinfo, 0);

		/*Enable interrupt.*/
		ret = abc_detect_conn_irq_enable(pinfo, true, i);

		if (ret < 0) {
			SEC_CONN_PRINT("gpio[%d] Interrupt not enabled.\n",
				       pdata->cond.irq_gpio[i]);
			return;
		}
	}
}

int abc_hub_cond_init(struct device *dev)
{
	/* implement init sequence : return 0 if success, return -1 if fail */
	// struct abc_hub_info *pinfo = dev_get_drvdata(dev);

	return 0;
}

int abc_hub_cond_suspend(struct device *dev)
{
	/* implement suspend sequence */

	return 0;
}

int abc_hub_cond_resume(struct device *dev)
{
	/* implement resume sequence */

	return 0;
}

MODULE_DESCRIPTION("Samsung ABC Hub Sub Module1(cond) Driver");
MODULE_AUTHOR("Samsung Electronics");
MODULE_LICENSE("GPL");