Initial commit
[kernel/linux-3.0.git] / drivers / media / isdbt / fc8100 / fc8100.c
1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/kernel.h>
4 #include <linux/fs.h>
5 #include <linux/errno.h>
6 #include <linux/types.h>
7 #include <linux/fcntl.h>
8 #include <linux/miscdevice.h>
9
10 #include <asm/uaccess.h>
11 #include <asm/io.h>
12
13 #include <linux/delay.h>
14 #include <linux/gpio.h>
15
16 #include <plat/gpio-cfg.h>
17 #include <plat/gpio-core.h>
18 #include <mach/regs-gpio.h>
19 #include <mach/gpio.h>
20
21 #include "fc8100.h"
22 #include "bbm.h"
23 #include "fci_oal.h"
24
25 #define FC8100_NAME             "isdbt"
26
27 int dmb_open (struct inode *inode, struct file *filp);
28 loff_t dmb_llseek (struct file *filp, loff_t off, int whence);
29 ssize_t dmb_read (struct file *filp, char *buf, size_t count, loff_t *f_pos);
30 ssize_t dmb_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos);
31 static long dmb_ioctl (struct file *filp, unsigned int cmd, unsigned long arg);
32 int dmb_release (struct inode *inode, struct file *filp);
33
34 /* GPIO Setting */
35 void dmb_hw_setting(void);
36 void dmb_hw_init(void);
37 void dmb_hw_deinit(void);
38
39 static struct file_operations dmb_fops = {
40         .owner          = THIS_MODULE,
41         .llseek         = dmb_llseek,
42         .read           = dmb_read,
43         .write          = dmb_write,
44         .unlocked_ioctl         = dmb_ioctl,
45         .open           = dmb_open,
46         .release        = dmb_release,
47 };
48
49 static struct miscdevice fc8100_misc_device = {
50     .minor = MISC_DYNAMIC_MINOR,
51     .name = FC8100_NAME,
52     .fops = &dmb_fops,
53 };
54
55 int dmb_open (struct inode *inode, struct file *filp)
56 {
57         int num = MINOR(inode->i_rdev);
58
59         /* ISDBT PWR ENABLE  FOR I2C*/
60         dmb_hw_setting();
61         dmb_hw_init();
62
63         PRINTF(0, "dmb open -> minor : %d\n", num);
64
65         return 0;
66 }
67
68 loff_t dmb_llseek (struct file *filp, loff_t off, int whence)
69 {
70         PRINTF(0, "dmb llseek -> off : %08X, whence : %08X\n", off, whence);
71         return 0x23;
72 }
73
74 ssize_t dmb_read (struct file *filp, char *buf, size_t count, loff_t *f_pos)
75 {
76         PRINTF(0, "dmb read -> buf : %08X, count : %08X \n", buf, count);
77         return 0x33;
78 }
79
80 ssize_t dmb_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos)
81 {
82         PRINTF(0, "dmb write -> buf : %08X, count : %08X \n", buf, count);
83         return 0x43;
84 }
85
86 static long dmb_ioctl (struct file *filp, unsigned int cmd, unsigned long arg)
87 {
88         s32 res = BBM_NOK;
89         int status = 0;
90         int status_usr = 0;
91         ioctl_info info;
92         int size;
93
94         if (_IOC_TYPE(cmd) != IOCTL_MAGIC)
95                         return -EINVAL;
96         if (_IOC_NR(cmd) >= IOCTL_MAXNR)
97                         return -EINVAL;
98
99         size = _IOC_SIZE(cmd);
100
101         switch (cmd) {
102                 case IOCTL_DMB_RESET:
103                         res = BBM_RESET(0);
104                         break;
105                 case IOCTL_DMB_INIT:
106                         res = BBM_INIT(0);
107                         break;
108                 case IOCTL_DMB_BYTE_READ:
109                         status = copy_from_user((void *)&info, (void *)arg, size);
110                         res = BBM_BYTE_READ(0, (u16)info.buff[0], (u8 *)(&info.buff[1]));
111                         status_usr = copy_to_user((void *)arg, (void *)&info, size);
112                         break;
113                 case IOCTL_DMB_WORD_READ:
114                         status = copy_from_user((void *)&info, (void *)arg, size);
115                         res = BBM_WORD_READ(0, (u16)info.buff[0], (u16 *)(&info.buff[1]));
116                         status_usr = copy_to_user((void *)arg, (void *)&info, size);
117                         break;
118                 case IOCTL_DMB_LONG_READ:
119                         status = copy_from_user((void *)&info, (void *)arg, size);
120                         res = BBM_LONG_READ(0, (u16)info.buff[0], (u32 *)(&info.buff[1]));
121                         status_usr = copy_to_user((void *)arg, (void *)&info, size);
122                         break;
123                 case IOCTL_DMB_BULK_READ:
124                         status = copy_from_user((void *)&info, (void *)arg, size);
125                         res = BBM_BULK_READ(0, (u16)info.buff[0], (u8 *)(&info.buff[2]), info.buff[1]);
126                         status_usr = copy_to_user((void *)arg, (void *)&info, size);
127                         break;
128                 case IOCTL_DMB_BYTE_WRITE:
129                         status = copy_from_user((void *)&info, (void *)arg, size);
130                         res = BBM_BYTE_WRITE(0, (u16)info.buff[0], (u8)info.buff[1]);
131                         break;
132                 case IOCTL_DMB_WORD_WRITE:
133                         status = copy_from_user((void *)&info, (void *)arg, size);
134                         res = BBM_WORD_WRITE(0, (u16)info.buff[0], (u16)info.buff[1]);
135                         break;
136                 case IOCTL_DMB_LONG_WRITE:
137                         status = copy_from_user((void *)&info, (void *)arg, size);
138                         res = BBM_LONG_WRITE(0, (u16)info.buff[0], (u32)info.buff[1]);
139                         break;
140                 case IOCTL_DMB_BULK_WRITE:
141                         status = copy_from_user((void *)&info, (void *)arg, size);
142                         res = BBM_BULK_WRITE(0, (u16)info.buff[0], (u8 *)(&info.buff[2]), info.buff[1]);
143                         break;
144                 case IOCTL_DMB_TUNER_SELECT:
145                         status = copy_from_user((void *)&info, (void *)arg, size);
146                         res = BBM_TUNER_SELECT(0, (u32)info.buff[0], 0);
147                         break;
148                 case IOCTL_DMB_TUNER_READ:
149                         status = copy_from_user((void *)&info, (void *)arg, size);
150                         res = BBM_TUNER_READ(0, (u8)info.buff[0], (u8)info.buff[1],  (u8 *)(&info.buff[3]), (u8)info.buff[2]);
151                         status_usr = copy_to_user((void *)arg, (void *)&info, size);
152                         break;
153                 case IOCTL_DMB_TUNER_WRITE:
154                         status = copy_from_user((void *)&info, (void *)arg, size);
155                         res = BBM_TUNER_WRITE(0, (u8)info.buff[0], (u8)info.buff[1], (u8 *)(&info.buff[3]), (u8)info.buff[2]);
156                         break;
157                 case IOCTL_DMB_TUNER_SET_FREQ:
158                         status = copy_from_user((void *)&info, (void *)arg, size);
159                         res = BBM_TUNER_SET_FREQ(0, (u8)info.buff[0]);
160                         break;
161                 case IOCTL_DMB_POWER_ON:
162                         PRINTF(0, "DMB_POWER_ON enter..\n");
163                         dmb_hw_init();
164                         break;
165                 case IOCTL_DMB_POWER_OFF:
166                         PRINTF(0, "DMB_POWER_OFF enter..\n");
167                         dmb_hw_deinit();
168                         break;
169         }
170         if ((status < 0) || (status_usr < 0)) {
171                 PRINTF(0, " copy to user or copy from user : ERROR..\n");
172                 return -EINVAL;
173         }
174
175         /* return status */
176         if (res < 0)
177                         res = -BBM_NOK;
178         else
179                         res = BBM_OK;
180
181         return res;
182 }
183
184 int dmb_release(struct inode *inode, struct file *filp)
185 {
186         /* ISDBT PWR ENABLE  FOR I2C*/
187         PRINTF(0, "dmb release\n");
188         return 0;
189 }
190
191 void dmb_hw_setting(void)
192 {
193         s3c_gpio_cfgpin(GPIO_ISDBT_SDA_28V, S3C_GPIO_INPUT);
194         s3c_gpio_setpull(GPIO_ISDBT_SDA_28V, S3C_GPIO_PULL_NONE);
195         s3c_gpio_cfgpin(GPIO_ISDBT_SCL_28V, S3C_GPIO_INPUT);
196         s3c_gpio_setpull(GPIO_ISDBT_SCL_28V, S3C_GPIO_PULL_NONE);
197         s3c_gpio_cfgpin(GPIO_ISDBT_PWR_EN, S3C_GPIO_OUTPUT);
198         gpio_set_value(GPIO_ISDBT_PWR_EN, GPIO_LEVEL_LOW);
199         s3c_gpio_cfgpin(GPIO_ISDBT_RST, S3C_GPIO_OUTPUT);
200         gpio_set_value(GPIO_ISDBT_RST, GPIO_LEVEL_HIGH);
201 }
202
203 void dmb_init_hw_setting(void)
204 {
205         s3c_gpio_cfgpin(GPIO_ISDBT_SDA_28V, S3C_GPIO_INPUT);
206         s3c_gpio_setpull(GPIO_ISDBT_SDA_28V, S3C_GPIO_PULL_NONE);
207         s3c_gpio_cfgpin(GPIO_ISDBT_SCL_28V, S3C_GPIO_INPUT);
208         s3c_gpio_setpull(GPIO_ISDBT_SCL_28V, S3C_GPIO_PULL_NONE);
209         s3c_gpio_cfgpin(GPIO_ISDBT_PWR_EN, S3C_GPIO_OUTPUT);
210         gpio_set_value(GPIO_ISDBT_PWR_EN, GPIO_LEVEL_LOW);
211         s3c_gpio_cfgpin(GPIO_ISDBT_RST, S3C_GPIO_OUTPUT);
212         gpio_set_value(GPIO_ISDBT_RST, GPIO_LEVEL_LOW);
213 }
214
215 void dmb_hw_init(void)
216 {
217         mdelay(1);
218         gpio_set_value(GPIO_ISDBT_PWR_EN, GPIO_LEVEL_HIGH);
219         mdelay(1);
220         gpio_set_value(GPIO_ISDBT_RST, GPIO_LEVEL_HIGH);
221         mdelay(100);
222 }
223
224 void dmb_hw_deinit(void)
225 {
226         mdelay(1);
227         gpio_set_value(GPIO_ISDBT_RST, GPIO_LEVEL_LOW);
228         mdelay(1);
229         gpio_set_value(GPIO_ISDBT_PWR_EN, GPIO_LEVEL_LOW);
230         mdelay(100);
231 }
232
233
234 int dmb_init(void)
235 {
236         int result;
237
238         PRINTF(0, "dmb dmb_init\n");
239
240         dmb_init_hw_setting();
241 /*
242         dmb_hw_init();
243 */
244         /*misc device registration*/
245         result = misc_register(&fc8100_misc_device);
246
247         if (result < 0)
248                 return result;
249
250         BBM_HOSTIF_SELECT(0, BBM_I2C);
251
252         dmb_hw_deinit();
253
254         return 0;
255 }
256
257 void dmb_exit(void)
258 {
259         PRINTF(0, "dmb dmb_exit\n");
260
261         BBM_HOSTIF_DESELECT(0);
262         dmb_hw_deinit();
263
264         /*misc device deregistration*/
265         misc_deregister(&fc8100_misc_device);
266 }
267
268 module_init(dmb_init);
269 module_exit(dmb_exit);
270
271 MODULE_LICENSE("Dual BSD/GPL");