saa7706h.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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-chip-ident.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. unsigned muted;
  114. };
  115. static inline struct saa7706h_state *to_state(struct v4l2_subdev *sd)
  116. {
  117. return container_of(sd, struct saa7706h_state, sd);
  118. }
  119. static int saa7706h_i2c_send(struct i2c_client *client, const u8 *data, int len)
  120. {
  121. int err = i2c_master_send(client, data, len);
  122. if (err == len)
  123. return 0;
  124. return err > 0 ? -EIO : err;
  125. }
  126. static int saa7706h_i2c_transfer(struct i2c_client *client,
  127. struct i2c_msg *msgs, int num)
  128. {
  129. int err = i2c_transfer(client->adapter, msgs, num);
  130. if (err == num)
  131. return 0;
  132. return err > 0 ? -EIO : err;
  133. }
  134. static int saa7706h_set_reg24(struct v4l2_subdev *sd, u16 reg, u32 val)
  135. {
  136. struct i2c_client *client = v4l2_get_subdevdata(sd);
  137. u8 buf[5];
  138. int pos = 0;
  139. buf[pos++] = reg >> 8;
  140. buf[pos++] = reg;
  141. buf[pos++] = val >> 16;
  142. buf[pos++] = val >> 8;
  143. buf[pos++] = val;
  144. return saa7706h_i2c_send(client, buf, pos);
  145. }
  146. static int saa7706h_set_reg24_err(struct v4l2_subdev *sd, u16 reg, u32 val,
  147. int *err)
  148. {
  149. return *err ? *err : saa7706h_set_reg24(sd, reg, val);
  150. }
  151. static int saa7706h_set_reg16(struct v4l2_subdev *sd, u16 reg, u16 val)
  152. {
  153. struct i2c_client *client = v4l2_get_subdevdata(sd);
  154. u8 buf[4];
  155. int pos = 0;
  156. buf[pos++] = reg >> 8;
  157. buf[pos++] = reg;
  158. buf[pos++] = val >> 8;
  159. buf[pos++] = val;
  160. return saa7706h_i2c_send(client, buf, pos);
  161. }
  162. static int saa7706h_set_reg16_err(struct v4l2_subdev *sd, u16 reg, u16 val,
  163. int *err)
  164. {
  165. return *err ? *err : saa7706h_set_reg16(sd, reg, val);
  166. }
  167. static int saa7706h_get_reg16(struct v4l2_subdev *sd, u16 reg)
  168. {
  169. struct i2c_client *client = v4l2_get_subdevdata(sd);
  170. u8 buf[2];
  171. int err;
  172. u8 regaddr[] = {reg >> 8, reg};
  173. struct i2c_msg msg[] = { {client->addr, 0, sizeof(regaddr), regaddr},
  174. {client->addr, I2C_M_RD, sizeof(buf), buf} };
  175. err = saa7706h_i2c_transfer(client, msg, ARRAY_SIZE(msg));
  176. if (err)
  177. return err;
  178. return buf[0] << 8 | buf[1];
  179. }
  180. static int saa7706h_unmute(struct v4l2_subdev *sd)
  181. {
  182. struct saa7706h_state *state = to_state(sd);
  183. int err = 0;
  184. err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
  185. SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
  186. SAA7706H_CTRL_PC_RESET_DSP2, &err);
  187. /* newer versions of the chip requires a small sleep after reset */
  188. msleep(1);
  189. err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
  190. SAA7706H_CTRL_PLL3_62975MHZ, &err);
  191. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_EVALUATION, 0, &err);
  192. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN1, 0x040022, &err);
  193. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN2,
  194. SAA7706H_CL_GEN2_WSEDGE_FALLING, &err);
  195. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN4, 0x024080, &err);
  196. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_SEL, 0x200080, &err);
  197. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IAC, 0xf4caed, &err);
  198. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_SET, 0x124334, &err);
  199. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_COEFF, 0x004a1a,
  200. &err);
  201. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_INPUT_SENS, 0x0071c7,
  202. &err);
  203. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PHONE_NAV_AUDIO,
  204. 0x0e22ff, &err);
  205. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IO_CONF_DSP2, 0x001ff8,
  206. &err);
  207. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_STATUS_DSP2, 0x080003,
  208. &err);
  209. err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PC_DSP2, 0x000004, &err);
  210. err = saa7706h_set_reg16_err(sd, SAA7706H_DSP1_MOD0, 0x0c6c, &err);
  211. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_MPTR0, 0x000b4b, &err);
  212. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x000600, &err);
  213. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x0000c0, &err);
  214. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000819,
  215. &err);
  216. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x00085a,
  217. &err);
  218. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_BUSAMP, 0x7fffff,
  219. &err);
  220. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_FDACPNTR, 0x2000cb,
  221. &err);
  222. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_IIS1PNTR, 0x2000cb,
  223. &err);
  224. err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVGA, 0x0f80, &err);
  225. err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT1, 0x0800,
  226. &err);
  227. err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT, 0x0800, &err);
  228. err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000905,
  229. &err);
  230. if (!err)
  231. state->muted = 0;
  232. return err;
  233. }
  234. static int saa7706h_mute(struct v4l2_subdev *sd)
  235. {
  236. struct saa7706h_state *state = to_state(sd);
  237. int err;
  238. err = saa7706h_set_reg16(sd, SAA7706H_REG_CTRL,
  239. SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
  240. SAA7706H_CTRL_PC_RESET_DSP2);
  241. if (!err)
  242. state->muted = 1;
  243. return err;
  244. }
  245. static int saa7706h_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  246. {
  247. switch (qc->id) {
  248. case V4L2_CID_AUDIO_MUTE:
  249. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  250. }
  251. return -EINVAL;
  252. }
  253. static int saa7706h_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  254. {
  255. struct saa7706h_state *state = to_state(sd);
  256. switch (ctrl->id) {
  257. case V4L2_CID_AUDIO_MUTE:
  258. ctrl->value = state->muted;
  259. return 0;
  260. }
  261. return -EINVAL;
  262. }
  263. static int saa7706h_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  264. {
  265. switch (ctrl->id) {
  266. case V4L2_CID_AUDIO_MUTE:
  267. if (ctrl->value)
  268. return saa7706h_mute(sd);
  269. return saa7706h_unmute(sd);
  270. }
  271. return -EINVAL;
  272. }
  273. static int saa7706h_g_chip_ident(struct v4l2_subdev *sd,
  274. struct v4l2_dbg_chip_ident *chip)
  275. {
  276. struct i2c_client *client = v4l2_get_subdevdata(sd);
  277. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA7706H, 0);
  278. }
  279. static const struct v4l2_subdev_core_ops saa7706h_core_ops = {
  280. .g_chip_ident = saa7706h_g_chip_ident,
  281. .queryctrl = saa7706h_queryctrl,
  282. .g_ctrl = saa7706h_g_ctrl,
  283. .s_ctrl = saa7706h_s_ctrl,
  284. };
  285. static const struct v4l2_subdev_ops saa7706h_ops = {
  286. .core = &saa7706h_core_ops,
  287. };
  288. /*
  289. * Generic i2c probe
  290. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  291. */
  292. static int __devinit saa7706h_probe(struct i2c_client *client,
  293. const struct i2c_device_id *id)
  294. {
  295. struct saa7706h_state *state;
  296. struct v4l2_subdev *sd;
  297. int err;
  298. /* Check if the adapter supports the needed features */
  299. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  300. return -EIO;
  301. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  302. client->addr << 1, client->adapter->name);
  303. state = kzalloc(sizeof(struct saa7706h_state), GFP_KERNEL);
  304. if (state == NULL)
  305. return -ENOMEM;
  306. sd = &state->sd;
  307. v4l2_i2c_subdev_init(sd, client, &saa7706h_ops);
  308. /* check the rom versions */
  309. err = saa7706h_get_reg16(sd, SAA7706H_DSP1_ROM_VER);
  310. if (err < 0)
  311. goto err;
  312. if (err != SUPPORTED_DSP1_ROM_VER)
  313. v4l2_warn(sd, "Unknown DSP1 ROM code version: 0x%x\n", err);
  314. state->muted = 1;
  315. /* startup in a muted state */
  316. err = saa7706h_mute(sd);
  317. if (err)
  318. goto err;
  319. return 0;
  320. err:
  321. v4l2_device_unregister_subdev(sd);
  322. kfree(to_state(sd));
  323. printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", err);
  324. return err;
  325. }
  326. static int __devexit saa7706h_remove(struct i2c_client *client)
  327. {
  328. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  329. saa7706h_mute(sd);
  330. v4l2_device_unregister_subdev(sd);
  331. kfree(to_state(sd));
  332. return 0;
  333. }
  334. static const struct i2c_device_id saa7706h_id[] = {
  335. {DRIVER_NAME, 0},
  336. {},
  337. };
  338. MODULE_DEVICE_TABLE(i2c, saa7706h_id);
  339. static struct i2c_driver saa7706h_driver = {
  340. .driver = {
  341. .owner = THIS_MODULE,
  342. .name = DRIVER_NAME,
  343. },
  344. .probe = saa7706h_probe,
  345. .remove = saa7706h_remove,
  346. .id_table = saa7706h_id,
  347. };
  348. module_i2c_driver(saa7706h_driver);
  349. MODULE_DESCRIPTION("SAA7706H Car Radio DSP driver");
  350. MODULE_AUTHOR("Mocean Laboratories");
  351. MODULE_LICENSE("GPL v2");