Skip to content

Commit 6782311

Browse files
Radhey Shyam Pandeygregkh
Radhey Shyam Pandey
authored andcommitted
usb: misc: onboard_usb_dev: add Microchip usb5744 SMBus programming support
usb5744 supports SMBus Configuration and it may be configured via the SMBus slave interface during the hub start-up configuration stage. To program it driver uses i2c-bus phandle (added in commit '02be19e914b8 dt-bindings: usb: Add support for Microchip usb5744 hub controller') to get i2c client device and then based on usb5744 compatible check calls usb5744 i2c default initialization sequence. Apart from the USB command attach, prevent the hub from suspend. when the USB Attach with SMBus (0xAA56) command is issued to the hub, the hub is getting enumerated and then it puts in a suspend mode. This causes the hub to NAK any SMBus access made by the SMBus Master during this period and not able to see the hub's slave address while running the "i2c probe" command. Prevent the MCU from putting the HUB in suspend mode through register write. The BYPASS_UDC_SUSPEND bit (Bit 3) of the RuntimeFlags2 register at address 0x411D controls this aspect of the hub. The BYPASS_UDC_SUSPEND bit in register 0x411Dh must be set to ensure that the MCU is always enabled and ready to respond to SMBus runtime commands. This register needs to be written before the USB attach command is issued. The byte sequence is as follows: Slave addr: 0x2d 00 00 05 00 01 41 1D 08 Slave addr: 0x2d 99 37 00 Slave addr: 0x2d AA 56 00 Also since usb5744 i2c initialization routine uses i2c SMBus APIs invoke these APIs only when i2c driver is enabled in the kernel configuration. Signed-off-by: Radhey Shyam Pandey <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 908f61b commit 6782311

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

drivers/usb/misc/onboard_usb_dev.c

+77
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/err.h>
1212
#include <linux/gpio/consumer.h>
1313
#include <linux/init.h>
14+
#include <linux/i2c.h>
1415
#include <linux/kernel.h>
1516
#include <linux/list.h>
1617
#include <linux/module.h>
@@ -29,6 +30,17 @@
2930

3031
#include "onboard_usb_dev.h"
3132

33+
/* USB5744 register offset and mask */
34+
#define USB5744_CMD_ATTACH 0xAA
35+
#define USB5744_CMD_ATTACH_LSB 0x56
36+
#define USB5744_CMD_CREG_ACCESS 0x99
37+
#define USB5744_CMD_CREG_ACCESS_LSB 0x37
38+
#define USB5744_CREG_MEM_ADDR 0x00
39+
#define USB5744_CREG_WRITE 0x00
40+
#define USB5744_CREG_RUNTIMEFLAGS2 0x41
41+
#define USB5744_CREG_RUNTIMEFLAGS2_LSB 0x1D
42+
#define USB5744_CREG_BYPASS_UDC_SUSPEND BIT(3)
43+
3244
static void onboard_dev_attach_usb_driver(struct work_struct *work);
3345

3446
static struct usb_device_driver onboard_dev_usbdev_driver;
@@ -297,10 +309,50 @@ static void onboard_dev_attach_usb_driver(struct work_struct *work)
297309
pr_err("Failed to attach USB driver: %pe\n", ERR_PTR(err));
298310
}
299311

312+
static int onboard_dev_5744_i2c_init(struct i2c_client *client)
313+
{
314+
#if IS_ENABLED(CONFIG_I2C)
315+
struct device *dev = &client->dev;
316+
int ret;
317+
318+
/*
319+
* Set BYPASS_UDC_SUSPEND bit to ensure MCU is always enabled
320+
* and ready to respond to SMBus runtime commands.
321+
* The command writes 5 bytes to memory and single data byte in
322+
* configuration register.
323+
*/
324+
char wr_buf[7] = {USB5744_CREG_MEM_ADDR, 5,
325+
USB5744_CREG_WRITE, 1,
326+
USB5744_CREG_RUNTIMEFLAGS2,
327+
USB5744_CREG_RUNTIMEFLAGS2_LSB,
328+
USB5744_CREG_BYPASS_UDC_SUSPEND};
329+
330+
ret = i2c_smbus_write_block_data(client, 0, sizeof(wr_buf), wr_buf);
331+
if (ret)
332+
return dev_err_probe(dev, ret, "BYPASS_UDC_SUSPEND bit configuration failed\n");
333+
334+
ret = i2c_smbus_write_word_data(client, USB5744_CMD_CREG_ACCESS,
335+
USB5744_CMD_CREG_ACCESS_LSB);
336+
if (ret)
337+
return dev_err_probe(dev, ret, "Configuration Register Access Command failed\n");
338+
339+
/* Send SMBus command to boot hub. */
340+
ret = i2c_smbus_write_word_data(client, USB5744_CMD_ATTACH,
341+
USB5744_CMD_ATTACH_LSB);
342+
if (ret < 0)
343+
return dev_err_probe(dev, ret, "USB Attach with SMBus command failed\n");
344+
345+
return ret;
346+
#else
347+
return -ENODEV;
348+
#endif
349+
}
350+
300351
static int onboard_dev_probe(struct platform_device *pdev)
301352
{
302353
struct device *dev = &pdev->dev;
303354
struct onboard_dev *onboard_dev;
355+
struct device_node *i2c_node;
304356
int err;
305357

306358
onboard_dev = devm_kzalloc(dev, sizeof(*onboard_dev), GFP_KERNEL);
@@ -340,6 +392,27 @@ static int onboard_dev_probe(struct platform_device *pdev)
340392
if (err)
341393
return err;
342394

395+
i2c_node = of_parse_phandle(pdev->dev.of_node, "i2c-bus", 0);
396+
if (i2c_node) {
397+
struct i2c_client *client;
398+
399+
client = of_find_i2c_device_by_node(i2c_node);
400+
of_node_put(i2c_node);
401+
402+
if (!client) {
403+
err = -EPROBE_DEFER;
404+
goto err_power_off;
405+
}
406+
407+
if (of_device_is_compatible(pdev->dev.of_node, "usb424,2744") ||
408+
of_device_is_compatible(pdev->dev.of_node, "usb424,5744"))
409+
err = onboard_dev_5744_i2c_init(client);
410+
411+
put_device(&client->dev);
412+
if (err < 0)
413+
goto err_power_off;
414+
}
415+
343416
/*
344417
* The USB driver might have been detached from the USB devices by
345418
* onboard_dev_remove() (e.g. through an 'unbind' by userspace),
@@ -351,6 +424,10 @@ static int onboard_dev_probe(struct platform_device *pdev)
351424
schedule_work(&attach_usb_driver_work);
352425

353426
return 0;
427+
428+
err_power_off:
429+
onboard_dev_power_off(onboard_dev);
430+
return err;
354431
}
355432

356433
static void onboard_dev_remove(struct platform_device *pdev)

0 commit comments

Comments
 (0)