blob: a7ff510cbdd06e113ee5e744ea0c1c9eb8c4618e [file] [log] [blame]
Daniel Macka8dd18f2009-10-15 02:59:35 +02001/*
2 * LEDs driver for LT3593 controllers
3 *
4 * See the datasheet at http://cds.linear.com/docs/Datasheet/3593f.pdf
5 *
6 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
7 *
8 * Based on leds-gpio.c,
9 *
10 * Copyright (C) 2007 8D Technologies inc.
11 * Raphael Assenat <raph@8d.com>
12 * Copyright (C) 2008 Freescale Semiconductor, Inc.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19#include <linux/kernel.h>
Daniel Macka8dd18f2009-10-15 02:59:35 +020020#include <linux/platform_device.h>
21#include <linux/leds.h>
Daniel Macka8dd18f2009-10-15 02:59:35 +020022#include <linux/delay.h>
23#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040025#include <linux/module.h>
Daniel Macka8dd18f2009-10-15 02:59:35 +020026
27struct lt3593_led_data {
28 struct led_classdev cdev;
29 unsigned gpio;
Daniel Macka8dd18f2009-10-15 02:59:35 +020030};
31
Andrew Lunna0011f12015-08-20 12:50:05 +020032static int lt3593_led_set(struct led_classdev *led_cdev,
33 enum led_brightness value)
Daniel Macka8dd18f2009-10-15 02:59:35 +020034{
Daniel Macka8dd18f2009-10-15 02:59:35 +020035 struct lt3593_led_data *led_dat =
Andrew Lunna0011f12015-08-20 12:50:05 +020036 container_of(led_cdev, struct lt3593_led_data, cdev);
37 int pulses;
Daniel Macka8dd18f2009-10-15 02:59:35 +020038
39 /*
40 * The LT3593 resets its internal current level register to the maximum
41 * level on the first falling edge on the control pin. Each following
42 * falling edge decreases the current level by 625uA. Up to 32 pulses
43 * can be sent, so the maximum power reduction is 20mA.
44 * After a timeout of 128us, the value is taken from the register and
45 * applied is to the output driver.
46 */
47
Andrew Lunna0011f12015-08-20 12:50:05 +020048 if (value == 0) {
Daniel Macka8dd18f2009-10-15 02:59:35 +020049 gpio_set_value_cansleep(led_dat->gpio, 0);
Andrew Lunna0011f12015-08-20 12:50:05 +020050 return 0;
Daniel Macka8dd18f2009-10-15 02:59:35 +020051 }
52
Andrew Lunna0011f12015-08-20 12:50:05 +020053 pulses = 32 - (value * 32) / 255;
Daniel Macka8dd18f2009-10-15 02:59:35 +020054
55 if (pulses == 0) {
56 gpio_set_value_cansleep(led_dat->gpio, 0);
57 mdelay(1);
58 gpio_set_value_cansleep(led_dat->gpio, 1);
Andrew Lunna0011f12015-08-20 12:50:05 +020059 return 0;
Daniel Macka8dd18f2009-10-15 02:59:35 +020060 }
61
62 gpio_set_value_cansleep(led_dat->gpio, 1);
63
64 while (pulses--) {
65 gpio_set_value_cansleep(led_dat->gpio, 0);
66 udelay(1);
67 gpio_set_value_cansleep(led_dat->gpio, 1);
68 udelay(1);
69 }
Daniel Macka8dd18f2009-10-15 02:59:35 +020070
Andrew Lunna0011f12015-08-20 12:50:05 +020071 return 0;
Daniel Macka8dd18f2009-10-15 02:59:35 +020072}
73
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050074static int create_lt3593_led(const struct gpio_led *template,
Daniel Macka8dd18f2009-10-15 02:59:35 +020075 struct lt3593_led_data *led_dat, struct device *parent)
76{
77 int ret, state;
78
79 /* skip leds on GPIOs that aren't available */
80 if (!gpio_is_valid(template->gpio)) {
Sachin Kamat30e5c3e2012-11-25 12:10:27 +053081 dev_info(parent, "%s: skipping unavailable LT3593 LED at gpio %d (%s)\n",
Daniel Macka8dd18f2009-10-15 02:59:35 +020082 KBUILD_MODNAME, template->gpio, template->name);
83 return 0;
84 }
85
Daniel Macka8dd18f2009-10-15 02:59:35 +020086 led_dat->cdev.name = template->name;
87 led_dat->cdev.default_trigger = template->default_trigger;
88 led_dat->gpio = template->gpio;
89
Andrew Lunna0011f12015-08-20 12:50:05 +020090 led_dat->cdev.brightness_set_blocking = lt3593_led_set;
Daniel Macka8dd18f2009-10-15 02:59:35 +020091
92 state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
93 led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
94
95 if (!template->retain_state_suspended)
96 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
97
Jingoo Han84f69422013-03-07 18:37:38 -080098 ret = devm_gpio_request_one(parent, template->gpio, state ?
99 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
100 template->name);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200101 if (ret < 0)
Jingoo Han507d967bc2012-10-23 05:17:56 -0700102 return ret;
Daniel Macka8dd18f2009-10-15 02:59:35 +0200103
Daniel Macka8dd18f2009-10-15 02:59:35 +0200104 ret = led_classdev_register(parent, &led_dat->cdev);
105 if (ret < 0)
Sachin Kamat95a8ff82012-11-25 10:14:27 +0530106 return ret;
Daniel Macka8dd18f2009-10-15 02:59:35 +0200107
Sachin Kamat30e5c3e2012-11-25 12:10:27 +0530108 dev_info(parent, "%s: registered LT3593 LED '%s' at GPIO %d\n",
Daniel Macka8dd18f2009-10-15 02:59:35 +0200109 KBUILD_MODNAME, template->name, template->gpio);
110
111 return 0;
Daniel Macka8dd18f2009-10-15 02:59:35 +0200112}
113
114static void delete_lt3593_led(struct lt3593_led_data *led)
115{
116 if (!gpio_is_valid(led->gpio))
117 return;
118
119 led_classdev_unregister(&led->cdev);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200120}
121
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500122static int lt3593_led_probe(struct platform_device *pdev)
Daniel Macka8dd18f2009-10-15 02:59:35 +0200123{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700124 struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200125 struct lt3593_led_data *leds_data;
126 int i, ret = 0;
127
128 if (!pdata)
129 return -EBUSY;
130
Bryan Wu0a8d9d42012-07-04 11:51:51 +0800131 leds_data = devm_kzalloc(&pdev->dev,
132 sizeof(struct lt3593_led_data) * pdata->num_leds,
133 GFP_KERNEL);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200134 if (!leds_data)
135 return -ENOMEM;
136
137 for (i = 0; i < pdata->num_leds; i++) {
138 ret = create_lt3593_led(&pdata->leds[i], &leds_data[i],
139 &pdev->dev);
140 if (ret < 0)
141 goto err;
142 }
143
144 platform_set_drvdata(pdev, leds_data);
145
146 return 0;
147
148err:
149 for (i = i - 1; i >= 0; i--)
150 delete_lt3593_led(&leds_data[i]);
151
Daniel Macka8dd18f2009-10-15 02:59:35 +0200152 return ret;
153}
154
Bill Pemberton678e8a62012-11-19 13:26:00 -0500155static int lt3593_led_remove(struct platform_device *pdev)
Daniel Macka8dd18f2009-10-15 02:59:35 +0200156{
157 int i;
Jingoo Han87aae1e2013-07-30 01:07:35 -0700158 struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200159 struct lt3593_led_data *leds_data;
160
161 leds_data = platform_get_drvdata(pdev);
162
163 for (i = 0; i < pdata->num_leds; i++)
164 delete_lt3593_led(&leds_data[i]);
165
Daniel Macka8dd18f2009-10-15 02:59:35 +0200166 return 0;
167}
168
169static struct platform_driver lt3593_led_driver = {
170 .probe = lt3593_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500171 .remove = lt3593_led_remove,
Daniel Macka8dd18f2009-10-15 02:59:35 +0200172 .driver = {
173 .name = "leds-lt3593",
Daniel Macka8dd18f2009-10-15 02:59:35 +0200174 },
175};
176
Axel Lin892a8842012-01-10 15:09:24 -0800177module_platform_driver(lt3593_led_driver);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200178
179MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
180MODULE_DESCRIPTION("LED driver for LT3593 controllers");
181MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800182MODULE_ALIAS("platform:leds-lt3593");