leds-renesas-tpu.c 8.8 KB
Newer Older
M
Magnus Damm 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * LED control using Renesas TPU
 *
 *  Copyright (C) 2011 Magnus Damm
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>
#include <linux/printk.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include <linux/clk.h>
#include <linux/leds.h>
29
#include <linux/platform_data/leds-renesas-tpu.h>
M
Magnus Damm 已提交
30 31 32 33
#include <linux/gpio.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/pm_runtime.h>
34
#include <linux/workqueue.h>
M
Magnus Damm 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47

enum r_tpu_pin { R_TPU_PIN_UNUSED, R_TPU_PIN_GPIO, R_TPU_PIN_GPIO_FN };
enum r_tpu_timer { R_TPU_TIMER_UNUSED, R_TPU_TIMER_ON };

struct r_tpu_priv {
	struct led_classdev ldev;
	void __iomem *mapbase;
	struct clk *clk;
	struct platform_device *pdev;
	enum r_tpu_pin pin_state;
	enum r_tpu_timer timer_state;
	unsigned long min_rate;
	unsigned int refresh_rate;
48 49
	struct work_struct work;
	enum led_brightness new_brightness;
M
Magnus Damm 已提交
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
};

static DEFINE_SPINLOCK(r_tpu_lock);

#define TSTR -1 /* Timer start register (shared register) */
#define TCR  0 /* Timer control register (+0x00) */
#define TMDR 1 /* Timer mode register (+0x04) */
#define TIOR 2 /* Timer I/O control register (+0x08) */
#define TIER 3 /* Timer interrupt enable register (+0x0c) */
#define TSR  4 /* Timer status register (+0x10) */
#define TCNT 5 /* Timer counter (+0x14) */
#define TGRA 6 /* Timer general register A (+0x18) */
#define TGRB 7 /* Timer general register B (+0x1c) */
#define TGRC 8 /* Timer general register C (+0x20) */
#define TGRD 9 /* Timer general register D (+0x24) */

static inline unsigned short r_tpu_read(struct r_tpu_priv *p, int reg_nr)
{
	struct led_renesas_tpu_config *cfg = p->pdev->dev.platform_data;
	void __iomem *base = p->mapbase;
	unsigned long offs = reg_nr << 2;

	if (reg_nr == TSTR)
		return ioread16(base - cfg->channel_offset);

	return ioread16(base + offs);
}

static inline void r_tpu_write(struct r_tpu_priv *p, int reg_nr,
			       unsigned short value)
{
	struct led_renesas_tpu_config *cfg = p->pdev->dev.platform_data;
	void __iomem *base = p->mapbase;
	unsigned long offs = reg_nr << 2;

	if (reg_nr == TSTR) {
		iowrite16(value, base - cfg->channel_offset);
		return;
	}

	iowrite16(value, base + offs);
}

static void r_tpu_start_stop_ch(struct r_tpu_priv *p, int start)
{
	struct led_renesas_tpu_config *cfg = p->pdev->dev.platform_data;
	unsigned long flags, value;

	/* start stop register shared by multiple timer channels */
	spin_lock_irqsave(&r_tpu_lock, flags);
	value = r_tpu_read(p, TSTR);

	if (start)
		value |= 1 << cfg->timer_bit;
	else
		value &= ~(1 << cfg->timer_bit);

	r_tpu_write(p, TSTR, value);
	spin_unlock_irqrestore(&r_tpu_lock, flags);
}

