V4L/DVB (11310): cx18: remove intermediate 'ioctl' step
[linux-2.6] / drivers / media / dvb / frontends / lnbp21.c
1 /*
2  * lnbp21.c - driver for lnb supply and control ic lnbp21
3  *
4  * Copyright (C) 2006 Oliver Endriss
5  * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
23  *
24  *
25  * the project's page is at http://www.linuxtv.org
26  */
27 #include <linux/delay.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/string.h>
33 #include <linux/slab.h>
34
35 #include "dvb_frontend.h"
36 #include "lnbp21.h"
37 #include "lnbh24.h"
38
39 struct lnbp21 {
40         u8                      config;
41         u8                      override_or;
42         u8                      override_and;
43         struct i2c_adapter      *i2c;
44         u8                      i2c_addr;
45 };
46
47 static int lnbp21_set_voltage(struct dvb_frontend *fe,
48                                         fe_sec_voltage_t voltage)
49 {
50         struct lnbp21 *lnbp21 = (struct lnbp21 *) fe->sec_priv;
51         struct i2c_msg msg = {  .addr = lnbp21->i2c_addr, .flags = 0,
52                                 .buf = &lnbp21->config,
53                                 .len = sizeof(lnbp21->config) };
54
55         lnbp21->config &= ~(LNBP21_VSEL | LNBP21_EN);
56
57         switch(voltage) {
58         case SEC_VOLTAGE_OFF:
59                 break;
60         case SEC_VOLTAGE_13:
61                 lnbp21->config |= LNBP21_EN;
62                 break;
63         case SEC_VOLTAGE_18:
64                 lnbp21->config |= (LNBP21_EN | LNBP21_VSEL);
65                 break;
66         default:
67                 return -EINVAL;
68         };
69
70         lnbp21->config |= lnbp21->override_or;
71         lnbp21->config &= lnbp21->override_and;
72
73         return (i2c_transfer(lnbp21->i2c, &msg, 1) == 1) ? 0 : -EIO;
74 }
75
76 static int lnbp21_enable_high_lnb_voltage(struct dvb_frontend *fe, long arg)
77 {
78         struct lnbp21 *lnbp21 = (struct lnbp21 *) fe->sec_priv;
79         struct i2c_msg msg = {  .addr = lnbp21->i2c_addr, .flags = 0,
80                                 .buf = &lnbp21->config,
81                                 .len = sizeof(lnbp21->config) };
82
83         if (arg)
84                 lnbp21->config |= LNBP21_LLC;
85         else
86                 lnbp21->config &= ~LNBP21_LLC;
87
88         lnbp21->config |= lnbp21->override_or;
89         lnbp21->config &= lnbp21->override_and;
90
91         return (i2c_transfer(lnbp21->i2c, &msg, 1) == 1) ? 0 : -EIO;
92 }
93
94 static void lnbp21_release(struct dvb_frontend *fe)
95 {
96         /* LNBP power off */
97         lnbp21_set_voltage(fe, SEC_VOLTAGE_OFF);
98
99         /* free data */
100         kfree(fe->sec_priv);
101         fe->sec_priv = NULL;
102 }
103
104 static struct dvb_frontend *lnbx2x_attach(struct dvb_frontend *fe,
105                                 struct i2c_adapter *i2c, u8 override_set,
106                                 u8 override_clear, u8 i2c_addr, u8 config)
107 {
108         struct lnbp21 *lnbp21 = kmalloc(sizeof(struct lnbp21), GFP_KERNEL);
109         if (!lnbp21)
110                 return NULL;
111
112         /* default configuration */
113         lnbp21->config = config;
114         lnbp21->i2c = i2c;
115         lnbp21->i2c_addr = i2c_addr;
116         fe->sec_priv = lnbp21;
117
118         /* bits which should be forced to '1' */
119         lnbp21->override_or = override_set;
120
121         /* bits which should be forced to '0' */
122         lnbp21->override_and = ~override_clear;
123
124         /* detect if it is present or not */
125         if (lnbp21_set_voltage(fe, SEC_VOLTAGE_OFF)) {
126                 kfree(lnbp21);
127                 return NULL;
128         }
129
130         /* install release callback */
131         fe->ops.release_sec = lnbp21_release;
132
133         /* override frontend ops */
134         fe->ops.set_voltage = lnbp21_set_voltage;
135         fe->ops.enable_high_lnb_voltage = lnbp21_enable_high_lnb_voltage;
136         printk(KERN_INFO "LNBx2x attached on addr=%x", lnbp21->i2c_addr);
137
138         return fe;
139 }
140
141 struct dvb_frontend *lnbh24_attach(struct dvb_frontend *fe,
142                                 struct i2c_adapter *i2c, u8 override_set,
143                                 u8 override_clear, u8 i2c_addr)
144 {
145         return lnbx2x_attach(fe, i2c, override_set, override_clear,
146                                                         i2c_addr, LNBH24_TTX);
147 }
148 EXPORT_SYMBOL(lnbh24_attach);
149
150 struct dvb_frontend *lnbp21_attach(struct dvb_frontend *fe,
151                                 struct i2c_adapter *i2c, u8 override_set,
152                                 u8 override_clear)
153 {
154         return lnbx2x_attach(fe, i2c, override_set, override_clear,
155                                                         0x08, LNBP21_ISEL);
156 }
157 EXPORT_SYMBOL(lnbp21_attach);
158
159 MODULE_DESCRIPTION("Driver for lnb supply and control ic lnbp21, lnbh24");
160 MODULE_AUTHOR("Oliver Endriss, Igor M. Liplianin");
161 MODULE_LICENSE("GPL");