Merge branch 'for-2.6.40' of git://linux-nfs.org/~bfields/linux
[linux-2.6-block.git] / drivers / staging / solo6x10 / gpio.c
1 /*
2  * Copyright (C) 2010 Bluecherry, LLC www.bluecherrydvr.com
3  * Copyright (C) 2010 Ben Collins <bcollins@bluecherry.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/fs.h>
22 #include <asm/uaccess.h>
23 #include "solo6x10.h"
24
25 static void solo_gpio_mode(struct solo_dev *solo_dev,
26                            unsigned int port_mask, unsigned int mode)
27 {
28         int port;
29         unsigned int ret;
30
31         ret = solo_reg_read(solo_dev, SOLO_GPIO_CONFIG_0);
32
33         /* To set gpio */
34         for (port = 0; port < 16; port++) {
35                 if (!((1 << port) & port_mask))
36                         continue;
37
38                 ret &= (~(3 << (port << 1)));
39                 ret |= ((mode & 3) << (port << 1));
40         }
41
42         solo_reg_write(solo_dev, SOLO_GPIO_CONFIG_0, ret);
43
44         /* To set extended gpio - sensor */
45         ret = solo_reg_read(solo_dev, SOLO_GPIO_CONFIG_1);
46
47         for (port = 0; port < 16; port++) {
48                 if (!((1 << (port + 16)) & port_mask))
49                         continue;
50
51                 if (!mode)
52                         ret &= ~(1 << port);
53                 else
54                         ret |= 1 << port;
55         }
56
57         solo_reg_write(solo_dev, SOLO_GPIO_CONFIG_1, ret);
58 }
59
60 static void solo_gpio_set(struct solo_dev *solo_dev, unsigned int value)
61 {
62         solo_reg_write(solo_dev, SOLO_GPIO_DATA_OUT,
63                        solo_reg_read(solo_dev, SOLO_GPIO_DATA_OUT) | value);
64 }
65
66 static void solo_gpio_clear(struct solo_dev *solo_dev, unsigned int value)
67 {
68         solo_reg_write(solo_dev, SOLO_GPIO_DATA_OUT,
69                        solo_reg_read(solo_dev, SOLO_GPIO_DATA_OUT) & ~value);
70 }
71
72 static void solo_gpio_config(struct solo_dev *solo_dev)
73 {
74         /* Video reset */
75         solo_gpio_mode(solo_dev, 0x30, 1);
76         solo_gpio_clear(solo_dev, 0x30);
77         udelay(100);
78         solo_gpio_set(solo_dev, 0x30);
79         udelay(100);
80
81         /* Warning: Don't touch the next line unless you're sure of what
82          * you're doing: first four gpio [0-3] are used for video. */
83         solo_gpio_mode(solo_dev, 0x0f, 2);
84
85         /* We use bit 8-15 of SOLO_GPIO_CONFIG_0 for relay purposes */
86         solo_gpio_mode(solo_dev, 0xff00, 1);
87
88         /* Initially set relay status to 0 */
89         solo_gpio_clear(solo_dev, 0xff00);
90 }
91
92 int solo_gpio_init(struct solo_dev *solo_dev)
93 {
94         solo_gpio_config(solo_dev);
95         return 0;
96 }
97
98 void solo_gpio_exit(struct solo_dev *solo_dev)
99 {
100         solo_gpio_clear(solo_dev, 0x30);
101         solo_gpio_config(solo_dev);
102 }