static int r_tpu_enable(struct r_tpu_priv *p, enum led_brightness brightness)
{
	struct led_renesas_tpu_config *cfg = p->pdev->dev.platform_data;
	int prescaler[] = { 1, 4, 16, 64 };
	int k, ret;
	unsigned long rate, tmp;

	if (p->timer_state == R_TPU_TIMER_ON)
		return 0;

	/* wake up device and enable clock */
	pm_runtime_get_sync(&p->pdev->dev);
	ret = clk_enable(p->clk);
	if (ret) {
		dev_err(&p->pdev->dev, "cannot enable clock\n");
		return ret;
	}

	/* make sure channel is disabled */
	r_tpu_start_stop_ch(p, 0);

	/* get clock rate after enabling it */
	rate = clk_get_rate(p->clk);

	/* pick the lowest acceptable rate */
136 137
	for (k = ARRAY_SIZE(prescaler) - 1; k >= 0; k--)
		if ((rate / prescaler[k]) >= p->min_rate)
M
Magnus Damm 已提交
138 139
			break;

140
	if (k < 0) {
M
Magnus Damm 已提交
141 142 143 144
		dev_err(&p->pdev->dev, "clock rate mismatch\n");
		goto err0;
	}
	dev_dbg(&p->pdev->dev, "rate = %lu, prescaler %u\n",
145
		rate, prescaler[k]);
M
Magnus Damm 已提交
146 147

	/* clear TCNT on TGRB match, count on rising edge, set prescaler */
148
	r_tpu_write(p, TCR, 0x0040 | k);
M
Magnus Damm 已提交
149 150 151 152

	/* output 0 until TGRA, output 1 until TGRB */
	r_tpu_write(p, TIOR, 0x0002);

153
	rate /= prescaler[k] * p->refresh_rate;
M
Magnus Damm 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	r_tpu_write(p, TGRB, rate);
	dev_dbg(&p->pdev->dev, "TRGB = 0x%04lx\n", rate);

	tmp = (cfg->max_brightness - brightness) * rate;
	r_tpu_write(p, TGRA, tmp / cfg->max_brightness);
	dev_dbg(&p->pdev->dev, "TRGA = 0x%04lx\n", tmp / cfg->max_brightness);

	/* PWM mode */
	r_tpu_write(p, TMDR, 0x0002);

	/* enable channel */
	r_tpu_start_stop_ch(p, 1);

	p->timer_state = R_TPU_TIMER_ON;
	return 0;
 err0:
	clk_disable(p->clk);
	pm_runtime_put_sync(&p->pdev->dev);
	return -ENOTSUPP;
}

static void r_tpu_disable(struct r_tpu_priv *p)
{
	if (p->timer_state == R_TPU_TIMER_UNUSED)
		return;

	/* disable channel */
	r_tpu_start_stop_ch(p, 0);

	/* stop clock and mark device as idle */
	clk_disable(p->clk);
	pm_runtime_put_sync(&p->pdev->dev);

	p->timer_state = R_TPU_TIMER_UNUSED;
}

static void r_tpu_set_pin(struct r_tpu_priv *p, enum r_tpu_pin new_state,
			  enum led_brightness brightness)
{
	struct led_renesas_tpu_config *cfg = p->pdev->dev.platform_data;

	if (p->pin_state == new_state) {
		if (p->pin_state == R_TPU_PIN_GPIO)
			gpio_set_value(cfg->pin_gpio, brightness);
		return;
	}

	if (p->pin_state == R_TPU_PIN_GPIO)
		gpio_free(cfg->pin_gpio);

	if (p->pin_state == R_TPU_PIN_GPIO_FN)
		gpio_free(cfg->pin_gpio_fn);

J
Jingoo Han 已提交
207
	if (new_state == R_TPU_PIN_GPIO)
208 209
		gpio_request_one(cfg->pin_gpio, !!brightness ?
				GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
J
Jingoo Han 已提交
210 211
				cfg->name);

M
Magnus Damm 已提交
212 213 214 215 216 217
	if (new_state == R_TPU_PIN_GPIO_FN)
		gpio_request(cfg->pin_gpio_fn, cfg->name);

	p->pin_state = new_state;
}

218
static void r_tpu_work(struct work_struct *work)
M
Magnus Damm 已提交
219
{
220 221
	struct r_tpu_priv *p = container_of(work, struct r_tpu_priv, work);
	enum led_brightness brightness = p->new_brightness;
M
Magnus Damm 已提交
222 223 224 225

	r_tpu_disable(p);

	/* off and maximum are handled as GPIO pins, in between PWM */
226
	if ((brightness == 0) || (brightness == p->ldev.max_brightness))
M
Magnus Damm 已提交
227 228 229 230 231 232 233
		r_tpu_set_pin(p, R_TPU_PIN_GPIO, brightness);
	else {
		r_tpu_set_pin(p, R_TPU_PIN_GPIO_FN, 0);
		r_tpu_enable(p, brightness);
	}
}

234 235 236 237 238 239 240 241
static void r_tpu_set_brightness(struct led_classdev *ldev,
				 enum led_brightness brightness)
{
	struct r_tpu_priv *p = container_of(ldev, struct r_tpu_priv, ldev);
	p->new_brightness = brightness;
	schedule_work(&p->work);
}

