2017-09-11 01:43:20 +08:00
|
|
|
/****************************************************************************
|
2022-08-01 17:31:49 +08:00
|
|
|
* drivers/power/supply/powerled.c
|
2017-09-11 01:43:20 +08:00
|
|
|
*
|
2020-10-10 20:28:05 +08:00
|
|
|
* 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
|
2017-09-11 01:43:20 +08:00
|
|
|
*
|
2020-10-10 20:28:05 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-09-11 01:43:20 +08:00
|
|
|
*
|
2020-10-10 20:28:05 +08:00
|
|
|
* 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.
|
2017-09-11 01:43:20 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-04 14:10:42 +08:00
|
|
|
/* Upper-half, character driver for high power LED driver. */
|
|
|
|
|
2017-09-11 01:43:20 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2021-05-18 14:59:14 +08:00
|
|
|
#include <assert.h>
|
2017-09-11 01:43:20 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/power/powerled.h>
|
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-12 18:16:13 +08:00
|
|
|
static int powerled_open(FAR struct file *filep);
|
|
|
|
static int powerled_close(FAR struct file *filep);
|
|
|
|
static int powerled_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
2017-09-11 01:43:20 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-22 22:43:07 +08:00
|
|
|
static const struct file_operations g_powerled_fops =
|
2017-09-11 01:43:20 +08:00
|
|
|
{
|
|
|
|
powerled_open, /* open */
|
|
|
|
powerled_close, /* close */
|
|
|
|
NULL, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 08:57:54 +08:00
|
|
|
powerled_ioctl, /* ioctl */
|
2017-09-11 01:43:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: powerled_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called whenever the POWERLED device is opened.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int powerled_open(FAR struct file *filep)
|
|
|
|
{
|
2017-10-05 05:22:27 +08:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct powerled_dev_s *dev = inode->i_private;
|
|
|
|
uint8_t tmp;
|
|
|
|
int ret;
|
2017-09-11 01:43:20 +08:00
|
|
|
|
|
|
|
/* If the port is the middle of closing, wait until the close is finished */
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
ret = nxmutex_lock(&dev->closelock);
|
2017-10-05 05:22:27 +08:00
|
|
|
if (ret >= 0)
|
2017-09-11 01:43:20 +08:00
|
|
|
{
|
|
|
|
/* Increment the count of references to the device. If this the first
|
2020-10-10 21:43:46 +08:00
|
|
|
* time that the driver has been opened for this device, then
|
|
|
|
* initialize the device.
|
2017-09-11 01:43:20 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
tmp = dev->ocount + 1;
|
|
|
|
if (tmp == 0)
|
|
|
|
{
|
|
|
|
/* More than 255 opens; uint8_t overflows to zero */
|
|
|
|
|
|
|
|
ret = -EMFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-10 21:43:46 +08:00
|
|
|
/* Check if this is the first time that the driver has been
|
|
|
|
* opened.
|
|
|
|
*/
|
2017-09-11 01:43:20 +08:00
|
|
|
|
|
|
|
if (tmp == 1)
|
|
|
|
{
|
|
|
|
/* Yes.. perform one time hardware initialization. */
|
|
|
|
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
ret = dev->ops->setup(dev);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Save the new open count on success */
|
|
|
|
|
|
|
|
dev->ocount = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_unlock(&dev->closelock);
|
2017-09-11 01:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: powerled_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This routine is called when the POWERLED device is closed.
|
|
|
|
* It waits for the last remaining data to be sent.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int powerled_close(FAR struct file *filep)
|
|
|
|
{
|
2017-10-05 05:22:27 +08:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct powerled_dev_s *dev = inode->i_private;
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
2017-09-11 01:43:20 +08:00
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
ret = nxmutex_lock(&dev->closelock);
|
2017-10-05 05:22:27 +08:00
|
|
|
if (ret >= 0)
|
2017-09-11 01:43:20 +08:00
|
|
|
{
|
|
|
|
/* Decrement the references to the driver. If the reference count will
|
|
|
|
* decrement to 0, then uninitialize the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->ocount > 1)
|
|
|
|
{
|
|
|
|
dev->ocount--;
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_unlock(&dev->closelock);
|
2017-09-11 01:43:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* There are no more references to the port */
|
|
|
|
|
|
|
|
dev->ocount = 0;
|
|
|
|
|
|
|
|
/* Free the IRQ and disable the POWERLED device */
|
|
|
|
|
2020-10-10 21:43:46 +08:00
|
|
|
flags = enter_critical_section(); /* Disable interrupts */
|
2017-09-11 01:43:20 +08:00
|
|
|
dev->ops->shutdown(dev); /* Disable the POWERLED */
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_unlock(&dev->closelock);
|
2017-09-11 01:43:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: powerled_ioctl
|
2019-10-29 08:28:41 +08:00
|
|
|
****************************************************************************/
|
2017-09-11 01:43:20 +08:00
|
|
|
|
|
|
|
static int powerled_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct powerled_dev_s *dev = inode->i_private;
|
|
|
|
FAR struct powerled_s *powerled = (FAR struct powerled_s *)dev->priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case PWRIOC_START:
|
|
|
|
{
|
2020-10-10 21:43:46 +08:00
|
|
|
/* Allow powerled start only when limits set and structure is
|
|
|
|
* locked
|
|
|
|
*/
|
2017-09-11 01:43:20 +08:00
|
|
|
|
|
|
|
if (powerled->limits.lock == false ||
|
|
|
|
powerled->limits.current <= 0)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: powerled limits must be set"
|
|
|
|
" and locked before start\n");
|
|
|
|
|
|
|
|
ret = -EPERM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check powerled mode */
|
|
|
|
|
|
|
|
if (powerled->opmode != POWERLED_OPMODE_CONTINUOUS &&
|
|
|
|
powerled->opmode != POWERLED_OPMODE_FLASH)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: unsupported powerled mode!");
|
|
|
|
|
|
|
|
ret = -EPERM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, call start from lower-half driver */
|
|
|
|
|
|
|
|
ret = dev->ops->start(dev);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: PWRIOC_START failed %d\n", ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PWRIOC_STOP:
|
|
|
|
{
|
|
|
|
ret = dev->ops->stop(dev);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: PWRIOC_STOP failed %d\n", ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PWRIOC_SET_MODE:
|
|
|
|
{
|
|
|
|
uint8_t mode = ((uint8_t)arg);
|
|
|
|
|
|
|
|
ret = dev->ops->mode_set(dev, mode);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: PWRIOC_SET_MODE failed %d\n", ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PWRIOC_SET_LIMITS:
|
|
|
|
{
|
|
|
|
FAR struct powerled_limits_s *limits =
|
|
|
|
(FAR struct powerled_limits_s *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
if (powerled->limits.lock == true)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: PWRRLED limits locked!\n");
|
|
|
|
|
|
|
|
ret = -EPERM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: this call must set the powerled_limits_s structure */
|
|
|
|
|
|
|
|
ret = dev->ops->limits_set(dev, limits);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: PWRIOC_SET_LIMITS failed %d\n", ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PWRIOC_GET_STATE:
|
|
|
|
{
|
|
|
|
FAR struct powerled_state_s *state =
|
|
|
|
(FAR struct powerled_state_s *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
ret = dev->ops->state_get(dev, state);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: PWRIOC_GET_STATE failed %d\n", ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PWRIOC_SET_FAULT:
|
|
|
|
{
|
|
|
|
uint8_t fault = ((uint8_t)arg);
|
|
|
|
|
|
|
|
ret = dev->ops->fault_set(dev, fault);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: PWRIOC_SET_FAULT failed %d\n", ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PWRIOC_GET_FAULT:
|
|
|
|
{
|
2019-10-29 08:28:41 +08:00
|
|
|
FAR uint8_t *fault = ((FAR uint8_t *)arg);
|
2017-09-11 01:43:20 +08:00
|
|
|
|
|
|
|
ret = dev->ops->fault_get(dev, fault);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: PWRIOC_GET_FAULT failed %d\n", ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PWRIOC_CLEAN_FAULT:
|
|
|
|
{
|
|
|
|
uint8_t fault = ((uint8_t)arg);
|
|
|
|
|
|
|
|
ret = dev->ops->fault_clean(dev, fault);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: PWRIOC_CLEAN_FAULT failed %d\n", ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PWRIOC_SET_PARAMS:
|
|
|
|
{
|
|
|
|
FAR struct powerled_params_s *params =
|
|
|
|
(FAR struct powerled_params_s *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
if (powerled->param.lock == true)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: powerled params locked!\n");
|
|
|
|
|
|
|
|
ret = -EPERM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params->brightness < 0.0 || params->brightness > 100.0 ||
|
2020-10-10 21:43:46 +08:00
|
|
|
params->frequency < 0.0 || params->duty < 0.0 ||
|
|
|
|
params->duty > 100.0)
|
2017-09-11 01:43:20 +08:00
|
|
|
{
|
|
|
|
pwrerr("ERROR: powerled invalid parameters %f %f %f\n",
|
|
|
|
params->brightness, params->frequency, params->duty);
|
|
|
|
|
|
|
|
ret = -EPERM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = dev->ops->params_set(dev, params);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
pwrerr("ERROR: PWRIOC_SET_PARAMS failed %d\n", ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
pwrinfo("Forwarding unrecognized cmd: %d arg: %ld\n", cmd, arg);
|
|
|
|
ret = dev->ops->ioctl(dev, cmd, arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: powerled_register
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-29 08:28:41 +08:00
|
|
|
int powerled_register(FAR const char *path, FAR struct powerled_dev_s *dev,
|
|
|
|
FAR void *lower)
|
2017-09-11 01:43:20 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(path != NULL && dev != NULL && lower != NULL);
|
|
|
|
DEBUGASSERT(dev->ops != NULL);
|
|
|
|
|
|
|
|
/* For safety reason, when some necessary low-level logic is not provided,
|
|
|
|
* system should fail before low-level hardware initialization, so:
|
|
|
|
* - all ops are checked here, before character driver registration
|
|
|
|
* - all ops must be provided, even if not used
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(dev->ops->setup != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->shutdown != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->stop != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->start != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->params_set != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->mode_set != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->limits_set != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->fault_set != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->state_get != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->fault_get != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->fault_clean != NULL);
|
|
|
|
DEBUGASSERT(dev->ops->ioctl != NULL);
|
|
|
|
|
2018-02-11 21:12:00 +08:00
|
|
|
/* Initialize the powerled device structure */
|
2017-09-11 01:43:20 +08:00
|
|
|
|
|
|
|
dev->ocount = 0;
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
/* Initialize mutex */
|
2017-09-11 01:43:20 +08:00
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_init(&dev->closelock);
|
2017-09-11 01:43:20 +08:00
|
|
|
|
|
|
|
/* Connect POWERLED driver with lower level interface */
|
|
|
|
|
|
|
|
dev->lower = lower;
|
|
|
|
|
|
|
|
/* Register the POWERLED character driver */
|
|
|
|
|
2023-04-22 22:43:07 +08:00
|
|
|
ret = register_driver(path, &g_powerled_fops, 0666, dev);
|
2017-09-11 01:43:20 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_destroy(&dev->closelock);
|
2017-09-11 01:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|