radio-tea5764.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * driver/media/radio/radio-tea5764.c
  3. *
  4. * Driver for TEA5764 radio chip for linux 2.6.
  5. * This driver is for TEA5764 chip from NXP, used in EZX phones from Motorola.
  6. * The I2C protocol is used for communicate with chip.
  7. *
  8. * Based in radio-tea5761.c Copyright (C) 2005 Nokia Corporation
  9. *
  10. * Copyright (c) 2008 Fabio Belavenuto <belavenuto@gmail.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * History:
  23. * 2008-12-06 Fabio Belavenuto <belavenuto@gmail.com>
  24. * initial code
  25. *
  26. * TODO:
  27. * add platform_data support for IRQs platform dependencies
  28. * add RDS support
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h> /* Initdata */
  34. #include <linux/videodev2.h> /* kernel radio structs */
  35. #include <linux/i2c.h> /* I2C */
  36. #include <media/v4l2-common.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-ctrls.h>
  40. #include <media/v4l2-event.h>
  41. #define DRIVER_VERSION "0.0.2"
  42. #define DRIVER_AUTHOR "Fabio Belavenuto <belavenuto@gmail.com>"
  43. #define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones."
  44. #define PINFO(format, ...)\
  45. printk(KERN_INFO KBUILD_MODNAME ": "\
  46. DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
  47. #define PWARN(format, ...)\
  48. printk(KERN_WARNING KBUILD_MODNAME ": "\
  49. DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
  50. # define PDEBUG(format, ...)\
  51. printk(KERN_DEBUG KBUILD_MODNAME ": "\
  52. DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
  53. /* Frequency limits in MHz -- these are European values. For Japanese
  54. devices, that would be 76000 and 91000. */
  55. #define FREQ_MIN 87500U
  56. #define FREQ_MAX 108000U
  57. #define FREQ_MUL 16
  58. /* TEA5764 registers */
  59. #define TEA5764_MANID 0x002b
  60. #define TEA5764_CHIPID 0x5764
  61. #define TEA5764_INTREG_BLMSK 0x0001
  62. #define TEA5764_INTREG_FRRMSK 0x0002
  63. #define TEA5764_INTREG_LEVMSK 0x0008
  64. #define TEA5764_INTREG_IFMSK 0x0010
  65. #define TEA5764_INTREG_BLMFLAG 0x0100
  66. #define TEA5764_INTREG_FRRFLAG 0x0200
  67. #define TEA5764_INTREG_LEVFLAG 0x0800
  68. #define TEA5764_INTREG_IFFLAG 0x1000
  69. #define TEA5764_FRQSET_SUD 0x8000
  70. #define TEA5764_FRQSET_SM 0x4000
  71. #define TEA5764_TNCTRL_PUPD1 0x8000
  72. #define TEA5764_TNCTRL_PUPD0 0x4000
  73. #define TEA5764_TNCTRL_BLIM 0x2000
  74. #define TEA5764_TNCTRL_SWPM 0x1000
  75. #define TEA5764_TNCTRL_IFCTC 0x0800
  76. #define TEA5764_TNCTRL_AFM 0x0400
  77. #define TEA5764_TNCTRL_SMUTE 0x0200
  78. #define TEA5764_TNCTRL_SNC 0x0100
  79. #define TEA5764_TNCTRL_MU 0x0080
  80. #define TEA5764_TNCTRL_SSL1 0x0040
  81. #define TEA5764_TNCTRL_SSL0 0x0020
  82. #define TEA5764_TNCTRL_HLSI 0x0010
  83. #define TEA5764_TNCTRL_MST 0x0008
  84. #define TEA5764_TNCTRL_SWP 0x0004
  85. #define TEA5764_TNCTRL_DTC 0x0002
  86. #define TEA5764_TNCTRL_AHLSI 0x0001
  87. #define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4)
  88. #define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
  89. #define TEA5764_TUNCHK_TUNTO 0x0100
  90. #define TEA5764_TUNCHK_LD 0x0008
  91. #define TEA5764_TUNCHK_STEREO 0x0004
  92. #define TEA5764_TESTREG_TRIGFR 0x0800
  93. struct tea5764_regs {
  94. u16 intreg; /* INTFLAG & INTMSK */
  95. u16 frqset; /* FRQSETMSB & FRQSETLSB */
  96. u16 tnctrl; /* TNCTRL1 & TNCTRL2 */
  97. u16 frqchk; /* FRQCHKMSB & FRQCHKLSB */
  98. u16 tunchk; /* IFCHK & LEVCHK */
  99. u16 testreg; /* TESTBITS & TESTMODE */
  100. u16 rdsstat; /* RDSSTAT1 & RDSSTAT2 */
  101. u16 rdslb; /* RDSLBMSB & RDSLBLSB */
  102. u16 rdspb; /* RDSPBMSB & RDSPBLSB */
  103. u16 rdsbc; /* RDSBBC & RDSGBC */
  104. u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
  105. u16 rdsbbl; /* PAUSEDET & RDSBBL */
  106. u16 manid; /* MANID1 & MANID2 */
  107. u16 chipid; /* CHIPID1 & CHIPID2 */
  108. } __attribute__ ((packed));
  109. struct tea5764_write_regs {
  110. u8 intreg; /* INTMSK */
  111. __be16 frqset; /* FRQSETMSB & FRQSETLSB */
  112. __be16 tnctrl; /* TNCTRL1 & TNCTRL2 */
  113. __be16 testreg; /* TESTBITS & TESTMODE */
  114. __be16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
  115. __be16 rdsbbl; /* PAUSEDET & RDSBBL */
  116. } __attribute__ ((packed));
  117. #ifdef CONFIG_RADIO_TEA5764_XTAL
  118. #define RADIO_TEA5764_XTAL 1
  119. #else
  120. #define RADIO_TEA5764_XTAL 0
  121. #endif
  122. static int radio_nr = -1;
  123. static int use_xtal = RADIO_TEA5764_XTAL;
  124. struct tea5764_device {
  125. struct v4l2_device v4l2_dev;
  126. struct v4l2_ctrl_handler ctrl_handler;
  127. struct i2c_client *i2c_client;
  128. struct video_device vdev;
  129. struct tea5764_regs regs;
  130. struct mutex mutex;
  131. };
  132. /* I2C code related */
  133. static int tea5764_i2c_read(struct tea5764_device *radio)
  134. {
  135. int i;
  136. u16 *p = (u16 *) &radio->regs;
  137. struct i2c_msg msgs[1] = {
  138. { .addr = radio->i2c_client->addr,
  139. .flags = I2C_M_RD,
  140. .len = sizeof(radio->regs),
  141. .buf = (void *)&radio->regs
  142. },
  143. };
  144. if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
  145. return -EIO;
  146. for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++)
  147. p[i] = __be16_to_cpu((__force __be16)p[i]);
  148. return 0;
  149. }
  150. static int tea5764_i2c_write(struct tea5764_device *radio)
  151. {
  152. struct tea5764_write_regs wr;
  153. struct tea5764_regs *r = &radio->regs;
  154. struct i2c_msg msgs[1] = {
  155. {
  156. .addr = radio->i2c_client->addr,
  157. .len = sizeof(wr),
  158. .buf = (void *)&wr
  159. },
  160. };
  161. wr.intreg = r->intreg & 0xff;
  162. wr.frqset = __cpu_to_be16(r->frqset);
  163. wr.tnctrl = __cpu_to_be16(r->tnctrl);
  164. wr.testreg = __cpu_to_be16(r->testreg);
  165. wr.rdsctrl = __cpu_to_be16(r->rdsctrl);
  166. wr.rdsbbl = __cpu_to_be16(r->rdsbbl);
  167. if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
  168. return -EIO;
  169. return 0;
  170. }
  171. static void tea5764_power_up(struct tea5764_device *radio)
  172. {
  173. struct tea5764_regs *r = &radio->regs;
  174. if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) {
  175. r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU |
  176. TEA5764_TNCTRL_HLSI);
  177. if (!use_xtal)
  178. r->testreg |= TEA5764_TESTREG_TRIGFR;
  179. else
  180. r->testreg &= ~TEA5764_TESTREG_TRIGFR;
  181. r->tnctrl |= TEA5764_TNCTRL_PUPD0;
  182. tea5764_i2c_write(radio);
  183. }
  184. }
  185. static void tea5764_power_down(struct tea5764_device *radio)
  186. {
  187. struct tea5764_regs *r = &radio->regs;
  188. if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
  189. r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
  190. tea5764_i2c_write(radio);
  191. }
  192. }
  193. static void tea5764_set_freq(struct tea5764_device *radio, int freq)
  194. {
  195. struct tea5764_regs *r = &radio->regs;
  196. /* formula: (freq [+ or -] 225000) / 8192 */
  197. if (r->tnctrl & TEA5764_TNCTRL_HLSI)
  198. r->frqset = (freq + 225000) / 8192;
  199. else
  200. r->frqset = (freq - 225000) / 8192;
  201. }
  202. static int tea5764_get_freq(struct tea5764_device *radio)
  203. {
  204. struct tea5764_regs *r = &radio->regs;
  205. if (r->tnctrl & TEA5764_TNCTRL_HLSI)
  206. return (r->frqchk * 8192) - 225000;
  207. else
  208. return (r->frqchk * 8192) + 225000;
  209. }
  210. /* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
  211. static void tea5764_tune(struct tea5764_device *radio, int freq)
  212. {
  213. tea5764_set_freq(radio, freq);
  214. if (tea5764_i2c_write(radio))
  215. PWARN("Could not set frequency!");
  216. }
  217. static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
  218. {
  219. struct tea5764_regs *r = &radio->regs;
  220. int tnctrl = r->tnctrl;
  221. if (audmode == V4L2_TUNER_MODE_MONO)
  222. r->tnctrl |= TEA5764_TNCTRL_MST;
  223. else
  224. r->tnctrl &= ~TEA5764_TNCTRL_MST;
  225. if (tnctrl != r->tnctrl)
  226. tea5764_i2c_write(radio);
  227. }
  228. static int tea5764_get_audout_mode(struct tea5764_device *radio)
  229. {
  230. struct tea5764_regs *r = &radio->regs;
  231. if (r->tnctrl & TEA5764_TNCTRL_MST)
  232. return V4L2_TUNER_MODE_MONO;
  233. else
  234. return V4L2_TUNER_MODE_STEREO;
  235. }
  236. static void tea5764_mute(struct tea5764_device *radio, int on)
  237. {
  238. struct tea5764_regs *r = &radio->regs;
  239. int tnctrl = r->tnctrl;
  240. if (on)
  241. r->tnctrl |= TEA5764_TNCTRL_MU;
  242. else
  243. r->tnctrl &= ~TEA5764_TNCTRL_MU;
  244. if (tnctrl != r->tnctrl)
  245. tea5764_i2c_write(radio);
  246. }
  247. /* V4L2 vidioc */
  248. static int vidioc_querycap(struct file *file, void *priv,
  249. struct v4l2_capability *v)
  250. {
  251. struct tea5764_device *radio = video_drvdata(file);
  252. struct video_device *dev = &radio->vdev;
  253. strlcpy(v->driver, dev->dev.driver->name, sizeof(v->driver));
  254. strlcpy(v->card, dev->name, sizeof(v->card));
  255. snprintf(v->bus_info, sizeof(v->bus_info),
  256. "I2C:%s", dev_name(&dev->dev));
  257. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  258. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  259. return 0;
  260. }
  261. static int vidioc_g_tuner(struct file *file, void *priv,
  262. struct v4l2_tuner *v)
  263. {
  264. struct tea5764_device *radio = video_drvdata(file);
  265. struct tea5764_regs *r = &radio->regs;
  266. if (v->index > 0)
  267. return -EINVAL;
  268. strlcpy(v->name, "FM", sizeof(v->name));
  269. v->type = V4L2_TUNER_RADIO;
  270. tea5764_i2c_read(radio);
  271. v->rangelow = FREQ_MIN * FREQ_MUL;
  272. v->rangehigh = FREQ_MAX * FREQ_MUL;
  273. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  274. if (r->tunchk & TEA5764_TUNCHK_STEREO)
  275. v->rxsubchans = V4L2_TUNER_SUB_STEREO;
  276. else
  277. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  278. v->audmode = tea5764_get_audout_mode(radio);
  279. v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf;
  280. v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk);
  281. return 0;
  282. }
  283. static int vidioc_s_tuner(struct file *file, void *priv,
  284. const struct v4l2_tuner *v)
  285. {
  286. struct tea5764_device *radio = video_drvdata(file);
  287. if (v->index > 0)
  288. return -EINVAL;
  289. tea5764_set_audout_mode(radio, v->audmode);
  290. return 0;
  291. }
  292. static int vidioc_s_frequency(struct file *file, void *priv,
  293. const struct v4l2_frequency *f)
  294. {
  295. struct tea5764_device *radio = video_drvdata(file);
  296. unsigned freq = f->frequency;
  297. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  298. return -EINVAL;
  299. if (freq == 0) {
  300. /* We special case this as a power down control. */
  301. tea5764_power_down(radio);
  302. /* Yes, that's what is returned in this case. This
  303. whole special case is non-compliant and should really
  304. be replaced with something better, but changing this
  305. might well break code that depends on this behavior.
  306. So we keep it as-is. */
  307. return -EINVAL;
  308. }
  309. freq = clamp(freq, FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL);
  310. tea5764_power_up(radio);
  311. tea5764_tune(radio, (freq * 125) / 2);
  312. return 0;
  313. }
  314. static int vidioc_g_frequency(struct file *file, void *priv,
  315. struct v4l2_frequency *f)
  316. {
  317. struct tea5764_device *radio = video_drvdata(file);
  318. struct tea5764_regs *r = &radio->regs;
  319. if (f->tuner != 0)
  320. return -EINVAL;
  321. tea5764_i2c_read(radio);
  322. f->type = V4L2_TUNER_RADIO;
  323. if (r->tnctrl & TEA5764_TNCTRL_PUPD0)
  324. f->frequency = (tea5764_get_freq(radio) * 2) / 125;
  325. else
  326. f->frequency = 0;
  327. return 0;
  328. }
  329. static int tea5764_s_ctrl(struct v4l2_ctrl *ctrl)
  330. {
  331. struct tea5764_device *radio =
  332. container_of(ctrl->handler, struct tea5764_device, ctrl_handler);
  333. switch (ctrl->id) {
  334. case V4L2_CID_AUDIO_MUTE:
  335. tea5764_mute(radio, ctrl->val);
  336. return 0;
  337. }
  338. return -EINVAL;
  339. }
  340. static const struct v4l2_ctrl_ops tea5764_ctrl_ops = {
  341. .s_ctrl = tea5764_s_ctrl,
  342. };
  343. /* File system interface */
  344. static const struct v4l2_file_operations tea5764_fops = {
  345. .owner = THIS_MODULE,
  346. .open = v4l2_fh_open,
  347. .release = v4l2_fh_release,
  348. .poll = v4l2_ctrl_poll,
  349. .unlocked_ioctl = video_ioctl2,
  350. };
  351. static const struct v4l2_ioctl_ops tea5764_ioctl_ops = {
  352. .vidioc_querycap = vidioc_querycap,
  353. .vidioc_g_tuner = vidioc_g_tuner,
  354. .vidioc_s_tuner = vidioc_s_tuner,
  355. .vidioc_g_frequency = vidioc_g_frequency,
  356. .vidioc_s_frequency = vidioc_s_frequency,
  357. .vidioc_log_status = v4l2_ctrl_log_status,
  358. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  359. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  360. };
  361. /* V4L2 interface */
  362. static const struct video_device tea5764_radio_template = {
  363. .name = "TEA5764 FM-Radio",
  364. .fops = &tea5764_fops,
  365. .ioctl_ops = &tea5764_ioctl_ops,
  366. .release = video_device_release_empty,
  367. };
  368. /* I2C probe: check if the device exists and register with v4l if it is */
  369. static int tea5764_i2c_probe(struct i2c_client *client,
  370. const struct i2c_device_id *id)
  371. {
  372. struct tea5764_device *radio;
  373. struct v4l2_device *v4l2_dev;
  374. struct v4l2_ctrl_handler *hdl;
  375. struct tea5764_regs *r;
  376. int ret;
  377. PDEBUG("probe");
  378. radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL);
  379. if (!radio)
  380. return -ENOMEM;
  381. v4l2_dev = &radio->v4l2_dev;
  382. ret = v4l2_device_register(&client->dev, v4l2_dev);
  383. if (ret < 0) {
  384. v4l2_err(v4l2_dev, "could not register v4l2_device\n");
  385. goto errfr;
  386. }
  387. hdl = &radio->ctrl_handler;
  388. v4l2_ctrl_handler_init(hdl, 1);
  389. v4l2_ctrl_new_std(hdl, &tea5764_ctrl_ops,
  390. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  391. v4l2_dev->ctrl_handler = hdl;
  392. if (hdl->error) {
  393. ret = hdl->error;
  394. v4l2_err(v4l2_dev, "Could not register controls\n");
  395. goto errunreg;
  396. }
  397. mutex_init(&radio->mutex);
  398. radio->i2c_client = client;
  399. ret = tea5764_i2c_read(radio);
  400. if (ret)
  401. goto errunreg;
  402. r = &radio->regs;
  403. PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid);
  404. if (r->chipid != TEA5764_CHIPID ||
  405. (r->manid & 0x0fff) != TEA5764_MANID) {
  406. PWARN("This chip is not a TEA5764!");
  407. ret = -EINVAL;
  408. goto errunreg;
  409. }
  410. radio->vdev = tea5764_radio_template;
  411. i2c_set_clientdata(client, radio);
  412. video_set_drvdata(&radio->vdev, radio);
  413. radio->vdev.lock = &radio->mutex;
  414. radio->vdev.v4l2_dev = v4l2_dev;
  415. /* initialize and power off the chip */
  416. tea5764_i2c_read(radio);
  417. tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO);
  418. tea5764_mute(radio, 1);
  419. tea5764_power_down(radio);
  420. ret = video_register_device(&radio->vdev, VFL_TYPE_RADIO, radio_nr);
  421. if (ret < 0) {
  422. PWARN("Could not register video device!");
  423. goto errunreg;
  424. }
  425. PINFO("registered.");
  426. return 0;
  427. errunreg:
  428. v4l2_ctrl_handler_free(hdl);
  429. v4l2_device_unregister(v4l2_dev);
  430. errfr:
  431. kfree(radio);
  432. return ret;
  433. }
  434. static int tea5764_i2c_remove(struct i2c_client *client)
  435. {
  436. struct tea5764_device *radio = i2c_get_clientdata(client);
  437. PDEBUG("remove");
  438. if (radio) {
  439. tea5764_power_down(radio);
  440. video_unregister_device(&radio->vdev);
  441. v4l2_ctrl_handler_free(&radio->ctrl_handler);
  442. v4l2_device_unregister(&radio->v4l2_dev);
  443. kfree(radio);
  444. }
  445. return 0;
  446. }
  447. /* I2C subsystem interface */
  448. static const struct i2c_device_id tea5764_id[] = {
  449. { "radio-tea5764", 0 },
  450. { } /* Terminating entry */
  451. };
  452. MODULE_DEVICE_TABLE(i2c, tea5764_id);
  453. static struct i2c_driver tea5764_i2c_driver = {
  454. .driver = {
  455. .name = "radio-tea5764",
  456. },
  457. .probe = tea5764_i2c_probe,
  458. .remove = tea5764_i2c_remove,
  459. .id_table = tea5764_id,
  460. };
  461. module_i2c_driver(tea5764_i2c_driver);
  462. MODULE_AUTHOR(DRIVER_AUTHOR);
  463. MODULE_DESCRIPTION(DRIVER_DESC);
  464. MODULE_LICENSE("GPL");
  465. MODULE_VERSION(DRIVER_VERSION);
  466. module_param(use_xtal, int, 0);
  467. MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board");
  468. module_param(radio_nr, int, 0);
  469. MODULE_PARM_DESC(radio_nr, "video4linux device number to use");