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