xref: /linux/drivers/leds/leds-da903x.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29e84561cSMike Rapoport /*
39e84561cSMike Rapoport  * LEDs driver for Dialog Semiconductor DA9030/DA9034
49e84561cSMike Rapoport  *
59e84561cSMike Rapoport  * Copyright (C) 2008 Compulab, Ltd.
69e84561cSMike Rapoport  *	Mike Rapoport <mike@compulab.co.il>
79e84561cSMike Rapoport  *
89e84561cSMike Rapoport  * Copyright (C) 2006-2008 Marvell International Ltd.
99e84561cSMike Rapoport  *	Eric Miao <eric.miao@marvell.com>
109e84561cSMike Rapoport  */
119e84561cSMike Rapoport 
129e84561cSMike Rapoport #include <linux/module.h>
139e84561cSMike Rapoport #include <linux/kernel.h>
149e84561cSMike Rapoport #include <linux/platform_device.h>
159e84561cSMike Rapoport #include <linux/leds.h>
169e84561cSMike Rapoport #include <linux/mfd/da903x.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
189e84561cSMike Rapoport 
199e84561cSMike Rapoport #define DA9030_LED1_CONTROL	0x20
209e84561cSMike Rapoport #define DA9030_LED2_CONTROL	0x21
219e84561cSMike Rapoport #define DA9030_LED3_CONTROL	0x22
229e84561cSMike Rapoport #define DA9030_LED4_CONTROL	0x23
239e84561cSMike Rapoport #define DA9030_LEDPC_CONTROL	0x24
249e84561cSMike Rapoport #define DA9030_MISC_CONTROL_A	0x26	/* Vibrator Control */
259e84561cSMike Rapoport 
269e84561cSMike Rapoport #define DA9034_LED1_CONTROL	0x35
279e84561cSMike Rapoport #define DA9034_LED2_CONTROL	0x36
289e84561cSMike Rapoport #define DA9034_VIBRA		0x40
299e84561cSMike Rapoport 
309e84561cSMike Rapoport struct da903x_led {
319e84561cSMike Rapoport 	struct led_classdev	cdev;
329e84561cSMike Rapoport 	struct device		*master;
339e84561cSMike Rapoport 	int			id;
349e84561cSMike Rapoport 	int			flags;
359e84561cSMike Rapoport };
369e84561cSMike Rapoport 
379e84561cSMike Rapoport #define DA9030_LED_OFFSET(id)	((id) - DA9030_ID_LED_1)
389e84561cSMike Rapoport #define DA9034_LED_OFFSET(id)	((id) - DA9034_ID_LED_1)
399e84561cSMike Rapoport 
40803bb384SAndrew Lunn static int da903x_led_set(struct led_classdev *led_cdev,
41803bb384SAndrew Lunn 			   enum led_brightness value)
429e84561cSMike Rapoport {
43803bb384SAndrew Lunn 	struct da903x_led *led =
44803bb384SAndrew Lunn 			container_of(led_cdev, struct da903x_led, cdev);
459e84561cSMike Rapoport 	uint8_t val;
46803bb384SAndrew Lunn 	int offset, ret = -EINVAL;
479e84561cSMike Rapoport 
489e84561cSMike Rapoport 	switch (led->id) {
499e84561cSMike Rapoport 	case DA9030_ID_LED_1:
509e84561cSMike Rapoport 	case DA9030_ID_LED_2:
519e84561cSMike Rapoport 	case DA9030_ID_LED_3:
529e84561cSMike Rapoport 	case DA9030_ID_LED_4:
539e84561cSMike Rapoport 	case DA9030_ID_LED_PC:
549e84561cSMike Rapoport 		offset = DA9030_LED_OFFSET(led->id);
559e84561cSMike Rapoport 		val = led->flags & ~0x87;
56803bb384SAndrew Lunn 		val |= value ? 0x80 : 0; /* EN bit */
57803bb384SAndrew Lunn 		val |= (0x7 - (value >> 5)) & 0x7; /* PWM<2:0> */
58803bb384SAndrew Lunn 		ret = da903x_write(led->master, DA9030_LED1_CONTROL + offset,
59803bb384SAndrew Lunn 				   val);
609e84561cSMike Rapoport 		break;
619e84561cSMike Rapoport 	case DA9030_ID_VIBRA:
629e84561cSMike Rapoport 		val = led->flags & ~0x80;
63803bb384SAndrew Lunn 		val |= value ? 0x80 : 0; /* EN bit */
64803bb384SAndrew Lunn 		ret = da903x_write(led->master, DA9030_MISC_CONTROL_A, val);
659e84561cSMike Rapoport 		break;
669e84561cSMike Rapoport 	case DA9034_ID_LED_1:
679e84561cSMike Rapoport 	case DA9034_ID_LED_2:
689e84561cSMike Rapoport 		offset = DA9034_LED_OFFSET(led->id);
69803bb384SAndrew Lunn 		val = (value * 0x5f / LED_FULL) & 0x7f;
709e84561cSMike Rapoport 		val |= (led->flags & DA9034_LED_RAMP) ? 0x80 : 0;
71803bb384SAndrew Lunn 		ret = da903x_write(led->master, DA9034_LED1_CONTROL + offset,
72803bb384SAndrew Lunn 				   val);
739e84561cSMike Rapoport 		break;
749e84561cSMike Rapoport 	case DA9034_ID_VIBRA:
75803bb384SAndrew Lunn 		val = value & 0xfe;
76803bb384SAndrew Lunn 		ret = da903x_write(led->master, DA9034_VIBRA, val);
779e84561cSMike Rapoport 		break;
789e84561cSMike Rapoport 	}
799e84561cSMike Rapoport 
80803bb384SAndrew Lunn 	return ret;
819e84561cSMike Rapoport }
829e84561cSMike Rapoport 
8398ea1ea2SBill Pemberton static int da903x_led_probe(struct platform_device *pdev)
849e84561cSMike Rapoport {
8587aae1eaSJingoo Han 	struct led_info *pdata = dev_get_platdata(&pdev->dev);
869e84561cSMike Rapoport 	struct da903x_led *led;
879e84561cSMike Rapoport 	int id, ret;
889e84561cSMike Rapoport 
899e84561cSMike Rapoport 	if (pdata == NULL)
909e84561cSMike Rapoport 		return 0;
919e84561cSMike Rapoport 
929e84561cSMike Rapoport 	id = pdev->id;
939e84561cSMike Rapoport 
949e84561cSMike Rapoport 	if (!((id >= DA9030_ID_LED_1 && id <= DA9030_ID_VIBRA) ||
959e84561cSMike Rapoport 	      (id >= DA9034_ID_LED_1 && id <= DA9034_ID_VIBRA))) {
969e84561cSMike Rapoport 		dev_err(&pdev->dev, "invalid LED ID (%d) specified\n", id);
979e84561cSMike Rapoport 		return -EINVAL;
989e84561cSMike Rapoport 	}
999e84561cSMike Rapoport 
100bad1c898SBryan Wu 	led = devm_kzalloc(&pdev->dev, sizeof(struct da903x_led), GFP_KERNEL);
1010c9a03b6SXiubo Li 	if (!led)
1029e84561cSMike Rapoport 		return -ENOMEM;
1039e84561cSMike Rapoport 
1049e84561cSMike Rapoport 	led->cdev.name = pdata->name;
1059e84561cSMike Rapoport 	led->cdev.default_trigger = pdata->default_trigger;
106803bb384SAndrew Lunn 	led->cdev.brightness_set_blocking = da903x_led_set;
1079e84561cSMike Rapoport 	led->cdev.brightness = LED_OFF;
1089e84561cSMike Rapoport 
1099e84561cSMike Rapoport 	led->id = id;
1109e84561cSMike Rapoport 	led->flags = pdata->flags;
1119e84561cSMike Rapoport 	led->master = pdev->dev.parent;
1129e84561cSMike Rapoport 
113eed16255SAmitoj Kaur Chawla 	ret = devm_led_classdev_register(led->master, &led->cdev);
1149e84561cSMike Rapoport 	if (ret) {
1159e84561cSMike Rapoport 		dev_err(&pdev->dev, "failed to register LED %d\n", id);
116bad1c898SBryan Wu 		return ret;
1179e84561cSMike Rapoport 	}
1189e84561cSMike Rapoport 
1199e84561cSMike Rapoport 	return 0;
1209e84561cSMike Rapoport }
1219e84561cSMike Rapoport 
1229e84561cSMike Rapoport static struct platform_driver da903x_led_driver = {
1239e84561cSMike Rapoport 	.driver	= {
1249e84561cSMike Rapoport 		.name	= "da903x-led",
1259e84561cSMike Rapoport 	},
1269e84561cSMike Rapoport 	.probe		= da903x_led_probe,
1279e84561cSMike Rapoport };
1289e84561cSMike Rapoport 
129892a8843SAxel Lin module_platform_driver(da903x_led_driver);
1309e84561cSMike Rapoport 
1319e84561cSMike Rapoport MODULE_DESCRIPTION("LEDs driver for Dialog Semiconductor DA9030/DA9034");
132141bfeeaSSachin Kamat MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
133141bfeeaSSachin Kamat MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
1349e84561cSMike Rapoport MODULE_LICENSE("GPL");
1359e84561cSMike Rapoport MODULE_ALIAS("platform:da903x-led");
136