acassis commented on code in PR #14895:
URL: https://github.com/apache/nuttx/pull/14895#discussion_r1852523495


##########
drivers/usbmisc/Kconfig:
##########
@@ -88,4 +88,32 @@ config FUSB303_NPOLLWAITERS
 
 endif
 
+config STUSB4500
+       bool "ST standalone USB PD sink controller"
+       default n
+       select I2C
+       ---help---
+               Enable device driver for ST standalone USB PD sink controller

Review Comment:
   ```suggestion
                Enable device driver for ST standalone USB PD (power delivery) 
sink controller that addresses sink up to 100 W (20 V; 5 A).



##########
drivers/usbmisc/stusb4500.c:
##########
@@ -0,0 +1,614 @@
+/****************************************************************************
+ * drivers/usbmisc/stusb4500.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <poll.h>
+#include <debug.h>
+#include <stdio.h>
+
+#include <nuttx/compiler.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mutex.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#include <nuttx/usb/stusb4500.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_DEBUG_STUSB4500
+  #define stusb4500_err(x, ...)        _err(x, ##__VA_ARGS__)
+  #define stusb4500_info(x, ...)       _info(x, ##__VA_ARGS__)
+#else
+  #define stusb4500_err(x, ...)        uerr(x, ##__VA_ARGS__)
+  #define stusb4500_info(x, ...)       uinfo(x, ##__VA_ARGS__)
+#endif
+
+#ifndef CONFIG_STUSB4500_I2C_FREQUENCY
+  #define CONFIG_STUSB4500_I2C_FREQUENCY 400000
+#endif
+
+/* Other macros */
+
+#define STUSB4500_I2C_RETRIES  10
+
+/* Debug */
+
+#ifdef CONFIG_DEBUG_STUSB4500
+
+#endif
+
+/* Registers */
+#define  STUSB4500_REG_BCD_TYPEC_REV_LOW         0x06
+#define  STUSB4500_REG_BCD_TYPEC_REV_HIGH        0x07
+#define  STUSB4500_REG_BCD_USBPD_REV_LOW         0x08
+#define  STUSB4500_REG_BCD_USBPD_REV_HIGH        0x09
+#define  STUSB4500_REG_DEVICE_CAPAB_HIGH         0x0A
+#define  STUSB4500_REG_ALERT_STATUS_1            0x0B
+#define  STUSB4500_REG_ALERT_STATUS_1_MASK       0x0C
+#define  STUSB4500_REG_PORT_STATUS_0             0x0D
+#define  STUSB4500_REG_PORT_STATUS_1             0x0E
+#define  STUSB4500_REG_TYPEC_MONITORING_STATUS_0 0x0F
+#define  STUSB4500_REG_TYPEC_MONITORING_STATUS_1 0x10
+#define  STUSB4500_REG_CC_STATUS                 0x11
+#define  STUSB4500_REG_CC_HW_FAULT_STATUS_0      0x12
+#define  STUSB4500_REG_CC_HW_FAULT_STATUS_1      0x13
+#define  STUSB4500_REG_PD_TYPEC_STATUS           0x14
+#define  STUSB4500_REG_TYPEC_STATUS              0x15
+#define  STUSB4500_REG_PRT_STATUS                0x16
+#define  STUSB4500_REG_PD_COMMAND_CTRL           0x1A
+#define  STUSB4500_REG_MONITORING_CTRL_0         0x20
+#define  STUSB4500_REG_MONITORING_CTRL_2         0x22
+#define  STUSB4500_REG_RESET_CTRL                0x23
+#define  STUSB4500_REG_VBUS_DISCHARGE_TIME_CTRL  0x25
+#define  STUSB4500_REG_VBUS_DISCHARGE_CTRL       0x26
+#define  STUSB4500_REG_VBUS_CTRL                 0x27
+#define  STUSB4500_REG_PE_FSM                    0x29
+#define  STUSB4500_REG_GPIO_SW_GPIO              0x2D
+#define  STUSB4500_REG_DEVICE_ID                 0x2F
+#define  STUSB4500_REG_RX_HEADER_LOW             0x31
+#define  STUSB4500_REG_RX_HEADER_HIGH            0x32
+#define  STUSB4500_REG_RX_DATA_OBJ1_0            0x33
+#define  STUSB4500_REG_RX_DATA_OBJ1_1            0x34
+#define  STUSB4500_REG_RX_DATA_OBJ1_2            0x35
+#define  STUSB4500_REG_RX_DATA_OBJ1_3            0x36
+#define  STUSB4500_REG_RX_DATA_OBJ2_0            0x37
+#define  STUSB4500_REG_RX_DATA_OBJ2_1            0x38
+#define  STUSB4500_REG_RX_DATA_OBJ2_2            0x39
+#define  STUSB4500_REG_RX_DATA_OBJ2_3            0x3A
+#define  STUSB4500_REG_RX_DATA_OBJ3_0            0x3B
+#define  STUSB4500_REG_RX_DATA_OBJ3_1            0x3C
+#define  STUSB4500_REG_RX_DATA_OBJ3_2            0x3D
+#define  STUSB4500_REG_RX_DATA_OBJ3_3            0x3E
+#define  STUSB4500_REG_RX_DATA_OBJ4_0            0x3F
+#define  STUSB4500_REG_RX_DATA_OBJ4_1            0x40
+#define  STUSB4500_REG_RX_DATA_OBJ4_2            0x41
+#define  STUSB4500_REG_RX_DATA_OBJ4_3            0x42
+#define  STUSB4500_REG_RX_DATA_OBJ5_0            0x43
+#define  STUSB4500_REG_RX_DATA_OBJ5_1            0x44
+#define  STUSB4500_REG_RX_DATA_OBJ5_2            0x45
+#define  STUSB4500_REG_RX_DATA_OBJ5_3            0x46
+#define  STUSB4500_REG_RX_DATA_OBJ6_0            0x47
+#define  STUSB4500_REG_RX_DATA_OBJ6_1            0x48
+#define  STUSB4500_REG_RX_DATA_OBJ6_2            0x49
+#define  STUSB4500_REG_RX_DATA_OBJ6_3            0x4A
+#define  STUSB4500_REG_RX_DATA_OBJ7_0            0x4B
+#define  STUSB4500_REG_RX_DATA_OBJ7_1            0x4C
+#define  STUSB4500_REG_RX_DATA_OBJ7_2            0x4D
+#define  STUSB4500_REG_RX_DATA_OBJ7_3            0x4E
+#define  STUSB4500_REG_TX_HEADER_LOW             0x51
+#define  STUSB4500_REG_TX_HEADER_HIGH            0x52
+#define  STUSB4500_REG_DPM_PDO_NUMB              0x70
+#define  STUSB4500_REG_DPM_SNK_PDO1_0            0x85
+#define  STUSB4500_REG_DPM_SNK_PDO1_1            0x86
+#define  STUSB4500_REG_DPM_SNK_PDO1_2            0x87
+#define  STUSB4500_REG_DPM_SNK_PDO1_3            0x88
+#define  STUSB4500_REG_DPM_SNK_PDO2_0            0x89
+#define  STUSB4500_REG_DPM_SNK_PDO2_1            0x8A
+#define  STUSB4500_REG_DPM_SNK_PDO2_2            0x8B
+#define  STUSB4500_REG_DPM_SNK_PDO2_3            0x8C
+#define  STUSB4500_REG_DPM_SNK_PDO3_0            0x8D
+#define  STUSB4500_REG_DPM_SNK_PDO3_1            0x8E
+#define  STUSB4500_REG_DPM_SNK_PDO3_2            0x8F
+#define  STUSB4500_REG_DPM_SNK_PDO3_3            0x90
+#define  STUSB4500_REG_RDO_REG_STATUS_0          0x91
+#define  STUSB4500_REG_RDO_REG_STATUS_1          0x92
+#define  STUSB4500_REG_RDO_REG_STATUS_2          0x93
+#define  STUSB4500_REG_RDO_REG_STATUS_3          0x94
+
+/****************************************************************************
+ * Private Data Types
+ ****************************************************************************/
+
+struct stusb4500_dev_s
+{
+  FAR struct i2c_master_s *i2c;          /* I2C interface */
+  uint8_t addr;                          /* I2C address */
+  mutex_t devlock;                       /* Manages exclusive access */
+};
+
+/****************************************************************************
+ * Private Function prototypes
+ ****************************************************************************/
+
+#ifdef CONFIG_DEBUG_STUSB4500
+
+#endif
+static int stusb4500_open(FAR struct file *filep);
+static int stusb4500_close(FAR struct file *filep);
+static ssize_t stusb4500_read(FAR struct file *, FAR char *, size_t);
+static ssize_t stusb4500_write(FAR struct file *filep,
+                               FAR const char *buffer, size_t buflen);
+static int stusb4500_ioctl(FAR struct file *filep, int cmd,
+                           unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations g_stusb4500ops =
+{
+  stusb4500_open,  /* open */
+  stusb4500_close, /* close */
+  stusb4500_read,  /* read */
+  stusb4500_write, /* write */
+  NULL,            /* seek */
+  stusb4500_ioctl, /* ioctl */
+  NULL,            /* mmap */
+  NULL,            /* truncate */
+  NULL             /* poll */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stusb4500_getreg
+ *
+ * Description:
+ *   Read from an 8-bit STUSB4500 register
+ *
+ * Input Parameters:
+ *   priv   - pointer to STUSB4500 Private Structure
+ *   reg    - register to read
+ *   rxbuf  - pointer to receive data buffer
+ *   len    - receive data length
+ *
+ * Returned Value:
+ *   Returns positive register value in case of success, otherwise ERROR
+ *
+ ****************************************************************************/
+
+static int stusb4500_getreg(FAR struct stusb4500_dev_s *priv, uint8_t reg,
+                            uint8_t *rxbuf, uint8_t len)
+{
+  int ret = -EIO;
+  int retries;
+  struct i2c_msg_s msg[2];
+
+  DEBUGASSERT(priv);
+
+  msg[0].frequency = CONFIG_STUSB4500_I2C_FREQUENCY;
+  msg[0].addr      = priv->addr;
+  msg[0].flags     = I2C_M_NOSTOP;
+  msg[0].buffer    = &reg;
+  msg[0].length    = 1;
+
+  msg[1].frequency = CONFIG_STUSB4500_I2C_FREQUENCY;
+  msg[1].addr      = priv->addr;
+  msg[1].flags     = I2C_M_READ;
+  msg[1].buffer    = rxbuf;
+  msg[1].length    = len;
+
+  /* Perform the transfer */
+
+  for (retries = 0; retries < STUSB4500_I2C_RETRIES; retries++)
+    {
+      ret = I2C_TRANSFER(priv->i2c, msg, 2);
+      if (ret >= 0)
+        {
+#define BUFFER_SIZE 128

Review Comment:
   Please move this macro to the right section of this file (top) or to 
stusb4500.h, also use STUSB4500_BUFFER_SIZE to avoid name collision



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to