B
Bill Pemberton 已提交
242
static int r_tpu_probe(struct platform_device *pdev)
M
Magnus Damm 已提交
243 244 245 246
{
	struct led_renesas_tpu_config *cfg = pdev->dev.platform_data;
	struct r_tpu_priv *p;
	struct resource *res;
247
	int ret;
M
Magnus Damm 已提交
248 249 250

	if (!cfg) {
		dev_err(&pdev->dev, "missing platform data\n");
251
		return -ENODEV;
M
Magnus Damm 已提交
252 253
	}

254
	p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
M
Magnus Damm 已提交
255 256
	if (p == NULL) {
		dev_err(&pdev->dev, "failed to allocate driver data\n");
257
		return -ENOMEM;
M
Magnus Damm 已提交
258 259 260 261 262
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "failed to get I/O memory\n");
263
		return -ENXIO;
M
Magnus Damm 已提交
264 265 266
	}

	/* map memory, let mapbase point to our channel */
J
Jingoo Han 已提交
267 268
	p->mapbase = devm_ioremap_nocache(&pdev->dev, res->start,
					resource_size(res));
M
Magnus Damm 已提交
269 270
	if (p->mapbase == NULL) {
		dev_err(&pdev->dev, "failed to remap I/O memory\n");
271
		return -ENXIO;
M
Magnus Damm 已提交
272 273 274
	}

	/* get hold of clock */
J
Jingoo Han 已提交
275
	p->clk = devm_clk_get(&pdev->dev, NULL);
M
Magnus Damm 已提交
276 277
	if (IS_ERR(p->clk)) {
		dev_err(&pdev->dev, "cannot get clock\n");
J
Jingoo Han 已提交
278
		return PTR_ERR(p->clk);
M
Magnus Damm 已提交
279 280 281 282 283 284 285 286 287
	}

	p->pdev = pdev;
	p->pin_state = R_TPU_PIN_UNUSED;
	p->timer_state = R_TPU_TIMER_UNUSED;
	p->refresh_rate = cfg->refresh_rate ? cfg->refresh_rate : 100;
	r_tpu_set_pin(p, R_TPU_PIN_GPIO, LED_OFF);
	platform_set_drvdata(pdev, p);

288
	INIT_WORK(&p->work, r_tpu_work);
M
Magnus Damm 已提交
289 290 291 292 293 294 295 296

	p->ldev.name = cfg->name;
	p->ldev.brightness = LED_OFF;
	p->ldev.max_brightness = cfg->max_brightness;
	p->ldev.brightness_set = r_tpu_set_brightness;
	p->ldev.flags |= LED_CORE_SUSPENDRESUME;
	ret = led_classdev_register(&pdev->dev, &p->ldev);
	if (ret < 0)
J
Jingoo Han 已提交
297
		goto err0;
M
Magnus Damm 已提交
298 299 300 301 302 303 304 305

	/* max_brightness may be updated by the LED core code */
	p->min_rate = p->ldev.max_brightness * p->refresh_rate;

	pm_runtime_enable(&pdev->dev);
	return 0;

 err0:
J
Jingoo Han 已提交
306
	r_tpu_set_pin(p, R_TPU_PIN_UNUSED, LED_OFF);
M
Magnus Damm 已提交
307 308 309
	return ret;
}

B
Bill Pemberton 已提交
310
static int r_tpu_remove(struct platform_device *pdev)
M
Magnus Damm 已提交
311 312 313 314 315
{
	struct r_tpu_priv *p = platform_get_drvdata(pdev);

	r_tpu_set_brightness(&p->ldev, LED_OFF);
	led_classdev_unregister(&p->ldev);
316
	cancel_work_sync(&p->work);
M
Magnus Damm 已提交
317 318 319 320 321 322 323 324 325 326
	r_tpu_disable(p);
	r_tpu_set_pin(p, R_TPU_PIN_UNUSED, LED_OFF);

	pm_runtime_disable(&pdev->dev);

	return 0;
}

static struct platform_driver r_tpu_device_driver = {
	.probe		= r_tpu_probe,
B
Bill Pemberton 已提交
327
	.remove		= r_tpu_remove,
M
Magnus Damm 已提交
328 329 330 331 332
	.driver		= {
		.name	= "leds-renesas-tpu",
	}
};

333
module_platform_driver(r_tpu_device_driver);
M
Magnus Damm 已提交
334 335 336 337

MODULE_AUTHOR("Magnus Damm");
MODULE_DESCRIPTION("Renesas TPU LED Driver");
MODULE_LICENSE("GPL v2");