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