1 /* 2 * Copyright 2011 bct electronic GmbH 3 * Copyright 2013 Qtechnology/AS 4 * 5 * Author: Peter Meerwald <p.meerwald@bct-electronic.com> 6 * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com> 7 * 8 * Based on leds-pca955x.c 9 * 10 * This file is subject to the terms and conditions of version 2 of 11 * the GNU General Public License. See the file COPYING in the main 12 * directory of this archive for more details. 13 * 14 * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62) 15 * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.) 16 * 17 * Note that hardware blinking violates the leds infrastructure driver 18 * interface since the hardware only supports blinking all LEDs with the 19 * same delay_on/delay_off rates. That is, only the LEDs that are set to 20 * blink will actually blink but all LEDs that are set to blink will blink 21 * in identical fashion. The delay_on/delay_off values of the last LED 22 * that is set to blink will be used for all of the blinking LEDs. 23 * Hardware blinking is disabled by default but can be enabled by setting 24 * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK' 25 * or by adding the 'nxp,hw-blink' property to the DTS. 26 */ 27 28 #include <linux/module.h> 29 #include <linux/delay.h> 30 #include <linux/string.h> 31 #include <linux/ctype.h> 32 #include <linux/leds.h> 33 #include <linux/err.h> 34 #include <linux/i2c.h> 35 #include <linux/property.h> 36 #include <linux/slab.h> 37 #include <linux/of.h> 38 #include <linux/platform_data/leds-pca963x.h> 39 40 /* LED select registers determine the source that drives LED outputs */ 41 #define PCA963X_LED_OFF 0x0 /* LED driver off */ 42 #define PCA963X_LED_ON 0x1 /* LED driver on */ 43 #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */ 44 #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */ 45 46 #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */ 47 48 #define PCA963X_MODE1 0x00 49 #define PCA963X_MODE2 0x01 50 #define PCA963X_PWM_BASE 0x02 51 52 enum pca963x_type { 53 pca9633, 54 pca9634, 55 pca9635, 56 }; 57 58 struct pca963x_chipdef { 59 u8 grppwm; 60 u8 grpfreq; 61 u8 ledout_base; 62 int n_leds; 63 unsigned int scaling; 64 }; 65 66 static struct pca963x_chipdef pca963x_chipdefs[] = { 67 [pca9633] = { 68 .grppwm = 0x6, 69 .grpfreq = 0x7, 70 .ledout_base = 0x8, 71 .n_leds = 4, 72 }, 73 [pca9634] = { 74 .grppwm = 0xa, 75 .grpfreq = 0xb, 76 .ledout_base = 0xc, 77 .n_leds = 8, 78 }, 79 [pca9635] = { 80 .grppwm = 0x12, 81 .grpfreq = 0x13, 82 .ledout_base = 0x14, 83 .n_leds = 16, 84 }, 85 }; 86 87 /* Total blink period in milliseconds */ 88 #define PCA963X_BLINK_PERIOD_MIN 42 89 #define PCA963X_BLINK_PERIOD_MAX 10667 90 91 static const struct i2c_device_id pca963x_id[] = { 92 { "pca9632", pca9633 }, 93 { "pca9633", pca9633 }, 94 { "pca9634", pca9634 }, 95 { "pca9635", pca9635 }, 96 { } 97 }; 98 MODULE_DEVICE_TABLE(i2c, pca963x_id); 99 100 struct pca963x_led; 101 102 struct pca963x { 103 struct pca963x_chipdef *chipdef; 104 struct mutex mutex; 105 struct i2c_client *client; 106 struct pca963x_led *leds; 107 unsigned long leds_on; 108 }; 109 110 struct pca963x_led { 111 struct pca963x *chip; 112 struct led_classdev led_cdev; 113 int led_num; /* 0 .. 15 potentially */ 114 char name[32]; 115 u8 gdc; 116 u8 gfrq; 117 }; 118 119 static int pca963x_brightness(struct pca963x_led *pca963x, 120 enum led_brightness brightness) 121 { 122 u8 ledout_addr = pca963x->chip->chipdef->ledout_base 123 + (pca963x->led_num / 4); 124 u8 ledout; 125 int shift = 2 * (pca963x->led_num % 4); 126 u8 mask = 0x3 << shift; 127 int ret; 128 129 ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); 130 switch (brightness) { 131 case LED_FULL: 132 ret = i2c_smbus_write_byte_data(pca963x->chip->client, 133 ledout_addr, 134 (ledout & ~mask) | (PCA963X_LED_ON << shift)); 135 break; 136 case LED_OFF: 137 ret = i2c_smbus_write_byte_data(pca963x->chip->client, 138 ledout_addr, ledout & ~mask); 139 break; 140 default: 141 ret = i2c_smbus_write_byte_data(pca963x->chip->client, 142 PCA963X_PWM_BASE + pca963x->led_num, 143 brightness); 144 if (ret < 0) 145 return ret; 146 ret = i2c_smbus_write_byte_data(pca963x->chip->client, 147 ledout_addr, 148 (ledout & ~mask) | (PCA963X_LED_PWM << shift)); 149 break; 150 } 151 152 return ret; 153 } 154 155 static void pca963x_blink(struct pca963x_led *pca963x) 156 { 157 u8 ledout_addr = pca963x->chip->chipdef->ledout_base + 158 (pca963x->led_num / 4); 159 u8 ledout; 160 u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client, 161 PCA963X_MODE2); 162 int shift = 2 * (pca963x->led_num % 4); 163 u8 mask = 0x3 << shift; 164 165 i2c_smbus_write_byte_data(pca963x->chip->client, 166 pca963x->chip->chipdef->grppwm, pca963x->gdc); 167 168 i2c_smbus_write_byte_data(pca963x->chip->client, 169 pca963x->chip->chipdef->grpfreq, pca963x->gfrq); 170 171 if (!(mode2 & PCA963X_MODE2_DMBLNK)) 172 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2, 173 mode2 | PCA963X_MODE2_DMBLNK); 174 175 mutex_lock(&pca963x->chip->mutex); 176 ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr); 177 if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) 178 i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr, 179 (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift)); 180 mutex_unlock(&pca963x->chip->mutex); 181 } 182 183 static int pca963x_power_state(struct pca963x_led *pca963x) 184 { 185 unsigned long *leds_on = &pca963x->chip->leds_on; 186 unsigned long cached_leds = pca963x->chip->leds_on; 187 188 if (pca963x->led_cdev.brightness) 189 set_bit(pca963x->led_num, leds_on); 190 else 191 clear_bit(pca963x->led_num, leds_on); 192 193 if (!(*leds_on) != !cached_leds) 194 return i2c_smbus_write_byte_data(pca963x->chip->client, 195 PCA963X_MODE1, *leds_on ? 0 : BIT(4)); 196 197 return 0; 198 } 199 200 static int pca963x_led_set(struct led_classdev *led_cdev, 201 enum led_brightness value) 202 { 203 struct pca963x_led *pca963x; 204 int ret; 205 206 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); 207 208 mutex_lock(&pca963x->chip->mutex); 209 210 ret = pca963x_brightness(pca963x, value); 211 if (ret < 0) 212 goto unlock; 213 ret = pca963x_power_state(pca963x); 214 215 unlock: 216 mutex_unlock(&pca963x->chip->mutex); 217 return ret; 218 } 219 220 static unsigned int pca963x_period_scale(struct pca963x_led *pca963x, 221 unsigned int val) 222 { 223 unsigned int scaling = pca963x->chip->chipdef->scaling; 224 225 return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val; 226 } 227 228 static int pca963x_blink_set(struct led_classdev *led_cdev, 229 unsigned long *delay_on, unsigned long *delay_off) 230 { 231 struct pca963x_led *pca963x; 232 unsigned long time_on, time_off, period; 233 u8 gdc, gfrq; 234 235 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev); 236 237 time_on = *delay_on; 238 time_off = *delay_off; 239 240 /* If both zero, pick reasonable defaults of 500ms each */ 241 if (!time_on && !time_off) { 242 time_on = 500; 243 time_off = 500; 244 } 245 246 period = pca963x_period_scale(pca963x, time_on + time_off); 247 248 /* If period not supported by hardware, default to someting sane. */ 249 if ((period < PCA963X_BLINK_PERIOD_MIN) || 250 (period > PCA963X_BLINK_PERIOD_MAX)) { 251 time_on = 500; 252 time_off = 500; 253 period = pca963x_period_scale(pca963x, 1000); 254 } 255 256 /* 257 * From manual: duty cycle = (GDC / 256) -> 258 * (time_on / period) = (GDC / 256) -> 259 * GDC = ((time_on * 256) / period) 260 */ 261 gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period; 262 263 /* 264 * From manual: period = ((GFRQ + 1) / 24) in seconds. 265 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) -> 266 * GFRQ = ((period * 24 / 1000) - 1) 267 */ 268 gfrq = (period * 24 / 1000) - 1; 269 270 pca963x->gdc = gdc; 271 pca963x->gfrq = gfrq; 272 273 pca963x_blink(pca963x); 274 275 *delay_on = time_on; 276 *delay_off = time_off; 277 278 return 0; 279 } 280 281 static struct pca963x_platform_data * 282 pca963x_get_pdata(struct i2c_client *client, struct pca963x_chipdef *chip) 283 { 284 struct pca963x_platform_data *pdata; 285 struct led_info *pca963x_leds; 286 struct fwnode_handle *child; 287 int count; 288 289 count = device_get_child_node_count(&client->dev); 290 if (!count || count > chip->n_leds) 291 return ERR_PTR(-ENODEV); 292 293 pca963x_leds = devm_kcalloc(&client->dev, 294 chip->n_leds, sizeof(struct led_info), GFP_KERNEL); 295 if (!pca963x_leds) 296 return ERR_PTR(-ENOMEM); 297 298 device_for_each_child_node(&client->dev, child) { 299 struct led_info led = {}; 300 u32 reg; 301 int res; 302 303 res = fwnode_property_read_u32(child, "reg", ®); 304 if ((res != 0) || (reg >= chip->n_leds)) 305 continue; 306 307 res = fwnode_property_read_string(child, "label", &led.name); 308 if ((res != 0) && is_of_node(child)) 309 led.name = to_of_node(child)->name; 310 311 fwnode_property_read_string(child, "linux,default-trigger", 312 &led.default_trigger); 313 314 pca963x_leds[reg] = led; 315 } 316 pdata = devm_kzalloc(&client->dev, 317 sizeof(struct pca963x_platform_data), GFP_KERNEL); 318 if (!pdata) 319 return ERR_PTR(-ENOMEM); 320 321 pdata->leds.leds = pca963x_leds; 322 pdata->leds.num_leds = chip->n_leds; 323 324 /* default to open-drain unless totem pole (push-pull) is specified */ 325 if (device_property_read_bool(&client->dev, "nxp,totem-pole")) 326 pdata->outdrv = PCA963X_TOTEM_POLE; 327 else 328 pdata->outdrv = PCA963X_OPEN_DRAIN; 329 330 /* default to software blinking unless hardware blinking is specified */ 331 if (device_property_read_bool(&client->dev, "nxp,hw-blink")) 332 pdata->blink_type = PCA963X_HW_BLINK; 333 else 334 pdata->blink_type = PCA963X_SW_BLINK; 335 336 if (device_property_read_u32(&client->dev, "nxp,period-scale", 337 &chip->scaling)) 338 chip->scaling = 1000; 339 340 /* default to non-inverted output, unless inverted is specified */ 341 if (device_property_read_bool(&client->dev, "nxp,inverted-out")) 342 pdata->dir = PCA963X_INVERTED; 343 else 344 pdata->dir = PCA963X_NORMAL; 345 346 return pdata; 347 } 348 349 static const struct of_device_id of_pca963x_match[] = { 350 { .compatible = "nxp,pca9632", }, 351 { .compatible = "nxp,pca9633", }, 352 { .compatible = "nxp,pca9634", }, 353 { .compatible = "nxp,pca9635", }, 354 {}, 355 }; 356 MODULE_DEVICE_TABLE(of, of_pca963x_match); 357 358 static int pca963x_probe(struct i2c_client *client, 359 const struct i2c_device_id *id) 360 { 361 struct pca963x *pca963x_chip; 362 struct pca963x_led *pca963x; 363 struct pca963x_platform_data *pdata; 364 struct pca963x_chipdef *chip; 365 int i, err; 366 367 chip = &pca963x_chipdefs[id->driver_data]; 368 pdata = dev_get_platdata(&client->dev); 369 370 if (!pdata) { 371 pdata = pca963x_get_pdata(client, chip); 372 if (IS_ERR(pdata)) { 373 dev_warn(&client->dev, "could not parse configuration\n"); 374 pdata = NULL; 375 } 376 } 377 378 if (pdata && (pdata->leds.num_leds < 1 || 379 pdata->leds.num_leds > chip->n_leds)) { 380 dev_err(&client->dev, "board info must claim 1-%d LEDs", 381 chip->n_leds); 382 return -EINVAL; 383 } 384 385 pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip), 386 GFP_KERNEL); 387 if (!pca963x_chip) 388 return -ENOMEM; 389 pca963x = devm_kcalloc(&client->dev, chip->n_leds, sizeof(*pca963x), 390 GFP_KERNEL); 391 if (!pca963x) 392 return -ENOMEM; 393 394 i2c_set_clientdata(client, pca963x_chip); 395 396 mutex_init(&pca963x_chip->mutex); 397 pca963x_chip->chipdef = chip; 398 pca963x_chip->client = client; 399 pca963x_chip->leds = pca963x; 400 401 /* Turn off LEDs by default*/ 402 for (i = 0; i < chip->n_leds / 4; i++) 403 i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00); 404 405 for (i = 0; i < chip->n_leds; i++) { 406 pca963x[i].led_num = i; 407 pca963x[i].chip = pca963x_chip; 408 409 /* Platform data can specify LED names and default triggers */ 410 if (pdata && i < pdata->leds.num_leds) { 411 if (pdata->leds.leds[i].name) 412 snprintf(pca963x[i].name, 413 sizeof(pca963x[i].name), "pca963x:%s", 414 pdata->leds.leds[i].name); 415 if (pdata->leds.leds[i].default_trigger) 416 pca963x[i].led_cdev.default_trigger = 417 pdata->leds.leds[i].default_trigger; 418 } 419 if (!pdata || i >= pdata->leds.num_leds || 420 !pdata->leds.leds[i].name) 421 snprintf(pca963x[i].name, sizeof(pca963x[i].name), 422 "pca963x:%d:%.2x:%d", client->adapter->nr, 423 client->addr, i); 424 425 pca963x[i].led_cdev.name = pca963x[i].name; 426 pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set; 427 428 if (pdata && pdata->blink_type == PCA963X_HW_BLINK) 429 pca963x[i].led_cdev.blink_set = pca963x_blink_set; 430 431 err = led_classdev_register(&client->dev, &pca963x[i].led_cdev); 432 if (err < 0) 433 goto exit; 434 } 435 436 /* Disable LED all-call address, and power down initially */ 437 i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4)); 438 439 if (pdata) { 440 u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client, 441 PCA963X_MODE2); 442 /* Configure output: open-drain or totem pole (push-pull) */ 443 if (pdata->outdrv == PCA963X_OPEN_DRAIN) 444 mode2 |= 0x01; 445 else 446 mode2 |= 0x05; 447 /* Configure direction: normal or inverted */ 448 if (pdata->dir == PCA963X_INVERTED) 449 mode2 |= 0x10; 450 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2, 451 mode2); 452 } 453 454 return 0; 455 456 exit: 457 while (i--) 458 led_classdev_unregister(&pca963x[i].led_cdev); 459 460 return err; 461 } 462 463 static int pca963x_remove(struct i2c_client *client) 464 { 465 struct pca963x *pca963x = i2c_get_clientdata(client); 466 int i; 467 468 for (i = 0; i < pca963x->chipdef->n_leds; i++) 469 led_classdev_unregister(&pca963x->leds[i].led_cdev); 470 471 return 0; 472 } 473 474 static struct i2c_driver pca963x_driver = { 475 .driver = { 476 .name = "leds-pca963x", 477 .of_match_table = of_pca963x_match, 478 }, 479 .probe = pca963x_probe, 480 .remove = pca963x_remove, 481 .id_table = pca963x_id, 482 }; 483 484 module_i2c_driver(pca963x_driver); 485 486 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>"); 487 MODULE_DESCRIPTION("PCA963X LED driver"); 488 MODULE_LICENSE("GPL v2"); 489