Commit | Line | Data |
---|---|---|
0c20e8ca MCC |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | // For Philips TEA5761 FM Chip | |
3 | // I2C address is always 0x20 (0x10 at 7-bit mode). | |
4 | // | |
32590819 | 5 | // Copyright (c) 2005-2007 Mauro Carvalho Chehab <mchehab@kernel.org> |
8573a9e6 MCC |
6 | |
7 | #include <linux/i2c.h> | |
5a0e3ad6 | 8 | #include <linux/slab.h> |
8573a9e6 | 9 | #include <linux/delay.h> |
7f6adeaf | 10 | #include <linux/videodev2.h> |
8573a9e6 | 11 | #include <media/tuner.h> |
7ab10bf7 MK |
12 | #include "tuner-i2c.h" |
13 | #include "tea5761.h" | |
8573a9e6 | 14 | |
ff699e6b | 15 | static int debug; |
7ab10bf7 MK |
16 | module_param(debug, int, 0644); |
17 | MODULE_PARM_DESC(debug, "enable verbose debug messages"); | |
8573a9e6 | 18 | |
db8a6956 MK |
19 | struct tea5761_priv { |
20 | struct tuner_i2c_props i2c_props; | |
7ab10bf7 MK |
21 | |
22 | u32 frequency; | |
2d351df4 | 23 | bool standby; |
db8a6956 MK |
24 | }; |
25 | ||
8573a9e6 MCC |
26 | /*****************************************************************************/ |
27 | ||
28 | /*************************** | |
29 | * TEA5761HN I2C registers * | |
30 | ***************************/ | |
31 | ||
32 | /* INTREG - Read: bytes 0 and 1 / Write: byte 0 */ | |
33 | ||
34 | /* first byte for reading */ | |
35 | #define TEA5761_INTREG_IFFLAG 0x10 | |
36 | #define TEA5761_INTREG_LEVFLAG 0x8 | |
37 | #define TEA5761_INTREG_FRRFLAG 0x2 | |
38 | #define TEA5761_INTREG_BLFLAG 0x1 | |
39 | ||
40 | /* second byte for reading / byte for writing */ | |
41 | #define TEA5761_INTREG_IFMSK 0x10 | |
42 | #define TEA5761_INTREG_LEVMSK 0x8 | |
43 | #define TEA5761_INTREG_FRMSK 0x2 | |
44 | #define TEA5761_INTREG_BLMSK 0x1 | |
45 | ||
46 | /* FRQSET - Read: bytes 2 and 3 / Write: byte 1 and 2 */ | |
47 | ||
48 | /* First byte */ | |
49 | #define TEA5761_FRQSET_SEARCH_UP 0x80 /* 1=Station search from botton to up */ | |
50 | #define TEA5761_FRQSET_SEARCH_MODE 0x40 /* 1=Search mode */ | |
51 | ||
52 | /* Bits 0-5 for divider MSB */ | |
53 | ||
54 | /* Second byte */ | |
55 | /* Bits 0-7 for divider LSB */ | |
56 | ||
57 | /* TNCTRL - Read: bytes 4 and 5 / Write: Bytes 3 and 4 */ | |
58 | ||
59 | /* first byte */ | |
60 | ||
61 | #define TEA5761_TNCTRL_PUPD_0 0x40 /* Power UP/Power Down MSB */ | |
62 | #define TEA5761_TNCTRL_BLIM 0X20 /* 1= Japan Frequencies, 0= European frequencies */ | |
63 | #define TEA5761_TNCTRL_SWPM 0x10 /* 1= software port is FRRFLAG */ | |
64 | #define TEA5761_TNCTRL_IFCTC 0x08 /* 1= IF count time 15.02 ms, 0= IF count time 2.02 ms */ | |
65 | #define TEA5761_TNCTRL_AFM 0x04 | |
66 | #define TEA5761_TNCTRL_SMUTE 0x02 /* 1= Soft mute */ | |
67 | #define TEA5761_TNCTRL_SNC 0x01 | |
68 | ||
69 | /* second byte */ | |
70 | ||
71 | #define TEA5761_TNCTRL_MU 0x80 /* 1=Hard mute */ | |
72 | #define TEA5761_TNCTRL_SSL_1 0x40 | |
73 | #define TEA5761_TNCTRL_SSL_0 0x20 | |
74 | #define TEA5761_TNCTRL_HLSI 0x10 | |
75 | #define TEA5761_TNCTRL_MST 0x08 /* 1 = mono */ | |
76 | #define TEA5761_TNCTRL_SWP 0x04 | |
77 | #define TEA5761_TNCTRL_DTC 0x02 /* 1 = deemphasis 50 us, 0 = deemphasis 75 us */ | |
78 | #define TEA5761_TNCTRL_AHLSI 0x01 | |
79 | ||
80 | /* FRQCHECK - Read: bytes 6 and 7 */ | |
81 | /* First byte */ | |
82 | ||
83 | /* Bits 0-5 for divider MSB */ | |
84 | ||
85 | /* Second byte */ | |
86 | /* Bits 0-7 for divider LSB */ | |
87 | ||
88 | /* TUNCHECK - Read: bytes 8 and 9 */ | |
89 | ||
90 | /* First byte */ | |
91 | #define TEA5761_TUNCHECK_IF_MASK 0x7e /* IF count */ | |
92 | #define TEA5761_TUNCHECK_TUNTO 0x01 | |
93 | ||
94 | /* Second byte */ | |
95 | #define TEA5761_TUNCHECK_LEV_MASK 0xf0 /* Level Count */ | |
96 | #define TEA5761_TUNCHECK_LD 0x08 | |
97 | #define TEA5761_TUNCHECK_STEREO 0x04 | |
98 | ||
99 | /* TESTREG - Read: bytes 10 and 11 / Write: bytes 5 and 6 */ | |
100 | ||
101 | /* All zero = no test mode */ | |
102 | ||
103 | /* MANID - Read: bytes 12 and 13 */ | |
104 | ||
105 | /* First byte - should be 0x10 */ | |
106 | #define TEA5767_MANID_VERSION_MASK 0xf0 /* Version = 1 */ | |
107 | #define TEA5767_MANID_ID_MSB_MASK 0x0f /* Manufacurer ID - should be 0 */ | |
108 | ||
109 | /* Second byte - Should be 0x2b */ | |
110 | ||
111 | #define TEA5767_MANID_ID_LSB_MASK 0xfe /* Manufacturer ID - should be 0x15 */ | |
112 | #define TEA5767_MANID_IDAV 0x01 /* 1 = Chip has ID, 0 = Chip has no ID */ | |
113 | ||
114 | /* Chip ID - Read: bytes 14 and 15 */ | |
115 | ||
116 | /* First byte - should be 0x57 */ | |
117 | ||
118 | /* Second byte - should be 0x61 */ | |
119 | ||
120 | /*****************************************************************************/ | |
121 | ||
8573a9e6 MCC |
122 | #define FREQ_OFFSET 0 /* for TEA5767, it is 700 to give the right freq */ |
123 | static void tea5761_status_dump(unsigned char *buffer) | |
124 | { | |
125 | unsigned int div, frq; | |
126 | ||
127 | div = ((buffer[2] & 0x3f) << 8) | buffer[3]; | |
128 | ||
129 | frq = 1000 * (div * 32768 / 1000 + FREQ_OFFSET + 225) / 4; /* Freq in KHz */ | |
130 | ||
2756665c | 131 | printk(KERN_INFO "tea5761: Frequency %d.%03d KHz (divider = 0x%04x)\n", |
8573a9e6 MCC |
132 | frq / 1000, frq % 1000, div); |
133 | } | |
134 | ||
135 | /* Freq should be specifyed at 62.5 Hz */ | |
2d351df4 MCC |
136 | static int __set_radio_freq(struct dvb_frontend *fe, |
137 | unsigned int freq, | |
138 | bool mono) | |
8573a9e6 | 139 | { |
7ab10bf7 | 140 | struct tea5761_priv *priv = fe->tuner_priv; |
2d351df4 | 141 | unsigned int frq = freq; |
8573a9e6 MCC |
142 | unsigned char buffer[7] = {0, 0, 0, 0, 0, 0, 0 }; |
143 | unsigned div; | |
144 | int rc; | |
145 | ||
7ab10bf7 | 146 | tuner_dbg("radio freq counter %d\n", frq); |
8573a9e6 | 147 | |
2d351df4 | 148 | if (priv->standby) { |
8573a9e6 MCC |
149 | tuner_dbg("TEA5761 set to standby mode\n"); |
150 | buffer[5] |= TEA5761_TNCTRL_MU; | |
151 | } else { | |
152 | buffer[4] |= TEA5761_TNCTRL_PUPD_0; | |
153 | } | |
154 | ||
155 | ||
2d351df4 | 156 | if (mono) { |
8573a9e6 MCC |
157 | tuner_dbg("TEA5761 set to mono\n"); |
158 | buffer[5] |= TEA5761_TNCTRL_MST; | |
8573a9e6 MCC |
159 | } else { |
160 | tuner_dbg("TEA5761 set to stereo\n"); | |
161 | } | |
162 | ||
163 | div = (1000 * (frq * 4 / 16 + 700 + 225) ) >> 15; | |
164 | buffer[1] = (div >> 8) & 0x3f; | |
165 | buffer[2] = div & 0xff; | |
166 | ||
7ab10bf7 | 167 | if (debug) |
8573a9e6 MCC |
168 | tea5761_status_dump(buffer); |
169 | ||
db8a6956 | 170 | if (7 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 7))) |
8573a9e6 | 171 | tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc); |
7ab10bf7 MK |
172 | |
173 | priv->frequency = frq * 125 / 2; | |
174 | ||
175 | return 0; | |
8573a9e6 MCC |
176 | } |
177 | ||
2d351df4 MCC |
178 | static int set_radio_freq(struct dvb_frontend *fe, |
179 | struct analog_parameters *params) | |
180 | { | |
181 | struct tea5761_priv *priv = fe->analog_demod_priv; | |
182 | ||
183 | priv->standby = false; | |
184 | ||
185 | return __set_radio_freq(fe, params->frequency, | |
186 | params->audmode == V4L2_TUNER_MODE_MONO); | |
187 | } | |
188 | ||
189 | static int set_radio_sleep(struct dvb_frontend *fe) | |
190 | { | |
191 | struct tea5761_priv *priv = fe->analog_demod_priv; | |
192 | ||
193 | priv->standby = true; | |
194 | ||
195 | return __set_radio_freq(fe, priv->frequency, false); | |
196 | } | |
197 | ||
fd443f74 | 198 | static int tea5761_read_status(struct dvb_frontend *fe, char *buffer) |
8573a9e6 | 199 | { |
7ab10bf7 | 200 | struct tea5761_priv *priv = fe->tuner_priv; |
fd443f74 | 201 | int rc; |
8573a9e6 | 202 | |
fd443f74 MK |
203 | memset(buffer, 0, 16); |
204 | if (16 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 16))) { | |
205 | tuner_warn("i2c i/o error: rc == %d (should be 16)\n", rc); | |
206 | return -EREMOTEIO; | |
207 | } | |
8573a9e6 | 208 | |
fd443f74 | 209 | return 0; |
8573a9e6 MCC |
210 | } |
211 | ||
fd443f74 | 212 | static inline int tea5761_signal(struct dvb_frontend *fe, const char *buffer) |
8573a9e6 | 213 | { |
7ab10bf7 | 214 | struct tea5761_priv *priv = fe->tuner_priv; |
8573a9e6 | 215 | |
fd443f74 MK |
216 | int signal = ((buffer[9] & TEA5761_TUNCHECK_LEV_MASK) << (13 - 4)); |
217 | ||
218 | tuner_dbg("Signal strength: %d\n", signal); | |
8573a9e6 | 219 | |
fd443f74 MK |
220 | return signal; |
221 | } | |
8573a9e6 | 222 | |
fd443f74 MK |
223 | static inline int tea5761_stereo(struct dvb_frontend *fe, const char *buffer) |
224 | { | |
225 | struct tea5761_priv *priv = fe->tuner_priv; | |
8573a9e6 | 226 | |
fd443f74 MK |
227 | int stereo = buffer[9] & TEA5761_TUNCHECK_STEREO; |
228 | ||
229 | tuner_dbg("Radio ST GET = %02x\n", stereo); | |
230 | ||
231 | return (stereo ? V4L2_TUNER_SUB_STEREO : 0); | |
8573a9e6 MCC |
232 | } |
233 | ||
7ab10bf7 MK |
234 | static int tea5761_get_status(struct dvb_frontend *fe, u32 *status) |
235 | { | |
fd443f74 | 236 | unsigned char buffer[16]; |
7ab10bf7 MK |
237 | |
238 | *status = 0; | |
239 | ||
fd443f74 MK |
240 | if (0 == tea5761_read_status(fe, buffer)) { |
241 | if (tea5761_signal(fe, buffer)) | |
242 | *status = TUNER_STATUS_LOCKED; | |
243 | if (tea5761_stereo(fe, buffer)) | |
244 | *status |= TUNER_STATUS_STEREO; | |
245 | } | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
250 | static int tea5761_get_rf_strength(struct dvb_frontend *fe, u16 *strength) | |
251 | { | |
252 | unsigned char buffer[16]; | |
253 | ||
254 | *strength = 0; | |
7ab10bf7 | 255 | |
fd443f74 MK |
256 | if (0 == tea5761_read_status(fe, buffer)) |
257 | *strength = tea5761_signal(fe, buffer); | |
7ab10bf7 MK |
258 | |
259 | return 0; | |
260 | } | |
261 | ||
262 | int tea5761_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr) | |
8573a9e6 MCC |
263 | { |
264 | unsigned char buffer[16]; | |
265 | int rc; | |
7ab10bf7 | 266 | struct tuner_i2c_props i2c = { .adap = i2c_adap, .addr = i2c_addr }; |
8573a9e6 | 267 | |
db8a6956 | 268 | if (16 != (rc = tuner_i2c_xfer_recv(&i2c, buffer, 16))) { |
7ab10bf7 | 269 | printk(KERN_WARNING "it is not a TEA5761. Received %i chars.\n", rc); |
867e835f | 270 | return -EINVAL; |
8573a9e6 MCC |
271 | } |
272 | ||
867e835f | 273 | if ((buffer[13] != 0x2b) || (buffer[14] != 0x57) || (buffer[15] != 0x061)) { |
8ca2e927 | 274 | printk(KERN_WARNING "Manufacturer ID= 0x%02x, Chip ID = %02x%02x. It is not a TEA5761\n", |
867e835f MCC |
275 | buffer[13], buffer[14], buffer[15]); |
276 | return -EINVAL; | |
8573a9e6 | 277 | } |
8ca2e927 | 278 | printk(KERN_WARNING "tea5761: TEA%02x%02x detected. Manufacturer ID= 0x%02x\n", |
867e835f MCC |
279 | buffer[14], buffer[15], buffer[13]); |
280 | ||
7ab10bf7 MK |
281 | return 0; |
282 | } | |
283 | ||
f2709c20 MCC |
284 | static void tea5761_release(struct dvb_frontend *fe) |
285 | { | |
286 | kfree(fe->tuner_priv); | |
287 | fe->tuner_priv = NULL; | |
288 | } | |
289 | ||
7ab10bf7 | 290 | static int tea5761_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
db8a6956 | 291 | { |
7ab10bf7 MK |
292 | struct tea5761_priv *priv = fe->tuner_priv; |
293 | *frequency = priv->frequency; | |
294 | return 0; | |
db8a6956 MK |
295 | } |
296 | ||
96105144 | 297 | static const struct dvb_tuner_ops tea5761_tuner_ops = { |
7ab10bf7 MK |
298 | .info = { |
299 | .name = "tea5761", // Philips TEA5761HN FM Radio | |
300 | }, | |
301 | .set_analog_params = set_radio_freq, | |
2d351df4 | 302 | .sleep = set_radio_sleep, |
f2709c20 | 303 | .release = tea5761_release, |
7ab10bf7 MK |
304 | .get_frequency = tea5761_get_frequency, |
305 | .get_status = tea5761_get_status, | |
fd443f74 | 306 | .get_rf_strength = tea5761_get_rf_strength, |
e407cd54 MK |
307 | }; |
308 | ||
7ab10bf7 MK |
309 | struct dvb_frontend *tea5761_attach(struct dvb_frontend *fe, |
310 | struct i2c_adapter* i2c_adap, | |
311 | u8 i2c_addr) | |
8573a9e6 | 312 | { |
db8a6956 | 313 | struct tea5761_priv *priv = NULL; |
8573a9e6 | 314 | |
4277106b | 315 | if (tea5761_autodetection(i2c_adap, i2c_addr) != 0) |
7ab10bf7 | 316 | return NULL; |
8573a9e6 | 317 | |
db8a6956 MK |
318 | priv = kzalloc(sizeof(struct tea5761_priv), GFP_KERNEL); |
319 | if (priv == NULL) | |
7ab10bf7 MK |
320 | return NULL; |
321 | fe->tuner_priv = priv; | |
db8a6956 | 322 | |
7ab10bf7 MK |
323 | priv->i2c_props.addr = i2c_addr; |
324 | priv->i2c_props.adap = i2c_adap; | |
2756665c | 325 | priv->i2c_props.name = "tea5761"; |
db8a6956 | 326 | |
7ab10bf7 MK |
327 | memcpy(&fe->ops.tuner_ops, &tea5761_tuner_ops, |
328 | sizeof(struct dvb_tuner_ops)); | |
8573a9e6 | 329 | |
7ab10bf7 | 330 | tuner_info("type set to %s\n", "Philips TEA5761HN FM Radio"); |
8573a9e6 | 331 | |
7ab10bf7 | 332 | return fe; |
8573a9e6 | 333 | } |
7ab10bf7 MK |
334 | |
335 | ||
336 | EXPORT_SYMBOL_GPL(tea5761_attach); | |
337 | EXPORT_SYMBOL_GPL(tea5761_autodetection); | |
338 | ||
339 | MODULE_DESCRIPTION("Philips TEA5761 FM tuner driver"); | |
32590819 | 340 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>"); |
0c20e8ca | 341 | MODULE_LICENSE("GPL v2"); |