2019-06-01 16:08:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-05-31 21:43:41 +08:00
|
|
|
/*
|
|
|
|
* pca9532.c - 16-bit Led dimmer
|
|
|
|
*
|
2011-05-25 08:13:26 +08:00
|
|
|
* Copyright (C) 2011 Jan Weitzel
|
2009-03-04 03:37:17 +08:00
|
|
|
* Copyright (C) 2008 Riku Voipio
|
2008-05-31 21:43:41 +08:00
|
|
|
*
|
2011-05-25 08:13:26 +08:00
|
|
|
* Datasheet: http://www.nxp.com/documents/data_sheet/PCA9532.pdf
|
2008-05-31 21:43:41 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/i2c.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2008-05-31 21:43:41 +08:00
|
|
|
#include <linux/leds.h>
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <linux/mutex.h>
|
2008-12-03 16:21:36 +08:00
|
|
|
#include <linux/workqueue.h>
|
2008-05-31 21:43:41 +08:00
|
|
|
#include <linux/leds-pca9532.h>
|
2019-08-08 21:20:20 +08:00
|
|
|
#include <linux/gpio/driver.h>
|
2016-06-14 15:36:17 +08:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
2008-05-31 21:43:41 +08:00
|
|
|
|
2011-05-25 08:13:26 +08:00
|
|
|
/* m = num_leds*/
|
|
|
|
#define PCA9532_REG_INPUT(i) ((i) >> 3)
|
|
|
|
#define PCA9532_REG_OFFSET(m) ((m) >> 4)
|
|
|
|
#define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
|
|
|
|
#define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
|
|
|
|
#define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
|
|
|
|
#define LED_NUM(led) (led & 0x3)
|
2020-09-23 03:28:35 +08:00
|
|
|
#define LED_SHIFT(led) (LED_NUM(led) * 2)
|
|
|
|
#define LED_MASK(led) (0x3 << LED_SHIFT(led))
|
2008-05-31 21:43:41 +08:00
|
|
|
|
|
|
|
#define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
|
|
|
|
|
2011-05-25 08:13:26 +08:00
|
|
|
struct pca9532_chip_info {
|
|
|
|
u8 num_leds;
|
|
|
|
};
|
|
|
|
|
2008-05-31 21:43:41 +08:00
|
|
|
struct pca9532_data {
|
|
|
|
struct i2c_client *client;
|
|
|
|
struct pca9532_led leds[16];
|
|
|
|
struct mutex update_lock;
|
2009-09-07 21:35:04 +08:00
|
|
|
struct input_dev *idev;
|
2009-06-19 19:53:07 +08:00
|
|
|
struct work_struct work;
|
2011-05-25 08:13:23 +08:00
|
|
|
#ifdef CONFIG_LEDS_PCA9532_GPIO
|
|
|
|
struct gpio_chip gpio;
|
|
|
|
#endif
|
2011-05-25 08:13:26 +08:00
|
|
|
const struct pca9532_chip_info *chip_info;
|
2008-05-31 21:43:41 +08:00
|
|
|
u8 pwm[2];
|
|
|
|
u8 psc[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int pca9532_probe(struct i2c_client *client,
|
|
|
|
const struct i2c_device_id *id);
|
|
|
|
static int pca9532_remove(struct i2c_client *client);
|
|
|
|
|
2011-05-25 08:13:26 +08:00
|
|
|
enum {
|
|
|
|
pca9530,
|
|
|
|
pca9531,
|
|
|
|
pca9532,
|
|
|
|
pca9533,
|
|
|
|
};
|
|
|
|
|
2008-05-31 21:43:41 +08:00
|
|
|
static const struct i2c_device_id pca9532_id[] = {
|
2011-05-25 08:13:26 +08:00
|
|
|
{ "pca9530", pca9530 },
|
|
|
|
{ "pca9531", pca9531 },
|
|
|
|
{ "pca9532", pca9532 },
|
|
|
|
{ "pca9533", pca9533 },
|
2008-05-31 21:43:41 +08:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(i2c, pca9532_id);
|
|
|
|
|
2011-05-25 08:13:26 +08:00
|
|
|
static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
|
|
|
|
[pca9530] = {
|
|
|
|
.num_leds = 2,
|
|
|
|
},
|
|
|
|
[pca9531] = {
|
|
|
|
.num_leds = 8,
|
|
|
|
},
|
|
|
|
[pca9532] = {
|
|
|
|
.num_leds = 16,
|
|
|
|
},
|
|
|
|
[pca9533] = {
|
|
|
|
.num_leds = 4,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-06-14 15:36:17 +08:00
|
|
|
#ifdef CONFIG_OF
|
|
|
|
static const struct of_device_id of_pca9532_leds_match[] = {
|
|
|
|
{ .compatible = "nxp,pca9530", .data = (void *)pca9530 },
|
|
|
|
{ .compatible = "nxp,pca9531", .data = (void *)pca9531 },
|
|
|
|
{ .compatible = "nxp,pca9532", .data = (void *)pca9532 },
|
|
|
|
{ .compatible = "nxp,pca9533", .data = (void *)pca9533 },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(of, of_pca9532_leds_match);
|
|
|
|
#endif
|
|
|
|
|
2008-05-31 21:43:41 +08:00
|
|
|
static struct i2c_driver pca9532_driver = {
|
|
|
|
.driver = {
|
2011-07-09 06:39:46 +08:00
|
|
|
.name = "leds-pca953x",
|
2016-06-14 15:36:17 +08:00
|
|
|
.of_match_table = of_match_ptr(of_pca9532_leds_match),
|
2008-05-31 21:43:41 +08:00
|
|
|
},
|
2009-09-07 21:35:04 +08:00
|
|
|
.probe = pca9532_probe,
|
2008-05-31 21:43:41 +08:00
|
|
|
.remove = pca9532_remove,
|
|
|
|
.id_table = pca9532_id,
|
|
|
|
};
|
|
|
|
|
2011-03-31 09:57:33 +08:00
|
|
|
/* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
|
2008-05-31 21:43:41 +08:00
|
|
|
* the clever Thecus people are using one pwm to drive the beeper. So,
|
|
|
|
* as a compromise we average one pwm to the values requested by all
|
|
|
|
* leds that are not ON/OFF.
|
|
|
|
* */
|
2008-12-03 16:21:36 +08:00
|
|
|
static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
|
2008-05-31 21:43:41 +08:00
|
|
|
enum led_brightness value)
|
|
|
|
{
|
|
|
|
int a = 0, b = 0, i = 0;
|
|
|
|
struct pca9532_data *data = i2c_get_clientdata(client);
|
2011-05-25 08:13:26 +08:00
|
|
|
for (i = 0; i < data->chip_info->num_leds; i++) {
|
2008-05-31 21:43:41 +08:00
|
|
|
if (data->leds[i].type == PCA9532_TYPE_LED &&
|
|
|
|
data->leds[i].state == PCA9532_PWM0+pwm) {
|
|
|
|
a++;
|
|
|
|
b += data->leds[i].ldev.brightness;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (a == 0) {
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"fear of division by zero %d/%d, wanted %d\n",
|
|
|
|
b, a, value);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
b = b/a;
|
|
|
|
if (b > 0xFF)
|
|
|
|
return -EINVAL;
|
|
|
|
data->pwm[pwm] = b;
|
2009-06-19 19:53:07 +08:00
|
|
|
data->psc[pwm] = blink;
|
|
|
|
return 0;
|
2008-12-03 16:21:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pca9532_setpwm(struct i2c_client *client, int pwm)
|
|
|
|
{
|
2009-06-19 19:53:07 +08:00
|
|
|
struct pca9532_data *data = i2c_get_clientdata(client);
|
2011-05-25 08:13:26 +08:00
|
|
|
u8 maxleds = data->chip_info->num_leds;
|
|
|
|
|
2009-06-19 19:53:07 +08:00
|
|
|
mutex_lock(&data->update_lock);
|
2011-05-25 08:13:26 +08:00
|
|
|
i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
|
2008-05-31 21:43:41 +08:00
|
|
|
data->pwm[pwm]);
|
2011-05-25 08:13:26 +08:00
|
|
|
i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
|
2008-05-31 21:43:41 +08:00
|
|
|
data->psc[pwm]);
|
|
|
|
mutex_unlock(&data->update_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set LED routing */
|
|
|
|
static void pca9532_setled(struct pca9532_led *led)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = led->client;
|
|
|
|
struct pca9532_data *data = i2c_get_clientdata(client);
|
2011-05-25 08:13:26 +08:00
|
|
|
u8 maxleds = data->chip_info->num_leds;
|
2008-05-31 21:43:41 +08:00
|
|
|
char reg;
|
|
|
|
|
|
|
|
mutex_lock(&data->update_lock);
|
2011-05-25 08:13:26 +08:00
|
|
|
reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
|
2008-05-31 21:43:41 +08:00
|
|
|
/* zero led bits */
|
2020-09-23 03:28:35 +08:00
|
|
|
reg = reg & ~LED_MASK(led->id);
|
2008-05-31 21:43:41 +08:00
|
|
|
/* set the new value */
|
2020-09-23 03:28:35 +08:00
|
|
|
reg = reg | (led->state << LED_SHIFT(led->id));
|
2011-05-25 08:13:26 +08:00
|
|
|
i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
|
2008-05-31 21:43:41 +08:00
|
|
|
mutex_unlock(&data->update_lock);
|
|
|
|
}
|
|
|
|
|
2015-08-20 18:08:10 +08:00
|
|
|
static int pca9532_set_brightness(struct led_classdev *led_cdev,
|
2008-05-31 21:43:41 +08:00
|
|
|
enum led_brightness value)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct pca9532_led *led = ldev_to_led(led_cdev);
|
|
|
|
|
|
|
|
if (value == LED_OFF)
|
|
|
|
led->state = PCA9532_OFF;
|
|
|
|
else if (value == LED_FULL)
|
|
|
|
led->state = PCA9532_ON;
|
|
|
|
else {
|
|
|
|
led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
|
2009-06-19 19:53:07 +08:00
|
|
|
err = pca9532_calcpwm(led->client, 0, 0, value);
|
2008-05-31 21:43:41 +08:00
|
|
|
if (err)
|
2015-08-20 18:08:10 +08:00
|
|
|
return err;
|
2008-05-31 21:43:41 +08:00
|
|
|
}
|
2015-08-20 18:08:10 +08:00
|
|
|
if (led->state == PCA9532_PWM0)
|
|
|
|
pca9532_setpwm(led->client, 0);
|
|
|
|
pca9532_setled(led);
|
|
|
|
return err;
|
2008-05-31 21:43:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pca9532_set_blink(struct led_classdev *led_cdev,
|
|
|
|
unsigned long *delay_on, unsigned long *delay_off)
|
|
|
|
{
|
|
|
|
struct pca9532_led *led = ldev_to_led(led_cdev);
|
|
|
|
struct i2c_client *client = led->client;
|
|
|
|
int psc;
|
2009-06-19 19:53:07 +08:00
|
|
|
int err = 0;
|
2008-05-31 21:43:41 +08:00
|
|
|
|
|
|
|
if (*delay_on == 0 && *delay_off == 0) {
|
2013-01-22 13:58:07 +08:00
|
|
|
/* led subsystem ask us for a blink rate */
|
2009-09-07 21:35:04 +08:00
|
|
|
*delay_on = 1000;
|
2008-05-31 21:43:41 +08:00
|
|
|
*delay_off = 1000;
|
|
|
|
}
|
|
|
|
if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Thecus specific: only use PSC/PWM 0 */
|
|
|
|
psc = (*delay_on * 152-1)/1000;
|
2009-06-19 19:53:07 +08:00
|
|
|
err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2015-08-20 18:08:10 +08:00
|
|
|
if (led->state == PCA9532_PWM0)
|
|
|
|
pca9532_setpwm(led->client, 0);
|
|
|
|
pca9532_setled(led);
|
|
|
|
|
2009-06-19 19:53:07 +08:00
|
|
|
return 0;
|
2008-05-31 21:43:41 +08:00
|
|
|
}
|
|
|
|
|
2008-11-17 22:33:41 +08:00
|
|
|
static int pca9532_event(struct input_dev *dev, unsigned int type,
|
|
|
|
unsigned int code, int value)
|
2008-05-31 21:43:41 +08:00
|
|
|
{
|
|
|
|
struct pca9532_data *data = input_get_drvdata(dev);
|
|
|
|
|
2009-03-04 04:13:06 +08:00
|
|
|
if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
|
2008-05-31 21:43:41 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* XXX: allow different kind of beeps with psc/pwm modifications */
|
|
|
|
if (value > 1 && value < 32767)
|
|
|
|
data->pwm[1] = 127;
|
|
|
|
else
|
|
|
|
data->pwm[1] = 0;
|
|
|
|
|
2009-06-19 19:53:07 +08:00
|
|
|
schedule_work(&data->work);
|
2008-12-03 16:21:36 +08:00
|
|
|
|
2009-06-19 19:53:07 +08:00
|
|
|
return 0;
|
2008-12-03 16:21:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pca9532_input_work(struct work_struct *work)
|
|
|
|
{
|
2011-05-25 08:13:26 +08:00
|
|
|
struct pca9532_data *data =
|
|
|
|
container_of(work, struct pca9532_data, work);
|
|
|
|
u8 maxleds = data->chip_info->num_leds;
|
|
|
|
|
2008-05-31 21:43:41 +08:00
|
|
|
mutex_lock(&data->update_lock);
|
2011-05-25 08:13:26 +08:00
|
|
|
i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
|
2008-05-31 21:43:41 +08:00
|
|
|
data->pwm[1]);
|
|
|
|
mutex_unlock(&data->update_lock);
|
2008-12-03 16:21:36 +08:00
|
|
|
}
|
2008-05-31 21:43:41 +08:00
|
|
|
|
2017-04-13 15:51:38 +08:00
|
|
|
static enum pca9532_state pca9532_getled(struct pca9532_led *led)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = led->client;
|
|
|
|
struct pca9532_data *data = i2c_get_clientdata(client);
|
|
|
|
u8 maxleds = data->chip_info->num_leds;
|
|
|
|
char reg;
|
|
|
|
enum pca9532_state ret;
|
|
|
|
|
|
|
|
mutex_lock(&data->update_lock);
|
|
|
|
reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
|
2020-09-23 03:28:35 +08:00
|
|
|
ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
|
2017-04-13 15:51:38 +08:00
|
|
|
mutex_unlock(&data->update_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-25 08:13:23 +08:00
|
|
|
#ifdef CONFIG_LEDS_PCA9532_GPIO
|
|
|
|
static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
|
|
|
|
{
|
2015-12-08 23:39:20 +08:00
|
|
|
struct pca9532_data *data = gpiochip_get_data(gc);
|
2011-05-25 08:13:23 +08:00
|
|
|
struct pca9532_led *led = &data->leds[offset];
|
|
|
|
|
|
|
|
if (led->type == PCA9532_TYPE_GPIO)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
|
|
|
|
{
|
2015-12-08 23:39:20 +08:00
|
|
|
struct pca9532_data *data = gpiochip_get_data(gc);
|
2011-05-25 08:13:23 +08:00
|
|
|
struct pca9532_led *led = &data->leds[offset];
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
led->state = PCA9532_ON;
|
|
|
|
else
|
|
|
|
led->state = PCA9532_OFF;
|
|
|
|
|
|
|
|
pca9532_setled(led);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
|
|
|
|
{
|
2015-12-08 23:39:20 +08:00
|
|
|
struct pca9532_data *data = gpiochip_get_data(gc);
|
2011-05-25 08:13:23 +08:00
|
|
|
unsigned char reg;
|
|
|
|
|
|
|
|
reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
|
|
|
|
|
|
|
|
return !!(reg & (1 << (offset % 8)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
|
|
|
|
{
|
|
|
|
/* To use as input ensure pin is not driven */
|
|
|
|
pca9532_gpio_set_value(gc, offset, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
|
|
|
|
{
|
|
|
|
pca9532_gpio_set_value(gc, offset, val);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_LEDS_PCA9532_GPIO */
|
|
|
|
|
|
|
|
static int pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
|
2011-01-13 08:59:15 +08:00
|
|
|
{
|
|
|
|
int i = n_devs;
|
|
|
|
|
|
|
|
if (!data)
|
2011-05-25 08:13:23 +08:00
|
|
|
return -EINVAL;
|
2011-01-13 08:59:15 +08:00
|
|
|
|
|
|
|
while (--i >= 0) {
|
|
|
|
switch (data->leds[i].type) {
|
|
|
|
case PCA9532_TYPE_NONE:
|
2011-05-25 08:13:23 +08:00
|
|
|
case PCA9532_TYPE_GPIO:
|
2011-01-13 08:59:15 +08:00
|
|
|
break;
|
|
|
|
case PCA9532_TYPE_LED:
|
|
|
|
led_classdev_unregister(&data->leds[i].ldev);
|
|
|
|
break;
|
|
|
|
case PCA9532_TYPE_N2100_BEEP:
|
|
|
|
if (data->idev != NULL) {
|
|
|
|
cancel_work_sync(&data->work);
|
|
|
|
data->idev = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-25 08:13:23 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_LEDS_PCA9532_GPIO
|
2015-11-04 16:56:26 +08:00
|
|
|
if (data->gpio.parent)
|
2014-07-13 04:30:14 +08:00
|
|
|
gpiochip_remove(&data->gpio);
|
2011-05-25 08:13:23 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
2011-01-13 08:59:15 +08:00
|
|
|
}
|
|
|
|
|
2008-05-31 21:43:41 +08:00
|
|
|
static int pca9532_configure(struct i2c_client *client,
|
|
|
|
struct pca9532_data *data, struct pca9532_platform_data *pdata)
|
|
|
|
{
|
|
|
|
int i, err = 0;
|
2011-05-25 08:13:23 +08:00
|
|
|
int gpios = 0;
|
2011-05-25 08:13:26 +08:00
|
|
|
u8 maxleds = data->chip_info->num_leds;
|
2008-05-31 21:43:41 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
data->pwm[i] = pdata->pwm[i];
|
|
|
|
data->psc[i] = pdata->psc[i];
|
2011-05-25 08:13:26 +08:00
|
|
|
i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
|
2008-05-31 21:43:41 +08:00
|
|
|
data->pwm[i]);
|
2011-05-25 08:13:26 +08:00
|
|
|
i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
|
2008-05-31 21:43:41 +08:00
|
|
|
data->psc[i]);
|
|
|
|
}
|
|
|
|
|
2011-05-25 08:13:26 +08:00
|
|
|
for (i = 0; i < data->chip_info->num_leds; i++) {
|
2008-05-31 21:43:41 +08:00
|
|
|
struct pca9532_led *led = &data->leds[i];
|
|
|
|
struct pca9532_led *pled = &pdata->leds[i];
|
|
|
|
led->client = client;
|
|
|
|
led->id = i;
|
|
|
|
led->type = pled->type;
|
|
|
|
switch (led->type) {
|
|
|
|
case PCA9532_TYPE_NONE:
|
|
|
|
break;
|
2011-05-25 08:13:23 +08:00
|
|
|
case PCA9532_TYPE_GPIO:
|
|
|
|
gpios++;
|
|
|
|
break;
|
2008-05-31 21:43:41 +08:00
|
|
|
case PCA9532_TYPE_LED:
|
2017-04-13 15:51:38 +08:00
|
|
|
if (pled->state == PCA9532_KEEP)
|
|
|
|
led->state = pca9532_getled(led);
|
|
|
|
else
|
|
|
|
led->state = pled->state;
|
2009-09-07 21:35:04 +08:00
|
|
|
led->name = pled->name;
|
2008-05-31 21:43:41 +08:00
|
|
|
led->ldev.name = led->name;
|
2016-10-26 22:08:02 +08:00
|
|
|
led->ldev.default_trigger = pled->default_trigger;
|
2008-05-31 21:43:41 +08:00
|
|
|
led->ldev.brightness = LED_OFF;
|
2015-08-20 18:08:10 +08:00
|
|
|
led->ldev.brightness_set_blocking =
|
|
|
|
pca9532_set_brightness;
|
2008-05-31 21:43:41 +08:00
|
|
|
led->ldev.blink_set = pca9532_set_blink;
|
2008-12-03 16:12:53 +08:00
|
|
|
err = led_classdev_register(&client->dev, &led->ldev);
|
|
|
|
if (err < 0) {
|
2008-05-31 21:43:41 +08:00
|
|
|
dev_err(&client->dev,
|
|
|
|
"couldn't register LED %s\n",
|
|
|
|
led->name);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
pca9532_setled(led);
|
|
|
|
break;
|
|
|
|
case PCA9532_TYPE_N2100_BEEP:
|
|
|
|
BUG_ON(data->idev);
|
|
|
|
led->state = PCA9532_PWM1;
|
|
|
|
pca9532_setled(led);
|
2012-12-25 18:16:54 +08:00
|
|
|
data->idev = devm_input_allocate_device(&client->dev);
|
2008-05-31 21:43:41 +08:00
|
|
|
if (data->idev == NULL) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
data->idev->name = pled->name;
|
|
|
|
data->idev->phys = "i2c/pca9532";
|
|
|
|
data->idev->id.bustype = BUS_HOST;
|
2009-09-07 21:35:04 +08:00
|
|
|
data->idev->id.vendor = 0x001f;
|
2008-05-31 21:43:41 +08:00
|
|
|
data->idev->id.product = 0x0001;
|
|
|
|
data->idev->id.version = 0x0100;
|
|
|
|
data->idev->evbit[0] = BIT_MASK(EV_SND);
|
|
|
|
data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
|
|
|
|
BIT_MASK(SND_TONE);
|
|
|
|
data->idev->event = pca9532_event;
|
|
|
|
input_set_drvdata(data->idev, data);
|
2009-06-19 19:53:07 +08:00
|
|
|
INIT_WORK(&data->work, pca9532_input_work);
|
2008-05-31 21:43:41 +08:00
|
|
|
err = input_register_device(data->idev);
|
|
|
|
if (err) {
|
2009-06-19 19:53:07 +08:00
|
|
|
cancel_work_sync(&data->work);
|
2008-05-31 21:43:41 +08:00
|
|
|
data->idev = NULL;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-25 08:13:23 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_LEDS_PCA9532_GPIO
|
|
|
|
if (gpios) {
|
|
|
|
data->gpio.label = "gpio-pca9532";
|
|
|
|
data->gpio.direction_input = pca9532_gpio_direction_input;
|
|
|
|
data->gpio.direction_output = pca9532_gpio_direction_output;
|
|
|
|
data->gpio.set = pca9532_gpio_set_value;
|
|
|
|
data->gpio.get = pca9532_gpio_get_value;
|
|
|
|
data->gpio.request = pca9532_gpio_request_pin;
|
|
|
|
data->gpio.can_sleep = 1;
|
|
|
|
data->gpio.base = pdata->gpio_base;
|
2011-05-25 08:13:26 +08:00
|
|
|
data->gpio.ngpio = data->chip_info->num_leds;
|
2015-11-04 16:56:26 +08:00
|
|
|
data->gpio.parent = &client->dev;
|
2011-05-25 08:13:23 +08:00
|
|
|
data->gpio.owner = THIS_MODULE;
|
|
|
|
|
2015-12-08 23:39:20 +08:00
|
|
|
err = gpiochip_add_data(&data->gpio, data);
|
2011-05-25 08:13:23 +08:00
|
|
|
if (err) {
|
|
|
|
/* Use data->gpio.dev as a flag for freeing gpiochip */
|
2015-11-04 16:56:26 +08:00
|
|
|
data->gpio.parent = NULL;
|
2011-05-25 08:13:23 +08:00
|
|
|
dev_warn(&client->dev, "could not add gpiochip\n");
|
|
|
|
} else {
|
|
|
|
dev_info(&client->dev, "gpios %i...%i\n",
|
|
|
|
data->gpio.base, data->gpio.base +
|
|
|
|
data->gpio.ngpio - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-31 21:43:41 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
exit:
|
2011-01-13 08:59:15 +08:00
|
|
|
pca9532_destroy_devices(data, i);
|
2008-05-31 21:43:41 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-06-14 15:36:17 +08:00
|
|
|
static struct pca9532_platform_data *
|
|
|
|
pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
|
|
|
|
{
|
|
|
|
struct pca9532_platform_data *pdata;
|
|
|
|
struct device_node *child;
|
|
|
|
int devid, maxleds;
|
|
|
|
int i = 0;
|
2017-04-13 15:51:38 +08:00
|
|
|
const char *state;
|
2016-06-14 15:36:17 +08:00
|
|
|
|
2019-10-05 05:43:25 +08:00
|
|
|
devid = (int)(uintptr_t)of_device_get_match_data(dev);
|
2016-06-14 15:36:17 +08:00
|
|
|
maxleds = pca9532_chip_info_tbl[devid].num_leds;
|
|
|
|
|
|
|
|
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
|
|
|
|
if (!pdata)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2021-04-14 19:51:24 +08:00
|
|
|
pdata->gpio_base = -1;
|
|
|
|
|
2020-09-23 03:31:15 +08:00
|
|
|
of_property_read_u8_array(np, "nxp,pwm", &pdata->pwm[0],
|
|
|
|
ARRAY_SIZE(pdata->pwm));
|
|
|
|
of_property_read_u8_array(np, "nxp,psc", &pdata->psc[0],
|
|
|
|
ARRAY_SIZE(pdata->psc));
|
|
|
|
|
2020-09-18 06:32:56 +08:00
|
|
|
for_each_available_child_of_node(np, child) {
|
2016-06-14 15:36:17 +08:00
|
|
|
if (of_property_read_string(child, "label",
|
|
|
|
&pdata->leds[i].name))
|
|
|
|
pdata->leds[i].name = child->name;
|
|
|
|
of_property_read_u32(child, "type", &pdata->leds[i].type);
|
|
|
|
of_property_read_string(child, "linux,default-trigger",
|
|
|
|
&pdata->leds[i].default_trigger);
|
2017-04-13 15:51:38 +08:00
|
|
|
if (!of_property_read_string(child, "default-state", &state)) {
|
|
|
|
if (!strcmp(state, "on"))
|
|
|
|
pdata->leds[i].state = PCA9532_ON;
|
|
|
|
else if (!strcmp(state, "keep"))
|
|
|
|
pdata->leds[i].state = PCA9532_KEEP;
|
|
|
|
}
|
2016-06-14 15:36:17 +08:00
|
|
|
if (++i >= maxleds) {
|
|
|
|
of_node_put(child);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pdata;
|
|
|
|
}
|
|
|
|
|
2008-05-31 21:43:41 +08:00
|
|
|
static int pca9532_probe(struct i2c_client *client,
|
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
2016-06-14 15:36:17 +08:00
|
|
|
int devid;
|
2008-05-31 21:43:41 +08:00
|
|
|
struct pca9532_data *data = i2c_get_clientdata(client);
|
2013-07-30 16:07:35 +08:00
|
|
|
struct pca9532_platform_data *pca9532_pdata =
|
|
|
|
dev_get_platdata(&client->dev);
|
2020-09-18 06:32:54 +08:00
|
|
|
struct device_node *np = dev_of_node(&client->dev);
|
2016-06-14 15:36:17 +08:00
|
|
|
|
|
|
|
if (!pca9532_pdata) {
|
|
|
|
if (np) {
|
|
|
|
pca9532_pdata =
|
|
|
|
pca9532_of_populate_pdata(&client->dev, np);
|
|
|
|
if (IS_ERR(pca9532_pdata))
|
|
|
|
return PTR_ERR(pca9532_pdata);
|
|
|
|
} else {
|
|
|
|
dev_err(&client->dev, "no platform data\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-10-05 05:43:25 +08:00
|
|
|
devid = (int)(uintptr_t)of_device_get_match_data(&client->dev);
|
2016-06-14 15:36:17 +08:00
|
|
|
} else {
|
|
|
|
devid = id->driver_data;
|
|
|
|
}
|
2008-05-31 21:43:41 +08:00
|
|
|
|
|
|
|
if (!i2c_check_functionality(client->adapter,
|
|
|
|
I2C_FUNC_SMBUS_BYTE_DATA))
|
|
|
|
return -EIO;
|
|
|
|
|
2012-07-04 11:59:19 +08:00
|
|
|
data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
|
2008-05-31 21:43:41 +08:00
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-06-14 15:36:17 +08:00
|
|
|
data->chip_info = &pca9532_chip_info_tbl[devid];
|
2011-05-25 08:13:26 +08:00
|
|
|
|
2008-05-31 21:43:41 +08:00
|
|
|
dev_info(&client->dev, "setting platform data\n");
|
|
|
|
i2c_set_clientdata(client, data);
|
|
|
|
data->client = client;
|
|
|
|
mutex_init(&data->update_lock);
|
|
|
|
|
2012-07-04 11:59:19 +08:00
|
|
|
return pca9532_configure(client, data, pca9532_pdata);
|
2008-05-31 21:43:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pca9532_remove(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
struct pca9532_data *data = i2c_get_clientdata(client);
|
2011-05-25 08:13:23 +08:00
|
|
|
|
2020-09-19 18:08:53 +08:00
|
|
|
return pca9532_destroy_devices(data, data->chip_info->num_leds);
|
2008-05-31 21:43:41 +08:00
|
|
|
}
|
|
|
|
|
2012-01-11 07:09:27 +08:00
|
|
|
module_i2c_driver(pca9532_driver);
|
2008-05-31 21:43:41 +08:00
|
|
|
|
2009-03-04 03:37:17 +08:00
|
|
|
MODULE_AUTHOR("Riku Voipio");
|
2008-05-31 21:43:41 +08:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("PCA 9532 LED dimmer");
|