blob: f49312968aa7472c7667b0b89925eaace1e7de4c [file] [log] [blame]
Mohan Pallaka53fe1a12011-12-15 16:56:37 +05301/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Jay Chokshi12e49bf2011-07-22 16:24:39 -070013#define pr_fmt(fmt) "%s: " fmt, __func__
14
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/kernel.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070016#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070017#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/platform_device.h>
20#include <linux/leds.h>
21#include <linux/workqueue.h>
Jay Chokshi868312e2011-09-16 13:57:13 -070022#include <linux/err.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023
24#include <linux/mfd/pm8xxx/core.h>
Jay Chokshi868312e2011-09-16 13:57:13 -070025#include <linux/mfd/pm8xxx/pwm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026#include <linux/leds-pm8xxx.h>
27
28#define SSBI_REG_ADDR_DRV_KEYPAD 0x48
29#define PM8XXX_DRV_KEYPAD_BL_MASK 0xf0
30#define PM8XXX_DRV_KEYPAD_BL_SHIFT 0x04
31
32#define SSBI_REG_ADDR_FLASH_DRV0 0x49
33#define PM8XXX_DRV_FLASH_MASK 0xf0
34#define PM8XXX_DRV_FLASH_SHIFT 0x04
35
36#define SSBI_REG_ADDR_FLASH_DRV1 0xFB
37
38#define SSBI_REG_ADDR_LED_CTRL_BASE 0x131
39#define SSBI_REG_ADDR_LED_CTRL(n) (SSBI_REG_ADDR_LED_CTRL_BASE + (n))
40#define PM8XXX_DRV_LED_CTRL_MASK 0xf8
41#define PM8XXX_DRV_LED_CTRL_SHIFT 0x03
42
Mohan Pallaka53fe1a12011-12-15 16:56:37 +053043#define SSBI_REG_ADDR_WLED_CTRL_BASE 0x25A
44#define SSBI_REG_ADDR_WLED_CTRL(n) (SSBI_REG_ADDR_WLED_CTRL_BASE + (n) - 1)
45
46/* wled control registers */
47#define WLED_MOD_CTRL_REG SSBI_REG_ADDR_WLED_CTRL(1)
48#define WLED_MAX_CURR_CFG_REG(n) SSBI_REG_ADDR_WLED_CTRL(n + 2)
Mohan Pallaka58ff4052012-11-19 16:58:59 +053049#define WLED_BRIGHTNESS_CNTL_REG1(n) SSBI_REG_ADDR_WLED_CTRL((2 * n) + 5)
50#define WLED_BRIGHTNESS_CNTL_REG2(n) SSBI_REG_ADDR_WLED_CTRL((2 * n) + 6)
Mohan Pallaka53fe1a12011-12-15 16:56:37 +053051#define WLED_SYNC_REG SSBI_REG_ADDR_WLED_CTRL(11)
52#define WLED_OVP_CFG_REG SSBI_REG_ADDR_WLED_CTRL(13)
53#define WLED_BOOST_CFG_REG SSBI_REG_ADDR_WLED_CTRL(14)
54#define WLED_HIGH_POLE_CAP_REG SSBI_REG_ADDR_WLED_CTRL(16)
55
56#define WLED_STRINGS 0x03
57#define WLED_OVP_VAL_MASK 0x30
58#define WLED_OVP_VAL_BIT_SHFT 0x04
59#define WLED_BOOST_LIMIT_MASK 0xE0
60#define WLED_BOOST_LIMIT_BIT_SHFT 0x05
Amy Maloche56913f52012-05-11 10:47:24 -070061#define WLED_BOOST_OFF 0x00
Mohan Pallaka53fe1a12011-12-15 16:56:37 +053062#define WLED_EN_MASK 0x01
63#define WLED_CP_SELECT_MAX 0x03
64#define WLED_CP_SELECT_MASK 0x03
65#define WLED_DIG_MOD_GEN_MASK 0x70
66#define WLED_CS_OUT_MASK 0x0E
67#define WLED_CTL_DLY_STEP 200
68#define WLED_CTL_DLY_MAX 1400
69#define WLED_CTL_DLY_MASK 0xE0
70#define WLED_CTL_DLY_BIT_SHFT 0x05
71#define WLED_MAX_CURR 25
72#define WLED_MAX_CURR_MASK 0x1F
Amy Malochebe73e192012-11-29 12:53:59 -080073#define WLED_BRIGHTNESS_MSB_MASK 0x0F
Mohan Pallaka53fe1a12011-12-15 16:56:37 +053074#define WLED_OP_FDBCK_MASK 0x1C
75#define WLED_OP_FDBCK_BIT_SHFT 0x02
76
Chandan Uddaraju6e73f0a2012-03-08 17:32:55 -080077#define WLED_MAX_LEVEL 255
Mohan Pallaka53fe1a12011-12-15 16:56:37 +053078#define WLED_8_BIT_MASK 0xFF
79#define WLED_8_BIT_SHFT 0x08
80#define WLED_MAX_DUTY_CYCLE 0xFFF
81
82#define WLED_SYNC_VAL 0x07
83#define WLED_SYNC_RESET_VAL 0x00
Amy Malochec366d5b2012-08-28 11:06:40 -070084#define WLED_SYNC_MASK 0xF8
85
86#define ONE_WLED_STRING 1
87#define TWO_WLED_STRINGS 2
88#define THREE_WLED_STRINGS 3
89
90#define WLED_CABC_ONE_STRING 0x01
91#define WLED_CABC_TWO_STRING 0x03
92#define WLED_CABC_THREE_STRING 0x07
93
94#define WLED_CABC_SHIFT 3
Mohan Pallaka53fe1a12011-12-15 16:56:37 +053095
Amy Malochec17c3732012-02-27 18:34:07 -080096#define SSBI_REG_ADDR_RGB_CNTL1 0x12D
97#define SSBI_REG_ADDR_RGB_CNTL2 0x12E
98
99#define PM8XXX_DRV_RGB_RED_LED BIT(2)
100#define PM8XXX_DRV_RGB_GREEN_LED BIT(1)
101#define PM8XXX_DRV_RGB_BLUE_LED BIT(0)
102
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700103#define MAX_FLASH_LED_CURRENT 300
104#define MAX_LC_LED_CURRENT 40
105#define MAX_KP_BL_LED_CURRENT 300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700107#define PM8XXX_ID_LED_CURRENT_FACTOR 2 /* Iout = x * 2mA */
108#define PM8XXX_ID_FLASH_CURRENT_FACTOR 20 /* Iout = x * 20mA */
109
110#define PM8XXX_FLASH_MODE_DBUS1 1
111#define PM8XXX_FLASH_MODE_DBUS2 2
112#define PM8XXX_FLASH_MODE_PWM 3
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114#define MAX_LC_LED_BRIGHTNESS 20
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700115#define MAX_FLASH_BRIGHTNESS 15
116#define MAX_KB_LED_BRIGHTNESS 15
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117
Jay Chokshide4cefb2011-08-04 18:10:44 -0700118#define PM8XXX_LED_OFFSET(id) ((id) - PM8XXX_ID_LED_0)
119
Jay Chokshi868312e2011-09-16 13:57:13 -0700120#define PM8XXX_LED_PWM_FLAGS (PM_PWM_LUT_LOOP | PM_PWM_LUT_RAMP_UP)
121
Amy Maloche3d2c24c2012-03-22 19:02:25 -0700122#define LED_MAP(_version, _kb, _led0, _led1, _led2, _flash_led0, _flash_led1, \
Amy Malochec17c3732012-02-27 18:34:07 -0800123 _wled, _rgb_led_red, _rgb_led_green, _rgb_led_blue)\
Amy Maloche3d2c24c2012-03-22 19:02:25 -0700124 {\
125 .version = _version,\
126 .supported = _kb << PM8XXX_ID_LED_KB_LIGHT | \
127 _led0 << PM8XXX_ID_LED_0 | _led1 << PM8XXX_ID_LED_1 | \
128 _led2 << PM8XXX_ID_LED_2 | \
129 _flash_led0 << PM8XXX_ID_FLASH_LED_0 | \
130 _flash_led1 << PM8XXX_ID_FLASH_LED_1 | \
Amy Malochec17c3732012-02-27 18:34:07 -0800131 _wled << PM8XXX_ID_WLED | \
132 _rgb_led_red << PM8XXX_ID_RGB_LED_RED | \
133 _rgb_led_green << PM8XXX_ID_RGB_LED_GREEN | \
134 _rgb_led_blue << PM8XXX_ID_RGB_LED_BLUE, \
Amy Maloche3d2c24c2012-03-22 19:02:25 -0700135 }
136
137/**
138 * supported_leds - leds supported for each PMIC version
139 * @version - version of PMIC
140 * @supported - which leds are supported on version
141 */
142
143struct supported_leds {
144 enum pm8xxx_version version;
145 u32 supported;
146};
147
148static const struct supported_leds led_map[] = {
Amy Malochec17c3732012-02-27 18:34:07 -0800149 LED_MAP(PM8XXX_VERSION_8058, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0),
150 LED_MAP(PM8XXX_VERSION_8921, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0),
151 LED_MAP(PM8XXX_VERSION_8018, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0),
152 LED_MAP(PM8XXX_VERSION_8922, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1),
153 LED_MAP(PM8XXX_VERSION_8038, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1),
Amy Maloche3d2c24c2012-03-22 19:02:25 -0700154};
155
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156/**
157 * struct pm8xxx_led_data - internal led data structure
158 * @led_classdev - led class device
159 * @id - led index
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160 * @work - workqueue for led
161 * @lock - to protect the transactions
162 * @reg - cached value of led register
Jay Chokshi868312e2011-09-16 13:57:13 -0700163 * @pwm_dev - pointer to PWM device if LED is driven using PWM
164 * @pwm_channel - PWM channel ID
165 * @pwm_period_us - PWM period in micro seconds
166 * @pwm_duty_cycles - struct that describes PWM duty cycles info
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 */
168struct pm8xxx_led_data {
169 struct led_classdev cdev;
170 int id;
171 u8 reg;
Amy Maloche56913f52012-05-11 10:47:24 -0700172 u8 wled_mod_ctrl_val;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173 struct device *dev;
174 struct work_struct work;
175 struct mutex lock;
Jay Chokshi868312e2011-09-16 13:57:13 -0700176 struct pwm_device *pwm_dev;
177 int pwm_channel;
178 u32 pwm_period_us;
179 struct pm8xxx_pwm_duty_cycles *pwm_duty_cycles;
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530180 struct wled_config_data *wled_cfg;
181 int max_current;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182};
183
184static void led_kp_set(struct pm8xxx_led_data *led, enum led_brightness value)
185{
186 int rc;
187 u8 level;
188
189 level = (value << PM8XXX_DRV_KEYPAD_BL_SHIFT) &
190 PM8XXX_DRV_KEYPAD_BL_MASK;
191
192 led->reg &= ~PM8XXX_DRV_KEYPAD_BL_MASK;
193 led->reg |= level;
194
195 rc = pm8xxx_writeb(led->dev->parent, SSBI_REG_ADDR_DRV_KEYPAD,
196 led->reg);
197 if (rc < 0)
198 dev_err(led->cdev.dev,
199 "can't set keypad backlight level rc=%d\n", rc);
200}
201
202static void led_lc_set(struct pm8xxx_led_data *led, enum led_brightness value)
203{
204 int rc, offset;
205 u8 level;
206
207 level = (value << PM8XXX_DRV_LED_CTRL_SHIFT) &
208 PM8XXX_DRV_LED_CTRL_MASK;
209
210 offset = PM8XXX_LED_OFFSET(led->id);
211
212 led->reg &= ~PM8XXX_DRV_LED_CTRL_MASK;
213 led->reg |= level;
214
215 rc = pm8xxx_writeb(led->dev->parent, SSBI_REG_ADDR_LED_CTRL(offset),
216 led->reg);
217 if (rc)
218 dev_err(led->cdev.dev, "can't set (%d) led value rc=%d\n",
219 led->id, rc);
220}
221
222static void
223led_flash_set(struct pm8xxx_led_data *led, enum led_brightness value)
224{
225 int rc;
226 u8 level;
227 u16 reg_addr;
228
229 level = (value << PM8XXX_DRV_FLASH_SHIFT) &
230 PM8XXX_DRV_FLASH_MASK;
231
232 led->reg &= ~PM8XXX_DRV_FLASH_MASK;
233 led->reg |= level;
234
235 if (led->id == PM8XXX_ID_FLASH_LED_0)
236 reg_addr = SSBI_REG_ADDR_FLASH_DRV0;
237 else
238 reg_addr = SSBI_REG_ADDR_FLASH_DRV1;
239
240 rc = pm8xxx_writeb(led->dev->parent, reg_addr, led->reg);
241 if (rc < 0)
242 dev_err(led->cdev.dev, "can't set flash led%d level rc=%d\n",
243 led->id, rc);
244}
245
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530246static int
247led_wled_set(struct pm8xxx_led_data *led, enum led_brightness value)
248{
249 int rc, duty;
250 u8 val, i, num_wled_strings;
251
252 if (value > WLED_MAX_LEVEL)
253 value = WLED_MAX_LEVEL;
254
Amy Maloche56913f52012-05-11 10:47:24 -0700255 if (value == 0) {
256 rc = pm8xxx_writeb(led->dev->parent, WLED_MOD_CTRL_REG,
257 WLED_BOOST_OFF);
258 if (rc) {
259 dev_err(led->dev->parent, "can't write wled ctrl config"
260 " register rc=%d\n", rc);
261 return rc;
262 }
263 } else {
264 rc = pm8xxx_writeb(led->dev->parent, WLED_MOD_CTRL_REG,
265 led->wled_mod_ctrl_val);
266 if (rc) {
267 dev_err(led->dev->parent, "can't write wled ctrl config"
268 " register rc=%d\n", rc);
269 return rc;
270 }
271 }
272
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530273 duty = (WLED_MAX_DUTY_CYCLE * value) / WLED_MAX_LEVEL;
274
275 num_wled_strings = led->wled_cfg->num_strings;
276
277 /* program brightness control registers */
278 for (i = 0; i < num_wled_strings; i++) {
279 rc = pm8xxx_readb(led->dev->parent,
280 WLED_BRIGHTNESS_CNTL_REG1(i), &val);
281 if (rc) {
282 dev_err(led->dev->parent, "can't read wled brightnes ctrl"
283 " register1 rc=%d\n", rc);
284 return rc;
285 }
286
Amy Malochebe73e192012-11-29 12:53:59 -0800287 val = (val & ~WLED_BRIGHTNESS_MSB_MASK) |
288 (duty >> WLED_8_BIT_SHFT);
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530289 rc = pm8xxx_writeb(led->dev->parent,
290 WLED_BRIGHTNESS_CNTL_REG1(i), val);
291 if (rc) {
292 dev_err(led->dev->parent, "can't write wled brightness ctrl"
293 " register1 rc=%d\n", rc);
294 return rc;
295 }
296
297 val = duty & WLED_8_BIT_MASK;
298 rc = pm8xxx_writeb(led->dev->parent,
299 WLED_BRIGHTNESS_CNTL_REG2(i), val);
300 if (rc) {
301 dev_err(led->dev->parent, "can't write wled brightness ctrl"
302 " register2 rc=%d\n", rc);
303 return rc;
304 }
305 }
Amy Malochec366d5b2012-08-28 11:06:40 -0700306 rc = pm8xxx_readb(led->dev->parent, WLED_SYNC_REG, &val);
307 if (rc) {
308 dev_err(led->dev->parent,
309 "can't read wled sync register rc=%d\n", rc);
310 return rc;
311 }
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530312 /* sync */
Amy Malochec366d5b2012-08-28 11:06:40 -0700313 val &= WLED_SYNC_MASK;
314 val |= WLED_SYNC_VAL;
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530315 rc = pm8xxx_writeb(led->dev->parent, WLED_SYNC_REG, val);
316 if (rc) {
317 dev_err(led->dev->parent,
318 "can't read wled sync register rc=%d\n", rc);
319 return rc;
320 }
Amy Malochec366d5b2012-08-28 11:06:40 -0700321 val &= WLED_SYNC_MASK;
322 val |= WLED_SYNC_RESET_VAL;
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530323 rc = pm8xxx_writeb(led->dev->parent, WLED_SYNC_REG, val);
324 if (rc) {
325 dev_err(led->dev->parent,
326 "can't read wled sync register rc=%d\n", rc);
327 return rc;
328 }
329 return 0;
330}
331
332static void wled_dump_regs(struct pm8xxx_led_data *led)
333{
334 int i;
335 u8 val;
336
337 for (i = 1; i < 17; i++) {
338 pm8xxx_readb(led->dev->parent,
339 SSBI_REG_ADDR_WLED_CTRL(i), &val);
340 pr_debug("WLED_CTRL_%d = 0x%x\n", i, val);
341 }
342}
343
Amy Malochec17c3732012-02-27 18:34:07 -0800344static void
Amy Malochead3c7842012-07-20 14:51:27 -0700345led_rgb_write(struct pm8xxx_led_data *led, u16 addr, enum led_brightness value)
Amy Malochec17c3732012-02-27 18:34:07 -0800346{
347 int rc;
348 u8 val, mask;
349
Amy Malochead3c7842012-07-20 14:51:27 -0700350 if (led->id != PM8XXX_ID_RGB_LED_BLUE &&
351 led->id != PM8XXX_ID_RGB_LED_RED &&
352 led->id != PM8XXX_ID_RGB_LED_GREEN)
353 return;
354
355 rc = pm8xxx_readb(led->dev->parent, addr, &val);
Amy Malochec17c3732012-02-27 18:34:07 -0800356 if (rc) {
357 dev_err(led->cdev.dev, "can't read rgb ctrl register rc=%d\n",
358 rc);
359 return;
360 }
361
362 switch (led->id) {
363 case PM8XXX_ID_RGB_LED_RED:
364 mask = PM8XXX_DRV_RGB_RED_LED;
365 break;
366 case PM8XXX_ID_RGB_LED_GREEN:
367 mask = PM8XXX_DRV_RGB_GREEN_LED;
368 break;
369 case PM8XXX_ID_RGB_LED_BLUE:
370 mask = PM8XXX_DRV_RGB_BLUE_LED;
371 break;
372 default:
373 return;
374 }
375
376 if (value)
377 val |= mask;
378 else
379 val &= ~mask;
380
Amy Malochead3c7842012-07-20 14:51:27 -0700381 rc = pm8xxx_writeb(led->dev->parent, addr, val);
Amy Malochec17c3732012-02-27 18:34:07 -0800382 if (rc < 0)
383 dev_err(led->cdev.dev, "can't set rgb led %d level rc=%d\n",
384 led->id, rc);
385}
386
Amy Malochead3c7842012-07-20 14:51:27 -0700387static void
388led_rgb_set(struct pm8xxx_led_data *led, enum led_brightness value)
389{
390 if (value) {
391 led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1, value);
392 led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL2, value);
393 } else {
394 led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL2, value);
395 led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1, value);
396 }
397}
398
Jay Chokshi868312e2011-09-16 13:57:13 -0700399static int pm8xxx_led_pwm_work(struct pm8xxx_led_data *led)
400{
401 int duty_us;
402 int rc = 0;
403
404 if (led->pwm_duty_cycles == NULL) {
405 duty_us = (led->pwm_period_us * led->cdev.brightness) /
406 LED_FULL;
407 rc = pwm_config(led->pwm_dev, duty_us, led->pwm_period_us);
Amy Malochead3c7842012-07-20 14:51:27 -0700408 if (led->cdev.brightness) {
409 led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1,
410 led->cdev.brightness);
Jay Chokshi868312e2011-09-16 13:57:13 -0700411 rc = pwm_enable(led->pwm_dev);
Amy Malochead3c7842012-07-20 14:51:27 -0700412 } else {
Jay Chokshi868312e2011-09-16 13:57:13 -0700413 pwm_disable(led->pwm_dev);
Amy Malochead3c7842012-07-20 14:51:27 -0700414 led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1,
415 led->cdev.brightness);
416 }
Jay Chokshi868312e2011-09-16 13:57:13 -0700417 } else {
Amy Malochead3c7842012-07-20 14:51:27 -0700418 if (led->cdev.brightness)
419 led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1,
420 led->cdev.brightness);
Jay Chokshi868312e2011-09-16 13:57:13 -0700421 rc = pm8xxx_pwm_lut_enable(led->pwm_dev, led->cdev.brightness);
Amy Malochead3c7842012-07-20 14:51:27 -0700422 if (!led->cdev.brightness)
423 led_rgb_write(led, SSBI_REG_ADDR_RGB_CNTL1,
424 led->cdev.brightness);
Jay Chokshi868312e2011-09-16 13:57:13 -0700425 }
426
427 return rc;
428}
429
Jay Chokshide4cefb2011-08-04 18:10:44 -0700430static void __pm8xxx_led_work(struct pm8xxx_led_data *led,
431 enum led_brightness level)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432{
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530433 int rc;
434
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 mutex_lock(&led->lock);
436
437 switch (led->id) {
438 case PM8XXX_ID_LED_KB_LIGHT:
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700439 led_kp_set(led, level);
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530440 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441 case PM8XXX_ID_LED_0:
442 case PM8XXX_ID_LED_1:
443 case PM8XXX_ID_LED_2:
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700444 led_lc_set(led, level);
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530445 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446 case PM8XXX_ID_FLASH_LED_0:
447 case PM8XXX_ID_FLASH_LED_1:
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700448 led_flash_set(led, level);
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530449 break;
450 case PM8XXX_ID_WLED:
451 rc = led_wled_set(led, level);
452 if (rc < 0)
453 pr_err("wled brightness set failed %d\n", rc);
454 break;
Amy Malochec17c3732012-02-27 18:34:07 -0800455 case PM8XXX_ID_RGB_LED_RED:
456 case PM8XXX_ID_RGB_LED_GREEN:
457 case PM8XXX_ID_RGB_LED_BLUE:
458 led_rgb_set(led, level);
459 break;
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530460 default:
461 dev_err(led->cdev.dev, "unknown led id %d", led->id);
462 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463 }
464
465 mutex_unlock(&led->lock);
466}
467
Jay Chokshide4cefb2011-08-04 18:10:44 -0700468static void pm8xxx_led_work(struct work_struct *work)
469{
Jay Chokshi868312e2011-09-16 13:57:13 -0700470 int rc;
471
Jay Chokshide4cefb2011-08-04 18:10:44 -0700472 struct pm8xxx_led_data *led = container_of(work,
473 struct pm8xxx_led_data, work);
Jay Chokshide4cefb2011-08-04 18:10:44 -0700474
Jay Chokshi868312e2011-09-16 13:57:13 -0700475 if (led->pwm_dev == NULL) {
476 __pm8xxx_led_work(led, led->cdev.brightness);
477 } else {
478 rc = pm8xxx_led_pwm_work(led);
479 if (rc)
480 pr_err("could not configure PWM mode for LED:%d\n",
481 led->id);
482 }
Jay Chokshide4cefb2011-08-04 18:10:44 -0700483}
484
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485static void pm8xxx_led_set(struct led_classdev *led_cdev,
486 enum led_brightness value)
487{
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700488 struct pm8xxx_led_data *led;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489
490 led = container_of(led_cdev, struct pm8xxx_led_data, cdev);
491
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700492 if (value < LED_OFF || value > led->cdev.max_brightness) {
493 dev_err(led->cdev.dev, "Invalid brightness value exceeds");
494 return;
495 }
496
497 led->cdev.brightness = value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 schedule_work(&led->work);
499}
500
Jay Chokshide4cefb2011-08-04 18:10:44 -0700501static int pm8xxx_set_led_mode_and_max_brightness(struct pm8xxx_led_data *led,
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700502 enum pm8xxx_led_modes led_mode, int max_current)
503{
Jay Chokshide4cefb2011-08-04 18:10:44 -0700504 switch (led->id) {
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700505 case PM8XXX_ID_LED_0:
506 case PM8XXX_ID_LED_1:
507 case PM8XXX_ID_LED_2:
508 led->cdev.max_brightness = max_current /
509 PM8XXX_ID_LED_CURRENT_FACTOR;
Jay Chokshide4cefb2011-08-04 18:10:44 -0700510 if (led->cdev.max_brightness > MAX_LC_LED_BRIGHTNESS)
511 led->cdev.max_brightness = MAX_LC_LED_BRIGHTNESS;
Willie Ruana39d1d42011-08-19 13:54:34 -0700512 led->reg = led_mode;
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700513 break;
514 case PM8XXX_ID_LED_KB_LIGHT:
515 case PM8XXX_ID_FLASH_LED_0:
516 case PM8XXX_ID_FLASH_LED_1:
517 led->cdev.max_brightness = max_current /
518 PM8XXX_ID_FLASH_CURRENT_FACTOR;
Jay Chokshide4cefb2011-08-04 18:10:44 -0700519 if (led->cdev.max_brightness > MAX_FLASH_BRIGHTNESS)
520 led->cdev.max_brightness = MAX_FLASH_BRIGHTNESS;
521
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700522 switch (led_mode) {
523 case PM8XXX_LED_MODE_PWM1:
524 case PM8XXX_LED_MODE_PWM2:
525 case PM8XXX_LED_MODE_PWM3:
Willie Ruana39d1d42011-08-19 13:54:34 -0700526 led->reg = PM8XXX_FLASH_MODE_PWM;
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700527 break;
528 case PM8XXX_LED_MODE_DTEST1:
Willie Ruana39d1d42011-08-19 13:54:34 -0700529 led->reg = PM8XXX_FLASH_MODE_DBUS1;
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700530 break;
531 case PM8XXX_LED_MODE_DTEST2:
Willie Ruana39d1d42011-08-19 13:54:34 -0700532 led->reg = PM8XXX_FLASH_MODE_DBUS2;
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700533 break;
534 default:
Willie Ruana39d1d42011-08-19 13:54:34 -0700535 led->reg = PM8XXX_LED_MODE_MANUAL;
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700536 break;
537 }
538 break;
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530539 case PM8XXX_ID_WLED:
540 led->cdev.max_brightness = WLED_MAX_LEVEL;
541 break;
Amy Malochec17c3732012-02-27 18:34:07 -0800542 case PM8XXX_ID_RGB_LED_RED:
543 case PM8XXX_ID_RGB_LED_GREEN:
544 case PM8XXX_ID_RGB_LED_BLUE:
545 led->cdev.max_brightness = LED_FULL;
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700546 break;
Amy Malochec17c3732012-02-27 18:34:07 -0800547 default:
548 dev_err(led->cdev.dev, "LED Id is invalid");
549 return -EINVAL;
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700550 }
551
Amy Malochec17c3732012-02-27 18:34:07 -0800552 return 0;
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700553}
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700554
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555static enum led_brightness pm8xxx_led_get(struct led_classdev *led_cdev)
556{
557 struct pm8xxx_led_data *led;
558
559 led = container_of(led_cdev, struct pm8xxx_led_data, cdev);
560
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700561 return led->cdev.brightness;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562}
563
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530564static int __devinit init_wled(struct pm8xxx_led_data *led)
565{
566 int rc, i;
567 u8 val, num_wled_strings;
568
569 num_wled_strings = led->wled_cfg->num_strings;
570
571 /* program over voltage protection threshold */
572 if (led->wled_cfg->ovp_val > WLED_OVP_37V) {
573 dev_err(led->dev->parent, "Invalid ovp value");
574 return -EINVAL;
575 }
576
577 rc = pm8xxx_readb(led->dev->parent, WLED_OVP_CFG_REG, &val);
578 if (rc) {
579 dev_err(led->dev->parent, "can't read wled ovp config"
580 " register rc=%d\n", rc);
581 return rc;
582 }
583
584 val = (val & ~WLED_OVP_VAL_MASK) |
585 (led->wled_cfg->ovp_val << WLED_OVP_VAL_BIT_SHFT);
586
587 rc = pm8xxx_writeb(led->dev->parent, WLED_OVP_CFG_REG, val);
588 if (rc) {
589 dev_err(led->dev->parent, "can't write wled ovp config"
590 " register rc=%d\n", rc);
591 return rc;
592 }
593
594 /* program current boost limit and output feedback*/
595 if (led->wled_cfg->boost_curr_lim > WLED_CURR_LIMIT_1680mA) {
596 dev_err(led->dev->parent, "Invalid boost current limit");
597 return -EINVAL;
598 }
599
600 rc = pm8xxx_readb(led->dev->parent, WLED_BOOST_CFG_REG, &val);
601 if (rc) {
602 dev_err(led->dev->parent, "can't read wled boost config"
603 " register rc=%d\n", rc);
604 return rc;
605 }
606
607 val = (val & ~WLED_BOOST_LIMIT_MASK) |
608 (led->wled_cfg->boost_curr_lim << WLED_BOOST_LIMIT_BIT_SHFT);
609
610 val = (val & ~WLED_OP_FDBCK_MASK) |
611 (led->wled_cfg->op_fdbck << WLED_OP_FDBCK_BIT_SHFT);
612
613 rc = pm8xxx_writeb(led->dev->parent, WLED_BOOST_CFG_REG, val);
614 if (rc) {
615 dev_err(led->dev->parent, "can't write wled boost config"
616 " register rc=%d\n", rc);
617 return rc;
618 }
619
620 /* program high pole capacitance */
621 if (led->wled_cfg->cp_select > WLED_CP_SELECT_MAX) {
622 dev_err(led->dev->parent, "Invalid pole capacitance");
623 return -EINVAL;
624 }
625
626 rc = pm8xxx_readb(led->dev->parent, WLED_HIGH_POLE_CAP_REG, &val);
627 if (rc) {
628 dev_err(led->dev->parent, "can't read wled high pole"
629 " capacitance register rc=%d\n", rc);
630 return rc;
631 }
632
633 val = (val & ~WLED_CP_SELECT_MASK) | led->wled_cfg->cp_select;
634
635 rc = pm8xxx_writeb(led->dev->parent, WLED_HIGH_POLE_CAP_REG, val);
636 if (rc) {
637 dev_err(led->dev->parent, "can't write wled high pole"
638 " capacitance register rc=%d\n", rc);
639 return rc;
640 }
641
642 /* program activation delay and maximum current */
643 for (i = 0; i < num_wled_strings; i++) {
644 rc = pm8xxx_readb(led->dev->parent,
Mohan Pallaka58ff4052012-11-19 16:58:59 +0530645 WLED_MAX_CURR_CFG_REG(i), &val);
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530646 if (rc) {
647 dev_err(led->dev->parent, "can't read wled max current"
648 " config register rc=%d\n", rc);
649 return rc;
650 }
651
652 if ((led->wled_cfg->ctrl_delay_us % WLED_CTL_DLY_STEP) ||
653 (led->wled_cfg->ctrl_delay_us > WLED_CTL_DLY_MAX)) {
654 dev_err(led->dev->parent, "Invalid control delay\n");
655 return rc;
656 }
657
658 val = val / WLED_CTL_DLY_STEP;
659 val = (val & ~WLED_CTL_DLY_MASK) |
660 (led->wled_cfg->ctrl_delay_us << WLED_CTL_DLY_BIT_SHFT);
661
662 if ((led->max_current > WLED_MAX_CURR)) {
663 dev_err(led->dev->parent, "Invalid max current\n");
664 return -EINVAL;
665 }
666
667 val = (val & ~WLED_MAX_CURR_MASK) | led->max_current;
668
669 rc = pm8xxx_writeb(led->dev->parent,
Mohan Pallaka58ff4052012-11-19 16:58:59 +0530670 WLED_MAX_CURR_CFG_REG(i), val);
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530671 if (rc) {
672 dev_err(led->dev->parent, "can't write wled max current"
673 " config register rc=%d\n", rc);
674 return rc;
675 }
676 }
677
Amy Malochec366d5b2012-08-28 11:06:40 -0700678 if (led->wled_cfg->cabc_en) {
679 rc = pm8xxx_readb(led->dev->parent, WLED_SYNC_REG, &val);
680 if (rc) {
681 dev_err(led->dev->parent,
682 "can't read cabc register rc=%d\n", rc);
683 return rc;
684 }
685
686 switch (num_wled_strings) {
687 case ONE_WLED_STRING:
688 val |= (WLED_CABC_ONE_STRING << WLED_CABC_SHIFT);
689 break;
690 case TWO_WLED_STRINGS:
691 val |= (WLED_CABC_TWO_STRING << WLED_CABC_SHIFT);
692 break;
693 case THREE_WLED_STRINGS:
694 val |= (WLED_CABC_THREE_STRING << WLED_CABC_SHIFT);
695 break;
696 default:
697 break;
698 }
699
700 rc = pm8xxx_writeb(led->dev->parent, WLED_SYNC_REG, val);
701 if (rc) {
702 dev_err(led->dev->parent,
703 "can't write to enable cabc rc=%d\n", rc);
704 return rc;
705 }
706 }
707
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530708 /* program digital module generator, cs out and enable the module */
709 rc = pm8xxx_readb(led->dev->parent, WLED_MOD_CTRL_REG, &val);
710 if (rc) {
711 dev_err(led->dev->parent, "can't read wled module ctrl"
712 " register rc=%d\n", rc);
713 return rc;
714 }
715
716 if (led->wled_cfg->dig_mod_gen_en)
717 val |= WLED_DIG_MOD_GEN_MASK;
718
719 if (led->wled_cfg->cs_out_en)
720 val |= WLED_CS_OUT_MASK;
721
722 val |= WLED_EN_MASK;
723
724 rc = pm8xxx_writeb(led->dev->parent, WLED_MOD_CTRL_REG, val);
725 if (rc) {
726 dev_err(led->dev->parent, "can't write wled module ctrl"
727 " register rc=%d\n", rc);
728 return rc;
729 }
Amy Maloche56913f52012-05-11 10:47:24 -0700730 led->wled_mod_ctrl_val = val;
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530731
732 /* dump wled registers */
733 wled_dump_regs(led);
734
735 return 0;
736}
737
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738static int __devinit get_init_value(struct pm8xxx_led_data *led, u8 *val)
739{
740 int rc, offset;
741 u16 addr;
742
743 switch (led->id) {
744 case PM8XXX_ID_LED_KB_LIGHT:
745 addr = SSBI_REG_ADDR_DRV_KEYPAD;
746 break;
747 case PM8XXX_ID_LED_0:
748 case PM8XXX_ID_LED_1:
749 case PM8XXX_ID_LED_2:
750 offset = PM8XXX_LED_OFFSET(led->id);
751 addr = SSBI_REG_ADDR_LED_CTRL(offset);
752 break;
753 case PM8XXX_ID_FLASH_LED_0:
754 addr = SSBI_REG_ADDR_FLASH_DRV0;
755 break;
756 case PM8XXX_ID_FLASH_LED_1:
757 addr = SSBI_REG_ADDR_FLASH_DRV1;
758 break;
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530759 case PM8XXX_ID_WLED:
760 rc = init_wled(led);
761 if (rc)
762 dev_err(led->cdev.dev, "can't initialize wled rc=%d\n",
763 rc);
764 return rc;
Amy Malochec17c3732012-02-27 18:34:07 -0800765 case PM8XXX_ID_RGB_LED_RED:
766 case PM8XXX_ID_RGB_LED_GREEN:
767 case PM8XXX_ID_RGB_LED_BLUE:
Amy Malochec17c3732012-02-27 18:34:07 -0800768 addr = SSBI_REG_ADDR_RGB_CNTL1;
769 break;
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530770 default:
771 dev_err(led->cdev.dev, "unknown led id %d", led->id);
772 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 }
774
775 rc = pm8xxx_readb(led->dev->parent, addr, val);
776 if (rc)
777 dev_err(led->cdev.dev, "can't get led(%d) level rc=%d\n",
778 led->id, rc);
779
780 return rc;
781}
782
Jay Chokshi868312e2011-09-16 13:57:13 -0700783static int pm8xxx_led_pwm_configure(struct pm8xxx_led_data *led)
784{
785 int start_idx, idx_len, duty_us, rc;
786
787 led->pwm_dev = pwm_request(led->pwm_channel,
788 led->cdev.name);
789
790 if (IS_ERR_OR_NULL(led->pwm_dev)) {
791 pr_err("could not acquire PWM Channel %d, "
792 "error %ld\n", led->pwm_channel,
793 PTR_ERR(led->pwm_dev));
794 led->pwm_dev = NULL;
795 return -ENODEV;
796 }
797
798 if (led->pwm_duty_cycles != NULL) {
799 start_idx = led->pwm_duty_cycles->start_idx;
800 idx_len = led->pwm_duty_cycles->num_duty_pcts;
801
802 if (idx_len >= PM_PWM_LUT_SIZE && start_idx) {
803 pr_err("Wrong LUT size or index\n");
804 return -EINVAL;
805 }
806 if ((start_idx + idx_len) > PM_PWM_LUT_SIZE) {
807 pr_err("Exceed LUT limit\n");
808 return -EINVAL;
809 }
810
811 rc = pm8xxx_pwm_lut_config(led->pwm_dev, led->pwm_period_us,
812 led->pwm_duty_cycles->duty_pcts,
813 led->pwm_duty_cycles->duty_ms,
814 start_idx, idx_len, 0, 0,
815 PM8XXX_LED_PWM_FLAGS);
816 } else {
817 duty_us = led->pwm_period_us;
818 rc = pwm_config(led->pwm_dev, duty_us, led->pwm_period_us);
819 }
820
821 return rc;
822}
823
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530824
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825static int __devinit pm8xxx_led_probe(struct platform_device *pdev)
826{
Jay Chokshi8994e392011-09-14 18:20:39 -0700827 const struct pm8xxx_led_platform_data *pdata = pdev->dev.platform_data;
828 const struct led_platform_data *pcore_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 struct led_info *curr_led;
830 struct pm8xxx_led_data *led, *led_dat;
Jay Chokshi8994e392011-09-14 18:20:39 -0700831 struct pm8xxx_led_config *led_cfg;
Amy Maloche3d2c24c2012-03-22 19:02:25 -0700832 enum pm8xxx_version version;
833 bool found = false;
834 int rc, i, j;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835
836 if (pdata == NULL) {
837 dev_err(&pdev->dev, "platform data not supplied\n");
838 return -EINVAL;
839 }
840
Jay Chokshi8994e392011-09-14 18:20:39 -0700841 pcore_data = pdata->led_core;
842
843 if (pcore_data->num_leds != pdata->num_configs) {
844 dev_err(&pdev->dev, "#no. of led configs and #no. of led"
845 "entries are not equal\n");
846 return -EINVAL;
847 }
848
849 led = kcalloc(pcore_data->num_leds, sizeof(*led), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 if (led == NULL) {
851 dev_err(&pdev->dev, "failed to alloc memory\n");
852 return -ENOMEM;
853 }
854
Jay Chokshi8994e392011-09-14 18:20:39 -0700855 for (i = 0; i < pcore_data->num_leds; i++) {
856 curr_led = &pcore_data->leds[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 led_dat = &led[i];
Jay Chokshi8994e392011-09-14 18:20:39 -0700858 led_cfg = &pdata->configs[i];
859
860 led_dat->id = led_cfg->id;
Jay Chokshi868312e2011-09-16 13:57:13 -0700861 led_dat->pwm_channel = led_cfg->pwm_channel;
862 led_dat->pwm_period_us = led_cfg->pwm_period_us;
863 led_dat->pwm_duty_cycles = led_cfg->pwm_duty_cycles;
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530864 led_dat->wled_cfg = led_cfg->wled_cfg;
865 led_dat->max_current = led_cfg->max_current;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866
867 if (!((led_dat->id >= PM8XXX_ID_LED_KB_LIGHT) &&
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530868 (led_dat->id < PM8XXX_ID_MAX))) {
Amy Maloche3d2c24c2012-03-22 19:02:25 -0700869 dev_err(&pdev->dev, "invalid LED ID(%d) specified\n",
870 led_dat->id);
871 rc = -EINVAL;
872 goto fail_id_check;
873
874 }
875
876 found = false;
877 version = pm8xxx_get_version(pdev->dev.parent);
878 for (j = 0; j < ARRAY_SIZE(led_map); j++) {
879 if (version == led_map[j].version
880 && (led_map[j].supported & (1 << led_dat->id))) {
881 found = true;
882 break;
883 }
884 }
885
886 if (!found) {
887 dev_err(&pdev->dev, "invalid LED ID(%d) specified\n",
888 led_dat->id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 rc = -EINVAL;
890 goto fail_id_check;
891 }
892
893 led_dat->cdev.name = curr_led->name;
894 led_dat->cdev.default_trigger = curr_led->default_trigger;
895 led_dat->cdev.brightness_set = pm8xxx_led_set;
896 led_dat->cdev.brightness_get = pm8xxx_led_get;
897 led_dat->cdev.brightness = LED_OFF;
Jay Chokshi8994e392011-09-14 18:20:39 -0700898 led_dat->cdev.flags = curr_led->flags;
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700899 led_dat->dev = &pdev->dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900
901 rc = get_init_value(led_dat, &led_dat->reg);
902 if (rc < 0)
903 goto fail_id_check;
904
Jay Chokshi8994e392011-09-14 18:20:39 -0700905 rc = pm8xxx_set_led_mode_and_max_brightness(led_dat,
906 led_cfg->mode, led_cfg->max_current);
Jay Chokshide4cefb2011-08-04 18:10:44 -0700907 if (rc < 0)
908 goto fail_id_check;
909
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910 mutex_init(&led_dat->lock);
911 INIT_WORK(&led_dat->work, pm8xxx_led_work);
912
913 rc = led_classdev_register(&pdev->dev, &led_dat->cdev);
914 if (rc) {
915 dev_err(&pdev->dev, "unable to register led %d,rc=%d\n",
916 led_dat->id, rc);
917 goto fail_id_check;
918 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530920 /* configure default state */
921 if (led_cfg->default_state)
922 led->cdev.brightness = led_dat->cdev.max_brightness;
923 else
924 led->cdev.brightness = LED_OFF;
925
Jay Chokshi868312e2011-09-16 13:57:13 -0700926 if (led_cfg->mode != PM8XXX_LED_MODE_MANUAL) {
Amy Malochec17c3732012-02-27 18:34:07 -0800927 if (led_dat->id == PM8XXX_ID_RGB_LED_RED ||
928 led_dat->id == PM8XXX_ID_RGB_LED_GREEN ||
929 led_dat->id == PM8XXX_ID_RGB_LED_BLUE)
930 __pm8xxx_led_work(led_dat, 0);
931 else
932 __pm8xxx_led_work(led_dat,
Jay Chokshide4cefb2011-08-04 18:10:44 -0700933 led_dat->cdev.max_brightness);
Jay Chokshi868312e2011-09-16 13:57:13 -0700934
935 if (led_dat->pwm_channel != -1) {
936 led_dat->cdev.max_brightness = LED_FULL;
937 rc = pm8xxx_led_pwm_configure(led_dat);
938 if (rc) {
939 dev_err(&pdev->dev, "failed to "
940 "configure LED, error: %d\n", rc);
941 goto fail_id_check;
942 }
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530943 schedule_work(&led->work);
Jay Chokshi868312e2011-09-16 13:57:13 -0700944 }
945 } else {
Mohan Pallaka53fe1a12011-12-15 16:56:37 +0530946 __pm8xxx_led_work(led_dat, led->cdev.brightness);
Jay Chokshi868312e2011-09-16 13:57:13 -0700947 }
Jay Chokshide4cefb2011-08-04 18:10:44 -0700948 }
Jay Chokshi12e49bf2011-07-22 16:24:39 -0700949
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950 platform_set_drvdata(pdev, led);
951
952 return 0;
953
954fail_id_check:
955 if (i > 0) {
956 for (i = i - 1; i >= 0; i--) {
957 mutex_destroy(&led[i].lock);
958 led_classdev_unregister(&led[i].cdev);
Jay Chokshi868312e2011-09-16 13:57:13 -0700959 if (led[i].pwm_dev != NULL)
960 pwm_free(led[i].pwm_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 }
962 }
963 kfree(led);
964 return rc;
965}
966
967static int __devexit pm8xxx_led_remove(struct platform_device *pdev)
968{
969 int i;
970 const struct led_platform_data *pdata =
971 pdev->dev.platform_data;
972 struct pm8xxx_led_data *led = platform_get_drvdata(pdev);
973
974 for (i = 0; i < pdata->num_leds; i++) {
975 cancel_work_sync(&led[i].work);
976 mutex_destroy(&led[i].lock);
977 led_classdev_unregister(&led[i].cdev);
Jay Chokshi868312e2011-09-16 13:57:13 -0700978 if (led[i].pwm_dev != NULL)
979 pwm_free(led[i].pwm_dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 }
981
982 kfree(led);
983
984 return 0;
985}
986
987static struct platform_driver pm8xxx_led_driver = {
988 .probe = pm8xxx_led_probe,
989 .remove = __devexit_p(pm8xxx_led_remove),
990 .driver = {
991 .name = PM8XXX_LEDS_DEV_NAME,
992 .owner = THIS_MODULE,
993 },
994};
995
996static int __init pm8xxx_led_init(void)
997{
998 return platform_driver_register(&pm8xxx_led_driver);
999}
Jay Chokshi12e49bf2011-07-22 16:24:39 -07001000subsys_initcall(pm8xxx_led_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001
1002static void __exit pm8xxx_led_exit(void)
1003{
1004 platform_driver_unregister(&pm8xxx_led_driver);
1005}
1006module_exit(pm8xxx_led_exit);
1007
1008MODULE_DESCRIPTION("PM8XXX LEDs driver");
1009MODULE_LICENSE("GPL v2");
1010MODULE_VERSION("1.0");
1011MODULE_ALIAS("platform:pm8xxx-led");