1 /* 2 * leds-lp3944.c - driver for National Semiconductor LP3944 Funlight Chip 3 * 4 * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 * 10 */ 11 12 /* 13 * I2C driver for National Semiconductor LP3944 Funlight Chip 14 * http://www.national.com/pf/LP/LP3944.html 15 * 16 * This helper chip can drive up to 8 leds, with two programmable DIM modes; 17 * it could even be used as a gpio expander but this driver assumes it is used 18 * as a led controller. 19 * 20 * The DIM modes are used to set _blink_ patterns for leds, the pattern is 21 * specified supplying two parameters: 22 * - period: from 0s to 1.6s 23 * - duty cycle: percentage of the period the led is on, from 0 to 100 24 * 25 * LP3944 can be found on Motorola A910 smartphone, where it drives the rgb 26 * leds, the camera flash light and the displays backlights. 27 */ 28 29 #include <linux/module.h> 30 #include <linux/i2c.h> 31 #include <linux/slab.h> 32 #include <linux/leds.h> 33 #include <linux/mutex.h> 34 #include <linux/leds-lp3944.h> 35 36 /* Read Only Registers */ 37 #define LP3944_REG_INPUT1 0x00 /* LEDs 0-7 InputRegister (Read Only) */ 38 #define LP3944_REG_REGISTER1 0x01 /* None (Read Only) */ 39 40 #define LP3944_REG_PSC0 0x02 /* Frequency Prescaler 0 (R/W) */ 41 #define LP3944_REG_PWM0 0x03 /* PWM Register 0 (R/W) */ 42 #define LP3944_REG_PSC1 0x04 /* Frequency Prescaler 1 (R/W) */ 43 #define LP3944_REG_PWM1 0x05 /* PWM Register 1 (R/W) */ 44 #define LP3944_REG_LS0 0x06 /* LEDs 0-3 Selector (R/W) */ 45 #define LP3944_REG_LS1 0x07 /* LEDs 4-7 Selector (R/W) */ 46 47 /* These registers are not used to control leds in LP3944, they can store 48 * arbitrary values which the chip will ignore. 49 */ 50 #define LP3944_REG_REGISTER8 0x08 51 #define LP3944_REG_REGISTER9 0x09 52 53 #define LP3944_DIM0 0 54 #define LP3944_DIM1 1 55 56 /* period in ms */ 57 #define LP3944_PERIOD_MIN 0 58 #define LP3944_PERIOD_MAX 1600 59 60 /* duty cycle is a percentage */ 61 #define LP3944_DUTY_CYCLE_MIN 0 62 #define LP3944_DUTY_CYCLE_MAX 100 63 64 #define ldev_to_led(c) container_of(c, struct lp3944_led_data, ldev) 65 66 /* Saved data */ 67 struct lp3944_led_data { 68 u8 id; 69 enum lp3944_type type; 70 struct led_classdev ldev; 71 struct i2c_client *client; 72 }; 73 74 struct lp3944_data { 75 struct mutex lock; 76 struct i2c_client *client; 77 struct lp3944_led_data leds[LP3944_LEDS_MAX]; 78 }; 79 80 static int lp3944_reg_read(struct i2c_client *client, u8 reg, u8 *value) 81 { 82 int tmp; 83 84 tmp = i2c_smbus_read_byte_data(client, reg); 85 if (tmp < 0) 86 return tmp; 87 88 *value = tmp; 89 90 return 0; 91 } 92 93 static int lp3944_reg_write(struct i2c_client *client, u8 reg, u8 value) 94 { 95 return i2c_smbus_write_byte_data(client, reg, value); 96 } 97 98 /** 99 * Set the period for DIM status 100 * 101 * @client: the i2c client 102 * @dim: either LP3944_DIM0 or LP3944_DIM1 103 * @period: period of a blink, that is a on/off cycle, expressed in ms. 104 */ 105 static int lp3944_dim_set_period(struct i2c_client *client, u8 dim, u16 period) 106 { 107 u8 psc_reg; 108 u8 psc_value; 109 int err; 110 111 if (dim == LP3944_DIM0) 112 psc_reg = LP3944_REG_PSC0; 113 else if (dim == LP3944_DIM1) 114 psc_reg = LP3944_REG_PSC1; 115 else 116 return -EINVAL; 117 118 /* Convert period to Prescaler value */ 119 if (period > LP3944_PERIOD_MAX) 120 return -EINVAL; 121 122 psc_value = (period * 255) / LP3944_PERIOD_MAX; 123 124 err = lp3944_reg_write(client, psc_reg, psc_value); 125 126 return err; 127 } 128 129 /** 130 * Set the duty cycle for DIM status 131 * 132 * @client: the i2c client 133 * @dim: either LP3944_DIM0 or LP3944_DIM1 134 * @duty_cycle: percentage of a period during which a led is ON 135 */ 136 static int lp3944_dim_set_dutycycle(struct i2c_client *client, u8 dim, 137 u8 duty_cycle) 138 { 139 u8 pwm_reg; 140 u8 pwm_value; 141 int err; 142 143 if (dim == LP3944_DIM0) 144 pwm_reg = LP3944_REG_PWM0; 145 else if (dim == LP3944_DIM1) 146 pwm_reg = LP3944_REG_PWM1; 147 else 148 return -EINVAL; 149 150 /* Convert duty cycle to PWM value */ 151 if (duty_cycle > LP3944_DUTY_CYCLE_MAX) 152 return -EINVAL; 153 154 pwm_value = (duty_cycle * 255) / LP3944_DUTY_CYCLE_MAX; 155 156 err = lp3944_reg_write(client, pwm_reg, pwm_value); 157 158 return err; 159 } 160 161 /** 162 * Set the led status 163 * 164 * @led: a lp3944_led_data structure 165 * @status: one of LP3944_LED_STATUS_OFF 166 * LP3944_LED_STATUS_ON 167 * LP3944_LED_STATUS_DIM0 168 * LP3944_LED_STATUS_DIM1 169 */ 170 static int lp3944_led_set(struct lp3944_led_data *led, u8 status) 171 { 172 struct lp3944_data *data = i2c_get_clientdata(led->client); 173 u8 id = led->id; 174 u8 reg; 175 u8 val = 0; 176 int err; 177 178 dev_dbg(&led->client->dev, "%s: %s, status before normalization:%d\n", 179 __func__, led->ldev.name, status); 180 181 switch (id) { 182 case LP3944_LED0: 183 case LP3944_LED1: 184 case LP3944_LED2: 185 case LP3944_LED3: 186 reg = LP3944_REG_LS0; 187 break; 188 case LP3944_LED4: 189 case LP3944_LED5: 190 case LP3944_LED6: 191 case LP3944_LED7: 192 id -= LP3944_LED4; 193 reg = LP3944_REG_LS1; 194 break; 195 default: 196 return -EINVAL; 197 } 198 199 if (status > LP3944_LED_STATUS_DIM1) 200 return -EINVAL; 201 202 /* invert only 0 and 1, leave unchanged the other values, 203 * remember we are abusing status to set blink patterns 204 */ 205 if (led->type == LP3944_LED_TYPE_LED_INVERTED && status < 2) 206 status = 1 - status; 207 208 mutex_lock(&data->lock); 209 lp3944_reg_read(led->client, reg, &val); 210 211 val &= ~(LP3944_LED_STATUS_MASK << (id << 1)); 212 val |= (status << (id << 1)); 213 214 dev_dbg(&led->client->dev, "%s: %s, reg:%d id:%d status:%d val:%#x\n", 215 __func__, led->ldev.name, reg, id, status, val); 216 217 /* set led status */ 218 err = lp3944_reg_write(led->client, reg, val); 219 mutex_unlock(&data->lock); 220 221 return err; 222 } 223 224 static int lp3944_led_set_blink(struct led_classdev *led_cdev, 225 unsigned long *delay_on, 226 unsigned long *delay_off) 227 { 228 struct lp3944_led_data *led = ldev_to_led(led_cdev); 229 u16 period; 230 u8 duty_cycle; 231 int err; 232 233 /* units are in ms */ 234 if (*delay_on + *delay_off > LP3944_PERIOD_MAX) 235 return -EINVAL; 236 237 if (*delay_on == 0 && *delay_off == 0) { 238 /* Special case: the leds subsystem requires a default user 239 * friendly blink pattern for the LED. Let's blink the led 240 * slowly (1Hz). 241 */ 242 *delay_on = 500; 243 *delay_off = 500; 244 } 245 246 period = (*delay_on) + (*delay_off); 247 248 /* duty_cycle is the percentage of period during which the led is ON */ 249 duty_cycle = 100 * (*delay_on) / period; 250 251 /* invert duty cycle for inverted leds, this has the same effect of 252 * swapping delay_on and delay_off 253 */ 254 if (led->type == LP3944_LED_TYPE_LED_INVERTED) 255 duty_cycle = 100 - duty_cycle; 256 257 /* NOTE: using always the first DIM mode, this means that all leds 258 * will have the same blinking pattern. 259 * 260 * We could find a way later to have two leds blinking in hardware 261 * with different patterns at the same time, falling back to software 262 * control for the other ones. 263 */ 264 err = lp3944_dim_set_period(led->client, LP3944_DIM0, period); 265 if (err) 266 return err; 267 268 err = lp3944_dim_set_dutycycle(led->client, LP3944_DIM0, duty_cycle); 269 if (err) 270 return err; 271 272 dev_dbg(&led->client->dev, "%s: OK hardware accelerated blink!\n", 273 __func__); 274 275 lp3944_led_set(led, LP3944_LED_STATUS_DIM0); 276 277 return 0; 278 } 279 280 static int lp3944_led_set_brightness(struct led_classdev *led_cdev, 281 enum led_brightness brightness) 282 { 283 struct lp3944_led_data *led = ldev_to_led(led_cdev); 284 285 dev_dbg(&led->client->dev, "%s: %s, %d\n", 286 __func__, led_cdev->name, brightness); 287 288 return lp3944_led_set(led, !!brightness); 289 } 290 291 static int lp3944_configure(struct i2c_client *client, 292 struct lp3944_data *data, 293 struct lp3944_platform_data *pdata) 294 { 295 int i, err = 0; 296 297 for (i = 0; i < pdata->leds_size; i++) { 298 struct lp3944_led *pled = &pdata->leds[i]; 299 struct lp3944_led_data *led = &data->leds[i]; 300 led->client = client; 301 led->id = i; 302 303 switch (pled->type) { 304 305 case LP3944_LED_TYPE_LED: 306 case LP3944_LED_TYPE_LED_INVERTED: 307 led->type = pled->type; 308 led->ldev.name = pled->name; 309 led->ldev.max_brightness = 1; 310 led->ldev.brightness_set_blocking = 311 lp3944_led_set_brightness; 312 led->ldev.blink_set = lp3944_led_set_blink; 313 led->ldev.flags = LED_CORE_SUSPENDRESUME; 314 315 err = led_classdev_register(&client->dev, &led->ldev); 316 if (err < 0) { 317 dev_err(&client->dev, 318 "couldn't register LED %s\n", 319 led->ldev.name); 320 goto exit; 321 } 322 323 /* to expose the default value to userspace */ 324 led->ldev.brightness = 325 (enum led_brightness) pled->status; 326 327 /* Set the default led status */ 328 err = lp3944_led_set(led, pled->status); 329 if (err < 0) { 330 dev_err(&client->dev, 331 "%s couldn't set STATUS %d\n", 332 led->ldev.name, pled->status); 333 goto exit; 334 } 335 break; 336 337 case LP3944_LED_TYPE_NONE: 338 default: 339 break; 340 341 } 342 } 343 return 0; 344 345 exit: 346 if (i > 0) 347 for (i = i - 1; i >= 0; i--) 348 switch (pdata->leds[i].type) { 349 350 case LP3944_LED_TYPE_LED: 351 case LP3944_LED_TYPE_LED_INVERTED: 352 led_classdev_unregister(&data->leds[i].ldev); 353 break; 354 355 case LP3944_LED_TYPE_NONE: 356 default: 357 break; 358 } 359 360 return err; 361 } 362 363 static int lp3944_probe(struct i2c_client *client, 364 const struct i2c_device_id *id) 365 { 366 struct lp3944_platform_data *lp3944_pdata = 367 dev_get_platdata(&client->dev); 368 struct lp3944_data *data; 369 int err; 370 371 if (lp3944_pdata == NULL) { 372 dev_err(&client->dev, "no platform data\n"); 373 return -EINVAL; 374 } 375 376 /* Let's see whether this adapter can support what we need. */ 377 if (!i2c_check_functionality(client->adapter, 378 I2C_FUNC_SMBUS_BYTE_DATA)) { 379 dev_err(&client->dev, "insufficient functionality!\n"); 380 return -ENODEV; 381 } 382 383 data = devm_kzalloc(&client->dev, sizeof(struct lp3944_data), 384 GFP_KERNEL); 385 if (!data) 386 return -ENOMEM; 387 388 data->client = client; 389 i2c_set_clientdata(client, data); 390 391 mutex_init(&data->lock); 392 393 err = lp3944_configure(client, data, lp3944_pdata); 394 if (err < 0) 395 return err; 396 397 dev_info(&client->dev, "lp3944 enabled\n"); 398 return 0; 399 } 400 401 static int lp3944_remove(struct i2c_client *client) 402 { 403 struct lp3944_platform_data *pdata = dev_get_platdata(&client->dev); 404 struct lp3944_data *data = i2c_get_clientdata(client); 405 int i; 406 407 for (i = 0; i < pdata->leds_size; i++) 408 switch (data->leds[i].type) { 409 case LP3944_LED_TYPE_LED: 410 case LP3944_LED_TYPE_LED_INVERTED: 411 led_classdev_unregister(&data->leds[i].ldev); 412 break; 413 414 case LP3944_LED_TYPE_NONE: 415 default: 416 break; 417 } 418 419 return 0; 420 } 421 422 /* lp3944 i2c driver struct */ 423 static const struct i2c_device_id lp3944_id[] = { 424 {"lp3944", 0}, 425 {} 426 }; 427 428 MODULE_DEVICE_TABLE(i2c, lp3944_id); 429 430 static struct i2c_driver lp3944_driver = { 431 .driver = { 432 .name = "lp3944", 433 }, 434 .probe = lp3944_probe, 435 .remove = lp3944_remove, 436 .id_table = lp3944_id, 437 }; 438 439 module_i2c_driver(lp3944_driver); 440 441 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>"); 442 MODULE_DESCRIPTION("LP3944 Fun Light Chip"); 443 MODULE_LICENSE("GPL"); 444