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>
19 * The HTC ASIC3 LED GPIOs are inputs, not outputs.
20 * Hence we turn the LEDs on/off via the TimeBase register.
24 * When TimeBase is 4 the clock resolution is about 32Hz.
25 * This driver supports hardware blinking with an on+off
26 * period from 62ms (2 clocks) to 125s (4000 clocks).
28 #define MS_TO_CLK(ms) DIV_ROUND_CLOSEST(((ms)*1024), 32000)
29 #define CLK_TO_MS(clk) (((clk)*32000)/1024)
30 #define MAX_CLK 4000 /* Fits into 12-bit Time registers */
31 #define MAX_MS CLK_TO_MS(MAX_CLK)
33 static const unsigned int led_n_base
[ASIC3_NUM_LEDS
] = {
34 [0] = ASIC3_LED_0_Base
,
35 [1] = ASIC3_LED_1_Base
,
36 [2] = ASIC3_LED_2_Base
,
39 static void brightness_set(struct led_classdev
*cdev
,
40 enum led_brightness value
)
42 struct platform_device
*pdev
= to_platform_device(cdev
->dev
->parent
);
43 const struct mfd_cell
*cell
= mfd_get_cell(pdev
);
44 struct asic3
*asic
= dev_get_drvdata(pdev
->dev
.parent
);
48 timebase
= (value
== LED_OFF
) ? 0 : (LED_EN
|0x4);
50 base
= led_n_base
[cell
->id
];
51 asic3_write_register(asic
, (base
+ ASIC3_LED_PeriodTime
), 32);
52 asic3_write_register(asic
, (base
+ ASIC3_LED_DutyTime
), 32);
53 asic3_write_register(asic
, (base
+ ASIC3_LED_AutoStopCount
), 0);
54 asic3_write_register(asic
, (base
+ ASIC3_LED_TimeBase
), timebase
);
57 static int blink_set(struct led_classdev
*cdev
,
58 unsigned long *delay_on
,
59 unsigned long *delay_off
)
61 struct platform_device
*pdev
= to_platform_device(cdev
->dev
->parent
);
62 const struct mfd_cell
*cell
= mfd_get_cell(pdev
);
63 struct asic3
*asic
= dev_get_drvdata(pdev
->dev
.parent
);
68 if (*delay_on
> MAX_MS
|| *delay_off
> MAX_MS
)
71 if (*delay_on
== 0 && *delay_off
== 0) {
72 /* If both are zero then a sensible default should be chosen */
76 on
= MS_TO_CLK(*delay_on
);
77 off
= MS_TO_CLK(*delay_off
);
78 if ((on
+ off
) > MAX_CLK
)
82 base
= led_n_base
[cell
->id
];
83 asic3_write_register(asic
, (base
+ ASIC3_LED_PeriodTime
), (on
+ off
));
84 asic3_write_register(asic
, (base
+ ASIC3_LED_DutyTime
), on
);
85 asic3_write_register(asic
, (base
+ ASIC3_LED_AutoStopCount
), 0);
86 asic3_write_register(asic
, (base
+ ASIC3_LED_TimeBase
), (LED_EN
|0x4));
88 *delay_on
= CLK_TO_MS(on
);
89 *delay_off
= CLK_TO_MS(off
);
94 static int __devinit
asic3_led_probe(struct platform_device
*pdev
)
96 struct asic3_led
*led
= pdev
->dev
.platform_data
;
99 ret
= mfd_cell_enable(pdev
);
103 led
->cdev
= kzalloc(sizeof(struct led_classdev
), GFP_KERNEL
);
109 led
->cdev
->name
= led
->name
;
110 led
->cdev
->flags
= LED_CORE_SUSPENDRESUME
;
111 led
->cdev
->brightness_set
= brightness_set
;
112 led
->cdev
->blink_set
= blink_set
;
113 led
->cdev
->default_trigger
= led
->default_trigger
;
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 int asic3_led_suspend(struct device
*dev
)
142 struct platform_device
*pdev
= to_platform_device(dev
);
143 const struct mfd_cell
*cell
= mfd_get_cell(pdev
);
148 ret
= (*cell
->suspend
)(pdev
);
153 static int asic3_led_resume(struct device
*dev
)
155 struct platform_device
*pdev
= to_platform_device(dev
);
156 const struct mfd_cell
*cell
= mfd_get_cell(pdev
);
161 ret
= (*cell
->resume
)(pdev
);
166 static const struct dev_pm_ops asic3_led_pm_ops
= {
167 .suspend
= asic3_led_suspend
,
168 .resume
= asic3_led_resume
,
171 static struct platform_driver asic3_led_driver
= {
172 .probe
= asic3_led_probe
,
173 .remove
= __devexit_p(asic3_led_remove
),
175 .name
= "leds-asic3",
176 .owner
= THIS_MODULE
,
177 .pm
= &asic3_led_pm_ops
,
181 MODULE_ALIAS("platform:leds-asic3");
183 static int __init
asic3_led_init(void)
185 return platform_driver_register(&asic3_led_driver
);
188 static void __exit
asic3_led_exit(void)
190 platform_driver_unregister(&asic3_led_driver
);
193 module_init(asic3_led_init
);
194 module_exit(asic3_led_exit
);
196 MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>");
197 MODULE_DESCRIPTION("HTC ASIC3 LED driver");
198 MODULE_LICENSE("GPL");