adv7180.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <media/v4l2-ioctl.h>
  26. #include <linux/videodev2.h>
  27. #include <media/v4l2-device.h>
  28. #include <media/v4l2-chip-ident.h>
  29. #define DRIVER_NAME "adv7180"
  30. #define ADV7180_INPUT_CONTROL_REG 0x00
  31. #define ADV7180_INPUT_CONTROL_PAL_BG_NTSC_J_SECAM 0x00
  32. #define ADV7180_AUTODETECT_ENABLE_REG 0x07
  33. #define ADV7180_AUTODETECT_DEFAULT 0x7f
  34. #define ADV7180_STATUS1_REG 0x10
  35. #define ADV7180_STATUS1_AUTOD_MASK 0x70
  36. #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
  37. #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
  38. #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
  39. #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
  40. #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
  41. #define ADV7180_STATUS1_AUTOD_SECAM 0x50
  42. #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
  43. #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
  44. #define ADV7180_IDENT_REG 0x11
  45. #define ADV7180_ID_7180 0x18
  46. struct adv7180_state {
  47. struct v4l2_subdev sd;
  48. };
  49. static v4l2_std_id determine_norm(struct i2c_client *client)
  50. {
  51. u8 status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
  52. switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
  53. case ADV7180_STATUS1_AUTOD_NTSM_M_J:
  54. return V4L2_STD_NTSC_M_JP;
  55. case ADV7180_STATUS1_AUTOD_NTSC_4_43:
  56. return V4L2_STD_NTSC_443;
  57. case ADV7180_STATUS1_AUTOD_PAL_M:
  58. return V4L2_STD_PAL_M;
  59. case ADV7180_STATUS1_AUTOD_PAL_60:
  60. return V4L2_STD_PAL_60;
  61. case ADV7180_STATUS1_AUTOD_PAL_B_G:
  62. return V4L2_STD_PAL;
  63. case ADV7180_STATUS1_AUTOD_SECAM:
  64. return V4L2_STD_SECAM;
  65. case ADV7180_STATUS1_AUTOD_PAL_COMB:
  66. return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
  67. case ADV7180_STATUS1_AUTOD_SECAM_525:
  68. return V4L2_STD_SECAM;
  69. default:
  70. return V4L2_STD_UNKNOWN;
  71. }
  72. }
  73. static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
  74. {
  75. return container_of(sd, struct adv7180_state, sd);
  76. }
  77. static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  78. {
  79. struct i2c_client *client = v4l2_get_subdevdata(sd);
  80. *std = determine_norm(client);
  81. return 0;
  82. }
  83. static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
  84. struct v4l2_dbg_chip_ident *chip)
  85. {
  86. struct i2c_client *client = v4l2_get_subdevdata(sd);
  87. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
  88. }
  89. static const struct v4l2_subdev_video_ops adv7180_video_ops = {
  90. .querystd = adv7180_querystd,
  91. };
  92. static const struct v4l2_subdev_core_ops adv7180_core_ops = {
  93. .g_chip_ident = adv7180_g_chip_ident,
  94. };
  95. static const struct v4l2_subdev_ops adv7180_ops = {
  96. .core = &adv7180_core_ops,
  97. .video = &adv7180_video_ops,
  98. };
  99. /*
  100. * Generic i2c probe
  101. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  102. */
  103. static int adv7180_probe(struct i2c_client *client,
  104. const struct i2c_device_id *id)
  105. {
  106. struct adv7180_state *state;
  107. struct v4l2_subdev *sd;
  108. int ret;
  109. /* Check if the adapter supports the needed features */
  110. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  111. return -EIO;
  112. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  113. client->addr << 1, client->adapter->name);
  114. state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
  115. if (state == NULL)
  116. return -ENOMEM;
  117. sd = &state->sd;
  118. v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
  119. /* Initialize adv7180 */
  120. /* enable autodetection */
  121. ret = i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  122. ADV7180_INPUT_CONTROL_PAL_BG_NTSC_J_SECAM);
  123. if (ret > 0)
  124. ret = i2c_smbus_write_byte_data(client,
  125. ADV7180_AUTODETECT_ENABLE_REG,
  126. ADV7180_AUTODETECT_DEFAULT);
  127. if (ret < 0) {
  128. printk(KERN_ERR DRIVER_NAME
  129. ": Failed to communicate to chip: %d\n", ret);
  130. return ret;
  131. }
  132. return 0;
  133. }
  134. static int adv7180_remove(struct i2c_client *client)
  135. {
  136. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  137. v4l2_device_unregister_subdev(sd);
  138. kfree(to_state(sd));
  139. return 0;
  140. }
  141. static const struct i2c_device_id adv7180_id[] = {
  142. {DRIVER_NAME, 0},
  143. {},
  144. };
  145. MODULE_DEVICE_TABLE(i2c, adv7180_id);
  146. static struct i2c_driver adv7180_driver = {
  147. .driver = {
  148. .owner = THIS_MODULE,
  149. .name = DRIVER_NAME,
  150. },
  151. .probe = adv7180_probe,
  152. .remove = adv7180_remove,
  153. .id_table = adv7180_id,
  154. };
  155. static __init int adv7180_init(void)
  156. {
  157. return i2c_add_driver(&adv7180_driver);
  158. }
  159. static __exit void adv7180_exit(void)
  160. {
  161. i2c_del_driver(&adv7180_driver);
  162. }
  163. module_init(adv7180_init);
  164. module_exit(adv7180_exit);
  165. MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
  166. MODULE_AUTHOR("Mocean Laboratories");
  167. MODULE_LICENSE("GPL v2");