adv7180.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
  126. case ADV7180_STATUS1_AUTOD_NTSM_M_J:
  127. return V4L2_STD_NTSC;
  128. case ADV7180_STATUS1_AUTOD_NTSC_4_43:
  129. return V4L2_STD_NTSC_443;
  130. case ADV7180_STATUS1_AUTOD_PAL_M:
  131. return V4L2_STD_PAL_M;
  132. case ADV7180_STATUS1_AUTOD_PAL_60:
  133. return V4L2_STD_PAL_60;
  134. case ADV7180_STATUS1_AUTOD_PAL_B_G:
  135. return V4L2_STD_PAL;
  136. case ADV7180_STATUS1_AUTOD_SECAM:
  137. return V4L2_STD_SECAM;
  138. case ADV7180_STATUS1_AUTOD_PAL_COMB:
  139. return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
  140. case ADV7180_STATUS1_AUTOD_SECAM_525:
  141. return V4L2_STD_SECAM;
  142. default:
  143. return V4L2_STD_UNKNOWN;
  144. }
  145. }
  146. static int v4l2_std_to_adv7180(v4l2_std_id std)
  147. {
  148. if (std == V4L2_STD_PAL_60)
  149. return ADV7180_INPUT_CONTROL_PAL60;
  150. if (std == V4L2_STD_NTSC_443)
  151. return ADV7180_INPUT_CONTROL_NTSC_443;
  152. if (std == V4L2_STD_PAL_N)
  153. return ADV7180_INPUT_CONTROL_PAL_N;
  154. if (std == V4L2_STD_PAL_M)
  155. return ADV7180_INPUT_CONTROL_PAL_M;
  156. if (std == V4L2_STD_PAL_Nc)
  157. return ADV7180_INPUT_CONTROL_PAL_COMB_N;
  158. if (std & V4L2_STD_PAL)
  159. return ADV7180_INPUT_CONTROL_PAL_BG;
  160. if (std & V4L2_STD_NTSC)
  161. return ADV7180_INPUT_CONTROL_NTSC_M;
  162. if (std & V4L2_STD_SECAM)
  163. return ADV7180_INPUT_CONTROL_PAL_SECAM;
  164. return -EINVAL;
  165. }
  166. static u32 adv7180_status_to_v4l2(u8 status1)
  167. {
  168. if (!(status1 & ADV7180_STATUS1_IN_LOCK))
  169. return V4L2_IN_ST_NO_SIGNAL;
  170. return 0;
  171. }
  172. static int __adv7180_status(struct i2c_client *client, u32 *status,
  173. v4l2_std_id *std)
  174. {
  175. int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
  176. if (status1 < 0)
  177. return status1;
  178. if (status)
  179. *status = adv7180_status_to_v4l2(status1);
  180. if (std)
  181. *std = adv7180_std_to_v4l2(status1);
  182. return 0;
  183. }
  184. static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
  185. {
  186. return container_of(sd, struct adv7180_state, sd);
  187. }
  188. static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  189. {
  190. struct adv7180_state *state = to_state(sd);
  191. int err = mutex_lock_interruptible(&state->mutex);
  192. if (err)
  193. return err;
  194. /* when we are interrupt driven we know the state */
  195. if (!state->autodetect || state->irq > 0)
  196. *std = state->curr_norm;
  197. else
  198. err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
  199. mutex_unlock(&state->mutex);
  200. return err;
  201. }
  202. static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
  203. u32 output, u32 config)
  204. {
  205. struct adv7180_state *state = to_state(sd);
  206. int ret = mutex_lock_interruptible(&state->mutex);
  207. struct i2c_client *client = v4l2_get_subdevdata(sd);
  208. if (ret)
  209. return ret;
  210. /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
  211. * all inputs and let the card driver take care of validation
  212. */
  213. if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input)
  214. goto out;
  215. ret = i2c_smbus_read_byte_data(client, ADV7180_INPUT_CONTROL_REG);
  216. if (ret < 0)
  217. goto out;
  218. ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
  219. ret = i2c_smbus_write_byte_data(client,
  220. ADV7180_INPUT_CONTROL_REG, ret | input);
  221. state->input = input;
  222. out:
  223. mutex_unlock(&state->mutex);
  224. return ret;
  225. }
  226. static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
  227. {
  228. struct adv7180_state *state = to_state(sd);
  229. int ret = mutex_lock_interruptible(&state->mutex);
  230. if (ret)
  231. return ret;
  232. ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
  233. mutex_unlock(&state->mutex);
  234. return ret;
  235. }
  236. static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
  237. struct v4l2_dbg_chip_ident *chip)
  238. {
  239. struct i2c_client *client = v4l2_get_subdevdata(sd);
  240. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
  241. }
  242. static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  243. {
  244. struct adv7180_state *state = to_state(sd);
  245. struct i2c_client *client = v4l2_get_subdevdata(sd);
  246. int ret = mutex_lock_interruptible(&state->mutex);
  247. if (ret)
  248. return ret;
  249. /* all standards -> autodetect */
  250. if (std == V4L2_STD_ALL) {
  251. ret =
  252. i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  253. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
  254. | state->input);
  255. if (ret < 0)
  256. goto out;
  257. __adv7180_status(client, NULL, &state->curr_norm);
  258. state->autodetect = true;
  259. } else {
  260. ret = v4l2_std_to_adv7180(std);
  261. if (ret < 0)
  262. goto out;
  263. ret = i2c_smbus_write_byte_data(client,
  264. ADV7180_INPUT_CONTROL_REG,
  265. ret | state->input);
  266. if (ret < 0)
  267. goto out;
  268. state->curr_norm = std;
  269. state->autodetect = false;
  270. }
  271. ret = 0;
  272. out:
  273. mutex_unlock(&state->mutex);
  274. return ret;
  275. }
  276. static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
  277. {
  278. struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
  279. struct adv7180_state *state = to_state(sd);
  280. struct i2c_client *client = v4l2_get_subdevdata(sd);
  281. int ret = mutex_lock_interruptible(&state->mutex);
  282. int val;
  283. if (ret)
  284. return ret;
  285. val = ctrl->val;
  286. switch (ctrl->id) {
  287. case V4L2_CID_BRIGHTNESS:
  288. ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG, val);
  289. break;
  290. case V4L2_CID_HUE:
  291. /*Hue is inverted according to HSL chart */
  292. ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, -val);
  293. break;
  294. case V4L2_CID_CONTRAST:
  295. ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG, val);
  296. break;
  297. case V4L2_CID_SATURATION:
  298. /*
  299. *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
  300. *Let's not confuse the user, everybody understands saturation
  301. */
  302. ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
  303. val);
  304. if (ret < 0)
  305. break;
  306. ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
  307. val);
  308. break;
  309. default:
  310. ret = -EINVAL;
  311. }
  312. mutex_unlock(&state->mutex);
  313. return ret;
  314. }
  315. static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
  316. .s_ctrl = adv7180_s_ctrl,
  317. };
  318. static int adv7180_init_controls(struct adv7180_state *state)
  319. {
  320. v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
  321. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  322. V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
  323. ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
  324. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  325. V4L2_CID_CONTRAST, ADV7180_CON_MIN,
  326. ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
  327. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  328. V4L2_CID_SATURATION, ADV7180_SAT_MIN,
  329. ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
  330. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  331. V4L2_CID_HUE, ADV7180_HUE_MIN,
  332. ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
  333. state->sd.ctrl_handler = &state->ctrl_hdl;
  334. if (state->ctrl_hdl.error) {
  335. int err = state->ctrl_hdl.error;
  336. v4l2_ctrl_handler_free(&state->ctrl_hdl);
  337. return err;
  338. }
  339. v4l2_ctrl_handler_setup(&state->ctrl_hdl);
  340. return 0;
  341. }
  342. static void adv7180_exit_controls(struct adv7180_state *state)
  343. {
  344. v4l2_ctrl_handler_free(&state->ctrl_hdl);
  345. }
  346. static const struct v4l2_subdev_video_ops adv7180_video_ops = {
  347. .querystd = adv7180_querystd,
  348. .g_input_status = adv7180_g_input_status,
  349. .s_routing = adv7180_s_routing,
  350. };
  351. static const struct v4l2_subdev_core_ops adv7180_core_ops = {
  352. .g_chip_ident = adv7180_g_chip_ident,
  353. .s_std = adv7180_s_std,
  354. .queryctrl = v4l2_subdev_queryctrl,
  355. .g_ctrl = v4l2_subdev_g_ctrl,
  356. .s_ctrl = v4l2_subdev_s_ctrl,
  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");