cregit-Linux how code gets into the kernel

Release 4.11 drivers/leds/leds-pca963x.c

Directory: drivers/leds
/*
 * Copyright 2011 bct electronic GmbH
 * Copyright 2013 Qtechnology/AS
 *
 * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
 * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com>
 *
 * Based on leds-pca955x.c
 *
 * This file is subject to the terms and conditions of version 2 of
 * the GNU General Public License.  See the file COPYING in the main
 * directory of this archive for more details.
 *
 * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
 * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
 *
 * Note that hardware blinking violates the leds infrastructure driver
 * interface since the hardware only supports blinking all LEDs with the
 * same delay_on/delay_off rates.  That is, only the LEDs that are set to
 * blink will actually blink but all LEDs that are set to blink will blink
 * in identical fashion.  The delay_on/delay_off values of the last LED
 * that is set to blink will be used for all of the blinking LEDs.
 * Hardware blinking is disabled by default but can be enabled by setting
 * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
 * or by adding the 'nxp,hw-blink' property to the DTS.
 */

#include <linux/acpi.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/leds.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/platform_data/leds-pca963x.h>

/* LED select registers determine the source that drives LED outputs */

#define PCA963X_LED_OFF		0x0	
/* LED driver off */

#define PCA963X_LED_ON		0x1	
/* LED driver on */

#define PCA963X_LED_PWM		0x2	
/* Controlled through PWM */

#define PCA963X_LED_GRP_PWM	0x3	
/* Controlled through PWM/GRPPWM */


#define PCA963X_MODE2_DMBLNK	0x20	
/* Enable blinking */


#define PCA963X_MODE1		0x00

#define PCA963X_MODE2		0x01

#define PCA963X_PWM_BASE	0x02


enum pca963x_type {
	
pca9633,
	
pca9634,
	
pca9635,
};


struct pca963x_chipdef {
	
u8			grppwm;
	
u8			grpfreq;
	
u8			ledout_base;
	
int			n_leds;
	
unsigned int		scaling;
};


static struct pca963x_chipdef pca963x_chipdefs[] = {
	[pca9633] = {
		.grppwm		= 0x6,
		.grpfreq	= 0x7,
		.ledout_base	= 0x8,
		.n_leds		= 4,
        },
	[pca9634] = {
		.grppwm		= 0xa,
		.grpfreq	= 0xb,
		.ledout_base	= 0xc,
		.n_leds		= 8,
        },
	[pca9635] = {
		.grppwm		= 0x12,
		.grpfreq	= 0x13,
		.ledout_base	= 0x14,
		.n_leds		= 16,
        },
};

/* Total blink period in milliseconds */

#define PCA963X_BLINK_PERIOD_MIN	42

#define PCA963X_BLINK_PERIOD_MAX	10667


static const struct i2c_device_id pca963x_id[] = {
	{ "pca9632", pca9633 },
	{ "pca9633", pca9633 },
	{ "pca9634", pca9634 },
	{ "pca9635", pca9635 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, pca963x_id);


static const struct acpi_device_id pca963x_acpi_ids[] = {
	{ "PCA9632", pca9633 },
	{ "PCA9633", pca9633 },
	{ "PCA9634", pca9634 },
	{ "PCA9635", pca9635 },
	{ }
};
MODULE_DEVICE_TABLE(acpi, pca963x_acpi_ids);

struct pca963x_led;


struct pca963x {
	
struct pca963x_chipdef *chipdef;
	
struct mutex mutex;
	
struct i2c_client *client;
	
struct pca963x_led *leds;
	
unsigned long leds_on;
};


struct pca963x_led {
	
struct pca963x *chip;
	
struct led_classdev led_cdev;
	
int led_num; /* 0 .. 15 potentially */
	
char name[32];
	
u8 gdc;
	
u8 gfrq;
};


static int pca963x_brightness(struct pca963x_led *pca963x, enum led_brightness brightness) { u8 ledout_addr = pca963x->chip->chipdef->ledout_base + (pca963x->led_num / 4); u8 ledout; int shift = 2 * (pca963x->led_num % 4); u8 mask = 0x3 << shift; int ret; ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); switch (brightness) { case LED_FULL: ret = i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, (ledout & ~mask) | (PCA963X_LED_ON << shift)); break; case LED_OFF: ret = i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, ledout & ~mask); break; default: ret = i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_PWM_BASE + pca963x->led_num, brightness); if (ret < 0) return ret; ret = i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, (ledout & ~mask) | (PCA963X_LED_PWM << shift)); break; } return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Peter Meerwald-Stadler10253.68%114.29%
Ricardo Ribalda Delgado5730.00%342.86%
Andrew Lunn2613.68%114.29%
Matthew Ranostay31.58%114.29%
Mark A. Greer21.05%114.29%
Total190100.00%7100.00%


