Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / radio / saa7706h.c
blobbf9eced906db4d0185ec1e73e2500adaaf5b5f13
1 /*
2 * saa7706.c Philips SAA7706H Car Radio DSP driver
3 * Copyright (c) 2009 Intel Corporation
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.
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.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/interrupt.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-ctrls.h>
26 #define DRIVER_NAME "saa7706h"
28 /* the I2C memory map looks like this
30 $1C00 - $FFFF Not Used
31 $2200 - $3FFF Reserved YRAM (DSP2) space
32 $2000 - $21FF YRAM (DSP2)
33 $1FF0 - $1FFF Hardware Registers
34 $1280 - $1FEF Reserved XRAM (DSP2) space
35 $1000 - $127F XRAM (DSP2)
36 $0FFF DSP CONTROL
37 $0A00 - $0FFE Reserved
38 $0980 - $09FF Reserved YRAM (DSP1) space
39 $0800 - $097F YRAM (DSP1)
40 $0200 - $07FF Not Used
41 $0180 - $01FF Reserved XRAM (DSP1) space
42 $0000 - $017F XRAM (DSP1)
45 #define SAA7706H_REG_CTRL 0x0fff
46 #define SAA7706H_CTRL_BYP_PLL 0x0001
47 #define SAA7706H_CTRL_PLL_DIV_MASK 0x003e
48 #define SAA7706H_CTRL_PLL3_62975MHZ 0x003e
49 #define SAA7706H_CTRL_DSP_TURBO 0x0040
50 #define SAA7706H_CTRL_PC_RESET_DSP1 0x0080
51 #define SAA7706H_CTRL_PC_RESET_DSP2 0x0100
52 #define SAA7706H_CTRL_DSP1_ROM_EN_MASK 0x0600
53 #define SAA7706H_CTRL_DSP1_FUNC_PROM 0x0000
54 #define SAA7706H_CTRL_DSP2_ROM_EN_MASK 0x1800
55 #define SAA7706H_CTRL_DSP2_FUNC_PROM 0x0000
56 #define SAA7706H_CTRL_DIG_SIL_INTERPOL 0x8000
58 #define SAA7706H_REG_EVALUATION 0x1ff0
59 #define SAA7706H_EVAL_DISABLE_CHARGE_PUMP 0x000001
60 #define SAA7706H_EVAL_DCS_CLOCK 0x000002
61 #define SAA7706H_EVAL_GNDRC1_ENABLE 0x000004
62 #define SAA7706H_EVAL_GNDRC2_ENABLE 0x000008
64 #define SAA7706H_REG_CL_GEN1 0x1ff3
65 #define SAA7706H_CL_GEN1_MIN_LOOPGAIN_MASK 0x00000f
66 #define SAA7706H_CL_GEN1_LOOPGAIN_MASK 0x0000f0
67 #define SAA7706H_CL_GEN1_COARSE_RATION 0xffff00
69 #define SAA7706H_REG_CL_GEN2 0x1ff4
70 #define SAA7706H_CL_GEN2_WSEDGE_FALLING 0x000001
71 #define SAA7706H_CL_GEN2_STOP_VCO 0x000002
72 #define SAA7706H_CL_GEN2_FRERUN 0x000004
73 #define SAA7706H_CL_GEN2_ADAPTIVE 0x000008
74 #define SAA7706H_CL_GEN2_FINE_RATIO_MASK 0x0ffff0
76 #define SAA7706H_REG_CL_GEN4 0x1ff6
77 #define SAA7706H_CL_GEN4_BYPASS_PLL1 0x001000
78 #define SAA7706H_CL_GEN4_PLL1_DIV_MASK 0x03e000
79 #define SAA7706H_CL_GEN4_DSP1_TURBO 0x040000
81 #define SAA7706H_REG_SEL 0x1ff7
82 #define SAA7706H_SEL_DSP2_SRCA_MASK 0x000007
83 #define SAA7706H_SEL_DSP2_FMTA_MASK 0x000031
84 #define SAA7706H_SEL_DSP2_SRCB_MASK 0x0001c0
85 #define SAA7706H_SEL_DSP2_FMTB_MASK 0x000e00
86 #define SAA7706H_SEL_DSP1_SRC_MASK 0x003000
87 #define SAA7706H_SEL_DSP1_FMT_MASK 0x01c003
88 #define SAA7706H_SEL_SPDIF2 0x020000
89 #define SAA7706H_SEL_HOST_IO_FMT_MASK 0x1c0000
90 #define SAA7706H_SEL_EN_HOST_IO 0x200000
92 #define SAA7706H_REG_IAC 0x1ff8
93 #define SAA7706H_REG_CLK_SET 0x1ff9
94 #define SAA7706H_REG_CLK_COEFF 0x1ffa
95 #define SAA7706H_REG_INPUT_SENS 0x1ffb
96 #define SAA7706H_INPUT_SENS_RDS_VOL_MASK 0x0003f
97 #define SAA7706H_INPUT_SENS_FM_VOL_MASK 0x00fc0
98 #define SAA7706H_INPUT_SENS_FM_MPX 0x01000
99 #define SAA7706H_INPUT_SENS_OFF_FILTER_A_EN 0x02000
100 #define SAA7706H_INPUT_SENS_OFF_FILTER_B_EN 0x04000
101 #define SAA7706H_REG_PHONE_NAV_AUDIO 0x1ffc
102 #define SAA7706H_REG_IO_CONF_DSP2 0x1ffd
103 #define SAA7706H_REG_STATUS_DSP2 0x1ffe
104 #define SAA7706H_REG_PC_DSP2 0x1fff
106 #define SAA7706H_DSP1_MOD0 0x0800
107 #define SAA7706H_DSP1_ROM_VER 0x097f
108 #define SAA7706H_DSP2_MPTR0 0x1000
110 #define SAA7706H_DSP1_MODPNTR 0x0000
112 #define SAA7706H_DSP2_XMEM_CONTLLCW 0x113e
113 #define SAA7706H_DSP2_XMEM_BUSAMP 0x114a
114 #define SAA7706H_DSP2_XMEM_FDACPNTR 0x11f9
115 #define SAA7706H_DSP2_XMEM_IIS1PNTR 0x11fb
117 #define SAA7706H_DSP2_YMEM_PVGA 0x212a
118 #define SAA7706H_DSP2_YMEM_PVAT1 0x212b
119 #define SAA7706H_DSP2_YMEM_PVAT 0x212c
120 #define SAA7706H_DSP2_YMEM_ROM_VER 0x21ff
122 #define SUPPORTED_DSP1_ROM_VER 0x667
124 struct saa7706h_state {
125 struct v4l2_subdev sd;
126 struct v4l2_ctrl_handler hdl;
127 unsigned muted;
130 static inline struct saa7706h_state *to_state(struct v4l2_subdev *sd)
132 return container_of(sd, struct saa7706h_state, sd);
135 static int saa7706h_i2c_send(struct i2c_client *client, const u8 *data, int len)
137 int err = i2c_master_send(client, data, len);
138 if (err == len)
139 return 0;
140 return err > 0 ? -EIO : err;
143 static int saa7706h_i2c_transfer(struct i2c_client *client,
144 struct i2c_msg *msgs, int num)
146 int err = i2c_transfer(client->adapter, msgs, num);
147 if (err == num)
148 return 0;
149 return err > 0 ? -EIO : err;
152 static int saa7706h_set_reg24(struct v4l2_subdev *sd, u16 reg, u32 val)
154 struct i2c_client *client = v4l2_get_subdevdata(sd);
155 u8 buf[5];
156 int pos = 0;
158 buf[pos++] = reg >> 8;
159 buf[pos++] = reg;
160 buf[pos++] = val >> 16;
161 buf[pos++] = val >> 8;
162 buf[pos++] = val;
164 return saa7706h_i2c_send(client, buf, pos);
167 static int saa7706h_set_reg24_err(struct v4l2_subdev *sd, u16 reg, u32 val,
168 int *err)
170 return *err ? *err : saa7706h_set_reg24(sd, reg, val);
173 static int saa7706h_set_reg16(struct v4l2_subdev *sd, u16 reg, u16 val)
175 struct i2c_client *client = v4l2_get_subdevdata(sd);
176 u8 buf[4];
177 int pos = 0;
179 buf[pos++] = reg >> 8;
180 buf[pos++] = reg;
181 buf[pos++] = val >> 8;
182 buf[pos++] = val;
184 return saa7706h_i2c_send(client, buf, pos);
187 static int saa7706h_set_reg16_err(struct v4l2_subdev *sd, u16 reg, u16 val,
188 int *err)
190 return *err ? *err : saa7706h_set_reg16(sd, reg, val);
193 static int saa7706h_get_reg16(struct v4l2_subdev *sd, u16 reg)
195 struct i2c_client *client = v4l2_get_subdevdata(sd);
196 u8 buf[2];
197 int err;
198 u8 regaddr[] = {reg >> 8, reg};
199 struct i2c_msg msg[] = {
201 .addr = client->addr,
202 .len = sizeof(regaddr),
203 .buf = regaddr
206 .addr = client->addr,
207 .flags = I2C_M_RD,
208 .len = sizeof(buf),
209 .buf = buf
213 err = saa7706h_i2c_transfer(client, msg, ARRAY_SIZE(msg));
214 if (err)
215 return err;
217 return buf[0] << 8 | buf[1];
220 static int saa7706h_unmute(struct v4l2_subdev *sd)
222 struct saa7706h_state *state = to_state(sd);
223 int err = 0;
225 err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
226 SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
227 SAA7706H_CTRL_PC_RESET_DSP2, &err);
229 /* newer versions of the chip requires a small sleep after reset */
230 msleep(1);
232 err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
233 SAA7706H_CTRL_PLL3_62975MHZ, &err);
235 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_EVALUATION, 0, &err);
237 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN1, 0x040022, &err);
239 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN2,
240 SAA7706H_CL_GEN2_WSEDGE_FALLING, &err);
242 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN4, 0x024080, &err);
244 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_SEL, 0x200080, &err);
246 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IAC, 0xf4caed, &err);
248 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_SET, 0x124334, &err);
250 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_COEFF, 0x004a1a,
251 &err);
253 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_INPUT_SENS, 0x0071c7,
254 &err);
256 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PHONE_NAV_AUDIO,
257 0x0e22ff, &err);
259 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IO_CONF_DSP2, 0x001ff8,
260 &err);
262 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_STATUS_DSP2, 0x080003,
263 &err);
265 err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PC_DSP2, 0x000004, &err);
267 err = saa7706h_set_reg16_err(sd, SAA7706H_DSP1_MOD0, 0x0c6c, &err);
269 err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_MPTR0, 0x000b4b, &err);
271 err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x000600, &err);
273 err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x0000c0, &err);
275 err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000819,
276 &err);
278 err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x00085a,
279 &err);
281 err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_BUSAMP, 0x7fffff,
282 &err);
284 err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_FDACPNTR, 0x2000cb,
285 &err);
287 err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_IIS1PNTR, 0x2000cb,
288 &err);
290 err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVGA, 0x0f80, &err);
292 err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT1, 0x0800,
293 &err);
295 err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT, 0x0800, &err);
297 err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000905,
298 &err);
299 if (!err)
300 state->muted = 0;
301 return err;
304 static int saa7706h_mute(struct v4l2_subdev *sd)
306 struct saa7706h_state *state = to_state(sd);
307 int err;
309 err = saa7706h_set_reg16(sd, SAA7706H_REG_CTRL,
310 SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
311 SAA7706H_CTRL_PC_RESET_DSP2);
312 if (!err)
313 state->muted = 1;
314 return err;
317 static int saa7706h_s_ctrl(struct v4l2_ctrl *ctrl)
319 struct saa7706h_state *state =
320 container_of(ctrl->handler, struct saa7706h_state, hdl);
322 switch (ctrl->id) {
323 case V4L2_CID_AUDIO_MUTE:
324 if (ctrl->val)
325 return saa7706h_mute(&state->sd);
326 return saa7706h_unmute(&state->sd);
328 return -EINVAL;
331 static const struct v4l2_ctrl_ops saa7706h_ctrl_ops = {
332 .s_ctrl = saa7706h_s_ctrl,
335 static const struct v4l2_subdev_ops empty_ops = {};
338 * Generic i2c probe
339 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
342 static int saa7706h_probe(struct i2c_client *client,
343 const struct i2c_device_id *id)
345 struct saa7706h_state *state;
346 struct v4l2_subdev *sd;
347 int err;
349 /* Check if the adapter supports the needed features */
350 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
351 return -EIO;
353 v4l_info(client, "chip found @ 0x%02x (%s)\n",
354 client->addr << 1, client->adapter->name);
356 state = kzalloc(sizeof(struct saa7706h_state), GFP_KERNEL);
357 if (state == NULL)
358 return -ENOMEM;
359 sd = &state->sd;
360 v4l2_i2c_subdev_init(sd, client, &empty_ops);
362 v4l2_ctrl_handler_init(&state->hdl, 4);
363 v4l2_ctrl_new_std(&state->hdl, &saa7706h_ctrl_ops,
364 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
365 sd->ctrl_handler = &state->hdl;
366 err = state->hdl.error;
367 if (err)
368 goto err;
370 /* check the rom versions */
371 err = saa7706h_get_reg16(sd, SAA7706H_DSP1_ROM_VER);
372 if (err < 0)
373 goto err;
374 if (err != SUPPORTED_DSP1_ROM_VER)
375 v4l2_warn(sd, "Unknown DSP1 ROM code version: 0x%x\n", err);
376 state->muted = 1;
378 /* startup in a muted state */
379 err = saa7706h_mute(sd);
380 if (err)
381 goto err;
383 return 0;
385 err:
386 v4l2_device_unregister_subdev(sd);
387 v4l2_ctrl_handler_free(&state->hdl);
388 kfree(to_state(sd));
390 printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", err);
392 return err;
395 static int saa7706h_remove(struct i2c_client *client)
397 struct v4l2_subdev *sd = i2c_get_clientdata(client);
398 struct saa7706h_state *state = to_state(sd);
400 saa7706h_mute(sd);
401 v4l2_device_unregister_subdev(sd);
402 v4l2_ctrl_handler_free(&state->hdl);
403 kfree(to_state(sd));
404 return 0;
407 static const struct i2c_device_id saa7706h_id[] = {
408 {DRIVER_NAME, 0},
412 MODULE_DEVICE_TABLE(i2c, saa7706h_id);
414 static struct i2c_driver saa7706h_driver = {
415 .driver = {
416 .name = DRIVER_NAME,
418 .probe = saa7706h_probe,
419 .remove = saa7706h_remove,
420 .id_table = saa7706h_id,
423 module_i2c_driver(saa7706h_driver);
425 MODULE_DESCRIPTION("SAA7706H Car Radio DSP driver");
426 MODULE_AUTHOR("Mocean Laboratories");
427 MODULE_LICENSE("GPL v2");