0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/clk.h>
0009 #include <linux/i2c.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/delay.h>
0012 #include <linux/slab.h>
0013 #include <linux/module.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/platform_data/usb3503.h>
0016 #include <linux/regmap.h>
0017
0018 #define USB3503_VIDL 0x00
0019 #define USB3503_VIDM 0x01
0020 #define USB3503_PIDL 0x02
0021 #define USB3503_PIDM 0x03
0022 #define USB3503_DIDL 0x04
0023 #define USB3503_DIDM 0x05
0024
0025 #define USB3503_CFG1 0x06
0026 #define USB3503_SELF_BUS_PWR (1 << 7)
0027
0028 #define USB3503_CFG2 0x07
0029 #define USB3503_CFG3 0x08
0030 #define USB3503_NRD 0x09
0031
0032 #define USB3503_PDS 0x0a
0033
0034 #define USB3503_SP_ILOCK 0xe7
0035 #define USB3503_SPILOCK_CONNECT (1 << 1)
0036 #define USB3503_SPILOCK_CONFIG (1 << 0)
0037
0038 #define USB3503_CFGP 0xee
0039 #define USB3503_CLKSUSP (1 << 7)
0040
0041 #define USB3503_RESET 0xff
0042
0043 struct usb3503 {
0044 enum usb3503_mode mode;
0045 struct regmap *regmap;
0046 struct device *dev;
0047 struct clk *clk;
0048 u8 port_off_mask;
0049 struct gpio_desc *intn;
0050 struct gpio_desc *reset;
0051 struct gpio_desc *connect;
0052 bool secondary_ref_clk;
0053 };
0054
0055 static int usb3503_reset(struct usb3503 *hub, int state)
0056 {
0057 if (!state && hub->connect)
0058 gpiod_set_value_cansleep(hub->connect, 0);
0059
0060 if (hub->reset)
0061 gpiod_set_value_cansleep(hub->reset, !state);
0062
0063
0064 if (state)
0065 usleep_range(4000, 10000);
0066
0067 return 0;
0068 }
0069
0070 static int usb3503_connect(struct usb3503 *hub)
0071 {
0072 struct device *dev = hub->dev;
0073 int err;
0074
0075 usb3503_reset(hub, 1);
0076
0077 if (hub->regmap) {
0078
0079 err = regmap_write(hub->regmap, USB3503_SP_ILOCK,
0080 (USB3503_SPILOCK_CONNECT
0081 | USB3503_SPILOCK_CONFIG));
0082 if (err < 0) {
0083 dev_err(dev, "SP_ILOCK failed (%d)\n", err);
0084 return err;
0085 }
0086
0087
0088 if (hub->port_off_mask) {
0089 err = regmap_update_bits(hub->regmap, USB3503_PDS,
0090 hub->port_off_mask,
0091 hub->port_off_mask);
0092 if (err < 0) {
0093 dev_err(dev, "PDS failed (%d)\n", err);
0094 return err;
0095 }
0096 }
0097
0098
0099 err = regmap_update_bits(hub->regmap, USB3503_CFG1,
0100 USB3503_SELF_BUS_PWR,
0101 USB3503_SELF_BUS_PWR);
0102 if (err < 0) {
0103 dev_err(dev, "CFG1 failed (%d)\n", err);
0104 return err;
0105 }
0106
0107
0108 err = regmap_update_bits(hub->regmap, USB3503_SP_ILOCK,
0109 (USB3503_SPILOCK_CONNECT
0110 | USB3503_SPILOCK_CONFIG), 0);
0111 if (err < 0) {
0112 dev_err(dev, "SP_ILOCK failed (%d)\n", err);
0113 return err;
0114 }
0115 }
0116
0117 if (hub->connect)
0118 gpiod_set_value_cansleep(hub->connect, 1);
0119
0120 hub->mode = USB3503_MODE_HUB;
0121 dev_info(dev, "switched to HUB mode\n");
0122
0123 return 0;
0124 }
0125
0126 static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
0127 {
0128 struct device *dev = hub->dev;
0129 int err = 0;
0130
0131 switch (mode) {
0132 case USB3503_MODE_HUB:
0133 err = usb3503_connect(hub);
0134 break;
0135
0136 case USB3503_MODE_STANDBY:
0137 usb3503_reset(hub, 0);
0138 dev_info(dev, "switched to STANDBY mode\n");
0139 break;
0140
0141 default:
0142 dev_err(dev, "unknown mode is requested\n");
0143 err = -EINVAL;
0144 break;
0145 }
0146
0147 return err;
0148 }
0149
0150 static const struct regmap_config usb3503_regmap_config = {
0151 .reg_bits = 8,
0152 .val_bits = 8,
0153
0154 .max_register = USB3503_RESET,
0155 };
0156
0157 static int usb3503_probe(struct usb3503 *hub)
0158 {
0159 struct device *dev = hub->dev;
0160 struct usb3503_platform_data *pdata = dev_get_platdata(dev);
0161 struct device_node *np = dev->of_node;
0162 int err;
0163 u32 mode = USB3503_MODE_HUB;
0164 const u32 *property;
0165 enum gpiod_flags flags;
0166 int len;
0167
0168 if (pdata) {
0169 hub->port_off_mask = pdata->port_off_mask;
0170 hub->mode = pdata->initial_mode;
0171 } else if (np) {
0172 u32 rate = 0;
0173 hub->port_off_mask = 0;
0174
0175 if (!of_property_read_u32(np, "refclk-frequency", &rate)) {
0176 switch (rate) {
0177 case 38400000:
0178 case 26000000:
0179 case 19200000:
0180 case 12000000:
0181 hub->secondary_ref_clk = 0;
0182 break;
0183 case 24000000:
0184 case 27000000:
0185 case 25000000:
0186 case 50000000:
0187 hub->secondary_ref_clk = 1;
0188 break;
0189 default:
0190 dev_err(dev,
0191 "unsupported reference clock rate (%d)\n",
0192 (int) rate);
0193 return -EINVAL;
0194 }
0195 }
0196
0197 hub->clk = devm_clk_get_optional(dev, "refclk");
0198 if (IS_ERR(hub->clk)) {
0199 dev_err(dev, "unable to request refclk (%ld)\n",
0200 PTR_ERR(hub->clk));
0201 return PTR_ERR(hub->clk);
0202 }
0203
0204 if (rate != 0) {
0205 err = clk_set_rate(hub->clk, rate);
0206 if (err) {
0207 dev_err(dev,
0208 "unable to set reference clock rate to %d\n",
0209 (int)rate);
0210 return err;
0211 }
0212 }
0213
0214 err = clk_prepare_enable(hub->clk);
0215 if (err) {
0216 dev_err(dev, "unable to enable reference clock\n");
0217 return err;
0218 }
0219
0220 property = of_get_property(np, "disabled-ports", &len);
0221 if (property && (len / sizeof(u32)) > 0) {
0222 int i;
0223 for (i = 0; i < len / sizeof(u32); i++) {
0224 u32 port = be32_to_cpu(property[i]);
0225 if ((1 <= port) && (port <= 3))
0226 hub->port_off_mask |= (1 << port);
0227 }
0228 }
0229
0230 of_property_read_u32(np, "initial-mode", &mode);
0231 hub->mode = mode;
0232 }
0233
0234 if (hub->secondary_ref_clk)
0235 flags = GPIOD_OUT_LOW;
0236 else
0237 flags = GPIOD_OUT_HIGH;
0238 hub->intn = devm_gpiod_get_optional(dev, "intn", flags);
0239 if (IS_ERR(hub->intn))
0240 return PTR_ERR(hub->intn);
0241 if (hub->intn)
0242 gpiod_set_consumer_name(hub->intn, "usb3503 intn");
0243
0244 hub->connect = devm_gpiod_get_optional(dev, "connect", GPIOD_OUT_LOW);
0245 if (IS_ERR(hub->connect))
0246 return PTR_ERR(hub->connect);
0247 if (hub->connect)
0248 gpiod_set_consumer_name(hub->connect, "usb3503 connect");
0249
0250 hub->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
0251 if (IS_ERR(hub->reset))
0252 return PTR_ERR(hub->reset);
0253 if (hub->reset) {
0254
0255 usleep_range(100, 10000);
0256 gpiod_set_consumer_name(hub->reset, "usb3503 reset");
0257 }
0258
0259 if (hub->port_off_mask && !hub->regmap)
0260 dev_err(dev, "Ports disabled with no control interface\n");
0261
0262 usb3503_switch_mode(hub, hub->mode);
0263
0264 dev_info(dev, "%s: probed in %s mode\n", __func__,
0265 (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
0266
0267 return 0;
0268 }
0269
0270 static int usb3503_i2c_probe(struct i2c_client *i2c,
0271 const struct i2c_device_id *id)
0272 {
0273 struct usb3503 *hub;
0274 int err;
0275
0276 hub = devm_kzalloc(&i2c->dev, sizeof(struct usb3503), GFP_KERNEL);
0277 if (!hub)
0278 return -ENOMEM;
0279
0280 i2c_set_clientdata(i2c, hub);
0281 hub->regmap = devm_regmap_init_i2c(i2c, &usb3503_regmap_config);
0282 if (IS_ERR(hub->regmap)) {
0283 err = PTR_ERR(hub->regmap);
0284 dev_err(&i2c->dev, "Failed to initialise regmap: %d\n", err);
0285 return err;
0286 }
0287 hub->dev = &i2c->dev;
0288
0289 return usb3503_probe(hub);
0290 }
0291
0292 static int usb3503_i2c_remove(struct i2c_client *i2c)
0293 {
0294 struct usb3503 *hub;
0295
0296 hub = i2c_get_clientdata(i2c);
0297 clk_disable_unprepare(hub->clk);
0298
0299 return 0;
0300 }
0301
0302 static int usb3503_platform_probe(struct platform_device *pdev)
0303 {
0304 struct usb3503 *hub;
0305
0306 hub = devm_kzalloc(&pdev->dev, sizeof(struct usb3503), GFP_KERNEL);
0307 if (!hub)
0308 return -ENOMEM;
0309 hub->dev = &pdev->dev;
0310 platform_set_drvdata(pdev, hub);
0311
0312 return usb3503_probe(hub);
0313 }
0314
0315 static int usb3503_platform_remove(struct platform_device *pdev)
0316 {
0317 struct usb3503 *hub;
0318
0319 hub = platform_get_drvdata(pdev);
0320 clk_disable_unprepare(hub->clk);
0321
0322 return 0;
0323 }
0324
0325 static int __maybe_unused usb3503_suspend(struct usb3503 *hub)
0326 {
0327 usb3503_switch_mode(hub, USB3503_MODE_STANDBY);
0328 clk_disable_unprepare(hub->clk);
0329
0330 return 0;
0331 }
0332
0333 static int __maybe_unused usb3503_resume(struct usb3503 *hub)
0334 {
0335 clk_prepare_enable(hub->clk);
0336 usb3503_switch_mode(hub, hub->mode);
0337
0338 return 0;
0339 }
0340
0341 static int __maybe_unused usb3503_i2c_suspend(struct device *dev)
0342 {
0343 struct i2c_client *client = to_i2c_client(dev);
0344
0345 return usb3503_suspend(i2c_get_clientdata(client));
0346 }
0347
0348 static int __maybe_unused usb3503_i2c_resume(struct device *dev)
0349 {
0350 struct i2c_client *client = to_i2c_client(dev);
0351
0352 return usb3503_resume(i2c_get_clientdata(client));
0353 }
0354
0355 static int __maybe_unused usb3503_platform_suspend(struct device *dev)
0356 {
0357 return usb3503_suspend(dev_get_drvdata(dev));
0358 }
0359
0360 static int __maybe_unused usb3503_platform_resume(struct device *dev)
0361 {
0362 return usb3503_resume(dev_get_drvdata(dev));
0363 }
0364
0365 static SIMPLE_DEV_PM_OPS(usb3503_i2c_pm_ops, usb3503_i2c_suspend,
0366 usb3503_i2c_resume);
0367
0368 static SIMPLE_DEV_PM_OPS(usb3503_platform_pm_ops, usb3503_platform_suspend,
0369 usb3503_platform_resume);
0370
0371 static const struct i2c_device_id usb3503_id[] = {
0372 { USB3503_I2C_NAME, 0 },
0373 { }
0374 };
0375 MODULE_DEVICE_TABLE(i2c, usb3503_id);
0376
0377 #ifdef CONFIG_OF
0378 static const struct of_device_id usb3503_of_match[] = {
0379 { .compatible = "smsc,usb3503", },
0380 { .compatible = "smsc,usb3503a", },
0381 {},
0382 };
0383 MODULE_DEVICE_TABLE(of, usb3503_of_match);
0384 #endif
0385
0386 static struct i2c_driver usb3503_i2c_driver = {
0387 .driver = {
0388 .name = USB3503_I2C_NAME,
0389 .pm = pm_ptr(&usb3503_i2c_pm_ops),
0390 .of_match_table = of_match_ptr(usb3503_of_match),
0391 },
0392 .probe = usb3503_i2c_probe,
0393 .remove = usb3503_i2c_remove,
0394 .id_table = usb3503_id,
0395 };
0396
0397 static struct platform_driver usb3503_platform_driver = {
0398 .driver = {
0399 .name = USB3503_I2C_NAME,
0400 .of_match_table = of_match_ptr(usb3503_of_match),
0401 .pm = pm_ptr(&usb3503_platform_pm_ops),
0402 },
0403 .probe = usb3503_platform_probe,
0404 .remove = usb3503_platform_remove,
0405 };
0406
0407 static int __init usb3503_init(void)
0408 {
0409 int err;
0410
0411 err = i2c_add_driver(&usb3503_i2c_driver);
0412 if (err) {
0413 pr_err("usb3503: Failed to register I2C driver: %d\n", err);
0414 return err;
0415 }
0416
0417 err = platform_driver_register(&usb3503_platform_driver);
0418 if (err) {
0419 pr_err("usb3503: Failed to register platform driver: %d\n",
0420 err);
0421 i2c_del_driver(&usb3503_i2c_driver);
0422 return err;
0423 }
0424
0425 return 0;
0426 }
0427 module_init(usb3503_init);
0428
0429 static void __exit usb3503_exit(void)
0430 {
0431 platform_driver_unregister(&usb3503_platform_driver);
0432 i2c_del_driver(&usb3503_i2c_driver);
0433 }
0434 module_exit(usb3503_exit);
0435
0436 MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
0437 MODULE_DESCRIPTION("USB3503 USB HUB driver");
0438 MODULE_LICENSE("GPL");