leds-mc13783.c 10.4 KB
Newer Older
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 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 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 136 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*
 * LEDs driver for Freescale MC13783
 *
 * Copyright (C) 2010 Philippe Rétornaz
 *
 * Based on leds-da903x:
 * Copyright (C) 2008 Compulab, Ltd.
 *      Mike Rapoport <mike@compulab.co.il>
 *
 * Copyright (C) 2006-2008 Marvell International Ltd.
 *      Eric Miao <eric.miao@marvell.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/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/workqueue.h>
#include <linux/mfd/mc13783.h>
#include <linux/slab.h>

struct mc13783_led {
	struct led_classdev	cdev;
	struct work_struct	work;
	struct mc13783		*master;
	enum led_brightness	new_brightness;
	int			id;
};

#define MC13783_REG_LED_CONTROL_0	51
#define MC13783_LED_C0_ENABLE_BIT	(1 << 0)
#define MC13783_LED_C0_TRIODE_MD_BIT	(1 << 7)
#define MC13783_LED_C0_TRIODE_AD_BIT	(1 << 8)
#define MC13783_LED_C0_TRIODE_KP_BIT	(1 << 9)
#define MC13783_LED_C0_BOOST_BIT	(1 << 10)
#define MC13783_LED_C0_ABMODE_MASK	0x7
#define MC13783_LED_C0_ABMODE		11
#define MC13783_LED_C0_ABREF_MASK	0x3
#define MC13783_LED_C0_ABREF		14

#define MC13783_REG_LED_CONTROL_1	52
#define MC13783_LED_C1_TC1HALF_BIT	(1 << 18)

#define MC13783_REG_LED_CONTROL_2	53
#define MC13783_LED_C2_BL_P_MASK	0xf
#define MC13783_LED_C2_MD_P		9
#define MC13783_LED_C2_AD_P		13
#define MC13783_LED_C2_KP_P		17
#define MC13783_LED_C2_BL_C_MASK	0x7
#define MC13783_LED_C2_MD_C		0
#define MC13783_LED_C2_AD_C		3
#define MC13783_LED_C2_KP_C		6

#define MC13783_REG_LED_CONTROL_3	54
#define MC13783_LED_C3_TC_P		6
#define MC13783_LED_C3_TC_P_MASK	0x1f

#define MC13783_REG_LED_CONTROL_4	55
#define MC13783_REG_LED_CONTROL_5	56

#define MC13783_LED_Cx_PERIOD		21
#define MC13783_LED_Cx_PERIOD_MASK	0x3
#define MC13783_LED_Cx_SLEWLIM_BIT      (1 << 23)
#define MC13783_LED_Cx_TRIODE_TC_BIT	(1 << 23)
#define MC13783_LED_Cx_TC_C_MASK	0x3

static void mc13783_led_work(struct work_struct *work)
{
	struct mc13783_led *led = container_of(work, struct mc13783_led, work);
	int reg = 0;
	int mask = 0;
	int value = 0;
	int bank, off, shift;

	switch (led->id) {
	case MC13783_LED_MD:
		reg = MC13783_REG_LED_CONTROL_2;
		mask = MC13783_LED_C2_BL_P_MASK << MC13783_LED_C2_MD_P;
		value = (led->new_brightness >> 4) << MC13783_LED_C2_MD_P;
		break;
	case MC13783_LED_AD:
		reg = MC13783_REG_LED_CONTROL_2;
		mask = MC13783_LED_C2_BL_P_MASK << MC13783_LED_C2_AD_P;
		value = (led->new_brightness >> 4) << MC13783_LED_C2_AD_P;
		break;
	case MC13783_LED_KP:
		reg = MC13783_REG_LED_CONTROL_2;
		mask = MC13783_LED_C2_BL_P_MASK << MC13783_LED_C2_KP_P;
		value = (led->new_brightness >> 4) << MC13783_LED_C2_KP_P;
		break;
	case MC13783_LED_R1:
	case MC13783_LED_G1:
	case MC13783_LED_B1:
	case MC13783_LED_R2:
	case MC13783_LED_G2:
	case MC13783_LED_B2:
	case MC13783_LED_R3:
	case MC13783_LED_G3:
	case MC13783_LED_B3:
		off = led->id - MC13783_LED_R1;
		bank = off/3;
		reg = MC13783_REG_LED_CONTROL_3 + off/3;
		shift = (off - bank * 3) * 5 + MC13783_LED_C3_TC_P;
		value = (led->new_brightness >> 3) << shift;
		mask = MC13783_LED_C3_TC_P_MASK << shift;
		break;
	}

	mc13783_lock(led->master);

	mc13783_reg_rmw(led->master, reg, mask, value);

	mc13783_unlock(led->master);
}

