saa7706h.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/i2c.h>
  25. #include <linux/slab.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-ctrls.h>
  28. #define DRIVER_NAME "saa7706h"
  29. /* 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)
  43. */
  44. #define SAA7706H_REG_CTRL 0x0fff
  45. #define SAA7706H_CTRL_BYP_PLL 0x0001
  46. #define SAA7706H_CTRL_PLL_DIV_MASK 0x003e
  47. #define SAA7706H_CTRL_PLL3_62975MHZ 0x003e
  48. #define SAA7706H_CTRL_DSP_TURBO 0x0040
  49. #define SAA7706H_CTRL_PC_RESET_DSP1 0x0080
  50. #define SAA7706H_CTRL_PC_RESET_DSP2 0x0100
  51. #define SAA7706H_CTRL_DSP1_ROM_EN_MASK 0x0600
  52. #define SAA7706H_CTRL_DSP1_FUNC_PROM 0x0000
  53. #define SAA7706H_CTRL_DSP2_ROM_EN_MASK 0x1800
  54. #define SAA7706H_CTRL_DSP2_FUNC_PROM 0x0000
  55. #define SAA7706H_CTRL_DIG_SIL_INTERPOL 0x8000
  56. #define SAA7706H_REG_EVALUATION 0x1ff0
  57. #define SAA7706H_EVAL_DISABLE_CHARGE_PUMP 0x000001
  58. #define SAA7706H_EVAL_DCS_CLOCK 0x000002
  59. #define SAA7706H_EVAL_GNDRC1_ENABLE 0x000004
  60. #define SAA7706H_EVAL_GNDRC2_ENABLE 0x000008
  61. #define SAA7706H_REG_CL_GEN1 0x1ff3
  62. #define SAA7706H_CL_GEN1_MIN_LOOPGAIN_MASK 0x00000f
  63. #define SAA7706H_CL_GEN1_LOOPGAIN_MASK 0x0000f0
  64. #define SAA7706H_CL_GEN1_COARSE_RATION 0xffff00
  65. #define SAA7706H_REG_CL_GEN2 0x1ff4
  66. #define SAA7706H_CL_GEN2_WSEDGE_FALLING 0x000001
  67. #define SAA7706H_CL_GEN2_STOP_VCO 0x000002
  68. #define SAA7706H_CL_GEN2_FRERUN 0x000004
  69. #define SAA7706H_CL_GEN2_ADAPTIVE 0x000008
  70. #define SAA7706H_CL_GEN2_FINE_RATIO_MASK 0x0ffff0
  71. #define SAA7706H_REG_CL_GEN4 0x1ff6
  72. #define SAA7706H_CL_GEN4_BYPASS_PLL1 0x001000
  73. #define SAA7706H_CL_GEN4_PLL1_DIV_MASK 0x03e000
  74. #define SAA7706H_CL_GEN4_DSP1_TURBO 0x040000
  75. #define SAA7706H_REG_SEL 0x1ff7
  76. #define SAA7706H_SEL_DSP2_SRCA_MASK 0x000007
  77. #define SAA7706H_SEL_DSP2_FMTA_MASK 0x000031
  78. #define SAA7706H_SEL_DSP2_SRCB_MASK 0x0001c0
  79. #define SAA7706H_SEL_DSP2_FMTB_MASK 0x000e00
  80. #define SAA7706H_SEL_DSP1_SRC_MASK 0x003000
  81. #define SAA7706H_SEL_DSP1_FMT_MASK 0x01c003
  82. #define SAA7706H_SEL_SPDIF2 0x020000
  83. #define SAA7706H_SEL_HOST_IO_FMT_MASK 0x1c0000
  84. #define SAA7706H_SEL_EN_HOST_IO 0x200000
  85. #define SAA7706H_REG_IAC 0x1ff8
  86. #define SAA7706H_REG_CLK_SET 0x1ff9
  87. #define SAA7706H_REG_CLK_COEFF 0x1ffa
  88. #define SAA7706H_REG_INPUT_SENS 0x1ffb
  89. #define SAA7706H_INPUT_SENS_RDS_VOL_MASK 0x0003f
  90. #define SAA7706H_INPUT_SENS_FM_VOL_MASK 0x00fc0
  91. #define SAA7706H_INPUT_SENS_FM_MPX 0x01000
  92. #define SAA7706H_INPUT_SENS_OFF_FILTER_A_EN 0x02000
  93. #define SAA7706H_INPUT_SENS_OFF_FILTER_B_EN 0x04000
  94. #define SAA7706H_REG_PHONE_NAV_AUDIO 0x1ffc
  95. #define SAA7706H_REG_IO_CONF_DSP2 0x1ffd
  96. #define SAA7706H_REG_STATUS_DSP2 0x1ffe
  97. #define SAA7706H_REG_PC_DSP2 0x1fff
  98. #define SAA7706H_DSP1_MOD0 0x0800
  99. #define SAA7706H_DSP1_ROM_VER 0x097f
  100. #define SAA7706H_DSP2_MPTR0 0x1000
  101. #define SAA7706H_DSP1_MODPNTR 0x0000
  102. #define SAA7706H_DSP2_XMEM_CONTLLCW 0x113e
  103. #define SAA7706H_DSP2_XMEM_BUSAMP 0x114a
  104. #define SAA7706H_DSP2_XMEM_FDACPNTR 0x11f9
  105. #define SAA7706H_DSP2_XMEM_IIS1PNTR 0x11fb
  106. #define SAA7706H_DSP2_YMEM_PVGA 0x212a
  107. #define SAA7706H_DSP2_YMEM_PVAT1 0x212b
  108. #define SAA7706H_DSP2_YMEM_PVAT 0x212c
  109. #define SAA7706H_DSP2_YMEM_ROM_VER 0x21ff
  110. #define SUPPORTED_DSP1_ROM_VER 0x667
  111. struct saa7706h_state {
  112. struct v4l2_subdev sd;
  113. struct v4l2_ctrl_handler hdl;
  114. unsigned muted;
  115. };
  116. static inline struct saa7706h_state *to_state(struct v4l2_subdev *sd)
  117. {
  118. return container_of(sd, struct saa7706h_state, sd);
  119. }
  120. static int saa7706h_i2c_send(struct i2c_client *client, const u8 *data, int len)
  121. {
  122. int err = i2c_master_send(client, data, len);
  123. if (err == len)
  124. return 0;
  125. return err > 0 ? -EIO : err;
  126. }
  127. static int saa7706h_i2c_transfer(struct i2c_client *client,
  128. struct i2c_msg *msgs, int num)
  129. {
  130. int err = i2c_transfer(client->adapter, msgs, num);
  131. if (err == num)
  132. return 0;
  133. return err > 0 ? -EIO : err;
  134. }
  135. static int saa7706h_set_reg24(struct v4l2_subdev *sd, u16 reg, u32 val)
  136. {
  137. struct i2c_client *client = v4l2_get_subdevdata(sd);
  138. u8 buf[5];
  139. int pos = 0;
  140. buf[pos++] = reg >> 8;
  141. buf[pos++] = reg;
  142. buf[pos++] = val >> 16;
  143. buf[pos++] = val >> 8;
  144. buf[pos++] = val;
  145. return saa7706h_i2c_send(client, buf, pos);
  146. }
  147. static int saa7706h_set_reg24_err(struct v4l2_subdev *sd, u16 reg, u32 val,
  148. int *err)
  149. {
  150. return *err ? *err : saa7706h_set_reg24(sd, reg, val);
  151. }
  152. static int saa7706h_set_reg16(struct v4l2_subdev *sd, u16 reg, u16 val)
  153. {
  154. struct i2c_client *client = v4l2_get_subdevdata(sd);
  155. u8 buf[4];
  156. int pos = 0;
  157. buf[pos++] = reg >> 8;
  158. buf[pos++] = reg;
  159. buf[pos++] = val >> 8;
  160. buf[pos++] = val;
  161. return saa7706h_i2c_send(client, buf, pos);
  162. }
  163. static int saa7706h_set_reg16_err(struct v4l2_subdev *sd, u16 reg, u16 val,
  164. int *err)
  165. {
  166. return *err ? *err : saa7706h_set_reg16(sd, reg, val);
  167. }
  168. static int saa7706h_get_reg16(struct v4l2_subdev *sd, u16 reg)
  169. {
  170. struct i2c_client *client = v4l2_get_subdevdata(sd);
  171. u8 buf[2];
  172. int err;
  173. u8 regaddr[] = {reg >> 8, reg};
  174. struct i2c_msg msg[] = {
  175. {
  176. .addr = client->addr,
  177. .len = sizeof(regaddr),
  178. .buf = regaddr
  179. },
  180. {
  181. .addr = client->addr,
  182. .flags = I2C_M_RD,
  183. .len = sizeof(buf),
  184. .buf = buf
  185. }
  186. };
  187. err = saa7706h_i2c_transfer(client, msg, ARRAY_SIZE(msg));
  188. if (err)
  189. return err;
  190. return buf[0] << 8 | buf[1];
  191. }
  192. static int saa7706h_unmute(struct v4l2_subdev *sd)
  193. {
  194. struct saa7706h_state *state = to_state(sd);
  195. int err = 0;
  196. err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
  197. SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
  198. SAA7706H_CTRL_PC_RESET_DSP2, &err);
  199. /* newer versions of the chip requires a small sleep after reset */
  200. msleep(1);
  201. err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
  202. SAA7706H_CTRL_PLL3_62975MHZ, &err);
  203. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_EVALUATION, 0, &err);
  204. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN1, 0x040022, &err);
  205. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN2,
  206. SAA7706H_CL_GEN2_WSEDGE_FALLING, &err);
  207. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN4, 0x024080, &err);
  208. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_SEL, 0x200080, &err);
  209. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IAC, 0xf4caed, &err);
  210. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_SET, 0x124334, &err);
  211. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_COEFF, 0x004a1a,
  212. &err);
  213. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_INPUT_SENS, 0x0071c7,
  214. &err);
  215. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PHONE_NAV_AUDIO,
  216. 0x0e22ff, &err);
  217. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IO_CONF_DSP2, 0x001ff8,
  218. &err);
  219. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_STATUS_DSP2, 0x080003,
  220. &err);
  221. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PC_DSP2, 0x000004, &err);
  222. err = saa7706h_set_reg16_err(sd, SAA7706H_DSP1_MOD0, 0x0c6c, &err);
  223. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_MPTR0, 0x000b4b, &err);
  224. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x000600, &err);
  225. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x0000c0, &err);
  226. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000819,
  227. &err);
  228. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x00085a,
  229. &err);
  230. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_BUSAMP, 0x7fffff,
  231. &err);
  232. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_FDACPNTR, 0x2000cb,
  233. &err);
  234. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_IIS1PNTR, 0x2000cb,
  235. &err);
  236. err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVGA, 0x0f80, &err);
  237. err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT1, 0x0800,
  238. &err);
  239. err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT, 0x0800, &err);
  240. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000905,
  241. &err);
  242. if (!err)
  243. state->muted = 0;
  244. return err;
  245. }
  246. static int saa7706h_mute(struct v4l2_subdev *sd)
  247. {
  248. struct saa7706h_state *state = to_state(sd);
  249. int err;
  250. err = saa7706h_set_reg16(sd, SAA7706H_REG_CTRL,
  251. SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
  252. SAA7706H_CTRL_PC_RESET_DSP2);
  253. if (!err)
  254. state->muted = 1;
  255. return err;
  256. }
  257. static int saa7706h_s_ctrl(struct v4l2_ctrl *ctrl)
  258. {
  259. struct saa7706h_state *state =
  260. container_of(ctrl->handler, struct saa7706h_state, hdl);
  261. switch (ctrl->id) {
  262. case V4L2_CID_AUDIO_MUTE:
  263. if (ctrl->val)
  264. return saa7706h_mute(&state->sd);
  265. return saa7706h_unmute(&state->sd);
  266. }
  267. return -EINVAL;
  268. }
  269. static const struct v4l2_ctrl_ops saa7706h_ctrl_ops = {
  270. .s_ctrl = saa7706h_s_ctrl,
  271. };
  272. static const struct v4l2_subdev_ops empty_ops = {};
  273. /*
  274. * Generic i2c probe
  275. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  276. */
  277. static int saa7706h_probe(struct i2c_client *client,
  278. const struct i2c_device_id *id)
  279. {
  280. struct saa7706h_state *state;
  281. struct v4l2_subdev *sd;
  282. int err;
  283. /* Check if the adapter supports the needed features */
  284. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  285. return -EIO;
  286. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  287. client->addr << 1, client->adapter->name);
  288. state = kzalloc(sizeof(struct saa7706h_state), GFP_KERNEL);
  289. if (state == NULL)
  290. return -ENOMEM;
  291. sd = &state->sd;
  292. v4l2_i2c_subdev_init(sd, client, &empty_ops);
  293. v4l2_ctrl_handler_init(&state->hdl, 4);
  294. v4l2_ctrl_new_std(&state->hdl, &saa7706h_ctrl_ops,
  295. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  296. sd->ctrl_handler = &state->hdl;
  297. err = state->hdl.error;
  298. if (err)
  299. goto err;
  300. /* check the rom versions */
  301. err = saa7706h_get_reg16(sd, SAA7706H_DSP1_ROM_VER);
  302. if (err < 0)
  303. goto err;
  304. if (err != SUPPORTED_DSP1_ROM_VER)
  305. v4l2_warn(sd, "Unknown DSP1 ROM code version: 0x%x\n", err);
  306. state->muted = 1;
  307. /* startup in a muted state */
  308. err = saa7706h_mute(sd);
  309. if (err)
  310. goto err;
  311. return 0;
  312. err:
  313. v4l2_device_unregister_subdev(sd);
  314. v4l2_ctrl_handler_free(&state->hdl);
  315. kfree(to_state(sd));
  316. printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", err);
  317. return err;
  318. }
  319. static int saa7706h_remove(struct i2c_client *client)
  320. {
  321. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  322. struct saa7706h_state *state = to_state(sd);
  323. saa7706h_mute(sd);
  324. v4l2_device_unregister_subdev(sd);
  325. v4l2_ctrl_handler_free(&state->hdl);
  326. kfree(to_state(sd));
  327. return 0;
  328. }
  329. static const struct i2c_device_id saa7706h_id[] = {
  330. {DRIVER_NAME, 0},
  331. {},
  332. };
  333. MODULE_DEVICE_TABLE(i2c, saa7706h_id);
  334. static struct i2c_driver saa7706h_driver = {
  335. .driver = {
  336. .name = DRIVER_NAME,
  337. },
  338. .probe = saa7706h_probe,
  339. .remove = saa7706h_remove,
  340. .id_table = saa7706h_id,
  341. };
  342. module_i2c_driver(saa7706h_driver);
  343. MODULE_DESCRIPTION("SAA7706H Car Radio DSP driver");
  344. MODULE_AUTHOR("Mocean Laboratories");
  345. MODULE_LICENSE("GPL v2");