Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 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> |
Arve Hjønnevåg | c1b197a | 2010-04-20 22:33:05 -0700 | [diff] [blame] | 19 | #include <linux/slab.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 20 | #include <linux/hrtimer.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/gpio.h> |
Somya Anand | d2b7787 | 2014-10-23 21:28:38 +0200 | [diff] [blame] | 23 | #include <linux/ktime.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 24 | |
| 25 | #include "timed_output.h" |
| 26 | #include "timed_gpio.h" |
| 27 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 28 | struct timed_gpio_data { |
| 29 | struct timed_output_dev dev; |
| 30 | struct hrtimer timer; |
| 31 | spinlock_t lock; |
Tracey Dent | e3251e0 | 2012-01-22 21:28:46 -0500 | [diff] [blame] | 32 | unsigned gpio; |
| 33 | int max_timeout; |
| 34 | u8 active_low; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static 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 | |
| 46 | static int gpio_get_time(struct timed_output_dev *dev) |
| 47 | { |
Murilo Opsfelder Araujo | 80039fa | 2014-07-31 08:34:11 -0300 | [diff] [blame] | 48 | struct timed_gpio_data *data; |
Somya Anand | d2b7787 | 2014-10-23 21:28:38 +0200 | [diff] [blame] | 49 | ktime_t t; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 50 | |
Murilo Opsfelder Araujo | 80039fa | 2014-07-31 08:34:11 -0300 | [diff] [blame] | 51 | data = container_of(dev, struct timed_gpio_data, dev); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 52 | |
Murilo Opsfelder Araujo | 80039fa | 2014-07-31 08:34:11 -0300 | [diff] [blame] | 53 | if (!hrtimer_active(&data->timer)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 54 | return 0; |
Murilo Opsfelder Araujo | 80039fa | 2014-07-31 08:34:11 -0300 | [diff] [blame] | 55 | |
Somya Anand | d2b7787 | 2014-10-23 21:28:38 +0200 | [diff] [blame] | 56 | t = hrtimer_get_remaining(&data->timer); |
Murilo Opsfelder Araujo | 80039fa | 2014-07-31 08:34:11 -0300 | [diff] [blame] | 57 | |
Somya Anand | d2b7787 | 2014-10-23 21:28:38 +0200 | [diff] [blame] | 58 | return ktime_to_ms(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static void gpio_enable(struct timed_output_dev *dev, int value) |
| 62 | { |
Jandy Gou | 2537468 | 2015-08-05 10:09:02 +0800 | [diff] [blame] | 63 | struct timed_gpio_data *data = |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 64 | container_of(dev, struct timed_gpio_data, dev); |
Jandy Gou | 2537468 | 2015-08-05 10:09:02 +0800 | [diff] [blame] | 65 | unsigned long flags; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 66 | |
| 67 | spin_lock_irqsave(&data->lock, flags); |
| 68 | |
| 69 | /* cancel previous timer and set GPIO according to value */ |
| 70 | hrtimer_cancel(&data->timer); |
| 71 | gpio_direction_output(data->gpio, data->active_low ? !value : !!value); |
| 72 | |
| 73 | if (value > 0) { |
| 74 | if (value > data->max_timeout) |
| 75 | value = data->max_timeout; |
| 76 | |
| 77 | hrtimer_start(&data->timer, |
Ioana Ciornei | f8b053e | 2015-11-01 16:38:22 +0200 | [diff] [blame] | 78 | ktime_set(value / 1000, (value % 1000) * 1000000), |
| 79 | HRTIMER_MODE_REL); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | spin_unlock_irqrestore(&data->lock, flags); |
| 83 | } |
| 84 | |
| 85 | static int timed_gpio_probe(struct platform_device *pdev) |
| 86 | { |
| 87 | struct timed_gpio_platform_data *pdata = pdev->dev.platform_data; |
| 88 | struct timed_gpio *cur_gpio; |
| 89 | struct timed_gpio_data *gpio_data, *gpio_dat; |
Axel Lin | 5d92f71 | 2012-03-21 09:24:57 +0800 | [diff] [blame] | 90 | int i, ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 91 | |
| 92 | if (!pdata) |
| 93 | return -EBUSY; |
| 94 | |
Amitoj Kaur Chawla | 0103fe2 | 2016-02-25 20:21:20 +0530 | [diff] [blame] | 95 | gpio_data = devm_kcalloc(&pdev->dev, pdata->num_gpios, |
| 96 | sizeof(*gpio_data), GFP_KERNEL); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 97 | if (!gpio_data) |
| 98 | return -ENOMEM; |
| 99 | |
| 100 | for (i = 0; i < pdata->num_gpios; i++) { |
| 101 | cur_gpio = &pdata->gpios[i]; |
| 102 | gpio_dat = &gpio_data[i]; |
| 103 | |
| 104 | hrtimer_init(&gpio_dat->timer, CLOCK_MONOTONIC, |
Ioana Ciornei | f8b053e | 2015-11-01 16:38:22 +0200 | [diff] [blame] | 105 | HRTIMER_MODE_REL); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 106 | gpio_dat->timer.function = gpio_timer_func; |
| 107 | spin_lock_init(&gpio_dat->lock); |
| 108 | |
| 109 | gpio_dat->dev.name = cur_gpio->name; |
| 110 | gpio_dat->dev.get_time = gpio_get_time; |
| 111 | gpio_dat->dev.enable = gpio_enable; |
Arve Hjønnevåg | 0445f15 | 2010-01-06 17:17:33 -0800 | [diff] [blame] | 112 | ret = gpio_request(cur_gpio->gpio, cur_gpio->name); |
Axel Lin | 5d92f71 | 2012-03-21 09:24:57 +0800 | [diff] [blame] | 113 | if (ret < 0) |
| 114 | goto err_out; |
| 115 | ret = timed_output_dev_register(&gpio_dat->dev); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 116 | if (ret < 0) { |
Axel Lin | 5d92f71 | 2012-03-21 09:24:57 +0800 | [diff] [blame] | 117 | gpio_free(cur_gpio->gpio); |
| 118 | goto err_out; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | gpio_dat->gpio = cur_gpio->gpio; |
| 122 | gpio_dat->max_timeout = cur_gpio->max_timeout; |
| 123 | gpio_dat->active_low = cur_gpio->active_low; |
| 124 | gpio_direction_output(gpio_dat->gpio, gpio_dat->active_low); |
| 125 | } |
| 126 | |
| 127 | platform_set_drvdata(pdev, gpio_data); |
| 128 | |
| 129 | return 0; |
Axel Lin | 5d92f71 | 2012-03-21 09:24:57 +0800 | [diff] [blame] | 130 | |
| 131 | err_out: |
| 132 | while (--i >= 0) { |
| 133 | timed_output_dev_unregister(&gpio_data[i].dev); |
| 134 | gpio_free(gpio_data[i].gpio); |
| 135 | } |
Axel Lin | 5d92f71 | 2012-03-21 09:24:57 +0800 | [diff] [blame] | 136 | |
| 137 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static int timed_gpio_remove(struct platform_device *pdev) |
| 141 | { |
| 142 | struct timed_gpio_platform_data *pdata = pdev->dev.platform_data; |
| 143 | struct timed_gpio_data *gpio_data = platform_get_drvdata(pdev); |
| 144 | int i; |
| 145 | |
Arve Hjønnevåg | 0445f15 | 2010-01-06 17:17:33 -0800 | [diff] [blame] | 146 | for (i = 0; i < pdata->num_gpios; i++) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 147 | timed_output_dev_unregister(&gpio_data[i].dev); |
Arve Hjønnevåg | 0445f15 | 2010-01-06 17:17:33 -0800 | [diff] [blame] | 148 | gpio_free(gpio_data[i].gpio); |
| 149 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 150 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static struct platform_driver timed_gpio_driver = { |
| 155 | .probe = timed_gpio_probe, |
| 156 | .remove = timed_gpio_remove, |
| 157 | .driver = { |
| 158 | .name = TIMED_GPIO_NAME, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 159 | }, |
| 160 | }; |
| 161 | |
Devendra Naga | c21be47 | 2012-07-20 22:45:48 +0545 | [diff] [blame] | 162 | module_platform_driver(timed_gpio_driver); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 163 | |
| 164 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
| 165 | MODULE_DESCRIPTION("timed gpio driver"); |
| 166 | MODULE_LICENSE("GPL"); |