1 /*
2  * isl6405.c - driver for dual lnb supply and control ic ISL6405
3  *
4  * Copyright (C) 2008 Hartmut Hackmann
5  * Copyright (C) 2006 Oliver Endriss
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 "isl6405.h"
37 
38 struct isl6405 {
39 	u8			config;
40 	u8			override_or;
41 	u8			override_and;
42 	struct i2c_adapter	*i2c;
43 	u8			i2c_addr;
44 };
45 
46 static int isl6405_set_voltage(struct dvb_frontend *fe,
47 			       enum fe_sec_voltage voltage)
48 {
49 	struct isl6405 *isl6405 = (struct isl6405 *) fe->sec_priv;
50 	struct i2c_msg msg = {	.addr = isl6405->i2c_addr, .flags = 0,
51 				.buf = &isl6405->config,
52 				.len = sizeof(isl6405->config) };
53 
54 	if (isl6405->override_or & 0x80) {
55 		isl6405->config &= ~(ISL6405_VSEL2 | ISL6405_EN2);
56 		switch (voltage) {
57 		case SEC_VOLTAGE_OFF:
58 			break;
59 		case SEC_VOLTAGE_13:
60 			isl6405->config |= ISL6405_EN2;
61 			break;
62 		case SEC_VOLTAGE_18:
63 			isl6405->config |= (ISL6405_EN2 | ISL6405_VSEL2);
64 			break;
65 		default:
66 			return -EINVAL;
67 		}
68 	} else {
69 		isl6405->config &= ~(ISL6405_VSEL1 | ISL6405_EN1);
70 		switch (voltage) {
71 		case SEC_VOLTAGE_OFF:
72 			break;
73 		case SEC_VOLTAGE_13:
74 			isl6405->config |= ISL6405_EN1;
75 			break;
76 		case SEC_VOLTAGE_18:
77 			isl6405->config |= (ISL6405_EN1 | ISL6405_VSEL1);
78 			break;
79 		default:
80 			return -EINVAL;
81 		}
82 	}
83 	isl6405->config |= isl6405->override_or;
84 	isl6405->config &= isl6405->override_and;
85 
86 	return (i2c_transfer(isl6405->i2c, &msg, 1) == 1) ? 0 : -EIO;
87 }
88 
89 static int isl6405_enable_high_lnb_voltage(struct dvb_frontend *fe, long arg)
90 {
91 	struct isl6405 *isl6405 = (struct isl6405 *) fe->sec_priv;
92 	struct i2c_msg msg = {	.addr = isl6405->i2c_addr, .flags = 0,
93 				.buf = &isl6405->config,
94 				.len = sizeof(isl6405->config) };
95 
96 	if (isl6405->override_or & 0x80) {
97 		if (arg)
98 			isl6405->config |= ISL6405_LLC2;
99 		else
100 			isl6405->config &= ~ISL6405_LLC2;
101 	} else {
102 		if (arg)
103 			isl6405->config |= ISL6405_LLC1;
104 		else
105 			isl6405->config &= ~ISL6405_LLC1;
106 	}
107 	isl6405->config |= isl6405->override_or;
108 	isl6405->config &= isl6405->override_and;
109 
110 	return (i2c_transfer(isl6405->i2c, &msg, 1) == 1) ? 0 : -EIO;
111 }
112 
113 static void isl6405_release(struct dvb_frontend *fe)
114 {
115 	/* power off */
116 	isl6405_set_voltage(fe, SEC_VOLTAGE_OFF);
117 
118 	/* free */
119 	kfree(fe->sec_priv);
120 	fe->sec_priv = NULL;
121 }
122 
123 struct dvb_frontend *isl6405_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c,
124 				    u8 i2c_addr, u8 override_set, u8 override_clear)
125 {
126 	struct isl6405 *isl6405 = kmalloc(sizeof(struct isl6405), GFP_KERNEL);
127 	if (!isl6405)
128 		return NULL;
129 
130 	/* default configuration */
131 	if (override_set & 0x80)
132 		isl6405->config = ISL6405_ISEL2;
133 	else
134 		isl6405->config = ISL6405_ISEL1;
135 	isl6405->i2c = i2c;
136 	isl6405->i2c_addr = i2c_addr;
137 	fe->sec_priv = isl6405;
138 
139 	/* bits which should be forced to '1' */
140 	isl6405->override_or = override_set;
141 
142 	/* bits which should be forced to '0' */
143 	isl6405->override_and = ~override_clear;
144 
145 	/* detect if it is present or not */
146 	if (isl6405_set_voltage(fe, SEC_VOLTAGE_OFF)) {
147 		kfree(isl6405);
148 		fe->sec_priv = NULL;
149 		return NULL;
150 	}
151 
152 	/* install release callback */
153 	fe->ops.release_sec = isl6405_release;
154 
155 	/* override frontend ops */
156 	fe->ops.set_voltage = isl6405_set_voltage;
157 	fe->ops.enable_high_lnb_voltage = isl6405_enable_high_lnb_voltage;
158 
159 	return fe;
160 }
161 EXPORT_SYMBOL(isl6405_attach);
162 
163 MODULE_DESCRIPTION("Driver for lnb supply and control ic isl6405");
164 MODULE_AUTHOR("Hartmut Hackmann & Oliver Endriss");
165 MODULE_LICENSE("GPL");
166