1/*
2 * lnbp22.h - driver for lnb supply and control ic lnbp22
3 *
4 * Copyright (C) 2006 Dominik Kuhlen
5 * Based on lnbp21 driver
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/moduleparam.h>
33#include <linux/string.h>
34#include <linux/slab.h>
35
36#include "dvb_frontend.h"
37#include "lnbp22.h"
38
39static int debug;
40module_param(debug, int, 0644);
41MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
42
43
44#define dprintk(lvl, arg...) if (debug >= (lvl)) printk(arg)
45
46struct lnbp22 {
47	u8		    config[4];
48	struct i2c_adapter *i2c;
49};
50
51static int lnbp22_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
52{
53	struct lnbp22 *lnbp22 = (struct lnbp22 *)fe->sec_priv;
54	struct i2c_msg msg = {
55		.addr = 0x08,
56		.flags = 0,
57		.buf = (char *)&lnbp22->config,
58		.len = sizeof(lnbp22->config),
59	};
60
61	dprintk(1, "%s: %d (18V=%d 13V=%d)\n", __func__, voltage,
62	       SEC_VOLTAGE_18, SEC_VOLTAGE_13);
63
64	lnbp22->config[3] = 0x60; /* Power down */
65	switch (voltage) {
66	case SEC_VOLTAGE_OFF:
67		break;
68	case SEC_VOLTAGE_13:
69		lnbp22->config[3] |= LNBP22_EN;
70		break;
71	case SEC_VOLTAGE_18:
72		lnbp22->config[3] |= (LNBP22_EN | LNBP22_VSEL);
73		break;
74	default:
75		return -EINVAL;
76	}
77
78	dprintk(1, "%s: 0x%02x)\n", __func__, lnbp22->config[3]);
79	return (i2c_transfer(lnbp22->i2c, &msg, 1) == 1) ? 0 : -EIO;
80}
81
82static int lnbp22_enable_high_lnb_voltage(struct dvb_frontend *fe, long arg)
83{
84	struct lnbp22 *lnbp22 = (struct lnbp22 *) fe->sec_priv;
85	struct i2c_msg msg = {
86		.addr = 0x08,
87		.flags = 0,
88		.buf = (char *)&lnbp22->config,
89		.len = sizeof(lnbp22->config),
90	};
91
92	dprintk(1, "%s: %d\n", __func__, (int)arg);
93	if (arg)
94		lnbp22->config[3] |= LNBP22_LLC;
95	else
96		lnbp22->config[3] &= ~LNBP22_LLC;
97
98	return (i2c_transfer(lnbp22->i2c, &msg, 1) == 1) ? 0 : -EIO;
99}
100
101static void lnbp22_release(struct dvb_frontend *fe)
102{
103	dprintk(1, "%s\n", __func__);
104	/* LNBP power off */
105	lnbp22_set_voltage(fe, SEC_VOLTAGE_OFF);
106
107	/* free data */
108	kfree(fe->sec_priv);
109	fe->sec_priv = NULL;
110}
111
112struct dvb_frontend *lnbp22_attach(struct dvb_frontend *fe,
113					struct i2c_adapter *i2c)
114{
115	struct lnbp22 *lnbp22 = kmalloc(sizeof(struct lnbp22), GFP_KERNEL);
116	if (!lnbp22)
117		return NULL;
118
119	/* default configuration */
120	lnbp22->config[0] = 0x00; /* ? */
121	lnbp22->config[1] = 0x28; /* ? */
122	lnbp22->config[2] = 0x48; /* ? */
123	lnbp22->config[3] = 0x60; /* Power down */
124	lnbp22->i2c = i2c;
125	fe->sec_priv = lnbp22;
126
127	/* detect if it is present or not */
128	if (lnbp22_set_voltage(fe, SEC_VOLTAGE_OFF)) {
129		dprintk(0, "%s LNBP22 not found\n", __func__);
130		kfree(lnbp22);
131		fe->sec_priv = NULL;
132		return NULL;
133	}
134
135	/* install release callback */
136	fe->ops.release_sec = lnbp22_release;
137
138	/* override frontend ops */
139	fe->ops.set_voltage = lnbp22_set_voltage;
140	fe->ops.enable_high_lnb_voltage = lnbp22_enable_high_lnb_voltage;
141
142	return fe;
143}
144EXPORT_SYMBOL(lnbp22_attach);
145
146MODULE_DESCRIPTION("Driver for lnb supply and control ic lnbp22");
147MODULE_AUTHOR("Dominik Kuhlen");
148MODULE_LICENSE("GPL");
149