aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/uart_select.c
blob: 22d14393b50ce764b91994c7682ccce8b6ff841c (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
/*
 * uart_sel.c - UART Selection Driver
 *
 * Copyright (C) 2009 Samsung Electronics
 * Kim Kyuwon <q1.kim@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/uart_select.h>

struct uart_select {
	struct uart_select_platform_data	*pdata;
	struct rw_semaphore			rwsem;
};

static int uart_saved_state = UART_SW_PATH_NA;

static ssize_t uart_select_show_state(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct uart_select *uart_sel =
			platform_get_drvdata(to_platform_device(dev));
	int ret;
	int path;

	path = uart_sel->pdata->get_uart_switch();

	down_read(&uart_sel->rwsem);
	uart_saved_state = path;
	if (path == UART_SW_PATH_NA)
		ret = sprintf(buf, "NA\n");
	else if (path == UART_SW_PATH_CP)
		ret = sprintf(buf, "CP\n");
	else
		ret = sprintf(buf, "AP\n");
	up_read(&uart_sel->rwsem);

	return ret;
}

static ssize_t uart_select_store_state(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	struct uart_select *uart_sel =
			platform_get_drvdata(to_platform_device(dev));
	struct uart_select_platform_data *pdata = uart_sel->pdata;
	int path;

	if (!count)
		return -EINVAL;

	down_write(&uart_sel->rwsem);
	if (!strncmp(buf, "CP", 2))
		path = UART_SW_PATH_CP;
	else if (!strncmp(buf, "AP", 2))
		path = UART_SW_PATH_AP;
	else {
		up_write(&uart_sel->rwsem);
		dev_err(dev, "Invalid cmd !!\n");
		return -EINVAL;
	}
	pdata->set_uart_switch(path);
	uart_saved_state = path;
	up_write(&uart_sel->rwsem);

	return count;
}

static struct device_attribute uart_select_attr = {
	.attr = {
		.name = "path",
		.mode = 0644,
	},
	.show = uart_select_show_state,
	.store = uart_select_store_state,
};

/* Used in uart isr to avoid triggering sysrq when uart is not in AP */
int uart_sel_get_state(void)
{
	if (uart_saved_state < 0)
		return -EPERM;
	else
		return uart_saved_state;
}
EXPORT_SYMBOL(uart_sel_get_state);

static int __devinit uart_select_probe(struct platform_device *pdev)
{
	struct uart_select *uart_sel;
	struct uart_select_platform_data *pdata = pdev->dev.platform_data;
	int ret;

	uart_sel = kzalloc(sizeof(struct uart_select), GFP_KERNEL);
	if (!uart_sel) {
		dev_err(&pdev->dev, "failed to allocate driver data\n");
		return -ENOMEM;
	}

	platform_set_drvdata(pdev, uart_sel);
	uart_sel->pdata = pdata;
	init_rwsem(&uart_sel->rwsem);

	uart_saved_state = pdata->get_uart_switch();

	ret = device_create_file(&pdev->dev, &uart_select_attr);
	if (ret) {
		dev_err(&pdev->dev, "failed to crreate device file\n");
		return ret;
	}

	return 0;
}

static int __devexit uart_select_remove(struct platform_device *pdev)
{
	device_remove_file(&pdev->dev, &uart_select_attr);
	platform_set_drvdata(pdev, NULL);

	return 0;
}

static struct platform_driver uart_select_driver = {
	.probe		= uart_select_probe,
	.remove		= __devexit_p(uart_select_remove),
	.driver		= {
		.name	= "uart-select",
		.owner	= THIS_MODULE,
	},
};

static int __init uart_select_init(void)
{
	return platform_driver_register(&uart_select_driver);
}
module_init(uart_select_init);

static void __exit uart_select_exit(void)
{
	platform_driver_unregister(&uart_select_driver);
}
module_exit(uart_select_exit);

MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>");
MODULE_DESCRIPTION("UART Selection Driver");
MODULE_LICENSE("GPL v2");