[media] a8293: improve LNB register programming logic
[firefly-linux-kernel-4.4.55.git] / drivers / media / dvb-frontends / a8293.c
1 /*
2  * Allegro A8293 SEC driver
3  *
4  * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
5  *
6  *    This program is free software; you can redistribute it and/or modify
7  *    it under the terms of the GNU General Public License as published by
8  *    the Free Software Foundation; either version 2 of the License, or
9  *    (at your option) any later version.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License along
17  *    with this program; if not, write to the Free Software Foundation, Inc.,
18  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "a8293.h"
22
23 struct a8293_dev {
24         struct i2c_client *client;
25         u8 reg[2];
26 };
27
28 static int a8293_set_voltage(struct dvb_frontend *fe,
29         enum fe_sec_voltage fe_sec_voltage)
30 {
31         struct a8293_dev *dev = fe->sec_priv;
32         struct i2c_client *client = dev->client;
33         int ret;
34         u8 reg0, reg1;
35
36         dev_dbg(&client->dev, "fe_sec_voltage=%d\n", fe_sec_voltage);
37
38         switch (fe_sec_voltage) {
39         case SEC_VOLTAGE_OFF:
40                 /* ENB=0 */
41                 reg0 = 0x10;
42                 break;
43         case SEC_VOLTAGE_13:
44                 /* VSEL0=1, VSEL1=0, VSEL2=0, VSEL3=0, ENB=1*/
45                 reg0 = 0x31;
46                 break;
47         case SEC_VOLTAGE_18:
48                 /* VSEL0=0, VSEL1=0, VSEL2=0, VSEL3=1, ENB=1*/
49                 reg0 = 0x38;
50                 break;
51         default:
52                 ret = -EINVAL;
53                 goto err;
54         }
55         if (reg0 != dev->reg[0]) {
56                 ret = i2c_master_send(client, &reg0, 1);
57                 if (ret < 0)
58                         goto err;
59                 dev->reg[0] = reg0;
60         }
61
62         /* TMODE=0, TGATE=1 */
63         reg1 = 0x82;
64         if (reg1 != dev->reg[1]) {
65                 ret = i2c_master_send(client, &reg1, 1);
66                 if (ret < 0)
67                         goto err;
68                 dev->reg[1] = reg1;
69         }
70
71         usleep_range(1500, 50000);
72         return 0;
73 err:
74         dev_dbg(&client->dev, "failed=%d\n", ret);
75         return ret;
76 }
77
78 static int a8293_probe(struct i2c_client *client,
79                        const struct i2c_device_id *id)
80 {
81         struct a8293_dev *dev;
82         struct a8293_platform_data *pdata = client->dev.platform_data;
83         struct dvb_frontend *fe = pdata->dvb_frontend;
84         int ret;
85         u8 buf[2];
86
87         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
88         if (!dev) {
89                 ret = -ENOMEM;
90                 goto err;
91         }
92
93         dev->client = client;
94
95         /* check if the SEC is there */
96         ret = i2c_master_recv(client, buf, 2);
97         if (ret < 0)
98                 goto err_kfree;
99
100         /* override frontend ops */
101         fe->ops.set_voltage = a8293_set_voltage;
102         fe->sec_priv = dev;
103         i2c_set_clientdata(client, dev);
104
105         dev_info(&client->dev, "Allegro A8293 SEC successfully attached\n");
106         return 0;
107 err_kfree:
108         kfree(dev);
109 err:
110         dev_dbg(&client->dev, "failed=%d\n", ret);
111         return ret;
112 }
113
114 static int a8293_remove(struct i2c_client *client)
115 {
116         struct a8293_dev *dev = i2c_get_clientdata(client);
117
118         dev_dbg(&client->dev, "\n");
119
120         kfree(dev);
121         return 0;
122 }
123
124 static const struct i2c_device_id a8293_id_table[] = {
125         {"a8293", 0},
126         {}
127 };
128 MODULE_DEVICE_TABLE(i2c, a8293_id_table);
129
130 static struct i2c_driver a8293_driver = {
131         .driver = {
132                 .owner  = THIS_MODULE,
133                 .name   = "a8293",
134                 .suppress_bind_attrs = true,
135         },
136         .probe          = a8293_probe,
137         .remove         = a8293_remove,
138         .id_table       = a8293_id_table,
139 };
140
141 module_i2c_driver(a8293_driver);
142
143 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
144 MODULE_DESCRIPTION("Allegro A8293 SEC driver");
145 MODULE_LICENSE("GPL");