static void pca963x_blink(struct pca963x_led *pca963x) { u8 ledout_addr = pca963x->chip->chipdef->ledout_base + (pca963x->led_num / 4); u8 ledout; u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client, PCA963X_MODE2); int shift = 2 * (pca963x->led_num % 4); u8 mask = 0x3 << shift; i2c_smbus_write_byte_data(pca963x->chip->client, pca963x->chip->chipdef->grppwm, pca963x->gdc); i2c_smbus_write_byte_data(pca963x->chip->client, pca963x->chip->chipdef->grpfreq, pca963x->gfrq); if (!(mode2 & PCA963X_MODE2_DMBLNK)) i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2, mode2 | PCA963X_MODE2_DMBLNK); mutex_lock(&pca963x->chip->mutex); ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift)); mutex_unlock(&pca963x->chip->mutex); }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer10651.96%120.00%
Ricardo Ribalda Delgado9747.55%360.00%
Andrew Lunn10.49%120.00%
Total204100.00%5100.00%


static int pca963x_power_state(struct pca963x_led *pca963x) { unsigned long *leds_on = &pca963x->chip->leds_on; unsigned long cached_leds = pca963x->chip->leds_on; if (pca963x->led_cdev.brightness) set_bit(pca963x->led_num, leds_on); else clear_bit(pca963x->led_num, leds_on); if (!(*leds_on) != !cached_leds) return i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE1, *leds_on ? 0 : BIT(4)); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Matthew Ranostay96100.00%1100.00%
Total96100.00%1100.00%


static int pca963x_led_set(struct led_classdev *led_cdev, enum led_brightness value) { struct pca963x_led *pca963x; int ret; pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); mutex_lock(&pca963x->chip->mutex); ret = pca963x_brightness(pca963x, value); if (ret < 0) goto unlock; ret = pca963x_power_state(pca963x); unlock: mutex_unlock(&pca963x->chip->mutex); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Matthew Ranostay4654.12%125.00%
Peter Meerwald-Stadler2832.94%125.00%
Ricardo Ribalda Delgado67.06%125.00%
Andrew Lunn55.88%125.00%
Total85100.00%4100.00%


static unsigned int pca963x_period_scale(struct pca963x_led *pca963x, unsigned int val) { unsigned int scaling = pca963x->chip->chipdef->scaling; return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val; }

Contributors

PersonTokensPropCommitsCommitProp
Matthew Ranostay42100.00%1100.00%
Total42100.00%1100.00%


