adv7180.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_AD_PAL_BG_NTSC_J_SECAM 0x00
  32. #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
  33. #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM 0x20
  34. #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM 0x30
  35. #define ADV7180_INPUT_CONTROL_NTSC_J 0x40
  36. #define ADV7180_INPUT_CONTROL_NTSC_M 0x50
  37. #define ADV7180_INPUT_CONTROL_PAL60 0x60
  38. #define ADV7180_INPUT_CONTROL_NTSC_443 0x70
  39. #define ADV7180_INPUT_CONTROL_PAL_BG 0x80
  40. #define ADV7180_INPUT_CONTROL_PAL_N 0x90
  41. #define ADV7180_INPUT_CONTROL_PAL_M 0xa0
  42. #define ADV7180_INPUT_CONTROL_PAL_M_PED 0xb0
  43. #define ADV7180_INPUT_CONTROL_PAL_COMB_N 0xc0
  44. #define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED 0xd0
  45. #define ADV7180_INPUT_CONTROL_PAL_SECAM 0xe0
  46. #define ADV7180_INPUT_CONTROL_PAL_SECAM_PED 0xf0
  47. #define ADV7180_AUTODETECT_ENABLE_REG 0x07
  48. #define ADV7180_AUTODETECT_DEFAULT 0x7f
  49. #define ADV7180_STATUS1_REG 0x10
  50. #define ADV7180_STATUS1_IN_LOCK 0x01
  51. #define ADV7180_STATUS1_AUTOD_MASK 0x70
  52. #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
  53. #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
  54. #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
  55. #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
  56. #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
  57. #define ADV7180_STATUS1_AUTOD_SECAM 0x50
  58. #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
  59. #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
  60. #define ADV7180_IDENT_REG 0x11
  61. #define ADV7180_ID_7180 0x18
  62. struct adv7180_state {
  63. struct v4l2_subdev sd;
  64. };
  65. static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
  66. {
  67. switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
  68. case ADV7180_STATUS1_AUTOD_NTSM_M_J:
  69. return V4L2_STD_NTSC;
  70. case ADV7180_STATUS1_AUTOD_NTSC_4_43:
  71. return V4L2_STD_NTSC_443;
  72. case ADV7180_STATUS1_AUTOD_PAL_M:
  73. return V4L2_STD_PAL_M;
  74. case ADV7180_STATUS1_AUTOD_PAL_60:
  75. return V4L2_STD_PAL_60;
  76. case ADV7180_STATUS1_AUTOD_PAL_B_G:
  77. return V4L2_STD_PAL;
  78. case ADV7180_STATUS1_AUTOD_SECAM:
  79. return V4L2_STD_SECAM;
  80. case ADV7180_STATUS1_AUTOD_PAL_COMB:
  81. return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
  82. case ADV7180_STATUS1_AUTOD_SECAM_525:
  83. return V4L2_STD_SECAM;
  84. default:
  85. return V4L2_STD_UNKNOWN;
  86. }
  87. }
  88. static u32 adv7180_status_to_v4l2(u8 status1)
  89. {
  90. if (!(status1 & ADV7180_STATUS1_IN_LOCK))
  91. return V4L2_IN_ST_NO_SIGNAL;
  92. return 0;
  93. }
  94. static int __adv7180_status(struct i2c_client *client, u32 *status,
  95. v4l2_std_id *std)
  96. {
  97. int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
  98. if (status1 < 0)
  99. return status1;
  100. if (status)
  101. *status = adv7180_status_to_v4l2(status1);
  102. if (std)
  103. *std = adv7180_std_to_v4l2(status1);
  104. return 0;
  105. }
  106. static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
  107. {
  108. return container_of(sd, struct adv7180_state, sd);
  109. }
  110. static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  111. {
  112. return __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
  113. }
  114. static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
  115. {
  116. return __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
  117. }
  118. static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
  119. struct v4l2_dbg_chip_ident *chip)
  120. {
  121. struct i2c_client *client = v4l2_get_subdevdata(sd);
  122. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
  123. }
  124. static const struct v4l2_subdev_video_ops adv7180_video_ops = {
  125. .querystd = adv7180_querystd,
  126. .g_input_status = adv7180_g_input_status,
  127. };
  128. static const struct v4l2_subdev_core_ops adv7180_core_ops = {
  129. .g_chip_ident = adv7180_g_chip_ident,
  130. };
  131. static const struct v4l2_subdev_ops adv7180_ops = {
  132. .core = &adv7180_core_ops,
  133. .video = &adv7180_video_ops,
  134. };
  135. /*
  136. * Generic i2c probe
  137. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  138. */
  139. static int adv7180_probe(struct i2c_client *client,
  140. const struct i2c_device_id *id)
  141. {
  142. struct adv7180_state *state;
  143. struct v4l2_subdev *sd;
  144. int ret;
  145. /* Check if the adapter supports the needed features */
  146. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  147. return -EIO;
  148. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  149. client->addr << 1, client->adapter->name);
  150. state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
  151. if (state == NULL)
  152. return -ENOMEM;
  153. sd = &state->sd;
  154. v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
  155. /* Initialize adv7180 */
  156. /* enable autodetection */
  157. ret = i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  158. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM);
  159. if (ret > 0)
  160. ret = i2c_smbus_write_byte_data(client,
  161. ADV7180_AUTODETECT_ENABLE_REG,
  162. ADV7180_AUTODETECT_DEFAULT);
  163. if (ret < 0) {
  164. printk(KERN_ERR DRIVER_NAME
  165. ": Failed to communicate to chip: %d\n", ret);
  166. return ret;
  167. }
  168. return 0;
  169. }
  170. static int adv7180_remove(struct i2c_client *client)
  171. {
  172. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  173. v4l2_device_unregister_subdev(sd);
  174. kfree(to_state(sd));
  175. return 0;
  176. }
  177. static const struct i2c_device_id adv7180_id[] = {
  178. {DRIVER_NAME, 0},
  179. {},
  180. };
  181. MODULE_DEVICE_TABLE(i2c, adv7180_id);
  182. static struct i2c_driver adv7180_driver = {
  183. .driver = {
  184. .owner = THIS_MODULE,
  185. .name = DRIVER_NAME,
  186. },
  187. .probe = adv7180_probe,
  188. .remove = adv7180_remove,
  189. .id_table = adv7180_id,
  190. };
  191. static __init int adv7180_init(void)
  192. {
  193. return i2c_add_driver(&adv7180_driver);
  194. }
  195. static __exit void adv7180_exit(void)
  196. {
  197. i2c_del_driver(&adv7180_driver);
  198. }
  199. module_init(adv7180_init);
  200. module_exit(adv7180_exit);
  201. MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
  202. MODULE_AUTHOR("Mocean Laboratories");
  203. MODULE_LICENSE("GPL v2");