162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * leds-max8997.c - LED class driver for MAX8997 LEDs. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2011 Samsung Electronics 662306a36Sopenharmony_ci * Donggeun Kim <dg77.kim@samsung.com> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/module.h> 1062306a36Sopenharmony_ci#include <linux/err.h> 1162306a36Sopenharmony_ci#include <linux/slab.h> 1262306a36Sopenharmony_ci#include <linux/leds.h> 1362306a36Sopenharmony_ci#include <linux/mfd/max8997.h> 1462306a36Sopenharmony_ci#include <linux/mfd/max8997-private.h> 1562306a36Sopenharmony_ci#include <linux/platform_device.h> 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#define MAX8997_LED_FLASH_SHIFT 3 1862306a36Sopenharmony_ci#define MAX8997_LED_FLASH_CUR_MASK 0xf8 1962306a36Sopenharmony_ci#define MAX8997_LED_MOVIE_SHIFT 4 2062306a36Sopenharmony_ci#define MAX8997_LED_MOVIE_CUR_MASK 0xf0 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci#define MAX8997_LED_FLASH_MAX_BRIGHTNESS 0x1f 2362306a36Sopenharmony_ci#define MAX8997_LED_MOVIE_MAX_BRIGHTNESS 0xf 2462306a36Sopenharmony_ci#define MAX8997_LED_NONE_MAX_BRIGHTNESS 0 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci#define MAX8997_LED0_FLASH_MASK 0x1 2762306a36Sopenharmony_ci#define MAX8997_LED0_FLASH_PIN_MASK 0x5 2862306a36Sopenharmony_ci#define MAX8997_LED0_MOVIE_MASK 0x8 2962306a36Sopenharmony_ci#define MAX8997_LED0_MOVIE_PIN_MASK 0x28 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci#define MAX8997_LED1_FLASH_MASK 0x2 3262306a36Sopenharmony_ci#define MAX8997_LED1_FLASH_PIN_MASK 0x6 3362306a36Sopenharmony_ci#define MAX8997_LED1_MOVIE_MASK 0x10 3462306a36Sopenharmony_ci#define MAX8997_LED1_MOVIE_PIN_MASK 0x30 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci#define MAX8997_LED_BOOST_ENABLE_MASK (1 << 6) 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_cistruct max8997_led { 3962306a36Sopenharmony_ci struct max8997_dev *iodev; 4062306a36Sopenharmony_ci struct led_classdev cdev; 4162306a36Sopenharmony_ci bool enabled; 4262306a36Sopenharmony_ci int id; 4362306a36Sopenharmony_ci enum max8997_led_mode led_mode; 4462306a36Sopenharmony_ci struct mutex mutex; 4562306a36Sopenharmony_ci}; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_cistatic void max8997_led_set_mode(struct max8997_led *led, 4862306a36Sopenharmony_ci enum max8997_led_mode mode) 4962306a36Sopenharmony_ci{ 5062306a36Sopenharmony_ci int ret; 5162306a36Sopenharmony_ci struct i2c_client *client = led->iodev->i2c; 5262306a36Sopenharmony_ci u8 mask = 0, val; 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci switch (mode) { 5562306a36Sopenharmony_ci case MAX8997_FLASH_MODE: 5662306a36Sopenharmony_ci mask = MAX8997_LED1_FLASH_MASK | MAX8997_LED0_FLASH_MASK; 5762306a36Sopenharmony_ci val = led->id ? 5862306a36Sopenharmony_ci MAX8997_LED1_FLASH_MASK : MAX8997_LED0_FLASH_MASK; 5962306a36Sopenharmony_ci led->cdev.max_brightness = MAX8997_LED_FLASH_MAX_BRIGHTNESS; 6062306a36Sopenharmony_ci break; 6162306a36Sopenharmony_ci case MAX8997_MOVIE_MODE: 6262306a36Sopenharmony_ci mask = MAX8997_LED1_MOVIE_MASK | MAX8997_LED0_MOVIE_MASK; 6362306a36Sopenharmony_ci val = led->id ? 6462306a36Sopenharmony_ci MAX8997_LED1_MOVIE_MASK : MAX8997_LED0_MOVIE_MASK; 6562306a36Sopenharmony_ci led->cdev.max_brightness = MAX8997_LED_MOVIE_MAX_BRIGHTNESS; 6662306a36Sopenharmony_ci break; 6762306a36Sopenharmony_ci case MAX8997_FLASH_PIN_CONTROL_MODE: 6862306a36Sopenharmony_ci mask = MAX8997_LED1_FLASH_PIN_MASK | 6962306a36Sopenharmony_ci MAX8997_LED0_FLASH_PIN_MASK; 7062306a36Sopenharmony_ci val = led->id ? 7162306a36Sopenharmony_ci MAX8997_LED1_FLASH_PIN_MASK : MAX8997_LED0_FLASH_PIN_MASK; 7262306a36Sopenharmony_ci led->cdev.max_brightness = MAX8997_LED_FLASH_MAX_BRIGHTNESS; 7362306a36Sopenharmony_ci break; 7462306a36Sopenharmony_ci case MAX8997_MOVIE_PIN_CONTROL_MODE: 7562306a36Sopenharmony_ci mask = MAX8997_LED1_MOVIE_PIN_MASK | 7662306a36Sopenharmony_ci MAX8997_LED0_MOVIE_PIN_MASK; 7762306a36Sopenharmony_ci val = led->id ? 7862306a36Sopenharmony_ci MAX8997_LED1_MOVIE_PIN_MASK : MAX8997_LED0_MOVIE_PIN_MASK; 7962306a36Sopenharmony_ci led->cdev.max_brightness = MAX8997_LED_MOVIE_MAX_BRIGHTNESS; 8062306a36Sopenharmony_ci break; 8162306a36Sopenharmony_ci default: 8262306a36Sopenharmony_ci led->cdev.max_brightness = MAX8997_LED_NONE_MAX_BRIGHTNESS; 8362306a36Sopenharmony_ci break; 8462306a36Sopenharmony_ci } 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci if (mask) { 8762306a36Sopenharmony_ci ret = max8997_update_reg(client, MAX8997_REG_LEN_CNTL, val, 8862306a36Sopenharmony_ci mask); 8962306a36Sopenharmony_ci if (ret) 9062306a36Sopenharmony_ci dev_err(led->iodev->dev, 9162306a36Sopenharmony_ci "failed to update register(%d)\n", ret); 9262306a36Sopenharmony_ci } 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci led->led_mode = mode; 9562306a36Sopenharmony_ci} 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_cistatic void max8997_led_enable(struct max8997_led *led, bool enable) 9862306a36Sopenharmony_ci{ 9962306a36Sopenharmony_ci int ret; 10062306a36Sopenharmony_ci struct i2c_client *client = led->iodev->i2c; 10162306a36Sopenharmony_ci u8 val = 0, mask = MAX8997_LED_BOOST_ENABLE_MASK; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci if (led->enabled == enable) 10462306a36Sopenharmony_ci return; 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci val = enable ? MAX8997_LED_BOOST_ENABLE_MASK : 0; 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci ret = max8997_update_reg(client, MAX8997_REG_BOOST_CNTL, val, mask); 10962306a36Sopenharmony_ci if (ret) 11062306a36Sopenharmony_ci dev_err(led->iodev->dev, 11162306a36Sopenharmony_ci "failed to update register(%d)\n", ret); 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci led->enabled = enable; 11462306a36Sopenharmony_ci} 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_cistatic void max8997_led_set_current(struct max8997_led *led, 11762306a36Sopenharmony_ci enum led_brightness value) 11862306a36Sopenharmony_ci{ 11962306a36Sopenharmony_ci int ret; 12062306a36Sopenharmony_ci struct i2c_client *client = led->iodev->i2c; 12162306a36Sopenharmony_ci u8 val = 0, mask = 0, reg = 0; 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci switch (led->led_mode) { 12462306a36Sopenharmony_ci case MAX8997_FLASH_MODE: 12562306a36Sopenharmony_ci case MAX8997_FLASH_PIN_CONTROL_MODE: 12662306a36Sopenharmony_ci val = value << MAX8997_LED_FLASH_SHIFT; 12762306a36Sopenharmony_ci mask = MAX8997_LED_FLASH_CUR_MASK; 12862306a36Sopenharmony_ci reg = led->id ? MAX8997_REG_FLASH2_CUR : MAX8997_REG_FLASH1_CUR; 12962306a36Sopenharmony_ci break; 13062306a36Sopenharmony_ci case MAX8997_MOVIE_MODE: 13162306a36Sopenharmony_ci case MAX8997_MOVIE_PIN_CONTROL_MODE: 13262306a36Sopenharmony_ci val = value << MAX8997_LED_MOVIE_SHIFT; 13362306a36Sopenharmony_ci mask = MAX8997_LED_MOVIE_CUR_MASK; 13462306a36Sopenharmony_ci reg = MAX8997_REG_MOVIE_CUR; 13562306a36Sopenharmony_ci break; 13662306a36Sopenharmony_ci default: 13762306a36Sopenharmony_ci break; 13862306a36Sopenharmony_ci } 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci if (mask) { 14162306a36Sopenharmony_ci ret = max8997_update_reg(client, reg, val, mask); 14262306a36Sopenharmony_ci if (ret) 14362306a36Sopenharmony_ci dev_err(led->iodev->dev, 14462306a36Sopenharmony_ci "failed to update register(%d)\n", ret); 14562306a36Sopenharmony_ci } 14662306a36Sopenharmony_ci} 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_cistatic void max8997_led_brightness_set(struct led_classdev *led_cdev, 14962306a36Sopenharmony_ci enum led_brightness value) 15062306a36Sopenharmony_ci{ 15162306a36Sopenharmony_ci struct max8997_led *led = 15262306a36Sopenharmony_ci container_of(led_cdev, struct max8997_led, cdev); 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci if (value) { 15562306a36Sopenharmony_ci max8997_led_set_current(led, value); 15662306a36Sopenharmony_ci max8997_led_enable(led, true); 15762306a36Sopenharmony_ci } else { 15862306a36Sopenharmony_ci max8997_led_set_current(led, value); 15962306a36Sopenharmony_ci max8997_led_enable(led, false); 16062306a36Sopenharmony_ci } 16162306a36Sopenharmony_ci} 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_cistatic ssize_t mode_show(struct device *dev, 16462306a36Sopenharmony_ci struct device_attribute *attr, char *buf) 16562306a36Sopenharmony_ci{ 16662306a36Sopenharmony_ci struct led_classdev *led_cdev = dev_get_drvdata(dev); 16762306a36Sopenharmony_ci struct max8997_led *led = 16862306a36Sopenharmony_ci container_of(led_cdev, struct max8997_led, cdev); 16962306a36Sopenharmony_ci ssize_t ret = 0; 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci mutex_lock(&led->mutex); 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci switch (led->led_mode) { 17462306a36Sopenharmony_ci case MAX8997_FLASH_MODE: 17562306a36Sopenharmony_ci ret += sprintf(buf, "FLASH\n"); 17662306a36Sopenharmony_ci break; 17762306a36Sopenharmony_ci case MAX8997_MOVIE_MODE: 17862306a36Sopenharmony_ci ret += sprintf(buf, "MOVIE\n"); 17962306a36Sopenharmony_ci break; 18062306a36Sopenharmony_ci case MAX8997_FLASH_PIN_CONTROL_MODE: 18162306a36Sopenharmony_ci ret += sprintf(buf, "FLASH_PIN_CONTROL\n"); 18262306a36Sopenharmony_ci break; 18362306a36Sopenharmony_ci case MAX8997_MOVIE_PIN_CONTROL_MODE: 18462306a36Sopenharmony_ci ret += sprintf(buf, "MOVIE_PIN_CONTROL\n"); 18562306a36Sopenharmony_ci break; 18662306a36Sopenharmony_ci default: 18762306a36Sopenharmony_ci ret += sprintf(buf, "NONE\n"); 18862306a36Sopenharmony_ci break; 18962306a36Sopenharmony_ci } 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci mutex_unlock(&led->mutex); 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci return ret; 19462306a36Sopenharmony_ci} 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_cistatic ssize_t mode_store(struct device *dev, 19762306a36Sopenharmony_ci struct device_attribute *attr, 19862306a36Sopenharmony_ci const char *buf, size_t size) 19962306a36Sopenharmony_ci{ 20062306a36Sopenharmony_ci struct led_classdev *led_cdev = dev_get_drvdata(dev); 20162306a36Sopenharmony_ci struct max8997_led *led = 20262306a36Sopenharmony_ci container_of(led_cdev, struct max8997_led, cdev); 20362306a36Sopenharmony_ci enum max8997_led_mode mode; 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci mutex_lock(&led->mutex); 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci if (!strncmp(buf, "FLASH_PIN_CONTROL", 17)) 20862306a36Sopenharmony_ci mode = MAX8997_FLASH_PIN_CONTROL_MODE; 20962306a36Sopenharmony_ci else if (!strncmp(buf, "MOVIE_PIN_CONTROL", 17)) 21062306a36Sopenharmony_ci mode = MAX8997_MOVIE_PIN_CONTROL_MODE; 21162306a36Sopenharmony_ci else if (!strncmp(buf, "FLASH", 5)) 21262306a36Sopenharmony_ci mode = MAX8997_FLASH_MODE; 21362306a36Sopenharmony_ci else if (!strncmp(buf, "MOVIE", 5)) 21462306a36Sopenharmony_ci mode = MAX8997_MOVIE_MODE; 21562306a36Sopenharmony_ci else 21662306a36Sopenharmony_ci mode = MAX8997_NONE; 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci max8997_led_set_mode(led, mode); 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci mutex_unlock(&led->mutex); 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci return size; 22362306a36Sopenharmony_ci} 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_cistatic DEVICE_ATTR_RW(mode); 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_cistatic struct attribute *max8997_attrs[] = { 22862306a36Sopenharmony_ci &dev_attr_mode.attr, 22962306a36Sopenharmony_ci NULL 23062306a36Sopenharmony_ci}; 23162306a36Sopenharmony_ciATTRIBUTE_GROUPS(max8997); 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_cistatic int max8997_led_probe(struct platform_device *pdev) 23462306a36Sopenharmony_ci{ 23562306a36Sopenharmony_ci struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent); 23662306a36Sopenharmony_ci struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev); 23762306a36Sopenharmony_ci struct max8997_led *led; 23862306a36Sopenharmony_ci char name[20]; 23962306a36Sopenharmony_ci int ret = 0; 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL); 24262306a36Sopenharmony_ci if (led == NULL) 24362306a36Sopenharmony_ci return -ENOMEM; 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci led->id = pdev->id; 24662306a36Sopenharmony_ci snprintf(name, sizeof(name), "max8997-led%d", pdev->id); 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci led->cdev.name = name; 24962306a36Sopenharmony_ci led->cdev.brightness_set = max8997_led_brightness_set; 25062306a36Sopenharmony_ci led->cdev.flags |= LED_CORE_SUSPENDRESUME; 25162306a36Sopenharmony_ci led->cdev.brightness = 0; 25262306a36Sopenharmony_ci led->cdev.groups = max8997_groups; 25362306a36Sopenharmony_ci led->iodev = iodev; 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci /* initialize mode and brightness according to platform_data */ 25662306a36Sopenharmony_ci if (pdata && pdata->led_pdata) { 25762306a36Sopenharmony_ci u8 mode = 0, brightness = 0; 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ci mode = pdata->led_pdata->mode[led->id]; 26062306a36Sopenharmony_ci brightness = pdata->led_pdata->brightness[led->id]; 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci max8997_led_set_mode(led, mode); 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci if (brightness > led->cdev.max_brightness) 26562306a36Sopenharmony_ci brightness = led->cdev.max_brightness; 26662306a36Sopenharmony_ci max8997_led_set_current(led, brightness); 26762306a36Sopenharmony_ci led->cdev.brightness = brightness; 26862306a36Sopenharmony_ci } else { 26962306a36Sopenharmony_ci max8997_led_set_mode(led, MAX8997_NONE); 27062306a36Sopenharmony_ci max8997_led_set_current(led, 0); 27162306a36Sopenharmony_ci } 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ci mutex_init(&led->mutex); 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci ret = devm_led_classdev_register(&pdev->dev, &led->cdev); 27662306a36Sopenharmony_ci if (ret < 0) 27762306a36Sopenharmony_ci return ret; 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci return 0; 28062306a36Sopenharmony_ci} 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_cistatic struct platform_driver max8997_led_driver = { 28362306a36Sopenharmony_ci .driver = { 28462306a36Sopenharmony_ci .name = "max8997-led", 28562306a36Sopenharmony_ci }, 28662306a36Sopenharmony_ci .probe = max8997_led_probe, 28762306a36Sopenharmony_ci}; 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_cimodule_platform_driver(max8997_led_driver); 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ciMODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); 29262306a36Sopenharmony_ciMODULE_DESCRIPTION("MAX8997 LED driver"); 29362306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 29462306a36Sopenharmony_ciMODULE_ALIAS("platform:max8997-led"); 295