timed_gpio.c revision 07960058f0ce77ddc3027d3e45a5de1fb977334f
1/* drivers/misc/timed_gpio.c 2 * 3 * Copyright (C) 2008 Google, Inc. 4 * Author: Mike Lockwood <lockwood@android.com> 5 * 6 * This software is licensed under the terms of the GNU General Public 7 * License version 2, as published by the Free Software Foundation, and 8 * may be copied, distributed, and modified under those terms. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 */ 16 17#include <linux/module.h> 18#include <linux/platform_device.h> 19#include <linux/hrtimer.h> 20#include <linux/err.h> 21#include <linux/gpio.h> 22 23#include "timed_gpio.h" 24 25 26static struct class *timed_gpio_class; 27 28struct timed_gpio_data { 29 struct device *dev; 30 struct hrtimer timer; 31 spinlock_t lock; 32 unsigned gpio; 33 int max_timeout; 34 u8 active_low; 35}; 36 37static enum hrtimer_restart gpio_timer_func(struct hrtimer *timer) 38{ 39 struct timed_gpio_data *gpio_data = container_of(timer, struct timed_gpio_data, timer); 40 41 gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? 1 : 0); 42 return HRTIMER_NORESTART; 43} 44 45static ssize_t gpio_enable_show(struct device *dev, struct device_attribute *attr, char *buf) 46{ 47 struct timed_gpio_data *gpio_data = dev_get_drvdata(dev); 48 int remaining; 49 50 if (hrtimer_active(&gpio_data->timer)) { 51 ktime_t r = hrtimer_get_remaining(&gpio_data->timer); 52 struct timeval t = ktime_to_timeval(r); 53 remaining = t.tv_sec * 1000 + t.tv_usec; 54 } else 55 remaining = 0; 56 57 return sprintf(buf, "%d\n", remaining); 58} 59 60static ssize_t gpio_enable_store( 61 struct device *dev, struct device_attribute *attr, 62 const char *buf, size_t size) 63{ 64 struct timed_gpio_data *gpio_data = dev_get_drvdata(dev); 65 int value; 66 unsigned long flags; 67 68 sscanf(buf, "%d", &value); 69 70 spin_lock_irqsave(&gpio_data->lock, flags); 71 72 /* cancel previous timer and set GPIO according to value */ 73 hrtimer_cancel(&gpio_data->timer); 74 gpio_direction_output(gpio_data->gpio, gpio_data->active_low ? !value : !!value); 75 76 if (value > 0) { 77 if (value > gpio_data->max_timeout) 78 value = gpio_data->max_timeout; 79 80 hrtimer_start(&gpio_data->timer, 81 ktime_set(value / 1000, (value % 1000) * 1000000), 82 HRTIMER_MODE_REL); 83 } 84 85 spin_unlock_irqrestore(&gpio_data->lock, flags); 86 87 return size; 88} 89 90static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, gpio_enable_show, gpio_enable_store); 91 92static int timed_gpio_probe(struct platform_device *pdev) 93{ 94 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data; 95 struct timed_gpio *cur_gpio; 96 struct timed_gpio_data *gpio_data, *gpio_dat; 97 int i, ret = 0; 98 99 if (!pdata) 100 return -EBUSY; 101 102 gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios, GFP_KERNEL); 103 if (!gpio_data) 104 return -ENOMEM; 105 106 for (i = 0; i < pdata->num_gpios; i++) { 107 cur_gpio = &pdata->gpios[i]; 108 gpio_dat = &gpio_data[i]; 109 110 hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 111 gpio_dat->timer.function = gpio_timer_func; 112 spin_lock_init(&gpio_dat->lock); 113 114 gpio_dat->gpio = cur_gpio->gpio; 115 gpio_dat->max_timeout = cur_gpio->max_timeout; 116 gpio_dat->active_low = cur_gpio->active_low; 117 gpio_direction_output(gpio_dat->gpio, gpio_dat->active_low); 118 119 gpio_dat->dev = device_create(timed_gpio_class, &pdev->dev, 0, "%s", cur_gpio->name); 120 if (unlikely(IS_ERR(gpio_dat->dev))) 121 return PTR_ERR(gpio_dat->dev); 122 123 dev_set_drvdata(gpio_dat->dev, gpio_dat); 124 ret = device_create_file(gpio_dat->dev, &dev_attr_enable); 125 if (ret) 126 return ret; 127 } 128 129 platform_set_drvdata(pdev, gpio_data); 130 131 return 0; 132} 133 134static int timed_gpio_remove(struct platform_device *pdev) 135{ 136 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data; 137 struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev); 138 int i; 139 140 for (i = 0; i < pdata->num_gpios; i++) { 141 device_remove_file(gpio_data[i].dev, &dev_attr_enable); 142 device_unregister(gpio_data[i].dev); 143 } 144 145 kfree(gpio_data); 146 147 return 0; 148} 149 150static struct platform_driver timed_gpio_driver = { 151 .probe = timed_gpio_probe, 152 .remove = timed_gpio_remove, 153 .driver = { 154 .name = "timed-gpio", 155 .owner = THIS_MODULE, 156 }, 157}; 158 159static int __init timed_gpio_init(void) 160{ 161 timed_gpio_class = class_create(THIS_MODULE, "timed_output"); 162 if (IS_ERR(timed_gpio_class)) 163 return PTR_ERR(timed_gpio_class); 164 return platform_driver_register(&timed_gpio_driver); 165} 166 167static void __exit timed_gpio_exit(void) 168{ 169 class_destroy(timed_gpio_class); 170 platform_driver_unregister(&timed_gpio_driver); 171} 172 173module_init(timed_gpio_init); 174module_exit(timed_gpio_exit); 175 176MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); 177MODULE_DESCRIPTION("timed gpio driver"); 178MODULE_LICENSE("GPL"); 179