timed_gpio.c revision 0445f1548fc66a72f3b91cdbe8f26b120245efd1
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/slab.h> 20#include <linux/hrtimer.h> 21#include <linux/err.h> 22#include <linux/gpio.h> 23 24#include "timed_output.h" 25#include "timed_gpio.h" 26 27 28struct timed_gpio_data { 29 struct timed_output_dev 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 *data = 40 container_of(timer, struct timed_gpio_data, timer); 41 42 gpio_direction_output(data->gpio, data->active_low ? 1 : 0); 43 return HRTIMER_NORESTART; 44} 45 46static int gpio_get_time(struct timed_output_dev *dev) 47{ 48 struct timed_gpio_data *data = 49 container_of(dev, struct timed_gpio_data, dev); 50 51 if (hrtimer_active(&data->timer)) { 52 ktime_t r = hrtimer_get_remaining(&data->timer); 53 struct timeval t = ktime_to_timeval(r); 54 return t.tv_sec * 1000 + t.tv_usec / 1000; 55 } else 56 return 0; 57} 58 59static void gpio_enable(struct timed_output_dev *dev, int value) 60{ 61 struct timed_gpio_data *data = 62 container_of(dev, struct timed_gpio_data, dev); 63 unsigned long flags; 64 65 spin_lock_irqsave(&data->lock, flags); 66 67 /* cancel previous timer and set GPIO according to value */ 68 hrtimer_cancel(&data->timer); 69 gpio_direction_output(data->gpio, data->active_low ? !value : !!value); 70 71 if (value > 0) { 72 if (value > data->max_timeout) 73 value = data->max_timeout; 74 75 hrtimer_start(&data->timer, 76 ktime_set(value / 1000, (value % 1000) * 1000000), 77 HRTIMER_MODE_REL); 78 } 79 80 spin_unlock_irqrestore(&data->lock, flags); 81} 82 83static int timed_gpio_probe(struct platform_device *pdev) 84{ 85 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data; 86 struct timed_gpio *cur_gpio; 87 struct timed_gpio_data *gpio_data, *gpio_dat; 88 int i, j, ret = 0; 89 90 if (!pdata) 91 return -EBUSY; 92 93 gpio_data = kzalloc(sizeof(struct timed_gpio_data) * pdata->num_gpios, 94 GFP_KERNEL); 95 if (!gpio_data) 96 return -ENOMEM; 97 98 for (i = 0; i < pdata->num_gpios; i++) { 99 cur_gpio = &pdata->gpios[i]; 100 gpio_dat = &gpio_data[i]; 101 102 hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC, 103 HRTIMER_MODE_REL); 104 gpio_dat->timer.function = gpio_timer_func; 105 spin_lock_init(&gpio_dat->lock); 106 107 gpio_dat->dev.name = cur_gpio->name; 108 gpio_dat->dev.get_time = gpio_get_time; 109 gpio_dat->dev.enable = gpio_enable; 110 ret = gpio_request(cur_gpio->gpio, cur_gpio->name); 111 if (ret >= 0) { 112 ret = timed_output_dev_register(&gpio_dat->dev); 113 if (ret < 0) 114 gpio_free(cur_gpio->gpio); 115 } 116 if (ret < 0) { 117 for (j = 0; j < i; j++) { 118 timed_output_dev_unregister(&gpio_data[i].dev); 119 gpio_free(gpio_data[i].gpio); 120 } 121 kfree(gpio_data); 122 return ret; 123 } 124 125 gpio_dat->gpio = cur_gpio->gpio; 126 gpio_dat->max_timeout = cur_gpio->max_timeout; 127 gpio_dat->active_low = cur_gpio->active_low; 128 gpio_direction_output(gpio_dat->gpio, gpio_dat->active_low); 129 } 130 131 platform_set_drvdata(pdev, gpio_data); 132 133 return 0; 134} 135 136static int timed_gpio_remove(struct platform_device *pdev) 137{ 138 struct timed_gpio_platform_data *pdata = pdev->dev.platform_data; 139 struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev); 140 int i; 141 142 for (i = 0; i < pdata->num_gpios; i++) { 143 timed_output_dev_unregister(&gpio_data[i].dev); 144 gpio_free(gpio_data[i].gpio); 145 } 146 147 kfree(gpio_data); 148 149 return 0; 150} 151 152static struct platform_driver timed_gpio_driver = { 153 .probe = timed_gpio_probe, 154 .remove = timed_gpio_remove, 155 .driver = { 156 .name = TIMED_GPIO_NAME, 157 .owner = THIS_MODULE, 158 }, 159}; 160 161static int __init timed_gpio_init(void) 162{ 163 return platform_driver_register(&timed_gpio_driver); 164} 165 166static void __exit timed_gpio_exit(void) 167{ 168 platform_driver_unregister(&timed_gpio_driver); 169} 170 171module_init(timed_gpio_init); 172module_exit(timed_gpio_exit); 173 174MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); 175MODULE_DESCRIPTION("timed gpio driver"); 176MODULE_LICENSE("GPL"); 177