static void mc13783_led_set(struct led_classdev *led_cdev,
			   enum led_brightness value)
{
	struct mc13783_led *led;

	led = container_of(led_cdev, struct mc13783_led, cdev);
	led->new_brightness = value;
	schedule_work(&led->work);
}

static int __devinit mc13783_led_setup(struct mc13783_led *led, int max_current)
{
	int shift = 0;
	int mask = 0;
	int value = 0;
	int reg = 0;
	int ret, bank;

	switch (led->id) {
	case MC13783_LED_MD:
		shift = MC13783_LED_C2_MD_C;
		mask = MC13783_LED_C2_BL_C_MASK;
		value = max_current & MC13783_LED_C2_BL_C_MASK;
		reg = MC13783_REG_LED_CONTROL_2;
		break;
	case MC13783_LED_AD:
		shift = MC13783_LED_C2_AD_C;
		mask = MC13783_LED_C2_BL_C_MASK;
		value = max_current & MC13783_LED_C2_BL_C_MASK;
		reg = MC13783_REG_LED_CONTROL_2;
		break;
	case MC13783_LED_KP:
		shift = MC13783_LED_C2_KP_C;
		mask = MC13783_LED_C2_BL_C_MASK;
		value = max_current & MC13783_LED_C2_BL_C_MASK;
		reg = MC13783_REG_LED_CONTROL_2;
		break;
	case MC13783_LED_R1:
	case MC13783_LED_G1:
	case MC13783_LED_B1:
	case MC13783_LED_R2:
	case MC13783_LED_G2:
	case MC13783_LED_B2:
	case MC13783_LED_R3:
	case MC13783_LED_G3:
	case MC13783_LED_B3:
		bank = (led->id - MC13783_LED_R1)/3;
		reg = MC13783_REG_LED_CONTROL_3 + bank;
		shift = ((led->id - MC13783_LED_R1) - bank * 3) * 2;
		mask = MC13783_LED_Cx_TC_C_MASK;
		value = max_current & MC13783_LED_Cx_TC_C_MASK;
		break;
	}

	mc13783_lock(led->master);

	ret = mc13783_reg_rmw(led->master, reg, mask << shift,
						value << shift);

	mc13783_unlock(led->master);
	return ret;
}

static int __devinit mc13783_leds_prepare(struct platform_device *pdev)
{
186
	struct mc13783_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	struct mc13783 *dev = dev_get_drvdata(pdev->dev.parent);
	int ret = 0;
	int reg = 0;

	mc13783_lock(dev);

	if (pdata->flags & MC13783_LED_TC1HALF)
		reg |= MC13783_LED_C1_TC1HALF_BIT;

	if (pdata->flags & MC13783_LED_SLEWLIMTC)
		reg |= MC13783_LED_Cx_SLEWLIM_BIT;

	ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_1, reg);
	if (ret)
		goto out;

	reg = (pdata->bl_period & MC13783_LED_Cx_PERIOD_MASK) <<
							MC13783_LED_Cx_PERIOD;

	if (pdata->flags & MC13783_LED_SLEWLIMBL)
		reg |= MC13783_LED_Cx_SLEWLIM_BIT;

	ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_2, reg);
	if (ret)
		goto out;

	reg = (pdata->tc1_period & MC13783_LED_Cx_PERIOD_MASK) <<
							MC13783_LED_Cx_PERIOD;

	if (pdata->flags & MC13783_LED_TRIODE_TC1)
		reg |= MC13783_LED_Cx_TRIODE_TC_BIT;

	ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_3, reg);
	if (ret)
		goto out;

	reg = (pdata->tc2_period & MC13783_LED_Cx_PERIOD_MASK) <<
							MC13783_LED_Cx_PERIOD;

	if (pdata->flags & MC13783_LED_TRIODE_TC2)
		reg |= MC13783_LED_Cx_TRIODE_TC_BIT;

	ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_4, reg);
	if (ret)
		goto out;

	reg = (pdata->tc3_period & MC13783_LED_Cx_PERIOD_MASK) <<
							MC13783_LED_Cx_PERIOD;

	if (pdata->flags & MC13783_LED_TRIODE_TC3)
237
		reg |= MC13783_LED_Cx_TRIODE_TC_BIT;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

	ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_5, reg);
	if (ret)
		goto out;

	reg = MC13783_LED_C0_ENABLE_BIT;
	if (pdata->flags & MC13783_LED_TRIODE_MD)
		reg |= MC13783_LED_C0_TRIODE_MD_BIT;
	if (pdata->flags & MC13783_LED_TRIODE_AD)
		reg |= MC13783_LED_C0_TRIODE_AD_BIT;
	if (pdata->flags & MC13783_LED_TRIODE_KP)
		reg |= MC13783_LED_C0_TRIODE_KP_BIT;
	if (pdata->flags & MC13783_LED_BOOST_EN)
		reg |= MC13783_LED_C0_BOOST_BIT;

	reg |= (pdata->abmode & MC13783_LED_C0_ABMODE_MASK) <<
							MC13783_LED_C0_ABMODE;
	reg |= (pdata->abref & MC13783_LED_C0_ABREF_MASK) <<
							MC13783_LED_C0_ABREF;

	ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_0, reg);

