summaryrefslogtreecommitdiff
path: root/drivers/media/dvb-frontends/lnbh29.c
blob: 410bae099c323b06ba189217589247b7ecf8a9de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// SPDX-License-Identifier: GPL-2.0
//
// Driver for LNB supply and control IC STMicroelectronics LNBH29
//
// Copyright (c) 2018 Socionext Inc.

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>

#include <media/dvb_frontend.h>
#include "lnbh29.h"

/**
 * struct lnbh29_priv - LNBH29 driver private data
 * @i2c:         Pointer to the I2C adapter structure
 * @i2c_address: I2C address of LNBH29 chip
 * @config:      Registers configuration
 *               offset 0: 1st register address, always 0x01 (DATA)
 *               offset 1: DATA register value
 */
struct lnbh29_priv {
	struct i2c_adapter *i2c;
	u8 i2c_address;
	u8 config[2];
};

#define LNBH29_STATUS_OLF     BIT(0)
#define LNBH29_STATUS_OTF     BIT(1)
#define LNBH29_STATUS_VMON    BIT(2)
#define LNBH29_STATUS_PNG     BIT(3)
#define LNBH29_STATUS_PDO     BIT(4)
#define LNBH29_VSEL_MASK      GENMASK(2, 0)
#define LNBH29_VSEL_0         0x00
/* Min: 13.188V, Typ: 13.667V, Max:14V */
#define LNBH29_VSEL_13        0x03
/* Min: 18.158V, Typ: 18.817V, Max:19.475V */
#define LNBH29_VSEL_18        0x07

static int lnbh29_read_vmon(struct lnbh29_priv *priv)
{
	u8 addr = 0x00;
	u8 status[2];
	int ret;
	struct i2c_msg msg[2] = {
		{
			.addr = priv->i2c_address,
			.flags = 0,
			.len = 1,
			.buf = &addr
		}, {
			.addr = priv->i2c_address,
			.flags = I2C_M_RD,
			.len = sizeof(status),
			.buf = status
		}
	};

	ret = i2c_transfer(priv->i2c, msg, 2);
	if (ret >= 0 && ret != 2)
		ret = -EIO;
	if (ret < 0) {
		dev_dbg(&priv->i2c->dev, "LNBH29 I2C transfer failed (%d)\n",
			ret);
		return ret;
	}

	if (status[0] & (LNBH29_STATUS_OLF | LNBH29_STATUS_VMON)) {
		dev_err(&priv->i2c->dev,
			"LNBH29 voltage in failure state, status reg 0x%x\n",
			status[0]);
		return -EIO;
	}

	return 0;
}

static int lnbh29_set_voltage(struct dvb_frontend *fe,
			      enum fe_sec_voltage voltage)
{
	struct lnbh29_priv *priv = fe->sec_priv;
	u8 data_reg;
	int ret;
	struct i2c_msg msg = {
		.addr = priv->i2c_address,
		.flags = 0,
		.len = sizeof(priv->config),
		.buf = priv->config
	};

	switch (voltage) {
	case SEC_VOLTAGE_OFF:
		data_reg = LNBH29_VSEL_0;
		break;
	case SEC_VOLTAGE_13:
		data_reg = LNBH29_VSEL_13;
		break;
	case SEC_VOLTAGE_18:
		data_reg = LNBH29_VSEL_18;
		break;
	default:
		return -EINVAL;
	}
	priv->config[1] &= ~LNBH29_VSEL_MASK;
	priv->config[1] |= data_reg;

	ret = i2c_transfer(priv->i2c, &msg, 1);
	if (ret >= 0 && ret != 1)
		ret = -EIO;
	if (ret < 0) {
		dev_err(&priv->i2c->dev, "LNBH29 I2C transfer error (%d)\n",
			ret);
		return ret;
	}

	/* Soft-start time (Vout 0V to 18V) is Typ. 6ms. */
	usleep_range(6000, 20000);

	if (voltage == SEC_VOLTAGE_OFF)
		return 0;

	return lnbh29_read_vmon(priv);
}

static void lnbh29_release(struct dvb_frontend *fe)
{
	lnbh29_set_voltage(fe, SEC_VOLTAGE_OFF);
	kfree(fe->sec_priv);
	fe->sec_priv = NULL;
}

struct dvb_frontend *lnbh29_attach(struct dvb_frontend *fe,
				   struct lnbh29_config *cfg,
				   struct i2c_adapter *i2c)
{
	struct lnbh29_priv *priv;

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return NULL;

	priv->i2c_address = (cfg->i2c_address >> 1);
	priv->i2c = i2c;
	priv->config[0] = 0x01;
	priv->config[1] = cfg->data_config;
	fe->sec_priv = priv;

	if (lnbh29_set_voltage(fe, SEC_VOLTAGE_OFF)) {
		dev_err(&i2c->dev, "no LNBH29 found at I2C addr 0x%02x\n",
			priv->i2c_address);
		kfree(priv);
		fe->sec_priv = NULL;
		return NULL;
	}

	fe->ops.release_sec = lnbh29_release;
	fe->ops.set_voltage = lnbh29_set_voltage;

	dev_info(&i2c->dev, "LNBH29 attached at I2C addr 0x%02x\n",
		 priv->i2c_address);

	return fe;
}
EXPORT_SYMBOL(lnbh29_attach);

MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
MODULE_DESCRIPTION("STMicroelectronics LNBH29 driver");
MODULE_LICENSE("GPL v2");