adv7180.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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-ctrls.h>
  29. #include <media/v4l2-chip-ident.h>
  30. #include <linux/mutex.h>
  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_INPUT_CONTROL_INSEL_MASK 0x0f
  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. /* Contrast */
  54. #define ADV7180_CON_REG 0x08 /*Unsigned */
  55. #define ADV7180_CON_MIN 0
  56. #define ADV7180_CON_DEF 128
  57. #define ADV7180_CON_MAX 255
  58. /* Brightness*/
  59. #define ADV7180_BRI_REG 0x0a /*Signed */
  60. #define ADV7180_BRI_MIN -128
  61. #define ADV7180_BRI_DEF 0
  62. #define ADV7180_BRI_MAX 127
  63. /* Hue */
  64. #define ADV7180_HUE_REG 0x0b /*Signed, inverted */
  65. #define ADV7180_HUE_MIN -127
  66. #define ADV7180_HUE_DEF 0
  67. #define ADV7180_HUE_MAX 128
  68. #define ADV7180_ADI_CTRL_REG 0x0e
  69. #define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
  70. #define ADV7180_PWR_MAN_REG 0x0f
  71. #define ADV7180_PWR_MAN_ON 0x04
  72. #define ADV7180_PWR_MAN_OFF 0x24
  73. #define ADV7180_PWR_MAN_RES 0x80
  74. #define ADV7180_STATUS1_REG 0x10
  75. #define ADV7180_STATUS1_IN_LOCK 0x01
  76. #define ADV7180_STATUS1_AUTOD_MASK 0x70
  77. #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
  78. #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
  79. #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
  80. #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
  81. #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
  82. #define ADV7180_STATUS1_AUTOD_SECAM 0x50
  83. #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
  84. #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
  85. #define ADV7180_IDENT_REG 0x11
  86. #define ADV7180_ID_7180 0x18
  87. #define ADV7180_ICONF1_ADI 0x40
  88. #define ADV7180_ICONF1_ACTIVE_LOW 0x01
  89. #define ADV7180_ICONF1_PSYNC_ONLY 0x10
  90. #define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
  91. /* Saturation */
  92. #define ADV7180_SD_SAT_CB_REG 0xe3 /*Unsigned */
  93. #define ADV7180_SD_SAT_CR_REG 0xe4 /*Unsigned */
  94. #define ADV7180_SAT_MIN 0
  95. #define ADV7180_SAT_DEF 128
  96. #define ADV7180_SAT_MAX 255
  97. #define ADV7180_IRQ1_LOCK 0x01
  98. #define ADV7180_IRQ1_UNLOCK 0x02
  99. #define ADV7180_ISR1_ADI 0x42
  100. #define ADV7180_ICR1_ADI 0x43
  101. #define ADV7180_IMR1_ADI 0x44
  102. #define ADV7180_IMR2_ADI 0x48
  103. #define ADV7180_IRQ3_AD_CHANGE 0x08
  104. #define ADV7180_ISR3_ADI 0x4A
  105. #define ADV7180_ICR3_ADI 0x4B
  106. #define ADV7180_IMR3_ADI 0x4C
  107. #define ADV7180_IMR4_ADI 0x50
  108. #define ADV7180_NTSC_V_BIT_END_REG 0xE6
  109. #define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F
  110. struct adv7180_state {
  111. struct v4l2_ctrl_handler ctrl_hdl;
  112. struct v4l2_subdev sd;
  113. struct work_struct work;
  114. struct mutex mutex; /* mutual excl. when accessing chip */
  115. int irq;
  116. v4l2_std_id curr_norm;
  117. bool autodetect;
  118. u8 input;
  119. };
  120. #define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler, \
  121. struct adv7180_state, \
  122. ctrl_hdl)->sd)
  123. static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
  124. {
  125. /* in case V4L2_IN_ST_NO_SIGNAL */
  126. if (!(status1 & ADV7180_STATUS1_IN_LOCK))
  127. return V4L2_STD_UNKNOWN;
  128. switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
  129. case ADV7180_STATUS1_AUTOD_NTSM_M_J:
  130. return V4L2_STD_NTSC;
  131. case ADV7180_STATUS1_AUTOD_NTSC_4_43:
  132. return V4L2_STD_NTSC_443;
  133. case ADV7180_STATUS1_AUTOD_PAL_M:
  134. return V4L2_STD_PAL_M;
  135. case ADV7180_STATUS1_AUTOD_PAL_60:
  136. return V4L2_STD_PAL_60;
  137. case ADV7180_STATUS1_AUTOD_PAL_B_G:
  138. return V4L2_STD_PAL;
  139. case ADV7180_STATUS1_AUTOD_SECAM:
  140. return V4L2_STD_SECAM;
  141. case ADV7180_STATUS1_AUTOD_PAL_COMB:
  142. return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
  143. case ADV7180_STATUS1_AUTOD_SECAM_525:
  144. return V4L2_STD_SECAM;
  145. default:
  146. return V4L2_STD_UNKNOWN;
  147. }
  148. }
  149. static int v4l2_std_to_adv7180(v4l2_std_id std)
  150. {
  151. if (std == V4L2_STD_PAL_60)
  152. return ADV7180_INPUT_CONTROL_PAL60;
  153. if (std == V4L2_STD_NTSC_443)
  154. return ADV7180_INPUT_CONTROL_NTSC_443;
  155. if (std == V4L2_STD_PAL_N)
  156. return ADV7180_INPUT_CONTROL_PAL_N;
  157. if (std == V4L2_STD_PAL_M)
  158. return ADV7180_INPUT_CONTROL_PAL_M;
  159. if (std == V4L2_STD_PAL_Nc)
  160. return ADV7180_INPUT_CONTROL_PAL_COMB_N;
  161. if (std & V4L2_STD_PAL)
  162. return ADV7180_INPUT_CONTROL_PAL_BG;
  163. if (std & V4L2_STD_NTSC)
  164. return ADV7180_INPUT_CONTROL_NTSC_M;
  165. if (std & V4L2_STD_SECAM)
  166. return ADV7180_INPUT_CONTROL_PAL_SECAM;
  167. return -EINVAL;
  168. }
  169. static u32 adv7180_status_to_v4l2(u8 status1)
  170. {
  171. if (!(status1 & ADV7180_STATUS1_IN_LOCK))
  172. return V4L2_IN_ST_NO_SIGNAL;
  173. return 0;
  174. }
  175. static int __adv7180_status(struct i2c_client *client, u32 *status,
  176. v4l2_std_id *std)
  177. {
  178. int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
  179. if (status1 < 0)
  180. return status1;
  181. if (status)
  182. *status = adv7180_status_to_v4l2(status1);
  183. if (std)
  184. *std = adv7180_std_to_v4l2(status1);
  185. return 0;
  186. }
  187. static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
  188. {
  189. return container_of(sd, struct adv7180_state, sd);
  190. }
  191. static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  192. {
  193. struct adv7180_state *state = to_state(sd);
  194. int err = mutex_lock_interruptible(&state->mutex);
  195. if (err)
  196. return err;
  197. /* when we are interrupt driven we know the state */
  198. if (!state->autodetect || state->irq > 0)
  199. *std = state->curr_norm;
  200. else
  201. err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
  202. mutex_unlock(&state->mutex);
  203. return err;
  204. }
  205. static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
  206. u32 output, u32 config)
  207. {
  208. struct adv7180_state *state = to_state(sd);
  209. int ret = mutex_lock_interruptible(&state->mutex);
  210. struct i2c_client *client = v4l2_get_subdevdata(sd);
  211. if (ret)
  212. return ret;
  213. /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
  214. * all inputs and let the card driver take care of validation
  215. */
  216. if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input)
  217. goto out;
  218. ret = i2c_smbus_read_byte_data(client, ADV7180_INPUT_CONTROL_REG);
  219. if (ret < 0)
  220. goto out;
  221. ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
  222. ret = i2c_smbus_write_byte_data(client,
  223. ADV7180_INPUT_CONTROL_REG, ret | input);
  224. state->input = input;
  225. out:
  226. mutex_unlock(&state->mutex);
  227. return ret;
  228. }
  229. static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
  230. {
  231. struct adv7180_state *state = to_state(sd);
  232. int ret = mutex_lock_interruptible(&state->mutex);
  233. if (ret)
  234. return ret;
  235. ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
  236. mutex_unlock(&state->mutex);
  237. return ret;
  238. }
  239. static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
  240. struct v4l2_dbg_chip_ident *chip)
  241. {
  242. struct i2c_client *client = v4l2_get_subdevdata(sd);
  243. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
  244. }
  245. static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  246. {
  247. struct adv7180_state *state = to_state(sd);
  248. struct i2c_client *client = v4l2_get_subdevdata(sd);
  249. int ret = mutex_lock_interruptible(&state->mutex);
  250. if (ret)
  251. return ret;
  252. /* all standards -> autodetect */
  253. if (std == V4L2_STD_ALL) {
  254. ret =
  255. i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  256. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
  257. | state->input);
  258. if (ret < 0)
  259. goto out;
  260. __adv7180_status(client, NULL, &state->curr_norm);
  261. state->autodetect = true;
  262. } else {
  263. ret = v4l2_std_to_adv7180(std);
  264. if (ret < 0)
  265. goto out;
  266. ret = i2c_smbus_write_byte_data(client,
  267. ADV7180_INPUT_CONTROL_REG,
  268. ret | state->input);
  269. if (ret < 0)
  270. goto out;
  271. state->curr_norm = std;
  272. state->autodetect = false;
  273. }
  274. ret = 0;
  275. out:
  276. mutex_unlock(&state->mutex);
  277. return ret;
  278. }
  279. static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
  280. {
  281. struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
  282. struct adv7180_state *state = to_state(sd);
  283. struct i2c_client *client = v4l2_get_subdevdata(sd);
  284. int ret = mutex_lock_interruptible(&state->mutex);
  285. int val;
  286. if (ret)
  287. return ret;
  288. val = ctrl->val;
  289. switch (ctrl->id) {
  290. case V4L2_CID_BRIGHTNESS:
  291. ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG, val);
  292. break;
  293. case V4L2_CID_HUE:
  294. /*Hue is inverted according to HSL chart */
  295. ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, -val);
  296. break;
  297. case V4L2_CID_CONTRAST:
  298. ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG, val);
  299. break;
  300. case V4L2_CID_SATURATION:
  301. /*
  302. *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
  303. *Let's not confuse the user, everybody understands saturation
  304. */
  305. ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
  306. val);
  307. if (ret < 0)
  308. break;
  309. ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
  310. val);
  311. break;
  312. default:
  313. ret = -EINVAL;
  314. }
  315. mutex_unlock(&state->mutex);
  316. return ret;
  317. }
  318. static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
  319. .s_ctrl = adv7180_s_ctrl,
  320. };
  321. static int adv7180_init_controls(struct adv7180_state *state)
  322. {
  323. v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
  324. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  325. V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
  326. ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
  327. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  328. V4L2_CID_CONTRAST, ADV7180_CON_MIN,
  329. ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
  330. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  331. V4L2_CID_SATURATION, ADV7180_SAT_MIN,
  332. ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
  333. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  334. V4L2_CID_HUE, ADV7180_HUE_MIN,
  335. ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
  336. state->sd.ctrl_handler = &state->ctrl_hdl;
  337. if (state->ctrl_hdl.error) {
  338. int err = state->ctrl_hdl.error;
  339. v4l2_ctrl_handler_free(&state->ctrl_hdl);
  340. return err;
  341. }
  342. v4l2_ctrl_handler_setup(&state->ctrl_hdl);
  343. return 0;
  344. }
  345. static void adv7180_exit_controls(struct adv7180_state *state)
  346. {
  347. v4l2_ctrl_handler_free(&state->ctrl_hdl);
  348. }
  349. static const struct v4l2_subdev_video_ops adv7180_video_ops = {
  350. .querystd = adv7180_querystd,
  351. .g_input_status = adv7180_g_input_status,
  352. .s_routing = adv7180_s_routing,
  353. };
  354. static const struct v4l2_subdev_core_ops adv7180_core_ops = {
  355. .g_chip_ident = adv7180_g_chip_ident,
  356. .s_std = adv7180_s_std,
  357. };
  358. static const struct v4l2_subdev_ops adv7180_ops = {
  359. .core = &adv7180_core_ops,
  360. .video = &adv7180_video_ops,
  361. };
  362. static void adv7180_work(struct work_struct *work)
  363. {
  364. struct adv7180_state *state = container_of(work, struct adv7180_state,
  365. work);
  366. struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
  367. u8 isr3;
  368. mutex_lock(&state->mutex);
  369. i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  370. ADV7180_ADI_CTRL_IRQ_SPACE);
  371. isr3 = i2c_smbus_read_byte_data(client, ADV7180_ISR3_ADI);
  372. /* clear */
  373. i2c_smbus_write_byte_data(client, ADV7180_ICR3_ADI, isr3);
  374. i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG, 0);
  375. if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
  376. __adv7180_status(client, NULL, &state->curr_norm);
  377. mutex_unlock(&state->mutex);
  378. enable_irq(state->irq);
  379. }
  380. static irqreturn_t adv7180_irq(int irq, void *devid)
  381. {
  382. struct adv7180_state *state = devid;
  383. schedule_work(&state->work);
  384. disable_irq_nosync(state->irq);
  385. return IRQ_HANDLED;
  386. }
  387. static int init_device(struct i2c_client *client, struct adv7180_state *state)
  388. {
  389. int ret;
  390. /* Initialize adv7180 */
  391. /* Enable autodetection */
  392. if (state->autodetect) {
  393. ret =
  394. i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  395. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
  396. | state->input);
  397. if (ret < 0)
  398. return ret;
  399. ret =
  400. i2c_smbus_write_byte_data(client,
  401. ADV7180_AUTODETECT_ENABLE_REG,
  402. ADV7180_AUTODETECT_DEFAULT);
  403. if (ret < 0)
  404. return ret;
  405. } else {
  406. ret = v4l2_std_to_adv7180(state->curr_norm);
  407. if (ret < 0)
  408. return ret;
  409. ret =
  410. i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  411. ret | state->input);
  412. if (ret < 0)
  413. return ret;
  414. }
  415. /* ITU-R BT.656-4 compatible */
  416. ret = i2c_smbus_write_byte_data(client,
  417. ADV7180_EXTENDED_OUTPUT_CONTROL_REG,
  418. ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
  419. if (ret < 0)
  420. return ret;
  421. /* Manually set V bit end position in NTSC mode */
  422. ret = i2c_smbus_write_byte_data(client,
  423. ADV7180_NTSC_V_BIT_END_REG,
  424. ADV7180_NTSC_V_BIT_END_MANUAL_NVEND);
  425. if (ret < 0)
  426. return ret;
  427. /* read current norm */
  428. __adv7180_status(client, NULL, &state->curr_norm);
  429. /* register for interrupts */
  430. if (state->irq > 0) {
  431. ret = request_irq(state->irq, adv7180_irq, 0, KBUILD_MODNAME,
  432. state);
  433. if (ret)
  434. return ret;
  435. ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  436. ADV7180_ADI_CTRL_IRQ_SPACE);
  437. if (ret < 0)
  438. return ret;
  439. /* config the Interrupt pin to be active low */
  440. ret = i2c_smbus_write_byte_data(client, ADV7180_ICONF1_ADI,
  441. ADV7180_ICONF1_ACTIVE_LOW |
  442. ADV7180_ICONF1_PSYNC_ONLY);
  443. if (ret < 0)
  444. return ret;
  445. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR1_ADI, 0);
  446. if (ret < 0)
  447. return ret;
  448. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR2_ADI, 0);
  449. if (ret < 0)
  450. return ret;
  451. /* enable AD change interrupts interrupts */
  452. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR3_ADI,
  453. ADV7180_IRQ3_AD_CHANGE);
  454. if (ret < 0)
  455. return ret;
  456. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR4_ADI, 0);
  457. if (ret < 0)
  458. return ret;
  459. ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  460. 0);
  461. if (ret < 0)
  462. return ret;
  463. }
  464. return 0;
  465. }
  466. static int adv7180_probe(struct i2c_client *client,
  467. const struct i2c_device_id *id)
  468. {
  469. struct adv7180_state *state;
  470. struct v4l2_subdev *sd;
  471. int ret;
  472. /* Check if the adapter supports the needed features */
  473. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  474. return -EIO;
  475. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  476. client->addr, client->adapter->name);
  477. state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
  478. if (state == NULL) {
  479. ret = -ENOMEM;
  480. goto err;
  481. }
  482. state->irq = client->irq;
  483. INIT_WORK(&state->work, adv7180_work);
  484. mutex_init(&state->mutex);
  485. state->autodetect = true;
  486. state->input = 0;
  487. sd = &state->sd;
  488. v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
  489. ret = adv7180_init_controls(state);
  490. if (ret)
  491. goto err_unreg_subdev;
  492. ret = init_device(client, state);
  493. if (ret)
  494. goto err_free_ctrl;
  495. return 0;
  496. err_free_ctrl:
  497. adv7180_exit_controls(state);
  498. err_unreg_subdev:
  499. mutex_destroy(&state->mutex);
  500. v4l2_device_unregister_subdev(sd);
  501. kfree(state);
  502. err:
  503. printk(KERN_ERR KBUILD_MODNAME ": Failed to probe: %d\n", ret);
  504. return ret;
  505. }
  506. static int adv7180_remove(struct i2c_client *client)
  507. {
  508. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  509. struct adv7180_state *state = to_state(sd);
  510. if (state->irq > 0) {
  511. free_irq(client->irq, state);
  512. if (cancel_work_sync(&state->work)) {
  513. /*
  514. * Work was pending, therefore we need to enable
  515. * IRQ here to balance the disable_irq() done in the
  516. * interrupt handler.
  517. */
  518. enable_irq(state->irq);
  519. }
  520. }
  521. mutex_destroy(&state->mutex);
  522. v4l2_device_unregister_subdev(sd);
  523. kfree(to_state(sd));
  524. return 0;
  525. }
  526. static const struct i2c_device_id adv7180_id[] = {
  527. {KBUILD_MODNAME, 0},
  528. {},
  529. };
  530. #ifdef CONFIG_PM
  531. static int adv7180_suspend(struct i2c_client *client, pm_message_t state)
  532. {
  533. int ret;
  534. ret = i2c_smbus_write_byte_data(client, ADV7180_PWR_MAN_REG,
  535. ADV7180_PWR_MAN_OFF);
  536. if (ret < 0)
  537. return ret;
  538. return 0;
  539. }
  540. static int adv7180_resume(struct i2c_client *client)
  541. {
  542. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  543. struct adv7180_state *state = to_state(sd);
  544. int ret;
  545. ret = i2c_smbus_write_byte_data(client, ADV7180_PWR_MAN_REG,
  546. ADV7180_PWR_MAN_ON);
  547. if (ret < 0)
  548. return ret;
  549. ret = init_device(client, state);
  550. if (ret < 0)
  551. return ret;
  552. return 0;
  553. }
  554. #endif
  555. MODULE_DEVICE_TABLE(i2c, adv7180_id);
  556. static struct i2c_driver adv7180_driver = {
  557. .driver = {
  558. .owner = THIS_MODULE,
  559. .name = KBUILD_MODNAME,
  560. },
  561. .probe = adv7180_probe,
  562. .remove = adv7180_remove,
  563. #ifdef CONFIG_PM
  564. .suspend = adv7180_suspend,
  565. .resume = adv7180_resume,
  566. #endif
  567. .id_table = adv7180_id,
  568. };
  569. module_i2c_driver(adv7180_driver);
  570. MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
  571. MODULE_AUTHOR("Mocean Laboratories");
  572. MODULE_LICENSE("GPL v2");