static int pca963x_blink_set(struct led_classdev *led_cdev, unsigned long *delay_on, unsigned long *delay_off) { struct pca963x_led *pca963x; unsigned long time_on, time_off, period; u8 gdc, gfrq; pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); time_on = *delay_on; time_off = *delay_off; /* If both zero, pick reasonable defaults of 500ms each */ if (!time_on && !time_off) { time_on = 500; time_off = 500; } period = pca963x_period_scale(pca963x, time_on + time_off); /* If period not supported by hardware, default to someting sane. */ if ((period < PCA963X_BLINK_PERIOD_MIN) || (period > PCA963X_BLINK_PERIOD_MAX)) { time_on = 500; time_off = 500; period = pca963x_period_scale(pca963x, 1000); } /* * From manual: duty cycle = (GDC / 256) -> * (time_on / period) = (GDC / 256) -> * GDC = ((time_on * 256) / period) */ gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period; /* * 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; pca963x->gdc = gdc; pca963x->gfrq = gfrq; pca963x_blink(pca963x); *delay_on = time_on; *delay_off = time_off; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Mark A. Greer15785.33%125.00%
Matthew Ranostay168.70%125.00%
Ricardo Ribalda Delgado105.43%125.00%
Andrew Lunn10.54%125.00%
Total184100.00%4100.00%

#if IS_ENABLED(CONFIG_OF)
static struct pca963x_platform_data * pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip) { struct device_node *np = client->dev.of_node, *child; struct pca963x_platform_data *pdata; struct led_info *pca963x_leds; int count; count = of_get_child_count(np); if (!count || count > chip->n_leds) return ERR_PTR(-ENODEV); pca963x_leds = devm_kzalloc(&client->dev, sizeof(struct led_info) * chip->n_leds, GFP_KERNEL); if (!pca963x_leds) return ERR_PTR(-ENOMEM); for_each_child_of_node(np, child) { struct led_info led = {}; u32 reg; int res; res = of_property_read_u32(child, "reg", &reg); if ((res != 0) || (reg >= chip->n_leds)) continue; led.name = of_get_property(child, "label", NULL) ? : child->name; led.default_trigger = of_get_property(child, "linux,default-trigger", NULL); pca963x_leds[reg] = led; } pdata = devm_kzalloc(&client->dev, sizeof(struct pca963x_platform_data), GFP_KERNEL); if (!pdata) return ERR_PTR(-ENOMEM); pdata->leds.leds = pca963x_leds; pdata->leds.num_leds = chip->n_leds; /* default to open-drain unless totem pole (push-pull) is specified */ if (of_property_read_bool(np, "nxp,totem-pole")) pdata->outdrv = PCA963X_TOTEM_POLE; else pdata->outdrv = PCA963X_OPEN_DRAIN; /* default to software blinking unless hardware blinking is specified */ if (of_property_read_bool(np, "nxp,hw-blink")) pdata->blink_type = PCA963X_HW_BLINK; else pdata->blink_type = PCA963X_SW_BLINK; if (of_property_read_u32(np, "nxp,period-scale", &chip->scaling)) chip->scaling = 1000; return pdata; }

Contributors

PersonTokensPropCommitsCommitProp
Tony Lindgren20967.86%114.29%
Ricardo Ribalda Delgado5618.18%342.86%
Mark A. Greer216.82%114.29%
Matthew Ranostay206.49%114.29%
Geert Uytterhoeven20.65%114.29%
Total308100.00%7100.00%

static const struct of_device_id of_pca963x_match[] = { { .compatible = "nxp,pca9632", }, { .compatible = "nxp,pca9633", }, { .compatible = "nxp,pca9634", }, { .compatible = "nxp,pca9635", }, {}, }; MODULE_DEVICE_TABLE(of, of_pca963x_match); #else
static struct pca963x_platform_data * pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip) { return ERR_PTR(-ENODEV); }

Contributors

PersonTokensPropCommitsCommitProp
Tony Lindgren1872.00%133.33%
Ricardo Ribalda Delgado728.00%266.67%
Total25100.00%3100.00%

