2019-08-22 14:49:05 +02:00
|
|
|
/****************************************************************************
|
2020-01-07 15:21:58 +01:00
|
|
|
* boards/arm/cxd56xx/drivers/sensors/bh1721fvc_scu.c
|
2019-08-22 14:49:05 +02:00
|
|
|
*
|
2021-01-25 08:52:15 +01: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
|
2019-08-22 14:49:05 +02:00
|
|
|
*
|
2021-01-25 08:52:15 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-08-22 14:49:05 +02:00
|
|
|
*
|
2021-01-25 08:52:15 +01: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.
|
2019-08-22 14:49:05 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fixedmath.h>
|
2021-06-08 20:00:55 +02:00
|
|
|
#include <assert.h>
|
2019-08-22 14:49:05 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <arch/types.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
|
|
|
#include <nuttx/sensors/bh1721fvc.h>
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <arch/chip/scu.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_BH1721FVC_SCU)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define BH1721FVC_ADDR 0x23 /* I2C Slave Address */
|
|
|
|
#define BH1721FVC_BYTESPERSAMPLE 2
|
|
|
|
#define BH1721FVC_ELEMENTSIZE 0
|
|
|
|
|
|
|
|
/* BH1721FVC Opecode */
|
|
|
|
|
|
|
|
#define BH1721FVC_POWERDOWN 0x00
|
|
|
|
#define BH1721FVC_POWERON 0x01
|
|
|
|
#define BH1721FVC_AUTORESOLUTION 0x10
|
|
|
|
|
|
|
|
#ifndef itemsof
|
|
|
|
# define itemsof(array) (sizeof(array)/sizeof(array[0]))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
2020-01-07 22:29:52 +01:00
|
|
|
|
2023-07-31 12:55:26 +02:00
|
|
|
/* Structure for bh1721fvc device */
|
2019-08-22 14:49:05 +02:00
|
|
|
|
|
|
|
struct bh1721fvc_dev_s
|
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
struct i2c_master_s *i2c; /* I2C interface */
|
|
|
|
uint8_t addr; /* I2C address */
|
|
|
|
int port; /* I2C port */
|
|
|
|
struct seq_s *seq; /* Sequencer instance */
|
|
|
|
int minor; /* Minor device number */
|
2019-08-22 14:49:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static int bh1721fvc_open(struct file *filep);
|
|
|
|
static int bh1721fvc_close(struct file *filep);
|
|
|
|
static ssize_t bh1721fvc_read(struct file *filep,
|
|
|
|
char *buffer,
|
2019-08-22 14:49:05 +02:00
|
|
|
size_t buflen);
|
2022-04-17 08:01:48 +02:00
|
|
|
static ssize_t bh1721fvc_write(struct file *filep,
|
|
|
|
const char *buffer,
|
2019-08-22 14:49:05 +02:00
|
|
|
size_t buflen);
|
2022-04-17 08:01:48 +02:00
|
|
|
static int bh1721fvc_ioctl(struct file *filep,
|
2020-04-17 09:59:16 +02:00
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
2019-08-22 14:49:05 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_bh1721fvcfops =
|
|
|
|
{
|
|
|
|
bh1721fvc_open, /* open */
|
|
|
|
bh1721fvc_close, /* close */
|
|
|
|
bh1721fvc_read, /* read */
|
|
|
|
bh1721fvc_write, /* write */
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL, /* seek */
|
2019-08-22 14:49:05 +02:00
|
|
|
bh1721fvc_ioctl, /* ioctl */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Take ambient light data. */
|
|
|
|
|
|
|
|
static const uint16_t g_bh1721fvcinst[] =
|
|
|
|
{
|
|
|
|
SCU_INST_RECV(BH1721FVC_BYTESPERSAMPLE) | SCU_INST_LAST,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Reference count */
|
|
|
|
|
|
|
|
static int g_refcnt = 0;
|
|
|
|
|
|
|
|
/* Sequencer instance */
|
|
|
|
|
|
|
|
static struct seq_s *g_seq = NULL;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bh1721fvc_writeopecode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to BH1721FVC opecode
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static void bh1721fvc_writeopecode(struct bh1721fvc_dev_s *priv,
|
2019-08-22 14:49:05 +02:00
|
|
|
uint8_t opecode)
|
|
|
|
{
|
|
|
|
uint16_t inst = SCU_INST_SEND(opecode) | SCU_INST_LAST;
|
|
|
|
|
|
|
|
/* Send opecode */
|
|
|
|
|
|
|
|
scu_i2ctransfer(priv->port, priv->addr, &inst, 1, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bh1721fvc_seqinit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize SCU sequencer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static int bh1721fvc_seqinit(struct bh1721fvc_dev_s *priv)
|
2019-08-22 14:49:05 +02:00
|
|
|
{
|
|
|
|
DEBUGASSERT(g_seq == NULL);
|
|
|
|
|
|
|
|
/* Open sequencer */
|
|
|
|
|
|
|
|
g_seq = seq_open(SEQ_TYPE_NORMAL, SCU_BUS_I2C0);
|
|
|
|
if (!g_seq)
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2020-01-07 22:29:52 +01:00
|
|
|
|
2019-08-22 14:49:05 +02:00
|
|
|
priv->seq = g_seq;
|
|
|
|
|
|
|
|
seq_setaddress(priv->seq, priv->addr);
|
|
|
|
|
|
|
|
/* Set instruction and sample data information to sequencer */
|
|
|
|
|
2020-04-17 09:59:16 +02:00
|
|
|
seq_setinstruction(priv->seq,
|
|
|
|
g_bh1721fvcinst,
|
|
|
|
itemsof(g_bh1721fvcinst));
|
|
|
|
seq_setsample(priv->seq,
|
|
|
|
BH1721FVC_BYTESPERSAMPLE,
|
|
|
|
0,
|
|
|
|
BH1721FVC_ELEMENTSIZE,
|
2019-08-22 14:49:05 +02:00
|
|
|
false);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bh1721fvc_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called whenever the BH1721FVC device is opened.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static int bh1721fvc_open(struct file *filep)
|
2019-08-22 14:49:05 +02:00
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
struct inode *inode = filep->f_inode;
|
|
|
|
struct bh1721fvc_dev_s *priv = inode->i_private;
|
2019-08-22 14:49:05 +02:00
|
|
|
|
|
|
|
if (g_refcnt == 0)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = bh1721fvc_seqinit(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bh1721fvc_writeopecode(priv, BH1721FVC_POWERON);
|
|
|
|
bh1721fvc_writeopecode(priv, BH1721FVC_AUTORESOLUTION);
|
|
|
|
}
|
2020-01-07 22:29:52 +01:00
|
|
|
|
2019-08-22 14:49:05 +02:00
|
|
|
g_refcnt++;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bh1721fvc_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This routine is called when the BH1721FVC device is closed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static int bh1721fvc_close(struct file *filep)
|
2019-08-22 14:49:05 +02:00
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
struct inode *inode = filep->f_inode;
|
|
|
|
struct bh1721fvc_dev_s *priv = inode->i_private;
|
2019-08-22 14:49:05 +02:00
|
|
|
|
|
|
|
g_refcnt--;
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
seq_ioctl(priv->seq, priv->minor, SCUIOC_STOP, 0);
|
2019-08-22 14:49:05 +02:00
|
|
|
|
|
|
|
if (g_refcnt == 0)
|
|
|
|
{
|
|
|
|
bh1721fvc_writeopecode(priv, BH1721FVC_POWERDOWN);
|
|
|
|
|
|
|
|
seq_close(g_seq);
|
|
|
|
g_seq = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
seq_ioctl(priv->seq, priv->minor, SCUIOC_FREEFIFO, 0);
|
2019-08-22 14:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bh1721fvc_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static ssize_t bh1721fvc_read(struct file *filep, char *buffer,
|
2019-08-22 14:49:05 +02:00
|
|
|
size_t len)
|
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
struct inode *inode = filep->f_inode;
|
|
|
|
struct bh1721fvc_dev_s *priv = inode->i_private;
|
2019-08-22 14:49:05 +02:00
|
|
|
|
|
|
|
len = len / BH1721FVC_BYTESPERSAMPLE * BH1721FVC_BYTESPERSAMPLE;
|
|
|
|
len = seq_read(priv->seq, priv->minor, buffer, len);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bh1721fvc_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static ssize_t bh1721fvc_write(struct file *filep,
|
|
|
|
const char *buffer,
|
2019-08-22 14:49:05 +02:00
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bh1721fvc_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static int bh1721fvc_ioctl(struct file *filep, int cmd,
|
2019-08-22 14:49:05 +02:00
|
|
|
unsigned long arg)
|
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
struct inode *inode = filep->f_inode;
|
|
|
|
struct bh1721fvc_dev_s *priv = inode->i_private;
|
2019-08-22 14:49:05 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
if (_SCUIOCVALID(cmd))
|
|
|
|
{
|
|
|
|
/* Redirect SCU commands */
|
|
|
|
|
|
|
|
ret = seq_ioctl(priv->seq, priv->minor, cmd, arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snerr("Unrecognized cmd: %d\n", cmd);
|
|
|
|
ret = - ENOTTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bh1721fvc_init
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the BH1721FVC device
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* i2c - An instance of the I2C interface to use to communicate with
|
|
|
|
* BH1721FVC
|
|
|
|
* port - I2C port (0 or 1)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
int bh1721fvc_init(struct i2c_master_s *i2c, int port)
|
2019-08-22 14:49:05 +02:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bh1721fvc_register
|
|
|
|
*
|
|
|
|
* Description:
|
2020-04-17 09:59:16 +02:00
|
|
|
* Register the BH1721FVC ambient light sensor character device as
|
|
|
|
* 'devpath'
|
2019-08-22 14:49:05 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register. E.g., "/dev/light0"
|
|
|
|
* minor - minor device number
|
|
|
|
* i2c - An instance of the I2C interface to use to communicate with
|
|
|
|
* BH1721FVC
|
|
|
|
* port - I2C port (0 or 1)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
int bh1721fvc_register(const char *devpath, int minor,
|
|
|
|
struct i2c_master_s *i2c, int port)
|
2019-08-22 14:49:05 +02:00
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
struct bh1721fvc_dev_s *priv;
|
2019-08-22 14:49:05 +02:00
|
|
|
char path[16];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize the BH1721FVC device structure */
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
priv = (struct bh1721fvc_dev_s *)
|
2019-08-22 14:49:05 +02:00
|
|
|
kmm_malloc(sizeof(struct bh1721fvc_dev_s));
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
snerr("Failed to allocate instance\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->addr = BH1721FVC_ADDR;
|
|
|
|
priv->port = port;
|
|
|
|
priv->seq = NULL;
|
|
|
|
priv->minor = minor;
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
snprintf(path, sizeof(path), "%s%d", devpath, minor);
|
2019-08-22 14:49:05 +02:00
|
|
|
ret = register_driver(path, &g_bh1721fvcfops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("Failed to register driver: %d\n", ret);
|
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_I2C && CONFIG_SENSORS_BH1721FVC_SCU */
|