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  *
18bcb63314SSakari Ailus  * To obtain the license, point your browser to
19bcb63314SSakari Ailus  * http://www.gnu.org/copyleft/gpl.html
209a0bf528SMauro Carvalho Chehab  *
219a0bf528SMauro Carvalho Chehab  *
22991ce92fSMauro Carvalho Chehab  * the project's page is at https://linuxtv.org
239a0bf528SMauro Carvalho Chehab  */
249a0bf528SMauro Carvalho Chehab #include <linux/delay.h>
259a0bf528SMauro Carvalho Chehab #include <linux/errno.h>
269a0bf528SMauro Carvalho Chehab #include <linux/init.h>
279a0bf528SMauro Carvalho Chehab #include <linux/kernel.h>
289a0bf528SMauro Carvalho Chehab #include <linux/module.h>
299a0bf528SMauro Carvalho Chehab #include <linux/string.h>
309a0bf528SMauro Carvalho Chehab #include <linux/slab.h>
319a0bf528SMauro Carvalho Chehab 
329a0bf528SMauro Carvalho Chehab #include "dvb_frontend.h"
339a0bf528SMauro Carvalho Chehab #include "lnbp21.h"
349a0bf528SMauro Carvalho Chehab #include "lnbh24.h"
359a0bf528SMauro Carvalho Chehab 
369a0bf528SMauro Carvalho Chehab struct lnbp21 {
379a0bf528SMauro Carvalho Chehab 	u8			config;
389a0bf528SMauro Carvalho Chehab 	u8			override_or;
399a0bf528SMauro Carvalho Chehab 	u8			override_and;
409a0bf528SMauro Carvalho Chehab 	struct i2c_adapter	*i2c;
419a0bf528SMauro Carvalho Chehab 	u8			i2c_addr;
429a0bf528SMauro Carvalho Chehab };
439a0bf528SMauro Carvalho Chehab 
449a0bf528SMauro Carvalho Chehab static int lnbp21_set_voltage(struct dvb_frontend *fe,
450df289a2SMauro Carvalho Chehab 			      enum fe_sec_voltage voltage)
469a0bf528SMauro Carvalho Chehab {
479a0bf528SMauro Carvalho Chehab 	struct lnbp21 *lnbp21 = (struct lnbp21 *) fe->sec_priv;
489a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {	.addr = lnbp21->i2c_addr, .flags = 0,
499a0bf528SMauro Carvalho Chehab 				.buf = &lnbp21->config,
509a0bf528SMauro Carvalho Chehab 				.len = sizeof(lnbp21->config) };
519a0bf528SMauro Carvalho Chehab 
529a0bf528SMauro Carvalho Chehab 	lnbp21->config &= ~(LNBP21_VSEL | LNBP21_EN);
539a0bf528SMauro Carvalho Chehab 
549a0bf528SMauro Carvalho Chehab 	switch(voltage) {
559a0bf528SMauro Carvalho Chehab 	case SEC_VOLTAGE_OFF:
569a0bf528SMauro Carvalho Chehab 		break;
579a0bf528SMauro Carvalho Chehab 	case SEC_VOLTAGE_13:
589a0bf528SMauro Carvalho Chehab 		lnbp21->config |= LNBP21_EN;
599a0bf528SMauro Carvalho Chehab 		break;
609a0bf528SMauro Carvalho Chehab 	case SEC_VOLTAGE_18:
619a0bf528SMauro Carvalho Chehab 		lnbp21->config |= (LNBP21_EN | LNBP21_VSEL);
629a0bf528SMauro Carvalho Chehab 		break;
639a0bf528SMauro Carvalho Chehab 	default:
649a0bf528SMauro Carvalho Chehab 		return -EINVAL;
65c2c1b415SPeter Senna Tschudin 	}
669a0bf528SMauro Carvalho Chehab 
679a0bf528SMauro Carvalho Chehab 	lnbp21->config |= lnbp21->override_or;
689a0bf528SMauro Carvalho Chehab 	lnbp21->config &= lnbp21->override_and;
699a0bf528SMauro Carvalho Chehab 
709a0bf528SMauro Carvalho Chehab 	return (i2c_transfer(lnbp21->i2c, &msg, 1) == 1) ? 0 : -EIO;
719a0bf528SMauro Carvalho Chehab }
729a0bf528SMauro Carvalho Chehab 
739a0bf528SMauro Carvalho Chehab static int lnbp21_enable_high_lnb_voltage(struct dvb_frontend *fe, long arg)
749a0bf528SMauro Carvalho Chehab {
759a0bf528SMauro Carvalho Chehab 	struct lnbp21 *lnbp21 = (struct lnbp21 *) fe->sec_priv;
769a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {	.addr = lnbp21->i2c_addr, .flags = 0,
779a0bf528SMauro Carvalho Chehab 				.buf = &lnbp21->config,
789a0bf528SMauro Carvalho Chehab 				.len = sizeof(lnbp21->config) };
799a0bf528SMauro Carvalho Chehab 
809a0bf528SMauro Carvalho Chehab 	if (arg)
819a0bf528SMauro Carvalho Chehab 		lnbp21->config |= LNBP21_LLC;
829a0bf528SMauro Carvalho Chehab 	else
839a0bf528SMauro Carvalho Chehab 		lnbp21->config &= ~LNBP21_LLC;
849a0bf528SMauro Carvalho Chehab 
859a0bf528SMauro Carvalho Chehab 	lnbp21->config |= lnbp21->override_or;
869a0bf528SMauro Carvalho Chehab 	lnbp21->config &= lnbp21->override_and;
879a0bf528SMauro Carvalho Chehab 
889a0bf528SMauro Carvalho Chehab 	return (i2c_transfer(lnbp21->i2c, &msg, 1) == 1) ? 0 : -EIO;
899a0bf528SMauro Carvalho Chehab }
909a0bf528SMauro Carvalho Chehab 
919a0bf528SMauro Carvalho Chehab static int lnbp21_set_tone(struct dvb_frontend *fe,
920df289a2SMauro Carvalho Chehab 			   enum fe_sec_tone_mode tone)
939a0bf528SMauro Carvalho Chehab {
949a0bf528SMauro Carvalho Chehab 	struct lnbp21 *lnbp21 = (struct lnbp21 *) fe->sec_priv;
959a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {	.addr = lnbp21->i2c_addr, .flags = 0,
969a0bf528SMauro Carvalho Chehab 				.buf = &lnbp21->config,
979a0bf528SMauro Carvalho Chehab 				.len = sizeof(lnbp21->config) };
989a0bf528SMauro Carvalho Chehab 
999a0bf528SMauro Carvalho Chehab 	switch (tone) {
1009a0bf528SMauro Carvalho Chehab 	case SEC_TONE_OFF:
1019a0bf528SMauro Carvalho Chehab 		lnbp21->config &= ~LNBP21_TEN;
1029a0bf528SMauro Carvalho Chehab 		break;
1039a0bf528SMauro Carvalho Chehab 	case SEC_TONE_ON:
1049a0bf528SMauro Carvalho Chehab 		lnbp21->config |= LNBP21_TEN;
1059a0bf528SMauro Carvalho Chehab 		break;
1069a0bf528SMauro Carvalho Chehab 	default:
1079a0bf528SMauro Carvalho Chehab 		return -EINVAL;
108c2c1b415SPeter Senna Tschudin 	}
1099a0bf528SMauro Carvalho Chehab 
1109a0bf528SMauro Carvalho Chehab 	lnbp21->config |= lnbp21->override_or;
1119a0bf528SMauro Carvalho Chehab 	lnbp21->config &= lnbp21->override_and;
1129a0bf528SMauro Carvalho Chehab 
1139a0bf528SMauro Carvalho Chehab 	return (i2c_transfer(lnbp21->i2c, &msg, 1) == 1) ? 0 : -EIO;
1149a0bf528SMauro Carvalho Chehab }
1159a0bf528SMauro Carvalho Chehab 
1169a0bf528SMauro Carvalho Chehab static void lnbp21_release(struct dvb_frontend *fe)
1179a0bf528SMauro Carvalho Chehab {
1189a0bf528SMauro Carvalho Chehab 	/* LNBP power off */
1199a0bf528SMauro Carvalho Chehab 	lnbp21_set_voltage(fe, SEC_VOLTAGE_OFF);
1209a0bf528SMauro Carvalho Chehab 
1219a0bf528SMauro Carvalho Chehab 	/* free data */
1229a0bf528SMauro Carvalho Chehab 	kfree(fe->sec_priv);
1239a0bf528SMauro Carvalho Chehab 	fe->sec_priv = NULL;
1249a0bf528SMauro Carvalho Chehab }
1259a0bf528SMauro Carvalho Chehab 
1269a0bf528SMauro Carvalho Chehab static struct dvb_frontend *lnbx2x_attach(struct dvb_frontend *fe,
1279a0bf528SMauro Carvalho Chehab 				struct i2c_adapter *i2c, u8 override_set,
1289a0bf528SMauro Carvalho Chehab 				u8 override_clear, u8 i2c_addr, u8 config)
1299a0bf528SMauro Carvalho Chehab {
1309a0bf528SMauro Carvalho Chehab 	struct lnbp21 *lnbp21 = kmalloc(sizeof(struct lnbp21), GFP_KERNEL);
1319a0bf528SMauro Carvalho Chehab 	if (!lnbp21)
1329a0bf528SMauro Carvalho Chehab 		return NULL;
1339a0bf528SMauro Carvalho Chehab 
1349a0bf528SMauro Carvalho Chehab 	/* default configuration */
1359a0bf528SMauro Carvalho Chehab 	lnbp21->config = config;
1369a0bf528SMauro Carvalho Chehab 	lnbp21->i2c = i2c;
1379a0bf528SMauro Carvalho Chehab 	lnbp21->i2c_addr = i2c_addr;
1389a0bf528SMauro Carvalho Chehab 	fe->sec_priv = lnbp21;
1399a0bf528SMauro Carvalho Chehab 
1409a0bf528SMauro Carvalho Chehab 	/* bits which should be forced to '1' */
1419a0bf528SMauro Carvalho Chehab 	lnbp21->override_or = override_set;
1429a0bf528SMauro Carvalho Chehab 
1439a0bf528SMauro Carvalho Chehab 	/* bits which should be forced to '0' */
1449a0bf528SMauro Carvalho Chehab 	lnbp21->override_and = ~override_clear;
1459a0bf528SMauro Carvalho Chehab 
1469a0bf528SMauro Carvalho Chehab 	/* detect if it is present or not */
1479a0bf528SMauro Carvalho Chehab 	if (lnbp21_set_voltage(fe, SEC_VOLTAGE_OFF)) {
1489a0bf528SMauro Carvalho Chehab 		kfree(lnbp21);
1499a0bf528SMauro Carvalho Chehab 		return NULL;
1509a0bf528SMauro Carvalho Chehab 	}
1519a0bf528SMauro Carvalho Chehab 
1529a0bf528SMauro Carvalho Chehab 	/* install release callback */
1539a0bf528SMauro Carvalho Chehab 	fe->ops.release_sec = lnbp21_release;
1549a0bf528SMauro Carvalho Chehab 
1559a0bf528SMauro Carvalho Chehab 	/* override frontend ops */
1569a0bf528SMauro Carvalho Chehab 	fe->ops.set_voltage = lnbp21_set_voltage;
1579a0bf528SMauro Carvalho Chehab 	fe->ops.enable_high_lnb_voltage = lnbp21_enable_high_lnb_voltage;
1589a0bf528SMauro Carvalho Chehab 	if (!(override_clear & LNBH24_TEN)) /*22kHz logic controlled by demod*/
1599a0bf528SMauro Carvalho Chehab 		fe->ops.set_tone = lnbp21_set_tone;
1609a0bf528SMauro Carvalho Chehab 	printk(KERN_INFO "LNBx2x attached on addr=%x\n", lnbp21->i2c_addr);
1619a0bf528SMauro Carvalho Chehab 
1629a0bf528SMauro Carvalho Chehab 	return fe;
1639a0bf528SMauro Carvalho Chehab }
1649a0bf528SMauro Carvalho Chehab 
1659a0bf528SMauro Carvalho Chehab struct dvb_frontend *lnbh24_attach(struct dvb_frontend *fe,
1669a0bf528SMauro Carvalho Chehab 				struct i2c_adapter *i2c, u8 override_set,
1679a0bf528SMauro Carvalho Chehab 				u8 override_clear, u8 i2c_addr)
1689a0bf528SMauro Carvalho Chehab {
1699a0bf528SMauro Carvalho Chehab 	return lnbx2x_attach(fe, i2c, override_set, override_clear,
1709a0bf528SMauro Carvalho Chehab 							i2c_addr, LNBH24_TTX);
1719a0bf528SMauro Carvalho Chehab }
1729a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(lnbh24_attach);
1739a0bf528SMauro Carvalho Chehab 
1749a0bf528SMauro Carvalho Chehab struct dvb_frontend *lnbp21_attach(struct dvb_frontend *fe,
1759a0bf528SMauro Carvalho Chehab 				struct i2c_adapter *i2c, u8 override_set,
1769a0bf528SMauro Carvalho Chehab 				u8 override_clear)
1779a0bf528SMauro Carvalho Chehab {
1789a0bf528SMauro Carvalho Chehab 	return lnbx2x_attach(fe, i2c, override_set, override_clear,
1799a0bf528SMauro Carvalho Chehab 							0x08, LNBP21_ISEL);
1809a0bf528SMauro Carvalho Chehab }
1819a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(lnbp21_attach);
1829a0bf528SMauro Carvalho Chehab 
1839a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("Driver for lnb supply and control ic lnbp21, lnbh24");
1849a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Oliver Endriss, Igor M. Liplianin");
1859a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
186