zephyr/drivers/dac/dac_mcux_dac.c
Martí Bolívar 7e0eed9235 devicetree: allow access to all nodes
Usually, we want to operate only on "available" device
nodes ("available" means "status is okay and a matching binding is
found"), but that's not true in all cases.

Sometimes we want to operate on special nodes without matching
bindings, such as those describing memory.

To handle the distinction, change various additional devicetree APIs
making it clear that they operate only on available device nodes,
adjusting gen_defines and devicetree.h implementation details
accordingly:

- emit macros for all existing nodes in gen_defines.py, regardless
  of status or matching binding
- rename DT_NUM_INST to DT_NUM_INST_STATUS_OKAY
- rename DT_NODE_HAS_COMPAT to DT_NODE_HAS_COMPAT_STATUS_OKAY
- rename DT_INST_FOREACH to DT_INST_FOREACH_STATUS_OKAY
- rename DT_ANY_INST_ON_BUS to DT_ANY_INST_ON_BUS_STATUS_OKAY
- rewrite DT_HAS_NODE_STATUS_OKAY in terms of a new DT_NODE_HAS_STATUS
- resurrect DT_HAS_NODE in the form of DT_NODE_EXISTS
- remove DT_COMPAT_ON_BUS as a public API
- use the new default_prop_types edtlib parameter

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2020-05-08 19:37:18 -05:00

113 lines
2.6 KiB
C

/*
* Copyright (c) 2020 Henrik Brix Andersen <henrik@brixandersen.dk>
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nxp_kinetis_dac
#include <zephyr.h>
#include <drivers/dac.h>
#include <logging/log.h>
#include <fsl_dac.h>
LOG_MODULE_REGISTER(dac_mcux_dac, CONFIG_DAC_LOG_LEVEL);
struct mcux_dac_config {
DAC_Type *base;
dac_reference_voltage_source_t reference;
bool low_power;
};
struct mcux_dac_data {
bool configured;
};
static int mcux_dac_channel_setup(struct device *dev,
const struct dac_channel_cfg *channel_cfg)
{
const struct mcux_dac_config *config = dev->config_info;
struct mcux_dac_data *data = dev->driver_data;
dac_config_t dac_config;
if (channel_cfg->channel_id != 0) {
LOG_ERR("unsupported channel %d", channel_cfg->channel_id);
return -ENOTSUP;
}
if (channel_cfg->resolution != 12) {
LOG_ERR("unsupported resolution %d", channel_cfg->resolution);
return -ENOTSUP;
}
DAC_GetDefaultConfig(&dac_config);
dac_config.enableLowPowerMode = config->low_power;
dac_config.referenceVoltageSource = config->reference;
DAC_Init(config->base, &dac_config);
data->configured = true;
return 0;
}
static int mcux_dac_write_value(struct device *dev, u8_t channel, u32_t value)
{
const struct mcux_dac_config *config = dev->config_info;
struct mcux_dac_data *data = dev->driver_data;
if (!data->configured) {
LOG_ERR("channel not initialized");
return -EINVAL;
}
if (channel != 0) {
LOG_ERR("unsupported channel %d", channel);
return -ENOTSUP;
}
if (value >= 4096) {
LOG_ERR("value %d out of range", value);
return -EINVAL;
}
/* Static operation */
DAC_EnableBuffer(config->base, false);
DAC_SetBufferValue(config->base, 0, value);
DAC_Enable(config->base, true);
return 0;
}
static int mcux_dac_init(struct device *dev)
{
return 0;
}
static const struct dac_driver_api mcux_dac_driver_api = {
.channel_setup = mcux_dac_channel_setup,
.write_value = mcux_dac_write_value,
};
#define TO_DAC_VREF_SRC(val) \
_DO_CONCAT(kDAC_ReferenceVoltageSourceVref, val)
#define MCUX_DAC_INIT(n) \
static struct mcux_dac_data mcux_dac_data_##n; \
\
static const struct mcux_dac_config mcux_dac_config_##n = { \
.base = (DAC_Type *)DT_INST_REG_ADDR(n), \
.reference = \
TO_DAC_VREF_SRC(DT_INST_PROP(n, voltage_reference)), \
.low_power = DT_INST_PROP(n, low_power_mode), \
}; \
\
DEVICE_AND_API_INIT(mcux_dac_##n, DT_INST_LABEL(n), \
mcux_dac_init, &mcux_dac_data_##n, \
&mcux_dac_config_##n, \
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,\
&mcux_dac_driver_api);
DT_INST_FOREACH_STATUS_OKAY(MCUX_DAC_INIT)