2 * Copyright (C) 2009 Samsung Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/platform_device.h>
26 #include <linux/micro_usb_switch.h>
28 struct micro_usb_switch {
29 struct micro_usb_switch_platform_data *pdata;
33 static ssize_t micro_usb_switch_show_state(struct device *dev,
34 struct device_attribute *attr, char *buf)
36 struct micro_usb_switch *usb_switch=
37 platform_get_drvdata(to_platform_device(dev));
38 struct micro_usb_switch_platform_data *pdata = usb_switch->pdata;
42 path = pdata->get_usb_switch();
44 if (path == USB_PATH_AP)
45 ret = sprintf(buf, "AP\n");
46 else if (path == USB_PATH_CP)
47 ret = sprintf(buf, "CP\n");
49 ret = sprintf(buf, "NA\n");
54 static ssize_t micro_usb_switch_store_state(struct device *dev,
55 struct device_attribute *attr, const char *buf, size_t count)
57 struct micro_usb_switch *usb_switch=
58 platform_get_drvdata(to_platform_device(dev));
59 struct micro_usb_switch_platform_data *pdata = usb_switch->pdata;
65 if (!strncmp(buf, "CP", 2))
67 else if (!strncmp(buf, "AP", 2))
72 if (path == usb_switch->path)
75 pdata->set_usb_switch(path);
77 if (pdata->usb_power) {
78 pdata->usb_power(path, USB_PATH_CP);
79 pdata->usb_power(!path, USB_PATH_AP);
82 usb_switch->path = path;
87 static struct device_attribute micro_usb_switch_attr = {
92 .show = micro_usb_switch_show_state,
93 .store = micro_usb_switch_store_state,
96 static int __devinit usb_switch_probe(struct platform_device *pdev)
98 struct micro_usb_switch_platform_data *pdata = pdev->dev.platform_data;
99 struct micro_usb_switch *usb_switch;
102 usb_switch = kzalloc(sizeof(struct micro_usb_switch), GFP_KERNEL);
104 dev_err(&pdev->dev, "failed to allocate driver data\n");
108 platform_set_drvdata(pdev, usb_switch);
109 usb_switch->pdata = pdata;
110 usb_switch->path = pdata->default_path;
112 ret = device_create_file(&pdev->dev, µ_usb_switch_attr);
114 dev_err(&pdev->dev, "failed to crreate device file\n");
121 static int __devexit usb_switch_remove(struct platform_device *pdev)
123 platform_set_drvdata(pdev, NULL);
127 static struct platform_driver usb_switch_driver = {
128 .probe = usb_switch_probe,
129 .remove = __devexit_p(usb_switch_remove),
131 .name = "usb-switch",
132 .owner = THIS_MODULE,
136 static int __init usb_switch_init(void)
138 return platform_driver_register(&usb_switch_driver);
140 module_init(usb_switch_init);
142 static void __exit usb_switch_exit(void)
144 platform_driver_unregister(&usb_switch_driver);
146 module_exit(usb_switch_exit);
148 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
149 MODULE_DESCRIPTION("Micro USB Switch Driver");
150 MODULE_LICENSE("GPL");