#endif
static int pca963x_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct pca963x *pca963x_chip; struct pca963x_led *pca963x; struct pca963x_platform_data *pdata; struct pca963x_chipdef *chip; int i, err; if (id) { chip = &pca963x_chipdefs[id->driver_data]; } else { const struct acpi_device_id *acpi_id; acpi_id = acpi_match_device(pca963x_acpi_ids, &client->dev); if (!acpi_id) return -ENODEV; chip = &pca963x_chipdefs[acpi_id->driver_data]; } pdata = dev_get_platdata(&client->dev); if (!pdata) { pdata = pca963x_dt_init(client, chip); if (IS_ERR(pdata)) { dev_warn(&client->dev, "could not parse configuration\n"); pdata = NULL; } } if (pdata && (pdata->leds.num_leds < 1 || pdata->leds.num_leds > chip->n_leds)) { dev_err(&client->dev, "board info must claim 1-%d LEDs", chip->n_leds); return -EINVAL; } pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip), GFP_KERNEL); if (!pca963x_chip) return -ENOMEM; pca963x = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca963x), GFP_KERNEL); if (!pca963x) return -ENOMEM; i2c_set_clientdata(client, pca963x_chip); mutex_init(&pca963x_chip->mutex); pca963x_chip->chipdef = chip; pca963x_chip->client = client; pca963x_chip->leds = pca963x; /* Turn off LEDs by default*/ for (i = 0; i < chip->n_leds / 4; i++) i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00); for (i = 0; i < chip->n_leds; i++) { pca963x[i].led_num = i; pca963x[i].chip = pca963x_chip; /* Platform data can specify LED names and default triggers */ if (pdata && i < pdata->leds.num_leds) { if (pdata->leds.leds[i].name) snprintf(pca963x[i].name, sizeof(pca963x[i].name), "pca963x:%s", pdata->leds.leds[i].name); if (pdata->leds.leds[i].default_trigger) pca963x[i].led_cdev.default_trigger = pdata->leds.leds[i].default_trigger; } if (!pdata || i >= pdata->leds.num_leds || !pdata->leds.leds[i].name) snprintf(pca963x[i].name, sizeof(pca963x[i].name), "pca963x:%d:%.2x:%d", client->adapter->nr, client->addr, i); pca963x[i].led_cdev.name = pca963x[i].name; pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set; if (pdata && pdata->blink_type == PCA963X_HW_BLINK) pca963x[i].led_cdev.blink_set = pca963x_blink_set; err = led_classdev_register(&client->dev, &pca963x[i].led_cdev); if (err < 0) goto exit; } /* Disable LED all-call address, and power down initially */ i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4)); if (pdata) { /* Configure output: open-drain or totem pole (push-pull) */ if (pdata->outdrv == PCA963X_OPEN_DRAIN) i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x01); else i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x05); } return 0; exit: while (i--) led_classdev_unregister(&pca963x[i].led_cdev); return err; }

Contributors

PersonTokensPropCommitsCommitProp
Peter Meerwald-Stadler34753.63%426.67%
Ricardo Ribalda Delgado18428.44%426.67%
Tin Huynh467.11%16.67%
Tony Lindgren365.56%16.67%
Mark A. Greer182.78%16.67%
Bryan Wu60.93%16.67%
Matthew Ranostay50.77%16.67%
Jingoo Han40.62%16.67%
Andrew Lunn10.15%16.67%
Total647100.00%15100.00%


static int pca963x_remove(struct i2c_client *client) { struct pca963x *pca963x = i2c_get_clientdata(client); int i; for (i = 0; i < pca963x->chipdef->n_leds; i++) led_classdev_unregister(&pca963x->leds[i].led_cdev); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Peter Meerwald-Stadler4680.70%125.00%
Ricardo Ribalda Delgado1119.30%375.00%
Total57100.00%4100.00%

static struct i2c_driver pca963x_driver = { .driver = { .name = "leds-pca963x", .of_match_table = of_match_ptr(of_pca963x_match), .acpi_match_table = ACPI_PTR(pca963x_acpi_ids), }, .probe = pca963x_probe, .remove = pca963x_remove, .id_table = pca963x_id, }; module_i2c_driver(pca963x_driver); MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>"); MODULE_DESCRIPTION("PCA963X LED driver"); MODULE_LICENSE("GPL v2");

Overall Contributors

PersonTokensPropCommitsCommitProp
Peter Meerwald-Stadler70830.72%421.05%
Ricardo Ribalda Delgado59125.64%526.32%
Mark A. Greer32113.93%15.26%
Tony Lindgren29712.89%15.26%
Matthew Ranostay23610.24%210.53%
Tin Huynh994.30%15.26%
Andrew Lunn341.48%15.26%
Javier Martinez Canillas70.30%15.26%
Bryan Wu60.26%15.26%
Jingoo Han40.17%15.26%
Geert Uytterhoeven20.09%15.26%
Total2305100.00%19100.00%
Directory: drivers/leds
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.