Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * The code contained herein is licensed under the GNU General Public |
| 5 | * License. You may obtain a copy of the GNU General Public License |
| 6 | * Version 2 or later at the following locations: |
| 7 | * |
| 8 | * http://www.opensource.org/licenses/gpl-license.html |
| 9 | * http://www.gnu.org/copyleft/gpl.html |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of_platform.h> |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/io.h> |
| 17 | |
| 18 | #include "ci13xxx_imx.h" |
| 19 | |
| 20 | #define USB_DEV_MAX 4 |
| 21 | |
| 22 | #define BM_OVER_CUR_DIS BIT(7) |
| 23 | |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 24 | struct imx_usbmisc { |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 25 | void __iomem *base; |
| 26 | spinlock_t lock; |
| 27 | struct clk *clk; |
| 28 | struct usbmisc_usb_device usbdev[USB_DEV_MAX]; |
| 29 | }; |
| 30 | |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 31 | static struct imx_usbmisc *usbmisc; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 32 | |
| 33 | static struct usbmisc_usb_device *get_usbdev(struct device *dev) |
| 34 | { |
| 35 | int i, ret; |
| 36 | |
| 37 | for (i = 0; i < USB_DEV_MAX; i++) { |
| 38 | if (usbmisc->usbdev[i].dev == dev) |
| 39 | return &usbmisc->usbdev[i]; |
| 40 | else if (!usbmisc->usbdev[i].dev) |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | if (i >= USB_DEV_MAX) |
| 45 | return ERR_PTR(-EBUSY); |
| 46 | |
| 47 | ret = usbmisc_get_init_data(dev, &usbmisc->usbdev[i]); |
| 48 | if (ret) |
| 49 | return ERR_PTR(ret); |
| 50 | |
| 51 | return &usbmisc->usbdev[i]; |
| 52 | } |
| 53 | |
| 54 | static int usbmisc_imx6q_init(struct device *dev) |
| 55 | { |
| 56 | |
| 57 | struct usbmisc_usb_device *usbdev; |
| 58 | unsigned long flags; |
| 59 | u32 reg; |
| 60 | |
| 61 | usbdev = get_usbdev(dev); |
| 62 | if (IS_ERR(usbdev)) |
| 63 | return PTR_ERR(usbdev); |
| 64 | |
| 65 | if (usbdev->disable_oc) { |
| 66 | spin_lock_irqsave(&usbmisc->lock, flags); |
| 67 | reg = readl(usbmisc->base + usbdev->index * 4); |
| 68 | writel(reg | BM_OVER_CUR_DIS, |
| 69 | usbmisc->base + usbdev->index * 4); |
| 70 | spin_unlock_irqrestore(&usbmisc->lock, flags); |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static const struct usbmisc_ops imx6q_usbmisc_ops = { |
| 77 | .init = usbmisc_imx6q_init, |
| 78 | }; |
| 79 | |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 80 | static const struct of_device_id usbmisc_imx_dt_ids[] = { |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 81 | { .compatible = "fsl,imx6q-usbmisc"}, |
| 82 | { /* sentinel */ } |
| 83 | }; |
| 84 | |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 85 | static int usbmisc_imx_probe(struct platform_device *pdev) |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 86 | { |
| 87 | struct resource *res; |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 88 | struct imx_usbmisc *data; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 89 | int ret; |
| 90 | |
| 91 | if (usbmisc) |
| 92 | return -EBUSY; |
| 93 | |
| 94 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 95 | if (!data) |
| 96 | return -ENOMEM; |
| 97 | |
| 98 | spin_lock_init(&data->lock); |
| 99 | |
| 100 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 148e113 | 2013-01-21 11:09:22 +0100 | [diff] [blame] | 101 | data->base = devm_ioremap_resource(&pdev->dev, res); |
| 102 | if (IS_ERR(data->base)) |
| 103 | return PTR_ERR(data->base); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 104 | |
| 105 | data->clk = devm_clk_get(&pdev->dev, NULL); |
| 106 | if (IS_ERR(data->clk)) { |
| 107 | dev_err(&pdev->dev, |
| 108 | "failed to get clock, err=%ld\n", PTR_ERR(data->clk)); |
| 109 | return PTR_ERR(data->clk); |
| 110 | } |
| 111 | |
| 112 | ret = clk_prepare_enable(data->clk); |
| 113 | if (ret) { |
| 114 | dev_err(&pdev->dev, |
| 115 | "clk_prepare_enable failed, err=%d\n", ret); |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | ret = usbmisc_set_ops(&imx6q_usbmisc_ops); |
| 120 | if (ret) { |
| 121 | clk_disable_unprepare(data->clk); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | usbmisc = data; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 130 | static int usbmisc_imx_remove(struct platform_device *pdev) |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 131 | { |
| 132 | usbmisc_unset_ops(&imx6q_usbmisc_ops); |
| 133 | clk_disable_unprepare(usbmisc->clk); |
| 134 | return 0; |
| 135 | } |
| 136 | |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 137 | static struct platform_driver usbmisc_imx_driver = { |
| 138 | .probe = usbmisc_imx_probe, |
| 139 | .remove = usbmisc_imx_remove, |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 140 | .driver = { |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 141 | .name = "usbmisc_imx", |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 142 | .owner = THIS_MODULE, |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 143 | .of_match_table = usbmisc_imx_dt_ids, |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 144 | }, |
| 145 | }; |
| 146 | |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 147 | int usbmisc_imx_drv_init(void) |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 148 | { |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 149 | return platform_driver_register(&usbmisc_imx_driver); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 150 | } |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 151 | subsys_initcall(usbmisc_imx_drv_init); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 152 | |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 153 | void usbmisc_imx_drv_exit(void) |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 154 | { |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 155 | platform_driver_unregister(&usbmisc_imx_driver); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 156 | } |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 157 | module_exit(usbmisc_imx_drv_exit); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 158 | |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 159 | MODULE_ALIAS("platform:usbmisc-imx"); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 160 | MODULE_LICENSE("GPL v2"); |
Michael Grzeschik | a7bc2fd | 2013-03-30 12:53:56 +0200 | [diff] [blame^] | 161 | MODULE_DESCRIPTION("driver for imx usb non-core registers"); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 162 | MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>"); |