adv7180.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * adv7180.c Analog Devices ADV7180 video decoder 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/errno.h>
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/i2c.h>
  24. #include <linux/i2c-id.h>
  25. #include <linux/slab.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include <linux/videodev2.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-chip-ident.h>
  30. #include <linux/mutex.h>
  31. #define DRIVER_NAME "adv7180"
  32. #define ADV7180_INPUT_CONTROL_REG 0x00
  33. #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00
  34. #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
  35. #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM 0x20
  36. #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM 0x30
  37. #define ADV7180_INPUT_CONTROL_NTSC_J 0x40
  38. #define ADV7180_INPUT_CONTROL_NTSC_M 0x50
  39. #define ADV7180_INPUT_CONTROL_PAL60 0x60
  40. #define ADV7180_INPUT_CONTROL_NTSC_443 0x70
  41. #define ADV7180_INPUT_CONTROL_PAL_BG 0x80
  42. #define ADV7180_INPUT_CONTROL_PAL_N 0x90
  43. #define ADV7180_INPUT_CONTROL_PAL_M 0xa0
  44. #define ADV7180_INPUT_CONTROL_PAL_M_PED 0xb0
  45. #define ADV7180_INPUT_CONTROL_PAL_COMB_N 0xc0
  46. #define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED 0xd0
  47. #define ADV7180_INPUT_CONTROL_PAL_SECAM 0xe0
  48. #define ADV7180_INPUT_CONTROL_PAL_SECAM_PED 0xf0
  49. #define ADV7180_EXTENDED_OUTPUT_CONTROL_REG 0x04
  50. #define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5
  51. #define ADV7180_AUTODETECT_ENABLE_REG 0x07
  52. #define ADV7180_AUTODETECT_DEFAULT 0x7f
  53. #define ADV7180_ADI_CTRL_REG 0x0e
  54. #define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
  55. #define ADV7180_STATUS1_REG 0x10
  56. #define ADV7180_STATUS1_IN_LOCK 0x01
  57. #define ADV7180_STATUS1_AUTOD_MASK 0x70
  58. #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
  59. #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
  60. #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
  61. #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
  62. #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
  63. #define ADV7180_STATUS1_AUTOD_SECAM 0x50
  64. #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
  65. #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
  66. #define ADV7180_IDENT_REG 0x11
  67. #define ADV7180_ID_7180 0x18
  68. #define ADV7180_ICONF1_ADI 0x40
  69. #define ADV7180_ICONF1_ACTIVE_LOW 0x01
  70. #define ADV7180_ICONF1_PSYNC_ONLY 0x10
  71. #define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
  72. #define ADV7180_IRQ1_LOCK 0x01
  73. #define ADV7180_IRQ1_UNLOCK 0x02
  74. #define ADV7180_ISR1_ADI 0x42
  75. #define ADV7180_ICR1_ADI 0x43
  76. #define ADV7180_IMR1_ADI 0x44
  77. #define ADV7180_IMR2_ADI 0x48
  78. #define ADV7180_IRQ3_AD_CHANGE 0x08
  79. #define ADV7180_ISR3_ADI 0x4A
  80. #define ADV7180_ICR3_ADI 0x4B
  81. #define ADV7180_IMR3_ADI 0x4C
  82. #define ADV7180_IMR4_ADI 0x50
  83. struct adv7180_state {
  84. struct v4l2_subdev sd;
  85. struct work_struct work;
  86. struct mutex mutex; /* mutual excl. when accessing chip */
  87. int irq;
  88. v4l2_std_id curr_norm;
  89. bool autodetect;
  90. };
  91. static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
  92. {
  93. switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
  94. case ADV7180_STATUS1_AUTOD_NTSM_M_J:
  95. return V4L2_STD_NTSC;
  96. case ADV7180_STATUS1_AUTOD_NTSC_4_43:
  97. return V4L2_STD_NTSC_443;
  98. case ADV7180_STATUS1_AUTOD_PAL_M:
  99. return V4L2_STD_PAL_M;
  100. case ADV7180_STATUS1_AUTOD_PAL_60:
  101. return V4L2_STD_PAL_60;
  102. case ADV7180_STATUS1_AUTOD_PAL_B_G:
  103. return V4L2_STD_PAL;
  104. case ADV7180_STATUS1_AUTOD_SECAM:
  105. return V4L2_STD_SECAM;
  106. case ADV7180_STATUS1_AUTOD_PAL_COMB:
  107. return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
  108. case ADV7180_STATUS1_AUTOD_SECAM_525:
  109. return V4L2_STD_SECAM;
  110. default:
  111. return V4L2_STD_UNKNOWN;
  112. }
  113. }
  114. static int v4l2_std_to_adv7180(v4l2_std_id std)
  115. {
  116. if (std == V4L2_STD_PAL_60)
  117. return ADV7180_INPUT_CONTROL_PAL60;
  118. if (std == V4L2_STD_NTSC_443)
  119. return ADV7180_INPUT_CONTROL_NTSC_443;
  120. if (std == V4L2_STD_PAL_N)
  121. return ADV7180_INPUT_CONTROL_PAL_N;
  122. if (std == V4L2_STD_PAL_M)
  123. return ADV7180_INPUT_CONTROL_PAL_M;
  124. if (std == V4L2_STD_PAL_Nc)
  125. return ADV7180_INPUT_CONTROL_PAL_COMB_N;
  126. if (std & V4L2_STD_PAL)
  127. return ADV7180_INPUT_CONTROL_PAL_BG;
  128. if (std & V4L2_STD_NTSC)
  129. return ADV7180_INPUT_CONTROL_NTSC_M;
  130. if (std & V4L2_STD_SECAM)
  131. return ADV7180_INPUT_CONTROL_PAL_SECAM;
  132. return -EINVAL;
  133. }
  134. static u32 adv7180_status_to_v4l2(u8 status1)
  135. {
  136. if (!(status1 & ADV7180_STATUS1_IN_LOCK))
  137. return V4L2_IN_ST_NO_SIGNAL;
  138. return 0;
  139. }
  140. static int __adv7180_status(struct i2c_client *client, u32 *status,
  141. v4l2_std_id *std)
  142. {
  143. int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
  144. if (status1 < 0)
  145. return status1;
  146. if (status)
  147. *status = adv7180_status_to_v4l2(status1);
  148. if (std)
  149. *std = adv7180_std_to_v4l2(status1);
  150. return 0;
  151. }
  152. static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
  153. {
  154. return container_of(sd, struct adv7180_state, sd);
  155. }
  156. static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  157. {
  158. struct adv7180_state *state = to_state(sd);
  159. int err = mutex_lock_interruptible(&state->mutex);
  160. if (err)
  161. return err;
  162. /* when we are interrupt driven we know the state */
  163. if (!state->autodetect || state->irq > 0)
  164. *std = state->curr_norm;
  165. else
  166. err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
  167. mutex_unlock(&state->mutex);
  168. return err;
  169. }
  170. static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
  171. {
  172. struct adv7180_state *state = to_state(sd);
  173. int ret = mutex_lock_interruptible(&state->mutex);
  174. if (ret)
  175. return ret;
  176. ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
  177. mutex_unlock(&state->mutex);
  178. return ret;
  179. }
  180. static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
  181. struct v4l2_dbg_chip_ident *chip)
  182. {
  183. struct i2c_client *client = v4l2_get_subdevdata(sd);
  184. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
  185. }
  186. static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  187. {
  188. struct adv7180_state *state = to_state(sd);
  189. struct i2c_client *client = v4l2_get_subdevdata(sd);
  190. int ret = mutex_lock_interruptible(&state->mutex);
  191. if (ret)
  192. return ret;
  193. /* all standards -> autodetect */
  194. if (std == V4L2_STD_ALL) {
  195. ret = i2c_smbus_write_byte_data(client,
  196. ADV7180_INPUT_CONTROL_REG,
  197. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM);
  198. if (ret < 0)
  199. goto out;
  200. __adv7180_status(client, NULL, &state->curr_norm);
  201. state->autodetect = true;
  202. } else {
  203. ret = v4l2_std_to_adv7180(std);
  204. if (ret < 0)
  205. goto out;
  206. ret = i2c_smbus_write_byte_data(client,
  207. ADV7180_INPUT_CONTROL_REG, ret);
  208. if (ret < 0)
  209. goto out;
  210. state->curr_norm = std;
  211. state->autodetect = false;
  212. }
  213. ret = 0;
  214. out:
  215. mutex_unlock(&state->mutex);
  216. return ret;
  217. }
  218. static const struct v4l2_subdev_video_ops adv7180_video_ops = {
  219. .querystd = adv7180_querystd,
  220. .g_input_status = adv7180_g_input_status,
  221. };
  222. static const struct v4l2_subdev_core_ops adv7180_core_ops = {
  223. .g_chip_ident = adv7180_g_chip_ident,
  224. .s_std = adv7180_s_std,
  225. };
  226. static const struct v4l2_subdev_ops adv7180_ops = {
  227. .core = &adv7180_core_ops,
  228. .video = &adv7180_video_ops,
  229. };
  230. static void adv7180_work(struct work_struct *work)
  231. {
  232. struct adv7180_state *state = container_of(work, struct adv7180_state,
  233. work);
  234. struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
  235. u8 isr3;
  236. mutex_lock(&state->mutex);
  237. i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  238. ADV7180_ADI_CTRL_IRQ_SPACE);
  239. isr3 = i2c_smbus_read_byte_data(client, ADV7180_ISR3_ADI);
  240. /* clear */
  241. i2c_smbus_write_byte_data(client, ADV7180_ICR3_ADI, isr3);
  242. i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG, 0);
  243. if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
  244. __adv7180_status(client, NULL, &state->curr_norm);
  245. mutex_unlock(&state->mutex);
  246. enable_irq(state->irq);
  247. }
  248. static irqreturn_t adv7180_irq(int irq, void *devid)
  249. {
  250. struct adv7180_state *state = devid;
  251. schedule_work(&state->work);
  252. disable_irq_nosync(state->irq);
  253. return IRQ_HANDLED;
  254. }
  255. /*
  256. * Generic i2c probe
  257. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  258. */
  259. static __devinit int adv7180_probe(struct i2c_client *client,
  260. const struct i2c_device_id *id)
  261. {
  262. struct adv7180_state *state;
  263. struct v4l2_subdev *sd;
  264. int ret;
  265. /* Check if the adapter supports the needed features */
  266. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  267. return -EIO;
  268. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  269. client->addr << 1, client->adapter->name);
  270. state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
  271. if (state == NULL) {
  272. ret = -ENOMEM;
  273. goto err;
  274. }
  275. state->irq = client->irq;
  276. INIT_WORK(&state->work, adv7180_work);
  277. mutex_init(&state->mutex);
  278. state->autodetect = true;
  279. sd = &state->sd;
  280. v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
  281. /* Initialize adv7180 */
  282. /* Enable autodetection */
  283. ret = i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  284. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM);
  285. if (ret < 0)
  286. goto err_unreg_subdev;
  287. ret = i2c_smbus_write_byte_data(client, ADV7180_AUTODETECT_ENABLE_REG,
  288. ADV7180_AUTODETECT_DEFAULT);
  289. if (ret < 0)
  290. goto err_unreg_subdev;
  291. /* ITU-R BT.656-4 compatible */
  292. ret = i2c_smbus_write_byte_data(client,
  293. ADV7180_EXTENDED_OUTPUT_CONTROL_REG,
  294. ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
  295. if (ret < 0)
  296. goto err_unreg_subdev;
  297. /* read current norm */
  298. __adv7180_status(client, NULL, &state->curr_norm);
  299. /* register for interrupts */
  300. if (state->irq > 0) {
  301. ret = request_irq(state->irq, adv7180_irq, 0, DRIVER_NAME,
  302. state);
  303. if (ret)
  304. goto err_unreg_subdev;
  305. ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  306. ADV7180_ADI_CTRL_IRQ_SPACE);
  307. if (ret < 0)
  308. goto err_unreg_subdev;
  309. /* config the Interrupt pin to be active low */
  310. ret = i2c_smbus_write_byte_data(client, ADV7180_ICONF1_ADI,
  311. ADV7180_ICONF1_ACTIVE_LOW | ADV7180_ICONF1_PSYNC_ONLY);
  312. if (ret < 0)
  313. goto err_unreg_subdev;
  314. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR1_ADI, 0);
  315. if (ret < 0)
  316. goto err_unreg_subdev;
  317. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR2_ADI, 0);
  318. if (ret < 0)
  319. goto err_unreg_subdev;
  320. /* enable AD change interrupts interrupts */
  321. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR3_ADI,
  322. ADV7180_IRQ3_AD_CHANGE);
  323. if (ret < 0)
  324. goto err_unreg_subdev;
  325. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR4_ADI, 0);
  326. if (ret < 0)
  327. goto err_unreg_subdev;
  328. ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  329. 0);
  330. if (ret < 0)
  331. goto err_unreg_subdev;
  332. }
  333. return 0;
  334. err_unreg_subdev:
  335. mutex_destroy(&state->mutex);
  336. v4l2_device_unregister_subdev(sd);
  337. kfree(state);
  338. err:
  339. printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", ret);
  340. return ret;
  341. }
  342. static __devexit int adv7180_remove(struct i2c_client *client)
  343. {
  344. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  345. struct adv7180_state *state = to_state(sd);
  346. if (state->irq > 0) {
  347. free_irq(client->irq, state);
  348. if (cancel_work_sync(&state->work)) {
  349. /*
  350. * Work was pending, therefore we need to enable
  351. * IRQ here to balance the disable_irq() done in the
  352. * interrupt handler.
  353. */
  354. enable_irq(state->irq);
  355. }
  356. }
  357. mutex_destroy(&state->mutex);
  358. v4l2_device_unregister_subdev(sd);
  359. kfree(to_state(sd));
  360. return 0;
  361. }
  362. static const struct i2c_device_id adv7180_id[] = {
  363. {DRIVER_NAME, 0},
  364. {},
  365. };
  366. MODULE_DEVICE_TABLE(i2c, adv7180_id);
  367. static struct i2c_driver adv7180_driver = {
  368. .driver = {
  369. .owner = THIS_MODULE,
  370. .name = DRIVER_NAME,
  371. },
  372. .probe = adv7180_probe,
  373. .remove = __devexit_p(adv7180_remove),
  374. .id_table = adv7180_id,
  375. };
  376. static __init int adv7180_init(void)
  377. {
  378. return i2c_add_driver(&adv7180_driver);
  379. }
  380. static __exit void adv7180_exit(void)
  381. {
  382. i2c_del_driver(&adv7180_driver);
  383. }
  384. module_init(adv7180_init);
  385. module_exit(adv7180_exit);
  386. MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
  387. MODULE_AUTHOR("Mocean Laboratories");
  388. MODULE_LICENSE("GPL v2");