0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/i2c.h>
0011 #include <linux/delay.h>
0012 #include <linux/slab.h>
0013 #include <linux/module.h>
0014 #include <linux/gpio/consumer.h>
0015
0016 enum usb4604_mode {
0017 USB4604_MODE_UNKNOWN,
0018 USB4604_MODE_HUB,
0019 USB4604_MODE_STANDBY,
0020 };
0021
0022 struct usb4604 {
0023 enum usb4604_mode mode;
0024 struct device *dev;
0025 struct gpio_desc *gpio_reset;
0026 };
0027
0028 static void usb4604_reset(struct usb4604 *hub, int state)
0029 {
0030 gpiod_set_value_cansleep(hub->gpio_reset, state);
0031
0032
0033 if (state)
0034 msleep(250);
0035 }
0036
0037 static int usb4604_connect(struct usb4604 *hub)
0038 {
0039 struct device *dev = hub->dev;
0040 struct i2c_client *client = to_i2c_client(dev);
0041 int err;
0042 u8 connect_cmd[] = { 0xaa, 0x55, 0x00 };
0043
0044 usb4604_reset(hub, 1);
0045
0046 err = i2c_master_send(client, connect_cmd, ARRAY_SIZE(connect_cmd));
0047 if (err < 0) {
0048 usb4604_reset(hub, 0);
0049 return err;
0050 }
0051
0052 hub->mode = USB4604_MODE_HUB;
0053 dev_dbg(dev, "switched to HUB mode\n");
0054
0055 return 0;
0056 }
0057
0058 static int usb4604_switch_mode(struct usb4604 *hub, enum usb4604_mode mode)
0059 {
0060 struct device *dev = hub->dev;
0061 int err = 0;
0062
0063 switch (mode) {
0064 case USB4604_MODE_HUB:
0065 err = usb4604_connect(hub);
0066 break;
0067
0068 case USB4604_MODE_STANDBY:
0069 usb4604_reset(hub, 0);
0070 dev_dbg(dev, "switched to STANDBY mode\n");
0071 break;
0072
0073 default:
0074 dev_err(dev, "unknown mode is requested\n");
0075 err = -EINVAL;
0076 break;
0077 }
0078
0079 return err;
0080 }
0081
0082 static int usb4604_probe(struct usb4604 *hub)
0083 {
0084 struct device *dev = hub->dev;
0085 struct device_node *np = dev->of_node;
0086 struct gpio_desc *gpio;
0087 u32 mode = USB4604_MODE_HUB;
0088
0089 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
0090 if (IS_ERR(gpio))
0091 return PTR_ERR(gpio);
0092 hub->gpio_reset = gpio;
0093
0094 if (of_property_read_u32(np, "initial-mode", &hub->mode))
0095 hub->mode = mode;
0096
0097 return usb4604_switch_mode(hub, hub->mode);
0098 }
0099
0100 static int usb4604_i2c_probe(struct i2c_client *i2c,
0101 const struct i2c_device_id *id)
0102 {
0103 struct usb4604 *hub;
0104
0105 hub = devm_kzalloc(&i2c->dev, sizeof(*hub), GFP_KERNEL);
0106 if (!hub)
0107 return -ENOMEM;
0108
0109 i2c_set_clientdata(i2c, hub);
0110 hub->dev = &i2c->dev;
0111
0112 return usb4604_probe(hub);
0113 }
0114
0115 static int __maybe_unused usb4604_i2c_suspend(struct device *dev)
0116 {
0117 struct i2c_client *client = to_i2c_client(dev);
0118 struct usb4604 *hub = i2c_get_clientdata(client);
0119
0120 usb4604_switch_mode(hub, USB4604_MODE_STANDBY);
0121
0122 return 0;
0123 }
0124
0125 static int __maybe_unused usb4604_i2c_resume(struct device *dev)
0126 {
0127 struct i2c_client *client = to_i2c_client(dev);
0128 struct usb4604 *hub = i2c_get_clientdata(client);
0129
0130 usb4604_switch_mode(hub, hub->mode);
0131
0132 return 0;
0133 }
0134
0135 static SIMPLE_DEV_PM_OPS(usb4604_i2c_pm_ops, usb4604_i2c_suspend,
0136 usb4604_i2c_resume);
0137
0138 static const struct i2c_device_id usb4604_id[] = {
0139 { "usb4604", 0 },
0140 { }
0141 };
0142 MODULE_DEVICE_TABLE(i2c, usb4604_id);
0143
0144 #ifdef CONFIG_OF
0145 static const struct of_device_id usb4604_of_match[] = {
0146 { .compatible = "smsc,usb4604" },
0147 {}
0148 };
0149 MODULE_DEVICE_TABLE(of, usb4604_of_match);
0150 #endif
0151
0152 static struct i2c_driver usb4604_i2c_driver = {
0153 .driver = {
0154 .name = "usb4604",
0155 .pm = pm_ptr(&usb4604_i2c_pm_ops),
0156 .of_match_table = of_match_ptr(usb4604_of_match),
0157 },
0158 .probe = usb4604_i2c_probe,
0159 .id_table = usb4604_id,
0160 };
0161 module_i2c_driver(usb4604_i2c_driver);
0162
0163 MODULE_DESCRIPTION("USB4604 USB HUB driver");
0164 MODULE_LICENSE("GPL v2");