0019-MIPS-ralink-add-pseudo-pwm-led-trigger-based-on-time.patch 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. From 9de00286e20a5f5edc419698373010f1cb6ff0ce Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Sun, 27 Jul 2014 09:25:02 +0100
  4. Subject: [PATCH 19/57] MIPS: ralink: add pseudo pwm led trigger based on
  5. timer0
  6. Signed-off-by: John Crispin <blogic@openwrt.org>
  7. ---
  8. arch/mips/ralink/timer.c | 213 ++++++++++++++++++++++++++++++++++++++++++----
  9. 1 file changed, 197 insertions(+), 16 deletions(-)
  10. --- a/arch/mips/ralink/timer.c
  11. +++ b/arch/mips/ralink/timer.c
  12. @@ -12,6 +12,8 @@
  13. #include <linux/timer.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/clk.h>
  16. +#include <linux/leds.h>
  17. +#include <linux/slab.h>
  18. #include <asm/mach-ralink/ralink_regs.h>
  19. @@ -23,16 +25,34 @@
  20. #define TMR0CTL_ENABLE BIT(7)
  21. #define TMR0CTL_MODE_PERIODIC BIT(4)
  22. -#define TMR0CTL_PRESCALER 1
  23. +#define TMR0CTL_PRESCALER 2
  24. #define TMR0CTL_PRESCALE_VAL (0xf - TMR0CTL_PRESCALER)
  25. #define TMR0CTL_PRESCALE_DIV (65536 / BIT(TMR0CTL_PRESCALER))
  26. +struct rt_timer_gpio {
  27. + struct list_head list;
  28. + struct led_classdev *led;
  29. +};
  30. +
  31. struct rt_timer {
  32. - struct device *dev;
  33. - void __iomem *membase;
  34. - int irq;
  35. - unsigned long timer_freq;
  36. - unsigned long timer_div;
  37. + struct device *dev;
  38. + void __iomem *membase;
  39. + int irq;
  40. +
  41. + unsigned long timer_freq;
  42. + unsigned long timer_div;
  43. +
  44. + struct list_head gpios;
  45. + struct led_trigger led_trigger;
  46. + unsigned int duty_cycle;
  47. + unsigned int duty;
  48. +
  49. + unsigned int fade;
  50. + unsigned int fade_min;
  51. + unsigned int fade_max;
  52. + unsigned int fade_speed;
  53. + unsigned int fade_dir;
  54. + unsigned int fade_count;
  55. };
  56. static inline void rt_timer_w32(struct rt_timer *rt, u8 reg, u32 val)
  57. @@ -48,8 +68,37 @@ static inline u32 rt_timer_r32(struct rt
  58. static irqreturn_t rt_timer_irq(int irq, void *_rt)
  59. {
  60. struct rt_timer *rt = (struct rt_timer *) _rt;
  61. + struct rt_timer_gpio *gpio;
  62. + unsigned int val;
  63. - rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
  64. + if (rt->fade && (rt->fade_count++ > rt->fade_speed)) {
  65. + rt->fade_count = 0;
  66. + if (rt->duty_cycle <= rt->fade_min)
  67. + rt->fade_dir = 1;
  68. + else if (rt->duty_cycle >= rt->fade_max)
  69. + rt->fade_dir = 0;
  70. +
  71. + if (rt->fade_dir)
  72. + rt->duty_cycle += 1;
  73. + else
  74. + rt->duty_cycle -= 1;
  75. +
  76. + }
  77. +
  78. + val = rt->timer_freq / rt->timer_div;
  79. + if (rt->duty)
  80. + val *= rt->duty_cycle;
  81. + else
  82. + val *= (100 - rt->duty_cycle);
  83. + val /= 100;
  84. +
  85. + if (!list_empty(&rt->gpios))
  86. + list_for_each_entry(gpio, &rt->gpios, list)
  87. + led_set_brightness(gpio->led, !!rt->duty);
  88. +
  89. + rt->duty = !rt->duty;
  90. +
  91. + rt_timer_w32(rt, TIMER_REG_TMR0LOAD, val + 1);
  92. rt_timer_w32(rt, TIMER_REG_TMRSTAT, TMRSTAT_TMR0INT);
  93. return IRQ_HANDLED;
  94. @@ -58,8 +107,8 @@ static irqreturn_t rt_timer_irq(int irq,
  95. static int rt_timer_request(struct rt_timer *rt)
  96. {
  97. - int err = request_irq(rt->irq, rt_timer_irq, 0,
  98. - dev_name(rt->dev), rt);
  99. + int err = devm_request_irq(rt->dev, rt->irq, rt_timer_irq,
  100. + 0, dev_name(rt->dev), rt);
  101. if (err) {
  102. dev_err(rt->dev, "failed to request irq\n");
  103. } else {
  104. @@ -81,8 +130,6 @@ static int rt_timer_config(struct rt_tim
  105. else
  106. rt->timer_div = divisor;
  107. - rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
  108. -
  109. return 0;
  110. }
  111. @@ -108,11 +155,128 @@ static void rt_timer_disable(struct rt_t
  112. rt_timer_w32(rt, TIMER_REG_TMR0CTL, t);
  113. }
  114. +static ssize_t led_fade_show(struct device *dev,
  115. + struct device_attribute *attr, char *buf)
  116. +{
  117. + struct led_classdev *led_cdev = dev_get_drvdata(dev);
  118. + struct rt_timer *rt = container_of(led_cdev->trigger, struct rt_timer, led_trigger);
  119. +
  120. + return sprintf(buf, "speed: %d, min: %d, max: %d\n", rt->fade_speed, rt->fade_min, rt->fade_max);
  121. +}
  122. +
  123. +static ssize_t led_fade_store(struct device *dev,
  124. + struct device_attribute *attr, const char *buf, size_t size)
  125. +{
  126. + struct led_classdev *led_cdev = dev_get_drvdata(dev);
  127. + struct rt_timer *rt = container_of(led_cdev->trigger, struct rt_timer, led_trigger);
  128. + unsigned int speed = 0, min = 0, max = 0;
  129. + ssize_t ret = -EINVAL;
  130. +
  131. + ret = sscanf(buf, "%u %u %u", &speed, &min, &max);
  132. +
  133. + if (ret == 3) {
  134. + rt->fade_speed = speed;
  135. + rt->fade_min = min;
  136. + rt->fade_max = max;
  137. + rt->fade = 1;
  138. + } else {
  139. + rt->fade = 0;
  140. + }
  141. +
  142. + return size;
  143. +}
  144. +
  145. +static DEVICE_ATTR(fade, 0644, led_fade_show, led_fade_store);
  146. +
  147. +static ssize_t led_duty_cycle_show(struct device *dev,
  148. + struct device_attribute *attr, char *buf)
  149. +{
  150. + struct led_classdev *led_cdev = dev_get_drvdata(dev);
  151. + struct rt_timer *rt = container_of(led_cdev->trigger, struct rt_timer, led_trigger);
  152. +
  153. + return sprintf(buf, "%u\n", rt->duty_cycle);
  154. +}
  155. +
  156. +static ssize_t led_duty_cycle_store(struct device *dev,
  157. + struct device_attribute *attr, const char *buf, size_t size)
  158. +{
  159. + struct led_classdev *led_cdev = dev_get_drvdata(dev);
  160. + struct rt_timer *rt = container_of(led_cdev->trigger, struct rt_timer, led_trigger);
  161. + unsigned long state;
  162. + ssize_t ret = -EINVAL;
  163. +
  164. + ret = kstrtoul(buf, 10, &state);
  165. + if (ret)
  166. + return ret;
  167. +
  168. + if (state <= 100)
  169. + rt->duty_cycle = state;
  170. + else
  171. + rt->duty_cycle = 100;
  172. +
  173. + rt->fade = 0;
  174. +
  175. + return size;
  176. +}
  177. +
  178. +static DEVICE_ATTR(duty_cycle, 0644, led_duty_cycle_show, led_duty_cycle_store);
  179. +
  180. +static void rt_timer_trig_activate(struct led_classdev *led_cdev)
  181. +{
  182. + struct rt_timer *rt = container_of(led_cdev->trigger, struct rt_timer, led_trigger);
  183. + struct rt_timer_gpio *gpio_data;
  184. + int rc;
  185. +
  186. + led_cdev->trigger_data = NULL;
  187. + gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
  188. + if (!gpio_data)
  189. + return;
  190. +
  191. + rc = device_create_file(led_cdev->dev, &dev_attr_duty_cycle);
  192. + if (rc)
  193. + goto err_gpio;
  194. + rc = device_create_file(led_cdev->dev, &dev_attr_fade);
  195. + if (rc)
  196. + goto err_out_duty_cycle;
  197. +
  198. + led_cdev->activated = true;
  199. + led_cdev->trigger_data = gpio_data;
  200. + gpio_data->led = led_cdev;
  201. + list_add(&gpio_data->list, &rt->gpios);
  202. + led_cdev->trigger_data = gpio_data;
  203. + rt_timer_enable(rt);
  204. + return;
  205. +
  206. +err_out_duty_cycle:
  207. + device_remove_file(led_cdev->dev, &dev_attr_duty_cycle);
  208. +
  209. +err_gpio:
  210. + kfree(gpio_data);
  211. +}
  212. +
  213. +static void rt_timer_trig_deactivate(struct led_classdev *led_cdev)
  214. +{
  215. + struct rt_timer *rt = container_of(led_cdev->trigger, struct rt_timer, led_trigger);
  216. + struct rt_timer_gpio *gpio_data = (struct rt_timer_gpio*) led_cdev->trigger_data;
  217. +
  218. + if (led_cdev->activated) {
  219. + device_remove_file(led_cdev->dev, &dev_attr_duty_cycle);
  220. + device_remove_file(led_cdev->dev, &dev_attr_fade);
  221. + led_cdev->activated = false;
  222. + }
  223. +
  224. + list_del(&gpio_data->list);
  225. + rt_timer_disable(rt);
  226. + led_set_brightness(led_cdev, LED_OFF);
  227. +}
  228. +
  229. static int rt_timer_probe(struct platform_device *pdev)
  230. {
  231. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  232. + const __be32 *divisor;
  233. struct rt_timer *rt;
  234. struct clk *clk;
  235. + int ret;
  236. rt = devm_kzalloc(&pdev->dev, sizeof(*rt), GFP_KERNEL);
  237. if (!rt) {
  238. @@ -140,12 +304,29 @@ static int rt_timer_probe(struct platfor
  239. if (!rt->timer_freq)
  240. return -EINVAL;
  241. + rt->duty_cycle = 100;
  242. rt->dev = &pdev->dev;
  243. platform_set_drvdata(pdev, rt);
  244. - rt_timer_request(rt);
  245. - rt_timer_config(rt, 2);
  246. - rt_timer_enable(rt);
  247. + ret = rt_timer_request(rt);
  248. + if (ret)
  249. + return ret;
  250. +
  251. + divisor = of_get_property(pdev->dev.of_node, "ralink,divisor", NULL);
  252. + if (divisor)
  253. + rt_timer_config(rt, be32_to_cpu(*divisor));
  254. + else
  255. + rt_timer_config(rt, 200);
  256. +
  257. + rt->led_trigger.name = "pwmtimer",
  258. + rt->led_trigger.activate = rt_timer_trig_activate,
  259. + rt->led_trigger.deactivate = rt_timer_trig_deactivate,
  260. +
  261. + ret = led_trigger_register(&rt->led_trigger);
  262. + if (ret)
  263. + return ret;
  264. +
  265. + INIT_LIST_HEAD(&rt->gpios);
  266. dev_info(&pdev->dev, "maximum frequency is %luHz\n", rt->timer_freq);
  267. @@ -156,6 +337,7 @@ static int rt_timer_remove(struct platfo
  268. {
  269. struct rt_timer *rt = platform_get_drvdata(pdev);
  270. + led_trigger_unregister(&rt->led_trigger);
  271. rt_timer_disable(rt);
  272. rt_timer_free(rt);
  273. @@ -180,6 +362,6 @@ static struct platform_driver rt_timer_d
  274. module_platform_driver(rt_timer_driver);
  275. -MODULE_DESCRIPTION("Ralink RT2880 timer");
  276. +MODULE_DESCRIPTION("Ralink RT2880 timer / pseudo pwm");
  277. MODULE_AUTHOR("John Crispin <blogic@openwrt.org");
  278. MODULE_LICENSE("GPL");