Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:43

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup shared_tod_i2c_rtc
0007  *
0008  * @brief This file provides the interfaces of @ref shared_tod_i2c_rtc.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2023-2024 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifndef LIBCHIP_I2C_RTC_H
0037 #define LIBCHIP_I2C_RTC_H
0038 
0039 #include <rtems.h>
0040 
0041 #ifdef __cplusplus
0042 extern "C" {
0043 #endif /* __cplusplus */
0044 
0045 /**
0046  * @defgroup shared_tod_i2c_rtc
0047  *
0048  * @ingroup shared_tod
0049  *
0050  * @brief Generic I2C RTC driver
0051  *
0052  * Base functionality for I2C based RTC drivers. An additional, chip specific
0053  * initialization is necessary.
0054  *
0055  * Expects a register block with the following time format:
0056  *
0057  * * <base>+off.second:
0058  *    * Bit 0 to 6: Seconds (BCD encoded)
0059  *    * Bit 7: Don't care
0060  * * <base>+off.minute:
0061  *    * Bit 0 to 6: Minutes (BCD encoded)
0062  *    * Bit 7: Don't care
0063  * * <base>+off.hour:
0064  *    * Bit 0 to 5: Hours (BCD encoded); Bit 5 indicates AM (0) and PM (1) in 12
0065  *      hour mode
0066  *    * Bit 6: 0 for 24 hour mode; 1 for 12 hour mode
0067  * * <base>+off.wkday:
0068  *    * Bit 0 to 2: Day of Week
0069  *    * Bit 3 to 7: Don't care
0070  * * <base>+off.day:
0071  *    * Bit 0 to 5: Day (BCD encoded)
0072  *    * Bit 6 and 7: Don't care
0073  * * <base>+off.month:
0074  *    * Bit 0 to 4: Month (BCD encoded)
0075  *    * Bit 5 to 7: Don't care
0076  * * <base>+off.year:
0077  *    * Bit 0 to 6: Year (BCD encoded)
0078  *    * Bit 7: Don't care
0079  *
0080  * See abeoz9 or mcp7940m for how to use this base driver.
0081  *
0082  * @{
0083  */
0084 
0085 #include <rtems.h>
0086 #include <rtems/thread.h>
0087 #include <libchip/rtc.h>
0088 #include <stdint.h>
0089 
0090 #ifdef __cplusplus
0091 extern "C" {
0092 #endif /* __cplusplus */
0093 
0094 extern const rtc_fns i2c_rtc_fns;
0095 bool i2c_rtc_probe(int minor);
0096 
0097 /** Base context for the driver. */
0098 struct i2c_rtc_base {
0099   /** Just initialize with RTEMS_MUTEX_INITIALIZER('mcp7940'). */
0100   rtems_mutex mutex;
0101 
0102   /** The path of the I2C bus device. */
0103   const char *i2c_bus_path;
0104 
0105   /** I2C address. */
0106   uint8_t i2c_addr;
0107 
0108   /** Whether the RTC has already been initialized. Used internally. */
0109   bool initialized;
0110 
0111   /**
0112    * Hardware initialization function. Will be called once when the RTC is used
0113    * for the first time. Can return 0 on success or anything else on error. In
0114    * an error case, it will be called again on the next use.
0115    */
0116   int (*hw_init) (struct i2c_rtc_base *ctx);
0117 
0118   /** Offset of the start of the clock register block. */
0119   size_t clock_offset;
0120 
0121   /** Order of the fields of the date. */
0122   struct {
0123     size_t year;
0124     size_t month;
0125     size_t wkday;
0126     size_t day;
0127     size_t hour;
0128     size_t min;
0129     size_t sec;
0130   } order;
0131 };
0132 
0133 /**
0134  * Read bytes from the I2C RTC. Can be used in derived drivers.
0135  *
0136  * Returns 0 on success or anything else on error.
0137  */
0138 int i2c_rtc_read(struct i2c_rtc_base *ctx, uint8_t addr, uint8_t *buf,
0139     size_t len);
0140 
0141 /**
0142  * Write bytes to the I2C RTC. Can be used in derived drivers.
0143  *
0144  * Returns 0 on success or anything else on error.
0145  */
0146 int i2c_rtc_write(struct i2c_rtc_base *ctx, uint8_t addr, const uint8_t *buf,
0147     size_t len);
0148 
0149 #define I2C_RTC_ORDER_sec_min_hour_wkday_day_month_year \
0150   { .sec = 0, .min = 1, .hour = 2, .day = 4, .wkday = 3, .month = 5, .year = 6 }
0151 
0152 #define I2C_RTC_ORDER_sec_min_hour_day_wkday_month_year \
0153   { .sec = 0, .min = 1, .hour = 2, .day = 3, .wkday = 4, .month = 5, .year = 6 }
0154 
0155 #define I2C_RTC_INITIALIZER(i2c_path, i2c_address, offset, reg_order, \
0156     driver_name, hwinit)\
0157   { \
0158     .mutex = RTEMS_MUTEX_INITIALIZER(driver_name), \
0159     .i2c_bus_path = i2c_path, \
0160     .i2c_addr = i2c_address, \
0161     .initialized = false, \
0162     .hw_init = hwinit, \
0163     .clock_offset = offset, \
0164     .order = reg_order, \
0165   }
0166 
0167 #define I2C_RTC_TBL_ENTRY(dev_name, i2c_rtc_ctx) \
0168   { \
0169     .sDeviceName = dev_name, \
0170     .deviceType = RTC_CUSTOM, \
0171     .pDeviceFns = &i2c_rtc_fns, \
0172     .deviceProbe = i2c_rtc_probe, \
0173     .pDeviceParams = (void *)i2c_rtc_ctx, \
0174     .ulCtrlPort1 = 0, \
0175     .ulDataPort = 0, \
0176     .getRegister = NULL, \
0177     .setRegister = NULL, \
0178   }
0179 
0180 /** @} */
0181 
0182 #ifdef __cplusplus
0183 }
0184 #endif /* __cplusplus */
0185 
0186 #endif /* LIBCHIP_I2C_RTC_H */