Skip to content
Permalink
Browse files

drivers: led: Add LED driver support for NXP PCA9633

Add support for NXP PCA9633 an I2C 4-bit LED driver.
Supported blinkink period: 41ms to 10667ms
Supported brightness value: 0 to 100%

This driver supports the following APIs:

1. led_blink
2. led_set_brightness
3. led_on
4. led_off

Signed-off-by: Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
  • Loading branch information...
sbourdelin authored and MaureenHelm committed Jun 26, 2018
1 parent e325510 commit ac1a9c4ef2cde62c4ba4b7340229770a71786b5d
Showing with 263 additions and 0 deletions.
  1. +1 −0 drivers/led/CMakeLists.txt
  2. +1 −0 drivers/led/Kconfig
  3. +38 −0 drivers/led/Kconfig.pca9633
  4. +206 −0 drivers/led/pca9633.c
  5. +17 −0 dts/bindings/led/nxp,pca9633.yaml
@@ -1,3 +1,4 @@
zephyr_sources_ifdef(CONFIG_LP3943 lp3943.c)
zephyr_sources_ifdef(CONFIG_PCA9633 pca9633.c)

zephyr_sources_ifdef(CONFIG_USERSPACE led_handlers.c)
@@ -42,5 +42,6 @@ config LED_INIT_PRIORITY
System initialization priority for LED drivers.

source "drivers/led/Kconfig.lp3943"
source "drivers/led/Kconfig.pca9633"

endif # LED
@@ -0,0 +1,38 @@
#
# Copyright (c) 2018 Savoir-Faire Linux
#
# SPDX-License-Identifier: Apache-2.0
#

menuconfig PCA9633
bool "PCA9633 LED driver"
depends on I2C
default n
help
Enable LED driver for PCA9633.

PCA9633 LED driver has 4 channels each with multi-programmable
states. Each channel can drive up to 25 mA per LED.

if !HAS_DTS_I2C_DEVICE

config PCA9633_DEV_NAME
string "PCA9633 device name"
default "PCA9633"
help
Device name for PCA9633 LED driver.

config PCA9633_I2C_ADDRESS
hex "PCA9633 I2C slave address"
default 0x62
help
Specify the I2C slave address for the PCA9633 LED driver.

config PCA9633_I2C_MASTER_DEV_NAME
string "I2C master where PCA9633 is connected"
default "I2C_1"
help
Specify the device name of the I2C master device to which
PCA9633 is connected.

endif
@@ -0,0 +1,206 @@
/*
* Copyright (c) 2018 Savoir-Faire Linux.
*
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @file
* @brief LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
*/

#include <i2c.h>
#include <led.h>
#include <misc/util.h>
#include <zephyr.h>

#define SYS_LOG_LEVEL CONFIG_SYS_LOG_LED_LEVEL
#include <logging/sys_log.h>

#include "led_context.h"

/* 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
#define PCA9633_PWM_BASE 0x02
#define PCA9633_GRPPWM 0x06
#define PCA9633_GRPFREQ 0x07
#define PCA9633_LEDOUT 0x08

/* PCA9633 mode register 2 */
#define PCA9633_MODE2_DMBLNK 0x20 /* Enable blinking */

#define PCA9633_MASK 0x03

struct pca9633_data {
struct device *i2c;
struct led_data dev_data;
};

static int pca9633_led_blink(struct device *dev, u32_t led,
u32_t delay_on, u32_t delay_off)
{
struct pca9633_data *data = dev->driver_data;
struct led_data *dev_data = &data->dev_data;
u8_t gdc, gfrq;
u32_t period;

period = delay_on + delay_off;

if (period < dev_data->min_period || period > dev_data->max_period) {
return -EINVAL;
}

/*
* From manual:
* duty cycle = (GDC / 256) ->
* (time_on / period) = (GDC / 256) ->
* GDC = ((time_on * 256) / period)
*/
gdc = delay_on * 256 / period;
if (i2c_reg_write_byte(data->i2c, CONFIG_PCA9633_I2C_ADDRESS,
PCA9633_GRPPWM,
gdc)) {
SYS_LOG_ERR("LED reg write failed");
return -EIO;
}

/*
* From manual:
* period = ((GFRQ + 1) / 24) in seconds.
* So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
* GFRQ = ((period * 24 / 1000) - 1)
*/
gfrq = (period * 24 / 1000) - 1;
if (i2c_reg_write_byte(data->i2c, CONFIG_PCA9633_I2C_ADDRESS,
PCA9633_GRPFREQ,
gfrq)) {
SYS_LOG_ERR("LED reg write failed");
return -EIO;
}

/* Enable blinking mode */
if (i2c_reg_update_byte(data->i2c, CONFIG_PCA9633_I2C_ADDRESS,
PCA9633_MODE2,
PCA9633_MODE2_DMBLNK,
PCA9633_MODE2_DMBLNK)) {
SYS_LOG_ERR("LED reg update failed");
return -EIO;
}

/* Select the GRPPWM source to drive the LED outpout */
if (i2c_reg_update_byte(data->i2c, CONFIG_PCA9633_I2C_ADDRESS,
PCA9633_LEDOUT,
PCA9633_MASK << (led << 1),
PCA9633_LED_GRP_PWM << (led << 1))) {
SYS_LOG_ERR("LED reg update failed");
return -EIO;
}

return 0;
}

