aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/leds
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-02-26 12:29:02 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2013-02-26 12:29:02 -0500
commit0512c04a2b5d29a33d96d315e1d14c55f5148aa7 (patch)
tree6373c0370abe32e1e42a933bd9cd08727e48b5d9 /drivers/leds
parent5115f3c19d17851aaff5a857f55b4a019c908775 (diff)
parent4b07c5d5123f76487c61cf9dc3f987d0b8c88a94 (diff)
Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/cooloney/linux-leds
Pull LED subsystem update from Bryan Wu. * 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/cooloney/linux-leds: (61 commits) leds: leds-sunfire: use dev_err()/pr_err() instead of printk() leds: 88pm860x: Add missing of_node_put() leds: tca6507: Use of_get_child_count() leds: leds-pwm: make it depend on PWM and not HAVE_PWM Documentation: leds: update LP55xx family devices leds-lp55xx: fix problem on removing LED attributes leds-lp5521/5523: add author and copyright description leds-lp5521/5523: use new lp55xx common header leds-lp55xx: clean up headers leds-lp55xx: clean up definitions leds-lp55xx: clean up unused data and functions leds-lp55xx: clean up _remove() leds-lp55xx: add new function for removing device attribtues leds-lp55xx: code refactoring on selftest function leds-lp55xx: use common device attribute driver function leds-lp55xx: support device specific attributes leds-lp5523: use generic firmware interface leds-lp5521: use generic firmware interface leds-lp55xx: support firmware interface leds-lp55xx: add new lp55xx_register_sysfs() for the firmware interface ...
Diffstat (limited to 'drivers/leds')
-rw-r--r--drivers/leds/Kconfig12
-rw-r--r--drivers/leds/Makefile1
-rw-r--r--drivers/leds/leds-88pm860x.c5
-rw-r--r--drivers/leds/leds-lm3530.c58
-rw-r--r--drivers/leds/leds-lm355x.c2
-rw-r--r--drivers/leds/leds-lm3642.c4
-rw-r--r--drivers/leds/leds-lp5521.c944
-rw-r--r--drivers/leds/leds-lp5523.c1050
-rw-r--r--drivers/leds/leds-lp55xx-common.c523
-rw-r--r--drivers/leds/leds-lp55xx-common.h134
-rw-r--r--drivers/leds/leds-lp8788.c9
-rw-r--r--drivers/leds/leds-pca9532.c6
-rw-r--r--drivers/leds/leds-pwm.c152
-rw-r--r--drivers/leds/leds-renesas-tpu.c12
-rw-r--r--drivers/leds/leds-ss4200.c3
-rw-r--r--drivers/leds/leds-sunfire.c19
-rw-r--r--drivers/leds/leds-tca6507.c72
-rw-r--r--drivers/leds/leds-wm831x-status.c2
18 files changed, 1439 insertions, 1569 deletions
diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index 4469b441b785..ec50824c02ec 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -193,9 +193,18 @@ config LEDS_LP3944
193 To compile this driver as a module, choose M here: the 193 To compile this driver as a module, choose M here: the
194 module will be called leds-lp3944. 194 module will be called leds-lp3944.
195 195
196config LEDS_LP55XX_COMMON
197 tristate "Common Driver for TI/National LP5521 and LP5523/55231"
198 depends on LEDS_LP5521 || LEDS_LP5523
199 select FW_LOADER
200 help
201 This option supports common operations for LP5521 and LP5523/55231
202 devices.
203
196config LEDS_LP5521 204config LEDS_LP5521
197 tristate "LED Support for N.S. LP5521 LED driver chip" 205 tristate "LED Support for N.S. LP5521 LED driver chip"
198 depends on LEDS_CLASS && I2C 206 depends on LEDS_CLASS && I2C
207 select LEDS_LP55XX_COMMON
199 help 208 help
200 If you say yes here you get support for the National Semiconductor 209 If you say yes here you get support for the National Semiconductor
201 LP5521 LED driver. It is 3 channel chip with programmable engines. 210 LP5521 LED driver. It is 3 channel chip with programmable engines.
@@ -205,6 +214,7 @@ config LEDS_LP5521
205config LEDS_LP5523 214config LEDS_LP5523
206 tristate "LED Support for TI/National LP5523/55231 LED driver chip" 215 tristate "LED Support for TI/National LP5523/55231 LED driver chip"
207 depends on LEDS_CLASS && I2C 216 depends on LEDS_CLASS && I2C
217 select LEDS_LP55XX_COMMON
208 help 218 help
209 If you say yes here you get support for TI/National Semiconductor 219 If you say yes here you get support for TI/National Semiconductor
210 LP5523/55231 LED driver. 220 LP5523/55231 LED driver.
@@ -310,7 +320,7 @@ config LEDS_DAC124S085
310config LEDS_PWM 320config LEDS_PWM
311 tristate "PWM driven LED Support" 321 tristate "PWM driven LED Support"
312 depends on LEDS_CLASS 322 depends on LEDS_CLASS
313 depends on HAVE_PWM 323 depends on PWM
314 help 324 help
315 This option enables support for pwm driven LEDs 325 This option enables support for pwm driven LEDs
316 326
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index 3fb9641b6194..215e7e3b6173 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_LEDS_PCA9532) += leds-pca9532.o
23obj-$(CONFIG_LEDS_GPIO_REGISTER) += leds-gpio-register.o 23obj-$(CONFIG_LEDS_GPIO_REGISTER) += leds-gpio-register.o
24obj-$(CONFIG_LEDS_GPIO) += leds-gpio.o 24obj-$(CONFIG_LEDS_GPIO) += leds-gpio.o
25obj-$(CONFIG_LEDS_LP3944) += leds-lp3944.o 25obj-$(CONFIG_LEDS_LP3944) += leds-lp3944.o
26obj-$(CONFIG_LEDS_LP55XX_COMMON) += leds-lp55xx-common.o
26obj-$(CONFIG_LEDS_LP5521) += leds-lp5521.o 27obj-$(CONFIG_LEDS_LP5521) += leds-lp5521.o
27obj-$(CONFIG_LEDS_LP5523) += leds-lp5523.o 28obj-$(CONFIG_LEDS_LP5523) += leds-lp5523.o
28obj-$(CONFIG_LEDS_LP8788) += leds-lp8788.o 29obj-$(CONFIG_LEDS_LP8788) += leds-lp8788.o
diff --git a/drivers/leds/leds-88pm860x.c b/drivers/leds/leds-88pm860x.c
index 6be2edd41173..f5b9ea315790 100644
--- a/drivers/leds/leds-88pm860x.c
+++ b/drivers/leds/leds-88pm860x.c
@@ -128,8 +128,10 @@ static void pm860x_led_set(struct led_classdev *cdev,
128static int pm860x_led_dt_init(struct platform_device *pdev, 128static int pm860x_led_dt_init(struct platform_device *pdev,
129 struct pm860x_led *data) 129 struct pm860x_led *data)
130{ 130{
131 struct device_node *nproot = pdev->dev.parent->of_node, *np; 131 struct device_node *nproot, *np;
132 int iset = 0; 132 int iset = 0;
133
134 nproot = of_node_get(pdev->dev.parent->of_node);
133 if (!nproot) 135 if (!nproot)
134 return -ENODEV; 136 return -ENODEV;
135 nproot = of_find_node_by_name(nproot, "leds"); 137 nproot = of_find_node_by_name(nproot, "leds");
@@ -145,6 +147,7 @@ static int pm860x_led_dt_init(struct platform_device *pdev,
145 break; 147 break;
146 } 148 }
147 } 149 }
150 of_node_put(nproot);
148 return 0; 151 return 0;
149} 152}
150#else 153#else
diff --git a/drivers/leds/leds-lm3530.c b/drivers/leds/leds-lm3530.c
index 214145483836..a036a19040fe 100644
--- a/drivers/leds/leds-lm3530.c
+++ b/drivers/leds/leds-lm3530.c
@@ -187,6 +187,40 @@ static void lm3530_als_configure(struct lm3530_platform_data *pdata,
187 (pdata->als2_resistor_sel << LM3530_ALS2_IMP_SHIFT); 187 (pdata->als2_resistor_sel << LM3530_ALS2_IMP_SHIFT);
188} 188}
189 189
190static int lm3530_led_enable(struct lm3530_data *drvdata)
191{
192 int ret;
193
194 if (drvdata->enable)
195 return 0;
196
197 ret = regulator_enable(drvdata->regulator);
198 if (ret) {
199 dev_err(drvdata->led_dev.dev, "Failed to enable vin:%d\n", ret);
200 return ret;
201 }
202
203 drvdata->enable = true;
204 return 0;
205}
206
207static void lm3530_led_disable(struct lm3530_data *drvdata)
208{
209 int ret;
210
211 if (!drvdata->enable)
212 return;
213
214 ret = regulator_disable(drvdata->regulator);
215 if (ret) {
216 dev_err(drvdata->led_dev.dev, "Failed to disable vin:%d\n",
217 ret);
218 return;
219 }
220
221 drvdata->enable = false;
222}
223
190static int lm3530_init_registers(struct lm3530_data *drvdata) 224static int lm3530_init_registers(struct lm3530_data *drvdata)
191{ 225{
192 int ret = 0; 226 int ret = 0;
@@ -245,15 +279,9 @@ static int lm3530_init_registers(struct lm3530_data *drvdata)
245 reg_val[12] = LM3530_DEF_ZT_3; /* LM3530_ALS_Z3T_REG */ 279 reg_val[12] = LM3530_DEF_ZT_3; /* LM3530_ALS_Z3T_REG */
246 reg_val[13] = LM3530_DEF_ZT_4; /* LM3530_ALS_Z4T_REG */ 280 reg_val[13] = LM3530_DEF_ZT_4; /* LM3530_ALS_Z4T_REG */
247 281
248 if (!drvdata->enable) { 282 ret = lm3530_led_enable(drvdata);
249 ret = regulator_enable(drvdata->regulator); 283 if (ret)
250 if (ret) { 284 return ret;
251 dev_err(&drvdata->client->dev,
252 "Enable regulator failed\n");
253 return ret;
254 }
255 drvdata->enable = true;
256 }
257 285
258 for (i = 0; i < LM3530_REG_MAX; i++) { 286 for (i = 0; i < LM3530_REG_MAX; i++) {
259 /* do not update brightness register when pwm mode */ 287 /* do not update brightness register when pwm mode */
@@ -305,13 +333,8 @@ static void lm3530_brightness_set(struct led_classdev *led_cdev,
305 else 333 else
306 drvdata->brightness = brt_val; 334 drvdata->brightness = brt_val;
307 335
308 if (brt_val == 0) { 336 if (brt_val == 0)
309 err = regulator_disable(drvdata->regulator); 337 lm3530_led_disable(drvdata);
310 if (err)
311 dev_err(&drvdata->client->dev,
312 "Disable regulator failed\n");
313 drvdata->enable = false;
314 }
315 break; 338 break;
316 case LM3530_BL_MODE_ALS: 339 case LM3530_BL_MODE_ALS:
317 break; 340 break;
@@ -458,8 +481,7 @@ static int lm3530_remove(struct i2c_client *client)
458 481
459 device_remove_file(drvdata->led_dev.dev, &dev_attr_mode); 482 device_remove_file(drvdata->led_dev.dev, &dev_attr_mode);
460 483
461 if (drvdata->enable) 484 lm3530_led_disable(drvdata);
462 regulator_disable(drvdata->regulator);
463 led_classdev_unregister(&drvdata->led_dev); 485 led_classdev_unregister(&drvdata->led_dev);
464 return 0; 486 return 0;
465} 487}
diff --git a/drivers/leds/leds-lm355x.c b/drivers/leds/leds-lm355x.c
index 65d79284c488..4117235ba618 100644
--- a/drivers/leds/leds-lm355x.c
+++ b/drivers/leds/leds-lm355x.c
@@ -380,7 +380,7 @@ static void lm355x_indicator_brightness_set(struct led_classdev *cdev,
380 380
381/* indicator pattern only for lm3556*/ 381/* indicator pattern only for lm3556*/
382static ssize_t lm3556_indicator_pattern_store(struct device *dev, 382static ssize_t lm3556_indicator_pattern_store(struct device *dev,
383 struct device_attribute *devAttr, 383 struct device_attribute *attr,
384 const char *buf, size_t size) 384 const char *buf, size_t size)
385{ 385{
386 ssize_t ret; 386 ssize_t ret;
diff --git a/drivers/leds/leds-lm3642.c b/drivers/leds/leds-lm3642.c
index 07b3dde90613..9f428d9dfe91 100644
--- a/drivers/leds/leds-lm3642.c
+++ b/drivers/leds/leds-lm3642.c
@@ -176,7 +176,7 @@ out:
176 176
177/* torch pin config for lm3642*/ 177/* torch pin config for lm3642*/
178static ssize_t lm3642_torch_pin_store(struct device *dev, 178static ssize_t lm3642_torch_pin_store(struct device *dev,
179 struct device_attribute *devAttr, 179 struct device_attribute *attr,
180 const char *buf, size_t size) 180 const char *buf, size_t size)
181{ 181{
182 ssize_t ret; 182 ssize_t ret;
@@ -233,7 +233,7 @@ static void lm3642_torch_brightness_set(struct led_classdev *cdev,
233 233
234/* strobe pin config for lm3642*/ 234/* strobe pin config for lm3642*/
235static ssize_t lm3642_strobe_pin_store(struct device *dev, 235static ssize_t lm3642_strobe_pin_store(struct device *dev,
236 struct device_attribute *devAttr, 236 struct device_attribute *attr,
237 const char *buf, size_t size) 237 const char *buf, size_t size)
238{ 238{
239 ssize_t ret; 239 ssize_t ret;
diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index cb8a5220200b..1001347ba70b 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -2,8 +2,10 @@
2 * LP5521 LED chip driver. 2 * LP5521 LED chip driver.
3 * 3 *
4 * Copyright (C) 2010 Nokia Corporation 4 * Copyright (C) 2010 Nokia Corporation
5 * Copyright (C) 2012 Texas Instruments
5 * 6 *
6 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com> 7 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
8 * Milo(Woogyom) Kim <milo.kim@ti.com>
7 * 9 *
8 * This program is free software; you can redistribute it and/or 10 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License 11 * modify it under the terms of the GNU General Public License
@@ -20,33 +22,21 @@
20 * 02110-1301 USA 22 * 02110-1301 USA
21 */ 23 */
22 24
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/i2c.h>
26#include <linux/mutex.h>
27#include <linux/gpio.h>
28#include <linux/interrupt.h>
29#include <linux/delay.h> 25#include <linux/delay.h>
30#include <linux/ctype.h> 26#include <linux/firmware.h>
31#include <linux/spinlock.h> 27#include <linux/i2c.h>
32#include <linux/wait.h> 28#include <linux/init.h>
33#include <linux/leds.h> 29#include <linux/leds.h>
34#include <linux/leds-lp5521.h> 30#include <linux/module.h>
35#include <linux/workqueue.h> 31#include <linux/mutex.h>
32#include <linux/platform_data/leds-lp55xx.h>
36#include <linux/slab.h> 33#include <linux/slab.h>
37 34
38#define LP5521_PROGRAM_LENGTH 32 /* in bytes */ 35#include "leds-lp55xx-common.h"
39
40#define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */
41#define LP5521_MAX_ENGINES 3 /* Maximum number of engines */
42
43#define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */
44#define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */
45 36
46#define LP5521_CMD_LOAD 0x15 /* 00010101 */ 37#define LP5521_PROGRAM_LENGTH 32
47#define LP5521_CMD_RUN 0x2a /* 00101010 */ 38#define LP5521_MAX_LEDS 3
48#define LP5521_CMD_DIRECT 0x3f /* 00111111 */ 39#define LP5521_CMD_DIRECT 0x3F
49#define LP5521_CMD_DISABLED 0x00 /* 00000000 */
50 40
51/* Registers */ 41/* Registers */
52#define LP5521_REG_ENABLE 0x00 42#define LP5521_REG_ENABLE 0x00
@@ -58,22 +48,14 @@
58#define LP5521_REG_G_CURRENT 0x06 48#define LP5521_REG_G_CURRENT 0x06
59#define LP5521_REG_B_CURRENT 0x07 49#define LP5521_REG_B_CURRENT 0x07
60#define LP5521_REG_CONFIG 0x08 50#define LP5521_REG_CONFIG 0x08
61#define LP5521_REG_R_CHANNEL_PC 0x09
62#define LP5521_REG_G_CHANNEL_PC 0x0A
63#define LP5521_REG_B_CHANNEL_PC 0x0B
64#define LP5521_REG_STATUS 0x0C 51#define LP5521_REG_STATUS 0x0C
65#define LP5521_REG_RESET 0x0D 52#define LP5521_REG_RESET 0x0D
66#define LP5521_REG_GPO 0x0E
67#define LP5521_REG_R_PROG_MEM 0x10 53#define LP5521_REG_R_PROG_MEM 0x10
68#define LP5521_REG_G_PROG_MEM 0x30 54#define LP5521_REG_G_PROG_MEM 0x30
69#define LP5521_REG_B_PROG_MEM 0x50 55#define LP5521_REG_B_PROG_MEM 0x50
70 56
71#define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM
72#define LP5521_PROG_MEM_SIZE 0x20
73
74/* Base register to set LED current */ 57/* Base register to set LED current */
75#define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT 58#define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
76
77/* Base register to set the brightness */ 59/* Base register to set the brightness */
78#define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM 60#define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
79 61
@@ -92,440 +74,287 @@
92/* default R channel current register value */ 74/* default R channel current register value */
93#define LP5521_REG_R_CURR_DEFAULT 0xAF 75#define LP5521_REG_R_CURR_DEFAULT 0xAF
94 76
95/* Pattern Mode */ 77/* Reset register value */
96#define PATTERN_OFF 0 78#define LP5521_RESET 0xFF
97 79
98struct lp5521_engine { 80/* Program Memory Operations */
99 int id; 81#define LP5521_MODE_R_M 0x30 /* Operation Mode Register */
100 u8 mode; 82#define LP5521_MODE_G_M 0x0C
101 u8 prog_page; 83#define LP5521_MODE_B_M 0x03
102 u8 engine_mask; 84#define LP5521_LOAD_R 0x10
103}; 85#define LP5521_LOAD_G 0x04
86#define LP5521_LOAD_B 0x01
104 87
105struct lp5521_led { 88#define LP5521_R_IS_LOADING(mode) \
106 int id; 89 ((mode & LP5521_MODE_R_M) == LP5521_LOAD_R)
107 u8 chan_nr; 90#define LP5521_G_IS_LOADING(mode) \
108 u8 led_current; 91 ((mode & LP5521_MODE_G_M) == LP5521_LOAD_G)
109 u8 max_current; 92#define LP5521_B_IS_LOADING(mode) \
110 struct led_classdev cdev; 93 ((mode & LP5521_MODE_B_M) == LP5521_LOAD_B)
111 struct work_struct brightness_work;
112 u8 brightness;
113};
114 94
115struct lp5521_chip { 95#define LP5521_EXEC_R_M 0x30 /* Enable Register */
116 struct lp5521_platform_data *pdata; 96#define LP5521_EXEC_G_M 0x0C
117 struct mutex lock; /* Serialize control */ 97#define LP5521_EXEC_B_M 0x03
118 struct i2c_client *client; 98#define LP5521_EXEC_M 0x3F
119 struct lp5521_engine engines[LP5521_MAX_ENGINES]; 99#define LP5521_RUN_R 0x20
120 struct lp5521_led leds[LP5521_MAX_LEDS]; 100#define LP5521_RUN_G 0x08
121 u8 num_channels; 101#define LP5521_RUN_B 0x02
122 u8 num_leds;
123};
124 102
125static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev) 103static inline void lp5521_wait_opmode_done(void)
126{ 104{
127 return container_of(cdev, struct lp5521_led, cdev); 105 /* operation mode change needs to be longer than 153 us */
106 usleep_range(200, 300);
128} 107}
129 108
130static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine) 109static inline void lp5521_wait_enable_done(void)
131{ 110{
132 return container_of(engine, struct lp5521_chip, 111 /* it takes more 488 us to update ENABLE register */
133 engines[engine->id - 1]); 112 usleep_range(500, 600);
134} 113}
135 114
136static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led) 115static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
137{ 116{
138 return container_of(led, struct lp5521_chip, 117 led->led_current = led_current;
139 leds[led->id]); 118 lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
119 led_current);
140} 120}
141 121
142static void lp5521_led_brightness_work(struct work_struct *work); 122static void lp5521_load_engine(struct lp55xx_chip *chip)
143
144static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
145{ 123{
146 return i2c_smbus_write_byte_data(client, reg, value); 124 enum lp55xx_engine_index idx = chip->engine_idx;
147} 125 u8 mask[] = {
126 [LP55XX_ENGINE_1] = LP5521_MODE_R_M,
127 [LP55XX_ENGINE_2] = LP5521_MODE_G_M,
128 [LP55XX_ENGINE_3] = LP5521_MODE_B_M,
129 };
148 130
149static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf) 131 u8 val[] = {
150{ 132 [LP55XX_ENGINE_1] = LP5521_LOAD_R,
151 s32 ret; 133 [LP55XX_ENGINE_2] = LP5521_LOAD_G,
134 [LP55XX_ENGINE_3] = LP5521_LOAD_B,
135 };
152 136
153 ret = i2c_smbus_read_byte_data(client, reg); 137 lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
154 if (ret < 0)
155 return ret;
156 138
157 *buf = ret; 139 lp5521_wait_opmode_done();
158 return 0;
159} 140}
160 141
161static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode) 142static void lp5521_stop_engine(struct lp55xx_chip *chip)
162{ 143{
163 struct lp5521_chip *chip = engine_to_lp5521(engine); 144 lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
164 struct i2c_client *client = chip->client; 145 lp5521_wait_opmode_done();
165 int ret;
166 u8 engine_state;
167
168 /* Only transition between RUN and DIRECT mode are handled here */
169 if (mode == LP5521_CMD_LOAD)
170 return 0;
171
172 if (mode == LP5521_CMD_DISABLED)
173 mode = LP5521_CMD_DIRECT;
174
175 ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state);
176 if (ret < 0)
177 return ret;
178
179 /* set mode only for this engine */
180 engine_state &= ~(engine->engine_mask);
181 mode &= engine->engine_mask;
182 engine_state |= mode;
183 return lp5521_write(client, LP5521_REG_OP_MODE, engine_state);
184} 146}
185 147
186static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern) 148static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
187{ 149{
188 struct lp5521_chip *chip = engine_to_lp5521(eng);
189 struct i2c_client *client = chip->client;
190 int ret; 150 int ret;
191 int addr;
192 u8 mode; 151 u8 mode;
152 u8 exec;
193 153
194 /* move current engine to direct mode and remember the state */ 154 /* stop engine */
195 ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT); 155 if (!start) {
196 if (ret) 156 lp5521_stop_engine(chip);
197 return ret; 157 lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
198 158 lp5521_wait_opmode_done();
199 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */ 159 return;
200 usleep_range(1000, 2000);
201 ret = lp5521_read(client, LP5521_REG_OP_MODE, &mode);
202 if (ret)
203 return ret;
204
205 /* For loading, all the engines to load mode */
206 lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
207 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
208 usleep_range(1000, 2000);
209 lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
210 /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
211 usleep_range(1000, 2000);
212
213 addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE;
214 i2c_smbus_write_i2c_block_data(client,
215 addr,
216 LP5521_PROG_MEM_SIZE,
217 pattern);
218
219 return lp5521_write(client, LP5521_REG_OP_MODE, mode);
220}
221
222static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr)
223{
224 return lp5521_write(chip->client,
225 LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
226 curr);
227}
228
229static void lp5521_init_engine(struct lp5521_chip *chip)
230{
231 int i;
232 for (i = 0; i < ARRAY_SIZE(chip->engines); i++) {
233 chip->engines[i].id = i + 1;
234 chip->engines[i].engine_mask = LP5521_ENG_MASK_BASE >> (i * 2);
235 chip->engines[i].prog_page = i;
236 } 160 }
237}
238
239static int lp5521_configure(struct i2c_client *client)
240{
241 struct lp5521_chip *chip = i2c_get_clientdata(client);
242 int ret;
243 u8 cfg;
244
245 lp5521_init_engine(chip);
246
247 /* Set all PWMs to direct control mode */
248 ret = lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
249
250 cfg = chip->pdata->update_config ?
251 : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
252 ret |= lp5521_write(client, LP5521_REG_CONFIG, cfg);
253
254 /* Initialize all channels PWM to zero -> leds off */
255 ret |= lp5521_write(client, LP5521_REG_R_PWM, 0);
256 ret |= lp5521_write(client, LP5521_REG_G_PWM, 0);
257 ret |= lp5521_write(client, LP5521_REG_B_PWM, 0);
258
259 /* Set engines are set to run state when OP_MODE enables engines */
260 ret |= lp5521_write(client, LP5521_REG_ENABLE,
261 LP5521_ENABLE_RUN_PROGRAM);
262 /* enable takes 500us. 1 - 2 ms leaves some margin */
263 usleep_range(1000, 2000);
264
265 return ret;
266}
267
268static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
269{
270 int ret;
271 u8 status;
272
273 ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
274 if (ret < 0)
275 return ret;
276
277 /* Check that ext clock is really in use if requested */
278 if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
279 if ((status & LP5521_EXT_CLK_USED) == 0)
280 return -EIO;
281 return 0;
282}
283
284static void lp5521_set_brightness(struct led_classdev *cdev,
285 enum led_brightness brightness)
286{
287 struct lp5521_led *led = cdev_to_led(cdev);
288 led->brightness = (u8)brightness;
289 schedule_work(&led->brightness_work);
290}
291
292static void lp5521_led_brightness_work(struct work_struct *work)
293{
294 struct lp5521_led *led = container_of(work,
295 struct lp5521_led,
296 brightness_work);
297 struct lp5521_chip *chip = led_to_lp5521(led);
298 struct i2c_client *client = chip->client;
299 161
300 mutex_lock(&chip->lock); 162 /*
301 lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr, 163 * To run the engine,
302 led->brightness); 164 * operation mode and enable register should updated at the same time
303 mutex_unlock(&chip->lock); 165 */
304}
305
306/* Detect the chip by setting its ENABLE register and reading it back. */
307static int lp5521_detect(struct i2c_client *client)
308{
309 int ret;
310 u8 buf;
311 166
312 ret = lp5521_write(client, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT); 167 ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
313 if (ret) 168 if (ret)
314 return ret; 169 return;
315 /* enable takes 500us. 1 - 2 ms leaves some margin */
316 usleep_range(1000, 2000);
317 ret = lp5521_read(client, LP5521_REG_ENABLE, &buf);
318 if (ret)
319 return ret;
320 if (buf != LP5521_ENABLE_DEFAULT)
321 return -ENODEV;
322 170
323 return 0; 171 ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
324} 172 if (ret)
173 return;
325 174
326/* Set engine mode and create appropriate sysfs attributes, if required. */ 175 /* change operation mode to RUN only when each engine is loading */
327static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode) 176 if (LP5521_R_IS_LOADING(mode)) {
328{ 177 mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R;
329 int ret = 0; 178 exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R;
179 }
330 180
331 /* if in that mode already do nothing, except for run */ 181 if (LP5521_G_IS_LOADING(mode)) {
332 if (mode == engine->mode && mode != LP5521_CMD_RUN) 182 mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G;
333 return 0; 183 exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G;
184 }
334 185
335 if (mode == LP5521_CMD_RUN) { 186 if (LP5521_B_IS_LOADING(mode)) {
336 ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN); 187 mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B;
337 } else if (mode == LP5521_CMD_LOAD) { 188 exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B;
338 lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
339 lp5521_set_engine_mode(engine, LP5521_CMD_LOAD);
340 } else if (mode == LP5521_CMD_DISABLED) {
341 lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
342 } 189 }
343 190
344 engine->mode = mode; 191 lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
192 lp5521_wait_opmode_done();
345 193
346 return ret; 194 lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
195 lp5521_wait_enable_done();
347} 196}
348 197
349static int lp5521_do_store_load(struct lp5521_engine *engine, 198static int lp5521_update_program_memory(struct lp55xx_chip *chip,
350 const char *buf, size_t len) 199 const u8 *data, size_t size)
351{ 200{
352 struct lp5521_chip *chip = engine_to_lp5521(engine); 201 enum lp55xx_engine_index idx = chip->engine_idx;
353 struct i2c_client *client = chip->client;
354 int ret, nrchars, offset = 0, i = 0;
355 char c[3];
356 unsigned cmd;
357 u8 pattern[LP5521_PROGRAM_LENGTH] = {0}; 202 u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
203 u8 addr[] = {
204 [LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM,
205 [LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM,
206 [LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM,
207 };
208 unsigned cmd;
209 char c[3];
210 int program_size;
211 int nrchars;
212 int offset = 0;
213 int ret;
214 int i;
358 215
359 while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) { 216 /* clear program memory before updating */
217 for (i = 0; i < LP5521_PROGRAM_LENGTH; i++)
218 lp55xx_write(chip, addr[idx] + i, 0);
219
220 i = 0;
221 while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) {
360 /* separate sscanfs because length is working only for %s */ 222 /* separate sscanfs because length is working only for %s */
361 ret = sscanf(buf + offset, "%2s%n ", c, &nrchars); 223 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
362 if (ret != 2) 224 if (ret != 1)
363 goto fail; 225 goto err;
226
364 ret = sscanf(c, "%2x", &cmd); 227 ret = sscanf(c, "%2x", &cmd);
365 if (ret != 1) 228 if (ret != 1)
366 goto fail; 229 goto err;
367 pattern[i] = (u8)cmd;
368 230
231 pattern[i] = (u8)cmd;
369 offset += nrchars; 232 offset += nrchars;
370 i++; 233 i++;
371 } 234 }
372 235
373 /* Each instruction is 16bit long. Check that length is even */ 236 /* Each instruction is 16bit long. Check that length is even */
374 if (i % 2) 237 if (i % 2)
375 goto fail; 238 goto err;
376 239
377 mutex_lock(&chip->lock); 240 program_size = i;
378 if (engine->mode == LP5521_CMD_LOAD) 241 for (i = 0; i < program_size; i++)
379 ret = lp5521_load_program(engine, pattern); 242 lp55xx_write(chip, addr[idx] + i, pattern[i]);
380 else
381 ret = -EINVAL;
382 mutex_unlock(&chip->lock);
383 243
384 if (ret) { 244 return 0;
385 dev_err(&client->dev, "failed loading pattern\n");
386 return ret;
387 }
388 245
389 return len; 246err:
390fail: 247 dev_err(&chip->cl->dev, "wrong pattern format\n");
391 dev_err(&client->dev, "wrong pattern format\n");
392 return -EINVAL; 248 return -EINVAL;
393} 249}
394 250
395static ssize_t store_engine_load(struct device *dev, 251static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
396 struct device_attribute *attr,
397 const char *buf, size_t len, int nr)
398{ 252{
399 struct i2c_client *client = to_i2c_client(dev); 253 const struct firmware *fw = chip->fw;
400 struct lp5521_chip *chip = i2c_get_clientdata(client);
401 return lp5521_do_store_load(&chip->engines[nr - 1], buf, len);
402}
403 254
404#define store_load(nr) \ 255 if (fw->size > LP5521_PROGRAM_LENGTH) {
405static ssize_t store_engine##nr##_load(struct device *dev, \ 256 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
406 struct device_attribute *attr, \ 257 fw->size);
407 const char *buf, size_t len) \ 258 return;
408{ \
409 return store_engine_load(dev, attr, buf, len, nr); \
410}
411store_load(1)
412store_load(2)
413store_load(3)
414
415static ssize_t show_engine_mode(struct device *dev,
416 struct device_attribute *attr,
417 char *buf, int nr)
418{
419 struct i2c_client *client = to_i2c_client(dev);
420 struct lp5521_chip *chip = i2c_get_clientdata(client);
421 switch (chip->engines[nr - 1].mode) {
422 case LP5521_CMD_RUN:
423 return sprintf(buf, "run\n");
424 case LP5521_CMD_LOAD:
425 return sprintf(buf, "load\n");
426 case LP5521_CMD_DISABLED:
427 return sprintf(buf, "disabled\n");
428 default:
429 return sprintf(buf, "disabled\n");
430 } 259 }
431}
432 260
433#define show_mode(nr) \ 261 /*
434static ssize_t show_engine##nr##_mode(struct device *dev, \ 262 * Program momery sequence
435 struct device_attribute *attr, \ 263 * 1) set engine mode to "LOAD"
436 char *buf) \ 264 * 2) write firmware data into program memory
437{ \ 265 */
438 return show_engine_mode(dev, attr, buf, nr); \ 266
267 lp5521_load_engine(chip);
268 lp5521_update_program_memory(chip, fw->data, fw->size);
439} 269}
440show_mode(1)
441show_mode(2)
442show_mode(3)
443 270
444static ssize_t store_engine_mode(struct device *dev, 271static int lp5521_post_init_device(struct lp55xx_chip *chip)
445 struct device_attribute *attr,
446 const char *buf, size_t len, int nr)
447{ 272{
448 struct i2c_client *client = to_i2c_client(dev); 273 int ret;
449 struct lp5521_chip *chip = i2c_get_clientdata(client); 274 u8 val;
450 struct lp5521_engine *engine = &chip->engines[nr - 1];
451 mutex_lock(&chip->lock);
452 275
453 if (!strncmp(buf, "run", 3)) 276 /*
454 lp5521_set_mode(engine, LP5521_CMD_RUN); 277 * Make sure that the chip is reset by reading back the r channel
455 else if (!strncmp(buf, "load", 4)) 278 * current reg. This is dummy read is required on some platforms -
456 lp5521_set_mode(engine, LP5521_CMD_LOAD); 279 * otherwise further access to the R G B channels in the
457 else if (!strncmp(buf, "disabled", 8)) 280 * LP5521_REG_ENABLE register will not have any effect - strange!
458 lp5521_set_mode(engine, LP5521_CMD_DISABLED); 281 */
282 ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
283 if (ret) {
284 dev_err(&chip->cl->dev, "error in resetting chip\n");
285 return ret;
286 }
287 if (val != LP5521_REG_R_CURR_DEFAULT) {
288 dev_err(&chip->cl->dev,
289 "unexpected data in register (expected 0x%x got 0x%x)\n",
290 LP5521_REG_R_CURR_DEFAULT, val);
291 ret = -EINVAL;
292 return ret;
293 }
294 usleep_range(10000, 20000);
459 295
460 mutex_unlock(&chip->lock); 296 /* Set all PWMs to direct control mode */
461 return len; 297 ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
462}
463 298
464#define store_mode(nr) \ 299 val = chip->pdata->update_config ?
465static ssize_t store_engine##nr##_mode(struct device *dev, \ 300 : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
466 struct device_attribute *attr, \ 301 ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
467 const char *buf, size_t len) \ 302 if (ret)
468{ \ 303 return ret;
469 return store_engine_mode(dev, attr, buf, len, nr); \
470}
471store_mode(1)
472store_mode(2)
473store_mode(3)
474 304
475static ssize_t show_max_current(struct device *dev, 305 /* Initialize all channels PWM to zero -> leds off */
476 struct device_attribute *attr, 306 lp55xx_write(chip, LP5521_REG_R_PWM, 0);
477 char *buf) 307 lp55xx_write(chip, LP5521_REG_G_PWM, 0);
478{ 308 lp55xx_write(chip, LP5521_REG_B_PWM, 0);
479 struct led_classdev *led_cdev = dev_get_drvdata(dev);
480 struct lp5521_led *led = cdev_to_led(led_cdev);
481 309
482 return sprintf(buf, "%d\n", led->max_current); 310 /* Set engines are set to run state when OP_MODE enables engines */
483} 311 ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
312 if (ret)
313 return ret;
484 314
485static ssize_t show_current(struct device *dev, 315 lp5521_wait_enable_done();
486 struct device_attribute *attr,
487 char *buf)
488{
489 struct led_classdev *led_cdev = dev_get_drvdata(dev);
490 struct lp5521_led *led = cdev_to_led(led_cdev);
491 316
492 return sprintf(buf, "%d\n", led->led_current); 317 return 0;
493} 318}
494 319
495static ssize_t store_current(struct device *dev, 320static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
496 struct device_attribute *attr,
497 const char *buf, size_t len)
498{ 321{
499 struct led_classdev *led_cdev = dev_get_drvdata(dev); 322 struct lp55xx_platform_data *pdata = chip->pdata;
500 struct lp5521_led *led = cdev_to_led(led_cdev); 323 int ret;
501 struct lp5521_chip *chip = led_to_lp5521(led); 324 u8 status;
502 ssize_t ret;
503 unsigned long curr;
504 325
505 if (kstrtoul(buf, 0, &curr)) 326 ret = lp55xx_read(chip, LP5521_REG_STATUS, &status);
506 return -EINVAL; 327 if (ret < 0)
328 return ret;
507 329
508 if (curr > led->max_current) 330 if (pdata->clock_mode != LP55XX_CLOCK_EXT)
509 return -EINVAL; 331 return 0;
510 332
511 mutex_lock(&chip->lock); 333 /* Check that ext clock is really in use if requested */
512 ret = lp5521_set_led_current(chip, led->id, curr); 334 if ((status & LP5521_EXT_CLK_USED) == 0)
513 mutex_unlock(&chip->lock); 335 return -EIO;
514 336
515 if (ret < 0) 337 return 0;
516 return ret; 338}
517 339
518 led->led_current = (u8)curr; 340static void lp5521_led_brightness_work(struct work_struct *work)
341{
342 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
343 brightness_work);
344 struct lp55xx_chip *chip = led->chip;
519 345
520 return len; 346 mutex_lock(&chip->lock);
347 lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
348 led->brightness);
349 mutex_unlock(&chip->lock);
521} 350}
522 351
523static ssize_t lp5521_selftest(struct device *dev, 352static ssize_t lp5521_selftest(struct device *dev,
524 struct device_attribute *attr, 353 struct device_attribute *attr,
525 char *buf) 354 char *buf)
526{ 355{
527 struct i2c_client *client = to_i2c_client(dev); 356 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
528 struct lp5521_chip *chip = i2c_get_clientdata(client); 357 struct lp55xx_chip *chip = led->chip;
529 int ret; 358 int ret;
530 359
531 mutex_lock(&chip->lock); 360 mutex_lock(&chip->lock);
@@ -534,133 +363,11 @@ static ssize_t lp5521_selftest(struct device *dev,
534 return sprintf(buf, "%s\n", ret ? "FAIL" : "OK"); 363 return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
535} 364}
536 365
537static void lp5521_clear_program_memory(struct i2c_client *cl)
538{
539 int i;
540 u8 rgb_mem[] = {
541 LP5521_REG_R_PROG_MEM,
542 LP5521_REG_G_PROG_MEM,
543 LP5521_REG_B_PROG_MEM,
544 };
545
546 for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) {
547 lp5521_write(cl, rgb_mem[i], 0);
548 lp5521_write(cl, rgb_mem[i] + 1, 0);
549 }
550}
551
552static void lp5521_write_program_memory(struct i2c_client *cl,
553 u8 base, u8 *rgb, int size)
554{
555 int i;
556
557 if (!rgb || size <= 0)
558 return;
559
560 for (i = 0; i < size; i++)
561 lp5521_write(cl, base + i, *(rgb + i));
562
563 lp5521_write(cl, base + i, 0);
564 lp5521_write(cl, base + i + 1, 0);
565}
566
567static inline struct lp5521_led_pattern *lp5521_get_pattern
568 (struct lp5521_chip *chip, u8 offset)
569{
570 struct lp5521_led_pattern *ptn;
571 ptn = chip->pdata->patterns + (offset - 1);
572 return ptn;
573}
574
575static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
576{
577 struct lp5521_led_pattern *ptn;
578 struct i2c_client *cl = chip->client;
579 int num_patterns = chip->pdata->num_patterns;
580
581 if (mode > num_patterns || !(chip->pdata->patterns))
582 return;
583
584 if (mode == PATTERN_OFF) {
585 lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
586 usleep_range(1000, 2000);
587 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
588 } else {
589 ptn = lp5521_get_pattern(chip, mode);
590 if (!ptn)
591 return;
592
593 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
594 usleep_range(1000, 2000);
595
596 lp5521_clear_program_memory(cl);
597
598 lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM,
599 ptn->r, ptn->size_r);
600 lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM,
601 ptn->g, ptn->size_g);
602 lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM,
603 ptn->b, ptn->size_b);
604
605 lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN);
606 usleep_range(1000, 2000);
607 lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
608 }
609}
610
611static ssize_t store_led_pattern(struct device *dev,
612 struct device_attribute *attr,
613 const char *buf, size_t len)
614{
615 struct lp5521_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
616 unsigned long val;
617 int ret;
618
619 ret = kstrtoul(buf, 16, &val);
620 if (ret)
621 return ret;
622
623 lp5521_run_led_pattern(val, chip);
624
625 return len;
626}
627
628/* led class device attributes */
629static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
630static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
631
632static struct attribute *lp5521_led_attributes[] = {
633 &dev_attr_led_current.attr,
634 &dev_attr_max_current.attr,
635 NULL,
636};
637
638static struct attribute_group lp5521_led_attribute_group = {
639 .attrs = lp5521_led_attributes
640};
641
642/* device attributes */ 366/* device attributes */
643static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
644 show_engine1_mode, store_engine1_mode);
645static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
646 show_engine2_mode, store_engine2_mode);
647static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
648 show_engine3_mode, store_engine3_mode);
649static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
650static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
651static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
652static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL); 367static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
653static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, store_led_pattern);
654 368
655static struct attribute *lp5521_attributes[] = { 369static struct attribute *lp5521_attributes[] = {
656 &dev_attr_engine1_mode.attr,
657 &dev_attr_engine2_mode.attr,
658 &dev_attr_engine3_mode.attr,
659 &dev_attr_selftest.attr, 370 &dev_attr_selftest.attr,
660 &dev_attr_engine1_load.attr,
661 &dev_attr_engine2_load.attr,
662 &dev_attr_engine3_load.attr,
663 &dev_attr_led_pattern.attr,
664 NULL 371 NULL
665}; 372};
666 373
@@ -668,217 +375,91 @@ static const struct attribute_group lp5521_group = {
668 .attrs = lp5521_attributes, 375 .attrs = lp5521_attributes,
669}; 376};
670 377
671static int lp5521_register_sysfs(struct i2c_client *client) 378/* Chip specific configurations */
672{ 379static struct lp55xx_device_config lp5521_cfg = {
673 struct device *dev = &client->dev; 380 .reset = {
674 return sysfs_create_group(&dev->kobj, &lp5521_group); 381 .addr = LP5521_REG_RESET,
675} 382 .val = LP5521_RESET,
676 383 },
677static void lp5521_unregister_sysfs(struct i2c_client *client) 384 .enable = {
678{ 385 .addr = LP5521_REG_ENABLE,
679 struct lp5521_chip *chip = i2c_get_clientdata(client); 386 .val = LP5521_ENABLE_DEFAULT,
680 struct device *dev = &client->dev; 387 },
681 int i; 388 .max_channel = LP5521_MAX_LEDS,
682 389 .post_init_device = lp5521_post_init_device,
683 sysfs_remove_group(&dev->kobj, &lp5521_group); 390 .brightness_work_fn = lp5521_led_brightness_work,
684 391 .set_led_current = lp5521_set_led_current,
685 for (i = 0; i < chip->num_leds; i++) 392 .firmware_cb = lp5521_firmware_loaded,
686 sysfs_remove_group(&chip->leds[i].cdev.dev->kobj, 393 .run_engine = lp5521_run_engine,
687 &lp5521_led_attribute_group); 394 .dev_attr_group = &lp5521_group,
688} 395};
689
690static int lp5521_init_led(struct lp5521_led *led,
691 struct i2c_client *client,
692 int chan, struct lp5521_platform_data *pdata)
693{
694 struct device *dev = &client->dev;
695 char name[32];
696 int res;
697
698 if (chan >= LP5521_MAX_LEDS)
699 return -EINVAL;
700
701 if (pdata->led_config[chan].led_current == 0)
702 return 0;
703
704 led->led_current = pdata->led_config[chan].led_current;
705 led->max_current = pdata->led_config[chan].max_current;
706 led->chan_nr = pdata->led_config[chan].chan_nr;
707
708 if (led->chan_nr >= LP5521_MAX_LEDS) {
709 dev_err(dev, "Use channel numbers between 0 and %d\n",
710 LP5521_MAX_LEDS - 1);
711 return -EINVAL;
712 }
713
714 led->cdev.brightness_set = lp5521_set_brightness;
715 if (pdata->led_config[chan].name) {
716 led->cdev.name = pdata->led_config[chan].name;
717 } else {
718 snprintf(name, sizeof(name), "%s:channel%d",
719 pdata->label ?: client->name, chan);
720 led->cdev.name = name;
721 }
722
723 res = led_classdev_register(dev, &led->cdev);
724 if (res < 0) {
725 dev_err(dev, "couldn't register led on channel %d\n", chan);
726 return res;
727 }
728
729 res = sysfs_create_group(&led->cdev.dev->kobj,
730 &lp5521_led_attribute_group);
731 if (res < 0) {
732 dev_err(dev, "couldn't register current attribute\n");
733 led_classdev_unregister(&led->cdev);
734 return res;
735 }
736 return 0;
737}
738 396
739static int lp5521_probe(struct i2c_client *client, 397static int lp5521_probe(struct i2c_client *client,
740 const struct i2c_device_id *id) 398 const struct i2c_device_id *id)
741{ 399{
742 struct lp5521_chip *chip; 400 int ret;
743 struct lp5521_platform_data *pdata; 401 struct lp55xx_chip *chip;
744 int ret, i, led; 402 struct lp55xx_led *led;
745 u8 buf; 403 struct lp55xx_platform_data *pdata = client->dev.platform_data;
746
747 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
748 if (!chip)
749 return -ENOMEM;
750
751 i2c_set_clientdata(client, chip);
752 chip->client = client;
753
754 pdata = client->dev.platform_data;
755 404
756 if (!pdata) { 405 if (!pdata) {
757 dev_err(&client->dev, "no platform data\n"); 406 dev_err(&client->dev, "no platform data\n");
758 return -EINVAL; 407 return -EINVAL;
759 } 408 }
760 409
761 mutex_init(&chip->lock); 410 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
762 411 if (!chip)
763 chip->pdata = pdata; 412 return -ENOMEM;
764
765 if (pdata->setup_resources) {
766 ret = pdata->setup_resources();
767 if (ret < 0)
768 return ret;
769 }
770 413
771 if (pdata->enable) { 414 led = devm_kzalloc(&client->dev,
772 pdata->enable(0); 415 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
773 usleep_range(1000, 2000); /* Keep enable down at least 1ms */ 416 if (!led)
774 pdata->enable(1); 417 return -ENOMEM;
775 usleep_range(1000, 2000); /* 500us abs min. */
776 }
777 418
778 lp5521_write(client, LP5521_REG_RESET, 0xff); 419 chip->cl = client;
779 usleep_range(10000, 20000); /* 420 chip->pdata = pdata;
780 * Exact value is not available. 10 - 20ms 421 chip->cfg = &lp5521_cfg;
781 * appears to be enough for reset.
782 */
783 422
784 /* 423 mutex_init(&chip->lock);
785 * Make sure that the chip is reset by reading back the r channel
786 * current reg. This is dummy read is required on some platforms -
787 * otherwise further access to the R G B channels in the
788 * LP5521_REG_ENABLE register will not have any effect - strange!
789 */
790 ret = lp5521_read(client, LP5521_REG_R_CURRENT, &buf);
791 if (ret) {
792 dev_err(&client->dev, "error in resetting chip\n");
793 goto fail2;
794 }
795 if (buf != LP5521_REG_R_CURR_DEFAULT) {
796 dev_err(&client->dev,
797 "unexpected data in register (expected 0x%x got 0x%x)\n",
798 LP5521_REG_R_CURR_DEFAULT, buf);
799 ret = -EINVAL;
800 goto fail2;
801 }
802 usleep_range(10000, 20000);
803 424
804 ret = lp5521_detect(client); 425 i2c_set_clientdata(client, led);
805 426
806 if (ret) { 427 ret = lp55xx_init_device(chip);
807 dev_err(&client->dev, "Chip not found\n"); 428 if (ret)
808 goto fail2; 429 goto err_init;
809 }
810 430
811 dev_info(&client->dev, "%s programmable led chip found\n", id->name); 431 dev_info(&client->dev, "%s programmable led chip found\n", id->name);
812 432
813 ret = lp5521_configure(client); 433 ret = lp55xx_register_leds(led, chip);
814 if (ret < 0) { 434 if (ret)
815 dev_err(&client->dev, "error configuring chip\n"); 435 goto err_register_leds;
816 goto fail1;
817 }
818
819 /* Initialize leds */
820 chip->num_channels = pdata->num_channels;
821 chip->num_leds = 0;
822 led = 0;
823 for (i = 0; i < pdata->num_channels; i++) {
824 /* Do not initialize channels that are not connected */
825 if (pdata->led_config[i].led_current == 0)
826 continue;
827
828 ret = lp5521_init_led(&chip->leds[led], client, i, pdata);
829 if (ret) {
830 dev_err(&client->dev, "error initializing leds\n");
831 goto fail2;
832 }
833 chip->num_leds++;
834
835 chip->leds[led].id = led;
836 /* Set initial LED current */
837 lp5521_set_led_current(chip, led,
838 chip->leds[led].led_current);
839
840 INIT_WORK(&(chip->leds[led].brightness_work),
841 lp5521_led_brightness_work);
842
843 led++;
844 }
845 436
846 ret = lp5521_register_sysfs(client); 437 ret = lp55xx_register_sysfs(chip);
847 if (ret) { 438 if (ret) {
848 dev_err(&client->dev, "registering sysfs failed\n"); 439 dev_err(&client->dev, "registering sysfs failed\n");
849 goto fail2; 440 goto err_register_sysfs;
850 }
851 return ret;
852fail2:
853 for (i = 0; i < chip->num_leds; i++) {
854 led_classdev_unregister(&chip->leds[i].cdev);
855 cancel_work_sync(&chip->leds[i].brightness_work);
856 } 441 }
857fail1: 442
858 if (pdata->enable) 443 return 0;
859 pdata->enable(0); 444
860 if (pdata->release_resources) 445err_register_sysfs:
861 pdata->release_resources(); 446 lp55xx_unregister_leds(led, chip);
447err_register_leds:
448 lp55xx_deinit_device(chip);
449err_init:
862 return ret; 450 return ret;
863} 451}
864 452
865static int lp5521_remove(struct i2c_client *client) 453static int lp5521_remove(struct i2c_client *client)
866{ 454{
867 struct lp5521_chip *chip = i2c_get_clientdata(client); 455 struct lp55xx_led *led = i2c_get_clientdata(client);
868 int i; 456 struct lp55xx_chip *chip = led->chip;
869 457
870 lp5521_run_led_pattern(PATTERN_OFF, chip); 458 lp5521_stop_engine(chip);
871 lp5521_unregister_sysfs(client); 459 lp55xx_unregister_sysfs(chip);
872 460 lp55xx_unregister_leds(led, chip);
873 for (i = 0; i < chip->num_leds; i++) { 461 lp55xx_deinit_device(chip);
874 led_classdev_unregister(&chip->leds[i].cdev);
875 cancel_work_sync(&chip->leds[i].brightness_work);
876 }
877 462
878 if (chip->pdata->enable)
879 chip->pdata->enable(0);
880 if (chip->pdata->release_resources)
881 chip->pdata->release_resources();
882 return 0; 463 return 0;
883} 464}
884 465
@@ -900,5 +481,6 @@ static struct i2c_driver lp5521_driver = {
900module_i2c_driver(lp5521_driver); 481module_i2c_driver(lp5521_driver);
901 482
902MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo"); 483MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
484MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
903MODULE_DESCRIPTION("LP5521 LED engine"); 485MODULE_DESCRIPTION("LP5521 LED engine");
904MODULE_LICENSE("GPL v2"); 486MODULE_LICENSE("GPL v2");
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 7f5be8948cde..229f734040af 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -2,8 +2,10 @@
2 * lp5523.c - LP5523 LED Driver 2 * lp5523.c - LP5523 LED Driver
3 * 3 *
4 * Copyright (C) 2010 Nokia Corporation 4 * Copyright (C) 2010 Nokia Corporation
5 * Copyright (C) 2012 Texas Instruments
5 * 6 *
6 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com> 7 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
8 * Milo(Woogyom) Kim <milo.kim@ti.com>
7 * 9 *
8 * This program is free software; you can redistribute it and/or 10 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License 11 * modify it under the terms of the GNU General Public License
@@ -20,502 +22,351 @@
20 * 02110-1301 USA 22 * 02110-1301 USA
21 */ 23 */
22 24
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/i2c.h>
26#include <linux/mutex.h>
27#include <linux/gpio.h>
28#include <linux/interrupt.h>
29#include <linux/delay.h> 25#include <linux/delay.h>
30#include <linux/ctype.h> 26#include <linux/firmware.h>
31#include <linux/spinlock.h> 27#include <linux/i2c.h>
32#include <linux/wait.h> 28#include <linux/init.h>
33#include <linux/leds.h> 29#include <linux/leds.h>
34#include <linux/leds-lp5523.h> 30#include <linux/module.h>
35#include <linux/workqueue.h> 31#include <linux/mutex.h>
32#include <linux/platform_data/leds-lp55xx.h>
36#include <linux/slab.h> 33#include <linux/slab.h>
37 34
35#include "leds-lp55xx-common.h"
36
37#define LP5523_PROGRAM_LENGTH 32
38#define LP5523_MAX_LEDS 9
39
40/* Registers */
38#define LP5523_REG_ENABLE 0x00 41#define LP5523_REG_ENABLE 0x00
39#define LP5523_REG_OP_MODE 0x01 42#define LP5523_REG_OP_MODE 0x01
40#define LP5523_REG_RATIOMETRIC_MSB 0x02
41#define LP5523_REG_RATIOMETRIC_LSB 0x03
42#define LP5523_REG_ENABLE_LEDS_MSB 0x04 43#define LP5523_REG_ENABLE_LEDS_MSB 0x04
43#define LP5523_REG_ENABLE_LEDS_LSB 0x05 44#define LP5523_REG_ENABLE_LEDS_LSB 0x05
44#define LP5523_REG_LED_CNTRL_BASE 0x06
45#define LP5523_REG_LED_PWM_BASE 0x16 45#define LP5523_REG_LED_PWM_BASE 0x16
46#define LP5523_REG_LED_CURRENT_BASE 0x26 46#define LP5523_REG_LED_CURRENT_BASE 0x26
47#define LP5523_REG_CONFIG 0x36 47#define LP5523_REG_CONFIG 0x36
48#define LP5523_REG_CHANNEL1_PC 0x37 48#define LP5523_REG_STATUS 0x3A
49#define LP5523_REG_CHANNEL2_PC 0x38 49#define LP5523_REG_RESET 0x3D
50#define LP5523_REG_CHANNEL3_PC 0x39
51#define LP5523_REG_STATUS 0x3a
52#define LP5523_REG_GPO 0x3b
53#define LP5523_REG_VARIABLE 0x3c
54#define LP5523_REG_RESET 0x3d
55#define LP5523_REG_TEMP_CTRL 0x3e
56#define LP5523_REG_TEMP_READ 0x3f
57#define LP5523_REG_TEMP_WRITE 0x40
58#define LP5523_REG_LED_TEST_CTRL 0x41 50#define LP5523_REG_LED_TEST_CTRL 0x41
59#define LP5523_REG_LED_TEST_ADC 0x42 51#define LP5523_REG_LED_TEST_ADC 0x42
60#define LP5523_REG_ENG1_VARIABLE 0x45 52#define LP5523_REG_PROG_PAGE_SEL 0x4F
61#define LP5523_REG_ENG2_VARIABLE 0x46
62#define LP5523_REG_ENG3_VARIABLE 0x47
63#define LP5523_REG_MASTER_FADER1 0x48
64#define LP5523_REG_MASTER_FADER2 0x49
65#define LP5523_REG_MASTER_FADER3 0x4a
66#define LP5523_REG_CH1_PROG_START 0x4c
67#define LP5523_REG_CH2_PROG_START 0x4d
68#define LP5523_REG_CH3_PROG_START 0x4e
69#define LP5523_REG_PROG_PAGE_SEL 0x4f
70#define LP5523_REG_PROG_MEM 0x50 53#define LP5523_REG_PROG_MEM 0x50
71 54
72#define LP5523_CMD_LOAD 0x15 /* 00010101 */ 55/* Bit description in registers */
73#define LP5523_CMD_RUN 0x2a /* 00101010 */
74#define LP5523_CMD_DISABLED 0x00 /* 00000000 */
75
76#define LP5523_ENABLE 0x40 56#define LP5523_ENABLE 0x40
77#define LP5523_AUTO_INC 0x40 57#define LP5523_AUTO_INC 0x40
78#define LP5523_PWR_SAVE 0x20 58#define LP5523_PWR_SAVE 0x20
79#define LP5523_PWM_PWR_SAVE 0x04 59#define LP5523_PWM_PWR_SAVE 0x04
80#define LP5523_CP_1 0x08
81#define LP5523_CP_1_5 0x10
82#define LP5523_CP_AUTO 0x18 60#define LP5523_CP_AUTO 0x18
83#define LP5523_INT_CLK 0x01
84#define LP5523_AUTO_CLK 0x02 61#define LP5523_AUTO_CLK 0x02
62
85#define LP5523_EN_LEDTEST 0x80 63#define LP5523_EN_LEDTEST 0x80
86#define LP5523_LEDTEST_DONE 0x80 64#define LP5523_LEDTEST_DONE 0x80
87 65#define LP5523_RESET 0xFF
88#define LP5523_DEFAULT_CURRENT 50 /* microAmps */
89#define LP5523_PROGRAM_LENGTH 32 /* in bytes */
90#define LP5523_PROGRAM_PAGES 6
91#define LP5523_ADC_SHORTCIRC_LIM 80 66#define LP5523_ADC_SHORTCIRC_LIM 80
92
93#define LP5523_LEDS 9
94#define LP5523_ENGINES 3
95
96#define LP5523_ENG_MASK_BASE 0x30 /* 00110000 */
97
98#define LP5523_ENG_STATUS_MASK 0x07 /* 00000111 */
99
100#define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING
101
102#define LP5523_EXT_CLK_USED 0x08 67#define LP5523_EXT_CLK_USED 0x08
103 68
104#define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led))) 69/* Memory Page Selection */
105#define SHIFT_MASK(id) (((id) - 1) * 2) 70#define LP5523_PAGE_ENG1 0
71#define LP5523_PAGE_ENG2 1
72#define LP5523_PAGE_ENG3 2
73
74/* Program Memory Operations */
75#define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */
76#define LP5523_MODE_ENG2_M 0x0C
77#define LP5523_MODE_ENG3_M 0x03
78#define LP5523_LOAD_ENG1 0x10
79#define LP5523_LOAD_ENG2 0x04
80#define LP5523_LOAD_ENG3 0x01
81
82#define LP5523_ENG1_IS_LOADING(mode) \
83 ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
84#define LP5523_ENG2_IS_LOADING(mode) \
85 ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
86#define LP5523_ENG3_IS_LOADING(mode) \
87 ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
88
89#define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */
90#define LP5523_EXEC_ENG2_M 0x0C
91#define LP5523_EXEC_ENG3_M 0x03
92#define LP5523_EXEC_M 0x3F
93#define LP5523_RUN_ENG1 0x20
94#define LP5523_RUN_ENG2 0x08
95#define LP5523_RUN_ENG3 0x02
106 96
107enum lp5523_chip_id { 97enum lp5523_chip_id {
108 LP5523, 98 LP5523,
109 LP55231, 99 LP55231,
110}; 100};
111 101
112struct lp5523_engine { 102static inline void lp5523_wait_opmode_done(void)
113 int id;
114 u8 mode;
115 u8 prog_page;
116 u8 mux_page;
117 u16 led_mux;
118 u8 engine_mask;
119};
120
121struct lp5523_led {
122 int id;
123 u8 chan_nr;
124 u8 led_current;
125 u8 max_current;
126 struct led_classdev cdev;
127 struct work_struct brightness_work;
128 u8 brightness;
129};
130
131struct lp5523_chip {
132 struct mutex lock; /* Serialize control */
133 struct i2c_client *client;
134 struct lp5523_engine engines[LP5523_ENGINES];
135 struct lp5523_led leds[LP5523_LEDS];
136 struct lp5523_platform_data *pdata;
137 u8 num_channels;
138 u8 num_leds;
139};
140
141static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev)
142{ 103{
143 return container_of(cdev, struct lp5523_led, cdev); 104 usleep_range(1000, 2000);
144}
145
146static inline struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine)
147{
148 return container_of(engine, struct lp5523_chip,
149 engines[engine->id - 1]);
150}
151
152static inline struct lp5523_chip *led_to_lp5523(struct lp5523_led *led)
153{
154 return container_of(led, struct lp5523_chip,
155 leds[led->id]);
156} 105}
157 106
158static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode); 107static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
159static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode);
160static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern);
161
162static void lp5523_led_brightness_work(struct work_struct *work);
163
164static int lp5523_write(struct i2c_client *client, u8 reg, u8 value)
165{ 108{
166 return i2c_smbus_write_byte_data(client, reg, value); 109 led->led_current = led_current;
110 lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
111 led_current);
167} 112}
168 113
169static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf) 114static int lp5523_post_init_device(struct lp55xx_chip *chip)
170{
171 s32 ret = i2c_smbus_read_byte_data(client, reg);
172
173 if (ret < 0)
174 return ret;
175
176 *buf = ret;
177 return 0;
178}
179
180static int lp5523_detect(struct i2c_client *client)
181{ 115{
182 int ret; 116 int ret;
183 u8 buf;
184 117
185 ret = lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE); 118 ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
186 if (ret)
187 return ret;
188 ret = lp5523_read(client, LP5523_REG_ENABLE, &buf);
189 if (ret) 119 if (ret)
190 return ret; 120 return ret;
191 if (buf == 0x40)
192 return 0;
193 else
194 return -ENODEV;
195}
196 121
197static int lp5523_configure(struct i2c_client *client)
198{
199 struct lp5523_chip *chip = i2c_get_clientdata(client);
200 int ret = 0;
201 u8 status;
202
203 /* one pattern per engine setting led mux start and stop addresses */
204 static const u8 pattern[][LP5523_PROGRAM_LENGTH] = {
205 { 0x9c, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
206 { 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
207 { 0x9c, 0x50, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
208 };
209
210 ret |= lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE);
211 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */ 122 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
212 usleep_range(1000, 2000); 123 usleep_range(1000, 2000);
213 124
214 ret |= lp5523_write(client, LP5523_REG_CONFIG, 125 ret = lp55xx_write(chip, LP5523_REG_CONFIG,
215 LP5523_AUTO_INC | LP5523_PWR_SAVE | 126 LP5523_AUTO_INC | LP5523_PWR_SAVE |
216 LP5523_CP_AUTO | LP5523_AUTO_CLK | 127 LP5523_CP_AUTO | LP5523_AUTO_CLK |
217 LP5523_PWM_PWR_SAVE); 128 LP5523_PWM_PWR_SAVE);
129 if (ret)
130 return ret;
218 131
219 /* turn on all leds */ 132 /* turn on all leds */
220 ret |= lp5523_write(client, LP5523_REG_ENABLE_LEDS_MSB, 0x01); 133 ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
221 ret |= lp5523_write(client, LP5523_REG_ENABLE_LEDS_LSB, 0xff); 134 if (ret)
222
223 /* hardcode 32 bytes of memory for each engine from program memory */
224 ret |= lp5523_write(client, LP5523_REG_CH1_PROG_START, 0x00);
225 ret |= lp5523_write(client, LP5523_REG_CH2_PROG_START, 0x10);
226 ret |= lp5523_write(client, LP5523_REG_CH3_PROG_START, 0x20);
227
228 /* write led mux address space for each channel */
229 ret |= lp5523_load_program(&chip->engines[0], pattern[0]);
230 ret |= lp5523_load_program(&chip->engines[1], pattern[1]);
231 ret |= lp5523_load_program(&chip->engines[2], pattern[2]);
232
233 if (ret) {
234 dev_err(&client->dev, "could not load mux programs\n");
235 return -1;
236 }
237
238 /* set all engines exec state and mode to run 00101010 */
239 ret |= lp5523_write(client, LP5523_REG_ENABLE,
240 (LP5523_CMD_RUN | LP5523_ENABLE));
241
242 ret |= lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_RUN);
243
244 if (ret) {
245 dev_err(&client->dev, "could not start mux programs\n");
246 return -1;
247 }
248
249 /* Let the programs run for couple of ms and check the engine status */
250 usleep_range(3000, 6000);
251 ret = lp5523_read(client, LP5523_REG_STATUS, &status);
252 if (ret < 0)
253 return ret; 135 return ret;
254 136
255 status &= LP5523_ENG_STATUS_MASK; 137 return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
256
257 if (status == LP5523_ENG_STATUS_MASK) {
258 dev_dbg(&client->dev, "all engines configured\n");
259 } else {
260 dev_info(&client->dev, "status == %x\n", status);
261 dev_err(&client->dev, "cound not configure LED engine\n");
262 return -1;
263 }
264
265 dev_info(&client->dev, "disabling engines\n");
266
267 ret |= lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
268
269 return ret;
270} 138}
271 139
272static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode) 140static void lp5523_load_engine(struct lp55xx_chip *chip)
273{ 141{
274 struct lp5523_chip *chip = engine_to_lp5523(engine); 142 enum lp55xx_engine_index idx = chip->engine_idx;
275 struct i2c_client *client = chip->client; 143 u8 mask[] = {
276 int ret; 144 [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
277 u8 engine_state; 145 [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
146 [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
147 };
278 148
279 ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state); 149 u8 val[] = {
280 if (ret) 150 [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
281 goto fail; 151 [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
152 [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
153 };
282 154
283 engine_state &= ~(engine->engine_mask); 155 u8 page_sel[] = {
156 [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
157 [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
158 [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
159 };
284 160
285 /* set mode only for this engine */ 161 lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
286 mode &= engine->engine_mask;
287 162
288 engine_state |= mode; 163 lp5523_wait_opmode_done();
289 164
290 ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state); 165 lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
291fail:
292 return ret;
293} 166}
294 167
295static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux) 168static void lp5523_stop_engine(struct lp55xx_chip *chip)
296{ 169{
297 struct lp5523_chip *chip = engine_to_lp5523(engine); 170 lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
298 struct i2c_client *client = chip->client; 171 lp5523_wait_opmode_done();
299 int ret = 0; 172}
300
301 ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
302 173
303 ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page); 174static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
304 ret |= lp5523_write(client, LP5523_REG_PROG_MEM, 175{
305 (u8)(mux >> 8)); 176 int i;
306 ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux));
307 engine->led_mux = mux;
308 177
309 return ret; 178 for (i = 0; i < LP5523_MAX_LEDS; i++)
179 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
310} 180}
311 181
312static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern) 182static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
313{ 183{
314 struct lp5523_chip *chip = engine_to_lp5523(engine); 184 int ret;
315 struct i2c_client *client = chip->client; 185 u8 mode;
186 u8 exec;
316 187
317 int ret = 0; 188 /* stop engine */
189 if (!start) {
190 lp5523_stop_engine(chip);
191 lp5523_turn_off_channels(chip);
192 return;
193 }
318 194
319 ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD); 195 /*
196 * To run the engine,
197 * operation mode and enable register should updated at the same time
198 */
320 199
321 ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, 200 ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
322 engine->prog_page); 201 if (ret)
323 ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM, 202 return;
324 LP5523_PROGRAM_LENGTH, pattern);
325 203
326 return ret; 204 ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
327} 205 if (ret)
206 return;
328 207
329static int lp5523_run_program(struct lp5523_engine *engine) 208 /* change operation mode to RUN only when each engine is loading */
330{ 209 if (LP5523_ENG1_IS_LOADING(mode)) {
331 struct lp5523_chip *chip = engine_to_lp5523(engine); 210 mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
332 struct i2c_client *client = chip->client; 211 exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
333 int ret; 212 }
334 213
335 ret = lp5523_write(client, LP5523_REG_ENABLE, 214 if (LP5523_ENG2_IS_LOADING(mode)) {
336 LP5523_CMD_RUN | LP5523_ENABLE); 215 mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
337 if (ret) 216 exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
338 goto fail; 217 }
339 218
340 ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN); 219 if (LP5523_ENG3_IS_LOADING(mode)) {
341fail: 220 mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
342 return ret; 221 exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
222 }
223
224 lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
225 lp5523_wait_opmode_done();
226
227 lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
343} 228}
344 229
345static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len) 230static int lp5523_update_program_memory(struct lp55xx_chip *chip,
231 const u8 *data, size_t size)
346{ 232{
233 u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
234 unsigned cmd;
235 char c[3];
236 int update_size;
237 int nrchars;
238 int offset = 0;
239 int ret;
347 int i; 240 int i;
348 u16 tmp_mux = 0;
349
350 len = min_t(int, len, LP5523_LEDS);
351 for (i = 0; i < len; i++) {
352 switch (buf[i]) {
353 case '1':
354 tmp_mux |= (1 << i);
355 break;
356 case '0':
357 break;
358 case '\n':
359 i = len;
360 break;
361 default:
362 return -1;
363 }
364 }
365 *mux = tmp_mux;
366 241
367 return 0; 242 /* clear program memory before updating */
368} 243 for (i = 0; i < LP5523_PROGRAM_LENGTH; i++)
244 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0);
369 245
370static void lp5523_mux_to_array(u16 led_mux, char *array) 246 i = 0;
371{ 247 while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
372 int i, pos = 0; 248 /* separate sscanfs because length is working only for %s */
373 for (i = 0; i < LP5523_LEDS; i++) 249 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
374 pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i)); 250 if (ret != 1)
251 goto err;
375 252
376 array[pos] = '\0'; 253 ret = sscanf(c, "%2x", &cmd);
377} 254 if (ret != 1)
255 goto err;
378 256
379/*--------------------------------------------------------------*/ 257 pattern[i] = (u8)cmd;
380/* Sysfs interface */ 258 offset += nrchars;
381/*--------------------------------------------------------------*/ 259 i++;
260 }
382 261
383static ssize_t show_engine_leds(struct device *dev, 262 /* Each instruction is 16bit long. Check that length is even */
384 struct device_attribute *attr, 263 if (i % 2)
385 char *buf, int nr) 264 goto err;
386{
387 struct i2c_client *client = to_i2c_client(dev);
388 struct lp5523_chip *chip = i2c_get_clientdata(client);
389 char mux[LP5523_LEDS + 1];
390 265
391 lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux); 266 update_size = i;
267 for (i = 0; i < update_size; i++)
268 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
392 269
393 return sprintf(buf, "%s\n", mux); 270 return 0;
394}
395 271
396#define show_leds(nr) \ 272err:
397static ssize_t show_engine##nr##_leds(struct device *dev, \ 273 dev_err(&chip->cl->dev, "wrong pattern format\n");
398 struct device_attribute *attr, \ 274 return -EINVAL;
399 char *buf) \
400{ \
401 return show_engine_leds(dev, attr, buf, nr); \
402} 275}
403show_leds(1)
404show_leds(2)
405show_leds(3)
406 276
407static ssize_t store_engine_leds(struct device *dev, 277static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
408 struct device_attribute *attr,
409 const char *buf, size_t len, int nr)
410{ 278{
411 struct i2c_client *client = to_i2c_client(dev); 279 const struct firmware *fw = chip->fw;
412 struct lp5523_chip *chip = i2c_get_clientdata(client);
413 u16 mux = 0;
414 ssize_t ret;
415 280
416 if (lp5523_mux_parse(buf, &mux, len)) 281 if (fw->size > LP5523_PROGRAM_LENGTH) {
417 return -EINVAL; 282 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
418 283 fw->size);
419 mutex_lock(&chip->lock); 284 return;
420 ret = -EINVAL; 285 }
421 if (chip->engines[nr - 1].mode != LP5523_CMD_LOAD)
422 goto leave;
423 286
424 if (lp5523_load_mux(&chip->engines[nr - 1], mux)) 287 /*
425 goto leave; 288 * Program momery sequence
289 * 1) set engine mode to "LOAD"
290 * 2) write firmware data into program memory
291 */
426 292
427 ret = len; 293 lp5523_load_engine(chip);
428leave: 294 lp5523_update_program_memory(chip, fw->data, fw->size);
429 mutex_unlock(&chip->lock);
430 return ret;
431} 295}
432 296
433#define store_leds(nr) \
434static ssize_t store_engine##nr##_leds(struct device *dev, \
435 struct device_attribute *attr, \
436 const char *buf, size_t len) \
437{ \
438 return store_engine_leds(dev, attr, buf, len, nr); \
439}
440store_leds(1)
441store_leds(2)
442store_leds(3)
443
444static ssize_t lp5523_selftest(struct device *dev, 297static ssize_t lp5523_selftest(struct device *dev,
445 struct device_attribute *attr, 298 struct device_attribute *attr,
446 char *buf) 299 char *buf)
447{ 300{
448 struct i2c_client *client = to_i2c_client(dev); 301 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
449 struct lp5523_chip *chip = i2c_get_clientdata(client); 302 struct lp55xx_chip *chip = led->chip;
303 struct lp55xx_platform_data *pdata = chip->pdata;
450 int i, ret, pos = 0; 304 int i, ret, pos = 0;
451 int led = 0;
452 u8 status, adc, vdd; 305 u8 status, adc, vdd;
453 306
454 mutex_lock(&chip->lock); 307 mutex_lock(&chip->lock);
455 308
456 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); 309 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
457 if (ret < 0) 310 if (ret < 0)
458 goto fail; 311 goto fail;
459 312
460 /* Check that ext clock is really in use if requested */ 313 /* Check that ext clock is really in use if requested */
461 if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT)) 314 if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
462 if ((status & LP5523_EXT_CLK_USED) == 0) 315 if ((status & LP5523_EXT_CLK_USED) == 0)
463 goto fail; 316 goto fail;
317 }
464 318
465 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */ 319 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
466 lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL, 320 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
467 LP5523_EN_LEDTEST | 16);
468 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */ 321 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
469 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); 322 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
470 if (ret < 0) 323 if (ret < 0)
471 goto fail; 324 goto fail;
472 325
473 if (!(status & LP5523_LEDTEST_DONE)) 326 if (!(status & LP5523_LEDTEST_DONE))
474 usleep_range(3000, 6000); /* Was not ready. Wait little bit */ 327 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
475 328
476 ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd); 329 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
477 if (ret < 0) 330 if (ret < 0)
478 goto fail; 331 goto fail;
479 332
480 vdd--; /* There may be some fluctuation in measurement */ 333 vdd--; /* There may be some fluctuation in measurement */
481 334
482 for (i = 0; i < LP5523_LEDS; i++) { 335 for (i = 0; i < LP5523_MAX_LEDS; i++) {
483 /* Skip non-existing channels */ 336 /* Skip non-existing channels */
484 if (chip->pdata->led_config[i].led_current == 0) 337 if (pdata->led_config[i].led_current == 0)
485 continue; 338 continue;
486 339
487 /* Set default current */ 340 /* Set default current */
488 lp5523_write(chip->client, 341 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
489 LP5523_REG_LED_CURRENT_BASE + i, 342 pdata->led_config[i].led_current);
490 chip->pdata->led_config[i].led_current);
491 343
492 lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff); 344 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
493 /* let current stabilize 2 - 4ms before measurements start */ 345 /* let current stabilize 2 - 4ms before measurements start */
494 usleep_range(2000, 4000); 346 usleep_range(2000, 4000);
495 lp5523_write(chip->client, 347 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
496 LP5523_REG_LED_TEST_CTRL,
497 LP5523_EN_LEDTEST | i); 348 LP5523_EN_LEDTEST | i);
498 /* ADC conversion time is 2.7 ms typically */ 349 /* ADC conversion time is 2.7 ms typically */
499 usleep_range(3000, 6000); 350 usleep_range(3000, 6000);
500 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); 351 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
501 if (ret < 0) 352 if (ret < 0)
502 goto fail; 353 goto fail;
503 354
504 if (!(status & LP5523_LEDTEST_DONE)) 355 if (!(status & LP5523_LEDTEST_DONE))
505 usleep_range(3000, 6000);/* Was not ready. Wait. */ 356 usleep_range(3000, 6000);/* Was not ready. Wait. */
506 ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc); 357
358 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
507 if (ret < 0) 359 if (ret < 0)
508 goto fail; 360 goto fail;
509 361
510 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM) 362 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
511 pos += sprintf(buf + pos, "LED %d FAIL\n", i); 363 pos += sprintf(buf + pos, "LED %d FAIL\n", i);
512 364
513 lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00); 365 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
514 366
515 /* Restore current */ 367 /* Restore current */
516 lp5523_write(chip->client, 368 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
517 LP5523_REG_LED_CURRENT_BASE + i, 369 led->led_current);
518 chip->leds[led].led_current);
519 led++; 370 led++;
520 } 371 }
521 if (pos == 0) 372 if (pos == 0)
@@ -530,249 +381,22 @@ release_lock:
530 return pos; 381 return pos;
531} 382}
532 383
533static void lp5523_set_brightness(struct led_classdev *cdev,
534 enum led_brightness brightness)
535{
536 struct lp5523_led *led = cdev_to_led(cdev);
537
538 led->brightness = (u8)brightness;
539
540 schedule_work(&led->brightness_work);
541}
542
543static void lp5523_led_brightness_work(struct work_struct *work) 384static void lp5523_led_brightness_work(struct work_struct *work)
544{ 385{
545 struct lp5523_led *led = container_of(work, 386 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
546 struct lp5523_led,
547 brightness_work); 387 brightness_work);
548 struct lp5523_chip *chip = led_to_lp5523(led); 388 struct lp55xx_chip *chip = led->chip;
549 struct i2c_client *client = chip->client;
550 389
551 mutex_lock(&chip->lock); 390 mutex_lock(&chip->lock);
552 391 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
553 lp5523_write(client, LP5523_REG_LED_PWM_BASE + led->chan_nr,
554 led->brightness); 392 led->brightness);
555
556 mutex_unlock(&chip->lock);
557}
558
559static int lp5523_do_store_load(struct lp5523_engine *engine,
560 const char *buf, size_t len)
561{
562 struct lp5523_chip *chip = engine_to_lp5523(engine);
563 struct i2c_client *client = chip->client;
564 int ret, nrchars, offset = 0, i = 0;
565 char c[3];
566 unsigned cmd;
567 u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
568
569 if (engine->mode != LP5523_CMD_LOAD)
570 return -EINVAL;
571
572 while ((offset < len - 1) && (i < LP5523_PROGRAM_LENGTH)) {
573 /* separate sscanfs because length is working only for %s */
574 ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
575 ret = sscanf(c, "%2x", &cmd);
576 if (ret != 1)
577 goto fail;
578 pattern[i] = (u8)cmd;
579
580 offset += nrchars;
581 i++;
582 }
583
584 /* Each instruction is 16bit long. Check that length is even */
585 if (i % 2)
586 goto fail;
587
588 mutex_lock(&chip->lock);
589 ret = lp5523_load_program(engine, pattern);
590 mutex_unlock(&chip->lock); 393 mutex_unlock(&chip->lock);
591
592 if (ret) {
593 dev_err(&client->dev, "failed loading pattern\n");
594 return ret;
595 }
596
597 return len;
598fail:
599 dev_err(&client->dev, "wrong pattern format\n");
600 return -EINVAL;
601}
602
603static ssize_t store_engine_load(struct device *dev,
604 struct device_attribute *attr,
605 const char *buf, size_t len, int nr)
606{
607 struct i2c_client *client = to_i2c_client(dev);
608 struct lp5523_chip *chip = i2c_get_clientdata(client);
609 return lp5523_do_store_load(&chip->engines[nr - 1], buf, len);
610} 394}
611 395
612#define store_load(nr) \
613static ssize_t store_engine##nr##_load(struct device *dev, \
614 struct device_attribute *attr, \
615 const char *buf, size_t len) \
616{ \
617 return store_engine_load(dev, attr, buf, len, nr); \
618}
619store_load(1)
620store_load(2)
621store_load(3)
622
623static ssize_t show_engine_mode(struct device *dev,
624 struct device_attribute *attr,
625 char *buf, int nr)
626{
627 struct i2c_client *client = to_i2c_client(dev);
628 struct lp5523_chip *chip = i2c_get_clientdata(client);
629 switch (chip->engines[nr - 1].mode) {
630 case LP5523_CMD_RUN:
631 return sprintf(buf, "run\n");
632 case LP5523_CMD_LOAD:
633 return sprintf(buf, "load\n");
634 case LP5523_CMD_DISABLED:
635 return sprintf(buf, "disabled\n");
636 default:
637 return sprintf(buf, "disabled\n");
638 }
639}
640
641#define show_mode(nr) \
642static ssize_t show_engine##nr##_mode(struct device *dev, \
643 struct device_attribute *attr, \
644 char *buf) \
645{ \
646 return show_engine_mode(dev, attr, buf, nr); \
647}
648show_mode(1)
649show_mode(2)
650show_mode(3)
651
652static ssize_t store_engine_mode(struct device *dev,
653 struct device_attribute *attr,
654 const char *buf, size_t len, int nr)
655{
656 struct i2c_client *client = to_i2c_client(dev);
657 struct lp5523_chip *chip = i2c_get_clientdata(client);
658 struct lp5523_engine *engine = &chip->engines[nr - 1];
659 mutex_lock(&chip->lock);
660
661 if (!strncmp(buf, "run", 3))
662 lp5523_set_mode(engine, LP5523_CMD_RUN);
663 else if (!strncmp(buf, "load", 4))
664 lp5523_set_mode(engine, LP5523_CMD_LOAD);
665 else if (!strncmp(buf, "disabled", 8))
666 lp5523_set_mode(engine, LP5523_CMD_DISABLED);
667
668 mutex_unlock(&chip->lock);
669 return len;
670}
671
672#define store_mode(nr) \
673static ssize_t store_engine##nr##_mode(struct device *dev, \
674 struct device_attribute *attr, \
675 const char *buf, size_t len) \
676{ \
677 return store_engine_mode(dev, attr, buf, len, nr); \
678}
679store_mode(1)
680store_mode(2)
681store_mode(3)
682
683static ssize_t show_max_current(struct device *dev,
684 struct device_attribute *attr,
685 char *buf)
686{
687 struct led_classdev *led_cdev = dev_get_drvdata(dev);
688 struct lp5523_led *led = cdev_to_led(led_cdev);
689
690 return sprintf(buf, "%d\n", led->max_current);
691}
692
693static ssize_t show_current(struct device *dev,
694 struct device_attribute *attr,
695 char *buf)
696{
697 struct led_classdev *led_cdev = dev_get_drvdata(dev);
698 struct lp5523_led *led = cdev_to_led(led_cdev);
699
700 return sprintf(buf, "%d\n", led->led_current);
701}
702
703static ssize_t store_current(struct device *dev,
704 struct device_attribute *attr,
705 const char *buf, size_t len)
706{
707 struct led_classdev *led_cdev = dev_get_drvdata(dev);
708 struct lp5523_led *led = cdev_to_led(led_cdev);
709 struct lp5523_chip *chip = led_to_lp5523(led);
710 ssize_t ret;
711 unsigned long curr;
712
713 if (kstrtoul(buf, 0, &curr))
714 return -EINVAL;
715
716 if (curr > led->max_current)
717 return -EINVAL;
718
719 mutex_lock(&chip->lock);
720 ret = lp5523_write(chip->client,
721 LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
722 (u8)curr);
723 mutex_unlock(&chip->lock);
724
725 if (ret < 0)
726 return ret;
727
728 led->led_current = (u8)curr;
729
730 return len;
731}
732
733/* led class device attributes */
734static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
735static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
736
737static struct attribute *lp5523_led_attributes[] = {
738 &dev_attr_led_current.attr,
739 &dev_attr_max_current.attr,
740 NULL,
741};
742
743static struct attribute_group lp5523_led_attribute_group = {
744 .attrs = lp5523_led_attributes
745};
746
747/* device attributes */
748static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
749 show_engine1_mode, store_engine1_mode);
750static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
751 show_engine2_mode, store_engine2_mode);
752static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
753 show_engine3_mode, store_engine3_mode);
754static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUSR,
755 show_engine1_leds, store_engine1_leds);
756static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUSR,
757 show_engine2_leds, store_engine2_leds);
758static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUSR,
759 show_engine3_leds, store_engine3_leds);
760static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
761static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
762static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
763static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL); 396static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
764 397
765static struct attribute *lp5523_attributes[] = { 398static struct attribute *lp5523_attributes[] = {
766 &dev_attr_engine1_mode.attr,
767 &dev_attr_engine2_mode.attr,
768 &dev_attr_engine3_mode.attr,
769 &dev_attr_selftest.attr, 399 &dev_attr_selftest.attr,
770 &dev_attr_engine1_load.attr,
771 &dev_attr_engine1_leds.attr,
772 &dev_attr_engine2_load.attr,
773 &dev_attr_engine2_leds.attr,
774 &dev_attr_engine3_load.attr,
775 &dev_attr_engine3_leds.attr,
776 NULL, 400 NULL,
777}; 401};
778 402
@@ -780,252 +404,91 @@ static const struct attribute_group lp5523_group = {
780 .attrs = lp5523_attributes, 404 .attrs = lp5523_attributes,
781}; 405};
782 406
783static int lp5523_register_sysfs(struct i2c_client *client) 407/* Chip specific configurations */
784{ 408static struct lp55xx_device_config lp5523_cfg = {
785 struct device *dev = &client->dev; 409 .reset = {
786 int ret; 410 .addr = LP5523_REG_RESET,
787 411 .val = LP5523_RESET,
788 ret = sysfs_create_group(&dev->kobj, &lp5523_group); 412 },
789 if (ret < 0) 413 .enable = {
790 return ret; 414 .addr = LP5523_REG_ENABLE,
791 415 .val = LP5523_ENABLE,
792 return 0; 416 },
793} 417 .max_channel = LP5523_MAX_LEDS,
794 418 .post_init_device = lp5523_post_init_device,
795static void lp5523_unregister_sysfs(struct i2c_client *client) 419 .brightness_work_fn = lp5523_led_brightness_work,
796{ 420 .set_led_current = lp5523_set_led_current,
797 struct lp5523_chip *chip = i2c_get_clientdata(client); 421 .firmware_cb = lp5523_firmware_loaded,
798 struct device *dev = &client->dev; 422 .run_engine = lp5523_run_engine,
799 int i; 423 .dev_attr_group = &lp5523_group,
800 424};
801 sysfs_remove_group(&dev->kobj, &lp5523_group);
802
803 for (i = 0; i < chip->num_leds; i++)
804 sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
805 &lp5523_led_attribute_group);
806}
807
808/*--------------------------------------------------------------*/
809/* Set chip operating mode */
810/*--------------------------------------------------------------*/
811static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode)
812{
813 /* if in that mode already do nothing, except for run */
814 if (mode == engine->mode && mode != LP5523_CMD_RUN)
815 return;
816
817 switch (mode) {
818 case LP5523_CMD_RUN:
819 lp5523_run_program(engine);
820 break;
821 case LP5523_CMD_LOAD:
822 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
823 lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
824 break;
825 case LP5523_CMD_DISABLED:
826 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
827 break;
828 default:
829 return;
830 }
831
832 engine->mode = mode;
833}
834
835/*--------------------------------------------------------------*/
836/* Probe, Attach, Remove */
837/*--------------------------------------------------------------*/
838static int __init lp5523_init_engine(struct lp5523_engine *engine, int id)
839{
840 if (id < 1 || id > LP5523_ENGINES)
841 return -1;
842 engine->id = id;
843 engine->engine_mask = LP5523_ENG_MASK_BASE >> SHIFT_MASK(id);
844 engine->prog_page = id - 1;
845 engine->mux_page = id + 2;
846
847 return 0;
848}
849 425
850static int lp5523_init_led(struct lp5523_led *led, struct device *dev, 426static int lp5523_probe(struct i2c_client *client,
851 int chan, struct lp5523_platform_data *pdata, 427 const struct i2c_device_id *id)
852 const char *chip_name)
853{ 428{
854 char name[32]; 429 int ret;
855 int res; 430 struct lp55xx_chip *chip;
431 struct lp55xx_led *led;
432 struct lp55xx_platform_data *pdata = client->dev.platform_data;
856 433
857 if (chan >= LP5523_LEDS) 434 if (!pdata) {
435 dev_err(&client->dev, "no platform data\n");
858 return -EINVAL; 436 return -EINVAL;
859
860 if (pdata->led_config[chan].led_current) {
861 led->led_current = pdata->led_config[chan].led_current;
862 led->max_current = pdata->led_config[chan].max_current;
863 led->chan_nr = pdata->led_config[chan].chan_nr;
864
865 if (led->chan_nr >= LP5523_LEDS) {
866 dev_err(dev, "Use channel numbers between 0 and %d\n",
867 LP5523_LEDS - 1);
868 return -EINVAL;
869 }
870
871 if (pdata->led_config[chan].name) {
872 led->cdev.name = pdata->led_config[chan].name;
873 } else {
874 snprintf(name, sizeof(name), "%s:channel%d",
875 pdata->label ? : chip_name, chan);
876 led->cdev.name = name;
877 }
878
879 led->cdev.brightness_set = lp5523_set_brightness;
880 res = led_classdev_register(dev, &led->cdev);
881 if (res < 0) {
882 dev_err(dev, "couldn't register led on channel %d\n",
883 chan);
884 return res;
885 }
886 res = sysfs_create_group(&led->cdev.dev->kobj,
887 &lp5523_led_attribute_group);
888 if (res < 0) {
889 dev_err(dev, "couldn't register current attribute\n");
890 led_classdev_unregister(&led->cdev);
891 return res;
892 }
893 } else {
894 led->led_current = 0;
895 } 437 }
896 return 0;
897}
898
899static int lp5523_probe(struct i2c_client *client,
900 const struct i2c_device_id *id)
901{
902 struct lp5523_chip *chip;
903 struct lp5523_platform_data *pdata;
904 int ret, i, led;
905 438
906 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); 439 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
907 if (!chip) 440 if (!chip)
908 return -ENOMEM; 441 return -ENOMEM;
909 442
910 i2c_set_clientdata(client, chip); 443 led = devm_kzalloc(&client->dev,
911 chip->client = client; 444 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
445 if (!led)
446 return -ENOMEM;
912 447
913 pdata = client->dev.platform_data; 448 chip->cl = client;
914 449 chip->pdata = pdata;
915 if (!pdata) { 450 chip->cfg = &lp5523_cfg;
916 dev_err(&client->dev, "no platform data\n");
917 return -EINVAL;
918 }
919 451
920 mutex_init(&chip->lock); 452 mutex_init(&chip->lock);
921 453
922 chip->pdata = pdata; 454 i2c_set_clientdata(client, led);
923 455
924 if (pdata->setup_resources) { 456 ret = lp55xx_init_device(chip);
925 ret = pdata->setup_resources();
926 if (ret < 0)
927 return ret;
928 }
929
930 if (pdata->enable) {
931 pdata->enable(0);
932 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
933 pdata->enable(1);
934 usleep_range(1000, 2000); /* 500us abs min. */
935 }
936
937 lp5523_write(client, LP5523_REG_RESET, 0xff);
938 usleep_range(10000, 20000); /*
939 * Exact value is not available. 10 - 20ms
940 * appears to be enough for reset.
941 */
942 ret = lp5523_detect(client);
943 if (ret) 457 if (ret)
944 goto fail1; 458 goto err_init;
945 459
946 dev_info(&client->dev, "%s Programmable led chip found\n", id->name); 460 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
947 461
948 /* Initialize engines */ 462 ret = lp55xx_register_leds(led, chip);
949 for (i = 0; i < ARRAY_SIZE(chip->engines); i++) { 463 if (ret)
950 ret = lp5523_init_engine(&chip->engines[i], i + 1); 464 goto err_register_leds;
951 if (ret) {
952 dev_err(&client->dev, "error initializing engine\n");
953 goto fail1;
954 }
955 }
956 ret = lp5523_configure(client);
957 if (ret < 0) {
958 dev_err(&client->dev, "error configuring chip\n");
959 goto fail1;
960 }
961
962 /* Initialize leds */
963 chip->num_channels = pdata->num_channels;
964 chip->num_leds = 0;
965 led = 0;
966 for (i = 0; i < pdata->num_channels; i++) {
967 /* Do not initialize channels that are not connected */
968 if (pdata->led_config[i].led_current == 0)
969 continue;
970
971 INIT_WORK(&chip->leds[led].brightness_work,
972 lp5523_led_brightness_work);
973
974 ret = lp5523_init_led(&chip->leds[led], &client->dev, i, pdata,
975 id->name);
976 if (ret) {
977 dev_err(&client->dev, "error initializing leds\n");
978 goto fail2;
979 }
980 chip->num_leds++;
981
982 chip->leds[led].id = led;
983 /* Set LED current */
984 lp5523_write(client,
985 LP5523_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
986 chip->leds[led].led_current);
987
988 led++;
989 }
990 465
991 ret = lp5523_register_sysfs(client); 466 ret = lp55xx_register_sysfs(chip);
992 if (ret) { 467 if (ret) {
993 dev_err(&client->dev, "registering sysfs failed\n"); 468 dev_err(&client->dev, "registering sysfs failed\n");
994 goto fail2; 469 goto err_register_sysfs;
995 } 470 }
996 return ret; 471
997fail2: 472 return 0;
998 for (i = 0; i < chip->num_leds; i++) { 473
999 led_classdev_unregister(&chip->leds[i].cdev); 474err_register_sysfs:
1000 flush_work(&chip->leds[i].brightness_work); 475 lp55xx_unregister_leds(led, chip);
1001 } 476err_register_leds:
1002fail1: 477 lp55xx_deinit_device(chip);
1003 if (pdata->enable) 478err_init:
1004 pdata->enable(0);
1005 if (pdata->release_resources)
1006 pdata->release_resources();
1007 return ret; 479 return ret;
1008} 480}
1009 481
1010static int lp5523_remove(struct i2c_client *client) 482static int lp5523_remove(struct i2c_client *client)
1011{ 483{
1012 struct lp5523_chip *chip = i2c_get_clientdata(client); 484 struct lp55xx_led *led = i2c_get_clientdata(client);
1013 int i; 485 struct lp55xx_chip *chip = led->chip;
1014
1015 /* Disable engine mode */
1016 lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
1017 486
1018 lp5523_unregister_sysfs(client); 487 lp5523_stop_engine(chip);
1019 488 lp55xx_unregister_sysfs(chip);
1020 for (i = 0; i < chip->num_leds; i++) { 489 lp55xx_unregister_leds(led, chip);
1021 led_classdev_unregister(&chip->leds[i].cdev); 490 lp55xx_deinit_device(chip);
1022 flush_work(&chip->leds[i].brightness_work);
1023 }
1024 491
1025 if (chip->pdata->enable)
1026 chip->pdata->enable(0);
1027 if (chip->pdata->release_resources)
1028 chip->pdata->release_resources();
1029 return 0; 492 return 0;
1030} 493}
1031 494
@@ -1049,5 +512,6 @@ static struct i2c_driver lp5523_driver = {
1049module_i2c_driver(lp5523_driver); 512module_i2c_driver(lp5523_driver);
1050 513
1051MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>"); 514MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
515MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
1052MODULE_DESCRIPTION("LP5523 LED engine"); 516MODULE_DESCRIPTION("LP5523 LED engine");
1053MODULE_LICENSE("GPL"); 517MODULE_LICENSE("GPL");
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
new file mode 100644
index 000000000000..d9eb84157423
--- /dev/null
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -0,0 +1,523 @@
1/*
2 * LP5521/LP5523/LP55231 Common Driver
3 *
4 * Copyright 2012 Texas Instruments
5 *
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Derived from leds-lp5521.c, leds-lp5523.c
13 */
14
15#include <linux/delay.h>
16#include <linux/firmware.h>
17#include <linux/i2c.h>
18#include <linux/leds.h>
19#include <linux/module.h>
20#include <linux/platform_data/leds-lp55xx.h>
21
22#include "leds-lp55xx-common.h"
23
24static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
25{
26 return container_of(cdev, struct lp55xx_led, cdev);
27}
28
29static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
30{
31 return cdev_to_lp55xx_led(dev_get_drvdata(dev));
32}
33
34static void lp55xx_reset_device(struct lp55xx_chip *chip)
35{
36 struct lp55xx_device_config *cfg = chip->cfg;
37 u8 addr = cfg->reset.addr;
38 u8 val = cfg->reset.val;
39
40 /* no error checking here because no ACK from the device after reset */
41 lp55xx_write(chip, addr, val);
42}
43
44static int lp55xx_detect_device(struct lp55xx_chip *chip)
45{
46 struct lp55xx_device_config *cfg = chip->cfg;
47 u8 addr = cfg->enable.addr;
48 u8 val = cfg->enable.val;
49 int ret;
50
51 ret = lp55xx_write(chip, addr, val);
52 if (ret)
53 return ret;
54
55 usleep_range(1000, 2000);
56
57 ret = lp55xx_read(chip, addr, &val);
58 if (ret)
59 return ret;
60
61 if (val != cfg->enable.val)
62 return -ENODEV;
63
64 return 0;
65}
66
67static int lp55xx_post_init_device(struct lp55xx_chip *chip)
68{
69 struct lp55xx_device_config *cfg = chip->cfg;
70
71 if (!cfg->post_init_device)
72 return 0;
73
74 return cfg->post_init_device(chip);
75}
76
77static ssize_t lp55xx_show_current(struct device *dev,
78 struct device_attribute *attr,
79 char *buf)
80{
81 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
82
83 return sprintf(buf, "%d\n", led->led_current);
84}
85
86static ssize_t lp55xx_store_current(struct device *dev,
87 struct device_attribute *attr,
88 const char *buf, size_t len)
89{
90 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
91 struct lp55xx_chip *chip = led->chip;
92 unsigned long curr;
93
94 if (kstrtoul(buf, 0, &curr))
95 return -EINVAL;
96
97 if (curr > led->max_current)
98 return -EINVAL;
99
100 if (!chip->cfg->set_led_current)
101 return len;
102
103 mutex_lock(&chip->lock);
104 chip->cfg->set_led_current(led, (u8)curr);
105 mutex_unlock(&chip->lock);
106
107 return len;
108}
109
110static ssize_t lp55xx_show_max_current(struct device *dev,
111 struct device_attribute *attr,
112 char *buf)
113{
114 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
115
116 return sprintf(buf, "%d\n", led->max_current);
117}
118
119static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, lp55xx_show_current,
120 lp55xx_store_current);
121static DEVICE_ATTR(max_current, S_IRUGO , lp55xx_show_max_current, NULL);
122
123static struct attribute *lp55xx_led_attributes[] = {
124 &dev_attr_led_current.attr,
125 &dev_attr_max_current.attr,
126 NULL,
127};
128
129static struct attribute_group lp55xx_led_attr_group = {
130 .attrs = lp55xx_led_attributes
131};
132
133static void lp55xx_set_brightness(struct led_classdev *cdev,
134 enum led_brightness brightness)
135{
136 struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
137
138 led->brightness = (u8)brightness;
139 schedule_work(&led->brightness_work);
140}
141
142static int lp55xx_init_led(struct lp55xx_led *led,
143 struct lp55xx_chip *chip, int chan)
144{
145 struct lp55xx_platform_data *pdata = chip->pdata;
146 struct lp55xx_device_config *cfg = chip->cfg;
147 struct device *dev = &chip->cl->dev;
148 char name[32];
149 int ret;
150 int max_channel = cfg->max_channel;
151
152 if (chan >= max_channel) {
153 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
154 return -EINVAL;
155 }
156
157 if (pdata->led_config[chan].led_current == 0)
158 return 0;
159
160 led->led_current = pdata->led_config[chan].led_current;
161 led->max_current = pdata->led_config[chan].max_current;
162 led->chan_nr = pdata->led_config[chan].chan_nr;
163
164 if (led->chan_nr >= max_channel) {
165 dev_err(dev, "Use channel numbers between 0 and %d\n",
166 max_channel - 1);
167 return -EINVAL;
168 }
169
170 led->cdev.brightness_set = lp55xx_set_brightness;
171
172 if (pdata->led_config[chan].name) {
173 led->cdev.name = pdata->led_config[chan].name;
174 } else {
175 snprintf(name, sizeof(name), "%s:channel%d",
176 pdata->label ? : chip->cl->name, chan);
177 led->cdev.name = name;
178 }
179
180 /*
181 * register led class device for each channel and
182 * add device attributes
183 */
184
185 ret = led_classdev_register(dev, &led->cdev);
186 if (ret) {
187 dev_err(dev, "led register err: %d\n", ret);
188 return ret;
189 }
190
191 ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
192 if (ret) {
193 dev_err(dev, "led sysfs err: %d\n", ret);
194 led_classdev_unregister(&led->cdev);
195 return ret;
196 }
197
198 return 0;
199}
200
201static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
202{
203 struct lp55xx_chip *chip = context;
204 struct device *dev = &chip->cl->dev;
205
206 if (!fw) {
207 dev_err(dev, "firmware request failed\n");
208 goto out;
209 }
210
211 /* handling firmware data is chip dependent */
212 mutex_lock(&chip->lock);
213
214 chip->fw = fw;
215 if (chip->cfg->firmware_cb)
216 chip->cfg->firmware_cb(chip);
217
218 mutex_unlock(&chip->lock);
219
220out:
221 /* firmware should be released for other channel use */
222 release_firmware(chip->fw);
223}
224
225static int lp55xx_request_firmware(struct lp55xx_chip *chip)
226{
227 const char *name = chip->cl->name;
228 struct device *dev = &chip->cl->dev;
229
230 return request_firmware_nowait(THIS_MODULE, true, name, dev,
231 GFP_KERNEL, chip, lp55xx_firmware_loaded);
232}
233
234static ssize_t lp55xx_show_engine_select(struct device *dev,
235 struct device_attribute *attr,
236 char *buf)
237{
238 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
239 struct lp55xx_chip *chip = led->chip;
240
241 return sprintf(buf, "%d\n", chip->engine_idx);
242}
243
244static ssize_t lp55xx_store_engine_select(struct device *dev,
245 struct device_attribute *attr,
246 const char *buf, size_t len)
247{
248 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
249 struct lp55xx_chip *chip = led->chip;
250 unsigned long val;
251 int ret;
252
253 if (kstrtoul(buf, 0, &val))
254 return -EINVAL;
255
256 /* select the engine to be run */
257
258 switch (val) {
259 case LP55XX_ENGINE_1:
260 case LP55XX_ENGINE_2:
261 case LP55XX_ENGINE_3:
262 mutex_lock(&chip->lock);
263 chip->engine_idx = val;
264 ret = lp55xx_request_firmware(chip);
265 mutex_unlock(&chip->lock);
266 break;
267 default:
268 dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
269 return -EINVAL;
270 }
271
272 if (ret) {
273 dev_err(dev, "request firmware err: %d\n", ret);
274 return ret;
275 }
276
277 return len;
278}
279
280static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
281{
282 if (chip->cfg->run_engine)
283 chip->cfg->run_engine(chip, start);
284}
285
286static ssize_t lp55xx_store_engine_run(struct device *dev,
287 struct device_attribute *attr,
288 const char *buf, size_t len)
289{
290 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
291 struct lp55xx_chip *chip = led->chip;
292 unsigned long val;
293
294 if (kstrtoul(buf, 0, &val))
295 return -EINVAL;
296
297 /* run or stop the selected engine */
298
299 if (val <= 0) {
300 lp55xx_run_engine(chip, false);
301 return len;
302 }
303
304 mutex_lock(&chip->lock);
305 lp55xx_run_engine(chip, true);
306 mutex_unlock(&chip->lock);
307
308 return len;
309}
310
311static DEVICE_ATTR(select_engine, S_IRUGO | S_IWUSR,
312 lp55xx_show_engine_select, lp55xx_store_engine_select);
313static DEVICE_ATTR(run_engine, S_IWUSR, NULL, lp55xx_store_engine_run);
314
315static struct attribute *lp55xx_engine_attributes[] = {
316 &dev_attr_select_engine.attr,
317 &dev_attr_run_engine.attr,
318 NULL,
319};
320
321static const struct attribute_group lp55xx_engine_attr_group = {
322 .attrs = lp55xx_engine_attributes,
323};
324
325int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
326{
327 return i2c_smbus_write_byte_data(chip->cl, reg, val);
328}
329EXPORT_SYMBOL_GPL(lp55xx_write);
330
331int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
332{
333 s32 ret;
334
335 ret = i2c_smbus_read_byte_data(chip->cl, reg);
336 if (ret < 0)
337 return ret;
338
339 *val = ret;
340 return 0;
341}
342EXPORT_SYMBOL_GPL(lp55xx_read);
343
344int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
345{
346 int ret;
347 u8 tmp;
348
349 ret = lp55xx_read(chip, reg, &tmp);
350 if (ret)
351 return ret;
352
353 tmp &= ~mask;
354 tmp |= val & mask;
355
356 return lp55xx_write(chip, reg, tmp);
357}
358EXPORT_SYMBOL_GPL(lp55xx_update_bits);
359
360int lp55xx_init_device(struct lp55xx_chip *chip)
361{
362 struct lp55xx_platform_data *pdata;
363 struct lp55xx_device_config *cfg;
364 struct device *dev = &chip->cl->dev;
365 int ret = 0;
366
367 WARN_ON(!chip);
368
369 pdata = chip->pdata;
370 cfg = chip->cfg;
371
372 if (!pdata || !cfg)
373 return -EINVAL;
374
375 if (pdata->setup_resources) {
376 ret = pdata->setup_resources();
377 if (ret < 0) {
378 dev_err(dev, "setup resoure err: %d\n", ret);
379 goto err;
380 }
381 }
382
383 if (pdata->enable) {
384 pdata->enable(0);
385 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
386 pdata->enable(1);
387 usleep_range(1000, 2000); /* 500us abs min. */
388 }
389
390 lp55xx_reset_device(chip);
391
392 /*
393 * Exact value is not available. 10 - 20ms
394 * appears to be enough for reset.
395 */
396 usleep_range(10000, 20000);
397
398 ret = lp55xx_detect_device(chip);
399 if (ret) {
400 dev_err(dev, "device detection err: %d\n", ret);
401 goto err;
402 }
403
404 /* chip specific initialization */
405 ret = lp55xx_post_init_device(chip);
406 if (ret) {
407 dev_err(dev, "post init device err: %d\n", ret);
408 goto err_post_init;
409 }
410
411 return 0;
412
413err_post_init:
414 lp55xx_deinit_device(chip);
415err:
416 return ret;
417}
418EXPORT_SYMBOL_GPL(lp55xx_init_device);
419
420void lp55xx_deinit_device(struct lp55xx_chip *chip)
421{
422 struct lp55xx_platform_data *pdata = chip->pdata;
423
424 if (pdata->enable)
425 pdata->enable(0);
426
427 if (pdata->release_resources)
428 pdata->release_resources();
429}
430EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
431
432int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
433{
434 struct lp55xx_platform_data *pdata = chip->pdata;
435 struct lp55xx_device_config *cfg = chip->cfg;
436 int num_channels = pdata->num_channels;
437 struct lp55xx_led *each;
438 u8 led_current;
439 int ret;
440 int i;
441
442 if (!cfg->brightness_work_fn) {
443 dev_err(&chip->cl->dev, "empty brightness configuration\n");
444 return -EINVAL;
445 }
446
447 for (i = 0; i < num_channels; i++) {
448
449 /* do not initialize channels that are not connected */
450 if (pdata->led_config[i].led_current == 0)
451 continue;
452
453 led_current = pdata->led_config[i].led_current;
454 each = led + i;
455 ret = lp55xx_init_led(each, chip, i);
456 if (ret)
457 goto err_init_led;
458
459 INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
460
461 chip->num_leds++;
462 each->chip = chip;
463
464 /* setting led current at each channel */
465 if (cfg->set_led_current)
466 cfg->set_led_current(each, led_current);
467 }
468
469 return 0;
470
471err_init_led:
472 lp55xx_unregister_leds(led, chip);
473 return ret;
474}
475EXPORT_SYMBOL_GPL(lp55xx_register_leds);
476
477void lp55xx_unregister_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
478{
479 int i;
480 struct lp55xx_led *each;
481
482 for (i = 0; i < chip->num_leds; i++) {
483 each = led + i;
484 led_classdev_unregister(&each->cdev);
485 flush_work(&each->brightness_work);
486 }
487}
488EXPORT_SYMBOL_GPL(lp55xx_unregister_leds);
489
490int lp55xx_register_sysfs(struct lp55xx_chip *chip)
491{
492 struct device *dev = &chip->cl->dev;
493 struct lp55xx_device_config *cfg = chip->cfg;
494 int ret;
495
496 if (!cfg->run_engine || !cfg->firmware_cb)
497 goto dev_specific_attrs;
498
499 ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
500 if (ret)
501 return ret;
502
503dev_specific_attrs:
504 return cfg->dev_attr_group ?
505 sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
506}
507EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
508
509void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
510{
511 struct device *dev = &chip->cl->dev;
512 struct lp55xx_device_config *cfg = chip->cfg;
513
514 if (cfg->dev_attr_group)
515 sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
516
517 sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
518}
519EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
520
521MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
522MODULE_DESCRIPTION("LP55xx Common Driver");
523MODULE_LICENSE("GPL");
diff --git a/drivers/leds/leds-lp55xx-common.h b/drivers/leds/leds-lp55xx-common.h
new file mode 100644
index 000000000000..ece4761a1302
--- /dev/null
+++ b/drivers/leds/leds-lp55xx-common.h
@@ -0,0 +1,134 @@
1/*
2 * LP55XX Common Driver Header
3 *
4 * Copyright (C) 2012 Texas Instruments
5 *
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * Derived from leds-lp5521.c, leds-lp5523.c
13 */
14
15#ifndef _LEDS_LP55XX_COMMON_H
16#define _LEDS_LP55XX_COMMON_H
17
18enum lp55xx_engine_index {
19 LP55XX_ENGINE_INVALID,
20 LP55XX_ENGINE_1,
21 LP55XX_ENGINE_2,
22 LP55XX_ENGINE_3,
23};
24
25struct lp55xx_led;
26struct lp55xx_chip;
27
28/*
29 * struct lp55xx_reg
30 * @addr : Register address
31 * @val : Register value
32 */
33struct lp55xx_reg {
34 u8 addr;
35 u8 val;
36};
37
38/*
39 * struct lp55xx_device_config
40 * @reset : Chip specific reset command
41 * @enable : Chip specific enable command
42 * @max_channel : Maximum number of channels
43 * @post_init_device : Chip specific initialization code
44 * @brightness_work_fn : Brightness work function
45 * @set_led_current : LED current set function
46 * @firmware_cb : Call function when the firmware is loaded
47 * @run_engine : Run internal engine for pattern
48 * @dev_attr_group : Device specific attributes
49 */
50struct lp55xx_device_config {
51 const struct lp55xx_reg reset;
52 const struct lp55xx_reg enable;
53 const int max_channel;
54
55 /* define if the device has specific initialization process */
56 int (*post_init_device) (struct lp55xx_chip *chip);
57
58 /* access brightness register */
59 void (*brightness_work_fn)(struct work_struct *work);
60
61 /* current setting function */
62 void (*set_led_current) (struct lp55xx_led *led, u8 led_current);
63
64 /* access program memory when the firmware is loaded */
65 void (*firmware_cb)(struct lp55xx_chip *chip);
66
67 /* used for running firmware LED patterns */
68 void (*run_engine) (struct lp55xx_chip *chip, bool start);
69
70 /* additional device specific attributes */
71 const struct attribute_group *dev_attr_group;
72};
73
74/*
75 * struct lp55xx_chip
76 * @cl : I2C communication for access registers
77 * @pdata : Platform specific data
78 * @lock : Lock for user-space interface
79 * @num_leds : Number of registered LEDs
80 * @cfg : Device specific configuration data
81 * @engine_idx : Selected engine number
82 * @fw : Firmware data for running a LED pattern
83 */
84struct lp55xx_chip {
85 struct i2c_client *cl;
86 struct lp55xx_platform_data *pdata;
87 struct mutex lock; /* lock for user-space interface */
88 int num_leds;
89 struct lp55xx_device_config *cfg;
90 enum lp55xx_engine_index engine_idx;
91 const struct firmware *fw;
92};
93
94/*
95 * struct lp55xx_led
96 * @chan_nr : Channel number
97 * @cdev : LED class device
98 * @led_current : Current setting at each led channel
99 * @max_current : Maximun current at each led channel
100 * @brightness_work : Workqueue for brightness control
101 * @brightness : Brightness value
102 * @chip : The lp55xx chip data
103 */
104struct lp55xx_led {
105 int chan_nr;
106 struct led_classdev cdev;
107 u8 led_current;
108 u8 max_current;
109 struct work_struct brightness_work;
110 u8 brightness;
111 struct lp55xx_chip *chip;
112};
113
114/* register access */
115extern int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val);
116extern int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val);
117extern int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg,
118 u8 mask, u8 val);
119
120/* common device init/deinit functions */
121extern int lp55xx_init_device(struct lp55xx_chip *chip);
122extern void lp55xx_deinit_device(struct lp55xx_chip *chip);
123
124/* common LED class device functions */
125extern int lp55xx_register_leds(struct lp55xx_led *led,
126 struct lp55xx_chip *chip);
127extern void lp55xx_unregister_leds(struct lp55xx_led *led,
128 struct lp55xx_chip *chip);
129
130/* common device attributes functions */
131extern int lp55xx_register_sysfs(struct lp55xx_chip *chip);
132extern void lp55xx_unregister_sysfs(struct lp55xx_chip *chip);
133
134#endif /* _LEDS_LP55XX_COMMON_H */
diff --git a/drivers/leds/leds-lp8788.c b/drivers/leds/leds-lp8788.c
index 4353942c5fd1..7c2cb384e7ae 100644
--- a/drivers/leds/leds-lp8788.c
+++ b/drivers/leds/leds-lp8788.c
@@ -130,9 +130,10 @@ static int lp8788_led_probe(struct platform_device *pdev)
130 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent); 130 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
131 struct lp8788_led_platform_data *led_pdata; 131 struct lp8788_led_platform_data *led_pdata;
132 struct lp8788_led *led; 132 struct lp8788_led *led;
133 struct device *dev = &pdev->dev;
133 int ret; 134 int ret;
134 135
135 led = devm_kzalloc(lp->dev, sizeof(struct lp8788_led), GFP_KERNEL); 136 led = devm_kzalloc(dev, sizeof(struct lp8788_led), GFP_KERNEL);
136 if (!led) 137 if (!led)
137 return -ENOMEM; 138 return -ENOMEM;
138 139
@@ -154,13 +155,13 @@ static int lp8788_led_probe(struct platform_device *pdev)
154 155
155 ret = lp8788_led_init_device(led, led_pdata); 156 ret = lp8788_led_init_device(led, led_pdata);
156 if (ret) { 157 if (ret) {
157 dev_err(lp->dev, "led init device err: %d\n", ret); 158 dev_err(dev, "led init device err: %d\n", ret);
158 return ret; 159 return ret;
159 } 160 }
160 161
161 ret = led_classdev_register(lp->dev, &led->led_dev); 162 ret = led_classdev_register(dev, &led->led_dev);
162 if (ret) { 163 if (ret) {
163 dev_err(lp->dev, "led register err: %d\n", ret); 164 dev_err(dev, "led register err: %d\n", ret);
164 return ret; 165 return ret;
165 } 166 }
166 167
diff --git a/drivers/leds/leds-pca9532.c b/drivers/leds/leds-pca9532.c
index cee8a5b483ac..0c597bdd23f9 100644
--- a/drivers/leds/leds-pca9532.c
+++ b/drivers/leds/leds-pca9532.c
@@ -186,7 +186,7 @@ static int pca9532_set_blink(struct led_classdev *led_cdev,
186 int err = 0; 186 int err = 0;
187 187
188 if (*delay_on == 0 && *delay_off == 0) { 188 if (*delay_on == 0 && *delay_off == 0) {
189 /* led subsystem ask us for a blink rate */ 189 /* led subsystem ask us for a blink rate */
190 *delay_on = 1000; 190 *delay_on = 1000;
191 *delay_off = 1000; 191 *delay_off = 1000;
192 } 192 }
@@ -311,7 +311,6 @@ static int pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
311 break; 311 break;
312 case PCA9532_TYPE_N2100_BEEP: 312 case PCA9532_TYPE_N2100_BEEP:
313 if (data->idev != NULL) { 313 if (data->idev != NULL) {
314 input_unregister_device(data->idev);
315 cancel_work_sync(&data->work); 314 cancel_work_sync(&data->work);
316 data->idev = NULL; 315 data->idev = NULL;
317 } 316 }
@@ -382,7 +381,7 @@ static int pca9532_configure(struct i2c_client *client,
382 BUG_ON(data->idev); 381 BUG_ON(data->idev);
383 led->state = PCA9532_PWM1; 382 led->state = PCA9532_PWM1;
384 pca9532_setled(led); 383 pca9532_setled(led);
385 data->idev = input_allocate_device(); 384 data->idev = devm_input_allocate_device(&client->dev);
386 if (data->idev == NULL) { 385 if (data->idev == NULL) {
387 err = -ENOMEM; 386 err = -ENOMEM;
388 goto exit; 387 goto exit;
@@ -401,7 +400,6 @@ static int pca9532_configure(struct i2c_client *client,
401 INIT_WORK(&data->work, pca9532_input_work); 400 INIT_WORK(&data->work, pca9532_input_work);
402 err = input_register_device(data->idev); 401 err = input_register_device(data->idev);
403 if (err) { 402 if (err) {
404 input_free_device(data->idev);
405 cancel_work_sync(&data->work); 403 cancel_work_sync(&data->work);
406 data->idev = NULL; 404 data->idev = NULL;
407 goto exit; 405 goto exit;
diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
index 2157524f277c..a1ea5f6a8d39 100644
--- a/drivers/leds/leds-pwm.c
+++ b/drivers/leds/leds-pwm.c
@@ -16,6 +16,7 @@
16#include <linux/kernel.h> 16#include <linux/kernel.h>
17#include <linux/init.h> 17#include <linux/init.h>
18#include <linux/platform_device.h> 18#include <linux/platform_device.h>
19#include <linux/of_platform.h>
19#include <linux/fb.h> 20#include <linux/fb.h>
20#include <linux/leds.h> 21#include <linux/leds.h>
21#include <linux/err.h> 22#include <linux/err.h>
@@ -30,6 +31,11 @@ struct led_pwm_data {
30 unsigned int period; 31 unsigned int period;
31}; 32};
32 33
34struct led_pwm_priv {
35 int num_leds;
36 struct led_pwm_data leds[0];
37};
38
33static void led_pwm_set(struct led_classdev *led_cdev, 39static void led_pwm_set(struct led_classdev *led_cdev,
34 enum led_brightness brightness) 40 enum led_brightness brightness)
35{ 41{
@@ -47,88 +53,152 @@ static void led_pwm_set(struct led_classdev *led_cdev,
47 } 53 }
48} 54}
49 55
50static int led_pwm_probe(struct platform_device *pdev) 56static inline size_t sizeof_pwm_leds_priv(int num_leds)
51{ 57{
52 struct led_pwm_platform_data *pdata = pdev->dev.platform_data; 58 return sizeof(struct led_pwm_priv) +
53 struct led_pwm *cur_led; 59 (sizeof(struct led_pwm_data) * num_leds);
54 struct led_pwm_data *leds_data, *led_dat; 60}
55 int i, ret = 0; 61
62static struct led_pwm_priv *led_pwm_create_of(struct platform_device *pdev)
63{
64 struct device_node *node = pdev->dev.of_node;
65 struct device_node *child;
66 struct led_pwm_priv *priv;
67 int count, ret;
56 68
57 if (!pdata) 69 /* count LEDs in this device, so we know how much to allocate */
58 return -EBUSY; 70 count = of_get_child_count(node);
71 if (!count)
72 return NULL;
59 73
60 leds_data = devm_kzalloc(&pdev->dev, 74 priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
61 sizeof(struct led_pwm_data) * pdata->num_leds, 75 GFP_KERNEL);
62 GFP_KERNEL); 76 if (!priv)
63 if (!leds_data) 77 return NULL;
64 return -ENOMEM;
65 78
66 for (i = 0; i < pdata->num_leds; i++) { 79 for_each_child_of_node(node, child) {
67 cur_led = &pdata->leds[i]; 80 struct led_pwm_data *led_dat = &priv->leds[priv->num_leds];
68 led_dat = &leds_data[i];
69 81
70 led_dat->pwm = pwm_request(cur_led->pwm_id, 82 led_dat->cdev.name = of_get_property(child, "label",
71 cur_led->name); 83 NULL) ? : child->name;
84
85 led_dat->pwm = devm_of_pwm_get(&pdev->dev, child, NULL);
72 if (IS_ERR(led_dat->pwm)) { 86 if (IS_ERR(led_dat->pwm)) {
73 ret = PTR_ERR(led_dat->pwm); 87 dev_err(&pdev->dev, "unable to request PWM for %s\n",
74 dev_err(&pdev->dev, "unable to request PWM %d\n", 88 led_dat->cdev.name);
75 cur_led->pwm_id);
76 goto err; 89 goto err;
77 } 90 }
91 /* Get the period from PWM core when n*/
92 led_dat->period = pwm_get_period(led_dat->pwm);
93
94 led_dat->cdev.default_trigger = of_get_property(child,
95 "linux,default-trigger", NULL);
96 of_property_read_u32(child, "max-brightness",
97 &led_dat->cdev.max_brightness);
78 98
79 led_dat->cdev.name = cur_led->name;
80 led_dat->cdev.default_trigger = cur_led->default_trigger;
81 led_dat->active_low = cur_led->active_low;
82 led_dat->period = cur_led->pwm_period_ns;
83 led_dat->cdev.brightness_set = led_pwm_set; 99 led_dat->cdev.brightness_set = led_pwm_set;
84 led_dat->cdev.brightness = LED_OFF; 100 led_dat->cdev.brightness = LED_OFF;
85 led_dat->cdev.max_brightness = cur_led->max_brightness;
86 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; 101 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
87 102
88 ret = led_classdev_register(&pdev->dev, &led_dat->cdev); 103 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
89 if (ret < 0) { 104 if (ret < 0) {
90 pwm_free(led_dat->pwm); 105 dev_err(&pdev->dev, "failed to register for %s\n",
106 led_dat->cdev.name);
107 of_node_put(child);
91 goto err; 108 goto err;
92 } 109 }
110 priv->num_leds++;
93 } 111 }
94 112
95 platform_set_drvdata(pdev, leds_data); 113 return priv;
114err:
115 while (priv->num_leds--)
116 led_classdev_unregister(&priv->leds[priv->num_leds].cdev);
96 117
97 return 0; 118 return NULL;
119}
98 120
99err: 121static int led_pwm_probe(struct platform_device *pdev)
100 if (i > 0) { 122{
101 for (i = i - 1; i >= 0; i--) { 123 struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
102 led_classdev_unregister(&leds_data[i].cdev); 124 struct led_pwm_priv *priv;
103 pwm_free(leds_data[i].pwm); 125 int i, ret = 0;
126
127 if (pdata && pdata->num_leds) {
128 priv = devm_kzalloc(&pdev->dev,
129 sizeof_pwm_leds_priv(pdata->num_leds),
130 GFP_KERNEL);
131 if (!priv)
132 return -ENOMEM;
133
134 for (i = 0; i < pdata->num_leds; i++) {
135 struct led_pwm *cur_led = &pdata->leds[i];
136 struct led_pwm_data *led_dat = &priv->leds[i];
137
138 led_dat->pwm = devm_pwm_get(&pdev->dev, cur_led->name);
139 if (IS_ERR(led_dat->pwm)) {
140 ret = PTR_ERR(led_dat->pwm);
141 dev_err(&pdev->dev,
142 "unable to request PWM for %s\n",
143 cur_led->name);
144 goto err;
145 }
146
147 led_dat->cdev.name = cur_led->name;
148 led_dat->cdev.default_trigger = cur_led->default_trigger;
149 led_dat->active_low = cur_led->active_low;
150 led_dat->period = cur_led->pwm_period_ns;
151 led_dat->cdev.brightness_set = led_pwm_set;
152 led_dat->cdev.brightness = LED_OFF;
153 led_dat->cdev.max_brightness = cur_led->max_brightness;
154 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
155
156 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
157 if (ret < 0)
158 goto err;
104 } 159 }
160 priv->num_leds = pdata->num_leds;
161 } else {
162 priv = led_pwm_create_of(pdev);
163 if (!priv)
164 return -ENODEV;
105 } 165 }
106 166
167 platform_set_drvdata(pdev, priv);
168
169 return 0;
170
171err:
172 while (i--)
173 led_classdev_unregister(&priv->leds[i].cdev);
174
107 return ret; 175 return ret;
108} 176}
109 177
110static int led_pwm_remove(struct platform_device *pdev) 178static int led_pwm_remove(struct platform_device *pdev)
111{ 179{
180 struct led_pwm_priv *priv = platform_get_drvdata(pdev);
112 int i; 181 int i;
113 struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
114 struct led_pwm_data *leds_data;
115 182
116 leds_data = platform_get_drvdata(pdev); 183 for (i = 0; i < priv->num_leds; i++)
117 184 led_classdev_unregister(&priv->leds[i].cdev);
118 for (i = 0; i < pdata->num_leds; i++) {
119 led_classdev_unregister(&leds_data[i].cdev);
120 pwm_free(leds_data[i].pwm);
121 }
122 185
123 return 0; 186 return 0;
124} 187}
125 188
189static const struct of_device_id of_pwm_leds_match[] = {
190 { .compatible = "pwm-leds", },
191 {},
192};
193MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
194
126static struct platform_driver led_pwm_driver = { 195static struct platform_driver led_pwm_driver = {
127 .probe = led_pwm_probe, 196 .probe = led_pwm_probe,
128 .remove = led_pwm_remove, 197 .remove = led_pwm_remove,
129 .driver = { 198 .driver = {
130 .name = "leds_pwm", 199 .name = "leds_pwm",
131 .owner = THIS_MODULE, 200 .owner = THIS_MODULE,
201 .of_match_table = of_match_ptr(of_pwm_leds_match),
132 }, 202 },
133}; 203};
134 204
diff --git a/drivers/leds/leds-renesas-tpu.c b/drivers/leds/leds-renesas-tpu.c
index e0fff1ca5923..d3c2b7e68fbc 100644
--- a/drivers/leds/leds-renesas-tpu.c
+++ b/drivers/leds/leds-renesas-tpu.c
@@ -133,24 +133,24 @@ static int r_tpu_enable(struct r_tpu_priv *p, enum led_brightness brightness)
133 rate = clk_get_rate(p->clk); 133 rate = clk_get_rate(p->clk);
134 134
135 /* pick the lowest acceptable rate */ 135 /* pick the lowest acceptable rate */
136 for (k = 0; k < ARRAY_SIZE(prescaler); k++) 136 for (k = ARRAY_SIZE(prescaler) - 1; k >= 0; k--)
137 if ((rate / prescaler[k]) < p->min_rate) 137 if ((rate / prescaler[k]) >= p->min_rate)
138 break; 138 break;
139 139
140 if (!k) { 140 if (k < 0) {
141 dev_err(&p->pdev->dev, "clock rate mismatch\n"); 141 dev_err(&p->pdev->dev, "clock rate mismatch\n");
142 goto err0; 142 goto err0;
143 } 143 }
144 dev_dbg(&p->pdev->dev, "rate = %lu, prescaler %u\n", 144 dev_dbg(&p->pdev->dev, "rate = %lu, prescaler %u\n",
145 rate, prescaler[k - 1]); 145 rate, prescaler[k]);
146 146
147 /* clear TCNT on TGRB match, count on rising edge, set prescaler */ 147 /* clear TCNT on TGRB match, count on rising edge, set prescaler */
148 r_tpu_write(p, TCR, 0x0040 | (k - 1)); 148 r_tpu_write(p, TCR, 0x0040 | k);
149 149
150 /* output 0 until TGRA, output 1 until TGRB */ 150 /* output 0 until TGRA, output 1 until TGRB */
151 r_tpu_write(p, TIOR, 0x0002); 151 r_tpu_write(p, TIOR, 0x0002);
152 152
153 rate /= prescaler[k - 1] * p->refresh_rate; 153 rate /= prescaler[k] * p->refresh_rate;
154 r_tpu_write(p, TGRB, rate); 154 r_tpu_write(p, TGRB, rate);
155 dev_dbg(&p->pdev->dev, "TRGB = 0x%04lx\n", rate); 155 dev_dbg(&p->pdev->dev, "TRGB = 0x%04lx\n", rate);
156 156
diff --git a/drivers/leds/leds-ss4200.c b/drivers/leds/leds-ss4200.c
index ec9b287ecfbf..64e204e714f6 100644
--- a/drivers/leds/leds-ss4200.c
+++ b/drivers/leds/leds-ss4200.c
@@ -63,8 +63,7 @@ MODULE_LICENSE("GPL");
63/* 63/*
64 * PCI ID of the Intel ICH7 LPC Device within which the GPIO block lives. 64 * PCI ID of the Intel ICH7 LPC Device within which the GPIO block lives.
65 */ 65 */
66static const struct pci_device_id ich7_lpc_pci_id[] = 66static DEFINE_PCI_DEVICE_TABLE(ich7_lpc_pci_id) = {
67{
68 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0) }, 67 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0) },
69 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_1) }, 68 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_1) },
70 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_30) }, 69 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_30) },
diff --git a/drivers/leds/leds-sunfire.c b/drivers/leds/leds-sunfire.c
index 07ff5a3a6cee..89792990088d 100644
--- a/drivers/leds/leds-sunfire.c
+++ b/drivers/leds/leds-sunfire.c
@@ -3,6 +3,8 @@
3 * Copyright (C) 2008 David S. Miller <davem@davemloft.net> 3 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
4 */ 4 */
5 5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
6#include <linux/kernel.h> 8#include <linux/kernel.h>
7#include <linux/module.h> 9#include <linux/module.h>
8#include <linux/init.h> 10#include <linux/init.h>
@@ -14,9 +16,6 @@
14#include <asm/fhc.h> 16#include <asm/fhc.h>
15#include <asm/upa.h> 17#include <asm/upa.h>
16 18
17#define DRIVER_NAME "leds-sunfire"
18#define PFX DRIVER_NAME ": "
19
20MODULE_AUTHOR("David S. Miller (davem@davemloft.net)"); 19MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
21MODULE_DESCRIPTION("Sun Fire LED driver"); 20MODULE_DESCRIPTION("Sun Fire LED driver");
22MODULE_LICENSE("GPL"); 21MODULE_LICENSE("GPL");
@@ -130,14 +129,14 @@ static int sunfire_led_generic_probe(struct platform_device *pdev,
130 int i, err; 129 int i, err;
131 130
132 if (pdev->num_resources != 1) { 131 if (pdev->num_resources != 1) {
133 printk(KERN_ERR PFX "Wrong number of resources %d, should be 1\n", 132 dev_err(&pdev->dev, "Wrong number of resources %d, should be 1\n",
134 pdev->num_resources); 133 pdev->num_resources);
135 return -EINVAL; 134 return -EINVAL;
136 } 135 }
137 136
138 p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL); 137 p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
139 if (!p) { 138 if (!p) {
140 printk(KERN_ERR PFX "Could not allocate struct sunfire_drvdata\n"); 139 dev_err(&pdev->dev, "Could not allocate struct sunfire_drvdata\n");
141 return -ENOMEM; 140 return -ENOMEM;
142 } 141 }
143 142
@@ -152,7 +151,7 @@ static int sunfire_led_generic_probe(struct platform_device *pdev,
152 151
153 err = led_classdev_register(&pdev->dev, lp); 152 err = led_classdev_register(&pdev->dev, lp);
154 if (err) { 153 if (err) {
155 printk(KERN_ERR PFX "Could not register %s LED\n", 154 dev_err(&pdev->dev, "Could not register %s LED\n",
156 lp->name); 155 lp->name);
157 for (i--; i >= 0; i--) 156 for (i--; i >= 0; i--)
158 led_classdev_unregister(&p->leds[i].led_cdev); 157 led_classdev_unregister(&p->leds[i].led_cdev);
@@ -188,7 +187,7 @@ static struct led_type clockboard_led_types[NUM_LEDS_PER_BOARD] = {
188 { 187 {
189 .name = "clockboard-right", 188 .name = "clockboard-right",
190 .handler = clockboard_right_set, 189 .handler = clockboard_right_set,
191 .default_trigger= "heartbeat", 190 .default_trigger = "heartbeat",
192 }, 191 },
193}; 192};
194 193
@@ -209,7 +208,7 @@ static struct led_type fhc_led_types[NUM_LEDS_PER_BOARD] = {
209 { 208 {
210 .name = "fhc-right", 209 .name = "fhc-right",
211 .handler = fhc_right_set, 210 .handler = fhc_right_set,
212 .default_trigger= "heartbeat", 211 .default_trigger = "heartbeat",
213 }, 212 },
214}; 213};
215 214
@@ -244,13 +243,13 @@ static int __init sunfire_leds_init(void)
244 int err = platform_driver_register(&sunfire_clockboard_led_driver); 243 int err = platform_driver_register(&sunfire_clockboard_led_driver);
245 244
246 if (err) { 245 if (err) {
247 printk(KERN_ERR PFX "Could not register clock board LED driver\n"); 246 pr_err("Could not register clock board LED driver\n");
248 return err; 247 return err;
249 } 248 }
250 249
251 err = platform_driver_register(&sunfire_fhc_led_driver); 250 err = platform_driver_register(&sunfire_fhc_led_driver);
252 if (err) { 251 if (err) {
253 printk(KERN_ERR PFX "Could not register FHC LED driver\n"); 252 pr_err("Could not register FHC LED driver\n");
254 platform_driver_unregister(&sunfire_clockboard_led_driver); 253 platform_driver_unregister(&sunfire_clockboard_led_driver);
255 } 254 }
256 255
diff --git a/drivers/leds/leds-tca6507.c b/drivers/leds/leds-tca6507.c
index b26a63bae16b..070ba0741b21 100644
--- a/drivers/leds/leds-tca6507.c
+++ b/drivers/leds/leds-tca6507.c
@@ -667,8 +667,68 @@ static void tca6507_remove_gpio(struct tca6507_chip *tca)
667} 667}
668#endif /* CONFIG_GPIOLIB */ 668#endif /* CONFIG_GPIOLIB */
669 669
670#ifdef CONFIG_OF
671static struct tca6507_platform_data *
672tca6507_led_dt_init(struct i2c_client *client)
673{
674 struct device_node *np = client->dev.of_node, *child;
675 struct tca6507_platform_data *pdata;
676 struct led_info *tca_leds;
677 int count;
678
679 count = of_get_child_count(np);
680 if (!count || count > NUM_LEDS)
681 return ERR_PTR(-ENODEV);
682
683 tca_leds = devm_kzalloc(&client->dev,
684 sizeof(struct led_info) * count, GFP_KERNEL);
685 if (!tca_leds)
686 return ERR_PTR(-ENOMEM);
687
688 for_each_child_of_node(np, child) {
689 struct led_info led;
690 u32 reg;
691 int ret;
692
693 led.name =
694 of_get_property(child, "label", NULL) ? : child->name;
695 led.default_trigger =
696 of_get_property(child, "linux,default-trigger", NULL);
697
698 ret = of_property_read_u32(child, "reg", &reg);
699 if (ret != 0)
700 continue;
701
702 tca_leds[reg] = led;
703 }
704 pdata = devm_kzalloc(&client->dev,
705 sizeof(struct tca6507_platform_data), GFP_KERNEL);
706 if (!pdata)
707 return ERR_PTR(-ENOMEM);
708
709 pdata->leds.leds = tca_leds;
710 pdata->leds.num_leds = count;
711
712 return pdata;
713}
714
715static const struct of_device_id of_tca6507_leds_match[] = {
716 { .compatible = "ti,tca6507", },
717 {},
718};
719
720#else
721static struct tca6507_platform_data *
722tca6507_led_dt_init(struct i2c_client *client)
723{
724 return ERR_PTR(-ENODEV);
725}
726
727#define of_tca6507_leds_match NULL
728#endif
729
670static int tca6507_probe(struct i2c_client *client, 730static int tca6507_probe(struct i2c_client *client,
671 const struct i2c_device_id *id) 731 const struct i2c_device_id *id)
672{ 732{
673 struct tca6507_chip *tca; 733 struct tca6507_chip *tca;
674 struct i2c_adapter *adapter; 734 struct i2c_adapter *adapter;
@@ -683,9 +743,12 @@ static int tca6507_probe(struct i2c_client *client,
683 return -EIO; 743 return -EIO;
684 744
685 if (!pdata || pdata->leds.num_leds != NUM_LEDS) { 745 if (!pdata || pdata->leds.num_leds != NUM_LEDS) {
686 dev_err(&client->dev, "Need %d entries in platform-data list\n", 746 pdata = tca6507_led_dt_init(client);
687 NUM_LEDS); 747 if (IS_ERR(pdata)) {
688 return -ENODEV; 748 dev_err(&client->dev, "Need %d entries in platform-data list\n",
749 NUM_LEDS);
750 return PTR_ERR(pdata);
751 }
689 } 752 }
690 tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL); 753 tca = devm_kzalloc(&client->dev, sizeof(*tca), GFP_KERNEL);
691 if (!tca) 754 if (!tca)
@@ -750,6 +813,7 @@ static struct i2c_driver tca6507_driver = {
750 .driver = { 813 .driver = {
751 .name = "leds-tca6507", 814 .name = "leds-tca6507",
752 .owner = THIS_MODULE, 815 .owner = THIS_MODULE,
816 .of_match_table = of_tca6507_leds_match,
753 }, 817 },
754 .probe = tca6507_probe, 818 .probe = tca6507_probe,
755 .remove = tca6507_remove, 819 .remove = tca6507_remove,
diff --git a/drivers/leds/leds-wm831x-status.c b/drivers/leds/leds-wm831x-status.c
index 74a24cf897c3..6bd5c679d877 100644
--- a/drivers/leds/leds-wm831x-status.c
+++ b/drivers/leds/leds-wm831x-status.c
@@ -157,7 +157,7 @@ static int wm831x_status_blink_set(struct led_classdev *led_cdev,
157 return ret; 157 return ret;
158} 158}
159 159
160static const char *led_src_texts[] = { 160static const char * const led_src_texts[] = {
161 "otp", 161 "otp",
162 "power", 162 "power",
163 "charger", 163 "charger",