saa7706h.c 11 KB

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