1eb80411bSKim, Milo /* 2eb80411bSKim, Milo * TI LP8788 MFD - keyled driver 3eb80411bSKim, Milo * 4eb80411bSKim, Milo * Copyright 2012 Texas Instruments 5eb80411bSKim, Milo * 6eb80411bSKim, Milo * Author: Milo(Woogyom) Kim <milo.kim@ti.com> 7eb80411bSKim, Milo * 8eb80411bSKim, Milo * This program is free software; you can redistribute it and/or modify 9eb80411bSKim, Milo * it under the terms of the GNU General Public License version 2 as 10eb80411bSKim, Milo * published by the Free Software Foundation. 11eb80411bSKim, Milo * 12eb80411bSKim, Milo */ 13eb80411bSKim, Milo 14eb80411bSKim, Milo #include <linux/module.h> 15eb80411bSKim, Milo #include <linux/slab.h> 16eb80411bSKim, Milo #include <linux/err.h> 17eb80411bSKim, Milo #include <linux/platform_device.h> 18eb80411bSKim, Milo #include <linux/leds.h> 19eb80411bSKim, Milo #include <linux/mutex.h> 20eb80411bSKim, Milo #include <linux/mfd/lp8788.h> 21eb80411bSKim, Milo #include <linux/mfd/lp8788-isink.h> 22eb80411bSKim, Milo 23eb80411bSKim, Milo #define MAX_BRIGHTNESS LP8788_ISINK_MAX_PWM 24eb80411bSKim, Milo #define DEFAULT_LED_NAME "keyboard-backlight" 25eb80411bSKim, Milo 26eb80411bSKim, Milo struct lp8788_led { 27eb80411bSKim, Milo struct lp8788 *lp; 28eb80411bSKim, Milo struct mutex lock; 29eb80411bSKim, Milo struct led_classdev led_dev; 30eb80411bSKim, Milo enum lp8788_isink_number isink_num; 31eb80411bSKim, Milo int on; 32eb80411bSKim, Milo }; 33eb80411bSKim, Milo 34eb80411bSKim, Milo struct lp8788_led_config { 35eb80411bSKim, Milo enum lp8788_isink_scale scale; 36eb80411bSKim, Milo enum lp8788_isink_number num; 37eb80411bSKim, Milo int iout; 38eb80411bSKim, Milo }; 39eb80411bSKim, Milo 40eb80411bSKim, Milo static struct lp8788_led_config default_led_config = { 41eb80411bSKim, Milo .scale = LP8788_ISINK_SCALE_100mA, 42eb80411bSKim, Milo .num = LP8788_ISINK_3, 43eb80411bSKim, Milo .iout = 0, 44eb80411bSKim, Milo }; 45eb80411bSKim, Milo 46eb80411bSKim, Milo static int lp8788_led_init_device(struct lp8788_led *led, 47eb80411bSKim, Milo struct lp8788_led_platform_data *pdata) 48eb80411bSKim, Milo { 49eb80411bSKim, Milo struct lp8788_led_config *cfg = &default_led_config; 50eb80411bSKim, Milo u8 addr, mask, val; 51eb80411bSKim, Milo int ret; 52eb80411bSKim, Milo 53eb80411bSKim, Milo if (pdata) { 54eb80411bSKim, Milo cfg->scale = pdata->scale; 55eb80411bSKim, Milo cfg->num = pdata->num; 56eb80411bSKim, Milo cfg->iout = pdata->iout_code; 57eb80411bSKim, Milo } 58eb80411bSKim, Milo 59eb80411bSKim, Milo led->isink_num = cfg->num; 60eb80411bSKim, Milo 61eb80411bSKim, Milo /* scale configuration */ 62eb80411bSKim, Milo addr = LP8788_ISINK_CTRL; 63eb80411bSKim, Milo mask = 1 << (cfg->num + LP8788_ISINK_SCALE_OFFSET); 644df7309aSAxel Lin val = cfg->scale << (cfg->num + LP8788_ISINK_SCALE_OFFSET); 65eb80411bSKim, Milo ret = lp8788_update_bits(led->lp, addr, mask, val); 66eb80411bSKim, Milo if (ret) 67eb80411bSKim, Milo return ret; 68eb80411bSKim, Milo 69eb80411bSKim, Milo /* current configuration */ 70eb80411bSKim, Milo addr = lp8788_iout_addr[cfg->num]; 71eb80411bSKim, Milo mask = lp8788_iout_mask[cfg->num]; 72eb80411bSKim, Milo val = cfg->iout; 73eb80411bSKim, Milo 74eb80411bSKim, Milo return lp8788_update_bits(led->lp, addr, mask, val); 75eb80411bSKim, Milo } 76eb80411bSKim, Milo 7764998371SAndrew Lunn static int lp8788_led_enable(struct lp8788_led *led, 78eb80411bSKim, Milo enum lp8788_isink_number num, int on) 79eb80411bSKim, Milo { 8064998371SAndrew Lunn int ret; 8164998371SAndrew Lunn 82eb80411bSKim, Milo u8 mask = 1 << num; 83eb80411bSKim, Milo u8 val = on << num; 84eb80411bSKim, Milo 8564998371SAndrew Lunn ret = lp8788_update_bits(led->lp, LP8788_ISINK_CTRL, mask, val); 8664998371SAndrew Lunn if (ret == 0) 87eb80411bSKim, Milo led->on = on; 8864998371SAndrew Lunn 8964998371SAndrew Lunn return ret; 90eb80411bSKim, Milo } 91eb80411bSKim, Milo 9264998371SAndrew Lunn static int lp8788_brightness_set(struct led_classdev *led_cdev, 9364998371SAndrew Lunn enum led_brightness val) 94eb80411bSKim, Milo { 9564998371SAndrew Lunn struct lp8788_led *led = 9664998371SAndrew Lunn container_of(led_cdev, struct lp8788_led, led_dev); 9764998371SAndrew Lunn 98eb80411bSKim, Milo enum lp8788_isink_number num = led->isink_num; 9964998371SAndrew Lunn int enable, ret; 100eb80411bSKim, Milo 101eb80411bSKim, Milo mutex_lock(&led->lock); 102eb80411bSKim, Milo 103eb80411bSKim, Milo switch (num) { 104eb80411bSKim, Milo case LP8788_ISINK_1: 105eb80411bSKim, Milo case LP8788_ISINK_2: 106eb80411bSKim, Milo case LP8788_ISINK_3: 10764998371SAndrew Lunn ret = lp8788_write_byte(led->lp, lp8788_pwm_addr[num], val); 10864998371SAndrew Lunn if (ret < 0) 10964998371SAndrew Lunn goto unlock; 110eb80411bSKim, Milo break; 111eb80411bSKim, Milo default: 112d45bb116SDevendra Naga mutex_unlock(&led->lock); 11364998371SAndrew Lunn return -EINVAL; 114eb80411bSKim, Milo } 115eb80411bSKim, Milo 116eb80411bSKim, Milo enable = (val > 0) ? 1 : 0; 117eb80411bSKim, Milo if (enable != led->on) 11864998371SAndrew Lunn ret = lp8788_led_enable(led, num, enable); 11964998371SAndrew Lunn unlock: 120eb80411bSKim, Milo mutex_unlock(&led->lock); 12164998371SAndrew Lunn return ret; 122eb80411bSKim, Milo } 123eb80411bSKim, Milo 12498ea1ea2SBill Pemberton static int lp8788_led_probe(struct platform_device *pdev) 125eb80411bSKim, Milo { 126eb80411bSKim, Milo struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent); 127eb80411bSKim, Milo struct lp8788_led_platform_data *led_pdata; 128eb80411bSKim, Milo struct lp8788_led *led; 129a1932edfSKim, Milo struct device *dev = &pdev->dev; 130eb80411bSKim, Milo int ret; 131eb80411bSKim, Milo 132a1932edfSKim, Milo led = devm_kzalloc(dev, sizeof(struct lp8788_led), GFP_KERNEL); 133eb80411bSKim, Milo if (!led) 134eb80411bSKim, Milo return -ENOMEM; 135eb80411bSKim, Milo 136eb80411bSKim, Milo led->lp = lp; 137eb80411bSKim, Milo led->led_dev.max_brightness = MAX_BRIGHTNESS; 13864998371SAndrew Lunn led->led_dev.brightness_set_blocking = lp8788_brightness_set; 139eb80411bSKim, Milo 140eb80411bSKim, Milo led_pdata = lp->pdata ? lp->pdata->led_pdata : NULL; 141eb80411bSKim, Milo 142eb80411bSKim, Milo if (!led_pdata || !led_pdata->name) 143eb80411bSKim, Milo led->led_dev.name = DEFAULT_LED_NAME; 144eb80411bSKim, Milo else 145eb80411bSKim, Milo led->led_dev.name = led_pdata->name; 146eb80411bSKim, Milo 147eb80411bSKim, Milo mutex_init(&led->lock); 148eb80411bSKim, Milo 149eb80411bSKim, Milo ret = lp8788_led_init_device(led, led_pdata); 150eb80411bSKim, Milo if (ret) { 151a1932edfSKim, Milo dev_err(dev, "led init device err: %d\n", ret); 152eb80411bSKim, Milo return ret; 153eb80411bSKim, Milo } 154eb80411bSKim, Milo 155*f165a66eSAmitoj Kaur Chawla ret = devm_led_classdev_register(dev, &led->led_dev); 156eb80411bSKim, Milo if (ret) { 157a1932edfSKim, Milo dev_err(dev, "led register err: %d\n", ret); 158eb80411bSKim, Milo return ret; 159eb80411bSKim, Milo } 160eb80411bSKim, Milo 161eb80411bSKim, Milo return 0; 162eb80411bSKim, Milo } 163eb80411bSKim, Milo 164eb80411bSKim, Milo static struct platform_driver lp8788_led_driver = { 165eb80411bSKim, Milo .probe = lp8788_led_probe, 166eb80411bSKim, Milo .driver = { 167eb80411bSKim, Milo .name = LP8788_DEV_KEYLED, 168eb80411bSKim, Milo }, 169eb80411bSKim, Milo }; 170eb80411bSKim, Milo module_platform_driver(lp8788_led_driver); 171eb80411bSKim, Milo 172eb80411bSKim, Milo MODULE_DESCRIPTION("Texas Instruments LP8788 Keyboard LED Driver"); 173eb80411bSKim, Milo MODULE_AUTHOR("Milo Kim"); 174eb80411bSKim, Milo MODULE_LICENSE("GPL"); 175eb80411bSKim, Milo MODULE_ALIAS("platform:lp8788-keyled"); 176