Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * LEDs driver for Dialog Semiconductor DA9030/DA9034 |
| 3 | * |
| 4 | * Copyright (C) 2008 Compulab, Ltd. |
Sachin Kamat | 141bfee | 2012-11-25 11:16:28 +0530 | [diff] [blame] | 5 | * Mike Rapoport <mike@compulab.co.il> |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 2006-2008 Marvell International Ltd. |
Sachin Kamat | 141bfee | 2012-11-25 11:16:28 +0530 | [diff] [blame] | 8 | * Eric Miao <eric.miao@marvell.com> |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/leds.h> |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 19 | #include <linux/mfd/da903x.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 21 | |
| 22 | #define DA9030_LED1_CONTROL 0x20 |
| 23 | #define DA9030_LED2_CONTROL 0x21 |
| 24 | #define DA9030_LED3_CONTROL 0x22 |
| 25 | #define DA9030_LED4_CONTROL 0x23 |
| 26 | #define DA9030_LEDPC_CONTROL 0x24 |
| 27 | #define DA9030_MISC_CONTROL_A 0x26 /* Vibrator Control */ |
| 28 | |
| 29 | #define DA9034_LED1_CONTROL 0x35 |
| 30 | #define DA9034_LED2_CONTROL 0x36 |
| 31 | #define DA9034_VIBRA 0x40 |
| 32 | |
| 33 | struct da903x_led { |
| 34 | struct led_classdev cdev; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 35 | struct device *master; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 36 | int id; |
| 37 | int flags; |
| 38 | }; |
| 39 | |
| 40 | #define DA9030_LED_OFFSET(id) ((id) - DA9030_ID_LED_1) |
| 41 | #define DA9034_LED_OFFSET(id) ((id) - DA9034_ID_LED_1) |
| 42 | |
Andrew Lunn | 803bb38 | 2015-08-20 12:43:49 +0200 | [diff] [blame] | 43 | static int da903x_led_set(struct led_classdev *led_cdev, |
| 44 | enum led_brightness value) |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 45 | { |
Andrew Lunn | 803bb38 | 2015-08-20 12:43:49 +0200 | [diff] [blame] | 46 | struct da903x_led *led = |
| 47 | container_of(led_cdev, struct da903x_led, cdev); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 48 | uint8_t val; |
Andrew Lunn | 803bb38 | 2015-08-20 12:43:49 +0200 | [diff] [blame] | 49 | int offset, ret = -EINVAL; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 50 | |
| 51 | switch (led->id) { |
| 52 | case DA9030_ID_LED_1: |
| 53 | case DA9030_ID_LED_2: |
| 54 | case DA9030_ID_LED_3: |
| 55 | case DA9030_ID_LED_4: |
| 56 | case DA9030_ID_LED_PC: |
| 57 | offset = DA9030_LED_OFFSET(led->id); |
| 58 | val = led->flags & ~0x87; |
Andrew Lunn | 803bb38 | 2015-08-20 12:43:49 +0200 | [diff] [blame] | 59 | val |= value ? 0x80 : 0; /* EN bit */ |
| 60 | val |= (0x7 - (value >> 5)) & 0x7; /* PWM<2:0> */ |
| 61 | ret = da903x_write(led->master, DA9030_LED1_CONTROL + offset, |
| 62 | val); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 63 | break; |
| 64 | case DA9030_ID_VIBRA: |
| 65 | val = led->flags & ~0x80; |
Andrew Lunn | 803bb38 | 2015-08-20 12:43:49 +0200 | [diff] [blame] | 66 | val |= value ? 0x80 : 0; /* EN bit */ |
| 67 | ret = da903x_write(led->master, DA9030_MISC_CONTROL_A, val); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 68 | break; |
| 69 | case DA9034_ID_LED_1: |
| 70 | case DA9034_ID_LED_2: |
| 71 | offset = DA9034_LED_OFFSET(led->id); |
Andrew Lunn | 803bb38 | 2015-08-20 12:43:49 +0200 | [diff] [blame] | 72 | val = (value * 0x5f / LED_FULL) & 0x7f; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 73 | val |= (led->flags & DA9034_LED_RAMP) ? 0x80 : 0; |
Andrew Lunn | 803bb38 | 2015-08-20 12:43:49 +0200 | [diff] [blame] | 74 | ret = da903x_write(led->master, DA9034_LED1_CONTROL + offset, |
| 75 | val); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 76 | break; |
| 77 | case DA9034_ID_VIBRA: |
Andrew Lunn | 803bb38 | 2015-08-20 12:43:49 +0200 | [diff] [blame] | 78 | val = value & 0xfe; |
| 79 | ret = da903x_write(led->master, DA9034_VIBRA, val); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 80 | break; |
| 81 | } |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 82 | |
Andrew Lunn | 803bb38 | 2015-08-20 12:43:49 +0200 | [diff] [blame] | 83 | return ret; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 86 | static int da903x_led_probe(struct platform_device *pdev) |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 87 | { |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 88 | struct led_info *pdata = dev_get_platdata(&pdev->dev); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 89 | struct da903x_led *led; |
| 90 | int id, ret; |
| 91 | |
| 92 | if (pdata == NULL) |
| 93 | return 0; |
| 94 | |
| 95 | id = pdev->id; |
| 96 | |
| 97 | if (!((id >= DA9030_ID_LED_1 && id <= DA9030_ID_VIBRA) || |
| 98 | (id >= DA9034_ID_LED_1 && id <= DA9034_ID_VIBRA))) { |
| 99 | dev_err(&pdev->dev, "invalid LED ID (%d) specified\n", id); |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | |
Bryan Wu | bad1c89 | 2012-07-03 13:07:53 +0800 | [diff] [blame] | 103 | led = devm_kzalloc(&pdev->dev, sizeof(struct da903x_led), GFP_KERNEL); |
Xiubo Li | 0c9a03b | 2014-05-07 20:25:52 -0700 | [diff] [blame] | 104 | if (!led) |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 105 | return -ENOMEM; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 106 | |
| 107 | led->cdev.name = pdata->name; |
| 108 | led->cdev.default_trigger = pdata->default_trigger; |
Andrew Lunn | 803bb38 | 2015-08-20 12:43:49 +0200 | [diff] [blame] | 109 | led->cdev.brightness_set_blocking = da903x_led_set; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 110 | led->cdev.brightness = LED_OFF; |
| 111 | |
| 112 | led->id = id; |
| 113 | led->flags = pdata->flags; |
| 114 | led->master = pdev->dev.parent; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 115 | |
Johan Hovold | 5f968e6 | 2020-06-01 15:39:46 +0200 | [diff] [blame] | 116 | ret = led_classdev_register(led->master, &led->cdev); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 117 | if (ret) { |
| 118 | dev_err(&pdev->dev, "failed to register LED %d\n", id); |
Bryan Wu | bad1c89 | 2012-07-03 13:07:53 +0800 | [diff] [blame] | 119 | return ret; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Johan Hovold | 5f968e6 | 2020-06-01 15:39:46 +0200 | [diff] [blame] | 122 | platform_set_drvdata(pdev, led); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int da903x_led_remove(struct platform_device *pdev) |
| 128 | { |
| 129 | struct da903x_led *led = platform_get_drvdata(pdev); |
| 130 | |
| 131 | led_classdev_unregister(&led->cdev); |
| 132 | |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static struct platform_driver da903x_led_driver = { |
| 137 | .driver = { |
| 138 | .name = "da903x-led", |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 139 | }, |
| 140 | .probe = da903x_led_probe, |
Johan Hovold | 5f968e6 | 2020-06-01 15:39:46 +0200 | [diff] [blame] | 141 | .remove = da903x_led_remove, |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 142 | }; |
| 143 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 144 | module_platform_driver(da903x_led_driver); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 145 | |
| 146 | MODULE_DESCRIPTION("LEDs driver for Dialog Semiconductor DA9030/DA9034"); |
Sachin Kamat | 141bfee | 2012-11-25 11:16:28 +0530 | [diff] [blame] | 147 | MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"); |
| 148 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 149 | MODULE_LICENSE("GPL"); |
| 150 | MODULE_ALIAS("platform:da903x-led"); |