xref: /linux/drivers/leds/leds-max8997.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28584cb82SDonggeun Kim /*
38584cb82SDonggeun Kim  * leds-max8997.c - LED class driver for MAX8997 LEDs.
48584cb82SDonggeun Kim  *
58584cb82SDonggeun Kim  * Copyright (C) 2011 Samsung Electronics
68584cb82SDonggeun Kim  * Donggeun Kim <dg77.kim@samsung.com>
78584cb82SDonggeun Kim  */
88584cb82SDonggeun Kim 
98584cb82SDonggeun Kim #include <linux/module.h>
108584cb82SDonggeun Kim #include <linux/err.h>
118584cb82SDonggeun Kim #include <linux/slab.h>
128584cb82SDonggeun Kim #include <linux/leds.h>
138584cb82SDonggeun Kim #include <linux/mfd/max8997.h>
148584cb82SDonggeun Kim #include <linux/mfd/max8997-private.h>
158584cb82SDonggeun Kim #include <linux/platform_device.h>
168584cb82SDonggeun Kim 
178584cb82SDonggeun Kim #define MAX8997_LED_FLASH_SHIFT			3
188584cb82SDonggeun Kim #define MAX8997_LED_FLASH_CUR_MASK		0xf8
198584cb82SDonggeun Kim #define MAX8997_LED_MOVIE_SHIFT			4
208584cb82SDonggeun Kim #define MAX8997_LED_MOVIE_CUR_MASK		0xf0
218584cb82SDonggeun Kim 
228584cb82SDonggeun Kim #define MAX8997_LED_FLASH_MAX_BRIGHTNESS	0x1f
238584cb82SDonggeun Kim #define MAX8997_LED_MOVIE_MAX_BRIGHTNESS	0xf
248584cb82SDonggeun Kim #define MAX8997_LED_NONE_MAX_BRIGHTNESS		0
258584cb82SDonggeun Kim 
268584cb82SDonggeun Kim #define MAX8997_LED0_FLASH_MASK			0x1
278584cb82SDonggeun Kim #define MAX8997_LED0_FLASH_PIN_MASK		0x5
288584cb82SDonggeun Kim #define MAX8997_LED0_MOVIE_MASK			0x8
298584cb82SDonggeun Kim #define MAX8997_LED0_MOVIE_PIN_MASK		0x28
308584cb82SDonggeun Kim 
318584cb82SDonggeun Kim #define MAX8997_LED1_FLASH_MASK			0x2
328584cb82SDonggeun Kim #define MAX8997_LED1_FLASH_PIN_MASK		0x6
338584cb82SDonggeun Kim #define MAX8997_LED1_MOVIE_MASK			0x10
348584cb82SDonggeun Kim #define MAX8997_LED1_MOVIE_PIN_MASK		0x30
358584cb82SDonggeun Kim 
368584cb82SDonggeun Kim #define MAX8997_LED_BOOST_ENABLE_MASK		(1 << 6)
378584cb82SDonggeun Kim 
388584cb82SDonggeun Kim struct max8997_led {
398584cb82SDonggeun Kim 	struct max8997_dev *iodev;
408584cb82SDonggeun Kim 	struct led_classdev cdev;
418584cb82SDonggeun Kim 	bool enabled;
428584cb82SDonggeun Kim 	int id;
438584cb82SDonggeun Kim 	enum max8997_led_mode led_mode;
448584cb82SDonggeun Kim 	struct mutex mutex;
458584cb82SDonggeun Kim };
468584cb82SDonggeun Kim 
max8997_led_set_mode(struct max8997_led * led,enum max8997_led_mode mode)478584cb82SDonggeun Kim static void max8997_led_set_mode(struct max8997_led *led,
488584cb82SDonggeun Kim 			enum max8997_led_mode mode)
498584cb82SDonggeun Kim {
508584cb82SDonggeun Kim 	int ret;
518584cb82SDonggeun Kim 	struct i2c_client *client = led->iodev->i2c;
52eb18618bSAxel Lin 	u8 mask = 0, val;
538584cb82SDonggeun Kim 
548584cb82SDonggeun Kim 	switch (mode) {
558584cb82SDonggeun Kim 	case MAX8997_FLASH_MODE:
56eb18618bSAxel Lin 		mask = MAX8997_LED1_FLASH_MASK | MAX8997_LED0_FLASH_MASK;
57eb18618bSAxel Lin 		val = led->id ?
588584cb82SDonggeun Kim 		      MAX8997_LED1_FLASH_MASK : MAX8997_LED0_FLASH_MASK;
598584cb82SDonggeun Kim 		led->cdev.max_brightness = MAX8997_LED_FLASH_MAX_BRIGHTNESS;
608584cb82SDonggeun Kim 		break;
618584cb82SDonggeun Kim 	case MAX8997_MOVIE_MODE:
62eb18618bSAxel Lin 		mask = MAX8997_LED1_MOVIE_MASK | MAX8997_LED0_MOVIE_MASK;
63eb18618bSAxel Lin 		val = led->id ?
648584cb82SDonggeun Kim 		      MAX8997_LED1_MOVIE_MASK : MAX8997_LED0_MOVIE_MASK;
658584cb82SDonggeun Kim 		led->cdev.max_brightness = MAX8997_LED_MOVIE_MAX_BRIGHTNESS;
668584cb82SDonggeun Kim 		break;
678584cb82SDonggeun Kim 	case MAX8997_FLASH_PIN_CONTROL_MODE:
68eb18618bSAxel Lin 		mask = MAX8997_LED1_FLASH_PIN_MASK |
69eb18618bSAxel Lin 		       MAX8997_LED0_FLASH_PIN_MASK;
70eb18618bSAxel Lin 		val = led->id ?
718584cb82SDonggeun Kim 		      MAX8997_LED1_FLASH_PIN_MASK : MAX8997_LED0_FLASH_PIN_MASK;
728584cb82SDonggeun Kim 		led->cdev.max_brightness = MAX8997_LED_FLASH_MAX_BRIGHTNESS;
738584cb82SDonggeun Kim 		break;
748584cb82SDonggeun Kim 	case MAX8997_MOVIE_PIN_CONTROL_MODE:
75eb18618bSAxel Lin 		mask = MAX8997_LED1_MOVIE_PIN_MASK |
76eb18618bSAxel Lin 		       MAX8997_LED0_MOVIE_PIN_MASK;
77eb18618bSAxel Lin 		val = led->id ?
788584cb82SDonggeun Kim 		      MAX8997_LED1_MOVIE_PIN_MASK : MAX8997_LED0_MOVIE_PIN_MASK;
798584cb82SDonggeun Kim 		led->cdev.max_brightness = MAX8997_LED_MOVIE_MAX_BRIGHTNESS;
808584cb82SDonggeun Kim 		break;
818584cb82SDonggeun Kim 	default:
828584cb82SDonggeun Kim 		led->cdev.max_brightness = MAX8997_LED_NONE_MAX_BRIGHTNESS;
838584cb82SDonggeun Kim 		break;
848584cb82SDonggeun Kim 	}
858584cb82SDonggeun Kim 
868584cb82SDonggeun Kim 	if (mask) {
87eb18618bSAxel Lin 		ret = max8997_update_reg(client, MAX8997_REG_LEN_CNTL, val,
88eb18618bSAxel Lin 					 mask);
898584cb82SDonggeun Kim 		if (ret)
908584cb82SDonggeun Kim 			dev_err(led->iodev->dev,
918584cb82SDonggeun Kim 				"failed to update register(%d)\n", ret);
928584cb82SDonggeun Kim 	}
938584cb82SDonggeun Kim 
948584cb82SDonggeun Kim 	led->led_mode = mode;
958584cb82SDonggeun Kim }
968584cb82SDonggeun Kim 
max8997_led_enable(struct max8997_led * led,bool enable)978584cb82SDonggeun Kim static void max8997_led_enable(struct max8997_led *led, bool enable)
988584cb82SDonggeun Kim {
998584cb82SDonggeun Kim 	int ret;
1008584cb82SDonggeun Kim 	struct i2c_client *client = led->iodev->i2c;
1018584cb82SDonggeun Kim 	u8 val = 0, mask = MAX8997_LED_BOOST_ENABLE_MASK;
1028584cb82SDonggeun Kim 
1038584cb82SDonggeun Kim 	if (led->enabled == enable)
1048584cb82SDonggeun Kim 		return;
1058584cb82SDonggeun Kim 
1068584cb82SDonggeun Kim 	val = enable ? MAX8997_LED_BOOST_ENABLE_MASK : 0;
1078584cb82SDonggeun Kim 
1088584cb82SDonggeun Kim 	ret = max8997_update_reg(client, MAX8997_REG_BOOST_CNTL, val, mask);
1098584cb82SDonggeun Kim 	if (ret)
1108584cb82SDonggeun Kim 		dev_err(led->iodev->dev,
1118584cb82SDonggeun Kim 			"failed to update register(%d)\n", ret);
1128584cb82SDonggeun Kim 
1138584cb82SDonggeun Kim 	led->enabled = enable;
1148584cb82SDonggeun Kim }
1158584cb82SDonggeun Kim 
max8997_led_set_current(struct max8997_led * led,enum led_brightness value)1168584cb82SDonggeun Kim static void max8997_led_set_current(struct max8997_led *led,
1178584cb82SDonggeun Kim 				enum led_brightness value)
1188584cb82SDonggeun Kim {
1198584cb82SDonggeun Kim 	int ret;
1208584cb82SDonggeun Kim 	struct i2c_client *client = led->iodev->i2c;
1218584cb82SDonggeun Kim 	u8 val = 0, mask = 0, reg = 0;
1228584cb82SDonggeun Kim 
1238584cb82SDonggeun Kim 	switch (led->led_mode) {
1248584cb82SDonggeun Kim 	case MAX8997_FLASH_MODE:
1258584cb82SDonggeun Kim 	case MAX8997_FLASH_PIN_CONTROL_MODE:
1268584cb82SDonggeun Kim 		val = value << MAX8997_LED_FLASH_SHIFT;
1278584cb82SDonggeun Kim 		mask = MAX8997_LED_FLASH_CUR_MASK;
1288584cb82SDonggeun Kim 		reg = led->id ? MAX8997_REG_FLASH2_CUR : MAX8997_REG_FLASH1_CUR;
1298584cb82SDonggeun Kim 		break;
1308584cb82SDonggeun Kim 	case MAX8997_MOVIE_MODE:
1318584cb82SDonggeun Kim 	case MAX8997_MOVIE_PIN_CONTROL_MODE:
1328584cb82SDonggeun Kim 		val = value << MAX8997_LED_MOVIE_SHIFT;
1338584cb82SDonggeun Kim 		mask = MAX8997_LED_MOVIE_CUR_MASK;
1348584cb82SDonggeun Kim 		reg = MAX8997_REG_MOVIE_CUR;
1358584cb82SDonggeun Kim 		break;
1368584cb82SDonggeun Kim 	default:
1378584cb82SDonggeun Kim 		break;
1388584cb82SDonggeun Kim 	}
1398584cb82SDonggeun Kim 
1408584cb82SDonggeun Kim 	if (mask) {
1418584cb82SDonggeun Kim 		ret = max8997_update_reg(client, reg, val, mask);
1428584cb82SDonggeun Kim 		if (ret)
1438584cb82SDonggeun Kim 			dev_err(led->iodev->dev,
1448584cb82SDonggeun Kim 				"failed to update register(%d)\n", ret);
1458584cb82SDonggeun Kim 	}
1468584cb82SDonggeun Kim }
1478584cb82SDonggeun Kim 
max8997_led_brightness_set(struct led_classdev * led_cdev,enum led_brightness value)1488584cb82SDonggeun Kim static void max8997_led_brightness_set(struct led_classdev *led_cdev,
1498584cb82SDonggeun Kim 				enum led_brightness value)
1508584cb82SDonggeun Kim {
1518584cb82SDonggeun Kim 	struct max8997_led *led =
1528584cb82SDonggeun Kim 			container_of(led_cdev, struct max8997_led, cdev);
1538584cb82SDonggeun Kim 
1548584cb82SDonggeun Kim 	if (value) {
1558584cb82SDonggeun Kim 		max8997_led_set_current(led, value);
1568584cb82SDonggeun Kim 		max8997_led_enable(led, true);
1578584cb82SDonggeun Kim 	} else {
1588584cb82SDonggeun Kim 		max8997_led_set_current(led, value);
1598584cb82SDonggeun Kim 		max8997_led_enable(led, false);
1608584cb82SDonggeun Kim 	}
1618584cb82SDonggeun Kim }
1628584cb82SDonggeun Kim 
mode_show(struct device * dev,struct device_attribute * attr,char * buf)1635ccfa39dSDwaipayan Ray static ssize_t mode_show(struct device *dev,
1648584cb82SDonggeun Kim 			 struct device_attribute *attr, char *buf)
1658584cb82SDonggeun Kim {
1668584cb82SDonggeun Kim 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
1678584cb82SDonggeun Kim 	struct max8997_led *led =
1688584cb82SDonggeun Kim 			container_of(led_cdev, struct max8997_led, cdev);
1698584cb82SDonggeun Kim 	ssize_t ret = 0;
1708584cb82SDonggeun Kim 
1718584cb82SDonggeun Kim 	mutex_lock(&led->mutex);
1728584cb82SDonggeun Kim 
1738584cb82SDonggeun Kim 	switch (led->led_mode) {
1748584cb82SDonggeun Kim 	case MAX8997_FLASH_MODE:
1758584cb82SDonggeun Kim 		ret += sprintf(buf, "FLASH\n");
1768584cb82SDonggeun Kim 		break;
1778584cb82SDonggeun Kim 	case MAX8997_MOVIE_MODE:
1788584cb82SDonggeun Kim 		ret += sprintf(buf, "MOVIE\n");
1798584cb82SDonggeun Kim 		break;
1808584cb82SDonggeun Kim 	case MAX8997_FLASH_PIN_CONTROL_MODE:
1818584cb82SDonggeun Kim 		ret += sprintf(buf, "FLASH_PIN_CONTROL\n");
1828584cb82SDonggeun Kim 		break;
1838584cb82SDonggeun Kim 	case MAX8997_MOVIE_PIN_CONTROL_MODE:
1848584cb82SDonggeun Kim 		ret += sprintf(buf, "MOVIE_PIN_CONTROL\n");
1858584cb82SDonggeun Kim 		break;
1868584cb82SDonggeun Kim 	default:
1878584cb82SDonggeun Kim 		ret += sprintf(buf, "NONE\n");
1888584cb82SDonggeun Kim 		break;
1898584cb82SDonggeun Kim 	}
1908584cb82SDonggeun Kim 
1918584cb82SDonggeun Kim 	mutex_unlock(&led->mutex);
1928584cb82SDonggeun Kim 
1938584cb82SDonggeun Kim 	return ret;
1948584cb82SDonggeun Kim }
1958584cb82SDonggeun Kim 
mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1965ccfa39dSDwaipayan Ray static ssize_t mode_store(struct device *dev,
1978584cb82SDonggeun Kim 			  struct device_attribute *attr,
1988584cb82SDonggeun Kim 			  const char *buf, size_t size)
1998584cb82SDonggeun Kim {
2008584cb82SDonggeun Kim 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
2018584cb82SDonggeun Kim 	struct max8997_led *led =
2028584cb82SDonggeun Kim 			container_of(led_cdev, struct max8997_led, cdev);
2038584cb82SDonggeun Kim 	enum max8997_led_mode mode;
2048584cb82SDonggeun Kim 
2058584cb82SDonggeun Kim 	mutex_lock(&led->mutex);
2068584cb82SDonggeun Kim 
2078584cb82SDonggeun Kim 	if (!strncmp(buf, "FLASH_PIN_CONTROL", 17))
2088584cb82SDonggeun Kim 		mode = MAX8997_FLASH_PIN_CONTROL_MODE;
2098584cb82SDonggeun Kim 	else if (!strncmp(buf, "MOVIE_PIN_CONTROL", 17))
2108584cb82SDonggeun Kim 		mode = MAX8997_MOVIE_PIN_CONTROL_MODE;
2118584cb82SDonggeun Kim 	else if (!strncmp(buf, "FLASH", 5))
2128584cb82SDonggeun Kim 		mode = MAX8997_FLASH_MODE;
2138584cb82SDonggeun Kim 	else if (!strncmp(buf, "MOVIE", 5))
2148584cb82SDonggeun Kim 		mode = MAX8997_MOVIE_MODE;
2158584cb82SDonggeun Kim 	else
2168584cb82SDonggeun Kim 		mode = MAX8997_NONE;
2178584cb82SDonggeun Kim 
2188584cb82SDonggeun Kim 	max8997_led_set_mode(led, mode);
2198584cb82SDonggeun Kim 
2208584cb82SDonggeun Kim 	mutex_unlock(&led->mutex);
2218584cb82SDonggeun Kim 
2228584cb82SDonggeun Kim 	return size;
2238584cb82SDonggeun Kim }
2248584cb82SDonggeun Kim 
2255ccfa39dSDwaipayan Ray static DEVICE_ATTR_RW(mode);
2268584cb82SDonggeun Kim 
22735c16499SJohan Hovold static struct attribute *max8997_attrs[] = {
22835c16499SJohan Hovold 	&dev_attr_mode.attr,
22935c16499SJohan Hovold 	NULL
23035c16499SJohan Hovold };
23135c16499SJohan Hovold ATTRIBUTE_GROUPS(max8997);
23235c16499SJohan Hovold 
max8997_led_probe(struct platform_device * pdev)23398ea1ea2SBill Pemberton static int max8997_led_probe(struct platform_device *pdev)
2348584cb82SDonggeun Kim {
2358584cb82SDonggeun Kim 	struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
2368584cb82SDonggeun Kim 	struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
2378584cb82SDonggeun Kim 	struct max8997_led *led;
2388584cb82SDonggeun Kim 	char name[20];
2398584cb82SDonggeun Kim 	int ret = 0;
2408584cb82SDonggeun Kim 
2417f13bbf7SSachin Kamat 	led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
2427f13bbf7SSachin Kamat 	if (led == NULL)
2437f13bbf7SSachin Kamat 		return -ENOMEM;
2448584cb82SDonggeun Kim 
2458584cb82SDonggeun Kim 	led->id = pdev->id;
2468584cb82SDonggeun Kim 	snprintf(name, sizeof(name), "max8997-led%d", pdev->id);
2478584cb82SDonggeun Kim 
2488584cb82SDonggeun Kim 	led->cdev.name = name;
2498584cb82SDonggeun Kim 	led->cdev.brightness_set = max8997_led_brightness_set;
2508584cb82SDonggeun Kim 	led->cdev.flags |= LED_CORE_SUSPENDRESUME;
2518584cb82SDonggeun Kim 	led->cdev.brightness = 0;
25235c16499SJohan Hovold 	led->cdev.groups = max8997_groups;
2538584cb82SDonggeun Kim 	led->iodev = iodev;
2548584cb82SDonggeun Kim 
2558584cb82SDonggeun Kim 	/* initialize mode and brightness according to platform_data */
256*57d91e03SPaul Cercueil 	if (pdata && pdata->led_pdata) {
2578584cb82SDonggeun Kim 		u8 mode = 0, brightness = 0;
2588584cb82SDonggeun Kim 
2598584cb82SDonggeun Kim 		mode = pdata->led_pdata->mode[led->id];
2608584cb82SDonggeun Kim 		brightness = pdata->led_pdata->brightness[led->id];
2618584cb82SDonggeun Kim 
2627a5de56dSColin Ian King 		max8997_led_set_mode(led, mode);
2638584cb82SDonggeun Kim 
2648584cb82SDonggeun Kim 		if (brightness > led->cdev.max_brightness)
2658584cb82SDonggeun Kim 			brightness = led->cdev.max_brightness;
2668584cb82SDonggeun Kim 		max8997_led_set_current(led, brightness);
2678584cb82SDonggeun Kim 		led->cdev.brightness = brightness;
2688584cb82SDonggeun Kim 	} else {
2698584cb82SDonggeun Kim 		max8997_led_set_mode(led, MAX8997_NONE);
2708584cb82SDonggeun Kim 		max8997_led_set_current(led, 0);
2718584cb82SDonggeun Kim 	}
2728584cb82SDonggeun Kim 
2738584cb82SDonggeun Kim 	mutex_init(&led->mutex);
2748584cb82SDonggeun Kim 
2751c430f90SAmitoj Kaur Chawla 	ret = devm_led_classdev_register(&pdev->dev, &led->cdev);
2768584cb82SDonggeun Kim 	if (ret < 0)
2777f13bbf7SSachin Kamat 		return ret;
2788584cb82SDonggeun Kim 
2798584cb82SDonggeun Kim 	return 0;
2808584cb82SDonggeun Kim }
2818584cb82SDonggeun Kim 
2828584cb82SDonggeun Kim static struct platform_driver max8997_led_driver = {
2838584cb82SDonggeun Kim 	.driver = {
2848584cb82SDonggeun Kim 		.name  = "max8997-led",
2858584cb82SDonggeun Kim 	},
2868584cb82SDonggeun Kim 	.probe  = max8997_led_probe,
2878584cb82SDonggeun Kim };
2888584cb82SDonggeun Kim 
2897fafefb8SSachin Kamat module_platform_driver(max8997_led_driver);
2908584cb82SDonggeun Kim 
2918584cb82SDonggeun Kim MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
2928584cb82SDonggeun Kim MODULE_DESCRIPTION("MAX8997 LED driver");
2938584cb82SDonggeun Kim MODULE_LICENSE("GPL");
2948584cb82SDonggeun Kim MODULE_ALIAS("platform:max8997-led");
295