adv7180.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. v4l2_std_id curr_norm;
  65. bool autodetect;
  66. };
  67. static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
  68. {
  69. switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
  70. case ADV7180_STATUS1_AUTOD_NTSM_M_J:
  71. return V4L2_STD_NTSC;
  72. case ADV7180_STATUS1_AUTOD_NTSC_4_43:
  73. return V4L2_STD_NTSC_443;
  74. case ADV7180_STATUS1_AUTOD_PAL_M:
  75. return V4L2_STD_PAL_M;
  76. case ADV7180_STATUS1_AUTOD_PAL_60:
  77. return V4L2_STD_PAL_60;
  78. case ADV7180_STATUS1_AUTOD_PAL_B_G:
  79. return V4L2_STD_PAL;
  80. case ADV7180_STATUS1_AUTOD_SECAM:
  81. return V4L2_STD_SECAM;
  82. case ADV7180_STATUS1_AUTOD_PAL_COMB:
  83. return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
  84. case ADV7180_STATUS1_AUTOD_SECAM_525:
  85. return V4L2_STD_SECAM;
  86. default:
  87. return V4L2_STD_UNKNOWN;
  88. }
  89. }
  90. static int v4l2_std_to_adv7180(v4l2_std_id std)
  91. {
  92. if (std == V4L2_STD_PAL_60)
  93. return ADV7180_INPUT_CONTROL_PAL60;
  94. if (std == V4L2_STD_NTSC_443)
  95. return ADV7180_INPUT_CONTROL_NTSC_443;
  96. if (std == V4L2_STD_PAL_N)
  97. return ADV7180_INPUT_CONTROL_PAL_N;
  98. if (std == V4L2_STD_PAL_M)
  99. return ADV7180_INPUT_CONTROL_PAL_M;
  100. if (std == V4L2_STD_PAL_Nc)
  101. return ADV7180_INPUT_CONTROL_PAL_COMB_N;
  102. if (std & V4L2_STD_PAL)
  103. return ADV7180_INPUT_CONTROL_PAL_BG;
  104. if (std & V4L2_STD_NTSC)
  105. return ADV7180_INPUT_CONTROL_NTSC_M;
  106. if (std & V4L2_STD_SECAM)
  107. return ADV7180_INPUT_CONTROL_PAL_SECAM;
  108. return -EINVAL;
  109. }
  110. static u32 adv7180_status_to_v4l2(u8 status1)
  111. {
  112. if (!(status1 & ADV7180_STATUS1_IN_LOCK))
  113. return V4L2_IN_ST_NO_SIGNAL;
  114. return 0;
  115. }
  116. static int __adv7180_status(struct i2c_client *client, u32 *status,
  117. v4l2_std_id *std)
  118. {
  119. int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
  120. if (status1 < 0)
  121. return status1;
  122. if (status)
  123. *status = adv7180_status_to_v4l2(status1);
  124. if (std)
  125. *std = adv7180_std_to_v4l2(status1);
  126. return 0;
  127. }
  128. static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
  129. {
  130. return container_of(sd, struct adv7180_state, sd);
  131. }
  132. static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  133. {
  134. struct adv7180_state *state = to_state(sd);
  135. int err = 0;
  136. if (!state->autodetect)
  137. *std = state->curr_norm;
  138. else
  139. err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
  140. return err;
  141. }
  142. static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
  143. {
  144. return __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
  145. }
  146. static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
  147. struct v4l2_dbg_chip_ident *chip)
  148. {
  149. struct i2c_client *client = v4l2_get_subdevdata(sd);
  150. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
  151. }
  152. static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  153. {
  154. struct adv7180_state *state = to_state(sd);
  155. struct i2c_client *client = v4l2_get_subdevdata(sd);
  156. int ret;
  157. /* all standards -> autodetect */
  158. if (std == V4L2_STD_ALL) {
  159. ret = i2c_smbus_write_byte_data(client,
  160. ADV7180_INPUT_CONTROL_REG,
  161. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM);
  162. if (ret < 0)
  163. goto out;
  164. state->autodetect = true;
  165. } else {
  166. ret = v4l2_std_to_adv7180(std);
  167. if (ret < 0)
  168. goto out;
  169. ret = i2c_smbus_write_byte_data(client,
  170. ADV7180_INPUT_CONTROL_REG, ret);
  171. if (ret < 0)
  172. goto out;
  173. state->curr_norm = std;
  174. state->autodetect = false;
  175. }
  176. ret = 0;
  177. out:
  178. return ret;
  179. }
  180. static const struct v4l2_subdev_video_ops adv7180_video_ops = {
  181. .querystd = adv7180_querystd,
  182. .g_input_status = adv7180_g_input_status,
  183. };
  184. static const struct v4l2_subdev_core_ops adv7180_core_ops = {
  185. .g_chip_ident = adv7180_g_chip_ident,
  186. .s_std = adv7180_s_std,
  187. };
  188. static const struct v4l2_subdev_ops adv7180_ops = {
  189. .core = &adv7180_core_ops,
  190. .video = &adv7180_video_ops,
  191. };
  192. /*
  193. * Generic i2c probe
  194. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  195. */
  196. static int adv7180_probe(struct i2c_client *client,
  197. const struct i2c_device_id *id)
  198. {
  199. struct adv7180_state *state;
  200. struct v4l2_subdev *sd;
  201. int ret;
  202. /* Check if the adapter supports the needed features */
  203. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  204. return -EIO;
  205. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  206. client->addr << 1, client->adapter->name);
  207. state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
  208. if (state == NULL)
  209. return -ENOMEM;
  210. state->autodetect = true;
  211. sd = &state->sd;
  212. v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
  213. /* Initialize adv7180 */
  214. /* enable autodetection */
  215. ret = i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  216. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM);
  217. if (ret > 0)
  218. ret = i2c_smbus_write_byte_data(client,
  219. ADV7180_AUTODETECT_ENABLE_REG,
  220. ADV7180_AUTODETECT_DEFAULT);
  221. if (ret < 0) {
  222. printk(KERN_ERR DRIVER_NAME
  223. ": Failed to communicate to chip: %d\n", ret);
  224. return ret;
  225. }
  226. return 0;
  227. }
  228. static int adv7180_remove(struct i2c_client *client)
  229. {
  230. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  231. v4l2_device_unregister_subdev(sd);
  232. kfree(to_state(sd));
  233. return 0;
  234. }
  235. static const struct i2c_device_id adv7180_id[] = {
  236. {DRIVER_NAME, 0},
  237. {},
  238. };
  239. MODULE_DEVICE_TABLE(i2c, adv7180_id);
  240. static struct i2c_driver adv7180_driver = {
  241. .driver = {
  242. .owner = THIS_MODULE,
  243. .name = DRIVER_NAME,
  244. },
  245. .probe = adv7180_probe,
  246. .remove = adv7180_remove,
  247. .id_table = adv7180_id,
  248. };
  249. static __init int adv7180_init(void)
  250. {
  251. return i2c_add_driver(&adv7180_driver);
  252. }
  253. static __exit void adv7180_exit(void)
  254. {
  255. i2c_del_driver(&adv7180_driver);
  256. }
  257. module_init(adv7180_init);
  258. module_exit(adv7180_exit);
  259. MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
  260. MODULE_AUTHOR("Mocean Laboratories");
  261. MODULE_LICENSE("GPL v2");