2 * Copyright 2011 bct electronic GmbH
4 * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
6 * Based on leds-pca955x.c
8 * This file is subject to the terms and conditions of version 2 of
9 * the GNU General Public License. See the file COPYING in the main
10 * directory of this archive for more details.
12 * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/string.h>
19 #include <linux/ctype.h>
20 #include <linux/leds.h>
21 #include <linux/err.h>
22 #include <linux/i2c.h>
23 #include <linux/workqueue.h>
24 #include <linux/slab.h>
26 /* LED select registers determine the source that drives LED outputs */
27 #define PCA9633_LED_OFF 0x0 /* LED driver off */
28 #define PCA9633_LED_ON 0x1 /* LED driver on */
29 #define PCA9633_LED_PWM 0x2 /* Controlled through PWM */
30 #define PCA9633_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
32 #define PCA9633_MODE1 0x00
33 #define PCA9633_MODE2 0x01
34 #define PCA9633_PWM_BASE 0x02
35 #define PCA9633_LEDOUT 0x08
37 static const struct i2c_device_id pca9633_id
[] = {
41 MODULE_DEVICE_TABLE(i2c
, pca9633_id
);
44 struct i2c_client
*client
;
45 struct work_struct work
;
46 enum led_brightness brightness
;
47 struct led_classdev led_cdev
;
48 int led_num
; /* 0 .. 3 potentially */
52 static void pca9633_led_work(struct work_struct
*work
)
54 struct pca9633_led
*pca9633
= container_of(work
,
55 struct pca9633_led
, work
);
56 u8 ledout
= i2c_smbus_read_byte_data(pca9633
->client
, PCA9633_LEDOUT
);
57 int shift
= 2 * pca9633
->led_num
;
58 u8 mask
= 0x3 << shift
;
60 switch (pca9633
->brightness
) {
62 i2c_smbus_write_byte_data(pca9633
->client
, PCA9633_LEDOUT
,
63 (ledout
& ~mask
) | (PCA9633_LED_ON
<< shift
));
66 i2c_smbus_write_byte_data(pca9633
->client
, PCA9633_LEDOUT
,
70 i2c_smbus_write_byte_data(pca9633
->client
,
71 PCA9633_PWM_BASE
+ pca9633
->led_num
,
73 i2c_smbus_write_byte_data(pca9633
->client
, PCA9633_LEDOUT
,
74 (ledout
& ~mask
) | (PCA9633_LED_PWM
<< shift
));
79 static void pca9633_led_set(struct led_classdev
*led_cdev
,
80 enum led_brightness value
)
82 struct pca9633_led
*pca9633
;
84 pca9633
= container_of(led_cdev
, struct pca9633_led
, led_cdev
);
86 pca9633
->brightness
= value
;
89 * Must use workqueue for the actual I/O since I2C operations
92 schedule_work(&pca9633
->work
);
95 static int __devinit
pca9633_probe(struct i2c_client
*client
,
96 const struct i2c_device_id
*id
)
98 struct pca9633_led
*pca9633
;
99 struct led_platform_data
*pdata
;
102 pdata
= client
->dev
.platform_data
;
105 if (pdata
->num_leds
<= 0 || pdata
->num_leds
> 4) {
106 dev_err(&client
->dev
, "board info must claim at most 4 LEDs");
111 pca9633
= devm_kzalloc(&client
->dev
, 4 * sizeof(*pca9633
), GFP_KERNEL
);
115 i2c_set_clientdata(client
, pca9633
);
117 for (i
= 0; i
< 4; i
++) {
118 pca9633
[i
].client
= client
;
119 pca9633
[i
].led_num
= i
;
121 /* Platform data can specify LED names and default triggers */
122 if (pdata
&& i
< pdata
->num_leds
) {
123 if (pdata
->leds
[i
].name
)
124 snprintf(pca9633
[i
].name
,
125 sizeof(pca9633
[i
].name
), "pca9633:%s",
126 pdata
->leds
[i
].name
);
127 if (pdata
->leds
[i
].default_trigger
)
128 pca9633
[i
].led_cdev
.default_trigger
=
129 pdata
->leds
[i
].default_trigger
;
131 snprintf(pca9633
[i
].name
, sizeof(pca9633
[i
].name
),
135 pca9633
[i
].led_cdev
.name
= pca9633
[i
].name
;
136 pca9633
[i
].led_cdev
.brightness_set
= pca9633_led_set
;
138 INIT_WORK(&pca9633
[i
].work
, pca9633_led_work
);
140 err
= led_classdev_register(&client
->dev
, &pca9633
[i
].led_cdev
);
145 /* Disable LED all-call address and set normal mode */
146 i2c_smbus_write_byte_data(client
, PCA9633_MODE1
, 0x00);
149 i2c_smbus_write_byte_data(client
, PCA9633_LEDOUT
, 0x00);
155 led_classdev_unregister(&pca9633
[i
].led_cdev
);
156 cancel_work_sync(&pca9633
[i
].work
);
162 static int __devexit
pca9633_remove(struct i2c_client
*client
)
164 struct pca9633_led
*pca9633
= i2c_get_clientdata(client
);
167 for (i
= 0; i
< 4; i
++) {
168 led_classdev_unregister(&pca9633
[i
].led_cdev
);
169 cancel_work_sync(&pca9633
[i
].work
);
175 static struct i2c_driver pca9633_driver
= {
177 .name
= "leds-pca9633",
178 .owner
= THIS_MODULE
,
180 .probe
= pca9633_probe
,
181 .remove
= __devexit_p(pca9633_remove
),
182 .id_table
= pca9633_id
,
185 module_i2c_driver(pca9633_driver
);
187 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
188 MODULE_DESCRIPTION("PCA9633 LED driver");
189 MODULE_LICENSE("GPL v2");