Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / dvb-frontends / s5h1432.c
blob740a60df04555a876c7491d0d8a7b84a1f90bb6e
1 /*
2 * Samsung s5h1432 DVB-T demodulator driver
4 * Copyright (C) 2009 Bill Liu <Bill.Liu@Conexant.com>
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.
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.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <media/dvb_frontend.h>
24 #include "s5h1432.h"
26 struct s5h1432_state {
28 struct i2c_adapter *i2c;
30 /* configuration settings */
31 const struct s5h1432_config *config;
33 struct dvb_frontend frontend;
35 enum fe_modulation current_modulation;
36 unsigned int first_tune:1;
38 u32 current_frequency;
39 int if_freq;
41 u8 inversion;
44 static int debug;
46 #define dprintk(arg...) do { \
47 if (debug) \
48 printk(arg); \
49 } while (0)
51 static int s5h1432_writereg(struct s5h1432_state *state,
52 u8 addr, u8 reg, u8 data)
54 int ret;
55 u8 buf[] = { reg, data };
57 struct i2c_msg msg = {.addr = addr, .flags = 0, .buf = buf, .len = 2 };
59 ret = i2c_transfer(state->i2c, &msg, 1);
61 if (ret != 1)
62 printk(KERN_ERR "%s: writereg error 0x%02x 0x%02x 0x%04x, ret == %i)\n",
63 __func__, addr, reg, data, ret);
65 return (ret != 1) ? -1 : 0;
68 static u8 s5h1432_readreg(struct s5h1432_state *state, u8 addr, u8 reg)
70 int ret;
71 u8 b0[] = { reg };
72 u8 b1[] = { 0 };
74 struct i2c_msg msg[] = {
75 {.addr = addr, .flags = 0, .buf = b0, .len = 1},
76 {.addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 1}
79 ret = i2c_transfer(state->i2c, msg, 2);
81 if (ret != 2)
82 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
83 __func__, ret);
84 return b1[0];
87 static int s5h1432_sleep(struct dvb_frontend *fe)
89 return 0;
92 static int s5h1432_set_channel_bandwidth(struct dvb_frontend *fe,
93 u32 bandwidth)
95 struct s5h1432_state *state = fe->demodulator_priv;
97 u8 reg = 0;
99 /* Register [0x2E] bit 3:2 : 8MHz = 0; 7MHz = 1; 6MHz = 2 */
100 reg = s5h1432_readreg(state, S5H1432_I2C_TOP_ADDR, 0x2E);
101 reg &= ~(0x0C);
102 switch (bandwidth) {
103 case 6:
104 reg |= 0x08;
105 break;
106 case 7:
107 reg |= 0x04;
108 break;
109 case 8:
110 reg |= 0x00;
111 break;
112 default:
113 return 0;
115 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x2E, reg);
116 return 1;
119 static int s5h1432_set_IF(struct dvb_frontend *fe, u32 ifFreqHz)
121 struct s5h1432_state *state = fe->demodulator_priv;
123 switch (ifFreqHz) {
124 case TAIWAN_HI_IF_FREQ_44_MHZ:
125 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x55);
126 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x55);
127 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0x15);
128 break;
129 case EUROPE_HI_IF_FREQ_36_MHZ:
130 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x00);
131 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x00);
132 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0x40);
133 break;
134 case IF_FREQ_6_MHZ:
135 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x00);
136 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x00);
137 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0xe0);
138 break;
139 case IF_FREQ_3point3_MHZ:
140 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x66);
141 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x66);
142 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0xEE);
143 break;
144 case IF_FREQ_3point5_MHZ:
145 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x55);
146 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x55);
147 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0xED);
148 break;
149 case IF_FREQ_4_MHZ:
150 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0xAA);
151 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0xAA);
152 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0xEA);
153 break;
154 default:
156 u32 value = 0;
157 value = (u32) (((48000 - (ifFreqHz / 1000)) * 512 *
158 (u32) 32768) / (48 * 1000));
159 printk(KERN_INFO
160 "Default IFFreq %d :reg value = 0x%x\n",
161 ifFreqHz, value);
162 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4,
163 (u8) value & 0xFF);
164 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5,
165 (u8) (value >> 8) & 0xFF);
166 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7,
167 (u8) (value >> 16) & 0xFF);
168 break;
173 return 1;
176 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
177 static int s5h1432_set_frontend(struct dvb_frontend *fe)
179 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
180 u32 dvb_bandwidth = 8;
181 struct s5h1432_state *state = fe->demodulator_priv;
183 if (p->frequency == state->current_frequency) {
184 /*current_frequency = p->frequency; */
185 /*state->current_frequency = p->frequency; */
186 } else {
187 fe->ops.tuner_ops.set_params(fe);
188 msleep(300);
189 s5h1432_set_channel_bandwidth(fe, dvb_bandwidth);
190 switch (p->bandwidth_hz) {
191 case 6000000:
192 dvb_bandwidth = 6;
193 s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
194 break;
195 case 7000000:
196 dvb_bandwidth = 7;
197 s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
198 break;
199 case 8000000:
200 dvb_bandwidth = 8;
201 s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
202 break;
203 default:
204 return 0;
206 /*fe->ops.tuner_ops.set_params(fe); */
207 /*Soft Reset chip*/
208 msleep(30);
209 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1a);
210 msleep(30);
211 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1b);
213 s5h1432_set_channel_bandwidth(fe, dvb_bandwidth);
214 switch (p->bandwidth_hz) {
215 case 6000000:
216 dvb_bandwidth = 6;
217 s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
218 break;
219 case 7000000:
220 dvb_bandwidth = 7;
221 s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
222 break;
223 case 8000000:
224 dvb_bandwidth = 8;
225 s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
226 break;
227 default:
228 return 0;
230 /*fe->ops.tuner_ops.set_params(fe); */
231 /*Soft Reset chip*/
232 msleep(30);
233 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1a);
234 msleep(30);
235 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1b);
239 state->current_frequency = p->frequency;
241 return 0;
244 static int s5h1432_init(struct dvb_frontend *fe)
246 struct s5h1432_state *state = fe->demodulator_priv;
248 u8 reg = 0;
249 state->current_frequency = 0;
250 printk(KERN_INFO " s5h1432_init().\n");
252 /*Set VSB mode as default, this also does a soft reset */
253 /*Initialize registers */
255 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x04, 0xa8);
256 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x05, 0x01);
257 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x07, 0x70);
258 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x19, 0x80);
259 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x1b, 0x9D);
260 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x1c, 0x30);
261 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x1d, 0x20);
262 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x1e, 0x1B);
263 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x2e, 0x40);
264 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x42, 0x84);
265 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x50, 0x5a);
266 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x5a, 0xd3);
267 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x68, 0x50);
268 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xb8, 0x3c);
269 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xc4, 0x10);
270 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xcc, 0x9c);
271 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xDA, 0x00);
272 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe1, 0x94);
273 /* s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xf4, 0xa1); */
274 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xf9, 0x00);
276 /*For NXP tuner*/
278 /*Set 3.3MHz as default IF frequency */
279 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x66);
280 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x66);
281 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0xEE);
282 /* Set reg 0x1E to get the full dynamic range */
283 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x1e, 0x31);
285 /* Mode setting in demod */
286 reg = s5h1432_readreg(state, S5H1432_I2C_TOP_ADDR, 0x42);
287 reg |= 0x80;
288 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x42, reg);
289 /* Serial mode */
291 /* Soft Reset chip */
293 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1a);
294 msleep(30);
295 s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1b);
298 return 0;
301 static int s5h1432_read_status(struct dvb_frontend *fe, enum fe_status *status)
303 return 0;
306 static int s5h1432_read_signal_strength(struct dvb_frontend *fe,
307 u16 *signal_strength)
309 return 0;
312 static int s5h1432_read_snr(struct dvb_frontend *fe, u16 *snr)
314 return 0;
317 static int s5h1432_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
320 return 0;
323 static int s5h1432_read_ber(struct dvb_frontend *fe, u32 *ber)
325 return 0;
328 static int s5h1432_get_tune_settings(struct dvb_frontend *fe,
329 struct dvb_frontend_tune_settings *tune)
331 return 0;
334 static void s5h1432_release(struct dvb_frontend *fe)
336 struct s5h1432_state *state = fe->demodulator_priv;
337 kfree(state);
340 static const struct dvb_frontend_ops s5h1432_ops;
342 struct dvb_frontend *s5h1432_attach(const struct s5h1432_config *config,
343 struct i2c_adapter *i2c)
345 struct s5h1432_state *state = NULL;
347 printk(KERN_INFO " Enter s5h1432_attach(). attach success!\n");
348 /* allocate memory for the internal state */
349 state = kmalloc(sizeof(struct s5h1432_state), GFP_KERNEL);
350 if (!state)
351 return NULL;
353 /* setup the state */
354 state->config = config;
355 state->i2c = i2c;
356 state->current_modulation = QAM_16;
357 state->inversion = state->config->inversion;
359 /* create dvb_frontend */
360 memcpy(&state->frontend.ops, &s5h1432_ops,
361 sizeof(struct dvb_frontend_ops));
363 state->frontend.demodulator_priv = state;
365 return &state->frontend;
367 EXPORT_SYMBOL(s5h1432_attach);
369 static const struct dvb_frontend_ops s5h1432_ops = {
370 .delsys = { SYS_DVBT },
371 .info = {
372 .name = "Samsung s5h1432 DVB-T Frontend",
373 .frequency_min = 177000000,
374 .frequency_max = 858000000,
375 .frequency_stepsize = 166666,
376 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
377 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
378 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
379 FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
380 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER},
382 .init = s5h1432_init,
383 .sleep = s5h1432_sleep,
384 .set_frontend = s5h1432_set_frontend,
385 .get_tune_settings = s5h1432_get_tune_settings,
386 .read_status = s5h1432_read_status,
387 .read_ber = s5h1432_read_ber,
388 .read_signal_strength = s5h1432_read_signal_strength,
389 .read_snr = s5h1432_read_snr,
390 .read_ucblocks = s5h1432_read_ucblocks,
391 .release = s5h1432_release,
394 module_param(debug, int, 0644);
395 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
397 MODULE_DESCRIPTION("Samsung s5h1432 DVB-T Demodulator driver");
398 MODULE_AUTHOR("Bill Liu");
399 MODULE_LICENSE("GPL");