18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * One-shot LED Trigger 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Based on ledtrig-timer.c by Richard Purdie <rpurdie@openedhand.com> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/init.h> 138c2ecf20Sopenharmony_ci#include <linux/device.h> 148c2ecf20Sopenharmony_ci#include <linux/ctype.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci#include <linux/leds.h> 178c2ecf20Sopenharmony_ci#include "../leds.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define DEFAULT_DELAY 100 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistruct oneshot_trig_data { 228c2ecf20Sopenharmony_ci unsigned int invert; 238c2ecf20Sopenharmony_ci}; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic ssize_t led_shot(struct device *dev, 268c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci struct led_classdev *led_cdev = led_trigger_get_led(dev); 298c2ecf20Sopenharmony_ci struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev); 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci led_blink_set_oneshot(led_cdev, 328c2ecf20Sopenharmony_ci &led_cdev->blink_delay_on, &led_cdev->blink_delay_off, 338c2ecf20Sopenharmony_ci oneshot_data->invert); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci /* content is ignored */ 368c2ecf20Sopenharmony_ci return size; 378c2ecf20Sopenharmony_ci} 388c2ecf20Sopenharmony_cistatic ssize_t led_invert_show(struct device *dev, 398c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev); 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", oneshot_data->invert); 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic ssize_t led_invert_store(struct device *dev, 478c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci struct led_classdev *led_cdev = led_trigger_get_led(dev); 508c2ecf20Sopenharmony_ci struct oneshot_trig_data *oneshot_data = led_trigger_get_drvdata(dev); 518c2ecf20Sopenharmony_ci unsigned long state; 528c2ecf20Sopenharmony_ci int ret; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci ret = kstrtoul(buf, 0, &state); 558c2ecf20Sopenharmony_ci if (ret) 568c2ecf20Sopenharmony_ci return ret; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci oneshot_data->invert = !!state; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci if (oneshot_data->invert) 618c2ecf20Sopenharmony_ci led_set_brightness_nosleep(led_cdev, LED_FULL); 628c2ecf20Sopenharmony_ci else 638c2ecf20Sopenharmony_ci led_set_brightness_nosleep(led_cdev, LED_OFF); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci return size; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic ssize_t led_delay_on_show(struct device *dev, 698c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci struct led_classdev *led_cdev = led_trigger_get_led(dev); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci return sprintf(buf, "%lu\n", led_cdev->blink_delay_on); 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cistatic ssize_t led_delay_on_store(struct device *dev, 778c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci struct led_classdev *led_cdev = led_trigger_get_led(dev); 808c2ecf20Sopenharmony_ci unsigned long state; 818c2ecf20Sopenharmony_ci int ret; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci ret = kstrtoul(buf, 0, &state); 848c2ecf20Sopenharmony_ci if (ret) 858c2ecf20Sopenharmony_ci return ret; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci led_cdev->blink_delay_on = state; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci return size; 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic ssize_t led_delay_off_show(struct device *dev, 938c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci struct led_classdev *led_cdev = led_trigger_get_led(dev); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci return sprintf(buf, "%lu\n", led_cdev->blink_delay_off); 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic ssize_t led_delay_off_store(struct device *dev, 1018c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, size_t size) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci struct led_classdev *led_cdev = led_trigger_get_led(dev); 1048c2ecf20Sopenharmony_ci unsigned long state; 1058c2ecf20Sopenharmony_ci int ret; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci ret = kstrtoul(buf, 0, &state); 1088c2ecf20Sopenharmony_ci if (ret) 1098c2ecf20Sopenharmony_ci return ret; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci led_cdev->blink_delay_off = state; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci return size; 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store); 1178c2ecf20Sopenharmony_cistatic DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store); 1188c2ecf20Sopenharmony_cistatic DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store); 1198c2ecf20Sopenharmony_cistatic DEVICE_ATTR(shot, 0200, NULL, led_shot); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cistatic struct attribute *oneshot_trig_attrs[] = { 1228c2ecf20Sopenharmony_ci &dev_attr_delay_on.attr, 1238c2ecf20Sopenharmony_ci &dev_attr_delay_off.attr, 1248c2ecf20Sopenharmony_ci &dev_attr_invert.attr, 1258c2ecf20Sopenharmony_ci &dev_attr_shot.attr, 1268c2ecf20Sopenharmony_ci NULL 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(oneshot_trig); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic void pattern_init(struct led_classdev *led_cdev) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci u32 *pattern; 1338c2ecf20Sopenharmony_ci unsigned int size = 0; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci pattern = led_get_default_pattern(led_cdev, &size); 1368c2ecf20Sopenharmony_ci if (!pattern) 1378c2ecf20Sopenharmony_ci goto out_default; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci if (size != 2) { 1408c2ecf20Sopenharmony_ci dev_warn(led_cdev->dev, 1418c2ecf20Sopenharmony_ci "Expected 2 but got %u values for delays pattern\n", 1428c2ecf20Sopenharmony_ci size); 1438c2ecf20Sopenharmony_ci goto out_default; 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci led_cdev->blink_delay_on = pattern[0]; 1478c2ecf20Sopenharmony_ci led_cdev->blink_delay_off = pattern[1]; 1488c2ecf20Sopenharmony_ci kfree(pattern); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci return; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ciout_default: 1538c2ecf20Sopenharmony_ci kfree(pattern); 1548c2ecf20Sopenharmony_ci led_cdev->blink_delay_on = DEFAULT_DELAY; 1558c2ecf20Sopenharmony_ci led_cdev->blink_delay_off = DEFAULT_DELAY; 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic int oneshot_trig_activate(struct led_classdev *led_cdev) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci struct oneshot_trig_data *oneshot_data; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci oneshot_data = kzalloc(sizeof(*oneshot_data), GFP_KERNEL); 1638c2ecf20Sopenharmony_ci if (!oneshot_data) 1648c2ecf20Sopenharmony_ci return -ENOMEM; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci led_set_trigger_data(led_cdev, oneshot_data); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) { 1698c2ecf20Sopenharmony_ci pattern_init(led_cdev); 1708c2ecf20Sopenharmony_ci /* 1718c2ecf20Sopenharmony_ci * Mark as initialized even on pattern_init() error because 1728c2ecf20Sopenharmony_ci * any consecutive call to it would produce the same error. 1738c2ecf20Sopenharmony_ci */ 1748c2ecf20Sopenharmony_ci led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci return 0; 1788c2ecf20Sopenharmony_ci} 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cistatic void oneshot_trig_deactivate(struct led_classdev *led_cdev) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci struct oneshot_trig_data *oneshot_data = led_get_trigger_data(led_cdev); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci kfree(oneshot_data); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci /* Stop blinking */ 1878c2ecf20Sopenharmony_ci led_set_brightness(led_cdev, LED_OFF); 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic struct led_trigger oneshot_led_trigger = { 1918c2ecf20Sopenharmony_ci .name = "oneshot", 1928c2ecf20Sopenharmony_ci .activate = oneshot_trig_activate, 1938c2ecf20Sopenharmony_ci .deactivate = oneshot_trig_deactivate, 1948c2ecf20Sopenharmony_ci .groups = oneshot_trig_groups, 1958c2ecf20Sopenharmony_ci}; 1968c2ecf20Sopenharmony_cimodule_led_trigger(oneshot_led_trigger); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ciMODULE_AUTHOR("Fabio Baltieri <fabio.baltieri@gmail.com>"); 1998c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("One-shot LED trigger"); 2008c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 201