174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29a0bf528SMauro Carvalho Chehab /*
39a0bf528SMauro Carvalho Chehab Intersil ISL6423 SEC and LNB Power supply controller
49a0bf528SMauro Carvalho Chehab
59a0bf528SMauro Carvalho Chehab Copyright (C) Manu Abraham <abraham.manu@gmail.com>
69a0bf528SMauro Carvalho Chehab
79a0bf528SMauro Carvalho Chehab */
89a0bf528SMauro Carvalho Chehab
99a0bf528SMauro Carvalho Chehab #include <linux/delay.h>
109a0bf528SMauro Carvalho Chehab #include <linux/errno.h>
119a0bf528SMauro Carvalho Chehab #include <linux/init.h>
129a0bf528SMauro Carvalho Chehab #include <linux/kernel.h>
139a0bf528SMauro Carvalho Chehab #include <linux/module.h>
149a0bf528SMauro Carvalho Chehab #include <linux/string.h>
159a0bf528SMauro Carvalho Chehab #include <linux/slab.h>
169a0bf528SMauro Carvalho Chehab
17fada1935SMauro Carvalho Chehab #include <media/dvb_frontend.h>
189a0bf528SMauro Carvalho Chehab #include "isl6423.h"
199a0bf528SMauro Carvalho Chehab
209a0bf528SMauro Carvalho Chehab static unsigned int verbose;
219a0bf528SMauro Carvalho Chehab module_param(verbose, int, 0644);
229a0bf528SMauro Carvalho Chehab MODULE_PARM_DESC(verbose, "Set Verbosity level");
239a0bf528SMauro Carvalho Chehab
249a0bf528SMauro Carvalho Chehab #define FE_ERROR 0
259a0bf528SMauro Carvalho Chehab #define FE_NOTICE 1
269a0bf528SMauro Carvalho Chehab #define FE_INFO 2
279a0bf528SMauro Carvalho Chehab #define FE_DEBUG 3
289a0bf528SMauro Carvalho Chehab #define FE_DEBUGREG 4
299a0bf528SMauro Carvalho Chehab
309a0bf528SMauro Carvalho Chehab #define dprintk(__y, __z, format, arg...) do { \
319a0bf528SMauro Carvalho Chehab if (__z) { \
329a0bf528SMauro Carvalho Chehab if ((verbose > FE_ERROR) && (verbose > __y)) \
339a0bf528SMauro Carvalho Chehab printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
349a0bf528SMauro Carvalho Chehab else if ((verbose > FE_NOTICE) && (verbose > __y)) \
359a0bf528SMauro Carvalho Chehab printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
369a0bf528SMauro Carvalho Chehab else if ((verbose > FE_INFO) && (verbose > __y)) \
379a0bf528SMauro Carvalho Chehab printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
389a0bf528SMauro Carvalho Chehab else if ((verbose > FE_DEBUG) && (verbose > __y)) \
399a0bf528SMauro Carvalho Chehab printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
409a0bf528SMauro Carvalho Chehab } else { \
419a0bf528SMauro Carvalho Chehab if (verbose > __y) \
429a0bf528SMauro Carvalho Chehab printk(format, ##arg); \
439a0bf528SMauro Carvalho Chehab } \
449a0bf528SMauro Carvalho Chehab } while (0)
459a0bf528SMauro Carvalho Chehab
469a0bf528SMauro Carvalho Chehab struct isl6423_dev {
479a0bf528SMauro Carvalho Chehab const struct isl6423_config *config;
489a0bf528SMauro Carvalho Chehab struct i2c_adapter *i2c;
499a0bf528SMauro Carvalho Chehab
509a0bf528SMauro Carvalho Chehab u8 reg_3;
519a0bf528SMauro Carvalho Chehab u8 reg_4;
529a0bf528SMauro Carvalho Chehab
539a0bf528SMauro Carvalho Chehab unsigned int verbose;
549a0bf528SMauro Carvalho Chehab };
559a0bf528SMauro Carvalho Chehab
isl6423_write(struct isl6423_dev * isl6423,u8 reg)569a0bf528SMauro Carvalho Chehab static int isl6423_write(struct isl6423_dev *isl6423, u8 reg)
579a0bf528SMauro Carvalho Chehab {
589a0bf528SMauro Carvalho Chehab struct i2c_adapter *i2c = isl6423->i2c;
599a0bf528SMauro Carvalho Chehab u8 addr = isl6423->config->addr;
609a0bf528SMauro Carvalho Chehab int err = 0;
619a0bf528SMauro Carvalho Chehab
629a0bf528SMauro Carvalho Chehab struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = ®, .len = 1 };
639a0bf528SMauro Carvalho Chehab
649a0bf528SMauro Carvalho Chehab dprintk(FE_DEBUG, 1, "write reg %02X", reg);
659a0bf528SMauro Carvalho Chehab err = i2c_transfer(i2c, &msg, 1);
669a0bf528SMauro Carvalho Chehab if (err < 0)
679a0bf528SMauro Carvalho Chehab goto exit;
689a0bf528SMauro Carvalho Chehab return 0;
699a0bf528SMauro Carvalho Chehab
709a0bf528SMauro Carvalho Chehab exit:
719a0bf528SMauro Carvalho Chehab dprintk(FE_ERROR, 1, "I/O error <%d>", err);
729a0bf528SMauro Carvalho Chehab return err;
739a0bf528SMauro Carvalho Chehab }
749a0bf528SMauro Carvalho Chehab
isl6423_set_modulation(struct dvb_frontend * fe)759a0bf528SMauro Carvalho Chehab static int isl6423_set_modulation(struct dvb_frontend *fe)
769a0bf528SMauro Carvalho Chehab {
779a0bf528SMauro Carvalho Chehab struct isl6423_dev *isl6423 = (struct isl6423_dev *) fe->sec_priv;
789a0bf528SMauro Carvalho Chehab const struct isl6423_config *config = isl6423->config;
799a0bf528SMauro Carvalho Chehab int err = 0;
809a0bf528SMauro Carvalho Chehab u8 reg_2 = 0;
819a0bf528SMauro Carvalho Chehab
829a0bf528SMauro Carvalho Chehab reg_2 = 0x01 << 5;
839a0bf528SMauro Carvalho Chehab
849a0bf528SMauro Carvalho Chehab if (config->mod_extern)
859a0bf528SMauro Carvalho Chehab reg_2 |= (1 << 3);
869a0bf528SMauro Carvalho Chehab else
879a0bf528SMauro Carvalho Chehab reg_2 |= (1 << 4);
889a0bf528SMauro Carvalho Chehab
899a0bf528SMauro Carvalho Chehab err = isl6423_write(isl6423, reg_2);
909a0bf528SMauro Carvalho Chehab if (err < 0)
919a0bf528SMauro Carvalho Chehab goto exit;
929a0bf528SMauro Carvalho Chehab return 0;
939a0bf528SMauro Carvalho Chehab
949a0bf528SMauro Carvalho Chehab exit:
959a0bf528SMauro Carvalho Chehab dprintk(FE_ERROR, 1, "I/O error <%d>", err);
969a0bf528SMauro Carvalho Chehab return err;
979a0bf528SMauro Carvalho Chehab }
989a0bf528SMauro Carvalho Chehab
isl6423_voltage_boost(struct dvb_frontend * fe,long arg)999a0bf528SMauro Carvalho Chehab static int isl6423_voltage_boost(struct dvb_frontend *fe, long arg)
1009a0bf528SMauro Carvalho Chehab {
1019a0bf528SMauro Carvalho Chehab struct isl6423_dev *isl6423 = (struct isl6423_dev *) fe->sec_priv;
1029a0bf528SMauro Carvalho Chehab u8 reg_3 = isl6423->reg_3;
1039a0bf528SMauro Carvalho Chehab u8 reg_4 = isl6423->reg_4;
1049a0bf528SMauro Carvalho Chehab int err = 0;
1059a0bf528SMauro Carvalho Chehab
1069a0bf528SMauro Carvalho Chehab if (arg) {
1079a0bf528SMauro Carvalho Chehab /* EN = 1, VSPEN = 1, VBOT = 1 */
1089a0bf528SMauro Carvalho Chehab reg_4 |= (1 << 4);
1099a0bf528SMauro Carvalho Chehab reg_4 |= 0x1;
1109a0bf528SMauro Carvalho Chehab reg_3 |= (1 << 3);
1119a0bf528SMauro Carvalho Chehab } else {
1129a0bf528SMauro Carvalho Chehab /* EN = 1, VSPEN = 1, VBOT = 0 */
1139a0bf528SMauro Carvalho Chehab reg_4 |= (1 << 4);
1149a0bf528SMauro Carvalho Chehab reg_4 &= ~0x1;
1159a0bf528SMauro Carvalho Chehab reg_3 |= (1 << 3);
1169a0bf528SMauro Carvalho Chehab }
1179a0bf528SMauro Carvalho Chehab err = isl6423_write(isl6423, reg_3);
1189a0bf528SMauro Carvalho Chehab if (err < 0)
1199a0bf528SMauro Carvalho Chehab goto exit;
1209a0bf528SMauro Carvalho Chehab
1219a0bf528SMauro Carvalho Chehab err = isl6423_write(isl6423, reg_4);
1229a0bf528SMauro Carvalho Chehab if (err < 0)
1239a0bf528SMauro Carvalho Chehab goto exit;
1249a0bf528SMauro Carvalho Chehab
1259a0bf528SMauro Carvalho Chehab isl6423->reg_3 = reg_3;
1269a0bf528SMauro Carvalho Chehab isl6423->reg_4 = reg_4;
1279a0bf528SMauro Carvalho Chehab
1289a0bf528SMauro Carvalho Chehab return 0;
1299a0bf528SMauro Carvalho Chehab exit:
1309a0bf528SMauro Carvalho Chehab dprintk(FE_ERROR, 1, "I/O error <%d>", err);
1319a0bf528SMauro Carvalho Chehab return err;
1329a0bf528SMauro Carvalho Chehab }
1339a0bf528SMauro Carvalho Chehab
1349a0bf528SMauro Carvalho Chehab
isl6423_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage voltage)1359a0bf528SMauro Carvalho Chehab static int isl6423_set_voltage(struct dvb_frontend *fe,
1369a0bf528SMauro Carvalho Chehab enum fe_sec_voltage voltage)
1379a0bf528SMauro Carvalho Chehab {
1389a0bf528SMauro Carvalho Chehab struct isl6423_dev *isl6423 = (struct isl6423_dev *) fe->sec_priv;
1399a0bf528SMauro Carvalho Chehab u8 reg_3 = isl6423->reg_3;
1409a0bf528SMauro Carvalho Chehab u8 reg_4 = isl6423->reg_4;
1419a0bf528SMauro Carvalho Chehab int err = 0;
1429a0bf528SMauro Carvalho Chehab
1439a0bf528SMauro Carvalho Chehab switch (voltage) {
1449a0bf528SMauro Carvalho Chehab case SEC_VOLTAGE_OFF:
1459a0bf528SMauro Carvalho Chehab /* EN = 0 */
1469a0bf528SMauro Carvalho Chehab reg_4 &= ~(1 << 4);
1479a0bf528SMauro Carvalho Chehab break;
1489a0bf528SMauro Carvalho Chehab
1499a0bf528SMauro Carvalho Chehab case SEC_VOLTAGE_13:
1509a0bf528SMauro Carvalho Chehab /* EN = 1, VSPEN = 1, VTOP = 0, VBOT = 0 */
1519a0bf528SMauro Carvalho Chehab reg_4 |= (1 << 4);
1529a0bf528SMauro Carvalho Chehab reg_4 &= ~0x3;
1539a0bf528SMauro Carvalho Chehab reg_3 |= (1 << 3);
1549a0bf528SMauro Carvalho Chehab break;
1559a0bf528SMauro Carvalho Chehab
1569a0bf528SMauro Carvalho Chehab case SEC_VOLTAGE_18:
1579a0bf528SMauro Carvalho Chehab /* EN = 1, VSPEN = 1, VTOP = 1, VBOT = 0 */
1589a0bf528SMauro Carvalho Chehab reg_4 |= (1 << 4);
1599a0bf528SMauro Carvalho Chehab reg_4 |= 0x2;
1609a0bf528SMauro Carvalho Chehab reg_4 &= ~0x1;
1619a0bf528SMauro Carvalho Chehab reg_3 |= (1 << 3);
1629a0bf528SMauro Carvalho Chehab break;
1639a0bf528SMauro Carvalho Chehab
1649a0bf528SMauro Carvalho Chehab default:
1659a0bf528SMauro Carvalho Chehab break;
1669a0bf528SMauro Carvalho Chehab }
1679a0bf528SMauro Carvalho Chehab err = isl6423_write(isl6423, reg_3);
1689a0bf528SMauro Carvalho Chehab if (err < 0)
1699a0bf528SMauro Carvalho Chehab goto exit;
1709a0bf528SMauro Carvalho Chehab
1719a0bf528SMauro Carvalho Chehab err = isl6423_write(isl6423, reg_4);
1729a0bf528SMauro Carvalho Chehab if (err < 0)
1739a0bf528SMauro Carvalho Chehab goto exit;
1749a0bf528SMauro Carvalho Chehab
1759a0bf528SMauro Carvalho Chehab isl6423->reg_3 = reg_3;
1769a0bf528SMauro Carvalho Chehab isl6423->reg_4 = reg_4;
1779a0bf528SMauro Carvalho Chehab
1789a0bf528SMauro Carvalho Chehab return 0;
1799a0bf528SMauro Carvalho Chehab exit:
1809a0bf528SMauro Carvalho Chehab dprintk(FE_ERROR, 1, "I/O error <%d>", err);
1819a0bf528SMauro Carvalho Chehab return err;
1829a0bf528SMauro Carvalho Chehab }
1839a0bf528SMauro Carvalho Chehab
isl6423_set_current(struct dvb_frontend * fe)1849a0bf528SMauro Carvalho Chehab static int isl6423_set_current(struct dvb_frontend *fe)
1859a0bf528SMauro Carvalho Chehab {
1869a0bf528SMauro Carvalho Chehab struct isl6423_dev *isl6423 = (struct isl6423_dev *) fe->sec_priv;
1879a0bf528SMauro Carvalho Chehab u8 reg_3 = isl6423->reg_3;
1889a0bf528SMauro Carvalho Chehab const struct isl6423_config *config = isl6423->config;
1899a0bf528SMauro Carvalho Chehab int err = 0;
1909a0bf528SMauro Carvalho Chehab
1919a0bf528SMauro Carvalho Chehab switch (config->current_max) {
1929a0bf528SMauro Carvalho Chehab case SEC_CURRENT_275m:
1939a0bf528SMauro Carvalho Chehab /* 275mA */
1949a0bf528SMauro Carvalho Chehab /* ISELH = 0, ISELL = 0 */
1959a0bf528SMauro Carvalho Chehab reg_3 &= ~0x3;
1969a0bf528SMauro Carvalho Chehab break;
1979a0bf528SMauro Carvalho Chehab
1989a0bf528SMauro Carvalho Chehab case SEC_CURRENT_515m:
1999a0bf528SMauro Carvalho Chehab /* 515mA */
2009a0bf528SMauro Carvalho Chehab /* ISELH = 0, ISELL = 1 */
2019a0bf528SMauro Carvalho Chehab reg_3 &= ~0x2;
2029a0bf528SMauro Carvalho Chehab reg_3 |= 0x1;
2039a0bf528SMauro Carvalho Chehab break;
2049a0bf528SMauro Carvalho Chehab
2059a0bf528SMauro Carvalho Chehab case SEC_CURRENT_635m:
2069a0bf528SMauro Carvalho Chehab /* 635mA */
2079a0bf528SMauro Carvalho Chehab /* ISELH = 1, ISELL = 0 */
2089a0bf528SMauro Carvalho Chehab reg_3 &= ~0x1;
2099a0bf528SMauro Carvalho Chehab reg_3 |= 0x2;
2109a0bf528SMauro Carvalho Chehab break;
2119a0bf528SMauro Carvalho Chehab
2129a0bf528SMauro Carvalho Chehab case SEC_CURRENT_800m:
2139a0bf528SMauro Carvalho Chehab /* 800mA */
2149a0bf528SMauro Carvalho Chehab /* ISELH = 1, ISELL = 1 */
2159a0bf528SMauro Carvalho Chehab reg_3 |= 0x3;
2169a0bf528SMauro Carvalho Chehab break;
2179a0bf528SMauro Carvalho Chehab }
2189a0bf528SMauro Carvalho Chehab
2199a0bf528SMauro Carvalho Chehab err = isl6423_write(isl6423, reg_3);
2209a0bf528SMauro Carvalho Chehab if (err < 0)
2219a0bf528SMauro Carvalho Chehab goto exit;
2229a0bf528SMauro Carvalho Chehab
2239a0bf528SMauro Carvalho Chehab switch (config->curlim) {
2249a0bf528SMauro Carvalho Chehab case SEC_CURRENT_LIM_ON:
2259a0bf528SMauro Carvalho Chehab /* DCL = 0 */
2269a0bf528SMauro Carvalho Chehab reg_3 &= ~0x10;
2279a0bf528SMauro Carvalho Chehab break;
2289a0bf528SMauro Carvalho Chehab
2299a0bf528SMauro Carvalho Chehab case SEC_CURRENT_LIM_OFF:
2309a0bf528SMauro Carvalho Chehab /* DCL = 1 */
2319a0bf528SMauro Carvalho Chehab reg_3 |= 0x10;
2329a0bf528SMauro Carvalho Chehab break;
2339a0bf528SMauro Carvalho Chehab }
2349a0bf528SMauro Carvalho Chehab
2359a0bf528SMauro Carvalho Chehab err = isl6423_write(isl6423, reg_3);
2369a0bf528SMauro Carvalho Chehab if (err < 0)
2379a0bf528SMauro Carvalho Chehab goto exit;
2389a0bf528SMauro Carvalho Chehab
2399a0bf528SMauro Carvalho Chehab isl6423->reg_3 = reg_3;
2409a0bf528SMauro Carvalho Chehab
2419a0bf528SMauro Carvalho Chehab return 0;
2429a0bf528SMauro Carvalho Chehab exit:
2439a0bf528SMauro Carvalho Chehab dprintk(FE_ERROR, 1, "I/O error <%d>", err);
2449a0bf528SMauro Carvalho Chehab return err;
2459a0bf528SMauro Carvalho Chehab }
2469a0bf528SMauro Carvalho Chehab
isl6423_release(struct dvb_frontend * fe)2479a0bf528SMauro Carvalho Chehab static void isl6423_release(struct dvb_frontend *fe)
2489a0bf528SMauro Carvalho Chehab {
2499a0bf528SMauro Carvalho Chehab isl6423_set_voltage(fe, SEC_VOLTAGE_OFF);
2509a0bf528SMauro Carvalho Chehab
2519a0bf528SMauro Carvalho Chehab kfree(fe->sec_priv);
2529a0bf528SMauro Carvalho Chehab fe->sec_priv = NULL;
2539a0bf528SMauro Carvalho Chehab }
2549a0bf528SMauro Carvalho Chehab
isl6423_attach(struct dvb_frontend * fe,struct i2c_adapter * i2c,const struct isl6423_config * config)2559a0bf528SMauro Carvalho Chehab struct dvb_frontend *isl6423_attach(struct dvb_frontend *fe,
2569a0bf528SMauro Carvalho Chehab struct i2c_adapter *i2c,
2579a0bf528SMauro Carvalho Chehab const struct isl6423_config *config)
2589a0bf528SMauro Carvalho Chehab {
2599a0bf528SMauro Carvalho Chehab struct isl6423_dev *isl6423;
2609a0bf528SMauro Carvalho Chehab
2619a0bf528SMauro Carvalho Chehab isl6423 = kzalloc(sizeof(struct isl6423_dev), GFP_KERNEL);
2629a0bf528SMauro Carvalho Chehab if (!isl6423)
2639a0bf528SMauro Carvalho Chehab return NULL;
2649a0bf528SMauro Carvalho Chehab
2659a0bf528SMauro Carvalho Chehab isl6423->config = config;
2669a0bf528SMauro Carvalho Chehab isl6423->i2c = i2c;
2679a0bf528SMauro Carvalho Chehab fe->sec_priv = isl6423;
2689a0bf528SMauro Carvalho Chehab
2699a0bf528SMauro Carvalho Chehab /* SR3H = 0, SR3M = 1, SR3L = 0 */
2709a0bf528SMauro Carvalho Chehab isl6423->reg_3 = 0x02 << 5;
2719a0bf528SMauro Carvalho Chehab /* SR4H = 0, SR4M = 1, SR4L = 1 */
2729a0bf528SMauro Carvalho Chehab isl6423->reg_4 = 0x03 << 5;
2739a0bf528SMauro Carvalho Chehab
2749a0bf528SMauro Carvalho Chehab if (isl6423_set_current(fe))
2759a0bf528SMauro Carvalho Chehab goto exit;
2769a0bf528SMauro Carvalho Chehab
2779a0bf528SMauro Carvalho Chehab if (isl6423_set_modulation(fe))
2789a0bf528SMauro Carvalho Chehab goto exit;
2799a0bf528SMauro Carvalho Chehab
2809a0bf528SMauro Carvalho Chehab fe->ops.release_sec = isl6423_release;
2819a0bf528SMauro Carvalho Chehab fe->ops.set_voltage = isl6423_set_voltage;
2829a0bf528SMauro Carvalho Chehab fe->ops.enable_high_lnb_voltage = isl6423_voltage_boost;
2839a0bf528SMauro Carvalho Chehab isl6423->verbose = verbose;
2849a0bf528SMauro Carvalho Chehab
2859a0bf528SMauro Carvalho Chehab return fe;
2869a0bf528SMauro Carvalho Chehab
2879a0bf528SMauro Carvalho Chehab exit:
2889a0bf528SMauro Carvalho Chehab kfree(isl6423);
2899a0bf528SMauro Carvalho Chehab fe->sec_priv = NULL;
2909a0bf528SMauro Carvalho Chehab return NULL;
2919a0bf528SMauro Carvalho Chehab }
292*86495af1SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(isl6423_attach);
2939a0bf528SMauro Carvalho Chehab
2949a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("ISL6423 SEC");
2959a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Manu Abraham");
2969a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
297