adv7180.c 12 KB

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