2 * Copyright (C) 2011 Paul Parsons <lost.distance@yahoo.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/platform_device.h>
12 #include <linux/leds.h>
13 #include <linux/slab.h>
15 #include <linux/mfd/asic3.h>
16 #include <linux/mfd/core.h>
17 #include <linux/module.h>
20 * The HTC ASIC3 LED GPIOs are inputs, not outputs.
21 * Hence we turn the LEDs on/off via the TimeBase register.
25 * When TimeBase is 4 the clock resolution is about 32Hz.
26 * This driver supports hardware blinking with an on+off
27 * period from 62ms (2 clocks) to 125s (4000 clocks).
29 #define MS_TO_CLK(ms) DIV_ROUND_CLOSEST(((ms)*1024), 32000)
30 #define CLK_TO_MS(clk) (((clk)*32000)/1024)
31 #define MAX_CLK 4000 /* Fits into 12-bit Time registers */
32 #define MAX_MS CLK_TO_MS(MAX_CLK)
34 static const unsigned int led_n_base
[ASIC3_NUM_LEDS
] = {
35 [0] = ASIC3_LED_0_Base
,
36 [1] = ASIC3_LED_1_Base
,
37 [2] = ASIC3_LED_2_Base
,
40 static void brightness_set(struct led_classdev
*cdev
,
41 enum led_brightness value
)
43 struct platform_device
*pdev
= to_platform_device(cdev
->dev
->parent
);
44 const struct mfd_cell
*cell
= mfd_get_cell(pdev
);
45 struct asic3
*asic
= dev_get_drvdata(pdev
->dev
.parent
);
49 timebase
= (value
== LED_OFF
) ? 0 : (LED_EN
|0x4);
51 base
= led_n_base
[cell
->id
];
52 asic3_write_register(asic
, (base
+ ASIC3_LED_PeriodTime
), 32);
53 asic3_write_register(asic
, (base
+ ASIC3_LED_DutyTime
), 32);
54 asic3_write_register(asic
, (base
+ ASIC3_LED_AutoStopCount
), 0);
55 asic3_write_register(asic
, (base
+ ASIC3_LED_TimeBase
), timebase
);
58 static int blink_set(struct led_classdev
*cdev
,
59 unsigned long *delay_on
,
60 unsigned long *delay_off
)
62 struct platform_device
*pdev
= to_platform_device(cdev
->dev
->parent
);
63 const struct mfd_cell
*cell
= mfd_get_cell(pdev
);
64 struct asic3
*asic
= dev_get_drvdata(pdev
->dev
.parent
);
69 if (*delay_on
> MAX_MS
|| *delay_off
> MAX_MS
)
72 if (*delay_on
== 0 && *delay_off
== 0) {
73 /* If both are zero then a sensible default should be chosen */
77 on
= MS_TO_CLK(*delay_on
);
78 off
= MS_TO_CLK(*delay_off
);
79 if ((on
+ off
) > MAX_CLK
)
83 base
= led_n_base
[cell
->id
];
84 asic3_write_register(asic
, (base
+ ASIC3_LED_PeriodTime
), (on
+ off
));
85 asic3_write_register(asic
, (base
+ ASIC3_LED_DutyTime
), on
);
86 asic3_write_register(asic
, (base
+ ASIC3_LED_AutoStopCount
), 0);
87 asic3_write_register(asic
, (base
+ ASIC3_LED_TimeBase
), (LED_EN
|0x4));
89 *delay_on
= CLK_TO_MS(on
);
90 *delay_off
= CLK_TO_MS(off
);
95 static int __devinit
asic3_led_probe(struct platform_device
*pdev
)
97 struct asic3_led
*led
= pdev
->dev
.platform_data
;
100 ret
= mfd_cell_enable(pdev
);
104 led
->cdev
= kzalloc(sizeof(struct led_classdev
), GFP_KERNEL
);
110 led
->cdev
->name
= led
->name
;
111 led
->cdev
->default_trigger
= led
->default_trigger
;
112 led
->cdev
->brightness_set
= brightness_set
;
113 led
->cdev
->blink_set
= blink_set
;
115 ret
= led_classdev_register(&pdev
->dev
, led
->cdev
);
124 (void) mfd_cell_disable(pdev
);
129 static int __devexit
asic3_led_remove(struct platform_device
*pdev
)
131 struct asic3_led
*led
= pdev
->dev
.platform_data
;
133 led_classdev_unregister(led
->cdev
);
137 return mfd_cell_disable(pdev
);
140 static struct platform_driver asic3_led_driver
= {
141 .probe
= asic3_led_probe
,
142 .remove
= __devexit_p(asic3_led_remove
),
144 .name
= "leds-asic3",
145 .owner
= THIS_MODULE
,
149 MODULE_ALIAS("platform:leds-asic3");
151 static int __init
asic3_led_init(void)
153 return platform_driver_register(&asic3_led_driver
);
156 static void __exit
asic3_led_exit(void)
158 platform_driver_unregister(&asic3_led_driver
);
161 module_init(asic3_led_init
);
162 module_exit(asic3_led_exit
);
164 MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>");
165 MODULE_DESCRIPTION("HTC ASIC3 LED driver");
166 MODULE_LICENSE("GPL");