Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Freescale Semiconductor, Inc. |
| 3 | * Copyright (C) 2012 Marek Vasut <marex@denx.de> |
| 4 | * on behalf of DENX Software Engineering GmbH |
| 5 | * |
| 6 | * The code contained herein is licensed under the GNU General Public |
| 7 | * License. You may obtain a copy of the GNU General Public License |
| 8 | * Version 2 or later at the following locations: |
| 9 | * |
| 10 | * http://www.opensource.org/licenses/gpl-license.html |
| 11 | * http://www.gnu.org/copyleft/gpl.html |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of_platform.h> |
| 16 | #include <linux/of_gpio.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/pm_runtime.h> |
| 19 | #include <linux/dma-mapping.h> |
| 20 | #include <linux/usb/chipidea.h> |
| 21 | #include <linux/clk.h> |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 22 | |
| 23 | #include "ci.h" |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 24 | #include "ci_hdrc_imx.h" |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 25 | |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame^] | 26 | #define CI_HDRC_IMX_IMX28_WRITE_FIX BIT(0) |
| 27 | |
| 28 | struct ci_hdrc_imx_platform_flag { |
| 29 | unsigned int flags; |
| 30 | }; |
| 31 | |
| 32 | static const struct ci_hdrc_imx_platform_flag imx27_usb_data = { |
| 33 | }; |
| 34 | |
| 35 | static const struct ci_hdrc_imx_platform_flag imx28_usb_data = { |
| 36 | .flags = CI_HDRC_IMX_IMX28_WRITE_FIX, |
| 37 | }; |
| 38 | |
| 39 | static const struct of_device_id ci_hdrc_imx_dt_ids[] = { |
| 40 | { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data}, |
| 41 | { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data}, |
| 42 | { /* sentinel */ } |
| 43 | }; |
| 44 | MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids); |
| 45 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 46 | struct ci_hdrc_imx_data { |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 47 | struct usb_phy *phy; |
| 48 | struct platform_device *ci_pdev; |
| 49 | struct clk *clk; |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 50 | struct imx_usbmisc_data *usbmisc_data; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 51 | }; |
| 52 | |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 53 | /* Common functions shared by usbmisc drivers */ |
| 54 | |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 55 | static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev) |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 56 | { |
| 57 | struct device_node *np = dev->of_node; |
| 58 | struct of_phandle_args args; |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 59 | struct imx_usbmisc_data *data; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 60 | int ret; |
| 61 | |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 62 | /* |
| 63 | * In case the fsl,usbmisc property is not present this device doesn't |
| 64 | * need usbmisc. Return NULL (which is no error here) |
| 65 | */ |
| 66 | if (!of_get_property(np, "fsl,usbmisc", NULL)) |
| 67 | return NULL; |
| 68 | |
| 69 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 70 | if (!data) |
| 71 | return ERR_PTR(-ENOMEM); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 72 | |
| 73 | ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells", |
| 74 | 0, &args); |
| 75 | if (ret) { |
| 76 | dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n", |
| 77 | ret); |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 78 | return ERR_PTR(ret); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 79 | } |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 80 | |
| 81 | data->index = args.args[0]; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 82 | of_node_put(args.np); |
| 83 | |
| 84 | if (of_find_property(np, "disable-over-current", NULL)) |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 85 | data->disable_oc = 1; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 86 | |
Michael Grzeschik | a068533 | 2013-03-30 12:54:01 +0200 | [diff] [blame] | 87 | if (of_find_property(np, "external-vbus-divider", NULL)) |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 88 | data->evdo = 1; |
Michael Grzeschik | a068533 | 2013-03-30 12:54:01 +0200 | [diff] [blame] | 89 | |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 90 | return data; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 91 | } |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 92 | |
| 93 | /* End of common functions shared by usbmisc drivers*/ |
| 94 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 95 | static int ci_hdrc_imx_probe(struct platform_device *pdev) |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 96 | { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 97 | struct ci_hdrc_imx_data *data; |
| 98 | struct ci_hdrc_platform_data pdata = { |
| 99 | .name = "ci_hdrc_imx", |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 100 | .capoffset = DEF_CAPOFFSET, |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 101 | .flags = CI_HDRC_REQUIRE_TRANSCEIVER | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 102 | CI_HDRC_DISABLE_STREAMING, |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 103 | }; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 104 | int ret; |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame^] | 105 | const struct of_device_id *of_id = |
| 106 | of_match_device(ci_hdrc_imx_dt_ids, &pdev->dev); |
| 107 | const struct ci_hdrc_imx_platform_flag *imx_platform_flag = of_id->data; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 108 | |
| 109 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 110 | if (!data) { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 111 | dev_err(&pdev->dev, "Failed to allocate ci_hdrc-imx data!\n"); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 112 | return -ENOMEM; |
| 113 | } |
| 114 | |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 115 | data->usbmisc_data = usbmisc_get_init_data(&pdev->dev); |
| 116 | if (IS_ERR(data->usbmisc_data)) |
| 117 | return PTR_ERR(data->usbmisc_data); |
| 118 | |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 119 | data->clk = devm_clk_get(&pdev->dev, NULL); |
| 120 | if (IS_ERR(data->clk)) { |
| 121 | dev_err(&pdev->dev, |
| 122 | "Failed to get clock, err=%ld\n", PTR_ERR(data->clk)); |
| 123 | return PTR_ERR(data->clk); |
| 124 | } |
| 125 | |
| 126 | ret = clk_prepare_enable(data->clk); |
| 127 | if (ret) { |
| 128 | dev_err(&pdev->dev, |
| 129 | "Failed to prepare or enable clock, err=%d\n", ret); |
| 130 | return ret; |
| 131 | } |
| 132 | |
Fabio Estevam | 046916d | 2013-06-25 12:58:05 +0300 | [diff] [blame] | 133 | data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0); |
Peter Chen | af59a8b | 2013-09-24 12:47:54 +0800 | [diff] [blame] | 134 | if (IS_ERR(data->phy)) { |
| 135 | ret = PTR_ERR(data->phy); |
Sascha Hauer | ea1418b | 2013-06-13 18:00:00 +0300 | [diff] [blame] | 136 | goto err_clk; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 137 | } |
| 138 | |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 139 | pdata.phy = data->phy; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 140 | |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame^] | 141 | if (imx_platform_flag->flags & CI_HDRC_IMX_IMX28_WRITE_FIX) |
| 142 | pdata.flags |= CI_HDRC_IMX28_WRITE_FIX; |
| 143 | |
Russell King | e1fd734 | 2013-06-27 12:36:37 +0100 | [diff] [blame] | 144 | ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
Russell King | 22d9d8e | 2013-06-10 16:28:49 +0100 | [diff] [blame] | 145 | if (ret) |
| 146 | goto err_clk; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 147 | |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 148 | if (data->usbmisc_data) { |
| 149 | ret = imx_usbmisc_init(data->usbmisc_data); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 150 | if (ret) { |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 151 | dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n", |
| 152 | ret); |
Peter Chen | af59a8b | 2013-09-24 12:47:54 +0800 | [diff] [blame] | 153 | goto err_clk; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 157 | data->ci_pdev = ci_hdrc_add_device(&pdev->dev, |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 158 | pdev->resource, pdev->num_resources, |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 159 | &pdata); |
Fabio Estevam | 770719d | 2013-06-13 17:59:48 +0300 | [diff] [blame] | 160 | if (IS_ERR(data->ci_pdev)) { |
| 161 | ret = PTR_ERR(data->ci_pdev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 162 | dev_err(&pdev->dev, |
| 163 | "Can't register ci_hdrc platform device, err=%d\n", |
| 164 | ret); |
Peter Chen | af59a8b | 2013-09-24 12:47:54 +0800 | [diff] [blame] | 165 | goto err_clk; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 166 | } |
| 167 | |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 168 | if (data->usbmisc_data) { |
| 169 | ret = imx_usbmisc_init_post(data->usbmisc_data); |
Michael Grzeschik | a068533 | 2013-03-30 12:54:01 +0200 | [diff] [blame] | 170 | if (ret) { |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 171 | dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n", |
| 172 | ret); |
Fabio Estevam | 770719d | 2013-06-13 17:59:48 +0300 | [diff] [blame] | 173 | goto disable_device; |
Michael Grzeschik | a068533 | 2013-03-30 12:54:01 +0200 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 177 | platform_set_drvdata(pdev, data); |
| 178 | |
| 179 | pm_runtime_no_callbacks(&pdev->dev); |
| 180 | pm_runtime_enable(&pdev->dev); |
| 181 | |
| 182 | return 0; |
| 183 | |
Fabio Estevam | 770719d | 2013-06-13 17:59:48 +0300 | [diff] [blame] | 184 | disable_device: |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 185 | ci_hdrc_remove_device(data->ci_pdev); |
Sascha Hauer | ea1418b | 2013-06-13 18:00:00 +0300 | [diff] [blame] | 186 | err_clk: |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 187 | clk_disable_unprepare(data->clk); |
| 188 | return ret; |
| 189 | } |
| 190 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 191 | static int ci_hdrc_imx_remove(struct platform_device *pdev) |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 192 | { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 193 | struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 194 | |
| 195 | pm_runtime_disable(&pdev->dev); |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 196 | ci_hdrc_remove_device(data->ci_pdev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 197 | clk_disable_unprepare(data->clk); |
| 198 | |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 202 | static struct platform_driver ci_hdrc_imx_driver = { |
| 203 | .probe = ci_hdrc_imx_probe, |
| 204 | .remove = ci_hdrc_imx_remove, |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 205 | .driver = { |
| 206 | .name = "imx_usb", |
| 207 | .owner = THIS_MODULE, |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 208 | .of_match_table = ci_hdrc_imx_dt_ids, |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 209 | }, |
| 210 | }; |
| 211 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 212 | module_platform_driver(ci_hdrc_imx_driver); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 213 | |
| 214 | MODULE_ALIAS("platform:imx-usb"); |
| 215 | MODULE_LICENSE("GPL v2"); |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 216 | MODULE_DESCRIPTION("CI HDRC i.MX USB binding"); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 217 | MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); |
| 218 | MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>"); |