| .. | .. |
|---|
| 7 | 7 | |
|---|
| 8 | 8 | #include <linux/clk.h> |
|---|
| 9 | 9 | #include <linux/i2c.h> |
|---|
| 10 | | -#include <linux/gpio.h> |
|---|
| 10 | +#include <linux/gpio/consumer.h> |
|---|
| 11 | 11 | #include <linux/delay.h> |
|---|
| 12 | 12 | #include <linux/slab.h> |
|---|
| 13 | 13 | #include <linux/module.h> |
|---|
| 14 | | -#include <linux/of_gpio.h> |
|---|
| 15 | 14 | #include <linux/platform_device.h> |
|---|
| 16 | 15 | #include <linux/platform_data/usb3503.h> |
|---|
| 17 | 16 | #include <linux/regmap.h> |
|---|
| .. | .. |
|---|
| 47 | 46 | struct device *dev; |
|---|
| 48 | 47 | struct clk *clk; |
|---|
| 49 | 48 | u8 port_off_mask; |
|---|
| 50 | | - int gpio_intn; |
|---|
| 51 | | - int gpio_reset; |
|---|
| 52 | | - int gpio_connect; |
|---|
| 49 | + struct gpio_desc *intn; |
|---|
| 50 | + struct gpio_desc *reset; |
|---|
| 51 | + struct gpio_desc *connect; |
|---|
| 53 | 52 | bool secondary_ref_clk; |
|---|
| 54 | 53 | }; |
|---|
| 55 | 54 | |
|---|
| 56 | 55 | static int usb3503_reset(struct usb3503 *hub, int state) |
|---|
| 57 | 56 | { |
|---|
| 58 | | - if (!state && gpio_is_valid(hub->gpio_connect)) |
|---|
| 59 | | - gpio_set_value_cansleep(hub->gpio_connect, 0); |
|---|
| 57 | + if (!state && hub->connect) |
|---|
| 58 | + gpiod_set_value_cansleep(hub->connect, 0); |
|---|
| 60 | 59 | |
|---|
| 61 | | - if (gpio_is_valid(hub->gpio_reset)) |
|---|
| 62 | | - gpio_set_value_cansleep(hub->gpio_reset, state); |
|---|
| 60 | + if (hub->reset) |
|---|
| 61 | + gpiod_set_value_cansleep(hub->reset, !state); |
|---|
| 63 | 62 | |
|---|
| 64 | 63 | /* Wait T_HUBINIT == 4ms for hub logic to stabilize */ |
|---|
| 65 | 64 | if (state) |
|---|
| .. | .. |
|---|
| 115 | 114 | } |
|---|
| 116 | 115 | } |
|---|
| 117 | 116 | |
|---|
| 118 | | - if (gpio_is_valid(hub->gpio_connect)) |
|---|
| 119 | | - gpio_set_value_cansleep(hub->gpio_connect, 1); |
|---|
| 117 | + if (hub->connect) |
|---|
| 118 | + gpiod_set_value_cansleep(hub->connect, 1); |
|---|
| 120 | 119 | |
|---|
| 121 | 120 | hub->mode = USB3503_MODE_HUB; |
|---|
| 122 | 121 | dev_info(dev, "switched to HUB mode\n"); |
|---|
| .. | .. |
|---|
| 163 | 162 | int err; |
|---|
| 164 | 163 | u32 mode = USB3503_MODE_HUB; |
|---|
| 165 | 164 | const u32 *property; |
|---|
| 165 | + enum gpiod_flags flags; |
|---|
| 166 | 166 | int len; |
|---|
| 167 | 167 | |
|---|
| 168 | 168 | if (pdata) { |
|---|
| 169 | 169 | hub->port_off_mask = pdata->port_off_mask; |
|---|
| 170 | | - hub->gpio_intn = pdata->gpio_intn; |
|---|
| 171 | | - hub->gpio_connect = pdata->gpio_connect; |
|---|
| 172 | | - hub->gpio_reset = pdata->gpio_reset; |
|---|
| 173 | 170 | hub->mode = pdata->initial_mode; |
|---|
| 174 | 171 | } else if (np) { |
|---|
| 175 | | - struct clk *clk; |
|---|
| 176 | 172 | u32 rate = 0; |
|---|
| 177 | 173 | hub->port_off_mask = 0; |
|---|
| 178 | 174 | |
|---|
| .. | .. |
|---|
| 198 | 194 | } |
|---|
| 199 | 195 | } |
|---|
| 200 | 196 | |
|---|
| 201 | | - clk = devm_clk_get(dev, "refclk"); |
|---|
| 202 | | - if (IS_ERR(clk) && PTR_ERR(clk) != -ENOENT) { |
|---|
| 197 | + hub->clk = devm_clk_get_optional(dev, "refclk"); |
|---|
| 198 | + if (IS_ERR(hub->clk)) { |
|---|
| 203 | 199 | dev_err(dev, "unable to request refclk (%ld)\n", |
|---|
| 204 | | - PTR_ERR(clk)); |
|---|
| 205 | | - return PTR_ERR(clk); |
|---|
| 200 | + PTR_ERR(hub->clk)); |
|---|
| 201 | + return PTR_ERR(hub->clk); |
|---|
| 206 | 202 | } |
|---|
| 207 | 203 | |
|---|
| 208 | | - if (!IS_ERR(clk)) { |
|---|
| 209 | | - hub->clk = clk; |
|---|
| 210 | | - |
|---|
| 211 | | - if (rate != 0) { |
|---|
| 212 | | - err = clk_set_rate(hub->clk, rate); |
|---|
| 213 | | - if (err) { |
|---|
| 214 | | - dev_err(dev, |
|---|
| 215 | | - "unable to set reference clock rate to %d\n", |
|---|
| 216 | | - (int) rate); |
|---|
| 217 | | - return err; |
|---|
| 218 | | - } |
|---|
| 219 | | - } |
|---|
| 220 | | - |
|---|
| 221 | | - err = clk_prepare_enable(hub->clk); |
|---|
| 204 | + if (rate != 0) { |
|---|
| 205 | + err = clk_set_rate(hub->clk, rate); |
|---|
| 222 | 206 | if (err) { |
|---|
| 223 | 207 | dev_err(dev, |
|---|
| 224 | | - "unable to enable reference clock\n"); |
|---|
| 208 | + "unable to set reference clock rate to %d\n", |
|---|
| 209 | + (int)rate); |
|---|
| 225 | 210 | return err; |
|---|
| 226 | 211 | } |
|---|
| 212 | + } |
|---|
| 213 | + |
|---|
| 214 | + err = clk_prepare_enable(hub->clk); |
|---|
| 215 | + if (err) { |
|---|
| 216 | + dev_err(dev, "unable to enable reference clock\n"); |
|---|
| 217 | + return err; |
|---|
| 227 | 218 | } |
|---|
| 228 | 219 | |
|---|
| 229 | 220 | property = of_get_property(np, "disabled-ports", &len); |
|---|
| .. | .. |
|---|
| 236 | 227 | } |
|---|
| 237 | 228 | } |
|---|
| 238 | 229 | |
|---|
| 239 | | - hub->gpio_intn = of_get_named_gpio(np, "intn-gpios", 0); |
|---|
| 240 | | - if (hub->gpio_intn == -EPROBE_DEFER) |
|---|
| 241 | | - return -EPROBE_DEFER; |
|---|
| 242 | | - hub->gpio_connect = of_get_named_gpio(np, "connect-gpios", 0); |
|---|
| 243 | | - if (hub->gpio_connect == -EPROBE_DEFER) |
|---|
| 244 | | - return -EPROBE_DEFER; |
|---|
| 245 | | - hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0); |
|---|
| 246 | | - if (hub->gpio_reset == -EPROBE_DEFER) |
|---|
| 247 | | - return -EPROBE_DEFER; |
|---|
| 248 | 230 | of_property_read_u32(np, "initial-mode", &mode); |
|---|
| 249 | 231 | hub->mode = mode; |
|---|
| 250 | 232 | } |
|---|
| 251 | 233 | |
|---|
| 252 | | - if (hub->port_off_mask && !hub->regmap) |
|---|
| 253 | | - dev_err(dev, "Ports disabled with no control interface\n"); |
|---|
| 234 | + if (hub->secondary_ref_clk) |
|---|
| 235 | + flags = GPIOD_OUT_LOW; |
|---|
| 236 | + else |
|---|
| 237 | + flags = GPIOD_OUT_HIGH; |
|---|
| 238 | + hub->intn = devm_gpiod_get_optional(dev, "intn", flags); |
|---|
| 239 | + if (IS_ERR(hub->intn)) |
|---|
| 240 | + return PTR_ERR(hub->intn); |
|---|
| 241 | + if (hub->intn) |
|---|
| 242 | + gpiod_set_consumer_name(hub->intn, "usb3503 intn"); |
|---|
| 254 | 243 | |
|---|
| 255 | | - if (gpio_is_valid(hub->gpio_intn)) { |
|---|
| 256 | | - int val = hub->secondary_ref_clk ? GPIOF_OUT_INIT_LOW : |
|---|
| 257 | | - GPIOF_OUT_INIT_HIGH; |
|---|
| 258 | | - err = devm_gpio_request_one(dev, hub->gpio_intn, val, |
|---|
| 259 | | - "usb3503 intn"); |
|---|
| 260 | | - if (err) { |
|---|
| 261 | | - dev_err(dev, |
|---|
| 262 | | - "unable to request GPIO %d as interrupt pin (%d)\n", |
|---|
| 263 | | - hub->gpio_intn, err); |
|---|
| 264 | | - return err; |
|---|
| 265 | | - } |
|---|
| 266 | | - } |
|---|
| 244 | + hub->connect = devm_gpiod_get_optional(dev, "connect", GPIOD_OUT_LOW); |
|---|
| 245 | + if (IS_ERR(hub->connect)) |
|---|
| 246 | + return PTR_ERR(hub->connect); |
|---|
| 247 | + if (hub->connect) |
|---|
| 248 | + gpiod_set_consumer_name(hub->connect, "usb3503 connect"); |
|---|
| 267 | 249 | |
|---|
| 268 | | - if (gpio_is_valid(hub->gpio_connect)) { |
|---|
| 269 | | - err = devm_gpio_request_one(dev, hub->gpio_connect, |
|---|
| 270 | | - GPIOF_OUT_INIT_LOW, "usb3503 connect"); |
|---|
| 271 | | - if (err) { |
|---|
| 272 | | - dev_err(dev, |
|---|
| 273 | | - "unable to request GPIO %d as connect pin (%d)\n", |
|---|
| 274 | | - hub->gpio_connect, err); |
|---|
| 275 | | - return err; |
|---|
| 276 | | - } |
|---|
| 277 | | - } |
|---|
| 278 | | - |
|---|
| 279 | | - if (gpio_is_valid(hub->gpio_reset)) { |
|---|
| 280 | | - err = devm_gpio_request_one(dev, hub->gpio_reset, |
|---|
| 281 | | - GPIOF_OUT_INIT_LOW, "usb3503 reset"); |
|---|
| 250 | + hub->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); |
|---|
| 251 | + if (IS_ERR(hub->reset)) |
|---|
| 252 | + return PTR_ERR(hub->reset); |
|---|
| 253 | + if (hub->reset) { |
|---|
| 282 | 254 | /* Datasheet defines a hardware reset to be at least 100us */ |
|---|
| 283 | 255 | usleep_range(100, 10000); |
|---|
| 284 | | - if (err) { |
|---|
| 285 | | - dev_err(dev, |
|---|
| 286 | | - "unable to request GPIO %d as reset pin (%d)\n", |
|---|
| 287 | | - hub->gpio_reset, err); |
|---|
| 288 | | - return err; |
|---|
| 289 | | - } |
|---|
| 256 | + gpiod_set_consumer_name(hub->reset, "usb3503 reset"); |
|---|
| 290 | 257 | } |
|---|
| 258 | + |
|---|
| 259 | + if (hub->port_off_mask && !hub->regmap) |
|---|
| 260 | + dev_err(dev, "Ports disabled with no control interface\n"); |
|---|
| 291 | 261 | |
|---|
| 292 | 262 | usb3503_switch_mode(hub, hub->mode); |
|---|
| 293 | 263 | |
|---|
| .. | .. |
|---|
| 324 | 294 | struct usb3503 *hub; |
|---|
| 325 | 295 | |
|---|
| 326 | 296 | hub = i2c_get_clientdata(i2c); |
|---|
| 327 | | - if (hub->clk) |
|---|
| 328 | | - clk_disable_unprepare(hub->clk); |
|---|
| 297 | + clk_disable_unprepare(hub->clk); |
|---|
| 329 | 298 | |
|---|
| 330 | 299 | return 0; |
|---|
| 331 | 300 | } |
|---|
| .. | .. |
|---|
| 348 | 317 | struct usb3503 *hub; |
|---|
| 349 | 318 | |
|---|
| 350 | 319 | hub = platform_get_drvdata(pdev); |
|---|
| 351 | | - if (hub->clk) |
|---|
| 352 | | - clk_disable_unprepare(hub->clk); |
|---|
| 320 | + clk_disable_unprepare(hub->clk); |
|---|
| 353 | 321 | |
|---|
| 354 | 322 | return 0; |
|---|
| 355 | 323 | } |
|---|
| 356 | 324 | |
|---|
| 357 | | -#ifdef CONFIG_PM_SLEEP |
|---|
| 358 | | -static int usb3503_i2c_suspend(struct device *dev) |
|---|
| 325 | +static int __maybe_unused usb3503_suspend(struct usb3503 *hub) |
|---|
| 359 | 326 | { |
|---|
| 360 | | - struct i2c_client *client = to_i2c_client(dev); |
|---|
| 361 | | - struct usb3503 *hub = i2c_get_clientdata(client); |
|---|
| 362 | | - |
|---|
| 363 | 327 | usb3503_switch_mode(hub, USB3503_MODE_STANDBY); |
|---|
| 364 | | - |
|---|
| 365 | | - if (hub->clk) |
|---|
| 366 | | - clk_disable_unprepare(hub->clk); |
|---|
| 328 | + clk_disable_unprepare(hub->clk); |
|---|
| 367 | 329 | |
|---|
| 368 | 330 | return 0; |
|---|
| 369 | 331 | } |
|---|
| 370 | 332 | |
|---|
| 371 | | -static int usb3503_i2c_resume(struct device *dev) |
|---|
| 333 | +static int __maybe_unused usb3503_resume(struct usb3503 *hub) |
|---|
| 372 | 334 | { |
|---|
| 373 | | - struct i2c_client *client = to_i2c_client(dev); |
|---|
| 374 | | - struct usb3503 *hub = i2c_get_clientdata(client); |
|---|
| 375 | | - |
|---|
| 376 | | - if (hub->clk) |
|---|
| 377 | | - clk_prepare_enable(hub->clk); |
|---|
| 378 | | - |
|---|
| 335 | + clk_prepare_enable(hub->clk); |
|---|
| 379 | 336 | usb3503_switch_mode(hub, hub->mode); |
|---|
| 380 | 337 | |
|---|
| 381 | 338 | return 0; |
|---|
| 382 | 339 | } |
|---|
| 383 | | -#endif |
|---|
| 340 | + |
|---|
| 341 | +static int __maybe_unused usb3503_i2c_suspend(struct device *dev) |
|---|
| 342 | +{ |
|---|
| 343 | + struct i2c_client *client = to_i2c_client(dev); |
|---|
| 344 | + |
|---|
| 345 | + return usb3503_suspend(i2c_get_clientdata(client)); |
|---|
| 346 | +} |
|---|
| 347 | + |
|---|
| 348 | +static int __maybe_unused usb3503_i2c_resume(struct device *dev) |
|---|
| 349 | +{ |
|---|
| 350 | + struct i2c_client *client = to_i2c_client(dev); |
|---|
| 351 | + |
|---|
| 352 | + return usb3503_resume(i2c_get_clientdata(client)); |
|---|
| 353 | +} |
|---|
| 354 | + |
|---|
| 355 | +static int __maybe_unused usb3503_platform_suspend(struct device *dev) |
|---|
| 356 | +{ |
|---|
| 357 | + return usb3503_suspend(dev_get_drvdata(dev)); |
|---|
| 358 | +} |
|---|
| 359 | + |
|---|
| 360 | +static int __maybe_unused usb3503_platform_resume(struct device *dev) |
|---|
| 361 | +{ |
|---|
| 362 | + return usb3503_resume(dev_get_drvdata(dev)); |
|---|
| 363 | +} |
|---|
| 384 | 364 | |
|---|
| 385 | 365 | static SIMPLE_DEV_PM_OPS(usb3503_i2c_pm_ops, usb3503_i2c_suspend, |
|---|
| 386 | 366 | usb3503_i2c_resume); |
|---|
| 367 | + |
|---|
| 368 | +static SIMPLE_DEV_PM_OPS(usb3503_platform_pm_ops, usb3503_platform_suspend, |
|---|
| 369 | + usb3503_platform_resume); |
|---|
| 387 | 370 | |
|---|
| 388 | 371 | static const struct i2c_device_id usb3503_id[] = { |
|---|
| 389 | 372 | { USB3503_I2C_NAME, 0 }, |
|---|
| .. | .. |
|---|
| 403 | 386 | static struct i2c_driver usb3503_i2c_driver = { |
|---|
| 404 | 387 | .driver = { |
|---|
| 405 | 388 | .name = USB3503_I2C_NAME, |
|---|
| 406 | | - .pm = &usb3503_i2c_pm_ops, |
|---|
| 389 | + .pm = pm_ptr(&usb3503_i2c_pm_ops), |
|---|
| 407 | 390 | .of_match_table = of_match_ptr(usb3503_of_match), |
|---|
| 408 | 391 | }, |
|---|
| 409 | 392 | .probe = usb3503_i2c_probe, |
|---|
| .. | .. |
|---|
| 415 | 398 | .driver = { |
|---|
| 416 | 399 | .name = USB3503_I2C_NAME, |
|---|
| 417 | 400 | .of_match_table = of_match_ptr(usb3503_of_match), |
|---|
| 401 | + .pm = pm_ptr(&usb3503_platform_pm_ops), |
|---|
| 418 | 402 | }, |
|---|
| 419 | 403 | .probe = usb3503_platform_probe, |
|---|
| 420 | 404 | .remove = usb3503_platform_remove, |
|---|