out:
	mc13783_unlock(dev);
	return ret;
}

static int __devinit mc13783_led_probe(struct platform_device *pdev)
{
267
	struct mc13783_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	struct mc13783_led_platform_data *led_cur;
	struct mc13783_led *led, *led_dat;
	int ret, i;
	int init_led = 0;

	if (pdata == NULL) {
		dev_err(&pdev->dev, "missing platform data\n");
		return -ENODEV;
	}

	if (pdata->num_leds < 1 || pdata->num_leds > MC13783_LED_MAX) {
		dev_err(&pdev->dev, "Invalid led count %d\n", pdata->num_leds);
		return -EINVAL;
	}

	led = kzalloc(sizeof(*led) * pdata->num_leds, GFP_KERNEL);
	if (led == NULL) {
		dev_err(&pdev->dev, "failed to alloc memory\n");
		return -ENOMEM;
	}

	ret = mc13783_leds_prepare(pdev);
	if (ret) {
		dev_err(&pdev->dev, "unable to init led driver\n");
		goto err_free;
	}

	for (i = 0; i < pdata->num_leds; i++) {
		led_dat = &led[i];
		led_cur = &pdata->led[i];

		if (led_cur->id > MC13783_LED_MAX || led_cur->id < 0) {
			dev_err(&pdev->dev, "invalid id %d\n", led_cur->id);
			ret = -EINVAL;
			goto err_register;
		}

		if (init_led & (1 << led_cur->id)) {
			dev_err(&pdev->dev, "led %d already initialized\n",
					led_cur->id);
			ret = -EINVAL;
			goto err_register;
		}

		init_led |= 1 << led_cur->id;
		led_dat->cdev.name = led_cur->name;
		led_dat->cdev.default_trigger = led_cur->default_trigger;
		led_dat->cdev.brightness_set = mc13783_led_set;
		led_dat->cdev.brightness = LED_OFF;
		led_dat->id = led_cur->id;
		led_dat->master = dev_get_drvdata(pdev->dev.parent);

		INIT_WORK(&led_dat->work, mc13783_led_work);

		ret = led_classdev_register(pdev->dev.parent, &led_dat->cdev);
		if (ret) {
			dev_err(&pdev->dev, "failed to register led %d\n",
					led_dat->id);
			goto err_register;
		}

		ret = mc13783_led_setup(led_dat, led_cur->max_current);
		if (ret) {
			dev_err(&pdev->dev, "unable to init led %d\n",
					led_dat->id);
			i++;
			goto err_register;
		}
	}

	platform_set_drvdata(pdev, led);
	return 0;

err_register:
	for (i = i - 1; i >= 0; i--) {
		led_classdev_unregister(&led[i].cdev);
		cancel_work_sync(&led[i].work);
	}

err_free:
	kfree(led);
	return ret;
}

static int __devexit mc13783_led_remove(struct platform_device *pdev)
{
354
	struct mc13783_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
	struct mc13783_led *led = platform_get_drvdata(pdev);
	struct mc13783 *dev = dev_get_drvdata(pdev->dev.parent);
	int i;

	for (i = 0; i < pdata->num_leds; i++) {
		led_classdev_unregister(&led[i].cdev);
		cancel_work_sync(&led[i].work);
	}

	mc13783_lock(dev);

	mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_0, 0);
	mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_1, 0);
	mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_2, 0);
	mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_3, 0);
	mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_4, 0);
	mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_5, 0);

	mc13783_unlock(dev);

	kfree(led);
	return 0;
}

static struct platform_driver mc13783_led_driver = {
	.driver	= {
		.name	= "mc13783-led",
		.owner	= THIS_MODULE,
	},
	.probe		= mc13783_led_probe,
	.remove		= __devexit_p(mc13783_led_remove),
};

static int __init mc13783_led_init(void)
{
	return platform_driver_register(&mc13783_led_driver);
}
module_init(mc13783_led_init);

static void __exit mc13783_led_exit(void)
{
	platform_driver_unregister(&mc13783_led_driver);
}
module_exit(mc13783_led_exit);

MODULE_DESCRIPTION("LEDs driver for Freescale MC13783 PMIC");
MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:mc13783-led");