2020-07-04 21:19:59 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Google LLC.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT atmel_sam0_dac
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <drivers/dac.h>
|
|
|
|
#include <soc.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Maps between the DTS reference property names and register values. Note that
|
|
|
|
* the ASF uses the 09/2015 names which differ from the 03/2020 datasheet.
|
|
|
|
*
|
|
|
|
* TODO(#21273): replace once improved support for enum values lands.
|
|
|
|
*/
|
|
|
|
#define SAM0_DAC_REFSEL_0 DAC_CTRLB_REFSEL_INT1V_Val
|
|
|
|
#define SAM0_DAC_REFSEL_1 DAC_CTRLB_REFSEL_AVCC_Val
|
|
|
|
#define SAM0_DAC_REFSEL_2 DAC_CTRLB_REFSEL_VREFP_Val
|
|
|
|
|
|
|
|
struct dac_sam0_cfg {
|
|
|
|
Dac *regs;
|
|
|
|
uint8_t pm_apbc_bit;
|
|
|
|
uint8_t gclk_clkctrl_id;
|
|
|
|
uint8_t refsel;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Write to the DAC. */
|
2020-05-01 02:33:38 +08:00
|
|
|
static int dac_sam0_write_value(const struct device *dev, uint8_t channel,
|
2020-07-04 21:19:59 +08:00
|
|
|
uint32_t value)
|
|
|
|
{
|
2022-01-18 22:20:23 +08:00
|
|
|
const struct dac_sam0_cfg *const cfg = dev->config;
|
2020-07-04 21:19:59 +08:00
|
|
|
Dac *regs = cfg->regs;
|
|
|
|
|
|
|
|
regs->DATA.reg = (uint16_t)value;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup the channel. As the SAM0 has one fixed width channel, this validates
|
|
|
|
* the input and does nothing else.
|
|
|
|
*/
|
2020-05-01 02:33:38 +08:00
|
|
|
static int dac_sam0_channel_setup(const struct device *dev,
|
2020-07-04 21:19:59 +08:00
|
|
|
const struct dac_channel_cfg *channel_cfg)
|
|
|
|
{
|
|
|
|
if (channel_cfg->channel_id != 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (channel_cfg->resolution != 10) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise and enable the DAC. */
|
2020-05-01 02:33:38 +08:00
|
|
|
static int dac_sam0_init(const struct device *dev)
|
2020-07-04 21:19:59 +08:00
|
|
|
{
|
2022-01-18 22:20:23 +08:00
|
|
|
const struct dac_sam0_cfg *const cfg = dev->config;
|
2020-07-04 21:19:59 +08:00
|
|
|
Dac *regs = cfg->regs;
|
|
|
|
|
|
|
|
/* Enable the GCLK */
|
|
|
|
GCLK->CLKCTRL.reg = cfg->gclk_clkctrl_id | GCLK_CLKCTRL_GEN_GCLK0 |
|
|
|
|
GCLK_CLKCTRL_CLKEN;
|
|
|
|
|
|
|
|
/* Enable the clock in PM */
|
|
|
|
PM->APBCMASK.reg |= 1 << cfg->pm_apbc_bit;
|
|
|
|
|
|
|
|
/* Reset then configure the DAC */
|
|
|
|
regs->CTRLA.bit.SWRST = 1;
|
|
|
|
while (regs->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
|
|
|
|
|
|
|
regs->CTRLB.bit.REFSEL = cfg->refsel;
|
|
|
|
regs->CTRLB.bit.EOEN = 1;
|
|
|
|
|
|
|
|
/* Enable */
|
|
|
|
regs->CTRLA.bit.ENABLE = 1;
|
|
|
|
while (regs->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dac_driver_api api_sam0_driver_api = {
|
|
|
|
.channel_setup = dac_sam0_channel_setup,
|
|
|
|
.write_value = dac_sam0_write_value
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SAM0_DAC_REFSEL(n) \
|
|
|
|
COND_CODE_1(DT_INST_NODE_HAS_PROP(n, reference), \
|
2021-11-17 21:01:42 +08:00
|
|
|
(DT_INST_ENUM_IDX(n, reference)), (0))
|
2020-07-04 21:19:59 +08:00
|
|
|
|
|
|
|
#define SAM0_DAC_INIT(n) \
|
|
|
|
static const struct dac_sam0_cfg dac_sam0_cfg_##n = { \
|
|
|
|
.regs = (Dac *)DT_INST_REG_ADDR(n), \
|
|
|
|
.pm_apbc_bit = DT_INST_CLOCKS_CELL_BY_NAME(n, pm, bit), \
|
|
|
|
.gclk_clkctrl_id = \
|
|
|
|
DT_INST_CLOCKS_CELL_BY_NAME(n, gclk, clkctrl_id), \
|
|
|
|
.refsel = UTIL_CAT(SAM0_DAC_REFSEL_, SAM0_DAC_REFSEL(n)), \
|
|
|
|
}; \
|
|
|
|
\
|
2021-04-28 16:29:41 +08:00
|
|
|
DEVICE_DT_INST_DEFINE(n, &dac_sam0_init, NULL, NULL, \
|
2020-07-04 21:19:59 +08:00
|
|
|
&dac_sam0_cfg_##n, POST_KERNEL, \
|
2021-10-26 08:13:48 +08:00
|
|
|
CONFIG_DAC_INIT_PRIORITY, \
|
2020-07-04 21:19:59 +08:00
|
|
|
&api_sam0_driver_api)
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(SAM0_DAC_INIT);
|