radio-tea5764.c 15 KB

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