Skip to content

Commit

Permalink
drivers: rtc: maxim,ds3231: RTC driver
Browse files Browse the repository at this point in the history
This is a squash of all the groundwork needed to
get a functioning driver for the DS3231 with the RTC API.

(cherry picked from commit 2759ada)

Original-Signed-off-by: Gergo Vari <work@gergovari.com>
GitOrigin-RevId: 2759ada
Cr-Build-Id: 8726362737952124785
Cr-Build-Url: https://cr-buildbucket.appspot.com/build/8726362737952124785
Copybot-Job-Name: zephyr-main-copybot-downstream
Change-Id: Idf14862d682179c0c1c50e3a8761c5e62cd97956
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/6154244
Tested-by: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
Bot-Commit: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
Commit-Queue: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
  • Loading branch information
Gergo Vari authored and Chromeos LUCI committed Jan 8, 2025
1 parent 3a31f7c commit f254c77
Show file tree
Hide file tree
Showing 20 changed files with 1,520 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/mfd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ zephyr_library_sources_ifdef(CONFIG_MFD_TLE9104 mfd_tle9104.c)
zephyr_library_sources_ifdef(CONFIG_MFD_ITE_IT8801 mfd_ite_it8801.c)
zephyr_library_sources_ifdef(CONFIG_MFD_ITE_IT8801_ALTCTRL mfd_it8801_altctrl.c)
zephyr_library_sources_ifdef(CONFIG_MFD_AW9523B mfd_aw9523b.c)
zephyr_library_sources_ifdef(CONFIG_MFD_DS3231 mfd_ds3231.c)
1 change: 1 addition & 0 deletions drivers/mfd/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ source "drivers/mfd/Kconfig.adp5585"
source "drivers/mfd/Kconfig.axp192"
source "drivers/mfd/Kconfig.aw9523b"
source "drivers/mfd/Kconfig.bd8lb600fs"
source "drivers/mfd/Kconfig.ds3231"
source "drivers/mfd/Kconfig.max20335"
source "drivers/mfd/Kconfig.max31790"
source "drivers/mfd/Kconfig.nct38xx"
Expand Down
10 changes: 10 additions & 0 deletions drivers/mfd/Kconfig.ds3231
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2024 Gergo Vari <work@gergovari.com>
# SPDX-License-Identifier: Apache-2.0

config MFD_DS3231
bool "DS3231 multi-function device driver"
default y
depends on DT_HAS_MAXIM_DS3231_MFD_ENABLED
select I2C
help
Enable the Maxim DS3231 multi-function device driver
77 changes: 77 additions & 0 deletions drivers/mfd/mfd_ds3231.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2024 Gergo Vari <work@gergovari.com>
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/drivers/mfd/ds3231.h>

#include <zephyr/kernel.h>
#include <zephyr/drivers/i2c.h>

#include <zephyr/sys/util.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(mfd_ds3231, CONFIG_MFD_LOG_LEVEL);

#define DT_DRV_COMPAT maxim_ds3231_mfd

struct mfd_ds3231_data {
struct k_sem lock;
const struct device *dev;
};

struct mfd_ds3231_conf {
struct i2c_dt_spec i2c_bus;
};

int mfd_ds3231_i2c_get_registers(const struct device *dev, uint8_t start_reg, uint8_t *buf,
const size_t buf_size)
{
struct mfd_ds3231_data *data = dev->data;
const struct mfd_ds3231_conf *config = dev->config;

/* FIXME: bad start_reg/buf_size values break i2c for that run */

(void)k_sem_take(&data->lock, K_FOREVER);
int err = i2c_burst_read_dt(&config->i2c_bus, start_reg, buf, buf_size);

k_sem_give(&data->lock);

return err;
}

int mfd_ds3231_i2c_set_registers(const struct device *dev, uint8_t start_reg, const uint8_t *buf,
const size_t buf_size)
{
struct mfd_ds3231_data *data = dev->data;
const struct mfd_ds3231_conf *config = dev->config;

(void)k_sem_take(&data->lock, K_FOREVER);
int err = i2c_burst_write_dt(&config->i2c_bus, start_reg, buf, buf_size);

k_sem_give(&data->lock);

return err;
}

static int mfd_ds3231_init(const struct device *dev)
{
struct mfd_ds3231_data *data = dev->data;
const struct mfd_ds3231_conf *config = (struct mfd_ds3231_conf *)(dev->config);

k_sem_init(&data->lock, 1, 1);
if (!i2c_is_ready_dt(&(config->i2c_bus))) {
LOG_ERR("I2C bus not ready.");
return -ENODEV;
}
return 0;
}

#define MFD_DS3231_DEFINE(inst) \
static const struct mfd_ds3231_conf config##inst = {.i2c_bus = \
I2C_DT_SPEC_INST_GET(inst)}; \
static struct mfd_ds3231_data data##inst; \
DEVICE_DT_INST_DEFINE(inst, &mfd_ds3231_init, NULL, &data##inst, &config##inst, \
POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, NULL);

DT_INST_FOREACH_STATUS_OKAY(MFD_DS3231_DEFINE)
1 change: 1 addition & 0 deletions drivers/rtc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ zephyr_library_sources_ifdef(CONFIG_RTC_RV8263 rtc_rv8263.c)
zephyr_library_sources_ifdef(CONFIG_RTC_AM1805 rtc_am1805.c)
zephyr_library_sources_ifdef(CONFIG_RTC_AMBIQ rtc_ambiq.c)
zephyr_library_sources_ifdef(CONFIG_RTC_DS1307 rtc_ds1307.c)
zephyr_library_sources_ifdef(CONFIG_RTC_DS3231 rtc_ds3231.c)
zephyr_library_sources_ifdef(CONFIG_USERSPACE rtc_handlers.c)
zephyr_library_sources_ifdef(CONFIG_RTC_EMUL rtc_emul.c)
zephyr_library_sources_ifdef(CONFIG_RTC_INFINEON_CAT1 rtc_ifx_cat1.c)
Expand Down
1 change: 1 addition & 0 deletions drivers/rtc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ config RTC_SHELL
source "drivers/rtc/Kconfig.am1805"
source "drivers/rtc/Kconfig.ambiq"
source "drivers/rtc/Kconfig.ds1307"
source "drivers/rtc/Kconfig.ds3231"
source "drivers/rtc/Kconfig.emul"
source "drivers/rtc/Kconfig.fake"
source "drivers/rtc/Kconfig.ifx_cat1"
Expand Down
23 changes: 23 additions & 0 deletions drivers/rtc/Kconfig.ds3231
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2024, Gergo Vari <work@gergovari.com>
#
# SPDX-License-Identifier: Apache-2.0
#

config RTC_DS3231
bool "Maxim DS3231 RTC/TCXO"
default y
depends on DT_HAS_MAXIM_DS3231_MFD_ENABLED
depends on DT_HAS_MAXIM_DS3231_RTC_ENABLED
select I2C
select MFD
help
Enable RTC driver based on Maxim DS3231 I2C device.

config RTC_DS3231_INIT_PRIORITY
int "DS3231 RTC driver initialization priority"
depends on RTC_DS3231
default 86
help
Initialization priority for the DS3231 RTC driver. It must be
greater than the I2C controller init priority and the mfd driver
init priority.
Loading

0 comments on commit f254c77

Please sign in to comment.