static int pca9633_led_set_brightness(struct device *dev, u32_t led,
u8_t value)
{
struct pca9633_data *data = dev->driver_data;
struct led_data *dev_data = &data->dev_data;
u8_t val;

if (value < dev_data->min_brightness ||
value > dev_data->max_brightness) {
return -EINVAL;
}

/* Set the LED brightness value */
val = (value * 255) / dev_data->max_brightness;
if (i2c_reg_write_byte(data->i2c, CONFIG_PCA9633_I2C_ADDRESS,
PCA9633_PWM_BASE + led,
val)) {
SYS_LOG_ERR("LED reg write failed");
return -EIO;
}

/* Set the LED driver to be controlled through its PWMx register. */
if (i2c_reg_update_byte(data->i2c, CONFIG_PCA9633_I2C_ADDRESS,
PCA9633_LEDOUT,
PCA9633_MASK << (led << 1),
PCA9633_LED_PWM << (led << 1))) {
SYS_LOG_ERR("LED reg update failed");
return -EIO;
}

return 0;
}

static inline int pca9633_led_on(struct device *dev, u32_t led)
{
struct pca9633_data *data = dev->driver_data;

/* Set LED state to ON */
if (i2c_reg_update_byte(data->i2c, CONFIG_PCA9633_I2C_ADDRESS,
PCA9633_LEDOUT,
PCA9633_MASK << (led << 1),
PCA9633_LED_ON << (led << 1))) {
SYS_LOG_ERR("LED reg update failed");
return -EIO;
}

return 0;
}

static inline int pca9633_led_off(struct device *dev, u32_t led)
{
struct pca9633_data *data = dev->driver_data;

/* Set LED state to OFF */
if (i2c_reg_update_byte(data->i2c, CONFIG_PCA9633_I2C_ADDRESS,
PCA9633_LEDOUT,
PCA9633_MASK << (led << 1),
PCA9633_LED_OFF)) {
SYS_LOG_ERR("LED reg update failed");
return -EIO;
}

return 0;
}

static int pca9633_led_init(struct device *dev)
{
struct pca9633_data *data = dev->driver_data;
struct led_data *dev_data = &data->dev_data;

data->i2c = device_get_binding(CONFIG_PCA9633_I2C_MASTER_DEV_NAME);
if (data->i2c == NULL) {
SYS_LOG_DBG("Failed to get I2C device");
return -EINVAL;
}

/* Hardware specific limits */
dev_data->min_period = 41;
dev_data->max_period = 10667;
dev_data->min_brightness = 0;
dev_data->max_brightness = 100;

return 0;
}

static struct pca9633_data pca9633_led_data;

static const struct led_driver_api pca9633_led_api = {
.blink = pca9633_led_blink,
.set_brightness = pca9633_led_set_brightness,
.on = pca9633_led_on,
.off = pca9633_led_off,
};

DEVICE_AND_API_INIT(pca9633_led, CONFIG_PCA9633_DEV_NAME,
&pca9633_led_init, &pca9633_led_data,
NULL, POST_KERNEL, CONFIG_LED_INIT_PRIORITY,
&pca9633_led_api);
@@ -0,0 +1,17 @@
---
title: NXP PCA9633 LED Driver
id: nxp,pca9633
version: 0.1

description: NXP PCA9633 LED binding

inherits:
!include i2c-device.yaml

properties:
compatible:
type: string
category: required
description: compatible strings
constraint: "nxp,pca9633"
...

0 comments on commit ac1a9c4

Please sign in to comment.
You can’t perform that action at this time.