2018-06-26 16:27:37 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Savoir-Faire Linux.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-25 11:23:27 -05:00
|
|
|
#define DT_DRV_COMPAT nxp_pca9633
|
|
|
|
|
2018-06-26 16:27:37 -04:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
|
|
|
|
*/
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/drivers/i2c.h>
|
|
|
|
#include <zephyr/drivers/led.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2018-06-26 16:27:37 -04:00
|
|
|
|
2018-10-08 18:10:25 -04:00
|
|
|
#define LOG_LEVEL CONFIG_LED_LOG_LEVEL
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
2018-10-08 18:10:25 -04:00
|
|
|
LOG_MODULE_REGISTER(pca9633);
|
2018-06-26 16:27:37 -04:00
|
|
|
|
|
|
|
/* PCA9633 select registers determine the source that drives LED outputs */
|
|
|
|
#define PCA9633_LED_OFF 0x0 /* LED driver off */
|
|
|
|
#define PCA9633_LED_ON 0x1 /* LED driver on */
|
|
|
|
#define PCA9633_LED_PWM 0x2 /* Controlled through PWM */
|
|
|
|
#define PCA9633_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
|
|
|
|
|
|
|
|
/* PCA9633 control register */
|
|
|
|
#define PCA9633_MODE1 0x00
|
|
|
|
#define PCA9633_MODE2 0x01
|
2021-11-04 00:47:55 +01:00
|
|
|
#define PCA9633_PWM_BASE 0x02 /* Reg 0x02-0x05 for brightness control LED01-04 */
|
2018-06-26 16:27:37 -04:00
|
|
|
#define PCA9633_GRPPWM 0x06
|
|
|
|
#define PCA9633_GRPFREQ 0x07
|
|
|
|
#define PCA9633_LEDOUT 0x08
|
|
|
|
|
2021-07-29 13:17:29 +02:00
|
|
|
/* PCA9633 mode register 1 */
|
2023-07-28 09:40:08 +02:00
|
|
|
#define PCA9633_MODE1_ALLCAL 0x01 /* All Call Address enabled */
|
2021-07-29 13:17:29 +02:00
|
|
|
#define PCA9633_MODE1_SLEEP 0x10 /* Sleep Mode */
|
2018-06-26 16:27:37 -04:00
|
|
|
/* PCA9633 mode register 2 */
|
|
|
|
#define PCA9633_MODE2_DMBLNK 0x20 /* Enable blinking */
|
|
|
|
|
|
|
|
#define PCA9633_MASK 0x03
|
|
|
|
|
2025-05-19 22:42:00 +01:00
|
|
|
#define PCA9633_MIN_PERIOD 41U
|
|
|
|
#define PCA9633_MAX_PERIOD 10667U
|
|
|
|
|
2021-11-04 00:47:55 +01:00
|
|
|
struct pca9633_config {
|
|
|
|
struct i2c_dt_spec i2c;
|
2023-07-28 09:40:08 +02:00
|
|
|
bool disable_allcall;
|
2021-11-04 00:47:55 +01:00
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int pca9633_led_blink(const struct device *dev, uint32_t led,
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t delay_on, uint32_t delay_off)
|
2018-06-26 16:27:37 -04:00
|
|
|
{
|
2021-11-04 00:47:55 +01:00
|
|
|
const struct pca9633_config *config = dev->config;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t gdc, gfrq;
|
|
|
|
uint32_t period;
|
2018-06-26 16:27:37 -04:00
|
|
|
|
|
|
|
period = delay_on + delay_off;
|
|
|
|
|
2025-05-19 22:42:00 +01:00
|
|
|
if (period < PCA9633_MIN_PERIOD || period > PCA9633_MAX_PERIOD) {
|
2018-06-26 16:27:37 -04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* From manual:
|
|
|
|
* duty cycle = (GDC / 256) ->
|
|
|
|
* (time_on / period) = (GDC / 256) ->
|
|
|
|
* GDC = ((time_on * 256) / period)
|
|
|
|
*/
|
2019-03-26 19:57:45 -06:00
|
|
|
gdc = delay_on * 256U / period;
|
2021-11-04 00:47:55 +01:00
|
|
|
if (i2c_reg_write_byte_dt(&config->i2c,
|
2018-06-26 16:27:37 -04:00
|
|
|
PCA9633_GRPPWM,
|
|
|
|
gdc)) {
|
2018-10-08 18:10:25 -04:00
|
|
|
LOG_ERR("LED reg write failed");
|
2018-06-26 16:27:37 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* From manual:
|
|
|
|
* period = ((GFRQ + 1) / 24) in seconds.
|
|
|
|
* So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
|
|
|
|
* GFRQ = ((period * 24 / 1000) - 1)
|
|
|
|
*/
|
2019-03-26 19:57:45 -06:00
|
|
|
gfrq = (period * 24U / 1000) - 1;
|
2021-11-04 00:47:55 +01:00
|
|
|
if (i2c_reg_write_byte_dt(&config->i2c,
|
2018-06-26 16:27:37 -04:00
|
|
|
PCA9633_GRPFREQ,
|
|
|
|
gfrq)) {
|
2018-10-08 18:10:25 -04:00
|
|
|
LOG_ERR("LED reg write failed");
|
2018-06-26 16:27:37 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable blinking mode */
|
2021-11-04 00:47:55 +01:00
|
|
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
2018-06-26 16:27:37 -04:00
|
|
|
PCA9633_MODE2,
|
|
|
|
PCA9633_MODE2_DMBLNK,
|
|
|
|
PCA9633_MODE2_DMBLNK)) {
|
2018-10-08 18:10:25 -04:00
|
|
|
LOG_ERR("LED reg update failed");
|
2018-06-26 16:27:37 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2022-02-24 12:00:55 +00:00
|
|
|
/* Select the GRPPWM source to drive the LED output */
|
2021-11-04 00:47:55 +01:00
|
|
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
2018-06-26 16:27:37 -04:00
|
|
|
PCA9633_LEDOUT,
|
|
|
|
PCA9633_MASK << (led << 1),
|
|
|
|
PCA9633_LED_GRP_PWM << (led << 1))) {
|
2018-10-08 18:10:25 -04:00
|
|
|
LOG_ERR("LED reg update failed");
|
2018-06-26 16:27:37 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int pca9633_led_set_brightness(const struct device *dev, uint32_t led,
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t value)
|
2018-06-26 16:27:37 -04:00
|
|
|
{
|
2021-11-04 00:47:55 +01:00
|
|
|
const struct pca9633_config *config = dev->config;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t val;
|
2018-06-26 16:27:37 -04:00
|
|
|
|
|
|
|
/* Set the LED brightness value */
|
2025-06-09 07:46:37 +02:00
|
|
|
val = (value * 255U) / LED_BRIGHTNESS_MAX;
|
2021-11-04 00:47:55 +01:00
|
|
|
if (i2c_reg_write_byte_dt(&config->i2c,
|
2018-06-26 16:27:37 -04:00
|
|
|
PCA9633_PWM_BASE + led,
|
|
|
|
val)) {
|
2018-10-08 18:10:25 -04:00
|
|
|
LOG_ERR("LED reg write failed");
|
2018-06-26 16:27:37 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the LED driver to be controlled through its PWMx register. */
|
2021-11-04 00:47:55 +01:00
|
|
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
2018-06-26 16:27:37 -04:00
|
|
|
PCA9633_LEDOUT,
|
|
|
|
PCA9633_MASK << (led << 1),
|
|
|
|
PCA9633_LED_PWM << (led << 1))) {
|
2018-10-08 18:10:25 -04:00
|
|
|
LOG_ERR("LED reg update failed");
|
2018-06-26 16:27:37 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline int pca9633_led_on(const struct device *dev, uint32_t led)
|
2018-06-26 16:27:37 -04:00
|
|
|
{
|
2021-11-04 00:47:55 +01:00
|
|
|
const struct pca9633_config *config = dev->config;
|
2018-06-26 16:27:37 -04:00
|
|
|
|
|
|
|
/* Set LED state to ON */
|
2021-11-04 00:47:55 +01:00
|
|
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
2018-06-26 16:27:37 -04:00
|
|
|
PCA9633_LEDOUT,
|
|
|
|
PCA9633_MASK << (led << 1),
|
|
|
|
PCA9633_LED_ON << (led << 1))) {
|
2018-10-08 18:10:25 -04:00
|
|
|
LOG_ERR("LED reg update failed");
|
2018-06-26 16:27:37 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline int pca9633_led_off(const struct device *dev, uint32_t led)
|
2018-06-26 16:27:37 -04:00
|
|
|
{
|
2021-11-04 00:47:55 +01:00
|
|
|
const struct pca9633_config *config = dev->config;
|
2018-06-26 16:27:37 -04:00
|
|
|
|
|
|
|
/* Set LED state to OFF */
|
2021-11-04 00:47:55 +01:00
|
|
|
if (i2c_reg_update_byte_dt(&config->i2c,
|
2018-06-26 16:27:37 -04:00
|
|
|
PCA9633_LEDOUT,
|
|
|
|
PCA9633_MASK << (led << 1),
|
|
|
|
PCA9633_LED_OFF)) {
|
2018-10-08 18:10:25 -04:00
|
|
|
LOG_ERR("LED reg update failed");
|
2018-06-26 16:27:37 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int pca9633_led_init(const struct device *dev)
|
2018-06-26 16:27:37 -04:00
|
|
|
{
|
2021-11-04 00:47:55 +01:00
|
|
|
const struct pca9633_config *config = dev->config;
|
2018-06-26 16:27:37 -04:00
|
|
|
|
2021-11-04 00:47:55 +01:00
|
|
|
if (!device_is_ready(config->i2c.bus)) {
|
|
|
|
LOG_ERR("I2C bus is not ready");
|
|
|
|
return -ENODEV;
|
2018-06-26 16:27:37 -04:00
|
|
|
}
|
|
|
|
|
2023-07-28 09:40:08 +02:00
|
|
|
/*
|
|
|
|
* Take the LED driver out from Sleep mode and disable All Call Address
|
|
|
|
* if specified in DT.
|
|
|
|
*/
|
|
|
|
if (i2c_reg_update_byte_dt(
|
|
|
|
&config->i2c, PCA9633_MODE1,
|
|
|
|
config->disable_allcall ? PCA9633_MODE1_SLEEP | PCA9633_MODE1_ALLCAL
|
|
|
|
: PCA9633_MODE1_SLEEP,
|
|
|
|
config->disable_allcall ? ~(PCA9633_MODE1_SLEEP | PCA9633_MODE1_ALLCAL)
|
|
|
|
: ~PCA9633_MODE1_SLEEP)) {
|
2021-07-29 13:17:29 +02:00
|
|
|
LOG_ERR("LED reg update failed");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2018-06-26 16:27:37 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-11-28 15:02:19 +02:00
|
|
|
static DEVICE_API(led, pca9633_led_api) = {
|
2018-06-26 16:27:37 -04:00
|
|
|
.blink = pca9633_led_blink,
|
|
|
|
.set_brightness = pca9633_led_set_brightness,
|
|
|
|
.on = pca9633_led_on,
|
|
|
|
.off = pca9633_led_off,
|
|
|
|
};
|
|
|
|
|
2021-11-04 00:47:55 +01:00
|
|
|
#define PCA9633_DEVICE(id) \
|
|
|
|
static const struct pca9633_config pca9633_##id##_cfg = { \
|
2023-07-28 09:40:08 +02:00
|
|
|
.i2c = I2C_DT_SPEC_INST_GET(id), \
|
|
|
|
.disable_allcall = DT_INST_PROP(id, disable_allcall), \
|
2021-11-04 00:47:55 +01:00
|
|
|
}; \
|
|
|
|
\
|
|
|
|
DEVICE_DT_INST_DEFINE(id, &pca9633_led_init, NULL, \
|
2025-05-19 22:42:00 +01:00
|
|
|
NULL, \
|
2021-11-04 00:47:55 +01:00
|
|
|
&pca9633_##id##_cfg, POST_KERNEL, \
|
|
|
|
CONFIG_LED_INIT_PRIORITY, \
|
|
|
|
&pca9633_led_api);
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(PCA9633_DEVICE)
|