19a0bf528SMauro Carvalho Chehab /*
29a0bf528SMauro Carvalho Chehab  * lnbp21.c - driver for lnb supply and control ic lnbp21
39a0bf528SMauro Carvalho Chehab  *
49a0bf528SMauro Carvalho Chehab  * Copyright (C) 2006, 2009 Oliver Endriss <o.endriss@gmx.de>
59a0bf528SMauro Carvalho Chehab  * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
69a0bf528SMauro Carvalho Chehab  *
79a0bf528SMauro Carvalho Chehab  * This program is free software; you can redistribute it and/or
89a0bf528SMauro Carvalho Chehab  * modify it under the terms of the GNU General Public License
99a0bf528SMauro Carvalho Chehab  * as published by the Free Software Foundation; either version 2
109a0bf528SMauro Carvalho Chehab  * of the License, or (at your option) any later version.
119a0bf528SMauro Carvalho Chehab  *
129a0bf528SMauro Carvalho Chehab  *
139a0bf528SMauro Carvalho Chehab  * This program is distributed in the hope that it will be useful,
149a0bf528SMauro Carvalho Chehab  * but WITHOUT ANY WARRANTY; without even the implied warranty of
159a0bf528SMauro Carvalho Chehab  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
169a0bf528SMauro Carvalho Chehab  * GNU General Public License for more details.
179a0bf528SMauro Carvalho Chehab  *
189a0bf528SMauro Carvalho Chehab  *
199a0bf528SMauro Carvalho Chehab  * You should have received a copy of the GNU General Public License
209a0bf528SMauro Carvalho Chehab  * along with this program; if not, write to the Free Software
219a0bf528SMauro Carvalho Chehab  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
229a0bf528SMauro Carvalho Chehab  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
239a0bf528SMauro Carvalho Chehab  *
249a0bf528SMauro Carvalho Chehab  *
259a0bf528SMauro Carvalho Chehab  * the project's page is at http://www.linuxtv.org
269a0bf528SMauro Carvalho Chehab  */
279a0bf528SMauro Carvalho Chehab #include <linux/delay.h>
289a0bf528SMauro Carvalho Chehab #include <linux/errno.h>
299a0bf528SMauro Carvalho Chehab #include <linux/init.h>
309a0bf528SMauro Carvalho Chehab #include <linux/kernel.h>
319a0bf528SMauro Carvalho Chehab #include <linux/module.h>
329a0bf528SMauro Carvalho Chehab #include <linux/string.h>
339a0bf528SMauro Carvalho Chehab #include <linux/slab.h>
349a0bf528SMauro Carvalho Chehab 
359a0bf528SMauro Carvalho Chehab #include "dvb_frontend.h"
369a0bf528SMauro Carvalho Chehab #include "lnbp21.h"
379a0bf528SMauro Carvalho Chehab #include "lnbh24.h"
389a0bf528SMauro Carvalho Chehab 
399a0bf528SMauro Carvalho Chehab struct lnbp21 {
409a0bf528SMauro Carvalho Chehab 	u8			config;
419a0bf528SMauro Carvalho Chehab 	u8			override_or;
429a0bf528SMauro Carvalho Chehab 	u8			override_and;
439a0bf528SMauro Carvalho Chehab 	struct i2c_adapter	*i2c;
449a0bf528SMauro Carvalho Chehab 	u8			i2c_addr;
459a0bf528SMauro Carvalho Chehab };
469a0bf528SMauro Carvalho Chehab 
479a0bf528SMauro Carvalho Chehab static int lnbp21_set_voltage(struct dvb_frontend *fe,
489a0bf528SMauro Carvalho Chehab 					fe_sec_voltage_t voltage)
499a0bf528SMauro Carvalho Chehab {
509a0bf528SMauro Carvalho Chehab 	struct lnbp21 *lnbp21 = (struct lnbp21 *) fe->sec_priv;
519a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {	.addr = lnbp21->i2c_addr, .flags = 0,
529a0bf528SMauro Carvalho Chehab 				.buf = &lnbp21->config,
539a0bf528SMauro Carvalho Chehab 				.len = sizeof(lnbp21->config) };
549a0bf528SMauro Carvalho Chehab 
559a0bf528SMauro Carvalho Chehab 	lnbp21->config &= ~(LNBP21_VSEL | LNBP21_EN);
569a0bf528SMauro Carvalho Chehab 
579a0bf528SMauro Carvalho Chehab 	switch(voltage) {
589a0bf528SMauro Carvalho Chehab 	case SEC_VOLTAGE_OFF:
599a0bf528SMauro Carvalho Chehab 		break;
609a0bf528SMauro Carvalho Chehab 	case SEC_VOLTAGE_13:
619a0bf528SMauro Carvalho Chehab 		lnbp21->config |= LNBP21_EN;
629a0bf528SMauro Carvalho Chehab 		break;
639a0bf528SMauro Carvalho Chehab 	case SEC_VOLTAGE_18:
649a0bf528SMauro Carvalho Chehab 		lnbp21->config |= (LNBP21_EN | LNBP21_VSEL);
659a0bf528SMauro Carvalho Chehab 		break;
669a0bf528SMauro Carvalho Chehab 	default:
679a0bf528SMauro Carvalho Chehab 		return -EINVAL;
68c2c1b415SPeter Senna Tschudin 	}
699a0bf528SMauro Carvalho Chehab 
709a0bf528SMauro Carvalho Chehab 	lnbp21->config |= lnbp21->override_or;
719a0bf528SMauro Carvalho Chehab 	lnbp21->config &= lnbp21->override_and;
729a0bf528SMauro Carvalho Chehab 
739a0bf528SMauro Carvalho Chehab 	return (i2c_transfer(lnbp21->i2c, &msg, 1) == 1) ? 0 : -EIO;
749a0bf528SMauro Carvalho Chehab }
759a0bf528SMauro Carvalho Chehab 
769a0bf528SMauro Carvalho Chehab static int lnbp21_enable_high_lnb_voltage(struct dvb_frontend *fe, long arg)
779a0bf528SMauro Carvalho Chehab {
789a0bf528SMauro Carvalho Chehab 	struct lnbp21 *lnbp21 = (struct lnbp21 *) fe->sec_priv;
799a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {	.addr = lnbp21->i2c_addr, .flags = 0,
809a0bf528SMauro Carvalho Chehab 				.buf = &lnbp21->config,
819a0bf528SMauro Carvalho Chehab 				.len = sizeof(lnbp21->config) };
829a0bf528SMauro Carvalho Chehab 
839a0bf528SMauro Carvalho Chehab 	if (arg)
849a0bf528SMauro Carvalho Chehab 		lnbp21->config |= LNBP21_LLC;
859a0bf528SMauro Carvalho Chehab 	else
869a0bf528SMauro Carvalho Chehab 		lnbp21->config &= ~LNBP21_LLC;
879a0bf528SMauro Carvalho Chehab 
889a0bf528SMauro Carvalho Chehab 	lnbp21->config |= lnbp21->override_or;
899a0bf528SMauro Carvalho Chehab 	lnbp21->config &= lnbp21->override_and;
909a0bf528SMauro Carvalho Chehab 
919a0bf528SMauro Carvalho Chehab 	return (i2c_transfer(lnbp21->i2c, &msg, 1) == 1) ? 0 : -EIO;
929a0bf528SMauro Carvalho Chehab }
939a0bf528SMauro Carvalho Chehab 
949a0bf528SMauro Carvalho Chehab static int lnbp21_set_tone(struct dvb_frontend *fe,
959a0bf528SMauro Carvalho Chehab 				fe_sec_tone_mode_t tone)
969a0bf528SMauro Carvalho Chehab {
979a0bf528SMauro Carvalho Chehab 	struct lnbp21 *lnbp21 = (struct lnbp21 *) fe->sec_priv;
989a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {	.addr = lnbp21->i2c_addr, .flags = 0,
999a0bf528SMauro Carvalho Chehab 				.buf = &lnbp21->config,
1009a0bf528SMauro Carvalho Chehab 				.len = sizeof(lnbp21->config) };
1019a0bf528SMauro Carvalho Chehab 
1029a0bf528SMauro Carvalho Chehab 	switch (tone) {
1039a0bf528SMauro Carvalho Chehab 	case SEC_TONE_OFF:
1049a0bf528SMauro Carvalho Chehab 		lnbp21->config &= ~LNBP21_TEN;
1059a0bf528SMauro Carvalho Chehab 		break;
1069a0bf528SMauro Carvalho Chehab 	case SEC_TONE_ON:
1079a0bf528SMauro Carvalho Chehab 		lnbp21->config |= LNBP21_TEN;
1089a0bf528SMauro Carvalho Chehab 		break;
1099a0bf528SMauro Carvalho Chehab 	default:
1109a0bf528SMauro Carvalho Chehab 		return -EINVAL;
111c2c1b415SPeter Senna Tschudin 	}
1129a0bf528SMauro Carvalho Chehab 
1139a0bf528SMauro Carvalho Chehab 	lnbp21->config |= lnbp21->override_or;
1149a0bf528SMauro Carvalho Chehab 	lnbp21->config &= lnbp21->override_and;
1159a0bf528SMauro Carvalho Chehab 
1169a0bf528SMauro Carvalho Chehab 	return (i2c_transfer(lnbp21->i2c, &msg, 1) == 1) ? 0 : -EIO;
1179a0bf528SMauro Carvalho Chehab }
1189a0bf528SMauro Carvalho Chehab 
1199a0bf528SMauro Carvalho Chehab static void lnbp21_release(struct dvb_frontend *fe)
1209a0bf528SMauro Carvalho Chehab {
1219a0bf528SMauro Carvalho Chehab 	/* LNBP power off */
1229a0bf528SMauro Carvalho Chehab 	lnbp21_set_voltage(fe, SEC_VOLTAGE_OFF);
1239a0bf528SMauro Carvalho Chehab 
1249a0bf528SMauro Carvalho Chehab 	/* free data */
1259a0bf528SMauro Carvalho Chehab 	kfree(fe->sec_priv);
1269a0bf528SMauro Carvalho Chehab 	fe->sec_priv = NULL;
1279a0bf528SMauro Carvalho Chehab }
1289a0bf528SMauro Carvalho Chehab 
1299a0bf528SMauro Carvalho Chehab static struct dvb_frontend *lnbx2x_attach(struct dvb_frontend *fe,
1309a0bf528SMauro Carvalho Chehab 				struct i2c_adapter *i2c, u8 override_set,
1319a0bf528SMauro Carvalho Chehab 				u8 override_clear, u8 i2c_addr, u8 config)
1329a0bf528SMauro Carvalho Chehab {
1339a0bf528SMauro Carvalho Chehab 	struct lnbp21 *lnbp21 = kmalloc(sizeof(struct lnbp21), GFP_KERNEL);
1349a0bf528SMauro Carvalho Chehab 	if (!lnbp21)
1359a0bf528SMauro Carvalho Chehab 		return NULL;
1369a0bf528SMauro Carvalho Chehab 
1379a0bf528SMauro Carvalho Chehab 	/* default configuration */
1389a0bf528SMauro Carvalho Chehab 	lnbp21->config = config;
1399a0bf528SMauro Carvalho Chehab 	lnbp21->i2c = i2c;
1409a0bf528SMauro Carvalho Chehab 	lnbp21->i2c_addr = i2c_addr;
1419a0bf528SMauro Carvalho Chehab 	fe->sec_priv = lnbp21;
1429a0bf528SMauro Carvalho Chehab 
1439a0bf528SMauro Carvalho Chehab 	/* bits which should be forced to '1' */
1449a0bf528SMauro Carvalho Chehab 	lnbp21->override_or = override_set;
1459a0bf528SMauro Carvalho Chehab 
1469a0bf528SMauro Carvalho Chehab 	/* bits which should be forced to '0' */
1479a0bf528SMauro Carvalho Chehab 	lnbp21->override_and = ~override_clear;
1489a0bf528SMauro Carvalho Chehab 
1499a0bf528SMauro Carvalho Chehab 	/* detect if it is present or not */
1509a0bf528SMauro Carvalho Chehab 	if (lnbp21_set_voltage(fe, SEC_VOLTAGE_OFF)) {
1519a0bf528SMauro Carvalho Chehab 		kfree(lnbp21);
1529a0bf528SMauro Carvalho Chehab 		return NULL;
1539a0bf528SMauro Carvalho Chehab 	}
1549a0bf528SMauro Carvalho Chehab 
1559a0bf528SMauro Carvalho Chehab 	/* install release callback */
1569a0bf528SMauro Carvalho Chehab 	fe->ops.release_sec = lnbp21_release;
1579a0bf528SMauro Carvalho Chehab 
1589a0bf528SMauro Carvalho Chehab 	/* override frontend ops */
1599a0bf528SMauro Carvalho Chehab 	fe->ops.set_voltage = lnbp21_set_voltage;
1609a0bf528SMauro Carvalho Chehab 	fe->ops.enable_high_lnb_voltage = lnbp21_enable_high_lnb_voltage;
1619a0bf528SMauro Carvalho Chehab 	if (!(override_clear & LNBH24_TEN)) /*22kHz logic controlled by demod*/
1629a0bf528SMauro Carvalho Chehab 		fe->ops.set_tone = lnbp21_set_tone;
1639a0bf528SMauro Carvalho Chehab 	printk(KERN_INFO "LNBx2x attached on addr=%x\n", lnbp21->i2c_addr);
1649a0bf528SMauro Carvalho Chehab 
1659a0bf528SMauro Carvalho Chehab 	return fe;
1669a0bf528SMauro Carvalho Chehab }
1679a0bf528SMauro Carvalho Chehab 
1689a0bf528SMauro Carvalho Chehab struct dvb_frontend *lnbh24_attach(struct dvb_frontend *fe,
1699a0bf528SMauro Carvalho Chehab 				struct i2c_adapter *i2c, u8 override_set,
1709a0bf528SMauro Carvalho Chehab 				u8 override_clear, u8 i2c_addr)
1719a0bf528SMauro Carvalho Chehab {
1729a0bf528SMauro Carvalho Chehab 	return lnbx2x_attach(fe, i2c, override_set, override_clear,
1739a0bf528SMauro Carvalho Chehab 							i2c_addr, LNBH24_TTX);
1749a0bf528SMauro Carvalho Chehab }
1759a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(lnbh24_attach);
1769a0bf528SMauro Carvalho Chehab 
1779a0bf528SMauro Carvalho Chehab struct dvb_frontend *lnbp21_attach(struct dvb_frontend *fe,
1789a0bf528SMauro Carvalho Chehab 				struct i2c_adapter *i2c, u8 override_set,
1799a0bf528SMauro Carvalho Chehab 				u8 override_clear)
1809a0bf528SMauro Carvalho Chehab {
1819a0bf528SMauro Carvalho Chehab 	return lnbx2x_attach(fe, i2c, override_set, override_clear,
1829a0bf528SMauro Carvalho Chehab 							0x08, LNBP21_ISEL);
1839a0bf528SMauro Carvalho Chehab }
1849a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(lnbp21_attach);
1859a0bf528SMauro Carvalho Chehab 
1869a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("Driver for lnb supply and control ic lnbp21, lnbh24");
1879a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Oliver Endriss, Igor M. Liplianin");
1889a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
189