2019-05-29 23:57:44 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2012-03-23 22:02:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 bct electronic GmbH
|
2013-08-14 21:23:47 +00:00
|
|
|
* Copyright 2013 Qtechnology/AS
|
2012-03-23 22:02:10 +00:00
|
|
|
*
|
|
|
|
* Author: Peter Meerwald <p.meerwald@bct-electronic.com>
|
2013-08-14 21:23:47 +00:00
|
|
|
* Author: Ricardo Ribalda <ricardo.ribalda@gmail.com>
|
2012-03-23 22:02:10 +00:00
|
|
|
*
|
|
|
|
* Based on leds-pca955x.c
|
|
|
|
*
|
|
|
|
* LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
|
2014-07-03 05:50:34 +00:00
|
|
|
* LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
|
2012-03-23 22:02:10 +00:00
|
|
|
*
|
2013-07-25 17:16:41 +00:00
|
|
|
* 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
|
2013-08-14 21:23:50 +00:00
|
|
|
* the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
|
2013-07-25 17:16:41 +00:00
|
|
|
* or by adding the 'nxp,hw-blink' property to the DTS.
|
2012-03-23 22:02:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2019-03-25 14:02:07 +00:00
|
|
|
#include <linux/property.h>
|
2012-03-23 22:02:10 +00:00
|
|
|
#include <linux/slab.h>
|
2013-06-26 12:52:49 +00:00
|
|
|
#include <linux/of.h>
|
2013-08-14 21:23:50 +00:00
|
|
|
#include <linux/platform_data/leds-pca963x.h>
|
2012-03-23 22:02:10 +00:00
|
|
|
|
|
|
|
/* LED select registers determine the source that drives LED outputs */
|
2013-08-14 21:23:50 +00:00
|
|
|
#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 */
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2019-11-18 21:02:55 +00:00
|
|
|
#define PCA963X_MODE2_OUTDRV 0x04 /* Open-drain or totem pole */
|
|
|
|
#define PCA963X_MODE2_INVRT 0x10 /* Normal or inverted direction */
|
2013-08-14 21:23:50 +00:00
|
|
|
#define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
|
2013-07-25 17:16:41 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
#define PCA963X_MODE1 0x00
|
|
|
|
#define PCA963X_MODE2 0x01
|
|
|
|
#define PCA963X_PWM_BASE 0x02
|
2013-08-14 21:23:47 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
enum pca963x_type {
|
2013-08-14 21:23:47 +00:00
|
|
|
pca9633,
|
|
|
|
pca9634,
|
2014-07-03 05:50:34 +00:00
|
|
|
pca9635,
|
2013-08-14 21:23:47 +00:00
|
|
|
};
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
struct pca963x_chipdef {
|
2013-08-14 21:23:47 +00:00
|
|
|
u8 grppwm;
|
|
|
|
u8 grpfreq;
|
|
|
|
u8 ledout_base;
|
|
|
|
int n_leds;
|
2016-10-13 13:16:12 +00:00
|
|
|
unsigned int scaling;
|
2013-08-14 21:23:47 +00:00
|
|
|
};
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
static struct pca963x_chipdef pca963x_chipdefs[] = {
|
2013-08-14 21:23:47 +00:00
|
|
|
[pca9633] = {
|
|
|
|
.grppwm = 0x6,
|
|
|
|
.grpfreq = 0x7,
|
|
|
|
.ledout_base = 0x8,
|
|
|
|
.n_leds = 4,
|
|
|
|
},
|
|
|
|
[pca9634] = {
|
|
|
|
.grppwm = 0xa,
|
|
|
|
.grpfreq = 0xb,
|
|
|
|
.ledout_base = 0xc,
|
|
|
|
.n_leds = 8,
|
|
|
|
},
|
2014-07-03 05:50:34 +00:00
|
|
|
[pca9635] = {
|
|
|
|
.grppwm = 0x12,
|
|
|
|
.grpfreq = 0x13,
|
|
|
|
.ledout_base = 0x14,
|
|
|
|
.n_leds = 16,
|
|
|
|
},
|
2013-08-14 21:23:47 +00:00
|
|
|
};
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2013-07-25 17:16:41 +00:00
|
|
|
/* Total blink period in milliseconds */
|
2013-08-14 21:23:50 +00:00
|
|
|
#define PCA963X_BLINK_PERIOD_MIN 42
|
|
|
|
#define PCA963X_BLINK_PERIOD_MAX 10667
|
2013-07-25 17:16:41 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
static const struct i2c_device_id pca963x_id[] = {
|
2013-08-14 21:23:47 +00:00
|
|
|
{ "pca9632", pca9633 },
|
|
|
|
{ "pca9633", pca9633 },
|
|
|
|
{ "pca9634", pca9634 },
|
2014-07-03 05:50:34 +00:00
|
|
|
{ "pca9635", pca9635 },
|
2012-03-23 22:02:10 +00:00
|
|
|
{ }
|
|
|
|
};
|
2013-08-14 21:23:50 +00:00
|
|
|
MODULE_DEVICE_TABLE(i2c, pca963x_id);
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
struct pca963x_led;
|
2013-08-14 21:23:49 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
struct pca963x {
|
|
|
|
struct pca963x_chipdef *chipdef;
|
2013-08-14 21:23:49 +00:00
|
|
|
struct mutex mutex;
|
|
|
|
struct i2c_client *client;
|
2013-08-14 21:23:50 +00:00
|
|
|
struct pca963x_led *leds;
|
2016-10-29 01:20:42 +00:00
|
|
|
unsigned long leds_on;
|
2013-08-14 21:23:49 +00:00
|
|
|
};
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
struct pca963x_led {
|
|
|
|
struct pca963x *chip;
|
2012-03-23 22:02:10 +00:00
|
|
|
struct led_classdev led_cdev;
|
2014-07-03 05:50:34 +00:00
|
|
|
int led_num; /* 0 .. 15 potentially */
|
2012-03-23 22:02:10 +00:00
|
|
|
char name[32];
|
2013-07-25 17:16:41 +00:00
|
|
|
u8 gdc;
|
|
|
|
u8 gfrq;
|
2012-03-23 22:02:10 +00:00
|
|
|
};
|
|
|
|
|
2015-08-20 10:38:44 +00:00
|
|
|
static int pca963x_brightness(struct pca963x_led *pca963x,
|
|
|
|
enum led_brightness brightness)
|
2012-03-23 22:02:10 +00:00
|
|
|
{
|
2013-08-14 21:23:50 +00:00
|
|
|
u8 ledout_addr = pca963x->chip->chipdef->ledout_base
|
|
|
|
+ (pca963x->led_num / 4);
|
2013-08-14 21:23:47 +00:00
|
|
|
u8 ledout;
|
2013-08-14 21:23:50 +00:00
|
|
|
int shift = 2 * (pca963x->led_num % 4);
|
2012-03-23 22:02:10 +00:00
|
|
|
u8 mask = 0x3 << shift;
|
2015-08-20 10:38:44 +00:00
|
|
|
int ret;
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
|
2015-08-20 10:38:44 +00:00
|
|
|
switch (brightness) {
|
2012-03-23 22:02:10 +00:00
|
|
|
case LED_FULL:
|
2015-08-20 10:38:44 +00:00
|
|
|
ret = i2c_smbus_write_byte_data(pca963x->chip->client,
|
|
|
|
ledout_addr,
|
2013-08-14 21:23:50 +00:00
|
|
|
(ledout & ~mask) | (PCA963X_LED_ON << shift));
|
2012-03-23 22:02:10 +00:00
|
|
|
break;
|
|
|
|
case LED_OFF:
|
2015-08-20 10:38:44 +00:00
|
|
|
ret = i2c_smbus_write_byte_data(pca963x->chip->client,
|
|
|
|
ledout_addr, ledout & ~mask);
|
2012-03-23 22:02:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-08-20 10:38:44 +00:00
|
|
|
ret = i2c_smbus_write_byte_data(pca963x->chip->client,
|
2013-08-14 21:23:50 +00:00
|
|
|
PCA963X_PWM_BASE + pca963x->led_num,
|
2015-08-20 10:38:44 +00:00
|
|
|
brightness);
|
|
|
|
if (ret < 0)
|
2016-10-29 01:20:42 +00:00
|
|
|
return ret;
|
2015-08-20 10:38:44 +00:00
|
|
|
ret = i2c_smbus_write_byte_data(pca963x->chip->client,
|
|
|
|
ledout_addr,
|
2013-08-14 21:23:50 +00:00
|
|
|
(ledout & ~mask) | (PCA963X_LED_PWM << shift));
|
2012-03-23 22:02:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-10-29 01:20:42 +00:00
|
|
|
|
2015-08-20 10:38:44 +00:00
|
|
|
return ret;
|
2012-03-23 22:02:10 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 10:38:44 +00:00
|
|
|
static void pca963x_blink(struct pca963x_led *pca963x)
|
2013-07-25 17:16:41 +00:00
|
|
|
{
|
2013-08-14 21:23:50 +00:00
|
|
|
u8 ledout_addr = pca963x->chip->chipdef->ledout_base +
|
|
|
|
(pca963x->led_num / 4);
|
2013-08-14 21:23:49 +00:00
|
|
|
u8 ledout;
|
2013-08-14 21:23:50 +00:00
|
|
|
u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
|
|
|
|
PCA963X_MODE2);
|
|
|
|
int shift = 2 * (pca963x->led_num % 4);
|
2013-07-25 17:16:41 +00:00
|
|
|
u8 mask = 0x3 << shift;
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
i2c_smbus_write_byte_data(pca963x->chip->client,
|
|
|
|
pca963x->chip->chipdef->grppwm, pca963x->gdc);
|
2013-07-25 17:16:41 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
i2c_smbus_write_byte_data(pca963x->chip->client,
|
|
|
|
pca963x->chip->chipdef->grpfreq, pca963x->gfrq);
|
2013-07-25 17:16:41 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
if (!(mode2 & PCA963X_MODE2_DMBLNK))
|
|
|
|
i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
|
|
|
|
mode2 | PCA963X_MODE2_DMBLNK);
|
2013-07-25 17:16:41 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
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);
|
2013-07-25 17:16:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-29 01:20:42 +00: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;
|
|
|
|
}
|
|
|
|
|
2015-08-20 10:38:44 +00:00
|
|
|
static int pca963x_led_set(struct led_classdev *led_cdev,
|
2012-03-23 22:02:10 +00:00
|
|
|
enum led_brightness value)
|
|
|
|
{
|
2013-08-14 21:23:50 +00:00
|
|
|
struct pca963x_led *pca963x;
|
2016-10-29 01:20:42 +00:00
|
|
|
int ret;
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2016-10-29 01:20:42 +00:00
|
|
|
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;
|
2012-03-23 22:02:10 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 13:16:12 +00: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;
|
|
|
|
}
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
static int pca963x_blink_set(struct led_classdev *led_cdev,
|
2013-07-25 17:16:41 +00:00
|
|
|
unsigned long *delay_on, unsigned long *delay_off)
|
|
|
|
{
|
2013-08-14 21:23:50 +00:00
|
|
|
struct pca963x_led *pca963x;
|
2013-07-25 17:16:41 +00:00
|
|
|
unsigned long time_on, time_off, period;
|
|
|
|
u8 gdc, gfrq;
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
|
2013-07-25 17:16:41 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-13 13:16:12 +00:00
|
|
|
period = pca963x_period_scale(pca963x, time_on + time_off);
|
2013-07-25 17:16:41 +00:00
|
|
|
|
|
|
|
/* If period not supported by hardware, default to someting sane. */
|
2013-08-14 21:23:50 +00:00
|
|
|
if ((period < PCA963X_BLINK_PERIOD_MIN) ||
|
|
|
|
(period > PCA963X_BLINK_PERIOD_MAX)) {
|
2013-07-25 17:16:41 +00:00
|
|
|
time_on = 500;
|
|
|
|
time_off = 500;
|
2016-10-13 13:16:12 +00:00
|
|
|
period = pca963x_period_scale(pca963x, 1000);
|
2013-07-25 17:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* From manual: duty cycle = (GDC / 256) ->
|
|
|
|
* (time_on / period) = (GDC / 256) ->
|
|
|
|
* GDC = ((time_on * 256) / period)
|
|
|
|
*/
|
2016-10-13 13:16:12 +00:00
|
|
|
gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period;
|
2013-07-25 17:16:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
pca963x->gdc = gdc;
|
|
|
|
pca963x->gfrq = gfrq;
|
2013-07-25 17:16:41 +00:00
|
|
|
|
2015-08-20 10:38:44 +00:00
|
|
|
pca963x_blink(pca963x);
|
2013-07-25 17:16:41 +00:00
|
|
|
|
|
|
|
*delay_on = time_on;
|
|
|
|
*delay_off = time_off;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
static struct pca963x_platform_data *
|
2019-03-25 14:02:07 +00:00
|
|
|
pca963x_get_pdata(struct i2c_client *client, struct pca963x_chipdef *chip)
|
2013-06-26 12:52:49 +00:00
|
|
|
{
|
2013-08-14 21:23:50 +00:00
|
|
|
struct pca963x_platform_data *pdata;
|
|
|
|
struct led_info *pca963x_leds;
|
2019-03-25 14:02:07 +00:00
|
|
|
struct fwnode_handle *child;
|
2013-06-26 12:52:49 +00:00
|
|
|
int count;
|
|
|
|
|
2019-03-25 14:02:07 +00:00
|
|
|
count = device_get_child_node_count(&client->dev);
|
2013-08-14 21:23:47 +00:00
|
|
|
if (!count || count > chip->n_leds)
|
2013-06-26 12:52:49 +00:00
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 21:07:58 +00:00
|
|
|
pca963x_leds = devm_kcalloc(&client->dev,
|
|
|
|
chip->n_leds, sizeof(struct led_info), GFP_KERNEL);
|
2013-08-14 21:23:50 +00:00
|
|
|
if (!pca963x_leds)
|
2013-06-26 12:52:49 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2019-03-25 14:02:07 +00:00
|
|
|
device_for_each_child_node(&client->dev, child) {
|
2015-03-15 10:41:52 +00:00
|
|
|
struct led_info led = {};
|
2013-06-26 12:52:49 +00:00
|
|
|
u32 reg;
|
|
|
|
int res;
|
|
|
|
|
2019-03-25 14:02:07 +00:00
|
|
|
res = fwnode_property_read_u32(child, "reg", ®);
|
2013-08-14 21:23:51 +00:00
|
|
|
if ((res != 0) || (reg >= chip->n_leds))
|
|
|
|
continue;
|
2019-03-25 14:02:07 +00:00
|
|
|
|
|
|
|
res = fwnode_property_read_string(child, "label", &led.name);
|
|
|
|
if ((res != 0) && is_of_node(child))
|
|
|
|
led.name = to_of_node(child)->name;
|
|
|
|
|
|
|
|
fwnode_property_read_string(child, "linux,default-trigger",
|
|
|
|
&led.default_trigger);
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
pca963x_leds[reg] = led;
|
2013-06-26 12:52:49 +00:00
|
|
|
}
|
|
|
|
pdata = devm_kzalloc(&client->dev,
|
2013-08-14 21:23:50 +00:00
|
|
|
sizeof(struct pca963x_platform_data), GFP_KERNEL);
|
2013-06-26 12:52:49 +00:00
|
|
|
if (!pdata)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
pdata->leds.leds = pca963x_leds;
|
2013-08-14 21:23:51 +00:00
|
|
|
pdata->leds.num_leds = chip->n_leds;
|
2013-06-26 12:52:49 +00:00
|
|
|
|
|
|
|
/* default to open-drain unless totem pole (push-pull) is specified */
|
2019-03-25 14:02:07 +00:00
|
|
|
if (device_property_read_bool(&client->dev, "nxp,totem-pole"))
|
2013-08-14 21:23:50 +00:00
|
|
|
pdata->outdrv = PCA963X_TOTEM_POLE;
|
2013-06-26 12:52:49 +00:00
|
|
|
else
|
2013-08-14 21:23:50 +00:00
|
|
|
pdata->outdrv = PCA963X_OPEN_DRAIN;
|
2013-06-26 12:52:49 +00:00
|
|
|
|
2013-07-25 17:16:41 +00:00
|
|
|
/* default to software blinking unless hardware blinking is specified */
|
2019-03-25 14:02:07 +00:00
|
|
|
if (device_property_read_bool(&client->dev, "nxp,hw-blink"))
|
2013-08-14 21:23:50 +00:00
|
|
|
pdata->blink_type = PCA963X_HW_BLINK;
|
2013-07-25 17:16:41 +00:00
|
|
|
else
|
2013-08-14 21:23:50 +00:00
|
|
|
pdata->blink_type = PCA963X_SW_BLINK;
|
2013-07-25 17:16:41 +00:00
|
|
|
|
2019-03-25 14:02:07 +00:00
|
|
|
if (device_property_read_u32(&client->dev, "nxp,period-scale",
|
|
|
|
&chip->scaling))
|
2016-10-13 13:16:12 +00:00
|
|
|
chip->scaling = 1000;
|
|
|
|
|
2017-04-27 06:37:33 +00:00
|
|
|
/* default to non-inverted output, unless inverted is specified */
|
2019-03-25 14:02:07 +00:00
|
|
|
if (device_property_read_bool(&client->dev, "nxp,inverted-out"))
|
2017-04-27 06:37:33 +00:00
|
|
|
pdata->dir = PCA963X_INVERTED;
|
|
|
|
else
|
|
|
|
pdata->dir = PCA963X_NORMAL;
|
|
|
|
|
2013-06-26 12:52:49 +00:00
|
|
|
return pdata;
|
|
|
|
}
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
static const struct of_device_id of_pca963x_match[] = {
|
2013-08-14 21:23:47 +00:00
|
|
|
{ .compatible = "nxp,pca9632", },
|
|
|
|
{ .compatible = "nxp,pca9633", },
|
|
|
|
{ .compatible = "nxp,pca9634", },
|
2014-07-03 05:50:34 +00:00
|
|
|
{ .compatible = "nxp,pca9635", },
|
2013-06-26 12:52:49 +00:00
|
|
|
{},
|
|
|
|
};
|
2015-08-25 06:31:16 +00:00
|
|
|
MODULE_DEVICE_TABLE(of, of_pca963x_match);
|
2013-06-26 12:52:49 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
static int pca963x_probe(struct i2c_client *client,
|
2012-03-23 22:02:10 +00:00
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
2013-08-14 21:23:50 +00:00
|
|
|
struct pca963x *pca963x_chip;
|
|
|
|
struct pca963x_led *pca963x;
|
|
|
|
struct pca963x_platform_data *pdata;
|
|
|
|
struct pca963x_chipdef *chip;
|
2012-03-23 22:02:10 +00:00
|
|
|
int i, err;
|
|
|
|
|
2019-03-15 19:13:41 +00:00
|
|
|
chip = &pca963x_chipdefs[id->driver_data];
|
2013-07-30 08:07:35 +00:00
|
|
|
pdata = dev_get_platdata(&client->dev);
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2013-06-26 12:52:49 +00:00
|
|
|
if (!pdata) {
|
2019-03-25 14:02:07 +00:00
|
|
|
pdata = pca963x_get_pdata(client, chip);
|
2013-06-26 12:52:49 +00:00
|
|
|
if (IS_ERR(pdata)) {
|
|
|
|
dev_warn(&client->dev, "could not parse configuration\n");
|
|
|
|
pdata = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 21:23:47 +00:00
|
|
|
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;
|
2012-03-23 22:02:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
|
2013-08-14 21:23:49 +00:00
|
|
|
GFP_KERNEL);
|
2013-08-14 21:23:50 +00:00
|
|
|
if (!pca963x_chip)
|
2013-08-14 21:23:49 +00:00
|
|
|
return -ENOMEM;
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 21:07:58 +00:00
|
|
|
pca963x = devm_kcalloc(&client->dev, chip->n_leds, sizeof(*pca963x),
|
2013-08-14 21:23:47 +00:00
|
|
|
GFP_KERNEL);
|
2013-08-14 21:23:50 +00:00
|
|
|
if (!pca963x)
|
2012-03-23 22:02:10 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
i2c_set_clientdata(client, pca963x_chip);
|
2013-08-14 21:23:49 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
mutex_init(&pca963x_chip->mutex);
|
|
|
|
pca963x_chip->chipdef = chip;
|
|
|
|
pca963x_chip->client = client;
|
|
|
|
pca963x_chip->leds = pca963x;
|
2013-08-14 21:23:49 +00:00
|
|
|
|
|
|
|
/* Turn off LEDs by default*/
|
2014-07-03 05:50:34 +00:00
|
|
|
for (i = 0; i < chip->n_leds / 4; i++)
|
|
|
|
i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00);
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2013-08-14 21:23:47 +00:00
|
|
|
for (i = 0; i < chip->n_leds; i++) {
|
2013-08-14 21:23:50 +00:00
|
|
|
pca963x[i].led_num = i;
|
|
|
|
pca963x[i].chip = pca963x_chip;
|
2012-03-23 22:02:10 +00:00
|
|
|
|
|
|
|
/* Platform data can specify LED names and default triggers */
|
2012-09-24 06:53:49 +00:00
|
|
|
if (pdata && i < pdata->leds.num_leds) {
|
|
|
|
if (pdata->leds.leds[i].name)
|
2013-08-14 21:23:50 +00:00
|
|
|
snprintf(pca963x[i].name,
|
|
|
|
sizeof(pca963x[i].name), "pca963x:%s",
|
2012-09-24 06:53:49 +00:00
|
|
|
pdata->leds.leds[i].name);
|
|
|
|
if (pdata->leds.leds[i].default_trigger)
|
2013-08-14 21:23:50 +00:00
|
|
|
pca963x[i].led_cdev.default_trigger =
|
2012-09-24 06:53:49 +00:00
|
|
|
pdata->leds.leds[i].default_trigger;
|
2012-03-23 22:02:10 +00:00
|
|
|
}
|
2013-08-14 21:23:48 +00:00
|
|
|
if (!pdata || i >= pdata->leds.num_leds ||
|
|
|
|
!pdata->leds.leds[i].name)
|
2013-08-14 21:23:50 +00:00
|
|
|
snprintf(pca963x[i].name, sizeof(pca963x[i].name),
|
|
|
|
"pca963x:%d:%.2x:%d", client->adapter->nr,
|
2013-08-14 21:23:48 +00:00
|
|
|
client->addr, i);
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
pca963x[i].led_cdev.name = pca963x[i].name;
|
2015-08-20 10:38:44 +00:00
|
|
|
pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set;
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
|
|
|
|
pca963x[i].led_cdev.blink_set = pca963x_blink_set;
|
2013-07-25 17:16:41 +00:00
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
|
2012-03-23 22:02:10 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2016-10-29 01:20:42 +00:00
|
|
|
/* Disable LED all-call address, and power down initially */
|
|
|
|
i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
|
2012-03-23 22:02:10 +00:00
|
|
|
|
2014-07-03 05:50:35 +00:00
|
|
|
if (pdata) {
|
2017-04-27 06:37:33 +00:00
|
|
|
u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
|
|
|
|
PCA963X_MODE2);
|
2014-07-03 05:50:35 +00:00
|
|
|
/* Configure output: open-drain or totem pole (push-pull) */
|
|
|
|
if (pdata->outdrv == PCA963X_OPEN_DRAIN)
|
2019-11-18 21:02:55 +00:00
|
|
|
mode2 &= ~PCA963X_MODE2_OUTDRV;
|
2014-07-03 05:50:35 +00:00
|
|
|
else
|
2019-11-18 21:02:55 +00:00
|
|
|
mode2 |= PCA963X_MODE2_OUTDRV;
|
2017-04-27 06:37:33 +00:00
|
|
|
/* Configure direction: normal or inverted */
|
|
|
|
if (pdata->dir == PCA963X_INVERTED)
|
2019-11-18 21:02:55 +00:00
|
|
|
mode2 |= PCA963X_MODE2_INVRT;
|
2017-04-27 06:37:33 +00:00
|
|
|
i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
|
|
|
|
mode2);
|
2014-07-03 05:50:35 +00:00
|
|
|
}
|
2012-09-24 06:53:49 +00:00
|
|
|
|
2012-03-23 22:02:10 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
exit:
|
2015-08-20 10:38:44 +00:00
|
|
|
while (i--)
|
2013-08-14 21:23:50 +00:00
|
|
|
led_classdev_unregister(&pca963x[i].led_cdev);
|
2012-03-23 22:02:10 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
static int pca963x_remove(struct i2c_client *client)
|
2012-03-23 22:02:10 +00:00
|
|
|
{
|
2013-08-14 21:23:50 +00:00
|
|
|
struct pca963x *pca963x = i2c_get_clientdata(client);
|
2012-03-23 22:02:10 +00:00
|
|
|
int i;
|
|
|
|
|
2015-08-20 10:38:44 +00:00
|
|
|
for (i = 0; i < pca963x->chipdef->n_leds; i++)
|
2013-08-14 21:23:50 +00:00
|
|
|
led_classdev_unregister(&pca963x->leds[i].led_cdev);
|
2012-03-23 22:02:10 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
static struct i2c_driver pca963x_driver = {
|
2012-03-23 22:02:10 +00:00
|
|
|
.driver = {
|
2013-08-14 21:23:50 +00:00
|
|
|
.name = "leds-pca963x",
|
2019-03-25 14:02:07 +00:00
|
|
|
.of_match_table = of_pca963x_match,
|
2012-03-23 22:02:10 +00:00
|
|
|
},
|
2013-08-14 21:23:50 +00:00
|
|
|
.probe = pca963x_probe,
|
|
|
|
.remove = pca963x_remove,
|
|
|
|
.id_table = pca963x_id,
|
2012-03-23 22:02:10 +00:00
|
|
|
};
|
|
|
|
|
2013-08-14 21:23:50 +00:00
|
|
|
module_i2c_driver(pca963x_driver);
|
2012-03-23 22:02:10 +00:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
|
2013-08-14 21:23:50 +00:00
|
|
|
MODULE_DESCRIPTION("PCA963X LED driver");
|
2012-03-23 22:02:10 +00:00
|
|
|
MODULE_LICENSE("GPL v2");
|