xref: /openbmc/linux/drivers/media/radio/saa7706h.c (revision 9cfc5c90)
1 /*
2  * saa7706.c Philips SAA7706H Car Radio DSP driver
3  * Copyright (c) 2009 Intel Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/interrupt.h>
25 #include <linux/i2c.h>
26 #include <linux/slab.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ctrls.h>
29 
30 #define DRIVER_NAME "saa7706h"
31 
32 /* the I2C memory map looks like this
33 
34 	$1C00 - $FFFF Not Used
35 	$2200 - $3FFF Reserved YRAM (DSP2) space
36 	$2000 - $21FF YRAM (DSP2)
37 	$1FF0 - $1FFF Hardware Registers
38 	$1280 - $1FEF Reserved XRAM (DSP2) space
39 	$1000 - $127F XRAM (DSP2)
40 	$0FFF        DSP CONTROL
41 	$0A00 - $0FFE Reserved
42 	$0980 - $09FF Reserved YRAM (DSP1) space
43 	$0800 - $097F YRAM (DSP1)
44 	$0200 - $07FF Not Used
45 	$0180 - $01FF Reserved XRAM (DSP1) space
46 	$0000 - $017F XRAM (DSP1)
47 */
48 
49 #define SAA7706H_REG_CTRL		0x0fff
50 #define SAA7706H_CTRL_BYP_PLL		0x0001
51 #define SAA7706H_CTRL_PLL_DIV_MASK	0x003e
52 #define SAA7706H_CTRL_PLL3_62975MHZ	0x003e
53 #define SAA7706H_CTRL_DSP_TURBO		0x0040
54 #define SAA7706H_CTRL_PC_RESET_DSP1	0x0080
55 #define SAA7706H_CTRL_PC_RESET_DSP2	0x0100
56 #define SAA7706H_CTRL_DSP1_ROM_EN_MASK	0x0600
57 #define SAA7706H_CTRL_DSP1_FUNC_PROM	0x0000
58 #define SAA7706H_CTRL_DSP2_ROM_EN_MASK	0x1800
59 #define SAA7706H_CTRL_DSP2_FUNC_PROM	0x0000
60 #define SAA7706H_CTRL_DIG_SIL_INTERPOL	0x8000
61 
62 #define SAA7706H_REG_EVALUATION			0x1ff0
63 #define SAA7706H_EVAL_DISABLE_CHARGE_PUMP	0x000001
64 #define SAA7706H_EVAL_DCS_CLOCK			0x000002
65 #define SAA7706H_EVAL_GNDRC1_ENABLE		0x000004
66 #define SAA7706H_EVAL_GNDRC2_ENABLE		0x000008
67 
68 #define SAA7706H_REG_CL_GEN1			0x1ff3
69 #define SAA7706H_CL_GEN1_MIN_LOOPGAIN_MASK	0x00000f
70 #define SAA7706H_CL_GEN1_LOOPGAIN_MASK		0x0000f0
71 #define SAA7706H_CL_GEN1_COARSE_RATION		0xffff00
72 
73 #define SAA7706H_REG_CL_GEN2			0x1ff4
74 #define SAA7706H_CL_GEN2_WSEDGE_FALLING		0x000001
75 #define SAA7706H_CL_GEN2_STOP_VCO		0x000002
76 #define SAA7706H_CL_GEN2_FRERUN			0x000004
77 #define SAA7706H_CL_GEN2_ADAPTIVE		0x000008
78 #define SAA7706H_CL_GEN2_FINE_RATIO_MASK	0x0ffff0
79 
80 #define SAA7706H_REG_CL_GEN4		0x1ff6
81 #define SAA7706H_CL_GEN4_BYPASS_PLL1	0x001000
82 #define SAA7706H_CL_GEN4_PLL1_DIV_MASK	0x03e000
83 #define SAA7706H_CL_GEN4_DSP1_TURBO	0x040000
84 
85 #define SAA7706H_REG_SEL	0x1ff7
86 #define SAA7706H_SEL_DSP2_SRCA_MASK	0x000007
87 #define SAA7706H_SEL_DSP2_FMTA_MASK	0x000031
88 #define SAA7706H_SEL_DSP2_SRCB_MASK	0x0001c0
89 #define SAA7706H_SEL_DSP2_FMTB_MASK	0x000e00
90 #define SAA7706H_SEL_DSP1_SRC_MASK	0x003000
91 #define SAA7706H_SEL_DSP1_FMT_MASK	0x01c003
92 #define SAA7706H_SEL_SPDIF2		0x020000
93 #define SAA7706H_SEL_HOST_IO_FMT_MASK	0x1c0000
94 #define SAA7706H_SEL_EN_HOST_IO		0x200000
95 
96 #define SAA7706H_REG_IAC		0x1ff8
97 #define SAA7706H_REG_CLK_SET		0x1ff9
98 #define SAA7706H_REG_CLK_COEFF		0x1ffa
99 #define SAA7706H_REG_INPUT_SENS		0x1ffb
100 #define SAA7706H_INPUT_SENS_RDS_VOL_MASK	0x0003f
101 #define SAA7706H_INPUT_SENS_FM_VOL_MASK		0x00fc0
102 #define SAA7706H_INPUT_SENS_FM_MPX		0x01000
103 #define SAA7706H_INPUT_SENS_OFF_FILTER_A_EN	0x02000
104 #define SAA7706H_INPUT_SENS_OFF_FILTER_B_EN	0x04000
105 #define SAA7706H_REG_PHONE_NAV_AUDIO	0x1ffc
106 #define SAA7706H_REG_IO_CONF_DSP2	0x1ffd
107 #define SAA7706H_REG_STATUS_DSP2	0x1ffe
108 #define SAA7706H_REG_PC_DSP2		0x1fff
109 
110 #define SAA7706H_DSP1_MOD0	0x0800
111 #define SAA7706H_DSP1_ROM_VER	0x097f
112 #define SAA7706H_DSP2_MPTR0	0x1000
113 
114 #define SAA7706H_DSP1_MODPNTR	0x0000
115 
116 #define SAA7706H_DSP2_XMEM_CONTLLCW	0x113e
117 #define SAA7706H_DSP2_XMEM_BUSAMP	0x114a
118 #define SAA7706H_DSP2_XMEM_FDACPNTR	0x11f9
119 #define SAA7706H_DSP2_XMEM_IIS1PNTR	0x11fb
120 
121 #define SAA7706H_DSP2_YMEM_PVGA		0x212a
122 #define SAA7706H_DSP2_YMEM_PVAT1	0x212b
123 #define SAA7706H_DSP2_YMEM_PVAT		0x212c
124 #define SAA7706H_DSP2_YMEM_ROM_VER	0x21ff
125 
126 #define SUPPORTED_DSP1_ROM_VER		0x667
127 
128 struct saa7706h_state {
129 	struct v4l2_subdev sd;
130 	struct v4l2_ctrl_handler hdl;
131 	unsigned muted;
132 };
133 
134 static inline struct saa7706h_state *to_state(struct v4l2_subdev *sd)
135 {
136 	return container_of(sd, struct saa7706h_state, sd);
137 }
138 
139 static int saa7706h_i2c_send(struct i2c_client *client, const u8 *data, int len)
140 {
141 	int err = i2c_master_send(client, data, len);
142 	if (err == len)
143 		return 0;
144 	return err > 0 ? -EIO : err;
145 }
146 
147 static int saa7706h_i2c_transfer(struct i2c_client *client,
148 	struct i2c_msg *msgs, int num)
149 {
150 	int err = i2c_transfer(client->adapter, msgs, num);
151 	if (err == num)
152 		return 0;
153 	return err > 0 ? -EIO : err;
154 }
155 
156 static int saa7706h_set_reg24(struct v4l2_subdev *sd, u16 reg, u32 val)
157 {
158 	struct i2c_client *client = v4l2_get_subdevdata(sd);
159 	u8 buf[5];
160 	int pos = 0;
161 
162 	buf[pos++] = reg >> 8;
163 	buf[pos++] = reg;
164 	buf[pos++] = val >> 16;
165 	buf[pos++] = val >> 8;
166 	buf[pos++] = val;
167 
168 	return saa7706h_i2c_send(client, buf, pos);
169 }
170 
171 static int saa7706h_set_reg24_err(struct v4l2_subdev *sd, u16 reg, u32 val,
172 	int *err)
173 {
174 	return *err ? *err : saa7706h_set_reg24(sd, reg, val);
175 }
176 
177 static int saa7706h_set_reg16(struct v4l2_subdev *sd, u16 reg, u16 val)
178 {
179 	struct i2c_client *client = v4l2_get_subdevdata(sd);
180 	u8 buf[4];
181 	int pos = 0;
182 
183 	buf[pos++] = reg >> 8;
184 	buf[pos++] = reg;
185 	buf[pos++] = val >> 8;
186 	buf[pos++] = val;
187 
188 	return saa7706h_i2c_send(client, buf, pos);
189 }
190 
191 static int saa7706h_set_reg16_err(struct v4l2_subdev *sd, u16 reg, u16 val,
192 	int *err)
193 {
194 	return *err ? *err : saa7706h_set_reg16(sd, reg, val);
195 }
196 
197 static int saa7706h_get_reg16(struct v4l2_subdev *sd, u16 reg)
198 {
199 	struct i2c_client *client = v4l2_get_subdevdata(sd);
200 	u8 buf[2];
201 	int err;
202 	u8 regaddr[] = {reg >> 8, reg};
203 	struct i2c_msg msg[] = {
204 					{
205 						.addr = client->addr,
206 						.len = sizeof(regaddr),
207 						.buf = regaddr
208 					},
209 					{
210 						.addr = client->addr,
211 						.flags = I2C_M_RD,
212 						.len = sizeof(buf),
213 						.buf = buf
214 					}
215 				};
216 
217 	err = saa7706h_i2c_transfer(client, msg, ARRAY_SIZE(msg));
218 	if (err)
219 		return err;
220 
221 	return buf[0] << 8 | buf[1];
222 }
223 
224 static int saa7706h_unmute(struct v4l2_subdev *sd)
225 {
226 	struct saa7706h_state *state = to_state(sd);
227 	int err = 0;
228 
229 	err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
230 		SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
231 		SAA7706H_CTRL_PC_RESET_DSP2, &err);
232 
233 	/* newer versions of the chip requires a small sleep after reset */
234 	msleep(1);
235 
236 	err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
237 		SAA7706H_CTRL_PLL3_62975MHZ, &err);
238 
239 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_EVALUATION, 0, &err);
240 
241 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN1, 0x040022, &err);
242 
243 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN2,
244 		SAA7706H_CL_GEN2_WSEDGE_FALLING, &err);
245 
246 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN4, 0x024080, &err);
247 
248 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_SEL, 0x200080, &err);
249 
250 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IAC, 0xf4caed, &err);
251 
252 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_SET, 0x124334, &err);
253 
254 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_COEFF, 0x004a1a,
255 		&err);
256 
257 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_INPUT_SENS, 0x0071c7,
258 		&err);
259 
260 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PHONE_NAV_AUDIO,
261 		0x0e22ff, &err);
262 
263 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IO_CONF_DSP2, 0x001ff8,
264 		&err);
265 
266 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_STATUS_DSP2, 0x080003,
267 		&err);
268 
269 	err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PC_DSP2, 0x000004, &err);
270 
271 	err = saa7706h_set_reg16_err(sd, SAA7706H_DSP1_MOD0, 0x0c6c, &err);
272 
273 	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_MPTR0, 0x000b4b, &err);
274 
275 	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x000600, &err);
276 
277 	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x0000c0, &err);
278 
279 	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000819,
280 		&err);
281 
282 	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x00085a,
283 		&err);
284 
285 	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_BUSAMP, 0x7fffff,
286 		&err);
287 
288 	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_FDACPNTR, 0x2000cb,
289 		&err);
290 
291 	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_IIS1PNTR, 0x2000cb,
292 		&err);
293 
294 	err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVGA, 0x0f80, &err);
295 
296 	err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT1, 0x0800,
297 		&err);
298 
299 	err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT, 0x0800, &err);
300 
301 	err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000905,
302 		&err);
303 	if (!err)
304 		state->muted = 0;
305 	return err;
306 }
307 
308 static int saa7706h_mute(struct v4l2_subdev *sd)
309 {
310 	struct saa7706h_state *state = to_state(sd);
311 	int err;
312 
313 	err = saa7706h_set_reg16(sd, SAA7706H_REG_CTRL,
314 		SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
315 		SAA7706H_CTRL_PC_RESET_DSP2);
316 	if (!err)
317 		state->muted = 1;
318 	return err;
319 }
320 
321 static int saa7706h_s_ctrl(struct v4l2_ctrl *ctrl)
322 {
323 	struct saa7706h_state *state =
324 		container_of(ctrl->handler, struct saa7706h_state, hdl);
325 
326 	switch (ctrl->id) {
327 	case V4L2_CID_AUDIO_MUTE:
328 		if (ctrl->val)
329 			return saa7706h_mute(&state->sd);
330 		return saa7706h_unmute(&state->sd);
331 	}
332 	return -EINVAL;
333 }
334 
335 static const struct v4l2_ctrl_ops saa7706h_ctrl_ops = {
336 	.s_ctrl = saa7706h_s_ctrl,
337 };
338 
339 static const struct v4l2_subdev_ops empty_ops = {};
340 
341 /*
342  * Generic i2c probe
343  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
344  */
345 
346 static int saa7706h_probe(struct i2c_client *client,
347 			  const struct i2c_device_id *id)
348 {
349 	struct saa7706h_state *state;
350 	struct v4l2_subdev *sd;
351 	int err;
352 
353 	/* Check if the adapter supports the needed features */
354 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
355 		return -EIO;
356 
357 	v4l_info(client, "chip found @ 0x%02x (%s)\n",
358 			client->addr << 1, client->adapter->name);
359 
360 	state = kzalloc(sizeof(struct saa7706h_state), GFP_KERNEL);
361 	if (state == NULL)
362 		return -ENOMEM;
363 	sd = &state->sd;
364 	v4l2_i2c_subdev_init(sd, client, &empty_ops);
365 
366 	v4l2_ctrl_handler_init(&state->hdl, 4);
367 	v4l2_ctrl_new_std(&state->hdl, &saa7706h_ctrl_ops,
368 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
369 	sd->ctrl_handler = &state->hdl;
370 	err = state->hdl.error;
371 	if (err)
372 		goto err;
373 
374 	/* check the rom versions */
375 	err = saa7706h_get_reg16(sd, SAA7706H_DSP1_ROM_VER);
376 	if (err < 0)
377 		goto err;
378 	if (err != SUPPORTED_DSP1_ROM_VER)
379 		v4l2_warn(sd, "Unknown DSP1 ROM code version: 0x%x\n", err);
380 	state->muted = 1;
381 
382 	/* startup in a muted state */
383 	err = saa7706h_mute(sd);
384 	if (err)
385 		goto err;
386 
387 	return 0;
388 
389 err:
390 	v4l2_device_unregister_subdev(sd);
391 	v4l2_ctrl_handler_free(&state->hdl);
392 	kfree(to_state(sd));
393 
394 	printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", err);
395 
396 	return err;
397 }
398 
399 static int saa7706h_remove(struct i2c_client *client)
400 {
401 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
402 	struct saa7706h_state *state = to_state(sd);
403 
404 	saa7706h_mute(sd);
405 	v4l2_device_unregister_subdev(sd);
406 	v4l2_ctrl_handler_free(&state->hdl);
407 	kfree(to_state(sd));
408 	return 0;
409 }
410 
411 static const struct i2c_device_id saa7706h_id[] = {
412 	{DRIVER_NAME, 0},
413 	{},
414 };
415 
416 MODULE_DEVICE_TABLE(i2c, saa7706h_id);
417 
418 static struct i2c_driver saa7706h_driver = {
419 	.driver = {
420 		.name	= DRIVER_NAME,
421 	},
422 	.probe		= saa7706h_probe,
423 	.remove		= saa7706h_remove,
424 	.id_table	= saa7706h_id,
425 };
426 
427 module_i2c_driver(saa7706h_driver);
428 
429 MODULE_DESCRIPTION("SAA7706H Car Radio DSP driver");
430 MODULE_AUTHOR("Mocean Laboratories");
431 MODULE_LICENSE("GPL v2");
432