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