leds-asic3.c 4.5 KB
Newer Older
P
Paul Parsons 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 *  Copyright (C) 2011 Paul Parsons <lost.distance@yahoo.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/slab.h>

#include <linux/mfd/asic3.h>
#include <linux/mfd/core.h>
16
#include <linux/module.h>
P
Paul Parsons 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

/*
 *	The HTC ASIC3 LED GPIOs are inputs, not outputs.
 *	Hence we turn the LEDs on/off via the TimeBase register.
 */

/*
 *	When TimeBase is 4 the clock resolution is about 32Hz.
 *	This driver supports hardware blinking with an on+off
 *	period from 62ms (2 clocks) to 125s (4000 clocks).
 */
#define MS_TO_CLK(ms)	DIV_ROUND_CLOSEST(((ms)*1024), 32000)
#define CLK_TO_MS(clk)	(((clk)*32000)/1024)
#define MAX_CLK		4000            /* Fits into 12-bit Time registers */
#define MAX_MS		CLK_TO_MS(MAX_CLK)

static const unsigned int led_n_base[ASIC3_NUM_LEDS] = {
	[0] = ASIC3_LED_0_Base,
	[1] = ASIC3_LED_1_Base,
	[2] = ASIC3_LED_2_Base,
};

static void brightness_set(struct led_classdev *cdev,
	enum led_brightness value)
{
	struct platform_device *pdev = to_platform_device(cdev->dev->parent);
	const struct mfd_cell *cell = mfd_get_cell(pdev);
	struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
	u32 timebase;
	unsigned int base;

	timebase = (value == LED_OFF) ? 0 : (LED_EN|0x4);

	base = led_n_base[cell->id];
	asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), 32);
	asic3_write_register(asic, (base + ASIC3_LED_DutyTime), 32);
	asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0);
	asic3_write_register(asic, (base + ASIC3_LED_TimeBase), timebase);
}

static int blink_set(struct led_classdev *cdev,
	unsigned long *delay_on,
	unsigned long *delay_off)
{
	struct platform_device *pdev = to_platform_device(cdev->dev->parent);
	const struct mfd_cell *cell = mfd_get_cell(pdev);
	struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
	u32 on;
	u32 off;
	unsigned int base;

	if (*delay_on > MAX_MS || *delay_off > MAX_MS)
		return -EINVAL;

	if (*delay_on == 0 && *delay_off == 0) {
		/* If both are zero then a sensible default should be chosen */
		on = MS_TO_CLK(500);
		off = MS_TO_CLK(500);
	} else {
		on = MS_TO_CLK(*delay_on);
		off = MS_TO_CLK(*delay_off);
		if ((on + off) > MAX_CLK)
			return -EINVAL;
	}

	base = led_n_base[cell->id];
	asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), (on + off));
	asic3_write_register(asic, (base + ASIC3_LED_DutyTime), on);
	asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0);
	asic3_write_register(asic, (base + ASIC3_LED_TimeBase), (LED_EN|0x4));

	*delay_on = CLK_TO_MS(on);
	*delay_off = CLK_TO_MS(off);

	return 0;
}

B
Bill Pemberton 已提交
94
static int asic3_led_probe(struct platform_device *pdev)
P
Paul Parsons 已提交
95
{
J
Jingoo Han 已提交
96
	struct asic3_led *led = dev_get_platdata(&pdev->dev);
P
Paul Parsons 已提交
97 98 99 100
	int ret;

	ret = mfd_cell_enable(pdev);
	if (ret < 0)
101
		return ret;
P
Paul Parsons 已提交
102

103 104
	led->cdev = devm_kzalloc(&pdev->dev, sizeof(struct led_classdev),
				GFP_KERNEL);
P
Paul Parsons 已提交
105 106
	if (!led->cdev) {
		ret = -ENOMEM;
107
		goto out;
P
Paul Parsons 已提交
108 109 110
	}

	led->cdev->name = led->name;
111
	led->cdev->flags = LED_CORE_SUSPENDRESUME;
P
Paul Parsons 已提交
112 113
	led->cdev->brightness_set = brightness_set;
	led->cdev->blink_set = blink_set;
114
	led->cdev->default_trigger = led->default_trigger;
P
Paul Parsons 已提交
115 116 117

	ret = led_classdev_register(&pdev->dev, led->cdev);
	if (ret < 0)
118
		goto out;
P
Paul Parsons 已提交
119 120 121

	return 0;

122
out:
P
Paul Parsons 已提交
123 124 125 126
	(void) mfd_cell_disable(pdev);
	return ret;
}

B
Bill Pemberton 已提交
127
static int asic3_led_remove(struct platform_device *pdev)
P
Paul Parsons 已提交
128
{
J
Jingoo Han 已提交
129
	struct asic3_led *led = dev_get_platdata(&pdev->dev);
P
Paul Parsons 已提交
130 131 132 133 134 135

	led_classdev_unregister(led->cdev);

	return mfd_cell_disable(pdev);
}

136
#ifdef CONFIG_PM_SLEEP
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
static int asic3_led_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	const struct mfd_cell *cell = mfd_get_cell(pdev);
	int ret;

	ret = 0;
	if (cell->suspend)
		ret = (*cell->suspend)(pdev);

	return ret;
}

static int asic3_led_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	const struct mfd_cell *cell = mfd_get_cell(pdev);
	int ret;

	ret = 0;
	if (cell->resume)
		ret = (*cell->resume)(pdev);

	return ret;
}
162
#endif
163

164
static SIMPLE_DEV_PM_OPS(asic3_led_pm_ops, asic3_led_suspend, asic3_led_resume);
165

P
Paul Parsons 已提交
166 167
static struct platform_driver asic3_led_driver = {
	.probe		= asic3_led_probe,
B
Bill Pemberton 已提交
168
	.remove		= asic3_led_remove,
P
Paul Parsons 已提交
169 170
	.driver		= {
		.name	= "leds-asic3",
171
		.pm	= &asic3_led_pm_ops,
P
Paul Parsons 已提交
172 173 174
	},
};

175
module_platform_driver(asic3_led_driver);
P
Paul Parsons 已提交
176 177 178 179

MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>");
MODULE_DESCRIPTION("HTC ASIC3 LED driver");
MODULE_LICENSE("GPL");
180
MODULE_ALIAS("platform:leds-asic3");