Fishwaldo commented on code in PR #19879:
URL: https://github.com/apache/nuttx/pull/19879#discussion_r3800908050


##########
drivers/power/supply/regulator.c:
##########
@@ -73,6 +80,291 @@ static rmutex_t g_reg_lock         = NXRMUTEX_INITIALIZER;
  * Private Functions
  ****************************************************************************/
 
+#ifdef CONFIG_REGULATOR_PROCFS
+
+/****************************************************************************
+ * Name: regulator_procfs_open
+ *
+ * Description:
+ *   Read only.  A rail is not something to move by writing to a file: the
+ *   consumers hold the knowledge of what a voltage may be, and the ordering
+ *   between them is the whole point of the framework.  Reporting is what
+ *   this entry is for.
+ *
+ * Input Parameters:
+ *   filep   - The file structure to attach the open file to
+ *   relpath - The path below /proc being opened
+ *   oflags  - Open flags; anything but read only is refused
+ *   mode    - Ignored, the entry cannot be created
+ *
+ * Returned Value:
+ *   Zero on success, or a negated errno on failure.
+ *
+ ****************************************************************************/
+
+static int regulator_procfs_open(FAR struct file *filep,
+                                 FAR const char *relpath,
+                                 int oflags, mode_t mode)
+{
+  FAR struct procfs_file_s *priv;
+
+  if ((oflags & O_ACCMODE) != O_RDONLY)
+    {
+      return -EACCES;
+    }
+
+  priv = kmm_zalloc(sizeof(struct procfs_file_s));
+  if (priv == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  filep->f_priv = priv;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: regulator_procfs_close
+ *
+ * Description:
+ *   Close /proc/regulator and free what open() allocated.
+ *
+ * Input Parameters:
+ *   filep - The open file
+ *
+ * Returned Value:
+ *   Zero on success, or a negated errno on failure.
+ *
+ ****************************************************************************/
+
+static int regulator_procfs_close(FAR struct file *filep)
+{
+  kmm_free(filep->f_priv);
+  filep->f_priv = NULL;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: regulator_procfs_read
+ *
+ * Description:
+ *   Describe every registered regulator: what it is called, what it is
+ *   putting out, the range it will accept, whether it is on and how many
+ *   consumers are holding it.
+ *
+ *   The voltage is read from the hardware rather than remembered, so a rail
+ *   moved by something other than this framework, which is the usual state
+ *   of affairs at start up, is reported as it is rather than as this
+ *   software last left it.
+ *
+ *   That is why this takes the list mutex directly rather than calling
+ *   regulator_list_lock(), which also disables interrupts for the benefit
+ *   of callers that may run in interrupt or idle context.  Asking a
+ *   regulator on a bus what it is doing means bus traffic, and bus traffic
+ *   waits; a reader of this file is always a task and can afford to.
+ *
+ * Input Parameters:
+ *   filep  - The open file, carrying the offset reached so far
+ *   buffer - Where to return the text
+ *   buflen - Size of buffer
+ *
+ * Returned Value:
+ *   The number of bytes returned, zero at end of file, or a negated errno
+ *   on failure.
+ *
+ ****************************************************************************/
+
+static ssize_t regulator_procfs_read(FAR struct file *filep,
+                                     FAR char *buffer, size_t buflen)
+{
+  FAR struct regulator_dev_s *rdev;
+  size_t remaining = buflen;
+  FAR char *dest = buffer;
+  off_t pos = filep->f_pos;
+  char line[192];
+  char extra[48];
+  size_t n;
+  int ret;
+
+  ret = nxrmutex_lock(&g_reg_lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  list_for_every_entry(&g_reg_list, rdev, struct regulator_dev_s, list)
+    {
+      FAR const struct regulator_desc_s *desc = rdev->desc;
+      int enabled;
+      int uv;
+
+      if (remaining == 0)
+        {
+          break;
+        }
+
+      n = snprintf(line, sizeof(line), "%-20s",
+                   desc->name != NULL ? desc->name : "-");
+
+      /* Both of these reach the hardware and can fail; a failure reports
+       * - rather than an errno formatted as a voltage, or as a rail that
+       * is switched on.
+       */
+
+      uv = _regulator_get_voltage(rdev);
+      if (uv >= 0)
+        {
+          n += snprintf(line + n, sizeof(line) - n, " uv:%d", uv);
+        }
+      else
+        {
+          n += snprintf(line + n, sizeof(line) - n, " uv:-");
+        }
+
+      n += snprintf(line + n, sizeof(line) - n, " min:%u max:%u",
+                    desc->min_uv, desc->max_uv);
+
+      enabled = _regulator_is_enabled(rdev);
+      if (enabled >= 0)
+        {
+          n += snprintf(line + n, sizeof(line) - n, " enabled:%d",
+                        enabled != 0);
+        }
+      else
+        {
+          n += snprintf(line + n, sizeof(line) - n, " enabled:-");
+        }
+
+      n += snprintf(line + n, sizeof(line) - n,
+                    " users:%" PRIu32 " opens:%" PRIu32
+                    " supply:%s always_on:%u boot_on:%u",
+                    rdev->use_count, rdev->open_count,
+                    desc->supply_name != NULL ? desc->supply_name : "-",
+                    desc->always_on ? 1u : 0u, desc->boot_on ? 1u : 0u);

Review Comment:
   Done. 



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to